From d33dca17c930f667ccbdfaf05e8ffd7e2f5e63b5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Le=20Ninivin?= <cedric.leninivin@tiolive.com>
Date: Fri, 25 Aug 2017 12:15:54 +0200
Subject: [PATCH] SimulationTool: getNextAlertInventoryDate can look for the
 lowest invetory inferior to the reference quantity

---
 product/ERP5/Tool/SimulationTool.py | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/product/ERP5/Tool/SimulationTool.py b/product/ERP5/Tool/SimulationTool.py
index 0667ce4341..cf9dc475d2 100644
--- a/product/ERP5/Tool/SimulationTool.py
+++ b/product/ERP5/Tool/SimulationTool.py
@@ -2032,6 +2032,7 @@ class SimulationTool(BaseTool):
                        range='min',
                        initial_inventory_kw=None,
                        inventory_list_kw=None,
+                       look_for_minimal=False,
                        **kw):
       """
       Give the next date where the quantity is lower than the
@@ -2046,6 +2047,8 @@ class SimulationTool(BaseTool):
 
       inventory_list_kw - additional parameters for looking at next movements
                           (exemple: use omit_output)
+
+      look_for_minimal - Return the date when the inventory is the lowest
       """
       result = None
       # First look at current inventory, we might have already an inventory
@@ -2070,7 +2073,7 @@ class SimulationTool(BaseTool):
       inventory_method = getattr(self, "get%sInventory" % simulation_period)
       initial_inventory = inventory_method(at_date=from_date,
                            **getAugmentedInventoryKeyword(initial_inventory_kw))
-      if checkQuantity(initial_inventory):
+      if checkQuantity(initial_inventory) and not look_for_minimal:
         result = from_date
       else:
         inventory_list_method = getattr(self,
@@ -2086,7 +2089,11 @@ class SimulationTool(BaseTool):
             total_inventory += inventory['inventory']
             if checkQuantity(total_inventory):
               result = inventory['date']
-              break
+              if look_for_minimal:
+                reference_quantity = total_inventory
+                checkQuantity = getCheckQuantityMethod()
+              else:
+                break
       return result
 
     security.declareProtected(Permissions.AccessContentsInformation,
-- 
2.30.9