[MERGE] from trunk
[odoo/odoo.git] / addons / account_budget / report / budget_report.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import time
23 from openerp.report import report_sxw
24
25 tot = {}
26
27 class budget_report(report_sxw.rml_parse):
28     def __init__(self, cr, uid, name, context):
29         super(budget_report, self).__init__(cr, uid, name, context=context)
30         self.localcontext.update({
31             'funct': self.funct,
32             'funct_total': self.funct_total,
33             'time': time,
34         })
35         self.context = context
36
37     def funct(self, object, form, ids=None, done=None, level=1):
38         if ids is None:
39             ids = {}
40         if not ids:
41             ids = self.ids
42         if not done:
43             done = {}
44         global tot
45         tot = {
46             'theo':0.00,
47             'pln':0.00,
48             'prac':0.00,
49             'perc':0.00
50         }
51         result = []
52
53         budgets = self.pool.get('account.budget.post').browse(self.cr, self.uid, [object.id], self.context.copy())
54         c_b_lines_obj = self.pool.get('crossovered.budget.lines')
55         acc_analytic_obj = self.pool.get('account.analytic.account')
56         for budget_id in budgets:
57             res = {}
58             budget_ids = []
59             d_from = form['date_from']
60             d_to = form['date_to']
61
62             for line in budget_id.crossovered_budget_line:
63                 budget_ids.append(line.id)
64
65             if not budget_ids:
66                 return []
67             self.cr.execute('SELECT DISTINCT(analytic_account_id) FROM crossovered_budget_lines WHERE id = ANY(%s)',(budget_ids,))
68             an_ids = self.cr.fetchall()
69
70             context = {'wizard_date_from': d_from, 'wizard_date_to': d_to}
71             for i in range(0, len(an_ids)):
72                 if not an_ids[i][0]:
73                     continue
74                 analytic_name = acc_analytic_obj.browse(self.cr, self.uid, [an_ids[i][0]])
75                 res={
76                     'b_id': '-1',
77                     'a_id': '-1',
78                     'name': analytic_name[0].name,
79                     'status': 1,
80                     'theo': 0.00,
81                     'pln': 0.00,
82                     'prac': 0.00,
83                     'perc': 0.00
84                 }
85                 result.append(res)
86
87                 line_ids = c_b_lines_obj.search(self.cr, self.uid, [('id', 'in', budget_ids), ('analytic_account_id','=',an_ids[i][0])])
88                 line_id = c_b_lines_obj.browse(self.cr, self.uid, line_ids)
89                 tot_theo = tot_pln = tot_prac = tot_perc = 0.00
90
91                 done_budget = []
92                 for line in line_id:
93                     if line.id in budget_ids:
94                         theo = pract = 0.00
95                         theo = c_b_lines_obj._theo_amt(self.cr, self.uid, [line.id], context)[line.id]
96                         pract = c_b_lines_obj._prac_amt(self.cr, self.uid, [line.id], context)[line.id]
97                         if line.general_budget_id.id in done_budget:
98                             for record in result:
99                                 if record['b_id'] == line.general_budget_id.id  and record['a_id'] == line.analytic_account_id.id:
100                                     record['theo'] += theo
101                                     record['pln'] += line.planned_amount
102                                     record['prac'] += pract
103                                     if record['theo'] <> 0.00:
104                                         perc = (record['prac'] / record['theo']) * 100
105                                     else:
106                                         perc = 0.00
107                                     record['perc'] = perc
108                                     tot_theo += theo
109                                     tot_pln += line.planned_amount
110                                     tot_prac += pract
111                                     tot_perc += perc
112                         else:
113                             if theo <> 0.00:
114                                 perc = (pract / theo) * 100
115                             else:
116                                 perc = 0.00
117                             res1 = {
118                                     'a_id': line.analytic_account_id.id,
119                                     'b_id': line.general_budget_id.id,
120                                     'name': line.general_budget_id.name,
121                                     'status': 2,
122                                     'theo': theo,
123                                     'pln': line.planned_amount,
124                                     'prac': pract,
125                                     'perc': perc,
126                             }
127                             tot_theo += theo
128                             tot_pln += line.planned_amount
129                             tot_prac += pract
130                             tot_perc += perc
131                             if form['report'] == 'analytic-full':
132                                 result.append(res1)
133                                 done_budget.append(line.general_budget_id.id)
134                     else:
135
136                         if line.general_budget_id.id in done_budget:
137                             continue
138                         else:
139                             res1={
140                                     'a_id': line.analytic_account_id.id,
141                                     'b_id': line.general_budget_id.id,
142                                     'name': line.general_budget_id.name,
143                                     'status': 2,
144                                     'theo': 0.00,
145                                     'pln': 0.00,
146                                     'prac': 0.00,
147                                     'perc': 0.00
148                             }
149                             if form['report'] == 'analytic-full':
150                                 result.append(res1)
151                                 done_budget.append(line.general_budget_id.id)
152                 if tot_theo == 0.00:
153                     tot_perc = 0.00
154                 else:
155                     tot_perc = float(tot_prac / tot_theo) * 100
156                 if form['report'] == 'analytic-full':
157                     result[-(len(done_budget) +1)]['theo'] = tot_theo
158                     tot['theo'] += tot_theo
159                     result[-(len(done_budget) +1)]['pln'] = tot_pln
160                     tot['pln'] += tot_pln
161                     result[-(len(done_budget) +1)]['prac'] = tot_prac
162                     tot['prac'] += tot_prac
163                     result[-(len(done_budget) +1)]['perc'] = tot_perc
164                 else:
165                     result[-1]['theo'] = tot_theo
166                     tot['theo'] += tot_theo
167                     result[-1]['pln'] = tot_pln
168                     tot['pln'] += tot_pln
169                     result[-1]['prac'] = tot_prac
170                     tot['prac'] += tot_prac
171                     result[-1]['perc'] = tot_perc
172             if tot['theo'] == 0.00:
173                 tot['perc'] = 0.00
174             else:
175                 tot['perc'] = float(tot['prac'] / tot['theo']) * 100
176         return result
177
178     def funct_total(self, form):
179         result = []
180         res = {}
181         res = {
182              'tot_theo': tot['theo'],
183              'tot_pln': tot['pln'],
184              'tot_prac': tot['prac'],
185              'tot_perc': tot['perc']
186         }
187         result.append(res)
188         return result
189
190 report_sxw.report_sxw('report.account.budget', 'account.budget.post', 'addons/account_budget/report/budget_report.rml', parser=budget_report, header='internal')
191
192 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: