~camptocamp/c2c-rd-addons/trunk

« back to all changes in this revision

Viewing changes to account_financial_report/report/account_chart.py

  • Committer: office at chricar
  • Date: 2011-09-18 18:37:34 UTC
  • Revision ID: office@chricar-20110918183734-9fyv3zyp9d012bdg
[ADD] account_financial_report see README

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
5
#    Copyright (c) 2009 Zikzakmedia S.L. (http://zikzakmedia.com) All Rights Reserved.
 
6
#                       Jordi Esteve <jesteve@zikzakmedia.com>
 
7
#
 
8
# WARNING: This program as such is intended to be used by professional
 
9
# programmers who take the whole responsability of assessing all potential
 
10
# consequences resulting from its eventual inadequacies and bugs
 
11
# End users who are looking for a ready-to-use solution with commercial
 
12
# garantees and support are strongly adviced to contract a Free Software
 
13
# Service Company
 
14
#
 
15
# This program is Free Software; you can redistribute it and/or
 
16
# modify it under the terms of the GNU General Public License
 
17
# as published by the Free Software Foundation; either version 2
 
18
# of the License, or (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, write to the Free Software
 
27
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
28
#
 
29
##############################################################################
 
30
 
 
31
import time
 
32
from report import report_sxw
 
33
import xml
 
34
import rml_parse
 
35
import copy
 
36
from operator import itemgetter
 
37
 
 
38
class account_chart(rml_parse.rml_parse):
 
39
    _name = 'report.account.account.chart.report'
 
40
    gadr_cpt = 0
 
41
 
 
42
    def __init__(self, cr, uid, name, context):
 
43
        super(account_chart, self).__init__(cr, uid, name, context)
 
44
        self.localcontext.update({
 
45
            'time': time,
 
46
            'lines': self.lines,
 
47
        })
 
48
        self.context = context
 
49
 
 
50
 
 
51
    def set_context(self, objects, data, ids, report_type = None):
 
52
        new_ids = []
 
53
        if (data['model'] == 'account.account'):
 
54
            new_ids = ids
 
55
        else:
 
56
            new_ids.append(data['form']['account'])
 
57
            objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
 
58
            
 
59
        super(account_chart, self).set_context(objects, data, new_ids, report_type)    
 
60
 
 
61
 
 
62
    def lines(self,ids={}, done=None, level=1):
 
63
        retour_unsort = self.get_result(ids,done)
 
64
        return self.sort_result(retour_unsort,31)
 
65
 
 
66
 
 
67
    def sort_result(self,account,rupture):
 
68
        max_level = self.max_level_search(account)
 
69
        if not account :
 
70
            return []
 
71
        ind_type = 0
 
72
        ind_rup = 0
 
73
        account_result = []
 
74
        while ind_type < len(account):
 
75
            account_elem = account[ind_type]
 
76
            res_sort = {
 
77
                'code' : account_elem['code'],
 
78
                'name':  account_elem['name'],
 
79
                'name_dr': '',
 
80
                'type_gd' : 1,
 
81
                'type_dr' : 1
 
82
            }
 
83
            if account_elem['level'] < max_level:
 
84
                res_sort['type_gd'] = 2;
 
85
                res_sort['name_gd'] = account_elem['name']
 
86
#            ind_type_dr = ind_type + rupture;
 
87
#            if (ind_type_dr >= len(account)):
 
88
#                account_result.append(res_sort)
 
89
#                ind_rup+=1
 
90
#                ind_type+=1
 
91
#                continue
 
92
#            account_elem = account[ind_type_dr]
 
93
#            res_sort['name_dr'] = account_elem['code']+ ' ' + account_elem['name']
 
94
#            if account_elem['level'] < 5:
 
95
#                res_sort['type_dr'] = 2
 
96
#                res_sort['name_dr'] = account_elem['name']
 
97
#                
 
98
#            print ind_type_dr
 
99
            account_result.append(res_sort)
 
100
            ind_rup+=1
 
101
            ind_type+=1
 
102
            # si on a remplis une page on fait la rupture de la page
 
103
#            if (ind_rup >= rupture):
 
104
#                # On multiplie la rupture par 2 pour repositionner le tableau correctement
 
105
#                ind_rup = 0 
 
106
#                ind_type = ind_type + rupture
 
107
            
 
108
        return account_result
 
109
 
 
110
 
 
111
    def get_result(self,ids={},done=None,level=1):
 
112
        if not ids:
 
113
            ids = self.ids
 
114
        if not ids:
 
115
            return []
 
116
        if not done:
 
117
            done={}
 
118
        result = []
 
119
        accounts = self.pool.get('account.account').browse(self.cr, self.uid, ids,self.context)
 
120
        def cmp_code(x, y):
 
121
            return cmp(x.code, y.code)
 
122
        accounts.sort(cmp_code)
 
123
        for account in accounts:
 
124
            if account.id in done:
 
125
                continue
 
126
            done[account.id] = 1
 
127
            res = {
 
128
                'code': account.code,
 
129
                'name' : account.name,
 
130
                'type': 1,
 
131
                'level': level
 
132
            }
 
133
            result.append(res)
 
134
            if account.child_id:
 
135
                ids2 = [(x.code,x.id) for x in account.child_id]
 
136
                ids2.sort()
 
137
                result += self.get_result([x[1] for x in ids2], done,level+1)
 
138
        return result
 
139
 
 
140
    def max_level_search(self,account_list):
 
141
        result = sorted(account_list, key=itemgetter('level'))
 
142
        if (result):
 
143
            return result[len(result)-1]['level']
 
144
        else:
 
145
            return 0
 
146
 
 
147
 
 
148
report_sxw.report_sxw('report.account.account.chart.report', 'account.account','addons/account_financial_report/report/account_chart.rml', parser=account_chart, header=False)
 
149
 
 
150