diff --git a/addons/sale_mrp/models/sale_order_line.py b/addons/sale_mrp/models/sale_order_line.py index 85cbca703f2..2cbdb1a5619 100644 --- a/addons/sale_mrp/models/sale_order_line.py +++ b/addons/sale_mrp/models/sale_order_line.py @@ -113,6 +113,8 @@ class SaleOrderLine(models.Model): # kits that are currently in delivery bom = self.env['mrp.bom']._bom_find(self.product_id, bom_type='phantom')[self.product_id] if bom: + if not self.product_uom_qty or (previous_product_uom_qty and not previous_product_uom_qty.get(self.id, 0.0)): + return (previous_product_uom_qty and previous_product_uom_qty.get(self.id, 0.0)) or self.qty_delivered moves = self.move_ids.filtered(lambda r: r.state != 'cancel' and not r.scrapped) filters = { 'incoming_moves': lambda m: m.location_dest_id.usage == 'customer' and (not m.origin_returned_move_id or (m.origin_returned_move_id and m.to_refund)), diff --git a/addons/sale_mrp/tests/test_sale_mrp_flow.py b/addons/sale_mrp/tests/test_sale_mrp_flow.py index 8fc60da83db..108b386ca34 100644 --- a/addons/sale_mrp/tests/test_sale_mrp_flow.py +++ b/addons/sale_mrp/tests/test_sale_mrp_flow.py @@ -2119,3 +2119,41 @@ class TestSaleMrpFlow(ValuationReconciliationTestCommon): self.assertEqual(so.order_line.purchase_price, 60) so.action_confirm() self.assertEqual(so.order_line.purchase_price, 60) + + def test_kit_decrease_sol_qty_to_zero(self): + """ + Create and confirm a SO with a kit product. Increasing/Decreasing the SOL qty + should update the qty on the delivery. + """ + stock_location = self.company_data['default_warehouse'].lot_stock_id + + grp_uom = self.env.ref('uom.group_uom') + self.env.user.write({'groups_id': [(4, grp_uom.id)]}) + + # 10 kit_3 = 10 x compo_f + 20 x compo_g + self.env['stock.quant']._update_available_quantity(self.component_f, stock_location, 10) + self.env['stock.quant']._update_available_quantity(self.component_g, stock_location, 20) + + so_form = Form(self.env['sale.order']) + so_form.partner_id = self.partner_a + with so_form.order_line.new() as line: + line.product_id = self.kit_3 + line.product_uom_qty = 2 + line.product_uom = self.uom_ten + so = so_form.save() + so.action_confirm() + + delivery = so.picking_ids + self.assertRecordValues(delivery.move_ids, [ + {'product_id': self.component_f.id, 'product_uom_qty': 20}, + {'product_id': self.component_g.id, 'product_uom_qty': 40}, + ]) + + # Decrease the qty to 0 + with Form(so) as so_form: + with so_form.order_line.edit(0) as line: + line.product_uom_qty = 0 + self.assertRecordValues(delivery.move_ids, [ + {'product_id': self.component_f.id, 'product_uom_qty': 0}, + {'product_id': self.component_g.id, 'product_uom_qty': 0}, + ])