From 3488398fd987551242243382d86d2869fba2ec22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Be=C3=B1at=20Jimenez?= Date: Wed, 6 Mar 2019 08:35:16 +0100 Subject: [PATCH] [FIX] Sales Team members must have the same Operating Unit --- sales_team_operating_unit/models/crm_team.py | 14 ++++++++++++-- .../tests/test_crm_team_operating_unit.py | 12 +++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/sales_team_operating_unit/models/crm_team.py b/sales_team_operating_unit/models/crm_team.py index 4facee5413..5dca77bd1d 100644 --- a/sales_team_operating_unit/models/crm_team.py +++ b/sales_team_operating_unit/models/crm_team.py @@ -5,7 +5,7 @@ # () # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). from odoo import api, fields, models, _ -from odoo.exceptions import UserError +from odoo.exceptions import UserError, ValidationError class CrmTeam(models.Model): @@ -24,5 +24,15 @@ def _check_company_operating_unit(self): if team.company_id and \ team.operating_unit_id and \ team.company_id != team.operating_unit_id.company_id: - raise UserError(_('Configuration error!\n\nThe Company in the\ + raise UserError(_('Configuration error\n\nThe Company in the\ Sales Team and in the Operating Unit must be the same.')) + + @api.multi + @api.constrains('operating_unit_id', 'member_ids') + def _check_member_operating_unit(self): + for rec in self.member_ids: + if (rec and self.operating_unit_id and + self.operating_unit_id not in rec.operating_unit_ids): + raise ValidationError(_('Configuration error. The user %s ' + 'has not assigned the same ' + 'Operating unit.' % rec.name)) diff --git a/sales_team_operating_unit/tests/test_crm_team_operating_unit.py b/sales_team_operating_unit/tests/test_crm_team_operating_unit.py index 671e7e9ac9..3e7e9a68fb 100644 --- a/sales_team_operating_unit/tests/test_crm_team_operating_unit.py +++ b/sales_team_operating_unit/tests/test_crm_team_operating_unit.py @@ -5,7 +5,7 @@ # () # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). from odoo.tests import common - +from odoo import exceptions class TestSaleTeamOperatingUnit(common.TransactionCase): @@ -65,3 +65,13 @@ def test_crm_team(self): ('operating_unit_id', '=', self.ou1.id)]) self.assertEqual(team.ids, [], 'User 2 should not have access to ' '%s' % self.ou1.name) + + def test_member_operating_unit(self): + # User 2 is assigned to B2C Operating Unit and cannot be a member of + # a team with the Operating Unit OU1 + with self.assertRaises(exceptions.ValidationError): + self.crm_team_model.sudo().create({ + 'name': 'Test Team', + 'operating_unit_id': self.ou1.id, + 'member_ids': [(4, self.user2.id)] + })