Vous êtes sur la page 1sur 14

CREATE OR REPLACE package body APPS.

XXAP_INV_BOARD_INTEGRATION is

-- Private type declarations

--type <TypeName> is <Datatype>;

-- Private constant declarations

-- <ConstantName> constant <Datatype> := <Value>;

-- Private variable declarations

--<VariableName> <Datatype>;

Function GetTermID(P_DUE_DATE Number) return Number As

l_terms_id Number;

Begin

Select

TERM_ID

Into

l_terms_id

From

( Select TL.TERM_ID, TL.DUE_DAYS

From AP_TERMS_LINES TL

Where TL.DUE_PERCENT = 100

And TL.DUE_DAYS >= P_DUE_DATE

Order by TL.DUE_DAYS )

Where rownum = 1;

return l_terms_id;

End;

-- Function and procedure implementations


Procedure ValidateInvoices as

Begin

-- Reset Errored Records

Update XXAP_INV_BOARD_INVOICES I

Set I.INTERFACED_FLAG ='N' ,

I.ERROR_MESSAGE = Null,

I.EBS_LAST_UPDATE_DATE = sysdate

Where Nvl(I.INTERFACED_FLAG,'N') = 'E';

-- Check Vendor ID exists

Update XXAP_INV_BOARD_INVOICES I

Set I.ERROR_MESSAGE = I.ERROR_MESSAGE || 'Vendor ID: ' || I.EBS_VENDOR_ID || ' not exists in
Vendor Table,

',

I.INTERFACED_FLAG ='E',

I.EBS_LAST_UPDATE_DATE = sysdate

Where

Nvl(I.INTERFACED_FLAG,'N') In('N')

And Not Exists (Select * from PO_VENDORS V Where V.VENDOR_ID = I.EBS_VENDOR_ID);

-- Check Vendor Site Exists

Update XXAP_INV_BOARD_INVOICES I

Set I.ERROR_MESSAGE = I.ERROR_MESSAGE || 'Vendor Site ID: ' || I.EBS_VENDOR_ID || ' not exists
in Vendor Site Table

',

I.INTERFACED_FLAG ='E',

I.EBS_LAST_UPDATE_DATE = sysdate

Where
Nvl(I.INTERFACED_FLAG,'N') In('N','E')

And Not Exists (Select * from PO_VENDOR_SITES_ALL V Where V.VENDOR_SITE_ID =


I.EBS_VENDOR_SITE_ID);

-- Check External Bank Account exists

Update XXAP_INV_BOARD_INVOICES I

Set I.ERROR_MESSAGE = I.ERROR_MESSAGE || 'Extrernal Bank Account ID: ' ||


I.EBS_EXTERNAL_BANK_ACCOUNT_ID || ' not exists in Banks Table

',

I.INTERFACED_FLAG ='E',

I.EBS_LAST_UPDATE_DATE = sysdate

Where

Nvl(I.INTERFACED_FLAG,'N') In('N','E')

And Not Exists (Select * from IBY_EXT_BANK_ACCOUNTS V Where V.EXT_BANK_ACCOUNT_ID =


I.EBS_EXTERNAL_BANK_ACCOUNT_ID);

-- Check Vessel in Invoice Line have equivlent in the lookup table

Update XXAP_INV_BOARD_INVOICES I

Set I.ERROR_MESSAGE = I.ERROR_MESSAGE || 'Invoice Line: No "XXAP_INV_BOARD_MAPPING"


Lookup Code matches the first digit of Vessel i.e. 1XX. 2XX, 3XX, 4XX or 5XX

',

I.INTERFACED_FLAG ='E',

I.EBS_LAST_UPDATE_DATE = sysdate

Where

Nvl(I.INTERFACED_FLAG,'N') In('N','E')

And Not Exists (Select * from FND_LOOKUP_VALUES LK, XXAP_INV_BOARD_INVOICE_LINES L Where


LK.LOOKUP_CODE = substr(L.EBS_VESSEL_CODE,1,1));

Update XXAP_INV_BOARD_INVOICES I

Set I.ERROR_MESSAGE = I.ERROR_MESSAGE || 'Invoice Line: No "XXAP_INV_BOARD_MAPPING"


Lookup Code matches the first digit of Vessel i.e. 1XX. 2XX, 3XX, 4XX or 5XX
',

I.INTERFACED_FLAG ='E',

I.EBS_LAST_UPDATE_DATE = sysdate

Where

Nvl(I.INTERFACED_FLAG,'N') In('N','E')

And Not Exists (Select * from FND_LOOKUP_VALUES LK, XXAP_INV_BOARD_INVOICE_LINES L,


XXAP_INV_BOARD_INVOICES I Where I.INVOICE_ID = L.INVOICE_ID And LK.LOOKUP_CODE (+) =
substr(L.EBS_VESSEL_CODE,1,1));

-- Populate EBS Invoice ID for non errored records

Update XXAP_INV_BOARD_INVOICES I

Set I.EBS_INVOICE_ID = AP_INVOICES_INTERFACE_S.NEXTVAL ,

I.EBS_LAST_UPDATE_DATE = sysdate

Where

Nvl(I.INTERFACED_FLAG,'N') = 'N';

End;

Procedure LoadInvoices as

Begin

Insert into AP.AP_INVOICES_INTERFACE(

INVOICE_ID, --878289

INVOICE_NUM, -- INV/1803/0675

INVOICE_TYPE_LOOKUP_CODE, -- STANDARD

INVOICE_DATE, --3/21/2018

VENDOR_ID, --271084

VENDOR_SITE_ID, -- 140779

INVOICE_AMOUNT, -- 191260.81
INVOICE_CURRENCY_CODE, -- USD

-- EXCHANGE_RATE, -- 3.75

EXCHANGE_RATE_TYPE,-- Corporate

EXCHANGE_DATE, -- 3/25/2018

TERMS_ID, -- 10280

DESCRIPTION, -- Sale of Bunker fuel for M/T JANA-v58. Bunker Fuel on board. OFFSHORE ARUBA
Has no direct or indirect interest with Iran

ATTRIBUTE1, -- 8887

SOURCE, -- 8887

GROUP_ID, -- INV/1803/0675

-- PAYMENT_CROSS_RATE_DATE, -- 3/21/2018

-- PAYMENT_CROSS_RATE, -- 1

-- PAYMENT_CURRENCY_CODE, -- USD

-- PAY_GROUP_LOOKUP_CODE, -- STANDARD

-- GOODS_RECEIVED_DATE, -- 3/21/2018

-- INVOICE_RECEIVED_DATE, -- 3/21/2018

GL_DATE, -- 3/25/2018

-- ACCTS_PAY_CODE_COMBINATION_ID, -- 1007

-- EXCLUSIVE_PAYMENT_FLAG,-- N

ORG_ID,-- 182

-- CALC_TAX_DURING_IMPORT_FLAG,-- Y

-- ADD_TAX_TO_INV_AMT_FLAG,-- Y

-- TAXATION_COUNTRY.-- SA

-- BANK_CHARGE_BEARER ,-- BEN

PAYMENT_METHOD_CODE,-- WIRE

-- NET_OF_RETAINAGE_FLAG,-- N

-- PARTY_SITE_ID,-- 338379

EXTERNAL_BANK_ACCOUNT_ID-- 5699

)
Select

I.EBS_INVOICE_ID, -- 878289

I.INVOICE_NUM, -- INV/1803/0675

'STANDARD'INVOICE_TYPE_LOOKUP_CODE,

I.INVOICE_DATE, --3/21/2018

I.EBS_VENDOR_ID, --271084

I.EBS_VENDOR_SITE_ID, -- 140779

I.INVOICE_AMOUNT, -- 191260.81

I.EBS_INVOICE_CURRENCY_CODE, -- USD

-- I.EXCHANGE_RATE, -- 3.75

'Corporate' EXCHANGE_RATE_TYPE,

trunc(sysdate) EXCHANGE_DATE, -- 3/25/2018

GetTermID(I.PAYMENT_TERM_DAYS) TERMS_ID, -- 10280

I.INVOICE_DESCRIPTION, -- Sale of Bunker fuel for M/T JANA-v58. Bunker Fuel on board.
OFFSHORE ARUBA Has no direct or indirect interest with Iran

'8887', -- ATTRIBUTE1

8887, -- SOURCE

'Bunker Invoices ' || to_char(sysdate,'YYYYMMDD HHMI'), -- GROUP_ID -- INV/1803/0675

-- PAYMENT_CROSS_RATE_DATE, -- 3/21/2018

-- PAYMENT_CROSS_RATE, -- 1

-- PAYMENT_CURRENCY_CODE, -- USD

-- PAY_GROUP_LOOKUP_CODE, -- STANDARD

-- GOODS_RECEIVED_DATE, -- 3/21/2018

-- INVOICE_RECEIVED_DATE, -- 3/21/2018

trunc(sysdate), --GL_DATE, -- 3/25/2018

-- ACCTS_PAY_CODE_COMBINATION_ID, -- 1007

-- EXCLUSIVE_PAYMENT_FLAG,-- N

VS.ORG_ID ORG_ID,-- 182

-- CALC_TAX_DURING_IMPORT_FLAG,-- Y
-- ADD_TAX_TO_INV_AMT_FLAG,-- Y

-- TAXATION_COUNTRY.-- SA

-- BANK_CHARGE_BEARER ,-- BEN

'WIRE', --PAYMENT_METHOD_CODE,

-- NET_OF_RETAINAGE_FLAG,-- N

-- PARTY_SITE_ID,-- 338379

I.EBS_EXTERNAL_BANK_ACCOUNT_ID --EXTERNAL_BANK_ACCOUNT_ID.-- 5699

From

XXAP_INV_BOARD_INVOICES I,

PO_VENDOR_SITES_ALL VS

Where

I.EBS_VENDOR_SITE_ID = VS.VENDOR_SITE_ID

And Nvl(I.INTERFACED_FLAG, 'N') ='N'; /* Fetch only non ERRORED NON intefaced records */

End;

Procedure LoadInvoiceLines as

Begin

Insert into AP.AP_INVOICE_LINES_INTERFACE(

INVOICE_ID,

INVOICE_LINE_ID,

LINE_TYPE_LOOKUP_CODE,

LINE_NUMBER,

AMOUNT,

DESCRIPTION,

DIST_CODE_CONCATENATED

Select
I.EBS_INVOICE_ID,

AP_INVOICE_LINES_INTERFACE_S.NEXTVAL,

'ITEM',

L.LINE_NUMBER,

L.AMOUNT,

L.DESCRIPTION,

LK.ATTRIBUTE5 || '.' || LK.ATTRIBUTE2 || '.0000.' || LK.ATTRIBUTE3 || '.' ||


L.EBS_VESSEL_CODE || '.' || L.EBS_VOYAGE_CODE || '.' || '0000' DIST_CODE_CONCATENATED

From

XXAP_INV_BOARD_INVOICES I,

XXAP_INV_BOARD_INVOICE_LINES L,

FND_LOOKUP_VALUES LK

Where

Nvl(I.INTERFACED_FLAG,'N') = 'N'

And I.INVOICE_ID = L.INVOICE_ID

And LK.LOOKUP_TYPE ='XXAP_INV_BOARD_MAPPING'

And LK.LOOKUP_CODE = substr(L.EBS_VESSEL_CODE,1,1)

And LK.LANGUAGE ='US';

End;

Procedure DeleteAckInvoices as

Begin

Delete

From XXAP_INV_BOARD_INVOICE_LINES L

Where Exists (Select *

From XXAP_INV_BOARD_INVOICES I

Where I.INVOICE_ID =L.INVOICE_ID

And I.PAY_ACK_FLAG = 'Y');

Delete

From XXAP_INV_BOARD_INVOICES I
Where I.PAY_ACK_FLAG = 'Y';

End;

Procedure MarkImportedInvoices as

Begin

Update XXAP_INV_BOARD_INVOICES I

Set I.IMPORTED_FLAG = 'Y'

Where

Nvl(I.INTERFACED_FLAG,'N') = 'Y' -- check only interfaced invoices

And Nvl(I.IMPORTED_FLAG,'N') = 'N' -- which is not marked as imported

And Exists (Select *

From AP_INVOICES_ALL II

Where II.INVOICE_NUM = I.INVOICE_NUM

And II.VENDOR_SITE_ID = I.EBS_VENDOR_SITE_ID

And II.INVOICE_DATE = I.INVOICE_DATE );

End;

Procedure MarkPaidInvoices as

Begin

Update XXAP_INV_BOARD_INVOICES I

Set

(I.PAID_AMOUNT, I.PAYMENT_FLAG) = (Select II.AMOUNT_PAID, CASE nvl(II.AMOUNT_PAID,0)

When I.Invoice_Amount THEN 'Y'

When 0 Then 'N'

ELSE 'P' END

From AP_INVOICES_ALL II

Where II.INVOICE_NUM = I.INVOICE_NUM

And II.VENDOR_SITE_ID = I.EBS_VENDOR_SITE_ID


And II.INVOICE_DATE = I.INVOICE_DATE

Where

Nvl(I.IMPORTED_FLAG,'N') = 'Y'

And Nvl(I.Payment_Flag,'N') = 'N'

And Exists (Select *

From AP_INVOICES_ALL II

Where II.INVOICE_NUM = I.INVOICE_NUM

And II.VENDOR_SITE_ID = I.EBS_VENDOR_SITE_ID

And II.INVOICE_DATE = I.INVOICE_DATE

And Nvl(II.AMOUNT_PAID,0) !=0

);

End;

Procedure PrintOutput(P_INTEGRATED_COUNT Number, P_ERROR_COUNT Number) as

cursor c_text is

select

rpad (invoice_num, 20) ||

rpad (invoice_date, 10,' ') ||

lpad(invoice_amount,20) || ' ' ||

rpad(substr(invoice_description,1,100),102) ||

rpad(ebs_vendor_id, 20) ||

rpad(ebs_vendor_site_id, 20) ||

rpad(ebs_invoice_currency_code,8) ||

rpad(ebs_external_bank_account_id, 20) ||

rpad(payment_term_days,18) || '

' || error_message

from XXAP_INV_BOARD_INVOICES t

Where Nvl(T.INTERFACED_FLAG,'N') = 'E';


l_text Varchar2(4000);

Begin

l_text := '================================================= Bunker Integration - Invoices


=================================================';

Fnd_file.put_line(fnd_file.output,l_text);

Fnd_file.NEW_LINE(fnd_file.output);

l_text := ' Sucessfuly Integrated Invoices: ' || P_INTEGRATED_COUNT ;

Fnd_file.put_line(fnd_file.output,l_text);

Fnd_file.NEW_LINE(fnd_file.output);

l_text := ' Invoices with Errors: ' || P_ERROR_COUNT;

Fnd_file.put_line(fnd_file.output,l_text);

Fnd_file.NEW_LINE(fnd_file.output);

l_text := 'Invoices with errors not moved to Interface table, errors must be corrected in the
XXAP_INV_BOARD_INVOICES table and resubmit the program.

Details of Invoices with errors:';

Fnd_file.put_line(fnd_file.output,l_text);

Fnd_file.NEW_LINE(fnd_file.output);

l_text := rpad ('Invoice Number', 20) ||

rpad ('Date', 10) ||

rpad('Invoice Amount',20) || ' ' ||

rpad(substr('Invoice Description',1,100),102) ||

rpad('EBS Vendor Id', 20) ||

rpad('EBS Vendor Site Id', 20) ||

rpad('CURR ',8) ||
rpad('Bank Account Id', 20) ||

rpad('Payment Term Days',18) ;

Fnd_file.put_line(fnd_file.output,l_text);

Fnd_file.NEW_LINE(fnd_file.output);

l_text := rpad ('-', 19,'-') || ' ' ||

rpad ('-', 9,'-') || ' ' ||

rpad ('-', 20,'-') || ' ' ||

rpad ('-', 101,'-') || ' ' ||

rpad ('-', 19,'-') || ' ' ||

rpad ('-', 19,'-') || ' ' ||

rpad ('-', 7,'-') || ' ' ||

rpad ('-', 19,'-') || ' ' ||

rpad ('-', 17,'-') ;

Fnd_file.put_line(fnd_file.output,l_text);

Fnd_file.NEW_LINE(fnd_file.output);

Open c_text;

Loop

FETCH c_text into l_text;

Exit When c_text%notfound;

Fnd_file.put_line(fnd_file.output,l_text);

Fnd_file.NEW_LINE(fnd_file.output);

End Loop;

end;

Procedure ProcessInvoices(ERRBUF OUT VARCHAR2,RETCODE OUT VARCHAR2) as


L_Retcode Number Default 0;

CONC_STATUS BOOLEAN;

l_error_count Number default 0;

l_integrated_count Number default 0;

Begin

-- Delete Paid Invoice Acknowleged by AP_INV_BOARD from staging Table

DeleteAckInvoices;

-- Mark succcessfuly Imported Invoices

MarkImportedInvoices;

-- Mark Paid Invoices

MarkPaidInvoices;

-- Validate Invoice Data in Staging Table

ValidateInvoices;

-- check if errors in staging table to raise warning.

Select count(*)

into l_error_count

From XXAP_INV_BOARD_INVOICES I

Where Nvl(I.INTERFACED_FLAG,'N') = 'E';

if l_error_count >0 then L_Retcode := 0; end if;

-- count to be intgrated invoices

Select count(*)

into l_integrated_count

From XXAP_INV_BOARD_INVOICES I

Where Nvl(I.INTERFACED_FLAG,'N') = 'N';


-- Load Validated invoices and its lines

LoadInvoices;

LoadInvoiceLines;

-- Mark inserted records as interfaced

Update XXAP_INV_BOARD_INVOICES I

Set I.INTERFACED_FLAG = 'Y' , I.EBS_LAST_UPDATE_DATE = sysdate

Where Nvl(I.INTERFACED_FLAG,'N') = 'N';

PrintOutput(l_integrated_count, l_error_count);

if L_Retcode = 0 then

RETCODE := 'Success';

CONC_STATUS := FND_CONCURRENT.SET_COMPLETION_STATUS('NORMAL', null);

elsif L_Retcode = 1 then

RETCODE := 'Warning';

CONC_STATUS := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Invoices with


error in XXAP_INV_BOARD_INVOICES, please check invoices with INTERFACED_FLAG = ''E'' ');

elsif L_Retcode = 2 then

RETCODE := 'Error';

CONC_STATUS := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',null);

end if;

commit;

End;

Begin

-- Initialization

null;

end XXAP_INV_BOARD_INTEGRATION;