73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\LineStop;
|
|
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 LineStopImporter extends Importer
|
|
{
|
|
protected static ?string $model = LineStop::class;
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
ImportColumn::make('code')
|
|
->requiredMapping()
|
|
->exampleHeader('Line Stop Code')
|
|
->example('A1R')
|
|
->label('Line Stop Code')
|
|
->rules(['required']),
|
|
ImportColumn::make('reason')
|
|
->requiredMapping()
|
|
->exampleHeader('Line Stop Reason')
|
|
->example('Power Shut Down')
|
|
->label('Line Stop Reason')
|
|
->rules(['required']),
|
|
];
|
|
}
|
|
|
|
public function resolveRecord(): ?LineStop
|
|
{
|
|
$warnMsg = [];
|
|
if (Str::length($this->data['code']) < 3 || !ctype_alnum($this->data['code'])) {
|
|
$warnMsg[] = "Invalid line stop code found";
|
|
}
|
|
if (Str::length($this->data['reason']) < 5) {
|
|
$warnMsg[] = "Invalid line stop reason found";
|
|
}
|
|
if (!empty($warnMsg)) {
|
|
throw new RowImportFailedException(implode(', ', $warnMsg));
|
|
}
|
|
return LineStop::updateOrCreate([
|
|
'code' => $this->data['code']
|
|
],
|
|
[
|
|
'reason' => $this->data['reason']
|
|
]
|
|
);
|
|
// return LineStop::firstOrNew([
|
|
// // Update existing records, matching them by `$this->data['column_name']`
|
|
// 'email' => $this->data['email'],
|
|
// ]);
|
|
|
|
// return new LineStop();
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = 'Your line stop 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;
|
|
}
|
|
}
|