2070 lines
100 KiB
PHP
2070 lines
100 KiB
PHP
<?php
|
||
use App\Models\WeldLog;
|
||
use App\Models\LineList;
|
||
use App\Models\NdeMatrix;
|
||
use App\Models\PaintMatrix;
|
||
use App\Models\PaintFollowUp;
|
||
use Carbon\Carbon;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Illuminate\Support\Facades\Schema;
|
||
|
||
// SaveTrigger variables from AdminController:
|
||
// $request, $tableName, $beforeData are available
|
||
$id = $request['key']['id'] ?? $request['key'];
|
||
|
||
|
||
|
||
try {
|
||
// Ana transaction'ı kaldır, her işlem grubu için ayrı transaction kullan
|
||
|
||
$lineLists = LineList::where("id", $id)->get();
|
||
$lineLists = db("line_lists")->where("id", $id)->get();
|
||
|
||
Log::debug("Line List verileri alındı", [
|
||
'id' => $id,
|
||
'found_records' => count($lineLists),
|
||
'line_number' => $lineLists[0]?->line_no ?? 'NULL',
|
||
'before_painting_cycle' => $beforeData->painting_cycle ?? 'NULL',
|
||
'current_painting_cycle' => $lineLists[0]?->painting_cycle ?? 'NULL'
|
||
]);
|
||
|
||
$lineListNumber = $lineLists[0]?->line_no;
|
||
$lineList = $lineLists[0];
|
||
|
||
$count = 0;
|
||
|
||
Log::debug("Paint cycle değişiklik kontrolü", [
|
||
'before_painting_cycle' => $beforeData->painting_cycle ?? 'NULL',
|
||
'current_painting_cycle' => $lineList->painting_cycle ?? 'NULL',
|
||
'line_no' => $lineList->line_no ?? 'NULL'
|
||
]);
|
||
|
||
// Paint cycle değişikliği kontrolü - Spool Status Changer tetiklemesi için
|
||
if(isset($beforeData) && $beforeData->painting_cycle != $lineList->painting_cycle) {
|
||
|
||
Log::debug("Paint cycle değişti - Spool Status Changer tetikleniyor", [
|
||
'before_painting_cycle' => $beforeData->painting_cycle ?? 'NULL',
|
||
'current_painting_cycle' => $lineList->painting_cycle ?? 'NULL',
|
||
'line_no' => $lineList->line_no
|
||
]);
|
||
|
||
// Get all weld logs for this line to extract iso + spool combinations
|
||
$weldLogsForSpoolStatus = db("weld_logs")
|
||
->where("line_number", $lineList->line_no)
|
||
->whereNotNull('spool_number')
|
||
->whereNotNull('iso_number')
|
||
->where('spool_number', '!=', '')
|
||
->where('iso_number', '!=', '')
|
||
->select('iso_number', 'spool_number')
|
||
->distinct()
|
||
->get();
|
||
|
||
Log::debug("Spool Status Changer için weld_logs verileri alındı", [
|
||
'line_no' => $lineList->line_no,
|
||
'found_combinations' => $weldLogsForSpoolStatus->count()
|
||
]);
|
||
|
||
$spoolStatusChangerCount = 0;
|
||
foreach($weldLogsForSpoolStatus as $weldLogData) {
|
||
try {
|
||
// Trigger spool status changer for each iso + spool combination
|
||
$spoolStatusChangerParams = [
|
||
'iso_number' => $weldLogData->iso_number,
|
||
'spool_number' => $weldLogData->spool_number
|
||
];
|
||
|
||
$spoolStatusChangerView = view('cron.spool-status-changer', $spoolStatusChangerParams)->render();
|
||
$spoolStatusChangerCount++;
|
||
|
||
Log::debug("Spool Status Changer tetiklendi", [
|
||
'iso_number' => $weldLogData->iso_number,
|
||
'spool_number' => $weldLogData->spool_number,
|
||
'line_no' => $lineList->line_no
|
||
]);
|
||
|
||
} catch (\Throwable $th) {
|
||
Log::error("Spool Status Changer trigger error: " . $th->getMessage(), [
|
||
'iso_number' => $weldLogData->iso_number ?? 'NULL',
|
||
'spool_number' => $weldLogData->spool_number ?? 'NULL',
|
||
'line_no' => $lineList->line_no
|
||
]);
|
||
}
|
||
}
|
||
|
||
Log::debug("Spool Status Changer tetikleme tamamlandı", [
|
||
'line_no' => $lineList->line_no,
|
||
'total_triggered' => $spoolStatusChangerCount,
|
||
'paint_cycle_change' => $beforeData->painting_cycle . ' -> ' . $lineList->painting_cycle
|
||
]);
|
||
|
||
// Update weld_logs painting_cycle field for all related records
|
||
try {
|
||
Log::debug("Paint cycle değişikliği - WeldLogs painting_cycle alanı güncelleniyor", [
|
||
'line_no' => $lineList->line_no,
|
||
'new_painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
|
||
$weldLogsUpdateCount = db("weld_logs")
|
||
->where("line_number", $lineList->line_no)
|
||
->update(['painting_cycle' => $lineList->painting_cycle]);
|
||
|
||
Log::debug("WeldLogs painting_cycle güncellendi", [
|
||
'line_no' => $lineList->line_no,
|
||
'updated_count' => $weldLogsUpdateCount,
|
||
'new_painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
|
||
} catch (\Throwable $th) {
|
||
Log::error("WeldLogs painting_cycle güncelleme hatası: " . $th->getMessage(), [
|
||
'line_no' => $lineList->line_no
|
||
]);
|
||
}
|
||
|
||
// Trigger comprehensive paint synchronizations (like weld_logs trigger)
|
||
try {
|
||
Log::debug("Paint cycle değişikliği - Kapsamlı paint senkronizasyonları başlatılıyor", [
|
||
'line_no' => $lineList->line_no,
|
||
'painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
|
||
// Get all weld logs for this line
|
||
$allWeldLogsForPaint = db("weld_logs")
|
||
->where("line_number", $lineList->line_no)
|
||
->get();
|
||
|
||
Log::debug("Paint senkronizasyonu için weld_logs verileri alındı", [
|
||
'line_no' => $lineList->line_no,
|
||
'total_weld_logs' => $allWeldLogsForPaint->count()
|
||
]);
|
||
|
||
// Synchronize Paint Follow Ups from Paint Matrix
|
||
$paintMatrix = PaintMatrix::where('line', $lineList->line_no)
|
||
->where('fluid_code', $lineList->fluid_code)
|
||
->first();
|
||
|
||
if ($paintMatrix) {
|
||
$paintFollowUpSyncCount = 0;
|
||
|
||
foreach ($allWeldLogsForPaint as $weldLogForPaint) {
|
||
// Skip if fluid code is empty
|
||
if (empty($weldLogForPaint->fluid_code)) {
|
||
continue;
|
||
}
|
||
|
||
// Base paint follow up data from paint matrix
|
||
$basePaintData = [
|
||
'project' => $weldLogForPaint->project ?? '',
|
||
'description' => $paintMatrix->description ?? '',
|
||
'area' => $weldLogForPaint->design_area ?? '',
|
||
'line' => $weldLogForPaint->line_number,
|
||
'iso_number' => $weldLogForPaint->iso_number ?? '',
|
||
'fluid_code' => $weldLogForPaint->fluid_code,
|
||
'fluid_code_description' => $lineList->fluid_ru ?? '',
|
||
'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 ?? '',
|
||
'status' => 'In Progress',
|
||
'updated_at' => now()
|
||
];
|
||
|
||
// Check if line has shop joints
|
||
$hasShopJoint = db("weld_logs")
|
||
->where('line_number', $weldLogForPaint->line_number)
|
||
->where('spool_number', $weldLogForPaint->spool_number)
|
||
->where('type_of_joint', 'S')
|
||
->exists();
|
||
|
||
// Update SHOP entries (spool_number)
|
||
if ($hasShopJoint && !empty($weldLogForPaint->spool_number)) {
|
||
$shopData = $basePaintData;
|
||
$shopData['location'] = 'SHOP';
|
||
$shopData['spool_no_joint_no'] = $weldLogForPaint->spool_number;
|
||
$shopData['surface_roughness'] = $paintMatrix->surface_preparation ?? '';
|
||
|
||
// Where koşulu - SHOP için sadece line + spool_no_joint_no (weld_logs.php ile aynı)
|
||
$shopWhereCondition = [
|
||
'line' => $weldLogForPaint->line_number,
|
||
'spool_no_joint_no' => $weldLogForPaint->spool_number,
|
||
];
|
||
|
||
// Mevcut kayıt var mı kontrolü
|
||
$existingShopRecord = db("paint_follow_ups")
|
||
->where($shopWhereCondition)
|
||
->first();
|
||
|
||
if ($existingShopRecord) {
|
||
// Mevcut kaydı güncelle
|
||
db("paint_follow_ups")
|
||
->where('id', $existingShopRecord->id)
|
||
->update($shopData);
|
||
$paintFollowUpSyncCount++;
|
||
} else {
|
||
// Yeni kayıt oluştur
|
||
$shopData['created_at'] = now();
|
||
db("paint_follow_ups")->insert($shopData);
|
||
$paintFollowUpSyncCount++;
|
||
}
|
||
}
|
||
|
||
// Update FIELD entries (joint_number)
|
||
if (!empty($weldLogForPaint->no_of_the_joint_as_per_as_built_survey)) {
|
||
$fieldData = $basePaintData;
|
||
$fieldData['location'] = 'FIELD';
|
||
$fieldData['spool_no_joint_no'] = $weldLogForPaint->no_of_the_joint_as_per_as_built_survey;
|
||
$fieldData['surface_roughness'] = $paintMatrix->touch_up_of_damaged_parts ?? '';
|
||
|
||
// Where koşulu - FIELD için line + fluid_code + spool_no_joint_no + location (weld_logs.php ile aynı)
|
||
$fieldWhereCondition = [
|
||
'line' => $weldLogForPaint->line_number,
|
||
'fluid_code' => $weldLogForPaint->fluid_code,
|
||
'spool_no_joint_no' => $weldLogForPaint->no_of_the_joint_as_per_as_built_survey,
|
||
'location' => 'FIELD'
|
||
];
|
||
|
||
// Mevcut kayıt var mı kontrolü
|
||
$existingFieldRecord = db("paint_follow_ups")
|
||
->where($fieldWhereCondition)
|
||
->first();
|
||
|
||
if ($existingFieldRecord) {
|
||
// Mevcut kaydı güncelle
|
||
db("paint_follow_ups")
|
||
->where('id', $existingFieldRecord->id)
|
||
->update($fieldData);
|
||
$paintFollowUpSyncCount++;
|
||
} else {
|
||
// Yeni kayıt oluştur
|
||
$fieldData['created_at'] = now();
|
||
db("paint_follow_ups")->insert($fieldData);
|
||
$paintFollowUpSyncCount++;
|
||
}
|
||
}
|
||
}
|
||
|
||
Log::debug("Paint Follow Ups senkronizasyonu tamamlandı", [
|
||
'line_no' => $lineList->line_no,
|
||
'synchronized_records' => $paintFollowUpSyncCount
|
||
]);
|
||
} else {
|
||
Log::warning("Paint Matrix bulunamadı - Paint Follow Ups senkronizasyonu atlandı", [
|
||
'line_no' => $lineList->line_no,
|
||
'fluid_code' => $lineList->fluid_code
|
||
]);
|
||
}
|
||
|
||
// Synchronize Construction Paint Logs
|
||
$constructionPaintLogSyncCount = 0;
|
||
|
||
// Get paint system data
|
||
$paintSystem = DB::table('paint_systems')
|
||
->where('paint_cycle', $lineList->painting_cycle)
|
||
->first();
|
||
|
||
foreach ($allWeldLogsForPaint as $weldLogForConstruction) {
|
||
// Skip if spool is empty
|
||
if (empty($weldLogForConstruction->spool_number)) {
|
||
continue;
|
||
}
|
||
|
||
// Check if line has shop joints
|
||
$hasShopJointForConstruction = db("weld_logs")
|
||
->where('line_number', $weldLogForConstruction->line_number)
|
||
->where('spool_number', $weldLogForConstruction->spool_number)
|
||
->where('type_of_joint', 'S')
|
||
->exists();
|
||
|
||
if ($hasShopJointForConstruction) {
|
||
$constructionPaintData = [
|
||
'line' => $weldLogForConstruction->line_number,
|
||
'spool' => $weldLogForConstruction->spool_number,
|
||
'unit' => $weldLogForConstruction->design_area ?? '',
|
||
'painting_system_type_1' => $lineList->painting_cycle ?? '',
|
||
'painting_system_type_2' => $lineList->painting_cycle ?? '',
|
||
'rev' => $lineList->rev ?? '',
|
||
'isolation_info' => $lineList->external_finish_type ?? '',
|
||
'engineering' => $lineList->engineering ?? '',
|
||
'fluid_code' => $lineList->fluid_code ?? '',
|
||
'fluid_code_description' => $lineList->fluid_ru ?? '',
|
||
'area' => $weldLogForConstruction->project ?? '',
|
||
'test_package' => $weldLogForConstruction->test_package_no ?? '',
|
||
'iso_drawings' => $weldLogForConstruction->iso_number ?? '',
|
||
'spool_status' => $weldLogForConstruction->spool_status ?? 'Waiting',
|
||
'construction_report_no' => $weldLogForConstruction->weld_map_no ?? '',
|
||
'dn_1' => $weldLogForConstruction->nps_1 ?? '',
|
||
'dn_2' => $weldLogForConstruction->nps_2 ?? '',
|
||
'dn_3' => '',
|
||
'updated_at' => now()
|
||
];
|
||
|
||
// Add paint system fields if available
|
||
if ($paintSystem) {
|
||
$constructionPaintData['brend_name_1'] = $paintSystem->brand_name_1 ?? $paintSystem->brend_name_1 ?? '';
|
||
$constructionPaintData['thickness_1'] = $paintSystem->thickness_1 ?? '';
|
||
$constructionPaintData['brend_name_2'] = $paintSystem->brand_name_2 ?? $paintSystem->brend_name_2 ?? '';
|
||
$constructionPaintData['thickness_2'] = $paintSystem->thickness_2 ?? '';
|
||
$constructionPaintData['brend_name_3'] = $paintSystem->brand_name_3 ?? $paintSystem->brend_name_3 ?? '';
|
||
$constructionPaintData['thickness_3'] = $paintSystem->thickness_3 ?? '';
|
||
}
|
||
|
||
// Add RAL values from paint matrix if available
|
||
if ($paintMatrix) {
|
||
$constructionPaintData['ral_1'] = $paintMatrix->ral_code_1 ?? '';
|
||
$constructionPaintData['ral_2'] = $paintMatrix->ral_code_2 ?? '';
|
||
$constructionPaintData['ral_3'] = $paintMatrix->ral_code_3 ?? '';
|
||
}
|
||
|
||
// Where koşulu - sadece line + spool (weld_logs.php ile aynı)
|
||
$constructionWhereCondition = [
|
||
'line' => $weldLogForConstruction->line_number,
|
||
'spool' => $weldLogForConstruction->spool_number
|
||
];
|
||
|
||
// Check if record exists
|
||
$existingConstructionRecord = db("construction_paint_logs")
|
||
->where($constructionWhereCondition)
|
||
->first();
|
||
|
||
if ($existingConstructionRecord) {
|
||
// Update existing record
|
||
db("construction_paint_logs")
|
||
->where('id', $existingConstructionRecord->id)
|
||
->update($constructionPaintData);
|
||
$constructionPaintLogSyncCount++;
|
||
} else {
|
||
// Create new record
|
||
$constructionPaintData['created_at'] = now();
|
||
db("construction_paint_logs")->insert($constructionPaintData);
|
||
$constructionPaintLogSyncCount++;
|
||
}
|
||
}
|
||
}
|
||
|
||
Log::debug("Construction Paint Logs senkronizasyonu tamamlandı", [
|
||
'line_no' => $lineList->line_no,
|
||
'synchronized_records' => $constructionPaintLogSyncCount
|
||
]);
|
||
|
||
Log::info("Paint cycle değişikliği - Tüm paint senkronizasyonları başarıyla tamamlandı", [
|
||
'line_no' => $lineList->line_no,
|
||
'painting_cycle' => $lineList->painting_cycle,
|
||
'paint_follow_ups_synced' => $paintFollowUpSyncCount ?? 0,
|
||
'construction_paint_logs_synced' => $constructionPaintLogSyncCount
|
||
]);
|
||
|
||
} catch (\Throwable $th) {
|
||
Log::error("Paint senkronizasyonları hatası: " . $th->getMessage(), [
|
||
'line_no' => $lineList->line_no,
|
||
'error_file' => $th->getFile(),
|
||
'error_line' => $th->getLine(),
|
||
'trace' => $th->getTraceAsString()
|
||
]);
|
||
}
|
||
}
|
||
|
||
// Check for painting cycle deletion (existing logic)
|
||
if(isset($beforeData) && $beforeData->painting_cycle !="" && $lineList->painting_cycle == "") {
|
||
|
||
Log::debug("Painting cycle siliniyor - ilgili verileri temizleme başlatılıyor", [
|
||
'before_painting_cycle' => $beforeData->painting_cycle,
|
||
'current_painting_cycle' => $lineList->painting_cycle,
|
||
'line_no' => $lineList->line_no,
|
||
'fluid_code' => $lineList->fluid_code
|
||
]);
|
||
|
||
$deletePaintMatrices = db("paint_matrices")
|
||
->where('line', $beforeData->line_no)
|
||
->where('fluid_code', $beforeData->fluid_code)
|
||
->delete();
|
||
|
||
$deletedPaintFollowUps = db('paint_follow_ups')
|
||
->where([
|
||
'line' => $lineList->line_no,
|
||
'area' => $lineList->unit,
|
||
])
|
||
->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')
|
||
->delete();
|
||
|
||
// Delete from construction_paint_logs
|
||
$deletedConstructionPaintLogs = db('construction_paint_logs')
|
||
->where([
|
||
'line' => $lineList->line_no,
|
||
'unit' => $lineList->unit,
|
||
])
|
||
->whereNull('blasting_date')
|
||
->whereNull('blasting_finish_date')
|
||
->whereNull('painting_date_1')
|
||
->whereNull('painting_finish_date_1')
|
||
->whereNull('painting_date_2')
|
||
->whereNull('painting_finish_date_2')
|
||
->whereNull('painting_date_3')
|
||
->whereNull('painting_finish_date_3')
|
||
->delete();
|
||
|
||
// IMPORTANT: NDE Matrix'i de temizle
|
||
$deletedNdeMatrices = db('nde_matrices')
|
||
->where('line', $lineList->line_no)
|
||
->where('fluid', $lineList->fluid_code)
|
||
->delete();
|
||
|
||
Log::debug("Painting cycle temizleme tamamlandı", [
|
||
'deleted_paint_matrices' => $deletePaintMatrices,
|
||
'deleted_paint_follow_ups' => $deletedPaintFollowUps,
|
||
'deleted_construction_paint_logs' => $deletedConstructionPaintLogs,
|
||
'deleted_nde_matrices' => $deletedNdeMatrices,
|
||
'line_no' => $lineList->line_no,
|
||
'fluid_code' => $lineList->fluid_code
|
||
]);
|
||
|
||
// Bu işlem grubu için ayrı transaction
|
||
DB::commit();
|
||
}
|
||
|
||
|
||
// WeldLog güncelleme işlemi için ayrı transaction
|
||
DB::beginTransaction();
|
||
try {
|
||
foreach($lineLists AS $lineList) {
|
||
|
||
|
||
$data = [
|
||
'painting_cycle' => $lineList->painting_cycle,
|
||
'main_nps' => $lineList->dn,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'service_category' => $lineList->category,
|
||
'fluid_group' => $lineList->fluid_group,
|
||
'piping_class' => $lineList->pipe_material_class,
|
||
'external_finish_type' => $lineList->external_finish_type,
|
||
// 'pipe_material_class' => $lineList->pipe_material_class,
|
||
// 'main_material' => $lineList->pipe_material_class,
|
||
'circuit_number' => $lineList->circuit_number,
|
||
'p_id' => $lineList->p_id,
|
||
'type_of_test' => $lineList->test_media,
|
||
'test_pressure' => $lineList->test_pressure_mpa,
|
||
'line_specification' => $lineList->line_specification,
|
||
'design_pressure_mpa' => $lineList->design_pressure_mpa,
|
||
'design_temperature_s' => $lineList->design_temperature,
|
||
'operating_pressure_mpa' => $lineList->working_pressure_mpa,
|
||
'operating_temperature_s' => $lineList->working_temperature,
|
||
'ndt_percent' => $lineList->ndt,
|
||
];
|
||
|
||
// Tüm line_number eşleşen kayıtları güncelle (design_area filtresi kaldırıldı)
|
||
$result = db("weld_logs")
|
||
->where("line_number", $lineList->line_no)
|
||
->update($data);
|
||
|
||
Log::debug("WeldLog güncellendi", [
|
||
'line_number' => $lineList->line_no,
|
||
'affected_rows' => $result,
|
||
'updated_data' => $data
|
||
]);
|
||
|
||
$count++;
|
||
}
|
||
DB::commit();
|
||
} catch (\Exception $e) {
|
||
DB::rollBack();
|
||
Log::error("WeldLog güncelleme hatası: " . $e->getMessage());
|
||
throw $e;
|
||
}
|
||
|
||
Log::debug("WeldLog updated rows: $count");
|
||
|
||
// WeldLog güncellendikten sonra fresh verileri çek
|
||
$weldlogs = db("weld_logs")
|
||
->where("line_number", $lineListNumber)
|
||
->get();
|
||
|
||
Log::debug("Fresh WeldLog verileri çekildi", [
|
||
'line_number' => $lineListNumber,
|
||
'total_records' => count($weldlogs),
|
||
'fluid_codes_found' => $weldlogs->pluck('fluid_code')->filter()->unique()->toArray()
|
||
]);
|
||
|
||
// Paint Matrix işlemi için ayrı transaction
|
||
DB::beginTransaction();
|
||
try {
|
||
$say = 0;
|
||
$paintFollowUpCount = 0;
|
||
|
||
$zones = [];
|
||
|
||
foreach($weldlogs AS $weldLog) {
|
||
$zones[$weldLog->line_number] = $weldLog->project;
|
||
}
|
||
|
||
foreach($lineLists AS $lineList) {
|
||
|
||
Log::debug("Paint Matrix işlemi kontrol ediliyor", [
|
||
'lineList_id' => $lineList->id ?? 'N/A',
|
||
'line_no' => $lineList->line_no ?? 'NULL',
|
||
'painting_cycle' => $lineList->painting_cycle ?? 'NULL',
|
||
'fluid_code' => $lineList->fluid_code ?? 'NULL',
|
||
'unit' => $lineList->unit ?? 'NULL'
|
||
]);
|
||
|
||
if($lineList->painting_cycle != "") {
|
||
|
||
$updateData = [
|
||
'project' => @$zones[$lineList->line_no],
|
||
'area' => $lineList->unit,
|
||
'description' => "PIPE",
|
||
'line' => $lineList->line_no,
|
||
'fluid_code_description' => $lineList->fluid_ru,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'design_temperature' => $lineList->design_temperature,
|
||
'operation_temperature' => $lineList->working_temperature,
|
||
'paint_cycle' => $lineList->painting_cycle,
|
||
];
|
||
|
||
$whereData = [
|
||
'line' => $lineList->line_no,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
];
|
||
|
||
// Check if paint matrix exists and has date fields filled
|
||
$existingPaintMatrix = PaintMatrix::where($whereData)->first();
|
||
|
||
if ($existingPaintMatrix) {
|
||
// Check if there are any date fields filled (assuming paint matrices might have date fields)
|
||
// Since paint_matrices table typically doesn't have date fields for painting activities,
|
||
// we can safely update it. But if your table has date fields, add them here:
|
||
|
||
// For now, we'll update paint matrix regardless as it typically doesn't have activity dates
|
||
$existingPaintMatrix->update($updateData);
|
||
Log::debug("Updated existing Paint Matrix for line: {$lineList->line_no}");
|
||
} else {
|
||
// Create new paint matrix
|
||
PaintMatrix::create($updateData);
|
||
Log::debug("Created new Paint Matrix for line: {$lineList->line_no}");
|
||
}
|
||
|
||
$paintFollowUpCount++;
|
||
} else {
|
||
Log::debug("Paint Matrix işlemi atlandı - painting_cycle boş", [
|
||
'lineList_id' => $lineList->id ?? 'N/A',
|
||
'line_no' => $lineList->line_no ?? 'NULL',
|
||
'fluid_code' => $lineList->fluid_code ?? 'NULL'
|
||
]);
|
||
}
|
||
}
|
||
|
||
|
||
//surface preparation
|
||
|
||
$paintFollowUps = PaintFollowUp::where("line", $lineListNumber)->get();
|
||
$paintMatrix = PaintMatrix::where("line", $lineListNumber)->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,
|
||
];
|
||
|
||
}
|
||
|
||
|
||
$paintMatrix2PaintFollowUpCount = 0;
|
||
|
||
foreach($paintFollowUps 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++;
|
||
}
|
||
|
||
}
|
||
|
||
$paintMatrix = db('paint_matrices')
|
||
->where('line', $lineListNumber)
|
||
->where('area', $lineList->unit)
|
||
->where('paint_cycle', $lineList->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' => $paintMatrix->line,
|
||
'cycle' => $paintMatrix->paint_cycle,
|
||
'area' => $paintMatrix->area,
|
||
])
|
||
->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");
|
||
|
||
DB::commit();
|
||
} catch (\Exception $e) {
|
||
DB::rollBack();
|
||
Log::error("Paint Matrix işlemi hatası: " . $e->getMessage());
|
||
throw $e;
|
||
}
|
||
|
||
|
||
|
||
|
||
$specJointTypes = [];
|
||
|
||
Log::debug("WeldLogs analizi başlatılıyor", [
|
||
'total_weldlogs' => count($weldlogs),
|
||
'line_number' => $lineListNumber
|
||
]);
|
||
|
||
foreach($weldlogs AS $weldlog) {
|
||
|
||
Log::debug("WeldLog analiz ediliyor", [
|
||
'weldlog_id' => $weldlog->id ?? 'N/A',
|
||
'fluid_code' => $weldlog->fluid_code ?? 'NULL',
|
||
'line_number' => $weldlog->line_number ?? 'NULL',
|
||
'type_of_welds' => $weldlog->type_of_welds ?? 'NULL'
|
||
]);
|
||
|
||
// Eğer fluid_code hala boşsa, line_list'ten tekrar doldur
|
||
if(empty($weldlog->fluid_code) && !empty($lineList->fluid_code)) {
|
||
Log::debug("WeldLog fluid_code boş, line_list'ten güncelleniyor", [
|
||
'weldlog_id' => $weldlog->id,
|
||
'line_list_fluid_code' => $lineList->fluid_code
|
||
]);
|
||
|
||
db("weld_logs")
|
||
->where("id", $weldlog->id)
|
||
->update([
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'service_category' => $lineList->category,
|
||
'fluid_group' => $lineList->fluid_group,
|
||
]);
|
||
|
||
// Güncellenen veriyi object'e yansıt
|
||
$weldlog->fluid_code = $lineList->fluid_code;
|
||
$weldlog->service_category = $lineList->category;
|
||
$weldlog->fluid_group = $lineList->fluid_group;
|
||
}
|
||
|
||
if(empty($weldlog->fluid_code)) {
|
||
Log::warning("WeldLog atlandı - fluid_code hala boş", [
|
||
'weldlog_id' => $weldlog->id ?? 'N/A',
|
||
'line_number' => $weldlog->line_number ?? 'NULL',
|
||
'line_list_fluid_code' => $lineList->fluid_code ?? 'NULL'
|
||
]);
|
||
continue;
|
||
}
|
||
|
||
if(empty($weldlog->line_number)) {
|
||
Log::warning("WeldLog atlandı - line_number boş", [
|
||
'weldlog_id' => $weldlog->id ?? 'N/A',
|
||
'fluid_code' => $weldlog->fluid_code ?? 'NULL'
|
||
]);
|
||
continue;
|
||
}
|
||
|
||
if(empty($weldlog->type_of_welds)) {
|
||
Log::warning("WeldLog atlandı - type_of_welds boş", [
|
||
'weldlog_id' => $weldlog->id ?? 'N/A',
|
||
'fluid_code' => $weldlog->fluid_code ?? 'NULL',
|
||
'line_number' => $weldlog->line_number ?? 'NULL'
|
||
]);
|
||
continue;
|
||
}
|
||
|
||
if(!isset($specJointTypes[$weldlog->fluid_code][$weldlog->line_number])) {
|
||
$specJointTypes[$weldlog->fluid_code][$weldlog->line_number] = [];
|
||
Log::debug("Yeni specJointTypes grubu oluşturuldu", [
|
||
'fluid_code' => $weldlog->fluid_code,
|
||
'line_number' => $weldlog->line_number
|
||
]);
|
||
}
|
||
|
||
if(!in_array($weldlog->type_of_welds, $specJointTypes[$weldlog->fluid_code][$weldlog->line_number])) {
|
||
$specJointTypes[$weldlog->fluid_code][$weldlog->line_number][] = $weldlog->type_of_welds;
|
||
Log::debug("Yeni joint type eklendi", [
|
||
'fluid_code' => $weldlog->fluid_code,
|
||
'line_number' => $weldlog->line_number,
|
||
'type_of_welds' => $weldlog->type_of_welds
|
||
]);
|
||
}
|
||
}
|
||
|
||
|
||
Log::debug('specJointTypes içeriği:', ['specJointTypes' => $specJointTypes]);
|
||
|
||
// NDE Matrix işlemi için ayrı transaction
|
||
DB::beginTransaction();
|
||
try {
|
||
$count = 0;
|
||
$update = 0;
|
||
$create = 0;
|
||
|
||
Log::debug("NDE Matrix senkronizasyonu başlatılıyor", [
|
||
'total_lineLists' => count($lineLists),
|
||
'line_number' => $lineListNumber
|
||
]);
|
||
|
||
foreach($lineLists AS $lineList) {
|
||
|
||
Log::debug("LineList işleniyor", [
|
||
'lineList_id' => $lineList->id ?? 'N/A',
|
||
'line_no' => $lineList->line_no ?? 'NULL',
|
||
'fluid_code' => $lineList->fluid_code ?? 'NULL',
|
||
'unit' => $lineList->unit ?? 'NULL'
|
||
]);
|
||
|
||
if(empty($lineList->fluid_code)) {
|
||
Log::warning("LineList atlandı - fluid_code boş", [
|
||
'lineList_id' => $lineList->id ?? 'N/A',
|
||
'line_no' => $lineList->line_no ?? 'NULL'
|
||
]);
|
||
continue;
|
||
}
|
||
|
||
if(empty($lineList->line_no)) {
|
||
Log::warning("LineList atlandı - line_no boş", [
|
||
'lineList_id' => $lineList->id ?? 'N/A',
|
||
'fluid_code' => $lineList->fluid_code ?? 'NULL'
|
||
]);
|
||
continue;
|
||
}
|
||
|
||
// Bu LineList için specJointTypes verileri var mı kontrol et
|
||
if(!isset($specJointTypes[$lineList->fluid_code][$lineList->line_no])) {
|
||
Log::warning("NDE Matrix işlemi atlandı - specJointTypes bulunamadı", [
|
||
'lineList_id' => $lineList->id ?? 'N/A',
|
||
'line_no' => $lineList->line_no ?? 'NULL',
|
||
'fluid_code' => $lineList->fluid_code ?? 'NULL',
|
||
'available_fluid_codes' => array_keys($specJointTypes),
|
||
'available_lines_for_fluid' => isset($specJointTypes[$lineList->fluid_code]) ? array_keys($specJointTypes[$lineList->fluid_code]) : 'Fluid code not found'
|
||
]);
|
||
continue;
|
||
}
|
||
|
||
$typeOfJoint = $specJointTypes[$lineList->fluid_code][$lineList->line_no];
|
||
Log::debug("NDE Matrix için joint types bulundu", [
|
||
'line_no' => $lineList->line_no,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'joint_types' => $typeOfJoint
|
||
]);
|
||
|
||
foreach($typeOfJoint AS $jointItem) {
|
||
$ht = 0;
|
||
|
||
if($lineList->pwht == "YES") $ht = 100;
|
||
|
||
$data = [
|
||
'design_area' => $lineList->unit,
|
||
'fluid' => $lineList->fluid_code,
|
||
'line' => $lineList->line_no,
|
||
'line_spec' => $lineList->line_specification,
|
||
'operation_temp' => $lineList->working_temperature,
|
||
'operations_pressure_kg' => $lineList->working_pressure_mpa,
|
||
'pipe_material_class' => $lineList->pipe_material_class,
|
||
'type_of_joint' => $jointItem,
|
||
'pwht' => $lineList->pwht,
|
||
'pwht_field' => $lineList->pwht,
|
||
'ht' => $ht,
|
||
'ndt' => $lineList->ndt,
|
||
//'ndt' => $lineList->painting_cycle,
|
||
'piping_class_according_to_gost' => $lineList->category,
|
||
'piping_group' => $lineList->fluid_group,
|
||
'rev' => $lineList->rev
|
||
];
|
||
|
||
Log::debug("NDE Matrix işlemi başlatılıyor", [
|
||
'line_no' => $lineList->line_no,
|
||
'joint_type' => $jointItem,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'unit' => $lineList->unit,
|
||
'data_to_process' => $data
|
||
]);
|
||
|
||
// UNIQUE CONSTRAINT CHECK: line + type_of_joint + fluid must be unique
|
||
$uniqueKey = [
|
||
'line' => $lineList->line_no,
|
||
'type_of_joint' => $jointItem,
|
||
'fluid' => $lineList->fluid_code,
|
||
];
|
||
|
||
$already = NdeMatrix::where($uniqueKey)->first();
|
||
|
||
if($already) {
|
||
Log::debug("Mevcut NDE Matrix kaydı bulundu, güncelleme yapılacak", [
|
||
'existing_record_id' => $already->id,
|
||
'line_no' => $lineList->line_no,
|
||
'joint_type' => $jointItem,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'unique_constraint' => $uniqueKey
|
||
]);
|
||
|
||
unset($data['pwht_field']);
|
||
|
||
try {
|
||
$updateResult = NdeMatrix::where($uniqueKey)->update($data);
|
||
|
||
Log::debug("NDE Matrix kaydı başarıyla güncellendi", [
|
||
'affected_rows' => $updateResult,
|
||
'line_no' => $lineList->line_no,
|
||
'joint_type' => $jointItem,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'updated_data' => $data
|
||
]);
|
||
|
||
$update++;
|
||
} catch (\Exception $e) {
|
||
Log::error("NDE Matrix güncelleme hatası", [
|
||
'line_no' => $lineList->line_no,
|
||
'joint_type' => $jointItem,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'error_message' => $e->getMessage(),
|
||
'data' => $data
|
||
]);
|
||
throw $e;
|
||
}
|
||
} else {
|
||
Log::debug("Yeni NDE Matrix kaydı oluşturulacak", [
|
||
'line_no' => $lineList->line_no,
|
||
'joint_type' => $jointItem,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'unique_constraint' => $uniqueKey,
|
||
'new_data' => $data
|
||
]);
|
||
|
||
try {
|
||
// updateOrCreate kullanarak duplicate riski ortadan kaldır
|
||
$ndeRecord = NdeMatrix::updateOrCreate($uniqueKey, $data);
|
||
|
||
// Hemen transaction'ı commit edelim ve kontrol edelim
|
||
DB::commit();
|
||
|
||
// Kaydın tabloda olup olmadığını kontrol et
|
||
$dbCheck = DB::table('nde_matrices')->where('id', $ndeRecord->id)->first();
|
||
|
||
Log::debug("NDE Matrix Database Check", [
|
||
'ndeRecord_id' => $ndeRecord->id,
|
||
'wasRecentlyCreated' => $ndeRecord->wasRecentlyCreated,
|
||
'db_record_exists' => $dbCheck ? 'YES' : 'NO',
|
||
'db_record_data' => $dbCheck ? $dbCheck : 'NULL'
|
||
]);
|
||
|
||
// Transaction'ı tekrar başlat
|
||
DB::beginTransaction();
|
||
|
||
$action = $ndeRecord->wasRecentlyCreated ? 'created' : 'updated';
|
||
|
||
Log::debug("NDE Matrix kaydı başarıyla işlendi", [
|
||
'record_id' => $ndeRecord->id,
|
||
'action' => $action,
|
||
'line_no' => $lineList->line_no,
|
||
'joint_type' => $jointItem,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'data' => $ndeRecord->toArray()
|
||
]);
|
||
|
||
if($action === 'created') {
|
||
$create++;
|
||
} else {
|
||
$update++;
|
||
}
|
||
} catch (\Exception $e) {
|
||
Log::error("NDE Matrix oluşturma/güncelleme hatası", [
|
||
'line_no' => $lineList->line_no,
|
||
'joint_type' => $jointItem,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'error_message' => $e->getMessage(),
|
||
'unique_constraint' => $uniqueKey,
|
||
'data' => $data
|
||
]);
|
||
throw $e;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
$count++;
|
||
|
||
}
|
||
|
||
Log::debug("NDE Matrix affected rows: $update updated $create created");
|
||
|
||
DB::commit();
|
||
} catch (\Exception $e) {
|
||
DB::rollBack();
|
||
Log::error("NDE Matrix işlemi hatası: " . $e->getMessage());
|
||
throw $e;
|
||
}
|
||
|
||
|
||
// Paint System sync işlemi için ayrı transaction
|
||
DB::beginTransaction();
|
||
try {
|
||
$lineLists = DB::table('line_lists')
|
||
->where("id", $id)
|
||
->whereNotNull('painting_cycle') // paint_cycle boş olmayan verileri al
|
||
->get();
|
||
|
||
Log::debug("Paint System sync başlatılıyor", [
|
||
'id' => $id,
|
||
'found_records_with_paint_cycle' => count($lineLists)
|
||
]);
|
||
|
||
$count = 0;
|
||
foreach ($lineLists as $line) {
|
||
|
||
Log::debug("Paint System kaydı işleniyor", [
|
||
'line_id' => $line->id ?? 'N/A',
|
||
'line_no' => $line->line_no ?? 'NULL',
|
||
'painting_cycle' => $line->painting_cycle ?? 'NULL',
|
||
'rev' => $line->rev ?? 'NULL',
|
||
'fluid_code' => $line->fluid_code ?? 'NULL'
|
||
]);
|
||
// paint_cycle'a göre update veya insert işlemi yap
|
||
// Check if record exists
|
||
$existingRecord = DB::table('paint_systems')
|
||
->where('paint_cycle', $line->painting_cycle)
|
||
->first();
|
||
|
||
if ($existingRecord) {
|
||
// Update existing record
|
||
DB::table('paint_systems')
|
||
->where('paint_cycle', $line->painting_cycle)
|
||
->update([
|
||
'paint_cycle' => $line->painting_cycle,
|
||
'revision' => $line->rev,
|
||
]);
|
||
} else {
|
||
// Insert new record
|
||
DB::table('paint_systems')->insert([
|
||
'paint_cycle' => $line->painting_cycle,
|
||
'revision' => $line->rev,
|
||
]);
|
||
}
|
||
|
||
DB::table('color_systems')->updateOrInsert(
|
||
['fluid_code' => $line->fluid_code], // Güncellenecek veya eklenecek satırı belirler
|
||
[
|
||
'fluid_code_description' => $line->fluid_ru ?? "",
|
||
'fluid_code' => $line->fluid_code,
|
||
'design_temperature' => $line->design_temperature ?? "",
|
||
'working_temperature' => $line->working_temperature ?? "",
|
||
'unit' => $line->unit ?? "",
|
||
|
||
]
|
||
);
|
||
|
||
$count++;
|
||
}
|
||
|
||
|
||
Log::debug("Sync Paint System $count row update or create");
|
||
|
||
DB::commit();
|
||
} catch (\Exception $e) {
|
||
DB::rollBack();
|
||
Log::error("Paint System sync hatası: " . $e->getMessage());
|
||
throw $e;
|
||
}
|
||
|
||
// Sync Paint Systems to Paint Matrices
|
||
Log::debug("Starting Paint Systems to Paint Matrices synchronization...");
|
||
|
||
// Get line list data for the current ID
|
||
$lineList = DB::table('line_lists')->where('id', $id)->first();
|
||
|
||
if (!$lineList || empty($lineList->painting_cycle)) {
|
||
Log::debug("No valid paint cycle found for line list ");
|
||
return;
|
||
}
|
||
|
||
// Get paint system for the current line's paint_cycle
|
||
$paintSystem = DB::table('paint_systems')
|
||
->where('paint_cycle', $lineList->painting_cycle)
|
||
->first();
|
||
|
||
if (!$paintSystem) {
|
||
Log::debug("No paint system found for paint cycle: {$lineList->painting_cycle}");
|
||
|
||
return;
|
||
}
|
||
|
||
// Get color system for the current line's fluid_code
|
||
$colorSystem = DB::table('color_systems')
|
||
->where('fluid_code', $lineList->fluid_code)
|
||
->first();
|
||
|
||
if (!$colorSystem) {
|
||
Log::debug("No color system found for fluid code: {$lineList->fluid_code}");
|
||
|
||
return;
|
||
}
|
||
|
||
// Get paint matrix data for the current line's fluid_code and line number
|
||
$paintMatrix = DB::table('paint_matrices')
|
||
->where('fluid_code', $lineList->fluid_code)
|
||
->where('line', $lineList->line_no)
|
||
->first();
|
||
|
||
// Get RAL values from color_systems
|
||
$ral1 = $colorSystem->ral_1 ?? '';
|
||
$ral2 = $colorSystem->ral_2 ?? '';
|
||
$ral3 = $colorSystem->ral_3 ?? '';
|
||
|
||
// Get Russian color descriptions
|
||
$ralCodes = j(setting("ral-codes"));
|
||
|
||
// Helper function to find Russian color description for a RAL code
|
||
if(!function_exists('findRussianColorDescription')) {
|
||
function findRussianColorDescription($ralCodes, $ralCode) {
|
||
if (empty($ralCode)) {
|
||
return '';
|
||
}
|
||
|
||
foreach ($ralCodes as $entry) {
|
||
if (isset($entry['ral_code']) && $entry['ral_code'] == $ralCode && isset($entry['ru'])) {
|
||
return $entry['ru'];
|
||
}
|
||
}
|
||
|
||
return '';
|
||
}
|
||
}
|
||
|
||
// Get Russian color descriptions from RAL codes
|
||
$colorRu1 = findRussianColorDescription($ralCodes, $ral1);
|
||
$colorRu2 = findRussianColorDescription($ralCodes, $ral2);
|
||
$colorRu3 = findRussianColorDescription($ralCodes, $ral3);
|
||
|
||
// Find paint matrices with matching paint_cycle
|
||
$paintMatrices = DB::table('paint_matrices')
|
||
->where('paint_cycle', $lineList->painting_cycle)
|
||
->get();
|
||
|
||
$updatedCount = 0;
|
||
foreach ($paintMatrices as $paintMatrix) {
|
||
$updateData = [
|
||
// Paint System verileri
|
||
'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),
|
||
|
||
// Color System verileri
|
||
'fluid_code_description' => $colorSystem->fluid_code_description,
|
||
'design_temperature' => $colorSystem->design_temperature,
|
||
'operation_temperature' => $colorSystem->working_temperature,
|
||
'ral_code_1' => $ral1,
|
||
'ral_code_2' => $ral2,
|
||
'ral_code_3' => $ral3,
|
||
'colour_1' => $colorRu1,
|
||
'colour_2' => $colorRu2,
|
||
'colour_3' => $colorRu3,
|
||
'updated_at' => now()
|
||
];
|
||
|
||
// Update paint matrix
|
||
$result = DB::table('paint_matrices')
|
||
->where('id', $paintMatrix->id)
|
||
->update($updateData);
|
||
|
||
if ($result) {
|
||
$updatedCount++;
|
||
}
|
||
}
|
||
|
||
Log::debug("Paint Systems to Paint Matrices synchronization completed. Updated $updatedCount records.");
|
||
//Construction Paint Logs Sync - Line Lists to Construction Paint Logs
|
||
Log::debug("Starting Construction Paint Logs synchronization from Line Lists...");
|
||
|
||
// Get line list data for the current ID
|
||
$lineList = db("line_lists")->where("id", $id)->first();
|
||
|
||
if ($lineList) {
|
||
// Skip processing if essential fields are missing
|
||
if (empty($lineList->line_no)) {
|
||
Log::debug("Skipping Construction Paint Logs sync - missing line number");
|
||
|
||
return;
|
||
}
|
||
|
||
// Retrieve all weld logs with the same line number
|
||
$allWeldLogs = db("weld_logs")
|
||
->where("line_number", $lineList->line_no)
|
||
->get();
|
||
|
||
Log::debug("Syncing " . count($allWeldLogs) . " weld logs with line number: " . $lineList->line_no);
|
||
|
||
// Get paint matrix data for RAL values
|
||
$paintMatrix = DB::table('paint_matrices')
|
||
->where('fluid_code', $lineList->fluid_code)
|
||
->where('line', $lineList->line_no)
|
||
->first();
|
||
|
||
if ($paintMatrix) {
|
||
Log::debug("Found paint matrix data for line: {$lineList->line_no}");
|
||
} else {
|
||
Log::debug("No paint matrix data found for line: {$lineList->line_no}");
|
||
}
|
||
|
||
// Get paint system data for the current line's paint_cycle
|
||
$paintSystem = DB::table('paint_systems')
|
||
->where('paint_cycle', $lineList->painting_cycle)
|
||
->first();
|
||
|
||
$createdCount = 0;
|
||
$updatedCount = 0;
|
||
|
||
// Tekrarlanan kayıt güncellemelerini önlemek için izlenen ID'ler
|
||
$processedPaintFollowUpIds = [];
|
||
|
||
// Process each weld log separately
|
||
foreach ($allWeldLogs as $weldLogData) {
|
||
// Skip if spool is empty
|
||
if (empty($weldLogData->spool_number)) {
|
||
continue;
|
||
}
|
||
|
||
// Define where condition (for determining if record exists)
|
||
$whereCondition = [
|
||
'line' => $lineList->line_no,
|
||
'spool' => $weldLogData->spool_number,
|
||
'unit' => $lineList->unit
|
||
];
|
||
|
||
// Define unique constraint condition (line + spool + unit + painting_system_type_1)
|
||
$uniqueConstraintCondition = [
|
||
'line' => $lineList->line_no,
|
||
'spool' => $weldLogData->spool_number,
|
||
'unit' => $lineList->unit,
|
||
'painting_system_type_1' => $lineList->painting_cycle
|
||
];
|
||
|
||
// Prepare data for update/insert
|
||
$updateData = [
|
||
// Fields to be synchronized from line_lists
|
||
'painting_system_type_1' => $lineList->painting_cycle ?? '',
|
||
'painting_system_type_2' => $lineList->painting_cycle ?? '',
|
||
'rev' => $lineList->rev ?? '',
|
||
'isolation_info' => $lineList->external_finish_type ?? '',
|
||
'engineering' => $lineList->engineering ?? '',
|
||
'line' => $lineList->line_no,
|
||
|
||
// Additional fields from line_lists
|
||
'fluid_code' => $lineList->fluid_code ?? '',
|
||
'fluid_code_description' => $lineList->fluid_ru ?? '',
|
||
'area' => $weldLogData->project ?? '',
|
||
'unit' => $lineList->unit ?? '',
|
||
|
||
// Fields from weld_logs
|
||
'test_package' => $weldLogData->test_package_no ?? '',
|
||
'spool' => $weldLogData->spool_number ?? '',
|
||
'iso_drawings' => $weldLogData->iso_number ?? '',
|
||
'spool_status' => $weldLogData->spool_status ?? 'Waiting',
|
||
'construction_report_no' => $weldLogData->weld_map_no ?? '',
|
||
|
||
// Dimensions from weld_logs
|
||
'dn_1' => $weldLogData->nps_1 ?? '',
|
||
'dn_2' => $weldLogData->nps_2 ?? '',
|
||
'dn_3' => '',
|
||
|
||
// Paint system fields if available
|
||
'brend_name_1' => $paintSystem->brand_name_1 ?? $paintSystem->brend_name_1 ?? '',
|
||
'thickness_1' => $paintSystem->thickness_1 ?? '',
|
||
'brend_name_2' => $paintSystem->brand_name_2 ?? $paintSystem->brend_name_2 ?? '',
|
||
'thickness_2' => $paintSystem->thickness_2 ?? '',
|
||
'brend_name_3' => $paintSystem->brand_name_3 ?? $paintSystem->brend_name_3 ?? '',
|
||
'thickness_3' => $paintSystem->thickness_3 ?? '',
|
||
'total_layer' => $paintSystem->total_kg ?? $paintSystem->total_microns ?? '',
|
||
|
||
// Timestamps
|
||
'updated_at' => now(),
|
||
'created_at' => DB::raw('CASE WHEN created_at IS NULL THEN NOW() ELSE created_at END')
|
||
];
|
||
|
||
// Remove empty fields to avoid overwriting existing data with empty values
|
||
|
||
/*
|
||
foreach ($updateData as $key => $value) {
|
||
if ($value === '' && !in_array($key, ['painting_system_type_1', 'line'])) { // Keep essential fields
|
||
unset($updateData[$key]);
|
||
}
|
||
}
|
||
*/
|
||
|
||
|
||
// Add RAL values from paint_matrices if available
|
||
if ($paintMatrix) {
|
||
$updateData['ral_1'] = $paintMatrix->ral_code_1 ?? '';
|
||
$updateData['ral_2'] = $paintMatrix->ral_code_2 ?? '';
|
||
$updateData['ral_3'] = $paintMatrix->ral_code_3 ?? '';
|
||
}
|
||
|
||
// Weld log kontrolü yap - Shop joint kontrolü
|
||
$hasShopJoint = db("weld_logs")
|
||
->where('line_number', $lineList->line_no)
|
||
->where('spool_number', $weldLogData->spool_number)
|
||
->where('type_of_joint', 'S')
|
||
->exists();
|
||
|
||
Log::debug("Shop joint kontrolü", [
|
||
'line_number' => $lineList->line_no,
|
||
'spool_number' => $weldLogData->spool_number ?? 'NULL',
|
||
'hasShopJoint' => $hasShopJoint ? 'true' : 'false',
|
||
'spool_empty' => empty($weldLogData->spool_number) ? 'true' : 'false'
|
||
]);
|
||
|
||
// Eğer Shop joint varsa ve spool numarası varsa işlemi gerçekleştir
|
||
if ($hasShopJoint && !empty($weldLogData->spool_number)) {
|
||
// Spool status'u güncelle
|
||
$updateData['spool_status'] = $weldLogData->spool_status;
|
||
|
||
// Check if record exists for this unique combination (line + spool + unit + painting_system_type_1)
|
||
$existingRecord = DB::table("construction_paint_logs")
|
||
->where($uniqueConstraintCondition)
|
||
->first();
|
||
|
||
// Also check if there are any records with different painting system for same line/spool/unit
|
||
$recordsWithDifferentPaintSystem = DB::table("construction_paint_logs")
|
||
->where($whereCondition)
|
||
->where('painting_system_type_1', '!=', $lineList->painting_cycle)
|
||
->get();
|
||
|
||
if ($existingRecord) {
|
||
// Record with same painting system already exists, just update it
|
||
DB::table("construction_paint_logs")
|
||
->where('id', $existingRecord->id)
|
||
->update($updateData);
|
||
|
||
$updatedCount++;
|
||
Log::debug("Updated existing Construction Paint Log with same painting system", [
|
||
'record_id' => $existingRecord->id,
|
||
'line' => $lineList->line_no,
|
||
'spool' => $weldLogData->spool_number,
|
||
'painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
|
||
} else {
|
||
// No record with this painting system exists
|
||
|
||
// Check if we need to handle records with different painting systems
|
||
if ($recordsWithDifferentPaintSystem->count() > 0) {
|
||
Log::debug("Found records with different painting systems for same spool", [
|
||
'line' => $lineList->line_no,
|
||
'spool' => $weldLogData->spool_number,
|
||
'current_painting_cycle' => $lineList->painting_cycle,
|
||
'existing_records_count' => $recordsWithDifferentPaintSystem->count()
|
||
]);
|
||
|
||
// Handle each record with different painting system
|
||
foreach ($recordsWithDifferentPaintSystem as $oldRecord) {
|
||
// Check if any date field is filled (any painting activity started)
|
||
$hasAnyDateFilled = !empty($oldRecord->blasting_date) ||
|
||
!empty($oldRecord->blasting_finish_date) ||
|
||
!empty($oldRecord->painting_date_1) ||
|
||
!empty($oldRecord->painting_finish_date_1) ||
|
||
!empty($oldRecord->painting_date_2) ||
|
||
!empty($oldRecord->painting_finish_date_2) ||
|
||
!empty($oldRecord->painting_date_3) ||
|
||
!empty($oldRecord->painting_finish_date_3) ||
|
||
!empty($oldRecord->rfi_date_1) ||
|
||
!empty($oldRecord->rfi_date_2) ||
|
||
!empty($oldRecord->rfi_date_3);
|
||
|
||
if ($hasAnyDateFilled) {
|
||
// Mark old record as HOLD
|
||
DB::table("construction_paint_logs")
|
||
->where('id', $oldRecord->id)
|
||
->update([
|
||
'spool_status' => $oldRecord->spool_status,
|
||
'updated_at' => now()
|
||
]);
|
||
|
||
Log::debug("Marked old Construction Paint Log as HOLD due to painting system change", [
|
||
'record_id' => $oldRecord->id,
|
||
'old_painting_cycle' => $oldRecord->painting_system_type_1,
|
||
'new_painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
} else {
|
||
// Update old record to new painting system if no dates filled
|
||
DB::table("construction_paint_logs")
|
||
->where('id', $oldRecord->id)
|
||
->update([
|
||
'painting_system_type_1' => $lineList->painting_cycle,
|
||
'painting_system_type_2' => $lineList->painting_cycle,
|
||
'updated_at' => now()
|
||
]);
|
||
|
||
$updatedCount++;
|
||
Log::debug("Updated old Construction Paint Log to new painting system", [
|
||
'record_id' => $oldRecord->id,
|
||
'old_painting_cycle' => $oldRecord->painting_system_type_1,
|
||
'new_painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
|
||
// Skip creating new record since we updated existing one
|
||
continue 2;
|
||
}
|
||
}
|
||
}
|
||
|
||
// Create new record with new painting system
|
||
$newRecordData = $updateData;
|
||
$newRecordData['blasting_date'] = null;
|
||
$newRecordData['blasting_finish_date'] = null;
|
||
$newRecordData['painting_date_1'] = null;
|
||
$newRecordData['painting_finish_date_1'] = null;
|
||
$newRecordData['painting_date_2'] = null;
|
||
$newRecordData['painting_finish_date_2'] = null;
|
||
$newRecordData['painting_date_3'] = null;
|
||
$newRecordData['painting_finish_date_3'] = null;
|
||
$newRecordData['rfi_date_1'] = null;
|
||
$newRecordData['rfi_date_2'] = null;
|
||
$newRecordData['rfi_date_3'] = null;
|
||
$newRecordData['created_at'] = now();
|
||
$newRecordData['updated_at'] = now();
|
||
|
||
DB::table("construction_paint_logs")->insert($newRecordData);
|
||
$createdCount++;
|
||
Log::debug("Created new Construction Paint Log with new painting system", [
|
||
'line' => $lineList->line_no,
|
||
'spool' => $weldLogData->spool_number,
|
||
'painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
}
|
||
} else if (empty($weldLogData->spool_number)) {
|
||
Log::debug("Construction paint log atlandı - spool number boş", [
|
||
'line_number' => $lineList->line_no,
|
||
'weld_log_id' => $weldLogData->id ?? 'N/A'
|
||
]);
|
||
} else {
|
||
Log::debug("Construction paint log atlandı - shop joint bulunamadı", [
|
||
'line_number' => $lineList->line_no,
|
||
'spool_number' => $weldLogData->spool_number,
|
||
'hasShopJoint' => $hasShopJoint ? 'true' : 'false'
|
||
]);
|
||
}
|
||
}
|
||
|
||
Log::debug("Construction Paint Logs synchronization completed: $createdCount created, $updatedCount updated");
|
||
} else {
|
||
Log::debug("Line List record not found for ID: $id");
|
||
}
|
||
|
||
// Painting cycle change handling is now integrated into the main construction paint logs sync above
|
||
// No separate handling needed here as it's already covered in the main loop
|
||
|
||
// Paint Follow Ups sync from Line Lists
|
||
DB::beginTransaction();
|
||
try {
|
||
Log::debug("Paint Follow Ups senkronizasyonu başlatılıyor...");
|
||
|
||
// Get line list data for the current ID
|
||
$lineList = db("line_lists")->where("id", $id)->first();
|
||
|
||
if ($lineList) {
|
||
// Skip processing if essential fields are missing
|
||
if (empty($lineList->line_no)) {
|
||
Log::debug("Paint Follow Ups senkronizasyonu atlanıyor - line_number eksik");
|
||
DB::commit();
|
||
return;
|
||
}
|
||
|
||
// Check for painting cycle deletion (similar to construction_paint_logs logic)
|
||
if(isset($beforeData) && $beforeData->painting_cycle !="" && $lineList->painting_cycle == "") {
|
||
|
||
Log::debug("Paint Follow Ups - Painting cycle siliniyor - ilgili verileri temizleme başlatılıyor", [
|
||
'before_painting_cycle' => $beforeData->painting_cycle,
|
||
'current_painting_cycle' => $lineList->painting_cycle,
|
||
'line_no' => $lineList->line_no,
|
||
'fluid_code' => $lineList->fluid_code
|
||
]);
|
||
|
||
$deletedPaintFollowUps = db('paint_follow_ups')
|
||
->where([
|
||
'line' => $lineList->line_no,
|
||
'area' => $lineList->unit,
|
||
])
|
||
->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')
|
||
->delete();
|
||
|
||
Log::debug("Paint Follow Ups painting cycle temizleme tamamlandı", [
|
||
'deleted_paint_follow_ups' => $deletedPaintFollowUps,
|
||
'line_no' => $lineList->line_no,
|
||
'fluid_code' => $lineList->fluid_code
|
||
]);
|
||
|
||
DB::commit();
|
||
return;
|
||
}
|
||
|
||
// Get all weld logs with this line number
|
||
$allWeldLogs = db("weld_logs")
|
||
->where("line_number", $lineList->line_no)
|
||
// ->groupBy('spool_number')
|
||
->get();
|
||
|
||
Log::debug("Line number " . $lineList->line_no . " için " . count($allWeldLogs) . " adet kayıt senkronize ediliyor");
|
||
|
||
// Get paint matrix for this line number
|
||
$paintMatrix = PaintMatrix::where('line', $lineList->line_no)
|
||
->where('fluid_code', $lineList->fluid_code)
|
||
->first();
|
||
|
||
if (!$paintMatrix) {
|
||
// Create paint matrix if it doesn't exist
|
||
$paintMatrixData = [
|
||
'project' => $allWeldLogs->first()->project ?? '',
|
||
'area' => $lineList->unit,
|
||
'description' => "PIPE",
|
||
'line' => $lineList->line_no,
|
||
'fluid_code_description' => $lineList->fluid_ru,
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'design_temperature' => $lineList->design_temperature,
|
||
'operation_temperature' => $lineList->working_temperature,
|
||
'paint_cycle' => $lineList->painting_cycle,
|
||
'created_at' => now(),
|
||
'updated_at' => now()
|
||
];
|
||
|
||
$paintMatrix = PaintMatrix::create($paintMatrixData);
|
||
Log::debug("Created new Paint Matrix for line: {$lineList->line_no}");
|
||
}
|
||
|
||
// Load temperature settings for volume and temperature calculations
|
||
$temperatures = j(setting("temperatures"));
|
||
$todayTempData = null;
|
||
|
||
// Get blasting_date from construction_paint_logs to determine temperature data
|
||
$blastingDateRecord = db('construction_paint_logs')
|
||
->where('line', $lineList->line_no)
|
||
->whereNotNull('blasting_date')
|
||
->first();
|
||
if (!is_null($temperatures) && $blastingDateRecord && !empty($blastingDateRecord->blasting_date)) {
|
||
$blastingDate = Carbon::parse($blastingDateRecord->blasting_date);
|
||
$dayOfYear = $blastingDate->format('z');
|
||
$todayTempData = $temperatures[$dayOfYear] ?? null;
|
||
|
||
Log::debug('Temperature data loaded based on blasting_date:', [
|
||
'blasting_date' => $blastingDateRecord->blasting_date,
|
||
'day_of_year' => $dayOfYear,
|
||
'temp_data' => $todayTempData
|
||
]);
|
||
} else {
|
||
if (is_null($temperatures)) {
|
||
Log::debug('Temperature settings not found, temperature updates will be skipped');
|
||
} else {
|
||
Log::debug('No blasting_date found or blasting_date is empty, temperature updates will be skipped');
|
||
}
|
||
}
|
||
|
||
// Calculate volume data for F and S type joints
|
||
$fAvgNps = $allWeldLogs->where("type_of_joint", "F")->avg("nps_1");
|
||
$sAvgNps = $allWeldLogs->where("type_of_joint", "S")->avg("nps_1");
|
||
|
||
// Get MTO total for S type volume calculation
|
||
$mtoTotal = db("m_t_o_s")
|
||
->where("description_en", "like", "%pipe%")
|
||
->where("line", $lineList->line_no)
|
||
->selectRaw("SUM(quantity * POWER(odmm_1/2000, 2) * PI()) AS total")
|
||
->first()->total ?? 0;
|
||
|
||
$fVolume = round(pi() * pow($fAvgNps, 2) * 200, 2);
|
||
$sVolume = round($mtoTotal, 2);
|
||
|
||
Log::debug('Volume calculations:', [
|
||
'f_avg_nps' => $fAvgNps,
|
||
's_avg_nps' => $sAvgNps,
|
||
'mto_total' => $mtoTotal,
|
||
'fVolume' => $fVolume,
|
||
'sVolume' => $sVolume
|
||
]);
|
||
|
||
$paintFollowUpCreated = 0;
|
||
$paintFollowUpUpdated = 0;
|
||
|
||
|
||
// Process each weld log
|
||
foreach ($allWeldLogs as $weldLog) {
|
||
// Skip if fluid code is empty
|
||
|
||
if (empty($weldLog->fluid_code)) {
|
||
Log::debug("Kayıt için Paint Follow Ups senkronizasyonu atlanıyor - fluid_code eksik");
|
||
continue;
|
||
}
|
||
|
||
// Base data for paint follow up
|
||
$basePaintFollowUpData = [
|
||
// Proje Bilgileri
|
||
'project' => $weldLog->project ?? '',
|
||
'description' => "PIPE",
|
||
'area' => $lineList->unit ?? '',
|
||
// 'protocol_date_cleaning' => $blastingDate ?? '',
|
||
|
||
// Hat Bilgileri
|
||
'line' => $lineList->line_no,
|
||
'iso_number' => $weldLog->iso_number ?? '',
|
||
'fluid_code' => $lineList->fluid_code,
|
||
'fluid_code_description' => $lineList->fluid_ru ?? '',
|
||
|
||
// Boya bilgileri
|
||
'cycle' => $lineList->painting_cycle ?? '',
|
||
|
||
// Primer coat bilgileri
|
||
'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 bilgileri
|
||
'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 bilgileri
|
||
'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 ?? '',
|
||
|
||
// Surface data
|
||
'surface_roughness' => $paintMatrix->surface_preparation ?? '',
|
||
|
||
// Durum bilgileri
|
||
'status' => 'In Progress',
|
||
'updated_at' => now()
|
||
];
|
||
// Check if line has shop joints
|
||
$hasShopJoint = db("weld_logs")
|
||
->where('line_number', $lineList->line_no)
|
||
->where('spool_number', $weldLog->spool_number)
|
||
->where('type_of_joint', 'S')
|
||
->exists();
|
||
|
||
Log::debug("Paint Follow Up SHOP joint kontrolü", [
|
||
'line_number' => $lineList->line_no,
|
||
'spool_number' => $weldLog->spool_number ?? 'NULL',
|
||
'hasShopJoint' => $hasShopJoint ? 'true' : 'false',
|
||
'spool_empty' => empty($weldLog->spool_number) ? 'true' : 'false'
|
||
]);
|
||
|
||
// Process SHOP entry - using spool_number
|
||
if ($hasShopJoint && !empty($weldLog->spool_number)) {
|
||
// Prepare shop data
|
||
$shopData = $basePaintFollowUpData;
|
||
$shopData['location'] = 'SHOP';
|
||
$shopData['spool_no_joint_no'] = $weldLog->spool_number;
|
||
$shopData['surface_roughness'] = $paintMatrix->surface_preparation ?? '';
|
||
|
||
// Add temperature and volume data for SHOP
|
||
if ($todayTempData) {
|
||
$shopData['substrate_temprature'] = $todayTempData['temp_material_shop'];
|
||
$shopData['ambient_temprature'] = $todayTempData['shop_ambient'];
|
||
}
|
||
$shopData['volume_1'] = $sVolume;
|
||
$shopData['volume_2'] = $sVolume;
|
||
$shopData['volume_3'] = $sVolume;
|
||
$shopData['total_volume'] = $sVolume * 3;
|
||
|
||
// Unique constraint condition for SHOP records (line + spool + cycle)
|
||
$shopUniqueConstraintCondition = [
|
||
'line' => $lineList->line_no,
|
||
'spool_no_joint_no' => $weldLog->spool_number,
|
||
'cycle' => $lineList->painting_cycle,
|
||
'location' => 'SHOP'
|
||
];
|
||
|
||
// Where condition for finding existing records (without cycle constraint)
|
||
$shopWhereCondition = [
|
||
'line' => $lineList->line_no,
|
||
'spool_no_joint_no' => $weldLog->spool_number,
|
||
'location' => 'SHOP'
|
||
];
|
||
|
||
// Find existing record with same painting cycle
|
||
$existingShopRecord = db("paint_follow_ups")
|
||
->where($shopUniqueConstraintCondition)
|
||
->first();
|
||
|
||
// Also check if there are any records with different painting cycle for same line/spool
|
||
$recordsWithDifferentPaintCycle = db("paint_follow_ups")
|
||
->where($shopWhereCondition)
|
||
->where('cycle', '!=', $lineList->painting_cycle)
|
||
->get();
|
||
|
||
if ($existingShopRecord) {
|
||
// Record with same painting cycle already exists, just update it
|
||
// Check if ALL coating dates are empty (no painting activity started)
|
||
$hasAllEmptyDates = empty($existingShopRecord->primer_coating_start_date) &&
|
||
empty($existingShopRecord->primer_coating_finish_date) &&
|
||
empty($existingShopRecord->start_intermediate_date2) &&
|
||
empty($existingShopRecord->finish_intermediate_date2) &&
|
||
empty($existingShopRecord->final_coat_start_date3) &&
|
||
empty($existingShopRecord->final_coat_finish_date3);
|
||
|
||
if ($hasAllEmptyDates) {
|
||
// Update existing record only if ALL dates are empty
|
||
db("paint_follow_ups")
|
||
->where('id', $existingShopRecord->id)
|
||
->update($shopData);
|
||
$paintFollowUpUpdated++;
|
||
Log::debug("Updated SHOP paint follow up {$existingShopRecord->id} with same painting cycle - all dates were empty");
|
||
} else {
|
||
// Even if dates are filled, update temperature and volume fields if they are empty
|
||
$tempVolumeUpdateData = [];
|
||
|
||
// Add temperature data if available and field is empty
|
||
if ($todayTempData) {
|
||
if (empty($existingShopRecord->substrate_temprature)) {
|
||
$tempVolumeUpdateData['substrate_temprature'] = $todayTempData['temp_material_shop'];
|
||
}
|
||
if (empty($existingShopRecord->ambient_temprature)) {
|
||
$tempVolumeUpdateData['ambient_temprature'] = $todayTempData['shop_ambient'];
|
||
}
|
||
}
|
||
|
||
// Add volume data if fields are empty
|
||
if (empty($existingShopRecord->volume_1)) {
|
||
$tempVolumeUpdateData['volume_1'] = $sVolume;
|
||
}
|
||
if (empty($existingShopRecord->volume_2)) {
|
||
$tempVolumeUpdateData['volume_2'] = $sVolume;
|
||
}
|
||
if (empty($existingShopRecord->volume_3)) {
|
||
$tempVolumeUpdateData['volume_3'] = $sVolume;
|
||
}
|
||
if (empty($existingShopRecord->total_volume)) {
|
||
$tempVolumeUpdateData['total_volume'] = $sVolume * 3;
|
||
}
|
||
|
||
if (!empty($tempVolumeUpdateData)) {
|
||
db("paint_follow_ups")
|
||
->where('id', $existingShopRecord->id)
|
||
->update($tempVolumeUpdateData);
|
||
Log::debug("Updated temperature/volume fields for SHOP record ID {$existingShopRecord->id}", [
|
||
'updated_fields' => array_keys($tempVolumeUpdateData)
|
||
]);
|
||
} else {
|
||
Log::debug("Skipping update for SHOP record ID {$existingShopRecord->id} - some dates are filled and no empty temp/volume fields");
|
||
}
|
||
}
|
||
} else {
|
||
// No record with this painting cycle exists
|
||
|
||
// Check if we need to handle records with different painting cycles
|
||
if ($recordsWithDifferentPaintCycle->count() > 0) {
|
||
Log::debug("Found SHOP records with different painting cycles for same spool", [
|
||
'line' => $lineList->line_no,
|
||
'spool' => $weldLog->spool_number,
|
||
'current_painting_cycle' => $lineList->painting_cycle,
|
||
'existing_records_count' => $recordsWithDifferentPaintCycle->count()
|
||
]);
|
||
|
||
// Handle each record with different painting cycle
|
||
foreach ($recordsWithDifferentPaintCycle as $oldRecord) {
|
||
// Check if any date field is filled (any painting activity started)
|
||
$hasAnyDateFilled = !empty($oldRecord->primer_coating_start_date) ||
|
||
!empty($oldRecord->primer_coating_finish_date) ||
|
||
!empty($oldRecord->start_intermediate_date2) ||
|
||
!empty($oldRecord->finish_intermediate_date2) ||
|
||
!empty($oldRecord->final_coat_start_date3) ||
|
||
!empty($oldRecord->final_coat_finish_date3);
|
||
|
||
if ($hasAnyDateFilled) {
|
||
// Mark old record status as HOLD (keep existing record but mark as inactive)
|
||
db("paint_follow_ups")
|
||
->where('id', $oldRecord->id)
|
||
->update([
|
||
'status' => 'HOLD',
|
||
'updated_at' => now()
|
||
]);
|
||
|
||
Log::debug("Marked old SHOP Paint Follow Up as HOLD due to painting cycle change", [
|
||
'record_id' => $oldRecord->id,
|
||
'old_painting_cycle' => $oldRecord->cycle,
|
||
'new_painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
} else {
|
||
// Update old record to new painting cycle if no dates filled
|
||
db("paint_follow_ups")
|
||
->where('id', $oldRecord->id)
|
||
->update([
|
||
'cycle' => $lineList->painting_cycle,
|
||
'updated_at' => now()
|
||
]);
|
||
|
||
$paintFollowUpUpdated++;
|
||
Log::debug("Updated old SHOP Paint Follow Up to new painting cycle", [
|
||
'record_id' => $oldRecord->id,
|
||
'old_painting_cycle' => $oldRecord->cycle,
|
||
'new_painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
|
||
// Skip creating new record since we updated existing one
|
||
continue 2;
|
||
}
|
||
}
|
||
}
|
||
|
||
// Create new record with new painting cycle
|
||
$newShopData = $shopData;
|
||
$newShopData['primer_coating_start_date'] = null;
|
||
$newShopData['primer_coating_finish_date'] = null;
|
||
$newShopData['start_intermediate_date2'] = null;
|
||
$newShopData['finish_intermediate_date2'] = null;
|
||
$newShopData['final_coat_start_date3'] = null;
|
||
$newShopData['final_coat_finish_date3'] = null;
|
||
$newShopData['created_at'] = now();
|
||
$newShopData['updated_at'] = now();
|
||
|
||
db("paint_follow_ups")->insert($newShopData);
|
||
$paintFollowUpCreated++;
|
||
Log::debug("Created new SHOP Paint Follow Up with new painting cycle", [
|
||
'line' => $lineList->line_no,
|
||
'spool' => $weldLog->spool_number,
|
||
'painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
}
|
||
} else {
|
||
if (empty($weldLog->spool_number)) {
|
||
Log::debug("Paint Follow Up SHOP atlandı - spool number boş", [
|
||
'line_number' => $lineList->line_no,
|
||
'weld_log_id' => $weldLog->id ?? 'N/A'
|
||
]);
|
||
} else {
|
||
Log::debug("Paint Follow Up SHOP atlandı - shop joint bulunamadı", [
|
||
'line_number' => $lineList->line_no,
|
||
'spool_number' => $weldLog->spool_number,
|
||
'hasShopJoint' => $hasShopJoint ? 'true' : 'false'
|
||
]);
|
||
}
|
||
}
|
||
|
||
// Process FIELD entry - using joint number
|
||
Log::debug("Paint Follow Up FIELD joint kontrolü", [
|
||
'line_number' => $lineList->line_no,
|
||
'joint_number' => $weldLog->no_of_the_joint_as_per_as_built_survey ?? 'NULL',
|
||
'joint_empty' => empty($weldLog->no_of_the_joint_as_per_as_built_survey) ? 'true' : 'false'
|
||
]);
|
||
|
||
if (!empty($weldLog->no_of_the_joint_as_per_as_built_survey)) {
|
||
// Check if this is a repair joint
|
||
$jointNo = $weldLog->no_of_the_joint_as_per_as_built_survey;
|
||
$jointType = "Normal";
|
||
|
||
if(strpos($jointNo, "R") !== false) {
|
||
$jointType = "Repair";
|
||
}
|
||
|
||
$fieldData = $basePaintFollowUpData;
|
||
$fieldData['location'] = 'FIELD';
|
||
$fieldData['spool_no_joint_no'] = $jointNo;
|
||
$fieldData['surface_roughness'] = $paintMatrix->touch_up_of_damaged_parts ?? '';
|
||
|
||
// Add temperature and volume data for FIELD
|
||
if ($todayTempData) {
|
||
$fieldData['substrate_temprature'] = $todayTempData['temp_material_field'];
|
||
$fieldData['ambient_temprature'] = $todayTempData['field_ambient'];
|
||
}
|
||
$fieldData['volume_1'] = $fVolume;
|
||
$fieldData['volume_2'] = $fVolume;
|
||
$fieldData['volume_3'] = $fVolume;
|
||
$fieldData['total_volume'] = $fVolume * 3;
|
||
|
||
// Unique constraint condition for FIELD records (line + joint + cycle)
|
||
$fieldUniqueConstraintCondition = [
|
||
'line' => $lineList->line_no,
|
||
'spool_no_joint_no' => $jointNo,
|
||
'cycle' => $lineList->painting_cycle,
|
||
'location' => 'FIELD'
|
||
];
|
||
|
||
// Where condition for finding existing records (without cycle constraint)
|
||
$fieldWhereCondition = [
|
||
'line' => $lineList->line_no,
|
||
'spool_no_joint_no' => $jointNo,
|
||
'location' => 'FIELD'
|
||
];
|
||
|
||
// Find existing record with same painting cycle
|
||
$existingFieldRecord = db("paint_follow_ups")
|
||
->where($fieldUniqueConstraintCondition)
|
||
->first();
|
||
|
||
// Also check if there are any records with different painting cycle for same line/joint
|
||
$recordsWithDifferentPaintCycle = db("paint_follow_ups")
|
||
->where($fieldWhereCondition)
|
||
->where('cycle', '!=', $lineList->painting_cycle)
|
||
->get();
|
||
|
||
if ($existingFieldRecord) {
|
||
// Eğer bu kayıt daha önce işlendiyse güncelleme işlemini atla
|
||
if (in_array($existingFieldRecord->id, $processedPaintFollowUpIds)) {
|
||
Log::debug("Skipping duplicate update for paint follow up {$existingFieldRecord->id}");
|
||
continue;
|
||
}
|
||
|
||
// İşlenmiş ID'ler listesine ekle
|
||
$processedPaintFollowUpIds[] = $existingFieldRecord->id;
|
||
|
||
// Check if ALL date fields are empty (no painting activity started)
|
||
$hasAllEmptyDates = empty($existingFieldRecord->primer_coating_start_date) &&
|
||
empty($existingFieldRecord->primer_coating_finish_date) &&
|
||
empty($existingFieldRecord->start_intermediate_date2) &&
|
||
empty($existingFieldRecord->finish_intermediate_date2) &&
|
||
empty($existingFieldRecord->final_coat_start_date3) &&
|
||
empty($existingFieldRecord->final_coat_finish_date3);
|
||
|
||
if ($hasAllEmptyDates) {
|
||
// Update existing record only if ALL dates are empty
|
||
db("paint_follow_ups")
|
||
->where('id', $existingFieldRecord->id)
|
||
->update($fieldData);
|
||
$paintFollowUpUpdated++;
|
||
Log::debug("Updated FIELD paint follow up {$existingFieldRecord->id} with same painting cycle - all dates were empty");
|
||
} else {
|
||
// Even if dates are filled, update temperature and volume fields if they are empty
|
||
$tempVolumeUpdateData = [];
|
||
|
||
// Add temperature data if available and field is empty
|
||
if ($todayTempData) {
|
||
if (empty($existingFieldRecord->substrate_temprature)) {
|
||
$tempVolumeUpdateData['substrate_temprature'] = $todayTempData['temp_material_field'];
|
||
}
|
||
if (empty($existingFieldRecord->ambient_temprature)) {
|
||
$tempVolumeUpdateData['ambient_temprature'] = $todayTempData['field_ambient'];
|
||
}
|
||
}
|
||
|
||
// Add volume data if fields are empty
|
||
if (empty($existingFieldRecord->volume_1)) {
|
||
$tempVolumeUpdateData['volume_1'] = $fVolume;
|
||
}
|
||
if (empty($existingFieldRecord->volume_2)) {
|
||
$tempVolumeUpdateData['volume_2'] = $fVolume;
|
||
}
|
||
if (empty($existingFieldRecord->volume_3)) {
|
||
$tempVolumeUpdateData['volume_3'] = $fVolume;
|
||
}
|
||
if (empty($existingFieldRecord->total_volume)) {
|
||
$tempVolumeUpdateData['total_volume'] = $fVolume * 3;
|
||
}
|
||
|
||
if (!empty($tempVolumeUpdateData)) {
|
||
db("paint_follow_ups")
|
||
->where('id', $existingFieldRecord->id)
|
||
->update($tempVolumeUpdateData);
|
||
Log::debug("Updated temperature/volume fields for FIELD record ID {$existingFieldRecord->id}", [
|
||
'updated_fields' => array_keys($tempVolumeUpdateData)
|
||
]);
|
||
} else {
|
||
Log::debug("Skipping update for FIELD record ID {$existingFieldRecord->id} - some dates are filled and no empty temp/volume fields");
|
||
}
|
||
}
|
||
} else {
|
||
// No record with this painting cycle exists
|
||
|
||
// Check if we need to handle records with different painting cycles
|
||
if ($recordsWithDifferentPaintCycle->count() > 0) {
|
||
Log::debug("Found FIELD records with different painting cycles for same joint", [
|
||
'line' => $lineList->line_no,
|
||
'joint' => $jointNo,
|
||
'current_painting_cycle' => $lineList->painting_cycle,
|
||
'existing_records_count' => $recordsWithDifferentPaintCycle->count()
|
||
]);
|
||
|
||
// Handle each record with different painting cycle
|
||
foreach ($recordsWithDifferentPaintCycle as $oldRecord) {
|
||
// Check if any date field is filled (any painting activity started)
|
||
$hasAnyDateFilled = !empty($oldRecord->primer_coating_start_date) ||
|
||
!empty($oldRecord->primer_coating_finish_date) ||
|
||
!empty($oldRecord->start_intermediate_date2) ||
|
||
!empty($oldRecord->finish_intermediate_date2) ||
|
||
!empty($oldRecord->final_coat_start_date3) ||
|
||
!empty($oldRecord->final_coat_finish_date3);
|
||
|
||
if ($hasAnyDateFilled) {
|
||
// Mark old record status as HOLD (keep existing record but mark as inactive)
|
||
db("paint_follow_ups")
|
||
->where('id', $oldRecord->id)
|
||
->update([
|
||
'status' => 'HOLD',
|
||
'updated_at' => now()
|
||
]);
|
||
|
||
Log::debug("Marked old FIELD Paint Follow Up as HOLD due to painting cycle change", [
|
||
'record_id' => $oldRecord->id,
|
||
'old_painting_cycle' => $oldRecord->cycle,
|
||
'new_painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
} else {
|
||
// Update old record to new painting cycle if no dates filled
|
||
db("paint_follow_ups")
|
||
->where('id', $oldRecord->id)
|
||
->update([
|
||
'cycle' => $lineList->painting_cycle,
|
||
'updated_at' => now()
|
||
]);
|
||
|
||
$paintFollowUpUpdated++;
|
||
Log::debug("Updated old FIELD Paint Follow Up to new painting cycle", [
|
||
'record_id' => $oldRecord->id,
|
||
'old_painting_cycle' => $oldRecord->cycle,
|
||
'new_painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
|
||
// Skip creating new record since we updated existing one
|
||
continue 2;
|
||
}
|
||
}
|
||
}
|
||
|
||
// Create new record with new painting cycle
|
||
$newFieldData = $fieldData;
|
||
$newFieldData['primer_coating_start_date'] = null;
|
||
$newFieldData['primer_coating_finish_date'] = null;
|
||
$newFieldData['start_intermediate_date2'] = null;
|
||
$newFieldData['finish_intermediate_date2'] = null;
|
||
$newFieldData['final_coat_start_date3'] = null;
|
||
$newFieldData['final_coat_finish_date3'] = null;
|
||
$newFieldData['created_at'] = now();
|
||
$newFieldData['updated_at'] = now();
|
||
|
||
db("paint_follow_ups")->insert($newFieldData);
|
||
$paintFollowUpCreated++;
|
||
Log::debug("Created new FIELD Paint Follow Up with new painting cycle", [
|
||
'line' => $lineList->line_no,
|
||
'joint' => $jointNo,
|
||
'painting_cycle' => $lineList->painting_cycle
|
||
]);
|
||
}
|
||
} else {
|
||
Log::debug("Paint Follow Up FIELD atlandı - joint number boş", [
|
||
'line_number' => $lineList->line_no,
|
||
'weld_log_id' => $weldLog->id ?? 'N/A',
|
||
'joint_number' => $weldLog->no_of_the_joint_as_per_as_built_survey ?? 'NULL'
|
||
]);
|
||
}
|
||
}
|
||
|
||
|
||
Log::debug("Paint Follow Ups senkronizasyonu başarıyla tamamlandı.");
|
||
Log::debug("Oluşturulan: $paintFollowUpCreated, Güncellenen: $paintFollowUpUpdated kayıt.");
|
||
} else {
|
||
Log::debug("ID: $id için line list kaydı bulunamadı");
|
||
}
|
||
|
||
DB::commit();
|
||
} catch (\Exception $e) {
|
||
DB::rollBack();
|
||
Log::error("Paint Follow Ups senkronizasyonu hatası: " . $e->getMessage());
|
||
throw $e;
|
||
}
|
||
|
||
// Run spool status changer after line list sync
|
||
$line_number = $lineList->line_no ?? null;
|
||
echo view('cron.spool-status-changer')->render();
|
||
|
||
|
||
// Ana transaction'ı kaldırdık, her işlem grubu kendi transaction'ını yönetiyor
|
||
Log::debug("=== LINE LIST SYNC BAŞARIYLA TAMAMLANDI ===", [
|
||
'id' => $id,
|
||
'line_number' => $lineListNumber,
|
||
'timestamp' => now()
|
||
]);
|
||
|
||
|
||
// Clean-up işlemini ayrı transaction olarak yap
|
||
try {
|
||
echo view('cron.delete-no-paint-followup', [
|
||
'lineNumber' => $lineList->line_no
|
||
])->render();
|
||
} catch (\Exception $cleanupException) {
|
||
Log::warning("Clean-up işlemi başarısız oldu: " . $cleanupException->getMessage(), [
|
||
'line_number' => $lineList->line_no
|
||
]);
|
||
}
|
||
} catch (\Exception $e) {
|
||
// Ana transaction olmadığı için rollback'e gerek yok
|
||
Log::error("Line list synchronization failed: " . $e->getMessage());
|
||
throw $e;
|
||
}
|
||
|
||
|
||
?>
|