2009-10-13 05:58:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-08-24 14:45:43 +00:00
|
|
|
##############################################################################
|
2009-11-27 11:35:13 +00:00
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
2010-01-12 09:18:39 +00:00
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
2008-08-24 14:45:43 +00:00
|
|
|
#
|
2008-11-03 19:18:56 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 11:15:34 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2008-08-24 14:45:43 +00:00
|
|
|
#
|
2008-11-03 19:18:56 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 11:15:34 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2008-08-24 14:45:43 +00:00
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2009-11-27 11:35:13 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-08-24 14:45:43 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
import time
|
2010-09-29 12:51:08 +00:00
|
|
|
import datetime
|
|
|
|
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp import pooler
|
|
|
|
from openerp.report import report_sxw
|
2008-08-24 14:45:43 +00:00
|
|
|
import operator
|
2012-12-06 15:13:16 +00:00
|
|
|
from openerp import osv
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
|
|
class budget_report(report_sxw.rml_parse):
|
|
|
|
def __init__(self, cr, uid, name, context):
|
2009-10-09 11:49:00 +00:00
|
|
|
super(budget_report, self).__init__(cr, uid, name, context=context)
|
2010-09-29 12:51:08 +00:00
|
|
|
self.localcontext.update({
|
2008-08-24 14:45:43 +00:00
|
|
|
'funct': self.funct,
|
|
|
|
'funct_total': self.funct_total,
|
|
|
|
'time': time,
|
|
|
|
})
|
2010-09-29 12:51:08 +00:00
|
|
|
self.context = context
|
2008-08-24 14:45:43 +00:00
|
|
|
|
2012-03-05 18:40:03 +00:00
|
|
|
def funct(self, object, form, ids=None, done=None, level=1):
|
|
|
|
if ids is None:
|
|
|
|
ids = {}
|
2008-08-24 14:45:43 +00:00
|
|
|
if not ids:
|
|
|
|
ids = self.ids
|
|
|
|
if not done:
|
2010-09-29 12:51:08 +00:00
|
|
|
done = {}
|
2008-08-24 14:45:43 +00:00
|
|
|
global tot
|
2010-09-29 12:51:08 +00:00
|
|
|
tot = {
|
2008-08-24 14:45:43 +00:00
|
|
|
'theo':0.00,
|
|
|
|
'pln':0.00,
|
|
|
|
'prac':0.00,
|
|
|
|
'perc':0.00
|
|
|
|
}
|
2010-09-29 12:51:08 +00:00
|
|
|
result = []
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
|
|
budgets = self.pool.get('crossovered.budget').browse(self.cr, self.uid, [object.id], self.context.copy())
|
2010-09-29 12:51:08 +00:00
|
|
|
c_b_lines_obj = self.pool.get('crossovered.budget.lines')
|
|
|
|
acc_analytic_obj = self.pool.get('account.analytic.account')
|
2008-08-24 14:45:43 +00:00
|
|
|
for budget_id in budgets:
|
2010-09-29 12:51:08 +00:00
|
|
|
res = {}
|
|
|
|
budget_lines = []
|
|
|
|
budget_ids = []
|
|
|
|
d_from = form['date_from']
|
|
|
|
d_to = form['date_to']
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
|
|
for line in budget_id.crossovered_budget_line:
|
|
|
|
budget_ids.append(line.id)
|
|
|
|
|
2008-09-29 07:37:13 +00:00
|
|
|
if not budget_ids:
|
2008-08-24 14:45:43 +00:00
|
|
|
return []
|
|
|
|
|
2010-09-29 12:51:08 +00:00
|
|
|
self.cr.execute('SELECT DISTINCT(analytic_account_id) FROM crossovered_budget_lines WHERE id = ANY(%s)',(budget_ids,))
|
|
|
|
an_ids = self.cr.fetchall()
|
2008-08-24 14:45:43 +00:00
|
|
|
|
2010-09-29 12:51:08 +00:00
|
|
|
context = {'wizard_date_from': d_from, 'wizard_date_to': d_to}
|
|
|
|
for i in range(0, len(an_ids)):
|
2011-04-15 06:09:42 +00:00
|
|
|
if not an_ids[i][0]:
|
|
|
|
continue
|
2010-09-29 12:51:08 +00:00
|
|
|
analytic_name = acc_analytic_obj.browse(self.cr, self.uid, [an_ids[i][0]])
|
2008-08-24 14:45:43 +00:00
|
|
|
res={
|
2010-09-29 12:51:08 +00:00
|
|
|
'b_id': '-1',
|
|
|
|
'a_id': '-1',
|
|
|
|
'name': analytic_name[0].name,
|
|
|
|
'status': 1,
|
|
|
|
'theo': 0.00,
|
|
|
|
'pln': 0.00,
|
|
|
|
'prac': 0.00,
|
|
|
|
'perc': 0.00
|
2008-08-24 14:45:43 +00:00
|
|
|
}
|
|
|
|
result.append(res)
|
|
|
|
|
2010-09-29 12:51:08 +00:00
|
|
|
line_ids = c_b_lines_obj.search(self.cr, self.uid, [('id', 'in', budget_ids), ('analytic_account_id','=',an_ids[i][0])])
|
2010-06-16 11:51:39 +00:00
|
|
|
line_id = c_b_lines_obj.browse(self.cr, self.uid, line_ids)
|
2010-09-29 12:51:08 +00:00
|
|
|
tot_theo = tot_pln = tot_prac = tot_perc = 0.00
|
2008-08-24 14:45:43 +00:00
|
|
|
|
2010-09-29 12:51:08 +00:00
|
|
|
done_budget = []
|
2008-08-24 14:45:43 +00:00
|
|
|
for line in line_id:
|
2008-09-29 07:37:13 +00:00
|
|
|
if line.id in budget_ids:
|
2010-09-29 12:51:08 +00:00
|
|
|
theo = pract = 0.00
|
|
|
|
theo = c_b_lines_obj._theo_amt(self.cr, self.uid, [line.id], context)[line.id]
|
|
|
|
pract = c_b_lines_obj._prac_amt(self.cr, self.uid, [line.id], context)[line.id]
|
2008-08-24 14:45:43 +00:00
|
|
|
if line.general_budget_id.id in done_budget:
|
|
|
|
for record in result:
|
2010-09-29 12:51:08 +00:00
|
|
|
if record['b_id'] == line.general_budget_id.id and record['a_id'] == line.analytic_account_id.id:
|
|
|
|
record['theo'] += theo
|
|
|
|
record['pln'] += line.planned_amount
|
|
|
|
record['prac'] += pract
|
|
|
|
if record['theo'] <> 0.00:
|
|
|
|
perc = (record['prac'] / record['theo']) * 100
|
2008-08-24 14:45:43 +00:00
|
|
|
else:
|
2010-09-29 12:51:08 +00:00
|
|
|
perc = 0.00
|
|
|
|
record['perc'] = perc
|
2008-08-24 14:45:43 +00:00
|
|
|
tot_theo += theo
|
2010-09-29 12:51:08 +00:00
|
|
|
tot_pln += line.planned_amount
|
|
|
|
tot_prac += pract
|
|
|
|
tot_perc += perc
|
2008-08-24 14:45:43 +00:00
|
|
|
else:
|
2010-09-29 12:51:08 +00:00
|
|
|
if theo <> 0.00:
|
|
|
|
perc = (pract / theo) * 100
|
2008-08-24 14:45:43 +00:00
|
|
|
else:
|
2010-09-29 12:51:08 +00:00
|
|
|
perc = 0.00
|
|
|
|
res1 = {
|
|
|
|
'a_id': line.analytic_account_id.id,
|
|
|
|
'b_id': line.general_budget_id.id,
|
|
|
|
'name': line.general_budget_id.name,
|
|
|
|
'status': 2,
|
|
|
|
'theo': theo,
|
|
|
|
'pln': line.planned_amount,
|
|
|
|
'prac': pract,
|
|
|
|
'perc': perc,
|
2008-08-24 14:45:43 +00:00
|
|
|
}
|
|
|
|
tot_theo += theo
|
2010-09-29 12:51:08 +00:00
|
|
|
tot_pln += line.planned_amount
|
|
|
|
tot_prac += pract
|
|
|
|
tot_perc += perc
|
|
|
|
if form['report'] == 'analytic-full':
|
2008-08-24 14:45:43 +00:00
|
|
|
result.append(res1)
|
|
|
|
done_budget.append(line.general_budget_id.id)
|
|
|
|
else:
|
|
|
|
|
|
|
|
if line.general_budget_id.id in done_budget:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
res1={
|
2010-09-29 12:51:08 +00:00
|
|
|
'a_id': line.analytic_account_id.id,
|
|
|
|
'b_id': line.general_budget_id.id,
|
|
|
|
'name': line.general_budget_id.name,
|
|
|
|
'status': 2,
|
|
|
|
'theo': 0.00,
|
|
|
|
'pln': 0.00,
|
|
|
|
'prac': 0.00,
|
|
|
|
'perc': 0.00
|
|
|
|
}
|
|
|
|
if form['report'] == 'analytic-full':
|
2008-08-24 14:45:43 +00:00
|
|
|
result.append(res1)
|
|
|
|
done_budget.append(line.general_budget_id.id)
|
2010-09-29 12:51:08 +00:00
|
|
|
if tot_theo == 0.00:
|
|
|
|
tot_perc = 0.00
|
2008-08-24 14:45:43 +00:00
|
|
|
else:
|
2010-09-29 12:51:08 +00:00
|
|
|
tot_perc = float(tot_prac / tot_theo) * 100
|
|
|
|
if form['report'] == 'analytic-full':
|
|
|
|
result[-(len(done_budget) +1)]['theo'] = tot_theo
|
|
|
|
tot['theo'] += tot_theo
|
|
|
|
result[-(len(done_budget) +1)]['pln'] = tot_pln
|
|
|
|
tot['pln'] += tot_pln
|
|
|
|
result[-(len(done_budget) +1)]['prac'] = tot_prac
|
|
|
|
tot['prac'] += tot_prac
|
|
|
|
result[-(len(done_budget) +1)]['perc'] = tot_perc
|
2008-08-24 14:45:43 +00:00
|
|
|
else:
|
2010-09-29 12:51:08 +00:00
|
|
|
result[-1]['theo'] = tot_theo
|
|
|
|
tot['theo'] += tot_theo
|
|
|
|
result[-1]['pln'] = tot_pln
|
|
|
|
tot['pln'] += tot_pln
|
|
|
|
result[-1]['prac'] = tot_prac
|
|
|
|
tot['prac'] += tot_prac
|
|
|
|
result[-1]['perc'] = tot_perc
|
|
|
|
if tot['theo'] == 0.00:
|
|
|
|
tot['perc'] = 0.00
|
2008-08-24 14:45:43 +00:00
|
|
|
else:
|
2010-09-29 12:51:08 +00:00
|
|
|
tot['perc'] = float(tot['prac'] / tot['theo']) * 100
|
2008-08-24 14:45:43 +00:00
|
|
|
return result
|
|
|
|
|
2010-06-16 11:51:39 +00:00
|
|
|
def funct_total(self, form):
|
2010-09-29 12:51:08 +00:00
|
|
|
result = []
|
|
|
|
res = {}
|
|
|
|
res = {
|
|
|
|
'tot_theo': tot['theo'],
|
|
|
|
'tot_pln': tot['pln'],
|
|
|
|
'tot_prac': tot['prac'],
|
|
|
|
'tot_perc': tot['perc']
|
2008-08-24 14:45:43 +00:00
|
|
|
}
|
|
|
|
result.append(res)
|
|
|
|
return result
|
2010-09-29 12:51:08 +00:00
|
|
|
|
2010-08-26 07:06:42 +00:00
|
|
|
report_sxw.report_sxw('report.crossovered.budget.report', 'crossovered.budget', 'addons/account_budget/report/crossovered_budget_report.rml',parser=budget_report,header='internal')
|
2010-09-29 12:51:08 +00:00
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|