Skip to content

Commit

Permalink
[IMP] stock_cycle_count: add period days to deadline date cycle count
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnauCForgeFlow committed May 9, 2024
1 parent 28c1195 commit 09c6757
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 43 deletions.
7 changes: 2 additions & 5 deletions stock_cycle_count/models/stock_cycle_count_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# (http://www.forgeflow.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from datetime import datetime, timedelta
from datetime import datetime

from odoo import _, api, fields, models
from odoo.exceptions import UserError, ValidationError
Expand Down Expand Up @@ -178,10 +178,7 @@ def _compute_rule_periodic(self, locs):
)
if latest_inventory_date:
try:
period = self.periodic_count_period / self.periodic_qty_per_period
next_date = fields.Datetime.from_string(
latest_inventory_date
) + timedelta(days=period)
next_date = fields.Datetime.from_string(latest_inventory_date)

Check warning on line 181 in stock_cycle_count/models/stock_cycle_count_rule.py

View check run for this annotation

Codecov / codecov/patch

stock_cycle_count/models/stock_cycle_count_rule.py#L181

Added line #L181 was not covered by tests
if next_date < datetime.today():
next_date = datetime.today()
except Exception as e:
Expand Down
27 changes: 4 additions & 23 deletions stock_cycle_count/models/stock_warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ def _cycle_count_rules_to_compute(self):

@api.model
def _prepare_cycle_count(self, cycle_count_proposed):
rule = cycle_count_proposed["rule_type"]
days_to_add = rule.periodic_count_period / rule.periodic_qty_per_period
deadline_date = cycle_count_proposed["date"] + timedelta(days=days_to_add)
return {
"automatic_deadline_date": cycle_count_proposed["date"],
"automatic_deadline_date": deadline_date,
"location_id": cycle_count_proposed["location"].id,
"cycle_count_rule_id": cycle_count_proposed["rule_type"].id,
"state": "draft",
Expand Down Expand Up @@ -99,28 +102,6 @@ def action_compute_cycle_count_rules(self):
)[0]
domain = [("location_id", "=", loc.id), ("state", "in", ["draft"])]
existing_cycle_counts = self.env["stock.cycle.count"].search(domain)
if existing_cycle_counts:
existing_earliest_date = sorted(
existing_cycle_counts.mapped("date_deadline")
)[0]
existing_earliest_date = fields.Date.from_string(
existing_earliest_date
)
cycle_count_proposed_date = fields.Date.from_string(
cycle_count_proposed["date"]
)
if cycle_count_proposed_date < existing_earliest_date:
cc_to_update = existing_cycle_counts.search(
[("date_deadline", "=", existing_earliest_date)]
)
cc_to_update.write(
{
"automatic_deadline_date": cycle_count_proposed_date,
"cycle_count_rule_id": cycle_count_proposed[
"rule_type"
].id,
}
)
delta = (
fields.Datetime.from_string(cycle_count_proposed["date"])
- datetime.today()
Expand Down
27 changes: 12 additions & 15 deletions stock_cycle_count/tests/test_stock_cycle_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,25 +198,22 @@ def test_cycle_count_planner(self):
pre_existing_count.unlink()
# Execute cron for first time
wh.cron_cycle_count()
# There are counts in state open(execution) and not in state draft
open_counts = self.cycle_count_model.search(
[("location_id", "in", locs.ids), ("state", "=", "open")]
)
self.assertTrue(open_counts, "Cycle counts in execution state")
draft_counts = self.cycle_count_model.search(
[("location_id", "in", locs.ids), ("state", "=", "draft")]
)
self.assertFalse(draft_counts, "No Cycle counts in draft state")
# Execute the cron for second time
wh.cron_cycle_count()
# New cycle counts for same location created in draft state
draft_counts = self.cycle_count_model.search(
[("location_id", "in", locs.ids), ("state", "=", "draft")]
open_counts = self.cycle_count_model.search(
[("location_id", "in", locs.ids), ("state", "=", "open")]
)
# Created counts in draft state
self.assertTrue(draft_counts, "Cycle counts in draft state")
self.assertFalse(open_counts, "No Cycle counts in execution state")
days_to_add = (
self.rule_periodic.periodic_count_period
/ self.rule_periodic.periodic_qty_per_period
)
self.assertTrue(draft_counts, "No Cycle counts in draft state")
# Inventory adjustment only started for cycle counts in open state
self.assertTrue(open_counts.stock_adjustment_ids)
self.assertFalse(draft_counts.stock_adjustment_ids)
expected_date = date.today() + timedelta(days=days_to_add)
# Date has to be today + (period days / counts per period)
self.assertEqual(draft_counts[0].date_deadline, expected_date.date())
# Zero-confirmations:
count = self.cycle_count_model.search(
[
Expand Down

0 comments on commit 09c6757

Please sign in to comment.