Blame view

product/ERP5/Document/Amount.py 24.3 KB
Jean-Paul Smets committed
1 2
##############################################################################
#
Romain Courteaud committed
3
# Copyright (c) 2002, 2004 Nexedi SARL and Contributors. All Rights Reserved.
Jean-Paul Smets committed
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
Romain Courteaud committed
5
#                    Romain Courteaud <romain@nexedi.com>
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
#
# 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.
#
##############################################################################

Jérome Perrin committed
30
import zope.interface
31
from Products.ERP5Type.Globals import InitializeClass
Jean-Paul Smets committed
32
from AccessControl import ClassSecurityInfo
Kevin Deldycke committed
33 34
from Products.ERP5.Variated import Variated
from Products.ERP5.VariationValue import VariationValue
Kazuhiko Shiozaki committed
35
from Products.ERP5Type import Permissions, PropertySheet, Constraint, interfaces
Jean-Paul Smets committed
36
from Products.ERP5Type.Base import Base
Romain Courteaud committed
37
from Products.ERP5Type.Base import TempBase
Romain Courteaud committed
38
from Products.CMFCategory.Renderer import Renderer
Jean-Paul Smets committed
39

Kevin Deldycke committed
40

Alexandre Boeglin committed
41
from zLOG import LOG, ERROR
Alexandre Boeglin committed
42
from warnings import warn
Jean-Paul Smets committed
43

Kevin Deldycke committed
44

Jean-Paul Smets committed
45 46 47 48 49 50 51 52 53 54 55 56
class Amount(Base, Variated):
  """
    A mix-in class which provides some utilities
    (variations, conversions, etc.)

    Utilities include

    - getVariation accesors (allows to access variations of whatever)

    -
  """

Romain Courteaud committed
57 58 59
  meta_type = 'ERP5 Amount'
  portal_type = 'Amount'

Jean-Paul Smets committed
60 61
  # Declarative security
  security = ClassSecurityInfo()
Alexandre Boeglin committed
62
  security.declareObjectProtected(Permissions.AccessContentsInformation)
Jean-Paul Smets committed
63 64

  # Declarative interfaces
Jérome Perrin committed
65 66
  zope.interface.implements(interfaces.IVariated,
                            interfaces.IAmount)
Jean-Paul Smets committed
67

Romain Courteaud committed
68 69 70 71
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.SimpleItem
                    , PropertySheet.Amount
                    , PropertySheet.Price
Kevin Deldycke committed
72
                    )
Romain Courteaud committed
73

Jean-Paul Smets committed
74 75
  # A few more mix-in methods which should be relocated
  # THIS MUST BE UPDATE WITH CATEGORY ACQUISITION
Romain Courteaud committed
76 77
  security.declareProtected(Permissions.AccessContentsInformation,
                            'getVariationCategoryList')
Alexandre Boeglin committed
78
  def getVariationCategoryList(self, default=[], base_category_list=(),
Alexandre Boeglin committed
79
      omit_optional_variation=0, omit_option_base_category=None):
Jean-Paul Smets committed
80 81 82 83
    """
      Returns the possible discrete variations
      (as a list of relative urls to categories)
    """
Alexandre Boeglin committed
84 85 86 87 88 89
    #XXX backwards compatibility
    if omit_option_base_category is not None:
      warn("Please use omit_optional_variation instead of"\
          " omit_option_base_category.", DeprecationWarning)
      omit_optional_variation = omit_option_base_category

Jean-Paul Smets committed
90 91 92
    result = []
    resource = self.getDefaultResourceValue()
    if resource is not None:
Romain Courteaud committed
93
      resource_variation_list = resource.getVariationBaseCategoryList(
Alexandre Boeglin committed
94
          omit_optional_variation=omit_optional_variation)
Jean-Paul Smets committed
95
      if len(base_category_list) > 0 :
Romain Courteaud committed
96 97
        variation_list = filter(lambda x: x in base_category_list,
                                resource_variation_list)
Jean-Paul Smets committed
98 99 100
      else :
        variation_list = resource_variation_list
      if len(variation_list) > 0:
Romain Courteaud committed
101
        result = self.getAcquiredCategoryMembershipList(variation_list, base=1)
Jean-Paul Smets committed
102 103
    return result

Kevin Deldycke committed
104
  security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud committed
105
                            'getVariationCategoryItemList')
Kevin Deldycke committed
106
  def getVariationCategoryItemList(self, base_category_list=(), base=1,
Jérome Perrin committed
107
                                   display_id='logical_path',
Sebastien Robin committed
108
                                   current_category=None,**kw):
Romain Courteaud committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    """
      Returns the list of possible variations
      XXX Copied and modified from Variated
      Result is left display.
    """
    variation_category_item_list = []
    if base_category_list == ():
      base_category_list = self.getVariationRangeBaseCategoryList()

    for base_category in base_category_list:
      variation_category_list = self.getVariationCategoryList(
                                          base_category_list=[base_category])

      resource_list = [self.portal_categories.resolveCategory(x) for x in\
                       variation_category_list]
      category_list = [x for x in resource_list \
                       if x.getPortalType() == 'Category']
      variation_category_item_list.extend(Renderer(
                             is_right_display=0,
                             display_none_category=0, base=base,
                             current_category=current_category,
Jérome Perrin committed
130
                             display_id=display_id, **kw).\
Romain Courteaud committed
131 132 133 134 135
                                               render(category_list))
      object_list = [x for x in resource_list \
                       if x.getPortalType() != 'Category']
      variation_category_item_list.extend(Renderer(
                             is_right_display=0,
Kevin Deldycke committed
136
                             base_category=base_category,
Romain Courteaud committed
137 138
                             display_none_category=0, base=base,
                             current_category=current_category,
Jérome Perrin committed
139
                             display_id='title', **kw).\
Romain Courteaud committed
140 141 142
                                               render(object_list))
    return variation_category_item_list

Romain Courteaud committed
143 144
  security.declareProtected(Permissions.ModifyPortalContent, 
                            '_setVariationCategoryList')
Jean-Paul Smets committed
145 146 147 148 149 150 151 152
  def _setVariationCategoryList(self, value):
    result = []
    resource = self.getDefaultResourceValue()
    if resource is not None:
      variation_list = resource.getVariationBaseCategoryList()
      if len(variation_list) > 0:
        self._setCategoryMembership(variation_list, value, base = 1)

Romain Courteaud committed
153 154
  security.declareProtected(Permissions.ModifyPortalContent, 
                            'setVariationCategoryList')
Jean-Paul Smets committed
155 156 157 158
  def setVariationCategoryList(self, value):
    self._setVariationCategoryList(value)
    self.reindexObject()

Aurel committed
159
  security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud committed
160
                            'getVariationBaseCategoryList')
Alexandre Boeglin committed
161
  def getVariationBaseCategoryList(self, default=[],
Alexandre Boeglin committed
162
      omit_optional_variation=0, omit_option_base_category=None):
Romain Courteaud committed
163
    """
Kevin Deldycke committed
164
      Return the list of base_category from all variation related to
Romain Courteaud committed
165
      amount.
Romain Courteaud committed
166 167
      It is maybe a nonsense, but useful for correcting user errors.
    """
Alexandre Boeglin committed
168 169 170 171 172 173
    #XXX backwards compatibility
    if omit_option_base_category is not None:
      warn("Please use omit_optional_variation instead of"\
          " omit_option_base_category.", DeprecationWarning)
      omit_optional_variation = omit_option_base_category

Kevin Deldycke committed
174
    base_category_list = []
Romain Courteaud committed
175
    for category in self.getVariationCategoryList(
Alexandre Boeglin committed
176
        omit_optional_variation=omit_optional_variation):
Kevin Deldycke committed
177 178 179 180
      base_category = category.split('/')[0]
      if base_category not in base_category_list:
        base_category_list.append(base_category)
    return base_category_list
Romain Courteaud committed
181

Jérome Perrin committed
182 183 184 185 186 187 188 189
  security.declareProtected(Permissions.ModifyPortalContent,
                            'setVariationBaseCategoryList')
  def setVariationBaseCategoryList(self, node_list):
    """Do nothing in the case of an amount, because variation base category
    list are set on the resource.
    """
    pass

Romain Courteaud committed
190 191
  security.declareProtected(Permissions.AccessContentsInformation, 
                            'getVariationBaseCategoryItemList')
Jean-Paul Smets committed
192
  def getVariationBaseCategoryItemList(self,display_id='getTitleOrId',**kw):
Romain Courteaud committed
193 194 195 196 197 198 199 200 201
    """
    Returns a list of base_category tuples.
    """
    return self.portal_categories.getItemList(
                                    self.getVariationBaseCategoryList(),
                                    display_id=display_id,**kw)

  security.declareProtected(Permissions.AccessContentsInformation, 
                            'getVariationValue')
Jean-Paul Smets committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  def getVariationValue(self):
    """
      New Method for dicrete and countinuous variations
      using a VariantValue instance

      A new instance of VariationValue is created with categories
      and attributes set to what they should be.

      A this point, we only implement discrete variations
    """
    return VariationValue(context = self)

  security.declareProtected(Permissions.ModifyPortalContent, '_setVariationValue')
  def _setVariationValue(self, variation_value):
    return variation_value.setVariationValue(self)

  security.declareProtected(Permissions.ModifyPortalContent, 'setVariationValue')
  def setVariationValue(self, variation_value):
    self._setVariationValue(variation_value)
    self.reindexObject()

Julien Muchembled committed
223
  security.declareProtected(Permissions.AccessContentsInformation, \
Romain Courteaud committed
224
                            'getVariationRangeCategoryItemList')
Julien Muchembled committed
225
  def getVariationRangeCategoryItemList(self, *args, **kw):
Jean-Paul Smets committed
226
    """
Romain Courteaud committed
227 228 229 230 231 232
      Returns possible variation category values for the
      order line 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.
Jean-Paul Smets committed
233
    """
Romain Courteaud committed
234
    resource = self.getResourceValue()
Julien Muchembled committed
235 236
    if resource is None:
      return []
Jérome Perrin committed
237
    kw['omit_individual_variation'] = 0
Julien Muchembled committed
238
    return resource.getVariationCategoryItemList(*args, **kw)
Yoshinori Okuji committed
239

Romain Courteaud committed
240 241
  security.declareProtected(Permissions.AccessContentsInformation, \
                            'getVariationRangeCategoryList')
Alexandre Boeglin committed
242 243
  def getVariationRangeCategoryList(self, default=[], base_category_list=(),
      base=1, **kw):
Yoshinori Okuji committed
244
    """
Romain Courteaud committed
245 246
      Returns possible variation category values for the
      order line according to the default resource.
Yoshinori Okuji committed
247
    """
Romain Courteaud committed
248 249
    return [x[1] for x in self.getVariationRangeCategoryItemList(
                                     base_category_list=base_category_list,
Romain Courteaud committed
250
                                     base=base, **kw)]
Jean-Paul Smets committed
251 252

  security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud committed
253
                            'getVariationRangeBaseCategoryList')
Alexandre Boeglin committed
254 255
  def getVariationRangeBaseCategoryList(self, default=[],
      omit_optional_variation=0, omit_option_base_category=None):
Jean-Paul Smets committed
256 257 258 259 260 261 262 263 264 265
    """
        Returns possible variations base categories for this amount ie.
        the variation base category of the resource (not the
        variation range).

        Should be a range because we shall variate the amount
        into cells (ie. the line into cells) on part of the
        getVariationRangeBaseCategoryList -> notion of
        getVariationBaseCategoryList is different
    """
Alexandre Boeglin committed
266 267 268 269 270 271
    #XXX backwards compatibility
    if omit_option_base_category is not None:
      warn("Please use omit_optional_variation instead of"\
          " omit_option_base_category.", DeprecationWarning)
      omit_optional_variation = omit_option_base_category

Kevin Deldycke committed
272
    resource = self.getDefaultResourceValue()
Romain Courteaud committed
273
    if resource is not None:
Romain Courteaud committed
274
      result = resource.getVariationBaseCategoryList(
Alexandre Boeglin committed
275
          omit_optional_variation=omit_optional_variation)
Romain Courteaud committed
276
    else:
Romain Courteaud committed
277
      result = Variated.getVariationRangeBaseCategoryList(self)
Romain Courteaud committed
278
    return result
Jean-Paul Smets committed
279

Romain Courteaud committed
280 281
  security.declareProtected(Permissions.AccessContentsInformation,
                            'getVariationRangeBaseCategoryItemList')
Alexandre Boeglin committed
282 283 284
  def getVariationRangeBaseCategoryItemList(self, omit_optional_variation=0,
      omit_option_base_category=None, display_id="title",
      display_none_category=0):
Romain Courteaud committed
285 286 287 288 289
    """
        Returns possible variations base categories for this amount ie.
        the variation base category of the resource (not the
        variation range).
    """
Alexandre Boeglin committed
290 291 292 293 294 295
    #XXX backwards compatibility
    if omit_option_base_category is not None:
      warn("Please use omit_optional_variation instead of"\
          " omit_option_base_category.", DeprecationWarning)
      omit_optional_variation = omit_option_base_category

Romain Courteaud committed
296
    return self.portal_categories.getItemList(
Alexandre Boeglin committed
297 298 299 300
        self.getVariationRangeBaseCategoryList(
            omit_optional_variation=omit_optional_variation),
        display_id=display_id,
        display_none_category=display_none_category)
Romain Courteaud committed
301

Romain Courteaud committed
302 303 304 305 306 307 308 309 310 311
  #####################################################################
  #  Variation property API
  #####################################################################
  security.declareProtected(Permissions.AccessContentsInformation,
                            'getVariationPropertyDict')
  def getVariationPropertyDict(self):
    """
      Return a dictionary of:
        {property_id: property_value,}
      Each property is a variation of the resource.
Kevin Deldycke committed
312
      The variation property list is defined on resource,
Romain Courteaud committed
313 314 315
      with setVariationPropertyList.
    """
    property_dict = {}
Kevin Deldycke committed
316
    resource = self.getDefaultResourceValue()
Romain Courteaud committed
317 318 319 320
    if resource is not None:
      variation_list = resource.getVariationPropertyList()
      for variation_property in variation_list:
        property_dict[variation_property] = \
Romain Courteaud committed
321
            self.getProperty(variation_property)
Romain Courteaud committed
322 323
    return property_dict

Kevin Deldycke committed
324
  security.declareProtected(Permissions.ModifyPortalContent,
Romain Courteaud committed
325 326 327 328 329 330
                            'setVariationPropertyDict')
  def setVariationPropertyDict(self, property_dict):
    """
      Take a parameter a property dict like:
        {property_id: property_value,}
      Each property is a variation of the resource.
Kevin Deldycke committed
331
      If one of the property_id is not a variation, a exception
Romain Courteaud committed
332 333
      KeyError is raised.
    """
Kevin Deldycke committed
334
    resource = self.getDefaultResourceValue()
Romain Courteaud committed
335 336 337 338 339 340 341 342 343
    if resource is not None:
      variation_list = resource.getVariationPropertyList()
    else:
      variation_list = []
    for property_id, property_value in property_dict.items():
      if property_id not in variation_list:
        raise KeyError, "Can not set the property variation '%s'" % \
                        property_id
      else:
Alexandre Boeglin committed
344 345 346
        try:
          self.setProperty(property_id, property_value)
        except KeyError:
Alexandre Boeglin committed
347
          LOG("Amount", ERROR, "Can not set %s with value %s on %s" % \
Alexandre Boeglin committed
348 349
                    (property_id, property_value, self.getRelativeUrl()))
          raise
Romain Courteaud committed
350

Jean-Paul Smets committed
351 352 353
  security.declareProtected(Permissions.AccessContentsInformation,
                                                 'getQuantityUnitRangeItemList')
  def getQuantityUnitRangeItemList(self, base_category_list=()):
Romain Courteaud committed
354 355 356 357
    resource = self.getDefaultResourceValue()
    if resource is not None:
      result = resource.getQuantityUnitList()
    else:
Jean-Paul Smets committed
358 359 360 361 362 363
      result = ()
    if result is ():
      return self.portal_categories.quantity_unit.getFormItemList()
    else:
      return result

Romain Courteaud committed
364 365 366 367 368
  security.declareProtected(Permissions.AccessContentsInformation, 'getResourceDefaultQuantityUnit')
  def getResourceDefaultQuantityUnit(self):
    """
      Return default quantity unit of the resource
    """
Romain Courteaud committed
369 370 371
    resource = self.getResourceValue()
    resource_quantity_unit = None
    if resource is not None:
Romain Courteaud committed
372 373
      resource_quantity_unit = resource.getDefaultQuantityUnit()
      #LOG("ERP5 WARNING:", 100, 'could not convert quantity for %s' % self.getRelativeUrl())
Kevin Deldycke committed
374
    return  resource_quantity_unit
Romain Courteaud committed
375

Romain Courteaud committed
376 377
  security.declareProtected(Permissions.AccessContentsInformation, 'getResourcePrice')
  def getResourcePrice(self):
Romain Courteaud committed
378
    """
Jean-Paul Smets committed
379 380 381
      Return price of the resource in the current context
      
      The price is expressed in the standard unit of the resource (?)
Romain Courteaud committed
382 383
    """
    resource = self.getResourceValue()
Jean-Paul Smets committed
384 385 386 387
    if resource is not None:
      return resource.getPrice(context=self)
    return None
      
Romain Courteaud committed
388 389 390 391 392 393 394
  security.declareProtected(Permissions.AccessContentsInformation, 'getDuration')
  def getDuration(self):
    """
      Return duration in minute
    """
    quantity = self.getQuantity()
    quantity_unit = self.getQuantityUnit()
Guillaume Michon committed
395 396
    if quantity_unit is None:
      return None
Romain Courteaud committed
397
    common_time_category = 'time'
Kevin Deldycke committed
398
    if common_time_category in quantity_unit[:len(common_time_category)]:
Romain Courteaud committed
399 400 401 402 403
      duration = quantity
    else:
      duration = None
    return duration

Jean-Paul Smets committed
404 405 406 407
  def getPrice(self):
    pass
  
  
Jérome Perrin committed
408
  security.declareProtected(Permissions.AccessContentsInformation, 'getTotalPrice')
Aurel committed
409
  def getTotalPrice(self, **kw):
Romain Courteaud committed
410
    """
Jean-Paul Smets committed
411 412 413 414
      Return total price for the number of items
      
      Price is defined on 
      
Romain Courteaud committed
415
    """
Julien Muchembled committed
416 417 418 419
    price = self.getResourcePrice()
    quantity = self.getNetConvertedQuantity()
    if isinstance(price, (int, float)) and isinstance(quantity, (int, float)):
      return quantity * price
Romain Courteaud committed
420

Jean-Paul Smets committed
421 422 423 424 425 426
  # Conversion to standard unit
  security.declareProtected(Permissions.AccessContentsInformation, 'getConvertedQuantity')
  def getConvertedQuantity(self):
    """
      Converts quantity to default unit
    """
Romain Courteaud committed
427 428 429
    resource = self.getResourceValue()
    quantity_unit = self.getQuantityUnit()
    quantity = self.getQuantity()
Julien Muchembled committed
430 431 432 433 434 435 436 437
    if quantity is not None and quantity_unit and resource is not None:
      converted = resource.convertQuantity(quantity, quantity_unit,
                                           resource.getDefaultQuantityUnit(),
                                           self.getVariationCategoryList())
      # For compatibility, return quantity non-converted if conversion fails.
      if converted is not None:
        return converted
    return quantity
Jean-Paul Smets committed
438

Jean-Paul Smets committed
439 440
  security.declareProtected(Permissions.ModifyPortalContent, 'setConvertedQuantity')
  def setConvertedQuantity(self, value):
Romain Courteaud committed
441 442
    resource = self.getResourceValue()
    quantity_unit = self.getQuantityUnit()
Julien Muchembled committed
443 444 445 446 447 448 449
    if value is not None and quantity_unit and resource is not None:
      quantity = resource.convertQuantity(value,
                                          resource.getDefaultQuantityUnit(),
                                          quantity_unit,
                                          self.getVariationCategoryList())
      if quantity is not None:
        return self.setQuantity(quantity)
Jean-Paul Smets committed
450

Jean-Paul Smets committed
451 452 453 454 455 456 457
  security.declareProtected(Permissions.AccessContentsInformation, 'getNetQuantity')
  def getNetQuantity(self):
    """
      Take into account efficiency in quantity
    """
    quantity = self.getQuantity()
    efficiency = self.getEfficiency()
