<?php

namespace App\Modules\Libros_electronicos\Reports;


use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use App\Modules\Libros_electronicos\Models\Rphonorario;
use App\Services\UtilitiesService;
use App\User;

class RphonorariosExport implements FromView,ShouldAutoSize
{
	protected $data;
	protected $table;
	protected $service;
    public function __construct($data)
    {
        $this->data = $data;
        $this->table = new Rphonorario();
        $this->service = new UtilitiesService();
    }
    /**
    * @return \Illuminate\Support\Collection
    */
    public function view(): View
    {	
    	$fecha = explode('-', $this->data);
    	$fecha_spanish = $this->service->year_and_month($this->data); 
        $data = $this->set_data_excel($this->table::whereMonth('create_at',$fecha[0])->whereYear('create_at',$fecha[1])->get());
        $user = User::find(Auth::id());
        return view('Libros_electronicos.views.reports.rphonorarios', [
            'data' => $data,
            'fecha_spanish' => $fecha_spanish,
            'mes' => $fecha[0],
            'empresa' => $user
        ]);
    }
    public function set_data_excel($data){
        $data->map(function($c){
            $c->dolares = 0;
            if ($c->divisa == 'US' OR $c->divisa == 'USD') {
                $c->dolares = $c->total_h; 
                $c->totales = $c->total_cambio; 
            }
        });
        return $data;
    }

}
