diff --git a/app/Filament/Imports/StickerPrintingImporter.php b/app/Filament/Imports/StickerPrintingImporter.php index 17bf171..e70e51d 100644 --- a/app/Filament/Imports/StickerPrintingImporter.php +++ b/app/Filament/Imports/StickerPrintingImporter.php @@ -2,15 +2,14 @@ namespace App\Filament\Imports; +use App\Models\Plant; use App\Models\StickerPrinting; +use Filament\Actions\Imports\Exceptions\RowImportFailedException; use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Models\Import; -use Filament\Actions\Imports\Exceptions\RowImportFailedException; -use App\Models\Plant; -use App\Models\User; -use Str; use Filament\Facades\Filament; +use Str; class StickerPrintingImporter extends Importer { @@ -49,33 +48,29 @@ class StickerPrintingImporter extends Importer // ]); $warnMsg = []; - $plant = Plant::where('code', $this->data['plant'])->first(); - - if (Str::length($this->data['serial_number']) < 9 || !ctype_alnum($this->data['serial_number'])) { - $warnMsg[] = "Invalid serial number found"; - } - - $existing = StickerPrinting::where('plant_id', $plant->id) - ->where('serial_number', $this->data['serial_number']) - ->first(); - - if ($existing) { - $warnMsg[] = "Serial number already exists for this plant!";//throw new RowImportFailedException("Serial number already exists for this plant!"); - } - + $plantCod = $this->data['plant']; + $plant = null; $serial = $this->data['serial_number']; - // --- Check duplicate in DB --- - $existsInDB = StickerPrinting::where('plant_id', $plant->id) - ->where('serial_number', $serial) - ->first(); - - if ($existsInDB) { - //throw new RowImportFailedException("Serial number '{$serial}' already exists in DB for this plant!"); - $warnMsg[] = "Serial number '{$serial}' already exists in DB for this plant!"; + if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) { + $warnMsg[] = 'Invalid plant code found'; + } else { + $plant = Plant::where('code', $plantCod)->first(); + if (! $plant) { + $warnMsg[] = 'Plant not found!'; + } } - if (!empty($warnMsg)) { + if (Str::length($serial) < 9 || ! ctype_alnum($serial)) { + $warnMsg[] = 'Invalid serial number found'; + } elseif ($plant) { + $existing = StickerPrinting::where('plant_id', $plant->id)->where('serial_number', $serial)->first(); + if ($existing) { + $warnMsg[] = "Serial number '{$serial}' already exists for plant '{$plantCod}'!"; // throw new RowImportFailedException("Serial number already exists for this plant!"); + } + } + + if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); } @@ -84,21 +79,21 @@ class StickerPrintingImporter extends Importer 'reference_number' => $this->data['reference_number'], 'serial_number' => $this->data['serial_number'], 'created_at' => now(), - 'updated_at' =>now(), + 'updated_at' => now(), 'created_by' => Filament::auth()->user()?->name, ]); return null; - //return new StickerPrinting(); + // return new StickerPrinting(); } public static function getCompletedNotificationBody(Import $import): string { - $body = 'Your sticker printing import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; + $body = 'Your sticker printing 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;