From 84ba65581357a0ef3a51973ed96dbe7cadbc6a03 Mon Sep 17 00:00:00 2001
From: Kazuhiko Shiozaki <kazuhiko@nexedi.com>
Date: Mon, 9 Nov 2009 15:16:47 +0000
Subject: [PATCH] code cleanup.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@30433 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/DeliverySolver/FIFO.py | 40 +++++++++++++++++++----------
 1 file changed, 26 insertions(+), 14 deletions(-)

diff --git a/product/ERP5/DeliverySolver/FIFO.py b/product/ERP5/DeliverySolver/FIFO.py
index 562027958a..7e35dd71b3 100644
--- a/product/ERP5/DeliverySolver/FIFO.py
+++ b/product/ERP5/DeliverySolver/FIFO.py
@@ -33,7 +33,8 @@ from DeliverySolver import DeliverySolver
 
 class FIFO(DeliverySolver):
   """
-    The FIFO solver reduces deliveted quantity by reducing the quantity of simulation movements from the last order.
+  The FIFO solver reduces deliveted quantity by reducing the quantity of
+  simulation movements from the last order.
   """
 
   # Declarative interfaces
@@ -45,8 +46,8 @@ class FIFO(DeliverySolver):
       Move this to mixin
     """
     self.simulation_movement_list = simulation_movement_list
-  
-  def getTotalQuantity():
+
+  def getTotalQuantity(self):
     """
       Move this to mixin
     """
@@ -59,22 +60,33 @@ class FIFO(DeliverySolver):
     """
     """
     result = []
-    def sortByOrderStartDate(a, b);
-      return cmp(a.getExplainationValue().getStartDate() b.getExplainationValue().getStartDate())
-    simulation_movement_list.sort(sortByOrderStartDate)
-    simulation_movement_list.reverse()
+    simulation_movement_list = self._getSimulationMovementList()
+    if len(simulation_movement_list):
+      simulation_movement_list.sort(
+        key=lambda x:x.getExplainationValue().getStartDate(), reverse=True)
     remaining_quantity = self.getTotalQuantity() - new_quantity
-    for movement in self.simulation_movement_list:
+    for movement in simulation_movement_list:
       if remaining_quantity:
-        if movement.getQuantity() < remaining_quantity:
-          result.append((movement, movement.getQuantity()))
-          remaining_quantity -= movement.getQuantity()
+        quantity = movement.getQuantity()
+        if quantity < remaining_quantity:
+          result.append((movement, quantity))
+          remaining_quantity -= quantity
           movement.setQuantity(0)
         else:
-          result.append((movement, remaining_quantity)) 
-          movement.setQuantity(movement.getQuantity() - remaining_quantity)
+          result.append((movement, remaining_quantity))
+          movement.setQuantity(quantity - remaining_quantity)
           remaining_quantity = 0
     # Return movement, split_quantity tuples
     for movement in simulation_movement_list:
       movement.setDeliveryRatio(movement.getQuantity() / new_quantity)
-    return result
\ No newline at end of file
+    return result
+
+  def _getSimulationMovementList(self):
+    """
+    Returns a list of simulation movement sorted from the last order.
+    """
+    simulation_movement_list = self.simulation_movement_list[:]
+    if len(simulation_movement_list):
+      simulation_movement_list.sort(
+        key=lambda x:x.getExplainationValue().getStartDate(), reverse=True)
+    return simulation_movement_list
-- 
2.30.9