Added validation in characteristic value importer #107

Merged
jothi merged 1 commits from ranjith-dev into master 2025-12-20 03:05:00 +00:00

View File

@@ -3,9 +3,16 @@
namespace App\Filament\Imports;
use App\Models\CharacteristicValue;
use App\Models\Item;
use App\Models\Line;
use App\Models\Machine;
use App\Models\Plant;
use App\Models\ProcessOrder;
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 CharacteristicValueImporter extends Importer
{
@@ -16,25 +23,52 @@ class CharacteristicValueImporter extends Importer
return [
ImportColumn::make('plant')
->requiredMapping()
->relationship()
->exampleHeader('Plant Code')
->example('1000')
->label('Plant Code')
->relationship(resolveUsing: 'code')
->rules(['required']),
ImportColumn::make('line')
->requiredMapping()
->relationship()
->exampleHeader('Line Name')
->example('4 inch pump line')
->label('Line Name')
->relationship(resolveUsing: 'name')
->rules(['required']),
ImportColumn::make('item')
->requiredMapping()
->relationship()
->exampleHeader('Item Code')
->example('123456')
->label('Item Code')
->relationship(resolveUsing: 'code')
->rules(['required']),
ImportColumn::make('machine')
->requiredMapping()
->relationship()
->exampleHeader('Work Center')
->example('RMGS09745')
->label('Work Center')
->relationship(resolveUsing: 'work_center')
->rules(['required']),
ImportColumn::make('process_order'),
ImportColumn::make('coil_number'),
ImportColumn::make('status'),
ImportColumn::make('created_by'),
ImportColumn::make('updated_by'),
ImportColumn::make('process_order')
->requiredMapping()
->exampleHeader('Process Order')
->example('23455256352')
->label('Process Order'),
ImportColumn::make('coil_number')
->requiredMapping()
->exampleHeader('Coil Number')
->example('0')
->label('Coil Number'),
ImportColumn::make('status')
->requiredMapping()
->exampleHeader('Status')
->example('Ok')
->label('Status'),
ImportColumn::make('created_by')
->requiredMapping()
->exampleHeader('Created By')
->example('RAW01234')
->label('Created By'),
];
}
@@ -45,15 +79,180 @@ class CharacteristicValueImporter extends Importer
// 'email' => $this->data['email'],
// ]);
return new CharacteristicValue();
$warnMsg = [];
$plantId = null;
$itemId = null;
$LineId = null;
$machineId = null;
$itemAgainstPlant = null;
$plantCode = $this->data['plant'];
$processOrder = trim($this->data['process_order'] ?? '');
$iCode = trim($this->data['item']);
$workCenter = trim($this->data['machine']);
$lineName = trim($this->data['line']);
$status = trim($this->data['status']);
$createdBy = trim($this->data['created_by']);
$coilNo = trim($this->data['coil_number']);
if ($plantCode == null || $plantCode == '') {
$warnMsg[] = 'Plant code cannot be empty';
} elseif ($iCode == null || $iCode == '') {
$warnMsg[] = 'Process Order cannot be empty';
} elseif ($workCenter == null || $workCenter == '') {
$warnMsg[] = 'Work center cannot be empty';
} elseif ($lineName == null || $lineName == '') {
$warnMsg[] = 'Line cannot be empty';
}
if (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) {
$warnMsg[] = 'Invalid plant code found';
} else {
$plant = Plant::where('code', $plantCode)->first();
if (! $plant) {
$warnMsg[] = 'Plant not found';
} else {
$plantId = $plant->id;
}
}
if (Str::length($iCode) < 6 || ! ctype_alnum($iCode)) {
$warnMsg[] = 'Invalid item code found';
} else {
$itemCode = Item::where('code', $iCode)->first();
if (! $itemCode) {
$warnMsg[] = 'Item code not found';
} else {
if ($plantId) {
$itemAgainstPlant = Item::where('code', $iCode)->where('plant_id', $plantId)->first();
if (! $itemAgainstPlant) {
$warnMsg[] = 'Item code not found for the given plant';
} else {
$itemId = $itemAgainstPlant->id;
}
}
}
}
$lineExists = Line::where('name', $lineName)->first();
if (! $lineExists) {
$warnMsg[] = 'Line name not found';
} else {
if ($plantId) {
$lineAgainstPlant = Line::where('name', $lineName)->where('plant_id', $plantId)->first();
if (! $lineAgainstPlant) {
$warnMsg[] = 'Line name not found for the given plant';
} else {
$LineId = $lineAgainstPlant->id;
}
}
}
$workCenterExist = Machine::where('work_center', $workCenter)->first();
if (! $workCenterExist) {
$warnMsg[] = 'Work Center not found';
}
// $workCenterAgainstPlant = Machine::where('work_center', $workCenter)
// ->where('plant_id', $plantId)
// ->first();
// if (!$workCenterAgainstPlant) {
// $warnMsg[] = 'Work center not found for the given plant';
// } else {
// $MachineId = $workCenterAgainstPlant->id;
// }
if ($plantId != null && $LineId != null) {
$machineAgaPlantLine = Machine::where('plant_id', $plantId)
->where('line_id', $LineId)
->where('work_center', $workCenter)
->first();
if (! $machineAgaPlantLine) {
$warnMsg[] = 'Work center not found for the given plant and line';
} else {
$machineId = $machineAgaPlantLine->id;
}
}
if ($processOrder == null || $processOrder == '') {
$warnMsg[] = 'Process Order cannot be empty';
}
if ($coilNo == null || $coilNo == '') {
$warnMsg[] = 'Coil No cannot be empty';
} elseif (! is_numeric($coilNo)) {
$warnMsg[] = 'Coil number should contain only numeric values!';
}
if ($status == null || $status == '' || ! $status) {
$warnMsg[] = 'Status cannot be empty';
} elseif (! in_array($status, ['Ok', 'NotOk'], true)) {
$warnMsg[] = "Status must be either 'Ok' or 'NotOk'!";
}
if ($createdBy == null || $createdBy == '' || ! $createdBy) {
$warnMsg[] = 'Created By cannot be empty';
}
// $existing = CharacteristicValue::where('plant_id', $plantId)
// ->where('process_order', $processOrder)
// ->where('coil_number', $coilNo)
// ->first();
// if ($existing) {
// $warnMsg[] = "Process order '{$processOrder}' with coil number '{$coilNo}' already exist for the plant code '{$plantCode}'!";
// }
if ($plantId && $processOrder) {
$existing = CharacteristicValue::where('plant_id', $plantId)
->where('process_order', $processOrder)
->where('coil_number', $coilNo)
->first();
if ($existing) {
$warnMsg[] = "Coil number '{$coilNo}' already exists for Plant '{$plantCode}' and Process Order '{$processOrder}'.";
}
}
if ($plant && $itemCode && $processOrder != '') {
$existingOrder = ProcessOrder::where('plant_id', $plant->id)
->where('process_order', $processOrder)
->first();
if ($existingOrder && $existingOrder->item_id !== ($itemCode->id ?? null)) {
$warnMsg[] = 'Same Process Order already exists for this Plant with a different Item Code';
}
}
if (! empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
}
return CharacteristicValue::create([
'plant_id' => $plantId,
'item_id' => $itemId,
'line_id' => $LineId,
'machine_id' => $machineId,
'process_order' => trim($this->data['process_order']),
'coil_number' => trim($this->data['coil_number']),
'status' => trim($this->data['status']),
'created_by' => trim($this->data['created_by']),
]);
// return null;
// return new CharacteristicValue;
}
public static function getCompletedNotificationBody(Import $import): string
{
$body = 'Your characteristic value import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.';
$body = 'Your characteristic value 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.';
$body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.';
}
return $body;