0% found this document useful (0 votes)
79 views

TR - Receipt Auto Application

This procedure applies receipts to invoices and debit memos for a given organization, customer, and period. It uses cursors to loop through unapplied receipts and unapplied transactions, applying receipt amounts to cover transaction amounts where possible. Any errors encountered, such as receipts outside the period, are logged.

Uploaded by

mohammad zubair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

TR - Receipt Auto Application

This procedure applies receipts to invoices and debit memos for a given organization, customer, and period. It uses cursors to loop through unapplied receipts and unapplied transactions, applying receipt amounts to cover transaction amounts where possible. Any errors encountered, such as receipts outside the period, are logged.

Uploaded by

mohammad zubair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

/******************************************/

CREATE OR REPLACE PROCEDURE APPS.mcn_ar_auto_apply_api(


errbuf OUT VARCHAR2,
retcode OUT VARCHAR2,
p_org_id NUMBER,
p_cust_id VARCHAR2,
p_period VARCHAR2,
p_resp_id NUMBER,
p_cust_type varchar2) AS
--/*-------------------------------------
-- Variables --
-----------------------------------------
-- Local
v_line_tax_amt NUMBER(15, 2);
v_exhange_rate NUMBER(15, 2);
v_currency_code VARCHAR2(25);
v_total_line_amt NUMBER(15, 2);
v_amt_applied NUMBER(15, 2);
v_amt_chk NUMBER(15, 2);
v_amt_chk2 NUMBER(15, 4);
v_rec_avl_amt NUMBER(15, 2);
v_apply_date DATE;
v_period_end_date date ;--VARCHAR2(25);
v_customer_no VARCHAR2(30);
-- API Parameters
l_return_status VARCHAR2(1);
l_msg_count NUMBER(15);
l_msg_data VARCHAR2(240);
l_count NUMBER(15);
l_msg_data_out VARCHAR2(240);
l_mesg VARCHAR2(240);
p_count NUMBER(15);

-------------------------------------*/--
/*-----------------------------------------------------
-- Cursor for UnApplied Receipts --
-----------------------------------------------------*/
CURSOR c_receipt(p_period_end_date date) IS
SELECT acra.cash_receipt_id cash_receipt_id,
acra.pay_from_customer customer_id,
acra.org_id org_id,
acra.receipt_date receipt_date,
acra.receipt_number receipt_number,
araa.gl_date gl_date,
acra.amount amount
FROM ar_cash_receipts_all acra,ar_cash_receipt_history_all acrha,
(SELECT DISTINCT saraa.cash_receipt_id, saraa.gl_date
FROM ar_receivable_applications_all saraa
WHERE saraa.creation_date =
(SELECT MIN(ssaraa.creation_date)
FROM ar_receivable_applications_all ssaraa
WHERE ssaraa.cash_receipt_id = saraa.cash_receipt_id)) araa
WHERE
-- Table Joins :
acra.cash_receipt_id = araa.cash_receipt_id
AND acra.CASH_RECEIPT_ID = acrha.CASH_RECEIPT_ID
AND araa.CASH_RECEIPT_ID = acrha.CASH_RECEIPT_ID
-- Parameters :
AND acra.org_id = NVL(p_org_id, acra.org_id) --* ORG
AND TO_CHAR(acra.pay_from_customer) =
NVL(p_cust_id, acra.pay_from_customer)
--* Customer
--AND TO_CHAR(araa.gl_date, 'yyyy/mm/dd') || ' 00:00:00' <=v_period_end_date
AND Trunc(araa.gl_date) <=p_period_end_date
--* GL Date
-- Conditions :
-- AND acra.AMOUNT <> 0 --* Should have some amount
AND acra.status = 'UNAPP' --* Should be unapplied
AND acrha.STATUS = 'CLEARED'
and acra.PAY_FROM_CUSTOMER in
(select hca.CUST_ACCOUNT_ID
from hz_parties hp, Hz_Cust_Accounts hca
where hca.PARTY_ID = hp.PARTY_ID
and hp.attribute6 = nvl(p_cust_type, hp.ATTRIBUTE6))
ORDER BY araa.gl_date;

-- Record of R_RECEIPT cursor row type to hold c_trx fetches


r_receipt c_receipt%ROWTYPE;

----------------------------------------------- C-Receipt ----*/


/*--------------------------------------------------------------
-- Cursor for UnApplied Invoices,Debit Memos --
--------------------------------------------------------------*/
CURSOR c_trx(cp_org_id NUMBER, --* org from receipt cursor
cp_customer_id NUMBER, --* customer from receipt cursor
cp_max_gl_date VARCHAR2) --* Period last Date
IS
SELECT rcta.customer_trx_id,
rcta.bill_to_customer_id,
rcta.org_id,
apsa.gl_date gl_date,
rcta.trx_number,
NVL(SUM(rctla.extended_amount), 0) line_amount,

