From 2c22675ead7336cee3924ceb2fbc519659507981 Mon Sep 17 00:00:00 2001 From: Simon Lejeune Date: Tue, 8 Oct 2019 11:21:48 +0000 Subject: [PATCH] [FIX] stock: remove move 'without package' properly This revert the revert 61fa590e679448bf5aaaca4cdf6cf01cd757f44a now that 400efc4d40d822a99f744faf50dadff6cd684333 is merged. closes odoo/odoo#38184 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 8f365343d41..4f7f43f62ae 100644 --- a/addons/stock/models/stock_picking.py +++ b/addons/stock/models/stock_picking.py @@ -715,21 +715,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 e1380a8ec28..24e6e2de655 100644 --- a/addons/stock/tests/test_move2.py +++ b/addons/stock/tests/test_move2.py @@ -1815,6 +1815,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) + def test_owner_1(self): """Make a receipt, set an owner and validate""" owner1 = self.env['res.partner'].create({'name': 'owner'})