feat: [finance] 계정과목 확장 및 전표 연동 시스템 구현

- AccountCode 모델/서비스 확장 (업데이트, 기본 계정과목 시딩)
- JournalSyncService 추가 (전표 자동 연동)
- SyncsExpenseAccounts 트레이트 추가
- CardTransactionController, TaxInvoiceController 기능 확장
- expense_accounts 테이블에 전표 연결 컬럼 마이그레이션
- account_codes 테이블 확장 마이그레이션
- 전체 테넌트 기본 계정과목 시딩 마이그레이션

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
유병철
2026-03-08 10:32:20 +09:00
parent 3ac64d5b76
commit 0044779eb4
16 changed files with 1247 additions and 6 deletions

View File

@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('expense_accounts', function (Blueprint $table) {
$table->unsignedBigInteger('journal_entry_id')->nullable()->after('loan_id');
$table->unsignedBigInteger('journal_entry_line_id')->nullable()->after('journal_entry_id');
$table->index(['tenant_id', 'journal_entry_id']);
$table->index(['journal_entry_line_id']);
});
}
public function down(): void
{
Schema::table('expense_accounts', function (Blueprint $table) {
$table->dropIndex(['tenant_id', 'journal_entry_id']);
$table->dropIndex(['journal_entry_line_id']);
$table->dropColumn(['journal_entry_id', 'journal_entry_line_id']);
});
}
};

View File

@@ -0,0 +1,54 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* 계정과목 마스터 확장
*
* - sub_category: 중분류 (유동자산, 판관비, 매출원가 등)
* - parent_code: 상위 계정과목 코드 (계층 구조)
* - depth: 계층 깊이 (1=대, 2=중, 3=소)
* - department_type: 부문 (common=공통, manufacturing=제조, admin=관리)
* - description: 계정과목 설명
*/
public function up(): void
{
Schema::table('account_codes', function (Blueprint $table) {
$table->string('sub_category', 50)->nullable()->after('category')
->comment('중분류 (current_asset, fixed_asset, selling_admin, cogs 등)');
$table->string('parent_code', 10)->nullable()->after('sub_category')
->comment('상위 계정과목 코드 (계층 구조)');
$table->tinyInteger('depth')->default(3)->after('parent_code')
->comment('계층 깊이 (1=대분류, 2=중분류, 3=소분류)');
$table->string('department_type', 20)->default('common')->after('depth')
->comment('부문 (common=공통, manufacturing=제조, admin=관리)');
$table->string('description', 500)->nullable()->after('department_type')
->comment('계정과목 설명');
$table->index(['tenant_id', 'category'], 'account_codes_tenant_category_idx');
$table->index(['tenant_id', 'parent_code'], 'account_codes_tenant_parent_idx');
$table->index(['tenant_id', 'depth'], 'account_codes_tenant_depth_idx');
});
}
public function down(): void
{
Schema::table('account_codes', function (Blueprint $table) {
$table->dropIndex('account_codes_tenant_category_idx');
$table->dropIndex('account_codes_tenant_parent_idx');
$table->dropIndex('account_codes_tenant_depth_idx');
$table->dropColumn([
'sub_category',
'parent_code',
'depth',
'department_type',
'description',
]);
});
}
};

View File

@@ -0,0 +1,218 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
/**
* 모든 기존 테넌트에 더존 Smart A 표준 계정과목 128건 자동 시드
*
* 조건: tenant_id + code 중복 시 skip (기존 데이터 보호)
*/
return new class extends Migration
{
public function up(): void
{
$tenantIds = DB::table('tenants')
->whereNull('deleted_at')
->pluck('id');
$defaults = $this->getDefaultAccountCodes();
$now = now();
foreach ($tenantIds as $tenantId) {
// 이미 등록된 코드 조회
$existingCodes = DB::table('account_codes')
->where('tenant_id', $tenantId)
->pluck('code')
->toArray();
$inserts = [];
foreach ($defaults as $item) {
if (! in_array($item['code'], $existingCodes)) {
$inserts[] = array_merge($item, [
'tenant_id' => $tenantId,
'is_active' => true,
'created_at' => $now,
'updated_at' => $now,
]);
}
}
if (! empty($inserts)) {
// 500건 단위 청크 insert
foreach (array_chunk($inserts, 500) as $chunk) {
DB::table('account_codes')->insert($chunk);
}
}
}
}
public function down(): void
{
// 시드 데이터만 롤백 (수동 추가 데이터는 보호)
$defaultCodes = array_column($this->getDefaultAccountCodes(), 'code');
DB::table('account_codes')
->whereIn('code', $defaultCodes)
->delete();
}
private function getDefaultAccountCodes(): array
{
$c = fn ($code, $name, $cat, $sub, $parent, $depth, $dept, $sort) => [
'code' => $code, 'name' => $name, 'category' => $cat,
'sub_category' => $sub, 'parent_code' => $parent,
'depth' => $depth, 'department_type' => $dept, 'sort_order' => $sort,
];
return [
// 자산 (Assets)
$c('1', '자산', 'asset', null, null, 1, 'common', 100),
$c('11', '유동자산', 'asset', 'current_asset', '1', 2, 'common', 110),
$c('10100', '현금', 'asset', 'current_asset', '11', 3, 'common', 1010),
$c('10200', '당좌예금', 'asset', 'current_asset', '11', 3, 'common', 1020),
$c('10300', '보통예금', 'asset', 'current_asset', '11', 3, 'common', 1030),
$c('10400', '기타제예금', 'asset', 'current_asset', '11', 3, 'common', 1040),
$c('10500', '정기적금', 'asset', 'current_asset', '11', 3, 'common', 1050),
$c('10800', '외상매출금', 'asset', 'current_asset', '11', 3, 'common', 1080),
$c('10900', '대손충당금(외상매출금)', 'asset', 'current_asset', '11', 3, 'common', 1090),
$c('11000', '받을어음', 'asset', 'current_asset', '11', 3, 'common', 1100),
$c('11400', '단기대여금', 'asset', 'current_asset', '11', 3, 'common', 1140),
$c('11600', '미수수익', 'asset', 'current_asset', '11', 3, 'common', 1160),
$c('12000', '미수금', 'asset', 'current_asset', '11', 3, 'common', 1200),
$c('12200', '소모품', 'asset', 'current_asset', '11', 3, 'common', 1220),
$c('12500', '미환급세금', 'asset', 'current_asset', '11', 3, 'common', 1250),
$c('13100', '선급금', 'asset', 'current_asset', '11', 3, 'common', 1310),
$c('13300', '선급비용', 'asset', 'current_asset', '11', 3, 'common', 1330),
$c('13400', '가지급금', 'asset', 'current_asset', '11', 3, 'common', 1340),
$c('13500', '부가세대급금', 'asset', 'current_asset', '11', 3, 'common', 1350),
$c('13600', '선납세금', 'asset', 'current_asset', '11', 3, 'common', 1360),
$c('14000', '선납법인세', 'asset', 'current_asset', '11', 3, 'common', 1400),
$c('12', '재고자산', 'asset', 'current_asset', '1', 2, 'common', 120),
$c('14600', '상품', 'asset', 'current_asset', '12', 3, 'common', 1460),
$c('15000', '제품', 'asset', 'current_asset', '12', 3, 'common', 1500),
$c('15300', '원재료', 'asset', 'current_asset', '12', 3, 'common', 1530),
$c('16200', '부재료', 'asset', 'current_asset', '12', 3, 'common', 1620),
$c('16700', '저장품', 'asset', 'current_asset', '12', 3, 'common', 1670),
$c('16900', '재공품', 'asset', 'current_asset', '12', 3, 'common', 1690),
$c('13', '비유동자산', 'asset', 'fixed_asset', '1', 2, 'common', 130),
$c('17600', '장기성예금', 'asset', 'fixed_asset', '13', 3, 'common', 1760),
$c('17900', '장기대여금', 'asset', 'fixed_asset', '13', 3, 'common', 1790),
$c('18700', '투자부동산', 'asset', 'fixed_asset', '13', 3, 'common', 1870),
$c('19200', '단체퇴직보험예치금', 'asset', 'fixed_asset', '13', 3, 'common', 1920),
$c('20100', '토지', 'asset', 'fixed_asset', '13', 3, 'common', 2010),
$c('20200', '건물', 'asset', 'fixed_asset', '13', 3, 'common', 2020),
$c('20300', '감가상각누계액(건물)', 'asset', 'fixed_asset', '13', 3, 'common', 2030),
$c('20400', '구축물', 'asset', 'fixed_asset', '13', 3, 'common', 2040),
$c('20500', '감가상각누계액(구축물)', 'asset', 'fixed_asset', '13', 3, 'common', 2050),
$c('20600', '기계장치', 'asset', 'fixed_asset', '13', 3, 'common', 2060),
$c('20700', '감가상각누계액(기계장치)', 'asset', 'fixed_asset', '13', 3, 'common', 2070),
$c('20800', '차량운반구', 'asset', 'fixed_asset', '13', 3, 'common', 2080),
$c('20900', '감가상각누계액(차량운반구)', 'asset', 'fixed_asset', '13', 3, 'common', 2090),
$c('21000', '공구와기구', 'asset', 'fixed_asset', '13', 3, 'common', 2100),
$c('21200', '비품', 'asset', 'fixed_asset', '13', 3, 'common', 2120),
$c('21300', '건설중인자산', 'asset', 'fixed_asset', '13', 3, 'common', 2130),
$c('24000', '소프트웨어', 'asset', 'fixed_asset', '13', 3, 'common', 2400),
// 부채 (Liabilities)
$c('2', '부채', 'liability', null, null, 1, 'common', 200),
$c('21', '유동부채', 'liability', 'current_liability', '2', 2, 'common', 210),
$c('25100', '외상매입금', 'liability', 'current_liability', '21', 3, 'common', 2510),
$c('25200', '지급어음', 'liability', 'current_liability', '21', 3, 'common', 2520),
$c('25300', '미지급금', 'liability', 'current_liability', '21', 3, 'common', 2530),
$c('25400', '예수금', 'liability', 'current_liability', '21', 3, 'common', 2540),
$c('25500', '부가세예수금', 'liability', 'current_liability', '21', 3, 'common', 2550),
$c('25900', '선수금', 'liability', 'current_liability', '21', 3, 'common', 2590),
$c('26000', '단기차입금', 'liability', 'current_liability', '21', 3, 'common', 2600),
$c('26100', '미지급세금', 'liability', 'current_liability', '21', 3, 'common', 2610),
$c('26200', '미지급비용', 'liability', 'current_liability', '21', 3, 'common', 2620),
$c('26400', '유동성장기차입금', 'liability', 'current_liability', '21', 3, 'common', 2640),
$c('26500', '미지급배당금', 'liability', 'current_liability', '21', 3, 'common', 2650),
$c('22', '비유동부채', 'liability', 'long_term_liability', '2', 2, 'common', 220),
$c('29300', '장기차입금', 'liability', 'long_term_liability', '22', 3, 'common', 2930),
$c('29400', '임대보증금', 'liability', 'long_term_liability', '22', 3, 'common', 2940),
$c('29500', '퇴직급여충당부채', 'liability', 'long_term_liability', '22', 3, 'common', 2950),
$c('30700', '장기임대보증금', 'liability', 'long_term_liability', '22', 3, 'common', 3070),
// 자본 (Capital)
$c('3', '자본', 'capital', null, null, 1, 'common', 300),
$c('31', '자본금', 'capital', 'capital', '3', 2, 'common', 310),
$c('33100', '자본금', 'capital', 'capital', '31', 3, 'common', 3310),
$c('33200', '우선주자본금', 'capital', 'capital', '31', 3, 'common', 3320),
$c('32', '잉여금', 'capital', 'capital', '3', 2, 'common', 320),
$c('34100', '주식발행초과금', 'capital', 'capital', '32', 3, 'common', 3410),
$c('35100', '이익준비금', 'capital', 'capital', '32', 3, 'common', 3510),
$c('37500', '이월이익잉여금', 'capital', 'capital', '32', 3, 'common', 3750),
$c('37900', '당기순이익', 'capital', 'capital', '32', 3, 'common', 3790),
// 수익 (Revenue)
$c('4', '수익', 'revenue', null, null, 1, 'common', 400),
$c('41', '매출', 'revenue', 'sales_revenue', '4', 2, 'common', 410),
$c('40100', '상품매출', 'revenue', 'sales_revenue', '41', 3, 'common', 4010),
$c('40400', '제품매출', 'revenue', 'sales_revenue', '41', 3, 'common', 4040),
$c('40700', '공사수입금', 'revenue', 'sales_revenue', '41', 3, 'common', 4070),
$c('41000', '임대료수입', 'revenue', 'sales_revenue', '41', 3, 'common', 4100),
$c('42', '영업외수익', 'revenue', 'other_revenue', '4', 2, 'common', 420),
$c('90100', '이자수익', 'revenue', 'other_revenue', '42', 3, 'common', 9010),
$c('90300', '배당금수익', 'revenue', 'other_revenue', '42', 3, 'common', 9030),
$c('90400', '수입임대료', 'revenue', 'other_revenue', '42', 3, 'common', 9040),
$c('90700', '외환차익', 'revenue', 'other_revenue', '42', 3, 'common', 9070),
$c('93000', '잡이익', 'revenue', 'other_revenue', '42', 3, 'common', 9300),
// 비용 (Expenses)
$c('5', '비용', 'expense', null, null, 1, 'common', 500),
$c('51', '매출원가', 'expense', 'cogs', '5', 2, 'manufacturing', 510),
$c('50100', '원재료비', 'expense', 'cogs', '51', 3, 'manufacturing', 5010),
$c('50200', '외주가공비', 'expense', 'cogs', '51', 3, 'manufacturing', 5020),
$c('50300', '급여(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5030),
$c('50400', '임금', 'expense', 'cogs', '51', 3, 'manufacturing', 5040),
$c('50500', '상여금(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5050),
$c('50800', '퇴직급여(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5080),
$c('51100', '복리후생비(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5110),
$c('51200', '여비교통비(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5120),
$c('51300', '접대비(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5130),
$c('51400', '통신비(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5140),
$c('51600', '전력비', 'expense', 'cogs', '51', 3, 'manufacturing', 5160),
$c('51700', '세금과공과금(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5170),
$c('51800', '감가상각비(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5180),
$c('51900', '지급임차료(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5190),
$c('52000', '수선비(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5200),
$c('52100', '보험료(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5210),
$c('52200', '차량유지비(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5220),
$c('52400', '운반비(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5240),
$c('53000', '소모품비(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5300),
$c('53100', '지급수수료(제조)', 'expense', 'cogs', '51', 3, 'manufacturing', 5310),
$c('52', '판매비와관리비', 'expense', 'selling_admin', '5', 2, 'admin', 520),
$c('80100', '임원급여', 'expense', 'selling_admin', '52', 3, 'admin', 8010),
$c('80200', '직원급여', 'expense', 'selling_admin', '52', 3, 'admin', 8020),
$c('80300', '상여금', 'expense', 'selling_admin', '52', 3, 'admin', 8030),
$c('80600', '퇴직급여', 'expense', 'selling_admin', '52', 3, 'admin', 8060),
$c('81100', '복리후생비', 'expense', 'selling_admin', '52', 3, 'admin', 8110),
$c('81200', '여비교통비', 'expense', 'selling_admin', '52', 3, 'admin', 8120),
$c('81300', '접대비', 'expense', 'selling_admin', '52', 3, 'admin', 8130),
$c('81400', '통신비', 'expense', 'selling_admin', '52', 3, 'admin', 8140),
$c('81500', '수도광열비', 'expense', 'selling_admin', '52', 3, 'admin', 8150),
$c('81700', '세금과공과금', 'expense', 'selling_admin', '52', 3, 'admin', 8170),
$c('81800', '감가상각비', 'expense', 'selling_admin', '52', 3, 'admin', 8180),
$c('81900', '지급임차료', 'expense', 'selling_admin', '52', 3, 'admin', 8190),
$c('82000', '수선비', 'expense', 'selling_admin', '52', 3, 'admin', 8200),
$c('82100', '보험료', 'expense', 'selling_admin', '52', 3, 'admin', 8210),
$c('82200', '차량유지비', 'expense', 'selling_admin', '52', 3, 'admin', 8220),
$c('82300', '경상연구개발비', 'expense', 'selling_admin', '52', 3, 'admin', 8230),
$c('82400', '운반비', 'expense', 'selling_admin', '52', 3, 'admin', 8240),
$c('82500', '교육훈련비', 'expense', 'selling_admin', '52', 3, 'admin', 8250),
$c('82600', '도서인쇄비', 'expense', 'selling_admin', '52', 3, 'admin', 8260),
$c('82700', '회의비', 'expense', 'selling_admin', '52', 3, 'admin', 8270),
$c('82900', '사무용품비', 'expense', 'selling_admin', '52', 3, 'admin', 8290),
$c('83000', '소모품비', 'expense', 'selling_admin', '52', 3, 'admin', 8300),
$c('83100', '지급수수료', 'expense', 'selling_admin', '52', 3, 'admin', 8310),
$c('83200', '보관료', 'expense', 'selling_admin', '52', 3, 'admin', 8320),
$c('83300', '광고선전비', 'expense', 'selling_admin', '52', 3, 'admin', 8330),
$c('83500', '대손상각비', 'expense', 'selling_admin', '52', 3, 'admin', 8350),
$c('84800', '잡비', 'expense', 'selling_admin', '52', 3, 'admin', 8480),
$c('53', '영업외비용', 'expense', 'other_expense', '5', 2, 'common', 530),
$c('93100', '이자비용', 'expense', 'other_expense', '53', 3, 'common', 9310),
$c('93200', '외환차손', 'expense', 'other_expense', '53', 3, 'common', 9320),
$c('93300', '기부금', 'expense', 'other_expense', '53', 3, 'common', 9330),
$c('96000', '잡손실', 'expense', 'other_expense', '53', 3, 'common', 9600),
$c('99800', '법인세', 'expense', 'other_expense', '53', 3, 'common', 9980),
$c('99900', '소득세등', 'expense', 'other_expense', '53', 3, 'common', 9990),
];
}
};