ImmobilisationDelivery.py 6.51 KB
Newer Older
Guillaume Michon's avatar
Guillaume Michon committed
1 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 32 33 34 35 36 37 38 39 40 41
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
#                    Guillaume Michon        <guillaume.michon@e-asc.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 Globals import InitializeClass, PersistentMapping
from AccessControl import ClassSecurityInfo

from DateTime import DateTime
from string import capitalize

from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5Type.DateUtils import addToDate, getClosestDate, getIntervalBetweenDates 
from Products.ERP5Type.DateUtils import getMonthAndDaysBetween, getRoundedMonthBetween
from Products.ERP5Type.DateUtils import getMonthFraction, getYearFraction, getBissextilCompliantYearFraction
from Products.ERP5Type.DateUtils import same_movement_interval, number_of_months_in_year, centis, millis
from Products.ERP5.Document.Amount import Amount
42
from Products.ERP5Type.Base import WorkflowMethod
Guillaume Michon's avatar
Guillaume Michon committed
43 44 45 46
from Products.CMFCore.utils import getToolByName
from Products.ERP5.Document.ImmobilisableItem import ImmobilisationValidityError


47
from zLOG import LOG, WARNING
Guillaume Michon's avatar
Guillaume Michon committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80


NEGLIGEABLE_PRICE = 10e-8

class ImmobilisationDelivery(XMLObject):
    """
      An Immobilisation Delivery is an object whose role is to
      contain delivery movements which can immobilise items.
    """

    meta_type = 'ERP5 Immobilisation Delivery'
    portal_type = 'Immobilisation Delivery'
    add_permission = Permissions.AddPortalContent
    isPortalContent = 1
    isRADContent = 1

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

    # Declarative properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      , PropertySheet.Task
                      , PropertySheet.Arrow
                      , PropertySheet.Movement
                      , PropertySheet.Delivery
                      , PropertySheet.Reference
                      )

    security.declareProtected(Permissions.View, 'updateImmobilisationState')
81
    def updateImmobilisationState(self, **kw):
Guillaume Michon's avatar
Guillaume Michon committed
82 83 84 85 86 87 88 89 90
      """
      This is often called as an activity, it will check if the
      delivery is valid as an immobilisation movement, and if so
      it will put the delivery in a valid state, if not valid in
      an invalid state
      """
      if self.getImmobilisationState() == 'calculating':
        try:
          if self.isValidImmobilisationMovement(**kw):
91
            self.validateImmobilisation()
Guillaume Michon's avatar
Guillaume Michon committed
92
          else:
93
            self.invalidateImmobilisation()
Guillaume Michon's avatar
Guillaume Michon committed
94
        except ImmobilisationValidityError:
95
          self.calculateImmobilisationValidity()
Guillaume Michon's avatar
Guillaume Michon committed
96 97 98 99 100 101 102 103 104 105 106 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 145 146 147 148 149 150 151 152 153 154

    security.declareProtected(Permissions.View, 'getImmobilisationMovementList')
    def getImmobilisationMovementList(self, **kw):
      """
      Return regular movements + immobilisation movements like
      Immobilisation Line and Immobilisation Cell
      """
      return self.getMovementList(self.getPortalMovementTypeList() +
                 ('Immobilisation Line', 'Immobilisation Cell'), **kw)
      
    security.declareProtected(Permissions.View, 'checkImmobilisationConsistency')
    def checkImmobilisationConsistency(self, *args, **kw):
      """
      Check the consistency about immobilisation values
      """
      return_list = []
      for movement in self.getImmobilisationMovementList():
        return_list.extend(movement.checkImmobilisationConsistency())
      return return_list

    security.declareProtected(Permissions.View, 'isValidImmobilisationMovement')
    def isValidImmobilisationMovement(self, *args, **kw):
      """
      Return true if all submovements are valid in terms of immobilisation
      """
      error_list = self.checkImmobilisationConsistency(*args, **kw)
      if len(error_list) == 0:
        return 1
      return 0

    security.declareProtected(Permissions.View, 'isInvalidImmobilisationMovement')
    def isInvalidImmobilisationMovement(self, *args, **kw):
      """
      Return false if all submovements are valid in terms of immobilisation
      """
      return not self.isValidImmobilisationMovement(*args, **kw)

    security.declareProtected(Permissions.View, 'getAggregatedItemsNextImmobilisationMovementValueList')
    def getAggregatedItemsNextImmobilisationMovementValueList(self, **kw):
      """
      Return the list of each next immobilisation movement for each aggregated item
      """
      returned_list = []
      sub_movement_list = self.contentValues()
      for movement in self.getImmobilisationMovementList(**kw):
        for item in movement.getAggregateValueList():
          future_movement_list = item.getFutureImmobilisationMovementValueList(
                                     at_date = self.getStopDate(),
                                     from_movement = self,
                                     filter_valid = 0)
          if future_movement_list is not None:
            for next_movement in future_movement_list:
              if next_movement is not None and \
                 next_movement not in sub_movement_list and \
                 next_movement not in returned_list and \
                 next_movement.getStopDate() != self.getStopDate():
                returned_list.append(next_movement)
      return returned_list