Files
star-erp/app/Modules/Finance/Controllers/AccountPayableController.php
sky121113 deef3baacc
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 57s
refactor: 重構模組通訊與調整儀表板功能
- 依循跨模組通訊規範,將 Sales 與 Production 模組中對 Inventory 的直接模型關聯改為透過 InventoryServiceInterface 取得
- 於 InventoryService 實作獲取最高庫存價值、即將過期商品等方法,供儀表板使用
- 確保所有跨模組調用皆採用手動水和(Manual Hydration)方式組合資料
- 移除本地已歸檔的 .agent 規範檔案
2026-02-25 11:48:52 +08:00

157 lines
5.4 KiB
PHP

<?php
namespace App\Modules\Finance\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Finance\Models\AccountPayable;
use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
class AccountPayableController extends Controller
{
protected $procurementService;
protected $inventoryService;
public function __construct(
ProcurementServiceInterface $procurementService,
InventoryServiceInterface $inventoryService
) {
$this->procurementService = $procurementService;
$this->inventoryService = $inventoryService;
}
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$query = AccountPayable::with(['creator']);
// 關鍵字搜尋 (單號、供應商名稱)
if ($request->filled('search')) {
$search = $request->search;
// 透過 ProcurementService 查詢符合關鍵字的 Vendor IDs
$matchedVendors = $this->procurementService->searchVendors($search);
$vendorIds = $matchedVendors->pluck('id')->toArray();
$query->where(function ($q) use ($search, $vendorIds) {
$q->where('document_number', 'like', "%{$search}%");
if (!empty($vendorIds)) {
$q->orWhereIn('vendor_id', $vendorIds);
}
});
}
// 狀態過濾
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();
// Manual Hydration for Vendors
$allVendorIds = collect($payables->items())->pluck('vendor_id')->unique()->filter()->toArray();
$vendorsMap = $this->procurementService->getVendorsByIds($allVendorIds)->keyBy('id');
$payables->getCollection()->transform(function ($item) use ($vendorsMap) {
$item->vendor = $vendorsMap->get($item->vendor_id);
return $item;
});
$vendors = $this->procurementService->getAllVendors();
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(['creator']);
if ($accountPayable->vendor_id) {
$accountPayable->vendor = $this->procurementService->getVendorsByIds([$accountPayable->vendor_id])->first();
}
// 嘗試加載來源單據資訊 (目前支援 goods_receipt)
$sourceDocumentCode = null;
if ($accountPayable->source_document_type === 'goods_receipt') {
$receiptData = app(\App\Modules\Inventory\Contracts\GoodsReceiptServiceInterface::class)
->getGoodsReceiptData($accountPayable->source_document_id);
if ($receiptData) {
$sourceDocumentCode = $receiptData['code'] ?? null;
}
}
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', '帳款已成功標記為已付款');
}
}