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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
import time
from datetime import datetime
from odoo import models, api, fields
FETCH_RANGE = 2000
import io
import json
try:
from odoo.tools.misc import xlsxwriter
except ImportError:
import xlsxwriter
year = datetime.now().year
class AccountCasgFlow(models.TransientModel):
_name = "account.cash.flow"
_inherit = "account.common.report"
date_from = fields.Date(string="Start Date", default=str(year)+'-01-01')
date_to = fields.Date(string="End Date", default=fields.Date.today)
today = fields.Date("Report Date", default=fields.Date.today)
levels = fields.Selection([('summary', 'Summary'),
('consolidated', 'Consolidated'),
('detailed', 'Detailed'),
('very', 'Very Detailed')],
string='Levels', required=True, default='summary',
help='Different levels for cash flow statements \n'
'Summary: Month wise report.\n'
'Consolidated: Based on account types.\n'
'Detailed: Based on accounts.\n'
'Very Detailed: Accounts with their move lines')
account_ids = fields.Many2many(
"account.account",
string="Accounts",
)
@api.model
def view_report(self, option):
r = self.env['account.cash.flow'].search([('id', '=', option[0])])
data = {
'model': self,
'journals': r.journal_ids,
'target_move': r.target_move,
'levels': r.levels,
}
if r.date_from:
data.update({
'date_from': r.date_from,
})
if r.date_to:
data.update({
'date_to': r.date_to,
})
filters = self.get_filter(option)
report_lines = self._get_report_values(data, option)
fetched_data = report_lines['fetched_data']
fetched = report_lines['fetched']
account_res = report_lines['account_res']
journal_res = report_lines['journal_res']
levels = report_lines['levels']
currency = self._get_currency()
return {
'name': "Cash Flow Statements",
'type': 'ir.actions.client',
'tag': 'c_f',
'report_lines': report_lines,
'fetched_data': fetched_data,
'fetched': fetched,
'account_res': account_res,
'journal_res': journal_res,
'levels': r.levels,
'filters': filters,
'currency': currency,
}
def get_filter(self, option):
data = self.get_filter_data(option)
filters = {}
if data.get('journal_ids'):
filters['journals'] = self.env['account.journal'].browse(data.get('journal_ids')).mapped('code')
else:
filters['journals'] = ['All']
if data.get('account_ids', []):
filters['accounts'] = self.env['account.account'].browse(data.get('account_ids', [])).mapped('code')
else:
filters['accounts'] = ['All']
if data.get('target_move'):
filters['target_move'] = data.get('target_move')
if data.get('date_from'):
filters['date_from'] = data.get('date_from')
if data.get('date_to'):
filters['date_to'] = data.get('date_to')
if data.get('levels'):
filters['levels'] = data.get('levels')
filters['company_id'] = ''
filters['accounts_list'] = data.get('accounts_list')
filters['journals_list'] = data.get('journals_list')
filters['company_name'] = data.get('company_name')
filters['target_move'] = data.get('target_move').capitalize()
return filters
def get_filter_data(self, option):
r = self.env['account.cash.flow'].search([('id', '=', option[0])])
default_filters = {}
company_id = self.env.company
company_domain = [('company_id', '=', company_id.id)]
journals = r.journal_ids if r.journal_ids else self.env['account.journal'].search(company_domain)
accounts = self.account_ids if self.account_ids else self.env['account.account'].search(company_domain)
filter_dict = {
'journal_ids': r.journal_ids.ids,
'account_ids': self.account_ids.ids,
'company_id': company_id.id,
'date_from': r.date_from,
'date_to': r.date_to,
'levels': r.levels,
'target_move': r.target_move,
'journals_list': [(j.id, j.name, j.code) for j in journals],
'accounts_list': [(a.id, a.name) for a in accounts],
'company_name': company_id and company_id.name,
}
filter_dict.update(default_filters)
return filter_dict
def _get_report_values(self, data, option):
cr = self.env.cr
data = self.get_filter(option)
company_id = self.env.company
currency = company_id.currency_id
symbol = company_id.currency_id.symbol
rounding = company_id.currency_id.rounding
position = company_id.currency_id.position
fetched_data = []
account_res = []
journal_res = []
fetched = []
account_type_id = self.env.ref('account.data_account_type_liquidity').id
model = self.env.context.get('active_model')
if data.get('levels') == 'summary':
state = """ WHERE am.state = 'posted' """ if data.get('target_move') == 'posted' else ''
query3 = """SELECT to_char(am.date, 'Month') as month_part, extract(YEAR from am.date) as year_part,
sum(aml.debit) AS total_debit, sum(aml.credit) AS total_credit,
sum(aml.balance) AS total_balance FROM (SELECT am.date, am.id, am.state FROM account_move as am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
WHERE am.date BETWEEN '""" + str(
data.get('date_from')) + """' and '""" + str(
data.get('date_to')) + """' AND aat.id='""" + str(
account_type_id) + """' ) am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
""" + state + """GROUP BY month_part,year_part"""
cr = self._cr
cr.execute(query3)
fetched_data = cr.dictfetchall()
elif data.get('date_from') is False:
account_type_id = self.env.ref(
'account.data_account_type_liquidity').id
state = """AND am.state = 'posted' """ if data.get(
'target_move') == 'posted' else ''
sql = """SELECT DISTINCT aa.id, aa.name,aa.code, sum(aml.debit) AS total_debit,
sum(aml.credit) AS total_credit,sum(aml.balance) AS total_balance
FROM (SELECT am.* FROM account_move as am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
WHERE am.date BETWEEN '""" + str(
data.get('date_from')) + """' and '""" + str(
data.get('date_to')) + """' AND aat.id='""" + str(
account_type_id) + """' """ + state + """) am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
GROUP BY aa.name, aa.code,aa.id"""
cr = self._cr
cr.execute(sql)
fetched_data = cr.dictfetchall()
elif data.get('date_from') is False and data.get('date_from') != False:
account_type_id = self.env.ref(
'account.data_account_type_liquidity').id
state = """AND am.state = 'posted' """ if data.get(
'target_move') == 'posted' else ''
sql = """SELECT DISTINCT aa.id, aa.name,aa.code, sum(aml.debit) AS total_debit,
sum(aml.credit) AS total_credit,sum(aml.balance) AS total_balance
FROM (SELECT am.* FROM account_move as am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
WHERE am.date BETWEEN '""" + str(
data.get('date_from')) + """' and '""" + str(
data.get('date_to')) + """' AND aat.id='""" + str(
account_type_id) + """' """ + state + """) am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
GROUP BY aa.name, aa.code,aa.id"""
cr = self._cr
cr.execute(sql)
fetched_data = cr.dictfetchall()
elif data.get('date_from') is False and data.get('date_from') != False:
account_type_id = self.env.ref(
'account.data_account_type_liquidity').id
state = """AND am.state = 'posted' """ if data.get(
'target_move') == 'posted' else ''
sql = """SELECT DISTINCT aa.id, aa.name,aa.code, sum(aml.debit) AS total_debit,
sum(aml.credit) AS total_credit,sum(aml.balance) AS total_balance
FROM (SELECT am.* FROM account_move as am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
WHERE am.date BETWEEN '""" + str(
data.get('date_from')) + """' and '""" + str(
data.get('date_to')) + """' AND aat.id='""" + str(
account_type_id) + """' """ + state + """) am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
GROUP BY aa.name, aa.code,aa.id"""
cr = self._cr
cr.execute(sql)
fetched_data = cr.dictfetchall()
elif data.get('date_to') == " ":
account_type_id = self.env.ref(
'account.data_account_type_liquidity').id
state = """AND am.state = 'posted' """ if data.get(
'target_move') == 'posted' else ''
sql = """SELECT DISTINCT aa.id, aa.name,aa.code, sum(aml.debit) AS total_debit,
sum(aml.credit) AS total_credit,sum(aml.balance) AS total_balance
FROM (SELECT am.* FROM account_move as am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
WHERE am.date BETWEEN '""" + str(
data.get('date_from')) + """' and '""" + str(
data.get('date_to')) + """' AND aat.id='""" + str(
account_type_id) + """' """ + state + """) am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
GROUP BY aa.name, aa.code,aa.id"""
cr = self._cr
cr.execute(sql)
fetched_data = cr.dictfetchall()
elif data.get('levels') == 'consolidated':
state = """ WHERE am.state = 'posted' """ if data.get('target_move') == 'posted' else ''
query2 = """SELECT aat.name, sum(aml.debit) AS total_debit, sum(aml.credit) AS total_credit,
sum(aml.balance) AS total_balance FROM ( SELECT am.id, am.state FROM account_move as am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
WHERE am.date BETWEEN '""" + str(data.get('date_from')) + """' and '""" + str(
data.get('date_to')) + """' AND aat.id='""" + str(
account_type_id) + """' ) am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
""" + state + """GROUP BY aat.name"""
cr = self._cr
cr.execute(query2)
fetched_data = cr.dictfetchall()
elif data.get('levels') == 'detailed':
state = """ WHERE am.state = 'posted' """ if data.get('target_move') == 'posted' else ''
query1 = """SELECT aa.id,aa.name,aa.code, sum(aml.debit) AS total_debit, sum(aml.credit) AS total_credit,
sum(aml.balance) AS total_balance FROM (SELECT am.id, am.state FROM account_move as am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
WHERE am.date BETWEEN '""" + str(
data.get('date_from')) + """' and '""" + str(
data.get('date_to')) + """' AND aat.id='""" + str(
account_type_id) + """' ) am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
""" + state + """GROUP BY aa.name, aa.code, aa.id"""
cr = self._cr
cr.execute(query1)
fetched_data = cr.dictfetchall()
for account in self.env['account.account'].search([]):
child_lines = self.get_journal_lines(account, data)
if child_lines:
journal_res.append(child_lines)
else:
account_type_id = self.env.ref(
'account.data_account_type_liquidity').id
state = """AND am.state = 'posted' """ if data.get('target_move') == 'posted' else ''
sql = """SELECT DISTINCT aa.id, aa.name,aa.code, sum(aml.debit) AS total_debit,
sum(aml.credit) AS total_credit,sum(aml.balance) AS total_balance
FROM (SELECT am.* FROM account_move as am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
WHERE am.date BETWEEN '""" + str(
data.get('date_from')) + """' and '""" + str(
data.get('date_to')) + """' AND aat.id='""" + str(
account_type_id) + """' """ + state + """) am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
GROUP BY aa.name, aa.code,aa.id"""
cr = self._cr
cr.execute(sql)
fetched_data = cr.dictfetchall()
for account in self.env['account.account'].search([]):
child_lines = self._get_lines(account, data)
if child_lines:
account_res.append(child_lines)
journals = self.get_journal_lines(account, data)
if journals:
journal_res.append(journals)
return {
'date_from': data.get('date_from'),
'date_to': data.get('date_to'),
'levels': data.get('level'),
'doc_ids': self.ids,
'doc_model': model,
'fetched_data': fetched_data,
'account_res': account_res,
'journal_res': journal_res,
'fetched': fetched,
'company_currency_id': currency,
'company_currency_symbol': symbol,
'company_currency_position': position,
}
def _get_lines(self, account, data):
account_type_id = self.env.ref(
'account.data_account_type_liquidity').id
state = """AND am.state = 'posted' """ if data.get('target_move') == 'posted' else ''
query = """SELECT aml.account_id,aj.id as j_id,aj.name,am.id, am.name as move_name, sum(aml.debit) AS total_debit,
sum(aml.credit) AS total_credit, COALESCE(SUM(aml.debit - aml.credit),0) AS balance FROM (SELECT am.* FROM account_move as am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
WHERE am.date BETWEEN '""" + str(
data.get('date_from')) + """' and '""" + str(
data.get('date_to')) + """' AND aat.id='""" + str(
account_type_id) + """' """ + state + """) am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_journal aj ON aj.id = am.journal_id
WHERE aa.id = """ + str(account.id) + """
GROUP BY am.name, aml.account_id, aj.id, aj.name, am.id"""
cr = self._cr
cr.execute(query)
fetched_data = cr.dictfetchall()
sql2 = """SELECT aa.name as account_name,aa.id as account_id, aj.id, aj.name, sum(aml.debit) AS total_debit,
sum(aml.credit) AS total_credit, sum(aml.balance) AS total_balance FROM (SELECT am.* FROM account_move as am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
WHERE am.date BETWEEN '""" + str(
data.get('date_from')) + """' and '""" + str(
data.get('date_to')) + """' AND aat.id='""" + str(
account_type_id) + """' """ + state + """) am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_journal aj ON aj.id = am.journal_id
WHERE aa.id = """ + str(
account.id) + """
GROUP BY aa.name, aj.name, aj.id,aa.id"""
cr = self._cr
cr.execute(sql2)
fetch_data = cr.dictfetchall()
if fetched_data:
return {
'account': account.name,
'id': account.id,
'code': account.code,
'move_lines': fetched_data,
'journal_lines': fetch_data,
}
def get_journal_lines(self, account, data, offset=0, fetch_range=FETCH_RANGE):
account_type_id = self.env.ref(
'account.data_account_type_liquidity').id
offset_count = offset * fetch_range
state = """AND am.state = 'posted' """ if data.get('target_move') == 'posted' else ''
sql2 = """SELECT aa.name as account_name, aj.name, sum(aml.debit) AS total_debit,
sum(aml.credit) AS total_credit, COALESCE(SUM(aml.debit - aml.credit),0) AS balance FROM (SELECT am.* FROM account_move as am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_account_type aat ON aat.id = aa.user_type_id
WHERE am.date BETWEEN '""" + str(
data.get('date_from')) + """' and '""" + str(
data.get('date_to')) + """' AND aat.id='""" + str(
account_type_id) + """' """ + state + """) am
LEFT JOIN account_move_line aml ON aml.move_id = am.id
LEFT JOIN account_account aa ON aa.id = aml.account_id
LEFT JOIN account_journal aj ON aj.id = am.journal_id
WHERE aa.id = """ + str(account.id) + """
GROUP BY aa.name, aj.name"""
cr = self._cr
cr.execute(sql2)
fetched_data = cr.dictfetchall()
if fetched_data:
return {
'account': account.name,
'id': account.id,
'journal_lines': fetched_data,
'offset': offset_count,
}
@api.model
def create(self, vals):
vals['target_move'] = 'posted'
res = super(AccountCasgFlow, self).create(vals)
return res
def write(self, vals):
if vals.get('target_move'):
vals.update({'target_move': vals.get('target_move').lower()})
if vals.get('journal_ids'):
vals.update({'journal_ids': [(6, 0, vals.get('journal_ids'))]})
if vals.get('journal_ids') == []:
vals.update({'journal_ids': [(5,)]})
if vals.get('account_ids'):
vals.update({'account_ids': [(4, j) for j in vals.get('account_ids')]})
if vals.get('account_ids') == []:
vals.update({'account_ids': [(5,)]})
res = super(AccountCasgFlow, self).write(vals)
return res
@api.model
def _get_currency(self):
journal = self.env['account.journal'].browse(
self.env.context.get('default_journal_id', False))
if journal.currency_id:
return journal.currency_id.id
lang = self.env.user.lang
if not lang:
lang = 'en_US'
lang = lang.replace("_", '-')
currency_array = [self.env.company.currency_id.symbol,
self.env.company.currency_id.position, lang]
return currency_array
def get_dynamic_xlsx_report(self, data, response, report_data, dfr_data):
report_main_data = json.loads(dfr_data)
data = json.loads(data)
report_data = report_main_data.get('report_lines')
output = io.BytesIO()
workbook = xlsxwriter.Workbook(output, {'in_memory': True})
fetched_data = report_data.get('fetched_data')
account_res = report_data.get('account_res')
journal_res = report_data.get('journal_res')
fetched = report_data.get('fetched')
account_type_id = self.env.ref('account.data_account_type_liquidity').id
currency_symbol = self.env.company.currency_id.symbol
logged_users = self.env['res.company']._company_default_get('account.account')
sheet = workbook.add_worksheet()
bold = workbook.add_format({'align': 'center',
'bold': True,
'font_size': '10px',
'border': 1})
date = workbook.add_format({'font_size': '10px'})
cell_format = workbook.add_format({'bold': True,
'font_size': '10px'})
head = workbook.add_format({'align': 'center',
'bold': True,
'bg_color': '#D3D3D3',
'font_size': '15px'})
txt = workbook.add_format({'align': 'left',
'font_size': '10px'})
txt_left = workbook.add_format({'align': 'left',
'font_size': '10px',
'border': 1})
txt_center = workbook.add_format({'align': 'center',
'font_size': '10px',
'border': 1})
amount = workbook.add_format({'align': 'right',
'font_size': '10px',
'border': 1})
amount_bold = workbook.add_format({'align': 'right',
'bold': True,
'font_size': '10px',
'border': 1})
txt_bold = workbook.add_format({'align': 'left',
'bold': True,
'font_size': '10px',
'border': 1})
sheet.set_column('C:C', 30, cell_format)
sheet.set_column('D:E', 20, cell_format)
sheet.set_column('F:F', 20, cell_format)
sheet.merge_range('C3:F5', '')
sheet.merge_range('C3:F4', 'CASH FLOW STATEMENTS', head)
sheet.merge_range('C4:F4', '')
sheet.write('C6', "Date From", cell_format)
sheet.write('D6', str(data['date_from']), date)
sheet.write('E6', "Date To", cell_format)
sheet.write('F6', str(data['date_to']), date)
if data.get('levels'):
sheet.write('C7', "Level", cell_format)
sheet.write('D7', data.get("levels"), date)
sheet.write('E7', "Target Moves", cell_format)
sheet.write('F7', data.get("target_move"), date)
sheet.write('C9', 'NAME', bold)
sheet.write('D9', 'CASH IN', bold)
sheet.write('E9', 'CASH OUT', bold)
sheet.write('F9', 'BALANCE', bold)
row_num = 9
col_num = 2
fetched_data_list = fetched_data
account_res_list = account_res
journal_res_list = journal_res
fetched_list = fetched
for i_rec in fetched_data_list:
if data['levels'] == 'summary':
sheet.write(row_num + 1, col_num, str(i_rec['month_part']) + str(int(i_rec['year_part'])), txt_left)
sheet.write(row_num + 1, col_num + 1, str(i_rec['total_debit']) + str(currency_symbol), amount)
sheet.write(row_num + 1, col_num + 2, str(i_rec['total_credit']) + str(currency_symbol), amount)
sheet.write(row_num + 1, col_num + 3,
str(i_rec['total_debit'] - i_rec['total_credit']) + str(currency_symbol),
amount)
row_num = row_num + 1
elif data['levels'] == 'consolidated':
sheet.write(row_num + 1, col_num, i_rec['name'], txt_left)
sheet.write(row_num + 1, col_num + 1, str(i_rec['total_debit']) + str(currency_symbol), amount)
sheet.write(row_num + 1, col_num + 2, str(i_rec['total_credit']) + str(currency_symbol), amount)
sheet.write(row_num + 1, col_num + 3,
str(i_rec['total_debit'] - i_rec['total_credit']) + str(currency_symbol),
amount)
row_num = row_num + 1
for j_rec in journal_res_list:
if data['levels'] == 'detailed':
for k in fetched_data_list:
if k['name'] == j_rec['account']:
sheet.write(row_num + 1, col_num, str(k['code']) + str(k['name']), txt_bold)
sheet.write(row_num + 1, col_num + 1, str(k['total_debit']) + str(currency_symbol), amount_bold)
sheet.write(row_num + 1, col_num + 2, str(k['total_credit']) + str(currency_symbol), amount_bold)
sheet.write(row_num + 1, col_num + 3,
str(k['total_debit'] - k['total_credit']) + str(currency_symbol), amount_bold)
row_num = row_num + 1
for l_jrec in j_rec['journal_lines']:
sheet.write(row_num + 1, col_num, l_jrec['name'], txt_left)
sheet.write(row_num + 1, col_num + 1, str(l_jrec['total_debit']) + str(currency_symbol), amount)
sheet.write(row_num + 1, col_num + 2, str(l_jrec['total_credit']) + str(currency_symbol), amount)
sheet.write(row_num + 1, col_num + 3,
str(l_jrec['total_debit'] - l_jrec['total_credit']) + str(currency_symbol),
amount)
row_num = row_num + 1
for j_rec in account_res_list:
if data['levels'] == 'very':
for k in fetched_data_list:
if k['name'] == j_rec['account']:
sheet.write(row_num + 1, col_num, str(k['code']) + str(k['name']), txt_bold)
sheet.write(row_num + 1, col_num + 1, str(k['total_debit']) + str(currency_symbol), amount_bold)
sheet.write(row_num + 1, col_num + 2, str(k['total_credit']) + str(currency_symbol), amount_bold)
sheet.write(row_num + 1, col_num + 3,
str(k['total_debit'] - k['total_credit']) + str(currency_symbol), amount_bold)
row_num = row_num + 1
for l_jrec in j_rec['journal_lines']:
if l_jrec['account_name'] == j_rec['account']:
sheet.write(row_num + 1, col_num, l_jrec['name'], txt_left)
sheet.write(row_num + 1, col_num + 1, str(l_jrec['total_debit']) + str(currency_symbol), amount)
sheet.write(row_num + 1, col_num + 2, str(l_jrec['total_credit']) + str(currency_symbol), amount)
sheet.write(row_num + 1, col_num + 3,
str(l_jrec['total_debit'] - l_jrec['total_credit']) + str(currency_symbol),
amount)
row_num = row_num + 1
for m_rec in j_rec['move_lines']:
if m_rec['name'] == l_jrec['name']:
sheet.write(row_num + 1, col_num, m_rec['move_name'], txt_center)
sheet.write(row_num + 1, col_num + 1, str(m_rec['total_debit']) + str(currency_symbol), amount)
sheet.write(row_num + 1, col_num + 2, str(m_rec['total_credit']) + str(currency_symbol), amount)
sheet.write(row_num + 1, col_num + 3,
str(m_rec['total_debit'] - m_rec['total_credit']) + str(currency_symbol),
amount)
row_num = row_num + 1
workbook.close()
output.seek(0)
response.stream.write(output.read())
output.close()
|