TransformedResource.py 19 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2
##############################################################################
#
3
# Copyright (c) 2002, 2004 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 35 36
#
# 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 Globals import InitializeClass
from AccessControl import ClassSecurityInfo

from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5Type.XMLMatrix import XMLMatrix
from Products.ERP5Type.Utils import cartesianProduct
Yoshinori Okuji's avatar
Yoshinori Okuji committed
37
from Products.ERP5Type.Base import TempBase
Jean-Paul Smets's avatar
Jean-Paul Smets committed
38

39
from Products.ERP5.Document.Amount import Amount
40
#from Products.ERP5.Document.TempAmount import TempAmount
Jean-Paul Smets's avatar
Jean-Paul Smets committed
41

42
from Products.CMFCore.Expression import Expression
Jean-Paul Smets's avatar
Jean-Paul Smets committed
43 44 45 46 47 48 49 50 51 52 53 54 55

from zLOG import LOG

class TransformedResource(XMLObject, XMLMatrix, Amount):
    """
        TransformedResource defines which
        resource is being transformed

        - variation
        - quantity

        Maybe defined by mapped values inside the transformed resource

56 57 58
      XXX Transformation works only for a miximum of 3 variation base category...
      Matrixbox must be rewrite for a clean implementation of n base category

Jean-Paul Smets's avatar
Jean-Paul Smets committed
59 60 61 62 63 64 65 66 67 68 69 70 71

    """

    meta_type = 'ERP5 Transformed Resource'
    portal_type = 'Transformed Resource'

    # Declarative security
    security = ClassSecurityInfo()
    security.declareObjectProtected(Permissions.View)

    # Declarative properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.SimpleItem
Jean-Paul Smets's avatar
Jean-Paul Smets committed
72
                      , PropertySheet.CategoryCore
Jean-Paul Smets's avatar
Jean-Paul Smets committed
73 74 75 76 77 78 79
                      , PropertySheet.Amount
                      , PropertySheet.TransformedResource
                      )

    # Declarative interfaces
    __implements__ = ( Interface.Variated, )

80

Jean-Paul Smets's avatar
Jean-Paul Smets committed
81 82 83

    ### Variation matrix definition
    #
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    security.declareProtected(Permissions.AccessContentsInformation, 'updateVariationCategoryList')
    def updateVariationCategoryList(self):
      """
        Check if variation category list of the resource changed and update transformed resource
        by doing a set cell range
      """
      self.setQVariationBaseCategoryList( self.getQVariationBaseCategoryList() )
      self.setVVariationBaseCategoryList( self.getVVariationBaseCategoryList() )

    security.declareProtected(Permissions.ModifyPortalContent, '_updateQMatrixCellRange')
    def _updateQMatrixCellRange(self):
      cell_range =  self.TransformedResource_asCellRange('quantity')

#      XXX TransformedResource works only for a maximum of 3 variation base category...
#      Matrixbox must be rewrite for a clean implementation of n base category
      if len(cell_range) <= 3:
        self.setCellRange(base_id='quantity', *cell_range)
      else:
        raise MoreThan3VariationBaseCategory

Jean-Paul Smets's avatar
Jean-Paul Smets committed
104 105 106 107 108 109 110
    security.declareProtected(Permissions.ModifyPortalContent, '_setQVariationBaseCategoryList')
    def _setQVariationBaseCategoryList(self, value):
      """
        Defines the possible base categories which Quantity value (Q)
        variate on
      """
      self._baseSetQVariationBaseCategoryList(value)
111
      self._updateQMatrixCellRange()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
112 113 114 115 116 117 118 119 120 121

    security.declareProtected(Permissions.ModifyPortalContent, 'setQVariationBaseCategoryList')
    def setQVariationBaseCategoryList(self, value):
      """
        Defines the possible base categories which Quantity value (Q)
        variate on and reindex the object
      """
      self._setQVariationBaseCategoryList(value)
      self.reindexObject()

122 123 124 125 126 127 128 129 130 131
    security.declareProtected(Permissions.ModifyPortalContent, '_updateVMatrixCellRange')
    def _updateVMatrixCellRange(self):
      cell_range =  self.TransformedResource_asCellRange('variation')
