TradeModelLine.py 5.35 KB
Newer Older
Yusei Tahara's avatar
Yusei Tahara committed
1
# -*- coding: utf-8 -*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
##############################################################################
#
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#                    Łukasz Nowak <luke@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from AccessControl import ClassSecurityInfo

32
from Products.ERP5Type import Permissions, PropertySheet, Constraint, interfaces
33
from Products.ERP5Type.XMLMatrix import XMLMatrix
34 35

from Products.ERP5.Document.Amount import Amount
36
from Products.ERP5.Variated import Variated
37 38

from Products.ERP5.AggregatedAmountList import AggregatedAmountList
39

40 41
import zope.interface

42
class TradeModelLine(XMLMatrix, Amount, Variated):
43 44 45 46 47 48 49 50 51 52
    """Trade Model Line
    """
    meta_type = 'ERP5 Trade Model Line'
    portal_type = 'Trade Model Line'

    # Declarative security
    security = ClassSecurityInfo()
    security.declareObjectProtected(Permissions.AccessContentsInformation)

    # Declarative interfaces
53
    # interfaces.IVariated - as soon as interfaces.IVariated will be zope3
54
    zope.interface.implements(
55
        interfaces.ITransformation
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    )

    # Declarative properties
    property_sheets = ( PropertySheet.Base
                    , PropertySheet.SimpleItem
                    , PropertySheet.Amount
                    , PropertySheet.Price
                    , PropertySheet.TradeModelLine
                    )

    def getPrice(self):
      return self._baseGetPrice()

    def updateAggregatedAmountList(self, context, **kw):
      raise NotImplementedError('TODO')

    def getAggregatedAmountList(self, context, movement_list = None,
        current_aggregated_amount_list = None, **kw):
      from Products.ERP5Type.Document import newTempMovement

76 77 78 79 80
      normal_resource_use_category_list = self.\
          portal_preferences.getPreferredNormalResourceUseCategoryList()
      if normal_resource_use_category_list is None:
        raise ValueError('preferred_normal_resource_use_category is not ' + \
            'configured in System Preferences')
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
      if current_aggregated_amount_list is None:
        current_aggregated_amount_list = []

      if movement_list is None:
        movement_list = []

      if len(movement_list) == 0:
        if context.getPortalType() == 'Applied Rule':
          movement_list = [context.getParentValue()]
        else:
          if callable(context.isMovement):
            is_movement = context.isMovement()
          else:
            is_movement = context.isMovement
          if is_movement:
            # we need to create aggreageted amount on context itself
            movement_list = [context]
          else:
Łukasz Nowak's avatar
Łukasz Nowak committed
99
            # XXX: filtering shall be in getMovementList
100 101 102 103
            movement_list = []
            for movement in context.getMovementList():
              movement_resource = movement.getResourceValue()
              if movement_resource is not None:
104 105 106
                if movement_resource.getUse() in \
                    normal_resource_use_category_list:
                  movement_list.append(movement)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
      aggregated_amount_list = AggregatedAmountList()
      base_application_list = self.getBaseApplicationList()

      self_id = self.getParentValue().getId() + '_' + self.getId()

      tmp_movement = [q for q in current_aggregated_amount_list \
          if q.getProperty('resource') == self.getResource() ]
      if len(tmp_movement) > 0:
        tmp_movement = tmp_movement[0]
        update = 1
      else:
        update = 0
        tmp_movement = newTempMovement(self.getPortalObject(), self_id )
        tmp_movement.edit(
          causality = self.getRelativeUrl(),
          resource = self.getResource(),
          base_application_list = base_application_list,
          base_contribution_list = self.getBaseContributionList(),
          price = self.getPrice(),
          trade_phase_list = self.getTradePhaseList(),
        )

      modified = 0
      for movement in movement_list:
        if set(base_application_list)\
            .intersection(set(movement.getBaseContributionList())):
          quantity = tmp_movement.getQuantity(0.0)
          modified = 1
          tmp_movement.edit(
            quantity = quantity + movement.getTotalPrice()
          )

      if not update:
        if modified:
          aggregated_amount_list.append(tmp_movement)

      return aggregated_amount_list