Skip to content

Commit

Permalink
[ADD] sale_commission_product_criteria_domain
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasProgrammer committed Aug 16, 2023
1 parent 2126be8 commit d4d4069
Show file tree
Hide file tree
Showing 21 changed files with 1,103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _get_commission_items(self, commission, product):
AND (item.commission_id = %s)
AND (item.active = TRUE)
ORDER BY
item.applied_on, categ.complete_name desc
item.applied_on, item.based_on, categ.complete_name desc
""",
(
product.product_tmpl_id.ids,
Expand Down
Empty file.
1 change: 1 addition & 0 deletions sale_commission_product_criteria_domain/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
26 changes: 26 additions & 0 deletions sale_commission_product_criteria_domain/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# © 2023 ooops404
# License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0.html
{
"name": "Sale Commission Product Criteria Domain",
"version": "14.0.1.0.0",
"author": "Ilyas," "Ooops404," "Odoo Community Association (OCA)",
"contributors": ["Ilyas"],
"maintainers": ["ilyasProgrammer"],
"website": "https://github.com/OCA/commission",
"category": "Sales Management",
"license": "AGPL-3",
"depends": [
"sale_commission_product_criteria_discount",
"web_domain_field",
],
"demo": [
"demo/demo_data.xml",
],
"data": [
"views/views.xml",
"security/ir.model.access.csv",
],
"application": False,
"installable": True,
"auto_install": False,
}
154 changes: 154 additions & 0 deletions sale_commission_product_criteria_domain/demo/demo_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?xml version="1.0" ?>
<odoo>
<!-- Commissions -->
<record id="demo_commission_rules_restrict" model="sale.commission">
<field name="name">Based on Rules Restricted</field>
<field name="commission_type">product_restricted</field>
</record>

<!-- Commission Groups -->
<record id="demo_cig_spain" model="commission.items.group">
<field name="name">Spain</field>
<field
name="commission_ids"
eval="[(6, 0, [ref('sale_commission_product_criteria_domain.demo_commission_rules_restrict')])]"
/>
</record>

<record id="demo_cig_italy" model="commission.items.group">
<field name="name">Italy</field>
<field
name="commission_ids"
eval="[(6, 0, [ref('sale_commission_product_criteria_domain.demo_commission_rules_restrict')])]"
/>
</record>

<!-- Commission Items -->
<record id="demo_crr_item_1" model="commission.item">
<field name="commission_id" ref="demo_commission_rules_restrict" />
<field name="sequence" eval="1" />
<field name="based_on">sol</field>
<field name="applied_on">3_global</field>
<field name="commission_type">fixed</field>
<field name="fixed_amount">10</field>
<field name="group_id" ref="demo_cig_italy" />
</record>

<record id="demo_crr_item_2" model="commission.item">
<field name="commission_id" ref="demo_commission_rules_restrict" />
<field name="sequence" eval="2" />
<field name="based_on">sol</field>
<field name="applied_on">2_product_category</field>
<field name="commission_type">fixed</field>
<field name="fixed_amount">20</field>
<field name="categ_id" ref="product.product_category_5" />
<field name="group_id" ref="demo_cig_spain" />
</record>

<record id="demo_crr_item_3" model="commission.item">
<field name="commission_id" ref="demo_commission_rules_restrict" />
<field name="sequence" eval="3" />
<field name="based_on">sol</field>
<field name="applied_on">1_product</field>
<field name="commission_type">percentage</field>
<field name="percent_amount">5</field>
<field
name="product_tmpl_id"
ref="product.product_product_4_product_template"
/>
<field name="group_id" ref="demo_cig_spain" />
</record>

<record id="demo_crr_item_4" model="commission.item">
<field name="commission_id" ref="demo_commission_rules_restrict" />
<field name="sequence" eval="4" />
<field name="based_on">sol</field>
<field name="applied_on">0_product_variant</field>
<field name="commission_type">percentage</field>
<field name="percent_amount">15</field>
<field name="product_id" ref="product.product_product_4" />
<field name="group_id" ref="demo_cig_spain" />
</record>

<record id="demo_crr_item_5_disc" model="commission.item">
<field name="commission_id" ref="demo_commission_rules_restrict" />
<field name="sequence" eval="1" />
<field name="based_on">discount</field>
<field name="applied_on">1_product</field>
<field name="commission_type">fixed</field>
<field name="fixed_amount">7.50</field>
<field name="discount_from">10.01</field>
<field name="discount_to">100</field>
<field
name="product_tmpl_id"
ref="product.product_product_11_product_template"
/>
<field name="group_id" ref="demo_cig_italy" />
</record>

<record id="demo_crr_item_6_disc" model="commission.item">
<field name="commission_id" ref="demo_commission_rules_restrict" />
<field name="sequence" eval="2" />
<field name="based_on">discount</field>
<field name="applied_on">1_product</field>
<field name="commission_type">fixed</field>
<field name="fixed_amount">15</field>
<field name="discount_from">0</field>
<field name="discount_to">10</field>
<field
name="product_tmpl_id"
ref="product.product_product_1_product_template"
/>
<field name="group_id" ref="demo_cig_spain" />
</record>

<!-- Agents -->
<record id="demo_agent_rules_restricted" model="res.partner">
<field name="name">Agent Rules Restricted</field>
<field name="is_company">True</field>
<field name="agent">True</field>
<field name="commission_id" ref="demo_commission_rules_restrict" />
<field
name="allowed_commission_group_ids"
eval="[(6, 0, [ref('sale_commission_product_criteria_domain.demo_cig_italy')])]"
/>
</record>

<!-- Azure -->
<record id="base.res_partner_12" model="res.partner">
<field name="apply_commission_restrictions" eval="True" />
<field
name="agent_ids"
eval="[(6, 0, [ref('sale_commission_product_criteria_domain.demo_agent_rules_restricted')])]"
/>
</record>
<record id="cia_azure" model="commission.item.agent">
<field name="partner_id" ref="base.res_partner_12" />
<field
name="group_ids"
eval="[(6, 0, [ref('sale_commission_product_criteria_domain.demo_cig_spain')])]"
/>
<field name="agent_id" ref="demo_agent_rules_restricted" />
</record>

<!-- Deco -->
<record id="base.res_partner_2" model="res.partner">
<field name="apply_commission_restrictions" eval="True" />
<field
name="agent_ids"
eval="[(6, 0, [ref('sale_commission_product_criteria_domain.demo_agent_rules_restricted')])]"
/>
</record>
<record id="cia_deco" model="commission.item.agent">
<field name="partner_id" ref="base.res_partner_2" />
<field
name="group_ids"
eval="[(6, 0, [ref('sale_commission_product_criteria_domain.demo_cig_italy')])]"
/>
<field
name="agent_id"
ref="sale_commission_product_criteria_domain.demo_agent_rules_restricted"
/>
</record>

</odoo>
5 changes: 5 additions & 0 deletions sale_commission_product_criteria_domain/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import commission
from . import commission_group
from . import partner
from . import account_invoice_line_agent
from . import sale_order_line_agent
Original file line number Diff line number Diff line change
@@ -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 not 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 not 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)
81 changes: 81 additions & 0 deletions sale_commission_product_criteria_domain/models/commission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# © 2023 ooops404
# License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0.html
from odoo import api, fields, models


class SaleCommission(models.Model):
_inherit = "sale.commission"

commission_type = fields.Selection(
selection_add=[("product_restricted", "Product criteria (with restrictions)")],
ondelete={"product_restricted": "set default"},
)


class CommissionItem(models.Model):
_inherit = "commission.item"

commission_id = fields.Many2one(
"sale.commission",
string="Commission Type",
domain=[("commission_type", "in", ["product", "product_restricted"])],
required=True,
)
sale_commission_type = fields.Selection(
related="commission_id.commission_type", readonly=True
)
group_id = fields.Many2one(
"commission.items.group",
ondelete="restrict",
)

def write(self, values):
res = super().write(values)
if self.group_id and not self.group_id.commission_ids.ids:
self.group_id.commission_ids = [(6, 0, self.commission_id.ids)]
if self.commission_id.commission_type != "product_restricted" and self.group_id:
self.group_id = False
return res


class CommissionItemAgent(models.Model):
_name = "commission.item.agent"
_description = "Commission Item Agent"

_sql_constraints = [
(
"commission_item_unique_agent",
"UNIQUE(partner_id, agent_id)",
"You can only add one time each agent into Commission "
"Items Groups Restrictions table.",
)
]

partner_agent_ids = fields.Many2many(related="partner_id.agent_ids")
agent_group_ids = fields.Many2many(
"commission.items.group", compute="_compute_agent_group_ids"
)
agent_id = fields.Many2one(
"res.partner", domain='[("id", "in", partner_agent_ids)]', required=True
)
partner_id = fields.Many2one(
"res.partner", domain=[("agent", "=", False)], required=True
)
group_ids = fields.Many2many(
"commission.items.group",
domain="[('id', 'in', agent_group_ids)]",
string="Commission Items Groups Restrictions",
required=True,
)

@api.depends("agent_id")
def _compute_agent_group_ids(self):
for rec in self:
if rec.agent_id.allowed_commission_group_ids:
dom = ("group_id", "in", rec.agent_id.allowed_commission_group_ids.ids)
else:
dom = ("group_id", "!=", False)
items = self.env["commission.item"].search(
[("commission_id", "=", rec.agent_id.commission_id.id), dom]
)
rec.agent_group_ids = [(6, 0, items.mapped("group_id").ids)]
Loading

0 comments on commit d4d4069

Please sign in to comment.