signature; // Log command start logNotificationStart($commandName); $this->info('Checking incoming control records for missing certificates...'); try { if (!Schema::hasTable('incoming_controls')) { $this->info('incoming_controls table not found; skipping check.'); $duration = microtime(true) - $startTime; logNotificationSkip($commandName, 'incoming_controls table not found'); return 0; } $notificationCode = 'notification_incoming_control_cert_missing'; // Skip if checked recently (within 5 minutes) if (!shouldCheckNotification($notificationCode)) { $this->info('Skipping: Last notification sent less than 5 minutes ago.'); logNotificationSkip($commandName, 'Last notification sent less than 5 minutes ago'); return 0; } // Get last check time (null = first run, check all records) $lastCheck = getLastCheckTimestamp($notificationCode); $query = DB::table('incoming_controls') ->whereNotNull('created_at') ->where(function($q) { $q->whereNull('certificate_no') ->orWhere('certificate_no', ''); }); // If not first run, only check new records if ($lastCheck !== null) { $query->where('created_at', '>', $lastCheck); // Get already notified IDs $alreadyNotifiedIds = getAlreadyNotifiedIds($notificationCode); if (!empty($alreadyNotifiedIds)) { $query->whereNotIn('id', $alreadyNotifiedIds); } } $ids = $query->pluck('id')->toArray(); if (empty($ids)) { $this->info('No new missing certificates found in incoming control.'); $duration = microtime(true) - $startTime; logNotificationComplete($commandName, 0, 0, 0, $duration); return 0; } // Prepare filter params $filterParams = [ 'table' => 'incoming_controls', 'conditions' => [] ]; // If not first run, merge with existing IDs if ($lastCheck !== null) { $existingIds = getAlreadyNotifiedIds($notificationCode); if (!empty($existingIds)) { $allIds = array_values(array_unique(array_merge($existingIds, $ids))); $newIds = array_diff($ids, $existingIds); if (empty($newIds)) { $this->info('Skipping: No new records since last notification.'); $duration = microtime(true) - $startTime; logNotificationSkip($commandName, 'No new records since last notification'); return 0; } $filterParams['conditions']['id'] = $allIds; } else { $filterParams['conditions']['id'] = $ids; } } else { $filterParams['conditions']['id'] = $ids; } // Check for duplicate if ($lastCheck !== null && isNotificationDuplicate($notificationCode, $filterParams)) { $this->info('Skipping: Same issue already notified and no new records.'); $duration = microtime(true) - $startTime; logNotificationSkip($commandName, 'Same issue already notified and no new records'); return 0; } $newCount = $lastCheck !== null ? count(array_diff($ids, getAlreadyNotifiedIds($notificationCode))) : count($ids); $title = 'Incoming Control Certificate Missing'; $message = sprintf( '%d incoming control record(s) have empty certificate numbers. Please update the certificate information.', count($filterParams['conditions']['id']) ); $sentCount = sendBatchNotificationWithFilter( $notificationCode, $title, $message, $filterParams, count($filterParams['conditions']['id']) ); $duration = microtime(true) - $startTime; $totalRecords = count($filterParams['conditions']['id']); // Log completion logNotificationComplete($commandName, $sentCount, $totalRecords, $newCount, $duration); $this->info("Sent batch notification for incoming control certificates ({$totalRecords} records, {$newCount} new)."); return 0; } catch (\Exception $e) { $duration = microtime(true) - $startTime; logNotificationError($commandName, $e->getMessage(), [ 'file' => $e->getFile(), 'line' => $e->getLine(), 'duration' => $duration, ]); $this->error("Error: " . $e->getMessage()); return 1; } } }