[FIX] Schedule jobs even if their next time has passed.
[odoo/odoo.git] / addons / scrum / report / _burndown.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23
24 from mx import DateTime
25 import time
26
27 def compute_burndown(cr, uid, tasks_id, date_start, date_stop):
28     latest = False
29     if tasks_id:
30         cr.execute('select id,create_date,state,planned_hours from project_task where id in %s order by create_date', (tuple(tasks_id),))
31         tasks = cr.fetchall()
32
33         cr.execute('select w.date,w.hours from project_task_work w left join project_task t on (t.id=w.task_id) where t.id in %s and t.state in (\'open\',\'progress\') order by date', (tuple(tasks_id),))
34
35         tasks2 = cr.fetchall()
36
37         cr.execute('select date_close,planned_hours from project_task where id in %s and state in (\'cancelled\',\'done\') order by date_close', (tuple(tasks_id),))
38         tasks2 += cr.fetchall()
39         tasks2.sort()
40     else:
41         tasks = []
42         tasks2 = []
43
44     current_date = date_start
45     total = 0
46     done = 0
47     result = []
48     while current_date<=date_stop:
49         while len(tasks) and tasks[0][1] and tasks[0][1][:10]<=current_date:
50             latest = tasks.pop(0)
51             total += latest[3]
52         i = 0
53         while i<len(tasks2):
54             if tasks2[i][0] and tasks2[i][0][:10]<=current_date:
55                 t = tasks2.pop(i)
56                 done += t[1]
57             else:
58                 i+=1
59         result.append( (int(time.mktime(time.strptime(current_date,'%Y-%m-%d'))), total-done) )
60         current_date = (DateTime.strptime(current_date, '%Y-%m-%d') + DateTime.RelativeDateTime(days=1)).strftime('%Y-%m-%d')
61         if not len(tasks) and not len(tasks2):
62             break
63     result.append( (int(time.mktime(time.strptime(date_stop,'%Y-%m-%d'))), 0) )
64     return result
65
66 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: