+ def _analysis_all(self, cr, uid, ids, fields, arg, context=None):
+ dp = 2
+ res = dict([(i, {}) for i in ids])
+ parent_ids = tuple(ids) #We don't want consolidation for each of these fields because those complex computation is resource-greedy.
+ 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] = False
+ 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():
+ if account_id not in res:
+ res[account_id] = {}
+ res[account_id][f] = sum
+ 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():
+ if account_id not in res:
+ res[account_id] = {}
+ res[account_id][f] = round(sum, dp)
+
+ # 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] = False
+ 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, lid in cr.fetchall():
+ res[account_id][f] = lid
+ elif f == 'last_worked_date':
+ for id in ids:
+ res[id][f] = False
+ 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, lwd in cr.fetchall():
+ if account_id not in res:
+ res[account_id] = {}
+ res[account_id][f] = lwd
+ 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, sua in cr.fetchall():
+ if account_id not in res:
+ res[account_id] = {}
+ res[account_id][f] = round(sua, dp)
+ 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, hq in ff:
+ if account_id not in res:
+ res[account_id] = {}
+ res[account_id][f] = round(hq, dp)
+ 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
+ # 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():
+ res[account_id][f] = round(sum, dp)