[IMP] hr_commission: rename template field, refactor account

This commit is contained in:
Jared Kipe
2022-11-17 20:57:04 +00:00
parent df18d41eaa
commit 1b9a66086a
6 changed files with 32 additions and 42 deletions

View File

@@ -8,7 +8,7 @@
'license': 'OPL-1',
'website': 'https://hibou.io/',
'depends': [
'account_invoice_margin',
# 'account_invoice_margin', # optional
'account',
'hr_contract',
],

View File

@@ -39,19 +39,18 @@ class AccountMove(models.Model):
def amount_for_commission(self, commission=None):
# Override to exclude ineligible products
amount = 0.0
if self.is_invoice():
invoice_lines = self.invoice_line_ids.filtered(lambda l: not l.product_id.no_commission)
if self.company_id.commission_amount_type == 'on_invoice_margin':
margin_threshold = float(self.env['ir.config_parameter'].sudo().get_param('commission.margin.threshold', default=0.0))
if margin_threshold:
invoice_lines = invoice_lines.filtered(lambda l: l.get_margin_percent() > margin_threshold)
sign = -1 if self.move_type in ['in_refund', 'out_refund'] else 1
margin = sum(invoice_lines.mapped('margin'))
amount = margin * sign
else:
amount = sum(invoice_lines.mapped('balance'))
amount = abs(amount) if self.move_type == 'entry' else -amount
return amount
invoice_lines = self.invoice_line_ids.filtered(lambda l: not l.product_id.is_commission_exempt)
sign = -1 if self.move_type in ['in_refund', 'out_refund'] else 1
if hasattr(self, 'margin') and self.company_id.commission_amount_type == 'on_invoice_margin':
margin_threshold = float(self.env['ir.config_parameter'].sudo().get_param('commission.margin.threshold', 0.0))
if margin_threshold:
invoice_lines = invoice_lines.filtered(lambda l: l.get_margin_percent() > margin_threshold)
amount = sum(invoice_lines.mapped('margin'))
elif self.company_id.commission_amount_type == 'on_invoice_untaxed':
amount = sum(invoice_lines.mapped('price_subtotal'))
else:
amount = sum(invoice_lines.mapped('price_total'))
return amount * sign
def action_cancel(self):
res = super(AccountMove, self).action_cancel()
@@ -64,15 +63,6 @@ class AccountMoveLine(models.Model):
_inherit = 'account.move.line'
def get_margin_percent(self):
for line in self:
currency = line.move_id.currency_id
price = line.purchase_price
if line.product_id and not price:
date = line.move_id.date if line.move_id.date else fields.Date.context_today(line.move_id)
from_cur = line.move_id.company_currency_id.with_context(date=date)
price = from_cur._convert(line.product_id.standard_price, currency, line.company_id, date, round=False)
total_price = price * line.quantity
if total_price == 0.0:
return -1.0
else:
return (line.margin / total_price) * 100.0
if not self.price_subtotal:
return 0.0
return ((self.margin or 0.0) / self.price_subtotal) * 100.0

View File

@@ -4,4 +4,4 @@ from odoo import fields, models
class ProductTemplate(models.Model):
_inherit = 'product.template'
no_commission = fields.Boolean('Exclude from Commissions')
is_commission_exempt = fields.Boolean('Exclude from Commissions')

View File

@@ -1,4 +1,4 @@
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
from . import test_commission
from . import test_no_commission
from . import test_is_commission_exempt

View File

@@ -5,7 +5,7 @@ import logging
_logger = logging.getLogger(__name__)
class TestNoCommission(common.TransactionCase):
class TestIsCommissionExempt(common.TransactionCase):
def setUp(self):
super().setUp()
@@ -49,10 +49,10 @@ class TestNoCommission(common.TransactionCase):
'invoice_policy': 'order',
'taxes_id': [],
})
self.product_no_commission = self.env['product.product'].create({
self.product_is_commission_exempt = self.env['product.product'].create({
'name': 'Test Product No Commission',
'invoice_policy': 'order',
'no_commission': True,
'is_commission_exempt': True,
'taxes_id': [],
})
@@ -69,9 +69,9 @@ class TestNoCommission(common.TransactionCase):
'tax_id': False,
}), (0, 0, {
'name': 'test product no commission',
'product_id': self.product_no_commission.id,
'product_id': self.product_is_commission_exempt.id,
'product_uom_qty': 1.0,
'product_uom': self.product_no_commission.uom_id.id,
'product_uom': self.product_is_commission_exempt.uom_id.id,
'price_unit': 20.0,
'tax_id': False,
})]
@@ -79,12 +79,12 @@ class TestNoCommission(common.TransactionCase):
self.assertEqual(order.amount_total, 120.0)
return order
def test_00_no_commission_total(self):
def test_00_is_commission_exempt_total(self):
# TODO: test refunds
# New attribute
self.assertFalse(self.product.no_commission)
self.assertTrue(self.product_no_commission.no_commission)
self.assertFalse(self.product.is_commission_exempt)
self.assertTrue(self.product_is_commission_exempt.is_commission_exempt)
# Calculate commission based on invoice total
self.env.user.company_id.commission_amount_type = 'on_invoice_total'
@@ -105,7 +105,7 @@ class TestNoCommission(common.TransactionCase):
# commmission should be 10.0
self.assertEqual(user_commission.amount, 10.0)
def test_10_no_commission_margin(self):
def test_10_is_commission_exempt_margin(self):
self.env['ir.config_parameter'].set_param('commission.margin.threshold', '51.0')
low_margin_product = self.env['product.product'].create({
'name': 'Test Low Margin Product',
@@ -114,7 +114,7 @@ class TestNoCommission(common.TransactionCase):
})
self.env.user.company_id.commission_amount_type = 'on_invoice_margin'
self.product.standard_price = 50.0 # margin is 100%, margin = $50.0
self.product_no_commission.standard_price = 10.0 # margin is 100%
self.product_is_commission_exempt.standard_price = 10.0 # margin is 100%
sale = self._createSaleOrder()
sale.write({
@@ -147,7 +147,7 @@ class TestNoCommission(common.TransactionCase):
def test_20_test_zero_price(self):
self.env.user.company_id.commission_amount_type = 'on_invoice_margin'
self.product.standard_price = 0.0 # margin_percent is NaN
self.product_no_commission.standard_price = 10.0 # margin is 100%
self.product_is_commission_exempt.standard_price = 10.0 # margin is 100%
sale = self._createSaleOrder()
sale.action_confirm()

View File

@@ -8,8 +8,8 @@
<field name="arch" type="xml">
<xpath expr="//div[@name='options']" position="inside">
<span class="d-inline-block">
<field name="no_commission" readonly="1"/>
<label for="no_commission"/>
<field name="is_commission_exempt" readonly="1"/>
<label for="is_commission_exempt"/>
</span>
</xpath>
</field>
@@ -21,7 +21,7 @@
<field name="inherit_id" ref="product.product_template_form_view"/>
<field name="groups_id" eval="[(4, ref('account.group_account_user'))]"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='no_commission']" position="attributes">
<xpath expr="//field[@name='is_commission_exempt']" position="attributes">
<attribute name="readonly">0</attribute>
</xpath>
</field>