info('Verificando paquetes por vencer...'); $this->newLine(); // Obtener configuración $notificationDays = array_map('intval', explode(',', $this->option('days'))); $testMode = $this->option('test'); if ($testMode) { $this->warn('TEST'); $this->newLine(); } // Obtener paquetes activos con sus relaciones $activePackages = PackSim::with(['package', 'simCard.clientSims.client']) ->where('is_active', true) ->whereNotNull('activated_at') ->get(); $this->info("paquetes activos: {$activePackages->count()}"); $this->newLine(); $notificationsSent = 0; $packagesChecked = 0; if ($this->option('show-all')) { $this->showAllPackages($activePackages); return Command::SUCCESS; } foreach ($activePackages as $packSim) { $packagesChecked++; // Calcular fecha de vencimiento $activatedAt = Carbon::parse($packSim->activated_at); $expirationDate = $activatedAt->copy()->addDays($packSim->package->period); // Días restante $daysRemaining = (int) now()->diffInDays($expirationDate, false); // Verificar si debe notificar if (in_array($daysRemaining, $notificationDays)) { // Obtener cliente activo $clientRelation = $packSim->simCard->activeClient()->first(); $client = $clientRelation; if (!$client) { $this->warn("SIM {$packSim->simCard->iccid} sin cliente activo"); continue; } // Mostrar información if ($testMode) { $this->displayNotificationDetails($client, $packSim, $daysRemaining); } else { // notificacion real $this->sendWhatsAppNotification($client, $packSim, $daysRemaining); } $notificationsSent++; } } $this->newLine(); $this->info('Resumen'); $this->table( ['Métrica', 'Valor'], [ ['Paquetes revisados', $packagesChecked], ['Notificaciones enviadas', $notificationsSent], ['Modo', $testMode ? 'TEST' : 'PRODUCCIÓN'], ] ); return Command::SUCCESS; } private function displayNotificationDetails($client, $packSim, $daysRemaining) { $this->line('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); $this->info(" NOTIFICACIÓN A ENVIAR:"); $this->line(" Cliente: {$client->full_name}"); $this->line(" Teléfono: {$client->phone}"); $this->line(" Email: {$client->email}"); $this->line(" Paquete: {$packSim->package->name}"); $this->line(" SIM (ICCID): {$packSim->simCard->iccid}"); $this->line(" Activado: " . Carbon::parse($packSim->activated_at)->format('d/m/Y')); $this->line(" Vence en: {$daysRemaining} día(s)"); $this->line('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); $this->newLine(); } private function showAllPackages($packages) { $this->info('Mostrando todos los paquetes activos con sus fechas de vencimiento:'); $this->newLine(); $data = []; foreach ($packages as $packSim) { $activatedAt = Carbon::parse($packSim->activated_at); $expirationDate = $activatedAt->copy()->addDays($packSim->package->period); $daysRemaining = (int) now()->diffInDays($expirationDate, false); $clientRelation = $packSim->simCard->activeClient()->first(); $clientName = $clientRelation ? $clientRelation->full_name : 'Sin cliente activo'; $data[] = [ 'Cliente' => $clientName, 'Paquete' => $packSim->package->name, 'SIM (ICCID)' => $packSim->simCard->iccid, 'Activado' => $activatedAt->format('d/m/Y'), 'Vence el' => $expirationDate->format('d/m/Y'), 'Días restantes' => $daysRemaining, ]; } $this->table( ['Cliente', 'Paquete', 'SIM (ICCID)', 'Activado', 'Vence el', 'Días restantes'], $data ); } }