From d766d63b111c157ffbaf072ed41949a03537f526 Mon Sep 17 00:00:00 2001 From: Djamel Touati Date: Mon, 18 Dec 2023 13:17:59 +0000 Subject: [PATCH] [FIX] purchase: Compute receipt_reminder_email based on the PO company Steps to reproduce the issue: - Have two companies in your database. - Create a partner: - Set "receipt_reminder_email" to True for Company 1. - Set "receipt_reminder_email" to False for Company 2. - Create a Purchase Order with Company 2. Problem: The field "receipt_reminder_email" is a related field, so its value is False. However, if you switch the current company to "Company 1" while keeping the Purchase Order in Company 2, the "receipt_reminder_email" is updated to True. Solution: The field should be computed instead. the readonly for this field in not neccessary anymore so the inverse is not mandoatory. opw-3593577 closes odoo/odoo#147648 X-original-commit: 2211e08f69eed7af9be20f178f659f6a71fb6ac4 Signed-off-by: William Henrotin (whe) Signed-off-by: Djamel Touati (otd) --- addons/purchase/models/purchase_order.py | 15 ++++++++---- addons/purchase/tests/test_purchase.py | 29 +++++++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/addons/purchase/models/purchase_order.py b/addons/purchase/models/purchase_order.py index d068b22724c..218157e23da 100644 --- a/addons/purchase/models/purchase_order.py +++ b/addons/purchase/models/purchase_order.py @@ -146,8 +146,8 @@ class PurchaseOrder(models.Model): mail_reminder_confirmed = fields.Boolean("Reminder Confirmed", default=False, readonly=True, copy=False, help="True if the reminder email is confirmed by the vendor.") mail_reception_confirmed = fields.Boolean("Reception Confirmed", default=False, readonly=True, copy=False, help="True if PO reception is confirmed by the vendor.") - receipt_reminder_email = fields.Boolean('Receipt Reminder Email', related='partner_id.receipt_reminder_email', readonly=False) - reminder_date_before_receipt = fields.Integer('Days Before Receipt', related='partner_id.reminder_date_before_receipt', readonly=False) + receipt_reminder_email = fields.Boolean('Receipt Reminder Email', compute='_compute_receipt_reminder_email') + reminder_date_before_receipt = fields.Integer('Days Before Receipt', compute='_compute_receipt_reminder_email') @api.constrains('company_id', 'order_line') def _check_order_line_company_id(self): @@ -198,6 +198,12 @@ class PurchaseOrder(models.Model): name += ': ' + formatLang(self.env, po.amount_total, currency_obj=po.currency_id) po.display_name = name + @api.depends('company_id', 'partner_id') + def _compute_receipt_reminder_email(self): + for order in self: + order.receipt_reminder_email = order.partner_id.with_company(order.company_id).receipt_reminder_email + order.reminder_date_before_receipt = order.partner_id.with_company(order.company_id).reminder_date_before_receipt + @api.depends_context('lang') @api.depends('order_line.taxes_id', 'order_line.price_subtotal', 'amount_total', 'amount_untaxed') def _compute_tax_totals(self): @@ -813,10 +819,11 @@ class PurchaseOrder(models.Model): """When auto sending a reminder mail, only send for unconfirmed purchase order and not all products are service.""" return self.search([ - ('receipt_reminder_email', '=', True), + ('partner_id', '!=', False), ('state', 'in', ['purchase', 'done']), ('mail_reminder_confirmed', '=', False) - ]).filtered(lambda p: p.mapped('order_line.product_id.product_tmpl_id.type') != ['service']) + ]).filtered(lambda p: p.partner_id.with_company(p.company_id).receipt_reminder_email and\ + p.mapped('order_line.product_id.product_tmpl_id.type') != ['service']) def _default_order_line_values(self): default_data = super()._default_order_line_values() diff --git a/addons/purchase/tests/test_purchase.py b/addons/purchase/tests/test_purchase.py index b91eb3ef15f..563f7967866 100644 --- a/addons/purchase/tests/test_purchase.py +++ b/addons/purchase/tests/test_purchase.py @@ -71,9 +71,13 @@ class TestPurchase(AccountTestInvoicingCommon): self.assertTrue(purchase_order.name.startswith('PO/2020/')) def test_reminder_1(self): - """Set to send reminder today, check if a reminder can be send to the + """Set to send reminder tomorrow, check if a reminder can be send to the partner. """ + # set partner to send reminder in Company 2 + self.partner_a.with_company(self.env.companies[1]).receipt_reminder_email = True + self.partner_a.with_company(self.env.companies[1]).reminder_date_before_receipt = 1 + # Create the PO in Company 1 self.env.user.tz = 'Europe/Brussels' po = Form(self.env['purchase.order']) po.partner_id = self.partner_a @@ -86,12 +90,25 @@ class TestPurchase(AccountTestInvoicingCommon): po_line.product_qty = 10 po_line.price_unit = 200 # set to send reminder today - date_planned = fields.Datetime.now().replace(hour=23, minute=0) + timedelta(days=1) + date_planned = fields.Datetime.now().replace(hour=23, minute=0) + timedelta(days=2) po.date_planned = date_planned - po.receipt_reminder_email = True - po.reminder_date_before_receipt = 1 po = po.save() po.button_confirm() + # Check that reminder is not set in Company 1 and the mail will not be sent + self.assertEqual(po.company_id, self.env.companies[0]) + self.assertFalse(po.receipt_reminder_email) + self.assertEqual(po.reminder_date_before_receipt, 1, "The default value should be taken from the company") + old_messages = po.message_ids + po._send_reminder_mail() + messages_send = po.message_ids - old_messages + self.assertFalse(messages_send) + # Set to send reminder in Company 1 + self.partner_a.receipt_reminder_email = True + self.partner_a.reminder_date_before_receipt = 2 + # Invalidate the cache to ensure that the computed fields are recomputed + self.env.invalidate_all() + self.assertTrue(po.receipt_reminder_email) + self.assertEqual(po.reminder_date_before_receipt, 2) # check date_planned is correctly set self.assertEqual(po.date_planned, date_planned) @@ -132,9 +149,9 @@ class TestPurchase(AccountTestInvoicingCommon): po_line.price_unit = 200 # set to send reminder tomorrow po.date_planned = fields.Datetime.now() + timedelta(days=2) - po.receipt_reminder_email = True - po.reminder_date_before_receipt = 1 po = po.save() + self.partner_a.receipt_reminder_email = True + self.partner_a.reminder_date_before_receipt = 1 po.button_confirm() # check vendor is a message recipient