Files
pds/app/Filament/Imports/CheckPointNameImporter.php
dhanabalan 1e57326e8e
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s
Added plant code instead of plant name on import and export
2026-01-13 16:18:10 +05:30

94 lines
3.2 KiB
PHP

<?php
namespace App\Filament\Imports;
use App\Models\CheckPointName;
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 Filament\Facades\Filament;
use Str;
class CheckPointNameImporter extends Importer
{
protected static ?string $model = CheckPointName::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('name')
->requiredMapping()
->exampleHeader('Check Point Name')
->example('STP BACKSIDE')
->label('Check Point Name')
->rules(['required']),
ImportColumn::make('created_by')
->requiredMapping()
->exampleHeader('Created By')
->example(Filament::auth()->user()->name ?? 'Admin')
->label('Created By')
->rules(['required']),
];
}
public function resolveRecord(): ?CheckPointName
{
$warnMsg = [];
$plantCod = $this->data['plant'];
$plant = null;
if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
$warnMsg[] = 'Invalid plant code found';
} else {
$plant = Plant::where('code', $plantCod)->first();
if (! $plant) {
$warnMsg[] = 'Plant not found'; // '" . $plantCod . "'
}
}
if (Str::length($this->data['name']) < 3) { // || !ctype_alnum($this->data['name'])
$warnMsg[] = 'Invalid check point name found';
}
$createdBy = $this->data['created_by'];
if (Str::length($createdBy) < 3) { // || !ctype_alnum($createdBy)
$warnMsg[] = 'Invalid created by name found';
}
if (! empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
}
return CheckPointName::updateOrCreate([
'name' => $this->data['name'],
'plant_id' => $plant->id,
],
[
'created_by' => $this->data['created_by'],
]
);
// // return CheckPointName::firstOrNew([
// // // Update existing records, matching them by `$this->data['column_name']`
// // 'email' => $this->data['email'],
// // ]);
// return new CheckPointName();
}
public static function getCompletedNotificationBody(Import $import): string
{
$body = 'Your check point name 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;
}
}