Added gr master validation logic for import
This commit is contained in:
@@ -6,6 +6,11 @@ use App\Models\GrMaster;
|
||||
use Filament\Actions\Imports\ImportColumn;
|
||||
use Filament\Actions\Imports\Importer;
|
||||
use Filament\Actions\Imports\Models\Import;
|
||||
use App\Models\Plant;
|
||||
use App\Models\Item;
|
||||
use Str;
|
||||
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||
use App\Models\User;
|
||||
|
||||
class GrMasterImporter extends Importer
|
||||
{
|
||||
@@ -57,7 +62,58 @@ class GrMasterImporter extends Importer
|
||||
// 'email' => $this->data['email'],
|
||||
// ]);
|
||||
|
||||
return new GrMaster();
|
||||
$warnMsg = [];
|
||||
|
||||
$plant = Plant::where('name', $this->data['plant'])->first();
|
||||
|
||||
if (!$plant) {
|
||||
$warnMsg[] = "Plant not found";
|
||||
}
|
||||
|
||||
$item = null;
|
||||
if ($plant) {
|
||||
$item = Item::where('code', $this->data['item'])->where('plant_id', $plant->id)->first();
|
||||
}
|
||||
if (!$item) {
|
||||
$warnMsg[] = "Item not found";
|
||||
}
|
||||
if (Str::length($this->data['serial_number']) < 9 || !ctype_alnum($this->data['serial_number'])) {
|
||||
$warnMsg[] = "Invalid serial number found";
|
||||
}
|
||||
|
||||
if (empty($this->data['gr_number'])) {
|
||||
$warnMsg[] = "GR Number cannot be empty.";
|
||||
}
|
||||
|
||||
$user = User::where('name', $this->data['created_by'])->first();
|
||||
if (!$user) {
|
||||
$warnMsg[] = "User not found";
|
||||
}
|
||||
|
||||
if (!empty($warnMsg)) {
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
}
|
||||
else { //if (empty($warnMsg))
|
||||
$grMaster = GrMaster::where('plant_id', $plant->id)
|
||||
->where('serial_number', $this->data['serial_number'])
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
if ($grMaster) {
|
||||
throw new RowImportFailedException("Serial number already exist!");
|
||||
}
|
||||
}
|
||||
|
||||
GrMaster::updateOrCreate([
|
||||
'plant_id' => $plant->id,
|
||||
'item_id' => $item->id,
|
||||
'serial_number' => $this->data['serial_number'],
|
||||
'gr_number' => $this->data['gr_number'] ?? null,
|
||||
'created_by' => $this->data['created_by'],
|
||||
]);
|
||||
|
||||
return null;
|
||||
//return new GrMaster();
|
||||
}
|
||||
|
||||
public static function getCompletedNotificationBody(Import $import): string
|
||||
|
||||
Reference in New Issue
Block a user