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 Jun 7, 2023
1 parent f001fa1 commit 2996e54
Show file tree
Hide file tree
Showing 13 changed files with 296 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
20 changes: 20 additions & 0 deletions sale_commission_product_criteria_domain/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# © 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"],
"data": [
"views/views.xml",
"security/ir.model.access.csv",
],
"application": False,
"installable": True,
"auto_install": False,
}
2 changes: 2 additions & 0 deletions sale_commission_product_criteria_domain/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import commission
from . import partner
104 changes: 104 additions & 0 deletions sale_commission_product_criteria_domain/models/commission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# © 2023 ooops404
# License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0.html
from odoo import api, fields, models


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

group_id = fields.Many2one("commission.items.group")


class CommissionItemsGroup(models.Model):
_name = "commission.items.group"
_description = "Commission Items Group"

name = fields.Char(required=True)
commission_id = fields.Many2one("sale.commission", required=True)


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)]',
)
partner_id = fields.Many2one(
"res.partner",
domain=[("agent", "=", False)],
)
group_ids = fields.Many2many(
"commission.items.group",
domain="[('id', 'in', agent_group_ids)]",
string="Commission Items Groups Restrictions",
)

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


class SaleCommissionLineMixin(models.AbstractModel):
_inherit = "sale.commission.line.mixin"

def _get_single_commission_amount(self, commission, subtotal, product, quantity):
self.ensure_one()
partner = False
if self.object_id._name == "account.move.line":
partner = self.object_id.partner_id
elif self.object_id._name == "sale.order.line":
partner = self.object_id.order_id.partner_id
if not partner or not partner.apply_commission_restrictions:
return super()._get_single_commission_amount(
commission, subtotal, product, quantity
)
item_ids = self._get_commission_items(commission, product)
if len(item_ids) == 0:
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 = [item for item in item_ids if item in ci_with_group_ids.ids]
if len(item_ids) == 0:
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)
12 changes: 12 additions & 0 deletions sale_commission_product_criteria_domain/models/partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# © 2023 ooops404
# License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0.html
from odoo import fields, models


class ResPartner(models.Model):
_inherit = "res.partner"

apply_commission_restrictions = fields.Boolean("Apply Restrictions")
commission_item_agent_ids = fields.One2many(
"commission.item.agent", "partner_id", string="Commission Items Groups"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Ooops404 <https://www.ooops404.com>`__:

* Ilyas <[email protected]>
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
cia1,cia1,model_commission_item_agent,sales_team.group_sale_manager,1,1,1,1
cia2,cia2,model_commission_item_agent,sales_team.group_sale_salesman,1,1,0,0
cig1,cig1,model_commission_items_group,sales_team.group_sale_manager,1,1,1,1
cig2,cig2,model_commission_items_group,sales_team.group_sale_salesman,1,1,0,0
142 changes: 142 additions & 0 deletions sale_commission_product_criteria_domain/views/views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="view_partner_form_agent_inherit" model="ir.ui.view">
<field name="name">res.partner.form.agent.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="sale_commission.view_partner_form_agent" />
<field name="arch" type="xml">
<field name="agent_ids" position="after">
<field name="apply_commission_restrictions" />
<field
name="commission_item_agent_ids"
attrs="{'invisible': [('apply_commission_restrictions', '=', False)]}"
/>
</field>
</field>
</record>

<record id="commission_item_agent_form" model="ir.ui.view">
<field name="name">commission.item.agent.form</field>
<field name="model">commission.item.agent</field>
<field name="priority" eval="1" />
<field name="arch" type="xml">
<form>
<sheet>
<group>
<group>
<field name="agent_id" />
<field name="group_ids" widget="many2many_tags" />
<field name="agent_group_ids" invisible="1" />
<field name="partner_agent_ids" invisible="1" />
<field name="partner_id" invisible="1" />
</group>
</group>
</sheet>
</form>
</field>
</record>

<record id="commission_item_agent_tree" model="ir.ui.view">
<field name="name">commission.item.agent.tree</field>
<field name="model">commission.item.agent</field>
<field name="priority" eval="1" />
<field name="arch" type="xml">
<tree string="Agents Items Restriction">
<field name="agent_id" />
<field name="group_ids" widget="many2many_tags" />
<field name="agent_group_ids" invisible="1" />
<field name="partner_agent_ids" invisible="1" />
<field name="partner_id" invisible="1" />
</tree>
</field>
</record>

<record id="commission_item_tree_view_inherit" model="ir.ui.view">
<field name="name">commission.item.tree.inherit</field>
<field name="model">commission.item</field>
<field
name="inherit_id"
ref="sale_commission_product_criteria.commission_item_tree_view"
/>
<field name="arch" type="xml">
<field name="based_on" position="after">
<field name="group_id" />
</field>
</field>
</record>

<record id="commission_item_form_view_inherit" model="ir.ui.view">
<field name="name">commission.item.form.inherit</field>
<field name="model">commission.item</field>
<field
name="inherit_id"
ref="sale_commission_product_criteria.commission_item_form_view"
/>
<field name="arch" type="xml">
<field name="commission_id" position="after">
<field name="group_id" />
</field>
</field>
</record>

<record id="sale_commission_form_lines_mod_inherit" model="ir.ui.view">
<field name="name">sale.commission.form.view.inherit</field>
<field name="model">sale.commission</field>
<field
name="inherit_id"
ref="sale_commission_product_criteria.sale_commission_form_lines_mod"
/>
<field name="arch" type="xml">
<field name="based_on" position="before">
<field name="group_id" />
</field>
</field>
</record>

<record id="commission_items_group_form" model="ir.ui.view">
<field name="name">commission.items.group.form</field>
<field name="model">commission.items.group</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<group>
<field name="name" />
<field name="commission_id" />
</group>
</group>
</sheet>
</form>
</field>
</record>

<record id="commission_items_group_tree" model="ir.ui.view">
<field name="name">commission.items.group.tree</field>
<field name="model">commission.items.group</field>
<field name="arch" type="xml">
<tree string="Commission Type Items Groups">
<field name="name" />
<field name="commission_id" />
</tree>
</field>
</record>

<record id="commission_items_group_tree_action" model="ir.actions.act_window">
<field name="name">Commission Type Items Groups</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">commission.items.group</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="commission_items_group_tree" />
</record>

<menuitem
name="Commission Type Items Groups"
id="menu_commission_items_group"
action="commission_items_group_tree_action"
parent="sale_commission.menu_sale_commissions_management"
groups="sales_team.group_sale_manager"
sequence="10"
/>

</odoo>
6 changes: 6 additions & 0 deletions setup/sale_commission_product_criteria_domain/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 2996e54

Please sign in to comment.