inventoryService = $inventoryService; $this->productService = $productService; } public function store(Request $request) { // 冪等性處理:若訂單已存在,回傳已建立的訂單資訊 $existingOrder = SalesOrder::where('external_order_id', $request->external_order_id)->first(); if ($existingOrder) { return response()->json([ 'message' => 'Order already exists', 'order_id' => $existingOrder->id, ], 200); } $request->validate([ 'external_order_id' => 'required|string|unique:sales_orders,external_order_id', 'warehouse' => 'nullable|string', 'warehouse_id' => 'nullable|integer', 'payment_method' => 'nullable|string|in:cash,credit_card,line_pay,ecpay,transfer,other', 'sold_at' => 'nullable|date', 'items' => 'required|array|min:1', 'items.*.pos_product_id' => 'required|string', 'items.*.qty' => 'required|numeric|min:0.0001', 'items.*.price' => 'required|numeric|min:0', ]); try { return DB::transaction(function () use ($request) { // 1. 建立訂單 $order = SalesOrder::create([ 'external_order_id' => $request->external_order_id, 'status' => 'completed', 'payment_method' => $request->payment_method ?? 'cash', 'total_amount' => 0, 'sold_at' => $request->sold_at ?? now(), 'raw_payload' => $request->all(), ]); // 2. 查找或建立倉庫 $warehouseId = $request->warehouse_id; if (empty($warehouseId)) { $warehouseName = $request->warehouse ?: '銷售倉庫'; $warehouse = $this->inventoryService->findOrCreateWarehouseByName($warehouseName); $warehouseId = $warehouse->id; } $totalAmount = 0; // 3. 處理訂單明細 foreach ($request->items as $itemData) { // 透過介面查找產品 $product = $this->productService->findByExternalPosId($itemData['pos_product_id']); if (!$product) { throw new \Exception( "Product not found for POS ID: " . $itemData['pos_product_id'] . ". Please sync product first." ); } $qty = $itemData['qty']; $price = $itemData['price']; $lineTotal = $qty * $price; $totalAmount += $lineTotal; // 建立訂單明細 SalesOrderItem::create([ 'sales_order_id' => $order->id, 'product_id' => $product->id, 'product_name' => $product->name, 'quantity' => $qty, 'price' => $price, 'total' => $lineTotal, ]); // 4. 扣除庫存(強制模式,允許負庫存) $this->inventoryService->decreaseStock( $product->id, $warehouseId, $qty, "POS Order: " . $order->external_order_id, true ); } $order->update(['total_amount' => $totalAmount]); return response()->json([ 'message' => 'Order synced and stock deducted successfully', 'order_id' => $order->id, ], 201); }); } catch (\Exception $e) { Log::error('Order Sync Failed', ['error' => $e->getMessage(), 'payload' => $request->all()]); return response()->json(['message' => 'Sync failed: ' . $e->getMessage()], 400); } } }