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
This commit is contained in:
Cédric Krier committed 2019-01-17 17:09:42 +01:00
1 parent 88ab30c40f
commit 085dcbf571
2 files changed
+17 -7

No files matched your search

+11 -7
View File
@@ -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 >=
+6
View File
@@ -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),