#      XXX TransformedResource works only for a maximum of 3 variation base category...
#      Matrixbox must be rewrite for a clean implementation of n base category
      if len(cell_range) <= 3:
        self.setCellRange(base_id='variation', *cell_range)
      else:
        raise MoreThan3VariationBaseCategory

Jean-Paul Smets's avatar
Jean-Paul Smets committed
132 133 134 135 136 137 138
    security.declareProtected(Permissions.ModifyPortalContent, '_setVVariationBaseCategoryList')
    def _setVVariationBaseCategoryList(self, value):
      """
        Defines the possible base categories which Variation value (V)
        variate on
      """
      self._baseSetVVariationBaseCategoryList(value)
139
      self._updateVMatrixCellRange()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
140 141 142 143 144 145 146 147 148 149 150

    security.declareProtected(Permissions.ModifyPortalContent, 'setVVariationBaseCategoryList')
    def setVVariationBaseCategoryList(self, value):
      """
        Defines the possible base categories which Variation value (V)
        variate on and reindex the object
      """
      self._setVVariationBaseCategoryList(value)
      self.reindexObject()


151 152 153 154 155 156 157 158 159 160 161 162
    security.declareProtected(Permissions.AccessContentsInformation,'getVariationRangeCategoryItemList')
    def getVariationRangeCategoryItemList(self, base_category_list = ()):
        """
          Returns possible variation category values for the
          transformation according to the default resource.
          Possible category values is provided as a list of
          tuples (id, title). This is mostly
          useful in ERP5Form instances to generate selection
          menus.
          Display is left...
        """
        resource = self.getResourceValue()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
163
        result = []
164 165 166
        if resource != None:
          if base_category_list is ():
            base_category_list = resource.getVariationBaseCategoryList()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
167

168
          result = resource.getVariationRangeCategoryItemList(base_category_list=base_category_list )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
169

170
        return result
Jean-Paul Smets's avatar
Jean-Paul Smets committed
171 172


173 174
    security.declareProtected(Permissions.AccessContentsInformation, 'getAggregatedAmountList')
    def getAggregatedAmountList(self, REQUEST):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
175
      """
176
        Get all interesting amount value and return TempAmount
Jean-Paul Smets's avatar
Jean-Paul Smets committed
177
      """
178 179 180 181 182 183
      # Start filing the value holder with what we have now
      # Maybe we should define a ValueHolder class XXX
      # First we create a object which id is the id of the transformed_resource
        
      # create temporary object to store result
      tmp_amount = TempAmount(self.getId())
Yoshinori Okuji's avatar
Yoshinori Okuji committed
184 185 186 187 188 189 190 191 192 193

      # First, we set initial values for quantity and variation
      # Currently, we only consider discrete variations
      # Continuous variations will be implemented in a future version of ERP5
      transformation = self.aq_parent
      error_list = []
      variation = []
      quantity = self.getQuantity()
      quantity_unit = self.getQuantityUnit()
      efficiency =  self.getEfficiency()
194

Yoshinori Okuji's avatar
Yoshinori Okuji committed
195 196
      # We look up the resource involved in this transformed resource
      resource = self.getDefaultResourceValue()
197 198
      if resource == None:
        tmp_amount.addError("No ressource define.")
Yoshinori Okuji's avatar
Yoshinori Okuji committed
199
      else:
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        resource_id = resource.getId()

        # XXX maybe this kind of constraints....
        # should be defined in the property sheet and handled
        # automaticaly
        if efficiency is None or efficiency is '' or efficiency == 0.0:
          efficiency = 1.0
        else:
          efficiency = float(efficiency)

        # and get some attributed we need for the summary
        priced_quantity = resource.getPricedQuantity()


        # XXX maybe this kind of constraints....
        # should be defined in the property sheet and handled
        # automaticaly
Yoshinori Okuji's avatar
Yoshinori Okuji committed
217
        try:
218 219
          priced_quantity = float(priced_quantity)
          if priced_quantity == 0.0: priced_quantity = 1.0
Yoshinori Okuji's avatar
Yoshinori Okuji committed
220
        except:
221 222 223 224 225 226 227 228 229 230 231 232 233 234
          priced_quantity = 1.0
          tmp_amount.addError("Priced Quantity could not be converted for resource %s" % resource_id )


        # source_base_price is the default base price.
        source_base_price = 0.0
        # base_price is defined according to the destination.
        base_price = 0.0
