diff --git a/location.py b/location.py index cf8a563403..c4a408cbd8 100644 --- a/location.py +++ b/location.py @@ -332,6 +332,20 @@ class Location(DeactivableMixin, tree(), ModelSQL, ModelView): ('code',) + tuple(clause[1:]), ] + @classmethod + def _get_quantity_grouping(cls): + context = Transaction().context + grouping, grouping_filter, key = (), (), [] + if context.get('product') is not None: + grouping = ('product',) + grouping_filter = ([context['product']],) + key = (context['product'],) + elif context.get('product_template') is not None: + grouping = ('product.template',) + grouping_filter = ([context['product_template']],) + key = (context['product_template'],) + return grouping, grouping_filter, key + @classmethod def get_quantity(cls, locations, name): pool = Pool() @@ -343,9 +357,6 @@ class Location(DeactivableMixin, tree(), ModelSQL, ModelView): return (trans_context.get(name) is not None and isinstance(trans_context[name], int)) - if not any(map(valid_context, ['product', 'product_template'])): - return {l.id: None for l in locations} - context = {} if (name == 'quantity' and (trans_context.get('stock_date_end', datetime.date.max) @@ -357,14 +368,10 @@ class Location(DeactivableMixin, tree(), ModelSQL, ModelView): if not trans_context.get('stock_date_end'): context['stock_date_end'] = datetime.date.max - if trans_context.get('product') is not None: - grouping = ('product',) - grouping_filter = ([trans_context['product']],) - key = trans_context['product'] - else: - grouping = ('product.template',) - grouping_filter = ([trans_context['product_template']],) - key = trans_context['product_template'] + grouping, grouping_filter, key = cls._get_quantity_grouping() + if not grouping: + return {loc.id: None for loc in locations} + pbl = {} for sub_locations in grouped_slice(locations): location_ids = [l.id for l in sub_locations] @@ -375,7 +382,7 @@ class Location(DeactivableMixin, tree(), ModelSQL, ModelView): grouping_filter=grouping_filter, with_childs=trans_context.get('with_childs', True))) - return dict((loc.id, pbl.get((loc.id, key), 0)) for loc in locations) + return dict((loc.id, pbl.get((loc.id,) + key, 0)) for loc in locations) @classmethod def search_quantity(cls, name, domain):