Files
citrus-cms/app/Http/Controllers/SaveTrigger/paint_matrices.php
T
2026-04-28 21:14:25 +03:00

108 lines
3.9 KiB
PHP

<?php
$id = $request['key'];
$paintMatrix = db('paint_matrices')->where(is_array($id) ? $id : ['id' => $id])->first();
// Get external_finish_type from line_lists
$lineList = db('line_lists')
->where('line_no', $paintMatrix->line)
->first();
$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,
'project' => $paintMatrix->project,
'description' => $paintMatrix->description,
'area' => $paintMatrix->area,
'line' => $paintMatrix->line,
'fluid_code' => $paintMatrix->fluid_code,
'fluid_code_description' => $paintMatrix->fluid_code_description,
'total_thickness' => floatval($paintMatrix->thickness_1 ?? 0) + floatval($paintMatrix->thickness_2 ?? 0) + floatval($paintMatrix->thickness_3 ?? 0),
];
$existingRecords = db('paint_follow_ups')->where([
'line' => $paintMatrix->line,
'area' => $paintMatrix->area,
])->get();
if ($existingRecords->isEmpty()) {
// Create new record if none exists
$updateArray['created_at'] = now();
$result = db('paint_follow_ups')->insert($updateArray);
dump("New record created in Paint Follow Ups");
} else {
// Update existing records
$result = db('paint_follow_ups')->where([
'line' => $paintMatrix->line,
'area' => $paintMatrix->area,
])
->whereNull('primer_coating_start_date')
->update($updateArray);
dump("$result data sync Paint Matrix ==> Paint Follow Ups");
}
// Sync to construction_paint_logs table
$constructionPaintLogData = [
// Painting System
'painting_system_type_1' => $paintMatrix->paint_cycle,
'painting_system_type_2' => $paintMatrix->paint_cycle,
'brend_name_1' => $paintMatrix->brend_name_1,
'ral_1' => $paintMatrix->ral_code_1,
'thickness_1' => $paintMatrix->thickness_1,
'brend_name_2' => $paintMatrix->brend_name_2,
'ral_2' => $paintMatrix->ral_code_2,
'thickness_2' => $paintMatrix->thickness_2,
'brend_name_3' => $paintMatrix->brend_name_3,
'ral_3' => $paintMatrix->ral_code_3,
'thickness_3' => $paintMatrix->thickness_3,
// Project Information
'unit' => $paintMatrix->area,
'line' => $paintMatrix->line,
'fluid_code' => $paintMatrix->fluid_code,
'fluid_code_description' => $paintMatrix->fluid_code_description,
'isolation_info' => $lineList ? $lineList->external_finish_type : null,
// Timestamps
'updated_at' => now()
];
// Update existing records or create new ones
$whereCondition = [
'line' => $paintMatrix->line,
'unit' => $paintMatrix->area
];
// Check if record exists
$existingRecord = db('construction_paint_logs')
->where($whereCondition)
->first();
if ($existingRecord) {
// Update existing record
$updatedPaintLog = db('construction_paint_logs')
// ->where('id', $existingRecord->id)
->whereNull('painting_date_1')
->update($constructionPaintLogData);
dump("$updatedPaintLog Construction paint log updated for line: " . $whereCondition['line']);
} else {
// Create new record
$constructionPaintLogData['created_at'] = now();
db('construction_paint_logs')->insert($constructionPaintLogData);
dump("New construction paint log created for line: " . $whereCondition['line']);
}
?>