<?php

namespace App\Console\Commands;

use App\Models\CoEnte;
use App\Models\CoSocios;
use App\Models\CtClienteDeuda;
use App\Models\Suspensiones;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class SuspensionCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'suspension:cron';

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

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $damefecha = Carbon::now()->subHour(5);
        $dataDeudads = CtClienteDeuda::where('fecha_vencimiento', '<', $damefecha)->where('saldo', '>', 0)->get();
        foreach ($dataDeudads as $value) {
            //proceso de bloqueo de Socio
            $existeSuspencion = Suspensiones::where('id_socio', $value->id_socio)->count();
            if ($existeSuspencion == 0) {
                $dataSocio =  CoSocios::where('IdSocios', $value->id_socio)->first();
                if ($dataSocio != null) {
                    CoEnte::where('id_ente', $dataSocio->IdEnte)->update(['activo' =>  'I']);
                    $dataSocio->Activo = 'I';
                    $dataSocio->EstacionModificacion = 'S';
                    $dataSocio->save();
                    Suspensiones::create(['id_socio' => $value->id_socio, 'id_deuda' => $value->id_cliente_deuda, 'estado' => 'A', 'fecha_ing' => $damefecha,'observacion'=>'Socio suspendido por tener un saldo pendiente de '.$value->saldo]);
                    $value->aud_mod_usuario = 'B';
                    $value->save();
                }
            }
        }
        $this->info('Proceso de Suspension');
        return 0;
    }
}
