painting_cycle == $lineListData->painting_cycle) { return ['skipped' => true, 'reason' => 'painting_cycle_not_changed']; } //değişiklik yapıldığında draft olan cycle'ın tüm recordlarını sil $paintFollowUps = db('paint_follow_ups') ->where('line', $lineListData->line_no) ->where('cycle', $beforeData->painting_cycle) ->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 deleted data", [ 'paint_follow_ups' => $paintFollowUps ]); $constructionPaintLogs = db('construction_paint_logs') ->where('line', $lineListData->line_no) ->where("painting_system_type_1", $beforeData->painting_cycle) ->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(); Log::debug("Construction paint logs deleted data", [ 'construction_paint_logs' => $constructionPaintLogs ]); Log::debug("Paint cycle changed - Starting processing", [ 'before_painting_cycle' => $beforeData->painting_cycle ?? 'NULL', 'current_painting_cycle' => $lineListData->painting_cycle ?? 'NULL', 'line_no' => $lineListData->line_no ]); $results = [ 'weld_logs_updated' => 0, 'paint_matrices_updated' => 0, 'paint_follow_ups_updated' => 0, 'paint_follow_ups_held' => 0, 'construction_paint_logs_deleted' => 0 ]; // Check if painting_cycle became empty if (empty($lineListData->painting_cycle)) { // Painting cycle was removed - delete Construction Paint Logs Log::debug("Painting cycle became empty - Deleting Construction Paint Logs", [ 'line_no' => $lineListData->line_no ]); $results['construction_paint_logs_deleted'] = $this->deleteConstructionPaintLogs($lineListData); } else { // Painting cycle has value - update WeldLogs, Paint Matrices AND Paint Follow Ups // This is critical: these updates must happen BEFORE PaintSystemToMatrixSyncTrigger runs // so that subsequent triggers can find records with the new painting_cycle value $results['weld_logs_updated'] = $this->updateWeldLogsPaintingCycle($lineListData); $results['paint_matrices_updated'] = $this->updatePaintMatricesPaintingCycle($lineListData, $beforeData); // Update ALL paint_follow_ups records for this line to new painting_cycle // This ensures PaintFollowUpsSyncTrigger will update ALL records, not just ones from weld_logs $pfuResults = $this->updatePaintFollowUpsCycle($lineListData, $beforeData); $results['paint_follow_ups_updated'] = $pfuResults['updated']; $results['paint_follow_ups_held'] = $pfuResults['held']; } Log::info("Paint cycle change processing completed", [ 'line_no' => $lineListData->line_no, 'painting_cycle' => $lineListData->painting_cycle, 'results' => $results ]); return $results; } /** * Update WeldLogs painting_cycle field */ protected function updateWeldLogsPaintingCycle($lineListData): int { try { $weldLogsUpdateCount = db("weld_logs") ->where("line_number", $lineListData->line_no) ->update(['painting_cycle' => $lineListData->painting_cycle]); Log::debug("WeldLogs painting_cycle updated", [ 'line_no' => $lineListData->line_no, 'updated_count' => $weldLogsUpdateCount, 'new_painting_cycle' => $lineListData->painting_cycle ]); return $weldLogsUpdateCount; } catch (\Throwable $th) { Log::error("WeldLogs painting_cycle update error: " . $th->getMessage(), [ 'line_no' => $lineListData->line_no ]); return 0; } } /** * Delete Construction Paint Logs when painting_cycle becomes empty */ protected function deleteConstructionPaintLogs($lineListData): int { try { // Get all spools for this line from weld_logs $spools = db("weld_logs") ->where("line_number", $lineListData->line_no) ->whereNotNull('spool_number') ->where('spool_number', '!=', '') ->distinct() ->pluck('spool_number'); if ($spools->isEmpty()) { Log::debug("No spools found for line", ['line_no' => $lineListData->line_no]); return 0; } // Delete Construction Paint Logs for these spools (only if painting not started) $deletedCount = db('construction_paint_logs') ->where('line', $lineListData->line_no) ->whereIn('spool', $spools) ->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(); Log::info("Construction Paint Logs deleted due to empty painting_cycle", [ 'line_no' => $lineListData->line_no, 'deleted_count' => $deletedCount, 'spool_count' => $spools->count() ]); return $deletedCount; } catch (\Throwable $th) { Log::error("Construction Paint Logs deletion error: " . $th->getMessage(), [ 'line_no' => $lineListData->line_no ]); return 0; } } /** * Update Paint Matrices painting_cycle field * * CRITICAL: This must run BEFORE PaintSystemToMatrixSyncTrigger and PaintFollowUpsSyncTrigger * so they can find the correct paint_matrices records with the new painting_cycle value */ protected function updatePaintMatricesPaintingCycle($lineListData, $beforeData): int { try { // Build query to find paint_matrices records to update $query = db("paint_matrices") ->where("line", $lineListData->line_no) ->where("fluid_code", $lineListData->fluid_code) ->where("area", $lineListData->unit); // If we know the old painting_cycle, use it for more precise targeting if (isset($beforeData) && !empty($beforeData->painting_cycle)) { $query->where("paint_cycle", $beforeData->painting_cycle); Log::debug("Updating paint_matrices from old to new painting_cycle", [ 'line_no' => $lineListData->line_no, 'fluid_code' => $lineListData->fluid_code, 'area' => $lineListData->unit, 'old_painting_cycle' => $beforeData->painting_cycle, 'new_painting_cycle' => $lineListData->painting_cycle ]); } else { Log::debug("Updating paint_matrices painting_cycle (no old value)", [ 'line_no' => $lineListData->line_no, 'fluid_code' => $lineListData->fluid_code, 'area' => $lineListData->unit, 'new_painting_cycle' => $lineListData->painting_cycle ]); } $paintMatricesUpdateCount = $query->update([ 'paint_cycle' => $lineListData->painting_cycle, 'updated_at' => now() ]); Log::info("Paint Matrices painting_cycle updated", [ 'line_no' => $lineListData->line_no, 'fluid_code' => $lineListData->fluid_code, 'area' => $lineListData->unit, 'updated_count' => $paintMatricesUpdateCount, 'new_painting_cycle' => $lineListData->painting_cycle ]); return $paintMatricesUpdateCount; } catch (\Throwable $th) { Log::error("Paint Matrices painting_cycle update error: " . $th->getMessage(), [ 'line_no' => $lineListData->line_no, 'file' => $th->getFile(), 'line' => $th->getLine(), 'trace' => $th->getTraceAsString() ]); return 0; } } /** * Update Paint Follow Ups cycle field for ALL records * * CRITICAL: This ensures ALL paint_follow_ups records (not just ones from weld_logs) * get their cycle updated when painting_cycle changes * * Strategy: * - Records with empty date fields: Update cycle to new value (will be updated by PaintFollowUpsSyncTrigger) * - Records with filled date fields: Mark as HOLD (painting already started, should not change) */ protected function updatePaintFollowUpsCycle($lineListData, $beforeData): array { $updatedCount = 0; $heldCount = 0; try { // Build query to find ALL paint_follow_ups records for this line $query = db("paint_follow_ups") ->where("line", $lineListData->line_no) ->where("fluid_code", $lineListData->fluid_code) ->where("area", $lineListData->unit); // If we know the old painting_cycle, target only those records if (isset($beforeData) && !empty($beforeData->painting_cycle)) { $query->where("cycle", $beforeData->painting_cycle); Log::debug("Updating paint_follow_ups from old to new cycle", [ 'line_no' => $lineListData->line_no, 'fluid_code' => $lineListData->fluid_code, 'area' => $lineListData->unit, 'old_cycle' => $beforeData->painting_cycle, 'new_cycle' => $lineListData->painting_cycle ]); } else { // If no old cycle, update records that don't match new cycle $query->where("cycle", "!=", $lineListData->painting_cycle); Log::debug("Updating paint_follow_ups to new cycle (no old value)", [ 'line_no' => $lineListData->line_no, 'fluid_code' => $lineListData->fluid_code, 'area' => $lineListData->unit, 'new_cycle' => $lineListData->painting_cycle ]); } $paintFollowUps = $query->get(); Log::info("Found {$paintFollowUps->count()} paint_follow_ups records to process", [ 'line_no' => $lineListData->line_no ]); foreach ($paintFollowUps as $pfu) { $hasAllEmptyDates = $this->hasAllEmptyDates($pfu); if ($hasAllEmptyDates) { // No painting started - safe to update cycle db("paint_follow_ups") ->where('id', $pfu->id) ->update([ 'cycle' => $lineListData->painting_cycle, 'updated_at' => now() ]); $updatedCount++; Log::debug("Updated paint_follow_ups cycle (dates empty)", [ 'id' => $pfu->id, 'spool_no_joint_no' => $pfu->spool_no_joint_no, 'location' => $pfu->location ]); } else { // Painting already started - mark as HOLD db("paint_follow_ups") ->where('id', $pfu->id) ->update([ 'status' => 'HOLD', 'updated_at' => now() ]); $heldCount++; Log::debug("Marked paint_follow_ups as HOLD (dates filled)", [ 'id' => $pfu->id, 'spool_no_joint_no' => $pfu->spool_no_joint_no, 'location' => $pfu->location, 'old_cycle' => $pfu->cycle ]); } } Log::info("Paint Follow Ups cycle update completed", [ 'line_no' => $lineListData->line_no, 'total_records' => $paintFollowUps->count(), 'updated_count' => $updatedCount, 'held_count' => $heldCount, 'new_cycle' => $lineListData->painting_cycle ]); return [ 'updated' => $updatedCount, 'held' => $heldCount ]; } catch (\Throwable $th) { Log::error("Paint Follow Ups cycle update error: " . $th->getMessage(), [ 'line_no' => $lineListData->line_no, 'file' => $th->getFile(), 'line' => $th->getLine(), 'trace' => $th->getTraceAsString() ]); return [ 'updated' => $updatedCount, 'held' => $heldCount ]; } } /** * Check if all date fields are empty in a paint_follow_ups record */ protected function hasAllEmptyDates($record): bool { return empty($record->primer_coating_start_date) && empty($record->primer_coating_finish_date) && empty($record->start_intermediate_date2) && empty($record->finish_intermediate_date2) && empty($record->final_coat_start_date3) && empty($record->final_coat_finish_date3); } }