From fbabc1e83a9451b0409a30a556a56782527615b6 Mon Sep 17 00:00:00 2001 From: Daniel Gergely Date: Mon, 7 Sep 2026 11:55:25 +0200 Subject: [PATCH] [T3445] FIX: prefer active/terminated sponsorships over cancelled/draft --- sbc_compassion/models/correspondence.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sbc_compassion/models/correspondence.py b/sbc_compassion/models/correspondence.py index e06d59533..a6c9e3f0f 100644 --- a/sbc_compassion/models/correspondence.py +++ b/sbc_compassion/models/correspondence.py @@ -1066,13 +1066,17 @@ def json_to_data(self, json, mapping_name=None): if "child_id" in odoo_data and "partner_id" in odoo_data: partner = odoo_data.get("partner_id") child = odoo_data.pop("child_id") - sponsorship = self.env["recurring.contract"].search( + sponsorships = self.env["recurring.contract"].search( [ ("correspondent_id", "=", partner), ("child_id", "=", child), - ], - limit=1, + ] ) + # Prefer the live sponsorship: bound to a cancelled/draft duplicate, + # send_communication drops the letter and it never gets published. + sponsorship = sponsorships.sorted( + lambda s: s.state in ("cancelled", "draft") + )[:1] if sponsorship: odoo_data["sponsorship_id"] = sponsorship.id