DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_PROCESS_APPLICATION

Source


1 PACKAGE BODY ARP_PROCESS_APPLICATION AS
2 /* $Header: ARCEAPPB.pls 120.86.12010000.7 2008/11/17 12:51:02 npanchak ship $ */
3 
4 /* =======================================================================
5  | Global Data Types
6  * ======================================================================*/
7 SUBTYPE ae_doc_rec_type   IS arp_acct_main.ae_doc_rec_type;
8 
9 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('AFLOG_ENABLED'), 'N');
10 --
11 -- Private procedures used by the package
12 --
13 
14 NULL_VAR ar_payment_schedules%ROWTYPE; /* Added for Bug 460959 for Oracle 8 */
15 --
16 FUNCTION check_reversable (
17         p_ra_id  IN ar_receivable_applications.receivable_application_id%TYPE,
18         p_module_name    IN VARCHAR2,
19         p_module_version IN VARCHAR2 ) RETURN BOOLEAN;
20 --
21 PROCEDURE reverse_action(
22         p_ra_id IN ar_receivable_applications.receivable_application_id%TYPE,
23         p_cr_id IN ar_cash_receipts.cash_receipt_id%TYPE,
24         p_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
25         p_reversal_gl_date	IN DATE
26 	, p_reversal_date	IN DATE,
27         P_SELECT_FLAG		IN BOOLEAN,
28         P_MODULE_NAME		IN VARCHAR2,
29         P_MODULE_VERSION	IN VARCHAR2 );
30 --
31 PROCEDURE VALIDATE_ARGS(
32         P_RA_ID IN AR_RECEIVABLE_APPLICATIONS.RECEIVABLE_APPLICATION_ID%TYPE,
33         P_REVERSAL_GL_DATE      IN DATE,
34         P_REVERSAL_DATE         IN DATE,
35         P_MODULE_NAME           IN VARCHAR2 );
36 --
37 PROCEDURE reversal_insert_oppos_ra_recs (
38 	  p_ra_rec		IN OUT NOCOPY ar_receivable_applications%ROWTYPE
39         , p_app_rec_trx_type    IN VARCHAR
40         , p_reversal_gl_date	IN DATE
41 	, p_reversal_date	IN DATE
42 	, p_module_name		IN VARCHAR2
43  	, p_called_from         IN VARCHAR2 DEFAULT NULL
44         , p_rec_app_id          OUT NOCOPY NUMBER);  /* jrautiai BR implementation */
45 
46 PROCEDURE reversal_update_old_ra_rec(
47 	  p_reversal_gl_date	DATE
48         , p_ra_rec		IN OUT NOCOPY ar_receivable_applications%ROWTYPE
49 	);
50 
51 PROCEDURE reversal_update_ps_recs (
52 	  p_ra_rec		IN ar_receivable_applications%ROWTYPE
53         , p_app_rec_trx_type    IN VARCHAR
54 	, p_reversal_gl_date	IN DATE
55 	, p_reversal_date	IN DATE);
56 --
57 PROCEDURE  validate_reverse_action_args(
58         p_ra_id IN ar_receivable_applications.receivable_application_id%TYPE,
59         p_cr_id IN ar_cash_receipts.cash_receipt_id%TYPE,
60         p_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
61         p_reversal_gl_date IN DATE, p_reversal_date IN DATE,
62         p_select_flag IN BOOLEAN );
63 --
64 PROCEDURE validate_args_appdel(
65         p_ra_id  IN ar_receivable_applications.receivable_application_id%TYPE );
66 --
67 PROCEDURE  validate_receipt_appln_args(
68 	p_receipt_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
69 	p_invoice_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
70         p_amount_applied IN ar_receivable_applications.amount_applied%TYPE,
71         p_amount_applied_from IN ar_receivable_applications.amount_applied_from%TYPE,
72         p_trans_to_receipt_rate IN ar_receivable_applications.trans_to_receipt_rate%TYPE,
73         p_invoice_currency_code IN ar_payment_schedules.invoice_currency_code%TYPE,
74         p_receipt_currency_code IN ar_cash_receipts.currency_code%TYPE,
75         p_earned_discount_taken IN ar_receivable_applications.earned_discount_taken%TYPE,
76         p_unearned_discount_taken IN ar_receivable_applications.unearned_discount_taken%TYPE,
77         p_apply_date IN ar_receivable_applications.apply_date%TYPE,
78 	p_gl_date IN ar_receivable_applications.gl_date%TYPE );
79 --
80 PROCEDURE  validate_cm_appln_args(
81         p_cm_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
82         p_invoice_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
83         p_amount_applied IN
84                 ar_receivable_applications.amount_applied%TYPE,
85         p_apply_date IN ar_receivable_applications.apply_date%TYPE,
86         p_gl_date IN ar_receivable_applications.gl_date%TYPE );
87 --
88 PROCEDURE  validate_on_account_args(
89         p_ps_id   IN ar_payment_schedules.payment_schedule_id%TYPE,
90         p_amount_applied IN
91                 ar_receivable_applications.amount_applied%TYPE,
92         p_apply_date IN ar_receivable_applications.apply_date%TYPE,
93         p_gl_date IN ar_receivable_applications.gl_date%TYPE );
94 
95 PROCEDURE validate_activity_args(
96         p_ps_id                    IN ar_payment_schedules.payment_schedule_id%TYPE,
97         p_application_ps_id        IN ar_receivable_applications.applied_payment_schedule_id%TYPE,
98         p_link_to_customer_trx_id  IN ar_receivable_applications.link_to_customer_trx_id%TYPE,
99         p_amount_applied           IN ar_receivable_applications.amount_applied%TYPE,
100         p_apply_date               IN ar_receivable_applications.apply_date%TYPE,
101         p_gl_date                  IN ar_receivable_applications.gl_date%TYPE,
102         p_receivables_trx_id       IN ar_receivable_applications.receivables_trx_id%TYPE);
103 
104 PROCEDURE validate_activity(
105         p_application_ps_id  IN ar_receivable_applications.applied_payment_schedule_id%TYPE,
106         p_activity_type      IN ar_receivables_trx.type%TYPE);
107 
108 PROCEDURE reverse_action_receipt_cb(
109         p_chargeback_customer_trx_id  IN ar_receivable_applications.application_ref_id%TYPE,
110         p_reversal_gl_date            IN DATE,
111         p_reversal_date               IN DATE,
112         p_module_name                 IN VARCHAR2,
113         p_module_version              IN VARCHAR2);
114 
115 PROCEDURE insert_trx_note(
116               p_customer_trx_id             IN  NUMBER
117             , p_receipt_number              IN  VARCHAR2
118             , p_claim_number                IN  VARCHAR2
119             , p_flag                        IN  VARCHAR2);
120 
121 
122 FUNCTION unapp_postable(p_applied_customer_trx_id  IN ar_receivable_applications.applied_customer_trx_id%TYPE,
123                         p_applied_ps_id            IN ar_receivable_applications.applied_payment_schedule_id%TYPE) RETURN BOOLEAN;
124 
125 PROCEDURE reverse_action_misc_receipt(
126 	p_cash_receipt_id IN ar_receivable_applications.application_ref_id%TYPE,
127 	p_reversal_gl_date IN DATE,
128     p_reversal_date IN DATE,
129 	p_reversal_comments IN VARCHAR2 DEFAULT NULL,
130 	p_called_from IN VARCHAR2  DEFAULT NULL);
131 --
132 /*===========================================================================+
133  | FUNCTION                                                                  |
134  |    revision                                                               |
135  |                                                                           |
136  | DESCRIPTION                                                               |
137  |    This function returns the revision number of this package.             |
138  |                                                                           |
139  | SCOPE - PUBLIC                                                            |
140  |                                                                           |
141  | RETURNS    : Revision number of this package                              |
142  |                                                                           |
143  | MODIFICATION HISTORY                                                      |
144  |	6/25/1996	Harri Kaukovuo	Created                              |
145  +===========================================================================*/
146 FUNCTION revision RETURN VARCHAR2 IS
147 BEGIN
148   RETURN '$Revision: 120.86.12010000.7 $';
149 END revision;
150 --
151 /*===========================================================================+
152  | PROCEDURE                                                                 |
153  |    reverse                                                                |
154  |                                                                           |
155  | DESCRIPTION                                                               |
156  |    Reverse a cash receipt application                                     |
157  |                                                                           |
158  | SCOPE - PUBLIC                                                            |
159  |                                                                           |
160  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
161  |     arp_app_pkg.fetch_p - Fetch a record from                             |
162  |                                        AR_RECEIVABLE_APPLICATIONS table   |
163  |     arp_app_pkg.lock_p  - lock  a record in                               |
164  |                                        AR_RECEIVABLE_APPLICATIONS table   |
165  |                                                                           |
166  | ARGUMENTS  : IN:                                                          |
167  |                    p_ra_id - Id of application to be reversed             |
168  |                    p_reversal_gl_date - Reversal GL date                  |
169  |                    p_reversal_date - Reversal Date                        |
170  |                    p_module_name - Name of module that called this proc   |
171  |                    p_module_version - Version of the module that called   |
172  |                                       this procedure                      |
173  |              OUT:    p_bal_due_remaining                                  |
174  |                                                                           |
175  | RETURNS    : NONE                                                         |
176  |                                                                           |
177  | NOTES                                                                     |
178  |                                                                           |
179  | MODIFICATION HISTORY - Created by Ganesh Vaidee - 04/25/95                |
180  |
181  |  21-Jul-97   Karen Lawrance  Release 11.
182  |                              Cleaned up code and included some more
183  |                              comments.
184  |  09-Jun-99   Debbie Jancis   added balance due remaining to return
185  |                              balance due on invoice to update the
186  |                              the field on the form.
187  |  12-JUL-2000 Jani Rautiainen Added parameter p_called_from to procedure   |
188  |                              reverse. This is needed in the logic to      |
189  |                              be able to create the transaction history    |
190  |                              record for an Bills Receivable transaction   |
191  |                              when its payment schedule is opened / closed |
192  | 06/12/2001  S.Nambiar        Bug 1830483 - Activity application should not
193  |                              show error when there is no CB attached while
194  |                              doing the unapplication
195  +===========================================================================*/
196 PROCEDURE reverse (
197 	  p_ra_id		IN NUMBER
198 	, p_reversal_gl_date    IN DATE
199 	, p_reversal_date      	IN DATE
200 	, p_module_name		IN VARCHAR2
201 	, p_module_version	IN VARCHAR2
202         , p_bal_due_remaining   OUT NOCOPY NUMBER
203  	, p_called_from         IN VARCHAR2) IS /* jrautiai BR implementation */
204 
205 l_ra_rec			ar_receivable_applications%ROWTYPE;
206 l_return_code			VARCHAR2(20);
207 l_ps_rec			ar_payment_schedules%ROWTYPE;
208 ln_batch_id			NUMBER;
209 l_payment_schedule_id    ar_payment_schedules.payment_schedule_id%TYPE;
210 l_bal_due_rem            NUMBER;
211 
212    l_old_ps_rec               ar_payment_schedules%ROWTYPE; /* jrautiai BR implementation */
213 l_rec_app_id                  NUMBER;
214 --apandit Bug : 2641517
215 l_status                      VARCHAR2(30);
216 BEGIN
217     IF PG_DEBUG in ('Y', 'C') THEN
218        arp_standard.debug('arp_process_application.reverse()+' );
219     END IF;
220 
221     -- Validate input arguments
222     validate_args( p_ra_id,
223                    p_reversal_gl_date,
224                    p_reversal_date, p_module_name );
225 
226      -- Populate the ar_receivable_applications record from
227      -- ar_receivable_applications table.
228 
229      --Bug:4068781
230      BEGIN
231        arp_app_pkg.lock_p( p_ra_id );
232      EXCEPTION
233        WHEN OTHERS
234          THEN
235            FND_MESSAGE.SET_NAME( 'FND', 'FND_LOCK_RECORD_ERROR');
236            FND_MSG_PUB.Add;
237            APP_EXCEPTION.raise_exception;
238      END;
239 
240      arp_app_pkg.fetch_p( p_ra_id, l_ra_rec );
241     --apandit Bug : 2641517
242      l_status := l_ra_rec.status;
243 
244      -- get the payment schedule id of the trx that receipt was
245      -- applied to.
246      l_payment_schedule_id := l_ra_rec.applied_payment_schedule_id;
247 
248     /* 12-JUL-2000 J Rautiainen BR Implementation
249      * Storing the old image of the payment schedule
250      * This is only done when application is done outside the BR remittance program */
251 
252     --Added ARXRWAPP also in the list for receipt write-off
253     --No need to fetch PS record for receipt write-off
254 
255 
256      IF NVL(p_called_from,'NONE') not in ('RISK_UNELIMINATED','BR_REMITTED',
257          'BR_FACTORED_WITH_RECOURSE','BR_FACTORED_WITHOUT_RECOURSE','ARXRWAPP')
258         AND l_ra_rec.status = 'APP' THEN
259 
260        arp_ps_pkg.fetch_p( l_payment_schedule_id, l_old_ps_rec );
261 
262      END IF;
263 
264      --
265      -- If status is 'APP', check if application may be deleted
266      --
267      /* 06-JUL-2000 J Rautiainen BR Implementation
268       * Activity application impacts the receipt payment schedule */
269      IF ( l_ra_rec.status in ('APP','ACTIVITY') AND l_ra_rec.display = 'Y' ) THEN
270          --
271          -- Reverse chargebacks and adjustments associated with the
272          -- application.
273          --
274         /* 06-JUL-2000 J Rautiainen BR Implementation
275          * No chargebacks or adjustments exist on Activity application
276          snambiar - except for receipt chargebacks                   */
277 
278          IF l_ra_rec.status = 'APP' THEN
279            IF ( check_reversable( p_ra_id, NULL, NULL ) = TRUE ) THEN
280 	        reverse_action( p_ra_id, NULL, NULL,
281                                 p_reversal_gl_date, p_reversal_date,
282                                 TRUE, NULL, NULL );
283 
284            ELSE
285                fnd_message.set_name( 'AR', 'AR_CBK_FAIL');
286                APP_EXCEPTION.raise_exception;
287            END IF;
288 
289        --snambiar for receipt chargeback,we need to reverse the chargeback also
290        --associated with the activity application.
291            ELSIF (l_ra_rec.status = 'ACTIVITY') THEN
292             IF(l_ra_rec.applied_payment_schedule_id = -5) THEN
293               IF (check_reversable( p_ra_id, NULL, NULL ) = TRUE ) THEN
294 
295                  reverse_action_receipt_cb(l_ra_rec.application_ref_id,
296                                           p_reversal_gl_date,
297                                           p_reversal_date,
298                                           NULL,
299                                           NULL );
300               ELSE
301                  fnd_message.set_name( 'AR', 'AR_CBK_FAIL');
302                  APP_EXCEPTION.raise_exception;
303               END IF;
304 
305             ELSIF (l_ra_rec.applied_payment_schedule_id in (-6 , -9 )) THEN --Bug 5532825 Added -9 for CCCB
306 
307 			   IF (NVL(P_called_from,'NONE')
308 			        not in ('REVERSE_MISC','RATE_ADJUST_MISC')) THEN
309 
310 			      IF (check_reversable( p_ra_id, NULL, NULL ) = TRUE) THEN
311                      reverse_action_misc_receipt(
312 	                      p_cash_receipt_id=>l_ra_rec.application_ref_id,
313 	                      p_reversal_gl_date=>p_reversal_gl_date,
314                           p_reversal_date=>p_reversal_gl_date,
315 	                      p_reversal_comments=>l_ra_rec.comments,
316 						  p_called_from=>p_called_from);
317                   ELSE
318                      fnd_message.set_name( 'AR', 'AR_RW_CCR_REMITTED');
319                      APP_EXCEPTION.raise_exception;
320 			      END IF;
321 			   END IF;
322 
323             END IF; --l_ra_rec.applied_payment_schedule_id
324 
325          END IF; --l_ra_rec.status
326 
327          -----------------------------------------------------------
328          -- If the status of the row is APP, we need to reverse the
329          -- application by updating the corresponding rows in
330          -- payment schedules.  This includes the payment schedule
331          -- row for both the Cash Receipt and applied Transaction.
332          --
333          -----------------------------------------------------------
334          -----------------------------------------------------------
335          -- Bug 2004654
336          -- If the status of the row is APP or ACIVITY, we need to reverse the
337          -- application by updating the corresponding rows in
338          -- payment schedules.  This includes the payment schedule
339          -- row for both the Cash Receipt and applied Transaction.
340          --
341          -----------------------------------------------------------
342 
343         IF ( NVL( l_ra_rec.confirmed_flag, 'Y' ) = 'Y'
344                 AND l_ra_rec.status in ('APP', 'ACTIVITY')) THEN
345 
346 	         reversal_update_ps_recs ( l_ra_rec,
347                                            'AR_APP',
348 	       				   p_reversal_gl_date ,
349 					   p_reversal_date);
350         END IF;
351 
352          -- bug 584303:  try to get the amount due remaining
353          -- on the applied trx so it can be returned to the
354          -- form.
355 
356         /* 06-JUL-2000 J Rautiainen BR Implementation
357          * Transaction payment schedule was not updated for activity application  */
358 
359          IF (l_payment_schedule_id IS NOT NULL and l_ra_rec.status <> 'ACTIVITY') THEN
360               SELECT amount_due_remaining
361                    INTO l_bal_due_rem
362                 from ar_payment_schedules
363               where payment_schedule_id = l_payment_schedule_id;
364          END IF;
365 
366          if (l_bal_due_rem IS NOT NULL) THEN
367              p_bal_due_remaining := l_bal_due_rem;
368          end if;
369      END IF;
370 
371      ----------------------------------------------------------
372      -- Update the current ar_receivable_applications record.
373      -- Set reversal_gl_date and display_flag to 'N'.
374      --
375      ----------------------------------------------------------
376      reversal_update_old_ra_rec( p_reversal_gl_date, l_ra_rec );
377 
378      /* Bug fix 2877224
379         Update the UNAPP record which is paired with the APP record being reversed.
380         The reversal_gl_date needs to be populated */
381        update ar_receivable_applications
382        set  reversal_gl_date = p_reversal_gl_date
383        where receivable_application_id = (select source_id
384                                            from ar_distributions
385                                            where source_table = 'RA'
386                                             and source_type = 'UNAPP'
387                                             and source_id_secondary = l_ra_rec.receivable_application_id);
388 
389      /* Bug fix 3000242
390         For upgraded data, source_id secondary will not be populated.
391         So if the above update can not update the paired UNAPP record, the following update
392         which uses the maximum matching criteria should be run to update the paired UNAPP record */
393         IF SQL%NOTFOUND THEN
394           IF PG_DEBUG in ('Y', 'C') THEN
395              arp_standard.debug('reverse: ' ||  'trans_to_receipt_rate = '||to_char(l_ra_rec.trans_to_receipt_rate));
396              arp_standard.debug('reverse: ' ||  'cash_receipt_id = '||to_char(l_ra_rec.cash_receipt_id));
397              arp_standard.debug('reverse: ' ||  'posting_control_id = '||to_char(l_ra_rec.posting_control_id));
398              arp_standard.debug('reverse: ' ||  'gl_posted_date = '||to_char(l_ra_rec.gl_posted_date,'DD-MON-YYYY'));
399              arp_standard.debug('reverse: ' ||  'amount_applied = '||to_char(l_ra_rec.amount_applied));
400              arp_standard.debug('reverse: ' ||  'amount_applied_from = '||to_char(l_ra_rec.amount_applied_from));
401              arp_standard.debug('reverse: ' ||  'gl_date = '||to_char(l_ra_rec.gl_date,'DD-MON-YYYY'));
402              arp_standard.debug('reverse: ' ||  'apply_date = '||to_char(l_ra_rec.apply_date,'DD-MON-YYYY'));
403           END IF;
404           IF l_ra_rec.trans_to_receipt_rate is NOT NULL THEN
405             update ar_receivable_applications
406                set reversal_gl_date = p_reversal_gl_date
407              where receivable_application_id = (select max(receivable_application_id)
408                                                   from ar_receivable_applications
409                                                  where cash_receipt_id = l_ra_rec.cash_receipt_id
410                                                    and status ='UNAPP'
411                                                    and posting_control_id = l_ra_rec.posting_control_id
412                                                    and nvl(gl_posted_date,sysdate) = nvl(l_ra_rec.gl_posted_date, sysdate)
413      -------  bug fix 3000242+  ----------
414                                                    and cash_receipt_history_id = l_ra_rec.cash_receipt_history_id
415                                                    and (request_id = l_ra_rec.request_id
416                                                         or
417                                                         (request_id IS NULL and l_ra_rec.request_id IS NULL))
418    -------  bug fix 3000242-  ----------
419                                                    and amount_applied_from = -l_ra_rec.amount_applied_from
420                                                    and gl_date             = l_ra_rec.gl_date
421                                                    and apply_date = l_ra_rec.apply_date
422                                                    and reversal_gl_date is NULL);
423           ELSE
424             update ar_receivable_applications
425                set reversal_gl_date = p_reversal_gl_date
426              where receivable_application_id = (select max(receivable_application_id)
427                                                   from ar_receivable_applications
428                                                  where cash_receipt_id = l_ra_rec.cash_receipt_id
429                                                    and status ='UNAPP'
430                                                    and posting_control_id = l_ra_rec.posting_control_id
431                                                    and nvl(gl_posted_date,sysdate) = nvl(l_ra_rec.gl_posted_date, sysdate)
432    -------  bug fix 3000242+  ----------
433                                                    and cash_receipt_history_id = l_ra_rec.cash_receipt_history_id
434                                                    and (request_id = l_ra_rec.request_id
435                                                         or
436                                                         (request_id IS NULL and l_ra_rec.request_id IS NULL))
437   -------  bug fix 3000242-  ----------
438                                                    and amount_applied = -l_ra_rec.amount_applied
439                                                    and gl_date        = l_ra_rec.gl_date
440                                                    and apply_date     = l_ra_rec.apply_date
441                                                    and reversal_gl_date is NULL);
442          END IF;
443        END IF;
444 
445      ----------------------------------------------------
446      -- Insert opposing rows in receivable applications.
447      --
448      ----------------------------------------------------
449 
450      reversal_insert_oppos_ra_recs(
451 		  l_ra_rec
452                 , 'AR_APP'
453 		, p_reversal_gl_date
454 		, p_reversal_date
455 		, p_module_name
456                 , p_called_from
457                 , l_rec_app_id);
458 
459 
460     -----------------------------------
461     -- Update batch status if needed.
462     -----------------------------------
463     SELECT
464       crh.batch_id
465     INTO
466       ln_batch_id
467     FROM
468 	  ar_cash_receipt_history	crh
469         , ar_receivable_applications	ra
470     WHERE
471 	ra.receivable_application_id	= p_ra_id
472     AND	ra.cash_receipt_id		= crh.cash_receipt_id
473     AND crh.current_record_flag		= 'Y';
474 
475     IF (ln_batch_id IS NOT NULL)
476     THEN
477       arp_rw_batches_check_pkg.update_batch_status(ln_batch_id);
478     END IF;
479 
480    /*---------------------------------------------------------------------------------+
481     |  12-JUL-2000 J Rautiainen BR Implementation                                     |
482     |  If Bills receivable PS is closed or opened we need to create the corresponding |
483     |  transaction history record. This logic is only for normal receipt applications |
484     |  outside the BR remittance program, since for BR programs the record will be    |
485     |  created by the BR API.                                                         |
486     +---------------------------------------------------------------------------------*/
487 
488     IF NVL(l_old_ps_rec.class,'INV') = 'BR'
489        AND NVL(p_called_from,'NONE') not in ('RISK_UNELIMINATED','BR_REMITTED','BR_FACTORED_WITH_RECOURSE','BR_FACTORED_WITHOUT_RECOURSE') THEN
490 
491 
492      /*------------------------------------+
493       |  Create transaction history record |
494       +------------------------------------*/
495 
496       ARP_PROC_TRANSACTION_HISTORY.create_trh_for_receipt_act(l_old_ps_rec,
497                                                               l_ra_rec,
498                                                               'ARCEAPPB');
499 
500 
501     END IF;
502 
503      --apandit
504      --Bug : 2641517 raise UnApply business event.
505      IF l_status in ('APP','ACTIVITY')  THEN
506       AR_BUS_EVENT_COVER.Raise_CR_UnApply_Event(l_rec_app_id);
507      END IF;
508 
509     -- RAM-C changes begin
510     --
511     -- call revenue management's receipt analyzer for revenue related
512     -- impact of this reversal.
513 
514     IF PG_DEBUG in ('Y', 'C') THEN
515        arp_standard.debug('reverse: ' ||  'calling receipt_analyzer in reversal mode');
516     END IF;
517 
518     ar_revenue_management_pvt.receipt_analyzer (
519       p_mode => ar_revenue_management_pvt.c_receipt_reversal_mode,
520       p_receivable_application_id => p_ra_id);
521 
522     IF PG_DEBUG in ('Y', 'C') THEN
523        arp_standard.debug('returned from receipt_analyzer');
524     END IF;
525 
526     -- RAM-C changes end
527 
528     IF PG_DEBUG in ('Y', 'C') THEN
529        arp_standard.debug('arp_process_application.reverse()-' );
530     END IF;
531 EXCEPTION
532     WHEN OTHERS THEN
533          IF PG_DEBUG in ('Y', 'C') THEN
534             arp_standard.debug('reverse: ' ||
535  	 'EXCEPTION: arp_process_application.reverse' );
536          END IF;
537          RAISE;
538 END reverse;
539 --
540 /*===========================================================================+
541  | PROCEDURE                                                                 |
542  |    reverse_cm_app                                                         |
543  |                                                                           |
544  | DESCRIPTION                                                               |
545  |    Reverse a credit memo application.                                     |
546  |	The algorithm for reversing an cm application is                     |
547 		1. Reverse existing application using opposite               |
548 		   amounts. This is done by creating a new row into          |
549 		   AR_RECEIVABLE_APPLICATIONS table.                         |
550 		2. Update applied transaction row in AR_PAYMENT_SCHEDULES    |
551  |                                                                           |
552  | SCOPE - PUBLIC                                                            |
553  |                                                                           |
554  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
555  |     arp_app_pkg.fetch_p - Fetch a record from
556  |                           AR_RECEIVABLE_APPLICATIONS table
557  |                                                                           |
558  | ARGUMENTS  : IN:                                                          |
559  |              pn_ra_id		Id of application to be reversed
560  |              pd_reversal_gl_date	Reversal GL date
561  |              pd_reversal_date	Reversal Date
562  |              pc_module_name		Name of module that called this proc.
563  |              pc_module_version	Version of the module that called
564  |                                      this procedure
565  |              OUT:                                                         |
566  |                                                                           |
567  | RETURNS    : NONE                                                         |
568  |                                                                           |
569  | NOTES                                                                     |
570  |                                                                           |
571  | MODIFICATION HISTORY
572  | 5/30/1996	Harri Kaukovuo		Created
573  |                                                                           |
574  +===========================================================================*/
575 PROCEDURE reverse_cm_app(
576           pn_ra_id               IN NUMBER
577 	, pn_applied_ps_id	 IN NUMBER
578         , pd_reversal_gl_date    IN DATE
579         , pd_reversal_date       IN DATE
580         , pc_module_name         IN VARCHAR2
581         , pc_module_version      IN VARCHAR2
582         , p_called_from          IN VARCHAR2 ) IS
583 
584 lr_ra_rec		ar_receivable_applications%ROWTYPE;
585 l_rec_app_id            NUMBER;
586 l_trx_type		VARCHAR2(20);
587 
588 -- added for unapplication of regular CM
589 l_rule_id               NUMBER;
590 l_reg_cm                BOOLEAN;
591 l_rev_rec_run           VARCHAR2(1);
592 l_sum_dist              NUMBER;
593 
594 BEGIN
595   IF PG_DEBUG in ('Y', 'C') THEN
596      arp_standard.debug( 'arp_process_application.reverse_cm_app()+' );
597   END IF;
598 
599   -- -------------------------------------------------------------------
600   -- Get ready to insert application row into ar_receivable_applications.
601   -- We use table handler to insert the record.
602   -- -------------------------------------------------------------------
603 
604   --   Populate the ar_receivable_applications record from
605   --   ar_receivable_applications table. Use ar_receivable_application_id
606   --   for selection.
607 
608   --Bug:4068781
609   BEGIN
610     arp_app_pkg.lock_p( pn_ra_id );
611   EXCEPTION
612     WHEN OTHERS
613       THEN
614         FND_MESSAGE.SET_NAME( 'FND', 'FND_LOCK_RECORD_ERROR');
615         FND_MSG_PUB.Add;
616         APP_EXCEPTION.raise_exception;
617   END;
618 
619   arp_app_pkg.fetch_p( pn_ra_id, lr_ra_rec );
620 
621   -- need some information to check if we have a regular cm or
622   -- on acct credit memo also if we have invoice with rules.
623   -- to do some processing before the application is reversed.
624   l_reg_cm :=  arp_process_application.is_regular_cm(
625                   p_customer_trx_id => lr_ra_rec.customer_trx_id,
626                   p_invoicing_rule_id => l_rule_id);
627 
628   IF (l_reg_cm) THEN
629     IF (l_rule_id <> -999) THEN
630        -- make sure rev rec is run, if not, run rev rec
631        IF (PG_DEBUG in ('Y', 'C'))  THEN
632           arp_standard.debug('reverse_cm_app: we have a reg cm with rules');
633        END IF;
634 
635        -- has rev rec been run for this CM?
636        l_rev_rec_run := arpt_sql_func_util.get_revenue_recog_run_flag(
637                              p_customer_Trx_id   => lr_ra_rec.customer_trx_id,
638                              p_invoicing_rule_id => l_rule_id);
639 
640        IF (l_rev_rec_run = 'N') THEN
641           -- we need to run rev rec - to be safe, we will run it for
642           -- the invoice as well as the CM.
643           l_sum_dist := ARP_AUTO_RULE.create_distributions
644                        ( p_commit => 'N',
645                          p_debug  => 'N',
646                          p_trx_id => lr_ra_rec.applied_customer_trx_id);
647 
648           l_sum_dist := ARP_AUTO_RULE.create_distributions
649                        ( p_commit => 'N',
650                          p_debug  => 'N',
651                          p_trx_id => lr_ra_rec.customer_trx_id);
652        END IF;
653 
654     END IF;
655 
656     arp_process_application.Unassociate_Regular_CM(
657                  p_cust_Trx_id      => lr_ra_rec.customer_trx_id,
658                  p_app_cust_trx_id  => lr_ra_rec.applied_customer_trx_id);
659 
660   END IF;
661 
662   -- If status of ar_receivable_applications record is 'APP', then
663   -- reverse the application by updating ar_payment_schedule of the
664   -- invoice, also set actual date closed and gl_date_closed.
665 
666   IF ( NVL( lr_ra_rec.confirmed_flag, 'Y' ) = 'Y' ) THEN
667      IF PG_DEBUG in ('Y', 'C') THEN
668         arp_standard.debug('reverse_cm_app: ' ||  'lr_ra_rec.confirmed_flag = Y' );
669      END IF;
670 
671      /* Bug 4122494 CM refunds */
672      IF pn_applied_ps_id = -8 THEN
673         l_trx_type := 'AR_CM_REF';
674      ELSE
675         l_trx_type := 'AR_CM';
676      END IF;
677 
678      reversal_update_ps_recs(
679 	  lr_ra_rec
680         , l_trx_type
681 	, pd_reversal_gl_date
682         , pd_reversal_date );
683   END IF;
684 
685   -- Update the current ar_receivable_applications record and set
686   -- reversal_gl_date and display_flag to 'N'.
687 
688   reversal_update_old_ra_rec( pd_reversal_gl_date, lr_ra_rec );
689 
690   -- Insert opposing application in ar_receivable_applications.
691   -- NOTE: We are passing module name ARREREVB to simulate same effect
692   -- as reversing the receipt would cause. We are not doing the same kind
693   -- of reversing as normal application reverse is doing.
694 
695   -- Normal application:
696   -- 	10 	APP	(this is the original applied record)
697   --	-10 	APP	<--   this is created to reverse applied row
698   --	10 	UNAPP	<--   this is created to mark reversed amount
699   --			      to be unapplied.
700 
701   -- Using 'ARREREVB' (reversing credit memo application):
702   --	10	APP	(this is the original applied record, against CM)
703   --	-10 	APP	<--   this is created to reverse applied row
704 
705   reversal_insert_oppos_ra_recs(
706                   lr_ra_rec
707                 , 'AR_CM'
708                 , pd_reversal_gl_date
709                 , pd_reversal_date
710                 , 'ARREREVB'
711                 , null
712                 , l_rec_app_id);
713 
714   --apandit
715   --Bug : 2641517 raise the business event
716   IF lr_ra_rec.status = 'APP' THEN
717      AR_BUS_EVENT_COVER.Raise_CM_UnApply_Event(l_rec_app_id);
718   END IF;
719 
720   IF PG_DEBUG in ('Y', 'C') THEN
721      arp_standard.debug( 'arp_process_application.reverse_cm_app()-' );
722   END IF;
723 
724 END reverse_cm_app;
725 --
726 /*===========================================================================+
727    PROCEDURE
728      update_selected_transaction
729 
730    DESCRIPTION
731  	This procedure is used to update the applied amount
732  	of an application. Usually receivable_applications row
733  	is not updated directly, but this is the case when
734  	we handle confirmed receipt applications that are not
735  	actually officially applied.
736 
737    SCOPE - PUBLIC
738 
739    EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE
740 
741    ARGUMENTS  : IN:
742                 pn_ra_id                Id of application to be reversed
743                                        this procedure
744 		pn_amount_applied	Amount which is going to be
745 					updated on application row.
746 	        pc_invoice_currency_code Invoice currency code
747        	 	pn_invoice_exchange_rate Invoice exchange rate
748         	pc_receipt_currency_code Receipt currency code
749         	pn_receipt_exchange_rate Receipt exchange rate
750 		pb_fetch_from_db_flag	Indicator which tells procedure
751 					to get invoice and receipt info
752 					from database instead of using
753 					passed parameters.
754 		pc_module_name		Name of the calling module
755 		pc_module_version	Version number of the module
756 		attribute_category
757 		attribute1
758                 attribute2
759                 attribute3
760                 attribute4
761                 attribute5
762                 attribute6
763                 attribute7
764                 attribute8
765                 attribute9
766                 attribute10
767                 attribute11
768                 attribute12
769                 attribute13
770                 attribute14
771                 attribute15
772 		global_attribute_category
773 		global_attribute1
774 		global_attribute2
775                 global_attribute3
776                 global_attribute4
777                 global_attribute5
778                 global_attribute6
779                 global_attribute7
780                 global_attribute8
781                 global_attribute9
782                 global_attribute10
783                 global_attribute11
784                 global_attribute12
785                 global_attribute13
786                 global_attribute14
787                 global_attribute15
788                 global_attribute16
789                 global_attribute17
790                 global_attribute18
791                 global_attribute19
792                 global_attribute20
793 
794                 OUT:
795 
796   RETURNS    : NONE
797 
798   NOTES
799  	I made this procedure to be ready for Rel 11 cross currency
800  	feature (i.e included both invoice and receipt currency
801  	information).
802 
803  	We don't use module and version information yet (not necessary
804  	but might be used for customizations).
805 
806    MODIFICATION HISTORY
807    6/18/1996    Harri Kaukovuo  Created
808    9/02/1997    Tasman Tang	Added global_attribute_category and
809  				global_attribute[1-20] for global
810  				descriptive flexfield
811  				Fixed bug 546626: check if functional
812  				currency is the same as invoice and receipt
813  				currency and do the corresponding parameter
814 				checking because rate can be null if receipt
815  				currency equals to functional currency
816    10/22/1997	Karen Murphy	Bug fix #567872.  Added code to update the
817 				UNAPP row when chaning the amount applied
818 				of one of the APP rows.
819    12/04/1997   Karen Murphy    Bug fix #567872.  Added the setting of the
820                                 acctd_amount_applied_from for the UNAPP row.
821    12/05/1997   Karen Murphy    Bug 546626.  Call to ARPCURR.functional_amount
822                                 needs to pass the functional currency not
823                                 the invoice and receipt currencies.  This
824                                 causes incorrect rounding.
825    09/10/2002   Debbie Jancis   Modified for MRC trigger replacement.  Added
826 				call to ar_mrc_engine3 to process receivable
827 				applications.
828  +===========================================================================*/
829 PROCEDURE update_selected_transaction(
830         pn_ra_id                      IN NUMBER,
831         pn_amount_applied             IN NUMBER,
832         pc_invoice_currency_code      IN VARCHAR2,
833         pn_invoice_exchange_rate      IN NUMBER,
834         pc_receipt_currency_code      IN VARCHAR2,
835         pn_receipt_exchange_rate      IN NUMBER,
836         pc_module_name                IN VARCHAR2,
837         pc_module_version             IN VARCHAR2,
838         p_attribute_category    IN VARCHAR2,
839         p_attribute1            IN VARCHAR2,
840         p_attribute2            IN VARCHAR2,
841         p_attribute3            IN VARCHAR2,
842         p_attribute4            IN VARCHAR2,
843         p_attribute5            IN VARCHAR2,
844         p_attribute6            IN VARCHAR2,
845         p_attribute7            IN VARCHAR2,
846         p_attribute8            IN VARCHAR2,
847         p_attribute9            IN VARCHAR2,
848         p_attribute10           IN VARCHAR2,
849         p_attribute11           IN VARCHAR2,
850         p_attribute12           IN VARCHAR2,
851         p_attribute13           IN VARCHAR2,
852         p_attribute14           IN VARCHAR2,
853         p_attribute15           IN VARCHAR2,
854         p_global_attribute_category IN VARCHAR2,
855         p_global_attribute1 IN VARCHAR2,
856         p_global_attribute2 IN VARCHAR2,
857         p_global_attribute3 IN VARCHAR2,
858         p_global_attribute4 IN VARCHAR2,
859         p_global_attribute5 IN VARCHAR2,
860         p_global_attribute6 IN VARCHAR2,
861         p_global_attribute7 IN VARCHAR2,
862         p_global_attribute8 IN VARCHAR2,
863         p_global_attribute9 IN VARCHAR2,
864         p_global_attribute10 IN VARCHAR2,
865         p_global_attribute11 IN VARCHAR2,
866         p_global_attribute12 IN VARCHAR2,
867         p_global_attribute13 IN VARCHAR2,
868         p_global_attribute14 IN VARCHAR2,
869         p_global_attribute15 IN VARCHAR2,
870         p_global_attribute16 IN VARCHAR2,
871         p_global_attribute17 IN VARCHAR2,
872         p_global_attribute18 IN VARCHAR2,
873         p_global_attribute19 IN VARCHAR2,
874         p_global_attribute20 IN VARCHAR2 ) IS
875 
876 lr_ra_rec               ar_receivable_applications%ROWTYPE;
877 functional_curr		VARCHAR2(100);
878 
879 ln_amount_change	NUMBER;
880 ln_cash_receipt_id	NUMBER;
881 ln_unapp_ra_id		NUMBER;
882 
883 l_app_ra_rec            ar_receivable_applications%ROWTYPE;
884 
885 BEGIN
886   IF PG_DEBUG in ('Y', 'C') THEN
887      arp_standard.debug( 'arp_process_application.update_selected_transaction()+');
888   END IF;
889 
890   functional_curr := arp_global.functional_currency;
891 
892   -- First check that all required arguments have some value
893   IF (pn_ra_id IS NULL)
894   THEN
895     APP_EXCEPTION.INVALID_ARGUMENT(
896           'ARP_PROCESS_APPLICATION.UPDATE_SELECTED_TRANSACTION'
897         , 'PN_RA_ID'
898         , 'NULL');
899   ELSIF (pn_amount_applied IS NULL)
900   THEN
901     APP_EXCEPTION.INVALID_ARGUMENT(
902           'ARP_PROCESS_APPLICATION.UPDATE_SELECTED_TRANSACTION'
903         , 'pn_amount_applied'
904         , 'NULL');
905   END IF;
906 
907   IF (pc_invoice_currency_code IS NULL) THEN
908     APP_EXCEPTION.INVALID_ARGUMENT(
909           'ARP_PROCESS_APPLICATION.UPDATE_SELECTED_TRANSACTION'
910         , 'pc_invoice_currency_code'
911         , 'NULL');
912   ELSIF (pc_invoice_currency_code <> functional_curr) AND
913         (pn_invoice_exchange_rate IS NULL) THEN
914     APP_EXCEPTION.INVALID_ARGUMENT(
915           'ARP_PROCESS_APPLICATION.UPDATE_SELECTED_TRANSACTION'
916         , 'pn_invoice_exchange_rate'
917         , 'NULL');
918   END IF;
919 
920   IF (pc_receipt_currency_code IS NULL) THEN
921     APP_EXCEPTION.INVALID_ARGUMENT(
922           'ARP_PROCESS_APPLICATION.UPDATE_SELECTED_TRANSACTION'
923         , 'pc_receipt_currency_code'
924         , 'NULL');
925   ELSIF (pc_receipt_currency_code <> functional_curr) AND
926 	(pn_receipt_exchange_rate IS NULL) THEN
927     APP_EXCEPTION.INVALID_ARGUMENT(
928           'ARP_PROCESS_APPLICATION.UPDATE_SELECTED_TRANSACTION'
929         , 'pn_receipt_exchange_rate'
930         , 'NULL');
931   END IF;
932 
933   --   Populate the ar_receivable_applications record from
934   --   ar_receivable_applications table. Use ar_receivable_application_id
935   --   for selection.
936 
937   --Bug:4068781
938   BEGIN
939     arp_app_pkg.lock_p( pn_ra_id );
940   EXCEPTION
941     WHEN OTHERS
942       THEN
943         FND_MESSAGE.SET_NAME( 'FND', 'FND_LOCK_RECORD_ERROR');
944         FND_MSG_PUB.Add;
945         APP_EXCEPTION.raise_exception;
946   END;
947   arp_app_pkg.fetch_p( pn_ra_id, lr_ra_rec );
948 
949   -- Before we update the APP row, we need to calculate the change in amount
950   -- so we can update the UNAPP row for the cash receipt.
951   IF lr_ra_rec.amount_applied <> pn_amount_applied THEN
952      ln_amount_change := lr_ra_rec.amount_applied - pn_amount_applied;
953      ln_cash_receipt_id := lr_ra_rec.cash_receipt_id;
954   END IF;
955 
956   -- Set the amount and calculate accounted amount (in base currency)
957   lr_ra_rec.amount_applied 		:= pn_amount_applied;
958 
959   -- This is functional amount for the transaction (invoice)
960   IF (pc_invoice_currency_code = functional_curr) THEN
961     lr_ra_rec.acctd_amount_applied_to := pn_amount_applied;
962   ELSE
963     lr_ra_rec.acctd_amount_applied_to :=
964   	ARPCURR.functional_amount(
965 		  amount	=> pn_amount_applied
966                 , currency_code	=> functional_curr
967                 , exchange_rate	=> pn_invoice_exchange_rate
968                 , precision	=> NULL
969 		, min_acc_unit	=> NULL );
970   END IF;
971 
972   -- This is functional amount for the receipt
973   IF (pc_receipt_currency_code = functional_curr) THEN
974     lr_ra_rec.acctd_amount_applied_from   := pn_amount_applied;
975   ELSE
976     lr_ra_rec.acctd_amount_applied_from 	:=
977   	ARPCURR.functional_amount(
978                   amount        => pn_amount_applied
979                 , currency_code => functional_curr
980                 , exchange_rate => pn_receipt_exchange_rate
981                 , precision     => NULL
982                 , min_acc_unit  => NULL );
983   END IF;
984 
985   lr_ra_rec.attribute_category := p_attribute_category;
986   lr_ra_rec.attribute1   := p_attribute1;
987   lr_ra_rec.attribute2   := p_attribute2;
988   lr_ra_rec.attribute3   := p_attribute3;
989   lr_ra_rec.attribute4   := p_attribute4;
990   lr_ra_rec.attribute5   := p_attribute5;
991   lr_ra_rec.attribute6   := p_attribute6;
992   lr_ra_rec.attribute7   := p_attribute7;
993   lr_ra_rec.attribute8   := p_attribute8;
994   lr_ra_rec.attribute9   := p_attribute9;
995   lr_ra_rec.attribute10  := p_attribute10;
996   lr_ra_rec.attribute11  := p_attribute11;
997   lr_ra_rec.attribute12  := p_attribute12;
998   lr_ra_rec.attribute13  := p_attribute13;
999   lr_ra_rec.attribute14  := p_attribute14;
1000   lr_ra_rec.attribute15  := p_attribute15;
1001 
1002   -- For global descriptive flexfield
1003   lr_ra_rec.global_attribute_category := p_global_attribute_category;
1004   lr_ra_rec.global_attribute1   := p_global_attribute1;
1005   lr_ra_rec.global_attribute2	:= p_global_attribute2;
1006   lr_ra_rec.global_attribute3   := p_global_attribute3;
1007   lr_ra_rec.global_attribute4   := p_global_attribute4;
1008   lr_ra_rec.global_attribute5   := p_global_attribute5;
1009   lr_ra_rec.global_attribute6   := p_global_attribute6;
1010   lr_ra_rec.global_attribute7   := p_global_attribute7;
1011   lr_ra_rec.global_attribute8   := p_global_attribute8;
1012   lr_ra_rec.global_attribute9   := p_global_attribute9;
1013   lr_ra_rec.global_attribute10   := p_global_attribute10;
1014   lr_ra_rec.global_attribute11   := p_global_attribute11;
1015   lr_ra_rec.global_attribute12   := p_global_attribute12;
1016   lr_ra_rec.global_attribute13   := p_global_attribute13;
1017   lr_ra_rec.global_attribute14   := p_global_attribute14;
1018   lr_ra_rec.global_attribute15   := p_global_attribute15;
1019   lr_ra_rec.global_attribute16   := p_global_attribute16;
1020   lr_ra_rec.global_attribute17   := p_global_attribute17;
1021   lr_ra_rec.global_attribute18   := p_global_attribute18;
1022   lr_ra_rec.global_attribute19   := p_global_attribute19;
1023   lr_ra_rec.global_attribute20   := p_global_attribute20;
1024 
1025   arp_app_pkg.update_p(lr_ra_rec);
1026 
1027   l_app_ra_rec := lr_ra_rec;
1028   --------------------------------------------------------------------
1029   -- Now that we have updated the APP row we need to update the UNAPP
1030   -- row for the cash receipt.
1031   --------------------------------------------------------------------
1032   IF ln_amount_change is not null THEN
1033 
1034      -- Get the receivable application id for the UNAPP row.
1035      select ra.receivable_application_id
1036      into   ln_unapp_ra_id
1037      from   ar_receivable_applications ra
1038      where  ra.cash_receipt_id = ln_cash_receipt_id
1039      and    ra.status = 'UNAPP';
1040 
1041      --Bug:4068781
1042      BEGIN
1043        arp_app_pkg.lock_p( ln_unapp_ra_id );
1044      EXCEPTION
1045        WHEN OTHERS
1046          THEN
1047            FND_MESSAGE.SET_NAME( 'FND', 'FND_LOCK_RECORD_ERROR');
1048            FND_MSG_PUB.Add;
1049            APP_EXCEPTION.raise_exception;
1050      END;
1051 
1052      -- Fetch the UNAPP row.
1053      arp_app_pkg.fetch_p( ln_unapp_ra_id, lr_ra_rec );
1054 
1055      -- Set the amount with the new value.
1056      lr_ra_rec.amount_applied := lr_ra_rec.amount_applied - ln_amount_change;
1057 
1058      -- Set the acctd amount with the new value.
1059      lr_ra_rec.acctd_amount_applied_from :=
1060      ARPCURR.functional_amount(
1061                amount        => lr_ra_rec.amount_applied
1062              , currency_code => functional_curr
1063              , exchange_rate => pn_receipt_exchange_rate
1064              , precision     => NULL
1065              , min_acc_unit  => NULL );
1066 
1067      -- Update the UNAPP row.
1068      arp_app_pkg.update_p(lr_ra_rec);
1069 
1070      -- Call MRC to replace receivable apps rows
1071 --     ar_mrc_engine3.update_selected_transaction(
1072 --                       pn_amount_applied,
1073 --                       l_app_ra_rec,
1074 --                       lr_ra_rec);
1075   END IF;
1076 
1077   IF PG_DEBUG in ('Y', 'C') THEN
1078      arp_standard.debug('update_selected_transaction: ' ||  'arp_process_application.update_amount_applied()-');
1079   END IF;
1080 
1081 EXCEPTION
1082   WHEN OTHERS THEN
1083     IF PG_DEBUG in ('Y', 'C') THEN
1084        arp_standard.debug('update_selected_transaction: ' || '-- EXCEPTION:');
1085        arp_standard.debug('update_selected_transaction: ' || 'Printing procedure parameter values:');
1086        arp_standard.debug('update_selected_transaction: ' || '-- pn_ra_id = '||TO_CHAR(pn_ra_id));
1087        arp_standard.debug('update_selected_transaction: ' || '-- pn_amount_applied = '||TO_CHAR(pn_amount_applied));
1088        arp_standard.debug('update_selected_transaction: ' || '-- pc_invoice_currency_code = '||
1089 			pc_invoice_currency_code);
1090        arp_standard.debug('update_selected_transaction: ' || '-- pn_invoice_exchange_rate = '||
1091 			to_char(pn_invoice_exchange_rate));
1092        arp_standard.debug('update_selected_transaction: ' || '-- pc_receipt_currency_code = '||
1093 			pc_receipt_currency_code);
1094        arp_standard.debug('update_selected_transaction: ' || '-- pn_receipt_exchange_rate = '||
1095 			to_char(pn_receipt_exchange_rate));
1096        arp_standard.debug('update_selected_transaction: ' || '-- pc_module_name = '||pc_module_name);
1097        arp_standard.debug('update_selected_transaction: ' || '-- pc_module_version = '||pc_module_version);
1098     END IF;
1099 
1100     app_exception.raise_exception;
1101 --    RAISE;
1102 END update_selected_transaction;
1103 --
1104 /*===========================================================================+
1105  | PROCEDURE                                                                 |
1106  |    validate_args                                                          |
1107  |                                                                           |
1108  | DESCRIPTION                                                               |
1109  |    Procedure to validate arguments passed to reverse() procedure          |
1110  |                                                                           |
1111  | SCOPE - PRIVATE                                                           |
1112  |                                                                           |
1113  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
1114  |                                                                          |
1115  | ARGUMENTS  : IN:                                                          |
1116  |                    p_ra_id - Id of application to be reversed             |
1117  |                    p_reversal_gl_date - Reversal GL date                  |
1118  |                    p_reversal_date - Reversal Date                        |
1119  |                    p_module_name - Name of module that called this proc.  |
1120  |              OUT:                                                         |
1121  |                                                                           |
1122  | RETURNS    : NONE                                                         |
1123  |                                                                           |
1124  | NOTES                                                                     |
1125  |                                                                           |
1126  | MODIFICATION HISTORY - Created by Ganesh Vaidee - 04/25/95                |
1127  |  1/2/1995 Harri Kaukovuo	Added more messages to point out NOCOPY where
1128  |				the problems is (changed app_exception.raise
1129  |				to APP_EXCEPTION.INVALID_ARGUMENT).
1130  |  2/2/1996 Harri Kaukovuo	Fixed procedure to pass module name
1131  +===========================================================================*/
1132 PROCEDURE validate_args(
1133 	p_ra_id IN ar_receivable_applications.receivable_application_id%TYPE,
1134         p_reversal_gl_date      IN DATE,
1135         p_reversal_date		IN DATE,
1136 	p_module_name		IN VARCHAR2 ) IS
1137 BEGIN
1138     IF PG_DEBUG in ('Y', 'C') THEN
1139        arp_standard.debug( 'arp_process_application.validate_args()+' );
1140     END IF;
1141 
1142     IF (p_ra_id IS NULL)
1143     THEN
1144       APP_EXCEPTION.INVALID_ARGUMENT(
1145 	  'ARP_PROCESS_APPLICATION'
1146 	, 'P_RA_ID'
1147 	, 'NULL');
1148 
1149     ELSIF (p_module_name IS NULL)
1150     THEN
1151       -- Let it be, let it be
1152       NULL;
1153 
1154     ELSIF (p_reversal_gl_date IS NULL)
1155     THEN
1156       APP_EXCEPTION.INVALID_ARGUMENT(
1157 	  'ARP_PROCESS_APPLICATION'
1158 	, 'P_REVERSAL_GL_DATE'
1159 	, 'NULL');
1160 
1161     ELSIF (p_reversal_date IS NULL)
1162     THEN
1163       APP_EXCEPTION.INVALID_ARGUMENT(
1164 	  'ARP_PROCESS_APPLICATION'
1165 	, 'P_REVERSAL_DATE'
1166 	, 'NULL');
1167     END IF;
1168 
1169     IF PG_DEBUG in ('Y', 'C') THEN
1170        arp_standard.debug( 'arp_process_application.validate_args()-' );
1171     END IF;
1172 
1173     EXCEPTION
1174       WHEN OTHERS THEN
1175     	IF PG_DEBUG in ('Y', 'C') THEN
1176     	   arp_standard.debug('EXCEPTION: arp_process_application.validate_args');
1177     	END IF;
1178         RAISE;
1179 END validate_args;
1180 --
1181 /*===========================================================================+
1182  | PROCEDURE                                                                 |
1183  |    reversal_update_ps_recs                                                |
1184  |                                                                           |
1185  | DESCRIPTION                                                               |
1186  |    This procedure is called from the standard (receipt) and credit memo   |
1187  |    reversal procedures.  It updates the payment schedule for both the     |
1188  |    source (cash receipt or on account credit) and the applied transaction.|
1189  |                                                                           |
1190  | SCOPE - PRIVATE                                                           |
1191  |                                                                           |
1192  | EXTERNAL PROCEDURES/FUNCTIONS ACCESSED                                    |
1193  | 	ARCUPSS.pls                                                          |
1194  |	arp_ps_util.get_closed_dates	Calculate and get closed dates       |
1195  |                                 	update Payment Schedule table        |
1196  |                                                                           |
1197  | ARGUMENTS  : IN:                                                          |
1198  |                    p_ra_rec - Receivables application record              |
1199  |                    p_reversal_gl_date - Reversal GL date                  |
1200  |                    p_reversal_date - Reversal Date                        |
1201  |              OUT:                                                         |
1202  |                                                                           |
1203  | RETURNS    : NONE                                                         |
1204  |                                                                           |
1205  | NOTES                                                                     |
1206  |                                                                           |
1207  | MODIFICATION HISTORY -                                                    |
1208  |  04/25/95	Ganesh Vaidee   Created
1209  |  1/3/1996 	Harri Kaukovuo	Added more code documentation
1210  |  1/4/1996	H.Kaukovuo	Added cash receipt payment schedule
1211  |				update.
1212  |  11-Mar-97   Karen Lawrance  Bug fix #464203.  Modified
1213  |                              modify_update_inv_ps_rec, added additional
1214  |                              parameter "p_app_rec_trx_type".  AR_APP is
1215  |                              passed into the procedure for the specific
1216  |                              (receipt) reversal case (called from
1217  |                              reverse()).  AR_CM is passed into the
1218  |                              procedure for the credit memo reversal
1219  |                              case (called from reverse_cm_app()).
1220  |                              modify_update_inv_ps_rec now sets
1221  |                              l_app_rec.trx_type to the parameter value
1222  |                              instead of "AR_APP" which is not correct
1223  |                              for the Credit Memo case.
1224  |                              The value of AR_APP was causing incorrect
1225  |                              processing in
1226  |                              arp_ps_util.update_reverse_actions
1227  |                              for the Credit Memo case.
1228  |  15-Mar-97   Karen Lawrance  Bug fix #493379.  Set values for lines, tax
1229  |                              freight and charges for the Credit Memo
1230  |                              case.  Should not be negative like the
1231  |                              applied transaction.
1232  |  21-Jul-97	Karen Lawrance  Release 11.
1233  |                              Renamed procedure from modify_update_inv_ps_rec
1234  |                              Cleaned up code and included some more
1235  |                              comments.
1236  |                              Included changes for cross currency,
1237  |                              using amount applied from for update of
1238  |                              receipt.
1239  |                              Bug fix #517496
1240  |                              The discount amounts apply to the transaction
1241  |                              not the source, changed code to null them
1242  |                              out NOCOPY so that they are not used in calculating
1243  |                              remaining amounts in the payment schedule
1244  |                              package.
1245  +===========================================================================*/
1246 PROCEDURE reversal_update_ps_recs (
1247 			p_ra_rec 		IN ar_receivable_applications%ROWTYPE,
1248                         -- Trx type is either
1249                         -- AR_APP meaning that it is a cash receipt reversal
1250                         -- AR_CM meaning that it is a credit memo reversal
1251                         p_app_rec_trx_type 	IN VARCHAR,
1252 			p_reversal_gl_date 	IN DATE,
1253 			p_reversal_date 	IN DATE) IS
1254 
1255 l_gl_date_closed	DATE;
1256 l_actual_date_closed	DATE;
1257 l_app_rec 		arp_global.app_rec_type;
1258 
1259 BEGIN
1260      IF PG_DEBUG in ('Y', 'C') THEN
1261         arp_standard.debug('validate_args: ' ||  'arp_process_application.reversal_update_ps_recs()+');
1262      END IF;
1263 
1264      --------------------------------------------------------------------------
1265      -- Process the payment schedule row of the applied Transaction...
1266      --
1267      --------------------------------------------------------------------------
1268 
1269      -- Determine gl_date_closed and actual_date_closed for the transaction payment
1270      -- schedule.
1271      arp_ps_util.get_closed_dates( p_ra_rec.applied_payment_schedule_id,
1272                          p_reversal_gl_date,
1273                          p_reversal_date,
1274                          l_gl_date_closed,
1275                          l_actual_date_closed, 'INV' );
1276 
1277      l_app_rec.gl_date_closed     := l_gl_date_closed;
1278      l_app_rec.actual_date_closed := l_actual_date_closed;
1279 
1280      l_app_rec.trx_type 	:= p_app_rec_trx_type;
1281 
1282      l_app_rec.user_id 		:= FND_GLOBAL.user_id;
1283      l_app_rec.ps_id 		:= p_ra_rec.applied_payment_schedule_id;
1284      l_app_rec.user_id 		:= FND_GLOBAL.user_id;
1285      l_app_rec.amount_applied 	:= -p_ra_rec.amount_applied;
1286      l_app_rec.acctd_amount_applied := -p_ra_rec.acctd_amount_applied_to;
1287      l_app_rec.line_applied 	:= -p_ra_rec.line_applied;
1288      l_app_rec.tax_applied 	:= -p_ra_rec.tax_applied;
1289      l_app_rec.freight_applied 	:= -p_ra_rec.freight_applied;
1290      l_app_rec.receivables_charges_applied :=-p_ra_rec.receivables_charges_applied;
1291      --
1292      l_app_rec.line_ediscounted     := -p_ra_rec.line_ediscounted ;
1293      l_app_rec.line_uediscounted    := -p_ra_rec.line_uediscounted ;
1294      l_app_rec.tax_ediscounted      := -p_ra_rec.tax_ediscounted ;
1295      l_app_rec.tax_uediscounted     := -p_ra_rec.tax_uediscounted ;
1296      l_app_rec.freight_ediscounted  := -p_ra_rec.freight_ediscounted ;
1297      l_app_rec.freight_uediscounted := -p_ra_rec.freight_uediscounted ;
1298      l_app_rec.charges_ediscounted  := -p_ra_rec.charges_ediscounted ;
1299      l_app_rec.charges_uediscounted := -p_ra_rec.charges_uediscounted ;
1300      --
1301      l_app_rec.unearned_discount_taken :=-p_ra_rec.unearned_discount_taken;
1302      l_app_rec.earned_discount_taken :=-p_ra_rec.earned_discount_taken;
1303      l_app_rec.acctd_earned_discount_taken :=-p_ra_rec.acctd_earned_discount_taken;
1304      l_app_rec.acctd_unearned_discount_taken :=-p_ra_rec.acctd_unearned_discount_taken;
1305 
1306      --
1307      -- Call the payment schedule utility package to update the transaction.
1308      --
1309 
1310      /* 06-JUL-2000 J Rautiainen BR Implementation
1311       * Transaction payment schedule not updated for activity application  */
1312      IF p_ra_rec.status NOT IN ('OTHER ACC', 'ACTIVITY') THEN
1313 
1314        arp_ps_util.update_reverse_actions(l_app_rec, NULL, NULL);
1315 
1316      END IF;
1317 
1318      --------------------------------------------------------------------------
1319      -- Process the payment schedule row of the source, cash receipt or on
1320      -- account credit...
1321      --
1322      -- Note that amount applied from is used if not null as this indicates
1323      -- that it is a cross currency application.  For cross currency
1324      -- applications the amount applied from holds the amount allocated
1325      -- from the receipt.  For same currency applications, the amount applied
1326      -- holds both the receipt and invoice amount applied.
1327      --------------------------------------------------------------------------
1328 
1329      l_app_rec.ps_id 			:= p_ra_rec.payment_schedule_id;
1330      l_app_rec.amount_applied 		:= nvl(p_ra_rec.amount_applied_from, p_ra_rec.amount_applied);
1331      l_app_rec.acctd_amount_applied 	:= p_ra_rec.acctd_amount_applied_from;
1332 
1333      /*  KML 07/21/97
1334          Bug fix #517496
1335          These discount amounts apply to the transaction not the source, null them
1336          out NOCOPY so that they are not used in calculating remaining amounts in the
1337          payment schedules package.  */
1338      l_app_rec.unearned_discount_taken  := NULL;
1339      l_app_rec.earned_discount_taken    := NULL;
1340      l_app_rec.acctd_earned_discount_taken := NULL;
1341      l_app_rec.acctd_unearned_discount_taken := NULL;
1342 
1343      /*  KML 05/14/97
1344          For the Credit Memo case, the line, tax, freight and charges
1345          are updated for the CM payment schedule row. */
1346      if p_app_rec_trx_type IN ( 'AR_CM', 'AR_CM_REF') then
1347         l_app_rec.line_applied     := p_ra_rec.line_applied;
1348         l_app_rec.tax_applied      := p_ra_rec.tax_applied;
1349         l_app_rec.freight_applied  := p_ra_rec.freight_applied;
1350         l_app_rec.receivables_charges_applied :=
1351                                       p_ra_rec.receivables_charges_applied;
1352      end if;
1353 
1354      --
1355      -- Call the payment schedule utility package to update the source.
1356      --
1357      arp_ps_util.update_reverse_actions(l_app_rec, NULL, NULL);
1358 
1359      IF PG_DEBUG in ('Y', 'C') THEN
1360         arp_standard.debug('validate_args: ' ||  'arp_process_application.reversal_update_ps_recs()-');
1361      END IF;
1362 EXCEPTION
1363      WHEN OTHERS THEN
1364         IF PG_DEBUG in ('Y', 'C') THEN
1365            arp_standard.debug('validate_args: ' ||
1366 	'EXCEPTION: arp_process_application.reversal_update_ps_recs' );
1367         END IF;
1368         RAISE;
1369 END reversal_update_ps_recs;
1370 --
1371 /*===========================================================================+
1372  | PROCEDURE                                                                 |
1373  |    reversal_insert_oppos_ra_recs                                          |
1374  |                                                                           |
1375  | DESCRIPTION                                                               |
1376  |    This procedure create opposing receivable application rows.            |
1377  |                                                                           |
1378  | SCOPE - PRIVATE                                                           |
1379  |                                                                           |
1380  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
1381  |	ARCIAPPS.pls                                                         |
1382  |      arp_app_pkg.insert_p	Table handler to insert into                 |
1383  |				ar_receivable_applications table.            |
1384  |                                                                           |
1385  | ARGUMENTS  : IN:                                                          |
1386  |              p_ra_rec		Receivables application record       |
1387  |              p_reversal_gl_date	Reversal GL date                     |
1388  |              p_reversal_date		Reversal Date                        |
1389  |		p_module_name		Calling module name                  |
1390  |              OUT:                                                         |
1391  |                                                                           |
1392  | RETURNS    : NONE                                                         |
1393  |                                                                           |
1394  | NOTES                                                                     |
1395  |                                                                           |
1396  | MODIFICATION HISTORY                                                      |
1397  |  Created by Ganesh Vaidee - 04/25/95
1398  |  1/2/1996	Harri Kaukovuo	Commented out NOCOPY sequence fetch because
1399  |				table handler will take care of that.
1400  |				Added second ar_receivable_applications
1401  |				row insert to reverse UNAPP row.
1402  |  1/3/1996	Harri Kaukovuo	Removed WHO column update because
1403  |				table handler will fill those.
1404  |  3/13/1996	Harri Kaukovuo	BUG 344689. Reverse receipt does not
1405  |				work.
1406  |  6/21/1996	Harri Kaukovuo	Changed reverse receipt application
1407  |				rule from '96' to
1408  |				'REVERSE RECEIPT'.
1409  |				Bug 375636 fix.
1410  |  05/06/1997  Karen Lawrance  Bug fix #481761.  Fixed application
1411  |                              rule for Credit Memo case.  Needs to
1412  |                              be 75 for CM.  Added parameter
1413  |                              p_app_rec_trx_type so we know what
1414  |                              type of application it is.
1415  |  21-Jul-97	Karen Lawrance  Release 11.
1416  |                              Renamed procedure from modify_insert_ra_rec
1417  |                              Cleaned up code and included some more
1418  |                              comments.
1419  |                              Included changes for cross currency, setting
1420  |                              opposing value for amount applied from and
1421  |                              using amount applied from for opposing UNAPP
1422  |                              rows if populated.
1423  |  27-Oct-97	Karen Murphy	Bug #495321.  In the Receipt reversal and
1424  |				Credit Memo reversal code the reversal
1425  |				of the acctd_unearned_discount_taken was
1426  |				being done twice, as a result setting it back
1427  |				to its initial value.  Removed the second
1428  |				assignment.
1429  |  10-Aug-98   Sushama Borde   Bug# 700204. Added code to make the
1430  |                              gl_posted_date NULL for reversed rows.
1431  |                              Bug# 657464. Apply_date was being set to
1432  |                              reversal_date for rows created after reversal,deleted this assignment.
1433  | 13-Jun-00  Satheesh Nambiar  Bug 1329091 - Passing a new parameter
1434  |                              pay_sched_upd_yn to accounting engine
1435  |                              to acknowldge PS is updated.
1436  | 19-Dec-03 Jyoti Pandey       Bug 2729626 Unapplied Amount is zero, but the
1437  |                              status of receipt is 'UNAPP'.
1438  +===========================================================================*/
1439 PROCEDURE reversal_insert_oppos_ra_recs (
1440 	  p_ra_rec 		IN OUT NOCOPY AR_RECEIVABLE_APPLICATIONS%ROWTYPE
1441         , p_app_rec_trx_type    IN VARCHAR
1442 	, p_reversal_gl_date 	DATE
1443 	, p_reversal_date 	DATE
1444 	, p_module_name		IN VARCHAR2
1445  	, p_called_from         IN VARCHAR2 DEFAULT NULL
1446         , p_rec_app_id          OUT NOCOPY NUMBER) IS  /* jrautiai BR implementation */
1447 
1448 l_ra_id 		NUMBER;
1449 l_ra_app_id 		NUMBER;
1450 
1451 l_rma_unapplied_ccid	NUMBER;
1452 l_rma_earned_ccid	NUMBER;
1453 l_rma_unearned_ccid	NUMBER;
1454 l_payment_schedule_id	NUMBER;
1455 
1456 l_amount_due_remaining	NUMBER;
1457 l_amount_due_original	NUMBER;
1458 l_on_account_total      NUMBER;   /*Added for Bug 2729626 */
1459 
1460 -- This is used to update cash receipt status after unapply.
1461 l_cr_rec		AR_CASH_RECEIPTS%ROWTYPE;
1462 l_ae_doc_rec            ae_doc_rec_type;
1463 
1464 
1465 l_gt_id                 NUMBER := 0;
1466 l_llca_flag             VARCHAR2(1) := 'N';
1467 l_prorated_line         NUMBER;
1468 l_prorated_tax          NUMBER;
1469 l_called_from_api       VARCHAR2(1);
1470 
1471   --Bug#2750340
1472   l_xla_ev_rec      arp_xla_events.xla_events_type;
1473   l_xla_doc_table   VARCHAR2(20);
1474 
1475   -- Bug 7241111
1476   l_llca_exist_rev varchar(1) := 'N';
1477   l_llca_exist     varchar(1) := 'N';
1478 
1479 BEGIN
1480   IF PG_DEBUG in ('Y', 'C') THEN
1481      arp_standard.debug('validate_args: ' ||  'arp_process_application.reversal_insert_oppos_ra_recs()+');
1482   END IF;
1483 
1484   -----------------------------------------------------------------
1485   -- Handle the Receipt reversal and Credit Memo reversal cases.
1486   --
1487   -- This code is called from the reverse receipt procedure.
1488   -- The reversal of on account credit memos also calls this code.
1489   --
1490   -- Reason being ...
1491   -- For Cash Receipt reversals we go through and create opposing
1492   -- rows for each record in receivable applications.  The receipt
1493   -- reversal procedure has a cursor that selects all receivable
1494   -- application rows for the receipt, then calls the reverse
1495   -- procedure for each row.
1496   --
1497   -- For Credit Memo applications, we don't have UNAPP rows.  So
1498   -- all we need to do is create an opposing APP row.  As this
1499   -- piece of code creates an opposing row for the provided
1500   -- receivable applications record, this is why it is being used
1501   -- for Credit Memos as well.
1502   -----------------------------------------------------------------
1503   IF (UPPER(p_module_name) = 'ARREREVB')
1504   THEN
1505     -- Setup the record structure before creating the opposing record.
1506     --
1507     p_ra_rec.cash_receipt_history_id := NULL;
1508     p_ra_rec.amount_applied := -p_ra_rec.amount_applied;
1509     p_ra_rec.amount_applied_from := -p_ra_rec.amount_applied_from;
1510     p_ra_rec.acctd_amount_applied_to := -p_ra_rec.acctd_amount_applied_to;
1511     p_ra_rec.acctd_amount_applied_from := -p_ra_rec.acctd_amount_applied_from;
1512 
1513     p_ra_rec.display := 'N';
1514     p_ra_rec.line_applied := -p_ra_rec.line_applied;
1515     p_ra_rec.tax_applied := -p_ra_rec.tax_applied;
1516     p_ra_rec.freight_applied := -p_ra_rec.freight_applied;
1517     p_ra_rec.receivables_charges_applied :=-p_ra_rec.receivables_charges_applied;
1518     --
1519     p_ra_rec.line_ediscounted     := -p_ra_rec.line_ediscounted ;
1520     p_ra_rec.line_uediscounted    := -p_ra_rec.line_uediscounted ;
1521     p_ra_rec.tax_ediscounted      := -p_ra_rec.tax_ediscounted ;
1522     p_ra_rec.tax_uediscounted     := -p_ra_rec.tax_uediscounted ;
1523     p_ra_rec.freight_ediscounted  := -p_ra_rec.freight_ediscounted ;
1524     p_ra_rec.freight_uediscounted := -p_ra_rec.freight_uediscounted ;
1525     p_ra_rec.charges_ediscounted  := -p_ra_rec.charges_ediscounted ;
1526     p_ra_rec.charges_uediscounted := -p_ra_rec.charges_uediscounted ;
1527     --
1528     -- Application Rule needs to be 96 for standard Applications
1529     -- and 75 for Credit Memo Applications.
1530     -- Some reports rely on the application rule being set to
1531     -- certain values .. so be careful changing them!
1532     if p_app_rec_trx_type = 'AR_CM' then
1533        p_ra_rec.application_rule := '75';
1534     else
1535        p_ra_rec.application_rule := '96';
1536     end if;
1537     --
1538     -- This will be the ID of ARCEAPPB.pls and this part of program
1539     p_ra_rec.program_id		:= -100100;
1540     p_ra_rec.earned_discount_taken := -p_ra_rec.earned_discount_taken;
1541     p_ra_rec.unearned_discount_taken := -p_ra_rec.unearned_discount_taken;
1542     p_ra_rec.acctd_earned_discount_taken :=-p_ra_rec.acctd_earned_discount_taken;
1543     p_ra_rec.acctd_unearned_discount_taken :=-p_ra_rec.acctd_unearned_discount_taken;
1544     p_ra_rec.posting_control_id := -3;
1545     p_ra_rec.gl_posted_date := NULL;
1546 
1547 /* Bugfix 2187105 */
1548     IF p_ra_rec.gl_date < p_reversal_gl_date THEN
1549         p_ra_rec.gl_date := p_reversal_gl_date;
1550     END IF;
1551 
1552     p_ra_rec.reversal_gl_date := p_ra_rec.gl_date;
1553 
1554     IF p_ra_rec.status = 'UNAPP' THEN
1555       p_ra_rec.receivables_trx_id      := NULL;
1556       p_ra_rec.link_to_customer_trx_id := NULL;
1557     END IF;
1558 
1559     ---------------------------------------------------
1560     -- Create the opposing receivable application row.
1561     --
1562     ---------------------------------------------------
1563     arp_app_pkg.insert_p( p_ra_rec, l_ra_id );
1564 
1565     -- Bug 7241111 Updating the ar_activity_details reversal record with RA IDs
1566 
1567     IF p_ra_rec.status = 'APP' or  p_ra_rec.application_type = 'CASH' THEN
1568 
1569      begin
1570        select 'Y' into l_llca_exist
1571        from ar_activity_details
1572        where cash_receipt_id = p_ra_rec.cash_receipt_id
1573 	   and source_id = p_ra_rec.receivable_application_id
1574 	   and source_table = 'RA'
1575 	   and nvl(CURRENT_ACTIVITY_FLAG,'Y') = 'R';
1576 
1577      exception
1578        when too_many_rows then
1579           l_llca_exist := 'Y';
1580        when no_data_found then
1581           l_llca_exist := 'N';
1582        when others then
1583           l_llca_exist := 'N';
1584       end;
1585 
1586 	     IF PG_DEBUG in ('Y', 'C') THEN
1587 	       arp_standard.debug('Total rows selected under activity details: ' || SQL%ROWCOUNT);
1588 	     END IF;
1589 
1590        IF NVL(l_llca_exist,'N') = 'Y' THEN
1591 
1592 
1593 	       update ar_activity_details
1594 		set source_table = 'RA',
1595 		    source_id = l_ra_id,
1596 		    CURRENT_ACTIVITY_FLAG = 'N',
1597 		     CREATED_BY = NVL(FND_GLOBAL.user_id,-1),
1598 		    CREATION_DATE = SYSDATE ,
1599 		    LAST_UPDATE_LOGIN = NVL( arp_standard.profile.last_update_login,
1600 			       p_ra_rec.last_update_login ),
1601 		    LAST_UPDATE_DATE = SYSDATE ,
1602 		    LAST_UPDATED_BY = NVL(FND_GLOBAL.user_id,-1)
1603 		where cash_receipt_id = p_ra_rec.cash_receipt_id
1604 		   and source_id = p_ra_rec.receivable_application_id
1605 		   and source_table = 'RA'
1606 		   and nvl(CURRENT_ACTIVITY_FLAG,'Y') = 'R';
1607 
1608 	     IF PG_DEBUG in ('Y', 'C') THEN
1609 	       arp_standard.debug('Total rows updated under activity details: ' || SQL%ROWCOUNT);
1610 	     END IF;
1611       END IF;
1612 
1613    END IF;
1614 
1615     IF l_ra_id IS NOT NULL THEN
1616     l_xla_ev_rec.xla_from_doc_id := l_ra_id;
1617     l_xla_ev_rec.xla_to_doc_id   := l_ra_id;
1618     l_xla_ev_rec.xla_mode        := 'O';
1619     l_xla_ev_rec.xla_call        := 'B';
1620     l_xla_ev_rec.xla_doc_table := 'APP';
1621     ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
1622     END IF;
1623 
1624     IF PG_DEBUG in ('Y', 'C') THEN
1625        arp_standard.debug('validate_args: ' || 'p_ra_rec.rec_app_id :'||p_ra_rec.receivable_application_id);
1626        arp_standard.debug('validate_args: ' || 'l_ra_id 1 :'||l_ra_id);
1627     END IF;
1628    --apandit
1629    --Bug : 2641517
1630    IF p_ra_rec.status in ('APP', 'ACTIVITY') THEN
1631     p_rec_app_id := l_ra_id;
1632    END IF;
1633 
1634    --
1635    --Release 11.5 VAT changes, reverse accounting associated with old
1636    --application and create new distributions with new application id
1637    --
1638     /* 14-APR-2000 jrautiai BR implementation
1639        If the row is not postable, accounting is not created */
1640 
1641     IF NVL(p_ra_rec.postable,'Y') = 'Y'
1642     THEN
1643       IF p_ra_rec.application_type = 'CASH'
1644       THEN
1645          l_ae_doc_rec.document_type             := 'RECEIPT';
1646          l_ae_doc_rec.document_id               := p_ra_rec.cash_receipt_id;
1647 
1648       /* 4566510 - Call etax to reverse discounts that
1649            have impacted recoverable tax
1650 
1651          NOTE:  We do not use the prorated amounts that are
1652          returned.  In theory, the reversal should be identical
1653          to the original application */
1654          IF nvl(p_ra_rec.earned_discount_taken, 0) <> 0
1655          THEN
1656             arp_etax_util.prorate_recoverable(
1657                       p_adj_id         => p_ra_rec.cash_receipt_id,
1658                       p_target_id      => p_ra_rec.applied_customer_trx_id,
1659                       p_target_line_id => null,
1660                       p_amount         => -1 *
1661                          (p_ra_rec.line_ediscounted +
1662                           p_ra_rec.tax_ediscounted),
1663                       p_apply_date     => p_ra_rec.gl_date,
1664                       p_mode           => 'UNAPP_ED',
1665                       p_upd_adj_and_ps => 'N',
1666                       p_gt_id          => l_gt_id,
1667                       p_prorated_line  => l_prorated_line,
1668                       p_prorated_tax   => l_prorated_tax,
1669                       p_ra_app_id      => l_ra_id);
1670          END IF;
1671 
1672          IF nvl(p_ra_rec.unearned_discount_taken, 0) <> 0
1673          THEN
1674            arp_etax_util.prorate_recoverable(
1675                       p_adj_id         => p_ra_rec.cash_receipt_id,
1676                       p_target_id      => p_ra_rec.applied_customer_trx_id,
1677                       p_target_line_id => null,
1678                       p_amount         => -1 *
1679                          (p_ra_rec.line_uediscounted +
1680                           p_ra_rec.tax_uediscounted),
1681                       p_apply_date     => p_ra_rec.gl_date,
1682                       p_mode           => 'UNAPP_UED',
1683                       p_upd_adj_and_ps => 'N',
1684                       p_gt_id          => l_gt_id,
1685                       p_prorated_line  => l_prorated_line,
1686                       p_prorated_tax   => l_prorated_tax,
1687                       p_ra_app_id      => l_ra_id);
1688          END IF;
1689 
1690       ELSE
1691          l_ae_doc_rec.document_type             := 'CREDIT_MEMO';
1692          l_ae_doc_rec.document_id               := p_ra_rec.customer_trx_id;
1693       END IF;
1694 
1695       l_ae_doc_rec.accounting_entity_level   := 'ONE';
1696       l_ae_doc_rec.source_table              := 'RA';
1697       l_ae_doc_rec.source_id                 := l_ra_id;                            --new id of reversal record
1698       l_ae_doc_rec.source_id_old             := p_ra_rec.receivable_application_id; --old record used for reversal
1699       l_ae_doc_rec.other_flag                := 'REVERSE';
1700 
1701     --Bug 1329091 - PS is updated before Accounting Engine Call
1702       l_ae_doc_rec.pay_sched_upd_yn := 'Y';
1703 
1704       IF l_gt_id <> 0
1705       THEN
1706         l_llca_flag := 'Y';
1707 
1708         /* 4607809 - distribute recoverable entries before acct_main call */
1709         arp_etax_util.distribute_recoverable(l_ra_id, l_gt_id);
1710       END IF;
1711 
1712       arp_acct_main.Create_Acct_Entry(l_ae_doc_rec,
1713                                       NULL,
1714                                       l_llca_flag,
1715                                       l_gt_id);
1716 
1717     END IF;
1718 
1719       IF PG_DEBUG in ('Y', 'C') THEN
1720          arp_standard.debug('validate_args: ' ||  'arp_process_application.reversal_insert_oppos_ra_recs()-');
1721       END IF;
1722       return;
1723 
1724   END IF;
1725 
1726   --------------------------------------------------------------
1727   -- This code caters for the standard case, creating opposing
1728   -- UNAPP and APP rows.
1729   --
1730   -- For example:
1731   -- Receipt
1732   -- 	100.00	UNAPP
1733   -- Apply to transaction
1734   --   -60.00	UNAPP
1735   -- 	60.00	APP
1736   -- Create On Account
1737   --   -40.00	UNAPP
1738   -- 	40.00	ACC
1739   -- Reverse/unapply applications
1740   --   -60.00   APP
1741   --    60.00   UNAPP
1742   --   -40.00   ACC
1743   --    40.00   UNAPP
1744   --
1745   --------------------------------------------------------------
1746 
1747   ----------------------------------------------------------
1748   -- Reverse the corresponding application (APP or ACC) row
1749   --
1750   ----------------------------------------------------------
1751 
1752   -- Setup the record structure before creating the opposing record.
1753   --
1754   p_ra_rec.amount_applied 		:= -p_ra_rec.amount_applied;
1755   p_ra_rec.amount_applied_from 		:= -p_ra_rec.amount_applied_from;
1756   p_ra_rec.acctd_amount_applied_from 	:= -p_ra_rec.acctd_amount_applied_from;
1757   p_ra_rec.acctd_amount_applied_to 	:= -p_ra_rec.acctd_amount_applied_to;
1758   p_ra_rec.line_applied 		:= -p_ra_rec.line_applied;
1759   p_ra_rec.tax_applied 			:= -p_ra_rec.tax_applied;
1760   p_ra_rec.freight_applied 		:= -p_ra_rec.freight_applied;
1761   p_ra_rec.receivables_charges_applied  := -p_ra_rec.receivables_charges_applied;
1762 
1763   p_ra_rec.line_ediscounted     := -p_ra_rec.line_ediscounted ;
1764   p_ra_rec.line_uediscounted    := -p_ra_rec.line_uediscounted ;
1765   p_ra_rec.tax_ediscounted      := -p_ra_rec.tax_ediscounted ;
1766   p_ra_rec.tax_uediscounted     := -p_ra_rec.tax_uediscounted ;
1767   p_ra_rec.freight_ediscounted  := -p_ra_rec.freight_ediscounted ;
1768   p_ra_rec.freight_uediscounted := -p_ra_rec.freight_uediscounted ;
1769   p_ra_rec.charges_ediscounted  := -p_ra_rec.charges_ediscounted ;
1770   p_ra_rec.charges_uediscounted := -p_ra_rec.charges_uediscounted ;
1771   p_ra_rec.earned_discount_taken 	:= -NVL(p_ra_rec.earned_discount_taken,0);
1772   p_ra_rec.unearned_discount_taken 	:= -NVL(p_ra_rec.unearned_discount_taken,0);
1773   p_ra_rec.acctd_earned_discount_taken  := -NVL(p_ra_rec.acctd_earned_discount_taken,0);
1774   p_ra_rec.acctd_unearned_discount_taken := -NVL(p_ra_rec.acctd_unearned_discount_taken,0);
1775 
1776   p_ra_rec.gl_date 			:= p_reversal_gl_date;
1777   p_ra_rec.reversal_gl_date 		:= p_reversal_gl_date;
1778 
1779   p_ra_rec.application_rule		:= '90.3';
1780   p_ra_rec.program_id			:= -100101;
1781 
1782   p_ra_rec.cash_receipt_history_id 	:= NULL;
1783   p_ra_rec.display 			:= 'N';
1784   p_ra_rec.posting_control_id 		:= -3;
1785   p_ra_rec.gl_posted_date               := NULL;
1786 
1787   -- Call the table handler to insert the APP or ACC row.
1788   arp_app_pkg.insert_p(
1789 	  p_ra_rec		-- IN
1790 	, l_ra_id		-- OUT NOCOPY
1791   	);
1792 
1793   -- Bug 7241111 Updating the ar_activity_details reversal record with RA IDs
1794 
1795    IF p_ra_rec.status = 'APP' or  p_ra_rec.application_type = 'CASH' THEN
1796 
1797       begin
1798              select 'Y' into l_llca_exist
1799 	       from ar_activity_details
1800 	       where cash_receipt_id = p_ra_rec.cash_receipt_id
1801 		   and source_id = p_ra_rec.receivable_application_id
1802 		   and source_table = 'RA';
1803       exception
1804         when too_many_rows then
1805           l_llca_exist := 'Y';
1806         when no_data_found then
1807           l_llca_exist := 'N';
1808         when others then
1809           l_llca_exist := 'N';
1810       end;
1811 
1812 
1813       IF NVL(l_llca_exist,'N') = 'Y' THEN
1814 
1815          -- To handle offset rows
1816 
1817 	     --{
1818 	         AR_ACTIVITY_DETAILS_PKG.Chk_offset_Row(l_ra_id,p_ra_rec.receivable_application_id,p_ra_rec.cash_receipt_id);
1819 
1820              --}
1821 
1822       END IF;
1823 
1824 
1825        begin
1826 	       select 'Y' into l_llca_exist_rev
1827 	       from ar_activity_details
1828 	       where cash_receipt_id = p_ra_rec.cash_receipt_id
1829 		   and source_id = p_ra_rec.receivable_application_id
1830 		   and source_table = 'RA'
1831 		   and nvl(CURRENT_ACTIVITY_FLAG,'Y') = 'R';
1832       exception
1833         when too_many_rows then
1834           l_llca_exist_rev := 'Y';
1835         when no_data_found then
1836           l_llca_exist_rev := 'N';
1837         when others then
1838           l_llca_exist_rev := 'N';
1839       end;
1840 
1841 	     IF PG_DEBUG in ('Y', 'C') THEN
1842 	       arp_standard.debug('Total rows selected under activity details: ' || SQL%ROWCOUNT);
1843 	     END IF;
1844 
1845        IF NVL(l_llca_exist_rev,'N') = 'Y' THEN
1846 
1847                update ar_activity_details
1848 		set source_table = 'RA',
1849 		    source_id = l_ra_id,
1850 		    CURRENT_ACTIVITY_FLAG = 'N',
1851 		    CREATED_BY = NVL(FND_GLOBAL.user_id,-1),
1852 		    CREATION_DATE = SYSDATE ,
1853 		    LAST_UPDATE_LOGIN = NVL( arp_standard.profile.last_update_login,
1854 			       p_ra_rec.last_update_login ),
1855 		    LAST_UPDATE_DATE = SYSDATE ,
1856 		    LAST_UPDATED_BY = NVL(FND_GLOBAL.user_id,-1)
1857 		where cash_receipt_id = p_ra_rec.cash_receipt_id
1858 		   and source_id = p_ra_rec.receivable_application_id
1859 		   and source_table = 'RA'
1860 		   and nvl(CURRENT_ACTIVITY_FLAG,'Y') = 'R';
1861 
1862 	     IF PG_DEBUG in ('Y', 'C') THEN
1863 	       arp_standard.debug('Total rows updated under activity details: ' || SQL%ROWCOUNT);
1864 	     END IF;
1865 
1866      END IF;
1867   END IF;
1868 
1869  IF l_ra_id IS NOT NULL THEN
1870    l_xla_ev_rec.xla_from_doc_id := l_ra_id;
1871    l_xla_ev_rec.xla_to_doc_id   := l_ra_id;
1872    l_xla_ev_rec.xla_mode        := 'O';
1873    l_xla_ev_rec.xla_call        := 'B';
1874    l_xla_ev_rec.xla_doc_table := 'APP';
1875    ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
1876  END IF;
1877 
1878     IF PG_DEBUG in ('Y', 'C') THEN
1879        arp_standard.debug('validate_args: ' || 'p_ra_rec.rec_app_id :'||p_ra_rec.receivable_application_id);
1880        arp_standard.debug('validate_args: ' || 'l_ra_id 2 :'||l_ra_id);
1881     END IF;
1882 
1883    --apandit
1884    --Bug : 2641517
1885    IF p_ra_rec.status in ('APP', 'ACTIVITY') THEN
1886     p_rec_app_id := l_ra_id;
1887    END IF;
1888 
1889    --
1890    --Release 11.5 VAT changes, reverse accounting associated with old
1891    --application and create new distributions with new application id
1892    --
1893 
1894   /* 4566510 - Reverse discounts via etax */
1895   IF p_ra_rec.application_type = 'CASH' THEN
1896 
1897       /* NOTE:  We do not use the prorated amounts that are
1898          returned.  In theory, the reversal should be identical
1899          to the original application */
1900      IF nvl(p_ra_rec.earned_discount_taken, 0) <> 0
1901      THEN
1902             arp_etax_util.prorate_recoverable(
1903                       p_adj_id         => p_ra_rec.cash_receipt_id,
1904                       p_target_id      => p_ra_rec.applied_customer_trx_id,
1905                       p_target_line_id => null,
1906                       p_amount         => -1 *
1907                         (p_ra_rec.line_ediscounted +
1908                          p_ra_rec.tax_ediscounted),
1909                       p_apply_date     => p_ra_rec.gl_date,
1910                       p_mode           => 'UNAPP_ED',
1911                       p_upd_adj_and_ps => 'N',
1912                       p_gt_id          => l_gt_id,
1913                       p_prorated_line  => l_prorated_line,
1914                       p_prorated_tax   => l_prorated_tax,
1915                       p_ra_app_id      => l_ra_id);
1916      END IF;
1917 
1918      IF nvl(p_ra_rec.unearned_discount_taken, 0) <> 0
1919      THEN
1920            arp_etax_util.prorate_recoverable(
1921                       p_adj_id         => p_ra_rec.cash_receipt_id,
1922                       p_target_id      => p_ra_rec.applied_customer_trx_id,
1923                       p_target_line_id => null,
1924                       p_amount         => -1 *
1925                         (p_ra_rec.line_uediscounted +
1926                          p_ra_rec.tax_uediscounted),
1927                       p_apply_date     => p_ra_rec.gl_date,
1928                       p_mode           => 'UNAPP_UED',
1929                       p_upd_adj_and_ps => 'N',
1930                       p_gt_id          => l_gt_id,
1931                       p_prorated_line  => l_prorated_line,
1932                       p_prorated_tax   => l_prorated_tax,
1933                       p_ra_app_id      => l_ra_id);
1934      END IF;
1935 
1936   END IF;
1937 
1938   /* 14-APR-2000 jrautiai BR implementation
1939      If the row is not postable, accounting is not created */
1940 
1941   IF NVL(p_ra_rec.postable,'Y') = 'Y' THEN -- jrautiai postable
1942 
1943     l_ae_doc_rec.document_type             := 'RECEIPT';
1944     l_ae_doc_rec.document_id               := p_ra_rec.cash_receipt_id;
1945     l_ae_doc_rec.accounting_entity_level   := 'ONE';
1946     l_ae_doc_rec.source_table              := 'RA';
1947     l_ae_doc_rec.source_id                 := l_ra_id;                            --new id of reversal record
1948     l_ae_doc_rec.source_id_old             := p_ra_rec.receivable_application_id; --old record used for reversal
1949     l_ae_doc_rec.other_flag                := 'REVERSE';
1950     l_ae_doc_rec.event                     := p_called_from; /* 28-SEP-2000 J Rautiainen, BR Implementation */
1951 
1952   --Bug 1329091 - PS is updated before Accounting Engine Call
1953     l_ae_doc_rec.pay_sched_upd_yn := 'Y';
1954 
1955     /* 4566510 - set llca_flag correctly */
1956     IF l_gt_id <> 0
1957     THEN
1958       l_llca_flag := 'Y';
1959 
1960       /* 4607809 - distribute recoverable entries before acct_main call */
1961       arp_etax_util.distribute_recoverable(l_ra_id, l_gt_id);
1962     END IF;
1963 
1964     arp_acct_main.Create_Acct_Entry(l_ae_doc_rec,
1965                                     NULL,
1966                                     l_llca_flag,
1967                                     l_gt_id);
1968 
1969     l_ra_app_id := l_ra_id; --store application id for pairing
1970 
1971   END IF;
1972   -----------------------------------------
1973   -- Reverse the corresponding UNAPP row
1974   --
1975   -----------------------------------------
1976 
1977   -- Setup the record structure before creating the opposing record.
1978   --
1979 
1980   -- First get cash receipt GL accounts and amount due information
1981   -- to update cash receipt status if needed.
1982     SELECT
1983 	  rma.unapplied_ccid
1984         , ed.code_combination_id    /* earned_ccid */
1985         , uned.code_combination_id  /* unearned_ccid */
1986         , ps.payment_schedule_id
1987 	, ps.amount_due_remaining
1988 	, ps.amount_due_original
1989     INTO
1990 	  l_rma_unapplied_ccid
1991         , l_rma_earned_ccid
1992         , l_rma_unearned_ccid
1993         , l_payment_schedule_id
1994 	, l_amount_due_remaining
1995 	, l_amount_due_original
1996     FROM
1997 	  ar_receipt_method_accounts	rma
1998 	, ar_payment_schedules		ps
1999 	, ar_cash_receipts		cr
2000         , ar_receivables_trx            ed
2001         , ar_receivables_trx            uned
2002     WHERE
2003 	cr.cash_receipt_id		= p_ra_rec.cash_receipt_id
2004 	AND cr.cash_receipt_id 		= ps.cash_receipt_id
2005 	AND rma.receipt_method_id 	= cr.receipt_method_id
2006 	AND rma.remit_bank_acct_use_id  = cr.remit_bank_acct_use_id
2007         AND rma.edisc_receivables_trx_id   = ed.receivables_trx_id (+)
2008         AND rma.unedisc_receivables_trx_id   = uned.receivables_trx_id (+);
2009 
2010   -- Remember that record fields have values already.
2011   -- They were fetched from AR_RECEIVABLE_APPLICATIONS table
2012   -- earlier in fetch_p(), in reverse procedure.
2013 
2014   -- Note that amount applied from is used if not null as this indicates
2015   -- that it is a cross currency application.  For cross currency
2016   -- applications the amount applied from holds the amount allocated
2017   -- from the receipt.  For same currency applications, the amount applied
2018   -- holds both the receipt and invoice amount applied.
2019   --
2020   p_ra_rec.amount_applied 		:= nvl(-p_ra_rec.amount_applied_from, -p_ra_rec.amount_applied);
2021   p_ra_rec.amount_applied_from 		:= -p_ra_rec.amount_applied_from;
2022   p_ra_rec.acctd_amount_applied_from 	:= -p_ra_rec.acctd_amount_applied_from;
2023   p_ra_rec.trans_to_receipt_rate	:= NULL;
2024   p_ra_rec.acctd_amount_applied_to 	:= NULL;
2025   p_ra_rec.line_applied 		:= NULL;
2026   p_ra_rec.tax_applied 			:= NULL;
2027   p_ra_rec.freight_applied 		:= NULL;
2028   p_ra_rec.receivables_charges_applied 	:= NULL;
2029   p_ra_rec.earned_discount_taken 	:= NULL;
2030   p_ra_rec.unearned_discount_taken 	:= NULL;
2031   p_ra_rec.acctd_earned_discount_taken 	:= NULL;
2032   p_ra_rec.acctd_unearned_discount_taken := NULL;
2033   p_ra_rec.line_ediscounted     := NULL;
2034   p_ra_rec.line_uediscounted    := NULL;
2035   p_ra_rec.tax_ediscounted      := NULL;
2036   p_ra_rec.tax_uediscounted     := NULL;
2037   p_ra_rec.freight_ediscounted  := NULL;
2038   p_ra_rec.freight_uediscounted := NULL;
2039   p_ra_rec.charges_ediscounted  := NULL;
2040   p_ra_rec.charges_uediscounted := NULL;
2041 
2042   -- Dates
2043 
2044   p_ra_rec.gl_date 			:= p_reversal_gl_date;
2045 
2046  /* Bug fix 2877224
2047     The new UNAPP record created while reversal of the application should
2048     have a value for the reversal_gl_date
2049   p_ra_rec.reversal_gl_date             := NULL; */
2050   p_ra_rec.reversal_gl_date             := p_reversal_gl_date;
2051 
2052   -- GL accounts
2053   p_ra_rec.earned_discount_ccid		:= l_rma_earned_ccid;
2054   p_ra_rec.unearned_discount_ccid	:= l_rma_unearned_ccid;
2055   p_ra_rec.code_combination_id		:= l_rma_unapplied_ccid;
2056 
2057   -- Other misc stuff. Application rule is for debugging which select
2058   -- statement created that application row.
2059   -- p_ra_rec.application_rule 		:= 'REVERSE APPLICATION2';
2060   p_ra_rec.application_rule		:= '90.4';
2061   p_ra_rec.program_id			:= -100102;
2062 
2063   -- This means that row is not yet posted to GL
2064   p_ra_rec.posting_control_id 		:= -3;
2065   p_ra_rec.gl_posted_date               := NULL;
2066 
2067   /* 14-APR-2000 jrautiai BR implementation
2068    * The new UNAPP record is only postable if the unapplied record
2069    *  was postable. In case of Short Term debt application the UNAPP rows are not postable */
2070 
2071   IF NVL(p_ra_rec.applied_payment_schedule_id,0) <> -2 AND unapp_postable(p_ra_rec.applied_customer_trx_id,p_ra_rec.applied_payment_schedule_id) THEN
2072     p_ra_rec.postable			:= 'Y';
2073   ELSE
2074     p_ra_rec.postable			:= 'N';
2075   END IF;
2076 
2077   p_ra_rec.receivables_trx_id      := NULL;
2078   p_ra_rec.link_to_customer_trx_id := NULL;
2079 
2080   p_ra_rec.cash_receipt_history_id 	:= NULL;
2081   p_ra_rec.display 			:= 'N';
2082   p_ra_rec.status 			:= 'UNAPP';
2083   p_ra_rec.application_type 		:= 'CASH';
2084   p_ra_rec.payment_schedule_id		:= l_payment_schedule_id;
2085 
2086   -- NULL out NOCOPY applied information, because this is an UNAPP row
2087   p_ra_rec.applied_payment_schedule_id	:= NULL;
2088   p_ra_rec.applied_customer_trx_id	:= NULL;
2089   p_ra_rec.applied_customer_trx_line_id	:= NULL;
2090 
2091 /* DEBUG */
2092 arp_debug.debug(' l_ra_id = ' || l_ra_id);
2093 l_ra_id := NULL;
2094 
2095   -- Call the table handler to insert the UNAPP row.
2096         arp_app_pkg.insert_p(
2097 	  p_ra_rec		-- IN
2098 	, l_ra_id		-- OUT NOCOPY
2099 	);
2100 
2101   IF l_ra_id IS NOT NULL THEN
2102    l_xla_ev_rec.xla_from_doc_id := l_ra_id;
2103    l_xla_ev_rec.xla_to_doc_id   := l_ra_id;
2104    l_xla_ev_rec.xla_mode        := 'O';
2105    l_xla_ev_rec.xla_call        := 'B';
2106    l_xla_ev_rec.xla_doc_table := 'APP';
2107    ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
2108   END IF;
2109 
2110     IF PG_DEBUG in ('Y', 'C') THEN
2111        arp_standard.debug('validate_args: ' || 'p_ra_rec.rec_app_id :'||p_ra_rec.receivable_application_id);
2112        arp_standard.debug('validate_args: ' || 'l_ra_id 3 :'||l_ra_id);
2113     END IF;
2114 
2115    --
2116    --Release 11.5 VAT changes, create UNAPP paired record
2117    --
2118   /* 14-APR-2000 jrautiai BR implementation
2119      If the row is not postable, accounting is not created */
2120 
2121   IF NVL(p_ra_rec.postable,'Y') = 'Y' THEN -- jrautiai postable
2122 
2123     l_ae_doc_rec.document_type             := 'RECEIPT';
2124     l_ae_doc_rec.document_id               := p_ra_rec.cash_receipt_id;
2125     l_ae_doc_rec.accounting_entity_level   := 'ONE';
2126     l_ae_doc_rec.source_table              := 'RA';
2127     l_ae_doc_rec.source_id                 := l_ra_id;     --new id of reversal record
2128     l_ae_doc_rec.source_id_old             := l_ra_app_id; --application id used for pairing UNAPP
2129     l_ae_doc_rec.other_flag                := 'PAIR';
2130     arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
2131 
2132     /* Bug fix 4910860 */
2133     IF nvl(p_module_name,'RAPI') = 'RAPI' THEN
2134        l_called_from_api := 'Y';
2135     ELSE
2136       l_called_from_api := 'N';
2137     END IF;
2138     arp_balance_check.Check_Appln_Balance(l_ra_id,
2139                                           l_ra_app_id,
2140                                           NULL,
2141                                           l_called_from_api);
2142 
2143   END IF;
2144   -------------------------------------------------------------------------
2145   -- Update receipt status if necessary. This means that if receipt
2146   -- was earliers applied and after unapply does not have any applications
2147   -- the status must be changed to 'Unapplied'.
2148   --
2149   -------------------------------------------------------------------------
2150 
2151   -- First, set ar_cash_receipt record values to dummy
2152   -- This is to distinguish between updateable NULL and NULL value (dummy)
2153   -- which means that column is not to be updated.
2154   arp_cash_receipts_pkg.set_to_dummy(l_cr_rec);
2155 
2156   -- Cash receipt must be fully applied in order to set the status
2157   -- to 'Applied'.
2158   /*For bug2729626 on-account amount also should be
2159   considered of determining status */
2160 
2161   select nvl(sum(ra.amount_applied),0)
2162   into   l_on_account_total
2163   from   ar_receivable_applications ra
2164   where  ra.cash_receipt_id = p_ra_rec.cash_receipt_id
2165   and    ra.status IN ('ACC','OTHER ACC');
2166 
2167   IF (l_amount_due_remaining + l_on_account_total < 0)
2168   THEN
2169     l_cr_rec.cash_receipt_id	:= p_ra_rec.cash_receipt_id;
2170     l_cr_rec.status 		:= 'UNAPP';
2171   ELSE
2172     l_cr_rec.cash_receipt_id	:= p_ra_rec.cash_receipt_id;
2173     l_cr_rec.status 		:= 'APP';
2174   END IF;
2175 
2176   -- Update cash receipt status
2177   arp_cash_receipts_pkg.update_p(l_cr_rec, p_ra_rec.cash_receipt_id);
2178 
2179   IF PG_DEBUG in ('Y', 'C') THEN
2180      arp_standard.debug('validate_args: ' ||  'arp_process_application.reversal_insert_oppos_ra_recs()-');
2181   END IF;
2182 EXCEPTION
2183   WHEN OTHERS THEN
2184     IF PG_DEBUG in ('Y', 'C') THEN
2185        arp_standard.debug('validate_args: ' ||
2186     'EXCEPTION: arp_process_application.reversal_insert_oppos_ra_recs');
2187     END IF;
2188     RAISE;
2189 
2190 END reversal_insert_oppos_ra_recs;
2191 --
2192 /*===========================================================================+
2193  | PROCEDURE                                                                 |
2194  |    reversal_update_old_ra_rec                                             |
2195  |                                                                           |
2196  | DESCRIPTION                                                               |
2197  |    This procedure updates the receivable application row that is being    |
2198  |    reversed, by setting the reversal dates and setting display to 'N'.    |
2199  |                                                                           |
2200  | SCOPE - PUBLIC                                                            |
2201  |                                                                           |
2202  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
2203  |      arp_app_pkg.update_p - table handler to update                       |
2204  |                                          ar_receivable_applications table |
2205  |                                                                           |
2206  | ARGUMENTS  : IN:                                                          |
2207  |                    p_ra_rec - Receivables application record              |
2208  |              OUT:                                                         |
2209  |                                                                           |
2210  | RETURNS    : NONE                                                         |
2211  |                                                                           |
2212  | NOTES                                                                     |
2213  |                                                                           |
2214  | MODIFICATION HISTORY - Created by Ganesh Vaidee - 04/25/95                |
2215  |
2216  |  21-Jul-97	Karen Lawrance  Release 11.
2217  |                              Renamed procedure from modify_update_old_ra_rec
2218  +===========================================================================*/
2219 PROCEDURE reversal_update_old_ra_rec( p_reversal_gl_date DATE,
2220                        p_ra_rec IN OUT NOCOPY ar_receivable_applications%ROWTYPE ) IS
2221 BEGIN
2222     IF PG_DEBUG in ('Y', 'C') THEN
2223        arp_standard.debug('validate_args: ' ||  'arp_process_application.reversal_update_old_ra_rec()+' );
2224     END IF;
2225     --
2226     p_ra_rec.display := 'N';
2227 
2228    IF p_reversal_gl_date > p_ra_rec.gl_date then
2229     p_ra_rec.reversal_gl_date := p_reversal_gl_date;
2230    ELSE
2231     p_ra_rec.reversal_gl_date := p_ra_rec.gl_date;
2232    END IF;
2233     --
2234     arp_app_pkg.update_p( p_ra_rec );
2235     --
2236     IF PG_DEBUG in ('Y', 'C') THEN
2237        arp_standard.debug('validate_args: ' ||  'arp_process_application.reversal_update_old_ra_rec()-' );
2238     END IF;
2239     EXCEPTION
2240          WHEN OTHERS THEN
2241               IF PG_DEBUG in ('Y', 'C') THEN
2242                  arp_standard.debug('validate_args: ' ||
2243 		'EXCEPTION: arp_process_application.reversal_update_old_ra_rec' );
2244               END IF;
2245               RAISE;
2246 END reversal_update_old_ra_rec;
2247 --
2248 /*===========================================================================+
2249  | PROCEDURE                                                                 |
2250  |    check_reversable                                                       |
2251  |                                                                           |
2252  | DESCRIPTION                                                               |
2253  |    This function checks -                                                 |
2254  |        1. Checks if the application has no actions, If so returns         |
2255  |           TRUE                                                            |
2256  |        2. CB should have no activity, If so fetch all customer_trx_ids    |
2257  |           and call 'validate_cb_reversal'                                 |
2258  |        3  Check  to see deletion of application  actions will not make    |
2259  |           the amount due remaining of the debit item go negative.         |
2260  |                                                                           |
2261  | SCOPE - PRIVATE                                                           |
2262  |                                                                           |
2263  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
2264  |        arp_process_chargeback.validate_cb_reversal - Check if a charge    |
2265  |                       back has any activity associated with it            |
2266  |                                                                           |
2267  | ARGUMENTS  : IN:                                                          |
2268  |                    p_ra_id - Id of application to be reversed             |
2269  |                    p_module_name - Name of module that called this proc.  |
2270  |                    p_module_name - Version of the module that called this |
2271  |                                    function                               |
2272  |              OUT:                                                         |
2273  |                    p_adj_id - Adjustment Id of inserted ar_adjustments row|
2274  |                                                                           |
2275  | RETURNS    : FALSE or TRUE                                                |
2276  |                                                                           |
2277  | NOTES - This could be a public function later                             |
2278  |                                                                           |
2279  | MODIFICATION HISTORY - Created by Ganesh Vaidee - 04/25/95                |
2280  |   04-MAY-95  G Vaidees      Added A check in validate_cb_reversal to      |
2281  |                             check for remaining balance only if adjustment|
2282  |                             exists for the application                    |
2283  |   10-MAY-95  G Vaidees      Added Over application check                  |
2284  |   12-SEP-00  skoukunt       Fix 1387071, Added (( l_bal - l_adj_amount +  |
2285  |                             l_pmt + l_edisc + l_udisc ) <> 0)             |
2286  |
2287  |   02-Jun-01  S.Nambiar      Bug 1808020 -Modified the routine to handle   |
2288  |                             Activity and receipt chargeback                                                                             |
2289  +===========================================================================*/
2290 FUNCTION check_reversable (
2291 	p_ra_id  IN ar_receivable_applications.receivable_application_id%TYPE,
2292         p_module_name    IN VARCHAR2,
2293         p_module_version IN VARCHAR2 ) RETURN BOOLEAN IS
2294     l_ps_id		ar_payment_schedules.payment_schedule_id%TYPE;
2295     l_ass_cr_id	 	ar_adjustments.associated_cash_receipt_id%TYPE;
2296     l_ct_id             ra_customer_trx.customer_trx_id%TYPE;
2297     l_adj_amt		NUMBER;
2298     l_pend_amt		NUMBER;
2299     l_pmt		NUMBER;
2300     l_edisc		NUMBER;
2301     l_udisc		NUMBER;
2302     l_bal		NUMBER;
2303     l_bal_org           NUMBER;
2304     l_status		VARCHAR2(30);
2305     l_rec_app_id	NUMBER;
2306     l_remaining_sign    NUMBER;
2307     l_adj_amount	NUMBER;
2308     --
2309     l_cb_count		NUMBER  DEFAULT 0;
2310     l_over_appln_flag	CHAR(1);
2311     l_ra_rec            ar_receivable_applications%ROWTYPE;
2312     l_dummy		NUMBER;
2313 BEGIN
2314 --  validate_args_appdel( p_ra_id );
2315     --
2316     IF PG_DEBUG in ('Y', 'C') THEN
2317        arp_standard.debug('validate_args: ' ||  'arp_process_application.check_reversable()+' );
2318     END IF;
2319     --
2320     arp_app_pkg.fetch_p(p_ra_id, l_ra_rec );
2321 
2322     IF (SIGN(l_ra_rec.applied_payment_schedule_id) <> -1) THEN
2323 
2324     BEGIN
2325         SELECT ra.cash_receipt_id,
2326 	       ra.applied_payment_schedule_id,
2327    	       ra.amount_applied,
2328 	       nvl( ra.earned_discount_taken, 0 ),
2329    	       nvl( ra.unearned_discount_taken, 0 ),
2330 	       nvl( ps.amount_due_remaining, 0 ),
2331 	       ra.status,
2332 	       ctt.allow_overapplication_flag,
2333                nvl(ps.amount_due_original, 0)
2334         INTO   l_ass_cr_id,
2335 	       l_ps_id,
2336 	       l_pmt,
2337 	       l_edisc,
2338 	       l_udisc,
2339 	       l_bal,
2340 	       l_status,
2341 	       l_over_appln_flag,
2342                l_bal_org
2343         FROM   ar_receivable_applications ra,
2344                ar_payment_schedules ps,
2345 	       ra_cust_trx_types ctt
2346         WHERE  ra.receivable_application_id = p_ra_id
2347         AND    ps.payment_schedule_id(+) = ra.applied_payment_schedule_id
2348 	AND    ctt.cust_trx_type_id = ps.cust_trx_type_id;
2349     --
2350     IF PG_DEBUG in ('Y', 'C') THEN
2351        arp_standard.debug('validate_args: ' ||  'arp_process_application.check_reversable()+' );
2352     END IF;
2353     EXCEPTION
2354 	WHEN NO_DATA_FOUND THEN
2355     	     IF PG_DEBUG in ('Y', 'C') THEN
2356     	        arp_standard.debug('validate_args: ' ||  'No data found in PS and RA table - check_reversable ' );
2357     	     END IF;
2358               RETURN FALSE;
2359      END;
2360     --
2361      ELSIF l_ra_rec.applied_payment_schedule_id = -5 THEN
2362 
2363         SELECT ra.cash_receipt_id,
2364                ra.application_ref_id,
2365                ra.amount_applied,
2366                nvl( ra.earned_discount_taken, 0 ),
2367                nvl( ra.unearned_discount_taken, 0 ),
2368                nvl( ps.amount_due_remaining, 0 ),
2369                ra.status,
2370                ctt.allow_overapplication_flag,
2371                nvl(ps.amount_due_original, 0)
2372         INTO   l_ass_cr_id,
2373                l_ct_id,
2374                l_pmt,
2375                l_edisc,
2376                l_udisc,
2377                l_bal,
2378                l_status,
2379                l_over_appln_flag,
2380                l_bal_org
2381         FROM   ar_receivable_applications ra,
2382                ar_payment_schedules ps,
2383                ra_cust_trx_types ctt
2384         WHERE  ra.receivable_application_id = p_ra_id
2385         AND    ps.customer_trx_id(+) = ra.application_ref_id
2386         AND    ctt.cust_trx_type_id = ps.cust_trx_type_id
2387         AND    ra.application_ref_type = 'CHARGEBACK';
2388 
2389         --This function will check whether any activity is against the
2390         --chargeback
2391 
2392         IF ( arp_process_chargeback.validate_cb_reversal( l_ct_id,
2393                                         p_module_name,
2394                                         p_module_version) <> TRUE ) THEN
2395               IF PG_DEBUG in ('Y', 'C') THEN
2396                  arp_standard.debug('validate_args: ' ||  'validate_cb_reversal failed' );
2397               END IF;
2398               RETURN FALSE;
2399         END IF;
2400 
2401     RETURN TRUE;
2402 
2403     ELSIF l_ra_rec.applied_payment_schedule_id IN (-6, -8) THEN
2404 	   BEGIN
2405 	      /** If the -ve Miscellaneous receipt of CC Refund is already remitted or
2406 		   ** cleared then do not allow the reversal or unapplication ***/
2407 	      SELECT 1
2408 	      INTO l_dummy
2409 	      FROM dual
2410 	      WHERE EXISTS
2411 	      ( SELECT 1
2412 	        FROM  AR_CASH_RECEIPT_HISTORY crh
2413 		    WHERE crh.cash_receipt_id = l_ra_rec.application_ref_id
2414 		    AND   crh.status IN ('REMITTED', 'CLEARED'));
2415        EXCEPTION
2416 	      WHEN NO_DATA_FOUND THEN
2417 		     l_dummy := 0;
2418 		  WHEN OTHERS THEN
2419 		     RAISE;
2420        END;
2421 
2422        IF PG_DEBUG in ('Y', 'C') THEN
2423           arp_standard.debug('validate_args: ' ||  'arp_process_application.check_reversable(-6)-' );
2424        END IF;
2425 
2426        IF l_dummy = 1 THEN
2427 	      RETURN FALSE;
2428        ELSE
2429           RETURN TRUE;
2430        END IF;
2431     ELSE
2432       --for any other activity, then no need to check further
2433       RETURN TRUE;
2434     END IF;
2435 
2436     IF PG_DEBUG in ('Y', 'C') THEN
2437        arp_standard.debug('validate_args: ' ||  'l_ps_id and l_ass_cr_id ARE' );
2438        arp_standard.debug('validate_args: ' ||  to_char( l_ps_id ) );
2439        arp_standard.debug('validate_args: ' ||  to_char( l_ass_cr_id ) );
2440     END IF;
2441     --
2442     -- If status = 'ACC', application is on account and no further
2443     -- validation is necessary. Return Status as 'NO_ACTION' since there will
2444     -- not be any actions associated with an on-account application.
2445     --
2446     IF PG_DEBUG in ('Y', 'C') THEN
2447        arp_standard.debug('validate_args: ' ||  'l_status in app_delete IS' );
2448        arp_standard.debug('validate_args: ' ||  l_status );
2449     END IF;
2450     IF ( l_status in ('ACC','OTHER ACC')) THEN
2451     	 IF PG_DEBUG in ('Y', 'C') THEN
2452     	    arp_standard.debug('validate_args: ' ||  'No application action found - check_reversable ' );
2453     	 END IF;
2454          RETURN TRUE;
2455     END IF;
2456     --
2457     -- Check if deletion would result in negative amount_due
2458     --
2459     SELECT NVL( SUM( amount), 0 )
2460     INTO    l_adj_amount
2461     FROM   ar_adjustments
2462     WHERE  payment_schedule_id = l_ps_id
2463     AND    associated_cash_receipt_id = l_ass_cr_id
2464     AND    status = 'A';
2465     --
2466     -- arp_standard.debug( 'l_over_appln_flag in app_delete IS ' );
2467     -- arp_standard.debug( l_over_appln_flag );
2468     -- arp_standard.debug( ' l_adj_amount in app_delete IS' );
2469     -- arp_standard.debug( to_char( l_bal ) || ' ' );
2470     -- arp_standard.debug( to_char( l_adj_amount ) || ' ' );
2471     -- arp_standard.debug( to_char( l_pmt )  || ' ');
2472     -- arp_standard.debug( to_char( l_edisc )  || ' ');
2473     -- arp_standard.debug( to_char( l_udisc )  || ' ');
2474     -- arp_standard.debug( to_char( l_bal_org )  || ' ');
2475     --
2476     -- If overapplication is not allowed with the applications and
2477     -- if reversal would result in negative amount for the application
2478     -- the return false
2479     --
2480     -- IF ( l_over_appln_flag = 'N' AND
2481     --    ( l_bal - l_adj_amount + l_pmt + l_edisc + l_udisc ) < 0 ) THEN
2482     --
2483     /* Bug #373738: Changed the condition in IF clause. Now checking that sign should not be
2484        different from original amount's sign. The previous condition was failing,
2485        when applications included credit memoes with adjustments and over-application
2486        is No for that credit-memo. In case of credit-memo, original amount will be negative,
2487        so it was giving problems. */
2488     --
2489     -- Fix 1387071
2490     -- Added (( l_bal - l_adj_amount + l_pmt + l_edisc + l_udisc ) <> 0)
2491     IF ( l_over_appln_flag = 'N' AND
2492        (( l_bal - l_adj_amount + l_pmt + l_edisc + l_udisc ) <> 0) AND
2493        (sign( l_bal - l_adj_amount + l_pmt + l_edisc + l_udisc ) <> sign(l_bal_org))) THEN
2494          IF PG_DEBUG in ('Y', 'C') THEN
2495             arp_standard.debug('validate_args: ' ||  'Reversal Application amount will have sign opposite to original amount' );
2496          END IF;
2497 
2498          RETURN FALSE;
2499     END IF;
2500     --
2501     SELECT count( distinct a.chargeback_customer_trx_id )
2502     INTO   l_cb_count
2503     FROM   ar_adjustments a,
2504            ar_adjustments b
2505     WHERE  a.receivables_trx_id = arp_global.G_CB_RT_ID
2506     AND    a.associated_cash_receipt_id = l_ass_cr_id
2507     AND    a.payment_schedule_id = l_ps_id
2508     AND    b.receivables_trx_id(+) = arp_global.G_CB_REV_RT_ID
2509     AND    b.customer_trx_id(+) = a.chargeback_customer_trx_id
2510     AND    b.customer_trx_id is NULL;
2511     --
2512     IF PG_DEBUG in ('Y', 'C') THEN
2513        arp_standard.debug('validate_args: ' ||  'lb_count IS ' );
2514        arp_standard.debug('validate_args: ' ||  to_char( l_cb_count ) );
2515     END IF;
2516     --
2517     -- Validate chargebacks
2518     --
2519     -- Check to see if an application has any actions, if not ,
2520     -- returns AR_APP_NO_ACTION
2521     --
2522     SELECT NVL( SUM( DECODE( status,
2523                              'A', amount,
2524                              0
2525                            )
2526                    ), 0
2527                ),
2528            NVL( SUM( DECODE( status,
2529                              'A', 0,
2530                              'R', 0,
2531                              'U', 0,
2532                                  amount
2533                            )
2534                    ), 0
2535               )
2536     INTO   l_adj_amt,
2537            l_pend_amt
2538     FROM   ar_adjustments
2539     WHERE  payment_schedule_id = l_ps_id
2540     AND    associated_cash_receipt_id = l_ass_cr_id
2541     AND    chargeback_customer_trx_id iS NULL;
2542     --
2543     IF (  l_cb_count = 0 AND l_adj_amt = 0 AND l_pend_amt = 0 ) THEN
2544           IF PG_DEBUG in ('Y', 'C') THEN
2545              arp_standard.debug('validate_args: ' ||  'No data found in ADJ table - check_reversable ' );
2546           END IF;
2547           RETURN TRUE;
2548     END IF;
2549     --
2550     IF ( l_cb_count <> 0 ) THEN
2551          IF PG_DEBUG in ('Y', 'C') THEN
2552             arp_standard.debug('validate_args: ' ||  'Inside l_cb_count <> 0' );
2553          END IF;
2554          IF ( arp_process_chargeback.validate_cb_reversal( l_ps_id,
2555 					l_ass_cr_id, l_cb_count, p_module_name,
2556 					p_module_version ) <> TRUE ) THEN
2557               IF PG_DEBUG in ('Y', 'C') THEN
2558                  arp_standard.debug('validate_args: ' ||  'validate_cb_reversal failed' );
2559               END IF;
2560               RETURN FALSE;
2561          END IF;
2562     END IF;
2563     IF PG_DEBUG in ('Y', 'C') THEN
2564        arp_standard.debug('validate_args: ' ||  'after validate_cb_reversal in app_delete' );
2565     END IF;
2566     --
2567 /****    FOR l_adj_rec IN ar_adjustments_C( l_ps_id, l_ass_cr_id )
2568     LOOP
2569         IF PG_DEBUG in ('Y', 'C') THEN
2570            arp_standard.debug('validate_args: ' ||  'inside for ar_adjustments_C in app_delete' );
2571         END IF;
2572         IF ( validate_cb_reversal(
2573 			 l_adj_rec.chargeback_customer_trx_id, l_cb_count
2574 			 p_module_name, p_module_version ) <> TRUE ) THEN
2575  	     RETURN FALSE;
2576              IF PG_DEBUG in ('Y', 'C') THEN
2577                 arp_standard.debug('validate_args: ' ||  'after validate_cb_reversal in app_delete' );
2578              END IF;
2579         END IF;
2580     END LOOP;  ****/
2581    --
2582    -- At this point, validation is successful. Lock adj and  cb records
2583    -- However, this should be done at a different level.
2584    --
2585    /* SELECT adj.adjustment_id,
2586           ps.payment_schedule_id
2587    FROM   ar_adjustments adj,
2588           ar_payment_schedules ps
2589    WHERE  adj.associated_cash_receipt_id = l_ass_cr_id
2590    AND    adj.payment_schedule_id = l_ps_id
2591    AND    adj.chargeback_customer_trx_id = ps.customer_trx_id(+)
2592    FOR    UPDATE OF ps.last_updated_by,adj.last_updated_by NOWAIT; */
2593     --
2594     IF PG_DEBUG in ('Y', 'C') THEN
2595        arp_standard.debug('validate_args: ' ||  'arp_process_application.check_reversable()-' );
2596     END IF;
2597     RETURN TRUE;
2598     --
2599     EXCEPTION
2600         WHEN OTHERS THEN
2601              IF PG_DEBUG in ('Y', 'C') THEN
2602                 arp_standard.debug('validate_args: ' ||
2603 		'EXCEPTION: arp_process_application.check_reversable - OTHER' );
2604              END IF;
2605              RAISE;
2606 END check_reversable;
2607 --
2608 /*===========================================================================+
2609  | PROCEDURE                                                                 |
2610  |    validate_args_appdel                                                   |
2611  |                                                                           |
2612  | DESCRIPTION                                                               |
2613  |    This procedure validates the check_reversable procedure arguments      |
2614  |                                                                           |
2615  | SCOPE - PRIVATE                                                           |
2616  |                                                                           |
2617  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
2618  |                                                                           |
2619  | ARGUMENTS  : IN:                                                          |
2620  |                    p_ra_id - receivable applications Id                   |
2621  |              OUT:                                                         |
2622  |                                                                           |
2623  | RETURNS    : NONE                                                         |
2624  |                                                                           |
2625  | NOTES                                                                     |
2626  |                                                                           |
2627  | MODIFICATION HISTORY - Created by Ganesh Vaidee - 04/25/95                |
2628  |                                                                           |
2629  +===========================================================================*/
2630 PROCEDURE validate_args_appdel(
2631      p_ra_id  IN ar_receivable_applications.receivable_application_id%TYPE ) IS
2632 BEGIN
2633     IF PG_DEBUG in ('Y', 'C') THEN
2634        arp_standard.debug( 'arp_process_application.validate_args_appdel()+' );
2635     END IF;
2636     --
2637     IF ( p_ra_id is NULL ) THEN
2638          FND_MESSAGE.set_name ('AR', 'AR_ARGUEMENTS_FAIL' );
2639          APP_EXCEPTION.raise_exception;
2640     END IF;
2641     --
2642     IF PG_DEBUG in ('Y', 'C') THEN
2643        arp_standard.debug( 'arp_process_application.validate_args_appdel()-' );
2644     END IF;
2645     EXCEPTION
2646          WHEN OTHERS THEN
2647     	      IF PG_DEBUG in ('Y', 'C') THEN
2648     	         arp_standard.debug('validate_args: ' ||
2649 		   'EXCEPTION: arp_process_application.validate_args_appdel' );
2650     	      END IF;
2651               RAISE;
2652 END validate_args_appdel;
2653 --
2654 /*===========================================================================+
2655  | PROCEDURE                                                                 |
2656  |    reverse_action                                                         |
2657  |                                                                           |
2658  | DESCRIPTION                                                               |
2659  |    Reverses adjustments and chargebacks associated with an application.   |
2660  |                                                                           |
2661  | SCOPE - PRIVATE                                                           |
2662  |                                                                           |
2663  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
2664  |      arp_process_chargeback.reverse_chargeback - Procedure to reverse     |
2665  |                                                   a chargeback            |
2666  |      arp_process_adjustments.reverse_adjustments- Procedure to reverse    |
2667  |                                                   a adjustment            |
2668  |      arp_ps_util.get_closed_dates - Calculate and get closed dates        |
2669  |                                                                           |
2670  | ARGUMENTS  : IN:                                                          |
2671  |                 p_ra_id -  receivable application id                      |
2672  |                 p_cr_id -  Cash receipt Id                                |
2673  |                 p_ps_id  - Payment Schedule Id                            |
2674  |                 p_reversal_gl_date - Reversal GL date                     |
2675  |                 p_select_flag - If this flag is TRUE, then select         |
2676  |				   Cash receipt Id and table, else use the   |
2677  |				   passed in values		             |
2678  |      	   p_module_name  - Name of the module that called this      |
2679  |				    procedure   			     |
2680  |      	   p_module_version  - Version of the module that called this|
2681  |			            procedure                                |
2682  |              OUT:                                                         |
2683  |                                                                           |
2684  | RETURNS    : NONE                                                         |
2685  |                                                                           |
2686  | NOTES  - This could be a public function later			     |
2687  |                                                                           |
2688  | MODIFICATION HISTORY - Created by Ganesh Vaidee - 04/25/95                |
2689  |                                                                           |
2690  +===========================================================================*/
2691 PROCEDURE reverse_action(
2692 	p_ra_id IN ar_receivable_applications.receivable_application_id%TYPE,
2693 	p_cr_id IN ar_cash_receipts.cash_receipt_id%TYPE,
2694 	p_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
2695 	p_reversal_gl_date IN DATE, p_reversal_date IN DATE,
2696 	p_select_flag IN BOOLEAN,
2697 	p_module_name IN VARCHAR2,
2698 	p_module_version IN VARCHAR2 ) IS
2699 l_adj_amount		NUMBER;
2700 l_ps_id 		ar_payment_schedules.payment_schedule_id%TYPE;
2701 l_ass_cr_id 		ar_cash_receipts.cash_receipt_id%TYPE;
2702 l_app_rec		arp_global.app_rec_type;
2703 l_ps_rec		ar_payment_schedules%ROWTYPE;
2704 
2705 l_count			NUMBER DEFAULT 0;
2706 --
2707 CURSOR ar_adjustments_C( l_ps_id ar_payment_schedules.payment_schedule_id%TYPE,
2708 			 l_ass_cr_id ar_cash_receipts.cash_receipt_id%TYPE ) IS
2709        SELECT a.chargeback_customer_trx_id,
2710 	      a.adjustment_id,
2711 	      a.status
2712        FROM   ar_adjustments a,
2713               ar_adjustments b
2714        WHERE  a.receivables_trx_id = arp_global.G_CB_RT_ID
2715        AND    a.associated_cash_receipt_id = l_ass_cr_id
2716        AND    a.payment_schedule_id = l_ps_id
2717        AND    b.receivables_trx_id(+) = arp_global.G_CB_REV_RT_ID
2718        AND    b.customer_trx_id(+) = a.chargeback_customer_trx_id
2719        AND    b.customer_trx_id is null;
2720 --
2721 CURSOR ar_adjustments_radj_C(
2722 			l_ps_id ar_payment_schedules.payment_schedule_id%TYPE,
2723 			l_ass_cr_id ar_cash_receipts.cash_receipt_id%TYPE ) IS
2724        SELECT adjustment_id,
2725 	      status
2726        FROM   ar_adjustments
2727        WHERE  associated_cash_receipt_id = l_ass_cr_id
2728        AND    payment_schedule_id = l_ps_id
2729        AND  status <> 'R'
2730        AND  associated_application_id = p_ra_id    --Bug2144783
2731        AND  chargeback_customer_trx_id is null;
2732 BEGIN
2733     IF PG_DEBUG in ('Y', 'C') THEN
2734        arp_standard.debug( 'arp_process_application.reverse_action()+' );
2735     END IF;
2736     --
2737     IF PG_DEBUG in ('Y', 'C') THEN
2738        arp_standard.debug(    to_char( p_ra_id ) );
2739        arp_standard.debug(    to_char( p_cr_id ) );
2740        arp_standard.debug(    to_char( p_ps_id ) );
2741     END IF;
2742     --
2743     IF PG_DEBUG in ('Y', 'C') THEN
2744        arp_standard.debug( 'inside BEGIN reverse_action in app_delete' );
2745     END IF;
2746 /**    IF ( p_module_name IS NOT NULL AND p_module_version IS NOT NULL ) THEN
2747          validate_reverse_action_args( p_ra_id, p_cr_id, p_ps_id,
2748         			       p_reversal_gl_date, p_reversal_date,
2749         			       p_select_flag );
2750     END IF;  **/
2751     --
2752     IF ( p_select_flag = TRUE ) THEN
2753         BEGIN
2754     	    SELECT cash_receipt_id,
2755                    applied_payment_schedule_id
2756     	    INTO   l_ass_cr_id,
2757 	           l_ps_id
2758     	    FROM   ar_receivable_applications
2759     	    WHERE  receivable_application_id = p_ra_id;
2760          EXCEPTION
2761 	    WHEN NO_DATA_FOUND THEN
2762 	         IF PG_DEBUG in ('Y', 'C') THEN
2763 	            arp_standard.debug(   'Select from ar_adjustments failed' );
2764 	         END IF;
2765 	         RETURN;
2766          END;
2767      ELSE
2768 	 l_ass_cr_id := p_cr_id;
2769 	 l_ps_id := p_ps_id;
2770      END IF;
2771      --
2772      -- Get the sum of approved and pending adjustments on the payment schedule
2773      -- This sum includes chargebacks amounts, too.
2774      --
2775      IF PG_DEBUG in ('Y', 'C') THEN
2776         arp_standard.debug( 'before SELECT NVL reverse_action in app_delete' );
2777      END IF;
2778      SELECT NVL( SUM( DECODE( status,
2779 		             'A', amount,
2780 		             0
2781 			    )
2782 		    ), 0
2783 	       ),
2784             NVL( SUM( DECODE( status,
2785 			      'A', acctd_amount,
2786 			      0
2787 			    )
2788 		    ), 0
2789 	       ),
2790             NVL( SUM( DECODE( chargeback_customer_trx_id,
2791 			      NULL, DECODE( status,
2792 					    'A', amount,
2793 					    0
2794 					  ),
2795 			      0
2796   			    )
2797 		     ), 0
2798 	        ),
2799              NVL( SUM( DECODE( status,
2800 			       'A', 0,
2801 			       'R', 0,
2802 			       'U', 0,
2803 			       amount
2804 			     )
2805 		     ), 0
2806 	        ),
2807              NVL( SUM( DECODE( status,
2808 			       'A', line_adjusted,
2809 			       0
2810 			     )
2811 		     ), 0
2812 	        ),
2813              NVL( SUM( DECODE( status,
2814 			       'A', tax_adjusted,
2815 			       0 )
2816 			     ), 0
2817 	        ),
2818              NVL( SUM( DECODE( status,
2819 			       'A', freight_adjusted,
2820 			       0
2821 			     )
2822 		     ), 0
2823 	        ),
2824              NVL( SUM( DECODE( status,
2825 			       'A', receivables_charges_adjusted,
2826 			       0
2827 			     )
2828 		     ), 0
2829 	        ),
2830              NVL( SUM( DECODE( status,
2831                                'A', DECODE( type ,
2832 			         'CHARGES',amount,0),
2833                                0
2834                              )
2835                      ), 0
2836                 )
2837        INTO  l_app_rec.amount_applied,
2838              l_app_rec.acctd_amount_applied,
2839              l_adj_amount,
2840              l_app_rec.amount_adjusted_pending,
2841              l_app_rec.line_applied,
2842              l_app_rec.tax_applied,
2843              l_app_rec.freight_applied,
2844              l_app_rec.receivables_charges_applied,
2845     	     l_app_rec.charges_type_adjusted
2846        FROM   ar_adjustments
2847        WHERE  payment_schedule_id = l_ps_id
2848        AND    associated_cash_receipt_id = l_ass_cr_id;
2849        --
2850        IF PG_DEBUG in ('Y', 'C') THEN
2851           arp_standard.debug( 'before for reverse_action in app_delete' );
2852            arp_standard.debug(   to_char( l_ps_id ) );
2853            arp_standard.debug(   to_char( l_ass_cr_id ) );
2854         END IF;
2855         FOR l_adj_rec IN ar_adjustments_C( l_ps_id, l_ass_cr_id )
2856         LOOP
2857 	    l_count := l_count + 1;
2858             IF PG_DEBUG in ('Y', 'C') THEN
2859                arp_standard.debug(   'inside for ar_adjustments_C in app_delete' );
2860             END IF;
2861 	     --
2862              -- reverse chargeback
2863 	     --
2864              arp_process_chargeback.reverse_chargeback(
2865 			             l_adj_rec.chargeback_customer_trx_id,
2866 			             p_reversal_gl_date, p_reversal_date,
2867 			             p_module_name, p_module_version );
2868              IF PG_DEBUG in ('Y', 'C') THEN
2869                 arp_standard.debug(   'after reverse_chargeback in app_delete' );
2870              END IF;
2871              --
2872              arp_process_adjustment.reverse_adjustment( l_adj_rec.adjustment_id,
2873 			           p_reversal_gl_date, p_reversal_date,
2874 			           p_module_name, p_module_version );
2875             IF PG_DEBUG in ('Y', 'C') THEN
2876                arp_standard.debug(
2877 		      'after ar_adjustment.reverse_adjustment in app_delete' );
2878             END IF;
2879             --
2880             UPDATE ra_customer_trx
2881             SET    status_trx = 'CL'
2882             WHERE  customer_trx_id = l_adj_rec.chargeback_customer_trx_id;
2883             IF PG_DEBUG in ('Y', 'C') THEN
2884                arp_standard.debug(   'after UPDATE in app_delete ' );
2885             END IF;
2886     END LOOP;
2887     --
2888     IF PG_DEBUG in ('Y', 'C') THEN
2889        arp_standard.debug(   'before if ar_adjustments_C%ROWCOUNT in app_delete ' );
2890     END IF;
2891     IF ( l_count = 0 AND l_adj_amount = 0 AND
2892 	 l_app_rec.amount_adjusted_pending = 0 ) THEN
2893          IF PG_DEBUG in ('Y', 'C') THEN
2894             arp_standard.debug(
2895 		      'AR-ARMDAPP:Theres no actions associated with the appln');
2896          END IF;
2897          RETURN;
2898     END IF;
2899     --
2900     -- Reverse adjustments , call armradj,
2901     -- select only adjustments that have not been rejected
2902     --
2903     IF PG_DEBUG in ('Y', 'C') THEN
2904        arp_standard.debug(   'before for ar_adjustments_radj_C in app_delete' );
2905     END IF;
2906     FOR l_adj_rec IN ar_adjustments_radj_C( l_ps_id, l_ass_cr_id )
2907     LOOP
2908         IF PG_DEBUG in ('Y', 'C') THEN
2909            arp_standard.debug(   'inside for ar_adjustments_radj_C in app_delete' );
2910         END IF;
2911         arp_process_adjustment.reverse_adjustment( l_adj_rec.adjustment_id,
2912                              			   p_reversal_gl_date,
2913                              			   p_reversal_date,
2914 			     			   p_module_name,
2915 						   p_module_version );
2916         IF PG_DEBUG in ('Y', 'C') THEN
2917            arp_standard.debug(
2918 			'after AR_ADJUSTMENT.reverse_adjustmen in app_delete' );
2919         END IF;
2920     END LOOP;
2921     --
2922     -- Call armups to update the payment schedule with approved adj
2923     --
2924     IF PG_DEBUG in ('Y', 'C') THEN
2925        arp_standard.debug(
2926 		   'before SELECT NVL( amount_adjusted_pending in app_delete' );
2927     END IF;
2928     SELECT NVL( amount_adjusted_pending, 0 ) -
2929            NVL( l_app_rec.amount_adjusted_pending, 0)
2930     INTO   l_app_rec.amount_adjusted_pending
2931     FROM   ar_payment_schedules
2932     WHERE  payment_schedule_id = l_ps_id;
2933     --
2934     IF PG_DEBUG in ('Y', 'C') THEN
2935        arp_standard.debug(   'before get_closed_dates in app_delete' );
2936     END IF;
2937     --
2938     -- Get closed dates to update payment schedule table
2939     --
2940     arp_ps_util.get_closed_dates( l_ps_id,
2941                      p_reversal_gl_date, p_reversal_date,
2942                      l_app_rec.gl_date_closed,
2943 		     l_app_rec.actual_date_closed, 'PMT' );
2944     --
2945     l_app_rec.ps_id := l_ps_id;
2946     l_app_rec.trx_type := 'AR_ADJ';
2947     l_app_rec.user_id := FND_GLOBAL.user_id;
2948     --
2949     IF PG_DEBUG in ('Y', 'C') THEN
2950        arp_standard.debug( 'before update_reverse_actions in app_delete' );
2951     END IF;
2952     arp_ps_util.update_reverse_actions( l_app_rec, NULL, NULL );
2953     --
2954     IF PG_DEBUG in ('Y', 'C') THEN
2955        arp_standard.debug( 'arp_process_application.reverse_action()-' );
2956     END IF;
2957     EXCEPTION
2958         WHEN OTHERS THEN
2959               IF PG_DEBUG in ('Y', 'C') THEN
2960                  arp_standard.debug(
2961 		         'EXCEPTION: arp_process_application.reverse_action' );
2962               END IF;
2963               RAISE;
2964 END reverse_action;
2965 --
2966 /*===========================================================================+
2967  | PROCEDURE                                                                 |
2968  |    validate_reverse_action_args                                           |
2969  |                                                                           |
2970  | DESCRIPTION                                                               |
2971  |    Validate arguments passed to reverse_action procedure                  |
2972  |                                                                           |
2973  | SCOPE - PRIVATE                                                           |
2974  |                                                                           |
2975  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
2976  |                                                                           |
2977  | ARGUMENTS  : IN:                                                          |
2978  |                 p_ra_id -  receivable application id                      |
2979  |                 p_cr_id -  Cash receipt Id                                |
2980  |                 p_ps_id  - Payment Schedule Id                            |
2981  |                 p_reversal_gl_date - Reversal GL date                     |
2982  |                 p_select_flag - If this flag is TRUE, then select         |
2983  |                                 Cash receipt Id and table, else use the   |
2984  |                                 passed in values                          |
2985  |              OUT:                                                         |
2986  |                                                                           |
2987  | RETURNS    : NONE                                                         |
2988  |                                                                           |
2989  | NOTES                                                                     |
2990  |                                                                           |
2991  | MODIFICATION HISTORY - Created by Ganesh Vaidee - 04/25/95                |
2992  |                                                                           |
2993  +===========================================================================*/
2994 PROCEDURE  validate_reverse_action_args(
2995         p_ra_id IN ar_receivable_applications.receivable_application_id%TYPE,
2996         p_cr_id IN ar_cash_receipts.cash_receipt_id%TYPE,
2997         p_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
2998         p_reversal_gl_date IN DATE, p_reversal_date IN DATE,
2999         p_select_flag IN BOOLEAN ) IS
3000 BEGIN
3001     IF PG_DEBUG in ('Y', 'C') THEN
3002        arp_standard.debug(
3003 	      'arp_process_application.validate_reverse_action_args()+' );
3004     END IF;
3005     --
3006     IF ( p_ra_id IS NULL OR p_reversal_gl_date IS NULL OR
3007 	 p_select_flag IS NULL ) THEN
3008          FND_MESSAGE.set_name ('AR', 'AR_ARGUEMENTS_FAIL' );
3009          APP_EXCEPTION.raise_exception;
3010     END IF;
3011     --
3012     IF ( p_select_flag <> TRUE ) THEN
3013          IF ( p_cr_id IS NULL OR p_ps_id IS NULL ) THEN
3014               FND_MESSAGE.set_name ('AR', 'AR_ARGUEMENTS_FAIL' );
3015               APP_EXCEPTION.raise_exception;
3016          END IF;
3017     END IF;
3018 --
3019     IF PG_DEBUG in ('Y', 'C') THEN
3020        arp_standard.debug( 'arp_process_application.validate_reverse_action_args()-' );
3021     END IF;
3022     EXCEPTION
3023          WHEN OTHERS THEN
3024               IF PG_DEBUG in ('Y', 'C') THEN
3025                  arp_standard.debug(
3026 		'EXCEPTION: arp_process_application.validate_reverse_action_args' );
3027               END IF;
3028               RAISE;
3029 END validate_reverse_action_args;
3030 --
3031 /*===========================================================================+
3032  | PROCEDURE                                                                 |
3033  |    receipt_application                                                    |
3034  |                                                                           |
3035  | DESCRIPTION                                                               |
3036  |    Do all actions neccessary to update PS rows and insert APP and UNAPP   |
3037  |    rows in RA table when a receipt is applied to a transaction.           |
3038  |    The PS table rows on the transaction and receipt side are updated      |
3039  |    and 2 RA rows are inserted with status 'APP' and 'UNAPP'.              |
3040  |                                                                           |
3041  | SCOPE - PUBLIC                                                            |
3042  |                                                                           |
3043  | EXTERNAL PROCEDURES/FUNCTIONS ACCESSED -                                  |
3044  |      arp_ps_util.update_invoice_related_columns                           |
3045  |      arp_ps_util.update_cm_related_columns                                |
3046  |                                                                           |
3047  | ARGUMENTS  : IN:                                                          |
3048  |                 p_receipt_ps_id - PS Id of the receipt                    |
3049  |                 p_invoice_ps_id - PS Id of the transaction                |
3050  |                 p_amount_applied - TO amount                              |
3051  |                 p_amount_applied_from - FROM amount                       |
3052  | 		   p_trans_to_receipt_rate - Cross currency rate             |
3053  |                 p_receipt_currency_code - Currency of the receipt         |
3054  |                 p_invoice_currency_code - Currency of the transaction     |
3055  |                 p_earned_discount_taken - Earned Discount taken           |
3056  |                 p_unearned_discount_taken - UnEarned Discount taken       |
3057  |                 p_apply_date - Application date                           |
3058  |                 p_gl_date    - GL Date                                    |
3059  |                 p_ussgl_transaction_code - USSGL transaction code         |
3060  |                 p_customer_trx_line_id - Line of the transaction applied  |
3061  |                 p_comments    - comments                                  |
3062  |                                                                           |
3063  |                 OTHER DESCRIPTIVE FLEX columns                            |
3064  |      	   p_module_name  - Name of the module that called this      |
3065  |				    procedure   			     |
3066  |      	   p_module_version  - Version of the module that called this|
3067  |			               procedure                             |
3068  |              OUT:                                                         |
3069  |                 p_receivable_application_id - Identifier of RA            |
3070  |                 p_acctd_amount_applied_from - Rounded acctd FROM amount   |
3071  |                 p_acctd_amount_applied_to - Rounded acctd TO amount       |
3072  |                                                                           |
3073  | RETURNS    : NONE                                                         |
3074  |                                                                           |
3075  | NOTES  -								     |
3076  |
3077  | MODIFICATION HISTORY - Created by Ganesh Vaidee - 08/29/95
3078  |  4/19/1996	Harri Kaukovuo	Added new parameter p_customer_trx_line_id
3079  |  5/3/1996	Harri Kaukovuo	Added new parameter p_out_rec_application_id
3080  |  5/6/1996	Harri Kaukovuo	Bug 354045, modified to update the
3081  |				batch status.
3082  |  10/17/1996  Karen Lawrance  Added code to prevent the creation of more
3083  |                              than one application against the same receipt
3084  |                              and invoice.
3085  |  05/06/1997  Karen Lawrance  Bug fix #481761.  Fixed application rule.
3086  |  07/21/1997	Karen Lawrance	Release 11.
3087  |                		Added processing for cross currency
3088  |                              functionality.  Included new IN, OUT NOCOPY
3089  |                              parameters and use of amount applied from.
3090  |  08/21/197	Tasman Tang	Added global_attribute_category,
3091  |				global_attribute[1-20] for global descriptive
3092  |				flexfield
3093  |  07/16/1998  Karen Murphy    Bug fix 634464.  Modified the code that works
3094  |                              out NOCOPY the status for the Receipt (APP or UNAPP)
3095  |                              Now includes the total On Account amount as
3096  |                              this is not included in the Pay Sched, Amt
3097  |                              Due Rem total.
3098  |  05/06/1999  Debbie Jancis   Modified receipt_application to accept
3099  |                              comments for insert into receivable_applications
3100  |                              for Bug 741914.
3101  |                                                                           |
3102  |  14-APR-2000 Jani Rautiainen Added parameter p_called_from. This is needed|
3103  |                              in the logic to decide whether UNAPP row is  |
3104  |                              postable or not. In BR scenario when an      |
3105  |                              Activity Application of Short Term Debt is   |
3106  |                              unapplied and then normal application is     |
3107  |                              done against the BR the UNAPP row is not     |
3108  |                              postable. This is an user requirement for BR.|
3109  |                              The parameter is defaulted to NULL so no     |
3110  |                              impact for the existing functionality.       |
3111  |                              Also added logic to prevent accounting       |
3112  |                              creation if the row is not postable.         |
3113  |                              Also added parameter p_move_deferred_tax     |
3114  |                              which indicates whether the accounting engine|
3115  |                              should move deferred tax or not              |
3116  | 13-Jun-00  Satheesh Nambiar  Bug 1329091 - Passing a new parameter
3117  |                              pay_sched_upd_yn to accounting engine
3118  |                              to acknowldge PS is updated.
3119  | 27-APR-00  jbeckett          Calls iClaim API if CLAIM                    |
3120  |                                                                           |
3121  | 06/02/2001 S.Nambiar         Bug 1808020 - Activity application should
3122  |				not fetch
3123  |                              PS record when unapplying or modifying amount
3124  | 08/03/2001  jbeckett    	Bug 1905659 - Added parameter
3125  |                              p_amount_due_remaining to receipt_application
3126  | 09/05/2002  jbeckett         Bug 2361331 - passes primary_salesrep_id to
3127  |                              create_claim
3128  | 03-Sep-02   Debbie Jancis    modified for mrc trigger replacement.        |
3129  | 				added processing for receivable apps         |
3130  | 06-SEP-02   jbeckett         Bug 2751910 - Added p_customer_reason        |
3131  | 28-Apr-03   Rahna Kader      Bug 1659928: Now the program checks for      |
3132  | 				over application before the applications     |
3133  | 				are saved                                    |
3134  | 07-AUG-03   Jon Beckett      Bug 3087819 - Claim is not created/updated   |
3135  |                              if called from Trade Management              |
3136  | 10-AUG-04   Jon Beckett	Bug 3773036 - new exception trade_mgt_err    |
3137  | 				raised if claim create/update fails to       |
3138  |				ensure control is passed correctly back to   |
3139  |				calling program and TM error is displayed.   |
3140  | 26-AUG-05   MRaymond         4566510 - Prorate discounts over tax via
3141  |                                etax.
3142  | 14-OCT-2005  Jon Beckett    Bug 4565758 - Legal entity passed to TM
3143  | 19-DEC-2006  M Raymond      5677984 - Removed etax calls for rec app
3144  |                               and moved them inside
3145  |                               update_invoice_related_columns
3146  +===========================================================================*/
3147 PROCEDURE receipt_application(
3148 	p_receipt_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
3149 	p_invoice_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
3150         p_amount_applied IN ar_receivable_applications.amount_applied%TYPE,
3151         p_amount_applied_from IN
3152                    ar_receivable_applications.amount_applied_from%TYPE,
3153         p_trans_to_receipt_rate IN
3154                    ar_receivable_applications.trans_to_receipt_rate%TYPE,
3155         p_invoice_currency_code IN
3156                    ar_payment_schedules.invoice_currency_code%TYPE,
3157         p_receipt_currency_code IN ar_cash_receipts.currency_code%TYPE,
3158         p_earned_discount_taken IN
3159                    ar_receivable_applications.earned_discount_taken%TYPE,
3160         p_unearned_discount_taken IN
3161                    ar_receivable_applications.unearned_discount_taken%TYPE,
3162         p_apply_date IN ar_receivable_applications.apply_date%TYPE,
3163 	p_gl_date IN ar_receivable_applications.gl_date%TYPE,
3164 	p_ussgl_transaction_code IN
3165                    ar_receivable_applications.ussgl_transaction_code%TYPE,
3166 	p_customer_trx_line_id	IN
3167                    ar_receivable_applications.applied_customer_trx_line_id%TYPE,
3168         p_application_ref_type IN
3169                 ar_receivable_applications.application_ref_type%TYPE,
3170         p_application_ref_id IN
3171                 ar_receivable_applications.application_ref_id%TYPE,
3172         p_application_ref_num IN
3173                 ar_receivable_applications.application_ref_num%TYPE,
3174         p_secondary_application_ref_id IN
3175                 ar_receivable_applications.secondary_application_ref_id%TYPE,
3176         p_attribute_category IN ar_receivable_applications.attribute_category%TYPE,
3177 	p_attribute1 IN ar_receivable_applications.attribute1%TYPE,
3178 	p_attribute2 IN ar_receivable_applications.attribute2%TYPE,
3179 	p_attribute3 IN ar_receivable_applications.attribute3%TYPE,
3180 	p_attribute4 IN ar_receivable_applications.attribute4%TYPE,
3181 	p_attribute5 IN ar_receivable_applications.attribute5%TYPE,
3182 	p_attribute6 IN ar_receivable_applications.attribute6%TYPE,
3183 	p_attribute7 IN ar_receivable_applications.attribute7%TYPE,
3184 	p_attribute8 IN ar_receivable_applications.attribute8%TYPE,
3185 	p_attribute9 IN ar_receivable_applications.attribute9%TYPE,
3186   	p_attribute10 IN ar_receivable_applications.attribute10%TYPE,
3187 	p_attribute11 IN ar_receivable_applications.attribute11%TYPE,
3188 	p_attribute12 IN ar_receivable_applications.attribute12%TYPE,
3189 	p_attribute13 IN ar_receivable_applications.attribute13%TYPE,
3190 	p_attribute14 IN ar_receivable_applications.attribute14%TYPE,
3191 	p_attribute15 IN ar_receivable_applications.attribute15%TYPE,
3192         p_global_attribute_category IN ar_receivable_applications.global_attribute_category%TYPE,
3193         p_global_attribute1 IN ar_receivable_applications.global_attribute1%TYPE,
3194         p_global_attribute2 IN ar_receivable_applications.global_attribute2%TYPE,
3195         p_global_attribute3 IN ar_receivable_applications.global_attribute3%TYPE,
3196         p_global_attribute4 IN ar_receivable_applications.global_attribute4%TYPE,
3197         p_global_attribute5 IN ar_receivable_applications.global_attribute5%TYPE,
3198         p_global_attribute6 IN ar_receivable_applications.global_attribute6%TYPE,
3199         p_global_attribute7 IN ar_receivable_applications.global_attribute7%TYPE,
3200         p_global_attribute8 IN ar_receivable_applications.global_attribute8%TYPE,
3201         p_global_attribute9 IN ar_receivable_applications.global_attribute9%TYPE,
3202         p_global_attribute10 IN ar_receivable_applications.global_attribute10%TYPE,
3203         p_global_attribute11 IN ar_receivable_applications.global_attribute11%TYPE,
3204         p_global_attribute12 IN ar_receivable_applications.global_attribute12%TYPE,
3205         p_global_attribute13 IN ar_receivable_applications.global_attribute13%TYPE,
3206         p_global_attribute14 IN ar_receivable_applications.global_attribute14%TYPE,
3207         p_global_attribute15 IN ar_receivable_applications.global_attribute15%TYPE,
3208         p_global_attribute16 IN ar_receivable_applications.global_attribute16%TYPE,
3209         p_global_attribute17 IN ar_receivable_applications.global_attribute17%TYPE,
3210         p_global_attribute18 IN ar_receivable_applications.global_attribute18%TYPE,
3211         p_global_attribute19 IN ar_receivable_applications.global_attribute19%TYPE,
3212         p_global_attribute20 IN ar_receivable_applications.global_attribute20%TYPE,
3213         p_comments IN ar_receivable_applications.comments%TYPE,
3214 	p_module_name IN VARCHAR2,
3215 	p_module_version IN VARCHAR2,
3216 	-- OUT NOCOPY
3217         x_application_ref_id OUT NOCOPY
3218                 ar_receivable_applications.application_ref_id%TYPE,
3219         x_application_ref_num OUT NOCOPY
3220                 ar_receivable_applications.application_ref_num%TYPE,
3221         x_return_status               OUT NOCOPY VARCHAR2,
3222         x_msg_count                   OUT NOCOPY NUMBER,
3223         x_msg_data                    OUT NOCOPY VARCHAR2,
3224 	p_out_rec_application_id OUT NOCOPY ar_receivable_applications.receivable_application_id%TYPE,
3225         p_acctd_amount_applied_from OUT NOCOPY ar_receivable_applications.acctd_amount_applied_from%TYPE,
3226         p_acctd_amount_applied_to OUT NOCOPY ar_receivable_applications.acctd_amount_applied_to%TYPE,
3227         x_claim_reason_name     OUT NOCOPY VARCHAR2,
3228 	p_called_from           IN VARCHAR2, /* jrautiai BR implementation */
3229 	p_move_deferred_tax     IN VARCHAR2,  /* jrautiai BR implementation */
3230         p_link_to_trx_hist_id   IN ar_receivable_applications.link_to_trx_hist_id%TYPE, /* jrautiai BR implementation */
3231         p_amount_due_remaining  IN
3232                 ar_payment_schedules.amount_due_remaining%TYPE,
3233 	p_payment_set_id        IN ar_receivable_applications.payment_set_id%TYPE,
3234         p_application_ref_reason IN ar_receivable_applications.application_ref_reason%TYPE,
3235         p_customer_reference     IN ar_receivable_applications.customer_reference%TYPE,
3236         p_customer_reason        IN ar_receivable_applications.customer_reason%TYPE,
3237         from_llca_call     IN VARCHAR2 DEFAULT 'N',
3238         p_gt_id            IN NUMBER   DEFAULT NULL
3239 ) IS
3240 
3241 l_rec_ra_rec     		ar_receivable_applications%ROWTYPE;
3242 l_inv_ra_rec     		ar_receivable_applications%ROWTYPE;
3243 
3244 l_cr_rec			ar_cash_receipts%ROWTYPE;
3245 l_amount_due_remaining		NUMBER;
3246 ln_batch_id			NUMBER;
3247 --
3248 l_on_account_total              NUMBER;
3249 l_ae_doc_rec                    ae_doc_rec_type;
3250 
3251   --Bug#2750340
3252   l_xla_ev_rec      arp_xla_events.xla_events_type;
3253   l_xla_doc_table   VARCHAR2(20);
3254 
3255 
3256 
3257 
3258    l_old_ps_rec                 ar_payment_schedules%ROWTYPE; /* jrautiai BR implementation */
3259    l_new_ps_rec                 ar_payment_schedules%ROWTYPE; /* jrautiai BR implementation */
3260    l_source_type                ar_distributions.source_type%TYPE; /* jrautiai BR implementation */
3261    l_exchange_rate_type         ra_customer_trx.exchange_rate_type%TYPE;
3262    l_exchange_rate_date         ra_customer_trx.exchange_date%TYPE;
3263    l_exchange_rate              ra_customer_trx.exchange_rate%TYPE;
3264    l_trx_number                 ra_customer_trx.trx_number%TYPE;
3265    l_cust_trx_type_id           ra_customer_trx.cust_trx_type_id%TYPE;
3266    l_customer_id                ra_customer_trx.bill_to_customer_id%TYPE;
3267    l_bill_to_site_use_id        ra_customer_trx.bill_to_site_use_id%TYPE;
3268    l_ship_to_site_use_id        ra_customer_trx.ship_to_site_use_id%TYPE;
3269    l_receipt_number             ar_cash_receipts.receipt_number%TYPE;
3270    l_trx_amount_due             NUMBER;
3271    l_claim_amount               NUMBER;
3272    l_salesrep_id                ra_customer_trx.primary_salesrep_id%TYPE;
3273    l_claim_trx_ps_id            NUMBER;
3274    l_claim_id                   NUMBER;
3275    l_claim_reason_code_id       NUMBER;
3276    l_claim_reason_name          VARCHAR2(80);
3277    l_request_id                 NUMBER;
3278    l_called_from_api            VARCHAR2(1);
3279 
3280    /* Bug fix 1659928 */
3281    l_inv_bal_amount             NUMBER;
3282    l_inv_orig_amount            NUMBER;
3283    l_allow_over_application     VARCHAR2(1);
3284    l_effective_amount_applied   NUMBER;
3285 
3286    /* 4566510 - etax */
3287    l_from_llca_call             VARCHAR2(1);
3288    l_gt_id                      NUMBER;
3289    l_legal_entity_id            NUMBER;
3290    l_ra_app_id                  NUMBER := NULL; -- holds APP ra_id
3291 
3292    CURSOR c_claim_trx_details (p_customer_trx_id NUMBER) IS
3293         SELECT t.exchange_rate_type
3294              , t.exchange_date
3295              , t.exchange_rate
3296              , t.trx_number
3297              , t.cust_trx_type_id
3298              , t.bill_to_customer_id
3299              , t.bill_to_site_use_id
3300              , t.ship_to_site_use_id
3301              , t.primary_salesrep_id
3302              , t.legal_entity_id
3303         FROM   ra_customer_trx t
3304         WHERE  t.customer_trx_id = p_customer_trx_id;
3305 
3306    CURSOR c_claim_rct_details (p_receipt_id NUMBER) IS
3307         SELECT receipt_number
3308         FROM   ar_cash_receipts
3309         WHERE  cash_receipt_id = p_receipt_id;
3310 
3311    CURSOR c_trx_amount_due (p_payment_schedule_id NUMBER) IS
3312         SELECT amount_due_remaining
3313         FROM   ar_payment_schedules
3314         WHERE  payment_schedule_id = p_payment_schedule_id;
3315 
3316    trade_mgt_err		EXCEPTION; -- Bug 3773036
3317 
3318 BEGIN
3319     IF PG_DEBUG in ('Y', 'C') THEN
3320        arp_standard.debug(   'arp_process_application.receipt_application()+' );
3321     END IF;
3322 
3323    /* Bug 3773036: Initializing return status ..*/
3324    x_return_status := FND_API.G_RET_STS_SUCCESS;
3325 
3326    /* move parm gt_id into local var.  It may get set by prorate_recoverable
3327       if discounts are present */
3328    l_gt_id := p_gt_id;
3329 
3330    /* 12-JUL-2000 J Rautiainen BR Implementation
3331     * Storing the old image of the payment schedule
3332     * This is only done when application is done outside the BR remittance program */
3333 
3334     IF NVL(p_called_from,'NONE') not in ('BR_REMITTED','BR_FACTORED_WITH_RECOURSE','BR_FACTORED_WITHOUT_RECOURSE')
3335        AND p_invoice_ps_id IS NOT NULL AND (SIGN(p_invoice_ps_id) <> -1)  THEN
3336 
3337       arp_ps_pkg.fetch_p( p_invoice_ps_id, l_old_ps_rec );
3338 
3339     END IF;
3340 
3341     -- Output IN parameters
3342     IF PG_DEBUG in ('Y', 'C') THEN
3343        arp_standard.debug(   'Receipt PS Id           : '||TO_CHAR( p_receipt_ps_id ) );
3344        arp_standard.debug(   'Invoice PS Id           : '||TO_CHAR( p_invoice_ps_id ) );
3345        arp_standard.debug(   'Amount Applied          : '||TO_CHAR( p_amount_applied ) );
3346        arp_standard.debug(   'Amount Applied From     : '||TO_CHAR( p_amount_applied_from ) );
3347        arp_standard.debug(   'Trans to Receipt Rate   : '||TO_CHAR( p_trans_to_receipt_rate ) );
3348        arp_standard.debug(   'Invoice Currency Code   : '||p_invoice_currency_code );
3349        arp_standard.debug(   'Receipt Currency Code   : '||p_receipt_currency_code );
3350        arp_standard.debug(   'Earned Discount         : '||TO_CHAR( p_earned_discount_taken ) );
3351        arp_standard.debug(   'Unearned Discount       : '||TO_CHAR( p_unearned_discount_taken ) );
3352        arp_standard.debug(   'GL Date                 : '||TO_CHAR( p_gl_date ) );
3353        arp_standard.debug(   'Apply Date              : '||TO_CHAR( p_apply_date ) );
3354        arp_standard.debug(   'Customer Trx Line Id    : '||TO_CHAR( p_customer_trx_line_id ) );
3355     END IF;
3356 
3357     -----------------------------------------------------
3358     --  KML 10/17/1996
3359     --  Prevent the creation of more than one application
3360     --  against the same receipt and invoice.
3361     --  Bug #505538
3362     --  Modified the condition to allow application for
3363     --  invoice applied to same receipt for different
3364     --  invoice lines.
3365     -----------------------------------------------------
3366     DECLARE
3367       l_found   varchar2(1) := 'N';
3368     BEGIN
3369       IF p_customer_trx_line_id IS NULL THEN
3370         select 'Y'
3371         into   l_found
3372         from   ar_receivable_applications rap
3373         where  rap.payment_schedule_id = p_receipt_ps_id
3374         and    rap.applied_payment_schedule_id = p_invoice_ps_id
3375         and    rap.applied_customer_trx_line_id IS NULL /* Bug fix 1659928*/
3376         and    rap.display = 'Y'
3377         and    rap.status = 'APP';
3378       ELSE
3379         select 'Y'
3380         into   l_found
3381         from   ar_receivable_applications rap
3382         where  rap.payment_schedule_id = p_receipt_ps_id
3383         and    rap.applied_payment_schedule_id = p_invoice_ps_id
3384         and    rap.applied_customer_trx_line_id = p_customer_trx_line_id
3385         and    rap.display = 'Y'
3386         and    rap.status = 'APP';
3387       END IF;
3388       if l_found = 'Y' then
3389         raise too_many_rows;
3390       end if;
3391 
3392     EXCEPTION
3393       when no_data_found then
3394         null;
3395       when too_many_rows then
3396         FND_MESSAGE.set_name ('AR', 'AR_RW_PAID_INVOICE_TWICE' );
3397         APP_EXCEPTION.raise_exception;
3398     END;
3399 
3400     /* Bug fix 1659928
3401        Check if the payment schedule of this transaction is over applied and
3402        the transaction type does not allow overapplication */
3403 
3404        select   ps.amount_due_remaining,ps.amount_due_original,ctt.allow_overapplication_flag
3405        into     l_inv_bal_amount, l_inv_orig_amount, l_allow_over_application
3406        from     ra_cust_trx_types ctt, ar_payment_schedules ps
3407        where    ps.payment_schedule_id = p_invoice_ps_id
3408        and      ps.cust_trx_type_id = ctt.cust_trx_type_id;
3409 
3410        l_effective_amount_applied := NVL(p_amount_applied,0) +
3411                                      NVL(p_earned_discount_taken,0)+
3412                                      NVL(p_unearned_discount_taken,0);
3413        IF l_allow_over_application ='N'
3414         AND arp_deduction.overapplication_indicator(l_inv_orig_amount,
3415                                                     l_inv_bal_amount,
3416                                                     l_effective_amount_applied) ='Y' THEN
3417           FND_MESSAGE.set_name ('AR', 'AR_CKAP_OVERAPP' );
3418           APP_EXCEPTION.raise_exception;
3419        END IF;
3420 
3421     /* End bug fix 1659928 */
3422 
3423     -- Validate the parameters that have been passed to the procedure.
3424     --
3425     IF (p_module_name IS NOT NULL AND
3426         p_module_version IS NOT NULL ) THEN
3427        validate_receipt_appln_args
3428           ( p_receipt_ps_id,
3429             p_invoice_ps_id,
3430             p_amount_applied,
3431             p_amount_applied_from,
3432             p_trans_to_receipt_rate,
3433             p_invoice_currency_code,
3434             p_receipt_currency_code,
3435             p_earned_discount_taken,
3436             p_unearned_discount_taken,
3437             p_apply_date,
3438             p_gl_date );
3439     END IF;
3440 
3441     --------------------------------------------------------------------------
3442     -- Process the Cash Receipt ...
3443     --
3444     -- This involves updating the Cash Receipt row in Payment Schedules
3445     -- and creating the UNAPP row in receivable applications.
3446     --------------------------------------------------------------------------
3447 
3448     -- Populate CC ID columns by selecting from receipt method accounts and
3449     -- payment schedule tables.
3450     -- Release 11.5 VAT changes get discount accounts from activity
3451     /*  Reverted the fix introduced by 5571095 for bug 5638732 and replaced ce_bank_acct_uses by ce_bank_acct_uses_ou_v
3452         for performance issue */
3453     /*  Removing the ce_bank_acct_uses table from the select statement for bug 5571095 by gnramasa on 06/10/2006  */
3454     /*  7332175 use different view as per input from CE to resolve no data found issue and keeping above performance issue in check*/
3455     SELECT ps.cash_receipt_id
3456 	 , ps.amount_due_remaining
3457          , rma.unapplied_ccid
3458          , ed.code_combination_id
3459          , uned.code_combination_id
3460 	 , crh.batch_id
3461     INTO   l_rec_ra_rec.cash_receipt_id
3462 	 , l_amount_due_remaining
3463          , l_rec_ra_rec.code_combination_id
3464          , l_inv_ra_rec.earned_discount_ccid
3465          , l_inv_ra_rec.unearned_discount_ccid
3466 	 , ln_batch_id
3467     FROM   ar_cash_receipts 		cr
3468          , ar_cash_receipt_history 	crh
3469          , ar_receipt_methods 	        rm
3470          , ce_bank_acct_uses_ou    	ba
3471          , ar_receipt_method_accounts	rma
3472 	 , ar_payment_schedules 	ps
3473          , ar_receivables_trx           ed
3474          , ar_receivables_trx           uned
3475     WHERE  ps.payment_schedule_id	= p_receipt_ps_id
3476     AND    cr.cash_receipt_id		= ps.cash_receipt_id
3477     AND	   cr.cash_receipt_id		= crh.cash_receipt_id
3478     AND    crh.current_record_flag	= 'Y'
3479     AND    rm.receipt_method_id		= cr.receipt_method_id
3480     AND    ba.bank_acct_use_id		= cr.remit_bank_acct_use_id
3481     AND    rma.remit_bank_acct_use_id	= ba.bank_acct_use_id
3482     AND    rma.receipt_method_id	= rm.receipt_method_id
3483     AND    rma.edisc_receivables_trx_id = ed.receivables_trx_id (+)
3484     AND    rma.unedisc_receivables_trx_id = uned.receivables_trx_id (+);
3485 
3486 
3487     /* 4566510 - get invoice trx_id for use in prorate_recoverable */
3488     SELECT customer_trx_id
3489     INTO   l_inv_ra_rec.applied_customer_trx_id
3490     FROM   ar_payment_schedules
3491     WHERE  payment_schedule_id = p_invoice_ps_id;
3492 
3493 arp_util.debug('trx_id = ' || l_inv_ra_rec.applied_customer_trx_id);
3494 
3495  -----------------------------------------------------
3496     --  Bug 1814806
3497     --  Prevent the application of a receipt against a claim number
3498     --  more than once
3499   -----------------------------------------------------
3500 /* Bug 2719456 : The following sql will be called only iclaim is installed */
3501 /* Bug 3251839 - checks against cached arp_global value */
3502    IF arp_global.tm_installed_flag = 'Y' THEN
3503 
3504      DECLARE
3505        l_count   number;
3506        claim_multi_assign exception;
3507 
3508      BEGIN
3509        select count(*)
3510        into   l_count
3511        from   ar_receivable_applications rap
3512        where  rap.cash_receipt_id = l_rec_ra_rec.cash_receipt_id
3513        and    rap.secondary_application_ref_id =
3514                                 p_secondary_application_ref_id
3515        and    rap.application_ref_type = 'CLAIM'
3516        and    rap.display = 'Y';
3517 
3518        if l_count > 0  then
3519           raise claim_multi_assign;
3520        end if;
3521 
3522      EXCEPTION
3523        when no_data_found then
3524          null;
3525        when claim_multi_assign then
3526          FND_MESSAGE.set_name ('AR', 'AR_RW_APP_CLAIM_MULTI_ASSIGN' );
3527          APP_EXCEPTION.raise_exception;
3528      END;
3529 
3530    END IF;
3531     -- Step 1, update the cash receipt in the payment schedule table.
3532     --
3533     -- Note that amount applied from is passed if not null as this indicates
3534     -- that it is a cross currency application.  For cross currency
3535     -- applications the amount applied from holds the amount allocated
3536     -- from the receipt.  For same currency applications, the amount applied
3537     -- holds both the receipt and invoice amount applied.
3538     --
3539     arp_ps_util.update_receipt_related_columns(
3540                 p_receipt_ps_id,
3541                 nvl(p_amount_applied_from, p_amount_applied),
3542                 p_apply_date,
3543                 p_gl_date,
3544                 l_rec_ra_rec.acctd_amount_applied_from,
3545                 NULL_VAR );         -- NULL modified to NULL_VAR for bug 460959 (Oracle 8)
3546 
3547     -- This is passed back to the client as the true acctd amount (calculated
3548     -- in the payment schedule utility procedure).
3549     p_acctd_amount_applied_from := l_rec_ra_rec.acctd_amount_applied_from;
3550 
3551     -- Step 2, create UNAPP row in receivable applications.
3552     --
3553     -- First we need to populate the receivable applications record with the
3554     -- required values.  Note nvl for amount applied again (see note above).
3555     --
3556     l_rec_ra_rec.status := 'UNAPP';
3557     l_rec_ra_rec.amount_applied := nvl(-p_amount_applied_from, -p_amount_applied);
3558     l_rec_ra_rec.amount_applied_from := -p_amount_applied_from;
3559     l_rec_ra_rec.trans_to_receipt_rate := null;
3560     l_rec_ra_rec.payment_schedule_id := p_receipt_ps_id;
3561     l_rec_ra_rec.application_type := 'CASH';
3562     l_rec_ra_rec.application_rule := '60.7';
3563     l_rec_ra_rec.program_id	:= -100103;
3564     l_rec_ra_rec.apply_date := p_apply_date;
3565     l_rec_ra_rec.gl_date := p_gl_date;
3566     l_rec_ra_rec.posting_control_id := -3;
3567 
3568     /* 14-APR-2000 jrautiai BR implementation
3569      * In specific BR scenario the UNAPP pair of application is not postable
3570      * See procedure description for more information */
3571 
3572     IF nvl(p_called_from,'NONE') = 'BR_FACTORED_WITH_RECOURSE' THEN -- jrautiai BR project
3573       l_rec_ra_rec.postable := 'N';
3574     END IF;
3575 
3576     l_rec_ra_rec.display := 'N';
3577     l_rec_ra_rec.ussgl_transaction_code := p_ussgl_transaction_code;
3578     l_rec_ra_rec.attribute_category := p_attribute_category;
3579     l_rec_ra_rec.attribute1 := p_attribute1;
3580     l_rec_ra_rec.attribute2 := p_attribute2;
3581     l_rec_ra_rec.attribute3 := p_attribute3;
3582     l_rec_ra_rec.attribute4 := p_attribute4;
3583     l_rec_ra_rec.attribute5 := p_attribute5;
3584     l_rec_ra_rec.attribute6 := p_attribute6;
3585     l_rec_ra_rec.attribute7 := p_attribute7;
3586     l_rec_ra_rec.attribute8 := p_attribute8;
3587     l_rec_ra_rec.attribute9 := p_attribute9;
3588     l_rec_ra_rec.attribute10 := p_attribute10;
3589     l_rec_ra_rec.attribute11 := p_attribute11;
3590     l_rec_ra_rec.attribute12 := p_attribute12;
3591     l_rec_ra_rec.attribute13 := p_attribute13;
3592     l_rec_ra_rec.attribute14 := p_attribute14;
3593     l_rec_ra_rec.attribute15 := p_attribute15;
3594     l_rec_ra_rec.global_attribute_category := p_global_attribute_category;
3595     l_rec_ra_rec.global_attribute1 := p_global_attribute1;
3596     l_rec_ra_rec.global_attribute2 := p_global_attribute2;
3597     l_rec_ra_rec.global_attribute3 := p_global_attribute3;
3598     l_rec_ra_rec.global_attribute4 := p_global_attribute4;
3599     l_rec_ra_rec.global_attribute5 := p_global_attribute5;
3600     l_rec_ra_rec.global_attribute6 := p_global_attribute6;
3601     l_rec_ra_rec.global_attribute7 := p_global_attribute7;
3602     l_rec_ra_rec.global_attribute8 := p_global_attribute8;
3603     l_rec_ra_rec.global_attribute9 := p_global_attribute9;
3604     l_rec_ra_rec.global_attribute10 := p_global_attribute10;
3605     l_rec_ra_rec.global_attribute11 := p_global_attribute11;
3606     l_rec_ra_rec.global_attribute12 := p_global_attribute12;
3607     l_rec_ra_rec.global_attribute13 := p_global_attribute13;
3608     l_rec_ra_rec.global_attribute14 := p_global_attribute14;
3609     l_rec_ra_rec.global_attribute15 := p_global_attribute15;
3610     l_rec_ra_rec.global_attribute16 := p_global_attribute16;
3611     l_rec_ra_rec.global_attribute17 := p_global_attribute17;
3612     l_rec_ra_rec.global_attribute18 := p_global_attribute18;
3613     l_rec_ra_rec.global_attribute19 := p_global_attribute19;
3614     l_rec_ra_rec.global_attribute20 := p_global_attribute20;
3615 
3616     --
3617     --
3618 IF nvl(p_called_from,'NONE') <> 'AUTORECAPI' THEN -- autorecapi bichatte project
3619 
3620     arp_app_pkg.insert_p( l_rec_ra_rec,
3621                           l_rec_ra_rec.receivable_application_id );
3622 
3623 END IF;
3624 
3625     --------------------------------------------------------------------------
3626     -- Process the Transaction ...
3627     --
3628     -- This involves updating the transaction row in Payment Schedules
3629     -- and creating the APP row in receivable applications.
3630     --------------------------------------------------------------------------
3631 
3632 /* END OF IF CASE THAT NEEDS TO BE REMOVED ONCE LLCA IS COMPLETE */
3633 
3634     -- Populate CC ID columns by selecting from ra_cust_trx_line_gl_dist and
3635     -- and payment schedule tables.
3636     --
3637 
3638     /* 14-APR-2000 jrautiai BR implementation
3639      * Moved into a procedure for BR transactions has the accounting in ar_distributions table.
3640      * instead of ra_cust_trx_line_gl_dist */
3641 
3642   --for PS -2, it should go in side the if condition
3643 
3644     IF (p_invoice_ps_id not in (-4,-5)) THEN
3645 
3646         ARP_PROCESS_APPLICATION.fetch_app_ccid(p_invoice_ps_id,
3647                                            l_inv_ra_rec.applied_customer_trx_id,
3648                                            l_inv_ra_rec.code_combination_id,
3649                                            l_source_type);
3650 
3651       -- Step 1, update the transaction in the payment schedule table.
3652     l_from_llca_call := from_llca_call;
3653 --{HYULLCA
3654 -- Not LLCA current process
3655 IF l_from_llca_call     = 'N' THEN
3656 arp_standard.debug(' LLCA application commit by pass maintenance of the invoice ps done by LLCA back ground');
3657 
3658       -- 5569488, for receipt where confirmation is required, pass the cash_receipt_id
3659       IF NULL_VAR.cash_receipt_id IS NULL AND NVL(p_called_from,'NONE') = 'AUTORECAPI' THEN
3660          NULL_VAR.cash_receipt_id := l_rec_ra_rec.cash_receipt_id ;
3661       END IF ;
3662 
3663     arp_ps_util.update_invoice_related_columns(
3664                 'CASH',
3665                 p_invoice_ps_id,
3666                 p_amount_applied,
3667                 p_earned_discount_taken,
3668                 p_unearned_discount_taken,
3669                 p_apply_date,
3670                 p_gl_date,
3671                 l_inv_ra_rec.acctd_amount_applied_to,
3672                 l_inv_ra_rec.acctd_earned_discount_taken,
3673                 l_inv_ra_rec.acctd_unearned_discount_taken,
3674                 l_inv_ra_rec.line_applied,
3675                 l_inv_ra_rec.tax_applied,
3676                 l_inv_ra_rec.freight_applied,
3677                 l_inv_ra_rec.receivables_charges_applied,
3678                 l_inv_ra_rec.line_ediscounted,
3679                 l_inv_ra_rec.tax_ediscounted,
3680                 l_inv_ra_rec.freight_ediscounted,
3681                 l_inv_ra_rec.charges_ediscounted,
3682                 l_inv_ra_rec.line_uediscounted,
3683                 l_inv_ra_rec.tax_uediscounted,
3684                 l_inv_ra_rec.freight_uediscounted,
3685                 l_inv_ra_rec.charges_uediscounted,
3686                 l_inv_ra_rec.rule_set_id,
3687                 NULL_VAR,
3688                 l_rec_ra_rec.cash_receipt_id,
3689                 l_ra_app_id,
3690                 l_gt_id );
3691     -- This is passed back to the client as the true acctd amount (calculated
3692     -- in the payment schedule utility procedure).
3693     p_acctd_amount_applied_to := l_inv_ra_rec.acctd_amount_applied_to;
3694 
3695 ELSE -- LLCA = 'Y'
3696 
3697 arp_standard.debug(' LLCA application commit');
3698 
3699    arp_process_det_pkg.get_app_ra_amounts
3700      (p_gt_id         => l_gt_id,
3701       x_ra_rec        => l_inv_ra_rec);
3702 
3703 arp_standard.debug('  Ra_id in GT from LLCA l_inv_ra_rec.receivable_application_id :'||l_inv_ra_rec.receivable_application_id);
3704 
3705  p_acctd_amount_applied_to := l_inv_ra_rec.acctd_amount_applied_to; /* 5189370 */
3706 
3707 END IF;
3708 --}
3709    END IF;
3710     -- Step 2, create APP row in receivable applications.
3711     --
3712     -- First we need to populate the receivable applications record with the
3713     -- required values.
3714     --
3715 
3716     l_inv_ra_rec.status 		:= 'APP';
3717     l_inv_ra_rec.amount_applied 	:= p_amount_applied;
3718     l_inv_ra_rec.amount_applied_from    := p_amount_applied_from;
3719     l_inv_ra_rec.trans_to_receipt_rate  := p_trans_to_receipt_rate;
3720     l_inv_ra_rec.cash_receipt_id 	:= l_rec_ra_rec.cash_receipt_id;
3721     l_inv_ra_rec.acctd_amount_applied_from := -l_rec_ra_rec.acctd_amount_applied_from;
3722     l_inv_ra_rec.payment_schedule_id 	:= p_receipt_ps_id;
3723     l_inv_ra_rec.applied_payment_schedule_id := p_invoice_ps_id;
3724     l_inv_ra_rec.earned_discount_taken 	:= p_earned_discount_taken;
3725     l_inv_ra_rec.unearned_discount_taken:= p_unearned_discount_taken;
3726     l_inv_ra_rec.application_type 	:= 'CASH';
3727     l_inv_ra_rec.application_rule       := '60.0';
3728     l_inv_ra_rec.program_id             := -100104;
3729     l_inv_ra_rec.apply_date 		:= p_apply_date;
3730     l_inv_ra_rec.gl_date 		:= p_gl_date;
3731     l_inv_ra_rec.posting_control_id 	:= -3;
3732     l_inv_ra_rec.display 		:= 'Y';
3733     l_inv_ra_rec.ussgl_transaction_code := p_ussgl_transaction_code;
3734     l_inv_ra_rec.attribute_category 	:= p_attribute_category;
3735     l_inv_ra_rec.attribute1 		:= p_attribute1;
3736     l_inv_ra_rec.attribute2 		:= p_attribute2;
3737     l_inv_ra_rec.attribute3 		:= p_attribute3;
3738     l_inv_ra_rec.attribute4 		:= p_attribute4;
3739     l_inv_ra_rec.attribute5 		:= p_attribute5;
3740     l_inv_ra_rec.attribute6 		:= p_attribute6;
3741     l_inv_ra_rec.attribute7 		:= p_attribute7;
3742     l_inv_ra_rec.attribute8 		:= p_attribute8;
3743     l_inv_ra_rec.attribute9 		:= p_attribute9;
3744     l_inv_ra_rec.attribute10 		:= p_attribute10;
3745     l_inv_ra_rec.attribute11 		:= p_attribute11;
3746     l_inv_ra_rec.attribute12 		:= p_attribute12;
3747     l_inv_ra_rec.attribute13 		:= p_attribute13;
3748     l_inv_ra_rec.attribute14 		:= p_attribute14;
3749     l_inv_ra_rec.attribute15 		:= p_attribute15;
3750     l_inv_ra_rec.global_attribute_category := p_global_attribute_category;
3751     l_inv_ra_rec.global_attribute1 := p_global_attribute1;
3752     l_inv_ra_rec.global_attribute2 := p_global_attribute2;
3753     l_inv_ra_rec.global_attribute3 := p_global_attribute3;
3754     l_inv_ra_rec.global_attribute4 := p_global_attribute4;
3755     l_inv_ra_rec.global_attribute5 := p_global_attribute5;
3756     l_inv_ra_rec.global_attribute6 := p_global_attribute6;
3757     l_inv_ra_rec.global_attribute7 := p_global_attribute7;
3758     l_inv_ra_rec.global_attribute8 := p_global_attribute8;
3759     l_inv_ra_rec.global_attribute9 := p_global_attribute9;
3760     l_inv_ra_rec.global_attribute10 := p_global_attribute10;
3761     l_inv_ra_rec.global_attribute11 := p_global_attribute11;
3762     l_inv_ra_rec.global_attribute12 := p_global_attribute12;
3763     l_inv_ra_rec.global_attribute13 := p_global_attribute13;
3764     l_inv_ra_rec.global_attribute14 := p_global_attribute14;
3765     l_inv_ra_rec.global_attribute15 := p_global_attribute15;
3766     l_inv_ra_rec.global_attribute16 := p_global_attribute16;
3767     l_inv_ra_rec.global_attribute17 := p_global_attribute17;
3768     l_inv_ra_rec.global_attribute18 := p_global_attribute18;
3769     l_inv_ra_rec.global_attribute19 := p_global_attribute19;
3770     l_inv_ra_rec.global_attribute20 := p_global_attribute20;
3771     l_inv_ra_rec.comments := p_comments;
3772     l_inv_ra_rec.applied_customer_trx_line_id := p_customer_trx_line_id;
3773     l_inv_ra_rec.payment_set_id               := p_payment_set_id;
3774     /* 03-JUL-2000 jrautiai BR implementation
3775      * Populate the link_to_trx_hist_id column */
3776     l_inv_ra_rec.link_to_trx_hist_id := p_link_to_trx_hist_id;
3777     l_inv_ra_rec.application_ref_type := p_application_ref_type;
3778     l_inv_ra_rec.application_ref_id := p_application_ref_id;
3779     l_inv_ra_rec.application_ref_num := p_application_ref_num;
3780     l_inv_ra_rec.secondary_application_ref_id := p_secondary_application_ref_id;
3781     l_inv_ra_rec.application_ref_reason := p_application_ref_reason;
3782     l_inv_ra_rec.customer_reference := p_customer_reference;
3783     l_inv_ra_rec.customer_reason := p_customer_reason;
3784 
3785     /* bug 5569488, Set the confirmation flag to N for receipt where confirmation is required */
3786     IF NVL(p_called_from,'NONE') = 'AUTORECAPI' THEN
3787       l_inv_ra_rec.confirmed_flag := 'N' ;
3788     END IF ;
3789 
3790     --
3791     -- Bug 2751910 - check if any active claims for this invoice.  Only
3792     -- create a new claim if no active claims exist
3793     /* Bug 5203336 : The existence of the claim should be checked against
3794        the installment */
3795     --
3796     BEGIN
3797       SELECT payment_schedule_id
3798       INTO   l_claim_trx_ps_id
3799       FROM   ar_payment_schedules
3800       WHERE  customer_trx_id = l_inv_ra_rec.applied_customer_trx_id
3801       AND    payment_schedule_id = l_inv_ra_rec.applied_payment_schedule_id
3802       AND    NVL(active_claim_flag,'N') <> 'N'
3803       AND    ROWNUM = 1;
3804     EXCEPTION
3805       WHEN OTHERS THEN
3806         l_claim_trx_ps_id := NULL;
3807     END;
3808 
3809     IF ((l_inv_ra_rec.application_ref_type = 'CLAIM' OR
3810          l_claim_trx_ps_id IS NOT NULL) AND
3811         NVL(p_called_from,'RAPI') <> 'TRADE_MANAGEMENT') THEN
3812       OPEN c_claim_trx_details(l_inv_ra_rec.applied_customer_trx_id);
3813       FETCH c_claim_trx_details INTO l_exchange_rate_type
3814                                    , l_exchange_rate_date
3815                                    , l_exchange_rate
3816                                    , l_trx_number
3817                                    , l_cust_trx_type_id
3818                                    , l_customer_id
3819                                    , l_bill_to_site_use_id
3820                                    , l_ship_to_site_use_id
3821                                    , l_salesrep_id
3822                                    , l_legal_entity_id;
3823       CLOSE c_claim_trx_details;
3824       OPEN c_claim_rct_details(l_inv_ra_rec.cash_receipt_id);
3825       FETCH c_claim_rct_details INTO l_receipt_number;
3826       CLOSE c_claim_rct_details;
3827       IF p_amount_due_remaining IS NULL THEN
3828         OPEN c_trx_amount_due(p_invoice_ps_id);
3829         FETCH c_trx_amount_due INTO l_trx_amount_due;
3830         CLOSE c_trx_amount_due;
3831         l_claim_amount := l_trx_amount_due ;
3832       ELSE
3833         l_claim_amount := p_amount_due_remaining;
3834       END IF;
3835 
3836       IF (l_claim_trx_ps_id IS NOT NULL ) THEN
3837          l_claim_id := NULL;
3838          update_claim(
3839               p_claim_id             =>  l_claim_id
3840             , p_invoice_ps_id        =>  l_claim_trx_ps_id
3841             , p_customer_trx_id      =>  l_inv_ra_rec.applied_customer_trx_id
3842             , p_amount               =>  l_claim_amount
3843             , p_amount_applied       =>  p_amount_applied
3844             , p_apply_date           =>  p_apply_date
3845             , p_cash_receipt_id      =>  l_inv_ra_rec.cash_receipt_id
3846             , p_receipt_number       =>  l_receipt_number
3847             , p_action_type          =>  'A'
3848             , x_claim_reason_code_id =>  l_claim_reason_code_id
3849             , x_claim_reason_name    =>  l_claim_reason_name
3850             , x_claim_number         =>  l_inv_ra_rec.application_ref_num
3851             , x_return_status        =>  x_return_status
3852             , x_msg_count            =>  x_msg_count
3853             , x_msg_data             =>  x_msg_data);
3854          -- Bug 3178008 - store the claim_id that is returned
3855          l_inv_ra_rec.secondary_application_ref_id := l_claim_id;
3856 
3857 
3858       ELSIF (l_inv_ra_rec.application_ref_type = 'CLAIM' AND
3859         l_inv_ra_rec.application_ref_num IS NULL)
3860       THEN
3861  --Bug 1812328 : added parameter p_invoice_ps_id to the create_claim() procedure.
3862  --Bug 1932026 : added dff parameters
3863  --Bug 2361331 : added p_salesrep_id
3864  --Bug 5495310 : added p_apply_date
3865         create_claim(
3866               p_amount               => l_claim_amount
3867             , p_amount_applied       => p_amount_applied
3868             , p_currency_code        => p_invoice_currency_code
3869             , p_exchange_rate_type   => l_exchange_rate_type
3870             , p_exchange_rate_date   => l_exchange_rate_date
3871             , p_exchange_rate        => l_exchange_rate
3872             , p_customer_trx_id      => l_inv_ra_rec.applied_customer_trx_id
3873             , p_invoice_ps_id        => p_invoice_ps_id
3874             , p_cust_trx_type_id     => l_cust_trx_type_id
3875             , p_trx_number           => l_trx_number
3876             , p_cust_account_id      => l_customer_id
3877             , p_bill_to_site_id      => l_bill_to_site_use_id
3878             , p_ship_to_site_id      => l_ship_to_site_use_id
3879             , p_salesrep_id          => l_salesrep_id
3880             , p_customer_ref_date    => NULL
3881             , p_customer_ref_number  => p_customer_reference
3882             , p_cash_receipt_id      => l_inv_ra_rec.cash_receipt_id
3883             , p_receipt_number       => l_receipt_number
3884             , p_comments             => p_comments
3885             , p_reason_id            => to_number(p_application_ref_reason)
3886             , p_customer_reason      => p_customer_reason
3887             , p_apply_date           => p_apply_date
3888             , p_attribute_category   => p_attribute_category
3889             , p_attribute1           => p_attribute1
3890             , p_attribute2           => p_attribute2
3891             , p_attribute3           => p_attribute3
3892             , p_attribute4           => p_attribute4
3893             , p_attribute5           => p_attribute5
3894             , p_attribute6           => p_attribute6
3895             , p_attribute7           => p_attribute7
3896             , p_attribute8           => p_attribute8
3897             , p_attribute9           => p_attribute9
3898             , p_attribute10          => p_attribute10
3899             , p_attribute11          => p_attribute11
3900             , p_attribute12          => p_attribute12
3901             , p_attribute13          => p_attribute13
3902             , p_attribute14          => p_attribute14
3903             , p_attribute15          => p_attribute15
3904             , x_return_status        => x_return_status
3905             , x_msg_count            => x_msg_count
3906             , x_msg_data             => x_msg_data
3907             , x_claim_id             => l_inv_ra_rec.secondary_application_ref_id
3908             , x_claim_number         => l_inv_ra_rec.application_ref_num
3909             , x_claim_reason_name    => x_claim_reason_name
3910 	    , p_legal_entity_id      => l_legal_entity_id);
3911       END IF;
3912 
3913       /* Bug 3773036 - treat all non success errors the same and ensure
3914          errors returned from TM are displayed */
3915       IF x_return_status <> FND_API.G_RET_STS_SUCCESS
3916       THEN
3917         RAISE trade_mgt_err;
3918       END IF;
3919 
3920       IF l_inv_ra_rec.application_ref_type IS NULL THEN
3921         l_inv_ra_rec.application_ref_type := 'CLAIM';
3922       END IF;
3923     END IF;
3924 
3925     x_application_ref_id  := l_inv_ra_rec.secondary_application_ref_id;
3926     x_application_ref_num := l_inv_ra_rec.application_ref_num;
3927 
3928     -- Call the applications table handler to create the APP row.
3929     --
3930     arp_app_pkg.insert_p( l_inv_ra_rec,
3931                           l_ra_app_id );
3932 
3933     /* 5677984 - copy l_ra_app_id into l_inv_ra_rec to preserve it for
3934        later processing */
3935     l_inv_ra_rec.receivable_application_id := l_ra_app_id;
3936 
3937 arp_standard.debug('Calling arp_app_pkg.insert_p and created l_inv_ra_rec.receivable_application_id :'||
3938 l_inv_ra_rec.receivable_application_id);
3939 
3940      --apandit
3941      --Bug : 2641517 raise Apply business event.
3942        AR_BUS_EVENT_COVER.Raise_CR_Apply_Event
3943                        (l_inv_ra_rec.receivable_application_id);
3944 
3945 
3946    /* 14-APR-2000 jrautiai BR implementation
3947       If the row is not postable, accounting is not created */
3948 
3949     l_ae_doc_rec.document_type             := 'RECEIPT';
3950     l_ae_doc_rec.document_id               := l_rec_ra_rec.cash_receipt_id;
3951     l_ae_doc_rec.accounting_entity_level   := 'ONE';
3952     l_ae_doc_rec.source_table              := 'RA';
3953     l_ae_doc_rec.override_source_type      := l_source_type;
3954 
3955    IF nvl(l_rec_ra_rec.postable,'Y') = 'Y' THEN -- jrautiai BR project
3956      --
3957      --Release 11.5 VAT changes, create paired UNAPP record accounting
3958      --in ar_distributions
3959      --
3960       l_ae_doc_rec.source_id                 := l_rec_ra_rec.receivable_application_id;
3961       l_ae_doc_rec.source_id_old             := l_inv_ra_rec.receivable_application_id; --Paired application id
3962       l_ae_doc_rec.other_flag                := 'PAIR';
3963 
3964 
3965      /* A receipt application can be either line or trx level (LLCA=Y for line).
3966         However, if the discount is recoverable, we may need to override the
3967         LLCA flag to Y (when it was previously N)
3968      */
3969      IF NVL(l_from_llca_call,'N') = 'N' AND
3970         NVL(l_gt_id, 0) <> 0
3971      THEN
3972         /* overriding flag */
3973         l_from_llca_call := 'Y';
3974         IF PG_DEBUG in ('Y', 'C') THEN
3975            arp_standard.debug('from_llca_call was N (not line level), overriding to Y');
3976            arp_standard.debug('  l_gt_id = ' || l_gt_id);
3977         END IF;
3978 
3979         /* 4607809 - distribute recoverable entries before acct_main call */
3980         arp_etax_util.distribute_recoverable(
3981                l_inv_ra_rec.receivable_application_id, l_gt_id);
3982 -- was l_rec_ra_rec?
3983      END IF;
3984 
3985      IF nvl(p_called_from,'NONE') <> 'AUTORECAPI' THEN -- autorecapi bichatte project
3986 
3987       arp_acct_main.Create_Acct_Entry(
3988                 p_ae_doc_rec    => l_ae_doc_rec,
3989                 p_client_server => NULL,
3990                 p_from_llca_call =>  l_from_llca_call,
3991                 p_gt_id          =>  l_gt_id);
3992      END IF;
3993    END IF;
3994 
3995    --
3996    --Release 11.5 VAT changes, create APP record accounting
3997    --in ar_distributions
3998    --
3999     l_ae_doc_rec.source_id                 := l_inv_ra_rec.receivable_application_id;
4000     l_ae_doc_rec.source_id_old             := '';
4001     l_ae_doc_rec.other_flag                := '';
4002     l_ae_doc_rec.deferred_tax              := p_move_deferred_tax; /* jrautiai BR implementation */
4003 
4004   --Bug 1329091 - PS is updated before Accounting Engine Call
4005     l_ae_doc_rec.pay_sched_upd_yn := 'Y';
4006 
4007 --    arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
4008    IF nvl(p_called_from,'NONE') <> 'AUTORECAPI' THEN -- autorecapi bichatte project
4009 
4010       arp_acct_main.Create_Acct_Entry(
4011                 p_ae_doc_rec    => l_ae_doc_rec,
4012                 p_client_server => NULL,
4013                 p_from_llca_call =>  l_from_llca_call,
4014                 p_gt_id          =>  l_gt_id);
4015 
4016      /* Bug 4910860
4017         Check if the accounting entries balance */
4018       IF nvl(p_module_name,'RAPI') = 'RAPI' THEN
4019          l_called_from_api := 'Y';
4020       ELSE
4021          l_called_from_api := 'N';
4022       END IF;
4023 /* Start FP Bug 5594328 - Base Bug 55878178 changed the call to check_appln_balance
4024 	with check_recp_balance as there could be problem in call to appln_balance
4025 	in case of fatcor with recourse case */
4026     arp_balance_check.Check_Recp_Balance(l_rec_ra_rec.cash_receipt_id,
4027                                           l_request_id);
4028 /*
4029       arp_balance_check.Check_Appln_Balance(l_inv_ra_rec.receivable_application_id, -- APP rec_app_id
4030                                             l_rec_ra_rec.receivable_application_id,   -- UNAPP rec_app_id
4031                                             NULL,
4032                                             l_called_from_api);
4033 */ /* End FP Bug 5594328 SPDIXIT */
4034 
4035       END IF;
4036 
4037 arp_standard.debug('l_rec_ra_rec.receivable_application_id:'||l_rec_ra_rec.receivable_application_id);
4038 arp_standard.debug('l_inv_ra_rec.receivable_application_id:'||l_inv_ra_rec.receivable_application_id);
4039 
4040       IF l_rec_ra_rec.receivable_application_id IS NOT NULL THEN
4041          l_xla_ev_rec.xla_from_doc_id := l_rec_ra_rec.receivable_application_id;
4042          l_xla_ev_rec.xla_to_doc_id   := l_rec_ra_rec.receivable_application_id;
4043          l_xla_ev_rec.xla_mode        := 'O';
4044          l_xla_ev_rec.xla_call        := 'B';
4045          l_xla_ev_rec.xla_doc_table := 'APP';
4046          ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
4047       END IF;
4048 --BUG#5416481
4049       IF l_inv_ra_rec.receivable_application_id IS NOT NULL THEN
4050          l_xla_ev_rec.xla_from_doc_id := l_inv_ra_rec.receivable_application_id;
4051          l_xla_ev_rec.xla_to_doc_id   := l_inv_ra_rec.receivable_application_id;
4052          l_xla_ev_rec.xla_mode        := 'O';
4053          l_xla_ev_rec.xla_call        := 'B';
4054          l_xla_ev_rec.xla_doc_table := 'APP';
4055          ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
4056       END IF;
4057 
4058     -- Return the receivable_application_id for the APP row to the client.
4059     --
4060     p_out_rec_application_id := l_inv_ra_rec.receivable_application_id;
4061 
4062 
4063     ------------------------------------------------
4064     -- Finally update cash receipt status...
4065     --
4066     ------------------------------------------------
4067 
4068     -- First, set ar_cash_receipt record values to dummy.
4069     -- This is to distinguish between updateable NULL and NULL
4070     -- value (dummy) which means that column is not to be updated.
4071     --
4072     arp_cash_receipts_pkg.set_to_dummy(l_cr_rec);
4073 
4074     -- Get the current amount that is On Account for this receipt.
4075     -- This is used next to determine it the receipt has been
4076     -- fully applied.
4077     --
4078     select nvl(sum(ra.amount_applied),0)
4079     into   l_on_account_total
4080     from   ar_receivable_applications ra
4081     where  ra.cash_receipt_id = l_rec_ra_rec.cash_receipt_id
4082     and    ra.status IN ('ACC','OTHER ACC');
4083 
4084     -- Cash receipt must be fully applied in order to set the status
4085     -- to 'APP'.
4086     --
4087     -- We include the total amount that is On Account as this is
4088     -- not included in the Payment Schedules, Amount Due Remaining
4089     -- total for the receipt.
4090     --
4091     -- Note that we use amount applied from first, as this is the applied
4092     -- receipt amount for cross currency applications.  It is null for
4093     -- same currency, so use amount applied instead as this is both the
4094     -- the receipt and invoice amount applied.
4095     --
4096     IF (l_amount_due_remaining + l_on_account_total + nvl(p_amount_applied_from, p_amount_applied) < 0)
4097     THEN
4098       l_cr_rec.status           := 'UNAPP';
4099     ELSE
4100       l_cr_rec.status           := 'APP';
4101     END IF;
4102 
4103     l_cr_rec.cash_receipt_id  := l_rec_ra_rec.cash_receipt_id;
4104 
4105     -- Update cash receipt status.
4106     --
4107     arp_cash_receipts_pkg.update_p(
4108           l_cr_rec
4109 	, l_rec_ra_rec.cash_receipt_id);
4110 
4111     -- Update the batch status if receipt has a batch.
4112     --
4113     IF (ln_batch_id IS NOT NULL) THEN
4114       arp_rw_batches_check_pkg.update_batch_status(ln_batch_id);
4115     END IF;
4116 
4117    /*---------------------------------------------------------------------------------+
4118     |  12-JUL-2000 J Rautiainen BR Implementation                                     |
4119     |  If Bills receivable PS is closed or opened we need to create the corresponding |
4120     |  transaction history record. This logic is only for normal receipt applications |
4121     |  outside the BR remittance program, since for BR programs the record will be    |
4122     |  created by the BR API.                                                         |
4123     +---------------------------------------------------------------------------------*/
4124 
4125     IF NVL(l_old_ps_rec.class,'INV') = 'BR'
4126        AND NVL(p_called_from,'NONE') not in ('BR_REMITTED','BR_FACTORED_WITH_RECOURSE','BR_FACTORED_WITHOUT_RECOURSE') THEN
4127 
4128      /*------------------------------------+
4129       |  Create transaction history record |
4130       +------------------------------------*/
4131 
4132       ARP_PROC_TRANSACTION_HISTORY.create_trh_for_receipt_act(l_old_ps_rec,
4133                                                               l_inv_ra_rec,
4134                                                               'ARCEAPPB');
4135 
4136 
4137     END IF;
4138 
4139     -- RAM-C changes begin
4140     -- call revenue management engine's receipt analyzer for revenue related
4141     -- impact of this application.
4142 
4143     IF PG_DEBUG in ('Y', 'C') THEN
4144        arp_standard.debug(   'calling receipt_analyzer in application mode');
4145     END IF;
4146 
4147     ar_revenue_management_pvt.receipt_analyzer (
4148       p_mode => ar_revenue_management_pvt.c_receipt_application_mode,
4149       p_customer_trx_id       => l_inv_ra_rec.applied_customer_trx_id,
4150       p_acctd_amount_applied  => l_inv_ra_rec.acctd_amount_applied_to,
4151       p_exchange_rate 	      => l_exchange_rate,
4152       p_invoice_currency_code => p_invoice_currency_code,
4153       p_tax_applied 	      => l_inv_ra_rec.tax_applied,
4154       p_charges_applied       => l_inv_ra_rec.receivables_charges_applied,
4155       p_freight_applied       => l_inv_ra_rec.freight_applied,
4156       p_line_applied 	      => l_inv_ra_rec.line_applied,
4157       p_gl_date               => p_gl_date);
4158 
4159     IF PG_DEBUG in ('Y', 'C') THEN
4160        arp_standard.debug(   'returned from receipt_analyzer');
4161     END IF;
4162 
4163     -- RAM-C changes end
4164 
4165     IF PG_DEBUG in ('Y', 'C') THEN
4166        arp_standard.debug(   'arp_process_application.receipt_application()-' );
4167     END IF;
4168 
4169     EXCEPTION
4170         /* Bug 3773036 - Trade management errors treated separately to
4171            ensure calling program displays TM error */
4172         WHEN trade_mgt_err THEN
4173               IF PG_DEBUG in ('Y', 'C') THEN
4174                  arp_standard.debug('Error occured in Trade Management: ' ||
4175 		    'EXCEPTION: arp_process_application.receipt_application' );
4176               END IF;
4177         WHEN OTHERS THEN
4178               IF PG_DEBUG in ('Y', 'C') THEN
4179                  arp_standard.debug(
4180 		    'EXCEPTION: arp_process_application.receipt_application' );
4181               END IF;
4182               RAISE;
4183 END receipt_application;
4184 --
4185 /*===========================================================================+
4186  | PROCEDURE                                                                 |
4187  |    validate_receipt_appln_args                                            |
4188  |                                                                           |
4189  | DESCRIPTION                                                               |
4190  |    Validate arguments passed to receipt_application procedure.            |
4191  |                                                                           |
4192  | SCOPE - PRIVATE                                                           |
4193  |                                                                           |
4194  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
4195  |                                                                           |
4196  | ARGUMENTS  : IN:                                                          |
4197  |                 p_receipt_ps_id - PS Id of the receipt                    |
4198  |                 p_invoice_ps_id - PS Id of the transaction                |
4199  |                 p_amount_applied - TO amount                              |
4200  |                 p_amount_applied_from - FROM amount                       |
4201  | 		   p_trans_to_receipt_rate - Cross currency rate             |
4202  |                 p_receipt_currency_code - Currency of the receipt         |
4203  |                 p_invoice_currency_code - Currency of the transaction     |
4204  |                 p_earned_discount_taken - Earned Discount taken           |
4205  |                 p_unearned_discount_taken - UnEarned Discount taken       |
4206  |                 p_apply_date - Application date                           |
4207  |                 p_gl_date    - GL Date                                    |
4208  |              OUT:                                                         |
4209  |                                                                           |
4210  | RETURNS    : NONE                                                         |
4211  |                                                                           |
4212  | NOTES                                                                     |
4213  |                                                                           |
4214  | MODIFICATION HISTORY - Created by Ganesh Vaidee - 04/25/95                |
4215  |
4216  | 17-Jul-97	K.Lawrance	Release 11.
4217  |				Added parameters to check for cross currency
4218  |				applications and the population of the amount
4219  |				applied from and trans to receipt rate.
4220  |
4221  +===========================================================================*/
4222 PROCEDURE  validate_receipt_appln_args(
4223 	p_receipt_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
4224 	p_invoice_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
4225         p_amount_applied IN ar_receivable_applications.amount_applied%TYPE,
4226         p_amount_applied_from IN ar_receivable_applications.amount_applied_from%TYPE,
4227         p_trans_to_receipt_rate IN ar_receivable_applications.trans_to_receipt_rate%TYPE,
4228         p_invoice_currency_code IN ar_payment_schedules.invoice_currency_code%TYPE,
4229         p_receipt_currency_code IN ar_cash_receipts.currency_code%TYPE,
4230         p_earned_discount_taken IN ar_receivable_applications.earned_discount_taken%TYPE,
4231         p_unearned_discount_taken IN ar_receivable_applications.unearned_discount_taken%TYPE,
4232         p_apply_date IN ar_receivable_applications.apply_date%TYPE,
4233 	p_gl_date IN ar_receivable_applications.gl_date%TYPE ) IS
4234 BEGIN
4235     IF PG_DEBUG in ('Y', 'C') THEN
4236        arp_standard.debug(   'arp_process_application.validate_receipt_appln_args()+' );
4237     END IF;
4238 
4239     -- Check for mandatory parameters.
4240     IF ( p_receipt_ps_id IS NULL OR
4241          p_invoice_ps_id IS NULL OR
4242 	 p_apply_date    IS NULL OR
4243          p_gl_date       IS NULL ) THEN
4244          FND_MESSAGE.set_name ('AR', 'AR_ARGUEMENTS_FAIL' );
4245          APP_EXCEPTION.raise_exception;
4246     END IF;
4247     --
4248     IF ( p_amount_applied IS NULL AND
4249          p_earned_discount_taken IS NULL AND
4250 	 p_unearned_discount_taken IS NULL ) THEN
4251          FND_MESSAGE.set_name ('AR', 'AR_ARGUEMENTS_FAIL' );
4252          APP_EXCEPTION.raise_exception;
4253     END IF;
4254 
4255     ------------------------------------------------------------------
4256     -- Check that if the currency code of the receipt is different to
4257     -- that of the invoice, i.e. cross currency application, that the
4258     -- amount_applied_from and trans_to_receipt_rate are both
4259     -- populated.
4260     ------------------------------------------------------------------
4261     IF ( p_receipt_currency_code <> p_invoice_currency_code
4262        AND (SIGN(p_invoice_ps_id) <> -1) AND
4263          ( p_amount_applied_from is NULL OR
4264            p_trans_to_receipt_rate is NULL ) ) THEN
4265          FND_MESSAGE.set_name ('AR', 'AR_ARGUEMENTS_FAIL' );
4266          APP_EXCEPTION.raise_exception;
4267     END IF;
4268 
4269 
4270     IF PG_DEBUG in ('Y', 'C') THEN
4271        arp_standard.debug(   'arp_process_application.validate_receipt_appln_args()-' );
4272     END IF;
4273 
4274     EXCEPTION
4275          WHEN OTHERS THEN
4276            IF PG_DEBUG in ('Y', 'C') THEN
4277               arp_standard.debug(  'EXCEPTION: arp_process_application.validate_receipt_appln_args' );
4278            END IF;
4279            RAISE;
4280 END validate_receipt_appln_args;
4281 --
4282 /*===========================================================================+
4283  | PROCEDURE                                                                 |
4284  |    cm_application                                                         |
4285  |                                                                           |
4286  | DESCRIPTION                                                               |
4287  |    Do all actions neccessary to update PS rows and insert APP             |
4288  |    row in RA table when a CM is applied to an invoice.                    |
4289  |                                                                           |
4290  | SCOPE - PUBLIC                                                            |
4291  |                                                                           |
4292  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
4293  |      arp_ps_util.update_invoice_related_columns                           |
4294  |      arp_ps_util.update_cm_related_columns                                |
4295  |                                                                           |
4296  | ARGUMENTS  : IN:                                                          |
4297  |                 p_cm_ps_id      - PS Id of the Credit Memo                |
4298  |                 p_invoice_ps_id - PS Id of the transaction                |
4299  |                 p_amount_applied - TO amount                              |
4300  |                 p_apply_date - Application date                           |
4301  |                 p_gl_date    - GL Date                                    |
4302  |                 p_ussgl_transaction_code - USSGL transaction code         |
4303  |                 p_customer_trx_line_id - Line of the transaction applied  |
4304  |                                                                           |
4305  |                 OTHER DESCRIPTIVE FLEX columns                            |
4306  |      	   p_module_name  - Name of the module that called this      |
4307  |				    procedure   			     |
4308  |      	   p_module_version  - Version of the module that called this|
4309  |			               procedure                             |
4310  |              OUT:                                                         |
4311  |                 p_receivable_application_id - Identifier of RA            |
4312  |                 p_acctd_amount_applied_from - Rounded acctd FROM amount   |
4313  |                 p_acctd_amount_applied_to - Rounded acctd TO amount       |
4314  |                                                                           |
4315  | RETURNS    : NONE                                                         |
4316  |                                                                           |
4317  | NOTES  -                                        	                     |
4318  |                                                                           |
4319  | MODIFICATION HISTORY                                                      |
4320  | 08/29/95	Ganesh Vaidee	Created                                      |
4321  | 02/06/1996	Harri Kaukovuo	Added new parameters                         |
4322  |					p_customer_trx_line_id               |
4323  |					p_out_rec_application_id             |
4324  |  10/17/1996  Karen Lawrance  Added code to prevent the creation of more   |
4325  |                              than one application against the same receipt|
4326  |                              and invoice.                                 |
4327  |  05/06/1997  Karen Lawrance  Bug fix #481761.  Fixed application rule.    |
4328  |  07/25/1997	Karen Lawrance	Release 11.                                  |
4329  |                              Added acctd amount from and to as OUT NOCOPY        |
4330  |				parameters to be consistent with receipt     |
4331  |				applications.                                |
4332  |				Also cleaned up code and added some more     |
4333  |				comments.                                    |
4334  |  08/21/1997	Tasman Tang	Added global_attribute_category,	     |
4335  |				global_attribute[1-20] for global            |
4336  |				descriptive flexfield			     |
4337  |  13-Jun-00  Satheesh Nambiar Bug 1329091 - Passing a new parameter
4338  |                              pay_sched_upd_yn to accounting engine
4339  |                              to acknowldge PS is updated.
4340  |  03-Sep-02  Debbie Jancis  	Added call to mrc_engine3 for processing
4341  |                              mrc data for ar_receivable_applications
4342  |  28-Apr-03   Rahna Kader     Bug 1659928: Now the program checks for      |
4343  | 				over application before the applications     |
4344  | 				are saved                                    |
4345  |  12-Mar-04   Bhushan Dhotkar Bug 2662270: Added a column p_comments
4346  +===========================================================================*/
4347 PROCEDURE cm_application(
4348 	p_cm_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
4349 	p_invoice_ps_id	IN ar_payment_schedules.payment_schedule_id%TYPE,
4350         p_amount_applied IN ar_receivable_applications.amount_applied%TYPE,
4351         p_apply_date IN ar_receivable_applications.apply_date%TYPE,
4352 	p_gl_date IN ar_receivable_applications.gl_date%TYPE,
4353 	p_ussgl_transaction_code IN ar_receivable_applications.ussgl_transaction_code%TYPE,
4354 	p_attribute_category IN ar_receivable_applications.attribute_category%TYPE,
4355 	p_attribute1 IN ar_receivable_applications.attribute1%TYPE,
4356 	p_attribute2 IN ar_receivable_applications.attribute2%TYPE,
4357 	p_attribute3 IN ar_receivable_applications.attribute3%TYPE,
4358 	p_attribute4 IN ar_receivable_applications.attribute4%TYPE,
4359 	p_attribute5 IN ar_receivable_applications.attribute5%TYPE,
4360 	p_attribute6 IN ar_receivable_applications.attribute6%TYPE,
4361 	p_attribute7 IN ar_receivable_applications.attribute7%TYPE,
4362 	p_attribute8 IN ar_receivable_applications.attribute8%TYPE,
4363 	p_attribute9 IN ar_receivable_applications.attribute9%TYPE,
4364 	p_attribute10 IN ar_receivable_applications.attribute10%TYPE,
4365 	p_attribute11 IN ar_receivable_applications.attribute11%TYPE,
4366 	p_attribute12 IN ar_receivable_applications.attribute12%TYPE,
4367 	p_attribute13 IN ar_receivable_applications.attribute13%TYPE,
4368 	p_attribute14 IN ar_receivable_applications.attribute14%TYPE,
4369 	p_attribute15 IN ar_receivable_applications.attribute15%TYPE,
4370         p_global_attribute_category IN ar_receivable_applications.global_attribute_category%TYPE,
4371         p_global_attribute1 IN ar_receivable_applications.global_attribute1%TYPE,
4372         p_global_attribute2 IN ar_receivable_applications.global_attribute2%TYPE,
4373         p_global_attribute3 IN ar_receivable_applications.global_attribute3%TYPE,
4374         p_global_attribute4 IN ar_receivable_applications.global_attribute4%TYPE,
4375         p_global_attribute5 IN ar_receivable_applications.global_attribute5%TYPE,
4376         p_global_attribute6 IN ar_receivable_applications.global_attribute6%TYPE,
4377         p_global_attribute7 IN ar_receivable_applications.global_attribute7%TYPE,
4378         p_global_attribute8 IN ar_receivable_applications.global_attribute8%TYPE,
4379         p_global_attribute9 IN ar_receivable_applications.global_attribute9%TYPE,
4380         p_global_attribute10 IN ar_receivable_applications.global_attribute10%TYPE,
4381         p_global_attribute11 IN ar_receivable_applications.global_attribute11%TYPE,
4382         p_global_attribute12 IN ar_receivable_applications.global_attribute12%TYPE,
4383         p_global_attribute13 IN ar_receivable_applications.global_attribute13%TYPE,
4384         p_global_attribute14 IN ar_receivable_applications.global_attribute14%TYPE,
4385         p_global_attribute15 IN ar_receivable_applications.global_attribute15%TYPE,
4386         p_global_attribute16 IN ar_receivable_applications.global_attribute16%TYPE,
4387         p_global_attribute17 IN ar_receivable_applications.global_attribute17%TYPE,
4388         p_global_attribute18 IN ar_receivable_applications.global_attribute18%TYPE,
4389         p_global_attribute19 IN ar_receivable_applications.global_attribute19%TYPE,
4390         p_global_attribute20 IN ar_receivable_applications.global_attribute20%TYPE,
4391         p_customer_trx_line_id IN NUMBER,
4392         p_comments IN ar_receivable_applications.comments%TYPE DEFAULT NULL,  --bug2662270
4393         p_module_name IN VARCHAR2,
4394         p_module_version IN VARCHAR2,
4395         -- OUT NOCOPY
4396         p_out_rec_application_id OUT NOCOPY NUMBER,
4397         p_acctd_amount_applied_from OUT NOCOPY ar_receivable_applications.acctd_amount_applied_from%TYPE,
4398         p_acctd_amount_applied_to OUT NOCOPY ar_receivable_applications.acctd_amount_applied_to%TYPE) IS
4399 
4400 l_inv_ra_rec     ar_receivable_applications%ROWTYPE;
4401 l_cm_ps_rec      ar_payment_schedules%ROWTYPE;
4402 l_ae_doc_rec     ae_doc_rec_type;
4403    l_source_type                ar_distributions.source_type%TYPE; /* jrautiai BR implementation */
4404 l_flag		 char; /* added for bug 2318048 */
4405 
4406    /* Bug fix 1659928 */
4407    l_inv_bal_amount             NUMBER;
4408    l_inv_orig_amount            NUMBER;
4409    l_allow_over_application     VARCHAR2(1);
4410    l_effective_amount_applied   NUMBER;
4411 
4412   --Bug#2750340
4413   l_xla_ev_rec      arp_xla_events.xla_events_type;
4414   l_xla_doc_table   VARCHAR2(20);
4415 
4416 BEGIN
4417     IF PG_DEBUG in ('Y', 'C') THEN
4418        arp_standard.debug(   'arp_process_application.cm_application()+' );
4419     END IF;
4420 
4421     -- Output IN parameters
4422     IF PG_DEBUG in ('Y', 'C') THEN
4423        arp_standard.debug(   'CM PS Id.               : '||TO_CHAR( p_cm_ps_id ) );
4424        arp_standard.debug(   'Invoice PS Id.          : '||TO_CHAR( p_invoice_ps_id ) );
4425        arp_standard.debug(   'Amount Applied          : '||TO_CHAR( p_amount_applied ) );
4426        arp_standard.debug(   'Gl Date                 : '||TO_CHAR( p_gl_date ) );
4427        arp_standard.debug(   'Apply Date              : '||TO_CHAR( p_apply_date ) );
4428        arp_standard.debug(   'Customer_trx_line_id    : '||TO_CHAR(p_customer_trx_line_id));
4429     END IF;
4430 
4431     -----------------------------------------------------
4432     --  KML 10/17/1996
4433     --  Prevent the creation of more than one application
4434     --  against the same credit memo and invoice.
4435     --  Bug #505538
4436     --  Modified the condition to allow application for
4437     --  invoice applied to same Credit Memo for different
4438     --  invoice lines.
4439     -----------------------------------------------------
4440     DECLARE
4441       l_found   varchar2(1) := 'N';
4442     BEGIN
4443       IF p_customer_trx_line_id IS NULL THEN
4444         select 'Y'
4445         into   l_found
4446         from   ar_receivable_applications rap
4447         where  rap.payment_schedule_id = p_cm_ps_id
4448         and    rap.applied_payment_schedule_id = p_invoice_ps_id
4449         and    rap.applied_customer_trx_line_id IS NULL /* Bug fix 1659928 */
4450         and    rap.display = 'Y'
4451         and    rap.status = 'APP';
4452       ELSE
4453         select 'Y'
4454         into   l_found
4455         from   ar_receivable_applications rap
4456         where  rap.payment_schedule_id = p_cm_ps_id
4457         and    rap.applied_payment_schedule_id = p_invoice_ps_id
4458         and    rap.applied_customer_trx_line_id = p_customer_trx_line_id
4459         and    rap.display = 'Y'
4460         and    rap.status = 'APP';
4461       END IF;
4462 
4463       if l_found = 'Y' then
4464         raise too_many_rows;
4465       end if;
4466 
4467     EXCEPTION
4468       when no_data_found then
4469         null;
4470       when too_many_rows then
4471         FND_MESSAGE.set_name ('AR', 'AR_RW_PAID_INVOICE_TWICE' );
4472         APP_EXCEPTION.raise_exception;
4473     END;
4474 
4475      /* Bug fix 1659928
4476        Check if the payment schedule of this transaction is over applied and
4477        the transaction type does not allow overapplication */
4478 
4479        select   ps.amount_due_remaining,ps.amount_due_original,ctt.allow_overapplication_flag
4480        into     l_inv_bal_amount, l_inv_orig_amount, l_allow_over_application
4481        from     ra_cust_trx_types ctt, ar_payment_schedules ps
4482        where    ps.payment_schedule_id = p_invoice_ps_id
4483        and      ps.cust_trx_type_id = ctt.cust_trx_type_id;
4484 
4485        l_effective_amount_applied := NVL(p_amount_applied,0) ;
4486 
4487        IF l_allow_over_application ='N'
4488         AND arp_deduction.overapplication_indicator(l_inv_orig_amount,
4489                                                     l_inv_bal_amount,
4490                                                     l_effective_amount_applied) ='Y' THEN
4491           FND_MESSAGE.set_name ('AR', 'AR_CKAP_OVERAPP' );
4492           APP_EXCEPTION.raise_exception;
4493        END IF;
4494 
4495     /* End bug fix 1659928 */
4496 
4497     -- Validate the parameters that have been passed to the procedure.
4498     --
4499     IF ( p_module_name IS NOT NULL AND
4500          p_module_version IS NOT NULL ) THEN
4501          validate_cm_appln_args( p_cm_ps_id,
4502                                  p_invoice_ps_id,
4503                                  p_amount_applied,
4504         			 p_apply_date,
4505                                  p_gl_date);
4506     END IF;
4507 
4508     --------------------------------------------------------------------------
4509     -- Process the Applied Transaction...
4510     --
4511     --------------------------------------------------------------------------
4512 
4513     -- Populate CC ID columns by selecting from ra_cust_trx_line_gl_dist table
4514     -- and Payment Schedule table.
4515     --
4516 
4517     /* 14-APR-2000 jrautiai BR implementation
4518      * Moved into a procedure for BR transactions has the accounting in ar_distributions table.
4519      * instead of ra_cust_trx_line_gl_dist */
4520 
4521     ARP_PROCESS_APPLICATION.fetch_app_ccid(p_invoice_ps_id,
4522                                            l_inv_ra_rec.applied_customer_trx_id,
4523                                            l_inv_ra_rec.code_combination_id,
4524                                            l_source_type);
4525 
4526     arp_ps_util.update_invoice_related_columns(
4527                 'CM',
4528                 p_invoice_ps_id,
4529                 p_amount_applied,
4530                 NULL,   /* Earned discount taken */
4531                 NULL,   /* UnEarned discount taken */
4532                 p_apply_date,
4533                 p_gl_date,
4534                 l_inv_ra_rec.acctd_amount_applied_to,
4535                 l_inv_ra_rec.acctd_earned_discount_taken,
4536                 l_inv_ra_rec.acctd_unearned_discount_taken,
4537                 l_inv_ra_rec.line_applied,
4538                 l_inv_ra_rec.tax_applied,
4539                 l_inv_ra_rec.freight_applied,
4540                 l_inv_ra_rec.receivables_charges_applied,
4541                 l_inv_ra_rec.line_ediscounted,
4542                 l_inv_ra_rec.tax_ediscounted,
4543                 l_inv_ra_rec.freight_ediscounted,
4544                 l_inv_ra_rec.charges_ediscounted,
4545                 l_inv_ra_rec.line_uediscounted,
4546                 l_inv_ra_rec.tax_uediscounted,
4547                 l_inv_ra_rec.freight_uediscounted,
4548                 l_inv_ra_rec.charges_uediscounted,
4549                 l_inv_ra_rec.rule_set_id,
4550                 NULL_VAR );     /* NULL modified to NULL_VAR  for bug 460959 - Oracle 8 */
4551 
4552     -- This is passed back to the client as the true acctd amount (calculated
4553     -- in the payment schedule utility procedure).
4554     p_acctd_amount_applied_to := l_inv_ra_rec.acctd_amount_applied_to;
4555 
4556     --------------------------------------------------------------------------
4557     -- Process the On-Account Credit...
4558     --
4559     --------------------------------------------------------------------------
4560 
4561     -- Get customer_trx_id of CM, from PS table. Pass the selected row
4562     -- to update_cm_related_columns procedure.
4563     --
4564     SELECT *
4565     INTO   l_cm_ps_rec
4566     FROM   ar_payment_schedules
4567     WHERE  payment_schedule_id = p_cm_ps_id;
4568     --
4569     arp_ps_util.update_cm_related_columns(
4570                 p_cm_ps_id,
4571                 p_amount_applied,
4572                 l_inv_ra_rec.line_applied,
4573                 l_inv_ra_rec.tax_applied,
4574                 l_inv_ra_rec.freight_applied,
4575                 l_inv_ra_rec.receivables_charges_applied,
4576                 p_apply_date,
4577                 p_gl_date,
4578                 l_inv_ra_rec.acctd_amount_applied_from,
4579                 l_cm_ps_rec );
4580 
4581 /* Added the following code for bug 2318048. If CM is postable then only make
4582    the APP row postable */
4583 
4584     select NVL(ctt.post_to_gl,'N') into l_flag
4585     from   ra_cust_trx_types ctt,
4586     	   ar_payment_schedules ps
4587     where  ctt.cust_trx_type_id = ps.cust_trx_type_id
4588     and    ps.payment_schedule_id = p_cm_ps_id;
4589 
4590     If l_flag = 'Y' then
4591 	l_inv_ra_rec.postable := 'Y';
4592     else
4593     	l_inv_ra_rec.postable := 'N';
4594     End if;
4595 
4596     -- This is passed back to the client as the true acctd amount (calculated
4597     -- in the payment schedule utility procedure).
4598     p_acctd_amount_applied_from := l_inv_ra_rec.acctd_amount_applied_from;
4599 
4600     l_inv_ra_rec.customer_trx_id := l_cm_ps_rec.customer_trx_id;
4601     l_inv_ra_rec.payment_schedule_id := p_cm_ps_id;
4602     l_inv_ra_rec.applied_payment_schedule_id := p_invoice_ps_id;
4603     l_inv_ra_rec.amount_applied := p_amount_applied;
4604     l_inv_ra_rec.status := 'APP';
4605     l_inv_ra_rec.application_type := 'CM';
4606 
4607     l_inv_ra_rec.application_rule := '75';
4608     l_inv_ra_rec.program_id     := -100105;
4609 
4610     l_inv_ra_rec.apply_date := p_apply_date;
4611     l_inv_ra_rec.gl_date := p_gl_date;
4612     l_inv_ra_rec.posting_control_id := -3;
4613     l_inv_ra_rec.display := 'Y';
4614     l_inv_ra_rec.ussgl_transaction_code := p_ussgl_transaction_code;
4615     l_inv_ra_rec.attribute_category := p_attribute_category;
4616     l_inv_ra_rec.attribute1 := p_attribute1;
4617     l_inv_ra_rec.attribute2 := p_attribute2;
4618     l_inv_ra_rec.attribute3 := p_attribute3;
4619     l_inv_ra_rec.attribute4 := p_attribute4;
4620     l_inv_ra_rec.attribute5 := p_attribute5;
4621     l_inv_ra_rec.attribute6 := p_attribute6;
4622     l_inv_ra_rec.attribute7 := p_attribute7;
4623     l_inv_ra_rec.attribute8 := p_attribute8;
4624     l_inv_ra_rec.attribute9 := p_attribute9;
4625     l_inv_ra_rec.attribute10 := p_attribute10;
4626     l_inv_ra_rec.attribute11 := p_attribute11;
4627     l_inv_ra_rec.attribute12 := p_attribute12;
4628     l_inv_ra_rec.attribute13 := p_attribute13;
4629     l_inv_ra_rec.attribute14 := p_attribute14;
4630     l_inv_ra_rec.attribute15 := p_attribute15;
4631     l_inv_ra_rec.global_attribute_category := p_global_attribute_category;
4632     l_inv_ra_rec.global_attribute1 := p_global_attribute1;
4633     l_inv_ra_rec.global_attribute2 := p_global_attribute2;
4634     l_inv_ra_rec.global_attribute3 := p_global_attribute3;
4635     l_inv_ra_rec.global_attribute4 := p_global_attribute4;
4636     l_inv_ra_rec.global_attribute5 := p_global_attribute5;
4637     l_inv_ra_rec.global_attribute6 := p_global_attribute6;
4638     l_inv_ra_rec.global_attribute7 := p_global_attribute7;
4639     l_inv_ra_rec.global_attribute8 := p_global_attribute8;
4640     l_inv_ra_rec.global_attribute9 := p_global_attribute9;
4641     l_inv_ra_rec.global_attribute10 := p_global_attribute10;
4642     l_inv_ra_rec.global_attribute11 := p_global_attribute11;
4643     l_inv_ra_rec.global_attribute12 := p_global_attribute12;
4644     l_inv_ra_rec.global_attribute13 := p_global_attribute13;
4645     l_inv_ra_rec.global_attribute14 := p_global_attribute14;
4646     l_inv_ra_rec.global_attribute15 := p_global_attribute15;
4647     l_inv_ra_rec.global_attribute16 := p_global_attribute16;
4648     l_inv_ra_rec.global_attribute17 := p_global_attribute17;
4649     l_inv_ra_rec.global_attribute18 := p_global_attribute18;
4650     l_inv_ra_rec.global_attribute19 := p_global_attribute19;
4651     l_inv_ra_rec.global_attribute20 := p_global_attribute20;
4652     l_inv_ra_rec.applied_customer_trx_line_id := p_customer_trx_line_id;
4653     l_inv_ra_rec.comments := p_comments; --Bug 2662270
4654     --------------------------------------------------------------------------
4655     -- Create the APP row in receivable applications.
4656     --
4657     --------------------------------------------------------------------------
4658     arp_app_pkg.insert_p( l_inv_ra_rec,
4659                           l_inv_ra_rec.receivable_application_id );
4660 
4661     IF l_inv_ra_rec.receivable_application_id IS NOT NULL THEN
4662       l_xla_ev_rec.xla_from_doc_id := l_inv_ra_rec.receivable_application_id;
4663       l_xla_ev_rec.xla_to_doc_id   := l_inv_ra_rec.receivable_application_id;
4664       l_xla_ev_rec.xla_mode        := 'O';
4665       l_xla_ev_rec.xla_call        := 'B';
4666       l_xla_ev_rec.xla_doc_table := 'APP';
4667       ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
4668     END IF;
4669     -----------------------------------------------------------------------
4670     -- Process MRC data
4671     -----------------------------------------------------------------------
4672 
4673 --    ar_mrc_engine3.cm_application(
4674 --                     p_cm_ps_id      => p_cm_ps_id,
4675 --                     p_invoice_ps_id => p_invoice_ps_id,
4676 --                     p_inv_ra_rec    => l_inv_ra_rec,
4677 --                     p_ra_id         => l_inv_ra_rec.receivable_application_id);
4678 
4679     --apandit
4680     --Bug : 2641517 raise business event.
4681     AR_BUS_EVENT_COVER.Raise_CM_Apply_Event(l_inv_ra_rec.receivable_application_id);
4682 
4683    --
4684    --Release 11.5 VAT changes, create APP record accounting
4685    --in ar_distributions
4686    --
4687     l_ae_doc_rec.document_type             := 'CREDIT_MEMO';
4688     l_ae_doc_rec.document_id               := l_inv_ra_rec.customer_trx_id;
4689     l_ae_doc_rec.accounting_entity_level   := 'ONE';
4690     l_ae_doc_rec.source_table              := 'RA';
4691     l_ae_doc_rec.source_id                 := l_inv_ra_rec.receivable_application_id;
4692     l_ae_doc_rec.source_id_old             := '';
4693     l_ae_doc_rec.other_flag                := '';
4694   --Bug 1329091 - PS is updated before Accounting Engine Call
4695     l_ae_doc_rec.pay_sched_upd_yn := 'Y';
4696 
4697     arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
4698 
4699     /* Bug 4910860
4700        Check if the accounting entries balance */
4701     arp_balance_check.Check_Appln_Balance(l_inv_ra_rec.receivable_application_id, -- APP record
4702                                           NULL,
4703                                           'N' );  -- No API for CM application as of now
4704 
4705     -- Return the new receivable_application_id
4706     p_out_rec_application_id := l_inv_ra_rec.receivable_application_id;
4707 
4708     IF PG_DEBUG in ('Y', 'C') THEN
4709        arp_standard.debug(   'arp_process_application.cm_application()-' );
4710     END IF;
4711 
4712     EXCEPTION
4713         WHEN OTHERS THEN
4714               IF PG_DEBUG in ('Y', 'C') THEN
4715                  arp_standard.debug(
4716 		    'EXCEPTION: arp_process_application.cm_application' );
4717               END IF;
4718               RAISE;
4719 END cm_application;
4720 --
4721 /*===========================================================================+
4722  | PROCEDURE                                                                 |
4723  |    validate_cm_appln_args                                                 |
4724  |                                                                           |
4725  | DESCRIPTION                                                               |
4726  |    Validate arguments passed to cm_application                            |
4727  |                                                                           |
4728  | SCOPE - PRIVATE                                                           |
4729  |                                                                           |
4730  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
4731  |                                                                           |
4732  | ARGUMENTS  : IN:                                                          |
4733  |                 p_cm_ps_id      - Receipt PS Id.                          |
4734  |                 p_invoice_ps_id - Invoice PS Id.                          |
4735  |                 p_amount_applied - Input amount applied                   |
4736  |                 p_gl_date         - Gl Date                               |
4737  |                 p_apply_date - Application date                           |
4738  |              OUT:                                                         |
4739  |                                                                           |
4740  | RETURNS    : NONE                                                         |
4741  |                                                                           |
4742  | NOTES                                                                     |
4743  |                                                                           |
4744  | MODIFICATION HISTORY - Created by Ganesh Vaidee - 04/25/95                |
4745  |                                                                           |
4746  +===========================================================================*/
4747 PROCEDURE  validate_cm_appln_args(
4748 	p_cm_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
4749 	p_invoice_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
4750         p_amount_applied IN
4751                 ar_receivable_applications.amount_applied%TYPE,
4752         p_apply_date IN ar_receivable_applications.apply_date%TYPE,
4753 	p_gl_date IN ar_receivable_applications.gl_date%TYPE ) IS
4754 BEGIN
4755     IF PG_DEBUG in ('Y', 'C') THEN
4756        arp_standard.debug(
4757 	      'arp_process_application.validate_cm_appln_args()+' );
4758     END IF;
4759     --
4760     IF ( p_cm_ps_id IS NULL OR p_invoice_ps_id IS NULL OR
4761 	 p_apply_date IS NULL OR p_gl_date IS NULL OR
4762          p_amount_applied IS NULL ) THEN
4763          FND_MESSAGE.set_name ('AR', 'AR_ARGUEMENTS_FAIL' );
4764          APP_EXCEPTION.raise_exception;
4765     END IF;
4766     --
4767     IF PG_DEBUG in ('Y', 'C') THEN
4768        arp_standard.debug(
4769 	      'arp_process_application.validate_cm_appln_args()-' );
4770     END IF;
4771     EXCEPTION
4772          WHEN OTHERS THEN
4773            IF PG_DEBUG in ('Y', 'C') THEN
4774               arp_standard.debug(
4775                 'EXCEPTION: arp_process_application.validate_cm_appln_args' );
4776            END IF;
4777               RAISE;
4778 END validate_cm_appln_args;
4779 --
4780 /*===========================================================================+
4781  | PROCEDURE                                                                 |
4782  |    on_account_receipts                                                    |
4783  |                                                                           |
4784  | DESCRIPTION                                                               |
4785  |    Do all actions neccessary to insert rows into AR_RA table during       |
4786  |    ON-ACCOUNT receipt insertion. No PS table row is updated, However      |
4787  |    2 RA rows are inserted - One as an UNAPP row and another as 'ACC' row  |
4788  |                                                                           |
4789  | SCOPE - PUBLIC                                                            |
4790  |                                                                           |
4791  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
4792  |         ARPCURR.functional_amount - Get the acctd amount of amount applied|
4793  |         arp_ps_pkg.fetch_p - Fetch a PS row                               |
4794  |         arp_app_pkg.insert_p - Insert a row into RA table                 |
4795  |                                                                           |
4796  | ARGUMENTS  : IN:                                                          |
4797  |                 p_ps_id - PS id of the receipt                            |
4798  |                 p_amount_applied - Input amount applied                   |
4799  |                 p_apply_date - Application date                           |
4800  |                 p_gl_date    - Gl Date                                    |
4801  |                 p_ussgl_transaction_code - USSGL transaction code         |
4802  |                 OTHER DESCRIPTIVE FLEX columns                            |
4803  |                 p_module_name  - Name of the module that called this      |
4804  |                                  procedure                                |
4805  |                 p_module_version  - Version of the module that called this|
4806  |                                  procedure                                |
4807  |              OUT:                                                         |
4808  |		   p_out_rec_application_id                                  |
4809  |				Returned receivable application id           |
4810  | RETURNS    : NONE                                                         |
4811  |                                                                           |
4812  | NOTES  -                                                                  |
4813  |                                                                           |
4814  | MODIFICATION HISTORY                                                      |
4815  | 08/29/1995	Ganesh Vaidee	Created                                      |
4816  | 05/03/1996	Harri Kaukovuo	Added OUT NOCOPY parameter p_out_rec_application_id |
4817  | 05/06/1996	Harri Kaukovuo	Added logics to update batch status          |
4818  | 10/31/1996   Karen Lawrance  Bug fix #414626.  Added code to update       |
4819  |                              the receipt status if the on account         |
4820  |                              application fully applies the receipt.       |
4821  | 08/21/1997	Tasman Tang	Added global_attribute_category,	     |
4822  |				global_attribute[1-20] for global  	     |
4823  |				descriptive flexfield			     |
4824  | 03/05/1998   Guat Eng Tan    Bug fix #627262.  In the call to             |
4825  |                              ARPCURR.functional_amount, replace l_ps_rec. |
4826  |                              invoice_currency_code with functional_curr.  |
4827  | 07/16/1998   Karen Murphy    Bug fix 634464.  Modified the code that works|
4828  |                              out NOCOPY the status for the Receipt (APP or UNAPP)|
4829  |                              Now includes the total On Account amount as  |
4830  |                              this is not included in the Pay Sched, Amt   |
4831  |                              Due Rem total.                               |
4832  | 04-JAN-02    VERAO           Bug 2047229 : added p_comments               |
4833  | 16-JAN-02    V Crisostomo	Bug 2184812 : changes done in 2047229 added  |
4834  |				parameter p_comments, but did not provide    |
4835  |				default value, hence all code calling this   |
4836  |				procedure and not passing comment is failing |
4837  |				modify code to DEFAULT NULL	   	     |
4838  | 04-Sep-02    Debbie Jancis   Added calls to mrc engine 3 for processing   |
4839  |                              inserts to ar_receivable_applications        |
4840  | 28-JUL-2003  Jon Beckett     Bug 2821139 - added p_customer_reason.       |
4841  +===========================================================================*/
4842 PROCEDURE on_account_receipts(
4843         p_receipt_ps_id   IN ar_payment_schedules.payment_schedule_id%TYPE,
4844         p_amount_applied IN
4845                 ar_receivable_applications.amount_applied%TYPE,
4846         p_apply_date IN ar_receivable_applications.apply_date%TYPE,
4847         p_gl_date IN ar_receivable_applications.gl_date%TYPE,
4848         p_ussgl_transaction_code IN
4849                 ar_receivable_applications.ussgl_transaction_code%TYPE,
4850         p_attribute_category IN
4851                 ar_receivable_applications.attribute_category%TYPE,
4852         p_attribute1 IN ar_receivable_applications.attribute1%TYPE,
4853         p_attribute2 IN ar_receivable_applications.attribute2%TYPE,
4854         p_attribute3 IN ar_receivable_applications.attribute3%TYPE,
4855         p_attribute4 IN ar_receivable_applications.attribute4%TYPE,
4856         p_attribute5 IN ar_receivable_applications.attribute5%TYPE,
4857         p_attribute6 IN ar_receivable_applications.attribute6%TYPE,
4858         p_attribute7 IN ar_receivable_applications.attribute7%TYPE,
4859         p_attribute8 IN ar_receivable_applications.attribute8%TYPE,
4860         p_attribute9 IN ar_receivable_applications.attribute9%TYPE,
4861         p_attribute10 IN ar_receivable_applications.attribute10%TYPE,
4862         p_attribute11 IN ar_receivable_applications.attribute11%TYPE,
4863         p_attribute12 IN ar_receivable_applications.attribute12%TYPE,
4864         p_attribute13 IN ar_receivable_applications.attribute13%TYPE,
4865         p_attribute14 IN ar_receivable_applications.attribute14%TYPE,
4866         p_attribute15 IN ar_receivable_applications.attribute15%TYPE,
4867         p_global_attribute_category IN ar_receivable_applications.global_attribute_category%TYPE,
4868         p_global_attribute1 IN ar_receivable_applications.global_attribute1%TYPE,
4869         p_global_attribute2 IN ar_receivable_applications.global_attribute2%TYPE,
4870         p_global_attribute3 IN ar_receivable_applications.global_attribute3%TYPE,
4871         p_global_attribute4 IN ar_receivable_applications.global_attribute4%TYPE,
4872         p_global_attribute5 IN ar_receivable_applications.global_attribute5%TYPE,
4873         p_global_attribute6 IN ar_receivable_applications.global_attribute6%TYPE,
4874         p_global_attribute7 IN ar_receivable_applications.global_attribute7%TYPE,
4875         p_global_attribute8 IN ar_receivable_applications.global_attribute8%TYPE,
4876         p_global_attribute9 IN ar_receivable_applications.global_attribute9%TYPE,
4877         p_global_attribute10 IN ar_receivable_applications.global_attribute10%TYPE,
4878         p_global_attribute11 IN ar_receivable_applications.global_attribute11%TYPE,
4879         p_global_attribute12 IN ar_receivable_applications.global_attribute12%TYPE,
4880         p_global_attribute13 IN ar_receivable_applications.global_attribute13%TYPE,
4881         p_global_attribute14 IN ar_receivable_applications.global_attribute14%TYPE,
4882         p_global_attribute15 IN ar_receivable_applications.global_attribute15%TYPE,
4883         p_global_attribute16 IN ar_receivable_applications.global_attribute16%TYPE,
4884         p_global_attribute17 IN ar_receivable_applications.global_attribute17%TYPE,
4885         p_global_attribute18 IN ar_receivable_applications.global_attribute18%TYPE,
4886         p_global_attribute19 IN ar_receivable_applications.global_attribute19%TYPE,
4887         p_global_attribute20 IN ar_receivable_applications.global_attribute20%TYPE,
4888         p_comments IN ar_receivable_applications.comments%TYPE,
4889         p_module_name IN VARCHAR2,
4890         p_module_version IN VARCHAR2,
4891 	p_out_rec_application_id	OUT NOCOPY NUMBER
4892       , p_application_ref_num IN ar_receivable_applications.application_ref_num%TYPE
4893       , p_secondary_application_ref_id IN ar_receivable_applications.secondary_application_ref_id%TYPE
4894       , p_customer_reference IN ar_receivable_applications.customer_reference%TYPE
4895       , p_customer_reason IN ar_receivable_applications.customer_reason%TYPE
4896       , p_secondary_app_ref_type IN
4897         ar_receivable_applications.secondary_application_ref_type%TYPE := null
4898       , p_secondary_app_ref_num IN
4899         ar_receivable_applications.secondary_application_ref_num%TYPE := null
4900 ) IS
4901 
4902 l_ra_rec   ar_receivable_applications%ROWTYPE;
4903 l_ps_rec   ar_payment_schedules%ROWTYPE;
4904 
4905 l_cr_rec                 ar_cash_receipts%ROWTYPE;
4906 l_amount_due_remaining   NUMBER;
4907 ln_batch_id	         NUMBER;
4908 functional_curr          VARCHAR2(100);
4909 
4910 l_onacc_cc_id  ar_receipt_method_accounts.on_account_ccid%TYPE;
4911 l_unapp_cc_id  ar_receipt_method_accounts.unapplied_ccid%TYPE;
4912 
4913 l_on_account_total      NUMBER;
4914 l_ae_doc_rec            ae_doc_rec_type;
4915 l_prev_unapp_id         NUMBER;
4916 l_called_from_api       VARCHAR2(1);
4917   --Bug#2750340
4918   l_xla_ev_rec      arp_xla_events.xla_events_type;
4919   l_xla_doc_table   VARCHAR2(20);
4920 
4921 BEGIN
4922     IF PG_DEBUG in ('Y', 'C') THEN
4923        arp_standard.debug(   'arp_process_application.on_account_receipts()+' );
4924        arp_standard.debug(   '-- p_receipt_ps_id = '||TO_CHAR(p_receipt_ps_id));
4925        arp_standard.debug(   '-- p_amount_applied = '||
4926                         TO_CHAR( p_amount_applied ) );
4927        arp_standard.debug(   '-- p_gl_date = '|| TO_CHAR( p_gl_date ) );
4928        arp_standard.debug(   '-- p_apply_date = '|| TO_CHAR( p_apply_date ) );
4929     END IF;
4930     --
4931     IF ( p_module_name IS NOT NULL AND p_module_version IS NOT NULL ) THEN
4932          validate_on_account_args( p_receipt_ps_id, p_amount_applied,
4933                                    p_apply_date, p_gl_date );
4934     END IF;
4935     --
4936     arp_ps_pkg.fetch_p( p_receipt_ps_id, l_ps_rec );
4937 
4938     functional_curr := arp_global.functional_currency;
4939 
4940     -- ---------------------------------------------------------------------
4941     -- Get UNAPP and ON-ACC CC'Ids by selecting from receipt method accounts
4942     -- table
4943     -- 05/06/1996 H.Kaukovuo	Added batch_id
4944     -- 10/31/1996 K.Lawrance    Added amount_due_remaining
4945     -- ---------------------------------------------------------------------
4946 
4947     SELECT ps.cash_receipt_id,
4948            ps.amount_due_remaining,
4949            rma.on_account_ccid,
4950            rma.unapplied_ccid,
4951 	   crh.batch_id
4952     INTO   l_ra_rec.cash_receipt_id,
4953            l_amount_due_remaining,
4954            l_onacc_cc_id,
4955            l_unapp_cc_id
4956 	   , ln_batch_id
4957     FROM     ar_payment_schedules 	ps
4958            , ar_cash_receipts 		cr
4959 	   , ar_cash_receipt_history	crh
4960            , ar_receipt_methods 	rm
4961            , ce_bank_acct_uses		ba
4962            , ar_receipt_method_accounts rma
4963     WHERE  ps.payment_schedule_id 	= p_receipt_ps_id
4964     AND    cr.cash_receipt_id 		= ps.cash_receipt_id
4965     AND	   crh.cash_receipt_id		= cr.cash_receipt_id
4966     AND	   crh.current_record_flag	= 'Y'
4967     AND    rm.receipt_method_id 	= cr.receipt_method_id
4968     AND    ba.bank_acct_use_id 	        = cr.remit_bank_acct_use_id
4969     AND    rma.remit_bank_acct_use_id 	= ba.bank_acct_use_id
4970     AND    rma.receipt_method_id 	= rm.receipt_method_id;
4971 
4972     -- Get the current amount that is On Account for this receipt.
4973     -- This is used later on to determine it the receipt has been
4974     -- fully applied.  We need to do the sum now so we don't include
4975     -- the On Account row that we are about to create.
4976     --
4977     select nvl(sum(ra.amount_applied),0)
4978     into   l_on_account_total
4979     from   ar_receivable_applications ra
4980     where  ra.cash_receipt_id = l_ra_rec.cash_receipt_id
4981     and    ra.status IN ('ACC','OTHER ACC');
4982 
4983     -- Prepare for 'UNAPP' record insertion with -ve amount applied
4984     -- applied_customer_trx_id is NULL and display = 'N'
4985     --
4986     l_ra_rec.payment_schedule_id := p_receipt_ps_id;
4987     l_ra_rec.amount_applied := -p_amount_applied;
4988     --
4989     -- Get the acctd_amount_applied_from value
4990     --
4991     l_ra_rec.acctd_amount_applied_from :=
4992                  ARPCURR.functional_amount( l_ra_rec.amount_applied,
4993                                             functional_curr,
4994                                             l_ps_rec.exchange_rate,
4995                                             NULL, NULL );
4996     IF PG_DEBUG in ('Y', 'C') THEN
4997        arp_standard.debug(   'acctd_amount_applied_from = '||
4998                          TO_CHAR( l_ra_rec.acctd_amount_applied_from ) );
4999     END IF;
5000     --
5001     l_ra_rec.status := 'UNAPP';
5002     l_ra_rec.application_type := 'CASH';
5003 
5004     -- l_ra_rec.application_rule := 'ON ACCOUNT APPLICATION';
5005     l_ra_rec.application_rule := '60.7';
5006     l_ra_rec.program_id	:= -100106;
5007 
5008     l_ra_rec.code_combination_id := l_unapp_cc_id;
5009     l_ra_rec.apply_date := p_apply_date;
5010     l_ra_rec.gl_date := p_gl_date;
5011     l_ra_rec.posting_control_id := -3;
5012     l_ra_rec.display := 'N';
5013     l_ra_rec.ussgl_transaction_code := p_ussgl_transaction_code;
5014     l_ra_rec.attribute_category := p_attribute_category;
5015     l_ra_rec.attribute1 := p_attribute1;
5016     l_ra_rec.attribute2 := p_attribute2;
5017     l_ra_rec.attribute3 := p_attribute3;
5018     l_ra_rec.attribute4 := p_attribute4;
5019     l_ra_rec.attribute5 := p_attribute5;
5020     l_ra_rec.attribute6 := p_attribute6;
5021     l_ra_rec.attribute7 := p_attribute7;
5022     l_ra_rec.attribute8 := p_attribute8;
5023     l_ra_rec.attribute9 := p_attribute9;
5024     l_ra_rec.attribute10 := p_attribute10;
5025     l_ra_rec.attribute11 := p_attribute11;
5026     l_ra_rec.attribute12 := p_attribute12;
5027     l_ra_rec.attribute13 := p_attribute13;
5028     l_ra_rec.attribute14 := p_attribute14;
5029     l_ra_rec.attribute15 := p_attribute15;
5030     l_ra_rec.global_attribute_category := p_global_attribute_category;
5031     l_ra_rec.global_attribute1 := p_global_attribute1;
5032     l_ra_rec.global_attribute2 := p_global_attribute2;
5033     l_ra_rec.global_attribute3 := p_global_attribute3;
5034     l_ra_rec.global_attribute4 := p_global_attribute4;
5035     l_ra_rec.global_attribute5 := p_global_attribute5;
5036     l_ra_rec.global_attribute6 := p_global_attribute6;
5037     l_ra_rec.global_attribute7 := p_global_attribute7;
5038     l_ra_rec.global_attribute8 := p_global_attribute8;
5039     l_ra_rec.global_attribute9 := p_global_attribute9;
5040     l_ra_rec.global_attribute10 := p_global_attribute10;
5041     l_ra_rec.global_attribute11 := p_global_attribute11;
5042     l_ra_rec.global_attribute12 := p_global_attribute12;
5043     l_ra_rec.global_attribute13 := p_global_attribute13;
5044     l_ra_rec.global_attribute14 := p_global_attribute14;
5045     l_ra_rec.global_attribute15 := p_global_attribute15;
5046     l_ra_rec.global_attribute16 := p_global_attribute16;
5047     l_ra_rec.global_attribute17 := p_global_attribute17;
5048     l_ra_rec.global_attribute18 := p_global_attribute18;
5049     l_ra_rec.global_attribute19 := p_global_attribute19;
5050     l_ra_rec.global_attribute20 := p_global_attribute20;
5051     l_ra_rec.comments := p_comments; --Bug 2047229
5052 
5053     --
5054     -- Insert UNAPP record
5055     --
5056     arp_app_pkg.insert_p( l_ra_rec, l_ra_rec.receivable_application_id );
5057 
5058     IF l_ra_rec.receivable_application_id IS NOT NULL THEN
5059          l_xla_ev_rec.xla_from_doc_id := l_ra_rec.receivable_application_id;
5060          l_xla_ev_rec.xla_to_doc_id   := l_ra_rec.receivable_application_id;
5061          l_xla_ev_rec.xla_mode        := 'O';
5062          l_xla_ev_rec.xla_call        := 'B';
5063          l_xla_ev_rec.xla_doc_table := 'APP';
5064          ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
5065     END IF;
5066 
5067 
5068     --Set UNAPP id for PAIRING
5069     l_prev_unapp_id := l_ra_rec.receivable_application_id;
5070 
5071     -- ---------------------------------------------------------------------
5072     -- Prepare for 'ACC' record insertion with +ve amount applied
5073     -- applied_payment_schedule_id and applied_customer_trx_id are -1 and
5074     -- display = 'Y', Only the following details change for the 'ACC' record
5075     -- from the UNAPP record during ON-ACCOUNT receipt insertion.
5076     -- ---------------------------------------------------------------------
5077 
5078     l_ra_rec.receivable_application_id := NULL; /* filled during act. insert */
5079     l_ra_rec.applied_customer_trx_id := -1;
5080     l_ra_rec.applied_payment_schedule_id := -1;
5081     l_ra_rec.code_combination_id := l_onacc_cc_id;
5082     l_ra_rec.amount_applied := p_amount_applied;
5083     l_ra_rec.application_rule := '60.0';
5084     l_ra_rec.program_id := -100107;
5085 
5086     --
5087     -- acctd_amount_applied_from is -ve of already calculated
5088     -- acctd_amount_applied_from for 'UNAPP' record
5089     --
5090     l_ra_rec.acctd_amount_applied_from := -l_ra_rec.acctd_amount_applied_from;
5091     --
5092     l_ra_rec.status := 'ACC';
5093     l_ra_rec.display := 'Y';
5094 
5095     l_ra_rec.application_ref_num := p_application_ref_num;
5096     l_ra_rec.secondary_application_ref_id := p_secondary_application_ref_id;
5097     l_ra_rec.secondary_application_ref_type := p_secondary_app_ref_type;
5098     l_ra_rec.secondary_application_ref_num := p_secondary_app_ref_num;
5099     l_ra_rec.customer_reference := p_customer_reference;
5100     l_ra_rec.customer_reason := p_customer_reason;  -- 4145224
5101 
5102     -- Insert ACC record
5103 
5104     arp_app_pkg.insert_p( l_ra_rec, l_ra_rec.receivable_application_id );
5105 
5106     IF l_ra_rec.receivable_application_id IS NOT NULL THEN
5107          l_xla_ev_rec.xla_from_doc_id := l_ra_rec.receivable_application_id;
5108          l_xla_ev_rec.xla_to_doc_id   := l_ra_rec.receivable_application_id;
5109          l_xla_ev_rec.xla_mode        := 'O';
5110          l_xla_ev_rec.xla_call        := 'B';
5111          l_xla_ev_rec.xla_doc_table := 'APP';
5112          ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
5113     END IF;
5114 
5115    --
5116    --Release 11.5 VAT changes, create paired UNAPP record accounting
5117    --in ar_distributions
5118    --
5119     l_ae_doc_rec.document_type             := 'RECEIPT';
5120     l_ae_doc_rec.document_id               := l_ra_rec.cash_receipt_id;
5121     l_ae_doc_rec.accounting_entity_level   := 'ONE';
5122     l_ae_doc_rec.source_table              := 'RA';
5123     l_ae_doc_rec.source_id                 := l_prev_unapp_id;
5124     l_ae_doc_rec.source_id_old             := l_ra_rec.receivable_application_id;
5125     l_ae_doc_rec.other_flag                := 'PAIR';
5126     arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
5127 
5128    --
5129    --Release 11.5 VAT changes, create ACC record accounting
5130    --in ar_distributions
5131    --
5132     l_ae_doc_rec.source_id                 := l_ra_rec.receivable_application_id;
5133     l_ae_doc_rec.source_id_old             := '';
5134     l_ae_doc_rec.other_flag                := '';
5135     arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
5136 
5137    /* Bug 4910860
5138        Check if the accounting entries balance */
5139       IF nvl(p_module_name,'RAPI') = 'RAPI' THEN
5140          l_called_from_api := 'Y';
5141       ELSE
5142         l_called_from_api := 'N';
5143       END IF;
5144     arp_balance_check.Check_Appln_Balance(l_ra_rec.receivable_application_id, -- ACC rec_app_id
5145                                             l_prev_unapp_id,  -- UNAPP rec_app_id
5146                                             NULL,
5147                                             l_called_from_api);
5148 
5149 
5150     -- ----------------------------------------------------------------------
5151     -- 10/31/1996 K.Lawrance
5152     -- Finally update cash receipt status.
5153     -- ----------------------------------------------------------------------
5154 
5155     -- First, set ar_cash_receipt record values to dummy ...
5156     -- This is to distinguish between updateable NULL and NULL value (dummy)
5157     -- which means that column is not to be updated.
5158 
5159     arp_cash_receipts_pkg.set_to_dummy(l_cr_rec);
5160 
5161     -- ---------------------------------------------------------------------
5162     -- Cash receipt must be fully applied in order to set the status
5163     -- to 'Applied'.
5164     -- ---------------------------------------------------------------------
5165     IF PG_DEBUG in ('Y', 'C') THEN
5166        arp_standard.debug (  '-- Defining receipt status ...');
5167        arp_standard.debug (  '-- p_amount_applied = '||to_char(p_amount_applied));
5168        arp_standard.debug (  '-- l_amount_due_remaining = '||
5169 		                            to_char(l_amount_due_remaining));
5170     END IF;
5171 
5172     -- Determine if the receipt has been fully applied.
5173     -- We include the total amount that is On Account as this is
5174     -- not included in the Payment Schedules, Amount Due Remaining
5175     -- total for the receipt.
5176     --
5177     IF (l_amount_due_remaining + l_on_account_total + p_amount_applied < 0)
5178     THEN
5179       l_cr_rec.status           := 'UNAPP';
5180     ELSE
5181       l_cr_rec.status           := 'APP';
5182     END IF;
5183 
5184     IF PG_DEBUG in ('Y', 'C') THEN
5185        arp_standard.debug (  '-- status = '|| l_cr_rec.status);
5186     END IF;
5187 
5188     l_cr_rec.cash_receipt_id  := l_ra_rec.cash_receipt_id;
5189 
5190     -- Update cash receipt status.
5191     arp_cash_receipts_pkg.update_p(
5192 	  l_cr_rec
5193 	, l_ra_rec.cash_receipt_id);
5194 
5195     -- ---------------------------------------------------------------------
5196     -- Update batch status if receipt has a batch
5197     -- ---------------------------------------------------------------------
5198     IF (ln_batch_id IS NOT NULL)
5199     THEN
5200       arp_rw_batches_check_pkg.update_batch_status(ln_batch_id);
5201     END IF;
5202 
5203     -- ---------------------------------------------------------------------
5204     -- Return the new receivable application id back to the form
5205     -- ---------------------------------------------------------------------
5206     p_out_rec_application_id := l_ra_rec.receivable_application_id;
5207 
5208     IF PG_DEBUG in ('Y', 'C') THEN
5209        arp_standard.debug(
5210               'arp_process_application.on_account_receipts()-' );
5211     END IF;
5212     EXCEPTION
5213          WHEN OTHERS THEN
5214            IF PG_DEBUG in ('Y', 'C') THEN
5215               arp_standard.debug(
5216                 'EXCEPTION: arp_process_application.on_account_receipts' );
5217            END IF;
5218               RAISE;
5219 
5220 END on_account_receipts;
5221 --
5222 /*===========================================================================+
5223  | PROCEDURE                                                                 |
5224  |    validate_on_account_args                                               |
5225  |                                                                           |
5226  | DESCRIPTION                                                               |
5227  |    Validate arguments passed to on_account_receipts procedure             |
5228  |                                                                           |
5229  | SCOPE - PRIVATE                                                           |
5230  |                                                                           |
5231  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
5232  |                                                                           |
5233  | ARGUMENTS  : IN:                                                          |
5234  |                 p_ps_id - Payment Schedule Id of the receipt              |
5235  |                 p_amount_applied - Input amount applied                   |
5236  |                 p_gl_date         - Gl Date                               |
5237  |                 p_apply_date - Application date                           |
5238  |              OUT:                                                         |
5239  |                                                                           |
5240  | RETURNS    : NONE                                                         |
5241  |                                                                           |
5242  | NOTES                                                                     |
5243  |                                                                           |
5244  | MODIFICATION HISTORY - Created by Ganesh Vaidee - 04/25/95                |
5245  |                                                                           |
5246  +===========================================================================*/
5247 PROCEDURE  validate_on_account_args(
5248         p_ps_id   IN ar_payment_schedules.payment_schedule_id%TYPE,
5249         p_amount_applied IN
5250                 ar_receivable_applications.amount_applied%TYPE,
5251         p_apply_date IN ar_receivable_applications.apply_date%TYPE,
5252         p_gl_date IN ar_receivable_applications.gl_date%TYPE ) IS
5253 BEGIN
5254     IF PG_DEBUG in ('Y', 'C') THEN
5255        arp_standard.debug(
5256               'arp_process_application.validate_on_account_args()+' );
5257     END IF;
5258     --
5259     IF ( p_ps_id IS NULL OR p_apply_date IS NULL OR
5260          p_gl_date IS NULL OR p_amount_applied IS NULL ) THEN
5261          FND_MESSAGE.set_name ('AR', 'AR_ARGUEMENTS_FAIL' );
5262          APP_EXCEPTION.raise_exception;
5263     END IF;
5264     --
5265     IF PG_DEBUG in ('Y', 'C') THEN
5266        arp_standard.debug(
5267               'arp_process_application.validate_on_account_args()-' );
5268     END IF;
5269     EXCEPTION
5270          WHEN OTHERS THEN
5271            IF PG_DEBUG in ('Y', 'C') THEN
5272               arp_standard.debug(
5273                 'EXCEPTION: arp_process_application.validate_on_account_args' );
5274            END IF;
5275               RAISE;
5276 END validate_on_account_args;
5277 
5278 
5279 /*===========================================================================+
5280  | PROCEDURE                                                                 |
5281  |    activity_application                                                   |
5282  |                                                                           |
5283  | DESCRIPTION                                                               |
5284  |    Create Activity applications. These applications are done against      |
5285  |    an seeded payment schedule (ie Short Term Debt activity application    |
5286  |    against ps of -2). The CCID for the application is retrieved from      |
5287  |    receivable activity. In STD case the receipt payment schedule is       |
5288  |    impacted by the application.                                           |
5289  |                                                                           |
5290  | SCOPE - PUBLIC                                                            |
5291  |                                                                           |
5292  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
5293  |                                                                           |
5294  | ARGUMENTS  : IN:                                                          |
5295  |               p_receipt_ps_id          - PS id of the receipt             |
5296  |               p_application_ps_id      - PS id of the application         |
5297  |               p_link_to_customer_trx_id- Link to customer_trx_id related  |
5298  |                                          to Short term debt applicatio    |
5299  |               p_amount_applied         - Input amount applied             |
5300  |               p_apply_date             - Application date                 |
5301  |               p_gl_date                - Gl Date                          |
5302  |               p_receivables_trx_id     - Receivable Activity ID           |
5303  |               p_ussgl_transaction_code - USSGL transaction code           |
5304  |               OTHER DESCRIPTIVE FLEX columns                              |
5305  |               p_module_name            - Name of the module that called   |
5306  |                                          this procedure                   |
5307  |               p_module_version         - Version of the module that       |
5308  |                                          called this procedure            |
5309  |              OUT:                                                         |
5310  |               p_out_rec_application_id Returned receivable application id |
5311  | RETURNS    : NONE                                                         |
5312  |                                                                           |
5313  | MODIFICATION HISTORY                                                      |
5314  | 14-APR-2000 Jani Rautiain Created                                         |
5315  | 08-Feb-01   S. Nambiar    Bug 1634986 - Modified activity_application     |
5316  |                           to update receipt status properly               |
5317  | 25-FEB-2001 Manoj Gudivak Modified to handle Claim Application            |
5318  | 19-APR-2001 Jon Beckett   Claim Application now handled by new procedure  |
5319  |                           other_account_application                       |
5320  | 08-May-2001 S Nambiar     Now p_application_ref_id contains the chargeback|
5321  |                           customer_trx_id.And claim_id is stored in       |
5322  |                           chargeback_customer_trx_id field.               |
5323  |                           This is a temporary arrangement.                |
5324  |  14-May-01  S.Nambiar     Modified activity_application() routine to replace
5325  |                           the balance segment of activity application to that
5326  |                           of receipt UNAPP
5327  | 06/20/2001  S.Nambiar     Bug 1823299 - Activity application should not
5328  |                           leave rounding difference amount as unapplied
5329  |                           for forign currency receipts.
5330  | 08-NOV-01   S.Nambiar     Bug 2103345. calculate acctd_amount_applied_to
5331  |                           for cc refund activity application.
5332  | 09-Sep-02   Debbie Jancis Modified for mrc trigger replacement.  Added
5333  |                           calls to ar_mrc_engine3 for processing receivable
5334  |			     applications.
5335  | 06-FEB-03   Jon Beckett   Bug 2751910 - Added p_applied_rec_app_id for
5336  |                           netting.
5337  | 29-MAY-03   Jon Beckett   Bug 2821138 - Added p_netted_receipt_flag and
5338  |                           calculation of acctd_amount_applied_to for
5339  |                 	     main receipt in a netting pair.
5340  | 02-Feb-05   Debbie Jancis Enhancement 4145224: added customer_Reason
5341  | 04-Mar-05   Jyoti Pandey  Bug: 4166986 CC Chargeback logic
5342  +===========================================================================*/
5343 PROCEDURE activity_application(
5344         p_receipt_ps_id             IN
5345                    ar_payment_schedules.payment_schedule_id%TYPE,
5346         p_application_ps_id         IN
5347                    ar_receivable_applications.applied_payment_schedule_id%TYPE,
5348         p_link_to_customer_trx_id   IN
5349                    ar_receivable_applications.link_to_customer_trx_id%TYPE,
5350         p_amount_applied            IN
5351                    ar_receivable_applications.amount_applied%TYPE,
5352         p_apply_date                IN
5353                    ar_receivable_applications.apply_date%TYPE,
5354         p_gl_date                   IN
5355                    ar_receivable_applications.gl_date%TYPE,
5356         p_receivables_trx_id        IN
5357                    ar_receivable_applications.receivables_trx_id%TYPE,
5358         p_ussgl_transaction_code    IN
5359                    ar_receivable_applications.ussgl_transaction_code%TYPE,
5360         p_attribute_category        IN
5361                    ar_receivable_applications.attribute_category%TYPE,
5362         p_attribute1                IN
5363                    ar_receivable_applications.attribute1%TYPE,
5364         p_attribute2                IN
5365                    ar_receivable_applications.attribute2%TYPE,
5366         p_attribute3                IN
5367                    ar_receivable_applications.attribute3%TYPE,
5368         p_attribute4                IN
5369                    ar_receivable_applications.attribute4%TYPE,
5370         p_attribute5                IN
5371                    ar_receivable_applications.attribute5%TYPE,
5372         p_attribute6                IN
5373                    ar_receivable_applications.attribute6%TYPE,
5374         p_attribute7                IN
5375                    ar_receivable_applications.attribute7%TYPE,
5376         p_attribute8                IN
5377                    ar_receivable_applications.attribute8%TYPE,
5378         p_attribute9                IN
5379                    ar_receivable_applications.attribute9%TYPE,
5380         p_attribute10               IN
5381                    ar_receivable_applications.attribute10%TYPE,
5382         p_attribute11               IN
5383                    ar_receivable_applications.attribute11%TYPE,
5384         p_attribute12               IN
5385                    ar_receivable_applications.attribute12%TYPE,
5386         p_attribute13               IN
5387                    ar_receivable_applications.attribute13%TYPE,
5388         p_attribute14               IN
5389                    ar_receivable_applications.attribute14%TYPE,
5390         p_attribute15               IN
5391                    ar_receivable_applications.attribute15%TYPE,
5392         p_global_attribute_category IN
5393                    ar_receivable_applications.global_attribute_category%TYPE,
5394         p_global_attribute1         IN
5395                    ar_receivable_applications.global_attribute1%TYPE,
5396         p_global_attribute2         IN
5397                    ar_receivable_applications.global_attribute2%TYPE,
5398         p_global_attribute3         IN
5399                    ar_receivable_applications.global_attribute3%TYPE,
5400         p_global_attribute4         IN
5401                    ar_receivable_applications.global_attribute4%TYPE,
5402         p_global_attribute5         IN
5403                    ar_receivable_applications.global_attribute5%TYPE,
5404         p_global_attribute6         IN
5405                    ar_receivable_applications.global_attribute6%TYPE,
5406         p_global_attribute7         IN
5407                    ar_receivable_applications.global_attribute7%TYPE,
5408         p_global_attribute8         IN
5409                    ar_receivable_applications.global_attribute8%TYPE,
5410         p_global_attribute9         IN
5411                    ar_receivable_applications.global_attribute9%TYPE,
5412         p_global_attribute10        IN
5413                    ar_receivable_applications.global_attribute10%TYPE,
5414         p_global_attribute11        IN
5415                    ar_receivable_applications.global_attribute11%TYPE,
5416         p_global_attribute12        IN
5417                    ar_receivable_applications.global_attribute12%TYPE,
5418         p_global_attribute13        IN
5419                    ar_receivable_applications.global_attribute13%TYPE,
5420         p_global_attribute14        IN
5421                    ar_receivable_applications.global_attribute14%TYPE,
5422         p_global_attribute15        IN
5423                    ar_receivable_applications.global_attribute15%TYPE,
5424         p_global_attribute16        IN
5425                    ar_receivable_applications.global_attribute16%TYPE,
5426         p_global_attribute17        IN
5427                    ar_receivable_applications.global_attribute17%TYPE,
5428         p_global_attribute18        IN
5429                    ar_receivable_applications.global_attribute18%TYPE,
5430         p_global_attribute19        IN
5431                    ar_receivable_applications.global_attribute19%TYPE,
5432         p_global_attribute20        IN
5433                    ar_receivable_applications.global_attribute20%TYPE,
5434         p_comments IN
5435                    ar_receivable_applications.comments%TYPE,
5436         p_module_name               IN VARCHAR2,
5437         p_module_version            IN VARCHAR2,
5438         p_application_ref_type IN OUT NOCOPY
5439                    ar_receivable_applications.application_ref_type%TYPE,
5440         p_application_ref_id IN OUT NOCOPY
5441                    ar_receivable_applications.application_ref_id%TYPE,
5442         p_application_ref_num IN OUT NOCOPY
5443                    ar_receivable_applications.application_ref_num%TYPE,
5444         p_secondary_application_ref_id IN OUT NOCOPY NUMBER,
5445         p_payment_set_id IN NUMBER,
5446 	p_called_from 		    IN VARCHAR2,  /*5444407*/
5447         p_out_rec_application_id    OUT NOCOPY NUMBER,
5448         p_applied_rec_app_id IN NUMBER,
5449         p_customer_reference IN ar_receivable_applications.customer_reference%TYPE,
5450 	p_netted_receipt_flag IN VARCHAR2,
5451 	p_netted_cash_receipt_id IN ar_cash_receipts.cash_receipt_id%TYPE ,
5452     p_secondary_app_ref_type IN
5453        ar_receivable_applications.secondary_application_ref_type%TYPE := null,
5454     p_secondary_app_ref_num IN
5455        ar_receivable_applications.secondary_application_ref_num%TYPE := null,
5456     p_customer_reason IN
5457            ar_receivable_applications.customer_reason%TYPE DEFAULT NULL,
5458 --Bug 5450371
5459     p_application_ref_reason IN ar_receivable_applications.application_ref_reason%TYPE Default NULL
5460 	) IS
5461 
5462   /* Cursor to application information for activity application */
5463   CURSOR activity_c IS
5464     SELECT cr.currency_code,
5465            ps.cash_receipt_id,
5466            ps.amount_due_remaining,
5467            rma.unapplied_ccid,
5468 	   crh.batch_id,
5469            rt.code_combination_id activity_ccid,
5470            rt.type activity_type
5471     FROM   ar_payment_schedules ps,
5472            ar_cash_receipts cr,
5473 	   ar_cash_receipt_history crh,
5474            ar_receipt_methods rm,
5475            ce_bank_acct_uses ba,
5476            ar_receipt_method_accounts rma,
5477            ar_receivables_trx rt
5478     WHERE  ps.payment_schedule_id 	= p_receipt_ps_id
5479     AND    cr.cash_receipt_id 		= ps.cash_receipt_id
5480     AND	   crh.cash_receipt_id		= cr.cash_receipt_id
5481     AND	   crh.current_record_flag	= 'Y'
5482     AND    rm.receipt_method_id 	= cr.receipt_method_id
5483     AND    ba.bank_acct_use_id 		= cr.remit_bank_acct_use_id
5484     AND    rma.remit_bank_acct_use_id 	= ba.bank_acct_use_id
5485     AND    rma.receipt_method_id 	= rm.receipt_method_id
5486     AND    rt.receivables_trx_id        = p_receivables_trx_id;
5487 
5488   activity_rec activity_c%ROWTYPE;
5489 
5490   l_ra_rec   ar_receivable_applications%ROWTYPE;
5491   l_ps_rec   ar_payment_schedules%ROWTYPE;
5492   l_cr_rec   ar_cash_receipts%ROWTYPE;
5493   l_crcpt_rec   ar_cash_receipts%ROWTYPE;
5494 
5495   l_unapp_ra_rec ar_receivable_applications%ROWTYPE;   /* MRC */
5496 
5497   functional_curr             VARCHAR2(100);
5498   l_on_account_total          NUMBER;
5499   l_ae_doc_rec                ae_doc_rec_type;
5500   l_prev_unapp_id             NUMBER;
5501   l_acctd_amount_applied_from ar_receivable_applications.acctd_amount_applied_from%TYPE;
5502   l_acctd_amount_applied_to   ar_receivable_applications.acctd_amount_applied_to%TYPE;
5503   l_invoice_currency_code     ra_customer_trx.invoice_currency_code%TYPE;
5504   l_receipt_currency_code     ar_cash_receipts.currency_code%TYPE;
5505   l_exchange_rate             ra_customer_trx.exchange_rate%TYPE;
5506   l_chart_of_accounts_id      gl_sets_of_books.chart_of_accounts_id%TYPE;
5507   l_func_amount_due_remaining ar_payment_schedules.amount_due_remaining%TYPE;
5508 
5509   l_fnd_api_constants_rec     ar_bills_main.fnd_api_constants_type     := ar_bills_main.get_fnd_api_constants_rec;
5510   l_fnd_msg_pub_constants_rec ar_bills_main.fnd_msg_pub_constants_type := ar_bills_main.get_fnd_msg_pub_constants_rec;
5511   l_attribute_rec             AR_RECEIPT_API_PUB.attribute_rec_type;
5512   l_global_attribute_rec      AR_RECEIPT_API_PUB.global_attribute_rec_type;
5513   l_return_status             VARCHAR2(1);
5514   l_msg_count                 NUMBER;
5515   l_msg_data                  VARCHAR2(2000);
5516   l_msg_index                 NUMBER;
5517 
5518   l_application_ref_type  ar_receivable_applications.application_ref_type%TYPE;
5519   l_application_ref_num   ar_receivable_applications.application_ref_num%TYPE;
5520   l_application_ref_id    ar_receivable_applications.application_ref_id%TYPE;
5521   l_secondary_application_ref_id ar_receivable_applications.secondary_application_ref_id%TYPE;
5522   l_secondary_app_ref_type  ar_receivable_applications.secondary_application_ref_type%TYPE;
5523   l_secondary_app_ref_num   ar_receivable_applications.secondary_application_ref_num%TYPE;
5524   l_netted_receipt_flag   VARCHAR2(1);
5525   l_mc_application_ref_id ar_receivable_applications.application_ref_id%TYPE;
5526   API_exception              EXCEPTION;
5527 
5528  --Bug 5450371
5529    l_application_ref_reason  ar_receivable_applications.application_ref_reason%TYPE;
5530 
5531   --For CC_Chargeback logic
5532   l_called_from   VARCHAR2(100) := null;
5533   l_called_from_api VARCHAR2(1);
5534 
5535 
5536   --Bug#2750340
5537   l_xla_ev_rec      arp_xla_events.xla_events_type;
5538   l_xla_doc_table   VARCHAR2(20);
5539 
5540   -- Bug 7317841
5541   l_pymnt_trxn_ext_id_temp  ar_cash_receipts.payment_trxn_extension_id%TYPE;
5542 
5543 BEGIN
5544  IF PG_DEBUG in ('Y', 'C') THEN
5545     arp_standard.debug(   'arp_process_application.activity_application()+' );
5546     arp_standard.debug(   '-- p_receipt_ps_id = '||TO_CHAR(p_receipt_ps_id));
5547     arp_standard.debug(   '-- p_application_ps_id = '||TO_CHAR(p_application_ps_id));
5548     arp_standard.debug(   '-- p_link_to_customer_trx_id = '||
5549                      TO_CHAR(p_link_to_customer_trx_id));
5550     arp_standard.debug(   '-- p_amount_applied = '||TO_CHAR( p_amount_applied ) );
5551     arp_standard.debug(   '-- p_gl_date = '|| TO_CHAR( p_gl_date ) );
5552     arp_standard.debug(   '-- p_receivables_trx_id = '||
5553                      TO_CHAR( p_receivables_trx_id ) );
5554     arp_standard.debug(   '-- p_apply_date = '|| TO_CHAR( p_apply_date ) );
5555  END IF;
5556 
5557  /* Validate parameters */
5558 
5559  validate_activity_args( p_receipt_ps_id, p_application_ps_id,
5560                          p_link_to_customer_trx_id,p_amount_applied,
5561                          p_apply_date, p_gl_date,p_receivables_trx_id );
5562 
5563     /* Fetch Receipt payment schedule */
5564     arp_ps_pkg.fetch_p( p_receipt_ps_id, l_ps_rec );
5565 
5566    --Store the details in local variable.
5567      l_application_ref_type := p_application_ref_type;
5568      l_application_ref_num := p_application_ref_num;
5569      l_application_ref_id  := p_application_ref_id;
5570      l_secondary_application_ref_id := p_secondary_application_ref_id;
5571      l_secondary_app_ref_type := p_secondary_app_ref_type;
5572      l_secondary_app_ref_num := p_secondary_app_ref_num;
5573      l_netted_receipt_flag := NVL(p_netted_receipt_flag,'N');
5574 
5575 
5576     functional_curr := arp_global.functional_currency;
5577 
5578     IF NVL(p_application_ps_id,0)    = -5 THEN
5579        l_application_ref_type := 'CHARGEBACK';
5580     ---CC_Chargeback
5581     ELSIF ( NVL(p_application_ps_id,0) = -6 ) OR
5582           ( NVL(p_application_ps_id,0) = -9 )   THEN
5583 
5584         l_application_ref_type := 'MISC_RECEIPT';
5585 
5586     ELSIF NVL(p_application_ps_id,0) = -8  THEN
5587         l_application_ref_type := 'AP_REFUND_REQUEST';
5588  	l_application_ref_reason := p_application_ref_reason; --Bug 5450371
5589 
5590     ELSE
5591           l_application_ref_type := NULL;
5592     END IF;
5593 
5594     -- Bug 1996893 if applied_ps_id is -6 (Credit card), then
5595     -- create Misc receipt also.
5596 
5597     --Activity application routine is called from rate_adjustment program also
5598     --But when rate adjusting,should not recreate the Misc.Receipt
5599     --CC_chargeback
5600     IF  ( (nvl(p_application_ps_id,0) = -6) OR
5601           (nvl(p_application_ps_id,0) = -9)
5602     AND nvl(p_module_name,'NONE') <> 'RATE_ADJUSTMENT_MAIN')  THEN
5603       --Fetch cash receipt details
5604          l_crcpt_rec.cash_receipt_id        :=      l_ps_rec.cash_receipt_id;
5605          arp_cash_receipts_pkg.fetch_p (l_crcpt_rec);
5606        DECLARE
5607 
5608 	   l_ex_rate l_crcpt_rec.exchange_rate%type := null;
5609        BEGIN
5610 /* BICHATTE CCREF*/
5611 
5612             IF (nvl(p_application_ps_id,0) = -6) THEN
5613                 l_called_from := 'CC_REFUND';
5614             ELSIF   (nvl(p_application_ps_id,0) = -9) THEN
5615                 l_called_from := 'CC_CHARGEBACK';
5616             END IF;
5617 
5618 
5619 	         IF NVL(l_crcpt_rec.exchange_rate_type, 'x') = 'User' THEN
5620 			    l_ex_rate := l_crcpt_rec.exchange_rate;
5621 			 END IF;
5622                 arp_util.debug ( 'cr_rec_id ' || l_ps_rec.cash_receipt_id );
5623                 arp_util.debug ( 'rec_num '|| l_crcpt_rec.receipt_number );
5624                 arp_util.debug ( 'payment_trxn_extension_id '|| l_crcpt_rec.payment_trxn_extension_id );
5625 
5626 	/* Start Bug 7317841 */
5627 	BEGIN
5628 		SELECT payment_trxn_extension_id
5629 		INTO   l_pymnt_trxn_ext_id_temp
5630 		FROM   ar_cash_receipts
5631 		WHERE  cash_receipt_id 	IN (
5632 				SELECT MAX(application_ref_id)
5633 				FROM   ar_receivable_applications
5634 				WHERE  cash_receipt_id = l_ps_rec.cash_receipt_id
5635 				AND    application_type = 'CASH'
5636 				AND    application_ref_type = 'MISC_RECEIPT');
5637 
5638 	    l_crcpt_rec.payment_trxn_extension_id := l_pymnt_trxn_ext_id_temp;
5639             arp_util.debug ( 'Latest payment_trxn_extension_id '|| l_crcpt_rec.payment_trxn_extension_id );
5640 	EXCEPTION
5641 	    WHEN NO_DATA_FOUND THEN
5642 		arp_util.debug ( 'Handled Exception for getting max pymnt_trxn_id - First Refund Case' );
5643 
5644 	    WHEN OTHERS THEN
5645 		     arp_util.debug(  'API EXCEPTION: Getting Max Pymnt_trxn_ext_id ' || SQLERRM);
5646 		     RAISE;
5647 	END;
5648 	/* End Bug 7317841 */
5649 
5650         --Call Misc receipt creation routine.
5651              AR_RECEIPT_API_PUB.create_misc(
5652                 -- IN parameters
5653                    p_api_version=>1.0,
5654                    p_init_msg_list=>l_fnd_api_constants_rec.G_FALSE,
5655                    p_commit=>l_fnd_api_constants_rec.G_FALSE,
5656                    p_validation_level=>l_fnd_api_constants_rec.G_VALID_LEVEL_FULL,
5657                    p_attribute_record=>l_attribute_rec,
5658                    p_global_attribute_record=>l_global_attribute_rec,
5659                    p_receipt_date=>p_apply_date,
5660                    p_amount=>(p_amount_applied * -1),
5661                    p_currency_code=>l_crcpt_rec.currency_code,
5662                    p_exchange_rate_type=>l_crcpt_rec.exchange_rate_type,
5663                    p_exchange_rate=>l_ex_rate,
5664                    p_exchange_rate_date=>l_crcpt_rec.exchange_date,
5665                    p_receipt_method_id=>l_crcpt_rec.receipt_method_id,
5666                    p_remittance_bank_account_id=>l_crcpt_rec.remit_bank_acct_use_id,
5667                    p_receivables_trx_id=>p_receivables_trx_id,
5668                    p_reference_type=>'RECEIPT',
5669                    p_reference_num=>l_crcpt_rec.receipt_number,
5670                    p_reference_id=>l_ps_rec.cash_receipt_id,
5671                    p_comments=>p_comments,
5672            -- OUT NOCOPY or IN/OUT parameters
5673                    x_return_status=>l_return_status,
5674                    x_msg_count=>l_msg_count,
5675                    x_msg_data=>l_msg_data,
5676                    p_receipt_number=>l_application_ref_num,
5677                    p_misc_receipt_id=>l_application_ref_id,
5678                    p_called_from => l_called_from, /* Bug fix 3619780 */
5679                    p_payment_trxn_extension_id => l_crcpt_rec.payment_trxn_extension_id
5680                    );
5681 
5682          /*------------------------------------------------+
5683           | Write API output to the concurrent program log |
5684           +------------------------------------------------*/
5685           IF PG_DEBUG in ('Y', 'C') THEN
5686              arp_util.debug(  'API error count '||to_char(NVL(l_msg_count,0)));
5687           END IF;
5688 
5689           IF NVL(l_msg_count,0)  > 0 Then
5690 
5691              IF l_msg_count  = 1 Then
5692 
5693                 /*------------------------------------------------+
5694                  | There is one message returned by the API, so it|
5695                  | has been sent out NOCOPY in the parameter x_msg_data  |
5696                  +------------------------------------------------*/
5697                  IF PG_DEBUG in ('Y', 'C') THEN
5698                     arp_util.debug(  l_msg_data);
5699                  END IF;
5700 
5701               ELSIF l_msg_count > 1 Then
5702 
5703                      /*-------------------------------------------------------+
5704                       | There are more than one messages returned by the API, |
5705                       | so call them in a loop and print the messages         |
5706                       +-------------------------------------------------------*/
5707 
5708                       FOR l_count IN 1..l_msg_count LOOP
5709 
5710                         l_msg_data := FND_MSG_PUB.Get(FND_MSG_PUB.G_NEXT,
5711                                                                       FND_API.G_FALSE);
5712                         IF PG_DEBUG in ('Y', 'C') THEN
5713                            arp_util.debug(  to_char(l_count)||' : '||l_msg_data);
5714                         END IF;
5715 
5716                       END LOOP;
5717 
5718                END IF; -- l_msg_count
5719 
5720            END IF; -- NVL(l_msg_count,0)
5721 
5722           /*-----------------------------------------------------+
5723            | If API return status is not SUCCESS raise exception |
5724            +-----------------------------------------------------*/
5725            IF l_return_status = FND_API.G_RET_STS_SUCCESS Then
5726 
5727              /*-----------------------------------------------------+
5728               | Success do nothing, else branch introduced to make  |
5729               | sure that NULL case will also raise exception       |
5730               +-----------------------------------------------------*/
5731               NULL;
5732 
5733            ELSE
5734               /*---------------------------+
5735                | Error, raise an exception |
5736                +---------------------------*/
5737                RAISE API_exception;
5738 
5739            END IF; -- l_return_status
5740 
5741           /*----------------------------------+
5742            | APIs propagate exception upwards |
5743            +----------------------------------*/
5744         EXCEPTION
5745               WHEN API_exception THEN
5746                    IF PG_DEBUG in ('Y', 'C') THEN
5747                       arp_util.debug(  'API EXCEPTION: ' ||
5748                              'arp_process_application.activity_application(misc_receipt creation))'
5749                                          ||SQLERRM);
5750                    END IF;
5751                    FND_MSG_PUB.Get (FND_MSG_PUB.G_FIRST, FND_API.G_TRUE,
5752                                            l_msg_data, l_msg_index);
5753                    FND_MESSAGE.Set_Encoded (l_msg_data);
5754                    app_exception.raise_exception;
5755 
5756               WHEN OTHERS THEN
5757                    IF PG_DEBUG in ('Y', 'C') THEN
5758                       arp_util.debug(  'API EXCEPTION: ' ||
5759                              'arp_process_application.activity_application(misc_receipt creation))'
5760                                          ||SQLERRM);
5761                    END IF;
5762                   RAISE;
5763          END; -- Misc receipt creation block
5764     END IF; --End of applied ps id -6 or -9
5765 
5766     --Bug 1634986 Fetch the PS before updating to reflect the changes before receipt.
5767     --The position of the actualy fetch has been changed to fetch it before.
5768 
5769     /* Get UNAPP and ACTIVITY CCIDs */
5770 
5771     OPEN activity_c;
5772     FETCH activity_c INTO activity_rec;
5773 
5774     IF activity_c%NOTFOUND THEN
5775       CLOSE activity_c;
5776       RAISE NO_DATA_FOUND;
5777     END IF;
5778 
5779     CLOSE activity_c;
5780 
5781     /* Update the cash receipt in the payment schedule table. */
5782     arp_ps_util.update_receipt_related_columns(
5783                   p_receipt_ps_id,
5784                   p_amount_applied,
5785                   p_apply_date,
5786                   p_gl_date,
5787                   l_acctd_amount_applied_from,
5788                   NULL_VAR ); -- NULL
5789 
5790     /* Validate that the activity used matches the seeded row */
5791     validate_activity(p_application_ps_id,activity_rec.activity_type);
5792 
5793     l_ra_rec.cash_receipt_id        := activity_rec.cash_receipt_id;
5794 
5795     /* Prepare for 'UNAPP' record insertion with -ve amount applied */
5796 
5797     l_ra_rec.payment_schedule_id    := p_receipt_ps_id;
5798     l_ra_rec.amount_applied         := -p_amount_applied;
5799 
5800 
5801     /* Get the acctd_amount_applied_from value */
5802     /*-------------------------------------------------------------------------+
5803      |Bug 1823299 - accounted amount should be calculated as follows           |
5804      |acctd_amount_applied_from =  acconnted amount due reminaing -            |
5805      |                             ((amount_due_remaining before application   |
5806      |                               - amount_applied)/Exchange rate)          |
5807      |This calculation will avoid leaving small amounts as unapplied eventhough|
5808      |receipt is been fully applied.                                           |
5809      +-------------------------------------------------------------------------*/
5810 
5811     l_func_amount_due_remaining        :=
5812                  ARPCURR.functional_amount((l_ps_rec.amount_due_remaining - l_ra_rec.amount_applied),
5813                                             functional_curr,
5814                                             l_ps_rec.exchange_rate,
5815                                             NULL, NULL );
5816 
5817     l_ra_rec.acctd_amount_applied_from :=   l_ps_rec.acctd_amount_due_remaining - l_func_amount_due_remaining;
5818 
5819    /* Get the acctd_amount_applied_from value */
5820 
5821     IF (p_application_ps_id = -5 AND
5822        p_application_ref_id is not null) THEN
5823 
5824        SELECT invoice_currency_code,
5825               exchange_rate
5826        INTO   l_invoice_currency_code,
5827               l_exchange_rate
5828        FROM   ra_customer_trx
5829        WHERE  customer_trx_id = p_application_ref_id;
5830 
5831        l_ra_rec.acctd_amount_applied_to :=
5832   	ARPCURR.functional_amount(
5833 		  amount	=> l_ra_rec.amount_applied
5834                 , currency_code	=> functional_curr
5835                 , exchange_rate	=> l_exchange_rate
5836                 , precision	=> NULL
5837 		, min_acc_unit	=> NULL );
5838 
5839     --CC Chargeback change
5840     ELSIF ( (p_application_ps_id = -6 OR p_application_ps_id = -9)
5841            AND l_application_ref_id is not null) THEN
5842     --Bug 2103345 - Calculate acctd_amount_applied_to for cc refund using exchange
5843     --rate of Misc.receipt associated.
5844 
5845        SELECT currency_code,
5846               exchange_rate
5847        INTO   l_receipt_currency_code,
5848               l_exchange_rate
5849        FROM   ar_cash_receipts
5850        WHERE  cash_receipt_id = l_application_ref_id;
5851 
5852         l_ra_rec.acctd_amount_applied_to :=
5853   	ARPCURR.functional_amount(
5854 		  amount	=> l_ra_rec.amount_applied
5855                 , currency_code	=> functional_curr
5856                 , exchange_rate	=> l_exchange_rate
5857                 , precision	=> NULL
5858 		, min_acc_unit	=> NULL );
5859     ELSIF (p_receivables_trx_id = -16) THEN
5860       -- Bug 2821139 - Calculate acctd_amount_applied_to for payment netting
5861       -- using exchange rate of applied to receipt
5862 
5863         IF l_netted_receipt_flag = 'N' THEN
5864           SELECT invoice_currency_code,
5865                  exchange_rate
5866           INTO   l_receipt_currency_code,
5867                  l_exchange_rate
5868           FROM   ar_payment_schedules
5869           WHERE  payment_schedule_id = p_application_ps_id;
5870         ELSE
5871           l_exchange_rate := l_ps_rec.exchange_rate;
5872         END IF;
5873 
5874         l_ra_rec.acctd_amount_applied_to :=
5875   	ARPCURR.functional_amount(
5876 		  amount	=> l_ra_rec.amount_applied
5877                 , currency_code	=> functional_curr
5878                 , exchange_rate	=> l_exchange_rate
5879                 , precision	=> NULL
5880 		, min_acc_unit	=> NULL );
5881     END IF;
5882 
5883     IF PG_DEBUG in ('Y', 'C') THEN
5884        arp_standard.debug(   'acctd_amount_applied_from = '||TO_CHAR( l_ra_rec.acctd_amount_applied_from ) );
5885        arp_standard.debug(   'acctd_amount_applied_to = '||TO_CHAR( l_ra_rec.acctd_amount_applied_to ) );
5886     END IF;
5887 
5888     /* Fill in the UNAPP application information */
5889 
5890     l_ra_rec.status                 := 'UNAPP';
5891     l_ra_rec.application_type       := 'CASH';
5892 
5893     l_ra_rec.application_rule       := 'ACTIVITY APPLICATION';
5894     l_ra_rec.program_id	            := -100106;
5895 
5896     l_ra_rec.code_combination_id    := activity_rec.unapplied_ccid;
5897     l_ra_rec.apply_date             := p_apply_date;
5898     l_ra_rec.gl_date                := p_gl_date;
5899     l_ra_rec.posting_control_id     := -3;
5900     l_ra_rec.display                := 'N';
5901     l_ra_rec.application_ref_id     := p_application_ref_id;
5902 
5903     /* 14-APR-2000 jrautiai BR implementation
5904      * For Short Term Debt applications the UNAPP row is not postable */
5905 
5906     IF NVL(p_application_ps_id,0) = -2 THEN
5907       l_ra_rec.postable             := 'N';
5908     ELSE
5909       l_ra_rec.postable             := 'Y';
5910     END IF;
5911 
5912     l_ra_rec.ussgl_transaction_code    := p_ussgl_transaction_code;
5913     l_ra_rec.attribute_category        := p_attribute_category;
5914     l_ra_rec.attribute1                := p_attribute1;
5915     l_ra_rec.attribute2                := p_attribute2;
5916     l_ra_rec.attribute3                := p_attribute3;
5917     l_ra_rec.attribute4                := p_attribute4;
5918     l_ra_rec.attribute5                := p_attribute5;
5919     l_ra_rec.attribute6                := p_attribute6;
5920     l_ra_rec.attribute7                := p_attribute7;
5921     l_ra_rec.attribute8                := p_attribute8;
5922     l_ra_rec.attribute9                := p_attribute9;
5923     l_ra_rec.attribute10               := p_attribute10;
5924     l_ra_rec.attribute11               := p_attribute11;
5925     l_ra_rec.attribute12               := p_attribute12;
5926     l_ra_rec.attribute13               := p_attribute13;
5927     l_ra_rec.attribute14               := p_attribute14;
5928     l_ra_rec.attribute15               := p_attribute15;
5929     l_ra_rec.global_attribute_category := p_global_attribute_category;
5930     l_ra_rec.global_attribute1         := p_global_attribute1;
5931     l_ra_rec.global_attribute2         := p_global_attribute2;
5932     l_ra_rec.global_attribute3         := p_global_attribute3;
5933     l_ra_rec.global_attribute4         := p_global_attribute4;
5934     l_ra_rec.global_attribute5         := p_global_attribute5;
5935     l_ra_rec.global_attribute6         := p_global_attribute6;
5936     l_ra_rec.global_attribute7         := p_global_attribute7;
5937     l_ra_rec.global_attribute8         := p_global_attribute8;
5938     l_ra_rec.global_attribute9         := p_global_attribute9;
5939     l_ra_rec.global_attribute10        := p_global_attribute10;
5940     l_ra_rec.global_attribute11        := p_global_attribute11;
5941     l_ra_rec.global_attribute12        := p_global_attribute12;
5942     l_ra_rec.global_attribute13        := p_global_attribute13;
5943     l_ra_rec.global_attribute14        := p_global_attribute14;
5944     l_ra_rec.global_attribute15        := p_global_attribute15;
5945     l_ra_rec.global_attribute16        := p_global_attribute16;
5946     l_ra_rec.global_attribute17        := p_global_attribute17;
5947     l_ra_rec.global_attribute18        := p_global_attribute18;
5948     l_ra_rec.global_attribute19        := p_global_attribute19;
5949     l_ra_rec.global_attribute20        := p_global_attribute20;
5950     l_ra_rec.comments                  := p_comments;
5951 
5952 
5953     /* Insert UNAPP record */
5954     arp_app_pkg.insert_p( l_ra_rec, l_ra_rec.receivable_application_id );
5955 
5956     IF l_ra_rec.receivable_application_id IS NOT NULL THEN
5957          l_xla_ev_rec.xla_from_doc_id := l_ra_rec.receivable_application_id;
5958          l_xla_ev_rec.xla_to_doc_id   := l_ra_rec.receivable_application_id;
5959          l_xla_ev_rec.xla_mode        := 'O';
5960          l_xla_ev_rec.xla_call        := 'B';
5961          l_xla_ev_rec.xla_doc_table := 'APP';
5962          ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
5963     END IF;
5964 
5965     l_unapp_ra_rec := l_ra_rec;
5966 
5967     /* Set UNAPP id for PAIRING */
5968     l_prev_unapp_id := l_ra_rec.receivable_application_id;
5969 
5970     /* ---------------------------------------------------------------------
5971      * Prepare for 'ACTIVITY' record insertion with +ve amount applied.
5972      * Applied_payment_schedule_id and applied_customer_trx_id are negative
5973      * ie for short term debt -2 and display = 'Y', Only the following
5974      * details change for the 'ACTIVITY' record from the UNAPP record during
5975      * application insertion.
5976      * --------------------------------------------------------------------- */
5977 
5978    /* -------------------------------------------------------------------+
5979     | Balancing segment of ACTIVITY application should be replaced with  |
5980     | that of Receipt's UNAPP record                                     |
5981     +--------------------------------------------------------------------*/
5982     IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'),
5983            'N') = 'N' THEN
5984        arp_util.Substitute_Ccid(
5985                              p_coa_id        => arp_global.chart_of_accounts_id,
5986                              p_original_ccid => activity_rec.activity_ccid    ,
5987                              p_subs_ccid     => activity_rec.unapplied_ccid   ,
5988                              p_actual_ccid   => l_ra_rec.code_combination_id );
5989     ELSE
5990      l_ra_rec.code_combination_id := activity_rec.activity_ccid;
5991     END IF;
5992 
5993     l_ra_rec.receivable_application_id   := NULL; /* filled during act.insert */
5994 --    l_ra_rec.applied_customer_trx_id     := p_application_ps_id;
5995     l_ra_rec.applied_payment_schedule_id := p_application_ps_id;
5996     l_ra_rec.receivables_trx_id          := p_receivables_trx_id;
5997     l_ra_rec.link_to_customer_trx_id     := p_link_to_customer_trx_id;
5998     l_ra_rec.amount_applied              := p_amount_applied;
5999     l_ra_rec.application_rule            := 'ACTIVITY APPLICATION';
6000     l_ra_rec.program_id                  := -100107;
6001 
6002     /* acctd_amount_applied_from is -ve of already calculated
6003      * acctd_amount_applied_from for 'UNAPP' record */
6004 
6005     l_ra_rec.acctd_amount_applied_from   := -l_ra_rec.acctd_amount_applied_from;
6006     l_ra_rec.acctd_amount_applied_to     := -l_ra_rec.acctd_amount_applied_to;
6007 
6008     l_ra_rec.status   := 'ACTIVITY';
6009     l_ra_rec.postable := 'Y';
6010     l_ra_rec.display  := 'Y';
6011     l_ra_rec.application_ref_type       := l_application_ref_type;
6012     l_ra_rec.application_ref_id         := l_application_ref_id;
6013     l_ra_rec.application_ref_num        := l_application_ref_num;
6014     l_ra_rec.secondary_application_ref_id  := l_secondary_application_ref_id;
6015     l_ra_rec.secondary_application_ref_type := l_secondary_app_ref_type;
6016     l_ra_rec.secondary_application_ref_num  := l_secondary_app_ref_num;
6017     l_ra_rec.payment_set_id             := p_payment_set_id;
6018     l_ra_rec.customer_reference         := p_customer_reference ;
6019     l_ra_rec.customer_reason            := p_customer_reason ;
6020     l_ra_rec.application_ref_reason     := l_application_ref_reason;--5450371
6021 
6022     --This is for temporary. When we change the column we will replace this
6023     --also
6024     l_ra_rec.secondary_application_ref_id := l_secondary_application_ref_id;
6025 
6026     l_ra_rec.amount_applied_from       := p_amount_applied;
6027 
6028 
6029     /* Insert ACTIVITY record */
6030     arp_app_pkg.insert_p( l_ra_rec, l_ra_rec.receivable_application_id );
6031 
6032     IF l_ra_rec.receivable_application_id IS NOT NULL THEN
6033          l_xla_ev_rec.xla_from_doc_id := l_ra_rec.receivable_application_id;
6034          l_xla_ev_rec.xla_to_doc_id   := l_ra_rec.receivable_application_id;
6035          l_xla_ev_rec.xla_mode        := 'O';
6036          l_xla_ev_rec.xla_call        := 'B';
6037          l_xla_ev_rec.xla_doc_table := 'APP';
6038          ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
6039     END IF;
6040 
6041    arp_standard.debug('netted cash receipt id = ' || to_char(p_netted_cash_receipt_id));
6042    IF (p_receivables_trx_id = -16) THEN
6043       l_mc_application_ref_id := p_netted_cash_receipt_id;
6044    ELSE
6045       l_mc_application_ref_id := l_application_ref_id;
6046    END IF;
6047    -- Process MRC data if needed before accounting engine is called.
6048 
6049    --apandit
6050    --Bug : 2641517 raise the business event
6051      AR_BUS_EVENT_COVER.Raise_CR_Apply_Event(l_ra_rec.receivable_application_id);
6052 
6053    IF NVL(p_application_ps_id,0) = -2 THEN
6054 
6055      /* In case of Short Term Debt application, the UNAPP record is NOT
6056       * postable so accounting only for activity (ACTIVITY application) */
6057 
6058       l_ae_doc_rec.document_type             := 'RECEIPT';
6059       l_ae_doc_rec.document_id               := l_ra_rec.cash_receipt_id;
6060       l_ae_doc_rec.accounting_entity_level   := 'ONE';
6061       l_ae_doc_rec.source_table              := 'RA';
6062       l_ae_doc_rec.source_id                 := l_ra_rec.receivable_application_id;
6063       l_ae_doc_rec.source_id_old             := '';
6064       l_ae_doc_rec.other_flag                := '';
6065       arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
6066 
6067      /* Bug fix 4910860 */
6068       IF nvl(p_module_name,'RAPI') = 'RAPI' THEN
6069          l_called_from_api := 'Y';
6070       ELSE
6071          l_called_from_api := 'N';
6072       END IF;
6073        arp_balance_check.CHECK_RECP_BALANCE(
6074                     l_ra_rec.cash_receipt_id,
6075                     NULL,
6076                     l_called_from_api);
6077 
6078    ELSE
6079 
6080      /* Otherwise the UNAPP record is postable so create paired accounting */
6081 
6082      /* Release 11.5 VAT changes, create paired UNAPP record accounting
6083       * in ar_distributions */
6084 
6085       l_ae_doc_rec.document_type             := 'RECEIPT';
6086       l_ae_doc_rec.document_id               := l_ra_rec.cash_receipt_id;
6087       l_ae_doc_rec.accounting_entity_level   := 'ONE';
6088       l_ae_doc_rec.source_table              := 'RA';
6089       l_ae_doc_rec.source_id                 := l_prev_unapp_id;
6090       l_ae_doc_rec.source_id_old             := l_ra_rec.receivable_application_id;
6091       l_ae_doc_rec.other_flag                := 'PAIR';
6092       arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
6093 
6094 
6095       /* Release 11.5 VAT changes, create ACTIVITY record accounting
6096        * in ar_distributions */
6097 
6098       l_ae_doc_rec.source_id                 := l_ra_rec.receivable_application_id;
6099       l_ae_doc_rec.source_id_old             := '';
6100       l_ae_doc_rec.other_flag                := '';
6101       arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
6102 
6103      /* Bug fix 4910860 */
6104       IF nvl(p_module_name,'RAPI') = 'RAPI' THEN
6105          l_called_from_api := 'Y';
6106       ELSE
6107          l_called_from_api := 'N';
6108       END IF;
6109       arp_balance_check.CHECK_RECP_BALANCE(
6110                      l_ra_rec.cash_receipt_id,
6111                       NULL,
6112                       l_called_from_api);
6113 
6114    END IF;
6115 
6116 
6117     /* First, set ar_cash_receipt record values to dummy.
6118      * This is to distinguish between updateable NULL and NULL value (dummy)
6119      * which means that column is not to be updated. */
6120 
6121     arp_cash_receipts_pkg.set_to_dummy(l_cr_rec);
6122 
6123     /* ---------------------------------------------------------------------
6124      * Cash receipt must be fully applied in order to set the status
6125      * to 'Applied'.
6126      * --------------------------------------------------------------------- */
6127     IF PG_DEBUG in ('Y', 'C') THEN
6128        arp_standard.debug (  '-- Defining receipt status ...');
6129        arp_standard.debug (  '-- p_amount_applied = '||to_char(p_amount_applied));
6130        arp_standard.debug (  '-- activity_rec.amount_due_remaining = '||to_char(activity_rec.amount_due_remaining));
6131     END IF;
6132 
6133 
6134     /* Determine if the receipt has been fully applied.
6135      * We include the total amount that is On Account as this is
6136      * not included in the Payment Schedules, Amount Due Remaining
6137      * total for the receipt. */
6138 
6139     select nvl(sum(ra.amount_applied),0)
6140     into   l_on_account_total
6141     from   ar_receivable_applications ra
6142     where  ra.cash_receipt_id = l_ra_rec.cash_receipt_id
6143     and    ra.status IN ('ACC','OTHER ACC');
6144 
6145     IF (activity_rec.amount_due_remaining + l_on_account_total + p_amount_applied < 0)
6146     THEN
6147       l_cr_rec.status           := 'UNAPP';
6148     ELSE
6149       l_cr_rec.status           := 'APP';
6150     END IF;
6151 
6152     IF PG_DEBUG in ('Y', 'C') THEN
6153        arp_standard.debug (  '-- status = '|| l_cr_rec.status);
6154     END IF;
6155 
6156     l_cr_rec.cash_receipt_id  := l_ra_rec.cash_receipt_id;
6157 
6158     /* Update cash receipt status. */
6159     arp_cash_receipts_pkg.update_p(l_cr_rec,
6160                                    l_ra_rec.cash_receipt_id);
6161 
6162     /* ---------------------------------------------------------------------
6163      * Update batch status if receipt has a batch
6164      * For Bills Receivable Short Term Debt application this does not do
6165      * anything, since the batch id for the cash receipt history record is
6166      * always NULL. For other type activity applications the called procedure
6167      * needs to be changed to support the new activity application.
6168      * --------------------------------------------------------------------- */
6169     IF (activity_rec.batch_id IS NOT NULL AND Nvl(p_called_from,'*')<>'WRITEOFF')  /*5444407*/
6170     THEN
6171       arp_rw_batches_check_pkg.update_batch_status(activity_rec.batch_id);
6172     END IF;
6173 
6174     /* ---------------------------------------------------------------------
6175      * Return the new receivable application id back to the form
6176      * --------------------------------------------------------------------- */
6177     p_out_rec_application_id     := l_ra_rec.receivable_application_id;
6178     p_application_ref_type       := l_application_ref_type;
6179     p_application_ref_id         := l_application_ref_id;
6180     p_application_ref_num        := l_application_ref_num;
6181     p_secondary_application_ref_id  := l_secondary_application_ref_id;
6182 
6183     IF PG_DEBUG in ('Y', 'C') THEN
6184        arp_standard.debug(  'arp_process_application.activity_application()-' );
6185     END IF;
6186     EXCEPTION
6187          WHEN OTHERS THEN
6188            IF PG_DEBUG in ('Y', 'C') THEN
6189               arp_standard.debug(  'EXCEPTION: arp_process_application.activity_application' );
6190            END IF;
6191               RAISE;
6192 
6193 END activity_application;
6194 
6195 
6196 /*===========================================================================+
6197  | PROCEDURE                                                                 |
6198  |    validate_activity_args                                                 |
6199  |                                                                           |
6200  | DESCRIPTION                                                               |
6201  |    Procedure to validate arguments passed to activity_application()       |
6202  |    procedure.                                                             |
6203  |                                                                           |
6204  | SCOPE - PRIVATE                                                           |
6205  |                                                                           |
6206  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
6207  |                                                                           |
6208  | ARGUMENTS  : IN:                                                          |
6209  |                    p_ps_id              - Id of receipt ps                |
6210  |                    p_application_ps_id  - Id of application ps            |
6211  |                    p_amount_applied     - Amount Applied                  |
6212  |                    p_apply_date         - Application date                |
6213  |                    p_gl_date            - GL date of the application      |
6214  |                    p_receivables_trx_id - Receivable activity ID          |
6215  |              OUT:                                                         |
6216  |                                                                           |
6217  | RETURNS    : NONE                                                         |
6218  |                                                                           |
6219  | MODIFICATION HISTORY - Created by Jani Rautiainen - 14-APR-2000           |
6220  |         Satheesh Nambiar 24-Aug-00 Modified to add a new applied payment  |
6221  |                                    schedule_id of -3 for receipt write-off|
6222  |                                                                           |
6223  +===========================================================================*/
6224 PROCEDURE validate_activity_args(
6225         p_ps_id                    IN ar_payment_schedules.payment_schedule_id%TYPE,
6226         p_application_ps_id        IN ar_receivable_applications.applied_payment_schedule_id%TYPE,
6227         p_link_to_customer_trx_id  IN ar_receivable_applications.link_to_customer_trx_id%TYPE,
6228         p_amount_applied           IN ar_receivable_applications.amount_applied%TYPE,
6229         p_apply_date               IN ar_receivable_applications.apply_date%TYPE,
6230         p_gl_date                  IN ar_receivable_applications.gl_date%TYPE,
6231         p_receivables_trx_id       IN ar_receivable_applications.receivables_trx_id%TYPE) IS
6232 
6233 BEGIN
6234     IF PG_DEBUG in ('Y', 'C') THEN
6235        arp_standard.debug(  'arp_process_application.validate_activity_args()+' );
6236     END IF;
6237 
6238     /* All the arguments must be defined */
6239 
6240     --SNAMBIAR Modified to add a new applied_ps_id -3 for receipt write-off
6241     -- Bug 2751910 - allow for +ve ps_id if netting activity -16
6242 
6243     IF ( p_ps_id IS NULL OR p_apply_date IS NULL OR p_gl_date IS NULL OR p_amount_applied IS NULL
6244          OR p_receivables_trx_id IS NULL OR p_application_ps_id IS NULL
6245          OR (p_receivables_trx_id <> -16 AND p_application_ps_id > -1) )  THEN
6246             FND_MESSAGE.set_name ('AR', 'AR_ARGUEMENTS_FAIL' );
6247             APP_EXCEPTION.raise_exception;
6248     END IF;
6249     IF   p_application_ps_id IN (-2) AND p_link_to_customer_trx_id IS NULL THEN
6250             FND_MESSAGE.set_name ('AR', 'AR_ARGUEMENTS_FAIL' );
6251             APP_EXCEPTION.raise_exception;
6252     END IF;
6253 
6254     IF PG_DEBUG in ('Y', 'C') THEN
6255        arp_standard.debug(  'arp_process_application.validate_activity_args()-' );
6256     END IF;
6257     EXCEPTION
6258          WHEN OTHERS THEN
6259            IF PG_DEBUG in ('Y', 'C') THEN
6260               arp_standard.debug(  'EXCEPTION: arp_process_application.validate_activity_args' );
6261            END IF;
6262            RAISE;
6263 END validate_activity_args;
6264 
6265 /*===========================================================================+
6266  | PROCEDURE                                                                 |
6267  |    fetch_app_ccid                                                         |
6268  |                                                                           |
6269  | DESCRIPTION                                                               |
6270  |    This procedure fetches the CCID for the application. This changed for  |
6271  |    the BR project since the accounting for Bills Receivable document is   |
6272  |    stored in the ar_distributions table instead of the                    |
6273  |    ra_cust_trx_lines_gl_dist table (where the accounting is stored for    |
6274  |    other transactions). This was done to confirm with SLA standards       |
6275  |                                                                           |
6276  | SCOPE - PRIVATE                                                           |
6277  |                                                                           |
6278  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
6279  |                                                                           |
6280  | ARGUMENTS  : IN:                                                          |
6281  |                    p_invoice_ps_id           - Id of transaction ps       |
6282  |                                                                           |
6283  |              OUT:                                                         |
6284  |                    p_applied_customer_trx_id - Id of applied transaction  |
6285  |                    p_code_combination        - CCID for the application   |
6286  |                                                                           |
6287  | RETURNS    : NONE                                                         |
6288  |                                                                           |
6289  | MODIFICATION HISTORY - Created by Jani Rautiainen - 14-APR-2000           |
6290  |                                                                           |
6291  +===========================================================================*/
6292 PROCEDURE fetch_app_ccid(
6293         p_invoice_ps_id           IN  ar_payment_schedules.payment_schedule_id%TYPE,
6294         p_applied_customer_trx_id OUT NOCOPY ar_receivable_applications.applied_customer_trx_id%TYPE,
6295         p_code_combination_id     OUT NOCOPY ar_receivable_applications.code_combination_id%TYPE,
6296         p_source_type             OUT NOCOPY ar_distributions.source_type%TYPE) IS
6297 
6298   /* Cursor to fetch the given transaction information */
6299   CURSOR doc_cur IS
6300     SELECT ps.customer_trx_id,
6301            ps.class,
6302            ps.amount_due_original
6303     FROM ar_payment_schedules ps
6304     WHERE ps.payment_schedule_id = p_invoice_ps_id;
6305 
6306   /* Cursor to fetch normal transaction CCID, the old functionality */
6307   CURSOR trx_dist_cur(l_customer_trx_id ra_customer_trx.customer_trx_id%TYPE) IS
6308     SELECT dist.code_combination_id
6309     FROM ra_cust_trx_line_gl_dist dist
6310     WHERE dist.customer_trx_id = l_customer_trx_id
6311     AND dist.account_class = 'REC'
6312     AND dist.latest_rec_flag = 'Y';
6313 
6314   /* Cursor to fetch BR transaction CCID, new functionality */
6315   CURSOR br_dist_cur(p_transaction_history_id ar_transaction_history.transaction_history_id%TYPE, p_sign NUMBER) IS
6316     SELECT dist.code_combination_id, dist.source_type
6317     FROM ar_distributions dist
6318     WHERE dist.source_id = p_transaction_history_id
6319     AND dist.source_table = 'TH'
6320     AND dist.source_type in ('REC','REMITTANCE','FACTOR','UNPAIDREC')
6321     AND dist.source_id_secondary    IS null
6322     AND dist.source_table_secondary IS null
6323     AND dist.source_type_secondary  IS null
6324     and   (((sign(p_sign) > 0)
6325              and ((nvl(dist.AMOUNT_DR,0) <> 0) OR (nvl(dist.ACCTD_AMOUNT_DR,0) <> 0))
6326              and (nvl(dist.AMOUNT_CR,0) = 0) and (nvl(dist.ACCTD_AMOUNT_CR,0) = 0))
6327         OR ((sign(p_sign) < 0)
6328              and ((nvl(dist.AMOUNT_CR,0) <> 0) OR (nvl(dist.ACCTD_AMOUNT_CR,0) <> 0))
6329              and (nvl(dist.AMOUNT_DR,0) = 0) and (nvl(dist.ACCTD_AMOUNT_DR,0) = 0)))
6330     order by dist.line_id desc;
6331 
6332   /* Cursor to fetch current BR transaction history record */
6333   CURSOR br_current_trh_cur(p_customer_trx_id ra_customer_trx.customer_trx_id%TYPE) IS
6334     SELECT trh.transaction_history_id, trh.postable_flag, trh.event
6335     FROM ar_transaction_history trh
6336     WHERE trh.customer_trx_id = p_customer_trx_id
6337     AND trh.current_accounted_flag = 'Y';
6338 
6339   /* Cursor to fetch previous BR transaction postable transaction history record */
6340   CURSOR br_prev_postable_cur(p_transaction_history_id ar_transaction_history.transaction_history_id%TYPE) IS
6341    SELECT transaction_history_id
6342     FROM ar_transaction_history
6343     WHERE postable_flag = 'Y'
6344     AND event  <> 'MATURITY_DATE'
6345     CONNECT BY PRIOR prv_trx_history_id = transaction_history_id
6346     START WITH transaction_history_id = p_transaction_history_id
6347     ORDER BY transaction_history_id desc;
6348 
6349 
6350  doc_rec               doc_cur%ROWTYPE;
6351  trx_dist_rec          trx_dist_cur%ROWTYPE;
6352  br_dist_rec           br_dist_cur%ROWTYPE;
6353  br_current_trh_rec    br_current_trh_cur%ROWTYPE;
6354  br_prev_postable_rec  br_prev_postable_cur%ROWTYPE;
6355  l_postable_trh_id     ar_transaction_history.transaction_history_id%TYPE;
6356 
6357 BEGIN
6358     IF PG_DEBUG in ('Y', 'C') THEN
6359        arp_standard.debug(  'arp_process_application.fetch_app_ccid()+' );
6360     END IF;
6361 
6362     /* Fetch transaction class to branch out NOCOPY the depending whether
6363      * we are dealing with Bills Receivable or not */
6364     OPEN doc_cur;
6365     FETCH doc_cur INTO doc_rec;
6366     CLOSE doc_cur;
6367 
6368     p_applied_customer_trx_id := doc_rec.customer_trx_id;
6369 
6370     IF NVL(doc_rec.class,'INV') = 'BR' THEN
6371 
6372       /* Fetch current transaction history record */
6373       OPEN br_current_trh_cur(doc_rec.customer_trx_id);
6374       FETCH br_current_trh_cur INTO br_current_trh_rec;
6375 
6376       IF br_current_trh_cur%NOTFOUND THEN
6377         CLOSE br_current_trh_cur;
6378         RAISE NO_DATA_FOUND;
6379       END IF;
6380 
6381       CLOSE br_current_trh_cur;
6382 
6383       IF NVL(br_current_trh_rec.postable_flag,'Y') = 'N' or NVL(br_current_trh_rec.event,'NONE') = 'MATURITY_DATE' THEN
6384 
6385         /* Fetch the previous posted history record */
6386         OPEN br_prev_postable_cur(br_current_trh_rec.transaction_history_id);
6387         FETCH br_prev_postable_cur INTO br_prev_postable_rec;
6388 
6389         IF br_prev_postable_cur%NOTFOUND THEN
6390           CLOSE br_prev_postable_cur;
6391           RAISE NO_DATA_FOUND;
6392         END IF;
6393 
6394         CLOSE br_prev_postable_cur;
6395 
6396         l_postable_trh_id := br_prev_postable_rec.transaction_history_id;
6397 
6398       ELSE
6399 
6400         l_postable_trh_id := br_current_trh_rec.transaction_history_id;
6401 
6402       END IF;
6403 
6404       /* Fetch current CCID */
6405       OPEN br_dist_cur(l_postable_trh_id,sign(doc_rec.amount_due_original));
6406       FETCH br_dist_cur INTO br_dist_rec;
6407 
6408       IF br_dist_cur%NOTFOUND THEN
6409         CLOSE br_dist_cur;
6410         RAISE NO_DATA_FOUND;
6411       END IF;
6412 
6413       CLOSE br_dist_cur;
6414 
6415       p_code_combination_id := br_dist_rec.code_combination_id;
6416       p_source_type         := br_dist_rec.source_type;
6417 
6418     ELSE
6419 
6420       /* Otherwise the accounting is stored in the ra_cust_trx_line_gl_dist table */
6421       OPEN trx_dist_cur(doc_rec.customer_trx_id);
6422       FETCH trx_dist_cur INTO trx_dist_rec;
6423 
6424       IF trx_dist_cur%NOTFOUND THEN
6425         CLOSE trx_dist_cur;
6426         RAISE NO_DATA_FOUND;
6427       END IF;
6428 
6429       CLOSE trx_dist_cur;
6430 
6431       p_code_combination_id := trx_dist_rec.code_combination_id;
6432       p_source_type         := NULL;
6433 
6434     END IF;
6435 
6436     IF PG_DEBUG in ('Y', 'C') THEN
6437        arp_standard.debug(  'arp_process_application.fetch_app_ccid()-' );
6438     END IF;
6439     EXCEPTION
6440          WHEN OTHERS THEN
6441 		   IF PG_DEBUG in ('Y', 'C') THEN
6442 		      arp_standard.debug(  SQLERRM(SQLCODE));
6443               arp_standard.debug(  'EXCEPTION: arp_process_application.fetch_app_ccid' );
6444            END IF;
6445            RAISE;
6446 END fetch_app_ccid;
6447 
6448 /*===========================================================================+
6449  | PROCEDURE                                                                 |
6450  |    validate_activity                                                      |
6451  |                                                                           |
6452  | DESCRIPTION                                                               |
6453  |    Procedure to validate activity ID given matches the seeded data        |
6454  |                                                                           |
6455  | SCOPE - PRIVATE                                                           |
6456  |                                                                           |
6457  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
6458  |                                                                           |
6459  | ARGUMENTS  : IN:                                                          |
6460  |                    p_application_ps_id  - Id of application ps            |
6461  |                    p_activity_type      - Receivable activity type        |
6462  |              OUT:                                                         |
6463  |                                                                           |
6464  | RETURNS    : NONE                                                         |
6465  |                                                                           |
6466  | MODIFICATION HISTORY - Created by Jani Rautiainen - 14-APR-2000           |
6467  |         Satheesh Nambiar 24-Aug-00 Modified to add a new applied payment  |
6468  |                                    schedule_id of -3 for receipt write-off|
6469  |                                                                           |
6470  +===========================================================================*/
6471 PROCEDURE validate_activity(
6472         p_application_ps_id  IN ar_receivable_applications.applied_payment_schedule_id%TYPE,
6473         p_activity_type      IN ar_receivables_trx.type%TYPE) IS
6474 
6475 BEGIN
6476     IF PG_DEBUG in ('Y', 'C') THEN
6477        arp_standard.debug(  'arp_process_application.validate_activity()+' );
6478     END IF;
6479 
6480     /* Activity and seeded ps must match. */
6481   --SNAMBIAR Added a new validation for applied PS id -3
6482     IF    (p_application_ps_id = (-2) and p_activity_type <> 'SHORT_TERM_DEBT' )
6483        OR (p_application_ps_id = (-3) and p_activity_type <> 'WRITEOFF' )
6484        OR (p_application_ps_id = (-6) and p_activity_type <> 'CCREFUND' )
6485        OR (p_application_ps_id = (-5) and p_activity_type <> 'ADJUST' )
6486        OR (p_application_ps_id = (-8) and p_activity_type <> 'CM_REFUND' )
6487     THEN
6488          FND_MESSAGE.set_name ('AR', 'AR_ARGUEMENTS_FAIL' );
6489          APP_EXCEPTION.raise_exception;
6490 
6491     END IF;
6492 
6493     IF PG_DEBUG in ('Y', 'C') THEN
6494        arp_standard.debug(  'arp_process_application.validate_activity()-' );
6495     END IF;
6496     EXCEPTION
6497          WHEN OTHERS THEN
6498            IF PG_DEBUG in ('Y', 'C') THEN
6499               arp_standard.debug(  'EXCEPTION: arp_process_application.validate_activity' );
6500            END IF;
6501            RAISE;
6502 END validate_activity;
6503 
6504 /*===========================================================================+
6505  | PROCEDURE                                                                 |
6506  |    unapp_postable                                                         |
6507  |                                                                           |
6508  | DESCRIPTION                                                               |
6509  |    Function checking whether the UNAPP row is postable or not.            |
6510  |    The unapp record is not postable if the BR transaction was closed      |
6511  |    by a application through a risk_elimination event                      |
6512  |                                                                           |
6513  | SCOPE - PRIVATE                                                           |
6514  |                                                                           |
6515  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED - NONE                            |
6516  |                                                                           |
6517  | ARGUMENTS  : IN:                                                          |
6518  |                    p_applied_customer_trx_id  - Id of application trx     |
6519  |                    p_applied_ps_id            - Id of application ps      |
6520  |              OUT:                                                         |
6521  |                    None                                                   |
6522  |                                                                           |
6523  | RETURNS    : TRUE  - If the UNAPP row is postable                         |
6524  |              FALSE - If the UNAPP row is not postable                     |
6525  | MODIFICATION HISTORY - Created by Jani Rautiainen - 09-OCT-2000           |
6526  |                                                                           |
6527  +===========================================================================*/
6528 FUNCTION unapp_postable(p_applied_customer_trx_id  IN ar_receivable_applications.applied_customer_trx_id%TYPE,
6529                         p_applied_ps_id            IN ar_receivable_applications.applied_payment_schedule_id%TYPE) RETURN BOOLEAN IS
6530 
6531   CURSOR trx_class_cur IS
6532     SELECT ps.class
6533     FROM ar_payment_schedules ps
6534     WHERE ps.payment_schedule_id     = p_applied_ps_id;
6535 
6536   CURSOR current_BR_cur IS
6537     SELECT trh.status, trh.event
6538     FROM ar_transaction_history trh
6539     WHERE trh.customer_trx_id     = p_applied_customer_trx_id
6540     AND   trh.current_record_flag = 'Y';
6541 
6542   trx_class_rec  trx_class_cur%ROWTYPE;
6543   current_BR_rec current_BR_cur%ROWTYPE;
6544 
6545 BEGIN
6546     IF PG_DEBUG in ('Y', 'C') THEN
6547        arp_standard.debug(  'arp_process_application.unapp_postable()+' );
6548     END IF;
6549 
6550     IF p_applied_customer_trx_id  IS NULL OR p_applied_ps_id IS NULL OR p_applied_ps_id = -1 THEN
6551 
6552       RETURN TRUE;
6553 
6554     END IF;
6555 
6556     OPEN trx_class_cur;
6557     FETCH trx_class_cur INTO trx_class_rec;
6558     CLOSE trx_class_cur;
6559 
6560     IF NVL(trx_class_rec.class,'INV') = 'BR' THEN
6561 
6562       OPEN current_BR_cur;
6563       FETCH current_BR_cur INTO current_BR_rec;
6564       CLOSE current_BR_cur;
6565 
6566       IF current_BR_rec.status = 'CLOSED' AND current_BR_rec.event = 'RISK_ELIMINATED' THEN
6567         RETURN FALSE;
6568       ELSE
6569         RETURN TRUE;
6570       END IF;
6571 
6572     ELSE
6573 
6574       RETURN TRUE;
6575 
6576     END IF;
6577 
6578     IF PG_DEBUG in ('Y', 'C') THEN
6579        arp_standard.debug(  'arp_process_application.unapp_postable()-' );
6580     END IF;
6581     EXCEPTION
6582          WHEN OTHERS THEN
6583            IF PG_DEBUG in ('Y', 'C') THEN
6584               arp_standard.debug(  'EXCEPTION: arp_process_application.unapp_postable' );
6585            END IF;
6586            RAISE;
6587 END unapp_postable;
6588 
6589 /*===========================================================================+
6590  | PROCEDURE                                                                 |
6591  |    other_account_application                                              |
6592  |                                                                           |
6593  | DESCRIPTION                                                               |
6594  |    Do all actions neccessary to insert rows into AR_RA table during       |
6595  |    Other Application. No PS table row is updated, However                 |
6596  |    2 RA rows are inserted - One as an UNAPP row and another as 'OTHER ACC'|
6597  |    application status OTHER ACC bahaves the same way as on-account ACC    |
6598  |    This new procedure is introduced for creating special applications like|
6599  |    claim and prepayment.                                                  |
6600  |                                                                           |
6601  | SCOPE - PUBLIC                                                            |
6602  |                                                                           |
6603  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
6604  |         ARPCURR.functional_amount - Get the acctd amount of amount applied|
6605  |         arp_ps_pkg.fetch_p - Fetch a PS row                               |
6606  |         arp_app_pkg.insert_p - Insert a row into RA table                 |
6607  |                                                                           |
6608  | ARGUMENTS  : IN:                                                          |
6609  |                 p_ps_id - PS id of the receipt                            |
6610  |                 p_amount_applied - Input amount applied                   |
6611  |                 p_apply_date - Application date                           |
6612  |                 p_gl_date    - Gl Date                                    |
6613  |                 p_receivables_trx_id  -Activity id                        |
6614  |                 p_applied_ps_id       - Applied payment schedule id -4,-7 |
6615  |                 p_ussgl_transaction_code - USSGL transaction code         |
6616  |                 OTHER DESCRIPTIVE FLEX columns                            |
6617  |                 p_module_name  - Name of the module that called this      |
6618  |                                  procedure                                |
6619  |                 p_module_version  - Version of the module that called this|
6620  |                                  procedure                                |
6621  |              OUT:                                                         |
6622  |		   p_out_rec_application_id                                  |
6623  |				Returned receivable application id           |
6624  | RETURNS    : NONE                                                         |
6625  |                                                                           |
6626  | NOTES  -                                                                  |
6627  |                                                                           |
6628  | MODIFICATION HISTORY                                                      |
6629  | 17-APR-01	S Nambiar	Created                                      |
6630  | 01-Jun-01    S.Nambiar       Bug 1811261 - OTHER ACC should derive the ccid
6631  |                              from activity selected                       |
6632  | 7-Sep-01     S.Nambiar       Added applied_ps_id,payment_set_id parameter |
6633  |                              to support prepayment applications.          |
6634  | 03-Sep-02   	Debbie Jancis   Modified for mrc trigger replacement.        |
6635  | 				added processing for receivable apps         |
6636  | 07-AUG-2003  Jon Beckett     Bug 3087819 - added p_called_from parameter  |
6637  |			        Claim is not created/updated if called from  |
6638  |                              Trade Management.                            |
6639  | 30-JUN-2004  Jon Beckett     Removed RAISE from handling of trade_mgt_err |
6640  |				to ensure TM errors are displayed correctly  |
6641  | 06-AUG-2004  Jon Beckett     Bug 3643551 - index on applied_ps_id         |
6642  |				ignored to ensure index on cash_receipt_id is|
6643  |				used in query on ar_receivable_applications  |
6644  | 03-MAR-2005  JASSING 	Added the code to check for the profile	     |
6645  |				option 'AR:Disable Receivable Activity       |
6646  |				Balancing Segment Substitution' for Claims   |
6647  |				and Prepayments. Bug Fix 4025652.	     |
6648  | 14-OCT-2005  Jon Beckett     Bug 4565758 - legal entity passed to TM      |
6649  +===========================================================================*/
6650 PROCEDURE other_account_application(
6651         p_receipt_ps_id   IN ar_payment_schedules.payment_schedule_id%TYPE,
6652         p_amount_applied IN
6653                 ar_receivable_applications.amount_applied%TYPE,
6654         p_apply_date IN ar_receivable_applications.apply_date%TYPE,
6655         p_gl_date IN ar_receivable_applications.gl_date%TYPE,
6656         p_receivables_trx_id ar_receivable_applications.receivables_trx_id%TYPE,
6657         p_applied_ps_id  IN ar_receivable_applications.applied_payment_schedule_id%TYPE,
6658         p_ussgl_transaction_code IN
6659                 ar_receivable_applications.ussgl_transaction_code%TYPE,
6660         p_application_ref_type IN
6661                 ar_receivable_applications.application_ref_type%TYPE,
6662         p_application_ref_id IN
6663                 ar_receivable_applications.application_ref_id%TYPE,
6664         p_application_ref_num IN
6665                 ar_receivable_applications.application_ref_num%TYPE,
6666         p_secondary_application_ref_id IN NUMBER,
6667         p_comments IN
6668                 ar_receivable_applications.comments%TYPE,
6669         p_attribute_category IN
6670                 ar_receivable_applications.attribute_category%TYPE,
6671         p_attribute1 IN ar_receivable_applications.attribute1%TYPE,
6672         p_attribute2 IN ar_receivable_applications.attribute2%TYPE,
6673         p_attribute3 IN ar_receivable_applications.attribute3%TYPE,
6674         p_attribute4 IN ar_receivable_applications.attribute4%TYPE,
6675         p_attribute5 IN ar_receivable_applications.attribute5%TYPE,
6676         p_attribute6 IN ar_receivable_applications.attribute6%TYPE,
6677         p_attribute7 IN ar_receivable_applications.attribute7%TYPE,
6678         p_attribute8 IN ar_receivable_applications.attribute8%TYPE,
6679         p_attribute9 IN ar_receivable_applications.attribute9%TYPE,
6680         p_attribute10 IN ar_receivable_applications.attribute10%TYPE,
6681         p_attribute11 IN ar_receivable_applications.attribute11%TYPE,
6682         p_attribute12 IN ar_receivable_applications.attribute12%TYPE,
6683         p_attribute13 IN ar_receivable_applications.attribute13%TYPE,
6684         p_attribute14 IN ar_receivable_applications.attribute14%TYPE,
6685         p_attribute15 IN ar_receivable_applications.attribute15%TYPE,
6686         p_global_attribute_category IN ar_receivable_applications.global_attribute_category%TYPE,
6687         p_global_attribute1 IN ar_receivable_applications.global_attribute1%TYPE,
6688         p_global_attribute2 IN ar_receivable_applications.global_attribute2%TYPE,
6689         p_global_attribute3 IN ar_receivable_applications.global_attribute3%TYPE,
6690         p_global_attribute4 IN ar_receivable_applications.global_attribute4%TYPE,
6691         p_global_attribute5 IN ar_receivable_applications.global_attribute5%TYPE,
6692         p_global_attribute6 IN ar_receivable_applications.global_attribute6%TYPE,
6693         p_global_attribute7 IN ar_receivable_applications.global_attribute7%TYPE,
6694         p_global_attribute8 IN ar_receivable_applications.global_attribute8%TYPE,
6695         p_global_attribute9 IN ar_receivable_applications.global_attribute9%TYPE,
6696         p_global_attribute10 IN ar_receivable_applications.global_attribute10%TYPE,
6697         p_global_attribute11 IN ar_receivable_applications.global_attribute11%TYPE,
6698         p_global_attribute12 IN ar_receivable_applications.global_attribute12%TYPE,
6699         p_global_attribute13 IN ar_receivable_applications.global_attribute13%TYPE,
6700         p_global_attribute14 IN ar_receivable_applications.global_attribute14%TYPE,
6701         p_global_attribute15 IN ar_receivable_applications.global_attribute15%TYPE,
6702         p_global_attribute16 IN ar_receivable_applications.global_attribute16%TYPE,
6703         p_global_attribute17 IN ar_receivable_applications.global_attribute17%TYPE,
6704         p_global_attribute18 IN ar_receivable_applications.global_attribute18%TYPE,
6705         p_global_attribute19 IN ar_receivable_applications.global_attribute19%TYPE,
6706         p_global_attribute20 IN ar_receivable_applications.global_attribute20%TYPE,
6707         p_module_name IN VARCHAR2,
6708         p_module_version IN VARCHAR2,
6709         p_payment_set_id   IN ar_receivable_applications.payment_set_id%TYPE,
6710         x_application_ref_id OUT NOCOPY
6711                 ar_receivable_applications.application_ref_id%TYPE,
6712         x_application_ref_num OUT NOCOPY
6713                 ar_receivable_applications.application_ref_num%TYPE
6714         , x_return_status               OUT NOCOPY VARCHAR2
6715         , x_msg_count                   OUT NOCOPY NUMBER
6716         , x_msg_data                    OUT NOCOPY VARCHAR2
6717 	, p_out_rec_application_id	OUT NOCOPY NUMBER
6718         , p_application_ref_reason IN ar_receivable_applications.application_ref_reason%TYPE
6719         , p_customer_reference     IN ar_receivable_applications.customer_reference%TYPE
6720         , p_customer_reason        IN ar_receivable_applications.customer_reason%TYPE
6721         , x_claim_reason_name      OUT NOCOPY VARCHAR2
6722 	, p_called_from		   IN  VARCHAR2) IS
6723 
6724 l_ra_rec   ar_receivable_applications%ROWTYPE;
6725 l_ps_rec   ar_payment_schedules%ROWTYPE;
6726 
6727 l_cr_rec                 ar_cash_receipts%ROWTYPE;
6728 l_amount_due_remaining   NUMBER;
6729 ln_batch_id	         NUMBER;
6730 functional_curr          VARCHAR2(100);
6731 
6732 l_activity_cc_id  ar_receipt_method_accounts.on_account_ccid%TYPE;
6733 l_unapp_cc_id  ar_receipt_method_accounts.unapplied_ccid%TYPE;
6734 
6735 l_on_account_total      NUMBER;
6736 l_ae_doc_rec            ae_doc_rec_type;
6737 l_prev_unapp_id         NUMBER;
6738 l_currency_code         ar_cash_receipts.currency_code%TYPE;
6739 l_exchange_rate_type    ar_cash_receipts.exchange_rate_type%TYPE;
6740 l_exchange_rate_date    ar_cash_receipts.exchange_date%TYPE;
6741 l_exchange_rate         ar_cash_receipts.exchange_rate%TYPE;
6742 l_customer_id           ar_cash_receipts.pay_from_customer%TYPE;
6743 l_customer_site_use_id  ar_cash_receipts.customer_site_use_id%TYPE;
6744 l_receipt_number        ar_cash_receipts.receipt_number%TYPE;
6745 l_claim_reason_code_id  NUMBER;
6746 l_claim_reason_name     VARCHAR2(80);
6747 l_amount_applied        NUMBER;
6748 l_legal_entity_id       NUMBER;
6749 l_called_from_api       VARCHAR2(1);
6750 
6751 trade_mgt_err           EXCEPTION;
6752 
6753   --Bug#2750340
6754   l_xla_ev_rec      arp_xla_events.xla_events_type;
6755   l_xla_doc_table   VARCHAR2(20);
6756 
6757 BEGIN
6758     IF PG_DEBUG in ('Y', 'C') THEN
6759        arp_standard.debug(   'arp_process_application.other_account_application()+' );
6760     END IF;
6761     x_return_status := FND_API.G_RET_STS_SUCCESS;
6762 
6763     IF PG_DEBUG in ('Y', 'C') THEN
6764        arp_standard.debug(   '-- p_receipt_ps_id = '||TO_CHAR(p_receipt_ps_id));
6765        arp_standard.debug(   '-- p_amount_applied = '||
6766                         TO_CHAR( p_amount_applied ) );
6767        arp_standard.debug(   '-- p_gl_date = '|| TO_CHAR( p_gl_date ) );
6768        arp_standard.debug(   '-- p_apply_date = '|| TO_CHAR( p_apply_date ) );
6769     END IF;
6770     --
6771     IF ( p_module_name IS NOT NULL AND p_module_version IS NOT NULL ) THEN
6772          validate_on_account_args( p_receipt_ps_id, p_amount_applied,
6773                                    p_apply_date, p_gl_date );
6774     END IF;
6775     --
6776     arp_ps_pkg.fetch_p( p_receipt_ps_id, l_ps_rec );
6777 
6778     functional_curr := arp_global.functional_currency;
6779     -- ---------------------------------------------------------------------
6780     -- Get UNAPP and OTHER ACC CC'Ids by selecting from receipt method accounts
6781     -- table
6782     -- ---------------------------------------------------------------------
6783     --Bug 1811261 - OTHER ACC should take the ccid from the activity
6784     SELECT ps.cash_receipt_id,
6785            ps.amount_due_remaining,
6786            rt.code_combination_id activity_ccid,
6787            rma.unapplied_ccid,
6788 	   crh.batch_id,
6789            cr.currency_code
6790          , cr.exchange_rate_type
6791          , cr.exchange_date
6792          , cr.exchange_rate
6793          , cr.pay_from_customer
6794          , cr.customer_site_use_id
6795          , cr.receipt_number
6796          , cr.legal_entity_id
6797     INTO     l_ra_rec.cash_receipt_id
6798            , l_amount_due_remaining
6799            , l_activity_cc_id
6800            , l_unapp_cc_id
6801 	   , ln_batch_id
6802            , l_currency_code
6803 	   , l_exchange_rate_type
6804 	   , l_exchange_rate_date
6805 	   , l_exchange_rate
6806 	   , l_customer_id
6807 	   , l_customer_site_use_id
6808            , l_receipt_number
6809            , l_legal_entity_id
6810     FROM     ar_payment_schedules 	ps
6811            , ar_cash_receipts 		cr
6812 	   , ar_cash_receipt_history	crh
6813            , ar_receipt_methods 	rm
6814            , ce_bank_acct_uses 		ba
6815            , ar_receipt_method_accounts rma
6816            , ar_receivables_trx         rt
6817     WHERE  ps.payment_schedule_id 	= p_receipt_ps_id
6818     AND    cr.cash_receipt_id 		= ps.cash_receipt_id
6819     AND	   crh.cash_receipt_id		= cr.cash_receipt_id
6820     AND	   crh.current_record_flag	= 'Y'
6821     AND    rm.receipt_method_id 	= cr.receipt_method_id
6822     AND    ba.bank_acct_use_id 		= cr.remit_bank_acct_use_id
6823     AND    rma.remit_bank_acct_use_id 	= ba.bank_acct_use_id
6824     AND    rma.receipt_method_id 	= rm.receipt_method_id
6825     AND    rt.receivables_trx_id        = p_receivables_trx_id;
6826 
6827     -----------------------------------------------------
6828     -- Bug 1775823 iClaim/deductions
6829     -- Check for receipt being applied to a claim investigation for a given
6830     -- claim number more than once
6831     -----------------------------------------------------
6832     IF NVL(p_applied_ps_id,0) = -4 THEN
6833 
6834        DECLARE
6835          l_found   varchar2(1) := 'N';
6836        BEGIN
6837 
6838        -- Bug 3643551: use of index on applied_ps_id prevented
6839         SELECT 'Y'
6840         INTO   l_found
6841         FROM   ar_receivable_applications rap
6842         WHERE  rap.cash_receipt_id = l_ra_rec.cash_receipt_id
6843         AND    rap.applied_payment_schedule_id + 0 = -4
6844         AND    rap.secondary_application_ref_id = p_secondary_application_ref_id
6845         AND    rap.display = 'Y'
6846         AND    rap.status = 'OTHER ACC';
6847 
6848        IF l_found = 'Y' THEN
6849         raise too_many_rows;
6850        END IF;
6851 
6852       EXCEPTION
6853       WHEN no_data_found THEN
6854         null;
6855       WHEN too_many_rows THEN
6856         x_return_status := FND_API.G_RET_STS_ERROR;
6857         FND_MESSAGE.set_name ('AR', 'AR_RW_APP_CLAIM_MULTI_ASSIGN' );
6858         APP_EXCEPTION.raise_exception;
6859       END;
6860     END IF;
6861 
6862     -- Get the current amount that is 'on account' application for this receipt.
6863     -- This is used later on to determine it the receipt has been
6864     -- fully applied.  We need to do the sum now so we don't include
6865     -- the claim application row that we are about to create.
6866     --
6867     BEGIN
6868        select nvl(sum(ra.amount_applied),0)
6869        into   l_on_account_total
6870        from   ar_receivable_applications ra
6871        where  ra.cash_receipt_id = l_ra_rec.cash_receipt_id
6872        and    ra.status IN ('ACC','OTHER ACC');
6873     EXCEPTION
6874      WHEN NO_DATA_FOUND then
6875         l_on_account_total := 0;
6876     END;
6877 
6878     -- Prepare for 'UNAPP' record insertion with -ve amount applied
6879     -- applied_customer_trx_id is NULL and display = 'N'
6880     --
6881     l_ra_rec.payment_schedule_id := p_receipt_ps_id;
6882     l_ra_rec.amount_applied := -p_amount_applied;
6883     --
6884     -- Get the acctd_amount_applied_from value
6885     --
6886     l_ra_rec.acctd_amount_applied_from :=
6887                  ARPCURR.functional_amount( l_ra_rec.amount_applied,
6888                                             functional_curr,
6889                                             l_ps_rec.exchange_rate,
6890                                             NULL, NULL );
6891     IF PG_DEBUG in ('Y', 'C') THEN
6892        arp_standard.debug(   'acctd_amount_applied_from = '||
6893                          TO_CHAR( l_ra_rec.acctd_amount_applied_from ) );
6894     END IF;
6895     --
6896     l_ra_rec.status := 'UNAPP';
6897     l_ra_rec.application_type := 'CASH';
6898 
6899     l_ra_rec.application_rule := '60.7';
6900     l_ra_rec.program_id	:= -100106;
6901 
6902     l_ra_rec.code_combination_id := l_unapp_cc_id;
6903     l_ra_rec.apply_date := p_apply_date;
6904     l_ra_rec.gl_date := p_gl_date;
6905     l_ra_rec.posting_control_id := -3;
6906     l_ra_rec.display := 'N';
6907     l_ra_rec.postable := 'Y';
6908     l_ra_rec.ussgl_transaction_code := p_ussgl_transaction_code;
6909     l_ra_rec.attribute_category := p_attribute_category;
6910     l_ra_rec.attribute1 := p_attribute1;
6911     l_ra_rec.attribute2 := p_attribute2;
6912     l_ra_rec.attribute3 := p_attribute3;
6913     l_ra_rec.attribute4 := p_attribute4;
6914     l_ra_rec.attribute5 := p_attribute5;
6915     l_ra_rec.attribute6 := p_attribute6;
6916     l_ra_rec.attribute7 := p_attribute7;
6917     l_ra_rec.attribute8 := p_attribute8;
6918     l_ra_rec.attribute9 := p_attribute9;
6919     l_ra_rec.attribute10 := p_attribute10;
6920     l_ra_rec.attribute11 := p_attribute11;
6921     l_ra_rec.attribute12 := p_attribute12;
6922     l_ra_rec.attribute13 := p_attribute13;
6923     l_ra_rec.attribute14 := p_attribute14;
6924     l_ra_rec.attribute15 := p_attribute15;
6925     l_ra_rec.global_attribute_category := p_global_attribute_category;
6926     l_ra_rec.global_attribute1 := p_global_attribute1;
6927     l_ra_rec.global_attribute2 := p_global_attribute2;
6928     l_ra_rec.global_attribute3 := p_global_attribute3;
6929     l_ra_rec.global_attribute4 := p_global_attribute4;
6930     l_ra_rec.global_attribute5 := p_global_attribute5;
6931     l_ra_rec.global_attribute6 := p_global_attribute6;
6932     l_ra_rec.global_attribute7 := p_global_attribute7;
6933     l_ra_rec.global_attribute8 := p_global_attribute8;
6934     l_ra_rec.global_attribute9 := p_global_attribute9;
6935     l_ra_rec.global_attribute10 := p_global_attribute10;
6936     l_ra_rec.global_attribute11 := p_global_attribute11;
6937     l_ra_rec.global_attribute12 := p_global_attribute12;
6938     l_ra_rec.global_attribute13 := p_global_attribute13;
6939     l_ra_rec.global_attribute14 := p_global_attribute14;
6940     l_ra_rec.global_attribute15 := p_global_attribute15;
6941     l_ra_rec.global_attribute16 := p_global_attribute16;
6942     l_ra_rec.global_attribute17 := p_global_attribute17;
6943     l_ra_rec.global_attribute18 := p_global_attribute18;
6944     l_ra_rec.global_attribute19 := p_global_attribute19;
6945     l_ra_rec.global_attribute20 := p_global_attribute20;
6946     --Bug 1814683.
6947     l_ra_rec.comments  := p_comments;
6948     --
6949     -- Insert UNAPP record
6950     --
6951     arp_app_pkg.insert_p( l_ra_rec, l_ra_rec.receivable_application_id );
6952 
6953     IF l_ra_rec.receivable_application_id IS NOT NULL THEN
6954          l_xla_ev_rec.xla_from_doc_id := l_ra_rec.receivable_application_id;
6955          l_xla_ev_rec.xla_to_doc_id   := l_ra_rec.receivable_application_id;
6956          l_xla_ev_rec.xla_mode        := 'O';
6957          l_xla_ev_rec.xla_call        := 'B';
6958          l_xla_ev_rec.xla_doc_table := 'APP';
6959          ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
6960     END IF;
6961 
6962 
6963     --Set UNAPP id for PAIRING
6964     l_prev_unapp_id := l_ra_rec.receivable_application_id;
6965 
6966     -- ---------------------------------------------------------------------
6967     -- Prepare for 'OTHER ACC' record insertion with +ve amount applied
6968     -- applied_payment_schedule_id and applied_customer_trx_id are -1 and
6969     -- display = 'Y', Only the following details change for the 'OTHER ACC' record
6970     -- from the UNAPP record during claim application.
6971     -- ---------------------------------------------------------------------
6972 
6973     l_ra_rec.receivable_application_id := NULL; /* filled during act. insert */
6974     l_ra_rec.applied_customer_trx_id := -1;
6975     l_ra_rec.applied_payment_schedule_id := p_applied_ps_id;
6976   /*  l_ra_rec.code_combination_id := l_activity_cc_id;  Bug 4025652 */
6977     l_ra_rec.amount_applied := p_amount_applied;
6978     l_ra_rec.application_rule := '60.0';
6979     l_ra_rec.program_id := -100107;
6980 
6981     --
6982     -- acctd_amount_applied_from is -ve of already calculated
6983     -- acctd_amount_applied_from for 'UNAPP' record
6984     --
6985     l_ra_rec.acctd_amount_applied_from := -l_ra_rec.acctd_amount_applied_from;
6986     --
6987     l_ra_rec.status := 'OTHER ACC';
6988     l_ra_rec.receivables_trx_id := p_receivables_trx_id;
6989     l_ra_rec.display := 'Y';
6990     l_ra_rec.application_ref_type := p_application_ref_type;
6991     l_ra_rec.application_ref_id := p_application_ref_id;
6992     l_ra_rec.application_ref_num := p_application_ref_num;
6993     l_ra_rec.secondary_application_ref_id := p_secondary_application_ref_id;
6994     l_ra_rec.payment_set_id               := p_payment_set_id;
6995     l_ra_rec.application_ref_reason := p_application_ref_reason;
6996     l_ra_rec.customer_reference := p_customer_reference;
6997     l_ra_rec.customer_reason := p_customer_reason;
6998     --
6999 
7000    /* bug fix 4025652 */
7001 
7002     IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'),'N') = 'N' THEN
7003         arp_util.Substitute_Ccid(
7004                              p_coa_id        => arp_global.chart_of_accounts_id,
7005                              p_original_ccid => l_activity_cc_id    ,
7006                              p_subs_ccid     => l_unapp_cc_id   ,
7007                              p_actual_ccid   => l_ra_rec.code_combination_id );
7008     ELSE
7009      l_ra_rec.code_combination_id := l_activity_cc_id;
7010     END IF;
7011    /* End of bug fix 4025652 */
7012     /*Bug 5495310. Added p_apply_date*/
7013 
7014     IF (l_ra_rec.application_ref_type = 'CLAIM' AND
7015         NVL(p_called_from,'RAPI') <> 'TRADE_MANAGEMENT') THEN
7016        IF (l_ra_rec.application_ref_num IS NULL) THEN
7017          create_claim(
7018               p_amount               => p_amount_applied
7019             , p_amount_applied       => p_amount_applied
7020             , p_currency_code        => l_currency_code
7021             , p_exchange_rate_type   => l_exchange_rate_type
7022             , p_exchange_rate_date   => l_exchange_rate_date
7023             , p_exchange_rate        => l_exchange_rate
7024             , p_customer_trx_id      => NULL
7025             , p_invoice_ps_id        => NULL
7026             , p_cust_trx_type_id     => NULL
7027             , p_trx_number           => NULL
7028             , p_cust_account_id      => l_customer_id
7029             , p_bill_to_site_id      => l_customer_site_use_id
7030             , p_ship_to_site_id      => NULL
7031             , p_salesrep_id          => NULL   -- Bug 2361331
7032             , p_customer_ref_date    => NULL
7033             , p_customer_ref_number  => p_customer_reference
7034             , p_cash_receipt_id      => l_ra_rec.cash_receipt_id
7035             , p_receipt_number       => l_receipt_number
7036             , p_comments             => p_comments
7037             , p_reason_id            => p_application_ref_reason
7038             , p_customer_reason      => p_customer_reason
7039             , p_apply_date           => p_apply_date
7040             , p_attribute_category   => p_attribute_category
7041             , p_attribute1           => p_attribute1
7042             , p_attribute2           => p_attribute2
7043             , p_attribute3           => p_attribute3
7044             , p_attribute4           => p_attribute4
7045             , p_attribute5           => p_attribute5
7046             , p_attribute6           => p_attribute6
7047             , p_attribute7           => p_attribute7
7048             , p_attribute8           => p_attribute8
7049             , p_attribute9           => p_attribute9
7050             , p_attribute10          => p_attribute10
7051             , p_attribute11          => p_attribute11
7052             , p_attribute12          => p_attribute12
7053             , p_attribute13          => p_attribute13
7054             , p_attribute14          => p_attribute14
7055             , p_attribute15          => p_attribute15
7056             , x_return_status        => x_return_status
7057             , x_msg_count            => x_msg_count
7058             , x_msg_data             => x_msg_data
7059             , x_claim_id             => l_ra_rec.secondary_application_ref_id
7060             , x_claim_number         => l_ra_rec.application_ref_num
7061             , x_claim_reason_name    => x_claim_reason_name
7062             , p_legal_entity_id      => l_legal_entity_id);
7063        ELSE
7064          /* Bug 4170060 - update TM amount_applied with outstanding claim
7065             amount */
7066          l_amount_applied := arpt_sql_func_util.get_claim_amount(l_ra_rec.secondary_application_ref_id) + p_amount_applied;
7067 
7068          update_claim(
7069               p_claim_id             =>  l_ra_rec.secondary_application_ref_id
7070             , p_invoice_ps_id        =>  NULL
7071             , p_customer_trx_id      =>  NULL
7072             , p_amount               =>  p_amount_applied * -1
7073             , p_amount_applied       =>  l_amount_applied
7074             , p_apply_date           =>  p_apply_date
7075             , p_cash_receipt_id      =>  l_ra_rec.cash_receipt_id
7076             , p_receipt_number       =>  l_receipt_number
7077             , p_action_type          =>  'A'
7078             , x_claim_reason_code_id =>  l_claim_reason_code_id
7079             , x_claim_reason_name    =>  l_claim_reason_name
7080             , x_claim_number         =>  l_ra_rec.application_ref_num
7081             , x_return_status        =>  x_return_status
7082             , x_msg_count            =>  x_msg_count
7083             , x_msg_data             =>  x_msg_data);
7084        END IF;
7085        IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7086           raise trade_mgt_err;
7087        END IF;
7088     END IF;
7089     x_application_ref_id := l_ra_rec.secondary_application_ref_id;
7090     x_application_ref_num := l_ra_rec.application_ref_num;
7091 
7092     -- Insert OTHER ACC record
7093 
7094     arp_app_pkg.insert_p( l_ra_rec, l_ra_rec.receivable_application_id );
7095 
7096     IF l_ra_rec.receivable_application_id IS NOT NULL THEN
7097          l_xla_ev_rec.xla_from_doc_id := l_ra_rec.receivable_application_id;
7098          l_xla_ev_rec.xla_to_doc_id   := l_ra_rec.receivable_application_id;
7099          l_xla_ev_rec.xla_mode        := 'O';
7100          l_xla_ev_rec.xla_call        := 'B';
7101          l_xla_ev_rec.xla_doc_table := 'APP';
7102          ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
7103     END IF;
7104 
7105    --  Call mrc engine to process inserts in to receivable apps
7106    --
7107    --Release 11.5 VAT changes, create paired UNAPP record accounting
7108    --in ar_distributions
7109    --
7110     l_ae_doc_rec.document_type             := 'RECEIPT';
7111     l_ae_doc_rec.document_id               := l_ra_rec.cash_receipt_id;
7112     l_ae_doc_rec.accounting_entity_level   := 'ONE';
7113     l_ae_doc_rec.source_table              := 'RA';
7114     l_ae_doc_rec.source_id                 := l_prev_unapp_id;
7115     l_ae_doc_rec.source_id_old             := l_ra_rec.receivable_application_id;
7116     l_ae_doc_rec.other_flag                := 'PAIR';
7117     arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
7118 
7119    --
7120    --Release 11.5 VAT changes, create OTHER ACC record accounting
7121    --in ar_distributions
7122    --
7123 
7124     l_ae_doc_rec.source_id                 := l_ra_rec.receivable_application_id;
7125     l_ae_doc_rec.source_id_old             := '';
7126     l_ae_doc_rec.other_flag                := '';
7127     arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
7128 
7129 
7130    /* Bug 4910860
7131        Check if the journals balance */
7132       IF nvl(p_module_name,'RAPI') = 'RAPI' THEN
7133          l_called_from_api := 'Y';
7134       ELSE
7135         l_called_from_api := 'N';
7136       END IF;
7137     arp_balance_check.Check_Appln_Balance(l_ra_rec.receivable_application_id,
7138                                             l_prev_unapp_id,
7139                                             NULL,
7140                                             l_called_from_api);
7141 
7142     -- ----------------------------------------------------------------------
7143     -- 10/31/1996 K.Lawrance
7144     -- Finally update cash receipt status.
7145     -- ----------------------------------------------------------------------
7146 
7147     -- First, set ar_cash_receipt record values to dummy ...
7148     -- This is to distinguish between updateable NULL and NULL value (dummy)
7149     -- which means that column is not to be updated.
7150 
7151     arp_cash_receipts_pkg.set_to_dummy(l_cr_rec);
7152 
7153     -- ---------------------------------------------------------------------
7154     -- Cash receipt must be fully applied in order to set the status
7155     -- to 'Applied'.
7156     -- ---------------------------------------------------------------------
7157     IF PG_DEBUG in ('Y', 'C') THEN
7158        arp_standard.debug (  '-- Defining receipt status ...');
7159        arp_standard.debug (  '-- p_amount_applied = '||to_char(p_amount_applied));
7160        arp_standard.debug (  '-- l_amount_due_remaining = '||
7161 		                            to_char(l_amount_due_remaining));
7162     END IF;
7163 
7164     -- Determine if the receipt has been fully applied.
7165     -- We include the total amount that is on account or claim as this is
7166     -- not included in the Payment Schedules, Amount Due Remaining
7167     -- total for the receipt.
7168     --
7169     IF (l_amount_due_remaining + l_on_account_total + p_amount_applied < 0)
7170     THEN
7171       l_cr_rec.status           := 'UNAPP';
7172     ELSE
7173       l_cr_rec.status           := 'APP';
7174     END IF;
7175 
7176     IF PG_DEBUG in ('Y', 'C') THEN
7177        arp_standard.debug (  '-- status = '|| l_cr_rec.status);
7178     END IF;
7179 
7180     l_cr_rec.cash_receipt_id  := l_ra_rec.cash_receipt_id;
7181 
7182     -- Update cash receipt status.
7183     arp_cash_receipts_pkg.update_p(
7184 	  l_cr_rec
7185 	, l_ra_rec.cash_receipt_id);
7186 
7187     -- ---------------------------------------------------------------------
7188     -- Update batch status if receipt has a batch
7189     -- ---------------------------------------------------------------------
7190     IF (ln_batch_id IS NOT NULL)
7191     THEN
7192       arp_rw_batches_check_pkg.update_batch_status(ln_batch_id);
7193     END IF;
7194 
7195     -- ---------------------------------------------------------------------
7196     -- Return the new receivable application id back to the form
7197     -- ---------------------------------------------------------------------
7198     p_out_rec_application_id := l_ra_rec.receivable_application_id;
7199 
7200     IF PG_DEBUG in ('Y', 'C') THEN
7201        arp_standard.debug(   'arp_process_application.other_account_application()-' );
7202     END IF;
7203     EXCEPTION
7204          WHEN trade_mgt_err THEN
7205            x_return_status := FND_API.G_RET_STS_ERROR;
7206            IF PG_DEBUG in ('Y', 'C') THEN
7207               arp_standard.debug('Trade Management : ' ||
7208                 'EXCEPTION: arp_process_application.other_account_application' );
7209            END IF;
7210          WHEN OTHERS THEN
7211            x_return_status := FND_API.G_RET_STS_ERROR;
7212            IF PG_DEBUG in ('Y', 'C') THEN
7213               arp_standard.debug(
7214                 'EXCEPTION: arp_process_application.other_account_application' );
7215            END IF;
7216               RAISE;
7217 
7218 END other_account_application;
7219 
7220 /*===========================================================================+
7221  | PROCEDURE                                                                 |
7222  |    create_claim                                                           |
7223  |                                                                           |
7224  | DESCRIPTION                                                               |
7225  |    Calls iClaim group API to create a deduction claim.                    |
7226  |                                                                           |
7227  | SCOPE - PRIVATE                                                           |
7228  |                                                                           |
7229  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
7230  |      OZF_Claim_GRP.Create_Deduction - Group API to create a claim from AR |
7231  |                                                                           |
7232  | ARGUMENTS  : IN:                                                          |
7233  |              OUT:                                                         |
7234  |                                                                           |
7235  | RETURNS    : NONE                                                         |
7236  |                                                                           |
7237  |                                                                           |
7238  | MODIFICATION HISTORY                                                      |
7239  |   jbeckett    27-APR-2001  Created                                        |
7240  |   apandit     04-JUN-2001  Adde parameter p_invoice_ps_id. Bug 1812328    |
7241  |   jbeckett    10-AUG-2001  Added parameter p_ship_to_site_id. Bug 1893980 |
7242  |   jbeckett    13-MAR-2001  Bug 2254777 - added parameter reason_id        |
7243  |   jbeckett    09-MAY-2002  Bug 2361331 - added parameter p_salesrep_id    |
7244  |   jbeckett    14-OCT-2005  Bug 4565758 - legal entity passed to TM        |
7245  |   balkumar    18-SEP-2006  Bug 5495310 - added parameter p_apply_date     |
7246  |                                                                           |
7247  +===========================================================================*/
7248 PROCEDURE create_claim(
7249               p_amount               IN  NUMBER
7250             , p_amount_applied       IN  NUMBER
7251             , p_currency_code        IN  VARCHAR2
7252             , p_exchange_rate_type   IN  VARCHAR2
7253             , p_exchange_rate_date   IN  DATE
7254             , p_exchange_rate        IN  NUMBER
7255             , p_customer_trx_id      IN  NUMBER
7256             , p_invoice_ps_id        IN  NUMBER
7257             , p_cust_trx_type_id     IN  NUMBER
7258             , p_trx_number           IN  VARCHAR2
7259             , p_cust_account_id      IN  NUMBER
7260             , p_bill_to_site_id      IN  NUMBER
7261             , p_ship_to_site_id      IN  NUMBER
7262             , p_salesrep_id          IN  NUMBER
7263             , p_customer_ref_date    IN  DATE
7264             , p_customer_ref_number  IN  VARCHAR2
7265             , p_cash_receipt_id      IN  NUMBER
7266             , p_receipt_number       IN  VARCHAR2
7267             , p_reason_id            IN  NUMBER
7268             , p_customer_reason      IN  VARCHAR2
7269             , p_comments             IN  VARCHAR2
7270             , p_apply_date           IN  DATE
7271             , p_attribute_category   IN  VARCHAR2
7272             , p_attribute1           IN  VARCHAR2
7273             , p_attribute2           IN  VARCHAR2
7274             , p_attribute3           IN  VARCHAR2
7275             , p_attribute4           IN  VARCHAR2
7276             , p_attribute5           IN  VARCHAR2
7277             , p_attribute6           IN  VARCHAR2
7278             , p_attribute7           IN  VARCHAR2
7279             , p_attribute8           IN  VARCHAR2
7280             , p_attribute9           IN  VARCHAR2
7281             , p_attribute10          IN  VARCHAR2
7282             , p_attribute11          IN  VARCHAR2
7283             , p_attribute12          IN  VARCHAR2
7284             , p_attribute13          IN  VARCHAR2
7285             , p_attribute14          IN  VARCHAR2
7286             , p_attribute15          IN  VARCHAR2
7287             , x_return_status        OUT NOCOPY VARCHAR2
7288             , x_msg_count            OUT NOCOPY NUMBER
7289             , x_msg_data             OUT NOCOPY VARCHAR2
7290             , x_claim_id             OUT NOCOPY NUMBER
7291             , x_claim_number         OUT NOCOPY VARCHAR2
7292             , x_claim_reason_name    OUT NOCOPY VARCHAR2
7293             , p_legal_entity_id      IN  NUMBER)
7294 IS
7295 
7296   l_claim_rec           OZF_Claim_GRP.Deduction_Rec_Type;
7297   l_return_status       VARCHAR2(1);
7298   l_claim_reason_code_id NUMBER;
7299 
7300 BEGIN
7301 
7302   IF PG_DEBUG in ('Y', 'C') THEN
7303      arp_standard.debug(   'arp_process_application.create_claim()+' );
7304   END IF;
7305   IF p_amount = 0
7306   THEN
7307     x_return_status := 'S';
7308     x_claim_id := NULL;
7309     x_claim_number := NULL;
7310     RETURN;
7311   END IF;
7312   l_claim_rec.claim_date := p_apply_date;  /* bug 5495310 */
7313   l_claim_rec.due_date := NULL;
7314   l_claim_rec.claim_type_id := NULL;
7315   l_claim_rec.amount := p_amount;
7316   l_claim_rec.currency_code := p_currency_code;
7317   l_claim_rec.exchange_rate_type := p_exchange_rate_type;
7318   l_claim_rec.exchange_rate_date := p_exchange_rate_date;
7319   l_claim_rec.exchange_rate := p_exchange_rate;
7320   l_claim_rec.set_of_books_id := arp_global.set_of_books_id;
7321   l_claim_rec.amount_applied := p_amount_applied;
7322   l_claim_rec.legal_entity_id := p_legal_entity_id;
7323   IF p_customer_trx_id IS NOT NULL
7324   THEN
7325     l_claim_rec.source_object_id := p_customer_trx_id;
7326     l_claim_rec.source_object_type_id := p_cust_trx_type_id;
7327     l_claim_rec.source_object_class := 'INVOICE';
7328     l_claim_rec.source_object_number := p_trx_number;
7329   ELSE
7330     l_claim_rec.source_object_id := NULL;
7331     l_claim_rec.source_object_type_id := NULL;
7332     l_claim_rec.source_object_class := NULL;
7333     l_claim_rec.source_object_number := NULL;
7334   END IF;
7335   l_claim_rec.cust_account_id := p_cust_account_id;
7336   l_claim_rec.cust_billto_acct_site_id := p_bill_to_site_id;
7337   l_claim_rec.cust_shipto_acct_site_id := p_ship_to_site_id;
7338   l_claim_rec.sales_rep_id := p_salesrep_id;
7339   l_claim_rec.reason_code_id := p_reason_id;
7340   l_claim_rec.customer_ref_date := p_customer_ref_date;
7341   l_claim_rec.customer_ref_number := p_customer_ref_number;
7342   l_claim_rec.receipt_id := p_cash_receipt_id;
7343   l_claim_rec.receipt_number := p_receipt_number;
7344   l_claim_rec.comments := p_comments;
7345   l_claim_rec.deduction_attribute_category := p_attribute_category;
7346   l_claim_rec.deduction_attribute1 := p_attribute1;
7347   l_claim_rec.deduction_attribute2 := p_attribute2;
7348   l_claim_rec.deduction_attribute3 := p_attribute3;
7349   l_claim_rec.deduction_attribute4 := p_attribute4;
7350   l_claim_rec.deduction_attribute5 := p_attribute5;
7351   l_claim_rec.deduction_attribute6 := p_attribute6;
7352   l_claim_rec.deduction_attribute7 := p_attribute7;
7353   l_claim_rec.deduction_attribute8 := p_attribute8;
7354   l_claim_rec.deduction_attribute9 := p_attribute9;
7355   l_claim_rec.deduction_attribute10 := p_attribute10;
7356   l_claim_rec.deduction_attribute11 := p_attribute11;
7357   l_claim_rec.deduction_attribute12 := p_attribute12;
7358   l_claim_rec.deduction_attribute13 := p_attribute13;
7359   l_claim_rec.deduction_attribute14 := p_attribute14;
7360   l_claim_rec.deduction_attribute15 := p_attribute15;
7361   l_claim_rec.customer_reason := p_customer_reason;
7362 
7363   -- Dumping all the values
7364 
7365   IF PG_DEBUG in ('Y', 'C') THEN
7366      arp_standard.debug('set_of_books id = '||arp_global.set_of_books_id);
7367      arp_standard.debug('p_amount = '||p_amount);
7368      arp_standard.debug('p_amount_applied = '||p_amount_applied);
7369      arp_standard.debug('p_currency_code = '||p_currency_code);
7370      arp_standard.debug('p_exchange_rate_type = '||p_exchange_rate_type);
7371      arp_standard.debug('p_exchange_rate_date = '||p_exchange_rate_date);
7372      arp_standard.debug('p_exchange_rate = '||p_exchange_rate);
7373      arp_standard.debug('p_customer_trx_id = '||p_customer_trx_id);
7374      arp_standard.debug('p_invoice_ps_id = '||p_invoice_ps_id);
7375      arp_standard.debug('p_cust_trx_type_id = '||p_cust_trx_type_id);
7376      arp_standard.debug('p_trx_number = '||p_trx_number);
7377      arp_standard.debug('p_cust_account_id = '||p_cust_account_id);
7378      arp_standard.debug('p_bill_to_site_id = '||p_bill_to_site_id);
7379      arp_standard.debug('p_ship_to_site_id = '||p_ship_to_site_id);
7380      arp_standard.debug('p_salesrep_id = '||p_salesrep_id);
7381      arp_standard.debug('p_customer_ref_date = '||p_customer_ref_date);
7382      arp_standard.debug('p_customer_ref_number = '||p_customer_ref_number);
7383      arp_standard.debug('p_cash_receipt_id = '||p_cash_receipt_id);
7384      arp_standard.debug('p_receipt_number = '||p_receipt_number);
7385      arp_standard.debug('p_reason_id = '||p_reason_id);
7386      arp_standard.debug('p_customer_reason = '||p_customer_reason);
7387   END IF;
7388 
7389   OZF_Claim_GRP.Create_Deduction
7390              (p_api_version_number   => 1.0
7391              ,p_init_msg_list        => FND_API.G_TRUE
7392              ,p_commit               => FND_API.G_FALSE
7393              ,x_return_status        => l_return_status
7394              ,x_msg_count            => x_msg_count
7395              ,x_msg_data             => x_msg_data
7396              ,p_deduction            => l_claim_rec
7397              ,x_claim_id             => x_claim_id
7398              ,x_claim_number         => x_claim_number
7399              ,x_claim_reason_code_id => l_claim_reason_code_id
7400              ,x_claim_reason_name    => x_claim_reason_name );
7401   IF l_return_status = FND_API.G_RET_STS_SUCCESS
7402   THEN
7403     x_return_status := 'S';
7404     IF p_customer_trx_id IS NOT NULL and  p_customer_trx_id > 0
7405     THEN
7406       insert_trx_note(p_customer_trx_id
7407                      ,p_receipt_number
7408                      ,x_claim_number
7409                      ,'CREATE');
7410       put_trx_in_dispute(p_invoice_ps_id
7411                         ,p_amount
7412                         ,'Y');
7413     END IF;
7414   ELSIF l_return_status = FND_API.G_RET_STS_ERROR
7415   THEN
7416     x_return_status := 'E';
7417   ELSE
7418     x_return_status := 'U';
7419   END IF;
7420 
7421   IF PG_DEBUG in ('Y', 'C') THEN
7422      arp_standard.debug(   'arp_process_application.create_claim()-' );
7423   END IF;
7424 
7425   EXCEPTION
7426     WHEN OTHERS THEN
7427       IF PG_DEBUG in ('Y', 'C') THEN
7428          arp_standard.debug(
7429        'EXCEPTION: arp_process_application.create_claim' );
7430       END IF;
7431       RAISE;
7432 END create_claim;
7433 
7434 /*===========================================================================+
7435  | PROCEDURE                                                                 |
7436  |    update_claim                                                           |
7437  |                                                                           |
7438  | DESCRIPTION                                                               |
7439  |    Calls iClaim group API to update a deduction claim.                    |
7440  |                                                                           |
7441  | SCOPE - PUBLIC                                                            |
7442  |                                                                           |
7443  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
7444  |      OZF_Claim_GRP.Update_Deduction - Group API to update a claim from AR |
7445  |                                                                           |
7446  | ARGUMENTS  : IN:                                                          |
7447  |              OUT:                                                         |
7448  |                                                                           |
7449  | RETURNS    : NONE                                                         |
7450  |                                                                           |
7451  |                                                                           |
7452  | MODIFICATION HISTORY                                                      |
7453  |   jbeckett    02-MAY-2001  Created                                        |
7454  |   apandit     04-JUN-2001  added parameter p_invoice_ps_id. Bug 1812328   |
7455  |   apandit     05-JUN-2001  added parameters p_claim_number and            |
7456  |                            p_customer_trx_id. Bug 1812334                 |
7457  |   jbeckett    07-FEB-2003  Bug 2751910 - no longer do we actually cancel  |
7458  |                            the claim, we just set the amount to 0 and set |
7459  |                            active_claim flag to 'C' on payment schedule if|
7460  |                            invoice related.                               |
7461  |                            REnamed from cancel_claim to reflect change.   |
7462  |                                                                           |
7463  +===========================================================================*/
7464 PROCEDURE update_claim(
7465               p_claim_id             IN  OUT NOCOPY NUMBER
7466             , p_invoice_ps_id        IN  NUMBER
7467             , p_customer_trx_id      IN  NUMBER
7468             , p_amount               IN  NUMBER
7469             , p_amount_applied       IN  NUMBER
7470             , p_apply_date           IN  DATE
7471             , p_cash_receipt_id      IN  NUMBER
7472             , p_receipt_number       IN  VARCHAR2
7473             , p_action_type          IN  VARCHAR2
7474             , x_claim_reason_code_id OUT NOCOPY NUMBER
7475             , x_claim_reason_name    OUT NOCOPY VARCHAR2
7476             , x_claim_number         OUT NOCOPY VARCHAR2
7477             , x_return_status        OUT NOCOPY VARCHAR2
7478             , x_msg_count            OUT NOCOPY NUMBER
7479             , x_msg_data             OUT NOCOPY VARCHAR2)
7480 IS
7481 
7482   l_claim_rec                  OZF_Claim_GRP.Deduction_Rec_Type;
7483   l_receipt_number             ar_cash_receipts.receipt_number%TYPE;
7484   l_return_status              VARCHAR2(1);
7485   l_object_version_number      NUMBER;
7486   l_claim_number               VARCHAR2(30);
7487   l_amount_from_dispute        NUMBER;
7488   l_active_claim_flag          ar_payment_schedules.active_claim_flag%TYPE;
7489   l_amount_due_original        NUMBER;
7490 
7491 BEGIN
7492   IF PG_DEBUG in ('Y', 'C') THEN
7493      arp_standard.debug(   'arp_process_application.update_claim()+' );
7494   END IF;
7495 
7496   l_return_status := FND_API.G_RET_STS_SUCCESS;
7497 
7498   IF p_claim_id IS NOT NULL THEN
7499     l_claim_rec.claim_id := p_claim_id;
7500   ELSE
7501     l_claim_rec.claim_id := NULL;
7502   END IF;
7503   l_claim_rec.amount := p_amount;
7504   l_claim_rec.applied_date := p_apply_date;
7505   l_claim_rec.amount_applied := p_amount_applied;
7506   l_claim_rec.applied_action_type := p_action_type;
7507   l_claim_rec.receipt_id := p_cash_receipt_id;
7508   l_claim_rec.applied_receipt_id := p_cash_receipt_id;
7509   IF p_receipt_number IS NULL
7510   THEN
7511     SELECT receipt_number INTO l_receipt_number
7512     FROM   ar_cash_receipts
7513     WHERE  cash_receipt_id = p_cash_receipt_id;
7514   ELSE
7515     l_receipt_number := p_receipt_number;
7516   END IF;
7517   l_claim_rec.receipt_number := l_receipt_number;
7518   l_claim_rec.applied_receipt_number := l_receipt_number;
7519 
7520   IF (p_customer_trx_id IS NOT NULL and p_customer_trx_id > 0)
7521   THEN
7522     l_claim_rec.source_object_id := p_customer_trx_id;
7523     l_claim_rec.source_object_class := 'INVOICE';
7524     SELECT amount_due_original
7525     INTO   l_amount_due_original
7526     FROM   ar_payment_schedules
7527     WHERE  payment_schedule_id = p_invoice_ps_id;
7528   ELSE
7529     l_claim_rec.source_object_id := NULL;
7530     l_claim_rec.source_object_class := NULL;
7531   END IF;
7532 
7533   IF PG_DEBUG in ('Y', 'C') THEN
7534   --dump the in parameters
7535      arp_standard.debug('update_claim: p_claim_id = '||p_claim_id);
7536      arp_standard.debug('update_claim: p_amount = '||p_amount);
7537      arp_standard.debug('update_claim: p_apply_date = '||p_apply_date);
7538      arp_standard.debug('update_claim: p_amount_applied = '||p_amount_applied);
7539      arp_standard.debug('update_claim: p_action_type = '||p_action_type);
7540      arp_standard.debug('update_claim: p_cash_receipt_id = '||p_cash_receipt_id);
7541      arp_standard.debug('update_claim: p_customer_trx_id = '||p_customer_trx_id);
7542      arp_standard.debug('update_claim: l_receipt_number = '||l_receipt_number);
7543   END IF;
7544 
7545   OZF_Claim_GRP.Update_Deduction
7546              (p_api_version_number    => 1.0
7547              ,p_init_msg_list         => FND_API.G_TRUE
7548              ,p_commit                => FND_API.G_FALSE
7549              ,x_return_status         => l_return_status
7550              ,x_msg_count             => x_msg_count
7551              ,x_msg_data              => x_msg_data
7552              ,p_deduction             => l_claim_rec
7553              ,x_object_version_number => l_object_version_number
7554              ,x_claim_reason_code_id  => x_claim_reason_code_id
7555              ,x_claim_reason_name     => x_claim_reason_name
7556              ,x_claim_id              => p_claim_id
7557              ,x_claim_number          => x_claim_number );
7558 
7559   IF l_return_status = FND_API.G_RET_STS_SUCCESS
7560   THEN
7561     x_return_status := 'S';
7562 
7563     --Bug 1812328 : added the call to remove_dispute_on_trx().
7564   --Bug 1812334 : added the call to insert_trx_note().
7565     IF (p_invoice_ps_id IS NOT NULL and  p_customer_trx_id > 0)
7566     THEN
7567 
7568       insert_trx_note(p_customer_trx_id
7569                      ,NULL
7570                      ,x_claim_number
7571                      ,'CANCEL');
7572 
7573       IF p_action_type = 'A'
7574       THEN
7575         l_amount_from_dispute := p_amount_applied;
7576       ELSE
7577         l_amount_from_dispute := p_amount_applied * -1;
7578       END IF;
7579 
7580       IF (p_amount = 0 OR p_amount = l_amount_due_original)
7581       THEN
7582         l_active_claim_flag := 'C';
7583       ELSE
7584         l_active_claim_flag := 'Y';
7585       END IF;
7586 
7587       update_dispute_on_trx(p_invoice_ps_id
7588                           , l_active_claim_flag
7589                           , l_amount_from_dispute);
7590 
7591     END IF;
7592 
7593   ELSIF l_return_status = FND_API.G_RET_STS_ERROR
7594   THEN
7595     x_return_status := 'E';
7596   ELSE
7597     x_return_status := 'U';
7598   END IF;
7599 
7600   IF PG_DEBUG in ('Y', 'C') AND l_return_status <> FND_API.G_RET_STS_SUCCESS
7601    THEN
7602      arp_standard.debug(   'arp_process_application.update_claim: ERROR occurred calling update_deduction: '||SQLERRM );
7603   END IF;
7604   IF PG_DEBUG in ('Y', 'C') THEN
7605      arp_standard.debug(   'arp_process_application.update_claim()-' );
7606   END IF;
7607 
7608   EXCEPTION
7609     WHEN OTHERS THEN
7610       IF PG_DEBUG in ('Y', 'C') THEN
7611          arp_standard.debug(
7612        'EXCEPTION: arp_process_application.update_claim' );
7613       END IF;
7614       RAISE;
7615 END update_claim;
7616 
7617 /*===========================================================================+
7618  | PROCEDURE                                                                 |
7619  |    insert_trx_note                                                        |
7620  |                                                                           |
7621  | DESCRIPTION                                                               |
7622  |    Calls arp_notes_pkg to insert a note into AR_NOTES for a given         |
7623  |    transaction                                                            |
7624  |                                                                           |
7625  | SCOPE - PUBLIC                                                            |
7626  |                                                                           |
7627  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
7628  |      arp_notes_pkg.insert_cover                                           |
7629  |                                                                           |
7630  | ARGUMENTS  : IN:                                                          |
7631  |              OUT:                                                         |
7632  |                                                                           |
7633  | RETURNS    : NONE                                                         |
7634  |                                                                           |
7635  |                                                                           |
7636  | MODIFICATION HISTORY                                                      |
7637  |   jbeckett    03-MAY-2001  Created                                        |
7638  |   apandit     05-JUN-2001  added parameter p_flag. Bug 1812334            |
7639  |                                                                           |
7640  +===========================================================================*/
7641 PROCEDURE insert_trx_note(
7642               p_customer_trx_id             IN  NUMBER
7643             , p_receipt_number              IN  VARCHAR2
7644             , p_claim_number                IN  VARCHAR2
7645             , p_flag                        IN  VARCHAR2)
7646 IS
7647   l_text                    VARCHAR2(2000);
7648   l_user_id                 NUMBER;
7649   l_last_update_login       NUMBER;
7650   l_sysdate                 DATE;
7651   l_note_id                 NUMBER;
7652 
7653 BEGIN
7654   IF PG_DEBUG in ('Y', 'C') THEN
7655      arp_standard.debug(   'arp_process_application.insert_trx_note()+' );
7656   END IF;
7657  --Bug 1812334 : ar_notes entry needs to be created also when the claim is cancelled.
7658 
7659  IF p_flag = 'CREATE'  THEN
7660    fnd_message.set_name('AR', 'AR_RW_APP_TRX_CLAIM_NOTE');
7661    fnd_message.set_token('RECEIPT_NUM',p_receipt_number);
7662    fnd_message.set_token('CLAIM_NUM',p_claim_number);
7663  ELSIF p_flag = 'CANCEL' THEN
7664    fnd_message.set_name('AR', 'AR_RW_APP_TRX_CLAIM_CANCL_NOTE');
7665    fnd_message.set_token('CLAIM_NUM',p_claim_number);
7666  END IF;
7667 
7668   /* bug 3161148, need to pass only 240 characters as that is the
7669      limit on the column in the table.  */
7670   l_text := substrb(fnd_message.get,1,240);
7671 
7672   l_user_id := arp_standard.profile.user_id;
7673   l_last_update_login := arp_standard.profile.last_update_login;
7674   l_sysdate := SYSDATE;
7675   arp_notes_pkg.insert_cover(
7676         p_note_type              => 'MAINTAIN',
7677         p_text                   => l_text,
7678         p_customer_call_id       => NULL,
7679         p_customer_call_topic_id => NULL,
7680         p_call_action_id         => NULL,
7681         p_customer_trx_id        => p_customer_trx_id,
7682         p_note_id                => l_note_id,
7683         p_last_updated_by        => l_user_id,
7684         p_last_update_date       => l_sysdate,
7685         p_last_update_login      => l_last_update_login,
7686         p_created_by             => l_user_id,
7687         p_creation_date          => l_sysdate);
7688   IF PG_DEBUG in ('Y', 'C') THEN
7689      arp_standard.debug(   'arp_process_application.insert_trx_note()-' );
7690   END IF;
7691 EXCEPTION
7692   WHEN OTHERS THEN
7693     IF PG_DEBUG in ('Y', 'C') THEN
7694        arp_standard.debug(  'EXCEPTION: arp_process_application.insert_trx_note' );
7695     END IF;
7696     RAISE;
7697 END insert_trx_note;
7698 
7699 --Bug 1812328 : modified the parameter p_customer_trx_id to p_invoice_ps_id.
7700 PROCEDURE put_trx_in_dispute(
7701               p_invoice_ps_id               IN  NUMBER
7702             , p_dispute_amount              IN  NUMBER
7703             , p_active_claim                IN VARCHAR2)
7704 IS
7705   l_last_update_login       NUMBER;
7706   l_user_id                 NUMBER;
7707 
7708   /*added for the bug 2641517 */
7709    l_term_changed_flag           VARCHAR2(1);
7710    l_trx_sum_hist_rec            AR_TRX_SUMMARY_HIST%rowtype;
7711    l_history_id                  NUMBER;
7712    l_trx_class                   varchar2(30);
7713    l_trx_dispute_date            DATE;
7714    l_new_dispute_amount          NUMBER;
7715    l_sysdate                     DATE;
7716    CURSOR get_existing_ps (p_ps_id IN NUMBER) IS
7717    SELECT payment_schedule_id,
7718           invoice_currency_code,
7719           due_date,
7720           amount_in_dispute,
7721           amount_due_original,
7722           amount_due_remaining,
7723           amount_adjusted,
7724           customer_trx_id,
7725           customer_id,
7726           customer_site_use_id,
7727           class,
7728           trx_date,
7729           dispute_date
7730    FROM   ar_payment_schedules
7731    WHERE  payment_schedule_id = p_ps_id;
7732 BEGIN
7733 IF PG_DEBUG in ('Y', 'C') THEN
7734    arp_standard.debug(   'arp_process_application.put_trx_in_dispute()+' );
7735 END IF;
7736   l_user_id := arp_standard.profile.user_id;
7737   l_last_update_login := arp_standard.profile.last_update_login;
7738   l_sysdate := SYSDATE;
7739 
7740    /*bug 2641517 : creating history record for business event */
7741    OPEN get_existing_ps(p_invoice_ps_id);
7742 
7743        FETCH get_existing_ps INTO
7744              l_trx_sum_hist_rec.payment_schedule_id,
7745              l_trx_sum_hist_rec.currency_code,
7746              l_trx_sum_hist_rec.due_date,
7747              l_trx_sum_hist_rec.amount_in_dispute,
7748              l_trx_sum_hist_rec.amount_due_original,
7749              l_trx_sum_hist_rec.amount_due_remaining,
7750              l_trx_sum_hist_rec.amount_adjusted,
7751              l_trx_sum_hist_rec.customer_trx_id,
7752              l_trx_sum_hist_rec.customer_id,
7753              l_trx_sum_hist_rec.site_use_id,
7754              l_trx_class,
7755              l_trx_sum_hist_rec.trx_date,
7756              l_trx_dispute_date;
7757 
7758 
7759              AR_BUS_EVENT_COVER.p_insert_trx_sum_hist(l_trx_sum_hist_rec,
7760                                                       l_history_id,
7761                                                       l_trx_class,
7762                                                       'MODIFY_TRX');
7763             /* Bug 5129946*/
7764             IF get_existing_ps%ROWCOUNT>0 THEN
7765             l_new_dispute_amount := nvl(l_trx_sum_hist_rec.amount_in_dispute, 0) + p_dispute_amount;
7766 
7767             if(l_new_dispute_amount <> l_trx_sum_hist_rec.amount_in_dispute)
7768             OR (l_new_dispute_amount IS NULL and l_trx_sum_hist_rec.amount_in_dispute IS NOT NULL)
7769             OR (l_new_dispute_amount IS  NOT NULL and l_trx_sum_hist_rec.amount_in_dispute IS  NULL)
7770             THEN
7771             arp_dispute_history.DisputeHistory(  p_DisputeDate => l_sysdate,
7772                                                  p_OldDisputeDate => l_trx_dispute_date,
7773                                                  p_PaymentScheduleId => l_trx_sum_hist_rec.payment_schedule_id,
7774                                                  p_OldPaymentScheduleId =>l_trx_sum_hist_rec.payment_schedule_id,
7775                                                  p_AmountDueRemaining  => l_trx_sum_hist_rec.amount_due_remaining,
7776                                                  p_AmountInDispute => l_new_dispute_amount,
7777                                                  p_OldAmountInDispute => l_trx_sum_hist_rec.amount_in_dispute,
7778                                                  p_CreatedBy =>l_user_id,
7779                                                  p_CreationDate => l_sysdate,
7780                                                  p_LastUpdatedBy =>l_user_id,
7781                                                  p_LastUpdateDate => l_sysdate,
7782                                                  p_lastUpdateLogin => l_last_update_login);
7783            END IF;--if(l_new_dispute_amount <>....
7784            END IF;--IF get_existing_ps%ROWCOUNT>0 THEN
7785    CLOSE get_existing_ps;
7786 
7787   UPDATE ar_payment_schedules ps
7788   SET    ps.amount_in_dispute = nvl(ps.amount_in_dispute,0) + p_dispute_amount,
7789          ps.dispute_date      = SYSDATE,
7790          last_updated_by      = l_user_id,
7791          last_update_login    = l_last_update_login,
7792          active_claim_flag    = p_active_claim
7793   WHERE  ps.payment_schedule_id = p_invoice_ps_id;
7794 
7795      /*bug 2641517 - raise business event */
7796        AR_BUS_EVENT_COVER.Raise_Trx_Modify_Event
7797                                              (p_invoice_ps_id,
7798                                               l_trx_class,
7799                                               l_history_id);
7800 
7801   IF PG_DEBUG in ('Y', 'C') THEN
7802      arp_standard.debug(   'arp_process_application.put_trx_in_dispute()-' );
7803   END IF;
7804 EXCEPTION
7805   WHEN OTHERS THEN
7806     IF PG_DEBUG in ('Y', 'C') THEN
7807        arp_standard.debug(  'EXCEPTION: arp_process_application.put_trx_in_dispute');
7808     END IF;
7809     RAISE;
7810 END put_trx_in_dispute;
7811 
7812 --Bug 1812328 : added routine remove_dispute_on_trx
7813 --Bug 2751910 : renamed to update_dispute_on_trx
7814 PROCEDURE update_dispute_on_trx(
7815               p_invoice_ps_id               IN  NUMBER
7816              ,p_active_claim                IN  VARCHAR2
7817              ,p_amount                      IN  NUMBER )
7818 IS
7819   l_last_update_login       NUMBER;
7820   l_user_id                 NUMBER;
7821   /* Added 6 variables and cursor get_existing_ps for bug 5129946*/
7822   l_old_dispute_date        DATE;
7823   l_old_dispute_amount      NUMBER;
7824   l_amount_due_remaining    NUMBER;
7825   l_ps_id                   NUMBER;
7826   l_new_dispute_amount      NUMBER;
7827   l_sysdate                 DATE;
7828   CURSOR get_existing_ps (p_ps_id IN NUMBER) IS
7829    SELECT payment_schedule_id,
7830           amount_in_dispute,
7831           amount_due_remaining,
7832           dispute_date
7833    FROM   ar_payment_schedules
7834    WHERE  payment_schedule_id = p_ps_id;
7835 BEGIN
7836   l_user_id := arp_standard.profile.user_id;
7837   l_last_update_login := arp_standard.profile.last_update_login;
7838   l_sysdate := SYSDATE;
7839   IF PG_DEBUG in ('Y', 'C') THEN
7840      arp_standard.debug(   'arp_process_application.update_dispute_on_trx()+' );
7841   END IF;
7842   /*Bug 5129946: Calling arp_dispute_history.DisputeHistory*/
7843   OPEN get_existing_ps(p_invoice_ps_id);
7844     FETCH get_existing_ps INTO
7845           l_ps_id,
7846           l_old_dispute_amount,
7847           l_amount_due_remaining,
7848           l_old_dispute_date;
7849     IF get_existing_ps%ROWCOUNT>0 THEN
7850     if(l_old_dispute_amount = p_amount) THEN
7851     l_new_dispute_amount := NULL;
7852     ELSE
7853     l_new_dispute_amount := (l_old_dispute_amount-p_amount);
7854     END IF;
7855     if(l_new_dispute_amount <> l_old_dispute_amount)
7856             OR (l_new_dispute_amount IS NULL and l_old_dispute_amount IS NOT NULL)
7857             OR (l_new_dispute_amount IS  NOT NULL and l_old_dispute_amount IS  NULL)
7858             THEN
7859             arp_dispute_history.DisputeHistory(p_DisputeDate => l_sysdate,
7860                                                p_OldDisputeDate => l_old_dispute_date,
7861                                                p_PaymentScheduleId => l_ps_id,
7862                                                p_OldPaymentScheduleId => l_ps_id,
7863                                                p_AmountDueRemaining => l_amount_due_remaining,
7864                                                p_AmountInDispute =>l_new_dispute_amount,
7865                                                p_OldAmountInDispute =>l_old_dispute_amount,
7866                                                p_CreatedBy => l_user_id,
7867                                                p_CreationDate => l_sysdate,
7868                                                p_LastUpdatedBy => l_user_id,
7869                                                p_LastUpdateDate => l_sysdate,
7870                                                p_lastUpdateLogin =>l_last_update_login);
7871            END IF;--if(l_new_dispute_amount <> l_old_dispute_amount)
7872       END IF;--IF get_existing_ps%ROWCOUNT>0 THEN
7873    CLOSE get_existing_ps;
7874   UPDATE ar_payment_schedules ps
7875   SET    ps.amount_in_dispute = DECODE(ps.amount_in_dispute, p_amount, NULL,
7876                                        (ps.amount_in_dispute-p_amount)),
7877          ps.dispute_date      = SYSDATE,
7878          last_updated_by      = l_user_id,
7879          last_update_login    = l_last_update_login,
7880          active_claim_flag    = p_active_claim
7881   WHERE  ps.payment_schedule_id  = p_invoice_ps_id;
7882   IF PG_DEBUG in ('Y', 'C') THEN
7883      arp_standard.debug(   'arp_process_application.update_dispute_on_trx()-' );
7884   END IF;
7885 EXCEPTION
7886   WHEN OTHERS THEN
7887     IF PG_DEBUG in ('Y', 'C') THEN
7888        arp_standard.debug(  'EXCEPTION: arp_process_application.update_dispute_on_trx');
7889     END IF;
7890     RAISE;
7891 END update_dispute_on_trx;
7892 
7893 /*===========================================================================+
7894  | PROCEDURE                                                                 |
7895  |    reverse_action_receipt_cb                                              |
7896  |                                                                           |
7897  | DESCRIPTION                                                               |
7898  |    Reverses  chargebacks associated with an receipt CB application.       |
7899  |                                                                           |
7900  | SCOPE - PRIVATE                                                           |
7901  |                                                                           |
7902  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
7903  |      arp_process_chargeback.reverse_chargeback - Procedure to reverse     |
7904  |                                                   a chargeback            |
7905  |                                                                           |
7906  | ARGUMENTS  : IN:                                                          |
7907  |                 p_chargeback_customer_trx_id - chargeback customer trx id |
7908  |                 p_reversal_gl_date - Reversal GL date                     |
7909  |      	   p_module_name  - Name of the module that called this      |
7910  |				    procedure   			     |
7911  |      	   p_module_version  - Version of the module that called this|
7912  |			            procedure                                |
7913  |              OUT:                                                         |
7914  |                                                                           |
7915  | RETURNS    : NONE                                                         |
7916  |                                                                           |
7917  |                                                                           |
7918  | MODIFICATION HISTORY - Created by S.Nambiar     26-APR-01                 |
7919  |                                                                           |
7920  +===========================================================================*/
7921 PROCEDURE reverse_action_receipt_cb(
7922 	p_chargeback_customer_trx_id
7923                IN ar_receivable_applications.application_ref_id%TYPE,
7924 	p_reversal_gl_date IN DATE,
7925         p_reversal_date IN DATE,
7926 	p_module_name IN VARCHAR2,
7927 	p_module_version IN VARCHAR2 ) IS
7928 --
7929 BEGIN
7930     IF PG_DEBUG in ('Y', 'C') THEN
7931        arp_standard.debug( 'arp_process_application.reverse_action_receipt_cb()+' );
7932     END IF;
7933     --
7934     -- reverse chargeback
7935     -- For receipt chargeback,there is no adjustment associated with it
7936     arp_process_chargeback.reverse_chargeback(
7937 		             p_chargeback_customer_trx_id,
7938 		             p_reversal_gl_date,
7939                              p_reversal_date,
7940 		             p_module_name,
7941                              p_module_version );
7942 
7943     IF PG_DEBUG in ('Y', 'C') THEN
7944        arp_standard.debug( 'arp_process_application.reverse_action_receipt_cb()-' );
7945     END IF;
7946     EXCEPTION
7947         WHEN OTHERS THEN
7948               IF PG_DEBUG in ('Y', 'C') THEN
7949                  arp_standard.debug(
7950 		         'EXCEPTION: arp_process_application.reverse_action_receipt_cb' );
7951               END IF;
7952               RAISE;
7953 END reverse_action_receipt_cb;
7954 
7955 /*===========================================================================+
7956  | PROCEDURE                                                                 |
7957  |    reverse_action_misc_receipt                                            |
7958  |                                                                           |
7959  | DESCRIPTION                                                               |
7960  |    Reverses  Misc Receipt associated with an Credit Card refund app       |
7961  |                                                                           |
7962  | SCOPE - PRIVATE                                                           |
7963  |                                                                           |
7964  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
7965  |      arp_receipt_api_pub.reverse - Procedure to reverse                   |
7966  |                                                   a receipt               |
7967  |                                                                           |
7968  | ARGUMENTS  : IN:                                                          |
7969  |                 p_cash_receipt_id - Misc receipt cash receipt id          |
7970  |                 p_reversal_gl_date - Reversal GL date                     |
7971  |      	   p_module_name  - Name of the module that called this          |
7972  |				    procedure   			                                 |
7973  |      	   p_module_version  - Version of the module that called this    |
7974  |			            procedure                                            |
7975  |              OUT:                                                         |
7976  |                                                                           |
7977  | RETURNS    : NONE                                                         |
7978  |                                                                           |
7979  |                                                                           |
7980  | MODIFICATION HISTORY - Created by Ramakant Alat           18-JUN-01       |
7981  |                                                                           |
7982  +===========================================================================*/
7983 PROCEDURE reverse_action_misc_receipt(
7984 	p_cash_receipt_id IN ar_receivable_applications.application_ref_id%TYPE,
7985 	p_reversal_gl_date IN DATE,
7986     p_reversal_date IN DATE,
7987 	p_reversal_comments IN VARCHAR2 DEFAULT NULL,
7988 	p_called_from IN VARCHAR2  DEFAULT NULL) IS
7989 --
7990 l_return_status            VARCHAR2(1);
7991 l_msg_count                NUMBER;
7992 l_msg_data                 VARCHAR2(2000);
7993 l_msg_index                NUMBER;
7994 API_exception              EXCEPTION;
7995 BEGIN
7996    IF PG_DEBUG in ('Y', 'C') THEN
7997       arp_standard.debug( 'arp_process_application.reverse_action_misc_receipt()+' );
7998    END IF;
7999    --
8000    -- reverse Misc Receipt
8001 
8002    AR_RECEIPT_API_PUB.Reverse(
8003 				     p_api_version           => 1.0,
8004 					 p_init_msg_list          => FND_API.G_TRUE,
8005 					 x_return_status          => l_return_status,
8006 					 x_msg_count              => l_msg_count,
8007 					 x_msg_data               => l_msg_data,
8008 					 p_cash_receipt_id        => p_cash_receipt_id,
8009                      p_reversal_reason_code   =>'CC REFUND CHANGE',
8010                      p_reversal_comments      => p_reversal_comments,
8011                      p_reversal_category_code =>'CCRR',
8012 					 p_reversal_gl_date       => p_reversal_gl_date,
8013 					 p_reversal_date          => p_reversal_date,
8014 					 p_called_from            => 'UNAPPLY_CCR');
8015 
8016    /*------------------------------------------------+
8017     | Write API output to the concurrent program log |
8018 	+------------------------------------------------*/
8019    IF PG_DEBUG in ('Y', 'C') THEN
8020       arp_util.debug(  'API error count '||to_char(NVL(l_msg_count,0)));
8021    END IF;
8022 
8023    IF NVL(l_msg_count,0)  > 0 Then
8024 
8025 	  IF l_msg_count  = 1 Then
8026 
8027 	     /*------------------------------------------------+
8028 	      | There is one message returned by the API, so it|
8029 	      | has been sent out NOCOPY in the parameter x_msg_data  |
8030 	      +------------------------------------------------*/
8031 		 IF PG_DEBUG in ('Y', 'C') THEN
8032 		    arp_util.debug(  l_msg_data);
8033 		 END IF;
8034 
8035       ELSIF l_msg_count > 1 Then
8036 
8037 		 /*-------------------------------------------------------+
8038 		  | There are more than one messages returned by the API, |
8039 		  | so call them in a loop and print the messages         |
8040 		  +-------------------------------------------------------*/
8041 
8042 	     FOR l_count IN 1..l_msg_count LOOP
8043 
8044 			l_msg_data := FND_MSG_PUB.Get(FND_MSG_PUB.G_NEXT,
8045 							              FND_API.G_FALSE);
8046 			IF PG_DEBUG in ('Y', 'C') THEN
8047 			   arp_util.debug(  to_char(l_count)||' : '||l_msg_data);
8048 			END IF;
8049 
8050 		 END LOOP;
8051 
8052 	  END IF; -- l_msg_count
8053 
8054    END IF; -- NVL(l_msg_count,0)
8055 
8056    /*-----------------------------------------------------+
8057     | If API return status is not SUCCESS raise exception |
8058     +-----------------------------------------------------*/
8059    IF l_return_status = FND_API.G_RET_STS_SUCCESS Then
8060 
8061       /*-----------------------------------------------------+
8062 	   | Success do nothing, else branch introduced to make  |
8063 	   | sure that NULL case will also raise exception       |
8064 	   +-----------------------------------------------------*/
8065 	  NULL;
8066 
8067    ELSE
8068 	  /*---------------------------+
8069 	   | Error, raise an exception |
8070 	   +---------------------------*/
8071       RAISE API_exception;
8072 
8073    END IF; -- l_return_status
8074 
8075    IF PG_DEBUG in ('Y', 'C') THEN
8076       arp_standard.debug( 'arp_process_application.reverse_action_misc_receipt()-' );
8077    END IF;
8078    /*----------------------------------+
8079 	| APIs propagate exception upwards |
8080 	+----------------------------------*/
8081 EXCEPTION
8082    WHEN API_exception THEN
8083 	  IF PG_DEBUG in ('Y', 'C') THEN
8084 	     arp_util.debug(  'API EXCEPTION: ' ||
8085 		             'arp_process_application.reverse_action_misc_receipt'
8086 					 ||SQLERRM);
8087 	  END IF;
8088 	  FND_MSG_PUB.Get (FND_MSG_PUB.G_FIRST, FND_API.G_TRUE,
8089 					   l_msg_data, l_msg_index);
8090 	  FND_MESSAGE.Set_Encoded (l_msg_data);
8091 	  app_exception.raise_exception;
8092 
8093    WHEN OTHERS THEN
8094 	  IF PG_DEBUG in ('Y', 'C') THEN
8095 	     arp_util.debug('EXCEPTION: arp_process_application.reverse_action_misc_receipt'
8096 					 ||SQLERRM);
8097 	  END IF;
8098       RAISE;
8099    END reverse_action_misc_receipt;
8100 
8101 /*===========================================================================+
8102  | PROCEDURE                                                                 |
8103  |    get_claim_status                                                       |
8104  |                                                                           |
8105  | DESCRIPTION                                                               |
8106  |    Retreives  claim status from Trade Management via dynamic sql          |
8107  |                                                                           |
8108  | SCOPE - PUBLIC                                                            |
8109  |                                                                           |
8110  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
8111  |                                                                           |
8112  | ARGUMENTS  : IN:                                                          |
8113  |                 p_claim_id - trade mgmt claim id                          |
8114  |              OUT:                                                         |
8115  |                 x_claim_status - trade mgmt claim status code             |
8116  |                                                                           |
8117  | RETURNS    : NONE                                                         |
8118  |                                                                           |
8119  |                                                                           |
8120  | MODIFICATION HISTORY                                                      |
8121  | jbeckett 25-MAR-02 Created - bug 2232366                                  |
8122  |                                                                           |
8123  +===========================================================================*/
8124 PROCEDURE get_claim_status(
8125 	p_claim_id IN     NUMBER,
8126         x_claim_status    OUT NOCOPY VARCHAR2)
8127 IS
8128   l_query_string          VARCHAR2(2000);
8129 
8130 BEGIN
8131   IF PG_DEBUG in ('Y', 'C') THEN
8132      arp_standard.debug( 'arp_process_application.get_claim_status()+' );
8133   END IF;
8134   l_query_string :=
8135    ' select status_code from ozf_ar_deductions_v where claim_id = :claim_id ';
8136   BEGIN
8137     EXECUTE IMMEDIATE l_query_string
8138     INTO    x_claim_status
8139     USING   p_claim_id;
8140   EXCEPTION
8141     WHEN OTHERS THEN
8142         FND_MESSAGE.set_name('AR','AR_RW_INVALID_CLAIM_ID');
8143         FND_MESSAGE.set_token('CLAIM_ID',p_claim_id);
8144         APP_EXCEPTION.raise_exception;
8145   END;
8146   IF PG_DEBUG in ('Y', 'C') THEN
8147      arp_standard.debug( 'arp_process_application.get_claim_status()-' );
8148   END IF;
8149 END get_claim_status;
8150 
8151 /*===========================================================================+
8152  | PROCEDURE                                                                 |
8153  |    cm_activity_application                                                |
8154  |                                                                           |
8155  | DESCRIPTION                                                               |
8156  |    Activity applications against a credit memo                            |
8157  |                                                                           |
8158  | SCOPE - PUBLIC                                                            |
8159  |                                                                           |
8160  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
8161  |                                                                           |
8162  |         ARPCURR.functional_amount - Get the acctd amount of amount applied|
8163  |         arp_ps_pkg.fetch_p - Fetch a PS row                               |
8164  |         arp_app_pkg.insert_p - Insert a row into RA table                 |
8165  |                                                                           |
8166  | ARGUMENTS  : IN:                                                          |
8167  |                 p_cm_ps_id - PS id of the receipt                         |
8168  |                 p_application_ps_id - PS id of the special application    |
8169  |                 p_amount_applied - Input amount applied                   |
8170  |                 p_apply_date - Application date                           |
8171  |                 p_gl_date    - Gl Date                                    |
8172  |                 p_ussgl_transaction_code - USSGL transaction code         |
8173  |                 p_receivables_trx_id  -Activity id                        |
8174  |		   p_receipt_method_id - payment method for misc receipt     |
8175  |                 OTHER DESCRIPTIVE FLEX columns                            |
8176  |                 p_module_name  - Name of the module that called this      |
8177  |                                  procedure                                |
8178  |                 p_module_version  - Version of the module that called this|
8179  |                                  procedure                                |
8180  |              OUT:                                                         |
8181  |		   p_out_rec_application_id                                  |
8182  |				Returned receivable application id           |
8183  | RETURNS    : NONE                                                         |
8184  |                                                                           |
8185  | MODIFICATION HISTORY                                                      |
8186  | jbeckett 10-JAN-05   Created for credit memo refunds.                     |
8187  |           		                                                     |
8188  +===========================================================================*/
8189 PROCEDURE cm_activity_application(
8190 	p_cm_ps_id IN ar_payment_schedules.payment_schedule_id%TYPE,
8191 	p_application_ps_id	IN ar_payment_schedules.payment_schedule_id%TYPE,
8192         p_amount_applied IN ar_receivable_applications.amount_applied%TYPE,
8193         p_apply_date IN ar_receivable_applications.apply_date%TYPE,
8194 	p_gl_date IN ar_receivable_applications.gl_date%TYPE,
8195 	p_ussgl_transaction_code IN ar_receivable_applications.ussgl_transaction_code%TYPE,
8196 	p_attribute_category IN ar_receivable_applications.attribute_category%TYPE,
8197 	p_attribute1 IN ar_receivable_applications.attribute1%TYPE,
8198 	p_attribute2 IN ar_receivable_applications.attribute2%TYPE,
8199 	p_attribute3 IN ar_receivable_applications.attribute3%TYPE,
8200 	p_attribute4 IN ar_receivable_applications.attribute4%TYPE,
8201 	p_attribute5 IN ar_receivable_applications.attribute5%TYPE,
8202 	p_attribute6 IN ar_receivable_applications.attribute6%TYPE,
8203 	p_attribute7 IN ar_receivable_applications.attribute7%TYPE,
8204 	p_attribute8 IN ar_receivable_applications.attribute8%TYPE,
8205 	p_attribute9 IN ar_receivable_applications.attribute9%TYPE,
8206 	p_attribute10 IN ar_receivable_applications.attribute10%TYPE,
8207 	p_attribute11 IN ar_receivable_applications.attribute11%TYPE,
8208 	p_attribute12 IN ar_receivable_applications.attribute12%TYPE,
8209 	p_attribute13 IN ar_receivable_applications.attribute13%TYPE,
8210 	p_attribute14 IN ar_receivable_applications.attribute14%TYPE,
8211 	p_attribute15 IN ar_receivable_applications.attribute15%TYPE,
8212         p_global_attribute_category IN ar_receivable_applications.global_attribute_category%TYPE,
8213         p_global_attribute1 IN ar_receivable_applications.global_attribute1%TYPE,
8214         p_global_attribute2 IN ar_receivable_applications.global_attribute2%TYPE,
8215         p_global_attribute3 IN ar_receivable_applications.global_attribute3%TYPE,
8216         p_global_attribute4 IN ar_receivable_applications.global_attribute4%TYPE,
8217         p_global_attribute5 IN ar_receivable_applications.global_attribute5%TYPE,
8218         p_global_attribute6 IN ar_receivable_applications.global_attribute6%TYPE,
8219         p_global_attribute7 IN ar_receivable_applications.global_attribute7%TYPE,
8220         p_global_attribute8 IN ar_receivable_applications.global_attribute8%TYPE,
8221         p_global_attribute9 IN ar_receivable_applications.global_attribute9%TYPE,
8222         p_global_attribute10 IN ar_receivable_applications.global_attribute10%TYPE,
8223         p_global_attribute11 IN ar_receivable_applications.global_attribute11%TYPE,
8224         p_global_attribute12 IN ar_receivable_applications.global_attribute12%TYPE,
8225         p_global_attribute13 IN ar_receivable_applications.global_attribute13%TYPE,
8226         p_global_attribute14 IN ar_receivable_applications.global_attribute14%TYPE,
8227         p_global_attribute15 IN ar_receivable_applications.global_attribute15%TYPE,
8228         p_global_attribute16 IN ar_receivable_applications.global_attribute16%TYPE,
8229         p_global_attribute17 IN ar_receivable_applications.global_attribute17%TYPE,
8230         p_global_attribute18 IN ar_receivable_applications.global_attribute18%TYPE,
8231         p_global_attribute19 IN ar_receivable_applications.global_attribute19%TYPE,
8232         p_global_attribute20 IN ar_receivable_applications.global_attribute20%TYPE,
8233 	p_receivables_trx_id IN ar_receivable_applications.receivables_trx_id%TYPE,
8234 	p_receipt_method_id IN ar_receipt_methods.receipt_method_id%TYPE,
8235         p_comments IN ar_receivable_applications.comments%TYPE ,
8236         p_module_name IN VARCHAR2,
8237         p_module_version IN VARCHAR2,
8238         p_application_ref_id IN OUT NOCOPY ar_receivable_applications.application_ref_id%TYPE,
8239         p_application_ref_num IN OUT NOCOPY ar_receivable_applications.application_ref_num%TYPE,
8240         -- OUT NOCOPY
8241         p_out_rec_application_id OUT NOCOPY NUMBER,
8242         p_acctd_amount_applied_from OUT NOCOPY ar_receivable_applications.acctd_amount_applied_from%TYPE,
8243         p_acctd_amount_applied_to OUT NOCOPY ar_receivable_applications.acctd_amount_applied_to%TYPE,
8244 	x_return_status     OUT NOCOPY VARCHAR2,
8245 	x_msg_count         OUT NOCOPY NUMBER,
8246 	x_msg_data          OUT NOCOPY VARCHAR2) IS
8247 
8248 l_inv_ra_rec     ar_receivable_applications%ROWTYPE;
8249 l_cm_ps_rec      ar_payment_schedules%ROWTYPE;
8250 l_ae_doc_rec     ae_doc_rec_type;
8251    l_source_type                ar_distributions.source_type%TYPE; /* jrautiai BR implementation */
8252 l_flag		 char;
8253   l_fnd_api_constants_rec     ar_bills_main.fnd_api_constants_type     := ar_bills_main.get_fnd_api_constants_rec;
8254 
8255    l_inv_bal_amount             NUMBER;
8256    l_inv_orig_amount            NUMBER;
8257    l_allow_over_application     VARCHAR2(1);
8258    l_effective_amount_applied   NUMBER;
8259   l_application_ref_type  ar_receivable_applications.application_ref_type%TYPE;
8260   l_application_ref_num   ar_receivable_applications.application_ref_num%TYPE;
8261   l_application_ref_id    ar_receivable_applications.application_ref_id%TYPE;
8262   l_secondary_application_ref_id ar_receivable_applications.secondary_application_ref_id%TYPE;
8263   l_attribute_rec             AR_RECEIPT_API_PUB.attribute_rec_type;
8264   l_global_attribute_rec      AR_RECEIPT_API_PUB.global_attribute_rec_type;
8265   l_return_status             VARCHAR2(1);
8266   l_msg_count                 NUMBER;
8267   l_msg_data                  VARCHAR2(2000);
8268   l_msg_index                 NUMBER;
8269   l_exchange_rate             ar_cash_receipts.exchange_rate%TYPE;
8270   functional_curr             VARCHAR2(100);
8271 
8272   l_receivable_application_id   NUMBER;
8273 
8274   API_exception              EXCEPTION;
8275   --Bug#2750340
8276   l_xla_ev_rec      arp_xla_events.xla_events_type;
8277   l_xla_doc_table   VARCHAR2(20);
8278 
8279 BEGIN
8280     IF PG_DEBUG in ('Y', 'C') THEN
8281        arp_standard.debug('arp_process_application.cm_activity_application()+' );
8282     END IF;
8283 
8284     x_return_status := FND_API.G_RET_STS_SUCCESS;
8285 
8286     -- Output IN parameters
8287     IF PG_DEBUG in ('Y', 'C') THEN
8288        arp_standard.debug('CM PS Id.               : '||TO_CHAR( p_cm_ps_id ) );
8289        arp_standard.debug('Amount Applied          : '||TO_CHAR( p_amount_applied ) );
8290        arp_standard.debug('Gl Date                 : '||TO_CHAR( p_gl_date ) );
8291        arp_standard.debug('Apply Date              : '||TO_CHAR( p_apply_date ) );
8292     END IF;
8293 
8294     -----------------------------------------------------
8295 
8296     /* Validate parameters */
8297 
8298     validate_activity_args( p_cm_ps_id, p_application_ps_id,
8299                             null,p_amount_applied,
8300                             p_apply_date, p_gl_date,p_receivables_trx_id );
8301 
8302     validate_activity(p_application_ps_id,'CM_REFUND');
8303     /* Fetch Receipt payment schedule */
8304     arp_ps_pkg.fetch_p( p_cm_ps_id, l_cm_ps_rec );
8305 
8306     l_inv_ra_rec.application_ref_type := 'AP_REFUND_REQUEST';
8307     l_inv_ra_rec.application_ref_num := p_application_ref_num;
8308     l_inv_ra_rec.application_ref_id := p_application_ref_id;
8309     --------------------------------------------------------------------------
8310     -- Process the On-Account Credit...
8311     --
8312     --------------------------------------------------------------------------
8313 
8314     -- Get customer_trx_id of CM, from PS table. Pass the selected row
8315     -- to update_cm_related_columns procedure.
8316     --
8317     --
8318     arp_ps_util.update_cm_related_columns(
8319                 p_cm_ps_id,
8320                 p_amount_applied,
8321                 l_inv_ra_rec.line_applied,
8322                 l_inv_ra_rec.tax_applied,
8323                 l_inv_ra_rec.freight_applied,
8324                 l_inv_ra_rec.receivables_charges_applied,
8325                 p_apply_date,
8326                 p_gl_date,
8327                 l_inv_ra_rec.acctd_amount_applied_from,
8328                 l_cm_ps_rec,
8329 		'Y' );
8330 
8331     select NVL(ctt.post_to_gl,'N') into l_flag
8332     from   ra_cust_trx_types ctt,
8333     	   ar_payment_schedules ps
8334     where  ctt.cust_trx_type_id = ps.cust_trx_type_id
8335     and    ps.payment_schedule_id = p_cm_ps_id;
8336 
8337     If l_flag = 'Y' then
8338 	l_inv_ra_rec.postable := 'Y';
8339     else
8340     	l_inv_ra_rec.postable := 'N';
8341     End if;
8342 
8343     select code_combination_id INTO l_inv_ra_rec.code_combination_id
8344     FROM   ar_receivables_trx
8345     WHERE  receivables_trx_id = p_receivables_trx_id;
8346 
8347     -- This is passed back to the client as the true acctd amount (calculated
8348     -- in the payment schedule utility procedure).
8349     p_acctd_amount_applied_from := l_inv_ra_rec.acctd_amount_applied_from;
8350     l_inv_ra_rec.acctd_amount_applied_to := l_inv_ra_rec.acctd_amount_applied_from;
8351 
8352     IF p_application_ref_id IS NOT NULL THEN
8353        BEGIN
8354           SELECT exchange_rate
8355           INTO   l_exchange_rate
8356           FROM   ap_invoices_v
8357           WHERE  invoice_id = p_application_ref_id;
8358 
8359           functional_curr := arp_global.functional_currency;
8360 
8361           l_inv_ra_rec.acctd_amount_applied_to :=
8362         	ARPCURR.functional_amount(
8363 		  amount	=> p_amount_applied
8364                 , currency_code	=> functional_curr
8365                 , exchange_rate	=> l_exchange_rate
8366                 , precision	=> NULL
8367 		, min_acc_unit	=> NULL );
8368        EXCEPTION
8369            WHEN OTHERS THEN
8370               IF PG_DEBUG in ('Y', 'C') THEN
8371                  arp_standard.debug(
8372 		    'EXCEPTION: arp_process_application.cm_activity_application' );
8373               END IF;
8374               RAISE;
8375        END;
8376     END IF;
8377 
8378     l_inv_ra_rec.customer_trx_id := l_cm_ps_rec.customer_trx_id;
8379     l_inv_ra_rec.payment_schedule_id := p_cm_ps_id;
8380     l_inv_ra_rec.applied_payment_schedule_id := p_application_ps_id;
8381     l_inv_ra_rec.amount_applied := p_amount_applied;
8382     l_inv_ra_rec.amount_applied_from := p_amount_applied;
8383 
8384     l_inv_ra_rec.status := 'ACTIVITY';
8385     l_inv_ra_rec.application_type := 'CM';
8386 
8387     l_inv_ra_rec.application_rule := '75';
8388     l_inv_ra_rec.program_id     := -100105;
8389 
8390     l_inv_ra_rec.apply_date := p_apply_date;
8391     l_inv_ra_rec.gl_date := p_gl_date;
8392     l_inv_ra_rec.posting_control_id := -3;
8393     l_inv_ra_rec.display := 'Y';
8394     l_inv_ra_rec.ussgl_transaction_code := p_ussgl_transaction_code;
8395     l_inv_ra_rec.attribute_category := p_attribute_category;
8396     l_inv_ra_rec.attribute1 := p_attribute1;
8397     l_inv_ra_rec.attribute2 := p_attribute2;
8398     l_inv_ra_rec.attribute3 := p_attribute3;
8399     l_inv_ra_rec.attribute4 := p_attribute4;
8400     l_inv_ra_rec.attribute5 := p_attribute5;
8401     l_inv_ra_rec.attribute6 := p_attribute6;
8402     l_inv_ra_rec.attribute7 := p_attribute7;
8403     l_inv_ra_rec.attribute8 := p_attribute8;
8404     l_inv_ra_rec.attribute9 := p_attribute9;
8405     l_inv_ra_rec.attribute10 := p_attribute10;
8406     l_inv_ra_rec.attribute11 := p_attribute11;
8407     l_inv_ra_rec.attribute12 := p_attribute12;
8408     l_inv_ra_rec.attribute13 := p_attribute13;
8409     l_inv_ra_rec.attribute14 := p_attribute14;
8410     l_inv_ra_rec.attribute15 := p_attribute15;
8411     l_inv_ra_rec.global_attribute_category := p_global_attribute_category;
8412     l_inv_ra_rec.global_attribute1 := p_global_attribute1;
8413     l_inv_ra_rec.global_attribute2 := p_global_attribute2;
8414     l_inv_ra_rec.global_attribute3 := p_global_attribute3;
8415     l_inv_ra_rec.global_attribute4 := p_global_attribute4;
8416     l_inv_ra_rec.global_attribute5 := p_global_attribute5;
8417     l_inv_ra_rec.global_attribute6 := p_global_attribute6;
8418     l_inv_ra_rec.global_attribute7 := p_global_attribute7;
8419     l_inv_ra_rec.global_attribute8 := p_global_attribute8;
8420     l_inv_ra_rec.global_attribute9 := p_global_attribute9;
8421     l_inv_ra_rec.global_attribute10 := p_global_attribute10;
8422     l_inv_ra_rec.global_attribute11 := p_global_attribute11;
8423     l_inv_ra_rec.global_attribute12 := p_global_attribute12;
8424     l_inv_ra_rec.global_attribute13 := p_global_attribute13;
8425     l_inv_ra_rec.global_attribute14 := p_global_attribute14;
8426     l_inv_ra_rec.global_attribute15 := p_global_attribute15;
8427     l_inv_ra_rec.global_attribute16 := p_global_attribute16;
8428     l_inv_ra_rec.global_attribute17 := p_global_attribute17;
8429     l_inv_ra_rec.global_attribute18 := p_global_attribute18;
8430     l_inv_ra_rec.global_attribute19 := p_global_attribute19;
8431     l_inv_ra_rec.global_attribute20 := p_global_attribute20;
8432     l_inv_ra_rec.receivables_trx_id := p_receivables_trx_id;
8433     l_inv_ra_rec.comments := p_comments;
8434 
8435     --------------------------------------------------------------------------
8436     -- Create the APP row in receivable applications.
8437     --
8438     --------------------------------------------------------------------------
8439     arp_app_pkg.insert_p( l_inv_ra_rec,
8440                           l_receivable_application_id );
8441 
8442     l_inv_ra_rec.receivable_application_id := l_receivable_application_id;
8443 
8444     IF l_inv_ra_rec.receivable_application_id IS NOT NULL THEN
8445          l_xla_ev_rec.xla_from_doc_id := l_inv_ra_rec.receivable_application_id;
8446          l_xla_ev_rec.xla_to_doc_id   := l_inv_ra_rec.receivable_application_id;
8447          l_xla_ev_rec.xla_mode        := 'O';
8448          l_xla_ev_rec.xla_call        := 'B';
8449          l_xla_ev_rec.xla_doc_table := 'CMAPP';
8450          ARP_XLA_EVENTS.create_events(p_xla_ev_rec => l_xla_ev_rec);
8451     END IF;
8452     -----------------------------------------------------------------------
8453     -- Process MRC data
8454     -----------------------------------------------------------------------
8455 
8456 --    ar_mrc_engine3.cm_application(
8457 --                     p_cm_ps_id      => p_cm_ps_id,
8458 --                     p_invoice_ps_id => p_application_ps_id,
8459 --                     p_inv_ra_rec    => l_inv_ra_rec,
8460 --                     p_ra_id         => l_inv_ra_rec.receivable_application_id);
8461 
8462     --apandit
8463     --Bug : 2641517 raise business event.
8464     AR_BUS_EVENT_COVER.Raise_CM_Apply_Event(l_inv_ra_rec.receivable_application_id);
8465 
8466    --
8467    --
8468     l_ae_doc_rec.document_type             := 'CREDIT_MEMO';
8469     l_ae_doc_rec.document_id               := l_inv_ra_rec.customer_trx_id;
8470     l_ae_doc_rec.accounting_entity_level   := 'ONE';
8471     l_ae_doc_rec.source_table              := 'RA';
8472     l_ae_doc_rec.source_id                 := l_inv_ra_rec.receivable_application_id;
8473     l_ae_doc_rec.source_id_old             := '';
8474     l_ae_doc_rec.other_flag                := '';
8475     l_ae_doc_rec.pay_sched_upd_yn := 'Y';
8476 
8477     arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
8478 
8479     -- Return the new receivable_application_id
8480     p_out_rec_application_id := l_inv_ra_rec.receivable_application_id;
8481 
8482     IF PG_DEBUG in ('Y', 'C') THEN
8483        arp_standard.debug('arp_process_application.cm_activity_application()-' );
8484     END IF;
8485 
8486     EXCEPTION
8487         WHEN OTHERS THEN
8488               IF PG_DEBUG in ('Y', 'C') THEN
8489                  arp_standard.debug(
8490 		    'EXCEPTION: arp_process_application.cm_activity_application' );
8491               END IF;
8492               RAISE;
8493 END cm_activity_application;
8494 
8495 /*===========================================================================+
8496  | PROCEDURE                                                                 |
8497  |    Unassociate_Regular_CM                                                 |
8498  |                                                                           |
8499  | DESCRIPTION                                                               |
8500  |    This routine will take care of clean up associated with unapplying a   |
8501  |    regular CM (in effect turning it into an on-account CM)                |
8502  |                                                                           |
8503  | SCOPE - PUBLIC                                                            |
8504  |                                                                           |
8505  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED -                                 |
8506  |                                                                           |
8507  | ARGUMENTS  : IN:                                                          |
8508  |                 p_cust_trx_id- Cust Trx ID of the CM                      |
8509  |                 p_app_cust_trx_id - cust trx ID of the original TRX       |
8510  |                              Returned receivable application id           |
8511  | RETURNS    : NONE                                                         |
8512  |                                                                           |
8513  | MODIFICATION HISTORY                                                      |
8514  | 12-Sep-05    Debbie Sue Jancis    Created                                 |
8515  |                                                                           |
8516  +===========================================================================*/
8517 PROCEDURE Unassociate_Regular_CM ( p_cust_Trx_id IN NUMBER,
8518                                p_app_cust_trx_id IN NUMBER) IS
8519 
8520 l_trx_number     ra_customer_trx.trx_number%TYPE;
8521 l_message_text   VARCHAR2(2000);
8522 
8523 BEGIN
8524    arp_standard.debug('arp_process_application.Unapply_Regular_CM()+');
8525 
8526    -- since we are unapplying the regular cm, we need to null out the
8527    -- previous_customer_trx_id and previous_customer_trx_line_id
8528 
8529    Update ra_customer_Trx
8530    set previous_customer_Trx_id = NULL
8531    where customer_Trx_id = p_cust_trx_id
8532    and previous_customer_Trx_id = p_app_cust_trx_id;
8533 
8534    update ra_customer_Trx_lines
8535    set previous_customer_Trx_line_id = NULL,
8536        previous_customer_Trx_id = NULL -- Fix for Bug 6726394 (customer_trx_id
8537    where customer_Trx_id = p_cust_trx_id -- value has to be set to null in ra_customer_Trx_lines)
8538    and previous_customer_Trx_id = p_app_cust_trx_id;
8539 
8540    SELECT trx_number
8541      INTO l_trx_number
8542      FROM ra_customer_trx
8543     WHERE customer_Trx_id = p_app_cust_Trx_id;
8544 
8545     FND_MESSAGE.SET_NAME('AR', 'AR_REG_CM_UNAPP_COMMENTS');
8546     FND_MESSAGE.SET_TOKEN('INVOICE_NUMBER', l_trx_number);
8547     l_message_text := fnd_message.get;
8548 
8549     UPDATE ra_cust_trx_line_gl_dist
8550        SET comments = comments || l_message_text
8551      WHERE customer_trx_id = p_cust_trx_id
8552        AND account_set_flag = 'N';
8553 
8554    arp_standard.debug('arp_process_application.Unassociate_Regular_CM()+');
8555 
8556 END Unassociate_Regular_CM;
8557 
8558 FUNCTION is_regular_cm (p_customer_Trx_id IN NUMBER,
8559                         p_invoicing_rule_id OUT NOCOPY NUMBER) RETURN BOOLEAN IS
8560 
8561 l_prev_cust_trx_id NUMBER;
8562 
8563 BEGIN
8564    arp_standard.debug('arp_process_application.is_regular_cm()+');
8565 
8566    SELECT nvl(previous_customer_Trx_id, -999),
8567           nvl(invoicing_rule_id, -999)
8568    INTO l_prev_cust_trx_id,
8569         p_invoicing_rule_id
8570    FROM ra_customer_trx
8571    WHERE customer_trx_id = p_customer_trx_id;
8572 
8573    IF (l_prev_cust_trx_id = -999) THEN
8574      return FALSE;
8575    ELSE
8576      return TRUE;
8577    END IF;
8578 END is_regular_cm;
8579 
8580 END arp_process_application;