/
account_standard_report.py
973 lines (885 loc) · 44.7 KB
/
account_standard_report.py
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# -*- coding: utf-8 -*-
import calendar
import odoo.addons.decimal_precision as dp
from datetime import datetime, timedelta
from odoo import api, models, fields, _
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
D_LEDGER = {'general': {'name': _('General Ledger'),
'group_by': 'account_id',
'model': 'account.account',
'short': 'code',
},
'partner': {'name': _('Partner Ledger'),
'group_by': 'partner_id',
'model': 'res.partner',
'short': 'name',
},
'journal': {'name': _('Journal Ledger'),
'group_by': 'journal_id',
'model': 'account.journal',
'short': 'code',
},
'open': {'name': _('Open Ledger'),
'group_by': 'account_id',
'model': 'account.account',
'short': 'code',
},
'aged': {'name': _('Aged Balance'),
'group_by': 'partner_id',
'model': 'res.partner',
'short': 'name',
},
}
class AccountStandardLedgerPeriode(models.TransientModel):
_name = 'account.report.standard.ledger.periode'
name = fields.Char('Name')
date_from = fields.Datetime('Date from')
date_to = fields.Datetime('Date to')
class AccountStandardLedgerReport(models.TransientModel):
_name = 'account.report.standard.ledger.report'
name = fields.Char()
report_object_ids = fields.One2many('account.report.standard.ledger.report.object', 'report_id')
report_name = fields.Char()
line_total_ids = fields.Many2many('account.report.standard.ledger.line', relation='table_standard_report_line_total')
line_super_total_id = fields.Many2one('account.report.standard.ledger.line')
print_time = fields.Char()
date_from = fields.Date(string='Start Date', help='Use to compute initial balance.')
date_to = fields.Date(string='End Date', help='Use to compute the entrie matched with futur.')
class AccountStandardLedgerLines(models.TransientModel):
_name = 'account.report.standard.ledger.line'
_order = 'id' # ,move_id,account_id #type,date,move_line_id_id,
_rec_name = 'move_id'
report_id = fields.Many2one('account.report.standard.ledger.report')
account_id = fields.Many2one('account.account', 'Account')
type = fields.Selection([('0_init', 'Initial'), ('1_init_line', 'Init Line'), ('2_line', 'Line'), ('3_compact',
'Compacted'), ('4_total', 'Total'), ('5_super_total', 'Super Total')], string='Type')
type_view = fields.Selection([('init', 'Init'), ('normal', 'Normal'), ('total', 'Total')])
journal_id = fields.Many2one('account.journal', 'Journal')
partner_id = fields.Many2one('res.partner', 'Partner')
group_by_key = fields.Char()
move_id = fields.Many2one('account.move', 'Entrie')
move_line_id = fields.Many2one('account.move.line')
date = fields.Date()
date_maturity = fields.Date('Due Date')
debit = fields.Float(default=0.0, digits=dp.get_precision('Account'))
credit = fields.Float(default=0.0, digits=dp.get_precision('Account'))
balance = fields.Float(default=0.0, digits=dp.get_precision('Account'))
cumul_balance = fields.Float(default=0.0, digits=dp.get_precision('Account'), string='Balance')
full_reconcile_id = fields.Many2one('account.full.reconcile', 'Match.')
reconciled = fields.Boolean('Reconciled')
report_object_id = fields.Many2one('account.report.standard.ledger.report.object')
current = fields.Float(default=0.0, digits=dp.get_precision('Account'), string='Not due')
age_30_days = fields.Float(default=0.0, digits=dp.get_precision('Account'), string='30 days')
age_60_days = fields.Float(default=0.0, digits=dp.get_precision('Account'), string='60 days')
age_90_days = fields.Float(default=0.0, digits=dp.get_precision('Account'), string='90 days')
age_120_days = fields.Float(default=0.0, digits=dp.get_precision('Account'), string='120 days')
older = fields.Float(default=0.0, digits=dp.get_precision('Account'), string='Older')
company_currency_id = fields.Many2one('res.currency')
@api.model
def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
res = super(AccountStandardLedgerLines, self).read_group(domain, fields, groupby, offset, limit=limit, orderby=orderby, lazy=lazy)
if 'cumul_balance' in fields and 'debit' in fields and 'credit' in fields:
for line in res:
line['cumul_balance'] = line['debit'] - line['credit']
return res
class AccountStandardLedgerReportObject(models.TransientModel):
_name = 'account.report.standard.ledger.report.object'
_order = 'name, id'
name = fields.Char()
object_id = fields.Integer()
report_id = fields.Many2one('account.report.standard.ledger.report')
line_ids = fields.One2many('account.report.standard.ledger.line', 'report_object_id')
account_id = fields.Many2one('account.account', 'Account')
journal_id = fields.Many2one('account.journal', 'Journal')
partner_id = fields.Many2one('res.partner', 'Partner')
class AccountStandardLedger(models.TransientModel):
_name = 'account.report.standard.ledger'
_description = 'Account Standard Ledger'
def _get_periode_date(self):
lang_code = self.env.user.lang or 'en_US'
date_format = self.env['res.lang']._lang_get(lang_code).date_format
today_year = fields.datetime.now().year
last_day = self.company_id.fiscalyear_last_day or 31
last_month = self.company_id.fiscalyear_last_month or 12
periode_obj = self.env['account.report.standard.ledger.periode']
periode_obj.search([]).unlink()
periode_ids = periode_obj
for year in range(today_year, today_year - 4, -1):
date_from = datetime(year - 1, last_month, last_day) + timedelta(days=1)
date_to = datetime(year, last_month, last_day)
user_periode = "%s - %s" % (date_from.strftime(date_format),
date_to.strftime(date_format),
)
vals = {
'name': user_periode,
'date_from': date_from.strftime(DEFAULT_SERVER_DATE_FORMAT),
'date_to': date_to.strftime(DEFAULT_SERVER_DATE_FORMAT), }
periode_ids += periode_obj.create(vals)
return False
name = fields.Char(default='Standard Report')
type_ledger = fields.Selection([('general', 'General Ledger'), ('partner', 'Partner Ledger'), ('journal', 'Journal Ledger'), ('open', 'Open Ledger'), ('aged', 'Aged Balance')], string='Type', default='general', required=True,
help=' * General Ledger : Journal entries group by account\n'
' * Partner Leger : Journal entries group by partner, with only payable/recevable accounts\n'
' * Journal Ledger : Journal entries group by journal, without initial balance\n'
' * Open Ledger : Openning journal at Start date\n')
summary = fields.Boolean('Trial Balance', default=False,
help=' * Check : generate a trial balance.\n'
' * Uncheck : detail report.\n')
amount_currency = fields.Boolean("With Currency", help="It adds the currency column on report if the currency differs from the company currency.")
reconciled = fields.Boolean('With Reconciled Entries', default=True,
help='Only for entrie with a payable/receivable account.\n'
' * Check this box to see un-reconcillied and reconciled entries with payable.\n'
' * Uncheck to see only un-reconcillied entries. Can be use only with parnter ledger.\n')
partner_select_ids = fields.Many2many(comodel_name='res.partner', string='Partners', domain=['|', ('is_company', '=', True), ('parent_id', '=', False)], help='If empty, get all partners')
account_methode = fields.Selection([('include', 'Include'), ('exclude', 'Exclude')], string="Methode")
account_in_ex_clude = fields.Many2many(comodel_name='account.account', string='Accounts', help='If empty, get all accounts')
init_balance_history = fields.Boolean('Initial balance with history.', default=True,
help=' * Check this box if you need to report all the debit and the credit sum before the Start Date.\n'
' * Uncheck this box to report only the balance before the Start Date\n')
company_id = fields.Many2one('res.company', string='Company', readonly=True, default=lambda self: self.env.user.company_id)
company_currency_id = fields.Many2one('res.currency', related='company_id.currency_id', string="Company Currency", readonly=True,
help='Utility field to express amount currency', store=True)
journal_ids = fields.Many2many('account.journal', string='Journals', required=True, default=lambda self: self.env['account.journal'].search([('company_id', '=', self.env.user.company_id.id)]),
help='Select journal, for the Open Ledger you need to set all journals.')
date_from = fields.Date(string='Start Date', help='Use to compute initial balance.')
date_to = fields.Date(string='End Date', help='Use to compute the entrie matched with futur.')
target_move = fields.Selection([('posted', 'All Posted Entries'),
('all', 'All Entries'),
], string='Target Moves', required=True, default='posted')
periode_date = fields.Many2one('account.report.standard.ledger.periode', 'Periode', default=_get_periode_date, help="Auto complete Start and End date.")
month_selec = fields.Selection([(1, '01 Junary'), (2, '02 Febuary'), (3, '03 March'), (4, '04 April'), (5, '05 May'), (6, '06 June'),
(7, '07 Jully'), (8, '08 August'), (9, '09 September'), (10, '10 October'), (11, '11 November'), (12, '12 December')],
string='Month')
result_selection = fields.Selection([('customer', 'Customer'),
('supplier', 'Supplier'),
('customer_supplier', 'Receivable and Payable Accounts')
], string="Partner's", required=True, default='supplier')
report_name = fields.Char('Report Name')
compact_account = fields.Boolean('Compacte account.', default=False)
report_id = fields.Many2one('account.report.standard.ledger.report')
account_ids = fields.Many2many('account.account', relation='table_standard_report_accounts')
partner_ids = fields.Many2many('res.partner', relation='table_standard_report_partner')
type = fields.Selection([('account', 'Account'), ('partner', 'Partner'), ('journal', 'Journal')])
@api.onchange('account_in_ex_clude')
def on_change_summary(self):
if self.account_in_ex_clude:
self.account_methode = 'include'
else:
self.account_methode = False
@api.onchange('type_ledger')
def on_change_type_ledger(self):
if self.type_ledger in ('partner', 'journal', 'open', 'aged'):
self.compact_account = False
if self.type_ledger == 'aged':
self.date_from = False
self.reconciled = False
else:
self.on_change_periode_date()
self.on_change_month_selec()
if self.type_ledger not in ('partner', 'aged',):
self.reconciled = True
return {'domain': {'account_in_ex_clude': []}}
self.account_in_ex_clude = False
if self.result_selection == 'suplier':
return {'domain': {'account_in_ex_clude': [('type_third_parties', '=', 'supplier')]}}
if self.result_selection == 'customer':
return {'domain': {'account_in_ex_clude': [('type_third_parties', '=', 'customer')]}}
return {'domain': {'account_in_ex_clude': [('type_third_parties', 'in', ('supplier', 'customer'))]}}
@api.onchange('periode_date')
def on_change_periode_date(self):
if self.periode_date:
self.date_from = self.periode_date.date_from
self.date_to = self.periode_date.date_to
if self.month_selec:
self.on_change_month_selec()
@api.onchange('month_selec')
def on_change_month_selec(self):
if self.periode_date and self.month_selec:
date_from = datetime.strptime(self.periode_date.date_from, DEFAULT_SERVER_DATETIME_FORMAT)
date_from = datetime(date_from.year, self.month_selec, 1)
date_to = datetime(date_from.year, self.month_selec, calendar.monthrange(date_from.year, self.month_selec)[1])
self.date_from = date_from.strftime(DEFAULT_SERVER_DATE_FORMAT)
self.date_to = date_to.strftime(DEFAULT_SERVER_DATE_FORMAT)
elif self.periode_date and not self.month_selec:
self.on_change_periode_date()
def action_view_lines(self):
self.ensure_one()
self.compute_data()
return {
'name': _("Ledger Lines"),
'view_type': 'form',
'view_mode': 'tree,form',
'views': [(self.env.ref('account_standard_report.view_aged_tree').id if self.type_ledger == 'aged' else False, 'tree'), (False, 'form')],
'res_model': 'account.report.standard.ledger.line',
'type': 'ir.actions.act_window',
'domain': "[('report_id','=',%s),('type','not in',('5_super_total','4_total'))]" % (self.report_id.id),
'context': {'search_default_%s' % self.type_ledger: 1, 'read_report_id': self.report_id.id},
'target': 'current',
}
def print_pdf_report(self):
self.ensure_one()
self.compute_data()
return self.env['report'].get_action(self, 'account_standard_report.report_account_standard_report')
def print_excel_report(self):
self.ensure_one()
self.compute_data()
return self.env['report'].get_action(self, 'account_standard_report.report_account_standard_excel')
def pre_compute(self):
lang_code = self.env.context.get('lang') or 'en_US'
date_format = self.env['res.lang']._lang_get(lang_code).date_format
time_format = self.env['res.lang']._lang_get(lang_code).time_format
vals = {'report_name': self._get_name_report(),
'name': self._get_name_report(),
'print_time': '%s' % fields.Datetime.context_timestamp(self.with_context(tz=self.env.user.tz), datetime.now()).strftime(('%s %s') % (date_format, time_format)),
'date_to': self.date_to if self.date_to else "2099-01-01",
'date_from': self.date_from if self.date_from else "1970-01-01",
}
self.report_id = self.env['account.report.standard.ledger.report'].create(vals)
self.account_ids = self._search_account()
self.partner_ids = self._search_partner()
if self.type_ledger in ('general', 'open'):
self.type = 'account'
elif self.type_ledger in ('partner', 'aged'):
self.type = 'partner'
else:
self.type = 'journal'
if self.type_ledger in ('partner', 'journal', 'open', 'aged'):
self.compact_account = False
if self.type_ledger not in ('partner', 'aged',):
self.reconciled = True
self.partner_select_ids = False
def compute_data(self):
self.pre_compute()
self._sql_report_object()
if self.type == 'account':
self._sql_unaffected_earnings()
if self.type in ('account, partner'):
if self.type_ledger != 'aged':
self._sql_init_balance()
self._sql_lines()
if self.compact_account and self.type_ledger == 'general':
self._sql_lines_compacted()
self._sql_total()
self._sql_super_total()
self.refresh()
# complet total line
line_obj = self.env['account.report.standard.ledger.line']
self.report_id.line_total_ids = line_obj.search([('report_id', '=', self.report_id.id), ('type', '=', '4_total')])
self.report_id.line_super_total_id = line_obj.search([('report_id', '=', self.report_id.id), ('type', '=', '5_super_total')], limit=1)
self._format_total()
def _sql_report_object(self):
query = """INSERT INTO account_report_standard_ledger_report_object
(report_id, create_uid, create_date, object_id, name, account_id, partner_id, journal_id)
SELECT DISTINCT
%s AS report_id,
%s AS create_uid,
NOW() AS create_date,
CASE
WHEN %s = 'account' THEN aml.account_id
WHEN %s = 'partner' THEN aml.partner_id
ELSE aml.journal_id
END AS object_id,
CASE
WHEN %s = 'account' THEN acc.code || ' ' || acc.name
WHEN %s = 'partner' THEN CASE WHEN rep.ref IS NULL THEN rep.name ELSE rep.ref || ' ' || rep.name END
ELSE acj.code || ' ' || acj.name
END AS name,
CASE WHEN %s = 'account' THEN aml.account_id ELSE NULL END AS account_id,
CASE WHEN %s = 'partner' THEN aml.partner_id ELSE NULL END AS partner_id,
CASE WHEN %s = 'journal' THEN aml.journal_id ELSE NULL END AS journal_id
FROM
account_move_line aml
LEFT JOIN account_account acc ON (acc.id = aml.account_id)
LEFT JOIN res_partner rep ON (rep.id = aml.partner_id)
LEFT JOIN account_journal acj ON (acj.id = aml.journal_id)
WHERE
aml.company_id = %s
AND aml.journal_id IN %s
AND aml.account_id IN %s
AND (%s in ('account', 'journal') OR aml.partner_id IN %s)
ORDER BY
name
"""
params = [
# SELECT
self.report_id.id,
self.env.uid,
self.type, self.type, self.type, self.type,
self.type, self.type, self.type,
# WHERE
self.company_id.id,
tuple(self.journal_ids.ids) if self.journal_ids else (None,),
tuple(self.account_ids.ids) if self.account_ids else (None,),
self.type,
tuple(self.partner_ids.ids) if self.partner_ids else (None,),
]
self.env.cr.execute(query, tuple(params))
def _sql_unaffected_earnings(self):
company = self.company_id
unaffected_earnings_account = self.env['account.account'].search([('company_id', '=', company.id), ('user_type_id', '=', self.env.ref('account.data_unaffected_earnings').id)], limit=1)
if unaffected_earnings_account not in self.account_ids:
return
report_object_id = self.report_id.report_object_ids.filtered(lambda x: x.object_id == unaffected_earnings_account.id)
if not report_object_id:
report_object_id = self.report_id.report_object_ids.create({'report_id': self.report_id.id,
'object_id': unaffected_earnings_account.id,
'name': '%s %s' % (unaffected_earnings_account.code, unaffected_earnings_account.name),
'account_id': unaffected_earnings_account.id})
query = """
INSERT INTO account_report_standard_ledger_line
(report_id, create_uid, create_date, account_id, type, type_view, date, debit, credit, balance, cumul_balance, company_currency_id, reconciled, report_object_id)
SELECT
%s AS report_id,
%s AS create_uid,
NOW() AS create_date,
%s AS account_id,
'0_init' AS type,
'init' AS type_view,
%s AS date,
CASE WHEN %s THEN COALESCE(SUM(aml.debit), 0) ELSE CASE WHEN COALESCE(SUM(aml.balance), 0) <= 0 THEN 0 ELSE COALESCE(SUM(aml.balance), 0) END END AS debit,
CASE WHEN %s THEN COALESCE(SUM(aml.credit), 0) ELSE CASE WHEN COALESCE(SUM(aml.balance), 0) >= 0 THEN 0 ELSE COALESCE(-SUM(aml.balance), 0) END END AS credit,
COALESCE(SUM(aml.balance), 0) AS balance,
COALESCE(SUM(aml.balance), 0) AS cumul_balance,
%s AS company_currency_id,
FALSE as reconciled,
%s AS report_object_id
FROM
account_move_line aml
LEFT JOIN account_account acc ON (aml.account_id = acc.id)
LEFT JOIN account_account_type acc_type ON (acc.user_type_id = acc_type.id)
LEFT JOIN account_move m ON (aml.move_id = m.id)
WHERE
m.state IN %s
AND aml.company_id = %s
AND aml.date < %s
AND acc_type.include_initial_balance = FALSE
HAVING
CASE
WHEN %s = FALSE THEN ABS(SUM(aml.balance)) > %s
ELSE ABS(SUM(aml.debit)) > %s OR ABS(SUM(aml.debit)) > %s OR ABS(SUM(aml.balance)) > %s
END
"""
date_from_fiscal = self.company_id.compute_fiscalyear_dates(datetime.strptime(self.report_id.date_from, DEFAULT_SERVER_DATE_FORMAT))['date_from']
params = [
# SELECT
self.report_id.id,
self.env.uid,
unaffected_earnings_account.id,
date_from_fiscal,
self.init_balance_history,
self.init_balance_history,
self.company_currency_id.id,
report_object_id.id,
# WHERE
('posted',) if self.target_move == 'posted' else ('posted', 'draft',),
company.id,
self.report_id.date_from,
# HAVING
self.init_balance_history,
self.company_currency_id.rounding, self.company_currency_id.rounding, self.company_currency_id.rounding, self.company_currency_id.rounding,
]
self.env.cr.execute(query, tuple(params))
def _sql_init_balance(self):
company = self.company_id
# initial balance partner
query = """
INSERT INTO account_report_standard_ledger_line
(report_id, create_uid, create_date, account_id, partner_id, group_by_key, type, type_view, date, debit, credit, balance, cumul_balance, company_currency_id, reconciled, report_object_id)
WITH matching_in_futur_before_init (id) AS
(
SELECT DISTINCT
afr.id as id
FROM
account_full_reconcile afr
INNER JOIN account_move_line aml ON aml.full_reconcile_id=afr.id
WHERE
aml.company_id = %s
AND aml.date >= %s
)
SELECT
%s AS report_id,
%s AS create_uid,
NOW() AS create_date,
MIN(aml.account_id),
CASE WHEN %s = 'partner' THEN MIN(aml.partner_id) ELSE NULL END,
(CASE
WHEN %s = 'account' THEN '-' || aml.account_id
ELSE aml.partner_id || '-' || aml.account_id
END) AS group_by_key,
'0_init' AS type,
'init' AS type_view,
%s AS date,
CASE WHEN %s THEN COALESCE(SUM(aml.debit), 0) ELSE CASE WHEN COALESCE(SUM(aml.balance), 0) <= 0 THEN 0 ELSE COALESCE(SUM(aml.balance), 0) END END AS debit,
CASE WHEN %s THEN COALESCE(SUM(aml.credit), 0) ELSE CASE WHEN COALESCE(SUM(aml.balance), 0) >= 0 THEN 0 ELSE COALESCE(-SUM(aml.balance), 0) END END AS credit,
COALESCE(SUM(aml.balance), 0) AS balance,
COALESCE(SUM(aml.balance), 0) AS cumul_balance,
%s AS company_currency_id,
FALSE as reconciled,
MIN(ro.id) AS report_object_id
FROM
account_report_standard_ledger_report_object ro
INNER JOIN account_move_line aml ON (CASE WHEN %s = 'account' THEN aml.account_id = ro.object_id ELSE aml.partner_id = ro.object_id END)
LEFT JOIN account_account acc ON (aml.account_id = acc.id)
LEFT JOIN account_account_type acc_type ON (acc.user_type_id = acc_type.id)
LEFT JOIN account_move m ON (aml.move_id = m.id)
LEFT JOIN matching_in_futur_before_init mif ON (aml.full_reconcile_id = mif.id)
WHERE
m.state IN %s
AND ro.report_id = %s
AND aml.company_id = %s
AND aml.date < %s
AND acc_type.include_initial_balance = TRUE
AND aml.journal_id IN %s
AND aml.account_id IN %s
AND (%s in ('account', 'journal') OR aml.partner_id IN %s)
AND ((%s AND acc.compacted = TRUE) OR acc.type_third_parties = 'no' OR (aml.full_reconcile_id IS NOT NULL AND mif.id IS NULL))
GROUP BY
group_by_key
HAVING
CASE
WHEN %s = FALSE THEN ABS(SUM(aml.balance)) > %s
ELSE ABS(SUM(aml.debit)) > %s OR ABS(SUM(aml.debit)) > %s OR ABS(SUM(aml.balance)) > %s
END
"""
params = [
# matching_in_futur
company.id,
self.report_id.date_from,
# init_account_table
# SELECT
self.report_id.id,
self.env.uid,
self.type, self.type,
self.report_id.date_from,
self.init_balance_history,
self.init_balance_history,
self.company_currency_id.id,
# FROM
self.type,
# WHERE
('posted',) if self.target_move == 'posted' else ('posted', 'draft',),
self.report_id.id,
company.id,
self.report_id.date_from,
tuple(self.journal_ids.ids) if self.journal_ids else (None,),
tuple(self.account_ids.ids) if self.account_ids else (None,),
self.type,
tuple(self.partner_ids.ids) if self.partner_ids else (None,),
self.compact_account,
# HAVING
self.init_balance_history,
self.company_currency_id.rounding, self.company_currency_id.rounding, self.company_currency_id.rounding, self.company_currency_id.rounding,
]
self.env.cr.execute(query, tuple(params))
def _sql_lines(self):
# lines_table
query = """
INSERT INTO account_report_standard_ledger_line
(report_id, create_uid, create_date, account_id, type, type_view, journal_id, partner_id, move_id, move_line_id, date, date_maturity, debit, credit, balance, full_reconcile_id, reconciled, report_object_id, cumul_balance, current, age_30_days, age_60_days, age_90_days, age_120_days, older, company_currency_id)
WITH matching_in_futur_before_init (id) AS
(
SELECT DISTINCT
afr.id AS id
FROM
account_full_reconcile afr
INNER JOIN account_move_line aml ON aml.full_reconcile_id=afr.id
WHERE
aml.company_id = %s
AND aml.date >= %s
),
matching_in_futur_after_date_to (id) AS
(
SELECT DISTINCT
afr.id AS id
FROM
account_full_reconcile afr
INNER JOIN account_move_line aml ON aml.full_reconcile_id = afr.id
WHERE
aml.company_id = %s
AND aml.date > %s
),
initial_balance (id, balance) AS
(
SELECT
MIN(report_object_id) AS id,
COALESCE(SUM(balance), 0) AS balance
FROM
account_report_standard_ledger_line
WHERE
report_id = %s
AND type = '0_init'
GROUP BY
report_object_id
),
date_range AS
(
SELECT
%s AS date_current,
DATE %s - INTEGER '30' AS date_less_30_days,
DATE %s - INTEGER '60' AS date_less_60_days,
DATE %s - INTEGER '90' AS date_less_90_days,
DATE %s - INTEGER '120' AS date_less_120_days,
DATE %s - INTEGER '150' AS date_older
)
SELECT
%s AS report_id,
%s AS create_uid,
NOW() AS create_date,
aml.account_id,
CASE WHEN aml.date >= %s THEN '2_line' ELSE '1_init_line' END AS type,
CASE WHEN aml.date >= %s THEN 'normal' ELSE 'init' END AS type_view,
aml.journal_id,
aml.partner_id,
aml.move_id,
aml.id,
aml.date,
aml.date_maturity,
aml.debit,
aml.credit,
aml.balance,
aml.full_reconcile_id,
CASE WHEN aml.full_reconcile_id is NOT NULL AND NOT mifad.id IS NOT NULL THEN TRUE ELSE FALSE END AS reconciled,
ro.id AS report_object_id,
CASE
WHEN %s = 'account' THEN COALESCE(init.balance, 0) + (SUM(aml.balance) OVER (PARTITION BY aml.account_id ORDER BY aml.account_id, aml.date, aml.id))
WHEN %s = 'partner' THEN COALESCE(init.balance, 0) + (SUM(aml.balance) OVER (PARTITION BY aml.partner_id ORDER BY aml.partner_id, aml.date, aml.id))
ELSE SUM(aml.balance) OVER (PARTITION BY aml.journal_id ORDER BY aml.journal_id, aml.date, aml.id)
END AS cumul_balance,
CASE WHEN aml.date_maturity > date_range.date_less_30_days THEN aml.balance END AS current,
CASE WHEN aml.date_maturity > date_range.date_less_60_days AND aml.date_maturity <= date_range.date_less_30_days THEN aml.balance END AS age_30_days,
CASE WHEN aml.date_maturity > date_range.date_less_90_days AND aml.date_maturity <= date_range.date_less_60_days THEN aml.balance END AS age_60_days,
CASE WHEN aml.date_maturity > date_range.date_less_120_days AND aml.date_maturity <= date_range.date_less_90_days THEN aml.balance END AS age_90_days,
CASE WHEN aml.date_maturity > date_range.date_older AND aml.date_maturity <= date_range.date_less_120_days THEN aml.balance END AS age_120_days,
CASE WHEN aml.date_maturity <= date_range.date_older THEN aml.balance END AS older,
%s AS company_currency_id
FROM
date_range,
account_report_standard_ledger_report_object ro
INNER JOIN account_move_line aml ON (
CASE
WHEN %s = 'account' THEN aml.account_id = ro.object_id
WHEN %s = 'partner' THEN aml.partner_id = ro.object_id
ELSE aml.journal_id = ro.object_id
END)
LEFT JOIN account_journal j ON (aml.journal_id = j.id)
LEFT JOIN account_account acc ON (aml.account_id = acc.id)
LEFT JOIN account_account_type acc_type ON (acc.user_type_id = acc_type.id)
LEFT JOIN account_move m ON (aml.move_id = m.id)
LEFT JOIN matching_in_futur_before_init mif ON (aml.full_reconcile_id = mif.id)
LEFT JOIN matching_in_futur_after_date_to mifad ON (aml.full_reconcile_id = mifad.id)
LEFT JOIN initial_balance init ON (ro.id = init.id)
WHERE
m.state IN %s
AND ro.report_id = %s
AND aml.company_id = %s
AND (CASE
WHEN %s = 'journal' THEN aml.date >= %s
WHEN aml.date >= %s THEN %s != 'open'
ELSE acc.type_third_parties IN ('supplier', 'customer') AND (aml.full_reconcile_id IS NULL OR mif.id IS NOT NULL)
END)
AND aml.date <= %s
AND aml.journal_id IN %s
AND aml.account_id IN %s
AND (%s IN ('account','journal') OR aml.partner_id IN %s)
AND NOT (%s AND acc.compacted = TRUE)
AND (%s OR NOT (aml.full_reconcile_id is NOT NULL AND NOT mifad.id IS NOT NULL))
ORDER BY
aml.date, aml.id
"""
params = [
# matching_in_futur init
self.company_id.id,
self.report_id.date_from,
# matching_in_futur date_to
self.company_id.id,
self.report_id.date_to,
# initial_balance
self.report_id.id,
# date_range
self.report_id.date_to, self.report_id.date_to, self.report_id.date_to, self.report_id.date_to, self.report_id.date_to, self.report_id.date_to,
# lines_table
# SELECT
self.report_id.id,
self.env.uid,
self.report_id.date_from,
self.report_id.date_from,
self.type, self.type,
self.company_currency_id.id,
# FROM
self.type, self.type,
# WHERE
('posted',) if self.target_move == 'posted' else ('posted', 'draft',),
self.report_id.id,
self.company_id.id,
self.type, self.report_id.date_from,
self.report_id.date_from, self.type_ledger,
self.report_id.date_to,
tuple(self.journal_ids.ids) if self.journal_ids else (None,),
tuple(self.account_ids.ids) if self.account_ids else (None,),
self.type,
tuple(self.partner_ids.ids) if self.partner_ids else (None,),
self.compact_account,
self.reconciled,
]
self.env.cr.execute(query, tuple(params))
def _sql_lines_compacted(self):
query = """
INSERT INTO account_report_standard_ledger_line
(report_id, create_uid, create_date, account_id, type, type_view, date, debit, credit, balance, cumul_balance, company_currency_id, report_object_id)
WITH initial_balance (id, balance) AS
(
SELECT
MIN(report_object_id) AS id,
COALESCE(SUM(balance), 0) AS balance
FROM
account_report_standard_ledger_line
WHERE
report_id = %s
AND type = '0_init'
GROUP BY
report_object_id
)
SELECT
%s AS report_id,
%s AS create_uid,
NOW() AS create_date,
MIN(aml.account_id) AS account_id,
'3_compact' AS type,
'normal' AS type_view,
%s AS date,
COALESCE(SUM(aml.debit), 0) AS debit,
COALESCE(SUM(aml.credit), 0) AS credit,
COALESCE(SUM(aml.balance), 0) AS balance,
COALESCE(MIN(init.balance), 0) + COALESCE(SUM(aml.balance), 0) AS cumul_balance,
%s AS company_currency_id,
MIN(ro.id) AS report_object_id
FROM
account_report_standard_ledger_report_object ro
INNER JOIN account_move_line aml ON (aml.account_id = ro.object_id)
LEFT JOIN account_journal j ON (aml.journal_id = j.id)
LEFT JOIN account_account acc ON (aml.account_id = acc.id)
LEFT JOIN account_account_type acc_type ON (acc.user_type_id = acc_type.id)
LEFT JOIN account_move m ON (aml.move_id = m.id)
LEFT JOIN initial_balance init ON (ro.id = init.id)
WHERE
m.state IN %s
AND ro.report_id = %s
AND aml.company_id = %s
AND aml.date >= %s
AND aml.date <= %s
AND aml.journal_id IN %s
AND aml.account_id IN %s
AND (%s AND acc.compacted = TRUE)
GROUP BY
aml.account_id
"""
params = [
# initial_balance
self.report_id.id,
# SELECT
self.report_id.id,
self.env.uid,
self.report_id.date_from,
self.company_currency_id.id,
# FROM
# WHERE
('posted',) if self.target_move == 'posted' else ('posted', 'draft',),
self.report_id.id,
self.company_id.id,
self.report_id.date_from,
self.report_id.date_to,
tuple(self.journal_ids.ids) if self.journal_ids else (None,),
tuple(self.account_ids.ids) if self.account_ids else (None,),
self.compact_account,
]
self.env.cr.execute(query, tuple(params))
def _sql_total(self):
query = """
INSERT INTO account_report_standard_ledger_line
(report_id, create_uid, create_date, account_id, partner_id, journal_id, type, type_view, date, debit, credit, balance, cumul_balance, report_object_id, current, age_30_days, age_60_days, age_90_days, age_120_days, older, company_currency_id)
SELECT
%s AS report_id,
%s AS create_uid,
NOW() AS create_date,
CASE WHEN %s = 'account' THEN MIN(account_id) ELSE NULL END AS account_id,
CASE WHEN %s = 'partner' THEN MIN(partner_id) ELSE NULL END AS partner_id,
CASE WHEN %s = 'journal' THEN MIN(journal_id) ELSE NULL END AS journal_id,
'4_total' AS type,
'total' AS type_view,
%s AS date,
COALESCE(SUM(debit), 0) AS debit,
COALESCE(SUM(credit), 0) AS credit,
COALESCE(SUM(balance), 0) AS balance,
COALESCE(SUM(balance), 0) AS cumul_balance,
MIN(report_object_id) AS report_object_id,
COALESCE(SUM(current), 0) AS current,
COALESCE(SUM(age_30_days), 0) AS age_30_days,
COALESCE(SUM(age_60_days), 0) AS age_60_days,
COALESCE(SUM(age_90_days), 0) AS age_90_days,
COALESCE(SUM(age_120_days), 0) AS age_120_days,
COALESCE(SUM(older), 0) AS older,
%s AS company_currency_id
FROM
account_report_standard_ledger_line
WHERE
report_id = %s
AND report_object_id IS NOT NULL
GROUP BY
report_object_id
ORDER BY
report_object_id
"""
params = [
# SELECT
self.report_id.id,
self.env.uid,
self.type, self.type, self.type,
self.report_id.date_from,
self.company_currency_id.id,
# WHERE
self.report_id.id,
]
self.env.cr.execute(query, tuple(params))
def _sql_super_total(self):
query = """
INSERT INTO account_report_standard_ledger_line
(report_id, create_uid, create_date, type, type_view, date, debit, credit, balance, cumul_balance, current, age_30_days, age_60_days, age_90_days, age_120_days, older, company_currency_id)
SELECT
%s AS report_id,
%s AS create_uid,
NOW() AS create_date,
'5_super_total' AS type,
'total' AS type_view,
%s AS date,
COALESCE(SUM(debit), 0) AS debit,
COALESCE(SUM(credit), 0) AS credit,
COALESCE(SUM(balance), 0) AS balance,
COALESCE(SUM(balance), 0) AS cumul_balance,
COALESCE(SUM(current), 0) AS current,
COALESCE(SUM(age_30_days), 0) AS age_30_days,
COALESCE(SUM(age_60_days), 0) AS age_60_days,
COALESCE(SUM(age_90_days), 0) AS age_90_days,
COALESCE(SUM(age_120_days), 0) AS age_120_days,
COALESCE(SUM(older), 0) AS older,
%s AS company_currency_id
FROM
account_report_standard_ledger_line
WHERE
report_id = %s
AND type = '4_total'
"""
params = [
# SELECT
self.report_id.id,
self.env.uid,
self.report_id.date_from,
self.company_currency_id.id,
self.report_id.id,
]
self.env.cr.execute(query, tuple(params))
def _search_account(self):
type_ledger = self.type_ledger
domain = [('deprecated', '=', False), ('company_id', '=', self.company_id.id)]
if type_ledger in ('partner', 'aged',):
result_selection = self.result_selection
if result_selection == 'supplier':
acc_type = ('supplier',)
elif result_selection == 'customer':
acc_type = ('customer',)
else:
acc_type = ('supplier', 'customer',)
domain.append(('type_third_parties', 'in', acc_type))
account_in_ex_clude = self.account_in_ex_clude.ids
acc_methode = self.account_methode
if account_in_ex_clude:
if acc_methode == 'include':
domain.append(('id', 'in', account_in_ex_clude))
elif acc_methode == 'exclude':
domain.append(('id', 'not in', account_in_ex_clude))
return self.env['account.account'].search(domain)
def _search_partner(self):
if self.type_ledger in ('partner', 'aged'):
if self.partner_select_ids:
return self.partner_select_ids
return self.env['res.partner'].search([])
return False
def _get_name_report(self):
report_name = D_LEDGER[self.type_ledger]['name']
if self.summary:
report_name += _(' Balance')
return report_name
def sql_get_line_for_report(self, type_l, report_object=None):
query = """SELECT
aml.report_object_id AS report_object_id,
aml.type_view AS type_view,
CASE
WHEN %s = 'account' THEN acc.code
WHEN %s = 'journal' THEN acj.code
ELSE rep.ref
END AS code,
CASE
WHEN %s = 'account' THEN acc.name
WHEN %s = 'journal' THEN acj.name
ELSE rep.name
END AS name,
acj.code AS j_code,
acc.code AS a_code,
acc.name AS a_name,
aml.current AS current,
aml.age_30_days AS age_30_days,
aml.age_60_days AS age_60_days,
aml.age_90_days AS age_90_days,
aml.age_120_days AS age_120_days,
aml.older AS older,
aml.credit AS credit,
aml.debit AS debit,
aml.cumul_balance AS cumul_balance,
aml.balance AS balance,
ml.name AS move_name,
ml.ref AS displayed_name,
rep.name AS partner_name,
aml.date AS date,
aml.date_maturity AS date_maturity,
CASE
WHEN aml.full_reconcile_id IS NOT NULL THEN (CASE WHEN aml.reconciled = TRUE THEN afr.name ELSE '*' END)
ELSE ''
END AS matching_number
FROM
account_report_standard_ledger_line aml
LEFT JOIN account_account acc ON (acc.id = aml.account_id)
LEFT JOIN account_journal acj ON (acj.id = aml.journal_id)
LEFT JOIN res_partner rep ON (rep.id = aml.partner_id)
LEFT JOIN account_move ml ON (ml.id = aml.move_id)
LEFT JOIN account_full_reconcile afr ON (aml.full_reconcile_id = afr.id)
WHERE
aml.report_id = %s
AND (%s OR aml.report_object_id = %s)
AND aml.type IN %s
ORDER BY
aml.id
"""
params = [
self.type, self.type, self.type, self.type,
self.report_id.id,
True if report_object is None else False,
report_object,
type_l
]
self.env.cr.execute(query, tuple(params))
return self.env.cr.dictfetchall()
def _format_total(self):
if not self.company_currency_id:
return
lines = self.report_id.line_total_ids + self.report_id.line_super_total_id
for line in lines:
line.write({
'debit': self.company_currency_id.round(line.debit) + 0.0,
'credit': self.company_currency_id.round(line.credit) + 0.0,
'balance': self.company_currency_id.round(line.balance) + 0.0,
'current': self.company_currency_id.round(line.current) + 0.0,
'age_30_days': self.company_currency_id.round(line.age_30_days) + 0.0,
'age_60_days': self.company_currency_id.round(line.age_60_days) + 0.0,
'age_90_days': self.company_currency_id.round(line.age_90_days) + 0.0,
'age_120_days': self.company_currency_id.round(line.age_120_days) + 0.0,
'older': self.company_currency_id.round(line.older) + 0.0,
})