Sunday, March 10, 2013

Package Query from All Modules (XLA to GL Interface)



CREATE OR REPLACE PACKAGE BODY APPS.PN_GL_IFACE_PKG AS
PROCEDURE PN_UPLOAD_TO_GL_IFACE(errcode NUMBER, errmsg VARCHAR2,bln varchar2,thn varchar2) IS
      
jml NUMBER;
P_DATE varchar2(10);
P_LEDGER_ID varchar2(5);

BEGIN
    DBMS_APPLICATION_INFO.SET_MODULE('PN_GL_INTERFACE','EXIT');
    Fnd_File.PUT_LINE(Fnd_File.LOG,'Begin PN_GL_INTERFACE  : '||TO_CHAR(SYSDATE,'DD-MON-RRRR HH24:MI:SS'));
    Fnd_File.PUT_LINE(Fnd_File.LOG,'Bulan : '||bln);
    Fnd_File.PUT_LINE(Fnd_File.LOG,'Tahun : '||thn);    
    
    jml :=0;
    P_LEDGER_ID := '2022';  
    P_DATE := bln||'/'||thn;
    
    insert into gl_1@db3(
          STATUS,
          LEDGER_ID,
          ACCOUNTING_DATE,
          CURRENCY_CODE,
          DATE_CREATED,
          CREATED_BY,
          ACTUAL_FLAG,
          USER_JE_CATEGORY_NAME,
          USER_JE_SOURCE_NAME,
          CURRENCY_CONVERSION_DATE,
          ENCUMBRANCE_TYPE_ID,
          BUDGET_VERSION_ID,
          USER_CURRENCY_CONVERSION_TYPE,
          CURRENCY_CONVERSION_RATE ,
          AVERAGE_JOURNAL_FLAG,
          ORIGINATING_BAL_SEG_VALUE,
          SEGMENT1,
          SEGMENT2,
          SEGMENT3,
          SEGMENT4,
          SEGMENT5,
          SEGMENT6,
          SEGMENT7,
          SEGMENT8,
          SEGMENT9,
          SEGMENT10,
          SEGMENT11,
          SEGMENT12,
          SEGMENT13,
          SEGMENT14,
          SEGMENT15,
          SEGMENT16,
          SEGMENT17,
          SEGMENT18,
          SEGMENT19,
          SEGMENT20,
          SEGMENT21,
          SEGMENT22,
          SEGMENT23,
          SEGMENT24,
          SEGMENT25,
          SEGMENT26,
          SEGMENT27,
          SEGMENT28,
          SEGMENT29,
          SEGMENT30,
          ENTERED_DR,
          ENTERED_CR,
          ACCOUNTED_DR,
          ACCOUNTED_CR,
          TRANSACTION_DATE,
          REFERENCE1,
          REFERENCE2,
          REFERENCE3,
          REFERENCE4,
          REFERENCE5,
          REFERENCE6,
          REFERENCE7,
          REFERENCE8,
          REFERENCE9,
          REFERENCE10,
          REFERENCE11,
          REFERENCE12,
          REFERENCE13,
          REFERENCE14,
          REFERENCE15,
          REFERENCE16,
          REFERENCE17,
          REFERENCE18,
          REFERENCE19,
          REFERENCE20,
          REFERENCE21,
          REFERENCE22,
          REFERENCE23,
          REFERENCE24,
          REFERENCE25,
          REFERENCE26,
          REFERENCE27,
          REFERENCE28,
          REFERENCE29,
          REFERENCE30,
          JE_BATCH_ID,
          PERIOD_NAME,
          JE_HEADER_ID,
          JE_LINE_NUM,
          CHART_OF_ACCOUNTS_ID,
          FUNCTIONAL_CURRENCY_CODE,
          CODE_COMBINATION_ID,
          DATE_CREATED_IN_GL,
          WARNING_CODE,
          STATUS_DESCRIPTION,
          STAT_AMOUNT,
          GROUP_ID,
          REQUEST_ID,
          SUBLEDGER_DOC_SEQUENCE_ID,
          SUBLEDGER_DOC_SEQUENCE_VALUE,
          ATTRIBUTE1,
          ATTRIBUTE2,
          GL_SL_LINK_ID,
          GL_SL_LINK_TABLE,
          ATTRIBUTE3,
          ATTRIBUTE4,
          ATTRIBUTE5,
          ATTRIBUTE6,
          ATTRIBUTE7,
          ATTRIBUTE8,
          ATTRIBUTE9,
          ATTRIBUTE10,
          ATTRIBUTE11,
          ATTRIBUTE12,
          ATTRIBUTE13,
          ATTRIBUTE14,
          ATTRIBUTE15,
          ATTRIBUTE16,
          ATTRIBUTE17,
          ATTRIBUTE18,
          ATTRIBUTE19,
          ATTRIBUTE20,
          CONTEXT,
          CONTEXT2,
          INVOICE_DATE,
          TAX_CODE,
          INVOICE_IDENTIFIER,
          INVOICE_AMOUNT,
          CONTEXT3,
          USSGL_TRANSACTION_CODE,
          DESCR_FLEX_ERROR_MESSAGE,
          JGZZ_RECON_REF,
          REFERENCE_DATE,
          SET_OF_BOOKS_ID,
          BALANCING_SEGMENT_VALUE,
          MANAGEMENT_SEGMENT_VALUE,
          FUNDS_RESERVED_FLAG,
          CODE_COMBINATION_ID_INTERIM
    )
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
        ,fab.ASSET_NUMBER||'-'||xe.EVENT_TYPE_CODE REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,substr(gjl.DESCRIPTION,1,150) REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
        ,fab.asset_number attribute1
        ,substr(fat.DESCRIPTION,1,150)  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
        ,null attribute3
        ,null attribute4
        ,null attribute5
        ,null attribute6
        ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct --link GL 
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL  
    ,fnd_application fa
    ,fa_deprn_detail fdd
    ,fa_additions_b fab
    ,fa_additions_tl fat
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
      --and xah.APPLICATION_ID=140
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Depreciation'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL      
      and xah.event_id = fdd.EVENT_ID
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and fdd.asset_id = fab.asset_id
      and fab.asset_id = fat.asset_id
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
union all
/* 2 dan 3.Source:Asset  Category: Adjustment, Addition,Transfer,Reclass,Retirement */
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4
        ,fab.ASSET_NUMBER||'-'||xe.EVENT_TYPE_CODE REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,substr(gjl.DESCRIPTION,1,150) REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE
        ,fab.asset_number attribute1
        ,substr(fat.DESCRIPTION,1,150)  attribute2
        ,null GL_SL_LINK_ID
        ,null GL_SL_LINK_TABLE
        ,null attribute3
        ,null attribute4
        ,null attribute5
        ,null attribute6
        ,null attribute7
        ,null attribute8
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
     ,GL_JE_CATEGORIES_TL gjct --link GL 
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL  
    ,fnd_application fa
    ,fa_transaction_headers fth
    ,fa_additions_b fab
    ,fa_additions_tl fat
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
--      and xah.APPLICATION_ID=140
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL
      and xah.event_id = fth.EVENT_ID
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and fth.asset_id = fab.asset_id
      and fab.asset_id = fat.asset_id
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID  
union all
/* 4. Source: Cost Management, Category: Receiving*/
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4       
        ,rsh.receipt_num REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,substr(gjl.DESCRIPTION,1,150) REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE
        ,asa.vendor_name attribute1
        ,pha.segment1  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,rsh.receipt_num attribute3
       ,null attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct --link GL 
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL  
    ,xla_transaction_entities_upg xteu
    ,fnd_application fa
    ,rcv_transactions rt
    ,rcv_shipment_headers rsh
    ,ap_suppliers asa
    ,po_headers_all pha
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=707
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Receiving'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = rt.transaction_id(+)
      and rt.SHIPMENT_HEADER_ID = rsh.SHIPMENT_HEADER_ID(+)
      and rt.vendor_id = asa.vendor_id(+)
      and rt.PO_HEADER_ID = pha.PO_HEADER_ID(+)
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID  
union all 
/* 5. Source: Cost Management Category: Inventory*/
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
        ,mtt.transaction_type_name||'-'|| pha.segment1||'-'||rsh.RECEIPT_NUM ||'-'||rsh.WAYBILL_AIRBILL_NUM||'-'||asa.vendor_name REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --        
       ,to_char(mmt.TRANSACTION_ID )attribute1
       ,mtst.TRANSACTION_SOURCE_TYPE_NAME  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,mtt.transaction_type_name attribute3
       ,pha.segment1 attribute4
       ,rsh.RECEIPT_NUM attribute5
       ,rsh.WAYBILL_AIRBILL_NUM attribute6
       ,asa.vendor_name attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct -- link GL
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL    
    ,xla_transaction_entities_upg xteu
    ,fnd_application fa
    ,rcv_transactions rt
     ,mtl_material_transactions mmt
    ,mtl_transaction_types mtt
    ,mfg_lookups ml
    ,mtl_txn_source_types mtst
    ,rcv_shipment_headers rsh
    ,ap_suppliers asa
    ,po_headers_all pha
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=707
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Inventory'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = mmt.TRANSACTION_ID
      and mmt.RCV_TRANSACTION_ID = rt.transaction_id(+)
      and rt.SHIPMENT_HEADER_ID = rsh.SHIPMENT_HEADER_ID(+)
      and rt.vendor_id = asa.vendor_id(+)
      and rt.PO_HEADER_ID = pha.PO_HEADER_ID(+)
      and mmt.transaction_type_id = mtt.transaction_type_id(+)
      and mtt.transaction_action_id= ml.lookup_code(+)
      and ml.lookup_type(+) = 'MTL_TRANSACTION_ACTION'
      --GET source type
      and mtt.TRANSACTION_SOURCE_TYPE_ID = mtst.TRANSACTION_SOURCE_TYPE_ID(+)
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
union all
/*6. Source : Manual ,Category:Manual */
select  'NEW' status
        ,gjh.LEDGER_ID
        ,last_day(to_date('01-'||gjh.period_name)) accounting_date
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,gjl.ENTERED_DR
        ,gjl.ENTERED_CR
        ,gjl.ACCOUNTED_DR
        ,gjl.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,gjh.NAME  REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,gjh.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,gjl.CODE_COMBINATION_ID        
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,null attribute1
       ,null attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,null attribute3
       ,null attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from gl_je_headers gjh
    ,gl_je_lines gjl
    ,GL_JE_CATEGORIES_TL gjct
    ,gl_je_sources_tl gjst-- link GL
    ,gl_code_combinations gcc
where gjh.JE_HEADER_ID = gjl.JE_HEADER_ID
      and gjh.je_source='Manual'
      and gjh.je_category='Adjustment'
      and TO_CHAR(gjl.EFFECTIVE_DATE,'MON/RR') = P_DATE
      and gjh.LEDGER_ID=P_LEDGER_ID
      and gjh.JE_CATEGORY = gjct.JE_CATEGORY_NAME
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and gjl.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID              
union all
/* 7. Source: Payables , Category : Reconciled Payment */
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,to_char(aca.check_number) REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,asa.vendor_name attribute1
       ,null  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,to_char(aca.check_number) attribute3
       ,substr(aca.DESCRIPTION,1,150) attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct -- link GL
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL    
    ,xla_transaction_entities_upg xteu
    ,fnd_application fa
    ,ap_checks_all aca
    ,ap_suppliers asa
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=200
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Reconciled Payments'
     and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL      
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = aca.CHECK_ID
      and aca.vendor_id = asa.vendor_id
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID      
union all
/*8. Source : Payables , Category : Payments*/
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,to_char(aca.check_number) REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,asa.vendor_name attribute1
       ,null  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,to_char(aca.check_number) attribute3
       ,substr(aca.DESCRIPTION,1,150) attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct -- link GL
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL
    ,xla_transaction_entities_upg xteu
    ,fnd_application fa
    ,ap_checks_all aca
    ,ap_suppliers asa
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
--      and xah.APPLICATION_ID=200
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Payments'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = aca.CHECK_ID
      and aca.vendor_id = asa.vendor_id
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
union all
/*9. Source : Payables , Category : Purchase Invoices*/
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,aia.invoice_num REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,asa.vendor_name attribute1
       ,aia.invoice_num  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,null attribute3
       ,substr(aia.DESCRIPTION,1,150) attribute4
       ,(
           select pha.segment1
           from po_headers_all pha
               ,ap_invoice_lines_all aila
           where aila.PO_HEADER_ID = pha.PO_HEADER_ID
                 and aila.org_id =pha.ORG_ID
                 and aila.invoice_id = aia.invoice_id
                 and aila.org_id = aia.org_id
                 and rownum=1
        ) attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct -- link GL
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL    
    ,xla_transaction_entities_upg xteu
    ,fnd_application fa
    ,ap_invoices_all aia
    ,ap_suppliers asa
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
--      and xah.APPLICATION_ID=200
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Purchase Invoices'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = aia.invoice_id
      and aia.vendor_id = asa.vendor_id
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
union all
/* 10. Source : Receivables , Category : Adjustment */
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,aaa.adjustment_number||' - '||hp.party_name REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,hp.party_number attribute1
       ,aaa.adjustment_number  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,null attribute3
       ,null attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct -- link GL
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL
    ,fnd_application fa
    ,ar_adjustments_all aaa
    ,ra_customer_trx_all rcta
    ,hz_cust_accounts hca
    ,hz_parties hp
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
    --  and xah.APPLICATION_ID=222
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Adjustment'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL      
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xah.event_id = aaa.event_id
      and aaa.ORG_ID = rcta.org_id
      and aaa.CUSTOMER_TRX_ID = rcta.customer_trx_id
      and rcta.BILL_TO_CUSTOMER_ID = hca.cust_account_id
      and hca.party_id = hp.party_id
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID      
union all
/*11. Source : Receivables , Category : Misc Receipt*/
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,acra.RECEIPT_NUMBER  ||' - '|| REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,null attribute1
       ,acra.receipt_number  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,substr(replace(acra.COMMENTS,chr(10),' '),1,150) attribute3
       ,null attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct -- link GL
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL
    ,fnd_application fa
    ,ar_cash_receipt_history_all acrha
    ,ar_cash_receipts_all acra
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
      --and xah.APPLICATION_ID=222
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Misc Receipts'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL           
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xah.EVENT_ID = acrha.EVENT_ID
      and acrha.ORG_ID = acra.ORG_ID
      and acrha.CASH_RECEIPT_ID = acra.CASH_RECEIPT_ID
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
union all
/* 12. Source : Receivables , Category: Credit Memos*/
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,rcta.trx_number  ||' - '||hp.party_name REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,hp.party_number attribute1
       ,rcta.trx_number  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,rcta.ct_reference attribute3
       ,null attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct -- link GL
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL    
    ,xla_transaction_entities_upg xteu
    ,fnd_application fa
    ,ra_customer_trx_all rcta
    ,hz_cust_accounts hca
    ,hz_parties hp
    ,gl_code_combinations gcc    
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
--      and xah.APPLICATION_ID=222
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Credit Memos'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL      
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = rcta.customer_trx_id
      and rcta.BILL_TO_CUSTOMER_ID = hca.cust_account_id
      and hca.party_id = hp.party_id
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
union all
/*13. Source : Receivables , Category : Receipts*/
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,acra.RECEIPT_NUMBER||'-'||hp.party_name  REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,hp.party_number attribute1
       ,acra.receipt_number  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,substr(replace(acra.COMMENTS,chr(10),' '),1,150) attribute3
       ,null attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,xla_transaction_entities_upg xteu
    ,GL_JE_CATEGORIES_TL gjct -- link GL
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL
    ,fnd_application fa
--    ,ar_cash_receipt_history_all acrha
    ,ar_cash_receipts_all acra
     ,hz_cust_site_uses_all hcsua
     ,hz_cust_acct_sites_all hcasa
     ,hz_cust_accounts hca
     ,hz_parties hp
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
--      and xah.APPLICATION_ID=222
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Receipts'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL      
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = acra.CASH_RECEIPT_ID
      and xteu.SECURITY_ID_INT_1= acra.ORG_ID
--      and xah.EVENT_ID = acrha.EVENT_ID
--      and acrha.ORG_ID = acra.ORG_ID
--      and acrha.CASH_RECEIPT_ID = acra.CASH_RECEIPT_ID
      and hcsua.SITE_USE_ID(+) = acra.CUSTOMER_SITE_USE_ID
      and hcsua.CUST_ACCT_SITE_ID = hcasa.CUST_ACCT_SITE_ID(+)
      and hcasa.CUST_ACCOUNT_ID = hca.CUST_ACCOUNT_ID(+)
      and hca.PARTY_ID = hp.party_id(+)  
      and xal.CODE_COMBINATION_ID= gcc.CODE_COMBINATION_ID  
union all
/* 14. Source : Receivables , Category: Sales Invoices */
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,rcta.trx_number  ||'-'||hp.party_name REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,hp.party_number attribute1
       ,rcta.trx_number  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,null attribute3
       ,null attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct -- link GL
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL    
    ,xla_transaction_entities_upg xteu
    ,fnd_application fa
    ,ra_customer_trx_all rcta
    ,hz_cust_accounts hca
    ,hz_parties hp
    ,gl_code_combinations gcc    
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
--      and xah.APPLICATION_ID=222
      and xah.application_id = fa.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Sales Invoices'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = rcta.customer_trx_id
      and rcta.BILL_TO_CUSTOMER_ID = hca.cust_account_id
      and hca.party_id = hp.party_id
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID      
union all
/* 15. Source : Inventory, Category : Misc Transactions*/
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,mmt.TRANSACTION_ID||'-'||mtst.TRANSACTION_SOURCE_TYPE_NAME||'-'||
        mtt.transaction_type_name||'-'||ml.MEANING||'-'|| 
        (
            select mtrh.REQUEST_NUMBER
            from mtl_material_Transactions mmt
                ,mtl_txn_request_lines mtrl
                ,mtl_txn_request_headers mtrh
            where mmt.MOVE_ORDER_LINE_ID = mtrl.LINE_ID
                  and mmt.ORGANIZATION_ID = mtrl.organization_id
                  and mtrl.HEADER_ID = mtrh.header_id
                  and mtrl.ORGANIZATION_ID = mtrl.organization_id
                  and rownum=1
        ) REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,to_char(mmt.TRANSACTION_ID )attribute1
       ,mtst.TRANSACTION_SOURCE_TYPE_NAME  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,mtt.transaction_type_name attribute3
       ,ml.MEANING attribute4
        ,(
            select mtrh.REQUEST_NUMBER
            from mtl_material_Transactions mmt
                ,mtl_txn_request_lines mtrl
                ,mtl_txn_request_headers mtrh
            where mmt.MOVE_ORDER_LINE_ID = mtrl.LINE_ID
                  and mmt.ORGANIZATION_ID = mtrl.organization_id
                  and mtrl.HEADER_ID = mtrh.header_id
                  and mtrl.ORGANIZATION_ID = mtrl.organization_id
                  and rownum=1
        ) attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct -- link GL
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR -- link GL
    ,gl_je_headers gjh -- link GL
    ,gl_je_lines gjl -- lilnk GL        
    ,xla_transaction_entities_upg xteu
    ,fnd_application_tl fat
    ,mtl_material_transactions mmt
    ,mtl_transaction_types mtt
    ,mfg_lookups ml
    ,mtl_txn_source_types mtst
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=555
      and xah.application_id = fat.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Miscellaneous Transaction'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME --link GL
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID --link GL
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID --link GL
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID --link GL
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM --link GL
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = mmt.transaction_id
      and xteu.SECURITY_ID_INT_1=mmt.organization_id
      and mmt.transaction_type_id = mtt.transaction_type_id
      and mtt.transaction_action_id= ml.lookup_code
      and ml.lookup_type = 'MTL_TRANSACTION_ACTION'
      --GET source type
      and mtt.TRANSACTION_SOURCE_TYPE_ID = mtst.TRANSACTION_SOURCE_TYPE_ID 
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID      
union all
/* 16. Source : Inventory , Category : OPM OM Shipments */
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,mmt.TRANSACTION_ID||'-'||mtst.TRANSACTION_SOURCE_TYPE_NAME||'-'||
        mtt.transaction_type_name||'-'|| 
        (
            select ooha.order_number||'.'||ottt.NAME||'.ORDER ENTRY'
            from rcv_transactions rt
                ,oe_order_headers_all ooha
                ,oe_transaction_types_tl ottt
            where rt.TRANSACTION_ID = mmt.RCV_TRANSACTION_ID
                  and rt.OE_ORDER_HEADER_ID= ooha.HEADER_ID
                  and ooha.ORDER_TYPE_ID = ottt.TRANSACTION_TYPE_ID
                  and rownum=1        
        ) REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,to_char(mmt.TRANSACTION_ID )attribute1
       ,mtst.TRANSACTION_SOURCE_TYPE_NAME  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,mtt.transaction_type_name attribute3
       ,        
       (
            select ooha.order_number||'.'||ottt.NAME||'.ORDER ENTRY'
            from rcv_transactions rt
                ,oe_order_headers_all ooha
                ,oe_transaction_types_tl ottt
            where rt.TRANSACTION_ID = mmt.RCV_TRANSACTION_ID
                  and rt.OE_ORDER_HEADER_ID= ooha.HEADER_ID
                  and ooha.ORDER_TYPE_ID = ottt.TRANSACTION_TYPE_ID
                  and rownum=1        
        ) attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR
    ,gl_je_headers gjh
    ,gl_je_lines gjl
    ,xla_transaction_entities_upg xteu
    ,fnd_application_tl fat
    ,mtl_material_transactions mmt
    ,mtl_transaction_types mtt
    ,mfg_lookups ml
    ,mtl_txn_source_types mtst
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.APPLICATION_ID = xal.APPLICATION_ID
      and xah.ledger_id = xal.ledger_id
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=555
      and xah.application_id = fat.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='OMSP'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = mmt.transaction_id
      and xteu.SECURITY_ID_INT_1=mmt.organization_id
      and mmt.transaction_type_id = mtt.transaction_type_id
      and mtt.transaction_action_id= ml.lookup_code
      and ml.lookup_type = 'MTL_TRANSACTION_ACTION'
      --GET source type
      and mtt.TRANSACTION_SOURCE_TYPE_ID = mtst.TRANSACTION_SOURCE_TYPE_ID 
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID      
union all
/* 17, Source : Inventory , Category : OPM Inv Receipts */
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,mmt.TRANSACTION_ID||'-'||mtst.TRANSACTION_SOURCE_TYPE_NAME||'-'||
        mtt.transaction_type_name||'-'|| pha.segment1||'-'||rsh.RECEIPT_NUM
        ||'-'||rsh.WAYBILL_AIRBILL_NUM||'-'||asa.vendor_name  name
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,to_char(mmt.TRANSACTION_ID )attribute1
       ,mtst.TRANSACTION_SOURCE_TYPE_NAME  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,mtt.transaction_type_name attribute3
       ,pha.segment1 attribute4
       ,rsh.RECEIPT_NUM attribute5
       ,rsh.WAYBILL_AIRBILL_NUM attribute6
       ,asa.vendor_name attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR
    ,gl_je_headers gjh
    ,gl_je_lines gjl
    ,xla_transaction_entities_upg xteu
    ,fnd_application_tl fat
