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'),
ExportColumn::make('work_center')
->label('WORK CENTER'),
ExportColumn::make('workGroupMaster.name')
->label('WORK GROUP CENTER'),
ExportColumn::make('created_at')
->label('CREATED AT'),
ExportColumn::make('updated_at')

View File

@@ -5,6 +5,7 @@ namespace App\Filament\Imports;
use App\Models\Line;
use App\Models\Machine;
use App\Models\Plant;
use App\Models\WorkGroupMaster;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
@@ -28,7 +29,15 @@ class MachineImporter extends Importer
->requiredMapping()
->exampleHeader('Work Center')
->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')
->requiredMapping()
->relationship(resolveUsing: 'name')
@@ -53,26 +62,52 @@ class MachineImporter extends Importer
$line = null;
$machine = $this->data['name'];
$workCenter = $this->data['work_center'];
$groupWorkCenter = WorkGroupMaster::where('name', $this->data['workGroupMaster'])->first();
if (!$plant) {
$warnMsg[] = "Plant not found";
$warnMsg[] = "Plant not found!";
}
else {
$line = Line::where('name', $this->data['line'])->where('plant_id', $plant->id)->first();
if ($line) {
$grpWrkCnr = $line->group_work_center;
if (!$grpWrkCnr || Str::length($grpWrkCnr) < 1)
$grpWrkCnr = $line->no_of_operation;
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
{
$warnMsg[] = "Line not found";
$warnMsg[] = "Line not found!";
}
}
if (Str::length($machine) <= 0) {
$warnMsg[] = "Machine name not found";
$warnMsg[] = "Machine name not found!";
}
if (!empty($warnMsg)) {
@@ -81,12 +116,13 @@ class MachineImporter extends Importer
Machine::updateOrCreate(
[
'name' => $machine,
'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;