Thursday, 29 January 2015

API TO CANCEL PO invoice

AP_CANCEL_PKG.IS_INVOICE_CANCELLABLE:
Is_Invoice_Cancellable is a Function in the AP_CANCEL_PKG package that checks that an Invoice is cancellable or not when an Invoice Cancellation process starts.
It follows the following steps and returns a Boolean value depending on the result.

    If invoice contains distribution that does not have open GL period return FALSE.
    If invoice has an effective payment, return FALSE.
    If invoice is selected for payment, return FALSE.
    If invoice is already cancelled, return FALSE.
    If invoice is credited invoice, return FALSE.
    If invoices have been applied against this invoice, return FALSE.
    If invoice is matched to Finally Closed PO’s, return FALSE.
    If project related invoices have pending adjustments, return FALSE.
    If cancelling will cause qty_billed or amount_billed to less than 0, return FALSE.
    If none of above, invoice is cancellable return True.

Here is a small procedure to check if an Invoice is cancellable or not.
--*****************************************************************************
 CREATE OR REPLACE PROCEDURE XX_INV_CANCELLABLE (p_inv_id IN NUMBER)
is
v_boolean               BOOLEAN;
v_error_code            VARCHAR2(100);
v_debug_info            VARCHAR2(1000);
begin
v_boolean :=AP_CANCEL_PKG.IS_INVOICE_CANCELLABLE(
                P_invoice_id       => p_inv_id,
                P_error_code       => v_error_code,
                P_debug_info       => v_debug_info,
                P_calling_sequence => NULL);
IF v_boolean=TRUE
THEN
DBMS_OUTPUT.put_line ('Invoice '||p_inv_id|| ' is cancellable' );
ELSE
DBMS_OUTPUT.put_line ('Invoice '||p_inv_id|| ' is not cancellable :'|| v_error_code );
END IF;
End XX_INV_CANCELLABLE;

Execute XX_INV_CANCELLABLE(12960);

--************************************************************************
AP_CANCEL_PKG.AP_CANCEL_SINGLE_INVOICE:
AP_CANCEL_SINGLE_INVOICE is a Function in the AP_CANCEL_PKG package that cancels one invoice by executing the following sequence of steps, returning TRUE if successful and FALSE otherwise.
1] Check if the invoice is cancellable. if yes, proceed otherwise return false.
2] If invoice has tax withheld, undo withholding.
3] Clear out all payment schedules.
4] Cancel all the non-discard lines.
a. reverse matching
b. fetch the maximum distribution line number
c. Set encumbered flags to ‘N’
d. Accounting event generation
e. reverse the distributions
f. update Line level Cancelled information
5] Zero out the Invoice.
6] Run AutoApproval for this invoice.
7] Check posting holds remain on this cancelled invoice.
a. if NOT exist – complete the cancellation by updating header level information set return value to TRUE.
b. if exist – no update, set the return values to FALSE, NO DATA rollback.
8] Commit the Data.
9] Populate the out parameters.
Here is a small procedure to cancel a single invoice.

--*********************************************************

CREATE OR REPLACE PROCEDURE XX_INV_CANCEL(
                            P_xx_invoice_id IN NUMBER,
                            P_xx_last_updated_by IN  NUMBER,
                            P_xx_last_update_login IN  NUMBER,
                            P_xx_accounting_date IN  DATE)
is
v_boolean               BOOLEAN;
v_message_name          VARCHAR2(1000);
v_invoice_amount        NUMBER;
v_base_amount           NUMBER;
v_temp_cancelled_amount NUMBER;
v_cancelled_by          VARCHAR2(1000);
v_cancelled_amount      NUMBER;
v_cancelled_date        DATE;
v_last_update_date      DATE;
v_orig_prepay_amt       NUMBER;
v_pay_cur_inv_amt       NUMBER;
v_token                 VARCHAR2(100);
BEGIN
v_boolean := AP_CANCEL_PKG.AP_CANCEL_SINGLE_INVOICE
            (p_invoice_id                 => P_xx_invoice_id,
             p_last_updated_by            => P_xx_last_updated_by,
             p_last_update_login          => P_xx_last_update_login,
             p_accounting_date            => P_xx_accounting_date,
             p_message_name               => v_message_name,
             p_invoice_amount             => v_invoice_amount,
             p_base_amount                => v_base_amount,
             p_temp_cancelled_amount      => v_temp_cancelled_amount,
             p_cancelled_by               => v_cancelled_by,
             p_cancelled_amount           => v_cancelled_amount,
             p_cancelled_date             => v_cancelled_date,
             p_last_update_date           => v_last_update_date,
             p_original_prepayment_amount => v_orig_prepay_amt,
             p_pay_curr_invoice_amount    => v_pay_cur_inv_amt,
             P_Token                      => v_token,
             p_calling_sequence           => NULL
             );

IF v_boolean
THEN
DBMS_OUTPUT.put_line ('Successfully Cancelled the Invoice' );
COMMIT;
ELSE
DBMS_OUTPUT.put_line ('Failed to Cancel the Invoice' );
ROLLBACK;
END IF;

END XX_INV_CANCEL;

EXECUTE XX_INV_CANCEL(120573,2325,-1,SYSDATE);

Oracle Apps Cancel PO API Training

 Oracle Apps Cancel PO API Training
Following is a sample script to cancel the purchase order using Cancel PO API:

To cancel the whole PO, submit the cancel API using sqlplus as follows:


DECLARE
   l_return_status   VARCHAR2 (1);
BEGIN
   fnd_global.apps_initialize (1053, 50578, 201);
   -- mo_global.init('PO'); -- need for R12

   --call the Cancel API for PO
   PO_Document_Control_PUB.control_document (1.0,  -- p_api_version
                                             FND_API.G_TRUE, -- p_init_msg_list
                                             FND_API.G_TRUE,       -- p_commit
                                             l_return_status, -- x_return_status
                                             'PO',               -- p_doc_type
                                             'STANDARD',      -- p_doc_subtype
                                             NULL,                 -- p_doc_id
                                             '23975',             -- p_doc_num
                                             NULL,             -- p_release_id
                                             NULL,            -- p_release_num
                                             NULL,            -- p_doc_line_id
                                             NULL,           -- p_doc_line_num
                                             NULL,        -- p_doc_line_loc_id
                                             NULL,       -- p_doc_shipment_num
                                             'CANCEL',             -- p_action
                                             SYSDATE,         -- p_action_date
                                             NULL,          -- p_cancel_reason
                                             'N',        -- p_cancel_reqs_flag
                                             NULL,             -- p_print_flag
                                             NULL);        -- p_note_to_vendor

   -- Get any messages returned by the Cancel API
   FOR i IN 1 .. FND_MSG_PUB.count_msg
   LOOP
      DBMS_OUTPUT.put_line (
         FND_MSG_PUB.Get (p_msg_index => i, p_encoded => 'F'));
   END LOOP;
END;




The first run of the API will demonstrate cancelling shipment number 3 on line number 1 of the PO. Submit the cancel API using sqlplus as follows:

DECLARE
   l_return_status   VARCHAR2 (1);
BEGIN
   fnd_global.apps_initialize (1053, 50578, 201);
   -- mo_global.init('PO'); -- need for R12

   --call the Cancel API for PO
   PO_Document_Control_PUB.control_document (1.0,  -- p_api_version
                                             FND_API.G_TRUE, -- p_init_msg_list
                                             FND_API.G_TRUE,       -- p_commit
                                             l_return_status, -- x_return_status
                                             'PO',               -- p_doc_type
                                             'STANDARD',      -- p_doc_subtype
                                             NULL,                 -- p_doc_id
                                             '23975',             -- p_doc_num
                                             NULL,             -- p_release_id
                                             NULL,            -- p_release_num
                                             NULL,            -- p_doc_line_id
                                             '1',            -- p_doc_line_num
                                             NULL,        -- p_doc_line_loc_id
                                             '3',        -- p_doc_shipment_num
                                             'CANCEL',             -- p_action
                                             SYSDATE,         -- p_action_date
                                             NULL,          -- p_cancel_reason
                                             'N',        -- p_cancel_reqs_flag
                                             NULL,             -- p_print_flag
                                             NULL);        -- p_note_to_vendor

   -- Get any messages returned by the Cancel API
   FOR i IN 1 .. FND_MSG_PUB.count_msg
   LOOP
      DBMS_OUTPUT.put_line (
         FND_MSG_PUB.Get (p_msg_index => i, p_encoded => 'F'));
   END LOOP;
END;



This time, the API will be used to cancel all of line 1. Submit the cancel API using sqlplus as follows:
DECLARE
   l_return_status   VARCHAR2 (1);
BEGIN
   fnd_global.apps_initialize (1053, 50578, 201);
   -- mo_global.init('PO'); -- need for R12

   --call the Cancel API for PO
   PO_Document_Control_PUB.control_document (1.0,  -- p_api_version
                                             FND_API.G_TRUE, -- p_init_msg_list
                                             FND_API.G_TRUE,       -- p_commit
                                             l_return_status, -- x_return_status
                                             'PO',               -- p_doc_type
                                             'STANDARD',      -- p_doc_subtype
                                             NULL,                 -- p_doc_id
                                             '23975',             -- p_doc_num
                                             NULL,             -- p_release_id
                                             NULL,            -- p_release_num
                                             NULL,            -- p_doc_line_id
                                             '1',            -- p_doc_line_num
                                             NULL,        -- p_doc_line_loc_id
                                             NULL,       -- p_doc_shipment_num
                                             'CANCEL',             -- p_action
                                             SYSDATE,         -- p_action_date
                                             NULL,          -- p_cancel_reason
                                             'N',        -- p_cancel_reqs_flag
                                             NULL,             -- p_print_flag
                                             NULL);        -- p_note_to_vendor

-- Get any messages returned by the Cancel API
   FOR i IN 1 .. FND_MSG_PUB.count_msg
   LOOP
      DBMS_OUTPUT.put_line (
         FND_MSG_PUB.Get (p_msg_index => i, p_encoded => 'F'));
   END LOOP;
END;

Tuesday, 20 January 2015

Submit Standard concurrent Program from Back end In R12

DECLARE
ln_responsibility_id NUMBER;
ln_application_id     NUMBER;
ln_user_id           NUMBER;
ln_request_id            NUMBER;
BEGIN

--  FND_GLOBAL.APPS_INITIALIZE (ln_user_id,ln_responsibility_id,ln_application_id);
    FND_GLOBAL.APPS_INITIALIZE (1312,20434,101);

   -- Submitting Concurrent Request
  ln_request_id := FND_REQUEST.SUBMIT_REQUEST (
                            application   => 'XXSVL', -- Application Short Code
                            program       => 'XXCUSTCONVPRG', -- Concurrent Program Short NAme
                            description   => 'SVL Customer Conversion Program', -- Concurrent Program Description
                            start_time    => SYSDATE,
                            sub_request   => FALSE,
argument1     => 100 -- Parameter Value
  );

  COMMIT; -- It is Mandatory

  IF ln_request_id = 0
  THEN
     DBMS_OUTPUT.PUT_LINE ('Concurrent Request Failed to Submit.');
  ELSE
     DBMS_OUTPUT.PUT_LINE('Successfully Submitted the Concurrent Request. Request Id: '||ln_request_id);
  END IF;

EXCEPTION
WHEN OTHERS THEN
  DBMS_OUTPUT.PUT_LINE('Error While Submitting Concurrent Request '||TO_CHAR(SQLCODE)||'-'||SQLERRM);
END;
/ 

Submitting Concurrent Program From Back end / PL SQL

Pre-requisites :
Step1: Data Definition and Template to be created
Step2: Concurrent program needs to be created

Steps To Create the PL/SQL package:

1. Initialize the  Session Specific variable using fnd_global.APPS_INITIALIZE
2. Set The BI publisher report layout Before submitting the concurrent program
3. Submit the Concurrent Program

Code: (Tested in R12.1.1 )

DECLARE
   l_user_id              fnd_user.user_id%TYPE;
   l_resp_id              fnd_responsibility.responsibility_id%TYPE;
   l_resp_appl_id         fnd_application.application_id%TYPE;
   l_set_layout           boolean;
   l_request_id           NUMBER;
   l_phase                VARCHAR2 (100);
   l_status               VARCHAR2 (100);
   l_dev_phase            VARCHAR2 (100);
   l_dev_status           VARCHAR2 (100);
   l_wait_for_request     boolean := FALSE;
   l_get_request_status   boolean := FALSE;
   Output_layout_failed EXCEPTION;
   request_submission_failed EXCEPTION;
   request_completion_abnormal EXCEPTION;
BEGIN
   l_request_id := NULL;

   --
   -- Get the Apps Intilization Variables
   --
   SELECT   fnd.user_id, fresp.responsibility_id, fresp.application_id
     INTO   l_user_id, l_resp_id, l_resp_appl_id
     FROM   fnd_user fnd, fnd_responsibility_tl fresp
    WHERE   fnd.user_name = 'OEAG'
            AND fresp.responsibility_name = 'Custom XML Reports';

   --
   --Initialize the Apps Variables
   --
   fnd_global.APPS_INITIALIZE (user_id        => l_user_id,
                               resp_id        => l_resp_id,
                               resp_appl_id   => l_resp_appl_id);

   COMMIT;

   --
   -- Set the Layout  for BI Publisher Report
   --

   l_set_layout :=
      fnd_request.add_layout (template_appl_name   => 'XXERP',
                              template_code        => 'XXORACLEERPAPPSGUIDE',
                              --Data Template Code
                              template_language    => 'en',
                              template_territory   => 'US',
                              output_format        => 'PDF');

   IF l_set_layout
   THEN
      -- Submit the Request

      l_request_id :=
         fnd_request.submit_request (application   => 'XXERP',
                                     program       => 'XXOEAG_PG',
                                     description   => '',
                                     start_time    => SYSDATE,
                                     sub_request   => FALSE,
                                     argument1     => l_person_id);

      COMMIT;

      IF l_request_id > 0
      THEN
         --
         --waits for the request completion
         --

         l_wait_for_request :=
            fnd_concurrent.wait_for_request (request_id   => l_request_id,
                                             interval     => 60,
                                             max_wait     => 0,
                                             phase        => l_phase,
                                             status       => l_status,
                                             dev_phase    => l_dev_phase,
                                             dev_status   => l_dev_status,
                                             MESSAGE      => l_messase);

         COMMIT;

         --
         -- Get the Request Completion Status.
         --
         l_get_request_status :=
            fnd_concurrent.get_request_status (
               request_id       => l_request_id,
               appl_shortname   => NULL,
               program          => NULL,
               phase            => l_phase,
               status           => l_status,
               dev_phase        => l_dev_phase,
               dev_status       => l_dev_status,
               MESSAGE          => l_messase
            );

         --
         --Check the status if It IS completed Normal Or Not
         --
         IF UPPER (l_dev_phase) != 'COMPLETED'
            AND UPPER (l_dev_status) != 'NORMAL'
         THEN
            RAISE request_completion_abnormal;
         END IF;
      ELSE
         RAISE request_submission_failed;
      END IF;
   ELSE
      RAISE Output_layout_failed;
   END IF;

   p_request_id := l_request_id;
EXCEPTION
   WHEN Output_layout_failed
   THEN
      DBMS_OUTPUT.put_line ('Out put Layout failed');
   WHEN request_submission_failed
   THEN
      DBMS_OUTPUT.put_line ('Concurrent request submission failed');
   WHEN request_completion_abnormal
   THEN
      DBMS_OUTPUT.put_line (
         'Submitted request completed with error' || l_request_id
      );
   WHEN OTHERS
   THEN
      DBMS_OUTPUT.put_line ('ERROR:' || SUBSTR (SQLERRM, 0, 240));
END;

/

API to Check the Available credit On a Sales Order.

DECLARE
   l_header_id                                       NUMBER;
   l_calling_action                                  VARCHAR2 (200);
   l_msg_index                                       NUMBER;
   l_error_message                                   VARCHAR2 (2000);
   x_msg_count                                       NUMBER;
   x_msg_data                                        VARCHAR2 (200);
   x_result_out                                      VARCHAR2 (200);
   x_return_status                                   VARCHAR2 (200);
BEGIN
   l_header_id                                              := 885421;
   l_calling_action                                         := 'BOOKING';
   x_msg_count                                              := NULL;
   x_msg_data                                               := NULL;
   x_result_out                                             := NULL;
   x_return_status                                          := NULL;
   apps.oe_credit_pub.check_available_credit (p_header_id                   => l_header_id
                                            , p_calling_action              => l_calling_action
                                            , p_msg_count                   => x_msg_count
                                            , p_msg_data                    => x_msg_data
                                            , p_result_out                  => x_result_out
                                            , p_return_status               => x_return_status
                                             );

   IF x_return_status <> fnd_api.g_ret_sts_success
   THEN
      FOR i IN 1 .. x_msg_count
      LOOP
         apps.fnd_msg_pub.get (p_msg_index                   => i
                             , p_encoded                     => fnd_api.g_false
                             , p_data                        => x_msg_data
                             , p_msg_index_out               => l_msg_index
                              );

         IF l_error_message IS NULL
         THEN
            l_error_message                                          := SUBSTR (x_msg_data, 1, 250);
         ELSE
            l_error_message                                          :=
                                                                      l_error_message || ' /' || SUBSTR (x_msg_data, 1, 250);
         END IF;
      END LOOP;

      DBMS_OUTPUT.put_line ('*****************************************');
      DBMS_OUTPUT.put_line ('API Error : ' || l_error_message);
      DBMS_OUTPUT.put_line ('*****************************************');
      ROLLBACK;
   ELSE
      DBMS_OUTPUT.put_line ('*****************************************');
      DBMS_OUTPUT.put_line ('Resul Out For Header Id : ' || l_header_id || '---' || x_result_out);
      DBMS_OUTPUT.put_line ('*****************************************');
      COMMIT;
   END IF;
EXCEPTION
   WHEN OTHERS
   THEN
      DBMS_OUTPUT.put_line ('Error is ' || SQLCODE || '---' || SQLERRM);
END;

Monday, 19 January 2015

Item Cost API


/*Typically, wrappers goes through following steps for each item:
1. Load the data from the flat file into PL/SQL table or record.
2. Call the Item Cost API for each item.
3. Check the return status.
4. Retrieve the messages from the message stack and write them into a log file.
Wrapper logic
1. Lines 66-69, declares PL/SQL variables to hold the data coming from flat file.
These variables gets passed to the public API, so that data type should
correspond with the data types specified in the API specification.
2. Lines 125-132, get the first line from the flat file and extract the first value. In
our case it is a type of the record. Following are the possible values:
10 : Item Cost Header record
20 : This Level record for the Item
30 : Lower Level record for the Item.
3. Lines 143-261, populate the item details into PL/SQL tables based on the type
of record.
4. Lines 263-281, error handling. If error is raised while populating header record
then skip all the detail records. If in case of detail record then skip only that
detail record and continue populating other detail records.
5. Lines 283-335, get the next line fromthe file and check type of record. Following
scenarios may occur :
n If the record type is of detail, then continue populating the record.
n If the record type is header record then call the public API for the previous
Item Cost Detail already read.
n If the fetch fails (i.e., end of file is reached - No_Data_Found error), then call
the public API for the previous Item Cost Detail already read.
6. Lines 343-373, read all the messages from the message stack and write it into a
log file.
7. Lines 376-383, check the out parameter l_costcmpnt_ids PL/SQL table, if any
records are inserted then get the information for what detail records have been
inserted.
8. Lines 385-389, check whether to continue the loop or not. If yes, then start over
again from step 2.*/



--+==========================================================================+
/* FUNCTION NAME
|
3 --| Create_Item_Cost
|
4 --|
|
5 --| TYPE
|
6 --| Public
|
7 --|
|
8 --| USAGE
|
9 --| Create item Cost
|
10 --|
|
11 --| DESCRIPTION
|
12 --| This is a PL/SQL wrapper function to call the
|
13 --| Create_Item_Cost API.
|
14 --| It reads item data from a flat file and outputs any error
|
15 --| messages to a second flat file. It also generates a log file
|
16 --| called gmf_api_cric_wrapper<session_id>.log in the p_dir directory.
|
17 --|
|
18 --| PARAMETERS
|
19 --| p_dir IN VARCHAR2 - Working directory for input
|
20 --| and output files.
|
21 --| p_input_file IN VARCHAR2 - Name of input file
|
22 --| p_output_file IN VARCHAR2 - Name of output file
|
23 --| p_delimiter IN VARCHAR2 - Delimiter character
|
24 --|
|
25 --| RETURNS
|
26 --| VARCHAR2 - 'S' All records processed successfully
|
27 --| 'E' 1 or more records errored
|
28 --| 'U' 1 or more record unexpected error
|
29 --|
|
30 --| HISTORY
|
31 --|
|
32*/
 --Api end of comments

 FUNCTION Create_Item_Cost
  ( p_dir IN VARCHAR2
   , p_input_file IN VARCHAR2
 , p_output_file IN VARCHAR2
 , p_delimiter IN VARCHAR240)

 RETURN VARCHAR2
 IS

 /* Local variables */

 l_status VARCHAR2(11);
 l_return_status VARCHAR2(11) :=FND_API.G_RET_STS_SUCCESS;
 l_count NUMBER(10) ;
 l_record_count NUMBER(10) :=0;
 l_loop_cnt NUMBER(10) :=0;
 l_dummy_cnt NUMBER(10) :=0;
 l_data VARCHAR2(1000);
 l_p_dir VARCHAR2(150);
 l_output_file VARCHAR2(120);
 l_outfile_handle UTL_FILE.FILE_TYPE;
 l_input_file VARCHAR2(120);
 l_infile_handle UTL_FILE.FILE_TYPE;
 l_line VARCHAR2(1000);
 l_delimiter VARCHAR(11);
 l_log_dir VARCHAR2(150);
 l_log_name VARCHAR2(120) :='gmf_api_cric_wrapper';
 l_log_handle UTL_FILE.FILE_TYPE;
 l_global_file VARCHAR2(120);

 l_header_rec GMF_ItemCost_PUB.Header_Rec_Type;
 l_this_lvl_tbl GMF_ItemCost_PUB.This_Level_Dtl_Tbl_Type;
 l_lower_lvl_tbl GMF_ItemCost_PUB.Lower_Level_Dtl_Tbl_Type;
 l_costcmpnt_ids GMF_ItemCost_PUB.costcmpnt_ids_tbl_type;
 l_idx NUMBER(10);
 l_idx1 NUMBER(10);
 l_type NUMBER(10);
 l_continue VARCHAR2(1) := 'Y' ;
 l_skip_details VARCHAR2(1) := 'N' ;

 l_session_id VARCHAR2(110);

 BEGIN

 /* Enable The Buffer */
 DBMS_OUTPUT.ENABLE(1000000);

 l_p_dir :=p_dir;
 l_input_file :=p_input_file;
 l_output_file :=p_output_file;
 l_delimiter :=p_delimiter;
 l_global_file :=l_input_file;

 /* Obtain The SessionId To Append To wrapper File Name. */

 l_session_id := USERENV('sessionid');

 l_log_name := CONCAT(l_log_name,l_session_id);
 l_log_name := CONCAT(l_log_name,'.log');

 /* Directory is now the same same as for the out file */
 l_log_dir := p_dir;


 /* Open The Wrapper File For Output And The Input File for Input. */

 l_log_handle :=UTL_FILE.FOPEN(l_log_dir, l_log_name, 'w');
 l_infile_handle :=UTL_FILE.FOPEN(l_p_dir, l_input_file, 'r');

 /* Loop thru flat file and call Item Cost API */
 DBMS_OUTPUT.PUT_LINE('Process Started at ' || to_char(SYSDATE,'DD-MON-YY HH24:MI:SS'));
 DBMS_OUTPUT.PUT_LINE('Input Directory ' || l_p_dir );
 DBMS_OUTPUT.PUT_LINE('Input File ' || l_input_file );
 DBMS_OUTPUT.PUT_LINE('Delimiter ' || l_delimiter );
 DBMS_OUTPUT.PUT_LINE('Output File ' || l_output_file );


 DBMS_OUTPUT.PUT_LINE('Start Processing');
 UTL_FILE.PUT_LINE(l_log_handle, 'Process Started at ' || to_char(SYSDATE,'DD-MON-YY HH24:MI:SS'));

 UTL_FILE.NEW_LINE(l_log_handle);
 UTL_FILE.PUT_LINE(l_log_handle, 'Input Directory ' || l_p_dir );
 UTL_FILE.PUT_LINE(l_log_handle, 'Input File ' || l_input_file );
 UTL_FILE.PUT_LINE(l_log_handle, 'Record Type ' || l_delimiter );
 UTL_FILE.PUT_LINE(l_log_handle, 'Output File ' || l_output_file );

 l_outfile_handle :=UTL_FILE.FOPEN(l_p_dir, l_output_file, 'w');

 /* Get the first record from the file */

 BEGIN
 UTL_FILE.GET_LINE(l_infile_handle, l_line);
 l_record_count :=l_record_count+1;
 l_type := Get_Field(l_line,l_delimiter,1) ; -- 10 : header rec, 20 :this level, 30 : lower level
 EXCEPTION
 WHEN NO_DATA_FOUND THEN
 raise;
 END;

 /* Populate appropriate pl/sql record or table based on the type of record */
 LOOP
 BEGIN

 UTL_FILE.PUT_LINE(l_log_handle, 'Reading Record...' || l_record_count);
 UTL_FILE.PUT_LINE(l_log_handle, 'Type = ' || l_type) ;

 /* Header record */

 IF l_type = 10 THEN
 -- empty the pl/sql tables to remove details of the previous item
 -- and initialize the index
 l_this_lvl_tbl.delete ;
 l_lower_lvl_tbl.delete ;
 l_costcmpnt_ids.delete;
 l_skip_details := 'N' ;
 l_idx := 0 ;
 l_idx1 := 0 ;

 l_header_rec.calendar_code := Get_Field(l_line,l_delimiter,2) ;
 l_header_rec.period_code := Get_Field(l_line,l_delimiter,3) ;
 l_header_rec.cost_mthd_code := Get_Field(l_line,l_delimiter,4) ;
 l_header_rec.whse_code := Get_Field(l_line,l_delimiter,5) ;
 l_header_rec.item_id := Get_Field(l_line,l_delimiter,6) ;
 l_header_rec.item_no := Get_Field(l_line,l_delimiter,7) ;
 l_header_rec.user_name := Get_Field(l_line,l_delimiter,8) ;

 UTL_FILE.PUT_LINE(l_log_handle, 'Type = ' || l_type) ;
 UTL_FILE.PUT_LINE(l_log_handle, 'calendar_code = ' || l_header_rec.calendar_code) ;
 UTL_FILE.PUT_LINE(l_log_handle, 'period_code = ' || l_header_rec.period_code) ;
 UTL_FILE.PUT_LINE(l_log_handle, 'cost_mthd_code = ' || l_header_rec.cost_mthd_code) ;
 UTL_FILE.PUT_LINE(l_log_handle, 'whse_code = ' || l_header_rec.whse_code) ;
 UTL_FILE.PUT_LINE(l_log_handle, 'item_id = ' || l_header_rec.item_id) ;
 UTL_FILE.PUT_LINE(l_log_handle, 'item_no = ' || l_header_rec.item_no) ;
 UTL_FILE.PUT_LINE(l_log_handle, 'user_name = ' || l_header_rec.user_name) ;

 /* This Level Detail record. Skip details in case errors in loading header record */
 ELSIF l_type = 20 AND l_skip_details = 'Y' THEN
 UTL_FILE.PUT_LINE(l_log_handle, 'Error : Skipping this record...');
 ELSIF l_type = 20 AND l_skip_details = 'N' THEN
 l_idx := l_idx + 1 ;
 l_this_lvl_tbl(l_idx).cmpntcost_id        := Get_Field(l_line,l_delimiter,2) ;
 l_this_lvl_tbl(l_idx).cost_cmpntcls_id    := Get_Field(l_line,l_delimiter,3) ;
 l_this_lvl_tbl(l_idx).cost_cmpntcls_code  := Get_Field(l_line,l_delimiter,4) ;
 l_this_lvl_tbl(l_idx).cost_analysis_code  := Get_Field(l_line,l_delimiter,5) ;
 l_this_lvl_tbl(l_idx).cmpnt_cost          := Get_Field(l_line,l_delimiter,6) ;
 l_this_lvl_tbl(l_idx).burden_ind          := Get_Field(l_line,l_delimiter,7) ;
 l_this_lvl_tbl(l_idx).total_qty           := Get_Field(l_line,l_delimiter,8) ;
 l_this_lvl_tbl(l_idx).costcalc_orig       := Get_Field(l_line,l_delimiter,9) ;
 l_this_lvl_tbl(l_idx).rmcalc_type := Get_Field(l_line,l_delimiter,10) ;
 l_this_lvl_tbl(l_idx).delete_mark := Get_Field(l_line,l_delimiter,11) ;
 l_this_lvl_tbl(l_idx).attribute1  := Get_Field(l_line,l_delimiter,12) ;
 l_this_lvl_tbl(l_idx).attribute2  := Get_Field(l_line,l_delimiter,13) ;
l_this_lvl_tbl(l_idx).attribute3   := Get_Field(l_line,l_delimiter,14) ;
 l_this_lvl_tbl(l_idx).attribute4  := Get_Field(l_line,l_delimiter,15) ;
 l_this_lvl_tbl(l_idx).attribute5  := Get_Field(l_line,l_delimiter,16) ;
 l_this_lvl_tbl(l_idx).attribute6  := Get_Field(l_line,l_delimiter,17) ;
 l_this_lvl_tbl(l_idx).attribute7  := Get_Field(l_line,l_delimiter,18) ;
 l_this_lvl_tbl(l_idx).attribute8  := Get_Field(l_line,l_delimiter,19) ;
 l_this_lvl_tbl(l_idx).attribute9  := Get_Field(l_line,l_delimiter,20) ;
 l_this_lvl_tbl(l_idx).attribute10 := Get_Field(l_line,l_delimiter,21) ;
 l_this_lvl_tbl(l_idx).attribute11 := Get_Field(l_line,l_delimiter,22) ;
 l_this_lvl_tbl(l_idx).attribute12 := Get_Field(l_line,l_delimiter,23) ;
 l_this_lvl_tbl(l_idx).attribute13 := Get_Field(l_line,l_delimiter,24) ;
 l_this_lvl_tbl(l_idx).attribute14 := Get_Field(l_line,l_delimiter,25) ;
 l_this_lvl_tbl(l_idx).attribute15 := Get_Field(l_line,l_delimiter,26) ;
 l_this_lvl_tbl(l_idx).attribute16 := Get_Field(l_line,l_delimiter,27) ;
 l_this_lvl_tbl(l_idx).attribute17 := Get_Field(l_line,l_delimiter,28) ;
 l_this_lvl_tbl(l_idx).attribute18 := Get_Field(l_line,l_delimiter,29) ;
 l_this_lvl_tbl(l_idx).attribute19 := Get_Field(l_line,l_delimiter,30) ;
 l_this_lvl_tbl(l_idx).attribute20 := Get_Field(l_line,l_delimiter,31) ;
 l_this_lvl_tbl(l_idx).attribute21 := Get_Field(l_line,l_delimiter,32) ;
 l_this_lvl_tbl(l_idx).attribute22 := Get_Field(l_line,l_delimiter,33) ;
 l_this_lvl_tbl(l_idx).attribute23 := Get_Field(l_line,l_delimiter,34) ;
 l_this_lvl_tbl(l_idx).attribute24 := Get_Field(l_line,l_delimiter,35) ;
 l_this_lvl_tbl(l_idx).attribute25 := Get_Field(l_line,l_delimiter,36) ;
 l_this_lvl_tbl(l_idx).attribute26 := Get_Field(l_line,l_delimiter,37) ;
 l_this_lvl_tbl(l_idx).attribute27 := Get_Field(l_line,l_delimiter,38) ;
 l_this_lvl_tbl(l_idx).attribute28 := Get_Field(l_line,l_delimiter,39) ;
 l_this_lvl_tbl(l_idx).attribute29 := Get_Field(l_line,l_delimiter,40) ;
 l_this_lvl_tbl(l_idx).attribute30 := Get_Field(l_line,l_delimiter,41) ;
 l_this_lvl_tbl(l_idx).attribute_category := Get_Field(l_line,l_delimiter,42) ;

 UTL_FILE.PUT_LINE(l_log_handle,'cmpntcost_id('||l_idx||') = '
||
 l_this_lvl_tbl(l_idx).cmpntcost_id) ;
 UTL_FILE.PUT_LINE(l_log_handle,'cost_cmpntcls_id('||l_idx||') = '
||
 l_this_lvl_tbl(l_idx).cost_cmpntcls_id) ;
 UTL_FILE.PUT_LINE(l_log_handle,'cost_cmpntcls_code('||l_idx||') = '
 ||
 l_this_lvl_tbl(l_idx).cost_cmpntcls_code) ;
 UTL_FILE.PUT_LINE(l_log_handle,'cost_analysis_code('||l_idx||') = '
 ||
 l_this_lvl_tbl(l_idx).cost_analysis_code) ;
 UTL_FILE.PUT_LINE(l_log_handle,'cmpnt_cost('||l_idx||') = '
||
 l_this_lvl_tbl(l_idx).cmpnt_cost) ;
 UTL_FILE.PUT_LINE(l_log_handle,'burden_ind('||l_idx||') = '
||
 l_this_lvl_tbl(l_idx).burden_ind) ;
 UTL_FILE.PUT_LINE(l_log_handle,'total_qty('||l_idx||') = '
||
 l_this_lvl_tbl(l_idx).total_qty) ;
 UTL_FILE.PUT_LINE(l_log_handle,'costcalc_orig('||l_idx||') = '
||
 l_this_lvl_tbl(l_idx).costcalc_orig) ;
 UTL_FILE.PUT_LINE(l_log_handle,'rmcalc_type('||l_idx||') = '
||
 l_this_lvl_tbl(l_idx).rmcalc_type) ;
 UTL_FILE.PUT_LINE(l_log_handle,'delete_mark('||l_idx||') = '
||
 l_this_lvl_tbl(l_idx).delete_mark) ;

 /* Lower Level Detail record. Skip details in case errors in loading header record */
 ELSIF l_type = 30 AND l_skip_details = 'Y' THEN
 UTL_FILE.PUT_LINE(l_log_handle, 'Error : Skipping this record...');
 ELSIF l_type = 30 AND l_skip_details = 'N' THEN
 l_idx1 := l_idx1 + 1 ;
 l_type := Get_Field(l_line,l_delimiter,1) ;
 l_lower_lvl_tbl(l_idx1).cmpntcost_id := Get_Field(l_line,l_delimiter,2) ;
 l_lower_lvl_tbl(l_idx1).cost_cmpntcls_id := Get_Field(l_line,l_delimiter,3) ;
 l_lower_lvl_tbl(l_idx1).cost_cmpntcls_code := Get_Field(l_line,l_delimiter,4) ;
 l_lower_lvl_tbl(l_idx1).cost_analysis_code := Get_Field(l_line,l_delimiter,5) ;
 l_lower_lvl_tbl(l_idx1).cmpnt_cost := Get_Field(l_line,l_delimiter,6) ;

 UTL_FILE.PUT_LINE(l_log_handle,'ll cmpntcost_id('||l_idx1||') ='
 || l_lower_lvl_tbl(l_idx1).cmpntcost_id) ;

 UTL_FILE.PUT_LINE(l_log_handle,'ll cost_cmpntcls_id('||l_idx1||') ='
 || l_lower_lvl_tbl(l_idx1).cost_cmpntcls_id) ;

 UTL_FILE.PUT_LINE(l_log_handle,'ll cost_cmpntcls_code('||l_idx1||') ='
 || l_lower_lvl_tbl(l_idx1).cost_cmpntcls_code) ;

 UTL_FILE.PUT_LINE(l_log_handle,'ll cost_analysis_code('||l_idx1||') ='
 || l_lower_lvl_tbl(l_idx1).cost_analysis_code) ;

 UTL_FILE.PUT_LINE(l_log_handle,'ll cmpnt_cost('||l_idx1||') =' || l_lower_lvl_tbl(l_idx1).cmpnt_cost) ;

 END IF ;

 EXCEPTION
 WHEN OTHERS THEN
 /* in case of any errors log the error and continue with the next record
 * in case of header, skip all the detail record
 * in case details, skip that particular record */

 UTL_FILE.PUT_LINE(l_outfile_handle, 'Error : ' || to_char(SQLCODE) || '' || SQLERRM);
 UTL_FILE.PUT_LINE(l_log_handle, 'Error : ' || to_char(SQLCODE) || '' || SQLERRM);

    IF l_type = 10 THEN
       l_skip_details := 'Y' ;
        UTL_FILE.PUT_LINE(l_log_handle, 'Error : Skip detail records.');
    ELSIF l_type = 20 THEN
      l_this_lvl_tbl.delete(l_idx);
    ELSIF l_type = 30 THEN
      l_lower_lvl_tbl.delete(l_idx1);
    END IF ;

 END ;

 BEGIN

 UTL_FILE.GET_LINE(l_infile_handle, l_line);
 l_record_count :=l_record_count+1;
 UTL_FILE.NEW_LINE(l_log_handle);
 l_type := Get_Field(l_line,l_delimiter,1) ; -- 10 : header rec, 20: this level, 30 : lower level

EXCEPTION
 /* End of file */
 WHEN NO_DATA_FOUND THEN
 IF l_skip_details = 'N' THEN

 GMF_ItemCost_PUB.Create_Item_Cost
 ( p_api_version      => 1.0
 , p_init_msg_list    => FND_API.G_TRUE
 , p_commit           => FND_API.G_TRUE
, x_return_status     => l_status
 , x_msg_count        => l_count
 , x_msg_data         => l_data
 , p_header_rec       => l_header_rec
 , p_this_level_dtl_tbl  => l_this_lvl_tbl
 , p_lower_level_dtl_Tbl => l_lower_lvl_tbl
 , x_costcmpnt_ids       => l_costcmpnt_ids );
   l_continue := 'N' ;
     goto GET_MSG_STACK ;
     END IF ;
   END;

 IF (l_type = 10 AND l_record_count != 1 AND l_skip_details = 'N') THEN

 DBMS_OUTPUT.PUT_LINE('Calling Create_Item_Cost API...');

 GMF_ItemCost_PUB.Create_Item_Cost
  ( p_api_version   => 1.0
 , p_init_msg_list  => FND_API.G_TRUE
 , p_commit         => FND_API.G_TRUE
 , x_return_status  => l_status
 , x_msg_count      => l_count
 , x_msg_data       => l_data
 , p_header_rec     => l_header_rec
 , p_this_level_dtl_tbl  => l_this_lvl_tbl
 , p_lower_level_dtl_Tbl => l_lower_lvl_tbl
 , x_costcmpnt_ids       => l_costcmpnt_ids
  );
 DBMS_OUTPUT.PUT_LINE('after API call. status := ' || l_status ||' cnt
:= ' || l_count );
 END IF;

<<GET_MSG_STACK>>
 NULL;

 /* Check if any messages generated. If so then decode and */
 /* output to error message flat file */

 IF l_count > 0 THEN

 l_loop_cnt :=1;
  LOOP
 FND_MSG_PUB.Get(
 p_msg_index => l_loop_cnt,
 p_data => l_data,
 p_encoded => FND_API.G_FALSE,
 p_msg_index_out => l_dummy_cnt);

 --DBMS_OUTPUT.PUT_LINE(l_data );
 UTL_FILE.PUT_LINE(l_log_handle, l_data);

 /* Update error status */
 IF (l_status = 'U') THEN
 l_return_status :=l_status;
 ELSIF (l_status = 'E' and l_return_status <> 'U') THEN
 l_return_status :=l_status;
 ELSE
 l_return_status :=l_status;
 END IF;

 l_loop_cnt := l_loop_cnt + 1;
 IF l_loop_cnt > l_count THEN
 EXIT;
 END IF;

 END LOOP; -- msg stack loop
 l_count := 0 ;

 END IF;-- if count of msg stack > 0

 DBMS_OUTPUT.PUT_LINE('# of CostIds inserted : ' || l_costcmpnt_ids.count);
 FOR i in 1..l_costcmpnt_ids.count
 LOOP
 UTL_FILE.PUT_LINE(l_log_handle,
 'CmpntClsId : ' || l_costcmpnt_ids(i).cost_cmpntcls_id ||
 ' Analysis Code : ' || l_costcmpnt_ids(i).cost_analysis_code ||
 ' Cost Level : ' || l_costcmpnt_ids(i).cost_level ||
 ' CostId : ' || l_costcmpnt_ids(i).cmpntcost_id);
 END LOOP ;

 IF l_continue = 'N' THEN
 EXIT ;
 END IF ;

 END LOOP;

 UTL_FILE.NEW_LINE(l_log_handle);
 UTL_FILE.PUT_LINE(l_log_handle, 'Process Completed at ' || to_char(SYSDATE,'DD-MON-YY HH24:MI:SS'));
 UTL_FILE.FCLOSE_ALL;

 RETURN l_return_status;

 EXCEPTION
 WHEN UTL_FILE.INVALID_OPERATION THEN
 /* DBMS_OUTPUT.PUT_LINE('Invalid Operation For '|| l_global_file); */
 UTL_FILE.FCLOSE_ALL;
 RETURN l_return_status;

 WHEN UTL_FILE.INVALID_PATH THEN
 /* DBMS_OUTPUT.PUT_LINE('Invalid Path For '|| l_global_file); */
 UTL_FILE.FCLOSE_ALL;
 RETURN l_return_status;

 WHEN UTL_FILE.INVALID_MODE THEN

 /* DBMS_OUTPUT.PUT_LINE('Invalid Mode For '|| l_global_file); */
 UTL_FILE.FCLOSE_ALL;
 RETURN l_return_status;

 WHEN UTL_FILE.INVALID_FILEHANDLE THEN
 /* DBMS_OUTPUT.PUT_LINE('Invalid File Handle '|| l_global_file); */
 UTL_FILE.FCLOSE_ALL;
 RETURN l_return_status;

 WHEN UTL_FILE.WRITE_ERROR THEN
 /* DBMS_OUTPUT.PUT_LINE('Invalid Write Error '|| l_global_file); */
 UTL_FILE.FCLOSE_ALL;
 RETURN l_return_status;

 WHEN UTL_FILE.READ_ERROR THEN
 /* DBMS_OUTPUT.PUT_LINE('Invalid Read Error '|| l_global_file); */
 UTL_FILE.FCLOSE_ALL;
 RETURN l_return_status;

 WHEN UTL_FILE.INTERNAL_ERROR THEN
 /* DBMS_OUTPUT.PUT_LINE('Internal Error'); */
 UTL_FILE.FCLOSE_ALL;
 RETURN l_return_status;

 WHEN OTHERS THEN
 /* DBMS_OUTPUT.PUT_LINE('Other Error'); */
 UTL_FILE.PUT_LINE(l_outfile_handle, 'Error : ' || to_char(SQLCODE) || ' '|| SQLERRM);
 UTL_FILE.PUT_LINE(l_log_handle, 'Error : ' || to_char(SQLCODE) || ' ' ||SQLERRM);
 UTL_FILE.PUT_LINE(l_log_handle, 'Process Completed at ' || to_char(SYSDATE,'DD-MON-YY HH24:MI:SS'));
 UTL_FILE.FCLOSE_ALL;
 l_return_status := 'U' ;
 RETURN l_return_status;

 END Create_Item_Cost;



--+==========================================================================+
 --| FUNCTION NAME
 --| Get_Field
 --| TYPE
 --| Public
 --|
 --| USAGE
 --| Get value of field n from a delimited line of ASCII data
 --|
--| DESCRIPTION
 --| This utility function will return the value of a field from
 --| a delimited line of ASCII text
 --|
 --| PARAMETERS
 --| p_line IN VARCHAR2 - line of data
 --| p_delimiter IN VARCHAR2 - Delimiter character
 --| p_field_no IN NUMBER - Field occurance to be
 --| returned
 --|
 --| RETURNS
 --| VARCHAR2 - Value of field
--| HISTORY

--+==========================================================================+
 -- Api end of comments

 FUNCTION Get_Field
 ( p_line IN VARCHAR2
 , p_delimiter IN VARCHAR2
 , p_field_no IN NUMBER
 )
 RETURN VARCHAR2
 IS

 /* Local variables */
 l_start NUMBER :=0;
 l_end NUMBER :=0;

 BEGIN

 /* Determine start position */
 IF p_field_no = 1
 THEN
 l_start :=0;
 ELSE
 l_start :=INSTR(p_line,p_delimiter,1,(p_field_no - 1));
 IF l_start = 0
 THEN
 RETURN NULL;
 END IF;
 END IF;

 /* Determine end position */
 l_end :=INSTR(p_line,p_delimiter,1,p_field_no);
 IF l_end = 0
 THEN
 l_end := LENGTH(p_line) + 1;
 END IF;

 /* Extract the field data */
 IF (l_end - l_start) = 1
 THEN
 RETURN NULL;
 ELSE
 RETURN SUBSTR(p_line,(l_start + 1),((l_end - l_start) - 1));
 END IF;

 EXCEPTION
 WHEN OTHERS
 THEN
 RETURN NULL;

 END Get_Field;


--+==========================================================================+
 --| FUNCTION NAME
 --| Get_Substring
 --| TYPE
 --| Public
 --| USAGE
 --| Get value of Sub-string from formatted ASCII data file record
 --|
 --| DESCRIPTION
 --| This utility function will return the value of a passed sub-string
 --| of a formatted ASCII data file record
 --|
 --| PARAMETERS
 --| p_substring IN VARCHAR2 - substring data
 --|
 --| RETURNS
 --| VARCHAR2 - Value of field
 --|
 --| HISTORY
 --|
--+==========================================================================+
 -- Api end of comments

 FUNCTION Get_Substring
 ( p_substring IN VARCHAR2
 )
 RETURN VARCHAR2
 IS

 /* Local variables */
 l_string_value VARCHAR2(200) :=' ';

 BEGIN

 /* Determine start position */
 l_string_value :=NVL(RTRIM(LTRIM(p_substring)),' ');

 RETURN l_string_value;
 EXCEPTION
 WHEN OTHERS
 THEN
 RETURN ' ';

END Get_Substring;