SimulatedDeliveryBuilder.py 17.7 KB
Newer Older
1 2 3 4 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
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2005-2008 Nexedi SA and Contributors. All Rights Reserved.
#                    Romain Courteaud <romain@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility 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
# guarantees 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
from zLOG import LOG, BLATHER
31 32
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet
33
from Products.ERP5.mixin.builder import BuilderMixin, SelectMethodError
34
from Products.ERP5Type.TransactionalVariable import getTransactionalVariable
35 36 37
from Products.ERP5Type.UnrestrictedMethod import UnrestrictedMethod
from Products.ERP5Type.CopySupport import CopyError, tryMethodCallWithTemporaryPermission

38

39
class SimulatedDeliveryBuilder(BuilderMixin):
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
  """
    Delivery Builder objects allow to gather multiple Simulation Movements
    into a single Delivery.

    The initial quantity property of the Delivery Line is calculated by
    summing quantities of related Simulation Movements.

    Delivery Builders are called for example whenever an order is confirmed.
    They are also called globaly in order to gather any confirmed or above
    Simulation Movement which was not associated to any Delivery Line.
    Such movements are called orphaned Simulation Movements.

    Delivery Builder objects are provided with a set a parameters to achieve
    their goal:

    A path definition: source, destination, etc. which defines the general
    kind of movements it applies.

    simulation_select_method which defines how to query all Simulation
    Movements which meet certain criteria (including the above path path
    definition).

    collect_order_list which defines how to group selected movements
    according to gathering rules.

    delivery_select_method which defines how to select existing Delivery
    which may eventually be updated with selected simulation movements.

    delivery_module, delivery_type and delivery_line_type which define the
    module and portal types for newly built Deliveries and Delivery Lines.

    Delivery Builders can also be provided with optional parameters to
    restrict selection to a given root Applied Rule caused by a single Order
    or to Simulation Movements related to a limited set of existing
    Deliveries.
  """

  # CMF Type Definition
  meta_type = 'ERP5 Delivery Builder'
  portal_type = 'Delivery Builder'

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

  # Default Properties
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.XMLObject
                    , PropertySheet.CategoryCore
                    , PropertySheet.DublinCore
                    , PropertySheet.Arrow
                    , PropertySheet.Amount
                    , PropertySheet.Comment
                    , PropertySheet.DeliveryBuilder
                    )

  def callBeforeBuildingScript(self):  # XXX-JPS
    """
      Redefine this method, because it seems nothing interesting can be
99
      done before building Delivery.
100 101 102 103 104 105 106 107 108
    """
    pass

  @UnrestrictedMethod
  def searchMovementList(self, applied_rule_uid=None, **kw):
    """
      defines how to query all Simulation Movements which meet certain criteria
      (including the above path path definition).

109
      First, select movement matching to criteria define on Delivery Builder
110 111 112
      Then, call script simulation_select_method to restrict movement_list
    """
    # Search only child movement from this applied rule
113
    if applied_rule_uid:
114 115 116
      kw['parent_uid'] = applied_rule_uid
    # XXX Add profile query
    # Add resource query
117 118 119 120 121 122 123 124 125 126 127 128
    portal_type = self.getResourcePortalType()
    if portal_type:
      kw['resource_portal_type'] = portal_type
    movement_list = []
    for movement in self._searchMovementList(
        portal_type='Simulation Movement', **kw):
      if movement.getDelivery():
        LOG("searchMovementList", BLATHER,
            "ignore already built simulation movement %r"
            % movement.getRelativeUrl())
      elif movement.isBuildable():
        movement_list.append(movement)
129 130 131 132 133 134 135 136 137 138 139 140 141
    # XXX  Add predicate test
    return movement_list

  def _setDeliveryMovementProperties(self, delivery_movement,
                                     simulation_movement, property_dict,
                                     update_existing_movement=0,
                                     force_update=0, activate_kw=None):
    """
      Initialize or update delivery movement properties.
      Set delivery ratio on simulation movement.
      Create the relation between simulation movement
      and delivery movement.
    """
