[IMP] nfu_ecommerce,nfu_sale_order_batch_packaging: use python rounding to get rid of buggy float_round

This commit is contained in:
Niels Göttsch 2024-10-07 22:18:01 +02:00
parent 847d34c889
commit 4128871c63
2 changed files with 5 additions and 8 deletions

View file

@ -1,5 +1,5 @@
from odoo import fields, models from odoo import fields, models
from odoo.tools import float_is_zero, float_repr, float_round from odoo.tools import float_is_zero, float_round
class SaleOrderLine(models.Model): class SaleOrderLine(models.Model):
@ -16,12 +16,7 @@ class SaleOrderLine(models.Model):
line.batch_uom_qty % line.product_packaging_id.qty, precision_rounding=precision_rounding line.batch_uom_qty % line.product_packaging_id.qty, precision_rounding=precision_rounding
) )
if not float_is_zero(qty_of_last_pack, precision_rounding=precision_rounding): if not float_is_zero(qty_of_last_pack, precision_rounding=precision_rounding):
open_qty = float_round( line.open_qty = round(line.product_packaging_id.qty - qty_of_last_pack, precision_digits)
line.product_packaging_id.qty - qty_of_last_pack, precision_rounding=precision_rounding
)
# somehow float_rounding() does not capture all cases ex. float_rounding(2-1.1)
# this is a samll workaround
line.open_qty = float(float_repr(open_qty, precision_digits))
else: else:
line.open_qty = 0.0 line.open_qty = 0.0
else: else:

View file

@ -28,13 +28,15 @@ class SaleOrderLine(models.Model):
if line.order_id.batch_id: if line.order_id.batch_id:
batch_id = line.batch_id batch_id = line.batch_id
product_id = line.product_id product_id = line.product_id
precision_rounding = line.product_id.uom_id.rounding
precision_digits = len(str(precision_rounding).split(".")[1])
batch_lines = ( batch_lines = (
self.env["sale.order.line"] self.env["sale.order.line"]
.sudo() .sudo()
.search([("batch_id", "=", batch_id.id), ("product_id", "=", product_id.id)]) .search([("batch_id", "=", batch_id.id), ("product_id", "=", product_id.id)])
) )
line.batch_uom_qty = sum(batch_lines.mapped("product_uom_qty")) line.batch_uom_qty = sum(batch_lines.mapped("product_uom_qty"))
line.batch_uom_max_qty = sum(batch_lines.mapped("product_uom_max_qty")) line.batch_uom_max_qty = round(sum(batch_lines.mapped("product_uom_max_qty")), precision_digits)
else: else:
line.batch_uom_max_qty = line.batch_uom_qty = 0 line.batch_uom_max_qty = line.batch_uom_qty = 0