From 94584d8270826d72153d0c2511aca168786f9e87 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sun, 20 Sep 2026 14:54:40 +0530 Subject: [PATCH] Added all teh process order logic --- app/Http/Controllers/PdfController.php | 410 ++++++++++++++++++++----- 1 file changed, 327 insertions(+), 83 deletions(-) diff --git a/app/Http/Controllers/PdfController.php b/app/Http/Controllers/PdfController.php index 25664ac..79de13d 100644 --- a/app/Http/Controllers/PdfController.php +++ b/app/Http/Controllers/PdfController.php @@ -2,9 +2,11 @@ namespace App\Http\Controllers; +use App\Models\CharacteristicValue; use App\Models\GrMaster; use App\Models\Item; use App\Models\ItemCharacteristic; +use App\Models\Line; use App\Models\Plant; use App\Models\ProcessOrder; use App\Models\StickerDetail; @@ -538,6 +540,8 @@ class PdfController extends Controller $processOrder = $request->header('process-order'); + // $machineName = $request->header('machine-name'); + if (! $plantCode) { return response()->json([ 'status_code' => 'ERROR', @@ -593,6 +597,13 @@ class PdfController extends Controller ->orderBy('id', 'desc') ->first(); + $lastRecord1 = ProcessOrder::with('item') + ->where('plant_id', $plant->id) + ->where('process_order', $processOrder) + ->where('rework_status', 0) + ->orderBy('id', 'desc') + ->first(); + $totalReceivedQty = $processOrderRecords->sum('received_quantity'); // $lastRecord = $processOrderRecords->first(); $item = $lastRecord->item; @@ -611,14 +622,22 @@ class PdfController extends Controller // ], 404); // } + $okQty = CharacteristicValue::where('plant_id', $plant->id)->where('process_order', $processOrder)->where('status', 'Ok')->get()->count(); + + $notOkQty = CharacteristicValue::where('plant_id', $plant->id)->where('process_order', $processOrder)->where('status', 'NotOk')->get()->count(); + + $currentCoil = ($lastRecord->rework_status == 1) ? (string) $lastRecord1->coil_number : (string) $lastRecord->coil_number; + return response()->json([ 'item_code' => $item?->code ?? '', 'description' => $item?->description ?? '', // 'coil_number' => $proOrdAgPlant->coil_number ?? "", // 'order_quantity' => (string)$proOrdAgPlant->order_quantity ?? "", - 'coil_number' => $lastRecord->coil_number ?? '', - 'order_quantity' => (string) $lastRecord->order_quantity ?? '', - 'received_quantity' => (string) $totalReceivedQty ?? '', + 'coil_number' => $currentCoil, + 'order_quantity' => (string) $lastRecord->updated_order_quantity ?? '0.000', + 'ok_quantity' => (string) $okQty ?? '0', + 'not_ok_quantity' => (string) $notOkQty ?? '0', + 'received_quantity' => (string) (($totalReceivedQty == 0) ? '0.000' : $totalReceivedQty) ?? '0.000', ]); } @@ -672,11 +691,14 @@ class PdfController extends Controller $plantId = $plant->id; $itemCode = $data['item_code'] ?? ''; + $lineName = $data['line_name'] ?? ''; $coilNo = $data['coil_number'] ?? ''; - $orderQty = $data['order_quantity'] ?? 0; + $updatedOrderQty = $data['order_quantity'] ?? 0; $receivedQty = $data['received_quantity'] ?? 0; + $scrapQty = $data['scrap_quantity'] ?? 0; $sfgNo = $data['sfg_number'] ?? ''; $machineId = $data['machine_id'] ?? ''; + $rework = $data['rework'] ?? ''; $createdBy = $data['created_by'] ?? ''; // $validated = $request->validate([ @@ -706,12 +728,53 @@ class PdfController extends Controller ], 404); } + if ($lineName == null || $lineName == '' || ! $lineName) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Line name can't be empty!", + ], 404); + } + + if ($scrapQty == null || $scrapQty == '') { + $scrapQty = 0; + } + + if ($rework == null || $rework == '' || ! $rework) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Rework value can't be empty!", + ], 404); + } + + $line = Line::where('name', $lineName)->first(); + + if (! $line) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Line name '{$lineName}' not found!", + ], 404); + } + + $lineNamePlant = Line::where('name', $lineName) + ->where('plant_id', $plantId) + ->first(); + + if (! $lineNamePlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Line name '{$lineName}' not found for the plant code '{$plantCode}'!", + ], 404); + } + + $lineNamePlantId = $lineNamePlant->id; + if ($coilNo == null || $coilNo == '') { return response()->json([ 'status_code' => 'ERROR', 'status_description' => "Coil number can't be empty!", ], 404); - } elseif (! is_numeric($coilNo) || Str::length($coilNo) <= 0 || ! preg_match('/^\d{1,}$/', $coilNo)) { // !ctype_digit($data['plant_code']) + } elseif (! is_numeric($coilNo) || Str::length($coilNo) <= 0 || ! preg_match('/^\d+(\.\d+)?$/', $coilNo) + ) { // !ctype_digit($data['plant_code']) return response()->json([ 'status_code' => 'ERROR', 'status_description' => 'Invalid coil number found!', @@ -811,58 +874,281 @@ class PdfController extends Controller ], 404); } - $existing = ProcessOrder::where('plant_id', $plantId) - ->where('process_order', $processOrder) - ->where('coil_number', $coilNo) - ->first(); - - if ($existing) { - return response()->json([ - 'status_code' => 'ERROR', - 'status_description' => "Process order '{$processOrder}' with coil number '{$coilNo}' already exist for the plant code '{$plantCode}'!", - ], 404); - } - $alreadyReceived = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) + ->where('machine_name', $machineId) ->where('item_id', $itemId) ->sum('received_quantity'); - if ($orderQty == 0) { - $orderQty = ProcessOrder::where('plant_id', $plantId) + $orderQty = 0; + if ($updatedOrderQty == 0) { + $orderExist = ProcessOrder::where('plant_id', $plantId) ->where('process_order', $processOrder) - ->where('item_id', $itemId) - ->value('order_quantity') ?? 0; + ->where('item_id', $itemId)->latest()->first(); + // ->value('order_quantity') ?? 0; + + if ($orderExist) { + $orderQty = $orderExist->order_quantity ?? 0; + $updatedOrderQty = $orderExist->updated_order_quantity ?? 0; + } else { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Order quantity can't be '0'", + ], 404); + } + } else { + $orderExist = ProcessOrder::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->where('item_id', $itemId)->latest()->first(); + // ->value('order_quantity') ?? 0; + + if ($orderExist) { + // $existOrderQty = $orderExist->updated_order_quantity ?? 0; + $orderQty = $orderExist->order_quantity ?? 0; + $existUpdatedOrdQuan = $orderExist->updated_order_quantity ?? 0; + + if ($existUpdatedOrdQuan != $updatedOrderQty) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Order quantity doesn't equal to exist process order '{$processOrder}'", + ], 404); + } + } } $total = $alreadyReceived + $receivedQty; - if ($total > $orderQty) { + if ($rework != 'Yes' && $rework != 'No') { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Received quantity should not exceed order quantity! Order Qty = {$orderQty}, Already Received Qty = {$alreadyReceived}, Trying to Insert Qty = {$receivedQty}", + 'status_description' => "Rework value should be either 'Yes' or 'No'!", ], 404); + } elseif ($rework == 'No') { + if ($scrapQty != 0) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Scrap Quanity value should be '0'!", + ], 404); + } } - try { - ProcessOrder::Create( - [ - 'plant_id' => $plantId, - 'process_order' => $processOrder, - 'item_id' => $itemId, - 'coil_number' => $coilNo, - 'order_quantity' => $orderQty, - 'received_quantity' => $receivedQty, - 'sfg_number' => $sfgNo, - 'machine_name' => $machineId, - 'created_by' => $createdBy, - ] - ); + $coilNo = (string) $coilNo; - return response()->json([ - 'status_code' => 'SUCCESS', - 'status_description' => 'Record Inserted Successfully', - ]); + try { + if ($rework == 'Yes') { + if ($coilNo && Str::contains($coilNo, '.')) { + // Contains dot + $coilMain = explode('.', $coilNo)[0]; // 333 + + $existingCoil = ProcessOrder::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->where('line_id', $lineNamePlantId) + ->where('coil_number', $coilMain) + // ->where('machine_name', $machineId) + ->first(); + + if (! $existingCoil) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Coil number '{$coilMain}' not exist against plant code '{$plantCode}' and process order '{$processOrder}'!", + ], 404); + } elseif ($existingCoil->rework_status == 0) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Rework coil number '{$coilMain}' not exist against plant code '{$plantCode}' and process order '{$processOrder}'!", + ], 404); + } + + if ($total > $updatedOrderQty) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Rework received quantity should not exceed exist order quantity! Exist Order Qty = '{$updatedOrderQty}', Already Exist Total Received Qty = '{$alreadyReceived}', Trying to Insert Rework Received Qty = '{$receivedQty}'", + ], 404); + } + + $existingCoil = ProcessOrder::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->where('line_id', $lineNamePlantId) + ->where('coil_number', $coilNo) + // ->where('machine_name', $machineId) + ->first(); + + if (! $existingCoil) { + ProcessOrder::Create( + [ + 'plant_id' => $plantId, + 'line_id' => $lineNamePlantId, + 'process_order' => $processOrder, + 'item_id' => $itemId, + 'coil_number' => $coilNo, + 'order_quantity' => $orderQty, + 'updated_order_quantity' => $updatedOrderQty, + 'received_quantity' => $receivedQty, + 'scrap_quantity' => $scrapQty, + 'sfg_number' => $sfgNo, + 'machine_name' => $machineId, + 'rework_status' => 1, + 'created_by' => $createdBy, + ] + ); + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => 'Record Updated Successfully (Rework).', + ]); + } else { + $updated = ProcessOrder::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->where('line_id', $lineNamePlantId) + ->where('coil_number', $coilNo) + ->update([ + // 'order_quantity' => $orderQty, + 'received_quantity' => $receivedQty, + 'scrap_quantity' => $scrapQty, + // 'sfg_number' => $sfgNo, + // 'machine_name' => $machineId, + 'rework_status' => 1, + 'updated_by' => $createdBy, + 'updated_at' => now(), + ]); + } + } else { + // if ($coilNo > 1) { + // $prevCoilNo = (string) ($coilNo - 1); + // $existPrevCoil = ProcessOrder::where('plant_id', $plantId) + // ->where('process_order', $processOrder) + // ->where('coil_number', $prevCoilNo) + // ->first(); + + // if (! $existPrevCoil) { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => "Previous coil number '{$prevCoilNo}' not exist against plant code '{$plantCode}' and process order '{$processOrder}'!", + // ], 404); + // } + // } + + $existingCoil = ProcessOrder::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->where('line_id', $lineNamePlantId) + ->where('coil_number', $coilNo) + ->first(); + + if (! $existingCoil) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Coil number '{$coilNo}' not exist against plant code '{$plantCode}' and process order '{$processOrder}'!", + ], 404); + } + + $existPrevCoil = ProcessOrder::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->where('item_id', $itemId) + ->where('coil_number', $coilNo) + ->first(); + + if ($existPrevCoil->received_quantity < $receivedQty) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Rework received quantity should not exceed exist received quantity! Exist Received Qty = '{$existPrevCoil->received_quantity}', Trying to Insert Rework Received Qty = '{$receivedQty}'", + ], 404); + } + + $updated = ProcessOrder::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->where('line_id', $lineNamePlantId) + ->where('coil_number', $coilNo) + ->where('machine_name', $machineId) + ->update([ + // 'order_quantity' => $orderQty, + 'received_quantity' => $receivedQty, + 'scrap_quantity' => $scrapQty, + // 'sfg_number' => $sfgNo, + // 'machine_name' => $machineId, + 'rework_status' => 1, + 'updated_by' => $createdBy, + 'updated_at' => now(), + ]); + } + + if ($updated == 0) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "No matching record found for rework coil number '{$coilNo}'!", + ], 404); + } else { + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => 'Record Updated Successfully (Rework).', + ]); + } + } else { + if ($coilNo && Str::contains($coilNo, '.')) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Rework value should be 'Yes' for rework coil number '{$coilNo}'!", + ], 404); + } + + $existing = ProcessOrder::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->where('coil_number', $coilNo) + ->where('machine_name', $machineId) + ->first(); + + if ($existing) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Coil number '{$coilNo}' already exist against plant code '{$plantCode}' and process order '{$processOrder}'!", + // "Process order '{$processOrder}' with coil number '{$coilNo}' already exist for the plant code '{$plantCode}'!", + ], 404); + } + + // if ($coilNo > 1) { + // $prevCoilNo = (string) ($coilNo - 1); + // $existPrevCoil = ProcessOrder::where('plant_id', $plantId) + // ->where('process_order', $processOrder) + // ->where('coil_number', $prevCoilNo) + // ->first(); + + // if (! $existPrevCoil) { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => "Previous coil number '{$prevCoilNo}' not exist against plant code '{$plantCode}' and process order '{$processOrder}'!", + // ], 404); + // } + // } + + if ($total > $updatedOrderQty) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Received quantity should not exceed exist order quantity! Exist Order Qty = '{$updatedOrderQty}', Already Exist Total Received Qty = '{$alreadyReceived}', Trying to Insert Received Qty = '{$receivedQty}'", + ], 404); + } + + ProcessOrder::Create( + [ + 'plant_id' => $plantId, + 'line_id' => $lineNamePlantId, + 'process_order' => $processOrder, + 'item_id' => $itemId, + 'coil_number' => $coilNo, + 'order_quantity' => $orderQty, + 'updated_order_quantity' => $updatedOrderQty, + 'received_quantity' => $receivedQty, + 'scrap_quantity' => $scrapQty, + 'sfg_number' => $sfgNo, + 'machine_name' => $machineId, + 'rework_status' => 0, + 'created_by' => $createdBy, + ] + ); + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => 'Record Inserted Successfully.', + ]); + } } catch (\Exception $e) { return response()->json([ 'status_code' => 'ERROR', @@ -1015,46 +1301,4 @@ class PdfController extends Controller // } // } - - public function generatePdf(Request $request, $stickerId) - { - $plantId = $request->query('plant_id'); - $itemCharacteristicId = $request->query('item_characteristic_id'); - $serialNumber = $request->query('serial_number'); - - if ($plantId && $itemCharacteristicId) { - $structure = StickerStructureDetail::findOrFail($stickerId); - - $dynamicElements = StickerDetail::where( - 'sticker_structure_detail_id', - $structure->id - )->where('element_type', 'Dynamic')->get(); - - $itemCharacteristic = ItemCharacteristic::where('plant_id', $plantId) - ->where('id', $itemCharacteristicId) - ->first(); - - // return (new StickerPdfService())->generatePdf1( - // $structure->sticker_id, - // $dynamicElements, - // $itemCharacteristic, - // $serialNumber - // ); - $pdfBytes = (new StickerPdfService())->generatePdf1( - $structure->sticker_id, - $dynamicElements, - $itemCharacteristic, - $serialNumber - ); - - - // Return as downloadable/streamed PDF - return response($pdfBytes, 200) - ->header('Content-Type', 'application/pdf') - ->header('Content-Disposition', 'inline; filename="sticker.pdf"'); - } - - return abort(404); - } - }