@k2works/claude-code-booster 3.2.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/lib/assets/docs/article/index.md +4 -1
  2. package/lib/assets/docs/article/practical-database-design/index.md +121 -0
  3. package/lib/assets/docs/article/practical-database-design/part1/chapter01.md +288 -0
  4. package/lib/assets/docs/article/practical-database-design/part1/chapter02.md +518 -0
  5. package/lib/assets/docs/article/practical-database-design/part1/chapter03.md +557 -0
  6. package/lib/assets/docs/article/practical-database-design/part2/chapter04.md +924 -0
  7. package/lib/assets/docs/article/practical-database-design/part2/chapter05.md +1627 -0
  8. package/lib/assets/docs/article/practical-database-design/part2/chapter06.md +2716 -0
  9. package/lib/assets/docs/article/practical-database-design/part2/chapter07.md +2082 -0
  10. package/lib/assets/docs/article/practical-database-design/part2/chapter08.md +2105 -0
  11. package/lib/assets/docs/article/practical-database-design/part2/chapter09.md +2031 -0
  12. package/lib/assets/docs/article/practical-database-design/part2/chapter10.md +1387 -0
  13. package/lib/assets/docs/article/practical-database-design/part2/chapter11.md +1677 -0
  14. package/lib/assets/docs/article/practical-database-design/part2/chapter12.md +1417 -0
  15. package/lib/assets/docs/article/practical-database-design/part2/chapter13.md +1434 -0
  16. package/lib/assets/docs/article/practical-database-design/part3/chapter14.md +667 -0
  17. package/lib/assets/docs/article/practical-database-design/part3/chapter15.md +1625 -0
  18. package/lib/assets/docs/article/practical-database-design/part3/chapter16.md +1915 -0
  19. package/lib/assets/docs/article/practical-database-design/part3/chapter17.md +1708 -0
  20. package/lib/assets/docs/article/practical-database-design/part3/chapter18.md +2095 -0
  21. package/lib/assets/docs/article/practical-database-design/part3/chapter19.md +1123 -0
  22. package/lib/assets/docs/article/practical-database-design/part3/chapter20.md +1031 -0
  23. package/lib/assets/docs/article/practical-database-design/part3/chapter21.md +1382 -0
  24. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter14-orm.md +991 -0
  25. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter15-orm.md +1300 -0
  26. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter16-orm.md +1166 -0
  27. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter17-orm.md +1584 -0
  28. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter18-orm.md +1183 -0
  29. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter19-orm.md +1016 -0
  30. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter20-orm.md +1753 -0
  31. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter21-orm.md +1447 -0
  32. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter22-orm.md +1878 -0
  33. package/lib/assets/docs/article/practical-database-design/part4/chapter22.md +965 -0
  34. package/lib/assets/docs/article/practical-database-design/part4/chapter23.md +2069 -0
  35. package/lib/assets/docs/article/practical-database-design/part4/chapter24.md +2439 -0
  36. package/lib/assets/docs/article/practical-database-design/part4/chapter25.md +3661 -0
  37. package/lib/assets/docs/article/practical-database-design/part4/chapter26.md +2916 -0
  38. package/lib/assets/docs/article/practical-database-design/part4/chapter27.md +3105 -0
  39. package/lib/assets/docs/article/practical-database-design/part4/chapter28.md +2697 -0
  40. package/lib/assets/docs/article/practical-database-design/part4/chapter29.md +2544 -0
  41. package/lib/assets/docs/article/practical-database-design/part4/chapter30.md +2180 -0
  42. package/lib/assets/docs/article/practical-database-design/part4/chapter31.md +1192 -0
  43. package/lib/assets/docs/article/practical-database-design/part4/chapter32.md +2101 -0
  44. package/lib/assets/docs/article/practical-database-design/part5/chapter33.md +1032 -0
  45. package/lib/assets/docs/article/practical-database-design/part5/chapter34.md +1609 -0
  46. package/lib/assets/docs/article/practical-database-design/part5/chapter35.md +1453 -0
  47. package/lib/assets/docs/article/practical-database-design/part5/chapter36.md +1292 -0
  48. package/lib/assets/docs/article/practical-database-design/part5/chapter37.md +1470 -0
  49. package/lib/assets/docs/article/practical-database-design/part5/chapter38.md +1698 -0
  50. package/lib/assets/docs/article/practical-database-design/part5/chapter39.md +2334 -0
  51. package/lib/assets/docs/article/practical-database-design/study/study2-1.md +1693 -0
  52. package/lib/assets/docs/article/practical-database-design/study/study2-2.md +1347 -0
  53. package/lib/assets/docs/article/practical-database-design/study/study2-3.md +2044 -0
  54. package/lib/assets/docs/article/practical-database-design/study/study2-4.md +2229 -0
  55. package/lib/assets/docs/article/practical-database-design/study/study2-5.md +2418 -0
  56. package/lib/assets/docs/article/practical-database-design/study/study3-1.md +2205 -0
  57. package/lib/assets/docs/article/practical-database-design/study/study3-2.md +2221 -0
  58. package/lib/assets/docs/article/practical-database-design/study/study3-3.md +2253 -0
  59. package/lib/assets/docs/article/practical-database-design/study/study3-4.md +2106 -0
  60. package/lib/assets/docs/article/practical-database-design/study/study3-5.md +2507 -0
  61. package/lib/assets/docs/article/practical-database-design/study/study4-1.md +2587 -0
  62. package/lib/assets/docs/article/practical-database-design/study/study4-2.md +2075 -0
  63. package/lib/assets/docs/article/practical-database-design/study/study4-3.md +1805 -0
  64. package/lib/assets/docs/article/practical-database-design/study/study4-4.md +1895 -0
  65. package/lib/assets/docs/article/practical-database-design/study/study4-5.md +2878 -0
  66. package/lib/assets/docs/reference//351/201/213/345/226/266/347/256/241/347/220/206.md +131 -39
  67. package/package.json +1 -1
