Added validation logic in process order importer
This commit is contained in:
@@ -2,10 +2,15 @@
|
||||
|
||||
namespace App\Filament\Imports;
|
||||
|
||||
use App\Models\Item;
|
||||
use App\Models\Plant;
|
||||
use App\Models\ProcessOrder;
|
||||
use App\Models\User;
|
||||
use Filament\Actions\Imports\ImportColumn;
|
||||
use Filament\Actions\Imports\Importer;
|
||||
use Filament\Actions\Imports\Models\Import;
|
||||
use Str;
|
||||
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||
|
||||
class ProcessOrderImporter extends Importer
|
||||
{
|
||||
@@ -58,12 +63,42 @@ class ProcessOrderImporter extends Importer
|
||||
|
||||
public function resolveRecord(): ?ProcessOrder
|
||||
{
|
||||
// return ProcessOrder::firstOrNew([
|
||||
// // Update existing records, matching them by `$this->data['column_name']`
|
||||
// 'email' => $this->data['email'],
|
||||
// ]);
|
||||
$warnMsg = [];
|
||||
$plant = Plant::where('name', $this->data['plant'])->first();
|
||||
$itemCode = Item::where('code', $this->data['item'])->first();
|
||||
$iCode = trim($this->data['item']);
|
||||
|
||||
return new ProcessOrder();
|
||||
if (!$plant) {
|
||||
$warnMsg[] = "Plant not found";
|
||||
}
|
||||
else if (Str::length($iCode) < 6 || !ctype_alnum($iCode)) {
|
||||
$warnMsg[] = "Invalid item code found";
|
||||
}
|
||||
else if(!$itemCode)
|
||||
{
|
||||
$warnMsg[] = "Item Code not found";
|
||||
}
|
||||
|
||||
$user = User::where('name', $this->data['created_by'])->first();
|
||||
if (!$user) {
|
||||
$warnMsg[] = "User not found";
|
||||
}
|
||||
|
||||
if (!empty($warnMsg)) {
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
}
|
||||
|
||||
return ProcessOrder::create([
|
||||
'plant_id' => $plant->id,
|
||||
'item_id' => $itemCode->id,
|
||||
'process_order' => trim($this->data['process_order']),
|
||||
'coil_number' => $this->data['coil_number'] ?? null,
|
||||
'order_quantity' => $this->data['order_quantity'],
|
||||
'received_quantity' => trim($this->data['received_quantity']),
|
||||
'created_by' => $user->name,
|
||||
]);
|
||||
|
||||
//return new ProcessOrder();
|
||||
}
|
||||
|
||||
public static function getCompletedNotificationBody(Import $import): string
|
||||
|
||||
Reference in New Issue
Block a user