DBA Data[Home] [Help]

PACKAGE BODY: APPS.AR_RECEIPT_LIB_PVT

Source


1 PACKAGE BODY ar_receipt_lib_pvt  AS
2 /* $Header: ARXPRELB.pls 120.92.12020000.6 2013/04/25 13:02:33 kgnanase ship $           */
3 
4 G_PKG_NAME   CONSTANT VARCHAR2(30)      := 'AR_RECEIPT_LIB_PVT';
5 
6 G_MSG_UERROR    CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR;
7 G_MSG_ERROR     CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_ERROR;
8 G_MSG_SUCCESS   CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_SUCCESS;
9 G_MSG_HIGH      CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_DEBUG_HIGH;
10 G_MSG_MEDIUM    CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_DEBUG_MEDIUM;
11 G_MSG_LOW       CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_DEBUG_LOW;
12 
13 G_SITE_USE_CACHE_SIZE            BINARY_INTEGER  := 1000;
14 G_CUST_NUM_CACHE_SIZE            BINARY_INTEGER  := 1000;
15 G_CUST_NAME_CACHE_SIZE           BINARY_INTEGER  := 1000;
16 G_METHOD_CACHE_SIZE              BINARY_INTEGER  := 1000;
17 G_CUST_BK_AC_NUM_CACHE_SIZE    BINARY_INTEGER  := 1000;
18 G_CUST_BK_AC_NAME_CACHE_SIZE   BINARY_INTEGER  := 1000;
19 G_REMIT_BK_AC_NUM_CACHE_SIZE   BINARY_INTEGER  := 1000;
20 G_REMIT_BK_AC_NAME_CACHE_SIZE  BINARY_INTEGER  := 1000;
21 G_CURRENCY_CACHE_SIZE            BINARY_INTEGER  := 1000;
22 G_RATE_TYPE_CACHE_SIZE           BINARY_INTEGER  := 1000;
23 G_METHOD_INFO_CACHE_SIZE         BINARY_INTEGER  := 1000;
24 G_EXCHANGE_RATE_CACHE_SIZE       BINARY_INTEGER  := 1000;
25 G_ACTIVITY_NAME_CACHE_SIZE       BINARY_INTEGER  := 1000; -- Bugfix 2853738
26 /* Bug fix 2982212 */
27 G_TAX_CODE_CACHE_SIZE            BINARY_INTEGER  := 1000;
28 G_REF_PAYMENT_CACHE_SIZE         BINARY_INTEGER  := 1000;
29 G_REF_PAYMENT_BATCH_CACHE_SIZE   BINARY_INTEGER  := 1000;
30 G_REF_RECEIPT_CACHE_SIZE         BINARY_INTEGER  := 1000;
31 G_REF_REMITTANCE_CACHE_SIZE      BINARY_INTEGER  := 1000;
32 G_LEGAL_ENTITY_CACHE_SIZE        BINARY_INTEGER  := 1000;
33 
34 /* modified for tca uptake */
35 TYPE Site_Use_Cache_Rec_Type IS RECORD
36                      (id               hz_cust_site_uses.site_use_id%TYPE,
37                       customer_id      hz_cust_acct_sites.cust_account_id%TYPE,
38                       site_use_code    hz_cust_site_uses.site_use_code%TYPE,
39                       location         hz_cust_site_uses.location%TYPE,
40                       primary_flag     VARCHAR2(1)
41                       );
42 TYPE Exchange_Rate_Cache_Rec_Type IS RECORD
43                    (exchange_rate    ar_cash_receipts.exchange_rate%TYPE,
44                     currency         ar_cash_receipts.currency_code%TYPE,
45                     exchange_rate_type ar_cash_receipts.exchange_rate_type%TYPE,
46                     exchange_date    ar_cash_receipts.exchange_date%TYPE
47                    );
48 
49 TYPE Method_Info_Cache_Rec_Type IS RECORD
50 	    (receipt_method_id            ar_cash_receipts.receipt_method_id%TYPE,
51 	     remit_bank_acct_use_id       ar_cash_receipts.remit_bank_acct_use_id%TYPE,
52 	     currency_code                ar_cash_receipts.currency_code%TYPE,
53              called_from                  VARCHAR2(200),
54 	     state                        ar_receipt_classes.creation_status%TYPE,
55 	     creation_method_code         ar_receipt_classes.creation_method_code%TYPE,
56 	     bau_end_date                 ce_bank_acct_uses.end_date%TYPE,
57 	     rm_start_date                ar_receipt_methods.start_date%TYPE,
58 	     rm_end_date                  ar_receipt_methods.end_date%TYPE,
59 	     rma_start_date               ar_receipt_method_accounts.start_date%TYPE,
60 	     rma_end_date                 ar_receipt_method_accounts.end_date%TYPE,
61 	     org_id                       ar_receipt_method_accounts.org_id%TYPE
62 	     );
63 
64 TYPE Id_Cache_Rec_Type IS RECORD
65                  ( id     VARCHAR2(100),
66                    value  VARCHAR2(100)
67                   );
68 
69 
70 TYPE Site_Use_Cache_Tbl_Type IS
71      TABLE OF    Site_Use_Cache_Rec_Type
72      INDEX BY    BINARY_INTEGER;
73 
74 
75 TYPE Exchange_Rate_Cache_Tbl_Type IS
76      TABLE OF    Exchange_Rate_Cache_Rec_Type
77      INDEX BY    BINARY_INTEGER;
78 
79 TYPE Method_Info_Cache_Tbl_Type IS
80      TABLE OF    Method_Info_Cache_Rec_Type
81      INDEX BY    BINARY_INTEGER;
82 
83 TYPE Id_Cache_Tbl_Type IS
84      TABLE OF    Id_Cache_Rec_Type
85      INDEX BY    BINARY_INTEGER;
86 
87 
88 G_Site_Use_Cache_Tbl             Site_Use_Cache_Tbl_Type;
89 G_Cust_Num_Cache_Tbl             Id_Cache_Tbl_Type;
90 G_Cust_Name_Cache_Tbl            Id_Cache_Tbl_Type;
91 G_Method_Cache_Tbl               Id_Cache_Tbl_Type;
92 G_Cust_Bank_Ac_Num_Cache_Tbl     Id_Cache_Tbl_Type;
93 G_Cust_Bank_Ac_Name_Cache_Tbl    Id_Cache_Tbl_Type;
94 G_Remit_Bank_Ac_Num_Cache_Tbl    Id_Cache_Tbl_Type;
95 G_Remit_Bank_Ac_Name_Cache_Tbl   Id_Cache_Tbl_Type;
96 G_Currency_Cache_Tbl             Id_Cache_Tbl_Type;
97 G_Rate_Type_Cache_Tbl            Id_Cache_Tbl_Type;
98 Method_Info_Cache_Tbl            Method_Info_Cache_Tbl_Type;
99 Exchange_Rate_Cache_Tbl          Exchange_Rate_Cache_Tbl_Type;
100 G_Activity_Name_Cache_Tbl        Id_Cache_Tbl_Type; -- Bugfix 2853738
101 /* Bug fix 2982212 */
102 G_Tax_Code_Cache_Tbl             Id_Cache_Tbl_Type;
103 G_Ref_Payment_Cache_Tbl          Id_Cache_Tbl_Type;
104 G_Ref_Payment_Batch_Cache_Tbl    Id_Cache_Tbl_Type;
105 G_Ref_Receipt_Cache_Tbl          Id_Cache_Tbl_Type;
106 G_Ref_Remittance_Cache_Tbl       Id_Cache_Tbl_Type;
107 G_Legal_Entity_Cache_Tbl         Id_Cache_Tbl_Type;
108 
109 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('AFLOG_ENABLED'), 'N');
110 
111 PROCEDURE Configure_Library_Caches(
112                 p_site_use_cache     IN BINARY_INTEGER   DEFAULT 1000,
113                 p_cust_number_cache  IN BINARY_INTEGER   DEFAULT 1000,
114                 p_cust_name_cache    IN BINARY_INTEGER   DEFAULT 1000,
115                 p_receipt_method_cache IN BINARY_INTEGER  DEFAULT 1000,
116                 p_cust_bank_ac_num_cache  IN BINARY_INTEGER  DEFAULT 1000,
117                 p_cust_bank_ac_name_cache IN BINARY_INTEGER  DEFAULT 1000,
118                 p_remit_bank_ac_num_cache IN BINARY_INTEGER  DEFAULT 1000,
119                 p_remit_bank_ac_name_cache IN BINARY_INTEGER DEFAULT 1000,
120                 p_currency_cache          IN BINARY_INTEGER  DEFAULT 1000,
121                 p_rate_type_cache         IN BINARY_INTEGER  DEFAULT 1000,
122                 p_method_info_cache       IN BINARY_INTEGER  DEFAULT 1000,
123                 p_exchange_rate_cache     IN BINARY_INTEGER  DEFAULT 1000
124                 )  IS
125 BEGIN
126    IF PG_DEBUG in ('Y', 'C') THEN
127       arp_util.debug('Configure_Library_Caches()+ ');
128    END IF;
129 
130 G_SITE_USE_CACHE_SIZE            := p_site_use_cache;
131 G_CUST_NUM_CACHE_SIZE            := p_cust_number_cache;
132 G_CUST_NAME_CACHE_SIZE           := p_cust_name_cache;
133 G_METHOD_CACHE_SIZE              := p_receipt_method_cache;
134 G_CUST_BK_AC_NUM_CACHE_SIZE    := p_cust_bank_ac_num_cache;
135 G_CUST_BK_AC_NAME_CACHE_SIZE   := p_cust_bank_ac_name_cache;
136 G_REMIT_BK_AC_NUM_CACHE_SIZE   := p_remit_bank_ac_num_cache;
137 G_REMIT_BK_AC_NAME_CACHE_SIZE  := p_remit_bank_ac_name_cache;
138 G_CURRENCY_CACHE_SIZE            := p_currency_cache;
139 G_RATE_TYPE_CACHE_SIZE           := p_rate_type_cache;
140 G_METHOD_INFO_CACHE_SIZE         := p_method_info_cache;
141 G_EXCHANGE_RATE_CACHE_SIZE       := p_exchange_rate_cache;
142 
143    IF PG_DEBUG in ('Y', 'C') THEN
144       arp_util.debug('Configure_Library_Caches()- ');
145    END IF;
146 
147 EXCEPTION
148    WHEN OTHERS THEN
149       IF PG_DEBUG in ('Y', 'C') THEN
150          arp_util.debug('EXCEPTION: Configure_Library_Caches() ');
151       END IF;
152       RAISE;
153 
154 END Configure_Library_Caches;
155 PROCEDURE Add_Value_To_Cache(p_cache_name  IN VARCHAR2,
156                              p_value       IN VARCHAR2,
157                              p_id          IN VARCHAR2,
158                              p_index       IN BINARY_INTEGER
159                             ) IS
160 
161 BEGIN
162 
163    IF PG_DEBUG in ('Y', 'C') THEN
164       arp_util.debug('Add_Value_To_Cache()+ ');
165    END IF;
166 
167    IF        ( p_cache_name = 'CUSTOMER_NUMBER' )
168     THEN
169              IF   ( p_index <= G_CUST_NUM_CACHE_SIZE )
170              THEN
171                   G_Cust_Num_Cache_Tbl( p_index ).value := p_value;
172                   G_Cust_Num_Cache_Tbl( p_index ).id    := p_id;
173              END IF;
174 
175    ELSIF     ( p_cache_name = 'CUSTOMER_NAME' )
176     THEN
177              IF   ( p_index <= G_CUST_NAME_CACHE_SIZE )
178              THEN
179                   G_Cust_Name_Cache_Tbl( p_index ).value := p_value;
180                   G_Cust_Name_Cache_Tbl( p_index ).id    := p_id;
181              END IF;
182    ELSIF     ( p_cache_name = 'RECEIPT_METHOD_NAME' )
183     THEN
184              IF   ( p_index <= G_METHOD_CACHE_SIZE )
185              THEN
186                   G_Method_Cache_Tbl( p_index ).value := p_value;
187                   G_Method_Cache_Tbl( p_index ).id    := p_id;
188              END IF;
189    ELSIF     ( p_cache_name = 'CUST_BANK_ACCOUNT_NUMBER' )
190     THEN
191              IF   ( p_index <= G_CUST_BK_AC_NUM_CACHE_SIZE )
192              THEN
193                   G_Cust_Bank_Ac_Num_Cache_Tbl( p_index ).value := p_value;
194                   G_Cust_Bank_Ac_Num_Cache_Tbl( p_index ).id    := p_id;
195              END IF;
196    ELSIF     ( p_cache_name = 'CUST_BANK_ACCOUNT_NAME' )
197     THEN
198              IF   ( p_index <= G_CUST_BK_AC_NAME_CACHE_SIZE )
199              THEN
200                   G_Cust_Bank_Ac_Name_Cache_Tbl( p_index ).value := p_value;
201                   G_Cust_Bank_Ac_Name_Cache_Tbl( p_index ).id    := p_id;
202              END IF;
203    ELSIF     ( p_cache_name = 'REMIT_BANK_ACCOUNT_NUMBER' )
204     THEN
205              IF   ( p_index <= G_REMIT_BK_AC_NUM_CACHE_SIZE )
206              THEN
207                   G_Remit_Bank_Ac_Num_Cache_Tbl( p_index ).value := p_value;
208                   G_Remit_Bank_Ac_Num_Cache_Tbl( p_index ).id    := p_id;
209              END IF;
210    ELSIF     ( p_cache_name = 'REMIT_BANK_ACCOUNT_NAME' )
211     THEN
212              IF   ( p_index <= G_REMIT_BK_AC_NAME_CACHE_SIZE )
213              THEN
214                   G_Remit_Bank_Ac_Name_Cache_Tbl( p_index ).value := p_value;
215                   G_Remit_Bank_Ac_Name_Cache_Tbl( p_index ).id    := p_id;
216              END IF;
217    ELSIF     ( p_cache_name = 'CURRENCY_NAME' )
218     THEN
219              IF   ( p_index <= G_CURRENCY_CACHE_SIZE )
220              THEN
221                   G_Currency_Cache_Tbl( p_index ).value := p_value;
222                   G_Currency_Cache_Tbl( p_index ).id    := p_id;
223              END IF;
224    ELSIF     ( p_cache_name = 'EXCHANGE_RATE_TYPE_NAME' )
225     THEN
226              IF   ( p_index <= G_RATE_TYPE_CACHE_SIZE )
227              THEN
228                   G_Rate_Type_Cache_Tbl( p_index ).value := p_value;
229                   G_Rate_Type_Cache_Tbl( p_index ).id    := p_id;
230              END IF;
231                -- Bugfix 2853738
232     ELSIF     ( p_cache_name = 'RECEIVABLES_ACTIVITY')
233      THEN
234               IF ( p_index <= G_ACTIVITY_NAME_CACHE_SIZE)
235               THEN
236                    G_Activity_Name_Cache_Tbl( p_index ).value := p_value;
237                    G_Activity_Name_Cache_Tbl( p_index ).id    := p_id;
238               END IF;
239               /* Bug fix 2982212 */
240     ELSIF     ( p_cache_name = 'TAX_CODE')
241      THEN
242               IF ( p_index <= G_TAX_CODE_CACHE_SIZE)
243               THEN
244                    G_Tax_Code_Cache_Tbl( p_index ).value := p_value;
245                    G_Tax_Code_Cache_Tbl( p_index ).id    := p_id;
246               END IF;
247     ELSIF     ( p_cache_name = 'REF_PAYMENT')
248      THEN
249               IF ( p_index <= G_REF_PAYMENT_CACHE_SIZE)
250               THEN
251                    G_Ref_Payment_Cache_Tbl( p_index ).value := p_value;
252                    G_Ref_Payment_Cache_Tbl( p_index ).id    := p_id;
253               END IF;
254     ELSIF     ( p_cache_name = 'REF_PAYMENT_BATCH')
255      THEN
256               IF ( p_index <= G_REF_PAYMENT_BATCH_CACHE_SIZE )
257               THEN
258                    G_Ref_Payment_Batch_Cache_Tbl( p_index ).value := p_value;
259                    G_Ref_Payment_Batch_Cache_Tbl( p_index ).id    := p_id;
260               END IF;
261     ELSIF     ( p_cache_name = 'REF_RECEIPT')
262      THEN
263               IF ( p_index <= G_REF_RECEIPT_CACHE_SIZE)
264               THEN
265                    G_Ref_Receipt_Cache_Tbl( p_index ).value := p_value;
266                    G_Ref_Receipt_Cache_Tbl( p_index ).id    := p_id;
267               END IF;
268     ELSIF     ( p_cache_name = 'REF_REMITTANCE')
269      THEN
270               IF ( p_index <= G_REF_REMITTANCE_CACHE_SIZE)
271               THEN
272                    G_Ref_Remittance_Cache_Tbl( p_index ).value := p_value;
273                    G_Ref_Remittance_Cache_Tbl( p_index ).id    := p_id;
274               END IF;
275     ELSIF     ( p_cache_name = 'LEGAL_ENTITY')
276      THEN
277               IF ( p_index <= G_LEGAL_ENTITY_CACHE_SIZE)
278               THEN
279                    G_Legal_Entity_Cache_Tbl( p_index ).value := p_value;
280                    G_Legal_Entity_Cache_Tbl( p_index ).id    := p_id;
281               END IF;
282     END IF;
283    IF PG_DEBUG in ('Y', 'C') THEN
284       arp_util.debug('Add_Value_To_Cache()- ');
285    END IF;
286 
287 EXCEPTION
288   WHEN OTHERS THEN
289    IF PG_DEBUG in ('Y', 'C') THEN
290       arp_util.debug('EXCEPTION: Add_Value_To_Cache. Cache: ' ||
291                                                 p_cache_name || ' ' ||
292                                  '  Value: ' || p_value);
293    END IF;
294         RAISE;
295 
296 END Add_Value_To_Cache;
297 
298 FUNCTION Find_Value_In_Cache(
299                      p_cache_name   IN VARCHAR2,
300                      p_value        IN VARCHAR2,
301                      p_index       OUT NOCOPY BINARY_INTEGER
302                    ) RETURN VARCHAR2 IS
303 
304    l_index  BINARY_INTEGER;
305    l_count  BINARY_INTEGER;
306 
307 BEGIN
308 
309    IF PG_DEBUG in ('Y', 'C') THEN
310       arp_util.debug('Find_Value_In_Cache()+ ');
311    END IF;
312 
313    IF       (p_cache_name = 'CUSTOMER_NUMBER')
314     THEN
315      l_count := G_Cust_Num_Cache_Tbl.count;
316 
317    ELSIF    (p_cache_name = 'CUSTOMER_NAME')
318     THEN
319      l_count := G_Cust_Name_Cache_Tbl.count;
320 
321    ELSIF    (p_cache_name = 'RECEIPT_METHOD_NAME')
322     THEN
323      l_count := G_Method_Cache_Tbl.count;
324 
325    ELSIF    (p_cache_name = 'CUST_BANK_ACCOUNT_NUMBER')
326     THEN
327      l_count := G_Cust_Bank_Ac_Num_Cache_Tbl.count;
328 
329    ELSIF    (p_cache_name = 'CUST_BANK_ACCOUNT_NAME')
330     THEN
331      l_count := G_Cust_Bank_Ac_Name_Cache_Tbl.count;
332 
333    ELSIF    (p_cache_name = 'REMIT_BANK_ACCOUNT_NUMBER')
334     THEN
335      l_count := G_Remit_Bank_Ac_Num_Cache_Tbl.count;
336 
337    ELSIF    (p_cache_name = 'REMIT_BANK_ACCOUNT_NAME')
338     THEN
339      l_count := G_Remit_Bank_Ac_Name_Cache_Tbl.count;
340 
341    ELSIF    (p_cache_name = 'CURRENCY_NAME')
342     THEN
343      l_count := G_Currency_Cache_Tbl.count;
344 
345    ELSIF    (p_cache_name = 'EXCHANGE_RATE_TYPE_NAME')
346     THEN
347      l_count := G_Rate_Type_Cache_Tbl.count;
348 
349     -- Bugfix 2853738
350     ELSIF    (p_cache_name = 'RECEIVABLES_ACTIVITY')
351      THEN
352       l_count := G_Activity_Name_Cache_Tbl.count ;
353 
354     /* Bugfix 2982212 */
355     ELSIF    (p_cache_name = 'TAX_CODE')
356      THEN
357       l_count := G_Tax_Code_Cache_Tbl.count ;
358 
359     ELSIF    (p_cache_name = 'REF_PAYMENT')
360      THEN
361       l_count := G_Ref_Payment_Cache_Tbl.count ;
362 
363     ELSIF    (p_cache_name = 'REF_PAYMENT_BATCH')
364      THEN
365       l_count := G_Ref_Payment_Batch_Cache_Tbl.count ;
366 
367     ELSIF    (p_cache_name = 'REF_RECEIPT')
368      THEN
369       l_count := G_Ref_Receipt_Cache_Tbl.count ;
370 
371     ELSIF    (p_cache_name = 'REF_REMITTANCE')
372      THEN
373       l_count := G_Ref_Remittance_Cache_Tbl.count ;
374 
375     ELSIF    (p_cache_name = 'LEGAL_ENTITY')
376      THEN
377       l_count := G_Legal_Entity_Cache_Tbl.count ;
378    END IF;
379 
380 
381    FOR l_index IN 1..l_count LOOP
382 
383        p_index := l_index;
384 
385        IF    ( p_cache_name = 'CUSTOMER_NAME'
386               AND G_Cust_Name_Cache_Tbl(l_index).value = p_value)
387         THEN
388          RETURN( G_Cust_Name_Cache_Tbl(l_index).id);
389 
390        ELSIF (p_cache_name = 'CUSTOMER_NUMBER' /* Bug 2982212*/
391               AND G_Cust_Num_Cache_Tbl(l_index).value = p_value)
392         THEN
393          RETURN( G_Cust_Num_Cache_Tbl(l_index).id);
394 
395        ELSIF (p_cache_name = 'RECEIPT_METHOD_NAME' /* Bug 2982212*/
396               AND G_Method_Cache_Tbl(l_index).value = p_value)
397         THEN
398          RETURN( G_Method_Cache_Tbl(l_index).id);
399 
400        ELSIF (p_cache_name = 'CUST_BANK_ACCOUNT_NUMBER'
401               AND G_Cust_Bank_Ac_Num_Cache_Tbl(l_index).value = p_value)
402         THEN
403          RETURN(G_Cust_Bank_Ac_Num_Cache_Tbl(l_index).id);
404 
405        ELSIF (p_cache_name = 'CUST_BANK_ACCOUNT_NAME'
406               AND G_Cust_Bank_Ac_Name_Cache_Tbl(l_index).value = p_value)
407         THEN
408          RETURN(G_Cust_Bank_Ac_Name_Cache_Tbl(l_index).id);
409 
410        ELSIF (p_cache_name = 'REMIT_BANK_ACCOUNT_NUMBER'
411                AND G_Remit_Bank_Ac_Num_Cache_Tbl(l_index).value = p_value)
412         THEN
413          RETURN(G_Remit_Bank_Ac_Num_Cache_Tbl(l_index).id);
414 
415        ELSIF (p_cache_name = 'REMIT_BANK_ACCOUNT_NAME'
416                AND G_Remit_Bank_Ac_Name_Cache_Tbl(l_index).value = p_value)
417         THEN
418          RETURN(G_Remit_Bank_Ac_Name_Cache_Tbl(l_index).id);
419 
420       -- bug2680657 : Added p_value condition for CURRENCY_NAME and
421       -- EXCHANGE_RATE_TYPE_NAME
422       ELSIF (p_cache_name = 'CURRENCY_NAME'
423                AND G_Currency_Cache_Tbl(l_index).value = p_value)
424        THEN
425          RETURN(G_Currency_Cache_Tbl(l_index).id);
426 
427       ELSIF (p_cache_name = 'EXCHANGE_RATE_TYPE_NAME'
428                AND G_Rate_Type_Cache_Tbl(l_index).value = p_value)
429        THEN
430         RETURN(G_Rate_Type_Cache_Tbl(l_index).id);
431 
432          -- Bugfix 2853738
433       ELSIF (p_cache_name = 'RECEIVABLES_ACTIVITY'
434                AND G_Activity_Name_Cache_Tbl(l_index).value = p_value)
435        THEN
436           RETURN( G_Activity_Name_Cache_Tbl(l_index).id);
437 
438          /* Bugfix 2982212 */
439       ELSIF (p_cache_name = 'TAX_CODE'
440                AND G_Tax_Code_Cache_Tbl(l_index).value = p_value)
441        THEN
442           RETURN( G_Tax_Code_Cache_Tbl(l_index).id);
443 
444       ELSIF (p_cache_name = 'REF_PAYMENT'
445                AND G_Ref_Payment_Cache_Tbl(l_index).value = p_value)
446        THEN
447           RETURN( G_Ref_Payment_Cache_Tbl(l_index).id);
448 
449       ELSIF (p_cache_name = 'REF_PAYMENT_BATCH'
450                AND G_Ref_Payment_Batch_Cache_Tbl(l_index).value = p_value)
451        THEN
452           RETURN( G_Ref_Payment_Batch_Cache_Tbl(l_index).id);
453 
454       ELSIF (p_cache_name = 'REF_RECEIPT'
455                AND G_Ref_Receipt_Cache_Tbl(l_index).value = p_value)
456        THEN
457           RETURN( G_Ref_Receipt_Cache_Tbl(l_index).id);
458 
459       ELSIF (p_cache_name = 'REF_REMITTANCE'
460                AND G_Ref_Remittance_Cache_Tbl(l_index).value = p_value)
461        THEN
462           RETURN( G_Ref_Remittance_Cache_Tbl(l_index).id);
463 
464       ELSIF (p_cache_name = 'LEGAL_ENTITY'
465                AND G_Legal_Entity_Cache_Tbl(l_index).value = p_value)
466        THEN
467           RETURN( G_Legal_Entity_Cache_Tbl(l_index).id);
468       END IF;
469 
470   END LOOP;
471 
472    IF PG_DEBUG in ('Y', 'C') THEN
473       arp_util.debug('Find_Value_In_Cache()- ');
474    END IF;
475 
476    RETURN(NULL);
477 
478 EXCEPTION
479 
480    WHEN OTHERS THEN
481         IF PG_DEBUG in ('Y', 'C') THEN
482            arp_util.debug('EXCEPTION: Find_Value_In_Cache. Cache: ' ||
483                                                 p_cache_name || ' ' ||
484                                  '  Value: ' || p_value);
485         END IF;
486 
487         RAISE;
488 
489 END Find_Value_In_Cache;
490 
491 
492 
493 FUNCTION Get_Id(
494                   p_entity    IN VARCHAR2,
495                   p_value     IN VARCHAR2,
496                   p_return_status OUT NOCOPY VARCHAR2,
497                   p_date      IN DATE DEFAULT NULL
498                ) RETURN VARCHAR2 IS
499 
500 l_cached_id    VARCHAR2(100);
501 l_selected_id  VARCHAR2(100);
502 l_index        BINARY_INTEGER;
503 l_date         DATE;    -- ETAX: bug 4594101
504 
505 BEGIN
506 
507       IF PG_DEBUG in ('Y', 'C') THEN
508          arp_util.debug('Get_Id()+ ');
509       END IF;
510 
511       IF    ( p_value  IS NULL )
512       THEN
513 
514            RETURN(NULL);
515 
516       ELSE
517            l_cached_id := Find_Value_In_Cache( p_entity,
518                                                p_value,
519                                                l_index
520                                              );
521 
522            IF ( l_cached_id  IS NOT NULL )
523            THEN
524 
525                 RETURN( l_cached_id );
526 
527            ELSE
528                 IF      ( p_entity = 'CUSTOMER_NUMBER' )
529                 THEN
530 
531                     /* modified for tca uptake */
532                     /* fixed bug 1544201:  removed customer_prospect_code */
533                     SELECT c.cust_account_id
534                     INTO   l_selected_id
535                     FROM   hz_cust_accounts c,
536                            hz_customer_profiles cp,
537                            hz_parties party
538                     WHERE  c.cust_account_id = cp.cust_account_id (+) and
539                            cp.site_use_id is null and
540                            c.account_number = p_value
541                       AND  c.party_id = party.party_id;
542 
543                 ELSIF ( p_entity = 'CUSTOMER_NAME' )
544                  THEN
545 
546                      /* modified for tca uptake */
547                      /* fixed bug 1544201:  removed customer_prospect_code */
548                     SELECT cust_acct.cust_account_id
549                     INTO   l_selected_id
550                     FROM   hz_cust_accounts cust_acct,
551                            hz_customer_profiles cp,
552                            hz_parties party
553                     WHERE  cust_acct.cust_account_id = cp.cust_account_id (+)
554                       and  cust_acct.party_id = party.party_id(+)
555                       and  cp.site_use_id is null
556                       and  party.party_name = p_value;
557 
558                 ELSIF (p_entity = 'RECEIPT_METHOD_NAME' )
559 
560                  THEN
561 
562                     SELECT receipt_method_id
563                     INTO   l_selected_id
564                     FROM   ar_receipt_methods
565                     WHERE  name = p_value;
566 /* PAYMENT UPTAKE  removed the following
567      a) getting the bank_account_id from CUST_BANK_ACCOUNT_NUMBER.
568      b) getting the bank_account_id from CUST_BANK_ACCOUNT_NAME.
569 */
570 
571                 ELSIF  (p_entity = 'REMIT_BANK_ACCOUNT_NUMBER')
572                  THEN
573                     SELECT cbau.bank_acct_use_id
574                     INTO   l_selected_id
575                     FROM   ce_bank_accounts cba, ce_bank_acct_uses cbau
576                     WHERE  bank_account_num = p_value
577 		    and    cba.bank_account_id = cbau.bank_account_id;
578 
579                 ELSIF (p_entity = 'REMIT_BANK_ACCOUNT_NAME')
580                   THEN
581                     SELECT cbau.bank_acct_use_id
582                     INTO   l_selected_id
583                     FROM   ce_bank_accounts cba, ce_bank_acct_uses cbau
584                     WHERE  bank_account_name = p_value
585 		    and    cba.bank_account_id = cbau.bank_account_id;
586 
587                 ELSIF (p_entity = 'CURRENCY_NAME')
588                    THEN
589                     SELECT currency_code
590                     INTO   l_selected_id
591                     FROM   fnd_currencies_vl
592                     WHERE  name = p_value;
593                 ELSIF (p_entity = 'EXCHANGE_RATE_TYPE_NAME')
594                    THEN
595                     SELECT conversion_type
596                     INTO   l_selected_id
597                     FROM   gl_daily_conversion_types
598                     WHERE  user_conversion_type = p_value ;
599 
600                 ELSIF (p_entity = 'REF_PAYMENT' ) THEN
601                     --get from ap_checks.
602                     select check_id
603                     into   l_selected_id
604                     from   ap_checks
605                     where  check_number = p_value;
606                 ELSIF (p_entity = 'REF_PAYMENT_BATCH' ) THEN
607                     --
608                     select isc.checkrun_id
609                     into   l_selected_id
610                     from   ap_invoice_selection_criteria isc
611                     where  isc.checkrun_name = p_value;
612                 ELSIF (p_entity = 'REF_RECEIPT' ) THEN
613                     --
614                     select cash_receipt_id
615                     into   l_selected_id
616                     from   ar_cash_receipts
617                     where  receipt_number = p_value;
618                 ELSIF (p_entity = 'REF_REMITTANCE' ) THEN
619                     --
620                     select batch_id
621                     into   l_selected_id
622                     from   ar_batches
623                     where  name = p_value
624                      and   type = 'REMITTANCE' ;
625                 ELSIF (p_entity = 'RECEIVABLES_ACTIVITY' ) THEN
626                     --
627                     select receivables_trx_id
628                     into   l_selected_id
629                     from   ar_receivables_trx
630                     where  name = p_value;
631                 ELSIF (p_entity = 'TAX_CODE' ) THEN
632                     --
633                     -- ETAX replaced reference of obsoleted ar vat tax
634 
635                     if (p_date is NULL) THEN
636                         l_date := SYSDATE;
637                     end if;
638 
639                     SELECT tax_rate_id
640                       INTO l_selected_id
641                       from zx_sco_rates
642                       WHERE tax_rate_code = p_value
643 		      AND nvl(active_flag, 'Y') = 'Y'	/* 4400063 */
644                       AND  l_date between
645                          nvl(effective_from, l_date)
646                      and nvl(effective_to, l_date);
647 
648                 ELSIF (p_entity = 'LEGAL_ENTITY' ) THEN
649                     --
650                     SELECT ba.account_owner_org_id
651 		    INTO   l_selected_id
652 		    FROM   ce_bank_accounts ba, ce_bank_acct_uses bau
653                     WHERE  ba.bank_account_id = bau.bank_account_id
654 		    AND    bau.bank_acct_use_id = p_value;
655                 END IF;
656 
657 
658                 Add_Value_To_Cache(
659                                     p_entity,
660                                     p_value,
661                                     l_selected_id,
662                                     NVL( l_index, 0 ) + 1
663                                   );
664 
665                 IF PG_DEBUG in ('Y', 'C') THEN
666                    arp_util.debug('Get_Id: ' || 'Value selected. Entity: '||
667                                                     p_entity || ',' ||
668                                      '  Value: ' || p_value  || ',' ||
669                                      'ID: ' || l_selected_id);
670                    arp_util.debug('Get_Id()- ');
671                 END IF;
672 
673                 RETURN( l_selected_id );
674 
675 
676            END IF;  -- end value not found in cache case
677 
678       END IF;  -- end p_value is not null case
679 
680 
681       IF PG_DEBUG in ('Y', 'C') THEN
682          arp_util.debug('Get_Id()- ');
683       END IF;
684 
685 
686 EXCEPTION
687 
688    WHEN NO_DATA_FOUND THEN
689         IF PG_DEBUG in ('Y', 'C') THEN
690            arp_util.debug('Get_Id: ' || 'Value not found. Entity: ' ||
691                                    p_entity ||'  Value: ' || p_value);
692         END IF;
693         return(null);
694         IF PG_DEBUG in ('Y', 'C') THEN
695            arp_util.debug('Get_Id()- ');
696         END IF;
697 
698    WHEN OTHERS THEN
699         IF PG_DEBUG in ('Y', 'C') THEN
700            arp_util.debug('Get_Id: ' || 'Value not found. Entity: ' ||
701                                    p_entity ||'  Value: ' || p_value);
702         END IF;
703         RAISE;
704 
705 END Get_Id;
706 
707 
708 /* modified for tca uptake */
709 PROCEDURE Add_Site_Use_To_Cache(
710                      p_customer_id   IN hz_cust_acct_sites.cust_account_id%TYPE,
711                      p_location      IN hz_cust_site_uses.location%TYPE,
712                      p_site_use_code IN hz_cust_site_uses.site_use_code%TYPE,
713                      p_id            IN hz_cust_site_uses.site_use_id%TYPE,
714                      p_index         IN BINARY_INTEGER
715                      ) IS
716 BEGIN
717 
718    IF PG_DEBUG in ('Y', 'C') THEN
719       arp_util.debug('Add_Site_Use_To_Cache()+ ');
720    END IF;
721    IF   ( p_index <= G_SITE_USE_CACHE_SIZE )
722    THEN
723                   G_Site_Use_Cache_Tbl(p_index).customer_id :=
724                                                         p_customer_id;
725                   G_Site_Use_Cache_Tbl(p_index).location    :=
726                                                         p_location;
727                   G_Site_Use_Cache_Tbl(p_index).site_use_code :=
728                                                         p_site_use_code;
729                   G_Site_Use_Cache_Tbl(p_index).id       := p_id;
730    END IF;
731    IF PG_DEBUG in ('Y', 'C') THEN
732       arp_util.debug('Add_Site_Use_To_Cache()- ');
733    END IF;
734 EXCEPTION
735    WHEN OTHERS THEN
736         IF PG_DEBUG in ('Y', 'C') THEN
737            arp_util.debug('EXCEPTION: Add_Site_Use_To_Cache');
738         END IF;
739         RAISE;
740 END Add_Site_Use_To_Cache;
741 
742 /* modified for tca uptake */
743 FUNCTION Find_Site_Use_In_Cache(
744                      p_customer_id   IN hz_cust_accounts.cust_account_id%TYPE,
745                      p_location      IN hz_cust_site_uses.site_use_id%TYPE,
746                      p_site_use_code IN hz_cust_site_uses.site_use_code%TYPE,
747                      p_index         OUT NOCOPY BINARY_INTEGER
748                    ) RETURN hz_cust_site_uses.site_use_id%TYPE IS
749 
750    l_index  BINARY_INTEGER;
751    l_count  BINARY_INTEGER;
752 
753 BEGIN
754 
755    IF PG_DEBUG in ('Y', 'C') THEN
756       arp_util.debug('Find_Site_Use_In_Cache()+ ');
757    END IF;
758 
759    l_count := G_Site_Use_Cache_Tbl.count;
760 
761    FOR l_index IN 1..l_count LOOP
762 
763        p_index := l_index;
764      IF p_location IS NOT NULL THEN
765 
766        IF  ( G_Site_Use_Cache_Tbl(l_index).customer_id = p_customer_id
767           AND G_Site_Use_Cache_Tbl(l_index).location  = p_location
768           AND G_Site_Use_Cache_Tbl(l_index).site_use_code = p_site_use_code)
769           THEN
770            RETURN( G_Site_Use_Cache_Tbl(l_index).id);
771 
772        END IF;
773      ELSE
774        IF ( G_Site_Use_Cache_Tbl(l_index).customer_id = p_customer_id
775            AND G_Site_Use_Cache_Tbl(l_index).site_use_code = p_site_use_code
776            AND G_Site_Use_Cache_Tbl(l_index).primary_flag = 'Y')
777          THEN
778            RETURN( G_Site_Use_Cache_Tbl(l_index).id);
779        END IF;
780      END IF;
781 
782    END LOOP;
783 
784    IF PG_DEBUG in ('Y', 'C') THEN
785       arp_util.debug('Find_Site_Use_In_Cache()- ');
786    END IF;
787 
788    RETURN(NULL);
789 
790 EXCEPTION
791 
792    WHEN OTHERS THEN
793         IF PG_DEBUG in ('Y', 'C') THEN
794            arp_util.debug('EXCEPTION: Find_Site_Use_In_Cache');
795         END IF;
796 
797         RAISE;
798 
799 END Find_Site_Use_In_Cache;
800 
801 /* modified for tca uptake */
802 FUNCTION Get_Site_Use_Id(
803                  p_customer_id IN hz_cust_acct_sites.cust_account_id%TYPE,
804                  p_location  IN hz_cust_site_uses.location%TYPE,
805                  p_site_use_code1 IN hz_cust_site_uses.site_use_code%TYPE DEFAULT NULL,
806                  p_site_use_code2 IN hz_cust_site_uses.site_use_code%TYPE DEFAULT  NULL)
807      RETURN hz_cust_site_uses.site_use_id%type IS
808 l_cached_id    hz_cust_site_uses.site_use_id%type;
809 l_selected_id  hz_cust_site_uses.site_use_id%type;
810 l_index        BINARY_INTEGER;
811 BEGIN
812 
813       IF PG_DEBUG in ('Y', 'C') THEN
814          arp_util.debug('Get_Site_Use_Id()+ ');
815       END IF;
816 
817  /*     IF  (  p_customer_id  IS NULL ) THEN
818            RETURN(NULL);
819       ELSE
820            l_cached_id := Find_Site_Use_In_Cache(
821                                             p_customer_id,
822                                             p_location,
823                                             p_site_use_code,
824                                             l_index
825                                               );
826            IF ( l_cached_id  IS NOT NULL ) THEN
827               RETURN( l_cached_id );
828            ELSE
829    */
830           IF p_customer_id IS NOT NULL THEN
831             IF (p_location IS NOT NULL) THEN
832               BEGIN
833                /* modified for tca uptake */
834                SELECT site_use.site_use_id
835                INTO   l_selected_id
836                FROM   hz_cust_site_uses site_use,
837                       hz_cust_acct_sites acct_site
838                WHERE  acct_site.cust_account_id   =  p_customer_id
839                  AND  acct_site.status        = 'A'
840                  AND  site_use.cust_acct_site_id = acct_site.cust_acct_site_id
841                  AND  (site_use.site_use_code = nvl(p_site_use_code1,
842                                                     site_use.site_use_code) OR
843                        site_use.site_use_code = nvl(p_site_use_code1,
844                                                     site_use.site_use_code))
845                  AND  site_use.status        = 'A'
846                  AND  site_use.location = p_location;
847               EXCEPTION
848                WHEN no_data_found THEN
849                   IF PG_DEBUG in ('Y', 'C') THEN
850                      arp_util.debug('Get_Site_Use_Id: ' || 'No data found in the hz_cust_site_uses for the location :'||p_location);
851                   END IF;
852                   --the error message will be raised in the validation routine.
853                   null;
854               END;
855 
856             ELSE
857              --the case when no location  is specified for the customer.
858              --here we are defaulting the primary bill_to loaction.
859               BEGIN
860                /* modified for tca uptake */
861                SELECT site_use.site_use_id
862                INTO   l_selected_id
863                FROM   hz_cust_site_uses site_use,
864                       hz_cust_acct_sites acct_site
865                WHERE  acct_site.cust_account_id   =  p_customer_id
866                  AND  acct_site.status        = 'A'
867                  AND  site_use.cust_acct_site_id  = acct_site.cust_acct_site_id
868                  AND  (site_use.site_use_code = nvl(p_site_use_code1,
869                                                     site_use.site_use_code) OR
870                        site_use.site_use_code = nvl(p_site_use_code1,
871                                                     site_use.site_use_code))
872                  AND  site_use.status        = 'A'
873                  AND  site_use.primary_flag  = 'Y';
874 
875               EXCEPTION
876                WHEN no_data_found THEN
877                 IF PG_DEBUG in ('Y', 'C') THEN
878                    arp_util.debug('Get_Site_Use_Id: ' || 'No_data_found : Site use id could not be defaulted for customer_id '||to_char(p_customer_id));
879                 END IF;
880                 --This is the case where customer site use id is null
881                 --neither it was supplied by the user nor it could be defaulted
882                 --a WARNING message raised in the validation routine to indicate
883                 --that the customer site use id could not be defaulted.
884                 null;
885               END;
886 
887            END IF;
888         END IF;
889 /*           Add_Site_Use_To_Cache(
890                                  p_customer_id,
891                                  p_location,
892                                  p_site_use_code,
893                                  l_selected_id,
894                                  NVL( l_index, 0 ) + 1
895                                    );
896                 RETURN( l_selected_id );
897 
898       END IF;
899  END IF;
900  */
901  RETURN( l_selected_id );
902 EXCEPTION
903  WHEN others THEN
904   IF PG_DEBUG in ('Y', 'C') THEN
905      arp_util.debug('EXCEPTION: Get_Site_Use_Id.');
906   END IF;
907   raise;
908 
909 END Get_Site_Use_Id;
910 
911 FUNCTION Get_Cross_Validated_Id( p_entity        IN VARCHAR2,
912                                  p_number_value  IN VARCHAR2,
913                                  p_name_value    IN VARCHAR2,
914                                  p_return_status OUT NOCOPY VARCHAR2
915                                 ) RETURN VARCHAR2 IS
916 l_id_from_name  VARCHAR2(100);
917 l_id_from_num   VARCHAR2(100);
918 BEGIN
919 
920    IF (p_number_value IS NULL) OR
921       (p_name_value IS NULL)
922     THEN
923     RETURN(NULL);
924    END IF;
925 
926    p_return_status := FND_API.G_RET_STS_SUCCESS;
927 
928    l_id_from_name := Get_Id(p_entity||'_NAME',
929                             p_name_value,
930                             p_return_status
931                            );
932 
933    l_id_from_num  := Get_Id(p_entity||'_NUMBER',
934                             p_number_value,
935                             p_return_status
936                            );
937 
938    IF l_id_from_name = l_id_from_num THEN
939      RETURN(l_id_from_name);
940    ELSE
941      --p_return_status := FND_API.G_RET_STS_ERROR;
942      --FND_MESSAGE.SET_NAME('AR', 'INVALID_VALUE_ERR');
943      --FND_MESSAGE.SET_TOKEN('PARAMETER','Customer Number/Name');
944      --FND_MSG_PUB.Add;
945      RETURN(NULL);
946    END IF;
947 
948 EXCEPTION
949  WHEN others THEN
950   IF PG_DEBUG in ('Y', 'C') THEN
951      arp_util.debug('EXCEPTION: Get_Cross_Validated_Id() '||p_entity);
952   END IF;
953   raise;
954 END Get_Cross_Validated_Id;
955 
956 PROCEDURE Add_Method_Info_To_Cache( p_method_info_record  IN OUT NOCOPY  Method_Info_Cache_Rec_Type,
957                                     p_index BINARY_INTEGER ) IS
958 
959 l_index BINARY_INTEGER;
960 BEGIN
961 
962    IF PG_DEBUG in ('Y', 'C') THEN
963       arp_util.debug('Add_Method_Info_To_Cache()+ ');
964    END IF;
965    IF p_index  IS NULL THEN
966        l_index := nvl(Method_Info_Cache_Tbl.LAST,0) + 1;
967    END IF;
968 
969    IF   ( l_index <= G_METHOD_INFO_CACHE_SIZE ) THEN
970 
971        Method_Info_Cache_Tbl(l_index) := p_method_info_record;
972 
973        IF PG_DEBUG in ('Y', 'C') THEN
974 	  arp_util.debug('Added the record at index '|| l_index);
975        END IF;
976    END IF;
977 
978    IF PG_DEBUG in ('Y', 'C') THEN
979       arp_util.debug('Add_Method_Info_To_Cache()- ');
980    END IF;
981 
982 EXCEPTION
983 
984    WHEN OTHERS THEN
985         IF PG_DEBUG in ('Y', 'C') THEN
986            arp_util.debug('EXCEPTION: Add_Method_Info_To_Cache');
987         END IF;
988         RAISE;
989 END Add_Method_Info_To_Cache;
990 
991 
992 PROCEDURE Get_Method_Info_From_Cache(
993                       p_method_info_record         IN OUT NOCOPY Method_Info_Cache_Rec_Type,
994                       p_receipt_date               IN ar_cash_receipts.receipt_date%TYPE ) IS
995 l_count  BINARY_INTEGER;
996 l_index_curr  BINARY_INTEGER;
997 
998 BEGIN
999     IF PG_DEBUG in ('Y', 'C') THEN
1000        arp_util.debug('Get_Method_Info_From_Cache ()+ ');
1001     END IF;
1002 
1003          l_count := Method_Info_Cache_Tbl.count;
1004 
1005     IF l_count IS NOT NULL THEN
1006 	--If The Record Exists In Cache,Then Set The Values To Out Params And
1007 	-- Return From The Procedure
1008 	For L_index In 1..L_count Loop
1009 
1010 	   If  ( Method_info_cache_tbl(L_index).Receipt_method_id = P_method_info_record.Receipt_method_id And
1011 		 Method_info_cache_tbl(L_index).Currency_code = Nvl(P_method_info_record.Currency_code,'0') And
1012 		 Method_info_cache_tbl(L_index).org_id = P_method_info_record.org_id And
1013 		 Method_info_cache_tbl(L_index).Called_from = P_method_info_record.Called_from  And
1014 		 Nvl(Method_info_cache_tbl(L_index).Bau_end_date,P_receipt_date +1) > P_receipt_date And
1015 		 ( P_receipt_date Between Method_info_cache_tbl(L_index).Rm_start_date And
1016 		   Nvl( Method_info_cache_tbl(L_index).Rm_end_date, P_receipt_date)) And
1017 		 ( P_receipt_date Between  Method_info_cache_tbl(L_index).Rma_start_date And
1018 		   Nvl(Method_info_cache_tbl(L_index).Rma_end_date, P_receipt_date ) ) )  Then
1019 	       P_method_info_record := Method_info_cache_tbl(L_index);
1020 
1021 	       If Pg_debug In ('y', 'c') Then
1022 		  Arp_util.Debug('found The Record In Cache ');
1023 	       End If;
1024 	       Exit;
1025 	   End If;
1026 	End Loop;
1027     END IF;
1028 
1029      IF PG_DEBUG in ('Y', 'C') THEN
1030         arp_util.debug('Get_Method_Info_From_Cache ()- ');
1031      END IF;
1032 
1033 EXCEPTION
1034  WHEN others THEN
1035   IF PG_DEBUG in ('Y', 'C') THEN
1036      arp_util.debug('EXCEPTION: Get_Method_Info_From_Cache() ');
1037   END IF;
1038   raise;
1039 END Get_Method_Info_From_Cache;
1040 
1041 
1042 PROCEDURE Add_Exchange_Rate_To_Cache(
1043                      p_currency_code      IN ar_cash_receipts.currency_code%TYPE,
1044                      p_exchange_rate_date IN ar_cash_receipts.exchange_date%TYPE,
1045                      p_exchange_rate_type IN ar_cash_receipts.exchange_rate_type%TYPE,
1046                      p_exchange_rate      IN ar_cash_receipts.exchange_rate%TYPE,
1047                      p_index              IN BINARY_INTEGER
1048                      ) IS
1049 BEGIN
1050 
1051    IF PG_DEBUG in ('Y', 'C') THEN
1052       arp_util.debug('Add_Exchange_Rate_To_Cache()+ ');
1053    END IF;
1054 
1055 
1056    IF   ( p_index <= G_EXCHANGE_RATE_CACHE_SIZE )
1057    THEN
1058                   Exchange_Rate_Cache_Tbl(p_index).exchange_rate :=
1059                                                         p_exchange_rate;
1060                   Exchange_Rate_Cache_Tbl(p_index).currency    :=
1061                                                         p_currency_code;
1062                   Exchange_Rate_Cache_Tbl(p_index).exchange_rate_type :=
1063                                                         p_exchange_rate_type;
1064                   Exchange_Rate_Cache_Tbl(p_index).exchange_date :=
1065                                                         p_exchange_rate_date;
1066    END IF;
1067 
1068    IF PG_DEBUG in ('Y', 'C') THEN
1069       arp_util.debug('Add_Exchange_Rate_To_Cache()- ');
1070    END IF;
1071 
1072 EXCEPTION
1073 
1074    WHEN OTHERS THEN
1075         IF PG_DEBUG in ('Y', 'C') THEN
1076            arp_util.debug('EXCEPTION: Add_Exchange_Rate_To_Cache');
1077         END IF;
1078 
1079         RAISE;
1080 
1081 END Add_Exchange_Rate_To_Cache;
1082 
1083 PROCEDURE Default_gl_date(p_entered_date IN  DATE,
1084                           p_gl_date      OUT NOCOPY DATE,
1085                           p_validation_date IN DATE, /* Bug fix 3547720 */
1086                           p_return_status OUT NOCOPY VARCHAR2) IS
1087 l_error_message        VARCHAR2(128);
1088 l_defaulting_rule_used VARCHAR2(100);
1089 l_default_gl_date      DATE;
1090 BEGIN
1091   p_return_status := FND_API.G_RET_STS_SUCCESS;
1092   IF PG_DEBUG in ('Y', 'C') THEN
1093      arp_util.debug('Default_gl_date ()+');
1094   END IF;
1095     IF p_gl_date IS NULL THEN
1096      IF (arp_util.validate_and_default_gl_date(
1097                 p_entered_date,
1098                 NULL,
1099                 p_validation_date, /* Bug fix 3547720 */
1100                 NULL,
1101                 NULL,
1102                 p_entered_date,
1103                 NULL,
1104                 NULL,
1105                 'N',
1106                 NULL,
1107                 arp_global.set_of_books_id,
1108                 222,
1109                 l_default_gl_date,
1110                 l_defaulting_rule_used,
1111                 l_error_message) = TRUE)
1112      THEN
1113         p_gl_date := l_default_gl_date;
1114       IF PG_DEBUG in ('Y', 'C') THEN
1115          arp_util.debug('Default_gl_date: ' || 'Defaulted GL Date : '||to_char(p_gl_date,'DD-MON-YYYY'));
1116       END IF;
1117      ELSE
1118       IF PG_DEBUG in ('Y', 'C') THEN
1119          arp_util.debug('Default_gl_date: ' || 'GL Date could not be defaulted ');
1120       END IF;
1121       -- Raise error message if failure in defaulting the gl_date
1122       FND_MESSAGE.SET_NAME('AR', 'GENERIC_MESSAGE');
1123       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT', l_error_message);
1124       FND_MSG_PUB.Add;
1125       p_return_status := FND_API.G_RET_STS_ERROR;
1126      END IF;
1127    END IF;
1128   IF PG_DEBUG in ('Y', 'C') THEN
1129      arp_util.debug('Default_gl_date ()-');
1130   END IF;
1131 END default_gl_date;
1132 
1133 
1134 FUNCTION Find_Cached_Exchange_Rate(
1135                        p_currency_code  IN ar_cash_receipts.currency_code%TYPE,
1136                        p_exchange_rate_date IN ar_cash_receipts.exchange_date%TYPE,
1137                        p_exchange_rate_type IN ar_cash_receipts.exchange_rate_type%TYPE
1138                       ) RETURN NUMBER IS
1139 l_count  BINARY_INTEGER;
1140 l_index_curr  BINARY_INTEGER;
1141 l_exchange_rate        NUMBER;
1142 l_set_of_books_id      NUMBER := arp_global.set_of_books_id;
1143 l_functional_currency  VARCHAR2(100) := arp_global.functional_currency;
1144 BEGIN
1145  IF PG_DEBUG in ('Y', 'C') THEN
1146     arp_util.debug('Find_Cached_Exchange_Rate ()+');
1147  END IF;
1148    IF (p_currency_code IS NOT NULL) AND
1149       (p_currency_code <> l_functional_currency) AND
1150       (p_exchange_rate_date IS NOT NULL) AND
1151       (p_exchange_rate_type IS NOT NULL) AND
1152       (p_exchange_rate_type <>'User')
1153     THEN
1154 --  This section of code is commented out NOCOPY as the implementation of the
1155 --  of the caching mechanism has been deferred as of now
1156 /*
1157       l_count := Exchange_Rate_Cache_Tbl.count;
1158 
1159       FOR l_index IN 1..l_count LOOP
1160 
1161        l_index_curr := l_index;
1162 
1163      IF (Exchange_Rate_Cache_Tbl(l_index).currency = p_currency_code AND
1164          Exchange_Rate_Cache_Tbl(l_index).exchange_rate_type = p_exchange_rate_type AND
1165          Exchange_Rate_Cache_Tbl(l_index).exchange_date = p_exchange_rate_date)
1166       THEN
1167          RETURN(Exchange_Rate_Cache_Tbl(l_index).exchange_rate);
1168      END IF;
1169 
1170      END LOOP;
1171 
1172      --the rate is not in the cache so get it from the database.
1173 */
1174      l_exchange_rate := gl_currency_api.get_rate(
1175                                            l_set_of_books_id,
1176                                            p_currency_code,
1177                                            p_exchange_rate_date,
1178                                            p_exchange_rate_type
1179                                            );
1180 /*
1181           Add_Exchange_Rate_To_Cache(
1182                                     p_currency_code,
1183                                      p_exchange_rate_date,
1184                                      p_exchange_rate_type,
1185                                      l_exchange_rate,
1186                                      NVL( l_index_curr, 0 ) + 1
1187                                      );
1188 */
1189   END IF;
1190   RETURN( l_exchange_rate );
1191  IF PG_DEBUG in ('Y', 'C') THEN
1192     arp_util.debug('Find_Cached_Exchange_Rate ()-');
1193  END IF;
1194 EXCEPTION
1195  WHEN gl_currency_api.NO_RATE THEN
1196   --rate does not exist set appropriate message.
1197   --p_return_status := FND_API.G_RET_STS_ERROR ;
1198   return(null);
1199   IF PG_DEBUG in ('Y', 'C') THEN
1200      arp_util.debug('Find_Cached_Exchange_Rate: ' || 'Exception : gl_currency_api.NO_RATE ');
1201   END IF;
1202  WHEN gl_currency_api.INVALID_CURRENCY  THEN
1203   -- invalid currency set appropriate message.
1204   --p_return_status := FND_API.G_RET_STS_ERROR ;
1205   return(null);
1206   IF PG_DEBUG in ('Y', 'C') THEN
1207      arp_util.debug('Find_Cached_Exchange_Rate: ' || 'Exception: gl_currency_api.INVALID_CURRENCY ');
1208   END IF;
1209  WHEN others THEN
1210   IF PG_DEBUG in ('Y', 'C') THEN
1211      arp_util.debug('EXCEPTION: Find_Cached_Exchange_Rate() ');
1212   END IF;
1213   raise;
1214 END Find_Cached_Exchange_Rate;
1215 
1216 PROCEDURE Default_Receipt_Method_Info(
1217            p_receipt_method_id          IN ar_cash_receipts.receipt_method_id%TYPE,
1218            p_currency_code              IN ar_cash_receipts.currency_code%TYPE,
1219            p_receipt_date               IN ar_cash_receipts.receipt_date%TYPE,
1220            p_remittance_bank_account_id IN OUT NOCOPY ar_receipt_method_accounts_all.remit_bank_acct_use_id%TYPE,
1221            p_state                      OUT NOCOPY ar_receipt_classes.creation_status%TYPE,
1222            p_creation_method_code       OUT NOCOPY ar_receipt_classes.creation_method_code%TYPE,
1223            p_called_from                IN VARCHAR2,
1224            p_return_status              OUT NOCOPY VARCHAR2
1225            ) IS
1226 /*Bug 3518573 changing the logic to pick default remittance bank account */
1227   CURSOR get_remit_bank_acct_id(
1228     p_currency_code ar_cash_receipts.currency_code%TYPE,
1229     p_receipt_date ar_cash_receipts.receipt_date%TYPE,
1230     p_called_from VARCHAR2,
1231     p_receipt_method_id ar_cash_receipts.receipt_method_id%TYPE)  is
1232   SELECT   ba.bank_acct_use_id,
1233            rc.creation_status,
1234            rc.creation_method_code,
1235 	   rma.start_date rma_start_date,
1236 	   rma.end_date  rma_end_date,
1237 	   rm.start_date rm_start_date,
1238 	   rm.end_date rm_end_date,
1239 	   ba.end_Date bau_end_date,
1240 	   rma.org_id  rma_org_id
1241     FROM   ar_receipt_methods rm,
1242            ce_bank_accounts   cba,
1243            ce_bank_acct_uses  ba,
1244            ar_receipt_method_accounts rma ,
1245            ar_receipt_classes rc
1246     WHERE  rm.receipt_method_id = p_receipt_method_id
1247       and  (p_receipt_date
1248                 between
1249                 rm.start_date and
1250                 nvl(rm.end_date, p_receipt_date))
1251       and  ((rc.creation_method_code = DECODE(p_called_from,'BR_REMITTED','BR_REMIT',
1252                                               'BR_FACTORED_WITH_RECOURSE','BR_REMIT',
1253                                               'BR_FACTORED_WITHOUT_RECOURSE','BR_REMIT','@*%?&')) or
1254             (rc.creation_method_code = 'MANUAL') or
1255             (rc.creation_method_code = 'AUTOMATIC'
1256              --and rc.remit_flag = 'Y'
1257              -- OSTEINME 2/27/2001: removed remit_flag
1258              -- condition for iReceivables CC functionality.
1259              -- See bug 1659109.
1260              -- bichatte autorecapi.
1261             and   rc.confirm_flag = decode(p_called_from, 'AUTORECAPI',rc.confirm_flag,'N')))
1262       and  cba.account_classification = 'INTERNAL'
1263       and  nvl(ba.end_date, p_receipt_date +1) > p_receipt_date
1264       and  p_receipt_date
1265                  between
1266                  rma.start_date and
1267                  nvl(rma.end_date, p_receipt_date)
1268       and  cba.currency_code = decode(cba.receipt_multi_currency_flag, 'Y',
1269                                       cba.currency_code, p_currency_code)
1270       and  rc.receipt_class_id = rm.receipt_class_id
1271       and  rm.receipt_method_id = rma.receipt_method_id
1272       and  rma.remit_bank_acct_use_id = ba.bank_acct_use_id
1273       and  ba.bank_account_id = cba.bank_account_id
1274      --APANDIT: changes made for the misc receipt creation api.
1275       and  ((nvl(p_called_from,'*&#$') <> 'MISC')
1276                or
1277                  (rm.receipt_class_id not in (
1278                  select arc.receipt_class_id
1279                  from ar_receipt_classes arc
1280                  where arc.notes_receivable='Y'
1281                     or arc.bill_of_exchange_flag='Y')))
1282       order by
1283       decode(rma.primary_flag,'Y',1,'N',2,3),
1284       decode(cba.currency_code,p_currency_code,1,2),
1285       cba.bank_branch_id,
1286       cba.bank_account_name,
1287       ba.bank_acct_use_id;
1288 
1289       l_method_info_record Method_Info_Cache_Rec_Type;
1290 
1291 BEGIN
1292   IF PG_DEBUG in ('Y', 'C') THEN
1293      arp_util.debug('Default_Receipt_Method_Info ()+');
1294   END IF;
1295 
1296   p_return_status := FND_API.G_RET_STS_SUCCESS;
1297 
1298  IF p_receipt_method_id IS NOT NULL THEN
1299 
1300 	IF p_remittance_bank_account_id IS NULL  THEN
1301 
1302 	    l_method_info_record.receipt_method_id  := p_receipt_method_id;
1303 	    l_method_info_record.currency_code      := p_currency_code;
1304 	    l_method_info_record.called_from        := nvl(p_called_from,'@*%?&');
1305 	    l_method_info_record.org_id             := arp_standard.sysparm.org_id;
1306 
1307 	    --check if it exists in cache
1308 	    Get_Method_Info_From_Cache( l_method_info_record,
1309 					p_receipt_date );
1310 
1311 	    IF  l_method_info_record.remit_bank_acct_use_id IS NULL THEN
1312 		OPEN get_remit_bank_acct_id(p_currency_code,p_receipt_date,p_called_from,p_receipt_method_id);
1313 
1314 		FETCH get_remit_bank_acct_id INTO
1315 		    l_method_info_record.remit_bank_acct_use_id,
1316 		    l_method_info_record.state,
1317 		    l_method_info_record.creation_method_code,
1318 		    l_method_info_record.rma_start_date,
1319 		    l_method_info_record.rma_end_date,
1320 		    l_method_info_record.rm_start_date,
1321 		    l_method_info_record.rm_end_date,
1322 		    l_method_info_record.bau_end_date,
1323 		    l_method_info_record.org_id;
1324 
1325 		IF get_remit_bank_acct_id%NOTFOUND then
1326 		    null;
1327 		END IF;
1328 		CLOSE get_remit_bank_acct_id;
1329 
1330 		--add it to cache
1331 	        Add_Method_Info_To_Cache( l_method_info_record,null);
1332             END IF;
1333 
1334 	    p_remittance_bank_account_id := l_method_info_record.remit_bank_acct_use_id;
1335 	    p_state                      := l_method_info_record.state;
1336 	    p_creation_method_code       := l_method_info_record.creation_method_code;
1337 	ELSE
1338 	    --user has specified the remittance bank account id
1339 	    --so get the creation_status only.
1340 
1341 	    SELECT rc.creation_status,
1342 		   rc.creation_method_code
1343 	    INTO   p_state,
1344 		p_creation_method_code
1345 	    FROM   ar_receipt_classes rc,
1346 		   ar_receipt_methods rm
1347 	    WHERE  rc.receipt_class_id = rm.receipt_class_id
1348 	    AND  rm.receipt_method_id = p_receipt_method_id;
1349 
1350 	    --the invalid error message will be raised in the
1351 	    --validation phase. We will check in the validation
1352 	    --phase that if p_state is null and method_id is not
1353 	    --null then method_id is invalid
1354 	END IF;
1355     END IF;
1356 
1357   IF PG_DEBUG in ('Y', 'C') THEN
1358      arp_util.debug('Default_Receipt_Method_Info ()-');
1359   END IF;
1360 
1361 EXCEPTION
1362  WHEN no_data_found  THEN
1363   --In the validation phase we will raise the relevant error
1364   --if either the p_state or remittance_bank_id is null
1365   null;
1366  WHEN too_many_rows  THEN
1367   --This will happen only if the receipt method was specified
1368   --and we tried to default the remittance bank and the state
1369   p_return_status := FND_API.G_RET_STS_ERROR;
1370   FND_MESSAGE.SET_NAME('AR','AR_RAPI_REM_BK_AC_ID_NULL');
1371   FND_MSG_PUB.Add;
1372   IF PG_DEBUG in ('Y', 'C') THEN
1373      arp_util.debug('Default_Receipt_Method_Info: ' || 'The remittance bank account could not be defaulted ');
1374   END IF;
1375  WHEN others THEN
1376   IF PG_DEBUG in ('Y', 'C') THEN
1377      arp_util.debug('EXCEPTION: Default_Receipt_Method_info() ');
1378      arp_util.debug('Default_Receipt_Method_Info: ' || 'p_receipt_method_id  =  '
1379                       ||TO_CHAR(p_receipt_method_id));
1380   END IF;
1381   raise;
1382 
1383 END Default_Receipt_Method_Info;
1384 
1385 /* Bug3315058 */
1386 PROCEDURE validate_cc_segments(
1387            p_code_combination_id        IN NUMBER,
1388            p_gl_date                    IN DATE,
1389            p_message                    IN VARCHAR2,
1390            p_return_status              OUT NOCOPY VARCHAR2
1391            ) IS
1392  l_concat_segs varchar2(2000);
1393  l_value boolean := FALSE;
1394  l_chart_of_accounts_id gl_sets_of_books.chart_of_accounts_id%type;
1395  l_validation_date date ;
1396 BEGIN
1397     IF PG_DEBUG in ('Y', 'C') THEN
1398       arp_util.debug('validate_cc_segments ()+');
1399     END IF;
1400     p_return_status := FND_API.G_RET_STS_SUCCESS;
1401     l_validation_date := p_gl_date;
1402     l_chart_of_accounts_id := arp_global.chart_of_accounts_id;
1403     IF PG_DEBUG in ('Y', 'C') THEN
1404        arp_util.debug('l_chart_of_accounts_id = '||to_char(l_chart_of_accounts_id));
1405     END IF;
1406     l_concat_segs := fnd_flex_ext.get_segs('SQLGL','GL#',
1407                                            l_chart_of_accounts_id,
1408                                            p_code_combination_id);
1409     IF PG_DEBUG in ('Y', 'C') THEN
1410        arp_util.debug('l_concat_segs = '||l_concat_segs);
1411     END IF;
1412     l_value := FND_FLEX_KEYVAL.validate_segs('CHECK_SEGMENTS','SQLGL','GL#',
1413                                               l_chart_of_accounts_id,
1414                                               l_concat_segs,
1415                                                   'V',l_validation_date);
1416     IF l_value THEN
1417        IF PG_DEBUG in ('Y', 'C') THEN
1418           arp_util.debug('Returning true');
1419        END IF;
1420     ELSE
1421        IF PG_DEBUG in ('Y', 'C') THEN
1422           arp_util.debug('Returning False');
1423        END IF;
1424     END IF;
1425     IF (NOT l_value)  THEN
1426        FND_MESSAGE.SET_NAME('AR',p_message);
1427        FND_MSG_PUB.Add;
1428        p_return_status := FND_API.G_RET_STS_ERROR;
1429     END IF;
1430 END validate_cc_segments;
1431 
1432 /* Bug fix 3315058
1433    Procedure which checks the validity of the different accounts of a payment method */
1434 PROCEDURE Validate_Receipt_Method_ccid(
1435                p_receipt_method_id            IN  ar_cash_receipts.receipt_method_id%TYPE ,
1436                p_remittance_bank_account_id   IN  ar_receipt_method_accounts_all.remit_bank_acct_use_id%TYPE,
1437                p_gl_date                      IN  DATE,
1438                p_return_status                OUT NOCOPY VARCHAR2
1439                )IS
1440    l_receipt_method_id ar_receipt_methods.receipt_method_id%type;
1441    l_remit_bank_account_id ar_receipt_method_accounts.remit_bank_acct_use_id%type;
1442    l_cash_ccid gl_code_combinations.code_combination_id%type;
1443    l_earned_ccid gl_code_combinations.code_combination_id%type;
1444    l_on_account_ccid gl_code_combinations.code_combination_id%type;
1445    l_unapplied_ccid gl_code_combinations.code_combination_id%type;
1446    l_unearned_ccid gl_code_combinations.code_combination_id%type;
1447    l_unidentified_ccid gl_code_combinations.code_combination_id%type;
1448    l_bank_charges_ccid gl_code_combinations.code_combination_id%type;
1449    l_factor_ccid gl_code_combinations.code_combination_id%type;
1450    l_remittance_ccid gl_code_combinations.code_combination_id%type;
1451    l_receipt_clearing_ccid gl_code_combinations.code_combination_id%type;
1452    l_short_term_debt_ccid gl_code_combinations.code_combination_id%type;
1453    l_message varchar2(100);
1454    l_gl_date DATE;
1455    l_cc_val_ret_status VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
1456 BEGIN
1457   IF PG_DEBUG in ('Y', 'C') THEN
1458      arp_util.debug('Validate_Receipt_Method_ccid ()+');
1459   END IF;
1460      p_return_status := FND_API.G_RET_STS_SUCCESS;
1461      l_gl_date := p_gl_date;
1462      IF p_receipt_method_id IS NOT NULL
1463         AND p_remittance_bank_account_id IS NOT NULL THEN
1464         SELECT rma.cash_ccid,art1.code_combination_id,rma.on_account_ccid,
1465             rma.unapplied_ccid,art2.code_combination_id,rma.unidentified_ccid,
1466             rma.bank_charges_ccid,rma.factor_ccid,rma.remittance_ccid,
1467             rma.receipt_clearing_ccid, rma.short_term_debt_ccid
1468       	INTO  l_cash_ccid,l_earned_ccid,l_on_account_ccid,
1469             l_unapplied_ccid,l_unearned_ccid,l_unidentified_ccid,
1470             l_bank_charges_ccid,l_factor_ccid,l_remittance_ccid,
1471             l_receipt_clearing_ccid,l_short_term_debt_ccid
1472       	FROM  ar_receipt_method_accounts rma,
1473             ar_receivables_trx art1, ar_receivables_trx art2
1474         WHERE remit_bank_acct_use_id = p_remittance_bank_account_id
1475        AND  receipt_method_id = p_receipt_method_id
1476        AND  art1.receivables_trx_id = rma.edisc_receivables_trx_id
1477        AND  art2.receivables_trx_id = rma.unedisc_receivables_trx_id;
1478      END IF;
1479 
1480            IF l_cash_ccid IS NOT NULL THEN
1481               l_message := 'AR_CASH_ACC_ASSGN';
1482               validate_cc_segments(l_cash_ccid,l_gl_date,
1483                  l_message,l_cc_val_ret_status);
1484               IF l_cc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN
1485                   p_return_status := FND_API.G_RET_STS_ERROR;
1486                   RETURN;
1487               END IF;
1488            END IF;
1489 
1490            IF l_earned_ccid IS NOT NULL THEN
1491               l_message := 'AR_EARN_DISC_ACC_ASSGN';
1492               validate_cc_segments(l_earned_ccid,l_gl_date,
1493                  l_message,l_cc_val_ret_status);
1494               IF l_cc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN
1495                  p_return_status := FND_API.G_RET_STS_ERROR;
1496                  RETURN;
1497               END IF;
1498            END IF;
1499 
1500            IF l_on_account_ccid IS NOT NULL THEN
1501               l_message := 'AR_ONACC_ACC_ASSGN';
1502               validate_cc_segments(l_on_account_ccid,l_gl_date,
1503                  l_message,l_cc_val_ret_status);
1504               IF l_cc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN
1505                  p_return_status := FND_API.G_RET_STS_ERROR;
1506                RETURN;
1507               END IF;
1508            END IF;
1509            IF l_unapplied_ccid IS NOT NULL THEN
1510               l_message := 'AR_UNAPP_ACC_ASSGN';
1511               validate_cc_segments(l_unapplied_ccid,l_gl_date,
1512                  l_message,l_cc_val_ret_status);
1513               IF l_cc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN
1514                  p_return_status := FND_API.G_RET_STS_ERROR;
1515                  RETURN;
1516               END IF;
1517            END IF;
1518 
1519            IF l_unearned_ccid IS NOT NULL THEN
1520               l_message := 'AR_UNEARN_DISC_ACC_ASSGN';
1521               validate_cc_segments(l_unearned_ccid,l_gl_date,
1522                  l_message,l_cc_val_ret_status);
1523               IF l_cc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN
1524                  p_return_status := FND_API.G_RET_STS_ERROR;
1525                  RETURN;
1526               END IF;
1527            END IF;
1528            IF l_unidentified_ccid IS NOT NULL THEN
1529               l_message := 'AR_UNIDEN_ACC_ASSGN';
1530               validate_cc_segments(l_unidentified_ccid,l_gl_date,
1531                  l_message,l_cc_val_ret_status);
1532               IF l_cc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN
1533                  p_return_status := FND_API.G_RET_STS_ERROR;
1534                  RETURN;
1535               END IF;
1536            END IF;
1537    	IF l_bank_charges_ccid IS NOT NULL THEN
1538               l_message := 'AR_BANK_CHRG_ACC_ASSGN';
1539               validate_cc_segments(l_bank_charges_ccid,l_gl_date,
1540                  l_message,l_cc_val_ret_status);
1541               IF l_cc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN
1542                  p_return_status := FND_API.G_RET_STS_ERROR;
1543                  RETURN;
1544               END IF;
1545            END IF;
1546 
1547            IF l_factor_ccid IS NOT NULL THEN
1548               l_message := 'AR_FCTR_ACC_ASSGN';
1549               validate_cc_segments(l_factor_ccid,l_gl_date,
1550                  l_message,l_cc_val_ret_status);
1551               IF l_cc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN
1552                  p_return_status := FND_API.G_RET_STS_ERROR;
1553                  RETURN;
1554               END IF;
1555            END IF;
1556 
1557            IF l_remittance_ccid IS NOT NULL THEN
1558               l_message := 'AR_REM_ACC_ASSGN';
1559               validate_cc_segments(l_remittance_ccid,l_gl_date,
1560                  l_message,l_cc_val_ret_status);
1561               IF l_cc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN
1562                  p_return_status := FND_API.G_RET_STS_ERROR;
1563                  RETURN;
1564               END IF;
1565            END IF;
1566            IF l_receipt_clearing_ccid IS NOT NULL THEN
1567               l_message := 'AR_CLRNCE_ACC_ASSGN';
1568               validate_cc_segments(l_receipt_clearing_ccid,l_gl_date,
1569                  l_message,l_cc_val_ret_status);
1570               IF l_cc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN
1571                  p_return_status := FND_API.G_RET_STS_ERROR;
1572                  RETURN;
1573               END IF;
1574   	   END IF;
1575 
1576            IF l_short_term_debt_ccid IS NOT NULL THEN
1577               l_message := 'AR_SHRT_TERM_DEBT_ACC_ASSGN';
1578               validate_cc_segments(l_short_term_debt_ccid,l_gl_date,
1579                  l_message,l_cc_val_ret_status);
1580               IF l_cc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN
1581                  p_return_status := FND_API.G_RET_STS_ERROR;
1582                  RETURN;
1583               END IF;
1584         END IF;
1585 END Validate_Receipt_Method_ccid;
1586  /* End bug fix 3315058 */
1587 
1588 FUNCTION Get_cross_rate (p_from_currency IN VARCHAR2,
1589                          p_to_currency  IN VARCHAR2,
1590                          p_exchange_rate_date IN DATE,
1591                          p_exchange_rate    IN NUMBER
1592                          ) RETURN NUMBER IS
1593 l_euro_to_emu_rate  NUMBER;
1594 l_fixed_rate  BOOLEAN;
1595 l_relationship  VARCHAR2(50);
1596 euro_code VARCHAR2(15);
1597 l_cross_rate  NUMBER;
1598 BEGIN
1599 
1600      gl_currency_api.get_relation(
1601                        p_from_currency,
1602                        p_to_currency,
1603                        trunc(p_exchange_rate_date),
1604                        l_fixed_rate,
1605                        l_relationship);
1606       euro_code := gl_currency_api.get_euro_code;
1607 
1608       IF (l_relationship = 'EMU-OTHER') THEN
1609                    l_euro_to_emu_rate :=
1610                                 gl_currency_api.get_rate(
1611                                                    euro_code,
1612                                                    p_from_currency,
1613                                                    trunc(p_exchange_rate_date),
1614                                                    NULL);
1615       ELSIF (l_relationship = 'OTHER-EMU') THEN
1616                    l_euro_to_emu_rate :=
1617                                 gl_currency_api.get_rate(
1618                                                    euro_code,
1619                                                    p_to_currency,
1620                                                    trunc(p_exchange_rate_date),
1621                                                    NULL);
1622       ELSE
1623           RAISE gl_euro_user_rate_api.INVALID_RELATION;
1624       END IF;
1625               l_cross_rate :=
1626                     gl_euro_user_rate_api.get_cross_rate(p_from_currency,
1627                                                 p_to_currency,
1628                                                 p_exchange_rate_date,
1629                                                 p_exchange_rate,
1630                                                 l_euro_to_emu_rate);
1631               return(l_cross_rate);
1632 EXCEPTION
1633   WHEN gl_euro_user_rate_api.INVALID_RELATION  THEN
1634     null;
1635   WHEN gl_euro_user_rate_api.INVALID_CURRENCY  THEN
1636     null;
1637   WHEN others THEN
1638     raise;
1639 END Get_cross_rate;
1640 
1641 PROCEDURE Default_Currency_info(
1642                    p_currency_code  IN OUT NOCOPY ar_cash_receipts.currency_code%TYPE,
1643                    p_receipt_date   IN OUT NOCOPY ar_cash_receipts.receipt_date%TYPE,
1644                    p_exchange_rate_date  IN OUT NOCOPY ar_cash_receipts.exchange_date%TYPE,
1645                    p_exchange_rate_type  IN OUT NOCOPY ar_cash_receipts.exchange_rate_type%TYPE,
1646                    p_exchange_rate  IN OUT NOCOPY ar_cash_receipts.exchange_rate%TYPE,
1647                    p_return_status  OUT NOCOPY VARCHAR2
1648                    ) IS
1649 
1650 l_euro_to_emu_rate      NUMBER;
1651 l_euro_to_other_prompt  VARCHAR2(30);
1652 l_euro_to_emu_prompt    VARCHAR2(30);
1653 l_emu_to_other_prompt   VARCHAR2(30);
1654 l_cross_rate            NUMBER;
1655 l_conversion_rate       NUMBER;
1656 BEGIN
1657   IF PG_DEBUG in ('Y', 'C') THEN
1658      arp_util.debug('Default_Currency_info ()+');
1659   END IF;
1660   p_return_status := FND_API.G_RET_STS_SUCCESS;
1661 
1662   IF p_currency_code <> arp_global.functional_currency THEN
1663 
1664     --default exchange rate date if null
1665     IF (p_exchange_rate_date IS NULL) THEN
1666       p_exchange_rate_date := p_receipt_date;
1667     END IF;
1668 
1669     --default exchange rate type if null
1670     IF p_exchange_rate_type IS NULL THEN
1671        p_exchange_rate_type := pg_profile_def_x_rate_type;
1672     END IF;
1673 
1674   IF p_exchange_rate_type IS NOT NULL THEN
1675 
1676     IF p_exchange_rate_type <> 'User' THEN
1677         --for any exchange_rate type other than 'User',
1678         --default exchange rate if not entered.
1679          IF p_exchange_rate IS NULL THEN
1680             p_exchange_rate := Find_Cached_Exchange_Rate(
1681                                                p_currency_code,
1682                                                p_exchange_rate_date,
1683                                                p_exchange_rate_type
1684                                                 );
1685          ELSE
1686            --if user has entered exchange rate for type <> User, raise error message
1687             p_return_status := FND_API.G_RET_STS_ERROR;
1688             FND_MESSAGE.SET_NAME('AR','AR_RAPI_X_RATE_INVALID');
1689             FND_MSG_PUB.Add;
1690          END IF;
1691     ELSE
1692      --case where rate_type is 'User'
1693 
1694       --if the user entered exchange rate is greater than 0 then
1695       --check for the case of EMU currency
1696    IF p_exchange_rate >0 THEN
1697 
1698      --This is the case rate_type is User and exchange_rate exists
1699 
1700      -- Returns 'Y' if the current conversion type is User AND
1701      -- they are converting from EMU -> OTHER or OTHER -> EMU AND
1702      -- they are not allowed to enter EMU -> OTHER and
1703      -- OTHER -> EMU rates directly
1704      -- Returns 'N' Otherwise
1705 
1706      IF (gl_euro_user_rate_api.is_cross_rate(p_currency_code,
1707                                        arp_global.functional_currency,
1708                                        p_exchange_rate_date,
1709                                        p_exchange_rate_type) = 'Y')
1710        THEN
1711 
1712               gl_euro_user_rate_api.get_prompts_and_rate(
1713                                                p_currency_code,
1714                                                arp_global.functional_currency,
1715                                                p_exchange_rate_date,
1716                                                l_euro_to_other_prompt,
1717                                                l_euro_to_emu_prompt,
1718                                                l_emu_to_other_prompt,
1719                                                l_euro_to_emu_rate);
1720 
1721               l_cross_rate :=
1722                gl_euro_user_rate_api.get_cross_rate(
1723                                                   p_currency_code,
1724                                                   p_currency_code,
1725                                                   p_exchange_rate_date,
1726                                                   p_exchange_rate,
1727                                                   l_euro_to_emu_rate);
1728 
1729         p_exchange_rate :=  l_cross_rate;
1730      ELSE
1731       -- case where gl_euro_user_rate_api.is_cross_rate = 'N'
1732       -- here the exchange_rate is directly between the EMU and the non-EMU currency.
1733 
1734         p_exchange_rate := round(p_exchange_rate,38);
1735 
1736      END IF; --is_cross_rate
1737 
1738    END IF; -- exchange_rate >0
1739 
1740   END IF; --rate type <> 'User'
1741   END IF; --if echange rate type IS NOT NULL
1742  END IF;  --entered_currency <> functional currency
1743 
1744    IF PG_DEBUG in ('Y', 'C') THEN
1745       arp_util.debug('Default_Currency_info ()+');
1746    END IF;
1747 EXCEPTION
1748  WHEN others THEN
1749   IF PG_DEBUG in ('Y', 'C') THEN
1750      arp_util.debug('EXCEPTION: Default_Currency_Info() ');
1751      arp_util.debug('Default_Currency_info: ' || 'p_currency_code  =  '||p_currency_code);
1752   END IF;
1753   raise;
1754 END Default_Currency_Info;
1755 
1756 /* modified for tca uptake */
1757 PROCEDURE Default_cash_ids(
1758               p_usr_currency_code            IN     fnd_currencies_vl.name%TYPE,
1759               p_usr_exchange_rate_type       IN     gl_daily_conversion_types.user_conversion_type%TYPE,
1760               p_customer_name                IN     hz_parties.party_name%TYPE,
1761               p_customer_number              IN     hz_cust_accounts.account_number%TYPE,
1762               p_location                     IN     hz_cust_site_uses.location%type,
1763               p_receipt_method_name          IN OUT NOCOPY ar_receipt_methods.name%TYPE,
1764               /* 6612301 */
1765               p_customer_bank_account_name   IN     iby_ext_bank_accounts_v.bank_account_name%TYPE,
1766               p_customer_bank_account_num    IN     iby_ext_bank_accounts_v.bank_account_number%TYPE,
1767               p_remittance_bank_account_name IN     ce_bank_accounts.bank_account_name%TYPE,
1768               p_remittance_bank_account_num  IN     ce_bank_accounts.bank_account_num%TYPE,
1769               p_currency_code                IN OUT NOCOPY ar_cash_receipts.currency_code%TYPE,
1770               p_exchange_rate_type           IN OUT NOCOPY ar_cash_receipts.exchange_rate_type%TYPE,
1771               p_customer_id                  IN OUT NOCOPY ar_cash_receipts.pay_from_customer%TYPE,
1772               p_customer_site_use_id         IN OUT NOCOPY hz_cust_site_uses.site_use_id%TYPE,
1773               p_receipt_method_id            IN OUT NOCOPY ar_cash_receipts.receipt_method_id%TYPE,
1774               /* 6612301 */
1775               p_customer_bank_account_id     IN OUT NOCOPY ar_cash_receipts.customer_bank_account_id%TYPE,
1776 	            p_customer_bank_branch_id     IN OUT NOCOPY  ar_cash_receipts.customer_bank_branch_id%TYPE,
1777               p_remittance_bank_account_id   IN OUT NOCOPY ar_cash_receipts.remit_bank_acct_use_id%TYPE,
1778               p_receipt_date                 IN     DATE,
1779               p_return_status                   OUT NOCOPY VARCHAR2,
1780               p_default_site_use             IN VARCHAR2 --4448307-4509459
1781                               )
1782 IS
1783 l_remittance_bank_account_id NUMBER;
1784 l_receipt_method_id          NUMBER;
1785 l_customer_id                NUMBER;
1786 l_return_status              VARCHAR2(1);
1787 l_get_id_return_status       VARCHAR2(1);
1788 l_get_x_val_return_status    VARCHAR2(1);
1789 l_boe_flag		     VARCHAR2(1);
1790 BEGIN
1791    IF PG_DEBUG in ('Y', 'C') THEN
1792       arp_util.debug('Default_cash_ids_from_values ()+');
1793    END IF;
1794    p_return_status := FND_API.G_RET_STS_SUCCESS;
1795 -- Customer ID/Number/Name
1796 -- If 0 has been entered in as the customer ID, consider it null
1797 IF (NVL(p_customer_id,0) = 0)
1798   THEN
1799    IF(p_customer_name IS NOT NULL) and
1800      (p_customer_number IS NULL)
1801     THEN
1802      p_customer_id := Get_Id('CUSTOMER_NAME',
1803                               p_customer_name,
1804                               l_get_id_return_status);
1805      IF p_customer_id IS NULL THEN
1806        FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUS_NAME_INVALID');
1807        FND_MSG_PUB.Add;
1808        p_return_status := FND_API.G_RET_STS_ERROR;
1809      END IF;
1810 
1811    ELSIF(p_customer_name IS NULL) and
1812         (p_customer_number IS NOT NULL)
1813     THEN
1814      p_customer_id := Get_Id( 'CUSTOMER_NUMBER',
1815                               p_customer_number,
1816                               l_get_id_return_status);
1817      IF p_customer_id IS NULL THEN
1818        FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUS_NUM_INVALID');
1819        FND_MSG_PUB.Add;
1820        p_return_status := FND_API.G_RET_STS_ERROR;
1821      END IF;
1822 
1823    ELSIF(p_customer_name IS NOT NULL) and
1824         (p_customer_number IS NOT NULL)
1825     THEN
1826      p_customer_id := Get_Cross_Validated_Id( 'CUSTOMER',
1827                                               p_customer_number,
1828                                               p_customer_name,
1829                                               l_get_x_val_return_status);
1830    IF p_customer_id IS NULL THEN
1831        FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUS_NAME_NUM_INVALID');
1832        FND_MSG_PUB.Add;
1833        p_return_status := FND_API.G_RET_STS_ERROR;
1834      END IF;
1835    END IF;
1836 
1837 ELSE
1838 --In case the ID has been entered by the user
1839    IF (p_customer_name IS NOT NULL) OR
1840       (p_customer_number IS NOT NULL) THEN
1841        --give a warning message to indicate that the customer_number and customer_name
1842        --entered by the user have been ignored.
1843        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
1844        	THEN
1845          FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUS_NAME_NUM_IGN');
1846          FND_MSG_PUB.Add;
1847          p_return_status := FND_API.G_RET_STS_ERROR;
1848        END IF;
1849     END IF;
1850 
1851 
1852 END IF;
1853 
1854  --Customer site use id
1855 /*
1856  IF p_customer_id IS NOT NULL  THEN
1857   IF p_customer_site_use_id IS NULL THEN
1858 
1859      p_customer_site_use_id := Get_Site_Use_Id(p_customer_id,
1860                                                p_location,
1861                                                'BILL_TO',
1862                                                'DRAWEE'  --added drawee for bug 1420529
1863                                                 );
1864   ELSE
1865     IF p_location IS NOT NULL THEN
1866       --raise warning that
1867       null;
1868     END IF;
1869   END IF;
1870  END IF;
1871 */
1872 
1873 
1874 /*bug4448307-4509459*/
1875 IF p_customer_id IS NOT NULL  THEN
1876     IF arp_global.sysparam.site_required_flag = 'Y' THEN
1877 
1878       IF p_customer_site_use_id IS NULL THEN
1879 
1880      p_customer_site_use_id := Get_Site_Use_Id(p_customer_id,
1881                                                p_location,
1882                                                'BILL_TO',
1883                                                'DRAWEE'  --added drawee for bug 1420529
1884                                                 );
1885      END IF;
1886    ELSE
1887     IF p_default_site_use = 'Y' THEN
1888         IF p_customer_site_use_id IS NULL THEN
1889 
1890      p_customer_site_use_id := Get_Site_Use_Id(p_customer_id,
1891                                                p_location,
1892                                                'BILL_TO',
1893                                                'DRAWEE'  --added drawee for bug 1420529
1894                                                 );
1895      END IF;
1896    ELSE
1897      null;
1898     END IF;
1899   END IF;
1900 END IF;
1901 /*bug4448307-4509459*/
1902 
1903 
1904 --Receipt method ID,Name
1905 IF p_receipt_method_id IS NULL
1906   THEN
1907    IF p_receipt_method_name IS NOT NULL THEN
1908       p_receipt_method_id := Get_Id('RECEIPT_METHOD_NAME',
1909                                      p_receipt_method_name,
1910                                      l_get_id_return_status
1911                                     );
1912      IF p_receipt_method_id IS NULL THEN
1913        FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_MD_NAME_INVALID');
1914        FND_MSG_PUB.Add;
1915        p_return_status := FND_API.G_RET_STS_ERROR;
1916      ELSE
1917         l_boe_flag := arpt_sql_func_util.check_BOE_Paymeth(p_receipt_method_id);
1918         if l_boe_flag = 'Y' then
1919            FND_MESSAGE.SET_NAME('AR','AR_BOE_OBSOLETE');
1920            FND_MSG_PUB.Add;
1921            p_return_status := FND_API.G_RET_STS_ERROR;
1922         end if;
1923      END IF;
1924    END IF;
1925  ELSE
1926     IF (p_receipt_method_name IS NOT NULL) THEN
1927        --give a warning message to indicate that the receipt_method_name
1928        --entered by the user has been ignored.
1929        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
1930        	THEN
1931          FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_MD_NAME_IGN');
1932          FND_MSG_PUB.Add;
1933        END IF;
1934     ELSE
1935         BEGIN
1936          SELECT name
1937          INTO   p_receipt_method_name
1938          FROM   ar_receipt_methods
1939          WHERE  receipt_method_id = p_receipt_method_id;
1940 
1941          l_boe_flag := arpt_sql_func_util.check_BOE_Paymeth(p_receipt_method_id);
1942          if l_boe_flag = 'Y' then
1943             FND_MESSAGE.SET_NAME('AR','AR_BOE_OBSOLETE');
1944             FND_MSG_PUB.Add;
1945             p_return_status := FND_API.G_RET_STS_ERROR;
1946          end if;
1947 
1948         EXCEPTION
1949          WHEN no_data_found THEN
1950           IF PG_DEBUG in ('Y', 'C') THEN
1951              arp_util.debug('Default_cash_ids: ' || 'Invalid receipt method id');
1952           END IF;
1953            null;
1954         END;
1955 
1956     END IF;
1957 
1958  END IF;
1959 
1960 --Currency code
1961 IF p_currency_code IS NULL THEN
1962    IF p_usr_currency_code IS NOT NULL
1963     THEN
1964       p_currency_code :=     Get_Id('CURRENCY_NAME',
1965                                      p_usr_currency_code,
1966                                      l_get_id_return_status
1967                                     );
1968       IF p_currency_code IS NULL THEN
1969         FND_MESSAGE.SET_NAME('AR','AR_RAPI_USR_CURR_CODE_INVALID');
1970         FND_MSG_PUB.Add;
1971         p_return_status := FND_API.G_RET_STS_ERROR;
1972       END IF;
1973    ELSE
1974 
1975      p_currency_code := arp_global.functional_currency;
1976      --Raise a warning message saying that currency was defaulted
1977      IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
1978        THEN
1979         FND_MESSAGE.SET_NAME('AR','AR_RAPI_FUNC_CURR_DEFAULTED');
1980         FND_MSG_PUB.Add;
1981      END IF;
1982 
1983    END IF;
1984 
1985 ELSE
1986    IF  (p_usr_currency_code IS NOT NULL) THEN
1987 
1988        --give a warning message to indicate that the usr_currency_code
1989        -- entered by the user have been ignored.
1990        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
1991        	THEN
1992          FND_MESSAGE.SET_NAME('AR','AR_RAPI_USR_CURR_CODE_IGN');
1993          FND_MSG_PUB.Add;
1994        END IF;
1995 
1996    END IF;
1997 END IF;
1998 
1999 /*  Revert changes done for customer bank ref under payment uptake */
2000 
2001 --Customer bank account Number,Name,ID
2002 
2003 IF p_customer_bank_account_id IS NULL
2004  THEN
2005   IF(p_customer_bank_account_name IS NOT NULL) and
2006      (p_customer_bank_account_num IS NULL)
2007    THEN
2008   /* p_customer_bank_account_id := Get_Id('CUST_BANK_ACCOUNT_NAME',
2009                                           p_customer_bank_account_name,
2010                                           l_get_id_return_status
2011                                         );
2012      IF p_customer_bank_account_id IS NULL THEN
2013        FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUS_BK_AC_NAME_INVALID');
2014        FND_MSG_PUB.Add;
2015        p_return_status := FND_API.G_RET_STS_ERROR;
2016      END IF; */
2017 
2018         begin
2019 				select  distinct bb.branch_party_id "bank_branch_id",
2020 					   bb.BANK_ACCOUNT_ID
2021 				into p_customer_bank_branch_id
2022 				     ,p_customer_bank_account_id
2023 				from iby_fndcpt_payer_assgn_instr_v a,
2024 				       iby_ext_bank_accounts_v bb
2025 				where a.cust_account_id = p_customer_id
2026 				and a.instrument_type = 'BANKACCOUNT'
2027 				and ( a.acct_site_use_id =  p_customer_site_use_id or a.acct_site_use_id is null)
2028 				and a.currency_code = p_currency_code
2029 				and bb.ext_bank_account_id = a.instrument_id
2030 				and bb.bank_account_name = p_customer_bank_account_name;
2031 		     exception
2032 		      when others then
2033 		       FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUS_BK_AC_NAME_INVALID');
2034 		       FND_MSG_PUB.Add;
2035 		       p_return_status := FND_API.G_RET_STS_ERROR;
2036 		     end;
2037 
2038   ELSIF(p_customer_bank_account_name IS  NULL) and
2039        (p_customer_bank_account_num IS NOT NULL)
2040    THEN
2041     /* p_customer_bank_account_id := Get_Id('CUST_BANK_ACCOUNT_NUMBER',
2042                                           p_customer_bank_account_num,
2043                                           l_get_id_return_status
2044                                         );
2045      IF p_customer_bank_account_id IS NULL THEN
2046        FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUS_BK_AC_NUM_INVALID');
2047        FND_MSG_PUB.Add;
2048        p_return_status := FND_API.G_RET_STS_ERROR;
2049      END IF; */
2050 
2051       begin
2052 			select  distinct bb.branch_party_id "bank_branch_id",
2053 				   bb.BANK_ACCOUNT_ID
2054 			into p_customer_bank_branch_id,
2055 			     p_customer_bank_account_id
2056 			from iby_fndcpt_payer_assgn_instr_v a,
2057 			       iby_ext_bank_accounts_v bb
2058 			where a.cust_account_id = p_customer_id
2059 			and a.instrument_type = 'BANKACCOUNT'
2060 			and ( a.acct_site_use_id =  p_customer_site_use_id or a.acct_site_use_id is null)
2061 			and a.currency_code = p_currency_code
2062 			and bb.ext_bank_account_id = a.instrument_id
2063 			and bb.bank_account_number = p_customer_bank_account_num;
2064 	     exception
2065 	      when others then
2066 	       FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUS_BK_AC_NUM_INVALID');
2067 	       FND_MSG_PUB.Add;
2068 	       p_return_status := FND_API.G_RET_STS_ERROR;
2069 	     end;
2070 
2071   ELSIF(p_customer_bank_account_name IS NOT NULL) and
2072        (p_customer_bank_account_num IS NOT NULL)
2073    THEN
2074      /* p_customer_bank_account_id := Get_Cross_Validated_Id( 'CUST_BANK_ACCOUNT',
2075                                               p_customer_bank_account_num,
2076                                               p_customer_bank_account_name,
2077                                               l_get_id_return_status
2078                                              );
2079      IF p_customer_bank_account_id IS NULL THEN
2080        FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUS_BK_AC_2_INVALID');
2081        FND_MSG_PUB.Add;
2082        p_return_status := FND_API.G_RET_STS_ERROR;
2083      END IF; */
2084 
2085         begin
2086 				select  distinct bb.branch_party_id "bank_branch_id",
2087 					   bb.BANK_ACCOUNT_ID
2088 				into p_customer_bank_branch_id
2089 				    ,p_customer_bank_account_id
2090 				from iby_fndcpt_payer_assgn_instr_v a,
2091 				       iby_ext_bank_accounts_v bb
2092 				where a.cust_account_id = p_customer_id
2093 				and a.instrument_type = 'BANKACCOUNT'
2094 				and ( a.acct_site_use_id =  p_customer_site_use_id or a.acct_site_use_id is null)
2095 				and a.currency_code = p_currency_code
2096 				and bb.ext_bank_account_id = a.instrument_id
2097 				and bb.bank_account_name = p_customer_bank_account_name
2098 				and bb.bank_account_number = p_customer_bank_account_num;
2099 		     exception
2100 		      when others then
2101 		       FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUS_BK_AC_2_INVALID');
2102 		       FND_MSG_PUB.Add;
2103 		       p_return_status := FND_API.G_RET_STS_ERROR;
2104 		     end;
2105 
2106   END IF;
2107 
2108 ELSE --In case the ID has been entered by the user
2109    IF (p_customer_bank_account_num IS NOT NULL) OR
2110       (p_customer_bank_account_name IS NOT NULL) THEN
2111        --give a warning message to indicate that the customer_bank_account_num and
2112        --p_customer_bank_account_name entered by the user have been ignored.
2113        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
2114        	THEN
2115          FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUS_BK_NAME_NUM_IGN');
2116          FND_MSG_PUB.Add;
2117        END IF;
2118     END IF;
2119 
2120 END IF;
2121 
2122 
2123 --Remittance bank account Number,Name,ID
2124 
2125 
2126  IF p_remittance_bank_account_id IS NULL
2127   THEN
2128   IF(p_remittance_bank_account_name IS NOT NULL) and
2129      (p_remittance_bank_account_num IS NULL)
2130     THEN
2131       p_remittance_bank_account_id := Get_Id('REMIT_BANK_ACCOUNT_NAME',
2132                                              p_remittance_bank_account_name,
2133                                              l_get_id_return_status
2134                                             );
2135      IF p_remittance_bank_account_id IS NULL THEN
2136        FND_MESSAGE.SET_NAME('AR','AR_RAPI_REM_BK_AC_NAME_INVALID');
2137        FND_MSG_PUB.Add;
2138        p_return_status := FND_API.G_RET_STS_ERROR;
2139      END IF;
2140    ELSIF(p_remittance_bank_account_name IS  NULL) and
2141         (p_remittance_bank_account_num IS NOT NULL)
2142     THEN
2143       p_remittance_bank_account_id := Get_Id('REMIT_BANK_ACCOUNT_NUMBER',
2144                                               p_remittance_bank_account_num,
2145                                               l_get_id_return_status
2146                                              );
2147      IF p_remittance_bank_account_id IS NULL THEN
2148        FND_MESSAGE.SET_NAME('AR','AR_RAPI_REM_BK_AC_NUM_INVALID');
2149        FND_MSG_PUB.Add;
2150        p_return_status := FND_API.G_RET_STS_ERROR;
2151      END IF;
2152    ELSIF(p_remittance_bank_account_name IS NOT NULL) and
2153         (p_remittance_bank_account_num IS NOT NULL)
2154     THEN
2155       p_remittance_bank_account_id := Get_Cross_Validated_Id( 'REMIT_BANK_ACCOUNT',
2156                                                p_remittance_bank_account_num,
2157                                                p_remittance_bank_account_name,
2158                                                l_get_x_val_return_status
2159                                               );
2160      IF p_remittance_bank_account_id IS NULL THEN
2161        FND_MESSAGE.SET_NAME('AR','AR_RAPI_REM_BK_AC_2_INVALID');
2162        FND_MSG_PUB.Add;
2163        p_return_status := FND_API.G_RET_STS_ERROR;
2164      END IF;
2165    END IF;
2166 
2167  ELSE
2168 
2169    IF (p_remittance_bank_account_name IS NOT NULL) OR
2170       (p_remittance_bank_account_num IS NOT NULL)
2171     THEN
2172        --give a warning message to indicate that the remittance_bank_account_num and
2173        --remittance_bank_account_name entered by the user have been ignored.
2174        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
2175        	THEN
2176          FND_MESSAGE.SET_NAME('AR','AR_RAPI_REM_BK_AC_NAME_NUM_IGN');
2177          FND_MSG_PUB.Add;
2178        END IF;
2179    END IF;
2180 
2181  END IF;
2182 
2183 -- Exchange_rate_type
2184  IF p_exchange_rate_type IS NULL THEN
2185    IF p_usr_exchange_rate_type IS NOT NULL
2186     THEN
2187       p_exchange_rate_type := Get_Id('EXCHANGE_RATE_TYPE_NAME',/* Bug fix 2982212*/
2188                                      p_usr_exchange_rate_type,
2189                                      l_get_id_return_status
2190                                     );
2191       IF p_exchange_rate_type IS NULL THEN
2192         FND_MESSAGE.SET_NAME('AR','AR_RAPI_USR_X_RATE_TYP_INVALID');
2193         FND_MSG_PUB.Add;
2194         p_return_status := FND_API.G_RET_STS_ERROR;
2195       END IF;
2196    END IF;
2197 
2198  ELSE
2199    IF  (p_usr_exchange_rate_type IS NOT NULL) THEN
2200        --give a warning message to indicate that the usr_exchange_rate_type
2201        -- entered by the user have been ignored.
2202        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
2203        	THEN
2204          FND_MESSAGE.SET_NAME('AR','AR_RAPI_USR_X_RATE_TYPE_IGN');
2205          FND_MSG_PUB.Add;
2206        END IF;
2207    END IF;
2208 
2209  END IF;
2210 
2211 
2212    IF PG_DEBUG in ('Y', 'C') THEN
2213       arp_util.debug('Default_cash_ids_from_values ()-');
2214    END IF;
2215 EXCEPTION
2216  WHEN others THEN
2217   IF PG_DEBUG in ('Y', 'C') THEN
2218      arp_util.debug('EXCEPTION: Default_cash_ids_from_values() ',
2219                             G_MSG_UERROR);
2220   END IF;
2221   raise;
2222 
2223 END Default_cash_ids;
2224 
2225 PROCEDURE Get_Cash_Defaults(
2226           p_currency_code               IN OUT NOCOPY ar_cash_receipts.currency_code%TYPE,
2227           p_exchange_rate_type          IN OUT NOCOPY ar_cash_receipts.exchange_rate_type%TYPE,
2228           p_exchange_rate               IN OUT NOCOPY ar_cash_receipts.exchange_rate%TYPE,
2229           p_exchange_rate_date          IN OUT NOCOPY ar_cash_receipts.exchange_date%TYPE,
2230           p_amount                      IN OUT NOCOPY ar_cash_receipts.amount%TYPE,
2231           p_factor_discount_amount      IN OUT NOCOPY ar_cash_receipts.factor_discount_amount%TYPE,
2232           p_receipt_date                IN OUT NOCOPY ar_cash_receipts.receipt_date%TYPE,
2233           p_gl_date                     IN OUT NOCOPY DATE,
2234           p_maturity_date               IN OUT NOCOPY DATE,
2235           p_customer_receipt_reference  IN OUT NOCOPY ar_cash_receipts.customer_receipt_reference%TYPE,
2236           p_override_remit_account_flag IN OUT NOCOPY ar_cash_receipts.override_remit_account_flag%TYPE,
2237           p_remittance_bank_account_id  IN OUT NOCOPY ar_cash_receipts.remit_bank_acct_use_id%TYPE,
2238           p_deposit_date                IN OUT NOCOPY ar_cash_receipts.deposit_date%TYPE,
2239           p_receipt_method_id           IN OUT NOCOPY ar_cash_receipts.receipt_method_id%TYPE,
2240           p_state                       OUT NOCOPY ar_receipt_classes.creation_status%TYPE,
2241           p_anticipated_clearing_date   IN OUT NOCOPY ar_cash_receipts.anticipated_clearing_date%TYPE,
2242           p_called_from                 IN  VARCHAR2,
2243           p_creation_method_code           OUT NOCOPY ar_receipt_classes.creation_method_code%TYPE,
2244           p_return_status               OUT NOCOPY VARCHAR2
2245            )
2246 IS
2247  l_cr                       CONSTANT char(1)        := '';
2248  l_temp_id                  NUMBER(15);
2249  l_temp_char                VARCHAR2(80) ;
2250  l_temp_date                DATE ;
2251  l_def_curr_return_status   VARCHAR2(1);
2252  l_def_rm_return_status     VARCHAR2(1);
2253  l_def_gl_dt_return_status  VARCHAR2(1);
2254  /* Bug fix 3315058 */
2255  l_bank_acc_val_ret_status   VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
2256 BEGIN
2257 
2258    IF PG_DEBUG in ('Y', 'C') THEN
2259       arp_util.debug('Get_Cash_Defaults()+ ');
2260    END IF;
2261    p_return_status := FND_API.G_RET_STS_SUCCESS;
2262   -- default the receipt date if NULL
2263   IF (p_receipt_date IS NULL)
2264     THEN
2265     Select trunc(sysdate)
2266     into p_receipt_date
2267     from dual;
2268   END IF;
2269 
2270   -- default the gl_date
2271   IF p_gl_date IS NULL THEN
2272     Default_gl_date(p_receipt_date,
2273                     p_gl_date,
2274                     NULL,
2275                     l_def_gl_dt_return_status);
2276     IF PG_DEBUG in ('Y', 'C') THEN
2277        arp_util.debug('Get_Cash_Defaults: ' || 'l_default_gl_date_return_status : '||l_def_gl_dt_return_status);
2278     END IF;
2279   END IF;
2280 
2281   IF (p_deposit_date IS NULL)
2282     THEN
2283     p_deposit_date := p_receipt_date;
2284   END IF;
2285 
2286   IF (p_maturity_date IS NULL) THEN
2287      p_maturity_date := p_deposit_date;
2288   END IF;
2289 
2290  -- Default the Currency parameters
2291     Default_Currency_info(p_currency_code,
2292                           p_receipt_date,
2293                           p_exchange_rate_date,
2294                           p_exchange_rate_type,
2295                           p_exchange_rate,
2296                           l_def_curr_return_status
2297                          );
2298 
2299 --Set the precision of the receipt amount as per currency
2300   IF p_amount is NOT NULL THEN
2301    p_amount := arp_util.CurrRound( p_amount,
2302                                    p_currency_code
2303                                   );
2304   END IF;
2305 
2306  --Default the Receipt Method related parameters
2307    Default_Receipt_Method_info(p_receipt_method_id,
2308                                p_currency_code,
2309                                p_receipt_date,
2310                                p_remittance_bank_account_id,
2311                                p_state,
2312                                p_creation_method_code,
2313                                p_called_from,
2314                                l_def_rm_return_status
2315                                 );
2316 
2317 /* Bug fix 3315058 */
2318    Validate_Receipt_Method_ccid(p_receipt_method_id,
2319                                 p_remittance_bank_account_id,
2320                                 p_gl_date,
2321                                 l_bank_acc_val_ret_status);
2322 
2323   --default the override_remit_bank_account_flag
2324   IF (p_override_remit_account_flag IS NULL) THEN
2325      p_override_remit_account_flag := 'Y';
2326   END IF;
2327 
2328   IF l_def_rm_return_status <> FND_API.G_RET_STS_SUCCESS OR
2329      l_def_gl_dt_return_status <> FND_API.G_RET_STS_SUCCESS OR
2330      l_def_curr_return_status <> FND_API.G_RET_STS_SUCCESS  OR
2331      l_bank_acc_val_ret_status <> FND_API.G_RET_STS_SUCCESS THEN /* Bug3315058 */
2332        p_return_status := FND_API.G_RET_STS_ERROR;
2333   END IF;
2334   IF PG_DEBUG in ('Y', 'C') THEN
2335      arp_util.debug('Get_Cash_Defaults: ' || '************Cash Defaults********************');
2336      arp_util.debug('Get_Cash_Defaults: ' || 'p_receipt_date               : '||to_char(p_receipt_date,'DD-MON-YYYY'));
2337      arp_util.debug('Get_Cash_Defaults: ' || 'p_gl_date                    : '||to_char(p_gl_date,'DD-MON-YYYY'));
2338      arp_util.debug('Get_Cash_Defaults: ' || 'p_deposit_date               : '||to_char(p_deposit_date,'DD-MON-YYYY'));
2339      arp_util.debug('Get_Cash_Defaults: ' || 'p_maturity_date              : '||to_char(p_maturity_date,'DD-MON-YYYY'));
2340      arp_util.debug('Get_Cash_Defaults: ' || 'p_currency_code              : '||p_currency_code);
2341      arp_util.debug('Get_Cash_Defaults: ' || 'p_exchange_rate_date         : '||to_char(p_exchange_rate_date,'DD-MON-YYYY'));
2342      arp_util.debug('Get_Cash_Defaults: ' || 'p_exchange_rate_type         : '||p_exchange_rate_type);
2343      arp_util.debug('Get_Cash_Defaults: ' || 'p_exchange_rate              : '||to_char(p_exchange_rate));
2344      arp_util.debug('Get_Cash_Defaults: ' || 'p_receipt_method_id          : '||to_char(p_receipt_method_id));
2345      arp_util.debug('Get_Cash_Defaults: ' || 'remit bank acct use id : '||to_char(p_remittance_bank_account_id));
2346      arp_util.debug('Get_Cash_Defaults: ' || 'p_state                      : '||p_state);
2347      arp_util.debug('Get_Cash_Defaults ()-');
2348   END IF;
2349 END Get_Cash_Defaults;
2350 
2351 FUNCTION Get_grace_days(
2352           p_customer_id   IN NUMBER,
2353           p_bill_to_site_use_id IN NUMBER) RETURN NUMBER IS
2354 
2355 l_grace_days         NUMBER;
2356 l_bill_to_site_use_id  NUMBER;
2357 
2358 BEGIN
2359 
2360    l_bill_to_site_use_id := NVL(p_bill_to_site_use_id,-99999);
2361   IF p_customer_id IS NOT NULL  THEN
2362 
2363 /* modified for tca uptake */
2364    SELECT NVL(NVL(site.discount_grace_days, cust.discount_grace_days),0)
2365     INTO  l_grace_days
2366     FROM
2367       hz_customer_profiles      cust,
2368       hz_customer_profiles      site,
2369       hz_cust_accounts          c
2370     WHERE
2371           c.cust_account_id     = p_customer_id
2372     AND   cust.cust_account_id  = c.cust_account_id
2373     AND   cust.site_use_id      IS NULL
2374     AND   site.cust_account_id (+)  = c.cust_account_id
2375     AND   site.site_use_id (+)  = l_bill_to_site_use_id;
2376   END IF;
2377 
2378   RETURN l_grace_days;
2379 
2380 EXCEPTION
2381    WHEN OTHERS THEN
2382     IF PG_DEBUG in ('Y', 'C') THEN
2383        arp_util.debug('EXCEPTION: Get_grace_days()');
2384     END IF;
2385     raise;
2386 END Get_grace_days;
2387 
2388 PROCEDURE Default_trans_to_receipt_rate(p_trx_currency_code IN VARCHAR2,
2389                                     p_cr_currency_code      IN VARCHAR2,
2390                                     p_receipt_date          IN VARCHAR2,
2391                                     p_trans_to_receipt_rate IN OUT NOCOPY NUMBER,
2392                                     p_amount_applied        IN NUMBER,
2393                                     p_amount_applied_from   IN NUMBER,
2394                                     p_cr_date               IN DATE,
2395                                     p_return_status         OUT NOCOPY VARCHAR2
2396                                       ) IS
2397 l_exchange_rate_type  VARCHAR2(30);
2398 l_temp_rate NUMBER;
2399 l_amount_applied  NUMBER;
2400 l_amount_applied_from  NUMBER;
2401 BEGIN
2402  IF PG_DEBUG in ('Y', 'C') THEN
2403     arp_util.debug('Default_trans_to_receipt_rate ()+');
2404  END IF;
2405 
2406  p_return_status := FND_API.G_RET_STS_SUCCESS;
2407 --if the trx currency and the receipt currency are not same
2408 IF p_cr_currency_code <> p_trx_currency_code THEN
2409 
2410    --do the rounding
2411    IF P_trans_to_receipt_rate IS NOT NULL THEN
2412      p_trans_to_receipt_rate := ROUND(p_trans_to_receipt_rate, 38);
2413    END IF;
2414 
2415   --get the default only if p_trans_to_receipt_rate has not been entered by
2416   --user
2417    IF p_trans_to_receipt_rate IS NULL AND
2418       (gl_currency_api.is_fixed_rate(
2419                                      p_cr_currency_code,
2420                                      p_trx_currency_code,
2421                                      p_cr_date
2422                                         ) <> 'Y')  THEN
2423 
2424      --try to first get the rate from gl if possible
2425 
2426       l_exchange_rate_type := pg_profile_cc_rate_type;
2427 
2428         IF (l_exchange_rate_type IS NOT NULL)
2429           THEN
2430           --relationship between the two currencies is not fixed
2431           --and the default CROSS_CURRENCY_RATE_TYPE exists
2432           --try to get the rate from gl
2433 
2434           BEGIN
2435                l_temp_rate := gl_currency_api.get_rate(
2436                                         p_trx_currency_code,
2437                                         p_cr_currency_code,
2438                                         p_cr_date,
2439                                         l_exchange_rate_type);
2440           EXCEPTION
2441            WHEN gl_currency_api.NO_RATE THEN
2442             --rate does not exist set appropriate message.
2443             --p_return_status := FND_API.G_RET_STS_ERROR ;
2444             l_temp_rate := NULL;
2445             IF PG_DEBUG in ('Y', 'C') THEN
2446                arp_util.debug('Default_trans_to_receipt_rate: ' || 'Exception : gl_currency_api.NO_RATE ');
2447             END IF;
2448            WHEN gl_currency_api.INVALID_CURRENCY  THEN
2449             -- invalid currency set appropriate message.
2450             --p_return_status := FND_API.G_RET_STS_ERROR ;
2451             l_temp_rate := NULL;
2452             IF PG_DEBUG in ('Y', 'C') THEN
2453                arp_util.debug('Default_trans_to_receipt_rate: ' || 'Exception: gl_currency_api.INVALID_CURRENCY ');
2454             END IF;
2455           END;
2456         END IF ;
2457 
2458    ELSIF gl_currency_api.is_fixed_rate(
2459                                         p_cr_currency_code,
2460                                         p_trx_currency_code,
2461                                         p_cr_date
2462                                            ) = 'Y'  THEN
2463         --In case of fixed relationship get the fixed rate and return it.
2464 
2465         BEGIN
2466            l_temp_rate := gl_currency_api.get_rate(
2467                                       p_trx_currency_code,
2468                                       p_cr_currency_code,
2469                                       p_cr_date,
2470                                       l_exchange_rate_type
2471                                       );
2472         EXCEPTION
2473          WHEN gl_currency_api.NO_RATE THEN
2474           --rate does not exist set appropriate message.
2475           --p_return_status := FND_API.G_RET_STS_ERROR ;
2476           l_temp_rate := NULL;
2477           IF PG_DEBUG in ('Y', 'C') THEN
2478              arp_util.debug('Default_trans_to_receipt_rate: ' || 'Exception : gl_currency_api.NO_RATE ');
2479           END IF;
2480          WHEN gl_currency_api.INVALID_CURRENCY  THEN
2481           -- invalid currency set appropriate message.
2482           --p_return_status := FND_API.G_RET_STS_ERROR ;
2483           l_temp_rate := NULL;
2484           IF PG_DEBUG in ('Y', 'C') THEN
2485              arp_util.debug('Default_trans_to_receipt_rate: ' || 'Exception: gl_currency_api.INVALID_CURRENCY ');
2486           END IF;
2487         END;
2488 
2489         IF p_trans_to_receipt_rate IS NOT  NULL THEN
2490           IF l_temp_rate <> p_trans_to_receipt_rate THEN
2491            --raise error as the user specified CC rate is not the same as the
2492            --fixed rate.
2493            p_return_status := FND_API.G_RET_STS_ERROR;
2494            FND_MESSAGE.SET_NAME('AR','AR_RAPI_CC_RATE_INVALID');
2495            FND_MSG_PUB.Add;
2496            return;
2497           END IF;
2498         END IF;
2499 
2500    END IF;
2501 
2502         --In the case where, neither the fixed rate relationship
2503         --exists between the two currencies nor rate_type is available
2504         --so user needs to enter the value here or the trans_to_receipt_rate
2505         --is to be derived from the amount applied and the amount applied from.
2506 
2507       IF l_temp_rate IS NOT NULL THEN
2508           p_trans_to_receipt_rate := l_temp_rate;
2509 
2510       ELSE
2511 
2512           --rate does not exist in the gl so try to get it from the
2513           --following formula for a CC application:
2514           --p_trans_to_receipt_rate =
2515           --              p_amount_applied_from/p_amount_applied
2516 
2517           IF p_amount_applied_from IS NOT NULL AND
2518              p_amount_applied IS NOT NULL
2519            THEN
2520              l_amount_applied_from := arp_util.CurrRound(
2521                                          p_amount_applied_from,
2522                                          p_cr_currency_code
2523                                           );
2524              /* Bugfix 2916389. Use p_trx_currency_code instead of
2525 		p_cr_currency_code. */
2526              l_amount_applied := arp_util.CurrRound(
2527                                          p_amount_applied,
2528                                          p_trx_currency_code
2529                                           );
2530              p_trans_to_receipt_rate := ROUND(l_amount_applied_from/l_amount_applied, 38);
2531 
2532           END IF;
2533       END IF;
2534 
2535  END IF;
2536 IF PG_DEBUG in ('Y', 'C') THEN
2537    arp_util.debug('Default_trans_to_receipt_rate: ' || 'p_trans_to_receipt_rate :'||to_char(p_trans_to_receipt_rate));
2538    arp_util.debug('Default_trans_to_receipt_rate ()-');
2539 END IF;
2540 EXCEPTION
2541  WHEN others THEN
2542   IF PG_DEBUG in ('Y', 'C') THEN
2543      arp_util.debug('EXCEPTION: Default_trans_to_receipt_rate()');
2544   END IF;
2545   raise;
2546 END Default_trans_to_receipt_rate;
2547 
2548 PROCEDURE Default_amount_applied_from(
2549                  p_amount_applied        IN NUMBER,
2550                  p_trx_currency_code     IN VARCHAR2,
2551                  p_trans_to_receipt_rate IN NUMBER,
2552                  p_cr_currency_code      IN VARCHAR2,
2553                  p_amount_applied_from   IN OUT NOCOPY NUMBER,
2554                  p_return_status         OUT NOCOPY VARCHAR2,
2555                  p_cash_receipt_id       IN NUMBER DEFAULT NULL -- Added for Bug 13595334
2556                     )  IS
2557 
2558 -- Added for Bug 13595334
2559 
2560 
2561 l_amt_app_from_prev NUMBER;
2562 l_amt_app_prev NUMBER;
2563 l_total_amt_app NUMBER;
2564 l_total_amt_app_from NUMBER;
2565 l_amount_app_from NUMBER;
2566 l_amt_due_remaining NUMBER;
2567 l_amt_due_original NUMBER;
2568 
2569 
2570 BEGIN
2571    IF PG_DEBUG in ('Y', 'C') THEN
2572       arp_util.debug('Default_amount_applied_from ()+');
2573    END IF;
2574     p_return_status := FND_API.G_RET_STS_SUCCESS;
2575 
2576    -- Added for Bug 13595334
2577 
2578    l_amt_app_from_prev := 0;
2579    l_amt_app_prev := 0;
2580    l_total_amt_app := 0;
2581    l_total_amt_app_from := 0;
2582    l_amount_app_from := 0;
2583    l_amt_due_remaining := 0;
2584    l_amt_due_original := 0;
2585 
2586 
2587    IF p_amount_applied_from IS NULL  THEN
2588       IF p_trx_currency_code <> p_cr_currency_code   THEN
2589 
2590         --this is the CC application case
2591         IF p_trans_to_receipt_rate IS NOT NULL AND
2592            p_amount_applied IS NOT NULL
2593          THEN
2594 
2595 	-- Added for Bug 13595334
2596 	/* while applying foreign currency invoices (foreign currency has different precision that functional currency)
2597 	   to functional currency receipt , the amount_applied_from has to be calculated by considering the
2598 	   ammount_applied and amount_applied_from of previous invoices applied.
2599 	   */
2600 
2601 	IF(p_cash_receipt_id IS NOT NULL) THEN
2602 
2603 	BEGIN
2604 	  SELECT amount_due_remaining,
2605 	         amount_due_original
2606 		  INTO l_amt_due_remaining,
2607 		      l_amt_due_original
2608 		  FROM ar_payment_schedules_all
2609 		  WHERE cash_receipt_id = p_cash_receipt_id;
2610 
2611 	EXCEPTION
2612 		WHEN no_data_found THEN
2613 		  l_amt_due_remaining := NULL;
2614 		  l_amt_due_original := NULL;
2615 	END;
2616 
2617 	IF nvl(l_amt_due_remaining,   0) <> nvl(l_amt_due_original,   0) THEN
2618 	  BEGIN
2619 	    SELECT SUM(ra.amount_applied),
2620       		SUM(ra.amount_applied_from)
2621 		    INTO l_amt_app_prev,
2622 		        l_amt_app_from_prev
2623 		    FROM ar_receivable_applications_all ra,
2624 		      ar_payment_schedules_all ps
2625 		    WHERE ra.cash_receipt_id = p_cash_receipt_id
2626 			     AND ra.status = 'APP'
2627 			     AND ra.display = 'Y'
2628 			     AND ra.trans_to_receipt_rate = p_trans_to_receipt_rate
2629 			     AND ps.invoice_currency_code = p_trx_currency_code
2630 			     AND ra.applied_payment_schedule_id = ps.payment_schedule_id;
2631 
2632 	  EXCEPTION
2633 		  WHEN no_data_found THEN
2634 		    l_amt_app_prev := NULL;
2635 		    l_amt_app_from_prev := NULL;
2636 	  END;
2637 
2638 	  IF l_amt_app_prev IS NOT NULL
2639 		   AND l_amt_app_from_prev IS NOT NULL THEN
2640 
2641 	    l_total_amt_app := l_amt_app_prev + p_amount_applied;
2642 
2643 	    l_total_amt_app_from := l_total_amt_app * p_trans_to_receipt_rate;
2644 
2645 	    l_amount_app_from := l_total_amt_app_from -l_amt_app_from_prev;
2646 
2647     	    p_amount_applied_from := arp_util.currround(l_amount_app_from,   p_cr_currency_code);
2648 
2649 	  ELSE
2650 	    p_amount_applied_from := arp_util.currround(p_amount_applied * p_trans_to_receipt_rate,   p_cr_currency_code);
2651 	  END IF;
2652 
2653 	 ELSE
2654 	  p_amount_applied_from := arp_util.currround(p_amount_applied * p_trans_to_receipt_rate,   p_cr_currency_code);
2655 	 END IF;
2656 	ELSE
2657 		p_amount_applied_from := arp_util.currround(p_amount_applied * p_trans_to_receipt_rate,   p_cr_currency_code);
2658 	END IF;
2659      END IF;
2660 
2661     END IF;
2662 
2663   ELSE
2664    --if user has entered the amount_applied_from then round it.
2665     p_amount_applied_from := arp_util.CurrRound(
2666                                       p_amount_applied_from,
2667                                       p_cr_currency_code
2668                                         );
2669   END IF;
2670 
2671    IF PG_DEBUG in ('Y', 'C') THEN
2672       arp_util.debug('Default_amount_applied_from ()+');
2673    END IF;
2674 
2675 EXCEPTION
2676  WHEN others THEN
2677   IF PG_DEBUG in ('Y', 'C') THEN
2678      arp_util.debug('Default_amount_applied_from: ' || 'EXCEPTION: Default_applied_amount_from()');
2679   END IF;
2680   raise;
2681 
2682 END Default_amount_applied_from;
2683 
2684 
2685 PROCEDURE Default_val_llca_parameters (
2686 	            p_llca_type		   IN VARCHAR2,
2687                     p_cash_receipt_id      IN	NUMBER,
2688                     p_customer_trx_id      IN	NUMBER,
2689     		    p_customer_trx_line_id IN NUMBER,
2690     		    p_amount_applied	   IN  OUT NOCOPY NUMBER,
2691 		    p_line_amount	   IN  OUT NOCOPY NUMBER,
2692 		    p_tax_amount           IN  OUT NOCOPY NUMBER,
2693 		    p_freight_amount       IN  OUT NOCOPY NUMBER,
2694 		    p_charges_amount       IN  OUT NOCOPY NUMBER,
2695 		    p_line_discount        IN  OUT NOCOPY NUMBER,
2696 		    p_tax_discount         IN  OUT NOCOPY NUMBER,
2697 		    p_freight_discount     IN  NUMBER,
2698 		    p_discount             IN  OUT NOCOPY NUMBER,
2699                     p_receipt_currency_code IN	VARCHAR2,
2700                     p_invoice_currency_code IN	VARCHAR2,
2701 		    p_cr_date		    IN  DATE,
2702 		    p_creation_sign 		IN VARCHAR2,
2703                     p_natural_app_only_flag 	IN VARCHAR2,
2704 		    p_return_status        OUT NOCOPY VARCHAR2,
2705                     p_msg_count            OUT NOCOPY NUMBER,
2706                     p_msg_data             OUT NOCOPY VARCHAR2
2707 		    )  IS
2708 
2709 cursor gt_lines_cur (p_cust_trx_id in number) is
2710 select * from ar_llca_trx_lines_gt
2711 where customer_trx_id = p_cust_trx_id;
2712 
2713 l_gt_count              NUMBER;
2714 l_line_amount_remaining	NUMBER;
2715 l_line_tax_remaining	NUMBER;
2716 l_line_number		NUMBER;
2717 l_calc_tot_amount_app   NUMBER;
2718 l_calc_line_per		NUMBER;
2719 l_calc_line_amount	NUMBER;
2720 l_calc_tax_amount	NUMBER;
2721 l_calc_freight_amount	NUMBER;
2722 l_cum_amount_app    	NUMBER := 0;
2723 l_cum_line_amount       NUMBER := 0;
2724 l_cum_tax_amount        NUMBER := 0;
2725 l_cum_line_discount     NUMBER := 0;
2726 l_cum_tax_discount      NUMBER := 0;
2727 l_count_err_gt          NUMBER;
2728 
2729 
2730 -- Bug 6931978 Cross Currency App
2731 l_def_rate_return_status VARCHAR2(1);
2732 l_receipt_date    DATE;
2733 l_trans_to_receipt_rate NUMBER;
2734 
2735 l_payment_amount_cal    NUMBER;
2736 l_payment_amount        NUMBER;
2737 l_sign_value_of_ps       NUMBER := 0;
2738 l_temp_amount            NUMBER := 0;
2739 l_message_name           VARCHAR2(100);
2740 
2741 
2742 BEGIN
2743     IF PG_DEBUG in ('Y', 'C') THEN
2744        arp_util.debug('Default_val_llca_parameters()+');
2745     END IF;
2746     p_return_status := FND_API.G_RET_STS_SUCCESS;
2747 
2748 
2749   -- Initialize the Sys Parameters /INV PS /REC PS / Copy Trx lines into GT
2750    arp_process_det_pkg.initialization (p_customer_trx_id,
2751  	                              p_cash_receipt_id,
2752 	 	  	 	      p_return_status,
2753 				      p_msg_data,
2754 				      p_msg_count);
2755 
2756     IF PG_DEBUG in ('Y', 'C') THEN
2757       arp_util.debug('Default_val_llca_parameters: Initialization Return Status : '||p_return_status);
2758     END IF;
2759 
2760     IF p_return_status <> FND_API.G_RET_STS_SUCCESS
2761     THEN
2762          FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
2763          FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_DET_PKG.INITIALIZTION '||SQLERRM);
2764          FND_MSG_PUB.Add;
2765          p_return_status := FND_API.G_RET_STS_ERROR ;
2766 
2767 	 IF PG_DEBUG in ('Y', 'C') THEN
2768                 arp_util.debug('Apply_In_Detail: ' || 'Error(s) occurred in arp_process_det_pkg.Initialization ');
2769          END IF;
2770         Return;
2771     END IF;
2772 
2773     IF  p_llca_type = 'S'
2774     THEN
2775        IF PG_DEBUG in ('Y', 'C') THEN
2776          arp_util.debug('Default_val_llca_parameters: Summary Level Application ');
2777        END IF;
2778        IF    p_line_amount IS NULL AND  p_tax_amount  IS NULL
2779         AND  p_freight_amount  IS NULL AND  p_charges_amount is NULL
2780        THEN
2781 		FND_MESSAGE.SET_NAME( 'AR','AR_RAPI_LTFC_AMT_NULL');
2782 		FND_MSG_PUB.ADD;
2783 		p_return_status := FND_API.G_RET_STS_ERROR ;
2784        END IF;
2785        IF    Nvl(p_amount_applied,0) <>
2786 		( Nvl(p_line_amount,0) + Nvl(p_tax_amount,0)
2787 		+ Nvl(p_freight_amount,0) + Nvl(p_charges_amount,0))
2788 	THEN
2789 	     p_amount_applied := Nvl(p_line_amount,0)
2790 			+ Nvl(p_tax_amount,0) + Nvl(p_freight_amount,0)
2791 			+ Nvl(p_charges_amount,0);
2792 
2793 	     IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
2794 	     THEN
2795 		FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_AMT_DEFLT_IGN');
2796 		FND_MSG_PUB.Add;
2797 	     END IF;
2798 
2799 	     IF PG_DEBUG in ('Y', 'C') THEN
2800 		 arp_util.debug('Default_application_info: ' ||
2801 		  'Amount applied has been defaulted to the sum of
2802 			line/tax/freight/charges amount ');
2803              END IF;
2804 	END IF;
2805 
2806 
2807    Elsif P_llca_type = 'L'
2808    Then
2809      IF PG_DEBUG in ('Y', 'C') THEN
2810          arp_util.debug('Default_val_llca_parameters: Line Level Application ');
2811       END IF;
2812      	 IF ( Nvl(p_line_amount,0) <> 0
2813    	  and Nvl(p_tax_amount,0) <> 0
2814 	  and nvl(p_charges_amount,0) <> 0
2815 	    )
2816 	 THEN
2817 	     p_line_amount    := NULL;
2818 	     p_tax_amount     := NULL;
2819 	     p_charges_amount := NULL;
2820 	     IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
2821 	     THEN
2822 	         FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_LTC_IGN');
2823 	         FND_MSG_PUB.Add;
2824 	     END IF;
2825 
2826 	     IF PG_DEBUG in ('Y', 'C') THEN
2827 		arp_util.debug('Default_val_llca_parameters: ' || 'Line, Tax,
2828 		, Charges amount has been defaulted to Null');
2829              END IF;
2830 	 END IF;
2831 
2832 	select count(*) into l_gt_count
2833 	from ar_llca_trx_lines_gt
2834 	where customer_trx_id = p_customer_trx_id
2835 	and rownum = 1;
2836 
2837         -- For All Lines, Amount due remaining / line remaining will be defaulted later.
2838        IF  nvl(l_gt_count,0) = 0
2839        THEN
2840            If  Nvl(p_amount_applied,0) <>  0
2841            THEN
2842              p_amount_applied := NULL;
2843 
2844 	     IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
2845 	     THEN
2846 	         FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_AMT_DEFLT_IGN');
2847 	         FND_MSG_PUB.Add;
2848 	     END IF;
2849 
2850 	     IF PG_DEBUG in ('Y', 'C') THEN
2851 		arp_util.debug('Default_val_llca_parameters: ' || 'Amount applied
2852 		amount has been defaulted to Null');
2853              END IF;
2854            END IF;
2855        Else
2856         -- Specified Lines
2857        IF PG_DEBUG in ('Y', 'C') THEN
2858            arp_util.debug('Specified one or more lines in PLSQL table... ');
2859        END IF;
2860 
2861        -- Calculate the line level amounts
2862        For sp_lines_row in gt_lines_cur(p_customer_trx_id)
2863        LOOP
2864         BEGIN
2865 
2866 	-- Bug 6931978
2867 	  IF p_invoice_currency_code = p_receipt_currency_code THEN
2868 		     If sp_lines_row.line_amount IS NULL  AND
2869 			sp_lines_row.tax_amount  IS NULL  AND
2870 			sp_lines_row.amount_applied is NULL AND
2871 			sp_lines_row.customer_trx_line_id is NOT NULL
2872 		     THEN
2873 			  ar_receipt_lib_pvt.populate_errors_gt (
2874 			  p_customer_trx_id      => p_customer_trx_id,
2875 			  p_customer_trx_line_id => sp_lines_row.customer_trx_line_id,
2876 			  p_error_message =>
2877 			   arp_standard.fnd_message('AR_RAPI_LTFC_AMT_NULL'),
2878 			  p_invalid_value	 => NULL
2879 			  );
2880 			  p_return_status := FND_API.G_RET_STS_ERROR ;
2881 			  EXIT;
2882 		     END IF;
2883            END IF;
2884 	-- Bug 6931978 END
2885         select  to_char(line.line_number) apply_to,
2886                 nvl(line.amount_due_remaining,0),
2887                 nvl(tax.amount_due_remaining,0)
2888         into
2889                 l_line_number,
2890                 l_line_amount_remaining,
2891                 l_line_tax_remaining
2892         from ra_customer_trx_lines line,
2893              (select link_to_cust_trx_line_id,
2894                      line_type,
2895                      sum(nvl(amount_due_original,0)) amount_due_original,
2896                      sum(nvl(amount_due_remaining,0)) amount_due_remaining
2897               from ra_customer_trx_lines
2898               where customer_trx_id =  sp_lines_row.customer_trx_id  -- Bug 7241703 Added condition
2899 	       and nvl(line_type,'TAX') =  'TAX'
2900               group by link_to_cust_trx_line_id,line_type
2901               ) tax
2902         where line.customer_Trx_id = sp_lines_row.customer_trx_id
2903         and   line.customer_trx_line_id = sp_lines_row.customer_trx_line_id
2904         and line.line_type = 'LINE'
2905         and line.customer_trx_line_id = tax.link_to_cust_trx_line_id (+);
2906         EXCEPTION
2907           WHEN NO_DATA_FOUND THEN
2908             IF PG_DEBUG in ('Y', 'C') THEN
2909                arp_util.debug('' || 'EXCEPTION: Default_val_llca_parameters()');
2910             END IF;
2911                p_return_status := FND_API.G_RET_STS_ERROR ;
2912               FND_MESSAGE.SET_NAME( 'AR','AR_RAPI_TRX_LINE_ID_INVALID');
2913               FND_MSG_PUB.ADD;
2914               RAISE;
2915           WHEN others THEN
2916             IF PG_DEBUG in ('Y', 'C') THEN
2917                arp_util.debug('' || 'EXCEPTION: Default_val_llca_parameters()');
2918             END IF;
2919             RAISE;
2920         END;
2921             IF PG_DEBUG in ('Y', 'C') THEN
2922                 arp_util.debug('Customer_trx_line_id =>'||to_char(sp_lines_row.customer_trx_line_id));
2923             END IF;
2924             Select decode ( ( Nvl(l_line_amount_remaining,0)
2925                                  / ( Nvl(l_line_amount_remaining,0)
2926                                    + Nvl(l_line_tax_remaining,0)
2927                                    )
2928                              ),0,1,
2929                              ( Nvl(l_line_amount_remaining,0)
2930                                  / ( Nvl(l_line_amount_remaining,0)
2931                                    + Nvl(l_line_tax_remaining,0)
2932                                    )
2933                              )
2934                            )
2935              into l_calc_line_per
2936              from dual;
2937 
2938 	-- Bug 6931978 Cross Currency App
2939               IF p_invoice_currency_code <> p_receipt_currency_code THEN
2940 		      IF Nvl(sp_lines_row.amount_applied_from,0) <> 0
2941 			 Then
2942 
2943 				Default_trans_to_receipt_rate(
2944 				       p_invoice_currency_code ,
2945 				       p_receipt_currency_code ,
2946 				       l_receipt_date ,
2947 				       l_trans_to_receipt_rate ,
2948 				       sp_lines_row.amount_applied,
2949 				       sp_lines_row.amount_applied_from,
2950 				       p_cr_date,
2951 				       l_def_rate_return_status
2952 						);
2953 					sp_lines_row.amount_applied :=  arp_util.CurrRound(
2954 									sp_lines_row.amount_applied_from / l_trans_to_receipt_rate,
2955 									p_invoice_currency_code);
2956 					sp_lines_row.tax_amount  := NULL;
2957 					sp_lines_row.line_amount := NULL;
2958 
2959 
2960 			      IF PG_DEBUG in ('Y', 'C') THEN
2961 				 arp_util.debug('Default_val_llca_parameters: ' || 'Default trans_to_receipt_rate status: '||l_def_rate_return_status);
2962 				 arp_util.debug('Default_val_llca_parameters: ' || 'l_trans_to_receipt_rate: '||l_trans_to_receipt_rate);
2963 				 arp_util.debug('Default_val_llca_parameters: ' || 'sp_lines_row.amount_applied: '||sp_lines_row.amount_applied);
2964 			      END IF;
2965 
2966 			END IF;
2967 		END IF;
2968 
2969         -- Bug 6931978 End
2970 
2971 
2972             -- First Priority Line Amount
2973             If sp_lines_row.line_amount is not null /*Nvl(sp_lines_row.line_amount,0) <> 0*/
2974             Then
2975                IF PG_DEBUG in ('Y', 'C') THEN
2976                 arp_util.debug('First priority : Line Amount ');
2977                 arp_util.debug('Line Amount has taken precedence over the amount applied ');
2978                END IF;
2979                l_calc_tot_amount_app := arp_util.CurrRound(
2980                                                 ( nvl(sp_lines_row.line_amount,0)
2981                                                  / nvl(l_calc_line_per,1)
2982                                                 )
2983                                                 ,p_invoice_currency_code);
2984                l_calc_line_amount    := arp_util.CurrRound(sp_lines_row.line_amount
2985                                                 ,p_invoice_currency_code);
2986 
2987             -- Calculate Line amount based on the Amount Applied.
2988             Elsif sp_lines_row.amount_applied is not null /*Nvl(sp_lines_row.amount_applied,0) <> 0*/
2989             Then
2990                IF PG_DEBUG in ('Y', 'C') THEN
2991                 arp_util.debug('Considered the Amount Applied value ');
2992                End If;
2993 
2994                 l_calc_tot_amount_app   := arp_util.CurrRound(sp_lines_row.amount_applied
2995                                                 ,p_invoice_currency_code);
2996                 l_calc_line_amount      :=  arp_util.CurrRound((l_calc_tot_amount_app
2997                                           * l_calc_line_per),p_invoice_currency_code);
2998             End If;
2999 
3000             IF PG_DEBUG in ('Y', 'C') THEN
3001                 arp_util.debug('l_calc_tot_amount_app -> '||to_char(l_calc_tot_amount_app));
3002                 arp_util.debug('l_calc_line_amount    -> '||to_char(l_calc_line_amount));
3003             END IF;
3004 
3005 
3006             -- Tax amount has taken precedence over the Line / amount applied
3007             If sp_lines_row.tax_amount is not null /*NVL(sp_lines_row.tax_amount,0) <> 0*/
3008             THEN
3009                IF PG_DEBUG in ('Y', 'C') THEN
3010                 arp_util.debug('Tax Amount has taken precedence over the amount applied ');
3011                End If;
3012                 l_calc_tax_amount := arp_util.CurrRound(sp_lines_row.tax_amount
3013                                                 ,p_invoice_currency_code);
3014 
3015                 l_calc_tot_amount_app := l_calc_line_amount +
3016                                                 l_calc_tax_amount;
3017             Else
3018                IF PG_DEBUG in ('Y', 'C') THEN
3019                 arp_util.debug('Amount Applied has taken precedence over the Tax Amount');
3020                End If;
3021               -- Amount applied has taken precedence over the tax amount
3022                 l_calc_tax_amount :=  arp_util.CurrRound((Nvl(l_calc_tot_amount_app,0)
3023                                           - Nvl(l_calc_line_amount,0))
3024                                           ,p_invoice_currency_code);
3025             End If;
3026 
3027 
3028             If Nvl(sp_lines_row.amount_applied,0) <> Nvl(l_calc_tot_amount_app,0)
3029             Then
3030                   IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
3031                   THEN
3032                          FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_AMT_DEFLT_IGN');
3033                          FND_MSG_PUB.Add;
3034                   END IF;
3035             END If;
3036 
3037 
3038             IF PG_DEBUG in ('Y', 'C') THEN
3039                 arp_util.debug('l_calc_tot_amount_app -> '||to_char(l_calc_tot_amount_app));
3040                 arp_util.debug('l_calc_line_amount   -> '||to_char(l_calc_line_amount));
3041                 arp_util.debug('l_calc_tax_amount    -> '||to_char(l_calc_tax_amount));
3042             END IF;
3043 
3044             -- Check for overapply
3045 		    l_payment_amount_cal := (-1) * l_calc_tot_amount_app;
3046 		    l_payment_amount     := NVL(l_line_amount_remaining,0) + NVL(l_line_tax_remaining,0);
3047 
3048 		    IF ( NVL( l_payment_amount, 0 ) < 0 ) THEN
3049 			l_sign_value_of_ps:= -1;
3050 		    ELSIF ( NVL( l_payment_amount, 0 ) > 0 ) THEN
3051 			l_sign_value_of_ps := 1;
3052 
3053 		    ELSE
3054 			-- check the creation_sign. The amount changed should only make the
3055 			-- amount_due_remaining go to where the creation_sign allows
3056 			--
3057 
3058 			arp_non_db_pkg.check_creation_sign( p_creation_sign,
3059 							    l_payment_amount_cal, 'WHEN-VALIDATE-ITEM',
3060 							    l_message_name);
3061 			IF l_message_name IS NOT NULL
3062 			THEN
3063 			 AR_RECEIPT_LIB_PVT.populate_errors_gt (
3064 					      p_customer_trx_id  => sp_lines_row.customer_trx_id,
3065 					      p_customer_trx_line_id => sp_lines_row.customer_trx_line_id,
3066 					      p_error_message	  => arp_standard.fnd_message('AR_LL_NO_OVERAPPLY'),
3067 					      p_invalid_value	 => NULL );
3068 			END IF;
3069 		    END IF;
3070 
3071 		     -- Then check whether it violates overapplication flag and natural
3072 		     -- application flag.
3073 		     --
3074 		     l_temp_amount := NVL( l_payment_amount, 0 ) + l_payment_amount_cal;
3075 		     IF ( ( l_sign_value_of_ps * l_temp_amount ) < 0 ) THEN
3076 			  AR_RECEIPT_LIB_PVT.populate_errors_gt (
3077 					      p_customer_trx_id  => sp_lines_row.customer_trx_id,
3078 					      p_customer_trx_line_id => sp_lines_row.customer_trx_line_id,
3079 					      p_error_message	  => arp_standard.fnd_message('AR_LL_NO_OVERAPPLY'),
3080 					      p_invalid_value	 => NULL );
3081 		     END IF;
3082 		     --
3083 		     -- check natural application
3084 		     --
3085 		 /*
3086 		   IF (p_natural_app_only_flag = 'Y') THEN
3087 		     IF ( l_payment_amount_cal < 0 ) THEN
3088 			     l_temp_amount := -1;
3089 		     ELSIF ( l_payment_amount_cal > 0 ) THEN
3090 			     l_temp_amount := 1;
3091 		     ELSE
3092 			     l_temp_amount := 0;
3093 		     END IF;
3094 
3095 		     IF (( l_sign_value_of_ps * l_temp_amount ) = 1) THEN
3096 		       AR_RECEIPT_LIB_PVT.populate_errors_gt (
3097 					      p_customer_trx_id  => sp_lines_row.customer_trx_id,
3098 					      p_customer_trx_line_id => sp_lines_row.customer_trx_line_id,
3099 					      p_error_message	  => arp_standard.fnd_message('AR_LL_NO_OVERAPPLY'),
3100 					      p_invalid_value	 => NULL );
3101 		     END IF;
3102 		   END IF;
3103                  */
3104 	    -- Check for overapply
3105 
3106 
3107 	    -- Update the GT table with calculated values
3108 
3109                Update ar_llca_trx_lines_gt
3110 	       set    amount_applied   = Nvl(l_calc_tot_amount_app,0),
3111                       line_amount      = Nvl(l_calc_line_amount,0),
3112                       tax_amount       = Nvl(l_calc_tax_amount,0)
3113 	       where customer_trx_id = p_customer_trx_id
3114 	       and   customer_trx_line_id = sp_lines_row.customer_trx_line_id;
3115 
3116              -- Running totals
3117                 l_cum_amount_app      := Nvl(l_cum_amount_app,0) + Nvl(l_calc_tot_amount_app,0);
3118                 l_cum_line_amount     := Nvl(l_cum_line_amount,0)+ Nvl(l_calc_line_amount,0);
3119                 l_cum_tax_amount      := Nvl(l_cum_tax_amount,0) + Nvl(l_calc_tax_amount,0);
3120                 l_cum_line_discount   := Nvl(l_cum_line_discount,0) + Nvl(sp_lines_row.line_discount,0);
3121                 l_cum_tax_discount    := Nvl(l_cum_tax_discount,0) + Nvl(sp_lines_row.tax_discount,0);
3122 
3123             IF PG_DEBUG in ('Y', 'C') THEN
3124                 arp_util.debug('l_cum_tot_amount_app -> '||to_char(l_cum_amount_app));
3125                 arp_util.debug('l_cum_line_amount   -> '||to_char(l_cum_line_amount));
3126                 arp_util.debug('l_cum_tax_amount    -> '||to_char(l_cum_tax_amount));
3127                 arp_util.debug('l_cum_line_discount -> '||to_char(l_cum_line_discount));
3128                 arp_util.debug('l_cum_tax_discount  -> '||to_char(l_cum_tax_discount));
3129 	    END IF;
3130        End Loop;
3131 
3132 		-- Check for overapply
3133 		select count(1) into l_count_err_gt from ar_llca_trx_errors_gt
3134 		where customer_trx_id = p_customer_trx_id;
3135 
3136 		  IF l_count_err_gt <> 0 AND p_llca_type = 'L'
3137 		  THEN
3138 		          p_return_status := FND_API.G_RET_STS_ERROR;
3139 
3140 			  IF PG_DEBUG in ('Y', 'C') THEN
3141 			     arp_util.debug('Apply_In_Detail: ' || 'Error(s) occurred in Overapply check. ');
3142 		          END IF;
3143 
3144 		  END IF;
3145 		  -- Check for overapply
3146 
3147                  p_amount_applied := nvl(l_cum_amount_app,0) + nvl(p_freight_amount,0);
3148                  p_line_amount    := nvl(l_cum_line_amount,0);
3149                  p_tax_amount     := nvl(l_cum_tax_amount,0);
3150                  p_line_discount  := nvl(l_cum_line_discount,0);
3151                  p_tax_discount   := nvl(l_cum_tax_discount,0);
3152                  p_discount       := nvl(p_line_discount,0) + nvl(p_tax_discount,0)
3153                                       + nvl(p_freight_discount,0);
3154             IF PG_DEBUG in ('Y', 'C') THEN
3155                 arp_util.debug('p_amount_applied => '||to_char(p_amount_applied) );
3156                 arp_util.debug('p_line_amount    => '||to_char(l_cum_line_amount));
3157                 arp_util.debug('p_tax_amount     => '||to_char(l_cum_tax_amount));
3158                 arp_util.debug('p_freight_amount => '||to_char(p_freight_amount));
3159                 arp_util.debug('p_line_discount  => '||to_char(p_line_discount));
3160                 arp_util.debug('p_tax_discount   => '||to_char(p_tax_discount));
3161                 arp_util.debug('p_freight_discount => '||to_char(p_freight_discount));
3162                 arp_util.debug('p_discount (Total) => '||to_char(p_discount));
3163 	     END IF;
3164 
3165        END IF; /* GT COUNT END IF */
3166       END IF;   /* P_llca_type ENDIF */
3167 
3168 
3169     IF PG_DEBUG in ('Y', 'C') THEN
3170        arp_util.debug('Default_val_llca_parameters()-');
3171     END IF;
3172 EXCEPTION
3173  WHEN others THEN
3174   IF PG_DEBUG in ('Y', 'C') THEN
3175      arp_util.debug('EXCEPTION: Default_val_llca_parameters: ()');
3176   END IF;
3177   raise;
3178 END;
3179 
3180 PROCEDURE Default_disc_and_amt_applied(
3181                                      p_customer_id                 IN NUMBER,
3182                                      p_bill_to_site_use_id         IN NUMBER,
3183                                      p_applied_payment_schedule_id IN NUMBER,
3184                                      p_amount_applied              IN  OUT NOCOPY NUMBER,
3185                                      p_discount                    IN OUT NOCOPY NUMBER,
3186                                      p_term_id                     IN NUMBER,
3187                                      p_installment                 IN NUMBER,
3188                                      p_trx_date                    IN DATE,
3189                                      p_cr_date                     IN DATE,
3190                                      p_cr_currency_code            IN VARCHAR2,
3191                                      p_trx_currency_code           IN VARCHAR2,
3192                                      p_cr_exchange_rate            IN NUMBER,
3193                                      p_trx_exchange_rate           IN NUMBER,--corresponds to app_folder.exchange_rate
3194                                      p_apply_date                  IN DATE,
3195                                      p_amount_due_original         IN NUMBER,
3196                                      p_amount_due_remaining        IN NUMBER,
3197                                      p_cr_unapp_amount             IN NUMBER,
3198                                      p_allow_overappln_flag        IN VARCHAR2,
3199                                      p_calc_discount_on_lines_flag IN VARCHAR2,
3200                                      p_partial_discount_flag       IN VARCHAR2,
3201                                      p_amount_line_items_original  IN NUMBER,
3202                                      p_discount_taken_unearned     IN NUMBER,
3203                                      p_discount_taken_earned       IN NUMBER,
3204                                      p_customer_trx_line_id        IN NUMBER,
3205                                      p_trx_line_amount             IN NUMBER,
3206      				     p_llca_type		   IN VARCHAR2,
3207                                      p_discount_max_allowed       OUT NOCOPY NUMBER,
3208                                      p_discount_earned_allowed    OUT NOCOPY NUMBER,
3209                                      p_discount_earned            OUT NOCOPY NUMBER,
3210                                      p_discount_unearned          OUT NOCOPY NUMBER,
3211                                      p_new_amount_due_remaining   OUT NOCOPY NUMBER,
3212                                      p_return_status              OUT NOCOPY VARCHAR2
3213                                       )
3214 IS
3215 
3216 l_grace_days                   NUMBER;
3217 l_applied_in_amount            NUMBER;
3218 l_earned_disc_pct              NUMBER;
3219 l_best_disc_pct                NUMBER;
3220 l_out_discount_date            DATE;
3221 l_out_earned_discount          NUMBER;
3222 l_out_unearned_discount        NUMBER;
3223 l_out_amount_to_apply          NUMBER;
3224 l_out_discount_to_take         NUMBER;
3225 l_discount_date                DATE;
3226 l_discount                     NUMBER;
3227 l_discount_mode                VARCHAR2(1);
3228 l_exchange_rate_type           VARCHAR2(30);
3229 tmp_rate                       NUMBER;
3230 l_default_amount_applied_flag  VARCHAR2(30);
3231 l_convert_amount_inv           NUMBER;
3232 l_convert_amount_cr	       NUMBER;
3233 l_allow_discount               VARCHAR2(1) DEFAULT 'Y'; /* Bug fix 3450317 */
3234 
3235 BEGIN
3236     IF PG_DEBUG in ('Y', 'C') THEN
3237        arp_util.debug('Default_disc_and_amt_applied ()+');
3238     END IF;
3239     p_return_status := FND_API.G_RET_STS_SUCCESS;
3240 IF PG_DEBUG in ('Y', 'C') THEN
3241    arp_util.debug('Default_disc_and_amt_applied: ' || 'p_discount = '||p_discount);
3242 END IF;
3243   IF  p_amount_applied  IS NULL THEN
3244     IF PG_DEBUG in ('Y', 'C') THEN
3245        arp_util.debug('Default_disc_and_amt_applied: ' || 'The p_amount_applied is NULL ');
3246     END IF;
3247     IF p_trx_currency_code <> p_cr_currency_code  --The cross-currency case
3248       THEN
3249          l_exchange_rate_type := pg_profile_cc_rate_type;
3250          IF PG_DEBUG in ('Y', 'C') THEN
3251             arp_util.debug('Default_disc_and_amt_applied: ' || 'default cross currency exchange rate type :'||l_exchange_rate_type);
3252          END IF;
3253        IF ( (gl_currency_api.is_fixed_rate(
3254                                         p_cr_currency_code,
3255                                         p_trx_currency_code,
3256                                         p_cr_date
3257                                            ) = 'Y' )  OR
3258             l_exchange_rate_type IS NOT NULL)
3259         THEN
3260          IF PG_DEBUG in ('Y', 'C') THEN
3261             arp_util.debug('Default_disc_and_amt_applied: ' || 'p_trx_currency_code    :'||p_trx_currency_code);
3262             arp_util.debug('Default_disc_and_amt_applied: ' || 'p_cr_currency_code     :'||p_cr_currency_code);
3263             arp_util.debug('Default_disc_and_amt_applied: ' || 'p_cr_date              :'||p_cr_date);
3264             arp_util.debug('Default_disc_and_amt_applied: ' || 'l_exchange_rate_type   :'||l_exchange_rate_type);
3265             arp_util.debug('Default_disc_and_amt_applied: ' || 'p_amount_due_remaining :'||to_char(p_amount_due_remaining));
3266             arp_util.debug('Default_disc_and_amt_applied: ' || 'p_cr_unapp_amount      :'||to_char(p_cr_unapp_amount));
3267          END IF;
3268 
3269          BEGIN
3270 
3271           /* bug 2174978 : cross currency applications are unable to do partial applications
3272 
3273              define converted amounts for
3274                a) transaction amount in receipt currency
3275                b) receipt amount in transaction currency
3276              and assign the appropriate converted amount to l_applied_in_amount below */
3277 
3278           -- invoice amount in receipt currency
3279           l_convert_amount_inv := gl_currency_api.convert_amount(
3280                                               p_trx_currency_code,
3281                                               p_cr_currency_code,
3282                                               p_cr_date,
3283                                               l_exchange_rate_type,
3284                                               p_amount_due_remaining);
3285 
3286           -- receipt amount in invoice currency
3287           l_convert_amount_cr := gl_currency_api.convert_amount(
3288                                               p_cr_currency_code,
3289                                               p_trx_currency_code,
3290                                               p_cr_date,
3291                                               l_exchange_rate_type,
3292                                               p_cr_unapp_amount);
3293 
3294           IF PG_DEBUG in ('Y', 'C') THEN
3295              arp_util.debug('Default_disc_and_amt_applied: ' || 'l_convert_amount_inv:'||to_char(l_convert_amount_inv));
3296              arp_util.debug('Default_disc_and_amt_applied: ' || 'l_convert_amount_cr :'||to_char(l_convert_amount_cr));
3297           END IF;
3298 
3299 
3300          EXCEPTION
3301            WHEN gl_currency_api.NO_RATE  THEN
3302               l_convert_amount_inv := NULL;
3303               l_convert_amount_cr  := NULL;
3304               IF PG_DEBUG in ('Y', 'C') THEN
3305                  arp_util.debug('Default_disc_and_amt_applied: ' || 'No rate exists in the GL for the cross currency conversion ');
3306               END IF;
3307            WHEN gl_currency_api.INVALID_CURRENCY  THEN
3308               IF PG_DEBUG in ('Y', 'C') THEN
3309                  arp_util.debug('Default_disc_and_amt_applied: ' || 'The Receipt/Invoice currency is Invalid ');
3310               END IF;
3311                raise;
3312          END;
3313 
3314          -- l_applied_in_amount is in terms of transaction currency
3315          -- because we need discounts in that currency
3316 
3317          IF (l_convert_amount_inv = p_cr_unapp_amount) THEN
3318             /* if invoice amount in receipt currency matches receipt's unapplied amount
3319                pass the invoice amount in invoice currency */
3320 
3321              l_applied_in_amount := p_amount_due_remaining;
3322              IF PG_DEBUG in ('Y', 'C') THEN
3323                 arp_util.debug('Default_disc_and_amt_applied: ' || 'l_convert_amount_inv = p_cr_unapp_amount, l_applied_in_amount = ' || to_char(l_applied_in_amount));
3324              END IF;
3325          ELSE
3326             /* bug 2174978 : if invoice amount in receipt currency <> receipt's unapplied amount
3327                pass the receipt amount in invoice currency */
3328 
3329              l_applied_in_amount := l_convert_amount_cr;
3330              IF PG_DEBUG in ('Y', 'C') THEN
3331                 arp_util.debug('Default_disc_and_amt_applied: ' || 'l_convert_amount_inv <> p_cr_unapp_amount, l_applied_in_amount :'||to_char(l_applied_in_amount));
3332              END IF;
3333          END IF;
3334 
3335           /* if exchange rate type was ok, but no rate in GL, the value returned is
3336              NULL so we have to set the value as though no fixed rate existed */
3337           IF (l_applied_in_amount IS NULL) THEN
3338               l_applied_in_amount := p_amount_due_remaining;
3339           END IF;
3340 
3341        ELSE
3342         -- No fixed rate exists then amount_due_remaining on the inv
3343         --is used as the amount for discount calculation
3344 
3345           l_applied_in_amount := p_amount_due_remaining;
3346        END IF;
3347     ELSE -- Same currency application
3348        /* Bug fix 3539657
3349           Set the l_applied_in_amount based on the value of ADR and the Unapplied amount */
3350         IF p_amount_due_remaining > p_cr_unapp_amount THEN
3351          l_applied_in_amount := p_cr_unapp_amount;
3352         ELSE
3353          l_applied_in_amount := p_amount_due_remaining;
3354         END IF;
3355     END IF;
3356 
3357     /* Bug fix 3539657
3358        If the p_customer_trx_line_id is specified, limit the amount applied to
3359        the line amount if the line amount is less than the defaulted amount applied */
3360     IF p_customer_trx_line_id IS NOT NULL
3361             AND p_llca_type is NULL
3362             AND p_trx_line_amount < l_applied_in_amount THEN
3363        l_applied_in_amount := p_trx_line_amount;
3364        --give a warning message here to let the user know that the
3365        --transaction line amount has been used as the default amount applied.
3366       IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS) THEN
3367          FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_LINE_AMT_DEFLT');
3368          FND_MSG_PUB.Add;
3369       END IF;
3370     END IF;
3371 
3372     -- Defaulting mode for disount routine.
3373     l_discount_mode := '3';
3374     l_default_amount_applied_flag := pg_profile_amt_applied_def;
3375   ELSE --user has entered the amount applied.
3376      If p_llca_type is NOT NULL
3377      THEN
3378        l_applied_in_amount := Nvl(p_amount_applied,0);
3379        -- Direct mode for discount routine.
3380        l_discount_mode := '3';
3381        l_default_amount_applied_flag := pg_profile_amt_applied_def;
3382      Else
3383        l_applied_in_amount := p_amount_applied;
3384       -- Direct mode for discount routine.
3385       l_discount_mode := '2';
3386       l_default_amount_applied_flag := '';
3387      End IF;
3388   END IF;
3389 
3390      -- Get discount grace days.
3391     l_grace_days := Get_grace_days( p_customer_id,
3392                                     p_bill_to_site_use_id );
3393 
3394     /* Bug fix 3450317
3395        See if discounts are allowed for this customer */
3396     SELECT NVL(NVL(site.discount_terms, cust.discount_terms),'Y')
3397     INTO  l_allow_discount
3398     FROM
3399       hz_customer_profiles      cust
3400     , hz_customer_profiles      site
3401     WHERE
3402           cust.cust_account_id          = p_customer_id
3403     AND   cust.site_use_id              IS NULL
3404     AND   site.cust_account_id (+)      = cust.cust_account_id
3405     AND   site.site_use_id (+)          = p_bill_to_site_use_id;
3406 
3407   IF p_applied_payment_schedule_id > 0 AND
3408      l_grace_days IS NOT NULL THEN
3409       IF PG_DEBUG in ('Y', 'C') THEN
3410          arp_util.debug('Default_disc_and_amt_applied: ' || 'Before calling the discounts routine ..');
3411       END IF;
3412 
3413       arp_calculate_discount.discounts_cover(
3414       -- IN PARAMETERS
3415       p_mode                    => l_discount_mode,
3416       p_invoice_currency_code   => p_trx_currency_code,
3417       p_ps_id                   => p_applied_payment_schedule_id,
3418       p_term_id                 => p_term_id,
3419       p_terms_sequence_number   => p_installment,
3420       p_trx_date                => p_trx_date,
3421       p_apply_date              => p_apply_date,
3422       p_grace_days              => l_grace_days,
3423       p_default_amt_apply_flag  => l_default_amount_applied_flag,
3424       p_partial_discount_flag   => p_partial_discount_flag,
3425       p_calc_discount_on_lines_flag=> p_calc_discount_on_lines_flag,
3426       p_allow_overapp_flag      => p_allow_overappln_flag,
3427       p_close_invoice_flag      => 'N',
3428       p_input_amount            => NVL(l_applied_in_amount,0),
3429       p_amount_due_original     => p_amount_due_original,
3430       p_amount_due_remaining    => p_amount_due_remaining,
3431       p_discount_taken_earned   =>  NVL(p_discount_taken_earned,0),
3432       p_discount_taken_unearned => NVL(p_discount_taken_unearned,0),
3433       p_amount_line_items_original=> p_amount_line_items_original,
3434       p_module_name             => 'RECEIPTAPI',
3435       p_module_version          => '1.0',
3436       p_allow_discount          => l_allow_discount , /* Bug fix 3450317 */
3437       --*** OUT NOCOPY
3438       p_earned_disc_pct         => l_earned_disc_pct,
3439       p_best_disc_pct           => l_best_disc_pct,
3440       p_out_discount_date       => l_discount_date,
3441       p_out_earned_discount     => l_out_earned_discount,
3442       p_out_unearned_discount   => l_out_unearned_discount,
3443       p_out_amount_to_apply     => l_out_amount_to_apply,
3444       p_out_discount_to_take    => l_discount
3445         );
3446 IF PG_DEBUG in ('Y', 'C') THEN
3447    arp_util.debug('Default_disc_and_amt_applied: ' || 'After calling the discounts routine ..');
3448    arp_util.debug('Default_disc_and_amt_applied: ' || 'l_out_earned_discount = '||l_out_earned_discount);
3449    arp_util.debug('Default_disc_and_amt_applied: ' || 'l_out_unearned_discount = '||l_out_unearned_discount);
3450    arp_util.debug('Default_disc_and_amt_applied: ' || 'l_discount = '||l_discount);
3451    arp_util.debug('Default_disc_and_amt_applied: ' || 'p_discount = '||p_discount);
3452 END IF;
3453    l_earned_disc_pct :=  ROUND(l_earned_disc_pct,6);
3454 
3455   -- Store the maximum allowed discount, to be used later while validating
3456    p_discount_max_allowed :=arp_util.CurrRound( l_out_earned_discount+
3457                                                 l_out_unearned_discount,
3458                                                 p_trx_currency_code);
3459    p_discount_earned_allowed := l_out_earned_discount;
3460 
3461    IF p_discount IS NULL THEN
3462      p_discount := l_discount;
3463    END IF;
3464 
3465   --earned discounts
3466    -- Bug 3527600: Allow for negative discount
3467    IF (ABS(p_discount) > ABS(p_discount_earned_allowed))
3468     THEN
3469       p_discount_earned := p_discount_earned_allowed;
3470    ELSE
3471       p_discount_earned := p_discount;
3472    END IF;
3473 
3474     -- unearned discounts
3475     /* Bug 2535663 - p_discount, the discount passed in, should be used, not
3476     l_discount which is the discount_earned */
3477     -- Bug 3527600: Allow for negative discount
3478     IF (((p_discount > 0) AND ( p_discount - p_discount_earned_allowed > 0)) OR
3479         ((p_discount < 0) AND ( p_discount - p_discount_earned_allowed < 0)))
3480     THEN
3481       p_discount_unearned := p_discount - p_discount_earned_allowed;
3482     ELSE
3483       p_discount_unearned := 0;
3484     END IF;
3485 
3486   -- Populate Amount Applied if user has not entered any value for it.
3487   -- if the line number has not been entered by the user or is NULL then
3488   -- default the amount_applied to the l_out_amount_to_apply obtained
3489   -- from the discounts routine above else default it to the inv_line_amount
3490 
3491   IF p_amount_applied IS NULL  THEN
3492    /* Bug fix 3539657 */
3493    /* IF p_customer_trx_line_id IS NULL
3494      THEN
3495        p_amount_applied := l_out_amount_to_apply;
3496     ELSE
3497 
3498       IF p_trx_line_amount IS NOT NULL AND
3499          p_trx_line_amount < l_out_amount_to_apply
3500        THEN
3501         p_amount_applied := p_trx_line_amount;
3502         --give a warning message here to let the user know that the
3503         --transaction line amount has been used as the default amount applied.
3504         IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS) THEN
3505           FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_LINE_AMT_DEFLT');
3506           FND_MSG_PUB.Add;
3507         END IF;
3508       END IF;
3509 
3510     END IF; */
3511 
3512     p_amount_applied := l_out_amount_to_apply;
3513   END IF;
3514 
3515   END IF;
3516 
3517   p_amount_applied := arp_util.CurrRound(p_amount_applied,
3518                                          p_trx_currency_code);
3519   p_discount := arp_util.CurrRound(p_discount,
3520                                  p_trx_currency_code);
3521   IF PG_DEBUG in ('Y', 'C') THEN
3522      arp_util.debug('Default_disc_and_amt_applied ()-');
3523   END IF;
3524 EXCEPTION
3525  WHEN others THEN
3526   IF PG_DEBUG in ('Y', 'C') THEN
3527      arp_util.debug('Default_disc_and_amt_applied: ' || 'EXCEPTION: Default_discount_and_amount_applied()');
3528   END IF;
3529   raise;
3530 END Default_disc_and_amt_applied;
3531 
3532 FUNCTION Get_trx_Line_amount(
3533                  p_customer_trx_id IN ra_customer_trx.customer_trx_id%TYPE,
3534                  p_payment_schedule_id IN ar_payment_schedules.payment_schedule_id%TYPE,
3535                  p_customer_trx_line_id IN ra_customer_trx_lines.customer_trx_line_id%TYPE,
3536                  p_return_status OUT NOCOPY VARCHAR2
3537                  ) RETURN NUMBER IS
3538 
3539 l_trx_line_amount  NUMBER;
3540 BEGIN
3541   IF p_customer_trx_id IS NOT NULL AND
3542      p_customer_trx_line_id IS NOT NULL
3543    THEN
3544 
3545     SELECT  ctl.extended_amount *
3546                nvl(tl.relative_amount,1)/ nvl(t.base_amount,1)
3547     INTO   l_trx_line_amount
3548     FROM  ra_customer_trx_lines ctl ,
3549           ra_terms t ,
3550           ra_terms_lines tl ,
3551           ar_payment_schedules ps
3552     WHERE ps.payment_schedule_id = p_payment_schedule_id and
3553           ctl.customer_trx_id = p_customer_trx_id and
3554           ctl.line_type = 'LINE' and
3555           tl.term_id(+) = ps.term_id and
3556           tl.sequence_num(+) = ps.terms_sequence_number and
3557           t.term_id(+) = tl.term_id and
3558           ctl.customer_trx_line_id = p_customer_trx_line_id;
3559 
3560          RETURN(l_trx_line_amount);
3561   END IF;
3562 
3563 EXCEPTION
3564   WHEN no_data_found THEN
3565    p_return_status := FND_API.G_RET_STS_ERROR ;
3566    FND_MESSAGE.SET_NAME( 'AR','AR_RAPI_TRX_LINE_ID_INVALID');
3567    FND_MSG_PUB.ADD;
3568    return(null);
3569 
3570   WHEN others THEN
3571     IF PG_DEBUG in ('Y', 'C') THEN
3572        arp_util.debug('Get_trx_Line_amount: ' || 'EXCEPTION: Default_Trx_Line_Amount()');
3573     END IF;
3574     raise;
3575 END Get_trx_Line_amount;
3576 
3577 
3578 PROCEDURE Default_Trx_Info(
3579               p_customer_trx_id              IN ra_customer_trx.customer_trx_id%TYPE,
3580               p_customer_trx_line_id         IN NUMBER,
3581               p_show_closed_invoices         IN VARCHAR2,
3582               p_cr_gl_date                   IN DATE,
3583               p_cr_customer_id               IN ar_cash_receipts.pay_from_customer%TYPE,
3584               p_cr_currency_code             IN VARCHAR2,
3585               p_cr_payment_schedule_id       IN NUMBER,
3586               p_cr_date                      IN DATE,
3587               p_called_from                  IN VARCHAR2,
3588               p_customer_id                  OUT NOCOPY NUMBER, --customer on transaction
3589               p_cust_trx_type_id             OUT NOCOPY ra_customer_trx.cust_trx_type_id%TYPE ,
3590               p_trx_due_date                 OUT NOCOPY DATE,
3591               p_trx_currency_code            OUT NOCOPY VARCHAR2,
3592               p_trx_exchange_rate            OUT NOCOPY NUMBER,
3593               p_trx_date                     OUT NOCOPY DATE,
3594               p_trx_gl_date                  OUT NOCOPY DATE,
3595               p_calc_discount_on_lines_flag  OUT NOCOPY VARCHAR2,
3596               p_partial_discount_flag        OUT NOCOPY VARCHAR2,
3597               p_allow_overappln_flag         OUT NOCOPY VARCHAR2,
3598               p_natural_appln_only_flag      OUT NOCOPY VARCHAR2,
3599               p_creation_sign                OUT NOCOPY VARCHAR2,
3600               p_applied_payment_schedule_id  IN OUT NOCOPY NUMBER,
3601               p_gl_date                      OUT NOCOPY DATE, --this is the application gl_date
3602               p_term_id                      OUT NOCOPY NUMBER,
3603               p_amount_due_original          OUT NOCOPY NUMBER,
3604               p_amount_line_items_original   OUT NOCOPY NUMBER,
3605               p_amount_due_remaining         OUT NOCOPY NUMBER,
3606               p_discount_taken_earned        OUT NOCOPY NUMBER,
3607               p_discount_taken_unearned      OUT NOCOPY NUMBER,
3608               p_trx_line_amount              OUT NOCOPY NUMBER,
3609               p_installment                  IN OUT NOCOPY NUMBER,
3610               p_bill_to_site_use_id          OUT NOCOPY NUMBER,
3611 	      p_llca_type		     IN	 VARCHAR2,
3612 	      p_line_items_original	     OUT NOCOPY NUMBER,
3613 	      p_line_items_remaining	     OUT NOCOPY NUMBER,
3614 	      p_tax_original		     OUT NOCOPY NUMBER,
3615 	      p_tax_remaining		     OUT NOCOPY NUMBER,
3616 	      p_freight_original	     OUT NOCOPY NUMBER,
3617 	      p_freight_remaining	     OUT NOCOPY NUMBER,
3618 	      p_rec_charges_charged	     OUT NOCOPY NUMBER,
3619 	      p_rec_charges_remaining	     OUT NOCOPY NUMBER,
3620               p_return_status                OUT NOCOPY VARCHAR2
3621                           ) IS
3622 l_location  VARCHAR2(50);
3623 l_found     VARCHAR2(1);
3624 l_applied_payment_schedule_id  NUMBER;
3625 l_line_amt_return_status   VARCHAR2(1);
3626 
3627 l_receipt_info_rec               AR_AUTOREC_API.receipt_info_rec;
3628 
3629 
3630 /* modified for tca uptake */
3631 CURSOR get_site_use_id IS
3632 SELECT site_use_id
3633 FROM   hz_cust_site_uses site_use,
3634        hz_cust_acct_sites acct_site
3635 WHERE  acct_site.cust_acct_site_id = site_use.cust_acct_site_id
3636 AND    acct_site.cust_account_id = p_customer_id
3637 AND    site_use.location = l_location
3638 AND    site_use.site_use_code IN ('BILL_TO','DRAWEE');
3639 
3640 BEGIN
3641  IF PG_DEBUG in ('Y', 'C') THEN
3642     arp_util.debug('Default_Trx_Info ()+');
3643  END IF;
3644  p_return_status := FND_API.G_RET_STS_SUCCESS;
3645 --this is only for regular application, not for on-account
3646 
3647 
3648 IF p_customer_trx_id IS NOT NULL AND
3649    p_installment IS NOT NULL THEN
3650 
3651   IF p_called_from IN ('BR_REMITTED',
3652                       'BR_FACTORED_WITH_RECOURSE',
3653                       'BR_FACTORED_WITHOUT_RECOURSE') THEN
3654     --called from the BR Remittance program
3655 
3656    -- To handle the case where the apply routine is called from the
3657    -- BR Remittance program, we refer to the underlying tables directly
3658    -- instead of accessing the view ar_open_trx_v. This is because
3659    -- as per the view definition of ar_open_trx_v  it will not return the
3660    -- 'reserved'(having reserved_type and reserved_value not null)
3661    -- transactions(Bill).
3662 
3663     IF arp_global.sysparam.pay_unrelated_invoices_flag = 'Y' THEN
3664 
3665         /* modified for tca uptake */
3666         SELECT
3667            ps.customer_id
3668          , ps.cust_trx_type_id
3669          , decode(ps.customer_id ,-1,NULL ,ps.due_date)
3670          , ps.invoice_currency_code
3671          , ps.exchange_rate
3672          , ct.trx_date
3673          , ps.gl_date
3674          , t.calc_discount_on_lines_flag
3675          , t.partial_discount_flag
3676          , ctt.allow_overapplication_flag
3677          , ctt.natural_application_only_flag
3678          , ctt.creation_sign
3679          , ps.payment_schedule_id
3680          , greatest(p_cr_gl_date,ps.gl_date,
3681                        decode(pg_profile_appln_gl_date_def,
3682                              'INV_REC_SYS_DT', sysdate, 'INV_REC_DT', ps.gl_date,
3683                                ps.gl_date)) gl_date
3684          , ps.term_id
3685          , ps.amount_due_original
3686          , ps.amount_line_items_original
3687          , arp_util.CurrRound(ps.amount_due_remaining,
3688                                  ps.invoice_currency_code)
3689          , ps.discount_taken_earned
3690          , ps.discount_taken_unearned
3691 	 , ps.amount_line_items_original
3692 	 , ps.amount_line_items_remaining
3693 	 , ps.tax_original
3694  	 , ps.tax_remaining
3695          , ps.freight_original
3696          , ps.freight_remaining
3697          , ps.receivables_charges_charged
3698 	 , ps.receivables_charges_remaining
3699          , su.location
3700         INTO
3701           p_customer_id ,
3702           p_cust_trx_type_id ,
3703           p_trx_due_date ,
3704           p_trx_currency_code,
3705           p_trx_exchange_rate,
3706           p_trx_date,
3707           p_trx_gl_date ,
3708           p_calc_discount_on_lines_flag ,
3709           p_partial_discount_flag ,
3710           p_allow_overappln_flag ,
3711           p_natural_appln_only_flag ,
3712           p_creation_sign ,
3713           l_applied_payment_schedule_id ,
3714           p_gl_date, --this is the application gl_date
3715           p_term_id ,
3716           p_amount_due_original,
3717           p_amount_line_items_original ,
3718           p_amount_due_remaining ,
3719           p_discount_taken_earned,
3720           p_discount_taken_unearned,
3721     	  p_line_items_original,
3722  	  p_line_items_remaining,
3723  	  p_tax_original,
3724 	  p_tax_remaining,
3725           p_freight_original,
3726           p_freight_remaining,
3727           p_rec_charges_charged,
3728           p_rec_charges_remaining,
3729           l_location
3730        FROM
3731            ra_customer_trx  ct
3732          , ra_cust_trx_types  ctt
3733          , hz_cust_site_uses   su
3734          , ra_batch_sources bs
3735          , ar_lookups   lu
3736          , hz_cust_accounts cust
3737          , ra_terms   t
3738          , ar_payment_schedules  ps
3739          , ar_cons_inv           ci
3740       WHERE
3741            ps.class                    in ('CB','CM','DEP','DM','INV','BR')
3742        AND ps.selected_for_receipt_batch_id is null
3743        AND t.term_id(+)                = ps.term_id
3744        AND ct.customer_trx_id(+)       = ps.customer_trx_id
3745        AND bs.batch_source_id (+)      = ct.batch_source_id
3746        AND ctt.cust_trx_type_id(+)     = ps.cust_trx_type_id
3747        AND cust.cust_account_id(+)          = ps.customer_id
3748        AND su.site_use_id(+)           = ps.customer_site_use_id
3749        AND ps.class                    = lu.lookup_code
3750        AND ct.previous_customer_trx_id is null
3751        AND lu.lookup_type              = 'INV/CM'
3752        AND ci.cons_inv_id(+)           = ps.cons_inv_id
3753        AND ct.customer_trx_id =  p_customer_trx_id
3754        AND ps.invoice_currency_code =
3755                   decode(nvl(pg_profile_enable_cc,'N'),
3756                          'Y',ps.invoice_currency_code,p_cr_currency_code)
3757        AND ps.status=decode(p_show_closed_invoices,'Y',ps.status,'OP')
3758        AND ps.terms_sequence_number = p_installment
3759        ;
3760     ELSE --This is the case where pay_unrelated_invoices_flag is 'N'
3761 
3762         /* modified for tca uptake */
3763        SELECT
3764            ps.customer_id
3765          , ps.cust_trx_type_id
3766          , decode(ps.customer_id ,-1,NULL,ps.due_date)
3767          , ps.invoice_currency_code
3768          , ps.exchange_rate
3769          , ct.trx_date
3770          , ps.gl_date
3771          , t.calc_discount_on_lines_flag
3772          , t.partial_discount_flag
3773          , ctt.allow_overapplication_flag
3774          , ctt.natural_application_only_flag
3775          , ctt.creation_sign
3776          , ps.payment_schedule_id
3777          , greatest(p_cr_gl_date,ps.gl_date,
3778                        decode(pg_profile_appln_gl_date_def,
3779                              'INV_REC_SYS_DT', sysdate, 'INV_REC_DT', ps.gl_date,
3780                                ps.gl_date)) gl_date
3781          , ps.term_id
3782          , ps.amount_due_original
3783          , ps.amount_line_items_original
3784          , arp_util.CurrRound(ps.amount_due_remaining,
3785                                  ps.invoice_currency_code)
3786          , ps.discount_taken_earned
3787          , ps.discount_taken_unearned
3788 	 , ps.amount_line_items_original
3789 	 , ps.amount_line_items_remaining
3790 	 , ps.tax_original
3791  	 , ps.tax_remaining
3792          , ps.freight_original
3793          , ps.freight_remaining
3794          , ps.receivables_charges_charged
3795 	 , ps.receivables_charges_remaining
3796          , su.location
3797         INTO
3798           p_customer_id ,
3799           p_cust_trx_type_id ,
3800           p_trx_due_date ,
3801           p_trx_currency_code,
3802           p_trx_exchange_rate,
3803           p_trx_date,
3804           p_trx_gl_date ,
3805           p_calc_discount_on_lines_flag ,
3806           p_partial_discount_flag ,
3807           p_allow_overappln_flag ,
3808           p_natural_appln_only_flag ,
3809           p_creation_sign ,
3810           l_applied_payment_schedule_id ,
3811           p_gl_date, --this is the application gl_date
3812           p_term_id ,
3813           p_amount_due_original,
3814           p_amount_line_items_original ,
3815           p_amount_due_remaining ,
3816           p_discount_taken_earned,
3817           p_discount_taken_unearned,
3818     	  p_line_items_original,
3819  	  p_line_items_remaining,
3820  	  p_tax_original,
3821 	  p_tax_remaining,
3822           p_freight_original,
3823           p_freight_remaining,
3824           p_rec_charges_charged,
3825           p_rec_charges_remaining,
3826           l_location
3827      FROM
3828            ra_customer_trx  ct
3829          , ra_cust_trx_types  ctt
3830          , hz_cust_site_uses   su
3831          , ra_batch_sources bs
3832          , ar_lookups   lu
3833          , hz_cust_accounts   cst
3834          , ra_terms   t
3835          , ar_payment_schedules  ps
3836          , ar_cons_inv           ci
3837      WHERE
3838            ps.class                    in ('CB','CM','DEP','DM','INV','BR')
3839        AND ps.selected_for_receipt_batch_id is null
3840        AND t.term_id(+)                = ps.term_id
3841        AND ct.customer_trx_id(+)       = ps.customer_trx_id
3842        AND bs.batch_source_id (+)      = ct.batch_source_id
3843        AND ctt.cust_trx_type_id(+)     = ps.cust_trx_type_id
3844        AND cst.cust_account_id(+)      = ps.customer_id
3845        AND su.site_use_id(+)           = ps.customer_site_use_id
3846        AND ps.class                    = lu.lookup_code
3847        AND ct.previous_customer_trx_id is null
3848        AND lu.lookup_type              = 'INV/CM'
3849        AND ci.cons_inv_id(+)           = ps.cons_inv_id
3850        AND ct.customer_trx_id =  p_customer_trx_id
3851        AND ps.invoice_currency_code =
3852                   decode(nvl(pg_profile_enable_cc,'N'),
3853                          'Y',ps.invoice_currency_code,p_cr_currency_code)
3854        AND ps.status=decode(p_show_closed_invoices,'Y',ps.status,'OP')
3855        AND ps.terms_sequence_number = p_installment
3856        AND ps.customer_id IN (
3857           SELECT rcr.related_cust_account_id
3858           FROM hz_cust_acct_relate rcr
3859           WHERE rcr.status='A' and
3860                 rcr.cust_account_id= p_cr_customer_id
3861             and rcr.bill_to_flag = 'Y'
3862           UNION
3863           SELECT p_cr_customer_id
3864           FROM dual
3865           UNION
3866           SELECT rel.related_cust_account_id
3867           FROM ar_paying_relationships_v rel,
3868                hz_cust_accounts acc
3869           WHERE rel.party_id = acc.party_id
3870             AND acc.cust_account_id = p_cr_customer_id
3871             AND p_cr_date BETWEEN effective_start_date
3872                               AND effective_end_date
3873           );
3874 
3875     END IF;
3876 
3877   ELSE
3878 
3879      IF arp_global.sysparam.pay_unrelated_invoices_flag = 'Y' THEN
3880 	IF nvl(p_called_from,'NONE') NOT IN ('AUTORECAPI2','AUTORECAPI') THEN
3881          /* modified for tca uptake */
3882         SELECT
3883           ot.customer_id ,
3884           ot.cust_trx_type_id ,
3885           ot.trx_due_date ,
3886           ot.invoice_currency_code,
3887           ot.trx_exchange_rate,
3888           ot.trx_date,
3889           ot.trx_gl_date ,
3890           ot.calc_discount_on_lines_flag ,
3891           ot.partial_discount_flag ,
3892           ot.allow_overapplication_flag ,
3893           ot.natural_application_only_flag ,
3894           ot.creation_sign ,
3895           ot.payment_schedule_id ,
3896           greatest(p_cr_gl_date,ot.trx_gl_date,
3897                    decode(pg_profile_appln_gl_date_def,
3898                           'INV_REC_SYS_DT', sysdate, 'INV_REC_DT', ot.trx_gl_date,
3899                            ot.trx_gl_date)) gl_date,
3900           ot.term_id ,
3901           ot.amount_due_original,
3902           ot.amount_line_items_original ,
3903           arp_util.CurrRound(ot.balance_due_curr_unformatted,
3904                              ot.invoice_currency_code) ,
3905           ot.discount_taken_earned,
3906           ot.discount_taken_unearned,
3907 	  ot.amount_line_items_original,
3908 	  ot.amount_line_items_remaining,
3909 	  ot.tax_original,
3910  	  ot.tax_remaining,
3911           ot.freight_original,
3912           ot.freight_remaining,
3913           Null receivables_charges_charged,
3914 	  ot.receivables_charges_remaining,
3915           ot.location
3916         INTO
3917           p_customer_id ,
3918           p_cust_trx_type_id ,
3919           p_trx_due_date ,
3920           p_trx_currency_code,
3921           p_trx_exchange_rate,
3922           p_trx_date,
3923           p_trx_gl_date ,
3924           p_calc_discount_on_lines_flag ,
3925           p_partial_discount_flag ,
3926           p_allow_overappln_flag ,
3927           p_natural_appln_only_flag ,
3928           p_creation_sign ,
3929           l_applied_payment_schedule_id ,
3930           p_gl_date, --this is the application gl_date
3931           p_term_id ,
3932           p_amount_due_original,
3933           p_amount_line_items_original ,
3934           p_amount_due_remaining ,
3935           p_discount_taken_earned,
3936           p_discount_taken_unearned,
3937       	  p_line_items_original,
3938  	  p_line_items_remaining,
3939  	  p_tax_original,
3940 	  p_tax_remaining,
3941           p_freight_original,
3942           p_freight_remaining,
3943           p_rec_charges_charged,
3944           p_rec_charges_remaining,
3945           l_location
3946         FROM
3947           ar_open_trx_v ot
3948         WHERE
3949           ot.customer_trx_id =  p_customer_trx_id and
3950           ot.invoice_currency_code =
3951             DECODE(NVL(pg_profile_enable_cc,'N'),
3952                    'Y',ot.invoice_currency_code,p_cr_currency_code) and
3953           ot.status=decode(p_show_closed_invoices,'Y',ot.status,'OP') and
3954           ot.terms_sequence_number = p_installment;
3955 
3956      ELSE
3957 	    ar_autorec_api.populate_cached_data( l_receipt_info_rec );
3958 
3959 	    p_customer_id                 := l_receipt_info_rec.customer_id;
3960 	    p_cust_trx_type_id            := l_receipt_info_rec.cust_trx_type_id;
3961 	    p_trx_due_date                := l_receipt_info_rec.trx_due_date;
3962 	    p_trx_currency_code           := l_receipt_info_rec.trx_currency_code;
3963 	    p_trx_exchange_rate           := l_receipt_info_rec.trx_exchange_rate;
3964 	    p_trx_date                    := l_receipt_info_rec.trx_date;
3965 	    p_trx_gl_date                 := l_receipt_info_rec.trx_gl_date;
3966 	    p_calc_discount_on_lines_flag := l_receipt_info_rec.calc_discount_on_lines_flag;
3967 	    p_partial_discount_flag       := l_receipt_info_rec.partial_discount_flag;
3968 	    p_allow_overappln_flag        := l_receipt_info_rec.allow_overappln_flag;
3969 	    p_natural_appln_only_flag     := l_receipt_info_rec.natural_appln_only_flag;
3970 	    p_creation_sign               := l_receipt_info_rec.creation_sign;
3971 	    l_applied_payment_schedule_id := l_receipt_info_rec.applied_payment_schedule_id;
3972 	    p_gl_date                     := l_receipt_info_rec.ot_gl_date;
3973 	    p_term_id                     := l_receipt_info_rec.term_id;
3974 	    p_amount_due_original         := l_receipt_info_rec.amount_due_original;
3975 	    p_amount_line_items_original  := l_receipt_info_rec.amount_line_items_original;
3976 	    p_amount_due_remaining        := l_receipt_info_rec.amount_due_remaining;
3977 	    p_discount_taken_earned       := l_receipt_info_rec.discount_taken_earned;
3978 	    p_discount_taken_unearned     := l_receipt_info_rec.discount_taken_unearned;
3979 	    p_line_items_original         := l_receipt_info_rec.line_items_original;
3980 	    p_line_items_remaining        := l_receipt_info_rec.line_items_remaining;
3981 	    p_tax_original                := l_receipt_info_rec.tax_original;
3982 	    p_tax_remaining               := l_receipt_info_rec.tax_remaining;
3983 	    p_freight_original            := l_receipt_info_rec.freight_original;
3984 	    p_freight_remaining           := l_receipt_info_rec.freight_remaining;
3985 	    p_rec_charges_charged         := l_receipt_info_rec.rec_charges_charged;
3986 	    p_rec_charges_remaining       := l_receipt_info_rec.rec_charges_remaining;
3987 	    l_location                    := l_receipt_info_rec.location;
3988 	END IF;
3989      ELSE
3990      --This is the case where pay_unrelated_invoices_flag is 'N'
3991          /* modified for tca uptake */
3992         SELECT
3993           ot.customer_id ,
3994           ot.cust_trx_type_id ,
3995           ot.trx_due_date ,
3996           ot.invoice_currency_code,
3997           ot.trx_exchange_rate,
3998           ot.trx_date,
3999           ot.trx_gl_date ,
4000           ot.calc_discount_on_lines_flag ,
4001           ot.partial_discount_flag ,
4002           ot.allow_overapplication_flag ,
4003           ot.natural_application_only_flag ,
4004           ot.creation_sign ,
4005           ot.payment_schedule_id ,
4006           greatest(p_cr_gl_date,ot.trx_gl_date,
4007                    decode(pg_profile_appln_gl_date_def,
4008                           'INV_REC_SYS_DT', sysdate, 'INV_REC_DT', ot.trx_gl_date,
4009                            ot.trx_gl_date)) gl_date,
4010           ot.term_id ,
4011           ot.amount_due_original,
4012           ot.amount_line_items_original ,
4013           arp_util.CurrRound(ot.balance_due_curr_unformatted,
4014                              ot.invoice_currency_code) ,
4015           ot.discount_taken_earned,
4016           ot.discount_taken_unearned,
4017   	  ot.amount_line_items_original,
4018 	  ot.amount_line_items_remaining,
4019 	  ot.tax_original,
4020  	  ot.tax_remaining,
4021           ot.freight_original,
4022           ot.freight_remaining,
4023           Null receivables_charges_charged,
4024 	  ot.receivables_charges_remaining,
4025           ot.location
4026         INTO
4027           p_customer_id ,
4028           p_cust_trx_type_id ,
4029           p_trx_due_date ,
4030           p_trx_currency_code,
4031           p_trx_exchange_rate,
4032           p_trx_date,
4033           p_trx_gl_date ,
4034           p_calc_discount_on_lines_flag ,
4035           p_partial_discount_flag,
4036           p_allow_overappln_flag,
4037           p_natural_appln_only_flag,
4038           p_creation_sign,
4039           l_applied_payment_schedule_id,
4040           p_gl_date, --this is the defaulted application gl_date
4041           p_term_id,
4042           p_amount_due_original,
4043           p_amount_line_items_original,
4044           p_amount_due_remaining,
4045           p_discount_taken_earned,
4046           p_discount_taken_unearned,
4047       	  p_line_items_original,
4048  	  p_line_items_remaining,
4049  	  p_tax_original,
4050 	  p_tax_remaining,
4051           p_freight_original,
4052           p_freight_remaining,
4053           p_rec_charges_charged,
4054           p_rec_charges_remaining,
4055           l_location
4056         FROM
4057           ar_open_trx_v ot
4058         WHERE
4059           ot.customer_trx_id =  p_customer_trx_id and
4060           ot.invoice_currency_code =
4061             DECODE(NVL(pg_profile_enable_cc,'N'),
4062                    'Y',ot.invoice_currency_code,p_cr_currency_code) and
4063           ot.status=decode(p_show_closed_invoices,'Y',ot.status,'OP') and
4064           ot.terms_sequence_number = p_installment and
4065           --ot.customer_id IN (
4066           exists
4067           -- Fix for bug 12635079
4068           --SELECT rcr.related_cust_account_id
4069           (SELECT /*+ no_unnest */ 'x'
4070           FROM hz_cust_acct_relate rcr
4071           WHERE rcr.status='A' and
4072                 rcr.cust_account_id= p_cr_customer_id
4073             and rcr.bill_to_flag = 'Y'
4074             and  ot.customer_id = rcr.related_cust_account_id
4075           UNION ALL
4076           SELECT /*+ no_unnest */ 'x'
4077           FROM dual
4078           WHERE ot.customer_id = p_cr_customer_id
4079           UNION ALL
4080           SELECT /*+ no_unnest */ 'x'
4081           FROM ar_paying_relationships_v rel,
4082                hz_cust_accounts acc
4083           WHERE rel.party_id = acc.party_id
4084             AND acc.cust_account_id = p_cr_customer_id
4085             AND p_cr_date BETWEEN effective_start_date
4086                               AND effective_end_date
4087             AND OT.CUSTOMER_ID = REL.RELATED_CUST_ACCOUNT_ID
4088           );
4089 
4090      END IF;
4091 
4092   END IF;
4093 
4094  --If the defaulted payment_schedule_id does not match the
4095  --applied_ps_id entered by the user, then raise error.
4096  IF p_applied_payment_schedule_id IS NOT NULL THEN
4097    IF l_applied_payment_schedule_id <>
4098                              p_applied_payment_schedule_id THEN
4099       FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_PS_ID_X_INVALID');
4100       FND_MSG_PUB.Add;
4101       p_return_status := FND_API.G_RET_STS_ERROR ;
4102    END IF;
4103  ELSE
4104       p_applied_payment_schedule_id := l_applied_payment_schedule_id;
4105  END IF;
4106 
4107   --Derive site_use_id from the Location
4108   --this could be a DRAWEE or BILL_TO site_use_id
4109     OPEN get_site_use_id;
4110     FETCH get_site_use_id INTO   p_bill_to_site_use_id;
4111     CLOSE get_site_use_id;
4112 
4113 
4114   --If line number has been specified and you have a customer_trx_id,(defaulted or entered)
4115   --then get the Line information
4116   --If the customer_trx_id enterd by the user was invalid then this section of the code
4117   --will not get executed as the control goes directly to the no_data_found exception
4118 
4119   --we check for the return status so that we are sure that the payment schedule id is valid
4120  IF p_return_status = FND_API.G_RET_STS_SUCCESS AND
4121     p_customer_trx_id IS NOT NULL AND
4122     p_llca_type	  IS NULL	  AND
4123     p_customer_trx_line_id IS NOT NULL THEN
4124 
4125     p_trx_line_amount :=
4126         Get_trx_Line_amount(p_customer_trx_id,
4127                             p_applied_payment_schedule_id,
4128                             p_customer_trx_line_id,
4129                             l_line_amt_return_status
4130                             );
4131      IF l_line_amt_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4132         p_return_status := FND_API.G_RET_STS_ERROR;
4133      END IF;
4134     p_trx_line_amount := arp_util.CurrRound(p_trx_line_amount,
4135 					    p_trx_currency_code);
4136  END IF;
4137      --bug 6660834 check is not required if called from autoreceipts
4138     IF NVL(p_called_from,'NONE')  NOT IN ('AUTORECAPI','AUTORECAPI2') THEN
4139 
4140  --Also check if the payment_schedule of the transaction has already been
4141  --applied once to the current_receipt, if yes then raise error
4142     BEGIN
4143       IF p_customer_trx_line_id IS NULL THEN
4144         select 'Y'
4145         into   l_found
4146         from   ar_receivable_applications rap
4147         where  rap.payment_schedule_id = p_cr_payment_schedule_id
4148         and    rap.applied_payment_schedule_id = p_applied_payment_schedule_id
4149         and    applied_customer_trx_line_id is NULL
4150 	and    rap.display = 'Y'
4151         and    rap.status = 'APP';
4152       ELSE
4153         select 'Y'
4154         into   l_found
4155         from   ar_receivable_applications rap
4156         where  rap.payment_schedule_id = p_cr_payment_schedule_id
4157         and    rap.applied_payment_schedule_id = p_applied_payment_schedule_id
4158         and    rap.applied_customer_trx_line_id = p_customer_trx_line_id
4159         and    rap.display = 'Y'
4160         and    rap.status = 'APP';
4161       END IF;
4162       if l_found = 'Y' then
4163         raise too_many_rows;
4164       end if;
4165 
4166     EXCEPTION
4167       when no_data_found then
4168         null;
4169       when too_many_rows then
4170         FND_MESSAGE.SET_NAME('AR', 'AR_RW_PAID_INVOICE_TWICE' );
4171         FND_MSG_PUB.Add;
4172         p_return_status := FND_API.G_RET_STS_ERROR ;
4173     END;
4174     END IF;
4175 
4176 ELSE --case when p_customer_trx_id is null
4177   --no further validation done in the validation routines for customer_trx_id
4178   FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUST_TRX_ID_NULL');
4179   FND_MSG_PUB.Add;
4180   p_return_status := FND_API.G_RET_STS_ERROR ;
4181 
4182 END IF;
4183 
4184   IF PG_DEBUG in ('Y', 'C') THEN
4185      arp_util.debug('Default_Trx_Info: ' || 'p_applied_payment_schedule_id : '||to_char(p_applied_payment_schedule_id));
4186      arp_util.debug('Default_Trx_Info: ' || 'p_bill_to_site_use_id  : '||to_char(p_bill_to_site_use_id));
4187      arp_util.debug('Default_Trx_Info: ' || 'p_calc_discount_on_lines_flag  : '||p_calc_discount_on_lines_flag);
4188      arp_util.debug('Default_Trx_Info: ' || 'p_partial_discount_flag        : '||p_partial_discount_flag );
4189      arp_util.debug('Default_Trx_Info: ' || 'p_allow_overappln_flag         : '||p_allow_overappln_flag);
4190      arp_util.debug('Default_Trx_Info: ' || 'p_natural_appln_only_flag      : '||p_natural_appln_only_flag);
4191      arp_util.debug('Default_Trx_Info: ' || 'p_creation_sign                : '||p_creation_sign);
4192    arp_util.debug('Default_Trx_Info ()-');
4193 END IF;
4194 EXCEPTION
4195   WHEN no_data_found THEN
4196     IF PG_DEBUG in ('Y', 'C') THEN
4197        arp_util.debug('Default_Trx_Info : No data found ');
4198     END IF;
4199    IF ar_receipt_api_pub.Original_application_info.customer_trx_id IS NOT NULL THEN
4200 
4201     IF ar_receipt_api_pub.Original_application_info.installment IS NOT NULL THEN
4202      p_return_status := FND_API.G_RET_STS_ERROR ;
4203      FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_ID_INST_INVALID');
4204      FND_MSG_PUB.ADD;
4205     ELSE
4206      p_return_status := FND_API.G_RET_STS_ERROR ;
4207      FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUST_TRX_ID_INVALID');
4208      FND_MSG_PUB.ADD;
4209     END IF;
4210 
4211    ELSIF ar_receipt_api_pub.Original_application_info.trx_number IS NOT NULL THEN
4212 
4213     IF ar_receipt_api_pub.Original_application_info.installment IS NOT NULL THEN
4214      p_return_status := FND_API.G_RET_STS_ERROR ;
4215      FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_NUM_INST_INVALID');
4216      FND_MSG_PUB.ADD;
4217     ELSE
4218      p_return_status := FND_API.G_RET_STS_ERROR ;
4219      FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_NUM_INVALID');
4220      FND_MSG_PUB.ADD;
4221     END IF;
4222 
4223    ELSIF ar_receipt_api_pub.Original_application_info.applied_payment_schedule_id IS NOT NULL THEN
4224      p_return_status := FND_API.G_RET_STS_ERROR ;
4225      FND_MESSAGE.SET_NAME('AR','AR_RAPI_APP_PS_ID_INVALID');
4226      FND_MSG_PUB.ADD;
4227    END IF;
4228  WHEN others THEN
4229     IF PG_DEBUG in ('Y', 'C') THEN
4230        arp_util.debug('EXCEPTION: Default_Trx_Info()');
4231     END IF;
4232     raise;
4233 END Default_Trx_Info;
4234 
4235 PROCEDURE Default_Receipt_Info(
4236                  p_cash_receipt_id           IN ar_cash_receipts.cash_receipt_id%TYPE,
4237                  p_cr_gl_date                OUT NOCOPY DATE,
4238                  p_cr_customer_id            OUT NOCOPY ar_cash_receipts.pay_from_customer%TYPE,
4239                  p_cr_amount                 OUT NOCOPY ar_cash_receipts.amount%TYPE,
4240                  p_cr_currency_code          OUT NOCOPY VARCHAR2,
4241                  p_cr_exchange_rate          OUT NOCOPY NUMBER,
4242                  p_cr_cust_site_use_id       OUT NOCOPY ar_cash_receipts.customer_site_use_id%TYPE,
4243                  p_cr_date                   OUT NOCOPY ar_cash_receipts.receipt_date%TYPE,
4244                  p_cr_unapp_amount           OUT NOCOPY NUMBER,
4245                  p_cr_payment_schedule_id    OUT NOCOPY NUMBER,
4246                  p_remittance_bank_account_id OUT NOCOPY NUMBER,
4247                  p_receipt_method_id         OUT NOCOPY  NUMBER,
4248                  p_return_status             OUT NOCOPY VARCHAR2
4249                  )  IS
4250 
4251 BEGIN
4252   p_return_status := FND_API.G_RET_STS_SUCCESS;
4253    IF PG_DEBUG in ('Y', 'C') THEN
4254       arp_util.debug('Default_Receipt_Info ()+');
4255    END IF;
4256   IF p_cash_receipt_id IS NOT NULL THEN
4257    BEGIN
4258     SELECT cr.pay_from_customer,
4259            crh.gl_date,
4260            cr.amount,
4261            cr.customer_site_use_id,
4262            cr.receipt_date,
4263            cr.currency_code,
4264            cr.exchange_rate,
4265            ps.payment_schedule_id,
4266            ba.bank_acct_use_id,
4267            cr.receipt_method_id
4268     INTO   p_cr_customer_id,
4269            p_cr_gl_date,
4270            p_cr_amount,
4271            p_cr_cust_site_use_id,
4272            p_cr_date,
4273            p_cr_currency_code,
4274            p_cr_exchange_rate,
4275            p_cr_payment_schedule_id,
4276            p_remittance_bank_account_id,
4277            p_receipt_method_id
4278     FROM   ar_cash_receipts cr,
4279            ar_cash_receipt_history crh,
4280            ar_payment_schedules ps,
4281            ce_bank_acct_uses ba
4282     WHERE  cr.remit_bank_acct_use_id = ba.bank_acct_use_id and
4283            cr.cash_receipt_id = crh.cash_receipt_id and
4284            crh.first_posted_record_flag = 'Y'     and     /* bug 3333680  */
4285            cr.cash_receipt_id = p_cash_receipt_id and
4286            cr.cash_receipt_id = ps.cash_receipt_id and
4287            crh.status IN ('CONFIRMED','CLEARED', 'REMITTED','APPROVED',
4288                decode(crh.factor_flag,'Y','RISK_ELIMINATED')); /* Risk Eliminated condition added
4289                                                                          for bug 2215047*/
4290     EXCEPTION
4291      WHEN NO_DATA_FOUND THEN
4292       BEGIN
4293        SELECT cr.pay_from_customer,
4294            crh.gl_date,
4295            cr.amount,
4296            cr.customer_site_use_id,
4297            cr.receipt_date,
4298            cr.currency_code,
4299            cr.exchange_rate,
4300            ps.payment_schedule_id,
4301            ba.bank_acct_use_id,
4302            cr.receipt_method_id
4303       INTO   p_cr_customer_id,
4304            p_cr_gl_date,
4305            p_cr_amount,
4306            p_cr_cust_site_use_id,
4307            p_cr_date,
4308            p_cr_currency_code,
4309            p_cr_exchange_rate,
4310            p_cr_payment_schedule_id,
4311            p_remittance_bank_account_id,
4312            p_receipt_method_id
4313       FROM   ar_cash_receipts cr,
4314            ar_cash_receipt_history crh,
4315            ar_payment_schedules ps,
4316            ce_bank_acct_uses ba
4317        WHERE  cr.remit_bank_acct_use_id = ba.bank_acct_use_id and
4318            cr.cash_receipt_id = crh.cash_receipt_id and
4319            crh.first_posted_record_flag = 'N'       and
4320            cr.cash_receipt_id = p_cash_receipt_id   and
4321            cr.cash_receipt_id = ps.cash_receipt_id  and
4322            crh.status  = 'APPROVED';
4323       EXCEPTION
4324       WHEN no_data_found THEN
4325        IF ar_receipt_api_pub.Original_application_info.cash_receipt_id IS NOT NULL THEN
4326          p_return_status := FND_API.G_RET_STS_ERROR ;
4327          FND_MESSAGE.SET_NAME( 'AR','AR_RAPI_CASH_RCPT_ID_INVALID');
4328          FND_MSG_PUB.ADD;
4329        ELSIF  ar_receipt_api_pub.Original_application_info.receipt_number IS NOT NULL THEN
4330          p_return_status := FND_API.G_RET_STS_ERROR ;
4331          FND_MESSAGE.SET_NAME( 'AR','AR_RAPI_RCPT_NUM_INVALID');
4332          FND_MSG_PUB.ADD;
4333        END IF;
4334       WHEN others THEN
4335        IF PG_DEBUG in ('Y', 'C') THEN
4336         arp_util.debug('EXCEPTION: Default_Receipt_Info()');
4337        END IF;
4338        raise;
4339       END;
4340     WHEN others THEN
4341      IF PG_DEBUG in ('Y', 'C') THEN
4342         arp_util.debug('EXCEPTION: Default_Receipt_Info()');
4343      END IF;
4344      raise;
4345     END;
4346 
4347     SELECT SUM(NVL(ra.amount_applied,0))
4348     INTO   p_cr_unapp_amount
4349     FROM   ar_receivable_applications ra
4350     WHERE  ra.status = 'UNAPP'
4351     AND    ra.cash_receipt_id = p_cash_receipt_id;
4352 
4353   ELSE
4354    --if the receipt number is also null raise error here
4355    --otherwise the error would have been raised while defaulting the cash receipt id from the
4356    --receipt number
4357    IF ar_receipt_api_pub.Original_application_info.receipt_number IS NULL THEN
4358     FND_MESSAGE.SET_NAME( 'AR','AR_RAPI_CASH_RCPT_ID_NULL');
4359     FND_MSG_PUB.ADD;
4360     p_return_status := FND_API.G_RET_STS_ERROR ;
4361    END IF;
4362 
4363   END IF;
4364   IF PG_DEBUG in ('Y', 'C') THEN
4365      arp_util.debug('Default_Receipt_Info: ' || ' p_return_status :'||p_return_status);
4366      arp_util.debug('Default_Receipt_Info ()-');
4367   END IF;
4368 EXCEPTION
4369   WHEN no_data_found THEN
4370   IF ar_receipt_api_pub.Original_application_info.cash_receipt_id IS NOT NULL THEN
4371     p_return_status := FND_API.G_RET_STS_ERROR ;
4372     FND_MESSAGE.SET_NAME( 'AR','AR_RAPI_CASH_RCPT_ID_INVALID');
4373     FND_MSG_PUB.ADD;
4374   ELSIF  ar_receipt_api_pub.Original_application_info.receipt_number IS NOT NULL THEN
4375     p_return_status := FND_API.G_RET_STS_ERROR ;
4376     FND_MESSAGE.SET_NAME( 'AR','AR_RAPI_RCPT_NUM_INVALID');
4377     FND_MSG_PUB.ADD;
4378   END IF;
4379   WHEN others THEN
4380     IF PG_DEBUG in ('Y', 'C') THEN
4381        arp_util.debug('EXCEPTION: Default_Receipt_Info()');
4382     END IF;
4383     raise;
4384 
4385 END Default_Receipt_Info;
4386 
4387 PROCEDURE Default_apply_date(p_receipt_date     IN DATE,
4388                              p_trx_date         IN DATE,
4389                              p_apply_date       IN OUT NOCOPY DATE) IS
4390 BEGIN
4391 
4392   --
4393   --The apply_date should be deafaulted to the greatest of the
4394   --sysdate,receipt_date and trx_date.
4395 
4396    /* 7693172 - trunc'd return value to insure that timestamp
4397       is not recorded in RA table.  This could cause problems
4398       with discount calculations */
4399    IF p_apply_date IS NULL THEN
4400       p_apply_date := TRUNC(GREATEST(sysdate,
4401                                GREATEST(NVL(p_receipt_date,sysdate),
4402                                         NVL(p_trx_date,sysdate))));
4403    END IF;
4404 
4405 
4406 END Default_apply_date;
4407 
4408 PROCEDURE Default_customer_trx_id(
4409                           p_customer_trx_id IN OUT NOCOPY NUMBER,
4410                           p_trx_number  IN VARCHAR,
4411                           p_return_status OUT NOCOPY VARCHAR2
4412                            ) IS
4413 BEGIN
4414   IF PG_DEBUG in ('Y', 'C') THEN
4415      arp_util.debug('Default_customer_trx_id ()+');
4416   END IF;
4417     p_return_status := FND_API.G_RET_STS_SUCCESS;
4418    IF p_customer_trx_id IS NULL THEN
4419      IF  p_trx_number IS NOT NULL THEN
4420        BEGIN
4421          SELECT customer_trx_id
4422          INTO   p_customer_trx_id
4423          FROM   ra_customer_trx
4424          WHERE   trx_number = p_trx_number;
4425        EXCEPTION
4426          WHEN no_data_found THEN
4427            FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_NUM_INVALID');
4428            FND_MSG_PUB.Add;
4429            p_return_status := FND_API.G_RET_STS_ERROR ;
4430        END;
4431      END IF;
4432 
4433    ELSE
4434 
4435       IF p_trx_number IS NOT NULL
4436       THEN
4437        --give a warning message to indicate that the trx number
4438        --entered by the user has been ignored.
4439        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
4440        	THEN
4441          FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_NUM_IGN');
4442          FND_MSG_PUB.Add;
4443        END IF;
4444      END IF;
4445    END IF;
4446  IF PG_DEBUG in ('Y', 'C') THEN
4447     arp_util.debug('Default_customer_trx_id ()-');
4448  END IF;
4449 EXCEPTION
4450   WHEN others THEN
4451    IF PG_DEBUG in ('Y', 'C') THEN
4452       arp_util.debug('EXCEPTION: Default_customer_trx_id()', G_MSG_UERROR);
4453    END IF;
4454 END Default_customer_trx_id;
4455 /* Bug 5284890 */
4456 PROCEDURE Default_group_id( p_customer_trx_id IN NUMBER,
4457                             p_group_id        IN VARCHAR2,
4458                             p_llca_type       IN VARCHAR2,
4459                             p_return_status OUT NOCOPY VARCHAR2
4460                            ) IS
4461 l_count         Number;
4462 BEGIN
4463   IF PG_DEBUG in ('Y', 'C') THEN
4464      arp_util.debug('Default_group_id ()+');
4465   END IF;
4466     p_return_status := FND_API.G_RET_STS_SUCCESS;
4467 
4468 	select count(1)
4469 	  into l_count
4470 	from ra_customer_trx_lines line
4471 	where line.customer_Trx_id = p_customer_trx_id
4472 	and line.line_type = 'LINE'
4473 	and line.source_data_key4 = p_group_id
4474 	and rownum = 1;
4475 
4476              If nvl(l_count,0) = 0   AND
4477                 nvl(p_llca_type,'S') = 'G'
4478 	     THEN
4479                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_GROUP_ID_INVALID');
4480                 FND_MSG_PUB.Add;
4481                 p_return_status := FND_API.G_RET_STS_ERROR ;
4482              END IF;
4483 
4484   IF PG_DEBUG in ('Y', 'C') THEN
4485      arp_util.debug('Default_group_id ()+');
4486   END IF;
4487 END ;
4488 /* Bug fix 3435834
4489    New procedure created for defaulting customer_trx_line_id */
4490 PROCEDURE Default_customer_trx_line_id(
4491                           p_customer_trx_id IN OUT NOCOPY NUMBER,
4492                           p_customer_trx_line_id IN OUT NOCOPY NUMBER,
4493                           p_line_number  IN NUMBER,
4494 			  p_llca_type	 IN VARCHAR2,
4495                           p_return_status OUT NOCOPY VARCHAR2
4496                            ) IS
4497 cursor c1 (p_cust_trx_id in number) is
4498 select * from ar_llca_trx_lines_gt
4499 where customer_trx_id = p_cust_trx_id;
4500 l_cust_trx_line_id	Number;
4501 BEGIN
4502   IF PG_DEBUG in ('Y', 'C') THEN
4503      arp_util.debug('Default_customer_trx_line_id ()+');
4504   END IF;
4505     p_return_status := FND_API.G_RET_STS_SUCCESS;
4506 
4507    IF p_customer_trx_line_id IS NOT NULL AND
4508       p_line_number IS NOT NULL
4509    THEN
4510         --give a warning message to indicate that the line number
4511         --entered by the user has been ignored.
4512           IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
4513            THEN
4514              FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_LINE_NUM_IGN');
4515              FND_MSG_PUB.Add;
4516           END IF;
4517    END IF;
4518 
4519  If p_llca_type IS NULL THEN
4520    IF p_customer_trx_id IS NOT NULL THEN
4521      IF p_customer_trx_line_id IS NULL THEN
4522         IF  p_line_number IS NOT NULL THEN
4523            BEGIN
4524              SELECT customer_trx_line_id
4525              INTO   p_customer_trx_line_id
4526              FROM   ra_customer_trx_lines
4527              WHERE  customer_trx_id = p_customer_trx_id
4528                AND   line_number = p_line_number
4529                AND   line_type =   'LINE';
4530            EXCEPTION
4531              WHEN no_data_found THEN
4532                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_LINE_NO_INVALID');
4533                 FND_MSG_PUB.Add;
4534                 p_return_status := FND_API.G_RET_STS_ERROR ;
4535            END;
4536         END IF;
4537     END IF;
4538    ELSE
4539     /* If the customer_trx_id is not provided and customer_trx_line_id is
4540        provided, derive the customer_trx_id */
4541      IF p_customer_trx_line_id IS NOT NULL THEN
4542         BEGIN
4543              SELECT customer_trx_id
4544              INTO   p_customer_trx_id
4545              FROM   ra_customer_trx_lines
4546              WHERE  customer_trx_line_id = p_customer_trx_line_id
4547                AND  line_type =   'LINE';
4548         EXCEPTION
4549              WHEN no_data_found THEN
4550                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_LINE_ID_INVALID');
4551                 FND_MSG_PUB.Add;
4552                 p_return_status := FND_API.G_RET_STS_ERROR ;
4553         END;
4554      END IF;
4555    END IF;
4556  Else  --LLCA
4557 
4558   If p_llca_type not in ('S','L','G')
4559   Then
4560                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_INVALID_LLCA_TYPE');
4561                 FND_MSG_PUB.Add;
4562                 p_return_status := FND_API.G_RET_STS_ERROR ;
4563   END IF;
4564 
4565   If p_llca_type = 'L'
4566   Then
4567 	 For i in C1(p_customer_trx_id)
4568 	 Loop
4569 	  IF i.customer_trx_line_id IS NULL  Then
4570   	    If i.line_number  is not null
4571 	    Then
4572 	     BEGIN
4573 	      SELECT customer_trx_line_id
4574 	      INTO   l_cust_trx_line_id
4575 	      FROM   ra_customer_trx_lines
4576 	      WHERE  customer_trx_id = p_customer_trx_id
4577 	       AND   line_number = i.line_number
4578 	       AND   line_type =   'LINE';
4579 
4580 	      Update ar_llca_trx_lines_gt
4581 	       set    customer_trx_line_id = l_cust_trx_line_id
4582 	       where customer_trx_id = p_customer_trx_id
4583 	       and   line_number     = i.line_number;
4584               EXCEPTION
4585 	        WHEN no_data_found THEN
4586 	 	 ar_receipt_lib_pvt.populate_errors_gt (
4587 		  p_customer_trx_id      => p_customer_trx_id,
4588 		  p_customer_trx_line_id => i.customer_trx_line_id,
4589   	          p_error_message =>
4590 		   arp_standard.fnd_message('AR_RAPI_TRX_LINE_NO_INVALID'),
4591 		  p_invalid_value	 => i.line_number
4592 		  );
4593 		  p_return_status := FND_API.G_RET_STS_ERROR ;
4594 	       END;
4595 	      End If; /* LINE_NUMBER */
4596 	    Else
4597 		BEGIN
4598 	          SELECT customer_trx_line_id
4599 	          INTO   l_cust_trx_line_id
4600 	          FROM   ra_customer_trx_lines
4601 	          WHERE  customer_trx_id = p_customer_trx_id
4602 	            AND  customer_trx_line_id = i.customer_trx_line_id
4603 	            AND  line_type =   'LINE';
4604 	        EXCEPTION
4605 	         WHEN no_data_found THEN
4606 	 	 ar_receipt_lib_pvt.populate_errors_gt (
4607 		  p_customer_trx_id      => p_customer_trx_id,
4608 		  p_customer_trx_line_id => i.customer_trx_line_id,
4609   	          p_error_message =>
4610 		   arp_standard.fnd_message('AR_RAPI_TRX_LINE_ID_INVALID'),
4611  		  p_invalid_value	 =>i.customer_trx_line_id
4612 		  );
4613                       p_return_status := FND_API.G_RET_STS_ERROR ;
4614 		  END;
4615 	     END IF;
4616 
4617 	     If i.line_amount IS NULL  AND
4618 	        i.tax_amount  IS NULL  AND
4619 	        i.amount_applied is NULL AND
4620 		i.amount_applied_from is NULL AND  -- Bug 6931978 - Cross Currency App
4621 	        i.customer_trx_line_id is NOT NULL
4622 	     THEN
4623 	     	  ar_receipt_lib_pvt.populate_errors_gt (
4624 		  p_customer_trx_id      => p_customer_trx_id,
4625 		  p_customer_trx_line_id => i.customer_trx_line_id,
4626   	          p_error_message =>
4627 		   arp_standard.fnd_message('AR_RAPI_LTFC_AMT_NULL'),
4628   		  p_invalid_value	 => NULL
4629 		  );
4630                   p_return_status := FND_API.G_RET_STS_ERROR ;
4631 	     END IF;
4632 	End Loop;
4633 END IF;
4634 END If;
4635  IF PG_DEBUG in ('Y', 'C') THEN
4636     arp_util.debug('Default_customer_trx_line_id ()-');
4637  END IF;
4638 EXCEPTION
4639   WHEN others THEN
4640    IF PG_DEBUG in ('Y', 'C') THEN
4641       arp_util.debug('EXCEPTION: Default_customer_trx_line_id()', G_MSG_UERROR);
4642    END IF;
4643 END Default_customer_trx_line_id;
4644 
4645 
4646 PROCEDURE Default_cash_receipt_id(
4647                          p_cash_receipt_id IN OUT NOCOPY NUMBER,
4648                          p_receipt_number IN VARCHAR2,
4649                          p_return_status   OUT NOCOPY VARCHAR2
4650                          ) IS
4651 BEGIN
4652  IF PG_DEBUG in ('Y', 'C') THEN
4653     arp_util.debug('Default_cash_receipt_id ()+');
4654  END IF;
4655    p_return_status := FND_API.G_RET_STS_SUCCESS;
4656      arp_util.debug('Default_appln_ids:CHECK 2' || 'p_receipt_number             :'||to_char(p_receipt_number));
4657    IF p_cash_receipt_id IS NULL
4658     THEN
4659       BEGIN
4660         SELECT cash_receipt_id
4661         INTO   p_cash_receipt_id
4662         FROM   ar_cash_receipts
4663         WHERE  receipt_number = p_receipt_number;
4664       EXCEPTION
4665         WHEN no_data_found THEN
4666           FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_NUM_INVALID');
4667           FND_MSG_PUB.Add;
4668           p_return_status := FND_API.G_RET_STS_ERROR ;
4669         WHEN too_many_rows THEN
4670           FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_NUM_INVALID');
4671           FND_MSG_PUB.Add;
4672           p_return_status := FND_API.G_RET_STS_ERROR ;
4673       END;
4674 
4675    ELSE
4676      IF p_receipt_number IS NOT NULL
4677       THEN
4678        --give a warning message to indicate that the receipt number
4679        --entered by the user has been ignored.
4680        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
4681        	THEN
4682          FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_NUM_IGN');
4683          FND_MSG_PUB.Add;
4684        END IF;
4685      END IF;
4686    END IF;
4687  IF PG_DEBUG in ('Y', 'C') THEN
4688     arp_util.debug('Default_cash_receipt_id ()-');
4689  END IF;
4690 EXCEPTION
4691  WHEN others THEN
4692    IF PG_DEBUG in ('Y', 'C') THEN
4693       arp_util.debug('EXCEPTION: Default_cash_receipt_id()');
4694    END IF;
4695 END Default_cash_receipt_id;
4696 
4697 PROCEDURE Default_appln_ids(
4698                 p_cash_receipt_id   IN OUT NOCOPY NUMBER,
4699                 p_receipt_number    IN VARCHAR2,
4700                 p_customer_trx_id   IN OUT NOCOPY NUMBER,
4701                 p_trx_number        IN VARCHAR2,
4702                 p_customer_trx_line_id  IN OUT NOCOPY NUMBER, /* Bug fix 3435834 */
4703                 p_line_number       IN NUMBER,
4704                 p_installment       IN OUT NOCOPY NUMBER,
4705                 p_applied_payment_schedule_id   IN NUMBER,
4706 	    	p_llca_type	    IN VARCHAR2,
4707 	    	p_group_id          IN VARCHAR2,   /* Bug 5284890 */
4708                 p_return_status     OUT NOCOPY VARCHAR2 ) IS
4709 CURSOR payment_schedule IS
4710 SELECT customer_trx_id,terms_sequence_number
4711 FROM   ar_payment_schedules
4712 WHERE  payment_schedule_id = p_applied_payment_schedule_id and
4713        payment_schedule_id >0 and
4714        class in ('INV','DM','DEP','CB','CM','BR');
4715  CURSOR install_number IS
4716  SELECT terms_sequence_number
4717  FROM   ar_payment_schedules
4718  WHERE  customer_trx_id = p_customer_trx_id;
4719 l_customer_trx_id  NUMBER;
4720 l_installment      NUMBER;
4721 p_return_status_lines  VARCHAR2(100); /* Bug fix 3435834 */
4722 p_return_status_group  VARCHAR2(100); /* Bug 5284890 */
4723 
4724 BEGIN
4725   IF PG_DEBUG in ('Y', 'C') THEN
4726      arp_util.debug('Default_appln_ids ()+ ');
4727   END IF;
4728   p_return_status := FND_API.G_RET_STS_SUCCESS;
4729 
4730    -- First get a valid value for the customer_trx_id
4731   IF  p_trx_number  IS NOT NULL THEN
4732     Default_customer_trx_id(p_customer_trx_id ,
4733                             p_trx_number ,
4734                             p_return_status);
4735   END IF;
4736 
4737   /* Bug fix 3435834 */
4738   /* Default the customer_trx_line_id  and customer_trx_id*/
4739     Default_Customer_trx_line_id(p_customer_trx_id,
4740                                  p_customer_trx_line_id,
4741                                  p_line_number,
4742 				 p_llca_type,
4743                                  p_return_status_lines);
4744 
4745   /* Group logic will be implement via bug 5440167
4746   IF  Nvl(p_llca_type,'S') = 'G'
4747   THEN
4748         Default_group_id (p_customer_trx_id,
4749                           p_group_id,
4750 		          p_llca_type,
4751                           p_return_status_group);
4752   END If; */
4753 
4754 --If the customer_trx_id id is null, use the entered aplied_payment_schedule_id
4755 --for deriving the customer_trx_id and installment.
4756    IF p_applied_payment_schedule_id IS NOT NULL THEN
4757      OPEN payment_schedule;
4758      FETCH payment_schedule INTO l_customer_trx_id, l_installment;
4759       IF payment_schedule%NOTFOUND  THEN
4760          FND_MESSAGE.SET_NAME('AR','AR_RAPI_APP_PS_ID_INVALID');
4761          FND_MSG_PUB.Add;
4762          p_return_status := FND_API.G_RET_STS_ERROR ;
4763       END IF;
4764      CLOSE payment_schedule;
4765 
4766     IF  p_return_status = FND_API.G_RET_STS_SUCCESS  THEN
4767 
4768       IF nvl(p_customer_trx_id,nvl(l_customer_trx_id,-99)) = nvl(l_customer_trx_id,-99) THEN
4769         p_customer_trx_id := l_customer_trx_id;
4770       END IF;
4771 
4772       IF nvl(p_installment,nvl(l_installment,-99))  =  nvl(l_installment,-99) THEN
4773          p_installment := l_installment;
4774       END IF;
4775 
4776     END IF;
4777    ELSE
4778      --default the installment from the customer_trx_id if not entered
4779      IF p_customer_trx_id IS NOT NULL THEN
4780        IF p_installment IS NULL THEN
4781          BEGIN
4782           SELECT terms_sequence_number
4783           INTO   p_installment
4784           FROM   ar_payment_schedules
4785           WHERE  customer_trx_id = p_customer_trx_id;
4786          EXCEPTION
4787           WHEN no_data_found THEN
4788            FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUST_TRX_ID_INVALID');
4789            FND_MSG_PUB.Add;
4790            p_return_status := FND_API.G_RET_STS_ERROR;
4791           WHEN too_many_rows THEN
4792 	   If p_llca_type is Null
4793 	   THEN
4794 	           FND_MESSAGE.SET_NAME('AR','AR_RAPI_INSTALL_NULL');
4795 	           FND_MSG_PUB.Add;
4796 	   Else
4797    	           FND_MESSAGE.SET_NAME('AR','AR_LL_INSTALL_NOT_ALLOWED');
4798 	           FND_MSG_PUB.Add;
4799 	   End If;
4800 		   p_return_status := FND_API.G_RET_STS_ERROR;
4801          END;
4802        END IF;
4803      END IF;
4804    END IF;
4805 
4806   -- You cannot apply a receipt to the installment transactions at line-level.
4807   If p_applied_payment_schedule_id IS NOT NULL and
4808      p_llca_type is NOT NULL
4809   Then
4810      Begin
4811       select terms_sequence_number into l_installment
4812       from ar_payment_schedules
4813       where customer_trx_id = p_customer_trx_id;
4814      Exception
4815           WHEN no_data_found THEN
4816            FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUST_TRX_ID_INVALID');
4817            FND_MSG_PUB.Add;
4818            p_return_status := FND_API.G_RET_STS_ERROR;
4819           WHEN too_many_rows THEN
4820            FND_MESSAGE.SET_NAME('AR','AR_LL_INSTALL_NOT_ALLOWED');
4821            FND_MSG_PUB.Add;
4822 	   p_return_status := FND_API.G_RET_STS_ERROR;
4823      END;
4824    End If;
4825 
4826    --Derive cash_receipt_id from receipt_number
4827    Default_cash_receipt_id(
4828                        p_cash_receipt_id ,
4829                        p_receipt_number ,
4830                        p_return_status
4831                          );
4832 
4833   /* Bug fix 3435834 */
4834   IF p_return_status_lines = FND_API.G_RET_STS_ERROR OR
4835      p_return_status_group = FND_API.G_RET_STS_ERROR OR
4836      p_return_status = FND_API.G_RET_STS_ERROR THEN
4837      p_return_status := FND_API.G_RET_STS_ERROR;
4838   END IF;
4839 
4840   IF PG_DEBUG in ('Y', 'C') THEN
4841      arp_util.debug('Default_appln_ids: ' || '****Defaulted Value for the application ids****');
4842      arp_util.debug('Default_appln_ids: ' || 'p_cash_receipt_id             :'||to_char(p_cash_receipt_id));
4843      arp_util.debug('Default_appln_ids: ' || 'p_customer_trx_id             :'||to_char(p_customer_trx_id));
4844      arp_util.debug('Default_appln_ids: ' || 'p_installment                 :'||to_char(p_installment));
4845      arp_util.debug('Default_appln_ids: ' || 'p_applied_payment_schedule_id :'||to_char(p_applied_payment_schedule_id));
4846      arp_util.debug('Default_appln_ids ()- ');
4847   END IF;
4848 EXCEPTION
4849  WHEN others THEN
4850    IF PG_DEBUG in ('Y', 'C') THEN
4851       arp_util.debug('EXCEPTION: Default_appln_ids()');
4852    END IF;
4853    raise;
4854 END Default_appln_ids;
4855 
4856 PROCEDURE Default_application_info(
4857               p_cash_receipt_id              IN ar_cash_receipts.cash_receipt_id%TYPE,
4858               p_cr_gl_date                   OUT NOCOPY DATE,
4859               p_cr_date                      OUT NOCOPY DATE,
4860               p_cr_amount                    OUT NOCOPY ar_cash_receipts.amount%TYPE,
4861               p_cr_unapp_amount              OUT NOCOPY NUMBER,
4862               p_cr_currency_code             OUT NOCOPY VARCHAR2,
4863               p_customer_trx_id              IN ra_customer_trx.customer_trx_id%TYPE,
4864               p_installment                  IN OUT NOCOPY NUMBER,
4865               p_show_closed_invoices         IN VARCHAR2,
4866               p_customer_trx_line_id         IN NUMBER,
4867               p_trx_due_date                 OUT NOCOPY DATE,
4868               p_trx_currency_code            OUT NOCOPY VARCHAR2,
4869               p_trx_date                     OUT NOCOPY DATE,
4870               p_trx_gl_date                   OUT NOCOPY DATE,
4871               p_apply_gl_date              IN OUT NOCOPY DATE,
4872               p_calc_discount_on_lines_flag   OUT NOCOPY VARCHAR2,
4873               p_partial_discount_flag         OUT NOCOPY VARCHAR2,
4874               p_allow_overappln_flag          OUT NOCOPY VARCHAR2,
4875               p_natural_appln_only_flag       OUT NOCOPY VARCHAR2,
4876               p_creation_sign                 OUT NOCOPY VARCHAR2,
4877               p_cr_payment_schedule_id        OUT NOCOPY NUMBER,
4878               p_applied_payment_schedule_id  IN OUT NOCOPY NUMBER,
4879               p_term_id                       OUT NOCOPY NUMBER,
4880               p_amount_due_original           OUT NOCOPY NUMBER,
4881               p_amount_due_remaining          OUT NOCOPY NUMBER,
4882               p_trx_line_amount               OUT NOCOPY NUMBER,
4883               p_discount                   IN OUT NOCOPY NUMBER,
4884               p_apply_date                 IN OUT NOCOPY DATE,
4885               p_discount_max_allowed          OUT NOCOPY NUMBER,
4886               p_discount_earned_allowed       OUT NOCOPY NUMBER,
4887               p_discount_earned               OUT NOCOPY NUMBER,
4888               p_discount_unearned             OUT NOCOPY NUMBER,
4889               p_new_amount_due_remaining      OUT NOCOPY NUMBER,
4890               p_remittance_bank_account_id    OUT NOCOPY NUMBER,
4891               p_receipt_method_id             OUT NOCOPY NUMBER,
4892               p_amount_applied             IN OUT NOCOPY NUMBER,
4893               p_amount_applied_from        IN OUT NOCOPY NUMBER,
4894               p_trans_to_receipt_rate      IN OUT NOCOPY NUMBER,
4895 	      p_llca_type		      IN VARCHAR2,
4896 	      p_line_amount		   IN OUT NOCOPY NUMBER,
4897 	      p_tax_amount		   IN OUT NOCOPY NUMBER,
4898 	      p_freight_amount		   IN OUT NOCOPY NUMBER,
4899 	      p_charges_amount		   IN OUT NOCOPY NUMBER,
4900 	      p_line_discount              IN OUT NOCOPY NUMBER,
4901 	      p_tax_discount               IN OUT NOCOPY NUMBER,
4902 	      p_freight_discount           IN OUT NOCOPY NUMBER,
4903 	      p_line_items_original	      OUT NOCOPY NUMBER,
4904 	      p_line_items_remaining	      OUT NOCOPY NUMBER,
4905 	      p_tax_original		      OUT NOCOPY NUMBER,
4906 	      p_tax_remaining		      OUT NOCOPY NUMBER,
4907 	      p_freight_original	      OUT NOCOPY NUMBER,
4908 	      p_freight_remaining	      OUT NOCOPY NUMBER,
4909 	      p_rec_charges_charged	      OUT NOCOPY NUMBER,
4910 	      p_rec_charges_remaining	      OUT NOCOPY NUMBER,
4911               p_called_from                IN  VARCHAR2,
4912               p_return_status              OUT NOCOPY VARCHAR2
4913                ) IS
4914 
4915 l_cr_customer_id   NUMBER;
4916 l_customer_id  NUMBER;
4917 l_acct_amount_due_remaining  NUMBER;
4918 l_trx_exchange_rate NUMBER;
4919 l_cr_exchange_rate  NUMBER;
4920 l_receipt_date    DATE;
4921 l_trx_date        DATE;
4922 l_discount_taken_earned NUMBER;
4923 l_discount_taken_unearned  NUMBER;
4924 l_amount_line_items_original  NUMBER;
4925 l_bill_to_site_use_id  NUMBER;--this is to store the bill_to_site_use_id on trx.
4926 l_cr_cust_site_use_id     NUMBER;--this is to store the site_use_id on the receipt
4927 l_cust_trx_type_id      NUMBER;
4928 l_def_cr_return_status  VARCHAR2(1);
4929 l_def_trx_return_status VARCHAR2(1);
4930 l_def_amt_return_status VARCHAR2(1);
4931 l_def_rate_return_status VARCHAR2(1);
4932 l_def_aaf_return_status VARCHAR2(1);
4933 --local variables used for OUT NOCOPY parameters
4934 l_calc_discount_on_lines_flag   VARCHAR2(1);
4935 l_partial_discount_flag         VARCHAR2(1);
4936 l_allow_overappln_flag          VARCHAR2(1);
4937 l_natural_appln_only_flag       VARCHAR2(1);
4938 l_creation_sign                 VARCHAR2(1);
4939 l_amount_due_original           NUMBER;
4940 l_amount_due_remaining          NUMBER;
4941 l_term_id                       NUMBER;
4942 l_trx_line_amount               NUMBER;
4943 l_cr_unapp_amount               NUMBER;
4944 l_cr_gl_date                    DATE;
4945 l_trx_gl_date                   DATE;
4946 l_applied_payment_schedule_id   NUMBER;
4947 l_cr_date                       DATE;
4948 l_cr_currency_code              VARCHAR2(15);
4949 l_trx_currency_code             VARCHAR2(15);
4950 l_gl_date                       DATE;
4951 --bug 3172587
4952 l_return  BOOLEAN;
4953 l_default_gl_date  DATE;
4954 l_defaulting_rule_used VARCHAR2(100);
4955 l_error_message  VARCHAR2(200);
4956 
4957 l_receipt_info_rec               AR_AUTOREC_API.receipt_info_rec;
4958 
4959 --LLCA
4960 l_line_items_original   NUMBER;
4961 l_line_items_remaining  NUMBER;
4962 l_tax_original		NUMBER;
4963 l_tax_remaining		NUMBER;
4964 l_freight_original	NUMBER;
4965 l_freight_remaining	NUMBER;
4966 l_rec_charges_charged	NUMBER;
4967 l_rec_charges_remaining NUMBER;
4968 l_val_llca_return_status        VARCHAR2(1);
4969 l_val_llca_msg_data	        VARCHAR2(2000);
4970 l_val_llca_msg_count		NUMBER;
4971 BEGIN
4972     IF PG_DEBUG in ('Y', 'C') THEN
4973        arp_util.debug('Default_application_info ()+');
4974        arp_util.debug('Default_application_info: ' || 'p_discount = '||p_discount );
4975     END IF;
4976 
4977    p_return_status := FND_API.G_RET_STS_SUCCESS;
4978    IF NVL(p_called_from,'NONE') IN ('AUTORECAPI','AUTORECAPI2') THEN
4979 
4980     	ar_autorec_api.populate_cached_data( l_receipt_info_rec );
4981 
4982 	l_cr_customer_id              := l_receipt_info_rec.customer_id;
4983 	l_cr_gl_date 		      := l_receipt_info_rec.cr_gl_date;
4984 	p_cr_amount		      := l_receipt_info_rec.cr_amount;
4985 	l_cr_cust_site_use_id 	      := l_receipt_info_rec.cust_site_use_id;
4986 	p_cr_date		      := l_receipt_info_rec.receipt_date;
4987 	l_cr_currency_code            := l_receipt_info_rec.cr_currency_code;
4988 	l_cr_exchange_rate            := l_receipt_info_rec.cr_exchange_rate;
4989 	p_cr_payment_schedule_id      := l_receipt_info_rec.cr_payment_schedule_id;
4990 	p_remittance_bank_account_id  := l_receipt_info_rec.remittance_bank_account_id;
4991 	p_receipt_method_id           := l_receipt_info_rec.receipt_method_id;
4992 
4993         --when the call is from autoreceipts then UNAPP amount is same as
4994 	--that of receipt amount
4995         l_cr_unapp_amount       := p_cr_amount;
4996 	--set the status to true to continue further process
4997 	l_def_cr_return_status  := FND_API.G_RET_STS_SUCCESS;
4998 
4999 	IF PG_DEBUG in ('Y', 'C') THEN
5000 	    arp_util.debug('Setting the values call from  AUTORECAPI ');
5001 	    arp_util.debug('l_cr_gl_date '||l_cr_gl_date);
5002 	    arp_util.debug('l_cr_customer_id '||l_cr_customer_id);
5003 	    arp_util.debug('p_cr_amount '||p_cr_amount);
5004 	    arp_util.debug('l_cr_cust_site_use_id '||l_cr_cust_site_use_id);
5005 	    arp_util.debug('p_cr_date '||p_cr_date);
5006 	    arp_util.debug('l_cr_currency_code '||l_cr_currency_code);
5007 	    arp_util.debug('l_cr_exchange_rate '||l_cr_exchange_rate);
5008 	    arp_util.debug('p_cr_payment_schedule_id '||p_cr_payment_schedule_id);
5009 	    arp_util.debug('p_remittance_bank_account_id '||p_remittance_bank_account_id);
5010 	    arp_util.debug('p_receipt_method_id '||p_receipt_method_id);
5011 	END IF;
5012     ELSE
5013     Default_Receipt_Info(
5014                  p_cash_receipt_id ,
5015                  l_cr_gl_date ,
5016                  l_cr_customer_id ,
5017                  p_cr_amount ,
5018                  l_cr_currency_code,
5019                  l_cr_exchange_rate,
5020                  l_cr_cust_site_use_id ,
5021                  l_cr_date ,
5022                  l_cr_unapp_amount ,
5023                  p_cr_payment_schedule_id ,
5024                  p_remittance_bank_account_id,
5025                  p_receipt_method_id,
5026                  l_def_cr_return_status );
5027       IF PG_DEBUG in ('Y', 'C') THEN
5028          arp_util.debug('Default_application_info: ' || 'Default_Receipt_Info return status = '||l_def_cr_return_status);
5029       END IF;
5030 
5031       /* Bug 4038708 */
5032       IF(l_cr_customer_id is NULL) THEN
5033       FND_MESSAGE.SET_NAME( 'AR','AR_RW_REC_NO_CT');
5034       FND_MSG_PUB.ADD;
5035       p_return_status := FND_API.G_RET_STS_ERROR ;
5036       END IF;
5037    END IF;
5038 
5039       --call the transaction defaulting routine only if the return status of the
5040       --cash defaulting routine was success
5041   IF l_def_cr_return_status = FND_API.G_RET_STS_SUCCESS  THEN
5042     Default_Trx_Info(
5043               p_customer_trx_id ,
5044               p_customer_trx_line_id ,
5045               p_show_closed_invoices ,
5046               l_cr_gl_date ,
5047               l_cr_customer_id ,
5048               l_cr_currency_code ,
5049               p_cr_payment_schedule_id,
5050               l_cr_date,
5051               p_called_from,
5052               l_customer_id ,
5053               l_cust_trx_type_id ,
5054               p_trx_due_date ,
5055               l_trx_currency_code ,
5056               l_trx_exchange_rate ,
5057               l_trx_date ,
5058               l_trx_gl_date ,
5059               l_calc_discount_on_lines_flag ,
5060               l_partial_discount_flag ,
5061               l_allow_overappln_flag ,
5062               l_natural_appln_only_flag ,
5063               l_creation_sign ,
5064               p_applied_payment_schedule_id ,
5065               l_gl_date , --this is the defaulted application gl_date
5066               l_term_id ,
5067               l_amount_due_original ,
5068               l_amount_line_items_original ,
5069               l_amount_due_remaining ,
5070               l_discount_taken_earned ,
5071               l_discount_taken_unearned ,
5072               l_trx_line_amount ,
5073               p_installment ,
5074               l_bill_to_site_use_id ,
5075 	      p_llca_type,
5076       	      l_line_items_original,
5077 	      l_line_items_remaining,
5078 	      l_tax_original,
5079 	      l_tax_remaining,
5080 	      l_freight_original,
5081 	      l_freight_remaining,
5082 	      l_rec_charges_charged,
5083 	      l_rec_charges_remaining,
5084 	      l_def_trx_return_status );
5085 
5086               IF PG_DEBUG in ('Y', 'C') THEN
5087                  arp_util.debug('Default_application_info: ' || 'Default trx info return status = '||l_def_trx_return_status);
5088               END IF;
5089    END IF;
5090 
5091           Default_apply_date(l_cr_date ,
5092                              l_trx_date,
5093                              p_apply_date);
5094 
5095           --If user has not entered the apply_gl_date default it
5096           --to gl_date defaulted by the Default_trx_info()
5097           IF p_apply_gl_date IS NULL THEN
5098            /*apandit : bug 3172587
5099              In case the apply_gl_date is defaulted we need to additionally validate this
5100               defaulted gl_date and in case it is incorrect, then we get the appropriate
5101               gl_date and use it as default.
5102             */
5103             l_return :=
5104               arp_util.validate_and_default_gl_date(
5105                   gl_date                => l_gl_date,
5106                   trx_date               => null,
5107                   validation_date1       => null,
5108                   validation_date2       => null,
5109                   validation_date3       => null,
5110                   default_date1          => l_gl_date,
5111                   default_date2          => null,
5112                   default_date3          => null,
5113                   p_allow_not_open_flag  => 'N',
5114                   p_invoicing_rule_id    => null,
5115                   p_set_of_books_id      => arp_global.set_of_books_id,
5116                   p_application_id       => 222,
5117                   default_gl_date        => l_default_gl_date ,
5118                   defaulting_rule_used   => l_defaulting_rule_used,
5119                   error_message          => l_error_message);
5120 
5121              IF l_return = TRUE  THEN
5122                p_apply_gl_date := l_default_gl_date;
5123              END IF;
5124 
5125           END IF;
5126 
5127        -- Default LLCA
5128        If P_llca_type is NOT NULL
5129        THEN
5130 
5131  	       Default_val_llca_parameters(
5132 	            p_llca_type		  ,
5133                     p_cash_receipt_id     ,
5134                     p_customer_trx_id     ,
5135     		    p_customer_trx_line_id,
5136     		    p_amount_applied	  ,
5137 		    p_line_amount	  ,
5138 		    p_tax_amount          ,
5139 		    p_freight_amount      ,
5140 		    p_charges_amount      ,
5141 		    p_line_discount       ,
5142 		    p_tax_discount        ,
5143 		    p_freight_discount    ,
5144                     p_discount            ,
5145                     l_cr_currency_code    ,
5146                     l_trx_currency_code   ,
5147 		    l_cr_date,              -- Bug 6931978 Cross Currency App
5148 		    l_creation_sign,
5149                     l_natural_appln_only_flag,
5150                     l_val_llca_return_status ,
5151                     l_val_llca_msg_data	,
5152                     l_val_llca_msg_count
5153 			);
5154 
5155 	END IF;
5156 
5157        --arp_util.debug('p_apply_gl_date '||p_apply_gl_date);
5158           Default_disc_and_amt_applied(
5159                              l_customer_id ,
5160                              l_bill_to_site_use_id ,
5161                              p_applied_payment_schedule_id ,
5162                              p_amount_applied  ,
5163                              p_discount ,
5164                              l_term_id  ,
5165                              p_installment ,
5166                              l_trx_date,
5167                              l_cr_date ,
5168                              l_cr_currency_code ,
5169                              l_trx_currency_code ,
5170                              l_cr_exchange_rate ,
5171                              l_trx_exchange_rate ,--corresponds to app_folder.exchange_rate
5172                              p_apply_date ,
5173                              l_amount_due_original ,
5174                              l_amount_due_remaining ,
5175                              l_cr_unapp_amount ,
5176                              l_allow_overappln_flag ,
5177                              l_calc_discount_on_lines_flag ,
5178                              l_partial_discount_flag ,
5179                              l_amount_line_items_original ,
5180                              l_discount_taken_unearned ,
5181                              l_discount_taken_earned ,
5182                              p_customer_trx_line_id ,
5183                              l_trx_line_amount ,
5184 			     p_llca_type,
5185                              p_discount_max_allowed ,
5186                              p_discount_earned_allowed ,
5187                              p_discount_earned  ,
5188                              p_discount_unearned ,
5189                              p_new_amount_due_remaining ,
5190                              l_def_amt_return_status);
5191 
5192 
5193   IF PG_DEBUG in ('Y', 'C') THEN
5194      arp_util.debug('Default_application_info: ' || 'l_calc_discount_on_lines_flag  : '||l_calc_discount_on_lines_flag);
5195      arp_util.debug('Default_application_info: ' || 'l_partial_discount_flag        : '||l_partial_discount_flag );
5196      arp_util.debug('Default_application_info: ' || 'l_allow_overappln_flag         : '||l_allow_overappln_flag);
5197      arp_util.debug('Default_application_info: ' || 'l_natural_appln_only_flag      : '||l_natural_appln_only_flag);
5198      arp_util.debug('Default_application_info: ' || 'l_creation_sign                : '||l_creation_sign);
5199                 arp_util.debug('Default_application_info: ' || 'Default amount return status :'||l_def_amt_return_status );
5200              END IF;
5201 
5202               Default_trans_to_receipt_rate(
5203                              l_trx_currency_code ,
5204                              l_cr_currency_code ,
5205                              l_receipt_date ,
5206                              p_trans_to_receipt_rate ,
5207                              p_amount_applied,
5208                              p_amount_applied_from,
5209                              l_cr_date,
5210                              l_def_rate_return_status
5211                                       );
5212             IF PG_DEBUG in ('Y', 'C') THEN
5213                arp_util.debug('Default_application_info: ' || 'Default trans_to_receipt_rate status :'||l_def_rate_return_status);
5214             END IF;
5215              Default_amount_applied_from(
5216                              p_amount_applied ,
5217                              l_trx_currency_code ,
5218                              p_trans_to_receipt_rate ,
5219                              l_cr_currency_code  ,
5220                              p_amount_applied_from ,
5221                              l_def_aaf_return_status,
5222 				     p_cash_receipt_id  -- Added for Bug 13595334
5223                               );
5224              IF PG_DEBUG in ('Y', 'C') THEN
5225                 arp_util.debug('Default_application_info: ' || 'Default amount applied from return status :'||l_def_aaf_return_status);
5226              END IF;
5227 
5228 
5229      --populate the parameters with the values in the local variables
5230 
5231         p_term_id := l_term_id;
5232         p_calc_discount_on_lines_flag   := l_calc_discount_on_lines_flag;
5233         p_partial_discount_flag         := l_partial_discount_flag;
5234         p_allow_overappln_flag          := l_allow_overappln_flag;
5235         p_natural_appln_only_flag       := l_natural_appln_only_flag;
5236         p_creation_sign                 := l_creation_sign;
5237         p_cr_unapp_amount               := l_cr_unapp_amount;
5238         p_amount_due_original           := l_amount_due_original;
5239         p_amount_due_remaining          := l_amount_due_remaining;
5240         p_trx_line_amount               := l_trx_line_amount;
5241         p_cr_currency_code              := l_cr_currency_code;
5242         p_trx_currency_code             := l_trx_currency_code;
5243         p_cr_gl_date                    := l_cr_gl_date;
5244         p_trx_gl_date                   := l_trx_gl_date;
5245         p_cr_date := l_cr_date;
5246         p_trx_date  := l_trx_date;
5247 
5248 	-- llca
5249 	p_line_items_original		:= l_line_items_original;
5250 	p_line_items_remaining		:= l_line_items_remaining;
5251 	p_tax_original			:= l_tax_original;
5252 	p_tax_remaining			:= l_tax_remaining;
5253 	p_freight_original		:= l_freight_original;
5254 	p_freight_remaining		:= l_freight_remaining;
5255 	p_rec_charges_charged		:= l_rec_charges_charged;
5256 	p_rec_charges_remaining		:= l_rec_charges_remaining;
5257 
5258         IF l_def_cr_return_status =  FND_API.G_RET_STS_ERROR OR
5259            l_def_trx_return_status =  FND_API.G_RET_STS_ERROR OR
5260            l_def_rate_return_status = FND_API.G_RET_STS_ERROR OR
5261 	   l_val_llca_return_status = FND_API.G_RET_STS_ERROR
5262          THEN
5263            p_return_status := FND_API.G_RET_STS_ERROR;
5264         END IF;
5265 
5266            IF PG_DEBUG in ('Y', 'C') THEN
5267               arp_util.debug('Default_application_info: ' || '*****************Application Defaults***************');
5268               arp_util.debug('Default_application_info: ' || 'p_cash_receipt_id             : '||to_char(p_cash_receipt_id));
5269               arp_util.debug('Default_application_info: ' || 'p_cr_gl_date                  : '||to_char(p_cr_gl_date,'DD-MON-YYYY'));
5270               arp_util.debug('Default_application_info: ' || 'p_cr_date                     : '||to_char(p_cr_date,'DD-MON-YYYY'));
5271               arp_util.debug('Default_application_info: ' || 'p_cr_amount                   : '||to_char(p_cr_amount));
5272               arp_util.debug('Default_application_info: ' || 'p_cr_unapp_amount             : '||to_char(p_cr_unapp_amount));
5273               arp_util.debug('Default_application_info: ' || 'p_cr_currency_code            : '||p_cr_currency_code);
5274               arp_util.debug('Default_application_info: ' || 'p_customer_trx_id             : '||to_char(p_customer_trx_id));
5275               arp_util.debug('Default_application_info: ' || 'p_installment                 : '||to_char(p_installment));
5276               arp_util.debug('Default_application_info: ' || 'p_show_closed_invoices        : '||p_show_closed_invoices);
5277               arp_util.debug('Default_application_info: ' || 'p_customer_trx_line_id        : '||to_char(p_customer_trx_line_id));
5278               arp_util.debug('Default_application_info: ' || 'p_trx_due_date                : '||to_char(p_trx_due_date,'DD-MON-YYYY'));
5279               arp_util.debug('Default_application_info: ' || 'p_trx_currency_code           : '||p_trx_currency_code);
5280               arp_util.debug('Default_application_info: ' || 'p_trx_gl_date                 : '||to_char(p_trx_due_date,'DD-MON-YYYY'));
5281               arp_util.debug('Default_application_info: ' || 'p_apply_gl_date               : '||to_char(p_apply_gl_date,'DD-MON-YYYY'));
5282               arp_util.debug('Default_application_info: ' || 'p_calc_discount_on_lines_flag : '||p_calc_discount_on_lines_flag);
5283               arp_util.debug('Default_application_info: ' || 'p_partial_discount_flag       : '||p_partial_discount_flag);
5284               arp_util.debug('Default_application_info: ' || 'p_allow_overappln_flag        : '||p_allow_overappln_flag);
5285               arp_util.debug('Default_application_info: ' || 'p_natural_appln_only_flag     : '||p_natural_appln_only_flag);
5286               arp_util.debug('Default_application_info: ' || 'p_creation_sign               : '||p_creation_sign);
5287               arp_util.debug('Default_application_info: ' || 'p_cr_payment_schedule_id      : '||to_char(p_cr_payment_schedule_id));
5288               arp_util.debug('Default_application_info: ' || 'p_applied_payment_schedule_id : '||to_char(p_applied_payment_schedule_id));
5289               arp_util.debug('Default_application_info: ' || 'p_term_id                     : '||to_char(p_term_id));
5290               arp_util.debug('Default_application_info: ' || 'p_amount_due_original         : '||to_char(p_amount_due_original));
5291               arp_util.debug('Default_application_info: ' || 'p_amount_due_remaining        : '||to_char(p_amount_due_remaining));
5292               arp_util.debug('Default_application_info: ' || 'p_trx_line_amount             : '||to_char(p_trx_line_amount));
5293               arp_util.debug('Default_application_info: ' || 'p_discount                    : '||to_char(p_discount));
5294               arp_util.debug('Default_application_info: ' || 'p_apply_date                  : '||to_char(p_apply_date,'DD-MON-YYYY'));
5295               arp_util.debug('Default_application_info: ' || 'p_discount_max_allowed        : '||to_char(p_discount_max_allowed));
5296               arp_util.debug('Default_application_info: ' || 'p_discount_earned_allowed     : '||to_char(p_discount_earned_allowed));
5297               arp_util.debug('Default_application_info: ' || 'p_discount_earned             : '||to_char(p_discount_earned));
5298               arp_util.debug('Default_application_info: ' || 'p_discount_unearned           : '||to_char(p_discount_unearned));
5299               arp_util.debug('Default_application_info: ' || 'p_new_amount_due_remaining    : '||to_char(p_new_amount_due_remaining));
5300               arp_util.debug('Default_application_info: ' || 'p_amount_applied              : '||to_char(p_amount_applied));
5301               arp_util.debug('Default_application_info: ' || 'p_amount_applied_from         : '||to_char(p_amount_applied_from));
5302               arp_util.debug('Default_application_info: ' || 'p_trans_to_receipt_rate       : '||to_char(p_trans_to_receipt_rate));
5303               arp_util.debug('Default_application_info: ' || '**********************************************************');
5304        arp_util.debug('Default_application_info ()-');
5305     END IF;
5306 END Default_application_info;
5307 
5308 FUNCTION Get_trx_ps_id(p_customer_trx_id IN OUT NOCOPY NUMBER,
5309                        p_installment     IN NUMBER,
5310                        p_called_from     IN VARCHAR2,
5311                        p_return_status   OUT NOCOPY VARCHAR2
5312                        ) RETURN NUMBER IS
5313 l_trx_ps_id  NUMBER;
5314 BEGIN
5315   IF PG_DEBUG in ('Y', 'C') THEN
5316      arp_util.debug('Get_trx_ps_id ()+');
5317   END IF;
5318   p_return_status := FND_API.G_RET_STS_SUCCESS;
5319 
5320   IF p_called_from IN ('BR_REMITTED',
5321                       'BR_FACTORED_WITH_RECOURSE',
5322                       'BR_FACTORED_WITHOUT_RECOURSE') THEN
5323   --called from the BR Remittance program
5324       IF p_installment IS NOT NULL THEN
5325         BEGIN
5326          SELECT ps.payment_schedule_id
5327          INTO   l_trx_ps_id
5328          FROM   ra_customer_trx ct,
5329                 ar_payment_schedules ps
5330          WHERE  ct.customer_trx_id = p_customer_trx_id
5331            AND  ct.customer_trx_id = ps.customer_trx_id
5332            AND  ps.class  IN ('CB','CM','DEP','DM','INV','BR')
5333            AND  ps.terms_sequence_number = p_installment
5334              ;
5335          EXCEPTION
5336           WHEN no_data_found THEN
5337 
5338             IF ar_receipt_api_pub.Original_unapp_info.customer_trx_id IS NOT NULL THEN
5339               FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_ID_INST_INVALID');
5340               FND_MSG_PUB.Add;
5341               p_return_status := FND_API.G_RET_STS_ERROR;
5342             ELSIF ar_receipt_api_pub.Original_unapp_info.trx_number IS NOT NULL THEN
5343               FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_NUM_INST_INVALID');
5344               FND_MSG_PUB.Add;
5345               p_return_status := FND_API.G_RET_STS_ERROR;
5346             END IF;
5347 
5348          END;
5349 
5350       ELSE
5351     --if the user has not entered the installment then if the transaction
5352     --has only one installment, get the ps_id for that installment
5353          BEGIN
5354            SELECT ps.payment_schedule_id
5355            INTO   l_trx_ps_id
5356            FROM   ra_customer_trx ct,
5357                   ar_payment_schedules ps
5358            WHERE  ct.customer_trx_id = p_customer_trx_id
5359              AND  ct.customer_trx_id = ps.customer_trx_id
5360              AND  ps.class  IN ('CB','CM','DEP','DM','INV','BR')
5361                   ;
5362          EXCEPTION
5363            WHEN no_data_found THEN
5364              FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUST_TRX_ID_INVALID');
5365              FND_MSG_PUB.Add;
5366              p_return_status := FND_API.G_RET_STS_ERROR;
5367            WHEN too_many_rows THEN
5368              FND_MESSAGE.SET_NAME('AR','AR_RAPI_INSTALL_NULL');
5369              FND_MSG_PUB.Add;
5370              p_return_status := FND_API.G_RET_STS_ERROR;
5371          END;
5372 
5373       END IF;
5374 
5375   ELSE --not called from the BR Remittance program
5376 
5377       IF p_installment IS NOT NULL THEN
5378         BEGIN
5379          SELECT ps.payment_schedule_id
5380          INTO   l_trx_ps_id
5381          FROM   ra_customer_trx ct,
5382                 ar_payment_schedules ps
5383          WHERE  ct.customer_trx_id = p_customer_trx_id
5384            AND  ct.customer_trx_id = ps.customer_trx_id
5385            AND  ps.class  IN ('CB','CM','DEP','DM','INV','BR')
5386            AND  ps.terms_sequence_number = p_installment
5387            --these two conditions are to ensure that the trx(bill) is not in remit process
5388            AND  ps.reserved_type IS NULL
5389            AND  ps.reserved_value IS NULL;
5390          EXCEPTION
5391           WHEN no_data_found THEN
5392 
5393             IF ar_receipt_api_pub.Original_unapp_info.customer_trx_id IS NOT NULL THEN
5394               FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_ID_INST_INVALID');
5395               FND_MSG_PUB.Add;
5396               p_return_status := FND_API.G_RET_STS_ERROR;
5397             ELSIF ar_receipt_api_pub.Original_unapp_info.trx_number IS NOT NULL THEN
5398               FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_NUM_INST_INVALID');
5399               FND_MSG_PUB.Add;
5400               p_return_status := FND_API.G_RET_STS_ERROR;
5401             END IF;
5402          END;
5403 
5404       ELSE
5405     --if the user has not entered the installment then if the transaction
5406     --has only one installment, get the ps_id for that installment
5407          BEGIN
5408            SELECT ps.payment_schedule_id
5409            INTO   l_trx_ps_id
5410            FROM   ra_customer_trx ct,
5411                   ar_payment_schedules ps
5412            WHERE  ct.customer_trx_id = p_customer_trx_id
5413              AND  ct.customer_trx_id = ps.customer_trx_id
5414              AND  ps.class  IN ('CB','CM','DEP','DM','INV','BR')
5415              --these two conditions are to ensure that the trx(bill) is not in remit process
5416              AND  ps.reserved_type IS NULL
5417              AND  ps.reserved_value IS NULL
5418                   ;
5419          EXCEPTION
5420            WHEN no_data_found THEN
5421              FND_MESSAGE.SET_NAME('AR','AR_RAPI_CUST_TRX_ID_INVALID');
5422              FND_MSG_PUB.Add;
5423              p_return_status := FND_API.G_RET_STS_ERROR;
5424            WHEN too_many_rows THEN
5425              FND_MESSAGE.SET_NAME('AR','AR_RAPI_INSTALL_NULL');
5426              FND_MSG_PUB.Add;
5427              p_return_status := FND_API.G_RET_STS_ERROR;
5428          END;
5429 
5430       END IF;
5431 
5432   END IF;
5433 
5434    RETURN(l_trx_ps_id);
5435  IF PG_DEBUG in ('Y', 'C') THEN
5436     arp_util.debug('Get_trx_ps_id ()-');
5437  END IF;
5438 EXCEPTION
5439  WHEN others THEN
5440    IF PG_DEBUG in ('Y', 'C') THEN
5441       arp_util.debug('EXCEPTION: Get_trx_ps_id()');
5442    END IF;
5443    raise;
5444 END Get_trx_ps_id;
5445 
5446 PROCEDURE Derive_receivable_appln_id(
5447                            p_cash_receipt_id             IN NUMBER,
5448                            p_applied_payment_schedule_id IN NUMBER,
5449                            p_apply_gl_date               OUT NOCOPY DATE,
5450                            p_receivable_application_id   OUT NOCOPY NUMBER,
5451 			   p_customer_trx_id		 OUT NOCOPY NUMBER,
5452                            p_return_status               OUT NOCOPY VARCHAR2
5453                            )  IS
5454 
5455 BEGIN
5456   IF PG_DEBUG in ('Y', 'C') THEN
5457      arp_util.debug('Derive_receivable_appln_id ()+');
5458   END IF;
5459   p_return_status := FND_API.G_RET_STS_SUCCESS;
5460   IF p_cash_receipt_id IS NOT NULL AND
5461      p_applied_payment_schedule_id IS NOT NULL
5462    THEN
5463       SELECT receivable_application_id, gl_date
5464 	     ,applied_customer_trx_id
5465       INTO   p_receivable_application_id, p_apply_gl_date
5466              ,p_customer_trx_id
5467       FROM   ar_receivable_applications ra
5468       WHERE  ra.cash_receipt_id = p_cash_receipt_id
5469         AND  ra.applied_payment_schedule_id = p_applied_payment_schedule_id
5470         AND  ra.display = 'Y'
5471         AND  ra.status = 'APP'
5472         AND  ra.application_type = 'CASH';
5473 
5474    END IF;
5475   IF PG_DEBUG in ('Y', 'C') THEN
5476      arp_util.debug('Derive_receivable_appln_id ()+');
5477   END IF;
5478 EXCEPTION
5479  WHEN no_data_found THEN
5480   FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_NOT_APP_TO_INV');
5481   FND_MSG_PUB.Add;
5482   p_return_status := FND_API.G_RET_STS_ERROR;
5483  WHEN others THEN
5484    IF PG_DEBUG in ('Y', 'C') THEN
5485       arp_util.debug('Derive_receivable_appln_id: ' || 'EXCEPTION: Get_receivable_application_id()');
5486    END IF;
5487    raise;
5488 END Derive_receivable_appln_id;
5489 
5490 PROCEDURE Get_payment_schedule_info(
5491                                  p_ps_id      IN NUMBER,
5492                                  p_customer_trx_id OUT NOCOPY VARCHAR2,
5493                                  p_installment     OUT NOCOPY NUMBER,
5494                                  p_called_from     IN VARCHAR2,
5495                                  p_return_status   OUT NOCOPY VARCHAR2 ) IS
5496 CURSOR payment_schedule IS
5497  SELECT ps.customer_trx_id, ps.terms_sequence_number
5498  FROM   ar_payment_schedules ps
5499  WHERE  ps.payment_schedule_id =  p_ps_id
5500    AND  ps.class  IN ('CB','CM','DEP','DM','INV','BR')
5501    --these two conditions are to ensure that the trx(bill) is not in remit process
5502    AND  ps.reserved_type  is null
5503    AND  ps.reserved_value  is null;
5504 
5505 CURSOR payment_schedule_br_remit IS
5506  SELECT ps.customer_trx_id, ps.terms_sequence_number
5507  FROM   ar_payment_schedules ps
5508  WHERE  ps.payment_schedule_id =  p_ps_id
5509    AND  ps.class  IN ('CB','CM','DEP','DM','INV','BR');
5510 
5511 BEGIN
5512   IF PG_DEBUG in ('Y', 'C') THEN
5513      arp_util.debug('Get_payment_schedule_info  ()+');
5514   END IF;
5515   p_return_status := FND_API.G_RET_STS_SUCCESS;
5516 
5517  IF p_called_from IN ('BR_REMITTED',
5518                       'BR_FACTORED_WITH_RECOURSE',
5519                       'BR_FACTORED_WITHOUT_RECOURSE') THEN
5520   OPEN payment_schedule_br_remit;
5521 
5522   FETCH payment_schedule_br_remit INTO   p_customer_trx_id, p_installment;
5523    IF payment_schedule_br_remit%NOTFOUND THEN
5524      --raise error message
5525      FND_MESSAGE.SET_NAME('AR','AR_RAPI_APP_PS_ID_INVALID');
5526      FND_MSG_PUB.Add;
5527      p_return_status := FND_API.G_RET_STS_ERROR;
5528    END IF;
5529   CLOSE payment_schedule_br_remit;
5530 
5531  ELSE
5532 
5533   OPEN payment_schedule;
5534   FETCH payment_schedule INTO   p_customer_trx_id, p_installment;
5535    IF payment_schedule%NOTFOUND THEN
5536      --raise error message
5537      FND_MESSAGE.SET_NAME('AR','AR_RAPI_APP_PS_ID_INVALID');
5538      FND_MSG_PUB.Add;
5539      p_return_status := FND_API.G_RET_STS_ERROR;
5540    END IF;
5541   CLOSE payment_schedule;
5542 
5543  END IF;
5544 
5545   IF PG_DEBUG in ('Y', 'C') THEN
5546      arp_util.debug('Get_payment_schedule_info  ()-');
5547   END IF;
5548 END Get_payment_schedule_info;
5549 
5550 PROCEDURE Get_receivable_appln_info(
5551                                    p_ra_id           IN NUMBER,
5552                                    p_ra_app_ps_id    OUT NOCOPY NUMBER,
5553                                    p_cash_receipt_id OUT NOCOPY NUMBER,
5554                                    p_apply_gl_date   OUT NOCOPY DATE, /* Bug fix 3451241 */
5555 				   p_customer_trx_id OUT NOCOPY NUMBER,  /* LLCA */
5556                                    p_called_from     IN VARCHAR2,
5557                                    p_return_status   OUT NOCOPY VARCHAR2
5558                                        ) IS
5559 CURSOR rec_apppln IS
5560 SELECT ra.cash_receipt_id, ra.applied_payment_schedule_id, ra.gl_date /* Bug fix 3451241 */
5561        , ra.applied_customer_trx_id
5562 FROM   ar_receivable_applications ra,
5563        ar_payment_schedules ps
5564 WHERE  ra.applied_payment_schedule_id = ps.payment_schedule_id
5565   AND  ra.receivable_application_id = p_ra_id
5566   AND  ra.display = 'Y'
5567   AND  ra.status = 'APP'
5568   AND  ps.reserved_value IS NULL
5569   AND  ps.reserved_type IS NULL;
5570 
5571 CURSOR rec_apppln_br_remit IS
5572 SELECT ra.cash_receipt_id, ra.applied_payment_schedule_id, ra.gl_date /* Bug fix 3451241 */
5573       , ra.applied_customer_trx_id
5574 FROM   ar_receivable_applications ra
5575 WHERE  ra.receivable_application_id = p_ra_id
5576   AND  ra.display = 'Y'
5577   AND  ra.status = 'APP';
5578 
5579 BEGIN
5580   IF PG_DEBUG in ('Y', 'C') THEN
5581      arp_util.debug('Get_receivable_appln_info ()+');
5582   END IF;
5583 
5584   p_return_status := FND_API.G_RET_STS_SUCCESS;
5585 
5586   IF p_called_from IN ('BR_REMITTED',
5587                       'BR_FACTORED_WITH_RECOURSE',
5588                       'BR_FACTORED_WITHOUT_RECOURSE') THEN
5589      OPEN rec_apppln_br_remit;
5590      FETCH rec_apppln_br_remit INTO p_cash_receipt_id, p_ra_app_ps_id, p_apply_gl_date, p_customer_trx_id; /* Bug fix 3451241 */
5591        IF rec_apppln_br_remit%NOTFOUND  THEN
5592          FND_MESSAGE.SET_NAME('AR','AR_RAPI_REC_APP_ID_INVALID');
5593          FND_MSG_PUB.Add;
5594          p_return_status := FND_API.G_RET_STS_ERROR;
5595        END IF;
5596      CLOSE rec_apppln_br_remit;
5597 
5598   ELSE
5599 
5600      OPEN rec_apppln;
5601      FETCH rec_apppln INTO p_cash_receipt_id, p_ra_app_ps_id, p_apply_gl_date,p_customer_trx_id; /* Bug fix 3451241 */
5602        IF rec_apppln%NOTFOUND  THEN
5603          FND_MESSAGE.SET_NAME('AR','AR_RAPI_REC_APP_ID_INVALID');
5604          FND_MSG_PUB.Add;
5605          p_return_status := FND_API.G_RET_STS_ERROR;
5606        END IF;
5607      CLOSE rec_apppln;
5608 
5609    END IF;
5610 
5611   IF PG_DEBUG in ('Y', 'C') THEN
5612      arp_util.debug('Get_receivable_appln_info ()-');
5613   END IF;
5614 END Get_receivable_appln_info;
5615 
5616 PROCEDURE Derive_unapp_ids(
5617                    p_trx_number                   IN VARCHAR2,
5618                    p_customer_trx_id              IN OUT NOCOPY NUMBER,
5619                    p_installment                  IN NUMBER,
5620                    p_applied_payment_schedule_id  IN OUT NOCOPY NUMBER,
5621                    p_receipt_number               IN VARCHAR2,
5622                    p_cash_receipt_id              IN OUT NOCOPY NUMBER,
5623                    p_receivable_application_id    IN OUT NOCOPY NUMBER,
5624                    p_called_from                  IN VARCHAR2,
5625                    p_apply_gl_date                OUT NOCOPY DATE,
5626                    p_return_status                OUT NOCOPY VARCHAR2
5627                    ) IS
5628 l_customer_trx_id             NUMBER;
5629 l_ra_customer_trx_id          NUMBER;
5630 l_installment                 NUMBER(15);
5631 l_ra_app_ps_id                NUMBER;
5632 l_applied_payment_schedule_id NUMBER;
5633 l_receivable_application_id   NUMBER;
5634 l_return_status               VARCHAR2(1);
5635 l_cash_receipt_id             NUMBER(15);
5636 l_cust_trx_return_status      VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
5637 l_ps_id_return_status         VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
5638 l_ra_return_status            VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
5639 l_cr_return_status            VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
5640 BEGIN
5641   IF PG_DEBUG in ('Y', 'C') THEN
5642      arp_util.debug('Derive_unapp_ids ()+');
5643   END IF;
5644   p_return_status := FND_API.G_RET_STS_SUCCESS ;
5645 
5646   -- First get a valid value for the customer_trx_id
5647   IF p_trx_number IS NOT NULL THEN
5648     Default_customer_trx_id(p_customer_trx_id ,
5649                             p_trx_number ,
5650                             l_cust_trx_return_status);
5651   END IF;
5652 
5653   --if error is raised in deriving the customer_trx_id from the trx_number,
5654   --do not process the applied_payment_schedule_id any further.
5655  IF l_cust_trx_return_status= FND_API.G_RET_STS_SUCCESS THEN
5656 
5657     IF p_applied_payment_schedule_id IS NOT NULL THEN
5658        Get_payment_schedule_info( p_applied_payment_schedule_id,
5659                                   l_customer_trx_id,
5660                                   l_installment,
5661                                   p_called_from,
5662                                   l_ps_id_return_status
5663                                   );
5664       IF l_cust_trx_return_status= FND_API.G_RET_STS_SUCCESS  AND
5665          l_ps_id_return_status = FND_API.G_RET_STS_SUCCESS
5666        THEN
5667        --Compare the two customer_trx_ids
5668        IF (nvl(p_customer_trx_id,l_customer_trx_id) <> l_customer_trx_id OR
5669           nvl(p_installment,l_installment)  <>  l_installment) THEN
5670            FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_PS_ID_X_INVALID');
5671            FND_MSG_PUB.Add;
5672            p_return_status := FND_API.G_RET_STS_ERROR;
5673        ELSE
5674            p_customer_trx_id := l_customer_trx_id;
5675        END IF;
5676       END IF;
5677 
5678     ELSE  --the user has not entered the applied_ps_id
5679 
5680      IF p_customer_trx_id IS NOT NULL THEN
5681         l_applied_payment_schedule_id :=
5682                       Get_trx_ps_id(p_customer_trx_id,
5683                                     p_installment,
5684                                     p_called_from,
5685                                     l_ps_id_return_status);
5686         p_applied_payment_schedule_id
5687                           :=l_applied_payment_schedule_id;
5688      END IF;
5689 
5690     END IF;
5691  END IF;
5692 
5693    --derive the cash_receipt_id from the receipt_number
5694    IF p_receipt_number IS NOT NULL THEN
5695      Default_cash_receipt_id(p_cash_receipt_id,
5696                              p_receipt_number,
5697                              l_cr_return_status);
5698    END IF;
5699 
5700    --If user has entered the receivable_application_id, get the related info
5701   IF p_receivable_application_id IS NOT NULL THEN
5702            Get_receivable_appln_info( p_receivable_application_id,
5703                                       l_ra_app_ps_id,
5704                                       l_cash_receipt_id,
5705                                       p_apply_gl_date, /* Bug fix 3451241 */
5706 				      l_customer_trx_id, /* LLCA */
5707                                       p_called_from,
5708                                       l_ra_return_status
5709                                      );
5710       IF nvl( l_ra_app_ps_id,-99) <> nvl(p_applied_payment_schedule_id,
5711                                                     nvl( l_ra_app_ps_id,-99))
5712        THEN
5713         IF ar_receipt_api_pub.Original_unapp_info.customer_trx_id IS NOT NULL OR
5714            ar_receipt_api_pub.Original_unapp_info.trx_number IS NOT NULL THEN
5715          FND_MESSAGE.SET_NAME('AR','AR_RAPI_TRX_RA_ID_X_INVALID');
5716          FND_MSG_PUB.Add;
5717          p_return_status := FND_API.G_RET_STS_ERROR;
5718         ELSIF ar_receipt_api_pub.Original_unapp_info.applied_ps_id IS NOT NULL THEN
5719          FND_MESSAGE.SET_NAME('AR','AR_RAPI_APP_PS_RA_ID_X_INVALID');
5720          FND_MSG_PUB.Add;
5721          p_return_status := FND_API.G_RET_STS_ERROR;
5722         END IF;
5723       ELSE
5724         p_applied_payment_schedule_id := l_ra_app_ps_id;
5725       END IF;
5726 
5727       IF nvl(l_cash_receipt_id,-99) <> nvl(p_cash_receipt_id,nvl(l_cash_receipt_id,-99)) THEN
5728         --Invalid receivable application identifier for the entered cash receipt
5729          FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_RA_ID_X_INVALID');
5730          FND_MSG_PUB.Add;
5731          p_return_status := FND_API.G_RET_STS_ERROR;
5732       ELSE
5733         p_cash_receipt_id := l_cash_receipt_id;
5734       END IF;
5735 
5736 	/* LLCA */
5737       IF nvl(l_customer_trx_id,-99) <> nvl(p_customer_trx_id,nvl(l_customer_trx_id,-99)) THEN
5738         --Invalid receivable application identifier for the entered customer trx id receipt
5739          FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_RA_ID_X_INVALID');
5740          FND_MSG_PUB.Add;
5741          p_return_status := FND_API.G_RET_STS_ERROR;
5742       ELSE
5743  	 p_customer_trx_id := l_customer_trx_id;
5744       END IF;
5745 
5746 
5747   ELSE --the user has not passed in the receivable application id
5748    --
5749    -- derive receivable_application_id
5750    --
5751    --If app_ps_id and the cash_receipt_id are not null then
5752    --get the default receivable_application_id which will be
5753    --used for defaulting or cross-validation
5754     IF p_cash_receipt_id IS NOT NULL AND
5755        p_applied_payment_schedule_id IS NOT NULL
5756      THEN
5757        --derive the receivable application id using the cash receipt id and
5758        --the applied payment schedule id
5759               Derive_receivable_appln_id(
5760                                   p_cash_receipt_id,
5761                                   p_applied_payment_schedule_id,
5762                                   p_apply_gl_date,
5763                                   l_receivable_application_id,
5764 				  l_customer_trx_id,
5765                                   l_ra_return_status);
5766               p_receivable_application_id := l_receivable_application_id;
5767 	      /* LLCA */
5768 	      If p_customer_trx_id is NULL
5769 	      THEN
5770 	      	p_customer_trx_id    := l_customer_trx_id;
5771 	      END IF;
5772 
5773     END IF; --in other cases we can't derive the receivable application id so it cannot be defaulted
5774 
5775   END IF;
5776 
5777   IF l_cust_trx_return_status <> FND_API.G_RET_STS_SUCCESS OR
5778      l_ps_id_return_status    <> FND_API.G_RET_STS_SUCCESS OR
5779      l_ra_return_status       <> FND_API.G_RET_STS_SUCCESS OR
5780      l_cr_return_status       <> FND_API.G_RET_STS_SUCCESS OR
5781      p_return_status          <> FND_API.G_RET_STS_SUCCESS
5782    THEN
5783      p_return_status  := FND_API.G_RET_STS_ERROR;
5784   END IF;
5785   IF PG_DEBUG in ('Y', 'C') THEN
5786      arp_util.debug('Derive_unapp_ids: ' || '****Defaulted Value for the unapplication ids****');
5787      arp_util.debug('Derive_unapp_ids: ' || 'p_cash_receipt_id             :'||to_char(p_cash_receipt_id));
5788      arp_util.debug('Derive_unapp_ids: ' || 'p_customer_trx_id             :'||to_char(p_customer_trx_id));
5789      arp_util.debug('Derive_unapp_ids: ' || 'p_installment                 :'||to_char(p_installment));
5790      arp_util.debug('Derive_unapp_ids: ' || 'p_applied_payment_schedule_id :'||to_char(p_applied_payment_schedule_id));
5791      arp_util.debug('Derive_unapp_ids: ' || 'p_receivable_application_id   :'||to_char(p_receivable_application_id));
5792      arp_util.debug('Derive_unapp_ids: ' || 'Return Status                 :'||p_return_status);
5793      arp_util.debug('Derive_unapp_ids ()-');
5794   END IF;
5795 EXCEPTION
5796  WHEN others THEN
5797   IF PG_DEBUG in ('Y', 'C') THEN
5798      arp_util.debug('EXCEPTION: Derive_unapp_ids()');
5799   END IF;
5800   raise;
5801 END Derive_unapp_ids;
5802 
5803 /* This is a routine to get the reversal_gl_date for the unapplication */
5804 PROCEDURE Default_reversal_gl_date(
5805                         p_receivable_application_id IN NUMBER,
5806                         p_reversal_gl_date IN OUT NOCOPY DATE,
5807                         p_apply_gl_date IN OUT NOCOPY DATE,
5808                         p_cash_receipt_id IN OUT NOCOPY NUMBER
5809                                    ) IS
5810 l_apply_gl_date     DATE;
5811 l_default_gl_date   DATE;
5812 l_defaulting_rule_used  VARCHAR2(100);
5813 l_error_message  VARCHAR2(240);
5814 BEGIN
5815   IF PG_DEBUG in ('Y', 'C') THEN
5816      arp_util.debug('Default_reversal_gl_date ()+');
5817   END IF;
5818     l_apply_gl_date := p_apply_gl_date;  /* Bug fix 3503167 */
5819 
5820     IF p_receivable_application_id IS NOT NULL THEN
5821      IF p_apply_gl_date  IS NULL THEN
5822       --get the gl_date for the application
5823       SELECT gl_date, cash_receipt_id
5824       INTO   l_apply_gl_date, p_cash_receipt_id
5825       FROM   ar_receivable_applications
5826       WHERE  receivable_application_id =
5827                 p_receivable_application_id;
5828      END IF;
5829       /* Bug fix 3503167 : We need to pass the apply_gl_date as the candidate gl_date and
5830          as the validation date */
5831       IF p_reversal_gl_date is null THEN
5832          IF (arp_util.validate_and_default_gl_date(
5833                 nvl(l_apply_gl_date,trunc(sysdate)),
5834                 NULL,
5835                 l_apply_gl_date,
5836                 NULL,
5837                 NULL,
5838                 NULL,
5839                 NULL,
5840                 NULL,
5841                 'N',
5842                 NULL,
5843                 arp_global.set_of_books_id,
5844                 222,
5845                 l_default_gl_date,
5846                 l_defaulting_rule_used,
5847                 l_error_message) = TRUE) THEN
5848 
5849            p_reversal_gl_date := l_default_gl_date;
5850          ELSE
5851          --we were not able to default the gl_date put the message
5852          --here on the stack, but the return status will be set
5853          --to FND_API.G_RET_STS_ERROR in the validation phase.
5854            FND_MESSAGE.SET_NAME('AR', 'GENERIC_MESSAGE');
5855            FND_MESSAGE.SET_TOKEN('GENERIC_TEXT', l_error_message);
5856            FND_MSG_PUB.Add;
5857          END IF;
5858       END IF;
5859     END IF;
5860   IF PG_DEBUG in ('Y', 'C') THEN
5861      arp_util.debug('Default_reversal_gl_date ()-');
5862   END IF;
5863 EXCEPTION
5864  When others THEN
5865     IF PG_DEBUG in ('Y', 'C') THEN
5866        arp_util.debug('EXCEPTION: Default_reversal_gl_date()');
5867     END IF;
5868     raise;
5869 END Default_reversal_gl_date;
5870 
5871 /*Added parameter p_cr_unapp_amount for bug 3119391 */
5872 PROCEDURE Default_unapp_info(
5873                         p_receivable_application_id IN NUMBER,
5874                         p_apply_gl_date    IN  DATE,
5875                         p_cash_receipt_id  IN  NUMBER,
5876                         p_reversal_gl_date IN OUT NOCOPY DATE,
5877                         p_receipt_gl_date  OUT NOCOPY DATE,
5878 			p_cr_unapp_amount OUT NOCOPY NUMBER
5879                           ) IS
5880 l_cash_receipt_id  NUMBER(15);
5881 l_apply_gl_date  DATE;
5882 BEGIN
5883   IF PG_DEBUG in ('Y', 'C') THEN
5884      arp_util.debug('Default_unapp_info ()+');
5885   END IF;
5886   l_apply_gl_date := p_apply_gl_date;
5887   l_cash_receipt_id := p_cash_receipt_id;
5888 
5889    Default_reversal_gl_date(p_receivable_application_id,
5890                             p_reversal_gl_date,
5891                             l_apply_gl_date,
5892                             l_cash_receipt_id );
5893 /* Default p_cr_unapp_amount for 3119391 */
5894 
5895    SELECT SUM(NVL(ra.amount_applied,0))
5896    INTO   p_cr_unapp_amount
5897    FROM   ar_receivable_applications ra
5898    WHERE  ra.status = 'UNAPP'
5899    AND    ra.cash_receipt_id = l_cash_receipt_id;
5900 
5901  --default the receipt gl date which is to be used later
5902  --in the validation of the reversal gl date.
5903     IF  p_receipt_gl_date IS NULL AND
5904         l_cash_receipt_id IS NOT NULL
5905       THEN
5906        BEGIN
5907          SELECT gl_date
5908          INTO   p_receipt_gl_date
5909          FROM   ar_cash_receipt_history crh
5910          WHERE  crh.cash_receipt_id = l_cash_receipt_id
5911              and crh.FIRST_POSTED_RECORD_FLAG = 'Y';
5912           -- Bug 3074658, Inconsistent bahaviour between UI/API
5913           -- AND  crh.current_record_flag = 'Y';
5914        EXCEPTION
5915          WHEN no_data_found THEN
5916           null;
5917           IF PG_DEBUG in ('Y', 'C') THEN
5918              arp_util.debug('Default_unapp_info: ' || 'Could not get the receipt_gl_date. ');
5919           END IF;
5920        END;
5921     END IF;
5922   IF PG_DEBUG in ('Y', 'C') THEN
5923      arp_util.debug('Default_unapp_info ()-');
5924   END IF;
5925 END Default_unapp_info;
5926 
5927 PROCEDURE Default_rev_catg_code(p_reversal_category_code IN  OUT NOCOPY VARCHAR2,
5928                                 p_reversal_category_name IN VARCHAR2,
5929                                 p_return_status OUT NOCOPY VARCHAR2
5930                                 ) IS
5931 
5932 BEGIN
5933   IF PG_DEBUG in ('Y', 'C') THEN
5934      arp_util.debug('Default_rev_catg_code ()+');
5935   END IF;
5936 
5937   p_return_status := FND_API.G_RET_STS_SUCCESS;
5938 
5939  IF p_reversal_category_code IS NULL THEN
5940   BEGIN
5941    SELECT lookup_code
5942    INTO   p_reversal_category_code
5943    FROM   ar_lookups
5944    WHERE  lookup_type = 'REVERSAL_CATEGORY_TYPE'
5945      AND  enabled_flag = 'Y'
5946      AND  meaning =  p_reversal_category_name;
5947   EXCEPTION
5948    WHEN no_data_found THEN
5949           FND_MESSAGE.SET_NAME('AR','AR_RAPI_REV_CAT_NAME_INVALID');
5950           FND_MSG_PUB.Add;
5951           p_return_status := FND_API.G_RET_STS_ERROR ;
5952      null;
5953   END;
5954  ELSE
5955    IF p_reversal_category_name IS NOT NULL THEN
5956        --give a warning message to indicate that the reversal category name
5957        --entered by the user has been ignored.
5958        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
5959        	THEN
5960          FND_MESSAGE.SET_NAME('AR','AR_RAPI_REV_CAT_NAME_IGN');
5961          FND_MSG_PUB.Add;
5962        END IF;
5963     END IF;
5964  END IF;
5965   IF PG_DEBUG in ('Y', 'C') THEN
5966      arp_util.debug('Default_rev_catg_code ()-');
5967   END IF;
5968 EXCEPTION
5969  WHEN others THEN
5970   IF PG_DEBUG in ('Y', 'C') THEN
5971      arp_util.debug('EXCEPTION: Default_rev_catg_code()');
5972   END IF;
5973   raise;
5974 END Default_rev_catg_code;
5975 
5976 PROCEDURE Default_rev_reason_code(
5977                          p_reversal_reason_code  IN OUT NOCOPY VARCHAR2,
5978                          p_reversal_reason_name  IN VARCHAR2,
5979                          p_return_status OUT NOCOPY VARCHAR2
5980                            ) IS
5981 
5982 BEGIN
5983   IF PG_DEBUG in ('Y', 'C') THEN
5984      arp_util.debug('Default_rev_reason_code ()+');
5985   END IF;
5986 
5987   p_return_status := FND_API.G_RET_STS_SUCCESS;
5988 
5989  IF p_reversal_reason_code IS NULL THEN
5990   BEGIN
5991    SELECT lookup_code
5992    INTO   p_reversal_reason_code
5993    FROM ar_lookups
5994    WHERE  lookup_type = 'CKAJST_REASON'
5995      AND  enabled_flag = 'Y'
5996      AND  meaning =  p_reversal_reason_name;
5997   EXCEPTION
5998    WHEN no_data_found THEN
5999           FND_MESSAGE.SET_NAME('AR','AR_RAPI_REV_REAS_NAME_INVALID');
6000           FND_MSG_PUB.Add;
6001           p_return_status := FND_API.G_RET_STS_ERROR ;
6002      null;
6003   END;
6004  ELSE
6005     IF p_reversal_reason_name IS NOT NULL THEN
6006        --give a warning message to indicate that the reversal category name
6007        --entered by the user has been ignored.
6008        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
6009        	THEN
6010          FND_MESSAGE.SET_NAME('AR','AR_RAPI_REV_REAS_NAME_IGN');
6011          FND_MSG_PUB.Add;
6012        END IF;
6013     END IF;
6014  END IF;
6015   IF PG_DEBUG in ('Y', 'C') THEN
6016      arp_util.debug('Default_rev_reason_code ()-');
6017   END IF;
6018 EXCEPTION
6019   WHEN others THEN
6020   IF PG_DEBUG in ('Y', 'C') THEN
6021      arp_util.debug('EXCEPTION: Default_rev_reason_code()');
6022   END IF;
6023   raise;
6024 END Default_rev_reason_code;
6025 
6026 PROCEDURE Derive_reverse_ids(
6027                          p_receipt_number         IN     VARCHAR2,
6028                          p_cash_receipt_id        IN OUT NOCOPY NUMBER,
6029                          p_reversal_category_name IN     VARCHAR2,
6030                          p_reversal_category_code IN OUT NOCOPY VARCHAR2,
6031                          p_reversal_reason_name   IN     VARCHAR2,
6032                          p_reversal_reason_code   IN OUT NOCOPY VARCHAR2,
6033                          p_return_status             OUT NOCOPY VARCHAR2
6034                            )  IS
6035 l_cr_def_return_status  VARCHAR2(1);
6036 l_rev_cat_return_status  VARCHAR2(1);
6037 l_rev_res_return_status  VARCHAR2(1);
6038 BEGIN
6039   IF PG_DEBUG in ('Y', 'C') THEN
6040      arp_util.debug('Derive_reverse_ids ()+ ');
6041   END IF;
6042    p_return_status :=  FND_API.G_RET_STS_SUCCESS ;
6043 
6044 
6045   --cash_receipt_id
6046   Default_cash_receipt_id(p_cash_receipt_id ,
6047                           p_receipt_number ,
6048                           l_cr_def_return_status);
6049    IF PG_DEBUG in ('Y', 'C') THEN
6050       arp_util.debug('Derive_reverse_ids: ' || 'l_cr_def__return_status   :'||l_cr_def_return_status);
6051    END IF;
6052 
6053   --reversal category
6054   Default_rev_catg_code(p_reversal_category_code,
6055                         p_reversal_category_name,
6056                         l_rev_cat_return_status
6057                          );
6058     IF PG_DEBUG in ('Y', 'C') THEN
6059        arp_util.debug('Derive_reverse_ids: ' || 'l_rev_cat_return_status  :'||l_rev_cat_return_status);
6060     END IF;
6061 
6062   --reversal reason code
6063   Default_rev_reason_code(p_reversal_reason_code,
6064                           p_reversal_reason_name,
6065                           l_rev_res_return_status
6066                            );
6067 
6068   IF l_rev_res_return_status <>  FND_API.G_RET_STS_SUCCESS OR
6069      l_rev_cat_return_status <>  FND_API.G_RET_STS_SUCCESS OR
6070      l_cr_def_return_status      <>  FND_API.G_RET_STS_SUCCESS
6071     THEN
6072       p_return_status := FND_API.G_RET_STS_ERROR ;
6073   END IF;
6074 
6075   IF PG_DEBUG in ('Y', 'C') THEN
6076      arp_util.debug('Derive_reverse_ids ()- ');
6077   END IF;
6078 EXCEPTION
6079  When others THEN
6080   IF PG_DEBUG in ('Y', 'C') THEN
6081      arp_util.debug('EXCEPTION: Derive_reverse_ids()');
6082   END IF;
6083 END  Derive_reverse_ids;
6084 
6085 PROCEDURE Default_reverse_info(p_cash_receipt_id  IN NUMBER,
6086                                p_reversal_gl_date IN OUT NOCOPY DATE,
6087                                p_reversal_date    IN OUT NOCOPY DATE,
6088                                p_receipt_state    OUT NOCOPY VARCHAR2,
6089                                p_receipt_gl_date  OUT NOCOPY DATE,
6090                                p_type             OUT NOCOPY VARCHAR2
6091                                ) IS
6092 l_receipt_date  DATE;
6093 l_return_status VARCHAR2(1);
6094 l_apply_date	DATE;
6095 l_gl_date	DATE;
6096 BEGIN
6097    IF PG_DEBUG in ('Y', 'C') THEN
6098       arp_util.debug('Default_reverse_info ()+');
6099    END IF;
6100    /*Get the receipt date and the status */
6101 
6102     SELECT cr.receipt_date, crh.status, cr.type
6103     INTO   l_receipt_date, p_receipt_state, p_type
6104     FROM   ar_cash_receipts cr,
6105            ar_cash_receipt_history crh
6106     WHERE  cr.cash_receipt_id = crh.cash_receipt_id
6107        AND crh.current_record_flag = 'Y'
6108        AND cr.cash_receipt_id = p_cash_receipt_id;
6109 
6110    /* Bug fix 3547720
6111       p_receipt_gl_date should be populated as the max(gl_date) from CRH */
6112 
6113     SELECT max(crh.gl_date)
6114     INTO   p_receipt_gl_date
6115     FROM   ar_cash_receipt_history crh
6116     WHERE  crh.cash_receipt_id = p_cash_receipt_id;
6117 
6118    /* Bug 8668394 : Default Reversal Date and Reversal Gl Date correctly */
6119     BEGIN
6120 	Select max(apply_date) , max(gl_date)
6121 	into   l_apply_date    , l_gl_date
6122 	from   ar_receivable_applications
6123 	where  cash_receipt_id = p_cash_receipt_id;
6124 
6125     EXCEPTION WHEN OTHERS THEN
6126 	IF PG_DEBUG in ('Y', 'C') THEN
6127 	    arp_util.debug('No application record exists for this receipt.');
6128 	    arp_util.debug('Default to sysdate to avoid NULL issue in defaulting logic.');
6129 	END IF;
6130 	    l_apply_date  := trunc(sysdate);
6131 	    l_gl_date	  := trunc(sysdate);
6132     END;
6133 
6134     /*Default reversal_date */
6135    IF p_reversal_date IS NULL THEN
6136     IF l_receipt_date > trunc(SYSDATE) THEN
6137        p_reversal_date := greatest(l_receipt_date, l_apply_date);
6138     ELSE
6139        p_reversal_date := greatest(trunc(SYSDATE), l_apply_date);
6140     END IF;
6141    END IF;
6142 
6143     /* default the reversal_gl_date */
6144    IF p_reversal_gl_date IS NULL THEN
6145     /* Bug fix 3547720 : Changed the call */
6146     /* Bug fix 3922062. Reversal_gl_date should be the maximum of (sysdate and the
6147       maximum of CRH gl_date) */
6148 
6149     Default_gl_date(greatest(p_receipt_gl_date, sysdate, l_gl_date),
6150                     p_reversal_gl_date,
6151                     p_receipt_gl_date,
6152                     l_return_status);
6153    END IF;
6154 
6155    IF PG_DEBUG in ('Y', 'C') THEN
6156       arp_util.debug('Default_reverse_info: ' || '**************Defaulted Variables ************');
6157       arp_util.debug('Default_reverse_info: ' || 'p_reversal_gl_date      : '||to_char(p_reversal_gl_date,'DD-MON-YYYY'));
6158       arp_util.debug('Default_reverse_info: ' || 'p_reversal_date         : '||to_char(p_reversal_date,'DD-MON-YYYY'));
6159       arp_util.debug('Default_reverse_info: ' || 'p_receipt_state         : '||p_receipt_state);
6160       arp_util.debug('Default_reverse_info: ' || 'p_receipt_gl_date       : '||to_char(p_receipt_gl_date,'DD-MON-YYYY'));
6161       arp_util.debug('Default_reverse_info ()-');
6162    END IF;
6163 EXCEPTION
6164 WHEN no_data_found  THEN
6165   --this may happen because of the invalid cash_receipt_id
6166   --do not raise any message here, it shall be raised in the
6167   --validation phase.
6168   IF PG_DEBUG in ('Y', 'C') THEN
6169      arp_util.debug('Default_reverse_info: ' || 'Could not default info :Cash Receipt Id is invalid');
6170   END IF;
6171 WHEN others THEN
6172   IF PG_DEBUG in ('Y', 'C') THEN
6173      arp_util.debug('EXCEPTION: Default_reverse_info() ');
6174   END IF;
6175   raise;
6176 END Default_reverse_info;
6177 
6178 PROCEDURE Default_on_ac_app_info(
6179                          p_cash_receipt_id  IN NUMBER,
6180                          p_cr_gl_date OUT NOCOPY DATE,
6181                          p_cr_unapp_amount OUT NOCOPY NUMBER,
6182                          p_receipt_date OUT NOCOPY DATE,
6183                          p_cr_payment_schedule_id OUT NOCOPY NUMBER,
6184                          p_amount_applied IN OUT NOCOPY NUMBER,
6185                          p_apply_gl_date IN OUT NOCOPY DATE,
6186                          p_apply_date    IN OUT NOCOPY DATE,
6187                          p_cr_currency_code OUT NOCOPY VARCHAR2,
6188                          p_return_status  OUT NOCOPY VARCHAR2
6189                               ) IS
6190 l_cr_customer_id  NUMBER;
6191 l_cr_currency_code  VARCHAR2(30);
6192 l_cr_exchange_rate  NUMBER;
6193 l_cr_cust_site_use_id  NUMBER;
6194 l_return_status  VARCHAR2(1);
6195 l_cr_amount   NUMBER;
6196 l_receipt_return_status  VARCHAR2(1);
6197 l_gl_date_return_status  VARCHAR2(1);
6198 l_receipt_date   DATE;
6199 l_cr_unapp_amount NUMBER;
6200 l_remit_bank_acct_use_id  NUMBER;
6201 l_receipt_method_id      NUMBER;
6202 BEGIN
6203       IF PG_DEBUG in ('Y', 'C') THEN
6204          arp_util.debug('Default_on_ac_app_info ()+');
6205       END IF;
6206               p_return_status := FND_API.G_RET_STS_SUCCESS;
6207 
6208                 Default_Receipt_Info(
6209                                  p_cash_receipt_id ,
6210                                  p_cr_gl_date,
6211                                  l_cr_customer_id,
6212                                  l_cr_amount,
6213                                  l_cr_currency_code,
6214                                  l_cr_exchange_rate,
6215                                  l_cr_cust_site_use_id,
6216                                  l_receipt_date,
6217                                  l_cr_unapp_amount,
6218                                  p_cr_payment_schedule_id,
6219                                  l_remit_bank_acct_use_id,
6220                                  l_receipt_method_id,
6221                                  l_receipt_return_status
6222                                   );
6223     IF PG_DEBUG in ('Y', 'C') THEN
6224        arp_util.debug('Default_on_ac_app_info: ' || 'Receipt defaulting return status :'||l_receipt_return_status);
6225     END IF;
6226                Default_apply_date(l_receipt_date ,
6227                                   null,
6228                                   p_apply_date);
6229 
6230            IF p_apply_gl_date IS NULL THEN
6231                Default_gl_date(p_cr_gl_date,
6232                                p_apply_gl_date,
6233                                NULL,
6234                                l_gl_date_return_status);
6235            END IF;
6236     IF PG_DEBUG in ('Y', 'C') THEN
6237        arp_util.debug('Default_on_ac_app_info: ' || 'Defaulting apply gl date return status :'|| l_gl_date_return_status);
6238     END IF;
6239 
6240               --default the amount applied
6241               IF p_amount_applied IS NULL THEN
6242                  p_amount_applied := l_cr_unapp_amount;
6243               END IF;
6244               --do the precision
6245               p_amount_applied :=  arp_util.CurrRound(
6246                                       p_amount_applied,
6247                                       l_cr_currency_code
6248                                         );
6249 
6250                p_receipt_date :=  l_receipt_date ;
6251                p_cr_unapp_amount := l_cr_unapp_amount;
6252 
6253               IF l_receipt_return_status <> FND_API.G_RET_STS_SUCCESS OR
6254                  l_receipt_return_status <> l_receipt_return_status  THEN
6255 
6256                  p_return_status := FND_API.G_RET_STS_ERROR ;
6257               END IF;
6258 
6259              p_cr_currency_code := l_cr_currency_code;
6260 
6261       IF PG_DEBUG in ('Y', 'C') THEN
6262          arp_util.debug('Default_on_ac_app_info: ' || '***************Default Values *****************');
6263          arp_util.debug('Default_on_ac_app_info: ' || 'p_cash_receipt_id       : '||to_char(p_cash_receipt_id));
6264          arp_util.debug('Default_on_ac_app_info: ' || 'p_cr_gl_date            : '||to_char(p_cr_gl_date,'DD-MON-YYYY'));
6265          arp_util.debug('Default_on_ac_app_info: ' || 'p_amount_applied        : '||to_char(p_amount_applied));
6266          arp_util.debug('Default_on_ac_app_info: ' || 'p_apply_gl_date         : '||to_char(p_apply_gl_date,'DD-MON-YYYY'));
6267          arp_util.debug('Default_on_ac_app_info: ' || 'p_apply_date            : '||to_char(p_apply_date,'DD-MON-YYYY'));
6268          arp_util.debug('Default_on_ac_app_info ()-');
6269       END IF;
6270 EXCEPTION
6271 WHEN others THEN
6272   IF PG_DEBUG in ('Y', 'C') THEN
6273      arp_util.debug('EXCEPTION: Default_on_ac_app_info() ');
6274   END IF;
6275   raise;
6276 END  Default_on_ac_app_info;
6277 
6278 PROCEDURE Derive_unapp_on_ac_ids(
6279                          p_receipt_number    IN VARCHAR2,
6280                          p_cash_receipt_id   IN OUT NOCOPY NUMBER,
6281                          p_receivable_application_id   IN OUT NOCOPY NUMBER,
6282                          p_apply_gl_date     OUT NOCOPY DATE,
6283                          p_return_status  OUT NOCOPY VARCHAR2
6284                                ) IS
6285 l_rec_appln_id  NUMBER ;
6286 l_apply_gl_date  DATE;
6287 l_cash_receipt_id   NUMBER;
6288 BEGIN
6289    p_return_status := FND_API.G_RET_STS_SUCCESS;
6290    IF PG_DEBUG in ('Y', 'C') THEN
6291       arp_util.debug('Derive_unapp_on_ac_ids ()+');
6292    END IF;
6293     --derive the cash_receipt_id from the receipt_number
6294     IF p_receipt_number IS NOT NULL THEN
6295         Default_cash_receipt_id(p_cash_receipt_id ,
6296                                 p_receipt_number ,
6297                                 p_return_status);
6298     END IF;
6299 
6300         --get the receivable application id for the on account application
6301         --on this cash receipt. If more than one on-account application exists
6302         --for the receipt, raise error.
6303       IF p_cash_receipt_id IS NOT NULL THEN
6304 
6305            BEGIN
6306               SELECT receivable_application_id, gl_date
6307               INTO   l_rec_appln_id , p_apply_gl_date
6308               FROM   ar_receivable_applications
6309               WHERE  cash_receipt_id = p_cash_receipt_id
6310                 AND  display = 'Y'
6311                 AND  status = 'ACC';
6312            EXCEPTION
6313              WHEN no_data_found THEN
6314                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_CASH_RCPT_ID_INVALID');
6315                 FND_MSG_PUB.Add;
6316                 p_return_status := FND_API.G_RET_STS_ERROR ;
6317              WHEN too_many_rows THEN
6318               IF p_receivable_application_id IS NULL THEN
6319                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_MULTIPLE_ON_AC_APP');
6320                 FND_MSG_PUB.Add;
6321                 p_return_status := FND_API.G_RET_STS_ERROR ;
6322               END IF;
6323 
6324            END;
6325 
6326       END IF;
6327 
6328        IF p_receivable_application_id IS NOT NULL THEN
6329 
6330           BEGIN
6331            SELECT  cash_receipt_id, gl_date
6332            INTO    l_cash_receipt_id , p_apply_gl_date
6333            FROM    ar_receivable_applications
6334            WHERE   receivable_application_id = p_receivable_application_id
6335              and   display = 'Y'
6336              and   status = 'ACC';
6337           EXCEPTION
6338             WHEN no_data_found THEN
6339                FND_MESSAGE.SET_NAME('AR','AR_RAPI_REC_APP_ID_INVALID');
6340                FND_MSG_PUB.Add;
6341                p_return_status := FND_API.G_RET_STS_ERROR ;
6342           END;
6343 
6344          --Compare the two cash_receipt_ids
6345          IF p_cash_receipt_id IS NOT NULL THEN
6346             IF p_cash_receipt_id <> NVL(l_cash_receipt_id,p_cash_receipt_id) THEN
6347                 --raise error X validation failed
6348                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_RA_ID_X_INVALID');
6349                 FND_MSG_PUB.Add;
6350                 p_return_status := FND_API.G_RET_STS_ERROR ;
6351             END IF;
6352          END IF;
6353 
6354        ELSE
6355         p_receivable_application_id := l_rec_appln_id ;
6356        END IF;
6357 
6358        IF p_cash_receipt_id IS NULL THEN
6359           p_cash_receipt_id := l_cash_receipt_id;
6360        END IF;
6361 
6362    IF PG_DEBUG in ('Y', 'C') THEN
6363       arp_util.debug('Derive_unapp_on_ac_ids ()+');
6364    END IF;
6365 END Derive_unapp_on_ac_ids;
6366 
6367 PROCEDURE Derive_otheraccount_ids(
6368                          p_receipt_number    IN VARCHAR2,
6369                          p_cash_receipt_id   IN OUT NOCOPY NUMBER,
6370                          p_applied_ps_id     IN NUMBER,
6371                          p_receivable_application_id   IN OUT NOCOPY NUMBER,
6372                          p_apply_gl_date     OUT NOCOPY DATE,
6373                          p_cr_unapp_amt     OUT NOCOPY NUMBER, /* Bug fix 3569640 */
6374                          p_return_status  OUT NOCOPY VARCHAR2
6375                                ) IS
6376 l_rec_appln_id  NUMBER ;
6377 l_apply_gl_date  DATE;
6378 l_cash_receipt_id   NUMBER;
6379 l_applied_ps_id     NUMBER;
6380 
6381 BEGIN
6382    p_return_status := FND_API.G_RET_STS_SUCCESS;
6383    IF PG_DEBUG in ('Y', 'C') THEN
6384       arp_util.debug('Derive_otheraccount_ids ()+');
6385    END IF;
6386     --derive the cash_receipt_id from the receipt_number
6387     IF p_receipt_number IS NOT NULL THEN
6388         Default_cash_receipt_id(p_cash_receipt_id ,
6389                                 p_receipt_number ,
6390                                 p_return_status);
6391     END IF;
6392 
6393         --get the receivable application id for the prepayment application
6394         --on this cash receipt. If more than one prepayment application exists
6395         --for the receipt, raise error.
6396       -- Bug 2751910 - restrict validation to prepayments
6397       IF (p_cash_receipt_id IS NOT NULL AND p_applied_ps_id = -7) THEN
6398 
6399            BEGIN
6400               SELECT receivable_application_id, gl_date
6401               INTO   l_rec_appln_id , p_apply_gl_date
6402               FROM   ar_receivable_applications
6403               WHERE  cash_receipt_id = p_cash_receipt_id
6404                 AND  applied_payment_schedule_id = p_applied_ps_id
6405                 AND  display = 'Y'
6406                 AND  status = 'OTHER ACC';
6407            EXCEPTION
6408              WHEN no_data_found THEN
6409                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_CASH_RCPT_ID_INVALID');
6410                 FND_MSG_PUB.Add;
6411                 p_return_status := FND_API.G_RET_STS_ERROR ;
6412              WHEN too_many_rows THEN
6413               IF p_receivable_application_id IS NULL THEN
6414                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_MULTIPLE_PREPAY');
6415                 FND_MSG_PUB.Add;
6416                 p_return_status := FND_API.G_RET_STS_ERROR ;
6417               END IF;
6418 
6419            END;
6420 
6421       END IF;
6422 
6423        IF p_receivable_application_id IS NOT NULL THEN
6424 
6425           BEGIN
6426            SELECT  cash_receipt_id, gl_date
6427            INTO    l_cash_receipt_id , p_apply_gl_date
6428            FROM    ar_receivable_applications
6429            WHERE   receivable_application_id = p_receivable_application_id
6430              and   display = 'Y'
6431              and   applied_payment_schedule_id = p_applied_ps_id
6432              and   status = 'OTHER ACC';
6433           EXCEPTION
6434             WHEN no_data_found THEN
6435                FND_MESSAGE.SET_NAME('AR','AR_RAPI_REC_APP_ID_INVALID');
6436                FND_MSG_PUB.Add;
6437                p_return_status := FND_API.G_RET_STS_ERROR ;
6438           END;
6439 
6440          --Compare the two cash_receipt_ids
6441          IF p_cash_receipt_id IS NOT NULL THEN
6442             IF p_cash_receipt_id <> NVL(l_cash_receipt_id,p_cash_receipt_id) THEN
6443                 --raise error X validation failed
6444                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_RA_ID_X_INVALID');
6445                 FND_MSG_PUB.Add;
6446                 p_return_status := FND_API.G_RET_STS_ERROR ;
6447             END IF;
6448          END IF;
6449 
6450        ELSE
6451         p_receivable_application_id := l_rec_appln_id ;
6452        END IF;
6453 
6454        IF p_cash_receipt_id IS NULL THEN
6455           p_cash_receipt_id := l_cash_receipt_id;
6456        END IF;
6457 
6458        /* Bug fix 3569640 */
6459        IF p_cash_receipt_id IS NOT NULL THEN
6460          SELECT SUM(NVL(ra.amount_applied,0))
6461          INTO  p_cr_unapp_amt
6462          FROM  ar_receivable_applications ra
6463          WHERE  ra.cash_receipt_id = p_cash_receipt_id
6464           AND   ra.status = 'UNAPP'
6465           AND   nvl(ra.confirmed_flag,'Y') = 'Y';
6466        END IF;
6467    IF PG_DEBUG in ('Y', 'C') THEN
6468       arp_util.debug('Derive_otheraccount_ids: ' || 'Derive_unapp_on_ac_ids ()+');
6469    END IF;
6470 END Derive_otheraccount_ids;
6471 
6472 PROCEDURE Default_unapp_on_ac_act_info(
6473                          p_receivable_application_id IN NUMBER,
6474                          p_apply_gl_date             IN DATE,
6475                          p_cash_receipt_id           IN NUMBER,
6476                          p_reversal_gl_date          IN OUT NOCOPY DATE,
6477                          p_receipt_gl_date           OUT NOCOPY DATE
6478                           ) IS
6479 l_apply_date DATE;
6480 l_apply_gl_date DATE;
6481 l_cash_receipt_id  NUMBER;
6482 l_default_gl_date  DATE;
6483 l_defaulting_rule_used  VARCHAR2(100);
6484 l_error_message  VARCHAR2(240);
6485 BEGIN
6486   IF PG_DEBUG in ('Y', 'C') THEN
6487      arp_util.debug('Default_unapp_on_ac_act_info: ' || 'Default_unapp_on_acc_act_info ()+');
6488   END IF;
6489   l_apply_gl_date := p_apply_gl_date;
6490 
6491       /* Bug fix 3503167 : We need to pass the apply_gl_date as the candidate gl_date and
6492          as the validation date */
6493       IF p_reversal_gl_date is null THEN
6494          IF (arp_util.validate_and_default_gl_date(
6495                 nvl(l_apply_gl_date,trunc(sysdate)),
6496                 NULL,
6497                 l_apply_gl_date,
6498                 NULL,
6499                 NULL,
6500                 NULL,
6501                 NULL,
6502                 NULL,
6503                 'N',
6504                 NULL,
6505                 arp_global.set_of_books_id,
6506                 222,
6507                 l_default_gl_date,
6508                 l_defaulting_rule_used,
6509                 l_error_message) = TRUE) THEN
6510 
6511            p_reversal_gl_date := l_default_gl_date;
6512          ELSE
6513          --we were not able to default the gl_date put the message
6514          --here on the stack, but the return status will be set
6515          --to FND_API.G_RET_STS_ERROR in the validation phase.
6516            FND_MESSAGE.SET_NAME('AR', 'GENERIC_MESSAGE');
6517            FND_MESSAGE.SET_TOKEN('GENERIC_TEXT', l_error_message);
6518            FND_MSG_PUB.Add;
6519          END IF;
6520       END IF;
6521 
6522    --derive the receipt_gl_date which is to be used
6523    --in the validation of the reversal gl date.
6524    IF  p_receipt_gl_date IS NULL AND
6525         l_cash_receipt_id IS NOT NULL
6526       THEN
6527        BEGIN
6528          SELECT gl_date
6529          INTO   p_receipt_gl_date
6530          FROM   ar_cash_receipt_history crh
6531          WHERE  crh.cash_receipt_id = l_cash_receipt_id
6532            and  crh.current_record_flag = 'Y';
6533        EXCEPTION
6534          WHEN no_data_found THEN
6535           null;
6536           IF PG_DEBUG in ('Y', 'C') THEN
6537              arp_util.debug('Default_unapp_on_ac_act_info: ' || 'Could not get the receipt_gl_date. ');
6538           END IF;
6539        END;
6540    END IF;
6541     IF PG_DEBUG in ('Y', 'C') THEN
6542        arp_util.debug('Default_unapp_on_ac_act_info: ' || '*****Defaulted Values *********');
6543        arp_util.debug('Default_unapp_on_ac_act_info: ' || 'p_cash_receipt_id            : '||to_char(p_cash_receipt_id));
6544        arp_util.debug('Default_unapp_on_ac_act_info: ' || 'p_receivable_application_id  : '||to_char(p_receivable_application_id));
6545        arp_util.debug('Default_unapp_on_ac_act_info: ' || 'p_apply_gl_date              : '||to_char(p_apply_gl_date,'DD-MON-YYYY'));
6546        arp_util.debug('Default_unapp_on_ac_act_info: ' || 'p_reversal_gl_date           : '||to_char(p_reversal_gl_date,'DD-MON-YYYY'));
6547        arp_util.debug('Default_unapp_on_ac_act_info: ' || 'Default_unapp_on_acc_act_info ()-');
6548     END IF;
6549 
6550 END Default_unapp_on_ac_act_info;
6551 
6552 PROCEDURE Derive_activity_unapp_ids(
6553                          p_receipt_number    IN VARCHAR2,
6554                          p_cash_receipt_id   IN OUT NOCOPY NUMBER,
6555                          p_receivable_application_id   IN OUT NOCOPY NUMBER,
6556                          p_called_from       IN VARCHAR2,
6557                          p_apply_gl_date     OUT NOCOPY DATE,
6558                          p_cr_unapp_amount   OUT NOCOPY NUMBER, /* Bug fix 3569640 */
6559                          p_return_status  OUT NOCOPY VARCHAR2
6560                                ) IS
6561 l_rec_appln_id  NUMBER ;
6562 l_apply_gl_date  DATE;
6563 l_cash_receipt_id   NUMBER;
6564 BEGIN
6565    p_return_status := FND_API.G_RET_STS_SUCCESS;
6566    IF PG_DEBUG in ('Y', 'C') THEN
6567       arp_util.debug('Derive_activity_unapp_ids ()+');
6568    END IF;
6569     --derive the cash_receipt_id from the receipt_number
6570     IF p_receipt_number IS NOT NULL THEN
6571         Default_cash_receipt_id(p_cash_receipt_id ,
6572                                 p_receipt_number ,
6573                                 p_return_status);
6574     END IF;
6575 
6576         --get the receivable application id for the on account application
6577         --on this cash receipt. If more than one activity application exists
6578         --for the receipt, raise error.
6579       /* Bug fix 2512907
6580          If the receivable_application_id is also passed by the user, do not
6581          do a generic select. Also receipt write-off unapplication requires
6582          that applied_payment_schedule_id should be compared with -3 */
6583       /* Bug 3840287 - credit card refund included */
6584       IF p_cash_receipt_id IS NOT NULL
6585            AND p_receivable_application_id IS NULL THEN
6586 
6587            BEGIN
6588               SELECT receivable_application_id, gl_date
6589               INTO   l_rec_appln_id , p_apply_gl_date
6590               FROM   ar_receivable_applications
6591               WHERE  cash_receipt_id = p_cash_receipt_id
6592 	      and  ((NVL(p_called_from,'RAPI') = 'BR_FACTORED_WITH_RECOURSE' AND
6593                      applied_payment_schedule_id = -2)
6594                  or applied_payment_schedule_id IN (-3,-6,-8)
6595                  or receivables_trx_id = -16)
6596               and    display = 'Y' and
6597                      status = 'ACTIVITY';
6598            EXCEPTION
6599              WHEN no_data_found THEN
6600               IF ar_receipt_api_pub.original_activity_unapp_info.cash_receipt_id IS NOT NULL THEN
6601                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_CASH_RCPT_ID_INVALID');
6602                 FND_MSG_PUB.Add;
6603                 p_return_status := FND_API.G_RET_STS_ERROR ;
6604               ELSIF ar_receipt_api_pub.original_activity_unapp_info.receipt_number IS NOT NULL THEN
6605                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_NUM_INVALID');
6606                 FND_MSG_PUB.Add;
6607                 p_return_status := FND_API.G_RET_STS_ERROR ;
6608               END IF;
6609              WHEN too_many_rows THEN
6610               IF p_receivable_application_id IS NULL THEN
6611                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_MULTIPLE_ACTIVITY_APP');
6612                 FND_MSG_PUB.Add;
6613                 p_return_status := FND_API.G_RET_STS_ERROR ;
6614               END IF;
6615 
6616            END;
6617 
6618       END IF;
6619 
6620        IF p_receivable_application_id IS NOT NULL THEN
6621 
6622           /* bug fix 2512907
6623              When a receipt write-off application is unapplied, the applied_payment_schedue_id
6624              should be compared with -3 */
6625          /* Bug 2751910 - or its a netting application with receivable_trx_id
6626                           of -16 */
6627          /* Bug 3840287 - credit card refund included */
6628           BEGIN
6629            SELECT  cash_receipt_id, gl_date
6630            INTO    l_cash_receipt_id , p_apply_gl_date
6631            FROM    ar_receivable_applications
6632            WHERE   receivable_application_id = p_receivable_application_id
6633 	      and  ((NVL(p_called_from,'RAPI') = 'BR_FACTORED_WITH_RECOURSE' AND
6634                      applied_payment_schedule_id = -2)
6635                  or applied_payment_schedule_id IN (-3,-6,-8)
6636                  or receivables_trx_id = -16)
6637              and   display = 'Y'
6638              and   status = 'ACTIVITY';
6639           EXCEPTION
6640             WHEN no_data_found THEN
6641                FND_MESSAGE.SET_NAME('AR','AR_RAPI_REC_APP_ID_INVALID');
6642                FND_MSG_PUB.Add;
6643                p_return_status := FND_API.G_RET_STS_ERROR ;
6644           END;
6645 
6646          --Compare the two cash_receipt_ids
6647          IF p_cash_receipt_id IS NOT NULL THEN
6648             IF p_cash_receipt_id <> NVL(l_cash_receipt_id,p_cash_receipt_id) THEN
6649                 --raise error X validation failed
6650                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_RA_ID_X_INVALID');
6651                 FND_MSG_PUB.Add;
6652                 p_return_status := FND_API.G_RET_STS_ERROR ;
6653             END IF;
6654          END IF;
6655 
6656        ELSE
6657         p_receivable_application_id := l_rec_appln_id ;
6658        END IF;
6659 
6660        IF p_cash_receipt_id IS NULL THEN
6661           p_cash_receipt_id := l_cash_receipt_id;
6662        END IF;
6663 
6664        /* Bug fix 3569640 */
6665        IF p_cash_receipt_id IS NOT NULL THEN
6666          SELECT SUM(NVL(ra.amount_applied,0))
6667          INTO  p_cr_unapp_amount
6668          FROM  ar_receivable_applications ra
6669          WHERE  ra.cash_receipt_id = p_cash_receipt_id
6670           AND   ra.status = 'UNAPP'
6671           AND   nvl(ra.confirmed_flag,'Y') = 'Y';
6672        END IF;
6673 
6674    IF PG_DEBUG in ('Y', 'C') THEN
6675       arp_util.debug('Derive_activity_unapp_ids ()+');
6676    END IF;
6677 END Derive_activity_unapp_ids;
6678 
6679 PROCEDURE get_doc_seq(p_application_id IN NUMBER,
6680                       p_document_name  IN VARCHAR2,
6681                       p_sob_id         IN NUMBER,
6682                       p_met_code	   IN VARCHAR2,
6683                       p_trx_date       IN DATE,
6684                       p_doc_sequence_value IN OUT NOCOPY NUMBER,
6685                       p_doc_sequence_id    OUT NOCOPY NUMBER,
6686                       p_return_status      OUT NOCOPY VARCHAR2
6687                       ) AS
6688 l_doc_seq_ret_stat   NUMBER;
6689 l_doc_sequence_name  VARCHAR2(50);
6690 l_doc_sequence_type  VARCHAR2(50);
6691 l_doc_sequence_value NUMBER;
6692 l_db_sequence_name  VARCHAR2(50);
6693 l_seq_ass_id  NUMBER;
6694 l_prd_tab_name  VARCHAR2(50);
6695 l_aud_tab_name  VARCHAR2(50);
6696 l_msg_flag      VARCHAR2(1);
6697 BEGIN
6698            IF PG_DEBUG in ('Y', 'C') THEN
6699               arp_util.debug('get_doc_seq ()+');
6700                arp_util.debug('get_doc_seq: ' || 'SEQ : '||NVL( pg_profile_doc_seq, 'N'));
6701                arp_util.debug('get_doc_seq: ' || 'p_document_name :'||p_document_name);
6702                arp_util.debug('get_doc_seq: ' || 'p_application_id :'||to_char(p_application_id));
6703                arp_util.debug('get_doc_seq: ' || 'p_sob_id  :'||to_char(p_sob_id));
6704             END IF;
6705             p_return_status := FND_API.G_RET_STS_SUCCESS;
6706 	     IF   ( NVL( pg_profile_doc_seq, 'N') <> 'N' )
6707            THEN
6708              BEGIN
6709                       /*------------------------------+
6710                        |  Get the document sequence.  |
6711                        +------------------------------*/
6712               l_doc_seq_ret_stat:=
6713                    fnd_seqnum.get_seq_info (
6714 		                                 p_application_id,
6715                                          p_document_name,
6716                                          p_sob_id,
6717                                          p_met_code,
6718                                          trunc(p_trx_date),
6719                                          p_doc_sequence_id,
6720                                          l_doc_sequence_type,
6721                                          l_doc_sequence_name,
6722                                          l_db_sequence_name,
6723                                          l_seq_ass_id,
6724                                          l_prd_tab_name,
6725                                          l_aud_tab_name,
6726                                          l_msg_flag,
6727                                          'Y',
6728                                          'Y');
6729              arp_util.debug('Doc sequence return status :'||to_char(nvl(l_doc_seq_ret_stat,-99)));
6730              IF PG_DEBUG in ('Y', 'C') THEN
6731                 arp_util.debug('get_doc_seq: ' || 'l_doc_sequence_name :'||l_doc_sequence_name);
6732              END IF;
6733              arp_util.debug('l_doc_sequence_id :'||to_char(nvl(p_doc_sequence_id,-99)));
6734 
6735                IF l_doc_seq_ret_stat = -8 THEN
6736                 --this is the case of Always Used
6737                  IF PG_DEBUG in ('Y', 'C') THEN
6738                     arp_util.debug('get_doc_seq: ' || 'The doc sequence does not exist for the current document');
6739                  END IF;
6740                  p_return_status := FND_API.G_RET_STS_ERROR;
6741                  --Error message
6742                  FND_MESSAGE.Set_Name( 'AR','AR_RAPI_DOC_SEQ_NOT_EXIST_A');
6743                  FND_MSG_PUB.Add;
6744                ELSIF l_doc_seq_ret_stat = -2  THEN
6745                --this is the case of Partially Used
6746                 IF PG_DEBUG in ('Y', 'C') THEN
6747                    arp_util.debug('get_doc_seq: ' || 'The doc sequence does not exist for the current document');
6748                 END IF;
6749                  --Warning
6750                  IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
6751                   THEN
6752                      FND_MESSAGE.SET_NAME('AR','AR_RAPI_DOC_SEQ_NOT_EXIST_P');
6753                      FND_MSG_PUB.Add;
6754                  END IF;
6755                END IF;
6756                /* Added SUBSTRB(l_doc_sequence_type,1,1) = 'A' in IF
6757                   for Bug 2667348 */
6758                /* Bug 3038259:  Above fix did not consider gapless as
6759                   Automatic type */
6760                 IF ( l_doc_sequence_name IS NOT NULL
6761                  AND p_doc_sequence_id   IS NOT NULL
6762                  AND SUBSTRB(l_doc_sequence_type,1,1) in ( 'A', 'G'))
6763                    THEN
6764                              /*------------------------------------+
6765                               |  Automatic Document Numbering case |
6766                               +------------------------------------*/
6767                      IF PG_DEBUG in ('Y', 'C') THEN
6768                         arp_util.debug('get_doc_seq: ' || 'Automatic Document Numbering case ');
6769                      END IF;
6770                               l_doc_seq_ret_stat :=
6771                                   fnd_seqnum.get_seq_val (
6772 		                                               p_application_id,
6773 		                                               p_document_name,
6774 		                                               p_sob_id,
6775                                                        p_met_code,
6776                                                        TRUNC(p_trx_date),
6777                                                        l_doc_sequence_value,
6778                                                        p_doc_sequence_id);
6779                       IF p_doc_sequence_value IS NOT NULL THEN
6780                       --raise an error message because the user is not supposed to pass
6781                       --in a value for the document sequence number in this case.
6782                          p_return_status := FND_API.G_RET_STS_ERROR;
6783                          FND_MESSAGE.Set_Name('AR', 'AR_RAPI_DOC_SEQ_AUTOMATIC');
6784                          FND_MSG_PUB.Add;
6785                       END IF;
6786                         p_doc_sequence_value := l_doc_sequence_value;
6787                         arp_util.debug('l_doc_sequence_value :'||to_char(nvl(p_doc_sequence_value,-99)));
6788                    ELSIF (
6789                        p_doc_sequence_id    IS NOT NULL
6790                    AND p_doc_sequence_value IS NOT NULL
6791                        )
6792                         THEN
6793                                  /*-------------------------------------+
6794                                   |  Manual Document Numbering case     |
6795                                   |  with the document value specified. |
6796                                   |  Use the specified value.           |
6797                                   +-------------------------------------*/
6798 
6799                                   NULL;
6800 
6801                    ELSIF (
6802                          p_doc_sequence_id    IS NOT NULL
6803                      AND p_doc_sequence_value IS NULL
6804                       )
6805                        THEN
6806                                  /*-----------------------------------------+
6807                                   |  Manual Document Numbering case         |
6808                                   |  with the document value not specified. |
6809                                   |  Generate a document value mandatory    |
6810                                   |  error.                                 |
6811                                   +-----------------------------------------*/
6812                           IF NVL(pg_profile_doc_seq,'N') = 'A' THEN
6813                                 p_return_status := FND_API.G_RET_STS_ERROR;
6814                                 FND_MESSAGE.Set_Name('AR', 'AR_RAPI_DOC_SEQ_VALUE_NULL_A');
6815                                 FND_MESSAGE.Set_Token('SEQUENCE', l_doc_sequence_name);
6816                                 FND_MSG_PUB.Add;
6817                            ELSIF NVL(pg_profile_doc_seq,'N') = 'P'  THEN
6818                              --Warning
6819                              IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS) THEN
6820                                 FND_MESSAGE.SET_NAME('AR','AR_RAPI_DOC_SEQ_VALUE_NULL_P');
6821                                 FND_MSG_PUB.Add;
6822                              END IF;
6823                            END IF;
6824 
6825 
6826                    END IF;
6827 
6828                    EXCEPTION
6829                    WHEN NO_DATA_FOUND THEN
6830                            /*------------------------------------------+
6831                             |  No document assignment was found.       |
6832                             |  Generate an error if document numbering |
6833                             |  is mandatory.                           |
6834                             +------------------------------------------*/
6835                          IF PG_DEBUG in ('Y', 'C') THEN
6836                             arp_util.debug('get_doc_seq: ' || 'no_data_found raised');
6837                          END IF;
6838                          IF   (pg_profile_doc_seq = 'A' ) THEN
6839                             p_return_status := FND_API.G_RET_STS_ERROR;
6840                             FND_MESSAGE.Set_Name( 'FND','UNIQUE-ALWAYS USED');
6841                             FND_MSG_PUB.Add;
6842                          ELSE
6843                            p_doc_sequence_id    := NULL;
6844                            p_doc_sequence_value := NULL;
6845                          END IF;
6846 
6847                    WHEN OTHERS THEN
6848                      IF PG_DEBUG in ('Y', 'C') THEN
6849                         arp_util.debug('get_doc_seq: ' || 'Unhandled exception in doc sequence assignment');
6850                      END IF;
6851                      raise;
6852 
6853                    END;
6854 
6855              END IF;
6856   IF PG_DEBUG in ('Y', 'C') THEN
6857      arp_util.debug('get_doc_seq ()+');
6858   END IF;
6859 END get_doc_seq;
6860 
6861 /* modified for tca uptake */
6862 PROCEDURE Derive_cust_info_from_trx(
6863                                 p_customer_trx_id               IN ar_payment_schedules.customer_trx_id%TYPE,
6864                                 p_trx_number                    IN ra_customer_trx.trx_number%TYPE,
6865                                 p_installment                   IN ar_payment_schedules.terms_sequence_number%TYPE,
6866                                 p_applied_payment_schedule_id IN ar_payment_schedules.payment_schedule_id%TYPE,
6867                                 p_currency_code               IN ar_cash_receipts.currency_code%TYPE,
6868                                 p_customer_id                OUT NOCOPY ar_payment_schedules.customer_id%TYPE,
6869                                 p_customer_site_use_id       OUT NOCOPY hz_cust_site_uses.site_use_id%TYPE,
6870                                 p_return_status              OUT NOCOPY  VARCHAR2
6871                                  )IS
6872 /* modified for tca uptake */
6873 --  Variables addition for Bug 1907635
6874 l_sel_stmt long;
6875 trx_customer INTEGER;
6876 l_rows_processed INTEGER;
6877 -- End of variables addition for Bug 1907635
6878 l_customer_trx_id  NUMBER;
6879 BEGIN
6880     -- Bug 1907635
6881     -- Build the query dynamically based on the input parameters
6882     l_sel_stmt := '
6883               SELECT ps.customer_id,
6884                      ps.customer_site_use_id
6885               FROM   hz_cust_site_uses su,
6886                      hz_cust_accounts cust_acct,
6887                      ra_cust_trx_types ctt,
6888                      ar_payment_schedules ps
6889               WHERE  su.site_use_id = ps.customer_site_use_id
6890                  and cust_acct.cust_account_id = ps.customer_id
6891                  and ctt.cust_trx_type_id = ps.cust_trx_type_id
6892                  and ps.selected_for_receipt_batch_id is null
6893                  and ps.class in (''BR'',''CB'',''CM'',''DEP'',''DM'',''INV'')
6894                  and ps.invoice_currency_code = decode(nvl(:pg_profile_enable_cc, ''N''), ''Y'',
6895                      decode(ps.class, ''CM'', :p_currency_code, ps.invoice_currency_code), :p_currency_code)
6896                  and ps.status = ''OP'' ';
6897 
6898    IF p_applied_payment_schedule_id IS NOT NULL THEN
6899       l_sel_stmt := l_sel_stmt || ' and ps.payment_schedule_id = :applied_ps_id ';
6900    END IF;
6901    IF p_customer_trx_id IS NOT NULL THEN
6902       l_sel_stmt := l_sel_stmt || ' and ps.customer_trx_id =  :cust_trx_id ';
6903    END IF;
6904    IF p_installment IS NOT NULL THEN
6905       l_sel_stmt := l_sel_stmt || ' and ps.terms_sequence_number =  :inst ';
6906    END IF;
6907 
6908    trx_customer := dbms_sql.open_cursor;
6909    dbms_sql.parse(trx_customer, l_sel_stmt, dbms_sql.v7);
6910    /* Bugfix 2605347 */
6911    dbms_sql.bind_variable(trx_customer,':pg_profile_enable_cc', pg_profile_enable_cc);
6912    dbms_sql.bind_variable(trx_customer,':p_currency_code', p_currency_code);
6913    IF p_applied_payment_schedule_id IS NOT NULL THEN
6914      dbms_sql.bind_variable(trx_customer,':applied_ps_id',p_applied_payment_schedule_id);
6915    END IF;
6916    IF p_customer_trx_id IS NOT NULL THEN
6917      dbms_sql.bind_variable(trx_customer, ':cust_trx_id', p_customer_trx_id);
6918    END IF;
6919    IF p_installment IS NOT NULL THEN
6920      dbms_sql.bind_variable(trx_customer, ':inst', p_installment);
6921    END IF;
6922    dbms_sql.define_column(trx_customer,1,p_customer_id);
6923    dbms_sql.define_column(trx_customer,2,p_customer_site_use_id);
6924    -- End of bug 1907635
6925 
6926  p_return_status := FND_API.G_RET_STS_SUCCESS;
6927  l_customer_trx_id := p_customer_trx_id;
6928   IF  p_trx_number  IS NOT NULL THEN
6929     Default_customer_trx_id(l_customer_trx_id ,
6930                             p_trx_number ,
6931                             p_return_status);
6932   END IF;
6933 
6934  IF  p_return_status = FND_API.G_RET_STS_SUCCESS THEN
6935   IF    p_applied_payment_schedule_id IS NOT NULL AND
6936         l_customer_trx_id IS NULL AND
6937         p_installment IS NULL THEN
6938         -- Bug 1907635
6939           l_rows_processed := dbms_sql.execute(trx_customer);
6940           IF dbms_sql.fetch_rows(trx_customer) > 0 then
6941             dbms_sql.column_value(trx_customer, 1, p_customer_id);
6942             dbms_sql.column_value(trx_customer, 2, p_customer_site_use_id);
6943           ELSE
6944             p_return_status := FND_API.G_RET_STS_ERROR;
6945             FND_MESSAGE.Set_Name( 'AR','AR_RAPI_PSID_NOT_DEF_CUS');
6946             FND_MSG_PUB.Add;
6947           END IF;
6948           dbms_sql.close_cursor(trx_customer);
6949          -- End Bug 1907635
6950   ELSIF p_applied_payment_schedule_id IS NOT NULL AND
6951         l_customer_trx_id IS NOT NULL AND
6952         p_installment IS NOT NULL THEN
6953         -- Bug 1907635
6954           l_rows_processed := dbms_sql.execute(trx_customer);
6955           IF dbms_sql.fetch_rows(trx_customer) > 0 then
6956             dbms_sql.column_value(trx_customer, 1, p_customer_id);
6957             dbms_sql.column_value(trx_customer, 2, p_customer_site_use_id);
6958           ELSE
6959             p_return_status := FND_API.G_RET_STS_ERROR;
6960             FND_MESSAGE.Set_Name( 'AR','AR_RAPI_TRX_INS_PS_NOT_DEF_CUS');
6961             FND_MSG_PUB.Add;
6962           END IF;
6963           dbms_sql.close_cursor(trx_customer);
6964          -- End Bug 1907635
6965   ELSIF p_applied_payment_schedule_id IS NOT NULL AND
6966         l_customer_trx_id IS NOT NULL AND
6967         p_installment IS NULL THEN
6968         -- Bug 1907635
6969           l_rows_processed := dbms_sql.execute(trx_customer);
6970           IF dbms_sql.fetch_rows(trx_customer) > 0 then
6971             dbms_sql.column_value(trx_customer, 1, p_customer_id);
6972             dbms_sql.column_value(trx_customer, 2, p_customer_site_use_id);
6973           ELSE
6974             p_return_status := FND_API.G_RET_STS_ERROR;
6975             FND_MESSAGE.Set_Name( 'AR','AR_RAPI_TRX_PS_NOT_DEF_CUS');
6976             FND_MSG_PUB.Add;
6977           END IF;
6978           dbms_sql.close_cursor(trx_customer);
6979          -- End Bug 1907635
6980   ELSIF p_applied_payment_schedule_id IS NOT NULL AND
6981         l_customer_trx_id IS NULL AND
6982         p_installment IS NOT NULL THEN
6983         -- Bug 1907635
6984           l_rows_processed := dbms_sql.execute(trx_customer);
6985           IF dbms_sql.fetch_rows(trx_customer) > 0 then
6986             dbms_sql.column_value(trx_customer, 1, p_customer_id);
6987             dbms_sql.column_value(trx_customer, 2, p_customer_site_use_id);
6988           ELSE
6989             p_return_status := FND_API.G_RET_STS_ERROR;
6990             FND_MESSAGE.Set_Name( 'AR','AR_RAPI_INS_PS_NOT_DEF_CUS');
6991             FND_MSG_PUB.Add;
6992           END IF;
6993           dbms_sql.close_cursor(trx_customer);
6994          -- End Bug 1907635
6995   ELSIF p_applied_payment_schedule_id IS NULL AND
6996         l_customer_trx_id IS NOT NULL AND
6997         p_installment IS NOT NULL THEN
6998         -- Bug 1907635
6999           l_rows_processed := dbms_sql.execute(trx_customer);
7000           IF dbms_sql.fetch_rows(trx_customer) > 0 then
7001             dbms_sql.column_value(trx_customer, 1, p_customer_id);
7002             dbms_sql.column_value(trx_customer, 2, p_customer_site_use_id);
7003           ELSE
7004             p_return_status := FND_API.G_RET_STS_ERROR;
7005             FND_MESSAGE.Set_Name( 'AR','AR_RAPI_TRX_INS_NOT_DEF_CUS');
7006             FND_MSG_PUB.Add;
7007           END IF;
7008           dbms_sql.close_cursor(trx_customer);
7009          -- End Bug 1907635
7010   ELSIF p_applied_payment_schedule_id IS NULL AND
7011         l_customer_trx_id IS NOT NULL AND
7012         p_installment IS NULL THEN
7013         -- Bug 1907635
7014           l_rows_processed := dbms_sql.execute(trx_customer);
7015           IF dbms_sql.fetch_rows(trx_customer) > 0 then
7016             dbms_sql.column_value(trx_customer, 1, p_customer_id);
7017             dbms_sql.column_value(trx_customer, 2, p_customer_site_use_id);
7018           ELSE
7019             p_return_status := FND_API.G_RET_STS_ERROR;
7020             FND_MESSAGE.Set_Name( 'AR','AR_RAPI_PSID_NOT_DEF_CUS');
7021             FND_MSG_PUB.Add;
7022           END IF;
7023           dbms_sql.close_cursor(trx_customer);
7024          -- End Bug 1907635
7025   ELSE
7026          -- Bug 1907635
7027          dbms_sql.close_cursor(trx_customer);
7028          -- End Bug 1907635
7029         p_customer_id := NULL;
7030         p_customer_site_use_id := NULL;
7031   END IF;
7032  END IF;
7033 
7034      IF p_customer_id IS NULL THEN
7035         --raise error because application cant be done against an unidentified receipt
7036         p_return_status := FND_API.G_RET_STS_ERROR;
7037         FND_MESSAGE.Set_Name( 'AR','AR_RAPI_CUST_ID_NULL');
7038         FND_MSG_PUB.Add;
7039      END IF;
7040 
7041   pg_cust_derived_from := 'TRANSACTION';
7042 
7043 END Derive_cust_info_from_trx;
7044 
7045 PROCEDURE Default_misc_ids(
7046               p_usr_currency_code            IN      VARCHAR2,
7047               p_usr_exchange_rate_type       IN      VARCHAR2,
7048               p_activity                     IN      VARCHAR2,
7049               p_reference_type               IN      VARCHAR2,
7050               p_reference_num                IN      VARCHAR2,
7051               p_tax_code                     IN      VARCHAR2,
7052               p_receipt_method_name          IN OUT NOCOPY  VARCHAR2,
7053               p_remittance_bank_account_name IN      VARCHAR2,
7054               p_remittance_bank_account_num  IN      VARCHAR2,
7055               p_currency_code                IN OUT NOCOPY  VARCHAR2,
7056               p_exchange_rate_type           IN OUT NOCOPY  VARCHAR2,
7057               p_receivables_trx_id           IN OUT NOCOPY  NUMBER,
7058               p_reference_id                 IN OUT NOCOPY  NUMBER,
7059               p_vat_tax_id                   IN OUT NOCOPY  NUMBER,
7060               p_receipt_method_id            IN OUT NOCOPY  NUMBER,
7061               p_remittance_bank_account_id   IN OUT NOCOPY  NUMBER,
7062               p_return_status                   OUT NOCOPY  VARCHAR2,
7063               p_receipt_date                 IN DATE DEFAULT NULL
7064                        )
7065 IS
7066 l_remittance_bank_account_id NUMBER;
7067 l_receipt_method_id          NUMBER;
7068 l_return_status              VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
7069 l_get_id_return_status       VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
7070 l_get_x_val_return_status    VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
7071 l_le_id                        NUMBER;
7072 BEGIN
7073 p_return_status := FND_API.G_RET_STS_SUCCESS;
7074 
7075 --Receivable Activity Name /Id.
7076  IF p_receivables_trx_id IS NULL THEN
7077     IF p_activity IS NOT NULL THEN
7078       p_receivables_trx_id:= Get_Id('RECEIVABLES_ACTIVITY',
7079                                      p_activity,
7080                                      l_get_id_return_status
7081                                     );
7082      IF p_receivables_trx_id IS NULL THEN
7083        FND_MESSAGE.SET_NAME('AR','AR_RAPI_ACTIVITY_INVALID');
7084        FND_MSG_PUB.Add;
7085        p_return_status := FND_API.G_RET_STS_ERROR;
7086      END IF;
7087 
7088     END IF;
7089 
7090  ELSE
7091     IF (p_activity IS NOT NULL) THEN
7092        --give a warning message to indicate that the activity name
7093        --entered by the user has been ignored.
7094        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
7095         THEN
7096          FND_MESSAGE.SET_NAME('AR','AR_RAPI_ACTIVITY_IGN');
7097          FND_MSG_PUB.Add;
7098        END IF;
7099     END IF;
7100 
7101  END IF;
7102 
7103 --Receipt method ID,Name
7104  IF p_receipt_method_id IS NULL
7105   THEN
7106    IF p_receipt_method_name IS NOT NULL THEN
7107       p_receipt_method_id := Get_Id('RECEIPT_METHOD_NAME',
7108                                      p_receipt_method_name,
7109                                      l_get_id_return_status
7110                                     );
7111      IF p_receipt_method_id IS NULL THEN
7112        FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_MD_NAME_INVALID');
7113        FND_MSG_PUB.Add;
7114        p_return_status := FND_API.G_RET_STS_ERROR;
7115      END IF;
7116    END IF;
7117 
7118  ELSE
7119     IF (p_receipt_method_name IS NOT NULL) THEN
7120        --give a warning message to indicate that the receipt_method_name
7121        --entered by the user has been ignored.
7122        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
7123         THEN
7124          FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_MD_NAME_IGN');
7125          FND_MSG_PUB.Add;
7126        END IF;
7127     ELSE
7128         BEGIN
7129          SELECT name
7130          INTO   p_receipt_method_name
7131          FROM   ar_receipt_methods
7132          WHERE  receipt_method_id = p_receipt_method_id;
7133         EXCEPTION
7134          WHEN no_data_found THEN
7135           IF PG_DEBUG in ('Y', 'C') THEN
7136              arp_util.debug('Default_misc_ids: ' || 'Invalid receipt method id');
7137           END IF;
7138            null;
7139         END;
7140 
7141     END IF;
7142  END IF;
7143 
7144 --Remittance bank account Number,Name,ID
7145 
7146  IF p_remittance_bank_account_id IS NULL
7147   THEN
7148   IF(p_remittance_bank_account_name IS NOT NULL) and
7149      (p_remittance_bank_account_num IS NULL)
7150     THEN
7151       p_remittance_bank_account_id := Get_Id('REMIT_BANK_ACCOUNT_NAME',
7152                                              p_remittance_bank_account_name,
7153                                              l_get_id_return_status
7154                                             );
7155      IF p_remittance_bank_account_id IS NULL THEN
7156        FND_MESSAGE.SET_NAME('AR','AR_RAPI_REM_BK_AC_NAME_INVALID');
7157        FND_MSG_PUB.Add;
7158        p_return_status := FND_API.G_RET_STS_ERROR;
7159      END IF;
7160   ELSIF(p_remittance_bank_account_name IS  NULL) and
7161         (p_remittance_bank_account_num IS NOT NULL)
7162     THEN
7163       p_remittance_bank_account_id := Get_Id('REMIT_BANK_ACCOUNT_NUMBER',
7164                                               p_remittance_bank_account_num,
7165                                               l_get_id_return_status
7166                                              );
7167      IF p_remittance_bank_account_id IS NULL THEN
7168        FND_MESSAGE.SET_NAME('AR','AR_RAPI_REM_BK_AC_NUM_INVALID');
7169        FND_MSG_PUB.Add;
7170        p_return_status := FND_API.G_RET_STS_ERROR;
7171      END IF;
7172    ELSIF(p_remittance_bank_account_name IS NOT NULL) and
7173         (p_remittance_bank_account_num IS NOT NULL)
7174     THEN
7175       p_remittance_bank_account_id := Get_Cross_Validated_Id( 'REMIT_BANK_ACCOUNT',
7176                                                p_remittance_bank_account_num,
7177                                                p_remittance_bank_account_name,
7178                                                l_get_x_val_return_status
7179                                               );
7180      IF p_remittance_bank_account_id IS NULL THEN
7181        FND_MESSAGE.SET_NAME('AR','AR_RAPI_REM_BK_AC_2_INVALID');
7182        FND_MSG_PUB.Add;
7183        p_return_status := FND_API.G_RET_STS_ERROR;
7184      END IF;
7185    END IF;
7186 
7187  ELSE
7188 
7189    IF (p_remittance_bank_account_name IS NOT NULL) OR
7190       (p_remittance_bank_account_num IS NOT NULL)
7191     THEN
7192        --give a warning message to indicate that the remittance_bank_account_num and
7193        --remittance_bank_account_name entered by the user have been ignored.
7194        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
7195         THEN
7196          FND_MESSAGE.SET_NAME('AR','AR_RAPI_REM_BK_AC_NAME_NUM_IGN');
7197          FND_MSG_PUB.Add;
7198        END IF;
7199    END IF;
7200 
7201  END IF;
7202 
7203  /* Initialize ZX */
7204    l_le_id :=
7205          ar_receipt_lib_pvt.get_legal_entity(p_remittance_bank_account_id);
7206 
7207 -- Exchange_rate_type
7208  IF p_exchange_rate_type IS NULL THEN
7209    IF p_usr_exchange_rate_type IS NOT NULL
7210     THEN
7211       p_exchange_rate_type := Get_Id('EXCHANGE_RATE_TYPE_NAME', /* Bug fix 2982212*/
7212                                      p_usr_exchange_rate_type,
7213                                      l_get_id_return_status
7214                                     );
7215       IF p_exchange_rate_type IS NULL THEN
7216         FND_MESSAGE.SET_NAME('AR','AR_RAPI_USR_X_RATE_TYP_INVALID');
7217         FND_MSG_PUB.Add;
7218         p_return_status := FND_API.G_RET_STS_ERROR;
7219       END IF;
7220    END IF;
7221 
7222  ELSE
7223    IF  (p_usr_exchange_rate_type IS NOT NULL) THEN
7224        --give a warning message to indicate that the usr_exchange_rate_type
7225        -- entered by the user have been ignored.
7226        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
7227         THEN
7228          FND_MESSAGE.SET_NAME('AR','AR_RAPI_USR_X_RATE_TYPE_IGN');
7229          FND_MSG_PUB.Add;
7230        END IF;
7231    END IF;
7232 
7233  END IF;
7234 
7235 --Currency code
7236  IF p_currency_code IS NULL THEN
7237    IF p_usr_currency_code IS NOT NULL
7238     THEN
7239       p_currency_code :=     Get_Id('CURRENCY_NAME',
7240                                      p_usr_currency_code,
7241                                      l_get_id_return_status
7242                                     );
7243       IF p_currency_code IS NULL THEN
7244         FND_MESSAGE.SET_NAME('AR','AR_RAPI_USR_CURR_CODE_INVALID');
7245         FND_MSG_PUB.Add;
7246         p_return_status := FND_API.G_RET_STS_ERROR;
7247       END IF;
7248    ELSE
7249 
7250      p_currency_code := arp_global.functional_currency;
7251      --Raise a warning message saying that currency was defaulted
7252      IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
7253        THEN
7254         FND_MESSAGE.SET_NAME('AR','AR_RAPI_FUNC_CURR_DEFAULTED');
7255         FND_MSG_PUB.Add;
7256      END IF;
7257 
7258    END IF;
7259 
7260  ELSE
7261    IF  (p_usr_currency_code IS NOT NULL) THEN
7262 
7263        --give a warning message to indicate that the usr_currency_code
7264        -- entered by the user have been ignored.
7265        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
7266         THEN
7267          FND_MESSAGE.SET_NAME('AR','AR_RAPI_USR_CURR_CODE_IGN');
7268          FND_MSG_PUB.Add;
7269        END IF;
7270 
7271    END IF;
7272  END IF;
7273 
7274 --Reference Number, ID
7275 --Based on the reference type, the corresponding reference id
7276 --will be derived from the reference number.
7277  IF p_reference_id IS NULL THEN
7278   IF p_reference_num IS NOT NULL THEN
7279 
7280    IF p_reference_type = 'PAYMENT' THEN
7281        p_reference_id :=    to_number(Get_Id('REF_PAYMENT',
7282                                      p_reference_num,
7283                                      l_get_id_return_status)
7284                                     );
7285    ELSIF p_reference_type = 'PAYMENT_BATCH'  THEN
7286       --
7287       p_reference_id :=     to_number(Get_Id('REF_PAYMENT_BATCH',
7288                                     p_reference_num,
7289                                     l_get_id_return_status)
7290                                     );
7291    ELSIF p_reference_type = 'RECEIPT'  THEN
7292       --
7293       p_reference_id :=    to_number(Get_Id('REF_RECEIPT',
7294                                     p_reference_num,
7295                                     l_get_id_return_status
7296                                     ));
7297    ELSIF p_reference_type = 'REMITTANCE' THEN
7298       --
7299       p_reference_id :=     to_number(Get_Id('REF_REMITTANCE',
7300                                     p_reference_num,
7301                                     l_get_id_return_status
7302                                     ));
7303    END IF;
7304 
7305    IF p_reference_id IS NULL THEN
7306        FND_MESSAGE.SET_NAME('AR','AR_RAPI_REF_NUM_INVALID');
7307        FND_MSG_PUB.Add;
7308        p_return_status := FND_API.G_RET_STS_ERROR;
7309    END IF;
7310 
7311   END IF;
7312 
7313  ELSE
7314    IF  (p_reference_num IS NOT NULL) THEN
7315 
7316        --give a warning message to indicate that the reference number
7317        -- entered by the user have been ignored.
7318  --    This warning message is coming as an error in the form. So commenting out NOCOPY
7319 
7320 /*    IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
7321         THEN
7322          FND_MESSAGE.SET_NAME('AR','AR_RAPI_REF_NUM_IGN');
7323          FND_MSG_PUB.Add;
7324        END IF;
7325 */
7326        null;
7327 
7328    END IF;
7329  END IF;
7330 
7331 
7332 END Default_misc_ids;
7333 
7334 PROCEDURE Get_misc_defaults(
7335               p_currency_code                IN OUT NOCOPY  VARCHAR2,
7336               p_exchange_rate_type           IN OUT NOCOPY  VARCHAR2,
7337               p_exchange_rate                IN OUT NOCOPY  NUMBER,
7338               p_exchange_date                IN OUT NOCOPY  DATE,
7339               p_amount                       IN OUT NOCOPY  NUMBER,
7340               p_receipt_date                 IN OUT NOCOPY  DATE,
7341               p_gl_date                      IN OUT NOCOPY  DATE,
7342               p_remittance_bank_account_id   IN OUT NOCOPY  NUMBER,
7343               p_deposit_date                 IN OUT NOCOPY  DATE,
7344               p_state                        IN OUT NOCOPY  VARCHAR2,
7345               p_distribution_set_id          IN OUT NOCOPY  NUMBER,
7346               p_vat_tax_id                   IN OUT NOCOPY  NUMBER,
7347               p_tax_rate                     IN OUT NOCOPY  NUMBER,
7348               p_receipt_method_id            IN      NUMBER,
7349               p_receivables_trx_id           IN      NUMBER,
7350               p_tax_code                     IN      VARCHAR2,
7351               p_tax_amount                   IN      NUMBER,
7352               p_creation_method_code            OUT NOCOPY  VARCHAR2,
7353               p_return_status                   OUT NOCOPY  VARCHAR2
7354                         )
7355 IS
7356  l_def_curr_return_status   VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
7357  l_def_rm_return_status     VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
7358  l_def_gl_dt_return_status  VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
7359  l_tax_rt_amt_x_ret_status  VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
7360  l_tax_amount               NUMBER;
7361  /* Bug fix 3315058 */
7362  l_bank_acc_val_ret_status   VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
7363  l_misc_activity_status      VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
7364  l_misc_tax_status           VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
7365  l_code_combination_id	     NUMBER;
7366  l_message		     VARCHAR2(100);
7367  l_le_id                     NUMBER;  /* bug 4594101 */
7368  l_get_id_return_status       VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
7369  l_zx_msg_count                 NUMBER;
7370  l_zx_msg_data                  VARCHAR2(1024);
7371  l_zx_effective_date            DATE;
7372  l_zx_return_status             VARCHAR2(10);
7373  l_tax_class			 VARCHAR2(20);
7374 
7375  /*Bug 5598297 */
7376  l_source_code ar_receivables_trx.gl_account_source%type;
7377  l_dist_set_id ar_receivables_trx.default_acctg_distribution_set%type;
7378 
7379      CURSOR get_dist_ccid(p_dist_id NUMBER) IS
7380      SELECT dist_code_combination_id
7381      FROM ar_distribution_set_lines
7382      WHERE distribution_set_id  = p_distribution_set_id;
7383 BEGIN
7384    IF PG_DEBUG in ('Y', 'C') THEN
7385       arp_util.debug('Get_misc_Defaults()+ ');
7386    END IF;
7387    p_return_status := FND_API.G_RET_STS_SUCCESS;
7388   -- default the receipt date if NULL
7389   IF (p_receipt_date IS NULL)
7390     THEN
7391     Select trunc(sysdate)
7392     into p_receipt_date
7393     from dual;
7394   END IF;
7395 
7396   -- default the gl_date
7397   IF p_gl_date IS NULL THEN
7398     Default_gl_date(p_receipt_date,
7399                     p_gl_date,
7400                     NULL,
7401                     l_def_gl_dt_return_status);
7402     IF PG_DEBUG in ('Y', 'C') THEN
7403        arp_util.debug('Get_misc_defaults: ' || 'l_default_gl_date_return_status : '||l_def_gl_dt_return_status);
7404     END IF;
7405   END IF;
7406 
7407   IF (p_deposit_date IS NULL)
7408     THEN
7409     p_deposit_date := p_receipt_date;
7410   END IF;
7411 
7412 
7413  -- Default the Currency parameters
7414     Default_Currency_info(p_currency_code,
7415                           p_receipt_date,
7416                           p_exchange_date,
7417                           p_exchange_rate_type,
7418                           p_exchange_rate,
7419                           l_def_curr_return_status
7420                          );
7421 
7422  --Set the precision of the receipt amount as per currency
7423   IF p_amount is NOT NULL THEN
7424    p_amount := arp_util.CurrRound( p_amount,
7425                                    p_currency_code
7426                                   );
7427   END IF;
7428 
7429  --Default the Receipt Method related parameters
7430    Default_Receipt_Method_info(p_receipt_method_id,
7431                                p_currency_code,
7432                                p_receipt_date,
7433                                p_remittance_bank_account_id,
7434                                p_state,
7435                                p_creation_method_code,
7436                                'MISC',
7437                                l_def_rm_return_status
7438                                 );
7439 
7440   /* Bug fix 3315058 */
7441    Validate_Receipt_Method_ccid(p_receipt_method_id,
7442                                 p_remittance_bank_account_id,
7443                                 p_gl_date,
7444                                 l_bank_acc_val_ret_status);
7445 
7446   /*------------------------------------------+
7447    |  Get legal_entity_id                     |
7448    +------------------------------------------*/
7449    l_le_id :=
7450          ar_receipt_lib_pvt.get_legal_entity(p_remittance_bank_account_id);
7451 
7452    /* 5955921 Remittance_bank_account info is not passed by the user then the system will not default
7453    the vat_tax_id based on the user tax_code. Hence moving the below set_tax_security_context
7454    from Default_misc_ids to Get_misc_defaults procedure.  */
7455    zx_api_pub.set_tax_security_context(
7456            p_api_version      => 1.0,
7457            p_init_msg_list    => 'T',
7458            p_commit           => 'F',
7459            p_validation_level => NULL,
7460            x_return_status    => l_zx_return_status,
7461            x_msg_count        => l_zx_msg_count,
7462            x_msg_data         => l_zx_msg_data,
7463            p_internal_org_id  => arp_standard.sysparm.org_id,
7464            p_legal_entity_id  => l_le_id,
7465            p_transaction_date => p_receipt_date,
7466            p_related_doc_date => NULL,
7467            p_adjusted_doc_date=> NULL,
7468            x_effective_date   => l_zx_effective_date);
7469  /* End - init */
7470 
7471 -- Moved tax default below remittance bank account
7472 -- so we could have LE and init ZX beforehand
7473 -- ETAX: Bug 4594101:  Added p_receipt_date to call.
7474 --Vat Tax Id, Tax code
7475  IF p_vat_tax_id IS NULL THEN
7476    IF p_tax_code IS NOT NULL THEN
7477      p_vat_tax_id := Get_Id('TAX_CODE',
7478                              p_tax_code,
7479                              l_get_id_return_status,
7480                              p_receipt_date
7481                               );
7482      IF p_vat_tax_id IS NULL THEN
7483        FND_MESSAGE.SET_NAME('AR','AR_RAPI_TAX_CODE_INVALID');
7484        FND_MSG_PUB.Add;
7485        p_return_status := FND_API.G_RET_STS_ERROR;
7486      END IF;
7487    END IF;
7488  ELSE
7489     IF (p_tax_code IS NOT NULL) THEN
7490        --give a warning message to indicate that the tax_code
7491        --entered by the user has been ignored.
7492        IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
7493         THEN
7494          FND_MESSAGE.SET_NAME('AR','AR_RAPI_TAX_CODE_IGN');
7495          FND_MSG_PUB.Add;
7496        END IF;
7497     END IF;
7498 
7499  END IF;
7500 
7501  --Default the vat_tax_id from the receivable_trx_id.
7502 
7503   IF ( p_receivables_trx_id IS NOT NULL AND p_vat_tax_id IS NULL  AND
7504      p_tax_code IS NULL)  THEN --the second condition reflects that the
7505                                --user had not entered any tax code
7506 
7507        If (p_amount >=0 )then
7508           l_tax_class := 'OUTPUT';
7509        else
7510           l_tax_class := 'INPUT';
7511        end if;
7512 
7513     IF (arp_legal_entity_util.Is_LE_Subscriber) THEN
7514        BEGIN
7515 
7516          /* 5236782 - made details not required for misc receipts */
7517          SELECT vat.tax_rate_id
7518            INTO   p_vat_tax_id
7519            FROM   ar_receivables_trx rt,
7520                   ar_rec_trx_le_details details,
7521                   zx_sco_rates vat
7522            WHERE  rt.receivables_trx_id = p_receivables_trx_id
7523              AND  rt.receivables_trx_id = details.receivables_trx_id (+)
7524              AND  details.legal_entity_id (+) = l_le_id
7525              AND  rt.type in ('MISCCASH', 'BANK_ERROR')
7526              AND  nvl(rt.status, 'A') = 'A'
7527              AND  vat.tax_rate_code = decode(sign(p_amount),
7528                                          1, nvl(details.asset_tax_code,
7529                                                 rt.asset_tax_code),
7530                                          0, nvl(details.asset_tax_code,
7531                                                 rt.asset_tax_code),
7532                                         -1, nvl(details.liability_tax_code,
7533                                                 rt.liability_tax_code))
7534              AND  nvl(vat.tax_class,l_tax_class) = l_tax_class
7535 	     AND  nvl(vat.active_flag, 'Y') = 'Y'		/* 4400063 */
7536              AND  p_receipt_date between
7537                          nvl(vat.effective_from, p_receipt_date)
7538                      and nvl(vat.effective_to, p_receipt_date);
7539 
7540        EXCEPTION
7541          WHEN no_data_found THEN
7542           null;
7543          WHEN others THEN
7544           raise;
7545        END;
7546     ELSE
7547        BEGIN
7548          SELECT vat.tax_rate_id
7549            INTO   p_vat_tax_id
7550            FROM   ar_receivables_trx rt,
7551                   zx_sco_rates vat
7552            WHERE  rt.receivables_trx_id = p_receivables_trx_id
7553              AND  rt.type in ('MISCCASH', 'BANK_ERROR')
7554              AND  nvl(rt.status, 'A') = 'A'
7555              AND  vat.tax_rate_code(+) = decode(sign(p_amount),
7556                                                 1, rt.asset_tax_code,
7557                                                 0, rt.asset_tax_code,
7558                                                -1, rt.liability_tax_code)
7559              AND  nvl(vat.tax_class,l_tax_class) = l_tax_class
7560 	     AND  nvl(vat.active_flag, 'Y') = 'Y'
7561              AND  p_receipt_date between
7562                          nvl(vat.effective_from, p_receipt_date)
7563                      and nvl(vat.effective_to, p_receipt_date);
7564 
7565        EXCEPTION
7566          WHEN no_data_found THEN
7567           null;
7568          WHEN others THEN
7569           raise;
7570        END;
7571     END IF;
7572 
7573   END IF;
7574 
7575 /*Bug3315058 After getting vat tax id and receivables trx id */
7576 
7577    /*Bug 5598297 distribution set id was never initialized */
7578    IF p_distribution_set_id is NULL THEN
7579      Begin
7580          select gl_account_source,default_acctg_distribution_set into
7581                 l_source_code, l_dist_set_id
7582          from ar_receivables_trx
7583          where  receivables_trx_id = nvl(p_receivables_trx_id,-99);
7584          IF l_source_code = 'DISTRIBUTION_SET' THEN
7585             p_distribution_set_id := l_dist_set_id;
7586          ELSE
7587             p_distribution_set_id := NULL;
7588          END IF;
7589      Exception
7590      when others then
7591        null;
7592      END;
7593    END IF;
7594    IF p_receivables_trx_id IS NOT NULL THEN
7595       IF p_distribution_set_id IS NULL THEN
7596 	  BEGIN
7597 	      select code_combination_id
7598                 into l_code_combination_id
7599  	        from ar_receivables_trx
7600                where receivables_trx_id=p_receivables_trx_id;
7601 
7602               l_message := 'AR_ACT_GL_ACC_ASSGN';
7603               validate_cc_segments(l_code_combination_id,
7604                                    p_gl_date,
7605                                    l_message,
7606                                    l_misc_activity_status);
7607 	   EXCEPTION
7608 	      WHEN OTHERS THEN
7609 		l_code_combination_id:=NULL;
7610 		l_misc_activity_status:=FND_API.G_RET_STS_ERROR;
7611 	   END;
7612       ELSE
7613           l_message := 'AR_GL_ACC_ASSGN_DISB_SET';
7614           OPEN get_dist_ccid(p_distribution_set_id);
7615 	  IF get_dist_ccid%NOTFOUND THEN
7616 	     l_misc_activity_status:=FND_API.G_RET_STS_ERROR;
7617 	     l_code_combination_id:=NULL;
7618 	  ELSE
7619              LOOP
7620                 FETCH  get_dist_ccid INTO l_code_combination_id;
7621                 EXIT WHEN get_dist_ccid%NOTFOUND;
7622                 validate_cc_segments(l_code_combination_id,
7623                                      p_gl_date,
7624                                      l_message,
7625                                      l_misc_activity_status);
7626 		IF l_misc_activity_status <> FND_API.G_RET_STS_SUCCESS THEN
7627 	     	    l_misc_activity_status:=FND_API.G_RET_STS_ERROR;
7628 		    EXIT;
7629 		END IF;
7630              END LOOP;
7631 	  END IF;
7632           CLOSE get_dist_ccid;
7633       END IF;
7634       IF p_vat_tax_id IS NOT NULL THEN
7635          BEGIN
7636 
7637             l_code_combination_id := arp_etax_util.get_tax_account(
7638                             p_subject_id      => p_vat_tax_id,
7639                             p_gl_date         => p_gl_date,
7640                             p_desired_account => 'TAX',
7641                             p_subject_table   => 'TAX_RATE');
7642 
7643    	    l_message := 'AR_TAX_ACC_ASSGN';
7644    	    validate_cc_segments(l_code_combination_id,
7645                                  p_gl_date,
7646                                  l_message,
7647                                  l_misc_tax_status);
7648         EXCEPTION
7649 	WHEN OTHERS THEN
7650            l_code_combination_id := NULL;
7651 	   l_misc_tax_status:=FND_API.G_RET_STS_ERROR;
7652         END;
7653       END IF;
7654   END IF;
7655   /* End of Bug3315058*/
7656    IF p_tax_amount IS NOT NULL THEN
7657 
7658       l_tax_amount := arp_util.CurrRound( p_tax_amount,
7659                                         p_currency_code
7660                                       );
7661    END IF;
7662 
7663   --Derive the tax rate from the tax_amount and the total receipt amount
7664   --also do the cross validation between the tax_amount,tax_rate and amount.
7665 
7666    IF p_tax_rate   IS NULL and
7667       l_tax_amount IS NOT NULL and
7668       p_amount     IS NOT NULL THEN
7669 
7670       p_tax_rate := Round(
7671                       (100 * (l_tax_amount/p_amount)) / ( 1 - (l_tax_amount/p_amount)),
7672                         3);
7673    ELSIF p_tax_rate   IS NOT NULL and
7674          l_tax_amount IS NOT NULL and
7675          p_amount     IS NOT NULL THEN
7676 
7677          IF round(p_tax_rate*p_amount,3) <> p_tax_amount THEN
7678             l_tax_rt_amt_x_ret_status := FND_API.G_RET_STS_ERROR;
7679             FND_MESSAGE.SET_NAME('AR','AR_RAPI_TAX_RATE_AMT_X_INVALID');
7680             FND_MSG_PUB.Add;
7681          END IF;
7682    END IF;
7683 
7684 
7685   IF l_def_rm_return_status <> FND_API.G_RET_STS_SUCCESS OR
7686      l_def_gl_dt_return_status <> FND_API.G_RET_STS_SUCCESS OR
7687      l_def_curr_return_status <> FND_API.G_RET_STS_SUCCESS  OR
7688      l_tax_rt_amt_x_ret_status <> FND_API.G_RET_STS_SUCCESS OR
7689      l_misc_activity_status <> FND_API.G_RET_STS_SUCCESS OR      /* Bug fix 3315058 */
7690      l_misc_tax_status <> FND_API.G_RET_STS_SUCCESS OR
7691      l_bank_acc_val_ret_status <>  FND_API.G_RET_STS_SUCCESS THEN /* Bug fix 3315058 */
7692        p_return_status := FND_API.G_RET_STS_ERROR;
7693   END IF;
7694   IF PG_DEBUG in ('Y', 'C') THEN
7695      arp_util.debug('Get_misc_defaults: ' || '************Cash Defaults********************');
7696      arp_util.debug('Get_misc_defaults: ' || 'p_receipt_date               : '||to_char(p_receipt_date,'DD-MON-YYYY'));
7697      arp_util.debug('Get_misc_defaults: ' || 'p_gl_date                    : '||to_char(p_gl_date,'DD-MON-YYYY'));
7698      arp_util.debug('Get_misc_defaults: ' || 'p_deposit_date               : '||to_char(p_deposit_date,'DD-MON-YYYY'));
7699      arp_util.debug('Get_misc_defaults: ' || 'p_currency_code              : '||p_currency_code);
7700      arp_util.debug('Get_misc_defaults: ' || 'p_exchange_rate_date         : '||to_char(p_exchange_date,'DD-MON-YYYY'));
7701      arp_util.debug('Get_misc_defaults: ' || 'p_exchange_rate_type         : '||p_exchange_rate_type);
7702      arp_util.debug('Get_misc_defaults: ' || 'p_exchange_rate              : '||to_char(p_exchange_rate));
7703      arp_util.debug('Get_misc_defaults: ' || 'p_receipt_method_id          : '||to_char(p_receipt_method_id));
7704      arp_util.debug('Get_misc_defaults: ' || 'p_remittance_bank_account_id : '||to_char(p_remittance_bank_account_id));
7705      arp_util.debug('Get_misc_defaults: ' || 'p_state                      : '||p_state);
7706      arp_util.debug('Get_misc_Defaults ()-');
7707   END IF;
7708 
7709 END Get_misc_defaults;
7710 
7711 PROCEDURE Validate_Desc_Flexfield(
7712                           p_desc_flex_rec       IN OUT NOCOPY  ar_receipt_api_pub.attribute_rec_type,
7713                           p_desc_flex_name      IN VARCHAR2,
7714                           p_return_status       IN OUT NOCOPY  varchar2
7715                          ) IS
7716 
7717 l_flex_name     fnd_descriptive_flexs.descriptive_flexfield_name%type;
7718 l_count         NUMBER;
7719 l_col_name     VARCHAR2(50);
7720 l_flex_exists  VARCHAR2(1);
7721 CURSOR desc_flex_exists IS
7722   SELECT 'Y'
7723   FROM fnd_descriptive_flexs
7724   WHERE application_id = 222
7725     and descriptive_flexfield_name = p_desc_flex_name;
7726 BEGIN
7727     IF PG_DEBUG in ('Y', 'C') THEN
7728        arp_util.debug('Validate_Desc_Flexfield ()+');
7729     END IF;
7730       p_return_status := FND_API.G_RET_STS_SUCCESS;
7731 
7732       OPEN desc_flex_exists;
7733       FETCH desc_flex_exists INTO l_flex_exists;
7734       IF desc_flex_exists%NOTFOUND THEN
7735        CLOSE desc_flex_exists;
7736         FND_MESSAGE.SET_NAME('AR', 'AR_RAPI_DESC_FLEX_INVALID');
7737         FND_MESSAGE.SET_TOKEN('DFF_NAME',p_desc_flex_name);
7738         FND_MSG_PUB.ADD ;
7739         p_return_status :=  FND_API.G_RET_STS_ERROR;
7740        return;
7741       END IF;
7742       CLOSE desc_flex_exists;
7743 
7744 
7745      fnd_flex_descval.set_context_value(p_desc_flex_rec.attribute_category);
7746 
7747      fnd_flex_descval.set_column_value('ATTRIBUTE1', p_desc_flex_rec.attribute1);
7748      fnd_flex_descval.set_column_value('ATTRIBUTE2', p_desc_flex_rec.attribute2);
7749      fnd_flex_descval.set_column_value('ATTRIBUTE3', p_desc_flex_rec.attribute3);
7750      fnd_flex_descval.set_column_value('ATTRIBUTE4', p_desc_flex_rec.attribute4);
7751      fnd_flex_descval.set_column_value('ATTRIBUTE5', p_desc_flex_rec.attribute5);
7752      fnd_flex_descval.set_column_value('ATTRIBUTE6', p_desc_flex_rec.attribute6);
7753      fnd_flex_descval.set_column_value('ATTRIBUTE7', p_desc_flex_rec.attribute7);
7754      fnd_flex_descval.set_column_value('ATTRIBUTE8', p_desc_flex_rec.attribute8);
7755      fnd_flex_descval.set_column_value('ATTRIBUTE9', p_desc_flex_rec.attribute9);
7756      fnd_flex_descval.set_column_value('ATTRIBUTE10', p_desc_flex_rec.attribute10);
7757      fnd_flex_descval.set_column_value('ATTRIBUTE11',p_desc_flex_rec.attribute11);
7758      fnd_flex_descval.set_column_value('ATTRIBUTE12', p_desc_flex_rec.attribute12);
7759      fnd_flex_descval.set_column_value('ATTRIBUTE13', p_desc_flex_rec.attribute13);
7760      fnd_flex_descval.set_column_value('ATTRIBUTE14', p_desc_flex_rec.attribute14);
7761      fnd_flex_descval.set_column_value('ATTRIBUTE15', p_desc_flex_rec.attribute15);
7762     /* Bugfix 2531340 */
7763     IF ( NOT fnd_flex_descval.validate_desccols('AR',p_desc_flex_name,'I') )
7764      THEN
7765 
7766        FND_MESSAGE.SET_NAME('AR', 'AR_RAPI_DESC_FLEX_INVALID');
7767        FND_MESSAGE.SET_TOKEN('DFF_NAME',p_desc_flex_name);
7768        FND_MSG_PUB.ADD ;
7769        p_return_status := FND_API.G_RET_STS_ERROR;
7770     END IF;
7771 
7772       l_count := fnd_flex_descval.segment_count;
7773 
7774       FOR i in 1..l_count LOOP
7775         l_col_name := fnd_flex_descval.segment_column_name(i);
7776 
7777         /* Bug fix 3184559
7778            The calls to fnd_flex_descval.segment_value(i) are replaced with
7779            fnd_flex_descval.segment_id(i)*/
7780         IF l_col_name = 'ATTRIBUTE1' THEN
7781           p_desc_flex_rec.attribute1 := fnd_flex_descval.segment_id(i);
7782         ELSIF l_col_name = 'ATTRIBUTE_CATEGORY'  THEN
7783           /* Bugfix 2531340 */
7784           p_desc_flex_rec.attribute_category := fnd_flex_descval.segment_id(i);
7785         ELSIF l_col_name = 'ATTRIBUTE2' THEN
7786           p_desc_flex_rec.attribute2 := fnd_flex_descval.segment_id(i);
7787         ELSIF l_col_name = 'ATTRIBUTE3' THEN
7788           p_desc_flex_rec.attribute3 := fnd_flex_descval.segment_id(i);
7789         ELSIF l_col_name = 'ATTRIBUTE4' THEN
7790           p_desc_flex_rec.attribute4 := fnd_flex_descval.segment_id(i);
7791         ELSIF l_col_name = 'ATTRIBUTE5' THEN
7792           p_desc_flex_rec.attribute5 := fnd_flex_descval.segment_id(i);
7793         ELSIF l_col_name = 'ATTRIBUTE6' THEN
7794           p_desc_flex_rec.attribute6 := fnd_flex_descval.segment_id(i);
7795         ELSIF l_col_name = 'ATTRIBUTE7' THEN
7796           p_desc_flex_rec.attribute7 := fnd_flex_descval.segment_id(i);
7797         ELSIF l_col_name = 'ATTRIBUTE8' THEN
7798           p_desc_flex_rec.attribute8 := fnd_flex_descval.segment_id(i);
7799         ELSIF l_col_name = 'ATTRIBUTE9' THEN
7800           p_desc_flex_rec.attribute9 := fnd_flex_descval.segment_id(i);
7801         ELSIF l_col_name = 'ATTRIBUTE10' THEN
7802           p_desc_flex_rec.attribute10 := fnd_flex_descval.segment_id(i);
7803         ELSIF l_col_name = 'ATTRIBUTE11' THEN
7804           p_desc_flex_rec.attribute11 := fnd_flex_descval.segment_id(i);
7805         ELSIF l_col_name = 'ATTRIBUTE12' THEN
7806           p_desc_flex_rec.attribute12 := fnd_flex_descval.segment_id(i);
7807         ELSIF l_col_name = 'ATTRIBUTE13' THEN
7808           p_desc_flex_rec.attribute13 := fnd_flex_descval.segment_id(i);
7809         ELSIF l_col_name = 'ATTRIBUTE14' THEN
7810           p_desc_flex_rec.attribute14 := fnd_flex_descval.segment_id(i);
7811         ELSIF l_col_name = 'ATTRIBUTE15' THEN
7812           p_desc_flex_rec.attribute15 := fnd_flex_descval.segment_id(i);
7813         END IF;
7814 
7815         IF i > l_count  THEN
7816           EXIT;
7817         END IF;
7818        END LOOP;
7819 
7820         IF PG_DEBUG in ('Y', 'C') THEN
7821            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute_category  : '||p_desc_flex_rec.attribute_category);
7822            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute1          : '||p_desc_flex_rec.attribute1);
7823            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute2          : '||p_desc_flex_rec.attribute2);
7824            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute3          : '||p_desc_flex_rec.attribute3);
7825            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute4          : '||p_desc_flex_rec.attribute4);
7826            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute5          : '||p_desc_flex_rec.attribute5);
7827            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute6          : '||p_desc_flex_rec.attribute6);
7828            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute7          : '||p_desc_flex_rec.attribute7);
7829            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute8          : '||p_desc_flex_rec.attribute8);
7830            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute9          : '||p_desc_flex_rec.attribute9);
7831            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute10         : '||p_desc_flex_rec.attribute10);
7832            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute11         : '||p_desc_flex_rec.attribute11);
7833            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute12         : '||p_desc_flex_rec.attribute12);
7834            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute13         : '||p_desc_flex_rec.attribute13);
7835            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute14         : '||p_desc_flex_rec.attribute14);
7836            arp_util.debug('Validate_Desc_Flexfield: ' || 'attribute15         : '||p_desc_flex_rec.attribute15);
7837       arp_util.debug('Validate_Desc_Flexfield ()-');
7838    END IF;
7839 END Validate_Desc_Flexfield;
7840 
7841 /* Bug fix 3539008 */
7842 PROCEDURE Default_Desc_Flexfield(
7843               p_desc_flex_rec                OUT NOCOPY  ar_receipt_api_pub.attribute_rec_type,
7844               p_cash_receipt_id              IN      NUMBER,
7845               p_return_status                IN OUT NOCOPY  VARCHAR2
7846                        ) IS
7847 BEGIN
7848    IF PG_DEBUG in ('Y', 'C') THEN
7849       arp_util.debug('ar_receipt_lib_pvt.Default_Desc_Flexfield(+)');
7850    END IF;
7851     p_return_status := FND_API.G_RET_STS_SUCCESS;
7852    IF p_cash_receipt_id IS NOT NULL THEN
7853         SELECT attribute_category,
7854                attribute1, attribute2,
7855                attribute3, attribute4,
7856                attribute5, attribute6,
7857                attribute7, attribute8,
7858                attribute9, attribute10,
7859                attribute11, attribute12,
7860                attribute13, attribute14,
7861                attribute15
7862         INTO   p_desc_flex_rec.attribute_category,
7863                p_desc_flex_rec.attribute1, p_desc_flex_rec.attribute2,
7864                p_desc_flex_rec.attribute3, p_desc_flex_rec.attribute4,
7865                p_desc_flex_rec.attribute5, p_desc_flex_rec.attribute6,
7866                p_desc_flex_rec.attribute7, p_desc_flex_rec.attribute8,
7867                p_desc_flex_rec.attribute9, p_desc_flex_rec.attribute10,
7868                p_desc_flex_rec.attribute11, p_desc_flex_rec.attribute12,
7869                p_desc_flex_rec.attribute13, p_desc_flex_rec.attribute14,
7870                p_desc_flex_rec.attribute15
7871        FROM   ar_cash_receipts
7872        WHERE  cash_receipt_id = p_cash_receipt_id;
7873    END IF;
7874    IF PG_DEBUG in ('Y', 'C') THEN
7875       arp_util.debug('ar_receipt_lib_pvt.Default_Desc_Flexfield(-)');
7876    END IF;
7877 
7878 EXCEPTION WHEN OTHERS THEN
7879         FND_MESSAGE.SET_NAME('AR','AR_RAPI_CASH_RCPT_ID_INVALID');
7880         FND_MSG_PUB.Add;
7881         p_return_status := FND_API.G_RET_STS_ERROR;
7882 End Default_Desc_Flexfield;
7883 /* End bug fix 3539008 */
7884 
7885 PROCEDURE Default_prepay_cc_activity(
7886               p_appl_type                    IN      VARCHAR2,
7887               p_receivable_trx_id            IN OUT NOCOPY  NUMBER,
7888               p_return_status                OUT NOCOPY     VARCHAR2
7889             ) IS
7890 BEGIN
7891    IF PG_DEBUG in ('Y', 'C') THEN
7892       arp_util.debug('ar_receipt_lib_pvt.Default_prepay_cc_activity(+)');
7893    END IF;
7894 
7895    p_return_status := FND_API.G_RET_STS_SUCCESS;
7896 
7897    IF p_receivable_trx_id is null THEN
7898 
7899     BEGIN
7900       SELECT rt.receivables_trx_id
7901       INTO   p_receivable_trx_id
7902       FROM   ar_receivables_trx rt
7903       WHERE   nvl(rt.status,'A') = 'A'
7904       AND trunc(sysdate) between nvl(rt.start_date_active,trunc(sysdate))
7905       and nvl(rt.end_date_active,trunc(sysdate))
7906       AND rt.type = p_appl_type
7907       AND ROWNUM = 1;
7908 
7909     EXCEPTION
7910       WHEN others THEN
7911            fnd_message.set_name('AR', 'AR_NO_ACTIVITY_FOUND');
7912            p_return_status := FND_API.G_RET_STS_ERROR;
7913     END;
7914   END IF;
7915 
7916   IF PG_DEBUG in ('Y', 'C') THEN
7917      arp_util.debug('ar_receipt_lib_pvt.Default_prepay_cc_activity(+)');
7918   END IF;
7919 
7920 END Default_prepay_cc_activity;
7921 
7922 PROCEDURE default_open_receipt(
7923               p_cash_receipt_id          IN OUT NOCOPY NUMBER
7924             , p_receipt_number           IN OUT NOCOPY VARCHAR2
7925             , p_applied_ps_id            IN OUT NOCOPY NUMBER
7926             , p_open_cash_receipt_id     IN OUT NOCOPY NUMBER
7927             , p_open_receipt_number      IN OUT NOCOPY VARCHAR2
7928             , p_apply_gl_date            IN OUT NOCOPY DATE
7929             , p_open_rec_app_id          IN NUMBER
7930             , x_cr_payment_schedule_id   OUT NOCOPY NUMBER
7931             , x_last_receipt_date        OUT NOCOPY DATE
7932             , x_open_applied_ps_id       OUT NOCOPY NUMBER
7933             , x_unapplied_cash           OUT NOCOPY NUMBER
7934             , x_open_amount_applied      OUT NOCOPY NUMBER
7935             , x_claim_rec_trx_id         OUT NOCOPY NUMBER
7936             , x_application_ref_num      OUT NOCOPY VARCHAR2
7937             , x_secondary_app_ref_id     OUT NOCOPY NUMBER
7938             , x_application_ref_reason   OUT NOCOPY VARCHAR2
7939             , x_customer_reference       OUT NOCOPY VARCHAR2
7940             , x_customer_reason          OUT NOCOPY VARCHAR2
7941             , x_cr_gl_date               OUT NOCOPY DATE
7942             , x_open_cr_gl_date          OUT NOCOPY DATE
7943             , x_receipt_currency         OUT NOCOPY VARCHAR2
7944             , x_open_receipt_currency    OUT NOCOPY VARCHAR2
7945             , x_cr_customer_id           OUT NOCOPY NUMBER
7946             , x_open_cr_customer_id      OUT NOCOPY NUMBER
7947             , x_return_status            OUT NOCOPY VARCHAR2
7948 ) IS
7949 
7950   l_open_cash_receipt_id        NUMBER;
7951   l_receipt_date                DATE;
7952   l_open_receipt_date           DATE;
7953   l_app_gl_date_prof            VARCHAR2(240);
7954 
7955   l_cr_amount                   NUMBER;
7956   l_cr_exchange_rate            NUMBER;
7957   l_cr_cust_site_use_id         NUMBER;
7958   l_cr_unapp_amount             NUMBER;
7959   l_cr_payment_schedule_id      NUMBER;
7960   l_remit_bank_acct_use_id      NUMBER;
7961   l_receipt_method_id           NUMBER;
7962   l_status                      ar_receivable_applications.status%TYPE;
7963   l_display			ar_receivable_applications.display%TYPE;
7964 
7965 BEGIN
7966 
7967   IF PG_DEBUG in ('Y', 'C') THEN
7968      arp_util.debug('ar_receipt_lib_pvt.Default_open_receipt(+)');
7969   END IF;
7970 
7971   x_return_status := FND_API.G_RET_STS_SUCCESS ;
7972   x_open_applied_ps_id := NULL;
7973   x_application_ref_num := NULL;
7974   x_secondary_app_ref_id := NULL;
7975 
7976   default_cash_receipt_id
7977         (p_cash_receipt_id,
7978          p_receipt_number,
7979          x_return_status);
7980 
7981   IF p_applied_ps_id IS NOT NULL
7982   THEN
7983    BEGIN
7984     SELECT cash_receipt_id INTO p_open_cash_receipt_id
7985     FROM   ar_payment_schedules
7986     WHERE  payment_schedule_id = p_applied_ps_id;
7987    EXCEPTION when others then
7988      --raise error message
7989      FND_MESSAGE.SET_NAME('AR','AR_RAPI_APP_PS_ID_INVALID');
7990      FND_MSG_PUB.Add;
7991      x_return_status := FND_API.G_RET_STS_ERROR;
7992    END;
7993   END IF;
7994 
7995   IF p_open_rec_app_id IS NOT NULL THEN
7996 
7997      BEGIN
7998        SELECT  applied_payment_schedule_id,
7999                amount_applied,
8000                cash_receipt_id,
8001                receivables_trx_id,
8002                secondary_application_ref_id,
8003                application_ref_num,
8004                application_ref_reason,
8005                customer_reference,
8006                customer_reason,
8007 	       status,
8008 	       NVL(display,'N')
8009        INTO    x_open_applied_ps_id,
8010                x_open_amount_applied,
8011                p_open_cash_receipt_id ,
8012                x_claim_rec_trx_id,
8013                x_secondary_app_ref_id,
8014                x_application_ref_num,
8015                x_application_ref_reason,
8016                x_customer_reference,
8017                x_customer_reason,
8018 	       l_status,
8019 	       l_display
8020        FROM    ar_receivable_applications
8021        WHERE   receivable_application_id = p_open_rec_app_id ;
8022      EXCEPTION
8023         WHEN no_data_found THEN
8024            FND_MESSAGE.SET_NAME('AR','AR_RAPI_REC_APP_ID_INVALID');
8025            FND_MSG_PUB.Add;
8026            x_return_status := FND_API.G_RET_STS_ERROR ;
8027      END;
8028 
8029   default_cash_receipt_id
8030         (p_open_cash_receipt_id,
8031          p_open_receipt_number,
8032          x_return_status);
8033 
8034      /* Bug 3018366 - check for current open receipt separated from logic above */
8035      IF (l_display <> 'Y' OR l_status NOT IN ('OTHER ACC','ACC')) THEN
8036            FND_MESSAGE.SET_NAME('AR','AR_RW_NET_OPEN_RCT_ONLY');
8037            FND_MSG_PUB.Add;
8038            x_return_status := FND_API.G_RET_STS_ERROR ;
8039      END IF;
8040 
8041      --Compare the two cash_receipt_ids
8042      IF p_open_cash_receipt_id IS NOT NULL THEN
8043         IF p_open_cash_receipt_id <> l_open_cash_receipt_id THEN
8044             --raise error X validation failed
8045             FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_RA_ID_X_INVALID');
8046             FND_MSG_PUB.Add;
8047             x_return_status := FND_API.G_RET_STS_ERROR ;
8048         END IF;
8049      END IF;
8050   END IF;
8051 
8052   IF x_return_status <> FND_API.G_RET_STS_ERROR THEN
8053      default_cash_receipt_id
8054         (p_open_cash_receipt_id,
8055          p_open_receipt_number,
8056          x_return_status);
8057   END IF;
8058 
8059   SELECT SUM(amount_applied)
8060   INTO   x_unapplied_cash
8061   FROM   ar_receivable_applications
8062   WHERE  cash_receipt_id = p_cash_receipt_id
8063   AND    status = 'UNAPP';
8064 
8065   IF p_open_rec_app_id IS NULL
8066   THEN
8067     SELECT SUM(amount_applied)
8068     INTO   x_open_amount_applied
8069     FROM   ar_receivable_applications
8070     WHERE  cash_receipt_id = p_open_cash_receipt_id
8071     AND    status = 'UNAPP';
8072   END IF;
8073 
8074   --
8075   -- Default receipt info
8076   --
8077   Default_Receipt_Info(
8078                                  p_cash_receipt_id ,
8079                                  x_cr_gl_date,
8080                                  x_cr_customer_id,
8081                                  l_cr_amount,
8082                                  x_receipt_currency,
8083                                  l_cr_exchange_rate,
8084                                  l_cr_cust_site_use_id,
8085                                  l_receipt_date,
8086                                  l_cr_unapp_amount,
8087                                  x_cr_payment_schedule_id,
8088                                  l_remit_bank_acct_use_id,
8089                                  l_receipt_method_id,
8090                                  x_return_status
8091                                   );
8092 
8093   Default_Receipt_Info(
8094                                  p_open_cash_receipt_id ,
8095                                  x_open_cr_gl_date,
8096                                  x_open_cr_customer_id,
8097                                  l_cr_amount,
8098                                  x_open_receipt_currency,
8099                                  l_cr_exchange_rate,
8100                                  l_cr_cust_site_use_id,
8101                                  l_open_receipt_date, /* Bug fix 3286069 */
8102                                  l_cr_unapp_amount,
8103                                  p_applied_ps_id,
8104                                  l_remit_bank_acct_use_id,
8105                                  l_receipt_method_id,
8106                                  x_return_status
8107                                   );
8108 
8109 
8110   IF p_apply_gl_date IS NULL
8111   THEN
8112     p_apply_gl_date := (GREATEST(x_cr_gl_date,x_open_cr_gl_date));
8113     l_app_gl_date_prof :=
8114           NVL(ar_receipt_lib_pvt.pg_profile_appln_gl_date_def,'INV_REC_DT');
8115     IF (l_app_gl_date_prof    = 'INV_REC_DT') THEN
8116       NULL;
8117     ELSIF (l_app_gl_date_prof = 'INV_REC_SYS_DT') THEN
8118       IF p_apply_gl_date > SYSDATE THEN
8119         NULL;
8120       ELSE
8121         p_apply_gl_date := SYSDATE;
8122       END IF;
8123     ELSE
8124       p_apply_gl_date := SYSDATE;
8125     END IF;
8126   END IF;
8127 
8128   x_last_receipt_date := GREATEST(l_receipt_date,l_open_receipt_date);
8129 
8130   IF PG_DEBUG in ('Y', 'C') THEN
8131      arp_util.debug('ar_receipt_lib_pvt.Default_open_receipt(-)');
8132   END IF;
8133 END default_open_receipt;
8134 
8135 PROCEDURE default_unapp_open_receipt(
8136               p_receivable_application_id  IN  NUMBER
8137             , x_applied_cash_receipt_id    OUT NOCOPY NUMBER
8138             , x_applied_rec_app_id         OUT NOCOPY NUMBER
8139             , x_amount_applied             OUT NOCOPY NUMBER
8140             , x_return_status              OUT NOCOPY VARCHAR2
8141 ) IS
8142 
8143 BEGIN
8144 
8145    x_return_status := FND_API.G_RET_STS_SUCCESS;
8146   IF PG_DEBUG in ('Y', 'C') THEN
8147      arp_util.debug('ar_receipt_lib_pvt.Default_unapp_open_receipt(+)');
8148   END IF;
8149 
8150   SELECT app.applied_rec_app_id,
8151          applied_app.cash_receipt_id,
8152          app.amount_applied
8153   INTO   x_applied_rec_app_id,
8154          x_applied_cash_receipt_id,
8155          x_amount_applied
8156   FROM   ar_receivable_applications app,
8157          ar_receivable_applications applied_app
8158   WHERE  app.applied_rec_app_id = applied_app.receivable_application_id
8159   AND    app.receivable_application_id = p_receivable_application_id;
8160 
8161   IF PG_DEBUG in ('Y', 'C') THEN
8162      arp_util.debug('ar_receipt_lib_pvt.Default_unapp_open_receipt(-)');
8163   END IF;
8164 
8165 EXCEPTION
8166         WHEN no_data_found THEN
8167            FND_MESSAGE.SET_NAME('AR','AR_RAPI_REC_APP_ID_INVALID');
8168            FND_MSG_PUB.Add;
8169            x_return_status := FND_API.G_RET_STS_ERROR ;
8170 END default_unapp_open_receipt;
8171 
8172 FUNCTION get_legal_entity (p_remit_bank_acct_use_id IN NUMBER)
8173 RETURN NUMBER
8174 IS
8175   l_legal_entity_id		NUMBER;
8176   l_return_status		VARCHAR2(1);
8177 BEGIN
8178   IF PG_DEBUG in ('Y', 'C') THEN
8179      arp_util.debug('ar_receipt_lib_pvt.get_legal_entity(+)');
8180   END IF;
8181 
8182   l_legal_entity_id := TO_NUMBER(Get_Id('LEGAL_ENTITY',
8183                                  p_remit_bank_acct_use_id,
8184                                  l_return_status
8185                                  ));
8186   RETURN l_legal_entity_id;
8187 
8188   IF PG_DEBUG in ('Y', 'C') THEN
8189      arp_util.debug('ar_receipt_lib_pvt.get_legal_entity(-)');
8190   END IF;
8191 EXCEPTION
8192   WHEN OTHERS THEN
8193      RETURN NULL;
8194 END get_legal_entity;
8195 
8196 PROCEDURE default_refund_attributes (
8197 	 p_cash_receipt_id IN ar_cash_receipts.cash_receipt_id%TYPE
8198 	,p_customer_trx_id IN ra_customer_trx.customer_trx_id%TYPE
8199 	,p_currency_code IN fnd_currencies.currency_code%TYPE
8200 	,p_amount IN NUMBER
8201 	,p_party_id IN OUT NOCOPY hz_parties.party_id%TYPE
8202 	,p_party_site_id IN OUT NOCOPY hz_party_sites.party_site_id%TYPE
8203 	,x_party_name OUT NOCOPY hz_parties.party_name%TYPE
8204 	,x_party_number OUT NOCOPY hz_parties.party_number%TYPE
8205 	,x_party_address OUT NOCOPY VARCHAR2
8206 	,x_exchange_rate OUT NOCOPY ar_cash_receipts.exchange_rate%TYPE
8207 	,x_exchange_rate_type OUT NOCOPY ar_cash_receipts.exchange_rate_type%TYPE
8208 	,x_exchange_date OUT NOCOPY ar_cash_receipts.exchange_date%TYPE
8209 	,x_legal_entity_id OUT NOCOPY ar_cash_receipts.legal_entity_id%TYPE
8210     	,x_payment_method_code OUT NOCOPY ap_invoices.payment_method_code%TYPE
8211     	,x_payment_method_name OUT NOCOPY VARCHAR2
8212     	,x_bank_account_id OUT NOCOPY ar_cash_receipts.customer_bank_account_id%TYPE
8213 
8214     	,x_bank_account_num OUT NOCOPY VARCHAR2
8215     	,x_payment_reason_code OUT NOCOPY ap_invoices.payment_reason_code%TYPE
8216     	,x_payment_reason_name OUT NOCOPY VARCHAR2
8217     	,x_delivery_channel_code OUT NOCOPY ap_invoices.delivery_channel_code%TYPE
8218     	,x_delivery_channel_name OUT NOCOPY VARCHAR2
8219     	,x_pay_alone_flag OUT NOCOPY VARCHAR2
8220 	,x_return_status OUT NOCOPY VARCHAR2
8221 	,x_msg_count OUT NOCOPY NUMBER
8222 	,x_msg_data OUT NOCOPY VARCHAR2
8223 	)
8224 IS
8225   l_trxn_attributes_rec		iby_disbursement_comp_pub.trxn_attributes_rec_type;
8226   l_legal_entity_id		ar_cash_receipts.legal_entity_id%TYPE;
8227   l_org_id			ar_cash_receipts.org_id%TYPE;
8228   l_cust_acct_id		hz_cust_accounts.cust_account_id%TYPE;
8229   l_site_use_id			hz_cust_site_uses.site_use_id%TYPE;
8230   l_party_id			hz_parties.party_id%TYPE;
8231   l_party_site_id		hz_parties.party_id%TYPE;
8232   l_pmt_attr_rec		iby_disbursement_comp_pub.default_pmt_attrs_rec_type;
8233 BEGIN
8234   IF PG_DEBUG in ('Y', 'C') THEN
8235      arp_util.debug('ar_receipt_lib_pvt.default_refund_attributes(+)');
8236   END IF;
8237     l_trxn_attributes_rec.application_id := 222;
8238 
8239     IF p_cash_receipt_id IS NOT NULL THEN
8240       begin
8241 	SELECT  legal_entity_id,
8242 		exchange_rate,
8243 		exchange_rate_type,
8244 		exchange_date,
8245 		org_id,
8246 		pay_from_customer,
8247 		customer_site_use_id
8248         INTO    x_legal_entity_id,
8249 		x_exchange_rate,
8250 		x_exchange_rate_type,
8251 		x_exchange_date,
8252 		l_org_id,
8253 		l_cust_acct_id,
8254 		l_site_use_id
8255         FROM 	ar_cash_receipts_all
8256 	WHERE	cash_receipt_id = p_cash_receipt_id;
8257       exception when others then null;
8258       end;
8259     ELSE
8260 	SELECT  legal_entity_id,
8261 		exchange_rate,
8262 		exchange_rate_type,
8263 		exchange_date,
8264 		org_id,
8265 		bill_to_customer_id,
8266 		bill_to_site_use_id
8267         INTO    x_legal_entity_id,
8268 		x_exchange_rate,
8269 		x_exchange_rate_type,
8270 		x_exchange_date,
8271 		l_org_id,
8272 		l_cust_acct_id,
8273 		l_site_use_id
8274         FROM 	ra_customer_trx
8275 	WHERE	customer_trx_id = p_customer_trx_id;
8276     END IF;
8277     IF p_party_id IS NULL THEN
8278        BEGIN
8279         SELECT p.party_id
8280 	      ,p.party_name
8281 	      ,p.party_number
8282  	INTO   p_party_id
8283 	      ,x_party_name
8284 	      ,x_party_number
8285 	FROM   hz_cust_accounts ca
8286               ,hz_parties p
8287 	WHERE  p.party_id = ca.party_id
8288 	AND    ca.cust_account_id = l_cust_acct_id;
8289       EXCEPTION
8290 	WHEN OTHERS THEN
8291 	   p_party_id := NULL;
8292 	   x_party_name := NULL;
8293 	   x_party_number := NULL;
8294       END;
8295     END IF;
8296     IF p_party_site_id IS NULL and p_party_id IS NOT NULL THEN
8297       begin
8298 	SELECT cas.party_site_id
8299 	 /*    , arh_addr_pkg.format_address(loc.address_style,loc.address1,
8300                                   	   loc.address2, loc.address3,
8301                             	           loc.address4, loc.city,
8302                                    	   loc.county, loc.state,
8303                                    	   loc.province, loc.postal_code,
8304                                    	   null)*/
8305                , loc.address1
8306 	INTO   p_party_site_id
8307               ,x_party_address
8308 	FROM   hz_cust_acct_sites_all cas,
8309 	       hz_cust_site_uses_all csu,
8310 	       hz_party_sites ps,
8311 	       hz_locations loc
8312 	WHERE  cas.cust_acct_site_id = csu.cust_acct_site_id
8313 	AND    csu.site_use_id = l_site_use_id
8314 	AND    cas.party_site_id = ps.party_site_id
8315 	AND    ps.location_id = loc.location_id;
8316      exception when others then null;
8317      end;
8318     END IF;
8319     IF p_party_id IS NULL THEN
8320 	RETURN;
8321     END IF;
8322     l_trxn_attributes_rec.payer_legal_entity_id := x_legal_entity_id;
8323     l_trxn_attributes_rec.payer_org_type := 'OPERATING_UNIT';
8324     l_trxn_attributes_rec.payer_org_id := l_org_id;
8325     l_trxn_attributes_rec.payee_party_id := p_party_id;
8326     l_trxn_attributes_rec.payee_party_site_id := p_party_site_id;
8327     l_trxn_attributes_rec.pay_proc_trxn_type_code := 'AR_CUSTOMER_REFUND';
8328     l_trxn_attributes_rec.payment_currency := p_currency_code;
8329     l_trxn_attributes_rec.payment_amount := p_amount;
8330     l_trxn_attributes_rec.payment_function := 'AR_CUSTOMER_REFUNDS';
8331 
8332     IF PG_DEBUG in ('Y', 'C') THEN
8333        arp_util.debug('Calling get_default_payment_attributes .......: ');
8334     END IF;
8335 
8336    /* Bug 5056865 p_ignore_payee_pref should be passed as 'Y' as discussed in
8337       Related Bug 5115632 */
8338    begin
8339     iby_disbursement_comp_pub.get_default_payment_attributes(
8340 	 p_api_version		=> 1.0
8341 	,p_init_msg_list	=> FND_API.G_FALSE
8342 	,p_ignore_payee_pref    => 'Y'
8343 	,p_trxn_attributes_rec	=> l_trxn_attributes_rec
8344 	,x_return_status	=> x_return_status
8345 	,x_msg_count		=> x_msg_count
8346 	,x_msg_data		=> x_msg_data
8347 	,x_default_pmt_attrs_rec=> l_pmt_attr_rec);
8348      exception when others then null;
8349      end;
8350 
8351     IF PG_DEBUG in ('Y', 'C') THEN
8352        arp_util.debug('get_default_payment_attributes return status: '||x_return_status);
8353     END IF;
8354 
8355     x_payment_method_code := l_pmt_attr_rec.payment_method.payment_method_code;
8356     x_payment_method_name := l_pmt_attr_rec.payment_method.payment_method_name;
8357     x_bank_account_id := l_pmt_attr_rec.payee_bankaccount.payee_bankaccount_id;
8358     x_bank_account_num := l_pmt_attr_rec.payee_bankaccount.payee_bankaccount_num;
8359     x_payment_reason_code := l_pmt_attr_rec.payment_reason.code;
8360     x_payment_reason_name := l_pmt_attr_rec.payment_reason.meaning;
8361     x_delivery_channel_code := l_pmt_attr_rec.delivery_channel.code;
8362     x_delivery_channel_name := l_pmt_attr_rec.delivery_channel.meaning;
8363     x_pay_alone_flag := l_pmt_attr_rec.pay_alone;
8364   IF PG_DEBUG in ('Y', 'C') THEN
8365      arp_util.debug('ar_receipt_lib_pvt.default_refund_attributes(-)');
8366   END IF;
8367 END default_refund_attributes;
8368 
8369 
8370 PROCEDURE populate_llca_gt (
8371 	     p_customer_trx_id        IN NUMBER,
8372   	     p_llca_type              IN VARCHAR2,
8373              p_llca_trx_lines_tbl     IN ar_receipt_api_pub.llca_trx_lines_tbl_type,
8374 	     p_line_amount	      IN NUMBER,
8375 	     p_tax_amount	      IN NUMBER,
8376   	     p_freight_amount	      IN NUMBER,
8377 	     p_charges_amount	      IN NUMBER,
8378 	     p_line_discount	      IN NUMBER,
8379 	     p_tax_discount	      IN NUMBER,
8380 	     p_freight_discount	      IN NUMBER,
8381 	     p_amount_applied	      IN NUMBER,
8382 	     p_amount_applied_from    IN NUMBER,
8383              p_return_status          OUT NOCOPY VARCHAR2)
8384 IS
8385 BEGIN
8386   IF PG_DEBUG in ('Y', 'C') THEN
8387      arp_util.debug('Populate_llca_gt ()+ ');
8388   END IF;
8389 
8390   p_return_status := FND_API.G_RET_STS_SUCCESS;
8391 
8392   -- Clean the GT Table first.
8393   delete from ar_llca_trx_lines_gt
8394   where customer_trx_id = p_customer_trx_id;
8395 
8396   delete from ar_llca_trx_errors_gt
8397   where customer_trx_id = p_customer_trx_id;
8398 
8399   If  p_llca_type = 'S'
8400   Then
8401      If p_llca_trx_lines_tbl.count <> 0 Then
8402          IF PG_DEBUG in ('Y', 'C') THEN
8403             arp_util.debug('Apply_In_Detail: ' || 'Table must be empty for
8404             Summary Level application ');
8405          END IF;
8406      End If;
8407   END IF;
8408 
8409   If  p_llca_type = 'L'
8410   Then
8411 	If p_llca_trx_lines_tbl.count = 0 Then
8412 	  IF PG_DEBUG in ('Y', 'C') THEN
8413            arp_util.debug('=======================================================');
8414            arp_util.debug('   PL SQL TABLE     (    INPUT PARAMETERS ........)+    ');
8415            arp_util.debug('=======================================================');
8416            arp_util.debug('Apply_In_Detail: ' || 'Pl Sql Table is empty ..
8417                   All Lines  ');
8418            END IF;
8419 	Else
8420 	  IF PG_DEBUG in ('Y', 'C') THEN
8421             arp_util.debug('No of records in PLSQL Table
8422                   =>'||to_char(p_llca_trx_lines_tbl.count));
8423           END IF;
8424 
8425 --bug7311231, Populating the GT table with flexfield information of each line.
8426 	     For i in p_llca_trx_lines_tbl.FIRST..p_llca_trx_lines_tbl.LAST
8427 	     Loop
8428 		 Insert into ar_llca_trx_lines_gt
8429 		 (  customer_trx_id,
8430 		    customer_trx_line_id,
8431 		    line_number,
8432 		    line_amount,
8433 		    tax_amount,
8434 		    freight_amount,
8435 		    charges_amount,
8436 		    amount_applied,
8437 		    amount_applied_from,
8438 		    line_discount,
8439 		    tax_discount,
8440 		    freight_discount,
8441 		    attribute_category,
8442 		    attribute1,
8443 		    attribute2,
8444 		    attribute3,
8445 		    attribute4,
8446 		    attribute5,
8447 		    attribute6,
8448 		    attribute7,
8449 		    attribute8,
8450 		    attribute9,
8451 		    attribute10,
8452 		    attribute11,
8453 		    attribute12,
8454 		    attribute13,
8455 		    attribute14,
8456 		    attribute15
8457 		 )
8458 		 values
8459 		 (
8460 		    p_customer_trx_id,
8461 		    p_llca_trx_lines_tbl(i).customer_trx_line_id,
8462 		    p_llca_trx_lines_tbl(i).line_number,
8463 		    p_llca_trx_lines_tbl(i).line_amount,
8464 		    p_llca_trx_lines_tbl(i).tax_amount,
8465 		    Null,
8466 		    Null,
8467 		    p_llca_trx_lines_tbl(i).amount_applied,
8468 		    p_llca_trx_lines_tbl(i).amount_applied_from,
8469 		    p_llca_trx_lines_tbl(i).line_discount,
8470 		    p_llca_trx_lines_tbl(i).tax_discount,
8471 		    Null,
8472 		    p_llca_trx_lines_tbl(i).attribute_category,
8473 		    p_llca_trx_lines_tbl(i).attribute1,
8474 		    p_llca_trx_lines_tbl(i).attribute2,
8475 		    p_llca_trx_lines_tbl(i).attribute3,
8476 		    p_llca_trx_lines_tbl(i).attribute4,
8477 		    p_llca_trx_lines_tbl(i).attribute5,
8478 		    p_llca_trx_lines_tbl(i).attribute6,
8479 		    p_llca_trx_lines_tbl(i).attribute7,
8480 		    p_llca_trx_lines_tbl(i).attribute8,
8481 		    p_llca_trx_lines_tbl(i).attribute9,
8482 		    p_llca_trx_lines_tbl(i).attribute10,
8483 		    p_llca_trx_lines_tbl(i).attribute11,
8484 		    p_llca_trx_lines_tbl(i).attribute12,
8485 		    p_llca_trx_lines_tbl(i).attribute13,
8486 		    p_llca_trx_lines_tbl(i).attribute14,
8487 		    p_llca_trx_lines_tbl(i).attribute15
8488 		 );
8489 
8490 	  IF PG_DEBUG in ('Y', 'C') THEN
8491            arp_util.debug('=======================================================');
8492            arp_util.debug(' Line .............=>'||to_char(i));
8493            arp_util.debug('p_customer_trx_id      =>'||to_char(p_customer_trx_id));
8494            arp_util.debug('p_customer_trx_line_id =>'||to_char(p_llca_trx_lines_tbl(i).customer_trx_line_id));
8495            arp_util.debug('p_line_number          =>'||to_char(p_llca_trx_lines_tbl(i).line_number));
8496            arp_util.debug('p_line_amount          =>'||to_char(p_llca_trx_lines_tbl(i).line_amount));
8497            arp_util.debug('p_tax_amount           =>'||to_char(p_llca_trx_lines_tbl(i).tax_amount));
8498 --         arp_util.debug('p_freight_amount       =>'||to_char(p_llca_trx_lines_tbl(i).freight_amount));
8499 --         arp_util.debug('p_charges_amount       =>'||to_char(p_llca_trx_lines_tbl(i).charges_amount));
8500            arp_util.debug('p_amount_applied       =>'||to_char(p_llca_trx_lines_tbl(i).amount_applied));
8501            arp_util.debug('p_amount_applied_from  =>'||to_char(p_llca_trx_lines_tbl(i).amount_applied_from));
8502            arp_util.debug('p_line_discount        =>'||to_char(p_llca_trx_lines_tbl(i).line_discount));
8503            arp_util.debug('p_tax_discount         =>'||to_char(p_llca_trx_lines_tbl(i).tax_discount));
8504 --         arp_util.debug('p_freight_discount     =>'||to_char(p_llca_trx_lines_tbl(i).freight_amount));
8505            arp_util.debug('=======================================================');
8506           END IF;
8507 	      End Loop;
8508 	  End If;
8509      End If;
8510   IF PG_DEBUG in ('Y', 'C') THEN
8511      arp_util.debug('Populate_llca_gt ()- ');
8512   END IF;
8513 
8514 EXCEPTION
8515  WHEN others THEN
8516    IF PG_DEBUG in ('Y', 'C') THEN
8517       arp_util.debug('EXCEPTION: (pouplate_llca_gt)');
8518    END IF;
8519    p_return_status := FND_API.G_RET_STS_ERROR;
8520    raise;
8521 End populate_llca_gt;
8522 
8523 PROCEDURE populate_errors_gt (
8524 	     p_customer_trx_id        IN NUMBER,
8525 	     p_customer_trx_line_id   IN NUMBER,
8526 	     p_error_message	      IN VARCHAR2,
8527 	     p_invalid_value	      IN VARCHAR2
8528 	     ) IS
8529 Begin
8530   IF PG_DEBUG in ('Y', 'C') THEN
8531      arp_util.debug('Populate_errors_gt ()+ ');
8532   END IF;
8533 
8534   Insert into  ar_llca_trx_errors_gt
8535   ( customer_trx_id,
8536     customer_trx_line_id,
8537     error_message,
8538     invalid_value
8539   )
8540   values
8541   ( p_customer_trx_id,
8542     p_customer_trx_line_id,
8543     p_error_message,
8544     p_invalid_value
8545   );
8546 
8547   IF PG_DEBUG in ('Y', 'C') THEN
8548      arp_util.debug('Populate_errors_gt ()- ');
8549   END IF;
8550 
8551 EXCEPTION
8552  WHEN others THEN
8553    IF PG_DEBUG in ('Y', 'C') THEN
8554       arp_util.debug('EXCEPTION: (pouplate_errors_gt)');
8555    END IF;
8556    raise;
8557 End populate_errors_gt;
8558 
8559 END ar_receipt_lib_pvt;