signature; // Log command start logNotificationStart($commandName); $this->info('Checking Support Log vs Weldlog count discrepancies by line...'); $notificationCode = 'notification_support_log_missing_in_weldlog'; 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); // Get line_number based weld count from supports (erection_type = 'weld') $supportWeldCountsQuery = DB::table('supports') ->select('line_number', DB::raw('COUNT(*) as support_count')) ->where('erection_type', 'weld') ->whereNotNull('line_number') ->where('line_number', '!=', '') ->groupBy('line_number'); // If not first run, only check lines with new/updated supports or welds if ($lastCheck !== null) { $supportWeldCountsQuery->where(function($q) use ($lastCheck) { $q->where('created_at', '>', $lastCheck) ->orWhere('updated_at', '>', $lastCheck); }); } $supportWeldCounts = $supportWeldCountsQuery->get()->keyBy('line_number'); if ($supportWeldCounts->isEmpty()) { $this->info('No new welded supports found in Support Log.'); $duration = microtime(true) - $startTime; logNotificationComplete($commandName, 0, 0, 0, $duration); return 0; } $this->info("Found {$supportWeldCounts->count()} unique lines with welded supports"); // Get already notified line numbers $alreadyNotifiedLines = []; if ($lastCheck !== null) { $lastNotification = \App\Models\Notification::where('notification_code', $notificationCode) ->whereNotNull('filter_params') ->orderBy('created_at', 'DESC') ->first(); if ($lastNotification && $lastNotification->filter_params) { $lastParams = is_array($lastNotification->filter_params) ? $lastNotification->filter_params : json_decode($lastNotification->filter_params, true); if (isset($lastParams['conditions']['line_number'])) { $alreadyNotifiedLines = is_array($lastParams['conditions']['line_number']) ? $lastParams['conditions']['line_number'] : [$lastParams['conditions']['line_number']]; } } } $discrepantLines = []; foreach ($supportWeldCounts as $lineNum => $supportData) { // Skip if already notified (unless first run) if ($lastCheck !== null && in_array($lineNum, $alreadyNotifiedLines)) { continue; } // Count matching records in weld_logs (piping_type = 'Process Piping Support') $weldlogCount = DB::table('weld_logs') ->where('line_number', $lineNum) ->where('piping_type', 'Process Piping Support') ->count(); // If counts don't match, add to discrepancy list if ($supportData->support_count != $weldlogCount) { $discrepantLines[] = [ 'line' => $lineNum, 'support_count' => $supportData->support_count, 'weldlog_count' => $weldlogCount ]; } } if (empty($discrepantLines)) { $this->info('No new mismatched counts found.'); $duration = microtime(true) - $startTime; logNotificationComplete($commandName, 0, 0, 0, $duration); return 0; } $lineNumbers = array_column($discrepantLines, 'line'); // Merge with existing lines if not first run if ($lastCheck !== null && !empty($alreadyNotifiedLines)) { $lineNumbers = array_values(array_unique(array_merge($alreadyNotifiedLines, $lineNumbers))); } else { $lineNumbers = array_values(array_unique($lineNumbers)); } $filterParams = [ 'table' => 'supports', 'conditions' => [ 'line_number' => $lineNumbers ] ]; // 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($lineNumbers, $alreadyNotifiedLines)) : count($lineNumbers); $message = sprintf( '%d line(s) have mismatched support weld counts between Support Log (erection_type=weld) and Weldlog (piping_type=Process Piping Support). Please review and reconcile.', count($lineNumbers) ); $sentCount = sendNotification( $notificationCode, $message, null, 'Support Log - Weldlog Count Mismatch', $filterParams, count($lineNumbers) ); $duration = microtime(true) - $startTime; $totalRecords = count($lineNumbers); // Log completion logNotificationComplete($commandName, $sentCount, $totalRecords, $newCount, $duration); $this->info("Sent {$sentCount} batch notification(s) for {$totalRecords} line(s) with mismatched counts ({$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; } } }