~camptocamp/account-financial-report/7.0-fix-1253659

« back to all changes in this revision

Viewing changes to account_financial_report/report/parser.py

[IMP] Linearization of computation for account_financial_report, necesary with big quantity of data from 90 minutes to 90 secs .... good Job team

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
from tools import config
36
36
from tools.translate import _
37
37
from osv import osv
 
38
import pdb
 
39
from openerp.tools.safe_eval import safe_eval as eval
38
40
 
39
41
 
40
42
class account_balance(report_sxw.rml_parse):
347
349
        ################################################################
348
350
        # Get the accounts                                             #
349
351
        ################################################################
 
352
        all_account_ids = _get_children_and_consol(
 
353
            self.cr, self.uid, account_ids, 100, self.context)
350
354
 
351
355
        account_ids = _get_children_and_consol(self.cr, self.uid, account_ids, form[
352
356
                                               'display_account_level'] and form['display_account_level'] or 100, self.context)
359
363
 
360
364
        credit_account_ids = list(set(
361
365
            credit_account_ids) - set(debit_account_ids))
362
 
 
363
366
        #
364
367
        # Generate the report lines (checking each account)
365
368
        #
388
391
                        p.append(l)
389
392
                        l = []
390
393
                        a = 0
391
 
 
392
 
            #~ period_ids = p
393
 
 
 
394
            tot_bal1 = 0.0
 
395
            tot_bal2 = 0.0
 
396
            tot_bal3 = 0.0
 
397
            tot_bal4 = 0.0
 
398
            tot_bal5 = 0.0
394
399
        elif form['columns'] == 'thirteen':
395
400
            period_ids = period_obj.search(self.cr, self.uid, [(
396
401
                'fiscalyear_id', '=', fiscalyear.id), ('special', '=', False)], order='date_start asc')
397
 
 
398
 
        if form['columns'] == 'qtr':
399
402
            tot_bal1 = 0.0
400
 
            tot_bal2 = 0.0
401
 
            tot_bal3 = 0.0
402
 
            tot_bal4 = 0.0
403
 
            tot_bal5 = 0.0
404
 
 
405
 
        elif form['columns'] == 'thirteen':
406
403
            tot_bal1 = 0.0
407
404
            tot_bal2 = 0.0
408
405
            tot_bal3 = 0.0
416
413
            tot_bal11 = 0.0
417
414
            tot_bal12 = 0.0
418
415
            tot_bal13 = 0.0
419
 
 
420
416
        else:
421
 
 
422
 
            ctx_init = _ctx_init(self.context.copy())
423
417
            ctx_end = _ctx_end(self.context.copy())
424
 
 
425
418
            tot_bin = 0.0
426
419
            tot_deb = 0.0
427
420
            tot_crd = 0.0
432
425
        result_acc = []
433
426
        tot = {}
434
427
 
 
428
        ###############################################################
 
429
        # Calculations of credit, debit and balance, 
 
430
        # without repeating operations.
 
431
        ###############################################################
 
432
 
 
433
        account_black_ids = account_obj.search(self.cr, self.uid, (
 
434
                                               [('id', 'in', [i[0] for i in all_account_ids]),
 
435
                                                ('type', 'not in',
 
436
                                                ('view', 'consolidation'))]))
 
437
 
 
438
        account_not_black_ids = account_obj.search(self.cr, self.uid, ([('id', 'in', [
 
439
                                                   i[0] for i in all_account_ids]), ('type', 'in', ('view', 'consolidation'))]))
 
440
 
 
441
        # This could be done quickly with a sql sentence
 
442
        account_not_black = account_obj.browse(
 
443
            self.cr, self.uid, account_not_black_ids)
 
444
        account_not_black.sort(key=lambda x: x.level)
 
445
        account_not_black.reverse()
 
446
        account_not_black_ids = [i.id for i in account_not_black]
 
447
 
 
448
        all_account_period = {}  # All accounts per period 
 
449
        
 
450
        # Iteration limit depending on the number of columns
 
451
        if form['columns'] == 'thirteen':
 
452
            limit = 13
 
453
        elif form['columns'] == 'qtr':
 
454
            limit = 5
 
455
        else:
 
456
            limit = 1
 
457
 
 
458
        for p_act in range(limit):
 
459
            if limit != 1:
 
460
                if p_act == limit-1:
 
461
                    form['periods'] = period_ids
 
462
                else:
 
463
                    if form['columns'] == 'thirteen':
 
464
                        form['periods'] = [period_ids[p_act]]
 
