From 699ba2d7828d64d873c42cb4b83b6f30815a4693 Mon Sep 17 00:00:00 2001 From: "Mahdi Cheikh Rouhou (macr)" Date: Wed, 31 May 2023 09:26:27 +0000 Subject: [PATCH] [FIX] repair : send email to author when included When we include the author in the recipients of the quotation email of a repair order he doesn't receive the email. Steps to reproduce the error : 1- create a repiar order 2- add the author in the list of recipients 3- send the email The origin of the problem is that mail_notify_author=False , se we need to add it as True when we have the author in the recipients. Similar old fix : https://github.com/odoo/odoo/commit/f49dbf595c870b682f36c11443c9cf1a9a027474 opw-3295744 closes odoo/odoo#126986 X-original-commit: d4d48526867e93295d93d4504e824cfdc2b146a7 Signed-off-by: William Henrotin (whe) Signed-off-by: Mahdi Cheikh Rouhou (macr) --- addons/repair/models/__init__.py | 1 + addons/repair/models/mail_compose_message.py | 13 +++++++++ addons/repair/tests/test_repair.py | 28 ++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 addons/repair/models/mail_compose_message.py diff --git a/addons/repair/models/__init__.py b/addons/repair/models/__init__.py index af19f542e98..43a7bd98095 100644 --- a/addons/repair/models/__init__.py +++ b/addons/repair/models/__init__.py @@ -7,3 +7,4 @@ from . import stock_traceability from . import stock_lot from . import account_move from . import product +from . import mail_compose_message diff --git a/addons/repair/models/mail_compose_message.py b/addons/repair/models/mail_compose_message.py new file mode 100644 index 00000000000..3d9520a60d9 --- /dev/null +++ b/addons/repair/models/mail_compose_message.py @@ -0,0 +1,13 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models + + +class MailComposeMessage(models.TransientModel): + _inherit = 'mail.compose.message' + + def _action_send_mail(self, auto_commit=False): + if self.model == 'repair.order': + self = self.with_context(mail_notify_author=self.env.user.partner_id in self.partner_ids) + return super(MailComposeMessage, self)._action_send_mail(auto_commit=auto_commit) diff --git a/addons/repair/tests/test_repair.py b/addons/repair/tests/test_repair.py index 73b0a83772a..ec17c181f98 100644 --- a/addons/repair/tests/test_repair.py +++ b/addons/repair/tests/test_repair.py @@ -386,3 +386,31 @@ class TestRepair(AccountTestInvoicingCommon): ('company_id', '=', repair.company_id.id), ], limit=1) self.assertEqual(repair.operations[0].location_dest_id, location_dest_id) + + def test_repair_order_send_to_self(self): + # when sender(logged in user) is also present in recipients of the mail composer, + # user should receive mail. + product_to_repair = self.product_product_5 + partner = self.res_partner_address_1 + repair_order = self.env['repair.order'].with_user(self.env.user).create({ + 'product_id': product_to_repair.id, + 'product_uom': product_to_repair.uom_id.id, + 'address_id': partner.id, + 'guarantee_limit': '2019-01-01', + 'location_id': self.stock_warehouse.lot_stock_id.id, + 'partner_id': self.env.user.partner_id.id + }) + email_ctx = repair_order.action_send_mail().get('context', {}) + # We need to prevent auto mail deletion, and so we copy the template and send the mail with + # added configuration in copied template. It will allow us to check whether mail is being + # sent to to author or not (in case author is present in 'Recipients' of composer). + mail_template = self.env['mail.template'].browse(email_ctx.get('default_template_id')).copy({'auto_delete': False}) + # send the mail with same user as customer + repair_order.with_context(**email_ctx).with_user(self.env.user).message_post_with_source( + mail_template, + subtype_xmlid='mail.mt_comment', + ) + mail_message = repair_order.message_ids[0] + self.assertEqual(mail_message.author_id, repair_order.partner_id, 'Repair: author should be same as customer') + self.assertEqual(mail_message.author_id, mail_message.partner_ids, 'Repair: author should be in composer recipients thanks to "partner_to" field set on template') + self.assertEqual(mail_message.partner_ids, mail_message.sudo().mail_ids.recipient_ids, 'Repair: author should receive mail due to presence in composer recipients')