@dainamite/billing 0.19.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.
- package/CHANGELOG.md +172 -0
- package/README.md +14 -0
- package/dist/modules/billing/acl.js +13 -1
- package/dist/modules/billing/acl.js.map +2 -2
- package/dist/modules/billing/api/accounts/[id]/summary/route.js +52 -0
- package/dist/modules/billing/api/accounts/[id]/summary/route.js.map +7 -0
- package/dist/modules/billing/api/accounts/route.js +46 -4
- package/dist/modules/billing/api/accounts/route.js.map +2 -2
- package/dist/modules/billing/api/export/account/[id]/route.js +16 -12
- package/dist/modules/billing/api/export/account/[id]/route.js.map +2 -2
- package/dist/modules/billing/api/invoices/[id]/memos/route.js +54 -0
- package/dist/modules/billing/api/invoices/[id]/memos/route.js.map +7 -0
- package/dist/modules/billing/api/invoices/[id]/pdf/route.js +69 -0
- package/dist/modules/billing/api/invoices/[id]/pdf/route.js.map +7 -0
- package/dist/modules/billing/api/invoices/[id]/route.js +44 -24
- package/dist/modules/billing/api/invoices/[id]/route.js.map +2 -2
- package/dist/modules/billing/api/invoices/[id]/send/route.js +107 -0
- package/dist/modules/billing/api/invoices/[id]/send/route.js.map +7 -0
- package/dist/modules/billing/api/invoices/add-line/route.js +3 -3
- package/dist/modules/billing/api/invoices/add-line/route.js.map +2 -2
- package/dist/modules/billing/api/invoices/delete-draft/route.js +33 -0
- package/dist/modules/billing/api/invoices/delete-draft/route.js.map +7 -0
- package/dist/modules/billing/api/invoices/edit-line/route.js +3 -3
- package/dist/modules/billing/api/invoices/edit-line/route.js.map +2 -2
- package/dist/modules/billing/api/invoices/post/route.js +2 -2
- package/dist/modules/billing/api/invoices/post/route.js.map +2 -2
- package/dist/modules/billing/api/invoices/remove-line/route.js +3 -3
- package/dist/modules/billing/api/invoices/remove-line/route.js.map +2 -2
- package/dist/modules/billing/api/invoices/route.js +45 -46
- package/dist/modules/billing/api/invoices/route.js.map +2 -2
- package/dist/modules/billing/api/items/bulk/route.js +1 -1
- package/dist/modules/billing/api/items/bulk/route.js.map +2 -2
- package/dist/modules/billing/api/items/change-price/route.js +44 -0
- package/dist/modules/billing/api/items/change-price/route.js.map +7 -0
- package/dist/modules/billing/api/items/route.js +19 -7
- package/dist/modules/billing/api/items/route.js.map +2 -2
- package/dist/modules/billing/api/payment-imports/[id]/route.js +49 -0
- package/dist/modules/billing/api/payment-imports/[id]/route.js.map +7 -0
- package/dist/modules/billing/api/payment-imports/route.js +57 -0
- package/dist/modules/billing/api/payment-imports/route.js.map +7 -0
- package/dist/modules/billing/api/payments/[id]/route.js +60 -0
- package/dist/modules/billing/api/payments/[id]/route.js.map +7 -0
- package/dist/modules/billing/api/payments/allocate/route.js +35 -0
- package/dist/modules/billing/api/payments/allocate/route.js.map +7 -0
- package/dist/modules/billing/api/payments/assign/route.js +36 -0
- package/dist/modules/billing/api/payments/assign/route.js.map +7 -0
- package/dist/modules/billing/api/payments/convert-to-topup/route.js +35 -0
- package/dist/modules/billing/api/payments/convert-to-topup/route.js.map +7 -0
- package/dist/modules/billing/api/payments/ignore/route.js +35 -0
- package/dist/modules/billing/api/payments/ignore/route.js.map +7 -0
- package/dist/modules/billing/api/payments/import/route.js +63 -0
- package/dist/modules/billing/api/payments/import/route.js.map +7 -0
- package/dist/modules/billing/api/payments/restore/route.js +35 -0
- package/dist/modules/billing/api/payments/restore/route.js.map +7 -0
- package/dist/modules/billing/api/payments/route.js +114 -0
- package/dist/modules/billing/api/payments/route.js.map +7 -0
- package/dist/modules/billing/api/payments/unallocate/route.js +35 -0
- package/dist/modules/billing/api/payments/unallocate/route.js.map +7 -0
- package/dist/modules/billing/api/payments/unassign/route.js +35 -0
- package/dist/modules/billing/api/payments/unassign/route.js.map +7 -0
- package/dist/modules/billing/api/run-outcomes/route.js +44 -7
- package/dist/modules/billing/api/run-outcomes/route.js.map +2 -2
- package/dist/modules/billing/api/runs/[id]/post-drafts/route.js +61 -0
- package/dist/modules/billing/api/runs/[id]/post-drafts/route.js.map +7 -0
- package/dist/modules/billing/api/runs/[id]/route.js +53 -0
- package/dist/modules/billing/api/runs/[id]/route.js.map +7 -0
- package/dist/modules/billing/api/runs/retry-failed/route.js +3 -3
- package/dist/modules/billing/api/runs/retry-failed/route.js.map +2 -2
- package/dist/modules/billing/api/runs/route.js +11 -5
- package/dist/modules/billing/api/runs/route.js.map +2 -2
- package/dist/modules/billing/api/settings/number-preview/route.js +42 -0
- package/dist/modules/billing/api/settings/number-preview/route.js.map +7 -0
- package/dist/modules/billing/api/settings/route.js +92 -0
- package/dist/modules/billing/api/settings/route.js.map +7 -0
- package/dist/modules/billing/api/statements/route.js +3 -0
- package/dist/modules/billing/api/statements/route.js.map +2 -2
- package/dist/modules/billing/api/test-invoices/wipe/route.js +2 -2
- package/dist/modules/billing/api/test-invoices/wipe/route.js.map +2 -2
- package/dist/modules/billing/backend/billing/accounts/[id]/page.js +121 -232
- package/dist/modules/billing/backend/billing/accounts/[id]/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/accounts/create/page.js +6 -0
- package/dist/modules/billing/backend/billing/accounts/create/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/accounts/page.js +63 -4
- package/dist/modules/billing/backend/billing/accounts/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/invoices/[id]/page.js +297 -65
- package/dist/modules/billing/backend/billing/invoices/[id]/page.js.map +3 -3
- package/dist/modules/billing/backend/billing/invoices/page.js +6 -172
- package/dist/modules/billing/backend/billing/invoices/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/items/[id]/page.js +51 -7
- package/dist/modules/billing/backend/billing/items/[id]/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/items/create/page.js +9 -6
- package/dist/modules/billing/backend/billing/items/create/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/items/page.js +8 -2
- package/dist/modules/billing/backend/billing/items/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/payments/[id]/page.js +297 -0
- package/dist/modules/billing/backend/billing/payments/[id]/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/payments/[id]/page.meta.js +21 -0
- package/dist/modules/billing/backend/billing/payments/[id]/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/payments/imports/[id]/page.js +74 -0
- package/dist/modules/billing/backend/billing/payments/imports/[id]/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/payments/imports/[id]/page.meta.js +21 -0
- package/dist/modules/billing/backend/billing/payments/imports/[id]/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/payments/page.js +58 -0
- package/dist/modules/billing/backend/billing/payments/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/payments/page.meta.js +18 -0
- package/dist/modules/billing/backend/billing/payments/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/runs/[id]/page.js +49 -14
- package/dist/modules/billing/backend/billing/runs/[id]/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/runs/page.js +58 -7
- package/dist/modules/billing/backend/billing/runs/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/statements/[id]/page.js +61 -6
- package/dist/modules/billing/backend/billing/statements/[id]/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/statements/page.js +1 -1
- package/dist/modules/billing/backend/billing/statements/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/topups/page.js +1 -1
- package/dist/modules/billing/backend/billing/topups/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/transactions/page.js +1 -1
- package/dist/modules/billing/backend/billing/transactions/page.js.map +2 -2
- package/dist/modules/billing/backend/billing/usage/create/page.js +3 -2
- package/dist/modules/billing/backend/billing/usage/create/page.js.map +2 -2
- package/dist/modules/billing/backend/config/billing/page.js +472 -0
- package/dist/modules/billing/backend/config/billing/page.js.map +7 -0
- package/dist/modules/billing/backend/config/billing/page.meta.js +19 -0
- package/dist/modules/billing/backend/config/billing/page.meta.js.map +7 -0
- package/dist/modules/billing/ce.js.map +2 -2
- package/dist/modules/billing/cli.js +22 -1
- package/dist/modules/billing/cli.js.map +2 -2
- package/dist/modules/billing/commands/accounts.js +6 -0
- package/dist/modules/billing/commands/accounts.js.map +2 -2
- package/dist/modules/billing/commands/index.js +3 -0
- package/dist/modules/billing/commands/index.js.map +2 -2
- package/dist/modules/billing/commands/invoices.js +272 -170
- package/dist/modules/billing/commands/invoices.js.map +2 -2
- package/dist/modules/billing/commands/items.js +108 -0
- package/dist/modules/billing/commands/items.js.map +2 -2
- package/dist/modules/billing/commands/memos.js +179 -0
- package/dist/modules/billing/commands/memos.js.map +7 -0
- package/dist/modules/billing/commands/payments.js +379 -0
- package/dist/modules/billing/commands/payments.js.map +7 -0
- package/dist/modules/billing/commands/runs.js +154 -7
- package/dist/modules/billing/commands/runs.js.map +2 -2
- package/dist/modules/billing/commands/settings.js +59 -0
- package/dist/modules/billing/commands/settings.js.map +7 -0
- package/dist/modules/billing/commands/topups.js +9 -2
- package/dist/modules/billing/commands/topups.js.map +2 -2
- package/dist/modules/billing/commands/transactions.js +2 -2
- package/dist/modules/billing/commands/transactions.js.map +2 -2
- package/dist/modules/billing/components/AccountForm.js +149 -42
- package/dist/modules/billing/components/AccountForm.js.map +2 -2
- package/dist/modules/billing/components/AccountInvoicesSection.js +64 -0
- package/dist/modules/billing/components/AccountInvoicesSection.js.map +7 -0
- package/dist/modules/billing/components/AccountPaymentsSection.js +43 -0
- package/dist/modules/billing/components/AccountPaymentsSection.js.map +7 -0
- package/dist/modules/billing/components/AccountPicker.js +56 -0
- package/dist/modules/billing/components/AccountPicker.js.map +7 -0
- package/dist/modules/billing/components/AllocatePaymentDialog.js +134 -0
- package/dist/modules/billing/components/AllocatePaymentDialog.js.map +7 -0
- package/dist/modules/billing/components/BillingDataTable.js +21 -0
- package/dist/modules/billing/components/BillingDataTable.js.map +7 -0
- package/dist/modules/billing/components/BillingSettingsLink.js +19 -0
- package/dist/modules/billing/components/BillingSettingsLink.js.map +7 -0
- package/dist/modules/billing/components/ChangeItemPriceDialog.js +158 -0
- package/dist/modules/billing/components/ChangeItemPriceDialog.js.map +7 -0
- package/dist/modules/billing/components/CreateMemoDialog.js +200 -0
- package/dist/modules/billing/components/CreateMemoDialog.js.map +7 -0
- package/dist/modules/billing/components/CustomerLabel.js +33 -0
- package/dist/modules/billing/components/CustomerLabel.js.map +7 -0
- package/dist/modules/billing/components/InvoicesTable.js +270 -0
- package/dist/modules/billing/components/InvoicesTable.js.map +7 -0
- package/dist/modules/billing/components/ItemForm.js +176 -44
- package/dist/modules/billing/components/ItemForm.js.map +2 -2
- package/dist/modules/billing/components/ItemPriceField.js +46 -0
- package/dist/modules/billing/components/ItemPriceField.js.map +7 -0
- package/dist/modules/billing/components/LineFormDialog.js +25 -2
- package/dist/modules/billing/components/LineFormDialog.js.map +2 -2
- package/dist/modules/billing/components/ManualPaymentDialog.js +122 -0
- package/dist/modules/billing/components/ManualPaymentDialog.js.map +7 -0
- package/dist/modules/billing/components/MoneyInput.js +14 -0
- package/dist/modules/billing/components/MoneyInput.js.map +7 -0
- package/dist/modules/billing/components/PaymentUploadDialog.js +115 -0
- package/dist/modules/billing/components/PaymentUploadDialog.js.map +7 -0
- package/dist/modules/billing/components/PaymentsTable.js +171 -0
- package/dist/modules/billing/components/PaymentsTable.js.map +7 -0
- package/dist/modules/billing/components/PostRunDraftsButton.js +105 -0
- package/dist/modules/billing/components/PostRunDraftsButton.js.map +7 -0
- package/dist/modules/billing/components/PrepaidPanel.js +14 -4
- package/dist/modules/billing/components/PrepaidPanel.js.map +2 -2
- package/dist/modules/billing/components/RunCyclesSection.js +209 -0
- package/dist/modules/billing/components/RunCyclesSection.js.map +7 -0
- package/dist/modules/billing/components/TestRunActions.js +127 -0
- package/dist/modules/billing/components/TestRunActions.js.map +7 -0
- package/dist/modules/billing/components/account/AccountItemsTab.js +157 -0
- package/dist/modules/billing/components/account/AccountItemsTab.js.map +7 -0
- package/dist/modules/billing/components/account/AccountRunBillingDialog.js +122 -0
- package/dist/modules/billing/components/account/AccountRunBillingDialog.js.map +7 -0
- package/dist/modules/billing/components/account/AccountRunsTab.js +152 -0
- package/dist/modules/billing/components/account/AccountRunsTab.js.map +7 -0
- package/dist/modules/billing/components/account/AccountStatementsTab.js +98 -0
- package/dist/modules/billing/components/account/AccountStatementsTab.js.map +7 -0
- package/dist/modules/billing/components/account/AccountSummaryCards.js +125 -0
- package/dist/modules/billing/components/account/AccountSummaryCards.js.map +7 -0
- package/dist/modules/billing/components/account/AccountUsageTab.js +91 -0
- package/dist/modules/billing/components/account/AccountUsageTab.js.map +7 -0
- package/dist/modules/billing/components/account/AccountView.js +73 -0
- package/dist/modules/billing/components/account/AccountView.js.map +7 -0
- package/dist/modules/billing/components/account/accountRow.js +39 -0
- package/dist/modules/billing/components/account/accountRow.js.map +7 -0
- package/dist/modules/billing/components/customerNames.js +42 -0
- package/dist/modules/billing/components/customerNames.js.map +7 -0
- package/dist/modules/billing/data/entities.js +615 -59
- package/dist/modules/billing/data/entities.js.map +2 -2
- package/dist/modules/billing/data/entityIds.js +10 -1
- package/dist/modules/billing/data/entityIds.js.map +2 -2
- package/dist/modules/billing/data/validators.js +269 -15
- package/dist/modules/billing/data/validators.js.map +2 -2
- package/dist/modules/billing/events.js +70 -0
- package/dist/modules/billing/events.js.map +2 -2
- package/dist/modules/billing/i18n/en.json +457 -21
- package/dist/modules/billing/i18n/pl.json +457 -21
- package/dist/modules/billing/index.js +6 -2
- package/dist/modules/billing/index.js.map +2 -2
- package/dist/modules/billing/lib/accountFinancials.js +58 -0
- package/dist/modules/billing/lib/accountFinancials.js.map +7 -0
- package/dist/modules/billing/lib/accountLock.js +14 -0
- package/dist/modules/billing/lib/accountLock.js.map +7 -0
- package/dist/modules/billing/lib/accountStatus.js +9 -7
- package/dist/modules/billing/lib/accountStatus.js.map +2 -2
- package/dist/modules/billing/lib/accountSummary.js +106 -0
- package/dist/modules/billing/lib/accountSummary.js.map +7 -0
- package/dist/modules/billing/lib/allocation.js +356 -0
- package/dist/modules/billing/lib/allocation.js.map +7 -0
- package/dist/modules/billing/lib/allocationEvents.js +22 -0
- package/dist/modules/billing/lib/allocationEvents.js.map +7 -0
- package/dist/modules/billing/lib/billPeriod.js +15 -1
- package/dist/modules/billing/lib/billPeriod.js.map +2 -2
- package/dist/modules/billing/lib/billRunEngine.js +241 -106
- package/dist/modules/billing/lib/billRunEngine.js.map +2 -2
- package/dist/modules/billing/lib/billRunLabel.js.map +1 -1
- package/dist/modules/billing/lib/billingSettings.js +116 -0
- package/dist/modules/billing/lib/billingSettings.js.map +7 -0
- package/dist/modules/billing/lib/commandRoute.js +81 -0
- package/dist/modules/billing/lib/commandRoute.js.map +7 -0
- package/dist/modules/billing/lib/customerLookup.js +97 -0
- package/dist/modules/billing/lib/customerLookup.js.map +7 -0
- package/dist/modules/billing/lib/dateOnly.js +13 -0
- package/dist/modules/billing/lib/dateOnly.js.map +7 -0
- package/dist/modules/billing/lib/documentParties.js +16 -0
- package/dist/modules/billing/lib/documentParties.js.map +7 -0
- package/dist/modules/billing/lib/documentTypes.js +27 -0
- package/dist/modules/billing/lib/documentTypes.js.map +7 -0
- package/dist/modules/billing/lib/dueDate.js +18 -0
- package/dist/modules/billing/lib/dueDate.js.map +7 -0
- package/dist/modules/billing/lib/iban.js +29 -0
- package/dist/modules/billing/lib/iban.js.map +7 -0
- package/dist/modules/billing/lib/invoiceDelivery.js +131 -0
- package/dist/modules/billing/lib/invoiceDelivery.js.map +7 -0
- package/dist/modules/billing/lib/invoiceLanguages.js +16 -0
- package/dist/modules/billing/lib/invoiceLanguages.js.map +7 -0
- package/dist/modules/billing/lib/invoiceWriter.js +107 -99
- package/dist/modules/billing/lib/invoiceWriter.js.map +2 -2
- package/dist/modules/billing/lib/itemBillingState.js +27 -0
- package/dist/modules/billing/lib/itemBillingState.js.map +7 -0
- package/dist/modules/billing/lib/itemPriceChange.js +18 -0
- package/dist/modules/billing/lib/itemPriceChange.js.map +7 -0
- package/dist/modules/billing/lib/itemSelector.js.map +2 -2
- package/dist/modules/billing/lib/lineAmounts.js +32 -0
- package/dist/modules/billing/lib/lineAmounts.js.map +7 -0
- package/dist/modules/billing/lib/memoGuards.js +25 -0
- package/dist/modules/billing/lib/memoGuards.js.map +7 -0
- package/dist/modules/billing/lib/navigation.js +48 -0
- package/dist/modules/billing/lib/navigation.js.map +7 -0
- package/dist/modules/billing/lib/numbering.js +120 -0
- package/dist/modules/billing/lib/numbering.js.map +7 -0
- package/dist/modules/billing/lib/overdue.js +36 -0
- package/dist/modules/billing/lib/overdue.js.map +7 -0
- package/dist/modules/billing/lib/paymentCsv.js +125 -0
- package/dist/modules/billing/lib/paymentCsv.js.map +7 -0
- package/dist/modules/billing/lib/paymentGateway.js +15 -0
- package/dist/modules/billing/lib/paymentGateway.js.map +7 -0
- package/dist/modules/billing/lib/paymentImport.js +164 -0
- package/dist/modules/billing/lib/paymentImport.js.map +7 -0
- package/dist/modules/billing/lib/paymentMatcher.js +36 -0
- package/dist/modules/billing/lib/paymentMatcher.js.map +7 -0
- package/dist/modules/billing/lib/pdf/attachmentStore.js +46 -0
- package/dist/modules/billing/lib/pdf/attachmentStore.js.map +7 -0
- package/dist/modules/billing/lib/pdf/fonts/embeddedFonts.js +7 -0
- package/dist/modules/billing/lib/pdf/fonts/embeddedFonts.js.map +7 -0
- package/dist/modules/billing/lib/pdf/invoiceModel.js +194 -0
- package/dist/modules/billing/lib/pdf/invoiceModel.js.map +7 -0
- package/dist/modules/billing/lib/pdf/invoicePdf.js +165 -0
- package/dist/modules/billing/lib/pdf/invoicePdf.js.map +7 -0
- package/dist/modules/billing/lib/pdf/invoiceTemplate.js +274 -0
- package/dist/modules/billing/lib/pdf/invoiceTemplate.js.map +7 -0
- package/dist/modules/billing/lib/prepaidConfig.js +17 -25
- package/dist/modules/billing/lib/prepaidConfig.js.map +2 -2
- package/dist/modules/billing/lib/prepaidConsume.js +26 -7
- package/dist/modules/billing/lib/prepaidConsume.js.map +2 -2
- package/dist/modules/billing/lib/prepaidPeriodClose.js +277 -158
- package/dist/modules/billing/lib/prepaidPeriodClose.js.map +2 -2
- package/dist/modules/billing/lib/prepaidTaxPoint.js +37 -0
- package/dist/modules/billing/lib/prepaidTaxPoint.js.map +7 -0
- package/dist/modules/billing/lib/seeds/demo.js +51 -3
- package/dist/modules/billing/lib/seeds/demo.js.map +2 -2
- package/dist/modules/billing/lib/seeds/paymentsSample.js +93 -0
- package/dist/modules/billing/lib/seeds/paymentsSample.js.map +7 -0
- package/dist/modules/billing/lib/seeds/sellerSettings.js +41 -0
- package/dist/modules/billing/lib/seeds/sellerSettings.js.map +7 -0
- package/dist/modules/billing/lib/servicePeriods.js +121 -0
- package/dist/modules/billing/lib/servicePeriods.js.map +7 -0
- package/dist/modules/billing/lib/tenantLock.js.map +1 -1
- package/dist/modules/billing/lib/testRunCleanup.js +24 -0
- package/dist/modules/billing/lib/testRunCleanup.js.map +7 -0
- package/dist/modules/billing/lib/topupCapture.js +2 -2
- package/dist/modules/billing/lib/topupCapture.js.map +2 -2
- package/dist/modules/billing/lib/topupReceipt.js +58 -79
- package/dist/modules/billing/lib/topupReceipt.js.map +2 -2
- package/dist/modules/billing/lib/usageRater.js.map +1 -1
- package/dist/modules/billing/lib/usageRunner.js +14 -6
- package/dist/modules/billing/lib/usageRunner.js.map +2 -2
- package/dist/modules/billing/lib/vat.js +39 -0
- package/dist/modules/billing/lib/vat.js.map +7 -0
- package/dist/modules/billing/migrations/Migration20261114000000_billing_customer_kind.js +15 -0
- package/dist/modules/billing/migrations/Migration20261114000000_billing_customer_kind.js.map +7 -0
- package/dist/modules/billing/migrations/Migration20261114100000_billing_cycle_semantics.js +25 -0
- package/dist/modules/billing/migrations/Migration20261114100000_billing_cycle_semantics.js.map +7 -0
- package/dist/modules/billing/migrations/Migration20261115000000_billing_invoice_ledger.js +115 -0
- package/dist/modules/billing/migrations/Migration20261115000000_billing_invoice_ledger.js.map +7 -0
- package/dist/modules/billing/migrations/Migration20261116000000_billing_invoice_documents.js +47 -0
- package/dist/modules/billing/migrations/Migration20261116000000_billing_invoice_documents.js.map +7 -0
- package/dist/modules/billing/migrations/Migration20261117000000_billing_payments.js +89 -0
- package/dist/modules/billing/migrations/Migration20261117000000_billing_payments.js.map +7 -0
- package/dist/modules/billing/migrations/Migration20261118000000_billing_memos.js +17 -0
- package/dist/modules/billing/migrations/Migration20261118000000_billing_memos.js.map +7 -0
- package/dist/modules/billing/migrations/Migration20261119000000_billing_operator_feedback.js +27 -0
- package/dist/modules/billing/migrations/Migration20261119000000_billing_operator_feedback.js.map +7 -0
- package/dist/modules/billing/migrations/Migration20261120000000_billing_allocation_scope_indexes.js +29 -0
- package/dist/modules/billing/migrations/Migration20261120000000_billing_allocation_scope_indexes.js.map +7 -0
- package/dist/modules/billing/setup.js +7 -184
- package/dist/modules/billing/setup.js.map +2 -2
- package/dist/modules/billing/subscribers/credit-memo-auto-send.js +16 -0
- package/dist/modules/billing/subscribers/credit-memo-auto-send.js.map +7 -0
- package/dist/modules/billing/subscribers/debit-memo-apply-credit.js +16 -0
- package/dist/modules/billing/subscribers/debit-memo-apply-credit.js.map +7 -0
- package/dist/modules/billing/subscribers/debit-memo-auto-send.js +16 -0
- package/dist/modules/billing/subscribers/debit-memo-auto-send.js.map +7 -0
- package/dist/modules/billing/subscribers/invoice-apply-credit.js +34 -0
- package/dist/modules/billing/subscribers/invoice-apply-credit.js.map +7 -0
- package/dist/modules/billing/subscribers/invoice-auto-send.js +30 -0
- package/dist/modules/billing/subscribers/invoice-auto-send.js.map +7 -0
- package/dist/modules/billing/subscribers/payment-captured-to-paid.js +69 -33
- package/dist/modules/billing/subscribers/payment-captured-to-paid.js.map +2 -2
- package/dist/modules/billing/subscribers/prepaid-topup-captured.js +4 -6
- package/dist/modules/billing/subscribers/prepaid-topup-captured.js.map +2 -2
- package/dist/modules/billing/workers/reap-stale-bill-runs.js +2 -2
- package/dist/modules/billing/workers/reap-stale-bill-runs.js.map +2 -2
- package/generated/entities/billing_account/index.ts +26 -0
- package/generated/entities/billing_account_balance/index.ts +13 -0
- package/generated/entities/billing_account_transaction/index.ts +21 -0
- package/generated/entities/billing_account_usage/index.ts +18 -0
- package/generated/entities/billing_allocation/index.ts +18 -0
- package/generated/entities/billing_document_sequence/index.ts +11 -0
- package/generated/entities/billing_draft_invoice_edit/index.ts +14 -0
- package/generated/entities/billing_invoice/index.ts +38 -0
- package/generated/entities/billing_invoice_line/index.ts +24 -0
- package/generated/entities/billing_item/index.ts +26 -0
- package/generated/entities/billing_payment/index.ts +25 -0
- package/generated/entities/billing_payment_import/index.ts +14 -0
- package/generated/entities/billing_run/index.ts +20 -0
- package/generated/entities/billing_run_outcome/index.ts +18 -0
- package/generated/entities/billing_settings/index.ts +26 -0
- package/generated/entities/billing_statement/index.ts +22 -0
- package/generated/entities/billing_topup/index.ts +20 -0
- package/generated/entities.ids.generated.ts +35 -0
- package/package.json +15 -10
- package/src/modules/billing/__integration__/TC-BILL-004.spec.ts +170 -0
- package/src/modules/billing/__integration__/TC-BILL-005.spec.ts +134 -0
- package/src/modules/billing/__integration__/TC-BILL-006.spec.ts +101 -0
- package/src/modules/billing/__integration__/TC-BILL-007.spec.ts +140 -0
- package/src/modules/billing/__integration__/TC-BILL-008.spec.ts +108 -0
- package/src/modules/billing/__integration__/TC-BILL-009.spec.ts +113 -0
- package/src/modules/billing/__integration__/TC-BILL-010.spec.ts +118 -0
- package/src/modules/billing/__integration__/TC-BILL-011.spec.ts +142 -0
- package/src/modules/billing/__integration__/TC-BILL-012.spec.ts +115 -0
- package/src/modules/billing/__integration__/TC-BILL-013.spec.ts +118 -0
- package/src/modules/billing/__integration__/sellerSettings.ts +24 -0
- package/src/modules/billing/__tests__/commands/invoices.test.ts +437 -355
- package/src/modules/billing/__tests__/commands/itemsBulk.test.ts +33 -0
- package/src/modules/billing/__tests__/commands/memos.test.ts +205 -0
- package/src/modules/billing/__tests__/commands/payments.test.ts +241 -0
- package/src/modules/billing/__tests__/commands/runsDelete.test.ts +146 -0
- package/src/modules/billing/__tests__/commands/runsPostDrafts.test.ts +106 -0
- package/src/modules/billing/__tests__/commands/topups.test.ts +11 -0
- package/src/modules/billing/__tests__/entityRegistry.test.ts +326 -0
- package/src/modules/billing/__tests__/events.test.ts +142 -0
- package/src/modules/billing/__tests__/helpers/prepaidMockEm.ts +28 -11
- package/src/modules/billing/__tests__/lib/accountFinancials.test.ts +53 -0
- package/src/modules/billing/__tests__/lib/accountSummary.test.ts +97 -0
- package/src/modules/billing/__tests__/lib/allocation.test.ts +319 -0
- package/src/modules/billing/__tests__/lib/billRunEngine.test.ts +231 -20
- package/src/modules/billing/__tests__/lib/billingSettings.test.ts +109 -0
- package/src/modules/billing/__tests__/lib/customerLookup.test.ts +156 -0
- package/src/modules/billing/__tests__/lib/dueDate.test.ts +35 -0
- package/src/modules/billing/__tests__/lib/iban.test.ts +38 -0
- package/src/modules/billing/__tests__/lib/invoiceDelivery.test.ts +189 -0
- package/src/modules/billing/__tests__/lib/invoiceWriter.test.ts +212 -10
- package/src/modules/billing/__tests__/lib/itemBillingState.test.ts +42 -0
- package/src/modules/billing/__tests__/lib/itemPriceChange.test.ts +165 -0
- package/src/modules/billing/__tests__/lib/lineAmounts.test.ts +86 -0
- package/src/modules/billing/__tests__/lib/navigation.test.ts +71 -0
- package/src/modules/billing/__tests__/lib/numbering.test.ts +91 -0
- package/src/modules/billing/__tests__/lib/overdue.test.ts +36 -0
- package/src/modules/billing/__tests__/lib/paymentCsv.test.ts +148 -0
- package/src/modules/billing/__tests__/lib/paymentImport.test.ts +113 -0
- package/src/modules/billing/__tests__/lib/paymentsSample.test.ts +73 -0
- package/src/modules/billing/__tests__/lib/pdf/invoicePdf.test.ts +199 -0
- package/src/modules/billing/__tests__/lib/pdf/invoicePdfService.test.ts +147 -0
- package/src/modules/billing/__tests__/lib/prepaidPeriodClose.test.ts +374 -98
- package/src/modules/billing/__tests__/lib/prepaidTaxPoint.test.ts +118 -0
- package/src/modules/billing/__tests__/lib/seedsDemo.test.ts +53 -2
- package/src/modules/billing/__tests__/lib/sellerSettings.test.ts +28 -0
- package/src/modules/billing/__tests__/lib/servicePeriods.test.ts +190 -0
- package/src/modules/billing/__tests__/lib/topupCapture.test.ts +6 -28
- package/src/modules/billing/__tests__/lib/topupReceipt.test.ts +21 -52
- package/src/modules/billing/__tests__/lib/vat.test.ts +51 -0
- package/src/modules/billing/__tests__/package-isolation.test.ts +22 -8
- package/src/modules/billing/__tests__/setup.test.ts +13 -4
- package/src/modules/billing/__tests__/validators.test.ts +193 -3
- package/src/modules/billing/acl.ts +12 -0
- package/src/modules/billing/api/accounts/[id]/summary/route.ts +62 -0
- package/src/modules/billing/api/accounts/route.ts +58 -4
- package/src/modules/billing/api/export/account/[id]/route.ts +20 -18
- package/src/modules/billing/api/invoices/[id]/memos/route.ts +63 -0
- package/src/modules/billing/api/invoices/[id]/pdf/route.ts +79 -0
- package/src/modules/billing/api/invoices/[id]/route.ts +57 -70
- package/src/modules/billing/api/invoices/[id]/send/route.ts +127 -0
- package/src/modules/billing/api/invoices/add-line/route.ts +3 -3
- package/src/modules/billing/api/invoices/delete-draft/route.ts +33 -0
- package/src/modules/billing/api/invoices/edit-line/route.ts +4 -4
- package/src/modules/billing/api/invoices/post/route.ts +7 -6
- package/src/modules/billing/api/invoices/remove-line/route.ts +3 -3
- package/src/modules/billing/api/invoices/route.ts +81 -86
- package/src/modules/billing/api/items/bulk/route.ts +2 -1
- package/src/modules/billing/api/items/change-price/route.ts +54 -0
- package/src/modules/billing/api/items/route.ts +23 -7
- package/src/modules/billing/api/payment-imports/[id]/route.ts +49 -0
- package/src/modules/billing/api/payment-imports/route.ts +57 -0
- package/src/modules/billing/api/payments/[id]/route.ts +63 -0
- package/src/modules/billing/api/payments/allocate/route.ts +35 -0
- package/src/modules/billing/api/payments/assign/route.ts +36 -0
- package/src/modules/billing/api/payments/convert-to-topup/route.ts +35 -0
- package/src/modules/billing/api/payments/ignore/route.ts +35 -0
- package/src/modules/billing/api/payments/import/route.ts +72 -0
- package/src/modules/billing/api/payments/restore/route.ts +35 -0
- package/src/modules/billing/api/payments/route.ts +147 -0
- package/src/modules/billing/api/payments/unallocate/route.ts +35 -0
- package/src/modules/billing/api/payments/unassign/route.ts +35 -0
- package/src/modules/billing/api/run-outcomes/route.ts +50 -7
- package/src/modules/billing/api/runs/[id]/post-drafts/route.ts +71 -0
- package/src/modules/billing/api/runs/[id]/route.ts +59 -0
- package/src/modules/billing/api/runs/retry-failed/route.ts +4 -3
- package/src/modules/billing/api/runs/route.ts +17 -9
- package/src/modules/billing/api/settings/number-preview/route.ts +46 -0
- package/src/modules/billing/api/settings/route.ts +116 -0
- package/src/modules/billing/api/statements/route.ts +5 -0
- package/src/modules/billing/api/test-invoices/wipe/route.ts +2 -2
- package/src/modules/billing/backend/billing/accounts/[id]/page.tsx +151 -372
- package/src/modules/billing/backend/billing/accounts/create/page.tsx +6 -0
- package/src/modules/billing/backend/billing/accounts/page.tsx +71 -5
- package/src/modules/billing/backend/billing/invoices/[id]/page.tsx +445 -96
- package/src/modules/billing/backend/billing/invoices/page.tsx +8 -227
- package/src/modules/billing/backend/billing/items/[id]/page.tsx +75 -6
- package/src/modules/billing/backend/billing/items/create/page.tsx +13 -8
- package/src/modules/billing/backend/billing/items/page.tsx +19 -3
- package/src/modules/billing/backend/billing/payments/[id]/page.meta.ts +19 -0
- package/src/modules/billing/backend/billing/payments/[id]/page.tsx +415 -0
- package/src/modules/billing/backend/billing/payments/imports/[id]/page.meta.ts +19 -0
- package/src/modules/billing/backend/billing/payments/imports/[id]/page.tsx +106 -0
- package/src/modules/billing/backend/billing/payments/page.meta.ts +16 -0
- package/src/modules/billing/backend/billing/payments/page.tsx +56 -0
- package/src/modules/billing/backend/billing/runs/[id]/page.tsx +50 -19
- package/src/modules/billing/backend/billing/runs/page.tsx +60 -9
- package/src/modules/billing/backend/billing/statements/[id]/page.tsx +87 -6
- package/src/modules/billing/backend/billing/statements/page.tsx +2 -2
- package/src/modules/billing/backend/billing/topups/page.tsx +2 -2
- package/src/modules/billing/backend/billing/transactions/page.tsx +2 -2
- package/src/modules/billing/backend/billing/usage/create/page.tsx +3 -2
- package/src/modules/billing/backend/config/billing/page.meta.ts +21 -0
- package/src/modules/billing/backend/config/billing/page.tsx +549 -0
- package/src/modules/billing/ce.ts +8 -7
- package/src/modules/billing/cli.ts +31 -1
- package/src/modules/billing/commands/accounts.ts +6 -0
- package/src/modules/billing/commands/index.ts +3 -0
- package/src/modules/billing/commands/invoices.ts +371 -238
- package/src/modules/billing/commands/items.ts +143 -0
- package/src/modules/billing/commands/memos.ts +218 -0
- package/src/modules/billing/commands/payments.ts +428 -0
- package/src/modules/billing/commands/runs.ts +208 -6
- package/src/modules/billing/commands/settings.ts +77 -0
- package/src/modules/billing/commands/topups.ts +12 -3
- package/src/modules/billing/commands/transactions.ts +2 -2
- package/src/modules/billing/components/AccountForm.tsx +199 -53
- package/src/modules/billing/components/AccountInvoicesSection.tsx +75 -0
- package/src/modules/billing/components/AccountPaymentsSection.tsx +42 -0
- package/src/modules/billing/components/AccountPicker.tsx +70 -0
- package/src/modules/billing/components/AllocatePaymentDialog.tsx +167 -0
- package/src/modules/billing/components/BillingDataTable.tsx +34 -0
- package/src/modules/billing/components/BillingSettingsLink.tsx +25 -0
- package/src/modules/billing/components/ChangeItemPriceDialog.tsx +180 -0
- package/src/modules/billing/components/CreateMemoDialog.tsx +232 -0
- package/src/modules/billing/components/CustomerLabel.tsx +53 -0
- package/src/modules/billing/components/InvoicesTable.tsx +344 -0
- package/src/modules/billing/components/ItemForm.tsx +233 -44
- package/src/modules/billing/components/ItemPriceField.tsx +81 -0
- package/src/modules/billing/components/LineFormDialog.tsx +25 -1
- package/src/modules/billing/components/ManualPaymentDialog.tsx +140 -0
- package/src/modules/billing/components/MoneyInput.tsx +26 -0
- package/src/modules/billing/components/PaymentUploadDialog.tsx +148 -0
- package/src/modules/billing/components/PaymentsTable.tsx +213 -0
- package/src/modules/billing/components/PostRunDraftsButton.tsx +129 -0
- package/src/modules/billing/components/PrepaidPanel.tsx +20 -7
- package/src/modules/billing/components/RunCyclesSection.tsx +298 -0
- package/src/modules/billing/components/TestRunActions.tsx +152 -0
- package/src/modules/billing/components/account/AccountItemsTab.tsx +192 -0
- package/src/modules/billing/components/account/AccountRunBillingDialog.tsx +147 -0
- package/src/modules/billing/components/account/AccountRunsTab.tsx +201 -0
- package/src/modules/billing/components/account/AccountStatementsTab.tsx +120 -0
- package/src/modules/billing/components/account/AccountSummaryCards.tsx +153 -0
- package/src/modules/billing/components/account/AccountUsageTab.tsx +122 -0
- package/src/modules/billing/components/account/AccountView.tsx +101 -0
- package/src/modules/billing/components/account/accountRow.ts +70 -0
- package/src/modules/billing/components/customerNames.ts +62 -0
- package/src/modules/billing/data/entities.ts +685 -15
- package/src/modules/billing/data/entityIds.ts +25 -11
- package/src/modules/billing/data/validators.ts +388 -8
- package/src/modules/billing/events.ts +82 -3
- package/src/modules/billing/i18n/en.json +457 -21
- package/src/modules/billing/i18n/pl.json +457 -21
- package/src/modules/billing/index.ts +6 -2
- package/src/modules/billing/lib/accountFinancials.ts +84 -0
- package/src/modules/billing/lib/accountLock.ts +32 -0
- package/src/modules/billing/lib/accountStatus.ts +11 -9
- package/src/modules/billing/lib/accountSummary.ts +172 -0
- package/src/modules/billing/lib/allocation.ts +515 -0
- package/src/modules/billing/lib/allocationEvents.ts +26 -0
- package/src/modules/billing/lib/billPeriod.ts +19 -0
- package/src/modules/billing/lib/billRunEngine.ts +362 -162
- package/src/modules/billing/lib/billRunLabel.ts +1 -1
- package/src/modules/billing/lib/billingSettings.ts +145 -0
- package/src/modules/billing/lib/commandRoute.ts +112 -0
- package/src/modules/billing/lib/customerLookup.ts +157 -0
- package/src/modules/billing/lib/dateOnly.ts +22 -0
- package/src/modules/billing/lib/documentParties.ts +23 -0
- package/src/modules/billing/lib/documentTypes.ts +37 -0
- package/src/modules/billing/lib/dueDate.ts +26 -0
- package/src/modules/billing/lib/iban.ts +36 -0
- package/src/modules/billing/lib/invoiceDelivery.ts +197 -0
- package/src/modules/billing/lib/invoiceLanguages.ts +34 -0
- package/src/modules/billing/lib/invoiceWriter.ts +162 -148
- package/src/modules/billing/lib/itemBillingState.ts +67 -0
- package/src/modules/billing/lib/itemPriceChange.ts +56 -0
- package/src/modules/billing/lib/itemSelector.ts +4 -1
- package/src/modules/billing/lib/lineAmounts.ts +61 -0
- package/src/modules/billing/lib/memoGuards.ts +41 -0
- package/src/modules/billing/lib/navigation.ts +64 -0
- package/src/modules/billing/lib/numbering.ts +174 -0
- package/src/modules/billing/lib/overdue.ts +60 -0
- package/src/modules/billing/lib/paymentCsv.ts +171 -0
- package/src/modules/billing/lib/paymentGateway.ts +52 -0
- package/src/modules/billing/lib/paymentImport.ts +228 -0
- package/src/modules/billing/lib/paymentMatcher.ts +88 -0
- package/src/modules/billing/lib/pdf/attachmentStore.ts +60 -0
- package/src/modules/billing/lib/pdf/fonts/NotoSans-Bold.ttf +0 -0
- package/src/modules/billing/lib/pdf/fonts/NotoSans-Regular.ttf +0 -0
- package/src/modules/billing/lib/pdf/fonts/OFL.txt +93 -0
- package/src/modules/billing/lib/pdf/fonts/embeddedFonts.ts +9 -0
- package/src/modules/billing/lib/pdf/invoiceModel.ts +328 -0
- package/src/modules/billing/lib/pdf/invoicePdf.ts +216 -0
- package/src/modules/billing/lib/pdf/invoiceTemplate.ts +338 -0
- package/src/modules/billing/lib/prepaidConfig.ts +27 -36
- package/src/modules/billing/lib/prepaidConsume.ts +29 -6
- package/src/modules/billing/lib/prepaidPeriodClose.ts +410 -242
- package/src/modules/billing/lib/prepaidTaxPoint.ts +76 -0
- package/src/modules/billing/lib/seeds/demo.ts +79 -6
- package/src/modules/billing/lib/seeds/paymentsSample.ts +136 -0
- package/src/modules/billing/lib/seeds/sellerSettings.ts +78 -0
- package/src/modules/billing/lib/servicePeriods.ts +226 -0
- package/src/modules/billing/lib/tenantLock.ts +1 -1
- package/src/modules/billing/lib/testRunCleanup.ts +42 -0
- package/src/modules/billing/lib/topupCapture.ts +3 -4
- package/src/modules/billing/lib/topupReceipt.ts +70 -103
- package/src/modules/billing/lib/usageRater.ts +1 -1
- package/src/modules/billing/lib/usageRunner.ts +17 -5
- package/src/modules/billing/lib/vat.ts +56 -0
- package/src/modules/billing/migrations/Migration20261114000000_billing_customer_kind.ts +25 -0
- package/src/modules/billing/migrations/Migration20261114100000_billing_cycle_semantics.ts +37 -0
- package/src/modules/billing/migrations/Migration20261115000000_billing_invoice_ledger.ts +132 -0
- package/src/modules/billing/migrations/Migration20261116000000_billing_invoice_documents.ts +58 -0
- package/src/modules/billing/migrations/Migration20261117000000_billing_payments.ts +97 -0
- package/src/modules/billing/migrations/Migration20261118000000_billing_memos.ts +21 -0
- package/src/modules/billing/migrations/Migration20261119000000_billing_operator_feedback.ts +39 -0
- package/src/modules/billing/migrations/Migration20261120000000_billing_allocation_scope_indexes.ts +35 -0
- package/src/modules/billing/setup.ts +11 -237
- package/src/modules/billing/subscribers/credit-memo-auto-send.ts +15 -0
- package/src/modules/billing/subscribers/debit-memo-apply-credit.ts +15 -0
- package/src/modules/billing/subscribers/debit-memo-auto-send.ts +15 -0
- package/src/modules/billing/subscribers/invoice-apply-credit.ts +39 -0
- package/src/modules/billing/subscribers/invoice-auto-send.ts +46 -0
- package/src/modules/billing/subscribers/payment-captured-to-paid.ts +84 -62
- package/src/modules/billing/subscribers/prepaid-topup-captured.ts +4 -8
- package/src/modules/billing/workers/reap-stale-bill-runs.ts +4 -4
- package/dist/modules/billing/lib/invoiceStatus.js +0 -38
- package/dist/modules/billing/lib/invoiceStatus.js.map +0 -7
- package/src/modules/billing/__tests__/lib/invoiceStatus.test.ts +0 -97
- package/src/modules/billing/lib/invoiceStatus.ts +0 -61
|
@@ -24,6 +24,7 @@ type Env = {
|
|
|
24
24
|
ctx: { container: { resolve: jest.MockedFunction<(name: string) => unknown> }; auth: null }
|
|
25
25
|
em: {
|
|
26
26
|
find: jest.MockedFunction<(entity: unknown, where: unknown) => Promise<unknown[]>>
|
|
27
|
+
findOne: jest.MockedFunction<(entity: unknown, where: unknown) => Promise<unknown>>
|
|
27
28
|
create: jest.MockedFunction<(entity: unknown, data: Record<string, unknown>) => unknown>
|
|
28
29
|
persist: jest.MockedFunction<(entity: unknown) => unknown>
|
|
29
30
|
flush: jest.MockedFunction<() => Promise<void>>
|
|
@@ -68,6 +69,10 @@ function createEnv(
|
|
|
68
69
|
}
|
|
69
70
|
return []
|
|
70
71
|
}) as never,
|
|
72
|
+
// Spec B: the org default VAT rate comes from BillingSettings.
|
|
73
|
+
findOne: jest.fn(async (entity: unknown) =>
|
|
74
|
+
(entity as { name?: string }).name === 'BillingSettings' ? { defaultTaxRate: '23' } : null,
|
|
75
|
+
) as never,
|
|
71
76
|
create: jest.fn((_entity: unknown, data: Record<string, unknown>) => {
|
|
72
77
|
// Real MikroORM leaves the UUID PK unassigned until flush — the
|
|
73
78
|
// mock mirrors that so id-before-flush bugs surface in tests.
|
|
@@ -276,6 +281,34 @@ describe('bulkCreateItemsCommand', () => {
|
|
|
276
281
|
// Distinct — A appears twice in the payload but once in the query.
|
|
277
282
|
expect(new Set(where.id?.$in)).toEqual(new Set([ACCOUNT_A, ACCOUNT_B]))
|
|
278
283
|
})
|
|
284
|
+
|
|
285
|
+
it('fills the org default VAT rate once for entries without one; explicit rates win (Spec B)', async () => {
|
|
286
|
+
const env = createEnv()
|
|
287
|
+
await bulkCreateItemsCommand.execute(
|
|
288
|
+
{
|
|
289
|
+
tenantId: TENANT,
|
|
290
|
+
organizationId: ORG,
|
|
291
|
+
items: [recurringEntry(ACCOUNT_A, 'ref-1'), { ...recurringEntry(ACCOUNT_A, 'ref-2'), taxRate: 8 }],
|
|
292
|
+
},
|
|
293
|
+
env.ctx as never,
|
|
294
|
+
)
|
|
295
|
+
const created = env.em.create.mock.calls.map(([, data]) => data.taxRate)
|
|
296
|
+
expect(created).toEqual(['23', '8'])
|
|
297
|
+
const settingsReads = env.em.findOne.mock.calls.filter(
|
|
298
|
+
([entity]) => (entity as { name?: string }).name === 'BillingSettings',
|
|
299
|
+
)
|
|
300
|
+
expect(settingsReads).toHaveLength(1)
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
it('does not read settings when every entry carries a rate', async () => {
|
|
304
|
+
const env = createEnv()
|
|
305
|
+
await bulkCreateItemsCommand.execute(
|
|
306
|
+
{ tenantId: TENANT, organizationId: ORG, items: [{ ...recurringEntry(ACCOUNT_A, 'ref-1'), taxRate: 0 }] },
|
|
307
|
+
env.ctx as never,
|
|
308
|
+
)
|
|
309
|
+
expect(env.em.findOne).not.toHaveBeenCalled()
|
|
310
|
+
expect(env.em.create.mock.calls[0][1].taxRate).toBe('0')
|
|
311
|
+
})
|
|
279
312
|
})
|
|
280
313
|
|
|
281
314
|
// Suppress unused-import warnings — the entity classes are referenced
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { createMemoCommand, deleteDraftMemoCommand } from '../../commands/memos'
|
|
2
|
+
import { BillingInvoice, BillingInvoiceLine, BillingSettings } from '../../data/entities'
|
|
3
|
+
import { canCorrect, displaySign, isMemo, isReceivable, seriesFor } from '../../lib/documentTypes'
|
|
4
|
+
import { creditFits, creditHeadroomCents } from '../../lib/memoGuards'
|
|
5
|
+
|
|
6
|
+
const TENANT = '11111111-1111-4111-8111-111111111111'
|
|
7
|
+
const ORG = '22222222-2222-4222-8222-222222222222'
|
|
8
|
+
const ORIGINAL = '33333333-3333-4333-8333-333333333333'
|
|
9
|
+
const LINE_A = '44444444-4444-4444-8444-444444444444'
|
|
10
|
+
const LINE_B = '55555555-5555-4555-8555-555555555555'
|
|
11
|
+
const base = { tenantId: TENANT, organizationId: ORG }
|
|
12
|
+
|
|
13
|
+
type Row = Record<string, unknown>
|
|
14
|
+
|
|
15
|
+
function original(overrides: Row = {}): Row {
|
|
16
|
+
return {
|
|
17
|
+
id: ORIGINAL,
|
|
18
|
+
...base,
|
|
19
|
+
billAccountId: 'acc-1',
|
|
20
|
+
documentType: 'invoice',
|
|
21
|
+
status: 'posted',
|
|
22
|
+
testRun: false,
|
|
23
|
+
number: 'INV/2026/11/0001',
|
|
24
|
+
issueDate: new Date('2026-11-14T00:00:00Z'),
|
|
25
|
+
periodStart: new Date('2026-11-01T00:00:00Z'),
|
|
26
|
+
periodEnd: new Date('2026-11-30T00:00:00Z'),
|
|
27
|
+
currencyCode: 'PLN',
|
|
28
|
+
totalGross: '146.0000',
|
|
29
|
+
...overrides,
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const lines: Row[] = [
|
|
34
|
+
{ id: LINE_A, description: 'Internet', quantity: '1.0000', unitPriceNet: '100.0000', taxRate: '23', billingType: 'recurring', servicePeriodStart: null, servicePeriodEnd: null },
|
|
35
|
+
{ id: LINE_B, description: 'Router', quantity: '2.0000', unitPriceNet: '10.0000', taxRate: '8', billingType: 'one_time', servicePeriodStart: null, servicePeriodEnd: null },
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
function env(opts: { original?: Row | null; corrections?: Array<{ document_type: string; gross: string }>; target?: Row | null } = {}) {
|
|
39
|
+
const created: Array<{ entity: unknown; data: Row }> = []
|
|
40
|
+
const executed: string[] = []
|
|
41
|
+
const em = {
|
|
42
|
+
findOne: jest.fn(async (entity: unknown) => {
|
|
43
|
+
if (entity === BillingInvoice) return opts.target !== undefined ? opts.target : opts.original === undefined ? original() : opts.original
|
|
44
|
+
if (entity === BillingSettings) return { defaultTaxRate: '5' }
|
|
45
|
+
return null
|
|
46
|
+
}),
|
|
47
|
+
find: jest.fn(async (entity: unknown) => (entity === BillingInvoiceLine ? lines : [])),
|
|
48
|
+
execute: jest.fn(async (sql: string) => {
|
|
49
|
+
executed.push(sql)
|
|
50
|
+
if (sql.includes('SUM(total_gross)')) return opts.corrections ?? []
|
|
51
|
+
return []
|
|
52
|
+
}),
|
|
53
|
+
create: jest.fn((entity: unknown, data: Row) => {
|
|
54
|
+
const row = { id: `new-${created.length + 1}`, ...data }
|
|
55
|
+
created.push({ entity, data: row })
|
|
56
|
+
return row
|
|
57
|
+
}),
|
|
58
|
+
persist: jest.fn(),
|
|
59
|
+
flush: jest.fn(async () => undefined),
|
|
60
|
+
transactional: jest.fn(),
|
|
61
|
+
fork: jest.fn(),
|
|
62
|
+
}
|
|
63
|
+
em.transactional.mockImplementation(async (fn: (tem: unknown) => Promise<unknown>) => fn(em))
|
|
64
|
+
em.fork.mockReturnValue(em)
|
|
65
|
+
const ctx = { container: { resolve: jest.fn(() => em) }, auth: { sub: 'u', tenantId: TENANT, orgId: ORG } }
|
|
66
|
+
return { em, ctx, created, executed }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const memoOf = (e: ReturnType<typeof env>) => e.created.find((c) => c.entity === BillingInvoice)!.data
|
|
70
|
+
const linesOf = (e: ReturnType<typeof env>) => e.created.filter((c) => c.entity === BillingInvoiceLine).map((c) => c.data)
|
|
71
|
+
|
|
72
|
+
describe('createMemoCommand', () => {
|
|
73
|
+
it('full credit copies every original line with its own VAT rate and snapshots the original', async () => {
|
|
74
|
+
const e = env()
|
|
75
|
+
const result = await createMemoCommand.execute({ ...base, invoiceId: ORIGINAL, type: 'credit_memo', reason: 'Wrong price' }, e.ctx as never)
|
|
76
|
+
expect(result.number).toMatch(/^DRAFT-/)
|
|
77
|
+
expect(memoOf(e)).toMatchObject({
|
|
78
|
+
documentType: 'credit_memo',
|
|
79
|
+
status: 'draft',
|
|
80
|
+
correctsInvoiceId: ORIGINAL,
|
|
81
|
+
correctsInvoiceNumber: 'INV/2026/11/0001',
|
|
82
|
+
correctionReason: 'Wrong price',
|
|
83
|
+
billAccountId: 'acc-1',
|
|
84
|
+
currencyCode: 'PLN',
|
|
85
|
+
subtotalNet: '120.0000',
|
|
86
|
+
taxTotal: '24.6000',
|
|
87
|
+
totalGross: '144.6000',
|
|
88
|
+
})
|
|
89
|
+
expect(linesOf(e).map((l) => [l.correctsLineId, l.taxRate, l.grossAmount])).toEqual([
|
|
90
|
+
[LINE_A, '23', '123.0000'],
|
|
91
|
+
[LINE_B, '8', '21.6000'],
|
|
92
|
+
])
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('partial credit: copied lines keep the original rate even if another is sent; new lines take the org default', async () => {
|
|
96
|
+
const e = env()
|
|
97
|
+
await createMemoCommand.execute(
|
|
98
|
+
{
|
|
99
|
+
...base,
|
|
100
|
+
invoiceId: ORIGINAL,
|
|
101
|
+
type: 'credit_memo',
|
|
102
|
+
reason: 'Goodwill',
|
|
103
|
+
lines: [
|
|
104
|
+
{ correctsLineId: LINE_A, description: 'Internet (3 days)', quantity: 1, unitPriceNet: 10, taxRate: 0 },
|
|
105
|
+
{ description: 'Goodwill', quantity: 1, unitPriceNet: 5 },
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
e.ctx as never,
|
|
109
|
+
)
|
|
110
|
+
expect(linesOf(e).map((l) => [l.correctsLineId, l.taxRate, l.netAmount])).toEqual([
|
|
111
|
+
[LINE_A, '23', '10.0000'],
|
|
112
|
+
[null, '5', '5.0000'],
|
|
113
|
+
])
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('debit memo without lines starts empty', async () => {
|
|
117
|
+
const e = env()
|
|
118
|
+
await createMemoCommand.execute({ ...base, invoiceId: ORIGINAL, type: 'debit_memo', reason: 'Missed fee' }, e.ctx as never)
|
|
119
|
+
expect(memoOf(e)).toMatchObject({ documentType: 'debit_memo', totalGross: '0.0000' })
|
|
120
|
+
expect(linesOf(e)).toHaveLength(0)
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it.each([
|
|
124
|
+
[{ status: 'draft' }, 'credit_memo'],
|
|
125
|
+
[{ testRun: true }, 'credit_memo'],
|
|
126
|
+
[{ documentType: 'credit_memo' }, 'credit_memo'],
|
|
127
|
+
[{ documentType: 'receipt', status: 'paid' }, 'debit_memo'],
|
|
128
|
+
[{ status: 'credited' }, 'credit_memo'],
|
|
129
|
+
])('refuses originals %j for %s', async (overrides, type) => {
|
|
130
|
+
const e = env({ original: original(overrides) })
|
|
131
|
+
await expect(
|
|
132
|
+
createMemoCommand.execute({ ...base, invoiceId: ORIGINAL, type: type as 'credit_memo', reason: 'x' }, e.ctx as never),
|
|
133
|
+
).rejects.toMatchObject({ status: 409, body: expect.objectContaining({ code: 'billing.memo.invalid_original' }) })
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('allows a debit memo on a fully credited invoice', async () => {
|
|
137
|
+
const e = env({ original: original({ status: 'credited' }) })
|
|
138
|
+
await expect(
|
|
139
|
+
createMemoCommand.execute({ ...base, invoiceId: ORIGINAL, type: 'debit_memo', reason: 'Re-bill' }, e.ctx as never),
|
|
140
|
+
).resolves.toMatchObject({ documentType: 'debit_memo' })
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('requires a reason and rejects lines of another invoice', async () => {
|
|
144
|
+
const e = env()
|
|
145
|
+
await expect(createMemoCommand.execute({ ...base, invoiceId: ORIGINAL, type: 'credit_memo', reason: ' ' }, e.ctx as never)).rejects.toThrow()
|
|
146
|
+
await expect(
|
|
147
|
+
createMemoCommand.execute(
|
|
148
|
+
{
|
|
149
|
+
...base,
|
|
150
|
+
invoiceId: ORIGINAL,
|
|
151
|
+
type: 'credit_memo',
|
|
152
|
+
reason: 'x',
|
|
153
|
+
lines: [{ correctsLineId: '99999999-9999-4999-8999-999999999999', description: 'x', quantity: 1, unitPriceNet: 1 }],
|
|
154
|
+
},
|
|
155
|
+
e.ctx as never,
|
|
156
|
+
),
|
|
157
|
+
).rejects.toMatchObject({ body: expect.objectContaining({ code: 'billing.memo.invalid_line' }) })
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('refuses a credit above what is left to credit (early guard)', async () => {
|
|
161
|
+
const e = env({ corrections: [{ document_type: 'credit_memo', gross: '100.0000' }] })
|
|
162
|
+
await expect(
|
|
163
|
+
createMemoCommand.execute({ ...base, invoiceId: ORIGINAL, type: 'credit_memo', reason: 'Full' }, e.ctx as never),
|
|
164
|
+
).rejects.toMatchObject({ body: expect.objectContaining({ code: 'billing.memo.exceeds_invoice' }) })
|
|
165
|
+
})
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
describe('deleteDraftMemoCommand', () => {
|
|
169
|
+
it('deletes a draft memo with its lines', async () => {
|
|
170
|
+
const e = env({ target: { id: 'm-1', ...base, status: 'draft', documentType: 'credit_memo' } })
|
|
171
|
+
await expect(deleteDraftMemoCommand.execute({ ...base, invoiceId: '66666666-6666-4666-8666-666666666666' }, e.ctx as never)).resolves.toEqual({ deleted: true })
|
|
172
|
+
expect(e.executed.filter((sql) => sql.startsWith('DELETE'))).toHaveLength(2)
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it('never deletes posted memos or invoice drafts', async () => {
|
|
176
|
+
for (const target of [
|
|
177
|
+
{ id: 'm-1', ...base, status: 'posted', documentType: 'credit_memo' },
|
|
178
|
+
{ id: 'i-1', ...base, status: 'draft', documentType: 'invoice' },
|
|
179
|
+
]) {
|
|
180
|
+
const e = env({ target })
|
|
181
|
+
await expect(deleteDraftMemoCommand.execute({ ...base, invoiceId: '66666666-6666-4666-8666-666666666666' }, e.ctx as never)).rejects.toMatchObject({
|
|
182
|
+
body: expect.objectContaining({ code: 'billing.memo.not_draft' }),
|
|
183
|
+
})
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
describe('document type helpers and credit guard', () => {
|
|
189
|
+
it('classifies document types', () => {
|
|
190
|
+
expect(seriesFor('credit_memo')).toBe('credit_memo')
|
|
191
|
+
expect(seriesFor('debit_memo')).toBe('invoice')
|
|
192
|
+
expect(seriesFor('receipt')).toBe('receipt')
|
|
193
|
+
expect(isReceivable('debit_memo')).toBe(true)
|
|
194
|
+
expect(isReceivable('credit_memo')).toBe(false)
|
|
195
|
+
expect(isMemo('credit_memo')).toBe(true)
|
|
196
|
+
expect(displaySign('credit_memo')).toBe(-1)
|
|
197
|
+
expect(canCorrect({ documentType: 'invoice', status: 'paid', testRun: false }, 'credit_memo')).toBe(true)
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
it('computes the credit headroom including debit memos', () => {
|
|
201
|
+
expect(creditHeadroomCents(10000, { creditCents: 3000, debitCents: 2000 })).toBe(9000)
|
|
202
|
+
expect(creditFits(10000, { creditCents: 3000, debitCents: 2000 }, 9000)).toBe(true)
|
|
203
|
+
expect(creditFits(10000, { creditCents: 3000, debitCents: 2000 }, 9001)).toBe(false)
|
|
204
|
+
})
|
|
205
|
+
})
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
jest.mock('../../lib/accountLock', () => ({
|
|
2
|
+
withAccountLock: jest.fn(async (em: unknown, _s: unknown, fn: (tem: unknown) => Promise<unknown>) => fn(em)),
|
|
3
|
+
}))
|
|
4
|
+
jest.mock('../../lib/allocation', () => {
|
|
5
|
+
const actual = jest.requireActual('../../lib/allocation')
|
|
6
|
+
return {
|
|
7
|
+
...actual,
|
|
8
|
+
allocate: jest.fn(async () => actual.emptyEffects()),
|
|
9
|
+
unallocate: jest.fn(async () => actual.emptyEffects()),
|
|
10
|
+
applyAccountCredit: jest.fn(async () => actual.emptyEffects()),
|
|
11
|
+
}
|
|
12
|
+
})
|
|
13
|
+
jest.mock('../../lib/topupCapture', () => ({ captureTopup: jest.fn(async () => ({ status: 'captured' })) }))
|
|
14
|
+
jest.mock('../../events', () => ({ emitBillingEvent: jest.fn(async () => undefined) }))
|
|
15
|
+
|
|
16
|
+
import { BillingAccount, BillingInvoice, BillingPayment, BillingSettings, BillingTopup } from '../../data/entities'
|
|
17
|
+
import { allocate, applyAccountCredit, unallocate } from '../../lib/allocation'
|
|
18
|
+
import { captureTopup } from '../../lib/topupCapture'
|
|
19
|
+
import { emitBillingEvent } from '../../events'
|
|
20
|
+
import {
|
|
21
|
+
allocatePaymentCommand,
|
|
22
|
+
assignPaymentCommand,
|
|
23
|
+
convertToTopupCommand,
|
|
24
|
+
createPaymentCommand,
|
|
25
|
+
ignorePaymentCommand,
|
|
26
|
+
restorePaymentCommand,
|
|
27
|
+
unallocateCommand,
|
|
28
|
+
unassignPaymentCommand,
|
|
29
|
+
} from '../../commands/payments'
|
|
30
|
+
import { recordGatewayCapture } from '../../subscribers/payment-captured-to-paid'
|
|
31
|
+
import { applyCreditForAccount } from '../../subscribers/invoice-apply-credit'
|
|
32
|
+
|
|
33
|
+
const TENANT = '11111111-1111-4111-8111-111111111111'
|
|
34
|
+
const ORG = '22222222-2222-4222-8222-222222222222'
|
|
35
|
+
const PAY = '33333333-3333-4333-8333-333333333333'
|
|
36
|
+
const ACC = '44444444-4444-4444-8444-444444444444'
|
|
37
|
+
const INV = '55555555-5555-4555-8555-555555555555'
|
|
38
|
+
const ALLOC = '66666666-6666-4666-8666-666666666666'
|
|
39
|
+
const base = { tenantId: TENANT, organizationId: ORG }
|
|
40
|
+
|
|
41
|
+
type Row = Record<string, unknown>
|
|
42
|
+
|
|
43
|
+
function env(opts: { payment?: Row | null; account?: Row | null; invoice?: Row | null; allocations?: number; existingByExternalRef?: Row | null } = {}) {
|
|
44
|
+
const executed: Array<{ sql: string; params: unknown[] }> = []
|
|
45
|
+
const created: Row[] = []
|
|
46
|
+
const em = {
|
|
47
|
+
findOne: jest.fn(async (entity: unknown, where: Row) => {
|
|
48
|
+
if (entity === BillingPayment) return 'externalRef' in where ? (opts.existingByExternalRef ?? null) : (opts.payment ?? null)
|
|
49
|
+
if (entity === BillingAccount) return opts.account ?? null
|
|
50
|
+
if (entity === BillingInvoice) return opts.invoice ?? null
|
|
51
|
+
if (entity === BillingSettings) return { topupTaxRate: '23' }
|
|
52
|
+
if (entity === BillingTopup) return null
|
|
53
|
+
return null
|
|
54
|
+
}),
|
|
55
|
+
execute: jest.fn(async (sql: string, params: unknown[] = []) => {
|
|
56
|
+
executed.push({ sql, params })
|
|
57
|
+
if (sql.includes('COUNT(*)::int AS n FROM billing_allocations')) return [{ n: opts.allocations ?? 0 }]
|
|
58
|
+
if (sql.includes('SELECT status, unallocated_amount')) return [{ status: 'matched', unallocated_amount: '10.0000' }]
|
|
59
|
+
if (sql.includes('SELECT bill_account_id FROM billing_allocations')) return opts.allocations === -1 ? [] : [{ bill_account_id: ACC }]
|
|
60
|
+
return []
|
|
61
|
+
}),
|
|
62
|
+
create: jest.fn((_e: unknown, data: Row) => {
|
|
63
|
+
const row = { id: `new-${created.length + 1}`, ...data }
|
|
64
|
+
created.push(row)
|
|
65
|
+
return row
|
|
66
|
+
}),
|
|
67
|
+
persist: jest.fn(),
|
|
68
|
+
flush: jest.fn(async () => undefined),
|
|
69
|
+
fork: jest.fn(),
|
|
70
|
+
}
|
|
71
|
+
em.fork.mockReturnValue(em)
|
|
72
|
+
const ctx = { container: { resolve: jest.fn(() => em) }, auth: { sub: 'user-1', tenantId: TENANT, orgId: ORG } }
|
|
73
|
+
return { em, ctx, executed, created }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const payment = (overrides: Row = {}): Row => ({
|
|
77
|
+
id: PAY,
|
|
78
|
+
...base,
|
|
79
|
+
amount: '100.0000',
|
|
80
|
+
unallocatedAmount: '100.0000',
|
|
81
|
+
currencyCode: 'PLN',
|
|
82
|
+
status: 'unmatched',
|
|
83
|
+
billAccountId: null,
|
|
84
|
+
reference: 'x',
|
|
85
|
+
...overrides,
|
|
86
|
+
})
|
|
87
|
+
const account = (overrides: Row = {}): Row => ({
|
|
88
|
+
id: ACC,
|
|
89
|
+
...base,
|
|
90
|
+
currencyCode: 'PLN',
|
|
91
|
+
billingMode: 'postpaid',
|
|
92
|
+
...overrides,
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
beforeEach(() => jest.clearAllMocks())
|
|
96
|
+
|
|
97
|
+
describe('payment commands', () => {
|
|
98
|
+
it('manual payment with an account is matched and spends its credit', async () => {
|
|
99
|
+
const e = env({ account: account() })
|
|
100
|
+
await createPaymentCommand.execute(
|
|
101
|
+
{ ...base, receivedAt: '2026-11-20', amount: 50, currencyCode: 'pln', billAccountId: ACC },
|
|
102
|
+
e.ctx as never,
|
|
103
|
+
)
|
|
104
|
+
expect(e.created[0]).toMatchObject({ status: 'matched', amount: '50.0000', unallocatedAmount: '50.0000', source: 'manual', billAccountId: ACC })
|
|
105
|
+
expect(applyAccountCredit).toHaveBeenCalledWith(e.em, base, { id: ACC, currencyCode: 'PLN' }, { onlySources: [{ type: 'payment', id: 'new-1' }] })
|
|
106
|
+
expect(emitBillingEvent).toHaveBeenCalledWith('billing.payment.created', expect.objectContaining({ source: 'manual' }))
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('manual payment without an account stays unmatched', async () => {
|
|
110
|
+
const e = env()
|
|
111
|
+
await createPaymentCommand.execute({ ...base, receivedAt: '2026-11-20', amount: 50, currencyCode: 'PLN' }, e.ctx as never)
|
|
112
|
+
expect(e.created[0]).toMatchObject({ status: 'unmatched', matchReason: 'manual' })
|
|
113
|
+
expect(applyAccountCredit).not.toHaveBeenCalled()
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('assigns an unmatched payment and applies it as credit; refuses prepaid / currency / non-unmatched', async () => {
|
|
117
|
+
const ok = env({ payment: payment(), account: account() })
|
|
118
|
+
await assignPaymentCommand.execute({ ...base, paymentId: PAY, billAccountId: ACC }, ok.ctx as never)
|
|
119
|
+
expect(ok.executed.some((x) => x.sql.includes("status = 'matched'"))).toBe(true)
|
|
120
|
+
expect(applyAccountCredit).toHaveBeenCalled()
|
|
121
|
+
|
|
122
|
+
const prepaid = env({ payment: payment(), account: account({ billingMode: 'prepaid' }) })
|
|
123
|
+
await expect(assignPaymentCommand.execute({ ...base, paymentId: PAY, billAccountId: ACC }, prepaid.ctx as never)).rejects.toMatchObject({
|
|
124
|
+
body: expect.objectContaining({ code: 'billing.payment.prepaid_account' }),
|
|
125
|
+
})
|
|
126
|
+
const eur = env({ payment: payment({ currencyCode: 'EUR' }), account: account() })
|
|
127
|
+
await expect(assignPaymentCommand.execute({ ...base, paymentId: PAY, billAccountId: ACC }, eur.ctx as never)).rejects.toMatchObject({
|
|
128
|
+
body: expect.objectContaining({ code: 'billing.payment.currency_mismatch' }),
|
|
129
|
+
})
|
|
130
|
+
const done = env({ payment: payment({ status: 'allocated' }), account: account() })
|
|
131
|
+
await expect(assignPaymentCommand.execute({ ...base, paymentId: PAY, billAccountId: ACC }, done.ctx as never)).rejects.toMatchObject({ status: 409 })
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('unassign requires no allocations', async () => {
|
|
135
|
+
const busy = env({ payment: payment({ status: 'matched', billAccountId: ACC }), allocations: 1 })
|
|
136
|
+
await expect(unassignPaymentCommand.execute({ ...base, paymentId: PAY }, busy.ctx as never)).rejects.toMatchObject({
|
|
137
|
+
body: expect.objectContaining({ code: 'billing.payment.has_allocations' }),
|
|
138
|
+
})
|
|
139
|
+
const free = env({ payment: payment({ status: 'matched', billAccountId: ACC }) })
|
|
140
|
+
await unassignPaymentCommand.execute({ ...base, paymentId: PAY }, free.ctx as never)
|
|
141
|
+
expect(free.executed.some((x) => x.sql.includes('bill_account_id = NULL'))).toBe(true)
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
it('allocates each requested amount in cents through the allocation service', async () => {
|
|
145
|
+
const e = env({ payment: payment({ status: 'matched', billAccountId: ACC }) })
|
|
146
|
+
await allocatePaymentCommand.execute({ ...base, paymentId: PAY, allocations: [{ invoiceId: INV, amount: 12.34 }] }, e.ctx as never)
|
|
147
|
+
expect(allocate).toHaveBeenCalledWith(e.em, base, {
|
|
148
|
+
source: { type: 'payment', id: PAY },
|
|
149
|
+
invoiceId: INV,
|
|
150
|
+
amountCents: 1234,
|
|
151
|
+
by: 'user',
|
|
152
|
+
userId: 'user-1',
|
|
153
|
+
})
|
|
154
|
+
const unassigned = env({ payment: payment() })
|
|
155
|
+
await expect(
|
|
156
|
+
allocatePaymentCommand.execute({ ...base, paymentId: PAY, allocations: [{ invoiceId: INV, amount: 1 }] }, unassigned.ctx as never),
|
|
157
|
+
).rejects.toMatchObject({ body: expect.objectContaining({ code: 'billing.payment.unassigned' }) })
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('un-allocates by allocation id (404 when unknown)', async () => {
|
|
161
|
+
const e = env()
|
|
162
|
+
await unallocateCommand.execute({ ...base, allocationId: ALLOC }, e.ctx as never)
|
|
163
|
+
expect(unallocate).toHaveBeenCalledWith(e.em, base, { allocationId: ALLOC, userId: 'user-1' })
|
|
164
|
+
const missing = env({ allocations: -1 })
|
|
165
|
+
await expect(unallocateCommand.execute({ ...base, allocationId: ALLOC }, missing.ctx as never)).rejects.toMatchObject({ status: 404 })
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
it('ignores only unallocated payments and restores them to their previous state', async () => {
|
|
169
|
+
const busy = env({ payment: payment({ status: 'matched', billAccountId: ACC }), allocations: 2 })
|
|
170
|
+
await expect(ignorePaymentCommand.execute({ ...base, paymentId: PAY }, busy.ctx as never)).rejects.toMatchObject({ status: 409 })
|
|
171
|
+
const ok = env({ payment: payment() })
|
|
172
|
+
await ignorePaymentCommand.execute({ ...base, paymentId: PAY }, ok.ctx as never)
|
|
173
|
+
expect(ok.executed.some((x) => x.sql.includes("status = 'ignored'"))).toBe(true)
|
|
174
|
+
|
|
175
|
+
const ignored = env({ payment: payment({ status: 'ignored', billAccountId: ACC }), account: account() })
|
|
176
|
+
await restorePaymentCommand.execute({ ...base, paymentId: PAY }, ignored.ctx as never)
|
|
177
|
+
expect(ignored.executed.some((x) => x.params[0] === 'matched')).toBe(true)
|
|
178
|
+
expect(applyAccountCredit).toHaveBeenCalled()
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it('converts an unmatched payment of a prepaid account into a captured top-up', async () => {
|
|
182
|
+
const e = env({ payment: payment(), account: account({ billingMode: 'prepaid' }) })
|
|
183
|
+
const result = await convertToTopupCommand.execute({ ...base, paymentId: PAY, billAccountId: ACC }, e.ctx as never)
|
|
184
|
+
expect(e.created[0]).toMatchObject({ providerKey: 'bank_transfer', paymentId: PAY, sourceRef: `topup-${PAY}`, amount: '100.0000' })
|
|
185
|
+
expect(captureTopup).toHaveBeenCalledWith(e.em, expect.objectContaining({ taxRate: 23 }))
|
|
186
|
+
expect(e.executed.some((x) => x.sql.includes("status = 'converted_to_topup'"))).toBe(true)
|
|
187
|
+
expect(result.topupId).toBe('new-1')
|
|
188
|
+
expect(emitBillingEvent).toHaveBeenCalledWith('billing.payment.converted_to_topup', expect.anything())
|
|
189
|
+
|
|
190
|
+
const postpaid = env({ payment: payment(), account: account() })
|
|
191
|
+
await expect(convertToTopupCommand.execute({ ...base, paymentId: PAY, billAccountId: ACC }, postpaid.ctx as never)).rejects.toMatchObject({
|
|
192
|
+
body: expect.objectContaining({ code: 'billing.payment.not_prepaid' }),
|
|
193
|
+
})
|
|
194
|
+
const partly = env({ payment: payment({ unallocatedAmount: '40.0000' }), account: account({ billingMode: 'prepaid' }) })
|
|
195
|
+
await expect(convertToTopupCommand.execute({ ...base, paymentId: PAY, billAccountId: ACC }, partly.ctx as never)).rejects.toMatchObject({
|
|
196
|
+
body: expect.objectContaining({ code: 'billing.payment.not_convertible' }),
|
|
197
|
+
})
|
|
198
|
+
})
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
describe('gateway capture subscriber', () => {
|
|
202
|
+
const invoice = (overrides: Row = {}): Row => ({
|
|
203
|
+
id: INV,
|
|
204
|
+
...base,
|
|
205
|
+
billAccountId: ACC,
|
|
206
|
+
documentType: 'invoice',
|
|
207
|
+
status: 'posted',
|
|
208
|
+
testRun: false,
|
|
209
|
+
number: 'INV/2026/11/0001',
|
|
210
|
+
currencyCode: 'PLN',
|
|
211
|
+
outstanding: '123.0000',
|
|
212
|
+
...overrides,
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it('records a gateway payment allocated to the invoice', async () => {
|
|
216
|
+
const e = env({ invoice: invoice() })
|
|
217
|
+
expect(await recordGatewayCapture(e.em as never, { ...base, paymentId: INV, transactionId: 'tx-1', amount: 123 })).toBe('recorded')
|
|
218
|
+
expect(e.created[0]).toMatchObject({ source: 'gateway', externalRef: 'tx-1', amount: '123.0000', billAccountId: ACC, status: 'matched' })
|
|
219
|
+
expect(allocate).toHaveBeenCalledWith(e.em, base, expect.objectContaining({ invoiceId: INV, amountCents: 12300, by: 'gateway' }))
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
it('is idempotent and ignores non-billing payments', async () => {
|
|
223
|
+
const dup = env({ invoice: invoice(), existingByExternalRef: { id: 'p' } })
|
|
224
|
+
expect(await recordGatewayCapture(dup.em as never, { ...base, paymentId: INV, transactionId: 'tx-1' })).toBe('duplicate')
|
|
225
|
+
const foreign = env({ invoice: null })
|
|
226
|
+
expect(await recordGatewayCapture(foreign.em as never, { ...base, paymentId: INV })).toBe('skipped')
|
|
227
|
+
expect(await recordGatewayCapture(env().em as never, { paymentId: INV })).toBe('skipped')
|
|
228
|
+
expect(allocate).not.toHaveBeenCalled()
|
|
229
|
+
})
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
describe('credit on post subscriber', () => {
|
|
233
|
+
it('spends account credit for postpaid accounts only', async () => {
|
|
234
|
+
const e = env({ account: account() })
|
|
235
|
+
await applyCreditForAccount(e.em as never, { ...base, billAccountId: ACC })
|
|
236
|
+
expect(applyAccountCredit).toHaveBeenCalledWith(e.em, base, { id: ACC, currencyCode: 'PLN' })
|
|
237
|
+
jest.clearAllMocks()
|
|
238
|
+
await applyCreditForAccount(env({ account: account({ billingMode: 'prepaid' }) }).em as never, { ...base, billAccountId: ACC })
|
|
239
|
+
expect(applyAccountCredit).not.toHaveBeenCalled()
|
|
240
|
+
})
|
|
241
|
+
})
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spec F (F1): deleting test / dry Bill Runs and the shared test-run cleanup.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
jest.mock('../../events', () => ({ emitBillingEvent: jest.fn(async () => undefined) }))
|
|
6
|
+
jest.mock('../../lib/billRunEngine', () => ({ runBillRun: jest.fn() }))
|
|
7
|
+
|
|
8
|
+
import { emitBillingEvent } from '../../events'
|
|
9
|
+
import { deleteRunCommand, runMode } from '../../commands/runs'
|
|
10
|
+
import { deleteTestRunArtifacts } from '../../lib/testRunCleanup'
|
|
11
|
+
|
|
12
|
+
const TENANT = '11111111-1111-4111-8111-111111111111'
|
|
13
|
+
const ORG = '22222222-2222-4222-8222-222222222222'
|
|
14
|
+
const RUN_ID = '33333333-3333-4333-8333-333333333333'
|
|
15
|
+
|
|
16
|
+
type Executed = { sql: string; params: unknown[] }
|
|
17
|
+
type RunRow = { id: string; dryRun: boolean; testMode: boolean; status: string }
|
|
18
|
+
|
|
19
|
+
function createEnv(opts: { run: RunRow | null; testInvoiceIds?: string[] }) {
|
|
20
|
+
const executed: Executed[] = []
|
|
21
|
+
type RunsMockEm = {
|
|
22
|
+
findOne: jest.Mock
|
|
23
|
+
execute: jest.Mock
|
|
24
|
+
transactional: jest.Mock
|
|
25
|
+
fork: jest.Mock
|
|
26
|
+
}
|
|
27
|
+
const em: RunsMockEm = {
|
|
28
|
+
findOne: jest.fn(async (_entity: unknown, where: Record<string, unknown>) => {
|
|
29
|
+
if (!opts.run) return null
|
|
30
|
+
if (where.tenantId !== TENANT || where.organizationId !== ORG) return null
|
|
31
|
+
return opts.run
|
|
32
|
+
}),
|
|
33
|
+
execute: jest.fn(async (sql: string, params: unknown[] = []) => {
|
|
34
|
+
executed.push({ sql, params })
|
|
35
|
+
if (sql.trim().startsWith('SELECT id FROM billing_invoices')) {
|
|
36
|
+
return (opts.testInvoiceIds ?? []).map((id) => ({ id }))
|
|
37
|
+
}
|
|
38
|
+
return []
|
|
39
|
+
}),
|
|
40
|
+
transactional: jest.fn(async (fn: (tem: unknown) => Promise<unknown>): Promise<unknown> => fn(em)),
|
|
41
|
+
fork: jest.fn((): unknown => em),
|
|
42
|
+
}
|
|
43
|
+
const ctx = {
|
|
44
|
+
container: { resolve: jest.fn(() => em) },
|
|
45
|
+
auth: { tenantId: TENANT, orgId: ORG },
|
|
46
|
+
selectedOrganizationId: ORG,
|
|
47
|
+
organizationIds: [ORG],
|
|
48
|
+
organizationScope: null,
|
|
49
|
+
}
|
|
50
|
+
return { em, ctx, executed }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const input = { tenantId: TENANT, organizationId: ORG, billRunId: RUN_ID }
|
|
54
|
+
|
|
55
|
+
describe('runMode', () => {
|
|
56
|
+
it('maps the run flags to a mode', () => {
|
|
57
|
+
expect(runMode({ dryRun: true, testMode: false })).toBe('dry')
|
|
58
|
+
expect(runMode({ dryRun: false, testMode: true })).toBe('test')
|
|
59
|
+
expect(runMode({ dryRun: false, testMode: false })).toBe('real')
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
describe('deleteRunCommand', () => {
|
|
64
|
+
beforeEach(() => jest.clearAllMocks())
|
|
65
|
+
|
|
66
|
+
it('refuses a real run with 422 and touches nothing', async () => {
|
|
67
|
+
const env = createEnv({ run: { id: RUN_ID, dryRun: false, testMode: false, status: 'completed' } })
|
|
68
|
+
await expect(deleteRunCommand.execute(input, env.ctx as never)).rejects.toMatchObject({
|
|
69
|
+
status: 422,
|
|
70
|
+
body: { code: 'billing.run.delete_real_forbidden' },
|
|
71
|
+
})
|
|
72
|
+
expect(env.executed).toHaveLength(0)
|
|
73
|
+
expect(emitBillingEvent).not.toHaveBeenCalled()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('refuses a running run with 409', async () => {
|
|
77
|
+
const env = createEnv({ run: { id: RUN_ID, dryRun: false, testMode: true, status: 'running' } })
|
|
78
|
+
await expect(deleteRunCommand.execute(input, env.ctx as never)).rejects.toMatchObject({
|
|
79
|
+
status: 409,
|
|
80
|
+
body: { code: 'billing.run.running' },
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('returns 404 when the run is missing or belongs to another organization', async () => {
|
|
85
|
+
const env = createEnv({ run: null })
|
|
86
|
+
await expect(deleteRunCommand.execute(input, env.ctx as never)).rejects.toMatchObject({ status: 404 })
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('deletes a test run: invoices (edits, lines, headers), parent links, outcomes, then the run', async () => {
|
|
90
|
+
const env = createEnv({
|
|
91
|
+
run: { id: RUN_ID, dryRun: false, testMode: true, status: 'completed' },
|
|
92
|
+
testInvoiceIds: ['inv-a', 'inv-b'],
|
|
93
|
+
})
|
|
94
|
+
const result = await deleteRunCommand.execute(input, env.ctx as never)
|
|
95
|
+
expect(result).toEqual({ ok: true, invoicesRemoved: 2 })
|
|
96
|
+
const writes = env.executed.filter((e) => !e.sql.trim().startsWith('SELECT')).map((e) => e.sql.split(/\s+/).slice(0, 3).join(' '))
|
|
97
|
+
expect(writes).toEqual([
|
|
98
|
+
'DELETE FROM billing_draft_invoice_edits',
|
|
99
|
+
'DELETE FROM billing_invoice_lines',
|
|
100
|
+
'DELETE FROM billing_invoices',
|
|
101
|
+
'UPDATE billing_runs SET',
|
|
102
|
+
'DELETE FROM billing_run_outcomes',
|
|
103
|
+
'DELETE FROM billing_runs',
|
|
104
|
+
])
|
|
105
|
+
// Every write that filters by scope carries tenant + organization.
|
|
106
|
+
const scoped = env.executed.filter((e) => e.sql.includes('tenant_id = ?'))
|
|
107
|
+
for (const row of scoped) {
|
|
108
|
+
expect(row.params.slice(0, 2)).toEqual([TENANT, ORG])
|
|
109
|
+
}
|
|
110
|
+
expect(emitBillingEvent).toHaveBeenCalledWith(
|
|
111
|
+
'billing.run.deleted',
|
|
112
|
+
expect.objectContaining({ billRunId: RUN_ID, mode: 'test', invoicesRemoved: 2 }),
|
|
113
|
+
{ persistent: true },
|
|
114
|
+
)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('deletes a dry run that wrote no invoices', async () => {
|
|
118
|
+
const env = createEnv({ run: { id: RUN_ID, dryRun: true, testMode: false, status: 'completed' } })
|
|
119
|
+
const result = await deleteRunCommand.execute(input, env.ctx as never)
|
|
120
|
+
expect(result.invoicesRemoved).toBe(0)
|
|
121
|
+
expect(env.executed.some((e) => e.sql.startsWith('DELETE FROM billing_invoices'))).toBe(false)
|
|
122
|
+
expect(env.executed.some((e) => e.sql.startsWith('DELETE FROM billing_runs'))).toBe(true)
|
|
123
|
+
})
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
describe('deleteTestRunArtifacts', () => {
|
|
127
|
+
it('only targets test invoices (never memos or real documents) and scopes by run', async () => {
|
|
128
|
+
const env = createEnv({ run: null, testInvoiceIds: [] })
|
|
129
|
+
await deleteTestRunArtifacts(env.em as never, { tenantId: TENANT, organizationId: ORG, billRunId: RUN_ID })
|
|
130
|
+
const select = env.executed[0]
|
|
131
|
+
expect(select.sql).toContain('test_run = true')
|
|
132
|
+
expect(select.sql).toContain("document_type = 'invoice'")
|
|
133
|
+
expect(select.sql).toContain('bill_run_id = ?')
|
|
134
|
+
expect(select.params).toEqual([TENANT, ORG, RUN_ID])
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('removes draft-edit audit rows before lines and invoices', async () => {
|
|
138
|
+
const env = createEnv({ run: null, testInvoiceIds: ['x'] })
|
|
139
|
+
expect(await deleteTestRunArtifacts(env.em as never, { tenantId: TENANT, organizationId: ORG })).toBe(1)
|
|
140
|
+
expect(env.executed.slice(1).map((e) => e.sql.split(' ')[2])).toEqual([
|
|
141
|
+
'billing_draft_invoice_edits',
|
|
142
|
+
'billing_invoice_lines',
|
|
143
|
+
'billing_invoices',
|
|
144
|
+
])
|
|
145
|
+
})
|
|
146
|
+
})
|