<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Query\Builder;

use App\Danoral\Bancos\Banco;
use App\Danoral\Bancos\sysOLD\Banco as BancoJSYS;
use App\Danoral\Bancos\sysOLD\Planilla;
use App\Danoral\Cobranzas\NotiPagoCuotas;
use App\Danoral\Cuotas\Cuota;
use App\Danoral\Condominios\Condominio;
use App\Danoral\Condominios\CondominioItems;
use App\Danoral\Cuotas\CuotaView;
use App\Danoral\Transacciones\TransDT;
use App\Danoral\Bancos\MercantilAPI\MercantilAPI;
use App\Danoral\Cobranzas\Cobros\Cobro;
use App\Http\Resources\Cobro as CobroResource;
use App\Danoral\Cobranzas\Cobros\CobroDTView;
use App\Danoral\Cobranzas\Cobros\CobroView;
use App\Danoral\Entidades\Entidad;
use App\Danoral\Entidades\SyncEntidades;
use App\GO\Token;
use App\Danoral\Transacciones\Trans;
use App\Danoral\U;

class Exec extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'exec';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Ejecuta comandos';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->info($this->description);
        /** INICIO ------------------------------------------------------------- */

        /*
        MercantilAPI::c2p_compra([
            "destination_mobile_number" => '584241513063',   # movil cliente
            "destination_id"            => 'V18367443',      # cedula cliente
            "destination_bank_id"       => '0105',           # banco cliente
            "twofactor_auth"            => '00001111',       # clave temporal
            "amount"                    => 100.00,           # monto compra
            "invoice_number"            => '00000004',       # numero factura ref   
        ]);
        */

        /*
        MercantilAPI::search([
            "destination_mobile_number" => '584166227839',   # movil cliente
            "amount"                    => 25.00,            # monto compra
            "payment_reference"         => '909560006014',   #
            "trx_date"                  => '2023-04-05',     # 
        ]);
        */
        
        /*
        $this->addBancoJSYS([
            'idE'           => 100100020000001203,
            'numCTA_banco'  => '01340440254401037157', //los primeros 4 digitos identifican el banco
            'ctaSysOld'     => 34,
            'codCTA'        => '10201',
            'idMoneda'      => 1001,
            'zona_banco'    => 4,
            'addJSys'       => true,
        ]);
        */
        
        
        //$this->degenerarCond2();
        //Cuota::eliminarCuotaInicial(100100020000001198);
        //Entidad::actualizarEdificios();        
        //$this->MigrarCodigosClientes();
        //$this->restablecerCodCliente();
        //$this->EncryptTest();
        //$this->dolarizarCuotasIniciales();
        //$this->actualizarCondominios(100100020000000480); //8624  AGUA DE COCO
        //$this->repararNotipagosCuotas();
        //Planilla::migrarPlanillas();
        //Token::generarTokensCondominios(2023, 12);
        //$this->migrarCuotas();
        $this->generarCobrosTdtCFP(); //***************** boton caja
        //TransDT::generarVinculacionesDolarizadas(100100020000002038);
        //$this->eliminarCuota(100100030000061868, 2023, 12);
        //$this->eliminarCuota_idCuotas(100100240000204142, 100100240000204196);
        //$this->getCobrosDTview();
        //$this->deleteCobroDuplicados();


        /*
        $datos = DB::select('   
            SELECT jsonb_object_keys(datose)
            FROM admin.entidades
            ');

        foreach( $datos as $d ){
            echo $d;
        }
        */


        /** FIN ---------------------------------------------------------------- */
        $this->info('');
        $this->info('<- FIN');
        return Command::SUCCESS;
    }

    private function degenerarCond2(){
        $this->info('degenerar condominio 2 (refactorizado 2024-12-18)');

        //Condominio
        $cond = new Condominio;
        $cond->setSchema_ano(2024);
        $cond = $cond::find(100100240000000756);
        
        if($cond){
            echo "si existe cond: $cond->idCond";
            Condominio::degenerarCond($cond);
        }
    }

    private function repararNotipagosCuotas() {
        $this->info('reparar Notipagos Cuotas');

        $NPCs = NotiPagoCuotas::whereIn('idCuota', function (Builder $query) 
            {
            $query->select('idCuota')
                ->from('admin.noti_pagos_cuotas')
                ->groupBy('idCuota')
                ->havingRaw('count("idCuota") > ?', [1]);
            })
            ->orderBy('idCuota')
            ->orderBy('idNP')
            ->get();
    
        $iCount = 1;
        $idCuotaActual = 0;
        $iRep = 1;
        if($NPCs) {
            foreach($NPCs as $npc) {
                if(($idCuotaActual != $npc->idCuota)){
                    echo " - $iRep" . PHP_EOL;
                    echo "$iCount : $npc->idCuota - $npc->idNP";
                    $idCuotaActual = $npc->idCuota;
                    $iCount++;
                    $iRep = 1;
                    
                    /*
                    NotiPagoCuotas::where([
                        ['idCuota', $npc->idCuota],
                        ['idNP', $npc->idNP]
                    ])
                    ->delete();
                      */                  
                }else{
                    $iRep++;
                }
            }
            echo " - $iRep" . PHP_EOL;
        }
    }

    private function actualizarCondominios($idE){
        $this->info('actualizarCondominios');

        $isBorrado = false;

        $condominios = Condominio::where('idE', $idE)
            ->orderBy('anoCond', 'asc')
            ->orderBy('mesCond', 'asc')
            ->get();

        foreach($condominios as $cond){
            echo "$cond->idCond - ($cond->anoCond/$cond->mesCond):" . PHP_EOL;

            //BORRADO
            if($isBorrado) {
                echo 'delete' . PHP_EOL;
                DB::table('admin.transdt_cambio')->delete();
                $this->info('  ---> transdt_cambio                     : delete');
                DB::table('admin.transdt')->where('typeDT', TransDT::$TYPE_EMISION_CUOTA_BS)->delete();
                $this->info('  ---> transdt 31 - TYPE_EMISION_CUOTA_BS : delete');
                DB::table('admin.transdt')->where('typeDT', TransDT::$TYPE_EMISION_ITEM_BS)->delete();
                $this->info('  ---> transdt 47 - TYPE_EMISION_ITEM_BS  : delete');
            }

            //CUOTAS $$$
            echo 'cuotas' . PHP_EOL;
            $cuotav = CuotaView::Where([
                    ['entidades.idESuper', $idE],
                    ['idMoneda', 1002],
                    ['anoDT', $cond->anoCond],
                    ['mesDT', $cond->mesCond]
                ])
                ->whereNotNull('idCond')
                ->leftJoin('admin.entidades', 'entidades.idE', '=', 'cuotas_view.idE')
                ->orderBy('anoDT', 'asc')
                ->orderBy('mesDT', 'asc')
                ->get();
                    
            //ITEMS CONDOMINIO $$$
            echo 'items Condominios' . PHP_EOL;
            $itemsConds =  CondominioItems::where([
                    ['idCond', $cond->idCond],
                    ['idMoneda', 1002]
                ])
                ->orderBy('ctaDT', 'asc')
                ->get();


            DB::beginTransaction();
            try{

                foreach($cuotav as $cv) {
                    $cv->upgradeCond();
                }

                foreach($itemsConds as $ic){
                    $ic->upgradeCond();
                }

                DB::commit();
                echo 'commit *' . PHP_EOL;

            }catch (\Exception $e) {
                DB::rollback();

                echo '+++++++++++++++++++++++++++++++++++++++++++++++  rollback'.PHP_EOL;
                var_dump($e->getMessage());
            }
        }
    }

    private function addBancoJSYS($datos){
        $this->info('addBancoJSYS');

        $arrBanco = [
            'codBanco'          => substr($datos['numCTA_banco'], 0, 4),
            'idE'               => $datos['idE'],
            'typeCuenta'        => Banco::$BANCO_TYPE_CORRIENTE,
            'nroCuenta'         => $datos['numCTA_banco'],
            'idMoneda'          => $datos['idMoneda'],
            'visiblePago'       => true,
            'heredable'         => true,
            'ctaSysOld'         => $datos['ctaSysOld'],
            'codCTA'            => $datos['codCTA'],
        ];

        $banco = new Banco;
        $banco->fill($arrBanco);

        BancoJSYS::addBancoJSYS($banco, $datos['zona_banco'], $datos['addJSys']);

    }

    private function EncryptTest(){
        $this->info('EncryptTest');

        /*
        $configargs = array(
            //"config" => "/opt/cpanel/ea-openssl11/etc/pki/tls/openssl.cnf",
            'private_key_bits'=> 2048, //2048
            'default_md' => "sha256", //sha256
            "private_key_type" => OPENSSL_KEYTYPE_RSA,
        );
        
        // Create the keypair
        $res=openssl_pkey_new($configargs);
        // Get private key
        openssl_pkey_export($res, $privKey,NULL,$configargs);
        var_dump($privKey);

        echo PHP_EOL;
        echo $privKey;
        echo PHP_EOL;
        
        // Get public key
        $publickey=openssl_pkey_get_details($res);
        //var_dump($publickey);

        $publickey=$publickey["key"];
        var_dump($publickey);

        $publickey=openssl_pkey_get_details($res);
        //var_dump($publickey);

        $publickey=$publickey["key"];
        var_dump($publickey);
        */

        $pk = config('key.private');
        echo $pk . PHP_EOL;

        $kh = openssl_pkey_get_private($pk);

        if($kh){
            var_dump($kh);
            $detalles = openssl_pkey_get_details($kh);
            //var_dump($detalles);
            $publickey=$detalles["key"];
            echo $publickey;
        }else{
            echo 'error key' . PHP_EOL;
            var_dump(openssl_error_string());
        }
    }

    private function dolarizarCuotasIniciales(){
        $this->info("Dolarizar Cuotas Iniciales");

        Cuota::dolarizarCuotasIniciales('1493');

    }

    private function getCobrosDTview(){
        $this->info("Obtiene los cobros desde cobrosDTview");

        //CobrosDTViews
        $cobrosTDTv = CobroDTView
            ::Where('codEdifCuota', '1461')
            //::Where('idCobro', 100100260000000124)
            ->orderBy('idCobro', 'asc')
            ->orderBy('anoDT', 'asc')
            ->orderBy('mesDT', 'asc')
            ->orderBy('idMoneda', 'asc')
            ->get();

        CobroDTView::getCobros($cobrosTDTv);
    }

    private function MigrarCodigosClientes(){
        $this->info('Migrar Codigos Clientes entre entidades por cambio de entidad edificio');

        $codE_origen  = 102001167; //3359
        $codE_destino = 102000973; //1385

        $e_apto_origen = DB::connection('mysql_danoral')
        ->table('entidades')
        ->leftJoin('entidadesapto', 'entidades.id_Entidad', '=', 'entidadesapto.id_Entidad')
        ->where([
            ['id_TipoEntidad', 3],
            ['cod_Entidad', '<>', ''],
            ['id_EntidadPadre', $codE_origen],
        ])
        ->get();

        $i_apto_edif = 0;
        foreach ($e_apto_origen as $a) {
            echo $i_apto_edif++ . " - " . $a->id_Entidad . " -> " . $a->cod_Entidad ."(". $a->codCliente_EA .")". PHP_EOL;

            //busca el apto en destino
            $ea = DB::connection('mysql_danoral')
            ->table('entidades')
            ->where([
                ['id_TipoEntidad', 3],
                ['id_EntidadPadre', $codE_destino],
                ['cod_Entidad', $a->cod_Entidad],
            ])
            ->first();

            if($ea){
                echo $ea->id_Entidad ." - ". $ea->cod_Entidad ." == ". trim($a->cod_Entidad) ." : ";

                $res = 1;
                
                //asigna el nuevo codigo cliente destino
                $res = DB::connection('mysql_danoral')
                    ->table('entidadesapto')
                    ->where('id_Entidad', $ea->id_Entidad)
                    ->update(
                        ['codCliente_EA' => $a->codCliente_EA]
                    );
                
                //asigna null al codigo cliente origen
                $res = DB::connection('mysql_danoral')
                    ->table('entidadesapto')
                    ->where('id_Entidad', $a->id_Entidad)
                    ->update(
                        ['codCliente_EA' => null]
                    );
                
                echo $res . PHP_EOL;

            }else{
                echo 'ERROR: codigo de apto no encontrado' . PHP_EOL;
            }
        }
    }

    private function restablecerCodCliente(){
        $this->info('restablece los Codigos Clientes de app > jsys');

        $idEdif = 100100020000001163; //3357

        $aptos = Entidad::Where([
            ['idESuper', $idEdif]
        ])
        ->orderBy(DB::raw("FORMAT('%10s', \"datose\"->'cod'->>'value')::bytea"), 'asc')
        ->get();

        $i_apto_edif = 1;
        foreach($aptos as $a){
            $datose = json_decode($a->datose);

            $idE = SyncEntidades::cambioID_apto_R($a->idE);
            $codCliente = $datose->apto_cod_cliente->value;

            echo $i_apto_edif++ . " - " . $idE . " -> " . $codCliente ." ( ". $datose->cod->value ." )". PHP_EOL;
            
            
            $res = DB::connection('mysql_danoral')
                ->table('entidadesapto')
                ->where('id_Entidad', $idE)
                ->update(
                    ['codCliente_EA' => $codCliente]
                );

            echo $res . PHP_EOL;
                
        }
    }

    private function MigrarMontoOrigen3061(){
        $this->info('Migrar los monto origen de la deuda anterior faltante');

        $recibos = DB::connection('mysql_danoral20')
        ->table('recibos')
        ->leftJoin('itemsdt', 'itemsdt.idDT', '=', 'recibos.idDT_Recibo')
        ->leftJoin('entidades', 'entidades.id_Entidad', '=', 'recibos.id_Entidad')
        ->where([
            ['entidades.id_EntidadPadre', 102000783],
        ])
        ->orderBy('recibos.ano_Recibo', 'asc')
        ->orderBy('recibos.mes_Recibo', 'asc')
        ->orderBy('entidades.cod_Entidad', 'asc')
        ->get();

        foreach($recibos as $r){            

            $idE_apto = SyncEntidades::cambioID_apto($r->id_Entidad);

            $cuotaView = CuotaView::firstWhere([
                ['idE', '=', $idE_apto],
                ['anoDT', '=', $r->ano_Recibo],
                ['mesDT', '=', $r->mes_Recibo],
            ]);

            if($cuotaView){
                echo $r->cod_Entidad . ' : ' . $r->ano_Recibo . ' / ' . $r->mes_Recibo . ' ->  ( ' . $r->montoDT . ' ) ' . PHP_EOL;

                $cuota = Cuota::find($cuotaView->idCuota);

                if($cuota){
                    echo "--> $cuotaView->idCuota : $cuotaView->montoDT | ";
                    echo "ENCONTRADO" . PHP_EOL;
                    $cuota->montoOrigen = $r->montoDT;
                    //$cuota->save();
                    echo "GUARDADO : $cuota->montoOrigen " . PHP_EOL;
                }else{
                    echo "************************************* NO ENCONTRADA" . PHP_EOL;
                    return;
                }
            }
        }

        $this->info("- end ");
    }

    private function upgradeCondominios(){
        $this->info('Regenerar las cuotas y los condominios para su actualizacion');

        $idEdif = 100100020000002038;
        $ano = 2024;
        $mes = 1;

        //CONDOMINIOS
        $cond = Condominio::firstWhere([
            ['idE', $idEdif],
            ['anoCond', $ano],
            ['mesCond', $mes]
        ]);
        
        
        //BORRADO
        /*
        echo 'delete' . PHP_EOL;
        DB::table('admin.transdt_cambio')->delete();
        $this->info('  ---> transdt_cambio                     : delete');
        DB::table('admin.transdt')->where('typeDT', TransDT::$TYPE_EMISION_CUOTA_BS)->delete();
        $this->info('  ---> transdt 31 - TYPE_EMISION_CUOTA_BS : delete');
        DB::table('admin.transdt')->where('typeDT', TransDT::$TYPE_EMISION_ITEM_BS)->delete();
        $this->info('  ---> transdt 47 - TYPE_EMISION_ITEM_BS  : delete');
        */

        //CUOTAS $$$
        echo 'cuotas' . PHP_EOL;
        $cuotav = CuotaView::Where([
            ['entidades.idESuper', $idEdif],
            ['idMoneda', 1002],
            ['anoDT', $ano],
            ['mesDT', $mes]
        ])
        ->whereNotNull('idCond')
        ->leftJoin('admin.entidades', 'entidades.idE', '=', 'cuotas_view.idE')
        ->orderBy('anoDT', 'asc')
        ->orderBy('mesDT', 'asc')
        ->get();
                
        foreach($cuotav as $cv) {
            $cv->generarVinculo();
        }


        //ITEMS CONDOMINIO $$$
        echo 'items Condominios' . PHP_EOL;
        $itemsConds =  CondominioItems::where([
            ['idCond', $cond->idCond],
            ['idMoneda', 1002]
            ])
            ->orderBy('ctaDT', 'asc')
            ->get();

        foreach($itemsConds as $ic){
            $ic->generarVinculo();
        }

    }

    private function reconversionTransInicial(){
        $type_trans = 2002;
        $codE = '3350';

        $trans = Trans::firstWhere([
            ['typeT', $type_trans],
            ['refT', $codE]
        ]);

        if(is_null($trans)){
            $this->info('no se encontro trans inicial');
            return;
        }

        //Cuotas
        $transDT = TransDT::where([
            ['idT', $trans->idT],
        ])->get();

        foreach($transDT as $tdt){

            echo $tdt->montoDT .' -> ';

            $tdt->montoDT = round($tdt->montoDT/10000)/100;

            echo $tdt->montoDT . ' : ';

            $tdt->save();

            echo 'save' . PHP_EOL;


        }

        $this->info('<-- FIN');

    }

    private function migrarCuotas(){
        $this->info('Migra las cuotas de un edif dolarizado a otro');

        $idEdif_Origen  = 100100020000001088; //4263
        $idEdif_Destino = 100100020000001180; //3362

        $typeT  = 2002;
        $refT   = '33622024';
        $dateT  = '2024-06-01 00:00:00';
        $descT  = "Condominio Inicial $refT ";

        echo "INICIO: migrar deuda $refT" . PHP_EOL;

        
        //Carga o crea la Trans Condominio
        $trans = Trans::myLoad([
            ['typeT',   $typeT],
            ['refT',    $refT]
        ],[
            'ctaT'          => null,
            'typeT'         => $typeT,
            'refT'          => $refT,
            'dateT'         => $dateT,
            'descT'         => $descT,
            'enableT'       => true,
            'idU'           => 1,
        ], 2025);
        

        $aptos = Entidad::Where([
            ['idESuper', $idEdif_Destino]
        ])
        ->orderBy(DB::raw("FORMAT('%10s', \"datose\"->'cod'->>'value')::bytea"), 'asc')
        ->get();

        foreach($aptos as $a){
            $datose = json_decode($a->datose);

            if( ! isset($datose->cod->value)){
                echo "ERROR | codE no encontrado : $a->idE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"; 
            }

            $codE = $datose->cod->value;

            echo "a_Destino: ". $a->idE . '( ' . $codE . ') : ';

            $aptoOrigen = Entidad::firstWhere([
                ['idESuper', $idEdif_Origen],
                ['datose->cod->value', $codE]
            ]);

            if($aptoOrigen){
                echo "a_Origen: ". $aptoOrigen->idE . PHP_EOL;

                $cuotas = CuotaView::getCuotasViewPendientes_entidad($aptoOrigen->idE);
            
                if($cuotas){
                    foreach($cuotas as $c){
                        echo "      ->  $c->idCuota ($c->anoDT/$c->mesDT) | $c->montoDT | $c->idMoneda | montoOrigen: $c->montoOrigen  " . PHP_EOL;

                        DB::beginTransaction();
                        try{
                            //crear 130  - Cuotas
                            $iDT_cuota = new TransDT;
                            $iDT_cuota->fill([
                                'ctaDT'         => 130,
                                'dhDT'          => 0,
                                'montoDT'       => $c->montoDT, //$montoUSD,
                                'typeDT'        => TransDT::$TYPE_CUOTA,
                                'anoDT'         => $c->anoDT,
                                'mesDT'         => $c->mesDT,
                                'idE'           => $a->idE,
                                'idT'           => $trans->idT,
                                'idMoneda'      => $c->idMoneda
                            ]);
                            $iDT_cuota->save();
            
                            echo 'idDT : ' . $iDT_cuota->idDT;
            
                            /*
                            $iDT_cuota_des = new TransDTDes;
                            $iDT_cuota_des->idDT = $iDT_cuota->idDT;
                            $iDT_cuota_des->descripcionDT = ' ( ' . $r->montoDT . ' ) VES @ ' . $date . ' : ' . number_format($tasa->valorTC, 2, ',', '.');
                            $iDT_cuota_des->save();
                            */
            
                            $cuota = new Cuota;
                            $cuota->fill([
                                'idCond'        => null,
                                'idE'           => $a->idE,
                                'idDTCuota'     => $iDT_cuota->idDT,
                                'montoOrigen'   => $c->montoOrigen,  //se utiliza solo para los recibos migrados a dolares para mantener el monto origen en Bs
                            ]);
                            $cuota->save();
            
                            DB::commit();

                            echo ' | OK ' . PHP_EOL;
            
                        }catch (\Exception $e) {
                            DB::rollback();
            
                            echo '+++++++++++++++++++++++++++++++++++++++++++++++  rollback'.PHP_EOL ;
                            var_dump($e->getMessage());
                        }
                    }
                }

            }else{
                echo 'no se encontro el apto' . PHP_EOL;
            }
            
        }

        $this->info('<-- FIN');

    }

    private function MigrarCuotasGaleon(){
        $this->info('Migra las cuotas saldo inicial 3350 desde el 8246');

        $idEdif      = 100100020000001139;
        $idEdifDesde = 100100020000001024;

        $type_trans = 2002;
        $ref_trans = '3350';

        echo "INICIO: migrar deuda $ref_trans" . PHP_EOL;

        //Carga o crea la Trans Condominio
        $trans = Trans::myLoad([
            ['typeT', $type_trans],
            ['refT',$ref_trans]
        ],[
            'ctaT'          => null,
            'typeT'         => $type_trans,
            'refT'          => $ref_trans,
            'dateT'         => '2022-06-30 00:00:00',
            'descT'         => 'Inicial ' . $ref_trans,
            'enableT'       => true,
            'idU'           => 1,
        ], 2025);


        $aptos = Entidad::Where([
            ['idESuper', $idEdif]
        ])
        ->orderBy(DB::raw("FORMAT('%10s', \"datose\"->'cod'->>'value')::bytea"), 'asc')
        ->get();

        foreach($aptos as $a){
            $datose = json_decode($a->datose);

            if( ! isset($datose->cod->value)){
                echo "codE no encontrado : $a->idE"; 
            }

            $codE = $datose->cod->value;

            echo $a->idE . '( ' . $codE . ') : ';

            $aptoDesde = Entidad::firstWhere([
                ['idESuper', $idEdifDesde],
                ['datose->cod->value', $codE]
            ]);

            if($aptoDesde){
                echo $aptoDesde->idE . PHP_EOL;



                switch($a->idE){
                    case 100100030000060082: //01E
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060083: //01F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060084: //02A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060085: //02B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060086: //02C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                            
                    case 100100030000060087: //02D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060089: //02F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060090: //03A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060091: //03B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060092: //03C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                    case 100100030000060093: //03D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060094: //03E
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060095: //03F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060096: //04A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060097: //04B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                            
                    case 100100030000060098: //04C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060099: //04D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060100: //04E
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060102: //05A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060103: //05B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                    case 100100030000060104: //05C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060105: //05D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060106: //05E
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060107: //05F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060108: //06A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                            
                    case 100100030000060109: //06B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060110: //06C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060111: //06D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060113: //06F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060114: //07A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                    case 100100030000060115: //07B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060116: //07C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060117: //07D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060118: //07E
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060120: //08A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                            
                    case 100100030000060121: //08B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060122: //08C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060123: //08D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060124: //08E
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060125: //08F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                    case 100100030000060082: //09A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060083: //01F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060084: //02A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060085: //02B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060086: //02C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                            
                    case 100100030000060087: //02D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060089: //02F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060090: //03A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060091: //03B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060092: //03C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                    case 100100030000060093: //03D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060094: //03E
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060095: //03F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060096: //04A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060097: //04B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                            
                    case 100100030000060098: //04C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060099: //04D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060100: //04E
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060102: //05A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060103: //05B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                    case 100100030000060104: //05C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060105: //05D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060106: //05E
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060107: //05F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060108: //06A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                            
                    case 100100030000060109: //06B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060110: //06C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060111: //06D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060113: //06F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060114: //07A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                    case 100100030000060115: //07B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060116: //07C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060117: //07D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060118: //07E
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060120: //08A
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
                            
                    case 100100030000060121: //08B
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060122: //08C
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060123: //08D
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;
    
                    case 100100030000060124: //08E
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                        break;

                    case 100100030000060125: //08F
                        $where = [
                            ['idE', $aptoDesde->idE],
                            ['anoDT', 2021],
                            ['mesDT', 6],['mesDT', 7],['mesDT', 8],['mesDT', 9],['mesDT', 10],['mesDT', 11],
                        ];
                    break;


                }

                $cuotas = CuotaView::Where([
                    ['idE', $aptoDesde->idE]
                ])
                ->orderBy('anoDT', 'asc')
                ->orderBy('mesDT', 'asc')
                ->get();

                if($cuotas){
                    foreach($cuotas as $c){
                        echo "      ->  $c->idCuota ($c->anoDT/$c->mesDT) " . PHP_EOL;




                        //DB::beginTransaction();
                        try{
                            //crear 130  - Cuotas
                            $iDT_cuota = new TransDT;
                            $iDT_cuota->fill([
                                'ctaDT'         => 130,
                                'dhDT'          => 0,
                                'montoDT'       => $c->montoDT, //$montoUSD,
                                'typeDT'        => TransDT::$TYPE_CUOTA,
                                'anoDT'         => $c->anoDT,
                                'mesDT'         => $c->mesDT,
                                'idE'           => $c->idE,
                                'idT'           => $trans->idT,
                                'idMoneda'      => 1002 //VES 1002//USD
                            ]);
                            //$iDT_cuota->save();
            
                            echo 'idDT : ' . $iDT_cuota->idDT . PHP_EOL;
            
                            /*
                            $iDT_cuota_des = new TransDTDes;
                            $iDT_cuota_des->idDT = $iDT_cuota->idDT;
                            $iDT_cuota_des->descripcionDT = ' ( ' . $r->montoDT . ' ) VES @ ' . $date . ' : ' . number_format($tasa->valorTC, 2, ',', '.');
                            $iDT_cuota_des->save();
                            */
            
                            $cuota = new Cuota;
                            $cuota->fill([
                                'idCond'        => null,
                                'idE'           => $c->idE,
                                'idDTCuota'     => $iDT_cuota->idDT,
                                //'montoOrigen'   => $r->montoDT,  //se utiliza solo para los recibos migrados a dolares para mantener el monto origen en Bs
                            ]);
                            //$cuota->save();
            
                            //DB::commit();
            
                        }catch (\Exception $e) {
                            //DB::rollback();
            
                            echo '+++++++++++++++++++++++++++++++++++++++++++++++  rollback'.PHP_EOL ;
                            var_dump($e->getMessage());
                        }
            

                    }
                }

            }else{
                echo 'no se encontro el apto' . PHP_EOL;
            }
            
        }

        $this->info('<-- FIN');
        

    }

    //se remplazo por killprocess.php
    private function procesosLinux(){
        $this->info('procesos linux');

        $procesos = preg_split('/[\n]+/', shell_exec('pgrep php-cgi'));
        print_r($procesos);

        while(count($procesos) > 10) {

            $pkill = shell_exec("pkill -9 -o -e php-cgi");
            echo "kill: $pkill" . PHP_EOL;

            echo PHP_EOL;

            $procesos = preg_split('/[\n]+/', shell_exec('pgrep php-cgi'));
            print_r($procesos);            

            sleep(4);

        } 

        echo "... fin" . PHP_EOL;

    }

    private function generarCobrosTdtCFP(){
        $this->info('Generar el detalle tdt_CFP de los cobros');

        Cobro::generarCobrosTdtCFP();

        $this->info('<-- FIN');
        
    }

    private function eliminarCuota($idE_apto, $ano, $mes){
        $type_trans = 2002;
        
        $u = new U;
        $u->isEcho = true;
        $u->eLinea('<--- eliminar cuota' . $idE_apto);

        $entidad = Entidad::find($idE_apto);

        if($entidad){
            $schema = $entidad->getSchema_ano(2025);
            $u->eLinea('schema: '.$schema);

            $datose = json_decode($entidad->datose); 
            if(!isset($datose->cod->value)){
                $u->eLinea('La entidad no tiene codigo');
                return;
            }

            $codA = $datose->cod->value;
            $u->eLinea('codApto: ' . $codA);


            DB::beginTransaction();
            try {
    
                //Cuotas-----------------------------------------------------------
                //SELECT CUOTAS
                $u->eLinea('* SELECT CUOTAS --------------------------------------------');

                //Cuotas
                $u->eLinea('Cuotas');
                $cuotas = DB::table("$schema.cuotas")
                    ->where([
                        ['transdt.idE', $idE_apto],
                        ['transdt.anoDT', $ano],
                        ['transdt.mesDT', $mes],
                    ])
                    ->Join("$schema.transdt", 'transdt.idDT', '=', 'cuotas.idDTCuota')
                    ->whereNull('cuotas.idDTCuotaCobro')
                    ->get();
                $u->eLinea(count($cuotas));

                $logList = Cuota::eliminarCuotas($cuotas, $schema);
                $u->addLogList($logList);

                DB::commit();
                $u->eLinea('*commit');
            
            }catch (\Exception $e) {
                DB::rollback();
    
                $u->eLinea('+++++++++++++++++++++++++++++++++++++++++++++++  rollback');
                var_dump($e->getMessage());
            }

        }
        return $u->logList;
    }

    private function eliminarCuota_idCuotas($idCuotaDesde, $idCuotaHasta, $schemaAno = 2025){
        $u = new U;
        $u->isEcho = true;
        $u->eLinea('<--- eliminar cuota | desde: '.$idCuotaDesde.' hasta: '.$idCuotaHasta);

        $cuotaDesde = new Cuota;
        $cuotaDesde->setSchema_ano($schemaAno);
        $cuotaDesde->find($idCuotaDesde);

        if($cuotaDesde){
            $schema = $cuotaDesde->getSchema_ano($schemaAno);
            $u->eLinea('schema: '.$schema);

            DB::beginTransaction();
            try {
    
                //Cuotas-----------------------------------------------------------
                //SELECT CUOTAS
                $u->eLinea('* SELECT CUOTAS --------------------------------------------');

                //Cuotas
                $u->eLinea('Cuotas');
                $cuotas = DB::table("$schema.cuotas")
                    ->where([
                        ['idCuota','>=', $idCuotaDesde],
                        ['idCuota','<=', $idCuotaHasta],
                    ])
                    ->Join("$schema.transdt", 'transdt.idDT', '=', 'cuotas.idDTCuota')
                    ->whereNull('cuotas.idDTCuotaCobro')
                    ->get();
                $u->eLinea(count($cuotas));

                $logList = Cuota::eliminarCuotas($cuotas, $schema);
                $u->addLogList($logList);

                DB::commit();
                $u->eLinea('*commit');
            
            }catch (\Exception $e) {
                DB::rollback();
    
                $u->eLinea('+++++++++++++++++++++++++++++++++++++++++++++++  rollback');
                var_dump($e->getMessage());
            }

        }else{
            $u->eLinea('No se encontro la cuota desde: '.$idCuotaDesde);
        }
        return $u->logList;
    }

    private function deleteCobroDuplicados(){
        $this->info('delecte Cobros duplicados');

        $codEdif = '4255';
        $cobrosVDT = CobroView::whereIn('idCobro', function ($query) use ($codEdif){
                    $query->select('idCobro')->distinct()
                        ->from('admin26.cobros_dt_view')
                        ->where('codEdifCuota', $codEdif);
                })
                ->orderBy('idCobro', 'asc')
                ->get();

        $cobros = Cobro::getCobros($cobrosVDT);

        echo "cobros: ".count($cobros)." *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*". PHP_EOL;

        $i = 0;
        foreach($cobros as $cobro) {
            echo ++$i ." - ". $cobro->idCobro . PHP_EOL;

            $idDT_ok = true;
            foreach($cobro->tdtCobros as $tdtC) {
                echo "      ". $tdtC->cuota->idDTCuotaCobro ." <-> ". $tdtC->idDT;
                echo ($tdtC->cuota->idDTCuotaCobro == $tdtC->idDT?" : OK":" : X"). PHP_EOL;
                
                $idDT_ok &= $tdtC->cuota->idDTCuotaCobro == $tdtC->idDT;
                

            }
            $isPlanilla = true;
            foreach($cobro->CFP as $cfp) {
                echo "      Planilla: ". $cfp->idPlanilla . PHP_EOL;
                $isPlanilla &= !empty($cfp->idPlanilla);
            }
           
            echo ".... idDT: $idDT_ok | planilla: $isPlanilla".PHP_EOL;
            echo PHP_EOL;

            if(($idDT_ok || $isPlanilla)){
                echo "------> NO BORRAR ".PHP_EOL;
            }else{
                echo "------> BORRAR ".PHP_EOL;
                //$cobro->deleteCobro();

                //return 0;
            }
            echo  PHP_EOL;

            //$cobroJSON = json_encode(new CobroResource($cobro), JSON_PRETTY_PRINT);

            

            
        }


    }


}
