[IMP]all: remove a complexity in openerp and move asset management and Payroll in...
[odoo/odoo.git] / addons / project_planning / project_planning.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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 Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (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 Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import time
24 from datetime import datetime
25 from dateutil.relativedelta import relativedelta
26
27 from osv import fields, osv
28 import tools
29
30
31 class one2many_mod3(fields.one2many):
32     def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
33         res = {}
34         for obj in obj.browse(cr, user, ids, context=context):
35             res[obj.id] = []
36             list_ids = []
37             children = obj.pool.get('report_account_analytic.planning')._child_compute(cr, user, [obj.user_id.id], '', [])
38             for u_id in children.get(obj.user_id.id, []):
39                 list_ids.append(u_id)
40             list_ids.append(obj.user_id.id)
41             ids2 = obj.pool.get(self._obj).search(cr, user, ['&',(self._fields_id,'=',obj.id),'|',('user_id','in',list_ids),('user_id','=',False)], limit=self._limit)
42             for r in obj.pool.get(self._obj)._read_flat(cr, user, ids2, [self._fields_id], context=context, load='_classic_write'):
43                 if r[self._fields_id] not in res:
44                     res[r[self._fields_id]] = []
45                 res[r[self._fields_id]].append( r['id'] )
46         return res
47
48 class report_account_analytic_planning(osv.osv):
49     _name = "report_account_analytic.planning"
50     _description = "Planning"
51
52     def emp_to_users(self, cr, uid, ids, context=None):
53         employees = self.pool.get('hr.employee').browse(cr, uid, ids, context=context)
54         user_ids = [e.user_id.id for e in employees if e.user_id]
55         return user_ids
56
57     def _child_compute(self, cr, uid, ids, name, args, context=None):
58         obj_dept = self.pool.get('hr.department')
59         obj_user = self.pool.get('res.users')
60         result = {}
61         for user_id in ids:
62             child_ids = []
63             cr.execute("""SELECT dept.id FROM hr_department AS dept
64                 LEFT JOIN hr_employee AS emp ON dept.manager_id = emp.id
65                 WHERE emp.id IN
66                     (SELECT emp.id FROM hr_employee
67                         JOIN resource_resource r ON r.id = emp.resource_id WHERE r.user_id = %s)
68                 """, (user_id,))
69             mgnt_dept_ids = [x[0] for x in cr.fetchall()]
70             ids_dept = obj_dept.search(cr, uid, [('id', 'child_of', mgnt_dept_ids)], context=context)
71             if ids_dept:
72                 data_dept = obj_dept.read(cr, uid, ids_dept, ['member_ids'], context=context)
73                 emp_children = map(lambda x: x['member_ids'], data_dept)
74                 emp_children = tools.flatten(emp_children)
75                 children = self.emp_to_users(cr, uid, emp_children, context=context)
76                 children = obj_user.search(cr, uid, [('id', 'in', children),('active', '=', True)], context=context)
77                 if user_id in children:
78                     children.remove(user_id)
79                 child_ids = list(set(child_ids + children))
80             result[user_id] = child_ids
81         return result
82
83     def _get_total_planned(self, cr, uid, ids, name, args, context=None):
84         result = {}
85         for plan in self.browse(cr, uid, ids, context=context):
86             plan_hrs=0.0
87             for p in plan.planning_user_ids:
88                 if not p.plan_open : p.plan_open = 0.0
89                 if not p.plan_tasks : p.plan_tasks = 0.0
90                 plan_hrs = plan_hrs + p.plan_open + p.plan_tasks
91             result[plan.id] = plan_hrs
92         return result
93
94     def _get_total_free(self, cr, uid, ids, name, args, context=None):
95         result = {}
96         for plan in self.browse(cr, uid, ids, context=context):
97             total_free = 0.0
98             for p in plan.planning_user_ids:
99                 if  p.free:
100                     total_free = total_free + p.free
101             result[plan.id] = total_free
102         return result
103
104     def _check_planning_responsible(self, cr, uid, ids, context=None):
105         for obj_plan in self.browse(cr, uid, ids, context=context):
106             cr.execute("""
107                 SELECT id FROM report_account_analytic_planning plan
108                 WHERE (   (%(date_from)s BETWEEN date_from AND date_to)
109                                 OR (%(date_to)s BETWEEN date_from AND date_to)
110                                 OR (%(date_from)s < date_from AND %(date_to)s > date_to)
111                                )  AND user_id = %(uid)s AND id <> %(id)s""",
112                         {"date_from": obj_plan.date_from,
113                          "date_to": obj_plan.date_to,
114                          "uid": obj_plan.user_id.id,
115                          "id" : obj_plan.id}
116                                )
117
118             res = cr.fetchone()
119             if res:
120                 return False
121         return True
122
123     _columns = {
124         'name': fields.char('Planning Name', required=True, size=32, states={'done':[('readonly', True)]}),
125         'code': fields.char('Code', size=32, states={'done':[('readonly', True)]}),
126         'user_id': fields.many2one('res.users', 'Responsible', required=True, states={'done':[('readonly', True)]}),
127         'date_from': fields.date('Start Date', required=True, states={'done':[('readonly', True)]}),
128         'date_to':fields.date('End Date', required=True, states={'done':[('readonly', True)]}),
129         'line_ids': fields.one2many('report_account_analytic.planning.line', 'planning_id', 'Planning lines', states={'done':[('readonly', True)]}),
130         'stat_ids': fields.one2many('report_account_analytic.planning.stat', 'planning_id', 'Planning analysis', readonly=True),
131         'state': fields.selection([('draft', 'Draft'), ('open', 'Open'), ('done', 'Done'), ('cancel', 'Cancelled')], 'Status', required=True),
132         'business_days': fields.integer('Business Days', required=True, states={'done':[('readonly', True)]}, help='Set here the number of working days within this planning for one person full time'),
133         'planning_user_ids': one2many_mod3('report_account_analytic.planning.user', 'planning_id', 'Planning By User'),
134         'planning_account': fields.one2many('report_account_analytic.planning.account', 'planning_id', 'Planning By Account'),
135         'total_planned': fields.function(_get_total_planned, string='Total Planned'),
136         'total_free': fields.function(_get_total_free, string='Total Free'),
137     }
138     _defaults = {
139         'date_from': lambda self,cr,uid,ctx: fields.date.context_today(self,cr,uid,timestamp=(datetime.now()+relativedelta(day=1)),context=ctx),
140         'date_to': lambda self,cr,uid,ctx: fields.date.context_today(self,cr,uid,timestamp=(datetime.now()+relativedelta(months=1, day=1, days=-1)),context=ctx),
141         'user_id': lambda self, cr, uid, c: uid,
142         'state': 'draft',
143         'business_days': 20,
144     }
145     _order = 'date_from desc'
146
147     _constraints = [
148         (_check_planning_responsible, 'Invalid planning ! Planning dates can\'t overlap for the same responsible. ', ['user_id'])
149     ]
150
151     def action_open(self, cr, uid, id, context=None):
152         self.write(cr, uid, id, {'state' : 'open'}, context=context)
153         return True
154
155     def action_cancel(self, cr, uid, id, context=None):
156         self.write(cr, uid, id, {'state' : 'cancel'}, context=context)
157         return True
158
159     def action_draft(self, cr, uid, id, context=None):
160         self.write(cr, uid, id, {'state' : 'draft'}, context=context)
161         return True
162
163     def action_done(self, cr, uid, id, context=None):
164         self.write(cr, uid, id, {'state' : 'done'}, context=context)
165         return True
166
167 report_account_analytic_planning()
168
169 class report_account_analytic_planning_line(osv.osv):
170     _name = "report_account_analytic.planning.line"
171     _description = "Planning Line"
172     _rec_name = 'user_id'
173
174     def name_get(self, cr, uid, ids, context=None):
175         if not len(ids):
176             return []
177         reads = self.read(cr, uid, ids, ['user_id', 'planning_id', 'note'], context=context)
178         res = []
179         for record in reads:
180             name = '['+record['planning_id'][1]
181             if record['user_id']:
182                 name += " - " +record['user_id'][1]+'] '
183             else:
184                 name += '] '
185             if record['note']:
186                 name += record['note']
187             res.append((record['id'], name))
188         return res
189
190     def _amount_base_uom(self, cr, uid, ids, name, args, context=None):
191         users_obj = self.pool.get('res.users')
192         result = {}
193         tm = users_obj.browse(cr, uid, uid, context=context).company_id.planning_time_mode_id
194         if tm and tm.factor:
195             div = tm.factor
196         else:
197             div = 1.0
198         for line in self.browse(cr, uid, ids, context=context):
199             result[line.id] = line.amount / line.amount_unit.factor * div
200         return result
201
202     _columns = {
203         'account_id': fields.many2one('account.analytic.account', 'Analytic account', select=True),
204         'planning_id': fields.many2one('report_account_analytic.planning', 'Planning', required=True, ondelete='cascade'),
205         'user_id': fields.many2one('res.users', 'User', select=True),
206         'amount': fields.float('Quantity', required=True),
207         'amount_unit': fields.many2one('product.uom', 'Qty Unit of Measure', required=True),
208         'note': fields.text('Note', size=64),
209         'amount_in_base_uom': fields.function(_amount_base_uom, string='Quantity in base Unit of Measure', store=True),
210         'task_ids': fields.one2many('project.task', 'planning_line_id', 'Planning Tasks'),
211     }
212     _order = 'user_id, account_id'
213
214 report_account_analytic_planning_line()
215
216 class account_analytic_account(osv.osv):
217     _name = 'account.analytic.account'
218     _inherit = 'account.analytic.account'
219     _columns = {
220         'planning_ids': fields.one2many('report_account_analytic.planning.line', 'account_id', 'Plannings'),
221     }
222
223 account_analytic_account()
224
225 class project_task(osv.osv):
226     _name = "project.task"
227     _inherit = "project.task"
228     _columns = {
229         'planning_line_id': fields.many2one('report_account_analytic.planning.line', 'Planning', ondelete='cascade'),
230     }
231
232     def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
233         if context is None:
234             context = {}
235         if not context.get('planning', False):
236             return super(project_task,self).search(cr, user, args,
237                 offset=offset, limit=limit, order=order, context=context, count=count)
238         cr.execute(" SELECT t.id, t.name \
239                         from project_task t \
240                         join report_account_analytic_planning_line l on (l.id = t.planning_line_id )\
241                         where l.planning_id=%s",(context.get('planning'),))
242         ids = map(lambda x: x[0], cr.fetchall())
243         return ids
244
245 project_task()
246
247 class report_account_analytic_planning_user(osv.osv):
248     _name = "report_account_analytic.planning.user"
249     _description = "Planning by User"
250     _rec_name = 'user_id'
251     _auto = False
252
253     def _get_tasks(self, cr, uid, ids, name, args, context=None):
254         users_obj = self.pool.get('res.users')
255         result = {}
256         tm = users_obj.browse(cr, uid, uid, context=context).company_id.project_time_mode_id
257         if tm and tm.factor:
258             div = tm.factor
259         else:
260             div = 1.0
261         tm2 = users_obj.browse(cr, uid, uid, context=context).company_id.planning_time_mode_id
262         if tm2 and tm2.factor:
263             div2 = tm2.factor
264         else:
265             div2 = 1.0
266         for line in self.browse(cr, uid, ids, context=context):
267             if line.user_id:
268                 cr.execute("""select COALESCE(sum(tasks.remaining_hours),0) from project_task tasks \
269                                where  tasks.planning_line_id IN (select id from report_account_analytic_planning_line\
270                 where planning_id = %s and user_id=%s)""", (line.planning_id.id, line.user_id.id,))
271
272                 result[line.id] = cr.fetchall()[0][0] / div * div2
273             else:
274                 result[line.id] = 0
275         return result
276
277     def _get_free(self, cr, uid, ids, name, args, context=None):
278         result = {}
279         for line in self.browse(cr, uid, ids, context=context):
280             if line.user_id:
281                 result[line.id] = line.planning_id.business_days - line.plan_tasks - line.plan_open - line.holiday
282             else:
283                 result[line.id] = 0.0
284         return result
285
286     def _get_timesheets(self, cr, uid, ids, name, args, context=None):
287         users_obj = self.pool.get('res.users')
288         result = {}
289         tm2 = users_obj.browse(cr, uid, uid, context=context).company_id.planning_time_mode_id
290         if tm2 and tm2.factor:
291             div2 = tm2.factor
292         else:
293             div2 = 1.0
294         for line in self.browse(cr, uid, ids, context=context):
295             if line.user_id:
296                 cr.execute("""
297                 SELECT sum(unit_amount/uom.factor) FROM account_analytic_line acc
298                 LEFT JOIN product_uom uom ON (uom.id = acc.product_uom_id)
299                 WHERE acc.date>=%s and acc.date<=%s and acc.user_id=%s""", (line.planning_id.date_from, line.planning_id.date_to, line.user_id.id ))
300                 res = cr.fetchall()
301                 result[line.id] = res[0][0] and res[0][0] * div2 or False
302             else:
303                 result[line.id] = 0
304         return result
305
306     _columns = {
307         'planning_id': fields.many2one('report_account_analytic.planning', 'Planning'),
308         'user_id': fields.many2one('res.users', 'User', readonly=True),
309         'tasks': fields.function(_get_tasks, string='Remaining Tasks', help='This value is given by the sum of work remaining to do on the task for this planning, expressed in days.'),
310         'plan_tasks': fields.float('Time Planned on Tasks', readonly=True, help='This value is given by the sum of time allocation with task(s) linked, expressed in days.'),
311         'free': fields.function(_get_free, string='Unallocated Time', readonly=True, help='Computed as \
312 Business Days - (Time Allocation of Tasks + Time Allocation without Tasks + Holiday Leaves)'),
313         'plan_open': fields.float('Time Allocation without Tasks', readonly=True,help='This value is given by the sum of time allocation without task(s) linked, expressed in days.'),
314         'holiday': fields.float('Leaves',help='This value is given by the total of validated leaves into the \'Date From\' and \'Date To\' of the planning.'),
315         'timesheet': fields.function(_get_timesheets, string='Timesheet', help='This value is given by the sum of all work encoded in the timesheet(s) between the \'Date From\' and \'Date To\' of the planning.'),
316     }
317
318     def init(self, cr):
319         cr.execute(""" CREATE OR REPLACE VIEW report_account_analytic_planning_user AS (
320         SELECT
321             planning.id AS planning_id,
322             (1000*(planning.id) + users.id)::integer AS id,
323             planning.business_days,
324             users.id AS user_id,
325             (SELECT sum(line1.amount_in_base_uom)
326                 FROM report_account_analytic_planning_line line1
327                 WHERE   (
328                         SELECT COUNT(1)
329                         FROM project_task task
330                         WHERE task.planning_line_id = line1.id
331                         ) > 0
332                 AND line1.user_id = users.id
333                 AND line1.planning_id = planning.id
334             )AS plan_tasks,
335             (SELECT SUM(line1.amount_in_base_uom)
336                 FROM report_account_analytic_planning_line line1
337                 WHERE   (
338                         SELECT COUNT(1)
339                         FROM project_task task
340                         WHERE task.planning_line_id = line1.id
341                         ) = 0
342                 AND line1.user_id = users.id
343                 AND line1.planning_id = planning.id
344             ) AS plan_open,
345             (SELECT -(SUM(holidays.number_of_days))
346                 FROM hr_holidays holidays
347                 WHERE holidays.employee_id IN
348                     (
349                     SELECT emp.id
350                     FROM hr_employee emp, resource_resource res WHERE emp.resource_id = res.id and res.user_id = users.id
351                     )
352                 AND holidays.state IN ('validate')
353                 AND holidays.type = 'remove'
354                 AND holidays.date_from >= planning.date_from
355                 AND holidays.date_to <= planning.date_to
356             ) AS holiday
357
358         FROM report_account_analytic_planning planning
359         LEFT JOIN report_account_analytic_planning_line line ON (line.planning_id = planning.id), res_users users
360         GROUP BY planning.id, planning.business_days, users.id, planning.date_from, planning.date_to
361
362         UNION
363
364         SELECT
365             planning.id AS planning_id,
366             (1000*(planning.id) - 1)::integer AS id,
367             planning.business_days,
368             line.user_id,
369             (SELECT SUM(line1.amount_in_base_uom)
370                 FROM report_account_analytic_planning_line line1
371                 WHERE (SELECT COUNT(1) FROM project_task task WHERE task.planning_line_id = line1.id) > 0
372                 AND line1.user_id IS NULL
373             ) AS plan_tasks,
374             (SELECT SUM(line1.amount_in_base_uom)
375                 FROM report_account_analytic_planning_line line1
376                 WHERE (SELECT COUNT(1) FROM project_task task WHERE task.planning_line_id = line1.id) = 0
377                 AND line1.user_id IS NULL
378             ) AS plan_open,
379             '0' AS holiday
380         FROM report_account_analytic_planning planning
381         INNER JOIN report_account_analytic_planning_line line ON line.planning_id = planning.id
382             AND line.user_id IS NULL
383         GROUP BY planning.id, planning.business_days, line.user_id, planning.date_from, planning.date_to
384         )
385         """)
386
387 report_account_analytic_planning_user()
388
389 class report_account_analytic_planning_account(osv.osv):
390     _name = "report_account_analytic.planning.account"
391     _description = "Planning by Account"
392     _rec_name = 'account_id'
393     _auto = False
394
395     def _get_tasks(self, cr, uid, ids, name, args, context=None):
396         users_obj = self.pool.get('res.users')
397         result = {}
398         tm = users_obj.browse(cr, uid, uid, context=context).company_id.project_time_mode_id
399         if tm and tm.factor:
400             div = tm.factor
401         else:
402             div = 1.0
403         tm2 = users_obj.browse(cr, uid, uid, context=context).company_id.planning_time_mode_id
404         if tm2 and tm2.factor:
405             div2 = tm2.factor
406         else:
407             div2 = 1.0
408         for line in self.browse(cr, uid, ids, context=context):
409             cr.execute("""
410                 SELECT COALESCE(sum(tasks.remaining_hours),0)
411                 FROM project_task tasks
412                 WHERE tasks.planning_line_id IN (
413                     SELECT id
414                     FROM report_account_analytic_planning_line
415                     WHERE planning_id = %s AND account_id=%s
416                 )""", (line.planning_id.id, line.account_id and line.account_id.id or None))
417             result[line.id] = cr.fetchall()[0][0] / div * div2
418         return result
419
420     def _get_timesheets(self, cr, uid, ids, name, args, context=None):
421         users_obj = self.pool.get('res.users')
422         result = {}
423         tm2 = users_obj.browse(cr, uid, uid, context=context).company_id.planning_time_mode_id
424         if tm2 and tm2.factor:
425             div2 = tm2.factor
426         else:
427             div2 = 1.0
428         for line in self.browse(cr, uid, ids, context=context):
429             cr.execute("""
430                 SELECT SUM(unit_amount/uom.factor) FROM account_analytic_line acc
431                 LEFT JOIN product_uom uom ON (uom.id = acc.product_uom_id)
432                 WHERE acc.date>=%s and acc.date<=%s and acc.account_id=%s""", (line.planning_id.date_from, line.planning_id.date_to, line.account_id and line.account_id.id or None))
433             res = cr.fetchall()[0][0]
434             if res:
435                 result[line.id] = res * div2
436             else:
437                 result[line.id] = 0
438         return result
439
440     _columns = {
441         'planning_id': fields.many2one('report_account_analytic.planning', 'Planning'),
442         'account_id': fields.many2one('account.analytic.account', 'Analytic account', readonly=True),
443         'tasks': fields.function(_get_tasks, string='Remaining Tasks', help='This value is given by the sum of work remaining to do on the task for this planning, expressed in days.'),
444         'plan_tasks': fields.float('Time Allocation of Tasks', readonly=True, help='This value is given by the sum of time allocation with the checkbox \'Assigned in Taks\' set to TRUE expressed in days.'),
445         'plan_open': fields.float('Time Allocation without Tasks', readonly=True, help='This value is given by the sum of time allocation with the checkbox \'Assigned in Taks\' set to FALSE, expressed in days.'),
446         'timesheet': fields.function(_get_timesheets, string='Timesheet', help='This value is given by the sum of all work encoded in the timesheet(s) between the \'Date From\' and \'Date To\' of the planning.'),
447     }
448
449     def init(self, cr):
450         cr.execute(""" CREATE OR REPLACE VIEW report_account_analytic_planning_account AS (
451           SELECT
452             MIN(l.id) AS id,
453             l.account_id AS account_id,
454             SUM(l.amount) AS quantity,
455             l.planning_id AS planning_id,
456             ( SELECT SUM(line1.amount_in_base_uom)
457               FROM report_account_analytic_planning_line line1
458               WHERE
459                 ( SELECT COUNT(1)
460                   FROM project_task task
461                   WHERE task.planning_line_id = line1.id
462                 ) > 0
463                 AND l.account_id = line1.account_id
464                 AND l.planning_id = line1.planning_id
465             ) AS plan_tasks,
466             ( SELECT SUM(line1.amount_in_base_uom)
467               FROM report_account_analytic_planning_line line1
468               WHERE
469                 ( SELECT COUNT(1)
470                   FROM project_task task
471                   WHERE task.planning_line_id = line1.id
472                 ) = 0
473                 AND l.account_id = line1.account_id
474                 AND planning.id = line1.planning_id
475             ) AS plan_open
476           FROM report_account_analytic_planning_line l
477           INNER JOIN report_account_analytic_planning planning ON planning.id=l.planning_id
478           GROUP BY l.account_id, l.planning_id, planning.date_from, planning.date_to, planning.id
479         )
480         """)
481
482 report_account_analytic_planning_account()
483
484 class report_account_analytic_planning_stat(osv.osv):
485     _name = "report_account_analytic.planning.stat"
486     _description = "Planning stat"
487     _rec_name = 'user_id'
488     _auto = False
489     _log_access = False
490     _order = 'planning_id,user_id'
491
492     def _sum_amount_real(self, cr, uid, ids, name, args, context=None):
493         users_obj = self.pool.get('res.users')
494         result = {}
495         tm2 = users_obj.browse(cr, uid, uid, context=context).company_id.planning_time_mode_id
496         if tm2 and tm2.factor:
497             div2 = tm2.factor
498         else:
499             div2 = 1.0
500         for line in self.browse(cr, uid, ids, context=context):
501             if line.user_id:
502                 cr.execute('''SELECT sum(acc.unit_amount/uom.factor) FROM account_analytic_line acc
503                 LEFT JOIN product_uom uom ON (uom.id = acc.product_uom_id)
504 WHERE user_id=%s and account_id=%s and date>=%s and date<=%s''', (line.user_id.id, line.account_id and line.account_id.id or None, line.planning_id.date_from, line.planning_id.date_to))
505             else:
506                 cr.execute('SELECT sum(unit_amount) FROM account_analytic_line WHERE account_id=%s AND date>=%s AND date<=%s', (line.account_id and line.account_id.id or None, line.planning_id.date_from, line.planning_id.date_to))
507
508         sum = cr.fetchone()
509         if sum and sum[0]:
510             result[line.id] = sum[0] * div2
511         return result
512
513     def _sum_amount_tasks(self, cr, uid, ids, name, args, context=None):
514         users_obj = self.pool.get('res.users')
515         result = {}
516         tm = users_obj.browse(cr, uid, uid, context=context).company_id.project_time_mode_id
517         if tm and tm.factor:
518             div = tm.factor
519         else:
520             div = 1.0
521         tm2 = users_obj.browse(cr, uid, uid, context=context).company_id.planning_time_mode_id
522         if tm2 and tm2.factor:
523             div2 = tm2.factor
524         else:
525             div2 = 1.0
526         for line in self.browse(cr, uid, ids, context=context):
527             where = ''
528             if line.user_id:
529                 where = 'user_id=' + str(line.user_id.id) + ' and '
530             cr.execute('''select
531                     sum(planned_hours)
532                 FROM
533                     project_task
534                 WHERE
535                 ''' + where + '''
536                     project_id IN (select id from project_project where analytic_account_id=%s) AND
537                     date_end>=%s AND
538                     date_end<=%s''', (
539                 line.account_id and line.account_id.id or None,
540                 line.planning_id.date_from,
541                 line.planning_id.date_to)
542             )
543             sum = cr.fetchone()
544             if sum and sum[0]:
545                 result[line.id] = sum[0] /div * div2
546         return result
547
548     _columns = {
549         'planning_id': fields.many2one('report_account_analytic.planning', 'Planning', select=True),
550         'user_id': fields.many2one('res.users', 'User', select=True),
551         'manager_id': fields.many2one('res.users', 'Manager'),
552         'account_id': fields.many2one('account.analytic.account', 'Account'),
553         'sum_amount': fields.float('Planned Days', required=True),
554         'sum_amount_real': fields.function(_sum_amount_real, string='Timesheet'),
555         'sum_amount_tasks': fields.function(_sum_amount_tasks, string='Tasks'),
556     }
557
558     def init(self, cr):
559         cr.execute("""
560             create or replace view report_account_analytic_planning_stat as (
561                 SELECT
562                     min(l.id) as id,
563                     l.user_id as user_id,
564                     a.user_id as manager_id,
565                     l.account_id as account_id,
566                     sum(l.amount/u.factor) as sum_amount,
567                     l.planning_id
568                 FROM
569                     report_account_analytic_planning_line l
570                 LEFT JOIN
571                     report_account_analytic_planning a on (a.id = l.planning_id)
572                 LEFT JOIN
573                     product_uom u on (l.amount_unit = u.id)
574                 GROUP BY
575                     l.planning_id, l.user_id, l.account_id, a.user_id
576             )
577         """)
578
579 report_account_analytic_planning_stat()
580
581 class res_company(osv.osv):
582     _inherit = 'res.company'
583     _columns = {
584         'planning_time_mode_id': fields.many2one('product.uom', 'Planning Time Unit',
585             help='This will set the unit of measure used in plannings.',
586         ),
587     }
588 res_company()
589
590 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: