feat(inventory): 支援庫存新增不使用批號模式與自動累加邏輯

This commit is contained in:
2026-02-06 15:56:50 +08:00
parent 6c259859cf
commit 200d1989bd
3 changed files with 75 additions and 40 deletions

View File

@@ -163,7 +163,7 @@ class InventoryController extends Controller
'items.*.productId' => 'required|exists:products,id',
'items.*.quantity' => 'required|numeric|min:0.01',
'items.*.unit_cost' => 'nullable|numeric|min:0', // 新增成本驗證
'items.*.batchMode' => 'required|in:existing,new',
'items.*.batchMode' => 'required|in:existing,new,none',
'items.*.inventoryId' => 'required_if:items.*.batchMode,existing|nullable|exists:inventories,id',
'items.*.originCountry' => 'required_if:items.*.batchMode,new|nullable|string|max:2',
'items.*.expiryDate' => 'nullable|date',
@@ -188,6 +188,26 @@ class InventoryController extends Controller
if (isset($item['unit_cost'])) {
$inventory->unit_cost = $item['unit_cost'];
}
} elseif ($item['batchMode'] === 'none') {
// 模式 C不使用批號 (自動累加至 NO-BATCH)
$inventory = $warehouse->inventories()->withTrashed()->firstOrNew(
[
'product_id' => $item['productId'],
'batch_number' => 'NO-BATCH'
],
[
'quantity' => 0,
'unit_cost' => $item['unit_cost'] ?? 0,
'total_value' => 0,
'arrival_date' => $validated['inboundDate'],
'expiry_date' => null,
'origin_country' => 'TW',
]
);
if ($inventory->trashed()) {
$inventory->restore();
}
} else {
// 模式 B建立新批號
$originCountry = $item['originCountry'] ?? 'TW';