465
                    elif form['columns'] == 'qtr':
 
466
                        form['periods'] = p[p_act]
 
467
 
 
468
            if form['inf_type'] == 'IS':
 
469
                ctx_to_use = _ctx_end(self.context.copy())
 
470
            else:
 
471
                ctx_i = _ctx_init(self.context.copy())
 
472
                ctx_to_use = _ctx_end(self.context.copy())
 
473
 
 
474
            account_black = account_obj.browse(
 
475
                self.cr, self.uid, account_black_ids, ctx_to_use)
 
476
 
 
477
            if form['inf_type'] == 'BS':
 
478
                account_black_init = account_obj.browse(
 
479
                    self.cr, self.uid, account_black_ids, ctx_i)
 
480
 
 
481
            #~ Black
 
482
            dict_black = {}
 
483
            for i in account_black:
 
484
                d = i.debit
 
485
                c = i.credit
 
486
                dict_black[i.id] = {
 
487
                    'obj': i,
 
488
                    'debit': d,
 
489
                    'credit': c,
 
490
                    'balance': d-c
 
491
                }
 
492
                if form['inf_type'] == 'BS':
 
493
                    dict_black.get(i.id)['balanceinit'] = 0.0
 
494
 
 
495
            # If the report is a balance sheet
 
496
            # Balanceinit values are added to the dictionary
 
497
            if form['inf_type'] == 'BS':
 
498
                for i in account_black_init:
 
499
                    dict_black.get(i.id)['balanceinit'] = i.balance
 
500
 
 
501
            #~ Not black
 
502
            dict_not_black = {}
 
503
            for i in account_not_black:
 
504
                dict_not_black[i.id] = {
 
505
                    'obj': i, 'debit': 0.0, 'credit': 0.0, 'balance': 0.0}
 
506
                if form['inf_type'] == 'BS':
 
507
                    dict_not_black.get(i.id)['balanceinit'] = 0.0
 
508
 
 
509
            all_account = dict_black.copy(
 
510
            )  #It makes a copy because they modify 
 
511
 
 
512
            for acc_id in account_not_black_ids:
 
513
                acc_childs = dict_not_black.get(acc_id).get('obj').child_id
 
514
                for child_id in acc_childs:
 
515
                    dict_not_black.get(acc_id)['debit'] += all_account.get(
 
516
                        child_id.id).get('debit')
 
517
                    dict_not_black.get(acc_id)['credit'] += all_account.get(
 
518
                        child_id.id).get('credit')
 
519
                    dict_not_black.get(acc_id)['balance'] += all_account.get(
 
520
                        child_id.id).get('balance')
 
521
                    if form['inf_type'] == 'BS':
 
522
                        dict_not_black.get(acc_id)['balanceinit'] += all_account.get(
 
523
                            child_id.id).get('balanceinit')
 
524
                all_account[acc_id] = dict_not_black[acc_id]
 
525
 
 
526
            if p_act == limit-1:
 
527
                all_account_period['all'] = all_account
 
528
            else:
 
529
                if form['columns'] == 'thirteen':
 
530
                    all_account_period[p_act] = all_account
 
531
                elif form['columns'] == 'qtr':
 
532
                    all_account_period[p_act] = all_account
 
533
 
 
534
        ###############################################################
 
535
        # End of the calculations of credit, debit and balance
 
536
        #
 
537
        ###############################################################
 
538
 
435
539
        for aa_id in account_ids:
436
540
            id = aa_id[0]
437
541
 
452
556
                }
453
557
 
454
558
                if form['columns'] == 'qtr':
455
 
                    pn = 1
456
 
                    for p_id in p:
457
 
                        form['periods'] = p_id
458
 
 
459
 
                        ctx_init = _ctx_init(self.context.copy())
460
 
                        aa_brw_init = account_obj.browse(
461
 
                            self.cr, self.uid, id, ctx_init)
462
 
 
463
 
                        ctx_end = _ctx_end(self.context.copy())
464
 
                        aa_brw_end = account_obj.browse(
465
 
                            self.cr, self.uid, id, ctx_end)
 
559
                    for pn in range(1, 5):
466
560
 
467
561
                        if form['inf_type'] == 'IS':
468
562
                            d, c, b = map(z, [
469
 
                                          aa_brw_end.debit, aa_brw_end.credit, aa_brw_end.balance])
 
563
                                          all_account_period.get(pn-1).get(id).get('debit', 0.0), all_account_period.get(pn-1).get(id).get('credit', 0.0), all_account_period.get(pn-1).get(id).get('balance', 0.0)])
470
564
                            res.update({
471
565
                                'dbr%s' % pn: self.exchange(d),
472
566
                                'cdr%s' % pn: self.exchange(c),
474
568
                            })
475
569
                        else:
476
570
                            i, d, c = map(z, [
477
 
                                          aa_brw_init.balance, aa_brw_end.debit, aa_brw_end.credit])
 
571
                                          all_account_period.get(pn-1).get(id).get('balanceinit', 0.0), all_account_period.get(pn-1).get(id).get('debit', 0.0), all_account_period.get(pn-1).get(id).get('credit', 0.0)])
478
572
                            b = z(i+d-c)
479
573
                            res.update({
480
574
                                'dbr%s' % pn: self.exchange(d),
482
576
                                'bal%s' % pn: self.exchange(b),
483
577
                            })
484
578
 
485
 
                        pn += 1
486
 
 
487
 
                    form['periods'] = period_ids
488
 
 
489
 
                    ctx_init = _ctx_init(self.context.copy())
490
 
                    aa_brw_init = account_obj.browse(
491
 
                        self.cr, self.uid, id, ctx_init)
492
 
 
493
 
                    ctx_end = _ctx_end(self.context.copy())
494
 
                    aa_brw_end = account_obj.browse(
495
 
                        self.cr, self.uid, id, ctx_end)
496
 
 
497
579
                    if form['inf_type'] == 'IS':
498
580
                        d, c, b = map(z, [
499
 
                                      aa_brw_end.debit, aa_brw_end.credit, aa_brw_end.balance])
 
581
                                      all_account_period.get('all').get(id).get('debit', 0.0), all_account_period.get('all').get(id).get('credit',0.0), all_account_period.get('all').get(id).get('balance')])
500
582
                        res.update({
501
583
                            'dbr5': self.exchange(d),
502
584
                            'cdr5': self.exchange(c),
504
586
                        })
505
587
                    else:
506
588
                        i, d, c = map(z, [
507
 
                                      aa_brw_init.balance, aa_brw_end.debit, aa_brw_end.credit])
 
589
                                      all_account_period.get('all').get(id).get('balanceinit', 0.0), all_account_period.get('all').get(id).get('debit', 0.0), all_account_period.get('all').get(id).get('credit',0.0)])
508
590
                        b = z(i+d-c)
509
591
                        res.update({
510
592
                            'dbr5': self.exchange(d),
514
596
 
515
597
                elif form['columns'] == 'thirteen':
516
598
                    pn = 1
517
 
                    for p_id in period_ids:
518
 
                        form['periods'] = [p_id]
519
 
 
520
 
                        ctx_init = _ctx_init(self.context.copy())
521
 
                        aa_brw_init = account_obj.browse(
522
 
                            self.cr, self.uid, id, ctx_init)
523
 
 
524
 
                        ctx_end = _ctx_end(self.context.copy())
525
 
                        aa_brw_end = account_obj.browse(
526
 
                            self.cr, self.uid, id, ctx_end)
 
599
                    for p_num in range(12):
527
600
 
528
601
                        if form['inf_type'] == 'IS':
529
602
                            d, c, b = map(z, [
530
 
                                          aa_brw_end.debit, aa_brw_end.credit, aa_brw_end.balance])
 
603
                                          all_account_period.get(p_num).get(id).get('debit', 0.0), all_account_period.get(p_num).get(id).get('credit', 0.0), all_account_period.get(p_num).get(id).get('balance', 0.0)])
531
604
                            res.update({
532
605
                                'dbr%s' % pn: self.exchange(d),
533
606
                                'cdr%s' % pn: self.exchange(c),
535
608
                            })
536
609
                        else:
537
610
                            i, d, c = map(z, [
538
 
                                          aa_brw_init.balance, aa_brw_end.debit, aa_brw_end.credit])
 
611
                                          all_account_period.get(p_num).get(id).get('balanceinit', 0.0), all_account_period.get(p_num).get(id).get('debit', 0.0), all_account_period.get(p_num).get(id).get('credit', 0.0)])
539
612
                            b = z(i+d-c)
540
613
                            res.update({
541
614
                                'dbr%s' % pn: self.exchange(d),
545
618
 
546
619
                        pn += 1
547
620
 
548
 
                    form['periods'] = period_ids
549
 
 
550
 
                    ctx_init = _ctx_init(self.context.copy())
551
 
                    aa_brw_init = account_obj.browse(
552
 
                        self.cr, self.uid, id, ctx_init)
553
 
 
554
 
                    ctx_end = _ctx_end(self.context.copy())
555
 
                    aa_brw_end = account_obj.browse(
556
 
                        self.cr, self.uid, id, ctx_end)
557
 
 
558
621
                    if form['inf_type'] == 'IS':
559
622
                        d, c, b = map(z, [
560
 
                                      aa_brw_end.debit, aa_brw_end.credit, aa_brw_end.balance])
 
623
                                      all_account_period.get('all').get(id).get('debit', 0.0), all_account_period.get('all').get(id).get('credit', 0.0), all_account_period.get('all').get(id).get('balance', 0.0)])
561
624
                        res.update({
562
625
                            'dbr13': self.exchange(d),
563
626
                            'cdr13': self.exchange(c),
565
628
                        })
566
629
                    else:
567
630
                        i, d, c = map(z, [
568
 
                                      aa_brw_init.balance, aa_brw_end.debit, aa_brw_end.credit])
 
631
                                      all_account_period.get('all').get(id).get('balanceinit', 0.0), all_account_period.get('all').get(id).get('debit', 0.0), all_account_period.get('all').get(id).get('credit', 0.0)])
569
632
                        b = z(i+d-c)
570
633
                        res.update({
571
634
                            'dbr13': self.exchange(d),
574
637
                        })
575
638
 
576
639
                else:
577
 
 
578
 
                    aa_brw_init = account_obj.browse(
579
 
                        self.cr, self.uid, id, ctx_init)
580
 
                    aa_brw_end = account_obj.browse(
581
 
                        self.cr, self.uid, id, ctx_end)
582
 
 
583
640
                    i, d, c = map(z, [
584
 
                                  aa_brw_init.balance, aa_brw_end.debit, aa_brw_end.credit])
 
641
                                  all_account_period.get('all').get(id).get('balanceinit', 0.0), all_account_period.get('all').get(id).get('debit', 0.0), all_account_period.get('all').get(id).get('credit', 0.0)])
585
642
                    b = z(i+d-c)
586
643
                    res.update({
587
644
                        'balanceinit': self.exchange(i),
695
752
                            tot_bal11 += res.get('bal11', 0.0)
696
753
                            tot_bal12 += res.get('bal12', 0.0)
697
754
                            tot_bal13 += res.get('bal13', 0.0)
698
 
 
699
755
                        else:
700
756
                            tot_check = True
701
757
                            #~ tot[res['id']] = True
715
771
            }
716
772
            if form['columns'] == 'qtr':
717
773
                res2.update(dict(
718
 
                            bal1=tot_bal1,
719
 
                            bal2=tot_bal2,
720
 
                            bal3=tot_bal3,
721
 
                            bal4=tot_bal4,
722
 
                            bal5=tot_bal5,))
 
774
                            bal1=z(tot_bal1),
 
775
                            bal2=z(tot_bal2),
 
776
                            bal3=z(tot_bal3),
 
777
                            bal4=z(tot_bal4),
 
778
                            bal5=z(tot_bal5),))
723
779
            elif form['columns'] == 'thirteen':
724
780
                res2.update(dict(
725
 
                            bal1=tot_bal1,
726
 
                            bal2=tot_bal2,
727
 
                            bal3=tot_bal3,
728
 
                            bal4=tot_bal4,
729
 
                            bal5=tot_bal5,
730
 
                            bal6=tot_bal6,
731
 
                            bal7=tot_bal7,
732
 
                            bal8=tot_bal8,
733
 
                            bal9=tot_bal9,
734
 
                            bal10=tot_bal10,
735
 
                            bal11=tot_bal11,
736
 
                            bal12=tot_bal12,
737
 
                            bal13=tot_bal13,))
 
781
                            bal1=z(tot_bal1),
 
782
                            bal2=z(tot_bal2),
 
783
                            bal3=z(tot_bal3),
 
784
                            bal4=z(tot_bal4),
 
785
                            bal5=z(tot_bal5),
 
786
                            bal6=z(tot_bal6),
 
787
                            bal7=z(tot_bal7),
 
788
                            bal8=z(tot_bal8),
 
789
                            bal9=z(tot_bal9),
 
790
                            bal10=z(tot_bal10),
 
791
                            bal11=z(tot_bal11),
 
792
                            bal12=z(tot_bal12),
 
793
                            bal13=z(tot_bal13),))
738
794
 
739
795
            else:
740
796
                res2.update({