[12.0][IMP] stock_cycle_count: auto link inventories to planned cycle counts

This commit is contained in:
Lois Rilo
2021-05-26 11:53:21 +02:00
parent 51110777ca
commit e22d8fdf62
3 changed files with 54 additions and 0 deletions

View File

@@ -39,6 +39,30 @@ class StockInventory(models.Model):
inv.cycle_count_id.state = 'done'
return True
def _domain_cycle_count_candidate(self):
return [
("state", "=", "draft"),
("location_id", "=", self.location_id.id),
]
def _link_to_planned_cycle_count(self):
self.ensure_one()
domain = self._domain_cycle_count_candidate()
candidate = self.env["stock.cycle.count"].search(
domain, limit=1, order="date_deadline asc")
# Also find inventories that do not exclude subloations but that are
# for a bin location (no childs). This makes the attachment logic more
# flexible and user friendly (no need to remember to tick the
# non-standard `exclude_sublocation` field).
if candidate and self.filter == "none" and (
self.exclude_sublocation or not self.location_id.child_ids):
candidate.state = "open"
self.write({
"cycle_count_id": candidate.id,
"exclude_sublocation": True,
})
return True
@api.multi
def action_validate(self):
res = super(StockInventory, self).action_validate()
@@ -51,6 +75,13 @@ class StockInventory(models.Model):
self._update_cycle_state()
return res
@api.model
def create(self, vals):
res = super().create(vals)
if not res.cycle_count_id:
res._link_to_planned_cycle_count()
return res
@api.multi
def write(self, vals):
for inventory in self:

View File

@@ -13,6 +13,7 @@ class LocationAccuracyReport(models.AbstractModel):
def _get_inventory_domain(self, loc_id, exclude_sublocation=True):
return [('location_id', '=', loc_id),
('exclude_sublocation', '=', exclude_sublocation),
('filter', '=', 'none'),
('state', '=', 'done')]
@api.model

View File

@@ -274,3 +274,25 @@ class TestStockCycleCount(common.TransactionCase):
with self.assertRaises(ValidationError):
self.zero_rule.warehouse_ids = [
(4, self.small_wh.id)]
def test_auto_link_inventory_to_cycle_count_1(self):
"""Create an inventory that could fit a planned cycle count should
auto-link it to that cycle count."""
self.assertEqual(self.cycle_count_1.state, "draft")
inventory = self.inventory_model.create({
"name": "new inventory",
"location_id": self.count_loc.id,
"exclude_sublocation": True,
})
self.assertEqual(inventory.cycle_count_id, self.cycle_count_1)
self.assertEqual(self.cycle_count_1.state, "open")
def test_auto_link_inventory_to_cycle_count_2(self):
"""Test auto-link when exclude sublocation is no set."""
self.assertEqual(self.cycle_count_1.state, "draft")
inventory = self.inventory_model.create({
"name": "new inventory",
"location_id": self.count_loc.id,
})
self.assertEqual(inventory.cycle_count_id, self.cycle_count_1)
self.assertEqual(self.cycle_count_1.state, "open")