SimulationMovement.py 23.3 KB
Newer Older
1
# -*- coding: utf-8 -*-
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2 3 4
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
5
#                    Jean-Paul Smets-Solanes <jp@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 33
from AccessControl import ClassSecurityInfo
from Products.CMFCore.utils import getToolByName

34
from Products.ERP5Type import Permissions, PropertySheet, interfaces
35
from Products.ERP5Type.TransactionalVariable import getTransactionalVariable
Jean-Paul Smets's avatar
Jean-Paul Smets committed
36

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

39
from zLOG import LOG, WARNING
Jean-Paul Smets's avatar
Jean-Paul Smets committed
40

41 42
from Acquisition import aq_base

43
from Products.ERP5.Document.AppliedRule import TREE_DELIVERED_CACHE_KEY, TREE_DELIVERED_CACHE_ENABLED
44
from Products.ERP5.mixin.property_recordable import PropertyRecordableMixin
45

Jean-Paul Smets's avatar
Jean-Paul Smets committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
# XXX Do we need to create groups ? (ie. confirm group include confirmed, getting_ready and ready

parent_to_movement_simulation_state = {
  'cancelled'        : 'cancelled',
  'draft'            : 'draft',
  'auto_planned'     : 'auto_planned',
  'planned'          : 'planned',
  'ordered'          : 'planned',
  'confirmed'        : 'planned',
  'getting_ready'    : 'planned',
  'ready'            : 'planned',
  'started'          : 'planned',
  'stopped'          : 'planned',
  'delivered'        : 'planned',
  'invoiced'         : 'planned',
}

63
class SimulationMovement(Movement, PropertyRecordableMixin):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
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
  """
      Simulation movements belong to a simulation workflow which includes
      the following steps

      - planned

      - ordered

      - confirmed (the movement is now confirmed in qty or date)

      - started (the movement has started)

      - stopped (the movement is now finished)

      - delivered (the movement is now archived in a delivery)

      The simulation worklow uses some variables, which are
      set by the template

      - is_order_required

      - is_delivery_required


      XX
      - is_problem_checking_required ?

      Other flag
      (forzen flag)

      NEW: we do not use DCWorklow so that the simulation process
      can be as much as possible independent of a Zope / CMF implementation.
  """
  meta_type = 'ERP5 Simulation Movement'
  portal_type = 'Simulation Movement'

  # Declarative security
  security = ClassSecurityInfo()
102
  security.declareObjectProtected(Permissions.AccessContentsInformation)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
103 104 105 106 107 108 109 110 111 112

  # Declarative properties
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.SimpleItem
                    , PropertySheet.CategoryCore
                    , PropertySheet.Amount
                    , PropertySheet.Task
                    , PropertySheet.Arrow
                    , PropertySheet.Movement
                    , PropertySheet.Simulation
113 114 115
                    # Need industrial_phase
                    , PropertySheet.TransformedResource
                    , PropertySheet.AppliedRule
116
                    , PropertySheet.ItemAggregation
117
                    , PropertySheet.Reference
Jean-Paul Smets's avatar
Jean-Paul Smets committed
118
                    )
119

120 121 122
  # Declarative interfaces
  zope.interface.implements(interfaces.IPropertyRecordable, )

123 124 125
  def tpValues(self) :
    """ show the content in the left pane of the ZMI """
    return self.objectValues()
126

Jean-Paul Smets's avatar
Jean-Paul Smets committed
127
  # Price should be acquired
128 129
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getPrice')
130
  def getPrice(self, default=None, context=None, REQUEST=None, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
131 132
    """
    """
133
    return self._baseGetPrice(default) # Call the price method
Jean-Paul Smets's avatar
Jean-Paul Smets committed
134

135 136
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getCausalityState')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
137 138 139 140
  def getCausalityState(self):
    """
      Returns the current state in causality
    """
141
    return getattr(aq_base(self), 'causality_state', 'solved')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
142

143 144
  security.declareProtected( Permissions.ModifyPortalContent,
                             'setCausalityState')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
145 146 147 148 149 150
  def setCausalityState(self, value):
    """
      Change causality state
    """
    self.causality_state = value

151 152
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getSimulationState')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
153 154 155 156
  def getSimulationState(self, id_only=1):
    """
      Returns the current state in simulation

157 158
      Inherit from order or delivery or parent (but use a conversion
      table to make orders planned when parent is confirmed)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
159 160 161 162 163 164

      XXX: movements in zero stock rule can not acquire simulation state
    """
    delivery = self.getDeliveryValue()
    if delivery is not None:
      return delivery.getSimulationState()
165
    # 'order' category is deprecated. it is kept for compatibility.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
166 167 168 169
    order = self.getOrderValue()
    if order is not None:
      return order.getSimulationState()
    try:
170
      parent_state = self.getParentValue().getSimulationState()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
171
      return parent_to_movement_simulation_state[parent_state]
172
    except (KeyError, AttributeError):
173 174 175
      LOG('SimulationMovement.getSimulationState', WARNING,
          'Could not acquire simulation state from %s'
          % self.getRelativeUrl())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
176 177
      return None

178 179 180 181 182 183 184 185 186
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getTranslatedSimulationStateTitle')
  def getTranslatedSimulationStateTitle(self):
    """Returns translated simulation state title, for user interface, such as
    stock browser.
    """
    delivery = self.getDeliveryValue()
    if delivery is not None:
      return delivery.getTranslatedSimulationStateTitle()
187
    # 'order' category is deprecated. it is kept for compatibility.
188 189 190 191 192 193 194 195 196
    order = self.getOrderValue()
    if order is not None:
      return order.getTranslatedSimulationStateTitle()
    # The simulation_state of a simulation movement is calculated by a
    # mapping, there's no reliable way of getting the translated title from a
    # simulation state ID, so we just return the state ID because we got
    # nothing better to return.
    return self.getSimulationState()

197 198 199 200 201 202 203 204
  security.declareProtected(Permissions.AccessContentsInformation,
                            'isCompleted')
  def isCompleted(self):
    """Zope publisher docstring. Documentation in ISimulationMovement"""
    # only available in BPM, so fail totally in case of working without BPM
    return self.getSimulationState() in self.getCausalityValue(
        portal_type='Business Path').getCompletedStateList()

205 206
  security.declareProtected( Permissions.AccessContentsInformation,
                            'isAccountable')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
207 208 209
  def isAccountable(self):
    """
      Returns 1 if this needs to be accounted
210 211 212
      Some Simulation movement corresponds to non accountable movements,
      the parent applied rule decide wether this movement is accountable
      or not.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
213
    """
214
    return self.getParentValue().isAccountable(self)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
215 216 217 218 219 220


  #######################################################
  # Causality Workflow Methods

  security.declareProtected(Permissions.ModifyPortalContent, 'expand')
221
  def expand(self, force=0, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
222
    """
223 224 225 226 227 228 229 230 231
    Checks all existing applied rules and make sure they still apply.
    Checks for other possible rules and starts expansion process (instanciates
    applied rules and calls expand on them).

    First get all applicable rules,
    then, delete all applied rules that no longer match and are not linked to
    a delivery,
    finally, apply new rules if no rule with the same type is already applied.
    """
232
    portal_rules = getToolByName(self.getPortalObject(), 'portal_rules')
233

234
    tv = getTransactionalVariable()
235 236 237 238 239 240 241 242 243 244 245
    cache = tv.setdefault(TREE_DELIVERED_CACHE_KEY, {})
    cache_enabled = cache.get(TREE_DELIVERED_CACHE_ENABLED, 0)

    # enable cache
    if not cache_enabled:
      cache[TREE_DELIVERED_CACHE_ENABLED] = 1

    applied_rule_dict = {}
    applicable_rule_dict = {}
    for rule in portal_rules.searchRuleList(self, sort_on='version',
        sort_order='descending'):
246 247
      reference = rule.getReference()
      if reference:
248
        applicable_rule_dict.setdefault(reference, rule)
249

250
    for applied_rule in list(self.objectValues()):
251
      rule = applied_rule.getSpecialiseValue()
252
      if rule.test(self) or applied_rule._isTreeDelivered():
253
        applied_rule_dict[rule.getReference()] = applied_rule
254
      else:
255
        self._delObject(applied_rule.getId())
256

257
    for reference, rule in applicable_rule_dict.iteritems():
258
      if reference not in applied_rule_dict:
259
        applied_rule = rule.constructNewAppliedRule(self, **kw)
260
        applied_rule_dict[reference] = applied_rule
261 262 263 264 265 266 267 268 269 270 271 272

    self.setCausalityState('expanded')
    # expand
    for applied_rule in applied_rule_dict.itervalues():
      applied_rule.expand(force=force, **kw)

    # disable and clear cache
    if not cache_enabled:
      try:
        del tv[TREE_DELIVERED_CACHE_KEY]
      except KeyError:
        pass
Jean-Paul Smets's avatar
Jean-Paul Smets committed
273 274 275 276 277 278 279 280 281 282

  security.declareProtected(Permissions.ModifyPortalContent, 'diverge')
  def diverge(self):
    """
       -> new status -> diverged

       Movements which diverge can not be expanded
    """
    self.setCausalityState('diverged')

283 284 285 286 287
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getExplanationValue')
  def getExplanationValue(self):
    """Returns the delivery if any or the order related to the root
    applied rule if any.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
288
    """
289 290
    delivery_value = self.getDeliveryValue()
    if delivery_value is None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
291 292 293
      ra = self.getRootAppliedRule()
      order = ra.getCausalityValue()
      if order is not None:
294
        return order
Jean-Paul Smets's avatar
Jean-Paul Smets committed
295 296
      else:
        # Ex. zero stock rule
297
        return ra
Jean-Paul Smets's avatar
Jean-Paul Smets committed
298
    else:
299 300
      explanation_value = delivery_value
      portal = self.getPortalObject()
301 302
      delivery_type_list = self.getPortalDeliveryTypeList() \
              + self.getPortalOrderTypeList()
303 304
      while explanation_value.getPortalType() not in delivery_type_list and \
          explanation_value != portal:
305
            explanation_value = explanation_value.getParentValue()
306
      if explanation_value != portal:
307
        return explanation_value
308

309 310 311 312 313 314 315 316 317 318 319 320 321 322
  def asComposedDocument(self, *args, **kw):
    # XXX: What delivery should be used to find amount generator lines ?
    #      With the currently enabled code, entire branches in the simulation
    #      tree get (temporary) deleted when new delivery lines are being built
    #      (and don't have yet a specialise value).
    #      With the commented code, changing the STC on a SIT generated from a
    #      SPL/SO would have no impact (and would never make the SIT divergent).
    #return self.getRootSimulationMovement() \
    #           .getDeliveryValue() \
    #           .asComposedDocument(*args, **kw)
    while 1:
      delivery_value = self.getDeliveryValue()
      if delivery_value is not None:
        return delivery_value.asComposedDocument(*args, **kw)
323 324 325 326 327
      # below code is for compatibility with old rules
      grand_parent = self.getParentValue().getParentValue()
      if grand_parent.getPortalType() == 'Simulation Tool':
        return self.getOrderValue().asComposedDocument(*args, **kw)
      self = grand_parent
328

Jean-Paul Smets's avatar
Jean-Paul Smets committed
329
  # Deliverability / orderability
330 331
  security.declareProtected( Permissions.AccessContentsInformation,
                             'isOrderable')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
332
  def isOrderable(self):
333 334
    # the value of this method is no longer used.
    return True
Jean-Paul Smets's avatar
Jean-Paul Smets committed
335

336 337
  getOrderable = isOrderable

338 339
  security.declareProtected( Permissions.AccessContentsInformation,
                             'isDeliverable')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
340
  def isDeliverable(self):
341 342
    # the value of this method is no longer used.
    return True
Jean-Paul Smets's avatar
Jean-Paul Smets committed
343

344
  getDeliverable = isDeliverable
345

Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
346 347 348
  security.declareProtected( Permissions.AccessContentsInformation,
                             'isDeletable')
  def isDeletable(self):
Nicolas Dumazet's avatar
Nicolas Dumazet committed
349
    return not self.isFrozen() and not self._isTreeDelivered()
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
350

351
  # Simulation Dates - acquire target dates
352 353
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getOrderStartDate')
354
  def getOrderStartDate(self):
355
    # 'order' category is deprecated. it is kept for compatibility.
356 357 358
    order_value = self.getOrderValue()
    if order_value is not None:
      return order_value.getStartDate()
359 360 361
    delivery_value = self.getDeliveryValue()
    if delivery_value is not None:
      return delivery_value.getStartDate()
362

363 364
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getOrderStopDate')
365
  def getOrderStopDate(self):
366
    # 'order' category is deprecated. it is kept for compatibility.
367 368 369
    order_value = self.getOrderValue()
    if order_value is not None:
      return order_value.getStopDate()
370 371 372
    delivery_value = self.getDeliveryValue()
    if delivery_value is not None:
      return delivery_value.getStopDate()
Romain Courteaud's avatar
Romain Courteaud committed
373

374 375
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDeliveryStartDateList')
Romain Courteaud's avatar
Romain Courteaud committed
376 377
  def getDeliveryStartDateList(self):
    """
378
      Returns the stop date of related delivery
Romain Courteaud's avatar
Romain Courteaud committed
379 380 381 382 383 384
    """
    start_date_list = []
    delivery_movement = self.getDeliveryValue()
    if delivery_movement is not None:
      start_date_list.append(delivery_movement.getStartDate())
    return start_date_list
385

386 387
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDeliveryStopDateList')
Romain Courteaud's avatar
Romain Courteaud committed
388 389
  def getDeliveryStopDateList(self):
    """
390
      Returns the stop date of related delivery
Romain Courteaud's avatar
Romain Courteaud committed
391 392 393 394 395 396
    """
    stop_date_list = []
    delivery_movement = self.getDeliveryValue()
    if delivery_movement is not None:
      stop_date_list.append(delivery_movement.getStopDate())
    return stop_date_list
397

398 399
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getDeliveryQuantity')
Romain Courteaud's avatar
Romain Courteaud committed
400 401
  def getDeliveryQuantity(self):
    """
402
      Returns the quantity of related delivery
Romain Courteaud's avatar
Romain Courteaud committed
403
    """
404
    quantity = 0.0
Romain Courteaud's avatar
Romain Courteaud committed
405 406 407 408
    delivery_movement = self.getDeliveryValue()
    if delivery_movement is not None:
      quantity = delivery_movement.getQuantity()
    return quantity
409

410 411
  security.declareProtected( Permissions.AccessContentsInformation,
                             'isConvergent')
412 413
  def isConvergent(self):
    """
414
      Returns true if the Simulation Movement is convergent with the
415
      the delivery value
416 417 418
    """
    return not self.isDivergent()

419
  security.declareProtected( Permissions.AccessContentsInformation,
420
      'isDivergent')
421 422
  def isDivergent(self):
    """
423
      Returns true if the Simulation Movement is divergent from the
424
      the delivery value
425
    """
426 427 428 429 430 431 432 433 434 435
    return self.getParentValue().isDivergent(self)

  security.declareProtected( Permissions.AccessContentsInformation,
      'getDivergenceList')
  def getDivergenceList(self):
    """
    Returns detailed information about the divergence
    """
    return self.getParentValue().getDivergenceList(self)

436 437
  security.declareProtected( Permissions.ModifyPortalContent,
                             'setDefaultDeliveryProperties')
438 439
  def setDefaultDeliveryProperties(self):
    """
440 441
    Sets the delivery_ratio and delivery_error properties to the
    calculated value
442 443 444 445 446
    """
    delivery = self.getDeliveryValue()
    if delivery is not None:
      delivery.updateSimulationDeliveryProperties(movement_list = [self])

447 448
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getCorrectedQuantity')
449 450
  def getCorrectedQuantity(self):
    """
451 452
    Returns the quantity property deducted by the possible profit_quantity and
    taking into account delivery error
453 454 455 456 457

    NOTE: XXX-JPS This method should not use profit_quantity. Profit and loss
          quantities are now only handled through explicit movements.
          Look are invocations of _isProfitAndLossMovement in
          ERP5.mixin.rule to understand how.
458
    """
459
    quantity = self.getQuantity()
460 461 462
    profit_quantity = self.getProfitQuantity() or 0
    delivery_error = self.getDeliveryError() or 0
    return quantity - profit_quantity + delivery_error
463

464 465
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getRootSimulationMovement')
466 467 468
  def getRootSimulationMovement(self):
    """
      Return the root simulation movement in the simulation tree.
469
      FIXME : this method should be called getRootSimulationMovementValue
470
    """
471
    parent_applied_rule = self.getParentValue()
472 473 474 475 476
    if parent_applied_rule.getRootAppliedRule() == parent_applied_rule:
      return self
    else:
      return parent_applied_rule.getRootSimulationMovement()

477 478
  security.declareProtected( Permissions.AccessContentsInformation,
                             'getRootSimulationMovementUid')
479 480 481 482 483 484 485 486 487
  def getRootSimulationMovementUid(self):
    """
      Return the uid of the root simulation movement in the simulation tree.
    """
    root_simulation_movement = self.getRootSimulationMovement()
    if root_simulation_movement is not None:
      return root_simulation_movement.getUid()
    return None

488
  security.declareProtected( Permissions.AccessContentsInformation,
489
                             'getRootCausalityValueList')
490 491 492 493 494 495 496 497
  def getRootCausalityValueList(self):
    """
      Returns the initial causality value for this movement.
      This method will look at the causality and check if the
      causality has already a causality
    """
    root_rule = self.getRootAppliedRule()
    return root_rule.getCausalityValueList()
498

499
  # XXX FIXME Use a interaction workflow instead
500
  # XXX This behavior is now done by simulation_movement_interaction_workflow
501
  # The call to activate() must be done after actual call to
502
  # setDelivery() on the movement,
503
  # but activate() must be called on the previous delivery...
504 505 506 507 508 509 510 511
  #def _setDelivery(self, value):
  #  LOG('setDelivery before', 0, '')
  #  delivery_value = self.getDeliveryValue()
  #  Movement.setDelivery(value)
  #  LOG('setDelivery', 0, '')
  #  if delivery_value is not None:
  #    LOG('delivery_value = ', 0, repr(delivery_value))
  #    activity = delivery_value.activate(
512
  #                activity='SQLQueue',
513
  #                after_path_and_method_id=(
514 515
  #                                        self.getPath(),
  #                                        ['immediateReindexObject',
516 517
  #                                         'recursiveImmediateReindexObject']))
  #    activity.edit()
518

519 520 521 522 523 524 525 526
  def _isTreeDelivered(self, ignore_first=0):
    """
    checks if subapplied rules  of this movement (going down the complete
    simulation tree) have a child with a delivery relation.
    Returns True if at least one is delivered, False if none of them are.

    see AppliedRule._isTreeDelivered
    """
527
    tv = getTransactionalVariable()
528 529 530 531
    cache = tv.setdefault(TREE_DELIVERED_CACHE_KEY, {})
    cache_enabled = cache.get(TREE_DELIVERED_CACHE_ENABLED, 0)

    def getTreeDelivered(movement, ignore_first=0):
532
      if not ignore_first:
533 534 535 536 537 538 539 540 541 542 543 544 545 546
        if len(movement.getDeliveryList()) > 0:
          return True
      for applied_rule in movement.objectValues():
        if applied_rule._isTreeDelivered():
          return True
      return False

    if ignore_first:
      rule_key = (self.getRelativeUrl(), 1)
    else:
      rule_key = self.getRelativeUrl()
    if cache_enabled:
      try:
        return cache[rule_key]
547
      except KeyError:
548 549 550 551 552 553
        result = getTreeDelivered(self, ignore_first=ignore_first)
        cache[rule_key] = result
        return result
    else:
      return getTreeDelivered(self, ignore_first=ignore_first)

554 555 556 557 558 559 560
  security.declareProtected(Permissions.AccessContentsInformation,
                            'isBuildable')
  def isBuildable(self):
    """Simulation Movement buildable logic"""
    if self.getDeliveryValue() is not None:
      # already delivered
      return False
561

562
    # might be buildable - business path dependent
563 564
    business_path = self.getCausalityValue(portal_type='Business Path')
    explanation_value = self.getExplanationValue()
565 566
    if business_path is None or explanation_value is None:
      return True
567

568
    return len(business_path.filterBuildableMovementList([self])) == 1
569

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
  def getSolverProcessValueList(self, movement=None, validation_state=None):
    """
    Returns the list of solver processes which are
    are in a given state and which apply to delivery_or_movement.
    This method is useful to find applicable solver processes
    for a delivery.

    movement -- not applicable

    validation_state -- a state of a list of states
                        to filter the result
    """
    raise NotImplementedError

  def getSolverDecisionValueList(self, movement=None, validation_state=None):
    """
    Returns the list of solver decisions which apply
    to a given movement.

    movement -- not applicable

    validation_state -- a state of a list of states
                        to filter the result
    """
    raise NotImplementedError

  def getSolvedPropertyApplicationValueList(self, movement=None, divergence_tester=None):
    """
    Returns the list of documents at which a given divergence resolution
    can be resolved at. For example, in most cases, date divergences can
    only be resolved at delivery level whereas quantities are usually
    resolved at cell level.

    The result of this method is a list of ERP5 documents.

    movement -- not applicable
    """
    raise NotImplementedError
608 609 610 611

  security.declareProtected(Permissions.AccessContentsInformation,
                            'getMappedProperty')
  def getMappedProperty(self, property):
612
    mapping = self.getPropertyMappingValue()
613
    if mapping is not None:
614
      # Special case: corrected quantity is difficult to handle,
Yoshinori Okuji's avatar
Yoshinori Okuji committed
615
      # because, if quantity is inverse in the mapping, other
616
      # parameters, profit quantity (deprecated) and delivery error,
Yoshinori Okuji's avatar
Yoshinori Okuji committed
617
      # must be inverse as well.
618 619 620 621 622 623 624 625 626 627 628
      if property == 'corrected_quantity':
        mapped_quantity_id = mapping.getMappedPropertyId('quantity')
        quantity = mapping.getMappedProperty(self, 'quantity')
        profit_quantity = self.getProfitQuantity() or 0
        delivery_error = self.getDeliveryError() or 0
        if mapped_quantity_id[:1] == '-':
          # XXX what about if "quantity | -something_different" is
          # specified?
          return quantity + profit_quantity - delivery_error
        else:
          return quantity - profit_quantity + delivery_error
629 630 631
      return mapping.getMappedProperty(self, property)
    else:
      return self.getProperty(property)
632 633 634 635 636 637 638 639 640

  security.declareProtected(Permissions.ModifyPortalContent,
                            'setMappedProperty')
  def setMappedProperty(self, property, value):
    mapping = self.getPropertyMappingValue()
    if mapping is not None:
      return mapping.setMappedProperty(self, property, value)
    else:
      return self.setProperty(property, value)