Infolinks

Thursday 21 June 2012

Package specification to import Purchase Orders data

/*--============================================ZORAN instace logic added on 17OCT2011========================
      ELSIF p_instance_name = 'ZORAN' THEN
      FND_FILE.PUT_LINE(FND_FILE.LOG,'Importing ZORAN instace');       
        import_zoran(p_rec_status    => 'E'
                  ,p_instance_name => p_instance_name
                  ,x_errbuf        => lc_errbuf
                  ,x_retcode       => ln_retcode
                  ,x_header_cnt    => ln_po_header_cnt
                  ,x_lines_cnt     => ln_po_lines_cnt
                  ,x_dist_cnt      => ln_po_dist_cnt
                  ,x_req_id        => ln_imp_req_id);
*/


CREATE OR REPLACE PACKAGE XXCSR_PO_PUR_ORDR_IMPORT_PKG AS

-- +==============================================================================+
-- | Oracle Solution Services India PvMSIT. Ltd., India                           |
-- +==============================================================================+
-- |                                                                              |
-- |$Id:$                                                                         |
-- | FILENAME                                                                     |
-- | XXCSR_PO_PUR_ORDR_IMPORT_PKG.sql                                             |
-- |                                                                              |
-- | Description:                                                                 |
-- | Package specification to import Purchase Orders data                         |
-- |                                                                              |
-- | NOTES                                                                        |
-- | Created by Amod Joshi                                                        |
-- |                                                                              |
-- | History:                                                                     |
-- |                                                                              |
-- | Version  Date          Author       Remarks                                  |
-- | Draft 1A 25-SEP-2009   Amod Joshi   Initial draft version                    |
-- +==============================================================================+
PROCEDURE interface_table_summary(x_errbuf             OUT    VARCHAR2
                                 ,x_retcode            OUT    NUMBER
                                 ,p_interface_table    IN     VARCHAR2
                                 ,p_request_id         IN     NUMBER
                                 ,p_total_record_count IN     NUMBER);
                                
PROCEDURE main
   (x_errbuf          OUT NOCOPY VARCHAR2
   ,x_retcode         OUT NOCOPY NUMBER
   ,p_data_file1      IN         VARCHAR2
   ,p_data_file2      IN         VARCHAR2
   ,p_data_file3      IN         VARCHAR2
   ,p_instance_name   IN         VARCHAR2
   ,p_process_error   IN     VARCHAR2
   );

END XXCSR_PO_PUR_ORDR_IMPORT_PKG;
/

SHOW ERRORS;

---EXIT;

-- +=================================================================+
-- | End of Script                                                   |
-- +=================================================================+



create or replace
PACKAGE BODY XXCSR_PO_PUR_ORDR_IMPORT_PKG AS

-- +==============================================================================+
-- | Oracle Solution Services India PvMSIT. Ltd., India                           |
-- +==============================================================================+
-- |                                                                              |
-- |$Id:$                                                                         |
-- | FILENAME                                                                     |
-- | XXCSR_PO_PUR_ORDR_IMPORT_PKG.sql                                             |
-- |                                                                              |
-- | Description:                                                                 |
-- | Package body to import Purchase Orders data                                  |
-- |                                                                              |
-- | NOTES                                                                        |
-- | Created by Amod Joshi                                                        |
-- |                                                                              |
-- | History:                                                                     |
-- |                                                                              |
-- | Version  Date          Author       Remarks                                  |
-- | Draft 1A 25-SEP-2009   Amod Joshi   Initial draft version                    |
-- | Draft 1B 02-FEB-2010   Nishka K.S   Based on MDT 1.17                        |
-- | Draft 1C 02-FEB-2010   Nishka K.S   Based on MDT 1.18                        |
-- | Draft 1D 12-FEB-2010   Nishka K.S   Based on MDT 1.19                        |
-- | Draft 1E 18-FEB-2010   Nishka K.S   Based on MDT 1.20                        |
-- | Draft 1F 19-FEB-2010   Nishka K.S   Based on MDT 1.21                        |
-- | Draft 1G 21-FEB-2010   Nishka K.S   Based on MDT 1.22                        |
-- | Draft 1H 23-FEB-2010   Nishka K.S   Based on MDT 1.23                        |
-- +==============================================================================+

gn_conc_req_id                  NUMBER          := FND_GLOBAL.CONC_REQUEST_ID;

PROCEDURE interface_table_summary(x_errbuf             OUT    VARCHAR2
                                 ,x_retcode            OUT    NUMBER
                                 ,p_interface_table    IN     VARCHAR2
                                 ,p_request_id         IN     NUMBER
                                 ,p_total_record_count IN     NUMBER)

-- +====================================================================+
-- | Name        : interface_table_summary                              |
-- |                                                                    |
-- | Description : This procedure is used to produce table data summary |
-- |                                                                    |
-- | Parameters  : p_interface_table      IN VARCHAR2                   |
-- |               p_request_id           IN NUMBER                     |
-- |               p_total_record_count   IN NUMBER                     |
-- |                                                                    |
-- | Returns     : None                                                 |
-- |                                                                    |
-- +====================================================================+

IS
lv_stmt               VARCHAR2(1500);
ln_error              NUMBER  := 0;
ln_valid              NUMBER  := 0;
ln_success            NUMBER  := 0;
ln_total              NUMBER  := 0;
ln_process            NUMBER  := 0;
TYPE lcu_summary_type IS REF CURSOR;
lcu_summary_rec       lcu_summary_type;
gn_request_id         NUMBER(15)   := FND_GLOBAL.CONC_REQUEST_ID;

BEGIN
  x_retcode := 0;
  ln_total := p_total_record_count;
  lv_stmt  := 'SELECT count(*) '||
                    --',SUM(DECODE(record_status,''E'',1,0))'||
                    --',SUM(DECODE(record_status,''C'',1,0) + DECODE(record_status,''P'',1,0))'||
                    --',SUM(DECODE(record_status,record_status,1)) '||
              ' FROM '||p_interface_table||
              ' WHERE request_id ='||p_request_id||
              ' AND  process_code != ''ACCEPTED''';


  OPEN lcu_summary_rec FOR lv_stmt;
  LOOP
    EXIT WHEN lcu_summary_rec%NOTFOUND;
    FETCH lcu_summary_rec INTO ln_error;
  END loop;
  CLOSE lcu_summary_rec;

  --ln_process := ln_success + ln_valid;
  FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'=====================================================');
  FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'Summary for interim table : '||p_interface_table);
  FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'=====================================================');
  FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' Records Loaded           : '||NVL(ln_total,0));
  FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' Records Errored          : '||NVL(ln_error,0));
  FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' Records Processed        : '||NVL((ln_total-ln_error),0));
  FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'=====================================================');

  IF ln_error = ln_total AND ln_error > 0 THEN
     x_errbuf  := 'All records have errored';
     x_retcode := 2;
  ELSIF ln_error > 0 THEN
     x_errbuf  := 'Some records have errored';
     x_retcode := 1;
  ELSIF ln_success = ln_total THEN
     x_retcode := 0;
  END IF;

EXCEPTION
  WHEN OTHERS THEN
  x_retcode := 2;
  x_errbuf  := SUBSTR(SQLERRM,1,500);
  FND_FILE.PUT_LINE(FND_FILE.OUTPUT, x_errbuf);
END interface_table_summary;

PROCEDURE import_sirf
   (p_rec_status      IN         VARCHAR2
   ,p_instance_name   IN         VARCHAR2
   ,x_errbuf          OUT NOCOPY VARCHAR2
   ,x_retcode         OUT NOCOPY NUMBER
   ,x_header_cnt      OUT NOCOPY NUMBER
   ,x_lines_cnt       OUT NOCOPY NUMBER
   ,x_dist_cnt        OUT NOCOPY NUMBER
   ,x_req_id          OUT NOCOPY NUMBER
   )
IS
--
  CURSOR lcu_po_header_data (pv_rec_status VARCHAR2)
  IS
      SELECT XHEC.rowid,XHEC.*
      FROM   xxcsr_po_headers_int XHEC
      WHERE  XHEC.record_status = pv_rec_status
      AND    XHEC.request_id = gn_conc_req_id
      AND    XHEC.source_instance = p_instance_name;

  CURSOR lcu_po_lines_data (pv_rec_status VARCHAR2)
  IS
      SELECT XHEC.rowid,XHEC.*
      FROM   xxcsr_po_lines_int XHEC
      WHERE  XHEC.record_status = pv_rec_status
      AND    XHEC.request_id = gn_conc_req_id
      AND    XHEC.source_instance = p_instance_name;

  CURSOR lcu_po_dist_data (pv_rec_status VARCHAR2)
  IS
      SELECT XHEC.rowid,XHEC.*
      FROM   xxcsr_po_distributions_int XHEC
      WHERE  XHEC.record_status = pv_rec_status
      AND    XHEC.request_id = gn_conc_req_id
      AND    XHEC.source_instance = p_instance_name;

  CURSOR lcu_chk_currency (pv_currency_code VARCHAR2)
  IS
      SELECT currency_code
      FROM   fnd_currencies
      WHERE  currency_code = pv_currency_code;

  CURSOR lcu_item_cat_id (pv_seg1 VARCHAR2,pv_seg2 VARCHAR2,pv_seg3 VARCHAR2,pv_seg4 VARCHAR2,pv_seg5 VARCHAR2)
  IS
      SELECT category_id
      FROM   mtl_categories
      WHERE  segment1 = pv_seg1
      AND    segment2 = pv_seg2
      AND    segment3 = pv_seg3
      AND    segment4 = pv_seg4
      AND    segment5 = pv_seg5;

  CURSOR lcu_get_int_header_id (pv_po_number VARCHAR2)
  IS
      SELECT interface_header_id
      FROM   po_headers_interface
      WHERE  document_num = pv_po_number
      ORDER BY 1 DESC;

  CURSOR lcu_get_int_line_id (pv_po_number VARCHAR2
                             ,pv_po_line_number VARCHAR2
                             ,p_shipment_num    VARCHAR2)
  IS
      SELECT interface_line_id
      FROM   po_lines_interface
      WHERE  document_num = pv_po_number
      AND    line_num     = pv_po_line_number
      AND    shipment_num = p_shipment_num;

  CURSOR lcu_account_id (pv_seg1 VARCHAR2,pv_seg2 VARCHAR2,pv_seg3 VARCHAR2,pv_seg4 VARCHAR2,pv_seg5 VARCHAR2,pv_seg6 VARCHAR2,pv_seg7 VARCHAR2)
  IS
      SELECT code_combination_id
      FROM   gl_code_combinations_kfv
      WHERE  segment1 = pv_seg1
      AND    segment2 = pv_seg2
      AND    segment3 = pv_seg3
      AND    segment4 = pv_seg4
      AND    segment5 = pv_seg5
      AND    segment6 = pv_seg6
      AND    segment7 = pv_seg7;

  CURSOR lcu_charge_account_id  (p_ship_location NUMBER, p_second_inv VARCHAR2)
  IS
      SELECT GCC.code_combination_id
      FROM   gl_code_combinations gcc
            ,mtl_secondary_inventories msi
            ,hr_locations hl
      WHERE  MSI.material_account         = GCC.code_combination_id
      AND    MSI.secondary_inventory_name = p_second_inv
      AND    MSI.organization_id          = HL.inventory_organization_id
      AND    HL.location_id               = p_ship_location;

  CURSOR lcu_get_sob_id (pv_sob_name VARCHAR2)
  IS
      SELECT set_of_books_id
      FROM   gl_sets_of_books
      WHERE  name = pv_sob_name;


  --sjacob commented below
  CURSOR lcu_location_id (pv_loc_code VARCHAR2)
  IS
      SELECT location_id
      FROM   hr_locations
      WHERE  location_code = pv_loc_code;

  CURSOR lcu_item_id(p_seg1 IN VARCHAR2
                    ,p_seg2 IN VARCHAR2
                    ,p_inv_org IN VARCHAR2)
  IS
      SELECT MSIB.inventory_item_id
      FROM mtl_system_items_b MSIB
          ,mtl_parameters MP
     WHERE MP.organization_id = MSIB.organization_id
     AND MSIB.segment1 = p_seg1
     AND NVL(MSIB.segment2,-99) = NVL(p_seg2,-99)
     AND MP.organization_code = p_inv_org;

  CURSOR lcu_item_attr17(p_seg1 IN VARCHAR2
                        ,p_seg2 IN VARCHAR2)
  IS
  SELECT MSI.attribute17
  FROM mtl_system_items MSI
      ,mtl_parameters MP
  WHERE MP.organization_id = MSI.organization_id
  AND MSI.segment1 = p_seg1
  AND MP.organization_id = MP.master_organization_id
  AND NVL(MSI.segment2,'XX') = NVL(p_seg2,'XX');

/*
  CURSOR lcu_vendor_der(p_vendor IN VARCHAR2)
  IS
      SELECT vendor_id
      FROM po_vendors
      WHERE attribute15 = p_vendor;
*/

  CURSOR lcu_vendor_tsmc(p_vendor IN VARCHAR2)
  IS
  SELECT vendor_id
  FROM po_vendors
  WHERE vendor_name = p_vendor;


  CURSOR lcu_vendor_der(p_srf_segment1 IN VARCHAR2)
  IS
      SELECT PV.vendor_id
      FROM   po_vendors PV
            ,fnd_lookup_values FLV
      WHERE  PV.segment1 = FLV.description
      AND    FLV.meaning = p_srf_segment1
      AND    PV.enabled_flag = 'Y'
      AND    SYSDATE BETWEEN NVL(PV.start_date_active,SYSDATE) AND NVL(PV.end_date_active,SYSDATE)
      AND    SYSDATE BETWEEN NVL(FLV.start_date_active,SYSDATE) AND NVL(FLV.end_date_active,SYSDATE)
      AND    FLV.enabled_flag = 'Y'
      AND    FLV.lookup_type = 'XXSIRF_CNV_SUPPLIER_NUMBERS';

  CURSOR lcu_hr_loc(p_location_code IN VARCHAR2)
  IS
      SELECT HLA.location_id
      FROM hr_locations_all HLA
      WHERE HLA.location_code = p_location_code;

  CURSOR lcu_hr_loc_org(p_location_code IN VARCHAR2)
  IS
      SELECT HLA.location_id
            ,OOD.organization_code
      FROM hr_locations_all HLA
          ,org_organization_definitions OOD
      WHERE HLA.location_code = p_location_code
      AND   OOD.organization_id = HLA.inventory_organization_id;


  CURSOR lcu_ship_po_line(p_po_number IN VARCHAR2
                         ,p_shipment_num IN NUMBER
                         ,p_line_num     IN NUMBER)
  IS
      SELECT ship_to_location
      FROM xxcsr_po_lines_int XPLI
      WHERE XPLI.po_number = p_po_number
      AND   XPLI.shipment_num = p_shipment_num
      AND   XPLI.line_num = p_line_num
      AND    XPLI.source_instance = p_instance_name;

  CURSOR lcu_func_curr(p_org_id IN NUMBER)
  IS
      SELECT GL.currency_code
      FROM hr_operating_units HOU
          ,gl_ledgers GL
      WHERE GL.ledger_id = HOU.set_of_books_id
      AND   HOU.organization_id = p_org_id;

  CURSOR lcu_get_segment2 (pc_item_type      VARCHAR2
                          ,pc_org_code       VARCHAR2
                          ,pc_product_family VARCHAR2
                          )
  IS
      SELECT item_segment2
           -- ,bom_check
            --,template_name
      FROM   xxcsr_item_conv_map_int
      WHERE  item_type         = pc_item_type
      AND    organization_code = pc_org_code
      AND    product_family    = pc_product_family
      AND    instance_name     = 'SIRF'
      AND    bom_check IS NULL;

  CURSOR lcu_po_item_line(p_po_number IN VARCHAR2)
  IS
      SELECT XPLI.itm_seg1_item_num
         ,XPLI.attr18
         ,XPLI.item_type
         ,XPLI.item_org_name
      FROM xxcsr_po_lines_int XPLI
      WHERE XPLI.po_number = p_po_number
      AND   XPLI.itm_seg1_item_num IS NOT NULL
      AND   XPLI.source_instance = p_instance_name;

  CURSOR lcu_get_product_family (pc_seg1 VARCHAR2)
  IS
      SELECT MSIB.attribute18
      FROM   mtl_system_items_b MSIB
            ,org_organization_definitions OOD
      WHERE  MSIB.organization_id  = OOD.organization_id
      AND    OOD.organization_code = 'CMO'
      AND    MSIB.segment1         = pc_seg1;

  CURSOR lcu_check_lines(p_po_number IN VARCHAR2)
  IS
      SELECT 1
      FROM xxcsr_po_lines_int XPLI
      WHERE XPLI.po_number = p_po_number;

  CURSOR lcu_prim_uom(p_org_code IN VARCHAR2
                     ,p_item_id IN NUMBER)
  IS
      SELECT MSIB.primary_unit_of_measure
      FROM mtl_system_items_b MSIB
          ,mtl_parameters MP
      WHERE MSIB.inventory_item_id = p_item_id
      AND   MSIB.organization_id = MP.master_organization_id
      AND   MP.organization_code = p_org_code;


  CURSOR lcu_vendor_site(p_vendor_id IN NUMBER
                        ,p_org_id IN NUMBER)
  IS
      SELECT POVS.vendor_site_code,POVS.vendor_site_id
      FROM   po_vendor_sites_all POVS
      WHERE POVS.vendor_id = p_vendor_id
      AND   POVS.org_id    = p_org_id;


  CURSOR lcu_supsite_attr15(p_doc_num VARCHAR2
                           ,p_lin_num VARCHAR2
                           ,p_shp_num VARCHAR2)
  IS
      SELECT PVS.attribute15
      FROM   po_headers_interface PHI
            ,po_lines_interface PLI
            ,po_vendor_sites_all PVS
      WHERE  PHI.interface_header_id = PLI.interface_header_id
      AND    PHI.vendor_id = PVS.vendor_id
      AND    PHI.vendor_id = PVS.vendor_id
      AND    PVS.org_id = PHI.org_id
      AND    PVS.vendor_site_code = PHI.vendor_site_code
      AND    PLI.document_num = p_doc_num
      AND    PLI.line_num = p_lin_num
      AND    PLI.shipment_num = p_shp_num;     
     
  lc_itm_chk_flag VARCHAR2(1);


  lr_po_header_data            lcu_po_header_data%ROWTYPE;
  lr_po_lines_data             lcu_po_lines_data%ROWTYPE;
  lr_po_dist_data              lcu_po_dist_data%ROWTYPE;

  lc_errmsg                    VARCHAR2(2000);
  lc_errloc                    VARCHAR2(1000);

  ln_request_id                NUMBER;
  lc_phase                     VARCHAR2(500);
  lc_status                    VARCHAR2(500);
  lc_devpha                    VARCHAR2(500);
  lc_devsta                    VARCHAR2(500);
  lc_mesg                      VARCHAR2(50);
  lb_wait                      BOOLEAN;

  ln_org_id                    NUMBER;
  ln_item_org_id               NUMBER;
  ln_agent_id                  NUMBER;
  lc_currency_code             VARCHAR2(15);
  ln_vendor_id                 NUMBER;
  lc_payment_terms             VARCHAR2(50);
  lc_freight_terms             VARCHAR2(25);
  ln_item_id                   NUMBER;
  lc_item_seg2                 VARCHAR2(40);
  lc_cat_seg1                  VARCHAR2(40);
  lc_cat_seg2                  VARCHAR2(40);
  lc_cat_seg3                  VARCHAR2(40);
  lc_cat_seg4                  VARCHAR2(40);
  lc_cat_seg5                  VARCHAR2(40);
  lc_inv_org_code              VARCHAR2(30);
  ln_cat_id                    NUMBER;
  ln_po_int_header_id          NUMBER;
  ln_po_int_line_id            NUMBER;
  ln_tax_code_id               NUMBER;
  ln_charge_acct_id            NUMBER;
  ln_budget_acct_id            NUMBER;
  ln_accrual_acct_id           NUMBER;
  ln_variance_acct_id          NUMBER;
  ln_sob_id                    NUMBER;
  ln_location_id               NUMBER;
  ln_person_org_id             NUMBER;
  ln_person_id                 NUMBER;
  ln_bu_id                     NUMBER;

  lc_chr_acc_seg1              VARCHAR2(40);
  lc_chr_acc_seg2              VARCHAR2(40);
  lc_chr_acc_seg3              VARCHAR2(40);
  lc_chr_acc_seg4              VARCHAR2(40);
  lc_chr_acc_seg5              VARCHAR2(40);
  lc_chr_acc_seg6              VARCHAR2(40);
  lc_chr_acc_seg7              VARCHAR2(40);

  lc_bud_acc_seg1              VARCHAR2(40);
  lc_bud_acc_seg2              VARCHAR2(40);
  lc_bud_acc_seg3              VARCHAR2(40);
  lc_bud_acc_seg4              VARCHAR2(40);
  lc_bud_acc_seg5              VARCHAR2(40);
  lc_bud_acc_seg6              VARCHAR2(40);
  lc_bud_acc_seg7              VARCHAR2(40);

  lc_accr_acc_seg1              VARCHAR2(40);
  lc_accr_acc_seg2              VARCHAR2(40);
  lc_accr_acc_seg3              VARCHAR2(40);
  lc_accr_acc_seg4              VARCHAR2(40);
  lc_accr_acc_seg5              VARCHAR2(40);
  lc_accr_acc_seg6              VARCHAR2(40);
  lc_accr_acc_seg7              VARCHAR2(40);

  lc_var_acc_seg1              VARCHAR2(40);
  lc_var_acc_seg2              VARCHAR2(40);
  lc_var_acc_seg3              VARCHAR2(40);
  lc_var_acc_seg4              VARCHAR2(40);
  lc_var_acc_seg5              VARCHAR2(40);
  lc_var_acc_seg6              VARCHAR2(40);
  lc_var_acc_seg7              VARCHAR2(40);
  lc_seg3_value                VARCHAR2(40);
  lc_seg5_value                VARCHAR2(40);

  lb_success                   BOOLEAN;

  lc_error_details             VARCHAR2(4000);
  lc_error                     VARCHAR2(4000);

  lc_errbuf                    VARCHAR2(240);
  ln_retcode                   NUMBER;

  ln_header_cnt                NUMBER;
  ln_lines_cnt                 NUMBER;
  ln_dist_cnt                  NUMBER;

  ln_cur_org_id                NUMBER;

  lc_new_orgname               VARCHAR2(240);
  lc_item_segment2             VARCHAR2(40);
  lc_new_glacct                VARCHAR2(240);
  lc_new_cat                   VARCHAR2(2000);
  lc_new_del                   VARCHAR2(240);
  lc_new_org_name              VARCHAR2(270);
  lc_new_sobname               VARCHAR2(270);
  lc_business_group            VARCHAR2(270);
  ln_bill_loc_id               NUMBER;
  ln_ship_loc_id               NUMBER;
  lc_inv_po_chk                VARCHAR2(270);
  lc_func_curr                 VARCHAR2(15);
  lc_rate_type                 VARCHAR2(30);
  ln_rate                      NUMBER;
  lc_vendor_site_id            NUMBER;
  ld_rate_date                 DATE;
  lc_new_ship                  VARCHAR2(240);
  lc_new_bill                  VARCHAR2(240);
  --lc_vendor_csr                VARCHAR2(240);
  lc_product_family            VARCHAR2(240);
  lc_line_exists               VARCHAR2(1);
  lc_vendor_site_code          VARCHAR2(150);
  lc_primary_uom               VARCHAR2(25);

  lc_line_ship_loc             VARCHAR2(60);
  lc_ship_org                  VARCHAR2(3);

  lc_item_attr17               VARCHAR2(50);
 
  lc_ship_via_lookup_code      VARCHAR2(25);

  ex_conc_error                EXCEPTION;

--
BEGIN

  ln_header_cnt := 0;

  --Process Purchase Orders data
  lc_errloc := 'Process purchase orders data';
  FOR lr_po_header_data IN lcu_po_header_data (p_rec_status) LOOP

    ln_org_id                   := NULL;
    ln_agent_id                 := NULL;
    ln_bu_id                    := NULL;
    lc_currency_code            := NULL;
    ln_vendor_id                := NULL;
    lc_payment_terms            := NULL;
    lc_freight_terms            := NULL;

    lc_error_details            := NULL;
    lc_new_orgname              := NULL;
    lc_business_group           := NULL;
    ln_bill_loc_id              := NULL;
    ln_ship_loc_id              := NULL;
    lc_inv_po_chk               := NULL;
    lc_func_curr                := NULL;
    lc_rate_type                := NULL;
    ln_rate                     := NULL;
    ld_rate_date                := NULL;
    lc_new_ship                 := NULL;
    lc_new_bill                 := NULL;
    --lc_vendor_csr               := NULL;

    lc_line_exists              := NULL;
    lc_vendor_site_code         := NULL;
    lc_vendor_site_id           := NULL;

    -- check if lines exist for this PO
    OPEN lcu_check_lines(lr_po_header_data.po_number);
    FETCH lcu_check_lines
    INTO lc_line_exists;
    CLOSE lcu_check_lines;

    IF lc_line_exists IS NULL THEN
      lc_error_details := lc_error_details||'No lines were found in the staging table for the given PO. ';
    END IF;

    lc_errloc      := 'Operating unit related derivations';
    lc_new_orgname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                          ,'XXCSR_CNV_OPERATING_UNIT'
                                                          ,'XXSIRF_CNV_OPERATING_UNIT'
                                                          ,lr_po_header_data.operating_unit
                                                          );

    IF lc_new_orgname IS NULL THEN
      lc_error_details := lc_error_details||'Could not derive new operating unit for '||lr_po_header_data.operating_unit||'. ';
    ELSE
      ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lc_new_orgname);
      IF ln_org_id IS NULL THEN
        lc_error_details := lc_error_details||'Could not derive org id for new operating unit '||lc_new_orgname||'. ';
      ELSE
        -- Compare functional and document currency
        OPEN lcu_func_curr(ln_org_id);
        FETCH lcu_func_curr
        INTO lc_func_curr;
        CLOSE lcu_func_curr;

        IF lc_func_curr = lr_po_header_data.currency_code THEN
          lc_rate_type := NULL;
          ln_rate      := NULL;
          ld_rate_date := NULL;
        ELSE
          lc_rate_type := lr_po_header_data.rate_type;
          ln_rate      := lr_po_header_data.rate;
          ld_rate_date := lr_po_header_data.rate_date;

        END IF;
      END IF;
    END IF;

    lc_errloc := 'Checking for inventory purchase orders';
    lc_inv_po_chk := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                          ,'XXCSR_CNV_INV_SUPPLIERS'
                                                          ,'XXSIRF_CNV_INV_SUPPLIERS'
                                                          ,lr_po_header_data.vendor_name
                                                          );

    IF lc_inv_po_chk IS NULL THEN
      lc_error_details := lc_error_details||'Only inventory purchase orders will be migrated';
    END IF;

    IF lr_po_header_data.emp_bu_name IN ( 'CSR United States','CSR United Kingdom') THEN
      lc_business_group := lr_po_header_data.emp_bu_name;
    ELSE
      lc_business_group := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                               ,'XXCSR_CNV_HR_BG'
                                                               ,'XXSIRF_CNV_HR_BG'
                                                               ,lr_po_header_data.emp_bu_name
                                                               );

    END IF;
    IF lc_business_group IS NULL THEN
      lc_error_details := lc_error_details||'Mapping for Business Group '||lr_po_header_data.emp_bu_name||' could not be found. ';
    ELSE
      ln_bu_id := XXCSR_COMMON_PKG.get_business_group_id(lc_business_group);

      IF ln_bu_id IS NULL THEN
        lc_error_details:= lc_error_details||' Business Group ID for '||lc_business_group||' could not be derived. ';
      ELSE
        --Derive agent id
        lc_errloc := 'Derive agent id';
        ln_agent_id := XXCSR_COMMON_PKG.get_person_id (p_first_name        => lr_po_header_data.emp_fname
                                                      ,p_last_name         => lr_po_header_data.emp_lname
                                                      ,p_date_of_birth     => lr_po_header_data.emp_dob
                                                      ,p_business_group_id => ln_bu_id
                                                      );
        IF ln_agent_id IS NULL THEN
            lc_error_details := lc_error_details || ' Invalid agent (employee) details.';
        END IF;

      END IF;
    END IF;



    --Check currency code
    lc_errloc := 'Check currency code';
    OPEN  lcu_chk_currency (lr_po_header_data.currency_code);
    FETCH lcu_chk_currency
    INTO  lc_currency_code;

    IF lcu_chk_currency%NOTFOUND THEN
        lc_error_details := lc_error_details || ' Invalid currency code.';
    END IF;

    CLOSE lcu_chk_currency;

    --Derive vendor id
    lc_errloc := 'Derive vendor id';
    IF lr_po_header_data.vendor_name_actual <> 'TSMC Ltd.' THEN
/*
      lc_vendor_csr := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                              ,p_csr_lookup    => 'NULL'
                                                              ,p_sirf_lookup   => 'XXSIRF_CNV_SUPPLIER_NUMBERS'
                                                              ,p_old_value     => lr_po_header_data.vendor_name
                                                              );
*/

      IF lr_po_header_data.vendor_name IS NOT NULL THEN
        OPEN lcu_vendor_der(lr_po_header_data.vendor_name);
        FETCH lcu_vendor_der
        INTO  ln_vendor_id;
        CLOSE lcu_vendor_der;

        IF ln_vendor_id IS NULL THEN
            lc_error_details := lc_error_details || ' Vendor not migrated.';
        ELSE
          OPEN lcu_vendor_site(ln_vendor_id,ln_org_id);
          FETCH lcu_vendor_site INTO lc_vendor_site_code,lc_vendor_site_id;
          CLOSE lcu_vendor_site;

          IF lc_vendor_site_code IS NULL THEN
            lc_error_details := lc_error_details || ' Vendor site code not found for vendor '||lr_po_header_data.vendor_name ||'. ';
          END IF;
        END IF;
      END IF;
    ELSE

      OPEN lcu_vendor_tsmc('TAIWAN SEMICONDUCTOR MANUFACTURING CO LTD');
      FETCH lcu_vendor_tsmc
      INTO ln_vendor_id;
      CLOSE lcu_vendor_tsmc;

      IF ln_vendor_id IS NULL THEN
          lc_error_details := lc_error_details || ' Vendor not migrated.';
      END IF;

      lc_vendor_site_code := 'ROC';


    END IF;



    lc_errloc := 'Check payment terms';
    IF lr_po_header_data.payment_terms IS NOT NULL THEN
    lc_payment_terms := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                            ,p_csr_lookup    => 'XXCSR_CNV_SUPPLIER_PAYTERMS'
                                                            ,p_sirf_lookup   => 'XXSIRF_CNV_SUPPLIER_PAYTERMS'
                                                            ,p_old_value     => lr_po_header_data.payment_terms
                                                            );

    IF lc_payment_terms IS NULL THEN
        lc_error_details := lc_error_details || ' Invalid payment terms.';
    END IF;
    END IF;

    --Check freight terms
    lc_errloc := 'Check freight terms';
    IF lr_po_header_data.freight_terms IS NOT NULL THEN
      lc_freight_terms := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                              ,p_csr_lookup    => 'XXCSR_CNV_FREIGHT_TERMS'
                                                              ,p_sirf_lookup   => 'XXSIRF_CNV_FREIGHT_TERMS'
                                                              ,p_old_value     => lr_po_header_data.freight_terms
                                                              );

      IF lc_freight_tebms IS NULL THEN
          lc_error_details := lc_error_details || ' Invalid freight terms.';
      END IF;

    END IF;

   { IF lr_po_header_data.ship_to_location IS NOT NULL THEN

      lc_new_ship := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                        &nbcp;      &nbst;                            ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                            ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                            ,lr_po_header_data.ship_to_location
                                                            );
      IF lc_new_ship IS NULL THEN

        lc_error_details := lc_error_details||'Could not derive new ship to location for '||lr_po_header_data.ship_to_location||' in XXSIRF_CNV_DELIVER_LOCATIONS. ';

      ELSE

        OPEN lcu_hr_loc(lc_new_ship);
        FETCH lcu_hr_loc
        INTO ln_ship_loc_id;
        CLOSE lcu_hr_loc;

        IF ln_ship_loc_id IS NULL THEN
          lc_error_details := lc_error_details || 'Ship to location id could not be derived for '||lr_po_header_data.ship_to_location;
        END IF;
      END IF;

    END IF;

    IF lr_po_header_data.bill_to_location IS NOT NULL THEN

      lc_new_bill := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                            ,'XXCSR_CNV_BILLTO_LOCATIONS'
                                                            ,'XXSIRF_CNV_BILLTO_LOCATIONS'
                                                            ,lr_po_header_data.bill_to_location
                                                            );
      IF lc_new_bill IS NULL THEN

        lc_error_details := lc_error_details||'Could not derive new bill to location for '||lr_po_header_data.bill_to_location||' in XXSIRF_CNV_BILLTO_LOCATIONS. ';

      ELSE

        OPEN lcu_hr_loc(lc_new_bill);
        FETCH lcu_hr_loc
        INTO ln_bill_loc_id;
        CLOSE lcu_hr_loc;

        IF ln_bill_loc_id IS NULL THEN
          lc_error_details := lc_error_details || 'Bill to location id could not be derived for '||lr_po_header_data.bill_to_location;
        END IF;

      END IF;

    END IF;
   
    lc_ship_via_lookup_code := NULL;
    IF lr_po_header_data.ship_via_lookup_code IS NOT NULL THEN
   
      lc_ship_via_lookup_code := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                    ,'XXCSR_CNV_FREIGHT_CARRIER'
                                                                    ,'XXCSR_CNV_FREIGHT_CARRIER'
                                                                    ,lr_po_header_data.ship_via_lookup_code
                                                                    );
   
    END IF;
   
   

    IF lc_error_details IS NULL THEN

        --Insert purchase order header data in interface table
        lc_errloc := 'Insert purchase order header data in interface table';

        INSERT INTO po_headers_interface
        (interface_header_id
        ,batch_id
        ,interface_source_code
        ,action
        ,process_code
        ,org_id
        ,document_type_code
        ,currency_code
        ,rate_type
        ,rate_date
        ,rate
        ,agent_id
        ,vendor_id
        ,vendor_site_code
        ,vendor_site_id
        ,ship_to_location_id
        ,bill_to_location_id
        ,payment_terms
        ,freight_terms
        ,approval_status
        ,approved_date
      --  ,revised_date
      --  ,revision_num
        ,confirming_order_flag
        ,comments
        ,acceptance_required_flag
        ,print_count
      --  ,printed_date
        ,frozen_flag
        ,closed_code
        ,from_rfq_num
        ,from_type_lookup_code
        ,attribute_category
        --,attribute1
       -- ,attribute2
       -- ,attribute3
        --,global_attribute1
        ,pay_on_code
        ,last_update_date
        ,last_updated_by
        ,creation_date
        ,created_by
        ,document_num
        ,freight_carrier
        )
        VALUES
        (po_headers_interface_s.nextval
        ,1
        ,'XXSIRF Conversion'
        ,'ORIGINAL'
        ,'PENDING'
        ,ln_org_id
        ,'STANDARD' -- lr_po_header_data.document_type_code
        ,lr_po_header_data.currency_code
        ,DECODE(lc_rate_type,'Corporate','User',lc_rate_type)
        ,ld_rate_date
        ,ln_rate
        ,ln_agent_id
        ,ln_vendor_id
        ,lc_vendor_site_code -- lr_po_header_data.vendor_site_code
        ,lc_vendor_site_id
        ,ln_ship_loc_id -- lr_po_header_data.ship_to_location
        ,ln_bill_loc_id -- lr_po_header_data.bill_to_location
        ,lc_payment_terms
        ,lc_freight_terms
        ,DECODE(lr_po_header_data.approval_status,'REQUIRES REAPPROVAL','INCOMPLETE',lr_po_header_data.approval_status)
        ,lr_po_header_data.approved_date
       -- ,lr_po_header_data.revised_date
       -- ,lr_po_header_data.revision_num
        ,lr_po_header_data.confirming_order_flag
        ,lr_po_header_data.comments
        ,lr_po_header_data.acceptance_required_flag
        --,DECODE(lr_po_header_data.approval_status,'APPROVED',lr_po_header_data.print_count,0)
        ,DECODE(lr_po_header_data.approval_status,'APPROVED',1,0)
      --  ,lr_po_header_data.printed_date
        ,lr_po_header_data.frozen_flag
        ,lr_po_header_data.closed_code
        ,lr_po_header_data.from_requisition_num
        ,lr_po_header_data.from_type_lookup_code
        ,lr_po_header_data.attribute_category
        --,lr_po_header_data.sourcing_rule
       -- ,lr_po_header_data.osp_flag
        --,lr_po_header_data.mrp_flag
        --,lr_po_header_data.po_total_plus_ipi_tax
        ,lr_po_header_data.pay_on_code
        ,SYSDATE
        ,fnd_global.user_id
        ,SYSDATE
        ,fnd_global.user_id
        ,lr_po_header_data.po_number
        ,lc_ship_via_lookup_code
        );

        UPDATE xxcsr_po_headers_int
        SET    record_status = 'C'
              ,request_id    = FND_GLOBAL.conc_request_id
              ,error_message = NULL
        WHERE rowid = lr_po_header_data.rowid;

        COMMIT;

        ln_header_cnt := ln_header_cnt + 1;

    ELSE
        UPDATE xxcsr_po_headers_int
        SET    record_status = 'E'
              ,request_id    = FND_GLOBAL.conc_request_id
              ,error_message = lc_error_details
        WHERE  rowid = lr_po_header_data.rowid;
    END IF; --lc_error_details IS NULL

    COMMIT;
  END LOOP;
  COMMIT;


  ln_lines_cnt := 0;

  --Process Purchase Order Lines data
  lc_errloc := 'Process purchase order lines data';
  FOR lr_po_lines_data IN lcu_po_lines_data (p_rec_status)
  LOOP

      ln_org_id                   := NULL;
      ln_item_org_id              := NULL;
      ln_item_id                  := NULL;
      lc_item_seg2                := NULL;
      lc_cat_seg1                 := NULL;
      lc_cat_seg2                 := NULL;
      lc_cat_seg3                 := NULL;
      lc_cat_seg4                 := NULL;
      lc_cat_seg5                 := NULL;
      ln_cat_id                   := NULL;
      ln_po_int_header_id         := NULL;
      ln_tax_code_id              := NULL;

      lc_error_details            := NULL;

      lc_new_orgname              := NULL;
      lc_item_segment2            := NULL;
      lc_new_glacct               := NULL;
      lc_new_cat                  := NULL;
      lc_new_ship                 := NULL;
      ln_ship_loc_id              := NULL;
      lc_new_org_name             := NULL;
      lc_primary_uom              := NULL;

      --Derive interface header id
      lc_errloc := 'Derive interface header id';
      OPEN  lcu_get_int_header_id (lr_po_lines_data.po_number);
      FETCH lcu_get_int_header_id
      INTO  ln_po_int_header_id;
      CLOSE lcu_get_int_header_id;

      IF ln_po_int_header_id IS NULL THEN
        lc_error_details := lc_error_details || 'Could not derive interface header id. Header record may have error. ';
      END IF;

      FND_FILE.PUT_LINE(FND_FILE.LOG,'ln_po_int_header_id : ' || ln_po_int_header_id);

      lc_errloc   := 'Operating unit related derivations';
      lc_new_orgname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                            ,'XXCSR_CNV_OPERATING_UNIT'
                                                            ,'XXSIRF_CNV_OPERATING_UNIT'
                                                            ,lr_po_lines_data.org_name
                                                            );

      IF lc_new_orgname IS NULL THEN
        lc_error_details := lc_error_details||'Could not derive new operating unit for '||lr_po_lines_data.org_name||'. ';
      ELSE
        ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lc_new_orgname);
        IF ln_org_id IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive org id for new operating unit '||lc_new_orgname||'. ';
        END IF;
      END IF;

      IF lr_po_lines_data.ship_to_location IS NOT NULL THEN

        lc_new_ship := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                              ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                              ,lr_po_lines_data.ship_to_location
                                                              );
        IF lc_new_ship IS NULL THEN

          lc_error_details := lc_error_details||'Could not derive new ship to location for '||lr_po_lines_data.ship_to_location||' in XXSIRF_CNV_DELIVER_LOCATIONS. ';

        ELSE

          OPEN lcu_hr_loc(lc_new_ship);
          FETCH lcu_hr_loc
          INTO ln_ship_loc_id;
          CLOSE lcu_hr_loc;

          IF ln_ship_loc_id IS NULL THEN
            lc_error_details := lc_error_details || 'Ship to location id could not be derived for '||lr_po_lines_data.ship_to_location;
          END IF;
        END IF;

      END IF;

      IF lr_po_lines_data.ship_to_org IS NOT NULL THEN

        lc_new_org_name := XXCSR_COMMON_PKG.get_organization_code (p_instance_name
                                                                  ,lr_po_lines_data.ship_to_org);

        IF lc_new_org_name IS NULL THEN
          lc_error_details := lc_error_details||'Mapping for organization '||lr_po_lines_data.ship_to_org||' not found in the table XXCSR_CNV_ORG_MAPPINGS. ';
        END IF;

      END IF;


    lc_product_family := NULL;

  --  IF lr_po_lines_data.itm_seg1_item_num IS NOT NULL THEN

      lc_errloc := 'Derive inventory item id';

      IF lr_po_lines_data.attr18 IS NULL THEN
          OPEN  lcu_get_product_family (lr_po_lines_data.itm_seg1_item_num);
          FETCH lcu_get_product_family
          INTO  lc_product_family;
          CLOSE lcu_get_product_family;
      ELSE
          lc_product_family := lr_po_lines_data.attr18;
      END IF;

/*
       OPEN lcu_get_segment2 (lr_po_lines_data.item_type
                             ,lr_po_lines_data.item_org_name
                             ,lc_product_family
                             );

       FETCH lcu_get_segment2
       INTO lc_item_segment2;
       CLOSE lcu_get_segment2;
*/
      IF lr_po_lines_data.item_type = 'DIE' THEN
        lc_item_segment2 := 'PD';
      ELSIF lr_po_lines_data.item_type = 'HYBRID' THEN
        lc_item_segment2 := 'HY';
      ELSIF lr_po_lines_data.item_type = 'SORTED-DIE' THEN
        lc_item_segment2 := 'DI';
      ELSIF lr_po_lines_data.item_type = 'WAFER' THEN
        lc_item_segment2 := 'UW';
      ELSIF lr_po_lines_data.item_type = 'SORTED-WAFER' THEN
        lc_item_segment2 := 'WF';
      ELSE
        lc_item_segment2 := NULL;
      END IF;

      ln_item_id := NULL;
            OPEN lcu_item_id(lr_po_lines_data.itm_seg1_item_num
                      ,lc_item_segment2
                      ,lc_new_org_name);
                      --,lr_po_lines_data.item_org_name);
      FETCH lcu_item_id INTO ln_item_id;
      CLOSE lcu_item_id;

      IF ln_item_id IS NULL THEN
        lc_error_details := lc_error_details || ' Inventory Item Not found - '|| NVL(lc_item_attr17,lr_po_lines_data.itm_seg1_item_num)||'.';
      ELSE
        -- DERIVE primary UOM for master item
        IF lc_new_org_name IS NOT NULL THEN
          OPEN lcu_prim_uom(lc_new_org_name
                           ,ln_item_id);
          FETCH lcu_prim_uom
          INTO lc_primary_uom;
          CLOSE lcu_prim_uom;

          IF lc_primary_uom IS NULL THEN
            lc_error_details := lc_error_details || 'Primary UOM code for the master item could not be derived';
          END IF;

        END IF;

      END IF;

  --  END IF;

      lc_errloc := 'Derive new GL account';
      lc_new_cat := 'INVENTORY.';

      IF lr_po_lines_data.inventory_item_flag = 'N' THEN

        UPDATE xxcsr_po_distributions_int
        SET destination_type_code = 'EXPENSE'
        WHERE po_number = lr_po_lines_data.po_number
        AND po_line_number = lr_po_lines_data.line_num
        AND shipment_num = lr_po_lines_data.shipment_num
        AND source_instance = p_instance_name;

      ELSE

        UPDATE xxcsr_po_distributions_int
        SET destination_type_code = 'INVENTORY'
        WHERE po_number = lr_po_lines_data.po_number
        AND po_line_number = lr_po_lines_data.line_num
        AND shipment_num = lr_po_lines_data.shipment_num
        AND source_instance = p_instance_name;

      END IF;

      IF lc_error_details IS NULL THEN

          --Insert purchase order lines data in interface table
          lc_errloc := 'Insert purchase order lines data in interface table';
          FND_FILE.PUT_LINE(FND_FILE.LOG,'Inserting in lines');

          INSERT INTO po_lines_interface
          (interface_line_id
          ,interface_header_id
          ,action
          ,process_code
          ,document_num
          ,line_num
          ,organization_id
          ,shipment_num
          ,shipment_type
          ,contract_num
          ,line_type
          ,item_id
          ,category
          ,item_description
          ,vendor_product_num
          ,unit_of_measure
          ,quantity
          ,unit_price
          ,list_price_per_unit
          ,allow_price_override_flag
          ,negotiated_by_preparer_flag
          ,note_to_vendor
          ,taxable_flag
         -- ,tax_name
          ,type_1099
          ,capital_expense_flag
          ,price_type
          ,closed_code
          ,closed_reason
          ,closed_date
          ,closed_by
          ,over_tolerance_error_flag
          ,line_attribute_category_lines
        --  ,line_attribute1
        --  ,line_attribute2
        --  ,line_attribute3
        --  ,line_attribute4
        --  ,line_attribute5
        --  ,line_attribute6
        --  ,line_attribute7
        --  ,line_attribute8
        --  ,line_attribute9
          --,global_attribute1
         -- ,tax_code_id
          ,secondary_unit_of_measure
          ,secondary_quantity
          ,inspection_required_flag
          ,receipt_required_flag
          ,invoice_close_tolerance
          ,receive_close_tolerance
          ,need_by_date
          ,promised_date
          ,consigned_flag
          ,base_unit_price
          ,last_update_date
          ,last_updated_by
          ,creation_date
          ,created_by
          ,ship_to_location_id
          ,ship_to_organization_code
          )
          VALUES
          (po_lines_interface_s.nextval
          ,ln_po_int_header_id
          ,'ORIGINAL'
          ,'PENDING'
          ,lr_po_lines_data.po_number
          ,lr_po_lines_data.line_num
          ,ln_org_id
          ,lr_po_lines_data.shipment_num
          ,lr_po_lines_data.shipment_type
          ,lr_po_lines_data.contract_num
          ,lr_po_lines_data.line_type
          ,ln_item_id
          ,lc_new_cat
          ,lr_po_lines_data.item_description
          ,lr_po_lines_data.vendor_product_num
          ,lc_primary_uom -- lr_po_lines_data.unit_of_measure
          ,lr_po_lines_data.quantity
          ,lr_po_lines_data.unit_price
          ,lr_po_lines_data.list_price_per_unit
          ,lr_po_lines_data.allow_price_override_flag
          ,lr_po_lines_data.negotiated_by_preparer_flag
          ,lr_po_lines_data.note_to_vendor
          ,lr_po_lines_data.taxable_flag
        --  ,lr_po_lines_data.tax_name
          ,lr_po_lines_data.type_1099
          ,lr_po_lines_data.capital_expense_flag
          ,lr_po_lines_data.price_type
          ,lr_po_lines_data.closed_code
          ,lr_po_lines_data.closed_reason
          ,lr_po_lines_data.closed_date
          ,lr_po_lines_data.closed_by
          ,lr_po_lines_data.over_tolerance_error_flag
          ,lr_po_lines_data.line_attribute_category_lines
         -- ,lr_po_lines_data.att1_item_number
         -- ,lr_po_lines_data.att2_manufacturer
         -- ,lr_po_lines_data.att3_manufacturer_part_no
         -- ,lr_po_lines_data.att4_vendor_name
         -- ,lr_po_lines_data.att5_last_po_price
         -- ,lr_po_lines_data.att6_last_transaction_qty
         -- ,lr_po_lines_data.att7_last_vendor_name
         -- ,lr_po_lines_data.att8_item_id
         -- ,lr_po_lines_data.att9_vendor_name
          --,lr_po_lines_data.fisc_classification_code
         -- ,ln_tax_code_id         commented as per R04
          ,lr_po_lines_data.secondary_unit_of_measure
          ,lr_po_lines_data.secondary_quantity
          ,lr_po_lines_data.inspection_required_flag
          ,lr_po_lines_data.receipt_required_flag
          ,lr_po_lines_data.invoice_close_tolerance
          ,lr_po_lines_data.receive_close_tolerance
          ,lr_po_lines_data.need_by_date
          ,lr_po_lines_data.promised_date
          ,lr_po_lines_data.consigned_flag
          ,lr_po_lines_data.base_unit_price
          ,SYSDATE
          ,fnd_global.user_id
          ,SYSDATE
          ,fnd_global.user_id
          ,ln_ship_loc_id
          ,lc_new_org_name
          );

          UPDATE xxcsr_po_lines_int
          SET    record_status = 'C'
                ,request_id    = FND_GLOBAL.conc_request_id
                ,error_message = NULL
          WHERE rowid          = lr_po_lines_data.rowid;

          COMMIT;

          ln_lines_cnt := ln_lines_cnt + 1;

      ELSE
          UPDATE xxcsr_po_lines_int
          SET    record_status = 'E'
                ,request_id    = FND_GLOBAL.conc_request_id
                ,error_message = lc_error_details
          WHERE  rowid         = lr_po_lines_data.rowid;

 IF ln_po_int_header_id IS NOT NULL THEN
   UPDATE xxcsr_po_headers_int
   SET record_status = 'E'
      ,error_message = error_message||'error in lines record'
   WHERE  po_number = lr_po_lines_data.po_number
   AND source_instance = p_instance_name;

   DELETE FROM po_headers_interface
   WHERE interface_header_id = ln_po_int_header_id;

   ln_header_cnt := ln_header_cnt - 1;

        END IF;
      END IF; --lc_error_details IS NULL

      COMMIT;
  END LOOP;
  COMMIT;


  ln_dist_cnt := 0;

  --Process Purchase Order distributions data
  lc_errloc := 'Process purchase order distributions data';
  FOR lr_po_dist_data IN lcu_po_dist_data (p_rec_status)
  LOOP

      ln_org_id                   := NULL;
      ln_sob_id                   := NULL;
      ln_location_id              := NULL;
      lc_inv_org_code             := NULL;
      ln_person_org_id            := NULL;
      ln_person_id                := NULL;

      lc_error_details            := NULL;
      lc_new_orgname              := NULL;
      lc_new_del                  := NULL;
      lc_new_org_name             := NULL;
      lc_new_sobname              := NULL;
      ln_po_int_line_id           := NULL;
      ln_po_int_header_id         := NULL;
      lc_business_group           := NULL;
      ln_bu_id                    := NULL;
      lc_chr_acc_seg1             := NULL;
      lc_chr_acc_seg2             := NULL;
      lc_chr_acc_seg3             := NULL;
      lc_chr_acc_seg4             := NULL;
      lc_chr_acc_seg5             := NULL;
      lc_chr_acc_seg6             := NULL;
      lc_chr_acc_seg7             := NULL;
      lc_bud_acc_seg1             := NULL;
      lc_bud_acc_seg2             := NULL;
      lc_bud_acc_seg3             := NULL;
      lc_bud_acc_seg4             := NULL;
      lc_bud_acc_seg5             := NULL;
      lc_bud_acc_seg6             := NULL;
      lc_bud_acc_seg7             := NULL;
      lc_accr_acc_seg1            := NULL;
      lc_accr_acc_seg2            := NULL;
      lc_accr_acc_seg3            := NULL;
      lc_accr_acc_seg4            := NULL;
      lc_accr_acc_seg5            := NULL;
      lc_accr_acc_seg6            := NULL;
      lc_accr_acc_seg7            := NULL;
      lc_var_acc_seg1             := NULL;
      lc_var_acc_seg2             := NULL;
      lc_var_acc_seg3             := NULL;
      lc_var_acc_seg4             := NULL;
      lc_var_acc_seg5             := NULL;
      lc_var_acc_seg6             := NULL;
      lc_var_acc_seg7             := NULL;
      ln_charge_acct_id           := NULL;
      ln_budget_acct_id           := NULL;
      ln_accrual_acct_id          := NULL;
      ln_variance_acct_id         := NULL;
      lc_seg3_value               := NULL;
      lc_seg5_value               := NULL;

      --Derive interface header id
      lc_errloc := 'Derive interface header id';
      OPEN  lcu_get_int_header_id (lr_po_dist_data.po_number);
      FETCH lcu_get_int_header_id
      INTO  ln_po_int_header_id;
      CLOSE lcu_get_int_header_id;

      IF ln_po_int_header_id IS NULL THEN
        lc_error_details := lc_error_details || 'Could not derive interface header id. Header record may have error. ';
      END IF;

      --Derive interface line id
      lc_errloc := 'Derive interface line id';
      OPEN  lcu_get_int_line_id (lr_po_dist_data.po_number
                                ,lr_po_dist_data.po_line_number
                                ,lr_po_dist_data.shipment_num);
      FETCH lcu_get_int_line_id
      INTO  ln_po_int_line_id;
      CLOSE lcu_get_int_line_id;

      IF ln_po_int_line_id IS NULL THEN
        lc_error_details := lc_error_details || 'Could not derive interface line id. Header/Line record may have error. ';
      END IF;

      lc_errloc   := 'Operating unit related derivations';
      lc_new_orgname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                            ,'XXCSR_CNV_OPERATING_UNIT'
                                                            ,'XXSIRF_CNV_OPERATING_UNIT'
                                                            ,lr_po_dist_data.org_name
                                                            );

      IF lc_new_orgname IS NULL THEN
        lc_error_details := lc_error_details||'Could not derive new operating unit for '||lr_po_dist_data.org_name||'. ';
      ELSE
        ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lc_new_orgname);
        IF ln_org_id IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive org id for new operating unit '||lc_new_orgname||'. ';
        END IF;
      END IF;

      --Derive sob id

      lc_new_sobname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                            ,'XXCSR_CNV_SOB_NAME'
                                                            ,'XXSIRF_CNV_SOB_NAME'
                                                            ,lr_po_dist_data.set_of_books
                                                            );

      IF lc_new_sobname IS NULL THEN
        lc_error_details := lc_error_details||'Could not derive new sob for '||lr_po_dist_data.set_of_books||' in XXSIRF_CNV_SOB_NAME. ';
      ELSE

        lc_errloc := 'Derive sob id';
        OPEN  lcu_get_sob_id (lc_new_sobname);
        FETCH lcu_get_sob_id
        INTO  ln_sob_id;
        CLOSE lcu_get_sob_id;

        IF ln_sob_id IS NULL THEN
            lc_error_details := lc_error_details || ' Invalid sets of books name- '||lc_new_sobname;
        END IF;

      END IF;

      IF lr_po_dist_data.deliver_to_location IS NOT NULL THEN

        lc_new_del := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                              ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                              ,lr_po_dist_data.deliver_to_location
                                                              );

        IF lc_new_del IS NULL THEN

          lc_error_details := lc_error_details||'Could not derive new deliver to location for '||lr_po_dist_data.deliver_to_location||' in XXSIRF_CNV_DELIVER_LOCATIONS. ';

        ELSE
          --Derive deliver to location id
          lc_errloc := 'Derive deliver to location id';
          OPEN  lcu_location_id (lc_new_del);
          FETCH lcu_location_id
          INTO  ln_location_id;
          CLOSE lcu_location_id;

          IF ln_location_id IS NULL THEN
              lc_error_details := lc_error_details || ' Invalid deliver to location code.';
          END IF;

        END IF;

      END IF;

      IF lr_po_dist_data.destination_organization IS NOT NULL THEN

        lc_new_org_name := XXCSR_COMMON_PKG.get_organization_code (p_instance_name
                                                                  ,lr_po_dist_data.destination_organization);

        IF lc_new_org_name IS NULL THEN
          lc_error_details := lc_error_details||'Mapping for organization '||lr_po_dist_data.destination_organization||' not found in the table XXCSR_CNV_ORG_MAPPINGS. ';
        END IF;

      END IF;


      --Derive deliver to person id
      IF  lr_po_dist_data.delv_per_fname IS NOT NULL THEN
        lc_business_group := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                 ,'XXCSR_CNV_HR_BG'
                                                                 ,'XXSIRF_CNV_HR_BG'
                                                                 ,lr_po_dist_data.delv_per_org_name
                                                                 );

        IF lc_business_group IS NULL THEN
          lc_error_details := lc_error_details||'Mapping for Business Group '||lr_po_dist_data.delv_per_org_name||' could not be found. ';
        ELSE
          ln_bu_id := XXCSR_COMMON_PKG.get_business_group_id(lc_business_group);
          IF ln_bu_id IS NULL THEN
            lc_error_details:= lc_error_details||' Business Group ID for '||lc_business_group||' could not be derived. ';
            --Derive deliver to person id
            lc_errloc := 'Derive deliver to person id';
            ln_person_id := XXCSR_COMMON_PKG.get_person_id (p_first_name        => lr_po_dist_data.delv_per_fname
                                                          ,p_last_name         => lr_po_dist_data.delv_per_lname
                                                          ,p_date_of_birth     => lr_po_dist_data.delv_per_dob
                                                          ,p_business_group_id => ln_bu_id
                                                          );
            IF ln_person_id IS NULL THEN
                lc_error_details := lc_error_details || ' Invalid agent (employee) details.';
            END IF;

          END IF;
        END IF;

      END IF;

      --BUDGET ACCOUNT
      --Derive budget account segment1 from lookup
      lc_errloc := 'Derive budget account segment1 from lookup';
      lc_bud_acc_seg1 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_COMPANY'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_COMPANY'
                                                             ,p_old_value     => lr_po_dist_data.bud_acc_seg1
                                                             );

      --Derive budget account segment2 from lookup
      lc_errloc := 'Derive budget account segment2 from lookup';
      lc_bud_acc_seg2 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_ACCOUNT'
                                                             ,p_old_value     => lr_po_dist_data.bud_acc_seg2
                                                             );

      --Derive budget account segment3 from lookup
      lc_errloc := 'Derive budget account segment3 from lookup';
      lc_bud_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                             ,p_old_value     => lr_po_dist_data.bud_acc_seg3
                                                             );

      --Derive budget account segment4 from lookup
      lc_errloc := 'Derive budget account segment4 from lookup';
      lc_bud_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                             ,p_old_value     => lr_po_dist_data.bud_acc_seg4
                                                             );

      --Derive budget account segment5 from lookup
      lc_errloc := 'Derive budget account segment5 from lookup';
      lc_bud_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                             ,p_old_value     => lr_po_dist_data.bud_acc_seg5
                                                             );

      --Derive budget account segment6 from lookup
      lc_errloc := 'Derive budget account segment6 from lookup';
      lc_bud_acc_seg6 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_INTERCO'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_INTERCO'
                                                             ,p_old_value     => lr_po_dist_data.bud_acc_seg6
                                                             );

      --Derive budget account segment7 from lookup
      lc_errloc := 'Derive budget account segment7 from lookup';
   /*   lc_bud_acc_seg7 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_BUD_ACC7_LKUP'
                                                             ,p_sirf_lookup   => 'XXSIRF_BUD_ACC7_LKUP'
                                                             ,p_old_value     => lr_po_dist_data.bud_acc_seg7
                                                             ); */

      lc_bud_acc_seg7 := '00000';


      --ACCRUAL ACCOUNT
      --Derive accrual account segment1 from lookup
      lc_errloc := 'Derive accrual account segment1 from lookup';
      lc_accr_acc_seg1 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_COMPANY'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_COMPANY'
                                                             ,p_old_value     => lr_po_dist_data.accr_acc_seg1
                                                             );

/*
      --Derive accrual account segment2 from lookup
      lc_errloc := 'Derive accrual account segment2 from lookup';

      IF lr_po_dist_data.vendor_site_attr3 IS NOT NULL THEN
        lc_accr_acc_seg2 := lr_po_dist_data.vendor_site_attr3;
      ELSE
        lc_accr_acc_seg2 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_ACCOUNT'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg2
                                                               );
      END IF;
*/
      --Derive accrual account segment2 from lookup
      lc_errloc := 'Derive accrual account segment2 from lookup';

      lc_accr_acc_seg2 := NULL;
      OPEN  lcu_supsite_attr15(lr_po_dist_data.po_number
                              ,lr_po_dist_data.po_line_number
                              ,lr_po_dist_data.shipment_num);
      FETCH lcu_supsite_attr15 INTO lc_accr_acc_seg2;
      CLOSE lcu_supsite_attr15;

      IF lc_accr_acc_seg2 IS NULL THEN

        lc_accr_acc_seg2 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_ACCOUNT'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg2
                                                               );
      END IF;


      --Derive accrual account segment3 from lookup
      lc_errloc := 'Derive accrual account segment3 from lookup';
      lc_accr_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                             ,p_old_value     => lr_po_dist_data.accr_acc_seg6
                                                             );

      --Derive accrual account segment4 from lookup
      lc_errloc := 'Derive accrual account segment4 from lookup';
      lc_accr_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                             ,p_old_value     => lr_po_dist_data.accr_acc_seg3
                                                             );

      --Derive accrual account segment5 from lookup
      lc_errloc := 'Derive accrual account segment5 from lookup';
      lc_accr_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                             ,p_old_value     => lr_po_dist_data.accr_acc_seg4
                                                             );

      --Derive accrual account segment6 from lookup
      lc_errloc := 'Derive accrual account segment6 from lookup';
      lc_accr_acc_seg6 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_INTERCO'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_INTERCO'
                                                             ,p_old_value     => lr_po_dist_data.accr_acc_seg5
                                                             );

      --Derive accrual account segment7 from lookup
      lc_errloc := 'Derive accrual account segment7 from lookup';
     /* lc_accr_acc_seg7 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_ACCR_ACC7_LKUP'
                                                             ,p_sirf_lookup   => 'XXSIRF_ACCR_ACC7_LKUP'
                                                             ,p_old_value     => lr_po_dist_data.accr_acc_seg7
                                                             ); */
      lc_accr_acc_seg7 := '00000';

      --VARIANCE ACCOUNT
      --Derive variance account segment1 from lookup
      lc_errloc := 'Derive variance account segment1 from lookup';
      lc_var_acc_seg1 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_COMPANY'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_COMPANY'
                                                             ,p_old_value     => lr_po_dist_data.var_acc_seg1
                                                             );

      --Derive variance account segment2 from lookup
      lc_errloc := 'Derive variance account segment2 from lookup';
      lc_var_acc_seg2 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_ACCOUNT'
                                                             ,p_old_value     => lr_po_dist_data.var_acc_seg2
                                                             );

      --Derive variance account segment3 from lookup
      lc_errloc := 'Derive variance account segment3 from lookup';
      lc_var_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                             ,p_old_value     => lr_po_dist_data.var_acc_seg3
                                                             );

      --Derive variance account segment4 from lookup
      lc_errloc := 'Derive variance account segment4 from lookup';
      lc_var_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                             ,p_old_value     => lr_po_dist_data.var_acc_seg4
                                                             );

      --Derive variance account segment5 from lookup
      lc_errloc := 'Derive variance account segment5 from lookup';
      lc_var_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                             ,p_old_value     => lr_po_dist_data.var_acc_seg5
                                                             );

      --Derive variance account segment6 from lookup
      lc_errloc := 'Derive variance account segment6 from lookup';
      lc_var_acc_seg6 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_CNV_GL_INTERCO'
                                                             ,p_sirf_lookup   => 'XXSIRF_CNV_GL_INTERCO'
                                                             ,p_old_value     => lr_po_dist_data.var_acc_seg6
                                                             );

      --Derive variance account segment7 from lookup
      lc_errloc := 'Derive variance account segment7 from lookup';
    /*  lc_var_acc_seg7 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                             ,p_csr_lookup    => 'XXCSR_VAR_ACC7_LKUP'
                                                             ,p_sirf_lookup   => 'XXSIRF_VAR_ACC7_LKUP'
                                                             ,p_old_value     => lr_po_dist_data.var_acc_seg7
                                                             ); */


      lc_var_acc_seg7 := '00000';

/*       sjacob commented
      --Derive charge account id
      lc_errloc := 'Derive charge account id';
      OPEN  lcu_account_id (lc_chr_acc_seg1
                           ,lc_chr_acc_seg2
                           ,lc_chr_acc_seg3
                           ,lc_chr_acc_seg4
                           ,lc_chr_acc_seg5
                           ,lc_chr_acc_seg6
                           ,lc_chr_acc_seg7);
      FETCH lcu_account_id
      INTO  ln_charge_acct_id;
      CLOSE lcu_account_id;

      IF ln_charge_acct_id IS NULL THEN
        lc_error_details := lc_error_details || 'Could not derive charge of accounts id :-'||lc_chr_acc_seg1||'-'||lc_chr_acc_seg2||'-'||lc_chr_acc_seg3||'-'||lc_chr_acc_seg4||'-'||lc_chr_acc_seg5||'-'||lc_chr_acc_seg6||'-'||lc_chr_acc_seg7;
      END IF;
*/

      lc_line_ship_loc := NULL;
      lc_new_ship      := NULL;
      ln_ship_loc_id   := NULL;
      lc_ship_org      := NULL;

      OPEN lcu_ship_po_line(lr_po_dist_data.po_number
                            ,lr_po_dist_data.shipment_num
                            ,lr_po_dist_data.po_line_number);
      FETCH lcu_ship_po_line
      INTO lc_line_ship_loc;
      CLOSE lcu_ship_po_line;

      lc_new_ship := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                              ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                              ,lc_line_ship_loc
                                                              );

      OPEN lcu_hr_loc_org(lc_new_ship);
       FETCH lcu_hr_loc_org
       INTO ln_ship_loc_id
           ,lc_ship_org;
      CLOSE lcu_hr_loc_org;

      --sjacob added
      --Derive charge account id
      IF lc_ship_org = 'CSD' THEN
        OPEN lcu_charge_account_id(ln_ship_loc_id,'UK_MAIN');
        FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
        CLOSE lcu_charge_account_id;
        IF ln_charge_acct_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
        END IF;
      ELSIF lc_ship_org = 'CSI' THEN
        OPEN lcu_charge_account_id(ln_ship_loc_id,'CSI_MAIN');
        FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
        CLOSE lcu_charge_account_id;
        IF ln_charge_acct_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
        END IF;
      ELSIF lc_ship_org = 'CSL' THEN
        OPEN lcu_charge_account_id(ln_ship_loc_id,'CSL_MAIN');
        FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
        CLOSE lcu_charge_account_id;
        IF ln_charge_acct_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
        END IF;
      ELSIF lc_ship_org = 'CDC' THEN
        OPEN lcu_charge_account_id(ln_ship_loc_id,'AK1_COM');
        FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
        CLOSE lcu_charge_account_id;
        IF ln_charge_acct_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
        END IF;
      ELSE
        OPEN lcu_charge_account_id(ln_ship_loc_id,'RM_COM');
        FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
        CLOSE lcu_charge_account_id;
        IF ln_charge_acct_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
        END IF;
      END IF;

      --Derive budget account id
      lc_errloc := 'Derive budget account id';
      OPEN  lcu_account_id (lc_bud_acc_seg1
                           ,lc_bud_acc_seg2
                           ,lc_bud_acc_seg3
                           ,lc_bud_acc_seg4
                           ,lc_bud_acc_seg5
                           ,lc_bud_acc_seg6
                           ,lc_bud_acc_seg7);
      FETCH lcu_account_id
      INTO  ln_budget_acct_id;
      CLOSE lcu_account_id;

      --Derive accrual account id
      lc_errloc := 'Derive accrual account id';
      OPEN  lcu_account_id (lc_accr_acc_seg1
                           ,lc_accr_acc_seg2
                           ,lc_accr_acc_seg3
                           ,lc_accr_acc_seg4
                           ,lc_accr_acc_seg5
                           ,lc_accr_acc_seg6
                           ,lc_accr_acc_seg7);
      FETCH lcu_account_id
      INTO  ln_accrual_acct_id;
      CLOSE lcu_account_id;

      --Derive variance account id
      lc_errloc := 'Derive variance account id';
      OPEN  lcu_account_id (lc_var_acc_seg1
                           ,lc_var_acc_seg2
                           ,lc_var_acc_seg3
                           ,lc_var_acc_seg4
                           ,lc_var_acc_seg5
                           ,lc_var_acc_seg6
                           ,lc_var_acc_seg7);
      FETCH lcu_account_id
      INTO  ln_variance_acct_id;
      CLOSE lcu_account_id;

      IF lc_error_details IS NULL THEN

          --Insert purchase order distrbutions data in interface table
          lc_errloc := 'Insert purchase order distrbutions data in interface table';

          INSERT INTO po_distributions_interface
          (interface_line_id
          ,interface_header_id
          ,interface_distribution_id
          ,process_code
          ,distribution_num
          ,org_id
          ,quantity_ordered
          ,quantity_delivered
          ,quantity_billed
          ,quantity_cancelled
          ,rate_date
          ,rate
          ,deliver_to_location_id
          ,deliver_to_person_id
          ,destination_type_code
          ,destination_organization
          ,destination_context
          ,set_of_books
          ,charge_account_id
          ,budget_account_id
          ,accrual_account_id
          ,variance_account_id
          ,amount_billed
          ,accrue_on_receipt_flag
          ,accrued_flag
          ,prevent_encumbrance_flag
          ,encumbered_flag
          ,gl_cancelled_date
          --,requisition_num
          --,requisition_line_num
          --,req_distribution_num
          ,project_accounting_context
          ,recoverable_tax
          ,nonrecoverable_tax
         -- ,recovery_rate
         -- ,tax_recovery_override_flag
          ,last_update_date
          ,last_updated_by
          ,creation_date
          ,created_by
          )
          VALUES
          (ln_po_int_line_id
          ,ln_po_int_header_id
          ,po_distributions_interface_s.nextval
          ,'PENDING'
          ,lr_po_dist_data.distribution_num
          ,ln_org_id
          ,(lr_po_dist_data.quantity_ordered - lr_po_dist_data.quantity_delivered - lr_po_dist_data.quantity_cancelled)
          ,0 --lr_po_dist_data.quantity_delivered
          ,0 --lr_po_dist_data.quantity_billed
          ,0 --lr_po_dist_data.quantity_cancelled
          ,lr_po_dist_data.rate_date
          ,lr_po_dist_data.rate
          ,ln_location_id
          ,ln_person_id
          ,lr_po_dist_data.destination_type_code
          ,lc_new_org_name --lr_po_dist_data.destination_organization
          ,lr_po_dist_data.destination_context
          ,ln_sob_id
          ,ln_charge_acct_id
          ,ln_budget_acct_id
          ,ln_accrual_acct_id
          ,ln_variance_acct_id
          ,lr_po_dist_data.amount_billed
          ,'Y' -- lr_po_dist_data.accrue_on_receipt_flag as per MDT version 1.23
          ,lr_po_dist_data.accrued_flag
          ,lr_po_dist_data.prevent_encumbrance_flag
          ,lr_po_dist_data.encumbered_flag
          ,lr_po_dist_data.gl_cancelled_date
          --,lr_po_dist_data.requisition_num
          --,lr_po_dist_data.requisition_line_num
          --,lr_po_dist_data.req_distribution_num
          ,lr_po_dist_data.project_accounting_context
          ,lr_po_dist_data.recoverable_tax
          ,lr_po_dist_data.nonrecoverable_tax
         -- ,lr_po_dist_data.recovery_rate
         -- ,lr_po_dist_data.tax_recovery_override_flag
          ,SYSDATE
          ,fnd_global.user_id
          ,SYSDATE
          ,fnd_global.user_id
          );

          UPDATE xxcsr_po_distributions_int
          SET    record_status = 'C'
                ,request_id    = FND_GLOBAL.conc_request_id
                ,error_message = NULL
          WHERE rowid          = lr_po_dist_data.rowid;

          ln_dist_cnt := ln_dist_cnt + 1;

      ELSE
        UPDATE xxcsr_po_distributions_int
        SET    record_status = 'E'
              ,request_id    = FND_GLOBAL.conc_request_id
              ,error_message = lc_error_details
        WHERE  rowid         = lr_po_dist_data.rowid;

        IF ln_po_int_line_id IS NOT NULL THEN
   UPDATE xxcsr_po_lines_int
   SET record_status = 'E'
      ,error_message = error_message||'error in distribution record'
   WHERE  po_number = lr_po_dist_data.po_number
   AND line_num = lr_po_dist_data.po_line_number
   AND shipment_num = lr_po_dist_data.shipment_num
   AND source_instance = p_instance_name;

   DELETE FROM po_lines_interface
   WHERE interface_line_id = ln_po_int_line_id;

   ln_lines_cnt := ln_lines_cnt - 1;

 END IF;

 IF ln_po_int_header_id IS NOT NULL THEN
   UPDATE xxcsr_po_headers_int
   SET record_status = 'E'
      ,error_message = error_message||'error in distribution record'
   WHERE  po_number = lr_po_dist_data.po_number
   AND source_instance = p_instance_name;

   DELETE FROM po_headers_interface
   WHERE interface_header_id = ln_po_int_header_id;

   ln_header_cnt := ln_header_cnt - 1;

        END IF;
      END IF; --lc_error_details IS NULL

      COMMIT;
  END LOOP;
  COMMIT;

  IF ln_header_cnt > 0 THEN

      --Calling Import Standard Purchase Orders program
      lc_errloc     := 'Call Import Standard Purchase Orders program';

      FND_FILE.PUT_LINE(FND_FILE.LOG,'Submitting Import Standard Purchase Orders concurrent program.');

      ln_cur_org_id := FND_PROFILE.VALUE('ORG_ID');

      FND_FILE.PUT_LINE(FND_FILE.LOG,'ln_cur_org_id : ' || ln_cur_org_id);

      ln_request_id := FND_REQUEST.SUBMIT_REQUEST (
                                                    application => 'PO'
                                                   ,program     => 'POXPOPDOI'
                                                   ,description =>  'Import Standard Purchase Orders'
                                                   ,start_time  =>  SYSDATE
                                                   ,sub_request =>  FALSE
                                                   ,argument1   =>  NULL --Default Buyer
                                                   ,argument2   =>  'STANDARD' --Document Type
                                                   ,argument3   =>  NULL --Document SubType
                                                   ,argument4   =>  'N' --Create or Update Items
                                                   ,argument5   =>  NULL --Create Sourcing Rules
                                                   ,argument6   =>  'APPROVED' --Approval Status
                                                   ,argument7   =>  NULL --Release Generation Method
                                                   ,argument8   =>  NULL --Batch Id
                                                   ,argument9   =>  ln_cur_org_id --Operating Unit
                                                   ,argument10  =>  NULL --Global Agreement
                                                   ,argument11  =>  NULL --Enable Sourcing Level
                                                   ,argument12  =>  NULL --Sourcing Level
                                                   ,argument13  =>  NULL --Inv Org Enable
                                                   ,argument14  =>  NULL --Inventory Organization
                                                  );
      COMMIT;

      IF ln_request_id = 0 THEN
          lc_error := 'Error submitting Import Standard Purchase Orders concurrent program.';
          RAISE ex_conc_error;
      END IF;

      FND_FILE.PUT_LINE(FND_FILE.LOG,'Import Standard Purchase Orders concurrent program submitted. Request_id : ' || ln_request_id);

      --Wait for the request to complete
      lb_wait := FND_CONCURRENT.WAIT_FOR_REQUEST
             (
               request_id  => ln_request_id
              ,interval    => 2
              ,phase       => lc_phase
              ,status      => lc_status
              ,dev_phase   => lc_devpha
              ,dev_status  => lc_devsta
              ,message     => lc_mesg
             );

  END IF; --ln_header_cnt > 0

  x_header_cnt := ln_header_cnt;
  x_lines_cnt  := ln_lines_cnt;
  x_dist_cnt   := ln_dist_cnt;
  x_req_id     := ln_request_id;

--
EXCEPTION
  WHEN EX_CONC_ERROR THEN
    x_retcode := 2;
    x_errbuf  := lc_errmsg;
    FND_FILE.PUT_LINE(FND_FILE.LOG,lc_errmsg);

  WHEN OTHERS THEN
    x_errbuf  := SUBSTR(SQLERRM,1,200);
    x_retcode := 2;
    FND_FILE.PUT_LINE(FND_FILE.LOG,'Unexpected error occurred in import procedure at ' || lc_errloc || ' Error : ' || SQLERRM);

END import_sirf;

--Import Routine
PROCEDURE import_csr
   (p_rec_status      IN         VARCHAR2
   ,p_instance_name   IN         VARCHAR2
   ,x_errbuf          OUT NOCOPY VARCHAR2
   ,x_retcode         OUT NOCOPY NUMBER
   ,x_header_cnt      OUT NOCOPY NUMBER
   ,x_lines_cnt       OUT NOCOPY NUMBER
   ,x_dist_cnt        OUT NOCOPY NUMBER
   ,x_req_id          OUT NOCOPY NUMBER
   )
IS
--
  CURSOR lcu_po_header_data (pv_rec_status VARCHAR2)
  IS
      SELECT XHEC.rowid,XHEC.*
      FROM   xxcsr_po_headers_int XHEC
      WHERE  XHEC.record_status = pv_rec_status
      AND    XHEC.request_id = gn_conc_req_id
      AND XHEC.source_instance = p_instance_name
      AND    XHEC.document_type_code <> 'CONTRACT';

  CURSOR lcu_po_header_contr (pv_rec_status VARCHAR2)
  IS
      SELECT XHEC.rowid,XHEC.*
      FROM   xxcsr_po_headers_int XHEC
      WHERE  XHEC.record_status = pv_rec_status
      AND    XHEC.request_id = gn_conc_req_id
      AND    XHEC.source_instance = p_instance_name
      AND    XHEC.document_type_code = 'CONTRACT';

  CURSOR lcu_po_lines_data (pv_rec_status VARCHAR2)
  IS
      SELECT XHEC.rowid,XHEC.*
      FROM   xxcsr_po_lines_int XHEC
      WHERE  XHEC.record_status = pv_rec_status
      AND    XHEC.request_id = gn_conc_req_id
      AND XHEC.source_instance = p_instance_name;

  CURSOR lcu_po_dist_data (pv_rec_status VARCHAR2)
  IS
      SELECT XHEC.rowid,XHEC.*
      FROM   xxcsr_po_distributions_int XHEC
      WHERE  XHEC.record_status = pv_rec_status
      AND    XHEC.request_id = gn_conc_req_id
      AND XHEC.source_instance = p_instance_name;

  CURSOR lcu_chk_currency (pv_currency_code VARCHAR2)
  IS
      SELECT currency_code
      FROM   fnd_currencies
      WHERE  currency_code = pv_currency_code
      AND    enabled_flag = 'Y'
      AND    SYSDATE BETWEEN NVL(START_DATE_ACTIVE, SYSDATE) AND NVL(END_DATE_ACTIVE, SYSDATE);

  CURSOR lcu_item_cat_id (pv_seg1 VARCHAR2,pv_seg2 VARCHAR2,pv_seg3 VARCHAR2,pv_seg4 VARCHAR2,pv_seg5 VARCHAR2)
  IS
      SELECT category_id
      FROM   mtl_categories
      WHERE  segment1 = pv_seg1
      AND    segment2 = pv_seg2
      AND    segment3 = pv_seg3
      AND    segment4 = pv_seg4
      AND    segment5 = pv_seg5
      AND    enabled_flag = 'Y'
      AND    SYSDATE BETWEEN NVL(START_DATE_ACTIVE, SYSDATE) AND NVL(END_DATE_ACTIVE, SYSDATE);

  CURSOR lcu_get_int_header_id (pv_po_number VARCHAR2)
  IS
      SELECT interface_header_id
      FROM   po_headers_interface
      WHERE  document_num = pv_po_number
      ORDER BY 1 DESC;

  CURSOR lcu_get_int_line_id (pv_po_number VARCHAR2
                             ,pv_po_line_number VARCHAR2
                             ,p_shipment_num    VARCHAR2)
  IS
      SELECT interface_line_id
      FROM   po_lines_interface
      WHERE  document_num = pv_po_number
      AND    line_num     = pv_po_line_number
      AND    shipment_num = p_shipment_num;

  CURSOR lcu_account_id (pv_seg1 VARCHAR2,pv_seg2 VARCHAR2,pv_seg3 VARCHAR2,pv_seg4 VARCHAR2,pv_seg5 VARCHAR2,pv_seg6 VARCHAR2,pv_seg7 VARCHAR2)
  IS
      SELECT code_combination_id
      FROM   gl_code_combinations_kfv
      WHERE  segment1 = pv_seg1
      AND    segment2 = pv_seg2
      AND    segment3 = pv_seg3
      AND    segment4 = pv_seg4
      AND    segment5 = pv_seg5
      AND    segment6 = pv_seg6
      AND    segment7 = pv_seg7;

  CURSOR lcu_charge_account_id  (p_ship_location NUMBER, p_second_inv VARCHAR2)
  IS
  SELECT GCC.code_combination_id
  FROM   gl_code_combinations gcc
        ,mtl_secondary_inventories msi
        ,hr_locations hl
  WHERE  MSI.material_account         = GCC.code_combination_id
  AND    MSI.secondary_inventory_name = p_second_inv
  AND    MSI.organization_id          = HL.inventory_organization_id
  AND    HL.location_id               = p_ship_location;

  CURSOR lcu_get_sob_id (pv_sob_name VARCHAR2)
  IS
      SELECT set_of_books_id
      FROM   gl_sets_of_books
      WHERE  name = pv_sob_name;


  --sjacob commented below
  CURSOR lcu_location_id (pv_loc_code VARCHAR2)
  IS
      SELECT location_id
      FROM   hr_locations
      WHERE  location_code = pv_loc_code;

  CURSOR lcu_item_id(p_seg1 IN VARCHAR2
                    ,p_seg2 IN VARCHAR2
                    ,p_seg3 IN VARCHAR2  -- sjacob commented as per Siva's mail
                    )
  IS
  SELECT MSIB.inventory_item_id
  FROM mtl_system_items_b MSIB
  WHERE MSIB.segment1 = p_seg1
  AND   NVL(MSIB.segment2,-99) = NVL(p_seg2,-99)
  AND   NVL(MSIB.segment3,-99) = NVL(p_seg3,-99)       -- sjacob commented as per Siva's mail
  AND   ROWNUM <= 1;

  CURSOR lcu_vendor_der(p_vendor IN VARCHAR2)
  IS
  SELECT vendor_id
  FROM po_vendors
  WHERE attribute15 = p_vendor;

  CURSOR lcu_hr_loc(p_location_code IN VARCHAR2)
  IS
  SELECT HLA.location_id
  FROM hr_locations_all HLA
  WHERE HLA.location_code = p_location_code;

  CURSOR lcu_hr_loc_org(p_location_code IN VARCHAR2)
  IS
  SELECT HLA.location_id
        ,OOD.organization_code
  FROM hr_locations_all HLA
      ,org_organization_definitions OOD
  WHERE HLA.location_code = p_location_code
  AND   OOD.organization_id = HLA.inventory_organization_id;


  CURSOR lcu_ship_po_line(p_po_number IN VARCHAR2
                         ,p_shipment_num IN NUMBER
                         ,p_line_num     IN NUMBER)
  IS
  SELECT ship_to_location
  FROM xxcsr_po_lines_int XPLI
  WHERE XPLI.po_number = p_po_number
  AND   XPLI.shipment_num = p_shipment_num
  AND   XPLI.line_num = p_line_num
  AND   XPLI.source_instance = p_instance_name;

  CURSOR lcu_func_curr(p_org_id IN NUMBER)
  IS
  SELECT GL.currency_code
  FROM hr_operating_units HOU
      ,gl_ledgers GL
  WHERE GL.ledger_id = HOU.set_of_books_id
  AND   HOU.organization_id = p_org_id;

  CURSOR lcu_po_line_itm(p_po_num IN VARCHAR2)
  IS
  SELECT count('x')
  FROM xxcsr_po_lines_int XPLI
  WHERE XPLI.po_number = p_po_num
  AND   XPLI.itm_seg1_item_num IS NOT NULL
  AND   XPLI.source_instance = p_instance_name;

  CURSOR lcu_vendor_site(p_vendor_site_code IN VARCHAR2
                        ,p_vendor_id IN NUMBER)
  IS
  SELECT POVS.vendor_site_id
  FROM po_vendor_sites_all POVS
  WHERE POVS.vendor_site_code = p_vendor_site_code
  AND POVS.vendor_id = p_vendor_id;

  CURSOR lcu_vendor_contact(p_vendor_site_id IN NUMBER)
  IS
  SELECT PVC.vendor_contact_id
  FROM po_vendor_contacts PVC
  WHERE PVC.vendor_site_id = p_vendor_site_id;

  CURSOR lcu_pay_term(p_payterm IN VARCHAR2)
  IS
  SELECT APT.term_id
  FROM ap_terms APT
  WHERE APT.name  = p_payterm;

  CURSOR lcu_check_po_exists(p_po_number IN VARCHAR2)
  IS
  SELECT 1
  FROM po_headers_all PHA
  WHERE PHA.segment1 = p_po_number;

  CURSOR lcu_check_lines(p_po_number IN VARCHAR2)
  IS
  SELECT 1
  FROM xxcsr_po_lines_int XPLI
  WHERE XPLI.po_number = p_po_number;

  CURSOR lcu_prim_uom(p_org_code IN VARCHAR2
                     ,p_item_id IN NUMBER)
  IS
  SELECT MSIB.primary_unit_of_measure
  FROM mtl_system_items_b MSIB
      ,mtl_parameters MP
  WHERE MSIB.inventory_item_id = p_item_id
  AND   MSIB.organization_id = MP.master_organization_id
  AND   MP.organization_code = p_org_code;

  CURSOR lcu_supsite_attr15(p_doc_num VARCHAR2
                           ,p_lin_num VARCHAR2
                           ,p_shp_num VARCHAR2)
  IS
  SELECT PVS.attribute15
  FROM   po_headers_interface PHI
        ,po_lines_interface PLI
        ,po_vendor_sites_all PVS
  WHERE  PHI.interface_header_id = PLI.interface_header_id
  AND    PHI.vendor_id = PVS.vendor_id
  AND    PHI.vendor_id = PVS.vendor_id
  AND    PVS.org_id = PHI.org_id
  AND    PVS.vendor_site_code = PHI.vendor_site_code
  AND    PLI.document_num = p_doc_num
  AND    PLI.line_num = p_lin_num
  AND    PLI.shipment_num = p_shp_num;

    lr_po_header_data            lcu_po_header_data%ROWTYPE;
    lr_po_lines_data             lcu_po_lines_data%ROWTYPE;
    lr_po_dist_data              lcu_po_dist_data%ROWTYPE;

    lc_errmsg                    VARCHAR2(2000);
    lc_errloc                    VARCHAR2(1000);

    ln_request_id                NUMBER;
    lc_phase                     VARCHAR2(500);
    lc_status                    VARCHAR2(500);
    lc_devpha                    VARCHAR2(500);
    lc_devsta                    VARCHAR2(500);
    lc_mesg                      VARCHAR2(50);
    lb_wait                      BOOLEAN;

    ln_org_id                    NUMBER;
    ln_item_org_id               NUMBER;
    ln_agent_id                  NUMBER;
    lc_currency_code             VARCHAR2(15);
    ln_vendor_id                 NUMBER;
    lc_payment_terms             VARCHAR2(50);
    lc_freight_terms             VARCHAR2(25);
    ln_item_id                   NUMBER;
    lc_item_seg2                 VARCHAR2(40);
    lc_cat_seg1                  VARCHAR2(40);
    lc_cat_seg2                  VARCHAR2(40);
    lc_cat_seg3                  VARCHAR2(40);
    lc_cat_seg4                  VARCHAR2(40);
    lc_cat_seg5                  VARCHAR2(40);
    lc_inv_org_code              VARCHAR2(30);
    ln_cat_id                    NUMBER;
    ln_po_int_header_id          NUMBER;
    ln_po_int_line_id            NUMBER;
    ln_tax_code_id               NUMBER;
    ln_charge_acct_id            NUMBER;
    ln_budget_acct_id            NUMBER;
    ln_accrual_acct_id           NUMBER;
    ln_variance_acct_id          NUMBER;
    ln_sob_id                    NUMBER;
    ln_location_id               NUMBER;
    ln_person_org_id             NUMBER;
    ln_person_id                 NUMBER;
    ln_bu_id                     NUMBER;

    lc_chr_acc_seg1              VARCHAR2(40);
    lc_chr_acc_seg2              VARCHAR2(40);
    lc_chr_acc_seg3              VARCHAR2(40);
    lc_chr_acc_seg4              VARCHAR2(40);
    lc_chr_acc_seg5              VARCHAR2(40);
    lc_chr_acc_seg6              VARCHAR2(40);
    lc_chr_acc_seg7              VARCHAR2(40);

    lc_bud_acc_seg1              VARCHAR2(40);
    lc_bud_acc_seg2              VARCHAR2(40);
    lc_bud_acc_seg3              VARCHAR2(40);
    lc_bud_acc_seg4              VARCHAR2(40);
    lc_bud_acc_seg5              VARCHAR2(40);
    lc_bud_acc_seg6              VARCHAR2(40);
    lc_bud_acc_seg7              VARCHAR2(40);

    lc_accr_acc_seg1              VARCHAR2(40);
    lc_accr_acc_seg2              VARCHAR2(40);
    lc_accr_acc_seg3              VARCHAR2(40);
    lc_accr_acc_seg4              VARCHAR2(40);
    lc_accr_acc_seg5              VARCHAR2(40);
    lc_accr_acc_seg6              VARCHAR2(40);
    lc_accr_acc_seg7              VARCHAR2(40);

    lc_var_acc_seg1              VARCHAR2(40);
    lc_var_acc_seg2              VARCHAR2(40);
    lc_var_acc_seg3              VARCHAR2(40);
    lc_var_acc_seg4              VARCHAR2(40);
    lc_var_acc_seg5              VARCHAR2(40);
    lc_var_acc_seg6              VARCHAR2(40);
    lc_var_acc_seg7              VARCHAR2(40);
    lc_seg3_value                VARCHAR2(40);
    lc_seg5_value                VARCHAR2(40);

    lb_success                   BOOLEAN;

    lc_error_details             VARCHAR2(4000);
    lc_error                     VARCHAR2(4000);

    lc_errbuf                    VARCHAR2(240);
    ln_retcode                   NUMBER;

    ln_header_cnt                NUMBER;
    ln_lines_cnt                 NUMBER;
    ln_dist_cnt                  NUMBER;

    ln_cur_org_id                NUMBER;

    lc_new_orgname               VARCHAR2(240);
    lc_item_segment2             VARCHAR2(40);
    lc_item_segment3             VARCHAR2(40);
    lc_new_glacct                VARCHAR2(240);
    lc_new_cat                   VARCHAR2(2000);
    lc_new_del                   VARCHAR2(240);
    lc_new_org_name              VARCHAR2(270);
    lc_new_sobname               VARCHAR2(270);
    lc_business_group            VARCHAR2(270);
    ln_bill_loc_id               NUMBER;
    ln_ship_loc_id               NUMBER;
    lc_inv_po_chk                VARCHAR2(270);
    lc_func_curr                 VARCHAR2(15);
    lc_rate_type                 VARCHAR2(30);
    ln_rate                      NUMBER;
    ld_rate_date                 DATE;
    lc_new_ship                  VARCHAR2(240);
    lc_new_bill                  VARCHAR2(240);
    ln_line_item_cnt             NUMBER;
    ln_po_item_chk               NUMBER;
    ln_vendor_site_id            NUMBER;
    ln_vendor_contact_id         NUMBER;
    ln_term_id                   NUMBER;
    lc_po_exists                 VARCHAR2(1);
    lc_line_exists               VARCHAR2(1);
    lc_primary_uom               VARCHAR2(25);
    lc_ship_via_lookup_code      VARCHAR2(25);

  lc_line_ship_loc  VARCHAR2(60);
  lc_ship_org       VARCHAR2(3);
  ln_po_header_id   NUMBER;

    ex_conc_error                EXCEPTION;

--
BEGIN

    ln_header_cnt := 0;

    --Process Purchase Orders data
    lc_errloc := 'Process purchase orders data';
    FOR lr_po_header_data IN lcu_po_header_data (p_rec_status)
    LOOP

        ln_org_id                   := NULL;
        ln_agent_id                 := NULL;
        ln_bu_id                    := NULL;
        lc_currency_code            := NULL;
        ln_vendor_id                := NULL;
        lc_payment_terms            := NULL;
        lc_freight_terms            := NULL;

        lc_error_details            := NULL;
        lc_new_orgname              := NULL;
        lc_business_group           := NULL;
        ln_bill_loc_id              := NULL;
        ln_ship_loc_id              := NULL;
        lc_inv_po_chk               := NULL;
        lc_func_curr                := NULL;
        lc_rate_type                := NULL;
        ln_rate                     := NULL;
        ld_rate_date                := NULL;
        lc_new_ship                 := NULL;
        lc_new_bill                 := NULL;
        lc_line_exists              := NULL;
        lc_primary_uom              := NULL;

        -- check if lines exist for this PO
        OPEN lcu_check_lines(lr_po_header_data.po_number);
        FETCH lcu_check_lines
        INTO lc_line_exists;
        CLOSE lcu_check_lines;

        IF lc_line_exists IS NULL THEN
          lc_error_details := lc_error_details||'No lines were found in the staging table for the given PO. ';
        END IF;

        lc_errloc   := 'Operating unit related derivations';
        lc_new_orgname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_OPERATING_UNIT'
                                                              ,'XXSIRF_CNV_OPERATING_UNIT'
                                                              ,lr_po_header_data.operating_unit
                                                              );

        IF lc_new_orgname IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive new operating unit for '||lr_po_header_data.operating_unit||'. ';
        ELSE
          ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lc_new_orgname);
          IF ln_org_id IS NULL THEN
            lc_error_details := lc_error_details||'Could not derive org id for new operating unit '||lc_new_orgname||'. ';
          ELSE
            -- Compare functional and document currency
            OPEN lcu_func_curr(ln_org_id);
            FETCH lcu_func_curr
            INTO lc_func_curr;
            CLOSE lcu_func_curr;

            IF lc_func_curr = lr_po_header_data.currency_code THEN
              lc_rate_type := NULL;
              ln_rate      := NULL;
              ld_rate_date := NULL;
            ELSE
              lc_rate_type := lr_po_header_data.rate_type;
              ln_rate      := lr_po_header_data.rate;
              ld_rate_date := lr_po_header_data.rate_date;

            END IF;
          END IF;
        END IF;

        lc_errloc := 'Checking for inventory purchase orders';
        lc_inv_po_chk := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_INV_SUPPLIERS'
                                                              ,'XXSIRF_CNV_INV_SUPPLIERS'
                                                              ,lr_po_header_data.vendor_name
                                                              );

        IF lc_inv_po_chk IS NULL THEN
          lc_error_details := lc_error_details||'Only inventory purchase orders will be migrated';
        END IF;



        IF lr_po_header_data.emp_bu_name IN ( 'CSR United States','CSR United Kingdom') THEN
          lc_business_group := lr_po_header_data.emp_bu_name;
        ELSE
          lc_business_group := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                   ,'XXCSR_CNV_HR_BG'
                                                                   ,'XXSIRF_CNV_HR_BG'
                                                                   ,lr_po_header_data.emp_bu_name
                                                                   );
        END IF;

        IF lc_business_group IS NULL THEN
          lc_error_details := lc_error_details||'Mapping for Business Group '||lr_po_header_data.emp_bu_name||' could not be found. ';
        ELSE
          ln_bu_id := XXCSR_COMMON_PKG.get_business_group_id(lc_business_group);
          IF ln_bu_id IS NULL THEN
            lc_error_details:= lc_error_details||' Business Group ID for '||lc_business_group||' could not be derived. ';
          ELSE
            --Derive agent id
            lc_errloc := 'Derive agent id';
            ln_agent_id := XXCSR_COMMON_PKG.get_person_id (p_first_name        => lr_po_header_data.emp_fname
                                                          ,p_last_name         => lr_po_header_data.emp_lname
                                                          ,p_date_of_birth     => lr_po_header_data.emp_dob
                                                          ,p_business_group_id => ln_bu_id
                                                          );
            IF ln_agent_id IS NULL THEN
                lc_error_details := lc_error_details || ' Invalid agent (employee) details.';
            END IF;

          END IF;
        END IF;



        --Check currency code
        lc_errloc := 'Check currency code';
        OPEN  lcu_chk_currency (lr_po_header_data.currency_code);
        FETCH lcu_chk_currency
        INTO  lc_currency_code;

        IF lcu_chk_currency%NOTFOUND THEN
            lc_error_details := lc_error_details || ' Invalid currency code.';
        END IF;

        CLOSE lcu_chk_currency;

        --Derive vendor id
        lc_errloc := 'Derive vendor id';

        IF lr_po_header_data.vendor_name IS NOT NULL THEN
          OPEN lcu_vendor_der(lr_po_header_data.vendor_name);
          FETCH lcu_vendor_der
          INTO  ln_vendor_id;
          CLOSE lcu_vendor_der;

          IF ln_vendor_id IS NULL THEN
              lc_error_details := lc_error_details || ' Vendor not migrated.';
          END IF;
        END IF;


        lc_errloc := 'Check payment terms';
        IF lr_po_header_data.payment_terms IS NOT NULL THEN
        lc_payment_terms := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                ,p_csr_lookup    => 'XXCSR_CNV_SUPPLIER_PAYTERMS'
                                                                ,p_sirf_lookup   => 'XXSIRF_CNV_SUPPLIER_PAYTERMS'
                                                                ,p_old_value     => lr_po_header_data.payment_terms
                                                                );

        IF lc_payment_terms IS NULL THEN
            lc_error_details := lc_error_details || ' Invalid payment terms.';
        END IF;
        END IF;

        --Check freight terms
        lc_errloc := 'Check freight terms';
        IF lr_po_header_data.freight_terms IS NOT NULL THEN
          lc_freight_terms := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                  ,p_csr_lookup    => 'XXCSR_CNV_FREIGHT_TERMS'
                                                                  ,p_sirf_lookup   => 'XXSIRF_CNV_FREIGHT_TERMS'
                                                                  ,p_old_value     => lr_po_header_data.freight_terms
                                                                  );

          IF lc_freight_terms IS NULL THEN
              lc_error_details := lc_error_details || ' Invalid freight terms.';
          END IF;

        END IF;

        IF lr_po_header_data.ship_to_location IS NOT NULL THEN

          lc_new_ship := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                                ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                                ,lr_po_header_data.ship_to_location
                                                                );
          IF lc_new_ship IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new ship to location for '||lr_po_header_data.ship_to_location||' in XXCSR_CNV_DELIVER_LOCATIONS. ';

          ELSE

            OPEN lcu_hr_loc(lc_new_ship);
            FETCH lcu_hr_loc
            INTO ln_ship_loc_id;
            CLOSE lcu_hr_loc;

            IF ln_ship_loc_id IS NULL THEN
              lc_error_details := lc_error_details || 'Ship to location id could not be derived for '||lr_po_header_data.ship_to_location;
            END IF;
          END IF;

        END IF;

        IF lr_po_header_data.bill_to_location IS NOT NULL THEN

          lc_new_bill := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_BILLTO_LOCATIONS'
                                                                ,'XXSIRF_CNV_BILLTO_LOCATIONS'
                                                                ,lr_po_header_data.bill_to_location
                                                                );
          IF lc_new_bill IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new bill to location for '||lr_po_header_data.bill_to_location||' in XXCSR_CNV_BILLTO_LOCATIONS. ';

          ELSE

            OPEN lcu_hr_loc(lc_new_bill);
            FETCH lcu_hr_loc
            INTO ln_bill_loc_id;
            CLOSE lcu_hr_loc;

            IF ln_bill_loc_id IS NULL THEN
              lc_error_details := lc_error_details || 'Bill to location id could not be derived for '||lr_po_header_data.bill_to_location;
            END IF;

          END IF;

        END IF;

        lc_ship_via_lookup_code := NULL;
        IF lr_po_header_data.ship_via_lookup_code IS NOT NULL THEN

          lc_ship_via_lookup_code := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                        ,'XXCSR_CNV_FREIGHT_CARRIER'
                                                                        ,'XXCSR_CNV_FREIGHT_CARRIER'
                                                                        ,lr_po_header_data.ship_via_lookup_code
                                                                        );

        END IF;


        IF lc_error_details IS NULL THEN

            --Insert purchase order header data in interface table
            lc_errloc := 'Insert purchase order header data in interface table';
            INSERT INTO po_headers_interface
            (interface_header_id
            ,batch_id
            ,interface_source_code
            ,action
            ,process_code
            ,org_id
            ,document_type_code
            ,currency_code
            ,rate_type
            ,rate_date
            ,rate
            ,agent_id
            ,vendor_id
            ,vendor_site_code
            ,ship_to_location_id
            ,bill_to_location_id
            ,payment_terms
            ,freight_terms
            ,approval_status
            ,approved_date
          --  ,revised_date
          --  ,revision_num
            ,confirming_order_flag
            ,comments
            ,acceptance_required_flag
            ,print_count
          --  ,printed_date
            ,frozen_flag
            ,closed_code
            ,from_rfq_num
            ,from_type_lookup_code
            ,attribute_category
            --,attribute1
            ,attribute2
           -- ,attribute3
            --,global_attribute1
            ,pay_on_code
            ,last_update_date
            ,last_updated_by
            ,creation_date
            ,created_by
            ,document_num
            ,freight_carrier
            )
            VALUES
            (po_headers_interface_s.nextval
            ,1
            ,'XXCSR Conversion'
            ,'ORIGINAL'
            ,'PENDING'
            ,ln_org_id
            ,lr_po_header_data.document_type_code -- 'STANDARD'
            ,lr_po_header_data.currency_code
            ,DECODE(lc_rate_type,'Corporate','User',lc_rate_type)
            ,ld_rate_date
            ,ln_rate
            ,ln_agent_id
            ,ln_vendor_id
            ,lr_po_header_data.vendor_site_code
            ,ln_ship_loc_id -- lr_po_header_data.ship_to_location
            ,ln_bill_loc_id -- lr_po_header_data.bill_to_location
            ,lc_payment_terms
            ,lc_freight_terms
            ,DECODE(lr_po_header_data.approval_status,'REQUIRES REAPPROVAL','INCOMPLETE',lr_po_header_data.approval_status)
            ,lr_po_header_data.approved_date
           -- ,lr_po_header_data.revised_date
           -- ,lr_po_header_data.revision_num
            ,lr_po_header_data.confirming_order_flag
            ,lr_po_header_data.comments
            ,lr_po_header_data.acceptance_required_flag
            --,DECODE(lr_po_header_data.approval_status,'APPROVED',lr_po_header_data.print_count,0)
            ,DECODE(lr_po_header_data.approval_status,'APPROVED',1,0)
          --  ,lr_po_header_data.printed_date
            ,lr_po_header_data.frozen_flag
            ,lr_po_header_data.closed_code
            ,lr_po_header_data.from_requisition_num
            ,lr_po_header_data.from_type_lookup_code
            ,lr_po_header_data.attribute_category
            --,lr_po_header_data.sourcing_rule
            ,lr_po_header_data.osp_flag
            --,lr_po_header_data.mrp_flag
            --,lr_po_header_data.po_total_plus_ipi_tax
            ,lr_po_header_data.pay_on_code
            ,SYSDATE
            ,fnd_global.user_id
            ,SYSDATE
            ,fnd_global.user_id
            ,lr_po_header_data.po_number
            ,lc_ship_via_lookup_code
            );

            UPDATE xxcsr_po_headers_int
            SET    record_status = 'C'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = NULL
            WHERE rowid = lr_po_header_data.rowid;

            COMMIT;

            ln_header_cnt := ln_header_cnt + 1;

        ELSE
            UPDATE xxcsr_po_headers_int
            SET    record_status = 'E'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = lc_error_details
            WHERE  rowid = lr_po_header_data.rowid;
        END IF; --lc_error_details IS NULL

        COMMIT;
    END LOOP;
    --COMMIT;


    ln_lines_cnt := 0;

    --Process Purchase Order Lines data
    lc_errloc := 'Process purchase order lines data';
    FOR lr_po_lines_data IN lcu_po_lines_data (p_rec_status)
    LOOP

        ln_org_id                   := NULL;
        ln_item_org_id              := NULL;
        ln_item_id                  := NULL;
        lc_item_seg2                := NULL;
        lc_cat_seg1                 := NULL;
        lc_cat_seg2                 := NULL;
        lc_cat_seg3                 := NULL;
        lc_cat_seg4                 := NULL;
        lc_cat_seg5                 := NULL;
        ln_cat_id                   := NULL;
        ln_po_int_header_id         := NULL;
        ln_tax_code_id              := NULL;

        lc_error_details            := NULL;

        lc_new_orgname              := NULL;
        lc_item_segment2            := NULL;
        lc_new_glacct               := NULL;
        lc_new_cat                  := NULL;
        lc_new_ship                 := NULL;
        ln_ship_loc_id              := NULL;
        lc_new_org_name             := NULL;

        --Derive interface header id
        lc_errloc := 'Derive interface header id';
        OPEN  lcu_get_int_header_id (lr_po_lines_data.po_number);
        FETCH lcu_get_int_header_id
        INTO  ln_po_int_header_id;
        CLOSE lcu_get_int_header_id;

        IF ln_po_int_header_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive interface header id. Header record may have error. ';
        END IF;

        FND_FILE.PUT_LINE(FND_FILE.LOG,'ln_po_int_header_id : ' || ln_po_int_header_id);

        lc_errloc   := 'Operating unit related derivations';
        lc_new_orgname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_OPERATING_UNIT'
                                                              ,'XXSIRF_CNV_OPERATING_UNIT'
                                                              ,lr_po_lines_data.org_name
                                                              );

        IF lc_new_orgname IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive new operating unit for '||lr_po_lines_data.org_name||'. ';
        ELSE
          ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lc_new_orgname);
          IF ln_org_id IS NULL THEN
            lc_error_details := lc_error_details||'Could not derive org id for new operating unit '||lc_new_orgname||'. ';
          END IF;
        END IF;

        IF lr_po_lines_data.ship_to_location IS NOT NULL THEN

          lc_new_ship := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                                ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                                ,lr_po_lines_data.ship_to_location
                                                                );
          IF lc_new_ship IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new ship to location for '||lr_po_lines_data.ship_to_location||' in XXCSR_CNV_DELIVER_LOCATIONS. ';

          ELSE

            OPEN lcu_hr_loc(lc_new_ship);
            FETCH lcu_hr_loc
            INTO ln_ship_loc_id;
            CLOSE lcu_hr_loc;

            IF ln_ship_loc_id IS NULL THEN
              lc_error_details := lc_error_details || 'Ship to location id could not be derived for '||lr_po_lines_data.ship_to_location;
            END IF;
          END IF;

        END IF;

        IF lr_po_lines_data.ship_to_org IS NOT NULL THEN

          lc_new_org_name := XXCSR_COMMON_PKG.get_organization_code (p_instance_name
                                                                    ,lr_po_lines_data.ship_to_org);

          IF lc_new_org_name IS NULL THEN
            lc_error_details := lc_error_details||'Mapping for organization '||lr_po_lines_data.ship_to_org||' not found in the table XXCSR_CNV_ORG_MAPPINGS. ';
          END IF;

        END IF;

      lc_item_segment3 := NULL;

      IF lr_po_lines_data.itm_seg3_item_type = 'MT' THEN
          lc_item_segment2 := 'MT';
          lc_item_segment3 := NULL;
      ELSIF lr_po_lines_data.itm_seg3_item_type = 'FL' THEN
          lc_item_segment2 := 'PD';
          lc_item_segment3 := NULL;
      ELSE
          lc_item_segment2 := lr_po_lines_data.itm_seg2_bom_level;
          lc_item_segment3 := lr_po_lines_data.itm_seg3_item_type;
      END IF;

      lc_errloc := 'Derive inventory item id';
   -- IF lr_po_lines_data.itm_seg1_item_num IS NOT NULL THEN

        OPEN lcu_item_id(lr_po_lines_data.itm_seg1_item_num
                        ,lc_item_segment2
                        ,lc_item_segment3); --sjacob commented as per Siva's mail lr_po_lines_data.itm_seg3_item_type
        FETCH lcu_item_id
        INTO ln_item_id;
        CLOSE lcu_item_id;

        IF ln_item_id IS NULL THEN
            lc_error_details := lc_error_details || ' Item number could not be derived.';
        ELSE
          -- DERIVE primary UOM for master item
          IF lc_new_org_name IS NOT NULL THEN
            OPEN lcu_prim_uom(lc_new_org_name
                             ,ln_item_id);
            FETCH lcu_prim_uom
            INTO lc_primary_uom;
            CLOSE lcu_prim_uom;

            IF lc_primary_uom IS NULL THEN
              lc_error_details := lc_error_details || 'Primary UOM code for the master item could not be derived';
            END IF;

          END IF;
        END IF;

   --   END IF;

        lc_errloc := 'Derive new GL account';
        lc_new_glacct := XXCSR_COMMON_PKG.get_lookup_meaning(p_instance_name => p_instance_name
                                                          ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                          ,p_sirf_lookup   => 'XXCSR_CNV_GL_ACCOUNT'
                                                          ,p_old_value     => lr_po_lines_data.gl_account
                                                          );

        lc_new_cat := XXCSR_COMMON_PKG.get_lookup_meaning(p_instance_name => p_instance_name
                                                          ,p_csr_lookup    => 'XXCSR_CNV_PO_CATEGORY'
                                                          ,p_sirf_lookup   => 'XXCSR_CNV_PO_CATEGORY'
                                                          ,p_old_value     => lc_new_glacct
                                                          );

        IF lc_new_cat IS NULL THEN
          lc_new_cat := 'MISCELLANEOUS.MISCELLANEOUS';
        END IF;

        IF lr_po_lines_data.inventory_item_flag = 'N' THEN

          UPDATE xxcsr_po_distributions_int
          SET destination_type_code = 'EXPENSE'
          WHERE po_number = lr_po_lines_data.po_number
          AND po_line_number = lr_po_lines_data.line_num
          AND shipment_num = lr_po_lines_data.shipment_num
          AND source_instance = p_instance_name;

        ELSE

          UPDATE xxcsr_po_distributions_int
          SET destination_type_code = 'INVENTORY'
          WHERE po_number = lr_po_lines_data.po_number
          AND po_line_number = lr_po_lines_data.line_num
          AND shipment_num = lr_po_lines_data.shipment_num
          AND source_instance = p_instance_name;

        END IF;

        IF lc_error_details IS NULL THEN

            --Insert purchase order lines data in interface table
            lc_errloc := 'Insert purchase order lines data in interface table';
            FND_FILE.PUT_LINE(FND_FILE.LOG,'Inserting in lines');
            INSERT INTO po_lines_interface
            (interface_line_id
            ,interface_header_id
            ,action
            ,process_code
            ,document_num
            ,line_num
            ,organization_id
            ,shipment_num
            ,shipment_type
            ,contract_num
            ,line_type
            ,item_id
            ,category
            ,item_description
            ,vendor_product_num
            ,unit_of_measure
            ,quantity
            ,unit_price
            ,list_price_per_unit
            ,allow_price_override_flag
            ,negotiated_by_preparer_flag
            ,note_to_vendor
            ,taxable_flag
           -- ,tax_name
            ,type_1099
            ,capital_expense_flag
            ,price_type
            ,closed_code
            ,closed_reason
            ,closed_date
            ,closed_by
            ,over_tolerance_error_flag
            ,line_attribute_category_lines
          --  ,line_attribute1
          --  ,line_attribute2
          --  ,line_attribute3
          --  ,line_attribute4
          --  ,line_attribute5
          --  ,line_attribute6
          --  ,line_attribute7
          --  ,line_attribute8
          --  ,line_attribute9
            --,global_attribute1
           -- ,tax_code_id
            ,secondary_unit_of_measure
            ,secondary_quantity
            ,inspection_required_flag
            ,receipt_required_flag
            ,invoice_close_tolerance
            ,receive_close_tolerance
            ,need_by_date
            ,promised_date
            ,consigned_flag
            ,base_unit_price
            ,last_update_date
            ,last_updated_by
            ,creation_date
            ,created_by
            ,ship_to_location_id
            ,ship_to_organization_code
            )
            VALUES
            (po_lines_interface_s.nextval
            ,ln_po_int_header_id
            ,'ORIGINAL'
            ,'PENDING'
            ,lr_po_lines_data.po_number
            ,lr_po_lines_data.line_num
            ,ln_org_id
            ,lr_po_lines_data.shipment_num
            ,lr_po_lines_data.shipment_type
            ,lr_po_lines_data.contract_num
            ,lr_po_lines_data.line_type
            ,ln_item_id
            ,lc_new_cat
            ,lr_po_lines_data.item_description
            ,lr_po_lines_data.vendor_product_num
            ,lc_primary_uom --lr_po_lines_data.unit_of_measure
            ,lr_po_lines_data.quantity
            ,lr_po_lines_data.unit_price
            ,lr_po_lines_data.list_price_per_unit
            ,lr_po_lines_data.allow_price_override_flag
            ,lr_po_lines_data.negotiated_by_preparer_flag
            ,lr_po_lines_data.note_to_vendor
            ,lr_po_lines_data.taxable_flag
          --  ,lr_po_lines_data.tax_name
            ,lr_po_lines_data.type_1099
            ,lr_po_lines_data.capital_expense_flag
            ,lr_po_lines_data.price_type
            ,lr_po_lines_data.closed_code
            ,lr_po_lines_data.closed_reason
            ,lr_po_lines_data.closed_date
            ,lr_po_lines_data.closed_by
            ,lr_po_lines_data.over_tolerance_error_flag
            ,lr_po_lines_data.line_attribute_category_lines
           -- ,lr_po_lines_data.att1_item_number
           -- ,lr_po_lines_data.att2_manufacturer
           -- ,lr_po_lines_data.att3_manufacturer_part_no
           -- ,lr_po_lines_data.att4_vendor_name
           -- ,lr_po_lines_data.att5_last_po_price
           -- ,lr_po_lines_data.att6_last_transaction_qty
           -- ,lr_po_lines_data.att7_last_vendor_name
           -- ,lr_po_lines_data.att8_item_id
           -- ,lr_po_lines_data.att9_vendor_name
            --,lr_po_lines_data.fisc_classification_code
           -- ,ln_tax_code_id         commented as per R04
            ,lr_po_lines_data.secondary_unit_of_measure
            ,lr_po_lines_data.secondary_quantity
            ,lr_po_lines_data.inspection_required_flag
            ,lr_po_lines_data.receipt_required_flag
            ,lr_po_lines_data.invoice_close_tolerance
            ,lr_po_lines_data.receive_close_tolerance
            ,lr_po_lines_data.need_by_date
            ,lr_po_lines_data.promised_date
            ,lr_po_lines_data.consigned_flag
            ,lr_po_lines_data.base_unit_price
            ,SYSDATE
            ,fnd_global.user_id
            ,SYSDATE
            ,fnd_global.user_id
            ,ln_ship_loc_id
            ,lc_new_org_name
            );

            UPDATE xxcsr_po_lines_int
            SET    record_status = 'C'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = NULL
            WHERE rowid          = lr_po_lines_data.rowid;

            COMMIT;

            ln_lines_cnt := ln_lines_cnt + 1;

        ELSE
            UPDATE xxcsr_po_lines_int
            SET    record_status = 'E'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = lc_error_details
            WHERE  rowid         = lr_po_lines_data.rowid;

           IF ln_po_int_header_id IS NOT NULL THEN
             UPDATE xxcsr_po_headers_int
             SET record_status = 'E'
                ,error_message = error_message||'error in lines record'
             WHERE  po_number = lr_po_lines_data.po_number
             AND source_instance = p_instance_name;

             DELETE FROM po_headers_interface
             WHERE interface_header_id = ln_po_int_header_id;

             ln_header_cnt := ln_header_cnt - 1;

          END IF;

        END IF; --lc_error_details IS NULL

        COMMIT;
    END LOOP;
    --COMMIT;


    ln_dist_cnt := 0;

    --Process Purchase Order distributions data
    lc_errloc := 'Process purchase order distributions data';
    FOR lr_po_dist_data IN lcu_po_dist_data (p_rec_status)
    LOOP

        ln_org_id                   := NULL;
        ln_sob_id                   := NULL;
        ln_location_id              := NULL;
        lc_inv_org_code             := NULL;
        ln_person_org_id            := NULL;
        ln_person_id                := NULL;

        lc_error_details            := NULL;
        lc_new_orgname              := NULL;
        lc_new_del                  := NULL;
        lc_new_org_name             := NULL;
        lc_new_sobname              := NULL;
        ln_po_int_line_id           := NULL;
        ln_po_int_header_id         := NULL;
        lc_business_group           := NULL;
        ln_bu_id                    := NULL;
        lc_chr_acc_seg1             := NULL;
        lc_chr_acc_seg2             := NULL;
        lc_chr_acc_seg3             := NULL;
        lc_chr_acc_seg4             := NULL;
        lc_chr_acc_seg5             := NULL;
        lc_chr_acc_seg6             := NULL;
        lc_chr_acc_seg7             := NULL;
        lc_bud_acc_seg1             := NULL;
        lc_bud_acc_seg2             := NULL;
        lc_bud_acc_seg3             := NULL;
        lc_bud_acc_seg4             := NULL;
        lc_bud_acc_seg5             := NULL;
        lc_bud_acc_seg6             := NULL;
        lc_bud_acc_seg7             := NULL;
        lc_accr_acc_seg1            := NULL;
        lc_accr_acc_seg2            := NULL;
        lc_accr_acc_seg3            := NULL;
        lc_accr_acc_seg4            := NULL;
        lc_accr_acc_seg5            := NULL;
        lc_accr_acc_seg6            := NULL;
        lc_accr_acc_seg7            := NULL;
        lc_var_acc_seg1             := NULL;
        lc_var_acc_seg2             := NULL;
        lc_var_acc_seg3             := NULL;
        lc_var_acc_seg4             := NULL;
        lc_var_acc_seg5             := NULL;
        lc_var_acc_seg6             := NULL;
        lc_var_acc_seg7             := NULL;
        ln_charge_acct_id           := NULL;
        ln_budget_acct_id           := NULL;
        ln_accrual_acct_id          := NULL;
        ln_variance_acct_id         := NULL;
        lc_seg3_value               := NULL;
        lc_seg5_value               := NULL;

        --Derive interface header id
        lc_errloc := 'Derive interface header id';
        OPEN  lcu_get_int_header_id (lr_po_dist_data.po_number);
        FETCH lcu_get_int_header_id
        INTO  ln_po_int_header_id;
        CLOSE lcu_get_int_header_id;

        IF ln_po_int_header_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive interface header id. Header record may have error. ';
        END IF;

        --Derive interface line id
        lc_errloc := 'Derive interface line id';
        OPEN  lcu_get_int_line_id (lr_po_dist_data.po_number
                                  ,lr_po_dist_data.po_line_number
                                  ,lr_po_dist_data.shipment_num);
        FETCH lcu_get_int_line_id
        INTO  ln_po_int_line_id;
        CLOSE lcu_get_int_line_id;

        IF ln_po_int_line_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive interface line id. Header/Line record may have error. ';
        END IF;

        lc_errloc   := 'Operating unit related derivations';
        lc_new_orgname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_OPERATING_UNIT'
                                                              ,'XXSIRF_CNV_OPERATING_UNIT'
                                                              ,lr_po_dist_data.org_name
                                                              );

        IF lc_new_orgname IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive new operating unit for '||lr_po_dist_data.org_name||'. ';
        ELSE
          ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lc_new_orgname);
          IF ln_org_id IS NULL THEN
            lc_error_details := lc_error_details||'Could not derive org id for new operating unit '||lc_new_orgname||'. ';
          END IF;
        END IF;

        --Derive sob id

        lc_new_sobname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_SOB_NAME'
                                                              ,'XXSIRF_CNV_SOB_NAME'
                                                              ,lr_po_dist_data.set_of_books
                                                              );

        IF lc_new_sobname IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive new sob for '||lr_po_dist_data.set_of_books||' in XXCSR_CNV_SOB_NAME. ';
        ELSE

          lc_errloc := 'Derive sob id';
          OPEN  lcu_get_sob_id (lc_new_sobname);
          FETCH lcu_get_sob_id
          INTO  ln_sob_id;
          CLOSE lcu_get_sob_id;

          IF ln_sob_id IS NULL THEN
              lc_error_details := lc_error_details || ' Invalid sets of books name- '||lc_new_sobname;
          END IF;

        END IF;

        IF lr_po_dist_data.deliver_to_location IS NOT NULL THEN

          lc_new_del := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                                ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                                ,lr_po_dist_data.deliver_to_location
                                                                );

          IF lc_new_del IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new deliver to location for '||lr_po_dist_data.deliver_to_location||' in XXCSR_CNV_DELIVER_LOCATIONS. ';

          ELSE
            --Derive deliver to location id
            lc_errloc := 'Derive deliver to location id';
            OPEN  lcu_location_id (lc_new_del);
            FETCH lcu_location_id
            INTO  ln_location_id;
            CLOSE lcu_location_id;

            IF ln_location_id IS NULL THEN
                lc_error_details := lc_error_details || ' Invalid deliver to location code.';
            END IF;

          END IF;

        END IF;

        IF lr_po_dist_data.destination_organization IS NOT NULL THEN

          lc_new_org_name := XXCSR_COMMON_PKG.get_organization_code (p_instance_name
                                                                    ,lr_po_dist_data.destination_organization);

          IF lc_new_org_name IS NULL THEN
            lc_error_details := lc_error_details||'Mapping for organization '||lr_po_dist_data.destination_organization||' not found in the table XXCSR_CNV_ORG_MAPPINGS. ';
          END IF;

        END IF;


        --Derive deliver to person id
        IF  lr_po_dist_data.delv_per_fname IS NOT NULL THEN
          lc_business_group := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                   ,'XXCSR_CNV_HR_BG'
                                                                   ,'XXSIRF_CNV_HR_BG'
                                                                   ,lr_po_dist_data.delv_per_org_name
                                                                   );

          IF lc_business_group IS NULL THEN
            lc_error_details := lc_error_details||'Mapping for Business Group '||lr_po_dist_data.delv_per_org_name||' could not be found. ';
          ELSE
            ln_bu_id := XXCSR_COMMON_PKG.get_business_group_id(lc_business_group);
            IF ln_bu_id IS NULL THEN
              lc_error_details:= lc_error_details||' Business Group ID for '||lc_business_group||' could not be derived. ';
              --Derive deliver to person id
              lc_errloc := 'Derive deliver to person id';
              ln_person_id := XXCSR_COMMON_PKG.get_person_id (p_first_name        => lr_po_dist_data.delv_per_fname
                                                            ,p_last_name         => lr_po_dist_data.delv_per_lname
                                                            ,p_date_of_birth     => lr_po_dist_data.delv_per_dob
                                                            ,p_business_group_id => ln_bu_id
                                                            );
              IF ln_person_id IS NULL THEN
                  lc_error_details := lc_error_details || ' Invalid agent (employee) details.';
              END IF;

            END IF;
          END IF;

        END IF;

        --BUDGET ACCOUNT
        --Derive budget account segment1 from lookup
        lc_errloc := 'Derive budget account segment1 from lookup';

      IF  (lr_po_dist_data.bud_acc_seg1 IS NOT NULL OR lr_po_dist_data.bud_acc_seg2 IS NOT NULL OR
          lr_po_dist_data.bud_acc_seg3 IS NOT NULL OR lr_po_dist_data.bud_acc_seg4 IS NOT NULL OR
          lr_po_dist_data.bud_acc_seg5 IS NOT NULL OR lr_po_dist_data.bud_acc_seg6 IS NOT NULL OR
          lr_po_dist_data.bud_acc_seg7 IS NOT NULL) THEN

        lc_bud_acc_seg1 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_COMPANY'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_COMPANY'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg1
                                                               );


        --Derive budget account segment2 from lookup
        lc_errloc := 'Derive budget account segment2 from lookup';
        lc_bud_acc_seg2 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_ACCOUNT'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg2
                                                               );

        --Derive budget account segment3 from lookup
        lc_errloc := 'Derive budget account segment3 from lookup';
     /*   lc_bud_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg4
                                                               ); */

        lc_seg3_value := NULL;

        IF UPPER(lr_po_dist_data.bud_acc_seg4) = LOWER(lr_po_dist_data.bud_acc_seg4) THEN
          lc_seg3_value := lr_po_dist_data.bud_acc_seg3;
        ELSE
          lc_seg3_value := lr_po_dist_data.bud_acc_seg4;
        END IF;

        lc_bud_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                               ,p_old_value     => lc_seg3_value
                                                               );


        --Derive budget account segment4 from lookup
        lc_errloc := 'Derive budget account segment4 from lookup';
        lc_bud_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg4
                                                               );

        --Derive budget account segment5 from lookup
        lc_errloc := 'Derive budget account segment5 from lookup';
      /*  lc_bud_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg5
                                                               ); */

       lc_seg5_value := NULL;

        /* Segments 5 and 6 in the legacy instance both map onto Segment 5 in the target instance.
        When mapping the data, take whichever one of the legacy instance values
        in non-zero and map using lookup XXCSR_CNV_GL_PRODPROJ.
        If both legacy values are zero, use the default of '000000' for Segment 5 of the target GL Account.  */

        IF UPPER(lr_po_dist_data.bud_acc_seg5) = LOWER(lr_po_dist_data.bud_acc_seg5) THEN -- 5 numeric
          IF TO_NUMBER(lr_po_dist_data.bud_acc_seg5) = 0 THEN -- 5 is zero
            IF UPPER(lr_po_dist_data.bud_acc_seg6) != LOWER(lr_po_dist_data.bud_acc_seg6) THEN
              lc_seg5_value := lr_po_dist_data.bud_acc_seg6;
            ELSIF TO_NUMBER(lr_po_dist_data.bud_acc_seg6) = 0 THEN -- 6 is zero
              lc_seg5_value := '000000';
            ELSE
              lc_seg5_value := lr_po_dist_data.bud_acc_seg6;
            END IF;
          ELSE
            IF UPPER(lr_po_dist_data.bud_acc_seg6) != LOWER(lr_po_dist_data.bud_acc_seg6) THEN
              lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.bud_acc_seg5||') and segment6('||lr_po_dist_data.bud_acc_seg6||') cannot be non zero';
            ELSIF TO_NUMBER(lr_po_dist_data.bud_acc_seg6) = 0 THEN -- 6 is zero
              lc_seg5_value := lr_po_dist_data.bud_acc_seg5;
            ELSE
              lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.bud_acc_seg5||') and segment6('||lr_po_dist_data.bud_acc_seg6||') cannot be non zero';
            END IF;
          END IF;

        ELSE -- 5 non numeric
          IF UPPER(lr_po_dist_data.bud_acc_seg6) != LOWER(lr_po_dist_data.bud_acc_seg6) THEN
            lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.bud_acc_seg5||') and segment6('||lr_po_dist_data.bud_acc_seg6||') cannot be non zero';
          ELSIF TO_NUMBER(lr_po_dist_data.bud_acc_seg6) = 0 THEN -- 6 is zero
            lc_seg5_value := lr_po_dist_data.bud_acc_seg5;
          ELSE
            lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.bud_acc_seg5||') and segment6('||lr_po_dist_data.bud_acc_seg6||') cannot be non zero';
          END IF;
        END IF;

        IF lc_seg5_value IS NOT NULL AND lc_seg5_value != '000000' THEN

          lc_bud_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                 ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                                 ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                                 ,p_old_value     => lc_seg5_value
                                                                 );

        ELSIF lc_seg5_value IS NOT NULL AND lc_seg5_value = '000000' THEN
          lc_bud_acc_seg5 := lc_seg5_value;
        END IF;

        --Derive budget account segment6 from lookup
        lc_errloc := 'Derive budget account segment6 from lookup';
   /*     lc_bud_acc_seg6 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_INTERCO'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_INTERCO'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg6
                                                               ); */

        lc_bud_acc_seg6 := '000';

        --Derive budget account segment7 from lookup
        lc_errloc := 'Derive budget account segment7 from lookup';
     /*   lc_bud_acc_seg7 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_BUD_ACC7_LKUP'
                                                               ,p_sirf_lookup   => 'XXSIRF_BUD_ACC7_LKUP'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg7
                                                               ); */

        lc_bud_acc_seg7 := '00000';
      END IF;

      IF (lr_po_dist_data.accr_acc_seg1 IS NOT NULL OR lr_po_dist_data.accr_acc_seg2 IS NOT NULL OR
          lr_po_dist_data.accr_acc_seg3 IS NOT NULL OR lr_po_dist_data.accr_acc_seg4 IS NOT NULL OR
          lr_po_dist_data.accr_acc_seg5 IS NOT NULL OR lr_po_dist_data.accr_acc_seg6 IS NOT NULL OR
          lr_po_dist_data.accr_acc_seg7 IS NOT NULL) THEN
        --ACCRUAL ACCOUNT
        --Derive accrual account segment1 from lookup
        lc_errloc := 'Derive accrual account segment1 from lookup';
        lc_accr_acc_seg1 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_COMPANY'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_COMPANY'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg1
                                                               );

        -- Change by manishar 12/04/10 as per mdt v1.26
        -- Deriving Attribute15 of the corresponding Suppliers Address
        lc_errloc := 'Derive accrual account segment2 from lookup';

        lc_accr_acc_seg2 := NULL;
        OPEN  lcu_supsite_attr15(lr_po_dist_data.po_number
                                ,lr_po_dist_data.po_line_number
                                ,lr_po_dist_data.shipment_num);
        FETCH lcu_supsite_attr15 INTO lc_accr_acc_seg2;
        CLOSE lcu_supsite_attr15;

        --Derive accrual account segment2 from lookup
        IF lc_accr_acc_seg2 IS NULL THEN

          lc_accr_acc_seg2 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                 ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                                 ,p_sirf_lookup   => 'XXSIRF_CNV_GL_ACCOUNT'
                                                                 ,p_old_value     => lr_po_dist_data.accr_acc_seg2
                                                                 );
        END IF;

        --Derive accrual account segment3 from lookup
        lc_errloc := 'Derive accrual account segment3 from lookup';
        lc_accr_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg3
                                                               );

        --Derive accrual account segment4 from lookup
        lc_errloc := 'Derive accrual account segment4 from lookup';
       /*  lc_accr_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg4
                                                               ); */

        --Derive budget account segment4 from lookup
        lc_errloc := 'Derive budget account segment4 from lookup';
        lc_accr_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg4
                                                               );

        --Derive accrual account segment5 from lookup
        lc_errloc := 'Derive accrual account segment5 from lookup';
        lc_accr_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg5
                                                               );

        --Derive accrual account segment6 from lookup
        lc_errloc := 'Derive accrual account segment6 from lookup';
       /* lc_accr_acc_seg6 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_INTERCO'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_INTERCO'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg6
                                                               ); */

        lc_accr_acc_seg6 := '000';

        --Derive accrual account segment7 from lookup
        lc_errloc := 'Derive accrual account segment7 from lookup';
       /* lc_accr_acc_seg7 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_ACCR_ACC7_LKUP'
                                                               ,p_sirf_lookup   => 'XXSIRF_ACCR_ACC7_LKUP'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg7
                                                               ); */
        lc_accr_acc_seg7 := '00000';

      END IF;

      IF (lr_po_dist_data.var_acc_seg1 IS NOT NULL OR lr_po_dist_data.var_acc_seg2 IS NOT NULL OR
         lr_po_dist_data.var_acc_seg3 IS NOT NULL OR lr_po_dist_data.var_acc_seg4 IS NOT NULL OR
         lr_po_dist_data.var_acc_seg5 IS NOT NULL OR lr_po_dist_data.var_acc_seg6 IS NOT NULL OR
         lr_po_dist_data.var_acc_seg7 IS NOT NULL) THEN

        --VARIANCE ACCOUNT
        --Derive variance account segment1 from lookup
        lc_errloc := 'Derive variance account segment1 from lookup';
        lc_var_acc_seg1 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_COMPANY'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_COMPANY'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg1
                                                               );

        --Derive variance account segment2 from lookup
        lc_errloc := 'Derive variance account segment2 from lookup';
        lc_var_acc_seg2 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_ACCOUNT'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg2
                                                               );

        --Derive variance account segment3 from lookup
        lc_errloc := 'Derive variance account segment3 from lookup';
        lc_var_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg4
                                                               );

        --Derive variance account segment4 from lookup
        lc_errloc := 'Derive variance account segment4 from lookup';
      /*  lc_var_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg4
                                                               ); */

        lc_seg3_value := NULL;

        IF UPPER(lr_po_dist_data.var_acc_seg4) = LOWER(lr_po_dist_data.var_acc_seg4) THEN
          lc_seg3_value := lr_po_dist_data.var_acc_seg3;
        ELSE
          lc_seg3_value := lr_po_dist_data.var_acc_seg4;
        END IF;

        lc_var_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                               ,p_old_value     => lc_seg3_value
                                                               );


        --Derive budget account segment4 from lookup
        lc_errloc := 'Derive budget account segment4 from lookup';
        lc_var_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg4
                                                               );


        --Derive variance account segment5 from lookup
        lc_errloc := 'Derive variance account segment5 from lookup';
      /*  lc_var_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg5
                                                               ); */

       lc_seg5_value := NULL;

        /* Segments 5 and 6 in the legacy instance both map onto Segment 5 in the target instance.
        When mapping the data, take whichever one of the legacy instance values
        in non-zero and map using lookup XXCSR_CNV_GL_PRODPROJ.
        If both legacy values are zero, use the default of '000000' for Segment 5 of the target GL Account.  */

        IF UPPER(lr_po_dist_data.var_acc_seg5) = LOWER(lr_po_dist_data.var_acc_seg5) THEN -- 5 numeric
          IF TO_NUMBER(lr_po_dist_data.var_acc_seg5) = 0 THEN -- 5 is zero
            IF UPPER(lr_po_dist_data.var_acc_seg6) != LOWER(lr_po_dist_data.var_acc_seg6) THEN
              lc_seg5_value := lr_po_dist_data.var_acc_seg6;
            ELSIF TO_NUMBER(lr_po_dist_data.var_acc_seg6) = 0 THEN -- 6 is zero
              lc_seg5_value := '000000';
            ELSE
              lc_seg5_value := lr_po_dist_data.var_acc_seg6;
            END IF;
          ELSE
            IF UPPER(lr_po_dist_data.var_acc_seg6) != LOWER(lr_po_dist_data.var_acc_seg6) THEN
              lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.var_acc_seg5||') and segment6('||lr_po_dist_data.var_acc_seg6||') cannot be non zero';
            ELSIF TO_NUMBER(lr_po_dist_data.var_acc_seg6) = 0 THEN -- 6 is zero
              lc_seg5_value := lr_po_dist_data.var_acc_seg5;
            ELSE
              lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.var_acc_seg5||') and segment6('||lr_po_dist_data.var_acc_seg6||') cannot be non zero';
            END IF;
          END IF;

        ELSE -- 5 non numeric
          IF UPPER(lr_po_dist_data.var_acc_seg6) != LOWER(lr_po_dist_data.var_acc_seg6) THEN
            lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.var_acc_seg5||') and segment6('||lr_po_dist_data.var_acc_seg6||') cannot be non zero';
          ELSIF TO_NUMBER(lr_po_dist_data.var_acc_seg6) = 0 THEN -- 6 is zero
            lc_seg5_value := lr_po_dist_data.var_acc_seg5;
          ELSE
            lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.var_acc_seg5||') and segment6('||lr_po_dist_data.var_acc_seg6||') cannot be non zero';
          END IF;
        END IF;

        IF lc_seg5_value IS NOT NULL AND lc_seg5_value != '000000' THEN

          lc_var_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                 ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                                 ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                                 ,p_old_value     => lc_seg5_value
                                                                 );

        ELSIF lc_seg5_value IS NOT NULL AND lc_seg5_value = '000000' THEN
          lc_var_acc_seg5 := lc_seg5_value;
        END IF;

        --Derive variance account segment6 from lookup
        lc_errloc := 'Derive variance account segment6 from lookup';
      /*  lc_var_acc_seg6 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_INTERCO'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_INTERCO'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg6
                                                               ); */

       lc_var_acc_seg6 := '000';


        --Derive variance account segment7 from lookup
        lc_errloc := 'Derive variance account segment7 from lookup';
      /*  lc_var_acc_seg7 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_VAR_ACC7_LKUP'
                                                               ,p_sirf_lookup   => 'XXSIRF_VAR_ACC7_LKUP'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg7
                                                               ); */


        lc_var_acc_seg7 := '00000';

      END IF;
/*
        --Derive charge account id
        lc_errloc := 'Derive charge account id';
        OPEN  lcu_account_id (lc_chr_acc_seg1
                             ,lc_chr_acc_seg2
                             ,lc_chr_acc_seg3
                             ,lc_chr_acc_seg4
                             ,lc_chr_acc_seg5
                             ,lc_chr_acc_seg6
                             ,lc_chr_acc_seg7);
        FETCH lcu_account_id
        INTO  ln_charge_acct_id;
        CLOSE lcu_account_id;

        IF ln_charge_acct_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive charge of accounts id :-'||lc_chr_acc_seg1||'-'||lc_chr_acc_seg2||'-'||lc_chr_acc_seg3||'-'||lc_chr_acc_seg4||'-'||lc_chr_acc_seg5||'-'||lc_chr_acc_seg6||'-'||lc_chr_acc_seg7;
        END IF;
*/

        lc_line_ship_loc := NULL;
        lc_new_ship      := NULL;
        ln_ship_loc_id   := NULL;
        lc_ship_org      := NULL;

        OPEN lcu_ship_po_line(lr_po_dist_data.po_number
                              ,lr_po_dist_data.shipment_num
                              ,lr_po_dist_data.po_line_number);
        FETCH lcu_ship_po_line
        INTO lc_line_ship_loc;
        CLOSE lcu_ship_po_line;

        lc_new_ship := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                                ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                                ,lc_line_ship_loc
                                                                );

        OPEN lcu_hr_loc_org(lc_new_ship);
        FETCH lcu_hr_loc_org
        INTO ln_ship_loc_id
            ,lc_ship_org;
        CLOSE lcu_hr_loc_org;

        IF lc_ship_org = 'CSD' THEN
          OPEN lcu_charge_account_id(ln_ship_loc_id,'UK_MAIN');
          FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
          CLOSE lcu_charge_account_id;
          IF ln_charge_acct_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
          END IF;
        ELSIF lc_ship_org = 'CSI' THEN
          OPEN lcu_charge_account_id(ln_ship_loc_id,'CSI_MAIN');
          FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
          CLOSE lcu_charge_account_id;
          IF ln_charge_acct_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
          END IF;
        ELSIF lc_ship_org = 'CSL' THEN
          OPEN lcu_charge_account_id(ln_ship_loc_id,'CSL_MAIN');
          FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
          CLOSE lcu_charge_account_id;
          IF ln_charge_acct_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
          END IF;
        ELSIF lc_ship_org = 'CDC' THEN
          OPEN lcu_charge_account_id(ln_ship_loc_id,'AK1_COM');
          FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
          CLOSE lcu_charge_account_id;
          IF ln_charge_acct_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
          END IF;
        ELSE
          OPEN lcu_charge_account_id(ln_ship_loc_id,'RM_COM');
          FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
          CLOSE lcu_charge_account_id;
          IF ln_charge_acct_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
          END IF;
        END IF;

        --Derive budget account id
        lc_errloc := 'Derive budget account id';
        OPEN  lcu_account_id (lc_bud_acc_seg1
                             ,lc_bud_acc_seg2
                             ,lc_bud_acc_seg3
                             ,lc_bud_acc_seg4
                             ,lc_bud_acc_seg5
                             ,lc_bud_acc_seg6
                             ,lc_bud_acc_seg7);
        FETCH lcu_account_id
        INTO  ln_budget_acct_id;
        CLOSE lcu_account_id;

        --Derive accrual account id
        lc_errloc := 'Derive accrual account id';
        OPEN  lcu_account_id (lc_accr_acc_seg1
                             ,lc_accr_acc_seg2
                             ,lc_accr_acc_seg3
                             ,lc_accr_acc_seg4
                             ,lc_accr_acc_seg5
                             ,lc_accr_acc_seg6
                             ,lc_accr_acc_seg7);
        FETCH lcu_account_id
        INTO  ln_accrual_acct_id;
        CLOSE lcu_account_id;

        --Derive variance account id
        lc_errloc := 'Derive variance account id';
        OPEN  lcu_account_id (lc_var_acc_seg1
                             ,lc_var_acc_seg2
                             ,lc_var_acc_seg3
                             ,lc_var_acc_seg4
                             ,lc_var_acc_seg5
                             ,lc_var_acc_seg6
                             ,lc_var_acc_seg7);
        FETCH lcu_account_id
        INTO  ln_variance_acct_id;
        CLOSE lcu_account_id;

        IF lc_error_details IS NULL THEN

            --Insert purchase order distrbutions data in interface table
            lc_errloc := 'Insert purchase order distrbutions data in interface table';
            INSERT INTO po_distributions_interface
            (interface_line_id
            ,interface_header_id
            ,interface_distribution_id
            ,process_code
            ,distribution_num
            ,org_id
            ,quantity_ordered
            ,quantity_delivered
            ,quantity_billed
            ,quantity_cancelled
            ,rate_date
            ,rate
            ,deliver_to_location_id
            ,deliver_to_person_id
            ,destination_type_code
            ,destination_organization
            ,destination_context
            ,set_of_books
            ,charge_account_id
            ,budget_account_id
            ,accrual_account_id
            ,variance_account_id
            ,amount_billed
            ,accrue_on_receipt_flag
            ,accrued_flag
            ,prevent_encumbrance_flag
            ,encumbered_flag
            ,gl_cancelled_date
            --,requisition_num
            --,requisition_line_num
            --,req_distribution_num
            ,project_accounting_context
            ,recoverable_tax
            ,nonrecoverable_tax
           -- ,recovery_rate
           -- ,tax_recovery_override_flag
            ,last_update_date
            ,last_updated_by
            ,creation_date
            ,created_by
            )
            VALUES
            (ln_po_int_line_id
            ,ln_po_int_header_id
            ,po_distributions_interface_s.nextval
            ,'PENDING'
            ,lr_po_dist_data.distribution_num
            ,ln_org_id
            ,(lr_po_dist_data.quantity_ordered - lr_po_dist_data.quantity_delivered - lr_po_dist_data.quantity_cancelled)
            ,0 --lr_po_dist_data.quantity_delivered
            ,0 --lr_po_dist_data.quantity_billed
            ,0 --lr_po_dist_data.quantity_cancelled
            ,lr_po_dist_data.rate_date
            ,lr_po_dist_data.rate
            ,ln_location_id
            ,ln_person_id
            ,lr_po_dist_data.destination_type_code
            ,lc_new_org_name --lr_po_dist_data.destination_organization
            ,lr_po_dist_data.destination_context
            ,ln_sob_id
            ,ln_charge_acct_id
            ,ln_budget_acct_id
            ,ln_accrual_acct_id
            ,ln_variance_acct_id
            ,lr_po_dist_data.amount_billed
            ,'Y' -- lr_po_dist_data.accrue_on_receipt_flag as per MDT version 1.23
            ,lr_po_dist_data.accrued_flag
            ,lr_po_dist_data.prevent_encumbrance_flag
            ,lr_po_dist_data.encumbered_flag
            ,lr_po_dist_data.gl_cancelled_date
            --,lr_po_dist_data.requisition_num
            --,lr_po_dist_data.requisition_line_num
            --,lr_po_dist_data.req_distribution_num
            ,lr_po_dist_data.project_accounting_context
            ,lr_po_dist_data.recoverable_tax
            ,lr_po_dist_data.nonrecoverable_tax
           -- ,lr_po_dist_data.recovery_rate
           -- ,lr_po_dist_data.tax_recovery_override_flag
            ,SYSDATE
            ,fnd_global.user_id
            ,SYSDATE
            ,fnd_global.user_id
            );

            UPDATE xxcsr_po_distributions_int
            SET    record_status = 'C'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = NULL
            WHERE rowid          = lr_po_dist_data.rowid;

            COMMIT;

            ln_dist_cnt := ln_dist_cnt + 1;

        ELSE
          UPDATE xxcsr_po_distributions_int
          SET    record_status = 'E'
                ,request_id    = FND_GLOBAL.conc_request_id
                ,error_message = lc_error_details
          WHERE  rowid         = lr_po_dist_data.rowid;

          IF ln_po_int_line_id IS NOT NULL THEN
             UPDATE xxcsr_po_lines_int
             SET record_status = 'E'
                ,error_message = error_message||'error in distribution record'
             WHERE  po_number = lr_po_dist_data.po_number
             AND line_num = lr_po_dist_data.po_line_number
             AND shipment_num = lr_po_dist_data.shipment_num
             AND source_instance = p_instance_name;

             DELETE FROM po_lines_interface
             WHERE interface_line_id = ln_po_int_line_id;

             ln_lines_cnt := ln_lines_cnt - 1;

           END IF;

         IF ln_po_int_header_id IS NOT NULL THEN
           UPDATE xxcsr_po_headers_int
           SET record_status = 'E'
              ,error_message = error_message||'error in distribution record'
           WHERE  po_number = lr_po_dist_data.po_number
           AND source_instance = p_instance_name;

           DELETE FROM po_headers_interface
           WHERE interface_header_id = ln_po_int_header_id;

           ln_header_cnt := ln_header_cnt - 1;

          END IF;
        END IF; --lc_error_details IS NULL

        COMMIT;
    END LOOP;
    COMMIT;

    IF ln_header_cnt > 0 THEN

        --Calling Import Standard Purchase Orders program
        lc_errloc     := 'Call Import Standard Purchase Orders program';

        FND_FILE.PUT_LINE(FND_FILE.LOG,'Submitting Import Standard Purchase Orders concurrent program.');

        ln_cur_org_id := FND_PROFILE.VALUE('ORG_ID');

        FND_FILE.PUT_LINE(FND_FILE.LOG,'ln_cur_org_id : ' || ln_cur_org_id);

        ln_request_id := FND_REQUEST.SUBMIT_REQUEST (
                                                      application => 'PO'
                                                     ,program     => 'POXPOPDOI'
                                                     ,description =>  'Import Standard Purchase Orders'
                                                     ,start_time  =>  SYSDATE
                                                     ,sub_request =>  FALSE
                                                     ,argument1   =>  NULL --Default Buyer
                                                     ,argument2   =>  'STANDARD' --Document Type
                                                     ,argument3   =>  NULL --Document SubType
                                                     ,argument4   =>  'N' --Create or Update Items
                                                     ,argument5   =>  NULL --Create Sourcing Rules
                                                     ,argument6   =>  'APPROVED' --Approval Status
                                                     ,argument7   =>  NULL --Release Generation Method
                                                     ,argument8   =>  NULL --Batch Id
                                                     ,argument9   =>  ln_cur_org_id --Operating Unit
                                                     ,argument10  =>  NULL --Global Agreement
                                                     ,argument11  =>  NULL --Enable Sourcing Level
                                                     ,argument12  =>  NULL --Sourcing Level
                                                     ,argument13  =>  NULL --Inv Org Enable
                                                     ,argument14  =>  NULL --Inventory Organization
                                                    );
        COMMIT;

        IF ln_request_id = 0 THEN
            lc_error := 'Error submitting Import Standard Purchase Orders concurrent program.';
            RAISE ex_conc_error;
        END IF;

        FND_FILE.PUT_LINE(FND_FILE.LOG,'Import Standard Purchase Orders concurrent program submitted. Request_id : ' || ln_request_id);

        --Wait for the request to complete
        lb_wait := FND_CONCURRENT.WAIT_FOR_REQUEST
               (
                 request_id  => ln_request_id
                ,interval    => 2
                ,phase       => lc_phase
                ,status      => lc_status
                ,dev_phase   => lc_devpha
                ,dev_status  => lc_devsta
                ,message     => lc_mesg
               );

    END IF; --ln_header_cnt > 0

    x_header_cnt := ln_header_cnt;
    x_lines_cnt  := ln_lines_cnt;
    x_dist_cnt   := ln_dist_cnt;
    x_req_id     := ln_request_id;

-- Contracts import

    FOR lr_po_header_contr IN lcu_po_header_contr (p_rec_status)
    LOOP

        ln_org_id                   := NULL;
        ln_agent_id                 := NULL;
        ln_bu_id                    := NULL;
        lc_currency_code            := NULL;
        ln_vendor_id                := NULL;
        lc_payment_terms            := NULL;
        lc_freight_terms            := NULL;

        lc_error_details            := NULL;
        lc_new_orgname              := NULL;
        lc_business_group           := NULL;
        ln_bill_loc_id              := NULL;
        ln_ship_loc_id              := NULL;
        lc_inv_po_chk               := NULL;
        lc_func_curr                := NULL;
        lc_rate_type                := NULL;
        ln_rate                     := NULL;
        ld_rate_date                := NULL;
        lc_new_ship                 := NULL;
        lc_new_bill                 := NULL;
        ln_vendor_site_id           := NULL;
        ln_vendor_contact_id        := NULL;
        ln_term_id                  := NULL;
        lc_po_exists                := NULL;

        -- check if PO already exists

        OPEN lcu_check_po_exists(lr_po_header_contr.po_number);
        FETCH lcu_check_po_exists
        INTO lc_po_exists;
        CLOSE lcu_check_po_exists;

        IF lc_po_exists IS NOT NULL THEN
          lc_error_details := lc_error_details||'PO already exists';
        END IF;

        lc_errloc   := 'Operating unit related derivations';
        lc_new_orgname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_OPERATING_UNIT'
                                                              ,'XXSIRF_CNV_OPERATING_UNIT'
                                                              ,lr_po_header_contr.operating_unit
                                                              );

        IF lc_new_orgname IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive new operating unit for '||lr_po_header_contr.operating_unit||'. ';
        ELSE
          ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lc_new_orgname);
          IF ln_org_id IS NULL THEN
            lc_error_details := lc_error_details||'Could not derive org id for new operating unit '||lc_new_orgname||'. ';
          ELSE
            -- Compare functional and document currency
            OPEN lcu_func_curr(ln_org_id);
            FETCH lcu_func_curr
            INTO lc_func_curr;
            CLOSE lcu_func_curr;

            IF lc_func_curr = lr_po_header_contr.currency_code THEN
              lc_rate_type := NULL;
              ln_rate      := NULL;
              ld_rate_date := NULL;
            ELSE
              lc_rate_type := lr_po_header_contr.rate_type;
              ln_rate      := lr_po_header_contr.rate;
              ld_rate_date := lr_po_header_contr.rate_date;

            END IF;
          END IF;
        END IF;

    /*    lc_errloc := 'Checking for inventory purchase orders';
        lc_inv_po_chk := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_INV_SUPPLIERS'
                                                              ,'XXSIRF_CNV_INV_SUPPLIERS'
                                                              ,lr_po_header_contr.vendor_name
                                                              );

        IF lc_inv_po_chk IS NULL THEN
          lc_error_details := lc_error_details||'Only inventory purchase orders will be migrated';
        END IF; */


   /*   Commented by Nishka, added the above derivation
        --Derive org id
        lc_errloc := 'Derive org id';
        ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lr_po_header_contr.operating_unit);

        IF ln_org_id IS NULL THEN
            lc_error_details :=  'Invalid operating unit name.';
        END IF;   */

   /*     OPEN  lcu_bu_id (lr_po_header_contr.emp_bu_name);
        FETCH lcu_bu_id
        INTO  ln_bu_id;
        CLOSE lcu_bu_id; */

        IF lr_po_header_contr.emp_bu_name IN ( 'CSR United States','CSR United Kingdom') THEN
          lc_business_group := lr_po_header_contr.emp_bu_name;
        ELSE
          lc_business_group := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                   ,'XXCSR_CNV_HR_BG'
                                                                   ,'XXSIRF_CNV_HR_BG'
                                                                   ,lr_po_header_contr.emp_bu_name
                                                                   );
        END IF;
        IF lc_business_group IS NULL THEN
          lc_error_details := lc_error_details||'Mapping for Business Group '||lr_po_header_contr.emp_bu_name||' could not be found. ';
        ELSE
          ln_bu_id := XXCSR_COMMON_PKG.get_business_group_id(lc_business_group);
          IF ln_bu_id IS NULL THEN
            lc_error_details:= lc_error_details||' Business Group ID for '||lc_business_group||' could not be derived. ';
          ELSE
            --Derive agent id
            lc_errloc := 'Derive agent id';
            ln_agent_id := XXCSR_COMMON_PKG.get_person_id (p_first_name        => lr_po_header_contr.emp_fname
                                                          ,p_last_name         => lr_po_header_contr.emp_lname
                                                          ,p_date_of_birth     => lr_po_header_contr.emp_dob
                                                          ,p_business_group_id => ln_bu_id
                                                          );
            IF ln_agent_id IS NULL THEN
                lc_error_details := lc_error_details || ' Invalid agent (employee) details.';
            END IF;

          END IF;
        END IF;



        --Check currency code
        lc_errloc := 'Check currency code';
        OPEN  lcu_chk_currency (lr_po_header_contr.currency_code);
        FETCH lcu_chk_currency
        INTO  lc_currency_code;

        IF lcu_chk_currency%NOTFOUND THEN
            lc_error_details := lc_error_details || ' Invalid currency code.';
        END IF;

        CLOSE lcu_chk_currency;

        --Derive vendor id
        lc_errloc := 'Derive vendor id';

        IF lr_po_header_contr.vendor_name IS NOT NULL THEN
          OPEN lcu_vendor_der(lr_po_header_contr.vendor_name);
          FETCH lcu_vendor_der
          INTO  ln_vendor_id;
          CLOSE lcu_vendor_der;

          IF ln_vendor_id IS NULL THEN
              lc_error_details := lc_error_details || ' Vendor not migrated.';
          END IF;
        END IF;


        lc_errloc := 'Check payment terms';
        IF lr_po_header_contr.payment_terms IS NOT NULL THEN
        lc_payment_terms := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                ,p_csr_lookup    => 'XXCSR_CNV_SUPPLIER_PAYTERMS'
                                                                ,p_sirf_lookup   => 'XXSIRF_CNV_SUPPLIER_PAYTERMS'
                                                                ,p_old_value     => lr_po_header_contr.payment_terms
                                                                );

        IF lc_payment_terms IS NULL THEN
            lc_error_details := lc_error_details || ' Invalid payment terms.';
        ELSE
          OPEN lcu_pay_term(lc_payment_terms);
          FETCH lcu_pay_term
          INTO ln_term_id;
          CLOSE lcu_pay_term;

          IF ln_term_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive payment term id for '||lc_payment_terms;
          END IF;
        END IF;
        END IF;

        --Check freight terms
        lc_errloc := 'Check freight terms';
        IF lr_po_header_contr.freight_terms IS NOT NULL THEN
          lc_freight_terms := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                  ,p_csr_lookup    => 'XXCSR_CNV_FREIGHT_TERMS'
                                                                  ,p_sirf_lookup   => 'XXSIRF_CNV_FREIGHT_TERMS'
                                                                  ,p_old_value     => lr_po_header_contr.freight_terms
                                                                  );

          IF lc_freight_terms IS NULL THEN
              lc_error_details := lc_error_details || ' Invalid freight terms.';
          END IF;

        END IF;

        IF lr_po_header_contr.ship_to_location IS NOT NULL THEN

          lc_new_ship := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                                ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                                ,lr_po_header_contr.ship_to_location
                                                                );
          IF lc_new_ship IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new ship to location for '||lr_po_header_contr.ship_to_location||' in XXCSR_CNV_DELIVER_LOCATIONS. ';

          ELSE

            OPEN lcu_hr_loc(lc_new_ship);
            FETCH lcu_hr_loc
            INTO ln_ship_loc_id;
            CLOSE lcu_hr_loc;

            IF ln_ship_loc_id IS NULL THEN
              lc_error_details := lc_error_details || 'Ship to location id could not be derived for '||lr_po_header_contr.ship_to_location;
            END IF;
          END IF;

        END IF;

        IF lr_po_header_contr.bill_to_location IS NOT NULL THEN

          lc_new_bill := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_BILLTO_LOCATIONS'
                                                                ,'XXSIRF_CNV_BILLTO_LOCATIONS'
                                                                ,lr_po_header_contr.bill_to_location
                                                                );
          IF lc_new_bill IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new bill to location for '||lr_po_header_contr.bill_to_location||' in XXCSR_CNV_BILLTO_LOCATIONS. ';

          ELSE

            OPEN lcu_hr_loc(lc_new_bill);
            FETCH lcu_hr_loc
            INTO ln_bill_loc_id;
            CLOSE lcu_hr_loc;

            IF ln_bill_loc_id IS NULL THEN
              lc_error_details := lc_error_details || 'Bill to location id could not be derived for '||lr_po_header_contr.bill_to_location;
            END IF;

          END IF;

        END IF;


        OPEN lcu_vendor_site(lr_po_header_contr.vendor_site_code
                            ,ln_vendor_id);
        FETCH lcu_vendor_site
        INTO ln_vendor_site_id;
        CLOSE lcu_vendor_site;

        OPEN lcu_vendor_contact(ln_vendor_site_id);
        FETCH lcu_vendor_contact
        INTO ln_vendor_contact_id;
        CLOSE lcu_vendor_contact;

 /*       ln_line_item_cnt := NULL;
        ln_po_item_chk   := NULL;
        -- Check whether the po line has valid items.
        OPEN lcu_po_line_itm(lr_po_header_contr.po_number);
        FETCH lcu_po_line_itm
        INTO ln_line_item_cnt;
        CLOSE lcu_po_line_itm;

        IF ln_line_item_cnt != 0 THEN

          OPEN lcu_po_itm_chk(lr_po_header_contr.po_number);
          FETCH lcu_po_itm_chk
          INTO ln_po_item_chk;
          CLOSE lcu_po_itm_chk;

          IF ln_po_item_chk = 0 THEN
            lc_error_details := lc_error_details || 'Inventory item id for po line could not be derived';
          END IF;
        END IF; */

        IF lc_error_details IS NULL THEN

            --Insert purchase order header data in interface table
            lc_errloc := 'Insert purchase order header data in interface table';

          BEGIN
         
          ln_po_header_id := NULL;
          ln_po_header_id := PO_HEADERS_S.nextval;

          INSERT INTO po_headers_all (
              po_header_id
             ,agent_id
             ,type_lookup_code
             ,created_by
              ,creation_date
              ,last_update_date
              ,last_updated_by
              ,last_update_login
             ,request_id
             ,segment1
             ,summary_flag
             ,enabled_flag
             ,vendor_id
             ,vendor_site_id
             ,vendor_contact_id
             ,ship_to_location_id
             ,bill_to_location_id
             ,terms_id
             ,currency_code
             ,rate_type
             ,rate_date
             ,rate
             ,authorization_status
             ,revision_num
             ,approved_flag
             ,approved_date
             ,print_count
             ,confirming_order_flag
             ,comments
             ,acceptance_required_flag
             ,cancel_flag
             ,frozen_flag
             ,closed_code
            ,org_id
            ,interface_source_code
         --  ,wf_item_type
         --  ,wf_item_key
             ,xml_flag
             ,document_creation_method
             ,submit_date
             ,style_id
             ,freight_terms_lookup_code
             ,attribute_category
             ,pay_on_code
             ,global_agreement_flag
              )
              VALUES
              (
              ln_po_header_id
             ,ln_agent_id
             ,'CONTRACT'
             ,FND_GLOBAL.USER_ID
             ,SYSDATE
             ,SYSDATE
             ,FND_GLOBAL.USER_ID
             ,FND_GLOBAL.LOGIN_ID
             ,gn_conc_req_id
             ,lr_po_header_contr.po_number
             ,'N'
             ,'Y'
             ,ln_vendor_id
             ,ln_vendor_site_id
             ,ln_vendor_contact_id
             ,ln_ship_loc_id -- lr_po_header_contr.ship_to_location
             ,ln_bill_loc_id -- lr_po_header_contr.bill_to_location
             ,ln_term_id
             ,lr_po_header_contr.currency_code
             ,DECODE(lc_rate_type,'Corporate','User',lc_rate_type)
             ,ld_rate_date
             ,ln_rate
             ,DECODE(lr_po_header_contr.approval_status,'REQUIRES REAPPROVAL','INCOMPLETE',lr_po_header_contr.approval_status)
             ,0
             ,lr_po_header_contr.approved_flag
             ,lr_po_header_contr.approved_date
             --,DECODE(lr_po_header_contr.approval_status,'APPROVED',lr_po_header_contr.print_count,0)
             ,DECODE(lr_po_header_contr.approval_status,'APPROVED',1,0)
             ,lr_po_header_contr.confirming_order_flag
             ,lr_po_header_contr.comments
             ,lr_po_header_contr.acceptance_required_flag
             ,'N'
             ,lr_po_header_contr.frozen_flag
             ,lr_po_header_contr.closed_code
      ,ln_org_id
      ,'XXCSR Conversion'
     -- ,-- wf_item_type
     -- ,-- wf_item_key
      ,'N'
      ,'PDOI'
      ,SYSDATE
      , 1 -- style_id
      ,lc_freight_terms
      ,lr_po_header_contr.attribute_category
      ,lr_po_header_contr.pay_on_code
      ,'Y'
              );
             
      INSERT INTO PO_GA_ORG_ASSIGNMENTS(PO_HEADER_ID          
                                       ,ORGANIZATION_ID       
                                       ,ENABLED_FLAG          
                                       ,VENDOR_SITE_ID        
                                       ,LAST_UPDATE_DATE      
                                       ,LAST_UPDATED_BY       
                                       ,CREATION_DATE         
                                       ,CREATED_BY            
                                       ,LAST_UPDATE_LOGIN     
                                       ,PURCHASING_ORG_ID     
                                       ,ORG_ASSIGNMENT_ID                   
                                       )
                                VALUES (ln_po_header_id
                                       ,ln_org_id
                                       ,'Y'
                                       ,ln_vendor_site_id
                                       ,SYSDATE
                                       ,FND_GLOBAL.USER_ID
                                       ,SYSDATE
                                       ,FND_GLOBAL.USER_ID
                                       ,FND_GLOBAL.LOGIN_ID
                                       ,ln_org_id
                                       ,PO_GA_ORG_ASSIGNMENTS_S.NEXTVAL
                                       );
             
             
             

            UPDATE xxcsr_po_headers_int
            SET    record_status = 'C'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = NULL
            WHERE rowid = lr_po_header_contr.rowid;

            COMMIT;

          EXCEPTION
          WHEN OTHERS THEN
            lc_error_details := SQLERRM;
            UPDATE xxcsr_po_headers_int
            SET    record_status = 'E'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = lc_error_details
            WHERE  rowid = lr_po_header_contr.rowid;
          END;

           -- ln_header_cnt := ln_header_cnt + 1;

        ELSE
            UPDATE xxcsr_po_headers_int
            SET    record_status = 'E'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = lc_error_details
            WHERE  rowid = lr_po_header_contr.rowid;
        END IF; --lc_error_details IS NULL

        COMMIT;
    END LOOP;
    COMMIT;

--
EXCEPTION
  WHEN EX_CONC_ERROR THEN
    x_retcode := 2;
    x_errbuf  := lc_errmsg;
    FND_FILE.PUT_LINE(FND_FILE.LOG,lc_errmsg);

  WHEN OTHERS THEN
    x_errbuf  := SUBSTR(SQLERRM,1,200);
    x_retcode := 2;
    FND_FILE.PUT_LINE(FND_FILE.LOG,'Unexpected error occurred in import procedure at ' || lc_errloc || ' Error : ' || SQLERRM);

END import_csr;

-----=========================Import ZORAN procedure=========================
PROCEDURE import_zoran
   (p_rec_status      IN         VARCHAR2
   ,p_instance_name   IN         VARCHAR2
   ,x_errbuf          OUT NOCOPY VARCHAR2
   ,x_retcode         OUT NOCOPY NUMBER
   ,x_header_cnt      OUT NOCOPY NUMBER
   ,x_lines_cnt       OUT NOCOPY NUMBER
   ,x_dist_cnt        OUT NOCOPY NUMBER
   ,x_req_id          OUT NOCOPY NUMBER
   )
IS
--
  CURSOR lcu_po_header_data (pv_rec_status VARCHAR2)
  IS
      SELECT XHEC.rowid,XHEC.*
      FROM   xxcsr_po_headers_int XHEC
      WHERE  XHEC.record_status = pv_rec_status
      AND    XHEC.request_id = gn_conc_req_id
      AND XHEC.source_instance = p_instance_name
      AND    XHEC.document_type_code <> 'CONTRACT';

  CURSOR lcu_po_header_contr (pv_rec_status VARCHAR2)
  IS
      SELECT XHEC.rowid,XHEC.*
      FROM   xxcsr_po_headers_int XHEC
      WHERE  XHEC.record_status = pv_rec_status
      AND    XHEC.request_id = gn_conc_req_id
      AND    XHEC.source_instance = p_instance_name
      AND    XHEC.document_type_code = 'CONTRACT';

  CURSOR lcu_po_lines_data (pv_rec_status VARCHAR2)
  IS
      SELECT XHEC.rowid,XHEC.*
      FROM   xxcsr_po_lines_int XHEC
      WHERE  XHEC.record_status = pv_rec_status
      AND    XHEC.request_id = gn_conc_req_id
      AND XHEC.source_instance = p_instance_name;

  CURSOR lcu_po_dist_data (pv_rec_status VARCHAR2)
  IS
      SELECT XHEC.rowid,XHEC.*
      FROM   xxcsr_po_distributions_int XHEC
      WHERE  XHEC.record_status = pv_rec_status
      AND    XHEC.request_id = gn_conc_req_id
      AND XHEC.source_instance = p_instance_name;

  CURSOR lcu_chk_currency (pv_currency_code VARCHAR2)
  IS
      SELECT currency_code
      FROM   fnd_currencies
      WHERE  currency_code = pv_currency_code
      AND    enabled_flag = 'Y'
      AND    SYSDATE BETWEEN NVL(START_DATE_ACTIVE, SYSDATE) AND NVL(END_DATE_ACTIVE, SYSDATE);

  CURSOR lcu_item_cat_id (pv_seg1 VARCHAR2,pv_seg2 VARCHAR2,pv_seg3 VARCHAR2,pv_seg4 VARCHAR2,pv_seg5 VARCHAR2)
  IS
      SELECT category_id
      FROM   mtl_categories
      WHERE  segment1 = pv_seg1
      AND    segment2 = pv_seg2
      AND    segment3 = pv_seg3
      AND    segment4 = pv_seg4
      AND    segment5 = pv_seg5
      AND    enabled_flag = 'Y'
      AND    SYSDATE BETWEEN NVL(START_DATE_ACTIVE, SYSDATE) AND NVL(END_DATE_ACTIVE, SYSDATE);

  CURSOR lcu_get_int_header_id (pv_po_number VARCHAR2)
  IS
      SELECT interface_header_id
      FROM   po_headers_interface
      WHERE  document_num = pv_po_number
      ORDER BY 1 DESC;

  CURSOR lcu_get_int_line_id (pv_po_number VARCHAR2
                             ,pv_po_line_number VARCHAR2
                             ,p_shipment_num    VARCHAR2)
  IS
      SELECT interface_line_id
      FROM   po_lines_interface
      WHERE  document_num = pv_po_number
      AND    line_num     = pv_po_line_number
      AND    shipment_num = p_shipment_num;

  CURSOR lcu_account_id (pv_seg1 VARCHAR2,pv_seg2 VARCHAR2,pv_seg3 VARCHAR2,pv_seg4 VARCHAR2,pv_seg5 VARCHAR2,pv_seg6 VARCHAR2,pv_seg7 VARCHAR2)
  IS
      SELECT code_combination_id
      FROM   gl_code_combinations_kfv
      WHERE  segment1 = pv_seg1
      AND    segment2 = pv_seg2
      AND    segment3 = pv_seg3
      AND    segment4 = pv_seg4
      AND    segment5 = pv_seg5
      AND    segment6 = pv_seg6
      AND    segment7 = pv_seg7;

  CURSOR lcu_charge_account_id  (p_ship_location NUMBER, p_second_inv VARCHAR2)
  IS
  SELECT GCC.code_combination_id
  FROM   gl_code_combinations gcc
        ,mtl_secondary_inventories msi
        ,hr_locations hl
  WHERE  MSI.material_account         = GCC.code_combination_id
  AND    MSI.secondary_inventory_name = p_second_inv
  AND    MSI.organization_id          = HL.inventory_organization_id
  AND    HL.location_id               = p_ship_location;

  CURSOR lcu_get_sob_id (pv_sob_name VARCHAR2)
  IS
      SELECT set_of_books_id
      FROM   gl_sets_of_books
      WHERE  name = pv_sob_name;


  --sjacob commented below
  CURSOR lcu_location_id (pv_loc_code VARCHAR2)
  IS
      SELECT location_id
      FROM   hr_locations
      WHERE  location_code = pv_loc_code;

  CURSOR lcu_item_id(p_seg1 IN VARCHAR2
                    ,p_seg2 IN VARCHAR2
                    ,p_seg3 IN VARCHAR2  -- sjacob commented as per Siva's mail
                    )
  IS
  SELECT MSIB.inventory_item_id
  FROM mtl_system_items_b MSIB
  WHERE MSIB.segment1 = p_seg1
  AND   NVL(MSIB.segment2,-99) = NVL(p_seg2,-99)
  AND   NVL(MSIB.segment3,-99) = NVL(p_seg3,-99)       -- sjacob commented as per Siva's mail
  AND   ROWNUM <= 1;

----------1)change
  CURSOR lcu_vendor_der(p_vendor IN VARCHAR2)
  IS
  SELECT vendor_id
  FROM   AP_SUPPLIERS
  WHERE  attribute15 = p_vendor;

  CURSOR lcu_hr_loc(p_location_code IN VARCHAR2)
  IS
  SELECT HLA.location_id
  FROM hr_locations_all HLA
  WHERE HLA.location_code = p_location_code;

  CURSOR lcu_hr_loc_org(p_location_code IN VARCHAR2)
  IS
  SELECT HLA.location_id
        ,OOD.organization_code
  FROM hr_locations_all HLA
      ,org_organization_definitions OOD
  WHERE HLA.location_code = p_location_code
  AND   OOD.organization_id = HLA.inventory_organization_id;


  CURSOR lcu_ship_po_line(p_po_number IN VARCHAR2
                         ,p_shipment_num IN NUMBER
                         ,p_line_num     IN NUMBER)
  IS
  SELECT ship_to_location
  FROM xxcsr_po_lines_int XPLI
  WHERE XPLI.po_number = p_po_number
  AND   XPLI.shipment_num = p_shipment_num
  AND   XPLI.line_num = p_line_num
  AND   XPLI.source_instance = p_instance_name;

  CURSOR lcu_func_curr(p_org_id IN NUMBER)
  IS
  SELECT GL.currency_code
  FROM hr_operating_units HOU
      ,gl_ledgers GL
  WHERE GL.ledger_id = HOU.set_of_books_id
  AND   HOU.organization_id = p_org_id;

  CURSOR lcu_po_line_itm(p_po_num IN VARCHAR2)
  IS
  SELECT count('x')
  FROM xxcsr_po_lines_int XPLI
  WHERE XPLI.po_number = p_po_num
  AND   XPLI.itm_seg1_item_num IS NOT NULL
  AND   XPLI.source_instance = p_instance_name;
---------2)change
  CURSOR lcu_vendor_site(p_vendor_site_code IN VARCHAR2
                        ,p_vendor_id IN NUMBER)
  IS
  SELECT POVS.vendor_site_id
  FROM   AP_SUPPLIER_SITES_ALL POVS
  WHERE  POVS.vendor_site_code = p_vendor_site_code
  AND    POVS.vendor_id = p_vendor_id;
---------3)change
  CURSOR lcu_vendor_contact(p_vendor_site_id IN NUMBER)
  IS
  SELECT PVC.vendor_contact_id
  FROM   AP_SUPPLIER_CONTACTS PVC
  WHERE  PVC.vendor_site_id = p_vendor_site_id;

  CURSOR lcu_pay_term(p_payterm IN VARCHAR2)
  IS
  SELECT APT.term_id
  FROM   ap_terms APT
  WHERE  APT.name  = p_payterm;

  CURSOR lcu_check_po_exists(p_po_number IN VARCHAR2)
  IS
  SELECT 1
  FROM po_headers_all PHA
  WHERE PHA.segment1 = p_po_number;

  CURSOR lcu_check_lines(p_po_number IN VARCHAR2)
  IS
  SELECT 1
  FROM xxcsr_po_lines_int XPLI
  WHERE XPLI.po_number = p_po_number;

  CURSOR lcu_prim_uom(p_org_code IN VARCHAR2
                     ,p_item_id IN NUMBER)
  IS
  SELECT MSIB.primary_unit_of_measure
  FROM mtl_system_items_b MSIB
      ,mtl_parameters MP
  WHERE MSIB.inventory_item_id = p_item_id
  AND   MSIB.organization_id = MP.master_organization_id
  AND   MP.organization_code = p_org_code;
--------4)change
  CURSOR lcu_supsite_attr15(p_doc_num VARCHAR2
                           ,p_lin_num VARCHAR2
                           ,p_shp_num VARCHAR2)
  IS
  SELECT PVS.attribute15
  FROM   po_headers_interface PHI
        ,po_lines_interface PLI
        ,AP_SUPPLIER_SITES_ALL PVS
  WHERE  PHI.interface_header_id = PLI.interface_header_id
  AND    PHI.vendor_id = PVS.vendor_id
  AND    PHI.vendor_id = PVS.vendor_id
  AND    PVS.org_id = PHI.org_id
  AND    PVS.vendor_site_code = PHI.vendor_site_code
  AND    PLI.document_num = p_doc_num
  AND    PLI.line_num = p_lin_num
  AND    PLI.shipment_num = p_shp_num;

    lr_po_header_data            lcu_po_header_data%ROWTYPE;
    lr_po_lines_data             lcu_po_lines_data%ROWTYPE;
    lr_po_dist_data              lcu_po_dist_data%ROWTYPE;

    lc_errmsg                    VARCHAR2(2000);
    lc_errloc                    VARCHAR2(1000);

    ln_request_id                NUMBER;
    lc_phase                     VARCHAR2(500);
    lc_status                    VARCHAR2(500);
    lc_devpha                    VARCHAR2(500);
    lc_devsta                    VARCHAR2(500);
    lc_mesg                      VARCHAR2(50);
    lb_wait                      BOOLEAN;

    ln_org_id                    NUMBER;
    ln_item_org_id               NUMBER;
    ln_agent_id                  NUMBER;
    lc_currency_code             VARCHAR2(15);
    ln_vendor_id                 NUMBER;
    lc_payment_terms             VARCHAR2(50);
    lc_freight_terms             VARCHAR2(25);
    ln_item_id                   NUMBER;
    lc_item_seg2                 VARCHAR2(40);
    lc_cat_seg1                  VARCHAR2(40);
    lc_cat_seg2                  VARCHAR2(40);
    lc_cat_seg3                  VARCHAR2(40);
    lc_cat_seg4                  VARCHAR2(40);
    lc_cat_seg5                  VARCHAR2(40);
    lc_inv_org_code              VARCHAR2(30);
    ln_cat_id                    NUMBER;
    ln_po_int_header_id          NUMBER;
    ln_po_int_line_id            NUMBER;
    ln_tax_code_id               NUMBER;
    ln_charge_acct_id            NUMBER;
    ln_budget_acct_id            NUMBER;
    ln_accrual_acct_id           NUMBER;
    ln_variance_acct_id          NUMBER;
    ln_sob_id                    NUMBER;
    ln_location_id               NUMBER;
    ln_person_org_id             NUMBER;
    ln_person_id                 NUMBER;
    ln_bu_id                     NUMBER;

    lc_chr_acc_seg1              VARCHAR2(40);
    lc_chr_acc_seg2              VARCHAR2(40);
    lc_chr_acc_seg3              VARCHAR2(40);
    lc_chr_acc_seg4              VARCHAR2(40);
    lc_chr_acc_seg5              VARCHAR2(40);
    lc_chr_acc_seg6              VARCHAR2(40);
    lc_chr_acc_seg7              VARCHAR2(40);

    lc_bud_acc_seg1              VARCHAR2(40);
    lc_bud_acc_seg2              VARCHAR2(40);
    lc_bud_acc_seg3              VARCHAR2(40);
    lc_bud_acc_seg4              VARCHAR2(40);
    lc_bud_acc_seg5              VARCHAR2(40);
    lc_bud_acc_seg6              VARCHAR2(40);
    lc_bud_acc_seg7              VARCHAR2(40);

    lc_accr_acc_seg1              VARCHAR2(40);
    lc_accr_acc_seg2              VARCHAR2(40);
    lc_accr_acc_seg3              VARCHAR2(40);
    lc_accr_acc_seg4              VARCHAR2(40);
    lc_accr_acc_seg5              VARCHAR2(40);
    lc_accr_acc_seg6              VARCHAR2(40);
    lc_accr_acc_seg7              VARCHAR2(40);

    lc_var_acc_seg1              VARCHAR2(40);
    lc_var_acc_seg2              VARCHAR2(40);
    lc_var_acc_seg3              VARCHAR2(40);
    lc_var_acc_seg4              VARCHAR2(40);
    lc_var_acc_seg5              VARCHAR2(40);
    lc_var_acc_seg6              VARCHAR2(40);
    lc_var_acc_seg7              VARCHAR2(40);
    lc_seg3_value                VARCHAR2(40);
    lc_seg5_value                VARCHAR2(40);

    lb_success                   BOOLEAN;

    lc_error_details             VARCHAR2(4000);
    lc_error                     VARCHAR2(4000);

    lc_errbuf                    VARCHAR2(240);
    ln_retcode                   NUMBER;

    ln_header_cnt                NUMBER;
    ln_lines_cnt                 NUMBER;
    ln_dist_cnt                  NUMBER;

    ln_cur_org_id                NUMBER;

    lc_new_orgname               VARCHAR2(240);
    lc_item_segment2             VARCHAR2(40);
    lc_item_segment3             VARCHAR2(40);
    lc_new_glacct                VARCHAR2(240);
    lc_new_cat                   VARCHAR2(2000);
    lc_new_del                   VARCHAR2(240);
    lc_new_org_name              VARCHAR2(270);
    lc_new_sobname               VARCHAR2(270);
    lc_business_group            VARCHAR2(270);
    ln_bill_loc_id               NUMBER;
    ln_ship_loc_id               NUMBER;
    lc_inv_po_chk                VARCHAR2(270);
    lc_func_curr                 VARCHAR2(15);
    lc_rate_type                 VARCHAR2(30);
    ln_rate                      NUMBER;
    ld_rate_date                 DATE;
    lc_new_ship                  VARCHAR2(240);
    lc_new_bill                  VARCHAR2(240);
    ln_line_item_cnt             NUMBER;
    ln_po_item_chk               NUMBER;
    ln_vendor_site_id            NUMBER;
    ln_vendor_contact_id         NUMBER;
    ln_term_id                   NUMBER;
    lc_po_exists                 VARCHAR2(1);
    lc_line_exists               VARCHAR2(1);
    lc_primary_uom               VARCHAR2(25);
    lc_ship_via_lookup_code      VARCHAR2(25);

  lc_line_ship_loc  VARCHAR2(60);
  lc_ship_org       VARCHAR2(3);
  ln_po_header_id   NUMBER;

    ex_conc_error                EXCEPTION;

--
BEGIN

    ln_header_cnt := 0;

    --Process Purchase Orders data
    lc_errloc := 'Process purchase orders data';
    FOR lr_po_header_data IN lcu_po_header_data (p_rec_status)
    LOOP

        ln_org_id                   := NULL;
        ln_agent_id                 := NULL;
        ln_bu_id                    := NULL;
        lc_currency_code            := NULL;
        ln_vendor_id                := NULL;
        lc_payment_terms            := NULL;
        lc_freight_terms            := NULL;

        lc_error_details            := NULL;
        lc_new_orgname              := NULL;
        lc_business_group           := NULL;
        ln_bill_loc_id              := NULL;
        ln_ship_loc_id              := NULL;
        lc_inv_po_chk               := NULL;
        lc_func_curr                := NULL;
        lc_rate_type                := NULL;
        ln_rate                     := NULL;
        ld_rate_date                := NULL;
        lc_new_ship                 := NULL;
        lc_new_bill                 := NULL;
        lc_line_exists              := NULL;
        lc_primary_uom              := NULL;

        -- check if lines exist for this PO
        OPEN lcu_check_lines(lr_po_header_data.po_number);
        FETCH lcu_check_lines
        INTO lc_line_exists;
        CLOSE lcu_check_lines;

        IF lc_line_exists IS NULL THEN
          lc_error_details := lc_error_details||'No lines were found in the staging table for the given PO. ';
        END IF;

        lc_errloc   := 'Operating unit related derivations';
        lc_new_orgname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_OPERATING_UNIT'
                                                              ,'XXSIRF_CNV_OPERATING_UNIT'
                                                              ,lr_po_header_data.operating_unit
                                                              );

        IF lc_new_orgname IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive new operating unit for '||lr_po_header_data.operating_unit||'. ';
        ELSE
          ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lc_new_orgname);
          IF ln_org_id IS NULL THEN
            lc_error_details := lc_error_details||'Could not derive org id for new operating unit '||lc_new_orgname||'. ';
          ELSE
            -- Compare functional and document currency
            OPEN lcu_func_curr(ln_org_id);
            FETCH lcu_func_curr
            INTO lc_func_curr;
            CLOSE lcu_func_curr;

            IF lc_func_curr = lr_po_header_data.currency_code THEN
              lc_rate_type := NULL;
              ln_rate      := NULL;
              ld_rate_date := NULL;
            ELSE
              lc_rate_type := lr_po_header_data.rate_type;
              ln_rate      := lr_po_header_data.rate;
              ld_rate_date := lr_po_header_data.rate_date;

            END IF;
          END IF;
        END IF;

        lc_errloc := 'Checking for inventory purchase orders';
        lc_inv_po_chk := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_INV_SUPPLIERS'
                                                              ,'XXSIRF_CNV_INV_SUPPLIERS'
                                                              ,lr_po_header_data.vendor_name
                                                              );

        IF lc_inv_po_chk IS NULL THEN
          lc_error_details := lc_error_details||'Only inventory purchase orders will be migrated';
        END IF;



        IF lr_po_header_data.emp_bu_name IN ( 'CSR United States','CSR United Kingdom') THEN
          lc_business_group := lr_po_header_data.emp_bu_name;
        ELSE
          lc_business_group := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                   ,'XXCSR_CNV_HR_BG'
                                                                   ,'XXSIRF_CNV_HR_BG'
                                                                   ,lr_po_header_data.emp_bu_name
                                                                   );
        END IF;

        IF lc_business_group IS NULL THEN
          lc_error_details := lc_error_details||'Mapping for Business Group '||lr_po_header_data.emp_bu_name||' could not be found. ';
        ELSE
          ln_bu_id := XXCSR_COMMON_PKG.get_business_group_id(lc_business_group);
          IF ln_bu_id IS NULL THEN
            lc_error_details:= lc_error_details||' Business Group ID for '||lc_business_group||' could not be derived. ';
          ELSE
            --Derive agent id
            lc_errloc := 'Derive agent id';
            ln_agent_id := XXCSR_COMMON_PKG.get_person_id (p_first_name        => lr_po_header_data.emp_fname
                                                          ,p_last_name         => lr_po_header_data.emp_lname
                                                          ,p_date_of_birth     => lr_po_header_data.emp_dob
                                                          ,p_business_group_id => ln_bu_id
                                                          );
            IF ln_agent_id IS NULL THEN
                lc_error_details := lc_error_details || ' Invalid agent (employee) details.';
            END IF;

          END IF;
        END IF;



        --Check currency code
        lc_errloc := 'Check currency code';
        OPEN  lcu_chk_currency (lr_po_header_data.currency_code);
        FETCH lcu_chk_currency
        INTO  lc_currency_code;

        IF lcu_chk_currency%NOTFOUND THEN
            lc_error_details := lc_error_details || ' Invalid currency code.';
        END IF;

        CLOSE lcu_chk_currency;

        --Derive vendor id
        lc_errloc := 'Derive vendor id';

        IF lr_po_header_data.vendor_name IS NOT NULL THEN
          OPEN lcu_vendor_der(lr_po_header_data.vendor_name);
          FETCH lcu_vendor_der
          INTO  ln_vendor_id;
          CLOSE lcu_vendor_der;

          IF ln_vendor_id IS NULL THEN
              lc_error_details := lc_error_details || ' Vendor not migrated.';
          END IF;
        END IF;


        lc_errloc := 'Check payment terms';
        IF lr_po_header_data.payment_terms IS NOT NULL THEN
        lc_payment_terms := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                ,p_csr_lookup    => 'XXCSR_CNV_SUPPLIER_PAYTERMS'
                                                                ,p_sirf_lookup   => 'XXSIRF_CNV_SUPPLIER_PAYTERMS'
                                                                ,p_old_value     => lr_po_header_data.payment_terms
                                                                );

        IF lc_payment_terms IS NULL THEN
            lc_error_details := lc_error_details || ' Invalid payment terms.';
        END IF;
        END IF;

        --Check freight terms
        lc_errloc := 'Check freight terms';
        IF lr_po_header_data.freight_terms IS NOT NULL THEN
          lc_freight_terms := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                  ,p_csr_lookup    => 'XXCSR_CNV_FREIGHT_TERMS'
                                                                  ,p_sirf_lookup   => 'XXSIRF_CNV_FREIGHT_TERMS'
                                                                  ,p_old_value     => lr_po_header_data.freight_terms
                                                                  );

          IF lc_freight_terms IS NULL THEN
              lc_error_details := lc_error_details || ' Invalid freight terms.';
          END IF;

        END IF;

        IF lr_po_header_data.ship_to_location IS NOT NULL THEN

          lc_new_ship := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                                ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                                ,lr_po_header_data.ship_to_location
                                                                );
          IF lc_new_ship IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new ship to location for '||lr_po_header_data.ship_to_location||' in XXCSR_CNV_DELIVER_LOCATIONS. ';

          ELSE

            OPEN lcu_hr_loc(lc_new_ship);
            FETCH lcu_hr_loc
            INTO ln_ship_loc_id;
            CLOSE lcu_hr_loc;

            IF ln_ship_loc_id IS NULL THEN
              lc_error_details := lc_error_details || 'Ship to location id could not be derived for '||lr_po_header_data.ship_to_location;
            END IF;
          END IF;

        END IF;

        IF lr_po_header_data.bill_to_location IS NOT NULL THEN

          lc_new_bill := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_BILLTO_LOCATIONS'
                                                                ,'XXSIRF_CNV_BILLTO_LOCATIONS'
                                                                ,lr_po_header_data.bill_to_location
                                                                );
          IF lc_new_bill IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new bill to location for '||lr_po_header_data.bill_to_location||' in XXCSR_CNV_BILLTO_LOCATIONS. ';

          ELSE

            OPEN lcu_hr_loc(lc_new_bill);
            FETCH lcu_hr_loc
            INTO ln_bill_loc_id;
            CLOSE lcu_hr_loc;

            IF ln_bill_loc_id IS NULL THEN
              lc_error_details := lc_error_details || 'Bill to location id could not be derived for '||lr_po_header_data.bill_to_location;
            END IF;

          END IF;

        END IF;

        lc_ship_via_lookup_code := NULL;
        IF lr_po_header_data.ship_via_lookup_code IS NOT NULL THEN

          lc_ship_via_lookup_code := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                        ,'XXCSR_CNV_FREIGHT_CARRIER'
                                                                        ,'XXCSR_CNV_FREIGHT_CARRIER'
                                                                        ,lr_po_header_data.ship_via_lookup_code
                                                                        );

        END IF;


        IF lc_error_details IS NULL THEN

            --Insert purchase order header data in interface table
            lc_errloc := 'Insert purchase order header data in interface table';
            INSERT INTO po_headers_interface
            (interface_header_id
            ,batch_id
            ,interface_source_code
            ,action
            ,process_code
            ,org_id
            ,document_type_code
            ,currency_code
            ,rate_type
            ,rate_date
            ,rate
            ,agent_id
            ,vendor_id
            ,vendor_site_code
            ,ship_to_location_id
            ,bill_to_location_id
            ,payment_terms
            ,freight_terms
            ,approval_status
            ,approved_date
          --  ,revised_date
          --  ,revision_num
            ,confirming_order_flag
            ,comments
            ,acceptance_required_flag
            ,print_count
          --  ,printed_date
            ,frozen_flag
            ,closed_code
            ,from_rfq_num
            ,from_type_lookup_code
            ,attribute_category
            --,attribute1
            ,attribute2
           -- ,attribute3
            --,global_attribute1
            ,pay_on_code
            ,last_update_date
            ,last_updated_by
            ,creation_date
            ,created_by
            ,document_num
            ,freight_carrier
            )
            VALUES
            (po_headers_interface_s.nextval
            ,1
            ,'XXCSR Conversion'
            ,'ORIGINAL'
            ,'PENDING'
            ,ln_org_id
            ,lr_po_header_data.document_type_code -- 'STANDARD'
            ,lr_po_header_data.currency_code
            ,DECODE(lc_rate_type,'Corporate','User',lc_rate_type)
            ,ld_rate_date
            ,ln_rate
            ,ln_agent_id
            ,ln_vendor_id
            ,lr_po_header_data.vendor_site_code
            ,ln_ship_loc_id -- lr_po_header_data.ship_to_location
            ,ln_bill_loc_id -- lr_po_header_data.bill_to_location
            ,lc_payment_terms
            ,lc_freight_terms
            ,DECODE(lr_po_header_data.approval_status,'REQUIRES REAPPROVAL','INCOMPLETE',lr_po_header_data.approval_status)
            ,lr_po_header_data.approved_date
           -- ,lr_po_header_data.revised_date
           -- ,lr_po_header_data.revision_num
            ,lr_po_header_data.confirming_order_flag
            ,lr_po_header_data.comments
            ,lr_po_header_data.acceptance_required_flag
            --,DECODE(lr_po_header_data.approval_status,'APPROVED',lr_po_header_data.print_count,0)
            ,DECODE(lr_po_header_data.approval_status,'APPROVED',1,0)
          --  ,lr_po_header_data.printed_date
            ,lr_po_header_data.frozen_flag
            ,lr_po_header_data.closed_code
            ,lr_po_header_data.from_requisition_num
            ,lr_po_header_data.from_type_lookup_code
            ,lr_po_header_data.attribute_category
            --,lr_po_header_data.sourcing_rule
            ,lr_po_header_data.osp_flag
            --,lr_po_header_data.mrp_flag
            --,lr_po_header_data.po_total_plus_ipi_tax
            ,lr_po_header_data.pay_on_code
            ,SYSDATE
            ,fnd_global.user_id
            ,SYSDATE
            ,fnd_global.user_id
            ,lr_po_header_data.po_number
            ,lc_ship_via_lookup_code
            );

            UPDATE xxcsr_po_headers_int
            SET    record_status = 'C'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = NULL
            WHERE rowid = lr_po_header_data.rowid;

            COMMIT;

            ln_header_cnt := ln_header_cnt + 1;

        ELSE
            UPDATE xxcsr_po_headers_int
            SET    record_status = 'E'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = lc_error_details
            WHERE  rowid = lr_po_header_data.rowid;
        END IF; --lc_error_details IS NULL

        COMMIT;
    END LOOP;
    --COMMIT;


    ln_lines_cnt := 0;

    --Process Purchase Order Lines data
    lc_errloc := 'Process purchase order lines data';
    FOR lr_po_lines_data IN lcu_po_lines_data (p_rec_status)
    LOOP

        ln_org_id                   := NULL;
        ln_item_org_id              := NULL;
        ln_item_id                  := NULL;
        lc_item_seg2                := NULL;
        lc_cat_seg1                 := NULL;
        lc_cat_seg2                 := NULL;
        lc_cat_seg3                 := NULL;
        lc_cat_seg4                 := NULL;
        lc_cat_seg5                 := NULL;
        ln_cat_id                   := NULL;
        ln_po_int_header_id         := NULL;
        ln_tax_code_id              := NULL;

        lc_error_details            := NULL;

        lc_new_orgname              := NULL;
        lc_item_segment2            := NULL;
        lc_new_glacct               := NULL;
        lc_new_cat                  := NULL;
        lc_new_ship                 := NULL;
        ln_ship_loc_id              := NULL;
        lc_new_org_name             := NULL;

        --Derive interface header id
        lc_errloc := 'Derive interface header id';
        OPEN  lcu_get_int_header_id (lr_po_lines_data.po_number);
        FETCH lcu_get_int_header_id
        INTO  ln_po_int_header_id;
        CLOSE lcu_get_int_header_id;

        IF ln_po_int_header_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive interface header id. Header record may have error. ';
        END IF;

        FND_FILE.PUT_LINE(FND_FILE.LOG,'ln_po_int_header_id : ' || ln_po_int_header_id);

        lc_errloc   := 'Operating unit related derivations';
        lc_new_orgname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_OPERATING_UNIT'
                                                              ,'XXSIRF_CNV_OPERATING_UNIT'
                                                              ,lr_po_lines_data.org_name
                                                              );

        IF lc_new_orgname IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive new operating unit for '||lr_po_lines_data.org_name||'. ';
        ELSE
          ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lc_new_orgname);
          IF ln_org_id IS NULL THEN
            lc_error_details := lc_error_details||'Could not derive org id for new operating unit '||lc_new_orgname||'. ';
          END IF;
        END IF;

        IF lr_po_lines_data.ship_to_location IS NOT NULL THEN

          lc_new_ship := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                                ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                                ,lr_po_lines_data.ship_to_location
                                                                );
          IF lc_new_ship IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new ship to location for '||lr_po_lines_data.ship_to_location||' in XXCSR_CNV_DELIVER_LOCATIONS. ';

          ELSE

            OPEN lcu_hr_loc(lc_new_ship);
            FETCH lcu_hr_loc
            INTO ln_ship_loc_id;
            CLOSE lcu_hr_loc;

            IF ln_ship_loc_id IS NULL THEN
              lc_error_details := lc_error_details || 'Ship to location id could not be derived for '||lr_po_lines_data.ship_to_location;
            END IF;
          END IF;

        END IF;

        IF lr_po_lines_data.ship_to_org IS NOT NULL THEN

          lc_new_org_name := XXCSR_COMMON_PKG.get_organization_code (p_instance_name
                                                                    ,lr_po_lines_data.ship_to_org);

          IF lc_new_org_name IS NULL THEN
            lc_error_details := lc_error_details||'Mapping for organization '||lr_po_lines_data.ship_to_org||' not found in the table XXCSR_CNV_ORG_MAPPINGS. ';
          END IF;

        END IF;

      lc_item_segment3 := NULL;

      IF lr_po_lines_data.itm_seg3_item_type = 'MT' THEN
          lc_item_segment2 := 'MT';
          lc_item_segment3 := NULL;
      ELSIF lr_po_lines_data.itm_seg3_item_type = 'FL' THEN
          lc_item_segment2 := 'PD';
          lc_item_segment3 := NULL;
      ELSE
          lc_item_segment2 := lr_po_lines_data.itm_seg2_bom_level;
          lc_item_segment3 := lr_po_lines_data.itm_seg3_item_type;
      END IF;

      lc_errloc := 'Derive inventory item id';
   -- IF lr_po_lines_data.itm_seg1_item_num IS NOT NULL THEN

        OPEN lcu_item_id(lr_po_lines_data.itm_seg1_item_num
                        ,lc_item_segment2
                        ,lc_item_segment3); --sjacob commented as per Siva's mail lr_po_lines_data.itm_seg3_item_type
        FETCH lcu_item_id
        INTO ln_item_id;
        CLOSE lcu_item_id;

        IF ln_item_id IS NULL THEN
            lc_error_details := lc_error_details || ' Item number could not be derived.';
        ELSE
          -- DERIVE primary UOM for master item
          IF lc_new_org_name IS NOT NULL THEN
            OPEN lcu_prim_uom(lc_new_org_name
                             ,ln_item_id);
            FETCH lcu_prim_uom
            INTO lc_primary_uom;
            CLOSE lcu_prim_uom;

            IF lc_primary_uom IS NULL THEN
              lc_error_details := lc_error_details || 'Primary UOM code for the master item could not be derived';
            END IF;

          END IF;
        END IF;

   --   END IF;

        lc_errloc := 'Derive new GL account';
        lc_new_glacct := XXCSR_COMMON_PKG.get_lookup_meaning(p_instance_name => p_instance_name
                                                          ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                          ,p_sirf_lookup   => 'XXCSR_CNV_GL_ACCOUNT'
                                                          ,p_old_value     => lr_po_lines_data.gl_account
                                                          );

        lc_new_cat := XXCSR_COMMON_PKG.get_lookup_meaning(p_instance_name => p_instance_name
                                                          ,p_csr_lookup    => 'XXCSR_CNV_PO_CATEGORY'
                                                          ,p_sirf_lookup   => 'XXCSR_CNV_PO_CATEGORY'
                                                          ,p_old_value     => lc_new_glacct
                                                          );

        IF lc_new_cat IS NULL THEN
          lc_new_cat := 'MISCELLANEOUS.MISCELLANEOUS';
        END IF;

        IF lr_po_lines_data.inventory_item_flag = 'N' THEN

          UPDATE xxcsr_po_distributions_int
          SET destination_type_code = 'EXPENSE'
          WHERE po_number = lr_po_lines_data.po_number
          AND po_line_number = lr_po_lines_data.line_num
          AND shipment_num = lr_po_lines_data.shipment_num
          AND source_instance = p_instance_name;

        ELSE

          UPDATE xxcsr_po_distributions_int
          SET destination_type_code = 'INVENTORY'
          WHERE po_number = lr_po_lines_data.po_number
          AND po_line_number = lr_po_lines_data.line_num
          AND shipment_num = lr_po_lines_data.shipment_num
          AND source_instance = p_instance_name;

        END IF;

        IF lc_error_details IS NULL THEN

            --Insert purchase order lines data in interface table
            lc_errloc := 'Insert purchase order lines data in interface table';
            FND_FILE.PUT_LINE(FND_FILE.LOG,'Inserting in lines');
            INSERT INTO po_lines_interface
            (interface_line_id
            ,interface_header_id
            ,action
            ,process_code
            ,document_num
            ,line_num
            ,organization_id
            ,shipment_num
            ,shipment_type
            ,contract_num
            ,line_type
            ,item_id
            ,category
            ,item_description
            ,vendor_product_num
            ,unit_of_measure
            ,quantity
            ,unit_price
            ,list_price_per_unit
            ,allow_price_override_flag
            ,negotiated_by_preparer_flag
            ,note_to_vendor
            ,taxable_flag
           -- ,tax_name
            ,type_1099
            ,capital_expense_flag
            ,price_type
            ,closed_code
            ,closed_reason
            ,closed_date
            ,closed_by
            ,over_tolerance_error_flag
            ,line_attribute_category_lines
          --  ,line_attribute1
          --  ,line_attribute2
          --  ,line_attribute3
          --  ,line_attribute4
          --  ,line_attribute5
          --  ,line_attribute6
          --  ,line_attribute7
          --  ,line_attribute8
          --  ,line_attribute9
            --,global_attribute1
           -- ,tax_code_id
            ,secondary_unit_of_measure
            ,secondary_quantity
            ,inspection_required_flag
            ,receipt_required_flag
            ,invoice_close_tolerance
            ,receive_close_tolerance
            ,need_by_date
            ,promised_date
            ,consigned_flag
            ,base_unit_price
            ,last_update_date
            ,last_updated_by
            ,creation_date
            ,created_by
            ,ship_to_location_id
            ,ship_to_organization_code
            )
            VALUES
            (po_lines_interface_s.nextval
            ,ln_po_int_header_id
            ,'ORIGINAL'
            ,'PENDING'
            ,lr_po_lines_data.po_number
            ,lr_po_lines_data.line_num
            ,ln_org_id
            ,lr_po_lines_data.shipment_num
            ,lr_po_lines_data.shipment_type
            ,lr_po_lines_data.contract_num
            ,lr_po_lines_data.line_type
            ,ln_item_id
            ,lc_new_cat
            ,lr_po_lines_data.item_description
            ,lr_po_lines_data.vendor_product_num
            ,lc_primary_uom --lr_po_lines_data.unit_of_measure
            ,lr_po_lines_data.quantity
            ,lr_po_lines_data.unit_price
            ,lr_po_lines_data.list_price_per_unit
            ,lr_po_lines_data.allow_price_override_flag
            ,lr_po_lines_data.negotiated_by_preparer_flag
            ,lr_po_lines_data.note_to_vendor
            ,lr_po_lines_data.taxable_flag
          --  ,lr_po_lines_data.tax_name
            ,lr_po_lines_data.type_1099
            ,lr_po_lines_data.capital_expense_flag
            ,lr_po_lines_data.price_type
            ,lr_po_lines_data.closed_code
            ,lr_po_lines_data.closed_reason
            ,lr_po_lines_data.closed_date
            ,lr_po_lines_data.closed_by
            ,lr_po_lines_data.over_tolerance_error_flag
            ,lr_po_lines_data.line_attribute_category_lines
           -- ,lr_po_lines_data.att1_item_number
           -- ,lr_po_lines_data.att2_manufacturer
           -- ,lr_po_lines_data.att3_manufacturer_part_no
           -- ,lr_po_lines_data.att4_vendor_name
           -- ,lr_po_lines_data.att5_last_po_price
           -- ,lr_po_lines_data.att6_last_transaction_qty
           -- ,lr_po_lines_data.att7_last_vendor_name
           -- ,lr_po_lines_data.att8_item_id
           -- ,lr_po_lines_data.att9_vendor_name
            --,lr_po_lines_data.fisc_classification_code
           -- ,ln_tax_code_id         commented as per R04
            ,lr_po_lines_data.secondary_unit_of_measure
            ,lr_po_lines_data.secondary_quantity
            ,lr_po_lines_data.inspection_required_flag
            ,lr_po_lines_data.receipt_required_flag
            ,lr_po_lines_data.invoice_close_tolerance
            ,lr_po_lines_data.receive_close_tolerance
            ,lr_po_lines_data.need_by_date
            ,lr_po_lines_data.promised_date
            ,lr_po_lines_data.consigned_flag
            ,lr_po_lines_data.base_unit_price
            ,SYSDATE
            ,fnd_global.user_id
            ,SYSDATE
            ,fnd_global.user_id
            ,ln_ship_loc_id
            ,lc_new_org_name
            );

            UPDATE xxcsr_po_lines_int
            SET    record_status = 'C'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = NULL
            WHERE rowid          = lr_po_lines_data.rowid;

            COMMIT;

            ln_lines_cnt := ln_lines_cnt + 1;

        ELSE
            UPDATE xxcsr_po_lines_int
            SET    record_status = 'E'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = lc_error_details
            WHERE  rowid         = lr_po_lines_data.rowid;

           IF ln_po_int_header_id IS NOT NULL THEN
             UPDATE xxcsr_po_headers_int
             SET record_status = 'E'
                ,error_message = error_message||'error in lines record'
             WHERE  po_number = lr_po_lines_data.po_number
             AND source_instance = p_instance_name;

             DELETE FROM po_headers_interface
             WHERE interface_header_id = ln_po_int_header_id;

             ln_header_cnt := ln_header_cnt - 1;

          END IF;

        END IF; --lc_error_details IS NULL

        COMMIT;
    END LOOP;
    --COMMIT;


    ln_dist_cnt := 0;

    --Process Purchase Order distributions data
    lc_errloc := 'Process purchase order distributions data';
    FOR lr_po_dist_data IN lcu_po_dist_data (p_rec_status)
    LOOP

        ln_org_id                   := NULL;
        ln_sob_id                   := NULL;
        ln_location_id              := NULL;
        lc_inv_org_code             := NULL;
        ln_person_org_id            := NULL;
        ln_person_id                := NULL;

        lc_error_details            := NULL;
        lc_new_orgname              := NULL;
        lc_new_del                  := NULL;
        lc_new_org_name             := NULL;
        lc_new_sobname              := NULL;
        ln_po_int_line_id           := NULL;
        ln_po_int_header_id         := NULL;
        lc_business_group           := NULL;
        ln_bu_id                    := NULL;
        lc_chr_acc_seg1             := NULL;
        lc_chr_acc_seg2             := NULL;
        lc_chr_acc_seg3             := NULL;
        lc_chr_acc_seg4             := NULL;
        lc_chr_acc_seg5             := NULL;
        lc_chr_acc_seg6             := NULL;
        lc_chr_acc_seg7             := NULL;
        lc_bud_acc_seg1             := NULL;
        lc_bud_acc_seg2             := NULL;
        lc_bud_acc_seg3             := NULL;
        lc_bud_acc_seg4             := NULL;
        lc_bud_acc_seg5             := NULL;
        lc_bud_acc_seg6             := NULL;
        lc_bud_acc_seg7             := NULL;
        lc_accr_acc_seg1            := NULL;
        lc_accr_acc_seg2            := NULL;
        lc_accr_acc_seg3            := NULL;
        lc_accr_acc_seg4            := NULL;
        lc_accr_acc_seg5            := NULL;
        lc_accr_acc_seg6            := NULL;
        lc_accr_acc_seg7            := NULL;
        lc_var_acc_seg1             := NULL;
        lc_var_acc_seg2             := NULL;
        lc_var_acc_seg3             := NULL;
        lc_var_acc_seg4             := NULL;
        lc_var_acc_seg5             := NULL;
        lc_var_acc_seg6             := NULL;
        lc_var_acc_seg7             := NULL;
        ln_charge_acct_id           := NULL;
        ln_budget_acct_id           := NULL;
        ln_accrual_acct_id          := NULL;
        ln_variance_acct_id         := NULL;
        lc_seg3_value               := NULL;
        lc_seg5_value               := NULL;

        --Derive interface header id
        lc_errloc := 'Derive interface header id';
        OPEN  lcu_get_int_header_id (lr_po_dist_data.po_number);
        FETCH lcu_get_int_header_id
        INTO  ln_po_int_header_id;
        CLOSE lcu_get_int_header_id;

        IF ln_po_int_header_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive interface header id. Header record may have error. ';
        END IF;

        --Derive interface line id
        lc_errloc := 'Derive interface line id';
        OPEN  lcu_get_int_line_id (lr_po_dist_data.po_number
                                  ,lr_po_dist_data.po_line_number
                                  ,lr_po_dist_data.shipment_num);
        FETCH lcu_get_int_line_id
        INTO  ln_po_int_line_id;
        CLOSE lcu_get_int_line_id;

        IF ln_po_int_line_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive interface line id. Header/Line record may have error. ';
        END IF;

        lc_errloc   := 'Operating unit related derivations';
        lc_new_orgname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_OPERATING_UNIT'
                                                              ,'XXSIRF_CNV_OPERATING_UNIT'
                                                              ,lr_po_dist_data.org_name
                                                              );

        IF lc_new_orgname IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive new operating unit for '||lr_po_dist_data.org_name||'. ';
        ELSE
          ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lc_new_orgname);
          IF ln_org_id IS NULL THEN
            lc_error_details := lc_error_details||'Could not derive org id for new operating unit '||lc_new_orgname||'. ';
          END IF;
        END IF;

        --Derive sob id

        lc_new_sobname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_SOB_NAME'
                                                              ,'XXSIRF_CNV_SOB_NAME'
                                                              ,lr_po_dist_data.set_of_books
                                                              );

        IF lc_new_sobname IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive new sob for '||lr_po_dist_data.set_of_books||' in XXCSR_CNV_SOB_NAME. ';
        ELSE

          lc_errloc := 'Derive sob id';
          OPEN  lcu_get_sob_id (lc_new_sobname);
          FETCH lcu_get_sob_id
          INTO  ln_sob_id;
          CLOSE lcu_get_sob_id;

          IF ln_sob_id IS NULL THEN
              lc_error_details := lc_error_details || ' Invalid sets of books name- '||lc_new_sobname;
          END IF;

        END IF;

        IF lr_po_dist_data.deliver_to_location IS NOT NULL THEN

          lc_new_del := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                                ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                                ,lr_po_dist_data.deliver_to_location
                                                                );

          IF lc_new_del IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new deliver to location for '||lr_po_dist_data.deliver_to_location||' in XXCSR_CNV_DELIVER_LOCATIONS. ';

          ELSE
            --Derive deliver to location id
            lc_errloc := 'Derive deliver to location id';
            OPEN  lcu_location_id (lc_new_del);
            FETCH lcu_location_id
            INTO  ln_location_id;
            CLOSE lcu_location_id;

            IF ln_location_id IS NULL THEN
                lc_error_details := lc_error_details || ' Invalid deliver to location code.';
            END IF;

          END IF;

        END IF;

        IF lr_po_dist_data.destination_organization IS NOT NULL THEN

          lc_new_org_name := XXCSR_COMMON_PKG.get_organization_code (p_instance_name
                                                                    ,lr_po_dist_data.destination_organization);

          IF lc_new_org_name IS NULL THEN
            lc_error_details := lc_error_details||'Mapping for organization '||lr_po_dist_data.destination_organization||' not found in the table XXCSR_CNV_ORG_MAPPINGS. ';
          END IF;

        END IF;


        --Derive deliver to person id
        IF  lr_po_dist_data.delv_per_fname IS NOT NULL THEN
          lc_business_group := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                   ,'XXCSR_CNV_HR_BG'
                                                                   ,'XXSIRF_CNV_HR_BG'
                                                                   ,lr_po_dist_data.delv_per_org_name
                                                                   );

          IF lc_business_group IS NULL THEN
            lc_error_details := lc_error_details||'Mapping for Business Group '||lr_po_dist_data.delv_per_org_name||' could not be found. ';
          ELSE
            ln_bu_id := XXCSR_COMMON_PKG.get_business_group_id(lc_business_group);
            IF ln_bu_id IS NULL THEN
              lc_error_details:= lc_error_details||' Business Group ID for '||lc_business_group||' could not be derived. ';
              --Derive deliver to person id
              lc_errloc := 'Derive deliver to person id';
              ln_person_id := XXCSR_COMMON_PKG.get_person_id (p_first_name        => lr_po_dist_data.delv_per_fname
                                                            ,p_last_name         => lr_po_dist_data.delv_per_lname
                                                            ,p_date_of_birth     => lr_po_dist_data.delv_per_dob
                                                            ,p_business_group_id => ln_bu_id
                                                            );
              IF ln_person_id IS NULL THEN
                  lc_error_details := lc_error_details || ' Invalid agent (employee) details.';
              END IF;

            END IF;
          END IF;

        END IF;

        --BUDGET ACCOUNT
        --Derive budget account segment1 from lookup
        lc_errloc := 'Derive budget account segment1 from lookup';

      IF  (lr_po_dist_data.bud_acc_seg1 IS NOT NULL OR lr_po_dist_data.bud_acc_seg2 IS NOT NULL OR
          lr_po_dist_data.bud_acc_seg3 IS NOT NULL OR lr_po_dist_data.bud_acc_seg4 IS NOT NULL OR
          lr_po_dist_data.bud_acc_seg5 IS NOT NULL OR lr_po_dist_data.bud_acc_seg6 IS NOT NULL OR
          lr_po_dist_data.bud_acc_seg7 IS NOT NULL) THEN

        lc_bud_acc_seg1 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_COMPANY'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_COMPANY'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg1
                                                               );


        --Derive budget account segment2 from lookup
        lc_errloc := 'Derive budget account segment2 from lookup';
        lc_bud_acc_seg2 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_ACCOUNT'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg2
                                                               );

        --Derive budget account segment3 from lookup
        lc_errloc := 'Derive budget account segment3 from lookup';
     /*   lc_bud_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg4
                                                               ); */

        lc_seg3_value := NULL;

        IF UPPER(lr_po_dist_data.bud_acc_seg4) = LOWER(lr_po_dist_data.bud_acc_seg4) THEN
          lc_seg3_value := lr_po_dist_data.bud_acc_seg3;
        ELSE
          lc_seg3_value := lr_po_dist_data.bud_acc_seg4;
        END IF;

        lc_bud_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                               ,p_old_value     => lc_seg3_value
                                                               );


        --Derive budget account segment4 from lookup
        lc_errloc := 'Derive budget account segment4 from lookup';
        lc_bud_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg4
                                                               );

        --Derive budget account segment5 from lookup
        lc_errloc := 'Derive budget account segment5 from lookup';
      /*  lc_bud_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg5
                                                               ); */

       lc_seg5_value := NULL;

        /* Segments 5 and 6 in the legacy instance both map onto Segment 5 in the target instance.
        When mapping the data, take whichever one of the legacy instance values
        in non-zero and map using lookup XXCSR_CNV_GL_PRODPROJ.
        If both legacy values are zero, use the default of '000000' for Segment 5 of the target GL Account.  */

        IF UPPER(lr_po_dist_data.bud_acc_seg5) = LOWER(lr_po_dist_data.bud_acc_seg5) THEN -- 5 numeric
          IF TO_NUMBER(lr_po_dist_data.bud_acc_seg5) = 0 THEN -- 5 is zero
            IF UPPER(lr_po_dist_data.bud_acc_seg6) != LOWER(lr_po_dist_data.bud_acc_seg6) THEN
              lc_seg5_value := lr_po_dist_data.bud_acc_seg6;
            ELSIF TO_NUMBER(lr_po_dist_data.bud_acc_seg6) = 0 THEN -- 6 is zero
              lc_seg5_value := '000000';
            ELSE
              lc_seg5_value := lr_po_dist_data.bud_acc_seg6;
            END IF;
          ELSE
            IF UPPER(lr_po_dist_data.bud_acc_seg6) != LOWER(lr_po_dist_data.bud_acc_seg6) THEN
              lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.bud_acc_seg5||') and segment6('||lr_po_dist_data.bud_acc_seg6||') cannot be non zero';
            ELSIF TO_NUMBER(lr_po_dist_data.bud_acc_seg6) = 0 THEN -- 6 is zero
              lc_seg5_value := lr_po_dist_data.bud_acc_seg5;
            ELSE
              lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.bud_acc_seg5||') and segment6('||lr_po_dist_data.bud_acc_seg6||') cannot be non zero';
            END IF;
          END IF;

        ELSE -- 5 non numeric
          IF UPPER(lr_po_dist_data.bud_acc_seg6) != LOWER(lr_po_dist_data.bud_acc_seg6) THEN
            lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.bud_acc_seg5||') and segment6('||lr_po_dist_data.bud_acc_seg6||') cannot be non zero';
          ELSIF TO_NUMBER(lr_po_dist_data.bud_acc_seg6) = 0 THEN -- 6 is zero
            lc_seg5_value := lr_po_dist_data.bud_acc_seg5;
          ELSE
            lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.bud_acc_seg5||') and segment6('||lr_po_dist_data.bud_acc_seg6||') cannot be non zero';
          END IF;
        END IF;

        IF lc_seg5_value IS NOT NULL AND lc_seg5_value != '000000' THEN

          lc_bud_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                 ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                                 ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                                 ,p_old_value     => lc_seg5_value
                                                                 );

        ELSIF lc_seg5_value IS NOT NULL AND lc_seg5_value = '000000' THEN
          lc_bud_acc_seg5 := lc_seg5_value;
        END IF;

        --Derive budget account segment6 from lookup
        lc_errloc := 'Derive budget account segment6 from lookup';
   /*     lc_bud_acc_seg6 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_INTERCO'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_INTERCO'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg6
                                                               ); */

        lc_bud_acc_seg6 := '000';

        --Derive budget account segment7 from lookup
        lc_errloc := 'Derive budget account segment7 from lookup';
     /*   lc_bud_acc_seg7 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_BUD_ACC7_LKUP'
                                                               ,p_sirf_lookup   => 'XXSIRF_BUD_ACC7_LKUP'
                                                               ,p_old_value     => lr_po_dist_data.bud_acc_seg7
                                                               ); */

        lc_bud_acc_seg7 := '00000';
      END IF;

      IF (lr_po_dist_data.accr_acc_seg1 IS NOT NULL OR lr_po_dist_data.accr_acc_seg2 IS NOT NULL OR
          lr_po_dist_data.accr_acc_seg3 IS NOT NULL OR lr_po_dist_data.accr_acc_seg4 IS NOT NULL OR
          lr_po_dist_data.accr_acc_seg5 IS NOT NULL OR lr_po_dist_data.accr_acc_seg6 IS NOT NULL OR
          lr_po_dist_data.accr_acc_seg7 IS NOT NULL) THEN
        --ACCRUAL ACCOUNT
        --Derive accrual account segment1 from lookup
        lc_errloc := 'Derive accrual account segment1 from lookup';
        lc_accr_acc_seg1 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_COMPANY'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_COMPANY'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg1
                                                               );

        -- Change by manishar 12/04/10 as per mdt v1.26
        -- Deriving Attribute15 of the corresponding Suppliers Address
        lc_errloc := 'Derive accrual account segment2 from lookup';

        lc_accr_acc_seg2 := NULL;
        OPEN  lcu_supsite_attr15(lr_po_dist_data.po_number
                                ,lr_po_dist_data.po_line_number
                                ,lr_po_dist_data.shipment_num);
        FETCH lcu_supsite_attr15 INTO lc_accr_acc_seg2;
        CLOSE lcu_supsite_attr15;

        --Derive accrual account segment2 from lookup
        IF lc_accr_acc_seg2 IS NULL THEN

          lc_accr_acc_seg2 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                 ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                                 ,p_sirf_lookup   => 'XXSIRF_CNV_GL_ACCOUNT'
                                                                 ,p_old_value     => lr_po_dist_data.accr_acc_seg2
                                                                 );
        END IF;

        --Derive accrual account segment3 from lookup
        lc_errloc := 'Derive accrual account segment3 from lookup';
        lc_accr_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg3
                                                               );

        --Derive accrual account segment4 from lookup
        lc_errloc := 'Derive accrual account segment4 from lookup';
       /*  lc_accr_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg4
                                                               ); */

        --Derive budget account segment4 from lookup
        lc_errloc := 'Derive budget account segment4 from lookup';
        lc_accr_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg4
                                                               );

        --Derive accrual account segment5 from lookup
        lc_errloc := 'Derive accrual account segment5 from lookup';
        lc_accr_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg5
                                                               );

        --Derive accrual account segment6 from lookup
        lc_errloc := 'Derive accrual account segment6 from lookup';
       /* lc_accr_acc_seg6 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_INTERCO'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_INTERCO'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg6
                                                               ); */

        lc_accr_acc_seg6 := '000';

        --Derive accrual account segment7 from lookup
        lc_errloc := 'Derive accrual account segment7 from lookup';
       /* lc_accr_acc_seg7 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_ACCR_ACC7_LKUP'
                                                               ,p_sirf_lookup   => 'XXSIRF_ACCR_ACC7_LKUP'
                                                               ,p_old_value     => lr_po_dist_data.accr_acc_seg7
                                                               ); */
        lc_accr_acc_seg7 := '00000';

      END IF;

      IF (lr_po_dist_data.var_acc_seg1 IS NOT NULL OR lr_po_dist_data.var_acc_seg2 IS NOT NULL OR
         lr_po_dist_data.var_acc_seg3 IS NOT NULL OR lr_po_dist_data.var_acc_seg4 IS NOT NULL OR
         lr_po_dist_data.var_acc_seg5 IS NOT NULL OR lr_po_dist_data.var_acc_seg6 IS NOT NULL OR
         lr_po_dist_data.var_acc_seg7 IS NOT NULL) THEN

        --VARIANCE ACCOUNT
        --Derive variance account segment1 from lookup
        lc_errloc := 'Derive variance account segment1 from lookup';
        lc_var_acc_seg1 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_COMPANY'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_COMPANY'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg1
                                                               );

        --Derive variance account segment2 from lookup
        lc_errloc := 'Derive variance account segment2 from lookup';
        lc_var_acc_seg2 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_ACCOUNT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_ACCOUNT'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg2
                                                               );

        --Derive variance account segment3 from lookup
        lc_errloc := 'Derive variance account segment3 from lookup';
        lc_var_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg4
                                                               );

        --Derive variance account segment4 from lookup
        lc_errloc := 'Derive variance account segment4 from lookup';
      /*  lc_var_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg4
                                                               ); */

        lc_seg3_value := NULL;

        IF UPPER(lr_po_dist_data.var_acc_seg4) = LOWER(lr_po_dist_data.var_acc_seg4) THEN
          lc_seg3_value := lr_po_dist_data.var_acc_seg3;
        ELSE
          lc_seg3_value := lr_po_dist_data.var_acc_seg4;
        END IF;

        lc_var_acc_seg3 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_BU'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_BU'
                                                               ,p_old_value     => lc_seg3_value
                                                               );


        --Derive budget account segment4 from lookup
        lc_errloc := 'Derive budget account segment4 from lookup';
        lc_var_acc_seg4 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_DEPARTMENT'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_DEPARTMENT'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg4
                                                               );


        --Derive variance account segment5 from lookup
        lc_errloc := 'Derive variance account segment5 from lookup';
      /*  lc_var_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg5
                                                               ); */

       lc_seg5_value := NULL;

        /* Segments 5 and 6 in the legacy instance both map onto Segment 5 in the target instance.
        When mapping the data, take whichever one of the legacy instance values
        in non-zero and map using lookup XXCSR_CNV_GL_PRODPROJ.
        If both legacy values are zero, use the default of '000000' for Segment 5 of the target GL Account.  */

        IF UPPER(lr_po_dist_data.var_acc_seg5) = LOWER(lr_po_dist_data.var_acc_seg5) THEN -- 5 numeric
          IF TO_NUMBER(lr_po_dist_data.var_acc_seg5) = 0 THEN -- 5 is zero
            IF UPPER(lr_po_dist_data.var_acc_seg6) != LOWER(lr_po_dist_data.var_acc_seg6) THEN
              lc_seg5_value := lr_po_dist_data.var_acc_seg6;
            ELSIF TO_NUMBER(lr_po_dist_data.var_acc_seg6) = 0 THEN -- 6 is zero
              lc_seg5_value := '000000';
            ELSE
              lc_seg5_value := lr_po_dist_data.var_acc_seg6;
            END IF;
          ELSE
            IF UPPER(lr_po_dist_data.var_acc_seg6) != LOWER(lr_po_dist_data.var_acc_seg6) THEN
              lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.var_acc_seg5||') and segment6('||lr_po_dist_data.var_acc_seg6||') cannot be non zero';
            ELSIF TO_NUMBER(lr_po_dist_data.var_acc_seg6) = 0 THEN -- 6 is zero
              lc_seg5_value := lr_po_dist_data.var_acc_seg5;
            ELSE
              lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.var_acc_seg5||') and segment6('||lr_po_dist_data.var_acc_seg6||') cannot be non zero';
            END IF;
          END IF;

        ELSE -- 5 non numeric
          IF UPPER(lr_po_dist_data.var_acc_seg6) != LOWER(lr_po_dist_data.var_acc_seg6) THEN
            lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.var_acc_seg5||') and segment6('||lr_po_dist_data.var_acc_seg6||') cannot be non zero';
          ELSIF TO_NUMBER(lr_po_dist_data.var_acc_seg6) = 0 THEN -- 6 is zero
            lc_seg5_value := lr_po_dist_data.var_acc_seg5;
          ELSE
            lc_error_details := lc_error_details || 'Both segment5('||lr_po_dist_data.var_acc_seg5||') and segment6('||lr_po_dist_data.var_acc_seg6||') cannot be non zero';
          END IF;
        END IF;

        IF lc_seg5_value IS NOT NULL AND lc_seg5_value != '000000' THEN

          lc_var_acc_seg5 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                 ,p_csr_lookup    => 'XXCSR_CNV_GL_PRODPROJ'
                                                                 ,p_sirf_lookup   => 'XXSIRF_CNV_GL_PRODPROJ'
                                                                 ,p_old_value     => lc_seg5_value
                                                                 );

        ELSIF lc_seg5_value IS NOT NULL AND lc_seg5_value = '000000' THEN
          lc_var_acc_seg5 := lc_seg5_value;
        END IF;

        --Derive variance account segment6 from lookup
        lc_errloc := 'Derive variance account segment6 from lookup';
      /*  lc_var_acc_seg6 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_CNV_GL_INTERCO'
                                                               ,p_sirf_lookup   => 'XXSIRF_CNV_GL_INTERCO'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg6
                                                               ); */

       lc_var_acc_seg6 := '000';


        --Derive variance account segment7 from lookup
        lc_errloc := 'Derive variance account segment7 from lookup';
      /*  lc_var_acc_seg7 := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                               ,p_csr_lookup    => 'XXCSR_VAR_ACC7_LKUP'
                                                               ,p_sirf_lookup   => 'XXSIRF_VAR_ACC7_LKUP'
                                                               ,p_old_value     => lr_po_dist_data.var_acc_seg7
                                                               ); */


        lc_var_acc_seg7 := '00000';

      END IF;
/*
        --Derive charge account id
        lc_errloc := 'Derive charge account id';
        OPEN  lcu_account_id (lc_chr_acc_seg1
                             ,lc_chr_acc_seg2
                             ,lc_chr_acc_seg3
                             ,lc_chr_acc_seg4
                             ,lc_chr_acc_seg5
                             ,lc_chr_acc_seg6
                             ,lc_chr_acc_seg7);
        FETCH lcu_account_id
        INTO  ln_charge_acct_id;
        CLOSE lcu_account_id;

        IF ln_charge_acct_id IS NULL THEN
          lc_error_details := lc_error_details || 'Could not derive charge of accounts id :-'||lc_chr_acc_seg1||'-'||lc_chr_acc_seg2||'-'||lc_chr_acc_seg3||'-'||lc_chr_acc_seg4||'-'||lc_chr_acc_seg5||'-'||lc_chr_acc_seg6||'-'||lc_chr_acc_seg7;
        END IF;
*/

        lc_line_ship_loc := NULL;
        lc_new_ship      := NULL;
        ln_ship_loc_id   := NULL;
        lc_ship_org      := NULL;

        OPEN lcu_ship_po_line(lr_po_dist_data.po_number
                              ,lr_po_dist_data.shipment_num
                              ,lr_po_dist_data.po_line_number);
        FETCH lcu_ship_po_line
        INTO lc_line_ship_loc;
        CLOSE lcu_ship_po_line;

        lc_new_ship := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                                ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                                ,lc_line_ship_loc
                                                                );

        OPEN lcu_hr_loc_org(lc_new_ship);
        FETCH lcu_hr_loc_org
        INTO ln_ship_loc_id
            ,lc_ship_org;
        CLOSE lcu_hr_loc_org;

        IF lc_ship_org = 'CSD' THEN
          OPEN lcu_charge_account_id(ln_ship_loc_id,'UK_MAIN');
          FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
          CLOSE lcu_charge_account_id;
          IF ln_charge_acct_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
          END IF;
        ELSIF lc_ship_org = 'CSI' THEN
          OPEN lcu_charge_account_id(ln_ship_loc_id,'CSI_MAIN');
          FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
          CLOSE lcu_charge_account_id;
          IF ln_charge_acct_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
          END IF;
        ELSIF lc_ship_org = 'CSL' THEN
          OPEN lcu_charge_account_id(ln_ship_loc_id,'CSL_MAIN');
          FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
          CLOSE lcu_charge_account_id;
          IF ln_charge_acct_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
          END IF;
        ELSIF lc_ship_org = 'CDC' THEN
          OPEN lcu_charge_account_id(ln_ship_loc_id,'AK1_COM');
          FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
          CLOSE lcu_charge_account_id;
          IF ln_charge_acct_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
          END IF;
        ELSE
          OPEN lcu_charge_account_id(ln_ship_loc_id,'RM_COM');
          FETCH lcu_charge_account_id INTO ln_charge_acct_id ;
          CLOSE lcu_charge_account_id;
          IF ln_charge_acct_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive charge of accounts id ';
          END IF;
        END IF;

        --Derive budget account id
        lc_errloc := 'Derive budget account id';
        OPEN  lcu_account_id (lc_bud_acc_seg1
                             ,lc_bud_acc_seg2
                             ,lc_bud_acc_seg3
                             ,lc_bud_acc_seg4
                             ,lc_bud_acc_seg5
                             ,lc_bud_acc_seg6
                             ,lc_bud_acc_seg7);
        FETCH lcu_account_id
        INTO  ln_budget_acct_id;
        CLOSE lcu_account_id;

        --Derive accrual account id
        lc_errloc := 'Derive accrual account id';
        OPEN  lcu_account_id (lc_accr_acc_seg1
                             ,lc_accr_acc_seg2
                             ,lc_accr_acc_seg3
                             ,lc_accr_acc_seg4
                             ,lc_accr_acc_seg5
                             ,lc_accr_acc_seg6
                             ,lc_accr_acc_seg7);
        FETCH lcu_account_id
        INTO  ln_accrual_acct_id;
        CLOSE lcu_account_id;

        --Derive variance account id
        lc_errloc := 'Derive variance account id';
        OPEN  lcu_account_id (lc_var_acc_seg1
                             ,lc_var_acc_seg2
                             ,lc_var_acc_seg3
                             ,lc_var_acc_seg4
                             ,lc_var_acc_seg5
                             ,lc_var_acc_seg6
                             ,lc_var_acc_seg7);
        FETCH lcu_account_id
        INTO  ln_variance_acct_id;
        CLOSE lcu_account_id;

        IF lc_error_details IS NULL THEN

            --Insert purchase order distrbutions data in interface table
            lc_errloc := 'Insert purchase order distrbutions data in interface table';
            INSERT INTO po_distributions_interface
            (interface_line_id
            ,interface_header_id
            ,interface_distribution_id
            ,process_code
            ,distribution_num
            ,org_id
            ,quantity_ordered
            ,quantity_delivered
            ,quantity_billed
            ,quantity_cancelled
            ,rate_date
            ,rate
            ,deliver_to_location_id
            ,deliver_to_person_id
            ,destination_type_code
            ,destination_organization
            ,destination_context
            ,set_of_books
            ,charge_account_id
            ,budget_account_id
            ,accrual_account_id
            ,variance_account_id
            ,amount_billed
            ,accrue_on_receipt_flag
            ,accrued_flag
            ,prevent_encumbrance_flag
            ,encumbered_flag
            ,gl_cancelled_date
            --,requisition_num
            --,requisition_line_num
            --,req_distribution_num
            ,project_accounting_context
            ,recoverable_tax
            ,nonrecoverable_tax
           -- ,recovery_rate
           -- ,tax_recovery_override_flag
            ,last_update_date
            ,last_updated_by
            ,creation_date
            ,created_by
            )
            VALUES
            (ln_po_int_line_id
            ,ln_po_int_header_id
            ,po_distributions_interface_s.nextval
            ,'PENDING'
            ,lr_po_dist_data.distribution_num
            ,ln_org_id
            ,(lr_po_dist_data.quantity_ordered - lr_po_dist_data.quantity_delivered - lr_po_dist_data.quantity_cancelled)
            ,0 --lr_po_dist_data.quantity_delivered
            ,0 --lr_po_dist_data.quantity_billed
            ,0 --lr_po_dist_data.quantity_cancelled
            ,lr_po_dist_data.rate_date
            ,lr_po_dist_data.rate
            ,ln_location_id
            ,ln_person_id
            ,lr_po_dist_data.destination_type_code
            ,lc_new_org_name --lr_po_dist_data.destination_organization
            ,lr_po_dist_data.destination_context
            ,ln_sob_id
            ,ln_charge_acct_id
            ,ln_budget_acct_id
            ,ln_accrual_acct_id
            ,ln_variance_acct_id
            ,lr_po_dist_data.amount_billed
            ,'Y' -- lr_po_dist_data.accrue_on_receipt_flag as per MDT version 1.23
            ,lr_po_dist_data.accrued_flag
            ,lr_po_dist_data.prevent_encumbrance_flag
            ,lr_po_dist_data.encumbered_flag
            ,lr_po_dist_data.gl_cancelled_date
            --,lr_po_dist_data.requisition_num
            --,lr_po_dist_data.requisition_line_num
            --,lr_po_dist_data.req_distribution_num
            ,lr_po_dist_data.project_accounting_context
            ,lr_po_dist_data.recoverable_tax
            ,lr_po_dist_data.nonrecoverable_tax
           -- ,lr_po_dist_data.recovery_rate
           -- ,lr_po_dist_data.tax_recovery_override_flag
            ,SYSDATE
            ,fnd_global.user_id
            ,SYSDATE
            ,fnd_global.user_id
            );

            UPDATE xxcsr_po_distributions_int
            SET    record_status = 'C'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = NULL
            WHERE rowid          = lr_po_dist_data.rowid;

            COMMIT;

            ln_dist_cnt := ln_dist_cnt + 1;

        ELSE
          UPDATE xxcsr_po_distributions_int
          SET    record_status = 'E'
                ,request_id    = FND_GLOBAL.conc_request_id
                ,error_message = lc_error_details
          WHERE  rowid         = lr_po_dist_data.rowid;

          IF ln_po_int_line_id IS NOT NULL THEN
             UPDATE xxcsr_po_lines_int
             SET record_status = 'E'
                ,error_message = error_message||'error in distribution record'
             WHERE  po_number = lr_po_dist_data.po_number
             AND line_num = lr_po_dist_data.po_line_number
             AND shipment_num = lr_po_dist_data.shipment_num
             AND source_instance = p_instance_name;

             DELETE FROM po_lines_interface
             WHERE interface_line_id = ln_po_int_line_id;

             ln_lines_cnt := ln_lines_cnt - 1;

           END IF;

         IF ln_po_int_header_id IS NOT NULL THEN
           UPDATE xxcsr_po_headers_int
           SET record_status = 'E'
              ,error_message = error_message||'error in distribution record'
           WHERE  po_number = lr_po_dist_data.po_number
           AND source_instance = p_instance_name;

           DELETE FROM po_headers_interface
           WHERE interface_header_id = ln_po_int_header_id;

           ln_header_cnt := ln_header_cnt - 1;

          END IF;
        END IF; --lc_error_details IS NULL

        COMMIT;
    END LOOP;
    COMMIT;

    IF ln_header_cnt > 0 THEN

        --Calling Import Standard Purchase Orders program
        lc_errloc     := 'Call Import Standard Purchase Orders program';

        FND_FILE.PUT_LINE(FND_FILE.LOG,'Submitting Import Standard Purchase Orders concurrent program.');

        ln_cur_org_id := FND_PROFILE.VALUE('ORG_ID');

        FND_FILE.PUT_LINE(FND_FILE.LOG,'ln_cur_org_id : ' || ln_cur_org_id);

        ln_request_id := FND_REQUEST.SUBMIT_REQUEST (
                                                      application => 'PO'
                                                     ,program     => 'POXPOPDOI'
                                                     ,description =>  'Import Standard Purchase Orders'
                                                     ,start_time  =>  SYSDATE
                                                     ,sub_request =>  FALSE
                                                     ,argument1   =>  NULL --Default Buyer
                                                     ,argument2   =>  'STANDARD' --Document Type
                                                     ,argument3   =>  NULL --Document SubType
                                                     ,argument4   =>  'N' --Create or Update Items
                                                     ,argument5   =>  NULL --Create Sourcing Rules
                                                     ,argument6   =>  'APPROVED' --Approval Status
                                                     ,argument7   =>  NULL --Release Generation Method
                                                     ,argument8   =>  NULL --Batch Id
                                                     ,argument9   =>  ln_cur_org_id --Operating Unit
                                                     ,argument10  =>  NULL --Global Agreement
                                                     ,argument11  =>  NULL --Enable Sourcing Level
                                                     ,argument12  =>  NULL --Sourcing Level
                                                     ,argument13  =>  NULL --Inv Org Enable
                                                     ,argument14  =>  NULL --Inventory Organization
                                                    );
        COMMIT;

        IF ln_request_id = 0 THEN
            lc_error := 'Error submitting Import Standard Purchase Orders concurrent program.';
            RAISE ex_conc_error;
        END IF;

        FND_FILE.PUT_LINE(FND_FILE.LOG,'Import Standard Purchase Orders concurrent program submitted. Request_id : ' || ln_request_id);

        --Wait for the request to complete
        lb_wait := FND_CONCURRENT.WAIT_FOR_REQUEST
               (
                 request_id  => ln_request_id
                ,interval    => 2
                ,phase       => lc_phase
                ,status      => lc_status
                ,dev_phase   => lc_devpha
                ,dev_status  => lc_devsta
                ,message     => lc_mesg
               );

    END IF; --ln_header_cnt > 0

    x_header_cnt := ln_header_cnt;
    x_lines_cnt  := ln_lines_cnt;
    x_dist_cnt   := ln_dist_cnt;
    x_req_id     := ln_request_id;

-- Contracts import

    FOR lr_po_header_contr IN lcu_po_header_contr (p_rec_status)
    LOOP

        ln_org_id                   := NULL;
        ln_agent_id                 := NULL;
        ln_bu_id                    := NULL;
        lc_currency_code            := NULL;
        ln_vendor_id                := NULL;
        lc_payment_terms            := NULL;
        lc_freight_terms            := NULL;

        lc_error_details            := NULL;
        lc_new_orgname              := NULL;
        lc_business_group           := NULL;
        ln_bill_loc_id              := NULL;
        ln_ship_loc_id              := NULL;
        lc_inv_po_chk               := NULL;
        lc_func_curr                := NULL;
        lc_rate_type                := NULL;
        ln_rate                     := NULL;
        ld_rate_date                := NULL;
        lc_new_ship                 := NULL;
        lc_new_bill                 := NULL;
        ln_vendor_site_id           := NULL;
        ln_vendor_contact_id        := NULL;
        ln_term_id                  := NULL;
        lc_po_exists                := NULL;

        -- check if PO already exists

        OPEN lcu_check_po_exists(lr_po_header_contr.po_number);
        FETCH lcu_check_po_exists
        INTO lc_po_exists;
        CLOSE lcu_check_po_exists;

        IF lc_po_exists IS NOT NULL THEN
          lc_error_details := lc_error_details||'PO already exists';
        END IF;

        lc_errloc   := 'Operating unit related derivations';
        lc_new_orgname := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_OPERATING_UNIT'
                                                              ,'XXSIRF_CNV_OPERATING_UNIT'
                                                              ,lr_po_header_contr.operating_unit
                                                              );

        IF lc_new_orgname IS NULL THEN
          lc_error_details := lc_error_details||'Could not derive new operating unit for '||lr_po_header_contr.operating_unit||'. ';
        ELSE
          ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lc_new_orgname);
          IF ln_org_id IS NULL THEN
            lc_error_details := lc_error_details||'Could not derive org id for new operating unit '||lc_new_orgname||'. ';
          ELSE
            -- Compare functional and document currency
            OPEN lcu_func_curr(ln_org_id);
            FETCH lcu_func_curr
            INTO lc_func_curr;
            CLOSE lcu_func_curr;

            IF lc_func_curr = lr_po_header_contr.currency_code THEN
              lc_rate_type := NULL;
              ln_rate      := NULL;
              ld_rate_date := NULL;
            ELSE
              lc_rate_type := lr_po_header_contr.rate_type;
              ln_rate      := lr_po_header_contr.rate;
              ld_rate_date := lr_po_header_contr.rate_date;

            END IF;
          END IF;
        END IF;

    /*    lc_errloc := 'Checking for inventory purchase orders';
        lc_inv_po_chk := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                              ,'XXCSR_CNV_INV_SUPPLIERS'
                                                              ,'XXSIRF_CNV_INV_SUPPLIERS'
                                                              ,lr_po_header_contr.vendor_name
                                                              );

        IF lc_inv_po_chk IS NULL THEN
          lc_error_details := lc_error_details||'Only inventory purchase orders will be migrated';
        END IF; */


   /*   Commented by Nishka, added the above derivation
        --Derive org id
        lc_errloc := 'Derive org id';
        ln_org_id := XXCSR_COMMON_PKG.get_organization_id(lr_po_header_contr.operating_unit);

        IF ln_org_id IS NULL THEN
            lc_error_details :=  'Invalid operating unit name.';
        END IF;   */

   /*     OPEN  lcu_bu_id (lr_po_header_contr.emp_bu_name);
        FETCH lcu_bu_id
        INTO  ln_bu_id;
        CLOSE lcu_bu_id; */

--------------        IF lr_po_header_contr.emp_bu_name IN ( 'CSR United States','CSR United Kingdom') THEN                ----------commented on 17oct2011
        IF lr_po_header_contr.emp_bu_name IN ( 'CSR United States','CSR United Kingdom') THEN              
          lc_business_group := lr_po_header_contr.emp_bu_name;
        ELSE
          lc_business_group := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                   ,'XXCSR_CNV_HR_BG'
                                                                   ,'XXSIRF_CNV_HR_BG'
                                                                   ,lr_po_header_contr.emp_bu_name
                                                                   );
        END IF;
        IF lc_business_group IS NULL THEN
          lc_error_details := lc_error_details||'Mapping for Business Group '||lr_po_header_contr.emp_bu_name||' could not be found. ';
        ELSE
          ln_bu_id := XXCSR_COMMON_PKG.get_business_group_id(lc_business_group);
          IF ln_bu_id IS NULL THEN
            lc_error_details:= lc_error_details||' Business Group ID for '||lc_business_group||' could not be derived. ';
          ELSE
            --Derive agent id
            lc_errloc := 'Derive agent id';
            ln_agent_id := XXCSR_COMMON_PKG.get_person_id (p_first_name        => lr_po_header_contr.emp_fname
                                                          ,p_last_name         => lr_po_header_contr.emp_lname
                                                          ,p_date_of_birth     => lr_po_header_contr.emp_dob
                                                          ,p_business_group_id => ln_bu_id
                                                          );
            IF ln_agent_id IS NULL THEN
                lc_error_details := lc_error_details || ' Invalid agent (employee) details.';
            END IF;

          END IF;
        END IF;



        --Check currency code
        lc_errloc := 'Check currency code';
        OPEN  lcu_chk_currency (lr_po_header_contr.currency_code);
        FETCH lcu_chk_currency
        INTO  lc_currency_code;

        IF lcu_chk_currency%NOTFOUND THEN
            lc_error_details := lc_error_details || ' Invalid currency code.';
        END IF;

        CLOSE lcu_chk_currency;

        --Derive vendor id
        lc_errloc := 'Derive vendor id';

        IF lr_po_header_contr.vendor_name IS NOT NULL THEN
          OPEN lcu_vendor_der(lr_po_header_contr.vendor_name);
          FETCH lcu_vendor_der
          INTO  ln_vendor_id;
          CLOSE lcu_vendor_der;

          IF ln_vendor_id IS NULL THEN
              lc_error_details := lc_error_details || ' Vendor not migrated.';
          END IF;
        END IF;


        lc_errloc := 'Check payment terms';
        IF lr_po_header_contr.payment_terms IS NOT NULL THEN
        lc_payment_terms := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                ,p_csr_lookup    => 'XXCSR_CNV_SUPPLIER_PAYTERMS'
                                                                ,p_sirf_lookup   => 'XXSIRF_CNV_SUPPLIER_PAYTERMS'
                                                                ,p_old_value     => lr_po_header_contr.payment_terms
                                                                );

        IF lc_payment_terms IS NULL THEN
            lc_error_details := lc_error_details || ' Invalid payment terms.';
        ELSE
          OPEN lcu_pay_term(lc_payment_terms);
          FETCH lcu_pay_term
          INTO ln_term_id;
          CLOSE lcu_pay_term;

          IF ln_term_id IS NULL THEN
            lc_error_details := lc_error_details || 'Could not derive payment term id for '||lc_payment_terms;
          END IF;
        END IF;
        END IF;

        --Check freight terms
        lc_errloc := 'Check freight terms';
        IF lr_po_header_contr.freight_terms IS NOT NULL THEN
          lc_freight_terms := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name => p_instance_name
                                                                  ,p_csr_lookup    => 'XXCSR_CNV_FREIGHT_TERMS'
                                                                  ,p_sirf_lookup   => 'XXSIRF_CNV_FREIGHT_TERMS'
                                                                  ,p_old_value     => lr_po_header_contr.freight_terms
                                                                  );

          IF lc_freight_terms IS NULL THEN
              lc_error_details := lc_error_details || ' Invalid freight terms.';
          END IF;

        END IF;

        IF lr_po_header_contr.ship_to_location IS NOT NULL THEN

          lc_new_ship := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_DELIVER_LOCATIONS'
                                                                ,'XXSIRF_CNV_DELIVER_LOCATIONS'
                                                                ,lr_po_header_contr.ship_to_location
                                                                );
          IF lc_new_ship IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new ship to location for '||lr_po_header_contr.ship_to_location||' in XXCSR_CNV_DELIVER_LOCATIONS. ';

          ELSE

            OPEN lcu_hr_loc(lc_new_ship);
            FETCH lcu_hr_loc
            INTO ln_ship_loc_id;
            CLOSE lcu_hr_loc;

            IF ln_ship_loc_id IS NULL THEN
              lc_error_details := lc_error_details || 'Ship to location id could not be derived for '||lr_po_header_contr.ship_to_location;
            END IF;
          END IF;

        END IF;

        IF lr_po_header_contr.bill_to_location IS NOT NULL THEN

          lc_new_bill := XXCSR_COMMON_PKG.get_lookup_meaning (p_instance_name
                                                                ,'XXCSR_CNV_BILLTO_LOCATIONS'
                                                                ,'XXSIRF_CNV_BILLTO_LOCATIONS'
                                                                ,lr_po_header_contr.bill_to_location
                                                                );
          IF lc_new_bill IS NULL THEN

            lc_error_details := lc_error_details||'Could not derive new bill to location for '||lr_po_header_contr.bill_to_location||' in XXCSR_CNV_BILLTO_LOCATIONS. ';

          ELSE

            OPEN lcu_hr_loc(lc_new_bill);
            FETCH lcu_hr_loc
            INTO ln_bill_loc_id;
            CLOSE lcu_hr_loc;

            IF ln_bill_loc_id IS NULL THEN
              lc_error_details := lc_error_details || 'Bill to location id could not be derived for '||lr_po_header_contr.bill_to_location;
            END IF;

          END IF;

        END IF;


        OPEN lcu_vendor_site(lr_po_header_contr.vendor_site_code
                            ,ln_vendor_id);
        FETCH lcu_vendor_site
        INTO ln_vendor_site_id;
        CLOSE lcu_vendor_site;

        OPEN lcu_vendor_contact(ln_vendor_site_id);
        FETCH lcu_vendor_contact
        INTO ln_vendor_contact_id;
        CLOSE lcu_vendor_contact;

 /*       ln_line_item_cnt := NULL;
        ln_po_item_chk   := NULL;
        -- Check whether the po line has valid items.
        OPEN lcu_po_line_itm(lr_po_header_contr.po_number);
        FETCH lcu_po_line_itm
        INTO ln_line_item_cnt;
        CLOSE lcu_po_line_itm;

        IF ln_line_item_cnt != 0 THEN

          OPEN lcu_po_itm_chk(lr_po_header_contr.po_number);
          FETCH lcu_po_itm_chk
          INTO ln_po_item_chk;
          CLOSE lcu_po_itm_chk;

          IF ln_po_item_chk = 0 THEN
            lc_error_details := lc_error_details || 'Inventory item id for po line could not be derived';
          END IF;
        END IF; */

        IF lc_error_details IS NULL THEN

            --Insert purchase order header data in interface table
            lc_errloc := 'Insert purchase order header data in interface table';

          BEGIN
         
          ln_po_header_id := NULL;
          ln_po_header_id := PO_HEADERS_S.nextval;

          INSERT INTO po_headers_all (
              po_header_id
             ,agent_id
             ,type_lookup_code
             ,created_by
              ,creation_date
              ,last_update_date
              ,last_updated_by
              ,last_update_login
             ,request_id
             ,segment1
             ,summary_flag
             ,enabled_flag
             ,vendor_id
             ,vendor_site_id
             ,vendor_contact_id
             ,ship_to_location_id
             ,bill_to_location_id
             ,terms_id
             ,currency_code
             ,rate_type
             ,rate_date
             ,rate
             ,authorization_status
             ,revision_num
             ,approved_flag
             ,approved_date
             ,print_count
             ,confirming_order_flag
             ,comments
             ,acceptance_required_flag
             ,cancel_flag
             ,frozen_flag
             ,closed_code
            ,org_id
            ,interface_source_code
         --  ,wf_item_type
         --  ,wf_item_key
             ,xml_flag
             ,document_creation_method
             ,submit_date
             ,style_id
             ,freight_terms_lookup_code
             ,attribute_category
             ,pay_on_code
             ,global_agreement_flag
              )
              VALUES
              (
              ln_po_header_id
             ,ln_agent_id
             ,'CONTRACT'
             ,FND_GLOBAL.USER_ID
             ,SYSDATE
             ,SYSDATE
             ,FND_GLOBAL.USER_ID
             ,FND_GLOBAL.LOGIN_ID
             ,gn_conc_req_id
             ,lr_po_header_contr.po_number
             ,'N'
             ,'Y'
             ,ln_vendor_id
             ,ln_vendor_site_id
             ,ln_vendor_contact_id
             ,ln_ship_loc_id -- lr_po_header_contr.ship_to_location
             ,ln_bill_loc_id -- lr_po_header_contr.bill_to_location
             ,ln_term_id
             ,lr_po_header_contr.currency_code
             ,DECODE(lc_rate_type,'Corporate','User',lc_rate_type)
             ,ld_rate_date
             ,ln_rate
             ,DECODE(lr_po_header_contr.approval_status,'REQUIRES REAPPROVAL','INCOMPLETE',lr_po_header_contr.approval_status)
             ,0
             ,lr_po_header_contr.approved_flag
             ,lr_po_header_contr.approved_date
             --,DECODE(lr_po_header_contr.approval_status,'APPROVED',lr_po_header_contr.print_count,0)
             ,DECODE(lr_po_header_contr.approval_status,'APPROVED',1,0)
             ,lr_po_header_contr.confirming_order_flag
             ,lr_po_header_contr.comments
             ,lr_po_header_contr.acceptance_required_flag
             ,'N'
             ,lr_po_header_contr.frozen_flag
             ,lr_po_header_contr.closed_code
      ,ln_org_id
      ,'XXCSR Conversion'
     -- ,-- wf_item_type
     -- ,-- wf_item_key
      ,'N'
      ,'PDOI'
      ,SYSDATE
      , 1 -- style_id
      ,lc_freight_terms
      ,lr_po_header_contr.attribute_category
      ,lr_po_header_contr.pay_on_code
      ,'Y'
              );
             
      INSERT INTO PO_GA_ORG_ASSIGNMENTS(PO_HEADER_ID          
                                       ,ORGANIZATION_ID       
                                       ,ENABLED_FLAG          
                                       ,VENDOR_SITE_ID        
                                       ,LAST_UPDATE_DATE      
                                       ,LAST_UPDATED_BY       
                                       ,CREATION_DATE         
                                       ,CREATED_BY            
                                       ,LAST_UPDATE_LOGIN     
                                       ,PURCHASING_ORG_ID     
                                       ,ORG_ASSIGNMENT_ID                   
                                       )
                                VALUES (ln_po_header_id
                                       ,ln_org_id
                                       ,'Y'
                                       ,ln_vendor_site_id
                                       ,SYSDATE
                                       ,FND_GLOBAL.USER_ID
                                       ,SYSDATE
                                       ,FND_GLOBAL.USER_ID
                                       ,FND_GLOBAL.LOGIN_ID
                                       ,ln_org_id
                                       ,PO_GA_ORG_ASSIGNMENTS_S.NEXTVAL
                                       );
             
             
             

            UPDATE xxcsr_po_headers_int
            SET    record_status = 'C'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = NULL
            WHERE rowid = lr_po_header_contr.rowid;

            COMMIT;

          EXCEPTION
          WHEN OTHERS THEN
            lc_error_details := SQLERRM;
            UPDATE xxcsr_po_headers_int
            SET    record_status = 'E'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = lc_error_details
            WHERE  rowid = lr_po_header_contr.rowid;
          END;

           -- ln_header_cnt := ln_header_cnt + 1;

        ELSE
            UPDATE xxcsr_po_headers_int
            SET    record_status = 'E'
                  ,request_id    = FND_GLOBAL.conc_request_id
                  ,error_message = lc_error_details
            WHERE  rowid = lr_po_header_contr.rowid;
        END IF; --lc_error_details IS NULL

        COMMIT;
    END LOOP;
    COMMIT;

--
EXCEPTION
  WHEN EX_CONC_ERROR THEN
    x_retcode := 2;
    x_errbuf  := lc_errmsg;
    FND_FILE.PUT_LINE(FND_FILE.LOG,lc_errmsg);

  WHEN OTHERS THEN
    x_errbuf  := SUBSTR(SQLERRM,1,200);
    x_retcode := 2;
    FND_FILE.PUT_LINE(FND_FILE.LOG,'Unexpected error occurred in import procedure at ' || lc_errloc || ' Error : ' || SQLERRM);

END import_zoran;

-----=========================End Import ZORAN procedure=====================

--Main Routine
PROCEDURE main
   (x_errbuf          OUT NOCOPY VARCHAR2
   ,x_retcode         OUT NOCOPY NUMBER
   ,p_data_file1      IN         VARCHAR2
   ,p_data_file2      IN         VARCHAR2
   ,p_data_file3      IN         VARCHAR2
   ,p_instance_name   IN         VARCHAR2
   ,p_process_error   IN             VARCHAR2
   )
IS
--
    CURSOR lcu_get_conc_info(pn_request_id NUMBER)
    IS
        SELECT  FCR.status_code
               ,FCR.phase_code
        FROM    fnd_concurrent_requests FCR
        WHERE   FCR.request_id = pn_request_id;

    lc_errloc         VARCHAR2(1000);
    ln_load_req_id    NUMBER;
    ln_load2_req_id   NUMBER;
    ln_imp_req_id     NUMBER;

    lc_errmsg         VARCHAR2(2000);
    lc_phase          VARCHAR2(500);
    lc_status         VARCHAR2(500);
    lc_devpha         VARCHAR2(500);
    lc_devsta         VARCHAR2(500);
    lc_mesg           VARCHAR2(50);
    lb_wait           BOOLEAN;

    lc_errbuf         VARCHAR2(500);
    ln_hdr_tab_retcode  NUMBER;
    ln_lin_tab_retcode  NUMBER;
    ln_dist_tab_retcode NUMBER;
    ln_hdr_stg_retcode  NUMBER;
    ln_lin_stg_retcode  NUMBER;
    ln_dist_stg_retcode NUMBER;
    ln_tab_retcode      NUMBER;
    ln_retcode          NUMBER;

    ln_po_header_cnt    NUMBER;
    ln_po_lines_cnt     NUMBER;
    ln_po_dist_cnt      NUMBER;

    ex_conc_error     EXCEPTION;
    ex_cust_error     EXCEPTION;

BEGIN

    --Check if process error is set to Yes
    IF p_process_error = 'Y' THEN

      UPDATE xxcsr_po_headers_int
      SET    request_id    = gn_conc_req_id
      WHERE  record_status ='E'
      AND    source_instance = p_instance_name;

      UPDATE xxcsr_po_lines_int
      SET    request_id    = gn_conc_req_id
      WHERE  record_status ='E'
      AND    source_instance = p_instance_name;

      UPDATE xxcsr_po_distributions_int
      SET    request_id    = gn_conc_req_id
      WHERE  record_status ='E'
      AND    source_instance = p_instance_name;

      update xxcsr_po_headers_int
      set emp_fname = 'Scott'
      , emp_lname = 'Simpson'
      , emp_dob =  to_date('02/26/1972','MM/DD/RRRR')
      , emp_bu_name = 'CSR United Kingdom'
      where emp_fname = 'Person'
      and emp_lname = 'Migration 2'
      AND    source_instance = p_instance_name;

      update xxcsr_po_headers_int
      set emp_fname = 'Mary'
      , emp_lname = 'Benak'
      , emp_dob =  to_date('12/04/1964','MM/DD/RRRR')
      , emp_bu_name = 'CSR United States'
      where emp_fname = 'Wee Kee'
      and emp_lname = 'Lim'
      AND    source_instance = p_instance_name;

      COMMIT;

        --Call Import routine for processing of error records
      IF p_instance_name = 'CSR' THEN
        import_csr(p_rec_status    => 'E'
                  ,p_instance_name => p_instance_name
                  ,x_errbuf        => lc_errbuf
                  ,x_retcode       => ln_retcode
                  ,x_header_cnt    => ln_po_header_cnt
                  ,x_lines_cnt     => ln_po_lines_cnt
                  ,x_dist_cnt      => ln_po_dist_cnt
                  ,x_req_id        => ln_imp_req_id);

        --import_csr('E',p_instance_name,lc_errbuf,ln_retcode,ln_po_header_cnt,ln_po_lines_cnt,ln_po_dist_cnt,ln_imp_req_id);
--============================================ZORAN instace logic added on 17OCT2011========================
      ELSIF p_instance_name = 'ZORAN' THEN
      FND_FILE.PUT_LINE(FND_FILE.LOG,'Importing ZORAN instace');       
        import_zoran(p_rec_status    => 'E'
                   ,p_instance_name => p_instance_name
                   ,x_errbuf        => lc_errbuf
                   ,x_retcode       => ln_retcode
                   ,x_header_cnt    => ln_po_header_cnt
                   ,x_lines_cnt     => ln_po_lines_cnt
                   ,x_dist_cnt      => ln_po_dist_cnt
                   ,x_req_id        => ln_imp_req_id);


      ELSE
        import_sirf(p_rec_status    => 'E'
                   ,p_instance_name => p_instance_name
                   ,x_errbuf        => lc_errbuf
                   ,x_retcode       => ln_retcode
                   ,x_header_cnt    => ln_po_header_cnt
                   ,x_lines_cnt     => ln_po_lines_cnt
                   ,x_dist_cnt      => ln_po_dist_cnt
                   ,x_req_id        => ln_imp_req_id);

        --import_sirf('E',p_instance_name,lc_errbuf,ln_retcode,ln_po_header_cnt,ln_po_lines_cnt,ln_po_dist_cnt,ln_imp_req_id);
      END IF;

    ELSE

        --Check and call loader for purchase order headers data file
        IF p_data_file1 IS NOT NULL THEN

            lc_errloc := 'Calling common loader program for data file : ' || p_data_file1;

            ln_load_req_id := FND_REQUEST.SUBMIT_REQUEST (
                                                            application => 'XXCSR'
                                                           ,program     => 'XXCSRLOAD'
                                                           ,description =>  NULL
                                                           ,start_time  =>  SYSDATE
                                                           ,sub_request =>  FALSE
                                                           ,argument1   =>  p_data_file1
                                                           ,argument2   =>  'XXCSR_PO_PUR_ORDR_HDR_IMPORT.ctl'
                                                           ,argument3   =>  FND_PROFILE.VALUE('XXCSR_DIRECTORY_PATH')
                                                          );
            COMMIT;

            IF ln_load_req_id = 0 THEN
                lc_errmsg := 'Error submitting loader concurrent program for data file ' || p_data_file1;
                RAISE ex_conc_error;
            END IF;

            FND_FILE.PUT_LINE(FND_FILE.LOG,'Common loader program submitted for data file : ' || p_data_file1 || ' Request_id : ' || ln_load_req_id);

            --Wait for the request to complete
            lb_wait := FND_CONCURRENT.WAIT_FOR_REQUEST
                   (
                     request_id  => ln_load_req_id
                    ,interval    => 2
                    ,phase       => lc_phase
                    ,status      => lc_status
                    ,dev_phase   => lc_devpha
                    ,dev_status  => lc_devsta
                    ,message     => lc_mesg
                   );

            -- Check if the load program completed execution normally. If not, stop further execution.
            IF (lcu_get_conc_info%ISOPEN) THEN
                CLOSE lcu_get_conc_info;
            END IF;

            OPEN  lcu_get_conc_info(ln_load_req_id);
            FETCH lcu_get_conc_info
            INTO  lc_devsta
                 ,lc_devpha;
            CLOSE lcu_get_conc_info;

            IF lc_devpha = 'C' AND lc_devsta <> 'C' THEN

                DELETE FROM xxcsr_po_headers_int
                WHERE  record_status = 'N'
                AND    source_instance = p_instance_name;

                COMMIT;

                lc_errmsg  := 'Loader program Failed. Please check Log file for request ID '|| ln_load_req_id;
                RAISE ex_conc_error;

            END IF;

        ELSE

            FND_FILE.PUT_LINE(FND_FILE.LOG,'Purchase Orders data file name is empty. No Purchase Orders created.');

        END IF; --p_data_file1 IS NOT NULL

        --Check and call loader for purchase order lines data file
        IF p_data_file2 IS NOT NULL THEN

            lc_errloc := 'Calling common loader program for data file : ' || p_data_file2;

            ln_load_req_id := FND_REQUEST.SUBMIT_REQUEST (
                                                            application => 'XXCSR'
                                                           ,program     => 'XXCSRLOAD'
                                                           ,description =>  NULL
                                                           ,start_time  =>  SYSDATE
                                                           ,sub_request =>  FALSE
                                                           ,argument1   =>  p_data_file2
                                                           ,argument2   =>  'XXCSR_PO_PUR_ORDR_LIN_IMPORT.ctl'
                                                           ,argument3   =>  FND_PROFILE.VALUE('XXCSR_DIRECTORY_PATH')
                                                          );
            COMMIT;

            IF ln_load_req_id = 0 THEN
                lc_errmsg := 'Error submitting loader concurrent program for data file ' || p_data_file2;
                RAISE ex_conc_error;
            END IF;

            FND_FILE.PUT_LINE(FND_FILE.LOG,'Common loader program submitted for data file : ' || p_data_file2 || ' Request_id : ' || ln_load_req_id);

            --Wait for the request to complete
            lb_wait := FND_CONCURRENT.WAIT_FOR_REQUEST
                   (
                     request_id  => ln_load_req_id
                    ,interval    => 2
                    ,phase       => lc_phase
                    ,status      => lc_status
                    ,dev_phase   => lc_devpha
                    ,dev_status  => lc_devsta
                    ,message     => lc_mesg
                   );

            -- Check if the load program completed execution normally. If not, stop further execution.
            IF (lcu_get_conc_info%ISOPEN) THEN
                CLOSE lcu_get_conc_info;
            END IF;

            OPEN  lcu_get_conc_info(ln_load_req_id);
            FETCH lcu_get_conc_info
            INTO  lc_devsta
                 ,lc_devpha;
            CLOSE lcu_get_conc_info;

            IF lc_devpha = 'C' AND lc_devsta <> 'C' THEN

                DELETE FROM xxcsr_po_headers_int
                WHERE  record_status = 'N'
                AND    source_instance = p_instance_name;

                DELETE FROM xxcsr_po_lines_int
                WHERE  record_status = 'N'
                AND    source_instance = p_instance_name;

                COMMIT;

                lc_errmsg  := 'Loader program Failed. Please check Log file for request ID '|| ln_load_req_id;
                RAISE ex_conc_error;

            END IF;

        ELSE

            FND_FILE.PUT_LINE(FND_FILE.LOG,'Purchase Order Lines data file name is empty. No Purchase Order Lines created.');

        END IF; --p_data_file2 IS NOT NULL


        --Check and call loader for purchase order distributions data file
        IF p_data_file3 IS NOT NULL THEN

            lc_errloc := 'Calling common loader program for data file : ' || p_data_file3;

            ln_load_req_id := FND_REQUEST.SUBMIT_REQUEST (
                                                            application => 'XXCSR'
                                                           ,program     => 'XXCSRLOAD'
                                                           ,description =>  NULL
                                                           ,start_time  =>  SYSDATE
                                                           ,sub_request =>  FALSE
                                                           ,argument1   =>  p_data_file3
                                                           ,argument2   =>  'XXCSR_PO_PUR_ORDR_DIST_IMPORT.ctl'
                                                           ,argument3   =>  FND_PROFILE.VALUE('XXCSR_DIRECTORY_PATH')
                                                          );
            COMMIT;

            IF ln_load_req_id = 0 THEN
                lc_errmsg := 'Error submitting loader concurrent program for data file ' || p_data_file3;
                RAISE ex_conc_error;
            END IF;

            FND_FILE.PUT_LINE(FND_FILE.LOG,'Common loader program submitted for data file : ' || p_data_file3 || ' Request_id : ' || ln_load_req_id);

            --Wait for the request to complete
            lb_wait := FND_CONCURRENT.WAIT_FOR_REQUEST
                   (
                     request_id  => ln_load_req_id
                    ,interval    => 2
                    ,phase       => lc_phase
                    ,status      => lc_status
                    ,dev_phase   => lc_devpha
                    ,dev_status  => lc_devsta
                    ,message     => lc_mesg
                   );

            -- Check if the load program completed execution normally. If not, stop further execution.
            IF (lcu_get_conc_info%ISOPEN) THEN
                CLOSE lcu_get_conc_info;
            END IF;

            OPEN  lcu_get_conc_info(ln_load_req_id);
            FETCH lcu_get_conc_info
            INTO  lc_devsta
                 ,lc_devpha;
            CLOSE lcu_get_conc_info;

            IF lc_devpha = 'C' AND lc_devsta <> 'C' THEN

                DELETE FROM xxcsr_po_headers_int
                WHERE  record_status = 'N'
                AND    source_instance = p_instance_name;

                DELETE FROM xxcsr_po_lines_int
                WHERE  record_status = 'N'
                AND    source_instance = p_instance_name;

                DELETE FROM xxcsr_po_distributions_int
                WHERE  record_status = 'N'
                AND    source_instance = p_instance_name;

                COMMIT;

                lc_errmsg  := 'Loader program Failed. Please check Log file for request ID '|| ln_load_req_id;
                RAISE ex_conc_error;

            END IF;

        ELSE

            FND_FILE.PUT_LINE(FND_FILE.LOG,'Purchase Order Distributions data file name is empty. No Purchase Order distributions created.');

        END IF; --p_data_file3 IS NOT NULL

        --Update the staging tables with request_id
        UPDATE xxcsr_po_headers_int
        SET    request_id    = gn_conc_req_id
        WHERE  record_status = 'N'
        AND    source_instance = p_instance_name;

        UPDATE xxcsr_po_lines_int
        SET    request_id    = gn_conc_req_id
        WHERE  record_status = 'N'
        AND    source_instance = p_instance_name;

        UPDATE xxcsr_po_distributions_int
        SET    request_id    = gn_conc_req_id
        WHERE  record_status = 'N'
        AND    source_instance = p_instance_name;

        COMMIT;
        update xxcsr_po_headers_int
        set emp_fname = 'Scott'
        , emp_lname = 'Simpson'
        , emp_dob =  to_date('02/26/1972','MM/DD/RRRR')
        , emp_bu_name = 'CSR United Kingdom'
        where emp_fname = 'Person'
        and emp_lname = 'Migration 2'
        AND    source_instance = p_instance_name;

        update xxcsr_po_headers_int
        set emp_fname = 'Mary'
        , emp_lname = 'Benak'
        , emp_dob =  to_date('12/04/1964','MM/DD/RRRR')
        , emp_bu_name = 'CSR United States'
        where emp_fname = 'Wee Kee'
        and emp_lname = 'Lim'
        AND    source_instance = p_instance_name;

        COMMIT;

        --Call Import program if all data is loaded successfully
        IF p_instance_name = 'CSR' THEN
          import_csr(p_rec_status    => 'N'
                    ,p_instance_name => p_instance_name
                    ,x_errbuf        => lc_errbuf
                    ,x_retcode       => ln_retcode
                    ,x_header_cnt    => ln_po_header_cnt
                    ,x_lines_cnt     => ln_po_lines_cnt
                    ,x_dist_cnt      => ln_po_dist_cnt
                    ,x_req_id        => ln_imp_req_id);

          --import_csr('E',p_instance_name,lc_errbuf,ln_retcode,ln_po_header_cnt,ln_po_lines_cnt,ln_po_dist_cnt,ln_imp_req_id);
        ELSE
          import_sirf(p_rec_status    => 'N'
                     ,p_instance_name => p_instance_name
                     ,x_errbuf        => lc_errbuf
                     ,x_retcode       => ln_retcode
                     ,x_header_cnt    => ln_po_header_cnt
                     ,x_lines_cnt     => ln_po_lines_cnt
                     ,x_dist_cnt      => ln_po_dist_cnt
                     ,x_req_id        => ln_imp_req_id);

          --import_sirf('N',p_instance_name,lc_errbuf,ln_retcode,ln_po_header_cnt,ln_po_lines_cnt,ln_po_dist_cnt,ln_imp_req_id);
        END IF;

    END IF; --p_process_error = 'Y'

    FND_FILE.PUT_LINE(FND_FILE.LOG,'ln_po_header_cnt : ' || ln_po_header_cnt);
    FND_FILE.PUT_LINE(FND_FILE.LOG,'ln_po_lines_cnt : '  || ln_po_lines_cnt);
    FND_FILE.PUT_LINE(FND_FILE.LOG,'ln_po_dist_cnt : '   || ln_po_dist_cnt);

    IF ln_imp_req_id IS NOT NULL THEN
      lc_errloc := 'Calling common routine : XXCSR_COMMON_PKG.interface_table_summary for xxcsr_po_headers_int';
      interface_table_summary(x_errbuf             => lc_errbuf
                             ,x_retcode            => ln_hdr_tab_retcode
                             ,p_interface_table    => 'po_headers_interface'
                             ,p_request_id         => ln_imp_req_id
                             ,p_total_record_count => ln_po_header_cnt
                             );

      lc_errloc := 'Calling common routine : XXCSR_COMMON_PKG.interface_table_summary for xxcsr_po_lines_int';
      interface_table_summary(x_errbuf             => lc_errbuf
                             ,x_retcode            => ln_lin_tab_retcode
                             ,p_interface_table    => 'po_lines_interface'
                             ,p_request_id         => ln_imp_req_id
                             ,p_total_record_count => ln_po_lines_cnt
                              );

      lc_errloc := 'Calling common routine : XXCSR_COMMON_PKG.interface_table_summary for xxcsr_po_distributions_int';
      interface_table_summary(x_errbuf             => lc_errbuf
                             ,x_retcode            => ln_dist_tab_retcode
                             ,p_interface_table    => 'po_distributions_interface'
                             ,p_request_id         => ln_imp_req_id
                             ,p_total_record_count => ln_po_dist_cnt
                              );

    END IF;

    lc_errloc := 'Calling common routine : XXCSR_COMMON_PKG.table_summary for XXCSR_PO_HEADERS_INT';
    XXCSR_COMMON_PKG.table_summary(lc_errbuf
                                  ,ln_hdr_stg_retcode
                                  ,'XXCSR_PO_HEADERS_INT'
                                  ,FND_GLOBAL.conc_request_id);

    lc_errloc := 'Calling common routine : XXCSR_COMMON_PKG.print_error for xxcsr_po_headers_int';
    XXCSR_COMMON_PKG.print_error(lc_errbuf,ln_retcode,'line_number','po_number','xxcsr_po_headers_int',FND_GLOBAL.conc_request_id,'XXCSR_PO_HEADERS_INT','LINE NUMBER','PURCHASE ORDER NUMBER');

    lc_errloc := 'Calling common routine : XXCSR_COMMON_PKG.table_summary for xxcsr_po_lines_int';
    XXCSR_COMMON_PKG.table_summary(lc_errbuf
                                  ,ln_lin_stg_retcode
                                  ,'XXCSR_PO_LINES_INT'
                                  ,FND_GLOBAL.conc_request_id);

    lc_errloc := 'Calling common routine : XXCSR_COMMON_PKG.print_error for xxcsr_po_lines_int';
    XXCSR_COMMON_PKG.print_error(lc_errbuf,ln_retcode,'line_number','po_number','xxcsr_po_lines_int',FND_GLOBAL.conc_request_id,'XXCSR_PO_LINES_INT','LINE NUMBER','PURCHASE ORDER NUMBER');

    lc_errloc := 'Calling common routine : XXCSR_COMMON_PKG.table_summary for xxcsr_po_distributions_int';
    XXCSR_COMMON_PKG.table_summary(lc_errbuf
                                  ,ln_dist_stg_retcode
                                  ,'XXCSR_PO_DISTRIBUTIONS_INT'
                                  ,FND_GLOBAL.conc_request_id);

    lc_errloc := 'Calling common routine : XXCSR_COMMON_PKG.print_error for xxcsr_po_distributions_int';
    XXCSR_COMMON_PKG.print_error(lc_errbuf,ln_retcode,'line_number','po_number','xxcsr_po_distributions_int',FND_GLOBAL.conc_request_id,'XXCSR_PO_DISTRIBUTIONS_INT','LINE NUMBER','PURCHASE ORDER NUMBER');

    IF ln_hdr_tab_retcode = 2 OR ln_lin_tab_retcode = 2 OR ln_dist_tab_retcode = 2 OR ln_hdr_stg_retcode = 2 OR ln_lin_stg_retcode = 2 OR ln_dist_stg_retcode = 2 THEN
        lc_errmsg := NULL;
        ln_tab_retcode := 2;
        RAISE ex_cust_error;
    END IF;

    IF ln_hdr_tab_retcode = 1 OR ln_lin_tab_retcode = 1 OR ln_dist_tab_retcode = 1 OR ln_hdr_stg_retcode = 1 OR ln_lin_stg_retcode = 1 OR ln_dist_stg_retcode = 1 THEN
        lc_errmsg := NULL;
        ln_tab_retcode := 1;
        RAISE ex_cust_error;
    END IF;

    IF ln_hdr_tab_retcode = 0 AND ln_lin_tab_retcode = 0 AND ln_dist_tab_retcode = 0 OR ln_hdr_stg_retcode = 0 OR ln_lin_stg_retcode = 0 OR ln_dist_stg_retcode = 0 THEN
        lc_errmsg := NULL;
        ln_tab_retcode := 0;
        RAISE ex_cust_error;
    END IF;
--
EXCEPTION
  WHEN EX_CONC_ERROR THEN
    x_retcode := 2;
    x_errbuf  := lc_errmsg;
    FND_FILE.PUT_LINE(FND_FILE.LOG,lc_errmsg);

  WHEN EX_CUST_ERROR THEN
    x_retcode := ln_tab_retcode;
    x_errbuf  := lc_errmsg;

  WHEN OTHERS THEN
    x_errbuf  := SUBSTR(SQLERRM,1,200);
    x_retcode := 2;
    FND_FILE.PUT_LINE(FND_FILE.LOG,'Unexpected error occurred in import procedure at ' || lc_errloc || ' Error : ' || SQLERRM);

END main;

END XXCSR_PO_PUR_ORDR_IMPORT_PKG;
/

SHOW ERRORS;

----EXIT;


-- +=================================================================+
-- | End of Script                                                   |
-- +=================================================================+

1 comment:

  1. Stop copying and pasting others code in your blog.

    I know you copied this from Oracle and you are not the author

    ReplyDelete