Skip to content

Commit 00a062c

Browse files
committed
[DEV] review of rule management
1 parent 30a3bc1 commit 00a062c

File tree

4 files changed

+62
-34
lines changed

4 files changed

+62
-34
lines changed

hr_duty_planner/TODO.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ToDo list
99
* DEV method to update all employees name list
1010
* service rule
1111
* DEV complete rule method management
12+
* DEV management of errors: log/popup
1213
* calendar
1314
* DEV check module
1415

hr_duty_planner/models/service_allocate.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,24 @@ def double_assign(self, parameters):
250250

251251
def rule_call(self, parameters):
252252
"""
253-
_TODO_ _FIX_ direct call to service.rule.rule_call on the button
253+
Call a rule method
254+
@param parameter dict: parameters to pass to the method
255+
rule_name string: name of the method
256+
param json: name:value couples with method parameters
257+
srv_id int: id of the service
258+
res_obj obj: resourse object to check with the roule
254259
"""
255260
result = self.env['service.rule'].rule_call(parameters['rule_name'],
256261
parameters['param'],
257-
parameters['srv_id'])
262+
parameters['srv_id'],
263+
parameters['res_obj'],
264+
)
258265
return result
259266

260267
def check_resource_rule(self, parameters):
261268
"""
262269
Check rules for each resource associated to the service
263-
@param srv_id int: id of the service
270+
@param srv_id int: id of the service
264271
"""
265272

266273
# get employee of the service
@@ -278,15 +285,14 @@ def check_resource_rule(self, parameters):
278285
# save rule/field value
279286
rule_method[rule.rule_id.method][rule.rule_field_id.field_name] = \
280287
rule.field_value
281-
# _todo_ activate check logic on method
288+
# _todo_ remove log
282289
_logger.info(employee.name+' '+json.dumps(rule_method))
290+
# execute the memorized rules
283291
for method in rule_method:
284-
_logger.info(method+' '+json.dumps(rule_method[method]))
285-
for param in rule_method[method]:
286-
_logger.info(param+' '+rule_method[method][param])
287292
self.rule_call({'rule_name': method,
288293
'param': json.dumps(rule_method[method]),
289-
'srv_id': employee.id
294+
'srv_id': employee.id,
295+
'res_obj': employee,
290296
})
291297

292298
# get vehicle of the service

hr_duty_planner/models/service_rule.py

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,15 @@ def double_assign(self, resource_type, obj_id):
135135
raise UserError(_('Elements with overlapped shift:')+'\n'+rule_msg)
136136
return rule_result
137137

138-
def rule_call(self, rule, param, obj_id):
138+
def rule_call(self, rule, param, srv_id, res_obj):
139139
"""
140140
Call requested rule
141-
@param rule string: name of the rule
142-
@param param json: parameters to pass to the method
143-
@param obj_id obj: object on wich check the method
141+
@param rule string: name of the rule
142+
@param param json: name:value couples of parameters to pass to the method
143+
@param srv_id int: service id
144+
@param res_obj obj: resource object
144145
145-
@return rule elaboration
146+
@return dict: rule elaboration
146147
"""
147148

148149
# _TODO_ check if in rule_id
@@ -153,25 +154,44 @@ def rule_call(self, rule, param, obj_id):
153154
if method == '_invalid_rule':
154155
result = self._invalid_rule(rule_name)
155156
else:
156-
result = method(param, obj_id)
157+
result = method(param, srv_id, res_obj)
157158
return result
158159

159160
def _invalid_rule(self, rule_name):
160161
"""
161162
Management of method non defined
162163
"""
164+
# _todo_ log/popup error management
163165
# raise UserError(_('Method %s not defined') % (rule_name))
164166
_logger.error('ERROR _invalid_rule: '+rule_name)
165167

166-
def _rule_method_template(self):
168+
return {'message': 'Rule '+rule_name+' not defined',
169+
'result': False,
170+
'data': {}
171+
}
172+
173+
####################################################################################
174+
# Rules's method definition
175+
176+
def _rule_method_template(self, param, srv_id, res_obj):
167177
"""
168-
Rules definition template
169-
@param _todo_
170-
@return _todo_
178+
Rules template definition
179+
180+
For standard development each rule method must have all the same parameters
181+
defined: use false value in call if not used
182+
183+
@param param json: name:value couples of method's parameters
184+
@param srv_id id: id of the service
185+
@param res_obj obj: resourse object
186+
187+
@return dictionary: {'message': string, 'result': boolean, 'data': dict}
188+
message: description of the elaboration result
189+
result: True/False based on the logic
190+
data: informations to be used by another method
171191
"""
172-
return 0
192+
return {'message': 'Template', 'result': True, 'data': {}}
173193

174-
def hour_active_week(self, param, obj_id):
194+
def hour_active_week(self, param, srv_id, res_obj):
175195
"""
176196
Calculate the total of active hours of a resource in a week.
177197
By active hours is meant work+on call
@@ -182,30 +202,30 @@ def hour_active_week(self, param, obj_id):
182202

183203
# extract employees of the service
184204
for employee in (self.env['service.allocate']
185-
.search([('id', '=', obj_id)]).employee_ids):
205+
.search([('id', '=', srv_id)]).employee_ids):
186206
# get services where employee is assigned
187207
sql = ('SELECT service_allocate_id '
188208
'FROM hr_employee_service_allocate_rel '
189209
'WHERE hr_employee_id= %s')
190-
self.env.cr.execute(sql % str(employee.id))
210+
self.env.cr.execute(sql, (str(employee.id),))
191211
# get duration of each service
192212
# _todo_ calculate as end-start
193-
for srv_id in self.env.cr.fetchall():
213+
for fetch_srv_id in self.env.cr.fetchall():
194214
total_time += self.env['service.allocate'] \
195-
.search([('id', 'in', srv_id)]) \
215+
.search([('id', 'in', fetch_srv_id)]) \
196216
.service_template_id.duration
197217
raise UserError(_('Totale ore uomo %s') % (total_time))
198218
# return total_time
199219

200-
def hour_rest_week(self, param, obj_id):
220+
def hour_rest_week(self, param, srv_id, res_obj):
201221
"""
202222
Calculate the total of rest hours of a resource in a week.
203223
By active hours is meant not work or on call
204224
_todo_ define/set active shift
205225
"""
206-
return 8
226+
return {'message': 'To DO', 'result': True, 'data': {'hour': 8}}
207227

208-
def hour_active_month(self, param, res_id):
228+
def hour_active_month(self, param, srv_id, res_obj):
209229
"""
210230
Calculate the total of active hours of a resource in a month.
211231
By active hours is meant work+on call
@@ -218,16 +238,16 @@ def hour_active_month(self, param, res_id):
218238
sql = ('SELECT service_allocate_id '
219239
'FROM hr_employee_service_allocate_rel '
220240
'WHERE hr_employee_id= %s')
221-
self.env.cr.execute(sql, (tuple(res_id),))
241+
self.env.cr.execute(sql, (res_obj.id,))
222242
# get duration of each service
223243
# _todo_ calculate as end-start
224-
for srv_id in self.env.cr.fetchall():
244+
for fetch_srv_id in self.env.cr.fetchall():
225245
total_time += self.env['service.allocate'] \
226-
.search([('id', 'in', srv_id)]) \
246+
.search([('id', 'in', fetch_srv_id)]) \
227247
.service_template_id.duration
228-
emp_name = self.env['hr.employee'].search([('id', '=', res_id)]).name
248+
empl_name = self.env['hr.employee'].search([('id', '=', res_obj.id)]).name
229249
param_dict = json.loads(param)
230250
# raise UserError
231-
_logger.error(_('Totale ore (mese) %s: %s [limite %s]')
232-
% (emp_name, total_time, param_dict['h_max']))
251+
_logger.info(_('Totale ore (mese) %s: %s [limite %s]')
252+
% (empl_name, total_time, param_dict['h_max']))
233253
# return total_time

hr_duty_planner/views/service_allocate_view.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
string="Check rules TEST"
9090
context = "{'rule_name':'hour_active_week',
9191
'param':'{}',
92-
'srv_id': id}"
92+
'srv_id': id,
93+
'res_obj': False}"
9394
type="object" class="btn-primary"/>
9495
<br/>
9596
<button name="check_resource_rule" colspan="2"

0 commit comments

Comments
 (0)