feat: [daily-report] 자금현황 카드용 필드 추가

- 미수금 잔액(receivable_balance) 계산 로직 구현
- 미지급금 잔액(payable_balance) 계산 로직 구현
- 당월 예상 지출(monthly_expense_total) 계산 로직 구현
- summary API 응답에 자금현황 3개 필드 포함

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
유병철
2026-03-03 21:06:09 +09:00
parent b7465becab
commit ad27090bfc

View File

@@ -5,6 +5,9 @@
use App\Models\Tenants\BankAccount;
use App\Models\Tenants\Bill;
use App\Models\Tenants\Deposit;
use App\Models\Tenants\ExpectedExpense;
use App\Models\Tenants\Purchase;
use App\Models\Tenants\Sale;
use App\Models\Tenants\Withdrawal;
use Carbon\Carbon;
@@ -155,6 +158,11 @@ public function summary(array $params): array
: null;
$operatingStability = $this->getOperatingStability($operatingMonths);
// 기획서 D1.7 자금현황 카드용 필드
$receivableBalance = $this->calculateReceivableBalance($tenantId, $date);
$payableBalance = $this->calculatePayableBalance($tenantId);
$monthlyExpenseTotal = $this->calculateMonthlyExpenseTotal($tenantId, $date);
return [
'date' => $date->format('Y-m-d'),
'day_of_week' => $date->locale('ko')->dayName,
@@ -167,9 +175,74 @@ public function summary(array $params): array
'monthly_operating_expense' => $monthlyOperatingExpense,
'operating_months' => $operatingMonths,
'operating_stability' => $operatingStability,
// 자금현황 카드용
'receivable_balance' => $receivableBalance,
'payable_balance' => $payableBalance,
'monthly_expense_total' => $monthlyExpenseTotal,
];
}
/**
* 미수금 잔액 계산
* = 전체 매출 - 전체 입금 - 전체 수취어음 (기준일까지)
* ReceivablesService.getTotalCarryForwardBalance() 동일 로직
*/
private function calculateReceivableBalance(int $tenantId, Carbon $date): float
{
$endDate = $date->format('Y-m-d');
$totalSales = Sale::where('tenant_id', $tenantId)
->whereNotNull('client_id')
->where('sale_date', '<=', $endDate)
->sum('total_amount');
$totalDeposits = Deposit::where('tenant_id', $tenantId)
->whereNotNull('client_id')
->where('deposit_date', '<=', $endDate)
->sum('amount');
$totalBills = Bill::where('tenant_id', $tenantId)
->whereNotNull('client_id')
->where('bill_type', 'received')
->where('issue_date', '<=', $endDate)
->sum('amount');
return (float) ($totalSales - $totalDeposits - $totalBills);
}
/**
* 미지급금 잔액 계산
* = 미지급 상태(pending, partial, overdue)인 ExpectedExpense 합계
*/
private function calculatePayableBalance(int $tenantId): float
{
return (float) ExpectedExpense::where('tenant_id', $tenantId)
->whereIn('payment_status', ['pending', 'partial', 'overdue'])
->sum('amount');
}
/**
* 당월 예상 지출 합계 계산
* = 당월 매입(Purchase) + 당월 예상지출(ExpectedExpense)
*/
private function calculateMonthlyExpenseTotal(int $tenantId, Carbon $date): float
{
$startOfMonth = $date->copy()->startOfMonth()->format('Y-m-d');
$endOfMonth = $date->copy()->endOfMonth()->format('Y-m-d');
// 당월 매입 합계
$purchaseTotal = Purchase::where('tenant_id', $tenantId)
->whereBetween('purchase_date', [$startOfMonth, $endOfMonth])
->sum('total_amount');
// 당월 예상 지출 합계 (매입 외: 카드, 어음, 급여, 임대료 등)
$expectedExpenseTotal = ExpectedExpense::where('tenant_id', $tenantId)
->whereBetween('expected_payment_date', [$startOfMonth, $endOfMonth])
->sum('amount');
return (float) ($purchaseTotal + $expectedExpenseTotal);
}
/**
* 직전 3개월 평균 월 운영비 계산
*