From f599fdc05b78f714ecbbe851ce6305434cd6f9d9 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Mon, 25 May 2026 15:02:55 +0530 Subject: [PATCH] Added asrs item validation importer and exporter --- .../Exports/AsrsItemValidationExporter.php | 66 +++++++++++ .../Imports/AsrsItemValidationImporter.php | 106 ++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 app/Filament/Exports/AsrsItemValidationExporter.php create mode 100644 app/Filament/Imports/AsrsItemValidationImporter.php diff --git a/app/Filament/Exports/AsrsItemValidationExporter.php b/app/Filament/Exports/AsrsItemValidationExporter.php new file mode 100644 index 0000000..7d9968f --- /dev/null +++ b/app/Filament/Exports/AsrsItemValidationExporter.php @@ -0,0 +1,66 @@ +label('NO') + ->state(function ($record) use (&$rowNumber) { + // Increment and return the row number + return ++$rowNumber; + }), + ExportColumn::make('plant.code') + ->label('PLANT'), + ExportColumn::make('item_code') + ->label('ITEM CODE'), + ExportColumn::make('item_description') + ->label('ITEM DESCRIPTION'), + ExportColumn::make('uom') + ->label('UOM'), + ExportColumn::make('mhe') + ->label('MHE'), + ExportColumn::make('bin_quantity') + ->label('BIN QUANTITY'), + ExportColumn::make('asrs') + ->label('ASRS'), + ExportColumn::make('asrs_category') + ->label('ASRS CATEGORY'), + ExportColumn::make('status') + ->label('STATUS'), + ExportColumn::make('created_at') + ->label('CREATED AT'), + ExportColumn::make('updated_at') + ->label('UPDATED AT'), + ExportColumn::make('created_by') + ->label('CREATED BY'), + ExportColumn::make('updated_by') + ->label('UPDATED BY'), + ExportColumn::make('deleted_at') + ->label('DELETED AT') + ->enabledByDefault(false), + ]; + } + + public static function getCompletedNotificationBody(Export $export): string + { + $body = 'Your asrs item validation export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; + + if ($failedRowsCount = $export->getFailedRowsCount()) { + $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.'; + } + + return $body; + } +} diff --git a/app/Filament/Imports/AsrsItemValidationImporter.php b/app/Filament/Imports/AsrsItemValidationImporter.php new file mode 100644 index 0000000..b2032d5 --- /dev/null +++ b/app/Filament/Imports/AsrsItemValidationImporter.php @@ -0,0 +1,106 @@ +requiredMapping() + ->exampleHeader('PLANT CODE') + ->example('1000') + ->label('PLANT CODE') + ->relationship(resolveUsing: 'code') + ->rules(['required']), + ImportColumn::make('item_code') + ->exampleHeader('ITEM CODE') + ->example('ITEM001') + ->label('ITEM CODE') + ->rules(['required']), + ImportColumn::make('item_description') + ->exampleHeader('ITEM DESCRIPTION') + ->example('Item Description') + ->label('ITEM DESCRIPTION'), + ImportColumn::make('uom') + ->exampleHeader('UOM') + ->example('EA') + ->label('UOM'), + ]; + } + + public function resolveRecord(): ?AsrsItemValidation + { + $warnMsg = []; + $plantCod = trim($this->data['plant']) ?? ''; + $iCode = trim($this->data['item_code']) ?? ''; + $itemDes = trim($this->data['item_description']) ?? ''; + $uom = trim($this->data['uom']) ?? ''; + $createdBy = Filament::auth()->user()?->name ?? ''; + $updatedBy = $createdBy; + + $plantId = null; + + if ($plantCod == null || $plantCod == '' || ! $plantCod) { + $warnMsg[] = "Plant code can't be empty!"; + } elseif (! is_numeric($plantCod)) { + $warnMsg[] = "Plant code '{$plantCod}' should contain only numeric values!"; + } elseif (Str::length($plantCod) < 4 || Str::length($plantCod) > 7) { + $warnMsg[] = "Plant code '{$plantCod}' must be between 4 and 7 digits only!"; + } elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCod)) { + $warnMsg[] = "Invalid plant code '{$plantCod}' found!"; + } else { + $plant = Plant::where('code', $plantCod)->first(); + if (! $plant) { + $warnMsg[] = 'Plant not found!'; + } else { + $plantId = $plant->id; + } + } + if (Str::length($iCode) < 6 || ! ctype_alnum($iCode)) { + $warnMsg[] = 'Invalid item code found'; + } + if (Str::length($itemDes) < 5) { + $warnMsg[] = 'Invalid description found'; + } + + if (! empty($warnMsg)) { + throw new RowImportFailedException(implode(', ', $warnMsg)); + } + + return AsrsItemValidation::updateOrCreate([ + 'item_code' => $iCode, + 'plant_id' => $plant->id, + ], + [ + 'item_description' => $itemDes, + 'uom' => trim($this->data['uom']), + 'created_by' => $createdBy, + 'updated_by' => $updatedBy, + ] + ); + } + + public static function getCompletedNotificationBody(Import $import): string + { + $body = 'Your asrs item validation 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.'; + } + + return $body; + } +}