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
42
43
44
45
46
47
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
278
279
280
281
282
283
284
285
286
287
288
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
##############################################################################
#
# 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
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Base, Permissions, PropertySheet, Constraint, Interface
from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5.Document.Amount import Amount
from Products.ERP5.Document.Movement import Movement
from Products.ERP5Type.DateUtils import millis
from string import capitalize
from zLOG import LOG
UNIMMOBILISING_METHOD = "unimmobilise"
NO_CHANGE_METHOD = "no_change"
AMORTISATION_METHOD_PREFIX = "portal_skins/erp5_accounting_"
IMMOBILISATION_NEEDED_PROPERTY_LIST = [
("date", "stop_date", "Date"),
("method", "amortisation_method", "Amortisation Method"),
]
IMMOBILISATION_UNCONTINUOUS_NEEDED_PROPERTY_LIST = [
("main_price", "amortisation_start_price", "Immobilised Price"),
("disposal_price", "disposal_price", "Disposal Price"),
("duration", "amortisation_duration", "Amortisation Duration"),
("vat", "immobilisation_vat", "VAT"),
("input_account", "input_account", "Input Account"),
("output_account", "output_account", "Output Account"),
("immobilisation_account", "immobilisation_account", "Immobilisation Account"),
("amortisation_account", "amortisation_account", "Amortisation Account"),
("depreciation_account", "depreciation_account", "Depreciation Account"),
("vat_account", "immobilisation_vat_account", "VAT Account"),
("extra_cost_account", "extra_cost_account", "Extra Costs Account"),
]
IMMOBILISATION_FACULTATIVE_PROPERTY_LIST = [
("monthly_amortisation_account", "monthly_amortisation_account", "Monthly Amortisation Account"),
("extra_cost_price", "extra_cost_price", "Extra Costs Price"),
("durability", "durability", "Durability"),
]
class ImmobilisationMovement(Movement, XMLObject):
"""
An ImmobilisationMovement is a generic object representing an immobilisation
and optional amortisation decision for any item. It holds information about
an accounting immobilisation (in order to amortise an object)
"""
meta_type = 'ERP5 Immobilisation Movement'
portal_type = 'Immobilisation Movement'
add_permission = Permissions.AddPortalContent
isPortalContent = 1
isRADContent = 1
# Declarative security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation)
# Declarative interfaces
__implements__ = ( Interface.Variated, )
# Declarative properties
property_sheets = ( PropertySheet.Base
, PropertySheet.XMLObject
, PropertySheet.CategoryCore
, PropertySheet.DublinCore
, PropertySheet.Task
, PropertySheet.Arrow
, PropertySheet.Movement
, PropertySheet.Delivery
, PropertySheet.Reference
, PropertySheet.Amount
, PropertySheet.Price
, PropertySheet.Amortisation
)
def _checkImmobilisationConsistency(self, fixit=0, mapped_value_property_list=(), to_translate=1):
"""
If to_translate is set, the method may return a dictionary {'msg':'...', 'mapping':{} }
"""
relative_url = self.getRelativeUrl()
def checkValuesAreNotNone(property_list):
errors = []
for key, value, name in property_list:
value = 'get' + ''.join(map(capitalize, value.split('_')))
value = getattr(self, value, None)
if value is not None:
value = value()
if key == 'method' and not value:
value = NO_CHANGE_METHOD
errors.extend(checkValue(variable=value,
forbidden_value_list=[None],
error_message=["Property value inconsistency", 0, "%s property is empty" % name]
)
)
return errors
def checkValue(variable,
forbidden_value_list=None,
authorized_value_list=None,
error_message=["Error Type", 0, "Error message"]):
if forbidden_value_list is not None:
if type(forbidden_value_list) != type([]):
forbidden_value_list = [forbidden_value_list]
if variable in forbidden_value_list:
return [ tuple([relative_url] + error_message) ]
if authorized_value_list is not None:
if type(authorized_value_list) != type([]):
authorized_value_list = [authorized_value_list]
if variable not in authorized_value_list:
return [ tuple([relative_url] + error_message) ]
return []
errors = []
# Check absolutely needed values
method = self.getAmortisationMethod() or NO_CHANGE_METHOD
errors.extend(checkValuesAreNotNone(IMMOBILISATION_NEEDED_PROPERTY_LIST))
property_list = self.getNeededSpecificParameterListForItem(None)
errors.extend(checkValuesAreNotNone(property_list))
# Check if the date of this movement is unique
date_error = 0
for item in self.getAggregateValueList():
same_date_list = item.getUnfilteredImmobilisationMovementValueList(
from_date = self.getStopDate(),
to_date = self.getStopDate(),
include_to_date = 1)
error_found = 0
for other_movement in same_date_list:
if other_movement != self and other_movement.getRootDeliveryValue().getImmobilisationState() == 'valid':
error_found = 1
date_error = 1
if error_found:
if to_translate:
msg = {'msg':"An other movement already exists at the same date for item ${item}",
'mapping': {'item':item.getRelativeUrl()}
}
else:
msg = "An other movement alreay exists at the same date for item %s" % item.getRelativeUrl()
errors.append([self.getRelativeUrl(),
"Property value inconsistency", 0,
msg
])
# Return to avoid infinite loops in case of date errors
if date_error:
return errors
item_list = self.getAggregateValueList()
if len(item_list) == 0:
# No item aggregated, so the movement is considered as valid
#errors.append([self.getRelativeUrl(),
# "Property value inconsistency", 0,
# "No item aggregated"])
return errors
# Check values needed if the amortisation method is not continuous
check_uncontinuous = 0
if self.getStopDate() is not None:
if method not in [None, "", NO_CHANGE_METHOD, UNIMMOBILISING_METHOD]:
get_amo_method_parameter = self.getAmortisationMethodParameter("continuous")
continuous = get_amo_method_parameter["continuous"]
if not continuous:
check_uncontinuous = 1
# We need to check if the preceding movement is in the same period, and valid
# This check must be done on each item
else:
# We need to check if the preceding movement is in the same period, and if it is valid
# This check must be done on each item
def checkPreviousMovementForItem(movement, item):
previous_movement = item.getLastImmobilisationMovementValue(at_date=movement.getStopDate())
if previous_movement is None:
return 0
if previous_movement.getImmobilisationState() == 'valid':
if previous_movement.getAmortisationMethod() not in ("", NO_CHANGE_METHOD):
return 1
return checkPreviousMovementForItem(previous_movement, item)
return checkPreviousMovementForItem(previous_movement, item)
for item in self.getAggregateValueList():
if not checkPreviousMovementForItem(self,item):
check_uncontinuous = 1
else:
# The last movement which is not a NO_CHANGE is valid
# Now check if the method is the same, then if the period is really continuing from previous movement
previous_movement = item.getLastImmobilisationMovementValue(at_date=self.getStopDate())
previous_movement_method = previous_movement.getActualAmortisationMethodForItem(item)
if previous_movement_method != method:
check_uncontinuous = 1
# If the previous method is the same, it means the previous movement did
# not stop the immobilisation, because stopping is a particular method
if check_uncontinuous:
errors.extend(checkValuesAreNotNone(IMMOBILISATION_UNCONTINUOUS_NEEDED_PROPERTY_LIST))
property_list = self.getUncontinuousNeededSpecificParameterListForItem(None)
errors.extend(checkValuesAreNotNone(property_list))
# Do not check facultative properties (of course)
# Check owner change for each aggregated item
# XXX Checking it here would be inadequate, since the owner can change by multiple ways :
# adding a new movement for this item, changing it, modifying related organisation, etc...
# It is not compatible with a validity workflow approach since the causalities are too numerous
# The actual check is done in ImmobilisableItem.getImmobilisationPeriodList()
return errors
security.declareProtected(Permissions.View, 'checkImmobilisationConsistency')
def checkImmobilisationConsistency(self, *args, **kw):
"""
Checks the consistency about immobilisation values
"""
return_value = self._checkImmobilisationConsistency(*args, **kw)
return return_value
security.declareProtected(Permissions.View, 'getAmortisationMethodParameter')
def getAmortisationMethodParameter(self, parameter_list, **kw):
"""
Returns a dictionary containing the value of each parameter
whose name is given in parameter_list.
Warning : can only be used on a movement whose amortisation_method
is not in (None, NO_CHANGE_METHOD)
"""
if self.getAmortisationMethod() in (None, "", NO_CHANGE_METHOD):
return None
return self.getAmortisationMethodParameterForItem(None, parameter_list, **kw)
security.declareProtected(Permissions.View, 'getAmortisationMethodParameterForItem')
def getAmortisationMethodParameterForItem(self, item, parameter_list, split_char=None, split_qty=3, **kw):
"""
Returns a dictionary containing the value of each parameter
whose name is given in parameter_list.
The value is get from the amortisation method parameter folder
(e.g. portal_skins/erp5_accounting_eu/linear)
This folder has specifical parameters needed for calculation
'item' can be None to access parameters on a movement whose method is not NO_CHANGE_METHOD nor None
"""
parameter_dict = {}
if type(parameter_list) == type(""):
parameter_list = [parameter_list]
for parameter in parameter_list:
parameter_dict[parameter] = None
amortisation_method = self.getActualAmortisationMethodForItem(item, **kw)
if amortisation_method not in (None, NO_CHANGE_METHOD, UNIMMOBILISING_METHOD, ""):
parameter_object = self.getPortalObject().unrestrictedTraverse(AMORTISATION_METHOD_PREFIX + amortisation_method)
if parameter_object is not None:
for parameter in parameter_list:
parameter_dict[parameter] = getattr(parameter_object, parameter, None)
if (split_char is not None) and (split_qty != 0):
new_parameter_dict = {}
for key in parameter_dict.keys():
param_list = parameter_dict[key]
if param_list is None:
new_parameter_dict[key] = []
if type(param_list) != type([]) and type(param_list) != type(()):
param_list = [param_list]
new_param_list = []
for param in param_list:
if param is not None:
if param.find(split_char) != -1:
param = param.split(split_char)
param = [x.strip() for x in param]
if type(param) != type([]) and type(param) != type(()):
param = [param]
if len(param) > split_qty:
param = param[:split_qty]
while len(param) < split_qty:
param.append(param[-1])
new_param_list.append(param)
new_parameter_dict[key] = new_param_list
parameter_dict = new_parameter_dict
return parameter_dict
security.declareProtected(Permissions.View, 'getActualAmortisationMethodForItem')
def getActualAmortisationMethodForItem(self, item, **kw):
"""
Returns the actual amortisation method by getting the previous
movement method if the current one is NO_CHANGE_METHOD
"""
method = self.getAmortisationMethod()
if method not in (None, "", NO_CHANGE_METHOD):
return method
stop_date = self.getStopDate()
if stop_date is None or item is None:
return None
previous_movement_list = item.getPastImmobilisationMovementValueList(at_date=stop_date, **kw)
if previous_movement_list is None:
return None
for i in range(len(previous_movement_list)-1, -1, -1):
movement = previous_movement_list[i]
if movement.getAmortisationMethod() not in (None, "", NO_CHANGE_METHOD):
return movement.getAmortisationMethod()
return None
security.declareProtected(Permissions.View, 'getNeededSpecificParameterListForItem')
def getNeededSpecificParameterListForItem(self, item, **kw):
"""
Returns the list of specific parameters which are
needed for the amortisation calculation for the given item
"""
return self.getAmortisationMethodParameterForItem(item=item,
parameter_list="needed_specific_parameter_list",
split_char = '|',
split_qty = 3,
**kw)["needed_specific_parameter_list"]
security.declareProtected(Permissions.View, 'getUncontinuousNeededSpecificParameterListForItem')
def getUncontinuousNeededSpecificParameterListForItem(self, item, **kw):
"""
Returns the list of specific parameters which are
needed for the amortisation calculation for the given item
when the amortisation is not continuing the previous period
"""
return self.getAmortisationMethodParameterForItem(item=item,
parameter_list="uncontinuous_needed_specific_parameter_list",
split_char = '|',
split_qty = 3,
**kw)["uncontinuous_needed_specific_parameter_list"]
security.declareProtected(Permissions.View, 'getFacultativeSpecificParameterListForItem')
def getFacultativeSpecificParameterListForItem(self, item, **kw):
"""
Returns the list of specific parameters which are
facultative for the amortisation calculation for the given item
"""
return self.getAmortisationMethodParameterForItem(item=item,
parameter_list="facultative_specific_parameter_list",
split_char = '|',
split_qty = 3,
**kw)["facultative_specific_parameter_list"]
security.declareProtected(Permissions.View, 'isUsingAmortisationMethod')
def isUsingAmortisationMethod(self, method):
"""
Return true if this item is using the given method
"""
if self.getAmortisationMethod() == method:
return 1
return 0
security.declareProtected(Permissions.View, 'getImmobilisationState')
def getImmobilisationState(self, id_only=1):
"""
Return root delivery immobilisation state
"""
return self.getRootDeliveryValue().getImmobilisationState()