changeset f200f0a07bfd in modules/stock_supply:default
details: 
https://hg.tryton.org/modules/stock_supply?cmd=changeset&node=f200f0a07bfd
description:
        Limit size of grouping_filter ids

        The list of ids in grouping_filter are used inside an IN-clause so we 
must
        limit the size.

        issue10589
        review379471002
diffstat:

 shipment.py |  38 +++++++++++++++++++++-----------------
 1 files changed, 21 insertions(+), 17 deletions(-)

diffs (73 lines):

diff -r 64d19d6aa27a -r f200f0a07bfd shipment.py
--- a/shipment.py       Wed Oct 27 21:26:20 2021 +0200
+++ b/shipment.py       Sat Oct 30 02:27:19 2021 +0200
@@ -1,8 +1,10 @@
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
 import datetime
+from collections import defaultdict
 
 from trytond.model import ModelView, ModelSQL
+from trytond.tools import grouped_slice
 from trytond.transaction import Transaction
 from trytond.pool import Pool
 
@@ -57,22 +59,26 @@
         id2location.update({l.id: l for l in implicit_locations})
         location_ids = list(id2location.keys())
 
+        def get_pbl(start, end, grouping_filter):
+            with Transaction().set_context(
+                    forecast=True, stock_date_start=start, stock_date_end=end):
+                return Product.products_by_location(
+                    location_ids, with_childs=True,
+                    grouping_filter=grouping_filter)
+
         # ordered by ids to speedup reduce_ids in products_by_location
         if implicit_locations:
             products = Product.search([
                     ('type', 'in', ['goods', 'assets']),
                     ], order=[('id', 'ASC')])
             product_ids = [p.id for p in products]
-            grouping_filter = None
+            pbl = get_pbl(None, today, None)
         else:
             product_ids = list(id2product.keys())
             product_ids.sort()
-            grouping_filter = (product_ids,)
-
-        with Transaction().set_context(forecast=True, stock_date_end=today):
-            pbl = Product.products_by_location(
-                location_ids, with_childs=True,
-                grouping_filter=grouping_filter)
+            pbl = defaultdict(int)
+            for sub_product_ids in grouped_slice(product_ids):
+                pbl.update(get_pbl(None, today, (list(sub_product_ids),)))
 
         shipments = []
         date = today
@@ -153,16 +159,14 @@
             date += datetime.timedelta(1)
 
             # Update quantities with next moves
-            with Transaction().set_context(
-                    forecast=True,
-                    stock_date_start=date,
-                    stock_date_end=date):
-                pbl = Product.products_by_location(
-                    location_ids,
-                    with_childs=True,
-                    grouping_filter=grouping_filter)
-            for key, qty in pbl.items():
-                current_qties[key] += qty
+            if implicit_locations:
+                for key, qty in get_pbl(date, date, None).items():
+                    current_qties[key] += qty
+            else:
+                for sub_product_ids in grouped_slice(product_ids):
+                    for key, qty in get_pbl(
+                            date, date, (list(sub_product_ids),)).items():
+                        current_qties[key] += qty
 
         if shipments:
             cls.save(shipments)

Reply via email to