Updated validation logic for Product Characteristics Master import
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 11s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 12s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 17s
Laravel Pint / pint (pull_request) Successful in 3m17s
Laravel Larastan / larastan (pull_request) Failing after 3m37s
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 11s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 12s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 17s
Laravel Pint / pint (pull_request) Successful in 3m17s
Laravel Larastan / larastan (pull_request) Failing after 3m37s
This commit is contained in:
@@ -24,75 +24,77 @@ class ProductCharacteristicsMasterImporter extends Importer
|
||||
{
|
||||
return [
|
||||
ImportColumn::make('plant')
|
||||
->label('PLANT CODE')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Plant Code')
|
||||
->example('1000')
|
||||
->label('Plant Code')
|
||||
->exampleHeader('PLANT CODE')
|
||||
->example(['1000', '1000'])
|
||||
->relationship(resolveUsing: 'code')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('item')
|
||||
->label('ITEM CODE')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Item Code')
|
||||
->example('630214')
|
||||
->label('Item Code')
|
||||
->exampleHeader('ITEM CODE')
|
||||
->example(['123456', '123456'])
|
||||
->relationship(resolveUsing: 'code')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('line')
|
||||
->label('LINE NAME')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Line Name')
|
||||
->example('4 inch pump line')
|
||||
->label('Line Name')
|
||||
->exampleHeader('LINE NAME')
|
||||
->example(['4 inch pump line', '4 inch pump line'])
|
||||
->relationship(resolveUsing: 'name')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('work_group_master_id')
|
||||
->label('Group Work Center')
|
||||
->label('GROUP WORK CENTER')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Group Work Center')
|
||||
->example('RMGCGABC')
|
||||
->exampleHeader('GROUP WORK CENTER')
|
||||
->example(['RMGSTR01', 'RMGSTR01'])
|
||||
->relationship(resolveUsing: 'name')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('machine')
|
||||
->label('WORK CENTER')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Work Center')
|
||||
->example('RMGCE001')
|
||||
->label('Work Center')
|
||||
->exampleHeader('WORK CENTER')
|
||||
->example(['RMISTR01', 'RMISTR02'])
|
||||
->relationship(resolveUsing: 'work_center')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('characteristics_type')
|
||||
->exampleHeader('Characteristics Type')
|
||||
->example('Process or Product')
|
||||
->label('Characteristics Type')
|
||||
->label('CHARACTERISTICS TYPE')
|
||||
->requiredMapping()
|
||||
->exampleHeader('CHARACTERISTICS TYPE')
|
||||
->example(['Product', 'Process'])
|
||||
->rules(['required']),
|
||||
ImportColumn::make('name')
|
||||
->exampleHeader('Characteristics Name')
|
||||
->example('Body')
|
||||
->label('Characteristics Name')
|
||||
->label('CHARACTERISTICS NAME')
|
||||
->requiredMapping()
|
||||
->exampleHeader('CHARACTERISTICS NAME')
|
||||
->example(['TEST01', 'TEST02'])
|
||||
->rules(['required']),
|
||||
ImportColumn::make('inspection_type')
|
||||
->exampleHeader('Inspection Type')
|
||||
->example('Visual or Value')
|
||||
->label('Inspection Type')
|
||||
->label('INSPECTION TYPE')
|
||||
->requiredMapping()
|
||||
->exampleHeader('INSPECTION TYPE')
|
||||
->example(['Value', 'Visual'])
|
||||
->rules(['required']),
|
||||
ImportColumn::make('lower')
|
||||
->exampleHeader('Lower')
|
||||
->example('0')
|
||||
->label('Lower')
|
||||
->rules(['numeric']),
|
||||
->label('LOWER')
|
||||
->requiredMapping()
|
||||
->exampleHeader('LOWER')
|
||||
->example(['5', '0'])
|
||||
->rules(['required']),
|
||||
ImportColumn::make('middle')
|
||||
->exampleHeader('Middle')
|
||||
->example('1')
|
||||
->label('Middle')
|
||||
->numeric()
|
||||
->rules(['numeric']),
|
||||
->label('MIDDLE')
|
||||
->requiredMapping()
|
||||
->exampleHeader('MIDDLE')
|
||||
->example(['10', '0'])
|
||||
->rules(['required']),
|
||||
ImportColumn::make('upper')
|
||||
->exampleHeader('Upper')
|
||||
->example('2')
|
||||
->label('Upper')
|
||||
->rules(['numeric']),
|
||||
ImportColumn::make('created_by')
|
||||
->exampleHeader('Created By')
|
||||
->example('Admin')
|
||||
->label('Created By'),
|
||||
->label('UPPER')
|
||||
->requiredMapping()
|
||||
->exampleHeader('UPPER')
|
||||
->example(['15', '0'])
|
||||
->rules(['required']),
|
||||
// ImportColumn::make('created_by'),
|
||||
// ImportColumn::make('updated_by'),
|
||||
|
||||
];
|
||||
@@ -102,59 +104,106 @@ class ProductCharacteristicsMasterImporter extends Importer
|
||||
{
|
||||
|
||||
$warnMsg = [];
|
||||
$plantCod = $this->data['plant'];
|
||||
$updatedBy = Filament::auth()->user()->name; // ?? 'Admin'
|
||||
$lower = null;
|
||||
$middle = null;
|
||||
$upper = null;
|
||||
$plantCod = trim($this->data['plant']) ?? null;
|
||||
$itemCod = trim($this->data['item']) ?? null;
|
||||
$lineNam = trim($this->data['line']) ?? null;
|
||||
$groupWorkCenter = trim($this->data['work_group_master_id']) ?? null;
|
||||
$workCenter = trim($this->data['machine']) ?? null;
|
||||
$charTyp = trim($this->data['characteristics_type']) ?? null;
|
||||
$charName = trim($this->data['name']) ?? null;
|
||||
$inspectTyp = trim($this->data['inspection_type']) ?? null;
|
||||
$lower = trim($this->data['lower']) ?? null;
|
||||
$middle = trim($this->data['middle']) ?? null;
|
||||
$upper = trim($this->data['upper']) ?? null;
|
||||
$createdBy = Filament::auth()->user()->name;
|
||||
$updatedBy = null;
|
||||
|
||||
$plantId = null;
|
||||
$name = trim($this->data['name']);
|
||||
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';
|
||||
} else {
|
||||
$plantId = $plant->id;
|
||||
$itemExists = Item::where('code', $this->data['item'])->first();
|
||||
if (! $itemExists) {
|
||||
$warnMsg[] = 'Item not found';
|
||||
$itemId = null;
|
||||
$lineId = null;
|
||||
$workGroupMasterId = null;
|
||||
$machineId = null;
|
||||
if ($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 found!';
|
||||
}
|
||||
if ($itemCod == null || $itemCod == '') {
|
||||
$warnMsg[] = "Item code can't be empty!";
|
||||
} elseif (Str::length($itemCod) < 6 || ! ctype_alnum($itemCod)) {
|
||||
$warnMsg[] = 'Invalid item code found!';
|
||||
}
|
||||
if ($lineNam == null || $lineNam == '') {
|
||||
$warnMsg[] = "Line name can't be empty!";
|
||||
}
|
||||
if ($groupWorkCenter == null || $groupWorkCenter == '') {
|
||||
$warnMsg[] = "Group work center can't be empty!";
|
||||
}
|
||||
if ($workCenter == null || $workCenter == '') {
|
||||
$warnMsg[] = "Work center can't be empty!";
|
||||
}
|
||||
if ($charTyp == null || $charTyp == '') {
|
||||
$warnMsg[] = "Characteristics type can't be empty!";
|
||||
}
|
||||
if ($charName == null || $charName == '') {
|
||||
$warnMsg[] = "Characteristics name can't be empty!";
|
||||
}
|
||||
if ($inspectTyp == null || $inspectTyp == '') {
|
||||
$warnMsg[] = "Inspection type can't be empty!";
|
||||
}
|
||||
if ($lower == null || $lower == '') {
|
||||
$warnMsg[] = "Lower value can't be empty!";
|
||||
}
|
||||
if ($middle == null || $middle == '') {
|
||||
$warnMsg[] = "Middle value can't be empty!";
|
||||
}
|
||||
if ($upper == null || $upper == '') {
|
||||
$warnMsg[] = "Upper value can't be empty!";
|
||||
}
|
||||
|
||||
$itemAgainstPlant = Item::where('code', $this->data['item'])
|
||||
->where('plant_id', $plantId)
|
||||
->first();
|
||||
if (! empty($warnMsg)) {
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
}
|
||||
|
||||
$plant = Plant::where('code', $plantCod)->first();
|
||||
if (! $plant) {
|
||||
$warnMsg[] = 'Plant code not found!';
|
||||
} else {
|
||||
$plantId = $plant->id;
|
||||
$itemExists = Item::where('code', $itemCod)->first();
|
||||
if (! $itemExists) {
|
||||
$warnMsg[] = 'Item code not found!';
|
||||
} else {
|
||||
$itemAgainstPlant = Item::where('code', $itemCod)->where('plant_id', $plantId)->first();
|
||||
if (! $itemAgainstPlant) {
|
||||
$warnMsg[] = 'Item code not found for the given plant';
|
||||
$warnMsg[] = 'Item code not found for the given plant!';
|
||||
} else {
|
||||
$itemId = $itemAgainstPlant->id;
|
||||
}
|
||||
}
|
||||
|
||||
$lineExists = Line::where('name', $this->data['line'])->first();
|
||||
$lineExists = Line::where('name', $lineNam)->first();
|
||||
if (! $lineExists) {
|
||||
$warnMsg[] = 'Line not found';
|
||||
}
|
||||
|
||||
$lineAgainstPlant = Line::where('name', $this->data['line'])
|
||||
->where('plant_id', $plantId)
|
||||
->first();
|
||||
|
||||
$warnMsg[] = 'Line name not found!';
|
||||
} else {
|
||||
$lineAgainstPlant = Line::where('name', $lineNam)->where('plant_id', $plantId)->first();
|
||||
if (! $lineAgainstPlant) {
|
||||
$warnMsg[] = 'Line not found for the given plant';
|
||||
$warnMsg[] = 'Line name not found for the given plant!';
|
||||
} else {
|
||||
$LineId = $lineAgainstPlant->id;
|
||||
}
|
||||
$lineId = $lineAgainstPlant->id;
|
||||
|
||||
$WorkgroupMaster = WorkGroupMaster::where('name', $this->data['work_group_master_id'])->where('plant_id', $plantId)->first();
|
||||
$WorkgroupMaster = WorkGroupMaster::where('name', $groupWorkCenter)->first();
|
||||
if (! $WorkgroupMaster) {
|
||||
$warnMsg[] = 'Work Group Master value not found';
|
||||
$warnMsg[] = 'Group work center not found!';
|
||||
} else {
|
||||
$WorkgroupMaster = WorkGroupMaster::where('name', $groupWorkCenter)->where('plant_id', $plantId)->first();
|
||||
if (! $WorkgroupMaster) {
|
||||
$warnMsg[] = 'Group work center not found for the given plant!';
|
||||
} else {
|
||||
|
||||
$workGroupMasterId = $WorkgroupMaster->id;
|
||||
|
||||
// 2. Now check if this WorkGroupMaster id exists in ANY of the 10 columns
|
||||
$existsInLine = Line::where('plant_id', $plantId)
|
||||
$existsInLine = Line::where('plant_id', $plantId)->where('id', $lineId)
|
||||
->where(function ($q) use ($workGroupMasterId) {
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$q->orWhere("work_group{$i}_id", $workGroupMasterId);
|
||||
@@ -163,56 +212,64 @@ class ProductCharacteristicsMasterImporter extends Importer
|
||||
->exists();
|
||||
|
||||
if (! $existsInLine) {
|
||||
$warnMsg[] = "Work Group Master '{$WorkgroupMaster->name}' is not mapped to any line in this plant";
|
||||
$workGroupMasterId = null;
|
||||
$warnMsg[] = "Group work center '{$WorkgroupMaster->name}' is not mapped for the given line!";
|
||||
} else {
|
||||
$workGroupMasterId = $WorkgroupMaster->id;
|
||||
}
|
||||
}
|
||||
|
||||
$machine = Machine::where('work_center', $this->data['machine'])->first();
|
||||
$machine = Machine::where('work_center', $workCenter)->first();
|
||||
if (! $machine) {
|
||||
$warnMsg[] = 'Work Center not found';
|
||||
$warnMsg[] = 'Work center not found!';
|
||||
} else {
|
||||
$machine = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->first();
|
||||
|
||||
if (! $machine) {
|
||||
$warnMsg[] = 'Work center not found for the given plant!';
|
||||
} else {
|
||||
$machine = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->where('line_id', $lineId)->first();
|
||||
|
||||
if (! $machine) {
|
||||
$warnMsg[] = 'Work center not found for the given line!';
|
||||
} else {
|
||||
$machine = Machine::where('work_center', $workCenter)->where('plant_id', $plantId)->where('line_id', $lineId)->where('work_group_master_id', $workGroupMasterId)->first();
|
||||
|
||||
if (! $machine) {
|
||||
$warnMsg[] = 'Work center not found for the given group work center!';
|
||||
} else {
|
||||
$machineId = $machine->id;
|
||||
}
|
||||
|
||||
$machineAgainstPlant = Machine::where('work_center', $this->data['machine'])
|
||||
->where('plant_id', $plantId)
|
||||
->first();
|
||||
|
||||
if (! $machineAgainstPlant) {
|
||||
$warnMsg[] = 'Work Center not found for the given plant';
|
||||
} else {
|
||||
$machineId = $machineAgainstPlant->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($name == null || $name == '') {
|
||||
$warnMsg[] = "Name can't be empty";
|
||||
}
|
||||
|
||||
$user = User::where('name', $this->data['created_by'])->first();
|
||||
$user = User::where('name', $createdBy)->first();
|
||||
if (! $user) {
|
||||
$warnMsg[] = 'Operator ID not found';
|
||||
$warnMsg[] = 'Created by user not found!';
|
||||
} else {
|
||||
$updatedBy = $createdBy;
|
||||
}
|
||||
|
||||
$updatedBy = Filament::auth()->user()->name; // ?? 'Admin'
|
||||
if (! $updatedBy) {
|
||||
$warnMsg[] = 'Invalid updated by user name found';
|
||||
}
|
||||
|
||||
if (($this->data['inspection_type'] ?? null) == 'Value') {
|
||||
$lower = $this->data['lower'] ?? null;
|
||||
$middle = $this->data['middle'] ?? null;
|
||||
$upper = $this->data['upper'] ?? null;
|
||||
|
||||
if ($inspectTyp == 'Value') {
|
||||
if (is_null($upper) || is_null($lower) || is_null($middle)) {
|
||||
$warnMsg[] = "For 'Value' inspection type, Upper, Lower, and Middle values are required.";
|
||||
$warnMsg[] = 'Upper, Lower, and Middle values are required.';
|
||||
} elseif (! is_numeric($upper) || ! is_numeric($lower) || ! is_numeric($middle)) {
|
||||
$warnMsg[] = 'Upper, Lower, and Middle values must be numeric.';
|
||||
} elseif (! ($lower <= $middle && $middle <= $upper)) {
|
||||
$warnMsg[] = "For 'Value' inspection type, values must satisfy: Lower ≤ Middle ≤ Upper.";
|
||||
} elseif ($lower == $upper) {
|
||||
if ($lower != $middle) {
|
||||
$warnMsg[] = "For 'Value' inspection type, values must satisfy: Lower = Middle = Upper.";
|
||||
}
|
||||
} elseif (! ($lower < $middle && $middle < $upper)) {
|
||||
$warnMsg[] = "For 'Value' inspection type, values must satisfy: Lower < Middle < Upper.";
|
||||
}
|
||||
} else {
|
||||
$lower = null;
|
||||
$middle = null;
|
||||
$upper = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,49 +277,33 @@ class ProductCharacteristicsMasterImporter extends Importer
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
}
|
||||
|
||||
// $record = ProductCharacteristicsMaster::firstOrNew([
|
||||
// 'plant_id' => $plantId,
|
||||
// 'item_id' => $itemId,
|
||||
// 'line_id' => $LineId,
|
||||
// 'work_group_master_id' => $workGroupMasterId,
|
||||
// 'machine_id' => $machineId,
|
||||
// ]);
|
||||
// // If record is new, fill all fields
|
||||
// if (! $record->exists) {
|
||||
// $record->name = $name;
|
||||
// $record->characteristics_type = $this->data['characteristics_type'];
|
||||
// $record->inspection_type = $this->data['inspection_type'];
|
||||
// $record->lower = $this->data['lower'] ?? null;
|
||||
// $record->upper = $this->data['upper'] ?? null;
|
||||
// $record->middle = $this->data['middle'] ?? null;
|
||||
// $record->created_by = $this->data['created_by'] ?? null;
|
||||
// $record->updated_by = $updatedBy ?? null;
|
||||
// } else {
|
||||
// // Record exists → update only updated_by and updated_at
|
||||
// $record->updated_by = $updatedBy ?? null;
|
||||
// $record->touch();
|
||||
// }
|
||||
// $record->save();
|
||||
if ($machineId) {
|
||||
$record = ProductCharacteristicsMaster::where('plant_id', $plantId)->where('line_id', $lineId)->where('work_group_master_id', $workGroupMasterId)->where('machine_id', $machineId)->where('item_id', $itemId)->first();
|
||||
// ->where('characteristics_type', $get('characteristics_type'))
|
||||
if ($record) {
|
||||
$createdBy = $record->created_by;
|
||||
}
|
||||
|
||||
ProductCharacteristicsMaster::updateOrCreate(
|
||||
[
|
||||
'plant_id' => $plantId,
|
||||
'item_id' => $itemId,
|
||||
'line_id' => $LineId,
|
||||
'line_id' => $lineId,
|
||||
'work_group_master_id' => $workGroupMasterId,
|
||||
'machine_id' => $machineId,
|
||||
// 'characteristics_type' => $charTyp,
|
||||
],
|
||||
[
|
||||
'name' => $name,
|
||||
'characteristics_type' => $this->data['characteristics_type'],
|
||||
'inspection_type' => $this->data['inspection_type'],
|
||||
'name' => $charName,
|
||||
'characteristics_type' => $charTyp,
|
||||
'inspection_type' => $inspectTyp,
|
||||
'lower' => $lower,
|
||||
'middle' => $middle,
|
||||
'upper' => $upper,
|
||||
// 'created_by' => user ?? $this->data['created_by'],
|
||||
'created_by' => $this->data['created_by'] ?? null,
|
||||
'updated_by' => $updatedBy ?? null,
|
||||
'created_by' => $createdBy,
|
||||
'updated_by' => $updatedBy,
|
||||
]);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user