diff --git a/app/Http/Controllers/PdfController.php b/app/Http/Controllers/PdfController.php index ff0c91b..197004d 100644 --- a/app/Http/Controllers/PdfController.php +++ b/app/Http/Controllers/PdfController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Models\GrMaster; use App\Models\Item; use App\Models\Plant; +use App\Models\ProcessOrder; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Response; @@ -529,6 +530,197 @@ class PdfController extends Controller return response()->json($result, 200); } + public function getProcessOrderData (Request $request) + { + $expectedUser = env('API_AUTH_USER'); + $expectedPw = env('API_AUTH_PW'); + $header_auth = $request->header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken != $header_auth) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $plantCode = $request->header('plant-code'); + + $processOrder = $request->header('process-order'); + + + if (!$plantCode) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant Code value can't be empty" + ], 404); + } + else if (!$processOrder) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Process Order cannot be empty!' + ], 403); + } + + $plant = Plant::where('code', $plantCode)->first(); + $plantId = $plant->id; + + if (!$plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant Code '{$plantCode}' not found!" + ], 404); + } + + $processOrderExist = ProcessOrder::where('process_order', $processOrder)->first(); + + if (!$processOrderExist) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Process order not found' + ], 404); + } + + $proOrdAgPlant = ProcessOrder::where('plant_id', $plantId) + ->where('process_order', $processOrder) + ->first(); + + if (!$proOrdAgPlant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Process order not found for this plant!' + ], 404); + } + + + $item = $proOrdAgPlant->item; + + $processOrderRecords = ProcessOrder::with('item') + ->where('plant_id', $plant->id) + ->where('process_order', $processOrder) + ->get(); + + $totalReceivedQty = $processOrderRecords->sum('received_quantity'); + $lastRecord = $processOrderRecords->first(); + $item = $lastRecord->item; + + 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 ?? "", + ]); + } + + public function storeProcessOrderData(Request $request) + { + $expectedUser = env('API_AUTH_USER'); + $expectedPw = env('API_AUTH_PW'); + $headerAuth = $request->header('Authorization'); + $expectedToken = "Bearer " . $expectedUser . ':' . $expectedPw; + + if ($headerAuth !== $expectedToken) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $plantCode = $request->header('plant-code'); + $processOrder = $request->header('process-order'); + + + if (!$plantCode || !$processOrder) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'plant-code and process-order are required' + ], 400); + } + + $plant = Plant::where('code', $plantCode)->first(); + if (!$plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid plant code' + ], 404); + } + + $plantId = $plant->id; + + $validated = $request->validate([ + 'item_code' => 'nullable|integer', + 'coil_number' => 'nullable|string', + 'order_quantity' => 'nullable|integer', + 'received_quantity' => 'nullable|integer', + 'created_by' => 'nullable|string', + ]); + + $item = Item::where('code', $validated['item_code']) + ->where('plant_id', $plantId) + ->first(); + + if (!$item) + { + return response()->json([ + 'message' => 'Invalid item_code for this plant' + ], 404); + } + + $createdBy = $validated['created_by'] ?? null; + + $user = User::where('name', $createdBy)->first(); + + if (!$user) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "User '{$createdBy}' not found" + ], 404); + } + + $existing = ProcessOrder::where('plant_id', $plant->id) + ->where('process_order', $processOrder) + ->where('item_id', '!=', $item->id) + ->first(); + + if ($existing) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' already has item_code '{$existing->item->code}'" + ], 409); + } + + try + { + ProcessOrder::Create( + [ + 'plant_id' => $plant->id, + 'process_order' => $processOrder, + 'item_id' => $item->id, + 'coil_number' => $validated['coil_number'] ?? "", + 'order_quantity' => $validated['order_quantity'] ?? 0, + 'received_quantity' => $validated['received_quantity'] ?? 0, + 'created_by' => $validated['created_by'] ?? "", + ] + ); + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => 'Record Inserted Successfully' + ]); + } catch (\Exception $e) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => $e->getMessage() + ], 500); + } + } + + /** * Display the specified resource.