Updated alignment and validation logic on import
This commit is contained in:
@@ -2,15 +2,14 @@
|
|||||||
|
|
||||||
namespace App\Filament\Imports;
|
namespace App\Filament\Imports;
|
||||||
|
|
||||||
|
use App\Models\Plant;
|
||||||
use App\Models\StickerPrinting;
|
use App\Models\StickerPrinting;
|
||||||
|
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||||
use Filament\Actions\Imports\ImportColumn;
|
use Filament\Actions\Imports\ImportColumn;
|
||||||
use Filament\Actions\Imports\Importer;
|
use Filament\Actions\Imports\Importer;
|
||||||
use Filament\Actions\Imports\Models\Import;
|
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 Filament\Facades\Filament;
|
||||||
|
use Str;
|
||||||
|
|
||||||
class StickerPrintingImporter extends Importer
|
class StickerPrintingImporter extends Importer
|
||||||
{
|
{
|
||||||
@@ -49,33 +48,29 @@ class StickerPrintingImporter extends Importer
|
|||||||
// ]);
|
// ]);
|
||||||
|
|
||||||
$warnMsg = [];
|
$warnMsg = [];
|
||||||
$plant = Plant::where('code', $this->data['plant'])->first();
|
$plantCod = $this->data['plant'];
|
||||||
|
$plant = null;
|
||||||
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!");
|
|
||||||
}
|
|
||||||
|
|
||||||
$serial = $this->data['serial_number'];
|
$serial = $this->data['serial_number'];
|
||||||
|
|
||||||
// --- Check duplicate in DB ---
|
if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
|
||||||
$existsInDB = StickerPrinting::where('plant_id', $plant->id)
|
$warnMsg[] = 'Invalid plant code found';
|
||||||
->where('serial_number', $serial)
|
} else {
|
||||||
->first();
|
$plant = Plant::where('code', $plantCod)->first();
|
||||||
|
if (! $plant) {
|
||||||
if ($existsInDB) {
|
$warnMsg[] = 'Plant not found!';
|
||||||
//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 (!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));
|
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,21 +79,21 @@ class StickerPrintingImporter extends Importer
|
|||||||
'reference_number' => $this->data['reference_number'],
|
'reference_number' => $this->data['reference_number'],
|
||||||
'serial_number' => $this->data['serial_number'],
|
'serial_number' => $this->data['serial_number'],
|
||||||
'created_at' => now(),
|
'created_at' => now(),
|
||||||
'updated_at' =>now(),
|
'updated_at' => now(),
|
||||||
'created_by' => Filament::auth()->user()?->name,
|
'created_by' => Filament::auth()->user()?->name,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
//return new StickerPrinting();
|
// return new StickerPrinting();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getCompletedNotificationBody(Import $import): string
|
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()) {
|
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;
|
return $body;
|
||||||
|
|||||||
Reference in New Issue
Block a user