Files
2026-04-28 21:14:25 +03:00

179 lines
6.3 KiB
PHP

<?php
namespace App\Services\WeldLogTriggers\Triggers;
use App\Services\WeldLogTriggers\Base\BaseTrigger;
use Illuminate\Support\Facades\Log;
/**
* Test Pack Sync Trigger
*
* Syncs weld log data to test packages when test_package_no changes or new record is inserted.
* This trigger calls the sync view with specific test_package_no filter.
*/
class TestPackSyncTrigger extends BaseTrigger
{
public function getName(): string
{
return 'Test Pack Sync from WeldLog';
}
public function getOrder(): int
{
return 8; // Run before TestPackBaseStatusChangerTrigger (order 9)
}
public function getDependentFields(): array
{
return [
'test_package_no',
'iso_number',
'welding_date',
'type_of_joint',
'nps_1',
'design_area',
'piping_type',
'circuit_number',
'p_id',
'type_of_test',
'test_pressure',
'no_of_the_joint_as_per_as_built_survey', // golden joints
'quantity_of_iso',
// NDT test dates
'rt_test_date',
'ut_test_date',
'pwht_test_date',
'pmi_test_date',
'ferrite_test_date',
'pt_test_date',
'mt_test_date',
'vt_test_date',
// NDT Request Dates and Numbers
'rt_request_date', 'rt_request_no',
'ut_request_date', 'ut_request_no',
'mt_request_no', 'mt_request_date',
'pt_request_no', 'pt_request_date',
'vt_request_no', 'vt_request_date',
'pwht_request_no', 'pwht_request_date',
'ferrite_request_no', 'ferrite_request_date',
'pmi_request_no', 'pmi_request_date',
'ht_request_no', 'ht_request_date',
];
}
/**
* Static registry to track already synced test packages in the current process
* to avoid redundant heavy sync calls.
*/
protected static $isSynced = [];
protected static $lastJobId = null;
protected function process($data, $beforeData, array $context): array
{
// Get current job ID if running in queue
$currentJobId = 'manual';
if (\Illuminate\Support\Facades\Request::header('X-Laravel-Job-Id')) {
$currentJobId = \Illuminate\Support\Facades\Request::header('X-Laravel-Job-Id');
} elseif (function_exists('app') && app()->bound(\Illuminate\Contracts\Queue\Job::class)) {
try {
$currentJobId = resolve(\Illuminate\Contracts\Queue\Job::class)->getJobId();
} catch (\Throwable $e) {
// Fallback if resolution fails despite being bound
}
}
// Reset registry if this is a new job
if (self::$lastJobId !== $currentJobId) {
self::$isSynced = [];
self::$lastJobId = $currentJobId;
}
$changedFields = $context['changed_fields'] ?? [];
$isNewRecord = $context['is_new_record'] ?? false;
// Collect all test package numbers that need to be synced
$testPackageNosToSync = [];
// Current test package
$currentTestPackageNo = $data->test_package_no;
if (!empty($currentTestPackageNo)) {
// Check if already synced in this process
if (!isset(self::$isSynced[$currentTestPackageNo])) {
$testPackageNosToSync[] = $currentTestPackageNo;
}
}
// If test_package_no changed, also sync the old test package
if (in_array('test_package_no', $changedFields) && $beforeData && !empty($beforeData->test_package_no)) {
$oldTestPackageNo = $beforeData->test_package_no;
if ($oldTestPackageNo !== $currentTestPackageNo && !in_array($oldTestPackageNo, $testPackageNosToSync)) {
if (!isset(self::$isSynced[$oldTestPackageNo])) {
$testPackageNosToSync[] = $oldTestPackageNo;
}
Log::info("TestPackSync: test_package_no changed, will sync both old and new", [
'old_test_package_no' => $oldTestPackageNo,
'new_test_package_no' => $currentTestPackageNo,
'weld_log_id' => $data->id
]);
}
}
// If no test packages to sync, return early
if (empty($testPackageNosToSync)) {
Log::info("TestPackSync: No test packages to sync", [
'weld_log_id' => $data->id,
'test_package_no' => $currentTestPackageNo
]);
return [
'success' => true,
'synced' => 0,
'reason' => 'no_test_package'
];
}
$syncedCount = 0;
$errors = [];
// Sync each test package
foreach ($testPackageNosToSync as $testPackageNo) {
try {
// Mark as synced before calling the view to prevent re-entry/collision
self::$isSynced[$testPackageNo] = true;
// Call the sync view with specific test_package_no
$result = view('cron.weld_logs-sync-from-weldlog-to-test-pack', [
'test_package_no' => $testPackageNo
])->render();
Log::info("TestPackSync: Sync completed for test package", [
'test_package_no' => $testPackageNo,
'weld_log_id' => $data->id
]);
$syncedCount++;
} catch (\Throwable $th) {
Log::error("TestPackSync: Sync failed for test package (non-critical)", [
'test_package_no' => $testPackageNo,
'weld_log_id' => $data->id,
'error' => $th->getMessage(),
'file' => $th->getFile(),
'line' => $th->getLine()
]);
$errors[] = [
'test_package_no' => $testPackageNo,
'error' => $th->getMessage()
];
}
}
return [
'success' => empty($errors),
'synced' => $syncedCount,
'test_packages_synced' => $testPackageNosToSync,
'errors' => $errors
];
}
}