Commit 5b3055d8 authored by Julien Muchembled's avatar Julien Muchembled

amount_generator: new GeneratedAmountList.aggregate method

Sometimes, we need to generate amounts with and without aggregation.
With this new method, we avoid duplicate computation.

The purpose is to generate amounts only once and
parent feb60a37
...@@ -60,4 +60,25 @@ class GeneratedAmountList(list): ...@@ -60,4 +60,25 @@ class GeneratedAmountList(list):
result += duration result += duration
return result return result
def aggregate(self):
# XXX: Do we handle rounding correctly ?
# What to do if only total price is rounded ??
aggregate_dict = {}
result_list = self.__class__()
for amount in self:
key = (amount.getPrice(), amount.getEfficiency(),
amount.getReference(), amount.categories)
aggregate = aggregate_dict.get(key)
if aggregate is None:
aggregate_dict[key] = [amount, amount.getQuantity()]
result_list.append(amount)
else:
aggregate[1] += amount.getQuantity()
for amount, quantity in aggregate_dict.itervalues():
# Before we ignore 'quantity==0' amount here for better performance,
# but it is not a good idea, especially when the first expand causes
# non-zero quantity and then quantity becomes zero.
amount._setQuantity(quantity)
return result_list
allow_class(GeneratedAmountList) allow_class(GeneratedAmountList)
...@@ -503,42 +503,12 @@ class AmountGeneratorMixin: ...@@ -503,42 +503,12 @@ class AmountGeneratorMixin:
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'getAggregatedAmountList') 'getAggregatedAmountList')
def getAggregatedAmountList(self, amount_list=None, rounding=False, def getAggregatedAmountList(self, *args, **kw):
amount_generator_type_list=None,
generate_empty_amounts=True):
""" """
Implementation of a generic transformation algorith which is Implementation of a generic transformation algorith which is
applicable to payroll, tax generation and BOMs. Return the applicable to payroll, tax generation and BOMs. Return the
list of amounts with aggregation. list of amounts with aggregation.
""" """
generated_amount_list = self.getGeneratedAmountList( return self.getGeneratedAmountList(*args, **kw).aggregate()
amount_list=amount_list, rounding=rounding,
amount_generator_type_list=amount_generator_type_list)
# XXX: Do we handle rounding correctly ?
# What to do if only total price is rounded ??
aggregate_dict = {}
result_list = GeneratedAmountList()
for amount in generated_amount_list:
key = (amount.getPrice(), amount.getEfficiency(),
amount.getReference(), amount.categories)
aggregate = aggregate_dict.get(key)
if aggregate is None:
aggregate_dict[key] = [amount, amount.getQuantity()]
result_list.append(amount)
else:
aggregate[1] += amount.getQuantity()
for amount, quantity in aggregate_dict.itervalues():
# Before we ignore 'quantity==0' amount here for better
# performance, but it is not a good idea, especially when the
# first expand causes non-zero quantity and then quantity
# becomes zero.
amount._setQuantity(quantity)
if 0:
print 'getAggregatedAmountList(%r) -> (%s)' % (
self.getRelativeUrl(),
', '.join('(%s, %s, %s)'
% (x.getResourceTitle(), x.getQuantity(), x.getPrice())
for x in result_list))
return result_list
InitializeClass(AmountGeneratorMixin) InitializeClass(AmountGeneratorMixin)
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