Delivery.py 22.6 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2
##############################################################################
#
Romain Courteaud's avatar
Romain Courteaud committed
3
# Copyright (c) 2002, 2005 Nexedi SARL and Contributors. All Rights Reserved.
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
Romain Courteaud's avatar
Romain Courteaud committed
5
#                    Romain Courteaud <romain@nexedi.com>
Jean-Paul Smets's avatar
Jean-Paul Smets committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#
# 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, PersistentMapping
from Products.CMFCore.utils import getToolByName
from Products.CMFCore.WorkflowCore import WorkflowMethod
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5Type.XMLObject import XMLObject
36
from Products.ERP5Type.Base import Base
Jean-Paul Smets's avatar
Jean-Paul Smets committed
37 38 39
from Products.ERP5.Document.DeliveryCell import DeliveryCell
from Acquisition import Explicit, Implicit
from Products.PythonScripts.Utility import allow_class
40
from DateTime import DateTime
Jean-Paul Smets's avatar
Jean-Paul Smets committed
41 42 43 44

from zLOG import LOG

class Delivery(XMLObject):
45 46 47 48
    """
        Each time delivery is modified, it MUST launch a reindexing of
        inventories which are related to the resources contained in the Delivery
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
49 50 51 52 53
    # CMF Type Definition
    meta_type = 'ERP5 Delivery'
    portal_type = 'Delivery'
    isPortalContent = 1
    isRADContent = 1
54
    isDelivery = 1
Jean-Paul Smets's avatar
Jean-Paul Smets committed
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

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

    # Default Properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      , PropertySheet.Task
                      , PropertySheet.Arrow
                      , PropertySheet.Movement
                      , PropertySheet.Delivery
                      , PropertySheet.Reference
                      )

    security.declareProtected(Permissions.AccessContentsInformation, 'isAccountable')
    def isAccountable(self):
      """
        Returns 1 if this needs to be accounted
        Only account movements which are not associated to a delivery
        Whenever delivery is there, delivery has priority
      """
      return 1

    # Pricing methods
    def _getTotalPrice(self, context):
      return 2.0

    def _getDefaultTotalPrice(self, context):
      return 3.0

    def _getSourceTotalPrice(self, context):
      return 4.0

    def _getDestinationTotalPrice(self, context):
      return 5.0

Yoshinori Okuji's avatar
Yoshinori Okuji committed
94
    security.declareProtected(Permissions.AccessContentsInformation, 'getDefaultTotalPrice')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    def getDefaultTotalPrice(self, context=None, REQUEST=None, **kw):
      """
      """
      return self._getDefaultTotalPrice(self.asContext(context=context, REQUEST=REQUEST, **kw))

    security.declareProtected(Permissions.AccessContentsInformation, 'getSourceTotalPrice')
    def getSourceTotalPrice(self, context=None, REQUEST=None, **kw):
      """
      """
      return self._getSourceTotalPrice(self.asContext(context=context, REQUEST=REQUEST, **kw))

    security.declareProtected(Permissions.AccessContentsInformation, 'getDestinationTotalPrice')
    def getDestinationTotalPrice(self, context=None, REQUEST=None, **kw):
      """
      """
      return self._getDestinationTotalPrice(self.asContext(context=context, REQUEST=REQUEST, **kw))

    # Pricing
Jean-Paul Smets's avatar
Jean-Paul Smets committed
113 114 115 116 117 118
    security.declareProtected( Permissions.ModifyPortalContent, 'updatePrice' )
    def updatePrice(self):
      for c in self.objectValues():
        if hasattr(aq_base(c), 'updatePrice'):
          c.updatePrice()

119 120 121 122 123 124 125
    security.declareProtected( Permissions.AccessContentsInformation,
                               'getTotalPrice')
    def getTotalPrice(self, fast=1, src__=0, **kw):
      """ Returns the total price for this order
        if the `fast` argument is set to a true value, then it use
        SQLCatalog to compute the price, otherwise it sums the total
        price of objects one by one.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
126
      """
127 128 129 130 131
      if not fast :
        kw.setdefault( 'portal_type',
                       self.getPortalDeliveryMovementTypeList())
        return sum([ line.getTotalPrice(fast=0) for line in
                        self.objectValues(**kw) ])
132
      kw['explanation_uid'] = self.getUid()
133 134
      kw.update(self.portal_catalog.buildSQLQuery(**kw))
      if src__:
135 136
        return self.Delivery_zGetTotal(src__=1, **kw)
      aggregate = self.Delivery_zGetTotal(**kw)[0]
137
      return aggregate.total_price or 0
Jean-Paul Smets's avatar
Jean-Paul Smets committed
138 139

    security.declareProtected(Permissions.AccessContentsInformation, 'getTotalQuantity')
140 141 142 143 144
    def getTotalQuantity(self, fast=1, src__=0, **kw):
      """ Returns the total quantity of this order.
        if the `fast` argument is set to a true value, then it use
        SQLCatalog to compute the quantity, otherwise it sums the total
        quantity of objects one by one.
145
      """
146 147 148 149 150
      if not fast :
        kw.setdefault( 'portal_type',
                       self.getPortalDeliveryMovementTypeList())
        return sum([ line.getTotalQuantity(fast=0) for line in
                        self.objectValues(**kw) ])
151
      kw['explanation_uid'] = self.getUid()
152 153
      kw.update(self.portal_catalog.buildSQLQuery(**kw))
      if src__:
154 155
        return self.Delivery_zGetTotal(src__=1, **kw)
      aggregate = self.Delivery_zGetTotal(**kw)[0]
156
      return aggregate.total_quantity or 0
Jean-Paul Smets's avatar
Jean-Paul Smets committed
157 158 159 160 161 162 163

    security.declareProtected(Permissions.AccessContentsInformation, 'getDeliveryUid')
    def getDeliveryUid(self):
      return self.getUid()

    security.declareProtected(Permissions.AccessContentsInformation, 'getDeliveryValue')
    def getDeliveryValue(self):
164 165 166 167 168 169 170 171 172 173 174
      """
      Deprecated, we should use getRootDeliveryValue instead
      """
      return self

    security.declareProtected(Permissions.AccessContentsInformation, 'getRootDeliveryValue')
    def getRootDeliveryValue(self):
      """
      This method returns the delivery, it is usefull to retrieve the delivery
      from a line or a cell
      """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
175 176
      return self

177 178 179 180
    security.declareProtected(Permissions.AccessContentsInformation, 'getDelivery')
    def getDelivery(self):
      return self.getRelativeUrl()

181
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
182 183
                             'getMovementList')
    def getMovementList(self, portal_type=None, **kw):
184 185 186
      """
        Return a list of movements.
      """
187 188
      if portal_type is None:
        portal_type = self.getPortalMovementTypeList()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
189
      movement_list = []
190
      for m in self.contentValues(filter={'portal_type': portal_type}):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
191
        if m.hasCellContent():
192
          for c in m.contentValues(filter={'portal_type': portal_type}):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
193 194 195 196 197
            movement_list.append(c)
        else:
          movement_list.append(m)
      return movement_list

198 199 200 201 202 203
    security.declareProtected(Permissions.AccessContentsInformation, 'getSimulatedMovementList')
    def getSimulatedMovementList(self):
      """
        Return a list of simulated movements.
        This does not contain Container Line or Container Cell.
      """
204
      return self.getMovementList(portal_type=self.getPortalSimulatedMovementTypeList())
205 206 207 208 209 210 211

    security.declareProtected(Permissions.AccessContentsInformation, 'getInvoiceMovementList')
    def getInvoiceMovementList(self):
      """
        Return a list of simulated movements.
        This does not contain Container Line or Container Cell.
      """
212
      return self.getMovementList(portal_type=self.getPortalInvoiceMovementTypeList())
213 214 215 216 217 218 219 220

    security.declareProtected(Permissions.AccessContentsInformation, 'getContainerList')
    def getContainerList(self):
      """
        Return a list of root containers.
        This does not contain sub-containers.
      """
      container_list = []
221
      for m in self.contentValues(filter={'portal_type': self.getPortalContainerTypeList()}):
222 223 224
        container_list.append(m)
      return container_list

225 226
    def applyToDeliveryRelatedMovement(self, portal_type='Simulation Movement',
        method_id = 'expand',**kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
227 228 229 230
      for my_simulation_movement in self.getDeliveryRelatedValueList(
                                                portal_type = 'Simulation Movement'):
          # And apply
          getattr(my_simulation_movement.getObject(), method_id)()
231
      for m in self.contentValues(filter={'portal_type': self.getPortalMovementTypeList()}):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
232 233 234 235
        # Find related in simulation
        for my_simulation_movement in m.getDeliveryRelatedValueList(
                                                portal_type = 'Simulation Movement'):
          # And apply
236
          getattr(my_simulation_movement.getObject(), method_id)(**kw)
237
        for c in m.contentValues(filter={'portal_type': self.getPortalMovementTypeList()}):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
238 239 240
          for my_simulation_movement in c.getDeliveryRelatedValueList(
                                                portal_type = 'Simulation Movement'):
            # And apply
241
            getattr(my_simulation_movement.getObject(), method_id)(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
242 243 244 245 246


    #######################################################
    # Causality computation
    security.declareProtected(Permissions.View, 'isConvergent')
247
    def isConvergent(self,**kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
248 249 250
      """
        Returns 0 if the target is not met
      """
251
      return int(not self.isDivergent(**kw))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
252

Jean-Paul Smets's avatar
Jean-Paul Smets committed
253 254
    security.declareProtected(Permissions.View, 'isSimulated')
    def isSimulated(self):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
255 256 257 258
      """
        Returns 1 if all movements have a delivery or order counterpart
        in the simulation
      """
259
      #LOG('Delivery.isSimulated getMovementList',0,self.getMovementList())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
260
      for m in self.getMovementList():
261 262
        #LOG('Delivery.isSimulated m',0,m.getPhysicalPath())
        #LOG('Delivery.isSimulated m.isSimulated',0,m.isSimulated())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
263
        if not m.isSimulated():
264 265
          #LOG('Delivery.isSimulated m.getQuantity',0,m.getQuantity())
          #LOG('Delivery.isSimulated m.getSimulationQuantity',0,m.getSimulationQuantity())
266
          if m.getQuantity() != 0.0 or m.getSimulationQuantity() != 0:
267 268
            return 0
          # else Do we need to create a simulation movement ? XXX probably not
Jean-Paul Smets's avatar
Jean-Paul Smets committed
269 270
      return 1

271
    security.declareProtected(Permissions.View, 'isDivergent')
272
    def isDivergent(self,fast=0,**kw):
273 274 275 276
      """
        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
