@k2works/claude-code-booster 3.6.0 → 3.7.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 (713) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +42 -42
  3. package/bin/claude-code-booster +90 -90
  4. package/lib/assets/.claude/README.md +258 -239
  5. package/lib/assets/.claude/agent-memory/xp-programmer/MEMORY.md +6 -0
  6. package/lib/assets/.claude/agent-memory/xp-programmer/project_cargo_tracker.md +11 -0
  7. package/lib/assets/.claude/agent-memory/xp-programmer/project_ddd_patterns.md +27 -0
  8. package/lib/assets/.claude/agent-memory/xp-programmer/project_us07_route_assignment.md +19 -0
  9. package/lib/assets/.claude/scripts/generate-inception-deck.mjs +911 -911
  10. package/lib/assets/.claude/settings.json +11 -11
  11. package/lib/assets/.claude/skills/ai-agent-guidelines/SKILL.md +111 -111
  12. package/lib/assets/.claude/skills/analyzing-architecture/SKILL.md +83 -83
  13. package/lib/assets/.claude/skills/analyzing-business/SKILL.md +95 -95
  14. package/lib/assets/.claude/skills/analyzing-data-model/SKILL.md +77 -77
  15. package/lib/assets/.claude/skills/analyzing-domain-model/SKILL.md +117 -117
  16. package/lib/assets/.claude/skills/analyzing-inception-deck/SKILL.md +84 -84
  17. package/lib/assets/.claude/skills/analyzing-non-functional/SKILL.md +95 -95
  18. package/lib/assets/.claude/skills/analyzing-operation/SKILL.md +95 -95
  19. package/lib/assets/.claude/skills/analyzing-requirements/SKILL.md +91 -91
  20. package/lib/assets/.claude/skills/analyzing-tech-stack/SKILL.md +101 -101
  21. package/lib/assets/.claude/skills/analyzing-test-strategy/SKILL.md +89 -89
  22. package/lib/assets/.claude/skills/analyzing-ui-design/SKILL.md +80 -80
  23. package/lib/assets/.claude/skills/analyzing-usecases/SKILL.md +72 -72
  24. package/lib/assets/.claude/skills/creating-adr/SKILL.md +113 -113
  25. package/lib/assets/.claude/skills/developing-backend/SKILL.md +100 -100
  26. package/lib/assets/.claude/skills/developing-frontend/SKILL.md +93 -93
  27. package/lib/assets/.claude/skills/developing-release/SKILL.md +120 -120
  28. package/lib/assets/.claude/skills/generating-bmc/SKILL.md +97 -0
  29. package/lib/assets/.claude/skills/generating-slides/SKILL.md +94 -94
  30. package/lib/assets/.claude/skills/git-commit/SKILL.md +81 -81
  31. package/lib/assets/.claude/skills/killing-processes/SKILL.md +44 -44
  32. package/lib/assets/.claude/skills/operating-backup/SKILL.md +59 -59
  33. package/lib/assets/.claude/skills/operating-cicd/SKILL.md +54 -54
  34. package/lib/assets/.claude/skills/operating-deploy/SKILL.md +67 -67
  35. package/lib/assets/.claude/skills/operating-docs/SKILL.md +219 -219
  36. package/lib/assets/.claude/skills/operating-provision/SKILL.md +77 -77
  37. package/lib/assets/.claude/skills/operating-setup/SKILL.md +63 -63
  38. package/lib/assets/.claude/skills/orchestrating-analysis/SKILL.md +104 -104
  39. package/lib/assets/.claude/skills/orchestrating-development/SKILL.md +162 -162
  40. package/lib/assets/.claude/skills/orchestrating-operation/SKILL.md +158 -158
  41. package/lib/assets/.claude/skills/orchestrating-project/SKILL.md +144 -144
  42. package/lib/assets/.claude/skills/planning-releases/SKILL.md +119 -119
  43. package/lib/assets/.claude/skills/syncing-github-project/SKILL.md +151 -151
  44. package/lib/assets/.claude/skills/tracking-progress/SKILL.md +91 -91
  45. package/lib/assets/.claude/skills/validating-iteration-plan/SKILL.md +215 -215
  46. package/lib/assets/.devcontainer/devcontainer.json +34 -34
  47. package/lib/assets/.env.example +17 -17
  48. package/lib/assets/.gitattributes +4 -4
  49. package/lib/assets/.github/workflows/docker-publish.yml +77 -77
  50. package/lib/assets/.github/workflows/mkdocs.yml +39 -39
  51. package/lib/assets/AGENTS.md +94 -94
  52. package/lib/assets/CLAUDE.md +1 -0
  53. package/lib/assets/README.md +254 -254
  54. package/lib/assets/docker-compose.yml +33 -33
  55. package/lib/assets/docs/adr/index.md +10 -10
  56. package/lib/assets/docs/article/functional-desgin-ppp/all/01-immutability-and-data-transformation.md +475 -475
  57. package/lib/assets/docs/article/functional-desgin-ppp/all/02-function-composition.md +519 -519
  58. package/lib/assets/docs/article/functional-desgin-ppp/all/03-polymorphism.md +537 -537
  59. package/lib/assets/docs/article/functional-desgin-ppp/all/04-data-validation.md +300 -300
  60. package/lib/assets/docs/article/functional-desgin-ppp/all/05-property-based-testing.md +320 -320
  61. package/lib/assets/docs/article/functional-desgin-ppp/all/06-tdd-and-functional.md +498 -498
  62. package/lib/assets/docs/article/functional-desgin-ppp/all/07-composite-pattern.md +298 -298
  63. package/lib/assets/docs/article/functional-desgin-ppp/all/08-decorator-pattern.md +291 -291
  64. package/lib/assets/docs/article/functional-desgin-ppp/all/09-adapter-pattern.md +336 -336
  65. package/lib/assets/docs/article/functional-desgin-ppp/all/10-strategy-pattern.md +303 -303
  66. package/lib/assets/docs/article/functional-desgin-ppp/all/11-command-pattern.md +286 -286
  67. package/lib/assets/docs/article/functional-desgin-ppp/all/12-visitor-pattern.md +322 -322
  68. package/lib/assets/docs/article/functional-desgin-ppp/all/13-abstract-factory-pattern.md +319 -319
  69. package/lib/assets/docs/article/functional-desgin-ppp/all/14-abstract-server-pattern.md +365 -365
  70. package/lib/assets/docs/article/functional-desgin-ppp/all/15-gossiping-bus-drivers.md +156 -156
  71. package/lib/assets/docs/article/functional-desgin-ppp/all/16-payroll-system.md +178 -178
  72. package/lib/assets/docs/article/functional-desgin-ppp/all/17-video-rental-system.md +312 -312
  73. package/lib/assets/docs/article/functional-desgin-ppp/all/18-concurrency-system.md +287 -287
  74. package/lib/assets/docs/article/functional-desgin-ppp/all/19-wa-tor-simulation.md +286 -286
  75. package/lib/assets/docs/article/functional-desgin-ppp/all/20-pattern-interactions.md +274 -274
  76. package/lib/assets/docs/article/functional-desgin-ppp/all/21-best-practices.md +294 -294
  77. package/lib/assets/docs/article/functional-desgin-ppp/all/22-oo-to-fp-migration.md +337 -337
  78. package/lib/assets/docs/article/functional-desgin-ppp/all/index.md +388 -388
  79. package/lib/assets/docs/article/functional-desgin-ppp/clojure/01-immutability-and-data-transformation.md +273 -273
  80. package/lib/assets/docs/article/functional-desgin-ppp/clojure/02-function-composition.md +380 -380
  81. package/lib/assets/docs/article/functional-desgin-ppp/clojure/03-polymorphism.md +384 -384
  82. package/lib/assets/docs/article/functional-desgin-ppp/clojure/04-clojure-spec.md +350 -350
  83. package/lib/assets/docs/article/functional-desgin-ppp/clojure/05-property-based-testing.md +352 -352
  84. package/lib/assets/docs/article/functional-desgin-ppp/clojure/06-tdd-in-functional.md +383 -383
  85. package/lib/assets/docs/article/functional-desgin-ppp/clojure/07-composite-pattern.md +529 -529
  86. package/lib/assets/docs/article/functional-desgin-ppp/clojure/08-decorator-pattern.md +395 -395
  87. package/lib/assets/docs/article/functional-desgin-ppp/clojure/09-adapter-pattern.md +399 -399
  88. package/lib/assets/docs/article/functional-desgin-ppp/clojure/10-strategy-pattern.md +485 -485
  89. package/lib/assets/docs/article/functional-desgin-ppp/clojure/11-command-pattern.md +566 -566
  90. package/lib/assets/docs/article/functional-desgin-ppp/clojure/12-visitor-pattern.md +567 -567
  91. package/lib/assets/docs/article/functional-desgin-ppp/clojure/13-abstract-factory-pattern.md +475 -475
  92. package/lib/assets/docs/article/functional-desgin-ppp/clojure/14-abstract-server-pattern.md +462 -462
  93. package/lib/assets/docs/article/functional-desgin-ppp/clojure/15-gossiping-bus-drivers.md +325 -325
  94. package/lib/assets/docs/article/functional-desgin-ppp/clojure/16-payroll-system.md +401 -401
  95. package/lib/assets/docs/article/functional-desgin-ppp/clojure/17-video-rental-system.md +450 -450
  96. package/lib/assets/docs/article/functional-desgin-ppp/clojure/18-concurrency-system.md +475 -475
  97. package/lib/assets/docs/article/functional-desgin-ppp/clojure/19-wator-simulation.md +739 -739
  98. package/lib/assets/docs/article/functional-desgin-ppp/clojure/20-pattern-interactions.md +567 -567
  99. package/lib/assets/docs/article/functional-desgin-ppp/clojure/21-best-practices.md +518 -518
  100. package/lib/assets/docs/article/functional-desgin-ppp/clojure/22-oo-to-fp-migration.md +532 -532
  101. package/lib/assets/docs/article/functional-desgin-ppp/clojure/index.md +241 -241
  102. package/lib/assets/docs/article/functional-desgin-ppp/elixir/01-immutability-and-data-transformation.md +383 -383
  103. package/lib/assets/docs/article/functional-desgin-ppp/elixir/02-function-composition.md +374 -374
  104. package/lib/assets/docs/article/functional-desgin-ppp/elixir/03-polymorphism.md +375 -375
  105. package/lib/assets/docs/article/functional-desgin-ppp/elixir/04-data-validation.md +195 -195
  106. package/lib/assets/docs/article/functional-desgin-ppp/elixir/05-property-based-testing.md +268 -268
  107. package/lib/assets/docs/article/functional-desgin-ppp/elixir/06-tdd-and-fp.md +294 -294
  108. package/lib/assets/docs/article/functional-desgin-ppp/elixir/07-effects-and-pure-functions.md +164 -164
  109. package/lib/assets/docs/article/functional-desgin-ppp/elixir/08-error-handling-strategies.md +168 -168
  110. package/lib/assets/docs/article/functional-desgin-ppp/elixir/09-io-and-external-systems.md +254 -254
  111. package/lib/assets/docs/article/functional-desgin-ppp/elixir/10-concurrency-patterns.md +269 -269
  112. package/lib/assets/docs/article/functional-desgin-ppp/elixir/11-command-pattern.md +148 -148
  113. package/lib/assets/docs/article/functional-desgin-ppp/elixir/12-visitor-pattern.md +176 -176
  114. package/lib/assets/docs/article/functional-desgin-ppp/elixir/13-abstract-factory-pattern.md +604 -604
  115. package/lib/assets/docs/article/functional-desgin-ppp/elixir/14-abstract-server-pattern.md +729 -729
  116. package/lib/assets/docs/article/functional-desgin-ppp/elixir/15-gossiping-bus-drivers.md +291 -291
  117. package/lib/assets/docs/article/functional-desgin-ppp/elixir/16-payroll-system.md +420 -420
  118. package/lib/assets/docs/article/functional-desgin-ppp/elixir/17-video-rental-system.md +319 -319
  119. package/lib/assets/docs/article/functional-desgin-ppp/elixir/18-concurrency-system.md +466 -466
  120. package/lib/assets/docs/article/functional-desgin-ppp/elixir/19-wator-simulation.md +523 -523
  121. package/lib/assets/docs/article/functional-desgin-ppp/elixir/20-pattern-interactions.md +287 -287
  122. package/lib/assets/docs/article/functional-desgin-ppp/elixir/21-best-practices.md +340 -340
  123. package/lib/assets/docs/article/functional-desgin-ppp/elixir/22-oo-to-fp-migration.md +395 -395
  124. package/lib/assets/docs/article/functional-desgin-ppp/elixir/index.md +248 -248
  125. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/01-immutability-and-data-transformation.md +384 -384
  126. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/02-function-composition.md +452 -452
  127. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/03-polymorphism.md +495 -495
  128. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/04-data-validation.md +416 -416
  129. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/05-property-based-testing.md +382 -382
  130. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/06-tdd-functional.md +687 -687
  131. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/07-composite-pattern.md +442 -442
  132. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/08-decorator-pattern.md +479 -479
  133. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/09-adapter-pattern.md +479 -479
  134. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/10-strategy-pattern.md +427 -427
  135. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/11-command-pattern.md +428 -428
  136. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/12-visitor-pattern.md +339 -339
  137. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/13-abstract-factory-pattern.md +309 -309
  138. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/14-abstract-server-pattern.md +596 -596
  139. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/15-gossiping-bus-drivers.md +355 -355
  140. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/16-payroll-system.md +350 -350
  141. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/17-video-rental-system.md +414 -414
  142. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/18-concurrency-system.md +367 -367
  143. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/19-wator-simulation.md +403 -403
  144. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/20-pattern-interactions.md +291 -291
  145. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/21-best-practices.md +324 -324
  146. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/22-oo-to-fp-migration.md +332 -332
  147. package/lib/assets/docs/article/functional-desgin-ppp/fsharp/index.md +274 -274
  148. package/lib/assets/docs/article/functional-desgin-ppp/haskell/01-immutability-and-data-transformation.md +298 -298
  149. package/lib/assets/docs/article/functional-desgin-ppp/haskell/02-function-composition.md +304 -304
  150. package/lib/assets/docs/article/functional-desgin-ppp/haskell/03-polymorphism.md +362 -362
  151. package/lib/assets/docs/article/functional-desgin-ppp/haskell/04-data-validation.md +257 -257
  152. package/lib/assets/docs/article/functional-desgin-ppp/haskell/05-property-based-testing.md +254 -254
  153. package/lib/assets/docs/article/functional-desgin-ppp/haskell/06-tdd-functional.md +283 -283
  154. package/lib/assets/docs/article/functional-desgin-ppp/haskell/07-composite-pattern.md +395 -395
  155. package/lib/assets/docs/article/functional-desgin-ppp/haskell/08-decorator-pattern.md +319 -319
  156. package/lib/assets/docs/article/functional-desgin-ppp/haskell/09-adapter-pattern.md +382 -382
  157. package/lib/assets/docs/article/functional-desgin-ppp/haskell/10-strategy-pattern.md +287 -287
  158. package/lib/assets/docs/article/functional-desgin-ppp/haskell/11-command-pattern.md +303 -303
  159. package/lib/assets/docs/article/functional-desgin-ppp/haskell/12-visitor-pattern.md +326 -326
  160. package/lib/assets/docs/article/functional-desgin-ppp/haskell/13-abstract-factory-pattern.md +332 -332
  161. package/lib/assets/docs/article/functional-desgin-ppp/haskell/14-abstract-server-pattern.md +379 -379
  162. package/lib/assets/docs/article/functional-desgin-ppp/haskell/15-gossiping-bus-drivers.md +177 -177
  163. package/lib/assets/docs/article/functional-desgin-ppp/haskell/16-payroll-system.md +219 -219
  164. package/lib/assets/docs/article/functional-desgin-ppp/haskell/17-video-rental-system.md +244 -244
  165. package/lib/assets/docs/article/functional-desgin-ppp/haskell/18-concurrency-system.md +363 -363
  166. package/lib/assets/docs/article/functional-desgin-ppp/haskell/19-wator-simulation.md +438 -438
  167. package/lib/assets/docs/article/functional-desgin-ppp/haskell/20-pattern-interactions.md +325 -325
  168. package/lib/assets/docs/article/functional-desgin-ppp/haskell/21-best-practices.md +403 -403
  169. package/lib/assets/docs/article/functional-desgin-ppp/haskell/22-oo-to-fp-migration.md +469 -469
  170. package/lib/assets/docs/article/functional-desgin-ppp/haskell/index.md +174 -174
  171. package/lib/assets/docs/article/functional-desgin-ppp/index.md +90 -90
  172. package/lib/assets/docs/article/functional-desgin-ppp/rust/01-immutability-and-data-transformation.md +450 -450
  173. package/lib/assets/docs/article/functional-desgin-ppp/rust/02-function-composition.md +463 -463
  174. package/lib/assets/docs/article/functional-desgin-ppp/rust/03-polymorphism.md +425 -425
  175. package/lib/assets/docs/article/functional-desgin-ppp/rust/04-data-validation.md +273 -273
  176. package/lib/assets/docs/article/functional-desgin-ppp/rust/05-property-based-testing.md +247 -247
  177. package/lib/assets/docs/article/functional-desgin-ppp/rust/06-tdd-and-functional.md +841 -841
  178. package/lib/assets/docs/article/functional-desgin-ppp/rust/07-composite-pattern.md +384 -384
  179. package/lib/assets/docs/article/functional-desgin-ppp/rust/08-decorator-pattern.md +383 -383
  180. package/lib/assets/docs/article/functional-desgin-ppp/rust/09-adapter-pattern.md +339 -339
  181. package/lib/assets/docs/article/functional-desgin-ppp/rust/10-strategy-pattern.md +331 -331
  182. package/lib/assets/docs/article/functional-desgin-ppp/rust/11-command-pattern.md +356 -356
  183. package/lib/assets/docs/article/functional-desgin-ppp/rust/12-visitor-pattern.md +379 -379
  184. package/lib/assets/docs/article/functional-desgin-ppp/rust/13-abstract-factory-pattern.md +361 -361
  185. package/lib/assets/docs/article/functional-desgin-ppp/rust/14-abstract-server-pattern.md +392 -392
  186. package/lib/assets/docs/article/functional-desgin-ppp/rust/15-gossiping-bus-drivers.md +300 -300
  187. package/lib/assets/docs/article/functional-desgin-ppp/rust/16-payroll-system.md +297 -297
  188. package/lib/assets/docs/article/functional-desgin-ppp/rust/17-video-rental-system.md +304 -304
  189. package/lib/assets/docs/article/functional-desgin-ppp/rust/18-concurrency-system.md +315 -315
  190. package/lib/assets/docs/article/functional-desgin-ppp/rust/19-wator-simulation.md +311 -311
  191. package/lib/assets/docs/article/functional-desgin-ppp/rust/20-pattern-interactions.md +304 -304
  192. package/lib/assets/docs/article/functional-desgin-ppp/rust/21-best-practices.md +336 -336
  193. package/lib/assets/docs/article/functional-desgin-ppp/rust/22-oo-to-fp-migration.md +349 -349
  194. package/lib/assets/docs/article/functional-desgin-ppp/rust/index.md +243 -243
  195. package/lib/assets/docs/article/functional-desgin-ppp/scala/01-immutability-and-data-transformation.md +328 -328
  196. package/lib/assets/docs/article/functional-desgin-ppp/scala/02-function-composition.md +348 -348
  197. package/lib/assets/docs/article/functional-desgin-ppp/scala/03-polymorphism.md +357 -357
  198. package/lib/assets/docs/article/functional-desgin-ppp/scala/04-data-validation.md +364 -364
  199. package/lib/assets/docs/article/functional-desgin-ppp/scala/05-property-based-testing.md +515 -515
  200. package/lib/assets/docs/article/functional-desgin-ppp/scala/06-tdd-functional.md +557 -557
  201. package/lib/assets/docs/article/functional-desgin-ppp/scala/07-composite-pattern.md +363 -363
  202. package/lib/assets/docs/article/functional-desgin-ppp/scala/08-decorator-pattern.md +327 -327
  203. package/lib/assets/docs/article/functional-desgin-ppp/scala/09-adapter-pattern.md +517 -517
  204. package/lib/assets/docs/article/functional-desgin-ppp/scala/10-strategy-pattern.md +441 -441
  205. package/lib/assets/docs/article/functional-desgin-ppp/scala/11-command-pattern.md +407 -407
  206. package/lib/assets/docs/article/functional-desgin-ppp/scala/12-visitor-pattern.md +379 -379
  207. package/lib/assets/docs/article/functional-desgin-ppp/scala/13-abstract-factory-pattern.md +398 -398
  208. package/lib/assets/docs/article/functional-desgin-ppp/scala/14-abstract-server-pattern.md +476 -476
  209. package/lib/assets/docs/article/functional-desgin-ppp/scala/15-gossiping-bus-drivers.md +391 -391
  210. package/lib/assets/docs/article/functional-desgin-ppp/scala/16-payroll-system.md +342 -342
  211. package/lib/assets/docs/article/functional-desgin-ppp/scala/17-video-rental-system.md +324 -324
  212. package/lib/assets/docs/article/functional-desgin-ppp/scala/18-concurrency-system.md +730 -730
  213. package/lib/assets/docs/article/functional-desgin-ppp/scala/19-wator-simulation.md +624 -624
  214. package/lib/assets/docs/article/functional-desgin-ppp/scala/20-pattern-interactions.md +512 -512
  215. package/lib/assets/docs/article/functional-desgin-ppp/scala/21-best-practices.md +433 -433
  216. package/lib/assets/docs/article/functional-desgin-ppp/scala/22-oo-to-fp-migration.md +688 -688
  217. package/lib/assets/docs/article/functional-desgin-ppp/scala/index.md +243 -243
  218. package/lib/assets/docs/article/getting-start-tdd/clojure/01-todo-list-and-first-test.md +166 -166
  219. package/lib/assets/docs/article/getting-start-tdd/clojure/02-fake-it-and-triangulation.md +162 -162
  220. package/lib/assets/docs/article/getting-start-tdd/clojure/03-obvious-implementation-and-refactoring.md +135 -135
  221. package/lib/assets/docs/article/getting-start-tdd/clojure/04-version-control-and-conventional-commits.md +88 -88
  222. package/lib/assets/docs/article/getting-start-tdd/clojure/05-package-management-and-static-analysis.md +299 -299
  223. package/lib/assets/docs/article/getting-start-tdd/clojure/06-task-runner-and-ci-cd.md +241 -241
  224. package/lib/assets/docs/article/getting-start-tdd/clojure/07-protocols-and-records.md +131 -131
  225. package/lib/assets/docs/article/getting-start-tdd/clojure/08-multimethods-and-design-patterns.md +130 -130
  226. package/lib/assets/docs/article/getting-start-tdd/clojure/09-namespaces-and-module-design.md +127 -127
  227. package/lib/assets/docs/article/getting-start-tdd/clojure/10-higher-order-functions-and-composition.md +114 -114
  228. package/lib/assets/docs/article/getting-start-tdd/clojure/11-persistent-data-and-pipeline.md +138 -138
  229. package/lib/assets/docs/article/getting-start-tdd/clojure/12-error-handling-and-spec.md +161 -161
  230. package/lib/assets/docs/article/getting-start-tdd/clojure/index.md +65 -65
  231. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter01.md +232 -232
  232. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter02.md +244 -244
  233. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter03.md +202 -202
  234. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter04.md +92 -92
  235. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter05.md +256 -256
  236. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter06.md +195 -195
  237. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter07.md +214 -214
  238. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter08.md +249 -249
  239. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter09.md +174 -174
  240. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter10.md +166 -166
  241. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter11.md +192 -192
  242. package/lib/assets/docs/article/getting-start-tdd/csharp/chapter12.md +211 -211
  243. package/lib/assets/docs/article/getting-start-tdd/csharp/index.md +83 -83
  244. package/lib/assets/docs/article/getting-start-tdd/elixir/01-todo-list-and-first-test.md +87 -87
  245. package/lib/assets/docs/article/getting-start-tdd/elixir/02-fake-it-and-triangulation.md +95 -95
  246. package/lib/assets/docs/article/getting-start-tdd/elixir/03-obvious-implementation-and-refactoring.md +109 -109
  247. package/lib/assets/docs/article/getting-start-tdd/elixir/04-version-control-and-conventional-commits.md +96 -96
  248. package/lib/assets/docs/article/getting-start-tdd/elixir/05-package-management-and-static-analysis.md +88 -88
  249. package/lib/assets/docs/article/getting-start-tdd/elixir/06-task-runner-and-ci-cd.md +71 -71
  250. package/lib/assets/docs/article/getting-start-tdd/elixir/07-structs-and-protocols.md +110 -110
  251. package/lib/assets/docs/article/getting-start-tdd/elixir/08-pattern-matching-and-guards.md +108 -108
  252. package/lib/assets/docs/article/getting-start-tdd/elixir/09-module-design-and-behaviours.md +104 -104
  253. package/lib/assets/docs/article/getting-start-tdd/elixir/10-higher-order-functions-and-pipeline.md +178 -178
  254. package/lib/assets/docs/article/getting-start-tdd/elixir/11-stream-and-lazy-evaluation.md +142 -142
  255. package/lib/assets/docs/article/getting-start-tdd/elixir/12-error-handling-and-with.md +145 -145
  256. package/lib/assets/docs/article/getting-start-tdd/elixir/index.md +35 -35
  257. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter01.md +202 -202
  258. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter02.md +246 -246
  259. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter03.md +218 -218
  260. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter04.md +179 -179
  261. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter05.md +267 -267
  262. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter06.md +190 -190
  263. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter07.md +161 -161
  264. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter08.md +175 -175
  265. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter09.md +222 -222
  266. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter10.md +189 -189
  267. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter11.md +212 -212
  268. package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter12.md +215 -215
  269. package/lib/assets/docs/article/getting-start-tdd/fsharp/index.md +71 -71
  270. package/lib/assets/docs/article/getting-start-tdd/go/01-todo-list-and-first-test.md +213 -213
  271. package/lib/assets/docs/article/getting-start-tdd/go/02-fake-it-and-triangulation.md +302 -302
  272. package/lib/assets/docs/article/getting-start-tdd/go/03-obvious-implementation-and-refactoring.md +339 -339
  273. package/lib/assets/docs/article/getting-start-tdd/go/04-version-control-and-conventional-commits.md +112 -112
  274. package/lib/assets/docs/article/getting-start-tdd/go/05-package-management-and-static-analysis.md +272 -272
  275. package/lib/assets/docs/article/getting-start-tdd/go/06-task-runner-and-ci-cd.md +233 -233
  276. package/lib/assets/docs/article/getting-start-tdd/go/07-encapsulation-and-polymorphism.md +394 -394
  277. package/lib/assets/docs/article/getting-start-tdd/go/08-design-patterns.md +422 -422
  278. package/lib/assets/docs/article/getting-start-tdd/go/09-solid-principles-and-module-design.md +400 -400
  279. package/lib/assets/docs/article/getting-start-tdd/go/10-higher-order-functions-and-composition.md +226 -226
  280. package/lib/assets/docs/article/getting-start-tdd/go/11-immutable-data-and-pipeline.md +296 -296
  281. package/lib/assets/docs/article/getting-start-tdd/go/12-error-handling-and-type-safety.md +411 -411
  282. package/lib/assets/docs/article/getting-start-tdd/go/index.md +83 -83
  283. package/lib/assets/docs/article/getting-start-tdd/haskell/01-todo-list-and-first-test.md +279 -279
  284. package/lib/assets/docs/article/getting-start-tdd/haskell/02-fake-it-and-triangulation.md +337 -337
  285. package/lib/assets/docs/article/getting-start-tdd/haskell/03-obvious-implementation-and-refactoring.md +257 -257
  286. package/lib/assets/docs/article/getting-start-tdd/haskell/04-version-control-and-conventional-commits.md +182 -182
  287. package/lib/assets/docs/article/getting-start-tdd/haskell/05-package-management-and-static-analysis.md +313 -313
  288. package/lib/assets/docs/article/getting-start-tdd/haskell/06-task-runner-and-ci-cd.md +309 -309
  289. package/lib/assets/docs/article/getting-start-tdd/haskell/07-algebraic-data-types-and-type-classes.md +412 -412
  290. package/lib/assets/docs/article/getting-start-tdd/haskell/08-pattern-matching-and-guards.md +390 -390
  291. package/lib/assets/docs/article/getting-start-tdd/haskell/09-module-design-and-smart-constructors.md +461 -461
  292. package/lib/assets/docs/article/getting-start-tdd/haskell/10-higher-order-functions-and-currying.md +434 -434
  293. package/lib/assets/docs/article/getting-start-tdd/haskell/11-function-composition-and-point-free.md +392 -392
  294. package/lib/assets/docs/article/getting-start-tdd/haskell/12-monad-and-error-handling.md +631 -631
  295. package/lib/assets/docs/article/getting-start-tdd/haskell/index.md +49 -49
  296. package/lib/assets/docs/article/getting-start-tdd/index.md +93 -93
  297. package/lib/assets/docs/article/getting-start-tdd/integration/01-language-overview.md +375 -375
  298. package/lib/assets/docs/article/getting-start-tdd/integration/02-test-framework-comparison.md +349 -349
  299. package/lib/assets/docs/article/getting-start-tdd/integration/03-tdd-pattern-comparison.md +445 -445
  300. package/lib/assets/docs/article/getting-start-tdd/integration/04-type-system-comparison.md +409 -409
  301. package/lib/assets/docs/article/getting-start-tdd/integration/05-dev-environment-comparison.md +330 -330
  302. package/lib/assets/docs/article/getting-start-tdd/integration/06-learning-roadmap.md +290 -290
  303. package/lib/assets/docs/article/getting-start-tdd/integration/index.md +69 -69
  304. package/lib/assets/docs/article/getting-start-tdd/java/01-todo-list-and-first-test.md +234 -234
  305. package/lib/assets/docs/article/getting-start-tdd/java/02-fake-it-and-triangulation.md +261 -261
  306. package/lib/assets/docs/article/getting-start-tdd/java/03-obvious-implementation-and-refactoring.md +185 -185
  307. package/lib/assets/docs/article/getting-start-tdd/java/04-version-control-and-conventional-commits.md +115 -115
  308. package/lib/assets/docs/article/getting-start-tdd/java/05-package-management-and-static-analysis.md +382 -382
  309. package/lib/assets/docs/article/getting-start-tdd/java/06-task-runner-and-ci-cd.md +272 -272
  310. package/lib/assets/docs/article/getting-start-tdd/java/07-encapsulation-and-polymorphism.md +626 -626
  311. package/lib/assets/docs/article/getting-start-tdd/java/08-design-patterns.md +393 -393
  312. package/lib/assets/docs/article/getting-start-tdd/java/09-solid-principles-and-module-design.md +310 -310
  313. package/lib/assets/docs/article/getting-start-tdd/java/10-higher-order-functions-and-composition.md +188 -188
  314. package/lib/assets/docs/article/getting-start-tdd/java/11-immutable-data-and-pipeline.md +167 -167
  315. package/lib/assets/docs/article/getting-start-tdd/java/12-error-handling-and-type-safety.md +205 -205
  316. package/lib/assets/docs/article/getting-start-tdd/java/index.md +61 -61
  317. package/lib/assets/docs/article/getting-start-tdd/node/01-todo-list-and-first-test.md +244 -244
  318. package/lib/assets/docs/article/getting-start-tdd/node/02-fake-it-and-triangulation.md +262 -262
  319. package/lib/assets/docs/article/getting-start-tdd/node/03-obvious-implementation-and-refactoring.md +169 -169
  320. package/lib/assets/docs/article/getting-start-tdd/node/04-version-control-and-conventional-commits.md +112 -112
  321. package/lib/assets/docs/article/getting-start-tdd/node/05-package-management-and-static-analysis.md +314 -314
  322. package/lib/assets/docs/article/getting-start-tdd/node/06-task-runner-and-ci-cd.md +235 -235
  323. package/lib/assets/docs/article/getting-start-tdd/node/07-encapsulation-and-polymorphism.md +327 -327
  324. package/lib/assets/docs/article/getting-start-tdd/node/08-design-patterns.md +322 -322
  325. package/lib/assets/docs/article/getting-start-tdd/node/09-solid-principles-and-module-design.md +285 -285
  326. package/lib/assets/docs/article/getting-start-tdd/node/10-higher-order-functions-and-composition.md +199 -199
  327. package/lib/assets/docs/article/getting-start-tdd/node/11-immutable-data-and-pipeline.md +207 -207
  328. package/lib/assets/docs/article/getting-start-tdd/node/12-error-handling-and-type-safety.md +295 -295
  329. package/lib/assets/docs/article/getting-start-tdd/node/index.md +56 -56
  330. package/lib/assets/docs/article/getting-start-tdd/php/01-todo-list-and-first-test.md +259 -259
  331. package/lib/assets/docs/article/getting-start-tdd/php/02-fake-it-and-triangulation.md +200 -200
  332. package/lib/assets/docs/article/getting-start-tdd/php/03-obvious-implementation-and-refactoring.md +248 -248
  333. package/lib/assets/docs/article/getting-start-tdd/php/04-version-control-and-conventional-commits.md +141 -141
  334. package/lib/assets/docs/article/getting-start-tdd/php/05-package-management-and-static-analysis.md +410 -410
  335. package/lib/assets/docs/article/getting-start-tdd/php/06-task-runner-and-ci-cd.md +321 -321
  336. package/lib/assets/docs/article/getting-start-tdd/php/07-encapsulation-and-polymorphism.md +372 -372
  337. package/lib/assets/docs/article/getting-start-tdd/php/08-design-patterns.md +453 -453
  338. package/lib/assets/docs/article/getting-start-tdd/php/09-solid-principles-and-module-design.md +460 -460
  339. package/lib/assets/docs/article/getting-start-tdd/php/10-higher-order-functions-and-composition.md +182 -182
  340. package/lib/assets/docs/article/getting-start-tdd/php/11-immutable-data-and-pipeline.md +266 -266
  341. package/lib/assets/docs/article/getting-start-tdd/php/12-error-handling-and-type-safety.md +308 -308
  342. package/lib/assets/docs/article/getting-start-tdd/php/index.md +84 -84
  343. package/lib/assets/docs/article/getting-start-tdd/python/01-todo-list-and-first-test.md +201 -201
  344. package/lib/assets/docs/article/getting-start-tdd/python/02-fake-it-and-triangulation.md +247 -247
  345. package/lib/assets/docs/article/getting-start-tdd/python/03-obvious-implementation-and-refactoring.md +199 -199
  346. package/lib/assets/docs/article/getting-start-tdd/python/04-version-control-and-conventional-commits.md +87 -87
  347. package/lib/assets/docs/article/getting-start-tdd/python/05-package-management-and-static-analysis.md +274 -274
  348. package/lib/assets/docs/article/getting-start-tdd/python/06-task-runner-and-ci-cd.md +190 -190
  349. package/lib/assets/docs/article/getting-start-tdd/python/07-encapsulation-and-polymorphism.md +208 -208
  350. package/lib/assets/docs/article/getting-start-tdd/python/08-design-patterns.md +172 -172
  351. package/lib/assets/docs/article/getting-start-tdd/python/09-solid-principles-and-module-design.md +130 -130
  352. package/lib/assets/docs/article/getting-start-tdd/python/10-higher-order-functions-and-composition.md +122 -122
  353. package/lib/assets/docs/article/getting-start-tdd/python/11-immutable-data-and-pipeline.md +116 -116
  354. package/lib/assets/docs/article/getting-start-tdd/python/12-error-handling-and-type-safety.md +126 -126
  355. package/lib/assets/docs/article/getting-start-tdd/python/index.md +55 -55
  356. package/lib/assets/docs/article/getting-start-tdd/ruby/01-todo-list-and-first-test.md +231 -231
  357. package/lib/assets/docs/article/getting-start-tdd/ruby/02-fake-it-and-triangulation.md +238 -238
  358. package/lib/assets/docs/article/getting-start-tdd/ruby/03-obvious-implementation-and-refactoring.md +228 -228
  359. package/lib/assets/docs/article/getting-start-tdd/ruby/04-version-control-and-conventional-commits.md +112 -112
  360. package/lib/assets/docs/article/getting-start-tdd/ruby/05-package-management-and-static-analysis.md +287 -287
  361. package/lib/assets/docs/article/getting-start-tdd/ruby/06-task-runner-and-ci-cd.md +248 -248
  362. package/lib/assets/docs/article/getting-start-tdd/ruby/07-encapsulation-and-polymorphism.md +279 -279
  363. package/lib/assets/docs/article/getting-start-tdd/ruby/08-design-patterns.md +329 -329
  364. package/lib/assets/docs/article/getting-start-tdd/ruby/09-solid-principles-and-module-design.md +196 -196
  365. package/lib/assets/docs/article/getting-start-tdd/ruby/10-higher-order-functions-and-composition.md +175 -175
  366. package/lib/assets/docs/article/getting-start-tdd/ruby/11-immutable-data-and-pipeline.md +237 -237
  367. package/lib/assets/docs/article/getting-start-tdd/ruby/12-error-handling-and-type-safety.md +398 -398
  368. package/lib/assets/docs/article/getting-start-tdd/ruby/index.md +83 -83
  369. package/lib/assets/docs/article/getting-start-tdd/rust/01-todo-list-and-first-test.md +211 -211
  370. package/lib/assets/docs/article/getting-start-tdd/rust/02-fake-it-and-triangulation.md +264 -264
  371. package/lib/assets/docs/article/getting-start-tdd/rust/03-obvious-implementation-and-refactoring.md +233 -233
  372. package/lib/assets/docs/article/getting-start-tdd/rust/04-version-control-and-conventional-commits.md +92 -92
  373. package/lib/assets/docs/article/getting-start-tdd/rust/05-package-management-and-static-analysis.md +212 -212
  374. package/lib/assets/docs/article/getting-start-tdd/rust/06-task-runner-and-ci-cd.md +164 -164
  375. package/lib/assets/docs/article/getting-start-tdd/rust/07-encapsulation-and-polymorphism.md +142 -142
  376. package/lib/assets/docs/article/getting-start-tdd/rust/08-design-patterns.md +145 -145
  377. package/lib/assets/docs/article/getting-start-tdd/rust/09-solid-principles-and-module-design.md +110 -110
  378. package/lib/assets/docs/article/getting-start-tdd/rust/10-higher-order-functions-and-composition.md +94 -94
  379. package/lib/assets/docs/article/getting-start-tdd/rust/11-immutable-data-and-pipeline.md +105 -105
  380. package/lib/assets/docs/article/getting-start-tdd/rust/12-error-handling-and-type-safety.md +112 -112
  381. package/lib/assets/docs/article/getting-start-tdd/rust/index.md +83 -83
  382. package/lib/assets/docs/article/getting-start-tdd/scala/01-todo-list-and-first-test.md +111 -111
  383. package/lib/assets/docs/article/getting-start-tdd/scala/02-fake-it-and-triangulation.md +107 -107
  384. package/lib/assets/docs/article/getting-start-tdd/scala/03-obvious-implementation-and-refactoring.md +99 -99
  385. package/lib/assets/docs/article/getting-start-tdd/scala/04-version-control-and-conventional-commits.md +123 -123
  386. package/lib/assets/docs/article/getting-start-tdd/scala/05-package-management-and-static-analysis.md +196 -196
  387. package/lib/assets/docs/article/getting-start-tdd/scala/06-task-runner-and-ci-cd.md +186 -186
  388. package/lib/assets/docs/article/getting-start-tdd/scala/07-case-classes-and-traits.md +139 -139
  389. package/lib/assets/docs/article/getting-start-tdd/scala/08-pattern-matching-and-sealed-traits.md +106 -106
  390. package/lib/assets/docs/article/getting-start-tdd/scala/09-packages-and-module-design.md +75 -75
  391. package/lib/assets/docs/article/getting-start-tdd/scala/10-higher-order-functions-and-composition.md +104 -104
  392. package/lib/assets/docs/article/getting-start-tdd/scala/11-collections-and-lazy-evaluation.md +94 -94
  393. package/lib/assets/docs/article/getting-start-tdd/scala/12-error-handling-and-type-safety.md +92 -92
  394. package/lib/assets/docs/article/getting-start-tdd/scala/index.md +65 -65
  395. package/lib/assets/docs/article/grokking-concurrency/all/index.md +404 -404
  396. package/lib/assets/docs/article/grokking-concurrency/all/part-1-ch02-sequential.md +554 -554
  397. package/lib/assets/docs/article/grokking-concurrency/all/part-2-ch04-05-threads.md +469 -469
  398. package/lib/assets/docs/article/grokking-concurrency/all/part-3-ch06-multitasking.md +520 -520
  399. package/lib/assets/docs/article/grokking-concurrency/all/part-4-ch07-parallel-patterns.md +420 -420
  400. package/lib/assets/docs/article/grokking-concurrency/all/part-5-ch08-09-synchronization.md +510 -510
  401. package/lib/assets/docs/article/grokking-concurrency/all/part-6-ch10-11-nonblocking-io.md +435 -435
  402. package/lib/assets/docs/article/grokking-concurrency/all/part-7-ch12-async.md +465 -465
  403. package/lib/assets/docs/article/grokking-concurrency/all/part-8-ch13-mapreduce.md +377 -377
  404. package/lib/assets/docs/article/grokking-concurrency/clojure/index.md +116 -116
  405. package/lib/assets/docs/article/grokking-concurrency/clojure/part-1.md +108 -108
  406. package/lib/assets/docs/article/grokking-concurrency/clojure/part-2.md +101 -101
  407. package/lib/assets/docs/article/grokking-concurrency/clojure/part-3.md +122 -122
  408. package/lib/assets/docs/article/grokking-concurrency/clojure/part-4.md +123 -123
  409. package/lib/assets/docs/article/grokking-concurrency/clojure/part-5.md +118 -118
  410. package/lib/assets/docs/article/grokking-concurrency/clojure/part-6.md +89 -89
  411. package/lib/assets/docs/article/grokking-concurrency/clojure/part-7.md +100 -100
  412. package/lib/assets/docs/article/grokking-concurrency/clojure/part-8.md +120 -120
  413. package/lib/assets/docs/article/grokking-concurrency/csharp/index.md +101 -101
  414. package/lib/assets/docs/article/grokking-concurrency/csharp/part-1.md +97 -97
  415. package/lib/assets/docs/article/grokking-concurrency/csharp/part-2.md +123 -123
  416. package/lib/assets/docs/article/grokking-concurrency/csharp/part-3.md +101 -101
  417. package/lib/assets/docs/article/grokking-concurrency/csharp/part-4.md +112 -112
  418. package/lib/assets/docs/article/grokking-concurrency/csharp/part-5.md +99 -99
  419. package/lib/assets/docs/article/grokking-concurrency/csharp/part-6.md +61 -61
  420. package/lib/assets/docs/article/grokking-concurrency/csharp/part-7.md +84 -84
  421. package/lib/assets/docs/article/grokking-concurrency/csharp/part-8.md +92 -92
  422. package/lib/assets/docs/article/grokking-concurrency/fsharp/index.md +65 -65
  423. package/lib/assets/docs/article/grokking-concurrency/fsharp/part-1.md +80 -80
  424. package/lib/assets/docs/article/grokking-concurrency/fsharp/part-2.md +103 -103
  425. package/lib/assets/docs/article/grokking-concurrency/fsharp/part-3.md +94 -94
  426. package/lib/assets/docs/article/grokking-concurrency/fsharp/part-4.md +110 -110
  427. package/lib/assets/docs/article/grokking-concurrency/fsharp/part-5.md +104 -104
  428. package/lib/assets/docs/article/grokking-concurrency/fsharp/part-6.md +93 -93
  429. package/lib/assets/docs/article/grokking-concurrency/fsharp/part-7.md +121 -121
  430. package/lib/assets/docs/article/grokking-concurrency/fsharp/part-8.md +107 -107
  431. package/lib/assets/docs/article/grokking-concurrency/haskell/index.md +248 -248
  432. package/lib/assets/docs/article/grokking-concurrency/haskell/part-1.md +96 -96
  433. package/lib/assets/docs/article/grokking-concurrency/haskell/part-2.md +96 -96
  434. package/lib/assets/docs/article/grokking-concurrency/haskell/part-3.md +91 -91
  435. package/lib/assets/docs/article/grokking-concurrency/haskell/part-4.md +106 -106
  436. package/lib/assets/docs/article/grokking-concurrency/haskell/part-5.md +99 -99
  437. package/lib/assets/docs/article/grokking-concurrency/haskell/part-6.md +95 -95
  438. package/lib/assets/docs/article/grokking-concurrency/haskell/part-7.md +111 -111
  439. package/lib/assets/docs/article/grokking-concurrency/haskell/part-8.md +118 -118
  440. package/lib/assets/docs/article/grokking-concurrency/index.md +66 -66
  441. package/lib/assets/docs/article/grokking-concurrency/java/index.md +102 -102
  442. package/lib/assets/docs/article/grokking-concurrency/java/part-1.md +308 -308
  443. package/lib/assets/docs/article/grokking-concurrency/java/part-2.md +334 -334
  444. package/lib/assets/docs/article/grokking-concurrency/java/part-3.md +221 -221
  445. package/lib/assets/docs/article/grokking-concurrency/java/part-4.md +213 -213
  446. package/lib/assets/docs/article/grokking-concurrency/java/part-5.md +112 -112
  447. package/lib/assets/docs/article/grokking-concurrency/java/part-6.md +69 -69
  448. package/lib/assets/docs/article/grokking-concurrency/java/part-7.md +101 -101
  449. package/lib/assets/docs/article/grokking-concurrency/java/part-8.md +101 -101
  450. package/lib/assets/docs/article/grokking-concurrency/python/index.md +313 -313
  451. package/lib/assets/docs/article/grokking-concurrency/python/part-1.md +239 -239
  452. package/lib/assets/docs/article/grokking-concurrency/python/part-2.md +418 -418
  453. package/lib/assets/docs/article/grokking-concurrency/python/part-3.md +227 -227
  454. package/lib/assets/docs/article/grokking-concurrency/python/part-4.md +299 -299
  455. package/lib/assets/docs/article/grokking-concurrency/python/part-5.md +315 -315
  456. package/lib/assets/docs/article/grokking-concurrency/python/part-6.md +297 -297
  457. package/lib/assets/docs/article/grokking-concurrency/python/part-7.md +314 -314
  458. package/lib/assets/docs/article/grokking-concurrency/python/part-8.md +360 -360
  459. package/lib/assets/docs/article/grokking-concurrency/rust/index.md +270 -270
  460. package/lib/assets/docs/article/grokking-concurrency/rust/part-1.md +108 -108
  461. package/lib/assets/docs/article/grokking-concurrency/rust/part-2.md +120 -120
  462. package/lib/assets/docs/article/grokking-concurrency/rust/part-3.md +126 -126
  463. package/lib/assets/docs/article/grokking-concurrency/rust/part-4.md +175 -175
  464. package/lib/assets/docs/article/grokking-concurrency/rust/part-5.md +158 -158
  465. package/lib/assets/docs/article/grokking-concurrency/rust/part-6.md +94 -94
  466. package/lib/assets/docs/article/grokking-concurrency/rust/part-7.md +133 -133
  467. package/lib/assets/docs/article/grokking-concurrency/rust/part-8.md +155 -155
  468. package/lib/assets/docs/article/grokking-concurrency/scala/index.md +69 -69
  469. package/lib/assets/docs/article/grokking-concurrency/scala/part-1.md +78 -78
  470. package/lib/assets/docs/article/grokking-concurrency/scala/part-2.md +112 -112
  471. package/lib/assets/docs/article/grokking-concurrency/scala/part-3.md +93 -93
  472. package/lib/assets/docs/article/grokking-concurrency/scala/part-4.md +110 -110
  473. package/lib/assets/docs/article/grokking-concurrency/scala/part-5.md +119 -119
  474. package/lib/assets/docs/article/grokking-concurrency/scala/part-6.md +83 -83
  475. package/lib/assets/docs/article/grokking-concurrency/scala/part-7.md +131 -131
  476. package/lib/assets/docs/article/grokking-concurrency/scala/part-8.md +129 -129
  477. package/lib/assets/docs/article/grokkingfp/all/index.md +368 -368
  478. package/lib/assets/docs/article/grokkingfp/all/part-1-ch01-fp-introduction.md +530 -530
  479. package/lib/assets/docs/article/grokkingfp/all/part-1-ch02-pure-functions.md +923 -923
  480. package/lib/assets/docs/article/grokkingfp/all/part-2-ch03-immutable-data.md +1128 -1128
  481. package/lib/assets/docs/article/grokkingfp/all/part-2-ch04-higher-order-functions.md +1104 -1104
  482. package/lib/assets/docs/article/grokkingfp/all/part-2-ch05-flatmap.md +1026 -1026
  483. package/lib/assets/docs/article/grokkingfp/all/part-3-ch06-option.md +785 -785
  484. package/lib/assets/docs/article/grokkingfp/all/part-3-ch07-either-adt.md +871 -871
  485. package/lib/assets/docs/article/grokkingfp/all/part-4-ch08-io-monad.md +972 -972
  486. package/lib/assets/docs/article/grokkingfp/all/part-4-ch09-streams.md +926 -926
  487. package/lib/assets/docs/article/grokkingfp/all/part-5-ch10-concurrency.md +870 -870
  488. package/lib/assets/docs/article/grokkingfp/all/part-6-ch11-application.md +715 -715
  489. package/lib/assets/docs/article/grokkingfp/all/part-6-ch12-testing.md +626 -626
  490. package/lib/assets/docs/article/grokkingfp/all/writing-plan.md +712 -712
  491. package/lib/assets/docs/article/grokkingfp/clojure/index.md +276 -276
  492. package/lib/assets/docs/article/grokkingfp/clojure/part-1.md +667 -667
  493. package/lib/assets/docs/article/grokkingfp/clojure/part-2.md +643 -643
  494. package/lib/assets/docs/article/grokkingfp/clojure/part-3.md +620 -620
  495. package/lib/assets/docs/article/grokkingfp/clojure/part-4.md +697 -697
  496. package/lib/assets/docs/article/grokkingfp/clojure/part-5.md +751 -751
  497. package/lib/assets/docs/article/grokkingfp/clojure/part-6.md +721 -721
  498. package/lib/assets/docs/article/grokkingfp/csharp/index.md +246 -246
  499. package/lib/assets/docs/article/grokkingfp/csharp/part-1.md +811 -811
  500. package/lib/assets/docs/article/grokkingfp/csharp/part-2.md +971 -971
  501. package/lib/assets/docs/article/grokkingfp/csharp/part-3.md +981 -981
  502. package/lib/assets/docs/article/grokkingfp/csharp/part-4.md +949 -949
  503. package/lib/assets/docs/article/grokkingfp/csharp/part-5.md +947 -947
  504. package/lib/assets/docs/article/grokkingfp/csharp/part-6.md +739 -739
  505. package/lib/assets/docs/article/grokkingfp/elixir/index.md +203 -203
  506. package/lib/assets/docs/article/grokkingfp/elixir/part-1.md +712 -712
  507. package/lib/assets/docs/article/grokkingfp/elixir/part-2.md +838 -838
  508. package/lib/assets/docs/article/grokkingfp/elixir/part-3.md +985 -985
  509. package/lib/assets/docs/article/grokkingfp/elixir/part-4.md +974 -974
  510. package/lib/assets/docs/article/grokkingfp/elixir/part-5.md +1286 -1286
  511. package/lib/assets/docs/article/grokkingfp/elixir/part-6.md +1049 -1049
  512. package/lib/assets/docs/article/grokkingfp/fsharp/index.md +210 -210
  513. package/lib/assets/docs/article/grokkingfp/fsharp/part-1.md +714 -714
  514. package/lib/assets/docs/article/grokkingfp/fsharp/part-2.md +961 -961
  515. package/lib/assets/docs/article/grokkingfp/fsharp/part-3.md +972 -972
  516. package/lib/assets/docs/article/grokkingfp/fsharp/part-4.md +832 -832
  517. package/lib/assets/docs/article/grokkingfp/fsharp/part-5.md +911 -911
  518. package/lib/assets/docs/article/grokkingfp/fsharp/part-6.md +922 -922
  519. package/lib/assets/docs/article/grokkingfp/haskell/index.md +234 -234
  520. package/lib/assets/docs/article/grokkingfp/haskell/part-1.md +591 -591
  521. package/lib/assets/docs/article/grokkingfp/haskell/part-2.md +866 -866
  522. package/lib/assets/docs/article/grokkingfp/haskell/part-3.md +915 -915
  523. package/lib/assets/docs/article/grokkingfp/haskell/part-4.md +878 -878
  524. package/lib/assets/docs/article/grokkingfp/haskell/part-5.md +845 -845
  525. package/lib/assets/docs/article/grokkingfp/haskell/part-6.md +844 -844
  526. package/lib/assets/docs/article/grokkingfp/index.md +143 -143
  527. package/lib/assets/docs/article/grokkingfp/java/index.md +211 -211
  528. package/lib/assets/docs/article/grokkingfp/java/part-1.md +648 -648
  529. package/lib/assets/docs/article/grokkingfp/java/part-2.md +675 -675
  530. package/lib/assets/docs/article/grokkingfp/java/part-3.md +672 -672
  531. package/lib/assets/docs/article/grokkingfp/java/part-4.md +771 -771
  532. package/lib/assets/docs/article/grokkingfp/java/part-5.md +959 -959
  533. package/lib/assets/docs/article/grokkingfp/java/part-6.md +1328 -1328
  534. package/lib/assets/docs/article/grokkingfp/python/index.md +258 -258
  535. package/lib/assets/docs/article/grokkingfp/python/part-1.md +443 -443
  536. package/lib/assets/docs/article/grokkingfp/python/part-2.md +958 -958
  537. package/lib/assets/docs/article/grokkingfp/python/part-3.md +1004 -1004
  538. package/lib/assets/docs/article/grokkingfp/python/part-4.md +765 -765
  539. package/lib/assets/docs/article/grokkingfp/python/part-5.md +747 -747
  540. package/lib/assets/docs/article/grokkingfp/python/part-6.md +861 -861
  541. package/lib/assets/docs/article/grokkingfp/ruby/index.md +330 -330
  542. package/lib/assets/docs/article/grokkingfp/ruby/part-1.md +755 -755
  543. package/lib/assets/docs/article/grokkingfp/ruby/part-2.md +938 -938
  544. package/lib/assets/docs/article/grokkingfp/ruby/part-3.md +946 -946
  545. package/lib/assets/docs/article/grokkingfp/ruby/part-4.md +921 -921
  546. package/lib/assets/docs/article/grokkingfp/ruby/part-5.md +908 -908
  547. package/lib/assets/docs/article/grokkingfp/ruby/part-6.md +1412 -1412
  548. package/lib/assets/docs/article/grokkingfp/rust/index.md +242 -242
  549. package/lib/assets/docs/article/grokkingfp/rust/part-1.md +634 -634
  550. package/lib/assets/docs/article/grokkingfp/rust/part-2.md +1060 -1060
  551. package/lib/assets/docs/article/grokkingfp/rust/part-3.md +994 -994
  552. package/lib/assets/docs/article/grokkingfp/rust/part-4.md +573 -573
  553. package/lib/assets/docs/article/grokkingfp/rust/part-5.md +705 -705
  554. package/lib/assets/docs/article/grokkingfp/rust/part-6.md +508 -508
  555. package/lib/assets/docs/article/grokkingfp/scala/index.md +171 -171
  556. package/lib/assets/docs/article/grokkingfp/scala/part-1.md +543 -543
  557. package/lib/assets/docs/article/grokkingfp/scala/part-2.md +946 -946
  558. package/lib/assets/docs/article/grokkingfp/scala/part-3.md +919 -919
  559. package/lib/assets/docs/article/grokkingfp/scala/part-4.md +742 -742
  560. package/lib/assets/docs/article/grokkingfp/scala/part-5.md +722 -722
  561. package/lib/assets/docs/article/grokkingfp/scala/part-6.md +867 -867
  562. package/lib/assets/docs/article/grokkingfp/typescript/index.md +273 -273
  563. package/lib/assets/docs/article/grokkingfp/typescript/part-1.md +561 -561
  564. package/lib/assets/docs/article/grokkingfp/typescript/part-2.md +1129 -1129
  565. package/lib/assets/docs/article/grokkingfp/typescript/part-3.md +842 -842
  566. package/lib/assets/docs/article/grokkingfp/typescript/part-4.md +1087 -1087
  567. package/lib/assets/docs/article/grokkingfp/typescript/part-5.md +717 -717
  568. package/lib/assets/docs/article/grokkingfp/typescript/part-6.md +982 -982
  569. package/lib/assets/docs/article/practical-database-design/index.md +121 -121
  570. package/lib/assets/docs/article/practical-database-design/part1/chapter01.md +288 -288
  571. package/lib/assets/docs/article/practical-database-design/part1/chapter02.md +518 -518
  572. package/lib/assets/docs/article/practical-database-design/part1/chapter03.md +557 -557
  573. package/lib/assets/docs/article/practical-database-design/part2/chapter04.md +924 -924
  574. package/lib/assets/docs/article/practical-database-design/part2/chapter05.md +1627 -1627
  575. package/lib/assets/docs/article/practical-database-design/part2/chapter06.md +2716 -2716
  576. package/lib/assets/docs/article/practical-database-design/part2/chapter07.md +2082 -2082
  577. package/lib/assets/docs/article/practical-database-design/part2/chapter08.md +2105 -2105
  578. package/lib/assets/docs/article/practical-database-design/part2/chapter09.md +2031 -2031
  579. package/lib/assets/docs/article/practical-database-design/part2/chapter10.md +1387 -1387
  580. package/lib/assets/docs/article/practical-database-design/part2/chapter11.md +1677 -1677
  581. package/lib/assets/docs/article/practical-database-design/part2/chapter12.md +1417 -1417
  582. package/lib/assets/docs/article/practical-database-design/part2/chapter13.md +1434 -1434
  583. package/lib/assets/docs/article/practical-database-design/part3/chapter14.md +667 -667
  584. package/lib/assets/docs/article/practical-database-design/part3/chapter15.md +1625 -1625
  585. package/lib/assets/docs/article/practical-database-design/part3/chapter16.md +1915 -1915
  586. package/lib/assets/docs/article/practical-database-design/part3/chapter17.md +1708 -1708
  587. package/lib/assets/docs/article/practical-database-design/part3/chapter18.md +2095 -2095
  588. package/lib/assets/docs/article/practical-database-design/part3/chapter19.md +1123 -1123
  589. package/lib/assets/docs/article/practical-database-design/part3/chapter20.md +1031 -1031
  590. package/lib/assets/docs/article/practical-database-design/part3/chapter21.md +1382 -1382
  591. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter14-orm.md +991 -991
  592. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter15-orm.md +1300 -1300
  593. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter16-orm.md +1166 -1166
  594. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter17-orm.md +1584 -1584
  595. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter18-orm.md +1183 -1183
  596. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter19-orm.md +1016 -1016
  597. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter20-orm.md +1753 -1753
  598. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter21-orm.md +1447 -1447
  599. package/lib/assets/docs/article/practical-database-design/part3-orm/chapter22-orm.md +1878 -1878
  600. package/lib/assets/docs/article/practical-database-design/part4/chapter22.md +965 -965
  601. package/lib/assets/docs/article/practical-database-design/part4/chapter23.md +2069 -2069
  602. package/lib/assets/docs/article/practical-database-design/part4/chapter24.md +2439 -2439
  603. package/lib/assets/docs/article/practical-database-design/part4/chapter25.md +3661 -3661
  604. package/lib/assets/docs/article/practical-database-design/part4/chapter26.md +2916 -2916
  605. package/lib/assets/docs/article/practical-database-design/part4/chapter27.md +3105 -3105
  606. package/lib/assets/docs/article/practical-database-design/part4/chapter28.md +2697 -2697
  607. package/lib/assets/docs/article/practical-database-design/part4/chapter29.md +2544 -2544
  608. package/lib/assets/docs/article/practical-database-design/part4/chapter30.md +2180 -2180
  609. package/lib/assets/docs/article/practical-database-design/part4/chapter31.md +1192 -1192
  610. package/lib/assets/docs/article/practical-database-design/part4/chapter32.md +2101 -2101
  611. package/lib/assets/docs/article/practical-database-design/part5/chapter33.md +1032 -1032
  612. package/lib/assets/docs/article/practical-database-design/part5/chapter34.md +1609 -1609
  613. package/lib/assets/docs/article/practical-database-design/part5/chapter35.md +1453 -1453
  614. package/lib/assets/docs/article/practical-database-design/part5/chapter36.md +1292 -1292
  615. package/lib/assets/docs/article/practical-database-design/part5/chapter37.md +1470 -1470
  616. package/lib/assets/docs/article/practical-database-design/part5/chapter38.md +1698 -1698
  617. package/lib/assets/docs/article/practical-database-design/part5/chapter39.md +2334 -2334
  618. package/lib/assets/docs/article/practical-database-design/study/study2-1.md +1693 -1693
  619. package/lib/assets/docs/article/practical-database-design/study/study2-2.md +1347 -1347
  620. package/lib/assets/docs/article/practical-database-design/study/study2-3.md +2044 -2044
  621. package/lib/assets/docs/article/practical-database-design/study/study2-4.md +2229 -2229
  622. package/lib/assets/docs/article/practical-database-design/study/study2-5.md +2418 -2418
  623. package/lib/assets/docs/article/practical-database-design/study/study3-1.md +2205 -2205
  624. package/lib/assets/docs/article/practical-database-design/study/study3-2.md +2221 -2221
  625. package/lib/assets/docs/article/practical-database-design/study/study3-3.md +2253 -2253
  626. package/lib/assets/docs/article/practical-database-design/study/study3-4.md +2106 -2106
  627. package/lib/assets/docs/article/practical-database-design/study/study3-5.md +2507 -2507
  628. package/lib/assets/docs/article/practical-database-design/study/study4-1.md +2587 -2587
  629. package/lib/assets/docs/article/practical-database-design/study/study4-2.md +2075 -2075
  630. package/lib/assets/docs/article/practical-database-design/study/study4-3.md +1805 -1805
  631. package/lib/assets/docs/article/practical-database-design/study/study4-4.md +1895 -1895
  632. package/lib/assets/docs/article/practical-database-design/study/study4-5.md +2878 -2878
  633. package/lib/assets/docs/assets/css/extra.css +29 -29
  634. package/lib/assets/docs/assets/js/extra.js +44 -44
  635. package/lib/assets/docs/development/index.md +39 -39
  636. package/lib/assets/docs/operation/index.md +11 -11
  637. package/lib/assets/docs/reference/CodexCLIMCP/343/202/242/343/203/227/343/203/252/343/202/261/343/203/274/343/202/267/343/203/247/343/203/263/351/226/213/347/231/272/343/203/225/343/203/255/343/203/274.md +532 -532
  638. package/lib/assets/docs/reference/CodexCLIMCP/343/202/265/343/203/274/343/203/220/343/203/274/350/250/255/345/256/232/346/211/213/351/240/206.md +341 -341
  639. package/lib/assets/docs/reference/Java/343/202/242/343/203/227/343/203/252/343/202/261/343/203/274/343/202/267/343/203/247/343/203/263/347/222/260/345/242/203/346/247/213/347/257/211/343/202/254/343/202/244/343/203/211.md +581 -581
  640. package/lib/assets/docs/reference/SonarQube/343/203/255/343/203/274/343/202/253/343/203/253/347/222/260/345/242/203/343/202/273/343/203/203/343/203/210/343/202/242/343/203/203/343/203/227/346/211/213/351/240/206/346/233/270.md +642 -642
  641. package/lib/assets/docs/reference/TypeScript/343/202/242/343/203/227/343/203/252/343/202/261/343/203/274/343/202/267/343/203/247/343/203/263/347/222/260/345/242/203/346/247/213/347/257/211/343/202/254/343/202/244/343/203/211.md +465 -465
  642. package/lib/assets/docs/reference/UI/350/250/255/350/250/210/343/202/254/343/202/244/343/203/211.md +450 -450
  643. package/lib/assets/docs/reference/images/Ansoff.drawio.svg +3 -3
  644. package/lib/assets/docs/reference/images/BrandBasicStrategy.drawio.svg +3 -3
  645. package/lib/assets/docs/reference/images/BrandCategorization.drawio.svg +3 -3
  646. package/lib/assets/docs/reference/images/BrandRecurutementStrategy.drawio.svg +3 -3
  647. package/lib/assets/docs/reference/images/BrandValue.drawio.svg +3 -3
  648. package/lib/assets/docs/reference/images/BusinessActivitiy.svg +3 -3
  649. package/lib/assets/docs/reference/images/HRM.drawio.svg +3 -3
  650. package/lib/assets/docs/reference/images/MarketingStructure.drawio.svg +3 -3
  651. package/lib/assets/docs/reference/images/OrganizationElemnts.svg +3 -3
  652. package/lib/assets/docs/reference/images/PPM.drawio.svg +3 -3
  653. package/lib/assets/docs/reference/images/PositioningMap.drawio.svg +3 -3
  654. package/lib/assets/docs/reference/images/ProductLayer.drawio.svg +3 -3
  655. package/lib/assets/docs/reference/images/ProductMix.drawio.svg +3 -3
  656. package/lib/assets/docs/reference/images/SWOT.drawio.svg +3 -3
  657. package/lib/assets/docs/reference/images/TargetMarket.drawio.svg +3 -3
  658. package/lib/assets/docs/reference/images/ThreeGenericStrategies.drawio.svg +3 -3
  659. package/lib/assets/docs/reference/images/VRIO.drawio.svg +3 -3
  660. package/lib/assets/docs/reference/images/ValueChain.drawio.svg +3 -3
  661. package/lib/assets/docs/reference/index.md +52 -52
  662. package/lib/assets/docs/reference//343/202/210/343/201/204/343/202/275/343/203/225/343/203/210/343/202/246/343/202/247/343/202/242/343/201/250/343/201/257.md +250 -250
  663. package/lib/assets/docs/reference//343/202/242/343/203/274/343/202/255/343/203/206/343/202/257/343/203/201/343/203/243/350/250/255/350/250/210/343/202/254/343/202/244/343/203/211.md +2216 -2216
  664. package/lib/assets/docs/reference//343/202/244/343/203/263/343/203/225/343/203/251/350/250/255/350/250/210/343/202/254/343/202/244/343/203/211.md +1878 -1878
  665. package/lib/assets/docs/reference//343/202/250/343/202/257/343/202/271/343/203/210/343/203/252/343/203/274/343/203/240/343/203/227/343/203/255/343/202/260/343/203/251/343/203/237/343/203/263/343/202/260.md +550 -550
  666. package/lib/assets/docs/reference//343/202/263/343/203/274/343/203/207/343/202/243/343/203/263/343/202/260/343/201/250/343/203/206/343/202/271/343/203/210/343/202/254/343/202/244/343/203/211.md +705 -705
  667. package/lib/assets/docs/reference//343/203/206/343/202/271/343/203/210/346/210/246/347/225/245/343/202/254/343/202/244/343/203/211.md +1313 -1313
  668. package/lib/assets/docs/reference//343/203/207/343/203/274/343/202/277/343/203/242/343/203/207/343/203/253/350/250/255/350/250/210/343/202/254/343/202/244/343/203/211.md +311 -311
  669. package/lib/assets/docs/reference//343/203/211/343/203/241/343/202/244/343/203/263/343/203/242/343/203/207/343/203/253/350/250/255/350/250/210/343/202/254/343/202/244/343/203/211.md +599 -599
  670. package/lib/assets/docs/reference//343/203/223/343/202/270/343/203/215/343/202/271/343/202/242/343/203/274/343/202/255/343/203/206/343/202/257/343/203/201/343/203/243/345/210/206/346/236/220/343/202/254/343/202/244/343/203/211.md +528 -528
  671. package/lib/assets/docs/reference//343/203/246/343/203/274/343/202/271/343/202/261/343/203/274/343/202/271/344/275/234/346/210/220/343/202/254/343/202/244/343/203/211.md +689 -689
  672. package/lib/assets/docs/reference//343/203/252/343/203/252/343/203/274/343/202/271/343/202/254/343/202/244/343/203/211.md +461 -461
  673. package/lib/assets/docs/reference//343/203/252/343/203/252/343/203/274/343/202/271/343/203/273/343/202/244/343/203/206/343/203/254/343/203/274/343/202/267/343/203/247/343/203/263/350/250/210/347/224/273/343/202/254/343/202/244/343/203/211.md +580 -580
  674. package/lib/assets/docs/reference//343/203/255/343/202/270/343/202/253/343/203/253/343/202/267/343/203/263/343/202/255/343/203/263/343/202/260.md +1367 -1367
  675. package/lib/assets/docs/reference//344/274/201/346/245/255/347/265/214/345/226/266/350/253/226.md +2637 -2637
  676. package/lib/assets/docs/reference//347/222/260/345/242/203/345/244/211/346/225/260/347/256/241/347/220/206/343/202/254/343/202/244/343/203/211.md +665 -665
  677. package/lib/assets/docs/reference//350/246/201/344/273/266/345/256/232/347/276/251/343/202/254/343/202/244/343/203/211.md +1248 -1248
  678. package/lib/assets/docs/reference//350/250/200/350/252/236/345/210/245/351/226/213/347/231/272/343/202/254/343/202/244/343/203/211.md +518 -518
  679. package/lib/assets/docs/reference//351/201/213/345/226/266/347/256/241/347/220/206.md +1482 -1482
  680. package/lib/assets/docs/reference//351/201/213/347/224/250/343/202/271/343/202/257/343/203/252/343/203/227/343/203/210/344/275/234/346/210/220/343/202/254/343/202/244/343/203/211.md +421 -421
  681. package/lib/assets/docs/reference//351/201/213/347/224/250/350/246/201/344/273/266/345/256/232/347/276/251/343/202/254/343/202/244/343/203/211.md +392 -392
  682. package/lib/assets/docs/reference//351/226/213/347/231/272/343/202/254/343/202/244/343/203/211.md +299 -299
  683. package/lib/assets/docs/reference//351/235/236/346/251/237/350/203/275/350/246/201/344/273/266/345/256/232/347/276/251/343/202/254/343/202/244/343/203/211.md +1236 -1236
  684. package/lib/assets/docs/review/index.md +5 -5
  685. package/lib/assets/docs/strategy/index.md +1 -1
  686. package/lib/assets/docs/template/ADR.md +30 -30
  687. package/lib/assets/docs/template/AWS/343/202/271/343/203/206/343/203/274/343/202/270/343/203/263/343/202/260/347/222/260/345/242/203/343/202/273/343/203/203/343/203/210/343/202/242/343/203/203/343/203/227/346/211/213/351/240/206/346/233/270.md +1366 -1366
  688. package/lib/assets/docs/template/AWS/343/203/227/343/203/255/343/203/200/343/202/257/343/202/267/343/203/247/343/203/263/347/222/260/345/242/203/343/202/273/343/203/203/343/203/210/343/202/242/343/203/203/343/203/227/346/211/213/351/240/206/346/233/270.md +634 -634
  689. package/lib/assets/docs/template/README.md +50 -50
  690. package/lib/assets/docs/template/index.md +23 -23
  691. package/lib/assets/docs/template//343/201/276/343/201/232/343/201/223/343/202/214/343/202/222/350/252/255/343/202/202/343/201/206/343/203/252/343/202/271/343/203/210.md +12 -12
  692. package/lib/assets/docs/template//343/202/242/343/203/227/343/203/252/343/202/261/343/203/274/343/202/267/343/203/247/343/203/263/351/226/213/347/231/272/347/222/260/345/242/203/343/202/273/343/203/203/343/203/210/343/202/242/343/203/203/343/203/227/346/211/213/351/240/206/346/233/270.md +547 -547
  693. package/lib/assets/docs/template//343/202/244/343/203/206/343/203/254/343/203/274/343/202/267/343/203/247/343/203/263/345/256/214/344/272/206/345/240/261/345/221/212/346/233/270.md +58 -58
  694. package/lib/assets/docs/template//343/202/244/343/203/263/343/202/273/343/203/227/343/202/267/343/203/247/343/203/263/343/203/207/343/203/203/343/202/255.md +13 -13
  695. package/lib/assets/docs/template//343/203/223/343/202/270/343/203/215/343/202/271/343/202/242/343/203/274/343/202/255/343/203/206/343/202/257/343/203/201/343/203/243.md +379 -379
  696. package/lib/assets/docs/template//344/274/201/346/245/255/345/210/206/346/236/220.md +573 -573
  697. package/lib/assets/docs/template//345/256/214/345/205/250/345/275/242/345/274/217/343/201/256/343/203/246/343/203/274/343/202/271/343/202/261/343/203/274/343/202/271.md +69 -69
  698. package/lib/assets/docs/template//350/246/201/344/273/266/345/256/232/347/276/251.md +669 -669
  699. package/lib/assets/docs/template//350/250/255/350/250/210.md +173 -173
  700. package/lib/assets/docs/template//351/226/213/347/231/272/347/222/260/345/242/203/343/202/273/343/203/203/343/203/210/343/202/242/343/203/203/343/203/227/346/211/213/351/240/206/346/233/270.md +688 -688
  701. package/lib/assets/gulpfile.js +25 -25
  702. package/lib/assets/mkdocs.yml +136 -136
  703. package/lib/assets/ops/docker/mkdoc/Dockerfile +19 -19
  704. package/lib/assets/ops/scripts/journal.js +180 -180
  705. package/lib/assets/ops/scripts/mkdocs.js +82 -82
  706. package/lib/assets/ops/scripts/release.js +431 -431
  707. package/lib/assets/ops/scripts/sonar_local.js +726 -726
  708. package/lib/assets/ops/scripts/ssh.js +190 -190
  709. package/lib/assets/ops/scripts/vault.js +299 -299
  710. package/lib/assets/package-lock.json +1653 -1653
  711. package/lib/assets/package.json +40 -40
  712. package/lib/gulpfile.js +37 -37
  713. package/package.json +41 -41
@@ -1,2082 +1,2082 @@
1
- # 第7章:債権管理の設計
2
-
3
- 販売管理システムにおいて、売上が発生した後の請求・入金管理は経営の根幹を支える重要な業務です。本章では、請求業務と回収業務のデータベース設計と実装を行います。
4
-
5
- ## 債権管理の全体像
6
-
7
- 債権管理は「売上 → 請求 → 入金 → 消込」という一連のフローで構成されます。
8
-
9
- ```plantuml
10
- @startuml
11
-
12
- title 債権管理フロー
13
-
14
- |営業部門|
15
- start
16
- :売上計上;
17
- note right
18
- 第6章で実装した
19
- 売上データ
20
- end note
21
-
22
- |経理部門|
23
- :請求データ作成;
24
- note right
25
- 都度請求 or 締め請求
26
- 回収予定日の設定
27
- end note
28
-
29
- :請求書発行;
30
-
31
- |財務部門|
32
- :入金確認;
33
- note right
34
- 銀行振込
35
- 現金
36
- 手形
37
- end note
38
-
39
- :入金消込;
40
- note right
41
- 請求と入金を
42
- 突合・消込
43
- end note
44
-
45
- if (全額入金?) then (yes)
46
- :請求完了;
47
- else (no)
48
- :残高管理;
49
- note right
50
- 売掛金残高を
51
- 更新
52
- end note
53
- endif
54
-
55
- :月次債権レポート;
56
-
57
- stop
58
-
59
- @enduml
60
- ```
61
-
62
- ### 債権管理で扱うデータ
63
-
64
- | データ | 説明 |
65
- |-------|------|
66
- | **請求データ** | 顧客への請求情報(請求金額、請求日、回収予定日) |
67
- | **請求明細** | 請求に含まれる売上データの明細 |
68
- | **入金データ** | 顧客からの入金情報(入金金額、入金方法) |
69
- | **入金消込明細** | 入金と請求の突合情報 |
70
- | **売掛金残高** | 顧客別の債権残高 |
71
-
72
- ---
73
-
74
- ## 7.1 請求業務の DB 設計
75
-
76
- ### 請求業務フローの理解
77
-
78
- 請求業務には「都度請求」と「締め請求」の2つのパターンがあります。
79
-
80
- ```plantuml
81
- @startuml
82
-
83
- title 請求パターンの比較
84
-
85
- rectangle "都度請求" #CCFFCC {
86
- card "売上発生" as s1
87
- card "即時請求" as i1
88
- s1 --> i1
89
- }
90
-
91
- rectangle "締め請求(月次)" #CCCCFF {
92
- card "売上1" as s2a
93
- card "売上2" as s2b
94
- card "売上3" as s2c
95
- card "締処理" as c
96
- card "月次請求" as i2
97
- s2a --> c
98
- s2b --> c
99
- s2c --> c
100
- c --> i2
101
- }
102
-
103
- note bottom of "都度請求"
104
- 取引の都度、請求書を発行
105
- - 単発取引
106
- - 現金取引
107
- end note
108
-
109
- note bottom of "締め請求(月次)"
110
- 月末に一括請求
111
- - 継続取引
112
- - 掛取引
113
- end note
114
-
115
- @enduml
116
- ```
117
-
118
- | 請求パターン | 特徴 | 用途 |
119
- |------------|------|------|
120
- | **都度請求** | 売上発生時に即座に請求 | 単発取引、現金取引 |
121
- | **締め請求** | 月末など締日にまとめて請求 | 継続取引、掛売り |
122
-
123
- ### 締処理の概念
124
-
125
- 締め請求では、顧客ごとに設定された締日に基づいて売上を集約します。
126
-
127
- ```plantuml
128
- @startuml
129
-
130
- title 締処理のタイムライン
131
-
132
- rectangle "1月" #FFFFCC {
133
- card "1/5 売上 10,000円" as s1
134
- card "1/15 売上 25,000円" as s2
135
- card "1/25 売上 15,000円" as s3
136
- card "1/31 締処理" as c1
137
- }
138
-
139
- rectangle "2月" #CCFFFF {
140
- card "2/10 売上 20,000円" as s4
141
- card "2/20 売上 30,000円" as s5
142
- card "2/28 締処理" as c2
143
- }
144
-
145
- s1 --> c1
146
- s2 --> c1
147
- s3 --> c1
148
- s4 --> c2
149
- s5 --> c2
150
-
151
- note right of c1
152
- 1月請求
153
- 売上合計: 50,000円
154
- 消費税: 5,000円
155
- 請求額: 55,000円
156
- end note
157
-
158
- note right of c2
159
- 2月請求
160
- 繰越残高: 55,000円
161
- 今月売上: 50,000円
162
- 消費税: 5,000円
163
- 請求額: 110,000円
164
- end note
165
-
166
- @enduml
167
- ```
168
-
169
- ### 請求データ・請求明細の構造
170
-
171
- #### 請求データの ER 図
172
-
173
- ```plantuml
174
- @startuml
175
-
176
- title 請求関連テーブル
177
-
178
- entity 売上データ {
179
- ID <<PK>>
180
- --
181
- 売上番号 <<UK>>
182
- 売上日
183
- 顧客コード <<FK>>
184
- 売上金額
185
- 消費税額
186
- 売上合計
187
- ステータス
188
- }
189
-
190
- entity 請求データ {
191
- ID <<PK>>
192
- --
193
- 請求番号 <<UK>>
194
- 請求日
195
- 請求先コード <<FK>>
196
- 顧客コード <<FK>>
197
- 締日
198
- 請求区分
199
- 前回請求残高
200
- 入金額
201
- 繰越残高
202
- 今回売上額
203
- 今回消費税額
204
- 今回請求額
205
- 請求残高
206
- 回収予定日
207
- ステータス
208
- バージョン
209
- 作成日時
210
- 更新日時
211
- }
212
-
213
- entity 請求明細 {
214
- ID <<PK>>
215
- --
216
- 請求ID <<FK>>
217
- 行番号
218
- 売上ID <<FK>>
219
- 売上番号
220
- 売上日
221
- 売上金額
222
- 消費税額
223
- 合計金額
224
- }
225
-
226
- entity 請求締履歴 {
227
- ID <<PK>>
228
- --
229
- 顧客コード <<FK>>
230
- 締年月
231
- 締日
232
- 売上件数
233
- 売上合計
234
- 消費税合計
235
- 請求ID <<FK>>
236
- 処理日時
237
- }
238
-
239
- 売上データ ||--o{ 請求明細
240
- 請求データ ||--o{ 請求明細
241
- 請求データ ||--o| 請求締履歴
242
-
243
- @enduml
244
- ```
245
-
246
- ### 請求ステータスの定義
247
-
248
- ```plantuml
249
- @startuml
250
-
251
- title 請求ステータス遷移図
252
-
253
- [*] --> 未発行
254
-
255
- 未発行 --> 発行済 : 請求書発行
256
-
257
- 発行済 --> 一部入金 : 一部入金
258
- 発行済 --> 入金済 : 全額入金
259
-
260
- 一部入金 --> 入金済 : 残額入金
261
-
262
- 入金済 --> [*]
263
-
264
- 発行済 --> 回収遅延 : 回収予定日超過
265
- 一部入金 --> 回収遅延 : 回収予定日超過
266
-
267
- @enduml
268
- ```
269
-
270
- | ステータス | 説明 |
271
- |-----------|------|
272
- | **未発行** | 請求データ作成済み、請求書未発行 |
273
- | **発行済** | 請求書を発行した状態 |
274
- | **一部入金** | 一部入金があり、残高がある状態 |
275
- | **入金済** | 全額入金が完了した状態 |
276
- | **回収遅延** | 回収予定日を過ぎても未回収の状態 |
277
-
278
- ### マイグレーション:請求関連テーブルの作成
279
-
280
- <details>
281
- <summary>V011__create_invoice_tables.sql</summary>
282
-
283
- ```sql
284
- -- src/main/resources/db/migration/V011__create_invoice_tables.sql
285
-
286
- -- 請求ステータス
287
- CREATE TYPE 請求ステータス AS ENUM ('未発行', '発行済', '一部入金', '入金済', '回収遅延');
288
-
289
- -- 請求区分は V001 で既に定義済み('都度', '締め')
290
-
291
- -- 請求データ(ヘッダ)
292
- CREATE TABLE "請求データ" (
293
- "ID" SERIAL PRIMARY KEY,
294
- "請求番号" VARCHAR(20) UNIQUE NOT NULL,
295
- "請求日" DATE NOT NULL,
296
- "請求先コード" VARCHAR(20) NOT NULL,
297
- "顧客コード" VARCHAR(20) NOT NULL,
298
- "顧客枝番" VARCHAR(10) DEFAULT '00',
299
- "締日" DATE,
300
- "請求区分" 請求区分 NOT NULL,
301
- "前回請求残高" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
302
- "入金額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
303
- "繰越残高" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
304
- "今回売上額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
305
- "今回消費税額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
306
- "今回請求額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
307
- "請求残高" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
308
- "回収予定日" DATE,
309
- "ステータス" 請求ステータス DEFAULT '未発行' NOT NULL,
310
- "備考" TEXT,
311
- "バージョン" INTEGER DEFAULT 1 NOT NULL,
312
- "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
313
- "作成者" VARCHAR(50),
314
- "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
315
- "更新者" VARCHAR(50),
316
- CONSTRAINT "fk_請求データ_顧客"
317
- FOREIGN KEY ("顧客コード", "顧客枝番") REFERENCES "顧客マスタ"("顧客コード", "顧客枝番")
318
- );
319
-
320
- -- 請求明細
321
- CREATE TABLE "請求明細" (
322
- "ID" SERIAL PRIMARY KEY,
323
- "請求ID" INTEGER NOT NULL,
324
- "行番号" INTEGER NOT NULL,
325
- "売上ID" INTEGER,
326
- "売上番号" VARCHAR(20),
327
- "売上日" DATE,
328
- "売上金額" DECIMAL(15, 2) NOT NULL,
329
- "消費税額" DECIMAL(15, 2) NOT NULL,
330
- "合計金額" DECIMAL(15, 2) NOT NULL,
331
- CONSTRAINT "fk_請求明細_請求"
332
- FOREIGN KEY ("請求ID") REFERENCES "請求データ"("ID") ON DELETE CASCADE,
333
- CONSTRAINT "fk_請求明細_売上"
334
- FOREIGN KEY ("売上ID") REFERENCES "売上データ"("ID"),
335
- CONSTRAINT "uk_請求明細_請求_行" UNIQUE ("請求ID", "行番号")
336
- );
337
-
338
- -- 請求締履歴
339
- CREATE TABLE "請求締履歴" (
340
- "ID" SERIAL PRIMARY KEY,
341
- "顧客コード" VARCHAR(20) NOT NULL,
342
- "顧客枝番" VARCHAR(10) DEFAULT '00',
343
- "締年月" VARCHAR(7) NOT NULL,
344
- "締日" DATE NOT NULL,
345
- "売上件数" INTEGER NOT NULL,
346
- "売上合計" DECIMAL(15, 2) NOT NULL,
347
- "消費税合計" DECIMAL(15, 2) NOT NULL,
348
- "請求ID" INTEGER,
349
- "処理日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
350
- CONSTRAINT "fk_請求締履歴_顧客"
351
- FOREIGN KEY ("顧客コード", "顧客枝番") REFERENCES "顧客マスタ"("顧客コード", "顧客枝番"),
352
- CONSTRAINT "fk_請求締履歴_請求"
353
- FOREIGN KEY ("請求ID") REFERENCES "請求データ"("ID"),
354
- CONSTRAINT "uk_請求締履歴_顧客_年月" UNIQUE ("顧客コード", "顧客枝番", "締年月")
355
- );
356
-
357
- -- 売掛金残高
358
- CREATE TABLE "売掛金残高" (
359
- "ID" SERIAL PRIMARY KEY,
360
- "顧客コード" VARCHAR(20) NOT NULL,
361
- "顧客枝番" VARCHAR(10) DEFAULT '00',
362
- "基準日" DATE NOT NULL,
363
- "前月残高" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
364
- "当月売上" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
365
- "当月入金" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
366
- "当月残高" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
367
- "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
368
- "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
369
- CONSTRAINT "fk_売掛金残高_顧客"
370
- FOREIGN KEY ("顧客コード", "顧客枝番") REFERENCES "顧客マスタ"("顧客コード", "顧客枝番"),
371
- CONSTRAINT "uk_売掛金残高_顧客_基準日" UNIQUE ("顧客コード", "顧客枝番", "基準日")
372
- );
373
-
374
- -- インデックス
375
- CREATE INDEX "idx_請求データ_顧客コード" ON "請求データ"("顧客コード");
376
- CREATE INDEX "idx_請求データ_請求日" ON "請求データ"("請求日");
377
- CREATE INDEX "idx_請求データ_ステータス" ON "請求データ"("ステータス");
378
- CREATE INDEX "idx_請求明細_請求ID" ON "請求明細"("請求ID");
379
- CREATE INDEX "idx_売掛金残高_基準日" ON "売掛金残高"("基準日");
380
-
381
- -- テーブルコメント
382
- COMMENT ON TABLE "請求データ" IS '請求ヘッダ情報を管理するテーブル';
383
- COMMENT ON TABLE "請求明細" IS '請求に含まれる売上明細を管理するテーブル';
384
- COMMENT ON TABLE "請求締履歴" IS '月次締処理の履歴を管理するテーブル';
385
- COMMENT ON TABLE "売掛金残高" IS '顧客別月次売掛金残高を管理するテーブル';
386
-
387
- -- カラムコメント
388
- COMMENT ON COLUMN "請求データ"."バージョン" IS '楽観ロック用バージョン番号';
389
- ```
390
-
391
- </details>
392
-
393
- ### 請求エンティティの実装
394
-
395
- <details>
396
- <summary>請求ステータス ENUM</summary>
397
-
398
- ```java
399
- // src/main/java/com/example/sms/domain/model/invoice/InvoiceStatus.java
400
- package com.example.sms.domain.model.invoice;
401
-
402
- import lombok.Getter;
403
- import lombok.RequiredArgsConstructor;
404
-
405
- /**
406
- * 請求ステータス.
407
- */
408
- @Getter
409
- @RequiredArgsConstructor
410
- public enum InvoiceStatus {
411
- DRAFT("未発行"),
412
- ISSUED("発行済"),
413
- PARTIALLY_PAID("一部入金"),
414
- PAID("入金済"),
415
- OVERDUE("回収遅延");
416
-
417
- private final String displayName;
418
-
419
- /**
420
- * 表示名から請求ステータスを取得する.
421
- *
422
- * @param displayName 表示名
423
- * @return 請求ステータス
424
- */
425
- public static InvoiceStatus fromDisplayName(String displayName) {
426
- for (InvoiceStatus status : values()) {
427
- if (status.displayName.equals(displayName)) {
428
- return status;
429
- }
430
- }
431
- throw new IllegalArgumentException("不正な請求ステータス: " + displayName);
432
- }
433
- }
434
- ```
435
-
436
- </details>
437
-
438
- <details>
439
- <summary>請求区分 ENUM</summary>
440
-
441
- ```java
442
- // src/main/java/com/example/sms/domain/model/invoice/InvoiceType.java
443
- package com.example.sms.domain.model.invoice;
444
-
445
- import lombok.Getter;
446
- import lombok.RequiredArgsConstructor;
447
-
448
- /**
449
- * 請求区分.
450
- */
451
- @Getter
452
- @RequiredArgsConstructor
453
- public enum InvoiceType {
454
- IMMEDIATE("都度"),
455
- CLOSING("締め");
456
-
457
- private final String displayName;
458
-
459
- /**
460
- * 表示名から請求区分を取得する.
461
- *
462
- * @param displayName 表示名
463
- * @return 請求区分
464
- */
465
- public static InvoiceType fromDisplayName(String displayName) {
466
- for (InvoiceType type : values()) {
467
- if (type.displayName.equals(displayName)) {
468
- return type;
469
- }
470
- }
471
- throw new IllegalArgumentException("不正な請求区分: " + displayName);
472
- }
473
- }
474
- ```
475
-
476
- </details>
477
-
478
- <details>
479
- <summary>請求エンティティ</summary>
480
-
481
- ```java
482
- // src/main/java/com/example/sms/domain/model/invoice/Invoice.java
483
- package com.example.sms.domain.model.invoice;
484
-
485
- import lombok.AllArgsConstructor;
486
- import lombok.Builder;
487
- import lombok.Data;
488
- import lombok.NoArgsConstructor;
489
-
490
- import java.math.BigDecimal;
491
- import java.time.LocalDate;
492
- import java.time.LocalDateTime;
493
- import java.util.ArrayList;
494
- import java.util.List;
495
-
496
- /**
497
- * 請求エンティティ.
498
- */
499
- @Data
500
- @Builder
501
- @NoArgsConstructor
502
- @AllArgsConstructor
503
- @SuppressWarnings("PMD.RedundantFieldInitializer")
504
- public class Invoice {
505
- private Integer id;
506
- private String invoiceNumber;
507
- private LocalDate invoiceDate;
508
- private String billingCode;
509
- private String customerCode;
510
- private String customerBranchNumber;
511
- private LocalDate closingDate;
512
- @Builder.Default
513
- private InvoiceType invoiceType = InvoiceType.CLOSING;
514
- @Builder.Default
515
- private BigDecimal previousBalance = BigDecimal.ZERO;
516
- @Builder.Default
517
- private BigDecimal receiptAmount = BigDecimal.ZERO;
518
- @Builder.Default
519
- private BigDecimal carriedBalance = BigDecimal.ZERO;
520
- @Builder.Default
521
- private BigDecimal currentSalesAmount = BigDecimal.ZERO;
522
- @Builder.Default
523
- private BigDecimal currentTaxAmount = BigDecimal.ZERO;
524
- @Builder.Default
525
- private BigDecimal currentInvoiceAmount = BigDecimal.ZERO;
526
- @Builder.Default
527
- private BigDecimal invoiceBalance = BigDecimal.ZERO;
528
- private LocalDate dueDate;
529
- @Builder.Default
530
- private InvoiceStatus status = InvoiceStatus.DRAFT;
531
- private String remarks;
532
- private LocalDateTime createdAt;
533
- private String createdBy;
534
- private LocalDateTime updatedAt;
535
- private String updatedBy;
536
-
537
- /** 楽観ロック用バージョン. */
538
- @Builder.Default
539
- private Integer version = 1;
540
-
541
- @Builder.Default
542
- private List<InvoiceDetail> details = new ArrayList<>();
543
-
544
- /**
545
- * 請求残高を計算する.
546
- *
547
- * @return 請求残高
548
- */
549
- public BigDecimal calculateInvoiceBalance() {
550
- return carriedBalance.add(currentInvoiceAmount).subtract(receiptAmount);
551
- }
552
-
553
- /**
554
- * 繰越残高を計算する.
555
- *
556
- * @return 繰越残高
557
- */
558
- public BigDecimal calculateCarriedBalance() {
559
- return previousBalance.subtract(receiptAmount);
560
- }
561
- }
562
- ```
563
-
564
- </details>
565
-
566
- <details>
567
- <summary>請求明細エンティティ</summary>
568
-
569
- ```java
570
- // src/main/java/com/example/sms/domain/model/invoice/InvoiceDetail.java
571
- package com.example.sms.domain.model.invoice;
572
-
573
- import lombok.AllArgsConstructor;
574
- import lombok.Builder;
575
- import lombok.Data;
576
- import lombok.NoArgsConstructor;
577
-
578
- import java.math.BigDecimal;
579
- import java.time.LocalDate;
580
-
581
- /**
582
- * 請求明細エンティティ.
583
- */
584
- @Data
585
- @Builder
586
- @NoArgsConstructor
587
- @AllArgsConstructor
588
- public class InvoiceDetail {
589
- private Integer id;
590
- private Integer invoiceId;
591
- private Integer lineNumber;
592
- private Integer salesId;
593
- private String salesNumber;
594
- private LocalDate salesDate;
595
- private BigDecimal salesAmount;
596
- private BigDecimal taxAmount;
597
- private BigDecimal totalAmount;
598
- }
599
- ```
600
-
601
- </details>
602
-
603
- ### 締処理サービスの実装
604
-
605
- <details>
606
- <summary>締処理サービス</summary>
607
-
608
- ```java
609
- // src/main/java/com/example/sms/application/service/ClosingService.java
610
- package com.example.sms.application.service;
611
-
612
- import com.example.sms.application.port.out.*;
613
- import com.example.sms.domain.model.invoice.*;
614
- import com.example.sms.domain.model.sales.Sales;
615
- import lombok.RequiredArgsConstructor;
616
- import org.springframework.stereotype.Service;
617
- import org.springframework.transaction.annotation.Transactional;
618
-
619
- import java.math.BigDecimal;
620
- import java.time.LocalDate;
621
- import java.time.YearMonth;
622
- import java.util.List;
623
-
624
- @Service
625
- @RequiredArgsConstructor
626
- public class ClosingService {
627
-
628
- private final InvoiceRepository invoiceRepository;
629
- private final SalesRepository salesRepository;
630
- private final CustomerRepository customerRepository;
631
- private final ClosingHistoryRepository closingHistoryRepository;
632
-
633
- /**
634
- * 月次締処理を実行する
635
- */
636
- @Transactional
637
- public Invoice executeMonthlyClosing(String customerCode, YearMonth yearMonth) {
638
- LocalDate closingDate = yearMonth.atEndOfMonth();
639
-
640
- // 既に締処理済みかチェック
641
- if (closingHistoryRepository.existsByCustomerAndYearMonth(customerCode, yearMonth)) {
642
- throw new IllegalStateException("既に締処理が実行されています: " + yearMonth);
643
- }
644
-
645
- // 対象期間の売上を取得
646
- LocalDate fromDate = yearMonth.atDay(1);
647
- LocalDate toDate = yearMonth.atEndOfMonth();
648
- List<Sales> salesList = salesRepository.findByCustomerCodeAndDateRange(
649
- customerCode, fromDate, toDate);
650
-
651
- if (salesList.isEmpty()) {
652
- throw new IllegalStateException("対象期間の売上がありません");
653
- }
654
-
655
- // 売上合計・消費税を集計
656
- BigDecimal totalSales = salesList.stream()
657
- .map(Sales::getSalesAmount)
658
- .reduce(BigDecimal.ZERO, BigDecimal::add);
659
- BigDecimal totalTax = salesList.stream()
660
- .map(Sales::getTaxAmount)
661
- .reduce(BigDecimal.ZERO, BigDecimal::add);
662
-
663
- // 前回請求残高を取得
664
- BigDecimal previousBalance = invoiceRepository
665
- .findLatestByCustomerCode(customerCode)
666
- .map(Invoice::getInvoiceBalance)
667
- .orElse(BigDecimal.ZERO);
668
-
669
- // 顧客の回収サイトから回収予定日を計算
670
- var customer = customerRepository.findByCustomerCode(customerCode)
671
- .orElseThrow(() -> new IllegalArgumentException("顧客が見つかりません"));
672
- LocalDate dueDate = calculateDueDate(closingDate, customer.getCollectionSite());
673
-
674
- // 請求データを作成
675
- var invoice = Invoice.builder()
676
- .invoiceNumber(generateInvoiceNumber())
677
- .invoiceDate(closingDate)
678
- .billingCode(customerCode)
679
- .customerCode(customerCode)
680
- .closingDate(closingDate)
681
- .invoiceType(InvoiceType.CLOSING)
682
- .previousBalance(previousBalance)
683
- .receiptAmount(BigDecimal.ZERO)
684
- .carriedBalance(previousBalance)
685
- .currentSalesAmount(totalSales)
686
- .currentTaxAmount(totalTax)
687
- .currentInvoiceAmount(totalSales.add(totalTax))
688
- .invoiceBalance(previousBalance.add(totalSales).add(totalTax))
689
- .dueDate(dueDate)
690
- .status(InvoiceStatus.DRAFT)
691
- .build();
692
-
693
- invoiceRepository.save(invoice);
694
-
695
- // 請求明細を作成
696
- int lineNumber = 1;
697
- for (Sales sales : salesList) {
698
- var detail = InvoiceDetail.builder()
699
- .invoiceId(invoice.getId())
700
- .lineNumber(lineNumber++)
701
- .salesId(sales.getId())
702
- .salesNumber(sales.getSalesNumber())
703
- .salesDate(sales.getSalesDate())
704
- .salesAmount(sales.getSalesAmount())
705
- .taxAmount(sales.getTaxAmount())
706
- .totalAmount(sales.getSalesTotal())
707
- .build();
708
- invoiceRepository.saveDetail(detail);
709
- }
710
-
711
- // 締履歴を保存
712
- var history = ClosingHistory.builder()
713
- .customerCode(customerCode)
714
- .closingYearMonth(yearMonth.toString())
715
- .closingDate(closingDate)
716
- .salesCount(salesList.size())
717
- .salesTotal(totalSales)
718
- .taxTotal(totalTax)
719
- .invoiceId(invoice.getId())
720
- .build();
721
- closingHistoryRepository.save(history);
722
-
723
- return invoice;
724
- }
725
-
726
- private LocalDate calculateDueDate(LocalDate closingDate, Integer collectionSite) {
727
- if (collectionSite == null) {
728
- collectionSite = 30; // デフォルト30日
729
- }
730
- return closingDate.plusDays(collectionSite);
731
- }
732
-
733
- private String generateInvoiceNumber() {
734
- return String.format("INV-%d-%04d",
735
- LocalDate.now().getYear(), System.currentTimeMillis() % 10000);
736
- }
737
- }
738
- ```
739
-
740
- </details>
741
-
742
- ---
743
-
744
- ## 7.2 回収業務の DB 設計
745
-
746
- ### 入金業務フローの理解
747
-
748
- 入金業務は、顧客からの入金を記録し、請求と突合(消込)する業務です。
749
-
750
- ```plantuml
751
- @startuml
752
-
753
- title 入金業務フロー
754
-
755
- |財務部門|
756
- start
757
- :銀行明細確認;
758
- note right
759
- 毎日の入金情報を
760
- 銀行から取得
761
- end note
762
-
763
- :入金データ登録;
764
- note right
765
- 入金日
766
- 顧客コード
767
- 入金金額
768
- 入金方法
769
- end note
770
-
771
- :入金消込;
772
-
773
- if (消込方法?) then (自動消込)
774
- :自動マッチング;
775
- note right
776
- 顧客コード + 金額
777
- で自動消込
778
- end note
779
- else (手動消込)
780
- :請求選択;
781
- :消込金額入力;
782
- endif
783
-
784
- if (過入金?) then (yes)
785
- :前受金計上;
786
- else (no)
787
- endif
788
-
789
- :消込完了;
790
-
791
- stop
792
-
793
- @enduml
794
- ```
795
-
796
- ### 入金方法の種類
797
-
798
- ```plantuml
799
- @startuml
800
-
801
- title 入金方法の分類
802
-
803
- rectangle "入金方法" {
804
- rectangle "即時入金" #CCFFCC {
805
- card "現金" as cash
806
- card "銀行振込" as transfer
807
- card "クレジットカード" as credit
808
- }
809
-
810
- rectangle "期日入金" #FFFFCC {
811
- card "手形" as bill
812
- card "電子記録債権" as denshi
813
- }
814
- }
815
-
816
- note bottom of cash
817
- 店頭での現金受取
818
- end note
819
-
820
- note bottom of transfer
821
- 銀行振込による入金
822
- 振込手数料の扱いに注意
823
- end note
824
-
825
- note bottom of bill
826
- 約束手形
827
- 期日に換金
828
- end note
829
-
830
- @enduml
831
- ```
832
-
833
- | 入金方法 | 特徴 | 手数料 |
834
- |---------|------|-------|
835
- | **現金** | 即時入金、店頭取引 | なし |
836
- | **銀行振込** | 最も一般的、振込手数料が発生 | 振込手数料(顧客負担 or 当社負担) |
837
- | **クレジットカード** | カード会社経由で入金 | カード手数料 |
838
- | **手形** | 期日に換金、不渡りリスク | なし |
839
- | **電子記録債権** | 電子的な債権管理 | 利用料 |
840
-
841
- ### 入金消込のパターン
842
-
843
- 入金と請求の消込には、以下のパターンがあります。
844
-
845
- ```plantuml
846
- @startuml
847
-
848
- title 入金消込のパターン
849
-
850
- rectangle "1:1 消込" #CCFFCC {
851
- card "入金 100,000円" as r1
852
- card "請求 100,000円" as i1
853
- r1 --> i1 : 全額消込
854
- }
855
-
856
- rectangle "1:N 消込(過入金)" #FFFFCC {
857
- card "入金 200,000円" as r2
858
- card "請求A 80,000円" as i2a
859
- card "請求B 80,000円" as i2b
860
- card "前受金 40,000円" as adv
861
- r2 --> i2a : 80,000
862
- r2 --> i2b : 80,000
863
- r2 --> adv : 40,000
864
- }
865
-
866
- rectangle "N:1 消込(分割入金)" #FFCCCC {
867
- card "入金1 50,000円" as r3a
868
- card "入金2 50,000円" as r3b
869
- card "請求 100,000円" as i3
870
- r3a --> i3 : 50,000
871
- r3b --> i3 : 50,000
872
- }
873
-
874
- @enduml
875
- ```
876
-
877
- | パターン | 説明 | 処理 |
878
- |---------|------|------|
879
- | **1:1** | 1件の入金で1件の請求を消込 | 通常の消込 |
880
- | **1:N** | 1件の入金で複数の請求を消込 | 古い請求から順に消込、余剰は前受金 |
881
- | **N:1** | 複数の入金で1件の請求を消込 | 分割入金、一部入金状態の管理 |
882
-
883
- ### 入金データ・入金消込明細の構造
884
-
885
- #### 入金データの ER 図
886
-
887
- ```plantuml
888
- @startuml
889
-
890
- title 入金関連テーブル
891
-
892
- entity 請求データ {
893
- ID <<PK>>
894
- --
895
- 請求番号
896
- 顧客コード
897
- 請求残高
898
- ステータス
899
- }
900
-
901
- entity 入金データ {
902
- ID <<PK>>
903
- --
904
- 入金番号 <<UK>>
905
- 入金日
906
- 顧客コード <<FK>>
907
- 入金方法
908
- 入金金額
909
- 消込済金額
910
- 未消込金額
911
- 手数料
912
- 振込名義
913
- 銀行名
914
- 口座番号
915
- ステータス
916
- バージョン
917
- 備考
918
- 作成日時
919
- 更新日時
920
- }
921
-
922
- entity 入金消込明細 {
923
- ID <<PK>>
924
- --
925
- 入金ID <<FK>>
926
- 行番号
927
- 請求ID <<FK>>
928
- 消込日
929
- 消込金額
930
- バージョン
931
- 備考
932
- 作成日時
933
- }
934
-
935
- entity 前受金データ {
936
- ID <<PK>>
937
- --
938
- 前受金番号 <<UK>>
939
- 発生日
940
- 顧客コード <<FK>>
941
- 入金ID <<FK>>
942
- 前受金額
943
- 使用済金額
944
- 残高
945
- 備考
946
- 作成日時
947
- 更新日時
948
- }
949
-
950
- 請求データ ||--o{ 入金消込明細
951
- 入金データ ||--o{ 入金消込明細
952
- 入金データ ||--o| 前受金データ
953
-
954
- @enduml
955
- ```
956
-
957
- ### 入金ステータスの定義
958
-
959
- ```plantuml
960
- @startuml
961
-
962
- title 入金ステータス遷移図
963
-
964
- [*] --> 入金済
965
-
966
- 入金済 --> 消込済 : 全額消込
967
- 入金済 --> 一部消込 : 一部消込
968
-
969
- 一部消込 --> 消込済 : 残額消込
970
- 一部消込 --> 過入金 : 余剰発生
971
-
972
- 入金済 --> 過入金 : 過入金発生
973
-
974
- 消込済 --> [*]
975
- 過入金 --> [*]
976
-
977
- @enduml
978
- ```
979
-
980
- | ステータス | 説明 |
981
- |-----------|------|
982
- | **入金済** | 入金登録完了、未消込状態 |
983
- | **一部消込** | 一部が消込済み |
984
- | **消込済** | 全額が消込完了 |
985
- | **過入金** | 入金額が請求額を超過 |
986
-
987
- ### マイグレーション:入金関連テーブルの作成
988
-
989
- <details>
990
- <summary>V012__create_receipt_tables.sql</summary>
991
-
992
- ```sql
993
- -- src/main/resources/db/migration/V012__create_receipt_tables.sql
994
-
995
- -- 入金ステータス
996
- CREATE TYPE 入金ステータス AS ENUM ('入金済', '一部消込', '消込済', '過入金');
997
-
998
- -- 入金方法(取引先の支払方法とは別)
999
- CREATE TYPE 入金方法 AS ENUM ('現金', '銀行振込', 'クレジットカード', '手形', '電子記録債権');
1000
-
1001
- -- 入金データ
1002
- CREATE TABLE "入金データ" (
1003
- "ID" SERIAL PRIMARY KEY,
1004
- "入金番号" VARCHAR(20) UNIQUE NOT NULL,
1005
- "入金日" DATE NOT NULL,
1006
- "顧客コード" VARCHAR(20) NOT NULL,
1007
- "顧客枝番" VARCHAR(10) DEFAULT '00',
1008
- "入金方法" 入金方法 NOT NULL,
1009
- "入金金額" DECIMAL(15, 2) NOT NULL,
1010
- "消込済金額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
1011
- "未消込金額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
1012
- "手数料" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
1013
- "振込名義" VARCHAR(100),
1014
- "銀行名" VARCHAR(50),
1015
- "口座番号" VARCHAR(20),
1016
- "ステータス" 入金ステータス DEFAULT '入金済' NOT NULL,
1017
- "備考" TEXT,
1018
- "バージョン" INTEGER DEFAULT 1 NOT NULL,
1019
- "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1020
- "作成者" VARCHAR(50),
1021
- "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1022
- "更新者" VARCHAR(50),
1023
- CONSTRAINT "fk_入金データ_顧客"
1024
- FOREIGN KEY ("顧客コード", "顧客枝番") REFERENCES "顧客マスタ"("顧客コード", "顧客枝番")
1025
- );
1026
-
1027
- -- 入金消込明細
1028
- CREATE TABLE "入金消込明細" (
1029
- "ID" SERIAL PRIMARY KEY,
1030
- "入金ID" INTEGER NOT NULL,
1031
- "行番号" INTEGER NOT NULL,
1032
- "請求ID" INTEGER,
1033
- "消込日" DATE NOT NULL,
1034
- "消込金額" DECIMAL(15, 2) NOT NULL,
1035
- "備考" TEXT,
1036
- "バージョン" INTEGER DEFAULT 1 NOT NULL,
1037
- "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1038
- CONSTRAINT "fk_入金消込明細_入金"
1039
- FOREIGN KEY ("入金ID") REFERENCES "入金データ"("ID") ON DELETE CASCADE,
1040
- CONSTRAINT "fk_入金消込明細_請求"
1041
- FOREIGN KEY ("請求ID") REFERENCES "請求データ"("ID"),
1042
- CONSTRAINT "uk_入金消込明細_入金_行" UNIQUE ("入金ID", "行番号")
1043
- );
1044
-
1045
- -- 前受金データ
1046
- CREATE TABLE "前受金データ" (
1047
- "ID" SERIAL PRIMARY KEY,
1048
- "前受金番号" VARCHAR(20) UNIQUE NOT NULL,
1049
- "発生日" DATE NOT NULL,
1050
- "顧客コード" VARCHAR(20) NOT NULL,
1051
- "顧客枝番" VARCHAR(10) DEFAULT '00',
1052
- "入金ID" INTEGER,
1053
- "前受金額" DECIMAL(15, 2) NOT NULL,
1054
- "使用済金額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
1055
- "残高" DECIMAL(15, 2) NOT NULL,
1056
- "備考" TEXT,
1057
- "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1058
- "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1059
- CONSTRAINT "fk_前受金データ_顧客"
1060
- FOREIGN KEY ("顧客コード", "顧客枝番") REFERENCES "顧客マスタ"("顧客コード", "顧客枝番"),
1061
- CONSTRAINT "fk_前受金データ_入金"
1062
- FOREIGN KEY ("入金ID") REFERENCES "入金データ"("ID")
1063
- );
1064
-
1065
- -- インデックス
1066
- CREATE INDEX "idx_入金データ_顧客コード" ON "入金データ"("顧客コード");
1067
- CREATE INDEX "idx_入金データ_入金日" ON "入金データ"("入金日");
1068
- CREATE INDEX "idx_入金データ_ステータス" ON "入金データ"("ステータス");
1069
- CREATE INDEX "idx_入金消込明細_入金ID" ON "入金消込明細"("入金ID");
1070
- CREATE INDEX "idx_入金消込明細_請求ID" ON "入金消込明細"("請求ID");
1071
- CREATE INDEX "idx_前受金データ_顧客コード" ON "前受金データ"("顧客コード");
1072
-
1073
- -- テーブルコメント
1074
- COMMENT ON TABLE "入金データ" IS '入金情報を管理するテーブル';
1075
- COMMENT ON TABLE "入金消込明細" IS '入金と請求の消込明細を管理するテーブル';
1076
- COMMENT ON TABLE "前受金データ" IS '過入金による前受金を管理するテーブル';
1077
-
1078
- -- カラムコメント
1079
- COMMENT ON COLUMN "入金データ"."バージョン" IS '楽観ロック用バージョン番号';
1080
- COMMENT ON COLUMN "入金消込明細"."バージョン" IS '楽観ロック用バージョン番号';
1081
- ```
1082
-
1083
- </details>
1084
-
1085
- ### 入金エンティティの実装
1086
-
1087
- <details>
1088
- <summary>入金ステータス ENUM</summary>
1089
-
1090
- ```java
1091
- // src/main/java/com/example/sms/domain/model/receipt/ReceiptStatus.java
1092
- package com.example.sms.domain.model.receipt;
1093
-
1094
- import lombok.Getter;
1095
- import lombok.RequiredArgsConstructor;
1096
-
1097
- /**
1098
- * 入金ステータス.
1099
- */
1100
- @Getter
1101
- @RequiredArgsConstructor
1102
- public enum ReceiptStatus {
1103
- RECEIVED("入金済"),
1104
- PARTIALLY_APPLIED("一部消込"),
1105
- APPLIED("消込済"),
1106
- OVERPAID("過入金");
1107
-
1108
- private final String displayName;
1109
-
1110
- /**
1111
- * 表示名から入金ステータスを取得する.
1112
- *
1113
- * @param displayName 表示名
1114
- * @return 入金ステータス
1115
- */
1116
- public static ReceiptStatus fromDisplayName(String displayName) {
1117
- for (ReceiptStatus status : values()) {
1118
- if (status.displayName.equals(displayName)) {
1119
- return status;
1120
- }
1121
- }
1122
- throw new IllegalArgumentException("不正な入金ステータス: " + displayName);
1123
- }
1124
- }
1125
- ```
1126
-
1127
- </details>
1128
-
1129
- <details>
1130
- <summary>入金方法 ENUM</summary>
1131
-
1132
- ```java
1133
- // src/main/java/com/example/sms/domain/model/receipt/ReceiptMethod.java
1134
- package com.example.sms.domain.model.receipt;
1135
-
1136
- import lombok.Getter;
1137
- import lombok.RequiredArgsConstructor;
1138
-
1139
- /**
1140
- * 入金方法.
1141
- */
1142
- @Getter
1143
- @RequiredArgsConstructor
1144
- public enum ReceiptMethod {
1145
- CASH("現金"),
1146
- BANK_TRANSFER("銀行振込"),
1147
- CREDIT_CARD("クレジットカード"),
1148
- BILL("手形"),
1149
- ELECTRONIC_BOND("電子記録債権");
1150
-
1151
- private final String displayName;
1152
-
1153
- /**
1154
- * 表示名から入金方法を取得する.
1155
- *
1156
- * @param displayName 表示名
1157
- * @return 入金方法
1158
- */
1159
- public static ReceiptMethod fromDisplayName(String displayName) {
1160
- for (ReceiptMethod method : values()) {
1161
- if (method.displayName.equals(displayName)) {
1162
- return method;
1163
- }
1164
- }
1165
- throw new IllegalArgumentException("不正な入金方法: " + displayName);
1166
- }
1167
- }
1168
- ```
1169
-
1170
- </details>
1171
-
1172
- <details>
1173
- <summary>入金エンティティ</summary>
1174
-
1175
- ```java
1176
- // src/main/java/com/example/sms/domain/model/receipt/Receipt.java
1177
- package com.example.sms.domain.model.receipt;
1178
-
1179
- import lombok.AllArgsConstructor;
1180
- import lombok.Builder;
1181
- import lombok.Data;
1182
- import lombok.NoArgsConstructor;
1183
-
1184
- import java.math.BigDecimal;
1185
- import java.time.LocalDate;
1186
- import java.time.LocalDateTime;
1187
- import java.util.ArrayList;
1188
- import java.util.List;
1189
-
1190
- /**
1191
- * 入金エンティティ.
1192
- */
1193
- @Data
1194
- @Builder
1195
- @NoArgsConstructor
1196
- @AllArgsConstructor
1197
- @SuppressWarnings("PMD.RedundantFieldInitializer")
1198
- public class Receipt {
1199
- private Integer id;
1200
- private String receiptNumber;
1201
- private LocalDate receiptDate;
1202
- private String customerCode;
1203
- private String customerBranchNumber;
1204
- private ReceiptMethod receiptMethod;
1205
- private BigDecimal receiptAmount;
1206
- @Builder.Default
1207
- private BigDecimal appliedAmount = BigDecimal.ZERO;
1208
- @Builder.Default
1209
- private BigDecimal unappliedAmount = BigDecimal.ZERO;
1210
- @Builder.Default
1211
- private BigDecimal bankFee = BigDecimal.ZERO;
1212
- private String payerName;
1213
- private String bankName;
1214
- private String accountNumber;
1215
- @Builder.Default
1216
- private ReceiptStatus status = ReceiptStatus.RECEIVED;
1217
- private String remarks;
1218
- private LocalDateTime createdAt;
1219
- private String createdBy;
1220
- private LocalDateTime updatedAt;
1221
- private String updatedBy;
1222
-
1223
- /** 楽観ロック用バージョン. */
1224
- @Builder.Default
1225
- private Integer version = 1;
1226
-
1227
- @Builder.Default
1228
- private List<ReceiptApplication> applications = new ArrayList<>();
1229
-
1230
- /**
1231
- * 未消込金額を計算する.
1232
- *
1233
- * @return 未消込金額
1234
- */
1235
- public BigDecimal calculateUnappliedAmount() {
1236
- return receiptAmount.subtract(appliedAmount).subtract(bankFee);
1237
- }
1238
-
1239
- /**
1240
- * 消込可能かどうかを判定する.
1241
- *
1242
- * @param amount 消込金額
1243
- * @return 消込可能な場合は true
1244
- */
1245
- public boolean canApply(BigDecimal amount) {
1246
- return unappliedAmount.compareTo(amount) >= 0;
1247
- }
1248
- }
1249
- ```
1250
-
1251
- </details>
1252
-
1253
- <details>
1254
- <summary>入金消込明細エンティティ</summary>
1255
-
1256
- ```java
1257
- // src/main/java/com/example/sms/domain/model/receipt/ReceiptApplication.java
1258
- package com.example.sms.domain.model.receipt;
1259
-
1260
- import lombok.AllArgsConstructor;
1261
- import lombok.Builder;
1262
- import lombok.Data;
1263
- import lombok.NoArgsConstructor;
1264
-
1265
- import java.math.BigDecimal;
1266
- import java.time.LocalDate;
1267
- import java.time.LocalDateTime;
1268
-
1269
- /**
1270
- * 入金消込明細エンティティ.
1271
- */
1272
- @Data
1273
- @Builder
1274
- @NoArgsConstructor
1275
- @AllArgsConstructor
1276
- @SuppressWarnings("PMD.RedundantFieldInitializer")
1277
- public class ReceiptApplication {
1278
- private Integer id;
1279
- private Integer receiptId;
1280
- private Integer lineNumber;
1281
- private Integer invoiceId;
1282
- private LocalDate applicationDate;
1283
- private BigDecimal appliedAmount;
1284
- private String remarks;
1285
- @Builder.Default
1286
- private Integer version = 1;
1287
- private LocalDateTime createdAt;
1288
- }
1289
- ```
1290
-
1291
- </details>
1292
-
1293
- ### 入金消込サービスの実装
1294
-
1295
- <details>
1296
- <summary>入金消込サービス</summary>
1297
-
1298
- ```java
1299
- // src/main/java/com/example/sms/application/service/ReceiptService.java
1300
- package com.example.sms.application.service;
1301
-
1302
- import com.example.sms.application.port.out.*;
1303
- import com.example.sms.domain.model.invoice.Invoice;
1304
- import com.example.sms.domain.model.invoice.InvoiceStatus;
1305
- import com.example.sms.domain.model.receipt.*;
1306
- import lombok.RequiredArgsConstructor;
1307
- import org.springframework.stereotype.Service;
1308
- import org.springframework.transaction.annotation.Transactional;
1309
-
1310
- import java.math.BigDecimal;
1311
- import java.time.LocalDate;
1312
- import java.util.ArrayList;
1313
- import java.util.List;
1314
-
1315
- @Service
1316
- @RequiredArgsConstructor
1317
- public class ReceiptService {
1318
-
1319
- private final ReceiptRepository receiptRepository;
1320
- private final InvoiceRepository invoiceRepository;
1321
- private final AdvanceReceiptRepository advanceReceiptRepository;
1322
-
1323
- /**
1324
- * 入金を登録する
1325
- */
1326
- @Transactional
1327
- public Receipt registerReceipt(Receipt receipt) {
1328
- receipt.setUnappliedAmount(
1329
- receipt.getReceiptAmount().subtract(receipt.getBankFee()));
1330
- receipt.setAppliedAmount(BigDecimal.ZERO);
1331
- receipt.setStatus(ReceiptStatus.RECEIVED);
1332
- receiptRepository.save(receipt);
1333
- return receipt;
1334
- }
1335
-
1336
- /**
1337
- * 入金を請求に消込する
1338
- */
1339
- @Transactional
1340
- public ReceiptApplication applyReceipt(
1341
- Integer receiptId, Integer invoiceId, BigDecimal amount) {
1342
-
1343
- var receipt = receiptRepository.findById(receiptId)
1344
- .orElseThrow(() -> new IllegalArgumentException(
1345
- "入金が見つかりません: " + receiptId));
1346
-
1347
- var invoice = invoiceRepository.findById(invoiceId)
1348
- .orElseThrow(() -> new IllegalArgumentException(
1349
- "請求が見つかりません: " + invoiceId));
1350
-
1351
- // バリデーション
1352
- if (!receipt.canApply(amount)) {
1353
- throw new IllegalStateException("消込可能金額が不足しています");
1354
- }
1355
- if (amount.compareTo(invoice.getInvoiceBalance()) > 0) {
1356
- throw new IllegalStateException("消込金額が請求残高を超えています");
1357
- }
1358
-
1359
- // 消込明細を作成
1360
- int lineNumber = getNextLineNumber(receiptId);
1361
- var application = ReceiptApplication.builder()
1362
- .receiptId(receiptId)
1363
- .lineNumber(lineNumber)
1364
- .invoiceId(invoiceId)
1365
- .applicationDate(LocalDate.now())
1366
- .appliedAmount(amount)
1367
- .build();
1368
- receiptRepository.saveApplication(application);
1369
-
1370
- // 入金の金額を更新
1371
- BigDecimal newAppliedAmount = receipt.getAppliedAmount().add(amount);
1372
- BigDecimal newUnappliedAmount = receipt.getUnappliedAmount().subtract(amount);
1373
- receiptRepository.updateAmounts(receiptId, newAppliedAmount, newUnappliedAmount);
1374
-
1375
- // 入金ステータスを更新
1376
- if (newUnappliedAmount.compareTo(BigDecimal.ZERO) <= 0) {
1377
- receiptRepository.updateStatus(receiptId, ReceiptStatus.APPLIED);
1378
- }
1379
-
1380
- // 請求の入金額を更新
1381
- invoiceRepository.updateReceiptAmount(invoiceId, amount);
1382
-
1383
- // 請求ステータスを更新
1384
- var updatedInvoice = invoiceRepository.findById(invoiceId).get();
1385
- if (updatedInvoice.getInvoiceBalance().compareTo(BigDecimal.ZERO) <= 0) {
1386
- invoiceRepository.updateStatus(invoiceId, InvoiceStatus.PAID);
1387
- } else {
1388
- invoiceRepository.updateStatus(invoiceId, InvoiceStatus.PARTIALLY_PAID);
1389
- }
1390
-
1391
- return application;
1392
- }
1393
-
1394
- /**
1395
- * 自動消込を実行する(古い請求から順に消込)
1396
- */
1397
- @Transactional
1398
- public List<ReceiptApplication> autoApply(Integer receiptId) {
1399
- var receipt = receiptRepository.findById(receiptId)
1400
- .orElseThrow(() -> new IllegalArgumentException(
1401
- "入金が見つかりません: " + receiptId));
1402
-
1403
- List<Invoice> unpaidInvoices = invoiceRepository
1404
- .findUnpaidByCustomerCode(receipt.getCustomerCode());
1405
-
1406
- BigDecimal remainingAmount = receipt.getUnappliedAmount();
1407
- List<ReceiptApplication> applications = new ArrayList<>();
1408
-
1409
- for (Invoice invoice : unpaidInvoices) {
1410
- if (remainingAmount.compareTo(BigDecimal.ZERO) <= 0) {
1411
- break;
1412
- }
1413
-
1414
- BigDecimal applyAmount = remainingAmount.min(invoice.getInvoiceBalance());
1415
- var application = applyReceipt(receiptId, invoice.getId(), applyAmount);
1416
- applications.add(application);
1417
-
1418
- remainingAmount = remainingAmount.subtract(applyAmount);
1419
- }
1420
-
1421
- // 過入金の場合は前受金として登録
1422
- if (remainingAmount.compareTo(BigDecimal.ZERO) > 0) {
1423
- createAdvanceReceipt(receipt.getCustomerCode(), receiptId, remainingAmount);
1424
- receiptRepository.updateStatus(receiptId, ReceiptStatus.OVERPAID);
1425
- }
1426
-
1427
- return applications;
1428
- }
1429
-
1430
- private void createAdvanceReceipt(
1431
- String customerCode, Integer receiptId, BigDecimal amount) {
1432
- var advanceReceipt = AdvanceReceipt.builder()
1433
- .advanceReceiptNumber(generateAdvanceReceiptNumber())
1434
- .occurredDate(LocalDate.now())
1435
- .customerCode(customerCode)
1436
- .receiptId(receiptId)
1437
- .advanceAmount(amount)
1438
- .usedAmount(BigDecimal.ZERO)
1439
- .balance(amount)
1440
- .build();
1441
- advanceReceiptRepository.save(advanceReceipt);
1442
- }
1443
-
1444
- private int getNextLineNumber(Integer receiptId) {
1445
- var applications = receiptRepository.findApplicationsByReceiptId(receiptId);
1446
- return applications.size() + 1;
1447
- }
1448
-
1449
- private String generateAdvanceReceiptNumber() {
1450
- return String.format("ADV-%d-%04d",
1451
- LocalDate.now().getYear(), System.currentTimeMillis() % 10000);
1452
- }
1453
- }
1454
- ```
1455
-
1456
- </details>
1457
-
1458
- ### 債権残高管理サービス
1459
-
1460
- <details>
1461
- <summary>売掛金残高サービス</summary>
1462
-
1463
- ```java
1464
- // src/main/java/com/example/sms/application/service/AccountsReceivableService.java
1465
- package com.example.sms.application.service;
1466
-
1467
- import com.example.sms.application.port.out.*;
1468
- import com.example.sms.domain.model.invoice.AccountsReceivable;
1469
- import lombok.RequiredArgsConstructor;
1470
- import org.springframework.stereotype.Service;
1471
- import org.springframework.transaction.annotation.Transactional;
1472
-
1473
- import java.math.BigDecimal;
1474
- import java.time.LocalDate;
1475
- import java.time.YearMonth;
1476
- import java.util.List;
1477
-
1478
- @Service
1479
- @RequiredArgsConstructor
1480
- public class AccountsReceivableService {
1481
-
1482
- private final AccountsReceivableRepository arRepository;
1483
- private final SalesRepository salesRepository;
1484
- private final ReceiptRepository receiptRepository;
1485
- private final CustomerRepository customerRepository;
1486
-
1487
- /**
1488
- * 月次売掛金残高を更新する
1489
- */
1490
- @Transactional
1491
- public void updateMonthlyBalance(YearMonth yearMonth) {
1492
- LocalDate baseDate = yearMonth.atEndOfMonth();
1493
- LocalDate fromDate = yearMonth.atDay(1);
1494
- LocalDate toDate = yearMonth.atEndOfMonth();
1495
-
1496
- var customers = customerRepository.findAll();
1497
-
1498
- for (var customer : customers) {
1499
- // 前月残高を取得
1500
- BigDecimal previousBalance = getPreviousMonthBalance(
1501
- customer.getCustomerCode(), yearMonth);
1502
-
1503
- // 当月売上を集計
1504
- BigDecimal currentSales = salesRepository
1505
- .sumSalesByCustomerAndDateRange(
1506
- customer.getCustomerCode(), fromDate, toDate);
1507
-
1508
- // 当月入金を集計
1509
- BigDecimal currentReceipts = receiptRepository
1510
- .sumReceiptsByCustomerAndDateRange(
1511
- customer.getCustomerCode(), fromDate, toDate);
1512
-
1513
- // 当月残高を計算
1514
- BigDecimal currentBalance = previousBalance
1515
- .add(currentSales)
1516
- .subtract(currentReceipts);
1517
-
1518
- // 売掛金残高を更新
1519
- var ar = AccountsReceivable.builder()
1520
- .customerCode(customer.getCustomerCode())
1521
- .baseDate(baseDate)
1522
- .previousMonthBalance(previousBalance)
1523
- .currentMonthSales(currentSales)
1524
- .currentMonthReceipts(currentReceipts)
1525
- .currentMonthBalance(currentBalance)
1526
- .build();
1527
- arRepository.upsert(ar);
1528
- }
1529
- }
1530
-
1531
- /**
1532
- * 顧客別売掛金残高を取得する
1533
- */
1534
- public List<AccountsReceivable> getCustomerBalances(LocalDate baseDate) {
1535
- return arRepository.findByBaseDate(baseDate);
1536
- }
1537
-
1538
- /**
1539
- * 滞留債権一覧を取得する
1540
- */
1541
- public List<AccountsReceivable> getOverdueBalances(
1542
- LocalDate currentDate, int overdueDays) {
1543
- LocalDate cutoffDate = currentDate.minusDays(overdueDays);
1544
- return arRepository.findOverdue(cutoffDate);
1545
- }
1546
-
1547
- private BigDecimal getPreviousMonthBalance(
1548
- String customerCode, YearMonth yearMonth) {
1549
- LocalDate previousMonthEnd = yearMonth.minusMonths(1).atEndOfMonth();
1550
- return arRepository.findByCustomerCodeAndBaseDate(
1551
- customerCode, previousMonthEnd)
1552
- .map(AccountsReceivable::getCurrentMonthBalance)
1553
- .orElse(BigDecimal.ZERO);
1554
- }
1555
- }
1556
- ```
1557
-
1558
- </details>
1559
-
1560
- ---
1561
-
1562
- ## 7.3 リレーションと楽観ロックの設計
1563
-
1564
- ### N+1 問題とその解決
1565
-
1566
- 請求データは、請求(ヘッダ)→ 請求明細の2層構造を持ちます。入金データも入金 → 入金消込明細の親子関係があります。これらのデータを効率的に取得するための設計を行います。
1567
-
1568
- ```plantuml
1569
- @startuml
1570
-
1571
- title N+1 問題の発生パターン
1572
-
1573
- rectangle "N+1 問題(非効率)" #FFCCCC {
1574
- card "1. 請求ヘッダ取得\n SELECT * FROM 請求データ\n (1回)" as q1
1575
- card "2. 各請求の明細取得\n SELECT * FROM 請求明細\n WHERE 請求ID = ?\n (N回)" as q2
1576
- }
1577
-
1578
- rectangle "解決策:JOINによる一括取得" #CCFFCC {
1579
- card "1回のJOINクエリで\n全データを取得\n SELECT i.*, id.*\n FROM 請求データ i\n LEFT JOIN 請求明細 id ON ..." as sol
1580
- }
1581
-
1582
- q1 --> q2 : N件
1583
- note right of q2
1584
- 10件の請求があれば
1585
- 合計11回のクエリ発行
1586
- end note
1587
-
1588
- @enduml
1589
- ```
1590
-
1591
- ### MyBatis ネストした ResultMap による関連付け
1592
-
1593
- <details>
1594
- <summary>請求データのリレーション設定(InvoiceMapper.xml)</summary>
1595
-
1596
- ```xml
1597
- <?xml version="1.0" encoding="UTF-8" ?>
1598
- <!DOCTYPE mapper
1599
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
1600
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
1601
- <mapper namespace="com.example.sms.infrastructure.persistence.mapper.InvoiceMapper">
1602
-
1603
- <!-- 請求(ヘッダ)の ResultMap -->
1604
- <resultMap id="InvoiceWithDetailsResultMap"
1605
- type="com.example.sms.domain.model.invoice.Invoice">
1606
- <id property="id" column="i_id"/>
1607
- <result property="invoiceNumber" column="i_請求番号"/>
1608
- <result property="invoiceDate" column="i_請求日"/>
1609
- <result property="billingCode" column="i_請求先コード"/>
1610
- <result property="customerCode" column="i_顧客コード"/>
1611
- <result property="closingDate" column="i_締日"/>
1612
- <result property="invoiceType" column="i_請求区分"
1613
- typeHandler="com.example.sms.infrastructure.persistence.typehandler.InvoiceTypeTypeHandler"/>
1614
- <result property="previousBalance" column="i_前回請求残高"/>
1615
- <result property="receiptAmount" column="i_入金額"/>
1616
- <result property="carriedBalance" column="i_繰越残高"/>
1617
- <result property="currentSalesAmount" column="i_今回売上額"/>
1618
- <result property="currentTaxAmount" column="i_今回消費税額"/>
1619
- <result property="currentInvoiceAmount" column="i_今回請求額"/>
1620
- <result property="invoiceBalance" column="i_請求残高"/>
1621
- <result property="dueDate" column="i_回収予定日"/>
1622
- <result property="status" column="i_ステータス"
1623
- typeHandler="com.example.sms.infrastructure.persistence.typehandler.InvoiceStatusTypeHandler"/>
1624
- <result property="version" column="i_バージョン"/>
1625
- <result property="createdAt" column="i_作成日時"/>
1626
- <result property="updatedAt" column="i_更新日時"/>
1627
- <!-- 請求明細との1:N関連 -->
1628
- <collection property="details"
1629
- ofType="com.example.sms.domain.model.invoice.InvoiceDetail"
1630
- resultMap="InvoiceDetailNestedResultMap"/>
1631
- </resultMap>
1632
-
1633
- <!-- 請求明細のネスト ResultMap -->
1634
- <resultMap id="InvoiceDetailNestedResultMap"
1635
- type="com.example.sms.domain.model.invoice.InvoiceDetail">
1636
- <id property="id" column="id_id"/>
1637
- <result property="invoiceId" column="id_請求ID"/>
1638
- <result property="lineNumber" column="id_行番号"/>
1639
- <result property="salesId" column="id_売上ID"/>
1640
- <result property="salesNumber" column="id_売上番号"/>
1641
- <result property="salesDate" column="id_売上日"/>
1642
- <result property="salesAmount" column="id_売上金額"/>
1643
- <result property="taxAmount" column="id_消費税額"/>
1644
- <result property="totalAmount" column="id_合計金額"/>
1645
- </resultMap>
1646
-
1647
- <!-- JOIN による一括取得クエリ -->
1648
- <select id="findWithDetailsByInvoiceNumber"
1649
- resultMap="InvoiceWithDetailsResultMap">
1650
- SELECT
1651
- i."ID" AS i_id,
1652
- i."請求番号" AS i_請求番号,
1653
- i."請求日" AS i_請求日,
1654
- i."請求先コード" AS i_請求先コード,
1655
- i."顧客コード" AS i_顧客コード,
1656
- i."締日" AS i_締日,
1657
- i."請求区分" AS i_請求区分,
1658
- i."前回請求残高" AS i_前回請求残高,
1659
- i."入金額" AS i_入金額,
1660
- i."繰越残高" AS i_繰越残高,
1661
- i."今回売上額" AS i_今回売上額,
1662
- i."今回消費税額" AS i_今回消費税額,
1663
- i."今回請求額" AS i_今回請求額,
1664
- i."請求残高" AS i_請求残高,
1665
- i."回収予定日" AS i_回収予定日,
1666
- i."ステータス" AS i_ステータス,
1667
- i."バージョン" AS i_バージョン,
1668
- i."作成日時" AS i_作成日時,
1669
- i."更新日時" AS i_更新日時,
1670
- id."ID" AS id_id,
1671
- id."請求ID" AS id_請求ID,
1672
- id."行番号" AS id_行番号,
1673
- id."売上ID" AS id_売上ID,
1674
- id."売上番号" AS id_売上番号,
1675
- id."売上日" AS id_売上日,
1676
- id."売上金額" AS id_売上金額,
1677
- id."消費税額" AS id_消費税額,
1678
- id."合計金額" AS id_合計金額
1679
- FROM "請求データ" i
1680
- LEFT JOIN "請求明細" id
1681
- ON i."ID" = id."請求ID"
1682
- WHERE i."請求番号" = #{invoiceNumber}
1683
- ORDER BY id."行番号"
1684
- </select>
1685
-
1686
- </mapper>
1687
- ```
1688
-
1689
- </details>
1690
-
1691
- ### リレーション設定のポイント
1692
-
1693
- | 設定項目 | 説明 |
1694
- |---------|------|
1695
- | `<collection>` | 1:N 関連のマッピング |
1696
- | `<id>` | 主キーの識別(MyBatis が重複排除に使用) |
1697
- | `resultMap` | ネストした ResultMap の参照 |
1698
- | エイリアス(AS) | カラム名の重複を避けるためのプレフィックス |
1699
- | `ORDER BY` | コレクションの順序を保証 |
1700
-
1701
- ### 楽観ロック(Optimistic Locking)の実装
1702
-
1703
- 複数ユーザーが同時に請求データや入金データを編集する場合、データの整合性を保つために楽観ロックを実装します。
1704
-
1705
- ```plantuml
1706
- @startuml
1707
-
1708
- title 楽観ロックの動作シーケンス
1709
-
1710
- actor "ユーザーA" as userA
1711
- actor "ユーザーB" as userB
1712
- database "請求テーブル" as db
1713
-
1714
- userA -> db : SELECT(version=1)
1715
- userB -> db : SELECT(version=1)
1716
- userA -> userA : 画面で編集
1717
- userB -> userB : 画面で編集
1718
- userA -> db : UPDATE WHERE version=1\n→ version=2 に更新
1719
- db -> userA : 更新成功(1行)
1720
- userB -> db : UPDATE WHERE version=1
1721
- db -> userB : 更新失敗(0行)\n楽観ロック例外
1722
-
1723
- note right of userB
1724
- 再読み込みを促す
1725
- エラーメッセージ表示
1726
- end note
1727
-
1728
- @enduml
1729
- ```
1730
-
1731
- ### バージョンカラムによる同時更新制御
1732
-
1733
- <details>
1734
- <summary>楽観ロック対応の MyBatis Mapper</summary>
1735
-
1736
- ```xml
1737
- <!-- 楽観ロック対応の更新(バージョンチェック付き) -->
1738
- <update id="updateWithOptimisticLock"
1739
- parameterType="com.example.sms.domain.model.invoice.Invoice">
1740
- UPDATE "請求データ"
1741
- SET
1742
- "請求日" = #{invoiceDate},
1743
- "請求先コード" = #{billingCode},
1744
- "顧客コード" = #{customerCode},
1745
- "締日" = #{closingDate},
1746
- "請求区分" = #{invoiceType,
1747
- typeHandler=com.example.sms.infrastructure.persistence.typehandler.InvoiceTypeTypeHandler}::請求区分,
1748
- "前回請求残高" = #{previousBalance},
1749
- "入金額" = #{receiptAmount},
1750
- "繰越残高" = #{carriedBalance},
1751
- "今回売上額" = #{currentSalesAmount},
1752
- "今回消費税額" = #{currentTaxAmount},
1753
- "今回請求額" = #{currentInvoiceAmount},
1754
- "請求残高" = #{invoiceBalance},
1755
- "回収予定日" = #{dueDate},
1756
- "ステータス" = #{status,
1757
- typeHandler=com.example.sms.infrastructure.persistence.typehandler.InvoiceStatusTypeHandler}::請求ステータス,
1758
- "更新日時" = CURRENT_TIMESTAMP,
1759
- "バージョン" = "バージョン" + 1
1760
- WHERE "ID" = #{id}
1761
- AND "バージョン" = #{version}
1762
- </update>
1763
-
1764
- <!-- 楽観ロック対応の削除 -->
1765
- <delete id="deleteWithOptimisticLock">
1766
- DELETE FROM "請求データ"
1767
- WHERE "ID" = #{id}
1768
- AND "バージョン" = #{version}
1769
- </delete>
1770
- ```
1771
-
1772
- </details>
1773
-
1774
- <details>
1775
- <summary>楽観ロック例外クラス</summary>
1776
-
1777
- ```java
1778
- // src/main/java/com/example/sms/domain/exception/OptimisticLockException.java
1779
- package com.example.sms.domain.exception;
1780
-
1781
- public class OptimisticLockException extends RuntimeException {
1782
-
1783
- public OptimisticLockException(String entityName, Integer id) {
1784
- super(String.format(
1785
- "%s(ID: %d)は他のユーザーによって更新または削除されました。" +
1786
- "画面を再読み込みしてください。",
1787
- entityName, id));
1788
- }
1789
-
1790
- public OptimisticLockException(
1791
- String entityName, Integer id,
1792
- Integer expectedVersion, Integer actualVersion) {
1793
- super(String.format(
1794
- "%s(ID: %d)は他のユーザーによって更新されました。" +
1795
- "(期待バージョン: %d, 実際のバージョン: %d)" +
1796
- "画面を再読み込みしてください。",
1797
- entityName, id, expectedVersion, actualVersion));
1798
- }
1799
- }
1800
- ```
1801
-
1802
- </details>
1803
-
1804
- <details>
1805
- <summary>Repository 実装:楽観ロック対応</summary>
1806
-
1807
- ```java
1808
- // src/main/java/com/example/sms/infrastructure/persistence/repository/InvoiceRepositoryImpl.java
1809
- package com.example.sms.infrastructure.persistence.repository;
1810
-
1811
- import com.example.sms.application.port.out.InvoiceRepository;
1812
- import com.example.sms.domain.exception.OptimisticLockException;
1813
- import com.example.sms.domain.model.invoice.*;
1814
- import com.example.sms.infrastructure.persistence.mapper.InvoiceMapper;
1815
- import lombok.RequiredArgsConstructor;
1816
- import org.springframework.stereotype.Repository;
1817
- import org.springframework.transaction.annotation.Transactional;
1818
-
1819
- @Repository
1820
- @RequiredArgsConstructor
1821
- public class InvoiceRepositoryImpl implements InvoiceRepository {
1822
-
1823
- private final InvoiceMapper invoiceMapper;
1824
-
1825
- /**
1826
- * 楽観ロック対応の更新
1827
- * @throws OptimisticLockException 他のユーザーによって更新された場合
1828
- */
1829
- @Override
1830
- @Transactional
1831
- public void update(Invoice invoice) {
1832
- int updatedCount = invoiceMapper.updateWithOptimisticLock(invoice);
1833
-
1834
- if (updatedCount == 0) {
1835
- Integer currentVersion = invoiceMapper.findVersionById(invoice.getId());
1836
-
1837
- if (currentVersion == null) {
1838
- throw new OptimisticLockException("請求", invoice.getId());
1839
- } else {
1840
- throw new OptimisticLockException(
1841
- "請求",
1842
- invoice.getId(),
1843
- invoice.getVersion(),
1844
- currentVersion
1845
- );
1846
- }
1847
- }
1848
- }
1849
-
1850
- /**
1851
- * 楽観ロック対応の削除
1852
- * @throws OptimisticLockException 他のユーザーによって更新された場合
1853
- */
1854
- @Override
1855
- @Transactional
1856
- public void delete(Integer id, Integer version) {
1857
- int deletedCount = invoiceMapper.deleteWithOptimisticLock(id, version);
1858
-
1859
- if (deletedCount == 0) {
1860
- throw new OptimisticLockException("請求", id);
1861
- }
1862
- }
1863
- }
1864
- ```
1865
-
1866
- </details>
1867
-
1868
- ### 楽観ロックのテスト
1869
-
1870
- <details>
1871
- <summary>楽観ロックのテストコード</summary>
1872
-
1873
- ```java
1874
- @Nested
1875
- @DisplayName("楽観ロックの更新テスト")
1876
- class OptimisticLockUpdateTest {
1877
-
1878
- @Test
1879
- @DisplayName("正しいバージョンで更新できる")
1880
- void shouldUpdateWithCorrectVersion() {
1881
- // Given: 請求を登録
1882
- var invoice = createTestInvoice("INV-0001");
1883
- invoiceRepository.save(invoice);
1884
-
1885
- // When: バージョン1で更新
1886
- var saved = invoiceRepository.findByInvoiceNumber("INV-0001").orElseThrow();
1887
- assertThat(saved.getVersion()).isEqualTo(1);
1888
- saved.setInvoiceBalance(new BigDecimal("200000"));
1889
- invoiceRepository.update(saved);
1890
-
1891
- // Then: バージョンが2に増加
1892
- var updated = invoiceRepository.findByInvoiceNumber("INV-0001").orElseThrow();
1893
- assertThat(updated.getVersion()).isEqualTo(2);
1894
- }
1895
-
1896
- @Test
1897
- @DisplayName("古いバージョンで更新すると楽観ロック例外がスローされる")
1898
- void shouldThrowOptimisticLockExceptionWithOldVersion() {
1899
- // Given: 請求を登録して、別のセッションで更新
1900
- var invoice = createTestInvoice("INV-0002");
1901
- invoiceRepository.save(invoice);
1902
-
1903
- var sessionA = invoiceRepository.findByInvoiceNumber("INV-0002").orElseThrow();
1904
- var sessionB = invoiceRepository.findByInvoiceNumber("INV-0002").orElseThrow();
1905
-
1906
- // セッションAで更新
1907
- sessionA.setInvoiceBalance(new BigDecimal("300000"));
1908
- invoiceRepository.update(sessionA);
1909
-
1910
- // When/Then: セッションBで更新すると例外
1911
- sessionB.setInvoiceBalance(new BigDecimal("400000"));
1912
- assertThatThrownBy(() -> invoiceRepository.update(sessionB))
1913
- .isInstanceOf(OptimisticLockException.class)
1914
- .hasMessageContaining("他のユーザーによって更新されました");
1915
- }
1916
- }
1917
- ```
1918
-
1919
- </details>
1920
-
1921
- ### 楽観ロックのベストプラクティス
1922
-
1923
- | ポイント | 説明 |
1924
- |---------|------|
1925
- | **バージョンカラム** | INTEGER 型で十分(オーバーフローは現実的に発生しない) |
1926
- | **WHERE 条件** | 必ず `AND "バージョン" = #{version}` を含める |
1927
- | **インクリメント** | `"バージョン" = "バージョン" + 1` でアトミックに更新 |
1928
- | **例外処理** | 更新件数が0の場合は楽観ロック例外をスロー |
1929
- | **エラーメッセージ** | ユーザーにわかりやすいメッセージで再読み込みを促す |
1930
-
1931
- ---
1932
-
1933
- ## 第7章のまとめ
1934
-
1935
- 本章では、債権管理の核心部分である請求・入金・売掛金管理について学びました。
1936
-
1937
- ### 学んだこと
1938
-
1939
- 1. **請求パターンの理解**
1940
- - 都度請求と締め請求の違い
1941
- - 締処理による売上の集約
1942
-
1943
- 2. **請求データの構造**
1944
- - 前回繰越・今回売上・請求残高の計算
1945
- - 回収予定日の設定
1946
-
1947
- 3. **入金消込の実装**
1948
- - 1:1、1:N、N:1 の消込パターン
1949
- - 自動消込機能
1950
-
1951
- 4. **債権残高管理**
1952
- - 月次売掛金残高の更新
1953
- - 滞留債権の管理
1954
-
1955
- 5. **リレーションと楽観ロック**
1956
- - N+1 問題の回避
1957
- - 同時更新の競合制御
1958
-
1959
- ### 債権管理の ER 図(全体像)
1960
-
1961
- ```plantuml
1962
- @startuml
1963
-
1964
- title 債権管理のER図
1965
-
1966
- entity 売上データ {
1967
- ID <<PK>>
1968
- --
1969
- 売上番号
1970
- 売上日
1971
- 顧客コード
1972
- 売上金額
1973
- 消費税額
1974
- ステータス
1975
- }
1976
-
1977
- entity 請求データ {
1978
- ID <<PK>>
1979
- --
1980
- 請求番号
1981
- 請求日
1982
- 顧客コード
1983
- 締日
1984
- 請求区分
1985
- 前回請求残高
1986
- 入金額
1987
- 繰越残高
1988
- 今回売上額
1989
- 今回消費税額
1990
- 今回請求額
1991
- 請求残高
1992
- 回収予定日
1993
- ステータス
1994
- バージョン
1995
- }
1996
-
1997
- entity 請求明細 {
1998
- ID <<PK>>
1999
- --
2000
- 請求ID <<FK>>
2001
- 売上ID <<FK>>
2002
- 売上日
2003
- 売上番号
2004
- 売上金額
2005
- 消費税額
2006
- }
2007
-
2008
- entity 入金データ {
2009
- ID <<PK>>
2010
- --
2011
- 入金番号
2012
- 入金日
2013
- 顧客コード
2014
- 入金方法
2015
- 入金金額
2016
- 消込済金額
2017
- 未消込金額
2018
- 手数料
2019
- ステータス
2020
- バージョン
2021
- }
2022
-
2023
- entity 入金消込明細 {
2024
- ID <<PK>>
2025
- --
2026
- 入金ID <<FK>>
2027
- 請求ID <<FK>>
2028
- 消込日
2029
- 消込金額
2030
- バージョン
2031
- }
2032
-
2033
- entity 前受金データ {
2034
- ID <<PK>>
2035
- --
2036
- 前受金番号
2037
- 発生日
2038
- 顧客コード
2039
- 入金ID <<FK>>
2040
- 前受金額
2041
- 使用済金額
2042
- 残高
2043
- }
2044
-
2045
- entity 売掛金残高 {
2046
- ID <<PK>>
2047
- --
2048
- 顧客コード
2049
- 基準日
2050
- 前月残高
2051
- 当月売上
2052
- 当月入金
2053
- 当月残高
2054
- }
2055
-
2056
- entity 顧客マスタ {
2057
- 顧客コード <<PK>>
2058
- --
2059
- 顧客名
2060
- 締日
2061
- 回収サイト
2062
- }
2063
-
2064
- 売上データ ||--o{ 請求明細
2065
- 請求データ ||--o{ 請求明細
2066
- 請求データ ||--o{ 入金消込明細
2067
- 入金データ ||--o{ 入金消込明細
2068
- 入金データ ||--o| 前受金データ
2069
- 顧客マスタ ||--o{ 請求データ
2070
- 顧客マスタ ||--o{ 入金データ
2071
- 顧客マスタ ||--o{ 売掛金残高
2072
-
2073
- @enduml
2074
- ```
2075
-
2076
- ### 次章の予告
2077
-
2078
- 第8章では、調達管理の設計に進みます。発注・入荷・仕入の一連の購買業務プロセスをデータベース設計と TDD で実装していきます。
2079
-
2080
- ---
2081
-
2082
- [← 第6章:受注・出荷・売上の設計](./chapter06.md) | [第8章:調達管理の設計 →](./chapter08.md)
1
+ # 第7章:債権管理の設計
2
+
3
+ 販売管理システムにおいて、売上が発生した後の請求・入金管理は経営の根幹を支える重要な業務です。本章では、請求業務と回収業務のデータベース設計と実装を行います。
4
+
5
+ ## 債権管理の全体像
6
+
7
+ 債権管理は「売上 → 請求 → 入金 → 消込」という一連のフローで構成されます。
8
+
9
+ ```plantuml
10
+ @startuml
11
+
12
+ title 債権管理フロー
13
+
14
+ |営業部門|
15
+ start
16
+ :売上計上;
17
+ note right
18
+ 第6章で実装した
19
+ 売上データ
20
+ end note
21
+
22
+ |経理部門|
23
+ :請求データ作成;
24
+ note right
25
+ 都度請求 or 締め請求
26
+ 回収予定日の設定
27
+ end note
28
+
29
+ :請求書発行;
30
+
31
+ |財務部門|
32
+ :入金確認;
33
+ note right
34
+ 銀行振込
35
+ 現金
36
+ 手形
37
+ end note
38
+
39
+ :入金消込;
40
+ note right
41
+ 請求と入金を
42
+ 突合・消込
43
+ end note
44
+
45
+ if (全額入金?) then (yes)
46
+ :請求完了;
47
+ else (no)
48
+ :残高管理;
49
+ note right
50
+ 売掛金残高を
51
+ 更新
52
+ end note
53
+ endif
54
+
55
+ :月次債権レポート;
56
+
57
+ stop
58
+
59
+ @enduml
60
+ ```
61
+
62
+ ### 債権管理で扱うデータ
63
+
64
+ | データ | 説明 |
65
+ |-------|------|
66
+ | **請求データ** | 顧客への請求情報(請求金額、請求日、回収予定日) |
67
+ | **請求明細** | 請求に含まれる売上データの明細 |
68
+ | **入金データ** | 顧客からの入金情報(入金金額、入金方法) |
69
+ | **入金消込明細** | 入金と請求の突合情報 |
70
+ | **売掛金残高** | 顧客別の債権残高 |
71
+
72
+ ---
73
+
74
+ ## 7.1 請求業務の DB 設計
75
+
76
+ ### 請求業務フローの理解
77
+
78
+ 請求業務には「都度請求」と「締め請求」の2つのパターンがあります。
79
+
80
+ ```plantuml
81
+ @startuml
82
+
83
+ title 請求パターンの比較
84
+
85
+ rectangle "都度請求" #CCFFCC {
86
+ card "売上発生" as s1
87
+ card "即時請求" as i1
88
+ s1 --> i1
89
+ }
90
+
91
+ rectangle "締め請求(月次)" #CCCCFF {
92
+ card "売上1" as s2a
93
+ card "売上2" as s2b
94
+ card "売上3" as s2c
95
+ card "締処理" as c
96
+ card "月次請求" as i2
97
+ s2a --> c
98
+ s2b --> c
99
+ s2c --> c
100
+ c --> i2
101
+ }
102
+
103
+ note bottom of "都度請求"
104
+ 取引の都度、請求書を発行
105
+ - 単発取引
106
+ - 現金取引
107
+ end note
108
+
109
+ note bottom of "締め請求(月次)"
110
+ 月末に一括請求
111
+ - 継続取引
112
+ - 掛取引
113
+ end note
114
+
115
+ @enduml
116
+ ```
117
+
118
+ | 請求パターン | 特徴 | 用途 |
119
+ |------------|------|------|
120
+ | **都度請求** | 売上発生時に即座に請求 | 単発取引、現金取引 |
121
+ | **締め請求** | 月末など締日にまとめて請求 | 継続取引、掛売り |
122
+
123
+ ### 締処理の概念
124
+
125
+ 締め請求では、顧客ごとに設定された締日に基づいて売上を集約します。
126
+
127
+ ```plantuml
128
+ @startuml
129
+
130
+ title 締処理のタイムライン
131
+
132
+ rectangle "1月" #FFFFCC {
133
+ card "1/5 売上 10,000円" as s1
134
+ card "1/15 売上 25,000円" as s2
135
+ card "1/25 売上 15,000円" as s3
136
+ card "1/31 締処理" as c1
137
+ }
138
+
139
+ rectangle "2月" #CCFFFF {
140
+ card "2/10 売上 20,000円" as s4
141
+ card "2/20 売上 30,000円" as s5
142
+ card "2/28 締処理" as c2
143
+ }
144
+
145
+ s1 --> c1
146
+ s2 --> c1
147
+ s3 --> c1
148
+ s4 --> c2
149
+ s5 --> c2
150
+
151
+ note right of c1
152
+ 1月請求
153
+ 売上合計: 50,000円
154
+ 消費税: 5,000円
155
+ 請求額: 55,000円
156
+ end note
157
+
158
+ note right of c2
159
+ 2月請求
160
+ 繰越残高: 55,000円
161
+ 今月売上: 50,000円
162
+ 消費税: 5,000円
163
+ 請求額: 110,000円
164
+ end note
165
+
166
+ @enduml
167
+ ```
168
+
169
+ ### 請求データ・請求明細の構造
170
+
171
+ #### 請求データの ER 図
172
+
173
+ ```plantuml
174
+ @startuml
175
+
176
+ title 請求関連テーブル
177
+
178
+ entity 売上データ {
179
+ ID <<PK>>
180
+ --
181
+ 売上番号 <<UK>>
182
+ 売上日
183
+ 顧客コード <<FK>>
184
+ 売上金額
185
+ 消費税額
186
+ 売上合計
187
+ ステータス
188
+ }
189
+
190
+ entity 請求データ {
191
+ ID <<PK>>
192
+ --
193
+ 請求番号 <<UK>>
194
+ 請求日
195
+ 請求先コード <<FK>>
196
+ 顧客コード <<FK>>
197
+ 締日
198
+ 請求区分
199
+ 前回請求残高
200
+ 入金額
201
+ 繰越残高
202
+ 今回売上額
203
+ 今回消費税額
204
+ 今回請求額
205
+ 請求残高
206
+ 回収予定日
207
+ ステータス
208
+ バージョン
209
+ 作成日時
210
+ 更新日時
211
+ }
212
+
213
+ entity 請求明細 {
214
+ ID <<PK>>
215
+ --
216
+ 請求ID <<FK>>
217
+ 行番号
218
+ 売上ID <<FK>>
219
+ 売上番号
220
+ 売上日
221
+ 売上金額
222
+ 消費税額
223
+ 合計金額
224
+ }
225
+
226
+ entity 請求締履歴 {
227
+ ID <<PK>>
228
+ --
229
+ 顧客コード <<FK>>
230
+ 締年月
231
+ 締日
232
+ 売上件数
233
+ 売上合計
234
+ 消費税合計
235
+ 請求ID <<FK>>
236
+ 処理日時
237
+ }
238
+
239
+ 売上データ ||--o{ 請求明細
240
+ 請求データ ||--o{ 請求明細
241
+ 請求データ ||--o| 請求締履歴
242
+
243
+ @enduml
244
+ ```
245
+
246
+ ### 請求ステータスの定義
247
+
248
+ ```plantuml
249
+ @startuml
250
+
251
+ title 請求ステータス遷移図
252
+
253
+ [*] --> 未発行
254
+
255
+ 未発行 --> 発行済 : 請求書発行
256
+
257
+ 発行済 --> 一部入金 : 一部入金
258
+ 発行済 --> 入金済 : 全額入金
259
+
260
+ 一部入金 --> 入金済 : 残額入金
261
+
262
+ 入金済 --> [*]
263
+
264
+ 発行済 --> 回収遅延 : 回収予定日超過
265
+ 一部入金 --> 回収遅延 : 回収予定日超過
266
+
267
+ @enduml
268
+ ```
269
+
270
+ | ステータス | 説明 |
271
+ |-----------|------|
272
+ | **未発行** | 請求データ作成済み、請求書未発行 |
273
+ | **発行済** | 請求書を発行した状態 |
274
+ | **一部入金** | 一部入金があり、残高がある状態 |
275
+ | **入金済** | 全額入金が完了した状態 |
276
+ | **回収遅延** | 回収予定日を過ぎても未回収の状態 |
277
+
278
+ ### マイグレーション:請求関連テーブルの作成
279
+
280
+ <details>
281
+ <summary>V011__create_invoice_tables.sql</summary>
282
+
283
+ ```sql
284
+ -- src/main/resources/db/migration/V011__create_invoice_tables.sql
285
+
286
+ -- 請求ステータス
287
+ CREATE TYPE 請求ステータス AS ENUM ('未発行', '発行済', '一部入金', '入金済', '回収遅延');
288
+
289
+ -- 請求区分は V001 で既に定義済み('都度', '締め')
290
+
291
+ -- 請求データ(ヘッダ)
292
+ CREATE TABLE "請求データ" (
293
+ "ID" SERIAL PRIMARY KEY,
294
+ "請求番号" VARCHAR(20) UNIQUE NOT NULL,
295
+ "請求日" DATE NOT NULL,
296
+ "請求先コード" VARCHAR(20) NOT NULL,
297
+ "顧客コード" VARCHAR(20) NOT NULL,
298
+ "顧客枝番" VARCHAR(10) DEFAULT '00',
299
+ "締日" DATE,
300
+ "請求区分" 請求区分 NOT NULL,
301
+ "前回請求残高" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
302
+ "入金額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
303
+ "繰越残高" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
304
+ "今回売上額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
305
+ "今回消費税額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
306
+ "今回請求額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
307
+ "請求残高" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
308
+ "回収予定日" DATE,
309
+ "ステータス" 請求ステータス DEFAULT '未発行' NOT NULL,
310
+ "備考" TEXT,
311
+ "バージョン" INTEGER DEFAULT 1 NOT NULL,
312
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
313
+ "作成者" VARCHAR(50),
314
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
315
+ "更新者" VARCHAR(50),
316
+ CONSTRAINT "fk_請求データ_顧客"
317
+ FOREIGN KEY ("顧客コード", "顧客枝番") REFERENCES "顧客マスタ"("顧客コード", "顧客枝番")
318
+ );
319
+
320
+ -- 請求明細
321
+ CREATE TABLE "請求明細" (
322
+ "ID" SERIAL PRIMARY KEY,
323
+ "請求ID" INTEGER NOT NULL,
324
+ "行番号" INTEGER NOT NULL,
325
+ "売上ID" INTEGER,
326
+ "売上番号" VARCHAR(20),
327
+ "売上日" DATE,
328
+ "売上金額" DECIMAL(15, 2) NOT NULL,
329
+ "消費税額" DECIMAL(15, 2) NOT NULL,
330
+ "合計金額" DECIMAL(15, 2) NOT NULL,
331
+ CONSTRAINT "fk_請求明細_請求"
332
+ FOREIGN KEY ("請求ID") REFERENCES "請求データ"("ID") ON DELETE CASCADE,
333
+ CONSTRAINT "fk_請求明細_売上"
334
+ FOREIGN KEY ("売上ID") REFERENCES "売上データ"("ID"),
335
+ CONSTRAINT "uk_請求明細_請求_行" UNIQUE ("請求ID", "行番号")
336
+ );
337
+
338
+ -- 請求締履歴
339
+ CREATE TABLE "請求締履歴" (
340
+ "ID" SERIAL PRIMARY KEY,
341
+ "顧客コード" VARCHAR(20) NOT NULL,
342
+ "顧客枝番" VARCHAR(10) DEFAULT '00',
343
+ "締年月" VARCHAR(7) NOT NULL,
344
+ "締日" DATE NOT NULL,
345
+ "売上件数" INTEGER NOT NULL,
346
+ "売上合計" DECIMAL(15, 2) NOT NULL,
347
+ "消費税合計" DECIMAL(15, 2) NOT NULL,
348
+ "請求ID" INTEGER,
349
+ "処理日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
350
+ CONSTRAINT "fk_請求締履歴_顧客"
351
+ FOREIGN KEY ("顧客コード", "顧客枝番") REFERENCES "顧客マスタ"("顧客コード", "顧客枝番"),
352
+ CONSTRAINT "fk_請求締履歴_請求"
353
+ FOREIGN KEY ("請求ID") REFERENCES "請求データ"("ID"),
354
+ CONSTRAINT "uk_請求締履歴_顧客_年月" UNIQUE ("顧客コード", "顧客枝番", "締年月")
355
+ );
356
+
357
+ -- 売掛金残高
358
+ CREATE TABLE "売掛金残高" (
359
+ "ID" SERIAL PRIMARY KEY,
360
+ "顧客コード" VARCHAR(20) NOT NULL,
361
+ "顧客枝番" VARCHAR(10) DEFAULT '00',
362
+ "基準日" DATE NOT NULL,
363
+ "前月残高" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
364
+ "当月売上" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
365
+ "当月入金" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
366
+ "当月残高" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
367
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
368
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
369
+ CONSTRAINT "fk_売掛金残高_顧客"
370
+ FOREIGN KEY ("顧客コード", "顧客枝番") REFERENCES "顧客マスタ"("顧客コード", "顧客枝番"),
371
+ CONSTRAINT "uk_売掛金残高_顧客_基準日" UNIQUE ("顧客コード", "顧客枝番", "基準日")
372
+ );
373
+
374
+ -- インデックス
375
+ CREATE INDEX "idx_請求データ_顧客コード" ON "請求データ"("顧客コード");
376
+ CREATE INDEX "idx_請求データ_請求日" ON "請求データ"("請求日");
377
+ CREATE INDEX "idx_請求データ_ステータス" ON "請求データ"("ステータス");
378
+ CREATE INDEX "idx_請求明細_請求ID" ON "請求明細"("請求ID");
379
+ CREATE INDEX "idx_売掛金残高_基準日" ON "売掛金残高"("基準日");
380
+
381
+ -- テーブルコメント
382
+ COMMENT ON TABLE "請求データ" IS '請求ヘッダ情報を管理するテーブル';
383
+ COMMENT ON TABLE "請求明細" IS '請求に含まれる売上明細を管理するテーブル';
384
+ COMMENT ON TABLE "請求締履歴" IS '月次締処理の履歴を管理するテーブル';
385
+ COMMENT ON TABLE "売掛金残高" IS '顧客別月次売掛金残高を管理するテーブル';
386
+
387
+ -- カラムコメント
388
+ COMMENT ON COLUMN "請求データ"."バージョン" IS '楽観ロック用バージョン番号';
389
+ ```
390
+
391
+ </details>
392
+
393
+ ### 請求エンティティの実装
394
+
395
+ <details>
396
+ <summary>請求ステータス ENUM</summary>
397
+
398
+ ```java
399
+ // src/main/java/com/example/sms/domain/model/invoice/InvoiceStatus.java
400
+ package com.example.sms.domain.model.invoice;
401
+
402
+ import lombok.Getter;
403
+ import lombok.RequiredArgsConstructor;
404
+
405
+ /**
406
+ * 請求ステータス.
407
+ */
408
+ @Getter
409
+ @RequiredArgsConstructor
410
+ public enum InvoiceStatus {
411
+ DRAFT("未発行"),
412
+ ISSUED("発行済"),
413
+ PARTIALLY_PAID("一部入金"),
414
+ PAID("入金済"),
415
+ OVERDUE("回収遅延");
416
+
417
+ private final String displayName;
418
+
419
+ /**
420
+ * 表示名から請求ステータスを取得する.
421
+ *
422
+ * @param displayName 表示名
423
+ * @return 請求ステータス
424
+ */
425
+ public static InvoiceStatus fromDisplayName(String displayName) {
426
+ for (InvoiceStatus status : values()) {
427
+ if (status.displayName.equals(displayName)) {
428
+ return status;
429
+ }
430
+ }
431
+ throw new IllegalArgumentException("不正な請求ステータス: " + displayName);
432
+ }
433
+ }
434
+ ```
435
+
436
+ </details>
437
+
438
+ <details>
439
+ <summary>請求区分 ENUM</summary>
440
+
441
+ ```java
442
+ // src/main/java/com/example/sms/domain/model/invoice/InvoiceType.java
443
+ package com.example.sms.domain.model.invoice;
444
+
445
+ import lombok.Getter;
446
+ import lombok.RequiredArgsConstructor;
447
+
448
+ /**
449
+ * 請求区分.
450
+ */
451
+ @Getter
452
+ @RequiredArgsConstructor
453
+ public enum InvoiceType {
454
+ IMMEDIATE("都度"),
455
+ CLOSING("締め");
456
+
457
+ private final String displayName;
458
+
459
+ /**
460
+ * 表示名から請求区分を取得する.
461
+ *
462
+ * @param displayName 表示名
463
+ * @return 請求区分
464
+ */
465
+ public static InvoiceType fromDisplayName(String displayName) {
466
+ for (InvoiceType type : values()) {
467
+ if (type.displayName.equals(displayName)) {
468
+ return type;
469
+ }
470
+ }
471
+ throw new IllegalArgumentException("不正な請求区分: " + displayName);
472
+ }
473
+ }
474
+ ```
475
+
476
+ </details>
477
+
478
+ <details>
479
+ <summary>請求エンティティ</summary>
480
+
481
+ ```java
482
+ // src/main/java/com/example/sms/domain/model/invoice/Invoice.java
483
+ package com.example.sms.domain.model.invoice;
484
+
485
+ import lombok.AllArgsConstructor;
486
+ import lombok.Builder;
487
+ import lombok.Data;
488
+ import lombok.NoArgsConstructor;
489
+
490
+ import java.math.BigDecimal;
491
+ import java.time.LocalDate;
492
+ import java.time.LocalDateTime;
493
+ import java.util.ArrayList;
494
+ import java.util.List;
495
+
496
+ /**
497
+ * 請求エンティティ.
498
+ */
499
+ @Data
500
+ @Builder
501
+ @NoArgsConstructor
502
+ @AllArgsConstructor
503
+ @SuppressWarnings("PMD.RedundantFieldInitializer")
504
+ public class Invoice {
505
+ private Integer id;
506
+ private String invoiceNumber;
507
+ private LocalDate invoiceDate;
508
+ private String billingCode;
509
+ private String customerCode;
510
+ private String customerBranchNumber;
511
+ private LocalDate closingDate;
512
+ @Builder.Default
513
+ private InvoiceType invoiceType = InvoiceType.CLOSING;
514
+ @Builder.Default
515
+ private BigDecimal previousBalance = BigDecimal.ZERO;
516
+ @Builder.Default
517
+ private BigDecimal receiptAmount = BigDecimal.ZERO;
518
+ @Builder.Default
519
+ private BigDecimal carriedBalance = BigDecimal.ZERO;
520
+ @Builder.Default
521
+ private BigDecimal currentSalesAmount = BigDecimal.ZERO;
522
+ @Builder.Default
523
+ private BigDecimal currentTaxAmount = BigDecimal.ZERO;
524
+ @Builder.Default
525
+ private BigDecimal currentInvoiceAmount = BigDecimal.ZERO;
526
+ @Builder.Default
527
+ private BigDecimal invoiceBalance = BigDecimal.ZERO;
528
+ private LocalDate dueDate;
529
+ @Builder.Default
530
+ private InvoiceStatus status = InvoiceStatus.DRAFT;
531
+ private String remarks;
532
+ private LocalDateTime createdAt;
533
+ private String createdBy;
534
+ private LocalDateTime updatedAt;
535
+ private String updatedBy;
536
+
537
+ /** 楽観ロック用バージョン. */
538
+ @Builder.Default
539
+ private Integer version = 1;
540
+
541
+ @Builder.Default
542
+ private List<InvoiceDetail> details = new ArrayList<>();
543
+
544
+ /**
545
+ * 請求残高を計算する.
546
+ *
547
+ * @return 請求残高
548
+ */
549
+ public BigDecimal calculateInvoiceBalance() {
550
+ return carriedBalance.add(currentInvoiceAmount).subtract(receiptAmount);
551
+ }
552
+
553
+ /**
554
+ * 繰越残高を計算する.
555
+ *
556
+ * @return 繰越残高
557
+ */
558
+ public BigDecimal calculateCarriedBalance() {
559
+ return previousBalance.subtract(receiptAmount);
560
+ }
561
+ }
562
+ ```
563
+
564
+ </details>
565
+
566
+ <details>
567
+ <summary>請求明細エンティティ</summary>
568
+
569
+ ```java
570
+ // src/main/java/com/example/sms/domain/model/invoice/InvoiceDetail.java
571
+ package com.example.sms.domain.model.invoice;
572
+
573
+ import lombok.AllArgsConstructor;
574
+ import lombok.Builder;
575
+ import lombok.Data;
576
+ import lombok.NoArgsConstructor;
577
+
578
+ import java.math.BigDecimal;
579
+ import java.time.LocalDate;
580
+
581
+ /**
582
+ * 請求明細エンティティ.
583
+ */
584
+ @Data
585
+ @Builder
586
+ @NoArgsConstructor
587
+ @AllArgsConstructor
588
+ public class InvoiceDetail {
589
+ private Integer id;
590
+ private Integer invoiceId;
591
+ private Integer lineNumber;
592
+ private Integer salesId;
593
+ private String salesNumber;
594
+ private LocalDate salesDate;
595
+ private BigDecimal salesAmount;
596
+ private BigDecimal taxAmount;
597
+ private BigDecimal totalAmount;
598
+ }
599
+ ```
600
+
601
+ </details>
602
+
603
+ ### 締処理サービスの実装
604
+
605
+ <details>
606
+ <summary>締処理サービス</summary>
607
+
608
+ ```java
609
+ // src/main/java/com/example/sms/application/service/ClosingService.java
610
+ package com.example.sms.application.service;
611
+
612
+ import com.example.sms.application.port.out.*;
613
+ import com.example.sms.domain.model.invoice.*;
614
+ import com.example.sms.domain.model.sales.Sales;
615
+ import lombok.RequiredArgsConstructor;
616
+ import org.springframework.stereotype.Service;
617
+ import org.springframework.transaction.annotation.Transactional;
618
+
619
+ import java.math.BigDecimal;
620
+ import java.time.LocalDate;
621
+ import java.time.YearMonth;
622
+ import java.util.List;
623
+
624
+ @Service
625
+ @RequiredArgsConstructor
626
+ public class ClosingService {
627
+
628
+ private final InvoiceRepository invoiceRepository;
629
+ private final SalesRepository salesRepository;
630
+ private final CustomerRepository customerRepository;
631
+ private final ClosingHistoryRepository closingHistoryRepository;
632
+
633
+ /**
634
+ * 月次締処理を実行する
635
+ */
636
+ @Transactional
637
+ public Invoice executeMonthlyClosing(String customerCode, YearMonth yearMonth) {
638
+ LocalDate closingDate = yearMonth.atEndOfMonth();
639
+
640
+ // 既に締処理済みかチェック
641
+ if (closingHistoryRepository.existsByCustomerAndYearMonth(customerCode, yearMonth)) {
642
+ throw new IllegalStateException("既に締処理が実行されています: " + yearMonth);
643
+ }
644
+
645
+ // 対象期間の売上を取得
646
+ LocalDate fromDate = yearMonth.atDay(1);
647
+ LocalDate toDate = yearMonth.atEndOfMonth();
648
+ List<Sales> salesList = salesRepository.findByCustomerCodeAndDateRange(
649
+ customerCode, fromDate, toDate);
650
+
651
+ if (salesList.isEmpty()) {
652
+ throw new IllegalStateException("対象期間の売上がありません");
653
+ }
654
+
655
+ // 売上合計・消費税を集計
656
+ BigDecimal totalSales = salesList.stream()
657
+ .map(Sales::getSalesAmount)
658
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
659
+ BigDecimal totalTax = salesList.stream()
660
+ .map(Sales::getTaxAmount)
661
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
662
+
663
+ // 前回請求残高を取得
664
+ BigDecimal previousBalance = invoiceRepository
665
+ .findLatestByCustomerCode(customerCode)
666
+ .map(Invoice::getInvoiceBalance)
667
+ .orElse(BigDecimal.ZERO);
668
+
669
+ // 顧客の回収サイトから回収予定日を計算
670
+ var customer = customerRepository.findByCustomerCode(customerCode)
671
+ .orElseThrow(() -> new IllegalArgumentException("顧客が見つかりません"));
672
+ LocalDate dueDate = calculateDueDate(closingDate, customer.getCollectionSite());
673
+
674
+ // 請求データを作成
675
+ var invoice = Invoice.builder()
676
+ .invoiceNumber(generateInvoiceNumber())
677
+ .invoiceDate(closingDate)
678
+ .billingCode(customerCode)
679
+ .customerCode(customerCode)
680
+ .closingDate(closingDate)
681
+ .invoiceType(InvoiceType.CLOSING)
682
+ .previousBalance(previousBalance)
683
+ .receiptAmount(BigDecimal.ZERO)
684
+ .carriedBalance(previousBalance)
685
+ .currentSalesAmount(totalSales)
686
+ .currentTaxAmount(totalTax)
687
+ .currentInvoiceAmount(totalSales.add(totalTax))
688
+ .invoiceBalance(previousBalance.add(totalSales).add(totalTax))
689
+ .dueDate(dueDate)
690
+ .status(InvoiceStatus.DRAFT)
691
+ .build();
692
+
693
+ invoiceRepository.save(invoice);
694
+
695
+ // 請求明細を作成
696
+ int lineNumber = 1;
697
+ for (Sales sales : salesList) {
698
+ var detail = InvoiceDetail.builder()
699
+ .invoiceId(invoice.getId())
700
+ .lineNumber(lineNumber++)
701
+ .salesId(sales.getId())
702
+ .salesNumber(sales.getSalesNumber())
703
+ .salesDate(sales.getSalesDate())
704
+ .salesAmount(sales.getSalesAmount())
705
+ .taxAmount(sales.getTaxAmount())
706
+ .totalAmount(sales.getSalesTotal())
707
+ .build();
708
+ invoiceRepository.saveDetail(detail);
709
+ }
710
+
711
+ // 締履歴を保存
712
+ var history = ClosingHistory.builder()
713
+ .customerCode(customerCode)
714
+ .closingYearMonth(yearMonth.toString())
715
+ .closingDate(closingDate)
716
+ .salesCount(salesList.size())
717
+ .salesTotal(totalSales)
718
+ .taxTotal(totalTax)
719
+ .invoiceId(invoice.getId())
720
+ .build();
721
+ closingHistoryRepository.save(history);
722
+
723
+ return invoice;
724
+ }
725
+
726
+ private LocalDate calculateDueDate(LocalDate closingDate, Integer collectionSite) {
727
+ if (collectionSite == null) {
728
+ collectionSite = 30; // デフォルト30日
729
+ }
730
+ return closingDate.plusDays(collectionSite);
731
+ }
732
+
733
+ private String generateInvoiceNumber() {
734
+ return String.format("INV-%d-%04d",
735
+ LocalDate.now().getYear(), System.currentTimeMillis() % 10000);
736
+ }
737
+ }
738
+ ```
739
+
740
+ </details>
741
+
742
+ ---
743
+
744
+ ## 7.2 回収業務の DB 設計
745
+
746
+ ### 入金業務フローの理解
747
+
748
+ 入金業務は、顧客からの入金を記録し、請求と突合(消込)する業務です。
749
+
750
+ ```plantuml
751
+ @startuml
752
+
753
+ title 入金業務フロー
754
+
755
+ |財務部門|
756
+ start
757
+ :銀行明細確認;
758
+ note right
759
+ 毎日の入金情報を
760
+ 銀行から取得
761
+ end note
762
+
763
+ :入金データ登録;
764
+ note right
765
+ 入金日
766
+ 顧客コード
767
+ 入金金額
768
+ 入金方法
769
+ end note
770
+
771
+ :入金消込;
772
+
773
+ if (消込方法?) then (自動消込)
774
+ :自動マッチング;
775
+ note right
776
+ 顧客コード + 金額
777
+ で自動消込
778
+ end note
779
+ else (手動消込)
780
+ :請求選択;
781
+ :消込金額入力;
782
+ endif
783
+
784
+ if (過入金?) then (yes)
785
+ :前受金計上;
786
+ else (no)
787
+ endif
788
+
789
+ :消込完了;
790
+
791
+ stop
792
+
793
+ @enduml
794
+ ```
795
+
796
+ ### 入金方法の種類
797
+
798
+ ```plantuml
799
+ @startuml
800
+
801
+ title 入金方法の分類
802
+
803
+ rectangle "入金方法" {
804
+ rectangle "即時入金" #CCFFCC {
805
+ card "現金" as cash
806
+ card "銀行振込" as transfer
807
+ card "クレジットカード" as credit
808
+ }
809
+
810
+ rectangle "期日入金" #FFFFCC {
811
+ card "手形" as bill
812
+ card "電子記録債権" as denshi
813
+ }
814
+ }
815
+
816
+ note bottom of cash
817
+ 店頭での現金受取
818
+ end note
819
+
820
+ note bottom of transfer
821
+ 銀行振込による入金
822
+ 振込手数料の扱いに注意
823
+ end note
824
+
825
+ note bottom of bill
826
+ 約束手形
827
+ 期日に換金
828
+ end note
829
+
830
+ @enduml
831
+ ```
832
+
833
+ | 入金方法 | 特徴 | 手数料 |
834
+ |---------|------|-------|
835
+ | **現金** | 即時入金、店頭取引 | なし |
836
+ | **銀行振込** | 最も一般的、振込手数料が発生 | 振込手数料(顧客負担 or 当社負担) |
837
+ | **クレジットカード** | カード会社経由で入金 | カード手数料 |
838
+ | **手形** | 期日に換金、不渡りリスク | なし |
839
+ | **電子記録債権** | 電子的な債権管理 | 利用料 |
840
+
841
+ ### 入金消込のパターン
842
+
843
+ 入金と請求の消込には、以下のパターンがあります。
844
+
845
+ ```plantuml
846
+ @startuml
847
+
848
+ title 入金消込のパターン
849
+
850
+ rectangle "1:1 消込" #CCFFCC {
851
+ card "入金 100,000円" as r1
852
+ card "請求 100,000円" as i1
853
+ r1 --> i1 : 全額消込
854
+ }
855
+
856
+ rectangle "1:N 消込(過入金)" #FFFFCC {
857
+ card "入金 200,000円" as r2
858
+ card "請求A 80,000円" as i2a
859
+ card "請求B 80,000円" as i2b
860
+ card "前受金 40,000円" as adv
861
+ r2 --> i2a : 80,000
862
+ r2 --> i2b : 80,000
863
+ r2 --> adv : 40,000
864
+ }
865
+
866
+ rectangle "N:1 消込(分割入金)" #FFCCCC {
867
+ card "入金1 50,000円" as r3a
868
+ card "入金2 50,000円" as r3b
869
+ card "請求 100,000円" as i3
870
+ r3a --> i3 : 50,000
871
+ r3b --> i3 : 50,000
872
+ }
873
+
874
+ @enduml
875
+ ```
876
+
877
+ | パターン | 説明 | 処理 |
878
+ |---------|------|------|
879
+ | **1:1** | 1件の入金で1件の請求を消込 | 通常の消込 |
880
+ | **1:N** | 1件の入金で複数の請求を消込 | 古い請求から順に消込、余剰は前受金 |
881
+ | **N:1** | 複数の入金で1件の請求を消込 | 分割入金、一部入金状態の管理 |
882
+
883
+ ### 入金データ・入金消込明細の構造
884
+
885
+ #### 入金データの ER 図
886
+
887
+ ```plantuml
888
+ @startuml
889
+
890
+ title 入金関連テーブル
891
+
892
+ entity 請求データ {
893
+ ID <<PK>>
894
+ --
895
+ 請求番号
896
+ 顧客コード
897
+ 請求残高
898
+ ステータス
899
+ }
900
+
901
+ entity 入金データ {
902
+ ID <<PK>>
903
+ --
904
+ 入金番号 <<UK>>
905
+ 入金日
906
+ 顧客コード <<FK>>
907
+ 入金方法
908
+ 入金金額
909
+ 消込済金額
910
+ 未消込金額
911
+ 手数料
912
+ 振込名義
913
+ 銀行名
914
+ 口座番号
915
+ ステータス
916
+ バージョン
917
+ 備考
918
+ 作成日時
919
+ 更新日時
920
+ }
921
+
922
+ entity 入金消込明細 {
923
+ ID <<PK>>
924
+ --
925
+ 入金ID <<FK>>
926
+ 行番号
927
+ 請求ID <<FK>>
928
+ 消込日
929
+ 消込金額
930
+ バージョン
931
+ 備考
932
+ 作成日時
933
+ }
934
+
935
+ entity 前受金データ {
936
+ ID <<PK>>
937
+ --
938
+ 前受金番号 <<UK>>
939
+ 発生日
940
+ 顧客コード <<FK>>
941
+ 入金ID <<FK>>
942
+ 前受金額
943
+ 使用済金額
944
+ 残高
945
+ 備考
946
+ 作成日時
947
+ 更新日時
948
+ }
949
+
950
+ 請求データ ||--o{ 入金消込明細
951
+ 入金データ ||--o{ 入金消込明細
952
+ 入金データ ||--o| 前受金データ
953
+
954
+ @enduml
955
+ ```
956
+
957
+ ### 入金ステータスの定義
958
+
959
+ ```plantuml
960
+ @startuml
961
+
962
+ title 入金ステータス遷移図
963
+
964
+ [*] --> 入金済
965
+
966
+ 入金済 --> 消込済 : 全額消込
967
+ 入金済 --> 一部消込 : 一部消込
968
+
969
+ 一部消込 --> 消込済 : 残額消込
970
+ 一部消込 --> 過入金 : 余剰発生
971
+
972
+ 入金済 --> 過入金 : 過入金発生
973
+
974
+ 消込済 --> [*]
975
+ 過入金 --> [*]
976
+
977
+ @enduml
978
+ ```
979
+
980
+ | ステータス | 説明 |
981
+ |-----------|------|
982
+ | **入金済** | 入金登録完了、未消込状態 |
983
+ | **一部消込** | 一部が消込済み |
984
+ | **消込済** | 全額が消込完了 |
985
+ | **過入金** | 入金額が請求額を超過 |
986
+
987
+ ### マイグレーション:入金関連テーブルの作成
988
+
989
+ <details>
990
+ <summary>V012__create_receipt_tables.sql</summary>
991
+
992
+ ```sql
993
+ -- src/main/resources/db/migration/V012__create_receipt_tables.sql
994
+
995
+ -- 入金ステータス
996
+ CREATE TYPE 入金ステータス AS ENUM ('入金済', '一部消込', '消込済', '過入金');
997
+
998
+ -- 入金方法(取引先の支払方法とは別)
999
+ CREATE TYPE 入金方法 AS ENUM ('現金', '銀行振込', 'クレジットカード', '手形', '電子記録債権');
1000
+
1001
+ -- 入金データ
1002
+ CREATE TABLE "入金データ" (
1003
+ "ID" SERIAL PRIMARY KEY,
1004
+ "入金番号" VARCHAR(20) UNIQUE NOT NULL,
1005
+ "入金日" DATE NOT NULL,
1006
+ "顧客コード" VARCHAR(20) NOT NULL,
1007
+ "顧客枝番" VARCHAR(10) DEFAULT '00',
1008
+ "入金方法" 入金方法 NOT NULL,
1009
+ "入金金額" DECIMAL(15, 2) NOT NULL,
1010
+ "消込済金額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
1011
+ "未消込金額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
1012
+ "手数料" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
1013
+ "振込名義" VARCHAR(100),
1014
+ "銀行名" VARCHAR(50),
1015
+ "口座番号" VARCHAR(20),
1016
+ "ステータス" 入金ステータス DEFAULT '入金済' NOT NULL,
1017
+ "備考" TEXT,
1018
+ "バージョン" INTEGER DEFAULT 1 NOT NULL,
1019
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1020
+ "作成者" VARCHAR(50),
1021
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1022
+ "更新者" VARCHAR(50),
1023
+ CONSTRAINT "fk_入金データ_顧客"
1024
+ FOREIGN KEY ("顧客コード", "顧客枝番") REFERENCES "顧客マスタ"("顧客コード", "顧客枝番")
1025
+ );
1026
+
1027
+ -- 入金消込明細
1028
+ CREATE TABLE "入金消込明細" (
1029
+ "ID" SERIAL PRIMARY KEY,
1030
+ "入金ID" INTEGER NOT NULL,
1031
+ "行番号" INTEGER NOT NULL,
1032
+ "請求ID" INTEGER,
1033
+ "消込日" DATE NOT NULL,
1034
+ "消込金額" DECIMAL(15, 2) NOT NULL,
1035
+ "備考" TEXT,
1036
+ "バージョン" INTEGER DEFAULT 1 NOT NULL,
1037
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1038
+ CONSTRAINT "fk_入金消込明細_入金"
1039
+ FOREIGN KEY ("入金ID") REFERENCES "入金データ"("ID") ON DELETE CASCADE,
1040
+ CONSTRAINT "fk_入金消込明細_請求"
1041
+ FOREIGN KEY ("請求ID") REFERENCES "請求データ"("ID"),
1042
+ CONSTRAINT "uk_入金消込明細_入金_行" UNIQUE ("入金ID", "行番号")
1043
+ );
1044
+
1045
+ -- 前受金データ
1046
+ CREATE TABLE "前受金データ" (
1047
+ "ID" SERIAL PRIMARY KEY,
1048
+ "前受金番号" VARCHAR(20) UNIQUE NOT NULL,
1049
+ "発生日" DATE NOT NULL,
1050
+ "顧客コード" VARCHAR(20) NOT NULL,
1051
+ "顧客枝番" VARCHAR(10) DEFAULT '00',
1052
+ "入金ID" INTEGER,
1053
+ "前受金額" DECIMAL(15, 2) NOT NULL,
1054
+ "使用済金額" DECIMAL(15, 2) DEFAULT 0 NOT NULL,
1055
+ "残高" DECIMAL(15, 2) NOT NULL,
1056
+ "備考" TEXT,
1057
+ "作成日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1058
+ "更新日時" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
1059
+ CONSTRAINT "fk_前受金データ_顧客"
1060
+ FOREIGN KEY ("顧客コード", "顧客枝番") REFERENCES "顧客マスタ"("顧客コード", "顧客枝番"),
1061
+ CONSTRAINT "fk_前受金データ_入金"
1062
+ FOREIGN KEY ("入金ID") REFERENCES "入金データ"("ID")
1063
+ );
1064
+
1065
+ -- インデックス
1066
+ CREATE INDEX "idx_入金データ_顧客コード" ON "入金データ"("顧客コード");
1067
+ CREATE INDEX "idx_入金データ_入金日" ON "入金データ"("入金日");
1068
+ CREATE INDEX "idx_入金データ_ステータス" ON "入金データ"("ステータス");
1069
+ CREATE INDEX "idx_入金消込明細_入金ID" ON "入金消込明細"("入金ID");
1070
+ CREATE INDEX "idx_入金消込明細_請求ID" ON "入金消込明細"("請求ID");
1071
+ CREATE INDEX "idx_前受金データ_顧客コード" ON "前受金データ"("顧客コード");
1072
+
1073
+ -- テーブルコメント
1074
+ COMMENT ON TABLE "入金データ" IS '入金情報を管理するテーブル';
1075
+ COMMENT ON TABLE "入金消込明細" IS '入金と請求の消込明細を管理するテーブル';
1076
+ COMMENT ON TABLE "前受金データ" IS '過入金による前受金を管理するテーブル';
1077
+
1078
+ -- カラムコメント
1079
+ COMMENT ON COLUMN "入金データ"."バージョン" IS '楽観ロック用バージョン番号';
1080
+ COMMENT ON COLUMN "入金消込明細"."バージョン" IS '楽観ロック用バージョン番号';
1081
+ ```
1082
+
1083
+ </details>
1084
+
1085
+ ### 入金エンティティの実装
1086
+
1087
+ <details>
1088
+ <summary>入金ステータス ENUM</summary>
1089
+
1090
+ ```java
1091
+ // src/main/java/com/example/sms/domain/model/receipt/ReceiptStatus.java
1092
+ package com.example.sms.domain.model.receipt;
1093
+
1094
+ import lombok.Getter;
1095
+ import lombok.RequiredArgsConstructor;
1096
+
1097
+ /**
1098
+ * 入金ステータス.
1099
+ */
1100
+ @Getter
1101
+ @RequiredArgsConstructor
1102
+ public enum ReceiptStatus {
1103
+ RECEIVED("入金済"),
1104
+ PARTIALLY_APPLIED("一部消込"),
1105
+ APPLIED("消込済"),
1106
+ OVERPAID("過入金");
1107
+
1108
+ private final String displayName;
1109
+
1110
+ /**
1111
+ * 表示名から入金ステータスを取得する.
1112
+ *
1113
+ * @param displayName 表示名
1114
+ * @return 入金ステータス
1115
+ */
1116
+ public static ReceiptStatus fromDisplayName(String displayName) {
1117
+ for (ReceiptStatus status : values()) {
1118
+ if (status.displayName.equals(displayName)) {
1119
+ return status;
1120
+ }
1121
+ }
1122
+ throw new IllegalArgumentException("不正な入金ステータス: " + displayName);
1123
+ }
1124
+ }
1125
+ ```
1126
+
1127
+ </details>
1128
+
1129
+ <details>
1130
+ <summary>入金方法 ENUM</summary>
1131
+
1132
+ ```java
1133
+ // src/main/java/com/example/sms/domain/model/receipt/ReceiptMethod.java
1134
+ package com.example.sms.domain.model.receipt;
1135
+
1136
+ import lombok.Getter;
1137
+ import lombok.RequiredArgsConstructor;
1138
+
1139
+ /**
1140
+ * 入金方法.
1141
+ */
1142
+ @Getter
1143
+ @RequiredArgsConstructor
1144
+ public enum ReceiptMethod {
1145
+ CASH("現金"),
1146
+ BANK_TRANSFER("銀行振込"),
1147
+ CREDIT_CARD("クレジットカード"),
1148
+ BILL("手形"),
1149
+ ELECTRONIC_BOND("電子記録債権");
1150
+
1151
+ private final String displayName;
1152
+
1153
+ /**
1154
+ * 表示名から入金方法を取得する.
1155
+ *
1156
+ * @param displayName 表示名
1157
+ * @return 入金方法
1158
+ */
1159
+ public static ReceiptMethod fromDisplayName(String displayName) {
1160
+ for (ReceiptMethod method : values()) {
1161
+ if (method.displayName.equals(displayName)) {
1162
+ return method;
1163
+ }
1164
+ }
1165
+ throw new IllegalArgumentException("不正な入金方法: " + displayName);
1166
+ }
1167
+ }
1168
+ ```
1169
+
1170
+ </details>
1171
+
1172
+ <details>
1173
+ <summary>入金エンティティ</summary>
1174
+
1175
+ ```java
1176
+ // src/main/java/com/example/sms/domain/model/receipt/Receipt.java
1177
+ package com.example.sms.domain.model.receipt;
1178
+
1179
+ import lombok.AllArgsConstructor;
1180
+ import lombok.Builder;
1181
+ import lombok.Data;
1182
+ import lombok.NoArgsConstructor;
1183
+
1184
+ import java.math.BigDecimal;
1185
+ import java.time.LocalDate;
1186
+ import java.time.LocalDateTime;
1187
+ import java.util.ArrayList;
1188
+ import java.util.List;
1189
+
1190
+ /**
1191
+ * 入金エンティティ.
1192
+ */
1193
+ @Data
1194
+ @Builder
1195
+ @NoArgsConstructor
1196
+ @AllArgsConstructor
1197
+ @SuppressWarnings("PMD.RedundantFieldInitializer")
1198
+ public class Receipt {
1199
+ private Integer id;
1200
+ private String receiptNumber;
1201
+ private LocalDate receiptDate;
1202
+ private String customerCode;
1203
+ private String customerBranchNumber;
1204
+ private ReceiptMethod receiptMethod;
1205
+ private BigDecimal receiptAmount;
1206
+ @Builder.Default
1207
+ private BigDecimal appliedAmount = BigDecimal.ZERO;
1208
+ @Builder.Default
1209
+ private BigDecimal unappliedAmount = BigDecimal.ZERO;
1210
+ @Builder.Default
1211
+ private BigDecimal bankFee = BigDecimal.ZERO;
1212
+ private String payerName;
1213
+ private String bankName;
1214
+ private String accountNumber;
1215
+ @Builder.Default
1216
+ private ReceiptStatus status = ReceiptStatus.RECEIVED;
1217
+ private String remarks;
1218
+ private LocalDateTime createdAt;
1219
+ private String createdBy;
1220
+ private LocalDateTime updatedAt;
1221
+ private String updatedBy;
1222
+
1223
+ /** 楽観ロック用バージョン. */
1224
+ @Builder.Default
1225
+ private Integer version = 1;
1226
+
1227
+ @Builder.Default
1228
+ private List<ReceiptApplication> applications = new ArrayList<>();
1229
+
1230
+ /**
1231
+ * 未消込金額を計算する.
1232
+ *
1233
+ * @return 未消込金額
1234
+ */
1235
+ public BigDecimal calculateUnappliedAmount() {
1236
+ return receiptAmount.subtract(appliedAmount).subtract(bankFee);
1237
+ }
1238
+
1239
+ /**
1240
+ * 消込可能かどうかを判定する.
1241
+ *
1242
+ * @param amount 消込金額
1243
+ * @return 消込可能な場合は true
1244
+ */
1245
+ public boolean canApply(BigDecimal amount) {
1246
+ return unappliedAmount.compareTo(amount) >= 0;
1247
+ }
1248
+ }
1249
+ ```
1250
+
1251
+ </details>
1252
+
1253
+ <details>
1254
+ <summary>入金消込明細エンティティ</summary>
1255
+
1256
+ ```java
1257
+ // src/main/java/com/example/sms/domain/model/receipt/ReceiptApplication.java
1258
+ package com.example.sms.domain.model.receipt;
1259
+
1260
+ import lombok.AllArgsConstructor;
1261
+ import lombok.Builder;
1262
+ import lombok.Data;
1263
+ import lombok.NoArgsConstructor;
1264
+
1265
+ import java.math.BigDecimal;
1266
+ import java.time.LocalDate;
1267
+ import java.time.LocalDateTime;
1268
+
1269
+ /**
1270
+ * 入金消込明細エンティティ.
1271
+ */
1272
+ @Data
1273
+ @Builder
1274
+ @NoArgsConstructor
1275
+ @AllArgsConstructor
1276
+ @SuppressWarnings("PMD.RedundantFieldInitializer")
1277
+ public class ReceiptApplication {
1278
+ private Integer id;
1279
+ private Integer receiptId;
1280
+ private Integer lineNumber;
1281
+ private Integer invoiceId;
1282
+ private LocalDate applicationDate;
1283
+ private BigDecimal appliedAmount;
1284
+ private String remarks;
1285
+ @Builder.Default
1286
+ private Integer version = 1;
1287
+ private LocalDateTime createdAt;
1288
+ }
1289
+ ```
1290
+
1291
+ </details>
1292
+
1293
+ ### 入金消込サービスの実装
1294
+
1295
+ <details>
1296
+ <summary>入金消込サービス</summary>
1297
+
1298
+ ```java
1299
+ // src/main/java/com/example/sms/application/service/ReceiptService.java
1300
+ package com.example.sms.application.service;
1301
+
1302
+ import com.example.sms.application.port.out.*;
1303
+ import com.example.sms.domain.model.invoice.Invoice;
1304
+ import com.example.sms.domain.model.invoice.InvoiceStatus;
1305
+ import com.example.sms.domain.model.receipt.*;
1306
+ import lombok.RequiredArgsConstructor;
1307
+ import org.springframework.stereotype.Service;
1308
+ import org.springframework.transaction.annotation.Transactional;
1309
+
1310
+ import java.math.BigDecimal;
1311
+ import java.time.LocalDate;
1312
+ import java.util.ArrayList;
1313
+ import java.util.List;
1314
+
1315
+ @Service
1316
+ @RequiredArgsConstructor
1317
+ public class ReceiptService {
1318
+
1319
+ private final ReceiptRepository receiptRepository;
1320
+ private final InvoiceRepository invoiceRepository;
1321
+ private final AdvanceReceiptRepository advanceReceiptRepository;
1322
+
1323
+ /**
1324
+ * 入金を登録する
1325
+ */
1326
+ @Transactional
1327
+ public Receipt registerReceipt(Receipt receipt) {
1328
+ receipt.setUnappliedAmount(
1329
+ receipt.getReceiptAmount().subtract(receipt.getBankFee()));
1330
+ receipt.setAppliedAmount(BigDecimal.ZERO);
1331
+ receipt.setStatus(ReceiptStatus.RECEIVED);
1332
+ receiptRepository.save(receipt);
1333
+ return receipt;
1334
+ }
1335
+
1336
+ /**
1337
+ * 入金を請求に消込する
1338
+ */
1339
+ @Transactional
1340
+ public ReceiptApplication applyReceipt(
1341
+ Integer receiptId, Integer invoiceId, BigDecimal amount) {
1342
+
1343
+ var receipt = receiptRepository.findById(receiptId)
1344
+ .orElseThrow(() -> new IllegalArgumentException(
1345
+ "入金が見つかりません: " + receiptId));
1346
+
1347
+ var invoice = invoiceRepository.findById(invoiceId)
1348
+ .orElseThrow(() -> new IllegalArgumentException(
1349
+ "請求が見つかりません: " + invoiceId));
1350
+
1351
+ // バリデーション
1352
+ if (!receipt.canApply(amount)) {
1353
+ throw new IllegalStateException("消込可能金額が不足しています");
1354
+ }
1355
+ if (amount.compareTo(invoice.getInvoiceBalance()) > 0) {
1356
+ throw new IllegalStateException("消込金額が請求残高を超えています");
1357
+ }
1358
+
1359
+ // 消込明細を作成
1360
+ int lineNumber = getNextLineNumber(receiptId);
1361
+ var application = ReceiptApplication.builder()
1362
+ .receiptId(receiptId)
1363
+ .lineNumber(lineNumber)
1364
+ .invoiceId(invoiceId)
1365
+ .applicationDate(LocalDate.now())
1366
+ .appliedAmount(amount)
1367
+ .build();
1368
+ receiptRepository.saveApplication(application);
1369
+
1370
+ // 入金の金額を更新
1371
+ BigDecimal newAppliedAmount = receipt.getAppliedAmount().add(amount);
1372
+ BigDecimal newUnappliedAmount = receipt.getUnappliedAmount().subtract(amount);
1373
+ receiptRepository.updateAmounts(receiptId, newAppliedAmount, newUnappliedAmount);
1374
+
1375
+ // 入金ステータスを更新
1376
+ if (newUnappliedAmount.compareTo(BigDecimal.ZERO) <= 0) {
1377
+ receiptRepository.updateStatus(receiptId, ReceiptStatus.APPLIED);
1378
+ }
1379
+
1380
+ // 請求の入金額を更新
1381
+ invoiceRepository.updateReceiptAmount(invoiceId, amount);
1382
+
1383
+ // 請求ステータスを更新
1384
+ var updatedInvoice = invoiceRepository.findById(invoiceId).get();
1385
+ if (updatedInvoice.getInvoiceBalance().compareTo(BigDecimal.ZERO) <= 0) {
1386
+ invoiceRepository.updateStatus(invoiceId, InvoiceStatus.PAID);
1387
+ } else {
1388
+ invoiceRepository.updateStatus(invoiceId, InvoiceStatus.PARTIALLY_PAID);
1389
+ }
1390
+
1391
+ return application;
1392
+ }
1393
+
1394
+ /**
1395
+ * 自動消込を実行する(古い請求から順に消込)
1396
+ */
1397
+ @Transactional
1398
+ public List<ReceiptApplication> autoApply(Integer receiptId) {
1399
+ var receipt = receiptRepository.findById(receiptId)
1400
+ .orElseThrow(() -> new IllegalArgumentException(
1401
+ "入金が見つかりません: " + receiptId));
1402
+
1403
+ List<Invoice> unpaidInvoices = invoiceRepository
1404
+ .findUnpaidByCustomerCode(receipt.getCustomerCode());
1405
+
1406
+ BigDecimal remainingAmount = receipt.getUnappliedAmount();
1407
+ List<ReceiptApplication> applications = new ArrayList<>();
1408
+
1409
+ for (Invoice invoice : unpaidInvoices) {
1410
+ if (remainingAmount.compareTo(BigDecimal.ZERO) <= 0) {
1411
+ break;
1412
+ }
1413
+
1414
+ BigDecimal applyAmount = remainingAmount.min(invoice.getInvoiceBalance());
1415
+ var application = applyReceipt(receiptId, invoice.getId(), applyAmount);
1416
+ applications.add(application);
1417
+
1418
+ remainingAmount = remainingAmount.subtract(applyAmount);
1419
+ }
1420
+
1421
+ // 過入金の場合は前受金として登録
1422
+ if (remainingAmount.compareTo(BigDecimal.ZERO) > 0) {
1423
+ createAdvanceReceipt(receipt.getCustomerCode(), receiptId, remainingAmount);
1424
+ receiptRepository.updateStatus(receiptId, ReceiptStatus.OVERPAID);
1425
+ }
1426
+
1427
+ return applications;
1428
+ }
1429
+
1430
+ private void createAdvanceReceipt(
1431
+ String customerCode, Integer receiptId, BigDecimal amount) {
1432
+ var advanceReceipt = AdvanceReceipt.builder()
1433
+ .advanceReceiptNumber(generateAdvanceReceiptNumber())
1434
+ .occurredDate(LocalDate.now())
1435
+ .customerCode(customerCode)
1436
+ .receiptId(receiptId)
1437
+ .advanceAmount(amount)
1438
+ .usedAmount(BigDecimal.ZERO)
1439
+ .balance(amount)
1440
+ .build();
1441
+ advanceReceiptRepository.save(advanceReceipt);
1442
+ }
1443
+
1444
+ private int getNextLineNumber(Integer receiptId) {
1445
+ var applications = receiptRepository.findApplicationsByReceiptId(receiptId);
1446
+ return applications.size() + 1;
1447
+ }
1448
+
1449
+ private String generateAdvanceReceiptNumber() {
1450
+ return String.format("ADV-%d-%04d",
1451
+ LocalDate.now().getYear(), System.currentTimeMillis() % 10000);
1452
+ }
1453
+ }
1454
+ ```
1455
+
1456
+ </details>
1457
+
1458
+ ### 債権残高管理サービス
1459
+
1460
+ <details>
1461
+ <summary>売掛金残高サービス</summary>
1462
+
1463
+ ```java
1464
+ // src/main/java/com/example/sms/application/service/AccountsReceivableService.java
1465
+ package com.example.sms.application.service;
1466
+
1467
+ import com.example.sms.application.port.out.*;
1468
+ import com.example.sms.domain.model.invoice.AccountsReceivable;
1469
+ import lombok.RequiredArgsConstructor;
1470
+ import org.springframework.stereotype.Service;
1471
+ import org.springframework.transaction.annotation.Transactional;
1472
+
1473
+ import java.math.BigDecimal;
1474
+ import java.time.LocalDate;
1475
+ import java.time.YearMonth;
1476
+ import java.util.List;
1477
+
1478
+ @Service
1479
+ @RequiredArgsConstructor
1480
+ public class AccountsReceivableService {
1481
+
1482
+ private final AccountsReceivableRepository arRepository;
1483
+ private final SalesRepository salesRepository;
1484
+ private final ReceiptRepository receiptRepository;
1485
+ private final CustomerRepository customerRepository;
1486
+
1487
+ /**
1488
+ * 月次売掛金残高を更新する
1489
+ */
1490
+ @Transactional
1491
+ public void updateMonthlyBalance(YearMonth yearMonth) {
1492
+ LocalDate baseDate = yearMonth.atEndOfMonth();
1493
+ LocalDate fromDate = yearMonth.atDay(1);
1494
+ LocalDate toDate = yearMonth.atEndOfMonth();
1495
+
1496
+ var customers = customerRepository.findAll();
1497
+
1498
+ for (var customer : customers) {
1499
+ // 前月残高を取得
1500
+ BigDecimal previousBalance = getPreviousMonthBalance(
1501
+ customer.getCustomerCode(), yearMonth);
1502
+
1503
+ // 当月売上を集計
1504
+ BigDecimal currentSales = salesRepository
1505
+ .sumSalesByCustomerAndDateRange(
1506
+ customer.getCustomerCode(), fromDate, toDate);
1507
+
1508
+ // 当月入金を集計
1509
+ BigDecimal currentReceipts = receiptRepository
1510
+ .sumReceiptsByCustomerAndDateRange(
1511
+ customer.getCustomerCode(), fromDate, toDate);
1512
+
1513
+ // 当月残高を計算
1514
+ BigDecimal currentBalance = previousBalance
1515
+ .add(currentSales)
1516
+ .subtract(currentReceipts);
1517
+
1518
+ // 売掛金残高を更新
1519
+ var ar = AccountsReceivable.builder()
1520
+ .customerCode(customer.getCustomerCode())
1521
+ .baseDate(baseDate)
1522
+ .previousMonthBalance(previousBalance)
1523
+ .currentMonthSales(currentSales)
1524
+ .currentMonthReceipts(currentReceipts)
1525
+ .currentMonthBalance(currentBalance)
1526
+ .build();
1527
+ arRepository.upsert(ar);
1528
+ }
1529
+ }
1530
+
1531
+ /**
1532
+ * 顧客別売掛金残高を取得する
1533
+ */
1534
+ public List<AccountsReceivable> getCustomerBalances(LocalDate baseDate) {
1535
+ return arRepository.findByBaseDate(baseDate);
1536
+ }
1537
+
1538
+ /**
1539
+ * 滞留債権一覧を取得する
1540
+ */
1541
+ public List<AccountsReceivable> getOverdueBalances(
1542
+ LocalDate currentDate, int overdueDays) {
1543
+ LocalDate cutoffDate = currentDate.minusDays(overdueDays);
1544
+ return arRepository.findOverdue(cutoffDate);
1545
+ }
1546
+
1547
+ private BigDecimal getPreviousMonthBalance(
1548
+ String customerCode, YearMonth yearMonth) {
1549
+ LocalDate previousMonthEnd = yearMonth.minusMonths(1).atEndOfMonth();
1550
+ return arRepository.findByCustomerCodeAndBaseDate(
1551
+ customerCode, previousMonthEnd)
1552
+ .map(AccountsReceivable::getCurrentMonthBalance)
1553
+ .orElse(BigDecimal.ZERO);
1554
+ }
1555
+ }
1556
+ ```
1557
+
1558
+ </details>
1559
+
1560
+ ---
1561
+
1562
+ ## 7.3 リレーションと楽観ロックの設計
1563
+
1564
+ ### N+1 問題とその解決
1565
+
1566
+ 請求データは、請求(ヘッダ)→ 請求明細の2層構造を持ちます。入金データも入金 → 入金消込明細の親子関係があります。これらのデータを効率的に取得するための設計を行います。
1567
+
1568
+ ```plantuml
1569
+ @startuml
1570
+
1571
+ title N+1 問題の発生パターン
1572
+
1573
+ rectangle "N+1 問題(非効率)" #FFCCCC {
1574
+ card "1. 請求ヘッダ取得\n SELECT * FROM 請求データ\n (1回)" as q1
1575
+ card "2. 各請求の明細取得\n SELECT * FROM 請求明細\n WHERE 請求ID = ?\n (N回)" as q2
1576
+ }
1577
+
1578
+ rectangle "解決策:JOINによる一括取得" #CCFFCC {
1579
+ card "1回のJOINクエリで\n全データを取得\n SELECT i.*, id.*\n FROM 請求データ i\n LEFT JOIN 請求明細 id ON ..." as sol
1580
+ }
1581
+
1582
+ q1 --> q2 : N件
1583
+ note right of q2
1584
+ 10件の請求があれば
1585
+ 合計11回のクエリ発行
1586
+ end note
1587
+
1588
+ @enduml
1589
+ ```
1590
+
1591
+ ### MyBatis ネストした ResultMap による関連付け
1592
+
1593
+ <details>
1594
+ <summary>請求データのリレーション設定(InvoiceMapper.xml)</summary>
1595
+
1596
+ ```xml
1597
+ <?xml version="1.0" encoding="UTF-8" ?>
1598
+ <!DOCTYPE mapper
1599
+ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
1600
+ "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
1601
+ <mapper namespace="com.example.sms.infrastructure.persistence.mapper.InvoiceMapper">
1602
+
1603
+ <!-- 請求(ヘッダ)の ResultMap -->
1604
+ <resultMap id="InvoiceWithDetailsResultMap"
1605
+ type="com.example.sms.domain.model.invoice.Invoice">
1606
+ <id property="id" column="i_id"/>
1607
+ <result property="invoiceNumber" column="i_請求番号"/>
1608
+ <result property="invoiceDate" column="i_請求日"/>
1609
+ <result property="billingCode" column="i_請求先コード"/>
1610
+ <result property="customerCode" column="i_顧客コード"/>
1611
+ <result property="closingDate" column="i_締日"/>
1612
+ <result property="invoiceType" column="i_請求区分"
1613
+ typeHandler="com.example.sms.infrastructure.persistence.typehandler.InvoiceTypeTypeHandler"/>
1614
+ <result property="previousBalance" column="i_前回請求残高"/>
1615
+ <result property="receiptAmount" column="i_入金額"/>
1616
+ <result property="carriedBalance" column="i_繰越残高"/>
1617
+ <result property="currentSalesAmount" column="i_今回売上額"/>
1618
+ <result property="currentTaxAmount" column="i_今回消費税額"/>
1619
+ <result property="currentInvoiceAmount" column="i_今回請求額"/>
1620
+ <result property="invoiceBalance" column="i_請求残高"/>
1621
+ <result property="dueDate" column="i_回収予定日"/>
1622
+ <result property="status" column="i_ステータス"
1623
+ typeHandler="com.example.sms.infrastructure.persistence.typehandler.InvoiceStatusTypeHandler"/>
1624
+ <result property="version" column="i_バージョン"/>
1625
+ <result property="createdAt" column="i_作成日時"/>
1626
+ <result property="updatedAt" column="i_更新日時"/>
1627
+ <!-- 請求明細との1:N関連 -->
1628
+ <collection property="details"
1629
+ ofType="com.example.sms.domain.model.invoice.InvoiceDetail"
1630
+ resultMap="InvoiceDetailNestedResultMap"/>
1631
+ </resultMap>
1632
+
1633
+ <!-- 請求明細のネスト ResultMap -->
1634
+ <resultMap id="InvoiceDetailNestedResultMap"
1635
+ type="com.example.sms.domain.model.invoice.InvoiceDetail">
1636
+ <id property="id" column="id_id"/>
1637
+ <result property="invoiceId" column="id_請求ID"/>
1638
+ <result property="lineNumber" column="id_行番号"/>
1639
+ <result property="salesId" column="id_売上ID"/>
1640
+ <result property="salesNumber" column="id_売上番号"/>
1641
+ <result property="salesDate" column="id_売上日"/>
1642
+ <result property="salesAmount" column="id_売上金額"/>
1643
+ <result property="taxAmount" column="id_消費税額"/>
1644
+ <result property="totalAmount" column="id_合計金額"/>
1645
+ </resultMap>
1646
+
1647
+ <!-- JOIN による一括取得クエリ -->
1648
+ <select id="findWithDetailsByInvoiceNumber"
1649
+ resultMap="InvoiceWithDetailsResultMap">
1650
+ SELECT
1651
+ i."ID" AS i_id,
1652
+ i."請求番号" AS i_請求番号,
1653
+ i."請求日" AS i_請求日,
1654
+ i."請求先コード" AS i_請求先コード,
1655
+ i."顧客コード" AS i_顧客コード,
1656
+ i."締日" AS i_締日,
1657
+ i."請求区分" AS i_請求区分,
1658
+ i."前回請求残高" AS i_前回請求残高,
1659
+ i."入金額" AS i_入金額,
1660
+ i."繰越残高" AS i_繰越残高,
1661
+ i."今回売上額" AS i_今回売上額,
1662
+ i."今回消費税額" AS i_今回消費税額,
1663
+ i."今回請求額" AS i_今回請求額,
1664
+ i."請求残高" AS i_請求残高,
1665
+ i."回収予定日" AS i_回収予定日,
1666
+ i."ステータス" AS i_ステータス,
1667
+ i."バージョン" AS i_バージョン,
1668
+ i."作成日時" AS i_作成日時,
1669
+ i."更新日時" AS i_更新日時,
1670
+ id."ID" AS id_id,
1671
+ id."請求ID" AS id_請求ID,
1672
+ id."行番号" AS id_行番号,
1673
+ id."売上ID" AS id_売上ID,
1674
+ id."売上番号" AS id_売上番号,
1675
+ id."売上日" AS id_売上日,
1676
+ id."売上金額" AS id_売上金額,
1677
+ id."消費税額" AS id_消費税額,
1678
+ id."合計金額" AS id_合計金額
1679
+ FROM "請求データ" i
1680
+ LEFT JOIN "請求明細" id
1681
+ ON i."ID" = id."請求ID"
1682
+ WHERE i."請求番号" = #{invoiceNumber}
1683
+ ORDER BY id."行番号"
1684
+ </select>
1685
+
1686
+ </mapper>
1687
+ ```
1688
+
1689
+ </details>
1690
+
1691
+ ### リレーション設定のポイント
1692
+
1693
+ | 設定項目 | 説明 |
1694
+ |---------|------|
1695
+ | `<collection>` | 1:N 関連のマッピング |
1696
+ | `<id>` | 主キーの識別(MyBatis が重複排除に使用) |
1697
+ | `resultMap` | ネストした ResultMap の参照 |
1698
+ | エイリアス(AS) | カラム名の重複を避けるためのプレフィックス |
1699
+ | `ORDER BY` | コレクションの順序を保証 |
1700
+
1701
+ ### 楽観ロック(Optimistic Locking)の実装
1702
+
1703
+ 複数ユーザーが同時に請求データや入金データを編集する場合、データの整合性を保つために楽観ロックを実装します。
1704
+
1705
+ ```plantuml
1706
+ @startuml
1707
+
1708
+ title 楽観ロックの動作シーケンス
1709
+
1710
+ actor "ユーザーA" as userA
1711
+ actor "ユーザーB" as userB
1712
+ database "請求テーブル" as db
1713
+
1714
+ userA -> db : SELECT(version=1)
1715
+ userB -> db : SELECT(version=1)
1716
+ userA -> userA : 画面で編集
1717
+ userB -> userB : 画面で編集
1718
+ userA -> db : UPDATE WHERE version=1\n→ version=2 に更新
1719
+ db -> userA : 更新成功(1行)
1720
+ userB -> db : UPDATE WHERE version=1
1721
+ db -> userB : 更新失敗(0行)\n楽観ロック例外
1722
+
1723
+ note right of userB
1724
+ 再読み込みを促す
1725
+ エラーメッセージ表示
1726
+ end note
1727
+
1728
+ @enduml
1729
+ ```
1730
+
1731
+ ### バージョンカラムによる同時更新制御
1732
+
1733
+ <details>
1734
+ <summary>楽観ロック対応の MyBatis Mapper</summary>
1735
+
1736
+ ```xml
1737
+ <!-- 楽観ロック対応の更新(バージョンチェック付き) -->
1738
+ <update id="updateWithOptimisticLock"
1739
+ parameterType="com.example.sms.domain.model.invoice.Invoice">
1740
+ UPDATE "請求データ"
1741
+ SET
1742
+ "請求日" = #{invoiceDate},
1743
+ "請求先コード" = #{billingCode},
1744
+ "顧客コード" = #{customerCode},
1745
+ "締日" = #{closingDate},
1746
+ "請求区分" = #{invoiceType,
1747
+ typeHandler=com.example.sms.infrastructure.persistence.typehandler.InvoiceTypeTypeHandler}::請求区分,
1748
+ "前回請求残高" = #{previousBalance},
1749
+ "入金額" = #{receiptAmount},
1750
+ "繰越残高" = #{carriedBalance},
1751
+ "今回売上額" = #{currentSalesAmount},
1752
+ "今回消費税額" = #{currentTaxAmount},
1753
+ "今回請求額" = #{currentInvoiceAmount},
1754
+ "請求残高" = #{invoiceBalance},
1755
+ "回収予定日" = #{dueDate},
1756
+ "ステータス" = #{status,
1757
+ typeHandler=com.example.sms.infrastructure.persistence.typehandler.InvoiceStatusTypeHandler}::請求ステータス,
1758
+ "更新日時" = CURRENT_TIMESTAMP,
1759
+ "バージョン" = "バージョン" + 1
1760
+ WHERE "ID" = #{id}
1761
+ AND "バージョン" = #{version}
1762
+ </update>
1763
+
1764
+ <!-- 楽観ロック対応の削除 -->
1765
+ <delete id="deleteWithOptimisticLock">
1766
+ DELETE FROM "請求データ"
1767
+ WHERE "ID" = #{id}
1768
+ AND "バージョン" = #{version}
1769
+ </delete>
1770
+ ```
1771
+
1772
+ </details>
1773
+
1774
+ <details>
1775
+ <summary>楽観ロック例外クラス</summary>
1776
+
1777
+ ```java
1778
+ // src/main/java/com/example/sms/domain/exception/OptimisticLockException.java
1779
+ package com.example.sms.domain.exception;
1780
+
1781
+ public class OptimisticLockException extends RuntimeException {
1782
+
1783
+ public OptimisticLockException(String entityName, Integer id) {
1784
+ super(String.format(
1785
+ "%s(ID: %d)は他のユーザーによって更新または削除されました。" +
1786
+ "画面を再読み込みしてください。",
1787
+ entityName, id));
1788
+ }
1789
+
1790
+ public OptimisticLockException(
1791
+ String entityName, Integer id,
1792
+ Integer expectedVersion, Integer actualVersion) {
1793
+ super(String.format(
1794
+ "%s(ID: %d)は他のユーザーによって更新されました。" +
1795
+ "(期待バージョン: %d, 実際のバージョン: %d)" +
1796
+ "画面を再読み込みしてください。",
1797
+ entityName, id, expectedVersion, actualVersion));
1798
+ }
1799
+ }
1800
+ ```
1801
+
1802
+ </details>
1803
+
1804
+ <details>
1805
+ <summary>Repository 実装:楽観ロック対応</summary>
1806
+
1807
+ ```java
1808
+ // src/main/java/com/example/sms/infrastructure/persistence/repository/InvoiceRepositoryImpl.java
1809
+ package com.example.sms.infrastructure.persistence.repository;
1810
+
1811
+ import com.example.sms.application.port.out.InvoiceRepository;
1812
+ import com.example.sms.domain.exception.OptimisticLockException;
1813
+ import com.example.sms.domain.model.invoice.*;
1814
+ import com.example.sms.infrastructure.persistence.mapper.InvoiceMapper;
1815
+ import lombok.RequiredArgsConstructor;
1816
+ import org.springframework.stereotype.Repository;
1817
+ import org.springframework.transaction.annotation.Transactional;
1818
+
1819
+ @Repository
1820
+ @RequiredArgsConstructor
1821
+ public class InvoiceRepositoryImpl implements InvoiceRepository {
1822
+
1823
+ private final InvoiceMapper invoiceMapper;
1824
+
1825
+ /**
1826
+ * 楽観ロック対応の更新
1827
+ * @throws OptimisticLockException 他のユーザーによって更新された場合
1828
+ */
1829
+ @Override
1830
+ @Transactional
1831
+ public void update(Invoice invoice) {
1832
+ int updatedCount = invoiceMapper.updateWithOptimisticLock(invoice);
1833
+
1834
+ if (updatedCount == 0) {
1835
+ Integer currentVersion = invoiceMapper.findVersionById(invoice.getId());
1836
+
1837
+ if (currentVersion == null) {
1838
+ throw new OptimisticLockException("請求", invoice.getId());
1839
+ } else {
1840
+ throw new OptimisticLockException(
1841
+ "請求",
1842
+ invoice.getId(),
1843
+ invoice.getVersion(),
1844
+ currentVersion
1845
+ );
1846
+ }
1847
+ }
1848
+ }
1849
+
1850
+ /**
1851
+ * 楽観ロック対応の削除
1852
+ * @throws OptimisticLockException 他のユーザーによって更新された場合
1853
+ */
1854
+ @Override
1855
+ @Transactional
1856
+ public void delete(Integer id, Integer version) {
1857
+ int deletedCount = invoiceMapper.deleteWithOptimisticLock(id, version);
1858
+
1859
+ if (deletedCount == 0) {
1860
+ throw new OptimisticLockException("請求", id);
1861
+ }
1862
+ }
1863
+ }
1864
+ ```
1865
+
1866
+ </details>
1867
+
1868
+ ### 楽観ロックのテスト
1869
+
1870
+ <details>
1871
+ <summary>楽観ロックのテストコード</summary>
1872
+
1873
+ ```java
1874
+ @Nested
1875
+ @DisplayName("楽観ロックの更新テスト")
1876
+ class OptimisticLockUpdateTest {
1877
+
1878
+ @Test
1879
+ @DisplayName("正しいバージョンで更新できる")
1880
+ void shouldUpdateWithCorrectVersion() {
1881
+ // Given: 請求を登録
1882
+ var invoice = createTestInvoice("INV-0001");
1883
+ invoiceRepository.save(invoice);
1884
+
1885
+ // When: バージョン1で更新
1886
+ var saved = invoiceRepository.findByInvoiceNumber("INV-0001").orElseThrow();
1887
+ assertThat(saved.getVersion()).isEqualTo(1);
1888
+ saved.setInvoiceBalance(new BigDecimal("200000"));
1889
+ invoiceRepository.update(saved);
1890
+
1891
+ // Then: バージョンが2に増加
1892
+ var updated = invoiceRepository.findByInvoiceNumber("INV-0001").orElseThrow();
1893
+ assertThat(updated.getVersion()).isEqualTo(2);
1894
+ }
1895
+
1896
+ @Test
1897
+ @DisplayName("古いバージョンで更新すると楽観ロック例外がスローされる")
1898
+ void shouldThrowOptimisticLockExceptionWithOldVersion() {
1899
+ // Given: 請求を登録して、別のセッションで更新
1900
+ var invoice = createTestInvoice("INV-0002");
1901
+ invoiceRepository.save(invoice);
1902
+
1903
+ var sessionA = invoiceRepository.findByInvoiceNumber("INV-0002").orElseThrow();
1904
+ var sessionB = invoiceRepository.findByInvoiceNumber("INV-0002").orElseThrow();
1905
+
1906
+ // セッションAで更新
1907
+ sessionA.setInvoiceBalance(new BigDecimal("300000"));
1908
+ invoiceRepository.update(sessionA);
1909
+
1910
+ // When/Then: セッションBで更新すると例外
1911
+ sessionB.setInvoiceBalance(new BigDecimal("400000"));
1912
+ assertThatThrownBy(() -> invoiceRepository.update(sessionB))
1913
+ .isInstanceOf(OptimisticLockException.class)
1914
+ .hasMessageContaining("他のユーザーによって更新されました");
1915
+ }
1916
+ }
1917
+ ```
1918
+
1919
+ </details>
1920
+
1921
+ ### 楽観ロックのベストプラクティス
1922
+
1923
+ | ポイント | 説明 |
1924
+ |---------|------|
1925
+ | **バージョンカラム** | INTEGER 型で十分(オーバーフローは現実的に発生しない) |
1926
+ | **WHERE 条件** | 必ず `AND "バージョン" = #{version}` を含める |
1927
+ | **インクリメント** | `"バージョン" = "バージョン" + 1` でアトミックに更新 |
1928
+ | **例外処理** | 更新件数が0の場合は楽観ロック例外をスロー |
1929
+ | **エラーメッセージ** | ユーザーにわかりやすいメッセージで再読み込みを促す |
1930
+
1931
+ ---
1932
+
1933
+ ## 第7章のまとめ
1934
+
1935
+ 本章では、債権管理の核心部分である請求・入金・売掛金管理について学びました。
1936
+
1937
+ ### 学んだこと
1938
+
1939
+ 1. **請求パターンの理解**
1940
+ - 都度請求と締め請求の違い
1941
+ - 締処理による売上の集約
1942
+
1943
+ 2. **請求データの構造**
1944
+ - 前回繰越・今回売上・請求残高の計算
1945
+ - 回収予定日の設定
1946
+
1947
+ 3. **入金消込の実装**
1948
+ - 1:1、1:N、N:1 の消込パターン
1949
+ - 自動消込機能
1950
+
1951
+ 4. **債権残高管理**
1952
+ - 月次売掛金残高の更新
1953
+ - 滞留債権の管理
1954
+
1955
+ 5. **リレーションと楽観ロック**
1956
+ - N+1 問題の回避
1957
+ - 同時更新の競合制御
1958
+
1959
+ ### 債権管理の ER 図(全体像)
1960
+
1961
+ ```plantuml
1962
+ @startuml
1963
+
1964
+ title 債権管理のER図
1965
+
1966
+ entity 売上データ {
1967
+ ID <<PK>>
1968
+ --
1969
+ 売上番号
1970
+ 売上日
1971
+ 顧客コード
1972
+ 売上金額
1973
+ 消費税額
1974
+ ステータス
1975
+ }
1976
+
1977
+ entity 請求データ {
1978
+ ID <<PK>>
1979
+ --
1980
+ 請求番号
1981
+ 請求日
1982
+ 顧客コード
1983
+ 締日
1984
+ 請求区分
1985
+ 前回請求残高
1986
+ 入金額
1987
+ 繰越残高
1988
+ 今回売上額
1989
+ 今回消費税額
1990
+ 今回請求額
1991
+ 請求残高
1992
+ 回収予定日
1993
+ ステータス
1994
+ バージョン
1995
+ }
1996
+
1997
+ entity 請求明細 {
1998
+ ID <<PK>>
1999
+ --
2000
+ 請求ID <<FK>>
2001
+ 売上ID <<FK>>
2002
+ 売上日
2003
+ 売上番号
2004
+ 売上金額
2005
+ 消費税額
2006
+ }
2007
+
2008
+ entity 入金データ {
2009
+ ID <<PK>>
2010
+ --
2011
+ 入金番号
2012
+ 入金日
2013
+ 顧客コード
2014
+ 入金方法
2015
+ 入金金額
2016
+ 消込済金額
2017
+ 未消込金額
2018
+ 手数料
2019
+ ステータス
2020
+ バージョン
2021
+ }
2022
+
2023
+ entity 入金消込明細 {
2024
+ ID <<PK>>
2025
+ --
2026
+ 入金ID <<FK>>
2027
+ 請求ID <<FK>>
2028
+ 消込日
2029
+ 消込金額
2030
+ バージョン
2031
+ }
2032
+
2033
+ entity 前受金データ {
2034
+ ID <<PK>>
2035
+ --
2036
+ 前受金番号
2037
+ 発生日
2038
+ 顧客コード
2039
+ 入金ID <<FK>>
2040
+ 前受金額
2041
+ 使用済金額
2042
+ 残高
2043
+ }
2044
+
2045
+ entity 売掛金残高 {
2046
+ ID <<PK>>
2047
+ --
2048
+ 顧客コード
2049
+ 基準日
2050
+ 前月残高
2051
+ 当月売上
2052
+ 当月入金
2053
+ 当月残高
2054
+ }
2055
+
2056
+ entity 顧客マスタ {
2057
+ 顧客コード <<PK>>
2058
+ --
2059
+ 顧客名
2060
+ 締日
2061
+ 回収サイト
2062
+ }
2063
+
2064
+ 売上データ ||--o{ 請求明細
2065
+ 請求データ ||--o{ 請求明細
2066
+ 請求データ ||--o{ 入金消込明細
2067
+ 入金データ ||--o{ 入金消込明細
2068
+ 入金データ ||--o| 前受金データ
2069
+ 顧客マスタ ||--o{ 請求データ
2070
+ 顧客マスタ ||--o{ 入金データ
2071
+ 顧客マスタ ||--o{ 売掛金残高
2072
+
2073
+ @enduml
2074
+ ```
2075
+
2076
+ ### 次章の予告
2077
+
2078
+ 第8章では、調達管理の設計に進みます。発注・入荷・仕入の一連の購買業務プロセスをデータベース設計と TDD で実装していきます。
2079
+
2080
+ ---
2081
+
2082
+ [← 第6章:受注・出荷・売上の設計](./chapter06.md) | [第8章:調達管理の設計 →](./chapter08.md)