From 695130eb2fc6b35872eeee704921635744eb605a Mon Sep 17 00:00:00 2001 From: Ilyas Date: Mon, 14 Aug 2023 16:57:56 +0200 Subject: [PATCH] wip tests --- .../models/__init__.py | 2 + .../models/account_invoice_line_agent.py | 60 ++++++++ .../models/commission.py | 129 ------------------ .../models/sale_order_line_agent.py | 75 ++++++++++ ...sale_commission_product_criteria_domain.py | 10 ++ 5 files changed, 147 insertions(+), 129 deletions(-) create mode 100644 sale_commission_product_criteria_domain/models/account_invoice_line_agent.py create mode 100644 sale_commission_product_criteria_domain/models/sale_order_line_agent.py diff --git a/sale_commission_product_criteria_domain/models/__init__.py b/sale_commission_product_criteria_domain/models/__init__.py index 148111ab5..bef5d718c 100644 --- a/sale_commission_product_criteria_domain/models/__init__.py +++ b/sale_commission_product_criteria_domain/models/__init__.py @@ -1,3 +1,5 @@ from . import commission from . import commission_group from . import partner +from . import account_invoice_line_agent +from . import sale_order_line_agent diff --git a/sale_commission_product_criteria_domain/models/account_invoice_line_agent.py b/sale_commission_product_criteria_domain/models/account_invoice_line_agent.py new file mode 100644 index 000000000..d9f78c7cf --- /dev/null +++ b/sale_commission_product_criteria_domain/models/account_invoice_line_agent.py @@ -0,0 +1,60 @@ +# © 2023 ooops404 +# License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0.html +from odoo import api, models + + +class AccountInvoiceLineAgent(models.Model): + _inherit = "account.invoice.line.agent" + + @api.depends( + "object_id.price_subtotal", + "object_id.product_id.commission_free", + "commission_id", + ) + def _compute_amount(self): + for line in self: + if ( + line.commission_id + and line.commission_id.commission_type == "product_restricted" + ): + inv_line = line.object_id + line.amount = line._get_single_commission_amount( + line.commission_id, + inv_line.price_subtotal, + inv_line.product_id, + inv_line.quantity, + ) + else: + super(AccountInvoiceLineAgent, line)._compute_amount() + + def _get_single_commission_amount(self, commission, subtotal, product, quantity): + self.ensure_one() + partner = self.object_id.partner_id + item_ids = self._get_commission_items(commission, product) + if item_ids: + return 0.0 + # Main idea is here: + group_ids = partner.commission_item_agent_ids.filtered( + lambda x: x.agent_id == self.agent_id + ).mapped("group_ids") + ci_with_group_ids = self.env["commission.item"].search( + [ + ("group_id", "in", group_ids.ids), + ] + ) + item_ids = list(set(item_ids).intersection(ci_with_group_ids.ids)) + if item_ids: + return 0.0 + commission_item = self.env["commission.item"].browse(item_ids[0]) + if commission.amount_base_type == "net_amount": + # If subtotal (sale_price * quantity) is less than + # standard_price * quantity, it means that we are selling at + # lower price than we bought, so set amount_base to 0 + subtotal = max([0, subtotal - product.standard_price * quantity]) + self.applied_commission_item_id = commission_item + # if self.agent_id.use_multi_type_commissions: + self.applied_commission_id = commission_item.commission_id + if commission_item.commission_type == "fixed": + return commission_item.fixed_amount + elif commission_item.commission_type == "percentage": + return subtotal * (commission_item.percent_amount / 100.0) diff --git a/sale_commission_product_criteria_domain/models/commission.py b/sale_commission_product_criteria_domain/models/commission.py index 5947462cd..c280d9535 100644 --- a/sale_commission_product_criteria_domain/models/commission.py +++ b/sale_commission_product_criteria_domain/models/commission.py @@ -79,132 +79,3 @@ 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)] - - -class SaleOrderLineAgent(models.Model): - _inherit = "sale.order.line.agent" - - @api.depends( - "object_id.price_subtotal", "object_id.product_id", "object_id.product_uom_qty" - ) - def _compute_amount(self): - for line in self: - if ( - line.commission_id - and line.commission_id.commission_type == "product_restricted" - ): - order_line = line.object_id - line.amount = line._get_single_commission_amount( - line.commission_id, - order_line.price_subtotal, - order_line.product_id, - order_line.product_uom_qty, - ) - else: - super(SaleOrderLineAgent, line)._compute_amount() - - def _get_single_commission_amount(self, commission, subtotal, product, quantity): - # Replaced to add pricelist condition. Original in sale.commission.line.mixin. - self.ensure_one() - item_ids = self._get_commission_items(commission, product) - partner = self.object_id.order_id.partner_id - if not item_ids: - return 0.0 - group_ids = partner.commission_item_agent_ids.filtered( - lambda x: x.agent_id == self.agent_id - ).mapped("group_ids") - ci_with_group_ids = self.env["commission.item"].search( - [ - ("group_id", "in", group_ids.ids), - ] - ) - item_ids = list(set(item_ids).intersection(ci_with_group_ids.ids)) - if not item_ids: - return 0.0 - # Check discount condition - commission_item = False - commission_items = self.env["commission.item"].browse(item_ids) - for commission_item in commission_items: - discount = self._get_discount_value(commission_item) - if commission_item.based_on != "sol": - if ( - commission_item.discount_from - <= discount - <= commission_item.discount_to - ): - break # suitable item found - else: - break # suitable item found - commission_item = False - if not commission_item: - # all commission items rejected - return 0.0 - if commission.amount_base_type == "net_amount": - # If subtotal (sale_price * quantity) is less than - # standard_price * quantity, it means that we are selling at - # lower price than we bought, so set amount_base to 0 - subtotal = max([0, subtotal - product.standard_price * quantity]) - self.applied_commission_item_id = commission_item - # if self.agent_id.use_multi_type_commissions: - self.applied_commission_id = commission_item.commission_id - if commission_item.commission_type == "fixed": - return commission_item.fixed_amount - elif commission_item.commission_type == "percentage": - return subtotal * (commission_item.percent_amount / 100.0) - - -class AccountInvoiceLineAgent(models.Model): - _inherit = "account.invoice.line.agent" - - @api.depends( - "object_id.price_subtotal", - "object_id.product_id.commission_free", - "commission_id", - ) - def _compute_amount(self): - for line in self: - if ( - line.commission_id - and line.commission_id.commission_type == "product_restricted" - ): - inv_line = line.object_id - line.amount = line._get_single_commission_amount( - line.commission_id, - inv_line.price_subtotal, - inv_line.product_id, - inv_line.quantity, - ) - else: - super(AccountInvoiceLineAgent, line)._compute_amount() - - def _get_single_commission_amount(self, commission, subtotal, product, quantity): - self.ensure_one() - partner = self.object_id.partner_id - item_ids = self._get_commission_items(commission, product) - if item_ids: - return 0.0 - # Main idea is here: - group_ids = partner.commission_item_agent_ids.filtered( - lambda x: x.agent_id == self.agent_id - ).mapped("group_ids") - ci_with_group_ids = self.env["commission.item"].search( - [ - ("group_id", "in", group_ids.ids), - ] - ) - item_ids = list(set(item_ids).intersection(ci_with_group_ids.ids)) - if item_ids: - return 0.0 - commission_item = self.env["commission.item"].browse(item_ids[0]) - if commission.amount_base_type == "net_amount": - # If subtotal (sale_price * quantity) is less than - # standard_price * quantity, it means that we are selling at - # lower price than we bought, so set amount_base to 0 - subtotal = max([0, subtotal - product.standard_price * quantity]) - self.applied_commission_item_id = commission_item - # if self.agent_id.use_multi_type_commissions: - self.applied_commission_id = commission_item.commission_id - if commission_item.commission_type == "fixed": - return commission_item.fixed_amount - elif commission_item.commission_type == "percentage": - return subtotal * (commission_item.percent_amount / 100.0) diff --git a/sale_commission_product_criteria_domain/models/sale_order_line_agent.py b/sale_commission_product_criteria_domain/models/sale_order_line_agent.py new file mode 100644 index 000000000..dd7cda0a2 --- /dev/null +++ b/sale_commission_product_criteria_domain/models/sale_order_line_agent.py @@ -0,0 +1,75 @@ +# © 2023 ooops404 +# License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0.html +from odoo import api, models + + +class SaleOrderLineAgent(models.Model): + _inherit = "sale.order.line.agent" + + @api.depends( + "object_id.price_subtotal", "object_id.product_id", "object_id.product_uom_qty" + ) + def _compute_amount(self): + for line in self: + if ( + line.commission_id + and line.commission_id.commission_type == "product_restricted" + ): + order_line = line.object_id + line.amount = line._get_single_commission_amount( + line.commission_id, + order_line.price_subtotal, + order_line.product_id, + order_line.product_uom_qty, + ) + else: + super(SaleOrderLineAgent, line)._compute_amount() + + def _get_single_commission_amount(self, commission, subtotal, product, quantity): + # Replaced to add pricelist condition. Original in sale.commission.line.mixin. + self.ensure_one() + item_ids = self._get_commission_items(commission, product) + partner = self.object_id.order_id.partner_id + if not item_ids: + return 0.0 + group_ids = partner.commission_item_agent_ids.filtered( + lambda x: x.agent_id == self.agent_id + ).mapped("group_ids") + ci_with_group_ids = self.env["commission.item"].search( + [ + ("group_id", "in", group_ids.ids), + ] + ) + item_ids = list(set(item_ids).intersection(ci_with_group_ids.ids)) + if not item_ids: + return 0.0 + # Check discount condition + commission_item = False + commission_items = self.env["commission.item"].browse(item_ids) + for commission_item in commission_items: + discount = self._get_discount_value(commission_item) + if commission_item.based_on != "sol": + if ( + commission_item.discount_from + <= discount + <= commission_item.discount_to + ): + break # suitable item found + else: + break # suitable item found + commission_item = False + if not commission_item: + # all commission items rejected + return 0.0 + if commission.amount_base_type == "net_amount": + # If subtotal (sale_price * quantity) is less than + # standard_price * quantity, it means that we are selling at + # lower price than we bought, so set amount_base to 0 + subtotal = max([0, subtotal - product.standard_price * quantity]) + self.applied_commission_item_id = commission_item + # if self.agent_id.use_multi_type_commissions: + self.applied_commission_id = commission_item.commission_id + if commission_item.commission_type == "fixed": + return commission_item.fixed_amount + elif commission_item.commission_type == "percentage": + return subtotal * (commission_item.percent_amount / 100.0) 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 b5462df16..cb310e675 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 @@ -187,6 +187,16 @@ def test_commission_domain(self): so.order_line.agent_ids._compute_amount() invoice.line_ids.agent_ids._compute_amount() + # computes was modified to consider new commission type: product_restricted + self.azure.agent_ids.commission_id = self.demo_commission_rules + so = self._create_sale_order(self.product_1, self.azure) + so.recompute_lines_agents() + so.action_confirm() + invoice = self._invoice_sale_order(so) + invoice.recompute_lines_agents() + so.order_line.agent_ids._compute_amount() + invoice.line_ids.agent_ids._compute_amount() + def _create_sale_order(self, product, partner): return self.sale_order_model.create( {