@@ -0,0 +1,1753 @@
1
+ # 第20章:財務会計データ設計(D社事例)【ORM版】
2
+
3
+ 本章では、化粧品製造販売会社D社を題材として、Spring Data JPA を使用した財務会計システムのデータ設計と Seed データ実装を解説します。JPA の CascadeType.ALL によるカスケード保存や、CommandLineRunner による起動時データ投入を活用します。
4
+
5
+ ## 20.1 D社の概要
6
+
7
+ ### 20.1.1 会社プロファイル
8
+
9
+ D社は化粧品製造販売を主業とする中堅企業です。
10
+
11
+ | 項目 | 内容 |
12
+ |------|------|
13
+ | 業種 | 化粧品製造販売 |
14
+ | 資本金 | 5億円 |
15
+ | 従業員数 | 300名 |
16
+ | 年間売上高 | 80億円 |
17
+ | 決算期 | 3月 |
18
+ | 経理体制 | 経理部10名、財務課3名 |
19
+
20
+ ### 20.1.2 企業概要
21
+
22
+ ```plantuml
23
+ @startuml
24
+ title D社 企業概要
25
+
26
+ left to right direction
27
+
28
+ rectangle "D社(化粧品製造販売)" as company {
29
+
30
+ card info [
31
+ **基本情報**
32
+ ----
33
+ 資本金: 5億円
34
+ 従業員: 300名
35
+ 売上高: 80億円/年
36
+ 決算期: 3月
37
+ ]
38
+
39
+ card business [
40
+ **事業内容**
41
+ ----
42
+ スキンケア製品製造
43
+ メイクアップ製品製造
44
+ OEM/ODM受託製造
45
+ 直営店舗運営
46
+ EC販売
47
+ ]
48
+
49
+ card feature [
50
+ **特徴**
51
+ ----
52
+ 原価管理重視
53
+ 部門別損益管理
54
+ プロジェクト別収益管理
55
+ 消費税複数税率対応
56
+ ]
57
+ }
58
+
59
+ @enduml
60
+ ```
61
+
62
+ ## 20.2 組織構成
63
+
64
+ ### 20.2.1 組織図
65
+
66
+ ```plantuml
67
+ @startwbs
68
+
69
+ title D社 組織図
70
+
71
+ * 代表取締役
72
+ ** 営業本部
73
+ *** 国内営業部
74
+ **** 東日本営業課
75
+ **** 西日本営業課
76
+ *** 海外営業部
77
+ **** 北米課
78
+ **** アジア課
79
+ *** EC事業部
80
+ **** 自社EC課
81
+ **** モール課
82
+ ** 製造本部
83
+ *** 製造部
84
+ **** 第一製造課
85
+ **** 第二製造課
86
+ *** 品質管理部
87
+ **** 品質保証課
88
+ **** 検査課
89
+ *** 研究開発部
90
+ **** 基礎研究課
91
+ **** 応用開発課
92
+ ** 管理本部
93
+ *** 経理部
94
+ **** 財務課
95
+ **** 主計課
96
+ **** 税務課
97
+ *** 総務部
98
+ **** 人事課
99
+ **** 庶務課
100
+ *** 情報システム部
101
+
102
+ @endwbs
103
+ ```
104
+
105
+ ### 20.2.2 部門コード体系
106
+
107
+ | 部門コード | 部門名 | 組織階層 | 部門パス |
108
+ |-----------|--------|----------|----------|
109
+ | 10000 | 全社 | 0 | 10000 |
110
+ | 11000 | 営業本部 | 1 | 10000~11000 |
111
+ | 11100 | 国内営業部 | 2 | 10000~11000~11100 |
112
+ | 11110 | 東日本営業課 | 3 | 10000~11000~11100~11110 |
113
+ | 11120 | 西日本営業課 | 3 | 10000~11000~11100~11120 |
114
+ | 12000 | 製造本部 | 1 | 10000~12000 |
115
+ | 12100 | 製造部 | 2 | 10000~12000~12100 |
116
+ | 13000 | 管理本部 | 1 | 10000~13000 |
117
+ | 13100 | 経理部 | 2 | 10000~13000~13100 |
118
+
119
+ ### 20.2.3 部門エンティティ(JPA)
120
+
121
+ <details>
122
+ <summary>Department エンティティ</summary>
123
+
124
+ ```java
125
+ // src/main/java/com/example/accounting/domain/model/department/Department.java
126
+ package com.example.accounting.domain.model.department;
127
+
128
+ import jakarta.persistence.*;
129
+ import lombok.*;
130
+
131
+ import java.time.LocalDateTime;
132
+
133
+ /**
134
+ * 部門エンティティ
135
+ */
136
+ @Entity
137
+ @Table(name = "部門マスタ")
138
+ @Data
139
+ @Builder
140
+ @NoArgsConstructor
141
+ @AllArgsConstructor
142
+ public class Department {
143
+
144
+ @Id
145
+ @Column(name = "部門コード", length = 5)
146
+ private String departmentCode;
147
+
148
+ @Column(name = "部門名", length = 40, nullable = false)
149
+ private String departmentName;
150
+
151
+ @Column(name = "組織階層")
152
+ private Integer organizationLevel;
153
+
154
+ @Column(name = "部門パス", length = 100)
155
+ private String departmentPath;
156
+
157
+ @Column(name = "最下層区分")
158
+ private Integer lowestLevelFlag;
159
+
160
+ @Column(name = "作成日時")
161
+ private LocalDateTime createdAt;
162
+
163
+ @Column(name = "更新日時")
164
+ private LocalDateTime updatedAt;
165
+
166
+ @Column(name = "更新者名", length = 20)
167
+ private String updatedBy;
168
+
169
+ /**
170
+ * 最下層(末端)部門かどうか
171
+ */
172
+ public boolean isLeaf() {
173
+ return lowestLevelFlag != null && lowestLevelFlag == 1;
174
+ }
175
+
176
+ /**
177
+ * 部門パスから親部門コードを取得
178
+ */
179
+ public String getParentDepartmentCode() {
180
+ if (departmentPath == null || !departmentPath.contains("~")) {
181
+ return null;
182
+ }
183
+ String[] parts = departmentPath.split("~");
184
+ if (parts.length < 2) {
185
+ return null;
186
+ }
187
+ return parts[parts.length - 2];
188
+ }
189
+
190
+ @PrePersist
191
+ protected void onCreate() {
192
+ if (createdAt == null) {
193
+ createdAt = LocalDateTime.now();
194
+ }
195
+ if (updatedAt == null) {
196
+ updatedAt = LocalDateTime.now();
197
+ }
198
+ }
199
+
200
+ @PreUpdate
201
+ protected void onUpdate() {
202
+ updatedAt = LocalDateTime.now();
203
+ }
204
+ }
205
+ ```
206
+
207
+ </details>
208
+
209
+ ### 20.2.4 部門リポジトリ(JPA)
210
+
211
+ <details>
212
+ <summary>DepartmentJpaRepository インターフェース</summary>
213
+
214
+ ```java
215
+ // src/main/java/com/example/accounting/infrastructure/persistence/repository/DepartmentJpaRepository.java
216
+ package com.example.accounting.infrastructure.persistence.repository;
217
+
218
+ import com.example.accounting.domain.model.department.Department;
219
+ import org.springframework.data.jpa.repository.JpaRepository;
220
+ import org.springframework.data.jpa.repository.Query;
221
+ import org.springframework.data.repository.query.Param;
222
+ import org.springframework.stereotype.Repository;
223
+
224
+ import java.util.List;
225
+
226
+ /**
227
+ * 部門リポジトリ(Spring Data JPA)
228
+ */
229
+ @Repository
230
+ public interface DepartmentJpaRepository extends JpaRepository<Department, String> {
231
+
232
+ /**
233
+ * 組織階層で検索する
234
+ */
235
+ List<Department> findByOrganizationLevel(Integer level);
236
+
237
+ /**
238
+ * 部門パスで前方一致検索する(配下の部門を取得)
239
+ */
240
+ @Query("SELECT d FROM Department d WHERE d.departmentPath LIKE CONCAT(:pathPrefix, '%') ORDER BY d.departmentPath")
241
+ List<Department> findByDepartmentPathStartsWith(@Param("pathPrefix") String pathPrefix);
242
+
243
+ /**
244
+ * 最下層部門のみ検索する
245
+ */
246
+ List<Department> findByLowestLevelFlag(Integer flag);
247
+
248
+ /**
249
+ * 最下層部門のみ検索する(便利メソッド)
250
+ */
251
+ default List<Department> findLeafDepartments() {
252
+ return findByLowestLevelFlag(1);
253
+ }
254
+
255
+ /**
256
+ * 部門コード順で全件取得
257
+ */
258
+ List<Department> findAllByOrderByDepartmentCodeAsc();
259
+ }
260
+ ```
261
+
262
+ </details>
263
+
264
+ ### 20.2.5 部門リポジトリのテスト(JPA)
265
+
266
+ <details>
267
+ <summary>DepartmentJpaRepositoryTest</summary>
268
+
269
+ ```java
270
+ // src/test/java/com/example/accounting/infrastructure/persistence/repository/DepartmentJpaRepositoryTest.java
271
+ package com.example.accounting.infrastructure.persistence.repository;
272
+
273
+ import com.example.accounting.domain.model.department.Department;
274
+ import org.junit.jupiter.api.*;
275
+ import org.springframework.beans.factory.annotation.Autowired;
276
+ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
277
+ import org.springframework.test.context.DynamicPropertyRegistry;
278
+ import org.springframework.test.context.DynamicPropertySource;
279
+ import org.testcontainers.containers.PostgreSQLContainer;
280
+ import org.testcontainers.junit.jupiter.Container;
281
+ import org.testcontainers.junit.jupiter.Testcontainers;
282
+
283
+ import static org.assertj.core.api.Assertions.*;
284
+
285
+ @DataJpaTest
286
+ @Testcontainers
287
+ @DisplayName("部門リポジトリ(JPA)")
288
+ class DepartmentJpaRepositoryTest {
289
+
290
+ @Container
291
+ static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16")
292
+ .withDatabaseName("testdb")
293
+ .withUsername("testuser")
294
+ .withPassword("testpass");
295
+
296
+ @DynamicPropertySource
297
+ static void configureProperties(DynamicPropertyRegistry registry) {
298
+ registry.add("spring.datasource.url", postgres::getJdbcUrl);
299
+ registry.add("spring.datasource.username", postgres::getUsername);
300
+ registry.add("spring.datasource.password", postgres::getPassword);
301
+ }
302
+
303
+ @Autowired
304
+ private DepartmentJpaRepository departmentRepository;
305
+
306
+ @BeforeEach
307
+ void setUp() {
308
+ departmentRepository.deleteAll();
309
+ }
310
+
311
+ @Nested
312
+ @DisplayName("登録")
313
+ class Insert {
314
+
315
+ @Test
316
+ @DisplayName("部門を登録できる")
317
+ void canInsertDepartment() {
318
+ // Arrange
319
+ var department = Department.builder()
320
+ .departmentCode("10000")
321
+ .departmentName("全社")
322
+ .organizationLevel(0)
323
+ .departmentPath("10000")
324
+ .lowestLevelFlag(0)
325
+ .build();
326
+
327
+ // Act
328
+ departmentRepository.save(department);
329
+
330
+ // Assert
331
+ var result = departmentRepository.findById("10000");
332
+ assertThat(result).isPresent();
333
+ assertThat(result.get().getDepartmentName()).isEqualTo("全社");
334
+ assertThat(result.get().getOrganizationLevel()).isEqualTo(0);
335
+ }
336
+
337
+ @Test
338
+ @DisplayName("階層構造を持つ部門を登録できる")
339
+ void canInsertHierarchicalDepartments() {
340
+ // Arrange & Act
341
+ departmentRepository.save(Department.builder()
342
+ .departmentCode("10000")
343
+ .departmentName("全社")
344
+ .organizationLevel(0)
345
+ .departmentPath("10000")
346
+ .lowestLevelFlag(0)
347
+ .build());
348
+
349
+ departmentRepository.save(Department.builder()
350
+ .departmentCode("11000")
351
+ .departmentName("営業本部")
352
+ .organizationLevel(1)
353
+ .departmentPath("10000~11000")
354
+ .lowestLevelFlag(0)
355
+ .build());
356
+
357
+ departmentRepository.save(Department.builder()
358
+ .departmentCode("11110")
359
+ .departmentName("東日本営業課")
360
+ .organizationLevel(3)
361
+ .departmentPath("10000~11000~11100~11110")
362
+ .lowestLevelFlag(1)
363
+ .build());
364
+
365
+ // Assert
366
+ var result = departmentRepository.findById("11110");
367
+ assertThat(result).isPresent();
368
+ assertThat(result.get().getDepartmentPath()).isEqualTo("10000~11000~11100~11110");
369
+ assertThat(result.get().isLeaf()).isTrue();
370
+ }
371
+ }
372
+
373
+ @Nested
374
+ @DisplayName("検索")
375
+ class Search {
376
+
377
+ @BeforeEach
378
+ void setUpTestData() {
379
+ departmentRepository.save(Department.builder()
380
+ .departmentCode("10000")
381
+ .departmentName("全社")
382
+ .organizationLevel(0)
383
+ .departmentPath("10000")
384
+ .lowestLevelFlag(0)
385
+ .build());
386
+
387
+ departmentRepository.save(Department.builder()
388
+ .departmentCode("11000")
389
+ .departmentName("営業本部")
390
+ .organizationLevel(1)
391
+ .departmentPath("10000~11000")
392
+ .lowestLevelFlag(0)
393
+ .build());
394
+
395
+ departmentRepository.save(Department.builder()
396
+ .departmentCode("11110")
397
+ .departmentName("東日本営業課")
398
+ .organizationLevel(3)
399
+ .departmentPath("10000~11000~11100~11110")
400
+ .lowestLevelFlag(1)
401
+ .build());
402
+
403
+ departmentRepository.save(Department.builder()
404
+ .departmentCode("12000")
405
+ .departmentName("製造本部")
406
+ .organizationLevel(1)
407
+ .departmentPath("10000~12000")
408
+ .lowestLevelFlag(0)
409
+ .build());
410
+ }
411
+
412
+ @Test
413
+ @DisplayName("組織階層で検索できる")
414
+ void canFindByOrganizationLevel() {
415
+ // Act
416
+ var result = departmentRepository.findByOrganizationLevel(1);
417
+
418
+ // Assert
419
+ assertThat(result).hasSize(2);
420
+ assertThat(result).extracting(Department::getDepartmentCode)
421
+ .containsExactlyInAnyOrder("11000", "12000");
422
+ }
423
+
424
+ @Test
425
+ @DisplayName("部門パスで配下の部門を検索できる")
426
+ void canFindByDepartmentPathStartsWith() {
427
+ // Act
428
+ var result = departmentRepository.findByDepartmentPathStartsWith("10000~11000");
429
+
430
+ // Assert
431
+ assertThat(result).hasSize(2);
432
+ assertThat(result).extracting(Department::getDepartmentCode)
433
+ .containsExactlyInAnyOrder("11000", "11110");
434
+ }
435
+
436
+ @Test
437
+ @DisplayName("最下層部門のみ検索できる")
438
+ void canFindLeafDepartments() {
439
+ // Act
440
+ var result = departmentRepository.findLeafDepartments();
441
+
442
+ // Assert
443
+ assertThat(result).hasSize(1);
444
+ assertThat(result.get(0).getDepartmentCode()).isEqualTo("11110");
445
+ }
446
+ }
447
+ }
448
+ ```
449
+
450
+ </details>
451
+
452
+ ## 20.3 勘定科目体系
453
+
454
+ ### 20.3.1 勘定科目の構成
455
+
456
+ D社の勘定科目は、貸借対照表(BS)科目と損益計算書(PL)科目で構成されています。
457
+
458
+ ```plantuml
459
+ @startwbs
460
+
461
+ title D社 貸借対照表科目(抜粋)
462
+
463
+ * 貸借対照表
464
+ ** (資産の部)
465
+ *** 流動資産
466
+ **** 現金及び預金
467
+ ***** 現金
468
+ ***** 当座預金
469
+ ***** 普通預金
470
+ **** 売上債権
471
+ ***** 売掛金
472
+ ***** 受取手形
473
+ **** 棚卸資産
474
+ ***** 製品
475
+ ***** 仕掛品
476
+ ***** 原材料
477
+ ***** 貯蔵品
478
+ **** その他流動資産
479
+ ***** 前払費用
480
+ ***** 仮払消費税
481
+ *** 固定資産
482
+ **** 有形固定資産
483
+ ***** 建物
484
+ ***** 機械装置
485
+ ***** 工具器具備品
486
+ **** 無形固定資産
487
+ ***** ソフトウェア
488
+ ** (負債の部)
489
+ *** 流動負債
490
+ **** 仕入債務
491
+ ***** 買掛金
492
+ ***** 支払手形
493
+ **** その他流動負債
494
+ ***** 未払金
495
+ ***** 未払費用
496
+ ***** 仮受消費税
497
+ ***** 預り金
498
+ *** 固定負債
499
+ **** 長期借入金
500
+ ** (純資産の部)
501
+ *** 資本金
502
+ *** 資本剰余金
503
+ *** 利益剰余金
504
+
505
+ @endwbs
506
+ ```
507
+
508
+ ### 20.3.2 主要勘定科目一覧
509
+
510
+ **資産(BS借方)**
511
+
512
+ | コード | 科目名 | 補助科目 | 説明 |
513
+ |--------|--------|----------|------|
514
+ | 11110 | 現金 | - | 手元現金 |
515
+ | 11130 | 普通預金 | 必須 | 銀行別管理 |
516
+ | 11210 | 売掛金 | 必須 | 得意先別管理 |
517
+ | 11330 | 原材料 | 必須 | 品目別管理 |
518
+ | 11430 | 仮払消費税 | - | 消費税処理用 |
519
+
520
+ **負債・純資産・収益・費用**
521
+
522
+ | コード | 科目名 | 消費税 | 説明 |
523
+ |--------|--------|--------|------|
524
+ | 21110 | 買掛金 | - | 仕入先別管理 |
525
+ | 21240 | 仮受消費税 | - | 消費税処理用 |
526
+ | 33200 | 繰越利益剰余金 | - | 留保利益 |
527
+ | 41110 | 国内売上高 | 課税10% | 国内向け売上 |
528
+ | 62100 | 旅費交通費 | 課税10% | 出張費等 |
529
+
530
+ ### 20.3.3 会計処理のパターン
531
+
532
+ ```plantuml
533
+ @startuml
534
+
535
+ title D社 主要な取引と仕訳パターン
536
+
537
+ left to right direction
538
+
539
+ rectangle "販売取引" {
540
+ card sales [
541
+ **売上計上**
542
+ ----
543
+ 借方: 売掛金
544
+ 貸方: 売上高
545
+ 貸方: 仮受消費税
546
+ ]
547
+
548
+ card receipt [
549
+ **入金**
550
+ ----
551
+ 借方: 普通預金
552
+ 貸方: 売掛金
553
+ ]
554
+ }
555
+
556
+ rectangle "製造取引" {
557
+ card material [
558
+ **材料仕入**
559
+ ----
560
+ 借方: 原材料
561
+ 借方: 仮払消費税
562
+ 貸方: 買掛金
563
+ ]
564
+
565
+ card expense [
566
+ **製造経費**
567
+ ----
568
+ 借方: 製造間接費
569
+ 借方: 仮払消費税
570
+ 貸方: 未払金
571
+ ]
572
+ }
573
+
574
+ rectangle "一般取引" {
575
+ card exp [
576
+ **経費精算**
577
+ ----
578
+ 借方: 旅費交通費/消耗品費
579
+ 借方: 仮払消費税
580
+ 貸方: 現金/未払金
581
+ ]
582
+
583
+ card salary [
584
+ **給与支払**
585
+ ----
586
+ 借方: 給料手当
587
+ 貸方: 普通預金
588
+ 貸方: 預り金(税・社保)
589
+ ]
590
+ }
591
+
592
+ @enduml
593
+ ```
594
+
595
+ ## 20.4 Seed データ投入サービス(JPA版)
596
+
597
+ ### 20.4.1 SeedDataService
598
+
599
+ JPA の CascadeType.ALL を活用し、仕訳と明細を一括保存します。
600
+
601
+ <details>
602
+ <summary>SeedDataService(JPA版)</summary>
603
+
604
+ ```java
605
+ // src/main/java/com/example/accounting/infrastructure/seed/SeedDataService.java
606
+ package com.example.accounting.infrastructure.seed;
607
+
608
+ import com.example.accounting.domain.model.account.*;
609
+ import com.example.accounting.domain.model.department.*;
610
+ import com.example.accounting.domain.model.journal.*;
611
+ import com.example.accounting.domain.model.tax.*;
612
+ import com.example.accounting.infrastructure.persistence.repository.*;
613
+ import lombok.RequiredArgsConstructor;
614
+ import lombok.extern.slf4j.Slf4j;
615
+ import org.springframework.stereotype.Service;
616
+ import org.springframework.transaction.annotation.Transactional;
617
+
618
+ import java.math.BigDecimal;
619
+ import java.time.LocalDate;
620
+ import java.util.ArrayList;
621
+ import java.util.List;
622
+
623
+ /**
624
+ * Seed データ投入サービス(JPA 版)
625
+ */
626
+ @Service
627
+ @RequiredArgsConstructor
628
+ @Slf4j
629
+ public class SeedDataService {
630
+
631
+ private final AccountJpaRepository accountRepository;
632
+ private final AccountStructureJpaRepository accountStructureRepository;
633
+ private final TaxTransactionJpaRepository taxTransactionRepository;
634
+ private final DepartmentJpaRepository departmentRepository;
635
+ private final JournalJpaRepository journalRepository;
636
+
637
+ /**
638
+ * 全Seedデータを投入
639
+ */
640
+ @Transactional
641
+ public void seedAll() {
642
+ log.info("Starting seed data insertion...");
643
+
644
+ seedTaxTransactions();
645
+ seedAccountMasters();
646
+ seedAccountStructures();
647
+ seedDepartments();
648
+ seedSampleJournals();
649
+
650
+ log.info("Seed data insertion completed.");
651
+ }
652
+
653
+ /**
654
+ * 課税取引マスタのSeedデータ
655
+ */
656
+ @Transactional
657
+ public void seedTaxTransactions() {
658
+ log.info("Seeding tax transactions...");
659
+
660
+ List<TaxTransaction> taxes = List.of(
661
+ TaxTransaction.builder()
662
+ .taxCode("00").taxName("免税").taxRate(BigDecimal.ZERO).build(),
663
+ TaxTransaction.builder()
664
+ .taxCode("08").taxName("軽減税率8%").taxRate(new BigDecimal("0.080")).build(),
665
+ TaxTransaction.builder()
666
+ .taxCode("10").taxName("標準税率10%").taxRate(new BigDecimal("0.100")).build(),
667
+ TaxTransaction.builder()
668
+ .taxCode("80").taxName("非課税").taxRate(BigDecimal.ZERO).build(),
669
+ TaxTransaction.builder()
670
+ .taxCode("99").taxName("対象外").taxRate(BigDecimal.ZERO).build()
671
+ );
672
+
673
+ int insertedCount = 0;
674
+ for (TaxTransaction tax : taxes) {
675
+ if (!taxTransactionRepository.existsById(tax.getTaxCode())) {
676
+ taxTransactionRepository.save(tax);
677
+ insertedCount++;
678
+ }
679
+ }
680
+
681
+ log.info("Tax transactions seeded: {} records", insertedCount);
682
+ }
683
+
684
+ /**
685
+ * 勘定科目マスタのSeedデータ
686
+ */
687
+ @Transactional
688
+ public void seedAccountMasters() {
689
+ log.info("Seeding account masters...");
690
+
691
+ List<Account> accounts = createAccountMasterSeedData();
692
+
693
+ int insertedCount = 0;
694
+ for (Account account : accounts) {
695
+ if (!accountRepository.existsById(account.getAccountCode())) {
696
+ accountRepository.save(account);
697
+ insertedCount++;
698
+ }
699
+ }
700
+
701
+ log.info("Account masters seeded: {} records", insertedCount);
702
+ }
703
+
704
+ /**
705
+ * 勘定科目構成マスタのSeedデータ
706
+ */
707
+ @Transactional
708
+ public void seedAccountStructures() {
709
+ log.info("Seeding account structures...");
710
+
711
+ List<Account> accounts = accountRepository.findAll();
712
+
713
+ int insertedCount = 0;
714
+ for (Account account : accounts) {
715
+ if (!accountStructureRepository.existsById(account.getAccountCode())) {
716
+ String path = generateAccountPath(account);
717
+ AccountStructure structure = AccountStructure.builder()
718
+ .accountCode(account.getAccountCode())
719
+ .accountPath(path)
720
+ .account(account)
721
+ .build();
722
+ accountStructureRepository.save(structure);
723
+ insertedCount++;
724
+ }
725
+ }
726
+
727
+ log.info("Account structures seeded: {} records", insertedCount);
728
+ }
729
+
730
+ /**
731
+ * 部門マスタのSeedデータ
732
+ */
733
+ @Transactional
734
+ public void seedDepartments() {
735
+ log.info("Seeding departments...");
736
+
737
+ List<Department> departments = createDepartmentSeedData();
738
+
739
+ int insertedCount = 0;
740
+ for (Department dept : departments) {
741
+ if (!departmentRepository.existsById(dept.getDepartmentCode())) {
742
+ departmentRepository.save(dept);
743
+ insertedCount++;
744
+ }
745
+ }
746
+
747
+ log.info("Departments seeded: {} records", insertedCount);
748
+ }
749
+
750
+ /**
751
+ * サンプル仕訳データのSeed
752
+ */
753
+ @Transactional
754
+ public void seedSampleJournals() {
755
+ log.info("Seeding sample journals...");
756
+
757
+ // 期首仕訳
758
+ createOpeningJournal();
759
+
760
+ // 売上仕訳
761
+ createSalesJournal();
762
+
763
+ // 経費仕訳
764
+ createExpenseJournal();
765
+
766
+ // 材料仕入仕訳
767
+ createMaterialPurchaseJournal();
768
+
769
+ // 給与支払仕訳
770
+ createSalaryJournal();
771
+
772
+ // 入金仕訳
773
+ createReceiptJournal();
774
+
775
+ // 支払仕訳
776
+ createPaymentJournal();
777
+
778
+ log.info("Sample journals seeded.");
779
+ }
780
+
781
+ private void createOpeningJournal() {
782
+ String journalNumber = "J2025040001";
783
+
784
+ if (journalRepository.existsById(journalNumber)) {
785
+ return;
786
+ }
787
+
788
+ LocalDate date = LocalDate.of(2025, 4, 1);
789
+
790
+ // 仕訳ヘッダ
791
+ Journal journal = Journal.builder()
792
+ .journalNumber(journalNumber)
793
+ .postingDate(date)
794
+ .inputDate(date)
795
+ .closingJournalFlag(0)
796
+ .singleFlag(0)
797
+ .journalSlipType(JournalSlipType.NORMAL)
798
+ .departmentCode("13100")
799
+ .build();
800
+
801
+ // 仕訳明細
802
+ JournalDetail detail = JournalDetail.builder()
803
+ .lineNumber(1)
804
+ .lineDescription("前期繰越")
805
+ .build();
806
+ detail.setJournal(journal);
807
+
808
+ // 借方:普通預金
809
+ JournalDcDetail debit = JournalDcDetail.builder()
810
+ .dcType(DcType.DEBIT)
811
+ .accountCode("11130")
812
+ .amount(new BigDecimal("50000000"))
813
+ .build();
814
+ debit.setJournalDetail(detail);
815
+
816
+ // 貸方:繰越利益剰余金
817
+ JournalDcDetail credit = JournalDcDetail.builder()
818
+ .dcType(DcType.CREDIT)
819
+ .accountCode("33200")
820
+ .amount(new BigDecimal("50000000"))
821
+ .build();
822
+ credit.setJournalDetail(detail);
823
+
824
+ // 関連付け
825
+ detail.addDcDetail(debit);
826
+ detail.addDcDetail(credit);
827
+ journal.addDetail(detail);
828
+
829
+ // CascadeType.ALL により一括保存
830
+ journalRepository.save(journal);
831
+ }
832
+
833
+ private void createSalesJournal() {
834
+ String journalNumber = "J2025040002";
835
+
836
+ if (journalRepository.existsById(journalNumber)) {
837
+ return;
838
+ }
839
+
840
+ LocalDate date = LocalDate.of(2025, 4, 5);
841
+
842
+ Journal journal = Journal.builder()
843
+ .journalNumber(journalNumber)
844
+ .postingDate(date)
845
+ .inputDate(date)
846
+ .closingJournalFlag(0)
847
+ .singleFlag(0)
848
+ .journalSlipType(JournalSlipType.AUTO)
849
+ .departmentCode("11110")
850
+ .build();
851
+
852
+ JournalDetail detail = JournalDetail.builder()
853
+ .lineNumber(1)
854
+ .lineDescription("ABC商事 スキンケアセット 100個")
855
+ .build();
856
+ detail.setJournal(journal);
857
+
858
+ // 借方:売掛金
859
+ JournalDcDetail debit = JournalDcDetail.builder()
860
+ .dcType(DcType.DEBIT)
861
+ .accountCode("11210")
862
+ .subAccountCode("ABC001")
863
+ .amount(new BigDecimal("330000"))
864
+ .build();
865
+ debit.setJournalDetail(detail);
866
+
867
+ // 貸方:国内売上高
868
+ JournalDcDetail creditSales = JournalDcDetail.builder()
869
+ .dcType(DcType.CREDIT)
870
+ .accountCode("41110")
871
+ .subAccountCode("ABC001")
872
+ .amount(new BigDecimal("300000"))
873
+ .taxType(TaxType.TAXABLE_SALES)
874
+ .taxRate(10)
875
+ .build();
876
+ creditSales.setJournalDetail(detail);
877
+
878
+ // 貸方:仮受消費税
879
+ JournalDcDetail creditTax = JournalDcDetail.builder()
880
+ .dcType(DcType.CREDIT)
881
+ .accountCode("21240")
882
+ .amount(new BigDecimal("30000"))
883
+ .build();
884
+ creditTax.setJournalDetail(detail);
885
+
886
+ detail.addDcDetail(debit);
887
+ detail.addDcDetail(creditSales);
888
+ detail.addDcDetail(creditTax);
889
+ journal.addDetail(detail);
890
+
891
+ journalRepository.save(journal);
892
+ }
893
+
894
+ private void createExpenseJournal() {
895
+ String journalNumber = "J2025040003";
896
+
897
+ if (journalRepository.existsById(journalNumber)) {
898
+ return;
899
+ }
900
+
901
+ LocalDate date = LocalDate.of(2025, 4, 10);
902
+
903
+ Journal journal = Journal.builder()
904
+ .journalNumber(journalNumber)
905
+ .postingDate(date)
906
+ .inputDate(date)
907
+ .closingJournalFlag(0)
908
+ .singleFlag(0)
909
+ .journalSlipType(JournalSlipType.NORMAL)
910
+ .departmentCode("11110")
911
+ .build();
912
+
913
+ JournalDetail detail = JournalDetail.builder()
914
+ .lineNumber(1)
915
+ .lineDescription("出張旅費 東京→大阪")
916
+ .build();
917
+ detail.setJournal(journal);
918
+
919
+ // 借方:旅費交通費
920
+ JournalDcDetail debitExp = JournalDcDetail.builder()
921
+ .dcType(DcType.DEBIT)
922
+ .accountCode("62100")
923
+ .amount(new BigDecimal("27273"))
924
+ .taxType(TaxType.TAXABLE_PURCHASE)
925
+ .taxRate(10)
926
+ .build();
927
+ debitExp.setJournalDetail(detail);
928
+
929
+ // 借方:仮払消費税
930
+ JournalDcDetail debitTax = JournalDcDetail.builder()
931
+ .dcType(DcType.DEBIT)
932
+ .accountCode("11430")
933
+ .amount(new BigDecimal("2727"))
934
+ .build();
935
+ debitTax.setJournalDetail(detail);
936
+
937
+ // 貸方:現金
938
+ JournalDcDetail credit = JournalDcDetail.builder()
939
+ .dcType(DcType.CREDIT)
940
+ .accountCode("11110")
941
+ .amount(new BigDecimal("30000"))
942
+ .build();
943
+ credit.setJournalDetail(detail);
944
+
945
+ detail.addDcDetail(debitExp);
946
+ detail.addDcDetail(debitTax);
947
+ detail.addDcDetail(credit);
948
+ journal.addDetail(detail);
949
+
950
+ journalRepository.save(journal);
951
+ }
952
+
953
+ private void createMaterialPurchaseJournal() {
954
+ String journalNumber = "J2025040004";
955
+
956
+ if (journalRepository.existsById(journalNumber)) {
957
+ return;
958
+ }
959
+
960
+ LocalDate date = LocalDate.of(2025, 4, 15);
961
+
962
+ Journal journal = Journal.builder()
963
+ .journalNumber(journalNumber)
964
+ .postingDate(date)
965
+ .inputDate(date)
966
+ .closingJournalFlag(0)
967
+ .singleFlag(0)
968
+ .journalSlipType(JournalSlipType.AUTO)
969
+ .departmentCode("12110")
970
+ .build();
971
+
972
+ JournalDetail detail = JournalDetail.builder()
973
+ .lineNumber(1)
974
+ .lineDescription("XYZ化学 原材料仕入")
975
+ .build();
976
+ detail.setJournal(journal);
977
+
978
+ // 借方:原材料
979
+ JournalDcDetail debitMat = JournalDcDetail.builder()
980
+ .dcType(DcType.DEBIT)
981
+ .accountCode("11330")
982
+ .subAccountCode("MAT001")
983
+ .amount(new BigDecimal("500000"))
984
+ .taxType(TaxType.TAXABLE_PURCHASE)
985
+ .taxRate(10)
986
+ .build();
987
+ debitMat.setJournalDetail(detail);
988
+
989
+ // 借方:仮払消費税
990
+ JournalDcDetail debitTax = JournalDcDetail.builder()
991
+ .dcType(DcType.DEBIT)
992
+ .accountCode("11430")
993
+ .amount(new BigDecimal("50000"))
994
+ .build();
995
+ debitTax.setJournalDetail(detail);
996
+
997
+ // 貸方:買掛金
998
+ JournalDcDetail credit = JournalDcDetail.builder()
999
+ .dcType(DcType.CREDIT)
1000
+ .accountCode("21110")
1001
+ .subAccountCode("XYZ001")
1002
+ .amount(new BigDecimal("550000"))
1003
+ .build();
1004
+ credit.setJournalDetail(detail);
1005
+
1006
+ detail.addDcDetail(debitMat);
1007
+ detail.addDcDetail(debitTax);
1008
+ detail.addDcDetail(credit);
1009
+ journal.addDetail(detail);
1010
+
1011
+ journalRepository.save(journal);
1012
+ }
1013
+
1014
+ private void createSalaryJournal() {
1015
+ String journalNumber = "J2025040005";
1016
+
1017
+ if (journalRepository.existsById(journalNumber)) {
1018
+ return;
1019
+ }
1020
+
1021
+ LocalDate date = LocalDate.of(2025, 4, 20);
1022
+
1023
+ Journal journal = Journal.builder()
1024
+ .journalNumber(journalNumber)
1025
+ .postingDate(date)
1026
+ .inputDate(date)
1027
+ .closingJournalFlag(0)
1028
+ .singleFlag(0)
1029
+ .journalSlipType(JournalSlipType.NORMAL)
1030
+ .departmentCode("13100")
1031
+ .build();
1032
+
1033
+ JournalDetail detail = JournalDetail.builder()
1034
+ .lineNumber(1)
1035
+ .lineDescription("4月度給与")
1036
+ .build();
1037
+ detail.setJournal(journal);
1038
+
1039
+ // 借方:給料手当
1040
+ JournalDcDetail debit = JournalDcDetail.builder()
1041
+ .dcType(DcType.DEBIT)
1042
+ .accountCode("61200")
1043
+ .amount(new BigDecimal("10000000"))
1044
+ .build();
1045
+ debit.setJournalDetail(detail);
1046
+
1047
+ // 貸方:普通預金
1048
+ JournalDcDetail creditBank = JournalDcDetail.builder()
1049
+ .dcType(DcType.CREDIT)
1050
+ .accountCode("11130")
1051
+ .amount(new BigDecimal("8500000"))
1052
+ .build();
1053
+ creditBank.setJournalDetail(detail);
1054
+
1055
+ // 貸方:預り金
1056
+ JournalDcDetail creditDeposit = JournalDcDetail.builder()
1057
+ .dcType(DcType.CREDIT)
1058
+ .accountCode("21250")
1059
+ .amount(new BigDecimal("1500000"))
1060
+ .build();
1061
+ creditDeposit.setJournalDetail(detail);
1062
+
1063
+ detail.addDcDetail(debit);
1064
+ detail.addDcDetail(creditBank);
1065
+ detail.addDcDetail(creditDeposit);
1066
+ journal.addDetail(detail);
1067
+
1068
+ journalRepository.save(journal);
1069
+ }
1070
+
1071
+ private void createReceiptJournal() {
1072
+ String journalNumber = "J2025040006";
1073
+
1074
+ if (journalRepository.existsById(journalNumber)) {
1075
+ return;
1076
+ }
1077
+
1078
+ LocalDate date = LocalDate.of(2025, 4, 25);
1079
+
1080
+ Journal journal = Journal.builder()
1081
+ .journalNumber(journalNumber)
1082
+ .postingDate(date)
1083
+ .inputDate(date)
1084
+ .closingJournalFlag(0)
1085
+ .singleFlag(0)
1086
+ .journalSlipType(JournalSlipType.NORMAL)
1087
+ .departmentCode("13100")
1088
+ .build();
1089
+
1090
+ JournalDetail detail = JournalDetail.builder()
1091
+ .lineNumber(1)
1092
+ .lineDescription("ABC商事 入金")
1093
+ .build();
1094
+ detail.setJournal(journal);
1095
+
1096
+ // 借方:普通預金
1097
+ JournalDcDetail debit = JournalDcDetail.builder()
1098
+ .dcType(DcType.DEBIT)
1099
+ .accountCode("11130")
1100
+ .amount(new BigDecimal("330000"))
1101
+ .build();
1102
+ debit.setJournalDetail(detail);
1103
+
1104
+ // 貸方:売掛金
1105
+ JournalDcDetail credit = JournalDcDetail.builder()
1106
+ .dcType(DcType.CREDIT)
1107
+ .accountCode("11210")
1108
+ .subAccountCode("ABC001")
1109
+ .amount(new BigDecimal("330000"))
1110
+ .build();
1111
+ credit.setJournalDetail(detail);
1112
+
1113
+ detail.addDcDetail(debit);
1114
+ detail.addDcDetail(credit);
1115
+ journal.addDetail(detail);
1116
+
1117
+ journalRepository.save(journal);
1118
+ }
1119
+
1120
+ private void createPaymentJournal() {
1121
+ String journalNumber = "J2025040007";
1122
+
1123
+ if (journalRepository.existsById(journalNumber)) {
1124
+ return;
1125
+ }
1126
+
1127
+ LocalDate date = LocalDate.of(2025, 4, 30);
1128
+
1129
+ Journal journal = Journal.builder()
1130
+ .journalNumber(journalNumber)
1131
+ .postingDate(date)
1132
+ .inputDate(date)
1133
+ .closingJournalFlag(0)
1134
+ .singleFlag(0)
1135
+ .journalSlipType(JournalSlipType.NORMAL)
1136
+ .departmentCode("13100")
1137
+ .build();
1138
+
1139
+ JournalDetail detail = JournalDetail.builder()
1140
+ .lineNumber(1)
1141
+ .lineDescription("XYZ化学 支払")
1142
+ .build();
1143
+ detail.setJournal(journal);
1144
+
1145
+ // 借方:買掛金
1146
+ JournalDcDetail debit = JournalDcDetail.builder()
1147
+ .dcType(DcType.DEBIT)
1148
+ .accountCode("21110")
1149
+ .subAccountCode("XYZ001")
1150
+ .amount(new BigDecimal("550000"))
1151
+ .build();
1152
+ debit.setJournalDetail(detail);
1153
+
1154
+ // 貸方:普通預金
1155
+ JournalDcDetail credit = JournalDcDetail.builder()
1156
+ .dcType(DcType.CREDIT)
1157
+ .accountCode("11130")
1158
+ .amount(new BigDecimal("550000"))
1159
+ .build();
1160
+ credit.setJournalDetail(detail);
1161
+
1162
+ detail.addDcDetail(debit);
1163
+ detail.addDcDetail(credit);
1164
+ journal.addDetail(detail);
1165
+
1166
+ journalRepository.save(journal);
1167
+ }
1168
+
1169
+ private List<Account> createAccountMasterSeedData() {
1170
+ List<Account> accounts = new ArrayList<>();
1171
+
1172
+ // 流動資産
1173
+ accounts.add(Account.builder()
1174
+ .accountCode("11110").accountName("現金").accountShortName("現金")
1175
+ .bsPlType(BsPlType.BS).dcType(AccountDcType.DEBIT).elementType(ElementType.ASSET)
1176
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.COMMON)
1177
+ .build());
1178
+
1179
+ accounts.add(Account.builder()
1180
+ .accountCode("11130").accountName("普通預金").accountShortName("普通")
1181
+ .bsPlType(BsPlType.BS).dcType(AccountDcType.DEBIT).elementType(ElementType.ASSET)
1182
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.COMMON)
1183
+ .subAccountType(SubAccountType.REQUIRED)
1184
+ .build());
1185
+
1186
+ accounts.add(Account.builder()
1187
+ .accountCode("11210").accountName("売掛金").accountShortName("売掛金")
1188
+ .bsPlType(BsPlType.BS).dcType(AccountDcType.DEBIT).elementType(ElementType.ASSET)
1189
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.COMMON)
1190
+ .subAccountType(SubAccountType.REQUIRED).dueDateManagementType(DueDateManagementType.REQUIRED)
1191
+ .build());
1192
+
1193
+ // 棚卸資産
1194
+ accounts.add(Account.builder()
1195
+ .accountCode("11330").accountName("原材料").accountShortName("原材料")
1196
+ .bsPlType(BsPlType.BS).dcType(AccountDcType.DEBIT).elementType(ElementType.ASSET)
1197
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.MANUFACTURING)
1198
+ .subAccountType(SubAccountType.REQUIRED)
1199
+ .build());
1200
+
1201
+ // 仮払消費税
1202
+ accounts.add(Account.builder()
1203
+ .accountCode("11430").accountName("仮払消費税").accountShortName("仮払税")
1204
+ .bsPlType(BsPlType.BS).dcType(AccountDcType.DEBIT).elementType(ElementType.ASSET)
1205
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.COMMON)
1206
+ .build());
1207
+
1208
+ // 負債
1209
+ accounts.add(Account.builder()
1210
+ .accountCode("21110").accountName("買掛金").accountShortName("買掛金")
1211
+ .bsPlType(BsPlType.BS).dcType(AccountDcType.CREDIT).elementType(ElementType.LIABILITY)
1212
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.COMMON)
1213
+ .subAccountType(SubAccountType.REQUIRED).dueDateManagementType(DueDateManagementType.REQUIRED)
1214
+ .build());
1215
+
1216
+ accounts.add(Account.builder()
1217
+ .accountCode("21240").accountName("仮受消費税").accountShortName("仮受税")
1218
+ .bsPlType(BsPlType.BS).dcType(AccountDcType.CREDIT).elementType(ElementType.LIABILITY)
1219
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.COMMON)
1220
+ .build());
1221
+
1222
+ accounts.add(Account.builder()
1223
+ .accountCode("21250").accountName("預り金").accountShortName("預り金")
1224
+ .bsPlType(BsPlType.BS).dcType(AccountDcType.CREDIT).elementType(ElementType.LIABILITY)
1225
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.COMMON)
1226
+ .build());
1227
+
1228
+ // 純資産
1229
+ accounts.add(Account.builder()
1230
+ .accountCode("33200").accountName("繰越利益剰余金").accountShortName("繰越利益")
1231
+ .bsPlType(BsPlType.BS).dcType(AccountDcType.CREDIT).elementType(ElementType.CAPITAL)
1232
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.COMMON)
1233
+ .build());
1234
+
1235
+ // 売上
1236
+ accounts.add(Account.builder()
1237
+ .accountCode("41110").accountName("国内売上高").accountShortName("国内売上")
1238
+ .bsPlType(BsPlType.PL).dcType(AccountDcType.CREDIT).elementType(ElementType.REVENUE)
1239
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.COMMON)
1240
+ .taxCalculationType(TaxCalculationType.SALES).taxCode("10")
1241
+ .build());
1242
+
1243
+ // 経費
1244
+ accounts.add(Account.builder()
1245
+ .accountCode("61200").accountName("給料手当").accountShortName("給料")
1246
+ .bsPlType(BsPlType.PL).dcType(AccountDcType.DEBIT).elementType(ElementType.EXPENSE)
1247
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.COMMON)
1248
+ .expenseType(ExpenseType.EXPENSE)
1249
+ .build());
1250
+
1251
+ accounts.add(Account.builder()
1252
+ .accountCode("62100").accountName("旅費交通費").accountShortName("旅費交通")
1253
+ .bsPlType(BsPlType.PL).dcType(AccountDcType.DEBIT).elementType(ElementType.EXPENSE)
1254
+ .summaryType(SummaryType.POSTING).managementType(ManagementType.COMMON)
1255
+ .expenseType(ExpenseType.EXPENSE)
1256
+ .taxCalculationType(TaxCalculationType.PURCHASE).taxCode("10")
1257
+ .build());
1258
+
1259
+ return accounts;
1260
+ }
1261
+
1262
+ private List<Department> createDepartmentSeedData() {
1263
+ return List.of(
1264
+ Department.builder()
1265
+ .departmentCode("10000").departmentName("全社")
1266
+ .organizationLevel(0).departmentPath("10000").lowestLevelFlag(0).build(),
1267
+ Department.builder()
1268
+ .departmentCode("11000").departmentName("営業本部")
1269
+ .organizationLevel(1).departmentPath("10000~11000").lowestLevelFlag(0).build(),
1270
+ Department.builder()
1271
+ .departmentCode("11110").departmentName("東日本営業課")
1272
+ .organizationLevel(3).departmentPath("10000~11000~11100~11110").lowestLevelFlag(1).build(),
1273
+ Department.builder()
1274
+ .departmentCode("12000").departmentName("製造本部")
1275
+ .organizationLevel(1).departmentPath("10000~12000").lowestLevelFlag(0).build(),
1276
+ Department.builder()
1277
+ .departmentCode("12110").departmentName("第一製造課")
1278
+ .organizationLevel(3).departmentPath("10000~12000~12100~12110").lowestLevelFlag(1).build(),
1279
+ Department.builder()
1280
+ .departmentCode("13000").departmentName("管理本部")
1281
+ .organizationLevel(1).departmentPath("10000~13000").lowestLevelFlag(0).build(),
1282
+ Department.builder()
1283
+ .departmentCode("13100").departmentName("経理部")
1284
+ .organizationLevel(2).departmentPath("10000~13000~13100").lowestLevelFlag(0).build()
1285
+ );
1286
+ }
1287
+
1288
+ private String generateAccountPath(Account account) {
1289
+ String code = account.getAccountCode();
1290
+
1291
+ // 簡易的なパス生成(実際はマスタの親子関係から生成)
1292
+ if (code.length() == 2) {
1293
+ return code;
1294
+ } else if (code.length() == 5) {
1295
+ String parent = code.substring(0, 2);
1296
+ return parent + "~" + code;
1297
+ }
1298
+ return code;
1299
+ }
1300
+ }
1301
+ ```
1302
+
1303
+ </details>
1304
+
1305
+ ### 20.4.2 CommandLineRunner による起動時データ投入
1306
+
1307
+ <details>
1308
+ <summary>SeedDataRunner(CommandLineRunner)</summary>
1309
+
1310
+ ```java
1311
+ // src/main/java/com/example/accounting/infrastructure/seed/SeedDataRunner.java
1312
+ package com.example.accounting.infrastructure.seed;
1313
+
1314
+ import lombok.RequiredArgsConstructor;
1315
+ import lombok.extern.slf4j.Slf4j;
1316
+ import org.springframework.boot.CommandLineRunner;
1317
+ import org.springframework.context.annotation.Profile;
1318
+ import org.springframework.stereotype.Component;
1319
+
1320
+ /**
1321
+ * アプリケーション起動時に Seed データを投入する
1322
+ *
1323
+ * dev, local プロファイル時のみ有効
1324
+ */
1325
+ @Component
1326
+ @Profile({"dev", "local"})
1327
+ @RequiredArgsConstructor
1328
+ @Slf4j
1329
+ public class SeedDataRunner implements CommandLineRunner {
1330
+
1331
+ private final SeedDataService seedDataService;
1332
+
1333
+ @Override
1334
+ public void run(String... args) {
1335
+ log.info("=== Starting Seed Data Initialization ===");
1336
+
1337
+ try {
1338
+ seedDataService.seedAll();
1339
+ log.info("=== Seed Data Initialization Completed ===");
1340
+ } catch (Exception e) {
1341
+ log.error("=== Seed Data Initialization Failed ===", e);
1342
+ // 起動は継続させる(テストデータが無くても動作は可能)
1343
+ }
1344
+ }
1345
+ }
1346
+ ```
1347
+
1348
+ </details>
1349
+
1350
+ ### 20.4.3 @PostConstruct を使用した代替実装
1351
+
1352
+ <details>
1353
+ <summary>SeedDataInitializer(@PostConstruct)</summary>
1354
+
1355
+ ```java
1356
+ // src/main/java/com/example/accounting/infrastructure/seed/SeedDataInitializer.java
1357
+ package com.example.accounting.infrastructure.seed;
1358
+
1359
+ import jakarta.annotation.PostConstruct;
1360
+ import lombok.RequiredArgsConstructor;
1361
+ import lombok.extern.slf4j.Slf4j;
1362
+ import org.springframework.context.annotation.Profile;
1363
+ import org.springframework.stereotype.Component;
1364
+
1365
+ /**
1366
+ * @PostConstruct を使用した Seed データ投入
1367
+ *
1368
+ * CommandLineRunner より早いタイミングで実行される
1369
+ */
1370
+ @Component
1371
+ @Profile({"dev", "local"})
1372
+ @RequiredArgsConstructor
1373
+ @Slf4j
1374
+ public class SeedDataInitializer {
1375
+
1376
+ private final SeedDataService seedDataService;
1377
+
1378
+ @PostConstruct
1379
+ public void initialize() {
1380
+ log.info("=== @PostConstruct Seed Data Initialization ===");
1381
+
1382
+ try {
1383
+ seedDataService.seedAll();
1384
+ log.info("=== Seed Data Initialization Completed ===");
1385
+ } catch (Exception e) {
1386
+ log.error("=== Seed Data Initialization Failed ===", e);
1387
+ }
1388
+ }
1389
+ }
1390
+ ```
1391
+
1392
+ </details>
1393
+
1394
+ ## 20.5 Seed データ投入テスト
1395
+
1396
+ <details>
1397
+ <summary>SeedDataServiceTest(JPA版)</summary>
1398
+
1399
+ ```java
1400
+ // src/test/java/com/example/accounting/infrastructure/seed/SeedDataServiceTest.java
1401
+ package com.example.accounting.infrastructure.seed;
1402
+
1403
+ import com.example.accounting.domain.model.account.*;
1404
+ import com.example.accounting.domain.model.journal.*;
1405
+ import com.example.accounting.infrastructure.persistence.repository.*;
1406
+ import org.junit.jupiter.api.*;
1407
+ import org.springframework.beans.factory.annotation.Autowired;
1408
+ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
1409
+ import org.springframework.context.annotation.Import;
1410
+ import org.springframework.test.context.DynamicPropertyRegistry;
1411
+ import org.springframework.test.context.DynamicPropertySource;
1412
+ import org.testcontainers.containers.PostgreSQLContainer;
1413
+ import org.testcontainers.junit.jupiter.Container;
1414
+ import org.testcontainers.junit.jupiter.Testcontainers;
1415
+
1416
+ import java.math.BigDecimal;
1417
+
1418
+ import static org.assertj.core.api.Assertions.*;
1419
+
1420
+ @DataJpaTest
1421
+ @Import(SeedDataService.class)
1422
+ @Testcontainers
1423
+ @DisplayName("Seedデータ投入(JPA)")
1424
+ class SeedDataServiceTest {
1425
+
1426
+ @Container
1427
+ static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16")
1428
+ .withDatabaseName("testdb")
1429
+ .withUsername("testuser")
1430
+ .withPassword("testpass");
1431
+
1432
+ @DynamicPropertySource
1433
+ static void configureProperties(DynamicPropertyRegistry registry) {
1434
+ registry.add("spring.datasource.url", postgres::getJdbcUrl);
1435
+ registry.add("spring.datasource.username", postgres::getUsername);
1436
+ registry.add("spring.datasource.password", postgres::getPassword);
1437
+ }
1438
+
1439
+ @Autowired
1440
+ private SeedDataService seedDataService;
1441
+
1442
+ @Autowired
1443
+ private AccountJpaRepository accountRepository;
1444
+
1445
+ @Autowired
1446
+ private TaxTransactionJpaRepository taxTransactionRepository;
1447
+
1448
+ @Autowired
1449
+ private DepartmentJpaRepository departmentRepository;
1450
+
1451
+ @Autowired
1452
+ private JournalJpaRepository journalRepository;
1453
+
1454
+ @Nested
1455
+ @DisplayName("課税取引マスタ")
1456
+ class TaxTransactionSeed {
1457
+
1458
+ @Test
1459
+ @DisplayName("課税取引マスタのSeedデータを投入できる")
1460
+ void canSeedTaxTransactions() {
1461
+ // Act
1462
+ seedDataService.seedTaxTransactions();
1463
+
1464
+ // Assert
1465
+ var tax10 = taxTransactionRepository.findById("10");
1466
+ assertThat(tax10).isPresent();
1467
+ assertThat(tax10.get().getTaxName()).isEqualTo("標準税率10%");
1468
+ assertThat(tax10.get().getTaxRate()).isEqualByComparingTo(new BigDecimal("0.100"));
1469
+
1470
+ var tax08 = taxTransactionRepository.findById("08");
1471
+ assertThat(tax08).isPresent();
1472
+ assertThat(tax08.get().getTaxName()).isEqualTo("軽減税率8%");
1473
+ }
1474
+ }
1475
+
1476
+ @Nested
1477
+ @DisplayName("勘定科目マスタ")
1478
+ class AccountMasterSeed {
1479
+
1480
+ @BeforeEach
1481
+ void setUp() {
1482
+ seedDataService.seedTaxTransactions();
1483
+ }
1484
+
1485
+ @Test
1486
+ @DisplayName("勘定科目マスタのSeedデータを投入できる")
1487
+ void canSeedAccountMasters() {
1488
+ // Act
1489
+ seedDataService.seedAccountMasters();
1490
+
1491
+ // Assert
1492
+ var cash = accountRepository.findById("11110");
1493
+ assertThat(cash).isPresent();
1494
+ assertThat(cash.get().getAccountName()).isEqualTo("現金");
1495
+ assertThat(cash.get().getBsPlType()).isEqualTo(BsPlType.BS);
1496
+ assertThat(cash.get().getDcType()).isEqualTo(AccountDcType.DEBIT);
1497
+
1498
+ var sales = accountRepository.findById("41110");
1499
+ assertThat(sales).isPresent();
1500
+ assertThat(sales.get().getAccountName()).isEqualTo("国内売上高");
1501
+ assertThat(sales.get().getBsPlType()).isEqualTo(BsPlType.PL);
1502
+ assertThat(sales.get().getDcType()).isEqualTo(AccountDcType.CREDIT);
1503
+ }
1504
+
1505
+ @Test
1506
+ @DisplayName("重複実行しても既存データが上書きされない(冪等性)")
1507
+ void seedIsIdempotent() {
1508
+ // Act
1509
+ seedDataService.seedAccountMasters();
1510
+ seedDataService.seedAccountMasters(); // 2回目
1511
+
1512
+ // Assert
1513
+ var accounts = accountRepository.findAll();
1514
+ long cashCount = accounts.stream()
1515
+ .filter(a -> "11110".equals(a.getAccountCode()))
1516
+ .count();
1517
+ assertThat(cashCount).isEqualTo(1);
1518
+ }
1519
+ }
1520
+
1521
+ @Nested
1522
+ @DisplayName("部門マスタ")
1523
+ class DepartmentSeed {
1524
+
1525
+ @Test
1526
+ @DisplayName("部門マスタのSeedデータを投入できる")
1527
+ void canSeedDepartments() {
1528
+ // Act
1529
+ seedDataService.seedDepartments();
1530
+
1531
+ // Assert
1532
+ var company = departmentRepository.findById("10000");
1533
+ assertThat(company).isPresent();
1534
+ assertThat(company.get().getDepartmentName()).isEqualTo("全社");
1535
+ assertThat(company.get().getOrganizationLevel()).isEqualTo(0);
1536
+
1537
+ var sales = departmentRepository.findById("11110");
1538
+ assertThat(sales).isPresent();
1539
+ assertThat(sales.get().getDepartmentName()).isEqualTo("東日本営業課");
1540
+ assertThat(sales.get().getDepartmentPath()).isEqualTo("10000~11000~11100~11110");
1541
+ }
1542
+ }
1543
+
1544
+ @Nested
1545
+ @DisplayName("仕訳データ")
1546
+ class JournalSeed {
1547
+
1548
+ @BeforeEach
1549
+ void setUp() {
1550
+ seedDataService.seedTaxTransactions();
1551
+ seedDataService.seedAccountMasters();
1552
+ seedDataService.seedDepartments();
1553
+ }
1554
+
1555
+ @Test
1556
+ @DisplayName("サンプル仕訳データを投入できる")
1557
+ void canSeedSampleJournals() {
1558
+ // Act
1559
+ seedDataService.seedSampleJournals();
1560
+
1561
+ // Assert
1562
+ // 期首仕訳
1563
+ var opening = journalRepository.findById("J2025040001");
1564
+ assertThat(opening).isPresent();
1565
+ assertThat(opening.get().getJournalSlipType()).isEqualTo(JournalSlipType.NORMAL);
1566
+
1567
+ // 売上仕訳
1568
+ var sales = journalRepository.findById("J2025040002");
1569
+ assertThat(sales).isPresent();
1570
+ assertThat(sales.get().getJournalSlipType()).isEqualTo(JournalSlipType.AUTO);
1571
+
1572
+ // CascadeType.ALL により明細も保存されていることを確認
1573
+ var salesWithDetails = journalRepository.findWithDetailsById("J2025040002");
1574
+ assertThat(salesWithDetails).isPresent();
1575
+ assertThat(salesWithDetails.get().getDetails()).hasSize(1);
1576
+ }
1577
+
1578
+ @Test
1579
+ @DisplayName("仕訳の貸借一致を確認できる")
1580
+ void journalBalanceIsCorrect() {
1581
+ // Arrange
1582
+ seedDataService.seedSampleJournals();
1583
+
1584
+ // Act
1585
+ var journal = journalRepository.findWithDetailsById("J2025040002").orElseThrow();
1586
+
1587
+ // Assert
1588
+ var detail = journal.getDetails().get(0);
1589
+ BigDecimal debitSum = detail.getDcDetails().stream()
1590
+ .filter(d -> d.getDcType() == DcType.DEBIT)
1591
+ .map(JournalDcDetail::getAmount)
1592
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
1593
+ BigDecimal creditSum = detail.getDcDetails().stream()
1594
+ .filter(d -> d.getDcType() == DcType.CREDIT)
1595
+ .map(JournalDcDetail::getAmount)
1596
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
1597
+
1598
+ assertThat(debitSum).isEqualByComparingTo(creditSum);
1599
+ assertThat(debitSum).isEqualByComparingTo(new BigDecimal("330000"));
1600
+ }
1601
+ }
1602
+
1603
+ @Nested
1604
+ @DisplayName("全データ一括投入")
1605
+ class SeedAll {
1606
+
1607
+ @Test
1608
+ @DisplayName("全Seedデータを一括投入できる")
1609
+ void canSeedAll() {
1610
+ // Act
1611
+ seedDataService.seedAll();
1612
+
1613
+ // Assert
1614
+ assertThat(taxTransactionRepository.findById("10")).isPresent();
1615
+ assertThat(accountRepository.findById("11110")).isPresent();
1616
+ assertThat(departmentRepository.findById("10000")).isPresent();
1617
+ assertThat(journalRepository.findById("J2025040001")).isPresent();
1618
+ }
1619
+ }
1620
+ }
1621
+ ```
1622
+
1623
+ </details>
1624
+
1625
+ ## 20.6 データ投入順序
1626
+
1627
+ 外部キー制約を考慮したデータ投入順序:
1628
+
1629
+ ```plantuml
1630
+ @startuml
1631
+
1632
+ title Seedデータ投入順序(JPA CascadeType.ALL 活用)
1633
+
1634
+ |マスタデータ|
1635
+ start
1636
+ :課税取引マスタ;
1637
+ note right: 外部キー参照元\nFK制約なし
1638
+ :勘定科目マスタ;
1639
+ note right: 課税取引マスタを参照
1640
+ :勘定科目構成マスタ;
1641
+ note right: 勘定科目マスタを参照\n@OneToOne 関連
1642
+ :部門マスタ;
1643
+ :通貨マスタ;
1644
+
1645
+ |トランザクションデータ|
1646
+ :仕訳データ;
1647
+ note right
1648
+ CascadeType.ALL により
1649
+ 明細も一括保存
1650
+ @OneToMany(cascade = ALL)
1651
+ end note
1652
+
1653
+ |残高データ|
1654
+ :日次勘定科目残高;
1655
+ note right: 仕訳から集計
1656
+ :月次勘定科目残高;
1657
+ note right: 日次残高から集計
1658
+
1659
+ stop
1660
+
1661
+ @enduml
1662
+ ```
1663
+
1664
+ ## まとめ
1665
+
1666
+ ### MyBatis 版との違い
1667
+
1668
+ | 観点 | MyBatis 版 | JPA 版 |
1669
+ |------|-----------|--------|
1670
+ | Repository | Mapper インターフェース + XML | JpaRepository インターフェース |
1671
+ | マッピング | resultMap で日本語カラム ↔ 英語プロパティ | @Column で日本語カラム名を指定 |
1672
+ | 関連の保存 | 個別に INSERT | CascadeType.ALL で一括保存 |
1673
+ | テスト | @MybatisTest | @DataJpaTest |
1674
+ | クエリメソッド | XML に SQL を記述 | メソッド名規約 or @Query |
1675
+ | 存在確認 | findByCode() == null | existsById() |
1676
+
1677
+ ### JPA 固有のポイント
1678
+
1679
+ 1. **CascadeType.ALL によるカスケード保存**
1680
+ ```java
1681
+ @OneToMany(mappedBy = "journal", cascade = CascadeType.ALL, orphanRemoval = true)
1682
+ private List<JournalDetail> details = new ArrayList<>();
1683
+ ```
1684
+
1685
+ 2. **@PrePersist / @PreUpdate による自動タイムスタンプ**
1686
+ ```java
1687
+ @PrePersist
1688
+ protected void onCreate() {
1689
+ if (createdAt == null) {
1690
+ createdAt = LocalDateTime.now();
1691
+ }
1692
+ }
1693
+ ```
1694
+
1695
+ 3. **existsById() による存在確認**
1696
+ ```java
1697
+ if (!accountRepository.existsById(account.getAccountCode())) {
1698
+ accountRepository.save(account);
1699
+ }
1700
+ ```
1701
+
1702
+ 4. **CommandLineRunner による起動時データ投入**
1703
+ ```java
1704
+ @Component
1705
+ @Profile({"dev", "local"})
1706
+ public class SeedDataRunner implements CommandLineRunner {
1707
+ @Override
1708
+ public void run(String... args) {
1709
+ seedDataService.seedAll();
1710
+ }
1711
+ }
1712
+ ```
1713
+
1714
+ ### 設計のポイント
1715
+
1716
+ 1. **実務に即した勘定科目体系**
1717
+ - 製造業向けの科目構成
1718
+ - 管理会計区分による部門別損益
1719
+ - 消費税複数税率対応
1720
+
1721
+ 2. **階層構造の管理**
1722
+ - 勘定科目のチルダ連結パス
1723
+ - 部門マスタの組織階層
1724
+ - 科目集計の効率化
1725
+
1726
+ 3. **Seedデータの設計**
1727
+ - 外部キー制約を考慮した投入順序
1728
+ - 冪等性(重複実行時の安全性)
1729
+ - テスト可能な構造
1730
+
1731
+ 4. **仕訳パターンの網羅**
1732
+ - 期首繰越仕訳
1733
+ - 売上・売掛金仕訳
1734
+ - 経費・消費税仕訳
1735
+ - 材料仕入仕訳
1736
+ - 給与支払仕訳
1737
+ - 入金・支払仕訳
1738
+
1739
+ ### テーブル一覧(Seed対象)
1740
+
1741
+ | テーブル名 | 件数目安 | 説明 |
1742
+ |-----------|---------|------|
1743
+ | 課税取引マスタ | 5件 | 税率マスタ |
1744
+ | 勘定科目マスタ | 100件 | 科目マスタ |
1745
+ | 勘定科目構成マスタ | 100件 | 科目階層 |
1746
+ | 部門マスタ | 30件 | 組織マスタ |
1747
+ | 仕訳 | 10件 | サンプル仕訳 |
1748
+ | 日次勘定科目残高 | 50件 | 日次残高 |
1749
+ | 月次勘定科目残高 | 20件 | 月次残高 |
1750
+
1751
+ ---
1752
+
1753
+ [← 第19章:赤黒とログの設計【ORM版】](./chapter19-orm.md) | [第21章:APIサービスの実装【ORM版】 →](./chapter21-orm.md)