Skip to Content
Menu
This question has been flagged
2 Replies
1388 Views

class PurchaseOrderInherit(models.Model):

     _inherit = "purchase.order.line"

     margin = fields.Float("Margin %", compute="_compute_margin",

inverse="_inverse_margin", digits='Product Price', store=True)

     mrp = fields.Float("MRP", help="Load from Product MRP(Latest)",

digits='Product Price')

     @api.onchange('product_id')

     def _compute_mrp(self):

         for rec in self:

             if rec.product_id:

                 rec.mrp = rec.product_id.lst_price

             else:

                 rec.mrp = False

     # Get Margin

     @api.depends("mrp", "product_qty", "price_subtotal", "product_id")

     def _compute_margin(self):

         for rec in self:

             if rec.mrp:

                 price_sub_mrp = self._get_tax_amount(rec.product_id,

rec.mrp)

                 margin = (price_sub_mrp * rec.product_qty) -

rec.price_subtotal

                 rec.margin = price_sub_mrp and margin / price_sub_mrp

             else:

                 rec.margin = False

     @api.onchange('margin')

     def _inverse_margin(self):

         for rec in self:

             if rec.order_id.mrp_updatable:

                 cost = rec.product_qty and rec.price_total /

rec.product_qty

                 rec.mrp = cost / (1 - rec.margin)

             else:

                 cost = rec.mrp * (1 - rec.margin)

                 cost_tax = self._get_tax_amount(rec.product_id, cost)

                 if rec.discount:

                     price_unit = cost_tax + cost_tax * (rec.discount / 100)

                 else:

                     price_unit = cost_tax

                 rec.price_unit = price_unit

     # Get price excluding tax amount

     def _get_tax_amount(self, product, price):

         taxes_id = product.taxes_id.filtered(

             lambda x: x.company_id.id == self.env.company.id

             or x.company_id.id == self.env.company.parent_id.id

         )

         if taxes_id:

             amount = taxes_id.compute_all(

                 price, product=product, partner=self.env["res.partner"]

             )

             amount = amount["total_excluded"]

         else:

             amount = price

         return amount

This is a custom code I have developed for purchase

It calculates the margin% for purchase and I have set a inverse method for margin so when ever I change the margin based on a Boolean(mrp_updateable) field it revere mrp or price_unit

But when I check this will results to some decimal point differences

If I change margin as 20 then when I save it shows 19.99 and if I change discount it changes price_unit

Can anyone review the code and help me

Thanks in advance..

Avatar
Discard
Best Answer

Hello Mad Max ,

    

    The issue you are facing with the slight decimal point differences when changing the margin and saving 

    could be due to several factors such as precision errors in floating-point arithmetic or how Odoo handles rounding. 

    

    Here are a few steps to ensure your calculations are as precise as possible:


    1) Ensure Precision in Float Fields:

    Make sure the fields involved in the calculations (like margin, mrp, price_unit, etc.) have the appropriate precision.


    2) Use Rounding:

    Explicitly round your values to avoid precision issues when performing arithmetic operations.


    3) Refactor Calculation Logic:

    Ensure your calculation logic is consistent and uses precise arithmetic.


    Let's go through your code and make some adjustments to improve precision and clarity.


   //Code in comment//

   Hope this helps.

    

    If you need any help in customization feel free to contact us.


Thanks & Regards,


Email:  odoo@aktivsoftware.com           


Skype: kalpeshmaheshwari  

Avatar
Discard

Code:

from odoo import api, fields, models
import math

class PurchaseOrderLineInherit(models.Model):
_inherit = "purchase.order.line"

margin = fields.Float("Margin %", compute="_compute_margin",
inverse="_inverse_margin", digits='Product Price', store=True)
mrp = fields.Float("MRP", help="Load from Product MRP (Latest)",
digits='Product Price')

@api.onchange('product_id')
def _compute_mrp(self):
for rec in self:
rec.mrp = rec.product_id.lst_price if rec.product_id else False

# Get Margin
@api.depends("mrp", "product_qty", "price_subtotal", "product_id")
def _compute_margin(self):
for rec in self:
if rec.mrp and rec.product_qty:
price_sub_mrp = self._get_tax_amount(rec.product_id, rec.mrp)
margin = (price_sub_mrp * rec.product_qty) - rec.price_subtotal
rec.margin = round(price_sub_mrp and margin / price_sub_mrp, 2)
else:
rec.margin = False

@api.onchange('margin')
def _inverse_margin(self):
for rec in self:
if rec.product_qty:
if rec.order_id.mrp_updatable:
cost = rec.price_total / rec.product_qty
rec.mrp = round(cost / (1 - rec.margin), 2) if rec.margin != 1 else cost
else:
cost = rec.mrp * (1 - rec.margin)
cost_tax = self._get_tax_amount(rec.product_id, cost)
price_unit = cost_tax + (cost_tax * (rec.discount / 100) if rec.discount else 0)
rec.price_unit = round(price_unit, 2)

# Get price excluding tax amount
def _get_tax_amount(self, product, price):
taxes_id = product.taxes_id.filtered(
lambda x: x.company_id.id == self.env.company.id
or x.company_id.id == self.env.company.parent_id.id
)

if taxes_id:
amount = taxes_id.compute_all(price, product=product, partner=self.env["res.partner"])
amount = amount["total_excluded"]
else:
amount = price

return amount

Author

Thanks for the effort.
but no luck for me.

Best Answer

Add Import for float_round and float_compare:

from odoo.tools.float_utils import float_round, float_compare

Update _compute_margin to use float_round:

rec.margin = float_round(rec.margin, precision_digits=rec._get_digits('Product Price'))

Update _inverse_margin to use float_round:

rec.mrp = float_round(rec.mrp, precision_digits=rec._ge(price_unit, precision_digits=rec._get_digits('Product Price'))

Add Helper Method _get_digits to fetch precision:

def _get_digits(self, precision_name):
    """Get the number of digits for the given precision."""
    return self.env['decimal.precision'].precision_get(precision_name)

Avatar
Discard
Author

Thanks for the answer, but nothing changes.

Related Posts Replies Views Activity
2
Mar 15
4882
0
Sep 23
1679
0
Aug 16
3859
0
Mar 15
4575
0
Jul 24
3437