Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][FIX] sale_commission_product_criteria_domain: onchange agents withs … #452

Merged
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
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
Loading