SimulationTool.py 143 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.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
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
#
# 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.
#
##############################################################################

30
from Products.CMFCore.utils import getToolByName
Jean-Paul Smets's avatar
Jean-Paul Smets committed
31 32

from AccessControl import ClassSecurityInfo
33
from Products.ERP5Type.Globals import InitializeClass, DTMLFile
Jean-Paul Smets's avatar
Jean-Paul Smets committed
34
from Products.ERP5Type import Permissions
35
from Products.ERP5Type.Tool.BaseTool import BaseTool
Jean-Paul Smets's avatar
Jean-Paul Smets committed
36 37 38

from Products.ERP5 import _dtmldir

39
from zLOG import LOG, PROBLEM
Jean-Paul Smets's avatar
Jean-Paul Smets committed
40 41

from Products.ERP5.Capacity.GLPK import solve
42
from numpy import zeros, resize
Alexandre Boeglin's avatar
Alexandre Boeglin committed
43
from DateTime import DateTime
Jean-Paul Smets's avatar
Jean-Paul Smets committed
44

45 46
from Products.ERP5 import DeliverySolver
from Products.ERP5 import TargetSolver
47
from Products.PythonScripts.Utility import allow_class
Jean-Paul Smets's avatar
Jean-Paul Smets committed
48

49
from Products.ZSQLCatalog.SQLCatalog import Query, ComplexQuery
50

51
from Shared.DC.ZRDB.Results import Results
52
from Products.ERP5Type.Utils import mergeZRDBResults
53

54 55
from warnings import warn

56
class SimulationTool(BaseTool):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    """
    The SimulationTool implements the ERP5
    simulation algorithmics.


    Examples of applications:

    -

    -
    ERP5 main purpose:

    -

    -

    """
    id = 'portal_simulation'
    meta_type = 'ERP5 Simulation Tool'
76
    portal_type = 'Simulation Tool'
Jean-Paul Smets's avatar
Jean-Paul Smets committed
77 78 79 80 81 82 83 84 85 86 87 88
    allowed_types = ( 'ERP5 Applied Rule', )

    # Declarative Security
    security = ClassSecurityInfo()

    #
    #   ZMI methods
    #
    security.declareProtected( Permissions.ManagePortal, 'manage_overview' )
    manage_overview = DTMLFile( 'explainSimulationTool', _dtmldir )

    def filtered_meta_types(self, user=None):
89 90 91 92 93 94 95
      # Filters the list of available meta types.
      all = SimulationTool.inheritedAttribute('filtered_meta_types')(self)
      meta_types = []
      for meta_type in self.all_meta_types():
        if meta_type['name'] in self.allowed_types:
          meta_types.append(meta_type)
      return meta_types
Jean-Paul Smets's avatar
Jean-Paul Smets committed
96

97 98 99
    def tpValues(self) :
      """ show the content in the left pane of the ZMI """
      return self.objectValues()
100

101 102 103
    security.declarePrivate('manage_afterAdd')
    def manage_afterAdd(self, item, container) :
      """Init permissions right after creation.
104

105 106 107 108 109 110 111 112 113 114 115 116
      Permissions in simulation tool are simple:
       o Each member can access and create some content.
       o Only manager can view, because simulation can be seen as
         sensitive information.
      """
      item.manage_permission(Permissions.AddPortalContent,
            ['Member', 'Author', 'Manager'])
      item.manage_permission(Permissions.AccessContentsInformation,
            ['Member', 'Auditor', 'Manager'])
      item.manage_permission(Permissions.View,
            ['Manager',])
      BaseTool.inheritedAttribute('manage_afterAdd')(self, item, container)
117

118 119
    def solveDelivery(self, delivery, delivery_solver_name, target_solver_name,
                      additional_parameters=None, **kw):
120
      """
121
        Solves a delivery by calling first DeliverySolver, then TargetSolver
122
      """
123 124 125
      return self._solveMovementOrDelivery(delivery, delivery_solver_name,
          target_solver_name, delivery=1,
          additional_parameters=additional_parameters, **kw)
126

127 128
    def solveMovement(self, movement, delivery_solver_name, target_solver_name,
                      additional_parameters=None, **kw):
129
      """
130
        Solves a movement by calling first DeliverySolver, then TargetSolver
131
      """
132 133 134
      return self._solveMovementOrDelivery(movement, delivery_solver_name,
          target_solver_name, movement=1,
          additional_parameters=additional_parameters, **kw)
135

136 137 138
    def _solveMovementOrDelivery(self, document, delivery_solver_name,
                                 target_solver_name, movement=0, delivery=0,
                                 additional_parameters=None,**kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
139
      """
140
        Solves a document by calling first DeliverySolver, then TargetSolver
Jean-Paul Smets's avatar
Jean-Paul Smets committed
141
      """
142 143 144 145 146 147 148 149
      if movement == delivery:
        raise ValueError('Parameters movement and delivery have to be'
                         ' different')

      solve_result = []
      for solver_name, solver_module in ((delivery_solver_name, DeliverySolver),
                                         (target_solver_name, TargetSolver)):
        result = None
150 151 152 153 154 155
        if solver_name is not None:
          solver_file_path = "%s.%s" % (solver_module.__name__,
                                        solver_name)
          __import__(solver_file_path)
          solver_file = getattr(solver_module, solver_name)
          solver_class = getattr(solver_file, solver_name)
156 157
          solver = solver_class(additional_parameters=additional_parameters,
              **kw)
158

159
          if movement:
160
            result = solver.solveMovement(document)
161
          if delivery:
162 163 164
            result = solver.solveDelivery(document)
        solve_result.append(result)
      return solve_result
165

Jean-Paul Smets's avatar
Jean-Paul Smets committed
166 167
    #######################################################
    # Stock Management
168

169
    def _generatePropertyUidList(self, prop, as_text=0):
170 171 172 173 174 175 176
      """
      converts relative_url or text (single element or list or dict)
        to an object usable by buildSQLQuery

      as_text == 0: tries to lookup an uid from the relative_url
      as_text == 1: directly passes the argument as text
      """
177
      if prop is None :
178 179
        return []
      category_tool = getToolByName(self, 'portal_categories')
180
      property_uid_list = []
181
      if isinstance(prop, str):
182
        if not as_text:
183
          prop_value = category_tool.getCategoryValue(prop)
184
          if prop_value is None:
185
            raise ValueError, 'Category %s does not exists' % prop
186
          property_uid_list.append(prop_value.getUid())
187
        else:
188 189 190
          property_uid_list.append(prop)
      elif isinstance(prop, (list, tuple)):
        for property_item in prop :
191
          if not as_text:
192 193 194 195
            prop_value = category_tool.getCategoryValue(property_item)
            if prop_value is None:
              raise ValueError, 'Category %s does not exists' % property_item
            property_uid_list.append(prop_value.getUid())
196 197
          else:
            property_uid_list.append(property_item)
198
      elif isinstance(prop, dict):
199
        tmp_uid_list = []
200 201 202
        if isinstance(prop['query'], str):
          prop['query'] = [prop['query']]
        for property_item in prop['query'] :
203
          if not as_text:
204 205 206 207
            prop_value = category_tool.getCategoryValue(property_item)
            if prop_value is None:
              raise ValueError, 'Category %s does not exists' % property_item
            tmp_uid_list.append(prop_value.getUid())
208 209
          else:
            tmp_uid_list.append(property_item)
210
        if tmp_uid_list:
211
          property_uid_list = {}
212
          property_uid_list['operator'] = prop['operator']
213 214 215
          property_uid_list['query'] = tmp_uid_list
      return property_uid_list

216 217
    def _getSimulationStateQuery(self, **kw):
      simulation_state_dict = self._getSimulationStateDict(**kw)
218
      return self._buildSimulationStateQuery(simulation_state_dict=simulation_state_dict)
Vincent Pelletier's avatar
Vincent Pelletier committed
219

220
    def _buildSimulationStateQuery(self, simulation_state_dict, table='stock'):
221 222 223 224 225 226 227
      input_simulation_state = simulation_state_dict.get(
                                 'input_simulation_state')
      output_simulation_state = simulation_state_dict.get(
                                 'output_simulation_state')
      simulation_state = simulation_state_dict.get('simulation_state')
      if simulation_state is not None:
        simulation_query = Query(operator='IN',
228
                                 **{'%s.simulation_state' % (table, ):
229 230
                                    simulation_state})
      elif input_simulation_state is not None:
231 232
        input_quantity_query = ComplexQuery(
                        ComplexQuery(
233
                            Query(**{'%s.quantity' % table: '>=0'}),
234 235 236 237 238 239 240
                            Query(**{'%s.is_cancellation' % table: 0}),
                            operator='AND'),
                        ComplexQuery(
                            Query(**{'%s.quantity' % table: '<0'}),
                            Query(**{'%s.is_cancellation' % table: 1}),
                            operator='AND'),
                        operator='OR')
241
        input_simulation_query = Query(operator='IN',
242
                                       **{'%s.simulation_state' % (table, ):
243 244 245 246 247
                                          input_simulation_state})
        simulation_query = ComplexQuery(input_quantity_query,
                                        input_simulation_query,
                                        operator='AND')
        if output_simulation_state is not None:
248 249 250 251 252 253
          output_quantity_query = ComplexQuery(
                        ComplexQuery(
                            Query(**{'%s.quantity' % table: '<0'}),
                            Query(**{'%s.is_cancellation' % table: 0}),
                            operator='AND'),
                        ComplexQuery(
254
                            Query(**{'%s.quantity' % table: '>=0'}),
255 256 257
                            Query(**{'%s.is_cancellation' % table: 1}),
                            operator='AND'),
                        operator='OR')
258
          output_simulation_query = Query(operator='IN',
259
                                          **{'%s.simulation_state' % (table, ):
260 261 262 263 264 265 266
                                             output_simulation_state})
          output_query = ComplexQuery(output_quantity_query,
                                      output_simulation_query,
                                      operator='AND')
          simulation_query = ComplexQuery(simulation_query, output_query,
                                          operator='OR')
      else:
267
        simulation_query = None
268 269 270
      return simulation_query

    def _getSimulationStateDict(self, simulation_state=None, omit_transit=0,
271 272 273 274 275 276 277 278 279 280 281 282
                                input_simulation_state=None,
                                output_simulation_state=None,
                                transit_simulation_state=None,
                                strict_simulation_state=None):
      """
      This method is used in order to give what should be
      the input_simulation_state or output_simulation_state
      depending on many parameters
      """
      string_or_list = (str, list, tuple)
      # Simulation States
      # If strict_simulation_state is set, we directly put it into the dictionary
283
      simulation_dict = {}
284 285 286
      if strict_simulation_state:
        if isinstance(simulation_state, string_or_list)\
                and simulation_state:
287 288
           simulation_query = Query(
                   **{'stock.simulation_state': simulation_state})
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
      else:
        # first, we evaluate simulation_state
        sql_kw = {}
        if simulation_state and isinstance(simulation_state, string_or_list):
          if isinstance(simulation_state, str):
            sql_kw['input_simulation_state'] = [simulation_state]
            sql_kw['output_simulation_state'] = [simulation_state]
          else:
            sql_kw['input_simulation_state'] = simulation_state
            sql_kw['output_simulation_state'] = simulation_state
        # then, if omit_transit == 1, we evaluate (simulation_state -
        # transit_simulation_state) for input_simulation_state
        if omit_transit:
          if isinstance(simulation_state, string_or_list)\
                and simulation_state:
            if isinstance(transit_simulation_state, string_or_list)\
                  and transit_simulation_state:
              # when we know both are usable, we try to calculate
              # (simulation_state - transit_simulation_state)
              if isinstance(simulation_state, str):
                simulation_state = [simulation_state]
              if isinstance(transit_simulation_state, str) :
                transit_simulation_state = [transit_simulation_state]
              delivered_simulation_state_list = []
              for state in simulation_state :
                if state not in transit_simulation_state :
                  delivered_simulation_state_list.append(state)
              sql_kw['input_simulation_state'] = delivered_simulation_state_list

        # alternatively, the user can directly define input_simulation_state
        # and output_simulation_state
        if input_simulation_state and isinstance(input_simulation_state,
                                                  string_or_list):
          if isinstance(input_simulation_state, str):
            input_simulation_state = [input_simulation_state]
          sql_kw['input_simulation_state'] = input_simulation_state
        if output_simulation_state and isinstance(output_simulation_state,
                                                  string_or_list):
          if isinstance(output_simulation_state, str):
            output_simulation_state = [output_simulation_state]
          sql_kw['output_simulation_state'] = output_simulation_state
        # XXX In this case, we must not set sql_kw[input_simumlation_state] before
        input_simulation_state = None
        output_simulation_state = None
        if sql_kw.has_key('input_simulation_state'):
          input_simulation_state = sql_kw.get('input_simulation_state')
        if sql_kw.has_key('output_simulation_state'):
          output_simulation_state = sql_kw.get('output_simulation_state')
        if input_simulation_state is not None \
           or output_simulation_state is not None:
          sql_kw.pop('input_simulation_state',None)
          sql_kw.pop('output_simulation_state',None)
        if input_simulation_state is not None:
          if output_simulation_state is not None:
            if input_simulation_state == output_simulation_state:
344
              simulation_dict['simulation_state'] = input_simulation_state
345
            else:
346 347
              simulation_dict['input_simulation_state'] = input_simulation_state
              simulation_dict['output_simulation_state'] = output_simulation_state
348
          else:
349
            simulation_dict['input_simulation_state'] = input_simulation_state
350
        elif output_simulation_state is not None:
351 352
          simulation_dict['simulation_state'] = output_simulation_state
      return simulation_dict
353

354 355 356 357 358 359
    def _getOmitQuery(self, query_table=None, omit_input=0, omit_output=0,
                      omit_asset_increase=0, omit_asset_decrease=0, **kw):
      omit_dict = self._getOmitDict(omit_input=omit_input,
                                    omit_output=omit_output,
                                    omit_asset_increase=omit_asset_increase,
                                    omit_asset_decrease=omit_asset_decrease)
360
      return self._buildOmitQuery(query_table=query_table, omit_dict=omit_dict)
Vincent Pelletier's avatar
Vincent Pelletier committed
361

362
    def _buildOmitQuery(self, query_table, omit_dict):
363 364 365 366
      """
      Build a specific query in order to take:
      - negatives quantity values if omit_input
      - postives quantity values if omit_output
367 368
      - negatives asset price values if omit_asset_increase
      - positives asset price values if omit_asset_decrease
369 370
      """
      omit_query = None
371 372
      omit_input = omit_dict.get('input', False)
      omit_output = omit_dict.get('output', False)
373 374 375 376
      omit_asset_increase = omit_dict.get('asset_increase', False)
      omit_asset_decrease = omit_dict.get('asset_decrease', False)
      if omit_input or omit_output\
          or omit_asset_increase or omit_asset_decrease:
377
        # Make sure to check some conditions
378 379 380 381 382 383 384
        condition_expression = \
          "%(query_table)s.node_uid <> %(query_table)s.mirror_node_uid \
         OR %(query_table)s.section_uid <> %(query_table)s.mirror_section_uid \
         OR %(query_table)s.mirror_node_uid IS NULL \
         OR %(query_table)s.mirror_section_uid IS NULL \
         OR %(query_table)s.payment_uid IS NOT NULL \
           " % {'query_table': query_table}
385
        if omit_input:
386 387 388 389 390 391
          quantity_query = ComplexQuery(
                        ComplexQuery(
                            Query(**{'%s.quantity' % query_table: '<0'}),
                            Query(**{'%s.is_cancellation' % query_table: 0}),
                            operator='AND'),
                        ComplexQuery(
392
                            Query(**{'%s.quantity' % query_table: '>=0'}),
393 394 395
                            Query(**{'%s.is_cancellation' % query_table: 1}),
                            operator='AND'),
                        operator='OR')
396 397 398
          omit_query = ComplexQuery(quantity_query, condition_expression,
                                    operator='AND')
        if omit_output:
399 400
          quantity_query = ComplexQuery(
                        ComplexQuery(
401
                            Query(**{'%s.quantity' % query_table: '>=0'}),
402 403 404 405 406 407 408
                            Query(**{'%s.is_cancellation' % query_table: 0}),
                            operator='AND'),
                        ComplexQuery(
                            Query(**{'%s.quantity' % query_table: '<0'}),
                            Query(**{'%s.is_cancellation' % query_table: 1}),
                            operator='AND'),
                        operator='OR')
409
          output_query = ComplexQuery(quantity_query, condition_expression,
410
                                      operator='AND')
411
          if omit_query is not None:
412
            omit_query = ComplexQuery(omit_query, output_query, operator='AND')
413 414 415 416 417 418 419 420 421 422
          else:
            omit_query = output_query

        if omit_asset_increase:
          asset_price_query = ComplexQuery(
                        ComplexQuery(
                            Query(**{'%s.total_price' % query_table: '<0'}),
                            Query(**{'%s.is_cancellation' % query_table: 0}),
                            operator='AND'),
                        ComplexQuery(
423
                            Query(**{'%s.total_price' % query_table: '>=0'}),
424 425 426 427 428 429 430 431 432 433 434 435 436
                            Query(**{'%s.is_cancellation' % query_table: 1}),
                            operator='AND'),
                        operator='OR')
          asset_increase_query = ComplexQuery(asset_price_query, condition_expression,
                                              operator='AND')
          if omit_query is not None:
            omit_query = ComplexQuery(omit_query, asset_increase_query, operator='AND')
          else:
            omit_query = asset_increase_query

        if omit_asset_decrease:
          asset_price_query = ComplexQuery(
                        ComplexQuery(
437
                            Query(**{'%s.total_price' % query_table: '>=0'}),
438 439 440 441 442 443 444 445 446 447 448 449 450
                            Query(**{'%s.is_cancellation' % query_table: 0}),
                            operator='AND'),
                        ComplexQuery(
                            Query(**{'%s.total_price' % query_table: '<0'}),
                            Query(**{'%s.is_cancellation' % query_table: 1}),
                            operator='AND'),
                        operator='OR')
          asset_decrease_query = ComplexQuery(asset_price_query, condition_expression,
                                              operator='AND')
          if omit_query is not None:
            omit_query = ComplexQuery(omit_query, asset_decrease_query, operator='AND')
          else:
            omit_query = asset_decrease_query
451 452 453

      return omit_query

454 455 456 457 458 459
    def _getOmitDict(self, omit_input=False, omit_output=False,
                     omit_asset_increase=False, omit_asset_decrease=False):
      return { 'input': omit_input,
               'output': omit_output,
               'asset_increase': omit_asset_increase,
               'asset_decrease': omit_asset_decrease, }
460

461
    def _generateSQLKeywordDict(self, table='stock', **kw):
462
        sql_kw, new_kw = self._generateKeywordDict(**kw)
463 464
        return self._generateSQLKeywordDictFromKeywordDict(table=table,
                 sql_kw=sql_kw, new_kw=new_kw)
465

466 467
    def _generateSQLKeywordDictFromKeywordDict(self, table='stock', sql_kw={},
                                               new_kw={}):
468
        ctool = getToolByName(self, 'portal_catalog')
469 470
        sql_kw = sql_kw.copy()
        new_kw = new_kw.copy()
471

472
        # Group-by expression  (eg. group_by=['node_uid'])
473
        group_by = new_kw.pop('group_by', [])
474

475
        # group by from stock table (eg. group_by_node=True)
476
        # prepend table name to avoid ambiguities.
477
        column_group_by = new_kw.pop('column_group_by', [])
478 479
        if column_group_by:
          group_by.extend(['%s.%s' % (table, x) for x in column_group_by])
480 481

        # group by from related keys columns (eg. group_by_node_category=True)
482 483 484
        related_key_group_by = new_kw.pop('related_key_group_by', [])
        if related_key_group_by:
          group_by.extend(['%s_%s' % (table, x) for x in related_key_group_by])
Vincent Pelletier's avatar
Vincent Pelletier committed
485

486 487 488
        # group by involving a related key (eg. group_by=['product_line_uid'])
        related_key_dict_passthrough_group_by = new_kw.get(
                'related_key_dict_passthrough', dict()).pop('group_by', [])
489 490 491
        if isinstance(related_key_dict_passthrough_group_by, basestring):
          related_key_dict_passthrough_group_by = (
                related_key_dict_passthrough_group_by,)
492 493
        group_by.extend(related_key_dict_passthrough_group_by)

494 495 496
        if group_by:
          new_kw['group_by'] = group_by

497 498 499 500
        # select expression
        select_dict = new_kw.get('select_dict', dict())
        related_key_select_expression_list = new_kw.pop(
                'related_key_select_expression_list', [])
501 502 503
        for related_key_select in related_key_select_expression_list:
          select_dict[related_key_select] = '%s_%s' % (table,
                                                       related_key_select)
504 505
        new_kw['select_dict'] = select_dict

506
        # Column values
507 508 509
        column_value_dict = new_kw.pop('column_value_dict', {})
        for key, value in column_value_dict.iteritems():
          new_kw['%s.%s' % (table, key)] = value
510
        # Related keys
511 512 513 514 515 516
        # First, the passthrough (acts as default values)
        for key, value in new_kw.pop('related_key_dict_passthrough', {})\
            .iteritems():
          new_kw[key] = value
        # Second, calculated values
        for key, value in new_kw.pop('related_key_dict', {}).iteritems():
517
          new_kw['%s_%s' % (table, key)] = value
518 519 520 521 522 523 524 525 526
        # Simulation states matched with input and output omission
        def joinQueriesIfNeeded(query_a, query_b, operator):
          if None not in (query_a, query_b):
            return ComplexQuery(query_a, query_b, operator=operator)
          elif query_a is not None:
            return query_a
          return query_b
        def getSimulationQuery(simulation_dict, omit_dict):
          simulation_query = self._buildSimulationStateQuery(
527 528
                               simulation_state_dict=simulation_dict,
                               table=table)
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
          omit_query = self._buildOmitQuery(query_table=table,
                                            omit_dict=omit_dict)
          return joinQueriesIfNeeded(query_a=simulation_query,
                                     query_b=omit_query, operator='AND')
        regular_query = getSimulationQuery(new_kw.pop('simulation_dict', {}),
                                           new_kw.pop('omit_dict', {}))
        reserved_kw = new_kw.pop('reserved_kw', None)
        if reserved_kw is not None:
          reserved_query = getSimulationQuery(
            reserved_kw.pop('simulation_dict', {}),
            reserved_kw.pop('omit_dict', {}))
          simulation_query = joinQueriesIfNeeded(query_a=regular_query,
                                                 query_b=reserved_query,
                                                 operator='OR')
        else:
          simulation_query = regular_query
        if simulation_query is not None:
          new_kw['query'] = simulation_query
547

548 549
        # Sort on
        if 'sort_on' in new_kw:
550 551
          table_column_list = ctool.getSQLCatalog()._getCatalogSchema(
                                                              table=table)
552 553 554
          sort_on = new_kw['sort_on']
          new_sort_on = []
          for column_id, sort_direction in sort_on:
555
            if column_id in table_column_list:
556 557 558
              column_id = '%s.%s' % (table, column_id)
            new_sort_on.append((column_id, sort_direction))
          new_kw['sort_on'] = tuple(new_sort_on)
Vincent Pelletier's avatar
Vincent Pelletier committed
559

560 561 562 563 564
        # Remove some internal parameters that does not have any meaning for
        # catalog
        new_kw.pop('ignore_group_by', None)
        new_kw.pop('movement_list_mode', None)

565
        sql_kw.update(ctool.buildSQLQuery(**new_kw))
566 567
        return sql_kw

568
    def _generateKeywordDict(self,
569
        # dates
570
        from_date=None, to_date=None, at_date=None,
571
        omit_mirror_date=1,
572
        # instances
Alexandre Boeglin's avatar
Alexandre Boeglin committed
573
        resource=None, node=None, payment=None,
574
        section=None, mirror_section=None, item=None,
575 576
        function=None, project=None, funding=None, payment_request=None,
        transformed_resource=None,
577 578 579
        # used for tracking
        input=0, output=0,
        # categories
Alexandre Boeglin's avatar
Alexandre Boeglin committed
580
        resource_category=None, node_category=None, payment_category=None,
581
        section_category=None, mirror_section_category=None,
582
        function_category=None, project_category=None, funding_category=None,
583
        payment_request_category=None,
584 585 586 587 588 589
        # categories with strict membership
        resource_category_strict_membership=None,
        node_category_strict_membership=None,
        payment_category_strict_membership=None,
        section_category_strict_membership=None,
        mirror_section_category_strict_membership=None,
590 591
        function_category_strict_membership=None,
        project_category_strict_membership=None,
592
        funding_category_strict_membership=None,
593
        payment_request_category_strict_membership=None,
594
        # simulation_state
595
        strict_simulation_state=0,
Alexandre Boeglin's avatar
Alexandre Boeglin committed
596
        simulation_state=None, transit_simulation_state = None, omit_transit=0,
597
        input_simulation_state=None, output_simulation_state=None,
598
        reserved_kw=None,
599
        # variations
600
        variation_text=None, sub_variation_text=None,
601
        variation_category=None,
602
        transformed_variation_text=None,
603
        # uids
604
        resource_uid=None, node_uid=None, section_uid=None, payment_uid=None,
605
        mirror_node_uid=None, mirror_section_uid=None, function_uid=None,
606
        project_uid=None, funding_uid=None, payment_request_uid=None,
607 608 609
        # omit input and output
        omit_input=0,
        omit_output=0,
610 611
        omit_asset_increase=0,
        omit_asset_decrease=0,
612 613
        # group by
        group_by_node=0,
614 615
        group_by_node_category=0,
        group_by_node_category_strict_membership=0,
616
        group_by_mirror_node=0,
617 618
        group_by_mirror_node_category=0,
        group_by_mirror_node_category_strict_membership=0,
619
        group_by_section=0,
620 621
        group_by_section_category=0,
        group_by_section_category_strict_membership=0,
622
        group_by_mirror_section=0,
623 624
        group_by_mirror_section_category=0,
        group_by_mirror_section_category_strict_membership=0,
625
        group_by_payment=0,
626 627
        group_by_payment_category=0,
        group_by_payment_category_strict_membership=0,
628 629 630
        group_by_sub_variation=0,
        group_by_variation=0,
        group_by_movement=0,
631
        group_by_resource=0,
632
        group_by_project=0,
633 634
        group_by_project_category=0,
        group_by_project_category_strict_membership=0,
635 636 637
        group_by_funding=0,
        group_by_funding_category=0,
        group_by_funding_category_strict_membership=0,
638 639 640
        group_by_payment_request=0,
        group_by_payment_request_category=0,
        group_by_payment_request_category_strict_membership=0,
641
        group_by_function=0,
642 643
        group_by_function_category=0,
        group_by_function_category_strict_membership=0,
644
        group_by_date=0,
645 646
        # sort_on
        sort_on=None,
647 648
        # keywords for related keys
        **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
649
      """
650 651 652 653
      Generates keywords and calls buildSQLQuery

      - omit_mirror_date: normally, date's parameters are only based on date
        column. If 0, it also used the mirror_date column.
Alexandre Boeglin's avatar
Alexandre Boeglin committed
654 655 656 657
      """
      new_kw = {}
      sql_kw = {}

658 659 660
      # input and output are used by getTrackingList
      sql_kw['input'] = input
      sql_kw['output'] = output
661 662 663
      # Add sort_on parameter if defined
      if sort_on is not None:
        new_kw['sort_on'] = sort_on
664

665 666
      class DictMixIn(dict):
        def set(dictionary, key, value):
667
          result = bool(value)
668 669 670 671 672 673 674 675 676
          if result:
            dictionary[key] = value
          return result

        def setUIDList(dictionary, key, value, as_text=0):
          uid_list = self._generatePropertyUidList(value, as_text=as_text)
          return dictionary.set(key, uid_list)

      column_value_dict = DictMixIn()
677 678

      if omit_mirror_date:
679
        date_dict = {}
680
        if from_date :
681
          date_dict.setdefault('query', []).append(from_date)
682 683
          date_dict['range'] = 'min'
          if to_date :
684
            date_dict.setdefault('query', []).append(to_date)
685 686
            date_dict['range'] = 'minmax'
          elif at_date :
687
            date_dict.setdefault('query', []).append(at_date)
688 689
            date_dict['range'] = 'minngt'
        elif to_date :
690
          date_dict.setdefault('query', []).append(to_date)
691
          date_dict['range'] = 'max'
692
        elif at_date :
693
          date_dict.setdefault('query', []).append(at_date)
694
          date_dict['range'] = 'ngt'
695 696
        if date_dict:
          column_value_dict['date'] = date_dict
697
      else:
698 699
        column_value_dict['date'] = {'query': [to_date], 'range': 'ngt'}
        column_value_dict['mirror_date'] = {'query': [from_date], 'range': 'nlt'}
700

701
      column_value_dict.set('resource_uid', resource_uid)
702
      column_value_dict.set('payment_uid', payment_uid)
703
      column_value_dict.set('project_uid', project_uid)
704
      column_value_dict.set('funding_uid', funding_uid)
705
      column_value_dict.set('payment_request_uid', payment_request_uid)
706
      column_value_dict.set('function_uid', function_uid)
707
      if column_value_dict.set('section_uid', section_uid):
708
        sql_kw['section_filtered'] = 1
709
      column_value_dict.set('node_uid', node_uid)
710 711
      column_value_dict.set('mirror_node_uid', mirror_node_uid)
      column_value_dict.set('mirror_section_uid', mirror_section_uid)
712 713 714 715
      column_value_dict.setUIDList('resource_uid', resource)
      column_value_dict.setUIDList('aggregate_uid', item)
      column_value_dict.setUIDList('node_uid', node)
      column_value_dict.setUIDList('payment_uid', payment)
716
      column_value_dict.setUIDList('project_uid', project)
717
      column_value_dict.setUIDList('funding_uid', funding)
718
      column_value_dict.setUIDList('payment_request_uid', payment_request)
719
      column_value_dict.setUIDList('function_uid', function)
720 721 722

      sql_kw['transformed_uid'] = self._generatePropertyUidList(transformed_resource)

723
      if column_value_dict.setUIDList('section_uid', section):
724
        sql_kw['section_filtered'] = 1
725 726 727 728 729 730 731 732
      column_value_dict.setUIDList('mirror_section_uid', mirror_section)
      column_value_dict.setUIDList('variation_text', variation_text,
                                   as_text=1)
      column_value_dict.setUIDList('sub_variation_text', sub_variation_text,
                                   as_text=1)
      new_kw['column_value_dict'] = column_value_dict.copy()

      related_key_dict = DictMixIn()
733
      # category membership
734 735
      related_key_dict.setUIDList('resource_category_uid', resource_category)
      related_key_dict.setUIDList('node_category_uid', node_category)
736
      related_key_dict.setUIDList('project_category_uid', project_category)
737
      related_key_dict.setUIDList('funding_category_uid', funding_category)
738
      related_key_dict.setUIDList('payment_request_category_uid', payment_request_category)
739
      related_key_dict.setUIDList('function_category_uid', function_category)
740 741 742
      related_key_dict.setUIDList('payment_category_uid', payment_category)
      if related_key_dict.setUIDList('section_category_uid',
                                     section_category):
743
        sql_kw['section_filtered'] = 1
744 745
      related_key_dict.setUIDList('mirror_section_category_uid',
                                  mirror_section_category)
746
      # category strict membership
747 748 749 750
      related_key_dict.setUIDList('resource_category_strict_membership_uid',
                                  resource_category_strict_membership)
      related_key_dict.setUIDList('node_category_strict_membership_uid',
                                  node_category_strict_membership)
751 752
      related_key_dict.setUIDList('project_category_strict_membership_uid',
                                  project_category_strict_membership)
753 754
      related_key_dict.setUIDList('funding_category_strict_membership_uid',
                                  funding_category_strict_membership)
755 756
      related_key_dict.setUIDList('payment_request_category_strict_membership_uid',
                                  payment_request_category_strict_membership)
757 758
      related_key_dict.setUIDList('function_category_strict_membership_uid',
                                  function_category_strict_membership)
759 760 761 762
      related_key_dict.setUIDList('payment_category_strict_membership_uid',
                                  payment_category_strict_membership)
      if related_key_dict.setUIDList('section_category_strict_membership_uid',
                                     section_category_strict_membership):
763
        sql_kw['section_filtered'] = 1
764 765 766
      related_key_dict.setUIDList(
        'mirror_section_category_strict_membership_uid',
        mirror_section_category_strict_membership)
Vincent Pelletier's avatar
Vincent Pelletier committed
767

768
      new_kw['related_key_dict'] = related_key_dict.copy()
769
      new_kw['related_key_dict_passthrough'] = kw
770

771 772 773
      #variation_category_uid_list = self._generatePropertyUidList(variation_category)
      #if len(variation_category_uid_list) :
      #  new_kw['variationCategory'] = variation_category_uid_list
Vincent Pelletier's avatar
Vincent Pelletier committed
774

775
      simulation_dict =  self._getSimulationStateDict(
Vincent Pelletier's avatar
Vincent Pelletier committed
776
                                simulation_state=simulation_state,
777 778 779 780 781
                                omit_transit=omit_transit,
                                input_simulation_state=input_simulation_state,
                                output_simulation_state=output_simulation_state,
                                transit_simulation_state=transit_simulation_state,
                                strict_simulation_state=strict_simulation_state)
782 783
      new_kw['simulation_dict'] = simulation_dict
      omit_dict = self._getOmitDict(omit_input=omit_input,
784 785 786
                                    omit_output=omit_output,
                                    omit_asset_increase=omit_asset_increase,
                                    omit_asset_decrease=omit_asset_decrease)
787
      new_kw['omit_dict'] = omit_dict
788
      if reserved_kw is not None:
789
        if not isinstance(reserved_kw, dict):
Vincent Pelletier's avatar
Vincent Pelletier committed
790
          # Not a dict when taken from URL, so, cast is needed
791 792
          # to make pop method available
          reserved_kw = dict(reserved_kw)
793
        new_reserved_kw = {}
794 795
        reserved_omit_input = reserved_kw.pop('omit_input',0)
        reserved_omit_output = reserved_kw.pop('omit_output',0)
796 797 798 799 800
        new_reserved_kw['omit_dict'] = self._getOmitDict(
                                         omit_input=reserved_omit_input,
                                         omit_output=reserved_omit_output)
        new_reserved_kw['simulation_dict'] = self._getSimulationStateDict(**reserved_kw)
        new_kw['reserved_kw'] = new_reserved_kw
801 802 803 804 805 806 807
      # It is necessary to use here another SQL query (or at least a subquery)
      # to get _DISTINCT_ uid from predicate_category table.
      # Otherwise, by using a where_expression, cells which fit conditions
      # more than one time are counted more than one time, and the resulting
      # inventory is false
      # XXX Perhaps is there a better solution
      add_kw = {}
808
      if variation_category is not None and variation_category:
809 810
        where_expression = self.getPortalObject().portal_categories\
          .buildSQLSelector(
811 812 813 814 815 816 817 818 819 820
            category_list = variation_category,
            query_table = 'predicate_category')
        if where_expression != '':
          add_kw['where_expression'] = where_expression
          add_kw['predicate_category.uid'] = '!=NULL'
          add_kw['group_by_expression'] = 'uid'
          add_query = self.portal_catalog(**add_kw)
          uid_list = []
          for line in add_query:
            uid_list.append(line.uid)
821 822
          new_kw['where_expression'] = '( %s )' % ' OR '.join(
                      ['catalog.uid=%s' % uid for uid in uid_list])
Alexandre Boeglin's avatar
Alexandre Boeglin committed
823

Sebastien Robin's avatar
Sebastien Robin committed
824
      # build the group by expression
825 826 827 828 829 830 831 832 833 834 835 836
      # if we group by a criterion, we also add this criterion to the select
      # expression, unless it is already selected in Resource_zGetInventoryList
      # the caller can also pass select_dict or select_list. select_expression,
      # which is deprecated in ZSQLCatalog is not supported here.
      select_dict = kw.get('select_dict', {})
      # we support select_list, if passed
      select_list = kw.get('select_list', [])
      for select_key in kw.get('select_list', []):
        select_dict[select_key] = None
      new_kw['select_dict'] = select_dict
      related_key_select_expression_list = []

837 838
      column_group_by_expression_list = []
      related_key_group_by_expression_list = []
839
      if group_by_node:
840
        column_group_by_expression_list.append('node_uid')
841
      if group_by_mirror_node:
842
        column_group_by_expression_list.append('mirror_node_uid')
843
      if group_by_section:
844
        column_group_by_expression_list.append('section_uid')
845
      if group_by_mirror_section:
846
        column_group_by_expression_list.append('mirror_section_uid')
847
      if group_by_payment:
848
        column_group_by_expression_list.append('payment_uid')
849
      if group_by_sub_variation:
850
        column_group_by_expression_list.append('sub_variation_text')
851
      if group_by_variation:
852
        column_group_by_expression_list.append('variation_text')
853
      if group_by_movement:
854
        column_group_by_expression_list.append('uid')
855
      if group_by_resource:
856 857 858
        column_group_by_expression_list.append('resource_uid')
      if group_by_project:
        column_group_by_expression_list.append('project_uid')
859 860
      if group_by_funding:
        column_group_by_expression_list.append('funding_uid')
861 862
      if group_by_payment_request:
        column_group_by_expression_list.append('payment_request_uid')
863 864
      if group_by_function:
        column_group_by_expression_list.append('function_uid')
865
      if group_by_date:
866 867 868 869 870 871 872
        column_group_by_expression_list.append('date')

      if column_group_by_expression_list:
        new_kw['column_group_by'] = column_group_by_expression_list

      if group_by_section_category:
        related_key_group_by_expression_list.append('section_category_uid')
873
        related_key_select_expression_list.append('section_category_uid')
874 875 876
      if group_by_section_category_strict_membership:
        related_key_group_by_expression_list.append(
            'section_category_strict_membership_uid')
877 878
        related_key_select_expression_list.append(
            'section_category_strict_membership_uid')
879 880
      if group_by_mirror_section_category:
        related_key_group_by_expression_list.append('mirror_section_category_uid')
881
        related_key_select_expression_list.append('mirror_section_category_uid')
882 883 884
      if group_by_mirror_section_category_strict_membership:
        related_key_group_by_expression_list.append(
            'mirror_section_category_strict_membership_uid')
885 886
        related_key_select_expression_list.append(
            'mirror_section_category_strict_membership_uid')
887 888
      if group_by_node_category:
        related_key_group_by_expression_list.append('node_category_uid')
889
        related_key_select_expression_list.append('node_category_uid')
890 891 892
      if group_by_node_category_strict_membership:
        related_key_group_by_expression_list.append(
            'node_category_strict_membership_uid')
893 894
        related_key_select_expression_list.append(
            'node_category_strict_membership_uid')
895 896 897 898 899
      if group_by_mirror_node_category:
        related_key_group_by_expression_list.append('mirror_node_category_uid')
      if group_by_mirror_node_category_strict_membership:
        related_key_group_by_expression_list.append(
            'mirror_node_category_strict_membership_uid')
900 901
        related_key_select_expression_list.append(
            'mirror_node_category_strict_membership_uid')
902 903
      if group_by_payment_category:
        related_key_group_by_expression_list.append('payment_category_uid')
904
        related_key_select_expression_list.append('payment_category_uid')
905 906 907
      if group_by_payment_category_strict_membership:
        related_key_group_by_expression_list.append(
            'payment_category_strict_membership_uid')
908 909
        related_key_select_expression_list.append(
            'payment_category_strict_membership_uid')
910 911
      if group_by_function_category:
        related_key_group_by_expression_list.append('function_category_uid')
912
        related_key_select_expression_list.append('function_category_uid')
913 914 915
      if group_by_function_category_strict_membership:
        related_key_group_by_expression_list.append(
            'function_category_strict_membership_uid')
916
        related_key_select_expression_list.append(
917
            'function_category_strict_membership_uid')
918 919 920 921 922 923 924 925
      if group_by_project_category:
        related_key_group_by_expression_list.append('project_category_uid')
        related_key_select_expression_list.append('project_category_uid')
      if group_by_project_category_strict_membership:
        related_key_group_by_expression_list.append(
            'project_category_strict_membership_uid')
        related_key_select_expression_list.append(
            'project_category_strict_membership_uid')
926 927 928 929 930 931 932 933
      if group_by_funding_category:
        related_key_group_by_expression_list.append('funding_category_uid')
        related_key_select_expression_list.append('funding_category_uid')
      if group_by_funding_category_strict_membership:
        related_key_group_by_expression_list.append(
            'funding_category_strict_membership_uid')
        related_key_select_expression_list.append(
            'funding_category_strict_membership_uid')
934 935 936 937 938 939 940 941
      if group_by_payment_category:
        related_key_group_by_expression_list.append('payment_request_category_uid')
        related_key_select_expression_list.append('payment_request_category_uid')
      if group_by_payment_request_category_strict_membership:
        related_key_group_by_expression_list.append(
            'payment_request_category_strict_membership_uid')
        related_key_select_expression_list.append(
            'payment_request_category_strict_membership_uid')
942

943 944
      if related_key_group_by_expression_list:
        new_kw['related_key_group_by'] = related_key_group_by_expression_list
945 946 947
      if related_key_select_expression_list:
        new_kw['related_key_select_expression_list'] =\
                related_key_select_expression_list
948 949

      return sql_kw, new_kw
950

Jean-Paul Smets's avatar
Jean-Paul Smets committed
951
    #######################################################
952 953
    # Inventory management
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
954
                              'getInventory')
955
    def getInventory(self, src__=0, simulation_period='', **kw):
956
      """
957 958
      Returns an inventory of a single or multiple resources on a single or
      multiple nodes as a single float value
959

960
      from_date (>=) - only take rows which date is >= from_date
961

962
      to_date   (<)  - only take rows which date is < to_date
963 964 965 966 967

      at_date   (<=) - only take rows which date is <= at_date

      resource (only in generic API in simulation)

968 969
      node           -  only take rows in stock table which node_uid is
                        equivalent to node
970

971 972
      payment        -  only take rows in stock table which payment_uid is
                        equivalent to payment
973

974 975
      section        -  only take rows in stock table which section_uid is
                        equivalent to section
976

977 978
      mirror_section -  only take rows in stock table which mirror_section_uid is
                        mirror_section
979

980 981
      resource_category  -  only take rows in stock table which
                        resource_uid is member of resource_category
982

983 984
      node_category   - only take rows in stock table which node_uid is
                        member of section_category
985

986 987
      payment_category   -  only take rows in stock table which payment_uid
                            is in section_category
988

989
      section_category -  only take rows in stock table which section_uid is
990 991
                          member of section_category

Vincent Pelletier's avatar
Vincent Pelletier committed
992
      mirror_section_category - only take rows in stock table which
993
                                mirror_section_uid is member of
994
                                mirror_section_category
995 996 997 998 999 1000

      node_filter     - only take rows in stock table which node_uid
                        matches node_filter

      payment_filter  - only take rows in stock table which payment_uid
                        matches payment_filter
1001

1002 1003 1004
      section_filter  - only take rows in stock table which section_uid
                        matches section_filter

1005 1006
      mirror_section_filter - only take rows in stock table which
                              mirror_section_uid matches mirror_section_filter
1007

1008 1009 1010 1011 1012 1013
      variation_text -  only take rows in stock table with specified
                        variation_text.
                        This needs to be extended with some kind of
                        variation_category ?
                        XXX this way of implementing variation selection is far
                        from perfect
1014

1015 1016
      sub_variation_text - only take rows in stock table with specified
                        variation_text
1017

1018 1019
      variation_category - variation or list of possible variations (it is not
                        a cross-search ; SQL query uses OR)
1020 1021 1022

      simulation_state - only take rows with specified simulation_state

1023
      transit_simulation_state - specifies which states are transit states
1024

1025
      omit_transit   -  do not evaluate transit_simulation_state
1026

1027 1028
      input_simulation_state - only take rows with specified simulation_state
                        and quantity > 0
1029

1030 1031
      output_simulation_state - only take rows with specified simulation_state
                        and quantity < 0
1032

1033 1034 1035
      ignore_variation -  do not take into account variation in inventory
                        calculation (useless on getInventory, but useful on
                        getInventoryList)
1036

1037 1038
      standardise    -  provide a standard quantity rather than an SKU (XXX
                        not implemented yet)
1039

1040 1041
      omit_simulation - doesn't take into account simulation movements

1042 1043 1044 1045
      only_accountable - Only take into account accountable movements. By
                        default, only movements for which isAccountable() is
                        true will be taken into account.

1046 1047 1048 1049 1050
      omit_input     -  doesn't take into account movement with quantity > 0

      omit_output    -  doesn't take into account movement with quantity < 0

      omit_asset_increase - doesn't take into account movement with asset_price > 0
1051

1052
      omit_asset_decrease - doesn't take into account movement with asset_price < 0
1053 1054 1055

      selection_domain, selection_report - see ListBox

1056 1057
      group_by_variation - (useless on getInventory, but useful on
                        getInventoryList)
Sebastien Robin's avatar
Sebastien Robin committed
1058

1059 1060
      group_by_node  -  (useless on getInventory, but useful on
                        getInventoryList)
Sebastien Robin's avatar
Sebastien Robin committed
1061

1062 1063
      group_by_mirror_node - (useless on getInventory, but useful on
                        getInventoryList)
Sebastien Robin's avatar
Sebastien Robin committed
1064

1065 1066
      group_by_sub_variation - (useless on getInventory, but useful on
                        getInventoryList)
1067

1068 1069 1070
      group_by_movement - (useless on getInventory, but useful on
                        getInventoryList)

1071 1072
      precision - the precision used to round quantities and prices.

1073 1074 1075 1076
      metric_type   - convert the results to a specific metric_type

      quantity_unit - display results using this specific quantity unit

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
      transformed_resource - one, or several resources. list resources that can
                             be produced using those resources as input in a
                             transformation.
                             relative_resource_url for each returned line will
                             point to the transformed resource, while the stock
                             will be the stock of the produced resource,
                             expressed in number of transformed resources.
      transformed_variation_text - to be used with transformed_resource, to
                                   to refine the transformation selection only
                                   to those using variated resources as input.

1088 1089
      **kw           -  if we want extended selection with more keywords (but
                        bad performance) check what we can do with
1090
                        buildSQLQuery
1091

1092 1093
      NOTE: we may want to define a parameter so that we can select the kind of
      inventory statistics we want to display (ex. sum, average, cost, etc.)
1094
      """
1095 1096 1097 1098 1099 1100 1101 1102
      # JPS: this is a hint for implementation of xxx_filter arguments
      # node_uid_count = portal_catalog.countResults(**node_filter)
      # if node_uid_count not too big:
      #   node_uid_list = cache(portal_catalog(**node_filter))
      #   pass this list to ZSQL method
      # else:
      #   build a table in MySQL
      #   and join that table with the stock table
1103
      method = getattr(self,'get%sInventoryList' % simulation_period)
1104 1105
      kw['ignore_group_by'] = 1
      result = method(inventory_list=0, src__=src__, **kw)
1106
      if src__:
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1107 1108
        return result

1109 1110
      total_result = 0.0
      if len(result) > 0:
1111 1112
        if len(result) != 1:
          raise ValueError, 'Sorry we must have only one'
1113 1114 1115 1116 1117 1118 1119 1120
        result = result[0]

        if hasattr(result, "converted_quantity"):
          total_result = result.converted_quantity
        else:
          inventory = result.total_quantity
          if inventory is not None:
            total_result = inventory
1121 1122

      return total_result
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1123

1124
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1125
                              'getCurrentInventory')
