From a6b7abef3faae3a40a1555d1164f3725cf8b72eb Mon Sep 17 00:00:00 2001 From: david Date: Tue, 23 Feb 2021 13:44:22 +0100 Subject: [PATCH] [FIX] stock_orderpoint_generator: stock history initial value With initial wrong values we can carry a computing error to the subsequent moves. --- stock_orderpoint_generator/models/product.py | 20 +++++++++++++------ .../tests/test_orderpoint_generator.py | 4 ++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/stock_orderpoint_generator/models/product.py b/stock_orderpoint_generator/models/product.py index afc1debdb..8403f457f 100644 --- a/stock_orderpoint_generator/models/product.py +++ b/stock_orderpoint_generator/models/product.py @@ -4,6 +4,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) from collections import OrderedDict +from datetime import timedelta from odoo import api, fields, models @@ -49,8 +50,13 @@ class ProductProduct(models.Model): # default the compute the stock value anyway to default the value # for products with no moves for the given period initial_stock = {} + # Compute the second before the given date so we don't duplicate + # history values in case the given hour is the same than the one + # of the first move + from_date_stock = from_date - timedelta(seconds=1) + to_date_stock = to_date + timedelta(seconds=1) initial_stock = self.with_context(location=location)._compute_quantities_dict( - False, False, False, to_date=from_date or to_date + False, False, False, to_date=from_date_stock or to_date_stock ) product_moves_dict = {} for move in moves: @@ -76,19 +82,21 @@ class ProductProduct(models.Model): # we can compute the stock historical values from the moves # sequence so we can exploit it statisticaly product_moves = OrderedDict(sorted(product_moves.items())) - stock = False + product_moves_dict[product.id]["stock_history"] = [ + prod_initial_stock.get("qty_available", 0) + ] + stock = 0 first_item = product_moves[next(iter(product_moves))] if from_date: stock = prod_initial_stock.get("qty_available") - if not stock: - stock = first_item["prod_qty"] - first_item["stock"] = stock + first_item["stock"] = stock + first_item["prod_qty"] + stock = first_item["stock"] iter_moves = iter(product_moves) next(iter_moves, None) for date in iter_moves: stock += product_moves[date]["prod_qty"] product_moves[date]["stock"] = stock - product_moves_dict[product.id]["stock_history"] = [ + product_moves_dict[product.id]["stock_history"] += [ v["stock"] for k, v in product_moves.items() ] return product_moves_dict diff --git a/stock_orderpoint_generator/tests/test_orderpoint_generator.py b/stock_orderpoint_generator/tests/test_orderpoint_generator.py index d07aee154..b506efe85 100644 --- a/stock_orderpoint_generator/tests/test_orderpoint_generator.py +++ b/stock_orderpoint_generator/tests/test_orderpoint_generator.py @@ -318,7 +318,7 @@ class TestOrderpointGenerator(SavepointCase): self.template.write( { "auto_min_qty": True, - "auto_min_date_start": "2019-01-01 00:00:00", + "auto_min_date_start": "2019-01-01 01:30:00", "auto_min_date_end": "2019-02-01 00:00:00", "auto_min_qty_criteria": "max", } @@ -368,7 +368,7 @@ class TestOrderpointGenerator(SavepointCase): # Auto min max over a shorter period self.template.write( { - "auto_max_date_start": "2019-01-01 02:00:00", + "auto_max_date_start": "2019-01-01 02:30:00", "auto_max_date_end": "2019-01-01 03:00:00", "auto_min_date_start": "2019-01-01 04:00:00", "auto_min_date_end": "2019-01-01 06:00:00",