From 09c67577e808190b8703e339c6ef296a50d3dae8 Mon Sep 17 00:00:00 2001 From: ArnauCForgeFlow Date: Thu, 9 May 2024 13:06:09 +0200 Subject: [PATCH] [IMP] stock_cycle_count: add period days to deadline date cycle count --- .../models/stock_cycle_count_rule.py | 7 ++--- stock_cycle_count/models/stock_warehouse.py | 27 +++---------------- .../tests/test_stock_cycle_count.py | 27 +++++++++---------- 3 files changed, 18 insertions(+), 43 deletions(-) diff --git a/stock_cycle_count/models/stock_cycle_count_rule.py b/stock_cycle_count/models/stock_cycle_count_rule.py index 2a3339a5202..ab96c48e353 100644 --- a/stock_cycle_count/models/stock_cycle_count_rule.py +++ b/stock_cycle_count/models/stock_cycle_count_rule.py @@ -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 @@ -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) if next_date < datetime.today(): next_date = datetime.today() except Exception as e: diff --git a/stock_cycle_count/models/stock_warehouse.py b/stock_cycle_count/models/stock_warehouse.py index b9d04fe8fbd..260e11f1300 100644 --- a/stock_cycle_count/models/stock_warehouse.py +++ b/stock_cycle_count/models/stock_warehouse.py @@ -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", @@ -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() diff --git a/stock_cycle_count/tests/test_stock_cycle_count.py b/stock_cycle_count/tests/test_stock_cycle_count.py index 06054bfa149..1974f34be98 100644 --- a/stock_cycle_count/tests/test_stock_cycle_count.py +++ b/stock_cycle_count/tests/test_stock_cycle_count.py @@ -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( [