From 3845972c5cb789a51381a7efdb67ce4b37c10522e56a7815bc7ca7b472db5a83 Mon Sep 17 00:00:00 2001 From: Abhishek Mali Date: Mon, 1 Dec 2025 12:45:25 +0530 Subject: [PATCH] error fix --- .../Admin/AdminOrderController.php | 125 ++++++++++++++++++ ..._063142_add_deleted_at_to_orders_table.php | 26 ++++ 2 files changed, 151 insertions(+) create mode 100644 database/migrations/2025_12_01_063142_add_deleted_at_to_orders_table.php diff --git a/app/Http/Controllers/Admin/AdminOrderController.php b/app/Http/Controllers/Admin/AdminOrderController.php index ce86dfc..15799d4 100644 --- a/app/Http/Controllers/Admin/AdminOrderController.php +++ b/app/Http/Controllers/Admin/AdminOrderController.php @@ -420,4 +420,129 @@ public function downloadExcel(Request $request) return Excel::download(new OrdersExport($request), 'orders-report-' . date('Y-m-d') . '.xlsx'); } +public function addTempItem(Request $request) + { + // Validate item fields + $item = $request->validate([ + 'mark_no' => 'required', + 'origin' => 'required', + 'destination' => 'required', + 'description' => 'required|string', + 'ctn' => 'nullable|numeric', + 'qty' => 'nullable|numeric', + 'ttl_qty' => 'nullable|numeric', + 'unit' => 'nullable|string', + 'price' => 'nullable|numeric', + 'ttl_amount' => 'nullable|numeric', + 'cbm' => 'nullable|numeric', + 'ttl_cbm' => 'nullable|numeric', + 'kg' => 'nullable|numeric', + 'ttl_kg' => 'nullable|numeric', + 'shop_no' => 'nullable|string', + ]); + + // ❌ Prevent changing mark_no once first item added + if (session()->has('mark_no') && session('mark_no') != $request->mark_no) { + return redirect()->to(route('admin.orders.index') . '#createOrderForm') + ->with('error', 'You must finish or clear the current order before changing Mark No.'); + } + + // Save mark, origin, destination ONLY ONCE + if (!session()->has('mark_no')) { + session([ + 'mark_no' => $request->mark_no, + 'origin' => $request->origin, + 'destination' => $request->destination + ]); + } + + // ❌ DO NOT overwrite these values again + // session(['mark_no' => $request->mark_no]); + // session(['origin' => $request->origin]); + // session(['destination' => $request->destination]); + + // Add new sub-item into session + session()->push('temp_order_items', $item); + + return redirect()->to(route('admin.orders.index') . '#createOrderForm') + ->with('success', 'Item added.'); + } + +public function finishOrder(Request $request) + { + $request->validate([ + 'mark_no' => 'required', + 'origin' => 'required', + 'destination' => 'required', + ]); + + $items = session('temp_order_items', []); + + if (empty($items)) { + return redirect()->to(route('admin.orders.index') . '#createOrderForm') + ->with('error', 'Add at least one item before finishing.'); + } + + // Generate Order ID + $year = date('y'); + $prefix = "KNT-$year-"; + + $lastOrder = Order::latest('id')->first(); + $nextNumber = $lastOrder ? intval(substr($lastOrder->order_id, -8)) + 1 : 1; + + $orderId = $prefix . str_pad($nextNumber, 8, '0', STR_PAD_LEFT); + + // TOTAL SUMS + $total_ctn = array_sum(array_column($items, 'ctn')); + $total_qty = array_sum(array_column($items, 'qty')); + $total_ttl_qty = array_sum(array_column($items, 'ttl_qty')); + $total_amount = array_sum(array_column($items, 'ttl_amount')); + $total_cbm = array_sum(array_column($items, 'cbm')); + $total_ttl_cbm = array_sum(array_column($items, 'ttl_cbm')); + $total_kg = array_sum(array_column($items, 'kg')); + $total_ttl_kg = array_sum(array_column($items, 'ttl_kg')); + + // CREATE ORDER + $order = Order::create([ + 'order_id' => $orderId, + 'mark_no' => $request->mark_no, + 'origin' => $request->origin, + 'destination' => $request->destination, + 'ctn' => $total_ctn, + 'qty' => $total_qty, + 'ttl_qty' => $total_ttl_qty, + 'ttl_amount' => $total_amount, + 'cbm' => $total_cbm, + 'ttl_cbm' => $total_ttl_cbm, + 'kg' => $total_kg, + 'ttl_kg' => $total_ttl_kg, + 'status' => 'pending', + ]); + + // SAVE ALL SUB-ITEMS + foreach ($items as $item) { + OrderItem::create([ + 'order_id' => $order->id, + 'description' => $item['description'], + 'ctn' => $item['ctn'], + 'qty' => $item['qty'], + 'ttl_qty' => $item['ttl_qty'], + 'unit' => $item['unit'], + 'price' => $item['price'], + 'ttl_amount' => $item['ttl_amount'], + 'cbm' => $item['cbm'], + 'ttl_cbm' => $item['ttl_cbm'], + 'kg' => $item['kg'], + 'ttl_kg' => $item['ttl_kg'], + 'shop_no' => $item['shop_no'], + ]); + } + + // CLEAR TEMP DATA + session()->forget(['temp_order_items', 'mark_no', 'origin', 'destination']); + + return redirect()->route('admin.orders.index') + ->with('success', 'Order saved successfully.'); + } + } diff --git a/database/migrations/2025_12_01_063142_add_deleted_at_to_orders_table.php b/database/migrations/2025_12_01_063142_add_deleted_at_to_orders_table.php new file mode 100644 index 0000000..656e68d --- /dev/null +++ b/database/migrations/2025_12_01_063142_add_deleted_at_to_orders_table.php @@ -0,0 +1,26 @@ +softDeletes(); // creates deleted_at column + }); + } + + public function down() + { + Schema::table('orders', function (Blueprint $table) { + $table->dropColumn('deleted_at'); + }); + } + +};