GeneratedAmountList.py 4.63 KB
Newer Older
1
# -*- coding: utf-8 -*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
##############################################################################
#
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#                    Łukasz Nowak <luke@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

30
from collections import defaultdict
31
import zope.interface
32
from AccessControl import allow_class
33
from Products.ERP5Type import interfaces
34

35
class GeneratedAmountList(list):
36 37 38
  """
    Temporary object needed to aggregate Amount value
    And to calculate some report or total value
39 40 41 42 43 44 45

    For example, delivery.getGeneratedAmountList() returns an object of this
    type, with amounts for each movement and/or for the delivery. This result
    can be used to get:
    1. totals for the delivery, by first using aggregate()
    2. detailed information on each movement with split(), which would be
       equivalent to call getGeneratedAmountList() on each movement
46
  """
47
  zope.interface.implements(interfaces.IAmountList)
48

49 50 51 52
  def getTotalPrice(self):
    """
      Return total base price
    """
53 54 55 56 57
    result = 0
    for amount in self:
      total_price = amount.getTotalPrice()
      if total_price:
        result += total_price
58 59 60 61 62 63
    return result

  def getTotalDuration(self):
    """
      Return total duration
    """
64 65 66 67 68
    result = 0
    for amount in self:
      duration = amount.getDuration()
      if duration:
        result += duration
69 70
    return result

71
  def aggregate(self):
72 73 74 75 76
    """Return a list of aggregated amounts

    Groups amounts with same price, efficiency, reference and categories, merge
    them by summing their quantities, and return the new amounts in a new list.
    """
77 78 79 80 81 82 83 84 85 86 87 88
    # 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()]
      else:
        aggregate[1] += amount.getQuantity()
89
    from erp5.component.document.RoundingModel import RoundingProxy
90 91 92 93
    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.
94 95 96 97
      aggregate = amount.aq_parent.newContent(temp_object=True,
                                              portal_type='Amount',
                                              id='',
                                              notify_workflow=False)
98
      aggregate.__dict__.update(amount.aq_base.__dict__)
99
      aggregate._setQuantity(quantity)
100 101 102 103 104 105
      if isinstance(amount, RoundingProxy):
        aggregate = amount.getPortalObject().portal_roundings.getRoundingProxy(
          aggregate)
      else:
        del aggregate._base
      result_list.append(aggregate)
106 107
    return result_list

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  def split(self):
    """Return a dictionary with all amounts grouped by base amount

    Return {amount: amount_list} where
      - amount is the Amount instance (e.g. movement, delivery)
        that generated amounts
      - amount_list is an instance of this class

    This is the opposite of aggregate(), which merges amounts that only differ
    by their base amounts.
    """
    result = defaultdict(self.__class__)
    for amount in self:
      result[amount._base].append(amount)
    return result

124
allow_class(GeneratedAmountList)