Merge branch 'master' of https://github.com/odoo/odoo
[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 from openerp.osv import osv
24 from openerp.report import report_sxw
25
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('crossovered.budget').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_lines = []
59             budget_ids = []
60             d_from = form['date_from']
61             d_to = form['date_to']
62
63             for line in budget_id.crossovered_budget_line:
64                 budget_ids.append(line.id)
65
66             if not budget_ids:
67                 return []
68
69             self.cr.execute('SELECT DISTINCT(analytic_account_id) FROM crossovered_budget_lines WHERE id = ANY(%s)',(budget_ids,))
70             an_ids = self.cr.fetchall()
71
72             context = {'wizard_date_from': d_from, 'wizard_date_to': d_to}
73             for i in range(0, len(an_ids)):
74                 if not an_ids[i][0]:
75                     continue
76                 analytic_name = acc_analytic_obj.browse(self.cr, self.uid, [an_ids[i][0]])
77                 res={
78                     'b_id': '-1',
79                     'a_id': '-1',
80                     'name': analytic_name[0].name,
81                     'status': 1,
82                     'theo': 0.00,
83                     'pln': 0.00,
84                     'prac': 0.00,
85                     'perc': 0.00
86                 }
87                 result.append(res)
88
89                 line_ids = c_b_lines_obj.search(self.cr, self.uid, [('id', 'in', budget_ids), ('analytic_account_id','=',an_ids[i][0]), ('date_to', '>=', d_from), ('date_from', '<=', d_to)])
90                 line_id = c_b_lines_obj.browse(self.cr, self.uid, line_ids)
91                 tot_theo = tot_pln = tot_prac = tot_perc = 0.00
92
93                 done_budget = []
94                 for line in line_id:
95                     if line.id in budget_ids:
96                         theo = pract = 0.00
97                         theo = c_b_lines_obj._theo_amt(self.cr, self.uid, [line.id], context)[line.id]
98                         pract = c_b_lines_obj._prac_amt(self.cr, self.uid, [line.id], context)[line.id]
99                         if line.general_budget_id.id in done_budget:
100                             for record in result:
101                                 if record['b_id'] == line.general_budget_id.id  and record['a_id'] == line.analytic_account_id.id:
102                                     record['theo'] += theo
103                                     record['pln'] += line.planned_amount
104                                     record['prac'] += pract
105                                     if record['theo'] <> 0.00:
106                                         perc = (record['prac'] / record['theo']) * 100
107                                     else:
108                                         perc = 0.00
109                                     record['perc'] = perc
110                                     tot_theo += theo
111                                     tot_pln += line.planned_amount
112                                     tot_prac += pract
113                                     tot_perc += perc
114                         else:
115                             if theo <> 0.00:
116                                 perc = (pract / theo) * 100
117                             else:
118                                 perc = 0.00
119                             res1 = {
120                                     'a_id': line.analytic_account_id.id,
121                                     'b_id': line.general_budget_id.id,
122                                     'name': line.general_budget_id.name,
123                                     'status': 2,
124                                     'theo': theo,
125                                     'pln': line.planned_amount,
126                                     'prac': pract,
127                                     'perc': perc,
128                             }
129                             tot_theo += theo
130                             tot_pln += line.planned_amount
131                             tot_prac += pract
132                             tot_perc += perc
133                             if form['report'] == 'analytic-full':
134                                 result.append(res1)
135                                 done_budget.append(line.general_budget_id.id)
136                     else:
137
138                         if line.general_budget_id.id in done_budget:
139                             continue
140                         else:
141                             res1={
142                                     'a_id': line.analytic_account_id.id,
143                                     'b_id': line.general_budget_id.id,
144                                     'name': line.general_budget_id.name,
145                                     'status': 2,
146                                     'theo': 0.00,
147                                     'pln': 0.00,
148                                     'prac': 0.00,
149                                     'perc': 0.00
150                             }
151                             if form['report'] == 'analytic-full':
152                                 result.append(res1)
153                                 done_budget.append(line.general_budget_id.id)
154                 if tot_theo == 0.00:
155                     tot_perc = 0.00
156                 else:
157                     tot_perc = float(tot_prac / tot_theo) * 100
158                 if form['report'] == 'analytic-full':
159                     result[-(len(done_budget) +1)]['theo'] = tot_theo
160                     tot['theo'] += tot_theo
161                     result[-(len(done_budget) +1)]['pln'] = tot_pln
162                     tot['pln'] += tot_pln
163                     result[-(len(done_budget) +1)]['prac'] = tot_prac
164                     tot['prac'] += tot_prac
165                     result[-(len(done_budget) +1)]['perc'] = tot_perc
166                 else:
167                     result[-1]['theo'] = tot_theo
168                     tot['theo'] += tot_theo
169                     result[-1]['pln'] = tot_pln
170                     tot['pln'] += tot_pln
171                     result[-1]['prac'] = tot_prac
172                     tot['prac'] += tot_prac
173                     result[-1]['perc'] = tot_perc
174             if tot['theo'] == 0.00:
175                 tot['perc'] = 0.00
176             else:
177                 tot['perc'] = float(tot['prac'] / tot['theo']) * 100
178         return result
179
180     def funct_total(self, form):
181         result = []
182         res = {}
183         res = {
184              'tot_theo': tot['theo'],
185              'tot_pln': tot['pln'],
186              'tot_prac': tot['prac'],
187              'tot_perc': tot['perc']
188         }
189         result.append(res)
190         return result
191
192
193 class report_crossoveredbudget(osv.AbstractModel):
194     _name = 'report.account_budget.report_crossoveredbudget'
195     _inherit = 'report.abstract_report'
196     _template = 'account_budget.report_crossoveredbudget'
197     _wrapped_report_class = budget_report
198
199 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: