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 22, 2023
1 parent 14f9757 commit 2fe4eec
Show file tree
Hide file tree
Showing 19 changed files with 1,080 additions and 0 deletions.
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",
"web_domain_field",
],
"demo": [
"demo/demo_data.xml",
],
"data": [
"views/views.xml",
"security/ir.model.access.csv",
],
"application": False,
"installable": True,
"auto_install": False,
}
132 changes: 132 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,132 @@
<?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>

<!-- Agents -->
<record id="demo_agent_rules_restricted_italy" model="res.partner">
<field name="name">Agent Rules Restricted Italy</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>
<record id="demo_agent_rules_restricted_spain" model="res.partner">
<field name="name">Agent Rules Restricted Spain</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_spain')])]"
/>
</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_spain')])]"
/>
</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_spain" />
</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_italy')])]"
/>
</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_italy"
/>
</record>

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


class CommissionItemsGroup(models.Model):
_name = "commission.items.group"
_description = "Commission Items Group"
_sql_constraints = [
(
"unique_cig_name",
"UNIQUE(name)",
"Commission items group with such name already exists. "
"Name must be unique.",
)
]

name = fields.Char(required=True)
commission_ids = fields.Many2many(
"sale.commission",
compute="_compute_commission_ids",
domain=[("commission_type", "=", "product_restricted")],
readonly=True,
store=True,
)
item_ids = fields.One2many(
"commission.item", "group_id", string="Items", readonly=True
)
agents_count = fields.Integer(compute="_compute_agents_count")

@api.depends("item_ids")
def _compute_commission_ids(self):
for rec in self:
rec.commission_ids = [(6, 0, rec.item_ids.mapped("commission_id").ids)]

def unlink(self):
if self.item_ids:
raise exceptions.ValidationError(
_(
"You can not delete this commission group since "
"there is related to it commission items."
)
)
return super().unlink()

def _compute_agents_count(self):
res_partner_obj = self.env["res.partner"]
for rec in self:
self.agents_count = res_partner_obj.search_count(
[
("agent", "=", True),
("allowed_commission_group_ids", "in", rec.ids),
]
)

def action_open_related_agents(self):
agent_ids = self.env["res.partner"].search(
[
("agent", "=", True),
("allowed_commission_group_ids", "in", self.ids),
]
)
return {
"name": _("Commission Group Agents"),
"type": "ir.actions.act_window",
"view_mode": "tree",
"res_model": "res.partner",
"context": self.env.context,
"domain": [("id", "in", agent_ids.ids)],
}
Loading

0 comments on commit 2fe4eec

Please sign in to comment.