184 lines
7.1 KiB
PHP
184 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Carbon\Carbon;
|
|
|
|
class NotificationCheckNDTRequestOverdue extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'notifications:check-ndt-request-overdue';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Check if NDT request dates are older than 10 days without test results';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$startTime = microtime(true);
|
|
$commandName = $this->signature;
|
|
|
|
// Log command start
|
|
logNotificationStart($commandName);
|
|
|
|
$this->info('Checking NDT request dates older than 10 days...');
|
|
|
|
$notificationCode = 'notification_ndt_request_overdue';
|
|
|
|
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);
|
|
|
|
$overdueThreshold = Carbon::now()->subDays(10)->toDateString();
|
|
|
|
$invalidIds = [];
|
|
|
|
// Get already notified IDs
|
|
$alreadyNotifiedIds = $lastCheck !== null ? getAlreadyNotifiedIds($notificationCode) : [];
|
|
|
|
// Request date and result fields to check
|
|
$requestChecks = [
|
|
['request' => 'rt_request_date', 'result' => 'rt_result', 'name' => 'RT'],
|
|
['request' => 'vt_request_date', 'result' => 'vt_result', 'name' => 'VT'],
|
|
['request' => 'ut_request_date', 'result' => 'ut_result', 'name' => 'UT'],
|
|
['request' => 'pt_request_date', 'result' => 'pt_result', 'name' => 'PT'],
|
|
['request' => 'mt_request_date', 'result' => 'mt_result', 'name' => 'MT'],
|
|
['request' => 'ht_request_date', 'result' => 'ht_result', 'name' => 'HT'],
|
|
['request' => 'pmi_request_date', 'result' => 'pmi_result', 'name' => 'PMI'],
|
|
['request' => 'pwht_request_date', 'result' => 'pwht_result', 'name' => 'PWHT'],
|
|
['request' => 'ferrite_request_date', 'result' => 'ferrite_result', 'name' => 'Ferrite'],
|
|
];
|
|
|
|
foreach ($requestChecks as $check) {
|
|
$this->info("Checking {$check['name']} requests...");
|
|
|
|
$query = DB::table('weld_logs')
|
|
->whereNotNull($check['request'])
|
|
->where($check['request'], '!=', '')
|
|
->where($check['request'], '!=', '0000-00-00')
|
|
->where($check['request'], '<=', $overdueThreshold)
|
|
->where(function($q) use ($check) {
|
|
$q->whereNull($check['result'])
|
|
->orWhere($check['result'], '')
|
|
->orWhere($check['result'], 'Pending');
|
|
});
|
|
|
|
// If not first run, only check records that might have changed
|
|
// (request date is still overdue, but result might have been updated)
|
|
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) . " overdue {$check['name']} requests (10+ days)");
|
|
$invalidIds = array_merge($invalidIds, $ids);
|
|
}
|
|
}
|
|
|
|
$invalidIds = array_values(array_unique($invalidIds));
|
|
|
|
if (empty($invalidIds)) {
|
|
$this->info('No new overdue NDT requests found. All requests within 10 days or have results.');
|
|
$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 request dates older than 10 days without test results (RT, VT, UT, PT, MT, HT, PWHT). Please follow up with testing laboratory.',
|
|
count($filterParams['conditions']['id'])
|
|
);
|
|
|
|
$sentCount = sendNotification(
|
|
$notificationCode,
|
|
$message,
|
|
null,
|
|
'NDT Request Overdue (10+ Days)',
|
|
$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} overdue NDT request(s) ({$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;
|
|
}
|
|
}
|
|
}
|
|
|