diff --git a/addons/sale_stock/tests/test_sale_stock.py b/addons/sale_stock/tests/test_sale_stock.py index 0fab4e26da5..cc367019650 100644 --- a/addons/sale_stock/tests/test_sale_stock.py +++ b/addons/sale_stock/tests/test_sale_stock.py @@ -1183,6 +1183,29 @@ class TestSaleStock(TestSaleCommon, ValuationReconciliationTestCommon): self.assertEqual(len(sale_order.picking_ids), 2) + def test_update_so_line_qty_with_package(self): + """ + Creates a sale order, then validates the delivery + modifying the sale order lines qty to 0 + move line should be deleted. + """ + self.product_a.type = 'product' + self.env['stock.quant']._update_available_quantity( + self.product_a, self.company_data['default_warehouse'].lot_stock_id, 10, + package_id=self.env['stock.quant.package'].create({'name': 'PacMan'})) + + # Create sale order + sale_order = self._get_new_sale_order(product=self.product_a) + sale_order.action_confirm() + + # Update the SO line + with Form(sale_order.with_context(import_file=True)) as so_form: + with so_form.order_line.edit(0) as line: + line.product_uom_qty = 0 + + self.assertFalse(sale_order.picking_ids.package_level_ids) + self.assertFalse(sale_order.picking_ids.move_line_ids) + def test_multiple_returns(self): # Creates a sale order for 10 products. sale_order = self._get_new_sale_order() diff --git a/addons/stock/models/stock_move_line.py b/addons/stock/models/stock_move_line.py index 45011c3daae..ff281966ef6 100644 --- a/addons/stock/models/stock_move_line.py +++ b/addons/stock/models/stock_move_line.py @@ -488,7 +488,11 @@ class StockMoveLine(models.Model): quants = quants_by_product[ml.product_id.id] quants._update_reserved_quantity(ml.product_id, ml.location_id, -ml.quantity_product_uom, lot_id=ml.lot_id, package_id=ml.package_id, owner_id=ml.owner_id, strict=True) moves = self.mapped('move_id') + package_levels = self.package_level_id res = super().unlink() + package_levels = package_levels.filtered(lambda pl: not (pl.move_line_ids or pl.move_ids)) + if package_levels: + package_levels.unlink() if moves: # Add with_prefetch() to set the _prefecht_ids = _ids # because _prefecht_ids generator look lazily on the cache of move_id