[Erp5-report] r15607 - /erp5/trunk/products/ERP5/Document/Inventory.py

nobody at svn.erp5.org nobody at svn.erp5.org
Fri Aug 10 17:49:48 CEST 2007


Author: vincent
Date: Fri Aug 10 17:49:48 2007
New Revision: 15607

URL: http://svn.erp5.org?rev=15607&view=rev
Log:
Move getInventoryList call out of main loop - avoid issuing multiple SQL queries when one is enough.

Modified:
    erp5/trunk/products/ERP5/Document/Inventory.py

Modified: erp5/trunk/products/ERP5/Document/Inventory.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5/Document/Inventory.py?rev=15607&r1=15606&r2=15607&view=diff
==============================================================================
--- erp5/trunk/products/ERP5/Document/Inventory.py (original)
+++ erp5/trunk/products/ERP5/Document/Inventory.py Fri Aug 10 17:49:48 2007
@@ -84,35 +84,50 @@
         temp_constructor = newTempDeliveryLine
       start_date = self.getStartDate()
       node = self.getDestination()
-      current_inventory_state_list = self.getPortalCurrentInventoryStateList()
+      portal_simulation = self.getPortalObject().portal_simulation
+      current_inventory_list = portal_simulation.getInventoryList( \
+                                    to_date          = start_date
+                                  , node             = node
+                                  , simulation_state = self.getPortalCurrentInventoryStateList()
+                                  , group_by_sub_variation = 1
+                                  , group_by_variation = 1
+                                  , group_by_resource = 1
+                                  )
+      current_inventory_dict = {}
+      current_inventory_key_id_list = ('resource_relative_url', 'variation_text')
+      for line in current_inventory_list:
+        current_inventory_key = tuple([line[x] for x in current_inventory_key_id_list])
+        try:
+          current_inventory_by_sub_variation = current_inventory_dict[current_inventory_key]
+        except KeyError:
+          current_inventory_by_sub_variation = current_inventory_dict[current_inventory_key] = {}
+        current_inventory_by_sub_variation[line['sub_variation_text']] = line['total_quantity']
+      def getCurrentInventoryBySubVariation(**criterion_dict):
+        current_inventory_key = tuple([criterion_dict[x] for x in current_inventory_key_id_list])
+        return current_inventory_dict.get(current_inventory_key, {})
       for movement in self.getMovementList():
-        resource =  movement.getResourceValue()
-        if resource is not None and movement.getQuantity() not in (None,''):
+        if movement.getResourceValue() is not None and movement.getQuantity() not in (None,''):
+          resource_path =  movement.getResource()
           variation_text = movement.getVariationText()
-          resource_and_variation_key = (resource,variation_text)
+          resource_and_variation_key = (resource_path, variation_text)
           if resource_and_variation_key not in resource_and_variation_dict:
             resource_and_variation_dict[resource_and_variation_key] = None
-            current_inventory_list = resource.getInventoryList( \
-                                    to_date          = start_date
-                                  , variation_text   = variation_text
-                                  , node             = node
-                                  , simulation_state = current_inventory_state_list
-                                  , group_by_sub_variation = 1
-                                  , group_by_variation = 1
-                                  )
             kwd = {'uid':self.getUid(),
                    'start_date': start_date}
             variation_list = variation_text.split('\n')
-            for inventory in current_inventory_list:
+            inventory_by_subvariation_dict = getCurrentInventoryBySubVariation(
+              resource_relative_url=resource_path,
+              variation_text=variation_text)
+            for sub_variation_text, total_quantity in inventory_by_subvariation_dict.iteritems():
               sub_variation_list = []
-              if inventory.sub_variation_text is not None:
-                sub_variation_list = inventory.sub_variation_text.split('\n')
+              if sub_variation_text is not None:
+                sub_variation_list = sub_variation_text.split('\n')
               category_list = self.getCategoryList()
-              if inventory.total_quantity != 0:
+              if total_quantity != 0:
                 temp_delivery_line = temp_constructor(self,
                                                       self.getId())
-                kwd['quantity'] = - inventory.total_quantity
-                category_list.append('resource/%s' % inventory.resource_relative_url)
+                kwd['quantity'] = - total_quantity
+                category_list.append('resource/%s' % resource_path)
                 category_list.extend(variation_list)
                 category_list.extend(sub_variation_list)
                 kwd['category_list'] = category_list




More information about the Erp5-report mailing list