Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/database/receipt-statuses.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ Each action has its own endpoint:
| The receipt does not exist | 404 |
| The employee is not signed in | 401 |

Issuing a receipt also checks that everything a tax receipt needs is there: the settings of the organization, the full name and address of the donor, at least one food item, and a total that matches its lines. If something is missing, the API answers 422 with one message per problem in `problems`, such as "The donor profile is incomplete: the postal code is missing.", and the receipt stays Approved without a number. When the receipt is issued, the name and address of the donor and the description and date of each food item are frozen on it. See [the tax receipt PDF](../receipts/tax-receipt-pdf.md).

To correct an issued receipt, call `POST /api/receipts/{id}/replace` with `{ "reason": "..." }`. It cancels the receipt and creates its replacement in Draft in one transaction. The replacement goes through approval again and gets a new number when it is issued.

## For developers

| Piece | Location | Role |
Expand Down
17 changes: 17 additions & 0 deletions docs/database/receipts.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Matches the migration 20261107041912_CreateReceipts, with the status added by 20261127052039_AddReceiptStatus
// and the receipt number added by 20261127053347_AddReceiptNumbers.
// donors and food_items come from the migration 20261105163138_CreateDonorsAndFoodItems (donor profile columns from 20261128005314_AddDonorProfile, active flag and search name from 20261128222105_AddDonorListSearch),
// The frozen issue details, the advantage and the replacement link come from 20261204154702_AddReceiptIssueDetails.
// catalog_items from 20261110175320_CreateCatalogItemsAndPrices and quality_classes from 20261112060008_CreateQualityClasses.
// They are shown here because receipts link to them.
// roles, users and donation_imports (20261103000127_CreateUsersAndRoles and 20261204063259_AddDonationImports)
Expand Down Expand Up @@ -140,12 +141,21 @@ Table receipts {
created_at timestamptz [not null, default: `now()`, note: 'Stored in UTC']
status varchar(30) [not null, default: 'Draft', note: 'Where the receipt is in the approval process. See receipt-statuses.md']
receipt_number varchar(10) [null, note: 'Unique number such as DEN_000001, given when the receipt is issued and kept if it is cancelled']
advantage_amount numeric(12,2) [not null, default: 0.0, note: 'Value of the advantage the donor received, in dollars. Eligible amount = total_amount - advantage_amount']
issued_at timestamptz [null, note: 'Stored in UTC, empty until the receipt is issued']
donor_name varchar(255) [null, note: 'Donor name frozen when the receipt is issued']
donor_address text [null, note: 'Donor street address frozen when the receipt is issued']
donor_city varchar(100) [null, note: 'Donor city frozen when the receipt is issued']
donor_province varchar(50) [null, note: 'Donor province frozen when the receipt is issued']
donor_postal_code varchar(20) [null, note: 'Donor postal code frozen when the receipt is issued']
replaces_receipt_id uuid [null, note: 'Cancelled receipt this one replaces, empty if none']

indexes {
id [pk, name: 'pk_receipts']
(id, donor_id) [unique, name: 'ak_receipts_id_donor_id']
donor_id [name: 'ix_receipts_donor_id']
receipt_number [unique, name: 'ix_receipts_receipt_number']
replaces_receipt_id [unique, name: 'ix_receipts_replaces_receipt_id']
}

checks {
Expand All @@ -154,6 +164,9 @@ Table receipts {
`receipt_number ~ '^DEN_[0-9]{6}$'` [name: 'ck_receipts_receipt_number_format']
`status <> 'Issued' OR receipt_number IS NOT NULL` [name: 'ck_receipts_issued_has_receipt_number']
`status IN ('Issued', 'Cancelled') OR receipt_number IS NULL` [name: 'ck_receipts_receipt_number_only_once_issued']
`advantage_amount >= 0 AND (advantage_amount = 0 OR advantage_amount <= total_amount)` [name: 'ck_receipts_advantage_amount_range']
`receipt_number IS NULL OR (issued_at IS NOT NULL AND donor_name IS NOT NULL)` [name: 'ck_receipts_issued_details_frozen']
`replaces_receipt_id <> id` [name: 'ck_receipts_replaces_another_receipt']
}

Note: 'Tax receipts given to donors'
Expand All @@ -168,6 +181,8 @@ Table receipt_items {
unit_price numeric(12,4) [not null, note: 'Price of one unit when the receipt was made']
quality_percent numeric(5,2) [not null, note: 'A = 100, B = 50 to 75, C = 0']
receipt_cancelled boolean [not null, default: false, note: 'True once the receipt is cancelled. The line is kept, but the food item is free again']
description varchar(255) [null, note: 'Food item name frozen when the receipt is issued']
donation_date date [null, note: 'Donation date frozen when the receipt is issued']

indexes {
(receipt_id, food_item_id) [pk, name: 'pk_receipt_items']
Expand Down Expand Up @@ -272,6 +287,8 @@ Ref fk_catalog_items_quality_classes_quality_class: catalog_items.quality_class

Ref fk_receipts_donors_donor_id: receipts.donor_id > donors.id [delete: restrict]

Ref fk_receipts_receipts_replaces_receipt_id: receipts.replaces_receipt_id > receipts.id [delete: restrict]

Ref fk_receipt_items_receipts_receipt_id_donor_id: receipt_items.(receipt_id, donor_id) > receipts.(id, donor_id) [delete: cascade]

Ref fk_receipt_items_food_items_food_item_id_donor_id: receipt_items.(food_item_id, donor_id) > food_items.(id, donor_id) [delete: restrict]
Expand Down
18 changes: 18 additions & 0 deletions docs/database/receipts.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ One row per tax receipt.
| status | Where the receipt is in the approval process: Draft, PendingSupplyApproval, PendingFinanceApproval, Approved, Issued or Cancelled. A new receipt starts as Draft. |
| receipt_number | Unique number of the receipt, such as DEN_000001. Empty until the receipt is issued, then kept even if the receipt is cancelled. |

Columns added for the PDF of the receipt (migration [20261204154702_AddReceiptIssueDetails](../../src/MoissonMontreal.Api/Data/Migrations/20261204154702_AddReceiptIssueDetails.cs)):

- advantage_amount: value of the advantage the donor received in return for the gift, in dollars. 0 for a plain food donation. The eligible amount printed on the receipt is total_amount minus advantage_amount.
- issued_at: date and time the receipt was issued, stored in UTC. Empty until then. Its date in Montreal time is the date of issue printed on the receipt.
- donor_name, donor_address, donor_city, donor_province, donor_postal_code: name and address of the donor, copied from the donor profile when the receipt is issued. The PDF of an issued receipt uses these columns, never the donor profile, so a later change to the donor does not change a receipt already given.
- replaces_receipt_id: the cancelled receipt this one replaces, or empty. A receipt can be replaced only once.

### receipt_items

One row per food item on a receipt.
Expand All @@ -41,6 +48,11 @@ One row per food item on a receipt.
| quality_percent | Share of the price kept for the item's quality grade: 100 for grade A, between 50 and 75 for grade B, 0 for grade C |
| receipt_cancelled | Yes once the receipt is cancelled. The line is kept as a record of the cancelled receipt, but the food item can then go on a new receipt. |

Columns added for the PDF of the receipt:

- description: name of the food item, copied when the receipt is issued and printed as the description of the line.
- donation_date: day the food was received, copied when the receipt is issued and printed on the line.

### receipt_status_changes

One row every time the status of a receipt changes. Rows are only added, so the history of a receipt is never rewritten.
Expand Down Expand Up @@ -141,6 +153,10 @@ created_at is a precise moment, stored and read back in UTC. Convert it to Ameri
15. Two receipts cannot have the same number.
16. An issued receipt must have a number, and a receipt that was never issued (Draft, waiting for approval or Approved) cannot have one. A receipt cancelled after it was issued keeps its number.
17. A receipt number is DEN_ followed by 6 digits.
18. The advantage cannot be negative, and cannot be larger than the total.
19. A receipt with a number must have its date of issue and the donor name frozen on it.
20. A receipt cannot replace itself, and two receipts cannot replace the same receipt.
21. Once a receipt has a number, the database refuses any change to it: only its status can go from Issued to Cancelled, and receipt_cancelled can only be turned on for its lines. It cannot be deleted, and no line can be added to it, removed from it, or moved to or from it. Two triggers enforce this: tr_receipts_issued_read_only and tr_receipt_items_issued_read_only. To correct an issued receipt, cancel it and issue a replacement (see [the tax receipt PDF](../receipts/tax-receipt-pdf.md)).

Because donor_id is part of both links, the app cannot change the donor of a receipt or of a food item once it is saved.

Expand All @@ -158,6 +174,8 @@ A cancelled receipt keeps its lines, so the original stays readable. Cancelling
| Receipt number migration | [20261127053347_AddReceiptNumbers](../../src/MoissonMontreal.Api/Data/Migrations/20261127053347_AddReceiptNumbers.cs), with [0010_add_receipt_numbers.sql](sql/0010_add_receipt_numbers.sql) and [ReceiptNumberCounterConfiguration.cs](../../src/MoissonMontreal.Api/Data/Configurations/ReceiptNumberCounterConfiguration.cs) |
| Status and history migration | [20261127052039_AddReceiptStatus](../../src/MoissonMontreal.Api/Data/Migrations/20261127052039_AddReceiptStatus.cs), with [0009_add_receipt_status.sql](sql/0009_add_receipt_status.sql) and [ReceiptStatusChangeConfiguration.cs](../../src/MoissonMontreal.Api/Data/Configurations/ReceiptStatusChangeConfiguration.cs) |

The PDF details migration is [20261204154702_AddReceiptIssueDetails](../../src/MoissonMontreal.Api/Data/Migrations/20261204154702_AddReceiptIssueDetails.cs), with [0015_add_receipt_issue_details.sql](sql/0015_add_receipt_issue_details.sql). It freezes the receipts already issued with the donor and the food items as they are when it runs, and the date of their first issue (or their creation date when their history has none).

The SQL script is generated from the migration with `dotnet ef migrations script CreateDonorsAndFoodItems CreateReceipts --project src/MoissonMontreal.Api --output docs/database/sql/0003_create_receipts.sql`. It must be run after [0001_create_users_and_roles.sql](sql/0001_create_users_and_roles.sql) and [0002_create_donors_and_food_items.sql](sql/0002_create_donors_and_food_items.sql). It records itself in the migration history, so Entity Framework will not apply it a second time.

The generated file starts with a UTF-8 byte order mark (BOM). Remove it before committing, for example with `sed -i '1s/^\xEF\xBB\xBF//' docs/database/sql/0003_create_receipts.sql`.
16 changes: 16 additions & 0 deletions docs/database/schema.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,21 @@ Table receipts {
created_at timestamptz [not null, default: `now()`, note: 'Stored in UTC']
status varchar(30) [not null, default: 'Draft', note: 'Where the receipt is in the approval process. See receipt-statuses.md']
receipt_number varchar(10) [null, note: 'Unique number such as DEN_000001, given when the receipt is issued and kept if it is cancelled']
advantage_amount numeric(12,2) [not null, default: 0.0, note: 'Value of the advantage the donor received, in dollars. Eligible amount = total_amount - advantage_amount']
issued_at timestamptz [null, note: 'Stored in UTC, empty until the receipt is issued']
donor_name varchar(255) [null, note: 'Donor name frozen when the receipt is issued']
donor_address text [null, note: 'Donor street address frozen when the receipt is issued']
donor_city varchar(100) [null, note: 'Donor city frozen when the receipt is issued']
donor_province varchar(50) [null, note: 'Donor province frozen when the receipt is issued']
donor_postal_code varchar(20) [null, note: 'Donor postal code frozen when the receipt is issued']
replaces_receipt_id uuid [null, note: 'Cancelled receipt this one replaces, empty if none']

indexes {
id [pk, name: 'pk_receipts']
(id, donor_id) [unique, name: 'ak_receipts_id_donor_id', note: 'Lets receipt_items check the donor']
donor_id [name: 'ix_receipts_donor_id']
receipt_number [unique, name: 'ix_receipts_receipt_number']
replaces_receipt_id [unique, name: 'ix_receipts_replaces_receipt_id']
}

checks {
Expand All @@ -255,6 +264,9 @@ Table receipts {
`receipt_number ~ '^DEN_[0-9]{6}$'` [name: 'ck_receipts_receipt_number_format']
`status <> 'Issued' OR receipt_number IS NOT NULL` [name: 'ck_receipts_issued_has_receipt_number']
`status IN ('Issued', 'Cancelled') OR receipt_number IS NULL` [name: 'ck_receipts_receipt_number_only_once_issued']
`advantage_amount >= 0 AND (advantage_amount = 0 OR advantage_amount <= total_amount)` [name: 'ck_receipts_advantage_amount_range']
`receipt_number IS NULL OR (issued_at IS NOT NULL AND donor_name IS NOT NULL)` [name: 'ck_receipts_issued_details_frozen']
`replaces_receipt_id <> id` [name: 'ck_receipts_replaces_another_receipt']
}

Note: 'Tax receipts given to donors'
Expand Down Expand Up @@ -310,6 +322,8 @@ Table receipt_items {
unit_price numeric(12,4) [not null, note: 'Price of one unit when the receipt was made']
quality_percent numeric(5,2) [not null, note: 'A = 100, B = 50 to 75, C = 0']
receipt_cancelled boolean [not null, default: false, note: 'True once the receipt is cancelled. The line is kept, but the food item is free again']
description varchar(255) [null, note: 'Food item name frozen when the receipt is issued']
donation_date date [null, note: 'Donation date frozen when the receipt is issued']

indexes {
(receipt_id, food_item_id) [pk, name: 'pk_receipt_items']
Expand Down Expand Up @@ -441,6 +455,8 @@ Ref fk_food_items_quality_classes_quality: food_items.quality > quality_classes.

Ref fk_receipts_donors_donor_id: receipts.donor_id > donors.id [delete: restrict]

Ref fk_receipts_receipts_replaces_receipt_id: receipts.replaces_receipt_id > receipts.id [delete: restrict]

Ref fk_receipt_items_receipts_receipt_id_donor_id: receipt_items.(receipt_id, donor_id) > receipts.(id, donor_id) [delete: cascade]

Ref fk_receipt_items_food_items_food_item_id_donor_id: receipt_items.(food_item_id, donor_id) > food_items.(id, donor_id) [delete: restrict]
Expand Down
107 changes: 107 additions & 0 deletions docs/database/sql/0015_add_receipt_issue_details.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
START TRANSACTION;
ALTER TABLE receipts ADD advantage_amount numeric(12,2) NOT NULL DEFAULT 0.0;

ALTER TABLE receipts ADD donor_address text;

ALTER TABLE receipts ADD donor_city character varying(100);

ALTER TABLE receipts ADD donor_name character varying(255);

ALTER TABLE receipts ADD donor_postal_code character varying(20);

ALTER TABLE receipts ADD donor_province character varying(50);

ALTER TABLE receipts ADD issued_at timestamp with time zone;

ALTER TABLE receipts ADD replaces_receipt_id uuid;

ALTER TABLE receipt_items ADD description character varying(255);

ALTER TABLE receipt_items ADD donation_date date;

UPDATE receipts AS r
SET issued_at = coalesce(
(SELECT min(c.changed_at) FROM receipt_status_changes AS c WHERE c.receipt_id = r.id AND c.to_status = 'Issued'),
r.created_at),
donor_name = d.name,
donor_address = d.address,
donor_city = d.city,
donor_province = d.province,
donor_postal_code = d.postal_code
FROM donors AS d
WHERE d.id = r.donor_id AND r.receipt_number IS NOT NULL;

UPDATE receipt_items AS i
SET description = f.name,
donation_date = f.donation_date
FROM food_items AS f, receipts AS r
WHERE f.id = i.food_item_id AND r.id = i.receipt_id AND r.receipt_number IS NOT NULL;

CREATE UNIQUE INDEX ix_receipts_replaces_receipt_id ON receipts (replaces_receipt_id);

ALTER TABLE receipts ADD CONSTRAINT ck_receipts_advantage_amount_range CHECK (advantage_amount >= 0 AND (advantage_amount = 0 OR advantage_amount <= total_amount));

ALTER TABLE receipts ADD CONSTRAINT ck_receipts_issued_details_frozen CHECK (receipt_number IS NULL OR (issued_at IS NOT NULL AND donor_name IS NOT NULL));

ALTER TABLE receipts ADD CONSTRAINT ck_receipts_replaces_another_receipt CHECK (replaces_receipt_id <> id);

ALTER TABLE receipts ADD CONSTRAINT fk_receipts_receipts_replaces_receipt_id FOREIGN KEY (replaces_receipt_id) REFERENCES receipts (id) ON DELETE RESTRICT;

CREATE FUNCTION refuse_change_to_issued_receipt() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF OLD.receipt_number IS NULL THEN
RETURN CASE WHEN TG_OP = 'DELETE' THEN OLD ELSE NEW END;
END IF;

IF TG_OP = 'UPDATE'
AND (to_jsonb(NEW) - 'status') = (to_jsonb(OLD) - 'status')
AND (NEW.status = OLD.status OR (OLD.status = 'Issued' AND NEW.status = 'Cancelled')) THEN
RETURN NEW;
END IF;

RAISE EXCEPTION 'Receipt % is issued and cannot be changed. Cancel it and issue a replacement instead.', OLD.receipt_number
USING ERRCODE = 'integrity_constraint_violation', CONSTRAINT = 'tr_receipts_issued_read_only';
END;
$$;

CREATE TRIGGER tr_receipts_issued_read_only
BEFORE UPDATE OR DELETE ON receipts
FOR EACH ROW EXECUTE FUNCTION refuse_change_to_issued_receipt();

CREATE FUNCTION refuse_change_to_issued_receipt_item() RETURNS trigger LANGUAGE plpgsql AS $$
DECLARE
old_number text;
new_number text;
BEGIN
IF TG_OP <> 'INSERT' THEN
SELECT receipt_number INTO old_number FROM receipts WHERE id = OLD.receipt_id;
END IF;

IF TG_OP <> 'DELETE' THEN
SELECT receipt_number INTO new_number FROM receipts WHERE id = NEW.receipt_id;
END IF;

IF old_number IS NULL AND new_number IS NULL THEN
RETURN CASE WHEN TG_OP = 'DELETE' THEN OLD ELSE NEW END;
END IF;

IF TG_OP = 'UPDATE'
AND (to_jsonb(NEW) - 'receipt_cancelled') = (to_jsonb(OLD) - 'receipt_cancelled')
AND (NEW.receipt_cancelled OR NOT OLD.receipt_cancelled) THEN
RETURN NEW;
END IF;

RAISE EXCEPTION 'Receipt % is issued and its food items cannot be changed. Cancel it and issue a replacement instead.', coalesce(old_number, new_number)
USING ERRCODE = 'integrity_constraint_violation', CONSTRAINT = 'tr_receipt_items_issued_read_only';
END;
$$;

CREATE TRIGGER tr_receipt_items_issued_read_only
BEFORE INSERT OR UPDATE OR DELETE ON receipt_items
FOR EACH ROW EXECUTE FUNCTION refuse_change_to_issued_receipt_item();

INSERT INTO "__EFMigrationsHistory" (migration_id, product_version)
VALUES ('20261204154702_AddReceiptIssueDetails', '10.0.13');

COMMIT;

Loading