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

29
from Products.ERP5Type.Globals import InitializeClass
Yoshinori Okuji's avatar
Yoshinori Okuji committed
30 31
from AccessControl import ClassSecurityInfo

32
from Products.ERP5Type import Permissions, PropertySheet, Constraint, interfaces
Yoshinori Okuji's avatar
Yoshinori Okuji committed
33 34 35 36 37 38
from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5Type.XMLMatrix import XMLMatrix
from Products.ERP5Type.Utils import cartesianProduct
from Products.ERP5.Document.TransformedResource import TransformedResource
from Products.ERP5Type.Base import TempBase

39
from Products.ERP5.Document.Amount import Amount
Yoshinori Okuji's avatar
Yoshinori Okuji committed
40

41
from Products.CMFCore.Expression import Expression
Yoshinori Okuji's avatar
Yoshinori Okuji committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

from zLOG import LOG

import operator

class AssortedResource(TransformedResource):
    """
        This code was copied from TransformedResource very stupidly.
        Therefore it is necessaery to review all the code. -yo

        AssortedResource defines which
        resource is being assorted

        - variation
        - quantity

        Maybe defined by mapped values inside the transformed resource

        WARNING: the notion of category range is quite complex in this case.
           getVariationRangeCategoryList -> possible variations of the transformed
                                            resource ie. getVariationCategoryList
                                            of the resource
           getVariationCategoryList      -> variation value of the transformed
                                            resource (ie. default variation)

           getVariationRangeBaseCategoryList -> possible variation base categories
                                                of the transformed resource
                                                (ie. getVariationBaseCategoryList
                                                of the resource)
           getVariationBaseCategoryList      -> choice of variation base categories
                                                defined by the transformed resource
                          (should be the same as getVariationRangeBaseCategoryList)

           getTransformationVariationRangeBaseCategoryList OK
                                              -> possible variation base categories
                                                  which can be used the the
                                                 transformation matrix
                                                 (based on resource)
           getTransformationVariationBaseCategoryList OK
                                              -> choice of variation base categories
                                                  which can be used the the
                                                 transformation matrix
                                                 (based on resource)

           getTransformationVariationRangeCategoryList OK
                                              -> possible category values
                                                 which can be used in the
                                                 transformation matrix
                                                 (based on resource)
           getTransformationVariationCategoryList OK
                                              -> choice of category values
                                                 which can be used in the
                                                 transformation matrix

           XXX WE HAVE an issue here:
           - the variation range of the transformation
             defines both the variation range of the main resource
             and the variation range for matrices

           - where do we define default variation value
             for the resource produced by the transformation ?
             (probably in the domain fields)

           - where do we define selection parameters ?

           getResourceVariationCategoryList
           getResourceVariationRangeCategoryList

      setValueUids could be overriden to provide quick and dirty
      behaviour of range update

    """

    meta_type = 'ERP5 Assorted Resource'
    portal_type = 'Assorted Resource'
117
    add_permission = Permissions.AddPortalContent
Yoshinori Okuji's avatar
Yoshinori Okuji committed
118 119 120

    # Declarative security
    security = ClassSecurityInfo()
121
    security.declareObjectProtected(Permissions.AccessContentsInformation)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

    # Declarative properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.SimpleItem
                      , PropertySheet.CategoryCore
                      , PropertySheet.Amount
                      , PropertySheet.TransformedResource
                      )

    # Local property sheet
    _properties = (
      { 'id'          : 'variation_base_category',
        'storage_id'  : 'variation_base_category_list', # Coramy Compatibility
        'description' : "",
        'type'        : 'tokens',
        'acquisition_base_category' : ('resource',),
138
        'acquisition_portal_type'   : Expression('python: portal.getPortalResourceTypeList()'),
Yoshinori Okuji's avatar
Yoshinori Okuji committed
139 140 141 142 143 144 145 146 147 148 149 150 151
        'acquisition_copy_value'    : 0,
        'acquisition_mask_value'    : 0,
        'acquisition_sync_value'    : 0,
        'acquisition_accessor_id'   : 'getVariationBaseCategoryList', ### XXX BUG
        'acquisition_depends'       : None,
        'mode'        : 'w' },
    )

    security.declareProtected(Permissions.AccessContentsInformation, 'getAssortedVariationCategoryList')
    def getAssortedVariationCategoryList(self, cell_index):
      """
        Nice for A
      """
152
      transformation = self.getParentValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
      transformation_category_list = transformation.getVariationCategoryList()
      variation_category_list = []
      for p in cell_index:
        if p is not None and p not in transformation_category_list:
          variation_category_list.append(p)
      #LOG('getAssortedVariationCategoryList', 0, repr(cell_index))
      #LOG('getAssortedVariationCategoryList', 0, repr(variation_category_list))
      return variation_category_list

    security.declareProtected(Permissions.AccessContentsInformation, 'getAssortedVariationBaseCategoryList')
    def getAssortedVariationBaseCategoryList(self):
      """
        Nice for A
      """
      return self.getQVariationBaseCategoryList()

    security.declareProtected(Permissions.AccessContentsInformation, 'getAssortmentVariationCategoryList')
    def getAssortmentVariationCategoryList(self, cell_index):
      """
        Nice for C
      """
174
      transformation = self.getParentValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188
      transformation_category_list = transformation.getVariationCategoryList()
      variation_category_list = []
      for p in cell_index:
        if p is not None and p in transformation_category_list:
          variation_category_list.append(p)
      #LOG('getAssortmentVariationCategoryList', 0, repr(cell_index))
      #LOG('getAssortmentVariationCategoryList', 0, repr(variation_category_list))
      return variation_category_list

    security.declareProtected(Permissions.AccessContentsInformation, 'getAssortmentVariationBaseCategoryList')
    def getAssortmentVariationBaseCategoryList(self):
      """
        Nice for C
      """
189
      #LOG('getAssortmentVariationBaseCategoryList', 0, repr(self))
190
      transformation = self.getParentValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204
      return transformation.getVariationBaseCategoryList()

    # XXX Should be moved to somewhere more global, as this is general.
    #     This does not depend even on self.
    #
    # Return a sorted list of base categories. This makes views of matrices consistent.
    # Use the global variables, 'column_base_category_list' and 'line_base_category_list'
    # (but not 'tab_base_category_list' at the moment).
    def _getSortedBaseCategoryList(self, base_category_list):
      base_category_list = base_category_list[:]  # Work on a copy.
      base_category_list.sort()
      column = None
      line = None
      sorted_list = [None, None]
205
      for category in self.getPortalColumnBaseCategoryList():
Yoshinori Okuji's avatar
Yoshinori Okuji committed
206 207 208 209 210 211
        if category in base_category_list:
          if column is None:
            column = category
          else:
            sorted_list.append(category)
          base_category_list.remove(category)
212
      for category in self.getPortalLineBaseCategoryList():
Yoshinori Okuji's avatar
Yoshinori Okuji committed
213 214 215 216 217 218 219 220 221 222 223 224
        if category in base_category_list:
          if line is None:
            line = category
          else:
            sorted_list.append(category)
          base_category_list.remove(category)
      sorted_list.extend(base_category_list)
      sorted_list[0] = line
      sorted_list[1] = column
      return sorted_list

    # Update the range of cells according to the currently selected base categories.
225
    def _updateCellRange(self, base=1, current_category=None):
226
      transformation = self.getParentValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
      kwd = {'base_id': 'quantity'}
      kw = []
      base_category_list = self._getSortedBaseCategoryList(self.getQVariationBaseCategoryList())
      for base_category in base_category_list:
        # FIXME: Actually, getVariationRangeCategoryList should be used here.
        #        But getVariationRangeCategoryList is inconsistent with
        #        getVariationRangeCategoryItemList, because getVariationRangeCategoryItemList
        #        is overrided in Amount and Resource. -yo
        if base_category is None:
          category_item_list = [(None,'')]
        else:
          category_item_list = self.getVariationRangeCategoryItemList(base_category_list = [base_category],
                                                                      base=1)
        category_list = []
        for item in category_item_list:
          category_list.append(item[0])
        kw.append(category_list)
      kw.append(transformation.getVariationCategoryList())
245
      #LOG('_updateCellRange', 20, str(kw))
