Launchpad automatic translations update.
[odoo/odoo.git] / addons / account / report / account_tax_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
24 from common_report_header import common_report_header
25 from report import report_sxw
26
27 class tax_report(report_sxw.rml_parse, common_report_header):
28     _name = 'report.account.vat.declaration'
29
30     def set_context(self, objects, data, ids, report_type=None):
31         new_ids = ids
32         res = {}
33         self.period_ids = []
34         period_obj = self.pool.get('account.period')
35         res['periods'] = ''
36         res['fiscalyear'] = data['form'].get('fiscalyear_id', False)
37
38         if data['form'].get('period_from', False) and data['form'].get('period_to', False):
39             self.period_ids = period_obj.build_ctx_periods(self.cr, self.uid, data['form']['period_from'], data['form']['period_to'])
40             periods_l = period_obj.read(self.cr, self.uid, self.period_ids, ['name'])
41             for period in periods_l:
42                 if res['periods'] == '':
43                     res['periods'] = period['name']
44                 else:
45                     res['periods'] += ", "+ period['name']
46         return super(tax_report, self).set_context(objects, data, new_ids, report_type=report_type)
47
48     def __init__(self, cr, uid, name, context=None):
49         super(tax_report, self).__init__(cr, uid, name, context=context)
50         self.localcontext.update({
51             'time': time,
52             'get_codes': self._get_codes,
53             'get_general': self._get_general,
54             'get_currency': self._get_currency,
55             'get_lines': self._get_lines,
56             'get_fiscalyear': self._get_fiscalyear,
57             'get_account': self._get_account,
58             'get_start_period': self.get_start_period,
59             'get_end_period': self.get_end_period,
60             'get_basedon': self._get_basedon,
61         })
62
63     def _get_basedon(self, form):
64         return form['form']['based_on']
65
66     def _get_lines(self, based_on, company_id=False, parent=False, level=0, context=None):
67         period_list = self.period_ids
68         res = self._get_codes(based_on, company_id, parent, level, period_list, context=context)
69         if period_list:
70             res = self._add_codes(based_on, res, period_list, context=context)
71         else:
72             self.cr.execute ("select id from account_fiscalyear")
73             fy = self.cr.fetchall()
74             self.cr.execute ("select id from account_period where fiscalyear_id = %s",(fy[0][0],))
75             periods = self.cr.fetchall()
76             for p in periods:
77                 period_list.append(p[0])
78             res = self._add_codes(based_on, res, period_list, context=context)
79
80         i = 0
81         top_result = []
82         while i < len(res):
83
84             res_dict = { 'code': res[i][1].code,
85                 'name': res[i][1].name,
86                 'debit': 0,
87                 'credit': 0,
88                 'tax_amount': res[i][1].sum_period,
89                 'type': 1,
90                 'level': res[i][0],
91                 'pos': 0
92             }
93
94             top_result.append(res_dict)
95             res_general = self._get_general(res[i][1].id, period_list, company_id, based_on, context=context)
96             ind_general = 0
97             while ind_general < len(res_general):
98                 res_general[ind_general]['type'] = 2
99                 res_general[ind_general]['pos'] = 0
100                 res_general[ind_general]['level'] = res_dict['level']
101                 top_result.append(res_general[ind_general])
102                 ind_general+=1
103             i+=1
104         return top_result
105
106     def _get_general(self, tax_code_id, period_list, company_id, based_on, context=None):
107         res = []
108         obj_account = self.pool.get('account.account')
109         periods_ids = tuple(period_list)
110         if based_on == 'payments':
111             self.cr.execute('SELECT SUM(line.tax_amount) AS tax_amount, \
112                         SUM(line.debit) AS debit, \
113                         SUM(line.credit) AS credit, \
114                         COUNT(*) AS count, \
115                         account.id AS account_id, \
116                         account.name AS name,  \
117                         account.code AS code \
118                     FROM account_move_line AS line, \
119                         account_account AS account, \
120                         account_move AS move \
121                         LEFT JOIN account_invoice invoice ON \
122                             (invoice.move_id = move.id) \
123                     WHERE line.state<>%s \
124                         AND line.tax_code_id = %s  \
125                         AND line.account_id = account.id \
126                         AND account.company_id = %s \
127                         AND move.id = line.move_id \
128                         AND line.period_id IN %s \
129                         AND ((invoice.state = %s) \
130                             OR (invoice.id IS NULL))  \
131                     GROUP BY account.id,account.name,account.code', ('draft', tax_code_id,
132                         company_id, periods_ids, 'paid',))
133
134         else:
135             self.cr.execute('SELECT SUM(line.tax_amount) AS tax_amount, \
136                         SUM(line.debit) AS debit, \
137                         SUM(line.credit) AS credit, \
138                         COUNT(*) AS count, \
139                         account.id AS account_id, \
140                         account.name AS name,  \
141                         account.code AS code \
142                     FROM account_move_line AS line, \
143                         account_account AS account \
144                     WHERE line.state <> %s \
145                         AND line.tax_code_id = %s  \
146                         AND line.account_id = account.id \
147                         AND account.company_id = %s \
148                         AND line.period_id IN %s\
149                         AND account.active \
150                     GROUP BY account.id,account.name,account.code', ('draft', tax_code_id,
151                         company_id, periods_ids,))
152         res = self.cr.dictfetchall()
153
154         i = 0
155         while i<len(res):
156             res[i]['account'] = obj_account.browse(self.cr, self.uid, res[i]['account_id'], context=context)
157             i+=1
158         return res
159
160     def _get_codes(self, based_on, company_id, parent=False, level=0, period_list=[], context=None):
161         obj_tc = self.pool.get('account.tax.code')
162         ids = obj_tc.search(self.cr, self.uid, [('parent_id','=',parent),('company_id','=',company_id)], context=context)
163
164         res = []
165         for code in obj_tc.browse(self.cr, self.uid, ids, {'based_on': based_on}):
166             res.append(('.'*2*level, code))
167
168             res += self._get_codes(based_on, company_id, code.id, level+1, context=context)
169         return res
170
171     def _add_codes(self, based_on, account_list=[], period_list=[], context=None):
172         res = []
173         obj_tc = self.pool.get('account.tax.code')
174         for account in account_list:
175             ids = obj_tc.search(self.cr, self.uid, [('id','=', account[1].id)], context=context)
176             sum_tax_add = 0
177             for period_ind in period_list:
178                 for code in obj_tc.browse(self.cr, self.uid, ids, {'period_id':period_ind,'based_on': based_on}):
179                     sum_tax_add = sum_tax_add + code.sum_period
180
181             code.sum_period = sum_tax_add
182
183             res.append((account[0], code))
184         return res
185
186     def _get_currency(self, form, context=None):
187         return self.pool.get('res.company').browse(self.cr, self.uid, form['company_id'], context=context).currency_id.name
188
189     def sort_result(self, accounts, context=None):
190         # On boucle sur notre rapport
191         result_accounts = []
192         ind=0
193         old_level=0
194         while ind<len(accounts):
195             #
196             account_elem = accounts[ind]
197             #
198
199             #
200             # we will now check if the level is lower than the previous level, in this case we will make a subtotal
201             if (account_elem['level'] < old_level):
202                 bcl_current_level = old_level
203                 bcl_rup_ind = ind - 1
204
205                 while (bcl_current_level >= int(accounts[bcl_rup_ind]['level']) and bcl_rup_ind >= 0 ):
206                     res_tot = { 'code': accounts[bcl_rup_ind]['code'],
207                         'name': '',
208                         'debit': 0,
209                         'credit': 0,
210                         'tax_amount': accounts[bcl_rup_ind]['tax_amount'],
211                         'type': accounts[bcl_rup_ind]['type'],
212                         'level': 0,
213                         'pos': 0
214                     }
215
216                     if res_tot['type'] == 1:
217                         # on change le type pour afficher le total
218                         res_tot['type'] = 2
219                         result_accounts.append(res_tot)
220                     bcl_current_level =  accounts[bcl_rup_ind]['level']
221                     bcl_rup_ind -= 1
222
223             old_level = account_elem['level']
224             result_accounts.append(account_elem)
225             ind+=1
226
227         return result_accounts
228
229 report_sxw.report_sxw('report.account.vat.declaration', 'account.tax.code',
230     'addons/account/report/account_tax_report.rml', parser=tax_report, header="internal")
231
232 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: