feat: 統一各模組分頁組件佈局並新增系統設定功能相關檔案
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m5s
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m5s
This commit is contained in:
@@ -23,8 +23,9 @@ class InventoryReportService
|
||||
* @param int|null $perPage 每頁筆數
|
||||
* @return \Illuminate\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection
|
||||
*/
|
||||
public function getReportData(array $filters, ?int $perPage = 10)
|
||||
public function getReportData(array $filters, ?int $perPage = null)
|
||||
{
|
||||
$perPage = $perPage ?? \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||
$dateFrom = $filters['date_from'] ?? null;
|
||||
$dateTo = $filters['date_to'] ?? null;
|
||||
$warehouseId = $filters['warehouse_id'] ?? null;
|
||||
@@ -197,8 +198,9 @@ class InventoryReportService
|
||||
/**
|
||||
* 取得特定商品的庫存異動明細
|
||||
*/
|
||||
public function getProductDetails($productId, array $filters, ?int $perPage = 20)
|
||||
public function getProductDetails($productId, array $filters, ?int $perPage = null)
|
||||
{
|
||||
$perPage = $perPage ?? \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||
$dateFrom = $filters['date_from'] ?? null;
|
||||
$dateTo = $filters['date_to'] ?? null;
|
||||
$warehouseId = $filters['warehouse_id'] ?? null;
|
||||
|
||||
@@ -258,10 +258,12 @@ class InventoryService implements InventoryServiceInterface
|
||||
/**
|
||||
* 即時庫存查詢:統計卡片 + 分頁明細
|
||||
*/
|
||||
public function getStockQueryData(array $filters = [], int $perPage = 10): array
|
||||
public function getStockQueryData(array $filters = [], ?int $perPage = null): array
|
||||
{
|
||||
$perPage = $perPage ?? \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||
$today = now()->toDateString();
|
||||
$expiryThreshold = now()->addDays(30)->toDateString();
|
||||
$expiryDays = \App\Modules\Core\Models\SystemSetting::getVal('inventory.expiry_warning_days', 30);
|
||||
$expiryThreshold = now()->addDays($expiryDays)->toDateString();
|
||||
|
||||
// 基礎查詢
|
||||
$query = Inventory::query()
|
||||
@@ -492,7 +494,8 @@ class InventoryService implements InventoryServiceInterface
|
||||
public function getDashboardStats(): array
|
||||
{
|
||||
$today = now()->toDateString();
|
||||
$expiryThreshold = now()->addDays(30)->toDateString();
|
||||
$expiryDays = \App\Modules\Core\Models\SystemSetting::getVal('inventory.expiry_warning_days', 30);
|
||||
$expiryThreshold = now()->addDays($expiryDays)->toDateString();
|
||||
|
||||
// 1. 庫存品項數 (明細總數)
|
||||
$totalItems = DB::table('inventories')
|
||||
|
||||
@@ -12,8 +12,9 @@ class TurnoverService
|
||||
/**
|
||||
* Get inventory turnover analysis data
|
||||
*/
|
||||
public function getAnalysisData(array $filters, int $perPage = 20)
|
||||
public function getAnalysisData(array $filters, ?int $perPage = null)
|
||||
{
|
||||
$perPage = $perPage ?? \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||
$warehouseId = $filters['warehouse_id'] ?? null;
|
||||
$categoryId = $filters['category_id'] ?? null;
|
||||
$search = $filters['search'] ?? null;
|
||||
@@ -60,7 +61,8 @@ class TurnoverService
|
||||
// Given potentially large data, subquery per row might be slow, but for pagination it's okay-ish.
|
||||
// Better approach: Join with a subquery of aggregated transactions.
|
||||
|
||||
$thirtyDaysAgo = Carbon::now()->subDays(30);
|
||||
$analysisDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.analysis_period_days', 30);
|
||||
$thirtyDaysAgo = Carbon::now()->subDays($analysisDays);
|
||||
|
||||
// Subquery for 30-day sales
|
||||
$salesSubquery = InventoryTransaction::query()
|
||||
@@ -111,7 +113,7 @@ class TurnoverService
|
||||
// Turnover Days Calculation in SQL: (stock / (sales_30d / 30)) => (stock * 30) / sales_30d
|
||||
// Handle division by zero: if sales_30d is 0, turnover is 'Inf' (or very high number like 9999)
|
||||
$turnoverDaysSql = "CASE WHEN COALESCE(sales_30d.sales_qty_30d, 0) > 0
|
||||
THEN (COALESCE(SUM(inventories.quantity), 0) * 30) / sales_30d.sales_qty_30d
|
||||
THEN (COALESCE(SUM(inventories.quantity), 0) * $analysisDays) / sales_30d.sales_qty_30d
|
||||
ELSE 9999 END";
|
||||
|
||||
$query->addSelect(DB::raw("$turnoverDaysSql as turnover_days"));
|
||||
@@ -125,7 +127,8 @@ class TurnoverService
|
||||
// For dead stock, definitive IS stock > 0.
|
||||
|
||||
if ($statusFilter === 'dead') {
|
||||
$ninetyDaysAgo = Carbon::now()->subDays(90);
|
||||
$deadStockDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.dead_stock_days', 90);
|
||||
$ninetyDaysAgo = Carbon::now()->subDays($deadStockDays);
|
||||
$query->havingRaw("current_stock > 0 AND (last_sale_date < ? OR last_sale_date IS NULL)", [$ninetyDaysAgo]);
|
||||
}
|
||||
|
||||
@@ -146,10 +149,13 @@ class TurnoverService
|
||||
$lastSale = $item->last_sale_date ? Carbon::parse($item->last_sale_date) : null;
|
||||
$daysSinceSale = $lastSale ? $lastSale->diffInDays(Carbon::now()) : 9999;
|
||||
|
||||
if ($item->current_stock > 0 && $daysSinceSale > 90) {
|
||||
$deadStockDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.dead_stock_days', 90);
|
||||
$slowMovingDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.slow_moving_days', 60);
|
||||
|
||||
if ($item->current_stock > 0 && $daysSinceSale > $deadStockDays) {
|
||||
$item->status = 'dead'; // 滯銷
|
||||
$item->status_label = '滯銷';
|
||||
} elseif ($item->current_stock > 0 && $item->turnover_days > 60) {
|
||||
} elseif ($item->current_stock > 0 && $item->turnover_days > $slowMovingDays) {
|
||||
$item->status = 'slow'; // 週轉慢
|
||||
$item->status_label = '週轉慢';
|
||||
} elseif ($item->current_stock == 0) {
|
||||
@@ -187,8 +193,8 @@ class TurnoverService
|
||||
// 2. Dead Stock Value (No sale in 90 days)
|
||||
// Need last sale date for each product-location or just product?
|
||||
// Assuming dead stock is product-level logic for simplicity.
|
||||
|
||||
$ninetyDaysAgo = Carbon::now()->subDays(90);
|
||||
$deadStockDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.dead_stock_days', 90);
|
||||
$ninetyDaysAgo = Carbon::now()->subDays($deadStockDays);
|
||||
|
||||
// Get IDs of products sold in last 90 days
|
||||
$soldProductIds = InventoryTransaction::query()
|
||||
@@ -225,17 +231,17 @@ class TurnoverService
|
||||
// Simplified: (Total Stock / Total Sales 30d) * 30
|
||||
|
||||
$totalStock = (clone $buildInvQuery())->sum('inventories.quantity');
|
||||
|
||||
$totalSales30d = DB::table('inventory_transactions')
|
||||
->join('inventories', 'inventory_transactions.inventory_id', '=', 'inventories.id')
|
||||
->join('products', 'inventories.product_id', '=', 'products.id')
|
||||
->where('inventory_transactions.type', '出庫')
|
||||
->where('inventory_transactions.actual_time', '>=', Carbon::now()->subDays(30))
|
||||
->when($warehouseId, fn($q) => $q->where('inventories.warehouse_id', $warehouseId))
|
||||
->when($categoryId, fn($q) => $q->where('products.category_id', $categoryId))
|
||||
->sum(DB::raw('ABS(inventory_transactions.quantity)'));
|
||||
|
||||
$avgTurnoverDays = $totalSales30d > 0 ? ($totalStock * 30) / $totalSales30d : 0;
|
||||
$analysisDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.analysis_period_days', 30);
|
||||
$totalSales30d = DB::table('inventory_transactions')
|
||||
->join('inventories', 'inventory_transactions.inventory_id', '=', 'inventories.id')
|
||||
->join('products', 'inventories.product_id', '=', 'products.id')
|
||||
->where('inventory_transactions.type', '出庫')
|
||||
->where('inventory_transactions.actual_time', '>=', Carbon::now()->subDays($analysisDays))
|
||||
->when($warehouseId, fn($q) => $q->where('inventories.warehouse_id', $warehouseId))
|
||||
->when($categoryId, fn($q) => $q->where('products.category_id', $categoryId))
|
||||
->sum(DB::raw('ABS(inventory_transactions.quantity)'));
|
||||
|
||||
$avgTurnoverDays = $totalSales30d > 0 ? ($totalStock * $analysisDays) / $totalSales30d : 0;
|
||||
|
||||
return [
|
||||
'total_stock_value' => $totalValue,
|
||||
|
||||
Reference in New Issue
Block a user