From 2d5ab66dec9ae8b7d6cbd49d4ae958a8b64d7dd5 Mon Sep 17 00:00:00 2001 From: nie Date: Fri, 5 Feb 2021 12:13:24 +0000 Subject: [PATCH] [FIX] stock_account: total quantity can be zero Follow-up of https://github.com/odoo/odoo/pull/64942 Explanation: When computing the total quantity, summing negative and positive values can sometimes lead to zero even if the scoped quantity is not zero. E.g.: Warehouse 1 has 10 items and Warehouse 2 has -10 items. Scoping on Warehouse 1 results in 10 items but the total quantity is 0. This commit also takes into account float precision. Inspired by: https://github.com/odoo/odoo/blob/d9b409247303c6b165a7df381169f2480a19900f/addons/stock_account/models/stock_quant.py#L36-L38 closes odoo/odoo#65661 X-original-commit: 3547b15cb362f73c60354070da5e49ffa674e432 Signed-off-by: backspac --- addons/stock_account/report/report_stock_forecasted.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/addons/stock_account/report/report_stock_forecasted.py b/addons/stock_account/report/report_stock_forecasted.py index 496e1c5085b..6742b091812 100644 --- a/addons/stock_account/report/report_stock_forecasted.py +++ b/addons/stock_account/report/report_stock_forecasted.py @@ -2,7 +2,7 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo import models -from odoo.tools.float_utils import float_repr +from odoo.tools.float_utils import float_is_zero, float_repr class ReplenishmentReport(models.AbstractModel): @@ -15,9 +15,10 @@ class ReplenishmentReport(models.AbstractModel): company = self.env['stock.location'].browse(wh_location_ids).mapped('company_id') svl = self.env['stock.valuation.layer'].search(domain + [('company_id', '=', company.id)]) currency = svl.currency_id or self.env.company.currency_id - quantity = sum(svl.filtered(lambda layer: layer.stock_move_id.location_dest_id.id in wh_location_ids).mapped('quantity')) - if quantity: - total_quantity = sum(svl.mapped('quantity')) + total_quantity = sum(svl.mapped('quantity')) + # Because we can have negative quantities, `total_quantity` may be equal to zero even if the warehouse's `quantity` is positive. + if svl and not float_is_zero(total_quantity, precision_rounding=svl.product_id.uom_id.rounding): + quantity = sum(svl.filtered(lambda layer: layer.stock_move_id.location_dest_id.id in wh_location_ids).mapped('quantity')) value = sum(svl.mapped('value')) * (quantity / total_quantity) else: value = 0