refactor(modular): 完成第二階段儀表板解耦與模型清理
This commit is contained in:
@@ -78,8 +78,34 @@ class VendorController extends Controller
|
||||
*/
|
||||
public function show(Vendor $vendor): Response
|
||||
{
|
||||
$vendor->load(['products.baseUnit', 'products.largeUnit']);
|
||||
// $vendor->load(['products.baseUnit', 'products.largeUnit']); // REMOVED: Cross-module relation
|
||||
|
||||
// 1. 獲取關聯的 Product IDs 與 Pivot Data
|
||||
$pivots = \Illuminate\Support\Facades\DB::table('product_vendor')
|
||||
->where('vendor_id', $vendor->id)
|
||||
->get();
|
||||
|
||||
$productIds = $pivots->pluck('product_id')->toArray();
|
||||
|
||||
// 2. 透過 Service 獲取 Products
|
||||
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
|
||||
|
||||
$supplyProducts = $pivots->map(function ($pivot) use ($products) {
|
||||
$product = $products->get($pivot->product_id);
|
||||
if (!$product) return null;
|
||||
|
||||
return (object) [
|
||||
'id' => (string) $pivot->id,
|
||||
'productId' => (string) $product->id,
|
||||
'productName' => $product->name,
|
||||
'unit' => $product->baseUnit?->name ?? 'N/A',
|
||||
'baseUnit' => $product->baseUnit?->name,
|
||||
'largeUnit' => $product->largeUnit?->name,
|
||||
'conversionRate' => (float) $product->conversion_rate,
|
||||
'lastPrice' => (float) $pivot->last_price,
|
||||
];
|
||||
})->filter()->values();
|
||||
|
||||
$formattedVendor = (object) [
|
||||
'id' => (string) $vendor->id,
|
||||
'code' => $vendor->code,
|
||||
@@ -93,16 +119,7 @@ class VendorController extends Controller
|
||||
'email' => $vendor->email,
|
||||
'address' => $vendor->address,
|
||||
'remark' => $vendor->remark,
|
||||
'supplyProducts' => $vendor->products->map(fn($p) => (object) [
|
||||
'id' => (string) $p->pivot->id,
|
||||
'productId' => (string) $p->id,
|
||||
'productName' => $p->name,
|
||||
'unit' => $p->baseUnit?->name ?? 'N/A',
|
||||
'baseUnit' => $p->baseUnit?->name,
|
||||
'largeUnit' => $p->largeUnit?->name,
|
||||
'conversionRate' => (float) $p->conversion_rate,
|
||||
'lastPrice' => (float) $p->pivot->last_price,
|
||||
]),
|
||||
'supplyProducts' => $supplyProducts,
|
||||
];
|
||||
|
||||
return Inertia::render('Vendor/Show', [
|
||||
|
||||
Reference in New Issue
Block a user