From addce55f5ca7c42b8b380d6c8d030fc61ef3ba89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 22 Apr 2018 22:39:30 +0200 Subject: [PATCH] Include expired product in quantity with stock_date_start The quantity over a period should include the removal of the expired product during the period. issue6243 review27091002 --- stock.py | 48 +++++++++++++++++++++++++++++------- tests/test_stock_lot_sled.py | 21 ++++++++++++++++ 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/stock.py b/stock.py index 544714e730..7e3d257b4d 100644 --- a/stock.py +++ b/stock.py @@ -2,7 +2,7 @@ # this repository contains the full copyright notices and license terms. import datetime -from sql import Union, Join, Select, Table, Null +from sql import Union, Join, Select, Table, Null, As from sql.conditionals import Greatest from trytond.pool import PoolMeta, Pool @@ -243,8 +243,9 @@ class Move: today = Date.today() stock_date_end = context.get('stock_date_end') or datetime.date.max - if query and ((stock_date_end == today and context.get('forecast')) - or stock_date_end > today): + if (query and not context.get('skip_lot_sled') + and ((stock_date_end == today and context.get('forecast')) + or stock_date_end > today)): lot = Lot.__table__() config = Config(1) @@ -277,10 +278,9 @@ class Move: elif isinstance(query, Select): yield query - union, = query.from_ - for sub_query in find_queries(union): + def add_join(from_): # Find move table - for i, table in enumerate(sub_query.from_): + for i, table in enumerate(from_): if isinstance(table, Table) and table._name == cls._table: sub_query.from_[i] = join(table) break @@ -291,9 +291,39 @@ class Move: break else: # Not query on move table - continue - sub_query.where &= ((lot.shelf_life_expiration_date == Null) - | (lot.shelf_life_expiration_date >= expiration_date)) + return False + return True + + union, = query.from_ + for sub_query in find_queries(union): + if add_join(sub_query.from_): + sub_query.where &= ( + (lot.shelf_life_expiration_date == Null) + | (lot.shelf_life_expiration_date >= expiration_date)) + + stock_date_start = context.get('stock_date_start') + if stock_date_start: + with Transaction().set_context( + stock_date_start=None, + skip_lot_sled=True): + query_expired = cls.compute_quantities_query( + location_ids, with_childs=with_childs, + grouping=grouping, grouping_filter=grouping_filter) + union_expired, = query_expired.from_ + for sub_query in find_queries(union_expired): + if add_join(sub_query.from_): + # only lot expiring during the period + sub_query.where &= ( + (lot.shelf_life_expiration_date >= + stock_date_start) + & (lot.shelf_life_expiration_date < + stock_date_end)) + # Inverse quantity + for column in sub_query._columns: + if (isinstance(column, As) + and column.output_name == 'quantity'): + column.expression = -column.expression + union.queries += union_expired.queries return query diff --git a/tests/test_stock_lot_sled.py b/tests/test_stock_lot_sled.py index 8e1fcf1904..11ac68be17 100644 --- a/tests/test_stock_lot_sled.py +++ b/tests/test_stock_lot_sled.py @@ -76,6 +76,7 @@ class StockLotSLEDTestCase(ModuleTestCase): empty = {} computed = {(storage.id, product.id): 5} + delta = {(storage.id, product.id): -5} for context, result in [ ({'stock_date_end': today + datetime.timedelta(days=-1)}, empty), @@ -88,6 +89,26 @@ class StockLotSLEDTestCase(ModuleTestCase): ({'stock_date_end': today + datetime.timedelta(days=6)}, empty), ({}, empty), + ({'stock_date_start': today, 'stock_date_end': today}, + empty), + ({'stock_date_start': today + datetime.timedelta(days=1), + 'stock_date_end': today + datetime.timedelta(days=7)}, + delta), + ({'stock_date_start': today + datetime.timedelta(days=1), + 'stock_date_end': today + datetime.timedelta(days=2)}, + empty), + ({'stock_date_start': today + datetime.timedelta(days=1), + 'stock_date_end': today + datetime.timedelta(days=5)}, + empty), + ({'stock_date_start': today + datetime.timedelta(days=5), + 'stock_date_end': today + datetime.timedelta(days=6)}, + delta), + ({'stock_date_start': today + datetime.timedelta(days=5), + 'stock_date_end': today + datetime.timedelta(days=7)}, + delta), + ({'stock_date_start': today + datetime.timedelta(days=6), + 'stock_date_end': today + datetime.timedelta(days=7)}, + empty), ]: with Transaction().set_context(context=context, locations=[storage.id]):