mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
[FIX] stock_orderpoint_generator: stock history initial value
With initial wrong values we can carry a computing error to the subsequent moves.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user