<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Danoral\Dolarizacion\TasaCambio;

class Cuota extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {

        if(is_null($this->fecha_calculo_tasa)){
            $tcCalculoTasa = TasaCambio::getTasaDia();
            //$fecha_calculoTasa = strtotime(date("d-m-Y H:i:00",time()));
            $fecha_calTasaMS = time();
        }else{
            $tcCalculoTasa = TasaCambio::getTasaFecha($this->fecha_calculo_tasa);
            $fecha_calTasaMS = strtotime($this->fecha_calculo_tasa);
        }

        if(!is_null($tcCalculoTasa)){
            $tcValor = $tcCalculoTasa->valorTC;
            $tcDate = $tcCalculoTasa->dateValorTC;
        }

        $isPeriodoGracia = false;

        $montoVes = $this->montoDT * $tcValor;

        if(!is_null($this->idCond)){
            if(!is_null($this->idTC)){
                if(strtotime($this->dateVenTasa) >= $fecha_calTasaMS){
                    $isPeriodoGracia = true;
                    $tcValor = $this->valorTC;
                    $tcDate = $this->dateValorTC;
                    $montoVes = $this->montoOrigen;
                }
            }
        }

        return [
            'idCuota'           => strval($this->idCuota),
            'idCond'            => strval($this->idCond),
            'idE'               => strval($this->idE),
            'ano'               => $this->anoDT,
            'mes'               => $this->mesDT,
            'monto'             => $this->montoDT*1,
            'idMoneda'          => $this->idMoneda,
            'montoVES'          => $montoVes,
            'fecha_calTasa'     => date("Y-m-d",$fecha_calTasaMS),
            'tcDate'            => $tcDate,
            'tcValor'           => $tcValor,
            'isPeriodoGracia'   => $isPeriodoGracia,
            'dateVenTasa'       => $this->dateVenTasa,
            'isCobrado'         => !is_null($this->idDTCuotaCobro),
        ];
    }
}