--* line's Due Amount


((SELECT NVL(SUM(rctla.extended_amount), 0)
FROM ra_customer_trx_lines_all rctla
WHERE UPPER(rctla.line_type) = 'LINE'
AND rctla.customer_trx_id = rcta.customer_trx_id) +
(SELECT NVL(SUM(rctla.extended_amount), 0)
FROM ra_customer_trx_lines_all rctla
WHERE UPPER(rctla.line_type) = 'TAX'
AND rctla.customer_trx_id = rcta.customer_trx_id) +
(select nvl(sum(AMOUNT),0)
from ar_adjustments_all aa
where 1=1
and aa.STATUS = 'A'
and aa.customer_trx_id = rcta.customer_trx_id ) -
(SELECT NVL(SUM(araa.amount_applied), 0)
FROM ar_receivable_applications_all araa
WHERE UPPER(araa.display) = 'Y'
AND araa.applied_customer_trx_id = rcta.customer_trx_id))
line_due_amount
FROM ra_customer_trx_all rcta,
ar_payment_schedules_all apsa,
ra_customer_trx_lines_all rctla,
ra_cust_trx_types_all rctta
WHERE
-- Table Joins
rcta.customer_trx_id = apsa.customer_trx_id
AND rcta.customer_trx_id = rctla.customer_trx_id
AND rctta.cust_trx_type_id = rcta.cust_trx_type_id
AND rctta.org_id = rcta.org_id
-- parameters
AND rcta.org_id = cp_org_id
AND rcta.bill_to_customer_id = cp_customer_id
-- AND TO_CHAR(apsa.gl_date, 'yyyy/mm/dd') || ' 00:00:00' <= cp_max_gl_date
AND trunc(apsa.gl_date) <= cp_max_gl_date
-- conditions
AND UPPER(rcta.complete_flag) = 'Y' --* should be complete
AND UPPER(rctla.line_type) = 'LINE' --* should be 'line' type line
AND apsa.amount_due_remaining <> 0 --* should have some amount
--and rctta.type in ('INV')
AND rctta.TYPE IN ('INV', 'DM' /*, 'CM'*/)
and rcta.BILL_TO_CUSTOMER_ID in
(select hca.CUST_ACCOUNT_ID
from hz_parties hp, Hz_Cust_Accounts hca
where hca.PARTY_ID = hp.PARTY_ID
and hp.attribute6 = nvl(p_cust_type, hp.ATTRIBUTE6))
--5188580--* Including Debit Memo and Credit Memo
GROUP BY rcta.customer_trx_id,
rcta.bill_to_customer_id,
rcta.org_id,
apsa.gl_date,
rcta.trx_number,
rctta.TYPE
--5188580--* Including Transaction Type to apply Credit Memo first then Debit
memo and then Invoice
ORDER BY TRUNC(apsa.gl_date);
--5188580--* Including Transaction Type to sort by type and then by date

-- order by apsa.gl_date ;
-- Record of C_TRX cursor row type to hold c_trx fetches
r_trx c_trx%ROWTYPE;
---------------------------------------------------- C-TRX ----*/
/****************************************
* *
* STARTING *
* *
****************************************/
BEGIN
fnd_global.apps_initialize(1010, p_resp_id, 222);
arp_standard.enable_debug;
--* writing output headings :
fnd_file.put_line(fnd_file.output, ' ');
fnd_file.put_line(fnd_file.output,
' Customized - Invoice - Receipts Knocking Off Program ');
fnd_file.put_line(fnd_file.output,

'-------------------------------------------------------------------- ');
fnd_file.put_line(fnd_file.output, ' ');
fnd_file.put_line(fnd_file.output, ' p_org_id : ' || p_org_id);
fnd_file.put_line(fnd_file.output, ' p_cust_id : ' || p_cust_id);
fnd_file.put_line(fnd_file.output, ' p_period : ' || p_period);
fnd_file.put_line(fnd_file.output, ' p_resp_id : ' || p_resp_id);
fnd_file.put_line(fnd_file.output, ' ');
fnd_file.put_line(fnd_file.output,
' ================ ========== ========== ============
============ =============== ========== ============ ============ ==========
============ ==========');
fnd_file.put_line(fnd_file.output,
' Customer # Receipt # Rec GLDate Receipt Amt Avl Rec
Amt Transaction # Trx GLDate TRX Line Amt Avl.Line Amt Appl.Date Applied Amt
Status ');
fnd_file.put_line(fnd_file.output,
' ================ ========== ========== ============
============ =============== ========== ============ ============ ==========
============ ==========');

/*************************commented by ahmed on 15-apr-19 on reuqest of waheed sb


should be on date instead of month
--* Getting period's end date :
SELECT DISTINCT trunc (gp.end_date) --TO_CHAR(gp.end_date, 'yyyy/mm/dd') || '
23:59:59'
INTO v_period_end_date
FROM gl_periods gp
WHERE UPPER(gp.period_set_name) = UPPER('TGC Calendar')
AND UPPER(gp.period_name) = UPPER(p_period);*/

SELECT p_period --TO_CHAR(gp.end_date, 'yyyy/mm/dd') || ' 23:59:59'


INTO v_period_end_date
from dual;

-----------------------------------------------------------------------------------
-
-- Receipt Loop
--

-----------------------------------------------------------------------------------
-
FOR r_receipt IN c_receipt(v_period_end_date) LOOP
--* If receipt date lies in next period or later, write error in log, and dont
apply this receipt
IF r_receipt.receipt_date >
-- TO_DATE(v_period_end_date, 'yyyy/mm/dd hh24:mi:ss')
v_period_end_date
THEN
fnd_file.put_line(fnd_file.LOG, ' ');
fnd_file.put_line(fnd_file.LOG,
'Receipt No.:' || r_receipt.receipt_number);
fnd_file.put_line(fnd_file.LOG,
' Error : Receipt Date does not lie in ' ||
p_period);
ELSE
--* other wise proceed with applying the receipt
--* getting customer number from customer id
SELECT ac.customer_number
INTO v_customer_no
FROM ar_customers ac
WHERE ac.customer_id = r_receipt.customer_id;

-------------------------------------------------------------------------------
-- TRX Loop
--

-------------------------------------------------------------------------------
FOR r_trx IN c_trx(r_receipt.org_id,
r_receipt.customer_id,
v_period_end_date) LOOP
-- * application date = later of REC_GL_date, TRX_GL_Date

-----**commented on 03-APRIL-2017**----
IF r_receipt.gl_date > r_trx.gl_date THEN
v_apply_date := trunc(sysdate);--r_receipt.gl_date;
ELSE
v_apply_date := trunc(sysdate);--r_trx.gl_date;
END IF;
-----**commented on 03-APRIL-2017**----
-----------------------------------------------
-- Applied amt calculation --
-----------------------------------------------
v_amt_applied := r_trx.line_due_amount;
--* due amount is applied amount

--* getting line tax amount


/* select nvl(sum(rctla.EXTENDED_AMOUNT),0)
into v_line_tax_amt
from ra_customer_trx_lines_all rctla
where rctla.LINK_TO_CUST_TRX_LINE_ID = r_trx.customer_trx_line_id;
--* computing total line amount ( line + tax )
v_total_line_amt := r_trx.line_amount + v_line_tax_amt;*/
/*
IF r_trx.line_due_amount <> -1 then
--* applied amount , lesser of line total amount
and line due amount
-- the condition doesn't seem to have significance
anymore but doesn't
-- but we're keeping it to be on safe side
IF v_total_line_amt < v_amt_applied THEN
v_amt_applied := v_total_line_amt ;
END IF;
end if;
*/
--* getting receipt's available amount
SELECT SUM(a.amount_applied)
INTO v_rec_avl_amt
FROM ar_receivable_applications_all a
WHERE a.cash_receipt_id = r_receipt.cash_receipt_id
AND a.status = 'UNAPP';

--* applied amount , lesser of 'available receipt amount' and 'amount


applicable on line'
IF v_amt_applied > v_rec_avl_amt THEN
v_amt_applied := v_rec_avl_amt;
END IF;

-- getting curecy code


SELECT rcta.invoice_currency_code
INTO v_currency_code
FROM ra_customer_trx_all rcta
WHERE rcta.customer_trx_id = r_trx.customer_trx_id;
--* getting Exchange Rate
/*IF v_currency_code <> 'PKR' THEN
SELECT NVL(rcta.exchange_rate, 1)
INTO v_exhange_rate
FROM ra_customer_trx_all rcta
WHERE rcta.customer_trx_id = r_trx.customer_trx_id;

v_amt_chk := v_amt_applied * v_exhange_rate;

IF v_rec_avl_amt < v_amt_chk THEN


v_amt_chk2 := v_rec_avl_amt / v_exhange_rate;
v_amt_applied := TRUNC(v_amt_chk2, 2);
END IF;
END IF;*/

--* move to next receipt if receipt should have run out of unapplied amout
IF 0 = v_rec_avl_amt THEN
EXIT;
END IF;

--/*----------------------------------------------------------------------------
-- API Call to APPLY
--

--------------------------------------------------------------------------------
IF v_currency_code = 'PKR' THEN

------------------------------------------------------------------------------
------- FOR PKR

------------------------------------------------------------------------------
ar_receipt_api_pub.APPLY(p_api_version => 1.0,
p_init_msg_list => fnd_api.g_true,
p_commit => fnd_api.g_true,
p_validation_level =>
fnd_api.g_valid_level_full,
-----------------User computed variables
p_org_id => r_trx.org_id,
p_cash_receipt_id => r_receipt.cash_receipt_id,
p_customer_trx_id => r_trx.customer_trx_id,
p_amount_applied => v_amt_applied,
----p_trans_to_receipt_rate => v_exhange_rate,
commented on 03 Oct 13
p_apply_date =>v_period_end_date,--
v_apply_date,
p_apply_gl_date =>v_period_end_date,--
v_apply_date,
------------------ message variables
x_return_status => l_return_status,
x_msg_count => l_msg_count,
x_msg_data => l_msg_data);
ELSE
-----------------------------------------------------------------------
--------------- For Other than PKR
-----------------------------------------------------------------------
ar_receipt_api_pub.APPLY(p_api_version => 1.0,
p_init_msg_list => fnd_api.g_true,
p_commit => fnd_api.g_true,
p_validation_level =>
fnd_api.g_valid_level_full,
-----------------User computed variables
--p_org_id => r_trx.org_id,
p_cash_receipt_id =>
r_receipt.cash_receipt_id,
p_customer_trx_id =>
r_trx.customer_trx_id,
p_amount_applied => v_amt_applied,
p_trans_to_receipt_rate => v_exhange_rate,
p_apply_date => v_period_end_date,--
v_apply_date,
p_apply_gl_date => v_period_end_date,--
v_apply_date,
------------------ message variables
x_return_status => l_return_status,
x_msg_count => l_msg_count,
x_msg_data => l_msg_data);
END IF;

--------------------------------------------------------------
-- API Error Logging --
--------------------------------------------------------------
IF l_return_status != 'S' THEN
fnd_file.put_line(fnd_file.LOG, ' ');
fnd_file.put_line(fnd_file.LOG,
'Receipt No.: ' || r_receipt.receipt_number ||
' TRX No.: ' || r_trx.trx_number || ' Status: ' ||
l_return_status);
fnd_file.put_line(fnd_file.LOG,
' ' || ' r_trx.org_id: ' || r_trx.org_id ||
' r_receipt.cash_receipt_id: ' ||
r_receipt.cash_receipt_id ||
' r_trx.customer_trx_id: ' ||
r_trx.customer_trx_id || ' v_amt_applied: ' ||
v_amt_applied || ' v_apply_date: ' ||
v_period_end_date);

IF l_msg_count = 1 THEN
fnd_file.put_line(fnd_file.LOG,
' Message : ' || l_msg_data);
ELSIF l_msg_count > 1 THEN
LOOP
p_count := p_count + 1;
l_msg_data := fnd_msg_pub.get(fnd_msg_pub.g_next,
fnd_api.g_false);

IF l_msg_data IS NULL THEN


EXIT;
END IF;

fnd_file.put_line(fnd_file.LOG,
' Message ' || p_count || ' : ' ||
l_msg_data);
END LOOP;
END IF;
END IF;

-- API Call End


----------------------------------------------------------------

---------------------------------------------------------------------------/*---
--* writing application to output file --
fnd_file.put_line(fnd_file.output,
' ' ||
-- values
RPAD(NVL(v_customer_no, ' '), 17) ||
RPAD(NVL(r_receipt.receipt_number, ' '), 11) ||
RPAD(r_receipt.gl_date, 11) ||
RPAD(NVL(r_receipt.amount, -1), 13) ||
RPAD(NVL(v_rec_avl_amt, -1), 13) ||
RPAD(NVL(r_trx.trx_number, ' '), 16) ||
RPAD(r_trx.gl_date, 11) ||
RPAD(NVL(r_trx.line_amount, -1), 13) ||
RPAD(NVL(r_trx.line_due_amount, -1), 13) ||
RPAD(v_period_end_date, 11) ||
RPAD(NVL(v_amt_applied, -1), 13) ||
RPAD(NVL(l_return_status, ' '), 11));
-- output written ------------------*/--
END LOOP; -- trx loop
END IF; -- skipping receipt later than period end date IF
END LOOP; -- receipt loop

arp_standard.disable_debug;
END;
/

You might also like