DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_PROCESS_DIST

Source


1 PACKAGE BODY ARP_PROCESS_DIST AS
2 /* $Header: ARTELGDB.pls 120.14 2010/05/07 06:40:20 nemani ship $ */
3 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('AFLOG_ENABLED'), 'N');
4 
5 pg_number_dummy       number;
6 
7 /*===========================================================================+
8  | PROCEDURE                                                                 |
9  |    set_flags								     |
10  |                                                                           |
11  | DESCRIPTION                                                               |
12  |    Sets various change and status flags for the current record.  	     |
13  |                                                                           |
14  | SCOPE - PRIVATE                                                           |
15  |                                                                           |
16  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
17  |    arp_util.debug                                                         |
18  |                                                                           |
19  | ARGUMENTS  : IN:                                                          |
20  | 		     p_cust_trx_line_gl_dist_id				     |
21  |		     p_new_dist_rec 					     |
22  |              OUT:                                                         |
23  |		     p_posted_flag 					     |
24  |		     p_ccid_changed_flag  				     |
25  |		     p_amount_percent_changed_flag 			     |
26  |          IN/ OUT:							     |
27  |                    None						     |
28  |                                                                           |
29  | RETURNS    : NONE                                                         |
30  |                                                                           |
31  | NOTES                                                                     |
32  |                                                                           |
33  | MODIFICATION HISTORY                                                      |
34  |     19-JUL-95  Charlie Tomberg     Created                                |
35  |     12-OCT-95  Martin Johnson      Replaced arp_trx_util.set_posted_flag  |
36  |                                     with IF statment to determine if      |
37  |                                     dist is posted (set_posted_flag       |
38  |                                     determines if trx is posted)          |
39  |                                                                           |
40  +===========================================================================*/
41 
42 PROCEDURE set_flags(p_cust_trx_line_gl_dist_id IN
43                        ra_cust_trx_line_gl_dist.cust_trx_line_gl_dist_id%type,
44                     p_new_dist_rec       IN ra_cust_trx_line_gl_dist%rowtype,
45                     p_posted_flag                  OUT NOCOPY boolean,
46                     p_ccid_changed_flag            OUT NOCOPY boolean,
47                     p_amount_percent_changed_flag  OUT NOCOPY boolean) IS
48 
49   l_old_dist_rec  		  ra_cust_trx_line_gl_dist%rowtype;
50   l_posted_flag                   boolean;
51   l_ccid_changed_flag         boolean;
52   l_amount_percent_changed_flag   boolean;
53 
54 BEGIN
55 
56    arp_util.debug('arp_process_dist.set_flags()+');
57 
58    arp_ctlgd_pkg.fetch_p( l_old_dist_rec,
59                           p_cust_trx_line_gl_dist_id);
60 
61 
62    IF     (
63              l_old_dist_rec.code_combination_id <>
64                                      p_new_dist_rec.code_combination_id AND
65              p_new_dist_rec.code_combination_id <> pg_number_dummy
66           )
67    THEN   l_ccid_changed_flag := TRUE;
68    ELSE   l_ccid_changed_flag := FALSE;
69    END IF;
70 
71    IF     (
72             (
73                nvl(l_old_dist_rec.amount, 0) <> nvl(p_new_dist_rec.amount, 0)
74              AND
75                p_new_dist_rec.amount <> pg_number_dummy
76             ) OR
77             (
78                nvl(l_old_dist_rec.percent, 0) <> nvl(p_new_dist_rec.percent, 0)
79              AND
80                p_new_dist_rec.percent <> pg_number_dummy
81             )
82           )
83    THEN  l_amount_percent_changed_flag := TRUE;
84    ELSE  l_amount_percent_changed_flag := FALSE;
85    END IF;
86 
87    IF ( l_old_dist_rec.gl_posted_date IS NULL )
88      THEN l_posted_flag := FALSE;
89      ELSE l_posted_flag := TRUE;
90    END IF;
91 
92    p_posted_flag	         := l_posted_flag;
93    p_ccid_changed_flag	 	 := l_ccid_changed_flag;
94    p_amount_percent_changed_flag := l_amount_percent_changed_flag;
95 
96    arp_util.debug('p_posted_flag                 = ' ||
97                   arp_trx_util.boolean_to_varchar2(l_posted_flag));
98 
99    arp_util.debug('p_ccid_changed_flag           = ' ||
100                  arp_trx_util.boolean_to_varchar2( l_ccid_changed_flag));
101 
102    arp_util.debug('p_amount_percent_changed_flag = ' ||
103                   arp_trx_util.boolean_to_varchar2(
104                                          l_amount_percent_changed_flag));
105 
106    arp_util.debug('arp_process_dist.set_flags()-');
107 
108 EXCEPTION
109   WHEN OTHERS THEN
110 
111   /*---------------------------------------------+
112    |  Display parameters and raise the exception |
113    +---------------------------------------------*/
114 
115    arp_util.debug('EXCEPTION:  arp_process_dist.set_flags()');
116 
117    arp_util.debug('');
118    arp_util.debug('---------- parameters for set_flags() ---------');
119 
120    arp_util.debug('p_cust_trx_line_gl_dist_id = ' ||
121                   p_cust_trx_line_gl_dist_id);
122 
123    arp_util.debug('');
124 
125    arp_util.debug('---------- new distribution record ----------');
126    arp_ctlgd_pkg.display_dist_rec( p_new_dist_rec );
127    arp_util.debug('');
128 
129    RAISE;
130 
131 END;
132 
133 /*===========================================================================+
134  | PROCEDURE validate_and_default_gl_date                                    |
135  |                                                                           |
136  | DESCRIPTION                                                               |
137  |    Returns a default GL date                                              |
138  |                                                                           |
139  | SCOPE - PRIVATE                                                           |
140  |                                                                           |
141  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
142  |    arp_util.debug                                                         |
143  |                                                                           |
144  | ARGUMENTS  : IN:                                                          |
145  |                    p_gl_date                                              |
146  |                    p_trx_date                                             |
147  |                    p_invoicing_rule_id                                    |
148  |              OUT:                                                         |
149  |                    None						     |
150  |          IN/ OUT:							     |
151  |                    p_default_gl_date                                      |
152  |                                                                           |
153  | RETURNS    : NONE                                                         |
154  |                                                                           |
155  | NOTES                                                                     |
156  |                                                                           |
157  | MODIFICATION HISTORY                                                      |
158  |     08-NOV-95  Martin Johnson      Created                                |
159  |                                                                           |
160  +===========================================================================*/
161 
162 
163 PROCEDURE validate_and_default_gl_date(
164                p_gl_date           IN date,
165                p_trx_date          IN date,
166                p_invoicing_rule_id IN
167                  ra_customer_trx.invoicing_rule_id%type,
168                p_default_gl_date   IN OUT NOCOPY date)
169 IS
170 
171    l_result                boolean;
172    l_gl_date               date;
173    l_trx_date              date;
174    l_defaulting_rule_used  varchar2(300);
175    l_error_message         varchar2(300);
176 
177 BEGIN
178 
179    IF PG_DEBUG in ('Y', 'C') THEN
180       arp_util.debug('arp_process_dist.validate_and_default_gl_date()+');
181    END IF;
182 
183    /* bug-7147479  Use GL Date as passed.Commented out the below lines of code
184       which assigns null when invoicing rule id is not null*/
185    l_gl_date  := p_gl_date;
186    l_trx_date := p_trx_date;
187    /*IF ( p_invoicing_rule_id IS NOT NULL)
188      THEN
189 
190        l_gl_date  := null;
191        l_trx_date := null;
192 
193      ELSE
194 
195        l_gl_date  := p_gl_date;
196        l_trx_date := p_trx_date;
197 
198    END IF;*/
199 
200    l_result := arp_util.validate_and_default_gl_date(
201                                      l_gl_date,
202                                      l_trx_date,
203                                      null,
204                                      null,
205                                      null,
206                                      null,
207                                      null,
208                                      null,
209                                      null,
210                                      p_invoicing_rule_id,
211                                      arp_global.set_of_books_id,
212                                      222,
213                                      p_default_gl_date,
214                                      l_defaulting_rule_used,
215                                      l_error_message );
216 
217    IF PG_DEBUG in ('Y', 'C') THEN
218       arp_util.debug(   'p_default_gl_date: ' ||
219                                 TO_CHAR(p_default_gl_date, 'DD-MON-YYYY') );
220       arp_util.debug(   'l_defaulting_rule_used: ' ||
221                                 l_defaulting_rule_used );
222       arp_util.debug(   'l_error_message: ' || l_error_message );
223    END IF;
224 
225    IF ( not l_result )
226      THEN
227        fnd_message.set_name('AR', 'GENERIC_MESSAGE');
228        fnd_message.set_token('GENERIC_TEXT', l_error_message);
229        app_exception.raise_exception;
230 
231      ELSIF ( p_default_gl_date IS NULL )
232        THEN
233          fnd_message.set_name('AR', 'AR_DIST_BACKOUT_GL_DATE');
234          app_exception.raise_exception;
235 
236    END IF;
237 
238    IF PG_DEBUG in ('Y', 'C') THEN
239       arp_util.debug('arp_process_dist.validate_and_default_gl_date()-');
240    END IF;
241 
242 EXCEPTION
243   WHEN OTHERS THEN
244 
245      /*---------------------------------------------+
246       |  Display parameters and raise the exception |
247       +---------------------------------------------*/
248 
249       IF PG_DEBUG in ('Y', 'C') THEN
250          arp_util.debug(
251             'EXCEPTION:  arp_process_dist.validate_and_default_gl_date()');
252          arp_util.debug(  '');
253          arp_util.debug(
254         '---------- parameters for validate_and_default_gl_date() ---------');
255          arp_util.debug(  'p_gl_date           = ' || p_gl_date );
256          arp_util.debug(  'p_trx_date          = ' || p_trx_date );
257          arp_util.debug(  'p_invoicing_rule_id = ' || p_invoicing_rule_id );
258       END IF;
259 
260       RAISE;
261 
262 END validate_and_default_gl_date;
263 
264 /*===========================================================================+
265  | PROCEDURE                                                                 |
266  |    backout_ccid							     |
267  |                                                                           |
268  | DESCRIPTION                                                               |
269  |    Inserts two records to backout the existing dist record.	             |
270  |    This procedure is called if backout is required and the dist's         |
271  |    code_combination_id has changed.	 		      	             |
272  |                                                                           |
273  | SCOPE - PRIVATE                                                           |
274  |                                                                           |
275  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
276  |    arp_util.debug                                                         |
277  |                                                                           |
278  | ARGUMENTS  : IN:                                                          |
279  |		      p_old_dist_rec					     |
280  |		      p_new_dist_rec					     |
281  |                    p_header_gl_date                                       |
282  |                    p_trx_date                                             |
283  |                    p_invoicing_rule_id                                    |
284  |		      p_exchange_rate					     |
285  |		      p_currency_code					     |
286  |		      p_precision					     |
287  |		      p_mau						     |
288  |              OUT:                                                         |
289  |                    None						     |
290  |          IN/ OUT:							     |
291  |                    None						     |
292  |                                                                           |
293  | RETURNS    : NONE                                                         |
294  |                                                                           |
295  | NOTES                                                                     |
296  |                                                                           |
297  | MODIFICATION HISTORY                                                      |
298  |     19-JUL-95  Charlie Tomberg     Created                                |
299  |     08-NOV-95  Martin Johnson      Made cust_trx_line_salesrep_id and     |
300  |                                      gl_posted_date null for new dist.    |
301  |                                      Call validate_and_default_gl_date()  |
302  |                                      to get gl_date.                      |
303  |                                                                           |
304  +===========================================================================*/
305 
306 PROCEDURE backout_ccid(
307                p_old_dist_rec      IN ra_cust_trx_line_gl_dist%rowtype,
308                p_new_dist_rec      IN ra_cust_trx_line_gl_dist%rowtype,
309                p_header_gl_date    IN date,
310                p_trx_date          IN date,
311                p_invoicing_rule_id IN ra_customer_trx.invoicing_rule_id%type,
312                p_exchange_rate     IN ra_customer_trx.exchange_rate%type,
313                p_currency_code     IN fnd_currencies.currency_code%type,
314                p_precision         IN fnd_currencies.precision%type,
315                p_mau               IN
316                                   fnd_currencies.minimum_accountable_unit%type
317                       )  IS
318 
319    l_cust_trx_line_gl_dist_id
320                    ra_cust_trx_line_gl_dist.cust_trx_line_gl_dist_id%type;
321 
322    l_old_dist_rec   ra_cust_trx_line_gl_dist%rowtype;
323    l_new_dist_rec   ra_cust_trx_line_gl_dist%rowtype;
324 
325    /* Bug 3598021 - 3630436 */
326    l_orig_dist_rec  ra_cust_trx_line_gl_dist%rowtype;
327 
328    l_default_gl_date       date;
329 
330 BEGIN
331 
332    arp_util.debug('arp_process_dist.backout_ccid()+');
333 
334    l_old_dist_rec := p_old_dist_rec;
335    l_new_dist_rec := p_new_dist_rec;
336 
337 
338    /*--------------------------+
339     |    insert the new row    |
340     +--------------------------*/
341 
342 
343    /*---------------------------------------------------------------+
344     |    If a new value was specified in the dist rec passed into   |
345     |    update_dist() use that value. Otherwise, use the  	    |
346     |    value from the original dist line.               	    |
347     +---------------------------------------------------------------*/
348 
349    arp_ctlgd_pkg.merge_dist_recs( l_old_dist_rec,
350                                   l_new_dist_rec,
351                                   l_new_dist_rec);
352 
353    /* Bug 7039029 JVARKEY cust_trx_line_salesrep_id should be retained in
354       the newly created distribution*/
355    --l_new_dist_rec.cust_trx_line_salesrep_id := null;
356    l_new_dist_rec.gl_posted_date := null;
357 
358    /*Bug-7147479 Pass GL DATE in l_default_gl_Date and
359  	Pass NVL(l_old_dist_rec.gl_date,p_header_gl_date)
360      The purpose is to use the GL Date on the old line if it is valid
361    */
362    l_default_gl_date:=NVL(l_old_dist_rec.gl_date,p_header_gl_date);
363    validate_and_default_gl_date(NVL(l_old_dist_rec.gl_date,p_header_gl_date),
364                                 p_trx_date,
365                                 p_invoicing_rule_id,
366                                 l_default_gl_date );
367 
368    l_new_dist_rec.gl_date          := l_default_gl_date;
369    l_new_dist_rec.original_gl_date := l_default_gl_date;
370    l_new_dist_rec.ccid_change_flag := 'Y'; /* Bug 8788491 */
371 
372    /*Bug 9085085 when both CCID and amount are changed we need to correct acctd amount
373      as well for new row being inserted */
374 
375    IF     (
376             (
377                nvl(l_old_dist_rec.amount, 0) <> nvl(l_new_dist_rec.amount, 0)
378              AND
379                p_new_dist_rec.amount <> pg_number_dummy
380             ) OR
381             (
382                nvl(l_old_dist_rec.percent, 0) <> nvl(l_new_dist_rec.percent, 0)
383              AND
384                l_new_dist_rec.percent <> pg_number_dummy
385             )
386           )
387    THEN
388      arp_util.debug('Before setting Acctd amount');
389      l_new_dist_rec.acctd_amount := arpcurr.functional_amount(
390                           l_new_dist_rec.amount,
391                           p_currency_code,
392                           p_exchange_rate,
393                           p_precision,
394                           p_mau);
395    END IF;
396 
397    /*-----------------------------------------------------------+
398     |    Call the table handler to insert the new dist record   |
399     +-----------------------------------------------------------*/
400 
401    arp_ctlgd_pkg.insert_p( l_new_dist_rec,
402                            l_cust_trx_line_gl_dist_id,
403                            p_exchange_rate,
404                            p_currency_code,
405                            p_precision,
406                            p_mau);
407 
408     /* Bug 8788491
409        Posted dist can be modified only once.
410        Dont allow this dist to modify here onwards. */
411     ARP_CTLGD_PKG.set_to_dummy(l_orig_dist_rec );
412     l_orig_dist_rec.ccid_change_flag := 'N';
413     ARP_CTLGD_PKG.update_p( l_orig_dist_rec ,
414                                 l_old_dist_rec.cust_trx_line_gl_dist_id );
415 
416    /* 6325023 - the point of the rec_offset_flag was to keep header level UNEARN
417        rows (all of them) together to make troubleshooting data issues easier.
418        I am removing the code here that sets the rec_offset_flags of the older/
419        existing rows to null as that is contrary to the point of this column
420    /+ Bug 3598021  - 3630436 +/
421    IF l_old_dist_rec.rec_offset_flag = 'Y'
422    THEN
423       ARP_CTLGD_PKG.set_to_dummy(l_orig_dist_rec );
424       l_orig_dist_rec.rec_offset_flag := NULL;
425       ARP_CTLGD_PKG.update_p( l_orig_dist_rec ,
426                                 l_old_dist_rec.cust_trx_line_gl_dist_id );
427    END IF;
428 
429    END of bug 6325023 */
430 
431    /*-------------------------------------+
432     |    backout the original dist row    |
433     +-------------------------------------*/
434 
435    l_old_dist_rec.amount       := -1 * l_old_dist_rec.amount;
436 
437    l_old_dist_rec.acctd_amount := -1 * l_old_dist_rec.acctd_amount;
438 
439    l_old_dist_rec.percent      := -1 * l_old_dist_rec.percent;
440 
441    /* 6325023 - Keep the Srep ID populated.. no reason not to
442    l_old_dist_rec.cust_trx_line_salesrep_id := null; */
443 
444    l_old_dist_rec.gl_posted_date := null;
445 
446    l_old_dist_rec.gl_date          := l_default_gl_date;
447    l_old_dist_rec.original_gl_date := l_default_gl_date;
448 
449    /* 6325023 - do not null rec_offset_flag
450    /+ Bug 3598021 - 3630436  +/
451    l_old_dist_rec.rec_offset_flag := NULL;
452    END of bug 6325023 */
453 
454    l_old_dist_rec.ccid_change_flag := 'N';  /* Bug 8788491 */
455 
456    arp_ctlgd_pkg.insert_p( l_old_dist_rec,
457                            l_cust_trx_line_gl_dist_id,
458                            p_exchange_rate,
459                            p_currency_code,
460                            p_precision,
461                            p_mau);
462 
463    arp_util.debug('arp_process_dist.backout_ccid()-');
464 
465 EXCEPTION
466   WHEN OTHERS THEN
467 
468      /*---------------------------------------------+
469       |  Display parameters and raise the exception |
470       +---------------------------------------------*/
471 
472       arp_util.debug('EXCEPTION:  arp_process_dist.backout_ccid()');
473       arp_util.debug('');
474       arp_util.debug('---------- parameters for backout_ccid() ---------');
475 
476       arp_util.debug('p_header_gl_date    = ' || p_header_gl_date);
477       arp_util.debug('p_trx_date          = ' || p_trx_date);
478       arp_util.debug('p_invoicing_rule_id = ' || p_invoicing_rule_id);
479       arp_util.debug('p_exchange_rate     = ' || p_exchange_rate);
480       arp_util.debug('p_currency_code     = ' || p_currency_code);
481       arp_util.debug('p_precision         = ' || p_precision );
482       arp_util.debug('p_mau               = ' || p_mau );
483 
484       arp_util.debug('');
485 
486       RAISE;
487 
488 END;
489 
490 /*===========================================================================+
491  | PROCEDURE                                                                 |
492  |    backout_amount							     |
493  |                                                                           |
494  | DESCRIPTION                                                               |
495  |    Inserts one record to backout the existing dist record.		     |
496  |    This procedure is called if backout is required and the amount or      |
497  |    percent of a dist record has changed.				     |
498  |                                                                           |
499  | SCOPE - PRIVATE                                                           |
500  |                                                                           |
501  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
502  |    arp_util.debug                                                         |
503  |                                                                           |
504  | ARGUMENTS  : IN:                                                          |
505  | 		      p_old_dist_rec					     |
506  | 		      p_new_dist_rec					     |
507  |                    p_header_gl_date                                       |
508  |                    p_trx_date                                             |
509  |                    p_invoicing_rule_id                                    |
510  |		      p_exchange_rate					     |
511  |		      p_currency_code					     |
512  |		      p_precision					     |
513  |		      p_mau						     |
514  |              OUT:                                                         |
515  |                    None						     |
516  |          IN/ OUT:							     |
517  |                    None						     |
518  |                                                                           |
519  | RETURNS    : NONE                                                         |
520  |                                                                           |
521  | NOTES                                                                     |
522  |                                                                           |
523  | MODIFICATION HISTORY                                                      |
524  |     19-JUL-95  Charlie Tomberg     Created                                |
525  |     08-NOV-95  Martin Johnson      Made cust_trx_line_salesrep_id and     |
526  |                                      gl_posted_date null for new dist.    |
527  |                                      Call validate_and_default_gl_date()  |
528  |                                      to get gl_date.                      |
529  |                                                                           |
530  +===========================================================================*/
531 
532 
533 PROCEDURE backout_amount(
534                p_old_dist_rec      IN ra_cust_trx_line_gl_dist%rowtype,
535                p_new_dist_rec      IN ra_cust_trx_line_gl_dist%rowtype,
536                p_header_gl_date    IN date,
537                p_trx_date          IN date,
538                p_invoicing_rule_id IN ra_customer_trx.invoicing_rule_id%type,
539                p_exchange_rate     IN ra_customer_trx.exchange_rate%type,
540                p_currency_code     IN fnd_currencies.currency_code%type,
541                p_precision         IN fnd_currencies.precision%type,
542                p_mau               IN
543                                   fnd_currencies.minimum_accountable_unit%type
544                         )  IS
545 
546 
547    l_cust_trx_line_gl_dist_id
548                    ra_cust_trx_line_gl_dist.cust_trx_line_gl_dist_id%type;
549 
550    l_old_dist_rec ra_cust_trx_line_gl_dist%rowtype;
551    l_new_dist_rec ra_cust_trx_line_gl_dist%rowtype;
552 
553    l_default_gl_date       date;
554 
555 BEGIN
556 
557    arp_util.debug('arp_process_dist.backout_amount()+');
558 
559    l_old_dist_rec := p_old_dist_rec;
560    l_new_dist_rec := p_new_dist_rec;
561 
562    /*---------------------------------------------------------------+
563     |    create an offsetting record  to preserve the audit trail   |
564     +---------------------------------------------------------------*/
565 
566 
567    l_new_dist_rec.amount  := l_new_dist_rec.amount - l_old_dist_rec.amount;
568 
569    l_new_dist_rec.percent := l_new_dist_rec.percent - l_old_dist_rec.percent;
570 
571    /*---------------------------------------------------------------+
572     |    If a new value was specified in the dist rec passed into   |
573     |    update_dist(), use that value. Otherwise, use the	    |
574     |    value from the original dist record.                	    |
575     +---------------------------------------------------------------*/
576 
577    arp_ctlgd_pkg.merge_dist_recs( l_old_dist_rec,
578                                   l_new_dist_rec,
579                                   l_old_dist_rec);
580 
581   /*--------------------------------------------------------+
582    |  Force insert_p() to recalculate the accounted amount  |
583    |  instead of using the value from the old dist record.  |
584    +--------------------------------------------------------*/
585 
586    l_old_dist_rec.acctd_amount := null;
587 
588    /* 6325023 - no reason to null srep ID
589    l_old_dist_rec.cust_trx_line_salesrep_id := null; */
590 
591    l_old_dist_rec.gl_posted_date := null;
592    /*Bug-7147479 Pass GL DATE in l_default_gl_Date and
593  	Pass NVL(l_old_dist_rec.gl_date,p_header_gl_date)
594      The purpose is to use the GL Date on the old line if it is valid
595    */
596    l_default_gl_date:=NVL(l_old_dist_rec.gl_date,p_header_gl_date);
597    validate_and_default_gl_date(NVL(l_old_dist_rec.gl_date,p_header_gl_date),
598                                 p_trx_date,
599                                 p_invoicing_rule_id,
600                                 l_default_gl_date );
601 
602    l_old_dist_rec.gl_date          := l_default_gl_date;
603    l_old_dist_rec.original_gl_date := l_default_gl_date;
604 
605   /*------------------------------------------------------------+
606    |  Call the table handler to create the backout dist record  |
607    +------------------------------------------------------------*/
608 
609    arp_ctlgd_pkg.insert_p( l_old_dist_rec,
610                            l_cust_trx_line_gl_dist_id,
611                            p_exchange_rate,
612                            p_currency_code,
613                            p_precision,
614                            p_mau);
615 
616 
617    arp_util.debug('arp_process_dist.backout_amount()-');
618 
619 EXCEPTION
620     WHEN OTHERS THEN
621 
622      /*---------------------------------------------+
623       |  Display parameters and raise the exception |
624       +---------------------------------------------*/
625 
626       arp_util.debug('EXCEPTION:  arp_process_dist.backout_amount()');
627       arp_util.debug('');
628       arp_util.debug('---------- parameters for backout_amount() ---------');
629 
630       arp_util.debug('p_header_gl_date    = ' || p_header_gl_date);
631       arp_util.debug('p_trx_date          = ' || p_trx_date);
632       arp_util.debug('p_invoicing_rule_id = ' || p_invoicing_rule_id);
633       arp_util.debug('p_exchange_rate     = ' || p_exchange_rate);
634       arp_util.debug('p_currency_code     = ' || p_currency_code);
635       arp_util.debug('p_precision         = ' || p_precision );
636       arp_util.debug('p_mau               = ' || p_mau );
637 
638       arp_util.debug('');
639 
640       RAISE;
641 
642 END;
643 
644 
645 /*===========================================================================+
646  | PROCEDURE                                                                 |
647  |    val_tax_from_revenue		               		             |
648  |                                                                           |
649  | DESCRIPTION                                                               |
650  |    Validate Revenue Account tax code is used at the Transaction line      |
651  |    if the system option enforces tax code from GL for a completed         |
652  |    transaction. Validation will be performed during completion for        |
653  |    incomplete transactions.                                               |
654  |    This is enforced for On-Account Credit Memos, Debit Memos and Invoices.|
655  |                                                                           |
656  | SCOPE - PRIVATE                                                           |
657  |                                                                           |
658  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
659  |    arp_util.debug                                                         |
660  |                                                                           |
661  | ARGUMENTS  : IN:                                                          |
662  |                    p_dist_rec					     |
663  |              OUT:                                                         |
664  |                    None                                                   |
665  |                                                                           |
666  | RETURNS    : NONE                                                         |
667  |                                                                           |
668  | NOTES                                                                     |
669  |                                                                           |
670  | MODIFICATION HISTORY                                                      |
671  |     02-Oct-97  Mahesh Sabapathy    Created                                |
672  |                                                                           |
673  +===========================================================================*/
674 
675 
676 PROCEDURE val_tax_from_revenue (
677 		p_dist_rec IN ra_cust_trx_line_gl_dist%rowtype ) IS
678 
679 BEGIN
680 
681    IF PG_DEBUG in ('Y', 'C') THEN
682       arp_util.debug('arp_process_dist.val_tax_from_revenue()+');
683    END IF;
684 
685    /* 4594101 - Removing validate_tax_enforcement
686          this is handled by etax during calculation */
687 
688    IF PG_DEBUG in ('Y', 'C') THEN
689       arp_util.debug('arp_process_dist.val_tax_from_revenue()-');
690    END IF;
691 
692 EXCEPTION
693     WHEN OTHERS THEN
694 
695        /*---------------------------------------------+
696         |  Display parameters and raise the exception |
697         +---------------------------------------------*/
698 
699         IF PG_DEBUG in ('Y', 'C') THEN
700            arp_util.debug(
701                'EXCEPTION:  arp_process_dist.val_tax_from_revenue()');
702            arp_util.debug('---------- val_tax_from_revenue() ---------');
703         END IF;
704         arp_ctlgd_pkg.display_dist_rec(p_dist_rec);
705 
706         RAISE;
707 
708 END val_tax_from_revenue;
709 
710 
711 /*===========================================================================+
712  | PROCEDURE                                                                 |
713  |    val_insert_dist		               		                     |
714  |                                                                           |
715  | DESCRIPTION                                                               |
716  |    Does validation necessary when a new dist is inserted.		     |
717  |                                                                           |
718  | SCOPE - PRIVATE                                                           |
719  |                                                                           |
720  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
721  |    arp_util.debug                                                         |
722  |                                                                           |
723  | ARGUMENTS  : IN:                                                          |
724  |                    p_dist_rec					     |
725  |              OUT:                                                         |
726  |                    None                                                   |
727  |                                                                           |
728  | RETURNS    : NONE                                                         |
729  |                                                                           |
730  | NOTES                                                                     |
731  |                                                                           |
732  | MODIFICATION HISTORY                                                      |
733  |     19-JUL-95  Charlie Tomberg     Created                                |
734  |                                                                           |
735  +===========================================================================*/
736 
737 
738 PROCEDURE val_insert_dist ( p_dist_rec IN ra_cust_trx_line_gl_dist%rowtype ) IS
739 
740 
741 BEGIN
742 
743    arp_util.debug('arp_process_dist.val_insert_dist()+');
744 
745 
746    arp_util.debug('arp_process_dist.val_val_insert_dist()-');
747 
748 EXCEPTION
749     WHEN OTHERS THEN
750 
751        /*---------------------------------------------+
752         |  Display parameters and raise the exception |
753         +---------------------------------------------*/
754 
755         arp_util.debug(
756                'EXCEPTION:  arp_process_dist.val_insert_dist()');
757 
758         arp_util.debug('---------- val_insert_dist() ---------');
759         arp_ctlgd_pkg.display_dist_rec(p_dist_rec);
760 
761         RAISE;
762 
763 END;
764 
765 /*===========================================================================+
766  | PROCEDURE                                                                 |
767  |    val_update_dist		          	                             |
768  |                                                                           |
769  | DESCRIPTION                                                               |
770  |    Does validation that is required when a dist is updated.		     |
771  |                                                                           |
772  | SCOPE - PRIVATE                                                           |
773  |                                                                           |
774  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
775  |    arp_util.debug                                                         |
776  |                                                                           |
777  | ARGUMENTS  : IN:                                                          |
778  |	  	      p_dist_rec					     |
779  |              OUT:                                                         |
780  |                    None                                                   |
781  |                                                                           |
782  | RETURNS    : NONE                                                         |
783  |                                                                           |
784  | NOTES                                                                     |
785  |                                                                           |
786  | MODIFICATION HISTORY                                                      |
787  |     19-JUL-95  Charlie Tomberg     Created                                |
788  |                                                                           |
789  +===========================================================================*/
790 
791 PROCEDURE val_update_dist ( p_dist_rec IN  ra_cust_trx_line_gl_dist%rowtype )
792                        IS
793 
794 
795 BEGIN
796 
797    arp_util.debug('arp_process_dist.val_update_dist()+');
798 
799 
800    arp_util.debug('arp_process_dist.val_val_update_dist()-');
801 
802 EXCEPTION
803     WHEN OTHERS THEN
804 
805        /*---------------------------------------------+
806         |  Display parameters and raise the exception |
807         +---------------------------------------------*/
808 
809         arp_util.debug(
810                'EXCEPTION:  arp_process_dist.val_update_dist()');
811 
812 
813         arp_util.debug('---------- val_update_dist() ---------');
814         arp_ctlgd_pkg.display_dist_rec(p_dist_rec);
815 
816         RAISE;
817 
818 END;
819 
820 /*===========================================================================+
821  | PROCEDURE                                                                 |
822  |    val_delete_dist			                                     |
823  |                                                                           |
824  | DESCRIPTION                                                               |
825  |    Does validation that is required when a dist is deleted.		     |
826  |                                                                           |
827  | SCOPE - PRIVATE                                                           |
828  |                                                                           |
829  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
830  |    arp_util.debug                                                         |
831  |                                                                           |
832  | ARGUMENTS  : IN:                                                          |
833  |	  	      p_dist_rec					     |
834  |              OUT:                                                         |
835  |                    None                                                   |
836  |                                                                           |
837  | RETURNS    : NONE                                                         |
838  |                                                                           |
839  | NOTES                                                                     |
840  |                                                                           |
841  | MODIFICATION HISTORY                                                      |
842  |     19-JUL-95  Charlie Tomberg     Created                                |
843  |                                                                           |
844  +===========================================================================*/
845 
846 PROCEDURE val_delete_dist ( p_dist_rec IN ra_cust_trx_line_gl_dist%rowtype ) IS
847 
848 
849 BEGIN
850 
851    arp_util.debug('arp_process_dist.val_delete_dist()+');
852 
853 
854    arp_util.debug('arp_process_dist.val_delete_dist()-');
855 
856 EXCEPTION
857     WHEN OTHERS THEN
858 
859        /*---------------------------------------------+
860         |  Display parameters and raise the exception |
861         +---------------------------------------------*/
862 
863         arp_util.debug('EXCEPTION:  arp_process_dist.val_delete_dist()');
864 
865 
866         arp_util.debug('---------- val_update_dist() ---------');
867         arp_ctlgd_pkg.display_dist_rec(p_dist_rec);
868 
869        RAISE;
870 
871 END;
872 
873 
874 /*===========================================================================+
875  | PROCEDURE                                                                 |
876  |    insert_dist							     |
877  |                                                                           |
878  | DESCRIPTION                                                               |
879  |    Inserts a record into ra_cust_trx_line_gl_dist			     |
880  |                                                                           |
881  | SCOPE - PUBLIC                                                            |
882  |                                                                           |
883  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
884  |    arp_util.debug                                                         |
885  |                                                                           |
886  | ARGUMENTS  : IN:                                                          |
887  |		      p_form_name					     |
888  |		      p_form_version					     |
889  |		      p_dist_rec					     |
890  |		      p_cust_trx_line_gl_dist_id 			     |
891  |		      p_exchange_rate					     |
892  |		      p_currency_code					     |
893  |		      p_precision					     |
894  |		      p_mau						     |
895  |              OUT:                                                         |
896  |                    p_cust_trx_line_gl_dist_id                             |
897  |          IN/ OUT:							     |
898  |                    None						     |
899  |                                                                           |
900  | RETURNS    : NONE                                                         |
901  |                                                                           |
902  | NOTES                                                                     |
903  |                                                                           |
904  | MODIFICATION HISTORY                                                      |
905  |     12-JUL-95  Charlie Tomberg     Created                                |
906  |                                                                           |
907  +===========================================================================*/
908 
909 
910 PROCEDURE insert_dist(
911            p_form_name     IN varchar2,
912            p_form_version  IN number,
913            p_dist_rec	   IN ra_cust_trx_line_gl_dist%rowtype,
914            p_exchange_rate IN ra_customer_trx.exchange_rate%type DEFAULT 1,
915            p_currency_code IN fnd_currencies.currency_code%type  DEFAULT null,
916            p_precision     IN fnd_currencies.precision%type      DEFAULT null,
917            p_mau           IN fnd_currencies.minimum_accountable_unit%type
918                               DEFAULT null,
919            p_cust_trx_line_gl_dist_id  OUT NOCOPY
920               ra_cust_trx_line_gl_dist.cust_trx_line_gl_dist_id%type)
921                    IS
922 
923 
924    l_cust_trx_line_gl_dist_id
925                    ra_cust_trx_line_gl_dist.cust_trx_line_gl_dist_id%type;
926 
927 --bug#2750340
928    l_ev_rec        arp_xla_events.xla_events_type;
929 BEGIN
930 
931       arp_util.debug('arp_process_dist.insert_dist()+');
932 
933       /*----------------------------------------------+
934        |   Check the form version to determine if it  |
935        |   is compatible with the entity handler.     |
936        +----------------------------------------------*/
937 
938       arp_trx_validate.ar_entity_version_check(p_form_name, p_form_version);
939 
940       /*-------------------------+
941        |  Do required validation |
942        +-------------------------*/
943 
944       arp_process_dist.val_insert_dist(p_dist_rec);
945 
946       /*----------------------------------------------------------------+
947        | Lock rows in other tables that reference this customer_trx_id  |
948        +----------------------------------------------------------------*/
949 
950       arp_trx_util.lock_transaction(p_dist_rec.customer_trx_id);
951 
952 
953       arp_ctlgd_pkg.insert_p( p_dist_rec,
954                               p_cust_trx_line_gl_dist_id,
955                               p_exchange_rate,
956                               p_currency_code,
957                               p_precision,
958                               p_mau);
959 
960 
961        /*----------------------------------------------------+
962         |  Validate tax from revenue account.                |
963         +----------------------------------------------------*/
964       val_tax_from_revenue( p_dist_rec );
965 
966 --bug#2750340
967       --------------------------------------------------------------
968       --  Need to call AR XLA events because when user insert the
969       --  a distribution directly through Trx WB, the distributions
970       --  are created by this api.
971       --  Call XLA event with the doc id
972       --------------------------------------------------------------
973       l_ev_rec.xla_from_doc_id   := p_dist_rec.customer_trx_id;
974       l_ev_rec.xla_to_doc_id     := p_dist_rec.customer_trx_id;
975       l_ev_rec.xla_req_id        := NULL;
976       l_ev_rec.xla_doc_table     := 'CT';
977       l_ev_rec.xla_doc_event     := NULL;
978       l_ev_rec.xla_mode          := 'O';
979       l_ev_rec.xla_call          := 'B';
980       l_ev_rec.xla_fetch_size    := 999;
981       arp_xla_events.create_events(p_xla_ev_rec => l_ev_rec );
982 
983       arp_util.debug('arp_process_dist.insert_dist()-');
984 
985 EXCEPTION
986   WHEN OTHERS THEN
987 
988     arp_util.debug('EXCEPTION:  arp_process_dist.insert_dist()');
989 
990    /*---------------------------------------------+
991     |  Display parameters and raise the exception |
992     +---------------------------------------------*/
993 
994     arp_util.debug('EXCEPTION:  arp_process_dist.set_flags()');
995 
996     arp_util.debug('');
997     arp_util.debug('---------- insert_dist() ---------');
998 
999     arp_util.debug('p_form_name         = ' || p_form_name);
1000     arp_util.debug('p_form_version      = ' || p_form_version);
1001     arp_util.debug('p_exchange_rate     = ' || p_exchange_rate);
1002     arp_util.debug('p_currency_code     = ' || p_currency_code);
1003     arp_util.debug('p_precision         = ' || p_precision );
1004     arp_util.debug('p_mau               = ' || p_mau );
1005 
1006     arp_util.debug('');
1007     arp_ctlgd_pkg.display_dist_rec( p_dist_rec );
1008     arp_util.debug('');
1009 
1010     RAISE;
1011 
1012 END;
1013 
1014 /*===========================================================================+
1015  | PROCEDURE                                                                 |
1016  |    update_dist						    	     |
1017  |                                                                           |
1018  | DESCRIPTION                                                               |
1019  |    Updates a record in ra_cust_trx_line_gl_dist			     |
1020  |                                                                           |
1021  | SCOPE - PUBLIC                                                            |
1022  |                                                                           |
1023  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
1024  |    arp_util.debug                                                         |
1025  |                                                                           |
1026  | ARGUMENTS  : IN:                                                          |
1027  |		      p_form_name					     |
1028  |		      p_form_version					     |
1029  |		      p_cust_trx_line_gl_dist_id  			     |
1030  |		      p_customer_trx_id		  			     |
1031  |		      p_dist_rec					     |
1032  |                    p_header_gl_date                                       |
1033  |                    p_trx_date                                             |
1034  |                    p_invoicing_rule_id                                    |
1035  |		      p_exchange_rate					     |
1036  |		      p_currency_code					     |
1037  |		      p_precision					     |
1038  |		      p_mau						     |
1039  |              OUT:                                                         |
1040  |		      p_backout_done_flag                                    |
1041  |          IN/ OUT:							     |
1042  |                    None						     |
1043  |                                                                           |
1044  | RETURNS    : NONE                                                         |
1045  |                                                                           |
1046  | NOTES                                                                     |
1047  |                                                                           |
1048  | MODIFICATION HISTORY                                                      |
1049  |     19-JUL-95  Charlie Tomberg     Created                                |
1050  |     07-NOV-95  Martin Johnson      Added OUT parameter p_backout_done_flag|
1051  |                                    Added IN parameters p_header_gl_date,  |
1052  |                                      p_trx_date, p_invoicing_rule_id      |
1053  |     28-MAY-03  Herve   Yu          Added XLA plug ins bug#2979254         |
1054  |                                                                           |
1055  +===========================================================================*/
1056 
1057 
1058 PROCEDURE update_dist(
1059            p_form_name                 IN varchar2,
1060            p_form_version              IN number,
1061            p_backout_flag              IN boolean,
1062            p_cust_trx_line_gl_dist_id  IN
1063                     ra_cust_trx_line_gl_dist.cust_trx_line_gl_dist_id%type,
1064            p_customer_trx_id	       IN ra_customer_trx.customer_trx_id%type,
1065            p_dist_rec		       IN OUT NOCOPY ra_cust_trx_line_gl_dist%rowtype,
1066            p_header_gl_date            IN date,
1067            p_trx_date                  IN date,
1068            p_invoicing_rule_id         IN
1069                     ra_customer_trx.invoicing_rule_id%type,
1070            p_backout_done_flag         OUT NOCOPY boolean,
1071            p_exchange_rate             IN ra_customer_trx.exchange_rate%type
1072                                           DEFAULT 1,
1073            p_currency_code             IN fnd_currencies.currency_code%type
1074                                           DEFAULT null,
1075            p_precision                 IN fnd_currencies.precision%type
1076                                           DEFAULT null,
1077            p_mau                       IN
1078                                    fnd_currencies.minimum_accountable_unit%type
1079                                    DEFAULT null )
1080                    IS
1081 
1082 
1083    l_cust_trx_line_gl_dist_id
1084                    ra_cust_trx_line_gl_dist.cust_trx_line_gl_dist_id%type;
1085 
1086    l_old_dist_rec	   ra_cust_trx_line_gl_dist%rowtype;
1087 
1088    l_backout_flag	   boolean;
1089    l_posted_flag	   boolean;
1090    l_ccid_changed_flag     boolean;
1091    l_amount_percent_changed_flag   boolean;
1092 
1093    /* Variables l_open_rec, l_ctt_type, l_previous_customer_trx_id
1094       l_ae_doc_rec added for bug 1580221 */
1095 
1096    l_open_rec VARCHAR2(1);
1097    l_ctt_type VARCHAR2(20);
1098    l_previous_customer_trx_id NUMBER;
1099    l_ae_doc_rec arp_acct_main.ae_doc_rec_type;
1100 
1101 --BUG#2750340
1102    l_ev_rec           arp_xla_events.xla_events_type;
1103 
1104 BEGIN
1105 
1106       arp_util.debug('arp_process_dist.update_dist()+');
1107 
1108       /*----------------------------------------------+
1109        |   Check the form version to determine if it  |
1110        |   is compatible with the entity handler.     |
1111        +----------------------------------------------*/
1112 
1113       arp_trx_validate.ar_entity_version_check(p_form_name, p_form_version);
1114 
1115       set_flags(p_cust_trx_line_gl_dist_id,
1116                 p_dist_rec,
1117                 l_posted_flag,
1118                 l_ccid_changed_flag,
1119                 l_amount_percent_changed_flag);
1120 
1121       /*-------------------------+
1122        |  Do required validation |
1123        +-------------------------*/
1124 
1125       arp_process_dist.val_update_dist(p_dist_rec);
1126 
1127       /*----------------------------------------------------------------+
1128        | Lock rows in other tables that reference this customer_trx_id  |
1129        +----------------------------------------------------------------*/
1130 
1131       arp_trx_util.lock_transaction(p_customer_trx_id);
1132 
1133       /*------------------------------------------------------------------+
1134        |  Set the backout flag to true if the transaction has been posted |
1135        |  and the amounts or the account has changed.	                  |
1136        +------------------------------------------------------------------*/
1137 
1138       IF  ( l_posted_flag = TRUE  AND
1139             (
1140               l_ccid_changed_flag           = TRUE   OR
1141               l_amount_percent_changed_flag = TRUE
1142             )
1143           )
1144       THEN l_backout_flag := TRUE;
1145            arp_util.debug('revised backout flag: TRUE');
1146       ELSE l_backout_flag := p_backout_flag;
1147 
1148            arp_util.debug('revised backout flag: ' ||
1149                           arp_trx_util.boolean_to_varchar2(l_backout_flag ));
1150 
1151       END IF;
1152 
1153 	/*------------------------------------------------------+
1154          |  If    backout is not required 			|
1155          |  THEN  do a simple update				|
1156          |  ELSE IF   the ccid has changed			|
1157          |       THEN create two offsetting records		|
1158          |       ELSE IF   the amount has changed		|
1159          |            THEN create one offsetting record		|
1160          |            ELSE do a simple update			|
1161 	 +------------------------------------------------------*/
1162 
1163       IF   (l_backout_flag = FALSE)
1164       THEN
1165  	      /*--------------------------------------+
1166                |  Do a simple update with no backout. |
1167                +--------------------------------------*/
1168 
1169  	      /*-----------------------------------------------+
1170                |  Break the link to ra_cust_trx_line_salesreps |
1171                |  if the amount or account has been changed    |
1172                +-----------------------------------------------*/
1173 
1174   /* Bug 4053374 Link should be kept intact */
1175   /*             IF      (
1176                           p_dist_rec.cust_trx_line_salesrep_id IS NOT NULL AND
1177                           (
1178                              l_amount_percent_changed_flag = TRUE  OR
1179                              l_ccid_changed_flag           = TRUE  OR
1180                              p_dist_rec.code_combination_id = -1
1181                           )
1182                        )
1183                THEN    p_dist_rec.cust_trx_line_salesrep_id := null;
1184                END IF; */
1185 
1186  	      /*------------------------------------------------+
1187                |  Call the table handler to do a simple update  |
1188                +------------------------------------------------*/
1189 
1190                arp_util.debug('simple update - case 1');
1191                arp_ctlgd_pkg.update_p( p_dist_rec,
1192                                        p_cust_trx_line_gl_dist_id,
1193                                        p_exchange_rate,
1194                                        p_currency_code,
1195                                        p_precision,
1196                                        p_mau);
1197 
1198 	/* Start Fix bug 9667061 */
1199                IF  (l_ccid_changed_flag = TRUE AND
1200                    p_dist_rec.account_class <> 'REC' ) THEN
1201                        UPDATE ar_distributions
1202                        SET ref_dist_ccid = p_dist_rec.code_combination_id
1203                        WHERE (source_id, source_table) IN
1204                          (SELECT app.receivable_application_id, 'RA'
1205                           FROM ar_receivable_applications app
1206                           WHERE app.applied_customer_trx_id = p_dist_rec.customer_trx_id
1207                           AND app.posting_control_id = -3
1208                           AND nvl(app.confirmed_flag,    'Y') = 'Y'
1209                           AND EXISTS
1210                            (SELECT 'x'
1211                             FROM ar_distributions ard
1212                             WHERE ard.source_table = 'RA'
1213                             AND ard.source_id = app.receivable_application_id
1214                             AND nvl(ard.ref_cust_trx_line_gl_dist_id,-9999) = p_cust_trx_line_gl_dist_id
1215                             AND nvl(ard.ref_dist_ccid,-9999) <> p_dist_rec.code_combination_id)
1216                          UNION ALL
1217                           SELECT app.receivable_application_id, 'RA'
1218                           FROM ar_receivable_applications app
1219                           WHERE app.customer_trx_id = p_dist_rec.customer_trx_id
1220                           AND app.posting_control_id = -3
1221                           AND nvl(app.confirmed_flag,    'Y') = 'Y'
1222                           AND EXISTS
1223                            (SELECT 'x'
1224                             FROM ar_distributions ard
1225                             WHERE ard.source_table = 'RA'
1226                             AND ard.source_id = app.receivable_application_id
1227                             AND nvl(ard.ref_cust_trx_line_gl_dist_id,-9999) = p_cust_trx_line_gl_dist_id
1228                             AND nvl(ard.ref_dist_ccid,-9999) <> p_dist_rec.code_combination_id)
1229                          UNION ALL
1230                           SELECT adj.adjustment_id, 'ADJ'
1231                           FROM ar_adjustments adj
1232                           WHERE adj.customer_trx_id = p_dist_rec.customer_trx_id
1233                           AND adj.posting_control_id = -3
1234                           AND nvl(adj.status,    'Y') = 'A'
1235                           AND EXISTS
1236                            (SELECT 'x'
1237                             FROM ar_distributions ard
1238                             WHERE ard.source_table = 'ADJ'
1239                             AND ard.source_id = adj.adjustment_id
1240                             AND nvl(ard.ref_cust_trx_line_gl_dist_id,-9999) = p_cust_trx_line_gl_dist_id
1241                             AND nvl(ard.ref_dist_ccid,-9999) <> p_dist_rec.code_combination_id)
1242                          )
1243                        AND ref_cust_trx_line_gl_dist_id = p_cust_trx_line_gl_dist_id;
1244                END IF;
1245 	/* End Fix bug 9667061 */
1246 
1247 	       /* Following IF clause added for bug 1580221. */
1248 
1249                IF  (l_ccid_changed_flag = TRUE AND
1250                    l_amount_percent_changed_flag = FALSE AND
1251                    p_dist_rec.account_class = 'REC' ) THEN
1252                    BEGIN
1253                    SELECT ctt.accounting_affect_flag,
1254                           ctt.type,
1255                           ct.previous_customer_trx_id
1256                      INTO
1257                           l_open_rec,
1258                           l_ctt_type,
1259                           l_previous_customer_trx_id
1260                      FROM ra_cust_trx_types ctt,
1261                           ra_customer_trx ct
1262                     WHERE ct.customer_trx_id   = p_customer_trx_id
1263                       AND ct.cust_trx_type_id  = ctt.cust_trx_type_id;
1264                     EXCEPTION
1265                     WHEN OTHERS THEN
1266                        RAISE;
1267                     END;
1268                     IF  (l_open_rec = 'Y' AND
1269                          l_ctt_type = 'CM' AND
1270                          l_previous_customer_trx_id IS NOT NULL)
1271                     THEN
1272                        DECLARE
1273                        CURSOR del_app_dist IS
1274                        SELECT app.receivable_application_id app_id,
1275                               app.customer_trx_id           trx_id
1276                          FROM    ar_receivable_applications app
1277                         WHERE  app.applied_customer_trx_id  = l_previous_customer_trx_id
1278                           AND  app.customer_trx_id = p_customer_trx_id
1279 			  AND  app.posting_control_id = -3
1280                           AND  NVL(app.confirmed_flag,'Y') = 'Y'
1281                           AND  EXISTS (SELECT 'x'
1282                                        FROM  ar_distributions ard
1283                                        WHERE  ard.source_table = 'RA'
1284                                        AND  ard.source_id    = app.receivable_application_id);
1285                       BEGIN
1286                       FOR l_rec_del_app in del_app_dist LOOP
1287                          l_ae_doc_rec.document_type           := 'CREDIT_MEMO';
1288                          l_ae_doc_rec.document_id             := l_rec_del_app.trx_id;
1289                          l_ae_doc_rec.accounting_entity_level := 'ONE';
1290                          l_ae_doc_rec.source_table            := 'RA';
1291                          l_ae_doc_rec.source_id               := l_rec_del_app.app_id;
1292                          l_ae_doc_rec.source_id_old           := '';
1293                          l_ae_doc_rec.other_flag              := '';
1294 
1295                          l_ae_doc_rec.pay_sched_upd_yn        := 'Y';
1296 
1297                          arp_acct_main.Delete_Acct_Entry(l_ae_doc_rec);
1298                          arp_acct_main.Create_Acct_Entry(l_ae_doc_rec);
1299                       END LOOP;
1300                       END;
1301                     END IF;
1302                END IF;
1303 
1304                p_backout_done_flag := FALSE;
1305 
1306       ELSE
1307            arp_ctlgd_pkg.fetch_p(l_old_dist_rec, p_cust_trx_line_gl_dist_id);
1308 
1309            IF  ( l_ccid_changed_flag = TRUE )
1310            THEN
1311 
1312   	        /*------------------------------------------------+
1313                  |  Insert 2 rows into ra_cust_trx_line_gl_dist   |
1314                  |       1. amount = -<db amount>,		  |
1315                  |          ccid = <db (old) ccid>		  |
1316                  |       2. amount = <displayed amount>,	  |
1317                  |          ccid = <new ccid>			  |
1318                  +------------------------------------------------*/
1319 
1320                 backout_ccid(l_old_dist_rec,
1321                              p_dist_rec,
1322                              p_header_gl_date,
1323                              p_trx_date,
1324                              p_invoicing_rule_id,
1325                              p_exchange_rate,
1326                              p_currency_code,
1327                              p_precision,
1328                              p_mau);
1329 
1330                 p_backout_done_flag := TRUE;
1331 
1332            ELSE IF (l_amount_percent_changed_flag = TRUE)
1333                 THEN
1334 
1335     	            /*------------------------------------------------+
1336                      |  Insert one new row to backout the old amount. |
1337                      |  amount = <displayed amount> - <db amount>     |
1338                      +------------------------------------------------*/
1339 
1340                      backout_amount(l_old_dist_rec,
1341                                     p_dist_rec,
1342                                     p_header_gl_date,
1343                                     p_trx_date,
1344                                     p_invoicing_rule_id,
1345                                     p_exchange_rate,
1346                                     p_currency_code,
1347                                     p_precision,
1348                                     p_mau);
1349 
1350                      p_backout_done_flag := TRUE;
1351 
1352                 ELSE
1353 
1354     	            /*--------------------------------------+
1355                      |  Do a simple update with no backout. |
1356                      |  Nothing of consequence has changed. |
1357                      +--------------------------------------*/
1358 
1359                      arp_util.debug('simple update - case 2');
1360 
1361                      arp_ctlgd_pkg.update_p(p_dist_rec,
1362                                             p_cust_trx_line_gl_dist_id,
1363                                             p_exchange_rate,
1364                                             p_currency_code,
1365                                             p_precision,
1366                                             p_mau);
1367 
1368                      p_backout_done_flag := FALSE;
1369 
1370                 END IF;
1371 
1372            END IF;
1373       END IF;
1374 
1375        /*----------------------------------------------------+
1376         |  Validate tax from revenue account.                |
1377         +----------------------------------------------------*/
1378       val_tax_from_revenue( p_dist_rec );
1379 
1380 --BUG#2750340
1381       --------------------------------------------------------------
1382       --  Need to call AR XLA events because when user update the
1383       --  a distribution directly through Trx WB, the distributions
1384       --  are created by this api.
1385       --  Call XLA event with the doc id to avoid missing any
1386       --  distributions unstamped
1387       --------------------------------------------------------------
1388       l_ev_rec.xla_from_doc_id   := p_dist_rec.customer_trx_id;
1389       l_ev_rec.xla_to_doc_id     := p_dist_rec.customer_trx_id;
1390       l_ev_rec.xla_req_id        := NULL;
1391       l_ev_rec.xla_doc_table     := 'CT';
1392       l_ev_rec.xla_doc_event     := NULL;
1393       l_ev_rec.xla_mode          := 'O';
1394       l_ev_rec.xla_call          := 'B';
1395       l_ev_rec.xla_fetch_size    := 999;
1396       arp_xla_events.create_events(p_xla_ev_rec => l_ev_rec );
1397 
1398      arp_util.debug('arp_process_dist.update_dist()-');
1399 
1400 EXCEPTION
1401   WHEN OTHERS THEN
1402 
1403   /*---------------------------------------------+
1404    |  Display parameters and raise the exception |
1405    +---------------------------------------------*/
1406 
1407     arp_util.debug('EXCEPTION:  arp_process_dist.update_dist()');
1408     arp_util.debug('---------- parameters for update_dist() ---------');
1409     arp_util.debug('p_form_name                 = ' || p_form_name);
1410     arp_util.debug('p_form_version              = ' || p_form_version);
1411 
1412 
1413     arp_util.debug('p_backout_flag              = ' ||
1414                              arp_trx_util.boolean_to_varchar2(p_backout_flag));
1415 
1416     arp_util.debug('p_cust_trx_line_gl_dist_id  = ' ||
1417                    p_cust_trx_line_gl_dist_id);
1418 
1419     arp_util.debug('p_customer_trx_id           = ' ||
1420                    p_customer_trx_id);
1421 
1422     arp_util.debug('p_header_gl_date            = ' || p_header_gl_date );
1423     arp_util.debug('p_trx_date                  = ' || p_trx_date);
1424     arp_util.debug('p_invoicing_rule_id         = ' || p_invoicing_rule_id);
1425 
1426     arp_util.debug('p_exchange_rate             = ' || p_exchange_rate);
1427     arp_util.debug('p_currency_code             = ' || p_currency_code);
1428     arp_util.debug('p_precision                 = ' || p_precision );
1429     arp_util.debug('p_mau                       = ' || p_mau );
1430 
1431     arp_ctlgd_pkg.display_dist_rec( p_dist_rec );
1432 
1433     RAISE;
1434 
1435 END;
1436 
1437 /*===========================================================================+
1438  | PROCEDURE                                                                 |
1439  |    delete_dist							     |
1440  |                                                                           |
1441  | DESCRIPTION                                                               |
1442  |    Deletes a record fromra_cust_trx_line_gl_dist.			     |
1443  |                                                                           |
1444  | SCOPE - PUBLIC                                                            |
1445  |                                                                           |
1446  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
1447  |    arp_util.debug                                                         |
1448  |                                                                           |
1449  | ARGUMENTS  : IN:                                                          |
1450  |		      p_form_name					     |
1451  |		      p_form_version					     |
1452  |		      p_cust_trx_line_gl_dist_id 			     |
1453  |		      p_customer_trx_id 				     |
1454  |		      p_dist_rec					     |
1455  |              OUT:                                                         |
1456  |                    None						     |
1457  |          IN/ OUT:							     |
1458  |                    None						     |
1459  |                                                                           |
1460  | RETURNS    : NONE                                                         |
1461  |                                                                           |
1462  | NOTES                                                                     |
1463  |                                                                           |
1464  | MODIFICATION HISTORY                                                      |
1465  |     19-JUL-95  Charlie Tomberg     Created                                |
1466  |                                                                           |
1467  +===========================================================================*/
1468 
1469 
1470 PROCEDURE delete_dist(
1471            p_form_name                 IN varchar2,
1472            p_form_version              IN number,
1473            p_cust_trx_line_gl_dist_id  IN
1474                     ra_cust_trx_line_gl_dist.cust_trx_line_gl_dist_id%type,
1475            p_customer_trx_id	       IN ra_customer_trx.customer_trx_id%type,
1476            p_dist_rec		       IN ra_cust_trx_line_gl_dist%rowtype)
1477                    IS
1478 
1479 
1480 BEGIN
1481 
1482       arp_util.debug('arp_process_dist.delete_dist()+');
1483 
1484       /*----------------------------------------------+
1485        |   Check the form version to determine if it  |
1486        |   is compatible with the entity handler.     |
1487        +----------------------------------------------*/
1488 
1489       arp_trx_validate.ar_entity_version_check(p_form_name, p_form_version);
1490 
1491       /*-------------------------+
1492        |  Do required validation |
1493        +-------------------------*/
1494 
1495       arp_process_dist.val_delete_dist(p_dist_rec);
1496 
1497       /*----------------------------------------------------------------+
1498        | Lock rows in other tables that reference this customer_trx_id  |
1499        +----------------------------------------------------------------*/
1500 
1501       arp_trx_util.lock_transaction(p_customer_trx_id);
1502 
1503        /*----------------------------------------------------+
1504         |  call the table-handler to delete the dist record  |
1505         +----------------------------------------------------*/
1506 
1507       arp_ctlgd_pkg.delete_p( p_cust_trx_line_gl_dist_id );
1508 
1509        /*----------------------------------------------------+
1510         |  Validate tax from revenue account.                |
1511         +----------------------------------------------------*/
1512       val_tax_from_revenue( p_dist_rec );
1513 
1514       arp_util.debug('arp_process_dist.delete_dist()-');
1515 
1516 EXCEPTION
1517  WHEN OTHERS THEN
1518 
1519    /*---------------------------------------------+
1520     |  Display parameters and raise the exception |
1521     +---------------------------------------------*/
1522 
1523     arp_util.debug('EXCEPTION:  arp_process_dist.delete_dist()');
1524 
1525     arp_util.debug('---------- delete_dist() ---------');
1526 
1527     arp_util.debug('p_form_name                  = ' || p_form_name);
1528     arp_util.debug('p_form_version               = ' || p_form_version);
1529 
1530     arp_util.debug('p_cust_trx_line_gl_dist_id  = ' ||
1531                    p_cust_trx_line_gl_dist_id);
1532 
1533     arp_ctlgd_pkg.display_dist_rec( p_dist_rec );
1534 
1535     RAISE;
1536 
1537 END;
1538 
1539 
1540   /*---------------------------------------------+
1541    |   Package initialization section.           |
1542    +---------------------------------------------*/
1543 
1544 BEGIN
1545 
1546    pg_number_dummy    := arp_ctlgd_pkg.get_number_dummy;
1547 
1548 END ARP_PROCESS_DIST;