From 770f0c9af3f650ed7fa1e2dc7fb3d3d39a27a415 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 13 May 2026 17:08:53 +0530 Subject: [PATCH] Import logic updated on Production Characteristic Importer --- .../ProductionCharacteristicImporter.php | 179 ++++++++++++++++-- 1 file changed, 158 insertions(+), 21 deletions(-) diff --git a/app/Filament/Imports/ProductionCharacteristicImporter.php b/app/Filament/Imports/ProductionCharacteristicImporter.php index 6814510..cedc038 100644 --- a/app/Filament/Imports/ProductionCharacteristicImporter.php +++ b/app/Filament/Imports/ProductionCharacteristicImporter.php @@ -2,10 +2,17 @@ namespace App\Filament\Imports; +use App\Models\Item; +use App\Models\Line; +use App\Models\Machine; +use App\Models\Plant; use App\Models\ProductionCharacteristic; +use Filament\Actions\Imports\Exceptions\RowImportFailedException; use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Models\Import; +use Filament\Facades\Filament; +use Str; class ProductionCharacteristicImporter extends Importer { @@ -16,40 +23,170 @@ class ProductionCharacteristicImporter extends Importer return [ ImportColumn::make('plant') ->requiredMapping() - ->relationship() + ->exampleHeader('PLANT CODE') + ->example('1200') + ->label('PLANT CODE') + ->relationship(resolveUsing: 'code') ->rules(['required']), ImportColumn::make('line') - ->requiredMapping() - ->relationship() - ->rules(['required']), + ->exampleHeader('LINE NAME') + ->example('Poly Wrapped Wire SFG') + ->label('LINE NAME') + ->relationship(resolveUsing: 'name'), ImportColumn::make('item') ->requiredMapping() - ->relationship() + ->exampleHeader('ITEM CODE') + ->example('123456') + ->label('ITEM CODE') + ->relationship(resolveUsing: 'code') ->rules(['required']), - ImportColumn::make('machine') + ImportColumn::make('machine_name') ->requiredMapping() - ->relationship() + ->exampleHeader('MACHINE NAME') + ->example('RMI001234') + ->label('MACHINE NAME') + // ->relationship(resolveUsing: 'work_center') ->rules(['required']), - ImportColumn::make('production_order'), - ImportColumn::make('serial_number'), - ImportColumn::make('characteristic_name'), - ImportColumn::make('observed_value'), - ImportColumn::make('status'), - ImportColumn::make('inspection_status'), - ImportColumn::make('remark'), - ImportColumn::make('created_by'), - ImportColumn::make('updated_by'), + ImportColumn::make('production_order') + ->requiredMapping() + ->exampleHeader('PRODUCTION ORDER') + ->example('1880231') + ->label('PRODUCTION ORDER') + ->rules(['required']), + ImportColumn::make('serial_number') + ->requiredMapping() + ->exampleHeader('SERIAL NUMBER') + ->example('20001202121') + ->label('SERIAL NUMBER') + ->rules(['required']), + ImportColumn::make('characteristic_name') + ->requiredMapping() + ->exampleHeader('CHARACTERISTIC NAME') + ->example('Voltage') + ->label('CHARACTERISTIC NAME'), + ImportColumn::make('observed_value') + ->exampleHeader('OBSERVED VALUE') + ->example('0') + ->label('OBSERVED VALUE'), + ImportColumn::make('status') + ->exampleHeader('STATUS') + ->example('Ok') + ->label('STATUS'), + ImportColumn::make('remark') + ->exampleHeader('REMARK') + ->example('Issue') + ->label('REMARK'), ]; } public function resolveRecord(): ?ProductionCharacteristic { - // return ProductionCharacteristic::firstOrNew([ - // // Update existing records, matching them by `$this->data['column_name']` - // 'email' => $this->data['email'], - // ]); - return new ProductionCharacteristic(); + $warnMsg = []; + $plant = null; + $plantCod = trim($this->data['plant']) ?? ''; + $plantId = null; + $iCode = trim($this->data['item']) ?? ''; + $itemId = null; + $lineNam = trim($this->data['line']) ?? ''; + $lineId = null; + $machineName = trim($this->data['machine_name'] ?? ''); + $pOrder = trim($this->data['production_order'] ?? ''); + $sNo = trim($this->data['serial_number'] ?? ''); + $charName = trim($this->data['characteristic_name'] ?? ''); + $obsValue = trim($this->data['observed_value'] ?? ''); + $status = trim($this->data['status'] ?? ''); + $remark = trim($this->data['remark'] ?? ''); + + if ($plantCod == null || $plantCod == '') { + $warnMsg[] = "Plant code can't be empty!"; + } elseif (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) { + $warnMsg[] = 'Invalid plant code found'; + } + if ($iCode == null || $iCode == '') { + $warnMsg[] = "Item code can't be empty!"; + } elseif (Str::length($iCode) < 6 || ! ctype_alnum($iCode)) { + $warnMsg[] = 'Invalid item code found!'; + } + if ($machineName != null && $machineName != '' && Str::length($machineName) > 18) { + $warnMsg[] = 'Invalid machine name found!'; + } + + $plant = Plant::where('code', $plantCod)->first(); + if (! $plant) { + $warnMsg[] = 'Plant not found!'; + } else { + $plantId = $plant->id; + } + + $itemCode = Item::where('code', $iCode)->first(); + if (! $itemCode) { + $warnMsg[] = 'Item code not found!'; + } else { + if ($plantId) { + $itemCode = Item::where('code', $iCode)->where('plant_id', $plantId)->first(); + if (! $itemCode) { + $warnMsg[] = 'Item code not found for the given plant!'; + } else { + $itemId = $itemCode->id; + } + } + } + + $lineExists = Line::where('name', $lineNam)->first(); + if (! $lineExists) { + $warnMsg[] = 'Line name not found!'; + } else { + if ($plantId) { + $lineAgainstPlant = Line::where('name', $lineNam)->where('plant_id', $plantId)->first(); + if (! $lineAgainstPlant) { + $warnMsg[] = 'Line name not found for the given plant!'; + } else { + $lineId = $lineAgainstPlant->id; + } + } + } + + $machineExists = Machine::where('work_center', $machineName)->first(); + if (! $machineExists) { + $warnMsg[] = 'Machine not found!'; + } else { + if ($plantId) { + $machineAgainstPlant = Machine::where('work_center', $machineName)->where('plant_id', $plantId)->first(); + if (! $machineAgainstPlant) { + $warnMsg[] = 'Machine not found for the given plant!'; + } else { + $machineId = $machineAgainstPlant->id; + } + } + } + + if (! empty($warnMsg)) { + throw new RowImportFailedException(implode(', ', $warnMsg)); + } + + ProductionCharacteristic::Create( + [ + 'plant_id' => $plantId, + 'line_id' => $lineId, + 'item_id' => $itemId, + 'machine_id' => $machineId, + 'production_order' => $pOrder, + 'serial_number' => $sNo, + 'characteristic_name' => $charName, + 'observed_value' => $obsValue, + 'status' => $status, + 'remark' => $remark, + 'created_at' => now(), + 'updated_at' => now(), + 'created_by' => Filament::auth()->user()->name, + 'updated_by' => Filament::auth()->user()->name, + ] + ); + + return null; + + // return new ProductionCharacteristic(); } public static function getCompletedNotificationBody(Import $import): string