Sei sulla pagina 1di 5

CREATE OR REPLACE PROCEDURE xxap_invoice_load_interface (errbuf OUT VARCHAR

2,retcode OUT VARCHAR2) AS


CURSOR c IS
SELECT a.ROWID, a.*
FROM xx_ap_invoice_stg a
WHERE a.STATUS_CODE = 'N'
ORDER BY a.LINE_NUM;
l_flag VARCHAR2 (9);
err_msg VARCHAR (1000) := NULL;
l_invoice_num VARCHAR2 (50);
l_currency_code VARCHAR2 (15);
l_vendor_name VARCHAR2 (240);
l_vendor_id NUMBER (20);
l_vendor_num VARCHAR2 (30);
l_vendor_site_id NUMBER;
l_vendor_site_code VARCHAR (15);
l_user_id NUMBER;
l_org_id NUMBER (20);
l_code_combination_id NUMBER (20);
l_request_id NUMBER;
inv_dist_amount_total NUMBER (15, 2) := 0;
l_amount NUMBER (14, 2);
l_invoice_id NUMBER;
l_terms_date DATE;
l_terms_id NUMBER (15);
l_terms_name VARCHAR2 (50);
l_invoice_type_lookup_code VARCHAR2 (50);
BEGIN
FOR i IN c
LOOP
l_flag := 'Y';
err_msg := NULL;
l_invoice_num := NULL;
l_currency_code := NULL;
l_vendor_name := NULL;
l_vendor_id := NULL;
l_vendor_num := NULL;
l_vendor_site_id := NULL;
l_vendor_site_code := NULL;
l_user_id := NULL;
l_org_id := NULL;
l_code_combination_id := NULL;
l_request_id := NULL;
inv_dist_amount_total := 0;
l_amount := NULL;
l_invoice_id := NULL;
l_terms_date := NULL;
l_terms_id := NULL;
l_terms_name := NULL;
l_invoice_type_lookup_code := NULL;
BEGIN
--VENDOR VALIDATION
BEGIN
SELECT vendor_id, vendor_name, segment1
INTO l_vendor_id, l_vendor_name, l_vendor_num
FROM po_vendors
WHERE TRIM (vendor_name) = TRIM (i.VENDOR_NAME );
--WHERE TRIM (segment1) = TRIM (i.vendor_num);
EXCEPTION
WHEN OTHERS
THEN
l_flag := 'E';
err_msg := err_msg || 'Invalid SUPPLIER ' || '/';
END;
/*
--VENDOR SITE VALIDATION
BEGIN
SELECT vendor_site_code, vendor_site_id
INTO l_vendor_site_code, l_vendor_site_id
FROM po_vendor_sites_all
WHERE vendor_id = l_vendor_id
AND org_id = 101
AND vendor_site_id = i.vendor_site_id
--AND TRIM(VENDOR_SITE_CODE) =nvl(TRIM(I.VENDOR_SITE_CODE),TRIM(VENDOR_SITE_CODE
))
AND ROWNUM = 1;
EXCEPTION
WHEN OTHERS
THEN
l_flag := 'E';
err_msg := err_msg || 'Invalid SUPPLIER Site ' || '/';
END;
*/
-- VALIDATION FOR DUPLICATE INVOICE NUMBER
/*
BEGIN
SELECT INVOICE_NUM INTO L_INVOICE_NUM
FROM AP_INVOICES_ALL
WHERE VENDOR_ID=L_VENDOR_id
AND UPPER(TRIM(INVOICE_NUM))=UPPER(TRIM(I.INVOICE_REFERENCE));

IF L_INVOICE_NUM IS NOT NULL THEN


L_FLAG:='E';
ERR_MSG:=ERR_MSG||'Duplicate invoice Number '||'/';
END IF;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
L_FLAG:='E';
ERR_MSG:=ERR_MSG||'Duplicate Invoice Number '||'/';
when others then
ERR_MSG:=ERR_MSG||'error at invoice Number checking '||'/';
END;
*/
--CURRENCY VALIDATION
BEGIN
SELECT currency_code
INTO l_currency_code
FROM fnd_currencies
WHERE TRIM (currency_code) = TRIM (i.INVOICE_CURRENCY_CODE);
EXCEPTION
WHEN OTHERS
THEN
l_flag := 'E';
err_msg := err_msg || 'Invalid Currency ' || '/';
END;
BEGIN
IF i.OPEN_AMOUNT < 0 OR i.BASE_OPEN_AMOUNT < 0
THEN
l_invoice_type_lookup_code := 'DEBIT';
ELSE
l_invoice_type_lookup_code := 'STANDARD';
END IF;
EXCEPTION
WHEN OTHERS
THEN
l_flag := 'E';
err_msg := err_msg || 'INVALID ' || '/';
END;
BEGIN
--IF l_currency_code = 'KES' THEN
l_amount := i.OPEN_AMOUNT;
--ELSE
-- l_amount := i.fc_amount;
-- END IF;
END;
BEGIN
SELECT ap_invoices_interface_s.NEXTVAL
INTO l_invoice_id
FROM DUAL;
EXCEPTION
WHEN OTHERS
THEN
NULL;
END;
BEGIN
SELECT term_id
INTO l_terms_id
FROM ap_terms
WHERE TRIM (NAME) = TRIM (i.TERMS_NAME);
EXCEPTION
WHEN OTHERS
THEN
l_flag := 'E';
err_msg := err_msg || 'Invalid TERM INFO ' || '/';
END;
IF l_flag <> 'E'
THEN
BEGIN
INSERT INTO ap_invoices_interface
(invoice_id, invoice_num,
invoice_date, vendor_id, vendor_num,
vendor_name, vendor_site_id,
vendor_site_code, invoice_amount,
invoice_currency_code, last_update_date,
last_updated_by, creation_date, created_by,
SOURCE, terms_id, org_id, exchange_date,
exchange_rate, exchange_rate_type, gl_date,
invoice_type_lookup_code,
payment_method_lookup_code,
GROUP_ID,
ATTRIBUTE_CATEGORY,ATTRIBUTE2
)
VALUES (l_invoice_id, i.INVOICE_NUM,
i.invoice_date, l_vendor_id, l_vendor_num,
l_vendor_name, l_vendor_site_id,
l_vendor_site_code, l_amount,
l_currency_code,SYSDATE,
1110, SYSDATE,1110,
'ERS', l_terms_id, 82, i.invoice_date,
i.EXCHANGE_RATE, i.EXCHANGE_RATE_TYPE, '30-SEP-2010'
,
l_invoice_type_lookup_code,
i.payment_method_lookup_code,
'AP_INVOICE_MIGRATION',
'Additional Invoice Details',
i.INVOICE_AMOUNT
);
UPDATE XX_AP_INVOICE_STG
SET status_CODE = 'P',
ERROR_MESSAGE = err_msg
WHERE LINE_NUM = i.LINE_NUM AND ROWID = i.ROWID;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('IN HEADER INSET ' || i.LINE_NUM);
RAISE;
END;
BEGIN
INSERT INTO ap_invoice_lines_interface
(invoice_id, invoice_line_id,
line_type_lookup_code, line_number, amount,
dist_code_combination_id, last_update_date,
last_updated_by, creation_date, created_by,
org_id
)
VALUES (l_invoice_id, l_invoice_id + 1,
'ITEM', 1, l_amount,
6318, SYSDATE,
1110, SYSDATE, 1110,
82
);
UPDATE XX_AP_INVOICE_STG
SET status_CODE = 'P',
ERROR_MESSAGE = err_msg
WHERE LINE_NUM = i.LINE_NUM AND ROWID = i.ROWID;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ( 'IN LINE INSET '
|| i.LINE_NUM
|| ' '
|| SQLERRM
|| ' '
|| SQLCODE
);
RAISE;
END;
UPDATE XX_AP_INVOICE_STG
SET status_CODE = 'P'
WHERE LINE_NUM = i.LINE_NUM AND ROWID = i.ROWID;
COMMIT;
ELSE
UPDATE XX_AP_INVOICE_STG
SET status_CODE = 'E',
ERROR_MESSAGE = err_msg
WHERE LINE_NUM = i.LINE_NUM AND ROWID = i.ROWID;
COMMIT;
END IF;
EXCEPTION
WHEN OTHERS
THEN
UPDATE XX_AP_INVOICE_STG
SET status_CODE = 'E',
ERROR_MESSAGE = err_msg
WHERE LINE_NUM = i.LINE_NUM AND ROWID = i.ROWID;
NULL;
END;
err_msg := NULL;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
NULL;
fnd_file.put_line (fnd_file.LOG,
'Error in mail loop -code: '
|| SQLCODE
|| ' Error message: '
|| SQLERRM
);
END xxap_invoice_load_interface;