[IMP] project: add company restrictions on projects and their company
Add a constraint preventing the user from switching the project from company if the partner doesn't belong to that company, and vice versa. task-3126301 closes odoo/odoo#109464 Signed-off-by: Xavier Bol (xbo) <xbo@odoo.com>
This commit is contained in:
@@ -313,7 +313,7 @@ class Project(models.Model):
|
||||
active = fields.Boolean(default=True,
|
||||
help="If the active field is set to False, it will allow you to hide the project without removing it.")
|
||||
sequence = fields.Integer(default=10)
|
||||
partner_id = fields.Many2one('res.partner', string='Customer', auto_join=True, tracking=True, domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")
|
||||
partner_id = fields.Many2one('res.partner', string='Customer', auto_join=True, tracking=True, check_company=True)
|
||||
partner_email = fields.Char(
|
||||
compute='_compute_partner_email', inverse='_inverse_partner_email',
|
||||
string='Email', readonly=False, store=True, copy=False)
|
||||
|
||||
@@ -8,7 +8,9 @@ from odoo.tools import email_normalize
|
||||
class ResPartner(models.Model):
|
||||
""" Inherits partner and adds Tasks information in the partner form """
|
||||
_inherit = 'res.partner'
|
||||
_check_company_auto = True
|
||||
|
||||
project_ids = fields.One2many('project.project', 'partner_id', string='Projects', check_company=True)
|
||||
task_ids = fields.One2many('project.task', 'partner_id', string='Tasks')
|
||||
task_count = fields.Integer(compute='_compute_task_count', string='# Tasks')
|
||||
|
||||
|
||||
@@ -174,3 +174,26 @@ class TestProjectBase(TestProjectCommon):
|
||||
|
||||
self.assertTrue(self.project_pigs.id < self.project_goats.id)
|
||||
self.assertEqual(Project.search(domain, order='id').ids, project_ids)
|
||||
|
||||
def test_change_project_or_partner_company(self):
|
||||
""" Tests that it is impossible to change the company of a project
|
||||
if the company of the partner is different and vice versa.
|
||||
"""
|
||||
company_1 = self.env.company
|
||||
company_2 = self.env['res.company'].create({'name': 'Company 2'})
|
||||
partner = self.env['res.partner'].create({
|
||||
'name': 'Partner',
|
||||
})
|
||||
self.project_pigs.partner_id = partner
|
||||
# Can change the company of a project if the company of the partner is not set
|
||||
self.assertFalse(partner.company_id)
|
||||
self.project_pigs.company_id = company_2
|
||||
self.project_pigs.partner_id.company_id = company_2
|
||||
|
||||
with self.assertRaises(UserError):
|
||||
# Cannot change the company of a partner if the company of the project is different
|
||||
partner.company_id = company_1
|
||||
|
||||
with self.assertRaises(UserError):
|
||||
# Cannot change the company of a project if the company of the partner is different
|
||||
self.project_pigs.company_id = company_1
|
||||
|
||||
Reference in New Issue
Block a user