258 lines
10 KiB
PHP
258 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Services\LineListTriggers\Triggers;
|
|
|
|
use App\Services\LineListTriggers\Base\BaseLineListTrigger;
|
|
use App\Models\PaintMatrix;
|
|
use App\Models\PaintFollowUp;
|
|
use App\Helpers\TransactionHelper;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* Paint Matrix Operations Trigger
|
|
*
|
|
* Handles Paint Matrix create/update operations and syncs to Paint Follow Ups:
|
|
* - Creates or updates paint_matrices records
|
|
* - Syncs surface roughness to paint_follow_ups
|
|
* - Syncs paint coat details (3 coats) to paint_follow_ups
|
|
*/
|
|
class PaintMatrixOperationsTrigger extends BaseLineListTrigger
|
|
{
|
|
public function getName(): string
|
|
{
|
|
return 'Paint Matrix Operations';
|
|
}
|
|
|
|
public function getDependentFields(): array
|
|
{
|
|
return [
|
|
|
|
];
|
|
}
|
|
|
|
protected function process($lineListData, $beforeData, array $context): array
|
|
{
|
|
// Skip processing if painting_cycle is empty
|
|
// Paint Matrix operations should only run when there is a painting cycle
|
|
if (empty($lineListData->painting_cycle)) {
|
|
Log::debug("Paint Matrix Operations skipped - painting_cycle is empty", [
|
|
'line_no' => $lineListData->line_no
|
|
]);
|
|
return ['skipped' => true, 'reason' => 'painting_cycle_empty'];
|
|
}
|
|
|
|
$paintFollowUpCount = 0;
|
|
$paintMatrix2PaintFollowUpCount = 0;
|
|
|
|
// Get zone (project) information from weld logs
|
|
$weldlogs = db("weld_logs")
|
|
->where("line_number", $lineListData->line_no)
|
|
->where('design_area', $lineListData->unit)
|
|
->get();
|
|
|
|
$zones = [];
|
|
foreach($weldlogs AS $weldLog) {
|
|
$zones[$weldLog->line_number] = $weldLog->project;
|
|
}
|
|
|
|
// Fetch paint system data for enrichment
|
|
$paintSystem = db('paint_systems')
|
|
->where('paint_cycle', $lineListData->painting_cycle)
|
|
->first();
|
|
|
|
$colorSystem = db('color_systems')
|
|
->where('fluid_code', $lineListData->fluid_code)
|
|
->first();
|
|
|
|
$ralCodes = j(setting("ral-codes"));
|
|
|
|
$ral1 = $colorSystem->ral_1 ?? null;
|
|
$ral2 = $colorSystem->ral_2 ?? null;
|
|
$ral3 = $colorSystem->ral_3 ?? null;
|
|
|
|
$colorRu1 = $this->findRussianColorDescription($ralCodes, $ral1);
|
|
$colorRu2 = $this->findRussianColorDescription($ralCodes, $ral2);
|
|
$colorRu3 = $this->findRussianColorDescription($ralCodes, $ral3);
|
|
|
|
// Create or update Paint Matrix
|
|
$updateData = [
|
|
'project' => @$zones[$lineListData->line_no],
|
|
'area' => $lineListData->unit,
|
|
'description' => "PIPE",
|
|
'line' => $lineListData->line_no,
|
|
'fluid_code_description' => $lineListData->fluid_ru,
|
|
'fluid_code' => $lineListData->fluid_code,
|
|
'design_temperature' => $lineListData->design_temperature,
|
|
'operation_temperature' => $lineListData->working_temperature,
|
|
'paint_cycle' => $lineListData->painting_cycle,
|
|
];
|
|
|
|
if ($paintSystem) {
|
|
$updateData = array_merge($updateData, [
|
|
'surface_preparation' => $paintSystem->surface_preparation,
|
|
'touch_up_of_damaged_parts' => $paintSystem->surface_roughness,
|
|
'primer_coat' => $paintSystem->primer_coat_name_1,
|
|
'brend_name_1' => $paintSystem->brand_name_1,
|
|
'thickness_1' => $paintSystem->thickness_1,
|
|
'intermediate_coat' => $paintSystem->primer_coat_name_2,
|
|
'brend_name_2' => $paintSystem->brand_name_2,
|
|
'thickness_2' => $paintSystem->thickness_2,
|
|
'final_coat' => $paintSystem->primer_coat_name_3,
|
|
'brend_name_3' => $paintSystem->brand_name_3,
|
|
'thickness_3' => $paintSystem->thickness_3,
|
|
'total_thickness' => ($paintSystem->thickness_1 ?? 0) + ($paintSystem->thickness_2 ?? 0) + ($paintSystem->thickness_3 ?? 0),
|
|
]);
|
|
}
|
|
|
|
if ($colorSystem) {
|
|
$updateData = array_merge($updateData, [
|
|
'fluid_code_description' => $colorSystem->fluid_code_description ?? $lineListData->fluid_ru,
|
|
'design_temperature' => $colorSystem->design_temperature ?? $lineListData->design_temperature,
|
|
'operation_temperature' => $colorSystem->working_temperature ?? $lineListData->working_temperature,
|
|
'ral_code_1' => $ral1,
|
|
'ral_code_2' => $ral2,
|
|
'ral_code_3' => $ral3,
|
|
'colour_1' => $colorRu1,
|
|
'colour_2' => $colorRu2,
|
|
'colour_3' => $colorRu3,
|
|
]);
|
|
}
|
|
|
|
$whereData = [
|
|
'line' => $lineListData->line_no,
|
|
'fluid_code' => $lineListData->fluid_code,
|
|
'area' => $lineListData->unit,
|
|
];
|
|
|
|
$existingPaintMatrix = PaintMatrix::where($whereData)->first();
|
|
|
|
if ($existingPaintMatrix) {
|
|
$existingPaintMatrix->update($updateData);
|
|
Log::debug("Updated existing Paint Matrix for line: {$lineListData->line_no}");
|
|
} else {
|
|
PaintMatrix::create($updateData);
|
|
Log::debug("Created new Paint Matrix for line: {$lineListData->line_no}");
|
|
}
|
|
|
|
$paintFollowUpCount++;
|
|
|
|
// Surface preparation sync: Paint Matrix -> Paint Follow Ups
|
|
$paintFollowUps = PaintFollowUp::where("line", $lineListData->line_no)
|
|
->where('area', $lineListData->unit)
|
|
->where('fluid_code', $lineListData->fluid_code)
|
|
->orderBy('id', 'ASC') // Deadlock prevention
|
|
->get();
|
|
|
|
$paintMatrix = PaintMatrix::where("line", $lineListData->line_no)->get();
|
|
|
|
$surfaces = [];
|
|
foreach($paintMatrix AS $pm) {
|
|
$surfaces[$pm->line] = [
|
|
'surface_preparation' => $pm->surface_preparation,
|
|
'touch_up_of_damaged_parts' => $pm->touch_up_of_damaged_parts,
|
|
];
|
|
}
|
|
|
|
// Process paint follow ups in chunks with TransactionHelper
|
|
TransactionHelper::chunkTransaction(
|
|
$paintFollowUps,
|
|
function ($pfuChunk) use ($surfaces, &$paintMatrix2PaintFollowUpCount) {
|
|
foreach($pfuChunk AS $pfu) {
|
|
if(isset($surfaces[$pfu->line])) {
|
|
if(strpos($pfu->spool_no_joint_no, "SPL") !== false) {
|
|
$thisSurface = $surfaces[$pfu->line]['surface_preparation'];
|
|
} else {
|
|
$thisSurface = $surfaces[$pfu->line]['touch_up_of_damaged_parts'];
|
|
}
|
|
|
|
db("paint_follow_ups")->where([
|
|
'id' => $pfu->id
|
|
])->update([
|
|
'surface_roughness' => $thisSurface
|
|
]);
|
|
$paintMatrix2PaintFollowUpCount++;
|
|
}
|
|
}
|
|
return $pfuChunk->count();
|
|
},
|
|
1, // Chunk size
|
|
10000 // 10ms delay
|
|
);
|
|
|
|
// Sync paint coat details to paint_follow_ups
|
|
/*
|
|
$paintMatrix = db('paint_matrices')
|
|
->where('line', $lineListData->line_no)
|
|
->where('area', $lineListData->unit)
|
|
->where('fluid_code', $lineListData->fluid_code)
|
|
->where('paint_cycle', $lineListData->painting_cycle)
|
|
->first();
|
|
|
|
if(!is_null($paintMatrix)) {
|
|
$updateArray = [
|
|
'cycle' => $paintMatrix->paint_cycle,
|
|
'primer_coat_name_1' => $paintMatrix->primer_coat,
|
|
'brend_name_1' => $paintMatrix->brend_name_1,
|
|
'colour_1' => $paintMatrix->colour_1,
|
|
'ral_code_1' => $paintMatrix->ral_code_1,
|
|
'thickness_1' => $paintMatrix->thickness_1,
|
|
'intermediate_coat_name_2' => $paintMatrix->intermediate_coat,
|
|
'brend_name_2' => $paintMatrix->brend_name_2,
|
|
'colour_2' => $paintMatrix->colour_2,
|
|
'ral_code_2' => $paintMatrix->ral_code_2,
|
|
'thickness_2' => $paintMatrix->thickness_2,
|
|
'final_coat_name_3' => $paintMatrix->final_coat,
|
|
'brend_name_3' => $paintMatrix->brend_name_3,
|
|
'colour_3' => $paintMatrix->colour_3,
|
|
'ral_code_3' => $paintMatrix->ral_code_3,
|
|
'thickness_3' => $paintMatrix->thickness_3,
|
|
];
|
|
|
|
$result = db('paint_follow_ups')->where([
|
|
'line' => $lineListData->line_no,
|
|
'cycle' => $lineListData->painting_cycle,
|
|
'area' => $lineListData->unit,
|
|
'fluid_code' => $lineListData->fluid_code,
|
|
])
|
|
->whereNull('primer_coating_start_date')
|
|
->whereNull('primer_coating_finish_date')
|
|
->whereNull('start_intermediate_date2')
|
|
->whereNull('finish_intermediate_date2')
|
|
->whereNull('final_coat_start_date3')
|
|
->whereNull('final_coat_finish_date3')
|
|
->update($updateArray);
|
|
|
|
Log::debug("$result data sync Paint Matrix ==> Paint Follow Ups");
|
|
}
|
|
|
|
Log::debug("$paintFollowUpCount Data has been create or update from lineList to Paint Matrix And update Paint Follow Up");
|
|
Log::debug("$paintMatrix2PaintFollowUpCount Data has been sync from Paint Matrix to Paint Follow Up");
|
|
*/
|
|
return [
|
|
'success' => true,
|
|
'paint_matrices_processed' => $paintFollowUpCount,
|
|
'surface_synced' => $paintMatrix2PaintFollowUpCount
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Find Russian color description for a RAL code
|
|
*/
|
|
protected function findRussianColorDescription($ralCodes, $ralCode): string
|
|
{
|
|
if (empty($ralCode) || empty($ralCodes)) {
|
|
return '';
|
|
}
|
|
|
|
foreach ($ralCodes as $entry) {
|
|
if (isset($entry['ral_code']) && $entry['ral_code'] == $ralCode && isset($entry['ru'])) {
|
|
return $entry['ru'];
|
|
}
|
|
}
|
|
|
|
return '';
|
|
}
|
|
}
|
|
|
|
|