DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_PROCESS_APPLICATION

Source


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