From 7427ade41e910ff3c16ae748890196354fec1e9a Mon Sep 17 00:00:00 2001 From: Carlos Roca Date: Fri, 22 Nov 2024 16:37:15 +0100 Subject: [PATCH] [IMP] purchase_stock_price_unit_sync: Avoid errors with cancelled stock moves When the price of a line is changed, and it does not have any associated stock.move completed but does have stock.moves in a different state, this will result in no SVLs being found. Consequently, attempting to write to the empty recordset is causing issues. --- .../models/purchase_order.py | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/purchase_stock_price_unit_sync/models/purchase_order.py b/purchase_stock_price_unit_sync/models/purchase_order.py index 1c14b88815f..39d52e588d5 100644 --- a/purchase_stock_price_unit_sync/models/purchase_order.py +++ b/purchase_stock_price_unit_sync/models/purchase_order.py @@ -33,21 +33,23 @@ def stock_price_unit_sync(self): bom_type="phantom", ): continue - line.move_ids.write({"price_unit": line._get_stock_move_price_unit()}) - # Apply sudo() to avoid access errors with users without Inventory > Admin - # permissions. - svls = ( - line.move_ids.sudo() - .mapped("stock_valuation_layer_ids") - .filtered( - # Filter children SVLs (like landed cost) - lambda x: not x.stock_valuation_layer_id + moves = line.move_ids.filtered(lambda m: m.state == "done") + if moves: + moves.write({"price_unit": line._get_stock_move_price_unit()}) + # Apply sudo() to avoid access errors with users without Inventory > Admin + # permissions. + svls = ( + moves.sudo() + .mapped("stock_valuation_layer_ids") + .filtered( + # Filter children SVLs (like landed cost) + lambda x: not x.stock_valuation_layer_id + ) + ) + svls.write( + { + "unit_cost": line.with_context( + skip_stock_price_unit_sync=True + )._get_stock_move_price_unit(), + } ) - ) - svls.write( - { - "unit_cost": line.with_context( - skip_stock_price_unit_sync=True - )._get_stock_move_price_unit(), - } - )