@dainamite/billing 0.20.0 → 0.21.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 (598) hide show
  1. package/CHANGELOG.md +90 -0
  2. package/dist/modules/billing/acl.js +13 -1
  3. package/dist/modules/billing/acl.js.map +2 -2
  4. package/dist/modules/billing/api/accounts/[id]/summary/route.js +52 -0
  5. package/dist/modules/billing/api/accounts/[id]/summary/route.js.map +7 -0
  6. package/dist/modules/billing/api/accounts/route.js +46 -4
  7. package/dist/modules/billing/api/accounts/route.js.map +2 -2
  8. package/dist/modules/billing/api/export/account/[id]/route.js +11 -7
  9. package/dist/modules/billing/api/export/account/[id]/route.js.map +2 -2
  10. package/dist/modules/billing/api/invoices/[id]/memos/route.js +54 -0
  11. package/dist/modules/billing/api/invoices/[id]/memos/route.js.map +7 -0
  12. package/dist/modules/billing/api/invoices/[id]/pdf/route.js +69 -0
  13. package/dist/modules/billing/api/invoices/[id]/pdf/route.js.map +7 -0
  14. package/dist/modules/billing/api/invoices/[id]/route.js +44 -24
  15. package/dist/modules/billing/api/invoices/[id]/route.js.map +2 -2
  16. package/dist/modules/billing/api/invoices/[id]/send/route.js +107 -0
  17. package/dist/modules/billing/api/invoices/[id]/send/route.js.map +7 -0
  18. package/dist/modules/billing/api/invoices/add-line/route.js +2 -2
  19. package/dist/modules/billing/api/invoices/add-line/route.js.map +2 -2
  20. package/dist/modules/billing/api/invoices/delete-draft/route.js +33 -0
  21. package/dist/modules/billing/api/invoices/delete-draft/route.js.map +7 -0
  22. package/dist/modules/billing/api/invoices/edit-line/route.js +2 -2
  23. package/dist/modules/billing/api/invoices/edit-line/route.js.map +2 -2
  24. package/dist/modules/billing/api/invoices/post/route.js +2 -2
  25. package/dist/modules/billing/api/invoices/post/route.js.map +2 -2
  26. package/dist/modules/billing/api/invoices/remove-line/route.js +2 -2
  27. package/dist/modules/billing/api/invoices/remove-line/route.js.map +2 -2
  28. package/dist/modules/billing/api/invoices/route.js +45 -46
  29. package/dist/modules/billing/api/invoices/route.js.map +2 -2
  30. package/dist/modules/billing/api/items/bulk/route.js +1 -1
  31. package/dist/modules/billing/api/items/bulk/route.js.map +2 -2
  32. package/dist/modules/billing/api/items/change-price/route.js +44 -0
  33. package/dist/modules/billing/api/items/change-price/route.js.map +7 -0
  34. package/dist/modules/billing/api/items/route.js +19 -7
  35. package/dist/modules/billing/api/items/route.js.map +2 -2
  36. package/dist/modules/billing/api/payment-imports/[id]/route.js +49 -0
  37. package/dist/modules/billing/api/payment-imports/[id]/route.js.map +7 -0
  38. package/dist/modules/billing/api/payment-imports/route.js +57 -0
  39. package/dist/modules/billing/api/payment-imports/route.js.map +7 -0
  40. package/dist/modules/billing/api/payments/[id]/route.js +60 -0
  41. package/dist/modules/billing/api/payments/[id]/route.js.map +7 -0
  42. package/dist/modules/billing/api/payments/allocate/route.js +35 -0
  43. package/dist/modules/billing/api/payments/allocate/route.js.map +7 -0
  44. package/dist/modules/billing/api/payments/assign/route.js +36 -0
  45. package/dist/modules/billing/api/payments/assign/route.js.map +7 -0
  46. package/dist/modules/billing/api/payments/convert-to-topup/route.js +35 -0
  47. package/dist/modules/billing/api/payments/convert-to-topup/route.js.map +7 -0
  48. package/dist/modules/billing/api/payments/ignore/route.js +35 -0
  49. package/dist/modules/billing/api/payments/ignore/route.js.map +7 -0
  50. package/dist/modules/billing/api/payments/import/route.js +63 -0
  51. package/dist/modules/billing/api/payments/import/route.js.map +7 -0
  52. package/dist/modules/billing/api/payments/restore/route.js +35 -0
  53. package/dist/modules/billing/api/payments/restore/route.js.map +7 -0
  54. package/dist/modules/billing/api/payments/route.js +114 -0
  55. package/dist/modules/billing/api/payments/route.js.map +7 -0
  56. package/dist/modules/billing/api/payments/unallocate/route.js +35 -0
  57. package/dist/modules/billing/api/payments/unallocate/route.js.map +7 -0
  58. package/dist/modules/billing/api/payments/unassign/route.js +35 -0
  59. package/dist/modules/billing/api/payments/unassign/route.js.map +7 -0
  60. package/dist/modules/billing/api/run-outcomes/route.js +43 -6
  61. package/dist/modules/billing/api/run-outcomes/route.js.map +2 -2
  62. package/dist/modules/billing/api/runs/[id]/post-drafts/route.js +61 -0
  63. package/dist/modules/billing/api/runs/[id]/post-drafts/route.js.map +7 -0
  64. package/dist/modules/billing/api/runs/[id]/route.js +53 -0
  65. package/dist/modules/billing/api/runs/[id]/route.js.map +7 -0
  66. package/dist/modules/billing/api/runs/retry-failed/route.js +1 -1
  67. package/dist/modules/billing/api/runs/retry-failed/route.js.map +2 -2
  68. package/dist/modules/billing/api/runs/route.js +9 -3
  69. package/dist/modules/billing/api/runs/route.js.map +2 -2
  70. package/dist/modules/billing/api/settings/number-preview/route.js +42 -0
  71. package/dist/modules/billing/api/settings/number-preview/route.js.map +7 -0
  72. package/dist/modules/billing/api/settings/route.js +92 -0
  73. package/dist/modules/billing/api/settings/route.js.map +7 -0
  74. package/dist/modules/billing/api/statements/route.js +3 -0
  75. package/dist/modules/billing/api/statements/route.js.map +2 -2
  76. package/dist/modules/billing/api/test-invoices/wipe/route.js +2 -2
  77. package/dist/modules/billing/api/test-invoices/wipe/route.js.map +2 -2
  78. package/dist/modules/billing/backend/billing/accounts/[id]/page.js +121 -232
  79. package/dist/modules/billing/backend/billing/accounts/[id]/page.js.map +2 -2
  80. package/dist/modules/billing/backend/billing/accounts/create/page.js +6 -0
  81. package/dist/modules/billing/backend/billing/accounts/create/page.js.map +2 -2
  82. package/dist/modules/billing/backend/billing/accounts/page.js +63 -4
  83. package/dist/modules/billing/backend/billing/accounts/page.js.map +2 -2
  84. package/dist/modules/billing/backend/billing/invoices/[id]/page.js +297 -65
  85. package/dist/modules/billing/backend/billing/invoices/[id]/page.js.map +3 -3
  86. package/dist/modules/billing/backend/billing/invoices/page.js +6 -172
  87. package/dist/modules/billing/backend/billing/invoices/page.js.map +2 -2
  88. package/dist/modules/billing/backend/billing/items/[id]/page.js +51 -7
  89. package/dist/modules/billing/backend/billing/items/[id]/page.js.map +2 -2
  90. package/dist/modules/billing/backend/billing/items/create/page.js +9 -6
  91. package/dist/modules/billing/backend/billing/items/create/page.js.map +2 -2
  92. package/dist/modules/billing/backend/billing/items/page.js +8 -2
  93. package/dist/modules/billing/backend/billing/items/page.js.map +2 -2
  94. package/dist/modules/billing/backend/billing/payments/[id]/page.js +297 -0
  95. package/dist/modules/billing/backend/billing/payments/[id]/page.js.map +7 -0
  96. package/dist/modules/billing/backend/billing/payments/[id]/page.meta.js +21 -0
  97. package/dist/modules/billing/backend/billing/payments/[id]/page.meta.js.map +7 -0
  98. package/dist/modules/billing/backend/billing/payments/imports/[id]/page.js +74 -0
  99. package/dist/modules/billing/backend/billing/payments/imports/[id]/page.js.map +7 -0
  100. package/dist/modules/billing/backend/billing/payments/imports/[id]/page.meta.js +21 -0
  101. package/dist/modules/billing/backend/billing/payments/imports/[id]/page.meta.js.map +7 -0
  102. package/dist/modules/billing/backend/billing/payments/page.js +58 -0
  103. package/dist/modules/billing/backend/billing/payments/page.js.map +7 -0
  104. package/dist/modules/billing/backend/billing/payments/page.meta.js +18 -0
  105. package/dist/modules/billing/backend/billing/payments/page.meta.js.map +7 -0
  106. package/dist/modules/billing/backend/billing/runs/[id]/page.js +49 -14
  107. package/dist/modules/billing/backend/billing/runs/[id]/page.js.map +2 -2
  108. package/dist/modules/billing/backend/billing/runs/page.js +58 -7
  109. package/dist/modules/billing/backend/billing/runs/page.js.map +2 -2
  110. package/dist/modules/billing/backend/billing/statements/[id]/page.js +61 -6
  111. package/dist/modules/billing/backend/billing/statements/[id]/page.js.map +2 -2
  112. package/dist/modules/billing/backend/billing/statements/page.js +1 -1
  113. package/dist/modules/billing/backend/billing/statements/page.js.map +2 -2
  114. package/dist/modules/billing/backend/billing/topups/page.js +1 -1
  115. package/dist/modules/billing/backend/billing/topups/page.js.map +2 -2
  116. package/dist/modules/billing/backend/billing/transactions/page.js +1 -1
  117. package/dist/modules/billing/backend/billing/transactions/page.js.map +2 -2
  118. package/dist/modules/billing/backend/billing/usage/create/page.js +3 -2
  119. package/dist/modules/billing/backend/billing/usage/create/page.js.map +2 -2
  120. package/dist/modules/billing/backend/config/billing/page.js +472 -0
  121. package/dist/modules/billing/backend/config/billing/page.js.map +7 -0
  122. package/dist/modules/billing/backend/config/billing/page.meta.js +19 -0
  123. package/dist/modules/billing/backend/config/billing/page.meta.js.map +7 -0
  124. package/dist/modules/billing/cli.js +22 -1
  125. package/dist/modules/billing/cli.js.map +2 -2
  126. package/dist/modules/billing/commands/accounts.js +6 -0
  127. package/dist/modules/billing/commands/accounts.js.map +2 -2
  128. package/dist/modules/billing/commands/index.js +3 -0
  129. package/dist/modules/billing/commands/index.js.map +2 -2
  130. package/dist/modules/billing/commands/invoices.js +269 -167
  131. package/dist/modules/billing/commands/invoices.js.map +2 -2
  132. package/dist/modules/billing/commands/items.js +108 -0
  133. package/dist/modules/billing/commands/items.js.map +2 -2
  134. package/dist/modules/billing/commands/memos.js +179 -0
  135. package/dist/modules/billing/commands/memos.js.map +7 -0
  136. package/dist/modules/billing/commands/payments.js +379 -0
  137. package/dist/modules/billing/commands/payments.js.map +7 -0
  138. package/dist/modules/billing/commands/runs.js +151 -4
  139. package/dist/modules/billing/commands/runs.js.map +2 -2
  140. package/dist/modules/billing/commands/settings.js +59 -0
  141. package/dist/modules/billing/commands/settings.js.map +7 -0
  142. package/dist/modules/billing/commands/topups.js +9 -2
  143. package/dist/modules/billing/commands/topups.js.map +2 -2
  144. package/dist/modules/billing/commands/transactions.js +2 -2
  145. package/dist/modules/billing/commands/transactions.js.map +2 -2
  146. package/dist/modules/billing/components/AccountForm.js +149 -42
  147. package/dist/modules/billing/components/AccountForm.js.map +2 -2
  148. package/dist/modules/billing/components/AccountInvoicesSection.js +64 -0
  149. package/dist/modules/billing/components/AccountInvoicesSection.js.map +7 -0
  150. package/dist/modules/billing/components/AccountPaymentsSection.js +43 -0
  151. package/dist/modules/billing/components/AccountPaymentsSection.js.map +7 -0
  152. package/dist/modules/billing/components/AccountPicker.js +56 -0
  153. package/dist/modules/billing/components/AccountPicker.js.map +7 -0
  154. package/dist/modules/billing/components/AllocatePaymentDialog.js +134 -0
  155. package/dist/modules/billing/components/AllocatePaymentDialog.js.map +7 -0
  156. package/dist/modules/billing/components/BillingDataTable.js +21 -0
  157. package/dist/modules/billing/components/BillingDataTable.js.map +7 -0
  158. package/dist/modules/billing/components/BillingSettingsLink.js +19 -0
  159. package/dist/modules/billing/components/BillingSettingsLink.js.map +7 -0
  160. package/dist/modules/billing/components/ChangeItemPriceDialog.js +158 -0
  161. package/dist/modules/billing/components/ChangeItemPriceDialog.js.map +7 -0
  162. package/dist/modules/billing/components/CreateMemoDialog.js +200 -0
  163. package/dist/modules/billing/components/CreateMemoDialog.js.map +7 -0
  164. package/dist/modules/billing/components/CustomerLabel.js +33 -0
  165. package/dist/modules/billing/components/CustomerLabel.js.map +7 -0
  166. package/dist/modules/billing/components/InvoicesTable.js +270 -0
  167. package/dist/modules/billing/components/InvoicesTable.js.map +7 -0
  168. package/dist/modules/billing/components/ItemForm.js +176 -44
  169. package/dist/modules/billing/components/ItemForm.js.map +2 -2
  170. package/dist/modules/billing/components/ItemPriceField.js +46 -0
  171. package/dist/modules/billing/components/ItemPriceField.js.map +7 -0
  172. package/dist/modules/billing/components/LineFormDialog.js +25 -2
  173. package/dist/modules/billing/components/LineFormDialog.js.map +2 -2
  174. package/dist/modules/billing/components/ManualPaymentDialog.js +122 -0
  175. package/dist/modules/billing/components/ManualPaymentDialog.js.map +7 -0
  176. package/dist/modules/billing/components/MoneyInput.js +14 -0
  177. package/dist/modules/billing/components/MoneyInput.js.map +7 -0
  178. package/dist/modules/billing/components/PaymentUploadDialog.js +115 -0
  179. package/dist/modules/billing/components/PaymentUploadDialog.js.map +7 -0
  180. package/dist/modules/billing/components/PaymentsTable.js +171 -0
  181. package/dist/modules/billing/components/PaymentsTable.js.map +7 -0
  182. package/dist/modules/billing/components/PostRunDraftsButton.js +105 -0
  183. package/dist/modules/billing/components/PostRunDraftsButton.js.map +7 -0
  184. package/dist/modules/billing/components/PrepaidPanel.js +14 -4
  185. package/dist/modules/billing/components/PrepaidPanel.js.map +2 -2
  186. package/dist/modules/billing/components/RunCyclesSection.js +209 -0
  187. package/dist/modules/billing/components/RunCyclesSection.js.map +7 -0
  188. package/dist/modules/billing/components/TestRunActions.js +127 -0
  189. package/dist/modules/billing/components/TestRunActions.js.map +7 -0
  190. package/dist/modules/billing/components/account/AccountItemsTab.js +157 -0
  191. package/dist/modules/billing/components/account/AccountItemsTab.js.map +7 -0
  192. package/dist/modules/billing/components/account/AccountRunBillingDialog.js +122 -0
  193. package/dist/modules/billing/components/account/AccountRunBillingDialog.js.map +7 -0
  194. package/dist/modules/billing/components/account/AccountRunsTab.js +152 -0
  195. package/dist/modules/billing/components/account/AccountRunsTab.js.map +7 -0
  196. package/dist/modules/billing/components/account/AccountStatementsTab.js +98 -0
  197. package/dist/modules/billing/components/account/AccountStatementsTab.js.map +7 -0
  198. package/dist/modules/billing/components/account/AccountSummaryCards.js +125 -0
  199. package/dist/modules/billing/components/account/AccountSummaryCards.js.map +7 -0
  200. package/dist/modules/billing/components/account/AccountUsageTab.js +91 -0
  201. package/dist/modules/billing/components/account/AccountUsageTab.js.map +7 -0
  202. package/dist/modules/billing/components/account/AccountView.js +73 -0
  203. package/dist/modules/billing/components/account/AccountView.js.map +7 -0
  204. package/dist/modules/billing/components/account/accountRow.js +39 -0
  205. package/dist/modules/billing/components/account/accountRow.js.map +7 -0
  206. package/dist/modules/billing/components/customerNames.js +42 -0
  207. package/dist/modules/billing/components/customerNames.js.map +7 -0
  208. package/dist/modules/billing/data/entities.js +560 -4
  209. package/dist/modules/billing/data/entities.js.map +2 -2
  210. package/dist/modules/billing/data/entityIds.js +10 -1
  211. package/dist/modules/billing/data/entityIds.js.map +2 -2
  212. package/dist/modules/billing/data/validators.js +269 -15
  213. package/dist/modules/billing/data/validators.js.map +2 -2
  214. package/dist/modules/billing/events.js +50 -0
  215. package/dist/modules/billing/events.js.map +2 -2
  216. package/dist/modules/billing/i18n/en.json +457 -21
  217. package/dist/modules/billing/i18n/pl.json +457 -21
  218. package/dist/modules/billing/index.js +6 -2
  219. package/dist/modules/billing/index.js.map +2 -2
  220. package/dist/modules/billing/lib/accountFinancials.js +58 -0
  221. package/dist/modules/billing/lib/accountFinancials.js.map +7 -0
  222. package/dist/modules/billing/lib/accountLock.js +14 -0
  223. package/dist/modules/billing/lib/accountLock.js.map +7 -0
  224. package/dist/modules/billing/lib/accountStatus.js +9 -7
  225. package/dist/modules/billing/lib/accountStatus.js.map +2 -2
  226. package/dist/modules/billing/lib/accountSummary.js +106 -0
  227. package/dist/modules/billing/lib/accountSummary.js.map +7 -0
  228. package/dist/modules/billing/lib/allocation.js +356 -0
  229. package/dist/modules/billing/lib/allocation.js.map +7 -0
  230. package/dist/modules/billing/lib/allocationEvents.js +22 -0
  231. package/dist/modules/billing/lib/allocationEvents.js.map +7 -0
  232. package/dist/modules/billing/lib/billPeriod.js +15 -1
  233. package/dist/modules/billing/lib/billPeriod.js.map +2 -2
  234. package/dist/modules/billing/lib/billRunEngine.js +237 -102
  235. package/dist/modules/billing/lib/billRunEngine.js.map +2 -2
  236. package/dist/modules/billing/lib/billingSettings.js +116 -0
  237. package/dist/modules/billing/lib/billingSettings.js.map +7 -0
  238. package/dist/modules/billing/lib/commandRoute.js +81 -0
  239. package/dist/modules/billing/lib/commandRoute.js.map +7 -0
  240. package/dist/modules/billing/lib/customerLookup.js +97 -0
  241. package/dist/modules/billing/lib/customerLookup.js.map +7 -0
  242. package/dist/modules/billing/lib/dateOnly.js +13 -0
  243. package/dist/modules/billing/lib/dateOnly.js.map +7 -0
  244. package/dist/modules/billing/lib/documentParties.js +16 -0
  245. package/dist/modules/billing/lib/documentParties.js.map +7 -0
  246. package/dist/modules/billing/lib/documentTypes.js +27 -0
  247. package/dist/modules/billing/lib/documentTypes.js.map +7 -0
  248. package/dist/modules/billing/lib/dueDate.js +18 -0
  249. package/dist/modules/billing/lib/dueDate.js.map +7 -0
  250. package/dist/modules/billing/lib/iban.js +29 -0
  251. package/dist/modules/billing/lib/iban.js.map +7 -0
  252. package/dist/modules/billing/lib/invoiceDelivery.js +131 -0
  253. package/dist/modules/billing/lib/invoiceDelivery.js.map +7 -0
  254. package/dist/modules/billing/lib/invoiceLanguages.js +16 -0
  255. package/dist/modules/billing/lib/invoiceLanguages.js.map +7 -0
  256. package/dist/modules/billing/lib/invoiceWriter.js +107 -99
  257. package/dist/modules/billing/lib/invoiceWriter.js.map +2 -2
  258. package/dist/modules/billing/lib/itemBillingState.js +27 -0
  259. package/dist/modules/billing/lib/itemBillingState.js.map +7 -0
  260. package/dist/modules/billing/lib/itemPriceChange.js +18 -0
  261. package/dist/modules/billing/lib/itemPriceChange.js.map +7 -0
  262. package/dist/modules/billing/lib/itemSelector.js.map +2 -2
  263. package/dist/modules/billing/lib/lineAmounts.js +32 -0
  264. package/dist/modules/billing/lib/lineAmounts.js.map +7 -0
  265. package/dist/modules/billing/lib/memoGuards.js +25 -0
  266. package/dist/modules/billing/lib/memoGuards.js.map +7 -0
  267. package/dist/modules/billing/lib/navigation.js +48 -0
  268. package/dist/modules/billing/lib/navigation.js.map +7 -0
  269. package/dist/modules/billing/lib/numbering.js +120 -0
  270. package/dist/modules/billing/lib/numbering.js.map +7 -0
  271. package/dist/modules/billing/lib/overdue.js +36 -0
  272. package/dist/modules/billing/lib/overdue.js.map +7 -0
  273. package/dist/modules/billing/lib/paymentCsv.js +125 -0
  274. package/dist/modules/billing/lib/paymentCsv.js.map +7 -0
  275. package/dist/modules/billing/lib/paymentGateway.js +15 -0
  276. package/dist/modules/billing/lib/paymentGateway.js.map +7 -0
  277. package/dist/modules/billing/lib/paymentImport.js +164 -0
  278. package/dist/modules/billing/lib/paymentImport.js.map +7 -0
  279. package/dist/modules/billing/lib/paymentMatcher.js +36 -0
  280. package/dist/modules/billing/lib/paymentMatcher.js.map +7 -0
  281. package/dist/modules/billing/lib/pdf/attachmentStore.js +46 -0
  282. package/dist/modules/billing/lib/pdf/attachmentStore.js.map +7 -0
  283. package/dist/modules/billing/lib/pdf/fonts/embeddedFonts.js +7 -0
  284. package/dist/modules/billing/lib/pdf/fonts/embeddedFonts.js.map +7 -0
  285. package/dist/modules/billing/lib/pdf/invoiceModel.js +194 -0
  286. package/dist/modules/billing/lib/pdf/invoiceModel.js.map +7 -0
  287. package/dist/modules/billing/lib/pdf/invoicePdf.js +165 -0
  288. package/dist/modules/billing/lib/pdf/invoicePdf.js.map +7 -0
  289. package/dist/modules/billing/lib/pdf/invoiceTemplate.js +274 -0
  290. package/dist/modules/billing/lib/pdf/invoiceTemplate.js.map +7 -0
  291. package/dist/modules/billing/lib/prepaidConfig.js +17 -25
  292. package/dist/modules/billing/lib/prepaidConfig.js.map +2 -2
  293. package/dist/modules/billing/lib/prepaidConsume.js +26 -7
  294. package/dist/modules/billing/lib/prepaidConsume.js.map +2 -2
  295. package/dist/modules/billing/lib/prepaidPeriodClose.js +277 -158
  296. package/dist/modules/billing/lib/prepaidPeriodClose.js.map +2 -2
  297. package/dist/modules/billing/lib/prepaidTaxPoint.js +37 -0
  298. package/dist/modules/billing/lib/prepaidTaxPoint.js.map +7 -0
  299. package/dist/modules/billing/lib/seeds/demo.js +51 -3
  300. package/dist/modules/billing/lib/seeds/demo.js.map +2 -2
  301. package/dist/modules/billing/lib/seeds/paymentsSample.js +93 -0
  302. package/dist/modules/billing/lib/seeds/paymentsSample.js.map +7 -0
  303. package/dist/modules/billing/lib/seeds/sellerSettings.js +41 -0
  304. package/dist/modules/billing/lib/seeds/sellerSettings.js.map +7 -0
  305. package/dist/modules/billing/lib/servicePeriods.js +121 -0
  306. package/dist/modules/billing/lib/servicePeriods.js.map +7 -0
  307. package/dist/modules/billing/lib/testRunCleanup.js +24 -0
  308. package/dist/modules/billing/lib/testRunCleanup.js.map +7 -0
  309. package/dist/modules/billing/lib/topupCapture.js +2 -2
  310. package/dist/modules/billing/lib/topupCapture.js.map +2 -2
  311. package/dist/modules/billing/lib/topupReceipt.js +58 -79
  312. package/dist/modules/billing/lib/topupReceipt.js.map +2 -2
  313. package/dist/modules/billing/lib/usageRater.js.map +1 -1
  314. package/dist/modules/billing/lib/usageRunner.js +14 -6
  315. package/dist/modules/billing/lib/usageRunner.js.map +2 -2
  316. package/dist/modules/billing/lib/vat.js +39 -0
  317. package/dist/modules/billing/lib/vat.js.map +7 -0
  318. package/dist/modules/billing/migrations/Migration20261114000000_billing_customer_kind.js +15 -0
  319. package/dist/modules/billing/migrations/Migration20261114000000_billing_customer_kind.js.map +7 -0
  320. package/dist/modules/billing/migrations/Migration20261114100000_billing_cycle_semantics.js +25 -0
  321. package/dist/modules/billing/migrations/Migration20261114100000_billing_cycle_semantics.js.map +7 -0
  322. package/dist/modules/billing/migrations/Migration20261115000000_billing_invoice_ledger.js +115 -0
  323. package/dist/modules/billing/migrations/Migration20261115000000_billing_invoice_ledger.js.map +7 -0
  324. package/dist/modules/billing/migrations/Migration20261116000000_billing_invoice_documents.js +47 -0
  325. package/dist/modules/billing/migrations/Migration20261116000000_billing_invoice_documents.js.map +7 -0
  326. package/dist/modules/billing/migrations/Migration20261117000000_billing_payments.js +89 -0
  327. package/dist/modules/billing/migrations/Migration20261117000000_billing_payments.js.map +7 -0
  328. package/dist/modules/billing/migrations/Migration20261118000000_billing_memos.js +17 -0
  329. package/dist/modules/billing/migrations/Migration20261118000000_billing_memos.js.map +7 -0
  330. package/dist/modules/billing/migrations/Migration20261119000000_billing_operator_feedback.js +27 -0
  331. package/dist/modules/billing/migrations/Migration20261119000000_billing_operator_feedback.js.map +7 -0
  332. package/dist/modules/billing/migrations/Migration20261120000000_billing_allocation_scope_indexes.js +29 -0
  333. package/dist/modules/billing/migrations/Migration20261120000000_billing_allocation_scope_indexes.js.map +7 -0
  334. package/dist/modules/billing/setup.js +7 -184
  335. package/dist/modules/billing/setup.js.map +2 -2
  336. package/dist/modules/billing/subscribers/credit-memo-auto-send.js +16 -0
  337. package/dist/modules/billing/subscribers/credit-memo-auto-send.js.map +7 -0
  338. package/dist/modules/billing/subscribers/debit-memo-apply-credit.js +16 -0
  339. package/dist/modules/billing/subscribers/debit-memo-apply-credit.js.map +7 -0
  340. package/dist/modules/billing/subscribers/debit-memo-auto-send.js +16 -0
  341. package/dist/modules/billing/subscribers/debit-memo-auto-send.js.map +7 -0
  342. package/dist/modules/billing/subscribers/invoice-apply-credit.js +34 -0
  343. package/dist/modules/billing/subscribers/invoice-apply-credit.js.map +7 -0
  344. package/dist/modules/billing/subscribers/invoice-auto-send.js +30 -0
  345. package/dist/modules/billing/subscribers/invoice-auto-send.js.map +7 -0
  346. package/dist/modules/billing/subscribers/payment-captured-to-paid.js +69 -33
  347. package/dist/modules/billing/subscribers/payment-captured-to-paid.js.map +2 -2
  348. package/dist/modules/billing/subscribers/prepaid-topup-captured.js +4 -6
  349. package/dist/modules/billing/subscribers/prepaid-topup-captured.js.map +2 -2
  350. package/generated/entities/billing_account/index.ts +3 -0
  351. package/generated/entities/billing_allocation/index.ts +18 -0
  352. package/generated/entities/billing_document_sequence/index.ts +11 -0
  353. package/generated/entities/billing_invoice/index.ts +38 -0
  354. package/generated/entities/billing_invoice_line/index.ts +24 -0
  355. package/generated/entities/billing_item/index.ts +4 -0
  356. package/generated/entities/billing_payment/index.ts +25 -0
  357. package/generated/entities/billing_payment_import/index.ts +14 -0
  358. package/generated/entities/billing_run/index.ts +0 -1
  359. package/generated/entities/billing_run_outcome/index.ts +1 -0
  360. package/generated/entities/billing_settings/index.ts +26 -0
  361. package/generated/entities/billing_statement/index.ts +1 -0
  362. package/generated/entities.ids.generated.ts +7 -0
  363. package/package.json +5 -1
  364. package/src/modules/billing/__integration__/TC-BILL-004.spec.ts +170 -0
  365. package/src/modules/billing/__integration__/TC-BILL-005.spec.ts +134 -0
  366. package/src/modules/billing/__integration__/TC-BILL-006.spec.ts +101 -0
  367. package/src/modules/billing/__integration__/TC-BILL-007.spec.ts +140 -0
  368. package/src/modules/billing/__integration__/TC-BILL-008.spec.ts +108 -0
  369. package/src/modules/billing/__integration__/TC-BILL-009.spec.ts +113 -0
  370. package/src/modules/billing/__integration__/TC-BILL-010.spec.ts +118 -0
  371. package/src/modules/billing/__integration__/TC-BILL-011.spec.ts +142 -0
  372. package/src/modules/billing/__integration__/TC-BILL-012.spec.ts +115 -0
  373. package/src/modules/billing/__integration__/TC-BILL-013.spec.ts +118 -0
  374. package/src/modules/billing/__integration__/sellerSettings.ts +24 -0
  375. package/src/modules/billing/__tests__/commands/invoices.test.ts +436 -354
  376. package/src/modules/billing/__tests__/commands/itemsBulk.test.ts +33 -0
  377. package/src/modules/billing/__tests__/commands/memos.test.ts +205 -0
  378. package/src/modules/billing/__tests__/commands/payments.test.ts +241 -0
  379. package/src/modules/billing/__tests__/commands/runsDelete.test.ts +146 -0
  380. package/src/modules/billing/__tests__/commands/runsPostDrafts.test.ts +106 -0
  381. package/src/modules/billing/__tests__/commands/topups.test.ts +11 -0
  382. package/src/modules/billing/__tests__/entityRegistry.test.ts +8 -1
  383. package/src/modules/billing/__tests__/helpers/prepaidMockEm.ts +28 -11
  384. package/src/modules/billing/__tests__/lib/accountFinancials.test.ts +53 -0
  385. package/src/modules/billing/__tests__/lib/accountSummary.test.ts +97 -0
  386. package/src/modules/billing/__tests__/lib/allocation.test.ts +319 -0
  387. package/src/modules/billing/__tests__/lib/billRunEngine.test.ts +228 -17
  388. package/src/modules/billing/__tests__/lib/billingSettings.test.ts +109 -0
  389. package/src/modules/billing/__tests__/lib/customerLookup.test.ts +156 -0
  390. package/src/modules/billing/__tests__/lib/dueDate.test.ts +35 -0
  391. package/src/modules/billing/__tests__/lib/iban.test.ts +38 -0
  392. package/src/modules/billing/__tests__/lib/invoiceDelivery.test.ts +189 -0
  393. package/src/modules/billing/__tests__/lib/invoiceWriter.test.ts +212 -10
  394. package/src/modules/billing/__tests__/lib/itemBillingState.test.ts +42 -0
  395. package/src/modules/billing/__tests__/lib/itemPriceChange.test.ts +165 -0
  396. package/src/modules/billing/__tests__/lib/lineAmounts.test.ts +86 -0
  397. package/src/modules/billing/__tests__/lib/navigation.test.ts +71 -0
  398. package/src/modules/billing/__tests__/lib/numbering.test.ts +91 -0
  399. package/src/modules/billing/__tests__/lib/overdue.test.ts +36 -0
  400. package/src/modules/billing/__tests__/lib/paymentCsv.test.ts +148 -0
  401. package/src/modules/billing/__tests__/lib/paymentImport.test.ts +113 -0
  402. package/src/modules/billing/__tests__/lib/paymentsSample.test.ts +73 -0
  403. package/src/modules/billing/__tests__/lib/pdf/invoicePdf.test.ts +199 -0
  404. package/src/modules/billing/__tests__/lib/pdf/invoicePdfService.test.ts +147 -0
  405. package/src/modules/billing/__tests__/lib/prepaidPeriodClose.test.ts +374 -98
  406. package/src/modules/billing/__tests__/lib/prepaidTaxPoint.test.ts +118 -0
  407. package/src/modules/billing/__tests__/lib/seedsDemo.test.ts +53 -2
  408. package/src/modules/billing/__tests__/lib/sellerSettings.test.ts +28 -0
  409. package/src/modules/billing/__tests__/lib/servicePeriods.test.ts +190 -0
  410. package/src/modules/billing/__tests__/lib/topupCapture.test.ts +6 -28
  411. package/src/modules/billing/__tests__/lib/topupReceipt.test.ts +21 -52
  412. package/src/modules/billing/__tests__/lib/vat.test.ts +51 -0
  413. package/src/modules/billing/__tests__/package-isolation.test.ts +22 -8
  414. package/src/modules/billing/__tests__/setup.test.ts +13 -4
  415. package/src/modules/billing/__tests__/validators.test.ts +193 -3
  416. package/src/modules/billing/acl.ts +12 -0
  417. package/src/modules/billing/api/accounts/[id]/summary/route.ts +62 -0
  418. package/src/modules/billing/api/accounts/route.ts +58 -4
  419. package/src/modules/billing/api/export/account/[id]/route.ts +13 -11
  420. package/src/modules/billing/api/invoices/[id]/memos/route.ts +63 -0
  421. package/src/modules/billing/api/invoices/[id]/pdf/route.ts +79 -0
  422. package/src/modules/billing/api/invoices/[id]/route.ts +57 -70
  423. package/src/modules/billing/api/invoices/[id]/send/route.ts +127 -0
  424. package/src/modules/billing/api/invoices/add-line/route.ts +2 -2
  425. package/src/modules/billing/api/invoices/delete-draft/route.ts +33 -0
  426. package/src/modules/billing/api/invoices/edit-line/route.ts +2 -2
  427. package/src/modules/billing/api/invoices/post/route.ts +7 -6
  428. package/src/modules/billing/api/invoices/remove-line/route.ts +2 -2
  429. package/src/modules/billing/api/invoices/route.ts +81 -86
  430. package/src/modules/billing/api/items/bulk/route.ts +2 -1
  431. package/src/modules/billing/api/items/change-price/route.ts +54 -0
  432. package/src/modules/billing/api/items/route.ts +23 -7
  433. package/src/modules/billing/api/payment-imports/[id]/route.ts +49 -0
  434. package/src/modules/billing/api/payment-imports/route.ts +57 -0
  435. package/src/modules/billing/api/payments/[id]/route.ts +63 -0
  436. package/src/modules/billing/api/payments/allocate/route.ts +35 -0
  437. package/src/modules/billing/api/payments/assign/route.ts +36 -0
  438. package/src/modules/billing/api/payments/convert-to-topup/route.ts +35 -0
  439. package/src/modules/billing/api/payments/ignore/route.ts +35 -0
  440. package/src/modules/billing/api/payments/import/route.ts +72 -0
  441. package/src/modules/billing/api/payments/restore/route.ts +35 -0
  442. package/src/modules/billing/api/payments/route.ts +147 -0
  443. package/src/modules/billing/api/payments/unallocate/route.ts +35 -0
  444. package/src/modules/billing/api/payments/unassign/route.ts +35 -0
  445. package/src/modules/billing/api/run-outcomes/route.ts +49 -6
  446. package/src/modules/billing/api/runs/[id]/post-drafts/route.ts +71 -0
  447. package/src/modules/billing/api/runs/[id]/route.ts +59 -0
  448. package/src/modules/billing/api/runs/retry-failed/route.ts +2 -1
  449. package/src/modules/billing/api/runs/route.ts +15 -7
  450. package/src/modules/billing/api/settings/number-preview/route.ts +46 -0
  451. package/src/modules/billing/api/settings/route.ts +116 -0
  452. package/src/modules/billing/api/statements/route.ts +5 -0
  453. package/src/modules/billing/api/test-invoices/wipe/route.ts +2 -2
  454. package/src/modules/billing/backend/billing/accounts/[id]/page.tsx +151 -372
  455. package/src/modules/billing/backend/billing/accounts/create/page.tsx +6 -0
  456. package/src/modules/billing/backend/billing/accounts/page.tsx +70 -4
  457. package/src/modules/billing/backend/billing/invoices/[id]/page.tsx +444 -95
  458. package/src/modules/billing/backend/billing/invoices/page.tsx +8 -227
  459. package/src/modules/billing/backend/billing/items/[id]/page.tsx +75 -6
  460. package/src/modules/billing/backend/billing/items/create/page.tsx +13 -8
  461. package/src/modules/billing/backend/billing/items/page.tsx +18 -2
  462. package/src/modules/billing/backend/billing/payments/[id]/page.meta.ts +19 -0
  463. package/src/modules/billing/backend/billing/payments/[id]/page.tsx +415 -0
  464. package/src/modules/billing/backend/billing/payments/imports/[id]/page.meta.ts +19 -0
  465. package/src/modules/billing/backend/billing/payments/imports/[id]/page.tsx +106 -0
  466. package/src/modules/billing/backend/billing/payments/page.meta.ts +16 -0
  467. package/src/modules/billing/backend/billing/payments/page.tsx +56 -0
  468. package/src/modules/billing/backend/billing/runs/[id]/page.tsx +49 -18
  469. package/src/modules/billing/backend/billing/runs/page.tsx +59 -8
  470. package/src/modules/billing/backend/billing/statements/[id]/page.tsx +86 -5
  471. package/src/modules/billing/backend/billing/statements/page.tsx +1 -1
  472. package/src/modules/billing/backend/billing/topups/page.tsx +1 -1
  473. package/src/modules/billing/backend/billing/transactions/page.tsx +1 -1
  474. package/src/modules/billing/backend/billing/usage/create/page.tsx +3 -2
  475. package/src/modules/billing/backend/config/billing/page.meta.ts +21 -0
  476. package/src/modules/billing/backend/config/billing/page.tsx +549 -0
  477. package/src/modules/billing/cli.ts +31 -1
  478. package/src/modules/billing/commands/accounts.ts +6 -0
  479. package/src/modules/billing/commands/index.ts +3 -0
  480. package/src/modules/billing/commands/invoices.ts +364 -231
  481. package/src/modules/billing/commands/items.ts +143 -0
  482. package/src/modules/billing/commands/memos.ts +218 -0
  483. package/src/modules/billing/commands/payments.ts +428 -0
  484. package/src/modules/billing/commands/runs.ts +205 -3
  485. package/src/modules/billing/commands/settings.ts +77 -0
  486. package/src/modules/billing/commands/topups.ts +12 -3
  487. package/src/modules/billing/commands/transactions.ts +2 -2
  488. package/src/modules/billing/components/AccountForm.tsx +199 -53
  489. package/src/modules/billing/components/AccountInvoicesSection.tsx +75 -0
  490. package/src/modules/billing/components/AccountPaymentsSection.tsx +42 -0
  491. package/src/modules/billing/components/AccountPicker.tsx +70 -0
  492. package/src/modules/billing/components/AllocatePaymentDialog.tsx +167 -0
  493. package/src/modules/billing/components/BillingDataTable.tsx +34 -0
  494. package/src/modules/billing/components/BillingSettingsLink.tsx +25 -0
  495. package/src/modules/billing/components/ChangeItemPriceDialog.tsx +180 -0
  496. package/src/modules/billing/components/CreateMemoDialog.tsx +232 -0
  497. package/src/modules/billing/components/CustomerLabel.tsx +53 -0
  498. package/src/modules/billing/components/InvoicesTable.tsx +344 -0
  499. package/src/modules/billing/components/ItemForm.tsx +233 -44
  500. package/src/modules/billing/components/ItemPriceField.tsx +81 -0
  501. package/src/modules/billing/components/LineFormDialog.tsx +25 -1
  502. package/src/modules/billing/components/ManualPaymentDialog.tsx +140 -0
  503. package/src/modules/billing/components/MoneyInput.tsx +26 -0
  504. package/src/modules/billing/components/PaymentUploadDialog.tsx +148 -0
  505. package/src/modules/billing/components/PaymentsTable.tsx +213 -0
  506. package/src/modules/billing/components/PostRunDraftsButton.tsx +129 -0
  507. package/src/modules/billing/components/PrepaidPanel.tsx +19 -6
  508. package/src/modules/billing/components/RunCyclesSection.tsx +298 -0
  509. package/src/modules/billing/components/TestRunActions.tsx +152 -0
  510. package/src/modules/billing/components/account/AccountItemsTab.tsx +192 -0
  511. package/src/modules/billing/components/account/AccountRunBillingDialog.tsx +147 -0
  512. package/src/modules/billing/components/account/AccountRunsTab.tsx +201 -0
  513. package/src/modules/billing/components/account/AccountStatementsTab.tsx +120 -0
  514. package/src/modules/billing/components/account/AccountSummaryCards.tsx +153 -0
  515. package/src/modules/billing/components/account/AccountUsageTab.tsx +122 -0
  516. package/src/modules/billing/components/account/AccountView.tsx +101 -0
  517. package/src/modules/billing/components/account/accountRow.ts +70 -0
  518. package/src/modules/billing/components/customerNames.ts +62 -0
  519. package/src/modules/billing/data/entities.ts +654 -5
  520. package/src/modules/billing/data/entityIds.ts +9 -0
  521. package/src/modules/billing/data/validators.ts +388 -8
  522. package/src/modules/billing/events.ts +56 -3
  523. package/src/modules/billing/i18n/en.json +457 -21
  524. package/src/modules/billing/i18n/pl.json +457 -21
  525. package/src/modules/billing/index.ts +6 -2
  526. package/src/modules/billing/lib/accountFinancials.ts +84 -0
  527. package/src/modules/billing/lib/accountLock.ts +32 -0
  528. package/src/modules/billing/lib/accountStatus.ts +11 -9
  529. package/src/modules/billing/lib/accountSummary.ts +172 -0
  530. package/src/modules/billing/lib/allocation.ts +515 -0
  531. package/src/modules/billing/lib/allocationEvents.ts +26 -0
  532. package/src/modules/billing/lib/billPeriod.ts +19 -0
  533. package/src/modules/billing/lib/billRunEngine.ts +350 -150
  534. package/src/modules/billing/lib/billingSettings.ts +145 -0
  535. package/src/modules/billing/lib/commandRoute.ts +112 -0
  536. package/src/modules/billing/lib/customerLookup.ts +157 -0
  537. package/src/modules/billing/lib/dateOnly.ts +22 -0
  538. package/src/modules/billing/lib/documentParties.ts +23 -0
  539. package/src/modules/billing/lib/documentTypes.ts +37 -0
  540. package/src/modules/billing/lib/dueDate.ts +26 -0
  541. package/src/modules/billing/lib/iban.ts +36 -0
  542. package/src/modules/billing/lib/invoiceDelivery.ts +197 -0
  543. package/src/modules/billing/lib/invoiceLanguages.ts +34 -0
  544. package/src/modules/billing/lib/invoiceWriter.ts +162 -148
  545. package/src/modules/billing/lib/itemBillingState.ts +67 -0
  546. package/src/modules/billing/lib/itemPriceChange.ts +56 -0
  547. package/src/modules/billing/lib/itemSelector.ts +4 -1
  548. package/src/modules/billing/lib/lineAmounts.ts +61 -0
  549. package/src/modules/billing/lib/memoGuards.ts +41 -0
  550. package/src/modules/billing/lib/navigation.ts +64 -0
  551. package/src/modules/billing/lib/numbering.ts +174 -0
  552. package/src/modules/billing/lib/overdue.ts +60 -0
  553. package/src/modules/billing/lib/paymentCsv.ts +171 -0
  554. package/src/modules/billing/lib/paymentGateway.ts +52 -0
  555. package/src/modules/billing/lib/paymentImport.ts +228 -0
  556. package/src/modules/billing/lib/paymentMatcher.ts +88 -0
  557. package/src/modules/billing/lib/pdf/attachmentStore.ts +60 -0
  558. package/src/modules/billing/lib/pdf/fonts/NotoSans-Bold.ttf +0 -0
  559. package/src/modules/billing/lib/pdf/fonts/NotoSans-Regular.ttf +0 -0
  560. package/src/modules/billing/lib/pdf/fonts/OFL.txt +93 -0
  561. package/src/modules/billing/lib/pdf/fonts/embeddedFonts.ts +9 -0
  562. package/src/modules/billing/lib/pdf/invoiceModel.ts +328 -0
  563. package/src/modules/billing/lib/pdf/invoicePdf.ts +216 -0
  564. package/src/modules/billing/lib/pdf/invoiceTemplate.ts +338 -0
  565. package/src/modules/billing/lib/prepaidConfig.ts +27 -36
  566. package/src/modules/billing/lib/prepaidConsume.ts +29 -6
  567. package/src/modules/billing/lib/prepaidPeriodClose.ts +407 -239
  568. package/src/modules/billing/lib/prepaidTaxPoint.ts +76 -0
  569. package/src/modules/billing/lib/seeds/demo.ts +79 -6
  570. package/src/modules/billing/lib/seeds/paymentsSample.ts +136 -0
  571. package/src/modules/billing/lib/seeds/sellerSettings.ts +78 -0
  572. package/src/modules/billing/lib/servicePeriods.ts +226 -0
  573. package/src/modules/billing/lib/testRunCleanup.ts +42 -0
  574. package/src/modules/billing/lib/topupCapture.ts +3 -4
  575. package/src/modules/billing/lib/topupReceipt.ts +70 -103
  576. package/src/modules/billing/lib/usageRater.ts +1 -1
  577. package/src/modules/billing/lib/usageRunner.ts +17 -5
  578. package/src/modules/billing/lib/vat.ts +56 -0
  579. package/src/modules/billing/migrations/Migration20261114000000_billing_customer_kind.ts +25 -0
  580. package/src/modules/billing/migrations/Migration20261114100000_billing_cycle_semantics.ts +37 -0
  581. package/src/modules/billing/migrations/Migration20261115000000_billing_invoice_ledger.ts +132 -0
  582. package/src/modules/billing/migrations/Migration20261116000000_billing_invoice_documents.ts +58 -0
  583. package/src/modules/billing/migrations/Migration20261117000000_billing_payments.ts +97 -0
  584. package/src/modules/billing/migrations/Migration20261118000000_billing_memos.ts +21 -0
  585. package/src/modules/billing/migrations/Migration20261119000000_billing_operator_feedback.ts +39 -0
  586. package/src/modules/billing/migrations/Migration20261120000000_billing_allocation_scope_indexes.ts +35 -0
  587. package/src/modules/billing/setup.ts +11 -237
  588. package/src/modules/billing/subscribers/credit-memo-auto-send.ts +15 -0
  589. package/src/modules/billing/subscribers/debit-memo-apply-credit.ts +15 -0
  590. package/src/modules/billing/subscribers/debit-memo-auto-send.ts +15 -0
  591. package/src/modules/billing/subscribers/invoice-apply-credit.ts +39 -0
  592. package/src/modules/billing/subscribers/invoice-auto-send.ts +46 -0
  593. package/src/modules/billing/subscribers/payment-captured-to-paid.ts +84 -62
  594. package/src/modules/billing/subscribers/prepaid-topup-captured.ts +4 -8
  595. package/dist/modules/billing/lib/invoiceStatus.js +0 -38
  596. package/dist/modules/billing/lib/invoiceStatus.js.map +0 -7
  597. package/src/modules/billing/__tests__/lib/invoiceStatus.test.ts +0 -97
  598. package/src/modules/billing/lib/invoiceStatus.ts +0 -61
