-
-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ADD] sale_commission_product_criteria_domain #434
Merged
OCA-git-bot
merged 3 commits into
OCA:14.0
from
ilyasProgrammer:14.0-sale_commission_product_criteria_domain
Aug 22, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
132
sale_commission_product_criteria_domain/demo/demo_data.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
28 changes: 28 additions & 0 deletions
28
sale_commission_product_criteria_domain/models/account_invoice_line_agent.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
81
sale_commission_product_criteria_domain/models/commission.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
70
sale_commission_product_criteria_domain/models/commission_group.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)], | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better use
write
function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@geomer198 why ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this solution is possible.