~therp-nl/therp-addons/7.0_lp1219418

« back to all changes in this revision

Viewing changes to account_report_alt/report/account_balance_sheet.py

  • Committer: Ronald Portier
  • Date: 2014-03-28 13:12:43 UTC
  • mfrom: (81.5.17 therp-addons-7.0)
  • Revision ID: ronald@therp.nl-20140328131243-d06yj7u2o9fhshrw
[MERGE] Merge upstream changes
    - resolve text conflict in fetchmail_invoice.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- encoding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6
 
#
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.
11
 
#
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.
16
 
#
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/>.
19
 
#
20
 
##############################################################################
21
 
 
22
 
import time
23
 
 
24
 
import pooler
25
 
from report import report_sxw
26
 
from openerp.addons.account_report_alt.report import account_profit_loss
27
 
from common_report_header import common_report_header
28
 
from tools.translate import _
29
 
 
30
 
class report_balancesheet_horizontal(report_sxw.rml_parse, common_report_header):
31
 
    def __init__(self, cr, uid, name, context=None):
32
 
        super(report_balancesheet_horizontal, self).__init__(cr, uid, name, context=context)
33
 
        self.obj_pl = account_profit_loss.report_pl_account_horizontal(cr, uid, name, context=context)
34
 
        self.result_sum_dr = 0.0
35
 
        self.result_sum_cr = 0.0
36
 
        self.result = {}
37
 
        self.res_bl = {}
38
 
        self.result_temp = []
39
 
        self.localcontext.update({
40
 
            'time': time,
41
 
            'get_lines': self.get_lines,
42
 
            'get_lines_another': self.get_lines_another,
43
 
            'get_company': self._get_company,
44
 
            'get_currency': self._get_currency,
45
 
            'sum_dr': self.sum_dr,
46
 
            'sum_cr': self.sum_cr,
47
 
            'get_data':self.get_data,
48
 
            'get_pl_balance':self.get_pl_balance,
49
 
            'get_fiscalyear': self._get_fiscalyear,
50
 
            'get_account': self._get_account,
51
 
            'get_start_period': self.get_start_period,
52
 
            'get_end_period': self.get_end_period,
53
 
            'get_sortby': self._get_sortby,
54
 
            'get_filter': self._get_filter,
55
 
            'get_journal': self._get_journal,
56
 
            'get_start_date':self._get_start_date,
57
 
            'get_end_date':self._get_end_date,
58
 
            'get_company':self._get_company,
59
 
            'get_target_move': self._get_target_move,
60
 
        })
61
 
        self.context = context
62
 
 
63
 
    def set_context(self, objects, data, ids, report_type=None):
64
 
        new_ids = ids
65
 
        if (data['model'] == 'ir.ui.menu'):
66
 
            new_ids = 'chart_account_id' in data['form'] and data['form']['chart_account_id'] and [data['form']['chart_account_id'][0]] or []
67
 
            objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
68
 
            lang_dict = self.pool.get('res.users').read(self.cr,self.uid,self.uid,['context_lang'])
69
 
            data['lang'] = lang_dict.get('context_lang') or False
70
 
        return super(report_balancesheet_horizontal, self).set_context(objects, data, new_ids, report_type=report_type)
71
 
 
72
 
    def sum_dr(self):
73
 
        if self.res_bl['type'] == _('Net Profit'):
74
 
            self.result_sum_dr += self.res_bl['balance']
75
 
        return self.result_sum_dr
76
 
 
77
 
    def sum_cr(self):
78
 
        if self.res_bl['type'] == _('Net Loss'):
79
 
            self.result_sum_cr += self.res_bl['balance']
80
 
        return self.result_sum_cr
81
 
 
82
 
    def get_pl_balance(self):
83
 
        return self.res_bl
84
 
 
85
 
    def get_data(self,data):
86
 
        cr, uid = self.cr, self.uid
87
 
        db_pool = pooler.get_pool(self.cr.dbname)
88
 
 
89
 
        #Getting Profit or Loss Balance from profit and Loss report
90
 
        self.obj_pl.get_data(data)
91
 
        self.res_bl = self.obj_pl.final_result()
92
 
 
93
 
        account_pool = db_pool.get('account.account')
94
 
        currency_pool = db_pool.get('res.currency')
95
 
 
96
 
        types = [
97
 
            'liability',
98
 
            'asset'
99
 
        ]
100
 
 
101
 
        ctx = self.context.copy()
102
 
        ctx['fiscalyear'] = data['form'].get('fiscalyear_id', False)
103
 
        if ctx['fiscalyear']:
104
 
            ctx['fiscalyear'] = ctx['fiscalyear'][0]
105
 
 
106
 
        if data['form']['filter'] == 'filter_period':
107
 
            ctx['periods'] = data['form'].get('periods', False)
108
 
        elif data['form']['filter'] == 'filter_date':
109
 
            ctx['date_from'] = data['form'].get('date_from', False)
110
 
            ctx['date_to'] =  data['form'].get('date_to', False)
111
 
        ctx['state'] = data['form'].get('target_move', 'all')
112
 
        cal_list = {}
113
 
        pl_dict = {}
114
 
        account_dict = {}
115
 
        account_id = data['form'].get('chart_account_id', False)
116
 
        if account_id:
117
 
            account_id = account_id[0]
118
 
        account_ids = account_pool._get_children_and_consol(cr, uid, account_id, context=ctx)
119
 
        accounts = account_pool.browse(cr, uid, account_ids, context=ctx)
120
 
 
121
 
        if not self.res_bl:
122
 
            self.res_bl['type'] = _('Net Profit')
123
 
            self.res_bl['balance'] = 0.0
124
 
 
125
 
        if self.res_bl['type'] == _('Net Profit'):
126
 
            self.res_bl['type'] = _('Net Profit')
127
 
        else:
128
 
            self.res_bl['type'] = _('Net Loss')
129
 
        pl_dict  = {
130
 
            'code': self.res_bl['type'],
131
 
            'name': self.res_bl['type'],
132
 
            'level': False,
133
 
            'balance':self.res_bl['balance'],
134
 
        }
135
 
        for typ in types:
136
 
            accounts_temp = []
137
 
            for account in accounts:
138
 
                if (account.user_type.report_type) and (account.user_type.report_type == typ):
139
 
                    account_dict = {
140
 
                        'id': account.id,
141
 
                        'code': account.code,
142
 
                        'name': account.name,
143
 
                        'level': account.level,
144
 
                        'balance': (account.balance and typ == 'liability' and -1 or 1 ) * account.balance,
145
 
                    }
146
 
                    currency = account.currency_id and account.currency_id or account.company_id.currency_id
147
 
                    if typ == 'liability' and account.type <> 'view' and (account.debit <> account.credit):
148
 
                        self.result_sum_dr += account_dict['balance']
149
 
                    if typ == 'asset' and account.type <> 'view' and (account.debit <> account.credit):
150
 
                        self.result_sum_cr += account_dict['balance']
151
 
                    if data['form']['display_account'] == 'bal_movement':
152
 
                        if (not currency_pool.is_zero(self.cr, self.uid, currency, account.credit)) or (not currency_pool.is_zero(self.cr, self.uid, currency, account.debit)) or (not currency_pool.is_zero(self.cr, self.uid, currency, account.balance)):
153
 
                            accounts_temp.append(account_dict)
154
 
                    elif data['form']['display_account'] == 'bal_solde':
155
 
                        if not currency_pool.is_zero(self.cr, self.uid, currency, account.balance):
156
 
                            accounts_temp.append(account_dict)
157
 
                    else:
158
 
                        accounts_temp.append(account_dict)
159
 
 
160
 
            self.result[typ] = accounts_temp
161
 
            cal_list[typ]=self.result[typ]
162
 
 
163
 
        if pl_dict['code'] == _('Net Loss'):
164
 
            self.result['asset'].append(pl_dict)
165
 
        else:
166
 
            self.result['liability'].append(pl_dict)
167
 
 
168
 
        if cal_list:
169
 
            temp = {}
170
 
            for i in range(0,max(len(cal_list['liability']),len(cal_list['asset']))):
171
 
                if i < len(cal_list['liability']) and i < len(cal_list['asset']):
172
 
                    temp={
173
 
                          'code': cal_list['liability'][i]['code'],
174
 
                          'name': cal_list['liability'][i]['name'],
175
 
                          'level': cal_list['liability'][i]['level'],
176
 
                          'balance':cal_list['liability'][i]['balance'],
177
 
                          'code1': cal_list['asset'][i]['code'],
178
 
                          'name1': cal_list['asset'][i]['name'],
179
 
                          'level1': cal_list['asset'][i]['level'],
180
 
                          'balance1':cal_list['asset'][i]['balance'],
181
 
                          }
182
 
                    self.result_temp.append(temp)
183
 
                else:
184
 
                    if i < len(cal_list['asset']):
185
 
                        temp={
186
 
                              'code': '',
187
 
                              'name': '',
188
 
                              'level': False,
189
 
                              'balance':False,
190
 
                              'code1': cal_list['asset'][i]['code'],
191
 
                              'name1': cal_list['asset'][i]['name'],
192
 
                              'level1': cal_list['asset'][i]['level'],
193
 
                              'balance1':cal_list['asset'][i]['balance'],
194
 
                          }
195
 
                        self.result_temp.append(temp)
196
 
                    if  i < len(cal_list['liability']):
197
 
                        temp={
198
 
                              'code': cal_list['liability'][i]['code'],
199
 
                              'name': cal_list['liability'][i]['name'],
200
 
                              'level': cal_list['liability'][i]['level'],
201
 
                              'balance':cal_list['liability'][i]['balance'],
202
 
                              'code1': '',
203
 
                              'name1': '',
204
 
                              'level1': False,
205
 
                              'balance1':False,
206
 
                          }
207
 
                        self.result_temp.append(temp)
208
 
        return None
209
 
 
210
 
    def get_lines(self):
211
 
        return self.result_temp
212
 
 
213
 
    def get_lines_another(self, group):
214
 
        return self.result.get(group, [])
215
 
 
216
 
report_sxw.report_sxw('report.account.balancesheet.horizontal', 'account.account',
217
 
    'addons/account_report_alt/report/account_balance_sheet_horizontal.rml',parser=report_balancesheet_horizontal,
218
 
    header='internal landscape')
219
 
 
220
 
report_sxw.report_sxw('report.account.balancesheet', 'account.account',
221
 
    'addons/account_report_alt/report/account_balance_sheet.rml',parser=report_balancesheet_horizontal,
222
 
    header='internal')
223
 
 
224
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: