~therp-nl/therp-addons/7.0_lp1219418

« back to all changes in this revision

Viewing changes to account_report_alt/report/account_profit_loss.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
 
##############################################################################
2
 
#
3
 
#    OpenERP, Open Source Management Solution
4
 
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
5
 
#
6
 
#    This program is free software: you can redistribute it and/or modify
7
 
#    it under the terms of the GNU Affero General Public License as
8
 
#    published by the Free Software Foundation, either version 3 of the
9
 
#    License, or (at your option) any later version.
10
 
#
11
 
#    This program is distributed in the hope that it will be useful,
12
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#    GNU Affero General Public License for more details.
15
 
#
16
 
#    You should have received a copy of the GNU Affero General Public License
17
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
#
19
 
##############################################################################
20
 
 
21
 
import time
22
 
import pooler
23
 
from report import report_sxw
24
 
from common_report_header import common_report_header
25
 
from tools.translate import _
26
 
 
27
 
class report_pl_account_horizontal(report_sxw.rml_parse, common_report_header):
28
 
 
29
 
    def __init__(self, cr, uid, name, context=None):
30
 
        super(report_pl_account_horizontal, self).__init__(cr, uid, name, context=context)
31
 
        self.result_sum_dr = 0.0
32
 
        self.result_sum_cr = 0.0
33
 
        self.res_pl = {}
34
 
        self.result = {}
35
 
        self.result_temp = []
36
 
        self.localcontext.update( {
37
 
            'time': time,
38
 
            'get_lines': self.get_lines,
39
 
            'get_lines_another': self.get_lines_another,
40
 
            'get_currency': self._get_currency,
41
 
            'get_data': self.get_data,
42
 
            'sum_dr': self.sum_dr,
43
 
            'sum_cr': self.sum_cr,
44
 
            'final_result': self.final_result,
45
 
            'get_fiscalyear': self._get_fiscalyear,
46
 
            'get_account': self._get_account,
47
 
            'get_start_period': self.get_start_period,
48
 
            'get_end_period': self.get_end_period,
49
 
            'get_sortby': self._get_sortby,
50
 
            'get_filter': self._get_filter,
51
 
            'get_journal': self._get_journal,
52
 
            'get_start_date':self._get_start_date,
53
 
            'get_end_date':self._get_end_date,
54
 
            'get_company':self._get_company,
55
 
            'get_target_move': self._get_target_move,
56
 
            'get_trans':self._get_trans
57
 
        })
58
 
        self.context = context
59
 
 
60
 
    def set_context(self, objects, data, ids, report_type=None):
61
 
        new_ids = ids
62
 
        if (data['model'] == 'ir.ui.menu'):
63
 
            new_ids = 'chart_account_id' in data['form'] and data['form']['chart_account_id'] and [data['form']['chart_account_id'][0]] or []
64
 
            objects = self.pool.get('account.account').browse(self.cr, self.uid, new_ids)
65
 
            lang_dict = self.pool.get('res.users').read(self.cr,self.uid,self.uid,['context_lang'])
66
 
            data['lang'] = lang_dict.get('context_lang') or False
67
 
        return super(report_pl_account_horizontal, self).set_context(objects, data, new_ids, report_type=report_type)
68
 
 
69
 
 
70
 
    def final_result(self):
71
 
        return self.res_pl
72
 
 
73
 
    def sum_dr(self):
74
 
        if self.res_pl['type'] == _('Net Profit'):
75
 
            self.result_sum_dr += self.res_pl['balance']
76
 
        return self.result_sum_dr
77
 
 
78
 
    def sum_cr(self):
79
 
        if self.res_pl['type'] == _('Net Loss'):
80
 
            self.result_sum_cr += self.res_pl['balance']
81
 
        return self.result_sum_cr
82
 
 
83
 
    def _get_trans(self, source):
84
 
        return _(source)
85
 
    
86
 
    def get_data(self, data):
87
 
        def get_account_repr(account, account_type):
88
 
            return {
89
 
                'code': account.code,
90
 
                'name': account.name,
91
 
                'level': account.level,
92
 
                'balance': account.balance and (account_type == 'income' and -1 or 1) * account.balance,
93
 
                }
94
 
 
95
 
        cr, uid = self.cr, self.uid
96
 
        db_pool = pooler.get_pool(self.cr.dbname)
97
 
 
98
 
        account_pool = db_pool.get('account.account')
99
 
        currency_pool = db_pool.get('res.currency')
100
 
 
101
 
        types = [
102
 
            'expense',
103
 
            'income'
104
 
                ]
105
 
 
106
 
        ctx = self.context.copy()
107
 
        ctx['fiscalyear'] = data['form'].get('fiscalyear_id', False)
108
 
        if ctx['fiscalyear']:
109
 
            ctx['fiscalyear'] = ctx['fiscalyear'][0]
110
 
 
111
 
        if data['form']['filter'] == 'filter_period':
112
 
            ctx['periods'] =  data['form'].get('periods', False)
113
 
        elif data['form']['filter'] == 'filter_date':
114
 
            ctx['date_from'] = data['form'].get('date_from', False)
115
 
            ctx['date_to'] =  data['form'].get('date_to', False)
116
 
 
117
 
        cal_list = {}
118
 
        account_id = data['form'].get('chart_account_id', False)
119
 
        if account_id:
120
 
            account_id = account_id[0]
121
 
        account_ids = account_pool._get_children_and_consol(cr, uid, account_id, context=ctx)
122
 
        accounts = account_pool.browse(cr, uid, account_ids, context=ctx)
123
 
        
124
 
        for typ in types:
125
 
            accounts_temp = []
126
 
            for account in accounts:
127
 
                if (account.user_type.report_type) and (account.user_type.report_type == typ):
128
 
                    currency = account.currency_id and account.currency_id or account.company_id.currency_id
129
 
                    if typ == 'expense' and account.type <> 'view' and (account.debit <> account.credit):
130
 
                        self.result_sum_dr += account.debit - account.credit
131
 
                    if typ == 'income' and account.type <> 'view' and (account.debit <> account.credit):
132
 
                        self.result_sum_cr += account.credit - account.debit
133
 
                    if data['form']['display_account'] == 'bal_movement':
134
 
                        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)):
135
 
                            accounts_temp.append(get_account_repr(account, typ))
136
 
                    elif data['form']['display_account'] == 'bal_solde':
137
 
                        if not currency_pool.is_zero(self.cr, self.uid, currency, account.balance):
138
 
                            accounts_temp.append(get_account_repr(account, typ))
139
 
                    else:
140
 
                        accounts_temp.append(get_account_repr(account, typ))
141
 
            if self.result_sum_dr > self.result_sum_cr:
142
 
                self.res_pl['type'] = _('Net Loss')
143
 
                self.res_pl['balance'] = (self.result_sum_dr - self.result_sum_cr)
144
 
            else:
145
 
                self.res_pl['type'] = _('Net Profit')
146
 
                self.res_pl['balance'] = (self.result_sum_cr - self.result_sum_dr)
147
 
            self.result[typ] = accounts_temp
148
 
            cal_list[typ] = self.result[typ]
149
 
        if cal_list:
150
 
            temp = {}
151
 
            for i in range(0,max(len(cal_list['expense']),len(cal_list['income']))):
152
 
                if i < len(cal_list['expense']) and i < len(cal_list['income']):
153
 
                    temp={
154
 
                          'code': cal_list['expense'][i]['code'],
155
 
                          'name': cal_list['expense'][i]['name'],
156
 
                          'level': cal_list['expense'][i]['level'],
157
 
                          'balance':cal_list['expense'][i]['balance'],
158
 
                          'code1': cal_list['income'][i]['code'],
159
 
                          'name1': cal_list['income'][i]['name'],
160
 
                          'level1': cal_list['income'][i]['level'],
161
 
                          'balance1': cal_list['income'][i]['balance'],
162
 
                          }
163
 
                    self.result_temp.append(temp)
164
 
                else:
165
 
                    if i < len(cal_list['income']):
166
 
                        temp={
167
 
                              'code': '',
168
 
                              'name': '',
169
 
                              'level': False,
170
 
                              'balance':False,
171
 
                              'code1': cal_list['income'][i]['code'],
172
 
                              'name1': cal_list['income'][i]['name'],
173
 
                              'level1': cal_list['income'][i]['level'],
174
 
                              'balance1': cal_list['income'][i]['balance'],
175
 
                              }
176
 
                        self.result_temp.append(temp)
177
 
                    if  i < len(cal_list['expense']):
178
 
                        temp={
179
 
                              'code': cal_list['expense'][i]['code'],
180
 
                              'name': cal_list['expense'][i]['name'],
181
 
                              'level': cal_list['expense'][i]['level'],
182
 
                              'balance': cal_list['expense'][i]['balance'],
183
 
                              'code1': '',
184
 
                              'name1': '',
185
 
                              'level1': False,
186
 
                              'balance1':False,
187
 
                              }
188
 
                        self.result_temp.append(temp)
189
 
        return None
190
 
 
191
 
    def get_lines(self):
192
 
        return self.result_temp
193
 
 
194
 
    def get_lines_another(self, group):
195
 
        return self.result.get(group, [])
196
 
 
197
 
report_sxw.report_sxw('report.account.profit_horizontal', 'account.account',
198
 
    'addons/account_report_alt/report/account_profit_horizontal.rml',parser=report_pl_account_horizontal, header='internal landscape')
199
 
 
200
 
report_sxw.report_sxw('report.account.profit_loss', 'account.account',
201
 
    'addons/account_report_alt/report/account_profit_loss.rml',parser=report_pl_account_horizontal, header='internal')
202
 
 
203
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: