Transformation.py 13.8 KB
Newer Older
Yusei Tahara's avatar
Yusei Tahara committed
1
# -*- coding: utf-8 -*-
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2 3 4 5
##############################################################################
#
# Copyright (c) 2002 Coramy SAS and Contributors. All Rights Reserved.
#                    Thierry_Faucher <Thierry_Faucher@coramy.com>
6
# Copyright (c) 2004-2009 Nexedi SA and Contributors. All Rights Reserved.
7
#                    Romain Courteaud <romain@nexedi.com>
8
#                    Łukasz Nowak <luke@nexedi.com>
Jean-Paul Smets's avatar
Jean-Paul Smets committed
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.
#
##############################################################################

33
import zope.interface
Jean-Paul Smets's avatar
Jean-Paul Smets committed
34 35
from AccessControl import ClassSecurityInfo

36
from Products.ERP5Type import Permissions, PropertySheet, interfaces
Jean-Paul Smets's avatar
Jean-Paul Smets committed
37 38 39 40
from Products.ERP5Type.XMLObject import XMLObject

from Products.ERP5.Variated import Variated

41
from Products.ERP5.Document.Predicate import Predicate
Jean-Paul Smets's avatar
Jean-Paul Smets committed
42

43
from Products.CMFCategory.Renderer import Renderer
44
from Products.ERP5.AggregatedAmountList import AggregatedAmountList
Jean-Paul Smets's avatar
Jean-Paul Smets committed
45

46 47
from zLOG import LOG, WARNING

48
class Transformation(XMLObject, Predicate, Variated):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
49 50 51 52 53 54
    """
      Build of material - contains a list of transformed resources

      Use of default_resource... (to define the variation range,
      to ...)

Nicolas Dumazet's avatar
Nicolas Dumazet committed
55
      XXX Transformation works only for a maximum of 3 variation base category...
Nicolas Dumazet's avatar
Nicolas Dumazet committed
56
      Matrixbox must be rewritten for a clean implementation of n base category
Jean-Paul Smets's avatar
Jean-Paul Smets committed
57 58 59 60 61 62 63

    """
    meta_type = 'ERP5 Transformation'
    portal_type = 'Transformation'

    # Declarative security
    security = ClassSecurityInfo()
64
    security.declareObjectProtected(Permissions.AccessContentsInformation)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
65 66 67 68 69 70 71

    # Declarative properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      , PropertySheet.VariationRange
72
                      , PropertySheet.Predicate
73 74
                      , PropertySheet.Comment
                      , PropertySheet.Reference
75
                      , PropertySheet.Version
76 77 78
                      #, PropertySheet.Resource
                      , PropertySheet.TransformedResource
                      , PropertySheet.Path
Jean-Paul Smets's avatar
Jean-Paul Smets committed
79 80 81 82
                      , PropertySheet.Transformation
                      )

    # Declarative interfaces
83
    zope.interface.implements(interfaces.IVariated, 
84
                              interfaces.IAmountGenerator
85 86
                              )

Jean-Paul Smets's avatar
Jean-Paul Smets committed
87

88

Fabien Morin's avatar
Fabien Morin committed
89
    security.declareProtected(Permissions.AccessContentsInformation,
90
                              'updateVariationCategoryList')
91 92
    def updateVariationCategoryList(self):
      """
Nicolas Dumazet's avatar
Nicolas Dumazet committed
93 94
        Check if variation category list of the resource has changed and update
        transformation and transformation line
95
      """
96
      self.setVariationBaseCategoryList(self.getVariationBaseCategoryList())
97 98 99 100
      transformation_line_list = self.contentValues()
      for transformation_line in transformation_line_list:
        transformation_line.updateVariationCategoryList()

101 102 103 104 105
    security.declareProtected(Permissions.AccessContentsInformation,
                              'getVariationRangeBaseCategoryList')
    def getVariationRangeBaseCategoryList(self):
      """
        Returns possible variation base_category ids of the
Nicolas Dumazet's avatar
Nicolas Dumazet committed
106
        default resource which can be used as variation axis
107 108 109 110 111 112 113 114 115 116 117 118
        in the transformation.
      """
      resource = self.getResourceValue()
      if resource is not None:
        result = resource.getVariationBaseCategoryList()
      else:
        # XXX result = self.getBaseCategoryIds()
        # Why calling this method ?
        # Get a global variable which define a list of variation base category
        result = self.getPortalVariationBaseCategoryList()
      return result

Fabien Morin's avatar
Fabien Morin committed
119
    security.declareProtected(Permissions.AccessContentsInformation,
120
                              'getVariationRangeBaseCategoryItemList')
121
    def getVariationRangeBaseCategoryItemList(self, display_id='getTitleOrId', **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
122
        """
123
          Returns possible variations of the transformation
Jean-Paul Smets's avatar
Jean-Paul Smets committed
124 125 126 127
          as a list of tuples (id, title). This is mostly
          useful in ERP5Form instances to generate selection
          menus.
        """
Fabien Morin's avatar
Fabien Morin committed
128
        return self.portal_categories.getItemList(
129 130
                              self.getVariationRangeBaseCategoryList(),
                              display_id=display_id, **kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
131

132 133
    security.declareProtected(Permissions.AccessContentsInformation,
                              'getVariationRangeCategoryItemList')
134
    def getVariationRangeCategoryItemList(self, base_category_list=(),
135
                                          omit_individual_variation=0,
136
                                          display_base_category=1, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
137 138 139
        """
          Returns possible variation category values for the
          transformation according to the default resource.
Nicolas Dumazet's avatar
Nicolas Dumazet committed
140
          Possible category values are provided as a list of
Jean-Paul Smets's avatar
Jean-Paul Smets committed
141 142 143
          tuples (id, title). This is mostly
          useful in ERP5Form instances to generate selection
          menus.
144
          User may want to define generic transformation without
Nicolas Dumazet's avatar
Nicolas Dumazet committed
145
          any defined resource.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
146 147 148
        """
        if base_category_list is ():
          base_category_list = self.getVariationBaseCategoryList()
149 150

        resource = self.getResourceValue()
Fabien Morin's avatar
Fabien Morin committed
151
        if resource is not None:
152
          result = resource.getVariationCategoryItemList(
153
                        base_category_list=base_category_list,
154
                        omit_individual_variation=omit_individual_variation,
155
                        display_base_category=display_base_category,**kw)
156
        else:
157 158 159 160
          # No resource is define on transformation. 
          # We want to display content of base categories
          result = self.portal_categories.getCategoryChildTitleItemList(
                         base_category_list, base=1, display_none_category=0)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
161 162
        return result

Fabien Morin's avatar
Fabien Morin committed
163
    security.declareProtected(Permissions.AccessContentsInformation,
164
                              '_setVariationBaseCategoryList')
165
    def _setVariationBaseCategoryList(self, value):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
166
      """
167
        Define the possible base categories
Jean-Paul Smets's avatar
Jean-Paul Smets committed
168
      """
169 170 171 172 173 174 175 176 177 178
#      XXX TransformedResource works only for a maximum of 3 variation base category...
#      Matrixbox must be rewrite for a clean implementation of n base category
      if len(value) <= 3:
        self._baseSetVariationBaseCategoryList(value)
      else:
        raise MoreThan3VariationBaseCategory

      # create relations between resource variation and transformation
      self._setVariationCategoryList( self.getVariationRangeCategoryList() )

Fabien Morin's avatar
Fabien Morin committed
179
    security.declareProtected(Permissions.AccessContentsInformation,
180
                              'setVariationBaseCategoryList')
181 182 183 184 185 186 187
    def setVariationBaseCategoryList(self, value):
      """
        Define the possible base categories and reindex object
      """
      self._setVariationBaseCategoryList(value)
      self.reindexObject()

188 189 190 191 192 193 194 195 196 197 198 199 200 201
    security.declareProtected(Permissions.AccessContentsInformation,
                              'getVariationCategoryList')
    def getVariationCategoryList(self, **kwd):
      """
      Returns the variation categories applicable to the Transformation.
      When a resource is defined, it should not return more than what is
      set through the Resource.
      """
      resource = self.getResourceValue()
      if resource is not None:
        return resource.getVariationCategoryList(**kwd)
      else:
        return self.getVariationCategoryList(**kwd)

Fabien Morin's avatar
Fabien Morin committed
202
    security.declareProtected(Permissions.AccessContentsInformation,
203
                              'getVariationCategoryItemList')
Fabien Morin's avatar
Fabien Morin committed
204 205
    def getVariationCategoryItemList(self, base_category_list=(), base=1,
                                     display_id='title',
206 207
                                     current_category=None,
                                     **kw):
208 209 210 211 212 213 214 215 216
      """
        Returns the list of possible variations
        XXX Copied and modified from Variated
        Result is left display.
      """
      variation_category_item_list = []
      if base_category_list == ():
        base_category_list = self.getVariationBaseCategoryList()

217 218 219 220 221 222
      category_renderer = Renderer(
                             is_right_display=0,
                             display_none_category=0, base=base,
                             current_category=current_category,
                             display_id='logical_path', **kw)

223 224 225 226
      for base_category in base_category_list:
        variation_category_list = self.getVariationCategoryList(
                                            base_category_list=[base_category])

227 228 229 230 231 232 233 234 235
        category_list = []
        object_list = []
        for variation_category in variation_category_list:
          resource = self.portal_categories.resolveCategory(variation_category)
          if resource.getPortalType() == 'Category':
            category_list.append(resource)
          else:
            object_list.append(resource)

236 237 238
        variation_category_item_list.extend(category_renderer.\
                                              render(category_list))

239 240
        variation_category_item_list.extend(Renderer(
                               is_right_display=0,
Fabien Morin's avatar
Fabien Morin committed
241
                               base_category=base_category,
242 243
                               display_none_category=0, base=base,
                               current_category=current_category,
244
                               display_id=display_id,**kw).\
245
                                                 render(object_list))
246 247
      return variation_category_item_list

248 249 250
    def updateAggregatedAmountList(self, context, **kw):
      raise NotImplementedError, 'need?'

Fabien Morin's avatar
Fabien Morin committed
251
    security.declareProtected(Permissions.AccessContentsInformation,
252 253
                              'getAggregatedAmountList')
    def getAggregatedAmountList(self, context=None, REQUEST=None,
254 255 256
                                trade_phase_list=None,
                                # obsolete, use trade_phase_list instead
                                ind_phase_url_list=None,
Fabien Morin's avatar
Fabien Morin committed
257
                                rejected_resource_uid_list=None,
258
                                context_quantity=0,**kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
259
      """
Nicolas Dumazet's avatar
Nicolas Dumazet committed
260
        getAggregatedAmountList returns an AggregatedAmountList which
261 262
        can be used either to do some calculation (ex. price, BOM)
        or to display a detailed view of a transformation.
263 264 265

        context_quantity : if set to one, multiply all quantities
                           with the quantity of the context
Jean-Paul Smets's avatar
Jean-Paul Smets committed
266
      """
267
      context = self.asContext(context=context, REQUEST=REQUEST, **kw)
268 269 270 271 272

      # A list of functions taking a transformation_line as sole argument
      # and returning True iif the line should be kept in the result
      filter_list = []

273 274
      # Get only lines related to a precise trade_phase
      if trade_phase_list is not None:
275 276 277 278 279
        def trade_phase_filter(line):
          return line.getTradePhase() in trade_phase_list

        filter_list.append(trade_phase_filter)

280
      # Get only lines related to a precise industrial_phase
281
      if ind_phase_url_list is not None:
282
        LOG("Transformation", WARNING, "ind_phase_list is obsolete")
283
        def industrial_phase_filter(line):
284 285
          ind_ph = line.getIndustrialPhaseValue()
          if ind_ph is not None:
286 287 288 289 290
            return ind_ph.getRelativeUrl() in ind_phase_url_list
          return False

        filter_list.append(industrial_phase_filter)

291 292
      # Filter lines with resource we do not want to see
      if rejected_resource_uid_list is not None:
293 294 295 296 297 298 299 300 301 302 303 304
        def rejected_uid_filter(line):
          return line.getResourceUid() not in rejected_resource_uid_list

        filter_list.append(rejected_uid_filter)

      def line_is_included(line):
        # XXX > 2.5 : all(f(line) for f in filter_list)
        for filterr in filter_list:
          if not filterr(line):
            return False
        return True

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
      # First we need to get the list of transformations which this
      # transformation depends on
      # At this moment, we only consider 1 dependency
      template_transformation_list = self.getSpecialiseValueList()

      # Browse all involved transformations and create one line per
      # line of transformation
      # Currently, we do not consider abstractions, we just add
      # whatever we find in all transformations
      result = AggregatedAmountList()
      for transformation in ([self] + template_transformation_list):
        for transformation_line in transformation.objectValues():
          # Browse each transformed or assorted resource of the current
          # transformation
          if line_is_included(transformation_line):
320 321 322 323 324 325 326
            try:
              result.extend(transformation_line.getAggregatedAmountList(context))
            except KeyError:
              # KeyError is raised by TransformedResource.getAggregatedAmountList
              # in case of misconfiguration of a Cell.
              # Just ignore the line
              pass
327

328 329
      if context_quantity:
        result.multiplyQuantity(context=context)
330
      return result