1 ##############################################################################
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ##############################################################################
32 from operator import itemgetter
35 from report import report_sxw
37 class account_balance(report_sxw.rml_parse):
39 _name = 'report.account.account.balance'
40 def __init__(self, cr, uid, name, context):
41 super(account_balance, self).__init__(cr, uid, name, context)
43 self.sum_credit = 0.00
45 self.date_lst_string = ''
46 self.localcontext.update({
49 'moveline':self.moveline,
50 'sum_debit': self._sum_debit,
51 'sum_credit': self._sum_credit,
52 'get_fiscalyear':self.get_fiscalyear,
53 'get_periods':self.get_periods,
55 self.context = context
57 def get_fiscalyear(self, form):
59 if form.has_key('fiscalyear'):
60 fisc_id = form['fiscalyear']
63 self.cr.execute("select name from account_fiscalyear where id = %d" %(int(fisc_id)))
64 res=self.cr.fetchone()
65 return res and res[0] or ''
67 def get_periods(self, form):
69 if form.has_key('periods'):
70 period_ids = ",".join([str(x) for x in form['periods'][0][2] if x])
71 self.cr.execute("select name from account_period where id in (%s)" % (period_ids))
72 res=self.cr.fetchall()
74 if (r == res[res.__len__()-1]):
78 return str(result and result[:-1]) or ''
80 def lines(self, form, ids={}, done=None, level=1):
89 ctx = self.context.copy()
90 if form.has_key('fiscalyear'):
91 self.transform_period_into_date_array(form)
92 ctx['fiscalyear'] = form['fiscalyear']
93 ctx['periods'] = form['periods'][0][2]
95 self.transform_date_into_date_array(form)
96 ctx['date_from'] = form['date_from']
97 ctx['date_to'] = form['date_to']
99 accounts = self.pool.get('account.account').browse(self.cr, self.uid, ids, ctx)
101 return cmp(x.code, y.code)
102 accounts.sort(cmp_code)
103 for account in accounts:
104 if account.id in done:
117 'code': account.code,
118 'name': account.name,
120 'debit': account.debit,
121 'credit': account.credit,
122 'balance': account.balance,
123 'leef': not bool(account.child_id),
126 self.sum_debit += account.debit
127 self.sum_credit += account.credit
128 if not (res['credit'] or res['debit']) and not account.child_id:
131 def _check_rec(account):
132 if not account.child_id:
133 return bool(account.credit or account.debit)
134 for c in account.child_id:
138 if not _check_rec(account):
140 if form['display_account'] == 'bal_mouvement':
141 if res['credit'] <> 0 or res['debit'] <> 0 or res['balance'] <> 0:
142 result_acc.append(res)
143 elif form['display_account'] == 'bal_solde':
144 if res['balance'] <> 0:
145 result_acc.append(res)
147 result_acc.append(res)
148 res1 = self.moveline(form, account.id,res['level'])
152 if account.code=='0':
155 ids2 = [(x.code,x.id) for x in account.child_id]
157 result_acc += self.lines(form, [x[1] for x in ids2], done, level+1)
160 def moveline(self,form,ids,level):
162 self.date_lst_string = '\'' + '\',\''.join(map(str,self.date_lst)) + '\''
164 "SELECT l.id as lid,l.date,j.code as jname, l.ref, l.name as lname, l.debit as debit1, l.credit as credit1 " \
165 "FROM account_move_line l " \
166 "LEFT JOIN account_journal j " \
167 "ON (l.journal_id = j.id) " \
168 "WHERE l.account_id = '"+str(ids)+"' " \
169 "AND l.date IN (" + self.date_lst_string + ") " \
171 res = self.cr.dictfetchall()
174 sum = r['debit1'] - r['credit1']
190 def date_range(self,start,end):
191 start = datetime.date.fromtimestamp(time.mktime(time.strptime(start,"%Y-%m-%d")))
192 end = datetime.date.fromtimestamp(time.mktime(time.strptime(end,"%Y-%m-%d")))
195 r = (end+datetime.timedelta(days=1)-start).days
197 date_array = [start+datetime.timedelta(days=i) for i in range(r)]
198 for date in date_array:
199 full_str_date.append(str(date))
203 def transform_period_into_date_array(self,form):
204 ## Get All Period Date
205 if not form['periods'][0][2] :
206 periods_id = self.pool.get('account.period').search(self.cr, self.uid, [('fiscalyear_id','=',form['fiscalyear'])])
208 periods_id = form['periods'][0][2]
210 for period_id in periods_id:
211 period_obj = self.pool.get('account.period').browse(self.cr, self.uid, period_id)
212 date_array = date_array + self.date_range(period_obj.date_start,period_obj.date_stop)
214 self.date_lst = date_array
217 def transform_date_into_date_array(self,form):
218 return_array = self.date_range(form['date_from'],form['date_to'])
219 self.date_lst = return_array
222 def _sum_credit(self):
223 return self.sum_credit
225 def _sum_debit(self):
226 return self.sum_debit
227 report_sxw.report_sxw('report.account.account.balance', 'account.account', 'addons/account/report/account_balance.rml', parser=account_balance, header=2)