Movement.py 28.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
#
# 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.ERP5.Core import MetaNode, MetaResource

from Products.ERP5Type.XMLObject import XMLObject

37
from Products.ERP5.Document.Amount import Amount
Jean-Paul Smets's avatar
Jean-Paul Smets committed
38

39 40
from zLOG import LOG

Jean-Paul Smets's avatar
Jean-Paul Smets committed
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 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
class Movement(XMLObject, Amount):
  """
    The Movement class allows to implement ERP5 universal accounting model.

    Movement instances are used in different situations:

    - Orders: Movement instances are use as a documentary object
      to define quantities in orders

    - Deliveries: movements track the actual transfer of resources
      in the past (accounting) or in the future (planning / budgetting)

    For example, the following objects are Orders:

    - a purchase order (the document we send to a supplier
      when we need some goods)

    - a sales order (the document we ask our customer
      to sign to confirm a sale)

    - a production order (the document we send to the workshop
      to confirm we need some operation / service to be achieved)

    Orders allow to describe a target, but can not be used to account
    the reality of actual transfered quantities.

    This is not the case for Deliveries:

    - an invoice (a delivery of money between abstract accounts)

    - a packing list (ie. a delivery of goods shipped)

    - a delivery report (ie. a delivery of goods received)

    - a production report (ie. a delivery of service)

    - a T/T report (a delivery of money between reals accounts)

    For simple planning, we use a simple approach. Deliveries contain
    a reference to an order (causality) 2 quantities:

    - a quantity value

    - a target_quantity value

    When quantity and target_quantity, the delivery can be closed.
    When quantity and target_quantity is different, a new delivery is created
    with 0 quantity and target_quantity = target_quantity - quantity.
    This simple system allows to delivery goods of a single order in
    multiple times, and to keep track of deliveries without any complex
    planning system. It is the basis of ERP5 trade module.

    If we need to implement more complex planning, such as in a production
    workshop, we can not use such a simple model. We have to implement a
    planning system. The approach in ERP5 is based on "simulation".

    In this case, target_quantity becomes meaningless.

    For complex planning, the following approach is used instead.

    1- Movements from an order are never modified once the order
       is confirmed. This is a rule. An Order is like a contract.
       It can only be amended, if all parties agree.

    2- Movements in a delivery may exist on their own
       (ex. an accounting transaction). Past movements
       can not be modified. Future movements may or may
       not be modified

    When an order is confirmed, the list of "order" movements
    it contains is copied into "delivery" movements. Each delivery
    movement contains a "causality" reference to the order
    it. This allows delivery to be completely different from order
    (ex. different resource, different date, different quantity)
    and allows to keep track of the causal relation between
    a delivery and an order.

    A delivery document (actuall a delivery line) then points to one or more of
    the "delivery" movements in the simulation. It is possible to know
    which items have been delivered by making sure each movement in the simulation
    is associated to a "delivery document".

    By looking at all "simulation"

    Delivery movements can be applied the following transformations:

    - split : one movement is cut into 2 or more movements

    - submovements : one movement "generates" many other movements.
      For example, a delivery of goods from the workshop to the stock,
      will result in a "pull" calculation which generates operations
      and sourcing based on a BOM document. The causality of each
      delivery is the "applied rule" which was used to generate submovements

    One should note that

    - movements are never joined (because it would breack causality and
      tracability)

    - movements acquire some context information from their causality

    Some submovements need no order to be "confirmed". Some submovements
    need an order to be "confirmed". For example

    - a submovement which allows to compute the CO2 emissions
      of a production facility needs no order confirmation
      (this kind of movement is mostly ised for reporting)

    - a submovement which takes some goods in a stock and
      brings them to a workshop needs some "stock movement"
      order to be "confirmed"

    - a submovement which requires someone to take his time
      for some operation nees a "service order" to be confirmed

    This means that the simulation process must make a distinction
    between different workflows applicable to a movement. For
    movements which require an order to be confirmed, the workflow
    involves the following steps:

    - an order is automaticaly generated, with "order movements"
      which become "causalities" for delivery movements (XXX
      this sound strange...)

    - each order movement is associated to one of the delivery

    As a result, a delivery movement which requires an order may
    have 2 causalities

    - one causality (ie. application of a rule)

    - another causality (ie. confirmation in an order)

    Each causality may define its own context. One context
    may be related for example to a customer request, another context
    may be related to an "internal customer" request (eg. the production manager).
    Context may eventually conflict each other.

    In a customer oriented company, movements should probably
    always be stored within the customer order and acquire
    from the order all context information.

    In a mass production facility, context may be defined in more
    than one location. This is an incentive for putting
    all movements in a simulation "box".

    The second approach is chosen for documentary consistency approach :
      in ERP5, documents rules, can be synchronized. Simulation can not be
      synchronized
  """
  meta_type = 'ERP5 Movement'
  portal_type = 'Movement'
193
  add_permission = Permissions.AddPortalContent
Jean-Paul Smets's avatar
Jean-Paul Smets committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  isPortalContent = 1
  isRADContent = 1
  isMovement = 1

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

  # Declarative interfaces
  __implements__ = ( Interface.Variated, )

  # Declarative properties
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.SimpleItem
                    , PropertySheet.Amount
                    , PropertySheet.Task
                    , PropertySheet.Arrow
                    , PropertySheet.Movement
                    , PropertySheet.Price
                    )

  # Pricing methods
  # _getPrice is defined in the order / delivery
  # Pricing mehod
  def _getPrice(self, context):
    # Call a script on the context
    return context.Movement_lookupPrice()

  def _getTotalPrice(self, context):
    price = self.getPrice(context=context)
    quantity = self.getQuantity()
225 226
    if type(price) in (type(1.0), type(1)) and \
        type(quantity) in (type(1.0), type(1)):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
227 228 229 230 231 232 233
      return quantity * price
    else:
      return None

  security.declareProtected(Permissions.AccessContentsInformation, 'getPrice')
  def getPrice(self, context=None, REQUEST=None, **kw):
    """
234 235 236
      Get the Price in the context.

      If price is not stored locally, lookup a price and store it.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
237 238 239
    """
    local_price = self._baseGetPrice()
    if local_price is None:
240 241 242
      # We must find a price for this movement
      local_price = self._getPrice(self.asContext(
                            context=context, REQUEST=REQUEST, **kw))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
243 244 245 246
      # And store it localy
      if local_price is not None: self.setPrice(local_price)
    return local_price

247 248
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getTotalPrice')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
249 250
  def getTotalPrice(self, context=None, REQUEST=None, **kw):
    """
251
      Get the Total Price in the context.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
252 253 254
    """
    return self._getTotalPrice(self.asContext(context=context, REQUEST=REQUEST, **kw))

255 256
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getTotalQuantity')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
257 258 259 260 261 262
  def getTotalQuantity(self):
    """
      Returns the quantity if no cell or the total quantity if cells
    """
    return self.getQuantity()

263
  # Industrial price API
264 265
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getIndustrialPrice')
266 267 268 269 270 271 272 273 274
  def getIndustrialPrice(self):
    """
      Calculates industrial price in context of this movement
    """
    resource = self.getResourceValue()
    if resource is not None:
      return resource.getIndustrialPrice(context=self)
    return None

275
  # Asset price calculation
276 277
  security.declareProtected(Permissions.AccessContentsInformation,
                            'getSourceInventoriatedTotalAssetPrice')
278
  def getSourceInventoriatedTotalAssetPrice(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
279
    """
280 281
      Returns a price which can be used to calculate stock value (asset)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
282 283
      Asset price is used for calculation of inventory asset value
      and for accounting
284 285 286 287 288 289 290 291 292

      If the asset price is specified (as in accounting for multi-currency),
      then it is returned. If no asset price is specified, then we use
      the price as defined on the line, but only for incoming quantities
      (purchase price, industrial price, etc.).

      For outgoing quantities, it is the responsability of database
      to calculate asset prices based on calculation rules (FIFO,
      FILO, AVERAGE, etc.).
Jean-Paul Smets's avatar
Jean-Paul Smets committed
293
    """
294
    result = self.getSourceTotalAssetPrice() # This is what we use for accounting
Jean-Paul Smets's avatar
Jean-Paul Smets committed
295
    if result is not None: return result
296
    quantity = self.getQuantity()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
297
    if quantity > 0.0:
298
      return None                       # Outgoing quantity
Jean-Paul Smets's avatar
Jean-Paul Smets committed
299
    elif quantity < 0.0:
300
      return self.getSourceAssetPrice() * quantity # XXX: price should be converted to the source currency
301
    return None
302

303 304
  security.declareProtected(Permissions.AccessContentsInformation, 'getDestinationInventoriatedTotalAssetPrice')
  def getDestinationInventoriatedTotalAssetPrice(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
305
    """
306 307
      Returns a price which can be used to calculate stock value (asset)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
308 309 310
      Asset price is used for calculation of inventory asset value
      and for accounting
    """
311
    result = self.getDestinationTotalAssetPrice() # This is what we use for accounting
Jean-Paul Smets's avatar
Jean-Paul Smets committed
312
    if result is not None: return result
313
    quantity = self.getQuantity()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
314
    if quantity < 0.0:
315
      return None                       # Outgoing quantity
Jean-Paul Smets's avatar
Jean-Paul Smets committed
316
    elif quantity > 0.0:
317
      return self.getDestinationAssetPrice() * quantity # XXX: price should be converted to the dest. currency
318
    return None
Jean-Paul Smets's avatar
Jean-Paul Smets committed
319

320 321
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSourceAssetPrice')
322 323 324 325
  def getSourceAssetPrice(self):
    """
      Returns the price converted to the currency of the source section

326
      This will be implemeted by calling currency conversion on currency resources
327
    """
328
    return self.getPrice() # XXX Not implemented yet TODO
329

330 331
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDestinationAssetPrice')
332 333 334 335
  def getDestinationAssetPrice(self):
    """
      Returns the price converted to the currency of the destination section
    """
336
    return self.getPrice() # XXX Not implemented yet TODO
337

Jean-Paul Smets's avatar
Jean-Paul Smets committed
338 339 340 341 342 343
  # Causality computation
  security.declareProtected(Permissions.View, 'isConvergent')
  def isConvergent(self):
    """
      Returns 0 if the target is not met
    """
344
    return int(not self.isDivergent())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
345 346 347 348

  security.declareProtected(Permissions.View, 'isDivergent')
  def isDivergent(self):
    """
349
      XXX documentation out of sync with actual use
Jean-Paul Smets's avatar
Jean-Paul Smets committed
350 351 352 353 354 355
      Returns 1 if the target is not met according to the current information
      After and edit, the isOutOfTarget will be checked. If it is 1,
      a message is emitted

      emit targetUnreachable !
    """
Sebastien Robin's avatar
Sebastien Robin committed
356 357 358
    for simulation_movement in self.getDeliveryRelatedValueList():
      if simulation_movement.isDivergent():
        return 1
359
    return 0
Jean-Paul Smets's avatar
Jean-Paul Smets committed
360

361 362
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getExplanation')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
363
  def getExplanation(self):
364
    """
365
      Returns the relative_url of the explanation of this movement.
366
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
367 368
    return self.getDelivery()

369 370
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getExplanationUid')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
371
  def getExplanationUid(self):
372
    """
373
      Returns the uid of the explanation of this movement.
374
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
375 376
    return self.getDeliveryUid()

377 378
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getExplanationValue')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
379
  def getExplanationValue(self):
380
    """
381
      Returns the object explanation of this movement.
382
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
383
    return self.getDeliveryValue()
384 385

  # Simulation
Jean-Paul Smets's avatar
Jean-Paul Smets committed
386 387
  security.declareProtected(Permissions.View, 'isSimulated')
  def isSimulated(self):
388 389
    return (len(self.getDeliveryRelatedValueList()) > 0) or\
           (len(self.getOrderRelatedValueList()) > 0)
390

391
  # New Causality API
392 393
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getOrderQuantity')
394 395 396 397 398
  def getOrderQuantity(self):
    """
      Returns the quantity of related order(s)
    """
    return self.getQuantity()
399

400 401
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDeliveryQuantity')
402 403 404 405
  def getDeliveryQuantity(self):
    """
      Returns the quantity of related delivery(s)
    """
406 407
    return self.getQuantity()

408 409
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSimulationQuantity')
410 411 412 413
  def getSimulationQuantity(self):
    """
      Returns the sum of quantities in related simulation movements
    """
414 415
    return self.getQuantity()

416 417
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getOrderStartDateList')
418 419
  def getOrderStartDateList(self):
    """
420
      Returns the list of start date of related order(s)
421 422
    """
    return [self.getStartDate()]
423

424 425
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDeliveryStartDateList')
426 427
  def getDeliveryStartDateList(self):
    """
428
      Returns the list of start date of related delivery(s)
429 430
    """
    return [self.getStartDate()]
431

432 433
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSimulationStartDateList')
434 435
  def getSimulationStartDateList(self):
    """
436
      Returns the list of start date related simulation movements
437 438
    """
    return [self.getStartDate()]
439

440 441
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getOrderStopDateList')
442 443
  def getOrderStopDateList(self):
    """
444
      Returns the list of stop date of related order(s)
445 446
    """
    return [self.getStopDate()]
447

448 449
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDeliveryStopDateList')
450 451
  def getDeliveryStopDateList(self):
    """
452
      Returns the list of stop date of related delivery(s)
453 454
    """
    return [self.getStopDate()]
455

456 457
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSimulationStopDateList')
458 459
  def getSimulationStopDateList(self):
    """
460
      Returns the list of stop date related simulation movements
461 462
    """
    return [self.getStopDate()]
463

464 465
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getOrderSourceList')
466 467 468 469
  def getOrderSourceList(self):
    """
      Returns the source of related orders
    """
470 471
    return self.getSourceList()

472 473
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDeliverySourceList')
474 475 476 477
  def getDeliverySourceList(self):
    """
      Returns the source of related deliveries
    """
478 479
    return self.getSourceList()

480 481
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSimulationSourceList')
482 483 484 485
  def getSimulationSourceList(self):
    """
      Returns the source of related simulation movements
    """
486 487
    return self.getSourceList()

488 489
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getOrderDestinationList')
490 491 492 493
  def getOrderDestinationList(self):
    """
      Returns the destination of related orders
    """
494 495
    return self.getDestinationList()

496 497
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDeliveryDestinationList')
498 499 500 501
  def getDeliveryDestinationList(self):
    """
      Returns the destination of related deliveries
    """
502 503
    return self.getDestinationList()

504 505
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSimulationDestinationList')
506 507 508 509
  def getSimulationDestinationList(self):
    """
      Returns the destination of related simulation movements
    """
510 511
    return self.getDestinationList()

512 513
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getOrderSourceSectionList')
514 515 516 517
  def getOrderSourceSectionList(self):
    """
      Returns the source_section of related orders
    """
518 519
    return self.getSourceSectionList()

520 521
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDeliverySourceSectionList')
522 523 524 525
  def getDeliverySourceSectionList(self):
    """
      Returns the source_section of related deliveries
    """
526 527
    return self.getSourceSectionList()

528 529
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSimulationSourceSectionList')
530 531 532 533
  def getSimulationSourceSectionList(self):
    """
      Returns the source_section of related simulation movements
    """
534 535
    return self.getSourceSectionList()

536 537
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getOrderDestinationSectionList')
538 539 540 541
  def getOrderDestinationSectionList(self):
    """
      Returns the destination_section of related orders
    """
542 543
    return self.getDestinationSectionList()

544 545
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDeliveryDestinationSectionList')
546 547 548 549
  def getDeliveryDestinationSectionList(self):
    """
      Returns the destination_section of related deliveries
    """
550 551
    return self.getDestinationSectionList()

552 553
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSimulationDestinationSectionList')
554 555 556 557
  def getSimulationDestinationSectionList(self):
    """
      Returns the destination_section of related simulation movements
    """
558 559
    return self.getDestinationSectionList()

560
  # Debit and credit methods
561 562
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSourceDebit')
563 564 565 566 567 568 569
  def getSourceDebit(self):
    """
      Return the quantity
    """
    quantity = self.getQuantity()
    try:
      quantity = float(quantity)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
570
    except TypeError:
571 572 573 574 575 576
      quantity = 0.0
    if quantity < 0:
      return - quantity
    else:
      return 0.0

577 578
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSourceCredit')
579 580 581 582 583 584 585
  def getSourceCredit(self):
    """
      Return the quantity
    """
    quantity = self.getQuantity()
    try:
      quantity = float(quantity)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
586
    except TypeError:
587 588 589 590 591 592
      quantity = 0.0
    if quantity < 0:
      return 0.0
    else:
      return quantity

593 594
  security.declareProtected( Permissions.AccessContentsInformation,
                    'getDestinationDebit', 'getDestinationCredit')
595 596 597 598 599 600 601 602 603 604 605 606
  getDestinationDebit = getSourceCredit
  getDestinationCredit = getSourceDebit

  security.declareProtected(Permissions.ModifyPortalContent, 'setSourceDebit')
  def setSourceDebit(self, source_debit):
    """
      Set the quantity
    """
    if source_debit in (None, ''):
      return 0.0
    try:
      source_debit = float(source_debit)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
607
    except TypeError:
608 609 610 611 612 613 614 615 616 617 618 619
      source_debit = 0.0
    self.setQuantity(- source_debit)

  security.declareProtected(Permissions.ModifyPortalContent, 'setSourceCredit')
  def setSourceCredit(self, source_credit):
    """
      Set the quantity
    """
    if source_credit in (None, ''):
      return 0.0
    try:
      source_credit = float(source_credit)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
620
    except TypeError:
621 622 623
      source_credit = 0.0
    self.setQuantity(source_credit)

624 625 626 627
  security.declareProtected( Permissions.ModifyPortalContent,
                             'setDestinationDebit', 'setDestinationCredit' )
  setDestinationDebit = setSourceCredit
  setDestinationCredit = setSourceDebit
628

629
  # Debit and credit methods for asset
630 631
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSourceAssetDebit' )
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
  def getSourceAssetDebit(self):
    """
      Return the debit part of the source total asset price.

      This is the same as getSourceDebit where quantity is replaced
      by source_total_asset_price
    """
    quantity = self.getSourceTotalAssetPrice()
    try:
      quantity = float(quantity)
    except TypeError:
      quantity = 0.0
    if quantity < 0:
      return - quantity
    else:
      return 0.0

649 650
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSourceAssetCredit' )
651 652
  def getSourceAssetCredit(self):
    """
653 654 655 656
      Return the credit part of the source total asset price.

      This is the same as getSourceCredit where quantity is replaced
      by source_total_asset_price
657 658 659 660 661 662 663 664 665 666
    """
    quantity = self.getSourceTotalAssetPrice()
    try:
      quantity = float(quantity)
    except TypeError:
      quantity = 0.0
    if quantity < 0:
      return 0.0
    else:
      return quantity
667 668 669 670 671 672
  
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDestinationAssetDebit' )
  def getDestinationAssetDebit(self):
    """
      Return the debit part of the destination total asset price.
673

674 675 676 677 678 679 680 681 682 683 684 685
      This is the same as getDestinationDebit where quantity is replaced
      by destination_total_asset_price
    """
    quantity = self.getDestinationTotalAssetPrice()
    try:
      quantity = float(quantity)
    except TypeError:
      quantity = 0.0
    if quantity < 0:
      return - quantity
    else:
      return 0.0
686

687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDestinationAssetCredit' )
  def getDestinationAssetCredit(self):
    """
      Return the credit part of the destination total asset price.

      This is the same as getDestinationCredit where quantity is replaced
      by destination_total_asset_price
    """
    quantity = self.getDestinationTotalAssetPrice()
    try:
      quantity = float(quantity)
    except TypeError:
      quantity = 0.0
    if quantity < 0:
      return 0.0
    else:
      return quantity
  
  security.declareProtected( Permissions.ModifyPortalContent,
                             'setSourceAssetDebit' )
708 709 710 711 712 713 714 715 716 717 718 719
  def setSourceAssetDebit(self, source_debit):
    """
      Set the quantity
    """
    if source_debit in (None, ''):
      return 0.0
    try:
      source_debit = float(source_debit)
    except TypeError:
      source_debit = 0.0
    self.setSourceTotalAssetPrice(- source_debit)

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
  security.declareProtected( Permissions.ModifyPortalContent,
                             'setSourceAssetCredit' )
  def setSourceAssetCredit(self, source_credit):
    """
      Set the quantity
    """
    if source_credit in (None, ''):
      return 0.0
    try:
      source_credit = float(source_credit)
    except TypeError:
      source_credit = 0.0
    self.setSourceTotalAssetPrice(source_credit)

  security.declareProtected( Permissions.ModifyPortalContent,
                             'setDestinationAssetDebit' )
  def setDestinationAssetDebit(self, destination_debit):
    """
      Set the quantity
    """
    if destination_debit in (None, ''):
      return 0.0
    try:
      destination_debit = float(destination_debit)
    except TypeError:
      destination_debit = 0.0
    self.setDestinationTotalAssetPrice(destination_debit)

  security.declareProtected( Permissions.ModifyPortalContent,
                             'setDestinationAssetCredit' )
  def setDestinationAssetCredit(self, destination_credit):
    """
      Set the quantity
    """
    if destination_credit in (None, ''):
      return 0.0
    try:
      destination_credit = float(destination_credit)
    except TypeError:
      destination_credit = 0.0
    self.setDestinationTotalAssetPrice(- destination_credit)
761

Jean-Paul Smets's avatar
Jean-Paul Smets committed
762 763 764
  # Item Access (tracking)
  security.declareProtected(Permissions.AccessContentsInformation, 'getTrackedItemUidList')
  def getTrackedItemUidList(self):
765
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
766
      Return a list of uid for related items
767
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
768 769 770
    ### XXX We should filter by portal type here
    return self.getAggregateUidList()

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
  # Helper methods to display total quantities as produced / consumed
  security.declareProtected(Permissions.AccessContentsInformation,
      'getProductionTotalQuantity')
  def getProductionTotalQuantity(self):
    """
      Return the produced quantity
    """
    quantity = self.getTotalQuantity()
    return self.getProductionQuantity(quantity=quantity)

  security.declareProtected(Permissions.AccessContentsInformation,
      'getConsumptionTotalQuantity')
  def getConsumptionTotalQuantity(self):
    """
      Return the produced quantity
    """
    quantity = self.getTotalQuantity()
    return self.getConsumptionQuantity(quantity=quantity)

790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
  security.declareProtected(Permissions.AccessContentsInformation,
      'getSubVariationText')
  def getSubVariationText(self,**kw):
    """
    Provide a string representation of XXX
    """
    base_category_list = self.getPortalSubVariationBaseCategoryList()
    portal_type_list = self.getPortalSubVariationTypeList()
    return_list = []
    for base_category in base_category_list:
      variation_list = self.getAcquiredCategoryMembershipList(base_category,
          portal_type=portal_type_list,base=1)
      return_list.extend(variation_list)
    return "\n".join(return_list)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
805 806 807 808 809
  # SKU vs. CU
#   security.declareProtected(Permissions.AccessContentsInformation, 'getSourceStandardInventoriatedQuantity')
#   def getSourceStandardInventoriatedQuantity(self):
#     """
#       The inventoriated quantity converted in a default unit
810
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
811 812
#       For assortments, returns the inventoriated quantity in terms of number of items
#       in the assortemnt.
813
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
814 815
#       For accounting, returns the quantity converted in a default unit
#     """
816 817
#     return self.getStandardInventoriatedQuantity()

Jean-Paul Smets's avatar
Jean-Paul Smets committed
818 819 820 821
#   security.declareProtected(Permissions.AccessContentsInformation, 'getDestinationStandardInventoriatedQuantity')
#   def getDestinationStandardInventoriatedQuantity(self):
#     """
#       The inventoriated quantity converted in a default unit
822
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
823 824
#       For assortments, returns the inventoriated quantity in terms of number of items
#       in the assortemnt.
825
#
Jean-Paul Smets's avatar
Jean-Paul Smets committed
826 827
#       For accounting, returns the quantity converted in a default unit
#     """
828
#     return self.getStandardInventoriatedQuantity()