@@ -0,0 +1,127 @@
1
+ "use client";
2
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
3
+ import * as React from "react";
4
+ import { Eraser, Trash2 } from "lucide-react";
5
+ import { useT } from "@open-mercato/shared/lib/i18n/context";
6
+ import { Button } from "@open-mercato/ui/primitives/button";
7
+ import { flash } from "@open-mercato/ui/backend/FlashMessages";
8
+ import { useConfirmDialog } from "@open-mercato/ui/backend/confirm-dialog";
9
+ import { apiCallOrThrow } from "@open-mercato/ui/backend/utils/apiCall";
10
+ import { normalizeCrudServerError } from "@open-mercato/ui/backend/utils/serverErrors";
11
+ function billRunMode(run) {
12
+ if (run.dry_run) return "dry";
13
+ if (run.test_mode) return "test";
14
+ return "real";
15
+ }
16
+ function useTestRunCleanup() {
17
+ const t = useT();
18
+ const { confirm, ConfirmDialogElement } = useConfirmDialog();
19
+ const [busy, setBusy] = React.useState(null);
20
+ const wipe = React.useCallback(
21
+ async (runId) => {
22
+ const ok = await confirm({
23
+ title: t("billing.runs.wipe.confirm.title", "Wipe test invoices?"),
24
+ text: t(
25
+ "billing.runs.wipe.confirm.text",
26
+ "Every test invoice from this Bill Run will be permanently deleted. The run itself stays. This cannot be undone."
27
+ ),
28
+ confirmText: t("billing.runs.wipe.confirm.button", "Delete test invoices"),
29
+ variant: "destructive"
30
+ });
31
+ if (!ok) return null;
32
+ setBusy("wipe");
33
+ try {
34
+ const call = await apiCallOrThrow("/api/billing/test-invoices/wipe", {
35
+ method: "POST",
36
+ headers: { "Content-Type": "application/json" },
37
+ body: JSON.stringify({ billRunId: runId })
38
+ });
39
+ const removed = call.result?.invoicesRemoved ?? 0;
40
+ flash(`${t("billing.runs.wipe.success", "Test invoices removed")} (${removed})`, "success");
41
+ return removed;
42
+ } catch (err) {
43
+ const { message } = normalizeCrudServerError(err);
44
+ flash(message || t("billing.runs.wipe.error", "Wipe failed"), "error");
45
+ return null;
46
+ } finally {
47
+ setBusy(null);
48
+ }
49
+ },
50
+ [confirm, t]
51
+ );
52
+ const remove = React.useCallback(
53
+ async (params) => {
54
+ const text = params.mode === "test" && typeof params.draftsCount === "number" && params.draftsCount > 0 ? `${t("billing.runs.delete.confirm.text_with_invoices", "The run, its results and its test invoices will be permanently deleted. Test invoices:")} ${params.draftsCount}` : t("billing.runs.delete.confirm.text", "The run and its results will be permanently deleted.");
55
+ const ok = await confirm({
56
+ title: t("billing.runs.delete.confirm.title", "Delete this run?"),
57
+ text,
58
+ confirmText: t("billing.runs.delete.confirm.button", "Delete run"),
59
+ variant: "destructive"
60
+ });
61
+ if (!ok) return null;
62
+ setBusy("delete");
63
+ try {
64
+ const call = await apiCallOrThrow(
65
+ `/api/billing/runs/${encodeURIComponent(params.runId)}`,
66
+ { method: "DELETE" }
67
+ );
68
+ flash(t("billing.runs.delete.success", "Run deleted"), "success");
69
+ return call.result?.invoicesRemoved ?? 0;
70
+ } catch (err) {
71
+ const { message } = normalizeCrudServerError(err);
72
+ flash(message || t("billing.runs.delete.error", "Delete failed"), "error");
73
+ return null;
74
+ } finally {
75
+ setBusy(null);
76
+ }
77
+ },
78
+ [confirm, t]
79
+ );
80
+ return { wipe, remove, busy, ConfirmDialogElement };
81
+ }
82
+ function TestRunActions({ runId, mode, draftsCount, onWiped, onDeleted }) {
83
+ const t = useT();
84
+ const { wipe, remove, busy, ConfirmDialogElement } = useTestRunCleanup();
85
+ if (mode === "real") return null;
86
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
87
+ mode === "test" ? /* @__PURE__ */ jsxs(
88
+ Button,
89
+ {
90
+ type: "button",
91
+ variant: "outline",
92
+ disabled: busy !== null,
93
+ onClick: async () => {
94
+ const removed = await wipe(runId);
95
+ if (removed !== null) onWiped?.(removed);
96
+ },
97
+ children: [
98
+ /* @__PURE__ */ jsx(Eraser, { size: 16 }),
99
+ busy === "wipe" ? t("billing.runs.wipe.in_progress", "Wiping\u2026") : t("billing.runs.wipe.action", "Wipe test invoices")
100
+ ]
101
+ }
102
+ ) : null,
103
+ /* @__PURE__ */ jsxs(
104
+ Button,
105
+ {
106
+ type: "button",
107
+ variant: "destructive",
108
+ disabled: busy !== null,
109
+ onClick: async () => {
110
+ const removed = await remove({ runId, mode, draftsCount });
111
+ if (removed !== null) onDeleted?.(removed);
112
+ },
113
+ children: [
114
+ /* @__PURE__ */ jsx(Trash2, { size: 16 }),
115
+ busy === "delete" ? t("billing.runs.delete.in_progress", "Deleting\u2026") : t("billing.runs.delete.action", "Delete run")
116
+ ]
117
+ }
118
+ ),
119
+ ConfirmDialogElement
120
+ ] });
121
+ }
122
+ export {
123
+ TestRunActions,
124
+ billRunMode,
125
+ useTestRunCleanup
126
+ };
127
+ //# sourceMappingURL=TestRunActions.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/modules/billing/components/TestRunActions.tsx"],
4
+ "sourcesContent": ["\"use client\"\nimport * as React from 'react'\nimport { Eraser, Trash2 } from 'lucide-react'\nimport { useT } from '@open-mercato/shared/lib/i18n/context'\nimport { Button } from '@open-mercato/ui/primitives/button'\nimport { flash } from '@open-mercato/ui/backend/FlashMessages'\nimport { useConfirmDialog } from '@open-mercato/ui/backend/confirm-dialog'\nimport { apiCallOrThrow } from '@open-mercato/ui/backend/utils/apiCall'\nimport { normalizeCrudServerError } from '@open-mercato/ui/backend/utils/serverErrors'\n\nexport type BillRunMode = 'real' | 'test' | 'dry'\n\nexport function billRunMode(run: { dry_run?: boolean | null; test_mode?: boolean | null }): BillRunMode {\n if (run.dry_run) return 'dry'\n if (run.test_mode) return 'test'\n return 'real'\n}\n\nexport type TestRunActionsProps = {\n runId: string\n mode: BillRunMode\n /** Test drafts the run wrote (from the run summary), for the confirm text. */\n draftsCount?: number\n onWiped?: (invoicesRemoved: number) => void\n onDeleted?: (invoicesRemoved: number) => void\n}\n\n/**\n * Confirm-then-call cleanup flows for a test / dry Bill Run (Spec F, F1),\n * shared by the run detail page, the runs list row action and the account\n * \"Run billing\" dialog. Each call resolves to the removed invoice count, or\n * `null` when the operator cancelled or the call failed (already flashed).\n */\nexport function useTestRunCleanup() {\n const t = useT()\n const { confirm, ConfirmDialogElement } = useConfirmDialog()\n const [busy, setBusy] = React.useState<'wipe' | 'delete' | null>(null)\n\n const wipe = React.useCallback(\n async (runId: string): Promise<number | null> => {\n const ok = await confirm({\n title: t('billing.runs.wipe.confirm.title', 'Wipe test invoices?'),\n text: t(\n 'billing.runs.wipe.confirm.text',\n 'Every test invoice from this Bill Run will be permanently deleted. The run itself stays. This cannot be undone.',\n ),\n confirmText: t('billing.runs.wipe.confirm.button', 'Delete test invoices'),\n variant: 'destructive',\n })\n if (!ok) return null\n setBusy('wipe')\n try {\n const call = await apiCallOrThrow<{ invoicesRemoved: number }>('/api/billing/test-invoices/wipe', {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ billRunId: runId }),\n })\n const removed = call.result?.invoicesRemoved ?? 0\n flash(`${t('billing.runs.wipe.success', 'Test invoices removed')} (${removed})`, 'success')\n return removed\n } catch (err) {\n const { message } = normalizeCrudServerError(err)\n flash(message || t('billing.runs.wipe.error', 'Wipe failed'), 'error')\n return null\n } finally {\n setBusy(null)\n }\n },\n [confirm, t],\n )\n\n const remove = React.useCallback(\n async (params: { runId: string; mode: BillRunMode; draftsCount?: number }): Promise<number | null> => {\n const text =\n params.mode === 'test' && typeof params.draftsCount === 'number' && params.draftsCount > 0\n ? `${t('billing.runs.delete.confirm.text_with_invoices', 'The run, its results and its test invoices will be permanently deleted. Test invoices:')} ${params.draftsCount}`\n : t('billing.runs.delete.confirm.text', 'The run and its results will be permanently deleted.')\n const ok = await confirm({\n title: t('billing.runs.delete.confirm.title', 'Delete this run?'),\n text,\n confirmText: t('billing.runs.delete.confirm.button', 'Delete run'),\n variant: 'destructive',\n })\n if (!ok) return null\n setBusy('delete')\n try {\n const call = await apiCallOrThrow<{ invoicesRemoved: number }>(\n `/api/billing/runs/${encodeURIComponent(params.runId)}`,\n { method: 'DELETE' },\n )\n flash(t('billing.runs.delete.success', 'Run deleted'), 'success')\n return call.result?.invoicesRemoved ?? 0\n } catch (err) {\n const { message } = normalizeCrudServerError(err)\n flash(message || t('billing.runs.delete.error', 'Delete failed'), 'error')\n return null\n } finally {\n setBusy(null)\n }\n },\n [confirm, t],\n )\n\n return { wipe, remove, busy, ConfirmDialogElement }\n}\n\n/**\n * Cleanup buttons for a test / dry Bill Run: \"Wipe test invoices\" keeps the\n * run, \"Delete run\" removes it with its test invoices. Renders nothing for\n * real runs (they are never deleted).\n */\nexport function TestRunActions({ runId, mode, draftsCount, onWiped, onDeleted }: TestRunActionsProps) {\n const t = useT()\n const { wipe, remove, busy, ConfirmDialogElement } = useTestRunCleanup()\n\n if (mode === 'real') return null\n return (\n <>\n {mode === 'test' ? (\n <Button\n type=\"button\"\n variant=\"outline\"\n disabled={busy !== null}\n onClick={async () => {\n const removed = await wipe(runId)\n if (removed !== null) onWiped?.(removed)\n }}\n >\n <Eraser size={16} />\n {busy === 'wipe'\n ? t('billing.runs.wipe.in_progress', 'Wiping\u2026')\n : t('billing.runs.wipe.action', 'Wipe test invoices')}\n </Button>\n ) : null}\n <Button\n type=\"button\"\n variant=\"destructive\"\n disabled={busy !== null}\n onClick={async () => {\n const removed = await remove({ runId, mode, draftsCount })\n if (removed !== null) onDeleted?.(removed)\n }}\n >\n <Trash2 size={16} />\n {busy === 'delete'\n ? t('billing.runs.delete.in_progress', 'Deleting\u2026')\n : t('billing.runs.delete.action', 'Delete run')}\n </Button>\n {ConfirmDialogElement}\n </>\n )\n}\n"],
5
+ "mappings": ";AAqHI,mBAWM,KATF,YAFJ;AApHJ,YAAY,WAAW;AACvB,SAAS,QAAQ,cAAc;AAC/B,SAAS,YAAY;AACrB,SAAS,cAAc;AACvB,SAAS,aAAa;AACtB,SAAS,wBAAwB;AACjC,SAAS,sBAAsB;AAC/B,SAAS,gCAAgC;AAIlC,SAAS,YAAY,KAA4E;AACtG,MAAI,IAAI,QAAS,QAAO;AACxB,MAAI,IAAI,UAAW,QAAO;AAC1B,SAAO;AACT;AAiBO,SAAS,oBAAoB;AAClC,QAAM,IAAI,KAAK;AACf,QAAM,EAAE,SAAS,qBAAqB,IAAI,iBAAiB;AAC3D,QAAM,CAAC,MAAM,OAAO,IAAI,MAAM,SAAmC,IAAI;AAErE,QAAM,OAAO,MAAM;AAAA,IACjB,OAAO,UAA0C;AAC/C,YAAM,KAAK,MAAM,QAAQ;AAAA,QACvB,OAAO,EAAE,mCAAmC,qBAAqB;AAAA,QACjE,MAAM;AAAA,UACJ;AAAA,UACA;AAAA,QACF;AAAA,QACA,aAAa,EAAE,oCAAoC,sBAAsB;AAAA,QACzE,SAAS;AAAA,MACX,CAAC;AACD,UAAI,CAAC,GAAI,QAAO;AAChB,cAAQ,MAAM;AACd,UAAI;AACF,cAAM,OAAO,MAAM,eAA4C,mCAAmC;AAAA,UAChG,QAAQ;AAAA,UACR,S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Q,IAAI;AAAA,MACd;AAAA,IACF;AAAA,IACA,CAAC,SAAS,CAAC;AAAA,EACb;AAEA,SAAO,EAAE,MAAM,QAAQ,MAAM,qBAAqB;AACpD;AAOO,SAAS,eAAe,EAAE,OAAO,MAAM,aAAa,SAAS,UAAU,GAAwB;AACpG,QAAM,IAAI,KAAK;AACf,QAAM,EAAE,MAAM,QAAQ,MAAM,qBAAqB,IAAI,kBAAkB;AAEvE,MAAI,SAAS,OAAQ,QAAO;AAC5B,SACE,iCACG;AAAA,aAAS,SACR;AAAA,MAAC;AAAA;AAAA,QACC,MAAK;AAAA,QACL,SAAQ;AAAA,QACR,UAAU,SAAS;AAAA,QACnB,SAAS,YAAY;AACnB,gBAAM,UAAU,MAAM,KAAK,KAAK;AAChC,cAAI,YAAY,KAAM,WAAU,OAAO;AAAA,QACzC;AAAA,QAEA;AAAA,8BAAC,UAAO,MAAM,IAAI;AAAA,UACjB,SAAS,SACN,EAAE,iCAAiC,cAAS,IAC5C,EAAE,4BAA4B,oBAAoB;AAAA;AAAA;AAAA,IACxD,IACE;AAAA,IACJ;AAAA,MAAC;AAAA;AAAA,QACC,MAAK;AAAA,QACL,SAAQ;AAAA,QACR,UAAU,SAAS;AAAA,QACnB,SAAS,YAAY;AACnB,gBAAM,UAAU,MAAM,OAAO,EAAE,OAAO,MAAM,YAAY,CAAC;AACzD,cAAI,YAAY,KAAM,aAAY,OAAO;AAAA,QAC3C;AAAA,QAEA;AAAA,8BAAC,UAAO,MAAM,IAAI;AAAA,UACjB,SAAS,WACN,EAAE,mCAAmC,gBAAW,IAChD,EAAE,8BAA8B,YAAY;AAAA;AAAA;AAAA,IAClD;AAAA,IACC;AAAA,KACH;AAEJ;",
6
+ "names": []
7
+ }
@@ -0,0 +1,157 @@
1
+ "use client";
2
+ import { jsx } from "react/jsx-runtime";
3
+ import * as React from "react";
4
+ import { useRouter } from "next/navigation";
5
+ import { useT } from "@open-mercato/shared/lib/i18n/context";
6
+ import { BillingDataTable as DataTable } from "../BillingDataTable.js";
7
+ import { Tag } from "@open-mercato/ui/primitives/tag";
8
+ import { readApiResultOrThrow } from "@open-mercato/ui/backend/utils/apiCall";
9
+ import { itemBillingState } from "../../lib/itemBillingState.js";
10
+ import { formatDate } from "./accountRow.js";
11
+ function localToday() {
12
+ const now = /* @__PURE__ */ new Date();
13
+ const month = String(now.getMonth() + 1).padStart(2, "0");
14
+ const date = String(now.getDate()).padStart(2, "0");
15
+ return `${now.getFullYear()}-${month}-${date}`;
16
+ }
17
+ function stateVariant(state) {
18
+ switch (state.status) {
19
+ case "billed":
20
+ case "current":
21
+ case "fully_billed":
22
+ return "success";
23
+ case "awaiting_run":
24
+ case "due":
25
+ return "warning";
26
+ case "metered":
27
+ return "info";
28
+ default:
29
+ return "neutral";
30
+ }
31
+ }
32
+ function AccountItemsTab({ accountId, currencyCode }) {
33
+ const t = useT();
34
+ const router = useRouter();
35
+ const [rows, setRows] = React.useState([]);
36
+ const [page, setPage] = React.useState(1);
37
+ const [total, setTotal] = React.useState(0);
38
+ const [totalPages, setTotalPages] = React.useState(1);
39
+ const [loading, setLoading] = React.useState(true);
40
+ const pageSize = 25;
41
+ const today = React.useMemo(() => localToday(), []);
42
+ const load = React.useCallback(async () => {
43
+ setLoading(true);
44
+ try {
45
+ const result = await readApiResultOrThrow(
46
+ `/api/billing/items?billAccountId=${accountId}&page=${page}&pageSize=${pageSize}&sortField=createdAt&sortDir=desc`
47
+ );
48
+ setRows(result.items ?? []);
49
+ setTotal(result.total ?? 0);
50
+ setTotalPages(result.totalPages ?? 1);
51
+ } finally {
52
+ setLoading(false);
53
+ }
54
+ }, [accountId, page]);
55
+ React.useEffect(() => {
56
+ void load();
57
+ }, [load]);
58
+ const typeLabel = React.useCallback(
59
+ (row) => {
60
+ if (row.type === "one_time") return t("billing.items.form.type.one_time", "One-time charge");
61
+ if (row.type === "usage") return t("billing.items.form.type.usage", "Usage (metered)");
62
+ return row.charge_timing === "in_arrears" ? t("billing.accounts.items.recurring_arrears", "Recurring \xB7 in arrears") : t("billing.accounts.items.recurring_advance", "Recurring \xB7 in advance");
63
+ },
64
+ [t]
65
+ );
66
+ const rateLabel = React.useCallback(
67
+ (row) => {
68
+ const rate = row.rate_json ?? {};
69
+ const amount = typeof rate.amount === "number" ? rate.amount : typeof rate.unit_price === "number" ? rate.unit_price : null;
70
+ if (amount === null) {
71
+ return "model" in rate ? `${t("billing.accounts.items.tiered", "Tiered")} (${String(rate.model)})` : "\u2014";
72
+ }
73
+ const perUnit = row.type === "usage" && row.uom_code ? ` / ${row.uom_code}` : "";
74
+ const basis = row.price_basis === "gross" ? t("billing.items.price_basis.gross", "Gross") : t("billing.items.price_basis.net", "Net");
75
+ return `${amount}${currencyCode ? ` ${currencyCode}` : ""}${perUnit} \xB7 ${basis}`;
76
+ },
77
+ [currencyCode, t]
78
+ );
79
+ const stateLabel = React.useCallback(
80
+ (state) => {
81
+ const date = state.date ? formatDate(state.date) : "";
82
+ switch (state.status) {
83
+ case "inactive":
84
+ return t("billing.common.inactive", "Inactive");
85
+ case "metered":
86
+ return t("billing.accounts.items.state.metered", "Billed from usage");
87
+ case "scheduled":
88
+ return `${t("billing.accounts.items.state.scheduled", "Starts")} ${date}`;
89
+ case "awaiting_run":
90
+ return t("billing.accounts.items.state.awaiting_run", "Not billed yet");
91
+ case "billed":
92
+ return t("billing.accounts.items.state.billed", "Billed");
93
+ case "current":
94
+ return `${t("billing.accounts.items.state.current", "Billed through")} ${date}`;
95
+ case "due":
96
+ return `${t("billing.accounts.items.state.due", "Due since")} ${date}`;
97
+ case "fully_billed":
98
+ return t("billing.accounts.items.state.fully_billed", "Fully billed");
99
+ }
100
+ },
101
+ [t]
102
+ );
103
+ const columns = React.useMemo(
104
+ () => [
105
+ {
106
+ accessorKey: "description",
107
+ header: t("billing.items.columns.description", "Description"),
108
+ cell: ({ row }) => /* @__PURE__ */ jsx("span", { className: "text-sm font-medium text-primary", children: row.original.description })
109
+ },
110
+ {
111
+ accessorKey: "type",
112
+ header: t("billing.items.columns.type", "Type"),
113
+ cell: ({ row }) => /* @__PURE__ */ jsx("span", { className: "text-sm", children: typeLabel(row.original) })
114
+ },
115
+ {
116
+ accessorKey: "rate_json",
117
+ header: t("billing.items.form.rate.title", "Rate"),
118
+ cell: ({ row }) => /* @__PURE__ */ jsx("span", { className: "text-sm tabular-nums", children: rateLabel(row.original) })
119
+ },
120
+ {
121
+ accessorKey: "bill_start_date",
122
+ header: t("billing.accounts.items.term", "Term"),
123
+ cell: ({ row }) => `${formatDate(row.original.bill_start_date)} \u2013 ${row.original.bill_end_date ? formatDate(row.original.bill_end_date) : t("billing.accounts.items.open_ended", "open-ended")}`
124
+ },
125
+ {
126
+ accessorKey: "billed_to_date",
127
+ header: t("billing.accounts.items.billed_through", "Billed through"),
128
+ cell: ({ row }) => row.original.type === "recurring" && row.original.billed_to_date ? formatDate(row.original.billed_to_date) : "\u2014"
129
+ },
130
+ {
131
+ id: "billing_state",
132
+ header: t("billing.accounts.items.state", "Billing"),
133
+ cell: ({ row }) => {
134
+ const state = itemBillingState(row.original, today);
135
+ return /* @__PURE__ */ jsx(Tag, { variant: stateVariant(state), children: stateLabel(state) });
136
+ }
137
+ }
138
+ ],
139
+ [rateLabel, stateLabel, t, today, typeLabel]
140
+ );
141
+ return /* @__PURE__ */ jsx(
142
+ DataTable,
143
+ {
144
+ title: t("billing.accounts.detail.items.title", "Items"),
145
+ columns,
146
+ data: rows,
147
+ isLoading: loading,
148
+ onRowClick: (row) => router.push(`/backend/billing/items/${row.id}`),
149
+ pagination: { page, pageSize, total, totalPages, onPageChange: setPage },
150
+ emptyState: /* @__PURE__ */ jsx("div", { className: "rounded-lg border bg-card p-6 text-center text-sm text-muted-foreground", children: t("billing.accounts.detail.items.empty", "No items for this account yet.") })
151
+ }
152
+ );
153
+ }
154
+ export {
155
+ AccountItemsTab
156
+ };
157
+ //# sourceMappingURL=AccountItemsTab.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../../src/modules/billing/components/account/AccountItemsTab.tsx"],
4
+ "sourcesContent": ["\"use client\"\nimport * as React from 'react'\nimport { useRouter } from 'next/navigation'\nimport type { LegacyColumnDef as ColumnDef } from '@tanstack/react-table/legacy'\nimport { useT } from '@open-mercato/shared/lib/i18n/context'\nimport { BillingDataTable as DataTable } from '../BillingDataTable'\nimport { Tag } from '@open-mercato/ui/primitives/tag'\nimport { readApiResultOrThrow } from '@open-mercato/ui/backend/utils/apiCall'\nimport { itemBillingState, type ItemBillingState } from '../../lib/itemBillingState'\nimport { formatDate } from './accountRow'\n\n// Snake_case projection of `GET /api/billing/items` (see its `fields`).\ntype AccountItemRow = {\n id: string\n description: string\n type: 'one_time' | 'recurring' | 'usage'\n bill_start_date: string\n bill_end_date: string | null\n billed_to_date: string | null\n rate_json: Record<string, unknown> | null\n charge_timing: 'in_advance' | 'in_arrears'\n price_basis?: 'net' | 'gross'\n uom_code: string | null\n is_active: boolean\n}\n\nfunction localToday(): string {\n const now = new Date()\n const month = String(now.getMonth() + 1).padStart(2, '0')\n const date = String(now.getDate()).padStart(2, '0')\n return `${now.getFullYear()}-${month}-${date}`\n}\n\nfunction stateVariant(state: ItemBillingState): 'success' | 'warning' | 'neutral' | 'info' {\n switch (state.status) {\n case 'billed':\n case 'current':\n case 'fully_billed':\n return 'success'\n case 'awaiting_run':\n case 'due':\n return 'warning'\n case 'metered':\n return 'info'\n default:\n return 'neutral'\n }\n}\n\n/**\n * The account's billing items (workspace Items tab): what each item costs,\n * its term, how far it has been billed and \u2014 against today \u2014 whether a Bill\n * Run still has to charge it.\n */\nexport function AccountItemsTab({ accountId, currencyCode }: { accountId: string; currencyCode?: string }) {\n const t = useT()\n const router = useRouter()\n const [rows, setRows] = React.useState<AccountItemRow[]>([])\n const [page, setPage] = React.useState(1)\n const [total, setTotal] = React.useState(0)\n const [totalPages, setTotalPages] = React.useState(1)\n const [loading, setLoading] = React.useState(true)\n const pageSize = 25\n const today = React.useMemo(() => localToday(), [])\n\n const load = React.useCallback(async () => {\n setLoading(true)\n try {\n const result = await readApiResultOrThrow<{ items: AccountItemRow[]; total: number; totalPages: number }>(\n `/api/billing/items?billAccountId=${accountId}&page=${page}` + `&pageSize=${pageSize}&sortField=createdAt&sortDir=desc`,\n )\n setRows(result.items ?? [])\n setTotal(result.total ?? 0)\n setTotalPages(result.totalPages ?? 1)\n } finally {\n setLoading(false)\n }\n }, [accountId, page])\n\n React.useEffect(() => {\n void load()\n }, [load])\n\n const typeLabel = React.useCallback(\n (row: AccountItemRow) => {\n if (row.type === 'one_time') return t('billing.items.form.type.one_time', 'One-time charge')\n if (row.type === 'usage') return t('billing.items.form.type.usage', 'Usage (metered)')\n return row.charge_timing === 'in_arrears'\n ? t('billing.accounts.items.recurring_arrears', 'Recurring \u00B7 in arrears')\n : t('billing.accounts.items.recurring_advance', 'Recurring \u00B7 in advance')\n },\n [t],\n )\n\n const rateLabel = React.useCallback(\n (row: AccountItemRow) => {\n const rate = row.rate_json ?? {}\n const amount = typeof rate.amount === 'number' ? rate.amount : typeof rate.unit_price === 'number' ? rate.unit_price : null\n if (amount === null) {\n return 'model' in rate ? `${t('billing.accounts.items.tiered', 'Tiered')} (${String(rate.model)})` : '\u2014'\n }\n const perUnit = row.type === 'usage' && row.uom_code ? ` / ${row.uom_code}` : ''\n const basis = row.price_basis === 'gross' ? t('billing.items.price_basis.gross', 'Gross') : t('billing.items.price_basis.net', 'Net')\n return `${amount}${currencyCode ? ` ${currencyCode}` : ''}${perUnit} \u00B7 ${basis}`\n },\n [currencyCode, t],\n )\n\n const stateLabel = React.useCallback(\n (state: ItemBillingState) => {\n const date = state.date ? formatDate(state.date) : ''\n switch (state.status) {\n case 'inactive':\n return t('billing.common.inactive', 'Inactive')\n case 'metered':\n return t('billing.accounts.items.state.metered', 'Billed from usage')\n case 'scheduled':\n return `${t('billing.accounts.items.state.scheduled', 'Starts')} ${date}`\n case 'awaiting_run':\n return t('billing.accounts.items.state.awaiting_run', 'Not billed yet')\n case 'billed':\n return t('billing.accounts.items.state.billed', 'Billed')\n case 'current':\n return `${t('billing.accounts.items.state.current', 'Billed through')} ${date}`\n case 'due':\n return `${t('billing.accounts.items.state.due', 'Due since')} ${date}`\n case 'fully_billed':\n return t('billing.accounts.items.state.fully_billed', 'Fully billed')\n }\n },\n [t],\n )\n\n const columns = React.useMemo<ColumnDef<AccountItemRow>[]>(\n () => [\n {\n accessorKey: 'description',\n header: t('billing.items.columns.description', 'Description'),\n cell: ({ row }) => <span className=\"text-sm font-medium text-primary\">{row.original.description}</span>,\n },\n {\n accessorKey: 'type',\n header: t('billing.items.columns.type', 'Type'),\n cell: ({ row }) => <span className=\"text-sm\">{typeLabel(row.original)}</span>,\n },\n {\n accessorKey: 'rate_json',\n header: t('billing.items.form.rate.title', 'Rate'),\n cell: ({ row }) => <span className=\"text-sm tabular-nums\">{rateLabel(row.original)}</span>,\n },\n {\n accessorKey: 'bill_start_date',\n header: t('billing.accounts.items.term', 'Term'),\n cell: ({ row }) =>\n `${formatDate(row.original.bill_start_date)} \u2013 ${\n row.original.bill_end_date ? formatDate(row.original.bill_end_date) : t('billing.accounts.items.open_ended', 'open-ended')\n }`,\n },\n {\n accessorKey: 'billed_to_date',\n header: t('billing.accounts.items.billed_through', 'Billed through'),\n cell: ({ row }) =>\n row.original.type === 'recurring' && row.original.billed_to_date ? formatDate(row.original.billed_to_date) : '\u2014',\n },\n {\n id: 'billing_state',\n header: t('billing.accounts.items.state', 'Billing'),\n cell: ({ row }) => {\n const state = itemBillingState(row.original, today)\n return <Tag variant={stateVariant(state)}>{stateLabel(state)}</Tag>\n },\n },\n ],\n [rateLabel, stateLabel, t, today, typeLabel],\n )\n\n return (\n <DataTable\n title={t('billing.accounts.detail.items.title', 'Items')}\n columns={columns}\n data={rows}\n isLoading={loading}\n onRowClick={(row) => router.push(`/backend/billing/items/${row.id}`)}\n pagination={{ page, pageSize, total, totalPages, onPageChange: setPage }}\n emptyState={\n <div className=\"rounded-lg border bg-card p-6 text-center text-sm text-muted-foreground\">\n {t('billing.accounts.detail.items.empty', 'No items for this account yet.')}\n </div>\n }\n />\n )\n}\n"],
5
+ "mappings": ";AA0I2B;AAzI3B,YAAY,WAAW;AACvB,SAAS,iBAAiB;AAE1B,SAAS,YAAY;AACrB,SAAS,oBAAoB,iBAAiB;AAC9C,SAAS,WAAW;AACpB,SAAS,4BAA4B;AACrC,SAAS,wBAA+C;AACxD,SAAS,kBAAkB;AAiB3B,SAAS,aAAqB;AAC5B,QAAM,MAAM,oBAAI,KAAK;AACrB,QAAM,QAAQ,OAAO,IAAI,SAAS,IAAI,CAAC,EAAE,SAAS,GAAG,GAAG;AACxD,QAAM,OAAO,OAAO,IAAI,QAAQ,CAAC,EAAE,SAAS,GAAG,GAAG;AAClD,SAAO,GAAG,IAAI,YAAY,CAAC,IAAI,KAAK,IAAI,IAAI;AAC9C;AAEA,SAAS,aAAa,OAAqE;AACzF,UAAQ,MAAM,QAAQ;AAAA,IACpB,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AAAA,IACL,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT;AACE,aAAO;AAAA,EACX;AACF;AAOO,SAAS,gBAAgB,EAAE,WAAW,aAAa,GAAiD;AACzG,QAAM,IAAI,KAAK;AACf,QAAM,SAAS,UAAU;AACzB,QAAM,CAAC,MAAM,OAAO,IAAI,MAAM,SAA2B,CAAC,CAAC;AAC3D,QAAM,CAAC,MAAM,OAAO,IAAI,MAAM,SAAS,CAAC;AACxC,QAAM,CAAC,OAAO,QAAQ,IAAI,MAAM,SAAS,CAAC;AAC1C,QAAM,CAAC,YAAY,aAAa,IAAI,MAAM,SAAS,CAAC;AACpD,QAAM,CAAC,SAAS,UAAU,IAAI,MAAM,SAAS,IAAI;AACjD,QAAM,WAAW;AACjB,QAAM,QAAQ,MAAM,QAAQ,MAAM,WAAW,GAAG,CAAC,CAAC;AAElD,QAAM,OAAO,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 import { jsx, jsxs } from "react/jsx-runtime";
3
+ import * as React from "react";
4
+ import { Play } from "lucide-react";
5
+ import { useT } from "@open-mercato/shared/lib/i18n/context";
6
+ import { Button } from "@open-mercato/ui/primitives/button";
7
+ import { Input } from "@open-mercato/ui/primitives/input";
8
+ import { Kbd, KbdShortcut } from "@open-mercato/ui/primitives/kbd";
9
+ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@open-mercato/ui/primitives/select";
10
+ import {
11
+ Dialog,
12
+ DialogContent,
13
+ DialogDescription,
14
+ DialogFooter,
15
+ DialogHeader,
16
+ DialogTitle
17
+ } from "@open-mercato/ui/primitives/dialog";
18
+ import { flash } from "@open-mercato/ui/backend/FlashMessages";
19
+ import { apiCallOrThrow, readApiResultOrThrow } from "@open-mercato/ui/backend/utils/apiCall";
20
+ import { normalizeCrudServerError } from "@open-mercato/ui/backend/utils/serverErrors";
21
+ function todayIso() {
22
+ return (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
23
+ }
24
+ function AccountRunBillingDialog({ accountId, open, onOpenChange, onCompleted }) {
25
+ const t = useT();
26
+ const [mode, setMode] = React.useState("test");
27
+ const [asOfDate, setAsOfDate] = React.useState(todayIso());
28
+ const [running, setRunning] = React.useState(false);
29
+ React.useEffect(() => {
30
+ if (open) {
31
+ setMode("test");
32
+ setAsOfDate(todayIso());
33
+ }
34
+ }, [open]);
35
+ const handleRun = React.useCallback(async () => {
36
+ setRunning(true);
37
+ try {
38
+ const call = await apiCallOrThrow("/api/billing/runs", {
39
+ method: "POST",
40
+ headers: { "Content-Type": "application/json" },
41
+ body: JSON.stringify({ mode, asOfDate, scopedAccountIds: [accountId] })
42
+ });
43
+ const billRunId = call.result?.billRunId;
44
+ if (!billRunId) throw new Error(t("billing.runs.trigger.error", "Failed to trigger run"));
45
+ const outcomes = await readApiResultOrThrow(
46
+ `/api/billing/run-outcomes?billRunId=${encodeURIComponent(billRunId)}&pageSize=1`
47
+ );
48
+ const outcome = outcomes.items?.[0];
49
+ if (!outcome) {
50
+ flash(t("billing.accounts.run.nothing_due", "Nothing is due up to this date \u2014 the next bill date is later."), "info");
51
+ } else if (outcome.status === "failed") {
52
+ flash(`${t("billing.accounts.run.failed", "Bill run failed")}: ${outcome.error_message ?? ""}`, "error");
53
+ } else {
54
+ const cycles = (outcome.cycles ?? []).filter((cycle) => cycle.status !== "empty").length;
55
+ flash(`${t("billing.accounts.run.done", "Bill run completed \u2014 bill dates with charges:")} ${cycles}`, "success");
56
+ }
57
+ onCompleted(billRunId);
58
+ } catch (err) {
59
+ const { message } = normalizeCrudServerError(err);
60
+ flash(message || t("billing.runs.trigger.error", "Failed to trigger run"), "error");
61
+ } finally {
62
+ setRunning(false);
63
+ }
64
+ }, [accountId, asOfDate, mode, onCompleted, t]);
65
+ const handleKeyDown = React.useCallback(
66
+ (event) => {
67
+ if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
68
+ event.preventDefault();
69
+ if (!running) void handleRun();
70
+ }
71
+ },
72
+ [handleRun, running]
73
+ );
74
+ return /* @__PURE__ */ jsx(Dialog, { open, onOpenChange: (next) => !running ? onOpenChange(next) : void 0, children: /* @__PURE__ */ jsxs(DialogContent, { onKeyDown: handleKeyDown, children: [
75
+ /* @__PURE__ */ jsxs(DialogHeader, { children: [
76
+ /* @__PURE__ */ jsx(DialogTitle, { children: t("billing.accounts.run.title", "Run billing for this account") }),
77
+ /* @__PURE__ */ jsx(DialogDescription, { children: t(
78
+ "billing.accounts.run.description",
79
+ "Bills every due cycle of this account up to the as-of date. Test and dry runs change nothing on the account."
80
+ ) })
81
+ ] }),
82
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-4 sm:grid-cols-2", children: [
83
+ /* @__PURE__ */ jsxs("label", { className: "flex flex-col gap-1 text-sm", children: [
84
+ /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: t("billing.runs.trigger.dialog.mode", "Mode") }),
85
+ /* @__PURE__ */ jsxs(Select, { value: mode, onValueChange: (next) => setMode(next === "real" || next === "dry" ? next : "test"), children: [
86
+ /* @__PURE__ */ jsx(SelectTrigger, { children: /* @__PURE__ */ jsx(SelectValue, {}) }),
87
+ /* @__PURE__ */ jsxs(SelectContent, { children: [
88
+ /* @__PURE__ */ jsx(SelectItem, { value: "test", children: t("billing.runs.trigger.dialog.mode.test", "Test \u2014 drafts tagged for cleanup") }),
89
+ /* @__PURE__ */ jsx(SelectItem, { value: "dry", children: t("billing.runs.trigger.dialog.mode.dry", "Dry-run \u2014 no rows written") }),
90
+ /* @__PURE__ */ jsx(SelectItem, { value: "real", children: t("billing.runs.trigger.dialog.mode.real", "Real \u2014 create draft invoices") })
91
+ ] })
92
+ ] })
93
+ ] }),
94
+ /* @__PURE__ */ jsxs("label", { className: "flex flex-col gap-1 text-sm", children: [
95
+ /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: t("billing.runs.trigger.dialog.as_of_date", "As-of date") }),
96
+ /* @__PURE__ */ jsx(Input, { type: "date", value: asOfDate, onChange: (event) => setAsOfDate(event.currentTarget.value) })
97
+ ] })
98
+ ] }),
99
+ /* @__PURE__ */ jsxs(DialogFooter, { className: "flex items-center justify-between gap-3 sm:justify-between", children: [
100
+ /* @__PURE__ */ jsxs("span", { className: "text-xs text-muted-foreground", children: [
101
+ /* @__PURE__ */ jsx(KbdShortcut, { keys: ["Ctrl/\u2318", "Enter"] }),
102
+ " ",
103
+ t("billing.accounts.run.submit_hint", "to run,"),
104
+ " ",
105
+ /* @__PURE__ */ jsx(Kbd, { children: "Esc" }),
106
+ " ",
107
+ t("billing.accounts.run.close_hint", "to close")
108
+ ] }),
109
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
110
+ /* @__PURE__ */ jsx(Button, { type: "button", variant: "outline", onClick: () => onOpenChange(false), disabled: running, children: t("billing.common.cancel", "Cancel") }),
111
+ /* @__PURE__ */ jsxs(Button, { type: "button", onClick: () => void handleRun(), disabled: running, children: [
112
+ /* @__PURE__ */ jsx(Play, { size: 16 }),
113
+ running ? t("billing.accounts.run.running", "Running\u2026") : t("billing.accounts.run.action", "Run billing")
114
+ ] })
115
+ ] })
116
+ ] })
117
+ ] }) });
118
+ }
119
+ export {
120
+ AccountRunBillingDialog
121
+ };
122
+ //# sourceMappingURL=AccountRunBillingDialog.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../../src/modules/billing/components/account/AccountRunBillingDialog.tsx"],
4
+ "sourcesContent": ["\"use client\"\nimport * as React from 'react'\nimport { Play } from 'lucide-react'\nimport { useT } from '@open-mercato/shared/lib/i18n/context'\nimport { Button } from '@open-mercato/ui/primitives/button'\nimport { Input } from '@open-mercato/ui/primitives/input'\nimport { Kbd, KbdShortcut } from '@open-mercato/ui/primitives/kbd'\nimport { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@open-mercato/ui/primitives/select'\nimport {\n Dialog,\n DialogContent,\n DialogDescription,\n DialogFooter,\n DialogHeader,\n DialogTitle,\n} from '@open-mercato/ui/primitives/dialog'\nimport { flash } from '@open-mercato/ui/backend/FlashMessages'\nimport { apiCallOrThrow, readApiResultOrThrow } from '@open-mercato/ui/backend/utils/apiCall'\nimport { normalizeCrudServerError } from '@open-mercato/ui/backend/utils/serverErrors'\nimport type { BillRunMode } from '../TestRunActions'\n\n/**\n * \"Run billing\" for one account (Spec E, E2-D3 / Spec F, F8): mode (Test by\n * default) + as-of date \u2192 `POST /api/billing/runs` with\n * `scopedAccountIds: [id]`. On success the dialog closes with a one-line\n * summary; the workspace switches to its Bill runs tab, where the run (with\n * its mode, documents and cleanup actions) is listed.\n */\n\ntype OutcomeRow = { status: string; error_message: string | null; cycles: Array<{ status: string }> | null }\n\nfunction todayIso(): string {\n return new Date().toISOString().slice(0, 10)\n}\n\nexport type AccountRunBillingDialogProps = {\n accountId: string\n open: boolean\n onOpenChange: (open: boolean) => void\n /** Called after a run finished \u2014 the workspace closes the dialog and refreshes. */\n onCompleted: (billRunId: string) => void\n}\n\nexport function AccountRunBillingDialog({ accountId, open, onOpenChange, onCompleted }: AccountRunBillingDialogProps) {\n const t = useT()\n const [mode, setMode] = React.useState<BillRunMode>('test')\n const [asOfDate, setAsOfDate] = React.useState<string>(todayIso())\n const [running, setRunning] = React.useState(false)\n\n React.useEffect(() => {\n if (open) {\n setMode('test')\n setAsOfDate(todayIso())\n }\n }, [open])\n\n const handleRun = React.useCallback(async () => {\n setRunning(true)\n try {\n const call = await apiCallOrThrow<{ billRunId: string | null }>('/api/billing/runs', {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ mode, asOfDate, scopedAccountIds: [accountId] }),\n })\n const billRunId = call.result?.billRunId\n if (!billRunId) throw new Error(t('billing.runs.trigger.error', 'Failed to trigger run'))\n const outcomes = await readApiResultOrThrow<{ items: OutcomeRow[] }>(\n `/api/billing/run-outcomes?billRunId=${encodeURIComponent(billRunId)}&pageSize=1`,\n )\n const outcome = outcomes.items?.[0]\n if (!outcome) {\n flash(t('billing.accounts.run.nothing_due', 'Nothing is due up to this date \u2014 the next bill date is later.'), 'info')\n } else if (outcome.status === 'failed') {\n flash(`${t('billing.accounts.run.failed', 'Bill run failed')}: ${outcome.error_message ?? ''}`, 'error')\n } else {\n const cycles = (outcome.cycles ?? []).filter((cycle) => cycle.status !== 'empty').length\n flash(`${t('billing.accounts.run.done', 'Bill run completed \u2014 bill dates with charges:')} ${cycles}`, 'success')\n }\n onCompleted(billRunId)\n } catch (err) {\n const { message } = normalizeCrudServerError(err)\n flash(message || t('billing.runs.trigger.error', 'Failed to trigger run'), 'error')\n } finally {\n setRunning(false)\n }\n }, [accountId, asOfDate, mode, onCompleted, t])\n\n const handleKeyDown = React.useCallback(\n (event: React.KeyboardEvent<HTMLDivElement>) => {\n if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {\n event.preventDefault()\n if (!running) void handleRun()\n }\n },\n [handleRun, running],\n )\n\n return (\n <Dialog open={open} onOpenChange={(next) => (!running ? onOpenChange(next) : undefined)}>\n <DialogContent onKeyDown={handleKeyDown}>\n <DialogHeader>\n <DialogTitle>{t('billing.accounts.run.title', 'Run billing for this account')}</DialogTitle>\n <DialogDescription>\n {t(\n 'billing.accounts.run.description',\n 'Bills every due cycle of this account up to the as-of date. Test and dry runs change nothing on the account.',\n )}\n </DialogDescription>\n </DialogHeader>\n <div className=\"grid grid-cols-1 gap-4 sm:grid-cols-2\">\n <label className=\"flex flex-col gap-1 text-sm\">\n <span className=\"text-muted-foreground\">{t('billing.runs.trigger.dialog.mode', 'Mode')}</span>\n <Select value={mode} onValueChange={(next) => setMode(next === 'real' || next === 'dry' ? next : 'test')}>\n <SelectTrigger>\n <SelectValue />\n </SelectTrigger>\n <SelectContent>\n <SelectItem value=\"test\">{t('billing.runs.trigger.dialog.mode.test', 'Test \u2014 drafts tagged for cleanup')}</SelectItem>\n <SelectItem value=\"dry\">{t('billing.runs.trigger.dialog.mode.dry', 'Dry-run \u2014 no rows written')}</SelectItem>\n <SelectItem value=\"real\">{t('billing.runs.trigger.dialog.mode.real', 'Real \u2014 create draft invoices')}</SelectItem>\n </SelectContent>\n </Select>\n </label>\n <label className=\"flex flex-col gap-1 text-sm\">\n <span className=\"text-muted-foreground\">{t('billing.runs.trigger.dialog.as_of_date', 'As-of date')}</span>\n <Input type=\"date\" value={asOfDate} onChange={(event) => setAsOfDate(event.currentTarget.value)} />\n </label>\n </div>\n <DialogFooter className=\"flex items-center justify-between gap-3 sm:justify-between\">\n <span className=\"text-xs text-muted-foreground\">\n <KbdShortcut keys={['Ctrl/\u2318', 'Enter']} /> {t('billing.accounts.run.submit_hint', 'to run,')} <Kbd>Esc</Kbd>{' '}\n {t('billing.accounts.run.close_hint', 'to close')}\n </span>\n <div className=\"flex items-center gap-2\">\n <Button type=\"button\" variant=\"outline\" onClick={() => onOpenChange(false)} disabled={running}>\n {t('billing.common.cancel', 'Cancel')}\n </Button>\n <Button type=\"button\" onClick={() => void handleRun()} disabled={running}>\n <Play size={16} />\n {running ? t('billing.accounts.run.running', 'Running\u2026') : t('billing.accounts.run.action', 'Run billing')}\n </Button>\n </div>\n </DialogFooter>\n </DialogContent>\n </Dialog>\n )\n}\n"],
5
+ "mappings": ";AAoGQ,SACE,KADF;AAnGR,YAAY,WAAW;AACvB,SAAS,YAAY;AACrB,SAAS,YAAY;AACrB,SAAS,cAAc;AACvB,SAAS,aAAa;AACtB,SAAS,KAAK,mBAAmB;AACjC,SAAS,QAAQ,eAAe,YAAY,eAAe,mBAAmB;AAC9E;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,aAAa;AACtB,SAAS,gBAAgB,4BAA4B;AACrD,SAAS,gCAAgC;AAazC,SAAS,WAAmB;AAC1B,UAAO,oBAAI,KAAK,GAAE,YAAY,EAAE,MAAM,GAAG,EAAE;AAC7C;AAUO,SAAS,wBAAwB,EAAE,WAAW,MAAM,cAAc,YAAY,GAAiC;AACpH,QAAM,IAAI,KAAK;AACf,QAAM,CAAC,MAAM,OAAO,IAAI,MAAM,SAAsB,MAAM;AAC1D,QAAM,CAAC,UAAU,WAAW,IAAI,MAAM,SAAiB,SAAS,CAAC;AACjE,QAAM,CAAC,SAAS,UAAU,IAAI,MAAM,SAAS,KAAK;AAElD,QAAM,UAAU,MAAM;AACpB,QAAI,MAAM;AACR,cAAQ,MAAM;AACd,kBAAY,SAAS,CAAC;AAAA,IACxB;AAAA,EACF,GAAG,CAAC,IAAI,CAAC;AAET,QAAM,YAAY,MAAM,YAAY,YAAY;AAC9C,eAAW,IAAI;AACf,QAAI;AACF,YAAM,OAAO,MAAM,eAA6C,qBAAqB;AAAA,QACnF,QAAQ;AAAA,QACR,SAAS,EAAE,gBAAgB,mBAAmB;AAAA,QAC9C,MAAM,KAAK,UAAU,EAAE,MAAM,UAAU,kBAAkB,CAAC,SAAS,EAAE,CAAC;AAAA,MACxE,CAAC;AACD,YAAM,YAAY,KAAK,QAAQ;AAC/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AC,UAAO,MAAK,UAAS,SAAS,MAAM,KAAK,UAAU,GAAG,UAAU,SAC/D;AAAA,8BAAC,QAAK,MAAM,IAAI;AAAA,UACf,UAAU,EAAE,gCAAgC,eAAU,IAAI,EAAE,+BAA+B,aAAa;AAAA,WAC3G;AAAA,SACF;AAAA,OACF;AAAA,KACF,GACF;AAEJ;",
6
+ "names": []
7
+ }
@@ -0,0 +1,152 @@
1
+ "use client";
2
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
3
+ import * as React from "react";
4
+ import Link from "next/link";
5
+ import { useT } from "@open-mercato/shared/lib/i18n/context";
6
+ import { BillingDataTable as DataTable } from "../BillingDataTable.js";
7
+ import { Tag } from "@open-mercato/ui/primitives/tag";
8
+ import { readApiResultOrThrow } from "@open-mercato/ui/backend/utils/apiCall";
9
+ import { RowActions } from "@open-mercato/ui/backend/RowActions";
10
+ import { useTestRunCleanup } from "../TestRunActions.js";
11
+ const PAGE_SIZE = 20;
12
+ function statusVariant(status) {
13
+ if (status === "success") return "success";
14
+ if (status === "failed") return "error";
15
+ if (status === "success_with_warnings" || status.startsWith("skipped")) return "warning";
16
+ return "neutral";
17
+ }
18
+ function AccountRunsTab({ accountId, refreshKey = 0 }) {
19
+ const t = useT();
20
+ const [rows, setRows] = React.useState([]);
21
+ const [page, setPage] = React.useState(1);
22
+ const [total, setTotal] = React.useState(0);
23
+ const [totalPages, setTotalPages] = React.useState(1);
24
+ const [loading, setLoading] = React.useState(true);
25
+ const [error, setError] = React.useState(null);
26
+ const cleanup = useTestRunCleanup();
27
+ const load = React.useCallback(async () => {
28
+ setLoading(true);
29
+ setError(null);
30
+ try {
31
+ const params = new URLSearchParams({
32
+ billAccountId: accountId,
33
+ page: String(page),
34
+ pageSize: String(PAGE_SIZE),
35
+ sortField: "createdAt",
36
+ sortDir: "desc"
37
+ });
38
+ const result = await readApiResultOrThrow(`/api/billing/run-outcomes?${params.toString()}`);
39
+ setRows(result.items ?? []);
40
+ setTotal(result.total ?? 0);
41
+ setTotalPages(result.totalPages ?? 1);
42
+ } catch {
43
+ setError(t("billing.accounts.runs.error", "Failed to load bill runs"));
44
+ } finally {
45
+ setLoading(false);
46
+ }
47
+ }, [accountId, page, t]);
48
+ React.useEffect(() => {
49
+ void load();
50
+ }, [load, refreshKey]);
51
+ const columns = React.useMemo(
52
+ () => [
53
+ {
54
+ accessorKey: "created_at",
55
+ header: t("billing.accounts.runs.columns.when", "Run at"),
56
+ cell: ({ row }) => /* @__PURE__ */ jsx(Link, { href: `/backend/billing/runs/${row.original.bill_run_id}`, className: "text-primary hover:underline", children: new Date(row.original.created_at).toLocaleString() })
57
+ },
58
+ {
59
+ accessorKey: "run_mode",
60
+ header: t("billing.runs.detail.field.mode", "Mode"),
61
+ cell: ({ row }) => row.original.run_mode === "dry" ? /* @__PURE__ */ jsx(Tag, { variant: "warning", children: t("billing.runs.mode.dry_run", "Dry-run") }) : row.original.run_mode === "test" ? /* @__PURE__ */ jsx(Tag, { variant: "warning", children: t("billing.runs.mode.test", "Test") }) : /* @__PURE__ */ jsx(Tag, { variant: "default", children: t("billing.runs.mode.real", "Real") })
62
+ },
63
+ {
64
+ accessorKey: "run_as_of_date",
65
+ header: t("billing.runs.columns.as_of_date", "As-of date"),
66
+ cell: ({ row }) => row.original.run_as_of_date ?? "\u2014"
67
+ },
68
+ {
69
+ accessorKey: "status",
70
+ header: t("billing.runs.outcomes.columns.status", "Status"),
71
+ cell: ({ row }) => /* @__PURE__ */ jsx(Tag, { variant: statusVariant(row.original.status), children: row.original.status })
72
+ },
73
+ {
74
+ id: "cycles",
75
+ header: t("billing.accounts.runs.columns.cycles", "Bill dates"),
76
+ cell: ({ row }) => {
77
+ const cycles = row.original.cycles ?? [];
78
+ if (cycles.length === 0) return "\u2014";
79
+ return cycles.map((cycle) => cycle.bill_date).join(", ");
80
+ }
81
+ },
82
+ {
83
+ id: "documents",
84
+ header: t("billing.accounts.runs.columns.documents", "Documents"),
85
+ cell: ({ row }) => {
86
+ const cycles = row.original.cycles ?? [];
87
+ const links = cycles.flatMap(
88
+ (cycle) => cycle.invoice_id ? [
89
+ /* @__PURE__ */ jsx(Link, { href: `/backend/billing/invoices/${cycle.invoice_id}`, className: "text-primary hover:underline", children: cycle.invoice_number ?? cycle.invoice_id.slice(0, 8) }, cycle.invoice_id)
90
+ ] : cycle.statement_id ? [
91
+ /* @__PURE__ */ jsxs(Link, { href: `/backend/billing/statements/${cycle.statement_id}`, className: "text-primary hover:underline", children: [
92
+ t("billing.runs.cycles.statement", "Statement"),
93
+ " ",
94
+ cycle.bill_date
95
+ ] }, cycle.statement_id)
96
+ ] : []
97
+ );
98
+ if (links.length === 0) return row.original.error_message ?? "\u2014";
99
+ return /* @__PURE__ */ jsx("span", { className: "flex flex-wrap gap-x-3 gap-y-1", children: links });
100
+ }
101
+ }
102
+ ],
103
+ [t]
104
+ );
105
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
106
+ /* @__PURE__ */ jsx(
107
+ DataTable,
108
+ {
109
+ title: t("billing.accounts.tabs.runs", "Bill runs"),
110
+ columns,
111
+ data: rows,
112
+ isLoading: loading,
113
+ error,
114
+ pagination: { page, pageSize: PAGE_SIZE, total, totalPages, onPageChange: setPage },
115
+ emptyState: t("billing.accounts.runs.empty", "No bill runs for this account yet."),
116
+ rowActions: (row) => /* @__PURE__ */ jsx(
117
+ RowActions,
118
+ {
119
+ items: [
120
+ { id: "open", label: t("billing.runs.actions.open", "Open"), href: `/backend/billing/runs/${row.bill_run_id}` },
121
+ ...row.run_mode === "test" ? [
122
+ {
123
+ id: "wipe",
124
+ label: t("billing.runs.wipe.action", "Wipe test invoices"),
125
+ onSelect: async () => {
126
+ if (await cleanup.wipe(row.bill_run_id) !== null) void load();
127
+ }
128
+ }
129
+ ] : [],
130
+ ...row.run_mode === "test" || row.run_mode === "dry" ? [
131
+ {
132
+ id: "delete",
133
+ label: t("billing.runs.delete.action", "Delete run"),
134
+ destructive: true,
135
+ onSelect: async () => {
136
+ const removed = await cleanup.remove({ runId: row.bill_run_id, mode: row.run_mode ?? "test" });
137
+ if (removed !== null) void load();
138
+ }
139
+ }
140
+ ] : []
141
+ ]
142
+ }
143
+ )
144
+ }
145
+ ),
146
+ cleanup.ConfirmDialogElement
147
+ ] });
148
+ }
149
+ export {
150
+ AccountRunsTab
151
+ };
152
+ //# sourceMappingURL=AccountRunsTab.js.map