288 lines
13 KiB
PHP
288 lines
13 KiB
PHP
<?php
|
||
use Illuminate\Support\Facades\Log;
|
||
use Carbon\Carbon;
|
||
|
||
$id = $request['key'];
|
||
|
||
Log::debug("=== CONSTRUCTION PAINT LOGS SYNC BAŞLATILIYOR ===", [
|
||
'id' => $id,
|
||
'timestamp' => now()
|
||
]);
|
||
|
||
try {
|
||
// Get paint system data for the current ID
|
||
$constructionPaintLog = DB::table('construction_paint_logs')->where(is_array($id) ? $id : ['id' => $id])->first();
|
||
|
||
if ($constructionPaintLog) {
|
||
Log::debug("Construction Paint Log bulundu", [
|
||
'id' => $id,
|
||
'line' => $constructionPaintLog->line ?? 'NULL',
|
||
'spool' => $constructionPaintLog->spool ?? 'NULL',
|
||
'iso_drawings' => $constructionPaintLog->iso_drawings ?? 'NULL'
|
||
]);
|
||
|
||
// Comprehensive field mapping from construction_paint_logs to paint_follow_ups
|
||
// Based on line_lists.php comprehensive sync structure
|
||
$fieldMappings = [
|
||
// Date fields
|
||
'blasting_date' => 'protocol_date_cleaning',
|
||
'blasting_rfi_no' => 'surface_preparation_rfi_no',
|
||
'painting_date_1' => 'primer_coating_start_date',
|
||
'painting_finish_date_1' => 'primer_coating_finish_date',
|
||
'rfi_date_1' => 'primer_coating_rfi_date_1',
|
||
'rfi_no_1' => 'primer_coating_rfi_no',
|
||
'painting_date_2' => 'start_intermediate_date2',
|
||
'painting_finish_date_2' => 'finish_intermediate_date2',
|
||
'rfi_date_2' => 'intermediate_coating_rfi_date_3',
|
||
'rfi_no_2' => 'intermediate_coating_rfi_no2',
|
||
'painting_date_3' => 'final_coat_start_date3',
|
||
'painting_finish_date_3' => 'final_coat_finish_date3',
|
||
'rfi_date_3' => 'final_coating_rfi_date_3',
|
||
'rfi_no_3' => 'final_coating_rfi_no3',
|
||
|
||
// Thickness fields
|
||
'thickness_1' => 'primer_measured_thickness_1',
|
||
'thickness_2' => 'intermediate_measured_thickness_2',
|
||
'thickness_3' => 'final_coating_measured_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_1' => 'ral_code_1',
|
||
'ral_2' => 'ral_code_2',
|
||
'ral_3' => 'ral_code_3',
|
||
|
||
// Additional fields (only existing columns in paint_follow_ups)
|
||
'fluid_code' => 'fluid_code',
|
||
'fluid_code_description' => 'fluid_code_description',
|
||
'line' => 'line',
|
||
'area' => 'project',
|
||
'spool' => 'spool_no_joint_no',
|
||
'iso_drawings' => 'iso_number'
|
||
];
|
||
|
||
// Prepare update data for paint_follow_ups
|
||
$updateData = [];
|
||
|
||
// Add fields from construction_paint_logs to updateData
|
||
foreach ($fieldMappings as $sourceField => $targetField) {
|
||
if (isset($constructionPaintLog->$sourceField) && $constructionPaintLog->$sourceField !== null) {
|
||
$updateData[$targetField] = $constructionPaintLog->$sourceField;
|
||
}
|
||
}
|
||
|
||
// Calculate total thickness
|
||
$updateData['total_thickness'] = (float)($constructionPaintLog->thickness_1 ?? 0) +
|
||
(float)($constructionPaintLog->thickness_2 ?? 0) +
|
||
(float)($constructionPaintLog->thickness_3 ?? 0);
|
||
|
||
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 paint_follow_ups table
|
||
$additionalFields = [
|
||
'project' => $constructionPaintLog->area ?? '',
|
||
'description' => 'PIPE',
|
||
'location' => 'SHOP', // Default location for construction paint logs
|
||
'cycle' => $constructionPaintLog->painting_system_type_1 ?? '',
|
||
'status' => 'In Progress'
|
||
];
|
||
|
||
// Merge additional fields into updateData
|
||
foreach ($additionalFields as $key => $value) {
|
||
if (!empty($value)) {
|
||
$updateData[$key] = $value;
|
||
}
|
||
}
|
||
|
||
// 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
|
||
// Only proceed if blasting_date is not null, not empty, and not blank
|
||
if (!is_null($temperatures) &&
|
||
!empty($constructionPaintLog->blasting_date) &&
|
||
trim($constructionPaintLog->blasting_date) !== '') {
|
||
|
||
$blastingDate = Carbon::parse($constructionPaintLog->blasting_date);
|
||
$dayOfYear = $blastingDate->format('z');
|
||
$todayTempData = $temperatures[$dayOfYear] ?? null;
|
||
|
||
Log::debug('Temperature data loaded based on blasting_date:', [
|
||
'blasting_date' => $constructionPaintLog->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, empty or blank - temperature updates will be skipped', [
|
||
'blasting_date_value' => $constructionPaintLog->blasting_date ?? 'NULL'
|
||
]);
|
||
}
|
||
}
|
||
|
||
// Only proceed if we have data to update
|
||
if (!empty($updateData)) {
|
||
// Set updated_at timestamp
|
||
$updateData['updated_at'] = now();
|
||
|
||
// Find matching records in paint_follow_ups by line and spool
|
||
$whereConditions = [
|
||
['iso_number', $constructionPaintLog->iso_drawings],
|
||
['spool_no_joint_no', $constructionPaintLog->spool]
|
||
];
|
||
|
||
// Also try to match by line if iso_drawings is empty
|
||
if (empty($constructionPaintLog->iso_drawings) && !empty($constructionPaintLog->line)) {
|
||
$whereConditions = [
|
||
['line', $constructionPaintLog->line],
|
||
['spool_no_joint_no', $constructionPaintLog->spool]
|
||
];
|
||
}
|
||
|
||
// Update paint_follow_ups with construction paint log data
|
||
$affectedRows = DB::table('paint_follow_ups')
|
||
->where($whereConditions)
|
||
->update($updateData);
|
||
|
||
Log::debug("Paint Follow Ups güncellendi", [
|
||
'whereConditions' => $whereConditions,
|
||
'affectedRows' => $affectedRows,
|
||
'updateData' => $updateData
|
||
]);
|
||
|
||
// Update temperature fields if todayTempData is available and fields are empty
|
||
if ($todayTempData && $affectedRows > 0) {
|
||
// Get existing records to check if temperature fields are empty
|
||
$existingRecords = DB::table('paint_follow_ups')
|
||
->where($whereConditions)
|
||
->get();
|
||
|
||
foreach ($existingRecords as $existingRecord) {
|
||
$tempUpdateData = [];
|
||
|
||
// Add temperature data based on location (SHOP or FIELD)
|
||
if ($existingRecord->location === 'SHOP') {
|
||
if (empty($existingRecord->substrate_temprature)) {
|
||
$tempUpdateData['substrate_temprature'] = $todayTempData['temp_material_shop'];
|
||
}
|
||
if (empty($existingRecord->ambient_temprature)) {
|
||
$tempUpdateData['ambient_temprature'] = $todayTempData['shop_ambient'];
|
||
}
|
||
} elseif ($existingRecord->location === 'FIELD') {
|
||
if (empty($existingRecord->substrate_temprature)) {
|
||
$tempUpdateData['substrate_temprature'] = $todayTempData['temp_material_field'];
|
||
}
|
||
if (empty($existingRecord->ambient_temprature)) {
|
||
$tempUpdateData['ambient_temprature'] = $todayTempData['field_ambient'];
|
||
}
|
||
}
|
||
|
||
if (!empty($tempUpdateData)) {
|
||
$tempUpdateData['updated_at'] = now();
|
||
DB::table('paint_follow_ups')
|
||
->where('id', $existingRecord->id)
|
||
->update($tempUpdateData);
|
||
|
||
Log::debug("Updated temperature fields for paint follow up record", [
|
||
'record_id' => $existingRecord->id,
|
||
'location' => $existingRecord->location,
|
||
'updated_fields' => array_keys($tempUpdateData)
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 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($constructionPaintLog->$brandField)) {
|
||
Log::debug("Marka için incoming_control_paints aranıyor", [
|
||
'brandField' => $brandField,
|
||
'brandValue' => $constructionPaintLog->$brandField
|
||
]);
|
||
|
||
// Get the latest incoming control paint record for this brand
|
||
$incomingControl = DB::table('incoming_control_paints')
|
||
->where('brend_name', $constructionPaintLog->$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()
|
||
];
|
||
|
||
// Update paint_follow_ups with incoming control data
|
||
$incomingAffectedRows = DB::table('paint_follow_ups')
|
||
->where($whereConditions)
|
||
->update($incomingUpdateData);
|
||
|
||
Log::debug("Paint Follow Ups incoming control güncellendi", [
|
||
'whereConditions' => $whereConditions,
|
||
'incomingUpdateData' => $incomingUpdateData,
|
||
'affectedRows' => $incomingAffectedRows
|
||
]);
|
||
} else {
|
||
Log::debug("Incoming control paint bulunamadı", [
|
||
'brandField' => $brandField,
|
||
'brandValue' => $constructionPaintLog->$brandField
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
|
||
Log::debug("Construction Paint Logs to Paint Follow Ups sync tamamlandı", [
|
||
'constructionPaintLogId' => $id,
|
||
'affectedRows' => $affectedRows
|
||
]);
|
||
} else {
|
||
Log::debug("Senkronize edilecek alan yok", [
|
||
'constructionPaintLogId' => $id
|
||
]);
|
||
}
|
||
} else {
|
||
Log::debug("Construction Paint Log bulunamadı", [
|
||
'id' => $id
|
||
]);
|
||
}
|
||
|
||
// Run spool status changer after construction paint logs sync
|
||
$spool_number = $constructionPaintLog->spool ?? null;
|
||
$line_number = $constructionPaintLog->line ?? null;
|
||
echo view('cron.spool-status-changer', ['line_number' => $line_number, 'spool_number' => $spool_number])->render();
|
||
|
||
Log::debug("=== CONSTRUCTION PAINT LOGS SYNC BAŞARIYLA TAMAMLANDI ===", [
|
||
'id' => $id,
|
||
'timestamp' => now()
|
||
]);
|
||
|
||
} catch (\Exception $e) {
|
||
Log::error("Construction Paint Logs synchronization failed: " . $e->getMessage(), [
|
||
'id' => $id,
|
||
'error' => $e->getMessage(),
|
||
'trace' => $e->getTraceAsString()
|
||
]);
|
||
throw $e;
|
||
}
|
||
|