Added work group center foreign key column to Machine export and import functionality with validation and error handling

This commit is contained in:
dhanabalan
2025-09-15 13:18:23 +05:30
parent 0381530463
commit 4bb3b60391
2 changed files with 48 additions and 10 deletions

View File

@@ -30,6 +30,8 @@ class MachineExporter extends Exporter
->label('MACHINE'), ->label('MACHINE'),
ExportColumn::make('work_center') ExportColumn::make('work_center')
->label('WORK CENTER'), ->label('WORK CENTER'),
ExportColumn::make('workGroupMaster.name')
->label('WORK GROUP CENTER'),
ExportColumn::make('created_at') ExportColumn::make('created_at')
->label('CREATED AT'), ->label('CREATED AT'),
ExportColumn::make('updated_at') ExportColumn::make('updated_at')

View File

@@ -5,6 +5,7 @@ namespace App\Filament\Imports;
use App\Models\Line; use App\Models\Line;
use App\Models\Machine; use App\Models\Machine;
use App\Models\Plant; use App\Models\Plant;
use App\Models\WorkGroupMaster;
use Filament\Actions\Imports\Exceptions\RowImportFailedException; use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Importer;
@@ -28,7 +29,15 @@ class MachineImporter extends Importer
->requiredMapping() ->requiredMapping()
->exampleHeader('Work Center') ->exampleHeader('Work Center')
->example('RMGCE001') ->example('RMGCE001')
->label('Work Center'), ->label('Work Center')
->rules(['required']),
ImportColumn::make('workGroupMaster')
->requiredMapping()
->relationship(resolveUsing: 'name')
->exampleHeader('Work Group Center')
->example(['RMGCGABC'])
->label('Work Group Center')
->rules(['required']),
ImportColumn::make('line') ImportColumn::make('line')
->requiredMapping() ->requiredMapping()
->relationship(resolveUsing: 'name') ->relationship(resolveUsing: 'name')
@@ -53,26 +62,52 @@ class MachineImporter extends Importer
$line = null; $line = null;
$machine = $this->data['name']; $machine = $this->data['name'];
$workCenter = $this->data['work_center']; $workCenter = $this->data['work_center'];
$groupWorkCenter = WorkGroupMaster::where('name', $this->data['workGroupMaster'])->first();
if (!$plant) { if (!$plant) {
$warnMsg[] = "Plant not found"; $warnMsg[] = "Plant not found!";
} }
else { else {
$line = Line::where('name', $this->data['line'])->where('plant_id', $plant->id)->first(); $line = Line::where('name', $this->data['line'])->where('plant_id', $plant->id)->first();
if ($line) { if ($line) {
$grpWrkCnr = $line->group_work_center; $grpWrkCnr = $line->no_of_operation;
if (!$grpWrkCnr || Str::length($grpWrkCnr) < 1) if (!$grpWrkCnr || $grpWrkCnr < 1)//Str::length($grpWrkCnr) < 1)
{ {
$warnMsg[] = "Group work center line not found"; $warnMsg[] = "Group work center line not found!";
}
else if (!$groupWorkCenter) {
$warnMsg[] = "Group work center not found!";
}
else {
$dupMachine = Machine::where('plant_id', $plant->id)->where('work_center', '!=', $workCenter)->where('name', $machine)->first();
if ($dupMachine) {
$warnMsg[] = "Duplicate machine name found!";
}
else {
$isValidGroupWork = false;
for ($i = 1; $i <= $line->no_of_operation; $i++) {
$column = "work_group{$i}_id";
if (!empty($line->$column)) {
if ($line->$column == $groupWorkCenter->id) {
$isValidGroupWork = true;
break;
}
}
}
if (!$isValidGroupWork) {
$warnMsg[] = "Group work center does not match with line!";
}
}
} }
} }
else else
{ {
$warnMsg[] = "Line not found"; $warnMsg[] = "Line not found!";
} }
} }
if (Str::length($machine) <= 0) { if (Str::length($machine) <= 0) {
$warnMsg[] = "Machine name not found"; $warnMsg[] = "Machine name not found!";
} }
if (!empty($warnMsg)) { if (!empty($warnMsg)) {
@@ -81,12 +116,13 @@ class MachineImporter extends Importer
Machine::updateOrCreate( Machine::updateOrCreate(
[ [
'name' => $machine,
'plant_id' => $plant->id, 'plant_id' => $plant->id,
'line_id' => $line->id 'work_center' => $workCenter
], ],
[ [
'work_center' => $workCenter 'line_id' => $line->id,
'name' => $machine,
'work_group_master_id' => $groupWorkCenter->id
] ]
); );
return null; return null;