Skip to content

feat(receipts): generate the tax receipt PDF - #102

Open
jspngl wants to merge 1 commit into
developfrom
feature/83-receipt-pdf
Open

jspngl wants to merge 1 commit into
developfrom
feature/83-receipt-pdf

Conversation

@jspngl

@jspngl jspngl commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

Overview

Closes #83, closes #84, closes #85

The goal is to generate the official tax receipt PDF given to donors, with the mandatory CRA mentions, and to make sure a receipt never changes once it is issued.

  • PDF generation: GET /api/receipts/{id}/pdf returns the receipt on letter paper, in French with English, with the font embedded so accents print correctly. Long names and descriptions wrap, and long receipts continue on more pages with the table header repeated. An issued receipt is printed from its frozen data, a cancelled one with a "cancelled" banner, and a receipt not issued yet as a preview with no number.
  • CRA mentions: official receipt statement, organization name, address and registration number, serial number, place of issue, donation and issue dates, donor name and address, description and fair market value, advantage and eligible amount, appraiser (when set), authorized signature and CRA website. The list still has to be validated with the client and against the CRA documentation (see below).
  • Refusal when data is missing: issuing a receipt or generating its PDF is refused with a 422 that lists each problem in plain words (incomplete donor address, organization settings not filled, total not matching its lines, characters the font cannot print such as emojis). A refused issue does not use a receipt number.
  • Freezing at issue: the donor's name and address and each line's description and date are copied onto the receipt when it is issued, so later changes to the donor profile or food items don't alter it.
  • Cancel and replace: POST /api/receipts/{id}/replace cancels an issued receipt (it keeps its number) and creates a Draft replacement that references it. Once issued, the replacement's PDF states which receipt it cancels and replaces.
  • Read-only issued receipts: two database triggers refuse any change to an issued receipt except its cancellation.
  • Migration 20261204154702_AddReceiptIssueDetails: adds the new columns and fills them in for receipts already issued. The SQL script is 0015_add_receipt_issue_details.sql, and the docs and diagrams are updated.

For reviewers

  • Skip the generated files: *.Designer.cs and MoissonDbContextModelSnapshot.cs.
  • The hand-written SQL (backfill and triggers) is in 20261204154702_AddReceiptIssueDetails.cs.
  • BackupTestData.sql also fixes a test-data bug already on develop (see Testing).

Still to confirm with Moisson Montréal

  • The real ReceiptIssuer settings (legal name, address, registration number, signatory). They are empty for now, so issuing is refused until they are filled.
  • The layout against their receipt template.
  • The CRA mention list, checked against the CRA documentation and Quebec requirements.
  • Their eligibility for the free QuestPDF Community license (registered charities qualify).

Full details: docs/receipts/tax-receipt-pdf.md

Testing

Automated tests: all 1,081 tests pass (dotnet test, with Docker running for the integration tests).

  • ReceiptPdfRendererTests: checks each CRA mention in the text extracted from the PDF. The expected wording is written separately in the test so a removed mention fails it. Also covers accents and special characters, letter size, embedded fonts, pagination with 120 lines, long text wrapping, cancelled and preview banners, and the replacement statement.
  • ReceiptIssueCheckTests: each refusal reason and its message.
  • ReceiptDocumentEndpointTests: end to end from a real price-grid calculation through approval, issue and PDF download; a donor change after issue doesn't change the PDF; refusals when data is missing; cancel and replace; permissions; the database triggers; and simultaneous replace requests.
  • ReceiptIssueDetailsMigrationTests: receipts issued before the migration get their frozen data.
  • The existing receipt tests were updated, since issuing now needs a complete donor and real lines.

Manual: rendered sample receipts and checked the layout visually (header, donor block, table, totals, signature, page footer).

Backup tests were already failing on develop

  • On a clean copy of develop (before any of this PR's changes), all 39 backup tests (BackupRestoreTests, from Feature/71 db backup restore #99) failed during setup.
  • Cause: their seed data (tests/MoissonMontreal.Tests/Fixtures/BackupTestData.sql) inserts donors without search_name. The donor search PR (feat(donors): add the paginated donor list with search by name #98) made that column required (NOT NULL), and the two PRs were merged separately, so the seed never got updated. Setup fails with null value in column "search_name" of relation "donors" violates not-null constraint.
  • Fix in this PR: the seed now fills search_name for each donor ('donor ' || i, which is what the app's normalization gives for those ASCII names).
  • The same file also needed a change for this issue: the seed inserted issued receipts directly, which the new rules refuse. Issued receipts must have their donor data frozen, and no lines can be added to a receipt once it has a number. The seed now inserts those receipts as Approved, adds their lines, then issues them with their number and frozen data, the same order the API uses.
  • With both fixes, all 39 backup tests pass. I also checked that the real restore isn't affected by the new triggers: pg_restore loads the data before it creates triggers.

Screenshots / Screencasts

No frontend changes (backend and API only).

Checklist

  • Code is neat, readable, and works
  • Code is commented where appropriate and well-documented
  • Commit messages follow our guidelines
  • Issue number is linked
  • Branch is linked
  • Reviewers are assigned (one of your tech leads)

Notes

Deployment

  • New ReceiptIssuer settings section. It must be set in the App Service configuration (for example ReceiptIssuer__RegistrationNumber) with Moisson Montréal's real values, or every issue is refused with a message naming the missing setting.

New dependencies

  • QuestPDF (API) for the PDF. Free under its Community license for registered charities, to be confirmed by Moisson Montréal.
  • PdfPig (tests only) to extract and check the PDF text.

Behavior change for other features

  • Issuing a receipt now requires a complete donor address, at least one food item, and a total that matches its lines. Whatever creates receipts later (for example from imported donations, or the desktop app) must provide that, or the issue returns a 422 with the list of problems.

Open questions for the client, beyond the list in the Overview

  • A receipt can include food from two calendar years (December and January). Receipts are normally per tax year, so we may want to refuse this.
  • A receipt worth $0 (all grade C) can be issued. Should it be refused?
  • The organization's settings are read when the PDF is made, not frozen at issue, so a change of address would show on reprints of old receipts.
  • Whether a scanned signature should be printed, and whether the line description should be the food item's name or the catalog item's name.

Team follow-up

Closes #83, #84 and #85. Adds the receipt PDF with the CRA mentions, refuses it when data is missing, freezes the donor and lines at issue, adds cancel and replace, and makes issued receipts read-only in the database.
@jspngl
jspngl requested a review from anis022 December 4, 2026 16:06
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