FIX: Ajustar cálculo de días restantes para paquetes en el command y controlador
This commit is contained in:
parent
1523cd408d
commit
1ae0dd6b0c
@ -6,38 +6,25 @@
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class CheckExpiringPackages extends Command
|
||||
{
|
||||
/**
|
||||
* Nombre del comando
|
||||
*/
|
||||
protected $signature = 'packages:check-expiring
|
||||
{--days= : Días de anticipación para notificar}
|
||||
{--test : Modo de prueba }
|
||||
{--show-all : Mostrar todos los paquetes con sus fechas de vencimiento}';
|
||||
|
||||
/**
|
||||
* La descripción del comando
|
||||
*/
|
||||
protected $description = 'Verificar paquetes por vencer y enviar notificaciones a los usuarios';
|
||||
|
||||
/**
|
||||
* Ejecutar comando
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->info('Verificando paquetes por vencer...');
|
||||
$this->newLine();
|
||||
|
||||
// Obtener configuración
|
||||
$customDays = $this->option('days');
|
||||
$testMode = $this->option('test');
|
||||
|
||||
if ($testMode) {
|
||||
$this->warn('TEST');
|
||||
$this->warn('MODO TEST');
|
||||
$this->newLine();
|
||||
}
|
||||
|
||||
@ -51,13 +38,12 @@ public function handle()
|
||||
}
|
||||
$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->info("Paquetes activos: {$activePackages->count()}");
|
||||
$this->newLine();
|
||||
|
||||
$notificationsSent = 0;
|
||||
@ -68,17 +54,24 @@ public function handle()
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$now = Carbon::now()->startOfDay();
|
||||
|
||||
foreach ($activePackages as $packSim) {
|
||||
$packagesChecked++;
|
||||
|
||||
// Calcular fecha de vencimiento
|
||||
$activatedAt = Carbon::parse($packSim->activated_at);
|
||||
$expirationDate = $activatedAt->copy()->addDays($packSim->package->period);
|
||||
$expirationDateStart = $expirationDate->copy()->startOfDay();
|
||||
|
||||
// Días restantes
|
||||
$daysRemaining = (int) now()->diffInDays($expirationDate, false);
|
||||
if ($expirationDateStart->isFuture()) {
|
||||
$daysRemaining = (int) $now->diffInDays($expirationDateStart, false);
|
||||
} elseif ($expirationDateStart->isToday()) {
|
||||
$daysRemaining = 0;
|
||||
} else {
|
||||
// Ya expiró, saltar
|
||||
continue;
|
||||
}
|
||||
|
||||
// Determinar si debe notificar según el periodo del paquete
|
||||
$shouldNotify = $this->shouldNotifyForPackage(
|
||||
$packSim->package->period,
|
||||
$daysRemaining,
|
||||
@ -86,7 +79,6 @@ public function handle()
|
||||
);
|
||||
|
||||
if ($shouldNotify) {
|
||||
// Obtener cliente activo
|
||||
$clientRelation = $packSim->simCard->activeClient()->first();
|
||||
$client = $clientRelation;
|
||||
|
||||
@ -95,11 +87,9 @@ public function handle()
|
||||
continue;
|
||||
}
|
||||
|
||||
// Mostrar información
|
||||
if ($testMode) {
|
||||
$this->displayNotificationDetails($client, $packSim, $daysRemaining);
|
||||
} else {
|
||||
// notificacion real
|
||||
$this->sendWhatsAppNotification($client, $packSim, $daysRemaining);
|
||||
}
|
||||
|
||||
@ -121,40 +111,25 @@ public function handle()
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determina si se debe notificar según el periodo del paquete y días restantes
|
||||
*/
|
||||
private function shouldNotifyForPackage($packagePeriod, $daysRemaining, $customDays = null)
|
||||
{
|
||||
// Si se especificaron días personalizados
|
||||
if ($customDays !== null) {
|
||||
$notificationDays = array_map('intval', explode(',', $customDays));
|
||||
return in_array($daysRemaining, $notificationDays);
|
||||
}
|
||||
|
||||
// Lógica según el periodo del paquete
|
||||
switch ($packagePeriod) {
|
||||
case 30:
|
||||
// Paquetes de 30 días: notificar a los 7 días
|
||||
return $daysRemaining === 7;
|
||||
|
||||
case 15:
|
||||
// Paquetes de 15 días: notificar a los 3 días
|
||||
return $daysRemaining === 3;
|
||||
|
||||
case 7:
|
||||
// Paquetes de 7 días: notificar a los 2 días
|
||||
return $daysRemaining === 2;
|
||||
|
||||
default:
|
||||
// Para otros periodos, no notificar (o puedes ajustar la lógica)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Envía notificación por WhatsApp (temporal: solo muestra en consola)
|
||||
*/
|
||||
private function sendWhatsAppNotification($client, $packSim, $daysRemaining)
|
||||
{
|
||||
$this->warn('PRODUCCIÓN: Aquí se enviaría la notificación real por WhatsApp');
|
||||
@ -180,18 +155,27 @@ private function displayNotificationDetails($client, $packSim, $daysRemaining)
|
||||
$this->newLine();
|
||||
}
|
||||
|
||||
|
||||
private function showAllPackages($packages)
|
||||
{
|
||||
$this->info('Mostrando todos los paquetes activos con sus fechas de vencimiento:');
|
||||
$this->newLine();
|
||||
|
||||
$data = [];
|
||||
$now = Carbon::now()->startOfDay();
|
||||
|
||||
foreach ($packages as $packSim) {
|
||||
$activatedAt = Carbon::parse($packSim->activated_at);
|
||||
$expirationDate = $activatedAt->copy()->addDays($packSim->package->period);
|
||||
$daysRemaining = (int) now()->diffInDays($expirationDate, false);
|
||||
$expirationDateStart = $expirationDate->copy()->startOfDay();
|
||||
|
||||
// ✅ CORRECCIÓN: Calcular días restantes correctamente
|
||||
if ($expirationDateStart->isFuture()) {
|
||||
$daysRemaining = (int) $now->diffInDays($expirationDateStart, false);
|
||||
} elseif ($expirationDateStart->isToday()) {
|
||||
$daysRemaining = 0;
|
||||
} else {
|
||||
$daysRemaining = -1; // Expirado
|
||||
}
|
||||
|
||||
$clientRelation = $packSim->simCard->activeClient()->first();
|
||||
$clientName = $clientRelation ? $clientRelation->full_name : 'Sin cliente activo';
|
||||
@ -202,7 +186,7 @@ private function showAllPackages($packages)
|
||||
'SIM (ICCID)' => $packSim->simCard->iccid,
|
||||
'Activado' => $activatedAt->format('d/m/Y'),
|
||||
'Vence el' => $expirationDate->format('d/m/Y'),
|
||||
'Días restantes' => $daysRemaining,
|
||||
'Días restantes' => $daysRemaining >= 0 ? $daysRemaining : 'Expirado',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@ -91,7 +91,18 @@ public function expiring(Request $request)
|
||||
// Calcular fecha de vencimiento
|
||||
$activatedAt = Carbon::parse($packSim->activated_at);
|
||||
$expirationDate = $activatedAt->copy()->addDays($packSim->package->period);
|
||||
$daysRemaining = (int) now()->diffInDays($expirationDate, false);
|
||||
|
||||
$now = Carbon::now()->startOfDay();
|
||||
$expirationDateStart = $expirationDate->copy()->startOfDay();
|
||||
|
||||
if ($expirationDateStart->isFuture()) {
|
||||
$daysRemaining = (int) $now->diffInDays($expirationDateStart, false);
|
||||
} elseif ($expirationDateStart->isToday()) {
|
||||
$daysRemaining = 0;
|
||||
} else {
|
||||
// Ya expiró, no incluir
|
||||
return null;
|
||||
}
|
||||
|
||||
// Determinar si debe incluirse
|
||||
$shouldInclude = $this->shouldIncludePackage(
|
||||
|
||||
@ -23,8 +23,6 @@ public function run(): void
|
||||
$this->call(UserSeeder::class);
|
||||
$this->call(SettingSeeder::class);
|
||||
|
||||
$this->call(ClientSeeder::class);
|
||||
$this->call(SimCardSeeder::class);
|
||||
$this->call(PackageSeeder::class);
|
||||
//$this->call(PackageSeeder::class);
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,7 +36,7 @@
|
||||
Route::resource('sales', SaleController::class);
|
||||
|
||||
Route::get('cash-closes', [CashCloseController::class, 'index']);
|
||||
Route::put('cash-closes/close', [CashCloseController::class, 'CloseCashClose']);
|
||||
Route::put('cash-closes/close', [CashCloseController::class, 'closeCashClose']);
|
||||
Route::get('cash-closes/report', [CashCloseController::class, 'report']);
|
||||
Route::get('cash-closes/export', [CashCloseController::class, 'exportReport']);
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user