document.erp5.SubscriptionItem.py 12 KB
Newer Older
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
##############################################################################
#
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#                    Jean-Paul Smets-Solanes <jp@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.
#
##############################################################################

30
import zope.interface
31 32
from AccessControl import ClassSecurityInfo

33
from Products.CMFCore.utils import getToolByName
34
from Products.ERP5Type import Permissions, PropertySheet
35
from erp5.component.document.Item import Item
36
from erp5.component.mixin.CompositionMixin import CompositionMixin
37
from erp5.component.mixin.SimulableMixin import SimulableMixin
38
from erp5.component.mixin.MovementGeneratorMixin import MovementGeneratorMixin
39
from Products.ERP5.mixin.periodicity import PeriodicityMixin
40
from Products.ERP5Type.Base import Base
41
from erp5.component.interface.IMovementGenerator import IMovementGenerator
42

43 44
class SubscriptionItem(Item, CompositionMixin, MovementGeneratorMixin,
                       SimulableMixin, PeriodicityMixin):
45
  """
46 47
    A SubscriptionItem is an Item which expands itself
    into simulation movements which represent the item future.
48
    Examples of subscription items (or subclasses) include:
49 50
    employee paysheet contracts, telecommunication subscriptions,
    banking service subscriptions, etc
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  """
  meta_type = 'ERP5 Subscription Item'
  portal_type = 'Subscription Item'

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

  # Declarative properties
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.XMLObject
                    , PropertySheet.CategoryCore
                    , PropertySheet.DublinCore
                    , PropertySheet.Price
                    , PropertySheet.Item
                    , PropertySheet.Amount
                    , PropertySheet.Reference
68
                    , PropertySheet.Periodicity
69 70
                    )

71
  # Declarative interfaces
72
  zope.interface.implements(IMovementGenerator,
73 74
                           )

75
  def _createRootAppliedRule(self):
76
    # only try to expand if we are not in draft state
Jean-Paul Smets's avatar
Jean-Paul Smets committed
77
    if self.getValidationState() in ('draft', ): # XXX-JPS harcoded
78
      return
79
    return super(SubscriptionItem, self)._createRootAppliedRule()
80

81 82
  def getSimulationMovementSimulationState(self, simulation_movement):
    """Returns the simulation state for this simulation movement.
83

84 85 86 87 88 89 90 91 92 93
    This generic implementation assumes that if there is one open order line
    which is validated or archived, the movements will be planned. This
    behaviour might have to be adapted in subclasses.
    """
    for path in self.getAggregateRelatedValueList(
        portal_type=self.getPortalObject().getPortalSupplyPathTypeList(),):
      if path.getValidationState() in ('validated', 'archived'):
        return 'planned'
    return 'draft'

Jean-Paul Smets's avatar
Jean-Paul Smets committed
94 95 96 97 98 99
  def isSimulated(self):
    """
      We are never simulated (unlike deliveries)
    """
    return False

100
  # IMovementGenerator interface implementation
Jean-Paul Smets's avatar
Jean-Paul Smets committed
101 102 103 104
  def _getUpdatePropertyDict(self, input_movement):
    # Default implementation bellow can be overriden by subclasses
    return {}

Jean-Paul Smets's avatar
Jean-Paul Smets committed
105
  def _getInputMovementList(self, movement_list=None, rounding=None):
106
    """
107 108 109 110 111
      Generate the list of input movements by looking at all
      open order lines relating to this subscription item.

      TODO: clever handling of quantity (based on the nature
      of resource, ie. float or unit)
112
    """
113
    result = []
114
    catalog_tool = getToolByName(self, 'portal_catalog')
115

116
    # Try to find the source open order
117 118 119 120 121 122 123 124 125
    for movement in catalog_tool(portal_type="Open Sale Order Line",
        default_aggregate_uid=self.getUid(),
        validation_state=('open', 'validated', 'archived'), # XXX-JPS hard coding
        sort_on=(('effective_date', 'descending'),
                # Do not return archived if effective dates are identical
                ('validation_state', 'descending')),
        limit=1 # Note Luke: Support the newest Open Order which defines
                # something for current subscription item
        ): # YXU-Why we have a list here?
126
        resource = movement.getResource()
127 128
        start_date = movement.getStartDate()
        stop_date = movement.getStopDate()
129 130 131
        if start_date is None or stop_date is None or start_date>=stop_date:
          # infinity nor time back machine does not exist
          continue
132 133
        source = movement.getSource()
        source_section = movement.getSourceSection()
134
        source_decision = movement.getSourceDecision()
135
        destination = movement.getDestination()
136
        destination_section = movement.getDestinationSection()
137
        destination_decision = movement.getDestinationDecision()
138
        quantity = movement.getQuantity()
139 140
        quantity_unit = movement.getQuantityUnit()
        price = movement.getPrice()
141
        price_currency = movement.getPriceCurrency()
Łukasz Nowak's avatar
Łukasz Nowak committed
142
        base_application_list = movement.getBaseApplicationList()
Alain Takoudjou's avatar
Alain Takoudjou committed
143
        base_contribution_list = movement.getBaseContributionList()
Łukasz Nowak's avatar
Łukasz Nowak committed
144
        use_list = movement.getUseList()
145

Jean-Paul Smets's avatar
Jean-Paul Smets committed
146
        specialise = movement.getSpecialise()
147
        current_date = start_date
Jean-Paul Smets's avatar
Jean-Paul Smets committed
148
        id_index = 0
149 150
        while current_date < stop_date:
          next_date = self.getNextPeriodicalDate(current_date)
151 152 153
          generated_movement = self.newContent(temp_object=True,
                                               portal_type='Movement',
                                               id='subscription_%s' % id_index)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
154
          generated_movement._edit(  aggregate_value=self,
155 156 157 158
                                     resource=resource,
                                     quantity=quantity,
                                     quantity_unit=quantity_unit,
                                     price=price,
159
                                     price_currency=price_currency,
160
                                     start_date=current_date,
161
                                     stop_date=next_date,
162 163
                                     source=source,
                                     source_section=source_section,
164
                                     source_decision=source_decision,
165 166
                                     destination=destination,
                                     destination_section=destination_section,
167
                                     destination_decision=destination_decision,
Jean-Paul Smets's avatar
Jean-Paul Smets committed
168
                                     specialise=specialise,
Łukasz Nowak's avatar
Łukasz Nowak committed
169 170 171
                                     base_application_list=base_application_list,
                                     base_contribution_list=base_contribution_list,
                                     use_list=use_list
172
                                    )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
173
          result.append(generated_movement)
174
          current_date = next_date
Jean-Paul Smets's avatar
Jean-Paul Smets committed
175
          id_index += 1
176

Jérome Perrin's avatar
Jérome Perrin committed
177
    return result
178

179 180 181 182
  def updateExpandableRootSimulation(self):
    """ Utility method to help use updateSimulation with SQLDict on activities """
    self.updateSimulation(expand_root=1)

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  # XXX BELOW HACKS
  def getResource(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getResource()

  def getStartDate(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getStartDate()

  def getStopDate(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getStopDate()

  def getSource(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getSource()

  def getSourceSection(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getSourceSection()

  def getDestination(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getDestination()

  def getDestinationSection(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getDestinationSection()

  def getQuantity(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getQuantity()

232
  def getQuantityUnit(self, checked_permission=None):
233 234 235
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
236
    return open_order_line.getQuantityUnit(checked_permission=checked_permission)
237

238
  def getPrice(self, context=None):
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getPrice()

  def getPriceCurrency(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getPriceCurrency()

  def getSpecialise(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getSpecialise()

  def getSpecialiseList(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return []
    return open_order_line.getSpecialiseList()

  def getSpecialiseValue(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return None
    return open_order_line.getSpecialiseValue()

  def getSpecialiseValueList(self):
    open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
    if open_order_line is None:
      return []
    return open_order_line.getSpecialiseValueList()

274 275 276 277 278 279 280 281 282 283
  def _getCategoryMembershipList(
      self,
      category,
      spec=(),
      filter=None, #  pylint:disable=redefined-builtin
      portal_type=(),
      base=0,
      keep_default=1,
      checked_permission=None,
      **kw):
284 285 286 287 288 289 290 291
    if category == 'specialise':
      open_order_line = self.getAggregateRelatedValue(portal_type='Open Sale Order Line')
      return open_order_line._getCategoryMembershipList(category, spec=spec, filter=filter,
                             portal_type=portal_type, base=base, keep_default=keep_default,
                             checked_permission=checked_permission, **kw)
    return Base._getCategoryMembershipList(self, category, spec=spec, filter=filter,
                portal_type=portal_type, base=base, keep_default=keep_default,
                checked_permission=checked_permission, **kw)