Skip to content

CS-649 fix: allocate invoice number on commit so unsaved invoices don't skip numbers - #1585

Open
yuryyas wants to merge 2 commits into
mainfrom
CS-649_Invoice_numbers_skipped
Open

yuryyas wants to merge 2 commits into
mainfrom
CS-649_Invoice_numbers_skipped

Conversation

@yuryyas

@yuryyas yuryyas commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

Invoice numbers were taken when an Invoice object was created, not when it was saved. Several flows build invoices that are never committed, and each one burned a number. CCE's gap (469922 → 469927 with consecutive DB ids 469916, 469917) matches this: no invoice is missing, four numbers were taken and thrown away.

 Invoice
-  postAdd()      # on newObject()
-    invoiceNumber ?= autoIncrementService.nextInvoiceNumber
+  prePersist()   # on commit, after validation, just before INSERT
+    invoiceNumber ?= autoIncrementService.nextInvoiceNumber

Flows that used to burn a number without saving anything:

POST /checkout/update        → CheckoutController.createCheckout   (every summary recalculation)
POST /checkout/createSession → imitateCheckoutCommitForValidation  (validate-only, no commit)
"Duplicate and reverse" preview → InvoiceApiService
funding invoices built during checkout

An unsaved invoice now has no number until commit, so getOriginalInvoiceLine() (Enrolment, EnrolmentMixin, EnrolmentService) uses one shared null-safe comparator, EnrolmentService.BY_INVOICE_NUMBER. It keeps lines of a not yet committed invoice last, as before. Without it the Java version would throw an NPE and the Groovy ones would treat the unsaved invoice as the oldest.

A number set explicitly before commit (e.g. quote → invoice) is kept.

Evidence

New InvoiceTest.testInvoiceNumberIsAllocatedOnCommitOnly:

commit invoice A
build invoice B, never commit   → B.invoiceNumber == null
commit invoice C                → C.invoiceNumber == A.invoiceNumber + 1
  • Before (old postAdd()): fails with expected: <null> but was: <2>, because the unsaved invoice took a number.
    After: InvoiceTest 6/6 passed.
  • ClassBudgetUtilTest (4) and ClassCostUtilTest (21) go through getOriginalInvoiceLine() and all passed.

These ran locally under server:integration against MariaDB with TZ=Australia/Sydney.

Merge Danger

Door: two-way

No schema or data change. Reverting restores the old behaviour. Numbers that were already skipped stay skipped.

Blast Radius: invoicing

Any code that reads invoiceNumber of a new, not yet committed invoice now gets null. I checked checkout: submitPayment and succeedPayment commit before fillResponse reads the number, and the client shows it only in the post-payment message. /checkout/update now returns invoiceNumber: null, which the summary doesn't display. Customer scripts that read the number of a brand-new invoice before it is saved would also see null.

🤖 Generated with Claude Code

…'t skip numbers

Invoice.postAdd() took the next invoice number as soon as an Invoice object
was created. Checkout recalculation (/checkout/update), card payment
createSession validation, the "Duplicate and reverse" preview and funding
invoices built during checkout all create invoices that are never committed,
so each of them burned a number and left gaps in the sequence.

The number is now allocated in prePersist(), i.e. on commit only. Since an
unsaved invoice has no number until then, getOriginalInvoiceLine() in
Enrolment, EnrolmentMixin and EnrolmentService share one null-safe comparator
that keeps lines of a not yet committed invoice last, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Unit Test Results

358 tests  ±0   358 ✔️ ±0   8m 59s ⏱️ - 1m 48s
241 suites ±0       0 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit fff397e. ± Comparison against base commit d00f9be.

♻️ This comment has been updated with latest results.

@claude

claude Bot commented Nov 20, 2026

Copy link
Copy Markdown

Claude review

Jira: CS-649. The Jira tool wasn't available in this run, so I couldn't check the change against the ticket. This incremental range adds no new CS-649 code changes.

Reviewed range: 3114ac361b..fff397e268

Assessment: This range is only a merge of main into the branch. The one file it brings in, .github/workflows/claude-review.yml, is already on main in the same form (git diff from the base to the head doesn't include it). The PR's own changes (Invoice.groovy, Enrolment.groovy, EnrolmentMixin.groovy, EnrolmentService.java, InvoiceTest.groovy) are the same as in commit 3114ac361b.

Issues: No issues found.

Reviewed up to fff397e

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant