diff --git a/addons/mail/models/models.py b/addons/mail/models/models.py index a58ba9116ee..38db70f08da 100644 --- a/addons/mail/models/models.py +++ b/addons/mail/models/models.py @@ -168,7 +168,10 @@ class BaseModel(models.AbstractModel): if len(record_email) >= 78: return record_email - company_name = self.env.company.name + if 'company_id' in self and len(self.company_id) == 1: + company_name = self.sudo().company_id.name + else: + company_name = self.env.company.name # try company_name + record_name, or record_name alone (or company_name alone) name = f"{company_name} {record_name}" if record_name else company_name diff --git a/addons/test_mail/tests/test_mail_multicompany.py b/addons/test_mail/tests/test_mail_multicompany.py index 8717408b186..e219fddf8e0 100644 --- a/addons/test_mail/tests/test_mail_multicompany.py +++ b/addons/test_mail/tests/test_mail_multicompany.py @@ -38,6 +38,7 @@ class TestMultiCompanySetup(TestMailCommon, TestRecipients): 'company_id': cls.user_employee_c2.company_id.id}, ]) + cls.company_3 = cls.env['res.company'].create({'name': 'ELIT'}) cls.partner_1 = cls.env['res.partner'].with_context(cls._test_context).create({ 'name': 'Valid Lelitre', 'email': 'valid.lelitre@agrolait.com', @@ -149,7 +150,8 @@ class TestMultiCompanySetup(TestMailCommon, TestRecipients): @users('employee_c2') def test_notify_reply_to_computation_mc(self): """ Test reply-to computation in multi company mode. Add notably tests - depending on user company_id / company_ids. """ + depending on user and records company_id / company_ids. """ + # Test1: no company_id field test_record = self.env['mail.test.gateway'].browse(self.test_record.ids) res = test_record._notify_get_reply_to() @@ -160,7 +162,7 @@ class TestMultiCompanySetup(TestMailCommon, TestRecipients): "%s@%s" % (self.alias_catchall, self.alias_domain))) ) - # Test2: company_id field, MC environment + # Test2: MC environment get default value from env self.user_employee_c2.write({'company_ids': [(4, self.user_employee.company_id.id)]}) test_records = self.env['mail.test.multi.company'].create([ {'name': 'Test', @@ -177,6 +179,23 @@ class TestMultiCompanySetup(TestMailCommon, TestRecipients): "%s@%s" % (self.alias_catchall, self.alias_domain))) ) + # Test3: get company from record (company_id field) + self.user_employee_c2.write({'company_ids': [(4, self.company_3.id)]}) + test_records = self.env['mail.test.multi.company'].create([ + {'name': 'Test1', + 'company_id': self.company_3.id}, + {'name': 'Test2', + 'company_id': self.company_3.id}, + ]) + res = test_records._notify_get_reply_to() + for test_record in test_records: + self.assertEqual( + res[test_record.id], + formataddr(( + "%s %s" % (self.company_3.name, test_record.name), + "%s@%s" % (self.alias_catchall, self.alias_domain))) + ) + @users('employee_c2') @mute_logger('odoo.addons.base.models.ir_rule') def test_post_with_read_access(self):