@tomei/finance 0.6.112 → 0.8.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 (381) hide show
  1. package/dist/account/account.d.ts +2 -2
  2. package/dist/account/account.d.ts.map +1 -0
  3. package/dist/account/account.js +1 -1
  4. package/dist/account/account.js.map +1 -1
  5. package/dist/account/account.repository.d.ts +1 -0
  6. package/dist/account/account.repository.d.ts.map +1 -0
  7. package/dist/account/account.repository.js +4 -1
  8. package/dist/account/account.repository.js.map +1 -1
  9. package/dist/account/interfaces/account-attr.interface.d.ts +1 -0
  10. package/dist/account/interfaces/account-attr.interface.d.ts.map +1 -0
  11. package/dist/account-system-entity/account-system-entity.d.ts +1 -0
  12. package/dist/account-system-entity/account-system-entity.d.ts.map +1 -0
  13. package/dist/account-system-entity/account-system-entity.js +4 -1
  14. package/dist/account-system-entity/account-system-entity.js.map +1 -1
  15. package/dist/account-system-entity/post-history.repository.d.ts +1 -0
  16. package/dist/account-system-entity/post-history.repository.d.ts.map +1 -0
  17. package/dist/account-system-entity/post-history.repository.js +4 -1
  18. package/dist/account-system-entity/post-history.repository.js.map +1 -1
  19. package/dist/config.d.ts +1 -0
  20. package/dist/config.d.ts.map +1 -0
  21. package/dist/config.js +17 -4
  22. package/dist/config.js.map +1 -1
  23. package/dist/customer/customer.d.ts +1 -0
  24. package/dist/customer/customer.d.ts.map +1 -0
  25. package/dist/customer/customer.js +38 -2
  26. package/dist/customer/customer.js.map +1 -1
  27. package/dist/customer/finance-customer.repository.d.ts +1 -0
  28. package/dist/customer/finance-customer.repository.d.ts.map +1 -0
  29. package/dist/customer/finance-customer.repository.js +4 -1
  30. package/dist/customer/finance-customer.repository.js.map +1 -1
  31. package/dist/customer/interfaces/customer.repository.interface.d.ts +1 -0
  32. package/dist/customer/interfaces/customer.repository.interface.d.ts.map +1 -0
  33. package/dist/customer/interfaces/finance-customer-attr.interface.d.ts +1 -0
  34. package/dist/customer/interfaces/finance-customer-attr.interface.d.ts.map +1 -0
  35. package/dist/customer/interfaces/finance-customer.repository.interface.d.ts +1 -0
  36. package/dist/customer/interfaces/finance-customer.repository.interface.d.ts.map +1 -0
  37. package/dist/database.d.ts +1 -0
  38. package/dist/database.d.ts.map +1 -0
  39. package/dist/database.js +18 -15
  40. package/dist/database.js.map +1 -1
  41. package/dist/document/document-item.d.ts +3 -2
  42. package/dist/document/document-item.d.ts.map +1 -0
  43. package/dist/document/document-item.js +1 -1
  44. package/dist/document/document-item.js.map +1 -1
  45. package/dist/document/document-item.repository.d.ts +1 -0
  46. package/dist/document/document-item.repository.d.ts.map +1 -0
  47. package/dist/document/document-item.repository.js +4 -1
  48. package/dist/document/document-item.repository.js.map +1 -1
  49. package/dist/document/document.d.ts +2 -1
  50. package/dist/document/document.d.ts.map +1 -0
  51. package/dist/document/document.js +242 -191
  52. package/dist/document/document.js.map +1 -1
  53. package/dist/document/document.repository.d.ts +1 -0
  54. package/dist/document/document.repository.d.ts.map +1 -0
  55. package/dist/document/document.repository.js +4 -1
  56. package/dist/document/document.repository.js.map +1 -1
  57. package/dist/document/interfaces/document-attr.interface.d.ts +1 -0
  58. package/dist/document/interfaces/document-attr.interface.d.ts.map +1 -0
  59. package/dist/document/interfaces/document-find-all.interface.d.ts +1 -0
  60. package/dist/document/interfaces/document-find-all.interface.d.ts.map +1 -0
  61. package/dist/document/interfaces/document-item-attr.interface.d.ts +1 -0
  62. package/dist/document/interfaces/document-item-attr.interface.d.ts.map +1 -0
  63. package/dist/document/interfaces/document-item.repository.interface.d.ts +1 -0
  64. package/dist/document/interfaces/document-item.repository.interface.d.ts.map +1 -0
  65. package/dist/document-tag/document-tag.d.ts +19 -0
  66. package/dist/document-tag/document-tag.d.ts.map +1 -0
  67. package/dist/document-tag/document-tag.js +68 -0
  68. package/dist/document-tag/document-tag.js.map +1 -0
  69. package/dist/document-tag/document-tag.repository.d.ts +6 -0
  70. package/dist/document-tag/document-tag.repository.d.ts.map +1 -0
  71. package/dist/document-tag/document-tag.repository.js +15 -0
  72. package/dist/document-tag/document-tag.repository.js.map +1 -0
  73. package/dist/document-tag/interfaces/document-tag-attr.interface.d.ts +8 -0
  74. package/dist/document-tag/interfaces/document-tag-attr.interface.d.ts.map +1 -0
  75. package/dist/document-tag/interfaces/document-tag-attr.interface.js +3 -0
  76. package/dist/document-tag/interfaces/document-tag-attr.interface.js.map +1 -0
  77. package/dist/enum/collect-payment-type.d.ts +1 -0
  78. package/dist/enum/collect-payment-type.d.ts.map +1 -0
  79. package/dist/enum/doc-type.enum.d.ts +1 -0
  80. package/dist/enum/doc-type.enum.d.ts.map +1 -0
  81. package/dist/enum/document-status.enum.d.ts +1 -0
  82. package/dist/enum/document-status.enum.d.ts.map +1 -0
  83. package/dist/enum/index.d.ts +2 -2
  84. package/dist/enum/index.d.ts.map +1 -0
  85. package/dist/enum/index.js +1 -3
  86. package/dist/enum/index.js.map +1 -1
  87. package/dist/enum/payment-method.enum.d.ts +1 -0
  88. package/dist/enum/payment-method.enum.d.ts.map +1 -0
  89. package/dist/enum/payment-status.enum.d.ts +1 -0
  90. package/dist/enum/payment-status.enum.d.ts.map +1 -0
  91. package/dist/enum/payment-type.enum.d.ts +1 -0
  92. package/dist/enum/payment-type.enum.d.ts.map +1 -0
  93. package/dist/enum/quick-book-client-scopes.enum.d.ts +1 -0
  94. package/dist/enum/quick-book-client-scopes.enum.d.ts.map +1 -0
  95. package/dist/enum/quick-book-client-scopes.enum.js +34 -1
  96. package/dist/enum/quick-book-client-scopes.enum.js.map +1 -1
  97. package/dist/enum/transaction-type.enum.d.ts +1 -0
  98. package/dist/enum/transaction-type.enum.d.ts.map +1 -0
  99. package/dist/finance-company/finance-company.d.ts +21 -18
  100. package/dist/finance-company/finance-company.d.ts.map +1 -0
  101. package/dist/finance-company/finance-company.js +89 -23
  102. package/dist/finance-company/finance-company.js.map +1 -1
  103. package/dist/finance-company/finance-company.repository.d.ts +1 -0
  104. package/dist/finance-company/finance-company.repository.d.ts.map +1 -0
  105. package/dist/finance-company/finance-company.repository.js +4 -1
  106. package/dist/finance-company/finance-company.repository.js.map +1 -1
  107. package/dist/helpers/login-user.d.ts +1 -0
  108. package/dist/helpers/login-user.d.ts.map +1 -0
  109. package/dist/helpers/typeof.d.ts +1 -0
  110. package/dist/helpers/typeof.d.ts.map +1 -0
  111. package/dist/index.d.ts +1 -0
  112. package/dist/index.d.ts.map +1 -0
  113. package/dist/index.js +51 -15
  114. package/dist/index.js.map +1 -1
  115. package/dist/interfaces/account-system.interface.d.ts +4 -0
  116. package/dist/interfaces/account-system.interface.d.ts.map +1 -0
  117. package/dist/interfaces/index.d.ts +1 -0
  118. package/dist/interfaces/index.d.ts.map +1 -0
  119. package/dist/journal-entry/interfaces/journal-entry-attr.interface.d.ts +1 -0
  120. package/dist/journal-entry/interfaces/journal-entry-attr.interface.d.ts.map +1 -0
  121. package/dist/journal-entry/journal-entry.d.ts +1 -0
  122. package/dist/journal-entry/journal-entry.d.ts.map +1 -0
  123. package/dist/journal-entry/journal-entry.js +4 -1
  124. package/dist/journal-entry/journal-entry.js.map +1 -1
  125. package/dist/journal-entry/journal-entry.repository.d.ts +1 -0
  126. package/dist/journal-entry/journal-entry.repository.d.ts.map +1 -0
  127. package/dist/journal-entry/journal-entry.repository.js +4 -1
  128. package/dist/journal-entry/journal-entry.repository.js.map +1 -1
  129. package/dist/ledger-transaction/interfaces/ledger-transaction-attr.interface.d.ts +1 -0
  130. package/dist/ledger-transaction/interfaces/ledger-transaction-attr.interface.d.ts.map +1 -0
  131. package/dist/ledger-transaction/interfaces/ledger-transaction.repository.interface.d.ts +1 -0
  132. package/dist/ledger-transaction/interfaces/ledger-transaction.repository.interface.d.ts.map +1 -0
  133. package/dist/ledger-transaction/ledger-transaction.d.ts +1 -0
  134. package/dist/ledger-transaction/ledger-transaction.d.ts.map +1 -0
  135. package/dist/ledger-transaction/ledger-transaction.repository.d.ts +1 -0
  136. package/dist/ledger-transaction/ledger-transaction.repository.d.ts.map +1 -0
  137. package/dist/ledger-transaction/ledger-transaction.repository.js +4 -1
  138. package/dist/ledger-transaction/ledger-transaction.repository.js.map +1 -1
  139. package/dist/models/account.entity.d.ts +1 -0
  140. package/dist/models/account.entity.d.ts.map +1 -0
  141. package/dist/models/account.entity.js +7 -4
  142. package/dist/models/account.entity.js.map +1 -1
  143. package/dist/models/customer.entity.d.ts +1 -0
  144. package/dist/models/customer.entity.d.ts.map +1 -0
  145. package/dist/models/customer.entity.js +4 -1
  146. package/dist/models/customer.entity.js.map +1 -1
  147. package/dist/models/document-item.entity.d.ts +1 -0
  148. package/dist/models/document-item.entity.d.ts.map +1 -0
  149. package/dist/models/document-item.entity.js +5 -2
  150. package/dist/models/document-item.entity.js.map +1 -1
  151. package/dist/models/document-tag.entity.d.ts +14 -0
  152. package/dist/models/document-tag.entity.d.ts.map +1 -0
  153. package/dist/models/document-tag.entity.js +64 -0
  154. package/dist/models/document-tag.entity.js.map +1 -0
  155. package/dist/models/document.entity.d.ts +3 -0
  156. package/dist/models/document.entity.d.ts.map +1 -0
  157. package/dist/models/document.entity.js +12 -4
  158. package/dist/models/document.entity.js.map +1 -1
  159. package/dist/models/finance-company.entity.d.ts +1 -0
  160. package/dist/models/finance-company.entity.d.ts.map +1 -0
  161. package/dist/models/finance-company.entity.js +5 -2
  162. package/dist/models/finance-company.entity.js.map +1 -1
  163. package/dist/models/journal-entry.entity.d.ts +1 -0
  164. package/dist/models/journal-entry.entity.d.ts.map +1 -0
  165. package/dist/models/journal-entry.entity.js +5 -2
  166. package/dist/models/journal-entry.entity.js.map +1 -1
  167. package/dist/models/ledger-transaction.entity.d.ts +1 -0
  168. package/dist/models/ledger-transaction.entity.d.ts.map +1 -0
  169. package/dist/models/ledger-transaction.entity.js +7 -4
  170. package/dist/models/ledger-transaction.entity.js.map +1 -1
  171. package/dist/models/payment-item.entity.d.ts +1 -0
  172. package/dist/models/payment-item.entity.d.ts.map +1 -0
  173. package/dist/models/payment-item.entity.js +4 -1
  174. package/dist/models/payment-item.entity.js.map +1 -1
  175. package/dist/models/payment-method-type.entity.d.ts +1 -0
  176. package/dist/models/payment-method-type.entity.d.ts.map +1 -0
  177. package/dist/models/payment-method-type.entity.js +5 -2
  178. package/dist/models/payment-method-type.entity.js.map +1 -1
  179. package/dist/models/payment-method.entity.d.ts +1 -0
  180. package/dist/models/payment-method.entity.d.ts.map +1 -0
  181. package/dist/models/payment-method.entity.js +5 -2
  182. package/dist/models/payment-method.entity.js.map +1 -1
  183. package/dist/models/payment-paid-with.entity.d.ts +1 -0
  184. package/dist/models/payment-paid-with.entity.d.ts.map +1 -0
  185. package/dist/models/payment-paid-with.entity.js +5 -2
  186. package/dist/models/payment-paid-with.entity.js.map +1 -1
  187. package/dist/models/payment.entity.d.ts +1 -0
  188. package/dist/models/payment.entity.d.ts.map +1 -0
  189. package/dist/models/payment.entity.js +6 -3
  190. package/dist/models/payment.entity.js.map +1 -1
  191. package/dist/models/post-history.entity.d.ts +1 -0
  192. package/dist/models/post-history.entity.d.ts.map +1 -0
  193. package/dist/models/tag-group.entity.d.ts +16 -0
  194. package/dist/models/tag-group.entity.d.ts.map +1 -0
  195. package/dist/models/tag-group.entity.js +95 -0
  196. package/dist/models/tag-group.entity.js.map +1 -0
  197. package/dist/models/tag.entity.d.ts +17 -0
  198. package/dist/models/tag.entity.d.ts.map +1 -0
  199. package/dist/models/tag.entity.js +100 -0
  200. package/dist/models/tag.entity.js.map +1 -0
  201. package/dist/models/tax.entity.d.ts +1 -0
  202. package/dist/models/tax.entity.d.ts.map +1 -0
  203. package/dist/models/tax.entity.js +4 -1
  204. package/dist/models/tax.entity.js.map +1 -1
  205. package/dist/payment/interfaces/payment-attr.interface.d.ts +1 -0
  206. package/dist/payment/interfaces/payment-attr.interface.d.ts.map +1 -0
  207. package/dist/payment/interfaces/payment-params.interface.d.ts +1 -0
  208. package/dist/payment/interfaces/payment-params.interface.d.ts.map +1 -0
  209. package/dist/payment/payment.d.ts +1 -0
  210. package/dist/payment/payment.d.ts.map +1 -0
  211. package/dist/payment/payment.js +5 -2
  212. package/dist/payment/payment.js.map +1 -1
  213. package/dist/payment/payment.repository.d.ts +1 -0
  214. package/dist/payment/payment.repository.d.ts.map +1 -0
  215. package/dist/payment/payment.repository.js +4 -1
  216. package/dist/payment/payment.repository.js.map +1 -1
  217. package/dist/payment-item/interfaces/payment-item-attr.interface.d.ts +1 -0
  218. package/dist/payment-item/interfaces/payment-item-attr.interface.d.ts.map +1 -0
  219. package/dist/payment-item/payment-item.d.ts +1 -0
  220. package/dist/payment-item/payment-item.d.ts.map +1 -0
  221. package/dist/payment-item/payment-item.js +4 -4
  222. package/dist/payment-item/payment-item.repository.d.ts +1 -0
  223. package/dist/payment-item/payment-item.repository.d.ts.map +1 -0
  224. package/dist/payment-item/payment-item.repository.js +4 -1
  225. package/dist/payment-item/payment-item.repository.js.map +1 -1
  226. package/dist/payment-method/interfaces/payment-method-attr.interface.d.ts +1 -0
  227. package/dist/payment-method/interfaces/payment-method-attr.interface.d.ts.map +1 -0
  228. package/dist/payment-method/payment-method.d.ts +1 -0
  229. package/dist/payment-method/payment-method.d.ts.map +1 -0
  230. package/dist/payment-method/payment-method.js +4 -1
  231. package/dist/payment-method/payment-method.js.map +1 -1
  232. package/dist/payment-method/payment-method.repository.d.ts +1 -0
  233. package/dist/payment-method/payment-method.repository.d.ts.map +1 -0
  234. package/dist/payment-method/payment-method.repository.js +4 -1
  235. package/dist/payment-method/payment-method.repository.js.map +1 -1
  236. package/dist/payment-method-type/interfaces/payment-method-type-attr.interface.d.ts +1 -0
  237. package/dist/payment-method-type/interfaces/payment-method-type-attr.interface.d.ts.map +1 -0
  238. package/dist/payment-method-type/payment-method-type.d.ts +1 -0
  239. package/dist/payment-method-type/payment-method-type.d.ts.map +1 -0
  240. package/dist/payment-method-type/payment-method-type.repository.d.ts +1 -0
  241. package/dist/payment-method-type/payment-method-type.repository.d.ts.map +1 -0
  242. package/dist/payment-method-type/payment-method-type.repository.js +4 -1
  243. package/dist/payment-method-type/payment-method-type.repository.js.map +1 -1
  244. package/dist/payment-paid-with/interfaces/payment-paid-with.interface.d.ts +1 -0
  245. package/dist/payment-paid-with/interfaces/payment-paid-with.interface.d.ts.map +1 -0
  246. package/dist/payment-paid-with/payment-paid-with.d.ts +1 -0
  247. package/dist/payment-paid-with/payment-paid-with.d.ts.map +1 -0
  248. package/dist/payment-paid-with/payment-paid-with.repository.d.ts +1 -0
  249. package/dist/payment-paid-with/payment-paid-with.repository.d.ts.map +1 -0
  250. package/dist/payment-paid-with/payment-paid-with.repository.js +4 -1
  251. package/dist/payment-paid-with/payment-paid-with.repository.js.map +1 -1
  252. package/dist/tag/interfaces/tag-attr.interface.d.ts +14 -0
  253. package/dist/tag/interfaces/tag-attr.interface.d.ts.map +1 -0
  254. package/dist/tag/interfaces/tag-attr.interface.js +3 -0
  255. package/dist/tag/interfaces/tag-attr.interface.js.map +1 -0
  256. package/dist/tag/tag.d.ts +26 -0
  257. package/dist/tag/tag.d.ts.map +1 -0
  258. package/dist/tag/tag.js +110 -0
  259. package/dist/tag/tag.js.map +1 -0
  260. package/dist/tag/tag.repository.d.ts +6 -0
  261. package/dist/tag/tag.repository.d.ts.map +1 -0
  262. package/dist/tag/tag.repository.js +15 -0
  263. package/dist/tag/tag.repository.js.map +1 -0
  264. package/dist/tag-group/interfaces/tag-group-attr.interface.d.ts +13 -0
  265. package/dist/tag-group/interfaces/tag-group-attr.interface.d.ts.map +1 -0
  266. package/dist/tag-group/interfaces/tag-group-attr.interface.js +3 -0
  267. package/dist/tag-group/interfaces/tag-group-attr.interface.js.map +1 -0
  268. package/dist/tag-group/tag-group.d.ts +24 -0
  269. package/dist/tag-group/tag-group.d.ts.map +1 -0
  270. package/dist/tag-group/tag-group.js +82 -0
  271. package/dist/tag-group/tag-group.js.map +1 -0
  272. package/dist/tag-group/tag-group.repository.d.ts +6 -0
  273. package/dist/tag-group/tag-group.repository.d.ts.map +1 -0
  274. package/dist/tag-group/tag-group.repository.js +15 -0
  275. package/dist/tag-group/tag-group.repository.js.map +1 -0
  276. package/dist/tax/interfaces/tax-attr.interface.d.ts +1 -0
  277. package/dist/tax/interfaces/tax-attr.interface.d.ts.map +1 -0
  278. package/dist/tax/tax.d.ts +1 -0
  279. package/dist/tax/tax.d.ts.map +1 -0
  280. package/dist/tax/tax.repository.d.ts +1 -0
  281. package/dist/tax/tax.repository.d.ts.map +1 -0
  282. package/dist/tax/tax.repository.js +4 -1
  283. package/dist/tax/tax.repository.js.map +1 -1
  284. package/dist/test-document.d.ts +1 -0
  285. package/dist/test-document.d.ts.map +1 -0
  286. package/dist/test.d.ts +1 -0
  287. package/dist/test.d.ts.map +1 -0
  288. package/dist/tsconfig.tsbuildinfo +1 -1
  289. package/migrations/finance-document-tag-migration.js +46 -0
  290. package/migrations/finance-tag-group-migration.js +53 -0
  291. package/migrations/finance-tag-migration.js +63 -0
  292. package/package.json +18 -18
  293. package/.gitlab-ci.yml +0 -16
  294. package/.husky/commit-msg +0 -13
  295. package/.husky/pre-commit +0 -4
  296. package/.prettierrc +0 -5
  297. package/CONTRIBUTING.md +0 -31
  298. package/README.md +0 -13
  299. package/eslint.config.mjs +0 -58
  300. package/nest-cli.json +0 -19
  301. package/sonar-project.properties +0 -13
  302. package/src/account/account.repository.ts +0 -11
  303. package/src/account/account.ts +0 -307
  304. package/src/account/interfaces/account-attr.interface.ts +0 -31
  305. package/src/account-system-entity/account-system-entity.ts +0 -66
  306. package/src/account-system-entity/post-history.repository.ts +0 -11
  307. package/src/config.ts +0 -382
  308. package/src/customer/customer.ts +0 -311
  309. package/src/customer/finance-customer.repository.ts +0 -13
  310. package/src/customer/interfaces/customer.repository.interface.ts +0 -3
  311. package/src/customer/interfaces/finance-customer-attr.interface.ts +0 -10
  312. package/src/customer/interfaces/finance-customer.repository.interface.ts +0 -4
  313. package/src/database.ts +0 -48
  314. package/src/document/document-item.repository.ts +0 -11
  315. package/src/document/document-item.ts +0 -146
  316. package/src/document/document.repository.ts +0 -11
  317. package/src/document/document.ts +0 -1328
  318. package/src/document/interfaces/document-attr.interface.ts +0 -28
  319. package/src/document/interfaces/document-find-all.interface.ts +0 -14
  320. package/src/document/interfaces/document-item-attr.interface.ts +0 -26
  321. package/src/document/interfaces/document-item.repository.interface.ts +0 -4
  322. package/src/enum/collect-payment-type.ts +0 -4
  323. package/src/enum/doc-type.enum.ts +0 -8
  324. package/src/enum/document-status.enum.ts +0 -6
  325. package/src/enum/index.ts +0 -19
  326. package/src/enum/payment-method.enum.ts +0 -3
  327. package/src/enum/payment-status.enum.ts +0 -11
  328. package/src/enum/payment-type.enum.ts +0 -4
  329. package/src/enum/quick-book-client-scopes.enum.ts +0 -14
  330. package/src/enum/transaction-type.enum.ts +0 -4
  331. package/src/finance-company/finance-company.repository.ts +0 -11
  332. package/src/finance-company/finance-company.ts +0 -2434
  333. package/src/helpers/login-user.ts +0 -38
  334. package/src/helpers/typeof.ts +0 -35
  335. package/src/index.ts +0 -32
  336. package/src/interfaces/account-system.interface.ts +0 -51
  337. package/src/interfaces/index.ts +0 -3
  338. package/src/journal-entry/interfaces/journal-entry-attr.interface.ts +0 -12
  339. package/src/journal-entry/journal-entry.repository.ts +0 -17
  340. package/src/journal-entry/journal-entry.ts +0 -264
  341. package/src/ledger-transaction/interfaces/ledger-transaction-attr.interface.ts +0 -18
  342. package/src/ledger-transaction/interfaces/ledger-transaction.repository.interface.ts +0 -7
  343. package/src/ledger-transaction/ledger-transaction.repository.ts +0 -17
  344. package/src/ledger-transaction/ledger-transaction.ts +0 -182
  345. package/src/models/account.entity.ts +0 -206
  346. package/src/models/customer.entity.ts +0 -93
  347. package/src/models/document-item.entity.ts +0 -167
  348. package/src/models/document.entity.ts +0 -211
  349. package/src/models/finance-company.entity.ts +0 -69
  350. package/src/models/journal-entry.entity.ts +0 -110
  351. package/src/models/ledger-transaction.entity.ts +0 -148
  352. package/src/models/payment-item.entity.ts +0 -60
  353. package/src/models/payment-method-type.entity.ts +0 -70
  354. package/src/models/payment-method.entity.ts +0 -51
  355. package/src/models/payment-paid-with.entity.ts +0 -133
  356. package/src/models/payment.entity.ts +0 -165
  357. package/src/models/post-history.entity.ts +0 -41
  358. package/src/models/tax.entity.ts +0 -75
  359. package/src/payment/interfaces/payment-attr.interface.ts +0 -23
  360. package/src/payment/interfaces/payment-params.interface.ts +0 -8
  361. package/src/payment/payment.repository.ts +0 -11
  362. package/src/payment/payment.ts +0 -236
  363. package/src/payment-item/interfaces/payment-item-attr.interface.ts +0 -10
  364. package/src/payment-item/payment-item.repository.ts +0 -11
  365. package/src/payment-item/payment-item.ts +0 -134
  366. package/src/payment-method/interfaces/payment-method-attr.interface.ts +0 -5
  367. package/src/payment-method/payment-method.repository.ts +0 -11
  368. package/src/payment-method/payment-method.ts +0 -146
  369. package/src/payment-method-type/interfaces/payment-method-type-attr.interface.ts +0 -8
  370. package/src/payment-method-type/payment-method-type.repository.ts +0 -11
  371. package/src/payment-method-type/payment-method-type.ts +0 -79
  372. package/src/payment-paid-with/interfaces/payment-paid-with.interface.ts +0 -20
  373. package/src/payment-paid-with/payment-paid-with.repository.ts +0 -11
  374. package/src/payment-paid-with/payment-paid-with.ts +0 -74
  375. package/src/tax/interfaces/tax-attr.interface.ts +0 -10
  376. package/src/tax/tax.repository.ts +0 -11
  377. package/src/tax/tax.ts +0 -68
  378. package/src/test-document.ts +0 -25
  379. package/src/test.ts +0 -3
  380. package/tsconfig.build.json +0 -4
  381. package/tsconfig.json +0 -23
@@ -1,2434 +0,0 @@
1
- import axios from 'axios';
2
- import {
3
- ClassError,
4
- HashTable,
5
- LoginUserBase,
6
- ObjectBase,
7
- RecordNotFoundError,
8
- } from '@tomei/general';
9
- import Account from '../account/account';
10
- import JournalEntry from '../journal-entry/journal-entry';
11
- import FinanceCustomerBase from '../customer/customer';
12
- import Document from '../document/document';
13
- import { IAccountSystem } from '../interfaces';
14
- import { FinanceCompanyRepository } from './finance-company.repository';
15
- import { FinanceCustomerRepository } from '../customer/finance-customer.repository';
16
- import { LedgerTransactionRepository } from '../ledger-transaction/ledger-transaction.repository';
17
- import {
18
- DocType,
19
- DocumentStatus,
20
- PaymentStatus,
21
- PaymentType,
22
- TransactionTypeOptions,
23
- } from '../enum';
24
- import PaymentMethodType from '../payment-method-type/payment-method-type';
25
- import { PaymentRepository } from '../payment/payment.repository';
26
- import { PaymentItemRepository } from '../payment-item/payment-item.repository';
27
- import Payment from '../payment/payment';
28
- import { DocumentRepository } from '../document/document.repository';
29
- import { DocumentItemRepository } from '../document/document-item.repository';
30
- import { PaymentMethodRepository } from '../payment-method/payment-method.repository';
31
- import { PaymentMethodTypeRepository } from '../payment-method-type/payment-method-type.repository';
32
- import PaymentMethod from '../payment-method/payment-method';
33
- import { AccountRepository } from '../account/account.repository';
34
- import { PaymentPaidWithRepository } from '../payment-paid-with/payment-paid-with.repository';
35
- import { MediasModel } from '@tomei/media';
36
- import DocumentItem from '../document/document-item';
37
- import { type } from '../helpers/typeof';
38
- import { LoginUser } from '@tomei/sso';
39
- import { ActionEnum, Activity } from '@tomei/activity-history';
40
- import { ApplicationConfig, ComponentConfig } from '@tomei/config';
41
- import { TaxRepository } from '../tax/tax.repository';
42
- import { Tax } from '../tax/tax';
43
- import { CollectPaymentType } from '../enum/collect-payment-type';
44
- import FinanceCustomerModel from 'src/models/customer.entity';
45
-
46
- export default class FinanceCompany extends ObjectBase {
47
- private _CompanyId = 'New';
48
- private _CompSystemCode = '';
49
- private _CompSystemRefId = '';
50
- private _AccSystemCode = 'REF';
51
- private _AccSystemRefId = 'REF';
52
- private _PostedToAccSystemYN = 'N';
53
- private _PostedById = null;
54
- private _PostedDateTime = null;
55
- private _ObjectType = 'FinanceCompany';
56
-
57
- private static _htFinanceCompanyIds = new HashTable();
58
- private static _htFinanceCompanies = new HashTable();
59
- private static _financeCompanyRepository = new FinanceCompanyRepository();
60
- private static _PaymentRepository = new PaymentRepository();
61
- private static _PaymentItemRepository = new PaymentItemRepository();
62
- private static _PaymentPaidWithRepository = new PaymentPaidWithRepository();
63
- private static _PaymentMethodRepository = new PaymentMethodRepository();
64
- private static _PaymentMethodTypeRepository =
65
- new PaymentMethodTypeRepository();
66
- private static _DocumentRepository = new DocumentRepository();
67
- private static _DocumentItemRepository = new DocumentItemRepository();
68
- private static _FinanceCustomerRepository = new FinanceCustomerRepository();
69
- private static _LedgerTransactionRepository =
70
- new LedgerTransactionRepository();
71
-
72
- private static _AccountRepository = new AccountRepository();
73
- private static _TaxRepository = new TaxRepository();
74
-
75
- private _AccountingSystem: IAccountSystem;
76
-
77
- private _DbTransaction: any;
78
- private _PaymentMethods = [];
79
- private _Taxes: Tax[] = [];
80
-
81
- get ObjectType() {
82
- return this._ObjectType;
83
- }
84
-
85
- get CompSystemCode(): string {
86
- return this._CompSystemCode;
87
- }
88
-
89
- private set CompSystemCode(code: string) {
90
- this._CompSystemCode = code;
91
- }
92
-
93
- get CompSystemRefId() {
94
- return this._CompSystemRefId;
95
- }
96
-
97
- set CompSystemRefId(id: string) {
98
- this._CompSystemRefId = id;
99
- }
100
-
101
- get AccSystemCode() {
102
- return this._AccSystemCode;
103
- }
104
-
105
- get AccSystemRefId() {
106
- return this._AccSystemRefId;
107
- }
108
-
109
- set AccSystemRefId(id: string) {
110
- this._AccSystemRefId = id;
111
- }
112
-
113
- get PostedToAccSystemYN() {
114
- return this._PostedToAccSystemYN;
115
- }
116
-
117
- set PostedToAccSystemYN(value: string) {
118
- this._PostedToAccSystemYN = value;
119
- }
120
-
121
- get PostedById() {
122
- return this._PostedById;
123
- }
124
-
125
- set PostedById(id: string) {
126
- this._PostedById = id;
127
- }
128
-
129
- get PostedDateTime() {
130
- return this._PostedDateTime;
131
- }
132
-
133
- set PostedDateTime(date: any) {
134
- this._PostedDateTime = date;
135
- }
136
-
137
- private set AccSystemCode(code: string) {
138
- this._AccSystemCode = code;
139
- }
140
-
141
- get CompanyId() {
142
- return this._CompanyId;
143
- }
144
-
145
- get ObjectId() {
146
- return this._CompanyId;
147
- }
148
-
149
- private set ObjectId(id: string) {
150
- this._CompanyId = id;
151
- }
152
-
153
- get ObjectName() {
154
- return `${this.CompSystemCode}-${this.CompSystemRefId}-${this.AccSystemCode}`;
155
- }
156
-
157
- get TableName() {
158
- return 'finance_Company';
159
- }
160
-
161
- get AccountingSystem(): IAccountSystem {
162
- return this._AccountingSystem;
163
- }
164
-
165
- set AccountingSystem(system: IAccountSystem) {
166
- this._AccountingSystem = system;
167
- }
168
-
169
- constructor(
170
- compSystemCode: string,
171
- compSystemRefId: string,
172
- accSystemCode: string,
173
- ) {
174
- super();
175
- this.CompSystemCode = compSystemCode;
176
- this.CompSystemRefId = compSystemRefId;
177
- this.AccSystemCode = accSystemCode || 'REF';
178
-
179
- this.AccSystemRefId = 'REF';
180
- this.PostedToAccSystemYN = 'N';
181
- }
182
-
183
- static async getFinanceCompanyId(
184
- compSystemCode: string,
185
- compSystemRefId: string,
186
- accSystemCode: string,
187
- ): Promise<string> {
188
- let sCompanyId = '';
189
- /*Assemble the hashtable key*/
190
- const sKey = `${compSystemCode}-${compSystemRefId}-${accSystemCode}`;
191
- /*Check if the FinanceCompany has previously being loaded*/
192
- if (!FinanceCompany._htFinanceCompanyIds.get(sKey)) {
193
- /*Instantiate a new FinanceCompany*/
194
- const financeCompany = new FinanceCompany(
195
- compSystemCode,
196
- compSystemRefId,
197
- accSystemCode,
198
- );
199
-
200
- /*Retrieve the finance company from finance_Company table using compSystemCode,
201
- * CompSystemRefId and accSystemCode */
202
- const company = await FinanceCompany._financeCompanyRepository.findOne({
203
- where: {
204
- CompSystemCode: compSystemCode,
205
- CompSystemRefId: compSystemRefId,
206
- AccSystemCode: accSystemCode,
207
- },
208
- });
209
-
210
- /*Retrieve and store the companyId from the result*/
211
- financeCompany.ObjectId = company.CompanyId;
212
- sCompanyId = financeCompany.ObjectId;
213
-
214
- /*Add the details into the hashtable*/
215
- FinanceCompany._htFinanceCompanyIds.add(sKey, financeCompany.ObjectId);
216
- FinanceCompany._htFinanceCompanies.add(sCompanyId, financeCompany);
217
- }
218
-
219
- if (typeof FinanceCompany._htFinanceCompanyIds.get(sKey) === 'string') {
220
- sCompanyId = FinanceCompany._htFinanceCompanyIds.get(sKey);
221
- }
222
-
223
- return sCompanyId;
224
- }
225
-
226
- static async getFinanceCompany(companyId: string): Promise<FinanceCompany> {
227
- /*Check if the finance company is previously be loaded*/
228
- if (!FinanceCompany._htFinanceCompanies.get(companyId)) {
229
- /*Retrieve the finance company from finance_Company table using compSystemCode,
230
- * CompSystemRefId and accSystemCode */
231
- const company = await FinanceCompany._financeCompanyRepository.findOne({
232
- where: { CompanyId: companyId },
233
- });
234
-
235
- if (!company) {
236
- throw Error('No finance company found. Please create first.');
237
- }
238
-
239
- /*Using the result returned, instantiate a new FinanceCompany*/
240
- const compSystemCode = company.CompSystemCode;
241
- const compSystemRefId = company.CompSystemRefId;
242
- const accSystemCode = company.AccSystemCode;
243
-
244
- const financeCompany = new FinanceCompany(
245
- compSystemCode,
246
- compSystemRefId,
247
- accSystemCode,
248
- );
249
-
250
- /*Retrieve and store the CompanyId from the result*/
251
- financeCompany.ObjectId = company.CompanyId;
252
- financeCompany._CompanyId = company.CompanyId;
253
-
254
- /*Add the details into the hashtable*/
255
- const sKey = `${compSystemCode}-${compSystemRefId}-${accSystemCode}`;
256
- FinanceCompany._htFinanceCompanyIds.add(sKey, financeCompany.ObjectId);
257
- FinanceCompany._htFinanceCompanies.add(
258
- financeCompany.ObjectId,
259
- financeCompany,
260
- );
261
- }
262
- // tslint:disable-next-line:no-console
263
- console.log('return from hash table');
264
- return FinanceCompany._htFinanceCompanies.get(companyId);
265
- }
266
-
267
- static async createFinanceCompany(
268
- dbTransaction: any,
269
- loginUser: LoginUserBase,
270
- companyId: string,
271
- compSystemCode: string,
272
- compSystemRefId: string,
273
- accSystemCode: string,
274
- accSystemRefId?: string,
275
- postedToAccSystemYN?: string,
276
- ) {
277
- /*Instantiate a new FinanceCompany*/
278
- const financeCompany = new FinanceCompany(
279
- compSystemCode,
280
- compSystemRefId,
281
- accSystemCode,
282
- );
283
-
284
- /*Validating if the finance company already exists in finance_Company*/
285
- const company = await FinanceCompany._financeCompanyRepository.findOne({
286
- where: {
287
- CompSystemCode: compSystemCode,
288
- CompSystemRefId: compSystemRefId,
289
- AccSystemCode: accSystemCode,
290
- },
291
- });
292
-
293
- if (company) {
294
- throw Error(
295
- 'There is already another Finance Company with the compSystemCode, CompSystemRefId and accSystemCode specified.',
296
- );
297
- }
298
-
299
- /*Generating the companyId*/
300
- financeCompany.ObjectId = companyId;
301
- financeCompany.PostedById = loginUser?.ObjectId || null;
302
- financeCompany.PostedDateTime = new Date();
303
-
304
- if (postedToAccSystemYN === 'Y' && !financeCompany.PostedById) {
305
- throw Error(
306
- 'Cannot post to the accounting system because no user is specified.',
307
- );
308
- }
309
-
310
- /*Save the FinanceCompany to the finance_Company table*/
311
- await FinanceCompany._financeCompanyRepository.create(
312
- {
313
- CompanyId: financeCompany.CompanyId,
314
- CompSystemCode: financeCompany.CompSystemCode,
315
- CompSystemRefId: financeCompany.CompSystemRefId,
316
- AccSystemCode: financeCompany.AccSystemCode,
317
- AccSystemRefId:
318
- postedToAccSystemYN === 'Y'
319
- ? accSystemRefId
320
- : financeCompany.AccSystemRefId,
321
- PostedToAccSystemYN:
322
- postedToAccSystemYN || financeCompany.PostedToAccSystemYN,
323
- PostedById:
324
- postedToAccSystemYN === 'Y' ? financeCompany.PostedById : null,
325
- PostedDateTime:
326
- postedToAccSystemYN === 'Y' ? financeCompany.PostedDateTime : null,
327
- },
328
- {
329
- transaction: dbTransaction,
330
- },
331
- );
332
-
333
- /*Add the details to hashtable*/
334
- const sKey = `${compSystemCode}-${compSystemRefId}-${accSystemCode}`;
335
- FinanceCompany._htFinanceCompanyIds.add(sKey, financeCompany.ObjectId);
336
- FinanceCompany._htFinanceCompanies.add(
337
- financeCompany.ObjectId,
338
- financeCompany,
339
- );
340
-
341
- // tslint:disable-next-line:no-console
342
- console.log('return from hash table');
343
- return FinanceCompany._htFinanceCompanies.get(companyId);
344
- }
345
-
346
- static async findAccount(
347
- loginUser: LoginUserBase,
348
- dbTransaction: any,
349
- accountNo: string,
350
- ): Promise<Account> {
351
- try {
352
- //Part 1: Privilege Check
353
- //Check if the user has the privilege to view the account
354
- const systemCode =
355
- ApplicationConfig.getComponentConfigValue('system-code');
356
- const isPrivileged = await loginUser.checkPrivileges(
357
- systemCode,
358
- 'FINANCECOMPANY_VIEW_ACCOUNT',
359
- );
360
-
361
- //If the user is not privileged, throw an error
362
- if (!isPrivileged) {
363
- throw new ClassError(
364
- 'FinanceCompany',
365
- 'findAccountErrMsg0X',
366
- 'User not privileged to view finance company account',
367
- );
368
- }
369
-
370
- //Part 2: Find Account
371
- //Find the account using the accountNo
372
- const record = await FinanceCompany._AccountRepository.findOne({
373
- where: {
374
- AccountNo: accountNo,
375
- },
376
- transaction: dbTransaction,
377
- });
378
-
379
- if (record) {
380
- const account = new Account(dbTransaction);
381
- account.init(record.get({ plain: true }));
382
- return account;
383
- }
384
- return undefined;
385
- } catch (error) {
386
- throw error;
387
- }
388
- }
389
-
390
- async createCustomer<T extends FinanceCustomerBase>(
391
- dbTransaction: any,
392
- custSystemCode: string,
393
- custSystemRefId: string,
394
- customer: T,
395
- loginUser: LoginUser,
396
- ) {
397
- try {
398
- if (!custSystemCode || !custSystemRefId) {
399
- throw new Error(
400
- 'CustSystemCode and CustomerRefId are required fields.',
401
- );
402
- }
403
-
404
- const financeCustomerData =
405
- await FinanceCompany._FinanceCustomerRepository.findOne({
406
- where: {
407
- CompanyId: this._CompanyId,
408
- CustSystemCode: custSystemCode,
409
- CustSystemRefId: custSystemRefId,
410
- },
411
- });
412
-
413
- if (financeCustomerData) {
414
- throw new Error('Customer already created previously.');
415
- }
416
-
417
- let AccCustomerRefId = 'REF';
418
- if (this.AccountingSystem) {
419
- AccCustomerRefId = await this.AccountingSystem.createCustomer(
420
- customer,
421
- dbTransaction,
422
- );
423
- }
424
-
425
- customer.CompanyId = this._CompanyId;
426
-
427
- const newCustomer = await customer.save(
428
- AccCustomerRefId,
429
- custSystemCode,
430
- custSystemRefId,
431
- dbTransaction,
432
- );
433
-
434
- const activity = new Activity();
435
- activity.ActivityId = this._createId().toUpperCase();
436
- activity.Action = ActionEnum.CREATE;
437
- activity.Description = 'Add Finance Customer';
438
- activity.EntityType = 'FinanceCustomer';
439
- activity.EntityId = newCustomer.CustomerId;
440
- activity.EntityValueBefore = JSON.stringify({});
441
- activity.EntityValueAfter = JSON.stringify(newCustomer);
442
-
443
- await activity.create(loginUser.ObjectId, dbTransaction);
444
-
445
- return customer;
446
- } catch (error) {
447
- throw error;
448
- }
449
- }
450
-
451
- async postJournal(
452
- dbTransaction: any,
453
- journalEntry: JournalEntry,
454
- loginUser: LoginUserBase,
455
- ) {
456
- const debitTransactions = await journalEntry.DebitTransactions;
457
- const creditTransactions = await journalEntry.CreditTransactions;
458
- try {
459
- if (creditTransactions.length < 1 || debitTransactions?.length < 1) {
460
- throw new Error(
461
- 'There should be at least 1 debit ledger transaction and 1 credit ledger transaction in the journal entry',
462
- );
463
- }
464
-
465
- let totalCreditAmount = creditTransactions.reduce(
466
- (accumulator, currentValue) => accumulator + currentValue.CreditAmount,
467
- 0,
468
- );
469
- let totalDebitAmount = debitTransactions.reduce(
470
- (accumulator, currentValue) => accumulator + currentValue.DebitAmount,
471
- 0,
472
- );
473
- console.log('totalCreditAmount: ', totalCreditAmount);
474
- console.log('totalDebitAmount: ', totalDebitAmount);
475
-
476
- if (typeof totalCreditAmount === 'string') {
477
- totalCreditAmount = parseFloat(totalCreditAmount);
478
- }
479
-
480
- if (typeof totalDebitAmount === 'string') {
481
- totalDebitAmount = parseFloat(totalDebitAmount);
482
- }
483
- if (totalCreditAmount.toFixed(2) !== totalDebitAmount.toFixed(2)) {
484
- throw new Error(
485
- 'Credit ledger transaction and debit ledger transaction should the same amount',
486
- );
487
- }
488
-
489
- const newJournalEntry = await journalEntry.save(
490
- loginUser.ObjectId,
491
- dbTransaction,
492
- );
493
-
494
- for (const ledgerTransaction of debitTransactions) {
495
- ledgerTransaction.JournalEntryId = newJournalEntry.JournalEntryId;
496
-
497
- await ledgerTransaction.save(dbTransaction);
498
- // const dt = await ledgerTransaction.newLedgerTransaction(
499
- // TransactionTypeOptions.DEBIT,
500
- // newJournalEntry.JournalEntryId,
501
- // );
502
- // await dt.save();
503
- }
504
-
505
- for (const ledgerTransaction of creditTransactions) {
506
- ledgerTransaction.JournalEntryId = newJournalEntry.JournalEntryId;
507
-
508
- await ledgerTransaction.save(dbTransaction);
509
- // const ct = await ledgerTransaction.newLedgerTransaction(
510
- // TransactionTypeOptions.CREDIT,
511
- // newJournalEntry.JournalEntryId,
512
- // );
513
- // await ct.save();
514
- }
515
-
516
- // await this.AccountingSystem.postJournalEntry(newJournalEntry);
517
-
518
- const payload = {
519
- Action: 'Create',
520
- Activity: 'Post Journal Entry',
521
- Description: `Journal Entry (ID: ${newJournalEntry.JournalEntryId}) has been created`,
522
- EntityType: 'JournalEntry',
523
- EntityValueBefore: JSON.stringify({}),
524
- EntityValueAfter: JSON.stringify(newJournalEntry),
525
- PerformedById: loginUser.ObjectId,
526
- PerformedAt: new Date(),
527
- EntityId: newJournalEntry.JournalEntryId,
528
- };
529
-
530
- await axios.post(
531
- `${process.env.COMMON_API_URL}/activity-histories`,
532
- payload,
533
- );
534
- } catch (error) {
535
- throw error;
536
- }
537
- }
538
-
539
- async createAccount(
540
- dbTransaction: any,
541
- account: Account,
542
- loginUser: LoginUserBase,
543
- ) {
544
- try {
545
- let accSystemRefId: string;
546
-
547
- if (!account.AccountType) {
548
- throw new Error('AccountType is required.');
549
- }
550
-
551
- // Retrieve & validate accountNoLength from finance config file
552
- const accountNoLength = ComponentConfig.getComponentConfigValue(
553
- '@tomei/finance',
554
- 'accountNoLength',
555
- );
556
-
557
- if (accountNoLength && account.AccountNo.length > accountNoLength) {
558
- throw new Error(
559
- `Account No length should not exceed ${accountNoLength} characters.`,
560
- );
561
- }
562
-
563
- let createAccountPayload: any = {
564
- Name: account.Name,
565
- AcctNum: account.AccountNo,
566
- AccountType: account.AccountType,
567
- AccountSubType: account.AccountSubtype,
568
- };
569
-
570
- if (this.AccountingSystem) {
571
- accSystemRefId = await this.AccountingSystem.createAccount(
572
- account,
573
- dbTransaction,
574
- );
575
- }
576
-
577
- if (account.isParentAccountExists()) {
578
- createAccountPayload = {
579
- ...createAccountPayload,
580
- CurrencyRef: 'MYR',
581
- ParentRef: account.ParentAccountNo,
582
- SubAccount: true,
583
- };
584
- }
585
-
586
- console.log(
587
- 'Finance Company Create Account: Before accSystemAccountId Create',
588
- );
589
-
590
- console.log(
591
- 'Finance Company Create Account: After accSystemAccountId Create',
592
- );
593
-
594
- console.log('Finance Company Create Account: Before new Account Create');
595
-
596
- if (account.AccSystemRefId) {
597
- account.PostedToAccSystemYN = 'Y';
598
- account.PostedById = loginUser.ObjectId;
599
- account.PostedDateTime = new Date();
600
- }
601
-
602
- const newAccount = await account.save(
603
- this.CompanyId,
604
- account.AccSystemRefId || 'REF',
605
- loginUser.ObjectId,
606
- dbTransaction,
607
- );
608
-
609
- console.log('Finance Company Create Account: After new Account Create');
610
-
611
- const payload = {
612
- Action: 'Create',
613
- Activity: 'Account Created',
614
- Description: `Account (ID: ${newAccount.AccountNo}) has been created`,
615
- EntityType: 'Account',
616
- EntityValueBefore: JSON.stringify({}),
617
- EntityValueAfter: JSON.stringify(newAccount),
618
- PerformedById: loginUser.ObjectId,
619
- PerformedAt: new Date(),
620
- EntityId: newAccount.AccountNo,
621
- };
622
-
623
- await axios.post(
624
- `${process.env.COMMON_API_URL}/activity-histories`,
625
- payload,
626
- );
627
-
628
- return newAccount;
629
- } catch (error) {
630
- throw error;
631
- }
632
- }
633
-
634
- /**
635
- * Issue an invoice
636
- *
637
- * @param dbTransaction - The database transaction to be used
638
- * @param loginUser - The user issuing the invoice
639
- * @param invoice - The document containing the invoice details
640
- * @param customer - The customer to be issued the invoice
641
- * @param dtAccountNo - The account number of the Account Receivable (AR) account to debit. If not provided, will debit the customer's default AR account
642
- *
643
- * @returns {Document} - Document object representing the full details of the invoice issued
644
- */
645
- async issueInvoice<C extends FinanceCustomerBase>(
646
- /*todo: loginUser & customer is NOT supposed to be optional */
647
- dbTransaction: any,
648
- invoice: Document,
649
- loginUser?: LoginUserBase,
650
- customer?: C,
651
- dtAccountNo?: string,
652
- ): Promise<Document> {
653
- try {
654
- /*Check if the invoice number already exists (should be unique)*/
655
- const duplicateInvoice = await FinanceCompany._DocumentRepository.findOne(
656
- {
657
- where: {
658
- DocNo: invoice.DocNo,
659
- },
660
- transaction: dbTransaction,
661
- },
662
- );
663
-
664
- if (duplicateInvoice) {
665
- throw new Error('Invoice number already exists');
666
- }
667
-
668
- const documentItems = await invoice.getDocumentItems(dbTransaction);
669
-
670
- /*Check if the document has at least 1 document item*/
671
- if (!documentItems.length) {
672
- throw new Error('Document must have at least 1 document item');
673
- }
674
-
675
- /*Check if each document item has CtAccountNo provided*/
676
- for (const invoiceItem of documentItems) {
677
- if (!invoiceItem.CtAccountNo) {
678
- throw new Error(
679
- 'Each document item should have CtAccountNo provided',
680
- );
681
- }
682
- }
683
-
684
- /*Set up the document type*/
685
- invoice.DocType = DocType.INVOICE;
686
-
687
- /*Generating the invoice*/
688
- let invoiceMedia: {
689
- HTMLMedia: MediasModel;
690
- PDFMedia: MediasModel;
691
- };
692
-
693
- if (invoice.UseAccSystemDocYN === 'Y') {
694
- /*todo: Posting to accounting system to generate invoice*/
695
- const accInvoiceRefIdDetail = await this.AccountingSystem.createInvoice(
696
- {
697
- customer,
698
- invoice,
699
- paymentMode: 'credit',
700
- },
701
- dbTransaction,
702
- );
703
- invoice.DocNo = accInvoiceRefIdDetail.invoiceNo;
704
- invoice.AccSystemRefId = accInvoiceRefIdDetail.invoiceRefId;
705
- invoice.PostedToAccSystemYN = 'Y';
706
- invoice.PostedById = loginUser.ObjectId;
707
- invoice.PostedDateTime = new Date();
708
-
709
- //for each document item, insert the document item accounting reference id that have matching name
710
- for (const documentItem of documentItems) {
711
- const accItem = accInvoiceRefIdDetail.invoiceItems.find(
712
- (item) => item.name === documentItem.Name,
713
- );
714
- if (accItem) {
715
- documentItem.DocNo = accInvoiceRefIdDetail.invoiceNo;
716
- documentItem.AccSystemRefId = accItem.itemRefId;
717
- documentItem.PostedToAccSystemYN = 'Y';
718
- documentItem.PostedById = loginUser.ObjectId;
719
- documentItem.PostedDateTime = new Date();
720
- }
721
- }
722
- } else {
723
- /*todo: check config file to see which invoice template is to be used for specific project*/
724
-
725
- /*Generating invoice based on template*/
726
- invoiceMedia = await invoice.generateInvoice(
727
- invoice.IssuedById,
728
- customer,
729
- dbTransaction,
730
- );
731
- }
732
-
733
- /*Saving the document and document items to the database*/
734
- await FinanceCompany._DocumentRepository.create(
735
- {
736
- DocNo: invoice.DocNo,
737
- DocType: invoice.DocType,
738
- DocDate: invoice.DocDate,
739
- CompanyId: invoice.CompanyId,
740
- Currency: invoice.Currency,
741
- Amount: invoice.Amount,
742
- Description: invoice.Description,
743
- Status: invoice.Status,
744
- IssuedById: invoice.IssuedById,
745
- IssuedToId: invoice.IssuedToId,
746
- IssuedToType: invoice.IssuedToType,
747
- RelatedObjectId: invoice.RelatedObjectId,
748
- RelatedObjectType: invoice.RelatedObjectType,
749
- CreatedById: invoice.CreatedById,
750
- CreatedAt: new Date(),
751
- UpdatedById: invoice.UpdatedById,
752
- UpdatedAt: new Date(),
753
- DocPDFFileMediaId:
754
- invoice.UseAccSystemDocYN == 'N'
755
- ? invoiceMedia.PDFMedia.MediaId
756
- : null,
757
- DocHTMLFileMediaId:
758
- invoice.UseAccSystemDocYN == 'N'
759
- ? invoiceMedia.HTMLMedia.MediaId
760
- : null,
761
- AccSystemRefId: invoice.AccSystemRefId,
762
- PostedToAccSystemYN: invoice.PostedToAccSystemYN,
763
- PostedById:
764
- invoice.UseAccSystemDocYN == 'Y' ? invoice.PostedById : null,
765
- PostedDateTime: new Date(),
766
- UseAccSystemDocYN: invoice.UseAccSystemDocYN,
767
- },
768
- {
769
- transaction: dbTransaction,
770
- },
771
- );
772
-
773
- for (const documentItem of documentItems) {
774
- await FinanceCompany._DocumentItemRepository.create(
775
- {
776
- DocumentItemId: this._createId().toUpperCase(),
777
- DocNo: invoice.DocNo,
778
- Name: documentItem.Name,
779
- NameBM: documentItem.NameBM,
780
- Description: documentItem.Description,
781
- ItemId: documentItem.ItemId,
782
- ItemType: documentItem.ItemType,
783
- ItemSKU: documentItem.ItemSKU,
784
- ItemSerialNo: documentItem.ItemSerialNo,
785
- Currency: documentItem.Currency,
786
- UnitPrice: documentItem.UnitPrice,
787
- Quantity: documentItem.Quantity,
788
- QuantityUOM: documentItem.QuantityUOM,
789
- Amount: documentItem.Amount,
790
- TaxCode: documentItem.TaxCode,
791
- TaxAmount: documentItem.TaxAmount,
792
- TaxRate: documentItem.TaxRate,
793
- TaxInclusiveYN: documentItem.TaxInclusiveYN,
794
- DtAccountNo: documentItem.DtAccountNo
795
- ? documentItem.DtAccountNo
796
- : null,
797
- CtAccountNo: documentItem.CtAccountNo
798
- ? documentItem.CtAccountNo
799
- : null,
800
- AccSystemRefId: documentItem.AccSystemRefId,
801
- PostedToAccSystemYN: documentItem.PostedToAccSystemYN,
802
- PostedById: documentItem.PostedById,
803
- PostedDateTime: documentItem.PostedDateTime,
804
- },
805
- {
806
- transaction: dbTransaction,
807
- },
808
- );
809
- }
810
-
811
- const transactionDate = new Date();
812
- const htCreditAccountAmount = new HashTable();
813
- const htCreditAccountCurrency = new HashTable();
814
- const htCreditAccountPurpose = new HashTable();
815
-
816
- documentItems.forEach((invoiceItem) => {
817
- if (!htCreditAccountAmount.exists(invoiceItem.CtAccountNo)) {
818
- //add the credit account to the hash table
819
- htCreditAccountAmount.add(
820
- invoiceItem.CtAccountNo,
821
- invoiceItem.Amount,
822
- );
823
-
824
- htCreditAccountCurrency.add(
825
- invoiceItem.CtAccountNo,
826
- invoiceItem.Currency,
827
- );
828
-
829
- htCreditAccountPurpose.add(invoiceItem.CtAccountNo, invoiceItem.Name);
830
- } else {
831
- //update the credit account amount
832
- const d = htCreditAccountAmount.get(invoiceItem.CtAccountNo);
833
- htCreditAccountAmount.add(
834
- invoiceItem.CtAccountNo,
835
- d + invoiceItem.Amount,
836
- );
837
- }
838
- });
839
-
840
- const savedItems = htCreditAccountAmount.list();
841
-
842
- for (const item of savedItems) {
843
- const journalEntry = new JournalEntry(dbTransaction);
844
- //Temporary fix to successfully save journal entry in PostJournal
845
- journalEntry.init({
846
- CompanyId: this.CompanyId,
847
- Name: 'Issue Invoice ' + invoice.DocNo,
848
- });
849
- // const account = await Account.initAccount(dbTransaction, item.value[0]);
850
- const creditAmount = item.value[1];
851
- const currency = htCreditAccountCurrency.get(item.value[0]);
852
- const purpose = htCreditAccountPurpose.get(item.value[0]);
853
-
854
- const dt = await journalEntry.newLedgerTransaction(
855
- TransactionTypeOptions.DEBIT,
856
- );
857
-
858
- if (dtAccountNo) {
859
- /*Transacting using AR account provided*/
860
- dt.AccountNo = dtAccountNo;
861
- } else {
862
- /*Transacting based on default customer AR account*/
863
- const arAccount = await customer.getAccountReceivable();
864
- dt.AccountNo = arAccount.AccountNo;
865
- }
866
-
867
- dt.Currency = currency ? currency : 'MYR';
868
- dt.DebitAmount = creditAmount ? creditAmount : 0.0;
869
- dt.Date = transactionDate;
870
- dt.Description = `${purpose}`;
871
- dt.Name = `${purpose}`;
872
- dt.RelatedDocNo = invoice.DocNo;
873
- dt.RelatedObjectId = invoice.RelatedObjectId;
874
- dt.RelatedObjectType = invoice.RelatedObjectType;
875
-
876
- const ct = await journalEntry.newLedgerTransaction(
877
- TransactionTypeOptions.CREDIT,
878
- );
879
- ct.AccountNo = item.value[0];
880
- ct.Currency = currency ? currency : 'MYR';
881
- ct.CreditAmount = creditAmount ? creditAmount : 0.0;
882
- ct.Date = transactionDate;
883
- ct.Description = customer.FullName;
884
- ct.Name = customer.FullName;
885
- ct.RelatedDocNo = invoice.DocNo;
886
- ct.RelatedObjectId = invoice.RelatedObjectId;
887
- ct.RelatedObjectType = invoice.RelatedObjectType;
888
-
889
- await this.postJournal(dbTransaction, journalEntry, loginUser);
890
- }
891
-
892
- return invoice;
893
- } catch (err) {
894
- // tslint:disable-next-line:no-console
895
- console.log('Issue invoice err: ', err);
896
- throw err;
897
- }
898
- }
899
-
900
- static async findCustomer(
901
- custSystemRefId: string,
902
- dbTransaction?: any,
903
- ): Promise<FinanceCustomerModel> {
904
- const data = await FinanceCompany._FinanceCustomerRepository.findOne({
905
- where: {
906
- CustSystemRefId: custSystemRefId,
907
- },
908
- transaction: dbTransaction,
909
- });
910
-
911
- return data;
912
- }
913
-
914
- /**
915
- * Issue a debit note
916
- *
917
- * @param dbTransaction - The database transaction to be used
918
- * @param loginUser - The user issuing the invoice
919
- * @param invoice - The document containing the invoice details
920
- * @param customer - The customer to be issued the invoice
921
- * @param dtAccountNo - The account number of the Account Receivable (AR) account to debit. If not provided, will debit the customer's default AR account
922
- *
923
- * @returns {Document} - Document object representing the full details of the invoice issued
924
- */
925
- async issueDebitNote(
926
- dbTransaction: any,
927
- loginUser: LoginUserBase,
928
- invoice: Document,
929
- customer: FinanceCustomerBase,
930
- dtAccountNo?: string,
931
- ): Promise<Document> {
932
- try {
933
- /*Check if the invoice number already exists (should be unique)*/
934
- const duplicateInvoice = await FinanceCompany._DocumentRepository.findOne(
935
- {
936
- where: {
937
- DocNo: invoice.DocNo,
938
- },
939
- transaction: dbTransaction,
940
- },
941
- );
942
-
943
- if (duplicateInvoice) {
944
- throw new Error('Invoice number already exists');
945
- }
946
-
947
- const documentItems = await invoice.getDocumentItems(dbTransaction);
948
-
949
- /*Check if the document has at least 1 document item*/
950
- if (!documentItems.length) {
951
- throw new Error('Document must have at least 1 document item');
952
- }
953
-
954
- /*Check if each document item has CtAccountNo provided*/
955
- for (const invoiceItem of documentItems) {
956
- if (!invoiceItem.CtAccountNo) {
957
- throw new Error(
958
- 'Each document item should have CtAccountNo provided',
959
- );
960
- }
961
- }
962
-
963
- /*Set up the document type*/
964
- invoice.DocType = DocType.DEBIT_NOTE;
965
- // invoice.DocNo = this._createId().toUpperCase();
966
-
967
- /*Generating the invoice*/
968
- if (invoice.UseAccSystemDocYN === 'Y') {
969
- /*todo: Posting to accounting system to generate invoice*/
970
- const accInvoiceRefIDetail = await this.AccountingSystem.createInvoice(
971
- {
972
- customer,
973
- invoice,
974
- paymentMode: 'credit',
975
- },
976
- dbTransaction,
977
- );
978
- invoice.AccSystemRefId = accInvoiceRefIDetail.invoiceRefId;
979
- invoice.PostedToAccSystemYN = 'Y';
980
- invoice.PostedById = loginUser.ObjectId;
981
- invoice.PostedDateTime = new Date();
982
-
983
- //for each document item, insert the document item accounting reference id that have matching name
984
- for (const documentItem of documentItems) {
985
- const accItem = accInvoiceRefIDetail.invoiceItems.find(
986
- (item) => item.name === documentItem.Name,
987
- );
988
- if (accItem) {
989
- documentItem.AccSystemRefId = accItem.itemRefId;
990
- documentItem.PostedToAccSystemYN = 'Y';
991
- documentItem.PostedById = loginUser.ObjectId;
992
- documentItem.PostedDateTime = new Date();
993
- }
994
- }
995
- } else {
996
- /*todo: check config file to see which invoice template is to be used for specific project*/
997
-
998
- /*Generating invoice based on template*/
999
- invoice.generateInvoice(loginUser.IDNo, customer, dbTransaction);
1000
- }
1001
-
1002
- /*Saving the document and document items to the database*/
1003
- await FinanceCompany._DocumentRepository.create(
1004
- {
1005
- DocNo: invoice.DocNo,
1006
- DocType: invoice.DocType,
1007
- DocDate: invoice.DocDate,
1008
- CompanyId: invoice.CompanyId,
1009
- Currency: invoice.Currency,
1010
- Amount: invoice.Amount,
1011
- Description: invoice.Description,
1012
- Status: invoice.Status,
1013
- IssuedById: loginUser.ObjectId,
1014
- IssuedToId: customer.ObjectId,
1015
- IssuedToType: invoice.IssuedToType,
1016
- RelatedObjectId: invoice.RelatedObjectId,
1017
- RelatedObjectType: invoice.RelatedObjectType,
1018
- CreatedById: loginUser.ObjectId,
1019
- CreatedAt: new Date(),
1020
- UpdatedById: loginUser.ObjectId,
1021
- UpdatedAt: new Date(),
1022
- DocPDFFileMediaId: invoice.DocPDFFileMediaId,
1023
- DocHTMLFileMediaId: invoice.DocHTMLFileMediaId,
1024
- AccSystemRefId: invoice.AccSystemRefId,
1025
- PostedToAccSystemYN: invoice.PostedToAccSystemYN,
1026
- PostedById:
1027
- invoice.PostedToAccSystemYN == 'Y' ? invoice.PostedById : null,
1028
- PostedDateTime: new Date(),
1029
- UseAccSystemDocYN: invoice.UseAccSystemDocYN,
1030
- },
1031
- {
1032
- transaction: dbTransaction,
1033
- },
1034
- );
1035
-
1036
- documentItems.forEach(async (documentItem) => {
1037
- await FinanceCompany._DocumentItemRepository.create(
1038
- {
1039
- DocumentItemId: this._createId().toUpperCase(),
1040
- DocNo: documentItem.DocNo,
1041
- Name: documentItem.Name,
1042
- NameBM: documentItem.NameBM,
1043
- Description: documentItem.Description,
1044
- ItemId: documentItem.ItemId,
1045
- ItemType: documentItem.ItemType,
1046
- ItemSKU: documentItem.ItemSKU,
1047
- ItemSerialNo: documentItem.ItemSerialNo,
1048
- Currency: documentItem.Currency,
1049
- UnitPrice: documentItem.UnitPrice,
1050
- Quantity: documentItem.Quantity,
1051
- QuantityUOM: documentItem.QuantityUOM,
1052
- Amount: documentItem.Amount,
1053
- TaxCode: documentItem.TaxCode,
1054
- TaxAmount: documentItem.TaxAmount,
1055
- TaxRate: documentItem.TaxRate,
1056
- TaxInclusiveYN: documentItem.TaxInclusiveYN,
1057
- DtAccountNo: documentItem.DtAccountNo,
1058
- CtAccountNo: documentItem.CtAccountNo,
1059
- AccSystemRefId: documentItem.AccSystemRefId,
1060
- PostedToAccSystemYN: documentItem.PostedToAccSystemYN,
1061
- PostedById: documentItem.PostedById,
1062
- PostedDateTime: documentItem.PostedDateTime,
1063
- },
1064
- {
1065
- transaction: dbTransaction,
1066
- },
1067
- );
1068
- });
1069
-
1070
- const transactionDate = new Date();
1071
- const htCreditAccountAmount = new HashTable();
1072
- const htCreditAccountCurrency = new HashTable();
1073
- const htCreditAccountPurpose = new HashTable();
1074
-
1075
- documentItems.forEach((invoiceItem) => {
1076
- if (!htCreditAccountAmount.exists(invoiceItem.CtAccountNo)) {
1077
- //add the credit account to the hash table
1078
- htCreditAccountAmount.add(
1079
- invoiceItem.CtAccountNo,
1080
- invoiceItem.Amount,
1081
- );
1082
-
1083
- htCreditAccountCurrency.add(
1084
- invoiceItem.CtAccountNo,
1085
- invoiceItem.Currency,
1086
- );
1087
-
1088
- htCreditAccountPurpose.add(invoiceItem.CtAccountNo, invoiceItem.Name);
1089
- } else {
1090
- //update the credit account amount
1091
- const d = htCreditAccountAmount.get(invoiceItem.CtAccountNo);
1092
- htCreditAccountAmount.add(
1093
- invoiceItem.CtAccountNo,
1094
- d + invoiceItem.Amount,
1095
- );
1096
- }
1097
- });
1098
-
1099
- const savedItems = htCreditAccountAmount.list();
1100
-
1101
- for (const item of savedItems) {
1102
- const journalEntry = new JournalEntry(dbTransaction);
1103
- //Temporary fix to successfully save journal entry in PostJournal
1104
- journalEntry.init({
1105
- CompanyId: this.CompanyId,
1106
- Name: 'issue Invoice ' + invoice.DocNo,
1107
- });
1108
- // const account = await Account.initAccount(dbTransaction, item.value[0]);
1109
- const creditAmount = item.value[1];
1110
- const currency = htCreditAccountCurrency.get(item.value[0]);
1111
- const purpose = htCreditAccountPurpose.get(item.value[0]);
1112
-
1113
- const dt = await journalEntry.newLedgerTransaction(
1114
- TransactionTypeOptions.DEBIT,
1115
- );
1116
-
1117
- if (dtAccountNo) {
1118
- /*Transacting using AR account provided*/
1119
- dt.AccountNo = dtAccountNo;
1120
- } else {
1121
- /*Transacting based on default customer AR account*/
1122
- const arAccount = await customer.getAccountReceivable();
1123
- dt.AccountNo = arAccount.AccountNo;
1124
- }
1125
-
1126
- dt.Currency = currency ? currency : 'MYR';
1127
- dt.DebitAmount = creditAmount ? creditAmount : 0.0;
1128
- dt.Date = transactionDate;
1129
- dt.Description = `${purpose}`;
1130
- dt.Name = `${purpose}`;
1131
- dt.RelatedDocNo = invoice.DocNo;
1132
- dt.RelatedObjectId = invoice.RelatedObjectId;
1133
- dt.RelatedObjectType = invoice.RelatedObjectType;
1134
-
1135
- const ct = await journalEntry.newLedgerTransaction(
1136
- TransactionTypeOptions.CREDIT,
1137
- );
1138
- ct.AccountNo = item.value[0];
1139
- ct.Currency = currency ? currency : 'MYR';
1140
- ct.CreditAmount = creditAmount ? creditAmount : 0.0;
1141
- ct.Date = transactionDate;
1142
- ct.Description = customer.FullName;
1143
- ct.Name = customer.FullName;
1144
- ct.RelatedDocNo = invoice.DocNo;
1145
- ct.RelatedObjectId = invoice.RelatedObjectId;
1146
- ct.RelatedObjectType = invoice.RelatedObjectType;
1147
-
1148
- await this.postJournal(dbTransaction, journalEntry, loginUser);
1149
- }
1150
-
1151
- return invoice;
1152
- } catch (err) {
1153
- // tslint:disable-next-line:no-console
1154
- console.log('Issue debit note err: ', err);
1155
- throw err;
1156
- }
1157
- }
1158
-
1159
- /**
1160
- * Issue a credit note
1161
- *
1162
- * @param dbTransaction - The database transaction to be used
1163
- * @param loginUser - The user issuing the credit note
1164
- * @param creditNote - The document containing the credit note details
1165
- * @param customer - The customer to be issued the credit note
1166
- * @param ctAccountNo - The account number of the Account Payable (AP) account to debit. If not provided, will debit the customer's default AR account
1167
- *
1168
- * @returns {Document} - Document object representing the full details of the invoice issued
1169
- */
1170
- async issueCreditNote(
1171
- dbTransaction: any,
1172
- loginUser: LoginUserBase,
1173
- creditNote: Document,
1174
- customer: FinanceCustomerBase,
1175
- ctAccountNo?: string,
1176
- ): Promise<Document> {
1177
- try {
1178
- /*Check if the invoice number already exists (should be unique)*/
1179
- const duplicateCreditNote =
1180
- await FinanceCompany._DocumentRepository.findOne({
1181
- where: {
1182
- DocNo: creditNote.DocNo,
1183
- },
1184
- transaction: dbTransaction,
1185
- });
1186
-
1187
- if (duplicateCreditNote) {
1188
- throw new Error('Invoice number already exists');
1189
- }
1190
-
1191
- const documentItems = await creditNote.getDocumentItems(dbTransaction);
1192
-
1193
- /*Check if the document has at least 1 document item*/
1194
- if (!documentItems.length) {
1195
- throw new Error('Document must have at least 1 document item');
1196
- }
1197
-
1198
- //Map each creditNote.DocumentItems and do checking below:
1199
- //1. Set actualDocument to the instantiation of existing Document. (to check valid Document)
1200
- //2. Check for each actualDocument to make sure IssuedToId all the same. If not, throw ClassError.
1201
- //3. Check if each document item has DtAccountNo provided
1202
- for (const invoiceItem of documentItems) {
1203
- if (!invoiceItem.DtAccountNo) {
1204
- throw new Error(
1205
- 'Each document item should have DtAccountNo provided',
1206
- );
1207
- }
1208
-
1209
- const actualDocument = await Document.initDocument(
1210
- dbTransaction,
1211
- invoiceItem.ItemId,
1212
- );
1213
-
1214
- if (actualDocument.IssuedToId !== creditNote.IssuedToId) {
1215
- throw new ClassError(
1216
- 'FinanceCompany',
1217
- 'FinanceCompanyErrMsgOX',
1218
- 'To issue credit note, all invoices must belong to same customer.',
1219
- );
1220
- }
1221
- }
1222
-
1223
- /*Set up the document type*/
1224
- creditNote.DocType = DocType.CREDIT_NOTE;
1225
- creditNote.Status = DocumentStatus.SETTLED;
1226
-
1227
- /*Saving the document and document items to the database*/
1228
- await FinanceCompany._DocumentRepository.create(
1229
- {
1230
- DocNo: creditNote.DocNo,
1231
- DocType: creditNote.DocType,
1232
- DocDate: creditNote.DocDate,
1233
- CompanyId: creditNote.CompanyId,
1234
- Currency: creditNote.Currency,
1235
- Amount: creditNote.Amount,
1236
- Description: creditNote.Description,
1237
- Status: creditNote.Status,
1238
- IssuedById: loginUser.ObjectId,
1239
- IssuedToId: customer.ObjectId,
1240
- IssuedToType: creditNote.IssuedToType,
1241
- RelatedObjectId: creditNote.RelatedObjectId,
1242
- RelatedObjectType: creditNote.RelatedObjectType,
1243
- CreatedById: loginUser.ObjectId,
1244
- CreatedAt: new Date(),
1245
- UpdatedById: loginUser.ObjectId,
1246
- UpdatedAt: new Date(),
1247
- DocPDFFileMediaId: creditNote.DocPDFFileMediaId,
1248
- DocHTMLFileMediaId: creditNote.DocHTMLFileMediaId,
1249
- AccSystemRefId: creditNote.AccSystemRefId,
1250
- PostedToAccSystemYN: creditNote.PostedToAccSystemYN,
1251
- PostedById:
1252
- creditNote.PostedToAccSystemYN == 'Y'
1253
- ? creditNote.PostedById
1254
- : null,
1255
- PostedDateTime: new Date(),
1256
- UseAccSystemDocYN: creditNote.UseAccSystemDocYN,
1257
- },
1258
- {
1259
- transaction: dbTransaction,
1260
- },
1261
- );
1262
-
1263
- for (const docItem of documentItems) {
1264
- await FinanceCompany._DocumentItemRepository.create(
1265
- {
1266
- DocumentItemId: this._createId().toUpperCase(),
1267
- DocNo: docItem.DocNo,
1268
- Name: docItem.Name,
1269
- NameBM: docItem.NameBM,
1270
- Description: docItem.Description,
1271
- ItemId: docItem.ItemId,
1272
- ItemType: docItem.ItemType,
1273
- ItemSKU: docItem.ItemSKU,
1274
- ItemSerialNo: docItem.ItemSerialNo,
1275
- Currency: docItem.Currency,
1276
- UnitPrice: docItem.UnitPrice,
1277
- Quantity: docItem.Quantity,
1278
- QuantityUOM: docItem.QuantityUOM,
1279
- Amount: docItem.Amount,
1280
- TaxCode: docItem.TaxCode,
1281
- TaxAmount: docItem.TaxAmount,
1282
- TaxRate: docItem.TaxRate,
1283
- TaxInclusiveYN: docItem.TaxInclusiveYN,
1284
- DtAccountNo: docItem.DtAccountNo ? docItem.DtAccountNo : null,
1285
- CtAccountNo: docItem.CtAccountNo ? docItem.CtAccountNo : null,
1286
- },
1287
- {
1288
- transaction: dbTransaction,
1289
- },
1290
- );
1291
-
1292
- //Call Document.settleByCreditNote to settle the invoice
1293
- await Document.settleByCreditNote(
1294
- loginUser,
1295
- dbTransaction,
1296
- docItem.ItemId,
1297
- docItem.Amount,
1298
- );
1299
- }
1300
-
1301
- /*Generating the credit note*/
1302
- if (creditNote.UseAccSystemDocYN === 'Y') {
1303
- /*todo: Posting to accounting system to generate creditNote*/
1304
- // await this.AccountingSystem.createCreditNote(creditNote);
1305
- } else {
1306
- /*todo: check config file to see which invoice template is to be used for specific project*/
1307
-
1308
- /*Generating credit note based on template*/
1309
- creditNote.generateCreditNote(loginUser.IDNo, customer);
1310
- }
1311
-
1312
- const journalEntry = new JournalEntry(dbTransaction);
1313
- //Temporary fix to successfully save journal entry in PostJournal
1314
- journalEntry.init({
1315
- CompanyId: this.CompanyId,
1316
- Name: 'Issue Credit Note ' + creditNote.DocNo,
1317
- });
1318
-
1319
- const transactionDate = new Date();
1320
-
1321
- const creditTransaction = await journalEntry.newLedgerTransaction(
1322
- TransactionTypeOptions.CREDIT,
1323
- );
1324
-
1325
- if (ctAccountNo) {
1326
- /*Transacting using AR account provided*/
1327
- creditTransaction.AccountNo = ctAccountNo;
1328
- } else {
1329
- /*Transacting based on default customer AR account*/
1330
- // creditTransaction.AccountNo = customer.CustSystemCode + '-AP';
1331
- // getAccountPayable
1332
- const arAccount = await customer.getAccountPayable();
1333
- creditTransaction.AccountNo = arAccount.AccountNo;
1334
- }
1335
-
1336
- creditTransaction.Currency = creditNote.Currency;
1337
- creditTransaction.CreditAmount = creditNote.Amount;
1338
- creditTransaction.Date = transactionDate;
1339
- creditTransaction.Description = creditNote.DocNo;
1340
- creditTransaction.Name = creditNote.DocNo;
1341
- creditTransaction.RelatedDocNo = creditNote.DocNo;
1342
- creditTransaction.RelatedObjectId = creditNote.RelatedObjectId;
1343
- creditTransaction.RelatedObjectType = creditNote.RelatedObjectType;
1344
-
1345
- for (const invoiceItem of documentItems) {
1346
- const itemLedger = await journalEntry.newLedgerTransaction(
1347
- TransactionTypeOptions.DEBIT,
1348
- );
1349
- itemLedger.AccountNo = invoiceItem.DtAccountNo;
1350
- itemLedger.Currency = invoiceItem.Currency;
1351
- itemLedger.DebitAmount = invoiceItem.Amount;
1352
- itemLedger.Date = transactionDate;
1353
- itemLedger.Description = invoiceItem.Name;
1354
- // itemLedger.Name = invoiceItem.Name;
1355
- itemLedger.RelatedDocNo = creditNote.DocNo;
1356
- itemLedger.RelatedObjectId = creditNote.RelatedObjectId;
1357
- itemLedger.RelatedObjectType = creditNote.RelatedObjectType;
1358
- }
1359
-
1360
- await this.postJournal(dbTransaction, journalEntry, loginUser);
1361
-
1362
- const entityValueAfter = {
1363
- LedgerNo: creditTransaction.LedgerNo,
1364
- TransactionType: creditTransaction.TransactionType,
1365
- JournalEntryId: creditTransaction.JournalEntryId,
1366
- AccountNo: creditTransaction.AccountNo,
1367
- Date: creditTransaction.Date,
1368
- Name: creditTransaction.Name,
1369
- Description: creditTransaction.Description,
1370
- Currency: creditTransaction.Currency,
1371
- DebitAmount: creditTransaction.DebitAmount,
1372
- CreditAmount: creditTransaction.CreditAmount,
1373
- RelatedObjectId: creditTransaction.RelatedObjectId,
1374
- RelatedObjectType: creditTransaction.RelatedObjectType,
1375
- RelatedDocNo: creditTransaction.RelatedDocNo,
1376
- RelatedPaymentId: creditTransaction.RelatedPaymentId,
1377
- };
1378
- const payload = {
1379
- Action: 'Create',
1380
- Activity: 'Issuing a Credit Note Transaction',
1381
- Description: `Credit Transaction (ID: ${creditTransaction.LedgerNo}) has been created`,
1382
- EntityType: 'CreditTransaction',
1383
- EntityValueBefore: JSON.stringify({}),
1384
- EntityValueAfter: JSON.stringify(entityValueAfter),
1385
- PerformedById: loginUser.ObjectId,
1386
- PerformedAt: transactionDate,
1387
- EntityId: creditTransaction.LedgerNo,
1388
- };
1389
-
1390
- await axios.post(
1391
- `${process.env.COMMON_API_URL}/activity-histories`,
1392
- payload,
1393
- );
1394
-
1395
- return creditNote;
1396
- } catch (err) {
1397
- // tslint:disable-next-line:no-console
1398
- console.log('Issue credit note err: ', err);
1399
- throw err;
1400
- }
1401
- }
1402
-
1403
- /**
1404
- * Register a payment collected
1405
- *
1406
- * @param dbTransaction - The database transaction to be used
1407
- * @param loginUser - The user collecting the payment
1408
- * @param payment - The payment object containing payment details
1409
- * @param customer - The customer making the payment
1410
- * @param ctAccountNo - The account number of the customer's Account Receivable account to transact, else the default customer account payable receivable will be used
1411
- * @param receiptNo - The document receipt number
1412
- *
1413
- * @returns {Payment} - Payment object representing the full detals of the payment collection recorded
1414
- */
1415
- async collectPayment(
1416
- dbTransaction: any,
1417
- loginUser: LoginUserBase,
1418
- payment: Payment,
1419
- collectPaymentType: CollectPaymentType = CollectPaymentType.AUTOMATIC,
1420
- ): Promise<Payment> {
1421
- try {
1422
- /*validation 1: Make sure that the payment has at least 1 payment item*/
1423
- const paymentItems = await payment.getPaymentItems();
1424
- if (paymentItems.length < 1) {
1425
- throw new Error(
1426
- 'Atleast one payment item is required to identify what payment is being paid for.',
1427
- );
1428
- }
1429
-
1430
- /*validation 2: Make sure that the payment has at least 1 payment method*/
1431
- const paymentPaidWithItems = await payment.getPaymentPaidWith();
1432
- if (paymentPaidWithItems.length < 1) {
1433
- throw new Error(
1434
- 'Atleast one payment paid with item is required to identify how the payment was made.',
1435
- );
1436
- }
1437
-
1438
- /*Saving payment, payment items, and payment paid with details to the database*/
1439
- payment.PaymentId = this._createId().toUpperCase();
1440
- payment.PaymentType = PaymentType.PAYMENT_RECEIVED;
1441
- payment.ReceivedBy = loginUser.ObjectId;
1442
- payment.IssuedBy = loginUser.ObjectId;
1443
-
1444
- await FinanceCompany._PaymentRepository.create(
1445
- {
1446
- PaymentId: payment.PaymentId,
1447
- PaymentType: payment.PaymentType,
1448
- PaymentDate: payment.PaymentDate,
1449
- Description: payment.Description,
1450
- Currency: payment.Currency,
1451
- Amount: payment.Amount,
1452
- Status:
1453
- collectPaymentType === CollectPaymentType.AUTOMATIC
1454
- ? PaymentStatus.CONFIRMED
1455
- : PaymentStatus.PENDING,
1456
- PostedToAccSystemYN: 'N',
1457
- ReceivedBy: payment.ReceivedBy,
1458
- IssuedBy: payment.IssuedBy,
1459
- Remarks: payment.Remarks,
1460
- RelatedObjectId: payment.RelatedObjectId,
1461
- RelatedObjectType: payment.RelatedObjectType,
1462
- ReceiptDocNo: payment.ReceiptDocNo,
1463
- UpdatedAt: new Date(),
1464
- UpdatedBy: loginUser.ObjectId,
1465
- CreatedAt: new Date(),
1466
- CreatedBy: loginUser.ObjectId,
1467
- },
1468
- { transaction: dbTransaction },
1469
- );
1470
-
1471
- for (const paymentItem of paymentItems) {
1472
- await FinanceCompany._PaymentItemRepository.create(
1473
- {
1474
- PaymentId: payment.PaymentId,
1475
- PayForObjectId: paymentItem.PayForObjectId,
1476
- PayForObjectType: paymentItem.PayForObjectType,
1477
- Currency: paymentItem.Currency,
1478
- Amount: paymentItem.Amount,
1479
- Name: paymentItem.Name,
1480
- Description: paymentItem.Description,
1481
- },
1482
- { transaction: dbTransaction },
1483
- );
1484
-
1485
- // await paymentItem.paid(dbTransaction);
1486
- }
1487
-
1488
- for (const paymentPaidWithItem of paymentPaidWithItems) {
1489
- // Validate payment method type used
1490
- await PaymentMethodType.initMethodType(
1491
- dbTransaction,
1492
- paymentPaidWithItem.MethodTypeId,
1493
- );
1494
-
1495
- await FinanceCompany._PaymentPaidWithRepository.create(
1496
- {
1497
- PaymentId: payment.PaymentId,
1498
- MethodTypeId: paymentPaidWithItem.MethodTypeId,
1499
- Currency: paymentPaidWithItem.Currency,
1500
- Amount: paymentPaidWithItem.Amount,
1501
- Status: paymentPaidWithItem.Status,
1502
- TransactionId: paymentPaidWithItem.TransactionId,
1503
- RefBank: paymentPaidWithItem.RefBank,
1504
- RefName: paymentPaidWithItem.RefName,
1505
- RefNo: paymentPaidWithItem.RefNo,
1506
- RefOther1: paymentPaidWithItem.RefOther1,
1507
- RefOther2: paymentPaidWithItem.RefOther2,
1508
- RefOther3: paymentPaidWithItem.RefOther3,
1509
- RefOther4: paymentPaidWithItem.RefOther4,
1510
- RefOther5: paymentPaidWithItem.RefOther5,
1511
- Remarks: paymentPaidWithItem.Remarks,
1512
- PaymentMediaId: paymentPaidWithItem.PaymentMediaId,
1513
- },
1514
- { transaction: dbTransaction },
1515
- );
1516
- }
1517
-
1518
- /*todo: saving a record into the activity history table*/
1519
-
1520
- return payment;
1521
- } catch (error) {
1522
- throw error;
1523
- }
1524
- }
1525
-
1526
- /**
1527
- * Method to make payment to a customer.
1528
- *
1529
- * @param dbTransaction - The database transaction to be used
1530
- * @param loginUser - The user logging in and the user who collected the payment.
1531
- * @param payment - The payment object containing payment details
1532
- * @param customer - The customer who is receiving the payment.
1533
- * @param ctAccountNo - The account number of the customer's Account Payable account to transact, else the default customer account payable will be used.
1534
- *
1535
- * @returns {Payment} - Payment object representing the full details of the payment collection recorded
1536
- */
1537
- async makePayment(
1538
- dbTransaction: any,
1539
- loginUser: LoginUserBase,
1540
- payment: Payment,
1541
- customer: FinanceCustomerBase,
1542
- dtAccountNo?: string,
1543
- ): Promise<Payment> {
1544
- let paymentMethodType: PaymentMethodType;
1545
- try {
1546
- const paymentPaidWith = await payment.getPaymentPaidWith();
1547
- if (paymentPaidWith.length < 1) {
1548
- throw new Error(
1549
- 'Atleast one payment paid with item is required to identify how the payment was made.',
1550
- );
1551
- }
1552
- paymentMethodType = await PaymentMethodType.initMethodType(
1553
- dbTransaction,
1554
- paymentPaidWith[0].MethodTypeId,
1555
- );
1556
-
1557
- const paymentItems = await payment.getPaymentItems();
1558
- if (paymentItems.length < 1) {
1559
- throw new Error(
1560
- 'Atleast one payment item is required to identify what payment is being paid for',
1561
- );
1562
- }
1563
-
1564
- payment.PaymentId = this._createId().toUpperCase();
1565
- payment.PaymentType = PaymentType.PAYOUT;
1566
- payment.ReceivedBy = loginUser.ObjectId;
1567
- payment.IssuedBy = loginUser.ObjectId;
1568
-
1569
- await FinanceCompany._PaymentRepository.create(
1570
- {
1571
- PaymentId: payment.PaymentId,
1572
- PaymentType: payment.PaymentType,
1573
- PaymentDate: payment.PaymentDate,
1574
- Description: payment.Description,
1575
- Currency: payment.Currency,
1576
- ReceivedBy: payment.ReceivedBy,
1577
- IssuedBy: payment.IssuedBy,
1578
- Remarks: payment.Remarks,
1579
- RelatedObjectId: payment.RelatedObjectId,
1580
- RelatedObjectType: payment.RelatedObjectType,
1581
- Amount: payment.Amount,
1582
- Status: PaymentStatus.CONFIRMED,
1583
- PostedToAccSystemYN: 'N',
1584
- UpdatedAt: new Date(),
1585
- UpdatedBy: loginUser.ObjectId,
1586
- CreatedAt: new Date(),
1587
- CreatedBy: loginUser.ObjectId,
1588
- },
1589
- { transaction: dbTransaction },
1590
- );
1591
-
1592
- paymentItems.forEach(async (paymentItem) => {
1593
- await FinanceCompany._PaymentItemRepository.create(
1594
- {
1595
- PaymentId: payment.PaymentId,
1596
- PayForObjectId: paymentItem.PayForObjectId,
1597
- PayForObjectType: paymentItem.PayForObjectType,
1598
- Currency: paymentItem.Currency,
1599
- Amount: paymentItem.Amount,
1600
- Name: paymentItem.Name,
1601
- Description: paymentItem.Description,
1602
- },
1603
- { transaction: dbTransaction },
1604
- );
1605
-
1606
- await paymentItem.paid(dbTransaction);
1607
- });
1608
-
1609
- for (const paymentPaidWithItem of paymentPaidWith) {
1610
- await FinanceCompany._PaymentPaidWithRepository.create(
1611
- {
1612
- PaymentId: payment.PaymentId,
1613
- MethodTypeId: paymentPaidWithItem.MethodTypeId,
1614
- Currency: paymentPaidWithItem.Currency,
1615
- Amount: paymentPaidWithItem.Amount,
1616
- Status: paymentPaidWithItem.Status,
1617
- TransactionId: paymentPaidWithItem.TransactionId,
1618
- RefBank: paymentPaidWithItem.RefBank,
1619
- RefName: paymentPaidWithItem.RefName,
1620
- RefNo: paymentPaidWithItem.RefNo,
1621
- RefOther1: paymentPaidWithItem.RefOther1,
1622
- RefOther2: paymentPaidWithItem.RefOther2,
1623
- RefOther3: paymentPaidWithItem.RefOther3,
1624
- RefOther4: paymentPaidWithItem.RefOther4,
1625
- RefOther5: paymentPaidWithItem.RefOther5,
1626
- Remarks: paymentPaidWithItem.Remarks,
1627
- PaymentMediaId: paymentPaidWithItem.PaymentMediaId,
1628
- },
1629
- { transaction: dbTransaction },
1630
- );
1631
- }
1632
- const transactionDate = new Date();
1633
- for (const paymentPaidWithItem of paymentPaidWith) {
1634
- try {
1635
- paymentMethodType = await PaymentMethodType.initMethodType(
1636
- dbTransaction,
1637
- paymentPaidWithItem.MethodTypeId,
1638
- );
1639
-
1640
- const journalEntry = new JournalEntry(dbTransaction);
1641
- //Temporary fix to successfully save journal entry in PostJournal
1642
- journalEntry.init({
1643
- CompanyId: this.CompanyId,
1644
- Name: 'Make Payment for ' + payment.PaymentId,
1645
- });
1646
-
1647
- const creditLT = await journalEntry.newLedgerTransaction(
1648
- TransactionTypeOptions.CREDIT,
1649
- );
1650
- creditLT.AccountNo = paymentMethodType.AccountNo;
1651
- creditLT.Currency = paymentPaidWithItem.Currency;
1652
- creditLT.CreditAmount = paymentPaidWithItem.Amount;
1653
- creditLT.Date = transactionDate;
1654
- creditLT.Description = customer.FullName;
1655
- creditLT.Name = customer.FullName;
1656
- creditLT.RelatedObjectId = payment.RelatedObjectId;
1657
- creditLT.RelatedObjectType = payment.RelatedObjectType;
1658
-
1659
- const debitLT = await journalEntry.newLedgerTransaction(
1660
- TransactionTypeOptions.DEBIT,
1661
- );
1662
- if (dtAccountNo) {
1663
- debitLT.AccountNo = dtAccountNo;
1664
- } else {
1665
- const apAccount = await customer.getAccountPayable();
1666
- debitLT.AccountNo = apAccount.AccountNo;
1667
- }
1668
- debitLT.Currency = paymentPaidWithItem.Currency;
1669
- debitLT.DebitAmount = paymentPaidWithItem.Amount;
1670
- debitLT.Date = transactionDate;
1671
- debitLT.Description = paymentMethodType.Name;
1672
- debitLT.Name = paymentMethodType.Name;
1673
- debitLT.RelatedObjectId = payment.RelatedObjectId;
1674
- debitLT.RelatedObjectType = payment.RelatedObjectType;
1675
-
1676
- await this.postJournal(dbTransaction, journalEntry, loginUser);
1677
- } catch (error) {
1678
- if (error instanceof RecordNotFoundError) {
1679
- throw new Error('Invalid Payment Method Type Id');
1680
- } else {
1681
- throw error;
1682
- }
1683
- }
1684
- }
1685
- /*todo: saving a record into the activity history table*/
1686
-
1687
- return payment;
1688
- } catch (error) {
1689
- if (error instanceof RecordNotFoundError) {
1690
- throw new Error('Invalid PaymentMethodType id');
1691
- } else {
1692
- throw error;
1693
- }
1694
- }
1695
- }
1696
-
1697
- get PaymentMethods(): Promise<PaymentMethod[]> {
1698
- return new Promise((resolve, reject) => {
1699
- if (this.CompanyId !== 'New') {
1700
- FinanceCompany._PaymentMethodRepository
1701
- .findAll({
1702
- where: {
1703
- CompanyId: this.CompanyId,
1704
- },
1705
- // transaction: this._DbTransaction,
1706
- })
1707
- .then((paymentMethod) => {
1708
- const paymentMethodObjects = paymentMethod.map(
1709
- (paymentMethodData) => {
1710
- return new Promise((resolve, reject) => {
1711
- FinanceCompany._PaymentMethodTypeRepository
1712
- .findAll({
1713
- where: {
1714
- MethodId: paymentMethodData.MethodId,
1715
- },
1716
- // transaction: this._DbTransaction,
1717
- raw: true,
1718
- })
1719
- .then((paymentMethodTypes) => {
1720
- const paymentMethodObjects = {
1721
- ...paymentMethodData.get({ plain: true }),
1722
- Types: paymentMethodTypes,
1723
- };
1724
- resolve(paymentMethodObjects);
1725
- })
1726
- .catch((err) => {
1727
- reject(err);
1728
- });
1729
- }).then((paymentMethods) => paymentMethods);
1730
- },
1731
- );
1732
- return Promise.all(paymentMethodObjects);
1733
- })
1734
- .then((paymentMethodObjects) => {
1735
- this._PaymentMethods = paymentMethodObjects;
1736
- resolve(this._PaymentMethods);
1737
- })
1738
- .catch((err) => {
1739
- reject(err);
1740
- });
1741
- } else {
1742
- resolve(this._PaymentMethods);
1743
- }
1744
- });
1745
- }
1746
-
1747
- get TaxCodes(): Promise<Tax[]> {
1748
- return new Promise((resolve, reject) => {
1749
- if (this.CompanyId !== 'New') {
1750
- FinanceCompany._TaxRepository
1751
- .findAll({
1752
- where: {
1753
- CompanyId: this.CompanyId,
1754
- },
1755
- transaction: this._DbTransaction,
1756
- })
1757
- .then((taxes) => {
1758
- const taxList = [];
1759
- taxes.forEach((tax) => {
1760
- taxList.push(
1761
- new Tax({
1762
- TaxCode: tax.TaxCode,
1763
- TaxRate:
1764
- typeof tax.TaxRate === 'number'
1765
- ? tax.TaxRate
1766
- : parseFloat(tax.TaxRate),
1767
- Description: tax.Description,
1768
- CompanyId: tax.CompanyId,
1769
- CreatedAt: tax.CreatedAt,
1770
- CreatedById: tax.CreatedById,
1771
- UpdatedAt: tax.UpdatedAt,
1772
- UpdatedById: tax.UpdatedById,
1773
- }),
1774
- );
1775
- });
1776
- this._Taxes = taxList;
1777
- resolve(this._Taxes);
1778
- })
1779
- .catch((err) => {
1780
- reject(err);
1781
- });
1782
- } else {
1783
- resolve(this._Taxes);
1784
- }
1785
- });
1786
- }
1787
-
1788
- /**
1789
- * Method to load / reload the payment methods based on the configuration file
1790
- */
1791
- async LoadPaymentMethods(
1792
- companyId: string,
1793
- paymentMethods: any,
1794
- transaction?: any,
1795
- ): Promise<void> {
1796
- const paymentMethod = new PaymentMethod();
1797
- for (const method in paymentMethods) {
1798
- const paymentMethodData =
1799
- await FinanceCompany._PaymentMethodRepository.findOne({
1800
- where: {
1801
- MethodId: paymentMethods[method].id,
1802
- Name: paymentMethods[method].name,
1803
- },
1804
- transaction: transaction,
1805
- });
1806
-
1807
- if (!paymentMethodData) {
1808
- const newPaymentMethod =
1809
- await FinanceCompany._PaymentMethodRepository.create(
1810
- {
1811
- MethodId: paymentMethods[method].id,
1812
- Name: paymentMethods[method].name,
1813
- CompanyId: companyId,
1814
- },
1815
- {
1816
- transaction: transaction,
1817
- },
1818
- );
1819
-
1820
- this._PaymentMethods.push(newPaymentMethod);
1821
- }
1822
- this._PaymentMethods.push(paymentMethodData);
1823
- }
1824
-
1825
- this._PaymentMethods.forEach(async (item) => {
1826
- const p = item?.get({ plain: true });
1827
-
1828
- for (const method in paymentMethods) {
1829
- if (!p) {
1830
- continue;
1831
- }
1832
-
1833
- if (p.MethodId === paymentMethods[method]?.id) {
1834
- const paymentMethodTypeData =
1835
- await FinanceCompany._PaymentMethodTypeRepository.findOne({
1836
- where: {
1837
- MethodId: p.MethodId,
1838
- },
1839
- transaction: transaction,
1840
- });
1841
-
1842
- if (!paymentMethodTypeData) {
1843
- const configPaymentMethodTypes = paymentMethods[method]?.types;
1844
-
1845
- for (const methodType in configPaymentMethodTypes) {
1846
- // TODO: Create a seeder for payment method account
1847
- /*validate whether account data already exists*/
1848
- const accountData =
1849
- await FinanceCompany._AccountRepository.findOne({
1850
- where: {
1851
- AccountNo: configPaymentMethodTypes[methodType].accountNo,
1852
- },
1853
- transaction: transaction,
1854
- });
1855
-
1856
- /*generating account data if not exist */
1857
- if (!accountData) {
1858
- const accountPayload = {
1859
- CompanyId: companyId,
1860
- Name: configPaymentMethodTypes[methodType].name,
1861
- AccountType: 'PaymentMethod',
1862
- CreatedAt: new Date(),
1863
- CreatedById: 'System',
1864
- AccSystemRefId: 'REF',
1865
- PostedToAccSystemYN: 'N',
1866
- };
1867
-
1868
- try {
1869
- await FinanceCompany._AccountRepository.create(
1870
- {
1871
- AccountNo: configPaymentMethodTypes[methodType].accountNo,
1872
- ...accountPayload,
1873
- },
1874
- {
1875
- transaction: transaction,
1876
- },
1877
- );
1878
-
1879
- await FinanceCompany._AccountRepository.create(
1880
- {
1881
- AccountNo:
1882
- configPaymentMethodTypes[methodType]
1883
- .processingFeeAccountNo,
1884
- ...accountPayload,
1885
- },
1886
- {
1887
- transaction: transaction,
1888
- },
1889
- );
1890
- } catch (err) {
1891
- throw err;
1892
- }
1893
- }
1894
-
1895
- const paymentMethodTypePayload = {
1896
- MethodId: p.MethodId,
1897
- MethodTypeId: configPaymentMethodTypes[methodType].id,
1898
- Name: configPaymentMethodTypes[methodType].name,
1899
- AccountNo: configPaymentMethodTypes[methodType].accountNo,
1900
- ProcessingFeeRate:
1901
- configPaymentMethodTypes[methodType].processingFeeRate,
1902
- ProcessingFeeAccountNo:
1903
- configPaymentMethodTypes[methodType].processingFeeAccountNo,
1904
- };
1905
-
1906
- try {
1907
- await paymentMethod.newPaymentMethodType(
1908
- paymentMethodTypePayload,
1909
- transaction,
1910
- );
1911
- } catch (err) {}
1912
- }
1913
- }
1914
- }
1915
- }
1916
- });
1917
- }
1918
-
1919
- /**
1920
- * Method to load / reload the payment methods based on the configuration file
1921
- */
1922
- async LoadTaxCodes(
1923
- companyId: string,
1924
- companyTaxes: any,
1925
- transaction?: any,
1926
- ): Promise<void> {
1927
- for (const tax in companyTaxes) {
1928
- let tx = await FinanceCompany._TaxRepository.findOne({
1929
- where: {
1930
- TaxCode: companyTaxes[tax].taxCode,
1931
- },
1932
- transaction: transaction,
1933
- });
1934
-
1935
- if (!tx) {
1936
- const newTx = await FinanceCompany._TaxRepository.create(
1937
- {
1938
- TaxCode: companyTaxes[tax].taxCode,
1939
- TaxRate: companyTaxes[tax].taxRate,
1940
- Description: companyTaxes[tax].description,
1941
- CompanyId: companyId,
1942
- UpdatedAt: new Date(),
1943
- CreatedById: 'System',
1944
- CreatedAt: new Date(),
1945
- UpdatedById: 'System',
1946
- },
1947
- {
1948
- transaction: transaction,
1949
- },
1950
- );
1951
-
1952
- tx = newTx;
1953
- }
1954
- this._Taxes.push(new Tax(tx.get({ plain: true })));
1955
- }
1956
- }
1957
-
1958
- async collectPaymentForMultipleCustomers(
1959
- dbTransaction: any,
1960
- loginUser: LoginUserBase,
1961
- payment: Payment,
1962
- creditTransaction: {
1963
- AccountNo: string;
1964
- Currency: string;
1965
- Amount: number;
1966
- }[],
1967
- receiptNo?: string,
1968
- collectPaymentType: CollectPaymentType = CollectPaymentType.AUTOMATIC,
1969
- ): Promise<Payment> {
1970
- //Method to collect payment for multiple customer.
1971
- try {
1972
- //Part 1: Privilege Checking\
1973
- const systemCode =
1974
- await ApplicationConfig.getComponentConfigValue('system-code');
1975
- const isPrivileged = await loginUser.checkPrivileges(
1976
- systemCode,
1977
- 'Collect Payment For Multiple Customers',
1978
- );
1979
-
1980
- if (!isPrivileged) {
1981
- throw new Error('User is not authorized to perform this action');
1982
- }
1983
-
1984
- //Part 2: Validation
1985
- //Make sure that the payment has at least 1 payment item
1986
- const paymentItems = await payment.getPaymentItems();
1987
- if (paymentItems.length < 1) {
1988
- throw new Error(
1989
- 'Atleast one payment item is required to identify what payment is being paid for.',
1990
- );
1991
- }
1992
-
1993
- //Make sure that the payment has at least 1 payment method
1994
- const paymentPaidWithItems = await payment.getPaymentPaidWith();
1995
- if (paymentPaidWithItems.length < 1) {
1996
- throw new Error(
1997
- 'Atleast one payment paid with item is required to identify how the payment was made.',
1998
- );
1999
- }
2000
-
2001
- //Make sure the payment items length is equal to creditTransactions length records.
2002
- if (paymentItems.length !== creditTransaction.length) {
2003
- throw new Error(
2004
- 'Payment items length is not equal to creditTransaction length',
2005
- );
2006
- }
2007
-
2008
- //Make sure the payment items total Amount is equal to credit transactions total Amount.
2009
- let totalAmount = 0;
2010
- paymentItems.forEach((paymentItem) => {
2011
- totalAmount += paymentItem.Amount;
2012
- });
2013
-
2014
- const totalCreditTransactionAmount = creditTransaction.reduce(
2015
- (accumulator, currentValue) => {
2016
- return accumulator + currentValue.Amount;
2017
- },
2018
- 0,
2019
- );
2020
-
2021
- if (totalAmount !== totalCreditTransactionAmount) {
2022
- throw new Error(
2023
- 'Payment items total Amount is dnot equal to credit transactions total Amount',
2024
- );
2025
- }
2026
-
2027
- //Part 3:Generate Receipt (KIV)
2028
-
2029
- //Part 4: Saving Payment, Payment Items and Payment Paid with Details to the Database
2030
-
2031
- //Set below Payment attributes:
2032
- payment.PaymentId = this._createId().toUpperCase();
2033
- payment.PaymentType = PaymentType.PAYMENT_RECEIVED;
2034
- payment.ReceivedBy = loginUser.ObjectId;
2035
- payment.IssuedBy = loginUser.ObjectId;
2036
-
2037
- //Call Payment Repository create() method by passing:
2038
- await FinanceCompany._PaymentRepository.create(
2039
- {
2040
- PaymentId: payment.PaymentId,
2041
- PaymentType: payment.PaymentType,
2042
- PaymentDate: payment.PaymentDate,
2043
- Description: payment.Description,
2044
- Currency: payment.Currency,
2045
- Amount: payment.Amount,
2046
- Status:
2047
- collectPaymentType === CollectPaymentType.MANUAL
2048
- ? PaymentStatus.PENDING
2049
- : PaymentStatus.CONFIRMED,
2050
- PostedToAccSystemYN: 'N',
2051
- ReceivedBy: payment.ReceivedBy,
2052
- IssuedBy: payment.IssuedBy,
2053
- Remarks: payment.Remarks,
2054
- RelatedObjectId: payment.RelatedObjectId,
2055
- RelatedObjectType: payment.RelatedObjectType,
2056
- ReceiptDocNo: payment.ReceiptDocNo,
2057
- UpdatedAt: new Date(),
2058
- UpdatedBy: loginUser.ObjectId,
2059
- CreatedAt: new Date(),
2060
- CreatedBy: loginUser.ObjectId,
2061
- },
2062
- { transaction: dbTransaction },
2063
- );
2064
-
2065
- //Map Payment.PaymentItems and insert record by passing dbTransaction
2066
- paymentItems.forEach(async (paymentItem) => {
2067
- await FinanceCompany._PaymentItemRepository.create(
2068
- {
2069
- PaymentId: payment.PaymentId,
2070
- PayForObjectId: paymentItem.PayForObjectId,
2071
- PayForObjectType: paymentItem.PayForObjectType,
2072
- Currency: paymentItem.Currency,
2073
- Amount: paymentItem.Amount,
2074
- Name: paymentItem.Name,
2075
- Description: paymentItem.Description,
2076
- },
2077
- { transaction: dbTransaction },
2078
- );
2079
- });
2080
-
2081
- // Map Payment.PaymentPaidWith and insert record by passing dbTransaction.
2082
- paymentPaidWithItems.forEach(async (paymentPaidWithItem) => {
2083
- await FinanceCompany._PaymentPaidWithRepository.create(
2084
- {
2085
- PaymentId: payment.PaymentId,
2086
- MethodTypeId: paymentPaidWithItem.MethodTypeId,
2087
- Currency: paymentPaidWithItem.Currency,
2088
- Amount: paymentPaidWithItem.Amount,
2089
- Status: paymentPaidWithItem.Status,
2090
- LedgerNo: paymentPaidWithItem.TransactionId,
2091
- RefBank: paymentPaidWithItem.RefBank,
2092
- RefName: paymentPaidWithItem.RefName,
2093
- RefNo: paymentPaidWithItem.RefNo,
2094
- RefOther1: paymentPaidWithItem.RefOther1,
2095
- RefOther2: paymentPaidWithItem.RefOther2,
2096
- RefOther3: paymentPaidWithItem.RefOther3,
2097
- RefOther4: paymentPaidWithItem.RefOther4,
2098
- RefOther5: paymentPaidWithItem.RefOther5,
2099
- Remarks: paymentPaidWithItem.Remarks,
2100
- PaymentMediaId: paymentPaidWithItem.PaymentMediaId,
2101
- },
2102
- { transaction: dbTransaction },
2103
- );
2104
- });
2105
-
2106
- //Part 5: Registering the Journal Entries for the transaction
2107
- //Initialise TransactionDate
2108
- // const transactionDate = new Date();
2109
- // //Initialise new JournalEntry
2110
- // const journalEntry = new JournalEntry(dbTransaction);
2111
- // journalEntry.init({
2112
- // CompanyId: this.CompanyId,
2113
- // Name: 'Collect-payments for ' + payment.PaymentId,
2114
- // });
2115
-
2116
- // // For each Payment.PaymentPaidWith:
2117
- // for (const paymentPaidWithItem of paymentPaidWithItems) {
2118
- // const paymentMethodType = await PaymentMethodType.initMethodType(
2119
- // dbTransaction,
2120
- // paymentPaidWithItem.MethodTypeId,
2121
- // );
2122
-
2123
- // //Initialise new Debit LedgerTransaction by calling JournalEntry.newLedgerTransaction() method and set below attributes:
2124
- // const debitLT = await journalEntry.newLedgerTransaction(
2125
- // TransactionTypeOptions.DEBIT,
2126
- // );
2127
- // debitLT.AccountNo = paymentMethodType.AccountNo;
2128
- // debitLT.Currency = paymentPaidWithItem.Currency;
2129
- // debitLT.DebitAmount = paymentPaidWithItem.Amount;
2130
- // debitLT.Date = transactionDate;
2131
- // debitLT.Description = 'Payment Received';
2132
- // debitLT.RelatedPaymentId = payment.PaymentId;
2133
- // }
2134
-
2135
- // //For each creditTransactions, Initialise new Credit LedgerTransaction by calling JournalEntry.newLedgerTransaction() method
2136
- // for (const ct of creditTransaction) {
2137
- // const creditLT = await journalEntry.newLedgerTransaction(
2138
- // TransactionTypeOptions.CREDIT,
2139
- // );
2140
- // creditLT.AccountNo = ct.AccountNo;
2141
- // creditLT.Currency = ct.Currency;
2142
- // creditLT.CreditAmount = ct.Amount;
2143
- // creditLT.Date = transactionDate;
2144
- // creditLT.Description = 'Payment Received';
2145
- // creditLT.RelatedPaymentId = payment.PaymentId;
2146
- // }
2147
-
2148
- // // Call this.postJournal()
2149
- // await this.postJournal(dbTransaction, journalEntry, loginUser);
2150
-
2151
- return payment;
2152
- } catch (error) {
2153
- throw error;
2154
- }
2155
- }
2156
-
2157
- /**
2158
- * Confirms a payment and performs necessary actions such as creating a payment receipt and registering journal entries.
2159
- * @param dbTransaction - The database transaction object.
2160
- * @param loginUser - The logged-in user object.
2161
- * @param customer - The customer object.
2162
- * @param payment - The payment object.
2163
- * @param status - The status of the payment (CONFIRMED, REJECTED, or FAILED).
2164
- * @param remarks - Additional remarks for the payment.
2165
- * @param ctAccountNo - Optional. The account number for the credit transaction.
2166
- * @param receiptNo - Optional. The receipt number for the payment.
2167
- * @returns A promise that resolves when the payment is confirmed.
2168
- * @throws Throws an error if the user does not have the necessary privileges, if the payment is not found, if the payment status is not 'Pending', or if an invalid status is provided.
2169
- */
2170
- async confirmPayment(
2171
- dbTransaction: any,
2172
- loginUser: LoginUserBase,
2173
- customer: FinanceCustomerBase,
2174
- payment: Payment,
2175
- status:
2176
- | PaymentStatus.CONFIRMED
2177
- | PaymentStatus.REJECTED
2178
- | PaymentStatus.FAILED,
2179
- remarks: string,
2180
- ctAccountNo?: string,
2181
- receiptNo?: string,
2182
- ): Promise<void> {
2183
- try {
2184
- this._DbTransaction = dbTransaction;
2185
- const systemCode =
2186
- ApplicationConfig.getComponentConfigValue('system-code');
2187
-
2188
- const isPrivileged = await loginUser.checkPrivileges(
2189
- systemCode,
2190
- 'FinanceCompany - Confirm Payment',
2191
- );
2192
- if (!isPrivileged) {
2193
- throw new ClassError(
2194
- 'FinanceCompany',
2195
- 'FinanceCompanyConfirmPaymentErrMsg00',
2196
- `You do not have 'Payment - Confirm' privilege.`,
2197
- );
2198
- }
2199
-
2200
- if (payment.PaymentId === 'New') {
2201
- throw new ClassError(
2202
- 'FinanceCompany',
2203
- 'FinanceCompanyConfirmPaymentErrMsg01',
2204
- `Payment not found.`,
2205
- );
2206
- }
2207
-
2208
- if (payment.Status !== PaymentStatus.PENDING) {
2209
- throw new ClassError(
2210
- 'Payment',
2211
- 'PaymentConfirmPaymentErrMsg02',
2212
- `Payment status is not 'Pending'.`,
2213
- );
2214
- }
2215
-
2216
- payment.Remarks = remarks;
2217
- let receiptDocNo = null;
2218
- switch (status) {
2219
- case PaymentStatus.REJECTED:
2220
- payment.Status = PaymentStatus.REJECTED;
2221
- break;
2222
- case PaymentStatus.FAILED:
2223
- payment.Status = PaymentStatus.FAILED;
2224
- break;
2225
- case PaymentStatus.CONFIRMED:
2226
- payment.Status = PaymentStatus.CONFIRMED;
2227
- //Creating the payment receipt
2228
- /*Generating the receipt*/
2229
- const receiptDocuments: any =
2230
- await FinanceCompany._DocumentRepository.findAll({
2231
- where: {
2232
- DocType: DocType.RECEIPT,
2233
- CompanyId: this.ObjectId,
2234
- },
2235
- });
2236
-
2237
- const receipt = new Document(dbTransaction);
2238
-
2239
- if (receiptNo) {
2240
- receipt.DocNo = receiptNo;
2241
- } else {
2242
- receipt.DocNo = `EZC-RCT-${receiptDocuments.length + 1}`;
2243
- }
2244
- receipt.DocType = DocType.RECEIPT;
2245
- receipt.DocDate = new Date();
2246
- receipt.CompanyId = this.ObjectId;
2247
- receipt.Currency = payment.Currency;
2248
- receipt.Description = 'Payment Received';
2249
- receipt.IssuedById = loginUser.ObjectId;
2250
- receipt.IssuedToId = customer.CustomerId;
2251
- receipt.IssuedToType = type(customer);
2252
- receipt.RelatedObjectId = payment.RelatedObjectId;
2253
- receipt.RelatedObjectType = payment.RelatedObjectType;
2254
- receipt.UseAccSystemDocYN = 'N';
2255
- receipt.CreatedById = loginUser.ObjectId;
2256
- receipt.UpdatedById = loginUser.ObjectId;
2257
-
2258
- const paymentItems = await payment.getPaymentItems(dbTransaction);
2259
- for (const paymentItem of paymentItems) {
2260
- const receiptItem = new DocumentItem(dbTransaction, receipt);
2261
- receiptItem.Name = `Payment for ${paymentItem.PayForObjectType} No. ${paymentItem.PayForObjectId}`;
2262
- receiptItem.NameBM = `Bayaran untuk ${paymentItem.PayForObjectType} No. ${paymentItem.PayForObjectId}`;
2263
- receiptItem.Description = '-';
2264
- receiptItem.Currency = paymentItem.Currency;
2265
- receiptItem.UnitPrice = paymentItem.Amount;
2266
- receiptItem.Quantity = 1;
2267
- receiptItem.Amount = receiptItem.UnitPrice * receiptItem.Quantity;
2268
- receiptItem.ItemId = receipt.DocNo;
2269
- receiptItem.ItemType = type(payment);
2270
- receiptItem.CtAccountNo = ctAccountNo;
2271
-
2272
- await paymentItem.paid(dbTransaction);
2273
- await receipt.newDocumentItem(receiptItem);
2274
- }
2275
-
2276
- const receiptMedia = await receipt.generateReceipt(
2277
- receipt.IssuedById,
2278
- customer,
2279
- dbTransaction,
2280
- );
2281
-
2282
- /*Saving the receipt document and document items to the database*/
2283
- await FinanceCompany._DocumentRepository.create(
2284
- {
2285
- DocNo: receipt.DocNo,
2286
- DocType: receipt.DocType,
2287
- DocDate: receipt.DocDate,
2288
- CompanyId: receipt.CompanyId,
2289
- Currency: receipt.Currency,
2290
- Amount: receipt.Amount,
2291
- Description: receipt.Description,
2292
- Status: receipt.Status,
2293
- IssuedById: receipt.IssuedById,
2294
- IssuedToId: receipt.IssuedToId,
2295
- IssuedToType: receipt.IssuedToType,
2296
- RelatedObjectId: receipt.RelatedObjectId,
2297
- RelatedObjectType: receipt.RelatedObjectType,
2298
- CreatedById: receipt.CreatedById,
2299
- CreatedAt: new Date(),
2300
- UpdatedById: receipt.UpdatedById,
2301
- UpdatedAt: new Date(),
2302
- DocPDFFileMediaId: receiptMedia.PDFMedia.MediaId,
2303
- DocHTMLFileMediaId: receiptMedia.HTMLMedia.MediaId,
2304
- AccSystemRefId: receipt.AccSystemRefId,
2305
- PostedToAccSystemYN: receipt.PostedToAccSystemYN,
2306
- PostedById:
2307
- receipt.PostedToAccSystemYN == 'Y' ? receipt.PostedById : null,
2308
- PostedDateTime: new Date(),
2309
- UseAccSystemDocYN: receipt.UseAccSystemDocYN,
2310
- },
2311
- {
2312
- transaction: dbTransaction,
2313
- },
2314
- );
2315
-
2316
- const receiptItems = await receipt.getDocumentItems(dbTransaction);
2317
-
2318
- for (const receiptItem of receiptItems) {
2319
- await FinanceCompany._DocumentItemRepository.create(
2320
- {
2321
- DocumentItemId: this._createId().toUpperCase(),
2322
- DocNo: receipt.DocNo,
2323
- Name: receiptItem.Name,
2324
- NameBM: receiptItem.NameBM,
2325
- Description: receiptItem.Description,
2326
- ItemId: receiptItem.ItemId,
2327
- ItemType: receiptItem.ItemType,
2328
- ItemSKU: receiptItem.ItemSKU,
2329
- ItemSerialNo: receiptItem.ItemSerialNo,
2330
- Currency: receiptItem.Currency,
2331
- UnitPrice: receiptItem.UnitPrice,
2332
- Quantity: receiptItem.Quantity,
2333
- QuantityUOM: receiptItem.QuantityUOM,
2334
- Amount: receiptItem.Amount,
2335
- TaxCode: receiptItem.TaxCode,
2336
- TaxAmount: receiptItem.TaxAmount,
2337
- TaxRate: receiptItem.TaxRate,
2338
- TaxInclusiveYN: receiptItem.TaxInclusiveYN,
2339
- DtAccountNo: receiptItem.DtAccountNo
2340
- ? receiptItem.DtAccountNo
2341
- : null,
2342
- CtAccountNo: receiptItem.CtAccountNo
2343
- ? receiptItem.CtAccountNo
2344
- : null,
2345
- },
2346
- {
2347
- transaction: dbTransaction,
2348
- },
2349
- );
2350
- }
2351
- //Set the receipt doc no to the payment
2352
- payment.ReceiptDocNo = receipt.DocNo;
2353
- receiptDocNo = receipt.DocNo;
2354
-
2355
- /*Registering the Journal Entries for the transaction*/
2356
- const transactionDate = new Date();
2357
- const paymentPaidWithItems =
2358
- await payment.getPaymentPaidWith(dbTransaction);
2359
-
2360
- for (const paymentPaidWith of paymentPaidWithItems) {
2361
- let paymentMethodType = await PaymentMethodType.initMethodType(
2362
- dbTransaction,
2363
- paymentPaidWith.MethodTypeId,
2364
- );
2365
-
2366
- const journalEntry = new JournalEntry(dbTransaction);
2367
-
2368
- journalEntry.init({
2369
- CompanyId: this.CompanyId,
2370
- Name: 'Collect-payments for ' + payment.PaymentId,
2371
- });
2372
-
2373
- const debitLT = await journalEntry.newLedgerTransaction(
2374
- TransactionTypeOptions.DEBIT,
2375
- );
2376
- debitLT.AccountNo = paymentMethodType.AccountNo;
2377
- debitLT.Currency = paymentPaidWith.Currency;
2378
- debitLT.DebitAmount = paymentPaidWith.Amount;
2379
- debitLT.Date = transactionDate;
2380
- debitLT.Description = 'Payment Received'; //customer.FullName;
2381
- debitLT.Name = customer.FullName;
2382
- debitLT.RelatedObjectId = payment.PaymentId;
2383
- debitLT.RelatedObjectType = 'Payment';
2384
- debitLT.RelatedPaymentId = payment.PaymentId;
2385
-
2386
- const creditLT = await journalEntry.newLedgerTransaction(
2387
- TransactionTypeOptions.CREDIT,
2388
- );
2389
-
2390
- if (ctAccountNo) {
2391
- creditLT.AccountNo = ctAccountNo;
2392
- } else {
2393
- const arAccount = await customer.getAccountReceivable();
2394
- creditLT.AccountNo = arAccount.AccountNo;
2395
- }
2396
- creditLT.Currency = paymentPaidWith.Currency;
2397
- creditLT.CreditAmount = paymentPaidWith.Amount;
2398
- creditLT.Date = transactionDate;
2399
- creditLT.Description = 'Payment Received'; //paymentMethodType.Name;
2400
- creditLT.Name = paymentMethodType.Name;
2401
- creditLT.RelatedObjectId = payment.PaymentId;
2402
- creditLT.RelatedObjectType = type(payment);
2403
- creditLT.RelatedPaymentId = payment.PaymentId;
2404
-
2405
- await this.postJournal(dbTransaction, journalEntry, loginUser);
2406
- }
2407
- break;
2408
- default:
2409
- throw new ClassError(
2410
- 'FinanceCompany',
2411
- 'FinanceCompanyConfirmPaymentErrMsg03',
2412
- `Invalid status.`,
2413
- );
2414
- }
2415
-
2416
- await FinanceCompany._PaymentRepository.update(
2417
- {
2418
- ReceiptDocNo: receiptDocNo,
2419
- Status: payment.Status,
2420
- UpdatedAt: new Date(),
2421
- UpdatedBy: loginUser.ObjectId,
2422
- },
2423
- {
2424
- where: {
2425
- PaymentId: payment.PaymentId,
2426
- },
2427
- transaction: dbTransaction,
2428
- },
2429
- );
2430
- } catch (error) {
2431
- throw error;
2432
- }
2433
- }
2434
- }