Wednesday, 8 June 2016

Intercompany Goods In Transit Journal Entry



Intercompany Goods In Transit Journal Entry

Overview

Goods In Transit (GIT) will come into picture when goods sent by Sender Organization is not received at Receivers Organization by closing of the period. In order to tally accounts of Sender Org and Receiver Org, we need to have GIT Journal should be entered in GL.

This will consider all shipments which are not received at Receivers Org and create a Journal Entry by considering the quantity and price from AR invoice along with PO, Item and Quantity information in GIT journal lines.

Business Rules

  • ·        Alternative sites should have receiver inventory org code mapping 
  • ·        Receipt should not created either Manual or through ASN 
  • ·        Manual AP invoice should not be created. 
  • ·        AR invoice should be generated from the senders to get the Unit price and Qty 
  • ·        By considering all the above scenarios GIT Journal should be created in GL one per

SCRIPT

CREATE OR REPLACE PACKAGE APPS.XXEUR_IC_GIT_PKG 
AS
    gn_sob_id       PLS_INTEGER  :=  fnd_profile.value('GL_SET_OF_BKS_ID');
    gn_sender_org_id                   NUMBER;
    gn_sender_org_unit                 NUMBER;
   
    PROCEDURE main(
             errbuf                    OUT VARCHAR2
            ,retcode                   OUT NUMBER
            ,p_organization_id         IN  NUMBER
            ,p_supp_site_id            IN  NUMBER
            ,p_gl_period               IN  VARCHAR2
       );
    FUNCTION batch_received_status (p_shipment_number IN VARCHAR2,
                                    p_batch_number IN VARCHAR2)
      RETURN NUMBER;
    FUNCTION batch_received_qty (p_shipment_number IN VARCHAR2
                                 ,p_batch_number    IN VARCHAR2)
      RETURN NUMBER;
            
END XXEUR_IC_GIT_PKG;
/

CREATE OR REPLACE PACKAGE BODY APPS.XXEUR_IC_GIT_PKG
AS
   gn_request_id       NUMBER       :=apps.fnd_global.conc_request_id;
   gn_prog_appl_id     NUMBER       :=apps.fnd_global.prog_appl_id;
   gn_program_id       NUMBER       :=apps.fnd_global.conc_program_id;
   gn_user_id          NUMBER       :=apps.fnd_global.user_id;
   gn_login_id         NUMBER       :=apps.fnd_global.login_id;
   gn_org_id           NUMBER       :=apps.fnd_global.org_id;
   gn_bulk_limit       NUMBER       :=1000;
   gn_bulk_err         NUMBER       :=0;
   gd_sysdate          DATE         :=SYSDATE;

   gc_failure          VARCHAR2(1)  := 'E';
   gc_success          VARCHAR2(1)  := 'V';
   g_last_date         DATE;
   PROCEDURE write_log
   ( p_msg IN VARCHAR2 )
   IS
   BEGIN
    APPS.FND_FILE.PUT_LINE (FND_FILE.LOG , P_MSG ) ;
   END write_log ;

   PROCEDURE write_output
   ( p_msg IN VARCHAR2 )
   IS
   BEGIN
    dbms_output.put_line (p_msg) ;
    APPS.FND_FILE.PUT_LINE (FND_FILE.output , P_MSG ) ;
   END write_output ;

    PROCEDURE main(
             errbuf                    OUT VARCHAR2
            ,retcode                   OUT NUMBER
            ,p_organization_id         IN  NUMBER
            ,p_supp_site_id            IN  NUMBER
            ,p_gl_period               IN  VARCHAR2
       ) IS

    CURSOR lcu_shipment(p_sender_io NUMBER,p_sender_ou NUMBER) IS
    SELECT DISTINCT WND.delivery_id shipment_number
          ,WDD.cust_po_number
          ,WDD.lot_number
          ,WDD.inventory_item_id
          ,SUM(WDD.shipped_quantity)shipped_quantity
          ,WDD.ship_method_code
          ,WDD.ship_to_site_use_id
          ,WDD.organization_id sender_io
          ,WDD.org_id sender_ou
          ,TRX_LINE.unit_selling_price UNIT_PRICE
          ,TRX.invoice_currency_code currency_code
          ,SUBSTR(SUP.vendor_name,1,90) vendor_name
          ,PO.vendor_site_id
    FROM wsh_new_deliveries        WND
        ,wsh_delivery_details      WDD
        ,wsh_delivery_assignments  WDS
        ,hz_cust_accounts          HCA
        ,po_headers_all            PO
        ,wsh_delivery_stops_v      WDSV
        ,ra_customer_trx_lines_all TRX_LINE
        ,ra_customer_trx_all       TRX
        ,ap_suppliers              SUP
    WHERE  WND.delivery_id         =WDS.delivery_id
    AND WDS.delivery_detail_id  =WDD.delivery_detail_id
    AND HCA.cust_account_id     = WDD.customer_id
    AND WDD.organization_id     = nvl(p_sender_io,WDD.organization_id)
    AND WDD.org_id              = nvl(p_sender_ou,WDD.org_id)
    AND HCA.customer_class_code = 'INTERCOMPANY'
    AND SUP.vendor_type_lookup_code = 'INTERCOMPANY'
    AND HCA.attribute_category  ='Related Party Information'
    AND HCA.attribute20         = TO_CHAR(p_organization_id) --gn_org_id
    AND WDD.cust_po_number      = PO.segment1
    AND WND.delivery_id         = WDSV.delivery_id
    AND WDSV.activity_code      = 'PU'
    AND TRUNC(WDSV.actual_departure_date) <= g_last_date
    AND TRX_LINE.interface_line_context = 'ORDER ENTRY'
    AND TRX_LINE.interface_line_attribute6 = TO_CHAR(WDD.source_line_id)
    AND TRX_LINE.customer_trx_id = TRX.customer_trx_id
    AND WDD.org_id              = TRX.org_id
    AND WDD.org_id              = TRX_LINE.org_id
    AND PO.vendor_id            = SUP.vendor_id    
    AND NVL(WND.attribute15,'N') <> 'Y'
    GROUP BY
           WND.delivery_id
          ,WDD.cust_po_number
          ,WDD.lot_number
          ,WDD.inventory_item_id
          ,WDD.ship_method_code
          ,WDD.ship_to_site_use_id
          ,WDD.organization_id
          ,WDD.org_id
          ,TRX_LINE.unit_selling_price
          ,TRX.invoice_currency_code
          ,vendor_name
          ,PO.vendor_site_id;
         
    lc_segment1     gl_code_combinations.segment1%TYPE;
    lc_segment2        gl_code_combinations.segment2%TYPE;
    lc_segment3        gl_code_combinations.segment3%TYPE;
    lc_segment4        gl_code_combinations.segment4%TYPE;
    lc_segment5        gl_code_combinations.segment5%TYPE;
    lc_segment6        gl_code_combinations.segment6%TYPE;
    lc_segment7        gl_code_combinations.segment7%TYPE;
    lc_segment8        gl_code_combinations.segment8%TYPE;
    lc_segment9        gl_code_combinations.segment9%TYPE;
  
    ln_retcode                  NUMBER:=-1;
    ln_cnt                      NUMBER;   
    l_return_status             VARCHAR2(10);
    l_msg_count                 NUMBER;
    l_msg_data                     VARCHAR2(1000);
    lc_desc                     VARCHAR2(100);
    ln_amount                      NUMBER;
    lc_user_je_category_name     VARCHAR2(50);   
    lc_user_je_source_name        VARCHAR2(50);   
    lc_status                    VARCHAR2(10);   
    lc_actual_flag                VARCHAR2(1);   
    lc_debit_acct                VARCHAR2(50);
    ln_position                 NUMBER:=0;   
    ln_set_of_books_id          NUMBER;       
    lc_credit_acct              VARCHAR2(50);
    ln_batch_qty                NUMBER;
    lc_journal_name             VARCHAR2(100);
    lc_item_number              VARCHAR2(25);
    lc_status_flag              VARCHAR2(1);
    BEGIN
       g_last_date := LAST_DAY(TO_DATE (p_gl_period, 'MON-YY'));
       ln_position :=1;
       write_log('    ----- GIT Program Start -----');
       write_log('   ');
       write_output('    ----- GIT Program Start  -----');
       ln_cnt:=0;
       ln_position :=2;      
      
       write_log('gn_sender_org_id - '||gn_sender_org_id||' - gn_sender_org_unit - '||gn_sender_org_unit);
       ln_position :=3;
       write_log('gn_sender_org_id->'||gn_sender_org_id);
       write_log('gn_sender_org_unit->'||gn_sender_org_unit);
      
        BEGIN
         SELECT OOD.organization_id
               ,OOD.operating_unit
         INTO   gn_sender_org_id
               ,gn_sender_org_unit        
         FROM  ap_supplier_sites_all ASS
              ,org_organization_definitions OOD
         WHERE OOD.organization_code = ASS.vendor_site_code_alt
         AND   ASS.vendor_site_id    = p_supp_site_id;
        EXCEPTION
         WHEN others THEN
             gn_sender_org_id   :=NULL;
             gn_sender_org_unit :=NULL;
        END;

       FOR lrec_shipment IN lcu_shipment(gn_sender_org_id,gn_sender_org_unit)
       LOOP
      
       lc_status_flag := 'S';
       gn_sender_org_id   := lrec_shipment.sender_io;
       gn_sender_org_unit := lrec_shipment.sender_ou;
       
           BEGIN
           SELECT primary_vendor_item
             INTO lc_item_number
             FROM po_approved_supplier_list
            WHERE item_id               = lrec_shipment.inventory_item_id
              AND USING_ORGANIZATION_ID = gn_sender_org_id;
           EXCEPTION WHEN OTHERS
           THEN
           lc_status_flag := 'E';
           write_log('Item not exits in ASL - inventory_item_id ->'||lrec_shipment.inventory_item_id);
           END;
          
          
        IF lc_status_flag = 'S'
        THEN
      
        BEGIN
           ln_position :=4;
           write_log('lrec_shipment.shipment_number->'||lrec_shipment.shipment_number);
           write_log('lrec_shipment.lot_number->'||lrec_shipment.lot_number);         
            
             lc_user_je_category_name     :='Goods In Transit';
             lc_user_je_source_name       :='GIT';
             lc_status                    :='NEW';
             lc_actual_flag               :='A';
             lc_debit_acct                :='1582101';
             lc_credit_acct               := '2461501';
             ln_position                  :=5;    
             lc_journal_name              := 'GIT - '||lrec_shipment.vendor_name;
            
            
             SELECT HU.set_of_books_id
                   ,GCC.segment1
                   ,GCC.segment2
                   ,GCC.segment3
                   ,GCC.segment4
                   ,GCC.segment5
                   ,GCC.segment6
                   ,GCC.segment7
                   ,GCC.segment8
                   ,GCC.segment9
            INTO    ln_set_of_books_id
                   ,lc_segment1
                   ,lc_segment2
                   ,lc_segment3
                   ,lc_segment4
                   ,lc_segment5
                   ,lc_segment6
                   ,lc_segment7
                   ,lc_segment8
                   ,lc_segment9
            FROM gl_code_combinations GCC
                ,ap_supplier_sites_all ASSA
                ,hr_operating_units HU
            WHERE 1=1
            AND  GCC.code_combination_id = ASSA.accts_pay_code_combination_id
            AND  HU.organization_id=ASSA.org_id
            AND  ASSA.vendor_site_id=(SELECT vendor_site_id
                                      FROM   po_headers_all
                                      WHERE segment1=lrec_shipment.cust_po_number
                                        AND org_id = p_organization_id);
           
            ln_position :=6;
            IF batch_received_status(lrec_shipment.shipment_number,lrec_shipment.lot_number) = 0 THEN

                write_log('Inside if condition->'||lrec_shipment.lot_number);
                 ln_amount := lrec_shipment.unit_price * lrec_shipment.shipped_quantity;
                 lc_desc                      :='PO No: '||lrec_shipment.cust_po_number||' | Batch No: '||lrec_shipment.lot_number
                                                ||' | Price : '||lrec_shipment.unit_price||' | Qty : '||lrec_shipment.shipped_quantity;

                ln_position :=7;
                INSERT INTO gl_interface
                            ( status                   
                             ,set_of_books_id           
                             ,user_je_source_name       
                             ,user_je_category_name   
                             ,accounting_date           
                             ,currency_code            
                             ,date_created           
                             ,created_by               
                             ,actual_flag
                             ,segment1
                             ,segment2
                             ,segment3
                             ,segment4
                             ,segment5
                             ,segment6
                             ,segment7
                             ,segment8
                             ,segment9
                             ,reference10
                             ,entered_cr
                             ,entered_dr
                             ,ledger_id                               
                             ,attribute9
                             ,attribute1
                             ,reference4
                             )
                    VALUES ( lc_status   
                            ,ln_set_of_books_id
                            ,lc_user_je_source_name
                            ,lc_user_je_category_name
                            ,LAST_DAY(TRUNC(gd_sysdate))
                            ,lrec_shipment.currency_code
                            ,gd_sysdate
                            ,gn_user_id
                            ,lc_actual_flag
                            ,lc_segment1
                            ,lc_segment2
                            ,lc_credit_acct
                            ,lc_segment4
                            ,lc_segment5
                            ,lc_segment6
                            ,lc_segment7
                            ,lc_segment8
                            ,lc_segment9
                            ,lc_desc
                            ,ln_amount
                            ,null
                            ,ln_set_of_books_id     
                            ,lrec_shipment.vendor_name
                            ,lc_item_number
                            ,lc_journal_name
                            );   
                           
                ln_position :=8;
                INSERT INTO gl_interface
                            ( status                   
                             ,set_of_books_id           
                             ,user_je_source_name       
                             ,user_je_category_name   
                             ,accounting_date           
                             ,currency_code           
                             ,date_created           
                             ,created_by               
                             ,actual_flag
                             ,segment1
                             ,segment2
                             ,segment3
                             ,segment4
                             ,segment5
                             ,segment6
                             ,segment7
                             ,segment8
                             ,segment9
                             ,reference10
                             ,entered_cr  
                             ,entered_dr
                             ,ledger_id                         
                             ,attribute9
                             ,attribute1
                             ,reference4
                             )
                    VALUES ( lc_status   
                            ,ln_set_of_books_id
                            ,lc_user_je_source_name
                            ,lc_user_je_category_name
                            ,LAST_DAY(TRUNC(gd_sysdate))
                            ,lrec_shipment.currency_code
                            ,gd_sysdate
                            ,gn_user_id
                            ,lc_actual_flag
                            ,lc_segment1
                            ,lc_segment2
                            ,lc_debit_acct
                            ,lc_segment4
                            ,lc_segment5
                            ,lc_segment6
                            ,lc_segment7
                            ,lc_segment8
                            ,lc_segment9
                            ,lc_desc
                            ,null
                            ,ln_amount
                            ,ln_set_of_books_id            
                            ,lrec_shipment.vendor_name
                            ,lc_item_number
                            ,lc_journal_name
                            );           

                ln_cnt :=ln_cnt+1;
            ELSE
            ln_position :=9;       
                IF batch_received_qty (lrec_shipment.shipment_number,
                                       lrec_shipment.lot_number) < lrec_shipment.shipped_quantity THEN
                   ln_batch_qty := lrec_shipment.shipped_quantity- batch_received_qty (lrec_shipment.shipment_number, lrec_shipment.lot_number);
                   ln_amount                  :=lrec_shipment.unit_price*ln_batch_qty;          
                   lc_desc                      :='PO No: '||lrec_shipment.cust_po_number||' | Batch No: '||lrec_shipment.lot_number
                                                ||' | Price : '||lrec_shipment.unit_price||' | Qty : '||ln_batch_qty;                                                                                     
                   ln_position :=10;
                    INSERT INTO gl_interface
                                ( status                   
                                 ,set_of_books_id           
                                 ,user_je_source_name       
                                 ,user_je_category_name   
                                 ,accounting_date           
                                 ,currency_code           
                                 ,date_created           
                                 ,created_by               
                                 ,actual_flag
                                 ,segment1
                                 ,segment2
                                 ,segment3
                                 ,segment4
                                 ,segment5
                                 ,segment6
                                 ,segment7
                                 ,segment8
                                 ,segment9
                                 ,reference10
                                 ,entered_cr
                                 ,entered_dr
                                 ,ledger_id                         
                                 ,attribute9
                                 ,attribute1
                                 ,reference4
                                 )
                        VALUES ( lc_status   
                                ,ln_set_of_books_id
                                ,lc_user_je_source_name
                                ,lc_user_je_category_name
                                ,LAST_DAY(TRUNC(gd_sysdate))
                                ,lrec_shipment.currency_code
                                ,gd_sysdate
                                ,gn_user_id
                                ,lc_actual_flag
                                ,lc_segment1
                                ,lc_segment2
                                ,lc_credit_acct
                                ,lc_segment4
                                ,lc_segment5
                                ,lc_segment6
                                ,lc_segment7
                                ,lc_segment8
                                ,lc_segment9
                                ,lc_desc
                                ,ln_amount
                                ,null
                                ,ln_set_of_books_id             
                                ,lrec_shipment.vendor_name
                                ,lc_item_number
                                ,lc_journal_name
                                );   
                               
                    ln_position :=11;
                    INSERT INTO gl_interface
                                ( status                   
                                 ,set_of_books_id           
                                 ,user_je_source_name       
                                 ,user_je_category_name   
                                 ,accounting_date           
                                 ,currency_code           
                                 ,date_created           
                                 ,created_by               
                                 ,actual_flag
                                 ,segment1
                                 ,segment2
                                 ,segment3
                                 ,segment4
                                 ,segment5
                                 ,segment6
                                 ,segment7
                                 ,segment8
                                 ,segment9
                                 ,reference10
                                 ,entered_cr  
                                 ,entered_dr
                                 ,ledger_id                                
                                 ,attribute9
                                 ,attribute1
                                 ,reference4
                                 )
                        VALUES ( lc_status   
                                ,ln_set_of_books_id
                                ,lc_user_je_source_name
                                ,lc_user_je_category_name
                                ,LAST_DAY(TRUNC(gd_sysdate))
                                ,lrec_shipment.currency_code
                                ,gd_sysdate
                                ,gn_user_id
                                ,lc_actual_flag
                                ,lc_segment1
                                ,lc_segment2
                                ,lc_debit_acct
                                ,lc_segment4
                                ,lc_segment5
                                ,lc_segment6
                                ,lc_segment7
                                ,lc_segment8
                                ,lc_segment9
                                ,lc_desc
                                ,null
                                ,ln_amount
                                ,ln_set_of_books_id                        
                                ,lrec_shipment.vendor_name
                                ,lc_item_number
                                ,lc_journal_name
                                );           

                ln_position :=12;
                ln_cnt :=ln_cnt+1;
                END IF;

            END IF;
       
         EXCEPTION
          WHEN OTHERS THEN
            write_log('Error In Insert statement at position:'||ln_position||' Shipment Number:- '||lrec_shipment.shipment_number||' Lot Number:- '||lrec_shipment.lot_number||' Error Message>>> '||SQLERRM);
             write_output('Error In Insert statement at position:'||ln_position||' Shipment Number:- '||lrec_shipment.shipment_number||' Lot Number'||lrec_shipment.lot_number||' Error Message>>> '||SQLERRM);
         END;
            --ln_cnt :=ln_cnt+1;
       
           BEGIN
           UPDATE wsh_new_deliveries
              SET attribute15 = 'Y'
            WHERE delivery_id = lrec_shipment.shipment_number;
           END;
       
        END IF;
        write_log(RPAD('-', 80, '-' ));
       END LOOP;
       write_log('Inside if condition->'||ln_cnt);      
       COMMIT;

        write_output(RPAD(' ', 80, ' '));
        write_output('    Number of Records Inserted into GL Interface table :-> ' || ln_cnt*2);
        write_log('   ');
        write_log('    ------ xxeur_lot_number_status_upd_pkg.MAIN Exit------');
        write_log(RPAD('*', 80, '*' ));
        write_log('   ');
        write_output('   ');
        write_output('    ------ xxeur_lot_number_status_upd_pkg Exit------');
        write_output(RPAD('*', 80, '*' ));
        write_output('   ');
    EXCEPTION WHEN OTHERS THEN
       write_log('   Error Occured in The Procedure MAIN');
       write_log('   Error Message Is :-> ' || SQLERRM);
       write_output('Error Message Is>>> '||SQLERRM);
       write_log('  ');
       write_log('    ------ xxeur_lot_number_status_upd_pkg.MAIN Exit------');
       write_log(RPAD('*', 80, '*' ));
       write_log('   ');
    END main;
    FUNCTION batch_received_status (p_shipment_number IN VARCHAR2,
                                    p_batch_number    IN VARCHAR2)
      RETURN NUMBER
    IS
    ln_count NUMBER:=0;
    BEGIN
        SELECT count(1)
        INTO ln_count
        FROM rcv_transactions            RT
            ,rcv_shipment_headers        RSH
            ,rcv_shipment_lines          RSL
            ,rcv_transactions            DLRT
            ,mtl_material_transactions   MMT
            ,mtl_transaction_lot_numbers MTLN
            ,po_headers_all              PO
        WHERE 1=1
        AND RT.shipment_header_id   = RSH.shipment_header_id
        and RSH.shipment_num        = p_shipment_number
        AND RT.transaction_type     = 'RECEIVE'
        AND DLRT.transaction_type   = 'DELIVER'
        AND MTLN.lot_number         = p_batch_number
        AND DLRT.po_header_id       = RT.po_header_id
        AND DLRT.shipment_line_id   = RT.shipment_line_id
        AND RSL.shipment_header_id  = RSH.shipment_header_id
        AND RT.po_line_id           = RSL.po_line_id
        AND DLRT.transaction_id     = MMT.rcv_transaction_id
        AND MTLN.transaction_id     = MMT.transaction_id
        AND PO.po_header_id         = RSL.po_header_id
        AND TRUNC(RSH.creation_date) <= g_last_date
        AND RSH.vendor_site_id IN (SELECT DISTINCT ass.vendor_site_id
                                   FROM  ap_supplier_sites ass
                                        ,org_organization_definitions ood
                                   WHERE ood.organization_code  = ass.vendor_site_code_alt
                                   AND   ood.organization_id    = gn_sender_org_id
                                   AND   ood.operating_unit     = gn_sender_org_unit);       
    RETURN ln_count;
   
    EXCEPTION
        WHEN others THEN
        RETURN ln_count;
    END batch_received_status;

    FUNCTION batch_received_qty (p_shipment_number IN VARCHAR2
                                 ,p_batch_number    IN VARCHAR2)
      RETURN NUMBER
    IS
    ln_count NUMBER:=0;
    ln_qty   NUMBER;
    BEGIN
        SELECT NVL(SUM(MTLN.TRANSACTION_QUANTITY),0)
        INTO ln_qty
        FROM rcv_transactions            RT
            ,rcv_shipment_headers        RSH
            ,rcv_shipment_lines          RSL
            ,rcv_transactions            DLRT
            ,mtl_material_transactions   MMT
            ,mtl_transaction_lot_numbers MTLN
            ,po_headers_all              PO
        WHERE 1=1
        AND RT.shipment_header_id   = RSH.shipment_header_id
        and RSH.shipment_num        = p_shipment_number
        AND RT.transaction_type     = 'RECEIVE'
        AND DLRT.transaction_type   = 'DELIVER'
        AND MTLN.lot_number         = p_batch_number
        AND DLRT.po_header_id       = RT.po_header_id
        AND DLRT.shipment_line_id   = RT.shipment_line_id
        AND RSL.shipment_header_id  = RSH.shipment_header_id
        AND RT.po_line_id           = RSL.po_line_id
        AND DLRT.transaction_id     = MMT.rcv_transaction_id
        AND MTLN.transaction_id     = MMT.transaction_id
        AND PO.po_header_id         = RSL.po_header_id
        AND TRUNC(RSH.creation_date) <= g_last_date
        AND RSH.vendor_site_id IN (SELECT DISTINCT ass.vendor_site_id
                                   FROM  ap_supplier_sites ass
                                        ,org_organization_definitions ood
                                   WHERE ood.organization_code  = ass.vendor_site_code_alt
                                   AND   ood.organization_id    = gn_sender_org_id
                                   AND   ood.operating_unit     = gn_sender_org_unit);       
    RETURN ln_qty;
   
    EXCEPTION
        WHEN others THEN
        ln_qty :=0;       
        RETURN ln_qty;
    END batch_received_qty;
   
END XXEUR_IC_GIT_PKG;
/

Intercompany AR Receipt Creation & Application



Intercompany AR Receipt Creation & Application



Overview

An automatic receipt should be created and applied to the AR transaction when receiver org creates the payment for the invoice raised in their system.

Business Rules

  • ·        Alternative sites should have receiver inventory org code mapping 
  • ·        Only the payments related to intercompany suppliers would be considered. 
  • ·        Check number would be considered as AR receipt number. 
  • ·        Payment amount would be the receipt amount 
  • ·        Customer will be identified based on the intercompany org setup in the customer attribute.
  •       AR invoice number would be same as AP invoice number. And AP invoice will be identified from the payment.
  • ·        By considering all the above scenarios, AR receipt will be created in the sender org.

 Script

CREATE OR REPLACE PACKAGE APPS.XXEUR_INTERCOMPREC_CREATE_PKG 
IS
PROCEDURE XXEUR_INTERCOMPREC_CREATE_PROC(errorbuf VARCHAR2,retcode VARCHAR2);

PROCEDURE XXEUR_INTERCOMPREC_APPLY_PROC(errorbuf VARCHAR2,retcode VARCHAR2);
END;
/


CREATE OR REPLACE PACKAGE BODY APPS.XXEUR_INTERCOMPREC_CREATE_PKG 
IS
PROCEDURE XXEUR_INTERCOMPREC_CREATE_PROC(errorbuf VARCHAR2,retcode VARCHAR2) 

IS
   p_cr_id                 NUMBER;
   l_return_status         VARCHAR2 (1);
   l_msg_count             NUMBER:=0;
   l_msg_data              VARCHAR2 (10000);
   ln_inv_amount           NUMBER;
   lc_inv_number           VARCHAR2(40);
   lc_receipt_number       VARCHAR2(30):=NULL;
   ln_receipt_id           NUMBER;
   ln_receipt_method       NUMBER;
   ln_bank_acct_id         NUMBER;
   ln_rec_org              NUMBER;
   p_attribute_rec         ar_receipt_api_pub.attribute_rec_type;
   lc_ap_iban                 VARCHAR2(50);
   lc_ar_iban                 VARCHAR2(50);
   --p_attribute_rec_type     ar_receipt_api_pub.attribute_rec_type%ROWTYPE;
  
  CURSOR lcu_ap_dets 

IS
   SELECT  check_number receipt_num
             ,AIA.cust_account_id
             ,ACA.check_id
             ,ACA.amount  amount
             ,ACA.currency_code
             ,AIA.ic_inv_org   
             ,iban SUPPLIER_IBAN

      FROM  ap_invoice_payments_all   AIPA
           ,ap_checks_all             ACA
           ,(SELECT ASPS.vendor_site_code_alt ic_inv_org
                 ,AIA.invoice_num
                 ,AIA.org_id
                 ,AIA.invoice_amount
                 ,HCA.cust_account_id
                 ,AIA.invoice_currency_code
                 ,AIA.invoice_id
                 ,EBA.iban 

             FROM ap_invoices_all          AIA
                 ,ap_suppliers             ASP
                 ,ap_supplier_sites_all    ASPS
                 ,hz_cust_accounts         HCA
                 ,iby_external_payees_all  EPA               

                 ,iby_pmt_instr_uses_all   PIU                                                  
                ,iby_ext_bank_accounts    EBA 
            WHERE AIA.vendor_id=ASP.vendor_id
              AND ASP.vendor_id=ASPS.vendor_id
              AND ASP.vendor_type_lookup_code='INTERCOMPANY'
              AND AIA.invoice_type_lookup_code='STANDARD'
              AND AIA.vendor_site_id=ASPS.vendor_site_id
              AND ASPS.vendor_site_code_alt IS NOT NULL
              AND HCA.attribute20=AIA.org_id

              AND HCA.customer_class_code='INTERCOMPANY'
              AND ASPS.vendor_site_id = EPA.supplier_site_id
              AND ASPS.org_id         = EPA.org_id
              AND EPA.ext_payee_id    = PIU.ext_pmt_party_id  
              AND PIU.instrument_id   = EBA.ext_bank_account_id                  
              ) AIA
       WHERE  AIA.invoice_id= AIPA.invoice_id
         AND  ACA.check_id=AIPA.check_id
         --AND TRUNC(ACA.creation_date)=TRUNC(SYSDATE)
         AND NOT EXISTS
                ( SELECT 1
                    FROM ar_cash_receipts_all ACA
                   WHERE ACA.attribute1=TO_CHAR(ACA.check_id)
                     AND ACA.attribute1 IS NOT NULL)
       GROUP BY ACA.check_number
               ,AIA.cust_account_id
               ,ACA.check_id
               ,ACA.currency_code
               ,AIA.ic_inv_org
               ,ACA.amount              

               ,iban;            
BEGIN

apps.fnd_file.put_line(apps.fnd_file.LOG,'========================================================');

   FOR lcr_apdets IN  lcu_ap_dets
     LOOP             
                ---- To get receipt method and bank details----- 
           BEGIN                                       
                 SELECT CBA.iban_number
                       ,ARM.receipt_method_id
                       ,ARMA.remit_bank_acct_use_id
                  INTO lc_ar_iban
                      ,ln_receipt_method
                      ,ln_bank_acct_id
                 FROM  ar_receipt_method_accounts_all ARMA
                      ,ar_receipt_methods             ARM
                      ,ce_bank_acct_uses_all          CBAU
                      ,ce_bank_accounts               CBA
                WHERE ARM.receipt_method_id = ARMA.receipt_method_id
                  AND CBA.bank_account_id   = CBAU.bank_account_id
                  AND bank_acct_use_id      = remit_bank_acct_use_id
                  AND CBA.iban_number       = lcr_apdets.supplier_iban
                  AND ARM.name              = 'Intercompany Receipts';
          
           EXCEPTION
           WHEN NO_DATA_FOUND THEN
           fnd_file.put_line(fnd_file.LOG,'No data found for Inter company receipt name');
          
           WHEN OTHERS THEN
           fnd_file.put_line(fnd_file.LOG,'Error fetching Inter comp receipt method');
          
           END;
       
            
                
        
    
        IF lc_ar_iban is NOT NULL THEN--lc_ap_iban=lc_ar_iban THEN
      
             ---------------Get org to create receipt --------------
            
           BEGIN
          
           SELECT operating_unit
             INTO ln_rec_org
             FROM org_organization_definitions 
            WHERE organization_code=lcr_apdets.ic_inv_org;
           
            EXCEPTION
            WHEN OTHERS THEN
            fnd_file.put_line(fnd_file.LOG,'Error fetching org to create receipt ');
          
           END;
    
     
          apps.fnd_global.resp_id,apps.fnd_global.resp_appl_id); 

           mo_global.init('AR');
           mo_global.set_policy_context('S',ln_rec_org);

           fnd_msg_pub.initialize;  
            
           fnd_file.put_line (fnd_file.LOG,'TRACK1');
          
           p_attribute_rec.attribute1:=lcr_apdets.check_id;
         
                apps.ar_receipt_api_pub.create_cash
            (p_api_version                   => 1.0
            ,p_init_msg_list                 => apps.FND_API.G_FALSE
            ,p_commit                        => apps.FND_API.G_FALSE
            ,p_validation_level              => apps.fnd_api.g_valid_level_full
            ,x_return_status                 => l_return_status
            ,x_msg_count                     => l_msg_count
            ,x_msg_data                      => l_msg_data
            ,p_usr_currency_code             => NULL
            ,p_currency_code                 => lcr_apdets.currency_code--'EUR'--NULL
            ,p_usr_exchange_rate_type        => NULL
            ,p_exchange_rate_type            => NULL
            ,p_exchange_rate                 => NULL
            ,p_exchange_rate_date            => NULL
            ,p_amount                        => lcr_apdets.amount
            ,p_factor_discount_amount        => NULL   
            ,p_receipt_number                => lcr_apdets.receipt_num
            ,p_receipt_date                  => SYSDATE-- p_receipt_date
            ,p_gl_date                       => SYSDATE--p_gl_date
            ,p_maturity_date                 => NULL
            ,p_postmark_date                 => NULL
            ,p_customer_id                   => lcr_apdets.cust_account_id                 

,p_customer_name                 => NULL
            ,p_customer_number               => NULL
            ,p_customer_bank_account_id      => NULL
            ,p_customer_bank_account_num     => NULL
            ,p_customer_bank_account_name    => NULL
            ,p_customer_site_use_id          => NULL
            ,p_customer_receipt_reference    => NULL
            ,p_remittance_bank_account_id    => ln_bank_acct_id
            ,p_remittance_bank_account_num   => NULL
            ,p_remittance_bank_account_name  => NULL
            ,p_deposit_date                  => NULL
            ,p_receipt_method_id             => ln_receipt_method

            ,p_receipt_method_name           => NULL
            ,p_doc_sequence_value            => NULL
            ,p_ussgl_transaction_code        => NULL
            ,p_anticipated_clearing_date     => NULL
            ,p_called_from                   => NULL--'PLSQL'
            ,p_comments                      => 'Intercompany Receipt'--p_comments
            ,p_attribute_rec                 =>p_attribute_rec
            --   ***  Notes Receivable Additional Information  ***
            ,p_issuer_name                   => NULL
            ,p_issue_date                    => NULL
            ,p_issuer_bank_branch_id         => NULL
            ,p_org_id                        => ln_rec_org
                 --   ** OUT NOCOPY variables
            ,p_cr_id                         => ln_receipt_id
            );   
     
             

                     IF l_return_status = 'S'
                        THEN
                           COMMIT;
                        
                           FND_FILE.PUT_LINE (
                              FND_FILE.LOG,
                              'Receipt created successfully'       );
                           FND_FILE.PUT_LINE (FND_FILE.LOG,
                                              'Return status-' || l_return_status);
                           FND_FILE.PUT_LINE (FND_FILE.LOG,
                                              'Message count-' || l_msg_count);
                           FND_FILE.PUT_LINE (FND_FILE.LOG, 'message data' || l_msg_data);
                        ELSE
                           ROLLBACK;
                           FND_FILE.PUT_LINE (FND_FILE.LOG,
                                              'Return status' || '-' || l_return_status);
                           FND_FILE.PUT_LINE (FND_FILE.LOG,
                                              'Message count' || '-' || l_msg_count);
                           FND_FILE.PUT_LINE (FND_FILE.LOG,
                                              'Message data' || '-' || l_msg_data);
                          
                              IF l_msg_count = 1    THEN
                               fnd_file.put_line (fnd_file.LOG,'Error messgae: ' || '--' || l_msg_data);
                               ELSIF l_msg_count > 1
                               THEN
                                LOOP
                                 l_msg_data :=
                                    fnd_msg_pub.get (fnd_msg_pub.g_next, fnd_api.g_false);

                                 IF l_msg_data IS NULL
                                 THEN
                                    EXIT;
                                 END IF;
                                   fnd_file.put_line (fnd_file.LOG,  'Error message:-'
                                                      || '  '
                                                      || l_msg_data);
                              END LOOP;
                                
                              END IF;
                          
                     END IF;
            
            
                              
                   apps.fnd_file.put_line(apps.fnd_file.LOG,'Receipt Id -->'||ln_receipt_id||' ,Receipt Num-->'||lcr_apdets.receipt_num);
              
                   apps.fnd_file.put_line(apps.fnd_file.LOG,'========================================================');
        ELSE
          
                   apps.fnd_file.put_line(apps.fnd_file.LOG,'Supplier Bank accoun IBAN does not macth with Intercompany IBAN  for Check Num-->'||lcr_apdets.receipt_num);       
                  
       END IF;
      
      
      
     END LOOP;
    
     EXCEPTION
       WHEN OTHERS THEN
       apps.fnd_file.put_line(apps.fnd_file.log,'Final exception Error :-> '||SQLERRM);
 END ;


PROCEDURE XXEUR_INTERCOMPREC_APPLY_PROC(errorbuf VARCHAR2,retcode VARCHAR2) 

IS
   p_cr_id                 NUMBER;
   l_return_status         VARCHAR2 (1);
   l_msg_count             NUMBER:=0;
   l_msg_data              VARCHAR2 (10000);
   ln_inv_amount           NUMBER;
   lc_inv_number           VARCHAR2(40);
   lc_receipt_number       VARCHAR2(30):=NULL;
   ln_receipt_id           NUMBER;
   ln_receipt_method       NUMBER;
   ln_bank_acct_id         NUMBER;
   ln_rec_org              NUMBER;
  
  CURSOR lcu_ar_dets IS
             SELECT AIPA.amount ap_amt
                   ,RCT.customer_trx_id
                   ,AIA.invoice_num
                   ,RCT.trx_number    
                   ,ACR.receipt_number
                   ,APS.amount_due_remaining         
                   ,ACR.org_id
                   ,ACR.cash_receipt_id
              FROM ar_cash_receipts_all          ACR
                  ,ap_checks_all                 ACA
                  ,ap_invoices_all               AIA
                  ,ap_invoice_payments_all       AIPA
                  ,ra_customer_trx_all           RCT
                  ,hz_cust_accounts              HCA
                  ,ar_payment_schedules_all      APS
             WHERE  TO_CHAR(ACA.check_id)=ACR.attribute1
               AND AIPA.invoice_id=AIA.invoice_id
               AND ACA.check_id=AIPA.check_id
               AND AIA.invoice_num=RCT.trx_number
               AND AIA.org_id=HCA.attribute20
               AND HCA.cust_account_id=ACR.pay_from_customer
               AND HCA.customer_class_code='INTERCOMPANY'  
               AND RCT.customer_trx_id=APS.customer_trx_id
               AND APS.status='OP'
               AND AIPA.amount<=APS.amount_due_remaining
               ORDER BY receipt_number;

BEGIN

     --apps.fnd_file.put_line(apps.fnd_file.LOG,'========================================================');
   FOR lcr_ardets IN  lcu_ar_dets
  
     LOOP
          
     fnd_file.put_line (fnd_file.LOG,'==========================================================');
         
apps.fnd_global.resp_id,apps.fnd_global.resp_appl_id);

           mo_global.init('AR');
         
           mo_global.set_policy_context('S',lcr_ardets.org_id);                     
          
           fnd_msg_pub.initialize;  
            
           fnd_file.put_line (fnd_file.LOG,'TRACK1');
         
           fnd_file.put_line (fnd_file.LOG,'Receipt Num--'||lcr_ardets.receipt_number);
          
           fnd_file.put_line (fnd_file.LOG,'Invoice Num--'||lcr_ardets.trx_number);
          
          
                 ar_receipt_api_pub.apply
                            ( p_api_version             => 1.0,
                            p_init_msg_list             => FND_API.G_TRUE,
                            p_commit                    => FND_API.G_TRUE,
                            p_validation_level          => FND_API.G_VALID_LEVEL_FULL,
                            p_cash_receipt_id           => lcr_ardets.cash_receipt_id,
                            p_customer_trx_id           => lcr_ardets.customer_trx_id,
                            p_amount_applied            => lcr_ardets.ap_amt,
                            x_return_status             => l_return_status,
                            x_msg_count                 => l_msg_count,
                            x_msg_data                  => l_msg_data
                            );
                  
                    

                     IF l_return_status = 'S'                    THEN
                          COMMIT;
                           fnd_file.put_line (fnd_file.LOG,'Receipt applied successfully'       );
                           fnd_file.put_line (fnd_file.LOG,'Return status' || l_return_status);
                           fnd_file.put_line (fnd_file.LOG,'Message count' || l_msg_count);
                           fnd_file.put_line (fnd_file.LOG,'Message data' || l_msg_data);
                        ELSE
                           ROLLBACK;
                           fnd_file.put_line (fnd_file.LOG,'Return status' || '-' || l_return_status);
                           fnd_file.put_line (fnd_file.LOG,'Message count' || '-' || l_msg_count);
                           fnd_file.put_line (fnd_file.LOG,'Message data'  || '-' || l_msg_data);
                          
                              IF l_msg_count = 1    THEN
                               fnd_file.put_line (fnd_file.LOG,'Error messgae: ' || '  ' || l_msg_data);
                               ELSIF l_msg_count > 1
                               THEN
                                LOOP
                                 l_msg_data :=
                                    fnd_msg_pub.get (fnd_msg_pub.g_next, fnd_api.g_false);

                                 IF l_msg_data IS NULL
                                 THEN
                                    EXIT;
                                 END IF;
                                   fnd_file.put_line (fnd_file.LOG,  'Error messgae:-'|| '-'|| l_msg_data);
                              END LOOP;
                                
                              END IF;
                          
                     END IF;                                                                apps.fnd_file.put_line(apps.fnd_file.LOG,'========================================================');      
      
     END LOOP;
    
     EXCEPTION
       WHEN OTHERS THEN
       apps.fnd_file.put_line(apps.fnd_file.log,'Final exception Error :-> '||SQLERRM);
 END ;

END;
/