You are on page 1of 10

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

********************
-KPMG, Hyderabad, INDIA
--***************************************************************************
********************
-Project
: Linkwell Telesystems
-Application
: Order Management
-Script
:
-Created by
: Mahesh
-Creation date : 30-Jan-2009
-Description
: This Procedure picks data from staging table and
inserts into
-supplier interface tables(ap_suppliers_int &
ap_supplier_sites_int)
-Parameters
: None
-Inputs
: None
-Outputs
: None
-Database
:
-Apps. Release : Oracle Applications 12.0
--Development and Maintenance History :
--Date
Version
Author
Description
Last Update
-------------------------------------------------------------------------------------------30-Jan-2009
1.0
Mahesh
Initial
30th 10.30
-***************************************************************************
*********************
CREATE TABLE APPS.LTS_OM_HEADERS_INTERFACE
(
OPERATING_UNIT
VARCHAR2(50 BYTE),
ORDER_SL_NO
VARCHAR2(50 BYTE),
ORDER_INV_NO
VARCHAR2(50 BYTE),
CUSTOMER_NAME
VARCHAR2(50 BYTE),
CUSTOMER_ID
VARCHAR2(50 BYTE),
CUSTOMER_BILL_TO_ID
VARCHAR2(50 BYTE),
ORDER_SOURCE
VARCHAR2(20 BYTE),
ORDER_TYPE
VARCHAR2(50 BYTE),
ORDER_LINE_TYPE
VARCHAR2(50 BYTE),
PRICE_LIST
VARCHAR2(50 BYTE),
ORDERED_DATE
VARCHAR2(50 BYTE),
PAYMENT_TERM
VARCHAR2(50 BYTE),
PARTY_SHIP_TO_NAME
VARCHAR2(50 BYTE),
TRANSACTION_CURR_CODE VARCHAR2(50 BYTE),
INTERFACE_STATUS
CHAR(1 BYTE),
ERROR_MESSAGE
VARCHAR2(1000 BYTE)
)
CREATE TABLE APPS.LTS_OM_LINES_INTERFACE
(
ORDER_SL_NO
VARCHAR2(50 BYTE),
ORDER_INV_NO
VARCHAR2(50 BYTE),
LINE_NO
VARCHAR2(50 BYTE),
ITEM_CODE
VARCHAR2(50 BYTE),

ITEM_QTY
ITEM_PRICE
ITEM_TOTAL_COST
REQUEST_DATE
SHIP_DATE
INTERFACE_STATUS
ERROR_MESSAGE

VARCHAR2(50 BYTE),
VARCHAR2(50 BYTE),
VARCHAR2(20 BYTE),
VARCHAR2(50 BYTE),
VARCHAR2(50 BYTE),
CHAR(1 BYTE),
VARCHAR2(1000 BYTE)

CREATE OR REPLACE PROCEDURE APPS.LTS_OM_INTERFACE_PROC


IS
ln_transaction_type_id NUMBER := 0;
ln_customer_id NUMBER := 0;
ln_order_source_id NUMBER := 0;
ln_list_header_id NUMBER := 0;
ln_transaction_line_type_id NUMBER := 0;
ln_org_id NUMBER := 0;
ln_ship_to_org_id NUMBER := 0;
ln_invoice_to_org_id NUMBER := 0;
ln_inventory_item_id NUMBER := 0;
ln_primary_uom_code VARCHAR2(30);
ln_unit_list_price NUMBER := 0;
ln_orig_sys_document_ref NUMBER := 0;
ln_payment_term_id NUMBER := 0;
ln_line_number NUMBER := 0;
ln_organization_id NUMBER := 0;
lc_error_flag VARCHAR2(1);
CURSOR c_order_details IS
SELECT DISTINCT a.operating_unit,
a.order_sl_no,
a.order_inv_no,
a.customer_name,
a.customer_id,
a.customer_bill_to_id,
a.order_source,
a.order_type,
a.order_line_type,
a.price_list,
a.ordered_date,
a.payment_term,
a.party_ship_to_name,
a.transaction_curr_code
FROM LTS_OM_HEADERS_INTERFACE a
,LTS_OM_LINES_INTERFACE b
WHERE a.interface_status IS NULL
AND a.ORDER_SL_NO = b.ORDER_SL_NO
AND a.ORDER_INV_NO = b.ORDER_INV_NO;
CURSOR c_order_lines(P_ORDER_SL_NO VARCHAR2,P_ORDER_INV_NO VARCHAR2) IS
SELECT *
FROM LTS_OM_LINES_INTERFACE
WHERE interface_status IS NULL
AND ORDER_SL_NO = P_ORDER_SL_NO
AND ORDER_INV_NO = P_ORDER_INV_NO;
BEGIN
--- Updating Interface status flag & message to NULL
-UPDATE LTS_OM_HEADERS_INTERFACE

SET interface_status = NULL,


error_message = NULL
WHERE NVL(interface_status,'N') = 'E';
FOR i IN c_order_details
LOOP
ln_transaction_type_id := 0;
ln_customer_id := 0;
ln_order_source_id := 0;
ln_list_header_id := 0;
ln_org_id := 0;
ln_ship_to_org_id := 0;
ln_invoice_to_org_id := 0;
ln_inventory_item_id := 0;
ln_primary_uom_code := NULL;
ln_unit_list_price := 0;
ln_orig_sys_document_ref := 0;
ln_line_number := 0;
ln_transaction_line_type_id := 0;
ln_payment_term_id := 0;
ln_organization_id := 0;
lc_error_flag := 'N';
-- Order Source Id
BEGIN
SELECT order_source_id
INTO ln_order_source_id
FROM oe_order_sources
WHERE name = 'Migration Orders';
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Order Source Id not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
-- Order Type Id
IF (lc_error_flag <> 'Y') then--1
BEGIN
SELECT transaction_type_id
INTO ln_transaction_type_id
FROM oe_transaction_types_tl
WHERE name = i.order_type;
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Order Type not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
END IF;--1
--Order Line Type Id
IF (lc_error_flag <> 'Y') then--1
BEGIN
SELECT transaction_type_id
INTO ln_transaction_line_type_id
FROM oe_transaction_types_tl
WHERE name = i.order_line_type;
EXCEPTION

WHEN OTHERS THEN


lc_error_flag := 'Y';
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Order Line Type not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;

END;
END IF;--1
-- Price List Id
IF (lc_error_flag <> 'Y') then--2
BEGIN
SELECT list_header_id
INTO ln_list_header_id
FROM qp_list_headers_tl
WHERE name = i.price_list;
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Price List not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
END IF;
-- Payment Term Id
IF (lc_error_flag <> 'Y') then--2
BEGIN
SELECT term_id
INTO ln_payment_term_id
FROM ra_terms
WHERE name = i.payment_term;
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Payment Term not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
END IF;
-- Org Id
IF (lc_error_flag <> 'Y') then
BEGIN
SELECT organization_id
INTO ln_org_id
FROM hr_operating_units
WHERE short_code LIKE TRIM(i.operating_unit);
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Operating Unit not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
END IF;
-- Customer Id

IF (lc_error_flag <> 'Y') then


BEGIN
SELECT customer_id
,bill_to_site_use_id
INTO ln_customer_id
,ln_invoice_to_org_id
FROM lts_customer_master_v
WHERE customer_id = TRIM(i.customer_id)
AND bill_to_address_id = TRIM(i.customer_bill_to_id)
AND site_use_code = 'BILL_TO'
AND org_id =ln_org_id;
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Customer not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
END IF;
-- Organization Id
IF (lc_error_flag <> 'Y' AND i.party_ship_to_name IS NOT NULL) then
BEGIN
SELECT organization_id
INTO ln_organization_id
FROM org_organization_definitions
WHERE organization_code = TRIM(i.PARTY_SHIP_TO_NAME);
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'organization_id not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
END IF;
-- ship_to_org_id Id
IF (lc_error_flag <> 'Y' AND i.party_ship_to_name IS NOT NULL) then
BEGIN
SELECT bill_to_site_use_id
INTO ln_ship_to_org_id
FROM lts_customer_master_v
--WHERE customer_name = TRIM(i.customer_name) -- Kaushal
WHERE customer_id = i.customer_id
AND bill_to_address_id = TRIM(i.customer_bill_to_id)
AND site_use_code = 'SHIP_TO'
AND org_id =ln_org_id;
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Customer SHIP_TO not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
END IF;
-- invoice_to_org_id Id
/* IF (lc_error_flag <> 'Y' AND i.party_bill_to_name IS NOT NULL) then

BEGIN
SELECT bill_to_site_use_id
INTO ln_invoice_to_org_id
FROM lts_customer_master_v
WHERE customer_name = i.customer_name
AND party_site_name = i.party_bill_to_name
AND site_use_code = 'BILL_TO';
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Customer BILL_TO not exists'
WHERE legcy_order_no = i.legcy_order_no;
END;
END IF;*/
IF (lc_error_flag <> 'Y') THEN
BEGIN
SELECT LTS_OM_HEADERS_INTERFACE_S1.NEXTVAL
INTO ln_orig_sys_document_ref
FROM DUAL;
INSERT INTO OE_HEADERS_IFACE_ALL (
order_source_id
,orig_sys_document_ref
,creation_date
,created_by
,last_update_date
,last_updated_by
,operation_code
,sold_to_org_id
,order_type_id
,ordered_date
,transactional_curr_code
,price_list_id
,ship_to_org_id
,invoice_to_org_id
--,payment_term_id
,org_id
,ship_from_org_id
--,booked_flag
)
VALUES(
ln_order_source_id --order_source_id
,ln_orig_sys_document_ref --orig_sys_document_ref
,SYSDATE --creation_date
,-1 --created_by
,SYSDATE --last_update_date
,-1 --last_updated_by
,'INSERT' --operation_code
,ln_customer_id --sold_to_org_id(CUSTOMER_ID )
,ln_transaction_type_id--order_type_id
,i.ordered_date--ordered_date
,i.transaction_curr_code--transactional_curr_code
,ln_list_header_id--price_list_id
,ln_ship_to_org_id-ship_to_org_id(BILL_TO_SITE_USE_ID)SHIP-TO
,ln_invoice_to_org_id-invoice_to_org_id(BILL_TO_SITE_USE_ID)BILL-TO
--,ln_payment_term_id -- payment_term_id
,ln_org_id --org_id
,ln_organization_id--ship_from_org_id

--,'N'--booked_flag
);
INSERT INTO OE_ACTIONS_IFACE_ALL
(ORDER_SOURCE_ID,
ORIG_SYS_DOCUMENT_REF,
OPERATION_CODE,
ORG_ID,
SOLD_TO_ORG_ID)
values
(ln_order_source_id--ORDER_SOURCE_ID
,ln_orig_sys_document_ref
,'BOOK_ORDER'
,ln_org_id
,ln_customer_id
-- Bug 3365408
);

EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
ROLLBACK;
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Error occured while inserting into
OE_HEADERS_IFACE_ALL'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
END IF;
IF (lc_error_flag <> 'Y') THEN
ln_line_number :=0;
FOR j IN c_order_lines(i.order_sl_no,i.order_inv_no)
LOOP
-- Inventory Item Id
BEGIN
SELECT inventory_item_id
,primary_uom_code
INTO ln_inventory_item_id
,ln_primary_uom_code
FROM mtl_system_items_b
WHERE segment1 = TRIM(j.item_code)
AND organization_id = ln_organization_id;
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
--DELETE FROM oe_actions_iface_all WHERE
orig_sys_document_ref =ln_orig_sys_document_ref;
--DELETE FROM oe_headers_iface_all WHERE
orig_sys_document_ref =ln_orig_sys_document_ref;
ROLLBACK;
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Item Code not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
UPDATE LTS_OM_LINES_INTERFACE
SET interface_status = 'E',
error_message = 'Unit List Price not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
-- Inventory Item Id
/* IF (lc_error_flag <> 'Y') THEN

BEGIN
SELECT operand
INTO ln_unit_list_price
FROM qp_list_lines_v
WHERE product_attr_value = ln_inventory_item_id
AND list_header_id = ln_list_header_id;
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
--DELETE FROM oe_actions_iface_all WHERE
orig_sys_document_ref = ln_orig_sys_document_ref;
--DELETE FROM oe_headers_iface_all WHERE
orig_sys_document_ref = ln_orig_sys_document_ref;
ROLLBACK;
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Unit List Price not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
UPDATE LTS_OM_LINES_INTERFACE
SET interface_status = 'E',
error_message = 'Unit List Price not exists'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
END IF;*/
ln_line_number := ln_line_number + 1;
IF (lc_error_flag <> 'Y') THEN
BEGIN
INSERT INTO OE_LINES_IFACE_ALL (
order_source_id
,orig_sys_document_ref
,orig_sys_line_ref
,inventory_item_id
,ordered_quantity
,operation_code
,created_by
,creation_date
,last_updated_by
,last_update_date
--,top_model_line_ref
--,link_to_line_ref
,item_type_code
,sold_to_org_id
,line_number
,order_quantity_uom
,unit_list_price
,unit_selling_price
,ship_from_org_id
,option_flag
,ship_to_org_id
,org_id
,request_date
,schedule_ship_date
)
VALUES(
ln_order_source_id --order_source_id
,ln_orig_sys_document_ref --orig_sys_document_ref
,LTS_OM_HEADERS_INTERFACE_S1.NEXTVAL --orig_sys_line_ref
,ln_inventory_item_id --inventory_item_id
,j.item_qty --ordered_quantity

,'INSERT' --operation_code
,-1 --created_by
,SYSDATE --creation_date
,-1 --last_updated_by
,SYSDATE --last_update_date
--,'1' --top_model_line_ref
--,'2' --link_to_line_ref
,'OPTION' --item_type_code
,ln_customer_id--sold_to_org_id
,ln_line_number--line_number
,ln_primary_uom_code--order_quantity_uom
,j.ITEM_PRICE--ln_unit_list_price--unit_list_price
,j.ITEM_PRICE--ln_unit_list_price--unit_selling_price
,ln_organization_id--ship_from_org_id
,'N'--option_flag
,ln_ship_to_org_id--ship_to_org_id
,ln_org_id --org_id
,j.request_date
,j.ship_date
);
EXCEPTION
WHEN OTHERS THEN
lc_error_flag := 'Y';
--DELETE FROM oe_actions_iface_all WHERE orig_sys_document_ref
= ln_orig_sys_document_ref;
--DELETE FROM oe_headers_iface_all WHERE orig_sys_document_ref
= ln_orig_sys_document_ref;
ROLLBACK;
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'E',
error_message = 'Error occured while inserting into
OE_LINES_IFACE_ALL'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
UPDATE LTS_OM_LINES_INTERFACE
SET interface_status = 'E',
error_message = 'Error occured while inserting into
OE_LINES_IFACE_ALL'
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
END;
END IF;
END LOOP;--j Loop
END IF;
IF (lc_error_flag <> 'Y') THEN
UPDATE LTS_OM_HEADERS_INTERFACE
SET interface_status = 'P',
error_message = NULL
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
UPDATE LTS_OM_LINES_INTERFACE
SET interface_status = 'P',
error_message = NULL
where order_sl_no = i.order_sl_no
and order_inv_no = i.order_inv_no;
COMMIT;
ELSE
COMMIT;
END IF;
END LOOP;--i Loop
--COMMIT;

END LTS_OM_INTERFACE_PROC;/