filled('search')) { $search = $request->search; $query->where(function ($q) use ($search) { $q->where('name', 'like', "%{$search}%") ->orWhere('code', 'like', "%{$search}%") ->orWhere('tax_id', 'like', "%{$search}%") ->orWhere('owner', 'like', "%{$search}%") ->orWhere('contact_name', 'like', "%{$search}%"); }); } $sortField = $request->input('sort_field', 'id'); $sortDirection = $request->input('sort_direction', 'desc'); $allowedSorts = ['id', 'code', 'name', 'owner', 'contact_name', 'phone']; if (!in_array($sortField, $allowedSorts)) { $sortField = 'id'; } if (!in_array(strtolower($sortDirection), ['asc', 'desc'])) { $sortDirection = 'desc'; } $perPage = $request->input('per_page', 10); $vendors = $query->orderBy($sortField, $sortDirection) ->paginate($perPage) ->withQueryString(); $vendors->getCollection()->transform(function ($vendor) { return (object) [ 'id' => (string) $vendor->id, 'code' => $vendor->code, 'name' => $vendor->name, 'shortName' => $vendor->short_name, 'taxId' => $vendor->tax_id, 'owner' => $vendor->owner, 'contactName' => $vendor->contact_name, 'phone' => $vendor->phone, 'tel' => $vendor->tel, 'email' => $vendor->email, 'address' => $vendor->address, 'remark' => $vendor->remark, ]; }); return Inertia::render('Vendor/Index', [ 'vendors' => $vendors, 'filters' => $request->only(['search', 'sort_field', 'sort_direction', 'per_page']), ]); } /** * 顯示指定資源。 */ public function show(Vendor $vendor): Response { $vendor->load(['products.baseUnit', 'products.largeUnit']); $formattedVendor = (object) [ 'id' => (string) $vendor->id, 'code' => $vendor->code, 'name' => $vendor->name, 'shortName' => $vendor->short_name, 'taxId' => $vendor->tax_id, 'owner' => $vendor->owner, 'contactName' => $vendor->contact_name, 'phone' => $vendor->phone, 'tel' => $vendor->tel, '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, ]), ]; return Inertia::render('Vendor/Show', [ 'vendor' => $formattedVendor, 'products' => $this->inventoryService->getAllProducts(), // 使用已有的服務獲取所有商品供選取 ]); } /** * 將新建立的資源儲存到儲存體中。 */ public function store(Request $request) { $validated = $request->validate([ 'name' => 'required|string|max:255', 'short_name' => 'nullable|string|max:255', 'tax_id' => 'nullable|string|max:8', 'owner' => 'nullable|string|max:255', 'contact_name' => 'nullable|string|max:255', 'tel' => 'nullable|string|max:50', 'phone' => 'nullable|string|max:50', 'email' => 'nullable|email|max:255', 'address' => 'nullable|string', 'remark' => 'nullable|string', ]); // 自動產生代碼 $prefix = 'V'; $lastVendor = Vendor::latest('id')->first(); $nextId = $lastVendor ? $lastVendor->id + 1 : 1; $code = $prefix . str_pad($nextId, 5, '0', STR_PAD_LEFT); $validated['code'] = $code; Vendor::create($validated); return redirect()->back()->with('success', '廠商已建立'); } /** * 更新儲存體中的指定資源。 */ public function update(Request $request, Vendor $vendor) { $validated = $request->validate([ 'name' => 'required|string|max:255', 'short_name' => 'nullable|string|max:255', 'tax_id' => 'nullable|string|max:8', 'owner' => 'nullable|string|max:255', 'contact_name' => 'nullable|string|max:255', 'tel' => 'nullable|string|max:50', 'phone' => 'nullable|string|max:50', 'email' => 'nullable|email|max:255', 'address' => 'nullable|string', 'remark' => 'nullable|string', ]); $vendor->update($validated); return redirect()->back()->with('success', '廠商資料已更新'); } /** * 從儲存體中移除指定資源。 */ public function destroy(Vendor $vendor) { $vendor->delete(); return redirect()->back()->with('success', '廠商已刪除'); } }