From 16ee89a6624298c78c2a308575a11f474dd77cfb Mon Sep 17 00:00:00 2001 From: svs-odoo Date: Mon, 26 Aug 2019 16:32:03 +0000 Subject: [PATCH] [FIX] stock: restart a canceled inventory When an inventory is canceled, the user is able to edit its locations and products fields. So, if he/she does and then restart the inventory, it can have some lines who don't match these fields. Now when an inventory adjustment is canceled, we delete all its lines. Its lines will be recreated when the inventory adjustment will be restarted. Thus, lines will always corresponding to their inventory `location_ids` and `product_ids`. Also, a confirmation popup was added to ask if user really want to cancel the inventory when he/shes clicks on the cancel button (to avoid users delete their on going inv. adj. by miss clicking). Task #2058665 --- addons/stock/models/stock_inventory.py | 1 + addons/stock/tests/test_inventory.py | 66 ++++++++++++++++++++ addons/stock/views/stock_inventory_views.xml | 3 +- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/addons/stock/models/stock_inventory.py b/addons/stock/models/stock_inventory.py index e8b76317630..64a370b430a 100644 --- a/addons/stock/models/stock_inventory.py +++ b/addons/stock/models/stock_inventory.py @@ -135,6 +135,7 @@ class Inventory(models.Model): def action_cancel_draft(self): self.mapped('move_ids')._action_cancel() + self.line_ids.unlink() self.write({'state': 'draft'}) def action_start(self): diff --git a/addons/stock/tests/test_inventory.py b/addons/stock/tests/test_inventory.py index d1be07924d3..d5703170494 100644 --- a/addons/stock/tests/test_inventory.py +++ b/addons/stock/tests/test_inventory.py @@ -333,6 +333,72 @@ class TestInventory(SavepointCase): self.assertEqual(line.product_qty, 0) self.assertNotEqual(line.theoretical_qty, 0) + def test_inventory_9_cancel_then_start(self): + """ Checks when we cancel an inventory, then change its locations and/or + products setup and restart it, it will remove all its lines and restart + like a new inventory. + """ + # Creates some records needed for the test... + product2 = self.env['product.product'].create({ + 'name': 'Product B', + 'type': 'product', + 'categ_id': self.env.ref('product.product_category_all').id, + }) + loc1 = self.env['stock.location'].create({ + 'name': 'SafeRoom A', + 'usage': 'internal', + 'location_id': self.stock_location.id, + }) + # Adds some quants. + self.env['stock.quant'].create({ + 'product_id': self.product1.id, + 'product_uom_id': self.uom_unit.id, + 'location_id': loc1.id, + 'quantity': 7, + 'reserved_quantity': 0, + }) + self.env['stock.quant'].create({ + 'product_id': self.product1.id, + 'product_uom_id': self.uom_unit.id, + 'location_id': self.stock_location.id, + 'quantity': 7, + 'reserved_quantity': 0, + }) + self.env['stock.quant'].create({ + 'product_id': product2.id, + 'product_uom_id': self.uom_unit.id, + 'location_id': loc1.id, + 'quantity': 7, + 'reserved_quantity': 0, + }) + self.env['stock.quant'].create({ + 'product_id': product2.id, + 'product_uom_id': self.uom_unit.id, + 'location_id': self.stock_location.id, + 'quantity': 7, + 'reserved_quantity': 0, + }) + # Creates the inventory and configures if for product1 + inventory_form = Form(self.env['stock.inventory'], view='stock.view_inventory_form') + inventory_form.product_ids.add(self.product1) + inventory = inventory_form.save() + inventory.action_start() + # Must have two inventory lines about product1. + self.assertEqual(len(inventory.line_ids), 2) + for line in inventory.line_ids: + self.assertEqual(line.product_id.id, self.product1.id) + + # Cancels the inventory and changes for product2 in its setup. + inventory.action_cancel_draft() + inventory_form = Form(inventory) + inventory_form.product_ids.remove(self.product1.id) + inventory_form.product_ids.add(product2) + inventory = inventory_form.save() + inventory.action_start() + # Must have two inventory lines about product2. + self.assertEqual(len(inventory.line_ids), 2) + self.assertEqual(inventory.line_ids.product_id.id, product2.id) + def test_inventory_prefill_counted_quantity(self): """ Checks that inventory lines have a `product_qty` set on zero or equals to quantity on hand, depending of the `prefill_counted_quantity`. diff --git a/addons/stock/views/stock_inventory_views.xml b/addons/stock/views/stock_inventory_views.xml index 582b76e48b4..2b325c6297a 100644 --- a/addons/stock/views/stock_inventory_views.xml +++ b/addons/stock/views/stock_inventory_views.xml @@ -170,7 +170,8 @@