-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathAR Aging.sql
More file actions
965 lines (951 loc) · 33.4 KB
/
AR Aging.sql
File metadata and controls
965 lines (951 loc) · 33.4 KB
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
/*************************************************************************/
/* */
/* (c) 2010-2024 Enginatics GmbH */
/* www.enginatics.com */
/* */
/*************************************************************************/
-- Report Name: AR Aging
-- Description: The AR Aging report allows users to review information about their open receivables items at a specified point in time (the As of Date). The report will show the aging of the open receivables items based on the selected aging bucket.
- The report includes detailed (Transaction Level) or summary (Customer Level) information about customers current and past due invoices, debit memos, and chargebacks.
- Optionally the report can include details of credit memos, on-account credits, unidentified payments, on-account and unapplied cash amounts, and receipts at risk.
- Optionally the report allows the open receivables items to be revalued to a different currency on a specified revaluation date using a specified revaluation currency rate type.
- All amounts in the report are shown in functional currency, except where the report is run for a specified entered currency, in which case the amounts are shown in the specified entered currency.
Report Parameters:
Reporting Level: The report can be run by Ledger or by Operating Unit.
Reporting Context: The Ledgers or Operating Units the report is to be run for. Only Ledgers or Operating Units accessible to the current responsibility maybe selected. The report supports the multiple selection of Ledgers or Operating Units allowing to be run for more than one Ledger or Operating Unit. If the Reporting Context is left null, then the report will be run for all Operating Units accessible to the current responsibility.
Report Summary: The report is summarized at either the Customer Level (Customer Summary) or at Transaction Level (Invoice Summary). The Customer Summary report includes open receivables totals at the customer level only and does not include transaction level details. The Invoice Summary report includes details and the outstanding amounts of the open receivables transactions.
As of Date: The report can be run to provide an aging snapshot at a specified point in time in the past. By default, the As of Date will be the current date.
Aging Bucket Name: The Aging Bucket Name determine the Aging Buckets to be used for aging the open receivables items. The aging amount columns in the report are dynamically determined based on the selected Aging Buket.
Aging Basis: Transactions can be aged based on their Due Date (default) or on their Transaction (Invoice) Date.
Show On Account: The report can optionally include the details and/or amounts for credit memos, on-account credits, unidentified payments, on-account and unapplied cash amounts.
The options for displaying these are:
Do Not Show – they are not included in the report.
Summarize – the amounts are shown as separate columns in the report and are not included in the Aging Amount report columns.
Age – the amounts are included in the Aging Amount report columns.
Show Receipts At Risk: The report can optionally include the details and/or amounts for receipts at risk.
Do Not Show – they are not included in the report.
Summarize – the amounts are shown as separate a column in the report and are not included in the Aging Amount report columns.
Age – the amounts are included in the Aging Amount report columns.
Entered Currency: Restrict the report to open receivables items entered in the specified currency. By default, all amounts in the report are shown in functional currency, except in the case the report is run in for a specified Entered Currency. In this case the amounts are shown in the specified entered currency.
Revaluation Date, Revaluation Currency, Revaluation Rate Type:
If a revaluation date, currency, and rate type are specified, the report will include additional columns showing the open receivables amounts and aging in the specified revaluation currency.
Additionally, there are several additional parameters which can be used to restrict the data returned by the report.
-- Excel Examle Output: https://www.enginatics.com/example/ar-aging/
-- Library Link: https://www.enginatics.com/reports/ar-aging/
-- Run Report: https://demo.enginatics.com/
select
x1.ledger ledger,
x1.operating_unit operating_unit,
fnd_flex_xml_publisher_apis.process_kff_combination_1('lp_acct_flex_bal_seg', 'SQLGL', 'GL#', x1.chart_of_accounts_id, NULL, x1.code_combination_id, 'GL_BALANCING', 'Y', 'VALUE') "&lp_bal_seg_p",
fnd_flex_xml_publisher_apis.process_kff_combination_1('lp_acct_flex_acc_seg', 'SQLGL', 'GL#', x1.chart_of_accounts_id, NULL, x1.code_combination_id, 'GL_ACCOUNT', 'Y', 'VALUE') "&lp_acc_seg_p",
fnd_flex_xml_publisher_apis.process_kff_combination_1('lp_accounting_flexfield', 'SQLGL', 'GL#', x1.chart_of_accounts_id, NULL, x1.code_combination_id, 'ALL', 'Y', 'VALUE') gl_account_segments,
x1.sort_field1 salesperson,
x1.cust_name customer,
x1.cust_no customer_number,
x1.cust_country customer_country,
x1.cust_class customer_classification,
x1.credit_limits,
x1.order_credit_limits,
x1.collector collector,
&lp_invoice_cols_s
x1.revaluation_from_currency amounts_currency,
nvl(sum(x1.amt_due_original),0) original_amount,
nvl(sum(x1.amt_due_remaining),0) outstanding_amount,
--
nvl(sum(
case
when x1.days_past_due > 0
or (:p_credit_option='DETAIL' and x1.class in ('PMT','CM','CLAIM'))
or (:p_risk_option='DETAIL' and x1.invoice_type=:p_risk_meaning)
then x1.amt_due_remaining
else 0
end
),0) past_due_amount,
--
&lp_on_acc_summ_cols
:p_age_basis Aging_Basis,
&lp_aging_amount_cols
&lp_aging_pct_cols
-- Revaluation Columns
&lp_reval_columns
&lp_reval_aging_amount_cols
--
&party_dff_cols1
&cust_dff_cols1
--
fnd_flex_xml_publisher_apis.process_kff_combination_1('lp_acct_flex_bal_seg', 'SQLGL', 'GL#', x1.chart_of_accounts_id, NULL, x1.code_combination_id, 'GL_BALANCING', 'Y', 'DESCRIPTION') "&lp_bal_seg_p Desc",
fnd_flex_xml_publisher_apis.process_kff_combination_1('lp_acct_flex_acc_seg', 'SQLGL', 'GL#', x1.chart_of_accounts_id, NULL, x1.code_combination_id, 'GL_ACCOUNT', 'Y', 'DESCRIPTION') "&lp_acc_seg_p Desc",
fnd_flex_xml_publisher_apis.process_kff_combination_1('lp_accounting_flexfield', 'SQLGL', 'GL#', x1.chart_of_accounts_id, NULL, x1.code_combination_id, 'ALL', 'Y', 'DESCRIPTION') gl_account_segments_desc
from
( --start x1
select
x.ledger,
(select haou.name from hr_all_organization_units haou where haou.organization_id=x.org_id) operating_unit,
x.sort_field1,
x.sort_field2,
hcpamv.credit_limits,
hcpamv.order_credit_limits,
x.cust_name,
x.cust_no,
x.cust_country,
x.cust_class,
(select ac.name
from ar_collectors ac
where ac.collector_id=nvl(hcps.collector_id,hcpa.collector_id)
) collector,
x.class,
x.cons_billing_number,
x.invnum,
x.invoice_currency_code,
x.term,
x.trx_date,
x.due_date,
x.days_past_trx,
x.days_past_due,
x.amt_due_original,
x.amount_adjusted,
x.amount_applied,
x.amount_credited,
x.gl_date,
x.data_converted,
x.ps_exchange_rate,
x.code_combination_id,
x.chart_of_accounts_id,
x.invoice_type,
x.comments,
--
&lp_bucket_cols1
--
case when (:p_credit_option='SUMMARY' and x.class in ('PMT','CM','CLAIM'))
or (:p_risk_option='SUMMARY' AND x.invoice_type=:p_risk_meaning)
then to_number(null)
else x.amt_due_remaining
end amt_due_remaining,
case when :p_credit_option='SUMMARY' AND x.class='PMT'
then x.amt_due_remaining
else null
end on_account_amount_cash,
case when :p_credit_option='SUMMARY' AND x.class='CM'
then x.amt_due_remaining
else null
end on_account_amount_credit,
case when :p_risk_option='SUMMARY' AND x.invoice_type=:p_risk_meaning
then x.amt_due_remaining
else null
end on_account_amount_risk,
case when :p_credit_option='SUMMARY' AND x.class='CLAIM'
then x.amt_due_remaining
else null
end cust_amount_claim,
nvl(:p_in_currency,x.functional_currency) revaluation_from_currency,
decode(nvl(:p_in_currency,x.functional_currency),:p_reval_currency,1,(select gdr.conversion_rate from gl_daily_conversion_types gdct, gl_daily_rates gdr where nvl(:p_in_currency,x.functional_currency)=gdr.from_currency and gdr.to_currency=:p_reval_currency and :p_reval_conv_date=gdr.conversion_date and gdct.user_conversion_type=:p_reval_conv_type and gdct.conversion_type=gdr.conversion_type)) reval_conv_rate,
--
&party_dff_cols2
&cust_dff_cols2
--
xxen_util.meaning(:p_reporting_level,'FND_MO_REPORTING_LEVEL',0) reporting_level,
case :p_reporting_level when '1000' then x.ledger when '3000' then (select haou.name from hr_all_organization_units haou where haou.organization_id=x.org_id) end reporting_entity
from
( --start x
select
substrb(hp.party_name,1,50) cust_name,
hca.account_number cust_no,
nvl(sales.name,jrrev.resource_name) sort_field1,
arpt_sql_func_util.get_org_trx_type_details(ps.cust_trx_type_id,ps.org_id) sort_field2,
nvl(sales.salesrep_id, -3) salesrep_id,
rt.name term,
site.site_use_id,
loc.state cust_state,
loc.city cust_city,
(select ftv.territory_short_name from fnd_territories_vl ftv where ftv.territory_code=loc.country) cust_country,
xxen_util.meaning(hca.customer_class_code,'CUSTOMER CLASS',222) cust_class,
decode(decode(upper(rtrim(rpad(:p_in_format_option_low, 1))),'D','D',null),null,-1,acct_site.cust_acct_site_id) addr_id,
nvl(hca.cust_account_id,-999) cust_id,
hca.party_id,
--
&party_dff_cols3
&cust_dff_cols3
--
ps.payment_schedule_id payment_sched_id,
ps.class class,
ps.trx_date,
ps.due_date,
decode(nvl2(:p_in_currency,'N','Y'), 'Y', ps.amount_due_original * nvl(ps.exchange_rate, 1), ps.amount_due_original) amt_due_original,
xxen_ar_arxagrw_pkg.comp_amt_due_remainingformula
( ps.class,
arpt_sql_func_util.get_org_trx_type_details(ps.cust_trx_type_id,ps.org_id),
ps.payment_schedule_id,
ps.amt_due_remaining,
ps.amount_applied,
ps.amount_credited,
ps.amount_adjusted
) amt_due_remaining,
ps.trx_number invnum,
ceil(:p_in_as_of_date_low - ps.trx_date) days_past_trx,
ceil(:p_in_as_of_date_low - ps.due_date) days_past_due,
ps.amount_adjusted amount_adjusted,
ps.amount_applied amount_applied,
ps.amount_credited amount_credited,
ps.gl_date gl_date,
decode(ps.invoice_currency_code, gsob.currency_code, NULL, decode(ps.exchange_rate, null, '*', null)) data_converted,
nvl(ps.exchange_rate, 1) ps_exchange_rate,
--
&lp_bucket_cols2
--
c.code_combination_id,
c.chart_of_accounts_id,
ci.cons_billing_number cons_billing_number,
arpt_sql_func_util.get_org_trx_type_details(ps.cust_trx_type_id,ps.org_id) invoice_type,
rct.comments,
ps.invoice_currency_code,
gsob.currency_code functional_currency,
gsob.name ledger,
ps.org_id
from
hz_cust_accounts hca,
hz_parties hp,
(select
a.customer_id,
a.customer_site_use_id ,
a.customer_trx_id,
a.payment_schedule_id,
a.class ,
sum(a.primary_salesrep_id) primary_salesrep_id,
a.term_id,
a.trx_date,
a.due_date,
sum(a.amount_due_remaining) amt_due_remaining,
a.trx_number,
a.amount_due_original,
a.amount_adjusted,
a.amount_applied ,
a.amount_credited ,
a.amount_adjusted_pending,
a.gl_date ,
a.cust_trx_type_id,
a.org_id,
a.invoice_currency_code,
a.exchange_rate,
sum(a.cons_inv_id) cons_inv_id
from
(
select
ps.customer_id,
ps.customer_site_use_id,
ps.customer_trx_id,
ps.payment_schedule_id,
ps.class,
0 primary_salesrep_id,
ps.term_id,
ps.trx_date,
ps.due_date,
nvl(sum(decode(nvl2(:p_in_currency,'N','Y'), 'Y', nvl(adj.acctd_amount, 0), adj.amount)),0) * (-1) amount_due_remaining,
ps.trx_number,
ps.amount_due_original,
ps.amount_adjusted,
ps.amount_applied,
ps.amount_credited,
ps.amount_adjusted_pending,
ps.gl_date,
ps.cust_trx_type_id,
ps.org_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate,1) exchange_rate,
0 cons_inv_id
from
ar_payment_schedules ps,
ar_adjustments adj
where
ps.gl_date <= :p_in_as_of_date_low
and ps.customer_id > 0
and ps.gl_date_closed > :p_in_as_of_date_low
and decode(upper(:p_in_currency),NULL, ps.invoice_currency_code,upper(:p_in_currency))=ps.invoice_currency_code
and adj.payment_schedule_id=ps.payment_schedule_id
and adj.status='A'
and adj.gl_date > :p_in_as_of_date_low
and xxen_ar_arxagrw_pkg.include_org_id(ps.org_id)='Y'
and adj.org_id=ps.org_id
group by
ps.customer_id,
ps.customer_site_use_id,
ps.customer_trx_id,
ps.class,
ps.term_id,
ps.trx_date,
ps.due_date,
ps.trx_number,
ps.amount_due_original,
ps.amount_adjusted,
ps.amount_applied,
ps.amount_credited,
ps.amount_adjusted_pending,
ps.gl_date,
ps.cust_trx_type_id,
ps.org_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate,1),
ps.payment_schedule_id
union all
select
ps.customer_id,
ps.customer_site_use_id,
ps.customer_trx_id,
ps.payment_schedule_id,
ps.class,
0 primary_salesrep_id,
ps.term_id,
ps.trx_date,
ps.due_date,
nvl(sum(decode( nvl2(:p_in_currency,'N','Y'),
'Y', (decode( ps.class,
'CM', decode( app.application_type, 'CM', app.acctd_amount_applied_from, app.acctd_amount_applied_to),
app.acctd_amount_applied_to
) +
nvl(app.acctd_earned_discount_taken,0) +
nvl(app.acctd_unearned_discount_taken,0)
)
, ( app.amount_applied +
nvl(app.earned_discount_taken,0) +
nvl(app.unearned_discount_taken,0)
)
) *
decode( ps.class,
'CM', decode(app.application_type, 'CM', -1, 1),
1
)
),0
) amount_due_remaining,
ps.trx_number,
ps.amount_due_original,
ps.amount_adjusted,
ps.amount_applied ,
ps.amount_credited,
ps.amount_adjusted_pending,
ps.gl_date gl_date,
ps.cust_trx_type_id,
ps.org_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate, 1) exchange_rate,
0 cons_inv_id
from
ar_payment_schedules ps,
ar_receivable_applications app
where
ps.gl_date <= :p_in_as_of_date_low
and ps.customer_id > 0
and ps.gl_date_closed > :p_in_as_of_date_low
and decode(upper(:p_in_currency),NULL, ps.invoice_currency_code, upper(:p_in_currency))=ps.invoice_currency_code
and (app.applied_payment_schedule_id=ps.payment_schedule_id or
app.payment_schedule_id=ps.payment_schedule_id
)
and app.status IN ('APP', 'ACTIVITY')
and nvl( app.confirmed_flag, 'Y' )='Y'
and app.gl_date > :p_in_as_of_date_low
and xxen_ar_arxagrw_pkg.include_org_id(ps.org_id)='Y'
and app.org_id=ps.org_id
group by
ps.customer_id,
ps.customer_site_use_id,
ps.customer_trx_id,
ps.class,
ps.term_id,
ps.trx_date,
ps.due_date,
ps.trx_number,
ps.amount_due_original,
ps.amount_adjusted,
ps.amount_applied,
ps.amount_credited,
ps.amount_adjusted_pending,
ps.gl_date,
ps.cust_trx_type_id,
ps.org_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate, 1),
ps.payment_schedule_id
union all
select
ps.customer_id,
ps.customer_site_use_id,
ps.customer_trx_id,
ps.payment_schedule_id,
ps.class class,
nvl(ct.primary_salesrep_id, -3) primary_salesrep_id,
ps.term_id,
ps.trx_date,
ps.due_date,
decode( nvl2(:p_in_currency,'N','Y'), 'Y', ps.acctd_amount_due_remaining, ps.amount_due_remaining) amt_due_remaining,
ps.trx_number,
ps.amount_due_original,
ps.amount_adjusted,
ps.amount_applied,
ps.amount_credited,
ps.amount_adjusted_pending,
ps.gl_date,
ps.cust_trx_type_id,
ps.org_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate, 1) exchange_rate,
ps.cons_inv_id
from
ar_payment_schedules ps,
ra_customer_trx ct
where
ps.gl_date <= :p_in_as_of_date_low
and ps.gl_date_closed > :p_in_as_of_date_low
and decode(upper(:p_in_currency),NULL, ps.invoice_currency_code, upper(:p_in_currency))=ps.invoice_currency_code
and ps.customer_trx_id=ct.customer_trx_id
and ps.class <> 'CB'
and xxen_ar_arxagrw_pkg.include_org_id(ps.org_id)='Y'
and ct.org_id=ps.org_id
union all
select
ps.customer_id,
ps.customer_site_use_id ,
ps.customer_trx_id,
ps.payment_schedule_id,
ps.class class,
nvl(ct.primary_salesrep_id,-3) primary_salesrep_id,
ps.term_id,
ps.trx_date,
ps.due_date,
decode( nvl2(:p_in_currency,'N','Y'), 'Y', ps.acctd_amount_due_remaining, ps.amount_due_remaining) amt_due_remaining,
ps.trx_number,
ps.amount_due_original,
ps.amount_adjusted,
ps.amount_applied,
ps.amount_credited,
ps.amount_adjusted_pending,
ps.gl_date,
ps.cust_trx_type_id,
ps.org_id,
ps.invoice_currency_code,
nvl(ps.exchange_rate, 1) exchange_rate,
ps.cons_inv_id
from
ar_payment_schedules ps,
ra_customer_trx ct,
ar_adjustments adj
where
ps.gl_date <= :p_in_as_of_date_low
and ps.gl_date_closed > :p_in_as_of_date_low
and decode(upper(:p_in_currency),NULL, ps.invoice_currency_code, upper(:p_in_currency))=ps.invoice_currency_code
and ps.class='CB'
and ps.customer_trx_id=adj.chargeback_customer_trx_id (+)
and ps.org_id=adj.org_id (+)
and adj.customer_trx_id=ct.customer_trx_id (+)
and adj.org_id=ct.org_id (+)
and xxen_ar_arxagrw_pkg.include_org_id(ps.org_id)='Y'
) a
group by
a.customer_id,
a.customer_site_use_id,
a.customer_trx_id,
a.payment_schedule_id,
a.class,
a.term_id,
a.trx_date,
a.due_date,
a.trx_number,
a.amount_due_original,
a.amount_adjusted,
a.amount_applied,
a.amount_credited,
a.amount_adjusted_pending,
a.gl_date,
a.cust_trx_type_id,
a.org_id,
a.invoice_currency_code,
a.exchange_rate
) ps,
ar_cons_inv ci,
ra_salesreps_all sales,
jtf_rs_resource_extns_vl jrrev,
hz_cust_site_uses site,
hz_cust_acct_sites acct_site,
hz_party_sites party_site,
hz_locations loc,
ra_cust_trx_line_gl_dist gld,
ar_dispute_history dh,
ra_terms rt,
gl_code_combinations c,
ra_customer_trx rct,
gl_sets_of_books gsob
where
--upper(RTRIM(RPAD(:p_in_summary_option_low,1)) )='I'
ps.customer_site_use_id=site.site_use_id
and site.cust_acct_site_id=acct_site.cust_acct_site_id
and acct_site.party_site_id=party_site.party_site_id
and loc.location_id=party_site.location_id
and ps.customer_id=hca.cust_account_id
and hca.party_id=hp.party_id
and ps.customer_trx_id=gld.customer_trx_id
and gld.account_class='REC'
and gld.latest_rec_flag='Y'
and gld.code_combination_id=c.code_combination_id
and ps.payment_schedule_id = dh.payment_schedule_id(+)
and ps.term_id=rt.term_id (+)
and :p_in_as_of_date_low >= nvl(dh.start_date(+), :p_in_as_of_date_low)
and :p_in_as_of_date_low < nvl(dh.end_date(+), :p_in_as_of_date_low + 1)
and ( dh.dispute_history_id is null
or dh.dispute_history_id =
(select max(dh2.dispute_history_id)
from ar_dispute_history dh2
where dh2.payment_schedule_id=ps.payment_schedule_id
and :p_in_as_of_date_low >= nvl(dh2.start_date(+), :p_in_as_of_date_low)
and :p_in_as_of_date_low < nvl(dh2.end_date(+), :p_in_as_of_date_low + 1)
)
)
and rct.customer_trx_id=ps.customer_trx_id
and gsob.set_of_books_id=rct.set_of_books_id
&lp_invoice_type_low
&lp_invoice_type_high
&lp_bal_seg_low
&lp_bal_seg_high
&lp_acc_seg_low
&lp_acc_seg_high
and ps.cons_inv_id=ci.cons_inv_id(+)
and nvl(ps.primary_salesrep_id,-3)=sales.salesrep_id
and sales.org_id=ps.org_id
and jrrev.resource_id=sales.resource_id
and xxen_ar_arxagrw_pkg.include_org_id(ps.org_id)='Y'
and gld.org_id=ps.org_id
and site.org_id=ps.org_id
and 2=2
union all
select
substrb(nvl(hp.party_name,:p_short_unid_phrase),1,50) cust_name,
hca.account_number cust_no,
nvl(sales.name,jrrev.resource_name),
initcap(:p_payment_meaning),
nvl(sales.salesrep_id,-3),
null term,
site.site_use_id,
loc.state cust_state,
loc.city cust_city,
(select ftv.territory_short_name from fnd_territories_vl ftv where ftv.territory_code=loc.country) cust_country,
xxen_util.meaning(hca.customer_class_code,'CUSTOMER CLASS',222) cust_class,
decode(decode(upper(RTRIM(RPAD(:p_in_format_option_low, 1))),'D','D',NULL),NULL,-1,acct_site.cust_acct_site_id) addr_id,
nvl(hca.cust_account_id, -999) cust_id,
hca.party_id,
--
&party_dff_cols3
&cust_dff_cols3
--
ps.payment_schedule_id,
app.class,
ps.trx_date,
ps.due_date,
decode(nvl2(:p_in_currency,'N','Y'), 'Y', ps.amount_due_original * nvl(ps.exchange_rate, 1), ps.amount_due_original) amt_due_original,
xxen_ar_arxagrw_pkg.Comp_Amt_Due_RemainingFormula
( app.class,
initcap(:p_payment_meaning),
ps.payment_schedule_id,
decode(nvl2(:p_in_currency,'N','Y'), 'Y', nvl(-SUM(app.acctd_amount), 0), nvl(-SUM(app.amount), 0)), -- amount due remaining
ps.amount_applied,
ps.amount_credited,
ps.amount_adjusted
) amt_due_remaining,
ps.trx_number,
ceil(:p_in_as_of_date_low - ps.trx_date) days_past_trx,
ceil(:p_in_as_of_date_low - ps.due_date) days_past_due,
ps.amount_adjusted,
ps.amount_applied,
ps.amount_credited,
ps.gl_date,
decode(ps.invoice_currency_code, gsob.currency_code, NULL, decode(ps.exchange_rate, NULL, '*', NULL)),
nvl(ps.exchange_rate, 1),
--
&lp_bucket_cols3
--
app.code_combination_id,
app.chart_of_accounts_id,
ci.cons_billing_number cons_billing_number,
initcap(:p_payment_meaning) invoice_type,
acr.comments,
ps.invoice_currency_code,
gsob.currency_code functional_currency,
gsob.name ledger,
ps.org_id
from
hz_cust_accounts hca,
hz_parties hp,
ar_payment_schedules ps,
ar_cons_inv ci,
ra_salesreps_all sales,
jtf_rs_resource_extns_vl jrrev,
hz_cust_site_uses site,
hz_cust_acct_sites acct_site,
hz_party_sites party_site,
hz_locations loc,
ar_cash_receipts acr,
gl_sets_of_books gsob,
(
select
c.code_combination_id,
c.chart_of_accounts_id,
ps.payment_schedule_id payment_schedule_id,
decode(app.applied_payment_schedule_id, -4, 'CLAIM', ps.class) class,
app.acctd_amount_applied_from acctd_amount,
app.amount_applied amount,
app.status status
from
ar_receivable_applications app,
gl_code_combinations c,
ar_payment_schedules ps
where
app.gl_date <= :p_in_as_of_date_low
--and upper(RTRIM(RPAD(:p_in_summary_option_low,1))) ='I'
and xxen_ar_arxagrw_pkg.include_org_id(ps.org_id)='Y'
and ps.cash_receipt_id=app.cash_receipt_id
and app.code_combination_id=c.code_combination_id
and app.status in ( 'ACC', 'UNAPP', 'UNID','OTHER ACC')
and nvl(app.confirmed_flag, 'Y')='Y'
and ps.gl_date_closed > :p_in_as_of_date_low
and ((app.reversal_gl_date is not null and
ps.gl_date <= :p_in_as_of_date_low
) or
app.reversal_gl_date is null
)
and decode(upper(:p_in_currency), null, ps.invoice_currency_code, upper(:p_in_currency))=ps.invoice_currency_code
and nvl( ps.receipt_confirmed_flag, 'Y' )='Y'
&lp_bal_seg_low
&lp_bal_seg_high
&lp_acc_seg_low
&lp_acc_seg_high
and app.org_id=ps.org_id
) app
where
ps.payment_schedule_id=app.payment_schedule_id
and ps.customer_id=hca.cust_account_id(+)
and hca.party_id=hp.party_id(+)
and ps.customer_site_use_id=site.site_use_id(+)
and site.cust_acct_site_id=acct_site.cust_acct_site_id(+)
and acct_site.party_site_id=party_site.party_site_id(+)
and loc.location_id(+)=party_site.location_id
and acr.cash_receipt_id=ps.cash_receipt_id
and gsob.set_of_books_id=acr.set_of_books_id
and ps.cons_inv_id=ci.cons_inv_id(+)
and sales.salesrep_id=-3
and sales.org_id =ps.org_id
and jrrev.resource_id=sales.resource_id
and site.org_id (+)=ps.org_id
and 2=2
group by
hp.party_name,
hca.account_number,
hca.customer_class_code,
site.site_use_id,
nvl(sales.name,jrrev.resource_name),
nvl(sales.salesrep_id,-3),
loc.state,
loc.city,
loc.country,
acct_site.cust_acct_site_id,
hca.cust_account_id,
hca.party_id,
--
&party_dff_cols3_g
&cust_dff_cols3_g
--
ps.payment_schedule_id,
ps.trx_date,
ps.due_date,
ps.trx_number,
ps.amount_due_original,
ps.amount_adjusted,
ps.amount_applied,
ps.amount_credited,
ps.gl_date,
ps.amount_in_dispute,
ps.amount_adjusted_pending,
ps.invoice_currency_code,
ps.exchange_rate,
app.class,
app.code_combination_id,
app.chart_of_accounts_id,
decode( app.status, 'UNID', 'UNID','OTHER ACC','OTHER ACC','UNAPP'),
ci.cons_billing_number ,
initcap(:p_payment_meaning),
acr.comments,
gsob.currency_code,
gsob.name,
ps.org_id
union all
select
substrb(nvl(hp.party_name, :p_short_unid_phrase),1,50) cust_name,
hca.account_number cust_no,
nvl(sales.name,jrrev.resource_name),
initcap(:p_risk_meaning),
nvl(sales.salesrep_id,-3),
null term,
site.site_use_id,
loc.state cust_state,
loc.city cust_city,
(select ftv.territory_short_name from fnd_territories_vl ftv where ftv.territory_code=loc.country) cust_country,
xxen_util.meaning(hca.customer_class_code,'CUSTOMER CLASS',222) cust_class,
decode(decode(upper(RTRIM(RPAD(:p_in_format_option_low, 1))),'D','D',NULL),NULL,-1,acct_site.cust_acct_site_id) addr_id,
nvl(hca.cust_account_id, -999) cust_id,
hca.party_id,
--
&party_dff_cols3
&cust_dff_cols3
--
ps.payment_schedule_id,
initcap(:p_risk_meaning),
ps.trx_date,
ps.due_date,
decode(nvl2(:p_in_currency,'N','Y'), 'Y', ps.amount_due_original * nvl(ps.exchange_rate, 1), ps.amount_due_original) amt_due_original,
xxen_ar_arxagrw_pkg.comp_amt_due_remainingformula
( initcap(:p_risk_meaning),
initcap(:p_risk_meaning),
ps.payment_schedule_id,
decode( nvl2(:p_in_currency,'N','Y'), 'Y', crh.acctd_amount, crh.amount), -- amount due remaining
ps.amount_applied,
ps.amount_credited,
ps.amount_adjusted
) amt_due_remaining,
ps.trx_number,
ceil(:p_in_as_of_date_low - ps.trx_date) days_past_trx,
ceil(:p_in_as_of_date_low - ps.due_date) days_past_due,
ps.amount_adjusted,
ps.amount_applied,
ps.amount_credited,
crh.gl_date,
decode(ps.invoice_currency_code, gsob.currency_code, NULL, decode(crh.exchange_rate, NULL, '*', NULL)),
nvl(crh.exchange_rate, 1),
--
&lp_bucket_cols4
--
c.code_combination_id,
c.chart_of_accounts_id,
ci.cons_billing_number cons_billing_number,
initcap(:p_risk_meaning) invoice_type,
cr.comments,
ps.invoice_currency_code,
gsob.currency_code functional_currency,
gsob.name ledger,
ps.org_id
from
hz_cust_accounts hca,
hz_parties hp,
ar_payment_schedules ps,
ar_cons_inv ci,
hz_cust_site_uses site,
hz_cust_acct_sites acct_site,
hz_party_sites party_site,
hz_locations loc,
ar_cash_receipts cr,
ar_cash_receipt_history crh,
gl_code_combinations c,
ra_salesreps_all sales,
jtf_rs_resource_extns_vl jrrev,
gl_sets_of_books gsob
where
crh.gl_date <= :p_in_as_of_date_low
--and upper(RTRIM(RPAD(:p_in_summary_option_low,1))) = 'I'
and upper(:p_risk_option) != 'NONE'
and ps.customer_id=hca.cust_account_id(+)
and hca.party_id=hp.party_id(+)
and ps.cash_receipt_id=cr.cash_receipt_id
and xxen_ar_arxagrw_pkg.include_org_id(ps.org_id)='Y'
and cr.cash_receipt_id=crh.cash_receipt_id
and crh.account_code_combination_id=c.code_combination_id
and ps.customer_site_use_id=site.site_use_id(+)
and site.cust_acct_site_id=acct_site.cust_acct_site_id(+)
and acct_site.party_site_id=party_site.party_site_id(+)
and loc.location_id(+)=party_site.location_id
and decode(upper(:p_in_currency), NULL, ps.invoice_currency_code, upper(:p_in_currency))=ps.invoice_currency_code
and (crh.current_record_flag='Y' or crh.reversal_gl_date > :p_in_as_of_date_low )
and crh.status not in (decode(crh.factor_flag,'Y','RISK_ELIMINATED','N','CLEARED'),'REVERSED')
and not exists (select 'x'
from ar_receivable_applications ra
where ra.cash_receipt_id=cr.cash_receipt_id
and ra.status='ACTIVITY'
and applied_payment_schedule_id=-2)
and cr.cash_receipt_id not in
(select ps.reversed_cash_receipt_id
from ar_payment_schedules ps
where ps.reversed_cash_receipt_id=cr.cash_receipt_id
and ps.class='DM'
and ps.gl_date<= (:p_in_as_of_date_low))
and gsob.set_of_books_id=cr.set_of_books_id
&lp_bal_seg_low
&lp_bal_seg_high
&lp_acc_seg_low
&lp_acc_seg_high
and ps.cons_inv_id=ci.cons_inv_id(+)
and sales.salesrep_id=-3
and sales.org_id=ps.org_id
and jrrev.resource_id=sales.resource_id
and crh.org_id=ps.org_id
and cr.org_id=ps.org_id
and site.org_id (+)=ps.org_id
and 2=2
union all
select
substrb(hp.party_name,1,50) cust_name,
hca.account_number cust_no,
nvl(sales.name,jrrev.resource_name) sort_field1,
arpt_sql_func_util.get_org_trx_type_details(ps.cust_trx_type_id,ps.org_id) sort_field2,
nvl(sales.salesrep_id, -3) salesrep_id,
rt.name term,
site.site_use_id contact_site_id,
loc.state cust_state,
loc.city cust_city,
(select ftv.territory_short_name from fnd_territories_vl ftv where ftv.territory_code=loc.country) cust_country,
xxen_util.meaning(hca.customer_class_code,'CUSTOMER CLASS',222) cust_class,
decode(decode(upper(rtrim(rpad(:p_in_format_option_low, 1))),'D','D',null),null,-1,acct_site.cust_acct_site_id) addr_id,
nvl(hca.cust_account_id,-999) cust_id,
hca.party_id,
--
&party_dff_cols3
&cust_dff_cols3
--
ps.payment_schedule_id payment_sched_id,
ps.class class,
ps.trx_date,
ps.due_date,
decode(nvl2(:p_in_currency,'N','Y'), 'Y', ps.amount_due_original * nvl(ps.exchange_rate, 1), ps.amount_due_original) amt_due_original,
xxen_ar_arxagrw_pkg.comp_amt_due_remainingformula
( ps.class,
arpt_sql_func_util.get_org_trx_type_details(ps.cust_trx_type_id,ps.org_id),
ps.payment_schedule_id,
decode( nvl2(:p_in_currency,'N','Y'), 'Y', ps.acctd_amount_due_remaining,ps.amount_due_remaining), -- amount due remaining
ps.amount_applied,
ps.amount_credited,
ps.amount_adjusted
) amt_due_remaining,
ps.trx_number invnum,
ceil(:p_in_as_of_date_low - ps.trx_date) days_past_trx,
ceil(:p_in_as_of_date_low - ps.due_date) days_past_due,
ps.amount_adjusted amount_adjusted,
ps.amount_applied amount_applied,
ps.amount_credited amount_credited,
ps.gl_date gl_date,
decode(ps.invoice_currency_code, gsob.currency_code, NULL, decode(ps.exchange_rate, NULL, '*', NULL)) data_converted,
nvl(ps.exchange_rate, 1) ps_exchange_rate,
--
&lp_bucket_cols3
--
c.code_combination_id,
c.chart_of_accounts_id,
ci.cons_billing_number cons_billing_number,
arpt_sql_func_util.get_org_trx_type_details(ps.cust_trx_type_id,ps.org_id) invoice_type,
ct.comments,
ps.invoice_currency_code,
gsob.currency_code functional_currency,
gsob.name ledger,
ps.org_id
from
hz_cust_accounts hca,
hz_parties hp,
ar_payment_schedules ps,
ar_cons_inv ci,
ra_salesreps_all sales,
jtf_rs_resource_extns_vl jrrev,
hz_cust_site_uses site,
hz_cust_acct_sites acct_site,
hz_party_sites party_site,
hz_locations loc,
ar_transaction_history th,
ar_distributions dist,
gl_code_combinations c,
ra_customer_trx ct,
ra_terms rt,
gl_sets_of_books gsob
where
ps.gl_date <= :p_in_as_of_date_low
--and upper(RTRIM(RPAD(:p_in_summary_option_low,1)) )='I'
and ps.customer_site_use_id=site.site_use_id
and xxen_ar_arxagrw_pkg.include_org_id(ps.org_id)='Y'
and site.cust_acct_site_id=acct_site.cust_acct_site_id
and acct_site.party_site_id =party_site.party_site_id
and loc.location_id=party_site.location_id
and ps.gl_date_closed > :p_in_as_of_date_low
and ps.class='BR'
and decode(upper(:p_in_currency),NULL, ps.invoice_currency_code, upper(:p_in_currency))=ps.invoice_currency_code
and hca.party_id=hp.party_id
and th.transaction_history_id =
(select max(transaction_history_id)
from ar_transaction_history th2,
ar_distributions dist2
where th2.transaction_history_id=dist2.source_id
and dist2.source_table='TH'
and th2.gl_date <= :p_in_as_of_date_low
and dist2.amount_dr is not null
and th2.customer_trx_id=ps.customer_trx_id)
and th.transaction_history_id=dist.source_id
and dist.source_table='TH'
and dist.amount_dr is not null
and dist.source_table_secondary is NULL
and dist.code_combination_id=c.code_combination_id
and gsob.set_of_books_id=ct.set_of_books_id
&lp_invoice_type_low
&lp_invoice_type_high
&lp_bal_seg_low
&lp_bal_seg_high
&lp_acc_seg_low
&lp_acc_seg_high
and ps.cons_inv_id=ci.cons_inv_id(+)
and ps.customer_id=hca.cust_account_id
and ps.customer_trx_id=ct.customer_trx_id
and ct.customer_trx_id=th.customer_trx_id
and nvl(ct.primary_salesrep_id,-3)=sales.salesrep_id
and sales.org_id=ct.org_id
and jrrev.resource_id=sales.resource_id
and ct.org_id=ps.org_id
and site.org_id=ps.org_id
and ps.term_id=rt.term_id (+)
and :p_br_enabled='Y'
and 2=2
) x,
hz_customer_profiles hcpa,
hz_customer_profiles hcps,
(select
hcpam.cust_account_profile_id,
listagg(hcpam.currency_code||'='||to_char(hcpam.overall_credit_limit),',') within group (order by hcpam.currency_code) as credit_limits,
listagg(hcpam.currency_code||'='||to_char(hcpam.trx_credit_limit),',') within group (order by hcpam.currency_code) as order_credit_limits
from
hz_cust_profile_amts hcpam
where
hcpam.overall_credit_limit is not null or hcpam.trx_credit_limit is not null
group by
hcpam.cust_account_profile_id
) hcpamv
where
hcpa.cust_account_id (+)=x.cust_id and
hcpa.site_use_id (+) is null and
hcps.cust_account_id (+)=x.cust_id and
hcps.site_use_id (+)=x.site_use_id and
hcpa.cust_account_profile_id=hcpamv.cust_account_profile_id(+)and
nvl(x.amt_due_remaining,0) != 0
) x1
where
1=1
group by
x1.ledger,
x1.operating_unit,
x1.code_combination_id,
x1.chart_of_accounts_id,
x1.sort_field1,
x1.cust_name,
x1.cust_no,
x1.cust_country,
x1.cust_class,
x1.credit_limits,
x1.order_credit_limits,
--
&party_dff_cols1_g
&cust_dff_cols1_g
--
x1.collector,
x1.revaluation_from_currency,
x1.reval_conv_rate
&lp_invoice_cols_g
order by
fnd_flex_xml_publisher_apis.process_kff_combination_1('lp_acct_flex_bal_seg', 'SQLGL', 'GL#', x1.chart_of_accounts_id, NULL, x1.code_combination_id, 'GL_BALANCING', 'Y', 'VALUE'),
x1.sort_field1,
x1.cust_name,
x1.cust_no
&lp_invoice_cols_g