72 lines
2.3 KiB
PHP
72 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\Item;
|
|
use Filament\Actions\Imports\ImportColumn;
|
|
use Filament\Actions\Imports\Importer;
|
|
use Filament\Actions\Imports\Models\Import;
|
|
|
|
class ItemImporter extends Importer
|
|
{
|
|
protected static ?string $model = Item::class;
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
ImportColumn::make('code')
|
|
->requiredMapping()
|
|
->exampleHeader('Item Code')
|
|
->example('123456')
|
|
->label('Item Code')
|
|
->rules(['required']),
|
|
ImportColumn::make('description')
|
|
->requiredMapping()
|
|
->exampleHeader('Description')
|
|
->example('Testing Model Description 1')
|
|
->label('Description')
|
|
->rules(['required']),
|
|
ImportColumn::make('hourly_quantity')
|
|
->requiredMapping()
|
|
->exampleHeader('Hourly Quantity')
|
|
->example('25')
|
|
->label('Hourly Quantity')
|
|
->numeric()
|
|
->rules(['required', 'integer']),
|
|
ImportColumn::make('plant')
|
|
->requiredMapping()
|
|
->exampleHeader('Plant Name')
|
|
->example('Ransar Industries-I')
|
|
->label('Plant Name')
|
|
->relationship(resolveUsing: 'name')
|
|
->rules(['required']),
|
|
];
|
|
}
|
|
|
|
public function resolveRecord(): ?Item
|
|
{
|
|
$plant = \App\Models\Plant::where('name', $this->data['plant'])->first();
|
|
return Item::updateOrCreate([
|
|
'code' => $this->data['code'],
|
|
'plant_id' => $plant->id
|
|
],
|
|
[
|
|
'description' => $this->data['description'],
|
|
'hourly_quantity' => $this->data['hourly_quantity']
|
|
]
|
|
);
|
|
// return new Item;
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = 'Your item 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;
|
|
}
|
|
}
|