修正庫存報表分頁參數衝突導致明細顯示為空的問題
This commit is contained in:
@@ -385,10 +385,11 @@ class InventoryService implements InventoryServiceInterface
|
||||
})
|
||||
->count();
|
||||
|
||||
// 4. 即將過期明細數
|
||||
// 4. 即將過期明細數 (必須排除已過期)
|
||||
$expiringCount = DB::table('inventories')
|
||||
->whereNull('deleted_at')
|
||||
->whereNotNull('expiry_date')
|
||||
->where('expiry_date', '>', $today)
|
||||
->where('expiry_date', '<=', $expiryThreshold)
|
||||
->count();
|
||||
|
||||
@@ -467,34 +468,46 @@ class InventoryService implements InventoryServiceInterface
|
||||
$today = now()->toDateString();
|
||||
$expiryThreshold = now()->addDays(30)->toDateString();
|
||||
|
||||
// 1. 庫存品項數 (Unique Product-Warehouse)
|
||||
// 1. 庫存品項數 (明細總數)
|
||||
$totalItems = DB::table('inventories')
|
||||
->whereNull('deleted_at')
|
||||
->distinct()
|
||||
->count(DB::raw('CONCAT(warehouse_id, "-", product_id)'));
|
||||
|
||||
// 2. 低庫存 (品項計數)
|
||||
$lowStockCount = DB::table('warehouse_product_safety_stocks as ss')
|
||||
->join(DB::raw('(SELECT warehouse_id, product_id, SUM(quantity) as total_qty FROM inventories WHERE deleted_at IS NULL GROUP BY warehouse_id, product_id) as inv'),
|
||||
function ($join) {
|
||||
$join->on('ss.warehouse_id', '=', 'inv.warehouse_id')
|
||||
->on('ss.product_id', '=', 'inv.product_id');
|
||||
})
|
||||
->whereRaw('inv.total_qty <= ss.safety_stock')
|
||||
->count();
|
||||
|
||||
// 3. 負庫存 (品項計數)
|
||||
$negativeCount = DB::table(DB::raw('(SELECT warehouse_id, product_id, SUM(quantity) as total_qty FROM inventories WHERE deleted_at IS NULL GROUP BY warehouse_id, product_id) as inv'))
|
||||
->where('total_qty', '<', 0)
|
||||
// 2. 低庫存 (明細計數:只要該明細所屬的「倉庫+商品」總量低於安全庫存,則所有相關明細都計入)
|
||||
$lowStockCount = DB::table('inventories as i')
|
||||
->join('warehouse_product_safety_stocks as ss', function ($join) {
|
||||
$join->on('i.warehouse_id', '=', 'ss.warehouse_id')
|
||||
->on('i.product_id', '=', 'ss.product_id');
|
||||
})
|
||||
->whereNull('i.deleted_at')
|
||||
->whereIn(DB::raw('(i.warehouse_id, i.product_id)'), function ($sub) {
|
||||
$sub->select('i2.warehouse_id', 'i2.product_id')
|
||||
->from('inventories as i2')
|
||||
->whereNull('i2.deleted_at')
|
||||
->groupBy('i2.warehouse_id', 'i2.product_id')
|
||||
->havingRaw('SUM(i2.quantity) <= (SELECT safety_stock FROM warehouse_product_safety_stocks WHERE warehouse_id = i2.warehouse_id AND product_id = i2.product_id LIMIT 1)');
|
||||
})
|
||||
->count();
|
||||
|
||||
// 4. 即將過期 (品項計數)
|
||||
// 3. 負庫存 (明細計數)
|
||||
$negativeCount = DB::table('inventories as i')
|
||||
->whereNull('i.deleted_at')
|
||||
->whereIn(DB::raw('(i.warehouse_id, i.product_id)'), function ($sub) {
|
||||
$sub->select('i2.warehouse_id', 'i2.product_id')
|
||||
->from('inventories as i2')
|
||||
->whereNull('i2.deleted_at')
|
||||
->groupBy('i2.warehouse_id', 'i2.product_id')
|
||||
->havingRaw('SUM(i2.quantity) < 0');
|
||||
})
|
||||
->count();
|
||||
|
||||
// 4. 即將過期 (明細計數)
|
||||
$expiringCount = DB::table('inventories')
|
||||
->whereNull('deleted_at')
|
||||
->whereNotNull('expiry_date')
|
||||
->where('expiry_date', '>', $today) // 確保不過期 (getStockQueryData 沒加這個但這裡加上以防與 expired 混淆? 不,stock query 是 > today && <= threshold)
|
||||
->where('expiry_date', '<=', $expiryThreshold)
|
||||
->distinct()
|
||||
->count(DB::raw('CONCAT(warehouse_id, "-", product_id)'));
|
||||
->count();
|
||||
|
||||
// 異常庫存前 10 筆 (明細面依然以個別批次為主,供快速跳轉)
|
||||
$abnormalItems = Inventory::query()
|
||||
|
||||
Reference in New Issue
Block a user