Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion stock_location_last_inventory_date/models/stock_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ class StockInventory(models.Model):
def _action_done(self):
super()._action_done()
for inventory in self:
done_locations = inventory._get_all_inventory_locations()
last_inventory_date = inventory.date
done_locations = inventory.line_ids.location_id
done_locations.write({"last_inventory_date": last_inventory_date})

def _get_all_inventory_locations(self):
"""Return all locations that should be updated with the inventory date."""
line_locations = self.line_ids.location_id
if self.product_ids:
return line_locations
LOCATION = self.env["stock.location"]
locations = LOCATION.search(
[("location_id", "child_of", self.location_ids.ids)]
)
return LOCATION.browse(list(set(locations.ids) | set(line_locations.ids)))
49 changes: 49 additions & 0 deletions stock_location_last_inventory_date/tests/test_stock_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,52 @@ def test_top_and_leaf_location(self):
]
)
self.assertEqual(locations, self.top_location)

def test_empty_location(self):
empty_location = self.env["stock.location"].create(
{"name": "Leaf", "location_id": self.top_location.id}
)
leaf_empty_location = self.env["stock.location"].create(
{"name": "Leaf", "location_id": empty_location.id}
)
inventory = self.env["stock.inventory"].create(
{
"name": "Inventory Adjustment",
"location_ids": [(4, empty_location.id)],
}
)
inventory.action_start()
inventory.action_validate()
self.assertEqual(empty_location.last_inventory_date, inventory.date)
self.assertEqual(leaf_empty_location.last_inventory_date, inventory.date)
self.assertFalse(self.top_location.last_inventory_date)

def test_inventory_nochange_location(self):

inventory = self.env["stock.inventory"].create(
{
"name": "Inventory Adjustment",
"product_ids": [(4, self.product.id)],
"location_ids": [(4, self.leaf_location.id)],
}
)
inventory.action_start()
inventory.action_validate()
self.assertEqual(self.leaf_location.last_inventory_date, inventory.date)
self.assertFalse(self.top_location.last_inventory_date)

new_inventory_date = fields.Date.add(inventory.date, days=10)
string_inventory_date = fields.Datetime.to_string(new_inventory_date)

with freeze_time(string_inventory_date):
inventory = self.env["stock.inventory"].create(
{
"name": "Inventory Adjustment",
"product_ids": [(4, self.product.id)],
"location_ids": [(4, self.leaf_location.id)],
}
)
inventory.action_start()
inventory.action_validate()
self.assertEqual(self.leaf_location.last_inventory_date, inventory.date)
self.assertFalse(self.top_location.last_inventory_date)