filled('search')) { $search = $request->search; $query->where(function ($q) use ($search) { $q->where('document_number', 'like', "%{$search}%") ->orWhereHas('vendor', function ($q) use ($search) { $q->where('name', 'like', "%{$search}%"); }); }); } // 狀態過濾 if ($request->filled('status') && $request->status !== 'all') { $query->where('status', $request->status); } // 供應商過濾 if ($request->filled('vendor_id') && $request->vendor_id !== 'all') { $query->where('vendor_id', $request->vendor_id); } // 日期區間過濾 if ($request->filled('date_start')) { $query->where('due_date', '>=', $request->date_start); } if ($request->filled('date_end')) { $query->where('due_date', '<=', $request->date_end); } $perPage = $request->input('per_page', 10); $payables = $query->latest()->paginate($perPage)->withQueryString(); $vendors = \App\Modules\Procurement\Models\Vendor::select('id', 'name')->get(); return Inertia::render('AccountPayable/Index', [ 'payables' => $payables, 'filters' => $request->all(['search', 'status', 'vendor_id', 'date_start', 'date_end', 'per_page']), 'vendors' => $vendors, ]); } /** * Display the specified resource. */ public function show(AccountPayable $accountPayable) { $accountPayable->load(['vendor', 'creator']); // 嘗試加載來源單據資訊 (目前支援 goods_receipt) $sourceDocumentCode = null; if ($accountPayable->source_document_type === 'goods_receipt') { $receipt = \App\Modules\Inventory\Models\GoodsReceipt::find($accountPayable->source_document_id); if ($receipt) { $sourceDocumentCode = $receipt->code; } } return Inertia::render('AccountPayable/Show', [ // 將 model 轉換成 array 加入額外資訊 'payable' => array_merge($accountPayable->toArray(), ['source_document_code' => $sourceDocumentCode]), ]); } /** * 更新發票資訊 */ public function updateInvoice(Request $request, AccountPayable $accountPayable) { $validated = $request->validate([ 'invoice_number' => 'nullable|string|max:50', 'invoice_date' => 'nullable|date', ]); $accountPayable->update([ 'invoice_number' => $validated['invoice_number'], 'invoice_date' => $validated['invoice_date'], ]); return back()->with('success', '發票資訊已更新'); } /** * 標記已付款 */ public function pay(Request $request, AccountPayable $accountPayable) { $validated = $request->validate([ 'payment_method' => 'required|string|max:50', 'paid_at' => 'required|date', 'payment_note' => 'nullable|string|max:255', ]); if ($accountPayable->status === AccountPayable::STATUS_PAID) { return back()->with('error', '該帳款已經標記為已付款'); } $accountPayable->update([ 'status' => AccountPayable::STATUS_PAID, 'payment_method' => $validated['payment_method'], 'paid_at' => $validated['paid_at'], 'payment_note' => $validated['payment_note'], ]); return back()->with('success', '帳款已成功標記為已付款'); } }