[FIX] module installation through -i module -d db --without-demo option corrected
[odoo/odoo.git] / addons / account / report / tax_report.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import time
24 import pooler
25 import rml_parse
26 import copy
27 from report import report_sxw
28 import pdb
29 import re
30
31 class tax_report(rml_parse.rml_parse):
32         _name = 'report.account.vat.declaration'
33         def __init__(self, cr, uid, name, context):
34                 super(tax_report, self).__init__(cr, uid, name, context)
35                 self.localcontext.update({
36                         'time': time,
37                         'get_period': self._get_period,
38                         'get_codes': self._get_codes,
39                         'get_general': self._get_general,
40                         'get_company': self._get_company,
41                         'get_currency': self._get_currency,
42                         'get_lines' : self._get_lines,
43                 })
44
45                 
46         def comma_me(self,amount):
47                 
48                 if  type(amount) is float :
49                         amount = str('%.2f'%amount)
50                 else :
51                         amount = str(amount)
52                 if (amount == '0'):
53                      return ' '
54                 orig = amount
55                 new = re.sub("^(-?\d+)(\d{3})", "\g<1>'\g<2>", amount)
56                 if orig == new:
57                         return new
58                 else:
59                         return self.comma_me(new)
60         def _get_lines(self, based_on,period_list,company_id=False, parent=False, level=0):
61                 res = self._get_codes(based_on,company_id,parent,level,period_list)
62                 
63                 if period_list[0][2] :
64                         res = self._add_codes(based_on,res,period_list)
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 = %d"%(fy[0][0]))
69                         periods = self.cr.fetchall()
70                         for p in periods :
71                                 period_list[0][2].append(p[0])
72                         res = self._add_codes(based_on,res,period_list)
73                 
74                 i = 0
75                 top_result = []
76                 
77                 while i < len(res):
78                         
79                         res_dict = { 'code' : res[i][1].code,
80                                 'name' : res[i][1].name,
81                                 'debit' : 0,
82                                 'credit' : 0,
83                                 'tax_amount' : res[i][1].sum_period,
84                                 'type' : 1,
85                                 'level' : res[i][0],
86                                 'pos' : 0
87                         }
88                         
89                         top_result.append(res_dict)
90                         res_general = self._get_general(res[i][1].id,period_list,company_id,based_on)
91                         ind_general = 0
92                         while ind_general < len(res_general) :
93                                 res_general[ind_general]['type'] = 2
94                                 res_general[ind_general]['pos'] = 0
95                                 res_general[ind_general]['level'] = res_dict['level']
96                                 top_result.append(res_general[ind_general])
97                                 ind_general+=1
98                         i+=1
99                 #array_result = self.sort_result(top_result)
100                 return top_result
101                 #return array_result
102
103         def _get_period(self, period_id):
104                 return self.pool.get('account.period').browse(self.cr, self.uid, period_id).name
105
106         def _get_general(self, tax_code_id,period_list ,company_id, based_on):
107                 res=[]
108                 period_sql_list =  ','.join(map(str, period_list[0][2]))
109                 
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 ((invoice.state = %s) \
129                                                         OR (invoice.id IS NULL))  \
130                                         GROUP BY account.id,account.name,account.code', ('draft',tax_code_id,
131                                                 company_id, 'paid'))
132
133                 else :
134                         self.cr.execute('SELECT SUM(line.tax_amount) AS tax_amount, \
135                                                 SUM(line.debit) AS debit, \
136                                                 SUM(line.credit) AS credit, \
137                                                 COUNT(*) AS count, \
138                                                 account.id AS account_id, \
139                                                 account.name AS name,  \
140                                                 account.code AS code \
141                                         FROM account_move_line AS line, \
142                                                 account_account AS account \
143                                         WHERE line.state <> %s \
144                                                 AND line.tax_code_id = %s  \
145                                                 AND line.account_id = account.id \
146                                                 AND account.company_id = %s \
147                                                 AND account.active \
148                                         GROUP BY account.id,account.name,account.code', ('draft',tax_code_id,
149                                                 company_id))
150                 res = self.cr.dictfetchall()
151                 
152                                                 #AND line.period_id IN ('+ period_sql_list +') \
153                 
154                 i = 0
155                 while i<len(res):
156                         res[i]['account'] = self.pool.get('account.account').browse(self.cr, self.uid, res[i]['account_id'])
157                         i+=1
158                 return res
159
160         def _get_codes(self,based_on, company_id, parent=False, level=0,period_list=[]):
161                 tc = self.pool.get('account.tax.code')
162                 ids = tc.search(self.cr, self.uid, [('parent_id','=',parent),('company_id','=',company_id)])
163         
164                 res = []
165                 for code in 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)
169                 return res
170         
171         def _add_codes(self,based_on, account_list=[],period_list=[]):
172                 res = []
173                 for account in account_list:
174                         tc = self.pool.get('account.tax.code')
175                         ids = tc.search(self.cr, self.uid, [('id','=',account[1].id)])
176                         sum_tax_add = 0
177                         for period_ind in period_list[0][2]:
178                                 for code in 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         
187         def _get_company(self, form):
188                 return pooler.get_pool(self.cr.dbname).get('res.company').browse(self.cr, self.uid, form['company_id']).name
189
190         def _get_currency(self, form):
191                 return pooler.get_pool(self.cr.dbname).get('res.company').browse(self.cr, self.uid, form['company_id']).currency_id.name
192         
193         def sort_result(self,accounts):
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                                         tot_elem = copy.copy(accounts[bcl_rup_ind])
211                                         res_tot = { 'code' : accounts[bcl_rup_ind]['code'],
212                                                 'name' : '',
213                                                 'debit' : 0,
214                                                 'credit' : 0,
215                                                 'tax_amount' : accounts[bcl_rup_ind]['tax_amount'],
216                                                 'type' : accounts[bcl_rup_ind]['type'],
217                                                 'level' : 0,
218                                                 'pos' : 0
219                                         }
220                                         
221                                         if res_tot['type'] == 1:
222                                                 # on change le type pour afficher le total
223                                                 res_tot['type'] = 2
224                                                 result_accounts.append(res_tot)
225                                         bcl_current_level =  accounts[bcl_rup_ind]['level']
226                                         bcl_rup_ind -= 1
227                                         
228                         old_level = account_elem['level']
229                         result_accounts.append(account_elem)
230                         ind+=1
231                         
232                                 
233                 return result_accounts
234         
235
236 report_sxw.report_sxw('report.account.vat.declaration', 'account.tax.code',
237         'addons/account/report/tax_report.rml', parser=tax_report, header=False)
238
239 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: