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