142 143
    delivery = delivery_movement.getExplanationValue()
    simulation_movement.recursiveReindexObject(activate_kw=dict(
144
      activate_kw or (), tag='build:'+delivery.getPath()))
145
    BuilderMixin._setDeliveryMovementProperties(
146 147 148
                            self, delivery_movement,
                            simulation_movement, property_dict,
                            update_existing_movement=update_existing_movement,
149
                            force_update=force_update,
150 151 152 153 154 155 156
                            activate_kw=activate_kw)

    if update_existing_movement and not force_update:
      # Important.
      # Attributes of delivery_movement must not be modified here.
      # Because we can not change values modified by the user.
      # Delivery will probably diverge now, but this is not the job of
157
      # Delivery Builder to resolve such problem.
158
      # Use Solver instead.
159
      simulation_movement._setDeliveryRatio(0)
160
    else:
161 162 163 164 165 166 167 168 169 170 171 172 173 174
      simulation_movement._setDeliveryRatio(1)
    delivery_movement = delivery_movement.getRelativeUrl()
    if simulation_movement.getDeliveryList() != [delivery_movement]:
      simulation_movement._setDelivery(delivery_movement)
      if not simulation_movement.isTempDocument():
        try:
          getCausalityState = delivery.aq_explicit.getCausalityState
        except AttributeError:
          return
        if getCausalityState() == 'building':
          # Make sure no other node is changing state of the delivery
          delivery.serializeCausalityState()
        else:
          delivery.startBuilding()
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

  # Simulation consistency propagation
  security.declareProtected(Permissions.ModifyPortalContent,
                            'updateFromSimulation')
  def updateFromSimulation(self, delivery_relative_url, **kw):
    """
      Update all lines of this transaction based on movements in the
      simulation related to this transaction.
    """
    # We have to get a delivery, else, raise a Error
    delivery = self.getPortalObject().restrictedTraverse(delivery_relative_url)

    divergence_to_adopt_list = delivery.getDivergenceList()
    return self.solveDivergence(
      delivery_relative_url,
      divergence_to_adopt_list=divergence_to_adopt_list)

  @UnrestrictedMethod
  def solveDeliveryGroupDivergence(self, delivery_relative_url,
                                   property_dict=None):
    """
      solve each divergence according to users decision (accept, adopt
      or do nothing).
    """
    if property_dict in (None, {}):
      return
    delivery = self.getPortalObject().restrictedTraverse(delivery_relative_url)
    for (property, value) in property_dict.iteritems():
      delivery.setPropertyList(property, value)

    # Try to remove existing properties/categories from Movements that
    # should exist on Deliveries.
    for movement in delivery.getMovementList():
      for prop in property_dict.keys():
        # XXX The following should be implemented in better way.
        if movement.hasProperty(prop):
          try:
            # for Property
            movement._delProperty(prop)
          except AttributeError:
            # for Category
            movement.setProperty(prop, None)

    divergence_to_accept_list = []
    for divergence in delivery.getDivergenceList():
      if divergence.getProperty('tested_property') not in property_dict.keys():
        continue
      divergence_to_accept_list.append(divergence)
    self._solveDivergence(delivery_relative_url,
                          divergence_to_accept_list=divergence_to_accept_list)

  def _solveDivergence(self, delivery_relative_url, # XXX-JPS what is this doing here ?????
                       divergence_to_accept_list=None,
                       divergence_to_adopt_list=None,
                       **kw):
    """
      solve each divergence according to users decision (accept, adopt
      or do nothing).
    """
    # We have to get a delivery, else, raise a Error
    delivery = self.getPortalObject().restrictedTraverse(delivery_relative_url)

    if divergence_to_accept_list is None:
      divergence_to_accept_list = []
    if divergence_to_adopt_list is None:
      divergence_to_adopt_list = []

    if not len(divergence_to_accept_list) and \
           not len(divergence_to_adopt_list):
      return
    divergence_list = delivery.getDivergenceList()

    # First, we update simulation movements according to
    # divergence_to_accept_list.
    if len(divergence_to_accept_list):
      solver_script = delivery._getTypeBasedMethod('acceptDecision',
                                                   'Delivery_acceptDecision')
      solver_script(divergence_to_accept_list)

    # Then, we update delivery/line/cell from simulation movements
    # according to divergence_to_adopt_list.
    if not len(divergence_to_adopt_list):
      return

    # Select
    movement_type_list = (self.getDeliveryLinePortalType(),
            self.getDeliveryCellPortalType())
    movement_list = delivery.getMovementList(portal_type=movement_type_list)
    simulation_movement_list = []
    for movement in movement_list:
      movement.edit(quantity=0)
      for simulation_movement in movement.getDeliveryRelatedValueList(
                                            portal_type="Simulation Movement"):
        simulation_movement_list.append(simulation_movement)

    # Collect
    root_group_node = self.collectMovement(simulation_movement_list)

    # Build
    portal = self.getPortalObject()
    delivery_module = getattr(portal, self.getDeliveryModule())
    delivery_to_update_list = [delivery]
    self._resetUpdated()
    delivery_list = self._processDeliveryGroup(
      delivery_module,
      root_group_node,
      self.getDeliveryMovementGroupList(),
      delivery_to_update_list=delivery_to_update_list,
      divergence_list=divergence_to_adopt_list,
      force_update=1)

    # Then, we should re-apply quantity divergence according to 'Do
    # nothing' quanity divergence list because all quantity are already
    # calculated in adopt prevision phase.
    quantity_dict = {}
    for divergence in divergence_list:
      if divergence.getProperty('divergence_scope') != 'quantity' or \
             divergence in divergence_to_accept_list or \
             divergence in divergence_to_adopt_list:
        continue
      s_m = divergence.getProperty('simulation_movement')
      delivery_movement = s_m.getDeliveryValue()
      quantity_gap = divergence.getProperty('decision_value') - \
                     divergence.getProperty('prevision_value')
      delivery_movement.setQuantity(delivery_movement.getQuantity() + \
                                    quantity_gap)
      quantity_dict[s_m] = \
          divergence.getProperty('decision_value')

    # Finally, recalculate delivery_ratio
    #
    # Here, created/updated movements are not indexed yet. So we try to
    # gather delivery relations from simulation movements.
    delivery_dict = {}
    for s_m in simulation_movement_list:
      delivery_path = s_m.getDelivery()
      delivery_dict[delivery_path] = \
                                   delivery_dict.get(delivery_path, []) + \
                                   [s_m]

    for s_m_list_per_movement in delivery_dict.values():
      total_quantity = sum([quantity_dict.get(s_m,
                                              s_m.getMappedProperty('quantity')) \
                            for s_m in s_m_list_per_movement])
      if total_quantity != 0.0:
        for s_m in s_m_list_per_movement:
          delivery_ratio = quantity_dict.get(s_m,
                                             s_m.getMappedProperty('quantity')) \
                                             / total_quantity
          s_m.edit(delivery_ratio=delivery_ratio)
      else:
        for s_m in s_m_list_per_movement:
          delivery_ratio = 1.0 / len(s_m_list_per_movement)
          s_m.edit(delivery_ratio=delivery_ratio)

    # Call afterscript if new deliveries are created
    new_delivery_list = [x for x in delivery_list if x != delivery]
    self.callAfterBuildingScript(new_delivery_list, simulation_movement_list)

    return delivery_list

  solveDivergence = UnrestrictedMethod(_solveDivergence)

