Skip to content

[FIX] thankyou_letters: forget deleted donation lines in pending thank you - #2224

Merged
Danielgergely merged 3 commits into
18.0from
fix-thankyou-deleted-lines
Nov 23, 2026
Merged

Danielgergely merged 3 commits into
18.0from
fix-thankyou-deleted-lines

Conversation

@davidwul

@davidwul davidwul commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What and why

A thank-you communication lists its donation lines in object_ids. When one of these lines is deleted while the thank you also holds other lines (typically: a bank statement line reconciled again, its entry lines deleted and recreated), account.move.line.unlink() refreshed the thank you but kept the deleted id. Rendering it again then failed:

MissingError: Record does not exist or has been deleted. (Record: account.move.line(9506226,))

Seen in production on a thank you grouping two donations booked from two bank statement lines.

Changes in thankyou_letters:

  • unlink() removes the deleted lines from object_ids before the refresh (the thank you is still deleted when no line is left, as before).
  • It now finds every communication not sent yet that holds the lines (_get_unsent_communications), whoever it is sent to, not only the one move_id.communication_id points to: thank yous merged at creation (partner.communication.job.create) or kept by a merge that cancelled others, the thank you wizard, ambassador receipts (crm_compassion, sent to the ambassador). It reads the communications not sent yet of the account.move.line configs (about 550 on the Swiss database) and keeps those holding the deleted lines.
  • generate_thank_you() drops deleted lines of the pending thank you it merges into, so a thank you already holding a dead id is healed by the next donation instead of copying the id forward. A pending thank you of another config whose lines were all deleted is cancelled instead of getting an empty object_ids.

Sent communications (processing, done) are not touched: their text is kept as sent.

Same scenario, queue side: the thank you of donations booked on a journal entry runs in a queue job on the lines. When the bank statement line is reconciled again before the job runs, the lines are deleted and recreated and the job failed with MissingError (seen in production), although the new lines have their own job. The job now calls generate_thank_you_job(), which skips the deleted lines and thanks those that remain.

Deployment notes

No upgrade needed (Python only). Thank-you jobs queued before the deployment still call generate_thank_you and fail the same way when their lines are gone: they are obsolete (the new lines have their own job) and can be set to done. Communications already holding deleted ids are repaired by a one-off script (thankyou_deleted_lines.py, private ops scripts, dry run unless APPLY=1): it can run before this PR is deployed, and once more after to catch the cases created in between.

Test evidence

Run on an empty database (-i thankyou_letters, then --test-tags /thankyou_letters:TestThankYouDeletedLine): 6 tests, 0 failed.

test_queued_thank_you_skips_lines_deleted_since, run with the former job entry point (generate_thank_you) instead of generate_thank_you_job: ERROR, MissingError, as in production.

On the first commit of this PR (ba612d6), the two tests added after the Greptile review fail:

  • test_deleted_line_of_thank_you_without_entry_pointer (receipt sent to an ambassador who is not the donor): FAIL, '59,60' != '60'
  • test_new_donation_cancels_thank_you_of_deleted_lines_only: FAIL, 'pending' != 'cancel'

On origin/18.0 (model file of the base, first four tests):

  • test_deleted_line_is_removed_from_thank_you (two entries sharing one thank you, one line deleted): FAIL, '27,25' != '25'
  • test_deleted_line_of_thank_you_without_entry_pointer: FAIL, '29,30' != '30'
  • test_new_donation_drops_deleted_line_of_pending_thank_you: ERROR, MissingError
  • test_thank_you_of_deleted_lines_is_removed: passes on both (characterization of the existing behaviour).

The one-off script was run in dry run on a copy of production data (922 communications with deleted ids, nearly all old sent letters) and on two communications set back to pending in the transaction: one refreshed without error with the remaining lines, one cancelled (no line left); its own check reported 0 errors.

Sibling paths

Invariant: a thank-you communication not sent yet never lists a deleted account.move.line.

  • Deleting lines: account.move.line.unlink is the choke point; account.move.write with Command.delete, account.move.unlink and account_reconcile_oca (_reconcile_bank_line_edit) all go through it. Covered by the tests above (driven through account.move.write); the reconciliation flow itself is not driven (other repository).
  • Queue: both places that queue a thank you on lines (account.move.generate_thank_you, cancel_thankyou_letter) now queue generate_thank_you_job; test test_queued_thank_you_skips_....
  • Writers of object_ids without entry pointer (create merge, wizard, ambassador receipts, cancelled-by-merge): covered by the search in _get_unsent_communications, which does not filter on the recipient; test ..._without_entry_pointer uses a receipt sent to another partner than the donor.
  • Readers: refresh_text / rendering get only live ids on these paths; generate_thank_you reads with .exists() (test test_new_donation_drops_...) and cancels a thank you left without line (test test_new_donation_cancels_...).
  • Sent communications: out of scope on purpose (text kept); the data script cleans their ids.

Rules review

Run with the code-rules reviewer on the first version of this change:

  • Should fix: communications found only through move_id.communication_id, missing the create merge, wizard and ambassador paths: fixed, search by lines (first by partner, widened after the Greptile review below).
  • Should fix: generate_thank_you copied dead ids forward: fixed with .exists(), new test.
  • Suggestion: record the merge gap in ROADMAP: no longer needed, the gap is closed.
  • Gap, existing, not changed: when all lines are deleted, the thank you is unlinked; internal users without ERP manager rights have no unlink access on partner.communication.job, so this branch may raise an AccessError for them. Unchanged by this PR, not covered by a test as a non-admin user.

Greptile review

  • P1 Ambassador receipts keep deleted lines: right, the search was filtered on the donor and its commercial partner. Fixed in cbe196d: no filter on the recipient; test with a receipt sent to another partner.
  • P1 Empty letters block new ones: right, an older pending thank you of another config holding only deleted lines got an empty object_ids. Fixed in cbe196d: it is cancelled; test added.

🤖 Generated with Claude Code

…k you

When a donation line was deleted (e.g. the bank statement line it was
booked on is reconciled again) while its pending thank you held other
lines, the thank you was refreshed but kept the deleted id in object_ids:
rendering it again failed with "MissingError: Record does not exist
(account.move.line(...))".

- unlink() removes the deleted lines from object_ids before the refresh,
  in every communication not sent yet that holds them: the one the entry
  points to, and the ones holding them without that pointer (merged at
  creation, cancelled by a merge, thank you wizard, ambassador receipts).
- generate_thank_you() no longer carries forward a deleted line of a
  pending thank you it merges into.

Assisted-by: claude-opus-5-5, medium, claude-code
@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the earlier non-blocking concern about scanning unrelated letters remains.

Findings

  1. P2 Deletions scan unrelated letters ▶

Reviews (3) · Last reviewed commit: "[FIX] thankyou_letters: queued thank you..." · Reviewed by Greptile

…hank you

Review of #2224:
- the communications holding deleted lines are searched among all those
  not sent yet, whoever they are sent to: ambassador receipts
  (crm_compassion) are sent to the ambassador, not to the donor;
- generate_thank_you() cancels a pending thank you of another config whose
  lines were all deleted, instead of writing it an empty object_ids.

Assisted-by: claude-opus-5-5, medium, claude-code
Comment on lines +68 to +73
jobs |= self.env["partner.communication.job"].search(
[
("config_id.model_id.model", "=", self._name),
("state", "in", ("pending", "call", "failure")),
]
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Deletions scan unrelated letters

Every account.move.line.unlink() now loads all pending, call, and failed communications for account lines, then parses every job's object_ids in Python—even when deleting an unrelated accounting line. This adds work that grows with the entire backlog rather than the affected donations.

Narrow the search by the deleted line IDs before loading and parsing jobs, while still allowing recipients other than the donor.

The thank you of donations booked on a journal entry runs in a queue job
on the lines. When the bank statement line is reconciled again before
the job runs, these lines are deleted and recreated: the job failed with
MissingError, although the new lines have their own job.

The job now calls generate_thank_you_job(), which skips the deleted lines.

Assisted-by: claude-opus-5-5, medium, claude-code
@Danielgergely
Danielgergely merged commit 8daca4d into 18.0 Nov 23, 2026
2 checks passed
@Danielgergely
Danielgergely deleted the fix-thankyou-deleted-lines branch November 23, 2026 17:00
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.

2 participants