From ad27090bfcd6c57e27d2bb3a27be710c7958c6ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=EB=B3=91=EC=B2=A0?= Date: Tue, 3 Mar 2026 21:06:09 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20[daily-report]=20=EC=9E=90=EA=B8=88?= =?UTF-8?q?=ED=98=84=ED=99=A9=20=EC=B9=B4=EB=93=9C=EC=9A=A9=20=ED=95=84?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 미수금 잔액(receivable_balance) 계산 로직 구현 - 미지급금 잔액(payable_balance) 계산 로직 구현 - 당월 예상 지출(monthly_expense_total) 계산 로직 구현 - summary API 응답에 자금현황 3개 필드 포함 Co-Authored-By: Claude Opus 4.6 --- app/Services/DailyReportService.php | 73 +++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/app/Services/DailyReportService.php b/app/Services/DailyReportService.php index fbabb97..5762d17 100644 --- a/app/Services/DailyReportService.php +++ b/app/Services/DailyReportService.php @@ -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개월 평균 월 운영비 계산 *