diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py index e3f1384bbab8cc836e724ae70074cdecf05e050b..15a58e1327b41f5e843962e1e3c9631225a2cdb9 100644 --- a/addons/stock/models/stock_move.py +++ b/addons/stock/models/stock_move.py @@ -1731,6 +1731,8 @@ class StockMove(models.Model): .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 43c55bb18e4c1b3de318328bbc0a4bed9abd42e1..5df30f1ec58bf28833ffca8b2d5972ca5e0db6a4 100644 --- a/addons/stock/tests/test_quant.py +++ b/addons/stock/tests/test_quant.py @@ -796,3 +796,33 @@ class StockQuant(TransactionCase): # cache to ensure that the value will be the newest quant.invalidate_cache(fnames=['quantity'], ids=quant.ids) self.assertEqual(quant.quantity, 11) + + 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), + ]))