Jean-Paul Smets's avatar
Jean-Paul Smets committed
277

278 279
        emit targetUnreachable !
      """
280 281 282 283
      # Delivery_zIsDivergent only works when object and simulation is
      # reindexed, so if an user change the delivery, he must wait
      # until everything is indexed, this is not acceptable for users
      # so we should not use it by default (and may be we should remove)
284
      if fast==1 and len(self.Delivery_zIsDivergent(uid=self.getUid())) > 0:
285 286
        return 1
      # Check if the total quantity equals the total of each simulation movement quantity
287 288 289
      for movement in self.getMovementList():
        if movement.isDivergent():
          return 1
Jean-Paul Smets's avatar
Jean-Paul Smets committed
290 291
      return 0

292 293 294 295 296 297
    def updateCausalityState(self,**kw):
      """
      This is often called as an activity, it will check if the
      deliver is convergent, and if so it will put the delivery
      in a solved state, if not convergent in a diverged state
      """
298
      if hasattr(self,'diverge') and hasattr(self,'converge'):
299
        if self.isDivergent(**kw):
300 301 302
          self.diverge()
        else:
          self.converge()
303

Jean-Paul Smets's avatar
Jean-Paul Smets committed
304 305 306 307 308 309
    #######################################################
    # Defer indexing process
    def reindexObject(self, *k, **kw):
      """
        Reindex children and simulation
      """
310 311 312 313
      self.recursiveReindexObject()
      # NEW: we never rexpand simulation - This is a task for DSolver / TSolver
      # Make sure expanded simulation is still OK (expand and reindex)
      # self.activate().applyToDeliveryRelatedMovement(method_id = 'expand')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
314 315 316 317 318

    #######################################################
    # Stock Management
    def _getMovementResourceList(self):
      resource_dict = {}
319
      for m in self.contentValues(filter={'portal_type': self.getPortalMovementTypeList()}):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
320 321 322 323 324 325
        r = m.getResource()
        if r is not None:
          resource_dict[r] = 1
      return resource_dict.keys()

    security.declareProtected(Permissions.AccessContentsInformation, 'getInventory')
326 327 328 329 330 331
    def getInventory(self, **kw):
      """
      Returns inventory
      """
      kw['resource'] = self._getMovementResourceList()
      return self.portal_simulation.getInventory(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
332 333

    security.declareProtected(Permissions.AccessContentsInformation, 'getCurrentInventory')
334
    def getCurrentInventory(self, **kw):
335 336 337 338 339
      """
      Returns current inventory
      """
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getCurrentInventory(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
340 341

    security.declareProtected(Permissions.AccessContentsInformation, 'getAvailableInventory')
342
    def getAvailableInventory(self, **kw):
343 344 345 346 347 348 349 350
      """
      Returns available inventory
      (current inventory - deliverable)
      """
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getAvailableInventory(**kw)

    security.declareProtected(Permissions.AccessContentsInformation, 'getFutureInventory')
351
    def getFutureInventory(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
352
      """
353
      Returns inventory at infinite
Jean-Paul Smets's avatar
Jean-Paul Smets committed
354
      """
355 356
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getFutureInventory(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
357 358

    security.declareProtected(Permissions.AccessContentsInformation, 'getInventoryList')
359
    def getInventoryList(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
360
      """
361
      Returns list of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
362
      """
363 364
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getInventoryList(**kw)
365

366
    security.declareProtected(Permissions.AccessContentsInformation, 'getCurrentInventoryList')
367
    def getCurrentInventoryList(self, **kw):
368 369 370 371 372
      """
      Returns list of inventory grouped by section or site
      """
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getCurrentInventoryList(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
373 374

    security.declareProtected(Permissions.AccessContentsInformation, 'getFutureInventoryList')
375
    def getFutureInventoryList(self, **kw):
376 377 378 379 380
      """
      Returns list of inventory grouped by section or site
      """
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getFutureInventoryList(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
381 382

    security.declareProtected(Permissions.AccessContentsInformation, 'getInventoryStat')
383
    def getInventoryStat(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
384
      """
385
      Returns statistics of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
386
      """
387 388
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getInventoryStat(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
389

390
    security.declareProtected(Permissions.AccessContentsInformation, 'getCurrentInventoryStat')
391
    def getCurrentInventoryStat(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
392
      """
393
      Returns statistics of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
394
      """
395 396
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getCurrentInventoryStat(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
397

398
    security.declareProtected(Permissions.AccessContentsInformation, 'getFutureInventoryStat')
399
    def getFutureInventoryStat(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
400
      """
401
      Returns statistics of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
402
      """
403 404
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getFutureInventoryStat(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
405 406

    security.declareProtected(Permissions.AccessContentsInformation, 'getInventoryChart')
407 408 409 410 411 412 413 414
    def getInventoryChart(self, **kw):
      """
      Returns list of inventory grouped by section or site
      """
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getInventoryChart(**kw)

    security.declareProtected(Permissions.AccessContentsInformation, 'getCurrentInventoryChart')
415
    def getCurrentInventoryChart(self, **kw):
416 417 418 419 420
      """
      Returns list of inventory grouped by section or site
      """
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getCurrentInventoryChart(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
421 422

    security.declareProtected(Permissions.AccessContentsInformation, 'getFutureInventoryChart')
423
    def getFutureInventoryChart(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
424
      """
425
      Returns list of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
426
      """
427 428
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getFutureInventoryChart(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
429

430
    security.declareProtected(Permissions.AccessContentsInformation, 'getInventoryHistoryList')
431
    def getInventoryHistoryList(self, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
432
      """
433
      Returns list of inventory grouped by section or site
Jean-Paul Smets's avatar
Jean-Paul Smets committed
434
      """
435 436
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getInventoryHistoryList(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
437

438
    security.declareProtected(Permissions.AccessContentsInformation, 'getInventoryHistoryChart')
439
    def getInventoryHistoryChart(self, **kw):
440 441 442 443 444
      """
      Returns list of inventory grouped by section or site
      """
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getInventoryHistoryChart(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
445 446

    security.declareProtected(Permissions.AccessContentsInformation, 'getMovementHistoryList')
447
    def getMovementHistoryList(self, **kw):
448 449 450 451 452
      """
      Returns list of inventory grouped by section or site
      """
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getMovementHistoryList(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
453 454

    security.declareProtected(Permissions.AccessContentsInformation, 'getMovementHistoryStat')
455
    def getMovementHistoryStat(self, **kw):
456 457 458 459 460
      """
      Returns list of inventory grouped by section or site
      """
      kw['category'] = self._getMovementResourceList()
      return self.portal_simulation.getMovementHistoryStat(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
461

462 463 464 465 466 467 468 469 470 471 472
    security.declarePrivate( '_edit' )
    def _edit(self, REQUEST=None, force_update = 0, **kw):
      """
      call propagateArrowToSimulation
      """
      XMLObject._edit(self,REQUEST=REQUEST,force_update=force_update,**kw)
      #self.propagateArrowToSimulation()
      # We must expand our applied rule only if not confirmed
      #if self.getSimulationState() in planned_order_state:
      #  self.updateAppliedRule() # This should be implemented with the interaction tool rather than with this hard coding

473 474 475 476 477 478 479 480 481
    security.declareProtected(Permissions.ModifyPortalContent, 'notifySimulationChange')
    def notifySimulationChange(self):
      """
        WorkflowMethod used to notify the causality workflow that the simulation
        has changed, so we have to check if the delivery is divergent or not
      """
      pass
    notifySimulationChange = WorkflowMethod(notifySimulationChange)

482 483
    ##########################################################################
    # Applied Rule stuff
484
    def updateAppliedRule(self, rule_id,force=0,**kw):
485 486 487 488 489 490 491 492
      """
        Create a new Applied Rule is none is related, or call expand
        on the existing one.
      """
      if (rule_id is not None) and\
         (self.getSimulationState() not in \
                                       self.getPortalDraftOrderStateList()):
        # Nothing to do if we are already simulated
493
        self._createAppliedRule(rule_id,force=force,**kw)
494

495
    def _createAppliedRule(self, rule_id,force=0,**kw):
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
      """
        Create a new Applied Rule is none is related, or call expand
        on the existing one.
      """
      # Return if draft or cancelled simulation_state
      if self.getSimulationState() in ('cancelled',):
        # The applied rule should be cleaned up 
        # ie. empty all movements which have no confirmed children
        return
      # Otherwise, expand
      # Look up if existing applied rule
      my_applied_rule_list = self.getCausalityRelatedValueList(\
                                            portal_type='Applied Rule')
      if len(my_applied_rule_list) == 0:
        if self.isSimulated(): 
          # No need to create a DeliveryRule 
          # if we are already in the simulation process
          return 
        # Create a new applied order rule (portal_rules.order_rule)
        portal_rules = getToolByName(self, 'portal_rules')
        portal_simulation = getToolByName(self, 'portal_simulation')
        my_applied_rule = portal_rules[rule_id].\
                                    constructNewAppliedRule(portal_simulation)
        # Set causality
        my_applied_rule.setCausalityValue(self)
        # We must make sure this rule is indexed
        # now in order not to create another one later
523
        my_applied_rule.reindexObject(**kw)
524 525 526 527
      elif len(my_applied_rule_list) == 1:
        # Re expand the rule if possible
        my_applied_rule = my_applied_rule_list[0]
      else:
Jérome Perrin's avatar
Jérome Perrin committed
528 529
        raise "SimulationError", 'Delivery %s has more than one applied'\
                                 ' rule.' % self.getRelativeUrl()
530 531 532

      # We are now certain we have a single applied rule
      # It is time to expand it
533
      self.activate(
534 535
        after_path_and_method_id=(
                my_applied_rule.getPath(),
536 537
               ['immediateReindexObject', 'recursiveImmediateReindexObject']),
               **kw
538
        ).expand(my_applied_rule.getId(),force=force,**kw)
539 540

    security.declareProtected(Permissions.ModifyPortalContent, 'expand')
541
    def expand(self, applied_rule_id, force=0, **kw):
542 543 544 545 546
      """
        Reexpand applied rule
      """
      my_applied_rule = self.portal_simulation.get(applied_rule_id, None)
      if my_applied_rule is not None:
547
        my_applied_rule.expand(force=force, **kw)
548 549 550
        # once expanded, the applied_rule must be reindexed
        # because some simulation_movement may change even
        # if there are not edited (acquisition)
551
        my_applied_rule.activate(**kw).recursiveReindexObject()
552
      else:
Jérome Perrin's avatar
Jérome Perrin committed
553
        LOG("ERP5 Error:", 100,
554 555
            "Could not expand applied rule %s for delivery %s" %\
                (applied_rule_id, self.getId()))
556