[IMP]:report_analytic_planning module sql queries to parameterized query

bzr revid: nch@tinyerp.com-20091124100529-q5t3i69can2moe3v
This commit is contained in:
nch@tinyerp.com
2009-11-24 15:35:29 +05:30
parent a90ddbe855
commit 046e2aff3e
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# $Id$
#
@@ -16,7 +16,7 @@
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
@@ -47,7 +47,7 @@ class one2many_mod3(fields.one2many):
class report_account_analytic_planning(osv.osv):
_name = "report_account_analytic.planning"
_description = "Planning"
def _get_total_planned(self, cr, uid, ids, name, args, context):
result = {}
for plan in self.browse(cr, uid, ids, context):
@@ -58,7 +58,7 @@ class report_account_analytic_planning(osv.osv):
plan_hrs = plan_hrs + p.plan_open + p.plan_tasks
result[plan.id] = plan_hrs
return result
def _get_total_free(self, cr, uid, ids, name, args, context):
result = {}
for plan in self.browse(cr, uid, ids, context):
@@ -68,15 +68,15 @@ class report_account_analytic_planning(osv.osv):
total_free = total_free + p.free
result[plan.id] = total_free
return result
def _check_planning_responsible(self, cr, uid, ids, context={}):
for obj_plan in self.browse(cr,uid,ids):
cr.execute("""
SELECT id FROM report_account_analytic_planning plan
SELECT id FROM report_account_analytic_planning plan
WHERE ( (%(date_from)s BETWEEN date_from AND date_to)
OR (%(date_to)s BETWEEN date_from AND date_to)
OR (%(date_from)s < date_from AND %(date_to)s > date_to)
) AND user_id = %(uid)s AND id <> %(id)s""",
) AND user_id = %(uid)s AND id <> %(id)s""",
{"date_from": obj_plan.date_from,
"date_to": obj_plan.date_to,
"uid": obj_plan.user_id.id,
@@ -87,31 +87,31 @@ class report_account_analytic_planning(osv.osv):
if res:
return False
return True
_columns = {
'name': fields.char('Planning Name', required=True, size=32, states={'done':[('readonly', True)]}),
'code': fields.char('Code', size=32, states={'done':[('readonly', True)]}),
'user_id': fields.many2one('res.users', 'Responsible', required=True, states={'done':[('readonly', True)]}),
'date_from':fields.date('Start Date', required=True, states={'done':[('readonly', True)]}),
'date_to':fields.date('End Date', required=True, states={'done':[('readonly', True)]}),
'line_ids': fields.one2many('report_account_analytic.planning.line', 'planning_id', 'Planning lines', states={'done':[('readonly', True)]}),
'stat_ids': fields.one2many('report_account_analytic.planning.stat', 'planning_id', 'Planning analysis', readonly=True),
'state': fields.selection([('draft','Draft'),('open', 'Open'), ('done', 'Done'),('cancel','Cancelled')], 'Status', required=True),
'name': fields.char('Planning Name', required=True, size=32, states={'done':[('readonly', True)]}),
'code': fields.char('Code', size=32, states={'done':[('readonly', True)]}),
'user_id': fields.many2one('res.users', 'Responsible', required=True, states={'done':[('readonly', True)]}),
'date_from':fields.date('Start Date', required=True, states={'done':[('readonly', True)]}),
'date_to':fields.date('End Date', required=True, states={'done':[('readonly', True)]}),
'line_ids': fields.one2many('report_account_analytic.planning.line', 'planning_id', 'Planning lines', states={'done':[('readonly', True)]}),
'stat_ids': fields.one2many('report_account_analytic.planning.stat', 'planning_id', 'Planning analysis', readonly=True),
'state': fields.selection([('draft','Draft'),('open', 'Open'), ('done', 'Done'),('cancel','Cancelled')], 'Status', required=True),
'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'),
'planning_user_ids': one2many_mod3('report_account_analytic.planning.user', 'planning_id', 'Planning By User'),
'planning_account': fields.one2many('report_account_analytic.planning.account', 'planning_id', 'Planning By Account'),
'total_planned' : fields.function(_get_total_planned, method=True, string='Total Planned'),
'planning_account': fields.one2many('report_account_analytic.planning.account', 'planning_id', 'Planning By Account'),
'total_planned' : fields.function(_get_total_planned, method=True, string='Total Planned'),
'total_free' : fields.function(_get_total_free, method=True, string='Total Free'),
}
_defaults = {
'date_from': lambda *a: time.strftime('%Y-%m-01'),
'date_to': lambda *a: (mx.DateTime.now()+mx.DateTime.RelativeDateTime(months=1, day=1, days=-1)).strftime('%Y-%m-%d'),
'user_id': lambda self, cr, uid, c: uid,
'state': lambda *args: 'draft',
'business_days' : lambda *a: 20,
'date_from': lambda *a: time.strftime('%Y-%m-01'),
'date_to': lambda *a: (mx.DateTime.now()+mx.DateTime.RelativeDateTime(months=1, day=1, days=-1)).strftime('%Y-%m-%d'),
'user_id': lambda self, cr, uid, c: uid,
'state': lambda *args: 'draft',
'business_days' : lambda *a: 20,
}
_order = 'date_from desc'
_constraints = [
(_check_planning_responsible, 'Invalid planning ! Planning dates can\'t overlap for the same responsible. ', ['user_id'])
]
@@ -123,15 +123,15 @@ class report_account_analytic_planning(osv.osv):
def action_cancel(self, cr, uid, id, context={}):
self.write(cr, uid, id, {'state' : 'cancel'})
return True
def action_draft(self, cr, uid, id, context={}):
self.write(cr, uid, id, {'state' : 'draft'})
return True
def action_done(self, cr, uid, id, context={}):
self.write(cr, uid, id, {'state' : 'done'})
return True
report_account_analytic_planning()
class report_account_analytic_planning_line(osv.osv):
@@ -167,17 +167,17 @@ class report_account_analytic_planning_line(osv.osv):
return result
_columns = {
'account_id':fields.many2one('account.analytic.account', 'Analytic account', required=True),
'planning_id': fields.many2one('report_account_analytic.planning', 'Planning', required=True, ondelete='cascade'),
'user_id': fields.many2one('res.users', 'User'),
'amount': fields.float('Quantity', required=True),
'amount_unit': fields.many2one('product.uom', 'Qty UoM', required=True),
'note': fields.text('Note', size=64),
'account_id':fields.many2one('account.analytic.account', 'Analytic account', required=True),
'planning_id': fields.many2one('report_account_analytic.planning', 'Planning', required=True, ondelete='cascade'),
'user_id': fields.many2one('res.users', 'User'),
'amount': fields.float('Quantity', required=True),
'amount_unit': fields.many2one('product.uom', 'Qty UoM', required=True),
'note': fields.text('Note', size=64),
'amount_in_base_uom': fields.function(_amount_base_uom, method=True, string='Quantity in base uom', store=True),
'task_ids': fields.one2many('project.task', 'planning_line_id', 'Planning Tasks'),
'task_ids': fields.one2many('project.task', 'planning_line_id', 'Planning Tasks'),
}
_order = 'user_id,account_id'
report_account_analytic_planning_line()
class account_analytic_account(osv.osv):
@@ -187,26 +187,26 @@ class account_analytic_account(osv.osv):
_columns = {
'planning_ids': fields.one2many('report_account_analytic.planning.line', 'account_id', 'Plannings'),
}
account_analytic_account()
class project_task(osv.osv):
_name = "project.task"
_inherit = "project.task"
_columns = {
'planning_line_id': fields.many2one('report_account_analytic.planning.line', 'Planning', ondelete='cascade'),
'planning_line_id': fields.many2one('report_account_analytic.planning.line', 'Planning', ondelete='cascade'),
}
def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
if not context.get('planning'):
return super(project_task,self).search(cr, user, args, offset, limit, order, context)
cr.execute(" SELECT t.id, t.name \
from project_task t \
join report_account_analytic_planning_line l on (l.id = t.planning_line_id )\
where l.planning_id=%s" % context.get('planning'))
where l.planning_id=%s",(context.get('planning'),))
ids = map(lambda x: x[0], cr.fetchall())
return ids
project_task()
@@ -215,8 +215,8 @@ class report_account_analytic_planning_user(osv.osv):
_description = "Planning by User"
_rec_name = 'user_id'
_auto = False
def _get_tasks(self, cr, uid, ids, name, args, context):
result = {}
tm = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.project_time_mode_id
@@ -234,7 +234,7 @@ class report_account_analytic_planning_user(osv.osv):
cr.execute("""select COALESCE(sum(tasks.remaining_hours),0) from project_task tasks \
where tasks.planning_line_id in (select id from report_account_analytic_planning_line\
where planning_id = %s and user_id=%s)""", (line.planning_id.id, line.user_id.id,))
result[line.id] = cr.fetchall()[0][0] / div * div2
else:
result[line.id] = 0
@@ -244,11 +244,11 @@ class report_account_analytic_planning_user(osv.osv):
result = {}
for line in self.browse(cr, uid, ids, context):
if line.user_id:
result[line.id] = line.planning_id.business_days - line.plan_tasks - line.plan_open - line.holiday
result[line.id] = line.planning_id.business_days - line.plan_tasks - line.plan_open - line.holiday
else:
result[line.id] = 0.0
return result
def _get_timesheets(self, cr, uid, ids, name, args, context):
result = {}
tm2 = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.planning_time_mode_id
@@ -259,7 +259,7 @@ class report_account_analytic_planning_user(osv.osv):
for line in self.browse(cr, uid, ids, context):
if line.user_id:
cr.execute("""
SELECT sum(unit_amount/uom.factor) FROM account_analytic_line acc
SELECT sum(unit_amount/uom.factor) FROM account_analytic_line acc
LEFT JOIN product_uom uom ON (uom.id = acc.product_uom_id)
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 ))
res = cr.fetchall()
@@ -269,54 +269,54 @@ class report_account_analytic_planning_user(osv.osv):
return result
_columns = {
'planning_id': fields.many2one('report_account_analytic.planning', 'Planning'),
'user_id': fields.many2one('res.users', 'User', readonly=True),
'tasks' : fields.function(_get_tasks, method=True, 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.'),
'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.'),
'planning_id': fields.many2one('report_account_analytic.planning', 'Planning'),
'user_id': fields.many2one('res.users', 'User', readonly=True),
'tasks' : fields.function(_get_tasks, method=True, 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.'),
'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.'),
'free' : fields.function(_get_free, method=True, string='Unallocated Time', readonly=True,help='Computed as \
Business Days - (Time Allocation of Tasks + Time Allocation without Tasks + Holiday Leaves)'),
'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.'),
'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.'),
'timesheet': fields.function(_get_timesheets, method=True, 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.'),
Business Days - (Time Allocation of Tasks + Time Allocation without Tasks + Holiday Leaves)'),
'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.'),
'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.'),
'timesheet': fields.function(_get_timesheets, method=True, 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.'),
}
def init(self, cr):
cr.execute(""" CREATE OR REPLACE VIEW report_account_analytic_planning_user AS (
SELECT
SELECT
planning.id AS planning_id,
planning.id::varchar(32) || '-' || COALESCE(users.id,0)::varchar(32) AS id,
planning.business_days,
users.id AS user_id,
(SELECT sum(line1.amount_in_base_uom)
(SELECT sum(line1.amount_in_base_uom)
FROM report_account_analytic_planning_line line1
WHERE (
SELECT COUNT(1)
FROM project_task task
SELECT COUNT(1)
FROM project_task task
WHERE task.planning_line_id = line1.id
) > 0
AND line1.user_id = users.id
) > 0
AND line1.user_id = users.id
AND line1.planning_id = planning.id
)AS plan_tasks,
(SELECT SUM(line1.amount_in_base_uom)
(SELECT SUM(line1.amount_in_base_uom)
FROM report_account_analytic_planning_line line1
WHERE (
SELECT COUNT(1)
FROM project_task task
SELECT COUNT(1)
FROM project_task task
WHERE task.planning_line_id = line1.id
) = 0
AND line1.user_id = users.id
) = 0
AND line1.user_id = users.id
AND line1.planning_id = planning.id
) AS plan_open,
(SELECT -(SUM(holidays.number_of_days))
FROM hr_holidays holidays
WHERE holidays.employee_id IN
FROM hr_holidays holidays
WHERE holidays.employee_id IN
(
SELECT emp.id
FROM hr_employee emp WHERE emp.user_id = users.id
SELECT emp.id
FROM hr_employee emp WHERE emp.user_id = users.id
)
AND holidays.state IN ('validate')
AND holidays.type = 'remove'
AND holidays.date_from >= planning.date_from
AND holidays.type = 'remove'
AND holidays.date_from >= planning.date_from
AND holidays.date_to <= planning.date_to
) AS holiday
@@ -331,32 +331,32 @@ Business Days - (Time Allocation of Tasks + Time Allocation without Tasks + Holi
planning.id::varchar(32) || '-' || '0' AS id,
planning.business_days,
line.user_id,
(SELECT SUM(line1.amount_in_base_uom)
(SELECT SUM(line1.amount_in_base_uom)
FROM report_account_analytic_planning_line line1
WHERE (SELECT COUNT(1) FROM project_task task WHERE task.planning_line_id = line1.id) > 0
WHERE (SELECT COUNT(1) FROM project_task task WHERE task.planning_line_id = line1.id) > 0
AND line1.user_id IS NULL
) AS plan_tasks,
(SELECT SUM(line1.amount_in_base_uom)
(SELECT SUM(line1.amount_in_base_uom)
FROM report_account_analytic_planning_line line1
WHERE (SELECT COUNT(1) FROM project_task task WHERE task.planning_line_id = line1.id) = 0
WHERE (SELECT COUNT(1) FROM project_task task WHERE task.planning_line_id = line1.id) = 0
AND line1.user_id IS NULL
) AS plan_open,
'0' AS holiday
FROM report_account_analytic_planning planning
INNER JOIN report_account_analytic_planning_line line ON line.planning_id = planning.id
FROM report_account_analytic_planning planning
INNER JOIN report_account_analytic_planning_line line ON line.planning_id = planning.id
AND line.user_id IS NULL
GROUP BY planning.id, planning.business_days, line.user_id, planning.date_from, planning.date_to
)
""")
report_account_analytic_planning_user()
class report_account_analytic_planning_account(osv.osv):
_name = "report_account_analytic.planning.account"
_description = "Planning by Account"
_rec_name = 'account_id'
_auto = False
def _get_tasks(self, cr, uid, ids, name, args, context):
result = {}
tm = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.project_time_mode_id
@@ -371,16 +371,16 @@ class report_account_analytic_planning_account(osv.osv):
div2 = 1.0
for line in self.browse(cr, uid, ids, context):
cr.execute("""
SELECT COALESCE(sum(tasks.remaining_hours),0)
FROM project_task tasks
SELECT COALESCE(sum(tasks.remaining_hours),0)
FROM project_task tasks
WHERE tasks.planning_line_id IN (
SELECT id
SELECT id
FROM report_account_analytic_planning_line
WHERE planning_id = %s AND account_id=%s
)""", (line.planning_id.id,line.account_id.id ))
result[line.id] = cr.fetchall()[0][0] / div *div2
return result
def _get_timesheets(self, cr, uid, ids, name, args, context):
result = {}
tm2 = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.planning_time_mode_id
@@ -390,7 +390,7 @@ class report_account_analytic_planning_account(osv.osv):
div2 = 1.0
for line in self.browse(cr, uid, ids, context):
cr.execute("""
SELECT SUM(unit_amount/uom.factor) FROM account_analytic_line acc
SELECT SUM(unit_amount/uom.factor) FROM account_analytic_line acc
LEFT JOIN product_uom uom ON (uom.id = acc.product_uom_id)
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.id, ))
res = cr.fetchall()[0][0]
@@ -401,38 +401,38 @@ class report_account_analytic_planning_account(osv.osv):
return result
_columns = {
'planning_id': fields.many2one('report_account_analytic.planning', 'Planning'),
'account_id': fields.many2one('account.analytic.account', 'Analytic account', readonly=True),
'tasks' : fields.function(_get_tasks, method=True, 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.'),
'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.'),
'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.'),
'timesheet': fields.function(_get_timesheets, method=True, 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.'),
'planning_id': fields.many2one('report_account_analytic.planning', 'Planning'),
'account_id': fields.many2one('account.analytic.account', 'Analytic account', readonly=True),
'tasks' : fields.function(_get_tasks, method=True, 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.'),
'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.'),
'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.'),
'timesheet': fields.function(_get_timesheets, method=True, 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.'),
}
def init(self, cr):
cr.execute(""" CREATE OR REPLACE VIEW report_account_analytic_planning_account AS (
SELECT
SELECT
MIN(l.id) AS id,
l.account_id AS account_id,
SUM(l.amount) AS quantity,
l.planning_id AS planning_id,
( SELECT SUM(line1.amount_in_base_uom)
( SELECT SUM(line1.amount_in_base_uom)
FROM report_account_analytic_planning_line line1
WHERE
( SELECT COUNT(1)
FROM project_task task
WHERE
( SELECT COUNT(1)
FROM project_task task
WHERE task.planning_line_id = line1.id
) > 0
) > 0
AND l.account_id = line1.account_id
AND l.planning_id = line1.planning_id
) AS plan_tasks,
( SELECT SUM(line1.amount_in_base_uom)
( SELECT SUM(line1.amount_in_base_uom)
FROM report_account_analytic_planning_line line1
WHERE
( SELECT COUNT(1)
FROM project_task task
WHERE
( SELECT COUNT(1)
FROM project_task task
WHERE task.planning_line_id = line1.id
) = 0
) = 0
AND l.account_id = line1.account_id
AND planning.id = line1.planning_id
) AS plan_open
@@ -441,8 +441,8 @@ class report_account_analytic_planning_account(osv.osv):
GROUP BY l.account_id, l.planning_id, planning.date_from, planning.date_to, planning.id
)
""")
report_account_analytic_planning_account()
class report_account_analytic_planning_stat(osv.osv):
@@ -452,7 +452,7 @@ class report_account_analytic_planning_stat(osv.osv):
_auto = False
_log_access = False
_order = 'planning_id,user_id'
def _sum_amount_real(self, cr, uid, ids, name, args, context):
result = {}
tm2 = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.planning_time_mode_id
@@ -469,7 +469,7 @@ where user_id=%s and account_id=%s and date>=%s and date<=%s''', (line.user_id.i
cr.execute('select sum(unit_amount) from account_analytic_line where account_id=%s and date>=%s and date<=%s', (line.account_id.id, line.planning_id.date_from, line.planning_id.date_to))
result[line.id] = cr.fetchone()[0] * div2
return result
def _sum_amount_tasks(self, cr, uid, ids, name, args, context):
result = {}
tm = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.project_time_mode_id
@@ -495,23 +495,23 @@ where user_id=%s and account_id=%s and date>=%s and date<=%s''', (line.user_id.i
project_id in (select id from project_project where category_id=%s) and
date_close>=%s and
date_close<=%s''', (
line.account_id.id,
line.planning_id.date_from,
line.account_id.id,
line.planning_id.date_from,
line.planning_id.date_to)
)
result[line.id] = cr.fetchone()[0] /div * div2
return result
_columns = {
'planning_id': fields.many2one('report_account_analytic.planning', 'Planning'),
'user_id': fields.many2one('res.users', 'User'),
'manager_id': fields.many2one('res.users', 'Manager'),
'account_id': fields.many2one('account.analytic.account', 'Account', required=True),
'sum_amount': fields.float('Planned Days', required=True),
'sum_amount_real': fields.function(_sum_amount_real, method=True, string='Timesheet'),
'sum_amount_tasks': fields.function(_sum_amount_tasks, method=True, string='Tasks'),
'planning_id': fields.many2one('report_account_analytic.planning', 'Planning'),
'user_id': fields.many2one('res.users', 'User'),
'manager_id': fields.many2one('res.users', 'Manager'),
'account_id': fields.many2one('account.analytic.account', 'Account', required=True),
'sum_amount': fields.float('Planned Days', required=True),
'sum_amount_real': fields.function(_sum_amount_real, method=True, string='Timesheet'),
'sum_amount_tasks': fields.function(_sum_amount_tasks, method=True, string='Tasks'),
}
def init(self, cr):
cr.execute("""
create or replace view report_account_analytic_planning_stat as (
@@ -539,7 +539,7 @@ class res_company(osv.osv):
_columns = {
'planning_time_mode_id': fields.many2one('product.uom', 'Planning Time Unit',
help='This will set the unit of measure used in plannings.',
),
}
res_company()