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 ##############################################################################
22 from datetime import datetime
23 from tools.translate import _
24 from osv import fields, osv
25 from openerp.addons.resource.faces import task as Task
27 class project_phase(osv.osv):
28 _name = "project.phase"
29 _description = "Project Phase"
31 def _check_recursion(self, cr, uid, ids, context=None):
35 data_phase = self.browse(cr, uid, ids[0], context=context)
36 prev_ids = data_phase.previous_phase_ids
37 next_ids = data_phase.next_phase_ids
38 # it should neither be in prev_ids nor in next_ids
39 if (data_phase in prev_ids) or (data_phase in next_ids):
41 ids = [id for id in prev_ids if id in next_ids]
42 # both prev_ids and next_ids must be unique
46 prev_ids = [rec.id for rec in prev_ids]
47 next_ids = [rec.id for rec in next_ids]
50 cr.execute('SELECT distinct prv_phase_id FROM project_phase_rel WHERE next_phase_id IN %s', (tuple(prev_ids),))
51 prv_phase_ids = filter(None, map(lambda x: x[0], cr.fetchall()))
52 if data_phase.id in prv_phase_ids:
54 ids = [id for id in prv_phase_ids if id in next_ids]
57 prev_ids = prv_phase_ids
60 cr.execute('SELECT distinct next_phase_id FROM project_phase_rel WHERE prv_phase_id IN %s', (tuple(next_ids),))
61 next_phase_ids = filter(None, map(lambda x: x[0], cr.fetchall()))
62 if data_phase.id in next_phase_ids:
64 ids = [id for id in next_phase_ids if id in prev_ids]
67 next_ids = next_phase_ids
70 def _check_dates(self, cr, uid, ids, context=None):
71 for phase in self.read(cr, uid, ids, ['date_start', 'date_end'], context=context):
72 if phase['date_start'] and phase['date_end'] and phase['date_start'] > phase['date_end']:
76 def _compute_progress(self, cr, uid, ids, field_name, arg, context=None):
80 for phase in self.browse(cr, uid, ids, context=context):
81 if phase.state=='done':
84 elif phase.state=="cancelled":
87 elif not phase.task_ids:
92 for task in phase.task_ids:
93 tot += task.total_hours
94 done += min(task.effective_hours, task.total_hours)
99 res[phase.id] = round(100.0 * done / tot, 2)
103 'name': fields.char("Name", size=64, required=True),
104 'date_start': fields.datetime('Start Date', select=True, help="It's computed by the scheduler according the project date or the end date of the previous phase.", states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}),
105 'date_end': fields.datetime('End Date', help=" It's computed by the scheduler according to the start date and the duration.", states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}),
106 'constraint_date_start': fields.datetime('Minimum Start Date', help='force the phase to start after this date', states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}),
107 'constraint_date_end': fields.datetime('Deadline', help='force the phase to finish before this date', states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}),
108 'project_id': fields.many2one('project.project', 'Project', required=True, select=True),
109 'next_phase_ids': fields.many2many('project.phase', 'project_phase_rel', 'prv_phase_id', 'next_phase_id', 'Next Phases', states={'cancelled':[('readonly',True)]}),
110 'previous_phase_ids': fields.many2many('project.phase', 'project_phase_rel', 'next_phase_id', 'prv_phase_id', 'Previous Phases', states={'cancelled':[('readonly',True)]}),
111 'sequence': fields.integer('Sequence', select=True, help="Gives the sequence order when displaying a list of phases."),
112 'duration': fields.float('Duration', required=True, help="By default in days", states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}),
113 'product_uom': fields.many2one('product.uom', 'Duration Unit of Measure', required=True, help="Unit of Measure (Unit of Measure) is the unit of measurement for Duration", states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}),
114 'task_ids': fields.one2many('project.task', 'phase_id', "Project Tasks", states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]}),
115 'user_force_ids': fields.many2many('res.users', string='Force Assigned Users'),
116 'user_ids': fields.one2many('project.user.allocation', 'phase_id', "Assigned Users",states={'done':[('readonly',True)], 'cancelled':[('readonly',True)]},
117 help="The resources on the project can be computed automatically by the scheduler."),
118 'state': fields.selection([('draft', 'New'), ('cancelled', 'Cancelled'),('open', 'In Progress'), ('pending', 'Pending'), ('done', 'Done')], 'Status', readonly=True, required=True,
119 help='If the phase is created the state \'Draft\'.\n If the phase is started, the state becomes \'In Progress\'.\n If review is needed the phase is in \'Pending\' state.\
120 \n If the phase is over, the states is set to \'Done\'.'),
121 'progress': fields.function(_compute_progress, string='Progress', help="Computed based on related tasks"),
126 'product_uom': lambda self,cr,uid,c: self.pool.get('product.uom').search(cr, uid, [('name', '=', _('Day'))], context=c)[0]
128 _order = "project_id, date_start, sequence"
130 (_check_recursion,'Loops in phases not allowed',['next_phase_ids', 'previous_phase_ids']),
131 (_check_dates, 'Phase start-date must be lower than phase end-date.', ['date_start', 'date_end']),
134 def onchange_project(self, cr, uid, ids, project, context=None):
137 def copy(self, cr, uid, id, default=None, context=None):
140 if not default.get('name', False):
141 default['name'] = self.browse(cr, uid, id, context=context).name + _(' (copy)')
142 return super(project_phase, self).copy(cr, uid, id, default, context)
144 def set_draft(self, cr, uid, ids, *args):
145 self.write(cr, uid, ids, {'state': 'draft'})
148 def set_open(self, cr, uid, ids, *args):
149 self.write(cr, uid, ids, {'state': 'open'})
152 def set_pending(self, cr, uid, ids, *args):
153 self.write(cr, uid, ids, {'state': 'pending'})
156 def set_cancel(self, cr, uid, ids, *args):
157 self.write(cr, uid, ids, {'state': 'cancelled'})
160 def set_done(self, cr, uid, ids, *args):
161 self.write(cr, uid, ids, {'state': 'done'})
164 def generate_phase(self, cr, uid, phases, context=None):
165 context = context or {}
168 task_pool = self.pool.get('project.task')
170 if phase.state in ('done','cancelled'):
173 'days': 'd', 'day': 'd', 'd':'d',
174 'months': 'm', 'month':'month', 'm':'m',
175 'weeks': 'w', 'week': 'w', 'w':'w',
176 'hours': 'H', 'hour': 'H', 'h':'H',
177 }.get(phase.product_uom.name.lower(), "H")
178 duration = str(phase.duration) + duration_uom
181 effort = \"%s\"''' % (phase.id, duration)
183 if phase.constraint_date_start:
184 start.append('datetime.datetime.strptime("'+str(phase.constraint_date_start)+'", "%Y-%m-%d %H:%M:%S")')
185 for previous_phase in phase.previous_phase_ids:
186 start.append("up.Phase_%s.end" % (previous_phase.id,))
190 ''' % (','.join(start))
192 if phase.user_force_ids:
195 ''' % '|'.join(map(lambda x: 'User_'+str(x.id), phase.user_force_ids))
197 result += task_pool._generate_task(cr, uid, phase.task_ids, ident=8, context=context)
203 class project_user_allocation(osv.osv):
204 _name = 'project.user.allocation'
205 _description = 'Phase User Allocation'
206 _rec_name = 'user_id'
208 'user_id': fields.many2one('res.users', 'User', required=True),
209 'phase_id': fields.many2one('project.phase', 'Project Phase', ondelete='cascade', required=True),
210 'project_id': fields.related('phase_id', 'project_id', type='many2one', relation="project.project", string='Project', store=True),
211 'date_start': fields.datetime('Start Date', help="Starting Date"),
212 'date_end': fields.datetime('End Date', help="Ending Date"),
214 project_user_allocation()
216 class project(osv.osv):
217 _inherit = "project.project"
219 def _phase_count(self, cr, uid, ids, field_name, arg, context=None):
220 res = dict.fromkeys(ids, 0)
221 phase_ids = self.pool.get('project.phase').search(cr, uid, [('project_id', 'in', ids)])
222 for phase in self.pool.get('project.phase').browse(cr, uid, phase_ids, context):
223 res[phase.project_id.id] += 1
227 'phase_ids': fields.one2many('project.phase', 'project_id', "Project Phases"),
228 'phase_count': fields.function(_phase_count, type='integer', string="Open Phases"),
231 def schedule_phases(self, cr, uid, ids, context=None):
232 context = context or {}
233 if type(ids) in (long, int,):
235 projects = self.browse(cr, uid, ids, context=context)
236 result = self._schedule_header(cr, uid, ids, context=context)
237 for project in projects:
238 result += self._schedule_project(cr, uid, project, context=context)
239 result += self.pool.get('project.phase').generate_phase(cr, uid, project.phase_ids, context=context)
242 exec result in local_dict
243 projects_gantt = Task.BalancedProject(local_dict['Project'])
245 for project in projects:
246 project_gantt = getattr(projects_gantt, 'Project_%d' % (project.id,))
247 for phase in project.phase_ids:
248 if phase.state in ('done','cancelled'):
250 # Maybe it's better to update than unlink/create if it already exists ?
251 p = getattr(project_gantt, 'Phase_%d' % (phase.id,))
253 self.pool.get('project.user.allocation').unlink(cr, uid,
254 [x.id for x in phase.user_ids],
258 for r in p.booked_resource:
259 self.pool.get('project.user.allocation').create(cr, uid, {
260 'user_id': int(r.name[5:]),
261 'phase_id': phase.id,
262 'date_start': p.start.strftime('%Y-%m-%d %H:%M:%S'),
263 'date_end': p.end.strftime('%Y-%m-%d %H:%M:%S')
265 self.pool.get('project.phase').write(cr, uid, [phase.id], {
266 'date_start': p.start.strftime('%Y-%m-%d %H:%M:%S'),
267 'date_end': p.end.strftime('%Y-%m-%d %H:%M:%S')
272 class account_analytic_account(osv.osv):
273 _inherit = 'account.analytic.account'
274 _description = 'Analytic Account'
276 'use_phases': fields.boolean('Phases Planing', help="Check this field if project manages phases"),
279 def on_change_template(self, cr, uid, ids, template_id, context=None):
280 res = super(account_analytic_account, self).on_change_template(cr, uid, ids, template_id, context=context)
281 if template_id and 'value' in res:
282 template = self.browse(cr, uid, template_id, context=context)
283 res['value']['use_phases'] = template.use_phases
286 def _trigger_project_creation(self, cr, uid, vals, context=None):
287 res= super(account_analytic_account, self)._trigger_project_creation(cr, uid, vals, context=context)
288 return res or vals.get('use_phases')
290 account_analytic_account()
292 class project_task(osv.osv):
293 _inherit = "project.task"
295 'phase_id': fields.many2one('project.phase', 'Project Phase'),
299 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: