<?php

namespace App\Danoral\Cuotas;

use App\Danoral\Cuotas\CuotaView;
use App\Http\Resources\CuotaEntidad as CuotaEntidadResources;
use App\Danoral\U;

class CuotaEntidad
{
    public $idCond;
    public $idE;
    public $ano;
    public $mes;

    public $idNP;

    public $tcValorCond;
    public $tcDateCond;

    public $montoTotal;
    public $cuotas = [];


    public static function getCuotasEntidad($idE, $fechaTasa = null){
        $cuotasv = CuotaView::where([
            ['cuotas_view.idE', $idE],
            ['cuotas_view.idDTCuotaCobro', null]
        ])
        ->orderBy('cuotas_view.anoDT', 'asc')
        ->orderBy('cuotas_view.mesDT', 'asc')
        ->orderBy('cuotas_view.idMoneda', 'asc')
        ->get();

        //echo count($cuotasv) . PHP_EOL;
        //var_dump($cuotasv);

        $val_act = "";

        $cuotasEntidad = [];
        $cE = null;
        $montoTotal = 0;
        $montoCuota = 0;
        //$montoCuotaBase = 0;

        //$tcDate = null;
        //$tcValor = 0;
        //$isPeriodoGracia = false;

        if(is_null($fechaTasa)){
            $fechaTasa = date('Y-m-d');
        }

        $sumTotal = 0;

        foreach($cuotasv as $cv){

            //$val = ($cv->anoDT . $cv->mesDT . $cv->typeEmisionCond);
            $val = ($cv->anoDT . $cv->mesDT);


            if($val_act != $val){
                if($cE != null){
                    $cE->montoTotal      = number_format($montoTotal, 2, '.', '');
                    $sumTotal += $montoTotal;

                    array_push($cuotasEntidad, $cE);
                }

                $cE = new CuotaEntidad();
                $montoTotal = 0;
                $montoCuota = 0;
                $montoCuotaBase = 0; //es el monto en VES resultante del calculo de la tasa correspondiente segun su fecha hasta

                $tcDate = null;
                $tcValor = 0;
                $isPeriodoGracia = false;

                $cE->ano = $cv->anoDT;
                $cE->mes = U::getMesTxt($cv->mesDT);
                $cE->typeEmisionCond = $cv->typeEmisionCond;

                $cE->idNP = $cv->idNP;

                $cE->idCond = $cv->idCond;
                $cE->tcValorCond = number_format($cv->valorTC, 2, '.', '');
                $cE->tcDateCond  = is_null($cv->dateValorTC)?null:date("d-m-Y",strtotime($cv->dateValorTC));

                $val_act = $val;
            }

            $montoCuota = floatval($cv->montoDT);

            $cv->calcularTasa($fechaTasa);
            $isPeriodoGracia =  $cv->tcCobro_isPeriodoGracia;
            $tcValor =          $cv->tcCobro_valor;
            $tcDate =           $cv->tcCobro_date;
            $montoCuotaBase =   $cv->tcCobro_montoCuota;

            $montoTotal += $montoCuotaBase;

            $cuota = [
                'idCuota'                   => strval($cv->idCuota),
                'idMoneda'                  => $cv->idMoneda,
                'symbolMoneda'              => $cv->getSymboloMoneda(),
                'monto'                     => number_format($montoCuota,       2, '.', ''),
                'tcCobro_montoCuota'        => number_format($montoCuotaBase,   2, '.', ''),
                'tcEmision_dateVen'         => is_null($tcDate)?null:date("d-m-Y",strtotime($cv->tcEmision_dateVen)),
                'tcCobro_isPeriodoGracia'   => $isPeriodoGracia,
                'tcCobro_valor'             => number_format($tcValor,          2, '.', ''),
                'tcCobro_date'              => is_null($tcDate)?null:date("d-m-Y",strtotime($tcDate)),
            ];
            array_push($cE->cuotas, $cuota);

        }
        if($cE != null){
            $cE->montoTotal      = number_format($montoTotal, 2, '.', '');
            $sumTotal += $montoTotal;

            array_push($cuotasEntidad, $cE);
        }

        return [
            'idE'           => strval($idE),
            'sumTotal'      => number_format($sumTotal, 2, '.', ''),
            'cuotasE'       => CuotaEntidadResources::collection($cuotasEntidad),
        ];
    }


}
