137 lines
5.3 KiB
PHP
137 lines
5.3 KiB
PHP
<?php
|
||
|
||
namespace App\Services\LineListTriggers\Triggers;
|
||
|
||
use App\Services\LineListTriggers\Base\BaseLineListTrigger;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
/**
|
||
* Painting Cycle Deletion Trigger
|
||
*
|
||
* Handles cleanup when painting_cycle is deleted (changed from value to empty):
|
||
* - Deletes paint_matrices records
|
||
* - Deletes paint_follow_ups records (where date fields are null)
|
||
* - Deletes construction_paint_logs records (where date fields are null)
|
||
* - Deletes nde_matrices records
|
||
*/
|
||
class PaintingCycleDeletionTrigger extends BaseLineListTrigger
|
||
{
|
||
public function getName(): string
|
||
{
|
||
return 'Painting Cycle Deletion';
|
||
}
|
||
|
||
public function getDependentFields(): array
|
||
{
|
||
return ['painting_cycle'];
|
||
}
|
||
|
||
protected function process($lineListData, $beforeData, array $context): array
|
||
{
|
||
// Only run if painting_cycle was deleted (had value, now empty)
|
||
if (!isset($beforeData) || $beforeData->painting_cycle == "" || $lineListData->painting_cycle != "") {
|
||
return ['skipped' => true, 'reason' => 'painting_cycle_not_deleted'];
|
||
}
|
||
|
||
Log::debug("Painting cycle siliniyor - ilgili verileri temizleme başlatılıyor", [
|
||
'before_painting_cycle' => $beforeData->painting_cycle,
|
||
'current_painting_cycle' => $lineListData->painting_cycle,
|
||
'line_no' => $lineListData->line_no,
|
||
'fluid_code' => $lineListData->fluid_code
|
||
]);
|
||
|
||
$results = [];
|
||
|
||
// Delete paint matrices
|
||
$results['deleted_paint_matrices'] = db("paint_matrices")
|
||
->where('line', $beforeData->line_no)
|
||
->where('fluid_code', $beforeData->fluid_code)
|
||
->where('area', $beforeData->unit)
|
||
->delete();
|
||
|
||
// Delete paint follow ups (only if no dates filled)
|
||
$results['deleted_paint_follow_ups'] = db('paint_follow_ups')
|
||
->where([
|
||
'line' => $lineListData->line_no,
|
||
'area' => $lineListData->unit,
|
||
'fluid_code' => $lineListData->fluid_code,
|
||
])
|
||
->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 (spool-based, only if no dates filled)
|
||
// 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->isNotEmpty()) {
|
||
$results['deleted_construction_paint_logs'] = 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::debug("Construction Paint Logs deleted (spool-based)", [
|
||
'line_no' => $lineListData->line_no,
|
||
'spool_count' => $spools->count(),
|
||
'deleted_count' => $results['deleted_construction_paint_logs']
|
||
]);
|
||
} else {
|
||
// Fallback to unit-based deletion if no spools found
|
||
$results['deleted_construction_paint_logs'] = db('construction_paint_logs')
|
||
->where([
|
||
'line' => $lineListData->line_no,
|
||
'unit' => $lineListData->unit,
|
||
'fluid_code' => $lineListData->fluid_code,
|
||
])
|
||
->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 (unit-based fallback)", [
|
||
'line_no' => $lineListData->line_no,
|
||
'unit' => $lineListData->unit,
|
||
'deleted_count' => $results['deleted_construction_paint_logs']
|
||
]);
|
||
}
|
||
|
||
// Delete NDE Matrix
|
||
$results['deleted_nde_matrices'] = db('nde_matrices')
|
||
->where('line', $lineListData->line_no)
|
||
->where('fluid', $lineListData->fluid_code)
|
||
->delete();
|
||
|
||
Log::info("Painting cycle deletion cleanup completed", [
|
||
'deleted_records' => $results,
|
||
'line_no' => $lineListData->line_no,
|
||
'fluid_code' => $lineListData->fluid_code
|
||
]);
|
||
|
||
return $results;
|
||
}
|
||
}
|
||
|
||
|