<?php

namespace App\Danoral\Transacciones\sysOLD;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Danoral\Transacciones\Trans as TransSys;

class Trans extends Model
{
    protected $connection = 'mysql_danoral';
    protected $table = 'trans';
    protected $primaryKey = 'idT';
    public $incrementing = false;
    public $timestamps = false;

    public $idT;
    public $trans;

    public function myLoad($whereLoad, $insertData, $ano = null, $mes = null)
    {
        $cn = (new TransSys)->getCN_jsys($ano, $mes);

        $this->trans = DB::connection($cn)
            ->table('trans')
            ->where($whereLoad)
            ->first();

        if($this->trans){
            //echo 'TransOld : myLoad : existe' . PHP_EOL;
            $this->idT = $this->trans->idT;
        }else{
            //echo 'TransOld : myLoad : NO existe' . PHP_EOL;
            $this->myInsert($insertData, $cn);
        }

        //echo 'idT : ' . $this->idT . PHP_EOL;
    }

    public function myInsert($data, $cn)
    {
        $idT = DB::select('SELECT nextval(\'admin."old_trans_idT_seq"\'::regclass)');
        $this->idT = $idT[0]->nextval;

        $data['idT'] = $this->idT;

        DB::connection($cn)
            ->table('trans')->insert($data);
    }
}
