Files
pds/app/Filament/Imports/LineImporter.php

91 lines
3.0 KiB
PHP

<?php
namespace App\Filament\Imports;
use App\Models\Line;
use App\Models\Plant;
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 LineImporter extends Importer
{
protected static ?string $model = Line::class;
public static function getColumns(): array
{
return [
ImportColumn::make('name')
->requiredMapping()
->exampleHeader('Line Name')
->example('4 inch pump line')
->label('Line Name')
->rules(['required']),
ImportColumn::make('type')
->requiredMapping()
->exampleHeader('Line Type')
->example('Domestic Assembly')
->label('Line Type')
->rules(['required']),
ImportColumn::make('group_work_center')
->requiredMapping()
->exampleHeader('Group Work Center')
->example('RMGCEABC')
->label('Group Work Center'),
ImportColumn::make('plant')
->requiredMapping()
->exampleHeader('Plant Name')
->example('Ransar Industries-I')
->label('Plant Name')
->relationship(resolveUsing:'name')
->rules(['required']),
];
}
public function resolveRecord(): ?Line
{
$warnMsg = [];
$plant = Plant::where('name', $this->data['plant'])->first();
if (!$plant) {
$warnMsg[] = "Plant '" . $this->data['plant'] . "' not found";
}
if (Str::length($this->data['name']) < 0) {
$warnMsg[] = "Line name not found";
}
if (Str::length($this->data['type']) < 0) {
$warnMsg[] = "Line type not found";
}
if (!empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
}
return Line::updateOrCreate([
'name' => $this->data['name'],
'plant_id' => $plant->id
],
[
'type' => $this->data['type'],
'group_work_center' => $this->data['group_work_center']
]
);
// return Line::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']`
// 'email' => $this->data['email'],
// ]);
// return new Line();
}
public static function getCompletedNotificationBody(Import $import): string
{
$body = 'Your line 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;
}
}