Added import validation functionality

This commit is contained in:
dhanabalan
2025-05-06 12:32:22 +05:30
parent 75f97e7a2b
commit f698909cd2

View File

@@ -2,10 +2,14 @@
namespace App\Filament\Imports;
use App\Models\Item;
use App\Models\Plant;
use App\Models\StickerMaster;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
use Str;
class StickerMasterImporter extends Importer
{
@@ -178,11 +182,69 @@ class StickerMasterImporter extends Importer
public function resolveRecord(): ?StickerMaster
{
$item = \App\Models\Item::where('code', $this->data['item'])->first();
$plant = \App\Models\Plant::where('name', $this->data['plant'])->first();
if (!$plant || !$item) {
// Optionally handle missing plant/block
return null;
$warnMsg = [];
$plant = Plant::where('name', $this->data['plant'])->first();
if (!$plant) {
$warnMsg[] = "Plant not found";
}
$item = Item::where('code', $this->data['item'])->where('plant_id', $plant->id)->first();
if (!$item) {
$warnMsg[] = "Item code not found";
}
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 (!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 (!empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
}
return StickerMaster::updateOrCreate([
'item_id' => $item->id,
@@ -211,14 +273,13 @@ class StickerMasterImporter extends Importer
'load_rate' => $this->data['load_rate'],
'bundle_quantity' => $this->data['bundle_quantity'],
'material_type' => $this->data['material_type']
]
);
]);
// return StickerMaster::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']`
// 'email' => $this->data['email'],
// ]);
return new StickerMaster();
// return new StickerMaster();
}
public static function getCompletedNotificationBody(Import $import): string