[FEAT] 優化會計報表:新增稅額、發票日期與付款方式等會計專用欄位並支援 CSV 完整匯出
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m0s
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m0s
This commit is contained in:
@@ -19,23 +19,48 @@ class FinanceService implements FinanceServiceInterface
|
||||
|
||||
public function getAccountingReportData(string $start, string $end): array
|
||||
{
|
||||
// 1. 獲取採購單資料
|
||||
$purchaseOrders = $this->procurementService->getPurchaseOrdersByDate($start, $end)
|
||||
->map(function ($po) {
|
||||
return [
|
||||
'id' => 'PO-' . $po->id,
|
||||
'date' => Carbon::parse($po->created_at)->timezone(config('app.timezone'))->toDateString(),
|
||||
'source' => '採購單',
|
||||
'category' => '進貨支出',
|
||||
'item' => $po->vendor->name ?? '未知廠商',
|
||||
'reference' => $po->code,
|
||||
'invoice_number' => $po->invoice_number,
|
||||
'amount' => (float)$po->grand_total,
|
||||
];
|
||||
});
|
||||
// 1. 獲取應付帳款資料 (已付款)
|
||||
$accountPayables = \App\Modules\Finance\Models\AccountPayable::where('status', \App\Modules\Finance\Models\AccountPayable::STATUS_PAID)
|
||||
->whereNotNull('paid_at')
|
||||
->whereBetween('paid_at', [$start, $end])
|
||||
->get();
|
||||
|
||||
// 2. 獲取公共事業費 (注意:目前資料表欄位為 transaction_date)
|
||||
$utilityFees = UtilityFee::whereBetween('transaction_date', [$start, $end])
|
||||
// 取得供應商資料 (Manual Hydration)
|
||||
$vendorIds = $accountPayables->pluck('vendor_id')->unique()->filter()->toArray();
|
||||
$vendorsMap = $this->procurementService->getVendorsByIds($vendorIds)->keyBy('id');
|
||||
|
||||
// 付款方式對映
|
||||
$paymentMethodMap = [
|
||||
'cash' => '現金',
|
||||
'bank_transfer' => '銀行轉帳',
|
||||
'check' => '支票',
|
||||
'credit_card' => '信用卡',
|
||||
];
|
||||
|
||||
$payableRecords = $accountPayables->map(function ($ap) use ($vendorsMap, $paymentMethodMap) {
|
||||
$vendorName = isset($vendorsMap[$ap->vendor_id]) ? $vendorsMap[$ap->vendor_id]->name : '未知廠商';
|
||||
$mappedPaymentMethod = $paymentMethodMap[$ap->payment_method] ?? $ap->payment_method;
|
||||
return [
|
||||
'id' => 'AP-' . $ap->id,
|
||||
'date' => Carbon::parse($ap->paid_at)->timezone(config('app.timezone'))->toDateString(),
|
||||
'source' => '應付帳款',
|
||||
'category' => '進貨支出',
|
||||
'item' => $vendorName,
|
||||
'reference' => $ap->document_number,
|
||||
'invoice_date' => $ap->invoice_date ? $ap->invoice_date->format('Y-m-d') : null,
|
||||
'invoice_number' => $ap->invoice_number,
|
||||
'amount' => (float)$ap->total_amount,
|
||||
'tax_amount' => (float)$ap->tax_amount,
|
||||
'status' => $ap->status,
|
||||
'payment_method' => $mappedPaymentMethod,
|
||||
'payment_note' => $ap->payment_note,
|
||||
'remarks' => $ap->remarks,
|
||||
];
|
||||
});
|
||||
|
||||
// 2. 獲取公共事業費 (已繳費)
|
||||
$utilityFees = UtilityFee::where('payment_status', UtilityFee::STATUS_PAID)
|
||||
->whereBetween('transaction_date', [$start, $end])
|
||||
->get()
|
||||
->map(function ($fee) {
|
||||
return [
|
||||
@@ -45,12 +70,18 @@ class FinanceService implements FinanceServiceInterface
|
||||
'category' => $fee->category,
|
||||
'item' => $fee->description ?: $fee->category,
|
||||
'reference' => '-',
|
||||
'invoice_date' => null,
|
||||
'invoice_number' => $fee->invoice_number,
|
||||
'amount' => (float)$fee->amount,
|
||||
'tax_amount' => 0.0,
|
||||
'status' => $fee->payment_status,
|
||||
'payment_method' => null,
|
||||
'payment_note' => null,
|
||||
'remarks' => $fee->description,
|
||||
];
|
||||
});
|
||||
|
||||
$allRecords = $purchaseOrders->concat($utilityFees)
|
||||
$allRecords = $payableRecords->concat($utilityFees)
|
||||
->sortByDesc('date')
|
||||
->values();
|
||||
|
||||
@@ -58,7 +89,7 @@ class FinanceService implements FinanceServiceInterface
|
||||
'records' => $allRecords,
|
||||
'summary' => [
|
||||
'total_amount' => $allRecords->sum('amount'),
|
||||
'purchase_total' => $purchaseOrders->sum('amount'),
|
||||
'payable_total' => $payableRecords->sum('amount'),
|
||||
'utility_total' => $utilityFees->sum('amount'),
|
||||
'record_count' => $allRecords->count(),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user