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