Skip to content

Commit

Permalink
[FIX] crm: create lead from email in right sale team and company
Browse files Browse the repository at this point in the history
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#30146
  • Loading branch information
kebeclibre committed Jan 11, 2019
1 parent 4a3f04b commit 8da3f0c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions addons/crm/models/crm_lead.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,10 @@ def message_new(self, msg_dict, custom_values=None):
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):
Expand Down
93 changes: 93 additions & 0 deletions addons/crm/tests/test_new_lead_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,96 @@ def test_new_lead_notification(self):
# 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)

0 comments on commit 8da3f0c

Please sign in to comment.