338 339
  def _createDelivery(self, delivery_module, movement_list, activate_kw,
                            temp_object):
340
    """
341 342
      Refer to the docstring in GeneratedDeliveryBuilder.
      Unlike GeneratedDeliveryBuilder, SimulatedDeliveryBuilder needs to respect
343 344 345 346 347 348 349 350 351 352
      existing relationship.
    """
    try:
      old_delivery = self._searchUpByPortalType(
        movement_list[0].getDeliveryValue(),
        self.getDeliveryPortalType())
    except AttributeError:
      old_delivery = None
    if old_delivery is None:
      # from scratch
Julien Muchembled's avatar
Julien Muchembled committed
353
      delivery = super(SimulatedDeliveryBuilder, self)._createDelivery(
354
        delivery_module, movement_list, activate_kw, temp_object)
355 356 357 358
      # Interactions will usually trigger reindexing of related SM when
      # simulation state changes. Disable them for this transaction
      # because we already do this in _setDeliveryMovementProperties
      delivery.updateSimulation(index_related=0)
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    else:
      # from duplicated original delivery
      cp = tryMethodCallWithTemporaryPermission(
        delivery_module, 'Copy or Move',
        lambda parent, *ids:
        parent._duplicate(parent.manage_copyObjects(ids=ids))[0],
        (delivery_module, old_delivery.getId()), {}, CopyError)
      delivery = delivery_module[cp['new_id']]
      # delete non-split movements
      keep_id_list = [y.getDeliveryValue().getId() for y in movement_list]
      delete_id_list = [x.getId() for x in delivery.contentValues() \
                       if x.getId() not in keep_id_list]
      delivery.deleteContent(delete_id_list)

    return delivery

