[IMP]:removed duplicate fields from hr.employee module, put resource_id also while...
[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 import mx.DateTime
22 import time
23 import math
24 from osv import fields, osv
25 from tools.translate import _
26
27 class resource_calendar(osv.osv):
28     _name = "resource.calendar"
29     _description = "Resource Calendar"
30     _columns = {
31         'name' : fields.char("Name", size=64, required=True),
32         'company_id' : fields.many2one('res.company', 'Company', required=True),
33         'week_id' : fields.one2many('resource.calendar.week', 'calendar_id', 'Working Time'),
34         'manager' : fields.many2one('res.users', 'Workgroup manager'),
35     }
36     _defaults = {
37         'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'resource.calendar', c)
38     }
39     def interval_min_get(self, cr, uid, id, dt_from, hours , resource=0):
40         dt_leave = []
41         if not id:
42             return [(dt_from-mx.DateTime.RelativeDateTime(hours=int(hours)*3), dt_from)]
43         if resource:
44             resource_leave_ids = self.pool.get('resource.calendar.leaves').search(cr,uid,[('resource_id','=',resource)])
45             if resource_leave_ids:
46                 res_leaves = self.pool.get('resource.calendar.leaves').read(cr,uid,resource_leave_ids,['date_from','date_to'])
47                 for i in range(len(res_leaves)):
48                     dtf = mx.DateTime.strptime(res_leaves[i]['date_from'],'%Y-%m-%d %H:%M:%S')
49                     dtt = mx.DateTime.strptime(res_leaves[i]['date_to'],'%Y-%m-%d %H:%M:%S')
50                     leave_days = ((dtt - dtf).days) + 1
51                     for x in range(int(leave_days)):
52                         dt_leave.append((dtf + mx.DateTime.RelativeDateTime(days=x)).strftime('%Y-%m-%d'))
53                     dt_leave.sort()
54         todo = hours
55         cycle = 0
56         result = []
57         maxrecur = 100
58         current_hour = dt_from.hour
59         while (todo>0) and maxrecur:
60             cr.execute("select hour_from,hour_to from resource_calendar_week where dayofweek='%s' and calendar_id=%s order by hour_from", (dt_from.day_of_week,id))
61             for (hour_from,hour_to) in cr.fetchall():
62                     if (hour_to>current_hour) and (todo>0):
63                         m = max(hour_from, current_hour)
64                         if (hour_to-m)>todo:
65                             hour_to = m+todo
66                         d1 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
67                         d2 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_to)),int((hour_to%1) * 60))
68                         dt1 = d1.strftime('%Y-%m-%d')
69                         dt2 = d2.strftime('%Y-%m-%d')
70                         for i in range(len(dt_leave)):
71                             if dt1 == dt_leave[i]:
72                                 dt_from += mx.DateTime.RelativeDateTime(days=1)
73                                 d1 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
74                                 d2 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_to)),int((hour_to%1) * 60))
75                                 dt1 = d1.strftime('%Y-%m-%d')
76                                 dt2 = d2.strftime('%Y-%m-%d')
77                         result.append((d1, d2))
78                         current_hour = hour_to
79                         todo -= (hour_to - m)
80             dt_from += mx.DateTime.RelativeDateTime(days=1)
81             current_hour = 0
82             maxrecur -= 1
83         return result
84
85     def interval_get(self, cr, uid, id, dt_from, hours, resource=0, byday=True):
86         dt_leave = []
87         if not id:
88             return [(dt_from,dt_from+mx.DateTime.RelativeDateTime(hours=int(hours)*3))]
89         if resource:
90             resource_leave_ids = self.pool.get('resource.calendar.leaves').search(cr,uid,[('resource_id','=',resource)])
91             if resource_leave_ids:
92                 res_leaves = self.pool.get('resource.calendar.leaves').read(cr,uid,resource_leave_ids,['date_from','date_to'])
93                 for i in range(len(res_leaves)):
94                     dtf = mx.DateTime.strptime(res_leaves[i]['date_from'],'%Y-%m-%d %H:%M:%S')
95                     dtt = mx.DateTime.strptime(res_leaves[i]['date_to'],'%Y-%m-%d %H:%M:%S')
96                     no = dtt - dtf
97                     leave_days = no.days + 1
98                     for x in range(int(leave_days)):
99                         dt_leave.append((dtf + mx.DateTime.RelativeDateTime(days=x)).strftime('%Y-%m-%d'))
100                     dt_leave.sort()
101         todo = hours
102         cycle = 0
103         result = []
104         maxrecur = 100
105         current_hour = dt_from.hour
106         while (todo>0) and maxrecur:
107             cr.execute("select hour_from,hour_to from resource_calendar_week where dayofweek='%s' and calendar_id=%s order by hour_from", (dt_from.day_of_week,id))
108             for (hour_from,hour_to) in cr.fetchall():
109                     if (hour_to>current_hour) and (todo>0):
110                         m = max(hour_from, current_hour)
111                         if (hour_to-m)>todo:
112                             hour_to = m+todo
113                         d1 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
114                         d2 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_to)),int((hour_to%1) * 60))
115                         dt1 = d1.strftime('%Y-%m-%d')
116                         dt2 = d2.strftime('%Y-%m-%d')
117                         for i in range(len(dt_leave)):
118                             if dt1 == dt_leave[i]:
119                                 dt_from += mx.DateTime.RelativeDateTime(days=1)
120                                 d1 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
121                                 d2 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_to)),int((hour_to%1) * 60))
122                                 dt1 = d1.strftime('%Y-%m-%d')
123                                 dt2 = d2.strftime('%Y-%m-%d')
124                         result.append((d1, d2))
125                         current_hour = hour_to
126                         todo -= (hour_to - m)
127             dt_from += mx.DateTime.RelativeDateTime(days=1)
128             current_hour = 0
129             maxrecur -= 1
130         return result
131
132 resource_calendar()
133
134 class resource_calendar_week(osv.osv):
135     _name = "resource.calendar.week"
136     _description = "Work Detail"
137     _columns = {
138         'name' : fields.char("Name", size=64, required=True),
139         'dayofweek': fields.selection([('0','Monday'),('1','Tuesday'),('2','Wednesday'),('3','Thursday'),('4','Friday'),('5','Saturday'),('6','Sunday')], 'Day of week'),
140         'date_from' : fields.date('Starting date'),
141         'hour_from' : fields.float('Work from', size=8, required=True),
142         'hour_to' : fields.float("Work to", size=8, required=True),
143         'calendar_id' : fields.many2one("resource.calendar", "Resource's Calendar", required=True),
144     }
145     _order = 'dayofweek, hour_from'
146 resource_calendar_week()
147
148 class resource_resource(osv.osv):
149     _name = "resource.resource"
150     _description = "Resource Detail"
151     _columns = {
152         'name' : fields.char("Name", size=64, required=True ),
153         'code': fields.char('Code', size=16),
154         'active' : fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the resource record without removing it."),
155         'company_id' : fields.many2one('res.company', 'Company', required=True),
156         'resource_type': fields.selection([('user','Human'),('material','Material')], 'Resource Type', required=True),
157         'user_id' : fields.many2one('res.users', 'User',help='Related user name for the resource to manage its access.'),
158         'time_efficiency' : fields.float('Efficiency factor', size=8, 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%."),
159         'calendar_id' : fields.many2one("resource.calendar", "Working time", help="Define the schedule of resource"),
160     }
161     _defaults = {
162         'resource_type' : lambda *a: 'user',
163         'time_efficiency' : lambda *a: 1,
164         'active' : lambda *a: True,
165         'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'resource.resource', c)
166     }
167 resource_resource()
168
169 class resource_calendar_leaves(osv.osv):
170     _name = "resource.calendar.leaves"
171     _description = "Leave Detail"
172     _columns = {
173         'name' : fields.char("Name", size=64),
174         'company_id' : fields.related('calendar_id','company_id',type='many2one',relation='res.company',string="Company",readonly=True),
175         'calendar_id' : fields.many2one("resource.calendar", "Working time"),
176         'date_from' : fields.datetime('Start Date', required=True),
177         'date_to' : fields.datetime('End Date', required=True),
178         '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"),
179
180     }
181 resource_calendar_leaves()
182 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: