From 750b01da931e3e1e0d2116ad5310f350258078d1 Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Tue, 24 May 2016 10:56:18 +0200
Subject: [PATCH] amount_generator: fix aggregation with rounding proxies

---
 product/ERP5/GeneratedAmountList.py    | 11 ++++++++---
 product/ERP5/mixin/amount_generator.py |  2 +-
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/product/ERP5/GeneratedAmountList.py b/product/ERP5/GeneratedAmountList.py
index e67cc073e2..a449fe19aa 100644
--- a/product/ERP5/GeneratedAmountList.py
+++ b/product/ERP5/GeneratedAmountList.py
@@ -31,6 +31,7 @@ from collections import defaultdict
 import zope.interface
 from AccessControl import allow_class
 from Products.ERP5Type import interfaces
+from Products.ERP5.Document.RoundingModel import RoundingProxy
 
 class GeneratedAmountList(list):
   """
@@ -92,10 +93,14 @@ class GeneratedAmountList(list):
       # but it is not a good idea, especially when the first expand causes
       # non-zero quantity and then quantity becomes zero.
       aggregate = newTempAmount(amount.aq_parent, '', notify_workflow=False)
-      result_list.append(aggregate)
-      aggregate.__dict__.update(amount.__dict__)
+      aggregate.__dict__.update(amount.aq_base.__dict__)
       aggregate._setQuantity(quantity)
-      del aggregate._base
+      if isinstance(amount, RoundingProxy):
+        aggregate = amount.getPortalObject().portal_roundings.getRoundingProxy(
+          aggregate)
+      else:
+        del aggregate._base
+      result_list.append(aggregate)
     return result_list
 
   def split(self):
diff --git a/product/ERP5/mixin/amount_generator.py b/product/ERP5/mixin/amount_generator.py
index c258cd509d..d65dea2b45 100644
--- a/product/ERP5/mixin/amount_generator.py
+++ b/product/ERP5/mixin/amount_generator.py
@@ -474,7 +474,6 @@ class AmountGeneratorMixin:
           property_dict['causality_value_list'][-1]
             .getRelativeUrl().replace('/', '_'),
           notify_workflow=False)
-        amount._base = delivery_amount
         amount._setCategoryList(property_dict.pop('category_list', ()))
         if amount.getQuantityUnit():
           del property_dict['quantity_unit']
@@ -489,6 +488,7 @@ class AmountGeneratorMixin:
         if rounding:
           # We hope here that rounding is sufficient at line level
           amount = getRoundingProxy(amount, context=self)
+        amount._base = delivery_amount
         result.append(amount)
         # Contribute
         quantity *= property_dict.get('price', 1)
-- 
2.30.9