2026-02-06 11:56:29 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Modules\Integration\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use App\Modules\Integration\Models\SalesOrder;
|
|
|
|
|
use App\Modules\Integration\Models\SalesOrderItem;
|
2026-02-23 10:10:03 +08:00
|
|
|
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
|
|
|
|
|
use App\Modules\Inventory\Contracts\ProductServiceInterface;
|
2026-02-06 11:56:29 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
class OrderSyncController extends Controller
|
|
|
|
|
{
|
|
|
|
|
protected $inventoryService;
|
2026-02-23 10:10:03 +08:00
|
|
|
protected $productService;
|
2026-02-06 11:56:29 +08:00
|
|
|
|
2026-02-23 10:10:03 +08:00
|
|
|
public function __construct(
|
|
|
|
|
InventoryServiceInterface $inventoryService,
|
|
|
|
|
ProductServiceInterface $productService
|
|
|
|
|
) {
|
2026-02-06 11:56:29 +08:00
|
|
|
$this->inventoryService = $inventoryService;
|
2026-02-23 10:10:03 +08:00
|
|
|
$this->productService = $productService;
|
2026-02-06 11:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
2026-02-23 10:10:03 +08:00
|
|
|
// 冪等性處理:若訂單已存在,回傳已建立的訂單資訊
|
|
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 11:56:29 +08:00
|
|
|
$request->validate([
|
|
|
|
|
'external_order_id' => 'required|string|unique:sales_orders,external_order_id',
|
|
|
|
|
'warehouse' => 'nullable|string',
|
2026-02-23 10:10:03 +08:00
|
|
|
'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',
|
2026-02-06 11:56:29 +08:00
|
|
|
'items.*.pos_product_id' => 'required|string',
|
|
|
|
|
'items.*.qty' => 'required|numeric|min:0.0001',
|
2026-02-23 10:10:03 +08:00
|
|
|
'items.*.price' => 'required|numeric|min:0',
|
2026-02-06 11:56:29 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return DB::transaction(function () use ($request) {
|
2026-02-23 10:10:03 +08:00
|
|
|
// 1. 建立訂單
|
2026-02-06 11:56:29 +08:00
|
|
|
$order = SalesOrder::create([
|
|
|
|
|
'external_order_id' => $request->external_order_id,
|
|
|
|
|
'status' => 'completed',
|
|
|
|
|
'payment_method' => $request->payment_method ?? 'cash',
|
2026-02-23 10:10:03 +08:00
|
|
|
'total_amount' => 0,
|
2026-02-06 11:56:29 +08:00
|
|
|
'sold_at' => $request->sold_at ?? now(),
|
|
|
|
|
'raw_payload' => $request->all(),
|
|
|
|
|
]);
|
|
|
|
|
|
2026-02-23 10:10:03 +08:00
|
|
|
// 2. 查找或建立倉庫
|
2026-02-06 11:56:29 +08:00
|
|
|
$warehouseId = $request->warehouse_id;
|
2026-02-23 10:10:03 +08:00
|
|
|
|
2026-02-06 11:56:29 +08:00
|
|
|
if (empty($warehouseId)) {
|
|
|
|
|
$warehouseName = $request->warehouse ?: '銷售倉庫';
|
2026-02-23 10:10:03 +08:00
|
|
|
$warehouse = $this->inventoryService->findOrCreateWarehouseByName($warehouseName);
|
2026-02-06 11:56:29 +08:00
|
|
|
$warehouseId = $warehouse->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$totalAmount = 0;
|
|
|
|
|
|
2026-02-23 10:10:03 +08:00
|
|
|
// 3. 處理訂單明細
|
2026-02-06 11:56:29 +08:00
|
|
|
foreach ($request->items as $itemData) {
|
2026-02-23 10:10:03 +08:00
|
|
|
// 透過介面查找產品
|
|
|
|
|
$product = $this->productService->findByExternalPosId($itemData['pos_product_id']);
|
2026-02-06 11:56:29 +08:00
|
|
|
|
|
|
|
|
if (!$product) {
|
2026-02-23 10:10:03 +08:00
|
|
|
throw new \Exception(
|
|
|
|
|
"Product not found for POS ID: " . $itemData['pos_product_id'] . ". Please sync product first."
|
|
|
|
|
);
|
2026-02-06 11:56:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$qty = $itemData['qty'];
|
|
|
|
|
$price = $itemData['price'];
|
|
|
|
|
$lineTotal = $qty * $price;
|
|
|
|
|
$totalAmount += $lineTotal;
|
|
|
|
|
|
2026-02-23 10:10:03 +08:00
|
|
|
// 建立訂單明細
|
2026-02-06 11:56:29 +08:00
|
|
|
SalesOrderItem::create([
|
|
|
|
|
'sales_order_id' => $order->id,
|
|
|
|
|
'product_id' => $product->id,
|
2026-02-23 10:10:03 +08:00
|
|
|
'product_name' => $product->name,
|
2026-02-06 11:56:29 +08:00
|
|
|
'quantity' => $qty,
|
|
|
|
|
'price' => $price,
|
|
|
|
|
'total' => $lineTotal,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-02-23 10:10:03 +08:00
|
|
|
// 4. 扣除庫存(強制模式,允許負庫存)
|
2026-02-06 11:56:29 +08:00
|
|
|
$this->inventoryService->decreaseStock(
|
|
|
|
|
$product->id,
|
|
|
|
|
$warehouseId,
|
|
|
|
|
$qty,
|
|
|
|
|
"POS Order: " . $order->external_order_id,
|
2026-02-23 10:10:03 +08:00
|
|
|
true
|
2026-02-06 11:56:29 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|