generate css from last commit
[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 import pooler
26 from report import report_sxw
27
28 class analytic_account_budget_report(report_sxw.rml_parse):
29     def __init__(self, cr, uid, name, context):
30         super(analytic_account_budget_report, self).__init__(cr, uid, name, context=context)
31         self.localcontext.update( {
32             'funct': self.funct,
33             'funct_total': self.funct_total,
34             'time': time,
35         })
36         self.context = context
37
38     def funct(self, object, form, ids={}, done=None, level=1):
39         if not ids:
40             ids = self.ids
41         if not done:
42             done = {}
43
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         accounts = self.pool.get('account.analytic.account').browse(self.cr, self.uid, [object.id], self.context.copy())
53         c_b_lines_obj = self.pool.get('crossovered.budget.lines')
54         obj_c_budget = self.pool.get('crossovered.budget')
55
56         for account_id in accounts:
57             res = {}
58             b_line_ids = []
59             for line in account_id.crossovered_budget_line:
60                 b_line_ids.append(line.id)
61             if not b_line_ids:
62                 return []
63             d_from = form['date_from']
64             d_to = form['date_to']
65
66             self.cr.execute('SELECT DISTINCT(crossovered_budget_id) FROM crossovered_budget_lines WHERE id =ANY(%s)',(b_line_ids,))
67             budget_ids = self.cr.fetchall()
68
69             context = {'wizard_date_from':d_from,'wizard_date_to':d_to}
70             for i in range(0, len(budget_ids)):
71                 budget_name = obj_c_budget.browse(self.cr, self.uid, [budget_ids[i][0]])
72                 res= {
73                      'b_id':'-1',
74                      'a_id':'-1',
75                      'name':budget_name[0].name,
76                      'status':1,
77                      'theo':0.00,
78                      'pln':0.00,
79                      'prac':0.00,
80                      'perc':0.00
81                 }
82                 result.append(res)
83
84                 line_ids = c_b_lines_obj.search(self.cr, self.uid, [('id', 'in', b_line_ids), ('crossovered_budget_id','=',budget_ids[i][0])])
85                 line_id = c_b_lines_obj.browse(self.cr, self.uid, line_ids)
86                 tot_theo = tot_pln = tot_prac = tot_perc = 0
87
88                 done_budget = []
89                 for line in line_id:
90                     if line.id in b_line_ids:
91                         theo = pract = 0.00
92                         theo = c_b_lines_obj._theo_amt(self.cr, self.uid, [line.id], context)[line.id]
93                         pract = c_b_lines_obj._prac_amt(self.cr, self.uid, [line.id], context)[line.id]
94                         if line.general_budget_id.id in done_budget:
95                             for record in result:
96                                if record['b_id'] == line.general_budget_id.id  and record['a_id'] == line.analytic_account_id.id:
97                                     record['theo'] += theo
98                                     record['pln'] += line.planned_amount
99                                     record['prac'] += pract
100                                     record['perc'] += line.percentage
101                                     tot_theo += theo
102                                     tot_pln += line.planned_amount
103                                     tot_prac += pract
104                                     tot_perc += line.percentage
105                         else:
106                             res1 = {
107                                  'b_id': line.general_budget_id.id,
108                                  'a_id': line.analytic_account_id.id,
109                                  'name': line.general_budget_id.name,
110                                  'status': 2,
111                                  'theo': theo,
112                                  'pln': line.planned_amount,
113                                  'prac': pract,
114                                  'perc': line.percentage
115                             }
116                             tot_theo += theo
117                             tot_pln += line.planned_amount
118                             tot_prac += pract
119                             tot_perc += line.percentage
120                             result.append(res1)
121                             done_budget.append(line.general_budget_id.id)
122                     else:
123                        if line.general_budget_id.id in done_budget:
124                             continue
125                        else:
126                             res1={
127                                     'b_id': line.general_budget_id.id,
128                                     'a_id': line.analytic_account_id.id,
129                                      'name': line.general_budget_id.name,
130                                      'status': 2,
131                                      'theo': 0.00,
132                                      'pln': 0.00,
133                                      'prac': 0.00,
134                                      'perc': 0.00
135                             }
136                             result.append(res1)
137                             done_budget.append(line.general_budget_id.id)
138                 if tot_theo == 0.00:
139                     tot_perc = 0.00
140                 else:
141                     tot_perc = float(tot_prac / tot_theo) * 100
142
143                 result[-(len(done_budget) +1)]['theo'] = tot_theo
144                 tot['theo'] +=tot_theo
145                 result[-(len(done_budget) +1)]['pln'] = tot_pln
146                 tot['pln'] +=tot_pln
147                 result[-(len(done_budget) +1)]['prac'] = tot_prac
148                 tot['prac'] +=tot_prac
149                 result[-(len(done_budget) +1)]['perc'] = tot_perc
150             if tot['theo'] == 0.00:
151                 tot['perc'] = 0.00
152             else:
153                 tot['perc'] = float(tot['prac'] / tot['theo']) * 100
154         return result
155
156     def funct_total(self,form):
157         result = []
158         res = {}
159         res = {
160              'tot_theo': tot['theo'],
161              'tot_pln': tot['pln'],
162              'tot_prac': tot['prac'],
163              'tot_perc': tot['perc']
164         }
165         result.append(res)
166         return result
167
168 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')
169
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: