OrderRule.py 6.2 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2
##############################################################################
#
3
# Copyright (c) 2002, 2005 Nexedi SARL and Contributors. All Rights Reserved.
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
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
#
# 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 AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5.Document.Rule import Rule
33
from Products.ERP5.Document.DeliveryRule import DeliveryRule
Jean-Paul Smets's avatar
Jean-Paul Smets committed
34 35
from zLOG import LOG

36
class OrderRule(DeliveryRule):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
37 38 39 40 41 42 43 44 45 46 47 48 49
    """
      Order Rule object make sure an Order in the similation
      is consistent with the real order

      WARNING: what to do with movement split ?
    """

    # CMF Type Definition
    meta_type = 'ERP5 Order Rule'
    portal_type = 'Order Rule'

    # Declarative security
    security = ClassSecurityInfo()
50
    security.declareObjectProtected(Permissions.AccessContentsInformation)
Jérome Perrin's avatar
Jérome Perrin committed
51 52 53
    
    __implements__ = ( Interface.Predicate,
                       Interface.Rule )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
54 55 56 57 58 59 60 61 62 63

    # Default Properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      )

    # Simulation workflow
    security.declareProtected(Permissions.ModifyPortalContent, 'expand')
64
    def expand(self, applied_rule, force=0, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
65 66 67 68 69 70 71 72 73 74 75
      """
        Expands the current movement downward.
        -> new status -> expanded
        An applied rule can be expanded only if its parent movement
        is expanded.
      """
      delivery_line_type = 'Simulation Movement'
      # Get the order when we come from
      my_order = applied_rule.getDefaultCausalityValue()
      # Only expand if my_order is not None and state is not 'confirmed'
      if my_order is not None:
76 77
        # Only expand order rule if order not yet confirmed (This is consistent
        # with the fact that once simulation is launched, we stick to it)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
78
        state = applied_rule.getLastExpandSimulationState()
79
        if force or \
Romain Courteaud's avatar
Romain Courteaud committed
80 81
           (state not in applied_rule.getPortalReservedInventoryStateList()\
           and state not in applied_rule.getPortalCurrentInventoryStateList()):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
82 83 84 85
          # First, check each contained movement and make
          # a list of order ids which do not need to be copied
          # eventually delete movement which do not exist anylonger
          existing_uid_list = []
Romain Courteaud's avatar
Romain Courteaud committed
86
          existing_uid_list_append = existing_uid_list.append
Yoshinori Okuji's avatar
Yoshinori Okuji committed
87
          movement_type_list = applied_rule.getPortalMovementTypeList()
Romain Courteaud's avatar
Romain Courteaud committed
88 89
          order_movement_type_list = \
                                 applied_rule.getPortalOrderMovementTypeList()
90
          # Calculate existing simulation movement to delete
Romain Courteaud's avatar
Romain Courteaud committed
91 92
          for movement in applied_rule.contentValues(
                                filter={'portal_type': movement_type_list}):
93
            order_value = movement.getOrderValue(\
Romain Courteaud's avatar
Romain Courteaud committed
94 95 96 97
                                         portal_type=order_movement_type_list)
            if (order_value is None) or\
               (order_value.hasCellContent()):
              # XXX Make sure this is not deleted if already in delivery
98
              applied_rule._delObject(movement.getId())  
Jean-Paul Smets's avatar
Jean-Paul Smets committed
99
            else:
Romain Courteaud's avatar
Romain Courteaud committed
100
              existing_uid_list_append(order_value.getUid())
101
          # Build simulation movement if necessary
Romain Courteaud's avatar
Romain Courteaud committed
102
          for order_movement in my_order.getMovementList():
Jean-Paul Smets's avatar
Jean-Paul Smets committed
103
            try:
Romain Courteaud's avatar
Romain Courteaud committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117
              if order_movement.getUid() not in existing_uid_list:
                # Generate a nicer ID
                if order_movement.getParentUid() ==\
                                      order_movement.getExplanationUid():
                  # We are on a line
                  new_id = order_movement.getId()
                else:
                  # On a cell
                  new_id = "%s_%s" % (order_movement.getParentId(),
                                      order_movement.getId())
                # Generate the simulation movement
                # Source, Destination, Quantity, Date, etc. are
                # acquired from the order and need not to be copied.
                new_sim_mvt = applied_rule.newContent(
118
                                portal_type=delivery_line_type,
Romain Courteaud's avatar
Romain Courteaud committed
119 120 121
                                id=new_id,
                                order_value=order_movement,
                                delivery_ratio=1,
122 123
                                deliverable=1,
                                **kw)
Romain Courteaud's avatar
Romain Courteaud committed
124 125
                                # No acquisition on variation_category_list 
                                # in this case to prevent user failure
Jean-Paul Smets's avatar
Jean-Paul Smets committed
126
            except AttributeError:
127 128 129
              LOG('ERP5: WARNING', 0,\
                  'AttributeError during expand on order movement %s'\
                  % order_movement.absolute_url())
130 131
          # Now we can set the last expand simulation state 
          # to the current state
Romain Courteaud's avatar
Romain Courteaud committed
132 133
          applied_rule.setLastExpandSimulationState(\
                                               my_order.getSimulationState())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
134
      # Pass to base class
135
      Rule.expand(self, applied_rule, force=force, **kw)