SupplyChain.py 13.2 KB
Newer Older
Romain Courteaud's avatar
Romain Courteaud committed
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
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL 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 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
30
from Products.ERP5Type import Permissions, PropertySheet
Romain Courteaud's avatar
Romain Courteaud committed
31 32 33
from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5.Document.Path import Path

34 35
class SupplyChainError(Exception): pass

Romain Courteaud's avatar
Romain Courteaud committed
36 37 38 39 40 41 42 43 44 45
class SupplyChain(Path, XMLObject):
  """
    SupplyChain defines the route used to produced a resource.
  """
  # CMF Type Definition
  meta_type = 'ERP5 Supply Chain'
  portal_type = 'Supply Chain'

  # Declarative security
  security = ClassSecurityInfo()
46
  security.declareObjectProtected(Permissions.AccessContentsInformation)
Romain Courteaud's avatar
Romain Courteaud committed
47 48 49 50 51 52 53 54

  # Default Properties
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.XMLObject
                    , PropertySheet.CategoryCore
                    , PropertySheet.DublinCore
                    , PropertySheet.Task
                    , PropertySheet.Arrow
55 56
                    , PropertySheet.Reference
                    , PropertySheet.Comment
Romain Courteaud's avatar
Romain Courteaud committed
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
                    , PropertySheet.Movement
                    , PropertySheet.Delivery
                    , PropertySheet.Path
                    , PropertySheet.FlowCapacity
                    )

  # Class variable
  supply_link_portal_type="Supply Link"

  security.declareProtected(Permissions.View, 'getLastLink')
  def getLastLink(self):
    """
      Return the SupplyLink representing the last ridge of the 
      SupplyChain (if this one is correctly defined...).
    """
    # Result value
    result = None
    # Get all lines.
    supply_link_list = self.objectValues(
                               portal_type=self.supply_link_portal_type)
    # Last line is defined by deliverable=1
    last_supply_link_list = [x for x in supply_link_list if\
                              x.getDeliverable()]
    # Check if user did not define multiple last links
    last_list_len = len(last_supply_link_list)
    if (last_list_len == 1):
      result = last_supply_link_list[0]
    else:
85
      raise SupplyChainError,\
Romain Courteaud's avatar
Romain Courteaud committed
86 87 88 89
            "Unable to get the last link of SupplyChain %s" %\
            str(self.getRelativeUrl())
    return result

90 91 92 93 94 95 96 97 98 99
  security.declareProtected(Permissions.View,
                            'getNextSupplyLinkList')
  def getNextSupplyLinkList(self, current_supply_link):
    """
      Return the previous SupplyLink  list.
    """
    supply_link_list = self.objectValues(
                                 portal_type=self.supply_link_portal_type)
    # Search next link
    next_node_value = current_supply_link.getNextNodeValue()
100 101 102 103 104 105 106 107 108 109

    next_supply_link_list = []
    next_production_list = []
    for supply_link in supply_link_list:
      if supply_link != current_supply_link and \
          supply_link.getCurrentNodeValue() == next_node_value:
        next_supply_link_list.append(supply_link)
        if supply_link.isProductionSupplyLink():
          next_production_list.append(supply_link)

110
    if next_production_list != []:
111
      return next_production_list
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    return next_supply_link_list

  security.declareProtected(Permissions.View,
                            'getNextProductionSupplyLinkList')
  def getNextProductionSupplyLinkList(self, current_supply_link):
    """
      Return the next SupplyLink which represents a production,
      if there is one.
      No recursion is done.
    """
    next_supply_link_list = self.getNextSupplyLinkList(current_supply_link)
    return [x for x in next_supply_link_list if x.isProductionSupplyLink()]
    
  security.declareProtected(Permissions.View,
                            'getNextProductionIndustrialPhaseList')
  def getNextProductionIndustrialPhaseList(self, current_supply_link):
    """
      Return all next industrial phase representing a production.
    """
    ind_phase_dict = {}
    for link in self.getNextProductionSupplyLinkList(current_supply_link):
      for ind_phase in link.getIndustrialPhaseValueList():
        ind_phase_dict[ind_phase] = 1
    # Remove None value, and generate the list
    ind_phase_dict.pop(None, None)
    return ind_phase_dict.keys()

  security.declareProtected(Permissions.View,
                            'getPreviousSupplyLinkList')
