SupplyLine.py 9.94 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2
##############################################################################
#
3
# Copyright (c) 2002, 2005 Nexedi SARL and Contributors. All Rights Reserved.
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
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, Constraint, interfaces
32
from Products.ERP5Type.XMLMatrix import XMLMatrix
Jean-Paul Smets's avatar
Jean-Paul Smets committed
33 34
from Products.ERP5.Document.DeliveryLine import DeliveryLine
from Products.ERP5.Document.Path import Path
35
from Products.ERP5Type.Utils import convertToUpperCase
Sebastien Robin's avatar
Sebastien Robin committed
36

37
from zLOG import LOG
Jean-Paul Smets's avatar
Jean-Paul Smets committed
38 39

class SupplyLine(DeliveryLine, Path):
40
    """A Supply Line is a path to define price 
Jean-Paul Smets's avatar
Jean-Paul Smets committed
41 42 43 44
    """

    meta_type = 'ERP5 Supply Line'
    portal_type = 'Supply Line'
45
    add_permission = Permissions.AddPortalContent
Jean-Paul Smets's avatar
Jean-Paul Smets committed
46 47
    isPortalContent = 1
    isRADContent = 1
48
    isPredicate = 1
Jean-Paul Smets's avatar
Jean-Paul Smets committed
49 50 51

    # Declarative security
    security = ClassSecurityInfo()
52
    security.declareObjectProtected(Permissions.AccessContentsInformation)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
53 54

    # Declarative interfaces
55 56
    __implements__ = ( interfaces.IVariated,
                       interfaces.IPredicate )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
57 58 59 60 61 62 63 64 65 66

    # Declarative properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.Amount
                      , PropertySheet.Task
                      , PropertySheet.Arrow
                      , PropertySheet.Movement
                      , PropertySheet.Price
67
                      , PropertySheet.SupplyLine
Jean-Paul Smets's avatar
Jean-Paul Smets committed
68 69 70
                      , PropertySheet.VariationRange
                      , PropertySheet.Path
                      , PropertySheet.FlowCapacity
71
                      , PropertySheet.Predicate
72
                      , PropertySheet.Comment
Jean-Paul Smets's avatar
Jean-Paul Smets committed
73 74
                      )

75
    #############################################
Jean-Paul Smets's avatar
Jean-Paul Smets committed
76
    # Pricing methods
77
    #############################################
78
    security.declareProtected(Permissions.AccessContentsInformation,
79
                              'getPrice')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
80 81 82 83 84 85 86
    def getPrice(self):
      # If price not defined, look up price
      if getattr(self, 'price', None) is None:
        self.price = 0.0
      # Return the price
      return self.price

87
    security.declareProtected(Permissions.AccessContentsInformation,
88
                              'getTotalPrice')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
89 90 91 92
    def getTotalPrice(self):
      """
        Returns the totals price for this line
      """
93 94 95
      quantity = self.getQuantity() or 0.0
      price = self.getPrice() or 0.0
      return quantity * price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
96 97 98 99 100 101 102 103 104 105 106 107 108

    def _getPrice(self, context):
       return 0.0

    def _getDefaultPrice(self, context):
       return 0.0

    def _getTotalPrice(self, context):
      return 0.0

    def _getDefaultTotalPrice(self, context):
      return 0.0

109
    security.declareProtected(Permissions.AccessContentsInformation,
110
                              'isAccountable')
111
    def isAccountable(self):
112
      """Supply Line are not accounted.
113 114 115
      """
      return 0

116
    security.declareProtected(Permissions.AccessContentsInformation,
117
                              'getDefaultPrice')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
118 119 120
    def getDefaultPrice(self, context=None, REQUEST=None, **kw):
      """
      """
121
      return self._getDefaultPrice(self.asContext(context=context,
122
                                                  REQUEST=REQUEST, **kw))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
123

124
    security.declareProtected(Permissions.AccessContentsInformation,
125
                              'getDefaultTotalPrice')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
126 127 128
    def getDefaultTotalPrice(self, context=None, REQUEST=None, **kw):
      """
      """
129
      return self._getDefaultTotalPrice(self.asContext(context=context,
130
                                                       REQUEST=REQUEST, **kw))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
131

132 133 134 135 136 137 138 139 140
    #############################################
    # Predicate method
    #############################################
    asPredicate = Path.asPredicate

    #############################################
    # XMLMatrix methods
    # XXX to be removed if possible
    #############################################
141 142
    security.declareProtected(Permissions.AccessContentsInformation,
                              'hasCellContent')
Sebastien Robin's avatar
Sebastien Robin committed
143 144 145 146
    def hasCellContent(self, base_id='path'):
      """
          This method can be overriden
      """
147
      return XMLMatrix.hasCellContent(self, base_id=base_id)
