diff --git a/app/Http/Controllers/CharacteristicsController.php b/app/Http/Controllers/CharacteristicsController.php index 570511f..02152c8 100644 --- a/app/Http/Controllers/CharacteristicsController.php +++ b/app/Http/Controllers/CharacteristicsController.php @@ -167,6 +167,7 @@ class CharacteristicsController extends Controller // ]; // return response()->json($response); + foreach ($characteristics as $char) { $line = Line::find($char->line_id); @@ -203,7 +204,6 @@ class CharacteristicsController extends Controller return response()->json([ 'items' => $items, ]); - } /** @@ -2231,62 +2231,89 @@ class CharacteristicsController extends Controller ], 404); } - $lineAgaPlant = Line::where('plant_id', $plantId)->where('name', $lineName)->first(); + $line = Line::where('plant_id', $plantId)->where('name', $lineName)->first(); - if (! $lineAgaPlant) { + if (! $line) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Line Name not found in lines table for the plant : '$plant->name'!", ], 404); } - $work = Machine::where('work_center', $workCenter)->first(); + $lineId = $line->id; - if (! $work) { + $machine = Machine::where('work_center', $workCenter)->first(); + + if (! $machine) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Work Center not found in machines table!', ], 404); } - $workAgaPlant = Machine::where('plant_id', $plantId)->where('work_center', $workCenter)->first(); + $machine = Machine::where('plant_id', $plantId)->where('work_center', $workCenter)->first(); - if (! $workAgaPlant) { + if (! $machine) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Work center not found in machines table for the plant : '$plant->name'!", ], 404); } + $machine = Machine::where('plant_id', $plantId)->where('line_id', $lineId)->where('work_center', $workCenter)->first(); + + if (! $machine) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Work center is not mapped for the given line!', + ], 404); + } + + $machineId = $machine->id; + // $description = $item ? $item->description : ''; // $uom = $item ? $item->uom : ''; // $category = $item ? $item->category : ''; - $charMaster = ProductCharacteristicsMaster::where('plant_id', $plantId)->where('item_id', $ItemId) - ->where('line_id', $lineAgaPlant->id)->where('machine_id', $workAgaPlant->id) - ->first(); + $charMasters = ProductCharacteristicsMaster::with('workGroupMaster')->where('plant_id', $plantId)->where('item_id', $ItemId)->where('line_id', $lineId)->where('machine_id', $machineId)->get(); // ->select(['name', 'characteristics_type', 'inspection_type', 'lower', 'middle', 'upper', 'work_group_master_id']) - if (! $charMaster) { + if (! $charMasters) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Characteristics not found in product master table for the plant : '$plant->name'!", ], 404); + } else { + $output = $charMasters->map(function ($charMast) { + $charMaster = [ + 'work_group_master' => optional($charMast->workGroupMaster)->name ?? '', + 'name' => $charMast?->name ?? '', + 'characteristics_type' => $charMast?->characteristics_type ?? '', + 'inspection_type' => $charMast?->inspection_type ?? '', + 'lower' => (string) $charMast?->lower ?? '', + 'middle' => (string) $charMast?->middle ?? '', + 'upper' => (string) $charMast?->upper ?? '', + ]; + + return $charMaster; + }); } - $workGroup = WorkGroupMaster::find($charMaster->work_group_master_id); - $workGroupName = $workGroup?->name ?? ''; + // $charMasters = ProductCharacteristicsMaster::with('workGroupMaster')->where('plant_id', $plantId)->where('item_id', $ItemId)->where('line_id', $lineId)->where('machine_id', $machineId)->first(); - $output = [ - 'work_group_master' => $workGroupName ?? '', - 'name' => $charMaster?->name ?? '', - 'characteristics_type' => $charMaster?->characteristics_type ?? '', - 'inspection_type' => $charMaster?->inspection_type ?? '', - 'lower' => (string) $charMaster?->lower ?? '', - 'middle' => (string) $charMaster?->middle ?? '', - 'upper' => (string) $charMaster?->upper ?? '', - ]; + // // $workGroup = WorkGroupMaster::find($charMasters->work_group_master_id); + // // $workGroupName = $workGroup?->name ?? ''; + + // $output = [ + // 'work_group_master' => $charMasters?->workGroupMaster->name ?? '', // $workGroupName ?? '', + // 'name' => $charMasters?->name ?? '', + // 'characteristics_type' => $charMasters?->characteristics_type ?? '', + // 'inspection_type' => $charMasters?->inspection_type ?? '', + // 'lower' => (string) $charMasters?->lower ?? '', + // 'middle' => (string) $charMasters?->middle ?? '', + // 'upper' => (string) $charMasters?->upper ?? '', + // ]; return response()->json($output, 200); } @@ -2347,15 +2374,15 @@ class CharacteristicsController extends Controller ], 404); } - $lineAgaPlant = Line::where('plant_id', $plantId)->where('name', $lineName)->first(); - if (! $lineAgaPlant) { + $line = Line::where('plant_id', $plantId)->where('name', $lineName)->first(); + if (! $line) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Line '{$lineName}' not found against plant code '$plantCode'!", ], 404); } - $lineId = $lineAgaPlant->id; + $lineId = $line->id; if ($itemCode == null || $itemCode == '') { return response()->json([ @@ -2372,15 +2399,15 @@ class CharacteristicsController extends Controller ], 404); } - $itemAgaPlant = Item::where('plant_id', $plantId)->where('code', $itemCode)->first(); - if (! $itemAgaPlant) { + $item = Item::where('plant_id', $plantId)->where('code', $itemCode)->first(); + if (! $item) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Item code '{$itemCode}' not found against plant code '$plantCode'!", ], 404); } - $itemId = $itemAgaPlant->id; + $itemId = $item->id; if ($workCenter == null || $workCenter == '') { return response()->json([ @@ -2397,23 +2424,23 @@ class CharacteristicsController extends Controller ], 404); } - $machineAgaPlant = Machine::where('plant_id', $plantId)->where('work_center', $workCenter)->first(); - if (! $machineAgaPlant) { + $machine = Machine::where('plant_id', $plantId)->where('work_center', $workCenter)->first(); + if (! $machine) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Work center '{$workCenter}' not found against plant code '$plantCode'!", ], 404); } - $machineAgaPlantLine = Machine::where('plant_id', $plantId)->where('line_id', $lineId)->where('work_center', $workCenter)->first(); - if (! $machineAgaPlantLine) { + $machine = Machine::where('plant_id', $plantId)->where('line_id', $lineId)->where('work_center', $workCenter)->first(); + if (! $machine) { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Work center '{$workCenter}' not found against plant code '$plantCode' and line name '$lineName'!", ], 404); } - $machineId = $machineAgaPlantLine->id; + $machineId = $machine->id; $data = $request->all();