feat: [loan] 대시보드 API에 경조사비 요약 데이터 추가

- condolence_expenses 테이블에서 경조사비 통계를 조회하여 dashboard 응답에 포함
- condolence_summary: total_count, total_amount, congratulation_amount, condolence_amount
- 대시보드 날짜 필터(start_date, end_date)를 event_date 기준으로 적용
This commit is contained in:
김보곤
2026-03-19 17:17:41 +09:00
parent db60499676
commit 01c9d5fca0

View File

@@ -2,6 +2,7 @@
namespace App\Services;
use App\Models\Tenants\CondolenceExpense;
use App\Models\Tenants\ExpenseAccount;
use App\Models\Tenants\Loan;
use App\Models\Tenants\Withdrawal;
@@ -313,7 +314,7 @@ private function syncGiftCertificateExpense(Loan $loan): void
'sub_type' => ExpenseAccount::SUB_TYPE_GIFT_CERTIFICATE,
'expense_date' => $loan->settlement_date ?? $loan->loan_date,
'amount' => $loan->amount,
'description' => ($metadata['cert_name'] ?? '상품권') . ' 접대비 전환',
'description' => ($metadata['cert_name'] ?? '상품권').' 접대비 전환',
'receipt_no' => $metadata['serial_number'] ?? null,
'vendor_name' => $metadata['vendor_name'] ?? null,
'vendor_id' => ! empty($metadata['vendor_id']) ? (int) $metadata['vendor_id'] : null,
@@ -534,6 +535,7 @@ public function dashboard(?string $startDate = null, ?string $endDate = null): a
if ($endDate) {
$query->where('loan_date', '<=', $endDate);
}
return $query;
};
@@ -565,7 +567,25 @@ public function dashboard(?string $startDate = null, ?string $endDate = null): a
// 3. 카테고리별 집계 (날짜 필터 적용)
$categoryBreakdown = $this->getCategoryBreakdown($tenantId, $startDate, $endDate);
// 4. 가지급금 목록 (미정산 우선, 날짜 필터 적용, used/disposed 상품권 제외)
// 4. 경조사비 요약 (condolence_expenses 테이블, 날짜 필터 적용)
$condolenceQuery = CondolenceExpense::query()->where('tenant_id', $tenantId);
if ($startDate) {
$condolenceQuery->where('event_date', '>=', $startDate);
}
if ($endDate) {
$condolenceQuery->where('event_date', '<=', $endDate);
}
$condolenceStats = $condolenceQuery->selectRaw('
COUNT(*) as total_count,
COALESCE(SUM(total_amount), 0) as total_amount,
COALESCE(SUM(CASE WHEN category = ? THEN total_amount ELSE 0 END), 0) as congratulation_amount,
COALESCE(SUM(CASE WHEN category = ? THEN total_amount ELSE 0 END), 0) as condolence_amount
', [
CondolenceExpense::CATEGORY_CONGRATULATION,
CondolenceExpense::CATEGORY_CONDOLENCE,
])->first();
// 5. 가지급금 목록 (미정산 우선, 날짜 필터 적용, used/disposed 상품권 제외)
$loansQuery = Loan::query()
->where('tenant_id', $tenantId)
->with(['user:id,name,email', 'withdrawal']);
@@ -600,6 +620,12 @@ public function dashboard(?string $startDate = null, ?string $endDate = null): a
'outstanding_count' => (int) ($stats->outstanding_count ?? 0),
],
'category_breakdown' => $categoryBreakdown,
'condolence_summary' => [
'total_count' => (int) $condolenceStats->total_count,
'total_amount' => (int) $condolenceStats->total_amount,
'congratulation_amount' => (int) $condolenceStats->congratulation_amount,
'condolence_amount' => (int) $condolenceStats->condolence_amount,
],
'loans' => $loans,
];
}