Skip to content

Commit

Permalink
wip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasProgrammer committed Aug 14, 2023
1 parent dec1254 commit 695130e
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 129 deletions.
2 changes: 2 additions & 0 deletions sale_commission_product_criteria_domain/models/__init__.py
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
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(

Check warning on line 40 in sale_commission_product_criteria_domain/models/account_invoice_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/account_invoice_line_agent.py#L40

Added line #L40 was not covered by tests
[
("group_id", "in", group_ids.ids),
]
)
item_ids = list(set(item_ids).intersection(ci_with_group_ids.ids))

Check warning on line 45 in sale_commission_product_criteria_domain/models/account_invoice_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/account_invoice_line_agent.py#L45

Added line #L45 was not covered by tests
if item_ids:
return 0.0
commission_item = self.env["commission.item"].browse(item_ids[0])

Check warning on line 48 in sale_commission_product_criteria_domain/models/account_invoice_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/account_invoice_line_agent.py#L47-L48

Added lines #L47 - L48 were not covered by tests
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

Check warning on line 54 in sale_commission_product_criteria_domain/models/account_invoice_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/account_invoice_line_agent.py#L53-L54

Added lines #L53 - L54 were not covered by tests
# if self.agent_id.use_multi_type_commissions:
self.applied_commission_id = commission_item.commission_id

Check warning on line 56 in sale_commission_product_criteria_domain/models/account_invoice_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/account_invoice_line_agent.py#L56

Added line #L56 was not covered by tests
if commission_item.commission_type == "fixed":
return commission_item.fixed_amount

Check warning on line 58 in sale_commission_product_criteria_domain/models/account_invoice_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/account_invoice_line_agent.py#L58

Added line #L58 was not covered by tests
elif commission_item.commission_type == "percentage":
return subtotal * (commission_item.percent_amount / 100.0)

Check warning on line 60 in sale_commission_product_criteria_domain/models/account_invoice_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/account_invoice_line_agent.py#L60

Added line #L60 was not covered by tests
129 changes: 0 additions & 129 deletions sale_commission_product_criteria_domain/models/commission.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,132 +79,3 @@ def _compute_agent_group_ids(self):
[("commission_id", "=", rec.agent_id.commission_id.id), dom]
)
rec.agent_group_ids = [(6, 0, items.mapped("group_id").ids)]


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)


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)
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

Check warning on line 57 in sale_commission_product_criteria_domain/models/sale_order_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/sale_order_line_agent.py#L57

Added line #L57 was not covered by tests
else:
break # suitable item found

Check warning on line 59 in sale_commission_product_criteria_domain/models/sale_order_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/sale_order_line_agent.py#L59

Added line #L59 was not covered by tests
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

Check warning on line 69 in sale_commission_product_criteria_domain/models/sale_order_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/sale_order_line_agent.py#L68-L69

Added lines #L68 - L69 were not covered by tests
# if self.agent_id.use_multi_type_commissions:
self.applied_commission_id = commission_item.commission_id

Check warning on line 71 in sale_commission_product_criteria_domain/models/sale_order_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/sale_order_line_agent.py#L71

Added line #L71 was not covered by tests
if commission_item.commission_type == "fixed":
return commission_item.fixed_amount

Check warning on line 73 in sale_commission_product_criteria_domain/models/sale_order_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/sale_order_line_agent.py#L73

Added line #L73 was not covered by tests
elif commission_item.commission_type == "percentage":
return subtotal * (commission_item.percent_amount / 100.0)

Check warning on line 75 in sale_commission_product_criteria_domain/models/sale_order_line_agent.py

View check run for this annotation

Codecov / codecov/patch

sale_commission_product_criteria_domain/models/sale_order_line_agent.py#L75

Added line #L75 was not covered by tests
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ def test_commission_domain(self):
so.order_line.agent_ids._compute_amount()
invoice.line_ids.agent_ids._compute_amount()

# computes was modified to consider new commission type: product_restricted
self.azure.agent_ids.commission_id = self.demo_commission_rules
so = self._create_sale_order(self.product_1, self.azure)
so.recompute_lines_agents()
so.action_confirm()
invoice = self._invoice_sale_order(so)
invoice.recompute_lines_agents()
so.order_line.agent_ids._compute_amount()
invoice.line_ids.agent_ids._compute_amount()

def _create_sale_order(self, product, partner):
return self.sale_order_model.create(
{
Expand Down

0 comments on commit 695130e

Please sign in to comment.