Sebastien Robin's avatar
Sebastien Robin committed
148

149
    security.declareProtected(Permissions.AccessContentsInformation,
150 151 152 153 154 155 156
                              'getCellValueList' )
    def getCellValueList(self, base_id='path'):
      """
          This method can be overriden
      """
      return XMLMatrix.getCellValueList(self, base_id=base_id)

157
    security.declareProtected(Permissions.AccessContentsInformation, 'getCell')
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    def getCell(self, *kw , **kwd):
      """
          This method can be overriden
      """
      if 'base_id' not in kwd:
        kwd['base_id'] = 'path'
      return XMLMatrix.getCell(self, *kw, **kwd)

    security.declareProtected(Permissions.ModifyPortalContent, 'newCell')
    def newCell(self, *kw, **kwd):
      """
          This method creates a new cell
      """
      if 'base_id' not in kwd:
        kwd['base_id'] = 'path'
      return XMLMatrix.newCell(self, *kw, **kwd)
174

175 176 177
    ############################################################
    # Quantity predicate API
    ############################################################
178
    security.declareProtected(Permissions.AccessContentsInformation,
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
                              'getQuantityPredicateIdList')
    def getQuantityPredicateIdList(self, price_parameter):
      """
        Return predicate id related to a price parameter.
      """
      predicate_id_start_with = "quantity_range_"
      if price_parameter != "base_price":
        predicate_id_start_with = "%s_%s" % \
            (price_parameter, predicate_id_start_with)
      # XXX Hardcoded portal type name
      predicate_list = self.contentIds(filter={'portal_type': 'Predicate'})
      # XXX filtering can be done with contentIds ?
      result = [x for x in predicate_list \
              if x.startswith(predicate_id_start_with)]
      return result
194

195
    security.declareProtected(Permissions.AccessContentsInformation,
196 197 198 199 200 201 202 203 204
                              'getQuantityPredicateValueList')
    def getQuantityPredicateValueList(self, price_parameter):
      """
        Return predicate related to a price parameter.
      """
      result = [getattr(self, x) for x in \
              self.getQuantityPredicateIdList(price_parameter)]
      return result

205 206
    security.declareProtected(Permissions.AccessContentsInformation,
                              'getQuantityStepList')
Romain Courteaud's avatar
Romain Courteaud committed
207
    def getQuantityStepList(self, *args, **kw):
208 209 210
      """
        Return predicate step related to a price_parameter
      """
Romain Courteaud's avatar
Romain Courteaud committed
211 212
      # We need to keep compatibility with generated accessor
      price_parameter = kw.get('price_parameter', "base_price")
213 214 215 216 217 218 219
      if price_parameter == "base_price":
        method_name = "_baseGetQuantityStepList"
      else:
        method_name = 'get%sList' % \
                     convertToUpperCase("%s_quantity_step" % price_parameter)
      return getattr(self, method_name)()

220
    security.declareProtected(Permissions.ModifyPortalContent,
221
                              'updateQuantityPredicate')
222 223
    def updateQuantityPredicate(self, price_parameter):
      """
224
        Update the quantity predicate for this price parameter
225
      """
Romain Courteaud's avatar
Romain Courteaud committed
226
      quantity_step_list = self.getQuantityStepList(price_parameter=price_parameter)
227 228 229
      quantity_step_list.sort()
      
      # remove old predicates
230
      for pid in self.getQuantityPredicateIdList(price_parameter):
231
        self.deleteContent(pid)
232 233 234 235
      
      if quantity_step_list:
        quantity_step_list = [None] + quantity_step_list + [None]
        # With this script, we can change the title of the predicate
236
        script = getattr(self, 'SupplyLine_getTitle', None)
237 238 239 240
        predicate_id_start_with = "quantity_range"
        if price_parameter != "base_price":
          predicate_id_start_with = "%s_%s" % \
              (price_parameter, predicate_id_start_with)
241 242 243
        for i in range(0, len(quantity_step_list)-1):
          min_quantity = quantity_step_list[i]
          max_quantity = quantity_step_list[i+1]
244
          # XXX Hardcoded portal type name
245
          p = self.newContent(id='%s_%s' % (predicate_id_start_with, str(i)),
246
                              portal_type = 'Predicate')
247
          p.setCriterionPropertyList(('quantity', ))
248
          p.setCriterion('quantity', min=min_quantity, max=max_quantity)
249
          if script is not None:
250
            title = script(min=min_quantity, max=max_quantity)
251 252
            p.setTitle(title)
          else:
253 254 255 256
            if min_quantity is None:
              p.setTitle('quantity < %s' % max_quantity)
            elif max_quantity is None:
              p.setTitle('%s <= quantity' % min_quantity)
257
            else:
258 259 260
              p.setTitle('%s <= quantity < %s' % (min_quantity, max_quantity))