Make compute_quantities_query works with product.template grouping

The where clause of the subquery could be empty and we should search for query
inside joins.

issue8394
review253951002
This commit is contained in:
Cédric Krier committed 2019-08-07 19:24:54 +02:00
1 parent 250552bb6c
commit 25306cf34e
2 files changed
+35 -4

No files matched your search

+19 -4
View File
@@ -263,9 +263,11 @@ class Move(metaclass=PoolMeta):
item = getattr(join, pos)
if isinstance(item, Table):
if item._name == cls._table:
return join, pos, getattr(join, pos)
return join, pos, item
else:
return find_table(item)
found = find_table(item)
if found:
return found
def find_queries(query):
if isinstance(query, Union):
@@ -277,6 +279,11 @@ class Move(metaclass=PoolMeta):
for table in query.from_:
for q in find_queries(table):
yield q
elif isinstance(query, Join):
for pos in ['left', 'right']:
item = getattr(query, pos)
for q in find_queries(item):
yield q
def add_join(from_, lot):
# Find move table
@@ -298,9 +305,13 @@ class Move(metaclass=PoolMeta):
for sub_query in find_queries(union):
lot = Lot.__table__()
if add_join(sub_query.from_, lot):
sub_query.where &= (
clause = (
(lot.shelf_life_expiration_date == Null)
| (lot.shelf_life_expiration_date >= expiration_date))
if sub_query.where:
sub_query.where &= clause
else:
sub_query.where = clause
stock_date_start = context.get('stock_date_start')
if stock_date_start:
@@ -315,11 +326,15 @@ class Move(metaclass=PoolMeta):
lot = Lot.__table__()
if add_join(sub_query.from_, lot):
# only lot expiring during the period
sub_query.where &= (
clause = (
(lot.shelf_life_expiration_date >=
stock_date_start)
& (lot.shelf_life_expiration_date <
stock_date_end))
if sub_query.where:
sub_query.where &= clause
else:
sub_query.where = clause
# Inverse quantity
for column in sub_query._columns:
if (isinstance(column, As)
+16
View File
@@ -128,6 +128,22 @@ class StockLotSLEDTestCase(ModuleTestCase):
self.assertEqual(quantity, result,
msg='context: %s, with childs' % repr(context))
quantity = Product.products_by_location(
[storage.id],
grouping=('product.template',),
grouping_filter=([product.template.id],),)
self.assertEqual(quantity, result,
msg='template, context: %s' % repr(context))
quantity = Product.products_by_location(
[storage.id],
grouping=('product.template',),
grouping_filter=([product.template.id],),
with_childs=True)
self.assertEqual(quantity, result,
msg='template, context: %s, with_childs' %
repr(context))
for context, delay, result in [
({'stock_date_end': datetime.date.min},
datetime.timedelta(days=-1), empty),