1126
    def getCurrentInventory(self, **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1127 1128 1129
      """
      Returns current inventory
      """
1130
      return self.getInventory(simulation_period='Current', **kw)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1131

1132
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1133
                              'getAvailableInventory')
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1134 1135 1136
    def getAvailableInventory(self, **kw):
      """
      Returns available inventory
1137
      (current inventory - reserved_inventory)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1138
      """
1139
      return self.getInventory(simulation_period='Available', **kw)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1140

1141
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1142
                              'getFutureInventory')
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1143 1144 1145 1146
    def getFutureInventory(self, **kw):
      """
      Returns future inventory
      """
1147
      return self.getInventory(simulation_period='Future', **kw)
Vincent Pelletier's avatar
Vincent Pelletier committed
1148 1149

    def _getDefaultGroupByParameters(self, ignore_group_by=0,
1150 1151
        group_by_node=0, group_by_mirror_node=0,
        group_by_section=0, group_by_mirror_section=0,
1152
        group_by_payment=0, group_by_project=0, group_by_funding=0,
1153
        group_by_function=0,
1154
        group_by_variation=0, group_by_sub_variation=0,
1155
        group_by_movement=0, group_by_date=0,
1156 1157
        group_by_section_category=0,
        group_by_section_category_strict_membership=0,
1158
        group_by_resource=None,
1159
        movement_list_mode=0,
1160
        group_by=None,
1161
        **ignored):
1162 1163
      """
      Set defaults group_by parameters
1164 1165 1166 1167 1168

      If ignore_group_by is true, this function returns an empty dict.

      If any group-by is provided, automatically group by resource aswell
      unless group_by_resource is explicitely set to false.
1169 1170 1171
      If no group by is provided, use the default group by: movement, node and
      resource, unless movement_list_mode is true, in that case, group by
      movement, node, resource and date (this is historically the default in
1172 1173 1174 1175
      getMovementHistoryList), section, mirror_section and payment (this is to
      make sure two lines will appear when we are, for instance both source and
      destination, implementation might not be optimal, because it uses lots of
      group by statements in SQL).
1176
      """
1177
      new_group_by_dict = {}
1178
      if not ignore_group_by and group_by is None:
1179
        if group_by_node or group_by_mirror_node or group_by_section or \
1180
           group_by_project or group_by_funding or group_by_function or \
1181
           group_by_mirror_section or group_by_payment or \
1182
           group_by_sub_variation or group_by_variation or \
1183 1184
           group_by_movement or group_by_date or group_by_section_category or\
           group_by_section_category_strict_membership:
1185 1186
          if group_by_resource is None:
            group_by_resource = 1
1187
          new_group_by_dict['group_by_resource'] = group_by_resource
1188
        elif group_by_resource is None:
1189 1190 1191
          new_group_by_dict['group_by_movement'] = 1
          new_group_by_dict['group_by_node'] = 1
          new_group_by_dict['group_by_resource'] = 1
1192 1193
          if movement_list_mode:
            new_group_by_dict['group_by_date'] = 1
1194 1195 1196 1197
            new_group_by_dict['group_by_mirror_node'] = 1
            new_group_by_dict['group_by_section'] = 1
            new_group_by_dict['group_by_mirror_section'] = 1
            new_group_by_dict['group_by_payment'] = 1
1198
      return new_group_by_dict
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1199

1200
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1201
                              'getInventoryList')
1202 1203
    def getInventoryList(self, src__=0, optimisation__=True,
                         ignore_variation=0, standardise=0,
Vincent Pelletier's avatar
Vincent Pelletier committed
1204
                         omit_simulation=0,
1205
                         only_accountable=True,
1206
                         default_stock_table='stock',
1207
                         selection_domain=None, selection_report=None,
Vincent Pelletier's avatar
Vincent Pelletier committed
1208
                         statistic=0, inventory_list=1,
1209
                         precision=None, connection_id=None,
1210
                         **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1211
      """
1212 1213 1214
        Returns a list of inventories for a single or multiple
        resources on a single or multiple nodes, grouped by resource,
        node, section, etc. Every line defines an inventory value for
Romain Courteaud's avatar
Romain Courteaud committed
1215
        a given group of resource, node, section.
1216 1217
        NOTE: we may want to define a parameter so that we can select
        the kind of inventory statistics we want to display (ex. sum,
Romain Courteaud's avatar
Romain Courteaud committed
1218
        average, cost, etc.)
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

        Optimisation queries.
        Optimisation of a stock lookup is done to avoid a table scan
        of all lines corresponding to a given node, section or payment,
        because they grow with time and query time should not.
        First query: Fetch fitting full inventory dates.
          For each node, section or payment, find the first anterior full
          inventory.
        Second query: Fetch full inventory amounts.
          Fetch values of inventory identified in the first query.
1229
        Third query: Classic stock table read.
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
          Fetch all rows in stock table which are posterior to the inventory.
        Final result
          Add results of the second and third queries, and return it.

        Missing optimisations:
         - In a getInventory case where everything is specified for the
           resource, it's not required for the inventory to be full, it
           just need to be done for the right resource.
           If the resource isn't completely defined, we require inventory
           to be full, which is implemented.
         - Querying multiple nodes/categories/payments in one call prevents
           from using optimisation, it should be equivalent to multiple calls
           on individual nodes/categories/payments.
Vincent Pelletier's avatar
Vincent Pelletier committed
1243
         -
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1244
      """
1245 1246 1247 1248
      getCategory = self.getPortalObject().portal_categories.getCategoryUid

      result_column_id_dict = {}

1249
      metric_type = kw.pop('metric_type', None)
1250 1251
      quantity_unit = kw.pop('quantity_unit', None)
      quantity_unit_uid = None
1252

1253 1254 1255 1256 1257 1258
      if quantity_unit is not None:

        if isinstance(quantity_unit, str):
          quantity_unit_uid = getCategory(quantity_unit, 'quantity_unit')
          if quantity_unit_uid is not None:
            result_column_id_dict['converted_quantity'] = None
1259 1260 1261
            if metric_type is None:
              # use the default metric type
              metric_type = quantity_unit.split("/", 1)[0]
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
1262
        elif isinstance(quantity_unit, (int, float)):
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
          # quantity_unit used to be a numerical parameter..
          raise ValueError('Numeric values for quantity_unit are not supported')


      convert_quantity_result = False
      if metric_type is not None:
        metric_type_uid = getCategory(metric_type, 'metric_type')

        if metric_type_uid is not None:
          convert_quantity_result = True
          kw['metric_type_uid'] = Query(
                                    metric_type_uid=metric_type_uid,
                                    table_alias_list=(("measure", "measure"),))

1277 1278
      if src__:
        sql_source_list = []
1279
      # If no group at all, give a default sort group by
1280
      kw.update(self._getDefaultGroupByParameters(**kw))
1281 1282
      sql_kw, new_kw = self._generateKeywordDict(**kw)
      stock_sql_kw = self._generateSQLKeywordDictFromKeywordDict(
1283
                       table=default_stock_table, sql_kw=sql_kw, new_kw=new_kw)
1284 1285
      Resource_zGetFullInventoryDate = \
        getattr(self, 'Resource_zGetFullInventoryDate', None)
1286 1287
      EQUAL_DATE_TABLE_ID = 'inventory_stock'
      GREATER_THAN_DATE_TABLE_ID = 'stock'
1288
      buildSQLQuery = self.getPortalObject().portal_catalog.buildSQLQuery
1289
      optimisation_success = optimisation__ and ('from_date' not in kw) and \
1290 1291
                             Resource_zGetFullInventoryDate is not None and \
                             (GREATER_THAN_DATE_TABLE_ID == default_stock_table)
1292 1293 1294
      # Generate first query parameter dict
      if optimisation_success:
        def getFirstQueryParameterDict(query_generator_kw):
1295
          optimisation_success = True
1296 1297 1298
          AVAILABLE_CRITERIONS_IN_INVENTORY_TABLE = ['node_uid',
                                                     'section_uid',
                                                     'payment_uid']
1299 1300
          # Only column group by are supported in full inventories
          group_by_list = query_generator_kw.get('column_group_by', [])
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
          column_value_dict = query_generator_kw.get('column_value_dict', {})
          new_group_by_list = []
          new_column_value_dict = {}
          for criterion_id in AVAILABLE_CRITERIONS_IN_INVENTORY_TABLE:
            criterion_value_list = column_value_dict.get(criterion_id, [])
            if not isinstance(criterion_value_list, (list, tuple)):
              criterion_value_list = [criterion_value_list]
            if len(criterion_value_list) > 0:
              if len(criterion_value_list) > 1:
                # Impossible to optimise if there is more than one possible
                # value per criterion.
                optimisation_success = False
                break
              new_column_value_dict[criterion_id] = criterion_value_list
              new_group_by_list.append(criterion_id)
            elif criterion_id in group_by_list:
              new_group_by_list.append(criterion_id)
          group_by_expression = ', '.join(new_group_by_list)
          column_id_list = new_column_value_dict.keys()
          column_value_list_list = new_column_value_dict.values()
          date_value_list = column_value_dict.get('date', {}).get('query', [])
1322
          where_expression = None
1323 1324
          if len(date_value_list) == 1:
            date = date_value_list[0]
1325
            # build a query for date to take range into account
1326
            date_query_result = buildSQLQuery(**{
1327 1328 1329 1330 1331 1332
              'inventory.date': {
                'query': date,
                'range': column_value_dict.get('date', {}).get('range', [])
              },
              'query_table': None,
            })
1333 1334
            if date_query_result['where_expression'] not in ('',None):
              where_expression = date_query_result['where_expression']
1335 1336 1337 1338 1339 1340 1341 1342
          elif len(date_value_list) > 1:
            # When more than one date is provided, we must not optimise.
            # Also, as we should never end up here (the only currently known
            # case where there are 2 dates is when a from_date is provided
            # along with either an at_date or a to_date, and we disable
            # optimisation when from_date is given), emit a log.
            # This can happen if there are more date parameters than mentioned
            # above.
1343
            LOG('SimulationTool', PROBLEM, 'There is more than one date condition'
1344 1345 1346 1347
              ' so optimisation got disabled. The result of this call will be'
              ' correct but it requires investigation as some cases might'
              ' have gone unnoticed and produced wrong results.')
            optimisation_success = False
1348 1349 1350
          return {'group_by_expression': group_by_expression,
                  'column_id_list': column_id_list,
                  'column_value_list_list': column_value_list_list,
1351 1352
                  'where_expression' : where_expression,}, optimisation_success
        first_query_param_dict, optimisation_success = getFirstQueryParameterDict(new_kw)
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
        if optimisation_success:
          if len(first_query_param_dict['column_id_list']):
            inventory_date_line_list = self.Resource_zGetFullInventoryDate(
                                         **first_query_param_dict)
            if src__:
              sql_source_list.append(
                self.Resource_zGetFullInventoryDate(src__=src__,
                  **first_query_param_dict))
            # Check that all expected uids have been found, otherwise a full
            # inventory of a node/section/payment might be missing.
            if len(inventory_date_line_list) >= max([len(x) for x in \
               first_query_param_dict['column_value_list_list']]):
              # Generate a where expression which filters on dates retrieved
              # in the first query to be used in the second query.
              # Also, generate a where expression to use in the third query,
              # since it is based on the same data.
              # XXX: uggly duplicated query generation code
              # XXX: duplicates SQL variable formatting present in
              #      ERP5Type/patches/sqlvar.py about datetime SQL columns.
1372 1373 1374 1375 1376 1377
              # Note: This code can generate queries like:
              #  date = 2000/01/01 and date >= 2001/01/01
              #  When latest full inventory is at 2000/01/01 and given
              #  from_date is 2001/01/01.
              #  It is not a serious problem since MySQL detects incompatible
              #  conditions and immediately returns (with 0 rows).
1378 1379 1380
              
              # get search key definitions from portal_catalog
              ctool = getToolByName(self, 'portal_catalog')
1381
              portal_catalog = ctool.getSQLCatalog()
1382
              keyword_search_keys = list(portal_catalog.sql_catalog_keyword_search_keys)
1383 1384 1385 1386 1387 1388
              datetime_search_keys = list(portal_catalog.sql_catalog_datetime_search_keys)
              full_text_search_keys = list(portal_catalog.sql_catalog_full_text_search_keys)
              search_key_mapping = dict(key_alias_dict = None,
                                        keyword_search_keys = keyword_search_keys,
                                        datetime_search_keys = datetime_search_keys,
                                        full_text_search_keys = full_text_search_keys)
1389 1390 1391 1392 1393 1394 1395
              equal_date_query_list = []
              greater_than_date_query_list = []
              for inventory_date_line_dict in \
                  inventory_date_line_list.dictionaries():
                date = inventory_date_line_dict['date']
                non_date_value_dict = dict([(k, v) for k, v \
                  in inventory_date_line_dict.iteritems() if k != 'date'])
1396

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
                equal_date_query_list.append(
                  ComplexQuery(
                    ComplexQuery(operator='AND',
                      *[Query(**{'%s.%s' % (EQUAL_DATE_TABLE_ID, k): v}) \
                        for k, v in non_date_value_dict.iteritems()]),
                    Query(**{'%s.date' % (EQUAL_DATE_TABLE_ID, ): date}),
                    operator='AND'))
                greater_than_date_query_list.append(
                  ComplexQuery(
                    ComplexQuery(operator='AND',
                      *[Query(**{'%s.%s' % (GREATER_THAN_DATE_TABLE_ID, k): \
                                 v}) \
                        for k, v in non_date_value_dict.iteritems()]),
1410 1411
                    # 'Use explicitly Universal' otherwise DateTime 
                    # search key will convert it to UTC one more time
1412 1413
                    Query(**{'%s.date' % (GREATER_THAN_DATE_TABLE_ID, ): date,
                             'range': 'nlt'}),
1414 1415 1416 1417
                    operator='AND'))
              assert len(equal_date_query_list) == \
                     len(greater_than_date_query_list)
              assert len(equal_date_query_list) > 0
1418 1419
              equal_date_query = buildSQLQuery(query=ComplexQuery(operator='OR', *equal_date_query_list), query_table=None)['where_expression']
              greater_than_date_query = buildSQLQuery(query=ComplexQuery(operator='OR', *greater_than_date_query_list), query_table=None)['where_expression']
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
              inventory_stock_sql_kw = \
                self._generateSQLKeywordDictFromKeywordDict(
                  table=EQUAL_DATE_TABLE_ID, sql_kw=sql_kw, new_kw=new_kw)
              inventory_stock_where_query = \
                inventory_stock_sql_kw.get('where_expression', '(1)')
              assert isinstance(inventory_stock_where_query, basestring) \
                     and len(inventory_stock_where_query)
              inventory_stock_sql_kw['where_expression'] = '(%s) AND (%s)' % \
                (inventory_stock_where_query, equal_date_query)
              where_query = stock_sql_kw.get('where_expression', '(1)')
              assert isinstance(where_query, basestring) and len(where_query)
              stock_sql_kw['where_expression'] = '(%s) AND (%s)' % \
                (where_query, greater_than_date_query)
              # Get initial inventory amount
              initial_inventory_line_list = self.Resource_zGetInventoryList(
1435
                stock_table_id=EQUAL_DATE_TABLE_ID,
1436 1437
                src__=src__, ignore_variation=ignore_variation,
                standardise=standardise, omit_simulation=omit_simulation,
1438
                only_accountable=only_accountable,
1439 1440 1441
                selection_domain=selection_domain,
                selection_report=selection_report, precision=precision,
                inventory_list=inventory_list,
1442 1443 1444
                statistic=statistic,
                quantity_unit_uid=quantity_unit_uid,
                convert_quantity_result=convert_quantity_result,
1445
                **inventory_stock_sql_kw)
1446 1447
              # Get delta inventory
              delta_inventory_line_list = self.Resource_zGetInventoryList(
1448
                stock_table_id=GREATER_THAN_DATE_TABLE_ID,
1449 1450
                src__=src__, ignore_variation=ignore_variation,
                standardise=standardise, omit_simulation=omit_simulation,
1451
                only_accountable=only_accountable,
1452 1453 1454
                selection_domain=selection_domain,
                selection_report=selection_report, precision=precision,
                inventory_list=inventory_list,
1455 1456 1457
                statistic=statistic,
                quantity_unit_uid=quantity_unit_uid,
                convert_quantity_result=convert_quantity_result,
1458
                **stock_sql_kw)
1459 1460 1461 1462 1463
              # Match & add initial and delta inventories
              if src__:
                sql_source_list.extend((initial_inventory_line_list,
                                        delta_inventory_line_list))
              else:
1464
                if 'column_group_by' in new_kw:
1465 1466
                  group_by_id_list = []
                  group_by_id_list_append = group_by_id_list.append
1467
                  for group_by_id in new_kw['column_group_by']:
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
                    if group_by_id == 'uid':
                      group_by_id_list_append('stock_uid')
                    else:
                      group_by_id_list_append(group_by_id)
                  def getInventoryListKey(line):
                    """
                      Generate a key based on values used in SQL group_by
                    """
                    return tuple([line[x] for x in group_by_id_list])
                else:
                  def getInventoryListKey(line):
                    """
                      No group by criterion, regroup everything.
                    """
                    return 'dummy_key'
                result_column_id_dict['inventory'] = None
                result_column_id_dict['total_quantity'] = None
                result_column_id_dict['total_price'] = None
                def addLineValues(line_a=None, line_b=None):
                  """
                    Addition columns of 2 lines and return a line with same
                    schema. If one of the parameters is None, returns the
                    other parameters.

1492
                    Arithmetic modifications on additions:
1493 1494 1495 1496 1497 1498 1499
                      None + x = x
                      None + None = None
                  """
                  if line_a is None:
                    return line_b
                  if line_b is None:
                    return line_a
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
                  # Create a new Shared.DC.ZRDB.Results.Results.__class__
                  # instance to add the line values.
                  # the logic for the 5 lines below is taken from
                  # Shared.DC.ZRDB.Results.Results.__getitem__
                  Result = line_a.__class__
                  parent = line_a.aq_parent
                  result = Result((), parent)
                  if parent is not None:
                    result = result.__of__(parent)

                  for key in line_a.__record_schema__:
                    value = line_a[key] 
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
                    if key in result_column_id_dict:
                      value_b = line_b[key]
                      if None not in (value, value_b):
                        result[key] = value + value_b
                      elif value is not None:
                        result[key] = value
                      else:
                        result[key] = value_b
                    elif line_a[key] == line_b[key]:
                      result[key] = line_a[key]
1522
                    elif key not in ('date', 'stock_uid', 'path'):
1523 1524
                      LOG('InventoryTool.getInventoryList.addLineValues',
                          PROBLEM,
1525
                          'mismatch for %s column: %s and %s' % \
1526 1527 1528 1529 1530
                          (key, line_a[key], line_b[key]))
                  return result
                inventory_list_dict = {}
                for line_list in (initial_inventory_line_list,
                                  delta_inventory_line_list):
1531
                  for line in line_list:
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
                    line_key = getInventoryListKey(line)
                    line_a = inventory_list_dict.get(line_key)
                    inventory_list_dict[line_key] = addLineValues(line_a,
                                                                  line)
                ## XXX: Returns a dict instead of an <r> instance
                ## As long as they are accessed like dicts it's ok, but...
                #result = inventory_list_dict.values()
                sorted_inventory_list = inventory_list_dict.values()
                sort_on = new_kw.get('sort_on', tuple())
                if len(sort_on) != 0:
                  def cmp_inventory_line(line_a, line_b):
                    """
                      Compare 2 inventory lines and sort them according to
                      sort_on parameter.
                    """
                    result = 0
                    for key, sort_direction in sort_on:
                      if not(key in line_a and key in line_b):
                        raise Exception, "Impossible to sort result since " \
                          "columns sort happens on are not available in " \
                          "result."
                      result = cmp(line_a[key], line_b[key])
                      if result != 0:
                        if len(sort_direction[0]) and \
                           sort_direction[0].upper() != 'A':
                          # Default sort is ascending, if a sort is given and
                          # it does not start with an 'A' then reverse sort.
                          # Tedious syntax checking is MySQL's job, and
                          # happened when queries were executed.
                          result *= -1
                        break
                    return result
                  sorted_inventory_list.sort(cmp_inventory_line)
                result = Results((delta_inventory_line_list.\
                                    _searchable_result_columns(),
                                 tuple(sorted_inventory_list)))
            else:
              # Not all required full inventories are found
              optimisation_success = False
          else:
            # Not enough criterions to trigger optimisation
            optimisation_success = False
      if not optimisation_success:
        result = self.Resource_zGetInventoryList(
1576
                    stock_table_id=default_stock_table,
1577
                    src__=src__, ignore_variation=ignore_variation,
1578
                    standardise=standardise, omit_simulation=omit_simulation,
1579
                    only_accountable=only_accountable,
1580
                    selection_domain=selection_domain,
1581
                    selection_report=selection_report, precision=precision,
Aurel's avatar
Aurel committed
1582
                    inventory_list=inventory_list, connection_id=connection_id,
1583 1584 1585
                    statistic=statistic,
                    quantity_unit_uid=quantity_unit_uid,
                    convert_quantity_result=convert_quantity_result,
1586
                    **stock_sql_kw)
1587 1588 1589 1590 1591
        if src__:
          sql_source_list.append(result)
      if src__:
        result = ';\n-- NEXT QUERY\n'.join(sql_source_list)
      return result
Romain Courteaud's avatar
Romain Courteaud committed
1592

1593 1594
    security.declareProtected(Permissions.AccessContentsInformation,
                              'getConvertedInventoryList')
1595
    def getConvertedInventoryList(self, simulation_period='', **kw):
1596 1597 1598 1599 1600
      """
      Return list of inventory with a 'converted_quantity' additional column,
      which contains the sum of measurements for the specified metric type,
      expressed in the 'quantity_unit' unit.

1601 1602
      metric_type   - category
      quantity_unit - category
1603 1604
      """

1605 1606
      warn('getConvertedInventoryList is Deprecated, use ' \
           'getInventory instead.', DeprecationWarning)
1607 1608

      method = getattr(self,'get%sInventoryList' % simulation_period)
1609 1610

      return method(**kw)
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645

    security.declareProtected(Permissions.AccessContentsInformation,
                              'getAllInventoryList')
    def getAllInventoryList(self, src__=0, **kw):
      """
      Returns list of inventory, for all periods.
      Performs 1 SQL request for each simulation state, and merge the results.
      Rename relevant columns with a '${simulation}_' prefix
      (ex: 'total_price' -> 'current_total_price').
      """
      columns = ('total_quantity', 'total_price', 'converted_quantity')

      # Guess the columns to use to identify each row, by looking at the GROUP
      # clause. Note that the call to 'mergeZRDBResults' will crash if the GROUP
      # clause contains a column not requested in the SELECT clause.
      kw.update(self._getDefaultGroupByParameters(**kw), ignore_group_by=1)
      group_by_list = self._generateKeywordDict(**kw)[1].get('group_by', ())

      results = []
      edit_result = {}
      get_false_value = lambda row, column_name: row.get(column_name) or 0

      for simulation in 'current', 'available', 'future':
        method = getattr(self, 'get%sInventoryList' % simulation.capitalize())
        rename = {'inventory': None} # inventory column is deprecated
        for column in columns:
          rename[column] = new_name = '%s_%s' % (simulation, column)
          edit_result[new_name] = get_false_value
        results += (method(src__=src__, **kw), rename),

      if src__:
        return ';\n-- NEXT QUERY\n'.join(r[0] for r in results)
      return mergeZRDBResults(results, group_by_list, edit_result)


1646
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1647
                              'getCurrentInventoryList')
Vincent Pelletier's avatar
Vincent Pelletier committed
1648
    def getCurrentInventoryList(self, omit_transit=1,
1649
                                transit_simulation_state=None, **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1650
      """
Romain Courteaud's avatar
Romain Courteaud committed
1651
        Returns list of current inventory grouped by section or site
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1652
      """
1653 1654 1655 1656 1657 1658 1659 1660 1661
      kw['simulation_state'] = self.getPortalCurrentInventoryStateList() + \
                               self.getPortalTransitInventoryStateList()
      if transit_simulation_state is None:
        transit_simulation_state = self.getPortalTransitInventoryStateList()

      return self.getInventoryList(
                            omit_transit=omit_transit,
                            transit_simulation_state=transit_simulation_state,
                            **kw)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1662

1663
    security.declareProtected(Permissions.AccessContentsInformation,
1664
                              'getAvailableInventoryList')
1665
    def getAvailableInventoryList(self, omit_transit=1, transit_simulation_state=None, **kw):
1666 1667 1668
      """
        Returns list of current inventory grouped by section or site
      """
1669 1670 1671 1672 1673 1674 1675 1676 1677
      if transit_simulation_state is None:
        transit_simulation_state = self.getPortalTransitInventoryStateList()
      kw['simulation_state'] = self.getPortalCurrentInventoryStateList() + \
                               self.getPortalTransitInventoryStateList()
      reserved_kw = {'simulation_state': self.getPortalReservedInventoryStateList(),
                     'transit_simulation_state': transit_simulation_state,
                     'omit_input': 1}
      return self.getInventoryList(reserved_kw=reserved_kw, omit_transit=omit_transit,
                     transit_simulation_state=transit_simulation_state, **kw)
1678 1679

    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1680
                              'getFutureInventoryList')
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1681 1682
    def getFutureInventoryList(self, **kw):
      """
Romain Courteaud's avatar
Romain Courteaud committed
1683
        Returns list of future inventory grouped by section or site
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1684
      """
Romain Courteaud's avatar
Romain Courteaud committed
1685
      kw['simulation_state'] = tuple(
Romain Courteaud's avatar
Romain Courteaud committed
1686
                 list(self.getPortalFutureInventoryStateList()) + \
1687
                 list(self.getPortalTransitInventoryStateList()) + \
Romain Courteaud's avatar
Romain Courteaud committed
1688
                 list(self.getPortalReservedInventoryStateList()) + \
Romain Courteaud's avatar
Romain Courteaud committed
1689
                 list(self.getPortalCurrentInventoryStateList()))
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1690 1691
      return self.getInventoryList(**kw)

1692
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1693
                              'getInventoryStat')
1694
    def getInventoryStat(self, simulation_period='', **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1695
      """
1696
      getInventoryStat is the pending to getInventoryList in order to
Romain Courteaud's avatar
Romain Courteaud committed
1697
      provide statistics on getInventoryList lines in ListBox such as:
1698
      total of inventories, number of variations, number of different
Romain Courteaud's avatar
Romain Courteaud committed
1699
      nodes, etc.
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1700
      """
Romain Courteaud's avatar
Romain Courteaud committed
1701
      kw['group_by_variation'] = 0
1702
      method = getattr(self,'get%sInventoryList' % simulation_period)
Vincent Pelletier's avatar
Vincent Pelletier committed
1703
      return method(statistic=1, inventory_list=0,
1704
                                   ignore_group_by=1, **kw)
1705 1706

    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1707
                              'getCurrentInventoryStat')
1708
    def getCurrentInventoryStat(self, **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1709 1710 1711
      """
      Returns statistics of current inventory grouped by section or site
      """
1712
      return self.getInventoryStat(simulation_period='Current', **kw)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1713

1714
    security.declareProtected(Permissions.AccessContentsInformation,
1715 1716 1717
                              'getAvailableInventoryStat')
    def getAvailableInventoryStat(self, **kw):
      """
1718
      Returns statistics of available inventory grouped by section or site
1719
      """
1720
      return self.getInventoryStat(simulation_period='Available', **kw)
1721 1722

    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1723
                              'getFutureInventoryStat')
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1724 1725 1726 1727
    def getFutureInventoryStat(self, **kw):
      """
      Returns statistics of future inventory grouped by section or site
      """
1728
      return self.getInventoryStat(simulation_period='Future', **kw)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1729

1730
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1731
                              'getInventoryChart')
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1732
    def getInventoryChart(self, src__=0, **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1733
      """
1734 1735
      Returns a list of couples derived from getInventoryList in order
      to feed a chart renderer. Each couple consist of a label
1736
      (node, section, payment, combination of node & section, etc.)
Romain Courteaud's avatar
Romain Courteaud committed
1737
      and an inventory value.
1738

1739
      Mostly useful for charts in ERP5 forms.
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1740
      """
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1741
      result = self.getInventoryList(src__=src__, **kw)
1742
      if src__ :
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1743
        return result
1744

1745
      return map(lambda r: (r.node_title, r.total_quantity), result)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1746

1747
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1748
                              'getCurrentInventoryChart')
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1749 1750 1751 1752
    def getCurrentInventoryChart(self, **kw):
      """
      Returns list of current inventory grouped by section or site
      """
1753
      kw['simulation_state'] = self.getPortalCurrentInventoryStateList()
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1754 1755
      return self.getInventoryChart(**kw)

1756
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1757
                              'getFutureInventoryChart')
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1758 1759 1760 1761
    def getFutureInventoryChart(self, **kw):
      """
      Returns list of future inventory grouped by section or site
      """
Romain Courteaud's avatar
Romain Courteaud committed
1762 1763 1764 1765
      kw['simulation_state'] = tuple(
                      list(self.getPortalFutureInventoryStateList()) + \
                      list(self.getPortalReservedInventoryStateList()) + \
                      list(self.getPortalCurrentInventoryStateList()))
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1766 1767
      return self.getInventoryChart(**kw)

1768
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1769
                              'getInventoryAssetPrice')
Vincent Pelletier's avatar
Vincent Pelletier committed
1770
    def getInventoryAssetPrice(self, src__=0,
1771 1772 1773
                               simulation_period='',
                               valuation_method=None,
                               **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1774
      """
1775
      Same thing as getInventory but returns an asset
1776
      price rather than an inventory.
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787

      If valuation method is None, returns the sum of total prices.

      Else it should be a string, in:
        Filo
        Fifo
        WeightedAverage
        MonthlyWeightedAverage
        MovingAverage
      When using a specific valuation method, a resource_uid is expected
      as well as one of (section_uid or node_uid).
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1788
      """
1789 1790 1791 1792 1793 1794
      if valuation_method is None:
        method = getattr(self,'get%sInventoryList' % simulation_period)
        kw['ignore_group_by'] = 1
        result = method( src__=src__, inventory_list=0, **kw)
        if src__ :
          return result
1795

1796 1797 1798 1799
        if len(result) == 0:
          return 0.0

        total_result = 0.0
1800
        for result_line in result:
1801
          if result_line.total_price is not None:
1802
            total_result += result_line.total_price
1803

1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
        return total_result

      if valuation_method not in ('Fifo', 'Filo', 'WeightedAverage',
        'MonthlyWeightedAverage', 'MovingAverage'):
        raise ValueError("Invalid valuation method: %s" % valuation_method)

      assert 'node_uid' in kw or 'section_uid' in kw
      sql_kw = self._generateSQLKeywordDict(**kw)

      if 'section_uid' in kw:
        # ignore internal movements
        sql_kw['where_expression'] += ' AND ' \
1816
          'NOT(stock.section_uid<=>stock.mirror_section_uid)'
1817 1818 1819

      result = self.Resource_zGetAssetPrice(
          valuation_method=valuation_method,
Aurel's avatar
Aurel committed
1820
          src__=src__,
1821 1822
          **sql_kw)

Aurel's avatar
Aurel committed
1823 1824 1825
      if src__ :
        return result

1826 1827
      if len(result) > 0:
        return result[-1].total_asset_price
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1828

1829
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1830
                              'getCurrentInventoryAssetPrice')
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1831 1832 1833 1834
    def getCurrentInventoryAssetPrice(self, **kw):
      """
      Returns list of current inventory grouped by section or site
      """
1835
      kw['simulation_state'] = self.getPortalCurrentInventoryStateList()
1836
      return self.getInventoryAssetPrice(simulation_period='Current',**kw)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1837

1838
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1839
                              'getAvailableInventoryAssetPrice')
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1840 1841 1842 1843 1844
    def getAvailableInventoryAssetPrice(self, **kw):
      """
      Returns list of available inventory grouped by section or site
      (current inventory - deliverable)
      """
1845 1846 1847
      kw['simulation_state'] = tuple(
                    list(self.getPortalReservedInventoryStateList()) + \
                    list(self.getPortalCurrentInventoryStateList()))
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1848 1849
      return self.getInventoryAssetPrice(**kw)

1850
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1851
                              'getFutureInventoryAssetPrice')
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1852 1853 1854 1855
    def getFutureInventoryAssetPrice(self, **kw):
      """
      Returns list of future inventory grouped by section or site
      """
Romain Courteaud's avatar
Romain Courteaud committed
1856 1857 1858 1859
      kw['simulation_state'] = tuple(
               list(self.getPortalFutureInventoryStateList()) + \
               list(self.getPortalReservedInventoryStateList()) + \
               list(self.getPortalCurrentInventoryStateList()))
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1860 1861
      return self.getInventoryAssetPrice(**kw)

1862
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1863
                              'getInventoryHistoryList')
1864
    def getInventoryHistoryList(self, src__=0, ignore_variation=0,
1865 1866
                                standardise=0, omit_simulation=0,
                                only_accountable=True, omit_input=0,
1867
                                omit_output=0, selection_domain=None,
1868
                                selection_report=None, precision=None, **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1869
      """
1870 1871 1872
      Returns a time based serie of inventory values
      for a single or a group of resource, node, section, etc. This is useful
      to list the evolution with time of inventory values (quantity, asset price).
1873 1874 1875

      TODO:
        - make sure getInventoryHistoryList can return
1876
          cumulative values calculated by SQL (JPS)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1877
      """
1878
      sql_kw = self._generateSQLKeywordDict(**kw)
Romain Courteaud's avatar
Romain Courteaud committed
1879
      return self.Resource_getInventoryHistoryList(
1880
                      src__=src__, ignore_variation=ignore_variation,
1881
                      standardise=standardise, omit_simulation=omit_simulation,
1882
                      only_accountable=only_accountable,
Romain Courteaud's avatar
Romain Courteaud committed
1883
                      omit_input=omit_input, omit_output=omit_output,
1884
                      selection_domain=selection_domain,
1885 1886
                      selection_report=selection_report, precision=precision,
                      **sql_kw)
1887

1888
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1889
                              'getInventoryHistoryChart')
1890
    def getInventoryHistoryChart(self, src__=0, ignore_variation=0,
1891
                                 standardise=0, omit_simulation=0,
1892
                                 only_accountable=True,
Romain Courteaud's avatar
Romain Courteaud committed
1893
                                 omit_input=0, omit_output=0,
1894
                                 selection_domain=None,
1895
                                 selection_report=None, precision=None, **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1896
      """
1897 1898 1899 1900 1901
      getInventoryHistoryChart is the pensing to getInventoryHistoryList
      to ease the rendering of time based graphs which show the evolution
      of one ore more inventories. Each item in the serie consists of
      time, value and "colour" (multiple graphs can be drawn for example
      for each variation of a resource)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1902
      """
1903 1904
      sql_kw = self._generateSQLKeywordDict(**kw)

Romain Courteaud's avatar
Romain Courteaud committed
1905
      return self.Resource_getInventoryHistoryChart(
1906
                    src__=src__, ignore_variation=ignore_variation,
1907
                    standardise=standardise, omit_simulation=omit_simulation,
1908
                    only_accountable=only_accountable,
Romain Courteaud's avatar
Romain Courteaud committed
1909
                    omit_input=omit_input, omit_output=omit_output,
1910
                    selection_domain=selection_domain,
1911 1912
                    selection_report=selection_report, precision=precision,
                    **sql_kw)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1913

1914
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1915
                              'getMovementHistoryList')
1916
    def getMovementHistoryList(self, src__=0, ignore_variation=0,
1917
                               standardise=0, omit_simulation=0,
1918
                               omit_input=0, omit_output=0,
1919
                               only_accountable=True,
1920
                               omit_asset_increase=0, omit_asset_decrease=0,
1921
                               selection_domain=None, selection_report=None,
1922
                               initial_running_total_quantity=0,
1923
                               initial_running_total_price=0, precision=None,
Romain Courteaud's avatar
Romain Courteaud committed
1924
                               **kw):
1925
      """Returns a list of movements which modify the inventory
1926
      for a single or a group of resource, node, section, etc.
1927 1928 1929
      A running total quantity and a running total price are available on
      brains. The initial values can be passed, in case you want to have an
      "initial summary line".
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1930
      """
1931 1932
      kw['movement_list_mode'] = 1
      kw.update(self._getDefaultGroupByParameters(**kw))
1933
      sql_kw = self._generateSQLKeywordDict(**kw)
1934

Romain Courteaud's avatar
Romain Courteaud committed
1935
      return self.Resource_zGetMovementHistoryList(
1936 1937
                         src__=src__, ignore_variation=ignore_variation,
                         standardise=standardise,
1938
                         omit_simulation=omit_simulation,
1939
                         only_accountable=only_accountable,
Romain Courteaud's avatar
Romain Courteaud committed
1940
                         omit_input=omit_input, omit_output=omit_output,
1941 1942
                         omit_asset_increase=omit_asset_increase,
                         omit_asset_decrease=omit_asset_decrease,
1943
                         selection_domain=selection_domain,
1944 1945 1946 1947 1948
                         selection_report=selection_report,
                         initial_running_total_quantity=
                                  initial_running_total_quantity,
                         initial_running_total_price=
                                  initial_running_total_price,
1949
                         precision=precision, **sql_kw)
1950

1951
    security.declareProtected(Permissions.AccessContentsInformation,
Romain Courteaud's avatar
Romain Courteaud committed
1952
                              'getMovementHistoryStat')
1953
    def getMovementHistoryStat(self, src__=0, ignore_variation=0,
1954 1955
                               standardise=0, omit_simulation=0,
                               only_accountable=True, omit_input=0,
1956
                               omit_output=0, selection_domain=None,
1957
                               selection_report=None, precision=None, **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1958
      """
Romain Courteaud's avatar
Romain Courteaud committed
1959 1960
      getMovementHistoryStat is the pending to getMovementHistoryList
      for ListBox stat
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1961
      """
1962
      sql_kw = self._generateSQLKeywordDict(**kw)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1963
      return self.Resource_zGetInventory(src__=src__,
1964
          ignore_variation=ignore_variation, standardise=standardise,
1965 1966 1967 1968
          omit_simulation=omit_simulation, only_accountable=only_accountable,
          omit_input=omit_input, omit_output=omit_output,
          selection_domain=selection_domain, selection_report=selection_report,
          precision=precision, **sql_kw)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1969

Vincent Pelletier's avatar
Vincent Pelletier committed
1970
    security.declareProtected(Permissions.AccessContentsInformation,
1971
                              'getNextNegativeInventoryDate')
1972
    def getNextNegativeInventoryDate(self, src__=0, **kw):
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1973 1974 1975
      """
      Returns statistics of inventory grouped by section or site
      """
1976 1977 1978
      #sql_kw = self._generateSQLKeywordDict(order_by_expression='stock.date', **kw)
      #sql_kw['group_by_expression'] = 'stock.uid'
      #sql_kw['order_by_expression'] = 'stock.date'
1979

1980
      result = self.getInventoryList(src__=src__,
1981
          sort_on = (('date', 'ascending'),), group_by_movement=1, **kw)
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1982 1983
      if src__ :
        return result
1984

1985
      total_inventory = 0.
1986
      for inventory in result:
1987 1988 1989 1990 1991
        if inventory['inventory'] is not None:
          total_inventory += inventory['inventory']
          if total_inventory < 0:
            return inventory['date']

1992
      return None
Alexandre Boeglin's avatar
Alexandre Boeglin committed
1993

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1994
    #######################################################
1995
    # Traceability management
1996
    security.declareProtected(Permissions.AccessContentsInformation, 'getTrackingList')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1997
    def getTrackingList(self, src__=0,
1998
        selection_domain=None, selection_report=None,
1999
        strict_simulation_state=1, history=0, **kw) :
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2000
      """
2001
      Returns a list of items in the form
2002

Jean-Paul Smets's avatar
Jean-Paul Smets committed
2003 2004 2005 2006 2007 2008
        uid (of item)
        date
        node_uid
        section_uid
        resource_uid
        variation_text
2009
        delivery_uid
2010

2011 2012 2013 2014 2015 2016 2017 2018
      If at_date is provided, returns the a list which answers
      to the question "where are those items at this date" or
      "which are those items which are there a this date"

      If at_date is not provided, returns a history of positions
      which answers the question "where have those items been
      between this time and this time". This will be handled by
      something like getTrackingHistoryList
2019 2020

      This method is only suitable for singleton items (an item which can
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2021 2022
      only be at a single place at a given time). Such items include
      containers, serial numbers (ex. for engine), rolls with subrolls,
2023 2024

      This method is not suitable for batches (ex. a coloring batch).
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2025
      For such items, standard getInventoryList method is appropriate
2026

Jean-Paul Smets's avatar
Jean-Paul Smets committed
2027
      Parameters are the same as for getInventory.
2028

2029 2030 2031 2032 2033 2034 2035 2036 2037
      Default sort orders is based on dates, reverse.


      from_date (>=) -

      to_date   (<)  -

      at_date   (<=) - only take rows which date is <= at_date

2038 2039
      history (boolean) - keep history variations

2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
      resource (only in generic API in simulation)

      node        -  only take rows in stock table which node_uid is equivalent to node

      section        -  only take rows in stock table which section_uid is equivalent to section

      resource_category        -  only take rows in stock table which resource_uid is in resource_category

      node_category        -  only take rows in stock table which node_uid is in section_category

      section_category        -  only take rows in stock table which section_uid is in section_category

      variation_text - only take rows in stock table with specified variation_text
                       this needs to be extended with some kind of variation_category ?
                       XXX this way of implementing variation selection is far from perfect

      variation_category - variation or list of possible variations

      simulation_state - only take rows with specified simulation_state

      selection_domain, selection_report - see ListBox

      **kw  - if we want extended selection with more keywords (but bad performance)
2063
              check what we can do with buildSQLQuery
2064 2065 2066 2067

      Extra parameters for getTrackingList :

      item
2068

2069 2070 2071 2072 2073
      input - if set, answers to the question "which are those items which have been
              delivered for the first time after from_date". Cannot be used with output

      output - if set, answers to the question "which are those items which have been
               delivered for the last time before at_date or to_date". Cannot be used with input
2074

Jean-Paul Smets's avatar
Jean-Paul Smets committed
2075
      """
2076
      new_kw = self._generateSQLKeywordDict(table='item',strict_simulation_state=strict_simulation_state,**kw)
2077 2078 2079 2080 2081 2082
      for key in ('at_date', 'to_date'):
        value = kw.get(key, None)
        if value is not None:
          if isinstance(value, DateTime):
            value = value.toZone('UTC').ISO()
          value = '%s' % (value, )
Vincent Pelletier's avatar
Vincent Pelletier committed
2083
        # Do not remove dates in new_kw, they are required in
2084 2085
        # order to do a "select item left join item on date"
        new_kw[key] = value
2086

2087
      # Extra parameters for the SQL Method
2088
      new_kw['join_on_item'] = not history and (new_kw.get('at_date') or \
2089
                               new_kw.get('to_date') or \
2090
                               new_kw.get('input') or \
2091
                               new_kw.get('output'))
2092
      new_kw['date_condition_in_join'] = not (new_kw.get('input') or new_kw.get('output'))
2093

2094 2095
      # Pass simulation state to request
      if kw.has_key('item.simulation_state'):
2096
        new_kw['simulation_state_list'] = kw['item.simulation_state']
2097
      else:
2098
        new_kw['simulation_state_list'] =  None
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2099

2100
      return self.Resource_zGetTrackingList(src__=src__,
2101 2102 2103
                                            selection_domain=selection_domain,
                                            selection_report=selection_report,
                                            **new_kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2104 2105 2106 2107 2108 2109

    security.declareProtected(Permissions.AccessContentsInformation, 'getCurrentTrackingList')
    def getCurrentTrackingList(self, **kw):
      """
      Returns list of current inventory grouped by section or site
      """
2110
      kw['item.simulation_state'] = self.getPortalCurrentInventoryStateList()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2111
      return self.getTrackingList(**kw)
Vincent Pelletier's avatar
Vincent Pelletier committed
2112

2113 2114 2115 2116 2117 2118 2119
    security.declareProtected(Permissions.AccessContentsInformation, 'getCurrentTrackingHistoryList')
    def getCurrentTrackingHistoryList(self, **kw):
      """
      Returns list of current inventory grouped by section or site
      """
      kw['item.simulation_state'] = self.getPortalCurrentInventoryStateList()
      return self.getTrackingHistoryList(**kw)
Vincent Pelletier's avatar
Vincent Pelletier committed
2120

2121 2122 2123 2124 2125 2126 2127
    security.declareProtected(Permissions.AccessContentsInformation, 'getTrackingHistoryList')
    def getTrackingHistoryList(self, **kw):
      """
      Returns history list of inventory grouped by section or site
      """
      kw['history'] = 1
      return self.getTrackingList(**kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2128 2129 2130 2131 2132 2133

    security.declareProtected(Permissions.AccessContentsInformation, 'getFutureTrackingList')
    def getFutureTrackingList(self, **kw):
      """
      Returns list of future inventory grouped by section or site
      """
2134
      kw['item.simulation_state'] = tuple(list(self.getPortalFutureInventoryStateList())
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2135 2136 2137
          + list(self.getPortalReservedInventoryStateList()) + list(self.getPortalCurrentInventoryStateList()))
      return self.getTrackingList(**kw)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
    #######################################################
    # Capacity Management
    security.declareProtected( Permissions.ModifyPortalContent, 'updateCapacity' )
    def updateCapacity(self, node):
      capacity_item_list = []
      for o in node.contentValues():
        if o.isCapacity():
          # Do whatever is needed
          capacity_item_list += o.asCapacityItemList()
          pass
      # Do whatever with capacity_item_list
      # and store the resulting new capacity in node
      node._capacity_item_list = capacity_item_list

    security.declareProtected( Permissions.ModifyPortalContent, 'isMovementInsideCapacity' )
    def isMovementInsideCapacity(self, movement):
      """
        Purpose: provide answer to customer for the question "can you do it ?"

        movement:
          date
          source destination (2 nodes)
          source_section ...
      """
      # Get nodes and dat
      source_node = movement.getSourceValue()
      destination_node = movement.getDestinationValue()
2165 2166
      start_date = movement.getStartDate()
      stop_date = movement.getStopDate()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
      # Return result
      return self.isNodeInsideCapacity(source_node, start_date, additional_movement=movement, sign=1) and self.isNodeInsideCapacity(destination_node, stop_date, additional_movement=movement, sign=-1)

    security.declareProtected( Permissions.ModifyPortalContent, 'isNodeInsideCapacity' )
    def isNodeInsideCapacity(self, node, date, simulation_state=None, additional_movement=None, sign=1):
      """
        Purpose: decide if a node is consistent with its capacity definitions
        at a certain date (ie. considreing the stock / production history
      """
      # First get the current inventory situation for this node
      inventory_list = node.getInventoryList(XXXXX)
      # Add additional movement
      if additional_movement:
          inventory_list = inventory_list + sign * additional_movement # needs to be implemented
      # Return answer
      return self.isAmountListInsideCapacity(node, inventory_list)

    security.declareProtected( Permissions.ModifyPortalContent, 'isAmountListInsideCapacity' )
    def isAmountListInsideCapacity(self, node, amount_list,
         resource_aggregation_base_category=None, resource_aggregation_depth=None):
      """
        Purpose: decide if a list of amounts is consistent with the capacity of a node

        If any resource in amount_list is missing in the capacity of the node, resource
        aggregation is performed, based on resource_aggregation_base_category. If the
        base category is not specified, it is an error (should guess instead?). The resource
        aggregation is done at the level of resource_aggregation_depth in the tree
        of categories. If resource_aggregation_depth is not specified, it's an error.

        Assumptions: amount_list is an association list, like ((R1 V1) (R2 V2)).
                     node has an attribute '_capacity_item_list' which is a list of association lists.
                     resource_aggregation_base_category is a Base Category object or a list of Base
                     Category objects or None.
                     resource_aggregation_depth is a strictly positive integer or None.
      """
      # Make a copy of the attribute _capacity_item_list, because it may be necessary
      # to modify it for resource aggregation.
      capacity_item_list = node._capacity_item_list[:]

      # Make a mapping between resources and its indices.
      resource_map = {}
      index = 0
      for alist in capacity_item_list:
        for pair in alist:
          resource = pair[0]
#          LOG('isAmountListInsideCapacity', 0,
#              "resource is %s" % repr(resource))
          if resource not in resource_map:
            resource_map[resource] = index
            index += 1

      # Build a point from the amount list.
      point = zeros(index, 'd') # Fill up zeros for safety.
      mask_map = {}     # This is used to skip items in amount_list.
      for amount in amount_list:
        if amount[0] in mask_map:
          continue
        # This will fail, if amount_list has any different resource from the capacity.
        # If it has any different point, then we should ......
        #
        # There would be two possible different solutions:
        # 1) If a missing resource is a meta-resource of resources supported by the capacity,
        #    it is possible to add the resource into the capacity by aggregation.
        # 2) If a missing resource has a meta-resource as a parent and the capacity supports
        #    the meta-resource directly or indirectly (`indirectly' means `by aggregation'),
        #    it is possible to convert the missing resource into the meta-resource.
        #
        # However, another way has been implemented here. This does the following, if the resource
        # is not present in the capacity:
        # 1) If the value is zero, just ignore the resource, because zero is always acceptable.
        # 2) Attempt to aggregate resources both of the capacity and of the amount list. This aggregation
        #    is performed at the depth of 'resource_aggregation_depth' under the base category
        #    'resource_aggregation_base_category'.
        #
        resource = amount[0]
        if resource in resource_map:
          point[resource_map[amount[0]]] = amount[1]
        else:
          if amount[1] == 0:
            # If the value is zero, no need to consider.
            pass
          elif resource_aggregation_base_category is None or resource_aggregation_depth is None:
            # XXX use an appropriate error class
            # XXX should guess a base category instead of emitting an exception
            raise RuntimeError, "The resource '%s' is not found in the capacity, and the argument 'resource_aggregation_base_category' or the argument 'resource_aggregation_depth' is not specified" % resource
          else:
            # It is necessary to aggregate resources, to guess the capacity of this resource.

            def getAggregationResourceUrl(url, depth):
              # Return a partial url of the argument 'url'.
              # If 'url' is '/foo/bar/baz' and 'depth' is 2, return '/foo/bar'.
              pos = 0
              for i in range(resource_aggregation_depth):
                pos = url.find('/', pos+1)
                if pos < 0:
                  break
              if pos < 0:
                return None
              pos = url.find('/', pos+1)
              if pos < 0:
                pos = len(url)
              return url[:pos]

            def getAggregatedResourceList(aggregation_url, category, resource_list):
              # Return a list of resources which should be aggregated. 'aggregation_url' is used
              # for a top url of those resources. 'category' is a base category for the aggregation.
              aggregated_resource_list = []
              for resource in resource_list:
                for url in resource.getCategoryMembershipList(category, base=1):
                  if url.startswith(aggregation_url):
                    aggregated_resource_list.append(resource)
              return aggregated_resource_list

            def getAggregatedItemList(item_list, resource_list, aggregation_resource):
              # Return a list of association lists, which is a result of an aggregation.
              # 'resource_list' is a list of resources which should be aggregated.
              # 'aggregation_resource' is a category object which is a new resource created by
              # this aggregation.
              # 'item_list' is a list of association lists.
              new_item_list = []
              for alist in item_list:
                new_val = 0
                new_alist = []
                # If a resource is not a aggregated, then add it to the new alist as it is.
                # Otherwise, aggregate it to a single value.
                for pair in alist:
                  if pair[0] in resource_list:
                    new_val += pair[1]
                  else:
                    new_alist.append(pair)
                # If it is zero, ignore this alist, as it is nonsense.
                if new_val != 0:
                  new_alist.append([aggregation_resource, new_val])
                  new_item_list.append(new_alist)
              return new_item_list

            # Convert this to a string if necessary, for convenience.
2304
            if not isinstance(resource_aggregation_base_category, (tuple, list)):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358
              resource_aggregation_base_category = (resource_aggregation_base_category,)

            done = 0
#            LOG('isAmountListInsideCapacity', 0,
#                "resource_aggregation_base_category is %s" % repr(resource_aggregation_base_category))
            for category in resource_aggregation_base_category:
              for resource_url in resource.getCategoryMembershipList(category, base=1):
                aggregation_url = getAggregationResourceUrl(resource_url,
                                                            resource_aggregation_depth)
                if aggregation_url is None:
                  continue
                aggregated_resource_list = getAggregatedResourceList (aggregation_url,
                                                                      category,
                                                                      resource_map.keys())
                # If any, do the aggregation.
                if len(aggregated_resource_list) > 0:
                  aggregation_resource = self.portal_categories.resolveCategory(aggregation_url)
                  # Add the resource to the mapping.
 #                 LOG('aggregation_resource', 0, str(aggregation_resource))
                  resource_map[aggregation_resource] = index
                  index += 1
                  # Add the resource to the point.
                  point = resize(point, (index,))
                  val = 0
                  for aggregated_amount in amount_list:
                    for url in aggregated_amount[0].getCategoryMembershipList(category, base=1):
                      if url.startswith(aggregation_url):
                        val += aggregated_amount[1]
                        mask_map[aggregated_amount[0]] = None
                        break
                  point[index-1] = val
                  # Add capacity definitions of the resource into the capacity.
                  capacity_item_list += getAggregatedItemList(capacity_item_list,
                                                              aggregated_resource_list,
                                                              aggregation_resource)
                  done = 1
                  break
              if done:
                break
            if not done:
              raise RuntimeError, "Aggregation failed"

      # Build a matrix from the capacity item list.
#      LOG('resource_map', 0, str(resource_map))
      matrix = zeros((len(capacity_item_list)+1, index), 'd')
      for index in range(len(capacity_item_list)):
        for pair in capacity_item_list[index]:
          matrix[index,resource_map[pair[0]]] = pair[1]

#      LOG('isAmountListInsideCapacity', 0,
#          "matrix = %s, point = %s, capacity_item_list = %s" % (str(matrix), str(point), str(capacity_item_list)))
      return solve(matrix, point)


Jean-Paul Smets's avatar
Jean-Paul Smets committed
2359 2360
    # Asset Price Calculation
    def updateAssetPrice(self, resource, variation_text, section_category, node_category,
2361 2362 2363
                         strict_membership=0, simulation_state=None):
      if simulation_state is None:
        simulation_state = self.getPortalCurrentInventoryStateList()
2364 2365 2366
      category_tool = getToolByName(self, 'portal_categories')
      section_value = category_tool.resolveCategory(section_category)
      node_value = category_tool.resolveCategory(node_category)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2367 2368 2369 2370
      # Initialize price
      current_asset_price = 0.0 # Missing: initial inventory price !!!
      current_inventory = 0.0
      # Parse each movement
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2371
      brain_list = self.Resource_zGetMovementHistoryList(resource=[resource],
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2372 2373 2374 2375
                             variation_text=variation_text,
                             section_category=section_category,
                             node_category=node_category,
                             strict_membership=strict_membership,
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2376 2377 2378 2379
                             simulation_state=simulation_state) # strict_membership not taken into account
                             # We select movements related to certain nodes (ex. Stock) and sections (ex.Coramy Group)
      result = []
      for b in brain_list:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2380 2381
        m = b.getObject()
        if m is not None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
          previous_inventory = current_inventory
          inventory_quantity = b.quantity # We should use the aggregated quantity provided by Resource_zGetMovementHistoryList
          quantity = m.getQuantity() # The movement quantity is important to determine the meaning of source and destination
          # Maybe we should take care of target qty in delired deliveries
          if quantity is None:
            quantity = 0.0
          if m.getSourceValue() is None:
            # This is a production movement or an inventory movement
            # Use Industrial Price
            current_inventory += inventory_quantity # Update inventory
            if m.getPortalType() in ('Inventory Line', 'Inventory Cell'): # XX should be replaced by isInventory ???
              asset_price = m.getPrice()
              if asset_price in (0.0, None):
                asset_price = current_asset_price # Use current price if no price defined
            else: # this is a production
              asset_price = m.getIndustrialPrice()
              if asset_price is None: asset_price = current_asset_price  # Use current price if no price defined
            result.append((m.getRelativeUrl(), m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2400
                          m.getQuantity(), 'Production or Inventory', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2401 2402 2403 2404 2405 2406
                        ))
          elif m.getDestinationValue() is None:
            # This is a consumption movement or an inventory movement
            current_inventory += inventory_quantity # Update inventory
            asset_price = current_asset_price
            result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2407
                          m.getQuantity(), 'Consumption or Inventory', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2408
                        ))
2409
          elif m.getSourceValue().isAcquiredMemberOf(node_category) and m.getDestinationValue().isAcquiredMemberOf(node_category):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2410 2411 2412 2413
            # This is an internal movement
            current_inventory += inventory_quantity # Update inventory
            asset_price = current_asset_price
            result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2414
                          m.getQuantity(), 'Internal', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2415
                        ))
2416
          elif m.getSourceValue().isAcquiredMemberOf(node_category) and quantity < 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2417 2418 2419 2420
            # This is a physically inbound movement - try to use commercial price
            if m.getSourceSectionValue() is None:
              # No meaning
              current_inventory += inventory_quantity # Update inventory
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2421
              asset_price = current_asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2422
              result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2423
                            m.getQuantity(), 'Error', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2424 2425 2426 2427
                          ))
            elif m.getDestinationSectionValue() is None:
              # No meaning
              current_inventory += inventory_quantity # Update inventory
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2428
              asset_price = current_asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2429
              result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2430
                            m.getQuantity(), 'Error', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2431
                          ))
2432
            elif m.getDestinationSectionValue().isAcquiredMemberOf(section_category):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2433
              current_inventory += inventory_quantity # Update inventory
2434
              if m.getDestinationValue().isAcquiredMemberOf('site/Piquage'):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2435 2436 2437 2438
                # Production
                asset_price = m.getIndustrialPrice()
                if asset_price is None: asset_price = current_asset_price  # Use current price if no price defined
                result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2439
                              m.getQuantity(), 'Production', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2440
                            ))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2441 2442 2443
              else:
                # Inbound from same section
                asset_price = current_asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2444
                result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2445
                              m.getQuantity(), 'Inbound same section', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2446
                            ))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2447
            else:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2448 2449 2450
              current_inventory += inventory_quantity # Update inventory
              asset_price = m.getPrice()
              result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2451
                            m.getQuantity(), 'Inbound different section', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2452
                          ))
2453
          elif m.getDestinationValue().isAcquiredMemberOf(node_category) and quantity > 0:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2454 2455 2456 2457 2458 2459
            # This is a physically inbound movement - try to use commercial price
            if m.getSourceSectionValue() is None:
              # No meaning
              current_inventory += inventory_quantity # Update inventory
              asset_price = current_asset_price
              result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2460
                            m.getQuantity(), 'Error', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2461 2462 2463 2464
                          ))
            elif m.getDestinationSectionValue() is None:
              # No meaning
              current_inventory += inventory_quantity # Update inventory
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2465
              asset_price = current_asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2466
              result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2467
                            m.getQuantity(), 'Error', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2468
                          ))
2469
            elif m.getSourceSectionValue().isAcquiredMemberOf(section_category):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2470
              current_inventory += inventory_quantity # Update inventory
2471
              if m.getSourceValue().isAcquiredMemberOf('site/Piquage'):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2472 2473 2474 2475
                # Production
                asset_price = m.getIndustrialPrice()
                if asset_price is None: asset_price = current_asset_price  # Use current price if no price defined
                result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2476
                              m.getQuantity(), 'Production', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2477
                            ))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2478
              else:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2479 2480 2481
                # Inbound from same section
                asset_price = current_asset_price
                result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2482
                            m.getQuantity(), 'Inbound same section', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2483
                          ))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2484
            else:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2485 2486 2487
              current_inventory += inventory_quantity # Update inventory
              asset_price = m.getPrice()
              result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2488
                            m.getQuantity(), 'Inbound different section', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2489 2490 2491 2492 2493 2494
                          ))
          else:
            # Outbound movement
            current_inventory += inventory_quantity # Update inventory
            asset_price = current_asset_price
            result.append((m.getRelativeUrl(),m.getStartDate(), m.getSource(), m.getSourceSection(), m.getDestination(), m.getDestinationSection(),
2495
                            m.getQuantity(), 'Outbound', 'Price: %s' % asset_price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
                          ))

          # Update asset_price
          if current_inventory > 0:
            if inventory_quantity is not None:
              # Update price with an average of incoming goods and current goods
              current_asset_price = ( current_asset_price * previous_inventory + asset_price * inventory_quantity ) / float(current_inventory)
          else:
            # New price is the price of incoming goods - negative stock has no meaning for asset calculation
            current_asset_price = asset_price

          result.append(('###New Asset Price', current_asset_price, 'New Inventory', current_inventory))

          # Update Asset Price on the right side
2510
          if m.getSourceSectionValue() is not None and m.getSourceSectionValue().isAcquiredMemberOf(section_category):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2511 2512
            # for each movement, source section is member of one and one only accounting category
            # therefore there is only one and one only source asset price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2513 2514 2515 2516 2517 2518 2519
            m._setSourceAssetPrice(current_asset_price)
            #quantity = m.getInventoriatedQuantity()
            #if quantity:
            #  #total_asset_price = - current_asset_price * quantity
            #  #m.Movement_zSetSourceTotalAssetPrice(uid=m.getUid(), total_asset_price = total_asset_price)
            #  m._setSourceAssetPrice(current_asset_price)
          if m.getDestinationSectionValue() is not None and m.getDestinationSectionValue().isMemberOf(section_category):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2520 2521
            # for each movement, destination section is member of one and one only accounting category
            # therefore there is only one and one only destination asset price
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2522 2523 2524 2525 2526
            m._setDestinationAssetPrice(current_asset_price)
            #quantity = m.getInventoriatedQuantity()
            #if quantity:
            #  total_asset_price = current_asset_price * quantity
            #  m.Movement_zSetDestinationTotalAssetPrice(uid=m.getUid(), total_asset_price = total_asset_price)
2527 2528 2529
          # Global reindexing required afterwards in any case: so let us do it now
          # Until we get faster methods (->reindexObject())
          #m.immediateReindexObject()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2530
          m.reindexObject()
2531
          #m.activate(priority=7).immediateReindexObject() # Too slow
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2532 2533

      return result
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2534

2535 2536 2537
    # Used for mergeDeliveryList.
    class MergeDeliveryListError(Exception): pass

2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
    security.declareProtected( Permissions.ModifyPortalContent, 'mergeDeliveryList' )
    def mergeDeliveryList(self, delivery_list):
      """
        Merge multiple deliveries into one delivery.
        All delivery lines are merged into the first one.
        The first one is therefore called main_delivery here.
        The others are cancelled.
        Return the main delivery.
      """
      # Sanity checks.
      if len(delivery_list) == 0:
2549
        raise self.MergeDeliveryListError, "No delivery is passed"
2550
      elif len(delivery_list) == 1:
2551
        raise self.MergeDeliveryListError, "Only one delivery is passed"
2552 2553 2554 2555

      main_delivery = delivery_list[0]
      delivery_list = delivery_list[1:]

2556
      # Another sanity check. It is necessary for them to be identical in some attributes.
2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
      for delivery in delivery_list:
        for attr in ('portal_type', 'simulation_state',
                     'source', 'destination',
                     'source_section', 'destination_section',
                     'source_decision', 'destination_decision',
                     'source_administration', 'destination_administration',
                     'source_payment', 'destination_payment'):
          main_value = main_delivery.getProperty(attr)
          value = delivery.getProperty(attr)
          if  main_value != value:
2567 2568 2569 2570
            raise self.MergeDeliveryListError, \
              "%s is not the same between %s and %s (%s and %s)" % (attr, delivery.getId(), main_delivery.getId(), value, main_value)

      # One more sanity check. Check if discounts are the same, if any.
2571
      main_discount_list = main_delivery.contentValues(filter = {'portal_type': self.getPortalDiscountTypeList()})
2572
      for delivery in delivery_list:
2573
        discount_list = delivery.contentValues(filter = {'portal_type': self.getPortalDiscountTypeList()})
2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
        if len(main_discount_list) != len(discount_list):
          raise self.MergeDeliveryListError, "Discount is not the same between %s and %s" % (delivery.getId(), main_delivery.getId())
        for discount in discount_list:
          for main_discount in main_discount_list:
            if discount.getDiscount() == main_discount.getDiscount() \
               and discount.getDiscountRatio() == main_discount.getDiscountRatio() \
               and discount.getDiscountType() == main_discount.getDiscountType() \
               and discount.getImmediateDiscount() == main_discount.getImmediateDiscount():
              break
          else:
            raise self.MergeDeliveryListError, "Discount is not the same between %s and %s" % (delivery.getId(), main_delivery.getId())

      # One more sanity check. Check if payment conditions are the same, if any.
2587
      main_payment_condition_list = main_delivery.contentValues(filter = {'portal_type': self.getPortalPaymentConditionTypeList()})
2588
      for delivery in delivery_list:
2589
        payment_condition_list = delivery.contentValues(filter = {'portal_type': self.getPortalPaymentConditionTypeList()})
2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602
        if len(main_payment_condition_list) != len(payment_condition_list):
          raise self.MergeDeliveryListError, "Payment Condition is not the same between %s and %s" % (delivery.getId(), main_delivery.getId())
        for condition in payment_condition_list:
          for main_condition in main_payment_condition_list:
            if condition.getPaymentMode() == main_condition.getPaymentMode() \
               and condition.getPaymentAdditionalTerm() == main_condition.getPaymentAdditionalTerm() \
               and condition.getPaymentAmount() == main_condition.getPaymentAmount() \
               and condition.getPaymentEndOfMonth() == main_condition.getPaymentEndOfMonth() \
               and condition.getPaymentRatio() == main_condition.getPaymentRatio() \
               and condition.getPaymentTerm() == main_condition.getPaymentTerm():
              break
          else:
            raise self.MergeDeliveryListError, "Payment Condition is not the same between %s and %s" % (delivery.getId(), main_delivery.getId())
2603 2604 2605

      # Make sure that all activities are flushed, to get simulation movements from delivery cells.
      for delivery in delivery_list:
2606
        for order in delivery.getCausalityValueList(portal_type = self.getPortalOrderTypeList()):
2607 2608
          for applied_rule in order.getCausalityRelatedValueList(portal_type = 'Applied Rule'):
            applied_rule.flushActivity(invoke = 1)
2609
        for causality_related_delivery in delivery.getCausalityValueList(portal_type = self.getPortalDeliveryTypeList()):
2610 2611
          for applied_rule in causality_related_delivery.getCausalityRelatedValueList(portal_type = 'Applied Rule'):
            applied_rule.flushActivity(invoke = 1)
2612

2613 2614 2615 2616 2617
      # Get a list of simulated movements and invoice movements.
      main_simulated_movement_list = main_delivery.getSimulatedMovementList()
      main_invoice_movement_list = main_delivery.getInvoiceMovementList()
      simulated_movement_list = main_simulated_movement_list[:]
      invoice_movement_list = main_invoice_movement_list[:]
2618
      for delivery in delivery_list:
2619 2620 2621
        simulated_movement_list.extend(delivery.getSimulatedMovementList())
        invoice_movement_list.extend(delivery.getInvoiceMovementList())

2622 2623 2624 2625
      #for movement in simulated_movement_list + invoice_movement_list:
      #  parent = movement.aq_parent
      #  LOG('mergeDeliveryList', 0, 'movement = %s, parent = %s, movement.getPortalType() = %s, parent.getPortalType() = %s' % (repr(movement), repr(parent), repr(movement.getPortalType()), repr(parent.getPortalType())))

2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
      LOG('mergeDeliveryList', 0, 'simulated_movement_list = %s, invoice_movement_list = %s' % (str(simulated_movement_list), str(invoice_movement_list)))
      for main_movement_list, movement_list in \
        ((main_simulated_movement_list, simulated_movement_list),
         (main_invoice_movement_list, invoice_movement_list)):
        root_group = self.collectMovement(movement_list,
                                          check_order = 0,
                                          check_path = 0,
                                          check_date = 0,
                                          check_criterion = 1,
                                          check_resource = 1,
                                          check_base_variant = 1,
                                          check_variant = 1)
2638 2639 2640 2641 2642 2643 2644 2645 2646
        for criterion_group in root_group.group_list:
          LOG('mergeDeliveryList dump tree', 0, 'criterion = %s, movement_list = %s, group_list = %s' % (repr(criterion_group.criterion), repr(criterion_group.movement_list), repr(criterion_group.group_list)))
          for resource_group in criterion_group.group_list:
            LOG('mergeDeliveryList dump tree', 0, 'resource = %s, movement_list = %s, group_list = %s' % (repr(resource_group.resource), repr(resource_group.movement_list), repr(resource_group.group_list)))
            for base_variant_group in resource_group.group_list:
              LOG('mergeDeliveryList dump tree', 0, 'base_category_list = %s, movement_list = %s, group_list = %s' % (repr(base_variant_group.base_category_list), repr(base_variant_group.movement_list), repr(base_variant_group.group_list)))
              for variant_group in base_variant_group.group_list:
                LOG('mergeDeliveryList dump tree', 0, 'category_list = %s, movement_list = %s, group_list = %s' % (repr(variant_group.category_list), repr(variant_group.movement_list), repr(variant_group.group_list)))

2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660
        for criterion_group in root_group.group_list:
          for resource_group in criterion_group.group_list:
            for base_variant_group in resource_group.group_list:
              # Get a list of categories.
              category_dict = {}
              for variant_group in base_variant_group.group_list:
                for category in variant_group.category_list:
                  category_dict[category] = 1
              category_list = category_dict.keys()

              # Try to find a delivery line.
              delivery_line = None
              for movement in base_variant_group.movement_list:
                if movement in main_movement_list:
2661 2662
                  if movement.aq_parent.getPortalType() in self.getPortalSimulatedMovementTypeList() \
                    or movement.aq_parent.getPortalType() in self.getPortalInvoiceMovementTypeList():
2663 2664 2665 2666 2667
                    delivery_line = movement.aq_parent
                  else:
                    delivery_line = movement
                  LOG('mergeDeliveryList', 0, 'delivery_line %s is found: criterion = %s, resource = %s, base_category_list = %s' % (repr(delivery_line), repr(criterion_group.criterion), repr(resource_group.resource), repr(base_variant_group.base_category_list)))
                  break
2668

2669 2670 2671
              if delivery_line is None:
                # Not found. So create a new delivery line.
                movement = base_variant_group.movement_list[0]
2672 2673
                if movement.aq_parent.getPortalType() in self.getPortalSimulatedMovementTypeList() \
                  or movement.aq_parent.getPortalType() in self.getPortalInvoiceMovementTypeList():
2674
                  delivery_line_type = movement.aq_parent.getPortalType()
2675
                else:
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
                  delivery_line_type = movement.getPortalType()
                delivery_line = main_delivery.newContent(portal_type = delivery_line_type,
                                                         resource = resource_group.resource)
                LOG('mergeDeliveryList', 0, 'New delivery_line %s is created: criterion = %s, resource = %s, base_category_list = %s' % (repr(delivery_line), repr(criterion_group.criterion), repr(resource_group.resource), repr(base_variant_group.base_category_list)))

              # Update the base categories and categories.
              #LOG('mergeDeliveryList', 0, 'base_category_list = %s, category_list = %s' % (repr(base_category_list), repr(category_list)))
              delivery_line.setVariationBaseCategoryList(base_variant_group.base_category_list)
              delivery_line.setVariationCategoryList(category_list)

2686
              object_to_update = None
2687 2688 2689 2690 2691 2692
              for variant_group in base_variant_group.group_list:
                if len(variant_group.category_list) == 0:
                  object_to_update = delivery_line
                else:
                  for delivery_cell in delivery_line.contentValues():
                    predicate_value_list = delivery_cell.getPredicateValueList()
2693
                    LOG('mergeDeliveryList', 0, 'delivery_cell = %s, predicate_value_list = %s, variant_group.category_list = %s' % (repr(delivery_cell), repr(predicate_value_list), repr(variant_group.category_list)))
2694 2695 2696 2697 2698 2699 2700
                    if len(predicate_value_list) == len(variant_group.category_list):
                      for category in variant_group.category_list:
                        if category not in predicate_value_list:
                          break
                      else:
                        object_to_update = delivery_cell
                        break
2701

2702
                #LOG('mergeDeliveryList', 0, 'object_to_update = %s' % repr(object_to_update))
2703
                if object_to_update is not None:
2704
                  cell_price = object_to_update.getPrice() or 0.0
2705
                  cell_quantity = object_to_update.getQuantity() or 0.0
2706
                  cell_target_quantity = object_to_update.getNetConvertedTargetQuantity() or 0.0 # XXX What to do ?
2707
                  cell_total_price = cell_target_quantity * cell_price
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717
                  cell_category_list = list(object_to_update.getCategoryList())

                  for movement in variant_group.movement_list:
                    if movement in main_movement_list:
                      continue
                    LOG('mergeDeliveryList', 0, 'movement = %s' % repr(movement))
                    cell_quantity += movement.getQuantity()
                    cell_target_quantity += movement.getNetConvertedTargetQuantity()
                    try:
                      # XXX WARNING - ADD PRICED QUANTITY
2718 2719
                      cell_price = movement.getPrice()
                      cell_total_price += movement.getNetConvertedTargetQuantity() * cell_price
Yoshinori Okuji's avatar
Yoshinori Okuji committed
2720
                    except TypeError:
2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
                      cell_total_price = None
                    for category in movement.getCategoryList():
                      if category not in cell_category_list:
                        cell_category_list.append(category)
                    # Make sure that simulation movements point to an appropriate delivery line or
                    # delivery cell.
                    if hasattr(movement, 'getDeliveryRelatedValueList'):
                      for simulation_movement in \
                        movement.getDeliveryRelatedValueList(portal_type = 'Simulation Movement'):
                        simulation_movement.setDeliveryValue(object_to_update)
                        #simulation_movement.reindexObject()
                    if hasattr(movement, 'getOrderRelatedValueList'):
                      for simulation_movement in \
                        movement.getOrderRelatedValueList(portal_type = 'Simulation Movement'):
                        simulation_movement.setOrderValue(object_to_update)
                        #simulation_movement.reindexObject()

                  if cell_target_quantity != 0 and cell_total_price is not None:
                    average_price = cell_total_price / cell_target_quantity
                  else:
                    average_price = 0

2743
                  LOG('mergeDeliveryList', 0, 'object_to_update = %s, cell_category_list = %s, cell_target_quantity = %s, cell_quantity = %s, average_price = %s' % (repr(object_to_update), repr(cell_category_list), repr(cell_target_quantity), repr(cell_quantity), repr(average_price)))
2744
                  object_to_update.setCategoryList(cell_category_list)
2745
                  if object_to_update.getPortalType() in self.getPortalSimulatedMovementTypeList():
2746 2747 2748 2749
                    object_to_update.edit(target_quantity = cell_target_quantity,
                                          quantity = cell_quantity,
                                          price = average_price,
                                          )
2750
                  elif object_to_update.getPortalType() in self.getPortalInvoiceMovementTypeList():
2751 2752 2753 2754 2755 2756 2757
                    # Invoices do not have target quantities, and the price never change.
                    object_to_update.edit(quantity = cell_quantity,
                                          price = cell_price,
                                          )
                  else:
                    raise self.MergeDeliveryListError, "Unknown portal type %s" % str(object_to_update.getPortalType())
                  #object_to_update.immediateReindexObject()
2758 2759 2760 2761 2762
                else:
                  raise self.MergeDeliveryListError, "No object to update"

      # Merge containers. Just copy them from other deliveries into the main.
      for delivery in delivery_list:
2763
        container_id_list = delivery.contentIds(filter = {'portal_type': self.getPortalContainerTypeList()})
2764 2765 2766
        if len(container_id_list) > 0:
          copy_data = delivery.manage_copyObjects(ids = container_id_list)
          new_id_list = main_delivery.manage_pasteObjects(copy_data)
2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786

      # Unify the list of causality.
      causality_list = main_delivery.getCausalityValueList()
      for delivery in delivery_list:
        for causality in delivery.getCausalityValueList():
          if causality not in causality_list:
            causality_list.append(causality)
      LOG("mergeDeliveryList", 0, "causality_list = %s" % str(causality_list))
      main_delivery.setCausalityValueList(causality_list)

      # Cancel deliveries.
      for delivery in delivery_list:
        LOG("mergeDeliveryList", 0, "cancelling %s" % repr(delivery))
        delivery.cancel()

      # Reindex the main delivery.
      main_delivery.reindexObject()

      return main_delivery

2787 2788
    #######################################################
    # Sequence
Vincent Pelletier's avatar
Vincent Pelletier committed
2789
    security.declareProtected(Permissions.AccessContentsInformation,
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799
                              'getSequence')
    def getSequence(self, **kw):
      """
      getSequence is take the same parameters as Sequence constructor,
      and return a Sequence.
      """
      return Sequence(**kw)

    #######################################################
    # Time Management
Vincent Pelletier's avatar
Vincent Pelletier committed
2800
    security.declareProtected(Permissions.AccessContentsInformation,
2801
                              'getAvailableTime')
Vincent Pelletier's avatar
Vincent Pelletier committed
2802 2803
    def getAvailableTime(self, from_date=None, to_date=None,
                         portal_type=[], node=[],
2804
                         resource=[], src__=0, **kw):
2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816
      """
      Calculate available time for a node
      Returns an inventory of a single or multiple resources on a single
      node as a single float value

      from_date (>=) - only take rows which mirror_date is >= from_date

      to_date   (<)  - only take rows which date is < to_date

      node           - only take rows in stock table which node_uid is
                       equivalent to node

2817 2818 2819
      resource       - only take rows in stock table which resource_uid is
                       equivalent to resource

2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830
      portal_type    - only take rows in stock table which portal_type
                       is in portal_type parameter
      """
      # XXX For now, consider that from_date and to_date are required
      if (from_date is None) or (to_date is None):
        raise NotImplementedError, \
              "getAvailableTime does not managed yet None values"
      # Calculate portal_type
      if portal_type == []:
        portal_type = self.getPortalCalendarPeriodTypeList()

2831 2832 2833 2834
      simulation_state = self.getPortalCurrentInventoryStateList() + \
                         self.getPortalTransitInventoryStateList() + \
                         self.getPortalReservedInventoryStateList()

2835
      sql_result = self.Person_zGetAvailableTime(
2836 2837 2838 2839
                          from_date=from_date,
                          to_date=to_date,
                          portal_type=portal_type,
                          node=node,
2840
                          resource=resource,
2841
                          simulation_state=simulation_state,
2842 2843
                          src__=src__, **kw)
      if not src__:
2844
        result = 0
2845 2846 2847 2848
        if len(sql_result) == 1:
          result = sql_result[0].total_quantity
      else:
        result = sql_result
2849 2850
      return result

Vincent Pelletier's avatar
Vincent Pelletier committed
2851
    security.declareProtected(Permissions.AccessContentsInformation,
2852
                              'getAvailableTimeSequence')
Vincent Pelletier's avatar
Vincent Pelletier committed
2853
    def getAvailableTimeSequence(self, from_date, to_date,
2854
                                 portal_type=[], node=[],
Vincent Pelletier's avatar
Vincent Pelletier committed
2855
                                 resource=[],
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867
                                 src__=0,
                                 **kw):
      """
      Calculate available time for a node in multiple period of time.
      Each row is the available time for a specific period

      node           - only take rows in stock table which node_uid is
                       equivalent to node

      portal_type    - only take rows in stock table which portal_type
                       is in portal_type parameter

2868 2869 2870
      resource       - only take rows in stock table which resource_uid is
                       equivalent to resource

2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882
      from_date (>=) - return period which start >= from_date

      to_date   (<)  - return period which start < to_date

      second, minute,
      hour, day,
      month, year   - duration of each time period (cumulative)
      """
      # Calculate portal_type
      if portal_type == []:
        portal_type = self.getPortalCalendarPeriodTypeList()

2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893
      sequence = Sequence(from_date, to_date, **kw)
      for sequence_item in sequence:
        setattr(sequence_item, 'total_quantity',
                self.getAvailableTime(
                          from_date=sequence_item.from_date,
                          to_date=sequence_item.to_date,
                          portal_type=portal_type,
                          node=node,
                          resource=resource,
                          src__=src__))
      return sequence
2894

2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906
    def _checkExpandAll(self, activate_kw={}):
      """Check all simulation trees using AppliedRule._checkExpand
      """
      portal = self.getPortalObject()
      active_process = portal.portal_activities.newActiveProcess().getPath()
      kw = dict(priority=3, tag='checkExpand')
      kw.update(group_method_cost=1, max_retry=0,
                active_process=active_process, **activate_kw)
      self._recurseCallMethod('_checkExpand', min_depth=1, max_depth=1,
                              activate_kw=kw)
      return active_process

2907
from Products.ERP5Type.DateUtils import addToDate
2908 2909 2910 2911 2912 2913 2914 2915 2916 2917

class SequenceItem:
  """
  SequenceItem define a time period.
  period.
  """
  def __init__(self, from_date, to_date):
    self.from_date = from_date
    self.to_date = to_date

2918 2919 2920 2921 2922
class Sequence:
  """
  Sequence is a iterable object, which calculate a range of time
  period.
  """
Vincent Pelletier's avatar
Vincent Pelletier committed
2923
  def __init__(self, from_date, to_date,
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940
               second=None, minute=None, hour=None,
               day=None, month=None, year=None):
    """
    Calculate a list of time period.
    Time period is a 2-tuple of 2 DateTime, which represent the from date
    and to date of the period.

    The start date of a period is calculated with the rule
        start_date of the previous + period duration

    from_date (>=) - return period which start >= from_date

    to_date   (<)  - return period which start < to_date

    second, minute,
    hour, day,
    month, year   - duration of each time period (cumulative)
2941
                    at least one of those parameters must be specified.
2942
    """
2943 2944 2945
    if not (second or minute or hour or day or month or year):
      raise ValueError('Period duration must be specified')

2946 2947 2948 2949
    self.item_list = []
    # Calculate all time period
    current_from_date = from_date
    while current_from_date < to_date:
Vincent Pelletier's avatar
Vincent Pelletier committed
2950
      current_to_date = addToDate(current_from_date,
2951 2952 2953 2954 2955 2956
                                  second=second,
                                  minute=minute,
                                  hour=hour,
                                  day=day,
                                  month=month,
                                  year=year)
Vincent Pelletier's avatar
Vincent Pelletier committed
2957
      self.item_list.append(SequenceItem(current_from_date,
2958
                                         current_to_date))
2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975
      current_from_date = current_to_date

  def __len__(self):
    return len(self.item_list)

  def __getitem__(self, key):
    return self.item_list[key]

  def __contains__(self, value):
    return (value in self.item_list)

  def __iter__(self):
    for x in self.item_list:
      yield x

InitializeClass(Sequence)
allow_class(Sequence)
2976 2977
InitializeClass(SequenceItem)
allow_class(SequenceItem)
2978

2979
InitializeClass(SimulationTool)