-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathCST COGS Revenue Matching.sql
More file actions
701 lines (688 loc) · 24.1 KB
/
CST COGS Revenue Matching.sql
File metadata and controls
701 lines (688 loc) · 24.1 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
/*************************************************************************/
/* */
/* (c) 2010-2024 Enginatics GmbH */
/* www.enginatics.com */
/* */
/*************************************************************************/
-- Report Name: CST COGS Revenue Matching
-- Description: Imported from Concurrent Program
Application: Bills of Material
Source: COGS Revenue Matching Report
Short Name: CSTRCMRX
The COGS/Revenue Matching Report displays earned and unearned (deferred) revenue, and cost of goods sold amounts for sales orders issues specified in the report's run-time parameters.
The report displays shipped sales order and associated sales order lines and shows the accounts where the earned and deferred COGS were charged.
The report is based on the Revenue and COGS Matching functionality delivered in Oracle EBS R12. Please refer to the following documentation regarding this functionality:
- Oracle Cost Management User Guide, Section: Revenue and COGS Matching
- MOS Document 1060202.1 Understanding COGS and DCOGS Recognition Accounting
Revenue / COGS Recognition Process Flow
=======================================
When you ship confirm one or more order lines in Oracle Order Management and then run the applicable Cost Management cost and accounting processes, the cost of goods sold associated with the sales order line is immediately debited to a Deferred COGS account pending the invoicing and recognition of the sales order revenue in Oracle Receivables.
When Oracle Receivables recognizes all or part of the sales revenue associated with a sales order line, you run a cost process that calculates the percentage of total billed revenue recognized. Oracle Inventory then creates a cost recognition transaction that adjusts the Deferred COGS and regular COGS amount for the order line. The proportion of total shipment cost that is recognized as COGS will always match the proportion of total billable quantity that is recognized as revenue.
Revenue / COGS Recognition Concurrent Processes
================================================
It is recommended the Revenue and COGS concurrent processes be run in the following order:
Run the AR Concurrent Processes first:
- Autoinvoice Master Program. Run autoinvoice to generate the invoice transactions.
- Revenue Recognition. Run the Revenue Recognition Master Program to generate the AR revenue recognition
Then the COGS Concurrent Processes:
- Record Order Management Transactions
The Record Order Management Transactions concurrent process picks up and costs all uncosted sales order issue and RMA return transactions and creates a record for each new order line in the costing COGS recognition matching table. This process is not for Perpetual Discrete Costing (Standard, Average, FIFO). In Discrete Costing, the cost processor selects and costs the uncosted sales order issues and inserts them into the COGS matching table
- Collect Revenue Recognition Information
The Collect Revenue Recognition Information concurrent process calls an Oracle Receivables API to retrieve the latest revenue recognition percentage of all invoiced sales order lines in Oracle receivables for a specific ledger and with activity dates within a user-specified date range. This process must be run before the Generate COGS recognition Event concurrent process.
- Generate COGS Recognition Events
The Generate COGS Recognition Events concurrent request compares the COGS recognition percentage for each sales order line and accounting period combination to the current earned revenue percentage. When the compared percentages are different, the process raises a COGS recognition event and creates a COGS recognition transaction in Oracle Inventory that adjusts the ratio of earned and deferred COGS to match that of earned and deferred revenue. You must run this process after completion of the Collect Revenue Recognition Information concurrent process.
-- Excel Examle Output: https://www.enginatics.com/example/cst-cogs-revenue-matching/
-- Library Link: https://www.enginatics.com/reports/cst-cogs-revenue-matching/
-- Run Report: https://demo.enginatics.com/
with
perpetual_qry_1 as
(
select
x.*
from
(
select
crcml.operating_unit_id,
haou.name operating_unit,
gl.name ledger,
gl.currency_code currency,
ooha.order_number,
ooha.booked_date,
ooha.transactional_curr_code,
oola.line_number,
oola.sold_to_org_id,
oola.item_type_code,
oola.order_quantity_uom uom,
crcml.sales_order_issue_date,
crcml.revenue_om_line_id,
crcml.organization_id,
crcml.deferred_cogs_acct_id,
crcml.cogs_om_line_id,
crcml.inventory_item_id,
crcml.unit_cost,
crcml.unit_material_cost,
crcml.unit_moh_cost,
crcml.unit_op_cost,
crcml.unit_resource_cost,
crcml.unit_overhead_cost,
sum(decode(mta.accounting_line_type, 35, mta.base_transaction_value,0)) cogs_balance,
last_value(sum(decode(mta.accounting_line_type, 35, mta.base_transaction_value,0))) -- CST_ACCOUNTING_LINE_TYPE 35 = Cost of Goods Sold
over ( partition by
crcml.operating_unit_id,
haou.name,
gl.name,
gl.currency_code,
ooha.order_number,
ooha.booked_date,
ooha.transactional_curr_code,
oola.line_number,
oola.sold_to_org_id,
oola.item_type_code,
oola.order_quantity_uom,
crcml.sales_order_issue_date,
crcml.revenue_om_line_id,
crcml.organization_id,
crcml.deferred_cogs_acct_id,
crcml.cogs_om_line_id
order by
crcml.operating_unit_id,
haou.name,
gl.name,
gl.currency_code,
ooha.order_number,
ooha.booked_date,
ooha.transactional_curr_code,
oola.line_number,
oola.sold_to_org_id,
oola.item_type_code,
oola.order_quantity_uom,
crcml.sales_order_issue_date,
crcml.revenue_om_line_id,
crcml.organization_id,
crcml.deferred_cogs_acct_id,
crcml.cogs_om_line_id
) total_cogs_balance,
sum(decode(mta.accounting_line_type, 36, mta.base_transaction_value,0)) def_cogs_balance,
last_value(sum(decode(mta.accounting_line_type, 36, mta.base_transaction_value,0))) -- CST_ACCOUNTING_LINE_TYPE 36 = Deferred Cost of Goods Sold
over( partition by
crcml.operating_unit_id,
haou.name,
gl.name,
gl.currency_code,
ooha.order_number,
ooha.booked_date,
ooha.transactional_curr_code,
oola.line_number,
oola.sold_to_org_id,
oola.item_type_code,
oola.order_quantity_uom,
crcml.sales_order_issue_date,
crcml.revenue_om_line_id,
crcml.organization_id,
crcml.deferred_cogs_acct_id,
crcml.cogs_om_line_id
order by
crcml.operating_unit_id,
haou.name,
gl.name,
gl.currency_code,
ooha.order_number,
ooha.booked_date,
ooha.transactional_curr_code,
oola.line_number,
oola.sold_to_org_id,
oola.item_type_code,
oola.order_quantity_uom,
crcml.sales_order_issue_date,
crcml.revenue_om_line_id,
crcml.organization_id,
crcml.deferred_cogs_acct_id,
crcml.cogs_om_line_id
) total_def_cogs_balance
from
cst_revenue_cogs_match_lines crcml,
cst_cogs_events cce,
oe_order_lines_all oola,
oe_order_headers_all ooha,
mtl_transaction_accounts mta,
gl_ledgers gl,
hr_all_organization_units haou
where
1=1 and
crcml.ledger_id = :p_ledger_id and
crcml.pac_cost_type_id is null and
cce.event_date<=:p_gps_end_dt and
cce.event_date>=:p_gps_start_dt and
cce.cogs_om_line_id = crcml.cogs_om_line_id and
gl.ledger_id = crcml.ledger_id and
oola.header_id = ooha.header_id and
oola.line_id = crcml.cogs_om_line_id and
mta.transaction_id (+) = cce.mmt_transaction_id and
haou.organization_id (+) = crcml.operating_unit_id
group by
crcml.operating_unit_id,
haou.name,
gl.name,
gl.currency_code,
ooha.order_number,
ooha.booked_date,
ooha.transactional_curr_code,
oola.line_number,
oola.sold_to_org_id,
oola.item_type_code,
oola.order_quantity_uom,
crcml.sales_order_issue_date,
crcml.revenue_om_line_id,
crcml.organization_id,
crcml.deferred_cogs_acct_id,
crcml.cogs_om_line_id,
crcml.inventory_item_id,
crcml.unit_cost,
crcml.unit_material_cost,
crcml.unit_moh_cost,
crcml.unit_op_cost,
crcml.unit_resource_cost,
crcml.unit_overhead_cost
) x
),
perpetual_qry_2 as
(
select
pq1.operating_unit_id,
pq1.operating_unit,
pq1.ledger,
pq1.currency ledger_currency,
pq1.order_number order_number,
pq1.booked_date order_date,
substrb(hp.party_name,1,50) customer_name,
pq1.transactional_curr_code order_currency,
pq1.line_number sales_order_line,
pq1.sales_order_issue_date,
pq1.revenue_om_line_id sales_order_line_id,
pq1.item_type_code item_type_code,
msiv.concatenated_segments item,
msiv.description item_description,
xxen_util.meaning(msiv.item_type,'ITEM_TYPE',3) user_item_type,
msiv.inventory_item_id,
mp.organization_code,
mp.organization_id,
rctla.line_number invoice_line,
rctla.unit_selling_price,
rctla.customer_trx_line_id,
rctla.customer_trx_id,
sum(cce.event_quantity) total_line_quantity,
decode(sum(cce.event_quantity),0,0,sum(cce.event_quantity * crcml.unit_cost) / sum(cce.event_quantity))item_cost,
decode(sum(cce.event_quantity),0,0,sum(cce.event_quantity * crcml.unit_material_cost) / sum(cce.event_quantity))material_cost,
decode(sum(cce.event_quantity),0,0,sum(cce.event_quantity * crcml.unit_moh_cost) / sum(cce.event_quantity))material_oh_cost,
decode(sum(cce.event_quantity),0,0,sum(cce.event_quantity * crcml.unit_op_cost) / sum(cce.event_quantity))osp_cost,
decode(sum(cce.event_quantity),0,0,sum(cce.event_quantity * crcml.unit_resource_cost) / sum(cce.event_quantity))resource_cost,
decode(sum(cce.event_quantity),0,0,sum(cce.event_quantity * crcml.unit_overhead_cost) / sum(cce.event_quantity))overhead_cost,
pq1.uom,
pq1.cogs_balance earned_cogs,
pq1.total_cogs_balance total_earned_cogs,
pq1.def_cogs_balance deferred_cogs,
pq1.total_def_cogs_balance total_deferred_cogs,
gcck_cogs.concatenated_segments cogs_account,
gcck_dcogs.concatenated_segments deferred_cogs_account,
pq1.cogs_om_line_id cogs_om_line_id,
gcck_cogs.code_combination_id cogs_account_ccid,
gcck_cogs.chart_of_accounts_id,
cce.event_date gl_date
from
perpetual_qry_1 pq1,
cst_revenue_cogs_match_lines crcml,
mtl_system_items_vl msiv,
mtl_parameters mp,
gl_code_combinations_kfv gcck_cogs,
gl_code_combinations_kfv gcck_dcogs,
ra_customer_trx_lines_all rctla,
hz_cust_accounts hca,
hz_parties hp,
cst_cogs_events cce
where
msiv.inventory_item_id = pq1.inventory_item_id and
msiv.organization_id = pq1.organization_id and
mp.organization_id = pq1.organization_id and
gcck_cogs.code_combination_id = crcml.cogs_acct_id and
gcck_dcogs.code_combination_id = pq1.deferred_cogs_acct_id and
rctla.line_type (+) = 'LINE' and
rctla.interface_line_context (+) = 'ORDER ENTRY' and
rctla.interface_line_attribute6 (+) = to_char(pq1.revenue_om_line_id) and
rctla.sales_order (+) = to_char(pq1.order_number) and
pq1.sold_to_org_id = hca.cust_account_id (+)and
hca.party_id = hp.party_id and
cce.cogs_om_line_id = pq1.cogs_om_line_id and
cce.event_type in (1,2) and
cce.event_date<=:p_gps_end_dt and
cce.event_date>=:p_gps_start_dt and
crcml.cogs_om_line_id = pq1.cogs_om_line_id and
crcml.pac_cost_type_id is null and
( :p_all_lines = 'Y' or pq1.def_cogs_balance not between -1*:p_amt_tolerance and :p_amt_tolerance)
group by
pq1.operating_unit_id,
pq1.operating_unit,
pq1.ledger,
pq1.currency,
pq1.order_number,
pq1.booked_date,
substrb(hp.party_name,1,50),
pq1.transactional_curr_code,
pq1.line_number,
pq1.sales_order_issue_date,
pq1.revenue_om_line_id,
pq1.item_type_code,
pq1.uom,
msiv.concatenated_segments,
msiv.description,
msiv.item_type,
msiv.inventory_item_id,
mp.organization_code,
mp.organization_id,
rctla.line_number,
rctla.unit_selling_price,
rctla.customer_trx_line_id,
rctla.customer_trx_id,
pq1.cogs_balance,
pq1.total_cogs_balance,
pq1.def_cogs_balance,
pq1.total_def_cogs_balance,
gcck_cogs.concatenated_segments,
gcck_dcogs.concatenated_segments,
pq1.cogs_om_line_id,
gcck_cogs.code_combination_id,
gcck_cogs.chart_of_accounts_id,
cce.event_date
),
perpetual_qry as
(
select
pq2.operating_unit_id,
pq2.operating_unit,
pq2.ledger,
pq2.ledger_currency,
pq2.order_number,
pq2.order_date,
pq2.customer_name,
pq2.order_currency,
pq2.sales_order_line,
pq2.sales_order_issue_date,
pq2.sales_order_line_id,
pq2.item_type_code,
pq2.item,
pq2.item_description,
pq2.user_item_type,
pq2.inventory_item_id,
pq2.organization_code,
pq2.organization_id,
pq2.invoice_line,
pq2.unit_selling_price,
pq2.customer_trx_line_id,
pq2.customer_trx_id,
pq2.total_line_quantity,
pq2.item_cost,
pq2.material_cost,
pq2.material_oh_cost,
pq2.osp_cost,
pq2.resource_cost,
pq2.overhead_cost,
pq2.uom,
pq2.earned_cogs,
case when pq2.item_type_code = 'INCLUDED'
then (select
sum(pq2_1.earned_cogs)
from
perpetual_qry_2 pq2_1
where
pq2_1.operating_unit_id = pq2.operating_unit_id and
pq2_1.order_number = pq2.order_number and
pq2_1.sales_order_line = pq2.sales_order_line and
pq2_1.sales_order_line_id = pq2.sales_order_line_id and
pq2_1.item_type_code = 'INCLUDED' and
pq2_1.customer_trx_line_id = pq2.customer_trx_line_id and
pq2_1.customer_trx_id = pq2.customer_trx_id
)
else pq2.total_earned_cogs
end total_earned_cogs,
pq2.deferred_cogs,
pq2.total_deferred_cogs,
pq2.cogs_account,
pq2.deferred_cogs_account,
pq2.cogs_om_line_id,
pq2.cogs_account_ccid,
pq2.chart_of_accounts_id,
pq2.gl_date
from
perpetual_qry_2 pq2
),
perpetual_lines_qry as
(
select /*+ leading(pq,rctla) index(rctla ra_customer_trx_lines_n9)*/
rctla.interface_line_attribute1,
rctla.interface_line_attribute6,
rcta.trx_number,
rctla.line_number,
rctla.customer_trx_id,
row_number() over (partition by rctla.sales_order,rctla.interface_line_attribute6,rctla.interface_line_context,rctla.line_type order by rctla.customer_trx_id) rctla_rank
from
ra_customer_trx_lines_all rctla,
ra_customer_trx_all rcta,
perpetual_qry pq
where
rctla.interface_line_context='ORDER ENTRY' and
rctla.line_type ='LINE' and
rctla.customer_trx_id = rcta.customer_trx_id and
rctla.sales_order = to_char(pq.order_number) and
rctla.interface_line_attribute6 = to_char(pq.sales_order_line_id)
)
--
-- Main Query Starts Here
--
select
x.ledger,
x.operating_unit,
x.order_number,
x.order_date,
x.customer,
x.order_currency,
x.order_line,
x.order_quantity,
x.uom,
x.sales_order_issue_date,
x.invoice_number,
x.quantity_invoiced,
x.invoice_line,
x.item,
x.item_description,
&category_columns
x.user_item_type,
x.organization_code,
(select mp.organization_code from mtl_parameters mp,oe_order_lines_all oola where oola.line_id=x.cogs_om_line_id and oola.ship_from_org_id=mp.organization_id)ship_from_warehouse,
x.ledger_currency,
:p_period_name period,
x.unit_selling_price,
x.item_cost,
x.material_cost,
x.material_oh_cost,
x.osp_cost,
x.resource_cost,
x.overhead_cost,
&lp_cost_type_col
x.earned_revenue,
x.unearned_revenue,
x.unbilled_revenue,
case when x.earned_revenue is not null and x.unearned_revenue is not null and x.unbilled_revenue is not null
then x.earned_revenue + x.unearned_revenue + x.unbilled_revenue
end total_revenue,
case when x.earned_revenue is not null and x.unearned_revenue is not null and x.unbilled_revenue is not null
then case when x.earned_revenue + x.unearned_revenue + x.unbilled_revenue = 0
then 100
else x.earned_revenue/(x.earned_revenue + x.unearned_revenue + x.unbilled_revenue)*100
end
end revenue_percent,
x.earned_cogs,
x.deferred_cogs,
x.earned_cogs+x.deferred_cogs total_cogs,
(select actual_shipment_date from oe_order_lines_all where line_id = x.sales_order_line_id) so_line_closed_date,
x.gl_date,
case when x.earned_cogs is not null and x.deferred_cogs is not null
then case when x.earned_cogs + x.deferred_cogs = 0
then 100
else x.earned_cogs/(x.earned_cogs + x.deferred_cogs)*100
end
end cogs_percent,
x.cogs_account,
x.deferred_cogs_account,
case when x.revenue_ccid is not null then fnd_flex_xml_publisher_apis.process_kff_combination_1('seg','SQLGL','GL#',x.chart_of_accounts_id,NULL,x.revenue_ccid,'ALL','Y','VALUE') end revenue_account_segments,
case when x.unearn_ccid is not null then fnd_flex_xml_publisher_apis.process_kff_combination_1('seg','SQLGL','GL#',x.chart_of_accounts_id,NULL,x.unearn_ccid,'ALL','Y','VALUE') end unearned_account_segments,
case when x.unbill_ccid is not null then fnd_flex_xml_publisher_apis.process_kff_combination_1('seg','SQLGL','GL#',x.chart_of_accounts_id,NULL,x.unbill_ccid,'ALL','Y','VALUE') end unbilled_account_segments,
&lp_cogs_account_segments
&lp_rev_account_segments
x.cogs_om_line_id
from
(
select
pq.ledger,
pq.ledger_currency,
pq.operating_unit,
pq.order_number,
pq.order_date,
pq.customer_name customer,
pq.order_currency,
pq.sales_order_line order_line,
decode(rcta.trx_number,min(plq.trx_number),decode(pq.invoice_line,min(plq.min_inv_line),pq.total_line_quantity)) order_quantity,
pq.uom,
pq.sales_order_issue_date,
rcta.trx_number invoice_number,
rctla.quantity_invoiced,
pq.invoice_line,
pq.unit_selling_price,
pq.item_cost,
pq.material_cost,
pq.material_oh_cost,
pq.osp_cost,
pq.resource_cost,
pq.overhead_cost,
pq.item,
pq.item_description,
pq.user_item_type,
pq.inventory_item_id,
pq.organization_code,
pq.organization_id,
round(sum(decode(axclv.account_class,'UNEARN',0,'UNBILL',0, axclv.acctd_amount))*decode(pq.earned_cogs, pq.total_earned_cogs,1,pq.earned_cogs/decode(pq.total_earned_cogs,0,1,pq.total_earned_cogs)),2) earned_revenue,
round(sum(decode(axclv.account_class,'REV', 0,'UNBILL',0, axclv.acctd_amount))*decode(pq.deferred_cogs, pq.total_deferred_cogs,1,pq.deferred_cogs/decode(pq.total_deferred_cogs,0,1,pq.total_deferred_cogs)),2) unearned_revenue,
round(sum(decode(axclv.account_class,'REV', 0,'UNEARN',0, axclv.acctd_amount))*decode(pq.deferred_cogs, pq.total_deferred_cogs,1,pq.deferred_cogs/decode(pq.total_deferred_cogs,0,1,pq.total_deferred_cogs)),2) unbilled_revenue,
decode(rcta.trx_number,min(plq.trx_number),decode(pq.invoice_line,min(plq.min_inv_line),pq.earned_cogs)) earned_cogs,
decode(rcta.trx_number,min(plq.trx_number),decode(pq.invoice_line,min(plq.min_inv_line),pq.deferred_cogs)) deferred_cogs,
pq.cogs_account,
pq.deferred_cogs_account,
min(decode(axclv.account_class,'REV',axclv.code_combination_id)) revenue_ccid,
min(decode(axclv.account_class,'UNEARN',axclv.code_combination_id)) unearn_ccid,
min(decode(axclv.account_class,'UNBILL',axclv.code_combination_id)) unbill_ccid,
plq.interface_line_attribute6,
pq.sales_order_line_id,
pq.cogs_om_line_id,
pq.cogs_account_ccid,
pq.chart_of_accounts_id,
axclv.gl_date
from
perpetual_qry pq,
ar_xla_ctlgd_lines_v axclv,
ra_customer_trx_lines_all rctla,
ra_customer_trx_all rcta,
(
select
plq.interface_line_attribute1,
plq.interface_line_attribute6,
plq.trx_number,
min(plq.line_number) min_inv_line
from
perpetual_lines_qry plq
where
plq.rctla_rank=1
group by
plq.interface_line_attribute1,
plq.interface_line_attribute6,
plq.trx_number
) plq
where
(pq.customer_trx_line_id = rctla.customer_trx_line_id or pq.customer_trx_line_id = rctla.previous_customer_trx_line_id) and
pq.customer_trx_id = rcta.customer_trx_id and
axclv.customer_trx_line_id = rctla.customer_trx_line_id and
axclv.account_set_flag = 'N' and
axclv.account_class in ('REV', 'UNEARN', 'UNBILL') and
axclv.gl_date<=:p_gps_end_dt and
axclv.gl_date>=:p_gps_start_dt and
to_char(pq.sales_order_line_id)=rctla.interface_line_attribute6 and
to_char(pq.sales_order_line_id)=plq.interface_line_attribute6
group by
pq.ledger,
pq.ledger_currency,
pq.operating_unit,
pq.order_number,
pq.order_date,
pq.customer_name,
pq.order_currency,
pq.sales_order_line,
pq.sales_order_issue_date,
rcta.trx_number,
rctla.quantity_invoiced,
pq.invoice_line,
pq.unit_selling_price,
pq.item_cost,
pq.material_cost,
pq.material_oh_cost,
pq.osp_cost,
pq.resource_cost,
pq.overhead_cost,
pq.item,
pq.item_description,
pq.user_item_type,
pq.inventory_item_id,
pq.organization_code,
pq.organization_id,
pq.total_line_quantity,
pq.uom,
pq.earned_cogs,
pq.total_earned_cogs,
pq.deferred_cogs,
pq.total_deferred_cogs,
pq.cogs_account,
pq.deferred_cogs_account,
plq.interface_line_attribute6,
pq.sales_order_line_id,
pq.cogs_om_line_id,
pq.cogs_account_ccid,
pq.chart_of_accounts_id,
axclv.gl_date
having
:p_all_lines = 'Y' or
(decode(sum(rctla.revenue_amount),0,1,round(sum(decode(axclv.account_class,'UNEARN',0,'UNBILL',0, axclv.amount))/(sum(rctla.revenue_amount)/count(axclv.cust_trx_line_gl_dist_id)),3)) <>
decode(pq.earned_cogs,0,decode(pq.deferred_cogs,0,1,0),round((pq.earned_cogs/decode(pq.deferred_cogs+pq.earned_cogs,0,1,pq.deferred_cogs+pq.earned_cogs)),3)) and
pq.deferred_cogs not between -1*:p_amt_tolerance and :p_amt_tolerance
)
union
select
pq.ledger,
pq.ledger_currency,
pq.operating_unit,
pq.order_number,
pq.order_date,
pq.customer_name customer,
pq.order_currency,
pq.sales_order_line order_line,
pq.total_line_quantity order_quantity,
pq.uom,
pq.sales_order_issue_date,
null invoice_number,
null quantity_invoiced,
null invoice_line,
null unit_selling_price,
pq.item_cost,
pq.material_cost,
pq.material_oh_cost,
pq.osp_cost,
pq.resource_cost,
pq.overhead_cost,
pq.item,
pq.item_description,
pq.user_item_type,
pq.inventory_item_id,
pq.organization_code,
pq.organization_id,
null earned_revenue,
null unearned_revenue,
null unbilled_revenue,
pq.earned_cogs,
pq.deferred_cogs,
pq.cogs_account,
pq.deferred_cogs_account,
null revenue_ccid,
null unearn_ccid,
null unbill_ccid,
null interface_line_attribute6,
pq.sales_order_line_id,
pq.cogs_om_line_id,
pq.cogs_account_ccid,
pq.chart_of_accounts_id,
pq.gl_date
from
perpetual_qry pq
where
(pq.customer_trx_line_id is null or pq.customer_trx_id is null) and
(:p_all_lines = 'Y' or pq.deferred_cogs not between -1*:p_amt_tolerance and :p_amt_tolerance)
union
select
pq.ledger,
pq.ledger_currency,
pq.operating_unit,
pq.order_number,
pq.order_date,
pq.customer_name customer,
pq.order_currency,
pq.sales_order_line order_line,
pq.total_line_quantity order_quantity,
pq.uom,
pq.sales_order_issue_date,
null invoice_number,
null quantity_invoiced,
null invoice_line,
null unit_selling_price,
pq.item_cost,
pq.material_cost,
pq.material_oh_cost,
pq.osp_cost,
pq.resource_cost,
pq.overhead_cost,
pq.item,
pq.item_description,
pq.user_item_type,
pq.inventory_item_id,
pq.organization_code,
pq.organization_id,
null earned_revenue,
null unearned_revenue,
null unbilled_revenue,
pq.earned_cogs,
pq.deferred_cogs,
pq.cogs_account,
pq.deferred_cogs_account,
null revenue_ccid,
null unearn_ccid,
null unbill_ccid,
null interface_line_attribute6,
pq.sales_order_line_id,
pq.cogs_om_line_id,
pq.cogs_account_ccid,
pq.chart_of_accounts_id,
pq.gl_date
from
perpetual_qry pq
where
pq.customer_trx_line_id is not null and
pq.customer_trx_id is not null and
not exists
(
select /*+ use_concat no_unnest leading(rctla) use_nl(rctla,rctlgda) */
null
from
ra_customer_trx_lines_all rctla,
ra_cust_trx_line_gl_dist_all rctlgda
where
(pq.customer_trx_line_id = rctla.customer_trx_line_id or pq.customer_trx_line_id = rctla.previous_customer_trx_line_id) and
rctla.customer_trx_line_id = rctlgda.customer_trx_line_id and
rctlgda.account_set_flag = 'N' and
rctlgda.account_class in ('REV', 'UNEARN', 'UNBILL') and
rctlgda.gl_date<=:p_gps_end_dt and
rctlgda.gl_date>=:p_gps_start_dt
) and
(:p_all_lines = 'Y' or pq.deferred_cogs not between -1*:p_amt_tolerance and :p_amt_tolerance)
) x
where
3=3 and
:p_ledger=:p_ledger and
:p_cost_method=1 and
nvl(:p_cost_type,'?')=nvl(:p_cost_type,'?')
order by
x.order_number,
x.order_line,
nvl2(order_quantity,1,2)