From 379b598754593ea248fb89e326f65298abc2e7d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Sainl=C3=A9ger?= Date: Fri, 19 May 2023 14:31:39 +0200 Subject: [PATCH] [IMP] partner_profiles: prevent company type edit in several cases Prevents the modification of the company type of res.partner: - if partner is a public or a position profile - if the partner has position profiles attached Also synchronize the company type between main and public profiles. --- partner_profiles/models/res_partner.py | 14 ++++++++++++++ partner_profiles/views/res_partner_view.xml | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/partner_profiles/models/res_partner.py b/partner_profiles/models/res_partner.py index 3980df1..8ed8205 100644 --- a/partner_profiles/models/res_partner.py +++ b/partner_profiles/models/res_partner.py @@ -3,6 +3,7 @@ import logging from odoo import _, api, fields, models +from odoo.exceptions import UserError _logger = logging.getLogger(__name__) @@ -80,6 +81,19 @@ class res_partner(models.Model): self.contact_type = "attached" self.partner_profile = position_profile.id + @api.onchange("is_company") + def _onchange_is_company(self): + for partner in self: + if partner.is_main_profile: + if partner.has_position or partner.child_ids.filtered(lambda c: c.is_position_profile): + raise UserError("You can not modify the partner company type when the parner has postion profiles associated. Please remove the position profiles before retrying.") + if partner.public_profile_id: + # public_partner = self.env["res.partner"].browse(partner.public_profile_id)[0] + values = { + "is_company": partner.is_company, + } + partner.public_profile_id.sudo().write(values) + @api.model def create(self, vals): """Assume if not type, default is contact""" diff --git a/partner_profiles/views/res_partner_view.xml b/partner_profiles/views/res_partner_view.xml index ef465f0..da4ac91 100644 --- a/partner_profiles/views/res_partner_view.xml +++ b/partner_profiles/views/res_partner_view.xml @@ -95,6 +95,10 @@ {'invisible': ['|', ('is_public_profile','=', True), ('is_position_profile','=', True)]} + + {'invisible': ['|', ('is_public_profile','=', True), + ('is_position_profile','=', True)]} + {'invisible': ['|', ('is_public_profile','=', True), ('is_position_profile','=', True)]}