--    ,rcv_transactions rt
    ,mtl_material_transactions mmt
    ,mtl_transaction_types mtt
    ,mfg_lookups ml
    ,mtl_txn_source_types mtst
    ,rcv_transactions rt
    ,rcv_shipment_headers rsh
    ,po_headers_all pha
    ,ap_suppliers asa    
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.APPLICATION_ID = xal.APPLICATION_ID
      and xah.ledger_id = xal.ledger_id
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=555
      and xah.application_id = fat.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='RCPT'
      and xah.EVENT_TYPE_CODE in ('PO_RECEIPT','PO_RECEIPT_ADJ','RET_TO_RECEIVING')
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = mmt.transaction_id
      and xteu.SECURITY_ID_INT_1=mmt.organization_id
      and mmt.transaction_type_id = mtt.transaction_type_id
      and mtt.transaction_action_id= ml.lookup_code
      and ml.lookup_type = 'MTL_TRANSACTION_ACTION'
      --GET source type
      and mtt.TRANSACTION_SOURCE_TYPE_ID = mtst.TRANSACTION_SOURCE_TYPE_ID
      and mmt.RCV_TRANSACTION_ID = rt.TRANSACTION_ID
      and rt.SHIPMENT_HEADER_ID = rsh.shipment_header_id
      and rt.PO_HEADER_ID = pha.PO_HEADER_ID
      and pha.vendor_id = asa.vendor_id     
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID     
union all          
/*18. Source : Inventory , Category : OPM Inv Receipts IPV ERV */
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,msib.segment1||'-'||to_char(gxeh.transaction_date,'MON-RR')||'-'||
        gxeh.valuation_cost_type REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,msib.segment1 attribute1
       ,to_char(gxeh.transaction_date,'MON-RR')  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,gxeh.valuation_cost_type attribute3
       ,null attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR
    ,gl_je_headers gjh
    ,gl_je_lines gjl
    ,fnd_application_tl fat
    ,gmf_xla_extract_headers gxeh
    ,mtl_system_items_b msib
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.APPLICATION_ID = xal.APPLICATION_ID
      and xah.ledger_id = xal.ledger_id
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=555
      and xah.application_id = fat.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='RCPT'
      and xah.EVENT_TYPE_CODE in ('INVOICE_ERV_ADJ','INVOICE_IPV_ADJ')
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM
      and xah.event_id = xe.event_id
      and xe.EVENT_ID = gxeh.EVENT_ID(+)
      and xah.application_id = xe.application_id
      and gxeh.INVENTORY_ITEM_ID = msib.INVENTORY_ITEM_ID(+)
      and gxeh.ORGANIZATION_ID = msib.ORGANIZATION_ID(+)
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID    
union all
/*19. Source : Inventory , Category :OPM Batch Release */
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,mmt.TRANSACTION_ID||'-'||mtst.TRANSACTION_SOURCE_TYPE_NAME||'-'||
        mtt.transaction_type_name||'-'|| 
        (
            select 'BATCH'||gbh.batch_no
            from gme_batch_header gbh
            where gbh.BATCH_ID=mmt.TRANSACTION_SOURCE_ID
                  and rownum=1          
        ) REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,to_char(mmt.TRANSACTION_ID )attribute1
       ,mtst.TRANSACTION_SOURCE_TYPE_NAME  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,mtt.transaction_type_name attribute3
       , (
            select 'BATCH'||gbh.batch_no
            from gme_batch_header gbh
            where gbh.BATCH_ID=mmt.TRANSACTION_SOURCE_ID
                  and rownum=1          
        ) attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR
    ,gl_je_headers gjh
    ,gl_je_lines gjl
    ,xla_transaction_entities_upg xteu
    ,fnd_application_tl fat
    ,mtl_material_transactions mmt
    ,mtl_transaction_types mtt
    ,mfg_lookups ml
    ,mtl_txn_source_types mtst
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.APPLICATION_ID = xal.APPLICATION_ID
      and xah.ledger_id = xal.ledger_id
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=555
      and xah.application_id = fat.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='RELE'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = mmt.transaction_id
      and xteu.SECURITY_ID_INT_1=mmt.organization_id
      and mmt.transaction_type_id = mtt.transaction_type_id
      and mtt.transaction_action_id= ml.lookup_code
      and ml.lookup_type = 'MTL_TRANSACTION_ACTION'
      --GET source type
      and mtt.TRANSACTION_SOURCE_TYPE_ID = mtst.TRANSACTION_SOURCE_TYPE_ID
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID            
union all
/* 20. Source : Inventory , Category : OPM Inv Transfers */
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,mmt.TRANSACTION_ID||'-'||mtst.TRANSACTION_SOURCE_TYPE_NAME||'-'||
        mtt.transaction_type_name||'-'|| 
        PN_INV_LOC_TRX_SOURCE_NAME (mmt.transaction_id,mtst.TRANSACTION_SOURCE_TYPE_NAME,mmt.TRANSACTION_SOURCE_NAME) REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,to_char(mmt.TRANSACTION_ID )attribute1
       ,mtst.TRANSACTION_SOURCE_TYPE_NAME  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,mtt.transaction_type_name attribute3
       ,PN_INV_LOC_TRX_SOURCE_NAME (mmt.transaction_id,mtst.TRANSACTION_SOURCE_TYPE_NAME,mmt.TRANSACTION_SOURCE_NAME) attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR
    ,gl_je_headers gjh
    ,gl_je_lines gjl
    ,xla_transaction_entities_upg xteu
    ,fnd_application_tl fat
    ,mtl_material_transactions mmt
    ,mtl_transaction_types mtt
    ,mfg_lookups ml
    ,mtl_txn_source_types mtst
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.APPLICATION_ID = xal.APPLICATION_ID
      and xah.ledger_id = xal.ledger_id
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=555
      and xah.application_id = fat.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='XFER'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = mmt.transaction_id
      and xteu.SECURITY_ID_INT_1=mmt.organization_id
      and mmt.transaction_type_id = mtt.transaction_type_id
      and mtt.transaction_action_id= ml.lookup_code
      and ml.lookup_type = 'MTL_TRANSACTION_ACTION'
      --GET source type
      and mtt.TRANSACTION_SOURCE_TYPE_ID = mtst.TRANSACTION_SOURCE_TYPE_ID 
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID
union all
/* 21. Source: Inventory , Category : Receiving*/
/*select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,mmt.TRANSACTION_ID||'-'||mtst.TRANSACTION_SOURCE_TYPE_NAME||'-'||
        mtt.transaction_type_name||'-'|| pha.segment1||'-'||rsh.RECEIPT_NUM
        ||'-'||rsh.WAYBILL_AIRBILL_NUM||'-'||asa.vendor_name  REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,to_char(mmt.TRANSACTION_ID )attribute1
       ,mtst.TRANSACTION_SOURCE_TYPE_NAME  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,mtt.transaction_type_name attribute3
       ,pha.segment1 attribute4
       ,rsh.RECEIPT_NUM attribute5
       ,rsh.WAYBILL_AIRBILL_NUM attribute6
       ,asa.vendor_name attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR
    ,gl_je_headers gjh
    ,gl_je_lines gjl
    ,gmf_xla_extract_headers gxeh
    ,fnd_application_tl fat
    ,mtl_material_transactions mmt
    ,mtl_transaction_types mtt
    ,mfg_lookups ml
    ,mtl_txn_source_types mtst
    ,rcv_transactions rt
    ,rcv_shipment_headers rsh
    ,po_headers_all pha
    ,ap_suppliers asa    
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.APPLICATION_ID = xal.APPLICATION_ID
      and xah.ledger_id = xal.ledger_id
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=555
      and xah.application_id = fat.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='Receiving'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.EVENT_ID = gxeh.EVENT_ID
      and gxeh.source_line_id = rt.TRANSACTION_ID
      and mmt.RCV_TRANSACTION_ID(+) = rt.TRANSACTION_ID
      and mmt.transaction_type_id = mtt.transaction_type_id(+)
      and mtt.transaction_action_id= ml.lookup_code(+)
      and ml.lookup_type(+) = 'MTL_TRANSACTION_ACTION'
      --GET source type
      and mtt.TRANSACTION_SOURCE_TYPE_ID = mtst.TRANSACTION_SOURCE_TYPE_ID(+)
      and rt.SHIPMENT_HEADER_ID = rsh.shipment_header_id
      and rt.PO_HEADER_ID = pha.PO_HEADER_ID
      and pha.vendor_id = asa.vendor_id     
      and gcc.CODE_COMBINATION_ID = xal.CODE_COMBINATION_ID
union all
/*22. Source : Inventory , Category :OPM Shipment */
/*select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,to_char(mmt.TRANSACTION_ID)||'-'||mtst.TRANSACTION_SOURCE_TYPE_NAME||'-'||
        mtt.transaction_type_name||'-'|| ooha.order_number||'.'||ottt.NAME||'.'||  mmt.source_code
        ||'-'|| ooha.order_number  REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,substr(gjl.DESCRIPTION,1,150) REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,to_char(mmt.TRANSACTION_ID) attribute1
       ,mtst.TRANSACTION_SOURCE_TYPE_NAME  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,mtt.transaction_type_name attribute3
       ,ooha.order_number||'.'||ottt.NAME||'.'||  mmt.source_code attribute4
       ,to_char(ooha.order_number) attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR
    ,gl_je_headers gjh
    ,gl_je_lines gjl
    ,xla_transaction_entities_upg xteu
    ,fnd_application_tl fat
    ,mtl_material_transactions mmt
    ,mtl_transaction_types mtt
    ,mfg_lookups ml
    ,mtl_txn_source_types mtst
    ,oe_order_headers_all ooha
    ,OE_TRANSACTION_TYPES_TL ottt
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.APPLICATION_ID = xal.APPLICATION_ID
      and xah.ledger_id = xal.ledger_id
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=555
      and xah.application_id = fat.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='SHIP'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.ENTITY_ID = xteu.ENTITY_ID
      and xe.APPLICATION_ID = xteu.APPLICATION_ID
      and xteu.SOURCE_ID_INT_1 = mmt.transaction_id
      and xteu.SECURITY_ID_INT_1=mmt.organization_id
      and mmt.transaction_type_id = mtt.transaction_type_id
      and mtt.transaction_action_id= ml.lookup_code
      and ml.lookup_type = 'MTL_TRANSACTION_ACTION'
      --GET source type
      and mtt.TRANSACTION_SOURCE_TYPE_ID = mtst.TRANSACTION_SOURCE_TYPE_ID
      and mmt.TRANSACTION_REFERENCE = to_char(ooha.header_id(+))
      and ooha.order_type_id = ottt.TRANSACTION_TYPE_ID(+)
      and xal.CODE_COMBINATION_ID =gcc.CODE_COMBINATION_ID
union all
/*23. Source: Inventory , Category : OPM Resource Step*/
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
       ,gbh.batch_no||'-'||msib.segment1  REFERENCE5
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,gbh.batch_no attribute1
       ,msib.segment1  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,null attribute3
       ,null attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR
    ,gl_je_headers gjh
    ,gl_je_lines gjl
    ,gmf_xla_extract_headers gxeh
    ,fnd_application_tl fat
    ,gme_batch_header gbh
    ,mtl_system_items_b msib
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.APPLICATION_ID = xal.APPLICATION_ID
      and xah.ledger_id = xal.ledger_id
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=555
      and xah.application_id = fat.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='STEP'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.EVENT_ID = gxeh.EVENT_ID
      and gxeh.SOURCE_DOCUMENT_ID = gbh.batch_id(+)
      and gxeh.INVENTORY_ITEM_ID = msib.inventory_item_id(+)
      and gxeh.ORGANIZATION_ID = msib.organization_id(+)
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID      
union all
/* 24. Source: Inventory, Category : Cost Revaluation */
select  'NEW' status
        ,xal.LEDGER_ID
        ,xal.ACCOUNTING_DATE
        ,'IDR' CURRENCY_CODE
        ,sysdate date_created
        ,1110 created_by
        ,'A' ACTUAL_FLAG
        ,gjct.USER_JE_CATEGORY_NAME
        ,gjst.USER_JE_SOURCE_NAME
        ,null CURRENCY_CONVERSION_DATE
        ,null ENCUMBRANCE_TYPE_ID
        ,null BUDGET_VERSION_ID
        ,null USER_CURRENCY_CONVERSION_TYPE
        ,null CURRENCY_CONVERSION_RATE
        ,null AVERAGE_JOURNAL_FLAG
        ,null ORIGINATING_BAL_SEG_VALUE
        ,gcc.segment1 segment1
        ,gcc.segment2 segment2
        ,gcc.segment3 segment3
        ,gcc.segment4 segment4
        ,gcc.segment5 segment5
        ,gcc.segment6 segment6
        ,gcc.segment7 segment7
        ,null segment8
        ,null segment9
        ,null segment10
        ,null segment11
        ,null segment12
        ,null segment13
        ,null segment14
        ,null segment15
        ,null segment16
        ,null segment17
        ,null segment18
        ,null segment19
        ,null segment20
        ,null segment21
        ,null segment22
        ,null segment23
        ,null segment24
        ,null segment25
        ,null segment26
        ,null segment27
        ,null segment28
        ,null segment29
        ,null segment30
        ,xal.ENTERED_DR
        ,xal.ENTERED_CR
        ,xal.ACCOUNTED_DR
        ,xal.ACCOUNTED_CR
        ,null TRANSACTION_DATE
        ,null REFERENCE1
        ,null REFERENCE2
        ,null REFERENCE3
        ,null REFERENCE4 --
        ,msib.segment1||'-'||to_char(gxeh.transaction_date,'MON-RR')||'-'||
        gxeh.valuation_cost_type||'-'||gxeh.SOURCE_DOCUMENT_ID REFERENCE5       
        ,null REFERENCE6
        ,null REFERENCE7
        ,null REFERENCE8
        ,null REFERENCE9
        ,gjl.DESCRIPTION REFERENCE10
        ,null REFERENCE11
        ,null REFERENCE12
        ,null REFERENCE13
        ,null REFERENCE14
        ,null REFERENCE15
        ,null REFERENCE16
        ,null REFERENCE17
        ,null REFERENCE18
        ,null REFERENCE19
        ,null REFERENCE20
        ,null REFERENCE21
        ,null REFERENCE22
        ,null REFERENCE23
        ,null REFERENCE24
        ,null REFERENCE25
        ,null REFERENCE26
        ,null REFERENCE27
        ,null REFERENCE28
        ,null REFERENCE29
        ,null REFERENCE30
        ,null JE_BATCH_ID
        ,xah.PERIOD_NAME
        ,null JE_HEADER_ID
        ,null JE_LINE_NUM
        ,null CHART_OF_ACCOUNTS_ID
        ,null FUNCTIONAL_CURRENCY_CODE
        ,xal.CODE_COMBINATION_ID
        ,null DATE_CREATED_IN_GL
        ,null WARNING_CODE
        ,null STATUS_DESCRIPTION
        ,null STAT_AMOUNT
        ,null GROUP_ID
        ,null REQUEST_ID
        ,null SUBLEDGER_DOC_SEQUENCE_ID
        ,null SUBLEDGER_DOC_SEQUENCE_VALUE --
       ,msib.segment1 attribute1
       ,to_char(gxeh.transaction_date,'MON-RR')  attribute2
        ,null GL_SL_LINK_ID --
        ,null GL_SL_LINK_TABLE --
       ,gxeh.valuation_cost_type attribute3
       ,to_char(gxeh.SOURCE_DOCUMENT_ID) attribute4
       ,null attribute5
       ,null attribute6
       ,null attribute7
        ,null attribute8--
        ,null attribute9
        ,null attribute10
        ,null attribute11
        ,null attribute12
        ,null attribute13
        ,null attribute14
        ,null attribute15
        ,null attribute16
        ,null attribute17
        ,null attribute18
        ,null attribute19
        ,null attribute20
        ,null CONTEXT
        ,null CONTEXT2
        ,null INVOICE_DATE
        ,null TAX_CODE
        ,null INVOICE_IDENTIFIER
        ,null INVOICE_AMOUNT
        ,null CONTEXT3
        ,null USSGL_TRANSACTION_CODE
        ,null DESCR_FLEX_ERROR_MESSAGE
        ,null JGZZ_RECON_REF
        ,null REFERENCE_DATE
        ,-1 set_of_books_id
        ,null BALANCING_SEGMENT_VALUE
        ,null MANAGEMENT_SEGMENT_VALUE
        ,null FUNDS_RESERVED_FLAG
        ,null CODE_COMBINATION_ID_INTERIM
from  xla_ae_lines xal
    ,xla_ae_headers xah
    ,xla_events xe
    ,GL_JE_CATEGORIES_TL gjct
    ,gl_je_sources_tl gjst-- link GL
    ,GL_IMPORT_REFERENCES GIR
    ,gl_je_headers gjh
    ,gl_je_lines gjl
    ,gmf_xla_extract_headers gxeh
    ,fnd_application_tl fat
    ,gme_batch_header gbh
    ,mtl_system_items_b msib
    ,gl_code_combinations gcc
where 
      XAH.AE_HEADER_ID = XAL.AE_HEADER_ID
      and xah.APPLICATION_ID = xal.APPLICATION_ID
      and xah.ledger_id = xal.ledger_id
      and xah.LEDGER_ID = P_LEDGER_ID
      and xah.APPLICATION_ID=555
      and xah.application_id = fat.application_id
      and TO_CHAR(xal.ACCOUNTING_DATE,'MON/RR') = P_DATE
      and xah.je_category_name='RVAL'
      and xah.JE_CATEGORY_NAME = gjct.JE_CATEGORY_NAME
      and gjh.JE_SOURCE = gjst.JE_SOURCE_NAME -- link GL
      and xal.GL_SL_LINK_ID = gir.GL_SL_LINK_ID
      and gir.JE_HEADER_ID = gjh.JE_HEADER_ID
      and gjh.JE_HEADER_ID = gjl.JE_HEADER_ID
      and gir.JE_LINE_NUM = gjl.JE_LINE_NUM
      and xah.event_id = xe.event_id
      and xah.application_id = xe.application_id
      and xe.EVENT_ID = gxeh.EVENT_ID
      and gxeh.SOURCE_DOCUMENT_ID = gbh.batch_id(+)
      and gxeh.INVENTORY_ITEM_ID = msib.inventory_item_id(+)
      and gxeh.ORGANIZATION_ID = msib.organization_id(+)
      and xal.CODE_COMBINATION_ID = gcc.CODE_COMBINATION_ID;
          
    commit;
    
    select count(*) into jml from gl_1@db3;
    
    Fnd_File.PUT_LINE(Fnd_File.LOG,'SUKSES  ');
    Fnd_File.PUT_LINE(Fnd_File.LOG,'Line : '||jml);
    DBMS_OUTPUT.PUT_LINE('sukses');
    DBMS_OUTPUT.PUT_LINE('Line : '||jml);
END PN_UPLOAD_TO_GL_IFACE;

END PN_GL_IFACE_PKG;
/