signature; // Log command start logNotificationStart($commandName); $this->info('Checking weldlog records for missing certificates...'); $notificationCode = 'notification_weldlog_certificate_missing'; try { // 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('weld_logs') ->whereNotNull('welding_date') ->where('welding_date', '!=', '') ->where(function($q) { $q->whereNull('certificate_number_of_1') ->orWhere('certificate_number_of_1', '') ->orWhereNull('certificate_number_of_2') ->orWhere('certificate_number_of_2', ''); }); // If not first run, only check new/updated records if ($lastCheck !== null) { $query->where(function($q) use ($lastCheck) { $q->where('created_at', '>', $lastCheck) ->orWhere('updated_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 weldlog.'); $duration = microtime(true) - $startTime; logNotificationComplete($commandName, 0, 0, 0, $duration); return 0; } // Prepare filter params $filterParams = [ 'table' => 'weld_logs', '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 = 'Certificate Number 1-2 Missing'; $message = sprintf( '%d weld log record(s) have missing certificate numbers. Please update certificate_number_of_1/2.', 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 weldlog 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; } } }