[FIX] 修復所有 E2E 模組測試的標題定位器以及將測試帳號還原為 admin 權限
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s

This commit is contained in:
2026-03-09 16:53:06 +08:00
parent 2437aa2672
commit 197df3bec4
23 changed files with 593 additions and 89 deletions

View File

@@ -44,16 +44,23 @@ class AdjustService
);
// 2. 抓取有差異的明細 (diff_qty != 0)
$itemsToInsert = [];
foreach ($countDoc->items as $item) {
if (abs($item->diff_qty) < 0.0001) continue;
$adjDoc->items()->create([
$itemsToInsert[] = [
'inventory_adjust_doc_id' => $adjDoc->id,
'product_id' => $item->product_id,
'batch_number' => $item->batch_number,
'qty_before' => $item->system_qty,
'adjust_qty' => $item->diff_qty,
'notes' => "盤點差異: " . $item->diff_qty,
]);
'created_at' => now(),
'updated_at' => now(),
];
}
if (!empty($itemsToInsert)) {
InventoryAdjustItem::insert($itemsToInsert);
}
return $adjDoc;
@@ -84,25 +91,35 @@ class AdjustService
$doc->items()->delete();
$itemsToInsert = [];
$productIds = collect($itemsData)->pluck('product_id')->unique()->toArray();
$products = \App\Modules\Inventory\Models\Product::whereIn('id', $productIds)->get()->keyBy('id');
// 批次取得當前庫存
$inventories = Inventory::where('warehouse_id', $doc->warehouse_id)
->whereIn('product_id', $productIds)
->get();
foreach ($itemsData as $data) {
// 取得當前庫存作為 qty_before 參考 (僅參考,實際扣減以過帳當下為準)
$inventory = Inventory::where('warehouse_id', $doc->warehouse_id)
->where('product_id', $data['product_id'])
$inventory = $inventories->where('product_id', $data['product_id'])
->where('batch_number', $data['batch_number'] ?? null)
->first();
$qtyBefore = $inventory ? $inventory->quantity : 0;
$newItem = $doc->items()->create([
$itemsToInsert[] = [
'inventory_adjust_doc_id' => $doc->id,
'product_id' => $data['product_id'],
'batch_number' => $data['batch_number'] ?? null,
'qty_before' => $qtyBefore,
'adjust_qty' => $data['adjust_qty'],
'notes' => $data['notes'] ?? null,
]);
'created_at' => now(),
'updated_at' => now(),
];
// 更新日誌中的品項列表
$productName = \App\Modules\Inventory\Models\Product::find($data['product_id'])?->name;
$productName = $products->get($data['product_id'])?->name ?? '未知商品';
$found = false;
foreach ($updatedItems as $idx => $ui) {
if ($ui['product_name'] === $productName && $ui['new'] === null) {
@@ -126,6 +143,10 @@ class AdjustService
}
}
if (!empty($itemsToInsert)) {
InventoryAdjustItem::insert($itemsToInsert);
}
// 清理沒被更新到的舊品項 (即真正被刪除的)
$finalUpdatedItems = [];
foreach ($updatedItems as $ui) {
@@ -162,11 +183,20 @@ class AdjustService
foreach ($doc->items as $item) {
if ($item->adjust_qty == 0) continue;
$inventory = Inventory::firstOrNew([
// 補上 lockForUpdate() 防止併發衝突
$inventory = Inventory::where([
'warehouse_id' => $doc->warehouse_id,
'product_id' => $item->product_id,
'batch_number' => $item->batch_number,
]);
])->lockForUpdate()->first();
if (!$inventory) {
$inventory = new Inventory([
'warehouse_id' => $doc->warehouse_id,
'product_id' => $item->product_id,
'batch_number' => $item->batch_number,
]);
}
// 如果是新建立的 object (id 為空),需要初始化 default 並先行儲存
if (!$inventory->exists) {