2d225527f0e92fa8a84cca7b629ad7655be95ea1
[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                 print 'Leave Details',res_leaves
48                 for i in range(len(res_leaves)):
49                     dtf = mx.DateTime.strptime(res_leaves[i]['date_from'],'%Y-%m-%d %H:%M:%S')
50                     dtt = mx.DateTime.strptime(res_leaves[i]['date_to'],'%Y-%m-%d %H:%M:%S')
51                     leave_days = ((dtt - dtf).days) + 1
52                     print 'No Of Leaves:::::::::',int(leave_days)
53                     for x in range(int(leave_days)):
54                         dt_leave.append((dtf + mx.DateTime.RelativeDateTime(days=x)).strftime('%Y-%m-%d'))
55                     dt_leave.sort()
56             print 'Sorted Leave Dates::::',dt_leave
57         todo = hours
58         cycle = 0
59         result = []
60         maxrecur = 100
61         current_hour = dt_from.hour
62         while (todo>0) and maxrecur:
63             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))
64             for (hour_from,hour_to) in cr.fetchall():
65                     if (hour_to>current_hour) and (todo>0):
66                         m = max(hour_from, current_hour)
67                         if (hour_to-m)>todo:
68                             hour_to = m+todo
69                         d1 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
70                         d2 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_to)),int((hour_to%1) * 60))
71                         dt1 = d1.strftime('%Y-%m-%d')
72                         dt2 = d2.strftime('%Y-%m-%d')
73                         print 'Date 1',dt1
74                         print 'Date 2',dt2
75                         for i in range(len(dt_leave)):
76                             if dt1 == dt_leave[i]:
77                                 print 'Leave::::::::'
78                                 dt_from += mx.DateTime.RelativeDateTime(days=1)
79                                 d1 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
80                                 d2 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_to)),int((hour_to%1) * 60))
81                                 dt1 = d1.strftime('%Y-%m-%d')
82                                 dt2 = d2.strftime('%Y-%m-%d')
83                         result.append((d1, d2))
84                         current_hour = hour_to
85                         todo -= (hour_to - m)
86             dt_from += mx.DateTime.RelativeDateTime(days=1)
87             current_hour = 0
88             maxrecur -= 1
89         return result
90
91     def interval_get(self, cr, uid, id, dt_from, hours, resource=0, byday=True):
92         dt_leave = []
93         if not id:
94             return [(dt_from,dt_from+mx.DateTime.RelativeDateTime(hours=int(hours)*3))]
95         if resource:
96             resource_leave_ids = self.pool.get('resource.calendar.leaves').search(cr,uid,[('resource_id','=',resource)])
97             if resource_leave_ids:
98                 res_leaves = self.pool.get('resource.calendar.leaves').read(cr,uid,resource_leave_ids,['date_from','date_to'])
99                 print 'Leave Details',res_leaves
100                 for i in range(len(res_leaves)):
101                     dtf = mx.DateTime.strptime(res_leaves[i]['date_from'],'%Y-%m-%d %H:%M:%S')
102                     dtt = mx.DateTime.strptime(res_leaves[i]['date_to'],'%Y-%m-%d %H:%M:%S')
103                     no = dtt - dtf
104                     leave_days = no.days + 1
105                     print 'No Of Leaves:::::::::',int(leave_days)
106                     for x in range(int(leave_days)):
107                         dt_leave.append((dtf + mx.DateTime.RelativeDateTime(days=x)).strftime('%Y-%m-%d'))
108                     dt_leave.sort()
109             print 'Sorted Leave Dates::::',dt_leave
110         todo = hours
111         cycle = 0
112         result = []
113         maxrecur = 100
114         current_hour = dt_from.hour
115         while (todo>0) and maxrecur:
116             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))
117             for (hour_from,hour_to) in cr.fetchall():
118                     if (hour_to>current_hour) and (todo>0):
119                         m = max(hour_from, current_hour)
120                         if (hour_to-m)>todo:
121                             hour_to = m+todo
122                         d1 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
123                         d2 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_to)),int((hour_to%1) * 60))
124                         dt1 = d1.strftime('%Y-%m-%d')
125                         dt2 = d2.strftime('%Y-%m-%d')
126                         print 'Date 1',dt1
127                         print 'Date 2',dt2
128                         for i in range(len(dt_leave)):
129                             if dt1 == dt_leave[i]:
130                                 print 'Leave::::::::'
131                                 dt_from += mx.DateTime.RelativeDateTime(days=1)
132                                 d1 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
133                                 d2 = mx.DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_to)),int((hour_to%1) * 60))
134                                 dt1 = d1.strftime('%Y-%m-%d')
135                                 dt2 = d2.strftime('%Y-%m-%d')
136                         result.append((d1, d2))
137                         current_hour = hour_to
138                         todo -= (hour_to - m)
139             dt_from += mx.DateTime.RelativeDateTime(days=1)
140             current_hour = 0
141             maxrecur -= 1
142         return result
143
144 resource_calendar()
145
146 class resource_calendar_week(osv.osv):
147     _name = "resource.calendar.week"
148     _description = "Work Detail"
149     _columns = {
150         'name' : fields.char("Name", size=64, required=True),
151         'dayofweek': fields.selection([('0','Monday'),('1','Tuesday'),('2','Wednesday'),('3','Thursday'),('4','Friday'),('5','Saturday'),('6','Sunday')], 'Day of week'),
152         'date_from' : fields.date('Starting date'),
153         'hour_from' : fields.float('Work from', size=8, required=True),
154         'hour_to' : fields.float("Work to", size=8, required=True),
155         'calendar_id' : fields.many2one("resource.calendar", "Resource's Calendar", required=True),
156     }
157     _order = 'dayofweek, hour_from'
158 resource_calendar_week()
159
160 class resource_resource(osv.osv):
161     _name = "resource.resource"
162     _description = "Resource Detail"
163     _columns = {
164         'name' : fields.char("Name", size=64, required=True),
165         'code': fields.char('Code', size=16),
166         '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."),
167         'company_id' : fields.many2one('res.company', 'Company', required=True),
168         'resource_type': fields.selection([('user','Human'),('material','Material')], 'Resource Type', required=True),
169         'user_id' : fields.many2one('res.users', 'User'),
170         'time_efficiency' : fields.float('Efficiency factor', size=8, help="This field depict the efficency of the ressource to complete tasks. e.g  ressource 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%."),
171         'calendar_id' : fields.many2one("resource.calendar", "Working time", required=True, help="Define the schedule of resource"),
172     }
173     _defaults = {
174         'resource_type' : lambda *a: 'user',
175         'time_efficiency' : lambda *a: 1,
176         'active' : lambda *a: True,
177         'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'resource.resource', c)
178     }
179 resource_resource()
180
181 class resource_calendar_leaves(osv.osv):
182     _name = "resource.calendar.leaves"
183     _description = "Leave Detail"
184     _columns = {
185         'name' : fields.char("Name", size=64),
186         'company_id' : fields.related('calendar_id','company_id',type='many2one',relation='res.company',string="Company",required=True),
187         #'company_id':fields.many2one('res.company', 'Company', required=True),
188         'calendar_id' : fields.many2one("resource.calendar", "Working time"),
189         'date_from' : fields.datetime('Start Date', required=True),
190         'date_to' : fields.datetime('End Date', required=True),
191         '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"),
192
193     }
194 resource_calendar_leaves()
195 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: