<?php

namespace App\Danoral\Cobranzas\Cobros;

use Illuminate\Support\Collection;

use App\Danoral\ModelBase;
use App\Danoral\Transacciones\TransDT;
use App\Danoral\Cuotas\CuotaView;
use App\Danoral\U;

class TransDTCobro extends ModelBase
{
    protected $tableName = 'transdt_cobros';
    protected $primaryKey = 'idDT';
    protected $keyType = 'bigint';
    public $incrementing = false;
    public $timestamps = false;

    protected $fillable = [
        'idCobro',
        'idCuota',
        'idCaja',
        'idNP'
    ];

    public $tDT;
    public $cuota; //CuotaView

    public $codEdif     = '';
    public $codApto     = '';
    public $ref         = '';

    function __construct() {
        $this->table = parent::$SCHEMA .'.'. $this->tableName;
    }

    public function setIdDT($idDT) {
        $this->idDT = $idDT;
        if(isset($this->cuota)) {
            $this->cuota->idDTCuotaCobro = $idDT;
        }
    }

    public function newTransDT($iCobro): TransDTCobro{
        $this->tDT = new TransDT;

        switch($iCobro['typeDT']){
            case 21:

                $cuota = CuotaView::firstWhere('cuotas_view.idCuota', $iCobro['idCuota']);
                
                if($cuota){

                    $this->idCuota = $iCobro['idCuota'];
                    $this->cuota = $cuota;

                    $this->tDT->fill([
                        'ctaDT'         => 130,
                        'dhDT'          => TransDT::$dhH,
                        'montoDT'       => $cuota->montoDT,
                        'typeDT'        => TransDT::$TYPE_CUOTA_COBRO,
                        'anoDT'         => $cuota->anoDT,
                        'mesDT'         => $cuota->mesDT,
                        'idE'           => $cuota->idE,
                        'idT'           => null,
                        'idMoneda'      => $cuota->idMoneda,
                    ]);
                }               
                break;
        }

        return $this;
    }

    /*

    SELECT * 
    FROM "admin26"."transdt_cobros"
    LEFT JOIN "admin26"."cuotas" ON "cuotas"."idCuota" = "transdt_cobros"."idCuota"
    WHERE "idDTCuotaCobro" != "idDT"

    SELECT * 
    FROM "admin26"."transdt_cobros"
    LEFT JOIN "admin26"."cuotas" ON "cuotas"."idCuota" = "transdt_cobros"."idCuota"
    JOIN "admin"."entidades" ON "entidades"."idE" = "cuotas"."idE"
    WHERE "idDTCuotaCobro" != "idDT"
    AND "entidades"."idESuper" = 100100020000001036  

    */

}