[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
This commit is contained in:
svs-odoo
2019-09-03 13:31:52 +00:00
parent d3ef8a958b
commit 16ee89a662
3 changed files with 69 additions and 1 deletions
+1
View File
@@ -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):
+66
View File
@@ -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`.
+2 -1
View File
@@ -170,7 +170,8 @@
<button name="action_open_inventory_lines" states="confirm" string="Continue Inventory" type="object" class="oe_highlight" groups="stock.group_stock_user"/>
<button name="action_validate" states="confirm" string="Validate Inventory" type="object" class="oe_highlight" groups="stock.group_stock_manager"/>
<button name="action_cancel_draft" states="cancel" string="Set to Draft" type="object"/>
<button name="action_cancel_draft" states="confirm" string="Cancel Inventory" type="object"/>
<button name="action_cancel_draft" states="confirm" string="Cancel Inventory" type="object"
confirm="If you cancel this inventory adjustment, all its inventory adjustment lines will be lost. Are you sure you want to discard it ?"/>
<field name="state" widget="statusbar" statusbar_visible="draft,confirm,done"/>
</header>
<sheet>