From 170b8edfcbafd2bae7b458363255ca719f79aa7a Mon Sep 17 00:00:00 2001 From: Emanuel Cino Date: Tue, 1 Oct 2024 14:27:16 +0200 Subject: [PATCH] T1758 FIX sending multiple exit communications to sponsors - Only send it when the contract is freshly terminated because of the lifecycle message --- .../models/lifecycle_events.py | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/partner_communication_compassion/models/lifecycle_events.py b/partner_communication_compassion/models/lifecycle_events.py index 41371676b..ba8edcc8f 100644 --- a/partner_communication_compassion/models/lifecycle_events.py +++ b/partner_communication_compassion/models/lifecycle_events.py @@ -7,6 +7,8 @@ # The licence is in the file __manifest__.py # ############################################################################## +from datetime import date + from odoo import api, models @@ -25,7 +27,8 @@ def process_commkit(self, commkit_data): "partner_communication_compassion.planned_exit_notification" ) sponsorship = lifecycle.child_id.sponsorship_ids[:1] - sponsorship.send_communication(communication_type, both=True) + if sponsorship.end_date.date() == date.today(): + sponsorship.send_communication(communication_type, both=True) else: communication_type = self.env["partner.communication.config"].search( [ @@ -49,14 +52,19 @@ def process_commkit(self, commkit_data): object_ids = lifecycle.child_id.sponsorship_ids[:1].id # Generate the communication - sponsor = lifecycle.child_id.sponsorship_ids[:1].correspondent_id - self.env["partner.communication.job"].create( - { - "config_id": communication_type.id, - "partner_id": sponsor.id, - "object_ids": object_ids, - } - ) + sponsorship = lifecycle.child_id.sponsorship_ids[:1] + if ( + sponsorship.state == "active" + or sponsorship.end_date.date() == date.today() + ): + sponsor = sponsorship.correspondent_id + self.env["partner.communication.job"].create( + { + "config_id": communication_type.id, + "partner_id": sponsor.id, + "object_ids": object_ids, + } + ) return ids