349 lines
16 KiB
PHP
349 lines
16 KiB
PHP
<?php
|
||
use Illuminate\Support\Facades\Log;
|
||
use Carbon\Carbon;
|
||
|
||
$id = $request['key'];
|
||
|
||
Log::debug("=== PAINT FOLLOW UPS SYNC BAŞLATILIYOR ===", [
|
||
'id' => $id,
|
||
'timestamp' => now()
|
||
]);
|
||
|
||
try {
|
||
// Get paint follow up data for the current ID
|
||
$paintFollowUp = DB::table('paint_follow_ups')->where(is_array($id) ? $id : ['id' => $id])->first();
|
||
|
||
if ($paintFollowUp) {
|
||
Log::debug("Paint Follow Up bulundu", [
|
||
'id' => $id,
|
||
'line' => $paintFollowUp->line ?? 'NULL',
|
||
'spool_no_joint_no' => $paintFollowUp->spool_no_joint_no ?? 'NULL',
|
||
'iso_number' => $paintFollowUp->iso_number ?? 'NULL'
|
||
]);
|
||
|
||
// Comprehensive field mapping from paint_follow_ups to construction_paint_logs
|
||
// Based on line_lists.php comprehensive sync structure
|
||
$fieldMappings = [
|
||
// Date fields
|
||
'protocol_date_cleaning' => 'blasting_date',
|
||
'surface_preparation_rfi_no' => 'blasting_rfi_no',
|
||
'primer_coating_start_date' => 'painting_date_1',
|
||
'primer_coating_finish_date' => 'painting_finish_date_1',
|
||
'primer_coating_rfi_no' => 'rfi_no_1',
|
||
'primer_coating_rfi_date_1' => 'rfi_date_1',
|
||
'start_intermediate_date2' => 'painting_date_2',
|
||
'finish_intermediate_date2' => 'painting_finish_date_2',
|
||
'intermediate_coating_rfi_no2' => 'rfi_no_2',
|
||
'intermediate_coating_rfi_date_3' => 'rfi_date_2',
|
||
'final_coat_start_date3' => 'painting_date_3',
|
||
'final_coat_finish_date3' => 'painting_finish_date_3',
|
||
'final_coating_rfi_no3' => 'rfi_no_3',
|
||
'final_coating_rfi_date_3' => 'rfi_date_3',
|
||
|
||
// Thickness fields
|
||
'primer_measured_thickness_1' => 'thickness_1',
|
||
'intermediate_measured_thickness_2' => 'thickness_2',
|
||
'final_coating_measured_thickness_3' => 'thickness_3',
|
||
|
||
// Brand and coating information
|
||
'brend_name_1' => 'brend_name_1',
|
||
'brend_name_2' => 'brend_name_2',
|
||
'brend_name_3' => 'brend_name_3',
|
||
'ral_code_1' => 'ral_1',
|
||
'ral_code_2' => 'ral_2',
|
||
'ral_code_3' => 'ral_3',
|
||
|
||
// Additional fields (only existing columns in construction_paint_logs)
|
||
'fluid_code' => 'fluid_code',
|
||
'fluid_code_description' => 'fluid_code_description',
|
||
'line' => 'line',
|
||
'unit' => 'area',
|
||
'spool_no_joint_no' => 'spool',
|
||
'iso_number' => 'iso_drawings'
|
||
];
|
||
|
||
// Prepare update data for construction_paint_logs
|
||
$updateData = [];
|
||
|
||
// Add fields from paint_follow_ups to updateData
|
||
foreach ($fieldMappings as $sourceField => $targetField) {
|
||
if (isset($paintFollowUp->$sourceField) && $paintFollowUp->$sourceField !== null) {
|
||
$updateData[$targetField] = $paintFollowUp->$sourceField;
|
||
}
|
||
}
|
||
|
||
Log::debug("Field mapping tamamlandı", [
|
||
'updateData' => $updateData,
|
||
'fieldMappings_count' => count($fieldMappings)
|
||
]);
|
||
|
||
// Add additional fields from line_lists.php comprehensive sync
|
||
// These fields are available in construction_paint_logs table
|
||
$additionalFields = [
|
||
'painting_system_type_1' => $paintFollowUp->cycle ?? '',
|
||
'painting_system_type_2' => $paintFollowUp->cycle ?? '',
|
||
/*
|
||
'rev' => '0', // Default revision
|
||
'isolation_info' => 'WEQE', // Default isolation info
|
||
'engineering' => 'DANIELI', // Default engineering
|
||
*/
|
||
'unit' => $paintFollowUp->area ?? '',
|
||
'test_package' => '',
|
||
'spool_status' => 'Waiting',
|
||
'construction_report_no' => '',
|
||
'dn_1' => '0',
|
||
'dn_2' => '0',
|
||
'dn_3' => '0',
|
||
'total_layer' => (float)($paintFollowUp->primer_measured_thickness_1 ?? 0) +
|
||
(float)($paintFollowUp->intermediate_measured_thickness_2 ?? 0) +
|
||
(float)($paintFollowUp->final_coating_measured_thickness_3 ?? 0)
|
||
];
|
||
|
||
// Merge additional fields into updateData
|
||
foreach ($additionalFields as $key => $value) {
|
||
if (!empty($value) || $value === '0' || $value === 0) {
|
||
$updateData[$key] = $value;
|
||
}
|
||
}
|
||
|
||
// Load temperature settings for volume and temperature calculations
|
||
$temperatures = j(setting("temperatures"));
|
||
$todayTempData = null;
|
||
|
||
// Get protocol_date_cleaning from paint_follow_ups to determine temperature data
|
||
// Only proceed if protocol_date_cleaning is not null, not empty, and not blank
|
||
if (!is_null($temperatures) &&
|
||
!empty($paintFollowUp->protocol_date_cleaning) &&
|
||
trim($paintFollowUp->protocol_date_cleaning) !== '') {
|
||
|
||
$protocolDate = Carbon::parse($paintFollowUp->protocol_date_cleaning);
|
||
$dayOfYear = $protocolDate->format('z');
|
||
$todayTempData = $temperatures[$dayOfYear] ?? null;
|
||
|
||
Log::debug('Temperature data loaded based on protocol_date_cleaning:', [
|
||
'protocol_date_cleaning' => $paintFollowUp->protocol_date_cleaning,
|
||
'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 protocol_date_cleaning found, empty or blank - temperature updates will be skipped', [
|
||
'protocol_date_cleaning_value' => $paintFollowUp->protocol_date_cleaning ?? 'NULL'
|
||
]);
|
||
}
|
||
}
|
||
|
||
// Only proceed if we have data to update
|
||
if (!empty($updateData)) {
|
||
// Set updated_at timestamp
|
||
$updateData['updated_at'] = now();
|
||
|
||
// Find matching records in construction_paint_logs by line and spool
|
||
$whereConditions = [
|
||
['iso_drawings', $paintFollowUp->iso_number],
|
||
['spool', $paintFollowUp->spool_no_joint_no]
|
||
];
|
||
|
||
// Also try to match by line if iso_number is empty
|
||
if (empty($paintFollowUp->iso_number) && !empty($paintFollowUp->line)) {
|
||
$whereConditions = [
|
||
'iso_drawings' => $paintFollowUp->iso_number,
|
||
'spool' => $paintFollowUp->spool_no_joint_no,
|
||
'painting_system_type_1' => $paintFollowUp->cycle
|
||
];
|
||
}
|
||
|
||
$affectedRows = DB::table('construction_paint_logs')
|
||
->where($whereConditions)
|
||
->update($updateData);
|
||
|
||
Log::debug("Construction Paint Logs güncellendi", [
|
||
'whereConditions' => $whereConditions,
|
||
'affectedRows' => $affectedRows,
|
||
'updateData' => $updateData
|
||
]);
|
||
|
||
// Update temperature fields in current paint_follow_ups record if todayTempData is available
|
||
if ($todayTempData) {
|
||
$tempUpdateDataPaintFollowUp = [];
|
||
|
||
// Add temperature data based on location (SHOP or FIELD)
|
||
if ($paintFollowUp->location === 'SHOP') {
|
||
if (empty($paintFollowUp->substrate_temprature)) {
|
||
$tempUpdateDataPaintFollowUp['substrate_temprature'] = $todayTempData['temp_material_shop'];
|
||
}
|
||
if (empty($paintFollowUp->ambient_temprature)) {
|
||
$tempUpdateDataPaintFollowUp['ambient_temprature'] = $todayTempData['shop_ambient'];
|
||
}
|
||
} elseif ($paintFollowUp->location === 'FIELD') {
|
||
if (empty($paintFollowUp->substrate_temprature)) {
|
||
$tempUpdateDataPaintFollowUp['substrate_temprature'] = $todayTempData['temp_material_field'];
|
||
}
|
||
if (empty($paintFollowUp->ambient_temprature)) {
|
||
$tempUpdateDataPaintFollowUp['ambient_temprature'] = $todayTempData['field_ambient'];
|
||
}
|
||
}
|
||
|
||
if (!empty($tempUpdateDataPaintFollowUp)) {
|
||
$tempUpdateDataPaintFollowUp['updated_at'] = now();
|
||
DB::table('paint_follow_ups')
|
||
->where('id', $id)
|
||
->update($tempUpdateDataPaintFollowUp);
|
||
|
||
Log::debug("Updated temperature fields for current paint follow up record", [
|
||
'record_id' => $id,
|
||
'location' => $paintFollowUp->location,
|
||
'updated_fields' => array_keys($tempUpdateDataPaintFollowUp)
|
||
]);
|
||
}
|
||
}
|
||
|
||
// Sync thickness values from construction_paint_logs back to paint_follow_ups
|
||
$constructionPaintLog = DB::table('construction_paint_logs')
|
||
->where($whereConditions)
|
||
->first();
|
||
|
||
if ($constructionPaintLog) {
|
||
Log::debug("Construction Paint Log bulundu, kalınlık değerleri senkronize ediliyor", [
|
||
'constructionPaintLog' => (array)$constructionPaintLog
|
||
]);
|
||
|
||
// Update thickness values from construction_paint_logs
|
||
$thicknessUpdateData = [
|
||
'primer_measured_thickness_1' => $constructionPaintLog->thickness_1,
|
||
'intermediate_measured_thickness_2' => $constructionPaintLog->thickness_2,
|
||
'final_coating_measured_thickness_3' => $constructionPaintLog->thickness_3,
|
||
'total_thickness' => (float)($constructionPaintLog->thickness_1 ?? 0) +
|
||
(float)($constructionPaintLog->thickness_2 ?? 0) +
|
||
(float)($constructionPaintLog->thickness_3 ?? 0),
|
||
'updated_at' => now()
|
||
];
|
||
|
||
$thicknessAffected = DB::table('paint_follow_ups')
|
||
->where('id', $id)
|
||
->update($thicknessUpdateData);
|
||
|
||
Log::debug("Paint Follow Ups kalınlık güncellemesi", [
|
||
'id' => $id,
|
||
'thicknessUpdateData' => $thicknessUpdateData,
|
||
'affectedRows' => $thicknessAffected
|
||
]);
|
||
|
||
// Note: Temperature fields are only updated in paint_follow_ups table
|
||
// construction_paint_logs table does not have temperature columns
|
||
} else {
|
||
Log::debug("Construction Paint Log bulunamadı", [
|
||
'whereConditions' => $whereConditions
|
||
]);
|
||
}
|
||
|
||
// Sync with Incoming Control Paints for each brand
|
||
$brands = ['brend_name_1', 'brend_name_2', 'brend_name_3'];
|
||
foreach ($brands as $index => $brandField) {
|
||
if (!empty($paintFollowUp->$brandField)) {
|
||
Log::debug("Marka için incoming_control_paints aranıyor", [
|
||
'brandField' => $brandField,
|
||
'brandValue' => $paintFollowUp->$brandField
|
||
]);
|
||
|
||
// Get the latest incoming control paint record for this brand
|
||
$incomingControl = DB::table('incoming_control_paints')
|
||
->where('brend_name', $paintFollowUp->$brandField)
|
||
->orderBy('rfi_date', 'desc')
|
||
->first();
|
||
|
||
if ($incomingControl) {
|
||
Log::debug("Incoming control paint bulundu", [
|
||
'incomingControl' => (array)$incomingControl
|
||
]);
|
||
|
||
$suffix = $index + 1;
|
||
$incomingUpdateData = [
|
||
'incoming_control_akt_no_' . $suffix => $incomingControl->akt_number,
|
||
'incoming_control_rfi_no_' . $suffix => $incomingControl->rfi_no,
|
||
'akt_date_' . $suffix => $incomingControl->akt_date,
|
||
'standartgost_iso_en_' . $suffix => $incomingControl->manufacturing_standard,
|
||
'certificate_passport_no_' . $suffix => $incomingControl->certificate_no,
|
||
'certificate_passport_date_' . $suffix => $incomingControl->certificate_date,
|
||
'updated_at' => now()
|
||
];
|
||
|
||
$incomingAffected = DB::table('paint_follow_ups')
|
||
->where('id', $id)
|
||
->update($incomingUpdateData);
|
||
|
||
Log::debug("Paint Follow Ups incoming control güncellemesi", [
|
||
'id' => $id,
|
||
'incomingUpdateData' => $incomingUpdateData,
|
||
'affectedRows' => $incomingAffected
|
||
]);
|
||
|
||
// Update construction_paint_logs with incoming control data
|
||
if ($affectedRows > 0) {
|
||
/*
|
||
$constructionIncomingUpdateData = [
|
||
|
||
'rfi_no_' . $suffix => $incomingControl->rfi_no,
|
||
|
||
'updated_at' => now()
|
||
];
|
||
|
||
// Update construction_paint_logs with incoming control data
|
||
$constructionIncomingAffected = DB::table('construction_paint_logs')
|
||
->where($whereConditions)
|
||
->update($constructionIncomingUpdateData);
|
||
|
||
Log::debug("Construction Paint Logs incoming control güncellendi", [
|
||
'whereConditions' => $whereConditions,
|
||
'constructionIncomingUpdateData' => $constructionIncomingUpdateData,
|
||
'affectedRows' => $constructionIncomingAffected
|
||
]);
|
||
*/
|
||
}
|
||
} else {
|
||
Log::debug("Incoming control paint bulunamadı", [
|
||
'brandField' => $brandField,
|
||
'brandValue' => $paintFollowUp->$brandField
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
|
||
Log::debug("Paint Follow Ups to Construction Paint Logs sync tamamlandı", [
|
||
'paintFollowUpId' => $id,
|
||
'affectedRows' => $affectedRows
|
||
]);
|
||
} else {
|
||
Log::debug("Senkronize edilecek alan yok", [
|
||
'paintFollowUpId' => $id
|
||
]);
|
||
}
|
||
} else {
|
||
Log::debug("Paint Follow Up bulunamadı", [
|
||
'id' => $id
|
||
]);
|
||
}
|
||
|
||
// Run spool status changer after paint follow ups sync
|
||
$spool_number = $paintFollowUp->spool_no_joint_no ?? null;
|
||
$line_number = $paintFollowUp->line ?? null;
|
||
spoolStatusChanger($line_number, $spool_number);
|
||
|
||
|
||
Log::debug("=== PAINT FOLLOW UPS SYNC BAŞARIYLA TAMAMLANDI ===", [
|
||
'id' => $id,
|
||
'timestamp' => now()
|
||
]);
|
||
|
||
} catch (\Exception $e) {
|
||
Log::error("Paint Follow Ups synchronization failed: " . $e->getMessage(), [
|
||
'id' => $id,
|
||
'error' => $e->getMessage(),
|
||
'trace' => $e->getTraceAsString()
|
||
]);
|
||
throw $e;
|
||
}
|
||
|