From 78ec646b42dba8a3c061854aeabb377374078156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B3=B4=EA=B3=A4?= Date: Thu, 19 Mar 2026 17:36:51 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20[finance]=20=EA=B3=84=EC=A0=95=EB=B3=84?= =?UTF-8?q?=EC=9B=90=EC=9E=A5=20=ED=99=88=ED=83=9D=EC=8A=A4=20=EB=B6=84?= =?UTF-8?q?=EA=B0=9C=20UNION=20=EC=A0=9C=EA=B1=B0=20(=EC=9D=BC=EB=B0=98?= =?UTF-8?q?=EC=A0=84=ED=91=9C=EB=A7=8C=20=EC=A1=B0=ED=9A=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/AccountLedgerService.php | 54 ++++----------------------- 1 file changed, 8 insertions(+), 46 deletions(-) diff --git a/app/Services/AccountLedgerService.php b/app/Services/AccountLedgerService.php index 914b92fc..f1ebd5cd 100644 --- a/app/Services/AccountLedgerService.php +++ b/app/Services/AccountLedgerService.php @@ -7,7 +7,7 @@ class AccountLedgerService extends Service { /** - * 계정별원장 조회 + * 계정별원장 조회 (일반전표만 — 카드/홈택스 분개는 이미 일반전표에 포함) */ public function index(array $params): array { @@ -16,7 +16,6 @@ public function index(array $params): array $endDate = $params['end_date']; $accountCode = $params['account_code']; - // 계정과목 정보 $account = DB::table('account_codes') ->where('tenant_id', $tenantId) ->where('code', $accountCode) @@ -33,8 +32,7 @@ public function index(array $params): array ]; } - // 일반전표 분개 라인 - $journalLines = DB::table('journal_entry_lines as jel') + $allLines = DB::table('journal_entry_lines as jel') ->join('journal_entries as je', 'je.id', '=', 'jel.journal_entry_id') ->leftJoin('trading_partners as tp', function ($join) use ($tenantId) { $join->on('tp.id', '=', 'jel.trading_partner_id') @@ -53,34 +51,12 @@ public function index(array $params): array 'jel.credit_amount', DB::raw("'journal' as source_type"), 'jel.journal_entry_id as source_id', - ]); - - // 홈택스 세금계산서 분개 - $hometaxLines = DB::table('hometax_invoice_journals as hij') - ->join('hometax_invoices as hi', 'hi.id', '=', 'hij.hometax_invoice_id') - ->where('hij.tenant_id', $tenantId) - ->where('hij.account_code', $accountCode) - ->whereBetween('hij.write_date', [$startDate, $endDate]) - ->whereNull('hi.deleted_at') - ->select([ - 'hij.write_date as date', - 'hij.description', - 'hij.trading_partner_name', - DB::raw("CASE WHEN hi.invoice_type = 'sales' THEN hi.invoicee_corp_num ELSE hi.invoicer_corp_num END as biz_no"), - 'hij.debit_amount', - 'hij.credit_amount', - DB::raw("'hometax' as source_type"), - 'hij.hometax_invoice_id as source_id', - ]); - - $allLines = $journalLines->unionAll($hometaxLines) - ->orderBy('date') + ]) + ->orderBy('je.entry_date') ->get(); - // 이월잔액 $carryForward = $this->calculateCarryForward($tenantId, $accountCode, $startDate, $account->category); - // 월별 그룹핑 + 잔액 계산 $isDebitNormal = in_array($account->category, ['asset', 'expense']); $runningBalance = $carryForward['balance']; $monthlyData = []; @@ -121,7 +97,6 @@ public function index(array $params): array $grandCredit += $credit; } - // 누계 $cumulativeDebit = 0; $cumulativeCredit = 0; foreach ($monthlyData as &$md) { @@ -132,11 +107,7 @@ public function index(array $params): array unset($md); return [ - 'account' => [ - 'code' => $account->code, - 'name' => $account->name, - 'category' => $account->category, - ], + 'account' => ['code' => $account->code, 'name' => $account->name, 'category' => $account->category], 'period' => ['start_date' => $startDate, 'end_date' => $endDate], 'carry_forward' => $carryForward, 'monthly_data' => array_values($monthlyData), @@ -146,7 +117,7 @@ public function index(array $params): array private function calculateCarryForward(int $tenantId, string $accountCode, string $startDate, string $category): array { - $journalSums = DB::table('journal_entry_lines as jel') + $sums = DB::table('journal_entry_lines as jel') ->join('journal_entries as je', 'je.id', '=', 'jel.journal_entry_id') ->where('jel.tenant_id', $tenantId) ->where('jel.account_code', $accountCode) @@ -155,17 +126,8 @@ private function calculateCarryForward(int $tenantId, string $accountCode, strin ->selectRaw('COALESCE(SUM(jel.debit_amount), 0) as total_debit, COALESCE(SUM(jel.credit_amount), 0) as total_credit') ->first(); - $hometaxSums = DB::table('hometax_invoice_journals as hij') - ->join('hometax_invoices as hi', 'hi.id', '=', 'hij.hometax_invoice_id') - ->where('hij.tenant_id', $tenantId) - ->where('hij.account_code', $accountCode) - ->where('hij.write_date', '<', $startDate) - ->whereNull('hi.deleted_at') - ->selectRaw('COALESCE(SUM(hij.debit_amount), 0) as total_debit, COALESCE(SUM(hij.credit_amount), 0) as total_credit') - ->first(); - - $debit = (int) $journalSums->total_debit + (int) $hometaxSums->total_debit; - $credit = (int) $journalSums->total_credit + (int) $hometaxSums->total_credit; + $debit = (int) $sums->total_debit; + $credit = (int) $sums->total_credit; $isDebitNormal = in_array($category, ['asset', 'expense']); $balance = $isDebitNormal ? ($debit - $credit) : ($credit - $debit);