#        duration = 0.0
        is_variated_quantity = 0 # The variated quantity is 0 by default

        # Try to update some attributes based on the resource attributes
        if resource.hasDefaultBasePrice():
          base_price = resource.getBasePrice()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
235
          try:
236
            base_price = float(base_price)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
237
          except:
238 239 240 241 242
            base_price = 0.0
            tmp_amount.addError("Default base price could not be converted for resource %s" % resource_id )

        if resource.hasSourceBasePrice():
          source_base_price = resource.getSourceBasePrice()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
243
          try:
244
            source_base_price = float(source_base_price)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
245
          except:
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
            source_base_price = 0.0
            tmp_amount.addError("Source base price could not be converted for resource %s" % resource_id )


#        resource_quantity_unit = resource.getDefaultQuantityUnit()

#        # This is very dirty and we must do some real unit conversion here XXX
#        if quantity_unit == "Temps/Minute":
#          duration = quantity

        transformation_line = self

        # and then call edit to update its attributed
        # We do not want to reindex Temp object
        tmp_amount._edit(

            transformation = transformation,
            transformation_id = transformation.getId(),
            transformation_relative_url = transformation.getRelativeUrl(),

#            transformed_resource = self,
            transformation_line = self,
            transformation_line_id = transformation_line.getId(),
            transformation_line_relative_url = transformation_line.getRelativeUrl(),

            resource = resource,
            resource_id = resource.getId(),
            resource_relative_url = resource.getRelativeUrl(),

            # XXX is this really correct ?
            # Because specialise category on transformation defines template transformation
#            specialise_id = transformation.getId(),
#            specialise_relative_url = transformation.getRelativeUrl(),

            # Properties define on transformation line
            description =  self.getDescription(),
            quantity_unit = quantity_unit,
#            duration = duration,
            quantity = quantity,
            efficiency = efficiency,
            base_price = base_price,

            # Properties define on resource
            source_base_price = source_base_price,
#            resource_quantity_unit = resource_quantity_unit
            resource_quantity_unit = resource.getDefaultQuantityUnit()

#            total_source_base_price = 0.0,
#            total_base_price = 0.0,
#            total_duration = 0.0,
#            base_price_defined_by = '',
#            source_base_price_defined_by = '',
#            quantity_defined_by = '',
#            variation_defined_by = '',
        )


#        return tmp_amount

        # We are going to try to find which variation applies to the current REQUEST
        # First we initialize variation to the default variation value define by
        # the transformed resource
        variation = self.getVariationCategoryList()
        variation_base_category_list = resource.getVariationBaseCategoryList()

        self.portal_categories.setCategoryMembership(tmp_amount, variation_base_category_list, variation, base=1)

        
        # and update the price with the variation price if necessary
        # XXX do not understand why we get the default price define on transformation ?
#        for resource_variation in self.getValueList( variation_base_category_list, portal_type=self.getPortalVariationTypeList() ):
#
#          if resource_variation.hasDefaultBasePrice():
#            new_base_price = resource_variation.getBasePrice()
#            try:
#              new_base_price = float(new_base_price)
#            except:
#              new_base_price = 0.0
#
#              tmp_amount.addError("Default base price could not be converted for resource variation %s" % resource_variation.id )
#
#
#            if new_base_price > 0.0:
#              base_price = new_base_price
#              tmp_amount.base_price_defined_by = resource_variation.getId()
#            new_source_base_price = resource_variation.getSourceBasePrice()
#          if resource_variation.hasSourceBasePrice():
#            try:
#              new_source_base_price = float(new_source_base_price)
#            except:
#              new_source_base_price = 0.0
#
#              tmp_amount.addError("Source base price could not be converted for resource variation %s" % resource_variation.id )
#
#            if new_source_base_price > 0.0:
#
#              source_base_price = new_source_base_price
#              tmp_amount._edit(
#                source_base_price = new_source_base_price,
#                source_base_price_defined_by = resource_variation.getId()
#              )


        # Now, let us update variations and quantities
        # We will browse the mapped values and determine which apply
        for mapped_value in self.objectValues():
          if mapped_value.test(REQUEST):

            # Update attributes defined by the mapped value
            for attribute in mapped_value.getMappedValuePropertyList():

              setattr(tmp_amount, attribute, mapped_value.get(attribute))

              if attribute == 'quantity':
