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
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
# Łukasz Nowak <luke@nexedi.com>
# Fabien Morin <fabien@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
from Products.ERP5Type import Permissions, PropertySheet, interfaces
from Products.ERP5Type.XMLMatrix import XMLMatrix
from Products.ERP5.Document.Amount import Amount
from Products.ERP5.Document.Predicate import Predicate
from Products.ERP5Type.Utils import cartesianProduct
from Products.ERP5.AggregatedAmountList import AggregatedAmountList
import zope.interface
def isMovement(document):
"""Hides isMovement method or variable complexity"""
if callable(document.isMovement):
is_movement = document.isMovement()
else:
is_movement = document.isMovement
return is_movement
class TradeModelLine(Predicate, XMLMatrix, Amount):
"""Trade Model Line is a way to represent trade transformation for movements
TODO:
- make this code readable
- use comments
- use docstrings
"""
meta_type = 'ERP5 Trade Model Line'
portal_type = 'Trade Model Line'
# Declarative security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation)
# Declarative interfaces
zope.interface.implements(
interfaces.ITransformation,
interfaces.IVariated
)
# Declarative properties
property_sheets = ( PropertySheet.Base
, PropertySheet.SimpleItem
, PropertySheet.CategoryCore
, PropertySheet.Amount
, PropertySheet.Price
, PropertySheet.TradeModelLine
, PropertySheet.Reference
, PropertySheet.Predicate
)
security.declareProtected(Permissions.AccessContentsInformation,
'getPrice')
def getPrice(self):
return self._baseGetPrice()
def updateAggregatedAmountList(self, context, **kw):
raise NotImplementedError('TODO')
security.declareProtected(Permissions.AccessContentsInformation,
'getAggregatedAmountList')
def getAggregatedAmountList(self, context, movement_list = None,
current_aggregated_amount_list = None, base_id='movement', **kw):
from Products.ERP5Type.Document import newTempSimulationMovement
# test with predicate if this model line could be applied
if not self.test(context):
# This model_line should not be applied
return []
if movement_list is None:
movement_list = []
if current_aggregated_amount_list is None:
current_aggregated_amount_list = []
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')
# if movement_list is passed as parameter, it shall be used,
# otherwise it is needed to look up for movements
if len(movement_list) == 0:
# no movements passed, need to find some
if isMovement(context):
# create movement lists from context
movement_list = [context]
else:
# create movement list for delivery's movements
movement_list = []
for movement in context.getMovementList():
# XXX: filtering shall be in getMovementList
# add only movement which are input (i.e. resource use category
# is in the normal resource use preference list). Output will
# be recalculated
movement_resource = movement.getResourceValue()
if movement_resource is not None:
if movement_resource.getUse() in \
normal_resource_use_category_list:
movement_list.append(movement)
aggregated_amount_list = AggregatedAmountList()
base_application_list = self.getBaseApplicationList()
self_id = self.getParentValue().getId() + '_' + self.getId()
tmp_movement_list = [q for q in current_aggregated_amount_list \
if q.getReference() == self.getReference() ]
if len(tmp_movement_list) > 0:
tmp_movement_list = tmp_movement_list[:1]
update = 1
else:
common_params = {
'causality': self.getRelativeUrl(),
'resource': self.getResource(),
'reference': self.getReference(),
'base_application_list': base_application_list,
'base_contribution_list': self.getBaseContributionList(),
'start_date': context.getStartDate(),
'stop_date': context.getStopDate(),
'create_line': self.isCreateLine(),
'trade_phase_list': self.getTradePhaseList(),
}
update = 0
base_category_list = self.getVariationBaseCategoryList()
category_list_list = []
for base_cat in base_category_list:
category_list = self.getVariationCategoryList(
base_category_list=base_cat)
category_list_list.append(category_list)
cartesian_product = cartesianProduct(category_list_list)
# if categories are used, we want to look for all cells
if len(category_list_list) > 0:
for cell_coordinates in cartesian_product:
cell = self.getCell(base_id=base_id, *cell_coordinates)
if cell is None:
raise ValueError("Can't find the cell corresponding to those "+\
"cells coordinates : %s" % cell_coordinates)
tmp_movement = newTempSimulationMovement(self.getPortalObject(),
self_id,
variation_base_category_list = cell.getVariationBaseCategoryList(),
variation_category_list = cell.getVariationCategoryList(),
price = cell.getPrice(),
quantity = cell.getQuantity(0.0),
**common_params
)
tmp_movement_list.append(tmp_movement)
else:
tmp_movement = newTempSimulationMovement(self.getPortalObject(),
self_id,
quantity = self.getQuantity(0.0),
price = self.getPrice(),
**common_params
)
tmp_movement_list.append(tmp_movement)
modified = 0
for tmp_movement in tmp_movement_list:
if len(self.getVariationCategoryList()) == 0 and \
self.getQuantity(None) is None or \
len(self.getVariationCategoryList()) and \
tmp_movement.getQuantity(None) is None:
# if the quantity is not defined, that mean we should search on
# all movements with corresponding base_amount (if we use cells, we
# have to look on cells, if we don't, look on self)
for movement in movement_list:
if set(base_application_list)\
.intersection(set(movement.getBaseContributionList())):
# if the movement have no variation category, it's the same as
# if he have all variation categories
if len(movement.getVariationCategoryList()) == 0 or \
set(movement.getVariationCategoryList()).intersection(\
set(tmp_movement.getVariationCategoryList())):
quantity = tmp_movement.getQuantity(0.0)
modified = 1
tmp_movement.setQuantity(quantity + movement.getTotalPrice())
else:
# if the quantity is defined, we use it
modified = 1
if tmp_movement.getPrice() in (0, None):
# if price is not defined, it the same as 100 %
tmp_movement.setPrice(1)
# check if slices are used
salary_range_list = tmp_movement.getVariationCategoryList(\
base_category_list='salary_range') #XXX hardcoded values
salary_range = len(salary_range_list) and salary_range_list[0] or None
if salary_range is not None:
# we use slice
model = self.getParentValue()
cell = model.getCell(salary_range)
if cell is None:
raise ValueError("Can't find the cell corresponding to those "+\
"cells coordinates : %s" % salary_range)
model_slice_min = cell.getQuantityRangeMin()
model_slice_max = cell.getQuantityRangeMax()
base_application = tmp_movement.getQuantity(0.0)
if base_application-model_slice_min>0:
if base_application <= model_slice_max:
tmp_movement.setQuantity(base_application-model_slice_min)
elif model_slice_max:
tmp_movement.setQuantity(model_slice_max-model_slice_min)
if not update:
if modified:
aggregated_amount_list.append(tmp_movement)
return aggregated_amount_list