Select /*+FIRST_ROWS */
ot.trx_number,
ot.customer_id,
ot.customer_number,
ot.class,
substrb(ot.class_meaning, 1, 30) class_meaning,
ot.trx_type,
ot.cust_trx_type_id,
ot.trx_due_date trx_due_date,
/* substrb(decode(sign(ot.payment_schedule_id),
-1,
:app_main.currency_code,
ot.invoice_currency_code),
1,
15) invoice_currency_code,*/
ot.balance_due_curr,
decode(sign(ot.payment_schedule_id),
-1,
to_number(Null),
ot.balance_due_functional) balance_due_functional,
ot.customer_name,
ot.location,
decode(sign(ot.payment_schedule_id),
-1,
to_number(Null),
ot.trx_exchange_rate) trx_exchange_rate,
ot.trx_date trx_date,
decode(sign(ot.payment_schedule_id), -1, to_date(Null), ot.trx_gl_date) trx_gl_date,
ot.calc_discount_on_lines_flag,
ot.partial_discount_flag,
ot.allow_overapplication_flag,
ot.natural_application_only_flag,
ot.creation_sign,
ot.payment_schedule_id,
ot.terms_sequence_number,
ot.customer_trx_id,
/*greatest(to_date(:app_main.cr_gl_date),
ot.trx_gl_date,
decode(fnd_profile.value('AR_APPLICATION_GL_DATE_DEFAULT'),
'INV_REC_SYS_DT',
Sysdate,
'INV_REC_DT',
ot.trx_gl_date,
ot.trx_gl_date)) gl_date,*/
decode(ot.default_reference,
'1',
ot.interface_header_attribute1,
'2',
ot.interface_header_attribute2,
'3',
ot.interface_header_attribute3,
'4',
ot.interface_header_attribute4,
'5',
ot.interface_header_attribute5,
'6',
ot.interface_header_attribute6,
'7',
ot.interface_header_attribute7,
'8',
ot.interface_header_attribute8,
'9',
ot.interface_header_attribute9,
'10',
ot.interface_header_attribute10,
'11',
ot.interface_header_attribute11,
'12',
ot.interface_header_attribute12,
'13',
ot.interface_header_attribute13,
'14',
ot.interface_header_attribute14,
'15',
ot.interface_header_attribute15,
Null) reference,
ot.term_id,
decode(sign(ot.payment_schedule_id),
-1,
to_number(Null),
ot.amount_due_original) amount_due_original,
ot.amount_line_items_original,
ot.balance_due_curr_unformatted,
ot.discount_taken_earned,
ot.discount_taken_unearned,
ot.trx_batch_source_name trx_batch_source_name,
ot.amount_adjusted amount_adjusted,
ot.amount_adjusted_pending amount_adjusted_pending,
ot.amount_line_items_remaining amount_line_items_remaining,
ot.freight_original freight_original,
ot.freight_remaining freight_remaining,
ot.receivables_charges_remaining receivables_charges_remaining,
ot.tax_original tax_original,
ot.tax_remaining tax_remaining,
ot.selected_for_receipt_batch_id selected_for_receipt_batch_id,
ot.trx_billing_number trx_billing_number,
ot.purchase_order purchase_order,
ot.bill_to_site_use_id bill_to_site_use_id,
ot.open_receipt_status_meaning,
ot.customer_reference,
ot.reason,
ot.receivable_application_id,
ot.account_description
From ar_open_trx_v ot
Where ot.status ='OP'
--decode(:rwapp_control.show_closed_invoices, 'TRUE', ot.status, 'OP')
--And nvl(ot.terms_sequence_number, 99) =
-- nvl(:app_folder.installment, nvl(ot.terms_sequence_number, 99)) /* for considering the defaulted installment number : bug 415478 */
Order By decode(sign(ot.payment_schedule_id), -1, 0, 4),
ot.trx_number,
ot.customer_name,
ot.terms_sequence_number,
ot.payment_schedule_id