Files
citrus-cms/resources/views/cron/weld_logs-sync-from-weldlog-to-paint-follow-up.blade.php
2026-04-28 21:15:09 +03:00

204 lines
6.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
use App\Models\PaintMatrix;
use App\Models\WeldLog;
use App\Models\PaintFollowUp;
$prefix = "sync-from-weldlog-to-paint-follow-up";
$weldLogs = apply_welded_filter(
db("weld_logs")
->where("no_of_the_joint_as_per_as_built_survey", "not like", "%clone%")
);
if(isset($id))
{
$weldLogs = $weldLogs->where("id", $id);
}
$weldLogs = $weldLogs
->get();
$paintMatrices = PaintMatrix:://groupBy("line", "fluid_code")->
get();
Log::debug("$prefix Start " . simdi());
$spoolDatas = [];
$jointDatas = [];
$datas = [];
$deletedJoints= [];
$lineStats = [];
$processedLines = [];
foreach($weldLogs AS $weldLog) {
$lineKey = $weldLog->line_number . '_' . $weldLog->fluid_code;
if(!isset($lineStats[$lineKey])) {
$lineStats[$lineKey] = [
's_count' => 0,
'f_count' => 0,
'total' => 0
];
}
if($weldLog->type_of_joint === 'S') {
$lineStats[$lineKey]['s_count']++;
} else if($weldLog->type_of_joint === 'F') {
$lineStats[$lineKey]['f_count']++;
}
$lineStats[$lineKey]['total']++;
// First, check if this line has any 'S' type joints
$hasSType = false;
foreach($weldLogs as $checkLog) {
if($checkLog->line_number === $weldLog->line_number &&
$checkLog->fluid_code === $weldLog->fluid_code &&
$checkLog->type_of_joint === 'S') {
$hasSType = true;
break;
}
}
// Only proceed if we found an 'S' type joint for this line
if($hasSType) {
if(!isset($processedLines[$lineKey])) {
// Log::debug("$prefix Line {$weldLog->line_number} ve Fluid Code {$weldLog->fluid_code} için işlem başladı. S tipi kayıt bulundu.");
$processedLines[$lineKey] = true;
}
$spoolDatas[$weldLog->line_number][$weldLog->fluid_code][] = [
'spool_no_joint_no' => $weldLog->spool_number,
'fluid_code' => $weldLog->fluid_code,
'iso_number' => $weldLog->iso_number,
'line' => $weldLog->line_number,
];
$jointNo = $weldLog->no_of_the_joint_as_per_as_built_survey;
$jointType = "Normal";
if(strpos($jointNo, "R") !== false) {
$jointType = "Repair";
$otherJointNo = deleteAfterR($jointNo);
$deletedJoints[] = $otherJointNo;
// Log::debug("$prefix Onarım tipi kayıt bulundu: $jointNo -> $otherJointNo");
}
$jointDatas[$weldLog->line_number][$weldLog->fluid_code][] = [
'spool_no_joint_no' => $weldLog->no_of_the_joint_as_per_as_built_survey,
'fluid_code' => $weldLog->fluid_code,
'iso_number' => $weldLog->iso_number,
'line' => $weldLog->line_number,
];
} else {
if(!isset($processedLines[$lineKey])) {
Log::debug("$prefix Line {$weldLog->line_number} ve Fluid Code {$weldLog->fluid_code} için S tipi kayıt bulunamadı, kayıtlar eklenmeyecek.");
$processedLines[$lineKey] = true;
}
}
}
// Log line statistics
foreach($lineStats as $lineKey => $stats) {
list($line, $fluid) = explode('_', $lineKey);
Log::debug("$prefix Line $line ve Fluid Code $fluid için istatistikler:");
Log::debug("$prefix - Toplam kayıt: {$stats['total']}");
Log::debug("$prefix - S tipi kayıt: {$stats['s_count']}");
Log::debug("$prefix - F tipi kayıt: {$stats['f_count']}");
}
foreach($deletedJoints AS $deleteJoint) {
removeItemBySpoolNoJointNo($jointDatas, $deleteJoint);
}
foreach($paintMatrices AS $paintMatrix) {
$thisData = [
'project' => $paintMatrix->project,
'description' => $paintMatrix->description,
'area' => $paintMatrix->area,
'cycle' => $paintMatrix->paint_cycle,
'primer_coat_name_1' => $paintMatrix->primer_coat,
'ral_code_1' => $paintMatrix->ral_code_1,
'colour_1' => $paintMatrix->colour_1,
'colour_2' => $paintMatrix->colour_2,
'colour_3' => $paintMatrix->colour_3,
'brend_name_1' => $paintMatrix->brend_name_1,
'brend_name_2' => $paintMatrix->brend_name_2,
'brend_name_3' => $paintMatrix->brend_name_3,
'thickness_1' => $paintMatrix->thickness_1,
'intermediate_coat_name_2' => $paintMatrix->intermediate_coat,
'ral_code_2' => $paintMatrix->ral_code_2,
'thickness_2' => $paintMatrix->thickness_2,
'final_coat_name_3' => $paintMatrix->final_coat,
'ral_code_3' => $paintMatrix->ral_code_3,
'thickness_3' => $paintMatrix->thickness_3,
'total_thickness' => $paintMatrix->total_thickness ?? 0,
];
if(isset($spoolDatas[$paintMatrix->line][$paintMatrix->fluid_code])) {
foreach($spoolDatas[$paintMatrix->line][$paintMatrix->fluid_code] AS $spoolData) {
$thisData['location'] = "SHOP";
$mergedData = array_merge($spoolData, $thisData);
$datas[$paintMatrix->line][$paintMatrix->fluid_code][] = $mergedData;
}
}
if(isset($jointDatas[$paintMatrix->line][$paintMatrix->fluid_code])) {
foreach($jointDatas[$paintMatrix->line][$paintMatrix->fluid_code] AS $jointData) {
$thisData['location'] = "FIELD";
$mergedData = array_merge($jointData, $thisData);
$datas[$paintMatrix->line][$paintMatrix->fluid_code][] = $mergedData;
}
}
}
DB::beginTransaction();
$k = 0;
$say = 0;
try {
foreach($datas AS $line => $data) {
foreach($data AS $fluid_code => $updateDatas) {
foreach($updateDatas AS $updateData) {
$whereData = [
'fluid_code' => $fluid_code,
'spool_no_joint_no' => $updateData['spool_no_joint_no'],
'line' => $line,
];
// //dump($updateData);
PaintFollowUp::updateOrCreate($whereData, $updateData);
$k++;
}
}
if($say == $commitSize) {
DB::commit();
DB::beginTransaction();
$say = 0;
}
$say++;
}
DB::commit();
} catch (\Throwable $th) {
Log::error($th->getMessage());
DB::rollback();
}
//dump("$k Data has been sync WeldLog ==> Paint Follow Up (update or create)");
Log::debug("$k data update Or Create");
Log::debug("$prefix Finish " . simdi());
?>