Amount.py 15.3 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2 3
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
Jean-Paul Smets's avatar
Jean-Paul Smets 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 29 30 31 32 33 34 35 36 37 38 39 40 41 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 117 118
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from Globals import InitializeClass
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5Type.Base import Base
from Products.ERP5.VariationValue import VariationValue
from Products.ERP5.Variated import Variated

from zLOG import LOG


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)

    -
  """

  # Declarative security
  security = ClassSecurityInfo()

  # Declarative interfaces
  __implements__ = (Interface.Variated)

  # A few more mix-in methods which should be relocated
  # THIS MUST BE UPDATE WITH CATEGORY ACQUISITION
  security.declareProtected(Permissions.AccessContentsInformation, 'getVariationCategoryList')
  def getVariationCategoryList(self, base_category_list = ()):
    """
      Returns the possible discrete variations
      (as a list of relative urls to categories)
    """
    result = []
    resource = self.getDefaultResourceValue()
    if resource is not None:
      resource_variation_list = resource.getVariationBaseCategoryList()
      if len(base_category_list) > 0 :
        variation_list = []
        for base_category in resource_variation_list :
          if base_category in base_category_list :
            variation_list.append(base_category)
      else :
        variation_list = resource_variation_list
      #LOG('in getVariationCategoryList', 0, str(variation_list))
      if len(variation_list) > 0:
        result = self.getAcquiredCategoryMembershipList(variation_list, base = 1)
    return result

  security.declareProtected(Permissions.ModifyPortalContent, '_setVariationCategoryList')
  def _setVariationCategoryList(self, value):
    result = []
    resource = self.getDefaultResourceValue()
    if resource is not None:
      variation_list = resource.getVariationBaseCategoryList()
      #LOG("_setVariationCategoryList",0,str(variation_list))
      #LOG("_setVariationCategoryList",0,str(value))
      if len(variation_list) > 0:
        self._setCategoryMembership(variation_list, value, base = 1)

  security.declareProtected(Permissions.ModifyPortalContent, 'setVariationCategoryList')
  def setVariationCategoryList(self, value):
    self._setVariationCategoryList(value)
    self.reindexObject()

  security.declareProtected(Permissions.AccessContentsInformation, 'getVariationValue')
  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()

119

Jean-Paul Smets's avatar
Jean-Paul Smets committed
120
  security.declareProtected(Permissions.AccessContentsInformation,
121
                                                'getVariationRangeCategoryItemList')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
122
  def getVariationRangeCategoryItemList(self, base_category_list = (),
123
                                        method_id='getTitle', base=1,  start_with_item=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
124 125 126 127 128 129
    """
      Returns possible category items for this amount ie.
      the variation of the resource (not the variation range)
    """
    try:
      return self.getDefaultResourceValue().getVariationCategoryItemList(
130
               base_category_list, method_id=method_id, base=base, start_with_item=start_with_item)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
131
    except:
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
      # FIXME: method_name vs. method_id, start_with_item vs. start_with_empty, etc. -yo
      return self.portal_categories.getCategoryChildItemList()

  security.declareProtected(Permissions.AccessContentsInformation,
                                              'getVariationRangeCategoryList')
  def getVariationRangeCategoryList(self, base_category_list = (), base=1):
    """
      Returns possible categories for this amount ie.
      the variation of the resource (not the variation range)
    """
    try:
      # FIXME: no base argument in getVariationCategoryList -yo
      return self.getDefaultResourceValue().getVariationCategoryList(base_category_list=base_category_list)
    except:
      # FIXME: method_name vs. method_id, etc. -yo
      return self.portal_categories.getCategoryChildList()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

  security.declareProtected(Permissions.AccessContentsInformation,
                                            'getVariationRangeBaseCategoryList')
  def getVariationRangeBaseCategoryList(self):
    """
        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
    """
    try:
      return self.getDefaultResourceValue().getVariationBaseCategoryList()
    except:
      return self.portal_categories.getObjectIds()

  security.declareProtected(Permissions.AccessContentsInformation,
                                                 'getQuantityUnitRangeItemList')
  def getQuantityUnitRangeItemList(self, base_category_list=()):
    try:
      result = self.getDefaultResourceValue().getQuantityUnitList()
    except:
      result = ()
    if result is ():
      return self.portal_categories.quantity_unit.getFormItemList()
    else:
      return result

  # Conversion to standard unit
  security.declareProtected(Permissions.AccessContentsInformation, 'getConvertedQuantity')
  def getConvertedQuantity(self):
    """
      Converts quantity to default unit
    """
    try:
    #if 1:
      resource = self.getResourceValue()
      resource_quantity_unit = resource.getDefaultQuantityUnit()
      quantity_unit = self.getQuantityUnit()
      quantity = self.getQuantity()
      converted_quantity = resource.convertQuantity(quantity, quantity_unit, resource_quantity_unit)
    except:
      LOG("ERP5 WARNING:", 100, 'could not convert quantity for %s' % self.getRelativeUrl())
      converted_quantity = None
    return converted_quantity

197 198 199 200 201 202 203 204 205 206 207 208
  security.declareProtected(Permissions.ModifyPortalContent, 'setConvertedQuantity')
  def setConvertedQuantity(self, value):
    try:
    #if 1:
      resource = self.getResourceValue()
      resource_quantity_unit = resource.getDefaultQuantityUnit()
      quantity_unit = self.getQuantityUnit()
      quantity = resource.convertQuantity(value, resource_quantity_unit, quantity_unit)
      self.setQuantity(quantity)
    except:
      LOG("ERP5 WARNING:", 100, 'could not set converted quantity for %s' % self.getRelativeUrl())

Jean-Paul Smets's avatar
Jean-Paul Smets committed
209 210 211 212 213
  security.declareProtected(Permissions.AccessContentsInformation, 'getConvertedTargetQuantity')
  def getConvertedTargetQuantity(self):
    """
      Converts target_quantity to default unit
    """
214
    #if 1:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
215
    try:
216
    #if 1:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
217 218 219 220 221
      resource = self.getResourceValue()
      resource_quantity_unit = resource.getDefaultQuantityUnit()
      quantity_unit = self.getQuantityUnit()
      quantity = self.getTargetQuantity()
      converted_quantity = resource.convertQuantity(quantity, quantity_unit, resource_quantity_unit)
222
    #else:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
223
    except:
224
    #else:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
225 226 227 228
      LOG("ERP5 WARNING:", 100, 'could not convert target_quantity for %s' % self.getRelativeUrl())
      converted_quantity = None
    return converted_quantity

229 230 231 232 233 234 235 236 237 238 239 240
  security.declareProtected(Permissions.ModifyPortalContent, 'setConvertedTargetQuantity')
  def setConvertedTargetQuantity(self, value):
    try:
    #if 1:
      resource = self.getResourceValue()
      resource_quantity_unit = resource.getDefaultQuantityUnit()
      quantity_unit = self.getQuantityUnit()
      quantity = resource.convertQuantity(value, resource_quantity_unit, quantity_unit)
      self.setTargetQuantity(quantity)
    except:
      LOG("ERP5 WARNING:", 100, 'could not set converted quantity for %s' % self.getRelativeUrl())

Jean-Paul Smets's avatar
Jean-Paul Smets committed
241 242 243 244 245 246 247
  security.declareProtected(Permissions.AccessContentsInformation, 'getNetQuantity')
  def getNetQuantity(self):
    """
      Take into account efficiency in quantity
    """
    quantity = self.getQuantity()
    efficiency = self.getEfficiency()
248
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
249 250 251 252 253 254 255 256 257 258
      efficiency = 1.0
    return float(quantity) / efficiency

  security.declareProtected(Permissions.AccessContentsInformation, 'getNetTargetQuantity')
  def getNetTargetQuantity(self):
    """
      Take into account efficiency in target quantity
    """
    quantity = self.getTargetQuantity()
    efficiency = self.getTargetEfficiency()
259
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
260 261 262 263 264 265 266 267 268 269
      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()
270
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
271
      efficiency = 1.0
272
    if quantity not in (None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
273 274 275 276
      return float(quantity) / efficiency
    else:
      return None

277 278 279 280 281 282
  security.declareProtected(Permissions.ModifyPortalContent, 'setNetConvertedQuantity')
  def setNetConvertedQuantity(self, value):
    """
      Take into account efficiency in converted quantity
    """
    efficiency = self.getEfficiency()
283
    if efficiency in (0, 0.0, None, ''):
284
      efficiency = 1.0
285
    if value not in (None, ''):
286
      quantity = float(value) * efficiency
287 288
    else:
      quantity = value
289 290
    self.setConvertedQuantity(quantity)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
291 292 293 294 295 296 297
  security.declareProtected(Permissions.AccessContentsInformation, 'getNetConvertedTargetQuantity')
  def getNetConvertedTargetQuantity(self):
    """
      Take into account efficiency in converted target quantity
    """
    quantity = self.getConvertedTargetQuantity()
    efficiency = self.getTargetEfficiency()
298
    if efficiency in (0, 0.0, None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
299
      efficiency = 1.0
300
    if quantity not in (None, ''):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
301 302 303 304
      return float(quantity) / efficiency
    else:
      return None

305 306 307 308 309 310 311 312
  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
313
    if value not in (None, ''):
314
      quantity = float(value) * efficiency
315 316
    else:
      quantity = value
317 318
    self.setConvertedTargetQuantity(quantity)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
  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')
  def getProductionQuantity(self):
    """
      Return the produced quantity
    """
    quantity = self.getQuantity()
    source = self.getSource()
    destination = self.getDestination()

    try:
      quantity = float(quantity)
    except:
      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')
  def getConsumptionQuantity(self):
    """
      Return the produced quantity
    """
    quantity = self.getQuantity()
    source = self.getSource()
    destination = self.getDestination()

    try:
      quantity = float(quantity)
    except:
      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()

    try:
      quantity = float(value)
    except:
      quantity = 0.0

    if source in (None, ''):
393
      if quantity >= 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
394 395 396 397 398
        self.setQuantity(quantity)
      else:
        return 0.0

    if destination in (None, ''):
399
      if quantity >= 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
        self.setQuantity(- quantity)
      else:
        return 0.0

  security.declareProtected(Permissions.ModifyPortalContent, 'setConsumptionQuantity')
  def setConsumptionQuantity(self, value):
    """
      Return the produced quantity
    """
    source = self.getSource()
    destination = self.getDestination()

    try:
      quantity = float(value)
    except:
      quantity = 0.0

    if destination in (None, ''):
418
      if quantity >= 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
419 420 421 422 423
        self.setQuantity(quantity)
      else:
        return 0.0

    if source in (None, ''):
424
      if quantity >= 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
425 426 427 428
        self.setQuantity(- quantity)
      else:
        return 0.0

429 430 431 432 433 434 435 436 437
  # Inventory
  security.declareProtected(Permissions.AccessContentsInformation, 'getConvertedInventory')
  def getConvertedInventory(self):
    """
      provides a default inventory value - None since
      no inventory was defined.
    """
    return None

438 439 440 441 442 443 444 445 446 447 448
  # Profit and Loss
  security.declareProtected(Permissions.ModifyPortalContent, 'getLostQuantity')
  def getLostQuantity(self):
    return - self.getProfitQuantity()

  security.declareProtected(Permissions.AccessContentsInformation, 'setLostQuantity')
  def setLostQuantity(self, value):
    return self.setProfitQuantity(- value)

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