From b4067a156a0d1bd2df496d4f4d2f83267e6d981d Mon Sep 17 00:00:00 2001 From: Ilyas Date: Tue, 12 Sep 2023 10:04:11 +0700 Subject: [PATCH] [FIX] sale_commission_product_criteria_domain: onchange agents --- .../models/commission.py | 10 +++++++++- .../models/partner.py | 11 ++++++++--- .../readme/DESCRIPTION.rst | 8 ++++++++ ...test_sale_commission_product_criteria_domain.py | 14 +++++++++++++- .../views/views.xml | 5 ++++- 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/sale_commission_product_criteria_domain/models/commission.py b/sale_commission_product_criteria_domain/models/commission.py index c280d9535..292403b0d 100644 --- a/sale_commission_product_criteria_domain/models/commission.py +++ b/sale_commission_product_criteria_domain/models/commission.py @@ -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): @@ -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.") + ) diff --git a/sale_commission_product_criteria_domain/models/partner.py b/sale_commission_product_criteria_domain/models/partner.py index 0d6e0c9fe..7be8e20de 100644 --- a/sale_commission_product_criteria_domain/models/partner.py +++ b/sale_commission_product_criteria_domain/models/partner.py @@ -40,14 +40,19 @@ 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} + { + "partner_id": rec._origin.id, + "agent_id": x._origin.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._origin.id not in exiting_agents.ids ] 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._origin.id + in (set(exiting_agents.ids) - set(rec.agent_ids.ids)) ) if to_delete: rec.update( diff --git a/sale_commission_product_criteria_domain/readme/DESCRIPTION.rst b/sale_commission_product_criteria_domain/readme/DESCRIPTION.rst index 4916f9805..9a28f3157 100644 --- a/sale_commission_product_criteria_domain/readme/DESCRIPTION.rst +++ b/sale_commission_product_criteria_domain/readme/DESCRIPTION.rst @@ -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. diff --git a/sale_commission_product_criteria_domain/tests/test_sale_commission_product_criteria_domain.py b/sale_commission_product_criteria_domain/tests/test_sale_commission_product_criteria_domain.py index 96ef954cf..958c68030 100644 --- a/sale_commission_product_criteria_domain/tests/test_sale_commission_product_criteria_domain.py +++ b/sale_commission_product_criteria_domain/tests/test_sale_commission_product_criteria_domain.py @@ -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( @@ -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( { diff --git a/sale_commission_product_criteria_domain/views/views.xml b/sale_commission_product_criteria_domain/views/views.xml index c1e00ae97..7aa8333a8 100644 --- a/sale_commission_product_criteria_domain/views/views.xml +++ b/sale_commission_product_criteria_domain/views/views.xml @@ -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')] + }" />