[13.0][FIX] stock_location_last_inventory_date

The prevoius implementation was only taking into account the last inventory
specific to the location.
This change includes also inventories done on parents location.

Also display the computed last inventory date in the form view.
This commit is contained in:
Thierry Ducrest
2021-09-08 06:59:36 +02:00
committed by Hai Lang
parent 8d973477f3
commit 7fe3180497
5 changed files with 34 additions and 64 deletions

View File

@@ -3,7 +3,7 @@
{ {
"name": "Stock Location Last Inventory Date", "name": "Stock Location Last Inventory Date",
"summary": "Show the last inventory date for a leaf location", "summary": "Show the last inventory date for a leaf location",
"version": "13.0.1.0.0", "version": "13.0.1.0.1",
"development_status": "Alpha", "development_status": "Alpha",
"category": "Warehouse", "category": "Warehouse",
"website": "https://github.com/OCA/stock-logistics-warehouse", "website": "https://github.com/OCA/stock-logistics-warehouse",
@@ -12,4 +12,5 @@
"application": False, "application": False,
"installable": True, "installable": True,
"depends": ["product", "stock"], "depends": ["product", "stock"],
"data": ["views/stock_location_views.xml"],
} }

View File

@@ -16,8 +16,8 @@ msgstr ""
#. module: stock_location_last_inventory_date #. module: stock_location_last_inventory_date
#: model:ir.model.fields,help:stock_location_last_inventory_date.field_stock_location__last_inventory_date #: model:ir.model.fields,help:stock_location_last_inventory_date.field_stock_location__last_inventory_date
msgid "" msgid ""
"Indicates the last inventory date for the location (only for validated " "Indicates the last inventory date for the location, including inventory done"
"inventories). It is only computed for leaf locations." " on parents location."
msgstr "" msgstr ""
#. module: stock_location_last_inventory_date #. module: stock_location_last_inventory_date
@@ -29,13 +29,3 @@ msgstr ""
#: model:ir.model.fields,field_description:stock_location_last_inventory_date.field_stock_location__last_inventory_date #: model:ir.model.fields,field_description:stock_location_last_inventory_date.field_stock_location__last_inventory_date
msgid "Last Inventory Date" msgid "Last Inventory Date"
msgstr "" msgstr ""
#. module: stock_location_last_inventory_date
#: model:ir.model.fields,field_description:stock_location_last_inventory_date.field_stock_location__validated_inventory_ids
msgid "Stock Inventories"
msgstr ""
#. module: stock_location_last_inventory_date
#: model:ir.model.fields,help:stock_location_last_inventory_date.field_stock_location__validated_inventory_ids
msgid "Stock inventories in state validated for this location."
msgstr ""

View File

@@ -1,6 +1,6 @@
# Copyright 2021 Camptocamp SA # Copyright 2021 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import api, fields, models from odoo import fields, models
class StockLocation(models.Model): class StockLocation(models.Model):
@@ -9,44 +9,19 @@ class StockLocation(models.Model):
last_inventory_date = fields.Datetime( last_inventory_date = fields.Datetime(
"Last Inventory Date", "Last Inventory Date",
compute="_compute_last_inventory_date", compute="_compute_last_inventory_date",
store=True, help="Indicates the last inventory date for the location, "
help="Indicates the last inventory date for the location (only for " "including inventory done on parents location.",
"validated inventories). It is only computed for leaf locations.",
) )
# This field reuses the Many2many already defined in the model
# stock.inventory, so this definition adds little overhead, and
# allows to create the list of depends needed by the field for the
# Last Inventory Date.
validated_inventory_ids = fields.Many2many(
"stock.inventory",
relation="stock_inventory_stock_location_rel",
column1="stock_location_id",
column2="stock_inventory_id",
string="Stock Inventories",
help="Stock inventories in state validated for this location.",
domain="[('location_ids', 'in', id), ('state', '=', 'done')]",
)
@api.depends(
"usage",
"child_ids",
"validated_inventory_ids",
"validated_inventory_ids.date",
"validated_inventory_ids.state",
"validated_inventory_ids.location_ids.usage",
"validated_inventory_ids.location_ids.child_ids",
)
def _compute_last_inventory_date(self): def _compute_last_inventory_date(self):
"""Store date of the last inventory for each leaf location""" for location in self:
for loc in self: location_ids = [
if ( int(location_id)
loc.usage != "view" for location_id in location.parent_path.rstrip("/").split("/")
and not loc.child_ids ]
and loc.validated_inventory_ids last_inventory = self.env["stock.inventory"].search(
): [("location_ids", "in", location_ids), ("state", "=", "done")],
loc.last_inventory_date = loc.validated_inventory_ids.sorted( order="date desc",
lambda inventory: inventory.date limit=1,
)[-1].date )
else: location.last_inventory_date = last_inventory.date
loc.last_inventory_date = False

View File

@@ -45,10 +45,6 @@ class TestStockLocation(SavepointCase):
) )
inventory.with_user(stock_user).action_start() inventory.with_user(stock_user).action_start()
inventory.with_user(stock_manager).action_validate() inventory.with_user(stock_manager).action_validate()
self.assertEqual(
self.leaf_location.with_user(stock_user).validated_inventory_ids.ids,
[inventory.id],
)
self.assertEqual( self.assertEqual(
self.leaf_location.with_user(stock_user).last_inventory_date, inventory.date self.leaf_location.with_user(stock_user).last_inventory_date, inventory.date
) )
@@ -60,8 +56,6 @@ class TestStockLocation(SavepointCase):
def test_leaf_location(self): def test_leaf_location(self):
self.assertFalse(self.leaf_location.child_ids) self.assertFalse(self.leaf_location.child_ids)
self.assertFalse(self.leaf_location.validated_inventory_ids)
self.assertFalse(self.leaf_location.last_inventory_date)
inventory = self.env["stock.inventory"].create( inventory = self.env["stock.inventory"].create(
{ {
"name": "Inventory Adjustment", "name": "Inventory Adjustment",
@@ -71,13 +65,10 @@ class TestStockLocation(SavepointCase):
) )
inventory.action_start() inventory.action_start()
inventory.action_validate() inventory.action_validate()
self.assertEqual(self.leaf_location.validated_inventory_ids.ids, [inventory.id])
self.assertEqual(self.leaf_location.last_inventory_date, inventory.date) self.assertEqual(self.leaf_location.last_inventory_date, inventory.date)
self.assertFalse(self.top_location.last_inventory_date)
def test_top_location(self): def test_top_location(self):
self.assertTrue(self.top_location.child_ids)
self.assertFalse(self.top_location.validated_inventory_ids)
self.assertFalse(self.top_location.last_inventory_date)
inventory = self.env["stock.inventory"].create( inventory = self.env["stock.inventory"].create(
{ {
"name": "Inventory Adjustment", "name": "Inventory Adjustment",
@@ -87,5 +78,5 @@ class TestStockLocation(SavepointCase):
) )
inventory.action_start() inventory.action_start()
inventory.action_validate() inventory.action_validate()
self.assertEqual(self.top_location.validated_inventory_ids.ids, [inventory.id]) self.assertEqual(self.leaf_location.last_inventory_date, inventory.date)
self.assertFalse(self.top_location.last_inventory_date) self.assertEqual(self.top_location.last_inventory_date, inventory.date)

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_location_form" model="ir.ui.view">
<field name="name">stock.location.last.inventory.date</field>
<field name="model">stock.location</field>
<field name="inherit_id" ref="stock.view_location_form" />
<field name="arch" type="xml">
<group name="additional_info" position="inside">
<field name="last_inventory_date" />
</group>
</field>
</record>
</odoo>