SubscriptionItem.py 11.4 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

Łukasz Nowak's avatar
Łukasz Nowak committed
33
from Products.CMFCore.utils import getToolByName
34
from Products.ERP5Type import Permissions, PropertySheet, interfaces
35
from Products.ERP5.Document.Item import Item
36
from Products.ERP5.mixin.composition import CompositionMixin
37
from Products.ERP5.mixin.rule import MovementGeneratorMixin, SimulableMixin
38
from Products.ERP5.mixin.periodicity import PeriodicityMixin
39 40
from Products.ERP5Type.UnrestrictedMethod import UnrestrictedMethod
from Products.ERP5Type.Base import Base
41

Jean-Paul Smets's avatar
Jean-Paul Smets committed
42 43
from zLOG import LOG

44 45
class SubscriptionItem(Item, CompositionMixin, MovementGeneratorMixin,
                       SimulableMixin, PeriodicityMixin):
46
  """
47 48
    A SubscriptionItem is an Item which expands itself
    into simulation movements which represent the item future.
49
    Examples of subscription items (or subclasses) include:
50 51
    employee paysheet contracts, telecommunication subscriptions,
    banking service subscriptions, etc
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  """
  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
69
                    , PropertySheet.Periodicity
70 71
                    )

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

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

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

85 86 87 88 89 90 91 92 93 94
    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
95 96 97 98 99 100
  def isSimulated(self):
    """
      We are never simulated (unlike deliveries)
    """
    return False

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

Jean-Paul Smets's avatar
Jean-Paul Smets committed
106
  def _getInputMovementList(self, movement_list=None, rounding=None):
107
    """
108 109 110 111 112
      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)
113
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
114
    from Products.ERP5Type.Document import newTempMovement
115
    result = []
116
    catalog_tool = getToolByName(self, 'portal_catalog')
117

118
    # Now generate movements for each valid open order
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'),),
        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
        if start_date is None or stop_date is None or start_date>=stop_date:
130 131
          # 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()
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
        while current_date < stop_date:
150
          next_date = self.getNextPeriodicalDate(current_date)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
151 152
          generated_movement = newTempMovement(self, 'subscription_%s' % id_index)
          generated_movement._edit(  aggregate_value=self,
153 154 155 156
                                     resource=resource,
                                     quantity=quantity,
                                     quantity_unit=quantity_unit,
                                     price=price,
157
                                     price_currency=price_currency,
158
                                     start_date=current_date,
159
                                     stop_date=next_date,
160 161
                                     source=source,
                                     source_section=source_section,
162
                                     source_decision=source_decision,
163 164
                                     destination=destination,
                                     destination_section=destination_section,
165
                                     destination_decision=destination_decision,
Jean-Paul Smets's avatar
Jean-Paul Smets committed
166
                                     specialise=specialise,
Łukasz Nowak's avatar
Łukasz Nowak committed
167 168 169
                                     base_application_list=base_application_list,
                                     base_contribution_list=base_contribution_list,
                                     use_list=use_list
170
                                    )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
171
          result.append(generated_movement)
172
          current_date = next_date
Jean-Paul Smets's avatar
Jean-Paul Smets committed
173
          id_index += 1
174

Jérome Perrin's avatar
Jérome Perrin committed
175
    return result
176 177 178 179 180 181 182 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 232 233 234 235 236 237 238 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 274 275 276 277

  # 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()

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

  def getPrice(self):
    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()

  def _getCategoryMembershipList(self, category, spec=(), filter=None,
      portal_type=(), base=0, keep_default=1, checked_permission=None, **kw):
    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)