diff --git a/app/Filament/Imports/WorkGroupMasterImporter.php b/app/Filament/Imports/WorkGroupMasterImporter.php index 3c38a96..0581dd8 100644 --- a/app/Filament/Imports/WorkGroupMasterImporter.php +++ b/app/Filament/Imports/WorkGroupMasterImporter.php @@ -20,16 +20,16 @@ class WorkGroupMasterImporter extends Importer return [ 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') ->rules(['required']), ImportColumn::make('name') ->requiredMapping() - ->exampleHeader('Name') + ->exampleHeader('Group Work Center') ->example('RMGCEABC') - ->label('Name') + ->label('Group Work Center') ->rules(['required']), ImportColumn::make('description') ->requiredMapping() @@ -52,8 +52,6 @@ class WorkGroupMasterImporter extends Importer ]; } - - public function resolveRecord(): ?WorkGroupMaster { // return WorkGroupMaster::firstOrNew([ @@ -61,92 +59,105 @@ class WorkGroupMasterImporter extends Importer // 'email' => $this->data['email'], // ]); $warnMsg = []; - $plant = Plant::where('name', $this->data['plant'])->first(); - if (!$plant) { - $warnMsg[] = "Plant not found"; - } - - if (Str::length($this->data['name']) <= 0) { //|| !ctype_alnum($this->data['description']) - $warnMsg[] = "Invalid name found"; - } - - if (Str::length(trim($this->data['description'])) <= 0) { - $warnMsg[] = "Invalid description found"; - } - + $plantCod = $this->data['plant']; + $createdBy = $this->data['created_by']; + $groupWorkCenter = $this->data['name']; $desc = trim($this->data['description']); + $opNo = $this->data['operation_number']; + $plantId = null; + + if (! $plantCod || $plantCod == null || $plantCod == '') { + $warnMsg[] = "Plant code can't be empty!"; + } elseif (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) { + $warnMsg[] = "Invalid plant code '{$plantCod}' found!"; + } else { + $plant = Plant::where('code', $plantCod)->first(); + if (! $plant) { + $warnMsg[] = "Plant code '{$plantCod}' not found!"; + } else { + $plantId = $plant->id; + } + } + + if (Str::length($groupWorkCenter) <= 0) { // || !ctype_alnum($desc) + $warnMsg[] = 'Invalid group work center found!'; + } + + if (Str::length(trim($desc)) <= 0) { + $warnMsg[] = 'Invalid description found!'; + } if (Str::length($desc) > 44) { - $warnMsg[] = "Description should be less than 44 digits."; + $warnMsg[] = 'Description should be less than 44 digits.'; } - if (Str::length($this->data['operation_number']) <= 0) { - $warnMsg[] = "Invalid operation number found"; + if (Str::length($opNo) <= 0) { + $warnMsg[] = 'Invalid operation number found!'; } - if(!is_numeric($this->data['operation_number'])) - { - $warnMsg[] = "Invalid operation number found must be numeric"; + if (! is_numeric($opNo)) { + $warnMsg[] = 'Operation number must be numeric values only.'; } - $user = User::where('name', $this->data['created_by'])->first(); - if (!$user) { - $warnMsg[] = "Operator ID not found"; + $user = User::where('name', $createdBy)->first(); + if (! $user) { + $warnMsg[] = "Operator ID '{$createdBy}' not found!"; } - if (!empty($warnMsg)) { + if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); - } - else - { - //Check (plant_id, name) - $existingByName = WorkGroupMaster::where('plant_id', $plant->id) - ->where('name', $this->data['name']) - ->first(); + } else { + // Check (plant_id, name) + // $existingByName = WorkGroupMaster::where('plant_id', $plantId) + // ->where('name', $groupWorkCenter) + // ->first(); - if ($existingByName) { - throw new RowImportFailedException("Work group name already exists for this plant!"); - } + // if ($existingByName) { + // throw new RowImportFailedException('Group work center already exists for this plant!'); + // } - //Check (plant_id, operation_number) - $existingByOpNum = WorkGroupMaster::where('plant_id', $plant->id) - ->where('operation_number', $this->data['operation_number']) - ->where('name', $this->data['name']) - ->first(); + // Check (plant_id, operation_number) + // $existingByOpNum = WorkGroupMaster::where('plant_id', $plantId) + // ->where('operation_number', $opNo) + // ->where('name', $groupWorkCenter) + // ->first(); - if ($existingByOpNum) { - throw new RowImportFailedException("Operation number already exists for this plant!"); - } + // if ($existingByOpNum) { + // throw new RowImportFailedException('Operation number already exists for this plant!'); + // } - //Check (plant_id) - $existingByOperator = WorkGroupMaster::where('plant_id', $plant->id) - ->where('name', $this->data['name']) + // Check (plant_id) + $existingByOperator = WorkGroupMaster::whereNot('plant_id', $plantId) + ->where('name', $groupWorkCenter) ->first(); if ($existingByOperator) { - throw new RowImportFailedException("Already work group name assigned to another plant!"); + throw new RowImportFailedException('Already group work center assigned to another plant!'); } } WorkGroupMaster::updateOrCreate([ - 'plant_id' => $plant->id, - 'name' => $this->data['name'], - 'description' => $this->data['description'], - 'operation_number' => $this->data['operation_number'], - 'created_by' => $this->data['created_by'], - ]); + 'plant_id' => $plantId, + 'name' => $groupWorkCenter, + ], + [ + 'description' => $desc, + 'operation_number' => $opNo, + 'created_by' => $createdBy, + ] + ); return null; - //return new WorkGroupMaster(); + // return new WorkGroupMaster(); } public static function getCompletedNotificationBody(Import $import): string { - $body = 'Your work group master import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; + $body = 'Your work group master 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;