Added asrs item validation importer and exporter
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
This commit is contained in:
106
app/Filament/Imports/AsrsItemValidationImporter.php
Normal file
106
app/Filament/Imports/AsrsItemValidationImporter.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Imports;
|
||||
|
||||
use App\Models\AsrsItemValidation;
|
||||
use App\Models\Plant;
|
||||
use Filament\Actions\Imports\ImportColumn;
|
||||
use Filament\Actions\Imports\Importer;
|
||||
use Filament\Actions\Imports\Models\Import;
|
||||
use Filament\Facades\Filament;
|
||||
use Str;
|
||||
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||
|
||||
class AsrsItemValidationImporter extends Importer
|
||||
{
|
||||
protected static ?string $model = AsrsItemValidation::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_code')
|
||||
->exampleHeader('ITEM CODE')
|
||||
->example('ITEM001')
|
||||
->label('ITEM CODE')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('item_description')
|
||||
->exampleHeader('ITEM DESCRIPTION')
|
||||
->example('Item Description')
|
||||
->label('ITEM DESCRIPTION'),
|
||||
ImportColumn::make('uom')
|
||||
->exampleHeader('UOM')
|
||||
->example('EA')
|
||||
->label('UOM'),
|
||||
];
|
||||
}
|
||||
|
||||
public function resolveRecord(): ?AsrsItemValidation
|
||||
{
|
||||
$warnMsg = [];
|
||||
$plantCod = trim($this->data['plant']) ?? '';
|
||||
$iCode = trim($this->data['item_code']) ?? '';
|
||||
$itemDes = trim($this->data['item_description']) ?? '';
|
||||
$uom = trim($this->data['uom']) ?? '';
|
||||
$createdBy = Filament::auth()->user()?->name ?? '';
|
||||
$updatedBy = $createdBy;
|
||||
|
||||
$plantId = null;
|
||||
|
||||
if ($plantCod == null || $plantCod == '' || ! $plantCod) {
|
||||
$warnMsg[] = "Plant code can't be empty!";
|
||||
} elseif (! is_numeric($plantCod)) {
|
||||
$warnMsg[] = "Plant code '{$plantCod}' should contain only numeric values!";
|
||||
} elseif (Str::length($plantCod) < 4 || Str::length($plantCod) > 7) {
|
||||
$warnMsg[] = "Plant code '{$plantCod}' must be between 4 and 7 digits only!";
|
||||
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCod)) {
|
||||
$warnMsg[] = "Invalid plant code '{$plantCod}' found!";
|
||||
} else {
|
||||
$plant = Plant::where('code', $plantCod)->first();
|
||||
if (! $plant) {
|
||||
$warnMsg[] = 'Plant not found!';
|
||||
} else {
|
||||
$plantId = $plant->id;
|
||||
}
|
||||
}
|
||||
if (Str::length($iCode) < 6 || ! ctype_alnum($iCode)) {
|
||||
$warnMsg[] = 'Invalid item code found';
|
||||
}
|
||||
if (Str::length($itemDes) < 5) {
|
||||
$warnMsg[] = 'Invalid description found';
|
||||
}
|
||||
|
||||
if (! empty($warnMsg)) {
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
}
|
||||
|
||||
return AsrsItemValidation::updateOrCreate([
|
||||
'item_code' => $iCode,
|
||||
'plant_id' => $plant->id,
|
||||
],
|
||||
[
|
||||
'item_description' => $itemDes,
|
||||
'uom' => trim($this->data['uom']),
|
||||
'created_by' => $createdBy,
|
||||
'updated_by' => $updatedBy,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public static function getCompletedNotificationBody(Import $import): string
|
||||
{
|
||||
$body = 'Your asrs item validation 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user