diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py index ebeb00c7f28..98a6a1c11d6 100644 --- a/addons/stock/models/stock_move.py +++ b/addons/stock/models/stock_move.py @@ -1861,6 +1861,8 @@ Please change the quantity done or the rounding precision of your unit of measur .filtered(lambda p: p.quant_ids and len(p.quant_ids) > 1): if len(result_package.quant_ids.filtered(lambda q: not float_is_zero(abs(q.quantity) + abs(q.reserved_quantity), precision_rounding=q.product_uom_id.rounding)).mapped('location_id')) > 1: raise UserError(_('You cannot move the same package content more than once in the same transfer or split the same package into two location.')) + if any(ml.package_id and ml.package_id == ml.result_package_id for ml in moves_todo.move_line_ids): + self.env['stock.quant']._unlink_zero_quants() picking = moves_todo.mapped('picking_id') moves_todo.write({'state': 'done', 'date': fields.Datetime.now()}) diff --git a/addons/stock/tests/test_quant.py b/addons/stock/tests/test_quant.py index f9ef1430d67..16778a0488b 100644 --- a/addons/stock/tests/test_quant.py +++ b/addons/stock/tests/test_quant.py @@ -974,3 +974,33 @@ class StockQuantRemovalStrategy(TransactionCase): [{'reserved_qty': 10}] * 5 + [{'reserved_qty': 5}] * 2 ) + + def test_clean_quant_after_package_move(self): + """ + A product is at WH/Stock in a package PK. We deliver PK. The user should + not find any quant at WH/Stock with PK anymore. + """ + package = self.env['stock.quant.package'].create({}) + self.env['stock.quant']._update_available_quantity(self.product, self.stock_location, 1.0, package_id=package) + + move = self.env['stock.move'].create({ + 'name': 'OUT 1 product', + 'product_id': self.product.id, + 'product_uom_qty': 1, + 'product_uom': self.product.uom_id.id, + 'location_id': self.stock_location.id, + 'location_dest_id': self.ref('stock.stock_location_customers'), + }) + move._action_confirm() + move._action_assign() + move.move_line_ids.write({ + 'result_package_id': package.id, + 'qty_done': 1, + }) + move._action_done() + + self.assertFalse(self.env['stock.quant'].search_count([ + ('product_id', '=', self.product.id), + ('package_id', '=', package.id), + ('location_id', '=', self.stock_location.id), + ]))