From 98cd3b90f8b3054ff292b3c560929ce361a5e7e1 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sun, 20 Sep 2026 15:19:21 +0530 Subject: [PATCH] changed logic in sticker master importer --- .../Imports/StickerMasterImporter.php | 284 ++++++++++++------ 1 file changed, 188 insertions(+), 96 deletions(-) diff --git a/app/Filament/Imports/StickerMasterImporter.php b/app/Filament/Imports/StickerMasterImporter.php index bcec1da..245ac67 100644 --- a/app/Filament/Imports/StickerMasterImporter.php +++ b/app/Filament/Imports/StickerMasterImporter.php @@ -9,6 +9,7 @@ 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 StickerMasterImporter extends Importer @@ -151,10 +152,10 @@ class StickerMasterImporter extends Importer ->example(''), ImportColumn::make('plant') ->requiredMapping() - ->exampleHeader('Plant Name') - ->example('Ransar Industries-I') - ->label('PLANT NAME') - ->relationship(resolveUsing: 'name') + ->exampleHeader('Plant Code') + ->example('1000') + ->label('PLANT CODE') + ->relationship(resolveUsing: 'code') ->rules(['required']), ]; } @@ -162,107 +163,198 @@ class StickerMasterImporter extends Importer public function resolveRecord(): ?StickerMaster { $warnMsg = []; + $plantCod = trim($this->data['plant']); + $itemCod = trim($this->data['item']); + $panelCod = trim($this->data['panel_box_code']); + $plant = null; $item = null; - $plant = Plant::where('name', $this->data['plant'])->first(); - if (!$plant) { - $warnMsg[] = "Plant not found"; + $plantId = null; + $itemId = null; + $parts = null; + $validParts = null; + $part1 = trim($this->data['part_validation1']); + $part2 = trim($this->data['part_validation2']); + $part3 = trim($this->data['part_validation3']); + $part4 = trim($this->data['part_validation4']); + $part5 = trim($this->data['part_validation5']); + $laserPart1 = trim($this->data['laser_part_validation1']); + $laserPart2 = trim($this->data['laser_part_validation2']); + $SerialNumberMotor = trim($this->data['serial_number_motor']) ?? null; + $SerialNumberMotor = ($SerialNumberMotor == '0') ? null : $SerialNumberMotor; + $SerialNumberPump = trim($this->data['serial_number_pump']) ?? null; + $SerialNumberPump = ($SerialNumberPump == '0') ? null : $SerialNumberPump; + $SerialNumberPumpSet = trim($this->data['serial_number_pumpset']) ?? null; + $SerialNumberPumpSet = ($SerialNumberPumpSet == '0') ? null : $SerialNumberPumpSet; + $PackSlipMotor = trim($this->data['pack_slip_motor']) ?? null; + $PackSlipMotor = ($PackSlipMotor == '0') ? null : $PackSlipMotor; + $PackSlipPump = trim($this->data['pack_slip_pump']) ?? null; + $PackSlipPump = ($PackSlipPump == '0') ? null : $PackSlipPump; + $PackSlipPumpSet = trim($this->data['pack_slip_pumpset']) ?? null; + $PackSlipPumpSet = ($PackSlipPumpSet == '0') ? null : $PackSlipPumpSet; + $NamePlateMotor = trim($this->data['name_plate_motor']) ?? null; + $NamePlateMotor = ($NamePlateMotor == '0') ? null : $NamePlateMotor; + $NamePlatePump = trim($this->data['name_plate_pump']) ?? null; + $NamePlatePump = ($NamePlatePump == '0') ? null : $NamePlatePump; + $NamePlatePumpSet = trim($this->data['name_plate_pumpset']) ?? null; + $NamePlatePumpSet = ($NamePlatePumpSet == '0') ? null : $NamePlatePumpSet; + $TubeStickMotor = trim($this->data['tube_sticker_motor']) ?? null; + $TubeStickMotor = ($TubeStickMotor == '0') ? null : $TubeStickMotor; + $TubeStickPump = trim($this->data['tube_sticker_pump']) ?? null; + $TubeStickPump = ($TubeStickPump == '0') ? null : $TubeStickPump; + $TubeStickPumpSet = trim($this->data['tube_sticker_pumpset']) ?? null; + $TubeStickPumpSet = ($TubeStickPumpSet == '0') ? null : $TubeStickPumpSet; + $WarrantyCard = trim($this->data['warranty_card']) ?? null; + $WarrantyCard = ($WarrantyCard == '0') ? null : $WarrantyCard; + $LoadRate = $this->data['load_rate'] ?? 0; + $BundleQuantity = $this->data['bundle_quantity'] ?? null; + $MaterialType = $this->data['material_type'] ?? null; + + $createdBy = Filament::auth()->user()->name; + $updatedBy = Filament::auth()->user()->name; + if ($plantCod == null || $plantCod == '') { + $warnMsg[] = "Plant code can't be empty!"; + } elseif (! is_numeric($plantCod)) { + $warnMsg[] = 'Plant code should contain only numeric values!'; + } elseif (Str::length($plantCod) < 4 || Str::length($plantCod) > 7) { + $warnMsg[] = 'Plant code must be between 4 and 7 digits only!'; + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCod)) { + $warnMsg[] = 'Invalid plant code found!'; + } else { + $plant = Plant::where('code', $plantCod)->first(); } - else - { - $item = Item::where('code', $this->data['item'])->where('plant_id', $plant->id)->first(); - if (!$item) { - $warnMsg[] = "Item code not found"; - } - else - { - if (Str::length($this->data['serial_number_motor']) > 0 && $this->data['serial_number_motor'] != '1') { - $warnMsg[] = "Serial number motor must be 1 or empty"; - } - if (Str::length($this->data['serial_number_pump']) > 0 && $this->data['serial_number_pump'] != '1') { - $warnMsg[] = "Serial number pump must be 1 or empty"; - } - if (Str::length($this->data['serial_number_pumpset']) > 0 && $this->data['serial_number_pumpset'] != '1') { - $warnMsg[] = "Serial number pumpset must be 1 or empty"; - } - if (Str::length($this->data['pack_slip_motor']) > 0 && $this->data['pack_slip_motor'] != '1') { - $warnMsg[] = "Pack slip motor must be 1 or empty"; - } - if (Str::length($this->data['pack_slip_pump']) > 0 && $this->data['pack_slip_pump'] != '1') { - $warnMsg[] = "Pack slip pump must be 1 or empty"; - } - if (Str::length($this->data['pack_slip_pumpset']) > 0 && $this->data['pack_slip_pumpset'] != '1') { - $warnMsg[] = "Pack slip pumpset must be 1 or empty"; - } - if (Str::length($this->data['name_plate_motor']) > 0 && $this->data['name_plate_motor'] != '1') { - $warnMsg[] = "Name plate motor must be 1 or empty"; - } - if (Str::length($this->data['name_plate_pump']) > 0 && $this->data['name_plate_pump'] != '1') { - $warnMsg[] = "Name plate pump must be 1 or empty"; - } - if (Str::length($this->data['name_plate_pumpset']) > 0 && $this->data['name_plate_pumpset'] != '1') { - $warnMsg[] = "Name plate pumpset must be 1 or empty"; - } - if (Str::length($this->data['tube_sticker_motor']) > 0 && $this->data['tube_sticker_motor'] != '1') { - $warnMsg[] = "Tube sticker motor must be 1 or empty"; - } - if (Str::length($this->data['tube_sticker_pump']) > 0 && $this->data['tube_sticker_pump'] != '1') { - $warnMsg[] = "Tube sticker pump must be 1 or empty"; - } - if (Str::length($this->data['tube_sticker_pumpset']) > 0 && $this->data['tube_sticker_pumpset'] != '1') { - $warnMsg[] = "Tube sticker pumpset must be 1 or empty"; - } - if (Str::length($this->data['warranty_card']) > 0 && $this->data['warranty_card'] != '1') { - $warnMsg[] = "Warranty card must be 1 or empty"; - } - if (Str::length($this->data['panel_box_code']) > 0 && (Str::length($this->data['panel_box_code']) < 6 || !ctype_alnum($this->data['panel_box_code']))) { - $warnMsg[] = "Invalid panel box code found"; - } - if (Str::length($this->data['load_rate']) < 0 || !is_numeric($this->data['load_rate']) || $this->data['load_rate'] < 0) { - $warnMsg[] = "Load rate must be greater than or equal to 0"; - } - if (Str::length($this->data['bundle_quantity']) > 0 && (!is_numeric($this->data['bundle_quantity']) || $this->data['bundle_quantity'] <= 1)) { - $warnMsg[] = "Bundle quantity must be greater than or equal to '2' or empty"; - } - if (Str::length($this->data['material_type']) > 0 && $this->data['material_type'] != '1' && $this->data['material_type'] != '2' && $this->data['material_type'] != '3') { //($this->data['material_type'] != null) && - $warnMsg[] = "Material type must be 1 or 2 or 3 or empty"; + + if (! $plant) { + $warnMsg[] = 'Plant not found!'; + } else { + $plantId = $plant->id; + if (Str::length($itemCod) < 6 || ! ctype_alnum($itemCod)) { + $warnMsg[] = 'Invalid item code found!'; + } else { + $item = Item::where('code', $itemCod)->first(); + if (! $item) { + $warnMsg[] = 'Item code not found!'; + } else { + $item = Item::where('code', $itemCod)->where('plant_id', $plantId)->first(); + if (! $item) { + $warnMsg[] = 'Item code not found for the plant!'; + } else { + $itemId = $item->id; + + if (! $laserPart1 && ! (Str::length($laserPart2) == 1 && is_numeric($laserPart2))) { + $laserPart1 = $laserPart2; + $laserPart2 = null; + } + + if (Str::length($panelCod) > 0 && (Str::length($panelCod) < 6 || ! ctype_alnum($panelCod))) { + $warnMsg[] = 'Invalid panel box code found!'; + } elseif ($panelCod) { + $parts = [$part1, $part2, $part3, $part4]; + $validParts = array_values(array_filter($parts, fn ($p) => ! empty($p))); + [$part1, $part2, $part3, $part4] = array_pad($validParts, 4, ''); + } else { + $parts = [$part1, $part2, $part3, $part4, $part5]; + $validParts = array_values(array_filter($parts, fn ($p) => ! empty($p))); + [$part1, $part2, $part3, $part4, $part5] = array_pad($validParts, 5, ''); + } + + if (Str::length($SerialNumberMotor) > 0 && (! is_numeric($SerialNumberMotor) || $SerialNumberMotor < 1)) { + $warnMsg[] = "Serial number motor must be greater than or equal to '1' or empty!"; + } + if (Str::length($SerialNumberPump) > 0 && (! is_numeric($SerialNumberPump) || $SerialNumberPump < 1)) { + $warnMsg[] = "Serial number pump must be greater than or equal to '1' or empty!"; + } + if (Str::length($SerialNumberPumpSet) > 0 && (! is_numeric($SerialNumberPumpSet) || $SerialNumberPumpSet < 1)) { + $warnMsg[] = "Serial number pumpset must be greater than or equal to '1' or empty!"; + } + if (Str::length($PackSlipMotor) > 0 && (! is_numeric($PackSlipMotor) || $PackSlipMotor < 1)) { + $warnMsg[] = "Pack slip motor must be greater than or equal to '1' or empty!"; + } + if (Str::length($PackSlipPump) > 0 && (! is_numeric($PackSlipPump) || $PackSlipPump < 1)) { + $warnMsg[] = "Pack slip pump must be greater than or equal to '1' or empty!"; + } + if (Str::length($PackSlipPumpSet) > 0 && (! is_numeric($PackSlipPumpSet) || $PackSlipPumpSet < 1)) { + $warnMsg[] = "Pack slip pumpset must be greater than or equal to '1' or empty!"; + } + if (Str::length($NamePlateMotor) > 0 && (! is_numeric($NamePlateMotor) || $NamePlateMotor < 1)) { + $warnMsg[] = "Name plate motor must be greater than or equal to '1' or empty!"; + } + if (Str::length($NamePlatePump) > 0 && (! is_numeric($NamePlatePump) || $NamePlatePump < 1)) { + $warnMsg[] = "Name plate pump must be greater than or equal to '1' or empty!"; + } + if (Str::length($NamePlatePumpSet) > 0 && (! is_numeric($NamePlatePumpSet) || $NamePlatePumpSet < 1)) { + $warnMsg[] = "Name plate pumpset must be greater than or equal to '1' or empty!"; + } + if (Str::length($TubeStickMotor) > 0 && (! is_numeric($TubeStickMotor) || $TubeStickMotor < 1)) { + $warnMsg[] = "Tube sticker motor must be greater than or equal to '1' or empty!"; + } + if (Str::length($TubeStickPump) > 0 && (! is_numeric($TubeStickPump) || $TubeStickPump < 1)) { + $warnMsg[] = "Tube sticker pump must be greater than or equal to '1' or empty!"; + } + if (Str::length($TubeStickPumpSet) > 0 && (! is_numeric($TubeStickPumpSet) || $TubeStickPumpSet < 1)) { + $warnMsg[] = "Tube sticker pumpset must be greater than or equal to '1' or empty!"; + } + if (Str::length($WarrantyCard) > 0 && (! is_numeric($WarrantyCard) || $WarrantyCard < 1)) { + $warnMsg[] = "Warranty card must be greater than or equal to '1' or empty!"; + } + if (Str::length($LoadRate) < 0 || ! is_numeric($LoadRate) || $LoadRate < 0) { + $warnMsg[] = "Load rate must be greater than or equal to '0'!"; + } + if (Str::length($BundleQuantity) > 0 && (! is_numeric($BundleQuantity) || $BundleQuantity <= 1)) { + $warnMsg[] = "Bundle quantity must be greater than or equal to '2' or empty!"; + } + if (Str::length($MaterialType) > 0 && $MaterialType != '1' && $MaterialType != '2' && $MaterialType != '3' && $MaterialType != '4') { // ($this->data['material_type'] != null) && + $warnMsg[] = "Material type must be '1' or '2' or '3' or '4' or empty!"; + } + + if (empty($warnMsg)) { + $recExist = StickerMaster::where('item_id', $itemId)->where('plant_id', $plantId)->first()?->created_by; + + if ($recExist) { + $createdBy = $recExist; + } + } + } } } } - if (!empty($warnMsg)) { + if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); } StickerMaster::updateOrCreate([ - 'item_id' => $item->id, - 'plant_id' => $plant->id + 'item_id' => $itemId, + 'plant_id' => $plantId, ], - [ - 'serial_number_motor' => $this->data['serial_number_motor'], - 'serial_number_pump' => $this->data['serial_number_pump'], - 'serial_number_pumpset' => $this->data['serial_number_pumpset'], - 'pack_slip_motor' => $this->data['pack_slip_motor'], - 'pack_slip_pump' => $this->data['pack_slip_pump'], - 'pack_slip_pumpset' => $this->data['pack_slip_pumpset'], - 'name_plate_motor' => $this->data['name_plate_motor'], - 'name_plate_pump' => $this->data['name_plate_pump'], - 'name_plate_pumpset' => $this->data['name_plate_pumpset'], - 'tube_sticker_motor' => $this->data['tube_sticker_motor'], - 'tube_sticker_pump' => $this->data['tube_sticker_pump'], - 'tube_sticker_pumpset' => $this->data['tube_sticker_pumpset'], - 'warranty_card' => $this->data['warranty_card'], - 'part_validation1' => $this->data['part_validation1'], - 'part_validation2' => $this->data['part_validation2'], - 'part_validation3' => $this->data['part_validation3'], - 'part_validation4' => $this->data['part_validation4'], - 'part_validation5' => $this->data['part_validation5'], - 'laser_part_validation1' => $this->data['laser_part_validation1'], - 'laser_part_validation2' => $this->data['laser_part_validation2'], - 'panel_box_code' => $this->data['panel_box_code'], - 'load_rate' => $this->data['load_rate'], - 'bundle_quantity' => $this->data['bundle_quantity'], - 'material_type' => $this->data['material_type'] - ]); + [ + 'serial_number_motor' => $SerialNumberMotor, + 'serial_number_pump' => $SerialNumberPump, + 'serial_number_pumpset' => $SerialNumberPumpSet, + 'pack_slip_motor' => $PackSlipMotor, + 'pack_slip_pump' => $PackSlipPump, + 'pack_slip_pumpset' => $PackSlipPumpSet, + 'name_plate_motor' => $NamePlateMotor, + 'name_plate_pump' => $NamePlatePump, + 'name_plate_pumpset' => $NamePlatePumpSet, + 'tube_sticker_motor' => $TubeStickMotor, + 'tube_sticker_pump' => $TubeStickPump, + 'tube_sticker_pumpset' => $TubeStickPumpSet, + 'warranty_card' => $WarrantyCard, + 'part_validation1' => $part1, + 'part_validation2' => $part2, + 'part_validation3' => $part3, + 'part_validation4' => $part4, + 'part_validation5' => $part5, + 'laser_part_validation1' => $laserPart1, + 'laser_part_validation2' => $laserPart2, + 'panel_box_code' => $panelCod, + 'load_rate' => $LoadRate, + 'material_type' => $MaterialType, + 'bundle_quantity' => $BundleQuantity, + 'created_by' => $createdBy, + 'updated_by' => $updatedBy, + ]); + return null; // return StickerMaster::firstOrNew([ // // Update existing records, matching them by `$this->data['column_name']` @@ -274,10 +366,10 @@ class StickerMasterImporter extends Importer public static function getCompletedNotificationBody(Import $import): string { - $body = 'Your sticker master import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; + $body = 'Your sticker master import has completed and '.number_format($import->successful_rows).' '.str('row')->plural($import->successful_rows).' imported.'; if ($failedRowsCount = $import->getFailedRowsCount()) { - $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.'; + $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.'; } return $body;