Files
citrus-cms/app/Console/Commands/NotificationCheckWeldlogTestDates.php
T
2026-04-28 21:14:25 +03:00

178 lines
6.4 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class NotificationCheckWeldlogTestDates extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'notifications:check-weldlog-test-dates';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check if NDT test dates are before welding_date in weld_logs';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$startTime = microtime(true);
$commandName = $this->signature;
// Log command start
logNotificationStart($commandName);
$this->info('Checking weldlog test dates vs welding dates...');
$notificationCode = 'notification_weldlog_test_date_invalid';
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);
$invalidIds = [];
// Test date fields to check
$testDateFields = [
'rt_test_date' => 'RT',
'date_of_vt' => 'VT',
'ut_test_date' => 'UT',
'pt_test_date' => 'PT',
'mt_test_date' => 'MT',
'ht_test_date' => 'HT',
'pmi_test_date' => 'PMI',
'pwht_date' => 'PWHT',
'date_of_ferrite_check' => 'Ferrite',
];
// Get already notified IDs
$alreadyNotifiedIds = $lastCheck !== null ? getAlreadyNotifiedIds($notificationCode) : [];
foreach ($testDateFields as $testField => $testName) {
$this->info("Checking {$testName} test dates...");
$query = DB::table('weld_logs')
->whereNotNull('welding_date')
->where('welding_date', '!=', '')
->where('welding_date', '!=', '0000-00-00')
->whereNotNull($testField)
->where($testField, '!=', '')
->where($testField, '!=', '0000-00-00')
->whereColumn($testField, '<', 'welding_date');
// 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);
});
if (!empty($alreadyNotifiedIds)) {
$query->whereNotIn('id', $alreadyNotifiedIds);
}
}
$ids = $query->pluck('id')->toArray();
if (!empty($ids)) {
$this->warn("Found " . count($ids) . " records with {$testName} test date before welding date");
$invalidIds = array_merge($invalidIds, $ids);
}
}
$invalidIds = array_values(array_unique($invalidIds));
if (empty($invalidIds)) {
$this->info('No new invalid test dates found. All test dates are >= welding_date.');
$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 && !empty($alreadyNotifiedIds)) {
$allIds = array_values(array_unique(array_merge($alreadyNotifiedIds, $invalidIds)));
$newIds = array_diff($invalidIds, $alreadyNotifiedIds);
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'] = $invalidIds;
}
// 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($invalidIds, $alreadyNotifiedIds)) : count($invalidIds);
$message = sprintf(
'%d weld log record(s) have NDT test dates that are before welding_date. Please review and correct (RT, VT, UT, PT, MT, HT, PWHT).',
count($filterParams['conditions']['id'])
);
$sentCount = sendNotification(
$notificationCode,
$message,
null,
'Weldlog Test Date Before Welding Date',
$filterParams,
count($filterParams['conditions']['id'])
);
$duration = microtime(true) - $startTime;
$totalRecords = count($filterParams['conditions']['id']);
// Log completion
logNotificationComplete($commandName, $sentCount, $totalRecords, $newCount, $duration);
$this->warn("Sent notification for {$totalRecords} record(s) with invalid test dates ({$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;
}
}
}