Order.py 5.52 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2
##############################################################################
#
Romain Courteaud's avatar
Romain Courteaud committed
3
# Copyright (c) 2002, 2005 Nexedi SARL and Contributors. All Rights Reserved.
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
Romain Courteaud's avatar
Romain Courteaud committed
5
#                    Romain Courteaud <romain@nexedi.com>
Jean-Paul Smets's avatar
Jean-Paul Smets committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#
# 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.
#
##############################################################################

from AccessControl import ClassSecurityInfo
31
from Products.ERP5Type import Permissions, PropertySheet
32
from Products.ERP5Type.Utils import deprecated
33
from Products.ERP5.Document.Delivery import Delivery
Jean-Paul Smets's avatar
Jean-Paul Smets committed
34

35
from warnings import warn
Jean-Paul Smets's avatar
Jean-Paul Smets committed
36 37 38 39 40 41 42 43

class Order(Delivery):
    # CMF Type Definition
    meta_type = 'ERP5 Order'
    portal_type = 'Order'

    # Declarative security
    security = ClassSecurityInfo()
44
    security.declareObjectProtected(Permissions.AccessContentsInformation)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
45 46 47 48 49 50 51 52 53 54

    # Default Properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      , PropertySheet.Task
                      , PropertySheet.Arrow
                      , PropertySheet.Reference
                      , PropertySheet.TradeCondition
Romain Courteaud's avatar
Romain Courteaud committed
55 56
                      , PropertySheet.Comment
                      , PropertySheet.Order
Jean-Paul Smets's avatar
Jean-Paul Smets committed
57 58
                      )

Romain Courteaud's avatar
Romain Courteaud committed
59 60
    security.declareProtected(Permissions.AccessContentsInformation, \
                                                   'isAccountable')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
61 62 63 64 65 66 67
    def isAccountable(self):
      """
        Returns 1 if this needs to be accounted
        Only account movements which are not associated to a delivery
        Whenever delivery is there, delivery has priority
      """
      return 0
68

69
    def getTotalPrice(self, **kw) :
70 71 72 73 74
      """Returns the total price for this Order.

      If base_contribution is passed, the trade model lines will be used to
      include movements that will be generated.
      """
75 76
      if kw.get('fast'):
        kw['only_accountable'] = False
77
      rounding = kw.get('rounding')
78

79 80 81 82 83
      if kw.get('base_contribution') is None:
        kw.setdefault('portal_type', self.getPortalOrderMovementTypeList())
        return Delivery.getTotalPrice(self, **kw)
      else:
        base_contribution = kw.get('base_contribution')
84
        # Find amounts from movements in the delivery.
85 86 87 88 89 90 91 92 93 94 95 96 97
        if isinstance(base_contribution, (tuple, list)):
          base_contribution_list = base_contribution
        else:
          base_contribution_list = (base_contribution,)
        base_contribution_value_list = []
        portal_categories = self.portal_categories
        for relative_url in base_contribution_list:
          base_contribution_value = portal_categories.getCategoryValue(relative_url)
          if base_contribution_value is not None:
            base_contribution_value_list.append(base_contribution_value)
        if not base_contribution_value_list:
          # We cannot find any amount so that the result is 0.
          return 0
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

        movement_list = self.getMovementList()


        if rounding:
          portal_roundings = self.portal_roundings
          movement_list = [
              portal_roundings.getRoundingProxy(movement)
                for movement in movement_list]

        trade_model_line_list = self.getAggregatedAmountList(rounding=rounding)
        matched_model_line_list = [ line for line in trade_model_line_list
              if set(line.getBaseApplicationValueList()).intersection(base_contribution_value_list)]

        result = sum([movement.getTotalPrice()
                          for movement in movement_list])
        result += sum([line.getTotalPrice()
                        for line in matched_model_line_list])
        return result
117

118
    def getTotalQuantity(self, **kw) :
119 120
      """Returns the total quantity for this Order. """
      kw.setdefault('portal_type', self.getPortalOrderMovementTypeList())
121 122
      if kw.get('fast'):
        kw['only_accountable'] = False
123
      return Delivery.getTotalQuantity(self, **kw)
124 125 126 127

    @deprecated
    def applyToOrderRelatedMovement(self, method_id='expand', **kw):
      # WARNING: does not work if it was not catalogued immediately
128
      # 'order' category is deprecated. it is kept for compatibility.
129
      for m in self.getMovementList():
130
        for my_simulation_movement in m.getDeliveryRelatedValueList(
131
            portal_type='Simulation Movement') or \
132
            m.getOrderRelatedValueList(
133
            portal_type='Simulation Movement'):
134
          getattr(my_simulation_movement, method_id)(**kw)