DeliveryRule.py 8.1 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 36 37 38 39 40 41 42 43 44 45 46 47 48
#
# 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

from zLOG import LOG

class DeliveryRule(Rule):
    """
      Delivery Rule object make sure orphaned movements in a Delivery
      (ie. movements which have no explanation in terms of order)
      are part of the simulation process
    """

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

    # Declarative security
    security = ClassSecurityInfo()
49
    security.declareObjectProtected(Permissions.AccessContentsInformation)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
50 51 52 53 54 55 56 57 58 59 60 61

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

    def test(self, movement):
      """
        Tests if the rule (still) applies
      """
Romain Courteaud's avatar
Romain Courteaud committed
62 63
      # A delivery rule never applies 
      # since it is always explicitely instanciated
Jean-Paul Smets's avatar
Jean-Paul Smets committed
64 65 66 67
      return 0

    # Simulation workflow
    security.declareProtected(Permissions.ModifyPortalContent, 'expand')
68 69
    def expand(self, applied_rule,
               movement_type_method='getPortalOrderMovementTypeList', **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
70 71 72 73 74 75 76
      """
        Expands the current movement downwards.
        -> new status -> expanded
        An applied rule can be expanded only if its parent movement
        is expanded.
      """
      delivery_line_type = 'Simulation Movement'
77
      # Get the delivery where we come from
Romain Courteaud's avatar
Romain Courteaud committed
78
      # Causality is a kind of Delivery (ex. Packing List)
79
      my_delivery = applied_rule.getDefaultCausalityValue()
Romain Courteaud's avatar
Romain Courteaud committed
80
      # Only expand if my_delivery is not None 
Jean-Paul Smets's avatar
Jean-Paul Smets committed
81
      if my_delivery is not None:
82
        #if my_delivery.getSimulationState() not in ('delivered', ):
83
        # Even if delivered, we should always calculate consequences
Romain Courteaud's avatar
Romain Courteaud committed
84 85

        # First, check each contained movement and make
86
        # a list of delivery uids which do not need to be copied
Romain Courteaud's avatar
Romain Courteaud committed
87 88
        # eventually delete movement which do not exist anylonger
        existing_uid_list = []
Romain Courteaud's avatar
Romain Courteaud committed
89
        existing_uid_list_append = existing_uid_list.append
90
        order_movement_type_list = getattr(applied_rule,
91
                                           movement_type_method)()
Romain Courteaud's avatar
Romain Courteaud committed
92

93
        for movement in applied_rule.objectValues() :
Romain Courteaud's avatar
Romain Courteaud committed
94
          delivery_value = movement.getDeliveryValue(
Romain Courteaud's avatar
Romain Courteaud committed
95
                                        portal_type=order_movement_type_list)
Romain Courteaud's avatar
Romain Courteaud committed
96

Romain Courteaud's avatar
Romain Courteaud committed
97 98 99
          if (delivery_value is None) or\
             (delivery_value.hasCellContent()) or\
             (len(delivery_value.getDeliveryRelatedValueList()) > 1):
Romain Courteaud's avatar
Romain Courteaud committed
100 101 102 103
            # Our delivery_value is already related 
            # to another simulation movement
            # Delete ourselve
            # XXX Make sure this is not deleted if already in delivery
104
            applied_rule._delObject(movement.getId())
Romain Courteaud's avatar
Romain Courteaud committed
105 106
          else:
            existing_uid_list_append(delivery_value.getUid())
Romain Courteaud's avatar
Romain Courteaud committed
107 108 109

        # Copy each movement (line or cell) from the delivery is that
        for delivery_movement in my_delivery.getMovementList():
110 111
          simulation_movement_to_update_list = delivery_movement.\
              getOrderRelatedValueList(portal_type = 'Simulation Movement')
Romain Courteaud's avatar
Romain Courteaud committed
112
          try:
113
            if len(delivery_movement.getDeliveryRelatedValueList()) == 0:
Romain Courteaud's avatar
Romain Courteaud committed
114 115 116 117 118 119 120
              # Only create if orphaned movement
              if delivery_movement.getUid() not in existing_uid_list:
                # Generate a nicer ID
                if delivery_movement.getParentUid() ==\
                                      delivery_movement.getExplanationUid():
                  # We are on a line
                  new_id = delivery_movement.getId()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
121
                else:
Romain Courteaud's avatar
Romain Courteaud committed
122 123 124 125 126
                  # On a cell
                  new_id = "%s_%s" % (delivery_movement.getParentId(),
                                      delivery_movement.getId())
                # Generate the simulation movement
                new_sim_mvt = applied_rule.newContent(
127 128 129 130 131 132 133 134 135 136
                                id          = new_id,
                                portal_type = delivery_line_type,
                                order_value = delivery_movement)
                simulation_movement_to_update_list.append(new_sim_mvt)
              
            for simulation_movement in simulation_movement_to_update_list :
              simulation_movement.edit(
                      delivery_value=delivery_movement,
                      # XXX Do we need to copy the quantity
                      # Why not the resource, the variation,...
137 138 139
                      # force_update is required in order
                      # to make sure the quantity is stored
                      # on the movement
140 141 142 143
                      quantity=delivery_movement.getQuantity(),
                      variation_category_list=\
                        delivery_movement.getVariationCategoryList(),
                      delivery_ratio=1,
144 145
                      deliverable=1,
                      force_update=1)
146

Romain Courteaud's avatar
Romain Courteaud committed
147
          except AttributeError:
148
            LOG('ERP5: WARNING', 0,
Romain Courteaud's avatar
Romain Courteaud committed
149
                'AttributeError during expand on delivery line %s'\
150
                % delivery_movement.absolute_url())
151 152


Jean-Paul Smets's avatar
Jean-Paul Smets committed
153
      # Pass to base class
154
      Rule.expand(self, applied_rule, **kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

    security.declareProtected(Permissions.ModifyPortalContent, 'solve')
    def solve(self, applied_rule, solution_list):
      """
        Solve inconsitency according to a certain number of solutions
        templates. This updates the

        -> new status -> solved

        This applies a solution to an applied rule. Once
        the solution is applied, the parent movement is checked.
        If it does not diverge, the rule is reexpanded. If not,
        diverge is called on the parent movement.
      """

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

        This basically sets the rule to "diverged"
        and blocks expansion process
      """

    # Solvers
    security.declareProtected(Permissions.View, 'isDivergent')
    def isDivergent(self, applied_rule):
      """
        Returns 1 if divergent rule
      """

    security.declareProtected(Permissions.View, 'getDivergenceList')
    def getDivergenceList(self, applied_rule):
      """
        Returns a list Divergence descriptors
      """

    security.declareProtected(Permissions.View, 'getSolverList')
    def getSolverList(self, applied_rule):
      """
        Returns a list Divergence solvers
      """
197 198 199 200 201 202

    # Deliverability / orderability
    def isOrderable(self, m):
      return 1

    def isDeliverable(self, m):
203
      if m.getSimulationState() in m.getPortalDraftOrderStateList():
204 205
        return 0
      return 1