1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
23 from resource.faces import *
24 from new import classobj
27 from tools.translate import _
28 from osv import osv, fields
30 import working_calendar as wkcal
32 class project_schedule_task(osv.osv_memory):
34 _name = "project.schedule.tasks"
35 _description = 'project.schedule.tasks'
37 'msg': fields.char('Message', size=64)
40 'msg': 'Task Scheduling Completed Successfully'
43 def default_get(self, cr, uid, fields_list, context=None):
46 res = super(project_schedule_task, self).default_get(cr, uid, fields_list, context)
47 self.compute_date(cr, uid, context=context)
50 def create_resources(self, cr, uid, phase, context=None):
52 Return a list of Resource Class objects for the resources allocated to the phase.
59 for resource in phase.resource_ids:
60 res = resource.resource_id
62 resource_eff = res.time_efficiency
63 resource_cal = res.calendar_id.id
65 cal_id = phase.project_id.resource_calendar_id and phase.project_id.resource_calendar_id.id or False
66 leaves = wkcal.compute_leaves(cr, uid, cal_id, res.id, resource_cal, context=context)
67 resource_objs.append(classobj(res.user_id.name.encode('utf8'), (Resource,),{
68 '__doc__': res.user_id.name,
69 '__name__': res.user_id.name,
70 'vacation': tuple(leaves),
71 'efficiency': resource_eff,
75 def compute_date(self, cr, uid, context=None):
77 Schedule the tasks according to resource available and priority.
80 phase_obj = self.pool.get('project.phase')
81 task_obj = self.pool.get('project.task')
82 user_obj = self.pool.get('res.users')
85 # It makes no sense to continue on empty context
86 return { 'warning': _("You must select some project phase to compute on")}
88 if not 'active_id' in context:
90 phase = phase_obj.browse(cr, uid, context['active_id'], context=context)
91 task_ids = map(lambda x : x.id, (filter(lambda x : x.state in ['open', 'draft', 'pending'] , phase.task_ids)))
94 tasks = task_obj.browse(cr, uid, task_ids, context=context)
95 start_date = str(phase.date_start)
96 if not phase.date_start:
97 if not phase.project_id.date_start:
98 start_date = datetime.datetime.now().strftime("%Y-%m-%d")
100 start_date = phase.project_id.date_start
101 date_start = datetime.datetime.strftime(datetime.datetime.strptime(start_date, "%Y-%m-%d"), "%Y-%m-%d %H:%M")
102 calendar_id = phase.project_id.resource_calendar_id.id
103 resources = self.create_resources(cr, uid, phase)
104 priority_dict = {'0': 1000, '1': 800, '2': 500, '3': 300, '4': 100}
105 # Create dynamic no of tasks with the resource specified
106 def create_tasks(j, eff, priorty=500, obj=False):
109 task is a dynamic method!
115 task.__doc__ = "TaskNO%d" %j
116 task.__name__ = "task%d" %j
119 # Create a 'Faces' project with all the tasks and resources
124 resource = reduce(operator.or_, resources)
126 raise osv.except_osv(_('Error'), _('Phase must have resources assigned !'))
127 minimum_time_unit = 1
128 if calendar_id: # If project has working calendar
129 working_days = wkcal.compute_working_calendar(cr, uid, calendar_id)
130 vacation = tuple(wkcal.compute_leaves(cr, uid, calendar_id))
131 # Dynamic creation of tasks
133 for each_task in tasks:
134 hours = str(each_task.planned_hours )+ 'H'
135 if each_task.priority in priority_dict.keys():
136 priorty = priority_dict[each_task.priority]
138 if each_task.user_id:
139 for resrce in resources:
140 if resrce.__name__ == each_task.user_id.name:
144 task = create_tasks(i, hours, priorty, resc)
147 project = BalancedProject(Project)
150 # Write back the computed dates
152 s_date = t.start.to_datetime()
153 e_date = t.end.to_datetime()
156 ctx.update({'scheduler': True})
157 user_id = user_obj.search(cr, uid, [('name', '=', t.booked_resource[0].__name__)])
158 task_obj.write(cr, uid, [tasks[loop_no-1].id], {
159 'date_start': s_date.strftime('%Y-%m-%d %H:%M:%S'),
160 'date_end': e_date.strftime('%Y-%m-%d %H:%M:%S'),
161 'user_id': user_id[0]
166 return {"warning": _("No tasks to compute for this phase") }
169 project_schedule_task()
171 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: