<?php

namespace App\Danoral\Bancos\sysOLD;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

use App\Danoral\Bancos\BancoView;
use App\Danoral\Bancos\Ingreso;


class PlanillaView extends Model
{
    protected $connection = 'mysql_danoral';
    protected $table = 'planillas_view';
    protected $primaryKey = 'id_Planilla';
    public $incrementing = false;
    public $timestamps = false;

    public $isUsed = false;


    public function getIngreso(): Ingreso {
        $banco = BancoView::where('ctaSysOld', $this->cta_Planilla)->first();

        $ingreso = new Ingreso;
        $ingreso->setBanco($banco);
        $ingreso->idIngreso     = null;
        $ingreso->refIngreso    = $this->num_Planilla;
        $ingreso->typeIngreso   = null;
        $ingreso->idPlanilla    = $this->id_Planilla;
        $ingreso->dateIngreso   = $this->fecha_Planilla;
        $ingreso->montoDT       = $this->montoDT;
        $ingreso->infoBT        =  [
            'fechaValor'    => $this->fechaValor_BM,
            'concepto'      => $this->concepto_BM,
            'descripcion'   => $this->descripcion_BM,
            'info1'         => $this->info1_BM,
            'info2'         => $this->info2_BM,
        ];
        $ingreso->infoCobro     =  [
            'numCaja'       => $this->num_Caja,
            'idListaCobro'  => $this->id_ListaCobros,
        ];

        return $ingreso;
    }

    public static function getPlanillas($filtro){

        //inicializacion where
        $where = [];
        //banco
        if(!empty($filtro['numBanco'])){  $where[] = ['cta_Planilla', $filtro['numBanco']]; }
        //fecha
        if(!empty($filtro['fechaDesde'])){
            $fechaDesde = date("Y-m-d",strtotime($filtro['fechaDesde']));
            $where[] = ['fecha_Planilla', '>=', $fechaDesde]; 
            
            if(!empty($filtro['fechaHasta'])){
                $fechaHasta = date("Y-m-d",strtotime($filtro['fechaHasta']));
                $where[] = ['fecha_Planilla', '<=', $fechaHasta]; 
            }
        }
        //['num_Planilla', 'like', '%'.substr($np->nroRefNP, -4).'%'],

        $planillas = PlanillaView::where($where)
            ->orderBy('fecha_Planilla', 'asc')
            ->orderBy('montoDT', 'asc')
            ->get();

        return $planillas;

    }

    public static function getPlanillas_notiPago($np){

        $dateD  = date("Y-m-d",strtotime($np->datePagoNP." - 1 days"));
        $dateH  = date("Y-m-d",strtotime($np->datePagoNP." + 1 days"));      
        
        $planillas = PlanillaView::where(
                function (Builder $query) use ($np) {
                    $query
                        ->orWhere('num_Planilla', 'like', '%'.substr($np->nroRefNP, -4).'%')
                        ->orWhere([
                            ['montoDT', '<=', $np->montoNP + 30],
                            ['montoDT', '>=', $np->montoNP - 30],
                        ]);
                }
            )
            
            ->where([
                //['cta_Planilla', $np->BancoView->ctaSysOld],
                ['fecha_Planilla', '>=', $dateD],
                ['fecha_Planilla', '<=', $dateH],
                //['num_Planilla', 'like', '%'.substr($np->nroRefNP, -4).'%'],
            ])
            ->orderBy('montoDT', 'asc')
            ->get();

        return $planillas;

    }

}