DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_CASHBOOK

Source


1 PACKAGE BODY ARP_CASHBOOK AS
2 /*$Header: ARRECBKB.pls 120.16.12000000.4 2007/10/18 08:58:15 ankausha ship $*/
3 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('AFLOG_ENABLED'), 'N');
4 
5 --
6 -- Public Procedures
7 --
8 PROCEDURE clear(
9 		p_cr_id       		IN ar_cash_receipts.cash_receipt_id%TYPE,
10 		p_trx_date		IN ar_cash_receipt_history.trx_date%TYPE,
11 		p_gl_date		IN ar_cash_receipt_history.gl_date%TYPE,
12 		p_actual_value_date	IN DATE,
13 		p_exchange_date		IN ar_cash_receipt_history.exchange_date%TYPE,
14 		p_exchange_rate_type	IN ar_cash_receipt_history.exchange_rate_type%TYPE,
15 		p_exchange_rate		IN ar_cash_receipt_history.exchange_rate%TYPE,
16 		p_bank_currency		IN ce_bank_accounts.currency_code%TYPE,
17 		p_amount_cleared	IN ar_cash_receipt_history.amount%TYPE,
18 		p_amount_factored	IN ar_cash_receipt_history.factor_discount_amount%TYPE,
19 		p_module_name   	IN VARCHAR2,
20 		p_module_version   	IN VARCHAR2,
21 		p_crh_id		OUT NOCOPY ar_cash_receipt_history.cash_receipt_history_id%TYPE ) IS
22 --
23 /*----------------------------------
24    Some notes to use this clear procedure:
25 
26    1. The p_amount_cleared and p_amount_factored to be passed in
27       should be in the bank currency.
28 
29    2. If p_bank_currency <> the currency of the receipt, this
30       means the p_bank_currency must be the functional currency,
31       In this case, it assumes the following has been
32       validated before calling this procedure:
33 
34         p_amount_cleared+p_amount_factored =
35                           p_exchange_rate * ar_cash_receipts.amount
36 
37    3. If p_bank_currency = the currency of the receipt,
38       In this case, it assumes the following has been validated
39       before calling this procedure:
40 
41         p_amount_cleared+p_amount_factored =
42                             ar_cash_receipts.amount
43 
44  ------------------------------------*/
45 l_cr_rec		ar_cash_receipts%ROWTYPE;
46 l_rma_rec		ar_receipt_method_accounts%ROWTYPE;
47 l_crh_rec_old		ar_cash_receipt_history%ROWTYPE;
48 l_crh_rec_new		ar_cash_receipt_history%ROWTYPE;
49 l_dist_rec 		ar_distributions%ROWTYPE;
50 l_radj_rec 		ar_rate_adjustments%ROWTYPE;
51 l_acctd_receipt_amt_new ar_cash_receipt_history.acctd_amount%TYPE;
52 l_acctd_receipt_amt_old ar_cash_receipt_history.acctd_amount%TYPE;
53 l_cash_amt 		ar_cash_receipt_history.amount%TYPE;
54 l_acctd_cash_amt 	ar_cash_receipt_history.acctd_amount%TYPE;
55 l_bank_amt 		ar_cash_receipt_history.factor_discount_amount%TYPE;
56 l_acctd_bank_amt 	ar_cash_receipt_history.acctd_factor_discount_amount%TYPE;
57 l_convert_receipt_amt   ar_cash_receipts.amount%TYPE;
58 --Bug#2750340
59 l_event_rec             arp_xla_events.xla_events_type;
60 
61 BEGIN
62 
63 IF PG_DEBUG in ('Y', 'C') THEN
64    arp_standard.debug('clear: ' || 'p_exchange_rate_type = ' || p_exchange_rate_type);
65    arp_standard.debug('clear: ' || 'p_exchange_rate = ' || to_char(p_exchange_rate));
66    arp_standard.debug('clear: ' || 'p_bank_currency = ' || p_bank_currency);
67    arp_standard.debug('p_amount_cleared = ' || to_char(p_amount_cleared));
68    arp_standard.debug('clear: ' || 'p_amount_factored = ' || to_char(p_amount_factored));
69    arp_standard.debug('p_cr_id:'|| p_cr_id);
70    arp_standard.debug('p_trx_date :'||p_trx_date);
71    arp_standard.debug('p_gl_date :'||p_gl_date);
72    arp_standard.debug('p_actual_value_date :'||p_actual_value_date);
73 
74       arp_util.debug( '>>>>>>> arp_cashbook.clear' );
75    END IF;
76 
77    --Setting the Org Context Bug5212892
78    ar_mo_global_cache.populate;
79    arp_global.init_global(mo_global.get_current_org_id);
80    arp_standard.init_standard(mo_global.get_current_org_id);
81 
82    -- Assume this receipt has already been locked
83 
84    -- Validate the GL Date is in open or future period
85 
86    -- Validate exchange info is correct : all missing or all provided
87 
88    -- Validate the amt_clr +bank_charge = receipt amt
89    -- receipt amt * rate = accnt amount
90 
91    -- Fetch the history record
92    arp_cr_history_pkg.fetch_f_crid( p_cr_id, l_crh_rec_old );
93 
94    -- check if receipt is reversed.  If yes, raise exception.
95    -- (bug 376817)
96 
97    IF ( l_crh_rec_old.status = 'REVERSED' ) THEN
98          fnd_message.set_name('AR', 'AR_CANNOT_CLEAR_REV_RECEIPT' );
99          app_exception.raise_exception;
100    END IF;
101 
102    -- Fetch the cash receipt record
103    l_cr_rec.cash_receipt_id := p_cr_id;
104    arp_cash_receipts_pkg.fetch_p( l_cr_rec );
105 
106 
107    -- Fetch the receipt method bank account record
108    arp_rm_accounts_pkg.fetch_p( l_cr_rec.receipt_method_id,
109 			     l_cr_rec.remit_bank_acct_use_id,
110 			     l_rma_rec );
111 
112    -- Insert a new history record
113 
114    -- Calculate entered amount and acctd amount
115    -- If the receipt is functional currency, then amount and
116    -- acctd amounts are the same.
117    -- If bank currency is the same as the receipt's currency,
118    -- then the amount_cleared passed in is in the entered amount.
119    -- If bank currency is not the same as the receipt's currency,
120    -- then the amount_cleared passed in is in the functional amount.
121    l_acctd_receipt_amt_old := l_crh_rec_old.acctd_amount + nvl(L_crh_rec_old.acctd_factor_discount_amount,0);
122 
123 IF PG_DEBUG in ('Y', 'C') THEN
124    arp_standard.debug('clear: ' || 'l_cr_rec.currency_code = ' || l_cr_rec.currency_code);
125    arp_standard.debug('clear: ' || 'ARP_GLOBAL.functional_currency = ' || ARP_GLOBAL.functional_currency);
126    arp_standard.debug('clear: ' || 'l_crh_rec_old.exchange_rate = ' || to_char(l_crh_rec_old.exchange_rate));
127 END IF;
128 
129    IF ( l_cr_rec.currency_code = ARP_GLOBAL.functional_currency )
130    THEN
131 	l_acctd_receipt_amt_new := l_cr_rec.amount;
132 	l_crh_rec_new.amount := p_amount_cleared;
133 	l_crh_rec_new.acctd_amount := p_amount_cleared;
134 	l_crh_rec_new.factor_discount_amount := p_amount_factored;
135 	l_crh_rec_new.acctd_factor_discount_amount := p_amount_factored;
136    ELSE
137 	IF p_bank_currency = l_cr_rec.currency_code
138 	   THEN
139 	        IF ( p_exchange_rate <> l_crh_rec_old.exchange_rate )
140 		THEN
141 	          -- Changes for triangulation: If exchange rate type is not
142 		  -- user, call GL API to calculate accounted amount
143 
144 		  -- Bug 925765: instead of l_cr_rec.exchange_rate_type
145                   -- (i.e. old rate type) use the new one for comparison
146 		  -- with 'User'!
147 
148 		  IF (p_exchange_rate_type = 'User') THEN
149 		   	l_acctd_receipt_amt_new := arp_util.functional_amount(
150 						l_cr_rec.amount,
151 						ARP_GLOBAL.functional_currency,
152 						nvl(p_exchange_rate,1),
153 						NULL,NULL );
154 		  ELSE
155 			l_acctd_receipt_amt_new := gl_currency_api.convert_amount(
156 						l_cr_rec.currency_code,
157 						ARP_GLOBAL.functional_currency,
158                                                 p_exchange_date,
159                                                 p_exchange_rate_type,
160                                                 l_cr_rec.amount);
161 		  END IF;
162 		ELSE
163 			l_acctd_receipt_amt_new := l_acctd_receipt_amt_old;
164 
165 		END IF;
166 
167 	 	l_crh_rec_new.amount := p_amount_cleared;
168 		-- Changes for triangulation: If exchange rate type is not
169                 -- user, call GL API to calculate accounted amount
170 
171 		  -- Bug 925765: instead of l_cr_rec.exchange_rate_type
172                   -- (i.e. old rate type) use the new one for comparison
173 		  -- with 'User'!
174 
175 		IF (p_exchange_rate_type = 'User') THEN
176 		  l_crh_rec_new.acctd_amount := arp_util.functional_amount(
177 					       	p_amount_cleared,
178 					       	ARP_GLOBAL.functional_currency,
179 						nvl(p_exchange_rate,1),
180 						NULL,NULL );
181 		ELSE
182 		  l_crh_rec_new.acctd_amount := gl_currency_api.convert_amount(
183                                                 l_cr_rec.currency_code,
184                                                 ARP_GLOBAL.functional_currency,
185                                                 p_exchange_date,
186                                                 p_exchange_rate_type,
187                                                 p_amount_cleared);
188 		END IF;
189 
190 		l_crh_rec_new.factor_discount_amount := p_amount_factored;
191 		l_crh_rec_new.acctd_factor_discount_amount := l_acctd_receipt_amt_new - L_crh_rec_new.acctd_amount;
192 
193 	   ELSE
194 	        IF ( p_exchange_rate <> l_crh_rec_old.exchange_rate )
195 		THEN
196 			l_acctd_receipt_amt_new := p_amount_cleared + p_amount_factored;
197 		ELSE
198 			l_acctd_receipt_amt_new := l_acctd_receipt_amt_old;
199 
200 		END IF;
201 
202                 -- Bug 646561
203                 -- Convert the receipt amount to the same currency as the
204                 -- cleared amount.
205 		-- Changes for triangulation: If exchange rate type is not
206 		-- user, call GL API to calculate accounted amount
207 
208 		  -- Bug 925765: instead of l_cr_rec.exchange_rate_type
209                   -- (i.e. old rate type) use the new one for comparison
210 		  -- with 'User'!
211 
212 		IF (p_exchange_rate_type = 'User') THEN
213                   l_convert_receipt_amt := arp_util.functional_amount
214                                             (l_cr_rec.amount,
215                                              ARP_GLOBAL.functional_currency,
216                                              p_exchange_rate,null,null);
217 		ELSE
218 		  l_convert_receipt_amt := gl_currency_api.convert_amount(
219 						l_cr_rec.currency_code,
220 						ARP_GLOBAL.functional_currency,
221                                                 p_exchange_date,
222                                                 p_exchange_rate_type,
223                                                 l_cr_rec.amount);
224 		END IF;
225 
226                 -- If the converted receipt amount is the same as the cleared
227                 -- amount then we don't need to calculate a new receipt amount.
228 
229                 IF l_convert_receipt_amt = p_amount_cleared then
230 
231                   l_crh_rec_new.amount := l_cr_rec.amount;
232 
233                 ELSE
234 
235 		  -- Changes for triangulation: If exchange rate type is not
236 		  -- user, call GL API to calculate accounted amount
237 
238 		  -- Bug 925765: instead of l_cr_rec.exchange_rate_type
239                   -- (i.e. old rate type) use the new one for comparison
240 		  -- with 'User'!
241 
242 		  IF (p_exchange_rate_type = 'User') THEN
243   		    l_crh_rec_new.amount := arp_util.functional_amount(
244 		   	       			p_amount_cleared,
245 					       	l_cr_rec.currency_code,
246 						1/nvl(p_exchange_rate,1),
247 						NULL,NULL );
248 		  ELSE
249 		    l_crh_rec_new.amount := gl_currency_api.convert_amount(
250 						ARP_GLOBAL.functional_currency,
251 						l_cr_rec.currency_code,
252                                                 p_exchange_date,
253                                                 p_exchange_rate_type,
254                                                 p_amount_cleared);
255 		  END IF;
256 
257                 END IF;
258 
259 		l_crh_rec_new.acctd_amount := p_amount_cleared;
260 		l_crh_rec_new.factor_discount_amount := l_cr_rec.amount - L_crh_rec_new.amount;
261 		l_crh_rec_new.acctd_factor_discount_amount := p_amount_factored;
262        END IF;
263    END IF;
264 
265    IF ( p_exchange_date = l_crh_rec_old.exchange_date ) OR
266       ( (p_exchange_date IS NULL) AND (l_crh_rec_old.exchange_date IS NULL) )
267    THEN
268       l_crh_rec_new.exchange_date := l_crh_rec_old.exchange_date;
269    ELSE
270       l_crh_rec_new.exchange_date := p_exchange_date;
271    END IF;
272 
273    IF ( p_exchange_rate = l_crh_rec_old.exchange_rate ) OR
274       ( (p_exchange_rate IS NULL) AND (l_crh_rec_old.exchange_rate IS NULL) )
275    THEN
276       l_crh_rec_new.exchange_rate := l_crh_rec_old.exchange_rate;
277    ELSE
278       l_crh_rec_new.exchange_rate := p_exchange_rate;
279    END IF;
280 
281    IF ( p_exchange_rate_type = l_crh_rec_old.exchange_rate_type ) OR
282       ( (p_exchange_rate_type IS NULL) AND (l_crh_rec_old.exchange_rate_type IS NULL) )
283    THEN
284       l_crh_rec_new.exchange_rate_type := l_crh_rec_old.exchange_rate_type;
285    ELSE
286       l_crh_rec_new.exchange_rate_type := p_exchange_rate_type;
287    END IF;
288 
289    --  11.5 VAT changes:
290    --  modified to get the conversion information from the cash receipt history
291    --  record.
292    l_dist_rec.currency_code            := l_cr_rec.currency_code;
293    l_dist_rec.currency_conversion_rate := l_crh_rec_new.exchange_rate;
294    l_dist_rec.currency_conversion_type := l_crh_rec_new.exchange_rate_type;
295    l_dist_rec.currency_conversion_date := l_crh_rec_new.exchange_date;
296    l_dist_rec.third_party_id           := l_cr_rec.pay_from_customer;
297    l_dist_rec.third_party_sub_id       := l_cr_rec.customer_site_use_id;
298 
299    l_crh_rec_new.cash_receipt_id := p_cr_id;
300    l_crh_rec_new.status := 'CLEARED';
301    l_crh_rec_new.trx_date := p_trx_date;
302    l_crh_rec_new.first_posted_record_flag := 'N';
303    l_crh_rec_new.postable_flag := 'Y';
304    l_crh_rec_new.factor_flag := l_crh_rec_old.factor_flag;
305    l_crh_rec_new.gl_date := p_gl_date;
306    l_crh_rec_new.current_record_flag := 'Y';
307 
308    l_crh_rec_new.batch_id := l_crh_rec_old.batch_id;
309 -- fix for bug # 766382
310 -- populating batch_id in ar_cash_receipt_history table
311 -- with the batch_id of the remittance record.
312 -- l_crh_rec_new.batch_id := NULL;
313 
314    l_crh_rec_new.account_code_combination_id := l_rma_rec.cash_ccid;
315 
316    l_crh_rec_new.reversal_gl_date := NULL;
317    l_crh_rec_new.reversal_cash_receipt_hist_id := NULL;
318 
319    l_crh_rec_new.bank_charge_account_ccid := l_rma_rec.bank_charges_ccid;
320    l_crh_rec_new.posting_control_id := -3;
321    l_crh_rec_new.reversal_posting_control_id := NULL;
322    l_crh_rec_new.gl_posted_date := NULL;
323    l_crh_rec_new.reversal_gl_posted_date := NULL;
324    IF (l_crh_rec_old.status = 'CLEARED' )
325    THEN
326       l_crh_rec_new.prv_stat_cash_receipt_hist_id := l_crh_rec_old.prv_stat_cash_receipt_hist_id;
327    ELSE
328       l_crh_rec_new.prv_stat_cash_receipt_hist_id := l_crh_rec_old.cash_receipt_history_id;
329    END IF;
330    l_crh_rec_new.created_from := substrb(p_module_name||'ARP_CASHBOOK.CLEAR',1,30);
331    l_crh_rec_new.reversal_created_from := NULL;
332    arp_cr_history_pkg.insert_p( l_crh_rec_new, l_crh_rec_new.cash_receipt_history_id );
333 
334    -- Update the old history record
335    l_crh_rec_old.current_record_flag := NULL;
336    l_crh_rec_old.reversal_gl_date := p_gl_date;
337    l_crh_rec_old.reversal_cash_receipt_hist_id := l_crh_rec_new.cash_receipt_history_id;
338    l_crh_rec_old.reversal_posting_control_id := -3;
339    l_crh_rec_old.reversal_created_from := substrb(p_module_name||'ARP_CASHBOOK.CLEAR',1,30);
340    arp_cr_history_pkg.update_p( l_crh_rec_old );
341 
342 --Bug#2750340
346 --}
343 --{BUG#5051143 - the cash book call is document based not request based
344 --    l_event_rec.xla_req_id      := arp_global.request_id;
345 --    l_event_rec.xla_mode        := 'B';
347     l_event_rec.xla_from_doc_id := p_cr_id;
348     l_event_rec.xla_to_doc_id   := p_cr_id;
349     l_event_rec.xla_doc_table   := 'CRH';
350     l_event_rec.xla_mode        := 'O';
351     l_event_rec.xla_call        := 'B';
352     arp_xla_events.Create_Events(p_xla_ev_rec => l_event_rec );
353 
354    -- Insert the cash account ar_distributions record
355 ------------------------------------------------------------------------------------
356 -- Removed the following 'if' as part of bug fix 868448
357 -- because we should be able to create zero dollar misc receipts and later clear them.
358 -- Because of this if, records are never created in ar_distributions and as a result
359 -- gl_transfer does'nt pick up these records to post
360 -----------------------------------------------------------------------------------
361    --IF ( l_crh_rec_new.amount <>0 ) OR
362    --   ( l_crh_rec_new.acctd_amount <> 0 )
363    --THEN
364       l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
365       l_dist_rec.source_table := 'CRH';
366       l_dist_rec.source_type := 'CASH';
367 
368       IF ( l_crh_rec_old.status = 'REMITTED' )
369       THEN
370          l_dist_rec.code_combination_id := l_rma_rec.cash_ccid;
371 	 l_cash_amt := l_crh_rec_new.amount;
372 	 l_acctd_cash_amt := l_crh_rec_new.acctd_amount;
373       ELSE
374 	 arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
375 				'CRH', 'CASH', l_rma_rec,
376 				l_dist_rec.code_combination_id);
377 
378 	 l_cash_amt := l_crh_rec_new.amount - L_crh_rec_old.amount;
379 	 l_acctd_cash_amt := l_crh_rec_new.acctd_amount - L_crh_rec_old.acctd_amount;
380       END IF;
381 
382       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
383       IF ( l_cash_amt < 0 )
384       THEN
385          l_dist_rec.amount_dr := NULL;
386          l_dist_rec.amount_cr := -l_cash_amt;
387          l_dist_rec.acctd_amount_dr := NULL;
388          l_dist_rec.acctd_amount_cr := -l_acctd_cash_amt;
389       ELSE
390          l_dist_rec.amount_dr := l_cash_amt;
391          l_dist_rec.amount_cr := NULL;
392          l_dist_rec.acctd_amount_dr := l_acctd_cash_amt;
393          l_dist_rec.acctd_amount_cr := NULL;
394       END IF;
395 /*
396       IF ( l_acctd_cash_amt < 0 )
397       THEN
398          l_dist_rec.acctd_amount_dr := NULL;
399          l_dist_rec.acctd_amount_cr := -l_acctd_cash_amt;
400       ELSE
401          l_dist_rec.acctd_amount_dr := l_acctd_cash_amt;
402          l_dist_rec.acctd_amount_cr := NULL;
403       END IF;
404 */
405       arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
406 
407    -- Bug 2580219: reinitialize variables for MRC use only.
408       l_dist_rec.source_id := l_crh_rec_old.cash_receipt_history_id;
409       l_dist_rec.source_table_secondary := 'MRC';
410       l_dist_rec.source_id_secondary := l_crh_rec_new.cash_receipt_history_id;
411 
412      /* need to insert records into the MRC table.  Calling new
413         mrc engine */
414 /*
415        ar_mrc_engine2.maintain_mrc_data2(
416                               p_event_mode => 'INSERT',
417                               p_table_name => 'AR_DISTRIBUTIONS',
418                               p_mode       => 'SINGLE',
419                               p_key_value  =>  l_dist_rec.line_id,
420                               p_row_info   =>  l_dist_rec);
421 */
422    -- Bug 2580219 reset the values after the call.. just incase they were used.
423       l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
424       l_dist_rec.source_table_secondary := NULL;
425       l_dist_rec.source_id_secondary := NULL;
426 
427    --END IF;
428 
429    -- insert the remittance account ar_distributions record
430 
431    /* Bug No. 3644849 JVARKEY */
432    -- For Remittance Row The exchange parameters must be as that of its history
433    IF ( l_crh_rec_old.status = 'REMITTED' )
434    THEN
435 
436       l_dist_rec.currency_conversion_rate := l_crh_rec_old.exchange_rate;
437       l_dist_rec.currency_conversion_type := l_crh_rec_old.exchange_rate_type;
438       l_dist_rec.currency_conversion_date := l_crh_rec_old.exchange_date;
439 
440    END IF;
441 
442    IF ( l_crh_rec_old.status = 'REMITTED' ) AND
443       ( l_crh_rec_old.factor_flag <> 'Y' ) -- AND
444 /* skoukunt: comment to Fix bug 1198295
445       (( l_crh_rec_old.amount <>0 ) OR
446        ( l_crh_rec_old.acctd_amount <> 0 ))
447 */
448    THEN
449       l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
450       l_dist_rec.source_table := 'CRH';
451       l_dist_rec.source_type := 'REMITTANCE';
452       arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
453 				'CRH', 'REMITTANCE', l_rma_rec,
454 				l_dist_rec.code_combination_id);
455       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
456       IF ( l_cr_rec.amount < 0 )
457       THEN
458          l_dist_rec.amount_cr := NULL;
459          l_dist_rec.amount_dr := -l_cr_rec.amount;
460          l_dist_rec.acctd_amount_cr := NULL;
461          l_dist_rec.acctd_amount_dr := -l_acctd_receipt_amt_old;
462       ELSE
463          l_dist_rec.amount_cr := l_cr_rec.amount;
467       END IF;
464          l_dist_rec.amount_dr := NULL;
465          l_dist_rec.acctd_amount_cr := l_acctd_receipt_amt_old;
466          l_dist_rec.acctd_amount_dr := NULL;
468 /*
469       IF ( l_acctd_receipt_amt_old < 0 )
470       THEN
471          l_dist_rec.acctd_amount_cr := NULL;
472          l_dist_rec.acctd_amount_dr := -l_acctd_receipt_amt_old;
473       ELSE
474          l_dist_rec.acctd_amount_cr := l_acctd_receipt_amt_old;
475          l_dist_rec.acctd_amount_dr := NULL;
476       END IF;
477 */
478       arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
479 
480      /* need to insert records into the MRC table.  Calling new
481         mrc engine */
482 /*
483       ar_mrc_engine2.maintain_mrc_data2(
484                               p_event_mode => 'INSERT',
485                               p_table_name => 'AR_DISTRIBUTIONS',
486                               p_mode       => 'SINGLE',
487                               p_key_value  =>  l_dist_rec.line_id,
488                               p_row_info   =>  l_dist_rec);
489 */
490    END IF;
491    /*4401288 New Exchange rate,type and date needs to be passed for new records*/
492    IF ( l_crh_rec_old.status = 'REMITTED' )
493    THEN
494 
495       l_dist_rec.currency_conversion_rate := l_crh_rec_new.exchange_rate;
496       l_dist_rec.currency_conversion_type := l_crh_rec_new.exchange_rate_type;
497       l_dist_rec.currency_conversion_date := l_crh_rec_new.exchange_date;
498 
499    END IF;
500 
501    -- insert the short term debt account ar_distributions record
502    IF ( l_crh_rec_old.status = 'REMITTED' ) AND
503       ( l_crh_rec_old.factor_flag ='Y' ) -- AND
504 /* skoukunt: comment to Fix bug 1198295
505       (( l_crh_rec_old.amount <>0 ) OR
506       ( l_crh_rec_old.acctd_amount <> 0 ))
507 */
508    THEN
509       l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
510       l_dist_rec.source_table := 'CRH';
511       l_dist_rec.source_type := 'SHORT_TERM_DEBT';
512       l_dist_rec.code_combination_id := l_rma_rec.short_term_debt_ccid;
513       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
514       IF ( l_cr_rec.amount < 0 )
515       THEN
516          l_dist_rec.amount_cr := NULL;
517          l_dist_rec.amount_dr := -l_cr_rec.amount;
518          l_dist_rec.acctd_amount_cr := NULL;
519          l_dist_rec.acctd_amount_dr := -l_acctd_receipt_amt_new;
520       ELSE
521          l_dist_rec.amount_cr := l_cr_rec.amount;
522          l_dist_rec.amount_dr := NULL;
523          l_dist_rec.acctd_amount_cr := l_acctd_receipt_amt_new;
524          l_dist_rec.acctd_amount_dr := NULL;
525       END IF;
526 /*
527       IF ( l_acctd_receipt_amt_new < 0 )
528       THEN
529          l_dist_rec.acctd_amount_cr := NULL;
530          l_dist_rec.acctd_amount_dr := -l_acctd_receipt_amt_new;
531       ELSE
532          l_dist_rec.acctd_amount_cr := l_acctd_receipt_amt_new;
533          l_dist_rec.acctd_amount_dr := NULL;
534       END IF;
535 */
536 
537       arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
538 
539      /* need to insert records into the MRC table.  Calling new
540         mrc engine */
541 
542         ar_mrc_engine2.maintain_mrc_data2(
543                               p_event_mode => 'INSERT',
544                               p_table_name => 'AR_DISTRIBUTIONS',
545                               p_mode       => 'SINGLE',
546                               p_key_value  =>  l_dist_rec.line_id,
547                               p_row_info   =>  l_dist_rec);
548 
549    END IF;
550 
551    -- insert the factor account ar_distributions record if it's
552    -- factor='Y' and there's a rate adj involved
553    IF ( l_crh_rec_old.factor_flag = 'Y' ) AND
554       ( l_crh_rec_old.exchange_rate <> l_crh_rec_new.exchange_rate ) AND
555       ( (l_acctd_receipt_amt_new - l_acctd_receipt_amt_old) <> 0 )
556    THEN
557       l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
558       l_dist_rec.source_table := 'CRH';
559       l_dist_rec.source_type := 'FACTOR';
560       arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
561 				'CRH', 'FACTOR', l_rma_rec,
562 				l_dist_rec.code_combination_id);
563       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
564       IF ( (l_acctd_receipt_amt_new - l_acctd_receipt_amt_old) < 0 )
565       THEN
566          l_dist_rec.amount_dr := NULL;
567          l_dist_rec.amount_cr := 0;
568          l_dist_rec.acctd_amount_dr := NULL;
569          l_dist_rec.acctd_amount_cr := -(l_acctd_receipt_amt_new - L_acctd_receipt_amt_old);
570       ELSE
571          l_dist_rec.amount_dr := 0;
572          l_dist_rec.amount_cr := NULL;
573          l_dist_rec.acctd_amount_dr := (l_acctd_receipt_amt_new - L_acctd_receipt_amt_old);
574          l_dist_rec.acctd_amount_cr := NULL;
575       END IF;
576 /*
577       IF ( (l_acctd_receipt_amt_new - l_acctd_receipt_amt_old) < 0 )
578       THEN
579          l_dist_rec.acctd_amount_dr := NULL;
580          l_dist_rec.acctd_amount_cr := -(l_acctd_receipt_amt_new - L_acctd_receipt_amt_old);
581       ELSE
582          l_dist_rec.acctd_amount_dr := (l_acctd_receipt_amt_new - L_acctd_receipt_amt_old);
583          l_dist_rec.acctd_amount_cr := NULL;
584       END IF;
585 */
586 
587       arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
591 
588 
589         /* need to insert records into the MRC table.  Calling new
590            mrc engine */
592         ar_mrc_engine2.maintain_mrc_data2(
593                               p_event_mode => 'INSERT',
594                               p_table_name => 'AR_DISTRIBUTIONS',
595                               p_mode       => 'SINGLE',
596                               p_key_value  =>  l_dist_rec.line_id,
597                               p_row_info   =>  l_dist_rec);
598 
599    END IF;
600 
601    -- insert the short term debt ar_distributions record if it's
602    -- factor='Y' and there's a rate adj involved and it's
603    -- prior history record is 'CLEARED'
604    IF ( l_crh_rec_old.factor_flag = 'Y' ) AND
605       ( l_crh_rec_old.status = 'CLEARED' ) AND
606       ( l_crh_rec_old.exchange_rate <> l_crh_rec_new.exchange_rate ) AND
607       ( (l_acctd_receipt_amt_new - l_acctd_receipt_amt_old) <> 0 )
608    THEN
609       l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
610       l_dist_rec.source_table := 'CRH';
611       l_dist_rec.source_type := 'SHORT_TERM_DEBT';
612       arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
613 				'CRH', 'SHORT_TERM_DEBT', l_rma_rec,
614 				l_dist_rec.code_combination_id);
615       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
616       IF ( (l_acctd_receipt_amt_new - l_acctd_receipt_amt_old) < 0 )
617       THEN
618          l_dist_rec.amount_cr := NULL;
619          l_dist_rec.amount_dr := 0;
620          l_dist_rec.acctd_amount_cr := NULL;
621          l_dist_rec.acctd_amount_dr := -(l_acctd_receipt_amt_new - L_acctd_receipt_amt_old);
622       ELSE
623          l_dist_rec.amount_cr := 0;
624          l_dist_rec.amount_dr := NULL;
625          l_dist_rec.acctd_amount_cr := (l_acctd_receipt_amt_new - L_acctd_receipt_amt_old);
626          l_dist_rec.acctd_amount_dr := NULL;
627       END IF;
628 /*
629       IF ( (l_acctd_receipt_amt_new - l_acctd_receipt_amt_old) < 0 )
630       THEN
631          l_dist_rec.acctd_amount_cr := NULL;
632          l_dist_rec.acctd_amount_dr := -(l_acctd_receipt_amt_new - L_acctd_receipt_amt_old);
633       ELSE
634          l_dist_rec.acctd_amount_cr := (l_acctd_receipt_amt_new - L_acctd_receipt_amt_old);
635          l_dist_rec.acctd_amount_dr := NULL;
636       END IF;
637 */
638 
639       arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
640 
641         /* need to insert records into the MRC table.  Calling new
642            mrc engine */
643 
644         ar_mrc_engine2.maintain_mrc_data2(
645                               p_event_mode => 'INSERT',
646                               p_table_name => 'AR_DISTRIBUTIONS',
647                               p_mode       => 'SINGLE',
648                               p_key_value  =>  l_dist_rec.line_id,
649                               p_row_info   =>  l_dist_rec);
650 
651    END IF;
652 
653    -- insert the bank charge account ar_distributions record
654    IF ( l_crh_rec_new.factor_discount_amount <>0 ) OR
655       ( l_crh_rec_new.acctd_factor_discount_amount <> 0 )
656    THEN
657       l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
658       l_dist_rec.source_table := 'CRH';
659       l_dist_rec.source_type := 'BANK_CHARGES';
660 
661       IF ( l_crh_rec_old.status = 'REMITTED' )
662       THEN
663          l_dist_rec.code_combination_id := l_crh_rec_new.bank_charge_account_ccid;
664 	 l_bank_amt := nvl(l_crh_rec_new.factor_discount_amount,0);
665 	 l_acctd_bank_amt := nvl(l_crh_rec_new.acctd_factor_discount_amount,0);
666       ELSE
667 	 arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
668 				'CRH', 'BANK_CHARGES', l_rma_rec,
669 				l_dist_rec.code_combination_id);
670 	 l_bank_amt := nvl(l_crh_rec_new.factor_discount_amount,0) - nvl(L_crh_rec_old.factor_discount_amount,0);
671 	 l_acctd_bank_amt := nvl(l_crh_rec_new.acctd_factor_discount_amount,0) - nvl(L_crh_rec_old.acctd_factor_discount_amount,0);
672       END IF;
673 
674       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
675       IF ( l_bank_amt < 0 )
676       THEN
677          l_dist_rec.amount_dr := NULL;
678          l_dist_rec.amount_cr := -l_bank_amt;
679          l_dist_rec.acctd_amount_dr := NULL;
680          l_dist_rec.acctd_amount_cr := -l_acctd_bank_amt;
681       ELSE
682          l_dist_rec.amount_dr := l_bank_amt;
683          l_dist_rec.amount_cr := NULL;
684          l_dist_rec.acctd_amount_dr := l_acctd_bank_amt;
685          l_dist_rec.acctd_amount_cr := NULL;
686       END IF;
687 /*
688       IF ( l_acctd_bank_amt < 0 )
689       THEN
690          l_dist_rec.acctd_amount_dr := NULL;
691          l_dist_rec.acctd_amount_cr := -l_acctd_bank_amt;
692       ELSE
693          l_dist_rec.acctd_amount_dr := l_acctd_bank_amt;
694          l_dist_rec.acctd_amount_cr := NULL;
695       END IF;
696 */
697       arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
698 
699      /* need to insert records into the MRC table.  Calling new
700         mrc engine */
701 
702      ar_mrc_engine2.maintain_mrc_data2(
703                               p_event_mode => 'INSERT',
704                               p_table_name => 'AR_DISTRIBUTIONS',
705                               p_mode       => 'SINGLE',
706                               p_key_value  =>  l_dist_rec.line_id,
710 
707                               p_row_info   =>  l_dist_rec);
708 
709    END IF;
711    -- If exchange rate has been changed
712    -- Insert a record into ar_rate_adjustments
713    -- Call arplbrad.main() to take care the ar_cash_receipts, ar_payment_schedules
714    -- and ar_receivable_applications/ar_misc_cash_distributions
715    --
716    -- 17-MAY-1999 J Rautiainen truncation exits on exchange rate so the comparison
717    -- was changed from comparing exchange rates to comparing accounted amounts in order
718    -- to fix bug 874052.
719    -- Commented out NOCOPY for bug fix 874052 IF ( l_crh_rec_old.exchange_rate <> l_crh_rec_new.exchange_rate )
720    IF ( (l_crh_rec_old.acctd_amount + NVL(l_crh_rec_old.acctd_factor_discount_amount,0) )
721         <> (l_crh_rec_new.acctd_amount + NVL(l_crh_rec_new.acctd_factor_discount_amount,0)))
722    THEN
723       l_radj_rec.cash_receipt_id := p_cr_id;
724       l_radj_rec.gain_loss := arp_util.functional_amount(
725 					       	l_cr_rec.amount,
726 					       	ARP_GLOBAL.functional_currency,
727 						(l_crh_rec_new.exchange_rate - l_crh_rec_old.exchange_rate),
728 						NULL,NULL );
729       l_radj_rec.gl_date := p_gl_date;
730       l_radj_rec.new_exchange_date := l_crh_rec_new.exchange_date;
731       l_radj_rec.new_exchange_rate := l_crh_rec_new.exchange_rate;
732       l_radj_rec.new_exchange_rate_type:= l_crh_rec_new.exchange_rate_type ;
733       l_radj_rec.old_exchange_date := l_crh_rec_old.exchange_date;
734       l_radj_rec.old_exchange_rate := l_crh_rec_old.exchange_rate;
735       l_radj_rec.old_exchange_rate_type:= l_crh_rec_old.exchange_rate_type;
736       l_radj_rec.gl_posted_date := NULL;
737       l_radj_rec.posting_control_id := -3;
738       l_radj_rec.created_from := substrb(p_module_name||'ARP_CASHBOOK.CLEAR',1,30);
739       arp_rate_adjustments_pkg.insert_p( l_radj_rec,l_radj_rec.rate_adjustment_id  );
740 
741       arp_rate_adj.main(
742 			p_cr_id,
743 			l_radj_rec.new_exchange_date,
744 			l_radj_rec.new_exchange_rate,
745 			l_radj_rec.new_exchange_rate_type,
746 			l_radj_rec.gl_date,
747 			ARP_GLOBAL.created_by,
748 			ARP_GLOBAL.creation_date,
749 			ARP_GLOBAL.last_updated_by,
750 			ARP_GLOBAL.last_update_date,
751 			ARP_GLOBAL.last_update_login,
752 			FALSE,
753 			l_crh_rec_new.cash_receipt_history_id
754 			);
755 
756    END IF;
757 
758 
759   -- Insert value date into CR record
760 
761   UPDATE AR_CASH_RECEIPTS
762   SET actual_value_date = p_actual_value_date,
763       rec_version_number =  nvl(rec_version_number,1)+1 /* bug 3372585 */
764   WHERE cash_receipt_id = p_cr_id;
765 
766    -- Populate OUT NOCOPY parameters
767    p_crh_id := l_crh_rec_new.cash_receipt_history_id;
768    IF PG_DEBUG in ('Y', 'C') THEN
769       arp_util.debug( '<<<<<<< arp_cashbook.clear' );
770    END IF;
771 
772 EXCEPTION
773      WHEN OTHERS THEN
774 	  IF PG_DEBUG in ('Y', 'C') THEN
775 	     arp_util.debug( 'EXCEPTION: ARP_CASHBOOK.clear' );
776 	  END IF;
777           RAISE;
778 
779 END clear;
780 
781 PROCEDURE unclear(
782 		p_cr_id       		IN ar_cash_receipts.cash_receipt_id%TYPE,
783 		p_trx_date		IN ar_cash_receipt_history.trx_date%TYPE,
784 		p_gl_date		IN ar_cash_receipt_history.gl_date%TYPE,
785 		p_actual_value_date	IN ar_cash_receipts.actual_value_date%TYPE,
786 		p_module_name   	IN VARCHAR2,
787 		p_module_version   	IN VARCHAR2,
788 		p_crh_id		OUT NOCOPY ar_cash_receipt_history.cash_receipt_history_id%TYPE ) IS
789 --
790 l_cr_rec	ar_cash_receipts%ROWTYPE;
791 l_crh_rec_prv_stat ar_cash_receipt_history%ROWTYPE;
792 l_crh_rec_old	ar_cash_receipt_history%ROWTYPE;
793 l_crh_rec_new	ar_cash_receipt_history%ROWTYPE;
794 l_rma_rec	ar_receipt_method_accounts%ROWTYPE;
795 l_radj_rec      ar_rate_adjustments%ROWTYPE;
796 l_dist_rec ar_distributions%ROWTYPE;
797 l_receipt_amt ar_cash_receipt_history.amount%TYPE;
798 l_acctd_receipt_amt ar_cash_receipt_history.acctd_amount%TYPE;
799 l_new_crh_id_fr_radj ar_cash_receipt_history.cash_receipt_history_id%TYPE;
800 
801 l_xla_ev_rec   arp_xla_events.xla_events_type;
802 
803 BEGIN
804 
805    IF PG_DEBUG in ('Y', 'C') THEN
806       arp_util.debug( '>>>>>>> arp_cashbook.unclear' );
807    END IF;
808 
809 
810    --Setting the Org Context Bug5212892
811    ar_mo_global_cache.populate;
812    arp_global.init_global(mo_global.get_current_org_id);
813    arp_standard.init_standard(mo_global.get_current_org_id);
814 
815 
816    -- Assume this receipt has already been locked
817 
818    -- Validate the GL Date is in open or future period
819 
820    -- Fetch the history record
821    arp_cr_history_pkg.fetch_f_crid( p_cr_id, l_crh_rec_old );
822 
823    -- Check if this receipt has already been reversed or unclear, then
824    -- fail and give an error message
825    IF ( l_crh_rec_old.status = 'REVERSED')
826    	THEN
827         	fnd_message.set_name('AR', 'AR_CANNOT_UNCLEAR_REV_RECEIPT' );
828  	 	app_exception.raise_exception;
829    	ELSE
830 		IF ( l_crh_rec_old.status <> 'CLEARED' )
831    		THEN
832         		fnd_message.set_name('AR', 'AR_RECEIPT_CANNOT_UNCLEAR' );
833          		app_exception.raise_exception;
834    		END IF;
835    END IF;
836    -- make sure the receipt was not created as CLEARED
837    -- (in that case there is no previous state, and we return to caller
838    -- without error (see Bug 305482)):
839 
840    IF ( l_crh_rec_old.prv_stat_cash_receipt_hist_id IS NOT NULL )
844      arp_cr_history_pkg.fetch_p( l_crh_rec_old.prv_stat_cash_receipt_hist_id, l_crh_rec_prv_stat );
841    THEN
842 
843      -- Fetch the history record of the prv status
845 
846      -- Fetch the cash receipt record
847      l_cr_rec.cash_receipt_id := p_cr_id;
848      arp_cash_receipts_pkg.fetch_p( l_cr_rec );
849 
850      --  11.5 VAT changes:
851      l_dist_rec.currency_code            := l_cr_rec.currency_code;
852      l_dist_rec.currency_conversion_rate := l_crh_rec_old.exchange_rate;
853      l_dist_rec.currency_conversion_type := l_crh_rec_old.exchange_rate_type;
854      l_dist_rec.currency_conversion_date := l_crh_rec_old.exchange_date;
855      l_dist_rec.third_party_id           := l_cr_rec.pay_from_customer;
856      l_dist_rec.third_party_sub_id       := l_cr_rec.customer_site_use_id;
857 
858      -- Fetch the receipt method bank account record
859      arp_rm_accounts_pkg.fetch_p( l_cr_rec.receipt_method_id,
860 			     l_cr_rec.remit_bank_acct_use_id,
861 			     l_rma_rec );
862 
863 
864      IF PG_DEBUG in ('Y', 'C') THEN
865         arp_standard.debug('unclear: ' || 'crh_id_old: ' || to_char(l_crh_rec_old.cash_receipt_history_id));
866         arp_standard.debug('unclear: ' || 'crh_id_prv_stat: ' || to_char(l_crh_rec_prv_stat.cash_receipt_history_id));
867      END IF;
868 
869 
870      -- Insert a new history record
871      l_receipt_amt := l_crh_rec_old.amount + nvl(L_crh_rec_old.factor_discount_amount,0);
872      l_acctd_receipt_amt := l_crh_rec_old.acctd_amount + nvl(L_crh_rec_old.acctd_factor_discount_amount,0);
873      l_crh_rec_new.amount := l_crh_rec_prv_stat.amount;
874      l_crh_rec_new.factor_discount_amount := l_crh_rec_prv_stat.factor_discount_amount;
875      -- Changes for triangulation: If exchange rate type is not
876      -- user, call GL API to calculate accounted amount
877 
878      -- Bug 925765: gl api was called with 'User' when it shouldn't be.
879      -- Problem was that IF statement compared l_crh_rec_old.exchange_rate_type
880      -- with 'User', but then we're really going to use the earlier value from
881      -- l_crh_rec_prv_stat.exchange_rate_type.
882 
883      IF (l_crh_rec_prv_stat.exchange_rate_type = 'User') THEN
884        l_crh_rec_new.acctd_amount := arp_util.functional_amount(
885 						l_crh_rec_prv_stat.amount,
886 						ARP_GLOBAL.functional_currency,
887 						nvl(l_crh_rec_prv_stat.exchange_rate,1),
888 						NULL,NULL );
889      ELSE
890        l_crh_rec_new.acctd_amount := gl_currency_api.convert_amount(
891 					l_cr_rec.currency_code,
892 					ARP_GLOBAL.functional_currency,
893 					l_crh_rec_prv_stat.exchange_date,
894 					l_crh_rec_prv_stat.exchange_rate_type,
895 					l_crh_rec_prv_stat.amount);
896      END IF;
897 
898      l_crh_rec_new.acctd_factor_discount_amount := l_acctd_receipt_amt - L_crh_rec_new.acctd_amount;
899 
900      -- Bug 925765: we were using the exchange rate, date, and type from
901      -- the l_crh_rec_old record, but we should have used the values from
902      -- the l_crh_rec_prv_stat record, since we're going back to that status.
903 
904      l_crh_rec_new.exchange_date := l_crh_rec_prv_stat.exchange_date;
905      l_crh_rec_new.exchange_rate := l_crh_rec_prv_stat.exchange_rate;
906      l_crh_rec_new.exchange_rate_type := l_crh_rec_prv_stat.exchange_rate_type;
907      l_crh_rec_new.cash_receipt_id := p_cr_id;
908      l_crh_rec_new.status := l_crh_rec_prv_stat.status;
909      l_crh_rec_new.trx_date := p_trx_date;
910      l_crh_rec_new.first_posted_record_flag := 'N';
911      l_crh_rec_new.postable_flag := 'Y';
912      l_crh_rec_new.factor_flag := l_crh_rec_old.factor_flag;
913      l_crh_rec_new.gl_date := p_gl_date;
914      l_crh_rec_new.current_record_flag := 'Y';
915      l_crh_rec_new.batch_id := l_crh_rec_prv_stat.batch_id;
916      IF ( l_crh_rec_old.factor_flag = 'Y' )
917      THEN
918         arp_cr_util.get_dist_ccid( l_crh_rec_prv_stat.cash_receipt_id,
919 				'CRH', 'FACTOR', l_rma_rec,
920 				l_crh_rec_new.account_code_combination_id);
921      ELSE
922       arp_cr_util.get_dist_ccid( l_crh_rec_prv_stat.cash_receipt_id,
923 				'CRH', 'REMITTANCE', l_rma_rec,
924 				l_crh_rec_new.account_code_combination_id);
925      END IF;
926      l_crh_rec_new.reversal_gl_date := NULL;
927      l_crh_rec_new.reversal_cash_receipt_hist_id := NULL;
928      arp_cr_util.get_dist_ccid( l_crh_rec_prv_stat.cash_receipt_id,
929 				'CRH', 'BANK_CHARGES', l_rma_rec,
930 				l_crh_rec_new.bank_charge_account_ccid);
931      l_crh_rec_new.posting_control_id := -3;
932      l_crh_rec_new.reversal_posting_control_id := NULL;
933      l_crh_rec_new.gl_posted_date := NULL;
934      l_crh_rec_new.reversal_gl_posted_date := NULL;
935      l_crh_rec_new.prv_stat_cash_receipt_hist_id := l_crh_rec_prv_stat.prv_stat_cash_receipt_hist_id;
936      l_crh_rec_new.created_from := substrb(p_module_name||'ARP_CASHBOOK.UNCLEAR',1,30);
937      l_crh_rec_new.reversal_created_from := NULL;
938      arp_cr_history_pkg.insert_p( l_crh_rec_new, l_crh_rec_new.cash_receipt_history_id );
939 
940      -- Update the old history record
941      l_crh_rec_old.current_record_flag := NULL;
942      l_crh_rec_old.reversal_gl_date := p_gl_date;
943      l_crh_rec_old.reversal_cash_receipt_hist_id := l_crh_rec_new.cash_receipt_history_id;
944      l_crh_rec_old.reversal_posting_control_id := -3;
945      l_crh_rec_old.reversal_created_from := substrb(p_module_name||'ARP_CASHBOOK.UNCLEAR',1,30);
946      arp_cr_history_pkg.update_p( l_crh_rec_old );
947 
948 --BUG#5569338
952     l_xla_ev_rec.xla_mode        := 'O';
949     l_xla_ev_rec.xla_from_doc_id := p_cr_id;
950     l_xla_ev_rec.xla_to_doc_id   := p_cr_id;
951     l_xla_ev_rec.xla_doc_table   := 'CRH';
953     l_xla_ev_rec.xla_call        := 'B';
954     ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
955 
956 
957 
958      -- Insert the remittance/short_term_debt account ar_distributions record
959 /* skoukunt: comment to Fix bug 1198295
960      IF ( l_receipt_amt <>0 ) OR
961         ( l_acctd_receipt_amt <> 0 )
962      THEN
963 */
964         l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
965         l_dist_rec.source_table := 'CRH';
966         IF ( l_crh_rec_old.factor_flag = 'Y' )
967         THEN
968            l_dist_rec.source_type := 'SHORT_TERM_DEBT';
969 	   arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
970 				'CRH', 'SHORT_TERM_DEBT', l_rma_rec,
971 				l_dist_rec.code_combination_id);
972         ELSE
973            l_dist_rec.source_type := 'REMITTANCE';
974     	   arp_cr_util.get_dist_ccid( l_crh_rec_prv_stat.cash_receipt_id,
975 				'CRH', 'REMITTANCE', l_rma_rec,
976 				l_dist_rec.code_combination_id);
977         END IF;
978 
979       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
980         IF ( l_receipt_amt < 0 )
981         THEN
982            l_dist_rec.amount_dr := NULL;
983            l_dist_rec.amount_cr := -l_receipt_amt;
984            l_dist_rec.acctd_amount_dr := NULL;
985            l_dist_rec.acctd_amount_cr := -l_acctd_receipt_amt;
986         ELSE
987            l_dist_rec.amount_dr := l_receipt_amt;
988            l_dist_rec.amount_cr := NULL;
989            l_dist_rec.acctd_amount_dr := l_acctd_receipt_amt;
990            l_dist_rec.acctd_amount_cr := NULL;
991         END IF;
992 /*
993         IF ( l_acctd_receipt_amt < 0 )
994         THEN
995            l_dist_rec.acctd_amount_dr := NULL;
996            l_dist_rec.acctd_amount_cr := -l_acctd_receipt_amt;
997         ELSE
998            l_dist_rec.acctd_amount_dr := l_acctd_receipt_amt;
999            l_dist_rec.acctd_amount_cr := NULL;
1000         END IF;
1001 */
1002         arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
1003 
1004 --     END IF;
1005 
1006      -- Insert the cash account ar_distributions record
1007 /* skoukunt: comment to Fix bug 1198295
1008      IF ( l_crh_rec_old.amount <>0 ) OR
1009         ( l_crh_rec_old.acctd_amount <> 0 )
1010      THEN
1011 */
1012         l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
1013         l_dist_rec.source_table := 'CRH';
1014         l_dist_rec.source_type := 'CASH';
1015         arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
1016 				'CRH', 'CASH', l_rma_rec,
1017 				l_dist_rec.code_combination_id);
1018       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
1019         IF ( l_crh_rec_old.amount < 0 )
1020         THEN
1021            l_dist_rec.amount_cr := NULL;
1022            l_dist_rec.amount_dr := -l_crh_rec_old.amount;
1023            l_dist_rec.acctd_amount_cr := NULL;
1024            l_dist_rec.acctd_amount_dr := -l_crh_rec_old.acctd_amount;
1025         ELSE
1026            l_dist_rec.amount_cr := l_crh_rec_old.amount;
1027            l_dist_rec.amount_dr := NULL;
1028            l_dist_rec.acctd_amount_cr := l_crh_rec_old.acctd_amount;
1029            l_dist_rec.acctd_amount_dr := NULL;
1030         END IF;
1031 /*
1032         IF ( l_crh_rec_old.acctd_amount < 0 )
1033         THEN
1034            l_dist_rec.acctd_amount_cr := NULL;
1035            l_dist_rec.acctd_amount_dr := -l_crh_rec_old.acctd_amount;
1036         ELSE
1037            l_dist_rec.acctd_amount_cr := l_crh_rec_old.acctd_amount;
1038            l_dist_rec.acctd_amount_dr := NULL;
1039         END IF;
1040 */
1041         arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
1042 
1043 
1044 --     END IF;
1045 
1046      -- Insert the bank charge account ar_distributions record
1047      IF ( l_crh_rec_old.factor_discount_amount <>0 ) OR
1048         ( l_crh_rec_old.acctd_factor_discount_amount <> 0 )
1049      THEN
1050         l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
1051         l_dist_rec.source_table := 'CRH';
1052         l_dist_rec.source_type := 'BANK_CHARGES';
1053         arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
1054 	  			'CRH', 'BANK_CHARGES', l_rma_rec,
1055 				l_dist_rec.code_combination_id);
1056 
1057       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
1058         IF ( l_crh_rec_old.factor_discount_amount < 0 )
1059         THEN
1060            l_dist_rec.amount_cr := NULL;
1061            l_dist_rec.amount_dr := -l_crh_rec_old.factor_discount_amount;
1062            l_dist_rec.acctd_amount_cr := NULL;
1063            l_dist_rec.acctd_amount_dr := -l_crh_rec_old.acctd_factor_discount_amount;
1064         ELSE
1065            l_dist_rec.amount_cr := l_crh_rec_old.factor_discount_amount;
1066            l_dist_rec.amount_dr := NULL;
1067            l_dist_rec.acctd_amount_cr := l_crh_rec_old.acctd_factor_discount_amount;
1068            l_dist_rec.acctd_amount_dr := NULL;
1069         END IF;
1070 /*
1071         IF ( l_crh_rec_old.acctd_factor_discount_amount < 0 )
1072         THEN
1073            l_dist_rec.acctd_amount_cr := NULL;
1074            l_dist_rec.acctd_amount_dr := -l_crh_rec_old.acctd_factor_discount_amount;
1078         END IF;
1075         ELSE
1076            l_dist_rec.acctd_amount_cr := l_crh_rec_old.acctd_factor_discount_amount;
1077            l_dist_rec.acctd_amount_dr := NULL;
1079 */
1080         arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
1081 
1082 
1083      END IF;
1084 
1085      -- This is a fix to conform to the old 10.5 design,when
1086      -- you unclear a row, it should retain the same exchange rate as
1087      -- the original remitted row.
1088      -- So, if the rate of the cleared row is different from the original
1089      -- remitted row, then we do a rate adjustment to adjust the exchange rate
1090      -- back to it's old rate comes from the remitted row.
1091      l_new_crh_id_fr_radj := NULL;
1092 
1093      -- 17-MAY-1999 J Rautiainen truncation exits on exchange rate so the comparison
1094      -- was changed from comparing exchange rates to comparing accounted amounts in order
1095      -- to fix bug 874052.
1096      -- Commented out NOCOPY for bugfix 874052 IF ( nvl(l_crh_rec_prv_stat.exchange_rate,1) <> nvl(l_crh_rec_new.exchange_rate,1))
1097      IF ( (nvl(l_crh_rec_prv_stat.acctd_amount,1) + NVL(l_crh_rec_prv_stat.acctd_factor_discount_amount,0) )
1098           <> (nvl(l_crh_rec_new.acctd_amount,1) + NVL(l_crh_rec_new.acctd_factor_discount_amount,0)))
1099      THEN
1100         l_radj_rec.cash_receipt_id := p_cr_id;
1101         l_radj_rec.gain_loss := arp_util.functional_amount(
1102 					       	l_cr_rec.amount,
1103 					       	ARP_GLOBAL.functional_currency,
1104 						(l_crh_rec_prv_stat.exchange_rate - l_crh_rec_new.exchange_rate),
1105 						NULL,NULL );
1106         l_radj_rec.gl_date := p_gl_date;
1107         l_radj_rec.new_exchange_date := l_crh_rec_prv_stat.exchange_date;
1108         l_radj_rec.new_exchange_rate := l_crh_rec_prv_stat.exchange_rate;
1109         l_radj_rec.new_exchange_rate_type := l_crh_rec_prv_stat.exchange_rate_type ;
1110         l_radj_rec.old_exchange_date := l_crh_rec_new.exchange_date;
1111         l_radj_rec.old_exchange_rate := l_crh_rec_new.exchange_rate;
1112         l_radj_rec.old_exchange_rate_type:= l_crh_rec_new.exchange_rate_type;
1113         l_radj_rec.gl_posted_date := NULL;
1114         l_radj_rec.posting_control_id := -3;
1115         l_radj_rec.created_from := substrb(p_module_name||'ARP_CASHBOOK.UNCLEAR',1,30);
1116         arp_rate_adjustments_pkg.insert_p( l_radj_rec,l_radj_rec.rate_adjustment_id  );
1117 
1118         arp_rate_adj.main(
1119 			p_cr_id,
1120 	  		l_radj_rec.new_exchange_date,
1121 			l_radj_rec.new_exchange_rate,
1122 			l_radj_rec.new_exchange_rate_type,
1123 			l_radj_rec.gl_date,
1124 			ARP_GLOBAL.created_by,
1125 			ARP_GLOBAL.creation_date,
1126 			ARP_GLOBAL.last_updated_by,
1127 			ARP_GLOBAL.last_update_date,
1128 			ARP_GLOBAL.last_update_login,
1129 			TRUE,       -- should this be FALSE??? OS 7/6/99
1130 			l_new_crh_id_fr_radj
1131 			);
1132 
1133      END IF;
1134 
1135      -- Insert value date into CR record
1136 
1137      UPDATE AR_CASH_RECEIPTS
1138      SET actual_value_date = p_actual_value_date,
1139          rec_version_number =  nvl(rec_version_number,1)+1 /* bug 3372585 */
1140      WHERE cash_receipt_id = p_cr_id;
1141 
1142      -- Populate OUT NOCOPY parameters
1143      p_crh_id := nvl(l_new_crh_id_fr_radj,l_crh_rec_new.cash_receipt_history_id);
1144    ELSE
1145      -- if unclear() cannot be performed because of the receipt being
1146      -- created as CLEARED, we return NULL as crh_id.
1147      p_crh_id := NULL;
1148    END IF;
1149 
1150    IF PG_DEBUG in ('Y', 'C') THEN
1151       arp_util.debug( '<<<<<<<< arp_cashbook.unclear' );
1152    END IF;
1153 
1154 EXCEPTION
1155      WHEN OTHERS THEN
1156 	  IF PG_DEBUG in ('Y', 'C') THEN
1157 	     arp_util.debug( 'EXCEPTION: ARP_CASHBOOK.unclear' );
1158 	  END IF;
1159           RAISE;
1160 
1161 END unclear;
1162 
1163 PROCEDURE risk_eliminate(
1164 		p_cr_id       		IN ar_cash_receipts.cash_receipt_id%TYPE,
1165 		p_trx_date		IN ar_cash_receipt_history.trx_date%TYPE,
1166 		p_gl_date		IN ar_cash_receipt_history.gl_date%TYPE,
1167 		p_module_name   	IN VARCHAR2,
1168 		p_module_version   	IN VARCHAR2,
1169 		p_crh_id		OUT NOCOPY ar_cash_receipt_history.cash_receipt_history_id%TYPE ) IS
1170 --
1171 l_crh_rec_old	ar_cash_receipt_history%ROWTYPE;
1172 l_crh_rec_new	ar_cash_receipt_history%ROWTYPE;
1173 l_dist_rec ar_distributions%ROWTYPE;
1174 l_receipt_amt ar_cash_receipt_history.amount%TYPE;
1175 l_acctd_receipt_amt ar_cash_receipt_history.acctd_amount%TYPE;
1176 NULL_VAR   ar_receipt_method_accounts%ROWTYPE;
1177 l_cr_rec        ar_cash_receipts%ROWTYPE;
1178 l_risk_event_rec             arp_xla_events.xla_events_type;
1179 BEGIN
1180 
1181    IF PG_DEBUG in ('Y', 'C') THEN
1182       arp_util.debug( '>>>>>>> arp_cashbook.risk_eliminate' );
1183    END IF;
1184 
1185    -- Assume this receipt has already been locked
1186 
1187    -- Validate the GL Date is in open or future period
1188 
1189    -- Fetch cash receipt record for 11.5 VAT changes:
1190    l_cr_rec.cash_receipt_id := p_cr_id;
1191    arp_cash_receipts_pkg.fetch_p(l_cr_rec);
1192 
1193    -- Fetch the history record
1194    arp_cr_history_pkg.fetch_f_crid( p_cr_id, l_crh_rec_old );
1195 
1196    --  11.5 VAT changes:
1197    l_dist_rec.currency_code            := l_cr_rec.currency_code;
1198    l_dist_rec.currency_conversion_rate := l_crh_rec_old.exchange_rate;
1199    l_dist_rec.currency_conversion_type := l_crh_rec_old.exchange_rate_type;
1203 
1200    l_dist_rec.currency_conversion_date := l_crh_rec_old.exchange_date;
1201    l_dist_rec.third_party_id           := l_cr_rec.pay_from_customer;
1202    l_dist_rec.third_party_sub_id       := l_cr_rec.customer_site_use_id;
1204    -- Check if this receipt has already been risk eliminated,
1205    -- Also, if it's not factoring, cannot risk eliminate either.
1206    -- then fail and give an error message.
1207    IF ( l_crh_rec_old.status = 'RISK_ELIMINATED' ) OR
1208       ( l_crh_rec_old.factor_flag <> 'Y' )
1209    THEN
1210          fnd_message.set_name('AR', 'AR_CANNOT_ELIMINATE_RISK' );
1211          app_exception.raise_exception;
1212    END IF;
1213 
1214    -- Insert a new history record
1215    l_receipt_amt := l_crh_rec_old.amount + nvl(L_crh_rec_old.factor_discount_amount,0);
1216    l_acctd_receipt_amt := l_crh_rec_old.acctd_amount + nvl(L_crh_rec_old.acctd_factor_discount_amount,0);
1217    l_crh_rec_new.amount := l_crh_rec_old.amount;
1218    l_crh_rec_new.factor_discount_amount := l_crh_rec_old.factor_discount_amount;
1219    l_crh_rec_new.acctd_amount := l_crh_rec_old.acctd_amount;
1220    l_crh_rec_new.acctd_factor_discount_amount := l_crh_rec_old.acctd_factor_discount_amount;
1221 
1222    l_crh_rec_new.exchange_date := l_crh_rec_old.exchange_date;
1223    l_crh_rec_new.exchange_rate := l_crh_rec_old.exchange_rate;
1224    l_crh_rec_new.exchange_rate_type := l_crh_rec_old.exchange_rate_type;
1225    l_crh_rec_new.cash_receipt_id := p_cr_id;
1226    l_crh_rec_new.status := 'RISK_ELIMINATED';
1227    l_crh_rec_new.trx_date := p_trx_date;
1228    l_crh_rec_new.first_posted_record_flag := 'N';
1229    l_crh_rec_new.postable_flag := 'Y';
1230    l_crh_rec_new.factor_flag := l_crh_rec_old.factor_flag;
1231    l_crh_rec_new.gl_date := p_gl_date;
1232    l_crh_rec_new.current_record_flag := 'Y';
1233    arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
1234 				'CRH','SHORT_TERM_DEBT', NULL_VAR,
1235 				l_crh_rec_new.account_code_combination_id);
1236    l_crh_rec_new.reversal_gl_date := NULL;
1237    l_crh_rec_new.reversal_cash_receipt_hist_id := NULL;
1238    arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
1239 				'CRH', 'BANK_CHARGES', NULL_VAR,
1240 				l_crh_rec_new.bank_charge_account_ccid);
1241    l_crh_rec_new.posting_control_id := -3;
1242    l_crh_rec_new.reversal_posting_control_id := NULL;
1243    l_crh_rec_new.gl_posted_date := NULL;
1244    l_crh_rec_new.reversal_gl_posted_date := NULL;
1245    l_crh_rec_new.prv_stat_cash_receipt_hist_id := l_crh_rec_old.cash_receipt_history_id;
1246    l_crh_rec_new.created_from := substrb(p_module_name||'ARP_CASHBOOK.RISK_ELIMINATE',1,30);
1247    l_crh_rec_new.reversal_created_from := NULL;
1248    arp_cr_history_pkg.insert_p( l_crh_rec_new, l_crh_rec_new.cash_receipt_history_id );
1249 
1250    -- Update the old history record
1251    l_crh_rec_old.current_record_flag := NULL;
1252    l_crh_rec_old.reversal_gl_date := p_gl_date;
1253    l_crh_rec_old.reversal_cash_receipt_hist_id := l_crh_rec_new.cash_receipt_history_id;
1254    l_crh_rec_old.reversal_posting_control_id := -3;
1255    l_crh_rec_old.reversal_created_from := substrb(p_module_name||'ARP_CASHBOOK.RISK_ELIMINATE',1,30);
1256    arp_cr_history_pkg.update_p( l_crh_rec_old );
1257 
1258    /* Bug 6494186 */
1259     l_risk_event_rec.xla_from_doc_id := p_cr_id;
1260     l_risk_event_rec.xla_to_doc_id   := p_cr_id;
1261     l_risk_event_rec.xla_doc_table   := 'CRH';
1262     l_risk_event_rec.xla_mode        := 'O';
1263     l_risk_event_rec.xla_call        := 'B';
1264     arp_xla_events.Create_Events(p_xla_ev_rec => l_risk_event_rec );
1265 
1266    -- Insert the short_term_debt account ar_distributions record
1267 /* skoukunt: comment to Fix bug 1198295
1268    IF ( l_receipt_amt <>0 ) OR
1269       ( l_acctd_receipt_amt <> 0 )
1270    THEN
1271 */
1272       l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
1273       l_dist_rec.source_table := 'CRH';
1274       l_dist_rec.source_type := 'SHORT_TERM_DEBT';
1275       arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
1276 				'CRH', 'SHORT_TERM_DEBT', NULL_VAR,
1277 				l_dist_rec.code_combination_id);
1278       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
1279       IF ( l_receipt_amt < 0 )
1280       THEN
1281          l_dist_rec.amount_dr := NULL;
1282          l_dist_rec.amount_cr := -l_receipt_amt;
1283          l_dist_rec.acctd_amount_dr := NULL;
1284          l_dist_rec.acctd_amount_cr := -l_acctd_receipt_amt;
1285       ELSE
1286          l_dist_rec.amount_dr := l_receipt_amt;
1287          l_dist_rec.amount_cr := NULL;
1288          l_dist_rec.acctd_amount_dr := l_acctd_receipt_amt;
1289          l_dist_rec.acctd_amount_cr := NULL;
1290       END IF;
1291 /*
1292       IF ( l_acctd_receipt_amt < 0 )
1293       THEN
1294          l_dist_rec.acctd_amount_dr := NULL;
1295          l_dist_rec.acctd_amount_cr := -l_acctd_receipt_amt;
1296       ELSE
1297          l_dist_rec.acctd_amount_dr := l_acctd_receipt_amt;
1298          l_dist_rec.acctd_amount_cr := NULL;
1299       END IF;
1300 */
1301       arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
1302 
1303         /* need to insert records into the MRC table.  Calling new
1304            mrc engine */
1305 
1306         ar_mrc_engine2.maintain_mrc_data2(
1307                               p_event_mode => 'INSERT',
1308                               p_table_name => 'AR_DISTRIBUTIONS',
1309                               p_mode       => 'SINGLE',
1310                               p_key_value  =>  l_dist_rec.line_id,
1314 
1311                               p_row_info   =>  l_dist_rec);
1312 
1313 --   END IF;
1315    -- Insert the factor account ar_distributions record
1316 /* skoukunt: comment to Fix bug 1198295
1317    IF ( l_crh_rec_old.amount <>0 ) OR
1318       ( l_crh_rec_old.acctd_amount <> 0 )
1319    THEN
1320 */
1321       l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
1322       l_dist_rec.source_table := 'CRH';
1323       l_dist_rec.source_type := 'FACTOR';
1324       arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
1325 				'CRH', 'FACTOR', NULL_VAR,
1326 				l_dist_rec.code_combination_id);
1327       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
1328       IF ( l_receipt_amt < 0 )
1329       THEN
1330          l_dist_rec.amount_cr := NULL;
1331          l_dist_rec.amount_dr := -l_receipt_amt;
1332          l_dist_rec.acctd_amount_cr := NULL;
1333          l_dist_rec.acctd_amount_dr := -l_acctd_receipt_amt;
1334       ELSE
1335          l_dist_rec.amount_cr := l_receipt_amt;
1336          l_dist_rec.amount_dr := NULL;
1337          l_dist_rec.acctd_amount_cr := l_acctd_receipt_amt;
1338          l_dist_rec.acctd_amount_dr := NULL;
1339       END IF;
1340 /*
1341       IF ( l_acctd_receipt_amt < 0 )
1342       THEN
1343          l_dist_rec.acctd_amount_cr := NULL;
1344          l_dist_rec.acctd_amount_dr := -l_acctd_receipt_amt;
1345       ELSE
1346          l_dist_rec.acctd_amount_cr := l_acctd_receipt_amt;
1347          l_dist_rec.acctd_amount_dr := NULL;
1348       END IF;
1349 */
1350       arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
1351 
1352         /* need to insert records into the MRC table.  Calling new
1353            mrc engine */
1354 
1355         ar_mrc_engine2.maintain_mrc_data2(
1356                               p_event_mode => 'INSERT',
1357                               p_table_name => 'AR_DISTRIBUTIONS',
1358                               p_mode       => 'SINGLE',
1359                               p_key_value  =>  l_dist_rec.line_id,
1360                               p_row_info   =>  l_dist_rec);
1361 
1362 --   END IF;
1363 
1364    -- Populate OUT NOCOPY parameters
1365    p_crh_id := l_crh_rec_new.cash_receipt_history_id;
1366    IF PG_DEBUG in ('Y', 'C') THEN
1367       arp_util.debug( '<<<<<<<< arp_cashbook.risk_eliminate' );
1368    END IF;
1369 
1370 EXCEPTION
1371      WHEN OTHERS THEN
1372 	  IF PG_DEBUG in ('Y', 'C') THEN
1373 	     arp_util.debug( 'EXCEPTION: ARP_CASHBOOK.risk_eliminate' );
1374 	  END IF;
1375           RAISE;
1376 
1377 END risk_eliminate;
1378 
1379 PROCEDURE undo_risk_eliminate(
1380 		p_cr_id       		IN ar_cash_receipts.cash_receipt_id%TYPE,
1381 		p_trx_date		IN ar_cash_receipt_history.trx_date%TYPE,
1382 		p_gl_date		IN ar_cash_receipt_history.gl_date%TYPE,
1383 		p_module_name   	IN VARCHAR2,
1384 		p_module_version   	IN VARCHAR2,
1385 		p_crh_id		OUT NOCOPY ar_cash_receipt_history.cash_receipt_history_id%TYPE ) IS
1386 --
1387 l_crh_rec_old	ar_cash_receipt_history%ROWTYPE;
1388 l_crh_rec_new	ar_cash_receipt_history%ROWTYPE;
1389 l_dist_rec ar_distributions%ROWTYPE;
1390 l_receipt_amt ar_cash_receipt_history.amount%TYPE;
1391 l_acctd_receipt_amt ar_cash_receipt_history.acctd_amount%TYPE;
1392 NULL_VAR   ar_receipt_method_accounts%ROWTYPE;
1393 l_cr_rec   ar_cash_receipts%ROWTYPE;
1394 BEGIN
1395 
1396    IF PG_DEBUG in ('Y', 'C') THEN
1397       arp_util.debug( '>>>>>>> arp_cashbook.undo_risk_eliminate' );
1398    END IF;
1399 
1400    -- Assume this receipt has already been locked
1401 
1402    -- Validate the GL Date is in open or future period
1403 
1404    -- Fetch cash receipt record for 11.5 VAT changes:
1405    l_cr_rec.cash_receipt_id := p_cr_id;
1406    arp_cash_receipts_pkg.fetch_p(l_cr_rec);
1407 
1408    -- Fetch the history record
1409    arp_cr_history_pkg.fetch_f_crid( p_cr_id, l_crh_rec_old );
1410 
1411    --  11.5 VAT changes:
1412    l_dist_rec.currency_code            := l_cr_rec.currency_code;
1413    l_dist_rec.currency_conversion_rate := l_crh_rec_old.exchange_rate;
1414    l_dist_rec.currency_conversion_type := l_crh_rec_old.exchange_rate_type;
1415    l_dist_rec.currency_conversion_date := l_crh_rec_old.exchange_date;
1416    l_dist_rec.third_party_id           := l_cr_rec.pay_from_customer;
1417    l_dist_rec.third_party_sub_id       := l_cr_rec.customer_site_use_id;
1418 
1419 
1420    -- Check if this receipt has already been risk eliminated,
1421    -- Also, if it's not factoring, cannot risk eliminate either.
1422    -- then fail and give an error message.
1423    IF ( l_crh_rec_old.status <> 'RISK_ELIMINATED' ) OR
1424       ( l_crh_rec_old.factor_flag <> 'Y' )
1425    THEN
1426          fnd_message.set_name('AR', 'AR_CANNOT_UNDO_RISK_ELIMINATE' );
1427          app_exception.raise_exception;
1428    END IF;
1429 
1430    -- Insert a new history record
1431    l_receipt_amt := l_crh_rec_old.amount + nvl(L_crh_rec_old.factor_discount_amount,0);
1432    l_acctd_receipt_amt := l_crh_rec_old.acctd_amount + nvl(L_crh_rec_old.acctd_factor_discount_amount,0);
1433    l_crh_rec_new.amount := l_crh_rec_old.amount;
1434    l_crh_rec_new.factor_discount_amount := l_crh_rec_old.factor_discount_amount;
1435    l_crh_rec_new.acctd_amount := l_crh_rec_old.acctd_amount;
1436    l_crh_rec_new.acctd_factor_discount_amount := l_crh_rec_old.acctd_factor_discount_amount;
1437 
1438    l_crh_rec_new.exchange_date := l_crh_rec_old.exchange_date;
1442    l_crh_rec_new.status := 'CLEARED';
1439    l_crh_rec_new.exchange_rate := l_crh_rec_old.exchange_rate;
1440    l_crh_rec_new.exchange_rate_type := l_crh_rec_old.exchange_rate_type;
1441    l_crh_rec_new.cash_receipt_id := p_cr_id;
1443    l_crh_rec_new.trx_date := p_trx_date;
1444    l_crh_rec_new.first_posted_record_flag := 'N';
1445    l_crh_rec_new.postable_flag := 'Y';
1446    l_crh_rec_new.factor_flag := l_crh_rec_old.factor_flag;
1447    l_crh_rec_new.gl_date := p_gl_date;
1448    l_crh_rec_new.current_record_flag := 'Y';
1449    arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
1450 				'CRH','FACTOR', NULL_VAR,
1451 				l_crh_rec_new.account_code_combination_id);
1452    l_crh_rec_new.reversal_gl_date := NULL;
1453    l_crh_rec_new.reversal_cash_receipt_hist_id := NULL;
1454    arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
1455 				'CRH', 'BANK_CHARGES', NULL_VAR,
1456 				l_crh_rec_new.bank_charge_account_ccid);
1457    l_crh_rec_new.posting_control_id := -3;
1458    l_crh_rec_new.reversal_posting_control_id := NULL;
1459    l_crh_rec_new.gl_posted_date := NULL;
1460    l_crh_rec_new.reversal_gl_posted_date := NULL;
1461    l_crh_rec_new.prv_stat_cash_receipt_hist_id := l_crh_rec_old.cash_receipt_history_id;
1462    l_crh_rec_new.created_from := substrb(p_module_name||'ARP_CASHBOOK.UNDO_RISK_ELIMINATE',1,30);
1463    l_crh_rec_new.reversal_created_from := NULL;
1464    arp_cr_history_pkg.insert_p( l_crh_rec_new, l_crh_rec_new.cash_receipt_history_id );
1465 
1466    -- Update the old history record
1467    l_crh_rec_old.current_record_flag := NULL;
1468    l_crh_rec_old.reversal_gl_date := p_gl_date;
1469    l_crh_rec_old.reversal_cash_receipt_hist_id := l_crh_rec_new.cash_receipt_history_id;
1470    l_crh_rec_old.reversal_posting_control_id := -3;
1471    l_crh_rec_old.reversal_created_from := substrb(p_module_name||'ARP_CASHBOOK.UNDO_RISK_ELIMINATE',1,30);
1472    arp_cr_history_pkg.update_p( l_crh_rec_old );
1473 
1474    -- Insert the short_term_debt account ar_distributions record
1475 /* skoukunt: comment to Fix bug 1198295
1476    IF ( l_receipt_amt <>0 ) OR
1477       ( l_acctd_receipt_amt <> 0 )
1478    THEN
1479 */
1480       l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
1481       l_dist_rec.source_table := 'CRH';
1482       l_dist_rec.source_type := 'SHORT_TERM_DEBT';
1483       arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
1484 				'CRH', 'SHORT_TERM_DEBT', NULL_VAR,
1485 				l_dist_rec.code_combination_id);
1486       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
1487       IF ( l_receipt_amt < 0 )
1488       THEN
1489          l_dist_rec.amount_cr := NULL;
1490          l_dist_rec.amount_dr := -l_receipt_amt;
1491          l_dist_rec.acctd_amount_cr := NULL;
1492          l_dist_rec.acctd_amount_dr := -l_acctd_receipt_amt;
1493       ELSE
1494          l_dist_rec.amount_cr := l_receipt_amt;
1495          l_dist_rec.amount_dr := NULL;
1496          l_dist_rec.acctd_amount_cr := l_acctd_receipt_amt;
1497          l_dist_rec.acctd_amount_dr := NULL;
1498       END IF;
1499 /*
1500       IF ( l_acctd_receipt_amt < 0 )
1501       THEN
1502          l_dist_rec.acctd_amount_cr := NULL;
1503          l_dist_rec.acctd_amount_dr := -l_acctd_receipt_amt;
1504       ELSE
1505          l_dist_rec.acctd_amount_cr := l_acctd_receipt_amt;
1506          l_dist_rec.acctd_amount_dr := NULL;
1507       END IF;
1508 */
1509       arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
1510 
1511         /* need to insert records into the MRC table.  Calling new
1512            mrc engine */
1513 
1514         ar_mrc_engine2.maintain_mrc_data2(
1515                               p_event_mode => 'INSERT',
1516                               p_table_name => 'AR_DISTRIBUTIONS',
1517                               p_mode       => 'SINGLE',
1518                               p_key_value  =>  l_dist_rec.line_id,
1519                               p_row_info   =>  l_dist_rec);
1520 
1521 
1522 --   END IF;
1523 
1524    -- Insert the factor account ar_distributions record
1525 /* skoukunt: comment to Fix bug 1198295
1526    IF ( l_crh_rec_old.amount <>0 ) OR
1527       ( l_crh_rec_old.acctd_amount <> 0 )
1528    THEN
1529 */
1530       l_dist_rec.source_id := l_crh_rec_new.cash_receipt_history_id;
1531       l_dist_rec.source_table := 'CRH';
1532       l_dist_rec.source_type := 'FACTOR';
1533       arp_cr_util.get_dist_ccid( l_crh_rec_old.cash_receipt_id,
1534 				'CRH', 'FACTOR', NULL_VAR,
1535 				l_dist_rec.code_combination_id);
1536       -- Fix 1119979, assign acctd_amount_dr and acctd_amount_cr in above condn
1537       IF ( l_receipt_amt < 0 )
1538       THEN
1539          l_dist_rec.amount_dr := NULL;
1540          l_dist_rec.amount_cr := -l_receipt_amt;
1541          l_dist_rec.acctd_amount_dr := NULL;
1542          l_dist_rec.acctd_amount_cr := -l_acctd_receipt_amt;
1543       ELSE
1544          l_dist_rec.amount_dr := l_receipt_amt;
1545          l_dist_rec.amount_cr := NULL;
1546          l_dist_rec.acctd_amount_dr := l_acctd_receipt_amt;
1547          l_dist_rec.acctd_amount_cr := NULL;
1548       END IF;
1549 /*
1550       IF ( l_acctd_receipt_amt < 0 )
1551       THEN
1552          l_dist_rec.acctd_amount_dr := NULL;
1553          l_dist_rec.acctd_amount_cr := -l_acctd_receipt_amt;
1554       ELSE
1555          l_dist_rec.acctd_amount_dr := l_acctd_receipt_amt;
1556          l_dist_rec.acctd_amount_cr := NULL;
1557       END IF;
1558 */
1559       arp_distributions_pkg.insert_p( l_dist_rec,l_dist_rec.line_id );
1560 
1564         ar_mrc_engine2.maintain_mrc_data2(
1561         /* need to insert records into the MRC table.  Calling new
1562            mrc engine */
1563 
1565                               p_event_mode => 'INSERT',
1566                               p_table_name => 'AR_DISTRIBUTIONS',
1567                               p_mode       => 'SINGLE',
1568                               p_key_value  =>  l_dist_rec.line_id,
1569                               p_row_info   =>  l_dist_rec);
1570 
1571 --   END IF;
1572 
1573    -- Populate OUT NOCOPY parameters
1574    p_crh_id := l_crh_rec_new.cash_receipt_history_id;
1575    IF PG_DEBUG in ('Y', 'C') THEN
1576       arp_util.debug( '<<<<<<<< arp_cashbook.undo_risk_eliminate' );
1577    END IF;
1578 
1579 EXCEPTION
1580      WHEN OTHERS THEN
1581 	  IF PG_DEBUG in ('Y', 'C') THEN
1582 	     arp_util.debug( 'EXCEPTION: ARP_CASHBOOK.undo_risk_eliminate' );
1583 	  END IF;
1584           RAISE;
1585 
1586 END undo_risk_eliminate;
1587 
1588 /*===========================================================================+
1589  | PROCEDURE                                                                 |
1590  |    ins_misc_txn                                                           |
1591  |                                                                           |
1592  | DESCRIPTION                                                               |
1593  |    Creates a miscellaneous receipt.                                       |
1594  |                                                                           |
1595  | SCOPE - PRIVATE                                                           |
1596  |                                                                           |
1597  | EXTERNAL PROCEDURES/FUNCTIONS ACCESSED                                    |
1598  |                                                                           |
1599  | ARGUMENTS                                                                 |
1600  |    IN:                                                                    |
1601  |    OUT:                                                                   |
1602  |                                                                           |
1603  | RETURNS                                                                   |
1604  |                                                                           |
1605  | NOTES                                                                     |
1606  |                                                                           |
1607  | MODIFICATION HISTORY                                                      |
1608  |   30-SEP-98  K.Murphy        Cash Management Enhancement: Allow creation  |
1609  |                              of Misc Receipts with distribution set.      |
1610  |                              Removed code that sets up the misc receipt   |
1611  |                              distribution record and calls the entity     |
1612  |                              handler.  Now calls the distribution         |
1613  |                              procedure passing the activity id.  This     |
1614  |                              procedure creates the required distribution  |
1615  |                              rows based on the activity.                  |
1616  |  04-JAN-99   D. Jancis       Modified for 11.5 VAT project.  Added calls  |
1617  |                              to get currency_code,                        |
1618  |                              currency_conversion_rate,                    |
1619  |                              currency_conversion_type, and                |
1620  |                              currency_conversion_date                     |
1621  |  01-MAR-99   D. Jancis       Modified routine to call GUI handler to do   |
1622  |                              all inserts.  Also added parameter tax_rate  |
1623  |                              required for VAT                             |
1624  |  04-JUN-99   GJWANG 		Derive distribution_set_id from in parameter |
1625  |                              receivables_trx_id when create misc receipt  |
1626  |  30-SEP-02   R Kader         Bug fix 2300268 : Added a new variable and   |
1627  |                              used this variable while calling the proc    |
1628  |                              insert_misc_receipt()
1629  |  21-FEB-03   R Kader         Bug fix 2742388 : Added a new variable and   |
1630  |                              used this variable while calling the proc    |
1631  |                              insert_misc_receipt()
1632  +===========================================================================*/
1633 
1634 PROCEDURE ins_misc_txn(
1635   p_receipt_number	    IN ar_cash_receipts.receipt_number%TYPE,
1636   p_document_number	    IN ar_cash_receipts.doc_sequence_value%TYPE,
1637   p_doc_sequence_id	    IN ar_cash_receipts.doc_sequence_id%TYPE,
1638   p_gl_date		    IN ar_cash_receipt_history.gl_date%TYPE,
1639   p_receipt_date	    IN ar_cash_receipts.receipt_date%TYPE,
1640   p_deposit_date	    IN ar_cash_receipts.deposit_date%TYPE,
1641   p_receipt_amount	    IN ar_cash_receipts.amount%TYPE,
1642   p_currency_code	    IN ar_cash_receipts.currency_code%TYPE,
1643   p_exchange_date           IN ar_cash_receipt_history.exchange_date%TYPE,
1644   p_exchange_rate_type      IN ar_cash_receipt_history.exchange_rate_type%TYPE,
1645   p_exchange_rate	    IN ar_cash_receipt_history.exchange_rate%TYPE,
1646   p_receipt_method_id	    IN ar_cash_receipts.receipt_method_id%TYPE,
1647   p_remit_bank_account_id   IN ar_cash_receipts.remit_bank_acct_use_id%TYPE,
1648   p_receivables_trx_id	    IN ar_cash_receipts.receivables_trx_id%TYPE,
1649   p_comments		    IN ar_cash_receipts.comments%TYPE,
1653   p_misc_payment_source     IN ar_cash_receipts.misc_payment_source%TYPE,
1650   p_vat_tax_id		    IN ar_cash_receipts.vat_tax_id%TYPE,
1651   p_reference_type	    IN ar_cash_receipts.reference_type%TYPE,
1652   p_reference_id	    IN ar_cash_receipts.reference_id%TYPE,
1654   p_anticipated_clearing_date IN ar_cash_receipts.anticipated_clearing_date%TYPE,
1655   p_module_name   	    IN VARCHAR2,
1656   p_module_version   	    IN VARCHAR2,
1657   p_cr_id		    OUT NOCOPY ar_cash_receipts.cash_receipt_id%TYPE,
1658   p_tax_rate                IN NUMBER ) IS
1659 --
1660 p_row_id       VARCHAR2(30);
1661 l_cr_id        ar_cash_Receipts.cash_receipt_id%TYPE;
1662 l_dis_set_id   ar_cash_receipts.distribution_set_id%TYPE;
1663 /* Bug fix 2300268 */
1664 l_tax_account_id               ar_distributions.code_combination_id%TYPE;
1665 /* Bug fix 2742388 */
1666 l_crh_id                       ar_cash_receipt_history.cash_receipt_history_id%TYPE;
1667 BEGIN
1668 
1669    IF PG_DEBUG in ('Y', 'C') THEN
1670       arp_util.debug( '>>>>>>> arp_cashbook.ins_misc_txn' );
1671    END IF;
1672 
1673    SELECT default_acctg_distribution_set
1674    INTO   l_dis_set_id
1675    FROM   ar_receivables_trx
1676    WHERE  receivables_trx_id = p_receivables_trx_id;
1677 
1678    /* Bug fix 2300268
1679       Get the tax account id corresponding to the vat_tax_id */
1680       IF p_vat_tax_id IS NOT NULL THEN
1681         /* bug 6034914  , commented out the select and added next 6 lines.
1682         SELECT tax_account_id
1683         INTO   l_tax_account_id
1684         FROM   ar_vat_tax
1685         WHERE  vat_tax_id = p_vat_tax_id;
1686         */
1687         l_tax_account_id := arp_etax_util.get_tax_account(p_vat_tax_id,
1688 			       trunc(p_receipt_date),'TAX','TAX_RATE');
1689 	if l_tax_account_id = -1
1690 	then
1691 	 l_tax_account_id := NULL;
1692 	end if;
1693      ELSE
1694         l_tax_account_id := NULL;
1695      END IF;
1696   /* End Bug fix 2300268 */
1697 
1698    IF PG_DEBUG in ('Y', 'C') THEN
1699       arp_util.debug('ins_misc_txn: ' || ' ====> Receipt_number ' || p_receipt_number);
1700       arp_util.debug('ins_misc_txn: ' || ' ====> distribution_set_id ' || l_dis_set_id);
1701    END IF;
1702 
1703    -- Bugs 975560/962254: Added NULL for P_USSGL_TRANSACTION_CODE
1704    -- parameter.
1705 
1706    ARP_PROCESS_MISC_RECEIPTS.insert_misc_receipt (
1707                             p_currency_code,
1708                             p_receipt_amount,
1709                             p_receivables_trx_id,
1710                             p_misc_payment_source,
1711                             p_receipt_number,
1712                             p_receipt_date,
1713                             p_gl_date,
1714                             p_comments,
1715                             p_exchange_rate_type,
1716                             p_exchange_rate,
1717                             p_exchange_date,
1718                             NULL,
1719                             NULL,
1720                             NULL,
1721                             NULL,
1722                             NULL,
1723                             NULL,
1724                             NULL,
1725                             NULL,
1726                             NULL,
1727                             NULL,
1728                             NULL,
1729                             NULL,
1730                             NULL,
1731                             NULL,
1732                             NULL,
1733                             NULL,
1734                             NULL,
1735                             p_remit_bank_account_id,
1736                             p_deposit_date,
1737                             p_receipt_method_id,
1738                             p_document_number,
1739                             p_doc_sequence_id,
1740                             l_dis_set_id,
1741                             p_reference_type,
1742                             p_reference_id,
1743                             p_vat_tax_id,
1744 			    NULL,              -- Bug 975560/962254
1745                             p_anticipated_clearing_date,
1746                             NULL,
1747                             NULL,
1748                             NULL,
1749                             NULL,
1750                             NULL,
1751                             NULL,
1752                             NULL,
1753                             NULL,
1754                             NULL,
1755                             NULL,
1756                             NULL,
1757                             NULL,
1758                             NULL,
1759                             NULL,
1760                             NULL,
1761                             NULL,
1762                             NULL,
1763                             NULL,
1764                             NULL,
1765                             NULL,
1766                             NULL,
1767                             l_cr_id,
1768                             p_row_id,
1769                             p_module_name,
1770                             p_module_version,
1771                             p_tax_rate,
1772                             l_tax_account_id, /* Bug fix 2300268 */
1773                             l_crh_id); /* Bug fix 2742388 */
1774 
1775    -- Populate OUT NOCOPY parameters
1776    p_cr_id := l_cr_id;
1777    IF PG_DEBUG in ('Y', 'C') THEN
1781 EXCEPTION
1778       arp_util.debug( '<<<<<<<< arp_cashbook.ins_misc_txn' );
1779    END IF;
1780 
1782      WHEN OTHERS THEN
1783 	  IF PG_DEBUG in ('Y', 'C') THEN
1784 	     arp_util.debug( 'EXCEPTION: arp_cashbook.ins_misc_txn' );
1785 	  END IF;
1786           RAISE;
1787 
1788 END ins_misc_txn;
1789 
1790 PROCEDURE reverse(
1791 	p_cr_id       IN ar_cash_receipts.cash_receipt_id%TYPE,
1792 	p_reversal_gl_date      IN ar_cash_receipt_history.reversal_gl_date%TYPE,
1793 	p_reversal_date         IN ar_cash_receipts.reversal_date%TYPE,
1794 	p_reversal_comments     IN ar_cash_receipts.reversal_comments%TYPE,
1795 	p_reversal_reason_code	IN ar_cash_receipts.reversal_reason_code%TYPE,
1796 	p_reversal_category	IN ar_cash_receipts.reversal_category%TYPE,
1797 	p_module_name   	IN VARCHAR2,
1798 	p_module_version   	IN VARCHAR2,
1799 	p_crh_id  OUT NOCOPY ar_cash_receipt_history.cash_receipt_history_id%TYPE) IS
1800 
1801   CURSOR current_crh_cur IS
1802     SELECT crh.cash_receipt_history_id
1803     FROM ar_cash_receipt_history crh
1804     WHERE crh.cash_receipt_id     = p_cr_id
1805     AND   crh.current_record_flag = 'Y'
1806     AND   crh.status              = 'REVERSED';
1807 
1808   l_reversal_category	     AR_CASH_RECEIPTS.REVERSAL_CATEGORY%TYPE;
1809   l_reversal_reason_code     AR_CASH_RECEIPTS.REVERSAL_REASON_CODE%TYPE;
1810   l_attribute_rec            AR_RECEIPT_API_PUB.ATTRIBUTE_REC_TYPE; /* Added for bug 2688370 */
1811   l_return_status            VARCHAR2(1);
1812   l_msg_count                NUMBER;
1813   l_msg_data                 VARCHAR2(2000);
1814   l_cr_id                    NUMBER;
1815   current_crh_rec            current_crh_cur%ROWTYPE;
1816   API_exception              EXCEPTION;
1817   l_msg_index                number;
1818 
1819 BEGIN
1820 
1821    IF PG_DEBUG in ('Y', 'C') THEN
1822       arp_util.debug( '>>>>>>> arp_cashbook.reverse' );
1823    END IF;
1824 
1825    -- if reversal category and reversal reason code are not
1826    -- passed in by CE, use 'NSF' as a default.
1827 
1828    IF (p_reversal_category IS NULL) THEN
1829      l_reversal_category := 'NSF';
1830    ELSE
1831      l_reversal_category := p_reversal_category;
1832    END IF;
1833 
1834    IF (p_reversal_reason_code IS NULL) THEN
1835      l_reversal_reason_code := 'NSF';
1836    ELSE
1837      l_reversal_reason_code := p_reversal_reason_code;
1838    END IF;
1839 
1840    /* Bugfix 2688370. Code modified so that the DFF values are passed
1841       to the call of  AR_RECEIPT_API_PUB.Reverse */
1842    SELECT attribute_category,
1843           attribute1, attribute2,
1844 	  attribute3, attribute4,
1845 	  attribute5, attribute6,
1846           attribute7, attribute8,
1847 	  attribute9, attribute10,
1848 	  attribute11, attribute12,
1849           attribute13, attribute14,
1850 	  attribute15
1851    INTO   l_attribute_rec.attribute_category,
1852           l_attribute_rec.attribute1, l_attribute_rec.attribute2,
1853 	  l_attribute_rec.attribute3, l_attribute_rec.attribute4,
1854 	  l_attribute_rec.attribute5, l_attribute_rec.attribute6,
1855           l_attribute_rec.attribute7, l_attribute_rec.attribute8,
1856 	  l_attribute_rec.attribute9, l_attribute_rec.attribute10,
1857 	  l_attribute_rec.attribute11, l_attribute_rec.attribute12,
1858           l_attribute_rec.attribute13, l_attribute_rec.attribute14,
1859 	  l_attribute_rec.attribute15
1860    FROM   ar_cash_receipts
1861    WHERE  cash_receipt_id = p_cr_id;
1862 
1863    BEGIN
1864 
1865      AR_RECEIPT_API_PUB.Reverse(p_api_version                  => 1.0,
1866                                 p_init_msg_list                => FND_API.G_TRUE,
1867                                 x_return_status                => l_return_status,
1868                                 x_msg_count                    => l_msg_count,
1869                                 x_msg_data                     => l_msg_data,
1870                                 p_cash_receipt_id              => p_cr_id,
1871                                 p_reversal_category_code       => l_reversal_category,
1872                                 p_reversal_gl_date             => p_reversal_gl_date,
1873                                 p_reversal_date                => p_reversal_date,
1874                                 p_reversal_reason_code         => l_reversal_reason_code,
1875                                 p_reversal_comments            => p_reversal_comments,
1876 				p_attribute_rec		       => l_attribute_rec,
1877                                 p_called_from                  => 'ARRECBKB');
1878 
1879    /*------------------------------------------------+
1880     | Write API output to the concurrent program log |
1881     +------------------------------------------------*/
1882     IF PG_DEBUG in ('Y', 'C') THEN
1883        arp_util.debug('reverse: ' || 'API error count '||to_char(NVL(l_msg_count,0)));
1884     END IF;
1885 
1886     IF NVL(l_msg_count,0)  > 0 Then
1887 
1888       IF l_msg_count  = 1 Then
1889        /*------------------------------------------------+
1890         | There is one message returned by the API, so it|
1891         | has been sent out NOCOPY in the parameter x_msg_data  |
1892         +------------------------------------------------*/
1893         IF PG_DEBUG in ('Y', 'C') THEN
1894            arp_util.debug('reverse: ' || l_msg_data);
1895         END IF;
1896 
1897       ELSIF l_msg_count > 1 Then
1898 
1899        /*-------------------------------------------------------+
1903 
1900         | There are more than one messages returned by the API, |
1901         | so call them in a loop and print the messages         |
1902         +-------------------------------------------------------*/
1904         FOR l_count IN 1..l_msg_count LOOP
1905 
1906              l_msg_data := FND_MSG_PUB.Get(FND_MSG_PUB.G_NEXT,FND_API.G_FALSE);
1907              IF PG_DEBUG in ('Y', 'C') THEN
1908                 arp_util.debug('reverse: ' || to_char(l_count)||' : '||l_msg_data);
1909              END IF;
1910 
1911         END LOOP;
1912 
1913       END IF;
1914 
1915     END IF;
1916 
1917    /*-----------------------------------------------------+
1918     | If API return status is not SUCCESS raise exception |
1919     +-----------------------------------------------------*/
1920     IF l_return_status = FND_API.G_RET_STS_SUCCESS Then
1921 
1922      /*-----------------------------------------------------+
1923       | Success do nothing, else branch introduced to make  |
1924       | sure that NULL case will also raise exception       |
1925       +-----------------------------------------------------*/
1926       NULL;
1927 
1928     ELSE
1929      /*---------------------------+
1930       | Error, raise an exception |
1931       +---------------------------*/
1932       RAISE API_exception;
1933 
1934     END IF;
1935 
1936    /*----------------------------------+
1937     | APIs propagate exception upwards |
1938     +----------------------------------*/
1939     EXCEPTION
1940       WHEN API_exception THEN
1941         IF PG_DEBUG in ('Y', 'C') THEN
1942            arp_util.debug('API Exception: arp_cashbook.reverse '||SQLERRM);
1943         END IF;
1944         FND_MSG_PUB.Get (FND_MSG_PUB.G_FIRST, FND_API.G_TRUE, l_msg_data, l_msg_index);
1945         FND_MESSAGE.Set_Encoded (l_msg_data);
1946         app_exception.raise_exception;
1947 
1948       WHEN OTHERS THEN
1949         IF PG_DEBUG in ('Y', 'C') THEN
1950            arp_util.debug('Exception: arp_cashbook.reverse '||SQLERRM);
1951         END IF;
1952         fnd_message.set_name('AR', 'AR_BR_CANNOT_REVERSE_REC');
1953         app_exception.raise_exception;
1954 
1955     END;
1956 
1957    -- Populate OUT NOCOPY parameters
1958    OPEN current_crh_cur;
1959    FETCH current_crh_cur INTO current_crh_rec;
1960 
1961    IF current_crh_cur%NOTFOUND THEN
1962      app_exception.raise_exception;
1963    END IF;
1964 
1965    CLOSE current_crh_cur;
1966 
1967    p_crh_id := current_crh_rec.cash_receipt_history_id;
1968 
1969    IF PG_DEBUG in ('Y', 'C') THEN
1970       arp_util.debug( '<<<<<<<< arp_cashbook.reverse' );
1971    END IF;
1972 
1973 EXCEPTION
1974      WHEN OTHERS THEN
1975 	  IF PG_DEBUG in ('Y', 'C') THEN
1976 	     arp_util.debug( 'EXCEPTION: ARP_CASHBOOK.reverse' );
1977 	  END IF;
1978           RAISE;
1979 
1980 END reverse;
1981 
1982 PROCEDURE debit_memo_reversal (
1983   p_cash_receipt_id       IN ar_cash_receipts.cash_receipt_id%TYPE,
1984   p_cc_id                 IN ra_cust_trx_line_gl_dist.code_combination_id%TYPE,
1985   p_dm_cust_trx_type_id   IN ra_cust_trx_types.cust_trx_type_id%TYPE,
1986   p_dm_cust_trx_type      IN ra_cust_trx_types.name%TYPE,
1987   p_reversal_gl_date      IN ar_cash_receipt_history.reversal_gl_date%TYPE,
1988   p_reversal_date         IN ar_cash_receipts.reversal_date%TYPE,
1989   p_reversal_category     IN ar_cash_receipts.reversal_category%TYPE,
1990   p_reversal_reason_code  IN ar_cash_receipts.reversal_reason_code%TYPE,
1991   p_reversal_comments     IN ar_cash_receipts.reversal_comments%TYPE,
1992   p_dm_number             OUT NOCOPY ar_payment_schedules.trx_number%TYPE,
1993   p_dm_doc_sequence_value IN ra_customer_trx.doc_sequence_value%TYPE,
1994   p_dm_doc_sequence_id    IN ra_customer_trx.doc_sequence_id%TYPE,
1995   p_tw_status             IN OUT NOCOPY VARCHAR2,
1996   p_module_name           IN VARCHAR2,
1997   p_module_version        IN VARCHAR2
1998                               ) IS
1999 
2000   CURSOR applied_to_reserved_br_cur IS
2001     SELECT 'Y'
2002     FROM   ar_payment_schedules ps,
2003            ar_receivable_applications ra
2004     WHERE  ra.cash_receipt_id = p_cash_receipt_id
2005     AND  ra.applied_payment_schedule_id = ps.payment_schedule_id
2006     AND  ps.reserved_type  IS NOT NULL
2007     AND  ps.reserved_value IS NOT NULL
2008     AND  ra.status         = 'APP'
2009     AND  ra.display        = 'Y';
2010 
2011   CURSOR applied_to_std_cur IS
2012     SELECT 'Y'
2013     FROM   ar_receivable_applications ra
2014     WHERE  ra.cash_receipt_id = p_cash_receipt_id
2015     AND  ra.applied_payment_schedule_id = -2
2016     AND  ra.display                     = 'Y';
2017 
2018   l_cr_rec                    ar_cash_receipts%ROWTYPE;
2019   l_dm_number                 ar_payment_schedules.trx_number%TYPE;
2020   applied_to_reserved_br_rec  applied_to_reserved_br_cur%ROWTYPE;
2021   applied_to_std_rec          applied_to_std_cur%ROWTYPE;
2022 
2023 
2024 BEGIN
2025 
2026     IF PG_DEBUG in ('Y', 'C') THEN
2027        arp_util.debug('>>>>>>>>>>>>>  arp_cashbook.debit_memo_reversal ');
2028     END IF;
2029 
2030     OPEN applied_to_std_cur;
2031     FETCH applied_to_std_cur INTO applied_to_std_rec;
2032 
2033     IF applied_to_std_cur%FOUND THEN
2034       fnd_message.set_name('AR', 'AR_RW_CANNOT_REVERSE_BR_STD');
2035       app_exception.raise_exception;
2039 
2036     END IF;
2037 
2038     CLOSE applied_to_std_cur;
2040     OPEN applied_to_reserved_br_cur;
2041     FETCH applied_to_reserved_br_cur INTO applied_to_reserved_br_rec;
2042 
2043     IF applied_to_reserved_br_cur%FOUND THEN
2044       fnd_message.set_name('AR', 'AR_RW_CANNOT_REVERSE_BR_STD');
2045       app_exception.raise_exception;
2046     END IF;
2047 
2048     CLOSE applied_to_reserved_br_cur;
2049 
2050     -- get cash receipt record:
2051     l_cr_rec.cash_receipt_id := p_cash_receipt_id;
2052     arp_cash_receipts_pkg.nowaitlock_fetch_p(l_cr_rec);
2053 
2054 
2055     arp_reverse_receipt.debit_memo_reversal(
2056                 l_cr_rec,
2057                 p_cc_id,
2058                 p_dm_cust_trx_type_id,
2059                 p_dm_cust_trx_type,
2060                 p_reversal_gl_date,
2061                 p_reversal_date,
2062                 p_reversal_category,
2063                 p_reversal_reason_code,
2064                 p_reversal_comments,
2065                 NULL, NULL,
2066                 NULL, NULL, NULL,
2067                 NULL, NULL, NULL,
2068                 NULL, NULL, NULL,
2069                 NULL, NULL, NULL,
2070                 NULL, NULL,
2071                 l_dm_number,
2072                 p_dm_doc_sequence_value,
2073                 p_dm_doc_sequence_id,
2074                 p_tw_status,
2075                 p_module_name,
2076                 p_module_version);
2077 
2078    -- Populate OUT NOCOPY parameters
2079    p_dm_number := l_dm_number;
2080 
2081 EXCEPTION
2082    WHEN OTHERS THEN
2083         IF PG_DEBUG in ('Y', 'C') THEN
2084            arp_util.debug('EXCEPTION: ARP_CASHBOOK.debit_memo_reversal');
2085         END IF;
2086         RAISE;
2087 END debit_memo_reversal;
2088 
2089 
2090 PROCEDURE Lock_Row(
2091         P_BATCH_ID                      IN ar_batches.batch_id%TYPE,
2092         P_AMOUNT                        IN ar_cash_receipt_history.amount%TYPE,
2093         P_ACCTD_AMOUNT                  IN ar_cash_receipt_history.acctd_amount%TYPE,
2094         P_NAME                          IN ar_batches.name%TYPE,
2095         P_BATCH_DATE                    IN ar_batches.batch_date%TYPE,
2096         P_GL_DATE                       IN ar_batches.gl_date%TYPE,
2097         P_STATUS                        IN ar_batches.status%TYPE,
2098         P_DEPOSIT_DATE                  IN ar_batches.deposit_date%TYPE,
2099         P_CLOSED_DATE                   IN ar_batches.closed_date%TYPE,
2100         P_TYPE                          IN ar_batches.type%TYPE,
2101         P_BATCH_SOURCE_ID               IN ar_batches.batch_source_id%TYPE,
2102         P_CONTROL_COUNT                 IN ar_batches.control_count%TYPE,
2103         P_CONTROL_AMOUNT                IN ar_batches.control_amount%TYPE,
2104         P_BATCH_APPLIED_STATUS          IN ar_batches.batch_applied_status%TYPE,
2105         P_CURRENCY_CODE                 IN ar_batches.currency_code%TYPE,
2106         P_EXCHANGE_RATE_TYPE            IN ar_batches.exchange_rate_type%TYPE,
2107         P_EXCHANGE_DATE                 IN ar_batches.exchange_date%TYPE,
2108         P_EXCHANGE_RATE                 IN ar_batches.exchange_rate%TYPE,
2109         P_TRANSMISSION_REQUEST_ID       IN ar_batches.transmission_request_id%TYPE,
2110         P_LOCKBOX_ID                    IN ar_batches.lockbox_id%TYPE,
2111         P_LOCKBOX_BATCH_NAME            IN ar_batches.lockbox_batch_name%TYPE,
2112         P_COMMENTS                      IN ar_batches.comments%TYPE,
2113         P_ATTRIBUTE_CATEGORY            IN ar_batches.attribute_category%TYPE,
2114         P_ATTRIBUTE1                    IN ar_batches.attribute1%TYPE,
2115         P_ATTRIBUTE2                    IN ar_batches.attribute2%TYPE,
2116         P_ATTRIBUTE3                    IN ar_batches.attribute3%TYPE,
2117         P_ATTRIBUTE4                    IN ar_batches.attribute4%TYPE,
2118         P_ATTRIBUTE5                    IN ar_batches.attribute5%TYPE,
2119         P_ATTRIBUTE6                    IN ar_batches.attribute6%TYPE,
2120         P_ATTRIBUTE7                    IN ar_batches.attribute7%TYPE,
2121         P_ATTRIBUTE8                    IN ar_batches.attribute8%TYPE,
2122         P_ATTRIBUTE9                    IN ar_batches.attribute9%TYPE,
2123         P_ATTRIBUTE10                   IN ar_batches.attribute10%TYPE,
2124         P_MEDIA_REFERENCE               IN ar_batches.media_reference%TYPE,
2125         P_OPERATION_REQUEST_ID          IN ar_batches.operation_request_id%TYPE,
2126         P_RECEIPT_METHOD_ID             IN ar_batches.receipt_method_id%TYPE,
2127         P_REMITTANCE_BANK_ACCOUNT_ID    IN ar_batches.remit_bank_acct_use_id%TYPE,
2128         P_RECEIPT_CLASS_ID              IN ar_batches.receipt_class_id%TYPE,
2129         P_ATTRIBUTE11                   IN ar_batches.attribute11%TYPE,
2130         P_ATTRIBUTE12                   IN ar_batches.attribute12%TYPE,
2131         P_ATTRIBUTE13                   IN ar_batches.attribute13%TYPE,
2132         P_ATTRIBUTE14                   IN ar_batches.attribute14%TYPE,
2133         P_ATTRIBUTE15                   IN ar_batches.attribute15%TYPE,
2134         P_PROGRAM_APPLICATION_ID        IN ar_batches.program_application_id%TYPE,
2135         P_PROGRAM_ID                    IN ar_batches.program_id%TYPE,
2136         P_PROGRAM_UPDATE_DATE           IN ar_batches.program_update_date%TYPE,
2137         P_REMITTANCE_BANK_BRANCH_ID     IN ar_batches.remittance_bank_branch_id%TYPE,
2138         P_REMIT_METHOD_CODE             IN ar_batches.remit_method_code%TYPE,
2139         P_REQUEST_ID                    IN ar_batches.request_id%TYPE,
2140         P_SET_OF_BOOKS_ID               IN ar_batches.set_of_books_id%TYPE,
2144 
2141         P_TRANSMISSION_ID               IN ar_batches.transmission_id%TYPE,
2142         P_BANK_DEPOSIT_NUMBER           IN ar_batches.bank_deposit_number%TYPE)
2143 IS
2145  CURSOR C IS
2146                 SELECT crh.cash_receipt_history_id
2147                 FROM   ar_cash_receipt_history crh, ar_cash_receipts acr
2148                 WHERE  crh.batch_id = P_BATCH_ID
2149                   AND  crh.status not in ('REVERSED')
2150                   AND  crh.cash_receipt_id = acr.cash_receipt_id
2151                 FOR UPDATE NOWAIT;
2152 
2153         CURSOR BATCH IS
2154                 SELECT *
2155                 FROM ar_batches
2156                 WHERE batch_id = P_BATCH_ID
2157 	        FOR UPDATE NOWAIT;
2158 
2159         Recinfo         C%ROWTYPE;
2160         Batchinfo       BATCH%ROWTYPE;
2161         c_batch_id      ar_batches.batch_id%TYPE;
2162 	c_amount	ar_cash_receipt_history.amount%TYPE;
2163 	c_acctd_amount	ar_cash_receipt_history.acctd_amount%TYPE;
2164 
2165   BEGIN
2166 
2167         OPEN C;
2168 	CLOSE C;
2169 
2170         OPEN BATCH;
2171         FETCH BATCH INTO Batchinfo;
2172         if (BATCH%NOTFOUND) then
2173          CLOSE BATCH;
2174          FND_MESSAGE.Set_Name('FND', 'FORM_RECORD_DELETED');
2175          APP_EXCEPTION.Raise_Exception;
2176         end if;
2177         CLOSE BATCH;
2178 
2179         SELECT sum(amount) , sum(acctd_amount)
2180 	INTO c_amount, c_acctd_amount
2181         FROM   ar_cash_receipt_history
2182         WHERE  batch_id = P_BATCH_ID
2183           AND  status not in ('REVERSED');
2184 
2185 
2186 	 if (   (c_amount                             = P_AMOUNT          OR
2187 		(c_amount is NULL AND P_AMOUNT is NULL))
2188             AND (c_acctd_amount                       = P_ACCTD_AMOUNT    OR
2189 		(c_acctd_amount is NULL AND P_ACCTD_AMOUNT is NULL))
2190             AND (Batchinfo.batch_id                   = P_BATCH_ID        OR
2191 		(Batchinfo.batch_id is NULL AND P_BATCH_ID is NULL))
2192             AND (Batchinfo.name                       = P_NAME            OR
2193 		(Batchinfo.name is NULL AND P_NAME is NULL))
2194             AND (Batchinfo.batch_date                 = P_BATCH_DATE      OR
2195 		(Batchinfo.batch_date is NULL AND P_BATCH_DATE is NULL))
2196             AND (Batchinfo.gl_date                    = P_GL_DATE         OR
2197 		(Batchinfo.gl_date is NULL AND P_GL_DATE is NULL))
2198             AND (Batchinfo.status                     = P_STATUS          OR
2199 		(Batchinfo.status is NULL AND P_STATUS is NULL))
2200             AND (Batchinfo.deposit_date               = P_DEPOSIT_DATE    OR
2201 		(Batchinfo.deposit_date is NULL AND P_DEPOSIT_DATE is NULL))
2202             AND (Batchinfo.closed_date                = P_CLOSED_DATE     OR
2203 		(Batchinfo.closed_date is NULL AND P_CLOSED_DATE is NULL))
2204             AND (Batchinfo.type                       = P_TYPE            OR
2205 		(Batchinfo.type is NULL AND P_TYPE is NULL))
2206             AND (Batchinfo.batch_source_id            = P_BATCH_SOURCE_ID OR
2207 		(Batchinfo.batch_source_id is NULL AND P_BATCH_SOURCE_ID is NULL))
2208             AND (Batchinfo.control_count              = P_CONTROL_COUNT   OR
2209 		(Batchinfo.control_count is NULL AND P_CONTROL_COUNT is NULL))
2210             AND (Batchinfo.control_amount             = P_CONTROL_AMOUNT  OR
2211 		(Batchinfo.control_amount is NULL AND P_CONTROL_AMOUNT is NULL))
2212             AND (Batchinfo.batch_applied_status       = P_BATCH_APPLIED_STATUS OR
2213 		(Batchinfo.batch_applied_status is NULL AND P_BATCH_APPLIED_STATUS is NULL))
2214             AND (Batchinfo.currency_code              = P_CURRENCY_CODE   OR
2215 		(Batchinfo.currency_code is NULL AND P_CURRENCY_CODE is NULL))
2216             AND (Batchinfo.exchange_rate_type         = P_EXCHANGE_RATE_TYPE  OR
2217 		(Batchinfo.exchange_rate_type is NULL AND P_EXCHANGE_RATE_TYPE is NULL))
2218             AND (Batchinfo.exchange_date              = P_EXCHANGE_DATE   OR
2219 		(Batchinfo.exchange_date is NULL AND P_EXCHANGE_DATE is NULL))
2220             AND (Batchinfo.exchange_rate              = P_EXCHANGE_RATE   OR
2221 		(Batchinfo.exchange_rate is NULL AND P_EXCHANGE_RATE is NULL))
2222             AND (Batchinfo.transmission_request_id    = P_TRANSMISSION_REQUEST_ID OR
2223 		(Batchinfo.transmission_request_id is NULL AND P_TRANSMISSION_REQUEST_ID is NULL))
2224             AND (Batchinfo.lockbox_id                 = P_LOCKBOX_ID      OR
2225 		(Batchinfo.lockbox_id is NULL AND P_LOCKBOX_ID is NULL))
2226             AND (Batchinfo.lockbox_batch_name         = P_LOCKBOX_BATCH_NAME  OR
2227 		(Batchinfo.lockbox_batch_name is NULL AND P_LOCKBOX_BATCH_NAME is NULL))
2228             AND (Batchinfo.comments                   = P_COMMENTS        OR
2229 		(Batchinfo.comments is NULL and P_COMMENTS is NULL))
2230 	    AND (Batchinfo.attribute_category         = P_ATTRIBUTE_CATEGORY OR
2231 		(Batchinfo.attribute_category is NULL AND P_ATTRIBUTE_CATEGORY is NULL))
2232             AND (Batchinfo.attribute1                 = P_ATTRIBUTE1         OR
2233 		(Batchinfo.attribute1 is NULL AND P_ATTRIBUTE1 is NULL))
2234 	    AND (Batchinfo.attribute2                 = P_ATTRIBUTE2         OR
2235 		(Batchinfo.attribute2 is NULL AND P_ATTRIBUTE2 is NULL))
2236             AND (Batchinfo.attribute3                 = P_ATTRIBUTE3         OR
2237 		(Batchinfo.attribute3 is NULL AND P_ATTRIBUTE3 is NULL))
2238             AND (Batchinfo.attribute4                 = P_ATTRIBUTE4         OR
2239 		(Batchinfo.attribute4 is NULL AND P_ATTRIBUTE4 is NULL))
2240             AND (Batchinfo.attribute5                 = P_ATTRIBUTE5         OR
2244             AND (Batchinfo.attribute7                 = P_ATTRIBUTE7         OR
2241 		(Batchinfo.attribute5 is NULL AND P_ATTRIBUTE5 is NULL))
2242             AND (Batchinfo.attribute6                 = P_ATTRIBUTE6         OR
2243 		(Batchinfo.attribute6 is NULL AND P_ATTRIBUTE6 is NULL))
2245 		(Batchinfo.attribute7 is NULL AND P_ATTRIBUTE7 is NULL))
2246             AND (Batchinfo.attribute8                 = P_ATTRIBUTE8         OR
2247 		(Batchinfo.attribute8 is NULL AND P_ATTRIBUTE8 is NULL))
2248             AND (Batchinfo.attribute9                 = P_ATTRIBUTE9         OR
2249 		(Batchinfo.attribute9 is NULL AND P_ATTRIBUTE9 is NULL))
2250             AND (Batchinfo.attribute10                = P_ATTRIBUTE10        OR
2251 		(Batchinfo.attribute10 is NULL AND P_ATTRIBUTE10 is NULL))
2252             AND (Batchinfo.media_reference            = P_MEDIA_REFERENCE    OR
2253 		(Batchinfo.media_reference is NULL AND P_MEDIA_REFERENCE is NULL))
2254             AND (Batchinfo.operation_request_id       = P_OPERATION_REQUEST_ID  OR
2255 		(Batchinfo.operation_request_id is NULL AND P_OPERATION_REQUEST_ID is NULL))
2256             AND (Batchinfo.receipt_method_id          = P_RECEIPT_METHOD_ID  OR
2257 		(Batchinfo.receipt_method_id is NULL AND P_RECEIPT_METHOD_ID is NULL))
2258             AND (Batchinfo.remit_bank_acct_use_id = P_REMITTANCE_BANK_ACCOUNT_ID  OR
2259 		(Batchinfo.remit_bank_acct_use_id is NULL AND P_REMITTANCE_BANK_ACCOUNT_ID is NULL))
2260             AND (Batchinfo.receipt_class_id           = P_RECEIPT_CLASS_ID   OR
2261 		(Batchinfo.receipt_class_id is NULL AND P_RECEIPT_CLASS_ID is NULL))
2262             AND (Batchinfo.attribute11                = P_ATTRIBUTE11        OR
2263 		(Batchinfo.attribute11 is NULL AND P_ATTRIBUTE11 is NULL))
2264             AND (Batchinfo.attribute12                = P_ATTRIBUTE12        OR
2265 		(Batchinfo.attribute12 is NULL AND P_ATTRIBUTE12 is NULL))
2266             AND (Batchinfo.attribute13                = P_ATTRIBUTE13        OR
2267 		(Batchinfo.attribute13 is NULL AND P_ATTRIBUTE13 is NULL))
2268             AND (Batchinfo.attribute14                = P_ATTRIBUTE14        OR
2269 		(Batchinfo.attribute14 is NULL AND P_ATTRIBUTE14 is NULL))
2270             AND (Batchinfo.attribute15                = P_ATTRIBUTE15        OR
2271 		(Batchinfo.attribute15 is NULL AND P_ATTRIBUTE15 is NULL))
2272             AND (Batchinfo.program_application_id     = P_PROGRAM_APPLICATION_ID OR
2273 		(Batchinfo.program_application_id is NULL AND P_PROGRAM_APPLICATION_ID is NULL))
2274             AND (Batchinfo.program_id                 = P_PROGRAM_ID         OR
2275 		(Batchinfo.program_id is NULL AND P_PROGRAM_ID is NULL))
2276             AND (Batchinfo.program_update_date        = P_PROGRAM_UPDATE_DATE OR
2277 		(Batchinfo.program_update_date is NULL AND P_PROGRAM_UPDATE_DATE is NULL))
2278             AND (Batchinfo.remittance_bank_branch_id  = P_REMITTANCE_BANK_BRANCH_ID OR
2279 		(Batchinfo.remittance_bank_branch_id is NULL AND P_REMITTANCE_BANK_BRANCH_ID is NULL))
2280             AND (Batchinfo.remit_method_code          = P_REMIT_METHOD_CODE   OR
2281 		(Batchinfo.remit_method_code is NULL AND P_REMIT_METHOD_CODE is NULL))
2282             AND (Batchinfo.request_id                 = P_REQUEST_ID          OR
2283 		(Batchinfo.request_id is NULL AND P_REQUEST_ID is NULL))
2284             AND (Batchinfo.set_of_books_id            = P_SET_OF_BOOKS_ID     OR
2285 		(Batchinfo.set_of_books_id is NULL AND P_SET_OF_BOOKS_ID is NULL))
2286             AND (Batchinfo.transmission_id            = P_TRANSMISSION_ID     OR
2287 		(Batchinfo.transmission_id is NULL AND P_TRANSMISSION_ID is NULL))
2288             AND (Batchinfo.bank_deposit_number        = P_BANK_DEPOSIT_NUMBER OR
2289 		(Batchinfo.bank_deposit_number is NULL AND P_BANK_DEPOSIT_NUMBER is NULL))
2290            )
2291         then
2292           return;
2293         else
2294           FND_MESSAGE.Set_Name('FND', 'FORM_RECORD_CHANGED');
2295           APP_EXCEPTION.Raise_Exception;
2296         end if;
2297   END Lock_Row;
2298 
2299 /* ----------------------------------------------------------------------
2300    Function receipt_debit_memo_reversed
2301 
2302    Parameters:  p_cash_receipt_id
2303 
2304    Return Value:  VARCHAR2(1)    'Y' if receipt was debit-memo-reversed
2305                                  'N' if receipt was not debit-memo-reversed
2306 
2307    This function was added for CE enhancement request 681187.  The function
2308    can be used in a view to determine if a given receipt was debit-memo-
2309    reversed.
2310    Note that the function will return 'N' if the receipt was reversed
2311    with normal (non-debit-memo) reversal.  It will also return 'N'
2312    if the passed in parameter is not a valid cash_receipt_id, i.e.,
2313    there is no error handling for this case.
2314 
2315    Modification History:
2316 
2317    08-JUL-98    Guat Eng Tan   created
2318    ---------------------------------------------------------------------- */
2319 
2320 
2321 FUNCTION receipt_debit_memo_reversed( p_cash_receipt_id IN NUMBER)
2322                            RETURN VARCHAR2 IS
2323   l_result  VARCHAR2(1);
2324 BEGIN
2325 
2326   BEGIN
2327     SELECT 'Y'
2328     INTO   l_result
2329     FROM   ar_payment_schedules ps_dm
2330     WHERE  ps_dm.reversed_cash_receipt_id = p_cash_receipt_id
2331       AND  ps_dm.class = 'DM';
2332 
2333     EXCEPTION
2334     WHEN NO_DATA_FOUND THEN
2335        l_result := 'N';
2336     WHEN OTHERS THEN
2337        RAISE;
2338   END;
2339 
2340   RETURN l_result;
2341 
2342 END;
2343 
2344 
2348 
2345 PROCEDURE update_actual_value_date(p_cash_receipt_id IN NUMBER,
2346 		p_actual_value_date IN DATE) IS
2347 BEGIN
2349   UPDATE AR_CASH_RECEIPTS
2350   SET actual_value_date = p_actual_value_date,
2351       rec_version_number =  nvl(rec_version_number,1)+1 /* bug 3372585 */
2352   WHERE cash_receipt_id = p_cash_receipt_id;
2353 
2354 END;
2355 
2356 FUNCTION revision RETURN VARCHAR2 IS
2357 BEGIN
2358 
2359   RETURN '$Revision: 120.16.12000000.4 $';
2360 
2361 END revision;
2362 
2363 END ARP_CASHBOOK;