SELECT DISTINCT TO_CHAR(SYSDATE,'DD-MON-YYYY') RUNDATE,
AIA.INVOICE_ID,
AIA.INVOICE_NUM,
ASU.VENDOR_NAME,
ASSA.VENDOR_SITE_CODE,
ASU.SEGMENT1 SUPPLIER_NUM,
AIA.INVOICE_ID VOUCHER,
AIA.ATTRIBUTE5 INV_CATEGORY,
AT.NAME PAYYMT_TERM,
AIA.SOURCE,
AIA.DESCRIPTION INV_DESCRIPTION,
AIA.INVOICE_TYPE_LOOKUP_CODE INVOICE_TYPE,
TO_CHAR(AIA.INVOICE_DATE,'DD-MON-YYYY') INVOICE_DATE,
TO_CHAR(AIA.TERMS_DATE,'DD-MON-YYYY')TERMS_DATE
,TO_CHAR(AIA.GL_DATE,'DD-MON-YYYY')GL_DATE,
AIA.INVOICE_AMOUNT,
AIA.AMOUNT_PAID,
( AIA.INVOICE_AMOUNT - AIA.AMOUNT_PAID ) BALANCE,
NVL(JAI.TDS_AMOUNT,0),
TO_CHAR(APSA.DUE_DATE,'DD-MON-YYYY') DUE_DATE,
APSA.GROSS_AMOUNT,
APSA.AMOUNT_REMAINING,
APSA.HOLD_FLAG,
APSA.IBY_HOLD_REASON HOLD_REASON,
APSA.PAYMENT_METHOD_CODE PAYMENT_METHOD,
APSA.PAYMENT_STATUS_FLAG PAYMENT_STATUS
,DECODE (AIA.WFAPPROVAL_STATUS,'WFAPPROVED','APPROVED','MANUALLY APPROVED','APPROVED','NOT REQUIRED','APPROAL NOT REQUIRED', 'REQUIRED', 'NEED APPROVAL', 'NOT REQUIRED', AIA.WFAPPROVAL_STATUS
) APPROVAL_STATUS
,DECODE (AP_INVOICES_PKG.GET_POSTING_STATUS( AIA.INVOICE_ID),'N','NO','Y','YES') ACCOUNTED
,DECODE(APPS.AP_INVOICES_PKG.GET_APPROVAL_STATUS(AIA.INVOICE_ID,AIA.INVOICE_AMOUNT,AIA.PAYMENT_STATUS_FLAG,AIA.INVOICE_TYPE_LOOKUP_CODE),
'APPROVED', 'VALIDATED',
'FULL', 'FULLY VALIDATD',
'UNAPPROVED', 'UNVALIDATED',
'NEEDS REAPPROVAL','NEEDS REVALIDATION',
'NEVER APPROVED','NEVER VALIDATED',
'CANCELLED', 'CANCELLED',
'UNPAID', 'UNPAID',
'AVAILABLE', 'AVAILABLE'
)VALIDATED
FROM
AP_SUPPLIERS ASU,
AP_SUPPLIER_SITES_ALL ASSA,
AP_INVOICES_ALL AIA,
AP_INVOICE_LINES_ALL AILA,
AP_TERMS AT,
JAI_AP_TDS_INVOICES JAI,
AP_PAYMENT_SCHEDULES_ALL APSA
,AP_INVOICE_DISTRIBUTIONS_ALL APDA
WHERE
1 = 1
AND ASU.VENDOR_ID = ASSA.VENDOR_ID
AND AILA.INVOICE_ID = AIA.INVOICE_ID
AND AIA.VENDOR_ID = ASU.VENDOR_ID
AND AIA.VENDOR_SITE_ID = ASSA.VENDOR_SITE_ID
AND AT.TERM_ID = AIA.TERMS_ID
AND JAI.INVOICE_ID (+) = AIA.INVOICE_ID
AND APSA.INVOICE_ID = AIA.INVOICE_ID
AND APDA.INVOICE_ID(+) = AIA.INVOICE_ID
AND AIA.INVOICE_NUM = NVL(:P_INVOICE_NUM,AIA.INVOICE_NUM)
AND AIA.INVOICE_DATE BETWEEN NVL(TO_DATE(:P_DATE_FROM,'YYYY/MM/DD HH24:MI:SS'),AIA.INVOICE_DATE) AND NVL(TO_DATE(:P_DATE_TO,'YYYY/MM/DD HH24:MI:SS'),AIA.INVOICE_DATE)
AIA.INVOICE_ID,
AIA.INVOICE_NUM,
ASU.VENDOR_NAME,
ASSA.VENDOR_SITE_CODE,
ASU.SEGMENT1 SUPPLIER_NUM,
AIA.INVOICE_ID VOUCHER,
AIA.ATTRIBUTE5 INV_CATEGORY,
AT.NAME PAYYMT_TERM,
AIA.SOURCE,
AIA.DESCRIPTION INV_DESCRIPTION,
AIA.INVOICE_TYPE_LOOKUP_CODE INVOICE_TYPE,
TO_CHAR(AIA.INVOICE_DATE,'DD-MON-YYYY') INVOICE_DATE,
TO_CHAR(AIA.TERMS_DATE,'DD-MON-YYYY')TERMS_DATE
,TO_CHAR(AIA.GL_DATE,'DD-MON-YYYY')GL_DATE,
AIA.INVOICE_AMOUNT,
AIA.AMOUNT_PAID,
( AIA.INVOICE_AMOUNT - AIA.AMOUNT_PAID ) BALANCE,
NVL(JAI.TDS_AMOUNT,0),
TO_CHAR(APSA.DUE_DATE,'DD-MON-YYYY') DUE_DATE,
APSA.GROSS_AMOUNT,
APSA.AMOUNT_REMAINING,
APSA.HOLD_FLAG,
APSA.IBY_HOLD_REASON HOLD_REASON,
APSA.PAYMENT_METHOD_CODE PAYMENT_METHOD,
APSA.PAYMENT_STATUS_FLAG PAYMENT_STATUS
,DECODE (AIA.WFAPPROVAL_STATUS,'WFAPPROVED','APPROVED','MANUALLY APPROVED','APPROVED','NOT REQUIRED','APPROAL NOT REQUIRED', 'REQUIRED', 'NEED APPROVAL', 'NOT REQUIRED', AIA.WFAPPROVAL_STATUS
) APPROVAL_STATUS
,DECODE (AP_INVOICES_PKG.GET_POSTING_STATUS( AIA.INVOICE_ID),'N','NO','Y','YES') ACCOUNTED
,DECODE(APPS.AP_INVOICES_PKG.GET_APPROVAL_STATUS(AIA.INVOICE_ID,AIA.INVOICE_AMOUNT,AIA.PAYMENT_STATUS_FLAG,AIA.INVOICE_TYPE_LOOKUP_CODE),
'APPROVED', 'VALIDATED',
'FULL', 'FULLY VALIDATD',
'UNAPPROVED', 'UNVALIDATED',
'NEEDS REAPPROVAL','NEEDS REVALIDATION',
'NEVER APPROVED','NEVER VALIDATED',
'CANCELLED', 'CANCELLED',
'UNPAID', 'UNPAID',
'AVAILABLE', 'AVAILABLE'
)VALIDATED
FROM
AP_SUPPLIERS ASU,
AP_SUPPLIER_SITES_ALL ASSA,
AP_INVOICES_ALL AIA,
AP_INVOICE_LINES_ALL AILA,
AP_TERMS AT,
JAI_AP_TDS_INVOICES JAI,
AP_PAYMENT_SCHEDULES_ALL APSA
,AP_INVOICE_DISTRIBUTIONS_ALL APDA
WHERE
1 = 1
AND ASU.VENDOR_ID = ASSA.VENDOR_ID
AND AILA.INVOICE_ID = AIA.INVOICE_ID
AND AIA.VENDOR_ID = ASU.VENDOR_ID
AND AIA.VENDOR_SITE_ID = ASSA.VENDOR_SITE_ID
AND AT.TERM_ID = AIA.TERMS_ID
AND JAI.INVOICE_ID (+) = AIA.INVOICE_ID
AND APSA.INVOICE_ID = AIA.INVOICE_ID
AND APDA.INVOICE_ID(+) = AIA.INVOICE_ID
AND AIA.INVOICE_NUM = NVL(:P_INVOICE_NUM,AIA.INVOICE_NUM)
AND AIA.INVOICE_DATE BETWEEN NVL(TO_DATE(:P_DATE_FROM,'YYYY/MM/DD HH24:MI:SS'),AIA.INVOICE_DATE) AND NVL(TO_DATE(:P_DATE_TO,'YYYY/MM/DD HH24:MI:SS'),AIA.INVOICE_DATE)