[MERGE] merge with latest stable
[odoo/odoo.git] / addons / resource / resource.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from datetime import datetime, timedelta
23 import math
24 from faces import *
25 from new import classobj
26 from osv import fields, osv
27 from tools.translate import _
28
29 from itertools import groupby
30 from operator import itemgetter
31
32
33 class resource_calendar(osv.osv):
34     _name = "resource.calendar"
35     _description = "Resource Calendar"
36     _columns = {
37         'name' : fields.char("Name", size=64, required=True),
38         'company_id' : fields.many2one('res.company', 'Company', required=False),
39         'attendance_ids' : fields.one2many('resource.calendar.attendance', 'calendar_id', 'Working Time'),
40         'manager' : fields.many2one('res.users', 'Workgroup manager'),
41     }
42     _defaults = {
43         'company_id': lambda self, cr, uid, context: self.pool.get('res.company')._company_default_get(cr, uid, 'resource.calendar', context=context)
44     }
45
46     def _get_leaves(self, cr, uid, id, resource):
47         resource_cal_leaves = self.pool.get('resource.calendar.leaves')
48         dt_leave = []
49
50         resource_leave_ids = resource_cal_leaves.search(cr, uid, [('calendar_id','=',id), '|', ('resource_id','=',False), ('resource_id','=',resource)])
51         #res_leaves = resource_cal_leaves.read(cr, uid, resource_leave_ids, ['date_from', 'date_to'])
52         res_leaves = resource_cal_leaves.browse(cr, uid, resource_leave_ids)
53
54         for leave in res_leaves:
55             dtf = datetime.strptime(leave.date_from, '%Y-%m-%d %H:%M:%S')
56             dtt = datetime.strptime(leave.date_to, '%Y-%m-%d %H:%M:%S')
57             no = dtt - dtf
58             [dt_leave.append((dtf + timedelta(days=x)).strftime('%Y-%m-%d')) for x in range(int(no.days + 1))]
59             dt_leave.sort()
60
61         return dt_leave
62
63     def interval_min_get(self, cr, uid, id, dt_from, hours, resource=False):
64         if not id:
65             td = int(hours)*3
66             return [(dt_from - timedelta(hours=td), dt_from)]
67         dt_leave = self._get_leaves(cr, uid, id, resource)
68         dt_leave.reverse()
69         todo = hours
70         result = []
71         maxrecur = 100
72         current_hour = dt_from.hour
73         while (todo>0) and maxrecur:
74             cr.execute("select hour_from,hour_to from resource_calendar_attendance where dayofweek='%s' and calendar_id=%s order by hour_from desc", (dt_from.weekday(),id))
75             for (hour_from,hour_to) in cr.fetchall():
76                 leave_flag  = False
77                 if (hour_from<current_hour) and (todo>0):
78                     m = min(hour_to, current_hour)
79                     if (m-hour_from)>todo:
80                         hour_from = m-todo
81                     dt_check = dt_from.strftime('%Y-%m-%d')
82                     for leave in dt_leave:
83                         if dt_check == leave:
84                             dt_check = datetime.strptime(dt_check, '%Y-%m-%d') + timedelta(days=1)
85                             leave_flag = True
86                     if leave_flag:
87                         break
88                     else:
89                         d1 = datetime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(hour_from)), int((hour_from%1) * 60))
90                         d2 = datetime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(m)), int((m%1) * 60))
91                         result.append((d1, d2))
92                         current_hour = hour_from
93                         todo -= (m-hour_from)
94             dt_from -= timedelta(days=1)
95             current_hour = 24
96             maxrecur -= 1
97         result.reverse()
98         return result
99
100     # def interval_get(self, cr, uid, id, dt_from, hours, resource=False, byday=True):
101     def interval_get_multi(self, cr, uid, date_and_hours_by_cal, resource=False, byday=True):
102         def group(lst, key):
103             lst.sort(key=itemgetter(key))
104             grouped = groupby(lst, itemgetter(key))
105             return dict([(k, [v for v in itr]) for k, itr in grouped])
106         # END group
107
108         cr.execute("select calendar_id, dayofweek, hour_from, hour_to from resource_calendar_attendance order by hour_from")
109         hour_res = cr.dictfetchall()
110         hours_by_cal = group(hour_res, 'calendar_id')
111
112         results = {}
113
114         for d, hours, id in date_and_hours_by_cal:
115             dt_from = datetime.strptime(d, '%Y-%m-%d %H:%M:%S')
116             if not id:
117                 td = int(hours)*3
118                 results[(d, hours, id)] = [(dt_from, dt_from + timedelta(hours=td))]
119                 continue
120
121             dt_leave = self._get_leaves(cr, uid, id, resource)
122             todo = hours
123             result = []
124             maxrecur = 100
125             current_hour = dt_from.hour
126             while (todo>0) and maxrecur:
127                 for (hour_from,hour_to) in [(item['hour_from'], item['hour_to']) for item in hours_by_cal[id] if item['dayofweek'] == str(dt_from.weekday())]:
128                     leave_flag  = False
129                     if (hour_to>current_hour) and (todo>0):
130                         m = max(hour_from, current_hour)
131                         if (hour_to-m)>todo:
132                             hour_to = m+todo
133                         dt_check = dt_from.strftime('%Y-%m-%d')
134                         for leave in dt_leave:
135                             if dt_check == leave:
136                                 dt_check = datetime.strptime(dt_check, '%Y-%m-%d') + timedelta(days=1)
137                                 leave_flag = True
138                         if leave_flag:
139                             break
140                         else:
141                             d1 = datetime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(m)), int((m%1) * 60))
142                             d2 = datetime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(hour_to)), int((hour_to%1) * 60))
143                             result.append((d1, d2))
144                             current_hour = hour_to
145                             todo -= (hour_to - m)
146                 dt_from += timedelta(days=1)
147                 current_hour = 0
148                 maxrecur -= 1
149             results[(d, hours, id)] = result
150         return results
151
152     def interval_get(self, cr, uid, id, dt_from, hours, resource=False, byday=True):
153         res = self.interval_get_multi(cr, uid, [(dt_from.strftime('%Y-%m-%d %H:%M:%S'), hours, id)], resource, byday)[(dt_from.strftime('%Y-%m-%d %H:%M:%S'), hours, id)]
154         return res
155
156     def interval_hours_get(self, cr, uid, id, dt_from, dt_to, resource=False):
157         if not id:
158             return 0.0
159         dt_leave = self._get_leaves(cr, uid, id, resource)
160         hours = 0.0
161
162         current_hour = dt_from.hour
163
164         while (dt_from <= dt_to):
165             cr.execute("select hour_from,hour_to from resource_calendar_attendance where dayofweek='%s' and calendar_id=%s order by hour_from", (dt_from.weekday(),id))
166             der =  cr.fetchall()
167             for (hour_from,hour_to) in der:
168                 if hours != 0.0:#For first time of the loop only,hours will be 0
169                     current_hour = hour_from
170                 leave_flag = False
171                 if (hour_to>=current_hour):
172                     dt_check = dt_from.strftime('%Y-%m-%d')
173                     for leave in dt_leave:
174                         if dt_check == leave:
175                             dt_check = datetime.strptime(dt_check, "%Y-%m-%d") + timedelta(days=1)
176                             leave_flag = True
177
178                     if leave_flag:
179                         break
180                     else:
181                         d1 = dt_from
182                         d2 = datetime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(hour_to)), int((hour_to%1) * 60))
183
184                         if hours != 0.0:#For first time of the loop only,hours will be 0
185                             d1 = datetime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(current_hour)), int((current_hour%1) * 60))
186
187                         if dt_from.day == dt_to.day:
188                             if hour_from <= dt_to.hour <= hour_to:
189                                 d2 = dt_to
190                         dt_from = d2
191                         hours += (d2-d1).seconds
192             dt_from = datetime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(current_hour)), int((current_hour%1) * 60)) + timedelta(days=1)
193             current_hour = 0.0
194
195         return (hours/3600)
196
197 resource_calendar()
198
199 class resource_calendar_attendance(osv.osv):
200     _name = "resource.calendar.attendance"
201     _description = "Work Detail"
202     _columns = {
203         'name' : fields.char("Name", size=64, required=True),
204         'dayofweek': fields.selection([('0','Monday'),('1','Tuesday'),('2','Wednesday'),('3','Thursday'),('4','Friday'),('5','Saturday'),('6','Sunday')], 'Day of week'),
205         'date_from' : fields.date('Starting date'),
206         'hour_from' : fields.float('Work from', size=8, required=True, help="Working time will start from"),
207         'hour_to' : fields.float("Work to", size=8, required=True, help="Working time will end at"),
208         'calendar_id' : fields.many2one("resource.calendar", "Resource's Calendar", required=True),
209     }
210     _order = 'dayofweek, hour_from'
211 resource_calendar_attendance()
212
213 def convert_timeformat(time_string):
214     split_list = str(time_string).split('.')
215     hour_part = split_list[0]
216     mins_part = split_list[1]
217     round_mins = int(round(float(mins_part) * 60,-2))
218     converted_string = hour_part + ':' + str(round_mins)[0:2]
219     return converted_string
220
221 class resource_resource(osv.osv):
222     _name = "resource.resource"
223     _description = "Resource Detail"
224     _columns = {
225         'name' : fields.char("Name", size=64, required=True),
226         'code': fields.char('Code', size=16),
227         'active' : fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the resource record without removing it."),
228         'company_id' : fields.many2one('res.company', 'Company'),
229         'resource_type': fields.selection([('user','Human'),('material','Material')], 'Resource Type', required=True),
230         'user_id' : fields.many2one('res.users', 'User', help='Related user name for the resource to manage its access.'),
231         'time_efficiency' : fields.float('Efficiency factor', size=8, required=True, help="This field depict the efficiency of the resource to complete tasks. e.g  resource put alone on a phase of 5 days with 5 tasks assigned to him, will show a load of 100% for this phase by default, but if we put a efficency of 200%, then his load will only be 50%."),
232         'calendar_id' : fields.many2one("resource.calendar", "Working Period", help="Define the schedule of resource"),
233     }
234     _defaults = {
235         'resource_type' : 'user',
236         'time_efficiency' : 1,
237         'active' : True,
238         'company_id': lambda self, cr, uid, context: self.pool.get('res.company')._company_default_get(cr, uid, 'resource.resource', context=context)
239     }
240
241     
242     def copy(self, cr, uid, id, default=None, context=None):
243         if default is None:
244             default = {}
245         if not default.get('name', False):
246             default['name'] = self.browse(cr, uid, id, context=context).name + _(' (copy)')
247         return super(resource_resource, self).copy(cr, uid, id, default, context)
248
249     def generate_resources(self, cr, uid, user_ids, calendar_id, context=None):
250         """
251         Return a list of  Resource Class objects for the resources allocated to the phase.
252         """
253         resource_objs = {}
254         user_pool = self.pool.get('res.users')
255         for user in user_pool.browse(cr, uid, user_ids, context=context):
256             resource_ids = self.search(cr, uid, [('user_id', '=', user.id)], context=context)
257             #assert len(resource_ids) < 1, "User should not has more than one resources"
258             leaves = []
259             resource_eff = 1.0
260             if resource_ids:
261                 resource_id = resource_ids[0]    
262                 resource = self.browse(cr, uid, resource_id, context=context)
263                 resource_eff = resource.time_efficiency
264                 resource_cal = resource.calendar_id.id
265                 if resource_cal:
266                     leaves = self.compute_vacation(cr, uid, calendar_id, resource.id, resource_cal, context=context)
267                 temp = {
268                          'name' : resource.name,
269                          'vacation': tuple(leaves),
270                          'efficiency': resource_eff,
271                       }
272                 resource_objs[resource_id] = temp     
273 #            resource_objs.append(classobj(str(user.name), (Resource,),{
274 #                                             '__doc__': user.name,
275 #                                             '__name__': user.name,
276 #                                             'vacation': tuple(leaves),
277 #                                             'efficiency': resource_eff,
278 #                                          }))
279         return resource_objs
280
281     def compute_vacation(self, cr, uid, calendar_id, resource_id=False, resource_calendar=False, context=None):
282         """
283         Compute the vacation from the working calendar of the resource.
284         @param calendar_id : working calendar of the project
285         @param resource_id : resource working on phase/task
286         @param resource_calendar : working calendar of the resource
287         """
288         resource_calendar_leaves_pool = self.pool.get('resource.calendar.leaves')
289         leave_list = []
290         if resource_id:
291             leave_ids = resource_calendar_leaves_pool.search(cr, uid, ['|', ('calendar_id', '=', calendar_id),
292                                                                        ('calendar_id', '=', resource_calendar),
293                                                                        ('resource_id', '=', resource_id)
294                                                                       ], context=context)
295         else:
296             leave_ids = resource_calendar_leaves_pool.search(cr, uid, [('calendar_id', '=', calendar_id),
297                                                                       ('resource_id', '=', False)
298                                                                       ], context=context)
299         leaves = resource_calendar_leaves_pool.read(cr, uid, leave_ids, ['date_from', 'date_to'], context=context)
300         for i in range(len(leaves)):
301             dt_start = datetime.strptime(leaves[i]['date_from'], '%Y-%m-%d %H:%M:%S')
302             dt_end = datetime.strptime(leaves[i]['date_to'], '%Y-%m-%d %H:%M:%S')
303             no = dt_end - dt_start
304             [leave_list.append((dt_start + timedelta(days=x)).strftime('%Y-%m-%d')) for x in range(int(no.days + 1))]
305             leave_list.sort()
306         return leave_list
307
308     def compute_working_calendar(self, cr, uid, calendar_id=False, context=None):
309         """
310         Change the format of working calendar from 'Openerp' format to bring it into 'Faces' format.
311         @param calendar_id : working calendar of the project
312         """
313         if not calendar_id:
314             # Calendar is not specified: working days: 24/7
315             return [('fri', '1:0-12:0','12:0-24:0'), ('thu', '1:0-12:0','12:0-24:0'), ('wed', '1:0-12:0','12:0-24:0'), 
316                    ('mon', '1:0-12:0','12:0-24:0'), ('tue', '1:0-12:0','12:0-24:0'), ('sat', '1:0-12:0','12:0-24:0'), ('sun', '1:0-12:0','12:0-24:0')]
317         resource_attendance_pool = self.pool.get('resource.calendar.attendance')
318         time_range = "8:00-8:00"
319         non_working = ""
320         week_days = {"0": "mon", "1": "tue", "2": "wed","3": "thu", "4": "fri", "5": "sat", "6": "sun"}
321         wk_days = {}
322         wk_time = {}
323         wktime_list = []
324         wktime_cal = []
325         week_ids = resource_attendance_pool.search(cr, uid, [('calendar_id', '=', calendar_id)], context=context)
326         weeks = resource_attendance_pool.read(cr, uid, week_ids, ['dayofweek', 'hour_from', 'hour_to'], context=context)
327         # Convert time formats into appropriate format required
328         # and create a list like [('mon', '8:00-12:00'), ('mon', '13:00-18:00')]
329         for week in weeks:
330             res_str = ""
331             day = None
332             if week_days.has_key(week['dayofweek']):
333                 day = week_days[week['dayofweek']]
334                 wk_days[week['dayofweek']] = week_days[week['dayofweek']]
335             hour_from_str = convert_timeformat(week['hour_from'])
336             hour_to_str = convert_timeformat(week['hour_to'])
337             res_str = hour_from_str + '-' + hour_to_str
338             wktime_list.append((day, res_str))
339         # Convert into format like [('mon', '8:00-12:00', '13:00-18:00')]
340         for item in wktime_list:
341             if wk_time.has_key(item[0]):
342                 wk_time[item[0]].append(item[1])
343             else:
344                 wk_time[item[0]] = [item[0]]
345                 wk_time[item[0]].append(item[1])
346         for k,v in wk_time.items():
347             wktime_cal.append(tuple(v))
348         # Add for the non-working days like: [('sat, sun', '8:00-8:00')]
349         for k, v in wk_days.items():
350             if week_days.has_key(k):
351                 week_days.pop(k)
352         for v in week_days.itervalues():
353             non_working += v + ','
354         if non_working:
355             wktime_cal.append((non_working[:-1], time_range))
356         return wktime_cal
357
358     #TODO: Write optimized alogrothem for resource availability. : Method Yet not implemented
359     def check_availability(self, cr, uid, ids, start, end, context=None):
360         if context ==  None:
361             contex = {}
362         allocation = {}
363         return allocation
364
365 resource_resource()
366
367 class resource_calendar_leaves(osv.osv):
368     _name = "resource.calendar.leaves"
369     _description = "Leave Detail"
370     _columns = {
371         'name' : fields.char("Name", size=64),
372         'company_id' : fields.related('calendar_id','company_id',type='many2one',relation='res.company',string="Company", store=True, readonly=True),
373         'calendar_id' : fields.many2one("resource.calendar", "Working time"),
374         'date_from' : fields.datetime('Start Date', required=True),
375         'date_to' : fields.datetime('End Date', required=True),
376         'resource_id' : fields.many2one("resource.resource", "Resource", help="If empty, this is a generic holiday for the company. If a resource is set, the holiday/leave is only for this resource"),
377     }
378
379     def check_dates(self, cr, uid, ids, context=None):
380          leave = self.read(cr, uid, ids[0], ['date_from', 'date_to'])
381          if leave['date_from'] and leave['date_to']:
382              if leave['date_from'] > leave['date_to']:
383                  return False
384          return True
385
386     _constraints = [
387         (check_dates, 'Error! leave start-date must be lower then leave end-date.', ['date_from', 'date_to'])
388     ]
389
390     def onchange_resource(self,cr, uid, ids, resource, context=None):
391         result = {}
392         if resource:
393             resource_pool = self.pool.get('resource.resource')
394             result['calendar_id'] = resource_pool.browse(cr, uid, resource, context=context).calendar_id.id
395             return {'value': result}
396         return {'value': {'calendar_id': []}}
397
398 resource_calendar_leaves()
399
400 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: