Commit 1c1ec0bc authored by Jérome Perrin's avatar Jérome Perrin

core/mrp: make sure getAggregatedAmountList return amounts in order

For some reports displaying transformations result to end users, it's much
better to have amount ordered in the same order as the model lines.

AmountGeneratorMixin was already sorting the lines, but the order was lost
during aggregation, because we used a dict, which on python2 does not
retain order. Switch to using an OrderedDict to keep the order.
parent dade8d98
......@@ -126,6 +126,30 @@ class TestTransformation(TestTransformationMixin, BaseTestUnitConversion):
aggregated_amount = aggregated_amount_list[0]
self.assertEqual(aggregated_amount.quantity, 2)
def test_getAggregatedAmountListKeepOrder(self):
"""
Make sure that getAggregatedAmountList return amounts in the same order as on the transformation
"""
transformation = self.createTransformation()
first_transformed_resource = self.createTransformedResource(transformation)
first_component = self.createComponent()
first_transformed_resource.edit(
resource_value=first_component,
int_index=1,
quantity=1)
second_transformed_resource = self.createTransformedResource(transformation)
second_component = self.createComponent()
second_transformed_resource.edit(
resource_value=second_component,
int_index=2,
quantity=2)
aggregated_amount_list = transformation.getAggregatedAmountList()
self.assertEqual(
[(a.getQuantity(), a.getResourceValue()) for a in aggregated_amount_list],
[(1, first_component), (2, second_component)]
)
def test_01_getAggregatedAmountListWithVariatedProperty(self):
"""
Make sure that getAggregatedAmountList is still working properly if we
......
......@@ -27,7 +27,7 @@
#
##############################################################################
from collections import defaultdict
from collections import defaultdict, OrderedDict
import zope.interface
from AccessControl import allow_class
from erp5.component.interface.IAmountList import IAmountList
......@@ -76,7 +76,7 @@ class GeneratedAmountList(list):
"""
# XXX: Do we handle rounding correctly ?
# What to do if only total price is rounded ??
aggregate_dict = {}
aggregate_dict = OrderedDict()
result_list = self.__class__()
for amount in self:
key = (amount.getPrice(), amount.getEfficiency(),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment