refactor: 重構模組通訊與調整儀表板功能
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 57s

- 依循跨模組通訊規範,將 Sales 與 Production 模組中對 Inventory 的直接模型關聯改為透過 InventoryServiceInterface 取得
- 於 InventoryService 實作獲取最高庫存價值、即將過期商品等方法,供儀表板使用
- 確保所有跨模組調用皆採用手動水和(Manual Hydration)方式組合資料
- 移除本地已歸檔的 .agent 規範檔案
This commit is contained in:
2026-02-25 11:48:52 +08:00
parent ad91b08dbc
commit deef3baacc
22 changed files with 826 additions and 1396 deletions

View File

@@ -5,7 +5,7 @@ namespace App\Modules\Sales\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Sales\Models\SalesImportBatch;
use App\Modules\Sales\Imports\SalesImport;
use App\Modules\Inventory\Services\InventoryService; // Assuming this exists or we need to use ProductService
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Maatwebsite\Excel\Facades\Excel;
@@ -66,17 +66,32 @@ class SalesImportController extends Controller
$import->load(['items', 'importer']);
$perPage = $request->input('per_page', 10);
$paginatedItems = $import->items()->paginate($perPage)->withQueryString();
// Manual Hydration for Products and Warehouses
$inventoryService = app(InventoryServiceInterface::class);
$productIds = collect($paginatedItems->items())->pluck('product_id')->filter()->unique()->toArray();
$machineCodes = collect($paginatedItems->items())->pluck('machine_id')->filter()->unique()->toArray();
$products = $inventoryService->getProductsByIds($productIds)->keyBy('id');
$warehouses = $inventoryService->getWarehousesByCodes($machineCodes)->keyBy('code');
$paginatedItems->getCollection()->transform(function ($item) use ($products, $warehouses) {
$item->product = $products->get($item->product_id);
$item->warehouse = $warehouses->get($item->machine_id);
return $item;
});
return Inertia::render('Sales/Import/Show', [
'import' => $import,
'items' => $import->items()->with(['product', 'warehouse'])->paginate($perPage)->withQueryString(),
'items' => $paginatedItems,
'filters' => [
'per_page' => $perPage,
],
]);
}
public function confirm(SalesImportBatch $import, InventoryService $inventoryService)
public function confirm(SalesImportBatch $import, InventoryServiceInterface $inventoryService)
{
if ($import->status !== 'pending') {
return back()->with('error', '此批次無法確認。');
@@ -87,8 +102,8 @@ class SalesImportController extends Controller
$aggregatedDeductions = []; // Key: "warehouse_id:product_id:slot"
// Pre-load necessary warehouses for matching
$machineIds = $import->items->pluck('machine_id')->filter()->unique();
$warehouses = \App\Modules\Inventory\Models\Warehouse::whereIn('code', $machineIds)->get()->keyBy('code');
$machineIds = $import->items->pluck('machine_id')->filter()->unique()->toArray();
$warehouses = $inventoryService->getWarehousesByCodes($machineIds)->keyBy('code');
foreach ($import->items as $item) {
// Only process shipped items with a valid product