[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
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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 <client_a@someprovider.com>
|
||||
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
|
||||
|
||||
<div>A good message</div>
|
||||
|
||||
--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 <client_b@someprovider.com>
|
||||
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
|
||||
|
||||
<div>A good message bis</div>
|
||||
|
||||
--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)
|
||||
|
||||
Reference in New Issue
Block a user