[IMP] account_analytic_analysis: performance improvement: reduced the number of call to sql queries

bzr revid: olt@tinyerp.com-20100923141455-mpa2kjzsrxk4ni1k
This commit is contained in:
olt@tinyerp.com 2010-09-23 16:14:55 +02:00
parent debb9fac86
commit b1e8536b11
1 changed files with 213 additions and 241 deletions

View File

@ -28,6 +28,210 @@ class account_analytic_account(osv.osv):
_name = "account.analytic.account"
_inherit = "account.analytic.account"
def _analysis_all(self, cr, uid, ids, fields, arg, context=None):
dp = 2
res = dict([(i, {}) for i in ids])
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
accounts = self.browse(cr, uid, ids, context=context)
for f in fields:
if f == 'user_ids':
cr.execute('SELECT MAX(id) FROM res_users')
max_user = cr.fetchone()[0]
if parent_ids:
cr.execute('SELECT DISTINCT("user") FROM account_analytic_analysis_summary_user ' \
'WHERE account_id IN %s AND unit_amount <> 0.0', (parent_ids,))
result = cr.fetchall()
else:
result = []
for id in ids:
res[id][f] = [int((id * max_user) + x[0]) for x in result]
elif f == 'month_ids':
if parent_ids:
cr.execute('SELECT DISTINCT(month_id) FROM account_analytic_analysis_summary_month ' \
'WHERE account_id IN %s AND unit_amount <> 0.0', (parent_ids,))
result = cr.fetchall()
else:
result = []
for id in ids:
res[id][f] = [int(id * 1000000 + int(x[0])) for x in result]
elif f == 'last_worked_invoiced_date':
for id in ids:
res[id][f] = 0.0
if parent_ids:
cr.execute("select account_analytic_line.account_id, max(date) \
from account_analytic_line \
where account_id IN %s \
and invoice_id is not null \
GROUP BY account_analytic_line.account_id;", (parent_ids,))
for account_id, sum in cr.fetchall():
res[account_id][f] = sum
for account in accounts:
for child in account.child_ids:
if res[account.id].get(f, '') < res.get(child.id, {}).get(f, ''):
res[account.id][f] = res.get(child.id, {}).get(f, '')
elif f == 'ca_to_invoice':
for id in ids:
res[id][f] = 0.0
res2 = {}
if parent_ids:
# Amount uninvoiced hours to invoice at sale price
# Warning
# This computation doesn't take care of pricelist !
# Just consider list_price
cr.execute("""SELECT account_analytic_account.id, \
COALESCE(sum (product_template.list_price * \
account_analytic_line.unit_amount * \
((100-hr_timesheet_invoice_factor.factor)/100)),0.0) \
AS ca_to_invoice \
FROM product_template \
join product_product \
on product_template.id = product_product.product_tmpl_id \
JOIN account_analytic_line \
on account_analytic_line.product_id = product_product.id \
JOIN account_analytic_journal \
on account_analytic_line.journal_id = account_analytic_journal.id \
JOIN account_analytic_account \
on account_analytic_account.id = account_analytic_line.account_id \
JOIN hr_timesheet_invoice_factor \
on hr_timesheet_invoice_factor.id = account_analytic_account.to_invoice \
WHERE account_analytic_account.id IN %s \
AND account_analytic_line.invoice_id is null \
AND account_analytic_line.to_invoice IS NOT NULL \
and account_analytic_journal.type in ('purchase','general') \
GROUP BY account_analytic_account.id;""", (parent_ids,))
for account_id, sum in cr.fetchall():
res[account_id][f] = round(sum, dp)
for account in accounts:
#res.setdefault(account.id, 0.0)
res2.setdefault(account.id, 0.0)
for child in account.child_ids:
if child.id != account.id:
res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
res2[account.id] += res2.get(child.id, 0.0)
# sum both result on account_id
for id in ids:
res[id][f] = round(res.get(id, {}).get(f, 0.0), dp) + round(res2.get(id, 0.0), 2)
elif f == 'last_invoice_date':
for id in ids:
res[id][f] = ''
if parent_ids:
cr.execute ("select account_analytic_line.account_id, \
date(max(account_invoice.date_invoice)) \
from account_analytic_line \
join account_invoice \
on account_analytic_line.invoice_id = account_invoice.id \
where account_analytic_line.account_id IN %s \
and account_analytic_line.invoice_id is not null \
GROUP BY account_analytic_line.account_id",(parent_ids,))
for account_id, sum in cr.fetchall():
res[account_id] = sum
for account in accounts:
for child in account.child_ids:
if res[account.id][f] < res.get(child.id, {}).get(f, ''):
res[account.id][f] = res.get(child.id, {}).get(f, '')
elif f == 'last_worked_date':
for id in ids:
res[id][f] = ''
if parent_ids:
cr.execute("select account_analytic_line.account_id, max(date) \
from account_analytic_line \
where account_id IN %s \
and invoice_id is null \
GROUP BY account_analytic_line.account_id" ,(parent_ids,))
for account_id, sum in cr.fetchall():
res[account_id][f] = sum
for account in accounts:
for child in account.child_ids:
if res[account.id][f] < res.get(child.id, {}).get(f, ''):
res[account.id][f] = res.get(child.id, {}).get(f, '')
elif f == 'hours_qtt_non_invoiced':
for id in ids:
res[id][f] = 0.0
if parent_ids:
cr.execute("select account_analytic_line.account_id, COALESCE(sum(unit_amount),0.0) \
from account_analytic_line \
join account_analytic_journal \
on account_analytic_line.journal_id = account_analytic_journal.id \
where account_analytic_line.account_id IN %s \
and account_analytic_journal.type='general' \
and invoice_id is null \
AND to_invoice IS NOT NULL \
GROUP BY account_analytic_line.account_id;",(parent_ids,))
for account_id, sum in cr.fetchall():
res[account_id][f] = round(sum, dp)
for account in accounts:
for child in account.child_ids:
if account.id != child.id:
res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
for id in ids:
res[id][f] = round(res[id][f], dp)
elif f == 'hours_quantity':
for id in ids:
res[id][f] = 0.0
if parent_ids:
cr.execute("select account_analytic_line.account_id,COALESCE(SUM(unit_amount),0.0) \
from account_analytic_line \
join account_analytic_journal \
on account_analytic_line.journal_id = account_analytic_journal.id \
where account_analytic_line.account_id IN %s \
and account_analytic_journal.type='general' \
GROUP BY account_analytic_line.account_id",(parent_ids,))
ff = cr.fetchall()
for account_id, sum in ff:
res[account_id][f] = round(sum, dp)
for account in accounts:
for child in account.child_ids:
if account.id != child.id:
res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
for id in ids:
res[id][f] = round(res[id][f], dp)
elif f == 'ca_theorical':
# TODO Take care of pricelist and purchase !
for id in ids:
res[id][f] = 0.0
res2 = {}
# Warning
# This computation doesn't take care of pricelist !
# Just consider list_price
if parent_ids:
cr.execute("""select account_analytic_line.account_id as account_id, \
COALESCE(sum((account_analytic_line.unit_amount * pt.list_price) \
- (account_analytic_line.unit_amount * pt.list_price \
* hr.factor)),0.0) as somme
from account_analytic_line \
left join account_analytic_journal \
on (account_analytic_line.journal_id = account_analytic_journal.id) \
join product_product pp \
on (account_analytic_line.product_id = pp.id) \
join product_template pt \
on (pp.product_tmpl_id = pt.id) \
join account_analytic_account a \
on (a.id=account_analytic_line.account_id) \
join hr_timesheet_invoice_factor hr \
on (hr.id=a.to_invoice) \
where account_analytic_line.account_id IN %s \
and a.to_invoice IS NOT NULL \
and account_analytic_journal.type IN ('purchase','general')
GROUP BY account_analytic_line.account_id""",(parent_ids,))
for account_id, sum in cr.fetchall():
res2[account_id] = round(sum, dp)
for account in accounts:
res2.setdefault(account.id, 0.0)
for child in account.child_ids:
if account.id != child.id:
res[account.id][f] += res.get(child.id, {}).get(f, 0.0)
res[account.id][f] += res2.get(child.id, 0.0)
# sum both result on account_id
for id in ids:
res[id][f] = round(res[id][f], dp) + round(res2.get(id, 0.0), dp)
return res
def _ca_invoiced_calc(self, cr, uid, ids, name, arg, context=None):
res = {}
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
@ -44,101 +248,6 @@ class account_analytic_account(osv.osv):
return self._compute_currency_for_level_tree(cr, uid, ids, parent_ids, res, context)
def _ca_to_invoice_calc(self, cr, uid, ids, name, arg, context=None):
res = {}
res2 = {}
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
if parent_ids:
# Amount uninvoiced hours to invoice at sale price
# Warning
# This computation doesn't take care of pricelist !
# Just consider list_price
cr.execute("""SELECT account_analytic_account.id, \
COALESCE(sum (product_template.list_price * \
account_analytic_line.unit_amount * \
((100-hr_timesheet_invoice_factor.factor)/100)),0.0) \
AS ca_to_invoice \
FROM product_template \
join product_product \
on product_template.id = product_product.product_tmpl_id \
JOIN account_analytic_line \
on account_analytic_line.product_id = product_product.id \
JOIN account_analytic_journal \
on account_analytic_line.journal_id = account_analytic_journal.id \
JOIN account_analytic_account \
on account_analytic_account.id = account_analytic_line.account_id \
JOIN hr_timesheet_invoice_factor \
on hr_timesheet_invoice_factor.id = account_analytic_account.to_invoice \
WHERE account_analytic_account.id IN %s \
AND account_analytic_line.invoice_id is null \
AND account_analytic_line.to_invoice IS NOT NULL \
and account_analytic_journal.type in ('purchase','general') \
GROUP BY account_analytic_account.id;""",(parent_ids,))
for account_id, sum in cr.fetchall():
res[account_id] = round(sum,2)
for obj_id in ids:
res.setdefault(obj_id, 0.0)
res2.setdefault(obj_id, 0.0)
for child_id in self.search(cr, uid,
[('parent_id', 'child_of', [obj_id])]):
if child_id != obj_id:
res[obj_id] += res.get(child_id, 0.0)
res2[obj_id] += res2.get(child_id, 0.0)
# sum both result on account_id
for id in ids:
res[id] = round(res.get(id, 0.0),2) + round(res2.get(id, 0.0),2)
return res
def _hours_qtt_non_invoiced_calc (self, cr, uid, ids, name, arg, context=None):
res = {}
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
if parent_ids:
cr.execute("select account_analytic_line.account_id, COALESCE(sum(unit_amount),0.0) \
from account_analytic_line \
join account_analytic_journal \
on account_analytic_line.journal_id = account_analytic_journal.id \
where account_analytic_line.account_id IN %s \
and account_analytic_journal.type='general' \
and invoice_id is null \
AND to_invoice IS NOT NULL \
GROUP BY account_analytic_line.account_id;",(parent_ids,))
for account_id, sum in cr.fetchall():
res[account_id] = round(sum,2)
for obj_id in ids:
res.setdefault(obj_id, 0.0)
for child_id in self.search(cr, uid,
[('parent_id', 'child_of', [obj_id])]):
if child_id != obj_id:
res[obj_id] += res.get(child_id, 0.0)
for id in ids:
res[id] = round(res.get(id, 0.0),2)
return res
def _hours_quantity_calc(self, cr, uid, ids, name, arg, context=None):
res = {}
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
if parent_ids:
cr.execute("select account_analytic_line.account_id,COALESCE(SUM(unit_amount),0.0) \
from account_analytic_line \
join account_analytic_journal \
on account_analytic_line.journal_id = account_analytic_journal.id \
where account_analytic_line.account_id IN %s \
and account_analytic_journal.type='general' \
GROUP BY account_analytic_line.account_id",(parent_ids,))
ff = cr.fetchall()
for account_id, sum in ff:
res[account_id] = round(sum,2)
for obj_id in ids:
res.setdefault(obj_id, 0.0)
for child_id in self.search(cr, uid,
[('parent_id', 'child_of', [obj_id])]):
if child_id != obj_id:
res[obj_id] += res.get(child_id, 0.0)
for id in ids:
res[id] = round(res.get(id, 0.0),2)
return res
def _total_cost_calc(self, cr, uid, ids, name, arg, context=None):
res = {}
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
@ -155,117 +264,6 @@ class account_analytic_account(osv.osv):
res[account_id] = round(sum,2)
return self._compute_currency_for_level_tree(cr, uid, ids, parent_ids, res, context)
# TODO Take care of pricelist and purchase !
def _ca_theorical_calc(self, cr, uid, ids, name, arg, context=None):
res = {}
res2 = {}
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
# Warning
# This computation doesn't take care of pricelist !
# Just consider list_price
if parent_ids:
cr.execute("""select account_analytic_line.account_id as account_id, \
COALESCE(sum((account_analytic_line.unit_amount * pt.list_price) \
- (account_analytic_line.unit_amount * pt.list_price \
* hr.factor)),0.0) as somme
from account_analytic_line \
left join account_analytic_journal \
on (account_analytic_line.journal_id = account_analytic_journal.id) \
join product_product pp \
on (account_analytic_line.product_id = pp.id) \
join product_template pt \
on (pp.product_tmpl_id = pt.id) \
join account_analytic_account a \
on (a.id=account_analytic_line.account_id) \
join hr_timesheet_invoice_factor hr \
on (hr.id=a.to_invoice) \
where account_analytic_line.account_id IN %s \
and a.to_invoice IS NOT NULL \
and account_analytic_journal.type IN ('purchase','general')
GROUP BY account_analytic_line.account_id""",(parent_ids,))
for account_id, sum in cr.fetchall():
res2[account_id] = round(sum,2)
for obj_id in ids:
res.setdefault(obj_id, 0.0)
res2.setdefault(obj_id, 0.0)
for child_id in self.search(cr, uid,
[('parent_id', 'child_of', [obj_id])]):
if child_id != obj_id:
res[obj_id] += res.get(child_id, 0.0)
res[obj_id] += res2.get(child_id, 0.0)
# sum both result on account_id
for id in ids:
res[id] = round(res.get(id, 0.0),2) + round(res2.get(id, 0.0),2)
return res
def _last_worked_date_calc (self, cr, uid, ids, name, arg, context=None):
res = {}
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
if parent_ids:
cr.execute("select account_analytic_line.account_id, max(date) \
from account_analytic_line \
where account_id IN %s \
and invoice_id is null \
GROUP BY account_analytic_line.account_id" ,(parent_ids,))
for account_id, sum in cr.fetchall():
res[account_id] = sum
for obj_id in ids:
res.setdefault(obj_id, '')
for child_id in self.search(cr, uid,
[('parent_id', 'child_of', [obj_id])]):
if res[obj_id] < res.get(child_id, ''):
res[obj_id] = res.get(child_id, '')
for id in ids:
res[id] = res.get(id, '')
return res
def _last_invoice_date_calc (self, cr, uid, ids, name, arg, context=None):
res = {}
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
if parent_ids:
cr.execute ("select account_analytic_line.account_id, \
date(max(account_invoice.date_invoice)) \
from account_analytic_line \
join account_invoice \
on account_analytic_line.invoice_id = account_invoice.id \
where account_analytic_line.account_id IN %s \
and account_analytic_line.invoice_id is not null \
GROUP BY account_analytic_line.account_id",(parent_ids,))
for account_id, sum in cr.fetchall():
res[account_id] = sum
for obj_id in ids:
res.setdefault(obj_id, '')
for child_id in self.search(cr, uid,
[('parent_id', 'child_of', [obj_id])]):
if res[obj_id] < res.get(child_id, ''):
res[obj_id] = res.get(child_id, '')
for id in ids:
res[id] = res.get(id, '')
return res
def _last_worked_invoiced_date_calc (self, cr, uid, ids, name, arg, context=None):
res = {}
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
if parent_ids:
cr.execute("select account_analytic_line.account_id, max(date) \
from account_analytic_line \
where account_id IN %s \
and invoice_id is not null \
GROUP BY account_analytic_line.account_id;",(parent_ids,))
for account_id, sum in cr.fetchall():
res[account_id] = sum
for obj_id in ids:
res.setdefault(obj_id, '')
for child_id in self.search(cr, uid,
[('parent_id', 'child_of', [obj_id])]):
if res[obj_id] < res.get(child_id, ''):
res[obj_id] = res.get(child_id, '')
for id in ids:
res[id] = res.get(id, '')
return res
def _remaining_hours_calc(self, cr, uid, ids, name, arg, context=None):
res = {}
for account in self.browse(cr, uid, ids):
@ -338,42 +336,16 @@ class account_analytic_account(osv.osv):
res[id] = round(res.get(id, 0.0),2)
return res
def _month(self, cr, uid, ids, name, arg, context=None):
res = {}
for id in ids:
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
if parent_ids:
cr.execute('SELECT DISTINCT(month_id) FROM account_analytic_analysis_summary_month ' \
'WHERE account_id IN %s AND unit_amount <> 0.0',(parent_ids,))
res[id] = [int(id * 1000000 + int(x[0])) for x in cr.fetchall()]
else:
res[id] = []
return res
def _user(self, cr, uid, ids, name, arg, context=None):
res = {}
cr.execute('SELECT MAX(id) FROM res_users')
max_user = cr.fetchone()[0]
for id in ids:
parent_ids = tuple(self.search(cr, uid, [('parent_id', 'child_of', ids)]))
if parent_ids:
cr.execute('SELECT DISTINCT("user") FROM account_analytic_analysis_summary_user ' \
'WHERE account_id IN %s AND unit_amount <> 0.0',(parent_ids,))
res[id] = [int((id * max_user) + x[0]) for x in cr.fetchall()]
else:
res[id] = []
return res
_columns ={
'ca_invoiced': fields.function(_ca_invoiced_calc, method=True, type='float', string='Invoiced Amount', help="Total customer invoiced amount for this account."),
'total_cost': fields.function(_total_cost_calc, method=True, type='float', string='Total Costs', help="Total of costs for this account. It includes real costs (from invoices) and indirect costs, like time spent on timesheets."),
'ca_to_invoice': fields.function(_ca_to_invoice_calc, method=True, type='float', string='Uninvoiced Amount', help="If invoice from analytic account, the remaining amount you can invoice to the customer based on the total costs."),
'ca_theorical': fields.function(_ca_theorical_calc, method=True, type='float', string='Theorical Revenue', help="Based on the costs you had on the project, what would have been the revenue if all these costs have been invoiced at the normal sale price provided by the pricelist."),
'hours_quantity': fields.function(_hours_quantity_calc, method=True, type='float', string='Hours Tot', help="Number of hours you spent on the analytic account (from timesheet). It computes on all journal of type 'general'."),
'last_invoice_date': fields.function(_last_invoice_date_calc, method=True, type='date', string='Last Invoice Date', help="Date of the last invoice created for this analytic account."),
'last_worked_invoiced_date': fields.function(_last_worked_invoiced_date_calc, method=True, type='date', string='Date of Last Invoiced Cost', help="If invoice from the costs, this is the date of the latest work or cost that have been invoiced."),
'last_worked_date': fields.function(_last_worked_date_calc, method=True, type='date', string='Date of Last Cost/Work', help="Date of the latest work done on this account."),
'hours_qtt_non_invoiced': fields.function(_hours_qtt_non_invoiced_calc, method=True, type='float', string='Uninvoiced Hours', help="Number of hours (from journal of type 'general') that can be invoiced if you invoice based on analytic account."),
'ca_to_invoice': fields.function(_analysis_all, method=True, multi='analytic_analysis', type='float', string='Uninvoiced Amount', help="If invoice from analytic account, the remaining amount you can invoice to the customer based on the total costs."),
'ca_theorical': fields.function(_analysis_all, method=True, multi='analytic_analysis', type='float', string='Theorical Revenue', help="Based on the costs you had on the project, what would have been the revenue if all these costs have been invoiced at the normal sale price provided by the pricelist."),
'hours_quantity': fields.function(_analysis_all, method=True, multi='analytic_analysis', type='float', string='Hours Tot', help="Number of hours you spent on the analytic account (from timesheet). It computes on all journal of type 'general'."),
'last_invoice_date': fields.function(_analysis_all, method=True, multi='analytic_analysis', type='date', string='Last Invoice Date', help="Date of the last invoice created for this analytic account."),
'last_worked_invoiced_date': fields.function(_analysis_all, method=True, multi='analytic_analysis', type='date', string='Date of Last Invoiced Cost', help="If invoice from the costs, this is the date of the latest work or cost that have been invoiced."),
'last_worked_date': fields.function(_analysis_all, method=True, multi='analytic_analysis', type='date', string='Date of Last Cost/Work', help="Date of the latest work done on this account."),
'hours_qtt_non_invoiced': fields.function(_analysis_all, method=True, multi='analytic_analysis', type='float', string='Uninvoiced Hours', help="Number of hours (from journal of type 'general') that can be invoiced if you invoice based on analytic account."),
'hours_qtt_invoiced': fields.function(_hours_qtt_invoiced_calc, method=True, type='float', string='Invoiced Hours', help="Number of hours that can be invoiced plus those that already have been invoiced."),
'remaining_hours': fields.function(_remaining_hours_calc, method=True, type='float', string='Remaining Hours', help="Computed using the formula: Maximum Quantity - Hours Tot."),
'remaining_ca': fields.function(_remaining_ca_calc, method=True, type='float', string='Remaining Revenue', help="Computed using the formula: Max Invoice Price - Invoiced Amount."),
@ -381,8 +353,8 @@ class account_analytic_account(osv.osv):
'real_margin': fields.function(_real_margin_calc, method=True, type='float', string='Real Margin', help="Computed using the formula: Invoiced Amount - Total Costs."),
'theorical_margin': fields.function(_theorical_margin_calc, method=True, type='float', string='Theorical Margin', help="Computed using the formula: Theorial Revenue - Total Costs"),
'real_margin_rate': fields.function(_real_margin_rate_calc, method=True, type='float', string='Real Margin Rate (%)', help="Computes using the formula: (Real Margin / Total Costs) * 100."),
'month_ids': fields.function(_month, method=True, type='many2many', relation='account_analytic_analysis.summary.month', string='Month'),
'user_ids': fields.function(_user, method=True, type="many2many", relation='account_analytic_analysis.summary.user', string='User'),
'month_ids': fields.function(_analysis_all, method=True, multi='analytic_analysis', type='many2many', relation='account_analytic_analysis.summary.month', string='Month'),
'user_ids': fields.function(_analysis_all, method=True, multi='analytic_analysis', type="many2many", relation='account_analytic_analysis.summary.user', string='User'),
}
account_analytic_account()