[MERGE] from trunk
[odoo/odoo.git] / addons / account_budget / report / analytic_account_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
27 class analytic_account_budget_report(report_sxw.rml_parse):
28     def __init__(self, cr, uid, name, context):
29         super(analytic_account_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
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         accounts = self.pool.get('account.analytic.account').browse(self.cr, self.uid, [object.id], self.context.copy())
54         c_b_lines_obj = self.pool.get('crossovered.budget.lines')
55         obj_c_budget = self.pool.get('crossovered.budget')
56
57         for account_id in accounts:
58             res = {}
59             b_line_ids = []
60             for line in account_id.crossovered_budget_line:
61                 b_line_ids.append(line.id)
62             if not b_line_ids:
63                 return []
64             d_from = form['date_from']
65             d_to = form['date_to']
66
67             self.cr.execute('SELECT DISTINCT(crossovered_budget_id) FROM crossovered_budget_lines WHERE id =ANY(%s)',(b_line_ids,))
68             budget_ids = self.cr.fetchall()
69
70             context = {'wizard_date_from':d_from,'wizard_date_to':d_to}
71             for i in range(0, len(budget_ids)):
72                 budget_name = obj_c_budget.browse(self.cr, self.uid, [budget_ids[i][0]])
73                 res= {
74                      'b_id':'-1',
75                      'a_id':'-1',
76                      'name':budget_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', b_line_ids), ('crossovered_budget_id','=',budget_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
88
89                 done_budget = []
90                 for line in line_id:
91                     if line.id in b_line_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                                     record['perc'] += line.percentage
102                                     tot_theo += theo
103                                     tot_pln += line.planned_amount
104                                     tot_prac += pract
105                                     tot_perc += line.percentage
106                         else:
107                             res1 = {
108                                  'b_id': line.general_budget_id.id,
109                                  'a_id': line.analytic_account_id.id,
110                                  'name': line.general_budget_id.name,
111                                  'status': 2,
112                                  'theo': theo,
113                                  'pln': line.planned_amount,
114                                  'prac': pract,
115                                  'perc': line.percentage
116                             }
117                             tot_theo += theo
118                             tot_pln += line.planned_amount
119                             tot_prac += pract
120                             tot_perc += line.percentage
121                             result.append(res1)
122                             done_budget.append(line.general_budget_id.id)
123                     else:
124                        if line.general_budget_id.id in done_budget:
125                             continue
126                        else:
127                             res1={
128                                     'b_id': line.general_budget_id.id,
129                                     'a_id': line.analytic_account_id.id,
130                                      'name': line.general_budget_id.name,
131                                      'status': 2,
132                                      'theo': 0.00,
133                                      'pln': 0.00,
134                                      'prac': 0.00,
135                                      'perc': 0.00
136                             }
137                             result.append(res1)
138                             done_budget.append(line.general_budget_id.id)
139                 if tot_theo == 0.00:
140                     tot_perc = 0.00
141                 else:
142                     tot_perc = float(tot_prac / tot_theo) * 100
143
144                 result[-(len(done_budget) +1)]['theo'] = tot_theo
145                 tot['theo'] +=tot_theo
146                 result[-(len(done_budget) +1)]['pln'] = tot_pln
147                 tot['pln'] +=tot_pln
148                 result[-(len(done_budget) +1)]['prac'] = tot_prac
149                 tot['prac'] +=tot_prac
150                 result[-(len(done_budget) +1)]['perc'] = tot_perc
151             if tot['theo'] == 0.00:
152                 tot['perc'] = 0.00
153             else:
154                 tot['perc'] = float(tot['prac'] / tot['theo']) * 100
155         return result
156
157     def funct_total(self, form):
158         result = []
159         res = {}
160         res = {
161              'tot_theo': tot['theo'],
162              'tot_pln': tot['pln'],
163              'tot_prac': tot['prac'],
164              'tot_perc': tot['perc']
165         }
166         result.append(res)
167         return result
168
169 report_sxw.report_sxw('report.account.analytic.account.budget', 'account.analytic.account', 'addons/account_budget/report/analytic_account_budget_report.rml',parser=analytic_account_budget_report,header='internal')
170
171 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: