Inventory On Hand Migration Oracle Apps
Inventory On Hand Migration Oracle Apps
IS
-- +===================================================================+
-- +===================================================================+
-- | |
-- | XX_INV_ONHAND_SERIAL_PKG |
-- | |
-- |Description: Procedure to Import Item Onhand Quantities With |
-- | Serial Numbers |
-- | |
-- | |
-- +===================================================================+
PROCEDURE XX_INV_ONHAND_PROC (ERRBUF IN OUT VARCHAR2, RETCODE IN OUT NUMBER)
IS
l_batch_num VARCHAR2 (30);
l_employee_id NUMBER;
l_job_id NUMBER;
l_userid NUMBER:= fnd_global.user_id;
l_org_id NUMBER;
l_inv_errors VARCHAR2 (2);
l_inv_message VARCHAR2 (2000);
l_item_id mtl_system_items_b.inventory_item_id%TYPE;
l_primary_uom_code mtl_system_items_b.primary_uom_code%TYPE;
l_sub_inventory mtl_secondary_inventories.secondary_invento
ry_name%TYPE;
l_mat_acc mtl_secondary_inventories.material_account%
TYPE:=1167;
l_trans_id NUMBER;
l_period_name VARCHAR2 (30);
l_ins_errors VARCHAR2 (2);
l_request_id NUMBER;
l_org_code VARCHAR2 (30);
l_trans_org_id NUMBER;
l_trans_quantity NUMBER;
l_intrans_errors VARCHAR2 (1);
l_error VARCHAR2 (2000);
l_transaction_type_id NUMBER;
l_transaction_action_id NUMBER;
l_transaction_source_type_id NUMBER;
l_transaction_date DATE;
l_rec_cnt NUMBER:=0;
l_inventory_location_id NUMBER:=0;
lv_lot_number VARCHAR2 (30);
l_lot_cnt NUMBER:=0;
l_TRX_LOT_INT_ID NUMBER:=0;
l_sub_organization_id NUMBER;
lv_auto_lot_alpha_prefix VARCHAR2 (30);
v_return_status VARCHAR2 (30);
v_msg_count VARCHAR2 (30);
v_msg_data VARCHAR2 (300);
v_object_id NUMBER;
l_expiration_date DATE:='01-JAN-4000';
l_lot_control_code NUMBER;
-- cursor to on hand Information from Staging Tables
CURSOR c_inv_onhand
IS
SELECT item,
subinventory_code,
transaction_quantity,
transaction_date,
l_inv_errors,
l_inv_message,
l_error,
l_ins_errors,
status,
org_code,
item_cost,
locator,
rowid
FROM XX_INVONHAND_STG
WHERE status IN ('PICK', 'ERRSTG', 'ERRINT') ;
BEGIN
-- Main procedure starts here
fnd_file.put_line(fnd_file.output,'===========================================
=======================================');
fnd_file.put_line(fnd_file.output,'XXL OHQ Conversion Interface Log.....'|| TO
_CHAR (SYSDATE, 'DD-MON-RRRR HH24:MI:SS'));
fnd_file.put_line(fnd_file.output,'===========================================
=======================================');
-- =========================================================================
=============================
-- Get Transaction_Type_Id, Transaction_Action_Id and Transaction_Source_Id
-- =========================================================================
=============================
BEGIN
SELECT transaction_type_id,
transaction_action_id,
transaction_source_type_id
INTO l_transaction_type_id,
l_transaction_action_id,
l_transaction_source_type_id
FROM mtl_transaction_types
WHERE UPPER (transaction_type_name) = 'MISCELLANEOUS RECEIPT';
EXCEPTION
WHEN NO_DATA_FOUND
THEN
fnd_file.put_line(fnd_file.LOG,'Miscellaneous receipt - Transaction
Types Not Defined');
dbms_output.put_line('Miscellaneous receipt - Transactio
n Types Not Defined');
-- p_errbuf := SUBSTR (SQLERRM, 1, 300);
-- p_retcode := 2;
WHEN OTHERS
THEN
fnd_file.put_line(fnd_file.LOG,'Unable to Get Transaction_Type_Id, Transaction_A
ction_Id and Transaction_Source_Id for Miscellaneous receipt...'|| SUBSTR (SQLER
RM, 1, 300));
-- p_errbuf := SUBSTR (SQLERRM, 1, 300);
-- p_retcode := 2;
END;
-- =====================================================================
=================================
-- Reset the flag
-- =====================================================================
=================================
/* Loop for populating data started */
FOR a_rec IN c_inv_onhand
LOOP
l_inv_errors := 'N';
l_inv_message := NULL;
l_error := NULL;
l_ins_errors := 'N';
-- l_lot_cnt:=0;
--l_org_id:=a_rec.org_id;
l_rec_cnt:=l_rec_cnt+1;
--==============================================================================
==
-- --Get the Organization id and Organization Code
--==============================================================================
==
IF (l_inv_errors = 'N')
THEN
---------
BEGIN
SELECT mp.organization_id,
mp.organization_code
INTO l_org_id,l_org_code
FROM hr_organization_units hr,
mtl_parameters mp
WHERE hr.organization_id = mp.organization_id
AND trim(mp.organization_code) = trim(a_rec.org_code);
EXCEPTION
WHEN NO_DATA_FOUND
THEN
l_error :=a_rec.org_code || ' Has Invalid Organization Code ';
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
l_inv_errors := 'Y';
WHEN OTHERS
THEN
l_error :=a_rec.org_code|| ' Error Retriving Organization Code
';
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
l_inv_errors := 'Y';
fnd_file.put_line (fnd_file.LOG, SQLERRM);
END;
--End Get the Organization id and Organization Code
END IF;
dbms_output.put_line('ORG CODE AND ID ARE '||' '||l_org_code);
dbms_output.put_line('Item Is '||a_rec.item);
-- =============================================================================
=============================
-- Get Inventory Period Status
-- =============================================================================
=============================
/* IF (l_inv_errors = 'N')
THEN
BEGIN
SELECT period_name
INTO l_period_name
FROM org_acct_periods_v
WHERE rec_type = 'ORG_PERIOD'
AND organization_id = l_org_id
AND BETWEEN TRUNC(start_date) AND TRUNC(end_date)
AND status = 'Open';
EXCEPTION
WHEN NO_DATA_FOUND
THEN
l_error :=' Inventory Period Is not Open for transaction date '|| a_r
ec.transaction_date;
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
dbms_output.put_line('Inventory Period Status
: '||l_error);
l_inv_errors := 'Y';
WHEN OTHERS
THEN
l_error := ' Error Retriving Inventory Period ';
dbms_output.put_line('Error Retriving Inventor
y Period ');
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
l_inv_errors := 'Y';
fnd_file.put_line (fnd_file.LOG, SQLERRM);
END;
--End Inventory Period Status
DBMS_OUTPUT.PUT_LINE('PERIOD NAME '||
l_period_name);
END IF;
*/
-- =============================================================================
=============================
-- Begin Get the Inventory Item Id
-- =============================================================================
=============================
IF (l_inv_errors = 'N')
THEN
BEGIN
SELECT inventory_item_id, primary_uom_code,lot_control_code
INTO l_item_id, l_primary_uom_code,l_lot_control_code
FROM mtl_system_items_b
WHERE trim(segment1) = trim(a_rec.item) AND organization_id = l_
org_id;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
l_error :=' Invalid Item '|| a_rec.item|| ' in Organization id
'|| TO_CHAR (l_org_id);
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
dbms_output.put_line('INVALID ITEM STATUS '||l_error);
l_inv_errors := 'Y';
WHEN OTHERS
THEN
l_error :=' Error Retriving item id for '|| a_rec.item|| ' in
Organization id '|| TO_CHAR (l_org_id);
dbms_output.put_line('Error Retriving item id for '|| a_rec.it
em);
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
l_inv_errors := 'Y';
fnd_file.put_line (fnd_file.LOG,SQLERRM);
END; --End Get the Inventory Item Id
END IF;
-- =============================================================================
=============================
-- Get the Sub Inventory
-- =============================================================================
=============================
IF (l_inv_errors = 'N')
THEN
BEGIN
SELECT secondary_inventory_name,
material_account,
organization_id
INTO l_sub_inventory,
l_mat_acc,
l_sub_organization_id
FROM mtl_secondary_inventories
WHERE organization_id = l_org_id
AND trim(UPPER (secondary_inventory_name)) =upper(trim(a_rec.s
ubinventory_code));
EXCEPTION
WHEN NO_DATA_FOUND
THEN
l_error :=' Invalid Subinventory Code '|| a_rec.subinventory_co
de|| ' in Organization id '|| TO_CHAR (l_org_id);
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
l_inv_errors := 'Y';
WHEN OTHERS
THEN
l_error :=' Error Retriving Sub Inventory Info for '|| a_rec.sub
inventory_code||' in Organization id '|| TO_CHAR(l_org_id);
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
l_inv_errors := 'Y';
fnd_file.put_line (fnd_file.LOG, SQLERRM);
END; --End Get the Sub Inventory
DBMS_OUTPUT.PUT_LINE('SUB INVENTORY NAME '||l_sub_invent
ory);
END IF;
-- =============================================================================
=============================
-- Get the Locator
-- =============================================================================
=============================
IF (l_inv_errors = 'N')
THEN
BEGIN
select INVENTORY_LOCATION_ID
into l_inventory_location_id
from MTL_ITEM_LOCATIONS
where 1=1
AND upper(segment4||'.'||segment1||'.'||segment2||'.'||segment
3)=upper(trim(a_rec.locator));
EXCEPTION
WHEN NO_DATA_FOUND
THEN
l_error :=' Invalid Locator '|| a_rec.Locator|| ' in Organizati
on id '|| TO_CHAR (l_org_id);
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
l_inv_errors := 'Y';
WHEN OTHERS
THEN
l_error :=' Error Retriving Locator Info for '|| a_rec.Locator||
' in Organization id '|| TO_CHAR(l_org_id);
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
l_inv_errors := 'Y';
fnd_file.put_line (fnd_file.LOG, SQLERRM);
END; --End Get the Sub Inventory
DBMS_OUTPUT.PUT_LINE('SUB INVENTORY NAME '||l_sub_invent
ory);
END IF;
-- =============================================================================
=============================
-- Generate Lot Number
-- =============================================================================
=============================
/* Formatted on 2010/11/22 12:25 (Formatter Plus v4.8.8) */
if l_lot_control_code=2 then -- if lot controlis Full Control
BEGIN
SELECT auto_lot_alpha_prefix
-- || '-'
-- || '0'
-- || (TO_NUMBER (start_auto_lot_number))+l_lot_cnt
INTO lv_auto_lot_alpha_prefix
FROM mtl_system_items_b
WHERE organization_id = 84
AND lot_control_code = '2'
AND TRIM (segment1) = trim(a_rec.item);
EXCEPTION
WHEN OTHERS
THEN
l_error :=
' Error Retriving Lot Info for '
|| a_rec.item
|| ' in Organization id '
|| TO_CHAR (l_org_id);
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
l_inv_errors := 'Y';
fnd_file.put_line (fnd_file.LOG, SQLERRM);
END;
BEGIN
DBMS_OUTPUT.put_line ('The Auto generation of Lot Number API');
-- initialization required for R12
mo_global.set_policy_context ('S', 82);
mo_global.init ('INV');
-- Initialization for Organization_id
inv_globals.set_org_id (83);
-- initialize environment
fnd_global.apps_initialize (user_id => 1110,
resp_id => 20634, --INVENTORY
resp_appl_id => 401
);
lv_lot_number := inv_lot_api_pub.auto_gen_lot (
p_org_id => l_org_id,
p_inventory_item_id => l_item_id,
p_parent_lot_number => null,
p_subinventory_code => null,
p_locator_id => null,
p_api_version => 1.0,
p_init_msg_list => fnd_api.g_true,
p_commit => fnd_api.g_false,
p_validation_level => fnd_api.g_valid_level_full,
x_return_status => v_return_status,
x_msg_count => v_msg_count,
x_msg_data => v_msg_data ) ;
IF v_return_status = fnd_api.g_ret_sts_success THEN
COMMIT;
DBMS_OUTPUT.put_line ('The Auto generation of Lot Number is Sucessful: '||lv_
lot_number);
ELSE
DBMS_OUTPUT.put_line ('The Auto generation of Lot Number Failed');
ROLLBACK;
END IF;
END;
END IF; -- if lot control code is 2
/* BEGIN
SELECT lot_number
INTO lv_lot_number
FROM mtl_lot_numbers
WHERE organization_id = 84
AND inventory_item_id=(select inventory_item_id from mtl_syste
m_items_b where TRIM (segment1) = a_rec.item and organization_id = 84);
EXCEPTION
WHEN OTHERS
THEN
l_error :=
' Error Retriving Lot Info for '
|| a_rec.item
|| ' in Organization id '
|| TO_CHAR (l_org_id);
l_inv_message := l_inv_message || l_error;
fnd_file.put_line (fnd_file.LOG, l_error);
l_inv_errors := 'Y';
fnd_file.put_line (fnd_file.LOG, SQLERRM);
END;*/
SELECT xx_mtl_trxlotid_seq.NEXTVAL
INTO l_trx_lot_int_id
FROM DUAL;
-- =============================================================================
=============================
-- --Start Inserting Records in MTL_TRANSACTIONS_INTERFACE
-- =============================================================================
=============================
IF (l_inv_errors = 'N')
THEN
BEGIN -- For Misc recipt
SELECT mtl_material_transactions_s.NEXTVAL
INTO l_trans_id -- For inserting in interface table
as transaction interface id
FROM DUAL;
DBMS_OUTPUT.PUT_LINE('ABOUT TO INSERT INTO MTI
');
INSERT INTO MTL_TRANSACTIONS_INTERFACE
(transaction_interface_id,
source_code,
source_line_id,
source_header_id,
process_flag,
transaction_mode,
last_update_date,
last_updated_by,
creation_date,
created_by,
organization_id,
Inventory_item_id,
item_segment1,
transaction_quantity,
transaction_uom,
transaction_date,
subinventory_code,
transaction_type_id,
transaction_action_id,
transaction_source_type_id,
distribution_account_id,
transaction_cost,
locator_id
)
VALUES (l_trans_id,
'LEGACY',
1001,
1002,
1, -- for processing r
ecords
3, -- for background processing
SYSDATE,
l_userid,
SYSDATE,
l_userid,
l_org_id, -- organization_id
l_item_id,
trim(a_rec.item),
TO_NUMBER (a_rec.transaction_quantity),
l_primary_uom_code,
trunc(to_date('30-SEP-2010')),
trim(a_rec.subinventory_code),
l_transaction_type_id, -- for Miscellaneou
s receipt
l_transaction_action_id, -- for Receipt into
stores
l_transaction_source_type_id, -- for Inventory
l_mat_acc, -- distribution acc
ount id from subinventory definition material_account_id
to_number(a_rec.item_cost),
l_inventory_location_id
);
END;
DBMS_OUTPUT.PUT_LINE('MTL TRANSACTIONS TABLE COMPLETED')
;
if l_lot_control_code=2 then
DBMS_OUTPUT.PUT_LINE('ABOUT TO INSERT INTO LOTS INTERFACE ');
/*INSERT INTO MTL_TRANSACTION_LOTS_INTERFACE
(transaction_interface_id,
lot_number,
transaction_quantity,
last_update_date,
last_updated_by,
creation_date,
created_by,
process_flag
)
VALUES(l_trans_id,
lv_lot_number,
to_number (a_rec.transaction_quantity),
sysdate,
l_userid,
sysdate,
l_userid,
3
);*/
INSERT INTO MTL_TRANSACTION_LOTS_INTERFACE
(
TRANSACTION_INTERFACE_ID,
SOURCE_LINE_ID,
SERIAL_TRANSACTION_TEMP_ID,
PROCESS_FLAG,
LOT_NUMBER,
LOT_EXPIRATION_DATE,
TRANSACTION_QUANTITY,
CREATED_BY,
CREATION_DATE,
LAST_UPDATED_BY,
LAST_UPDATE_DATE)
VALUES
(
l_trans_id,
1001,
l_TRX_LOT_INT_ID,--DIPL_MTL_TRXLOTID_SEQ.NEXTVAL,
'1',
lv_lot_number,--REC_LOT.LOT_NUMBER,
trunc(to_date('30-SEP-2010')),
TO_NUMBER (a_rec.transaction_quantity),
fnd_profile.value('user_id'),--1110,
SYSDATE,
fnd_profile.value('user_id'),--1110,
SYSDATE
);
DBMS_OUTPUT.PUT_LINE('ASSIGNING LOT IS COMPLETED');
END IF;
l_lot_cnt:=l_lot_cnt+1;
END IF;