[fix] complex bug in BufferedDataSet
[odoo/odoo.git] / addons / project_long_term / project_long_term.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
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
26
27 class project_phase(osv.osv):
28     _name = "project.phase"
29     _description = "Project Phase"
30
31     def _check_recursion(self, cr, uid, ids, context=None):
32          if context is None:
33             context = {}
34
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):
40              return False
41          ids = [id for id in prev_ids if id in next_ids]
42          # both prev_ids and next_ids must be unique
43          if ids:
44              return False
45          # unrelated project
46          prev_ids = [rec.id for rec in prev_ids]
47          next_ids = [rec.id for rec in next_ids]
48          # iter prev_ids
49          while prev_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:
53                  return False
54              ids = [id for id in prv_phase_ids if id in next_ids]
55              if ids:
56                  return False
57              prev_ids = prv_phase_ids
58          # iter next_ids
59          while next_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:
63                  return False
64              ids = [id for id in next_phase_ids if id in prev_ids]
65              if ids:
66                  return False
67              next_ids = next_phase_ids
68          return True
69
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']:
73                  return False
74          return True
75
76     def _compute_progress(self, cr, uid, ids, field_name, arg, context=None):
77         res = {}
78         if not ids:
79             return res
80         for phase in self.browse(cr, uid, ids, context=context):
81             if phase.state=='done':
82                 res[phase.id] = 100.0
83                 continue
84             elif phase.state=="cancelled":
85                 res[phase.id] = 0.0
86                 continue
87             elif not phase.task_ids:
88                 res[phase.id] = 0.0
89                 continue
90
91             tot = done = 0.0
92             for task in phase.task_ids:
93                 tot += task.total_hours
94                 done += min(task.effective_hours, task.total_hours)
95
96             if not tot:
97                 res[phase.id] = 0.0
98             else:
99                 res[phase.id] = round(100.0 * done / tot, 2)
100         return res
101
102     _columns = {
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 UoM', required=True, help="UoM (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 ressources on the project can be computed automatically by the scheduler"),
118         'state': fields.selection([('draft', 'New'), ('open', 'In Progress'), ('pending', 'Pending'), ('cancelled', 'Cancelled'), ('done', 'Done')], 'State', 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"),
122      }
123     _defaults = {
124         'state': 'draft',
125         'sequence': 10,
126         'product_uom': lambda self,cr,uid,c: self.pool.get('product.uom').search(cr, uid, [('name', '=', _('Day'))], context=c)[0]
127     }
128     _order = "project_id, date_start, sequence"
129     _constraints = [
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']),
132     ]
133
134     def onchange_project(self, cr, uid, ids, project, context=None):
135         return {}
136
137     def copy(self, cr, uid, id, default=None, context=None):
138         if default is None:
139             default = {}
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)
143
144     def set_draft(self, cr, uid, ids, *args):
145         self.write(cr, uid, ids, {'state': 'draft'})
146         return True
147
148     def set_open(self, cr, uid, ids, *args):
149         self.write(cr, uid, ids, {'state': 'open'})
150         return True
151
152     def set_pending(self, cr, uid, ids, *args):
153         self.write(cr, uid, ids, {'state': 'pending'})
154         return True
155
156     def set_cancel(self, cr, uid, ids, *args):
157         self.write(cr, uid, ids, {'state': 'cancelled'})
158         return True
159
160     def set_done(self, cr, uid, ids, *args):
161         self.write(cr, uid, ids, {'state': 'done'})
162         return True
163
164     def generate_phase(self, cr, uid, phases, context=None):
165         context = context or {}
166         result = ""
167
168         task_pool = self.pool.get('project.task')
169         for phase in phases:
170             if phase.state in ('done','cancelled'):
171                 continue
172             duration_uom = {
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
179             result += '''
180     def Phase_%s():
181         effort = \"%s\"''' % (phase.id, duration)
182             start = []
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,))
187             if start:
188                 result += '''
189         start = max(%s)
190 ''' % (','.join(start))
191
192             if phase.user_force_ids:
193                 result += '''
194         resource = %s
195 ''' % '|'.join(map(lambda x: 'User_'+str(x.id), phase.user_force_ids))
196
197             result += task_pool._generate_task(cr, uid, phase.task_ids, ident=8, context=context)
198             result += "\n"
199
200         return result
201 project_phase()
202
203 class project_user_allocation(osv.osv):
204     _name = 'project.user.allocation'
205     _description = 'Phase User Allocation'
206     _rec_name = 'user_id'
207     _columns = {
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"),
213     }
214 project_user_allocation()
215
216 class project(osv.osv):
217     _inherit = "project.project"
218     _columns = {
219         'phase_ids': fields.one2many('project.phase', 'project_id', "Project Phases"),
220     }
221     def schedule_phases(self, cr, uid, ids, context=None):
222         context = context or {}
223         if type(ids) in (long, int,):
224             ids = [ids]
225         projects = self.browse(cr, uid, ids, context=context)
226         result = self._schedule_header(cr, uid, ids, context=context)
227         for project in projects:
228             result += self._schedule_project(cr, uid, project, context=context)
229             result += self.pool.get('project.phase').generate_phase(cr, uid, project.phase_ids, context=context)
230
231         local_dict = {}
232         exec result in local_dict
233         projects_gantt = Task.BalancedProject(local_dict['Project'])
234
235         for project in projects:
236             project_gantt = getattr(projects_gantt, 'Project_%d' % (project.id,))
237             for phase in project.phase_ids:
238                 if phase.state in ('done','cancelled'):
239                     continue
240                 # Maybe it's better to update than unlink/create if it already exists ?
241                 p = getattr(project_gantt, 'Phase_%d' % (phase.id,))
242
243                 self.pool.get('project.user.allocation').unlink(cr, uid, 
244                     [x.id for x in phase.user_ids],
245                     context=context
246                 )
247
248                 for r in p.booked_resource:
249                     self.pool.get('project.user.allocation').create(cr, uid, {
250                         'user_id': int(r.name[5:]),
251                         'phase_id': phase.id,
252                         'date_start': p.start.strftime('%Y-%m-%d %H:%M:%S'),
253                         'date_end': p.end.strftime('%Y-%m-%d %H:%M:%S')
254                     }, context=context)
255                 self.pool.get('project.phase').write(cr, uid, [phase.id], {
256                     'date_start': p.start.strftime('%Y-%m-%d %H:%M:%S'),
257                     'date_end': p.end.strftime('%Y-%m-%d %H:%M:%S')
258                 }, context=context)
259         return True
260 project()
261
262 class project_task(osv.osv):
263     _inherit = "project.task"
264     _columns = {
265         'phase_id': fields.many2one('project.phase', 'Project Phase'),
266     }
267 project_task()
268
269 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: