Resource.py 27.9 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 31 32 33 34
#
# 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

from DateTime import DateTime

from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
Jean-Paul Smets's avatar
Jean-Paul Smets committed
35
from Products.ERP5Type.XMLMatrix import XMLMatrix
Jean-Paul Smets's avatar
Jean-Paul Smets committed
36 37 38

from Products.ERP5.Variated import Variated
from Products.ERP5.Core.Resource import Resource as CoreResource
Sebastien Robin's avatar
Sebastien Robin committed
39
from Products.CMFCore.WorkflowCore import WorkflowMethod
40
from Products.CMFCategory.Renderer import Renderer
41
from Products.CMFCore.utils import getToolByName
Jean-Paul Smets's avatar
Jean-Paul Smets committed
42 43 44

from zLOG import LOG

Jean-Paul Smets's avatar
Jean-Paul Smets committed
45
class Resource(XMLMatrix, CoreResource, Variated):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
46 47 48 49 50 51
    """
      A Resource
    """

    meta_type = 'ERP5 Resource'
    portal_type = 'Resource'
52
    add_permission = Permissions.AddPortalContent
Jean-Paul Smets's avatar
Jean-Paul Smets committed
53 54 55 56 57
    isPortalContent = 1
    isRADContent = 1

    # Declarative security
    security = ClassSecurityInfo()
58
    security.declareObjectProtected(Permissions.AccessContentsInformation)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
59 60 61 62 63 64 65 66 67 68 69 70

    # Declarative interfaces
    __implements__ = ( Interface.Variated, )

    # Declarative properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      , PropertySheet.Price
                      , PropertySheet.Resource
                      , PropertySheet.Reference
71
                      , PropertySheet.FlowCapacity
Sebastien Robin's avatar
Sebastien Robin committed
72
                      , PropertySheet.VariationRange
73
                      , PropertySheet.DefaultSupply
Jean-Paul Smets's avatar
Jean-Paul Smets committed
74 75 76 77 78 79 80
                      )

    # Is it OK now ?
    # The same method is at about 3 different places
    # Some genericity is needed
    security.declareProtected(Permissions.AccessContentsInformation,
                                           'getVariationRangeCategoryItemList')