Yoshinori Okuji's avatar
Yoshinori Okuji committed
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
      self.setCellRange(*kw, **kwd)

    ### Variation matrix definition
    #
    security.declareProtected(Permissions.ModifyPortalContent, '_setQVariationBaseCategoryList')
    def _setQVariationBaseCategoryList(self, value):
      """
        Defines the possible base categories which Quantity value (Q)
        variate on
      """
      self._baseSetQVariationBaseCategoryList(value)
      self._updateCellRange()
      # And fix it in case the cells are not renamed (XXX this will be removed in the future)
      self._checkConsistency(fixit=1)

    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()

    security.declareProtected(Permissions.ModifyPortalContent, '_setVVariationBaseCategoryList')
    def _setVVariationBaseCategoryList(self, value):
      """
        Defines the possible base categories which Variation value (V)
        variate on
      """
      self._baseSetVVariationBaseCategoryList(value)
      kwd = {}
      kwd['base_id'] = 'variation'
      kw = []
280
      transformation = self.getParentValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
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
      line_id = transformation.getVariationBaseCategoryLine()
      column_id = transformation.getVariationBaseCategoryColumn()
      line = [[None]]
      column = [[None]]
      for v in value:
        if v == line_id:
          line = [transformation.getCategoryMembershipList(v,base=1)]
        elif v == column_id:
          column = [transformation.getCategoryMembershipList(v,base=1)]
        else:
          kw += [transformation.getCategoryMembershipList(v,base=1)]
      kw = line + column + kw
      self.setCellRange(*kw, **kwd)
      # Empty cells if no variation
      if line == [[None]] and column == [[None]]:
        self.delCells(base_id='variation')
      # And fix it in case the cells are not renamed (XXX this will be removed in the future)
      self._checkConsistency(fixit=1)

    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()

    # Methods for matrix UI widgets
    security.declareProtected(Permissions.AccessContentsInformation, 'getQLineItemList')
311
    def getQLineItemList(self, display_id='getTitle', base=1, current_category=None):
Yoshinori Okuji's avatar
Yoshinori Okuji committed
312 313 314
      """
      """
      line_category = self._getSortedBaseCategoryList(self.getQVariationBaseCategoryList())[0]
315
      #LOG('getQLineItemList', 0, "%s" % str(line_category))
Yoshinori Okuji's avatar
Yoshinori Okuji committed
316 317 318 319
      if line_category is None:
        result = [(None,'')]
      else:
        result = self.getVariationRangeCategoryItemList(base_category_list = [line_category],
320
                                                        display_id=display_id,
Yoshinori Okuji's avatar
Yoshinori Okuji committed
321
                                                        base=base,
322
                                                        current_category=current_category)
323
      #LOG('getQLineItemList', 10, "%s" % str(result))
Yoshinori Okuji's avatar
Yoshinori Okuji committed
324 325 326
      return result

    security.declareProtected(Permissions.AccessContentsInformation, 'getQColumnItemList')
327
    def getQColumnItemList(self, display_id='getTitle', base=1, current_category=None):
Yoshinori Okuji's avatar
Yoshinori Okuji committed
328 329 330
      """
      """
      column_category = self._getSortedBaseCategoryList(self.getQVariationBaseCategoryList())[1]
331
      #LOG('getQColumnItemList', 0, "%s" % str(column_category))
Yoshinori Okuji's avatar
Yoshinori Okuji committed
332 333 334 335
      if column_category is None:
        result = [(None,'')]
      else:
        result = self.getVariationRangeCategoryItemList(base_category_list = [column_category],
336
                                                        display_id=display_id,
Yoshinori Okuji's avatar
Yoshinori Okuji committed
337
                                                        base=base,
338
                                                        current_category=current_category)
339
      #LOG('getQColumnItemList', 0, "%s" % str(result))
Yoshinori Okuji's avatar
Yoshinori Okuji committed
340 341 342
      return result

    security.declareProtected(Permissions.AccessContentsInformation, 'getQTabItemList')
343
    def getQTabItemList(self, display_id='getTitle', base=1, current_category=None):
Yoshinori Okuji's avatar
Yoshinori Okuji committed
344 345 346 347 348 349 350 351
      """
        Returns a list of items which can be used as index for
        each tab of a matrix or to define a cell range.
      """
      tab_category_list = self._getSortedBaseCategoryList(self.getQVariationBaseCategoryList())[2:]
      tab_category_item_list_list = []
      for tab_category in tab_category_list:
        tab_category_item_list = self.getVariationRangeCategoryItemList(base_category_list = [tab_category],
352
                                                                        display_id=display_id,
Yoshinori Okuji's avatar
Yoshinori Okuji committed
353
                                                                        base=base,
354
                                                                        current_category=current_category)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
355
        tab_category_item_list_list.append(tab_category_item_list)
356
      transformation = self.getParentValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
357
      transformation_category_item_list = transformation.getVariationCategoryItemList(
Yoshinori Okuji's avatar
Yoshinori Okuji committed
358
                                                          display_id=display_id,
Yoshinori Okuji's avatar
Yoshinori Okuji committed
359
                                                          base=base,
360
                                                          current_category=current_category)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
      tab_category_item_list_list.append(transformation_category_item_list)
      if len(tab_category_item_list_list) > 0:
        product_list = cartesianProduct(tab_category_item_list_list)
        result = []
        for item_list in product_list:
          value_list = []
          label_list = []
          for item in item_list:
            value_list.append(item[0])
            label_list.append(item[1])
          result.append((value_list, label_list))
      else:
        result = [(None,'')]
      return result

    security.declareProtected(Permissions.AccessContentsInformation, 'getVLineItemList')
    def getVLineItemList(self):
378
      base_category = self.getParentValue().getVariationBaseCategoryLine()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
379
      if base_category in self.getVVariationBaseCategoryList():
380
        clist = self.getParentValue().getCategoryMembershipList(base_category, base=1)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
381 382 383 384 385 386 387 388 389
      else:
        clist = [None]
      result = []
      for c in clist:
        result += [(c,c)]
      return result

    security.declareProtected(Permissions.AccessContentsInformation, 'getVColumnItemList')
    def getVColumnItemList(self):
390
      base_category = self.getParentValue().getVariationBaseCategoryColumn()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
391
      if base_category in self.getVVariationBaseCategoryList():
392
        clist = self.getParentValue().getCategoryMembershipList(base_category, base=1)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
393 394 395 396 397 398 399 400 401 402 403 404
      else:
        clist = [None]
      result = []
      for c in clist:
        result += [(c,c)]

      result.sort() # XXX Temp until set / list issue solved

      return result

    security.declareProtected(Permissions.AccessContentsInformation, 'getVTabItemList')
    def getVTabItemList(self):
405
      transformation = self.getParentValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
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
      line_id = transformation.getVariationBaseCategoryLine()
      column_id = transformation.getVariationBaseCategoryColumn()
      base_category_list = transformation.getVariationBaseCategoryList()
      base_category = []
      for c in base_category_list:
        if not c in (line_id, column_id):
          if c in self.getVVariationBaseCategoryList():
            base_category += [transformation.getCategoryMembershipList(c, base=1)]
      if len(base_category) > 0:
        clist = cartesianProduct(base_category)
        result = []
        for c in clist:
          result += [(c,c)]
      else:
        result = [(None,'')]

      result.sort() # XXX Temp until set / list issue solved

      return result

    security.declareProtected( Permissions.ModifyPortalContent, 'newCell' )
    def newCell(self, *kw, **kwd):
      result = XMLMatrix.newCell(self, *kw, **kwd)
      result._setPredicateOperator("SUPERSET_OF")
      membership_list = []
      for c in kw:
        if c is not None:
          membership_list += [c]
      result._setPredicateValueList(membership_list)
      base_id = kwd.get('base_id', 'cell')
      if base_id == 'quantity':
        result._setDomainBaseCategoryList(self.getQVariationBaseCategoryList())
      elif base_id == 'variation':
        result._setDomainBaseCategoryList(self.getVVariationBaseCategoryList())

      return result

    security.declareProtected( Permissions.ModifyPortalContent, 'newCellContent' )
444
    def newCellContent(self, id, portal_type='Set Mapped Value', **kw):
445
      """Overriden to specify default portal type
Yoshinori Okuji's avatar
Yoshinori Okuji committed
446
      """
447
      return self.newContent(id=id, portal_type=portal_type, **kw)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
448 449 450 451 452 453

    security.declarePrivate('_checkConsistency')
    def _checkConsistency(self, fixit=0):
      """
        Check the constitency of transformation elements
      """
