<?php

namespace App\Danoral\Bancos;

use Illuminate\Support\Facades\DB;
use App\Danoral\Entidades\Entidad;
use App\Danoral\ModelBase;

class Banco extends ModelBase
{
    protected $table = 'admin.bancos';
    protected $primaryKey = 'idBanco';
    public $incrementing = true;
    public $timestamps = false;

    protected $fillable = [
        'idBanco',
        'codBanco',
        'idE',
        'typeCuenta',
        'nroCuenta',        
        'idMoneda',
        'visiblePago',
        'heredable',
        'ctaSysOld',
        'codCTA',
        'descripcionBanco',
    ];

    public static $BANCO_TYPE_CORRIENTE = 1;
    public static $BANCO_TYPE_AHORRO = 2;
    public static $BANCO_TYPE_CUSTODIA = 3;

    public function Entidad(){
        return $this->hasOne(Entidad::class, 'idE', 'idE');
    }

    public function getTypeCuenta_text(){
        switch($this->typeCuenta){
            case self::$BANCO_TYPE_CORRIENTE:
                return 'Corriente';
            case self::$BANCO_TYPE_AHORRO:
                return 'Ahorro';
            case self::$BANCO_TYPE_CUSTODIA:
                return 'Custodia';
            default:
                return 'N/A';
        }
    }

    public function getTrans($input = null){

        $idE = $this->idE;
    
        $where = [
            ['enableT', true]
        ];

        if(!is_null($input)){
            if(isset($input['dateD'])){
                $dateD = date("Y-m-d",strtotime($input['dateD']));
                
                if(isset($input['dateH'])){
                    $dateH = date("Y-m-d",strtotime($input['dateH']));
                    $where[] = ['dateT', '>=', $dateD];
                    $where[] = ['dateT', '<=', $dateH];
                }else{
                    $where[] = ['dateT', $dateD];
                }
            }    
        }    

        $bancoT = DB::table('admin.transdt_view')
            ->where([
                ['idE', $idE],
                ['ctaDT', $this->codCTA],
            ])
            ->where($where)
            ->where(function($query) use ($idE)  {
                $query->orWhere('idESuper', $idE)
                    ->orWhere('idE', $idE);
            })
            ->whereNotNull('idT')
            ->orderBy('dateT', 'desc')
            ->limit(50)
            ->get();

        return $bancoT;
    }


}
