@@ -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
0 commit comments