OrderRule.py 6.12 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)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
51 52 53 54 55 56 57 58 59 60

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

    # Simulation workflow
    security.declareProtected(Permissions.ModifyPortalContent, 'expand')
61
    def expand(self, applied_rule, force=0, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
62 63 64 65 66 67 68 69 70 71 72
      """
        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:
73 74
        # 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
75
        state = applied_rule.getLastExpandSimulationState()
76
        if force or \
Romain Courteaud's avatar
Romain Courteaud committed
77 78
           (state not in applied_rule.getPortalReservedInventoryStateList()\
           and state not in applied_rule.getPortalCurrentInventoryStateList()):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
79 80 81 82
          # 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
83
          existing_uid_list_append = existing_uid_list.append
Yoshinori Okuji's avatar
Yoshinori Okuji committed
84
          movement_type_list = applied_rule.getPortalMovementTypeList()
Romain Courteaud's avatar
Romain Courteaud committed
85 86
          order_movement_type_list = \
                                 applied_rule.getPortalOrderMovementTypeList()
87
          # Calculate existing simulation movement to delete
Romain Courteaud's avatar
Romain Courteaud committed
88 89
          for movement in applied_rule.contentValues(
                                filter={'portal_type': movement_type_list}):
90
            order_value = movement.getOrderValue(\
Romain Courteaud's avatar
Romain Courteaud committed
91 92 93 94
                                         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
95
              applied_rule._delObject(movement.getId())  
Jean-Paul Smets's avatar
Jean-Paul Smets committed
96
            else:
Romain Courteaud's avatar
Romain Courteaud committed
97
              existing_uid_list_append(order_value.getUid())
98
          # Build simulation movement if necessary
Romain Courteaud's avatar
Romain Courteaud committed
99
          for order_movement in my_order.getMovementList():
Jean-Paul Smets's avatar
Jean-Paul Smets committed
100
            try:
Romain Courteaud's avatar
Romain Courteaud committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114
              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(
115
                                portal_type=delivery_line_type,
Romain Courteaud's avatar
Romain Courteaud committed
116 117 118
                                id=new_id,
                                order_value=order_movement,
                                delivery_ratio=1,
119 120
                                deliverable=1,
                                **kw)
Romain Courteaud's avatar
Romain Courteaud committed
121 122
                                # No acquisition on variation_category_list 
                                # in this case to prevent user failure
Jean-Paul Smets's avatar
Jean-Paul Smets committed
123
            except AttributeError:
124 125 126
              LOG('ERP5: WARNING', 0,\
                  'AttributeError during expand on order movement %s'\
                  % order_movement.absolute_url())
127 128
          # Now we can set the last expand simulation state 
          # to the current state
Romain Courteaud's avatar
Romain Courteaud committed
129 130
          applied_rule.setLastExpandSimulationState(\
                                               my_order.getSimulationState())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
131
      # Pass to base class
132
      Rule.expand(self, applied_rule, force=force, **kw)