signature; // Log command start logNotificationStart($commandName); $this->info('Checking NDE Matrix discrepancies...'); $totalCount = 0; $totalSent = 0; $totalRecords = 0; $totalNew = 0; try { // Check 1: NDE Matrix lines not in Weldlog $result1 = $this->checkNDEMatrixNotInWeldlog($totalSent, $totalRecords, $totalNew); if ($result1 > 0) { $totalCount += 1; } // Check 2: Weldlog lines not in Line List $result2 = $this->checkWeldlogNotInLineList($totalSent, $totalRecords, $totalNew); if ($result2 > 0) { $totalCount += 1; } // Check 3: Line List entries with missing NDT ratio $result3 = $this->checkLineListMissingNDT($totalSent, $totalRecords, $totalNew); if ($result3 > 0) { $totalCount += 1; } $duration = microtime(true) - $startTime; logNotificationComplete($commandName, $totalSent, $totalRecords, $totalNew, $duration); $this->info("Total notifications sent: {$totalCount}"); 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; } } /** * Check 1: NDE Matrix rows with empty NDT values (all 6 columns empty) */ private function checkNDEMatrixNotInWeldlog(&$totalSent = null, &$totalRecords = null, &$totalNew = null) { $this->info('Check 1: NDE Matrix rows with empty NDT values...'); $notificationCode = 'notification_nde_matrix_not_in_weldlog'; // Skip if checked recently if (!shouldCheckNotification($notificationCode)) { $this->info('Skipping Check 1: Last notification sent less than 5 minutes ago.'); return 0; } $lastCheck = getLastCheckTimestamp($notificationCode); $query = DB::table('nde_matrices') ->select('id', 'line', 'project', 'type_of_joint') ->whereNotNull('line') ->where('line', '!=', '') ->where(function($q) { $q->where(function($sq) { $sq->whereNull('rt')->orWhere('rt', '')->orWhere('rt', 0)->orWhere('rt', '0'); }) ->where(function($sq) { $sq->whereNull('ut')->orWhere('ut', '')->orWhere('ut', 0)->orWhere('ut', '0'); }) ->where(function($sq) { $sq->whereNull('pt')->orWhere('pt', '')->orWhere('pt', 0)->orWhere('pt', '0'); }) ->where(function($sq) { $sq->whereNull('mt')->orWhere('mt', '')->orWhere('mt', 0)->orWhere('mt', '0'); }) ->where(function($sq) { $sq->whereNull('pmi')->orWhere('pmi', '')->orWhere('pmi', 0)->orWhere('pmi', '0'); }) ->where(function($sq) { $sq->whereNull('ht')->orWhere('ht', '')->orWhere('ht', 0)->orWhere('ht', '0'); }); }); if ($lastCheck !== null) { $query->where(function($q) use ($lastCheck) { $q->where('created_at', '>', $lastCheck) ->orWhere('updated_at', '>', $lastCheck); }); $alreadyNotifiedIds = getAlreadyNotifiedIds($notificationCode); if (!empty($alreadyNotifiedIds)) { $query->whereNotIn('id', $alreadyNotifiedIds); } } $emptyRows = $query->limit(50)->get(); if ($emptyRows->isEmpty()) { $this->info('No new empty NDE Matrix rows found.'); return 0; } $emptyIds = array_values(array_unique($emptyRows->pluck('id')->toArray())); $filterParams = [ 'table' => 'nde_matrices', 'conditions' => [] ]; if ($lastCheck !== null) { $existingIds = getAlreadyNotifiedIds($notificationCode); if (!empty($existingIds)) { $allIds = array_values(array_unique(array_merge($existingIds, $emptyIds))); $newIds = array_diff($emptyIds, $existingIds); if (empty($newIds)) { $this->info('Skipping Check 1: No new records since last notification.'); return 0; } $filterParams['conditions']['id'] = $allIds; } else { $filterParams['conditions']['id'] = $emptyIds; } } else { $filterParams['conditions']['id'] = $emptyIds; } if ($lastCheck !== null && isNotificationDuplicate($notificationCode, $filterParams)) { $this->info('Skipping Check 1: Same issue already notified and no new records.'); return 0; } $newCount = $lastCheck !== null ? count(array_diff($emptyIds, getAlreadyNotifiedIds($notificationCode))) : count($emptyIds); $totalRecords = count($filterParams['conditions']['id']); $message = sprintf( '%d NDE Matrix row(s) have empty NDT values (RT, UT, PT, MT, PMI, HT). Please assign NDT requirements.', $totalRecords ); $sentCount = sendNotification( $notificationCode, $message, null, 'NDE Matrix Empty Row - NDT Assignment Required', $filterParams, $totalRecords ); // Update totals for handle method if ($totalSent !== null) { $totalSent += $sentCount; $totalRecords += $totalRecords; $totalNew += $newCount; } $this->info("Sent {$sentCount} batch notification(s) for empty NDE Matrix rows ({$totalRecords} records, {$newCount} new)."); return $sentCount > 0 ? 1 : 0; } /** * Check 2: Lines in Weldlog but not in Line List */ private function checkWeldlogNotInLineList(&$totalSent = null, &$totalRecords = null, &$totalNew = null) { $this->info('Check 2: Weldlog lines not found in Line List...'); $notificationCode = 'notification_weldlog_not_in_line_list'; // Skip if checked recently if (!shouldCheckNotification($notificationCode)) { $this->info('Skipping Check 2: Last notification sent less than 5 minutes ago.'); return 0; } $lastCheck = getLastCheckTimestamp($notificationCode); $weldlogLinesQuery = DB::table('weld_logs') ->select('line_number', 'project', 'iso_number') ->whereNotNull('line_number') ->where('line_number', '!=', '') ->whereNotNull('welding_date') ->distinct(); if ($lastCheck !== null) { $weldlogLinesQuery->where(function($q) use ($lastCheck) { $q->where('created_at', '>', $lastCheck) ->orWhere('updated_at', '>', $lastCheck); }); } $weldlogLines = $weldlogLinesQuery->limit(50)->get(); // 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']]; } } } $missingLines = []; foreach ($weldlogLines as $weldLine) { if ($lastCheck !== null && in_array($weldLine->line_number, $alreadyNotifiedLines)) { continue; } $existsInLineList = DB::table('line_lists') ->where('line_no', $weldLine->line_number) ->exists(); if (!$existsInLineList) { $missingLines[] = $weldLine->line_number; } } if (empty($missingLines)) { $this->info('No new discrepancies found for Check 2.'); return 0; } if ($lastCheck !== null && !empty($alreadyNotifiedLines)) { $missingLines = array_values(array_unique(array_merge($alreadyNotifiedLines, $missingLines))); } else { $missingLines = array_values(array_unique($missingLines)); } $filterParams = [ 'table' => 'weld_logs', 'conditions' => [ 'line_number' => $missingLines ] ]; if ($lastCheck !== null && isNotificationDuplicate($notificationCode, $filterParams)) { $this->info('Skipping Check 2: Same issue already notified and no new records.'); return 0; } $newCount = $lastCheck !== null ? count(array_diff($missingLines, $alreadyNotifiedLines)) : count($missingLines); $totalRecords = count($missingLines); $message = sprintf( '%d weld log line(s) could not be matched in the Line List. Please review and add the missing line records.', $totalRecords ); $sentCount = sendNotification( $notificationCode, $message, null, 'Weldlog Not in Line List', $filterParams, $totalRecords ); // Update totals for handle method if ($totalSent !== null) { $totalSent += $sentCount; $totalRecords += $totalRecords; $totalNew += $newCount; } $this->info("Sent {$sentCount} batch notification(s) for Check 2 ({$totalRecords} lines, {$newCount} new)."); return $sentCount > 0 ? 1 : 0; } /** * Check 3: Line List entries with missing NDT ratio */ private function checkLineListMissingNDT(&$totalSent = null, &$totalRecords = null, &$totalNew = null) { $this->info('Check 3: Line List entries missing NDT ratio...'); $notificationCode = 'notification_line_list_ndt_missing'; // Skip if checked recently if (!shouldCheckNotification($notificationCode)) { $this->info('Skipping Check 3: Last notification sent less than 5 minutes ago.'); return 0; } $lastCheck = getLastCheckTimestamp($notificationCode); $query = DB::table('line_lists') ->select('line_lists.id', 'line_lists.line_no', 'line_lists.ndt') ->join('weld_logs', 'weld_logs.line_number', '=', 'line_lists.line_no') ->where(function($q) { $q->whereNull('line_lists.ndt') ->orWhere('line_lists.ndt', '') ->orWhere('line_lists.ndt', '0'); }) ->whereNotNull('weld_logs.welding_date') ->distinct(); if ($lastCheck !== null) { $query->where(function($q) use ($lastCheck) { $q->where('line_lists.created_at', '>', $lastCheck) ->orWhere('line_lists.updated_at', '>', $lastCheck) ->orWhere('weld_logs.created_at', '>', $lastCheck) ->orWhere('weld_logs.updated_at', '>', $lastCheck); }); } $missingNDT = $query->limit(50)->get(); if ($missingNDT->isEmpty()) { $this->info('No new discrepancies found for Check 3.'); return 0; } $lineNumbers = array_values(array_unique($missingNDT->pluck('line_no')->toArray())); // 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_no'])) { $alreadyNotifiedLines = is_array($lastParams['conditions']['line_no']) ? $lastParams['conditions']['line_no'] : [$lastParams['conditions']['line_no']]; } } } if ($lastCheck !== null && !empty($alreadyNotifiedLines)) { $lineNumbers = array_values(array_unique(array_merge($alreadyNotifiedLines, $lineNumbers))); } $filterParams = [ 'table' => 'line_lists', 'conditions' => [ 'line_no' => $lineNumbers ] ]; if ($lastCheck !== null && isNotificationDuplicate($notificationCode, $filterParams)) { $this->info('Skipping Check 3: Same issue already notified and no new records.'); return 0; } $newCount = $lastCheck !== null ? count(array_diff($lineNumbers, $alreadyNotifiedLines)) : count($lineNumbers); $totalRecords = count($lineNumbers); $message = sprintf( '%d Line List entry/entries used in Weldlog are missing NDT ratio. Please update the Line List records.', $totalRecords ); $sentCount = sendNotification( $notificationCode, $message, null, 'Line List NDT Missing', $filterParams, $totalRecords ); // Update totals for handle method if ($totalSent !== null) { $totalSent += $sentCount; $totalRecords += $totalRecords; $totalNew += $newCount; } $this->info("Sent {$sentCount} batch notification(s) for Check 3 ({$totalRecords} lines, {$newCount} new)."); return $sentCount > 0 ? 1 : 0; } }