380 lines
16 KiB
PHP
380 lines
16 KiB
PHP
<?php
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
$id = $request['key'];
|
||
|
||
// Get paint system data for the current ID
|
||
$paintSystem = DB::table('paint_systems')->where(is_array($id) ? $id : ['id' => $id])->first();
|
||
|
||
if (!$paintSystem) {
|
||
Log::debug("Paint system record not found", ['id' => $id]);
|
||
return;
|
||
}
|
||
|
||
Log::debug("Starting synchronization from Paint Systems", [
|
||
'paint_cycle' => $paintSystem->paint_cycle
|
||
]);
|
||
|
||
DB::beginTransaction();
|
||
try {
|
||
// Skip processing if essential fields are missing
|
||
if (empty($paintSystem->paint_cycle)) {
|
||
Log::debug("Skipping synchronization - missing paint_cycle");
|
||
DB::commit();
|
||
return;
|
||
}
|
||
|
||
// ===== SYNC TO CONSTRUCTION_PAINT_LOGS =====
|
||
Log::debug("Syncing to Construction Paint Logs");
|
||
|
||
// Field mappings from paint_systems to construction_paint_logs
|
||
// Only including relevant fields based on construction_paint_logs table structure
|
||
$cpUpdateData = [
|
||
// Essential paint system fields
|
||
'rev' => $paintSystem->revision ?? '',
|
||
|
||
// First layer
|
||
'brend_name_1' => $paintSystem->brand_name_1 ?? $paintSystem->brend_name_1 ?? '',
|
||
'thickness_1' => $paintSystem->thickness_1 ?? '',
|
||
|
||
// Second layer
|
||
'brend_name_2' => $paintSystem->brand_name_2 ?? $paintSystem->brend_name_2 ?? '',
|
||
'thickness_2' => $paintSystem->thickness_2 ?? '',
|
||
|
||
// Third layer
|
||
'brend_name_3' => $paintSystem->brand_name_3 ?? $paintSystem->brend_name_3 ?? '',
|
||
'thickness_3' => $paintSystem->thickness_3 ?? '',
|
||
|
||
// Totals
|
||
'total_layer' => $paintSystem->total_kg ?? '',
|
||
|
||
// 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 ($cpUpdateData as $key => $value) {
|
||
if ($value === '' && $key !== 'painting_system_type_1') { // Keep the key field
|
||
unset($cpUpdateData[$key]);
|
||
}
|
||
}
|
||
|
||
// Log the fields being synced
|
||
if (count($cpUpdateData) > 2) { // >2 because 'updated_at' and 'created_at' are always included
|
||
Log::debug("Syncing fields to Construction Paint Logs", ['fields' => array_diff_key($cpUpdateData, array_flip(['updated_at', 'created_at']))]);
|
||
} else {
|
||
Log::debug("No relevant data found to sync to Construction Paint Logs. Only updating timestamp.");
|
||
}
|
||
|
||
// Count existing records for logging purposes
|
||
$existingCount1 = DB::table('construction_paint_logs')
|
||
->where('painting_system_type_1', $paintSystem->paint_cycle)
|
||
->count();
|
||
|
||
$existingCount2 = DB::table('construction_paint_logs')
|
||
->where('painting_system_type_2', $paintSystem->paint_cycle)
|
||
->count();
|
||
|
||
// Get all existing records to check which ones to update vs create
|
||
$existingRecords = DB::table('construction_paint_logs')
|
||
->where('painting_system_type_1', $paintSystem->paint_cycle)
|
||
->orWhere('painting_system_type_2', $paintSystem->paint_cycle)
|
||
->get();
|
||
|
||
$cpUpdatedCount = 0;
|
||
$cpCreatedCount = 0;
|
||
|
||
// Construction Paint Logs işlemlerini chunk'lara böl
|
||
$existingRecordsChunked = $existingRecords->chunk(10); // 10'lu gruplar
|
||
|
||
foreach($existingRecordsChunked as $recordsChunk) {
|
||
// Her chunk için ayrı transaction
|
||
DB::transaction(function () use ($recordsChunk, $paintSystem, $cpUpdateData, &$cpUpdatedCount, &$cpCreatedCount) {
|
||
if ($recordsChunk->isNotEmpty()) {
|
||
// Update existing records
|
||
foreach ($recordsChunk as $record) {
|
||
// Create a copy of updateData to avoid modifying the original
|
||
$recordUpdateData = $cpUpdateData;
|
||
|
||
// For existing records, don't modify painting_system_type fields to avoid breaking relations
|
||
if ($record->painting_system_type_1 === $paintSystem->paint_cycle) {
|
||
unset($recordUpdateData['painting_system_type_2']); // Don't change type_2 if type_1 matches
|
||
} else {
|
||
unset($recordUpdateData['painting_system_type_1']); // Don't change type_1 if type_2 matches
|
||
}
|
||
|
||
DB::table('construction_paint_logs')
|
||
->where('id', $record->id)
|
||
->whereNull('painting_date_1')
|
||
->update($recordUpdateData);
|
||
|
||
$cpUpdatedCount++;
|
||
}
|
||
|
||
Log::debug("Updated existing Construction Paint Logs records", ['count' => $cpUpdatedCount]);
|
||
} else {
|
||
Log::debug("No existing Construction Paint Logs records found", ['paint_cycle' => $paintSystem->paint_cycle]);
|
||
}
|
||
}, 3); // 3 deneme ile retry
|
||
|
||
// Her chunk arasında kısa bekleme
|
||
usleep(100000); // 0.1 saniye
|
||
}
|
||
|
||
// ===== SYNC TO PAINT_MATRICES =====
|
||
Log::debug("Syncing to Paint Matrices");
|
||
|
||
// Get RAL codes JSON for Russian color mapping
|
||
$ralCodes = j(setting("ral-codes"));
|
||
|
||
// Helper function to find Russian color description from RAL code
|
||
$findRussianColorDescription = function($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 '';
|
||
};
|
||
|
||
// Field mappings from paint_systems to paint_matrices
|
||
$pmUpdateData = [
|
||
// Main identifiers
|
||
'paint_cycle' => $paintSystem->paint_cycle ?? '',
|
||
|
||
// Preparation fields
|
||
'surface_preparation' => $paintSystem->surface_preparation ?? '',
|
||
'touch_up_of_damaged_parts' => $paintSystem->surface_roughness ?? '',
|
||
|
||
// First layer (Primer coat)
|
||
'primer_coat' => $paintSystem->primer_coat_name_1 ?? '',
|
||
'brend_name_1' => $paintSystem->brand_name_1 ?? '',
|
||
'thickness_1' => $paintSystem->thickness_1 ?? '',
|
||
|
||
// Second layer (Intermediate coat)
|
||
'intermediate_coat' => $paintSystem->primer_coat_name_2 ?? '',
|
||
'brend_name_2' => $paintSystem->brand_name_2 ?? '',
|
||
'thickness_2' => $paintSystem->thickness_2 ?? '',
|
||
|
||
// Third layer (Final coat)
|
||
'final_coat' => $paintSystem->primer_coat_name_3 ?? '',
|
||
'brend_name_3' => $paintSystem->brand_name_3 ?? '',
|
||
'thickness_3' => $paintSystem->thickness_3 ?? '',
|
||
|
||
// Totals
|
||
'total_thickness' => ($paintSystem->thickness_1 ?? 0) + ($paintSystem->thickness_2 ?? 0) + ($paintSystem->thickness_3 ?? 0) ?? '',
|
||
|
||
// Timestamps
|
||
'updated_at' => now()
|
||
];
|
||
|
||
// Remove empty fields to avoid overwriting existing data with empty values
|
||
foreach ($pmUpdateData as $key => $value) {
|
||
if ($value === '' && $key !== 'paint_cycle') { // Keep the key field
|
||
unset($pmUpdateData[$key]);
|
||
}
|
||
}
|
||
|
||
// Log the fields being synced (base fields, color data will be added per matrix)
|
||
if (count($pmUpdateData) > 2) { // >2 because 'updated_at' is always included
|
||
Log::debug("Syncing fields to Paint Matrices (base data from paint_systems)", ['fields' => array_diff_key($pmUpdateData, array_flip(['updated_at']))]);
|
||
Log::debug("Color data (RAL codes and Russian descriptions) will be fetched from color_systems for each matrix based on fluid_code");
|
||
} else {
|
||
Log::debug("No relevant data found to sync to Paint Matrices. Only updating timestamp.");
|
||
}
|
||
|
||
// Paint Matrices işlemlerini chunk'lara böl
|
||
$paintMatrices = DB::table('paint_matrices')
|
||
->where('paint_cycle', $paintSystem->paint_cycle)
|
||
->get();
|
||
|
||
$paintMatricesChunked = $paintMatrices->chunk(8); // 8'li gruplar
|
||
|
||
$pmUpdatedCount = 0;
|
||
|
||
foreach($paintMatricesChunked as $matricesChunk) {
|
||
// Her chunk için ayrı transaction
|
||
DB::transaction(function () use ($matricesChunk, $pmUpdateData, $findRussianColorDescription, $ralCodes, &$pmUpdatedCount) {
|
||
foreach($matricesChunk as $matrix) {
|
||
// Create a copy of update data for each matrix
|
||
$matrixUpdateData = $pmUpdateData;
|
||
|
||
// Get color system data for this matrix's fluid_code
|
||
if (!empty($matrix->fluid_code)) {
|
||
$colorSystem = DB::table('color_systems')
|
||
->where('fluid_code', $matrix->fluid_code)
|
||
->first();
|
||
|
||
if ($colorSystem) {
|
||
// Get RAL values from color_systems
|
||
$ral1 = $colorSystem->ral_1 ?? $colorSystem->ral_code_1 ?? '';
|
||
$ral2 = $colorSystem->ral_2 ?? $colorSystem->ral_code_2 ?? '';
|
||
$ral3 = $colorSystem->ral_3 ?? $colorSystem->ral_code_3 ?? '';
|
||
|
||
// Get Russian color descriptions
|
||
$colorRu1 = $findRussianColorDescription($ralCodes, $ral1);
|
||
$colorRu2 = $findRussianColorDescription($ralCodes, $ral2);
|
||
$colorRu3 = $findRussianColorDescription($ralCodes, $ral3);
|
||
|
||
// Add RAL codes and Russian colors to update data
|
||
$matrixUpdateData['ral_code_1'] = $ral1;
|
||
$matrixUpdateData['ral_code_2'] = $ral2;
|
||
$matrixUpdateData['ral_code_3'] = $ral3;
|
||
$matrixUpdateData['colour_1'] = $colorRu1;
|
||
$matrixUpdateData['colour_2'] = $colorRu2;
|
||
$matrixUpdateData['colour_3'] = $colorRu3;
|
||
|
||
// Add fluid code description and temperature data
|
||
$matrixUpdateData['fluid_code_description'] = $colorSystem->fluid_code_description ?? '';
|
||
$matrixUpdateData['design_temperature'] = $colorSystem->design_temperature ?? '';
|
||
$matrixUpdateData['operation_temperature'] = $colorSystem->working_temperature ?? '';
|
||
|
||
Log::debug("Added color data for matrix {$matrix->id} - fluid_code: {$matrix->fluid_code}, RAL1: {$ral1}, Color1: {$colorRu1}");
|
||
} else {
|
||
Log::debug("No color system found for fluid_code: {$matrix->fluid_code} in matrix {$matrix->id}");
|
||
}
|
||
}
|
||
|
||
DB::table('paint_matrices')
|
||
->where('id', $matrix->id)
|
||
->update($matrixUpdateData);
|
||
$pmUpdatedCount++;
|
||
}
|
||
}, 3); // 3 deneme ile retry
|
||
|
||
// Her chunk arasında kısa bekleme
|
||
usleep(100000); // 0.1 saniye
|
||
}
|
||
|
||
if ($pmUpdatedCount > 0) {
|
||
Log::debug("Updated Paint Matrices records with paint system and color system data", ['count' => $pmUpdatedCount]);
|
||
} else {
|
||
Log::debug("No Paint Matrices records found", ['paint_cycle' => $paintSystem->paint_cycle]);
|
||
}
|
||
|
||
// ===== SYNC TO PAINT_FOLLOW_UPS =====
|
||
Log::debug("Syncing to Paint Follow Ups");
|
||
|
||
// Field mappings from paint_systems to paint_follow_ups
|
||
$pfUpdateData = [
|
||
// Main identifiers
|
||
'cycle' => $paintSystem->paint_cycle ?? '',
|
||
|
||
// Preparation fields
|
||
'surface_preparation_equipment' => $paintSystem->surface_preparation ?? '',
|
||
'surface_roughness' => $paintSystem->surface_roughness ?? '',
|
||
|
||
// First layer
|
||
'primer_coat_name_1' => $paintSystem->primer_coat_name_1 ?? '',
|
||
'brend_name_1' => $paintSystem->brand_name_1 ?? '',
|
||
'thickness_1' => $paintSystem->thickness_1 ?? '',
|
||
|
||
// Second layer
|
||
'intermediate_coat_name_2' => $paintSystem->primer_coat_name_2 ?? '',
|
||
'brend_name_2' => $paintSystem->brand_name_2 ?? '',
|
||
'thickness_2' => $paintSystem->thickness_2 ?? '',
|
||
|
||
// Third layer
|
||
'final_coat_name_3' => $paintSystem->primer_coat_name_3 ?? '',
|
||
'brend_name_3' => $paintSystem->brand_name_3 ?? '',
|
||
'thickness_3' => $paintSystem->thickness_3 ?? '',
|
||
|
||
// Totals
|
||
'total_thickness' => ($paintSystem->thickness_1 ?? 0) + ($paintSystem->thickness_2 ?? 0) + ($paintSystem->thickness_3 ?? 0),
|
||
|
||
// 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 ($pfUpdateData as $key => $value) {
|
||
if ($value === '' && $key !== 'cycle') { // Keep the key field
|
||
unset($pfUpdateData[$key]);
|
||
}
|
||
}
|
||
|
||
// Log the fields being synced
|
||
if (count($pfUpdateData) > 2) { // >2 because timestamps are always included
|
||
Log::debug("Syncing fields to Paint Follow Ups", ['fields' => array_diff_key($pfUpdateData, array_flip(['updated_at', 'created_at']))]);
|
||
} else {
|
||
Log::debug("No relevant data found to sync to Paint Follow Ups. Only updating timestamp.");
|
||
}
|
||
|
||
// Paint Follow Ups işlemlerini chunk'lara böl
|
||
$paintFollowUps = DB::table('paint_follow_ups')
|
||
->where('cycle', $paintSystem->paint_cycle)
|
||
->whereNull('primer_coating_start_date')
|
||
->get();
|
||
|
||
$paintFollowUpsChunked = $paintFollowUps->chunk(8); // 8'li gruplar
|
||
|
||
$pfUpdatedCount = 0;
|
||
|
||
foreach($paintFollowUpsChunked as $followUpsChunk) {
|
||
// Her chunk için ayrı transaction
|
||
DB::transaction(function () use ($followUpsChunk, $pfUpdateData, &$pfUpdatedCount) {
|
||
foreach($followUpsChunk as $followUp) {
|
||
DB::table('paint_follow_ups')
|
||
->where('id', $followUp->id)
|
||
->update($pfUpdateData);
|
||
$pfUpdatedCount++;
|
||
}
|
||
}, 3); // 3 deneme ile retry
|
||
|
||
// Her chunk arasında kısa bekleme
|
||
usleep(100000); // 0.1 saniye
|
||
}
|
||
|
||
if ($pfUpdatedCount > 0) {
|
||
Log::debug("Updated Paint Follow Ups records", ['count' => $pfUpdatedCount]);
|
||
} else {
|
||
// Check if this paint_cycle exists in weld_logs before creating new record
|
||
$weldLogExists = DB::table('weld_logs')
|
||
->where('painting_cycle', $paintSystem->paint_cycle)
|
||
->exists();
|
||
|
||
if ($weldLogExists) {
|
||
// If no records with this cycle exist but weld_log exists, create a new one
|
||
$newRecordData = array_merge($pfUpdateData, [
|
||
'cycle' => $paintSystem->paint_cycle ?? '',
|
||
'status' => 'Not Started',
|
||
'created_at' => now()
|
||
]);
|
||
|
||
// DB::table('paint_follow_ups')->insert($newRecordData);
|
||
Log::debug("Created new Paint Follow Ups record", ['cycle' => $paintSystem->paint_cycle]);
|
||
} else {
|
||
Log::debug("Skipped creating Paint Follow Ups record - no related weld_log found", ['cycle' => $paintSystem->paint_cycle]);
|
||
}
|
||
}
|
||
|
||
DB::commit();
|
||
Log::debug("Synchronization completed", [
|
||
'construction_paint_logs' => ['updated' => $cpUpdatedCount, 'created' => $cpCreatedCount],
|
||
'paint_matrices' => ['updated' => $pmUpdatedCount],
|
||
'paint_follow_ups' => ['updated' => $pfUpdatedCount]
|
||
]);
|
||
} catch (\Throwable $th) {
|
||
DB::rollback();
|
||
Log::error("Error synchronizing from Paint Systems: " . $th->getMessage());
|
||
|
||
// Log detailed error information for debugging
|
||
Log::error("Error details: ", [
|
||
'paintSystemId' => $id,
|
||
'paint_cycle' => $paintSystem->paint_cycle ?? 'unknown',
|
||
'exception' => get_class($th),
|
||
'file' => $th->getFile(),
|
||
'line' => $th->getLine(),
|
||
'trace' => $th->getTraceAsString()
|
||
]);
|
||
}
|
||
?>
|