changeset ae31a427c396 in modules/stock_lot_sled:default
details:
https://hg.tryton.org/modules/stock_lot_sled?cmd=changeset;node=ae31a427c396
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
diffstat:
stock.py | 18 +++++++++++-------
tests/test_stock_lot_sled.py | 6 ++++++
2 files changed, 17 insertions(+), 7 deletions(-)
diffs (80 lines):
diff -r 4331c3f92832 -r ae31a427c396 stock.py
--- a/stock.py Tue Jan 15 00:00:18 2019 +0100
+++ b/stock.py Thu Jan 17 17:09:42 2019 +0100
@@ -238,7 +238,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')
@@ -253,7 +252,7 @@
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 @@
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 @@
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 @@
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 -r 4331c3f92832 -r ae31a427c396 tests/test_stock_lot_sled.py
--- a/tests/test_stock_lot_sled.py Tue Jan 15 00:00:18 2019 +0100
+++ b/tests/test_stock_lot_sled.py Thu Jan 17 17:09:42 2019 +0100
@@ -122,6 +122,12 @@
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),