~account-report-core-editor/account-financial-report/6.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#
#    Copyright (c) 2013 Noviat nv/sa (www.noviat.com). All rights reserved.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import xlwt
import time
from datetime import datetime
from openerp.report import report_sxw
from openerp.addons.report_xls.report_xls import report_xls
from openerp.addons.report_xls.utils import rowcol_to_cell
from openerp.addons.account_financial_report_webkit.report.general_ledger import GeneralLedgerWebkit
from openerp.tools.translate import _
#import logging
#_logger = logging.getLogger(__name__)

_column_sizes = [
    ('date', 12),
    ('period', 12),
    ('move', 20),
    ('journal', 12),
    ('account_code', 12),
    ('partner', 30),
    ('label', 45),
    ('counterpart', 30),
    ('debit', 15), 
    ('credit', 15),
    ('cumul_bal', 15),
    ('curr_bal', 15), 
    ('curr_code', 7),
]

class general_ledger_xls(report_xls):
    column_sizes = [x[1] for x in _column_sizes]
    
    def generate_xls_report(self, _p, _xs, data, objects, wb):
        
        ws = wb.add_sheet(_p.report_name[:31])
        ws.panes_frozen = True
        ws.remove_splits = True
        ws.portrait = 0 # Landscape
        ws.fit_width_to_pages = 1
        row_pos = 0
        
        # set print header/footer
        ws.header_str = self.xls_headers['standard']
        ws.footer_str = self.xls_footers['standard']
        
        # cf. account_report_general_ledger.mako  
        initial_balance_text = {'initial_balance': _('Computed'), 'opening_balance': _('Opening Entries'), False: _('No')}

        # Title
        cell_style = xlwt.easyxf(_xs['xls_title'])
        report_name =  ' - '.join([_p.report_name.upper(), _p.company.partner_id.name, _p.company.currency_id.name])
        c_specs = [
            ('report_name', 1, 0, 'text', report_name),
        ]       
        row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
        row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)

        # write empty row to define column sizes
        c_sizes = self.column_sizes
        c_specs = [('empty%s'%i, 1, c_sizes[i], 'text', None) for i in range(0,len(c_sizes))]
        row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
        row_pos = self.xls_write_row(ws, row_pos, row_data, set_column_size=True)        
        
        # Header Table
        cell_format = _xs['bold'] + _xs['fill_blue'] + _xs['borders_all']
        cell_style = xlwt.easyxf(cell_format)
        cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
        c_specs = [
            ('coa', 2, 0, 'text', _('Chart of Account')),
            ('fy', 1, 0, 'text', _('Fiscal Year')),
            ('df', 3, 0, 'text', _p.filter_form(data) == 'filter_date' and _('Dates Filter') or _('Periods Filter')),
            ('af', 1, 0, 'text', _('Accounts Filter')),
            ('tm', 2, 0, 'text',  _('Target Moves')),
            ('ib', 2, 0, 'text',  _('Initial Balance')),

        ]       
        row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
        row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style_center)

        cell_format = _xs['borders_all']
        cell_style = xlwt.easyxf(cell_format)
        cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
        c_specs = [
            ('coa', 2, 0, 'text', _p.chart_account.name),
            ('fy', 1, 0, 'text', _p.fiscalyear.name if _p.fiscalyear else '-'),
        ]
        df = _('From') + ': '
        if _p.filter_form(data) == 'filter_date':
            df += _p.start_date if _p.start_date else u'' 
        else:
            df += _p.start_period.name if _p.start_period else u''
        df += ' ' + _('To') + ': '
        if _p.filter_form(data) == 'filter_date':
            df += _p.stop_date if _p.stop_date else u''
        else:
            df += _p.stop_period.name if _p.stop_period else u''
        c_specs += [
            ('df', 3, 0, 'text', df),
            ('af', 1, 0, 'text', _p.accounts(data) and ', '.join([account.code for account in _p.accounts(data)]) or _('All')),
            ('tm', 2, 0, 'text', _p.display_target_move(data)),
            ('ib', 2, 0, 'text', initial_balance_text[_p.initial_balance_mode]),
        ]       
        row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
        row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style_center)  
        ws.set_horz_split_pos(row_pos)   
        row_pos += 1

        # Column Title Row
        cell_format = _xs['bold']
        c_title_cell_style = xlwt.easyxf(cell_format)

        # Column Header Row
        cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
        c_hdr_cell_style = xlwt.easyxf(cell_format)
        c_hdr_cell_style_right = xlwt.easyxf(cell_format + _xs['right'])
        c_hdr_cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
        c_hdr_cell_style_decimal = xlwt.easyxf(cell_format + _xs['right'], num_format_str = report_xls.decimal_format)

        # Column Initial Balance Row
        cell_format = _xs['italic'] + _xs['borders_all']
        c_init_cell_style = xlwt.easyxf(cell_format)
        c_init_cell_style_right = xlwt.easyxf(cell_format + _xs['right'])
        c_init_cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
        c_init_cell_style_decimal = xlwt.easyxf(cell_format + _xs['right'], num_format_str = report_xls.decimal_format)

        c_specs = [
            ('date', 1, 0, 'text', _('Date'), None, c_hdr_cell_style),
            ('period', 1, 0, 'text', _('Period'), None, c_hdr_cell_style),
            ('move', 1, 0, 'text', _('Entry'), None, c_hdr_cell_style),
            ('journal', 1, 0, 'text', _('Journal'), None, c_hdr_cell_style),
            ('account_code', 1, 0, 'text', _('Account'), None, c_hdr_cell_style),
            ('partner', 1, 0, 'text', _('Partner'), None, c_hdr_cell_style),
            ('label', 1, 0, 'text', _('Label'), None, c_hdr_cell_style),
            ('counterpart', 1, 0, 'text', _('Counterpart'), None, c_hdr_cell_style),
            ('debit', 1, 0, 'text', _('Debit'), None, c_hdr_cell_style_right),
            ('credit', 1, 0, 'text', _('Credit'), None, c_hdr_cell_style_right),
            ('cumul_bal', 1, 0, 'text', _('Cumul. Bal.'), None, c_hdr_cell_style_right),                    
        ]  
        if _p.amount_currency(data):
            c_specs += [
            ('curr_bal', 1, 0, 'text', _('Curr. Bal.'), None, c_hdr_cell_style_right),
            ('curr_code', 1, 0, 'text', _('Curr.'), None, c_hdr_cell_style_center),
            ]
        c_hdr_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
        
        # cell styles for ledger lines
        ll_cell_format = _xs['borders_all']
        ll_cell_style = xlwt.easyxf(ll_cell_format)
        ll_cell_style_right = xlwt.easyxf(ll_cell_format + _xs['right'])
        ll_cell_style_center = xlwt.easyxf(ll_cell_format + _xs['center'])
        ll_cell_style_date = xlwt.easyxf(ll_cell_format + _xs['left'], num_format_str = report_xls.date_format)
        ll_cell_style_decimal = xlwt.easyxf(ll_cell_format + _xs['right'], num_format_str = report_xls.decimal_format)
        
        cnt = 0
        for account in objects:

            display_initial_balance = account.init_balance and (account.init_balance.get('debit', 0.0) != 0.0 or account.init_balance.get('credit', 0.0) != 0.0)
            display_ledger_lines = account.ledger_lines

            if _p.display_account_raw(data) == 'all' or (display_ledger_lines or display_initial_balance):
                #TO DO : replace cumul amounts by xls formulas
                cnt += 1
                cumul_debit = 0.0
                cumul_credit = 0.0
                cumul_balance =  0.0
                cumul_balance_curr = 0.0
                c_specs = [
                    ('acc_title', 11, 0, 'text', ' - '.join([account.code, account.name])),
                ]
                row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
                row_pos = self.xls_write_row(ws, row_pos, row_data, c_title_cell_style)    
                row_pos = self.xls_write_row(ws, row_pos, c_hdr_data)   
                row_start = row_pos

                if display_initial_balance:
                    cumul_debit = account.init_balance.get('debit') or 0.0
                    cumul_credit = account.init_balance.get('credit') or 0.0
                    cumul_balance = account.init_balance.get('init_balance') or 0.0
                    cumul_balance_curr = account.init_balance.get('init_balance_currency') or 0.0     
                    debit_cell = rowcol_to_cell(row_pos, 8)                
                    credit_cell = rowcol_to_cell(row_pos, 9)
                    bal_formula = debit_cell + '-' + credit_cell                              
                    c_specs = [('empty%s' %x, 1, 0, 'text', None) for x in range(6)]
                    c_specs += [
                        ('init_bal', 1, 0, 'text', _('Initial Balance')),
                        ('counterpart', 1, 0, 'text', None),
                        ('debit', 1, 0, 'number', cumul_debit, None, c_init_cell_style_decimal),
                        ('credit', 1, 0, 'number', cumul_credit, None, c_init_cell_style_decimal),
                        ('cumul_bal', 1, 0, 'number', cumul_balance, None, c_init_cell_style_decimal),                    
                    ]  
                    if _p.amount_currency(data):
                        c_specs += [
                        ('curr_bal', 1, 0, 'number', cumul_balance_curr, None, c_init_cell_style_decimal),
                        ('curr_code', 1, 0, 'text', None),
                        ]
                    row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
                    row_pos = self.xls_write_row(ws, row_pos, row_data, c_init_cell_style) 

                for line in account.ledger_lines:
                    
                    cumul_debit += line.get('debit') or 0.0
                    cumul_credit += line.get('credit') or 0.0
                    cumul_balance_curr += line.get('amount_currency') or 0.0
                    cumul_balance += line.get('balance') or 0.0
                    label_elements = [line.get('lname') or '']
                    if line.get('invoice_number'):
                      label_elements.append("(%s)" % (line['invoice_number'],))
                    label = ' '.join(label_elements)

                    if line.get('ldate'):
                        c_specs = [
                            ('ldate', 1, 0, 'date', datetime.strptime(line['ldate'],'%Y-%m-%d'), None, ll_cell_style_date),
                        ]
                    else:
                        c_specs = [
                            ('ldate', 1, 0, 'text', None),
                        ]    
                    c_specs += [
                        ('period', 1, 0, 'text', line.get('period_code') or ''),
                        ('move', 1, 0, 'text', line.get('move_name') or ''),
                        ('journal', 1, 0, 'text', line.get('jcode') or ''),
                        ('account_code', 1, 0, 'text', account.code),
                        ('partner', 1, 0, 'text', line.get('partner_name') or ''),
                        ('label', 1, 0, 'text', label),
                        ('counterpart', 1, 0, 'text', line.get('counterparts') or ''),
                        ('debit', 1, 0, 'number', line.get('debit', 0.0), None, ll_cell_style_decimal),
                        ('credit', 1, 0, 'number', line.get('credit', 0.0), None, ll_cell_style_decimal),
                        ('cumul_bal', 1, 0, 'number', cumul_balance, None, ll_cell_style_decimal),
                    ]  
                    if _p.amount_currency(data):
                        c_specs += [
                        ('curr_bal', 1, 0, 'number', line.get('amount_currency') or 0.0, None, ll_cell_style_decimal),
                        ('curr_code', 1, 0, 'text', line.get('currency_code') or '', None, ll_cell_style_center),
                        ]
                    row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
                    row_pos = self.xls_write_row(ws, row_pos, row_data, ll_cell_style) 
                
                debit_start = rowcol_to_cell(row_start, 8)                
                debit_end = rowcol_to_cell(row_pos-1, 8)
                debit_formula = 'SUM(' + debit_start + ':' + debit_end + ')'
                credit_start = rowcol_to_cell(row_start, 9)                
                credit_end = rowcol_to_cell(row_pos-1, 9)
                credit_formula = 'SUM(' + credit_start + ':' + credit_end + ')'
                balance_debit = rowcol_to_cell(row_pos, 8)                
                balance_credit = rowcol_to_cell(row_pos, 9)
                balance_formula = balance_debit + '-' + balance_credit
                c_specs = [
                    ('acc_title', 7, 0, 'text', ' - '.join([account.code, account.name])),
                    ('cum_bal', 1, 0, 'text', _('Cumulated Balance on Account'), None, c_hdr_cell_style_right),
                    ('debit', 1, 0, 'number', None, debit_formula, c_hdr_cell_style_decimal),             
                    ('credit', 1, 0, 'number', None, credit_formula, c_hdr_cell_style_decimal),             
                    ('balance', 1, 0, 'number', None, balance_formula, c_hdr_cell_style_decimal),                       
                ]
                if _p.amount_currency(data):
                    if account.currency_id:
                        c_specs += [('curr_bal', 1, 0, 'number', cumul_balance_curr, None, c_hdr_cell_style_decimal)]
                    else:
                        c_specs += [('curr_bal', 1, 0, 'text', None)]
                    c_specs += [('curr_code', 1, 0, 'text', None)]
                row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
                row_pos = self.xls_write_row(ws, row_pos, row_data, c_hdr_cell_style) 
                row_pos += 1

general_ledger_xls('report.account.account_report_general_ledger_xls', 'account.account',
    parser=GeneralLedgerWebkit)
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: