[FIX] Project_issue/Resource : Corrections for working hours calculation
bzr revid: jvo@tinyerp.com-20100604104947-8roj4grvx1m7sk59
This commit is contained in:
@@ -63,36 +63,43 @@ class project_issue(osv.osv, crm.crm_case):
|
||||
res_obj = self.pool.get('resource.resource')
|
||||
|
||||
res = {}
|
||||
for issue in self.browse(cr, uid, ids , context):
|
||||
for issue in self.browse(cr, uid, ids, context):
|
||||
for field in fields:
|
||||
res[issue.id] = {}
|
||||
duration = 0
|
||||
ans = False
|
||||
if field == 'day_open':
|
||||
hours = 0
|
||||
|
||||
if field in ['working_hours_open','day_open']:
|
||||
if issue.date_open:
|
||||
date_create = datetime.strptime(issue.create_date, "%Y-%m-%d %H:%M:%S")
|
||||
date_open = datetime.strptime(issue.date_open, "%Y-%m-%d %H:%M:%S")
|
||||
ans = date_open - date_create
|
||||
date_until = issue.date_open
|
||||
elif field == 'day_close':
|
||||
hours = cal_obj.interval_hours_get(cr, uid, issue.project_id.resource_calendar_id.id,
|
||||
mx.DateTime.strptime(issue.create_date, '%Y-%m-%d %H:%M:%S'),
|
||||
mx.DateTime.strptime(issue.date_open, '%Y-%m-%d %H:%M:%S'))
|
||||
elif field in ['working_hours_close','day_close']:
|
||||
if issue.date_closed:
|
||||
date_create = datetime.strptime(issue.create_date, "%Y-%m-%d %H:%M:%S")
|
||||
date_close = datetime.strptime(issue.date_closed, "%Y-%m-%d %H:%M:%S")
|
||||
date_until = issue.date_closed
|
||||
ans = date_close - date_create
|
||||
hours = cal_obj.interval_hours_get(cr, uid, issue.project_id.resource_calendar_id.id,
|
||||
mx.DateTime.strptime(issue.create_date, '%Y-%m-%d %H:%M:%S'),
|
||||
mx.DateTime.strptime(issue.date_closed, '%Y-%m-%d %H:%M:%S'))
|
||||
if ans:
|
||||
resource_id = False
|
||||
if issue.user_id:
|
||||
resource_ids = res_obj.search(cr, uid, [('user_id','=',issue.user_id.id)])
|
||||
if resource_ids and len(resource_ids):
|
||||
resource_id = resource_ids[0]
|
||||
|
||||
duration = float(ans.days)
|
||||
if issue.section_id and issue.section_id.resource_calendar_id:
|
||||
duration = float(ans.days) * 24
|
||||
new_dates = cal_obj.interval_get(cr,
|
||||
if issue.project_id and issue.project_id.resource_calendar_id:
|
||||
duration = float(ans.days) * 24
|
||||
new_dates = cal_obj.interval_min_get(cr,
|
||||
uid,
|
||||
issue.section_id.resource_calendar_id and issue.section_id.resource_calendar_id.id or False,
|
||||
issue.project_id.resource_calendar_id.id,
|
||||
mx.DateTime.strptime(issue.create_date, '%Y-%m-%d %H:%M:%S'),
|
||||
duration,
|
||||
resource=resource_id
|
||||
@@ -104,9 +111,11 @@ class project_issue(osv.osv, crm.crm_case):
|
||||
no_days.append(in_time.date)
|
||||
if out_time > date_until:
|
||||
break
|
||||
duration = len(no_days)
|
||||
res[issue.id][field] = abs(float(duration))
|
||||
|
||||
duration = len(no_days)
|
||||
if field in ['working_hours_open','working_hours_close']:
|
||||
res[issue.id][field] = hours
|
||||
else:
|
||||
res[issue.id][field] = abs(float(duration))
|
||||
return res
|
||||
|
||||
_columns = {
|
||||
@@ -164,8 +173,10 @@ class project_issue(osv.osv, crm.crm_case):
|
||||
'day_close': fields.function(_compute_day, string='Days to Close', \
|
||||
method=True, multi='day_close', type="float", store=True),
|
||||
'assigned_to' : fields.many2one('res.users', 'Assigned to'),
|
||||
'working_hours_open': fields.float('Working Hours to Open the Issue'),
|
||||
'working_hours_close': fields.float('Working Hours to Close the Issue'),
|
||||
'working_hours_open': fields.function(_compute_day, string='Working Hours to Open the Issue', \
|
||||
method=True, multi='working_days_open', type="float", store=True),
|
||||
'working_hours_close': fields.function(_compute_day, string='Working Hours to Close the Issue', \
|
||||
method=True, multi='working_days_close', type="float", store=True),
|
||||
}
|
||||
|
||||
def _get_project(self, cr, uid, context):
|
||||
|
||||
@@ -19,17 +19,7 @@
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
import base64
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import mx.DateTime
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import tools
|
||||
from crm import crm
|
||||
from osv import fields,osv,orm
|
||||
from osv.orm import except_orm
|
||||
from tools.translate import _
|
||||
|
||||
class project_issue(osv.osv):
|
||||
|
||||
+72
-33
@@ -37,21 +37,28 @@ class resource_calendar(osv.osv):
|
||||
_defaults = {
|
||||
'company_id': lambda self, cr, uid, context: self.pool.get('res.company')._company_default_get(cr, uid, 'resource.calendar', context=context)
|
||||
}
|
||||
|
||||
def interval_min_get(self, cr, uid, id, dt_from, hours, resource=False):
|
||||
|
||||
def _get_leaves(self, cr, uid, id, resource):
|
||||
resource_cal_leaves = self.pool.get('resource.calendar.leaves')
|
||||
dt_leave = []
|
||||
if not id:
|
||||
return [(dt_from - mx.DateTime.RelativeDateTime(hours=int(hours)*3), dt_from)]
|
||||
|
||||
resource_leave_ids = resource_cal_leaves.search(cr, uid, [('calendar_id','=',id), '|', ('resource_id','=',False), ('resource_id','=',resource)])
|
||||
res_leaves = resource_cal_leaves.read(cr, uid, resource_leave_ids, ['date_from', 'date_to'])
|
||||
|
||||
for leave in res_leaves:
|
||||
dtf = mx.DateTime.strptime(leave['date_from'], '%Y-%m-%d %H:%M:%S')
|
||||
dtt = mx.DateTime.strptime(leave['date_to'], '%Y-%m-%d %H:%M:%S')
|
||||
no = dtt - dtf
|
||||
[dt_leave.append((dtf + mx.DateTime.RelativeDateTime(days=x)).strftime('%Y-%m-%d')) for x in range(int(no.days + 1))]
|
||||
dt_leave.sort()
|
||||
dt_leave.reverse()
|
||||
|
||||
return dt_leave
|
||||
|
||||
def interval_min_get(self, cr, uid, id, dt_from, hours, resource=False):
|
||||
if not id:
|
||||
return [(dt_from - mx.DateTime.RelativeDateTime(hours=int(hours)*3), dt_from)]
|
||||
dt_leave = self._get_leaves(cr, uid, id, resource)
|
||||
dt_leave.reverse()
|
||||
todo = hours
|
||||
cycle = 0
|
||||
result = []
|
||||
@@ -85,18 +92,9 @@ class resource_calendar(osv.osv):
|
||||
return result
|
||||
|
||||
def interval_get(self, cr, uid, id, dt_from, hours, resource=False, byday=True):
|
||||
resource_cal_leaves = self.pool.get('resource.calendar.leaves')
|
||||
dt_leave = []
|
||||
if not id:
|
||||
return [(dt_from,dt_from + mx.DateTime.RelativeDateTime(hours=int(hours)*3))]
|
||||
resource_leave_ids = resource_cal_leaves.search(cr, uid, [('calendar_id','=',id), '|', ('resource_id','=',False), ('resource_id','=',resource)])
|
||||
res_leaves = resource_cal_leaves.read(cr, uid, resource_leave_ids, ['date_from', 'date_to'])
|
||||
for leave in res_leaves:
|
||||
dtf = mx.DateTime.strptime(leave['date_from'], '%Y-%m-%d %H:%M:%S')
|
||||
dtt = mx.DateTime.strptime(leave['date_to'], '%Y-%m-%d %H:%M:%S')
|
||||
no = dtt - dtf
|
||||
[dt_leave.append((dtf + mx.DateTime.RelativeDateTime(days=x)).strftime('%Y-%m-%d')) for x in range(int(no.days + 1))]
|
||||
dt_leave.sort()
|
||||
dt_leave = self._get_leaves(cr, uid, id, resource)
|
||||
todo = hours
|
||||
cycle = 0
|
||||
result = []
|
||||
@@ -105,29 +103,70 @@ class resource_calendar(osv.osv):
|
||||
while (todo>0) and maxrecur:
|
||||
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))
|
||||
for (hour_from,hour_to) in cr.fetchall():
|
||||
leave_flag = False
|
||||
if (hour_to>current_hour) and (todo>0):
|
||||
m = max(hour_from, current_hour)
|
||||
if (hour_to-m)>todo:
|
||||
hour_to = m+todo
|
||||
dt_check = dt_from.strftime('%Y-%m-%d')
|
||||
for leave in dt_leave:
|
||||
if dt_check == leave:
|
||||
dt_check = mx.DateTime.strptime(dt_check, "%Y-%m-%d") + mx.DateTime.RelativeDateTime(days=1)
|
||||
leave_flag = True
|
||||
if leave_flag:
|
||||
break
|
||||
else:
|
||||
d1 = mx.DateTime.DateTime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(m)), int((m%1) * 60))
|
||||
d2 = mx.DateTime.DateTime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(hour_to)), int((hour_to%1) * 60))
|
||||
result.append((d1, d2))
|
||||
current_hour = hour_to
|
||||
todo -= (hour_to - m)
|
||||
leave_flag = False
|
||||
if (hour_to>current_hour) and (todo>0):
|
||||
m = max(hour_from, current_hour)
|
||||
if (hour_to-m)>todo:
|
||||
hour_to = m+todo
|
||||
dt_check = dt_from.strftime('%Y-%m-%d')
|
||||
for leave in dt_leave:
|
||||
if dt_check == leave:
|
||||
dt_check = mx.DateTime.strptime(dt_check, "%Y-%m-%d") + mx.DateTime.RelativeDateTime(days=1)
|
||||
leave_flag = True
|
||||
if leave_flag:
|
||||
break
|
||||
else:
|
||||
d1 = mx.DateTime.DateTime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(m)), int((m%1) * 60))
|
||||
d2 = mx.DateTime.DateTime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(hour_to)), int((hour_to%1) * 60))
|
||||
result.append((d1, d2))
|
||||
current_hour = hour_to
|
||||
todo -= (hour_to - m)
|
||||
dt_from += mx.DateTime.RelativeDateTime(days=1)
|
||||
current_hour = 0
|
||||
maxrecur -= 1
|
||||
return result
|
||||
|
||||
def interval_hours_get(self, cr, uid, id, dt_from, dt_to, resource=False):
|
||||
result = []
|
||||
if not id:
|
||||
return 0.0
|
||||
dt_leave = self._get_leaves(cr, uid, id, resource)
|
||||
hours = 0.0
|
||||
|
||||
current_hour = dt_from.hour
|
||||
|
||||
while (dt_from <= dt_to):
|
||||
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))
|
||||
der = cr.fetchall()
|
||||
for (hour_from,hour_to) in der:
|
||||
if hours != 0.0:#For first time of the loop only,hours will be 0
|
||||
current_hour = hour_from
|
||||
leave_flag = False
|
||||
if (hour_to>=current_hour):
|
||||
dt_check = dt_from.strftime('%Y-%m-%d')
|
||||
for leave in dt_leave:
|
||||
if dt_check == leave:
|
||||
dt_check = mx.DateTime.strptime(dt_check, "%Y-%m-%d") + mx.DateTime.RelativeDateTime(days=1)
|
||||
leave_flag = True
|
||||
|
||||
if leave_flag:
|
||||
break
|
||||
else:
|
||||
d1 = dt_from
|
||||
d2 = mx.DateTime.DateTime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(hour_to)), int((hour_to%1) * 60))
|
||||
|
||||
if hours != 0.0:#For first time of the loop only,hours will be 0
|
||||
d1 = mx.DateTime.DateTime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(current_hour)), int((current_hour%1) * 60))
|
||||
|
||||
if dt_from.day == dt_to.day:
|
||||
if hour_from <= dt_to.hour <= hour_to:
|
||||
d2 = dt_to
|
||||
dt_from = d2
|
||||
hours += (d2-d1).seconds
|
||||
dt_from = mx.DateTime.DateTime(dt_from.year, dt_from.month, dt_from.day, int(math.floor(current_hour)), int((current_hour%1) * 60)) + mx.DateTime.RelativeDateTime(days=1)
|
||||
current_hour = 0.0
|
||||
return (hours/3600)
|
||||
|
||||
resource_calendar()
|
||||
|
||||
class resource_calendar_week(osv.osv):
|
||||
|
||||
Reference in New Issue
Block a user