diff --git a/addons/sale_stock/tests/test_sale_stock.py b/addons/sale_stock/tests/test_sale_stock.py index e90130cb68c..ea2a5bb53dd 100644 --- a/addons/sale_stock/tests/test_sale_stock.py +++ b/addons/sale_stock/tests/test_sale_stock.py @@ -232,3 +232,37 @@ class TestSaleStock(TestSale): self.so.action_done() self.assertEqual(self.so.invoice_status, 'invoiced', 'Sale Stock: so invoice_status should be "invoiced" when set to done') + + def test_04_create_picking_update_saleorderline(self): + """ + Test that updating multiple sale order lines after a succesful delivery creates a single picking containing + the new move lines. + """ + # sell two products + item1 = self.products['prod_order'] + item2 = self.products['prod_del'] + + self.so = self.env['sale.order'].create({ + 'partner_id': self.partner.id, + 'order_line': [ + (0, 0, {'name': item1.name, 'product_id': item1.id, 'product_uom_qty': 1, 'product_uom': item1.uom_id.id, 'price_unit': item1.list_price}), + (0, 0, {'name': item2.name, 'product_id': item2.id, 'product_uom_qty': 1, 'product_uom': item2.uom_id.id, 'price_unit': item2.list_price}), + ], + }) + self.so.action_confirm() + + # deliver them + self.assertEquals(len(self.so.picking_ids), 1) + self.so.picking_ids[0].action_done() + self.assertEquals(self.so.picking_ids[0].state, "done") + + # update the two original sale order lines + self.so.write({ + 'order_line': [ + (1, self.so.order_line[0].id, {'product_uom_qty': 2}), + (1, self.so.order_line[1].id, {'product_uom_qty': 2}), + ] + }) + + # a single picking should be created for the new delivery + self.assertEquals(len(self.so.picking_ids), 2) diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py index 6cc4a35a0bd..c2cfd811f3b 100644 --- a/addons/stock/models/stock_move.py +++ b/addons/stock/models/stock_move.py @@ -412,6 +412,14 @@ class StockMove(models.Model): type (moves should already have them identical). Otherwise, create a new picking to assign them to. """ Picking = self.env['stock.picking'] + + # If this method is called in batch by a write on a one2many and + # at some point had to create a picking, some next iterations could + # try to find back the created picking. As we look for it by searching + # on some computed fields, we have to force a recompute, else the + # record won't be found. + self.recompute() + for move in self: picking = Picking.search([ ('group_id', '=', move.group_id.id),