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