changeset 5909014f8cfc in modules/stock_quantity_early_planning:default
details: 
https://hg.tryton.org/modules/stock_quantity_early_planning?cmd=changeset&node=5909014f8cfc
description:
        Plan consumable earlier for today

        issue10880
        review372381002
diffstat:

 stock.py |  77 +++++++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 45 insertions(+), 32 deletions(-)

diffs (101 lines):

diff -r a2639d2639dc -r 5909014f8cfc stock.py
--- a/stock.py  Fri Oct 15 19:14:03 2021 +0200
+++ b/stock.py  Tue Oct 26 19:37:27 2021 +0200
@@ -230,8 +230,6 @@
         Date = pool.get('ir.date')
         Location = pool.get('stock.location')
         Move = pool.get('stock.move')
-        ProductQuantitiesByWarehouse = pool.get(
-            'stock.product_quantities_warehouse')
 
         transaction = Transaction()
         today = Date.today()
@@ -271,36 +269,7 @@
 
             for product, moves in groupby(moves, lambda m: m.product):
                 for move in moves:
-                    with transaction.set_context(
-                            product=product.id,
-                            warehouse=warehouse.id,
-                            stock_skip_warehouse=False,
-                            ):
-                        product_quantities = (
-                            ProductQuantitiesByWarehouse.search([
-                                    ('date', '>=', today),
-                                    ('date', '<=', move.planned_date),
-                                    ],
-                                order=[('date', 'DESC')]))
-                        last_product_quantity, = (
-                            ProductQuantitiesByWarehouse.search(
-                                [], order=[('date', 'DESC')], limit=1))
-                    earlier_date = move.planned_date
-                    if product_quantities:
-                        assert product_quantities[0].date == move.planned_date
-                        # The new date must left the same available
-                        # quantity for other moves at the current date
-                        min_quantity = (
-                            product_quantities[0].quantity
-                            + move.internal_quantity)
-                        if last_product_quantity.quantity > 0:
-                            # The remaining quantities can be used
-                            min_quantity -= last_product_quantity.quantity
-                        if min_quantity >= 0:
-                            for product_quantity in product_quantities[1:]:
-                                if product_quantity.quantity < min_quantity:
-                                    break
-                                earlier_date = product_quantity.date
+                    earlier_date = cls._get_earlier_date(move, warehouse)
                     plan = cls._add(move, plans)
                     if earlier_date < move.planned_date:
                         plan.early_date = earlier_date
@@ -320,6 +289,50 @@
         cls.delete(to_delete)
 
     @classmethod
+    def _get_earlier_date(cls, move, warehouse):
+        pool = Pool()
+        Date = pool.get('ir.date')
+        ProductQuantitiesByWarehouse = pool.get(
+            'stock.product_quantities_warehouse')
+        product = move.product
+        today = Date.today()
+
+        if product.consumable:
+            return today
+
+        with Transaction().set_context(
+                product=product.id,
+                warehouse=warehouse.id,
+                stock_skip_warehouse=False,
+                ):
+            product_quantities = (
+                ProductQuantitiesByWarehouse.search([
+                        ('date', '>=', today),
+                        ('date', '<=', move.planned_date),
+                        ],
+                    order=[('date', 'DESC')]))
+            last_product_quantity, = (
+                ProductQuantitiesByWarehouse.search(
+                    [], order=[('date', 'DESC')], limit=1))
+        earlier_date = move.planned_date
+        if product_quantities:
+            assert product_quantities[0].date == move.planned_date
+            # The new date must left the same available
+            # quantity for other moves at the current date
+            min_quantity = (
+                product_quantities[0].quantity
+                + move.internal_quantity)
+            if last_product_quantity.quantity > 0:
+                # The remaining quantities can be used
+                min_quantity -= last_product_quantity.quantity
+            if min_quantity >= 0:
+                for product_quantity in product_quantities[1:]:
+                    if product_quantity.quantity < min_quantity:
+                        break
+                    earlier_date = product_quantity.date
+        return earlier_date
+
+    @classmethod
     def _add(cls, origin, plans):
         if origin in plans:
             plan = plans[origin]

Reply via email to