Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion res_partner_operating_unit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"category": "Generic",
"depends": ["operating_unit"],
"license": "LGPL-3",
"data": ["security/res_partner_security.xml", "views/res_partner_view.xml"],
"data": ["views/res_partner_view.xml"],
"installable": True,
"pre_init_hook": "pre_init_hook",
}
41 changes: 29 additions & 12 deletions res_partner_operating_unit/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,34 @@ class ResPartner(models.Model):
_inherit = "res.partner"
_check_company_auto = True

@api.model
def _default_operating_unit(self):
user = self.env["res.users"].browse(self.env.user.id)
return user.default_operating_unit_id

operating_unit_ids = fields.Many2many(
"operating.unit",
"operating_unit_partner_rel",
"partner_id",
"operating_unit_id",
"Operating Units",
required=True,
default=lambda self: self._default_operating_unit(),
comodel_name="operating.unit",
relation="operating_unit_partner_rel",
column1="partner_id",
column2="operating_unit_id",
string="Operating Units",
)

@api.model
def _user_ous_domain(self):
ou_ids = self.env.user.operating_unit_ids.ids
domain = [
"|",
("operating_unit_ids", "in", ou_ids),
("operating_unit_ids", "=", False),
]
return domain

# Extending methods to replace a record rule.
# Ref: https://github.com/OCA/operating-unit/issues/258
@api.model
def search(self, args, offset=0, limit=None, order=None):
# Get the OUs of the user
domain = self._user_ous_domain()
return super().search(domain + args, offset=offset, limit=limit, order=order)

@api.model
def search_count(self, args, limit=None):
# Get the OUs of the user
domain = self._user_ous_domain()
return super().search_count(domain + args, limit=limit)
55 changes: 26 additions & 29 deletions res_partner_operating_unit/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,39 @@
class ResUsers(models.Model):
_inherit = "res.users"

def _sync_partner_default_operating_unit(self):
for user in self:
if user.default_operating_unit_id:
user.partner_id.operating_unit_ids = [
Command.link(user.default_operating_unit_id.id)
]
user.check_partner_operating_unit()

@api.model_create_multi
def create(self, vals_list):
users = super().create(vals_list)
for user in users:
user_ou = user.default_operating_unit_id or user._default_operating_unit()
if not user_ou:
user.check_partner_operating_unit()
continue
user.partner_id.operating_unit_ids = [Command.link(user_ou.id)]
user.check_partner_operating_unit()
user._sync_partner_default_operating_unit()
return users

def write(self, vals):
for user in self:
res = super().write(vals)
if vals.get("default_operating_unit_id"):
# Add the new OU
user.partner_id.operating_unit_ids = [
Command.link(user.default_operating_unit_id.id)
]
user.check_partner_operating_unit()
return res
res = super().write(vals)
if vals.get("default_operating_unit_id"):
for user in self:
user._sync_partner_default_operating_unit()
return res

def check_partner_operating_unit(self):
self.ensure_one()
if (
self.partner_id.operating_unit_ids
and self.default_operating_unit_id
and (
self.default_operating_unit_id.id
not in self.partner_id.operating_unit_ids.ids
)
):
raise UserError(
_(
"The operating units of the partner must include the default "
"one of the user."
for user in self:
if (
user.partner_id.operating_unit_ids
and user.default_operating_unit_id
and user.default_operating_unit_id.id
not in user.partner_id.operating_unit_ids.ids
):
raise UserError(
_(
"The operating units of the partner must include the default "
"one of the user."
)
)
)
20 changes: 0 additions & 20 deletions res_partner_operating_unit/security/res_partner_security.xml

This file was deleted.

Loading