Skip to content

Commit

Permalink
[FIX] sale_commission_product_criteria_domain: onchange agents
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasProgrammer committed Sep 19, 2023
1 parent 4b992c3 commit 7739869
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
10 changes: 9 additions & 1 deletion sale_commission_product_criteria_domain/models/commission.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# © 2023 ooops404
# License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0.html
from odoo import api, fields, models
from odoo import _, api, exceptions, fields, models


class SaleCommission(models.Model):
Expand Down Expand Up @@ -79,3 +79,11 @@ def _compute_agent_group_ids(self):
[("commission_id", "=", rec.agent_id.commission_id.id), dom]
)
rec.agent_group_ids = [(6, 0, items.mapped("group_id").ids)]

@api.constrains("group_ids", "agent_id", "partner_id")
def _constraint_commission_item_agent_ids(self):
for cia in self:
if not cia.group_ids:
raise exceptions.ValidationError(
_("At least one group for each restriction must be selected.")
)
9 changes: 6 additions & 3 deletions sale_commission_product_criteria_domain/models/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ def _onchange_agent_ids(self):
for rec in self:
exiting_agents = rec.commission_item_agent_ids.mapped("agent_id")
to_create = [
{"partner_id": rec._origin.id, "agent_id": x._origin.id}
{
"agent_id": x.id,
"group_ids": [(6, 0, x.allowed_commission_group_ids.ids)],
}
for x in rec.agent_ids.filtered(
lambda x: x.commission_id.commission_type == "product_restricted"
)
if x not in exiting_agents.ids
if x not in exiting_agents
]
to_delete = rec.commission_item_agent_ids.filtered(
lambda x: x.agent_id.id in (exiting_agents - rec.agent_ids).ids
lambda x: x.agent_id in (exiting_agents - rec.agent_ids)
)
if to_delete:
rec.update(
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
This module allows to limit applied commission items for specific groups.

This module allows to manage the following commission structure use case:

Agent A receives commission 50 when selling product 1 to customer X

Agent A receives commission 20 when selling product 1 to customer Y

This implementation is based on pricelist-like Commission Type structure provided by sale_commission_product_criteria.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def test_commission_domain(self):

#
tst_partner = so.partner_id.copy({})
tst_partner.commission_item_agent_ids = [(6, 0, self.demo_cig_italy.ids)]
tst_partner.commission_item_agent_ids.group_ids = [
(6, 0, self.demo_cig_italy.ids)
]
so.partner_id = tst_partner
so.order_line.agent_ids.agent_id = self.demo_agent_rules_restricted_italy
res = so.order_line.agent_ids._get_single_commission_amount(
Expand Down Expand Up @@ -285,6 +287,16 @@ def test_commission_domain(self):
self.assertEqual(so.order_line.agent_ids.amount, 0)
self.assertEqual(invoice.line_ids.agent_ids.amount, 0)

# check constraint: group_ids must be set
with self.assertRaises(odoo.exceptions.ValidationError):
self.env["commission.item.agent"].create(
{
"group_ids": False,
"agent_id": 1,
"partner_id": 1,
}
)

def _create_sale_order(self, product, partner):
return self.sale_order_model.create(
{
Expand Down
5 changes: 4 additions & 1 deletion sale_commission_product_criteria_domain/views/views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
domain="allowed_commission_group_ids_domain"
widget="many2many_tags"
options="{'no_create': True}"
attrs="{'invisible': [('commission_type', '!=', 'product_restricted')]}"
attrs="{
'invisible': [('commission_type', '!=', 'product_restricted')],
'required': [('commission_type', '=', 'product_restricted')]
}"
/>
</field>
</field>
Expand Down

0 comments on commit 7739869

Please sign in to comment.