feat(API): 카드 거래 대시보드 API 신규 추가

- GET /api/v1/card-transactions/dashboard 엔드포인트 추가
- 월별 추이, 사용자별 비율, 최근 거래 목록 포함
- CEO 대시보드 cm1 모달용 데이터 제공

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-01-22 22:28:41 +09:00
parent f85e070913
commit 857192e8ac
4 changed files with 269 additions and 0 deletions

View File

@@ -263,4 +263,175 @@ public function destroy(int $id): bool
return true;
});
}
/**
* 카드 거래 대시보드 데이터
*
* CEO 대시보드 카드/가지급금 관리 섹션의 cm1 모달용 상세 데이터 제공
*
* @return array{
* summary: array{
* current_month_total: float,
* previous_month_total: float,
* change_rate: float,
* unprocessed_count: int
* },
* monthly_trend: array<array{month: string, amount: float}>,
* user_ratio: array<array{user_name: string, amount: float, percentage: float}>,
* recent_transactions: array
* }
*/
public function dashboard(): array
{
$currentDate = now();
// 당월/전월 기간 계산
$currentMonthStart = $currentDate->copy()->startOfMonth()->toDateString();
$currentMonthEnd = $currentDate->copy()->endOfMonth()->toDateString();
$previousMonthStart = $currentDate->copy()->subMonth()->startOfMonth()->toDateString();
$previousMonthEnd = $currentDate->copy()->subMonth()->endOfMonth()->toDateString();
// 1. Summary 데이터
$currentMonthTotal = $this->getMonthTotal($currentMonthStart, $currentMonthEnd);
$previousMonthTotal = $this->getMonthTotal($previousMonthStart, $previousMonthEnd);
$changeRate = $previousMonthTotal > 0
? round((($currentMonthTotal - $previousMonthTotal) / $previousMonthTotal) * 100, 1)
: ($currentMonthTotal > 0 ? 100 : 0);
// 미정리 건수 (account_code가 없는 건)
$unprocessedCount = Withdrawal::query()
->where('payment_method', 'card')
->whereNull('account_code')
->count();
// 2. 최근 6개월 추이
$monthlyTrend = $this->getMonthlyTrend(6);
// 3. 사용자별 비율 (당월 기준)
$userRatio = $this->getUserRatio($currentMonthStart, $currentMonthEnd);
// 4. 최근 거래 10건
$recentTransactions = $this->getRecentTransactions(10);
return [
'summary' => [
'current_month_total' => (float) $currentMonthTotal,
'previous_month_total' => (float) $previousMonthTotal,
'change_rate' => $changeRate,
'unprocessed_count' => $unprocessedCount,
],
'monthly_trend' => $monthlyTrend,
'user_ratio' => $userRatio,
'recent_transactions' => $recentTransactions,
];
}
/**
* 특정 기간 카드 사용액 합계
*/
private function getMonthTotal(string $startDate, string $endDate): float
{
return (float) Withdrawal::query()
->where('payment_method', 'card')
->whereBetween(DB::raw('DATE(COALESCE(used_at, withdrawal_date))'), [$startDate, $endDate])
->sum('amount');
}
/**
* 최근 N개월 월별 추이
*
* @return array<array{month: string, amount: float}>
*/
private function getMonthlyTrend(int $months): array
{
$result = [];
$currentDate = now();
for ($i = $months - 1; $i >= 0; $i--) {
$targetDate = $currentDate->copy()->subMonths($i);
$monthStart = $targetDate->copy()->startOfMonth()->toDateString();
$monthEnd = $targetDate->copy()->endOfMonth()->toDateString();
$amount = $this->getMonthTotal($monthStart, $monthEnd);
$result[] = [
'month' => $targetDate->format('Y-m'),
'amount' => $amount,
];
}
return $result;
}
/**
* 사용자별 카드 사용 비율
*
* @return array<array{user_name: string, amount: float, percentage: float}>
*/
private function getUserRatio(string $startDate, string $endDate): array
{
$data = Withdrawal::query()
->select([
'cards.assigned_user_id',
DB::raw('COALESCE(users.name, "미지정") as user_name'),
DB::raw('SUM(withdrawals.amount) as total_amount'),
])
->leftJoin('cards', 'withdrawals.card_id', '=', 'cards.id')
->leftJoin('users', 'cards.assigned_user_id', '=', 'users.id')
->where('withdrawals.payment_method', 'card')
->whereBetween(DB::raw('DATE(COALESCE(withdrawals.used_at, withdrawals.withdrawal_date))'), [$startDate, $endDate])
->groupBy('cards.assigned_user_id', 'users.name')
->orderByDesc('total_amount')
->get();
$totalAmount = $data->sum('total_amount');
return $data->map(function ($item) use ($totalAmount) {
return [
'user_name' => $item->user_name,
'amount' => (float) $item->total_amount,
'percentage' => $totalAmount > 0
? round(($item->total_amount / $totalAmount) * 100, 1)
: 0,
];
})->toArray();
}
/**
* 최근 거래 목록
*/
private function getRecentTransactions(int $limit): array
{
return Withdrawal::query()
->select([
'withdrawals.id',
'withdrawals.used_at',
'withdrawals.withdrawal_date',
'withdrawals.merchant_name',
'withdrawals.amount',
'withdrawals.account_code',
'cards.card_name',
DB::raw('COALESCE(users.name, "미지정") as user_name'),
])
->leftJoin('cards', 'withdrawals.card_id', '=', 'cards.id')
->leftJoin('users', 'cards.assigned_user_id', '=', 'users.id')
->where('withdrawals.payment_method', 'card')
->orderByDesc(DB::raw('COALESCE(withdrawals.used_at, withdrawals.withdrawal_date)'))
->limit($limit)
->get()
->map(function ($item) {
return [
'id' => $item->id,
'card_name' => $item->card_name ?? '미지정',
'user_name' => $item->user_name,
'used_at' => $item->used_at?->format('Y-m-d H:i:s')
?? $item->withdrawal_date?->format('Y-m-d'),
'merchant_name' => $item->merchant_name ?? '',
'amount' => (float) $item->amount,
'usage_type' => $item->account_code,
];
})
->toArray();
}
}