From 1801a8aea196c557b6b020e7cd6736c2d12d9717 Mon Sep 17 00:00:00 2001
From: Romain Courteaud <romain@nexedi.com>
Date: Thu, 9 Jun 2005 09:05:32 +0000
Subject: [PATCH] Bug fix: distribute ratio when total_quantity is 0.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@3224 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/DeliveryBuilder.py | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/product/ERP5/Document/DeliveryBuilder.py b/product/ERP5/Document/DeliveryBuilder.py
index 7a92c877bf..d301840b94 100755
--- a/product/ERP5/Document/DeliveryBuilder.py
+++ b/product/ERP5/Document/DeliveryBuilder.py
@@ -504,14 +504,21 @@ class DeliveryBuilder(XMLObject, Amount, Predicate):
     # Store the good quantity value on delivery line
     for movement in delivery.getMovementList():
       total_quantity = 0
-      for simulation_movement in movement.getDeliveryRelatedValueList(
-                                            portal_type="Simulation Movement"):
+      sim_mvt_list = movement.getDeliveryRelatedValueList(
+                                             portal_type="Simulation Movement")
+
+      for simulation_movement in sim_mvt_list:
         total_quantity += simulation_movement.getQuantity()
 
-      for simulation_movement in movement.getDeliveryRelatedValueList(
-                                            portal_type="Simulation Movement"):
-        simulation_movement.setDeliveryRatio(
-             simulation_movement.getQuantity()/total_quantity)
+      if total_quantity != 0:
+        for simulation_movement in sim_mvt_list:
+          quantity = simulation_movement.getQuantity()
+          simulation_movement.setDeliveryRatio(quantity/total_quantity)
+      else:
+        # Distribute equally ratio to all movement
+        mvt_ratio = 1 / len(sim_mvt_list)
+        for simulation_movement in sim_mvt_list:
+          simulation_movement.setDeliveryRatio(mvt_ratio)
 
       movement.edit(quantity=total_quantity)
 
-- 
2.30.9