Aggregate product by locations before applying filter when searching by quantity

Closes #14887
(grafted from d7abce5cd10ccba9852f26c7b1aef465e20b4585)

--HG--
branch : 8.0
This commit is contained in:
Cédric Krier
2026-06-11 15:30:11 +02:00
parent f812dd6da9
commit 6c8cb152c6
2 changed files with 27 additions and 9 deletions
+8 -9
View File
@@ -151,6 +151,12 @@ class StockMixin(object):
pbl = Product.products_by_location(
location_ids, with_childs=with_childs, grouping=grouping)
quantities = defaultdict(float)
for key, quantity in pbl.items():
# pbl could return None in some keys
if key[position] is not None:
quantities[key[position]] += quantity
operator_ = {
'=': operator.eq,
'>=': operator.ge,
@@ -161,15 +167,8 @@ class StockMixin(object):
'in': lambda v, l: v in l,
'not in': lambda v, l: v not in l,
}.get(operator_, lambda v, l: False)
record_ids = []
for key, quantity in pbl.items():
if (quantity is not None and operand is not None
and operator_(quantity, operand)):
# pbl could return None in some keys
if key[position] is not None:
record_ids.append(key[position])
return [('id', 'in', record_ids)]
return [('id', 'in',
[k for k, v in quantities.items() if operator_(v, operand)])]
class Move(Workflow, ModelSQL, ModelView):
+19
View File
@@ -335,6 +335,25 @@ class StockTestCase(
test_products_by_location()
with transaction.set_context(
locations=[supplier.id, customer.id, storage.id]):
for domain, found in [
([('quantity', '=', 0)], True),
([('quantity', '>=', 0)], True),
([('quantity', '>', 0)], False),
([('quantity', '<=', 0)], True),
([('quantity', '<', 0)], False),
([('quantity', '!=', 0)], False),
([('quantity', 'in', [0])], True),
([('quantity', 'in', [1])], False),
([('quantity', 'not in', [0])], False),
([('quantity', 'not in', [1])], True),
]:
with self.subTest(domain=domain):
self.assertEqual(
Product.search(domain),
[product] if found else [])
periods = [
today + relativedelta(days=-6),
today + relativedelta(days=-5),