getPort() != $demoPort) && in_array(request()->getHost(), $centralDomains)) { return redirect()->route('landlord.dashboard'); } // 計算低庫存數量:各商品在各倉庫的總量 < 安全庫存 $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(); $stats = [ 'productsCount' => Product::count(), 'vendorsCount' => Vendor::count(), 'purchaseOrdersCount' => PurchaseOrder::count(), 'warehousesCount' => Warehouse::count(), 'totalInventoryValue' => Inventory::join('products', 'inventories.product_id', '=', 'products.id') ->sum('inventories.quantity'), 'pendingOrdersCount' => PurchaseOrder::where('status', 'pending')->count(), 'lowStockCount' => $lowStockCount, ]; return Inertia::render('Dashboard', [ 'stats' => $stats, ]); } }