Jean-Paul Smets committed
458
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets committed
459 460 461 462 463 464 465
      efficiency = 1.0
    return float(quantity) / efficiency

  security.declareProtected(Permissions.AccessContentsInformation, 'getNetTargetQuantity')
  def getNetTargetQuantity(self):
    """
      Take into account efficiency in target quantity
Jean-Paul Smets committed
466
      XXX - dreprecated
Jean-Paul Smets committed
467 468 469
    """
    quantity = self.getTargetQuantity()
    efficiency = self.getTargetEfficiency()
Jean-Paul Smets committed
470
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets committed
471 472 473 474 475 476 477 478 479 480
      efficiency = 1.0
    return float(quantity) / efficiency

  security.declareProtected(Permissions.AccessContentsInformation, 'getNetConvertedQuantity')
  def getNetConvertedQuantity(self):
    """
      Take into account efficiency in converted quantity
    """
    quantity = self.getConvertedQuantity()
    efficiency = self.getEfficiency()
Jean-Paul Smets committed
481
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets committed
482
      efficiency = 1.0
Jean-Paul Smets committed
483
    if quantity not in (None, ''):
Jean-Paul Smets committed
484 485 486 487
      return float(quantity) / efficiency
    else:
      return None

Jean-Paul Smets committed
488 489 490 491 492 493
  security.declareProtected(Permissions.ModifyPortalContent, 'setNetConvertedQuantity')
  def setNetConvertedQuantity(self, value):
    """
      Take into account efficiency in converted quantity
    """
    efficiency = self.getEfficiency()
Jean-Paul Smets committed
494
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets committed
495
      efficiency = 1.0
Jean-Paul Smets committed
496
    if value not in (None, ''):
Jean-Paul Smets committed
497
      quantity = float(value) * efficiency
Jean-Paul Smets committed
498 499
    else:
      quantity = value
Jean-Paul Smets committed
500 501
    self.setConvertedQuantity(quantity)

Jean-Paul Smets committed
502 503 504 505 506 507 508
  security.declareProtected(Permissions.AccessContentsInformation, 'getNetConvertedTargetQuantity')
  def getNetConvertedTargetQuantity(self):
    """
      Take into account efficiency in converted target quantity
    """
    quantity = self.getConvertedTargetQuantity()
    efficiency = self.getTargetEfficiency()
Jean-Paul Smets committed
509
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets committed
510
      efficiency = 1.0
Jean-Paul Smets committed
511
    if quantity not in (None, ''):
Jean-Paul Smets committed
512 513 514 515
      return float(quantity) / efficiency
    else:
      return None

Jean-Paul Smets committed
516 517 518 519 520 521 522 523
  security.declareProtected(Permissions.ModifyPortalContent, 'setNetConvertedTargetQuantity')
  def setNetConvertedTargetQuantity(self, value):
    """
      Take into account efficiency in converted quantity
    """
    efficiency = self.getEfficiency()
    if efficiency in (0, 0.0, None):
      efficiency = 1.0
Jean-Paul Smets committed
524
    if value not in (None, ''):
Jean-Paul Smets committed
525
      quantity = float(value) * efficiency
Jean-Paul Smets committed
526 527
    else:
      quantity = value
Jean-Paul Smets committed
528 529
    self.setConvertedTargetQuantity(quantity)

Jean-Paul Smets committed
530 531 532 533 534 535 536 537 538
  security.declareProtected(Permissions.AccessContentsInformation, 'getInventoriatedQuantity')
  def getInventoriatedQuantity(self):
    """
      Take into account efficiency in converted target quantity
    """
    return self.getNetConvertedQuantity()

  # Helper methods to display quantities as produced / consumed
  security.declareProtected(Permissions.AccessContentsInformation, 'getProductionQuantity')
Sebastien Robin committed
539
  def getProductionQuantity(self,quantity=None):
Jean-Paul Smets committed
540 541 542
    """
      Return the produced quantity
    """
Sebastien Robin committed
543 544
    if quantity is None:
      quantity = self.getQuantity()
Jean-Paul Smets committed
545 546 547
    source = self.getSource()
    destination = self.getDestination()

Romain Courteaud committed
548
    if quantity is not None:
Jean-Paul Smets committed
549
      quantity = float(quantity)
Romain Courteaud committed
550
    else:
Jean-Paul Smets committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
      quantity = 0.0

    if source in (None, ''):
      if quantity > 0:
        return quantity
      else:
        return 0.0

    if destination in (None, ''):
      if quantity < 0:
        return - quantity
      else:
        return 0.0

  security.declareProtected(Permissions.AccessContentsInformation, 'getConsumptionQuantity')
Sebastien Robin committed
566
  def getConsumptionQuantity(self,quantity=None):
Jean-Paul Smets committed
567
    """
Sebastien Robin committed
568
      Return the consumption quantity
Jean-Paul Smets committed
569
    """
Sebastien Robin committed
570 571
    if quantity is None:
      quantity = self.getQuantity()
Jean-Paul Smets committed
572 573 574
    source = self.getSource()
    destination = self.getDestination()

Romain Courteaud committed
575
    if quantity is not None:
Jean-Paul Smets committed
576
      quantity = float(quantity)
Romain Courteaud committed
577
    else:
Jean-Paul Smets committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
      quantity = 0.0

    if destination in (None, ''):
      if quantity > 0:
        return quantity
      else:
        return 0.0

    if source in (None, ''):
      if quantity < 0:
        return - quantity
      else:
        return 0.0

  security.declareProtected(Permissions.ModifyPortalContent, 'setProductionQuantity')
  def setProductionQuantity(self, value):
    """
      Return the produced quantity
    """
    source = self.getSource()
    destination = self.getDestination()
Sebastien Robin committed
599
    quantity = value
Jean-Paul Smets committed
600

Romain Courteaud committed
601 602 603
    if quantity is not None:
      quantity = float(quantity)
    else:
Jean-Paul Smets committed
604 605 606
      quantity = 0.0

    if source in (None, ''):
Thierry committed
607
      if quantity >= 0:
Jean-Paul Smets committed
608 609 610
        self.setQuantity(quantity)

    if destination in (None, ''):
Thierry committed
611
      if quantity >= 0:
Jean-Paul Smets committed
612 613 614 615 616 617 618 619 620
        self.setQuantity(- quantity)

  security.declareProtected(Permissions.ModifyPortalContent, 'setConsumptionQuantity')
  def setConsumptionQuantity(self, value):
    """
      Return the produced quantity
    """
    source = self.getSource()
    destination = self.getDestination()
Sebastien Robin committed
621
    quantity = value
Jean-Paul Smets committed
622

Romain Courteaud committed
623 624 625
    if quantity is not None:
      quantity = float(quantity)
    else:
Jean-Paul Smets committed
626 627 628
      quantity = 0.0

    if destination in (None, ''):
Thierry committed
629
      if quantity >= 0:
Jean-Paul Smets committed
630 631 632
        self.setQuantity(quantity)

    if source in (None, ''):
Thierry committed
633
      if quantity >= 0:
Jean-Paul Smets committed
634 635
        self.setQuantity(- quantity)

Jean-Paul Smets committed
636 637 638 639 640 641 642 643 644
  # Inventory
  security.declareProtected(Permissions.AccessContentsInformation, 'getConvertedInventory')
  def getConvertedInventory(self):
    """
      provides a default inventory value - None since
      no inventory was defined.
    """
    return None

Jean-Paul Smets committed
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
#   # SKU vs. CU
#   security.declareProtected(Permissions.AccessContentsInformation, 'getStandardInventoriatedQuantity')
#   def getStandardInventoriatedQuantity(self):
#     """
#       The inventoriated quantity converted in a default unit
#       
#       For assortments, returns the inventoriated quantity in terms of number of items
#       in the assortemnt.
#       
#       For accounting, returns the quantity converted in a default unit
#     """
#     resource = self.getResourceValue()
#     result = self.getInventoriatedQuantity()
#     if resource is not None:
#       result = resource.standardiseQuantity(result)
#     return result  
    
Jean-Paul Smets committed
662
  # Profit and Loss
Jérome Perrin committed
663
  security.declareProtected(Permissions.AccessContentsInformation, 'getLostQuantity')
Jean-Paul Smets committed
664 665 666
  def getLostQuantity(self):
    return - self.getProfitQuantity()

Jérome Perrin committed
667
  security.declareProtected(Permissions.ModifyPortalContent, 'setLostQuantity')
Jean-Paul Smets committed
668 669 670 671 672
  def setLostQuantity(self, value):
    return self.setProfitQuantity(- value)

  def _setLostQuantity(self, value):
    return self._setProfitQuantity(- value)