105 lines
3.3 KiB
PHP
105 lines
3.3 KiB
PHP
<?php
|
|
|
|
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
|
|
{
|
|
protected static ?string $model = ProcessOrder::class;
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
ImportColumn::make('plant')
|
|
->requiredMapping()
|
|
->exampleHeader('Plant Code')
|
|
->example('1000')
|
|
->label('Plant Code')
|
|
->relationship(resolveUsing:'code')
|
|
->rules(['required']),
|
|
ImportColumn::make('item')
|
|
->requiredMapping()
|
|
->exampleHeader('Item Code')
|
|
->example('123456')
|
|
->label('Item Code')
|
|
->relationship(resolveUsing: 'code')
|
|
->rules(['required']),
|
|
ImportColumn::make('process_order')
|
|
->exampleHeader('Process Order')
|
|
->example('200000166843')
|
|
->label('Process Order')
|
|
->rules(['required']),
|
|
ImportColumn::make('created_by')
|
|
->exampleHeader('Created By')
|
|
->example('RAW01234')
|
|
->label('Created By'),
|
|
];
|
|
}
|
|
|
|
public function resolveRecord(): ?ProcessOrder
|
|
{
|
|
$warnMsg = [];
|
|
$plant = Plant::where('code', $this->data['plant'])->first();
|
|
$itemCode = Item::where('code', $this->data['item'])->first();
|
|
$iCode = trim($this->data['item']);
|
|
|
|
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";
|
|
}
|
|
|
|
$processOrder = trim($this->data['process_order'] ?? '');
|
|
|
|
if ($processOrder == '') {
|
|
$warnMsg[] = "Process Order 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));
|
|
}
|
|
|
|
return ProcessOrder::create([
|
|
'plant_id' => $plant->id,
|
|
'item_id' => $itemCode->id,
|
|
'process_order' => trim($this->data['process_order']),
|
|
'coil_number' => "0",
|
|
'order_quantity' => 0,
|
|
'received_quantity' => 0,
|
|
'created_by' => $user->name,
|
|
]);
|
|
|
|
//return new ProcessOrder();
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = 'Your process order 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.';
|
|
}
|
|
|
|
return $body;
|
|
}
|
|
}
|