From c3a4985f45d3789a8841d62583b9cdf3b33a0fca Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 20 Feb 2026 17:11:05 +0530 Subject: [PATCH] Updated validation logic on importer and exporter --- .../ProductCharacteristicsMasterExporter.php | 2 +- app/Filament/Imports/ProcessOrderImporter.php | 19 +-- .../ProductCharacteristicsMasterImporter.php | 126 +++++++++--------- 3 files changed, 70 insertions(+), 77 deletions(-) diff --git a/app/Filament/Exports/ProductCharacteristicsMasterExporter.php b/app/Filament/Exports/ProductCharacteristicsMasterExporter.php index a2a05c4..14ac682 100644 --- a/app/Filament/Exports/ProductCharacteristicsMasterExporter.php +++ b/app/Filament/Exports/ProductCharacteristicsMasterExporter.php @@ -29,7 +29,7 @@ class ProductCharacteristicsMasterExporter extends Exporter ExportColumn::make('line.name') ->label('LINE NAME'), // machine.workGroupMaster.name ExportColumn::make('machine.workGroupMaster.name') - ->label('WORK GROUP MASTER'), + ->label('GROUP WORK CENTER'), ExportColumn::make('machine.work_center') ->label('WORK CENTER'), ExportColumn::make('characteristics_type') diff --git a/app/Filament/Imports/ProcessOrderImporter.php b/app/Filament/Imports/ProcessOrderImporter.php index 913217b..fb26133 100644 --- a/app/Filament/Imports/ProcessOrderImporter.php +++ b/app/Filament/Imports/ProcessOrderImporter.php @@ -176,20 +176,7 @@ class ProcessOrderImporter extends Importer throw new RowImportFailedException(implode(', ', $warnMsg)); } - // return ProcessOrder::create([ - // 'plant_id' => $plant->id, - // 'item_id' => $itemCode->id, - // 'line_id' => $lineId, - // 'process_order' => trim($this->data['process_order']), - // 'coil_number' => $coilNo, - // 'order_quantity' => $orderQuan, - // 'received_quantity' => $recQuan, - // 'sfg_number' => $sfgNo, - // 'machine_name' => $machineName, - // 'scrap_quantity' => $scrapQuan, - // 'rework_status' => $reworkStatus, - // 'created_by' => $createdBy, - // ]); + $lineId = null; if($lineName != null && $lineName != ''){ @@ -233,7 +220,7 @@ class ProcessOrderImporter extends Importer 'scrap_quantity' => $scrapQuan, // 'sfg_number' => $sfgNo, // 'machine_name' => $machineId, - 'rework_status' => 1, + 'rework_status' => $reworkStatus, 'updated_by' => $updatedBy, 'updated_at' => $updatedAt, ]); @@ -279,7 +266,7 @@ class ProcessOrderImporter extends Importer 'scrap_quantity' => $scrapQuan, // 'sfg_number' => $sfgNo, // 'machine_name' => $machineId, - 'rework_status' => 1, + 'rework_status' => $reworkStatus, 'updated_by' => $updatedBy, 'updated_at' => $updatedAt, ]); diff --git a/app/Filament/Imports/ProductCharacteristicsMasterImporter.php b/app/Filament/Imports/ProductCharacteristicsMasterImporter.php index 23711db..03b52d3 100644 --- a/app/Filament/Imports/ProductCharacteristicsMasterImporter.php +++ b/app/Filament/Imports/ProductCharacteristicsMasterImporter.php @@ -13,7 +13,7 @@ use Filament\Actions\Imports\Exceptions\RowImportFailedException; use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Models\Import; -use Illuminate\Support\Facades\Auth; +use Filament\Facades\Filament; use Str; class ProductCharacteristicsMasterImporter extends Importer @@ -75,7 +75,7 @@ class ProductCharacteristicsMasterImporter extends Importer ->rules(['required']), ImportColumn::make('lower') ->exampleHeader('Lower') - ->example('-0.2') + ->example('0') ->label('Lower') ->rules(['numeric']), ImportColumn::make('middle') @@ -86,7 +86,7 @@ class ProductCharacteristicsMasterImporter extends Importer ->rules(['numeric']), ImportColumn::make('upper') ->exampleHeader('Upper') - ->example('0.2') + ->example('2') ->label('Upper') ->rules(['numeric']), ImportColumn::make('created_by') @@ -103,7 +103,12 @@ class ProductCharacteristicsMasterImporter extends Importer $warnMsg = []; $plantCod = $this->data['plant']; - $plant = null; + $updatedBy = Filament::auth()->user()->name; // ?? 'Admin' + $lower = null; + $middle = null; + $upper = null; + $plantId = null; + $name = trim($this->data['name']); if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) { $warnMsg[] = 'Invalid plant code found'; } else { @@ -111,13 +116,14 @@ class ProductCharacteristicsMasterImporter extends Importer if (! $plant) { $warnMsg[] = 'Plant not found'; } else { + $plantId = $plant->id; $itemExists = Item::where('code', $this->data['item'])->first(); if (! $itemExists) { $warnMsg[] = 'Item not found'; } $itemAgainstPlant = Item::where('code', $this->data['item']) - ->where('plant_id', $plant->id) + ->where('plant_id', $plantId) ->first(); if (! $itemAgainstPlant) { $warnMsg[] = 'Item code not found for the given plant'; @@ -131,7 +137,7 @@ class ProductCharacteristicsMasterImporter extends Importer } $lineAgainstPlant = Line::where('name', $this->data['line']) - ->where('plant_id', $plant->id) + ->where('plant_id', $plantId) ->first(); if (! $lineAgainstPlant) { @@ -140,7 +146,7 @@ class ProductCharacteristicsMasterImporter extends Importer $LineId = $lineAgainstPlant->id; } - $WorkgroupMaster = WorkGroupMaster::where('name', $this->data['work_group_master_id'])->where('plant_id', $plant->id)->first(); + $WorkgroupMaster = WorkGroupMaster::where('name', $this->data['work_group_master_id'])->where('plant_id', $plantId)->first(); if (! $WorkgroupMaster) { $warnMsg[] = 'Work Group Master value not found'; } else { @@ -148,7 +154,7 @@ class ProductCharacteristicsMasterImporter extends Importer $workGroupMasterId = $WorkgroupMaster->id; // 2. Now check if this WorkGroupMaster id exists in ANY of the 10 columns - $existsInLine = Line::where('plant_id', $plant->id) + $existsInLine = Line::where('plant_id', $plantId) ->where(function ($q) use ($workGroupMasterId) { for ($i = 1; $i <= 10; $i++) { $q->orWhere("work_group{$i}_id", $workGroupMasterId); @@ -171,7 +177,7 @@ class ProductCharacteristicsMasterImporter extends Importer } $machineAgainstPlant = Machine::where('work_center', $this->data['machine']) - ->where('plant_id', $plant->id) + ->where('plant_id', $plantId) ->first(); if (! $machineAgainstPlant) { @@ -180,15 +186,24 @@ class ProductCharacteristicsMasterImporter extends Importer $machineId = $machineAgainstPlant->id; } + if ($name == null || $name == '') { + $warnMsg[] = "Name can't be empty"; + } + $user = User::where('name', $this->data['created_by'])->first(); if (! $user) { $warnMsg[] = 'Operator ID not found'; } + $updatedBy = Filament::auth()->user()->name; // ?? 'Admin' + if (! $updatedBy) { + $warnMsg[] = 'Invalid updated by user name found'; + } + if (($this->data['inspection_type'] ?? null) == 'Value') { - $upper = $this->data['upper'] ?? null; $lower = $this->data['lower'] ?? null; $middle = $this->data['middle'] ?? null; + $upper = $this->data['upper'] ?? null; if (is_null($upper) || is_null($lower) || is_null($middle)) { $warnMsg[] = "For 'Value' inspection type, Upper, Lower, and Middle values are required."; @@ -203,64 +218,55 @@ class ProductCharacteristicsMasterImporter extends Importer if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); - } else { - $record = ProductCharacteristicsMaster::firstOrNew([ - 'plant_id' => $plant->id, + } + + // $record = ProductCharacteristicsMaster::firstOrNew([ + // 'plant_id' => $plantId, + // 'item_id' => $itemId, + // 'line_id' => $LineId, + // 'work_group_master_id' => $workGroupMasterId, + // 'machine_id' => $machineId, + // ]); + // // If record is new, fill all fields + // if (! $record->exists) { + // $record->name = $name; + // $record->characteristics_type = $this->data['characteristics_type']; + // $record->inspection_type = $this->data['inspection_type']; + // $record->lower = $this->data['lower'] ?? null; + // $record->upper = $this->data['upper'] ?? null; + // $record->middle = $this->data['middle'] ?? null; + // $record->created_by = $this->data['created_by'] ?? null; + // $record->updated_by = $updatedBy ?? null; + // } else { + // // Record exists → update only updated_by and updated_at + // $record->updated_by = $updatedBy ?? null; + // $record->touch(); + // } + // $record->save(); + + ProductCharacteristicsMaster::updateOrCreate( + [ + 'plant_id' => $plantId, 'item_id' => $itemId, 'line_id' => $LineId, 'work_group_master_id' => $workGroupMasterId, 'machine_id' => $machineId, + ], + [ + 'name' => $name, + 'characteristics_type' => $this->data['characteristics_type'], + 'inspection_type' => $this->data['inspection_type'], + 'lower' => $lower, + 'middle' => $middle, + 'upper' => $upper, + // 'created_by' => user ?? $this->data['created_by'], + 'created_by' => $this->data['created_by'] ?? null, + 'updated_by' => $updatedBy ?? null, ]); - $currentUser = Auth::check() ? Auth::user()->name : ($this->data['created_by'] ?? 'System'); - - // If record is new, fill all fields - if (! $record->exists) { - $record->name = $this->data['name']; - $record->characteristics_type = $this->data['characteristics_type']; - $record->inspection_type = $this->data['inspection_type']; - $record->upper = $this->data['upper'] ?? null; - $record->lower = $this->data['lower'] ?? null; - $record->middle = $this->data['middle'] ?? null; - $record->created_by = $currentUser; - } else { - // Record exists → update only updated_by and updated_at - $record->updated_by = $currentUser; - $record->touch(); - } - - $record->save(); - - return null; - } - // else - // { - // ProductCharacteristicsMaster::updateOrCreate( - // [ - // 'plant_id' => $plant->id, - // 'item_id' => $itemId, - // 'line_id' => $LineId, - // 'work_group_master_id' => $workGroupMasterId, - // 'machine_id'=> $machineId, - - // ], - // [ - // 'name' => $this->data['name'], - // 'characteristics_type' => $this->data['characteristics_type'], - // 'inspection_type' => $this->data['inspection_type'], - // 'upper' => $this->data['upper'] ?? null, - // 'lower' => $this->data['lower'] ?? null, - // 'middle' => $this->data['middle'] ?? null, - // //'created_by' => user ?? $this->data['created_by'], - // 'created_by' => Auth::check() ? Auth::user()->name :($this->data['created_by'] ?? null) - // ] - // ); - - // return null; - // } + return null; // return new WorkGroupMaster(); - // return new ProductCharacteristicsMaster(); } -- 2.49.1