1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
25 from openerp import pooler
26 from openerp.report import report_sxw
28 from openerp import osv
30 class budget_report(report_sxw.rml_parse):
31 def __init__(self, cr, uid, name, context):
32 super(budget_report, self).__init__(cr, uid, name, context=context)
33 self.localcontext.update({
35 'funct_total': self.funct_total,
38 self.context = context
40 def funct(self, object, form, ids=None, done=None, level=1):
56 budgets = self.pool.get('crossovered.budget').browse(self.cr, self.uid, [object.id], self.context.copy())
57 c_b_lines_obj = self.pool.get('crossovered.budget.lines')
58 acc_analytic_obj = self.pool.get('account.analytic.account')
59 for budget_id in budgets:
63 d_from = form['date_from']
64 d_to = form['date_to']
66 for line in budget_id.crossovered_budget_line:
67 budget_ids.append(line.id)
72 self.cr.execute('SELECT DISTINCT(analytic_account_id) FROM crossovered_budget_lines WHERE id = ANY(%s)',(budget_ids,))
73 an_ids = self.cr.fetchall()
75 context = {'wizard_date_from': d_from, 'wizard_date_to': d_to}
76 for i in range(0, len(an_ids)):
79 analytic_name = acc_analytic_obj.browse(self.cr, self.uid, [an_ids[i][0]])
83 'name': analytic_name[0].name,
92 line_ids = c_b_lines_obj.search(self.cr, self.uid, [('id', 'in', budget_ids), ('analytic_account_id','=',an_ids[i][0])])
93 line_id = c_b_lines_obj.browse(self.cr, self.uid, line_ids)
94 tot_theo = tot_pln = tot_prac = tot_perc = 0.00
98 if line.id in budget_ids:
100 theo = c_b_lines_obj._theo_amt(self.cr, self.uid, [line.id], context)[line.id]
101 pract = c_b_lines_obj._prac_amt(self.cr, self.uid, [line.id], context)[line.id]
102 if line.general_budget_id.id in done_budget:
103 for record in result:
104 if record['b_id'] == line.general_budget_id.id and record['a_id'] == line.analytic_account_id.id:
105 record['theo'] += theo
106 record['pln'] += line.planned_amount
107 record['prac'] += pract
108 if record['theo'] <> 0.00:
109 perc = (record['prac'] / record['theo']) * 100
112 record['perc'] = perc
114 tot_pln += line.planned_amount
119 perc = (pract / theo) * 100
123 'a_id': line.analytic_account_id.id,
124 'b_id': line.general_budget_id.id,
125 'name': line.general_budget_id.name,
128 'pln': line.planned_amount,
133 tot_pln += line.planned_amount
136 if form['report'] == 'analytic-full':
138 done_budget.append(line.general_budget_id.id)
141 if line.general_budget_id.id in done_budget:
145 'a_id': line.analytic_account_id.id,
146 'b_id': line.general_budget_id.id,
147 'name': line.general_budget_id.name,
154 if form['report'] == 'analytic-full':
156 done_budget.append(line.general_budget_id.id)
160 tot_perc = float(tot_prac / tot_theo) * 100
161 if form['report'] == 'analytic-full':
162 result[-(len(done_budget) +1)]['theo'] = tot_theo
163 tot['theo'] += tot_theo
164 result[-(len(done_budget) +1)]['pln'] = tot_pln
165 tot['pln'] += tot_pln
166 result[-(len(done_budget) +1)]['prac'] = tot_prac
167 tot['prac'] += tot_prac
168 result[-(len(done_budget) +1)]['perc'] = tot_perc
170 result[-1]['theo'] = tot_theo
171 tot['theo'] += tot_theo
172 result[-1]['pln'] = tot_pln
173 tot['pln'] += tot_pln
174 result[-1]['prac'] = tot_prac
175 tot['prac'] += tot_prac
176 result[-1]['perc'] = tot_perc
177 if tot['theo'] == 0.00:
180 tot['perc'] = float(tot['prac'] / tot['theo']) * 100
183 def funct_total(self, form):
187 'tot_theo': tot['theo'],
188 'tot_pln': tot['pln'],
189 'tot_prac': tot['prac'],
190 'tot_perc': tot['perc']
195 report_sxw.report_sxw('report.crossovered.budget.report', 'crossovered.budget', 'addons/account_budget/report/crossovered_budget_report.rml',parser=budget_report,header='internal')
197 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: