diff --git a/stock.py b/stock.py index 23be292a07..a52a2ddd67 100644 --- a/stock.py +++ b/stock.py @@ -238,7 +238,6 @@ class Move(metaclass=PoolMeta): 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) shelf_life_delay = config.get_multivalue('shelf_life_delay') @@ -253,7 +252,7 @@ class Move(metaclass=PoolMeta): else: expiration_date = stock_date_end - def join(move): + def join(move, lot): return move.join(lot, 'LEFT', condition=move.lot == lot.id) @@ -275,17 +274,20 @@ class Move(metaclass=PoolMeta): yield q elif isinstance(query, Select): yield query + for table in query.from_: + for q in find_queries(table): + yield q - def add_join(from_): + def add_join(from_, lot): # Find move table for i, table in enumerate(from_): if isinstance(table, Table) and table._name == cls._table: - sub_query.from_[i] = join(table) + sub_query.from_[i] = join(table, lot) break found = find_table(table) if found: join_, pos, table = found - setattr(join_, pos, join(table)) + setattr(join_, pos, join(table, lot)) break else: # Not query on move table @@ -294,7 +296,8 @@ class Move(metaclass=PoolMeta): union, = query.from_ for sub_query in find_queries(union): - if add_join(sub_query.from_): + lot = Lot.__table__() + if add_join(sub_query.from_, lot): sub_query.where &= ( (lot.shelf_life_expiration_date == Null) | (lot.shelf_life_expiration_date >= expiration_date)) @@ -309,7 +312,8 @@ class Move(metaclass=PoolMeta): 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_): + lot = Lot.__table__() + if add_join(sub_query.from_, lot): # only lot expiring during the period sub_query.where &= ( (lot.shelf_life_expiration_date >= diff --git a/tests/test_stock_lot_sled.py b/tests/test_stock_lot_sled.py index 3cd116b0de..e7dff25d77 100644 --- a/tests/test_stock_lot_sled.py +++ b/tests/test_stock_lot_sled.py @@ -122,6 +122,12 @@ class StockLotSLEDTestCase(ModuleTestCase): self.assertEqual(quantity, result, msg='context: %s' % repr(context)) + quantity = Product.products_by_location( + [storage.id], grouping_filter=([product.id],), + with_childs=True) + self.assertEqual(quantity, result, + msg='context: %s, with childs' % repr(context)) + for context, delay, result in [ ({'stock_date_end': datetime.date.min}, datetime.timedelta(days=-1), empty),