From c35df66c8f42a47830470813f30c10f9d280ab68 Mon Sep 17 00:00:00 2001 From: JordiMForgeFlow Date: Thu, 11 Apr 2024 12:03:50 +0200 Subject: [PATCH] [FIX] purchase_stock: use correct picking when updating purchase qty Before the fix, the picking used to create the new stock moves resulting from changes in the purchased quantity is simply taking the first available picking of the Purchase. This causes problems when you handle Purchase Orders with multiple open pickings, as the correct picking to use when the quantity is updated is not always taken. After the fix we give priority to evaluate open pickings already related to the line we are updating. We fallback to the same behaviour as before is none is available. closes odoo/odoo#161721 X-original-commit: b3335566a878fd2df29ff02023033acd3cabef70 Signed-off-by: Arnold Moyaux (arm) --- addons/purchase_stock/models/purchase_order_line.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/addons/purchase_stock/models/purchase_order_line.py b/addons/purchase_stock/models/purchase_order_line.py index fd40e3fbd65..e9e4563e87c 100644 --- a/addons/purchase_stock/models/purchase_order_line.py +++ b/addons/purchase_stock/models/purchase_order_line.py @@ -172,8 +172,13 @@ class PurchaseOrderLine(models.Model): note=_('The quantities on your purchase order indicate less than billed. You should ask for a refund.')) # If the user increased quantity of existing line or created a new line - pickings = line.order_id.picking_ids.filtered(lambda x: x.state not in ('done', 'cancel') and x.location_dest_id.usage in ('internal', 'transit', 'customer')) - picking = pickings and pickings[0] or False + # Give priority to the pickings related to the line + line_pickings = line.move_ids.picking_id.filtered(lambda p: p.state not in ('done', 'cancel') and p.location_dest_id.usage in ('internal', 'transit', 'customer')) + if line_pickings: + picking = line_pickings[0] + else: + pickings = line.order_id.picking_ids.filtered(lambda x: x.state not in ('done', 'cancel') and x.location_dest_id.usage in ('internal', 'transit', 'customer')) + picking = pickings and pickings[0] or False if not picking: if not line.product_qty > line.qty_received: continue