~vauxoo/account-financial-report/7.0-Migrate-account-financial-report

« back to all changes in this revision

Viewing changes to account_financial_report/report/parser.py

  • Committer: Humberto Arocha
  • Date: 2012-08-15 06:37:35 UTC
  • mfrom: (18.1.49 merging_parsers_hbto)
  • Revision ID: humbertoarocha@gmail.com-20120815063735-v502b9gsmzj06xs8

[MERGE] Se mezcla nueva iteracion del asistente
de reportes finacieros, que cuenta con:
- Balance General
- Estado de Resultado,
- Mayor Analitico,
Adicionalmente,
Tanto el Balance General como el Estado de Resultados
se pueden Pedir en cinco sabores:
- Una Columna (Saldo al Final del Periodo)
- Dos Columnas (Debito y Credito del Periodo pedido)
- Cuatro Columnas (Inicial, Debito, Credito, Saldo Final)
- Cinco Columnas (Inicial, Debito, Credito, Cambio en Periodo, Saldo Final)
- Trece Columnas (12 Meses, Saldo al Final)
- Cuartos (4 Cuartos y Saldo al Final)

Todos los reportes ahora usan un solo parser,
que centraliza y minimiza el proceso de correciones,
y optimiza el proceso de mejoras.

Agrega tambien esta mejora una seccion para guardar
Plantillas que se pueden reutilizar,

Asi como la generacion de los reportes expresados
en otras monedas.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
###########################################################################
 
3
#    Module Writen to OpenERP, Open Source Management Solution
 
4
#    Copyright (C) OpenERP Venezuela (<http://openerp.com.ve>).
 
5
#    All Rights Reserved
 
6
###############Credits######################################################
 
7
#    Coded by:   Humberto Arocha humberto@openerp.com.ve
 
8
#                Angelica Barrios angelicaisabelb@gmail.com
 
9
#               Jordi Esteve <jesteve@zikzakmedia.com>
 
10
#               Javier Duran <javieredm@gmail.com>
 
11
#    Planified by: Humberto Arocha
 
12
#    Finance by: LUBCAN COL S.A.S http://www.lubcancol.com
 
13
#    Audited by: Humberto Arocha humberto@openerp.com.ve
 
14
#############################################################################
 
15
#    This program is free software: you can redistribute it and/or modify
 
16
#    it under the terms of the GNU General Public License as published by
 
17
#    the Free Software Foundation, either version 3 of the License, or
 
18
#    (at your option) any later version.
 
19
#
 
20
#    This program is distributed in the hope that it will be useful,
 
21
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
#    GNU General Public License for more details.
 
24
#
 
25
#    You should have received a copy of the GNU General Public License
 
26
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
27
##############################################################################
 
28
 
 
29
import xml
 
30
import copy
 
31
from operator import itemgetter
 
32
import time
 
33
import datetime
 
34
from report import report_sxw
 
35
from tools import config
 
36
from tools.translate import _
 
37
from osv import osv
 
38
class account_balance(report_sxw.rml_parse):
 
39
 
 
40
    def __init__(self, cr, uid, name, context):
 
41
        super(account_balance, self).__init__(cr, uid, name, context)
 
42
        self.sum_debit = 0.00
 
43
        self.sum_credit = 0.00
 
44
        self.sum_balance = 0.00
 
45
        self.sum_debit_fy = 0.00
 
46
        self.sum_credit_fy = 0.00
 
47
        self.sum_balance_fy = 0.00
 
48
        self.date_lst = []
 
49
        self.date_lst_string = ''
 
50
        self.localcontext.update({
 
51
            'time': time,
 
52
            'lines': self.lines,
 
53
            'get_fiscalyear_text': self.get_fiscalyear_text,
 
54
            'get_periods_and_date_text': self.get_periods_and_date_text,
 
55
            'get_informe_text': self.get_informe_text,
 
56
            'get_month':self.get_month,
 
57
            'exchange_name':self.exchange_name,
 
58
        })
 
59
        self.context = context
 
60
 
 
61
 
 
62
    def get_fiscalyear_text(self, form):
 
63
        """
 
64
        Returns the fiscal year text used on the report.
 
65
        """
 
66
        fiscalyear_obj = self.pool.get('account.fiscalyear')
 
67
        fiscalyear = None
 
68
        if form.get('fiscalyear'):
 
69
            fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, form['fiscalyear'])
 
70
            return fiscalyear.name or fiscalyear.code
 
71
        else:
 
72
            fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, fiscalyear_obj.find(self.cr, self.uid))
 
73
            return "%s*" % (fiscalyear.name or fiscalyear.code)
 
74
 
 
75
    def get_informe_text(self, form):
 
76
        """
 
77
        Returns the header text used on the report.
 
78
        """
 
79
        afr_id = form['afr_id'] and type(form['afr_id']) in (list,tuple) and form['afr_id'][0] or form['afr_id']
 
80
        if afr_id:
 
81
            name = self.pool.get('afr').browse(self.cr, self.uid, afr_id).name
 
82
        elif form['analytic_ledger'] and form['columns']=='four' and form['inf_type'] == 'BS':
 
83
            name = _('Analytic Ledger')
 
84
        elif form['inf_type'] == 'BS':
 
85
            name = _('Balance Sheet')
 
86
        elif form['inf_type'] == 'IS':
 
87
            name = _('Income Statement')
 
88
        
 
89
        return name
 
90
 
 
91
    def get_month(self, form):
 
92
        '''
 
93
        return day, year and month
 
94
        '''
 
95
        if form['filter'] in ['bydate', 'all']:
 
96
            months=["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"]
 
97
            mes = months[time.strptime(form['date_to'],"%Y-%m-%d")[1]-1]
 
98
            ano = time.strptime(form['date_to'],"%Y-%m-%d")[0]
 
99
            dia = time.strptime(form['date_to'],"%Y-%m-%d")[2]
 
100
            return _('From ')+self.formatLang(form['date_from'], date=True)+ _(' to ')+self.formatLang(form['date_to'], date=True)
 
101
        elif form['filter'] in ['byperiod', 'all']:
 
102
            aux=[]
 
103
            period_obj = self.pool.get('account.period')
 
104
            
 
105
            for period in period_obj.browse(self.cr, self.uid, form['periods']):
 
106
                aux.append(period.date_start)
 
107
                aux.append(period.date_stop)
 
108
            sorted(aux)
 
109
            return _('From ')+self.formatLang(aux[0], date=True)+_(' to ')+self.formatLang(aux[-1], date=True)
 
110
 
 
111
    def get_periods_and_date_text(self, form):
 
112
        """
 
113
        Returns the text with the periods/dates used on the report.
 
114
        """
 
115
        period_obj = self.pool.get('account.period')
 
116
        periods_str = None
 
117
        fiscalyear_id = form['fiscalyear'] or fiscalyear_obj.find(self.cr, self.uid)
 
118
        period_ids = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear_id),('special','=',False)])
 
119
        if form['filter'] in ['byperiod', 'all']:
 
120
            period_ids = form['periods']
 
121
        periods_str = ', '.join([period.name or period.code for period in period_obj.browse(self.cr, self.uid, period_ids)])
 
122
 
 
123
        dates_str = None
 
124
        if form['filter'] in ['bydate', 'all']:
 
125
            dates_str = self.formatLang(form['date_from'], date=True) + ' - ' + self.formatLang(form['date_to'], date=True) + ' '
 
126
        return {'periods':periods_str, 'date':dates_str}
 
127
 
 
128
 
 
129
    def special_period(self, periods):
 
130
        period_obj = self.pool.get('account.period')
 
131
        period_brw = period_obj.browse(self.cr, self.uid, periods)
 
132
        period_counter = [True for i in period_brw if not i.special]
 
133
        if not period_counter:
 
134
            return True
 
135
        return False
 
136
        
 
137
    def exchange_name(self, form):
 
138
        self.from_currency_id = self.get_company_currency(form['company_id'] and type(form['company_id']) in (list,tuple) and form['company_id'][0] or form['company_id'])
 
139
        if not form['currency_id']:
 
140
            self.to_currency_id = self.from_currency_id
 
141
        else:
 
142
            self.to_currency_id = form['currency_id'] and type(form['currency_id']) in (list, tuple) and form['currency_id'][0] or form['currency_id']
 
143
        return self.pool.get('res.currency').browse(self.cr, self.uid, self.to_currency_id).name
 
144
 
 
145
    def exchange(self, from_amount):
 
146
        if self.from_currency_id == self.to_currency_id:
 
147
            return from_amount
 
148
        curr_obj = self.pool.get('res.currency')
 
149
        return curr_obj.compute(self.cr, self.uid, self.from_currency_id, self.to_currency_id, from_amount)
 
150
    
 
151
    def get_company_currency(self, company_id):
 
152
        rc_obj = self.pool.get('res.company')
 
153
        return rc_obj.browse(self.cr, self.uid, company_id).currency_id.id
 
154
    
 
155
    def get_company_accounts(self, company_id, acc='credit'):
 
156
        rc_obj = self.pool.get('res.company')
 
157
        if acc=='credit':
 
158
            return [brw.id for brw in rc_obj.browse(self.cr, self.uid, company_id).credit_account_ids]
 
159
        else:
 
160
            return [brw.id for brw in rc_obj.browse(self.cr, self.uid, company_id).debit_account_ids]
 
