From 9d1cb826c7af054e95da7819e93d416b44dbf866 Mon Sep 17 00:00:00 2001 From: bve-odoo Date: Thu, 14 Jul 2022 14:05:04 +0000 Subject: [PATCH] [FIX] crm: missing company on default crm team on incoming emails Commit 0edc854a8494eb475087d011efddc7e15b262127 introduces a company check on the salesperson, partner & team on the lead model. However 184334a97d1ccd19b9a7266a13ff51531d22cff4 was not able to find the expected company on a lead created by email when the team did not have a company set. Which is the case by default for the following crm.team: - Sales (3ad1867ea4d7fcda4ccfeb0a7f3d3860b16d389e) - Pre-Sales - Website (3ad1867ea4d7fcda4ccfeb0a7f3d3860b16d389e) - PoS Having added the company check introduce an unexpected error: "Incompatible companies on records:\n- '[header subject of email]' belongs to company False and 'Customer' (partner_id: '[name in header from of email]') belongs to another company." Error was hard to spot as the db logs are reporting that the routing of the email was correctly done. But script to send eml using xmlrpc showed the error. In this commit we fix the issue by removing the custom code trying to set a company in ``message_new`` method of crm.lead. Indeed as it is a computed field with a complete heuristic it is easier to let the ORM do its job and remove that wrong value. It is now correctly computed as with any other lead. opw-2862509 opw-2783249 opw-2832435 opw-2881841 closes odoo/odoo#98746 X-original-commit: 2fec9c53314ef3f7c7e3305a6ee2417018d70ce5 Signed-off-by: Thibault Delavallee (tde) Co-authored-by: Xavier Alt --- addons/crm/models/crm_lead.py | 3 --- .../crm/tests/test_crm_lead_multicompany.py | 23 ++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 374a6977a25..8a71718cd83 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -1889,9 +1889,6 @@ class Lead(models.Model): 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, msg_vals): diff --git a/addons/crm/tests/test_crm_lead_multicompany.py b/addons/crm/tests/test_crm_lead_multicompany.py index 583bfee4032..8f26eb18872 100644 --- a/addons/crm/tests/test_crm_lead_multicompany.py +++ b/addons/crm/tests/test_crm_lead_multicompany.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo.addons.crm.tests.common import TestCrmCommon +from odoo.addons.crm.tests.common import TestCrmCommon, INCOMING_EMAIL from odoo.exceptions import AccessError, UserError from odoo.tests import Form, tagged from odoo.tests.common import users @@ -299,3 +299,24 @@ class TestCRMLeadMultiCompany(TestCrmCommon): crm_lead_form.user_id = self.env['res.users'] # ensuring that company_id is not overwritten when the salesperson becomes empty (w\o any team_id) self.assertEqual(crm_lead_form.company_id, self.company_2, 'Crm: company comes from partner') + + def test_gateway_incompatible_company_error_on_incoming_email(self): + self.assertTrue(self.sales_team_1.alias_name) + self.assertFalse(self.sales_team_1.company_id) + customer_company = self.env['res.partner'].create({ + 'company_id': self.company_2.id, + 'email': 'customer.another.company@test.customer.com', + 'mobile': '+32455000000', + 'name': 'InCompany Customer', + }) + + new_lead = self.format_and_process( + INCOMING_EMAIL, + customer_company.email, + '%s@%s' % (self.sales_team_1.alias_name, self.alias_domain), + subject='Team having partner in company', + target_model='crm.lead', + ) + self.assertEqual(new_lead.company_id, self.company_2) + self.assertEqual(new_lead.email_from, customer_company.email) + self.assertEqual(new_lead.partner_id, customer_company)