-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dec1254
commit 695130e
Showing
5 changed files
with
147 additions
and
129 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
60 changes: 60 additions & 0 deletions
60
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,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) | ||
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
75 changes: 75 additions & 0 deletions
75
sale_commission_product_criteria_domain/models/sale_order_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,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) | ||
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