81 82 83
    def getVariationRangeCategoryItemList(self, base_category_list=(), base=1, 
                                          root=1, display_id='title', 
                                          display_base_category=1,
84
                                          current_category=None, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
85 86
        """
          Returns possible variations
87 88 89

          resource.getVariationRangeCategoryItemList
            => [(display, value)]
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        
      ## Variation API (exemple) ##
        Base categories defined:
          - colour
          - morphology
          - size
        Categories defined:
          - colour/blue
          - colour/red
          - size/Man
          - size/Woman
        Resource 'resource' created with variation_base_category_list:
            (colour, morphology, size)

        resource.getVariationRangeCategoryList
        variation   | individual variation | result
        ____________________________________________________________________________________
                    |                      | (colour/blue, colour/red, size/Man, size/Woman)
        size/Man    |                      | (colour/blue, colour/red, size/Man, size/Woman)
        colour/blue |                      | (colour/blue, colour/red, size/Man, size/Woman)
                    |  colour/1            | (colour/1, size/Man, size/Woman)
                    |  morphology/2        | (colour/blue, colour/red, size/Man, size/Woman, morphology/2)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
112
        """
113
        result = []
Jean-Paul Smets's avatar
Jean-Paul Smets committed
114 115 116 117
        if base_category_list is ():
          base_category_list = self.getVariationBaseCategoryList()
        elif type(base_category_list) is type('a'):
          base_category_list = (base_category_list,)
118

119
        other_base_category_dict = dict([(i,1) for i in base_category_list])
Yoshinori Okuji's avatar
Yoshinori Okuji committed
120
        other_variations = self.searchFolder( \
121 122
                               portal_type=self.getPortalVariationTypeList(),
                               sort_on=[('title','ascending')])
Yoshinori Okuji's avatar
Yoshinori Okuji committed
123 124
        other_variations = [x.getObject() for x in other_variations]
        other_variations = [x for x in other_variations if x is not None]
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

        for object in other_variations:
          for base_category in object.getVariationBaseCategoryList():
            if (base_category_list is ()) or \
               (base_category in base_category_list):
              other_base_category_dict[base_category] = 0
              # XXX now, call Renderer a lot of time.
              # Better implementation needed
              result.extend(Renderer(
                                   base_category=base_category, 
                                   display_base_category=display_base_category,
                                   display_none_category=0, base=base,
                                   current_category=current_category,
                                   display_id=display_id).\
                                                     render([object]))
140

141 142 143 144
        other_base_category_item_list = filter(lambda x: x[1]==1, 
            other_base_category_dict.items())
        other_base_category_list = map(lambda x: x[0],
            other_base_category_item_list)
145 146 147 148
        # Get category variation
        if len(other_base_category_list) != 0:
          result += Variated.getVariationRangeCategoryItemList(
              self, base_category_list=other_base_category_list,
149
              base=base, display_base_category=display_base_category, **kw)
150
        # Return result
151
        return result
152

Jean-Paul Smets's avatar
Jean-Paul Smets committed
153 154
    security.declareProtected(Permissions.AccessContentsInformation,
                                           'getVariationCategoryItemList')
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    def getVariationCategoryItemList(self, base_category_list=(), 
                                     omit_individual_variation=1, base=1,
                                     current_category=None,
                                     display_base_category=1,
                                     display_id='title', **kw):
      """
        Returns variations of the resource.
        If omit_individual_variation==1, does not return individual 
        variation.
        Else, returns them.
        Display is on left.
            => [(display, value)]

        *old parameters: base=1, current_category=None, 
                         display_id='getTitle' (default value getTitleOrId)
      """
      result = Variated.getVariationCategoryItemList(self, 
                            base_category_list=base_category_list, 
173 174
                            display_base_category=display_base_category, 
                            base=base, **kw)
175
      if not omit_individual_variation:
176 177
        other_variations = self.searchFolder(
                              portal_type=self.getPortalVariationTypeList())
178 179 180 181 182 183 184 185 186 187 188 189

        other_variations = map(lambda x: x.getObject(), other_variations)
        other_variations = filter(lambda x: x is not None, other_variations)

        for object in other_variations:
          for base_category in object.getVariationBaseCategoryList():
            if (base_category_list is ()) or \
               (base_category in base_category_list):
              # XXX append object, relative_url ?
              # XXX now, call Renderer a lot of time.
              # Better implementation needed
              result.extend(Renderer(
190 191 192 193 194 195
                             base_category=base_category, 
                             display_base_category=display_base_category,
                             display_none_category=0, base=base,
                             current_category=current_category,
                             display_id=display_id, **kw).\
                                               render([object]))
196 197 198
      return result

    security.declareProtected(Permissions.AccessContentsInformation,
199
                              'getVariationCategoryList')
200
    def getVariationCategoryList(self, base_category_list=(),
201
                                 omit_individual_variation=1, **kw):
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
      """
        Returns variations of the resource.
        If omit_individual_variation==1, does not return individual 
        variation.
        Else, returns them.

        ## Variation API (exemple) ##
        Base categories defined:
          - colour
          - morphology
          - size
        Categories defined:
          - colour/blue
          - colour/red
          - size/Man
          - size/Woman
        Resource 'resource' created with variation_base_category_list:
            (colour, morphology, size)

        resource.getVariationCategoryList
        variation   | individual variation | result
        _____________________________________________________
                    |                      | ()
        size/Man    |                      | (size/Man, )
        colour/blue |                      | (colour/blue, )
                    |  colour/1            | (colour/1, )
                    |  morphology/2        | (morphology/2, )
      """
      vcil = self.getVariationCategoryItemList(
231 232
                    base_category_list=base_category_list,
                    omit_individual_variation=omit_individual_variation,**kw)
233
      return map(lambda x: x[1], vcil)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
234 235 236 237 238 239 240 241 242 243 244 245

    # Unit conversion
    security.declareProtected(Permissions.AccessContentsInformation, 'convertQuantity')
    def convertQuantity(self, quantity, from_unit, to_unit):
      return quantity

# This patch is temporary and allows to circumvent name conflict in ZSQLCatalog process for Coramy
    security.declareProtected(Permissions.AccessContentsInformation,
                                              'getDefaultDestinationAmountBis')
    def getDefaultDestinationAmountBis(self, unit=None, variation=None, REQUEST=None):
      try:
        return self.getDestinationReference()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
246
      except AttributeError:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
247 248 249 250 251 252 253 254
        return None

# This patch is temporary and allows to circumvent name conflict in ZSQLCatalog process for Coramy
    security.declareProtected(Permissions.AccessContentsInformation,
                                              'getDefaultSourceAmountBis')
    def getDefaultSourceAmountBis(self, unit=None, variation=None, REQUEST=None):
      try:
        return self.getSourceReference()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
255
      except AttributeError:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
256 257 258 259 260 261 262 263 264 265
        return None


    # This patch allows variations to find a resource
    security.declareProtected(Permissions.AccessContentsInformation,
                                              'getDefaultResourceValue')
    def getDefaultResourceValue(self):
      return self


Romain Courteaud's avatar
Romain Courteaud committed
266
    ####################################################
Jean-Paul Smets's avatar
Jean-Paul Smets committed
267
    # Stock Management
Romain Courteaud's avatar
Romain Courteaud committed
268 269 270
    ####################################################
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getInventory')
271
    def getInventory(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
272
      """
273
      Returns inventory
Jean-Paul Smets's avatar
Jean-Paul Smets committed
274
      """
275 276
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getInventory(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
277

Romain Courteaud's avatar
Romain Courteaud committed
278 279
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getCurrentInventory')
280
    def getCurrentInventory(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
281
      """
282
      Returns current inventory
Jean-Paul Smets's avatar
Jean-Paul Smets committed
283
      """
284 285
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getCurrentInventory(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
286

Romain Courteaud's avatar
Romain Courteaud committed
287 288
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getAvailableInventory')
289
    def getAvailableInventory(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
290
      """
291 292
      Returns available inventory
      (current inventory - deliverable)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
293
      """
294 295
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getAvailableInventory(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
296

Romain Courteaud's avatar
Romain Courteaud committed
297 298
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getFutureInventory')
299
    def getFutureInventory(self, **kw):
300 301 302 303 304
      """
      Returns inventory at infinite
      """
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getFutureInventory(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
305

Romain Courteaud's avatar
Romain Courteaud committed
306 307
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getInventoryList')
308 309 310 311 312 313
    def getInventoryList(self, **kw):
      """
      Returns list of inventory grouped by section or site
      """
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getInventoryList(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
314

Romain Courteaud's avatar
Romain Courteaud committed
315 316
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getCurrentInventoryList')
317
    def getCurrentInventoryList(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
318
      """
319
      Returns list of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
320
      """
321 322
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getCurrentInventoryList(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
323

Romain Courteaud's avatar
Romain Courteaud committed
324 325
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getFutureInventoryList')
326
    def getFutureInventoryList(self, **kw):
327 328 329 330 331
      """
      Returns list of inventory grouped by section or site
      """
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getFutureInventoryList(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
332

Romain Courteaud's avatar
Romain Courteaud committed
333 334
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getInventoryStat')
335
    def getInventoryStat(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
336
      """
337
      Returns statistics of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
338
      """
339 340
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getInventoryStat(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
341

Romain Courteaud's avatar
Romain Courteaud committed
342 343
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getCurrentInventoryStat')
344
    def getCurrentInventoryStat(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
345
      """
346
      Returns statistics of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
347
      """
348 349
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getCurrentInventoryStat(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
350

Romain Courteaud's avatar
Romain Courteaud committed
351 352
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getFutureInventoryStat')
353
    def getFutureInventoryStat(self, **kw):
354 355 356 357 358
      """
      Returns statistics of inventory grouped by section or site
      """
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getFutureInventoryStat(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
359

Romain Courteaud's avatar
Romain Courteaud committed
360 361
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getInventoryChart')
362
    def getInventoryChart(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
363
      """
364
      Returns list of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
365
      """
366 367
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getInventoryChart(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
368

Romain Courteaud's avatar
Romain Courteaud committed
369 370
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getCurrentInventoryChart')
371
    def getCurrentInventoryChart(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
372
      """
373
      Returns list of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
374
      """
375 376
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getCurrentInventoryChart(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
377

Romain Courteaud's avatar
Romain Courteaud committed
378 379
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getFutureInventoryChart')
380
    def getFutureInventoryChart(self, **kw):
381 382 383 384 385
      """
      Returns list of inventory grouped by section or site
      """
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getFutureInventoryChart(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
386

Romain Courteaud's avatar
Romain Courteaud committed
387 388
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getInventoryHistoryList')
389
    def getInventoryHistoryList(self, **kw):
390 391 392 393 394
      """
      Returns list of inventory grouped by section or site
      """
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getInventoryHistoryList(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
395

Romain Courteaud's avatar
Romain Courteaud committed
396 397
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getInventoryHistoryChart')
398
    def getInventoryHistoryChart(self, **kw):
399 400 401 402 403
      """
      Returns list of inventory grouped by section or site
      """
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getInventoryHistoryChart(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
404

405 406 407 408
    # XXX FIXME
    # Method getCurrentMovementHistoryList, 
    # getAvailableMovementHistoryList, getFutureMovementHistoryList
    # can be added
Romain Courteaud's avatar
Romain Courteaud committed
409 410
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getMovementHistoryList')
411
    def getMovementHistoryList(self, **kw):
412 413 414 415 416
      """
      Returns list of inventory grouped by section or site
      """
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getMovementHistoryList(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
417

Romain Courteaud's avatar
Romain Courteaud committed
418 419
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getMovementHistoryStat')
420
    def getMovementHistoryStat(self, **kw):
421 422 423 424 425
      """
      Returns list of inventory grouped by section or site
      """
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getMovementHistoryStat(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
426

Romain Courteaud's avatar
Romain Courteaud committed
427 428
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getNextNegativeInventoryDate')
429
    def getNextNegativeInventoryDate(self, **kw):
430 431 432 433 434
      """
      Returns list of inventory grouped by section or site
      """
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getNextNegativeInventoryDate(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
435 436


437
    # Asset Price API
Jean-Paul Smets's avatar
Jean-Paul Smets committed
438
    security.declareProtected(Permissions.AccessContentsInformation, 'getInventoryAssetPrice')
439
    def getInventoryAssetPrice(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
440
      """
441
      Returns list of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
442
      """
443 444
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getInventoryAssetPrice(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
445 446

    security.declareProtected(Permissions.AccessContentsInformation, 'getCurrentInventoryAssetPrice')
447
    def getCurrentInventoryAssetPrice(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
448
      """
449
      Returns list of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
450
      """
451 452
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getCurrentInventoryAssetPrice(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
453 454

    security.declareProtected(Permissions.AccessContentsInformation, 'getAvailableInventoryAssetPrice')
455
    def getAvailableInventoryAssetPrice(self, **kw):
456 457 458 459 460 461 462
      """
      Returns list of inventory grouped by section or site
      """
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getAvailableInventoryAssetPrice(**kw)

    security.declareProtected(Permissions.AccessContentsInformation, 'getFutureInventoryAssetPrice')
463
    def getFutureInventoryAssetPrice(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
464
      """
465
      Returns list of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
466
      """
467 468 469
      kw['resource'] = self.getRelativeUrl()
      return self.portal_simulation.getFutureInventoryAssetPrice(**kw)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
470

471
    # Industrial price API
472 473 474 475 476 477 478 479 480 481 482 483 484
    security.declareProtected(Permissions.AccessContentsInformation, 'getIndustrialPrice')
    def getIndustrialPrice(self, context=None, REQUEST=None, **kw):
      """
        Returns industrial price
      """
      context = self.asContext(context=context, REQUEST=REQUEST, **kw)
      result = self._getIndustrialPrice(context)
      return result

    def _getIndustrialPrice(self, context):
      # Default value is None
      return None

485
    # Predicate handling
486 487
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'asPredicate')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
488 489 490 491 492 493 494 495 496
    def asPredicate(self):
      """
      Returns a temporary Predicate based on the Resource properties
      """
      from Products.ERP5 import newTempPredicateGroup as newTempPredicate
      p = newTempPredicate(self.getId(), uid = self.getUid())
      p.setMembershipCriterionBaseCategoryList(('resource',))
      p.setMembershipCriterionCategoryList(('resource/%s' % self.getRelativeUrl(),))
      return p
497

498 499 500 501
    def _pricingSortMethod(self, a, b):
      # Simple method : the one that matches the highest number of criterions wins
      return cmp(len(b.getAcquiredCategoryList()), len(a.getAcquiredCategoryList()))

502
    security.declareProtected(Permissions.AccessContentsInformation, 
503 504
                              '_getPriceParameterDict')
    def _getPriceParameterDict(self, context=None, REQUEST=None, **kw):
505
      """
506
      Get all pricing parameters from Predicate.
507
      """
508
      # Search all categories context
509
      new_category_list = []
510
      if context is not None:
511
        new_category_list += context.getCategoryList()
512 513
      #XXX This should be 'category_list' instead of 'categories' to respect
      # the naming convention. Must take care of side effects when fixing
514 515 516
      if kw.has_key('categories'):
        new_category_list.extend(kw['categories'])
        del kw['categories']
517 518
      resource_category = 'resource/' + self.getRelativeUrl()
      if not resource_category in new_category_list:
519
        new_category_list += (resource_category, )
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
      # Generate a mapped value without option, and one for each option
      # Separate option from new_category_list
      option_base_category_list = self.getPortalOptionBaseCategoryList()
      option_category_list = []
      no_option_category_list = []
      for new_category in new_category_list:
        is_option = 0
        for option_base_category in option_base_category_list:
          if new_category.startswith(option_base_category):
            is_option = 1
            break
        if is_option:
          option_category_list.append(new_category)
        else:
          no_option_category_list.append(new_category)
535 536
      # Generate the predicate mapped value
      # to get some price values.
537
      mapped_value_list = []
538
      domain_tool = getToolByName(self,'portal_domains')
539
      portal_type_list = self.getPortalSupplyPathTypeList()
540 541 542 543 544 545 546 547 548 549 550

      category_list_list = [no_option_category_list] + \
          [no_option_category_list+[x] for x in option_category_list]
      for category_list in category_list_list:
        # Generate the fake context
        tmp_context = self.asContext(context=context, 
                                     categories=category_list,
                                     REQUEST=REQUEST, **kw)
        mapped_value = domain_tool.generateMappedValue(
                                               tmp_context,
                                               portal_type=portal_type_list,
551
                                               sort_method=self._pricingSortMethod,
552 553 554
                                               has_cell_content=0, **kw)
        if mapped_value is not None:
          mapped_value_list.append(mapped_value)
555 556 557
      # Get price parameters
      price_parameter_dict = {
        'base_price': None,
558 559 560
        'additional_price': [],
        'surcharge_ratio': [],
        'discount_ratio': [],
561
        'exclusive_discount_ratio': None,
562 563
        'variable_additional_price': [],
        'non_discountable_additional_price': [],
564
      }
565
      for mapped_value in mapped_value_list:
566
        for price_parameter_name in price_parameter_dict.keys():
567
          price_parameter_value = \
568
            mapped_value.getProperty(price_parameter_name)
569 570 571 572 573 574 575 576 577 578
          if price_parameter_value not in [None, '']:
            try:
              price_parameter_dict[price_parameter_name].append(
                                              price_parameter_value)
            except AttributeError:
              if price_parameter_dict[price_parameter_name] is None:
                price_parameter_dict[price_parameter_name] = \
                                                price_parameter_value
      return price_parameter_dict
      
579 580
    security.declareProtected(Permissions.AccessContentsInformation,
        '_getPricingVariable')
581
    def _getPricingVariable(self, context=None):
582 583 584 585
      """
      Return the value of the property used to calculate variable pricing
      This basically calls a script like Product_getPricingVariable
      """
586 587 588 589 590
      if context is not None:
        method = context._getTypeBasedMethod('_getPricingVariable')
      if method is None or context is None:
        method = self._getTypeBasedMethod('_getPricingVariable')

591 592 593 594
      if method is None:
        return 0.0
      return float(method())

595 596 597 598 599 600 601 602 603 604
    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getPrice')
    def getPrice(self, context=None, REQUEST=None, **kw):
      """
      Return the unit price of a resource in a specific context.
      """
      price_parameter_dict = self._getPriceParameterDict(
                                     context=context, REQUEST=REQUEST, **kw)
      # Calculate the unit price
      unit_base_price = None
605
      # Calculate
606 607 608 609 610 611 612 613 614 615 616 617
#     ((base_price + SUM(additional_price) +
#     variable_value * SUM(variable_additional_price)) *
#     (1 - MIN(1, MAX(SUM(discount_ratio) , exclusive_discount_ratio ))) +
#     SUM(non_discountable_additional_price)) *
#     (1 + SUM(surcharge_ratio))
      # Or, as one single line :
#     ((bp + S(ap) + v * S(vap)) * (1 - m(1, M(S(dr), edr))) + S(ndap)) * (1 + S(sr))
      # Variable value is dynamically configurable through a python script.
      # It can be anything, depending on business requirements.
      # It can be seen as a way to define a pricing model that not only
      # depends on discrete variations, but also on a continuous property of the object

618
      base_price = price_parameter_dict['base_price']
619
      if base_price in [None, '']:
620 621
        # XXX Compatibility
        # base_price must not be defined on resource
622 623
        base_price = self.getBasePrice()
      if base_price not in [None, '']:
624 625
        unit_base_price = base_price
        # Sum additional price
626
        for additional_price in price_parameter_dict['additional_price']:
627
          unit_base_price += additional_price
628
        # Sum variable additional price
629
        variable_value = self._getPricingVariable(context=context)
630 631
        for variable_additional_price in price_parameter_dict['variable_additional_price']:
          unit_base_price += variable_additional_price * variable_value
632
        # Discount
633 634 635
        sum_discount_ratio = 0
        for discount_ratio in price_parameter_dict['discount_ratio']:
          sum_discount_ratio += discount_ratio
636 637 638
        exclusive_discount_ratio = \
            price_parameter_dict['exclusive_discount_ratio']
        d_ratio = 0
639
        d_ratio = max(d_ratio, sum_discount_ratio)
640 641 642 643 644
        if exclusive_discount_ratio not in [None, '']:
          d_ratio = max(d_ratio, exclusive_discount_ratio)
        if d_ratio != 0:
          d_ratio = 1 - min(1, d_ratio)
          unit_base_price = unit_base_price * d_ratio
645 646 647 648 649 650 651 652 653
        # Sum non discountable additional price
        for non_discountable_additional_price in\
            price_parameter_dict['non_discountable_additional_price']:
          unit_base_price += non_discountable_additional_price
        # Surcharge ratio
        sum_surcharge_ratio = 1
        for surcharge_ratio in price_parameter_dict['surcharge_ratio']:
          sum_surcharge_ratio += surcharge_ratio
        unit_base_price = unit_base_price * sum_surcharge_ratio
654 655
      # Divide by the priced quantity
      if unit_base_price is not None:
656
        priced_quantity = self.getPricedQuantity()
657
        unit_base_price = unit_base_price / priced_quantity
658 659
      # Return result
      return unit_base_price
Yoshinori Okuji's avatar
Yoshinori Okuji committed
660 661 662 663 664 665 666 667 668 669

    security.declareProtected(Permissions.AccessContentsInformation, 
                              'getQuantityPrecision')
    def getQuantityPrecision(self):
      """Return the floating point precision of a quantity.
      """
      quantity = str(self.getBaseUnitQuantity())
      i = quantity.find('.')
      if i < 0:
        return 0
670
      return len(quantity[i+1:].rstrip('0'))