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

385 lines
14 KiB
PHP

<?php
namespace App\Services\WeldLogTriggers\Triggers;
use App\Services\WeldLogTriggers\Base\BaseTrigger;
use Illuminate\Support\Facades\Log;
/**
* Request Date Operations Trigger
*
* Handles request number generation for all test types (RT, UT, MT, PT, VT, PWHT, Ferrite, PMI)
* Updates request numbers in both weld_logs and test-specific tables
*/
class RequestDateOperationsTrigger extends BaseTrigger
{
public function getName(): string
{
return 'Request Date Operations';
}
public function getOrder(): int
{
return 4;
}
public function getDependentFields(): array
{
return [
'iso_number',
'no_of_the_joint_as_per_as_built_survey',
'rt_request_date', 'rt_request_no', 'test_laboratory_rt',
'ht_request_date', 'ht_request_no', 'test_laboratory_ht',
'ut_request_date', 'ut_request_no', 'test_laboratory_ut',
'mt_request_date', 'mt_request_no', 'test_laboratory_mt',
'pt_request_date', 'pt_request_no', 'test_laboratory_pt',
'vt_request_date', 'vt_request_no', 'test_laboratory_vt',
'pwht_request_date', 'pwht_request_no', 'test_laboratory_pwht',
'ferrite_request_date', 'ferrite_request_no', 'test_laboratory_ferrite',
'pmi_request_date', 'pmi_request_no', 'test_laboratory_pmi',
'rt_test_date',
'ut_test_date',
'mt_test_date',
'pt_test_date',
'vt_test_date',
'pwht_test_date',
'pmi_test_date',
'ferrite_test_date',
];
}
protected function process($data, $beforeData, array $context): array
{
$logs_request_number_pattern = setting("logs_request_number_pattern");
$logTestTypes = log_test_types();
Log::info("Available test types for request date processing", [
'test_types' => array_keys($logTestTypes),
'iso_number' => $data->iso_number,
'joint_no' => $data->no_of_the_joint_as_per_as_built_survey
]);
$testNames = array_keys($logTestTypes);
$processedTests = [];
foreach($testNames AS $testName) {
$result = $this->processTestType(
$testName,
$logTestTypes[$testName],
$data,
$beforeData,
$logs_request_number_pattern
);
if ($result['processed']) {
$processedTests[] = $testName;
}
}
return [
'success' => true,
'processed_test_types' => $processedTests,
'total_processed' => count($processedTests)
];
}
/**
* Process a single test type for request number generation
*/
protected function processTestType(
string $testName,
string $testTableName,
$data,
$beforeData,
string $logs_request_number_pattern
): array {
$requestDate = $testName . "_request_date";
$run = false;
Log::info("Processing test type", [
'test_name' => $testName,
'request_date_field' => $requestDate,
'current_request_date' => $data->$requestDate ?? 'null',
'before_request_date' => $beforeData->$requestDate ?? 'null'
]);
// Check if request date is not empty
if($data->$requestDate != "") {
$requestNo = $testName . "_request_no";
$laboratoryCompany = "test_laboratory_" . $testName;
Log::info("Request date is not empty, checking conditions", [
'test_name' => $testName,
'request_date' => $data->$requestDate,
'current_request_no' => $data->$requestNo ?? 'null',
'current_laboratory' => $data->$laboratoryCompany ?? 'null',
'before_laboratory' => $beforeData->$laboratoryCompany ?? 'null'
]);
// Check conditions to run
if($data->$requestNo == "") {
// Request no not assigned yet
$run = true;
Log::info("Run condition met: Request number is empty", [
'test_name' => $testName,
'reason' => 'request_no_empty'
]);
}
if($data->$laboratoryCompany != $beforeData->$laboratoryCompany) {
// Laboratory changed
$run = true;
Log::info("Run condition met: Laboratory changed", [
'test_name' => $testName,
'reason' => 'laboratory_changed',
'old_laboratory' => $beforeData->$laboratoryCompany ?? 'null',
'new_laboratory' => $data->$laboratoryCompany ?? 'null'
]);
}
$requestDateChange = false;
if($data->$requestDate != $beforeData->$requestDate) {
// Request date changed
$run = true;
Log::info("Run condition met: Request date changed", [
'test_name' => $testName,
'reason' => 'request_date_changed',
'old_date' => $beforeData->$requestDate ?? 'null',
'new_date' => $data->$requestDate
]);
// Check if this date was already used
$isOldSignReqDate = db("weld_logs")
->where([
$requestDate => $data->$requestDate,
$laboratoryCompany => $data->$laboratoryCompany,
])
->where("id", "<>", $data->id)
->first();
if(!$isOldSignReqDate) {
$requestDateChange = true;
Log::info("Request date is unique, will generate new request number", [
'test_name' => $testName,
'request_date' => $data->$requestDate,
'laboratory' => $data->$laboratoryCompany ?? 'null'
]);
} else {
Log::info("Request date already exists, will reuse existing request number", [
'test_name' => $testName,
'request_date' => $data->$requestDate,
'laboratory' => $data->$laboratoryCompany ?? 'null',
'existing_record_id' => $isOldSignReqDate->id ?? 'null'
]);
}
}
if($run) {
$this->generateAndUpdateRequestNumber(
$testName,
$testTableName,
$data,
$requestDate,
$requestNo,
$laboratoryCompany,
$requestDateChange,
$logs_request_number_pattern
);
return ['processed' => true, 'test_name' => $testName];
} else {
Log::info("Request number generation skipped", [
'test_name' => $testName,
'reason' => 'run_condition_false',
'current_request_date' => $data->$requestDate ?? 'null',
'current_request_no' => $data->$requestNo ?? 'null'
]);
}
} else {
Log::info("Test processing skipped", [
'test_name' => $testName,
'reason' => 'request_date_empty',
'request_date_field' => $requestDate
]);
}
return ['processed' => false];
}
/**
* Generate and update request number for a test type
*/
protected function generateAndUpdateRequestNumber(
string $testName,
string $testTableName,
$data,
string $requestDate,
string $requestNo,
string $laboratoryCompany,
bool $requestDateChange,
string $logs_request_number_pattern
) {
Log::info("Starting request number generation process", [
'test_name' => $testName,
'request_date_changed' => $requestDateChange,
'current_request_no' => $data->$requestNo ?? 'null',
'laboratory' => $data->$laboratoryCompany ?? 'null'
]);
$thisRequestNumberPattern = $logs_request_number_pattern;
// Get company code
$companyCode = db("subcontractors")
->where("company_name_en", $data->$laboratoryCompany)
->first();
if(!is_null($companyCode)) {
$companyCode = $companyCode->company_code;
} else {
$companyCode = "";
}
Log::info("Company code resolved", [
'test_name' => $testName,
'laboratory_name' => $data->$laboratoryCompany ?? 'null',
'company_code' => $companyCode
]);
$thisRequestNumberPattern = str_replace("{company_code}", $companyCode, $thisRequestNumberPattern);
// Get counter
if($requestDateChange) {
// Different date, get new counter
$lastRequestNumber = get_counter($companyCode . $testName);
Log::info("Getting new counter for date change", [
'test_name' => $testName,
'counter_key' => $companyCode . $testName,
'new_counter' => $lastRequestNumber
]);
} else {
// Same date, get existing or new counter
$lastRequestNumber = get_counter($companyCode . $testName, "");
Log::info("Getting existing or new counter for same date", [
'test_name' => $testName,
'counter_key' => $companyCode . $testName,
'counter' => $lastRequestNumber
]);
}
$thisRequestNumberPattern = str_replace("{number}", $lastRequestNumber, $thisRequestNumberPattern);
$thisRequestNumberPattern = str_replace("{log_name}", strtoupper($testName), $thisRequestNumberPattern);
Log::info("Request number pattern generated", [
'test_name' => $testName,
'pattern' => $thisRequestNumberPattern,
'counter' => $lastRequestNumber
]);
// Check if same company/date already has a request number today
$todayDataThisCompanyThisTest = db("weld_logs")->where([
$requestDate => $data->$requestDate,
$laboratoryCompany => $data->$laboratoryCompany,
])->first();
if($todayDataThisCompanyThisTest) {
if($todayDataThisCompanyThisTest->$requestNo !="") {
$thisRequestNumberPattern = $todayDataThisCompanyThisTest->$requestNo;
Log::info("Reusing existing request number for same date/company", [
'test_name' => $testName,
'existing_request_no' => $thisRequestNumberPattern,
'existing_record_id' => $todayDataThisCompanyThisTest->id
]);
}
}
// Update weld_logs
$whereData = [
'iso_number' => $data->iso_number,
'no_of_the_joint_as_per_as_built_survey' => $data->no_of_the_joint_as_per_as_built_survey,
];
$updateData = [
$requestNo => $thisRequestNumberPattern,
"updated_at" => simdi()
];
Log::info("Updating weld_logs with request number", [
'test_name' => $testName,
'where_data' => $whereData,
'update_data' => $updateData,
'final_request_number' => $thisRequestNumberPattern
]);
$weldLogUpdateResult = db("weld_logs")
->where($whereData)
->update($updateData);
Log::info("Weld_logs update completed", [
'test_name' => $testName,
'affected_rows' => $weldLogUpdateResult,
'request_number' => $thisRequestNumberPattern
]);
// Update test table
$weldLogData = db("weld_logs")->where($whereData)->first();
$testTableUpdateData = [];
$testTableColumns = table_columns($testTableName);
foreach($weldLogData AS $weldLogColumn => $weldLogValue)
{
if(in_array($weldLogColumn, $testTableColumns))
{
$testTableUpdateData[$weldLogColumn] = $weldLogValue;
}
}
unset($testTableUpdateData['id']);
// Check if control_standart exists in test table columns
if(in_array('control_standart', $testTableColumns)) {
$ndeMatrixInfo = db("nde_matrices")
->where("line", $weldLogData->line_number)
->where("type_of_joint", $weldLogData->type_of_welds)
->first();
if ($ndeMatrixInfo) {
$testTableUpdateData['control_standart'] = $ndeMatrixInfo->control_standart;
Log::info("Added control_standart from NDE Matrix", [
'test_name' => $testName,
'control_standart' => $ndeMatrixInfo->control_standart
]);
} else {
Log::warning("Control standart not found in NDE Matrix", [
'test_name' => $testName,
'line' => $weldLogData->line_number,
'type_of_joint' => $weldLogData->type_of_welds
]);
}
} else {
Log::warning("Control standart column not found in test table", [
'test_name' => $testName,
'test_table' => $testTableName
]);
}
Log::info("Updating test table with weld log data", [
'test_name' => $testName,
'test_table' => $testTableName,
'where_data' => $whereData,
'update_fields_count' => count($testTableUpdateData)
]);
$testTableUpdateResult = db($testTableName)
->updateOrInsert($whereData, $testTableUpdateData);
Log::info("Test table update completed", [
'test_name' => $testName,
'test_table' => $testTableName,
'operation_result' => $testTableUpdateResult ? 'success' : 'failed'
]);
}
}