|
2 | 2 | # Part of Odoo. See LICENSE file for full copyright and licensing details. |
3 | 3 |
|
4 | 4 | from odoo.addons.crm.tests.common import TestCrmCommon |
5 | | -from odoo.exceptions import AccessError |
| 5 | +from odoo.exceptions import AccessError, UserError |
6 | 6 | from odoo.tests import Form, tagged |
7 | 7 | from odoo.tests.common import users |
8 | 8 |
|
@@ -116,6 +116,38 @@ def test_lead_mc_company_computation_env_user_restrict(self): |
116 | 116 | self.assertEqual(lead.team_id, self.sales_team_1) |
117 | 117 | self.assertEqual(lead.user_id, self.user_sales_manager_mc) |
118 | 118 |
|
| 119 | + |
| 120 | + @users('user_sales_manager_mc') |
| 121 | + def test_lead_mc_company_computation_partner_restrict(self): |
| 122 | + """ Check company on partner limits the company on lead. As contacts may |
| 123 | + be separated by company, lead with a partner should be limited to that |
| 124 | + company. """ |
| 125 | + partner_c2 = self.partner_c2.with_env(self.env) |
| 126 | + self.assertEqual(partner_c2.company_id, self.company_2) |
| 127 | + lead = self.env['crm.lead'].create({ |
| 128 | + 'partner_id': partner_c2.id, |
| 129 | + 'name': 'MC Partner, no company lead', |
| 130 | + 'user_id': False, |
| 131 | + 'team_id': False, |
| 132 | + }) |
| 133 | + self.assertEqual(lead.company_id, self.company_2) |
| 134 | + |
| 135 | + partner_main = self.env['res.partner'].create({ |
| 136 | + 'company_id': self.company_main.id, |
| 137 | + |
| 138 | + 'name': 'Customer for Main', |
| 139 | + }) |
| 140 | + lead.write({'partner_id': partner_main}) |
| 141 | + self.assertEqual(lead.company_id, self.company_main) |
| 142 | + |
| 143 | + # writing current user on lead would imply putting its team and team's company |
| 144 | + # on lead (aka self.company_2), and this clashes with company restriction on |
| 145 | + # customer |
| 146 | + with self.assertRaises(UserError): |
| 147 | + lead.write({ |
| 148 | + 'user_id': self.env.user, |
| 149 | + }) |
| 150 | + |
119 | 151 | @users('user_sales_manager_mc') |
120 | 152 | def test_lead_mc_company_form(self): |
121 | 153 | """ Test lead company computation using form view """ |
@@ -187,3 +219,21 @@ def test_lead_mc_company_form_progressives_setup(self): |
187 | 219 | crm_lead_form.user_id = self.env.user |
188 | 220 | # self.assertEqual(crm_lead_form.company_id, self.env['res.company']) # FIXME |
189 | 221 | self.assertEqual(crm_lead_form.company_id, self.company_2) |
| 222 | + |
| 223 | + @users('user_sales_manager_mc') |
| 224 | + def test_lead_mc_company_form_w_partner_id(self): |
| 225 | + """ Test lead company computation with partner having a company. """ |
| 226 | + partner_c2 = self.partner_c2.with_env(self.env) |
| 227 | + crm_lead_form = Form(self.env['crm.lead']) |
| 228 | + crm_lead_form.name = "Test Lead" |
| 229 | + |
| 230 | + crm_lead_form.user_id = self.user_sales_manager_mc |
| 231 | + crm_lead_form.partner_id = partner_c2 |
| 232 | + self.assertEqual(crm_lead_form.company_id, self.company_2, 'Crm: company comes from sales') |
| 233 | + self.assertEqual(crm_lead_form.team_id, self.team_company2, 'Crm: team comes from sales') |
| 234 | + |
| 235 | + # reset sales: should not reset company, as partner constrains it |
| 236 | + crm_lead_form.team_id = self.env['crm.team'] |
| 237 | + crm_lead_form.user_id = self.env['res.users'] |
| 238 | + # ensuring that company_id is not overwritten when the salesperson becomes empty (w\o any team_id) |
| 239 | + self.assertEqual(crm_lead_form.company_id, self.company_2, 'Crm: company comes from partner') |
0 commit comments