feat: 倉庫業務屬性、庫存成本追蹤與採購單功能更新
1. 倉庫管理:新增業務類型 (Owned/External/Customer) 與車牌資訊與司機欄位。 2. 庫存管理:實作成本追蹤 (unit_cost, total_value),更新列表與撥補單顯示。 3. 採購單:新增採購日期 (order_date),調整欄位名稱與順序。 4. 前端優化:更新相關 TS Type 定義與 UI 顯示。
This commit is contained in:
@@ -105,16 +105,28 @@ class InventoryService implements InventoryServiceInterface
|
||||
$inventory = Inventory::lockForUpdate()->find($inventory->id);
|
||||
$balanceBefore = $inventory->quantity;
|
||||
|
||||
// 加權平均成本計算 (可選,這裡先採簡單邏輯:若有新成本則更新,否則沿用)
|
||||
// 若本次入庫有指定成本,則更新該批次單價 (假設同批號成本相同)
|
||||
if (isset($data['unit_cost'])) {
|
||||
$inventory->unit_cost = $data['unit_cost'];
|
||||
}
|
||||
|
||||
$inventory->quantity += $data['quantity'];
|
||||
// 更新總價值
|
||||
$inventory->total_value = $inventory->quantity * $inventory->unit_cost;
|
||||
|
||||
// 更新其他可能變更的欄位 (如最後入庫日)
|
||||
$inventory->arrival_date = $data['arrival_date'] ?? $inventory->arrival_date;
|
||||
$inventory->save();
|
||||
} else {
|
||||
// 若不存在,則建立新紀錄
|
||||
$unitCost = $data['unit_cost'] ?? 0;
|
||||
$inventory = Inventory::create([
|
||||
'warehouse_id' => $data['warehouse_id'],
|
||||
'product_id' => $data['product_id'],
|
||||
'quantity' => $data['quantity'],
|
||||
'unit_cost' => $unitCost,
|
||||
'total_value' => $data['quantity'] * $unitCost,
|
||||
'batch_number' => $data['batch_number'] ?? null,
|
||||
'box_number' => $data['box_number'] ?? null,
|
||||
'origin_country' => $data['origin_country'] ?? 'TW',
|
||||
@@ -129,6 +141,7 @@ class InventoryService implements InventoryServiceInterface
|
||||
'inventory_id' => $inventory->id,
|
||||
'type' => '入庫',
|
||||
'quantity' => $data['quantity'],
|
||||
'unit_cost' => $inventory->unit_cost, // 記錄當下成本
|
||||
'balance_before' => $balanceBefore,
|
||||
'balance_after' => $inventory->quantity,
|
||||
'reason' => $data['reason'] ?? '手動入庫',
|
||||
@@ -148,13 +161,17 @@ class InventoryService implements InventoryServiceInterface
|
||||
$inventory = Inventory::lockForUpdate()->findOrFail($inventoryId);
|
||||
$balanceBefore = $inventory->quantity;
|
||||
|
||||
$inventory->decrement('quantity', $quantity);
|
||||
$inventory->decrement('quantity', $quantity); // decrement 不會自動觸發 total_value 更新
|
||||
// 需要手動更新總價值
|
||||
$inventory->refresh();
|
||||
$inventory->total_value = $inventory->quantity * $inventory->unit_cost;
|
||||
$inventory->save();
|
||||
|
||||
\App\Modules\Inventory\Models\InventoryTransaction::create([
|
||||
'inventory_id' => $inventory->id,
|
||||
'type' => '出庫',
|
||||
'quantity' => -$quantity,
|
||||
'unit_cost' => $inventory->unit_cost, // 記錄出庫時的成本
|
||||
'balance_before' => $balanceBefore,
|
||||
'balance_after' => $inventory->quantity,
|
||||
'reason' => $reason ?? '庫存扣減',
|
||||
|
||||
Reference in New Issue
Block a user