fix: [finance] 계정별원장 홈택스 분개 UNION 제거 (일반전표만 조회)
- 카드/홈택스 거래는 이미 일반전표에 분개되어 UNION 시 중복 발생 - hometax_invoice_journals 조회 제거 - 이월잔액 계산에서도 홈택스 부분 제거
This commit is contained in:
@@ -23,7 +23,7 @@ public function index(Request $request)
|
||||
}
|
||||
|
||||
/**
|
||||
* 계정별원장 데이터 조회
|
||||
* 계정별원장 데이터 조회 (일반전표만)
|
||||
*/
|
||||
public function list(Request $request): JsonResponse
|
||||
{
|
||||
@@ -41,8 +41,8 @@ public function list(Request $request): JsonResponse
|
||||
]);
|
||||
}
|
||||
|
||||
// 계정과목 정보
|
||||
$account = AccountCode::where('tenant_id', $tenantId)
|
||||
$account = AccountCode::withoutGlobalScopes()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('code', $accountCode)
|
||||
->first();
|
||||
|
||||
@@ -50,8 +50,8 @@ public function list(Request $request): JsonResponse
|
||||
return response()->json(['error' => '계정과목을 찾을 수 없습니다.'], 404);
|
||||
}
|
||||
|
||||
// 일반전표 분개 라인
|
||||
$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')
|
||||
@@ -64,40 +64,17 @@ public function list(Request $request): JsonResponse
|
||||
->select([
|
||||
'je.entry_date as date',
|
||||
'jel.description',
|
||||
'jel.trading_partner_id',
|
||||
'jel.trading_partner_name',
|
||||
'tp.biz_no',
|
||||
'jel.debit_amount',
|
||||
'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',
|
||||
DB::raw('NULL as trading_partner_id'),
|
||||
'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',
|
||||
]);
|
||||
|
||||
// UNION ALL → 날짜순 정렬
|
||||
$allLines = $journalLines->unionAll($hometaxLines)
|
||||
->orderBy('date')
|
||||
])
|
||||
->orderBy('je.entry_date')
|
||||
->get();
|
||||
|
||||
// 이월잔액 계산 (조회 시작일 이전)
|
||||
// 이월잔액 (일반전표만)
|
||||
$carryForward = $this->calculateCarryForward($tenantId, $accountCode, $startDate, $account->category);
|
||||
|
||||
// 월별 그룹핑 + 잔액 계산
|
||||
@@ -108,7 +85,7 @@ public function list(Request $request): JsonResponse
|
||||
$grandCredit = 0;
|
||||
|
||||
foreach ($allLines as $line) {
|
||||
$month = substr($line->date, 0, 7); // YYYY-MM
|
||||
$month = substr($line->date, 0, 7);
|
||||
|
||||
if (! isset($monthlyData[$month])) {
|
||||
$monthlyData[$month] = [
|
||||
@@ -121,11 +98,7 @@ public function list(Request $request): JsonResponse
|
||||
$debit = (int) $line->debit_amount;
|
||||
$credit = (int) $line->credit_amount;
|
||||
|
||||
if ($isDebitNormal) {
|
||||
$runningBalance += ($debit - $credit);
|
||||
} else {
|
||||
$runningBalance += ($credit - $debit);
|
||||
}
|
||||
$runningBalance += $isDebitNormal ? ($debit - $credit) : ($credit - $debit);
|
||||
|
||||
$monthlyData[$month]['items'][] = [
|
||||
'date' => $line->date,
|
||||
@@ -136,7 +109,7 @@ public function list(Request $request): JsonResponse
|
||||
'credit_amount' => $credit,
|
||||
'balance' => $runningBalance,
|
||||
'source_type' => $line->source_type,
|
||||
'source_id' => $line->source_id,
|
||||
'source_id' => (int) $line->source_id,
|
||||
];
|
||||
|
||||
$monthlyData[$month]['subtotal']['debit'] += $debit;
|
||||
@@ -145,16 +118,13 @@ public function list(Request $request): JsonResponse
|
||||
$grandCredit += $credit;
|
||||
}
|
||||
|
||||
// 누계 계산
|
||||
// 누계
|
||||
$cumulativeDebit = 0;
|
||||
$cumulativeCredit = 0;
|
||||
foreach ($monthlyData as &$md) {
|
||||
$cumulativeDebit += $md['subtotal']['debit'];
|
||||
$cumulativeCredit += $md['subtotal']['credit'];
|
||||
$md['cumulative'] = [
|
||||
'debit' => $cumulativeDebit,
|
||||
'credit' => $cumulativeCredit,
|
||||
];
|
||||
$md['cumulative'] = ['debit' => $cumulativeDebit, 'credit' => $cumulativeCredit];
|
||||
}
|
||||
unset($md);
|
||||
|
||||
@@ -164,27 +134,19 @@ public function list(Request $request): JsonResponse
|
||||
'name' => $account->name,
|
||||
'category' => $account->category,
|
||||
],
|
||||
'period' => [
|
||||
'start_date' => $startDate,
|
||||
'end_date' => $endDate,
|
||||
],
|
||||
'period' => ['start_date' => $startDate, 'end_date' => $endDate],
|
||||
'carry_forward' => $carryForward,
|
||||
'monthly_data' => array_values($monthlyData),
|
||||
'grand_total' => [
|
||||
'debit' => $grandDebit,
|
||||
'credit' => $grandCredit,
|
||||
'balance' => $runningBalance,
|
||||
],
|
||||
'grand_total' => ['debit' => $grandDebit, 'credit' => $grandCredit, 'balance' => $runningBalance],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 이월잔액 계산 (조회 시작일 이전 합계)
|
||||
* 이월잔액 계산 (일반전표만)
|
||||
*/
|
||||
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)
|
||||
@@ -193,19 +155,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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user