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