[REFACTOR] 統一訂單同步 API 錯誤回應與修正 Linter 警告

This commit is contained in:
2026-03-19 14:07:32 +08:00
parent e3ceedc579
commit 0b4aeacb55
15 changed files with 1173 additions and 108 deletions

View File

@@ -58,29 +58,35 @@ class SyncOrderAction
];
}
// --- 預檢 (Pre-flight check) N+1 優化 ---
// --- 預檢 (Pre-flight check) 僅使用 product_id ---
$items = $data['items'];
$posProductIds = array_column($items, 'pos_product_id');
$targetErpIds = array_column($items, 'product_id');
// 一次性查出所有相關的 Product
$products = $this->productService->findByExternalPosIds($posProductIds)->keyBy('external_pos_id');
$productsById = $this->productService->findByIds($targetErpIds)->keyBy('id');
$resolvedProducts = [];
$missingIds = [];
foreach ($posProductIds as $id) {
if (!$products->has($id)) {
$missingIds[] = $id;
foreach ($items as $index => $item) {
$productId = $item['product_id'];
$product = $productsById->get($productId);
if ($product) {
$resolvedProducts[$index] = $product;
} else {
$missingIds[] = $productId;
}
}
if (!empty($missingIds)) {
// 回報所有缺漏的 ID
throw ValidationException::withMessages([
'items' => ["The following products are not found: " . implode(', ', $missingIds) . ". Please sync products first."]
'items' => ["The following product IDs are not found: " . implode(', ', array_unique($missingIds)) . ". Please ensure these products exist in the system."]
]);
}
// --- 執行寫入交易 ---
$result = DB::transaction(function () use ($data, $items, $products) {
$result = DB::transaction(function () use ($data, $items, $resolvedProducts) {
// 1. 建立訂單
$order = SalesOrder::create([
'external_order_id' => $data['external_order_id'],
@@ -108,11 +114,12 @@ class SyncOrderAction
// 3. 處理訂單明細
$orderItemsData = [];
foreach ($items as $itemData) {
$product = $products->get($itemData['pos_product_id']);
foreach ($items as $index => $itemData) {
$product = $resolvedProducts[$index];
$qty = $itemData['qty'];
$price = $itemData['price'];
$batchNumber = $itemData['batch_number'] ?? null;
$lineTotal = $qty * $price;
$totalAmount += $lineTotal;
@@ -134,9 +141,10 @@ class SyncOrderAction
$qty,
"POS Order: " . $order->external_order_id,
true,
null,
null, // Slot (location)
\App\Modules\Integration\Models\SalesOrder::class,
$order->id
$order->id,
$batchNumber
);
}

View File

@@ -21,10 +21,13 @@ class InventorySyncController extends Controller
* @param string $warehouseCode
* @return JsonResponse
*/
public function show(string $warehouseCode): JsonResponse
public function show(\Illuminate\Http\Request $request, string $warehouseCode): JsonResponse
{
// 透過 Service 調用跨模組庫存查詢功能
$inventoryData = $this->inventoryService->getPosInventoryByWarehouseCode($warehouseCode);
// 透過 Service 調用跨模組庫存查詢功能,傳入篩選條件
$inventoryData = $this->inventoryService->getPosInventoryByWarehouseCode(
$warehouseCode,
$request->only(['product_id', 'barcode', 'code', 'external_pos_id'])
);
// 若回傳 null表示尋無此倉庫代碼
if (is_null($inventoryData)) {
@@ -40,9 +43,18 @@ class InventorySyncController extends Controller
'warehouse_code' => $warehouseCode,
'data' => $inventoryData->map(function ($item) {
return [
'product_id' => $item->product_id,
'external_pos_id' => $item->external_pos_id,
'product_code' => $item->product_code,
'product_name' => $item->product_name,
'barcode' => $item->barcode,
'category_name' => $item->category_name ?? '未分類',
'unit_name' => $item->unit_name ?? '個',
'price' => (float) $item->price,
'brand' => $item->brand,
'specification' => $item->specification,
'batch_number' => $item->batch_number,
'expiry_date' => $item->expiry_date,
'quantity' => (float) $item->total_quantity,
];
})

View File

@@ -23,8 +23,8 @@ class ProductSyncController extends Controller
'name' => 'required|string|max:255',
'price' => 'nullable|numeric|min:0|max:99999999.99',
'barcode' => 'nullable|string|max:100',
'category' => 'nullable|string|max:100',
'unit' => 'nullable|string|max:100',
'category' => 'required|string|max:100',
'unit' => 'required|string|max:100',
'brand' => 'nullable|string|max:100',
'specification' => 'nullable|string|max:255',
'cost_price' => 'nullable|numeric|min:0|max:99999999.99',
@@ -41,6 +41,8 @@ class ProductSyncController extends Controller
'data' => [
'id' => $product->id,
'external_pos_id' => $product->external_pos_id,
'code' => $product->code,
'barcode' => $product->barcode,
]
]);
} catch (\Exception $e) {
@@ -50,4 +52,63 @@ class ProductSyncController extends Controller
], 500);
}
}
/**
* 搜尋商品(供外部 API 使用)。
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
$request->validate([
'product_id' => 'nullable|integer',
'external_pos_id' => 'nullable|string|max:255',
'barcode' => 'nullable|string|max:100',
'code' => 'nullable|string|max:100',
'category' => 'nullable|string|max:100',
'updated_after' => 'nullable|date',
'per_page' => 'nullable|integer|min:1|max:100',
]);
try {
$perPage = $request->input('per_page', 50);
$products = $this->productService->searchProducts($request->all(), $perPage);
return response()->json([
'status' => 'success',
'data' => $products->getCollection()->map(function ($product) {
return [
'id' => $product->id,
'code' => $product->code,
'barcode' => $product->barcode,
'name' => $product->name,
'external_pos_id' => $product->external_pos_id,
'category_name' => $product->category?->name ?? '未分類',
'brand' => $product->brand,
'specification' => $product->specification,
'unit_name' => $product->baseUnit?->name ?? '個',
'price' => (float) $product->price,
'cost_price' => (float) $product->cost_price,
'member_price' => (float) $product->member_price,
'wholesale_price' => (float) $product->wholesale_price,
'is_active' => (bool) $product->is_active,
'updated_at' => $product->updated_at->format('Y-m-d H:i:s'),
];
}),
'meta' => [
'current_page' => $products->currentPage(),
'last_page' => $products->lastPage(),
'per_page' => $products->perPage(),
'total' => $products->total(),
]
]);
} catch (\Exception $e) {
Log::error('Product Search Failed', ['error' => $e->getMessage()]);
return response()->json([
'status' => 'error',
'message' => 'Search failed: ' . $e->getMessage(),
], 500);
}
}
}

View File

@@ -27,7 +27,8 @@ class SyncOrderRequest extends FormRequest
'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.*.product_id' => 'required|integer',
'items.*.batch_number' => 'nullable|string',
'items.*.qty' => 'required|numeric|min:0.0001',
'items.*.price' => 'required|numeric|min:0',
];

View File

@@ -9,6 +9,7 @@ use App\Modules\Integration\Controllers\InventorySyncController;
Route::prefix('api/v1/integration')
->middleware(['api', 'throttle:integration', 'integration.tenant', 'auth:sanctum'])
->group(function () {
Route::get('products', [ProductSyncController::class, 'index']);
Route::post('products/upsert', [ProductSyncController::class, 'upsert']);
Route::post('orders', [OrderSyncController::class, 'store']);
Route::post('vending/orders', [VendingOrderSyncController::class, 'store']);