mirror of
https://gitlab.com/hibou-io/hibou-odoo/suite.git
synced 2025-01-20 12:37:31 +02:00
[FIX] hr_commission: refactor margin_percent field to method
H11044
This commit is contained in:
@@ -44,7 +44,7 @@ class AccountMove(models.Model):
|
|||||||
if self.company_id.commission_amount_type == 'on_invoice_margin':
|
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))
|
margin_threshold = float(self.env['ir.config_parameter'].sudo().get_param('commission.margin.threshold', default=0.0))
|
||||||
if margin_threshold:
|
if margin_threshold:
|
||||||
invoice_lines = invoice_lines.filtered(lambda l: l.margin_percent > 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
|
sign = -1 if self.move_type in ['in_refund', 'out_refund'] else 1
|
||||||
margin = sum(invoice_lines.mapped('margin'))
|
margin = sum(invoice_lines.mapped('margin'))
|
||||||
amount = margin * sign
|
amount = margin * sign
|
||||||
@@ -63,10 +63,7 @@ class AccountMove(models.Model):
|
|||||||
class AccountMoveLine(models.Model):
|
class AccountMoveLine(models.Model):
|
||||||
_inherit = 'account.move.line'
|
_inherit = 'account.move.line'
|
||||||
|
|
||||||
margin_percent = fields.Float(string='Margin percent (%)', compute='_compute_margin_percent', digits=(3, 2))
|
def get_margin_percent(self):
|
||||||
|
|
||||||
@api.depends('margin', 'product_id', 'purchase_price', 'quantity', 'price_unit', 'price_subtotal')
|
|
||||||
def _compute_margin_percent(self):
|
|
||||||
for line in self:
|
for line in self:
|
||||||
currency = line.move_id.currency_id
|
currency = line.move_id.currency_id
|
||||||
price = line.purchase_price
|
price = line.purchase_price
|
||||||
@@ -76,6 +73,6 @@ class AccountMoveLine(models.Model):
|
|||||||
price = from_cur._convert(line.product_id.standard_price, currency, line.company_id, date, round=False)
|
price = from_cur._convert(line.product_id.standard_price, currency, line.company_id, date, round=False)
|
||||||
total_price = price * line.quantity
|
total_price = price * line.quantity
|
||||||
if total_price == 0.0:
|
if total_price == 0.0:
|
||||||
line.margin_percent = -1.0
|
return -1.0
|
||||||
else:
|
else:
|
||||||
line.margin_percent = (line.margin / total_price) * 100.0
|
return (line.margin / total_price) * 100.0
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ class TestNoCommission(common.TransactionCase):
|
|||||||
sale.action_confirm()
|
sale.action_confirm()
|
||||||
self.assertIn(sale.state, ('sale', 'done'), 'Could not confirm, maybe archive exception rules.')
|
self.assertIn(sale.state, ('sale', 'done'), 'Could not confirm, maybe archive exception rules.')
|
||||||
inv = sale._create_invoices()
|
inv = sale._create_invoices()
|
||||||
self.assertEqual(inv.invoice_line_ids.mapped('margin_percent'), [100.0, 100.0, 1.0])
|
self.assertEqual(inv.invoice_line_ids.mapped(lambda l: l.get_margin_percent()), [100.0, 100.0, 1.0])
|
||||||
self.assertFalse(inv.commission_ids, 'Commissions exist when invoice is created.')
|
self.assertFalse(inv.commission_ids, 'Commissions exist when invoice is created.')
|
||||||
inv.action_post()
|
inv.action_post()
|
||||||
self.assertTrue(inv.commission_ids, 'Commissions not created when invoice is validated.')
|
self.assertTrue(inv.commission_ids, 'Commissions not created when invoice is validated.')
|
||||||
@@ -153,4 +153,4 @@ class TestNoCommission(common.TransactionCase):
|
|||||||
sale.action_confirm()
|
sale.action_confirm()
|
||||||
self.assertIn(sale.state, ('sale', 'done'), 'Could not confirm, maybe archive exception rules.')
|
self.assertIn(sale.state, ('sale', 'done'), 'Could not confirm, maybe archive exception rules.')
|
||||||
inv = sale._create_invoices()
|
inv = sale._create_invoices()
|
||||||
self.assertEqual(inv.invoice_line_ids.mapped('margin_percent'), [-1.0, 100.0])
|
self.assertEqual(inv.invoice_line_ids.mapped(lambda l: l.get_margin_percent()), [-1.0, 100.0])
|
||||||
|
|||||||
Reference in New Issue
Block a user