Romain Courteaud's avatar
Romain Courteaud committed
141 142 143 144
  def getPreviousSupplyLinkList(self, current_supply_link):
    """
      Return the previous SupplyLink  list.
    """
145 146 147 148
    if current_supply_link is None:
      # No current_supply_link defined, we need to return the last SupplyLink
      return [self.getLastLink()]

149 150 151 152 153
    # Get all SupplyLink in the SupplyChain
    supply_link_list = self.objectValues(
                               portal_type=self.supply_link_portal_type)
    # Destination of valid link must be the source of the current link.
    current_node_value = current_supply_link.getCurrentNodeValue()
154

155 156 157 158 159 160 161 162
    previous_supply_link_list = []
    previous_production_list = []
    for supply_link in supply_link_list:
      if supply_link != current_supply_link and \
        supply_link.getNextNodeValue() == current_node_value:
        previous_supply_link_list.append(supply_link)
        if supply_link.isProductionSupplyLink():
          previous_production_list.append(supply_link)
163

164 165 166
    if previous_production_list != []:
      return previous_production_list
    return previous_supply_link_list
Romain Courteaud's avatar
Romain Courteaud committed
167 168 169 170

  security.declareProtected(Permissions.View,
                            'getPreviousProductionSupplyLinkList')
  def getPreviousProductionSupplyLinkList(self, current_supply_link, 
171
                                          recursive=False, all=False,
Romain Courteaud's avatar
Romain Courteaud committed
172 173 174
                                          checked_link_list=None):
    """
      Return the previous SupplyLink which represents a production.
175
      If recursive, browse the SupplyChain until a valid link is found.
Romain Courteaud's avatar
Romain Courteaud committed
176 177
      checked_link_list is used to prevent infinite loop.
    """
Nicolas Dumazet's avatar
Nicolas Dumazet committed
178 179
    # XXX document "all" parameter if you can

Romain Courteaud's avatar
Romain Courteaud committed
180 181 182 183 184 185
    # Initialize checked_link_list parameter...
    if checked_link_list is None:
      checked_link_list = []
    # Checked if we already tested this link 
    # to prevent infinite loop
    if current_supply_link in checked_link_list:
186
      raise SupplyChainError,\
Romain Courteaud's avatar
Romain Courteaud committed
187
            "SupplyLink %r is in a loop." % current_supply_link
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
    transformation_link_list = []
    checked_link_list.append(current_supply_link)
    # Get the previous link list
    previous_link_list = self.getPreviousSupplyLinkList(current_supply_link)
    # Test each link
    for previous_link in previous_link_list:
      if not previous_link.isProductionSupplyLink():
        # current is invalid
        if not recursive:
          continue
      else:
        # Great, we found a valid one
        transformation_link_list.append(previous_link)
        # Prevent infinite loop when 2 production_link have the same
        # destination
        if (current_supply_link is not None) and \
           (current_supply_link.isProductionSupplyLink()):
          raise SupplyChainError,\
                "Those SupplyLinks are in conflict: %r and %r" %\
                (current_supply_link.getRelativeUrl(),\
                 previous_link.getRelativeUrl())
        if not recursive and not all:
          continue

      # Browse the previous link
      transformation_link_list.extend(
        self.getPreviousProductionSupplyLinkList(
                                     previous_link, 
                                     recursive=recursive, all=all,
                                     checked_link_list=checked_link_list))
    # Return result
    return transformation_link_list
Romain Courteaud's avatar
Romain Courteaud committed
221 222 223 224

  security.declareProtected(Permissions.View,
                            'getPreviousPackingListSupplyLinkList')
  def getPreviousPackingListSupplyLinkList(self, current_supply_link, 
Nicolas Dumazet's avatar
Nicolas Dumazet committed
225
                                           recursive=False, all=False,
Romain Courteaud's avatar
Romain Courteaud committed
226 227 228 229
                                           checked_link_list=None,
                                           movement=None):
    """
      Return the previous SupplyLink which represents a production.
Nicolas Dumazet's avatar
Nicolas Dumazet committed
230
      If recursive, browse the SupplyChain until a valid link is found.
Romain Courteaud's avatar
Romain Courteaud committed
231 232
      checked_link_list is used to prevent infinite loop.
    """
Nicolas Dumazet's avatar
Nicolas Dumazet committed
233 234
    # XXX document "all" parameter

Romain Courteaud's avatar
Romain Courteaud committed
235 236 237 238 239 240
    # Initialize checked_link_list parameter...
    if checked_link_list is None:
      checked_link_list = []
    # Checked if we already tested this link 
    # to prevent infinite loop
    if current_supply_link in checked_link_list:
241
      raise SupplyChainError,\
Romain Courteaud's avatar
Romain Courteaud committed
242
            "SupplyLink %r is in a loop." % current_supply_link
Nicolas Dumazet's avatar
Nicolas Dumazet committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

    packing_list_link_list = []
    checked_link_list.append(current_supply_link)
    # Get the previous link list
    previous_link_list = self.getPreviousSupplyLinkList(current_supply_link)
    # Test each link
    for previous_link in previous_link_list:
      concurrent_list = previous_link_list[:]
      concurrent_list.remove(previous_link)
      # Great, we find a valid one
      if previous_link.isPackingListSupplyLink():
        if (movement is None) or\
           (previous_link.test(movement, concurrent_list)):
          packing_list_link_list.append(previous_link)
        # Browse the previous link
        if recursive:
          packing_list_link_list.extend(
            self.getPreviousPackingListSupplyLinkList(
                                       previous_link,
                                       recursive=recursive,
                                       checked_link_list=checked_link_list))
    # Return result
    return packing_list_link_list
Romain Courteaud's avatar
Romain Courteaud committed
266 267

  def getPreviousIndustrialPhaseList(self, current_supply_link, method_id,
Nicolas Dumazet's avatar
Nicolas Dumazet committed
268
                                     include_current=False, all=False):
Romain Courteaud's avatar
Romain Courteaud committed
269 270 271 272
    """
      Return recursively all previous industrial phase.
    """
    method = getattr(self, method_id)
273 274
    previous_supply_link_list = method(current_supply_link, recursive=1,
                                       all=all)
Romain Courteaud's avatar
Romain Courteaud committed
275
    # Add the current industrial phase
Nicolas Dumazet's avatar
Nicolas Dumazet committed
276
    if include_current:
Romain Courteaud's avatar
Romain Courteaud committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290
      previous_supply_link_list.append(current_supply_link)
    # Generate the industrial phase list, and remove double
    ind_phase_dict = {}
    for supply_link in previous_supply_link_list:
      ind_phase_value_list = supply_link.getIndustrialPhaseValueList()
      for ind_phase in ind_phase_value_list:
        ind_phase_dict[ind_phase] = 1
    # Remove None value, and generate the list
    ind_phase_dict.pop(None, None)
    ind_phase_list = ind_phase_dict.keys()
    return ind_phase_list

  security.declareProtected(Permissions.View,
                            'getPreviousProductionIndustrialPhaseList')
291
  def getPreviousProductionIndustrialPhaseList(self, current_supply_link,
Nicolas Dumazet's avatar
Nicolas Dumazet committed
292
                                               all=False):
Romain Courteaud's avatar
Romain Courteaud committed
293 294 295 296 297 298
    """
      Return recursively all previous industrial phase representing 
      a production.
    """
    return self.getPreviousIndustrialPhaseList(
                                   current_supply_link,
299 300
                                   "getPreviousProductionSupplyLinkList",
                                   all=all)
Romain Courteaud's avatar
Romain Courteaud committed
301 302 303 304 305 306 307 308 309 310 311

  security.declareProtected(Permissions.View,
                            'getPreviousPackingListIndustrialPhaseList')
  def getPreviousPackingListIndustrialPhaseList(self, current_supply_link):
    """
      Return recursively all previous industrial phase representing 
      a packing list.
    """
    return self.getPreviousIndustrialPhaseList(
                                   current_supply_link,
                                   "getPreviousPackingListSupplyLinkList",
Nicolas Dumazet's avatar
Nicolas Dumazet committed
312
                                   include_current=True)
Romain Courteaud's avatar
Romain Courteaud committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

  security.declareProtected(Permissions.View,
                            'test')
  def test(self, current_supply_link, movement):
    """
      Test if the resource on the movement can be delivered by 
      the previous supply link of the current one.
    """
    result = 0
    previous_packing_link_list = self.\
                   getPreviousPackingListSupplyLinkList(current_supply_link)
    for previous_supply_link in previous_packing_link_list:
      concurrent_list = previous_packing_link_list[:]
      concurrent_list.remove(previous_supply_link)
      if previous_supply_link.test(movement, concurrent_list):
        result = 1
        break
    return result