You are on page 1of 6

EXECUTE APPS.

MCN_AP_DIVEST_OU(947, 66474);

CREATE OR REPLACE PROCEDURE APPS.MCN_AP_DIVEST_OU


(
p_org_id NUMBER,
p_vend_id VARCHAR2
)
AS

/*
This API will do the follows:

1) Identify all open supplier "Currency Wise" balances.


2) Create Supplier Currency Wise Invoices in relevant OU

*/

/*

To-Do:

2) Convert this API into concurrent program


4) Have Excel report developed by Zubair
5) Make sure Invoice is Created in DIVESTITURE batch
7) See if procedure can apply Prepayments
9) Pass Code COmbination ID from Parameters

*/

--/*-------------------------------------
-- Variables --
-----------------------------------------
-- Local
lv_invoice_source varchar2(100);
lv_invoice_id number;
lv_ou_currency varchar2(100);
lv_exc_rate number;
lv_invoice_type varchar2(100);
v_request_id NUMBER;
l_boolean BOOLEAN;
l_phase VARCHAR2 (200);
l_status VARCHAR2 (200);
l_dev_phase VARCHAR2 (200);
l_dev_status VARCHAR2 (200);
l_message VARCHAR2 (200);
g_invalid_ex EXCEPTION;
l_error_seq VARCHAR2 (200);
l_error_msg VARCHAR2 (4000);

/*--------------------------------------------------------------
-- Cursor for Open Supplier Transaction Balances --
--------------------------------------------------------------*/
CURSOR unset_trans is
select *
from APPS.MCN_SUPPLIER_BALANCES
where 1=1
and org_id = p_org_id
and vendor_id = p_vend_id
;

BEGIN

/*
Begining of Cursor
- Creation of Invoice for each line in cursor
*/
for rec in unset_trans

LOOP

/* Setting Invoice ID in LV for Interface */


lv_invoice_id := ap_invoices_interface_s.nextval;

/* Fetching Base Currency of Operating Unit*/


BEGIN

SELECT gs.currency_code
into lv_ou_currency
FROM gl_sets_of_books gs,
financials_system_params_all os,
hr_operating_units ho
WHERE os.set_of_books_id = gs.set_of_books_id
AND ho.organization_id = os.org_id
AND ho.organization_id = rec.org_id
;
exception when others then EXIT;

END;

/* Fetching Currency Rate of SYSDATE*/

IF rec.invoice_currency_code <> lv_ou_currency

THEN

BEGIN
select conversion_rate
into lv_exc_rate
from gl_daily_rates
where conversion_type = 'Corporate'
and from_currency = rec.invoice_currency_code
and to_currency = lv_ou_currency
and conversion_date = trunc(sysdate)
;

exception when others then EXIT;

END;
END IF;

/* Evaluating Creation of STANDARD invoice or DEBIT MEMO*/


BEGIN

IF
rec.balance >= 0

THEN lv_invoice_type := 'DEBIT';

ELSIF rec.balance < 0

THEN lv_invoice_type := 'STANDARD';

END IF;

END;

BEGIN

/*
Insertion of Lines in Interface
*/

INSERT INTO ap_invoices_interface (invoice_id


,invoice_num
,vendor_id
,vendor_site_id
,invoice_amount
,invoice_currency_code
,invoice_date
,description
,source
,org_id
,payment_method_code
,exchange_rate_type
,exchange_rate
,invoice_type_lookup_code
)

VALUES (lv_invoice_id
,'Divestiture Invoice -' ||
lv_invoice_id
,rec.vendor_id
,rec.vendor_site_id
,rec.balance*-1 --
Reversing Balance
,rec.invoice_currency_code
,trunc(sysdate)
,'This invoice is created
for Divestiture purposes for this Operating Unit'
,'DIVEST'
,rec.org_id
,rec.payment_method_code
,
CASE
WHEN
REC.invoice_currency_code = lv_ou_currency
THEN NULL
WHEN
REC.invoice_currency_code <> lv_ou_currency
THEN 'User'
END
,
CASE
WHEN
REC.invoice_currency_code = lv_ou_currency
THEN NULL
WHEN
REC.invoice_currency_code <> lv_ou_currency
THEN lv_exc_rate
END
,lv_invoice_type
);

END;

BEGIN

INSERT INTO ap_invoice_lines_interface ( invoice_id,


line_number,

line_type_lookup_code,
amount,

accounting_date,

dist_code_combination_id
)
VALUES( lv_invoice_id,
1,
'ITEM',
rec.balance*-1,
--Reversing Balance
trunc(sysdate),
1173088 --Dubai
MCN SWAP Account -- COnfig via request parameters
);
END;

END LOOP;

/*
Run concurrent request to import payables lines
*/

BEGIN

mo_global.init ('SQLAP');
fnd_global.apps_initialize (user_id => 18981, resp_id => 20639,
resp_appl_id => 200); ---Make Dynamic
-- fnd_request.set_org_id (204);
-- mo_global.set_policy_context ('S', 204);

v_request_id :=

fnd_request.submit_request (application => 'SQLAP'


,program => 'APXIIMPT'
,description => ''
,start_time => NULL
,sub_request => FALSE
,argument1 => p_org_id
,argument2 => 'DIVEST'
,argument3 => NULL
,argument4 => NULL
,argument5 => NULL
,argument6 => NULL
,argument7 => NULL
,argument8 => 'N'
,argument9 => 'Y');
COMMIT;

IF v_request_id > 0 THEN


l_boolean :=
fnd_concurrent.wait_for_request (v_request_id
,20
,0
,l_phase
,l_status
,l_dev_phase
,l_dev_status
,l_message);
END IF;

dbms_output.put_line ('********************************');

IF (l_status = 'Normal') THEN


dbms_output.put_line ('Invoice Created Successfully, Please see
the output of Payables OPEN Invoice Import program request id :' || v_request_id);
ELSE
l_error_seq := '02';
l_error_msg := 'Payable Open Ivoice Pogram failed you can see the
log from the application for the following reqiest id :' || v_request_id;
RAISE g_invalid_ex;
END IF;

END;

dbms_output.put_line ('********************************');
EXCEPTION

WHEN g_invalid_ex THEN


dbms_output.put_line ('l_error_seq = ' || l_error_seq);
dbms_output.put_line ('l_error_msg = ' || l_error_msg);
WHEN OTHERS THEN
dbms_output.put_line ('Error :' || sqlerrm);

END;
END ;

You might also like