Skip to content

Commit

Permalink
[FIX] sale_commission_agent_restrict: write method has to manage reco…
Browse files Browse the repository at this point in the history
…rdsets
  • Loading branch information
SirTakobi committed Aug 29, 2022
1 parent 4c3c6d9 commit 419147d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
42 changes: 20 additions & 22 deletions sale_commission_agent_restrict/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,26 @@ class ResPartner(models.Model):
_inherit = "res.partner"

def write(self, values):
res = super(ResPartner, self).write(values)
if len(self.user_ids) > 0 and "agent" in values:
group_agent_own_customers = self.env.ref(
"sale_commission_agent_restrict.group_agent_own_customers"
)
group_agent_own_commissions = self.env.ref(
"sale_commission_agent_restrict.group_agent_own_commissions"
)
if values["agent"]:
group_agent_own_customers.users = [
(4, user.id) for user in self.user_ids
]
group_agent_own_commissions.users = [
(4, user.id) for user in self.user_ids
]
else:
group_agent_own_customers.users = [
(3, user.id) for user in self.user_ids
]
group_agent_own_commissions.users = [
(3, user.id) for user in self.user_ids
]
res = super().write(values)
if 'agent' in values:
linked_users = self.mapped('user_ids')
if linked_users:
group_agent_own_customers = self.env.ref(
"sale_commission_agent_restrict.group_agent_own_customers"
)
group_agent_own_commissions = self.env.ref(
"sale_commission_agent_restrict.group_agent_own_commissions"
)
if values["agent"]:
update_users = [
(4, user.id) for user in linked_users
]
else:
update_users = [
(3, user.id) for user in linked_users
]
group_agent_own_customers.users = update_users
group_agent_own_commissions.users = update_users
return res

@api.model
Expand Down
22 changes: 15 additions & 7 deletions sale_commission_agent_restrict/models/res_users.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright 2022 Simone Rubino - TAKOBI
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, api, models
from odoo.exceptions import ValidationError

Expand Down Expand Up @@ -29,13 +32,18 @@ def write(self, values):
group1_name = "in_group_" + str(group1_id)
group2_name = "in_group_" + str(group2_id)
if group1_name in values or group2_name in values:
if not self.partner_id.agent:
raise ValidationError(
_(
"This user is not associated to any agent. "
"Please mark corresponding to this user partner as agent to be "
"able to assign agent related group to him."
for user in self:
if not user.partner_id.agent:
raise ValidationError(
_(
"User {user} is not associated to any agent.\n"
"Please mark "
"partner corresponding to this user as agent "
"to be able to assign agent related group to him."
)
.format(
user=user.display_name,
)
)
)
res = super(ResUsers, self).write(values)
return res

0 comments on commit 419147d

Please sign in to comment.