From 3431d7b1cd5fd1650855445604610fc7520aae0b Mon Sep 17 00:00:00 2001 From: William Date: Mon, 9 Sep 2019 12:33:55 +0000 Subject: [PATCH] [FIX] stock: remove move 'without package' properly on a picking, move_ids_without_package is a subset of move_lines. The inverse function on move_ids_without_package only work well when we add a move. This commit improves the inverse function to take deletion into account closes odoo/odoo#36558 Signed-off-by: Simon Lejeune (sle) --- addons/stock/models/stock_picking.py | 36 ++++++++++++++++++---------- addons/stock/tests/test_move2.py | 14 +++++++++++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/addons/stock/models/stock_picking.py b/addons/stock/models/stock_picking.py index 1398d26188c..58a9c5e630d 100644 --- a/addons/stock/models/stock_picking.py +++ b/addons/stock/models/stock_picking.py @@ -645,21 +645,31 @@ class Picking(models.Model): @api.depends('state', 'move_lines', 'move_lines.state', 'move_lines.package_level_id', 'move_lines.move_line_ids.package_level_id') def _compute_move_without_package(self): for picking in self: - move_ids_without_package = self.env['stock.move'] - if picking.picking_type_entire_packs == False: - move_ids_without_package = picking.move_lines - else: - for move in picking.move_lines: - if not move.package_level_id: - if move.state in ('assigned', 'done'): - if any(not ml.package_level_id for ml in move.move_line_ids): - move_ids_without_package |= move - else: - move_ids_without_package |= move - picking.move_ids_without_package = move_ids_without_package.filtered(lambda move: not move.scrap_ids) + picking.move_ids_without_package = picking._get_move_ids_without_package() def _set_move_without_package(self): - self.move_lines |= self.move_ids_without_package + new_mwp = self[0].move_ids_without_package + for picking in self: + old_mwp = picking._get_move_ids_without_package() + picking.move_lines = (picking.move_lines - old_mwp) | new_mwp + moves_to_unlink = old_mwp - new_mwp + if moves_to_unlink: + moves_to_unlink.unlink() + + def _get_move_ids_without_package(self): + self.ensure_one() + move_ids_without_package = self.env['stock.move'] + if not self.picking_type_entire_packs: + move_ids_without_package = self.move_lines + else: + for move in self.move_lines: + if not move.package_level_id: + if move.state in ('assigned', 'done'): + if any(not ml.package_level_id for ml in move.move_line_ids): + move_ids_without_package |= move + else: + move_ids_without_package |= move + return move_ids_without_package.filtered(lambda move: not move.scrap_ids) def _check_move_lines_map_quant_package(self, package): """ This method checks that all product of the package (quant) are well present in the move_line_ids of the picking. """ diff --git a/addons/stock/tests/test_move2.py b/addons/stock/tests/test_move2.py index 14c4f133f3c..0a78a4ee01f 100644 --- a/addons/stock/tests/test_move2.py +++ b/addons/stock/tests/test_move2.py @@ -1837,6 +1837,20 @@ class TestSinglePicking(TestStockCommon): back_order = self.env['stock.picking'].search([('backorder_id', '=', delivery_order.id)]) self.assertFalse(back_order, 'There should be no back order') + def test_unlink_move_1(self): + picking = Form(self.env['stock.picking']) + ptout = self.env['stock.picking.type'].browse(self.picking_type_out) + picking.picking_type_id = ptout + with picking.move_ids_without_package.new() as move: + move.product_id = self.productA + move.product_uom_qty = 10 + picking = picking.save() + + picking = Form(picking) + picking.move_ids_without_package.remove(0) + picking = picking.save() + self.assertEqual(len(picking.move_ids_without_package), 0) + class TestStockUOM(TestStockCommon): def setUp(self):