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