Skip to content

Commit

Permalink
Merge PR #450 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Sep 14, 2023
2 parents b5337b4 + 519bffc commit f332dd4
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 217 deletions.
1 change: 0 additions & 1 deletion sale_commission_agent_restrict/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from . import models
from .hooks import restore_access_rules
11 changes: 8 additions & 3 deletions sale_commission_agent_restrict/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
{
"name": "Sales Commissions Agent Restrict",
"version": "14.0.1.1.1",
"version": "14.0.2.0.0",
"author": "Ooops404, Ilyas, Odoo Community Association (OCA)",
"maintainers": ["aleuffre"],
"category": "Sales",
"website": "https://github.com/OCA/commission",
"license": "AGPL-3",
"depends": ["sale_commission", "web_domain_field"],
"depends": [
"base_rule_visibility_restriction",
"sale_commission",
"sales_team",
"web_domain_field",
],
"data": [
"security/security.xml",
"security/ir.model.access.csv",
"views/res_partner_views.xml",
"views/sale_order_view.xml",
],
"uninstall_hook": "restore_access_rules",
"installable": True,
}
19 changes: 0 additions & 19 deletions sale_commission_agent_restrict/hooks.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from openupgradelib import openupgrade

PREDEFINED_RULES = [
"base.res_partner_rule_private_employee",
"base.res_partner_rule_private_group",
]


def reset_predefined_rules_to_active(env):
"""
Set rules that were deactivated by the previous version
of the module back to active
"""
for rule_xml_id in PREDEFINED_RULES:
env.ref(rule_xml_id).active = True


def remove_view_with_removed_field(env):
"""
This view had nothing else in it but the removed field
so it should be safe to remove
"""
view = env.ref(
"sale_commission_agent_restrict.view_order_form_partner_domain_inherit",
raise_if_not_found=False,
)
if view:
view.unlink()


@openupgrade.migrate()
def migrate(env, version):
reset_predefined_rules_to_active(env)
remove_view_with_removed_field(env)
3 changes: 1 addition & 2 deletions sale_commission_agent_restrict/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from . import res_users
from . import res_partner
from . import res_users
from . import sale
from . import mail_thread
16 changes: 0 additions & 16 deletions sale_commission_agent_restrict/models/mail_thread.py

This file was deleted.

87 changes: 61 additions & 26 deletions sale_commission_agent_restrict/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,83 @@
from odoo import api, models
from odoo import _, api, models
from odoo.exceptions import UserError


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"
def assign_remove_agent_groups(self, is_agent):
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 is_agent:
self.user_ids.write(
{
"groups_id": [
(4, group_agent_own_customers.id),
(4, group_agent_own_commissions.id),
]
}
)
group_agent_own_commissions = self.env.ref(
"sale_commission_agent_restrict.group_agent_own_commissions"
else:
self.user_ids.write(
{
"groups_id": [
(3, group_agent_own_customers.id),
(3, group_agent_own_commissions.id),
]
}
)
if values["agent"]:
group_agent_own_customers.users = [
(4, user.id) for user in self._origin.user_ids
]
group_agent_own_commissions.users = [
(4, user.id) for user in self._origin.user_ids
]
else:
group_agent_own_customers.users = [
(3, user.id) for user in self._origin.user_ids
]
group_agent_own_commissions.users = [
(3, user.id) for user in self._origin.user_ids
]

@api.model
def check_agent_changing_payment_terms(self, vals):
if self.env.user.partner_id.agent:
if (
"property_payment_term_id" in vals
or "property_supplier_payment_term_id" in vals
):
raise UserError(
_("Agents are not allowed to change Contacts' payment terms")
)

@api.model
def check_agent_changing_agents(self, vals):
if self.env.user.partner_id.agent:
if "agent_ids" in vals:
raise UserError(
_(
"Agents are not allowed to change the agents assigned to a Contact"
)
)

def write(self, vals):
self.check_agent_changing_payment_terms(vals)
self.check_agent_changing_agents(vals)
res = super(ResPartner, self).write(vals)
if "agent" in vals and self.user_ids:
self.assign_remove_agent_groups(vals["agent"])
return res

@api.model
def create(self, vals):
self.check_agent_changing_payment_terms(vals)
if self.env.user.partner_id.agent:
vals["agent_ids"] = [(4, self.env.user.partner_id.id, 0)]
res = super(ResPartner, self).create(vals)
return res
# extra safe, but the field should never be pre-populated because
# agent cannot see the field agent_ids in the form view
vals["agent_ids"] = vals.get("agent_ids", []) + [
(4, self.env.user.partner_id.id, 0)
]
return super(ResPartner, self).create(vals)

def _update_fields_values(self, fields):
res = super(ResPartner, self)._update_fields_values(fields)
if "agent_ids" in res.keys():
for agent in self.agent_ids:
for user_id in agent.user_ids:
if user_id and user_id.has_group(
"sale_commission_agent_restrict.group_agent_own_customers"
"sale_commission_agent_restrict.group_agent_own_commissions"
):
# do not populate parents agent_ids to child partners
res.pop("agent_ids")
Expand Down
22 changes: 4 additions & 18 deletions sale_commission_agent_restrict/models/res_users.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
from odoo import _, api, models
from odoo import _, models
from odoo.exceptions import ValidationError

# -- List of predefined rules that must be managed
PREDEFINED_RULES = [
"res.partner.rule.private.employee",
"res.partner.rule.private.group",
]


class ResUsers(models.Model):
_inherit = "res.users"

@api.model
def tweak_access_rules(self):
# Need to shut down non-updatable rules to ensure tweak is applied correctly
rules = self.env["ir.rule"].sudo().search([("name", "in", PREDEFINED_RULES)])
if rules:
rules.sudo().write({"active": False})

def write(self, values):
def write(self, vals):
# users partner must be an agent to have following groups
group1_id = self.env.ref(
"sale_commission_agent_restrict.group_agent_own_customers"
Expand All @@ -28,7 +15,7 @@ def write(self, values):
).id
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 group1_name in vals or group2_name in vals:
if not self.partner_id.agent:
raise ValidationError(
_(
Expand All @@ -37,5 +24,4 @@ def write(self, values):
"able to assign agent related group to him."
)
)
res = super(ResUsers, self).write(values)
return res
return super().write(vals)
24 changes: 3 additions & 21 deletions sale_commission_agent_restrict/models/sale.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

from odoo import api, fields, models


Expand All @@ -8,30 +6,14 @@ class SaleOrder(models.Model):

current_agent_total = fields.Float(
string="My Commission",
compute="_compute_commission_total",
store=True,
compute="_compute_current_agent_total",
store=False,
)
partner_id_domain = fields.Char(compute="_compute_partner_id_domain")

@api.depends("order_line.agent_ids.amount")
def _compute_commission_total(self):
def _compute_current_agent_total(self):
for record in self:
record.commission_total = sum(record.mapped("order_line.agent_ids.amount"))
my_com = record.mapped("order_line.agent_ids").filtered(
lambda x: x.agent_id == self.env.user.partner_id
)
record.current_agent_total = sum(my_com.mapped("amount"))

@api.depends_context("company", "uid")
@api.depends("company_id")
def _compute_partner_id_domain(self):
for rec in self:
rec.partner_id_domain = json.dumps(
[
"&",
("id", "!=", self.env.user.partner_id.id),
"|",
("company_id", "=", False),
("company_id", "=", self.env.company.id),
]
)
4 changes: 4 additions & 0 deletions sale_commission_agent_restrict/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* `Ooops404 <https://www.ooops404.com>`__:

* Ilyas <[email protected]>

* `PyTech SRL <https://www.pytech.it>`__:

* Alessandro Uffreduzzi <[email protected]>
Loading

0 comments on commit f332dd4

Please sign in to comment.