375 376
  def _createDeliveryLine(self, delivery, movement_list, activate_kw,
                                temp_object):
377
    """
378 379
      Refer to the docstring in GeneratedDeliveryBuilder.
      Unlike GeneratedDeliveryBuilder, SimulatedDeliveryBuilder needs to respect
380 381 382 383 384 385 386 387 388 389 390 391 392
      existing relationship.
    """
    try:
      old_delivery_line = self._searchUpByPortalType(
        movement_list[0].getDeliveryValue(),
        self.getDeliveryLinePortalType())
    except AttributeError:
      old_delivery_line = None
    if old_delivery_line is None:
      # from scratch
      delivery_line = delivery.newContent(
        portal_type=self.getDeliveryLinePortalType(),
        variation_category_list=[],
393
        temp_object=temp_object,
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
        activate_kw=activate_kw)
    else:
      # from duplicated original line
      cp = tryMethodCallWithTemporaryPermission(
        delivery, 'Copy or Move',
        lambda parent, *ids:
        parent._duplicate(parent.manage_copyObjects(ids=ids))[0],
        (delivery, old_delivery_line.getId()), {}, CopyError)
      delivery_line = delivery[cp['new_id']]
      # reset variation category list
      delivery_line.setVariationCategoryList([])
      # delete non-split movements
      keep_id_list = [y.getDeliveryValue().getId() for y in movement_list]
      delete_id_list = [x.getId() for x in delivery_line.contentValues() \
                       if x.getId() not in keep_id_list]
      delivery_line.deleteContent(delete_id_list)

    return delivery_line

413
  def _createDeliveryCell(self, delivery_line, movement, activate_kw,
414
                          base_id, cell_key, temp_object):
415
    """
416 417
      Refer to the docstring in GeneratedDeliveryBuilder.
      Unlike GeneratedDeliveryBuilder, SimulatedDeliveryBuilder needs to respect
418 419 420 421 422 423 424 425 426 427
      existing relationship.
    """
    try:
      old_cell = movement.getDeliveryValue()
    except AttributeError:
      old_cell = None
    if old_cell is None:
      # from scratch
      cell = delivery_line.newCell(base_id=base_id, \
               portal_type=self.getDeliveryCellPortalType(),
428
               activate_kw=activate_kw,temp_object=temp_object, *cell_key)
429 430 431 432 433 434 435 436 437 438
    else:
      # from duplicated original line
      cp = tryMethodCallWithTemporaryPermission(
        delivery_line, 'Copy or Move',
        lambda parent, *ids:
        parent._duplicate(parent.manage_copyObjects(ids=ids))[0],
        (delivery_line, old_cell.getId()), {}, CopyError)
      cell = delivery_line[cp['new_id']]

    return cell