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