161
 
 
162
 
 
163
    def _get_analytic_ledger(self, account, ctx={}):
 
164
        res = []
 
165
 
 
166
        if account['type'] in ('other','liquidity','receivable','payable'):
 
167
            #~ TODO: CUANDO EL PERIODO ESTE VACIO LLENARLO CON LOS PERIODOS DEL EJERCICIO
 
168
            #~ FISCAL, SIN LOS PERIODOS ESPECIALES
 
169
            periods = ', '.join([str(i) for i in ctx['periods']])
 
170
            #~ periods = str(tuple(ctx['periods']))
 
171
            where = """where aml.period_id in (%s) and aa.id = %s and aml.state <> 'draft'"""%(periods,account['id'])
 
172
 
 
173
            sql_detalle = """select aml.id as id, aj.name as diario, aa.name as descripcion,
 
174
                (select name from res_partner where aml.partner_id = id) as partner,
 
175
                aa.code as cuenta, aml.name as name,
 
176
                aml.ref as ref,
 
177
                case when aml.debit is null then 0.00 else aml.debit end as debit, 
 
178
                case when aml.credit is null then 0.00 else aml.credit end as credit,
 
179
                (select code from account_analytic_account where  aml.analytic_account_id = id) as analitica,
 
180
                aml.date as date, ap.name as periodo,
 
181
                am.name as asiento
 
182
                from account_move_line aml
 
183
                inner join account_journal aj on aj.id = aml.journal_id
 
184
                inner join account_account aa on aa.id = aml.account_id
 
185
                inner join account_period ap on ap.id = aml.period_id
 
186
                inner join account_move am on am.id = aml.move_id """ + where +\
 
187
                """ order by date, am.name"""
 
188
 
 
189
            self.cr.execute(sql_detalle)
 
190
            resultat = self.cr.dictfetchall()
 
191
            balance = account['balanceinit']
 
192
            for det in resultat:
 
193
                balance += det['debit'] - det['credit']
 
194
                res.append({
 
195
                    'id': det['id'],
 
196
                    'date': det['date'],
 
197
                    'journal':det['diario'],
 
198
                    'partner':det['partner'],
 
199
                    'name':det['name'],
 
200
                    'entry':det['asiento'],
 
201
                    'ref': det['ref'],
 
202
                    'debit': det['debit'],
 
203
                    'credit': det['credit'],
 
204
                    'analytic': det['analitica'],
 
205
                    'period': det['periodo'],
 
206
                    'balance': balance,
 
207
                })
 
208
        return res
 
209
 
 
210
    def lines(self, form, level=0):
 
211
        """
 
212
        Returns all the data needed for the report lines
 
213
        (account info plus debit/credit/balance in the selected period
 
214
        and the full year)
 
215
        """
 
216
        
 
217
        account_obj = self.pool.get('account.account')
 
218
        period_obj = self.pool.get('account.period')
 
219
        fiscalyear_obj = self.pool.get('account.fiscalyear')
 
220
        
 
221
        def _get_children_and_consol(cr, uid, ids, level, context={},change_sign=False):
 
222
            aa_obj = self.pool.get('account.account')
 
223
            ids2=[]
 
224
            for aa_brw in aa_obj.browse(cr, uid, ids, context):
 
225
                if aa_brw.child_id and aa_brw.level < level and aa_brw.type !='consolidation':
 
226
                    if not change_sign:
 
227
                        ids2.append([aa_brw.id,True, False,aa_brw])
 
228
                    ids2 += _get_children_and_consol(cr, uid, [x.id for x in aa_brw.child_id], level, context,change_sign=change_sign)
 
229
                    if change_sign:
 
230
                        ids2.append(aa_brw.id) 
 
231
                    else:
 
232
                        ids2.append([aa_brw.id,False,True,aa_brw])
 
233
                else:
 
234
                    if change_sign:
 
235
                        ids2.append(aa_brw.id) 
 
236
                    else:
 
237
                        ids2.append([aa_brw.id,True,True,aa_brw])
 
238
            return ids2
 
239
 
 
240
        #############################################################################
 
241
        # CONTEXT FOR ENDIND BALANCE                                                #
 
242
        #############################################################################
 
243
 
 
244
        def _ctx_end(ctx):
 
245
            ctx_end = ctx
 
246
            ctx_end['filter'] = form.get('filter','all')
 
247
            ctx_end['fiscalyear'] = fiscalyear.id
 
248
            #~ ctx_end['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('special','=',False)])
 
249
            
 
250
            if ctx_end['filter'] not in ['bydate','none']:
 
251
                special = self.special_period(form['periods'])
 
252
            else:
 
253
                special = False
 
254
            
 
255
            if form['filter'] in ['byperiod', 'all']:
 
256
                if special:
 
257
                    ctx_end['periods'] = period_obj.search(self.cr, self.uid, [('id','in',form['periods'] or ctx_end.get('periods',False))])
 
258
                else:
 
259
                    ctx_end['periods'] = period_obj.search(self.cr, self.uid, [('id','in',form['periods'] or ctx_end.get('periods',False)),('special','=',False)])
 
260
                    
 
261
            if form['filter'] in ['bydate','all','none']:
 
262
                ctx_end['date_from'] = form['date_from']
 
263
                ctx_end['date_to'] = form['date_to']
 
264
            
 
265
            return ctx_end.copy()
 
266
        
 
267
        def missing_period(ctx_init):
 
268
            
 
269
            ctx_init['fiscalyear'] = fiscalyear_obj.search(self.cr, self.uid, [('date_stop','<',fiscalyear.date_start)],order='date_stop') and \
 
270
                                fiscalyear_obj.search(self.cr, self.uid, [('date_stop','<',fiscalyear.date_start)],order='date_stop')[-1] or []
 
271
            ctx_init['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',ctx_init['fiscalyear']),('date_stop','<',fiscalyear.date_start)])
 
272
            return ctx_init
 
273
        #############################################################################
 
274
        # CONTEXT FOR INITIAL BALANCE                                               #
 
275
        #############################################################################
 
276
        
 
277
        def _ctx_init(ctx):
 
278
            ctx_init = self.context.copy()
 
279
            ctx_init['filter'] = form.get('filter','all')
 
280
            ctx_init['fiscalyear'] = fiscalyear.id
 
281
 
 
282
            if form['filter'] in ['byperiod', 'all']:
 
283
                ctx_init['periods'] = form['periods']
 
284
                if not ctx_init['periods']:
 
285
                    ctx_init = missing_period(ctx_init.copy())
 
286
                date_start = min([period.date_start for period in period_obj.browse(self.cr, self.uid, ctx_init['periods'])])
 
287
                ctx_init['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('date_stop','<=',date_start)])
 
288
            elif form['filter'] in ['bydate']:
 
289
                ctx_init['date_from'] = fiscalyear.date_start
 
290
                ctx_init['date_to'] = form['date_from']
 
291
                ctx_init['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('date_stop','<=',ctx_init['date_to'])])
 
292
            elif form['filter'] == 'none':
 
293
                ctx_init['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('special','=',True)])
 
294
                date_start = min([period.date_start for period in period_obj.browse(self.cr, self.uid, ctx_init['periods'])])
 
295
                ctx_init['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('date_start','<=',date_start),('special','=',True)])
 
296
            
 
297
            return ctx_init.copy()
 
298
 
 
299
        def z(n):
 
300
            return abs(n) < 0.005 and 0.0 or n
 
301
                
 
302
 
 
303
        self.from_currency_id = self.get_company_currency(form['company_id'] and type(form['company_id']) in (list,tuple) and form['company_id'][0] or form['company_id'])
 
304
        if not form['currency_id']:
 
305
            self.to_currency_id = self.from_currency_id
 
306
        else:
 
307
            self.to_currency_id = form['currency_id'] and type(form['currency_id']) in (list, tuple) and form['currency_id'][0] or form['currency_id']
 
308
 
 
309
        if form.has_key('account_list') and form['account_list']:
 
310
            account_ids = form['account_list']
 
311
            del form['account_list']
 
312
        
 
313
        credit_account_ids = self.get_company_accounts(form['company_id'] and type(form['company_id']) in (list,tuple) and form['company_id'][0] or form['company_id'],'credit')
 
314
        
 
315
        debit_account_ids = self.get_company_accounts(form['company_id'] and type(form['company_id']) in (list,tuple) and form['company_id'][0] or form['company_id'],'debit')
 
316
 
 
317
        if form.get('fiscalyear'):
 
318
            if type(form.get('fiscalyear')) in (list,tuple):
 
319
                fiscalyear = form['fiscalyear'] and form['fiscalyear'][0]
 
320
            elif type(form.get('fiscalyear')) in (int,):
 
321
                fiscalyear = form['fiscalyear']
 
322
        fiscalyear = fiscalyear_obj.browse(self.cr, self.uid, fiscalyear)
 
323
 
 
324
        ################################################################
 
325
        # Get the accounts                                             #
 
326
        ################################################################
 
327
 
 
328
        account_ids = _get_children_and_consol(self.cr, self.uid, account_ids, form['display_account_level'] and form['display_account_level'] or 100,self.context)
 
329
        
 
330
        credit_account_ids = _get_children_and_consol(self.cr, self.uid, credit_account_ids, 100,self.context,change_sign=True)
 
331
        
 
332
        debit_account_ids = _get_children_and_consol(self.cr, self.uid, debit_account_ids, 100,self.context,change_sign=True)
 
333
        
 
334
        credit_account_ids = list(set(credit_account_ids) - set(debit_account_ids))
 
335
 
 
336
        #
 
337
        # Generate the report lines (checking each account)
 
338
        #
 
339
        
 
340
        tot_check = False
 
341
        
 
342
        if not form['periods']:
 
343
            form['periods'] = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('special','=',False)],order='date_start asc')
 
344
            if not form['periods']:
 
345
                raise osv.except_osv(_('UserError'),_('The Selected Fiscal Year Does not have Regular Periods'))
 
346
 
 
347
        if form['columns'] == 'qtr':
 
348
            period_ids = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('special','=',False)],order='date_start asc')
 
349
            a=0
 
350
            l=[]
 
351
            p=[]
 
352
            for x in period_ids:
 
353
                a+=1
 
354
                if a<3:
 
355
                        l.append(x)
 
356
                else:
 
357
                        l.append(x)
 
358
                        p.append(l)
 
359
                        l=[]
 
360
                        a=0
 
361
            
 
362
            #~ period_ids = p
 
363
 
 
364
        elif form['columns'] == 'thirteen':
 
365
            period_ids = period_obj.search(self.cr, self.uid, [('fiscalyear_id','=',fiscalyear.id),('special','=',False)],order='date_start asc')
 
366
 
 
367
        if form['columns'] == 'qtr':
 
368
            tot_bal1 = 0.0
 
369
            tot_bal2 = 0.0
 
370
            tot_bal3 = 0.0
 
371
            tot_bal4 = 0.0
 
372
            tot_bal5 = 0.0
 
373
 
 
374
        elif form['columns'] == 'thirteen':
 
375
            tot_bal1 = 0.0
 
376
            tot_bal2 = 0.0
 
377
            tot_bal3 = 0.0
 
378
            tot_bal4 = 0.0
 
379
            tot_bal5 = 0.0
 
380
            tot_bal6 = 0.0
 
381
            tot_bal7 = 0.0
 
382
            tot_bal8 = 0.0
 
383
            tot_bal9 = 0.0
 
384
            tot_bal10 = 0.0
 
385
            tot_bal11 = 0.0
 
386
            tot_bal12 = 0.0
 
387
            tot_bal13 = 0.0
 
388
 
 
389
        else:
 
390
            
 
391
            ctx_init = _ctx_init(self.context.copy())
 
392
            ctx_end = _ctx_end(self.context.copy())
 
393
 
 
394
            tot_bin = 0.0
 
395
            tot_deb = 0.0
 
396
            tot_crd = 0.0
 
397
            tot_ytd = 0.0
 
398
            tot_eje = 0.0
 
399
        
 
400
        res = {}
 
401
        result_acc = []
 
402
        tot = {}        
 
403
        
 
404
 
 
405
        for aa_id in account_ids:
 
406
            id = aa_id[0]
 
407
 
 
408
            #
 
409
            # Check if we need to include this level
 
410
            #
 
411
            if not form['display_account_level'] or aa_id[3].level <= form['display_account_level']:
 
412
                res = {
 
413
                'id'        : id,
 
414
                'type'      : aa_id[3].type,
 
415
                'code'      : aa_id[3].code,
 
416
                'name'      : (aa_id[2] and not aa_id[1]) and 'TOTAL %s'%(aa_id[3].name.upper()) or aa_id[3].name,
 
417
                'parent_id' : aa_id[3].parent_id and aa_id[3].parent_id.id,
 
418
                'level'     : aa_id[3].level,
 
419
                'label'     : aa_id[1],
 
420
                'total'     : aa_id[2],
 
421
                'change_sign' : credit_account_ids and (id  in credit_account_ids and -1 or 1) or 1
 
422
                }
 
423
                
 
424
                if form['columns'] == 'qtr':
 
425
                    pn = 1
 
426
                    for p_id in p:
 
427
                        form['periods'] = p_id
 
428
                        
 
429
                        ctx_init = _ctx_init(self.context.copy())
 
430
                        aa_brw_init = account_obj.browse(self.cr, self.uid, id, ctx_init)
 
431
                        
 
432
                        ctx_end = _ctx_end(self.context.copy())
 
433
                        aa_brw_end  = account_obj.browse(self.cr, self.uid, id, ctx_end)
 
434
                        
 
435
                        if form['inf_type'] == 'IS':
 
436
                            d,c,b = map(z,[aa_brw_end.debit,aa_brw_end.credit,aa_brw_end.balance])
 
437
                            res.update({
 
438
                                'dbr%s'%pn: self.exchange(d),
 
439
                                'cdr%s'%pn: self.exchange(c),
 
440
                                'bal%s'%pn: self.exchange(b),
 
441
                            })
 
442
                        else:
 
443
                            i,d,c = map(z,[aa_brw_init.balance,aa_brw_end.debit,aa_brw_end.credit])
 
444
                            b = z(i+d-c)
 
445
                            res.update({
 
446
                                'dbr%s'%pn: self.exchange(d),
 
447
                                'cdr%s'%pn: self.exchange(c),
 
448
                                'bal%s'%pn: self.exchange(b),
 
449
                            })
 
450
                            
 
451
                        pn +=1
 
452
            
 
453
                    form['periods'] = period_ids
 
454
                    
 
455
                    ctx_init = _ctx_init(self.context.copy())
 
456
                    aa_brw_init = account_obj.browse(self.cr, self.uid, id, ctx_init)
 
457
                    
 
458
                    ctx_end = _ctx_end(self.context.copy())
 
459
                    aa_brw_end  = account_obj.browse(self.cr, self.uid, id, ctx_end)
 
460
                    
 
461
                    if form['inf_type'] == 'IS':
 
462
                        d,c,b = map(z,[aa_brw_end.debit,aa_brw_end.credit,aa_brw_end.balance])
 
463
                        res.update({
 
464
                            'dbr5': self.exchange(d),
 
465
                            'cdr5': self.exchange(c),
 
466
                            'bal5': self.exchange(b),
 
467
                        })
 
468
                    else:
 
469
                        i,d,c = map(z,[aa_brw_init.balance,aa_brw_end.debit,aa_brw_end.credit])
 
470
                        b = z(i+d-c)
 
471
                        res.update({
 
472
                            'dbr5': self.exchange(d),
 
473
                            'cdr5': self.exchange(c),
 
474
                            'bal5': self.exchange(b),
 
475
                        })
 
476
                
 
477
                elif form['columns'] == 'thirteen':
 
478
                    pn = 1
 
479
                    for p_id in period_ids:
 
480
                        form['periods'] = [p_id]
 
481
                        
 
482
                        ctx_init = _ctx_init(self.context.copy())
 
483
                        aa_brw_init = account_obj.browse(self.cr, self.uid, id, ctx_init)
 
484
                        
 
485
                        ctx_end = _ctx_end(self.context.copy())
 
486
                        aa_brw_end  = account_obj.browse(self.cr, self.uid, id, ctx_end)
 
487
                        
 
488
                        if form['inf_type'] == 'IS':
 
489
                            d,c,b = map(z,[aa_brw_end.debit,aa_brw_end.credit,aa_brw_end.balance])
 
490
                            res.update({
 
491
                                'dbr%s'%pn: self.exchange(d),
 
492
                                'cdr%s'%pn: self.exchange(c),
 
493
                                'bal%s'%pn: self.exchange(b),
 
494
                            })
 
495
                        else:
 
496
                            i,d,c = map(z,[aa_brw_init.balance,aa_brw_end.debit,aa_brw_end.credit])
 
497
                            b = z(i+d-c)
 
498
                            res.update({
 
499
                                'dbr%s'%pn: self.exchange(d),
 
500
                                'cdr%s'%pn: self.exchange(c),
 
501
                                'bal%s'%pn: self.exchange(b),
 
502
                            })
 
503
                            
 
504
                        pn +=1
 
505
            
 
506
                    form['periods'] = period_ids
 
507
                    
 
508
                    ctx_init = _ctx_init(self.context.copy())
 
509
                    aa_brw_init = account_obj.browse(self.cr, self.uid, id, ctx_init)
 
510
                    
 
511
                    ctx_end = _ctx_end(self.context.copy())
 
512
                    aa_brw_end  = account_obj.browse(self.cr, self.uid, id, ctx_end)
 
513
                    
 
514
                    if form['inf_type'] == 'IS':
 
515
                        d,c,b = map(z,[aa_brw_end.debit,aa_brw_end.credit,aa_brw_end.balance])
 
516
                        res.update({
 
517
                            'dbr13': self.exchange(d),
 
518
                            'cdr13': self.exchange(c),
 
519
                            'bal13': self.exchange(b),
 
520
                        })
 
521
                    else:
 
522
                        i,d,c = map(z,[aa_brw_init.balance,aa_brw_end.debit,aa_brw_end.credit])
 
523
                        b = z(i+d-c)
 
524
                        res.update({
 
525
                            'dbr13': self.exchange(d),
 
526
                            'cdr13': self.exchange(c),
 
527
                            'bal13': self.exchange(b),
 
528
                        })
 
529
                
 
530
                else:
 
531
 
 
532
                    aa_brw_init = account_obj.browse(self.cr, self.uid, id, ctx_init)
 
533
                    aa_brw_end  = account_obj.browse(self.cr, self.uid, id, ctx_end)
 
534
 
 
535
                    i,d,c = map(z,[aa_brw_init.balance,aa_brw_end.debit,aa_brw_end.credit])
 
536
                    b = z(i+d-c)
 
537
                    res.update({
 
538
                        'balanceinit': self.exchange(i),
 
539
                        'debit': self.exchange(d),
 
540
                        'credit': self.exchange(c),
 
541
                        'ytd': self.exchange(d-c),
 
542
                    })
 
543
                
 
544
                    if form['inf_type'] == 'IS' and  form['columns'] == 'one':
 
545
                        res.update({
 
546
                            'balance': self.exchange(d-c),
 
547
                        })
 
548
                    else:
 
549
                        res.update({
 
550
                            'balance': self.exchange(b),
 
551
                        })
 
552
 
 
553
                #
 
554
                # Check whether we must include this line in the report or not
 
555
                #
 
556
                to_include = False
 
557
                
 
558
                if form['columns'] in ('thirteen', 'qtr'):
 
559
                    to_test = [False]
 
560
                    if form['display_account'] == 'mov' and aa_id[3].parent_id:
 
561
                        # Include accounts with movements
 
562
                        for x in range(pn-1):
 
563
                            to_test.append(res.get('dbr%s'%x,0.0) >= 0.005 and True or False)
 
564
                            to_test.append(res.get('cdr%s'%x,0.0) >= 0.005 and True or False)
 
565
                        if any(to_test):
 
566
                            to_include = True
 
567
                        
 
568
                    elif form['display_account'] == 'bal' and aa_id[3].parent_id:
 
569
                        # Include accounts with balance
 
570
                        for x in range(pn-1):
 
571
                            to_test.append(res.get('bal%s'%x,0.0) >= 0.005 and True or False)
 
572
                        if any(to_test):
 
573
                            to_include = True
 
574
                            
 
575
                    elif form['display_account'] == 'bal_mov' and aa_id[3].parent_id:
 
576
                        # Include accounts with balance or movements
 
577
                        for x in range(pn-1):
 
578
                            to_test.append(res.get('bal%s'%x,0.0) >= 0.005 and True or False)
 
579
                            to_test.append(res.get('dbr%s'%x,0.0) >= 0.005 and True or False)
 
580
                            to_test.append(res.get('cdr%s'%x,0.0) >= 0.005 and True or False)
 
581
                        if any(to_test):
 
582
                            to_include = True
 
583
                    else:
 
584
                        # Include all accounts
 
585
                        to_include = True
 
586
                
 
587
                else:
 
588
 
 
589
                    if form['display_account'] == 'mov' and aa_id[3].parent_id:
 
590
                        # Include accounts with movements
 
591
                        if abs(d) >= 0.005 or abs(c) >= 0.005:
 
592
                            to_include = True
 
593
                    elif form['display_account'] == 'bal' and aa_id[3].parent_id:
 
594
                        # Include accounts with balance
 
595
                        if abs(b) >= 0.005:
 
596
                            to_include = True
 
597
                    elif form['display_account'] == 'bal_mov' and aa_id[3].parent_id:
 
598
                        # Include accounts with balance or movements
 
599
                        if abs(b) >= 0.005 or abs(d) >= 0.005 or abs(c) >= 0.005:
 
600
                            to_include = True
 
601
                    else:
 
602
                        # Include all accounts
 
603
                        to_include = True
 
604
                
 
605
                #~ ANALYTIC LEDGER
 
606
                if to_include and form['analytic_ledger'] and form['columns']=='four' and form['inf_type'] == 'BS' and res['type'] in ('other','liquidity','receivable','payable'):
 
607
                    res['mayor'] = self._get_analytic_ledger(res,ctx=ctx_end)
 
608
                else:
 
609
                    res['mayor'] = []
 
610
                
 
611
                
 
612
                if to_include:
 
613
                    result_acc.append(res)
 
614
                    #
 
615
                    # Check whether we must sumarize this line in the report or not
 
616
                    #
 
617
                    if form['tot_check'] and res['type'] == 'view' and res['level'] == 1 and (res['id'] not in tot):
 
618
 
 
619
                        if form['columns'] == 'qtr':
 
620
                            tot_check = True
 
621
                            tot[res['id']] = True
 
622
                            tot_bal1 += res.get('bal1',0.0)
 
623
                            tot_bal2 += res.get('bal2',0.0)
 
624
                            tot_bal3 += res.get('bal3',0.0)
 
625
                            tot_bal4 += res.get('bal4',0.0)
 
626
                            tot_bal5 += res.get('bal5',0.0)
 
627
 
 
628
                        elif form['columns'] == 'thirteen':
 
629
                            tot_check = True
 
630
                            tot[res['id']] = True
 
631
                            tot_bal1 += res.get('bal1',0.0)
 
632
                            tot_bal2 += res.get('bal2',0.0)
 
633
                            tot_bal3 += res.get('bal3',0.0)
 
634
                            tot_bal4 += res.get('bal4',0.0)
 
635
                            tot_bal5 += res.get('bal5',0.0)
 
636
                            tot_bal6 += res.get('bal6',0.0)
 
637
                            tot_bal7 += res.get('bal7',0.0)
 
638
                            tot_bal8 += res.get('bal8',0.0)
 
639
                            tot_bal9 += res.get('bal9',0.0)
 
640
                            tot_bal10 += res.get('bal10',0.0)
 
641
                            tot_bal11 += res.get('bal11',0.0)
 
642
                            tot_bal12 += res.get('bal12',0.0)
 
643
                            tot_bal13 += res.get('bal13',0.0)
 
644
 
 
645
                        else:
 
646
                            tot_check = True
 
647
                            tot[res['id']] = True
 
648
                            tot_bin += res['balanceinit']
 
649
                            tot_deb += res['debit']
 
650
                            tot_crd += res['credit']
 
651
                            tot_ytd += res['ytd']
 
652
                            tot_eje += res['balance']
 
653
 
 
654
        if tot_check:
 
655
            str_label = form['lab_str']
 
656
            res2 = {
 
657
                    'type' : 'view',
 
658
                    'name': 'TOTAL %s'%(str_label),
 
659
                    'label': False,
 
660
                    'total': True,
 
661
            }
 
662
            if form['columns'] == 'qtr':
 
663
                res2.update(dict(
 
664
                            bal1 = tot_bal1,
 
665
                            bal2 = tot_bal2,
 
666
                            bal3 = tot_bal3,
 
667
                            bal4 = tot_bal4,
 
668
                            bal5 = tot_bal5,))
 
669
            elif form['columns'] == 'thirteen':
 
670
                res2.update(dict(
 
671
                            bal1 = tot_bal1,
 
672
                            bal2 = tot_bal2,
 
673
                            bal3 = tot_bal3,
 
674
                            bal4 = tot_bal4,
 
675
                            bal5 = tot_bal5,
 
676
                            bal6 = tot_bal6,
 
677
                            bal7 = tot_bal7,
 
678
                            bal8 = tot_bal8,
 
679
                            bal9 = tot_bal9,
 
680
                            bal10 = tot_bal10,
 
681
                            bal11 = tot_bal11,
 
682
                            bal12 = tot_bal12,
 
683
                            bal13 = tot_bal13,))
 
684
 
 
685
            else:
 
686
                res2.update({
 
687
                        'balanceinit': tot_bin,
 
688
                        'debit': tot_deb,
 
689
                        'credit': tot_crd,
 
690
                        'ytd': tot_ytd,
 
691
                        'balance': tot_eje,
 
692
                })
 
693
                
 
694
            result_acc.append(res2)
 
695
        return result_acc
 
696
 
 
697
report_sxw.report_sxw('report.afr.1cols', 
 
698
                      'wizard.report', 
 
699
                      'account_financial_report/report/balance_full.rml',
 
700
                       parser=account_balance, 
 
701
                       header=False)
 
702
 
 
703
report_sxw.report_sxw('report.afr.2cols', 
 
704
                      'wizard.report', 
 
705
                      'account_financial_report/report/balance_full_2_cols.rml',
 
706
                       parser=account_balance, 
 
707
                       header=False)
 
708
 
 
709
report_sxw.report_sxw('report.afr.4cols', 
 
710
                      'wizard.report', 
 
711
                      'account_financial_report/report/balance_full_4_cols.rml',
 
712
                       parser=account_balance, 
 
713
                       header=False)
 
714
 
 
715
report_sxw.report_sxw('report.afr.analytic.ledger', 
 
716
                      'wizard.report', 
 
717
                      'account_financial_report/report/balance_full_4_cols_analytic_ledger.rml',
 
718
                       parser=account_balance, 
 
719
                       header=False)
 
720
                       
 
721
report_sxw.report_sxw('report.afr.5cols', 
 
722
                      'wizard.report', 
 
723
                      'account_financial_report/report/balance_full_5_cols.rml',
 
724
                       parser=account_balance, 
 
725
                       header=False)
 
726
                       
 
727
report_sxw.report_sxw('report.afr.qtrcols', 
 
728
                      'wizard.report', 
 
729
                      'account_financial_report/report/balance_full_qtr_cols.rml',
 
730
                       parser=account_balance, 
 
731
                       header=False)
 
732
 
 
733
report_sxw.report_sxw('report.afr.13cols', 
 
734
                      'wizard.report', 
 
735
                      'account_financial_report/report/balance_full_13_cols.rml',
 
736
                       parser=account_balance, 
 
737
                       header=False)