Amount.py 24 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
#
# 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.
#
##############################################################################

30
import zope.interface
Jean-Paul Smets's avatar
Jean-Paul Smets committed
31 32
from Globals import InitializeClass
from AccessControl import ClassSecurityInfo
33 34
from Products.ERP5.Variated import Variated
from Products.ERP5.VariationValue import VariationValue
35
from Products.ERP5Type import Permissions, PropertySheet, Constraint, interfaces
Jean-Paul Smets's avatar
Jean-Paul Smets committed
36
from Products.ERP5Type.Base import Base
37
from Products.ERP5Type.Base import TempBase
38
from Products.CMFCategory.Renderer import Renderer
Jean-Paul Smets's avatar
Jean-Paul Smets committed
39

40

41
from zLOG import LOG, ERROR
42
from warnings import warn
Jean-Paul Smets's avatar
Jean-Paul Smets committed
43

44

Jean-Paul Smets's avatar
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)

    -
  """

57 58 59
  meta_type = 'ERP5 Amount'
  portal_type = 'Amount'

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

  # Declarative interfaces
65
  zope.interface.implements(interfaces.IVariated,)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
66

67 68 69 70
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.SimpleItem
                    , PropertySheet.Amount
                    , PropertySheet.Price
71
                    )
72

Jean-Paul Smets's avatar
Jean-Paul Smets committed
73 74
  # A few more mix-in methods which should be relocated
  # THIS MUST BE UPDATE WITH CATEGORY ACQUISITION
75 76
  security.declareProtected(Permissions.AccessContentsInformation,
                            'getVariationCategoryList')
77
  def getVariationCategoryList(self, default=[], base_category_list=(),
78
      omit_optional_variation=0, omit_option_base_category=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
79 80 81 82
    """
      Returns the possible discrete variations
      (as a list of relative urls to categories)
    """
83 84 85 86 87 88
    #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's avatar
Jean-Paul Smets committed
89 90 91
    result = []
    resource = self.getDefaultResourceValue()
    if resource is not None:
92
      resource_variation_list = resource.getVariationBaseCategoryList(
93
          omit_optional_variation=omit_optional_variation)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
94
      if len(base_category_list) > 0 :
95 96
        variation_list = filter(lambda x: x in base_category_list,
                                resource_variation_list)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
97 98 99
      else :
        variation_list = resource_variation_list
      if len(variation_list) > 0:
100
        result = self.getAcquiredCategoryMembershipList(variation_list, base=1)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
101 102
    return result

103
  security.declareProtected(Permissions.AccessContentsInformation,
104
                            'getVariationCategoryItemList')
105
  def getVariationCategoryItemList(self, base_category_list=(), base=1,
106
                                   display_id='logical_path',
107
                                   current_category=None,**kw):
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    """
      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,
129
                             display_id=display_id, **kw).\
130 131 132 133 134
                                               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,
135
                             base_category=base_category,
136 137
                             display_none_category=0, base=base,
                             current_category=current_category,
138
                             display_id='title', **kw).\
139 140 141
                                               render(object_list))
    return variation_category_item_list

142 143
  security.declareProtected(Permissions.ModifyPortalContent, 
                            '_setVariationCategoryList')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
144 145 146 147 148 149 150 151
  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)

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

158
  security.declareProtected(Permissions.AccessContentsInformation,
159
                            'getVariationBaseCategoryList')
160
  def getVariationBaseCategoryList(self, default=[],
161
      omit_optional_variation=0, omit_option_base_category=None):
162
    """
163
      Return the list of base_category from all variation related to
164
      amount.
165 166
      It is maybe a nonsense, but useful for correcting user errors.
    """
167 168 169 170 171 172
    #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

173
    base_category_list = []
174
    for category in self.getVariationCategoryList(
175
        omit_optional_variation=omit_optional_variation):
176 177 178 179
      base_category = category.split('/')[0]
      if base_category not in base_category_list:
        base_category_list.append(base_category)
    return base_category_list
180

181 182
  security.declareProtected(Permissions.AccessContentsInformation, 
                            'getVariationBaseCategoryItemList')
183
  def getVariationBaseCategoryItemList(self,display_id='getTitleOrId',**kw):
184 185 186 187 188 189 190 191 192
    """
    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's avatar
Jean-Paul Smets committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  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()

214 215
  security.declareProtected(Permissions.AccessContentsInformation, \
                            'getVariationRangeCategoryItemList')
216
  def getVariationRangeCategoryItemList(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
217
    """
218 219 220 221 222 223
      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's avatar
Jean-Paul Smets committed
224
    """
225
    resource = self.getResourceValue()
Fabien Morin's avatar
Fabien Morin committed
226
    if resource is not None:
227
      result = resource.getVariationCategoryItemList(
228
                               omit_individual_variation=0,**kw)
229 230 231
    else:
      result = []
    return result
232

233 234
  security.declareProtected(Permissions.AccessContentsInformation, \
                            'getVariationRangeCategoryList')
235 236
  def getVariationRangeCategoryList(self, default=[], base_category_list=(),
      base=1, **kw):
237
    """
238 239
      Returns possible variation category values for the
      order line according to the default resource.
240
    """
241 242
    return [x[1] for x in self.getVariationRangeCategoryItemList(
                                     base_category_list=base_category_list,
243
                                     base=base, **kw)]
Jean-Paul Smets's avatar
Jean-Paul Smets committed
244 245

  security.declareProtected(Permissions.AccessContentsInformation,
246
                            'getVariationRangeBaseCategoryList')
247 248
  def getVariationRangeBaseCategoryList(self, default=[],
      omit_optional_variation=0, omit_option_base_category=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
249 250 251 252 253 254 255 256 257 258
    """
        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
    """
259 260 261 262 263 264
    #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

265
    resource = self.getDefaultResourceValue()
266
    if resource is not None:
267
      result = resource.getVariationBaseCategoryList(
268
          omit_optional_variation=omit_optional_variation)
269
    else:
270
      result = Variated.getVariationRangeBaseCategoryList(self)
271
    return result
Jean-Paul Smets's avatar
Jean-Paul Smets committed
272

273 274
  security.declareProtected(Permissions.AccessContentsInformation,
                            'getVariationRangeBaseCategoryItemList')
275 276 277
  def getVariationRangeBaseCategoryItemList(self, omit_optional_variation=0,
      omit_option_base_category=None, display_id="title",
      display_none_category=0):
278 279 280 281 282
    """
        Returns possible variations base categories for this amount ie.
        the variation base category of the resource (not the
        variation range).
    """
283 284 285 286 287 288
    #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

289
    return self.portal_categories.getItemList(
290 291 292 293
        self.getVariationRangeBaseCategoryList(
            omit_optional_variation=omit_optional_variation),
        display_id=display_id,
        display_none_category=display_none_category)
294

295 296 297 298 299 300 301 302 303 304
  #####################################################################
  #  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.
305
      The variation property list is defined on resource,
306 307 308
      with setVariationPropertyList.
    """
    property_dict = {}
309
    resource = self.getDefaultResourceValue()
310 311 312 313
    if resource is not None:
      variation_list = resource.getVariationPropertyList()
      for variation_property in variation_list:
        property_dict[variation_property] = \
314
            self.getProperty(variation_property)
315 316
    return property_dict

317
  security.declareProtected(Permissions.ModifyPortalContent,
318 319 320 321 322 323
                            '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.
324
      If one of the property_id is not a variation, a exception
325 326
      KeyError is raised.
    """
327
    resource = self.getDefaultResourceValue()
328 329 330 331 332 333 334 335 336
    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:
337 338 339
        try:
          self.setProperty(property_id, property_value)
        except KeyError:
340
          LOG("Amount", ERROR, "Can not set %s with value %s on %s" % \
341 342
                    (property_id, property_value, self.getRelativeUrl()))
          raise
343

Jean-Paul Smets's avatar
Jean-Paul Smets committed
344 345 346
  security.declareProtected(Permissions.AccessContentsInformation,
                                                 'getQuantityUnitRangeItemList')
  def getQuantityUnitRangeItemList(self, base_category_list=()):
347 348 349 350
    resource = self.getDefaultResourceValue()
    if resource is not None:
      result = resource.getQuantityUnitList()
    else:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
351 352 353 354 355 356
      result = ()
    if result is ():
      return self.portal_categories.quantity_unit.getFormItemList()
    else:
      return result

357 358 359 360 361
  security.declareProtected(Permissions.AccessContentsInformation, 'getResourceDefaultQuantityUnit')
  def getResourceDefaultQuantityUnit(self):
    """
      Return default quantity unit of the resource
    """
362 363 364
    resource = self.getResourceValue()
    resource_quantity_unit = None
    if resource is not None:
365 366
      resource_quantity_unit = resource.getDefaultQuantityUnit()
      #LOG("ERP5 WARNING:", 100, 'could not convert quantity for %s' % self.getRelativeUrl())
367
    return  resource_quantity_unit
368

369 370
  security.declareProtected(Permissions.AccessContentsInformation, 'getResourcePrice')
  def getResourcePrice(self):
371
    """
372 373 374
      Return price of the resource in the current context
      
      The price is expressed in the standard unit of the resource (?)
375 376
    """
    resource = self.getResourceValue()
377 378 379 380
    if resource is not None:
      return resource.getPrice(context=self)
    return None
      
381 382 383 384 385 386 387
  security.declareProtected(Permissions.AccessContentsInformation, 'getDuration')
  def getDuration(self):
    """
      Return duration in minute
    """
    quantity = self.getQuantity()
    quantity_unit = self.getQuantityUnit()
388 389
    if quantity_unit is None:
      return None
390
    common_time_category = 'time'
391
    if common_time_category in quantity_unit[:len(common_time_category)]:
392 393 394 395 396
      duration = quantity
    else:
      duration = None
    return duration

397 398 399 400
  def getPrice(self):
    pass
  
  
401
  security.declareProtected(Permissions.AccessContentsInformation, 'getTotalPrice')
Aurel's avatar
Aurel committed
402
  def getTotalPrice(self, **kw):
403
    """
404 405 406 407
      Return total price for the number of items
      
      Price is defined on 
      
408
    """
409 410 411 412
    price = self.getResourcePrice()
    quantity = self.getNetConvertedQuantity()
    if isinstance(price, (int, float)) and isinstance(quantity, (int, float)):
      return quantity * price
413

Jean-Paul Smets's avatar
Jean-Paul Smets committed
414 415 416 417 418 419
  # Conversion to standard unit
  security.declareProtected(Permissions.AccessContentsInformation, 'getConvertedQuantity')
  def getConvertedQuantity(self):
    """
      Converts quantity to default unit
    """
420 421 422
    resource = self.getResourceValue()
    quantity_unit = self.getQuantityUnit()
    quantity = self.getQuantity()
423 424 425 426 427 428 429 430
    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's avatar
Jean-Paul Smets committed
431

432 433
  security.declareProtected(Permissions.ModifyPortalContent, 'setConvertedQuantity')
  def setConvertedQuantity(self, value):
434 435
    resource = self.getResourceValue()
    quantity_unit = self.getQuantityUnit()
436 437 438 439 440 441 442
    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)
443

Jean-Paul Smets's avatar
Jean-Paul Smets committed
444 445 446 447 448 449 450
  security.declareProtected(Permissions.AccessContentsInformation, 'getNetQuantity')
  def getNetQuantity(self):
    """
      Take into account efficiency in quantity
    """
    quantity = self.getQuantity()
    efficiency = self.getEfficiency()
451
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
452 453 454 455 456 457 458
      efficiency = 1.0
    return float(quantity) / efficiency

  security.declareProtected(Permissions.AccessContentsInformation, 'getNetTargetQuantity')
  def getNetTargetQuantity(self):
    """
      Take into account efficiency in target quantity
459
      XXX - dreprecated
Jean-Paul Smets's avatar
Jean-Paul Smets committed
460 461 462
    """
    quantity = self.getTargetQuantity()
    efficiency = self.getTargetEfficiency()
463
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
464 465 466 467 468 469 470 471 472 473
      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()
474
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
475
      efficiency = 1.0
476
    if quantity not in (None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
477 478 479 480
      return float(quantity) / efficiency
    else:
      return None

481 482 483 484 485 486
  security.declareProtected(Permissions.ModifyPortalContent, 'setNetConvertedQuantity')
  def setNetConvertedQuantity(self, value):
    """
      Take into account efficiency in converted quantity
    """
    efficiency = self.getEfficiency()
487
    if efficiency in (0, 0.0, None, ''):
488
      efficiency = 1.0
489
    if value not in (None, ''):
490
      quantity = float(value) * efficiency
491 492
    else:
      quantity = value
493 494
    self.setConvertedQuantity(quantity)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
495 496 497 498 499 500 501
  security.declareProtected(Permissions.AccessContentsInformation, 'getNetConvertedTargetQuantity')
  def getNetConvertedTargetQuantity(self):
    """
      Take into account efficiency in converted target quantity
    """
    quantity = self.getConvertedTargetQuantity()
    efficiency = self.getTargetEfficiency()
502
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
503
      efficiency = 1.0
504
    if quantity not in (None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
505 506 507 508
      return float(quantity) / efficiency
    else:
      return None

509 510 511 512 513 514 515 516
  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
517
    if value not in (None, ''):
518
      quantity = float(value) * efficiency
519 520
    else:
      quantity = value
521 522
    self.setConvertedTargetQuantity(quantity)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
523 524 525 526 527 528 529 530 531
  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')
532
  def getProductionQuantity(self,quantity=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
533 534 535
    """
      Return the produced quantity
    """
536 537
    if quantity is None:
      quantity = self.getQuantity()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
538 539 540
    source = self.getSource()
    destination = self.getDestination()

541
    if quantity is not None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
542
      quantity = float(quantity)
543
    else:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
      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')
559
  def getConsumptionQuantity(self,quantity=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
560
    """
561
      Return the consumption quantity
Jean-Paul Smets's avatar
Jean-Paul Smets committed
562
    """
563 564
    if quantity is None:
      quantity = self.getQuantity()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
565 566 567
    source = self.getSource()
    destination = self.getDestination()

568
    if quantity is not None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
569
      quantity = float(quantity)
570
    else:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
      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()
592
    quantity = value
Jean-Paul Smets's avatar
Jean-Paul Smets committed
593

594 595 596
    if quantity is not None:
      quantity = float(quantity)
    else:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
597 598 599
      quantity = 0.0

    if source in (None, ''):
600
      if quantity >= 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
601 602 603
        self.setQuantity(quantity)

    if destination in (None, ''):
604
      if quantity >= 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
605 606 607 608 609 610 611 612 613
        self.setQuantity(- quantity)

  security.declareProtected(Permissions.ModifyPortalContent, 'setConsumptionQuantity')
  def setConsumptionQuantity(self, value):
    """
      Return the produced quantity
    """
    source = self.getSource()
    destination = self.getDestination()
614
    quantity = value
Jean-Paul Smets's avatar
Jean-Paul Smets committed
615

616 617 618
    if quantity is not None:
      quantity = float(quantity)
    else:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
619 620 621
      quantity = 0.0

    if destination in (None, ''):
622
      if quantity >= 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
623 624 625
        self.setQuantity(quantity)

    if source in (None, ''):
626
      if quantity >= 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
627 628
        self.setQuantity(- quantity)

629 630 631 632 633 634 635 636 637
  # Inventory
  security.declareProtected(Permissions.AccessContentsInformation, 'getConvertedInventory')
  def getConvertedInventory(self):
    """
      provides a default inventory value - None since
      no inventory was defined.
    """
    return None

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
#   # 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  
    
655
  # Profit and Loss
656
  security.declareProtected(Permissions.AccessContentsInformation, 'getLostQuantity')
657 658 659
  def getLostQuantity(self):
    return - self.getProfitQuantity()

660
  security.declareProtected(Permissions.ModifyPortalContent, 'setLostQuantity')
661 662 663 664 665
  def setLostQuantity(self, value):
    return self.setProfitQuantity(- value)

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