From 8da3f0c87385c9fcd9846d19568c87b130f8ae88 Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Fri, 11 Jan 2019 13:36:02 +0000 Subject: [PATCH] [FIX] crm: create lead from email in right sale team and company crm lead is created with a default company_id which is the current company of the user When creating a lead from email, this logic doesn't hold, especially in v12.0 where the __system__ user is not someone real In that case, we retrieve the company from the sales team OPW 1918837 closes odoo/odoo#30146 --- addons/crm/models/crm_lead.py | 4 + .../crm/tests/test_new_lead_notification.py | 93 +++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 7fa44b31bfd..8f9b8f3638b 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -1204,6 +1204,10 @@ class Lead(models.Model): if msg_dict.get('priority') in dict(crm_stage.AVAILABLE_PRIORITIES): defaults['priority'] = msg_dict.get('priority') defaults.update(custom_values) + + # assign right company + if 'company_id' not in defaults and 'team_id' in defaults: + defaults['company_id'] = self.env['crm.team'].browse(defaults['team_id']).company_id.id return super(Lead, self).message_new(msg_dict, custom_values=defaults) def _message_post_after_hook(self, message, *args, **kwargs): diff --git a/addons/crm/tests/test_new_lead_notification.py b/addons/crm/tests/test_new_lead_notification.py index dedcd02449a..7335dafa17e 100644 --- a/addons/crm/tests/test_new_lead_notification.py +++ b/addons/crm/tests/test_new_lead_notification.py @@ -41,3 +41,96 @@ class NewLeadNotification(TestCrmCases): # The user should have a new unread message lead_user = lead.sudo(self.crm_salesman) self.assertTrue(lead_user.message_needaction) + + def test_new_lead_from_email_multicompany(self): + companies = self.env['res.company'].search([]) + + company0 = companies[0] + company1 = companies[1] + self.env.user.write({ + 'company_ids': [(4, company0.id, False), (4, company1.id, False)], + }) + + crm_team_model = self.env['ir.model'].search([('model', '=', 'crm.team')]) + crm_lead_model = self.env['ir.model'].search([('model', '=', 'crm.lead')]) + self.env["ir.config_parameter"].sudo().set_param("mail.catchall.domain", 'aqualung.com') + + crm_team0 = self.env['crm.team'].create({ + 'name': 'crm team 0', + 'company_id': company0.id, + }) + crm_team1 = self.env['crm.team'].create({ + 'name': 'crm team 1', + 'company_id': company1.id, + }) + + mail_alias0 = self.env['mail.alias'].create({ + 'alias_name': 'sale_team_0', + 'alias_model_id': crm_lead_model.id, + 'alias_parent_model_id': crm_team_model.id, + 'alias_parent_thread_id': crm_team0.id, + 'alias_defaults': "{'type': 'opportunity', 'team_id': %s}" % crm_team0.id, + }) + mail_alias1 = self.env['mail.alias'].create({ + 'alias_name': 'sale_team_1', + 'alias_model_id': crm_lead_model.id, + 'alias_parent_model_id': crm_team_model.id, + 'alias_parent_thread_id': crm_team1.id, + 'alias_defaults': "{'type': 'opportunity', 'team_id': %s}" % crm_team1.id, + }) + + crm_team0.write({'alias_id': mail_alias0.id}) + crm_team1.write({'alias_id': mail_alias1.id}) + + new_message0 = """MIME-Version: 1.0 +Date: Thu, 27 Dec 2018 16:27:45 +0100 +Message-ID: blablabla0 +Subject: sale team 0 in company 0 +From: A client +To: sale_team_0@aqualung.com +Content-Type: multipart/alternative; boundary="000000000000a47519057e029630" + +--000000000000a47519057e029630 +Content-Type: text/plain; charset="UTF-8" + + +--000000000000a47519057e029630 +Content-Type: text/html; charset="UTF-8" +Content-Transfer-Encoding: quoted-printable + +
A good message
+ +--000000000000a47519057e029630-- +""" + + new_message1 = """MIME-Version: 1.0 +Date: Thu, 27 Dec 2018 16:27:45 +0100 +Message-ID: blablabla1 +Subject: sale team 1 in company 1 +From: B client +To: sale_team_1@aqualung.com +Content-Type: multipart/alternative; boundary="000000000000a47519057e029630" + +--000000000000a47519057e029630 +Content-Type: text/plain; charset="UTF-8" + + +--000000000000a47519057e029630 +Content-Type: text/html; charset="UTF-8" +Content-Transfer-Encoding: quoted-printable + +
A good message bis
+ +--000000000000a47519057e029630-- +""" + crm_lead0_id = self.env['mail.thread'].message_process('crm.lead', new_message0) + crm_lead1_id = self.env['mail.thread'].message_process('crm.lead', new_message1) + + crm_lead0 = self.env['crm.lead'].browse(crm_lead0_id) + crm_lead1 = self.env['crm.lead'].browse(crm_lead1_id) + + self.assertEqual(crm_lead0.team_id, crm_team0) + self.assertEqual(crm_lead1.team_id, crm_team1) + + self.assertEqual(crm_lead0.company_id, company0) + self.assertEqual(crm_lead1.company_id, company1)