191 lines
6.6 KiB
PHP
191 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class NotificationCheckTestLogPDF extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'notifications:check-test-log-pdf';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Check NDT test logs with results but missing PDF reports (VT, RT, UT, PT, MT, HT, PMI, PWHT, Ferrite)';
|
|
|
|
/**
|
|
* Test log tables with their result field names
|
|
*/
|
|
private $testLogs = [
|
|
'v_t_logs' => ['result' => 'vt_result', 'name' => 'VT'],
|
|
'radiographic_tests' => ['result' => 'rt_result', 'name' => 'RT'],
|
|
'ultrasonic_tests' => ['result' => 'ut_result', 'name' => 'UT'],
|
|
'p_t_logs' => ['result' => 'pt_result', 'name' => 'PT'],
|
|
'magnetic_tests' => ['result' => 'mt_result', 'name' => 'MT'],
|
|
'hardness_tests' => ['result' => 'ht_result', 'name' => 'HT'],
|
|
'p_m_i_tests' => ['result' => 'pmi_result', 'name' => 'PMI'],
|
|
'p_w_h_t_s' => ['result' => 'pwht_result', 'name' => 'PWHT'],
|
|
'ferrits' => ['result' => 'ferrite_result', 'name' => 'Ferrite'],
|
|
];
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$startTime = microtime(true);
|
|
$commandName = $this->signature;
|
|
|
|
// Log command start
|
|
logNotificationStart($commandName);
|
|
|
|
$this->info('Checking test logs for missing PDF reports...');
|
|
|
|
$totalNotifications = 0;
|
|
$totalSent = 0;
|
|
$totalRecords = 0;
|
|
$totalNew = 0;
|
|
|
|
try {
|
|
foreach ($this->testLogs as $table => $config) {
|
|
$result = $this->checkTestLog($table, $config['result'], $config['name'], $totalSent, $totalRecords, $totalNew);
|
|
if ($result > 0) {
|
|
$totalNotifications += 1; // Count as notification sent
|
|
}
|
|
}
|
|
|
|
$duration = microtime(true) - $startTime;
|
|
logNotificationComplete($commandName, $totalSent, $totalRecords, $totalNew, $duration);
|
|
|
|
$this->info("Total notifications sent: {$totalNotifications}");
|
|
|
|
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 a specific test log table for missing PDFs
|
|
*/
|
|
private function checkTestLog(string $table, string $resultField, string $testName, &$totalSent, &$totalRecords, &$totalNew): int
|
|
{
|
|
$this->info("Checking {$testName} Log...");
|
|
|
|
$notificationCode = 'notification_test_log_pdf_missing_' . strtolower($testName);
|
|
|
|
// Skip if checked recently (within 5 minutes)
|
|
if (!shouldCheckNotification($notificationCode)) {
|
|
$this->info("Skipping {$testName}: 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($table)
|
|
->whereNotNull($resultField)
|
|
->where($resultField, '!=', '')
|
|
->where($resultField, '!=', 'Cancel')
|
|
->where(function($q) {
|
|
$q->whereNull('report_file')
|
|
->orWhere('report_file', '');
|
|
});
|
|
|
|
// 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 PDFs found in {$testName} Log.");
|
|
return 0;
|
|
}
|
|
|
|
// Prepare filter params
|
|
$filterParams = [
|
|
'table' => $table,
|
|
'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 {$testName}: 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 {$testName}: Same issue already notified and no new records.");
|
|
return 0;
|
|
}
|
|
|
|
$newCount = $lastCheck !== null ? count(array_diff($ids, getAlreadyNotifiedIds($notificationCode))) : count($ids);
|
|
$recordCount = count($filterParams['conditions']['id']);
|
|
$message = sprintf(
|
|
'%d %s test record(s) have results (non-Cancel) but missing PDF report files. Please upload the missing reports.',
|
|
$recordCount,
|
|
$testName
|
|
);
|
|
|
|
$sentCount = sendBatchNotificationWithFilter(
|
|
$notificationCode,
|
|
"{$testName} Test - PDF Report Missing",
|
|
$message,
|
|
$filterParams,
|
|
$recordCount
|
|
);
|
|
|
|
// Update totals for handle method
|
|
$totalSent += $sentCount;
|
|
$totalRecords += $recordCount;
|
|
$totalNew += $newCount;
|
|
|
|
$this->info("Sent batch notification for {$testName} Log ({$recordCount} records, {$newCount} new).");
|
|
return $sentCount > 0 ? 1 : 0;
|
|
}
|
|
}
|
|
|