#                tmp_amount.quantity_defined_by = mapped_value.getId()
                tmp_amount._edit( 
                  quantity_defined_by = mapped_value.getId()
                )

                # If we have to do this, then there is a problem....
                # We'd better have better API for this, like an update function in the mapped_value
Yoshinori Okuji's avatar
Yoshinori Okuji committed
367
                try:
368 369 370
                  quantity = float(mapped_value.quantity)
                  is_variated_quantity = 1 # The variated quantity is 1
                  #                          when the quantity is defined by a variation matrix
Yoshinori Okuji's avatar
Yoshinori Okuji committed
371
                except:
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
                  tmp_amount.addError("Quantity defined by %s is not a float" % mapped_value.getId() )


            # Update categories defined by the mapped value
            base_category_list = mapped_value.getMappedValueBaseCategoryList()
            if len(base_category_list) > 0:
              tmp_amount.variation_defined_by = mapped_value.getId()
              #LOG('In Transformation prevariation',0,str(mapped_value.getCategoryMembershipList(base_category_list, base=1)))
              self.portal_categories.setCategoryMembership(tmp_amount, base_category_list,
                      mapped_value.getCategoryMembershipList(base_category_list, base=1), base=1)
              for resource_variation in mapped_value.getValueList(base_category_list,
                                                                  portal_type=self.getPortalVariationTypeList()):
                if resource_variation.hasDefaultBasePrice():
                  new_base_price = resource_variation.getBasePrice()
                  try:
                    new_base_price = float(new_base_price)
                  except:
                    new_base_price = 0.0
                    tmp_amount.addError("Default base price could not be converted for resource variation %s" % resource_variation.id )

                  if new_base_price > 0.0:
                    base_price = new_base_price
                    tmp_amount.base_price_defined_by = resource_variation.getId()
                if resource_variation.hasSourceBasePrice():
                  new_source_base_price = resource_variation.getSourceBasePrice()
                  try:
                    new_source_base_price = float(new_source_base_price)
                  except:
                    new_source_base_price = 0.0
                    tmp_amount.addError("Source base price could not be converted for resource variation %s" % resource_variation.id )
                  if new_source_base_price > 0.0:
                    source_base_price = new_source_base_price
                    tmp_amount.source_base_price_defined_by = resource_variation.getId()



        # Convert Quantities
        # XXX XXX do not convert anymore ! Convert method must be define on TempAmount
#        converted_quantity = resource.convertQuantity(quantity, quantity_unit,
#                                                                resource_quantity_unit)
#        try:
#          converted_quantity = float(converted_quantity)
#        except:
#          converted_quantity = 0.0
#          error_list += ["Quantity could not be converted for resource %s" % resource.id]
#        # Convert price to unit price
#        unit_base_price = base_price / priced_quantity
#        unit_source_base_price = source_base_price / priced_quantity
#        variation = self.portal_categories.getCategoryMembershipList(tmp_amount,
#                                              variation_base_category_list, base=1)
#        #LOG('In Transformation variation',0,str(variation))
#        total_base_price = converted_quantity * unit_base_price / efficiency
#        total_source_base_price = converted_quantity * unit_source_base_price / efficiency
#        # Define variated price
#        if is_variated_quantity:
#          total_variated_base_price = total_base_price
#          total_variated_source_base_price = total_source_base_price
#        else:
#          total_variated_base_price = 0.0
#          total_variated_source_base_price = 0.0
#        # Create a nice presentation of the variation
#        pretty_variation = ''
#        for variation_item in variation:
#          pretty_variation += "<br>%s" % str(variation_item)
#        # Update the value and calculate total
#        tmp_amount.edit(
#          converted_quantity = converted_quantity,
#          base_price = base_price,
#          unit_base_price = unit_base_price,
#          total_base_price = total_base_price,
#          source_base_price = source_base_price,
#          unit_source_base_price = unit_source_base_price,
#          total_source_base_price = total_source_base_price,
#          variation = variation,
#          variation_category_list = variation,
#          quantity = quantity,
#          pretty_variation = pretty_variation,
#          error_list = error_list
#        )


#        return [tmp_amount], total_base_price, total_source_base_price, \
#              total_variated_base_price, total_variated_source_base_price, duration

        return tmp_amount