From 8fe7229e1984811f3456dbf502cb03fba879e180 Mon Sep 17 00:00:00 2001 From: Jorge Pinna Puissant Date: Mon, 17 Dec 2018 14:31:47 +0000 Subject: [PATCH] [FIX] sale_stock: fix onchange product_uom_qty opw-1916064 Have a sales order line on which you decrease the quantity. Before, this commit, the warning message that says to update the delivery order did not show. This was because, the onchange in the python side compared the new quantity against the saved one, and did not find any difference. This is caused by the onchange itself, which, in a o2m setting, received from the JS, the changed order (already containing the changed line) and only then proceeded to the onchange of the line. The first operation wrote onto _origin the new line value, which corresponded subsequently to the new value. After this commit, we compare the real value explicitly retrieved from the database to the one just entered by the user as an onchange. The warning message works as expected. Long story short, _origin in an onchange doesn't guarantee to yield persisted values in all cases. closes odoo/odoo#29582 --- addons/sale_stock/models/sale_order.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/addons/sale_stock/models/sale_order.py b/addons/sale_stock/models/sale_order.py index da048baa577..d59b222b850 100644 --- a/addons/sale_stock/models/sale_order.py +++ b/addons/sale_stock/models/sale_order.py @@ -188,7 +188,14 @@ class SaleOrderLine(models.Model): @api.onchange('product_uom_qty') def _onchange_product_uom_qty(self): - if self.state == 'sale' and self.product_id.type in ['product', 'consu'] and self.product_uom_qty < self._origin.product_uom_qty: + # When modifying a one2many, _origin doesn't guarantee that its values will be the ones + # in database. Hence, we need to explicitly read them from there. + if self._origin: + product_uom_qty_origin = self._origin.read(["product_uom_qty"])[0]["product_uom_qty"] + else: + product_uom_qty_origin = 0 + + if self.state == 'sale' and self.product_id.type in ['product', 'consu'] and self.product_uom_qty < product_uom_qty_origin: # Do not display this warning if the new quantity is below the delivered # one; the `write` will raise an `UserError` anyway. if self.product_uom_qty < self.qty_delivered: