1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
24 from report import report_sxw
26 #from addons.account.wizard import wizard_account_balance_report
35 class account_balance(report_sxw.rml_parse):
36 def __init__(self, cr, uid, name, context):
37 super(account_balance, self).__init__(cr, uid, name, context=context)
46 self.result_total = {}
47 self.total_for_perc=[]
48 self.localcontext.update({
51 'get_lines':self.get_lines,
52 'linesForTotal': self.linesForTotal,
53 'linesForYear': self.linesForYear,
54 'get_years':self.get_years,
55 'cal_total':self.cal_total,
56 'total_dr':self.total_dr,
57 'total_cr':self.total_cr
59 self.context = context
61 def linesForYear(self, form):
71 pattern=form['compare_pattern']
73 if form['show_columns']!=1:
76 show=form['show_columns']
78 if form['format_perc']!=1:
81 perc=form['format_perc']
83 if form['account_choice']=='bal_zero':
88 ctx = self.context.copy()
91 if form['select_account']!=False:
92 ref_ac=self.pool.get('account.account').browse(self.cr, self.uid, form['select_account'], ctx.copy())
93 if ref_ac.balance<>0.00:
94 ref_bal=ref_ac.balance
105 self.total_for_perc=self.linesForTotal(form, ids={}, doneAccount={}, level=1)
108 for t1 in range(0,len(form['fiscalyear'])):
109 locale.setlocale(locale.LC_ALL, '')
110 self.result_total["sum_credit" + str(t1)]=locale.format("%.2f", self.result_total["sum_credit" + str(t1)], grouping=True)
111 self.result_total["sum_debit" + str(t1)]=locale.format("%.2f", self.result_total["sum_debit" + str(t1)], grouping=True)
113 for temp in range(0,len(form['fiscalyear'])):
114 fy=self.pool.get('account.fiscalyear').name_get(self.cr, self.uid, form['fiscalyear'][temp])
115 years["year"+str(temp)]=fy[0][1][12:16]
120 def linesForTotal(self, form, ids={}, doneAccount={}, level=1):
121 if self.done_total==1:
124 return [self.result_total]
130 ctx = self.context.copy()
131 result_total_parent=[]
133 for id in form['fiscalyear']:
136 ctx['fiscalyear'] = id
137 ctx['periods'] = form['periods']
138 ctx['period_manner'] = form['period_manner']
139 ctx['state'] = form['context'].get('state','all')
140 tmp = self.pool.get('account.account').browse(self.cr, self.uid, ids, ctx.copy())
145 merged_accounts=zip(*accounts)
146 # used to check for the frst record so all sum_credit and sum_debit r set to 0.00
149 for entry in merged_accounts:
151 if entry[0].id in doneAccount:
153 doneAccount[entry[0].id] = 1
154 for k in range(0,len(entry)):
157 if entry[0].type <> 'view':
158 temp_credit+=entry[k].credit
159 temp_debit+=entry[k].debit
162 self.result_total["sum_credit" + str(k)]=0.00
163 self.result_total["sum_debit" + str(k)]=0.00
164 if form['account_choice']=='bal_zero':
165 if temp_credit<>temp_debit:
166 self.result_total["sum_credit" + str(k)]+=temp_credit
167 self.result_total["sum_debit" + str(k)]+=temp_debit
169 self.result_total["sum_credit" + str(k)]+=temp_credit
170 self.result_total["sum_debit" + str(k)]+=temp_debit
174 if entry[0].child_id:
175 ids2 = [(x.code,x.id) for x in entry[0].child_id]
178 result_total_parent = self.linesForTotal(form, [x[1] for x in ids2], doneAccount, level+1)
180 return [self.result_total]
182 def lines(self, form, ids={}, done={}, level=1):
189 ctx = self.context.copy()
191 for id in form['fiscalyear']:
193 ctx['fiscalyear'] = id
194 ctx['periods'] = form['periods']
195 ctx['period_manner']=form['period_manner']
196 ctx['state'] = form['context'].get('state','all')
197 tmp1 = self.pool.get('account.account').browse(self.cr, self.uid, ids, ctx.copy())
200 accounts.append(tmp1)
202 if level==1: #if parent is called,done is not empty when called again.
206 return cmp(x.code, y.code)
207 for n in range(0,len(accounts)):
208 accounts[n].sort(cmp_code)
210 merged_accounts=zip(*accounts)
212 for entry in merged_accounts:
216 if form['account_choice']!='all': # if checked,include empty a/c;not otherwise
219 if entry[0].id in done:
221 done[entry[0].id] = 1
223 if entry[0].child_id: # this is for parent account,dont check 0 for it
225 self.status=1 # for displaying it Bold
230 for i in range(0,len(entry)):
232 if entry[i].balance<>0.0:
239 if entry[i].credit <> 0.0 or entry[i].debit <> 0.0:
247 # this is the point where we skip those accounts which are encountered as empty ones
253 'code': entry[0].code,
254 'name': entry[0].name,
256 'status': self.status,
259 for j in range(0,len(entry)):
261 locale.setlocale(locale.LC_ALL, '')
262 res["debit"+str(j)]=locale.format("%.2f", entry[j].debit, grouping=True)
263 res["credit"+str(j)]=locale.format("%.2f", entry[j].credit, grouping=True)
264 res["balance"+str(j)]=locale.format("%.2f", entry[j].balance, grouping=True)
268 res["bal_cash"+str(j)]="0.00"
269 res["bal_perc"+str(j)]="0.00%"
271 temp_cash=entry[j].balance - entry[j-1].balance
272 res["bal_cash"+str(j)]=locale.format("%.2f", temp_cash, grouping=True)
273 if entry[j-1].balance<>0.00:
274 temp_perc=(entry[j].balance - entry[j-1].balance )*100/entry[j-1].balance
276 temp_perc=(entry[j].balance) *100
278 res["bal_perc"+str(j)]=locale.format("%.2f", temp_perc) + "%"
280 if ref_bal=='nothing':
286 if self.parent_bal==1:
287 res["balance_perc"+str(j)]="/"
289 if entry[j].balance==0.00:
290 if self.baldiv["baldiv"+str(level-1)+str(j)]<>0.00:
291 res["balance_perc"+str(j)]="0.00%"
293 res["balance_perc"+str(j)]="/"
295 if self.baldiv["baldiv"+str(level-1)+str(j)]<>0.00:
296 temp=self.baldiv["baldiv"+str(level-1)+str(j)]
297 temp1=(entry[j].balance * 100 )/ float(temp)
299 res["balance_perc" + str(j)]=str(temp1)+"%"
301 res["balance_perc"+str(j)]="/"
303 res["balance_perc"+str(j)]=str( (entry[j].balance * 100 )/ float(ref_bal)) + "%"
307 if entry[0].child_id:
309 for q in range(0,len(form['fiscalyear'])):
310 self.baldiv["baldiv"+str(level)+str(q)]=entry[q].balance
312 ids2 = [(x.code,x.id) for x in entry[0].child_id]
315 dir += self.lines(form, [x[1] for x in ids2], done, level+1)
317 for w in range(0,len(form['fiscalyear'])):
318 if entry[w].credit <> 0.0 or entry[w].debit <> 0.0 or entry[w].balance<>0.00:
326 result.pop(-1) # here we pop up the parent having its children as emprty accounts
332 def get_years(self, form):
335 for temp in range(0,len(form['fiscalyear'])):
337 fy=self.pool.get('account.fiscalyear').name_get(self.cr, self.uid, form['fiscalyear'][temp])
342 self.linesForYear(form)
345 def get_lines(self, year_dict, form):
349 line_l = self.lines(form)
350 self.cal_total(year_dict)
354 res['code'] = l['code']
355 res['name'] = l['name']
356 res['level'] = l['level']
357 for k,v in l.items():
358 if k.startswith('debit'+str(year_dict['last_str'])):
360 if k.startswith('credit'+str(year_dict['last_str'])):
362 if k.startswith('balance'+str(year_dict['last_str'])) and not k.startswith('balance_perc'+str(year_dict['last_str'])):
364 if k.startswith('balance_perc'+str(year_dict['last_str'])) and not k.startswith('balance'+str(year_dict['last_str'])):
365 res['balance_perc'] = v
366 if form['compare_pattern'] == 'bal_perc':
367 if k.startswith('bal_perc'+str(year_dict['last_str'])):
369 elif form['compare_pattern'] == 'bal_cash':
370 if k.startswith('bal_cash'+str(year_dict['last_str'])):
374 final_result.append(res)
377 def cal_total(self, year_dict):
378 total_l = self.result_total
380 for k,v in total_l.items():
381 if k.startswith('sum_debit'+str(year_dict['last_str'])):
383 elif k.startswith('sum_credit'+str(year_dict['last_str'])):
395 report_sxw.report_sxw('report.account.balance.account.balance', 'account.account', 'addons/account/report/account_balance.rml', parser=account_balance, header="internal")
396 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: