refactor(modular): 完成第二階段儀表板解耦與模型清理
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 1m1s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped

This commit is contained in:
2026-01-27 08:59:45 +08:00
parent ac6a81b3d2
commit 0e51992cb4
13 changed files with 140 additions and 52 deletions

View File

@@ -28,4 +28,11 @@ interface CoreServiceInterface
* @return Collection
*/
public function getAllUsers(): Collection;
/**
* Get the system user or create one if not exists.
*
* @return object
*/
public function ensureSystemUserExists();
}

View File

@@ -4,18 +4,24 @@ namespace App\Modules\Core\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Models\Product;
use App\Modules\Procurement\Models\Vendor;
use App\Modules\Procurement\Models\PurchaseOrder;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Models\Inventory;
use App\Modules\Inventory\Models\WarehouseProductSafetyStock;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use Inertia\Inertia;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller
{
protected $inventoryService;
protected $procurementService;
public function __construct(
InventoryServiceInterface $inventoryService,
ProcurementServiceInterface $procurementService
) {
$this->inventoryService = $inventoryService;
$this->procurementService = $procurementService;
}
public function index()
{
$centralDomains = config('tenancy.central_domains', []);
@@ -25,25 +31,17 @@ class DashboardController extends Controller
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();
$invStats = $this->inventoryService->getDashboardStats();
$procStats = $this->procurementService->getDashboardStats();
$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,
'productsCount' => $invStats['productsCount'],
'vendorsCount' => $procStats['vendorsCount'],
'purchaseOrdersCount' => $procStats['purchaseOrdersCount'],
'warehousesCount' => $invStats['warehousesCount'],
'totalInventoryValue' => $invStats['totalInventoryQuantity'], // 原本前端命名是 totalInventoryValue 但實作是 Quantity暫且保留欄位名以不破壞前端
'pendingOrdersCount' => $procStats['pendingOrdersCount'],
'lowStockCount' => $invStats['lowStockCount'],
];
return Inertia::render('Dashboard', [

View File

@@ -39,4 +39,17 @@ class CoreService implements CoreServiceInterface
{
return User::all();
}
public function ensureSystemUserExists()
{
$user = User::first();
if (!$user) {
$user = User::create([
'name' => '系統管理員',
'email' => 'admin@example.com',
'password' => bcrypt('password'),
]);
}
return $user;
}
}