diff --git a/contract/__manifest__.py b/contract/__manifest__.py index 723fafb89..78021afff 100644 --- a/contract/__manifest__.py +++ b/contract/__manifest__.py @@ -5,13 +5,13 @@ # Copyright 2017 Tecnativa - Vicent Cubells # Copyright 2016-2017 LasLabs Inc. # Copyright 2018-2019 ACSONE SA/NV -# Copyright 2020 Tecnativa - Pedro M. Baeza +# Copyright 2020-2021 Tecnativa - Pedro M. Baeza # Copyright 2020 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). { "name": "Recurring - Contracts Management", - "version": "13.0.2.2.1", + "version": "13.0.2.3.1", "category": "Contract Management", "license": "AGPL-3", "author": "Tecnativa, ACSONE SA/NV, Odoo Community Association (OCA)", diff --git a/contract/models/contract.py b/contract/models/contract.py index 6b7f2a553..8959296c0 100644 --- a/contract/models/contract.py +++ b/contract/models/contract.py @@ -113,6 +113,12 @@ class ContractContract(models.Model): string="Modifications", ) + def get_formview_id(self, access_uid=None): + if self.contract_type == "sale": + return self.env.ref("contract.contract_contract_customer_form_view").id + else: + return self.env.ref("contract.contract_contract_supplier_form_view").id + @api.model_create_multi def create(self, vals_list): records = super().create(vals_list) @@ -131,12 +137,16 @@ class ContractContract(models.Model): @api.model def _set_start_contract_modification(self): + subtype_id = self.env.ref("contract.mail_message_subtype_contract_modification") for record in self: if record.contract_line_ids: date_start = min(record.contract_line_ids.mapped("date_start")) else: date_start = record.create_date - record.write( + record.message_subscribe( + partner_ids=[record.partner_id.id], subtype_ids=[subtype_id.id] + ) + record.with_context(skip_modification_mail=True).write( { "modification_ids": [ (0, 0, {"date": date_start, "description": _("Contract start")}) @@ -151,16 +161,12 @@ class ContractContract(models.Model): lambda x: not x.sent ) if modification_ids_not_sent: - contract_modification_subtype = self.env.ref( - "contract.mail_message_subtype_contract_modification" - ) - notified_partners = record.message_follower_ids.filtered( - lambda x: contract_modification_subtype in x.subtype_ids - ).mapped("partner_id") - if notified_partners: + if not self.env.context.get("skip_modification_mail"): record.message_post_with_template( self.env.ref("contract.mail_template_contract_modification").id, - partner_ids=[(4, x.id) for x in notified_partners], + subtype_id=self.env.ref( + "contract.mail_message_subtype_contract_modification" + ).id, email_layout_xmlid="contract.template_contract_modification", ) modification_ids_not_sent.write({"sent": True}) @@ -541,7 +547,9 @@ class ContractContract(models.Model): lambda x: invoice_create_subtype in x.subtype_ids ).mapped("partner_id") if partner_ids: - invoices.message_subscribe(partner_ids=partner_ids.ids) + (invoices & item._get_related_invoices()).message_subscribe( + partner_ids=partner_ids.ids + ) def _recurring_create_invoice(self, date_ref=False): invoices_values = self._prepare_recurring_invoices_values(date_ref) diff --git a/contract/tests/test_contract.py b/contract/tests/test_contract.py index 2e21f3c4f..ccf445169 100644 --- a/contract/tests/test_contract.py +++ b/contract/tests/test_contract.py @@ -172,11 +172,10 @@ class TestContract(TestContractBase): return self.env["contract.template.line"].create(vals) def test_add_modifications(self): + partner2 = self.partner.copy() + subtype = self.env.ref("contract.mail_message_subtype_contract_modification") self.contract.message_subscribe( - partner_ids=self.contract.partner_id.ids, - subtype_ids=self.env.ref( - "contract.mail_message_subtype_contract_modification" - ).ids, + partner_ids=partner2.ids, subtype_ids=subtype.ids, ) # Check initial modification auto-creation self.assertEqual(len(self.contract.modification_ids), 1) @@ -188,11 +187,18 @@ class TestContract(TestContractBase): ] } ) - self.assertGreaterEqual(len(self.contract.message_partner_ids), 1) - mail_messages = self.env["mail.message"].search( - [("model", "=", "contract.contract"), ("res_id", "=", self.contract.id)] + partner_ids = self.contract.message_follower_ids.filtered( + lambda x: subtype in x.subtype_ids + ).mapped("partner_id") + self.assertGreaterEqual(len(partner_ids), 2) + total_mail_messages = self.env["mail.message"].search( + [ + ("model", "=", "contract.contract"), + ("res_id", "=", self.contract.id), + ("subtype_id", "=", subtype.id), + ] ) - self.assertGreaterEqual(len(mail_messages), 2) + self.assertGreaterEqual(len(total_mail_messages), 1) def test_check_discount(self): with self.assertRaises(ValidationError): @@ -257,7 +263,7 @@ class TestContract(TestContractBase): self.contract._recurring_create_invoice() invoice_daily = self.contract._get_related_invoices() self.assertTrue(invoice_daily) - self.assertEqual(len(invoice_daily.message_partner_ids.ids), 1) + self.assertGreaterEqual(len(invoice_daily.message_follower_ids), 1) def test_contract_weekly_post_paid(self): recurring_next_date = to_date("2018-03-01") diff --git a/contract/views/contract_portal_templates.xml b/contract/views/contract_portal_templates.xml index f0bd86cee..583b8f145 100644 --- a/contract/views/contract_portal_templates.xml +++ b/contract/views/contract_portal_templates.xml @@ -82,7 +82,7 @@