Subject: changeset in modules/stock_lot_sled:5.0 Search queries inside Select and add different lot instances From: Cédric Krier <[email protected]>
changeset 3e94c352ce84 in modules/stock_lot_sled:5.0 details: https://hg.tryton.org/modules/stock_lot_sled?cmd=changeset;node=3e94c352ce84 description: Search queries inside Select and add different lot instances When compute_quantities_query is called with with_childs, the selected move table is a union. So we must search inside this union for the actual move table. As we may need to join with lot table multiple times, we must ensure to always use a different instance of the table to avoid alias clash. issue7882 review48701002 (grafted from ae31a427c3966a91e3267bdd10bb2ecc1524d9ae) diffstat: stock.py | 18 +++++++++++------- 1 files changed, 11 insertions(+), 7 deletions(-) diffs (64 lines): diff -r 034bb0779970 -r 3e94c352ce84 stock.py --- a/stock.py Mon Jan 14 23:59:08 2019 +0100 +++ b/stock.py Thu Jan 17 17:09:42 2019 +0100 @@ -243,7 +243,6 @@ 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') @@ -258,7 +257,7 @@ else: expiration_date = stock_date_end - def join(move): + def join(move, lot): return move.join(lot, 'LEFT', condition=move.lot == lot.id) @@ -280,17 +279,20 @@ 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 @@ -299,7 +301,8 @@ 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)) @@ -314,7 +317,8 @@ 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 >=
