Changed plant name as plant code in item importer and exporter #101

Merged
jothi merged 1 commits from ranjith-dev into master 2025-12-12 11:39:06 +00:00
2 changed files with 31 additions and 20 deletions

View File

@@ -24,8 +24,8 @@ class ItemExporter extends Exporter
// Increment and return the row number
return ++$rowNumber;
}),
ExportColumn::make('plant.name')
->label('PLANT'),
ExportColumn::make('plant.code')
->label('PLANT CODE'),
ExportColumn::make('category')
->label('CATEGORY'),
ExportColumn::make('code')

View File

@@ -8,7 +8,8 @@ use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
use Str;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class ItemImporter extends Importer
{
@@ -48,10 +49,10 @@ class ItemImporter extends Importer
->label('Unit of Measure'),
ImportColumn::make('plant')
->requiredMapping()
->exampleHeader('Plant Name')
->example('Ransar Industries-I')
->label('Plant Name')
->relationship(resolveUsing: 'name')
->exampleHeader('Plant Code')
->example('1000')
->label('Plant Code')
->relationship(resolveUsing: 'code') // Lookup Plant by code column
->rules(['required']),
];
}
@@ -59,36 +60,46 @@ class ItemImporter extends Importer
public function resolveRecord(): ?Item
{
$warnMsg = [];
$plantCod = $this->data['plant'];
$plant = null;
Log::info('ResolveRecord triggered', $this->data);
$iCode = trim($this->data['code']);
$description = trim($this->data['description']);
$plant = Plant::where('name', $this->data['plant'])->first();
if (!$plant) {
$warnMsg[] = "Plant not found"; // '" . $this->data['plant'] . "'
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($iCode) < 6 || !ctype_alnum($iCode)) {
$warnMsg[] = "Invalid item code found";
if (Str::length($iCode) < 6 || ! ctype_alnum($iCode)) {
$warnMsg[] = 'Invalid item code found';
}
// if (Str::length($this->data['uom']) <= 0) {
// $warnMsg[] = "Invalid unit of measure found";
// }
if (Str::length($description) < 5) {
$warnMsg[] = "Invalid description found";
$warnMsg[] = 'Invalid description found';
}
if (Str::length($this->data['hourly_quantity']) < 0 || !is_numeric($this->data['hourly_quantity']) || $this->data['hourly_quantity'] <= 0) {
$warnMsg[] = "Invalid hourly quantity found";
if (Str::length($this->data['hourly_quantity']) < 0 || ! is_numeric($this->data['hourly_quantity']) || $this->data['hourly_quantity'] <= 0) {
$warnMsg[] = 'Invalid hourly quantity found';
}
if (!empty($warnMsg)) {
if (! empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
}
return Item::updateOrCreate([
'code' => $iCode,
'plant_id' => $plant->id
],
'code' => $iCode,
'plant_id' => $plant->id,
],
[
'category' => trim($this->data['category']),
'description' => $description,
'hourly_quantity' => $this->data['hourly_quantity'],
'uom' => trim($this->data['uom'])
'uom' => trim($this->data['uom']),
]
);
// return new Item;