Consumption.py 4.58 KB
Newer Older
Romain Courteaud's avatar
Romain Courteaud committed
1 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 30 31 32
##############################################################################
#
# Copyright (c) 2002 Coramy SAS and Contributors. All Rights Reserved.
#                    Thierry_Faucher <Thierry_Faucher@coramy.com>
# Copyright (c) 2004 Nexedi SARL and Contributors. All Rights Reserved.
#                    Courteaud_Romain <romain@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.
#
##############################################################################

from AccessControl import ClassSecurityInfo

33
from Products.ERP5Type import Permissions, PropertySheet
Romain Courteaud's avatar
Romain Courteaud committed
34 35 36 37
from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5Type.XMLMatrix import XMLMatrix
from Products.ERP5.Variated import Variated

38
class Consumption(XMLObject, XMLMatrix, Variated):
Romain Courteaud's avatar
Romain Courteaud committed
39 40 41 42 43 44 45 46 47 48
    """
      A matrix which provides default quantities
      for a given quantity
    """

    meta_type = 'ERP5 Consumption'
    portal_type = 'Consumption'

    # Declarative security
    security = ClassSecurityInfo()
49
    security.declareObjectProtected(Permissions.AccessContentsInformation)
Romain Courteaud's avatar
Romain Courteaud committed
50 51 52 53 54 55 56 57 58

    # Declarative properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      , PropertySheet.VariationRange
                      )

59 60
    security.declareProtected(Permissions.ModifyPortalContent, 
                              '_setVariationCategoryList')
Romain Courteaud's avatar
Romain Courteaud committed
61 62 63 64 65
    def _setVariationCategoryList(self,value):
      """
        Set consumption variation category list.
        Set matrix cell range.
      """
66 67
      self._setCategoryMembership(self.getVariationRangeBaseCategoryList(),
                                  value, base=1)
Romain Courteaud's avatar
Romain Courteaud committed
68 69
      # XXX Must use in futur this method, but it failed today 
      #Variated._setVariationCategoryList(self, value)
70 71 72
      # XXX FIXME: Use a interaction workflow instead
      # Kept for compatibility.
      self.updateCellRange(base_id='quantity')
Romain Courteaud's avatar
Romain Courteaud committed
73

74 75
    security.declareProtected(Permissions.ModifyPortalContent, 
                              'setVariationCategoryList')
Romain Courteaud's avatar
Romain Courteaud committed
76 77 78 79 80 81 82 83
    def setVariationCategoryList(self,value):
      """
        Set consumption variation category list.
        Reindex Object.
      """
      self._setVariationCategoryList(value)
      self.reindexObject()

84 85
    security.declareProtected(Permissions.ModifyPortalContent, 
                              'getVariationRangeBaseCategoryItemList')
Romain Courteaud's avatar
Romain Courteaud committed
86 87 88 89 90 91 92 93
    def getVariationRangeBaseCategoryItemList(self):
      """
        Return range of base variation item
        Left display
      """
      # XXX get TitleOrId
      return map( lambda x: (x,x)  , self.getVariationRangeBaseCategoryList() )

94 95 96 97
    security.declareProtected(Permissions.ModifyPortalContent, 
                              'getQuantityRatio')
    def getQuantityRatio(self, variation_category_line, 
                         variation_category_column):
Romain Courteaud's avatar
Romain Courteaud committed
98 99 100 101 102 103
      """
        Return quantity ratio for a virtual cell.
        Return None if not result can be return.
      """
      cell_quantity_ratio_list = []
      
104 105
      for variation_category in (variation_category_line, 
                                 variation_category_column):
Romain Courteaud's avatar
Romain Courteaud committed
106 107 108 109 110 111 112 113 114 115 116
        cell = self.getCell(variation_category, base_id='quantity')
        if cell is None:
          return None
        else:
          cell_quantity_ratio = cell.getProperty('quantity')
          if cell_quantity_ratio in [None, 0, '']:
            return None
          else:
            cell_quantity_ratio_list.append( cell_quantity_ratio )

      return cell_quantity_ratio_list[1] / cell_quantity_ratio_list[0]