454
      transformation = self.getParentValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
      transformation_category_list = transformation.getVariationCategoryList()

      error_list = XMLMatrix._checkConsistency(self, fixit=fixit)

      # Quantity should be empty if no variation
      q_range = self.getCellRange(base_id = 'quantity')
      if q_range is not None:
        range_is_empty = 1
        for q_list in q_range:
          if q_list is not None:
            range_is_empty = 0
            break
        if range_is_empty:
          matrix_is_not_empty = 0
          for k in self.getCellIds(base_id = 'quantity'):
            if hasattr(self, k):matrix_is_not_empty = 1
          if matrix_is_not_empty:
            if fixit:
              self.delCells(base_id = 'quantity')
              error_message =  "Variation cells for quantity should be empty (fixed)"
            else:
              error_message =  "Variation cells for quantity should be empty"
            error_list += [(self.getRelativeUrl(),
                          'TransformedResource inconsistency', 100, error_message)]

      # First quantity
      # We build an attribute equality and look at all cells
      q_constraint = Constraint.AttributeEquality(
        domain_base_category_list = self.getQVariationBaseCategoryList(),
        predicate_operator = 'SUPERSET_OF',
        mapped_value_property_list = ['quantity'] )
      for kw in self.getCellKeys(base_id = 'quantity'):
        kwd={'base_id': 'quantity'}
        c = self.getCell(*kw, **kwd)
        if c is not None:
          predicate_value_list = []
          categories_list = []
          for p in kw:
            if p is not None:
              if p in transformation_category_list:
495 496
                if p not in predicate_value_list:
                  predicate_value_list.append(p)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
497
              else:
498 499
                if p not in categories_list:
                  categories_list.append(p)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513
          q_constraint.edit(predicate_value_list = predicate_value_list,
                            categories_list = categories_list)
          if fixit:
            error_list += q_constraint.fixConsistency(c)
          else:
            error_list += q_constraint.checkConsistency(c)

      return error_list

    security.declareProtected(Permissions.AccessContentsInformation, 'getAggregatedAmountList')
    def getAggregatedAmountList(self, REQUEST):
      # 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
514
      transformation = self.getParentValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
      error_list = []
      variation = []
      line_item_list = []
      total_base_price_list = []
      total_source_base_price_list = []
      total_variated_base_price_list = []
      total_variated_source_base_price_list = []
      quantity = self.getQuantity()
      quantity_unit = self.getQuantityUnit()
      efficiency =  self.getEfficiency()
      # XXXXXXXXXXXXXXXXXX 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)
      # We look up the resource involved in this transformed resource
      resource = self.getDefaultResourceValue()
      if resource is not None:
        resource_id = resource.getId()
      else:
        resource_id = None
      # and get some attributed we need for the summary
      priced_quantity = resource.getPricedQuantity()
      # XXXXXXXXXXXXXXXXXX maybe this kind of constraints....
      # should be defined in the property sheet and handled
      # automaticaly
      try:
        priced_quantity = float(priced_quantity)
        if priced_quantity == 0.0: priced_quantity = 1.0
Yoshinori Okuji's avatar
Yoshinori Okuji committed
546
      except TypeError:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
547 548 549 550 551 552 553 554 555 556 557 558 559
        priced_quantity = 1.0
        error_list += ["Priced Quantity could not be converted for resource %s" % resource_id]
      # source_base_price is the default base price.
      # base_price is defined according to the destination.
      source_base_price = 0.0
      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()
        try:
          base_price = float(base_price)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
560
        except TypeError:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
561 562 563 564 565 566
          base_price = 0.0
          error_list += ["Default base price could not be converted for resource %s" % resource_id]
      if resource.hasSourceBasePrice():
        source_base_price = resource.getSourceBasePrice()
        try:
          source_base_price = float(source_base_price)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
567
        except TypeError:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
          source_base_price = 0.0
          error_list += ["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 XXXXXXX
      if quantity_unit == "Temps/Minute":
        duration = quantity
      # Now, let us update variations and quantities
      # We will browse the mapped values and dermine which apply
      for mapped_value in self.objectValues():
        if mapped_value.test(REQUEST):
          # Start filing the value holder with what we have now
          # Maybe we should define a ValueHolder class XXXXXXXXXXXXXXXXXXXXXX
          # First we create a object which id is the id of this assorted resource
          line_item = TempBase(self.id)
          # 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(line_item,
                                  variation_base_category_list, variation, base=1)
          # and update the price with the variation price if necessary
          for resource_variation in self.getValueList(
591
                              variation_base_category_list, portal_type=self.getPortalVariationTypeList()):
Yoshinori Okuji's avatar
Yoshinori Okuji committed
592 593 594 595
            if resource_variation.hasDefaultBasePrice():
              new_base_price = resource_variation.getBasePrice()
              try:
                new_base_price = float(new_base_price)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
596
              except TypeError:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
597 598 599 600 601 602 603 604 605 606
                new_base_price = 0.0
                error_list += ["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
                line_item.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)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
607
              except TypeError:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
608 609 610 611 612 613 614 615 616
                new_source_base_price = 0.0
                error_list += ["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
                line_item.source_base_price_defined_by = resource_variation.getId()
          # and then call edit to update its attributed
          # XXXXXXXXXXXXX bad: this call will call reindex() which is not needed at all
          # How can we create standard objects which are temporary and not indexed ???
617 618
          #LOG("Quantity Unit", 0, str(quantity_unit))
          #LOG("Quantity Unit", 0, str(resource))
Yoshinori Okuji's avatar
Yoshinori Okuji committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
          line_item.edit(
              transformation = transformation,
              transformed_resource = self,
              resource = resource,
              transformation_id = transformation.getId(),
              resource_id = resource.getId(),
              resource_relative_url = resource.getRelativeUrl(),
              specialise_id = transformation.getId(),
              specialise_relative_url = transformation.getRelativeUrl(),
              description =  self.getDescription(),
              quantity_unit = quantity_unit,
              duration = duration,
              quantity = quantity,
              efficiency = efficiency,
              base_price = base_price,
              source_base_price = source_base_price,
              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 = '',
              resource_quantity_unit = resource_quantity_unit
            )
          # Update attributes defined by the mapped value
          for attribute in mapped_value.getMappedValuePropertyList():
            setattr(line_item, attribute, mapped_value.get(attribute))
            if attribute == 'quantity':
              line_item.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
              try:
                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
655
              except TypeError:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
656 657 658 659 660 661 662 663
                error_list += ["Quantity defined by %s is not a float" % mapped_value.id]
          # Update categories defined by the mapped value
          base_category_list = mapped_value.getMappedValueBaseCategoryList()
          if len(base_category_list) > 0:
            line_item.variation_defined_by = mapped_value.getId()
            self.portal_categories.setCategoryMembership(line_item, base_category_list,
                    mapped_value.getCategoryMembershipList(base_category_list, base=1), base=1)
            for resource_variation in mapped_value.getValueList(base_category_list,
664
                                                                portal_type=self.getPortalVariationTypeList()):
Yoshinori Okuji's avatar
Yoshinori Okuji committed
665 666 667 668
              if resource_variation.hasDefaultBasePrice():
                new_base_price = resource_variation.getBasePrice()
                try:
                  new_base_price = float(new_base_price)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
669
                except TypeError:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
670 671 672 673 674 675 676 677 678 679 680
                  new_base_price = 0.0
                  error_list += \
                      ["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
                  line_item.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)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
681
                except TypeError:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
682 683 684 685 686 687 688 689 690 691 692 693
                  new_source_base_price = 0.0
                  error_list += \
                      ["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
                  line_item.source_base_price_defined_by = resource_variation.getId()
          # Convert Quantities
          converted_quantity = resource.convertQuantity(quantity, quantity_unit,
                                                                  resource_quantity_unit)
          try:
            converted_quantity = float(converted_quantity)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
694
          except TypeError:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
            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(line_item,
                                                variation_base_category_list, base=1)
          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
          line_item.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
          )
          line_item_list.append(line_item)
          total_base_price_list.append(total_base_price)
          total_source_base_price_list.append(total_source_base_price)
          total_variated_base_price_list.append(total_variated_base_price)
          total_variated_source_base_price_list.append(total_variated_source_base_price)
      return line_item_list, \
            reduce(operator.add, total_base_price_list, 0), \
            reduce(operator.add, total_source_base_price_list, 0), \
            reduce(operator.add, total_variated_base_price_list, 0), \
            reduce(operator.add, total_variated_source_base_price_list, 0), \
            duration