DBA Data[Home] [Help]

PACKAGE BODY: APPS.AR_RECEIPT_API_PUB

Source


1 PACKAGE BODY AR_RECEIPT_API_PUB AS
2 /* $Header: ARXPRECB.pls 120.81.12010000.11 2009/01/07 15:13:33 pbapna ship $           */
3 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('AFLOG_ENABLED'), 'N');
4 /* =======================================================================
5  | Global Data Types
6  * ======================================================================*/
7 
8 G_PKG_NAME     CONSTANT VARCHAR2(30) := 'AR_RECEIPT_API_PUB';
9 
10 G_MSG_UERROR    CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR;
11 G_MSG_ERROR     CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_ERROR;
12 G_MSG_SUCCESS   CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_SUCCESS;
13 G_MSG_HIGH      CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_DEBUG_HIGH;
14 G_MSG_MEDIUM    CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_DEBUG_MEDIUM;
15 G_MSG_LOW       CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_DEBUG_LOW;
16 
17 pg_update_claim_amount	 NUMBER; /* Bug 4170060 for rct to rct applications */
18 
19 --This routine initialize_profile_globals is used to set the profile option
20 --values in the corresponding package global variables. This kind of approach
21 --was adopted to enable the testing routine to assign different testcase values
22 --to the package global variables having the profile option values. So when we
23 --run the testing routine, the profile option package variables are overidden and
24 --the procedure initialize_profile_globals would not do any initialization in that
25 --case
26 PROCEDURE initialize_profile_globals IS
27   BEGIN
28    IF ar_receipt_lib_pvt.pg_profile_doc_seq = FND_API.G_MISS_CHAR THEN
29       ar_receipt_lib_pvt.pg_profile_doc_seq
30                        := fnd_profile.value('UNIQUE:SEQ_NUMBERS');
31    END IF;
32 -- pofile option AR_ENABLE_CROSS_CURRENCY has been obsolited
33 -- it will now always be 'Y'
34 --   IF ar_receipt_lib_pvt.pg_profile_enable_cc = FND_API.G_MISS_CHAR THEN
35       ar_receipt_lib_pvt.pg_profile_enable_cc:='Y';
36 --                      := fnd_profile.value('AR_ENABLE_CROSS_CURRENCY');
37 --   END IF;
38    IF ar_receipt_lib_pvt.pg_profile_appln_gl_date_def = FND_API.G_MISS_CHAR  THEN
39       ar_receipt_lib_pvt.pg_profile_appln_gl_date_def
40                        := fnd_profile.value('AR_APPLICATION_GL_DATE_DEFAULT');
41    END IF;
42 ---Profile option: AR: Cash - default Amount Applied has been
43 -- obsoleted
44    IF ar_receipt_lib_pvt.pg_profile_amt_applied_def = FND_API.G_MISS_CHAR  THEN
45       ar_receipt_lib_pvt.pg_profile_amt_applied_def :='INV';
46    END IF;
47    IF ar_receipt_lib_pvt.pg_profile_cc_rate_type = FND_API.G_MISS_CHAR  THEN
48       ar_receipt_lib_pvt.pg_profile_cc_rate_type
49                      := ar_setup.value('AR_CROSS_CURRENCY_RATE_TYPE',null);
50    -- null should be replaced with org_id, to find profile for diffrent org
51    END IF;
52 
53    IF ar_receipt_lib_pvt.pg_profile_dsp_inv_rate = FND_API.G_MISS_CHAR  THEN
54       ar_receipt_lib_pvt.pg_profile_dsp_inv_rate
55                        := fnd_profile.value('DISPLAY_INVERSE_RATE');
56    END IF;
57    IF ar_receipt_lib_pvt.pg_profile_create_bk_charges = FND_API.G_MISS_CHAR  THEN
58       ar_receipt_lib_pvt.pg_profile_create_bk_charges
59                        := fnd_profile.value('AR_JG_CREATE_BANK_CHARGES');
60    END IF;
61    IF ar_receipt_lib_pvt.pg_profile_def_x_rate_type = FND_API.G_MISS_CHAR  THEN
62       ar_receipt_lib_pvt.pg_profile_def_x_rate_type
63                         := fnd_profile.value('AR_DEFAULT_EXCHANGE_RATE_TYPE');
64    END IF;
65    arp_util.debug('*******Profile Option Values************');
66    arp_util.debug('pg_profile_appln_gl_date_def :'||ar_receipt_lib_pvt.pg_profile_appln_gl_date_def);
67    arp_util.debug('pg_profile_amt_applied_def   :'||ar_receipt_lib_pvt.pg_profile_amt_applied_def);
68    arp_util.debug('pg_profile_cc_rate_type      :'||ar_receipt_lib_pvt.pg_profile_cc_rate_type);
69    arp_util.debug('pg_profile_doc_seq           :'||ar_receipt_lib_pvt.pg_profile_doc_seq);
70    arp_util.debug('pg_profile_enable_cc         :'||ar_receipt_lib_pvt.pg_profile_enable_cc);
71    arp_util.debug('pg_profile_dsp_inv_rate      :'||ar_receipt_lib_pvt.pg_profile_dsp_inv_rate);
72    arp_util.debug('pg_profile_create_bk_charges :'||ar_receipt_lib_pvt.pg_profile_create_bk_charges);
73    arp_util.debug('pg_profile_def_x_rate_type   :'||ar_receipt_lib_pvt.pg_profile_def_x_rate_type);
74 
75  END initialize_profile_globals;
76 
77 
78 PROCEDURE Create_cash_1(
79            -- Standard API parameters.
80                  p_api_version      IN  NUMBER,
81                  p_init_msg_list    IN  VARCHAR2 := FND_API.G_FALSE,
82                  p_commit           IN  VARCHAR2 := FND_API.G_FALSE,
83                  p_validation_level IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
84                  x_return_status    OUT NOCOPY VARCHAR2,
85                  x_msg_count        OUT NOCOPY NUMBER,
86                  x_msg_data         OUT NOCOPY VARCHAR2,
87                  -- Receipt info. parameters
88                  p_usr_currency_code       IN  VARCHAR2 DEFAULT NULL, --the translated currency code
89                  p_currency_code           IN  VARCHAR2 DEFAULT NULL,
90                  p_usr_exchange_rate_type  IN  VARCHAR2 DEFAULT NULL,
91                  p_exchange_rate_type      IN  VARCHAR2 DEFAULT NULL,
92                  p_exchange_rate           IN  NUMBER   DEFAULT NULL,
93                  p_exchange_rate_date      IN  DATE     DEFAULT NULL,
94                  p_amount                  IN  NUMBER   DEFAULT NULL,
95                  p_factor_discount_amount  IN  NUMBER   DEFAULT NULL,
96                  p_receipt_number          IN  VARCHAR2 DEFAULT NULL,
97                  p_receipt_date            IN  DATE     DEFAULT NULL,
98                  p_gl_date                 IN  DATE     DEFAULT NULL,
99                  p_maturity_date           IN  DATE     DEFAULT NULL,
100                  p_postmark_date           IN  DATE     DEFAULT NULL,
101                  p_customer_id             IN  NUMBER   DEFAULT NULL,
102                  p_customer_name           IN  VARCHAR2 DEFAULT NULL,
103                  p_customer_number         IN VARCHAR2  DEFAULT NULL,
104                  p_customer_bank_account_id IN NUMBER   DEFAULT NULL,
105                  p_customer_bank_account_num   IN  VARCHAR2  DEFAULT NULL,
106                  p_customer_bank_account_name  IN  VARCHAR2  DEFAULT NULL,
107                  p_payment_trxn_extension_id  IN  NUMBER  DEFAULT NULL, --payment uptake changes bichatte
108                  p_location                 IN  VARCHAR2 DEFAULT NULL,
109                  p_customer_site_use_id     IN  NUMBER  DEFAULT NULL,
110                  p_default_site_use         IN VARCHAR2 DEFAULT 'Y', --The default site use bug 4448307-4509459
111                  p_customer_receipt_reference IN  VARCHAR2  DEFAULT NULL,
112                  p_override_remit_account_flag IN  VARCHAR2 DEFAULT NULL,
113                  p_remittance_bank_account_id  IN  NUMBER  DEFAULT NULL,
114                  p_remittance_bank_account_num  IN VARCHAR2 DEFAULT NULL,
115                  p_remittance_bank_account_name IN VARCHAR2 DEFAULT NULL,
116                  p_deposit_date             IN  DATE     DEFAULT NULL,
117                  p_receipt_method_id        IN  NUMBER   DEFAULT NULL,
118                  p_receipt_method_name      IN  VARCHAR2 DEFAULT NULL,
119                  p_doc_sequence_value       IN  NUMBER   DEFAULT NULL,
120                  p_ussgl_transaction_code   IN  VARCHAR2 DEFAULT NULL,
121                  p_anticipated_clearing_date IN DATE     DEFAULT NULL,
122                  p_called_from               IN VARCHAR2 DEFAULT NULL,
123                  p_attribute_rec         IN  attribute_rec_type ,
124        -- ******* Global Flexfield parameters *******
125                  p_global_attribute_rec  IN global_attribute_rec_type,
126                  p_comments             IN VARCHAR2 DEFAULT NULL,
127       --   ***  Notes Receivable Additional Information  ***
128                  p_issuer_name                  IN VARCHAR2  DEFAULT NULL,
129                  p_issue_date                   IN DATE    DEFAULT NULL,
130                  p_issuer_bank_branch_id        IN NUMBER  DEFAULT NULL,
131       --added  parameters to differentiate between create_cash and create_and_apply
132                  p_customer_trx_id              IN NUMBER  DEFAULT NULL,
133                  p_trx_number                   IN VARCHAR2  DEFAULT NULL,
134                  p_installment                  IN NUMBER  DEFAULT NULL,
135                  p_applied_payment_schedule_id  IN NUMBER  DEFAULT NULL,
136                  p_calling_api                  IN VARCHAR2 DEFAULT 'CREATE_CASH',
137                  p_org_id                       IN NUMBER  DEFAULT NULL,
138       --   ** OUT NOCOPY variables
139                  p_cr_id		  OUT NOCOPY NUMBER
140                   )
141 IS
142 l_api_name       CONSTANT VARCHAR2(20) := 'Create_cash';
143 l_api_version    CONSTANT NUMBER       := 1.0;
144 
145 l_currency_code       ar_cash_receipts.currency_code%TYPE;
146 l_exchange_rate_type  ar_cash_receipts.exchange_rate_type%TYPE;
147 l_exchange_rate       ar_cash_receipts.exchange_rate%TYPE;
148 l_exchange_rate_date  ar_cash_receipts.exchange_date%TYPE;
149 l_amount              ar_cash_receipts.amount%TYPE;
150 l_factor_discount_amount ar_cash_receipts.factor_discount_amount%TYPE;
151 l_receipt_number      ar_cash_receipts.receipt_number%TYPE;
152 l_receipt_date        ar_cash_receipts.receipt_date%TYPE;
153 l_gl_date             ar_cash_receipt_history.gl_date%TYPE;
154 l_maturity_date       DATE;
155 l_customer_id         ar_cash_receipts.pay_from_customer%TYPE;
156 l_customer_name       hz_parties.party_name%TYPE;  /*tca uptake*/
157 l_customer_bank_account_id         ar_cash_receipts.customer_bank_account_id%TYPE;
158 /* 6612301 */
159 l_customer_bank_account_num        iby_ext_bank_accounts_v.bank_account_number%TYPE;
160 l_customer_bank_account_name       iby_ext_bank_accounts_v.bank_account_name%TYPE;
161 l_payment_trxn_extension_id        ar_cash_receipts.payment_trxn_extension_id%TYPE; /* bichatte payment uptake project */
162 l_customer_bank_branch_id          ar_cash_receipts.customer_bank_branch_id%TYPE;
163 l_location                         hz_cust_site_uses.location%TYPE;
164 l_customer_site_use_id             hz_cust_site_uses.site_use_id%TYPE;
165 l_customer_receipt_reference       ar_cash_receipts.customer_receipt_reference%TYPE;
166 l_override_remit_account_flag      ar_cash_receipts.override_remit_account_flag%TYPE;
167 l_remit_bank_acct_use_id          ar_cash_receipts.remit_bank_acct_use_id%TYPE;
168 l_remittance_bank_account_num      ce_bank_accounts.bank_account_num%TYPE;
169 l_deposit_date                     ar_cash_receipts.deposit_date%TYPE;
170 l_receipt_method_id                ar_cash_receipts.receipt_method_id%TYPE;
171 l_receipt_method_name              ar_receipt_methods.name%TYPE;
172 l_ussgl_transaction_code           ar_cash_receipts.ussgl_transaction_code%TYPE;
173 l_anticipated_clearing_date        ar_cash_receipts.anticipated_clearing_date%TYPE;
174 l_state                            VARCHAR2(30);
175 l_cr_id                            NUMBER;
176 l_ps_id                            NUMBER;
177 l_row_id                           VARCHAR2(30);
178 l_validation_status                VARCHAR2(2)    DEFAULT  FND_API.G_RET_STS_SUCCESS;
179 l_doc_seq_status                   VARCHAR2(10);
180 l_doc_sequence_id                  NUMBER;
181 l_doc_sequence_value               NUMBER;
182 l_postmark_date                    DATE;
183 l_cash_def_return_status           VARCHAR2(1);
184 l_val_return_status                VARCHAR2(1);
185 l_def_cash_id_return_status        VARCHAR2(1);
186 l_derive_cust_return_status        VARCHAR2(1);
187 l_dflex_val_return_status          VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
188 l_attribute_rec                    attribute_rec_type;
189 l_global_attribute_rec             global_attribute_rec_type;
190 l_gdflex_return_status             VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
191 l_creation_method_code             ar_receipt_classes.creation_method_code%TYPE;
192 l_creation_method                  VARCHAR2(1);
193 l_org_return_status                VARCHAR2(1);
194 l_org_id                           NUMBER;
195 l_legal_entity_id		   NUMBER; /* R12 LE uptake */
196 /* bichatte payment uptake */
197 l_copy_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
198 l_copy_msg_count           NUMBER;
199 l_copy_msg_data        VARCHAR2(2000);
200 l_copy_pmt_trxn_extension_id        ar_cash_receipts.payment_trxn_extension_id%TYPE; /* bichatte payment uptake project */
201 l_create_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
202 l_create_msg_count           NUMBER;
203 l_create_msg_data        VARCHAR2(2000);
204 l_create_pmt_trxn_extension_id        ar_cash_receipts.payment_trxn_extension_id%TYPE; /* bichatte payment uptake project */
205 l_default_site_use                 VARCHAR2(1);  --bug4448307-4509459
206 l_rec_creation_rule_code           ar_receipt_methods.receipt_creation_rule_code%TYPE;
207 
208 BEGIN
209 
210  --assignment to local variables
211 l_currency_code               := p_currency_code;
212 l_exchange_rate_type          := p_exchange_rate_type;
213 l_exchange_rate               := p_exchange_rate;
214 l_exchange_rate_date          := trunc(p_exchange_rate_date);
215 l_amount                      := p_amount;
216 l_factor_discount_amount      := p_factor_discount_amount;
217 l_receipt_number              := p_receipt_number;
218 l_receipt_date                := trunc(p_receipt_date);
219 l_gl_date                     := trunc(p_gl_date);
220 l_maturity_date               := trunc(p_maturity_date);
221 l_customer_id                 := p_customer_id;
222 l_customer_name               := p_customer_name;
223 /* 6612301 */
224 l_customer_bank_account_id    := p_customer_bank_account_id;
225 l_customer_bank_account_num   := p_customer_bank_account_num;
226 l_customer_bank_account_name  := p_customer_bank_account_name;
227 l_payment_trxn_extension_id   := p_payment_trxn_extension_id; /* bichatte payment uptake project */
228 l_location                    := p_location;
229 l_customer_site_use_id        := p_customer_site_use_id;
230 l_customer_receipt_reference  := p_customer_receipt_reference;
231 l_override_remit_account_flag := p_override_remit_account_flag;
232 l_remit_bank_acct_use_id      := p_remittance_bank_account_id;
233 l_remittance_bank_account_num := p_remittance_bank_account_num;
234 l_deposit_date                := trunc(p_deposit_date);
235 l_receipt_method_id           := p_receipt_method_id;
236 l_receipt_method_name         := p_receipt_method_name;
237 l_ussgl_transaction_code      := p_ussgl_transaction_code;
238 l_anticipated_clearing_date   := trunc(p_anticipated_clearing_date);
239 l_postmark_date               := trunc(p_postmark_date);
240 l_doc_sequence_value          := p_doc_sequence_value;
241 l_attribute_rec               := p_attribute_rec;
242 l_global_attribute_rec        := p_global_attribute_rec;
243 l_org_id                      := p_org_id;
244 l_default_site_use            := p_default_site_use; --bug4448307-4509459
245 
246        /*------------------------------------+
247         |   Standard start of API savepoint  |
248         +------------------------------------*/
249 
250       SAVEPOINT Create_cash_PVT;
251 
252        /*--------------------------------------------------+
253         |   Standard call to check for call compatibility  |
254         +--------------------------------------------------*/
255 
256         IF NOT FND_API.Compatible_API_Call(
257                                             l_api_version,
258                                             p_api_version,
259                                             l_api_name,
260                                             G_PKG_NAME
261                                           )
262         THEN
263               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
264         END IF;
265 
266        /*--------------------------------------------------------------+
267         |   Initialize message list if p_init_msg_list is set to TRUE  |
268         +--------------------------------------------------------------*/
269 
270         IF FND_API.to_Boolean( p_init_msg_list )
271           THEN
272               FND_MSG_PUB.initialize;
273         END IF;
274 
275 
276         Original_create_cash_info.customer_id := p_customer_id;
277         Original_create_cash_info.customer_name := p_customer_name;
278         Original_create_cash_info.customer_number := p_customer_number; /*  Revert changes done for customer bank ref under payment uptake */
279 	/* 6612301 */
280   Original_create_cash_info.customer_bank_account_id
281                                            := p_customer_bank_account_id;
282         Original_create_cash_info.customer_bank_account_num
283                                              := p_customer_bank_account_num;
284         Original_create_cash_info.customer_bank_account_name
285                                             := p_customer_bank_account_name;
286         Original_create_cash_info.remit_bank_acct_use_id
287                                          := p_remittance_bank_account_id;
288         Original_create_cash_info.remittance_bank_account_num
289                                          := p_remittance_bank_account_num;
290         Original_create_cash_info.remittance_bank_account_name
291                                          := p_remittance_bank_account_name;
292         Original_create_cash_info.receipt_method_id
293                                          := p_receipt_method_id;
294         Original_create_cash_info.receipt_method_name
295                                          := p_receipt_method_name;
296 
297         IF PG_DEBUG in ('Y', 'C') THEN
298            arp_util.debug('Create_cash_1: ' || 'Create_cash_receipt()+ ');
299         END IF;
300        /*-----------------------------------------+
301         |   Initialize return status to SUCCESS   |
302         +-----------------------------------------*/
303 
304         x_return_status := FND_API.G_RET_STS_SUCCESS;
305         l_doc_seq_status := FND_API.G_RET_STS_SUCCESS;
306 
307        /* SSA change */
308        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
309        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
310                                                 p_return_status =>l_org_return_status);
311 
312        IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
313           x_return_status := FND_API.G_RET_STS_ERROR;
314        ELSE
315 
316         /*-------------------------------------------------+
317          | Initialize the profile option package variables |
318          +-------------------------------------------------*/
319 
320            initialize_profile_globals;
321 
322        /*---------------------------------------------+
323         |   ========== Start of API Body ==========   |
324         +---------------------------------------------*/
325 
326         --If any value to id conversion fails then error status is returned
327         -- bichatte removed customer bank variables payment uptake project (Reverted)
328         -- we dont need to default the payment_trxn_extension_id
329 
330         ar_receipt_lib_pvt.Default_cash_ids(
331                               p_usr_currency_code  ,
332                               p_usr_exchange_rate_type,
333                               p_customer_name,
334                               p_customer_number,
335                               p_location,
336                               l_receipt_method_name,
337                                /* 6612301 */
338                               p_customer_bank_account_name,
339                               p_customer_bank_account_num,
340                               p_remittance_bank_account_name ,
341                               p_remittance_bank_account_num ,
342                               l_currency_code ,
343                               l_exchange_rate_type ,
344                               l_customer_id ,
345                               l_customer_site_use_id,
346                               l_receipt_method_id,
347                                /* 6612301 */
348                               l_customer_bank_account_id,
349 			                        l_customer_bank_branch_id,
350                               l_remit_bank_acct_use_id ,
351                               l_receipt_date, /* Bug fix 3135407 */
352                               l_def_cash_id_return_status,
353                               l_default_site_use --bug4448307-4509459
354                               );
355 
356          IF p_calling_api  = 'CREATE_AND_APPLY'  THEN
357           IF l_customer_id IS NULL AND
358              l_def_cash_id_return_status = FND_API.G_RET_STS_SUCCESS AND
359              l_currency_code IS NOT NULL
360            THEN
361            ar_receipt_lib_pvt.Derive_cust_info_from_trx(
362                                        p_customer_trx_id,
363                                        p_trx_number,
364                                        p_installment,
365                                        p_applied_payment_schedule_id,
366                                        l_currency_code,
367                                        l_customer_id,
368                                        l_customer_site_use_id,
369                                        l_derive_cust_return_status
370                                             );
371           END IF;
372          END IF;
373          IF PG_DEBUG in ('Y', 'C') THEN
374             arp_util.debug('Create_cash_1: ' || 'Default_cash_ids return status :'||l_derive_cust_return_status);
375          END IF;
376         ar_receipt_lib_pvt.Get_cash_defaults(
377                      l_currency_code,
378                      l_exchange_rate_type,
379                      l_exchange_rate,
380                      l_exchange_rate_date,
381                      l_amount,
382                      l_factor_discount_amount,
383                      l_receipt_date,
384                      l_gl_date,
385                      l_maturity_date,
386                      l_customer_receipt_reference,
387                      l_override_remit_account_flag,
388                      l_remit_bank_acct_use_id,
389                      l_deposit_date,
390                      l_receipt_method_id,
391                      l_state,
392                      l_anticipated_clearing_date,
393                      p_called_from,
394                      l_creation_method_code,
395                      l_cash_def_return_status
396                     );
397            IF PG_DEBUG in ('Y', 'C') THEN
398               arp_util.debug('Create_cash_1: ' || 'Get_Cash_defaults return status :'||l_cash_def_return_status);
399            END IF;
400 
401        /*------------------------------------------+
402         |  Get legal_entity_id                     |
403         +------------------------------------------*/  /* R12 LE uptake */
404            l_legal_entity_id := ar_receipt_lib_pvt.get_legal_entity(l_remit_bank_acct_use_id);
405 
406        /*------------------------------------------+
407         |  Validate the receipt information.       |
408         |  Do not continue if there are errors.    |
409         +------------------------------------------*/
410 
411         -- bichatte removed the custome bank variables since we dont need to
412         -- validate them. payment uptake project. (Reverted)
413 
414 
415         ar_receipt_val_pvt.Validate_Cash_Receipt(
416                                             l_receipt_number,
417                                             l_receipt_method_id,
418                                             l_state,
419                                             l_receipt_date,
420                                             l_gl_date,
421                                             l_maturity_date,
422                                             l_deposit_date,
423                                             l_amount,
424                                             l_factor_discount_amount,
425                                             l_customer_id,
426                                             /* 6612301 */
427                                             l_customer_bank_account_id,
428                                             p_location,
429                                             l_customer_site_use_id,
430                                             l_remit_bank_acct_use_id,
431                                             l_override_remit_account_flag,
432                                             l_anticipated_clearing_date,
433                                             l_currency_code,
434                                             l_exchange_rate_type,
435                                             l_exchange_rate,
436                                             l_exchange_rate_date,
437                                             l_doc_sequence_value,
438                                             p_called_from,
439                                             l_val_return_status
440                                              );
441 
442          /* bug 3604739 / 3517523 : when receipt API is called from BR module, we bypass
443             the validation for DFF */
444 
445          IF p_called_from in ('BR_FACTORED_WITH_RECOURSE', 'BR_FACTORED_WITHOUT_RECOURSE') THEN
446             NULL;
447          ELSE
448             --validate and default the flexfields
449             ar_receipt_lib_pvt.Validate_Desc_Flexfield(
450                                             l_attribute_rec,
451                                             'AR_CASH_RECEIPTS',
452                                             l_dflex_val_return_status
453                                             );
454 
455             --validation and defaulting of the global descriptive flexfield
456             JG_AR_CASH_RECEIPTS.Validate_gbl(
457                                             l_global_attribute_rec.global_attribute_category,
458                                             l_global_attribute_rec.global_attribute1,
459                                             l_global_attribute_rec.global_attribute2,
460                                             l_global_attribute_rec.global_attribute3,
461                                             l_global_attribute_rec.global_attribute4,
462                                             l_global_attribute_rec.global_attribute5,
463                                             l_global_attribute_rec.global_attribute6,
464                                             l_global_attribute_rec.global_attribute7,
465                                             l_global_attribute_rec.global_attribute8,
466                                             l_global_attribute_rec.global_attribute9,
467                                             l_global_attribute_rec.global_attribute10,
468                                             l_global_attribute_rec.global_attribute11,
469                                             l_global_attribute_rec.global_attribute12,
470                                             l_global_attribute_rec.global_attribute13,
471                                             l_global_attribute_rec.global_attribute14,
472                                             l_global_attribute_rec.global_attribute15,
473                                             l_global_attribute_rec.global_attribute16,
474                                             l_global_attribute_rec.global_attribute17,
475                                             l_global_attribute_rec.global_attribute18,
476                                             l_global_attribute_rec.global_attribute19,
477                                             l_global_attribute_rec.global_attribute20,
478                                             l_gdflex_return_status
479                                             );
480          END IF;
481 
482        END IF;
483 
484          IF l_cash_def_return_status <> FND_API.G_RET_STS_SUCCESS OR
485             l_val_return_status <> FND_API.G_RET_STS_SUCCESS  OR
486             l_def_cash_id_return_status <> FND_API.G_RET_STS_SUCCESS OR
487             l_dflex_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
488             l_gdflex_return_status <> FND_API.G_RET_STS_SUCCESS THEN
489 
490             x_return_status := FND_API.G_RET_STS_ERROR;
491          END IF;
492 
493         --APANDIT : after the changes made for iReceivables credit card feature
494         --the receipt api will also be creating the automatic receipts.
495         --Bug 1817727
496 
497         /* 6612301 */
498         IF NVL(l_creation_method_code, 'X') = 'MANUAL'
499                 THEN
500                    l_creation_method := 'M';
501                    l_payment_trxn_extension_id := NULL;
502                 ELSIF NVL(l_creation_method_code, 'X') = 'AUTOMATIC'
503                 THEN
504                    l_creation_method := 'A';
505                    l_customer_bank_account_id := NULL;
506                 ELSE
507                    l_creation_method := 'M';
508                    l_payment_trxn_extension_id := NULL;
509         END IF;
510 
511         --Call the document sequence routine only there have been no errors
512         --reported so far.
513         IF x_return_status = FND_API.G_RET_STS_SUCCESS  THEN
514 
515            ar_receipt_lib_pvt.get_doc_seq(222,
516                                           l_receipt_method_name,
517                                           arp_global.set_of_books_id,
518                                           l_creation_method,
519                                           l_receipt_date,
520                                           l_doc_sequence_value,
521                                           l_doc_sequence_id,
522                                           l_doc_seq_status
523                                           );
524         END IF;
525 
526       --If receipt number has not been passed in the document sequence value is
527       --used as the receipt number.
528         IF l_receipt_number IS NULL THEN
529           IF l_doc_sequence_value IS NOT NULL THEN
530             l_receipt_number := l_doc_sequence_value;
531             --warning message
532              IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
533               THEN
534                  FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_NUM_DFLT_DOC_SEQ');
535                  FND_MSG_PUB.Add;
536              END IF;
537           ELSE
538             IF PG_DEBUG in ('Y', 'C') THEN
539                arp_util.debug('Create_cash_1: ' || 'Receipt Number is null ');
540             END IF;
541             --raise error message
542               FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_NUM_NULL');
543               FND_MSG_PUB.Add;
544               x_return_status := FND_API.G_RET_STS_ERROR;
545           END IF;
546         END IF;
547 
548    /* Bug 5364287 */
549    arp_standard.debug ('x_return_status (' || x_return_status ||')');
550    arp_standard.debug ('l_doc_seq_status (' || l_doc_seq_status ||')');
551    IF (
552               x_return_status         <> FND_API.G_RET_STS_SUCCESS
553               OR l_doc_seq_status     <> FND_API.G_RET_STS_SUCCESS
554       )
555    THEN
556               x_return_status := FND_API.G_RET_STS_ERROR ;
557    ELSE
558 
559      -- HERE we have to call the payment engine in order to get the payment_trx_extension_id
560      -- the conditions are if creation_method = 'A' and p_payment_trx_extension_id is null.
561      -- bichatte payment_uptake project.
562 
563          arp_util.debug ('Create_cash -- CHECK' || p_installment );
564 
565      -- We have to check the receipt_creation_rule_code and
566      -- accordingly call create or COPY
567 
568 
569 
570         select nvl(RECEIPT_CREATION_RULE_CODE,'MANUAL')
571         into   l_rec_creation_rule_code
572         from  AR_RECEIPT_METHODS
573         where receipt_method_id = l_receipt_method_id;
574 
575 
576 
577 
578 
579       IF ( l_creation_method = 'A' and l_payment_trxn_extension_id is NULL) THEN
580 
581                arp_util.debug('Create_cash_122: ' || l_creation_method_code);
582                arp_util.debug('Create_cash_122: ' || l_payment_trxn_extension_id );
583               FND_MESSAGE.SET_NAME('AR','AR_CC_AUTH_FAILED');
584               FND_MSG_PUB.Add;
585               x_return_status := FND_API.G_RET_STS_ERROR;
586       END IF;
587 
588    /*Bug 5614569
589     Commented out the existing logic to call the create_payment_extension if the receipt creation
590     rule code is anything other than 'PER_INVOICE'. For all the cases whenver we pass the Payment extesnion id
591    for an Automatic Receipt Method  to create the receipt we need to call the copy_payment_extension */
592 
593    IF nvl(p_installment,1) = 1 THEN
594 
595 
596       IF l_creation_method = 'A'  THEN
597           arp_standard.debug('calling copy  Extension....');
598 
599            Copy_payment_extension (
600               p_payment_trxn_extension_id => l_payment_trxn_extension_id,
601               p_customer_id => l_customer_id,
602               p_receipt_method_id =>l_receipt_method_id,
603               p_org_id =>l_org_id,
604               p_customer_site_use_id  =>l_customer_site_use_id,
605               p_receipt_number=> l_receipt_number,
606               x_msg_count => l_copy_msg_count,
607               x_msg_data => l_copy_msg_data,
608               x_return_status =>l_copy_return_status,
609               o_payment_trxn_extension_id =>l_copy_pmt_trxn_extension_id
610                  );
611 
612          IF l_copy_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
613                arp_util.debug('Create_cash_123: ' );
614              FND_MESSAGE.set_name('AR', 'AR_CC_AUTH_FAILED');
615              FND_MSG_PUB.Add;
616               x_return_status := FND_API.G_RET_STS_ERROR;
617          END IF;
618 
619             l_payment_trxn_extension_id := l_copy_pmt_trxn_extension_id;
620 
621  	  arp_standard.debug('calling copy  Extension  end ....');
622   	 arp_standard.debug('calling copy  Extension  end ...2'|| to_char(l_copy_pmt_trxn_extension_id));
623       END IF;
624 
625   ELSE
626  arp_util.debug ('Create_cash -- CHECK2' || p_installment );
627    IF l_creation_method = 'A'  THEN
628    arp_standard.debug('calling CREATE  Extension....');
629     Create_payment_extension (
630        p_payment_trxn_extension_id => l_payment_trxn_extension_id,
631        p_customer_id => l_customer_id,
632        p_receipt_method_id =>l_receipt_method_id,
633        p_org_id =>l_org_id,
634        p_customer_site_use_id  =>l_customer_site_use_id,
635        p_cash_receipt_id => l_cr_id,
636        p_receipt_number=> l_receipt_number,
637        x_msg_count => l_create_msg_count,
638        x_msg_data => l_create_msg_data,
639        x_return_status =>l_create_return_status,
640        o_payment_trxn_extension_id =>l_create_pmt_trxn_extension_id
641 	 );
642 
643 	 IF l_create_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
644               arp_util.debug('Create_cash_123: ' );
645 	      FND_MESSAGE.set_name('AR', 'AR_CC_AUTH_FAILED');
646 	      FND_MSG_PUB.Add;
647 	       x_return_status := FND_API.G_RET_STS_ERROR;
648 	 END IF;
649 
650 	 l_payment_trxn_extension_id := l_create_pmt_trxn_extension_id;
651 
652 	 arp_standard.debug('calling create  Extension  end ....');
653 	 arp_standard.debug('calling create  Extension end ...2'||
654 	 to_char(l_create_pmt_trxn_extension_id));
655 
656 
657     END IF;
658 
659 
660    END IF; -- p_installment check
661 
662 
663  END IF;  /* Return Status */
664 
665        --Dump all the variables before calling the entity handler
666       IF PG_DEBUG in ('Y', 'C') THEN
667          arp_util.debug('Create_cash_1: ' || '*********DUMPING ALL THE VARIABLES ********');
668          arp_util.debug('Create_cash_1: ' || 'l_currency_code = '||l_currency_code);
669          arp_util.debug('Create_cash_1: ' || 'l_amount = '||to_char(l_amount));
670          arp_util.debug('Create_cash_1: ' || 'l_customer_id = '||to_char(l_customer_id));
671          arp_util.debug('Create_cash_1: ' || 'l_receipt_number = '||l_receipt_number);
672          arp_util.debug('Create_cash_1: ' || 'l_receipt_date = '||to_char(l_receipt_date,'dd-mon-yy'));
673          arp_util.debug('Create_cash_1: ' || 'l_gl_date = '||to_char(l_gl_date,'dd-mon-yy'));
674          arp_util.debug('Create_cash_1: ' || 'l_maturity_date = '||to_char(l_maturity_date,'dd-mon-yyy'));
675          arp_util.debug('Create_cash_1: ' || 'l_exchange_rate_type = '||l_exchange_rate_type);
676          arp_util.debug('Create_cash_1: ' || 'l_exchange_rate = '||to_char(l_exchange_rate));
677          arp_util.debug('Create_cash_1: ' || 'l_exchange_rate_date = '||to_char(l_exchange_rate_date,'dd-mon-yy'));
678          arp_util.debug('Create_cash_1: ' || 'l_override_remit_account_flag = '||l_override_remit_account_flag);
679          arp_util.debug('Create_cash_1: ' || 'l_remit_bank_acct_use_id = '||to_char(l_remit_bank_acct_use_id));
680          arp_util.debug('Create_cash_1: ' || 'l_payment_trxn_extension_id = '||to_char(l_payment_trxn_extension_id));
681          arp_util.debug('Create_cash_1: ' || 'l_customer_site_use_id = '||to_char(l_customer_site_use_id));
682          arp_util.debug('Create_cash_1: ' || 'l_factor_discount_amount = '||to_char(l_factor_discount_amount));
683          arp_util.debug('Create_cash_1: ' || 'l_deposit_date = '||to_char(l_deposit_date,'dd-mon-yy'));
684          arp_util.debug('Create_cash_1: ' || 'l_receipt_method_id = '||to_char(l_receipt_method_id));
685          arp_util.debug('Create_cash_1: ' || 'l_doc_sequence_value = '||to_char(l_doc_sequence_value));
686          arp_util.debug('Create_cash_1: ' || 'l_legal_entity_id = '||to_char(l_legal_entity_id)); /* R12 LE uptake */
687       END IF;
688        /*------------------------------------------------------------+
689         |  If any errors - including validation failures - occurred, |
690         |  rollback any changes and return an error status.          |
691         +------------------------------------------------------------*/
692       IF PG_DEBUG in ('Y', 'C') THEN
693          arp_util.debug('Create_cash_1: ' || 'x_return_status : '||x_return_status);
694          arp_util.debug('Create_cash_1: ' || 'l_doc_seq_status : '||l_doc_seq_status);
695       END IF;
696 	 IF (
697               x_return_status         <> FND_API.G_RET_STS_SUCCESS
698               OR l_doc_seq_status     <> FND_API.G_RET_STS_SUCCESS
699              )
700              THEN
701 
702               ROLLBACK TO Create_cash_PVT;
703 
704               x_return_status := FND_API.G_RET_STS_ERROR ;
705 
706               FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
707                                         p_count => x_msg_count,
708                                         p_data  => x_msg_data);
709 
710               IF PG_DEBUG in ('Y', 'C') THEN
711                  arp_util.debug('Create_cash_1: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
712               END IF;
713              Return;
714         END IF;
715           IF PG_DEBUG in ('Y', 'C') THEN
716              arp_util.debug('Create_cash_1: ' || 'x_return_status '||x_return_status);
717           END IF;
718                  /* bichatte removed customer bank variables for the payment uptake project
719                     added the l_payment_trxn_extension_id  */
720       BEGIN
721           arp_proc_receipts2.insert_cash_receipt(
722   						l_currency_code,
723 						l_amount,
724 						l_customer_id,
725 						l_receipt_number,
726 						l_receipt_date,
727 						l_gl_date,
728 						l_maturity_date,
729 						p_comments,
730 						l_exchange_rate_type,
731 						l_exchange_rate,
732 						l_exchange_rate_date,
733 						NULL,  --batch_id
734 						l_attribute_rec.attribute_category,
735 						l_attribute_rec.attribute1,
736 						l_attribute_rec.attribute2,
737 						l_attribute_rec.attribute3,
738 						l_attribute_rec.attribute4,
739 						l_attribute_rec.attribute5,
740 						l_attribute_rec.attribute6,
741 						l_attribute_rec.attribute7,
742 						l_attribute_rec.attribute8,
743 						l_attribute_rec.attribute9,
744 						l_attribute_rec.attribute10,
745 						l_attribute_rec.attribute11,
746 						l_attribute_rec.attribute12,
747 						l_attribute_rec.attribute13,
748 						l_attribute_rec.attribute14,
749 						l_attribute_rec.attribute15,
750 						l_override_remit_account_flag,
751 						l_remit_bank_acct_use_id ,
752                                                 l_customer_bank_account_id ,
753 						l_customer_site_use_id,
754 						l_customer_receipt_reference ,
755 						l_factor_discount_amount ,
756 						l_deposit_date,
757 						l_receipt_method_id,
758 						l_doc_sequence_value,
759 						l_doc_sequence_id,
760 						l_ussgl_transaction_code ,
761                                                 NULL,   --Vat_tax_id
762 						l_anticipated_clearing_date ,
763                                                 l_customer_bank_branch_id,
764                                                 l_postmark_date,
765 						l_global_attribute_rec.global_attribute1,
766 						l_global_attribute_rec.global_attribute2,
767 						l_global_attribute_rec.global_attribute3,
768 						l_global_attribute_rec.global_attribute4,
769 						l_global_attribute_rec.global_attribute5,
770 						l_global_attribute_rec.global_attribute6,
771 						l_global_attribute_rec.global_attribute7,
772 						l_global_attribute_rec.global_attribute8,
773 						l_global_attribute_rec.global_attribute9,
774 						l_global_attribute_rec.global_attribute10,
775 						l_global_attribute_rec.global_attribute11,
776 						l_global_attribute_rec.global_attribute12,
777 						l_global_attribute_rec.global_attribute13,
778 						l_global_attribute_rec.global_attribute14,
779 						l_global_attribute_rec.global_attribute15,
780 						l_global_attribute_rec.global_attribute16,
781 						l_global_attribute_rec.global_attribute17,
782 						l_global_attribute_rec.global_attribute18,
783 						l_global_attribute_rec.global_attribute19,
784 						l_global_attribute_rec.global_attribute20,
785 						l_global_attribute_rec.global_attribute_category,
786                                                 p_issuer_name,
787                                                 trunc(p_issue_date), /* Bug fix 3135407 */
788                                                 p_issuer_bank_branch_id,
789                                                 null,  -- application_notes
790 						l_cr_id,
791 						l_ps_id,
792 						l_row_id,
793 						'RAPI',
794 						p_api_version
795                                                 ,p_called_from
796 						,l_legal_entity_id /* R12 LE updtake */
797                                                 ,l_payment_trxn_extension_id /* payment uptake */
798 				           );
799          EXCEPTION
800          WHEN OTHERS THEN
801 
802                /*-------------------------------------------------------+
803                 |  Handle application errors that result from trapable  |
804                 |  error conditions. The error messages have already    |
805                 |  been put on the error stack.                         |
806                 +-------------------------------------------------------*/
807 
808                 IF (SQLCODE = -20001)
809                 THEN
810                      ROLLBACK TO Create_Cash_PVT;
811 
812                       --  Display_Parameters;
813                       x_return_status := FND_API.G_RET_STS_ERROR ;
814                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
815                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROC_RECEIPTS2.INSERT_CASH_RECEIPT : '||SQLERRM);
816                        FND_MSG_PUB.Add;
817 
818                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
819                                                   p_count  =>  x_msg_count,
820                                                   p_data   => x_msg_data
821                                                 );
822                       RETURN;
823                 ELSE
824                    RAISE;
825                 END IF;
826 
827          END;
828 
829 			p_cr_id := l_cr_id;
830             IF PG_DEBUG in ('Y', 'C') THEN
831                arp_util.debug('Create_cash_1: ' || 'Cash Receipt id : '||to_char(l_cr_id));
832             END IF;
833 
834        /*-------------------------------------------------------+
835         | FND_MSG_PUB.Count_And_Get used  get the count of mesg.|
836         | in the message stack. If there is only one message in |
837         | the stack it retrieves this message                   |
838         +-------------------------------------------------------*/
839 
840  	  FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
841                                    p_count => x_msg_count,
842                                    p_data  => x_msg_data
843                                  );
844 
845 
846        /*--------------------------------+
847         |   Standard check of p_commit   |
848         +--------------------------------*/
849 
850         IF FND_API.To_Boolean( p_commit )
851         THEN
852             IF PG_DEBUG in ('Y', 'C') THEN
853                arp_util.debug('Create_cash_1: ' || 'committing');
854             END IF;
855             Commit;
856         END IF;
857 
858         IF PG_DEBUG in ('Y', 'C') THEN
859            arp_util.debug('Create_cash_1: ' || 'Create_Cash_Receipt()- ');
860         END IF;
861 
862 
863 EXCEPTION
864        WHEN FND_API.G_EXC_ERROR THEN
865 
866                 IF PG_DEBUG in ('Y', 'C') THEN
867                    arp_util.debug('Create_cash_1: ' || SQLCODE, G_MSG_ERROR);
868                    arp_util.debug('Create_cash_1: ' || SQLERRM, G_MSG_ERROR);
869                 END IF;
870 
871                 ROLLBACK TO Create_Cash_PVT;
872                 x_return_status := FND_API.G_RET_STS_ERROR ;
873 
874                 --Display_Parameters;
875 
876                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
877                                            p_count       =>      x_msg_count,
878                                            p_data        =>      x_msg_data
879                                          );
880 
881         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
882 
883                 IF PG_DEBUG in ('Y', 'C') THEN
884                    arp_util.debug('Create_cash_1: ' || SQLERRM, G_MSG_ERROR);
885                 END IF;
886                 ROLLBACK TO Create_Cash_PVT;
887                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
888 
889                --  Display_Parameters;
890 
891                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
892                                            p_count       =>      x_msg_count,
893                                            p_data        =>      x_msg_data
894                                          );
895 
896         WHEN OTHERS THEN
897 
898                /*-------------------------------------------------------+
899                 |  Handle application errors that result from trapable  |
900                 |  error conditions. The error messages have already    |
901                 |  been put on the error stack.                         |
902                 +-------------------------------------------------------*/
903 
904                 IF (SQLCODE = -20001)
905                 THEN
906                      ROLLBACK TO Create_Cash_PVT;
907 
908                       --  Display_Parameters;
909                       x_return_status := FND_API.G_RET_STS_ERROR ;
910                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
911                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','CREATE_CASH : '||SQLERRM);
912                       FND_MSG_PUB.Add;
913 
914                       FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
915                                                  p_count  =>  x_msg_count,
916                                                  p_data   => x_msg_data
917                                                 );
918                       RETURN;
919                 ELSE
920                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
921                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
922                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','CREATE_CASH : '||SQLERRM);
923                       FND_MSG_PUB.Add;
924                 END IF;
925 
926                 IF PG_DEBUG in ('Y', 'C') THEN
927                    arp_util.debug('Create_cash_1: ' || SQLCODE);
928                    arp_util.debug('Create_cash_1: ' || SQLERRM);
929                 END IF;
930 
931                 ROLLBACK TO Create_Cash_PVT;
932 
933 
934                 IF      FND_MSG_PUB.Check_Msg_Level
935                 THEN
936                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
937                                         l_api_name
938                                        );
939                 END IF;
940 
941              --   Display_Parameters;
942                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
943                                            p_count       =>      x_msg_count,
944                                            p_data        =>      x_msg_data
945                                          );
946 
947 END Create_cash_1;
948 
949 PROCEDURE Create_cash(
950            -- Standard API parameters.
951                  p_api_version      IN  NUMBER,
952                  p_init_msg_list    IN  VARCHAR2 := FND_API.G_FALSE,
953                  p_commit           IN  VARCHAR2 := FND_API.G_FALSE,
954                  p_validation_level IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
955                  x_return_status    OUT NOCOPY VARCHAR2,
956                  x_msg_count        OUT NOCOPY NUMBER,
957                  x_msg_data         OUT NOCOPY VARCHAR2,
958                  -- Receipt info. parameters
959                  p_usr_currency_code       IN  VARCHAR2 DEFAULT NULL, --the translated currency code
960                  p_currency_code           IN  VARCHAR2 DEFAULT NULL,
961                  p_usr_exchange_rate_type  IN  VARCHAR2 DEFAULT NULL,
962                  p_exchange_rate_type      IN  VARCHAR2 DEFAULT NULL,
963                  p_exchange_rate           IN  NUMBER   DEFAULT NULL,
964                  p_exchange_rate_date      IN  DATE     DEFAULT NULL,
965                  p_amount                  IN  NUMBER   DEFAULT NULL,
966                  p_factor_discount_amount  IN  NUMBER   DEFAULT NULL,
967                  p_receipt_number          IN  VARCHAR2 DEFAULT NULL,
968                  p_receipt_date            IN  DATE     DEFAULT NULL,
969                  p_gl_date                 IN  DATE     DEFAULT NULL,
970                  p_maturity_date           IN  DATE     DEFAULT NULL,
971                  p_postmark_date           IN  DATE     DEFAULT NULL,
972                  p_customer_id             IN  NUMBER   DEFAULT NULL,
973                  p_customer_name           IN  VARCHAR2 DEFAULT NULL,
974                  p_customer_number         IN VARCHAR2  DEFAULT NULL,
975                  p_customer_bank_account_id IN NUMBER   DEFAULT NULL,
976                  p_customer_bank_account_num   IN  VARCHAR2  DEFAULT NULL,
977                  p_customer_bank_account_name  IN  VARCHAR2  DEFAULT NULL,
978                  p_payment_trxn_extension_id  IN NUMBER DEFAULT NULL,
979                  p_location                 IN  VARCHAR2 DEFAULT NULL,
980                  p_customer_site_use_id     IN  NUMBER  DEFAULT NULL,
981                  p_default_site_use         IN VARCHAR2  DEFAULT 'Y', --bug4448307-4509459
982                  p_customer_receipt_reference IN  VARCHAR2  DEFAULT NULL,
983                  p_override_remit_account_flag IN  VARCHAR2 DEFAULT NULL,
984                  p_remittance_bank_account_id  IN  NUMBER  DEFAULT NULL,
985                  p_remittance_bank_account_num  IN VARCHAR2 DEFAULT NULL,
986                  p_remittance_bank_account_name IN VARCHAR2 DEFAULT NULL,
987                  p_deposit_date             IN  DATE     DEFAULT NULL,
988                  p_receipt_method_id        IN  NUMBER   DEFAULT NULL,
989                  p_receipt_method_name      IN  VARCHAR2 DEFAULT NULL,
990                  p_doc_sequence_value       IN  NUMBER   DEFAULT NULL,
991                  p_ussgl_transaction_code   IN  VARCHAR2 DEFAULT NULL,
992                  p_anticipated_clearing_date IN DATE     DEFAULT NULL,
993                  p_called_from               IN VARCHAR2 DEFAULT NULL,
994                  p_attribute_rec         IN  attribute_rec_type DEFAULT attribute_rec_const,
995        -- ******* Global Flexfield parameters *******
996                  p_global_attribute_rec  IN global_attribute_rec_type DEFAULT global_attribute_rec_const,
997                  p_comments             IN VARCHAR2 DEFAULT NULL,
998       --   ***  Notes Receivable Additional Information  ***
999                  p_issuer_name                  IN VARCHAR2  DEFAULT NULL,
1000                  p_issue_date                   IN DATE   DEFAULT NULL,
1001                  p_issuer_bank_branch_id        IN NUMBER  DEFAULT NULL,
1002                  p_org_id                       IN NUMBER  DEFAULT NULL,
1003                  p_installment                  IN NUMBER DEFAULT NULL,
1004       --   ** OUT NOCOPY variables
1005                  p_cr_id		  OUT NOCOPY NUMBER
1006                   )
1007 IS
1008 l_api_name       CONSTANT VARCHAR2(20) := 'Create_cash';
1009 l_api_version    CONSTANT NUMBER       := 1.0;
1010 
1011 l_currency_code       ar_cash_receipts.currency_code%TYPE;
1012 l_exchange_rate_type  ar_cash_receipts.exchange_rate_type%TYPE;
1013 l_exchange_rate       ar_cash_receipts.exchange_rate%TYPE;
1014 l_exchange_rate_date  ar_cash_receipts.exchange_date%TYPE;
1015 l_amount              ar_cash_receipts.amount%TYPE;
1016 l_factor_discount_amount ar_cash_receipts.factor_discount_amount%TYPE;
1017 l_receipt_number      ar_cash_receipts.receipt_number%TYPE;
1018 l_receipt_date        ar_cash_receipts.receipt_date%TYPE;
1019 l_gl_date             ar_cash_receipt_history.gl_date%TYPE;
1020 l_maturity_date       DATE;
1021 l_customer_id         ar_cash_receipts.pay_from_customer%TYPE;
1022 l_customer_name       hz_parties.party_name%TYPE; /* tca uptake */
1023 l_payment_trxn_extension_id        ar_cash_receipts.payment_trxn_extension_id%TYPE;
1024 l_location                         hz_cust_site_uses.location%TYPE;
1025 l_customer_site_use_id             hz_cust_site_uses.site_use_id%TYPE;
1026 l_customer_receipt_reference       ar_cash_receipts.customer_receipt_reference%TYPE;
1027 l_override_remit_account_flag      ar_cash_receipts.override_remit_account_flag%TYPE;
1028 l_remit_bank_acct_use_id       ar_cash_receipts.remit_bank_acct_use_id%TYPE;
1029 l_remittance_bank_account_num      ce_bank_accounts.bank_account_num%TYPE;
1030 l_deposit_date                     ar_cash_receipts.deposit_date%TYPE;
1031 l_receipt_method_id                ar_cash_receipts.receipt_method_id%TYPE;
1032 l_receipt_method_name              ar_receipt_methods.name%TYPE;
1033 l_ussgl_transaction_code           ar_cash_receipts.ussgl_transaction_code%TYPE;
1034 l_anticipated_clearing_date        ar_cash_receipts.anticipated_clearing_date%TYPE;
1035 l_state VARCHAR2(30);
1036 l_cr_id          NUMBER;
1037 l_ps_id          NUMBER;
1038 l_row_id         VARCHAR2(30);
1039 l_validation_status  VARCHAR2(2)    := FND_API.G_RET_STS_SUCCESS;
1040 l_doc_seq_status     VARCHAR2(10);
1041 l_doc_sequence_id    NUMBER;
1042 l_doc_sequence_value NUMBER;
1043 l_postmark_date  DATE;
1044 l_cash_def_return_status VARCHAR2(1);
1045 l_val_return_status  VARCHAR2(1);
1046 l_def_cash_id_return_status  VARCHAR2(1);
1047 l_default_site_use VARCHAR2(1);  --bug4448307-4509459
1048 BEGIN
1049    --call the internal routine
1050    Create_cash_1(
1051            -- Standard API parameters.
1052                  p_api_version ,
1053                  p_init_msg_list,
1054                  p_commit,
1055                  p_validation_level,
1056                  x_return_status,
1057                  x_msg_count,
1058                  x_msg_data ,
1059                  -- Receipt info. parameters
1060                  p_usr_currency_code, --the translated currency code
1061                  p_currency_code    ,
1062                  p_usr_exchange_rate_type ,
1063                  p_exchange_rate_type     ,
1064                  p_exchange_rate         ,
1065                  p_exchange_rate_date    ,
1066                  p_amount                ,
1067                  p_factor_discount_amount,
1068                  p_receipt_number        ,
1069                  p_receipt_date          ,
1070                  p_gl_date               ,
1071                  p_maturity_date         ,
1072                  p_postmark_date         ,
1073                  p_customer_id           ,
1074                  p_customer_name         ,
1075                  p_customer_number       ,
1076                  p_customer_bank_account_id ,
1077                  p_customer_bank_account_num ,
1078                  p_customer_bank_account_name ,
1079                  p_payment_trxn_extension_id , -- bichatte payment uptake project
1080                  p_location               ,
1081                  p_customer_site_use_id   ,
1082                  p_default_site_use,                 --BUG4448307-4509459
1083                  p_customer_receipt_reference ,
1084                  p_override_remit_account_flag ,
1085                  p_remittance_bank_account_id  ,
1086                  p_remittance_bank_account_num ,
1087                  p_remittance_bank_account_name ,
1088                  p_deposit_date             ,
1089                  p_receipt_method_id        ,
1090                  p_receipt_method_name      ,
1091                  p_doc_sequence_value       ,
1092                  p_ussgl_transaction_code   ,
1093                  p_anticipated_clearing_date ,
1094                  p_called_from               ,
1095                  p_attribute_rec         ,
1096        -- ******* Global Flexfield parameters *******
1097                  p_global_attribute_rec  ,
1098                  p_comments             ,
1099       --   ***  Notes Receivable Additional Information  ***
1100                  p_issuer_name    ,
1101                  p_issue_date     ,
1102                  p_issuer_bank_branch_id  ,
1103       --added  parameters to differentiate between create_cash and create_and_apply
1104                  NULL,
1105                  NULL,
1106                  p_installment,
1107                  NULL,
1108                  'CREATE_CASH',
1109                  p_org_id,
1110       --   ** OUT NOCOPY variables
1111                  p_cr_id
1112                   );
1113 
1114 END Create_cash;
1115 
1116 
1117 PROCEDURE Apply(
1118 -- Standard API parameters.
1119       p_api_version                  IN  NUMBER,
1120       p_init_msg_list                IN  VARCHAR2 := FND_API.G_FALSE,
1121       p_commit                       IN  VARCHAR2 := FND_API.G_FALSE,
1122       p_validation_level             IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
1123       x_return_status                OUT NOCOPY VARCHAR2,
1124       x_msg_count                    OUT NOCOPY NUMBER,
1125       x_msg_data                     OUT NOCOPY VARCHAR2,
1126  --  Receipt application parameters.
1127       p_cash_receipt_id              IN ar_cash_receipts.cash_receipt_id%TYPE DEFAULT NULL,
1128       p_receipt_number               IN ar_cash_receipts.receipt_number%TYPE DEFAULT NULL,
1129       p_customer_trx_id              IN ra_customer_trx.customer_trx_id%TYPE DEFAULT NULL,
1130       p_trx_number                   IN ra_customer_trx.trx_number%TYPE DEFAULT NULL,
1131       p_installment                  IN ar_payment_schedules.terms_sequence_number%TYPE DEFAULT NULL,
1132       p_applied_payment_schedule_id  IN ar_payment_schedules.payment_schedule_id%TYPE DEFAULT NULL,
1133       p_amount_applied               IN ar_receivable_applications.amount_applied%TYPE DEFAULT NULL,
1134       -- this is the allocated receipt amount
1135       p_amount_applied_from          IN ar_receivable_applications.amount_applied_from%TYPE DEFAULT NULL,
1136       p_trans_to_receipt_rate        IN ar_receivable_applications.trans_to_receipt_rate%TYPE DEFAULT NULL,
1137       p_discount                     IN ar_receivable_applications.earned_discount_taken%TYPE DEFAULT NULL,
1138       p_apply_date                   IN ar_receivable_applications.apply_date%TYPE DEFAULT NULL,
1139       p_apply_gl_date                IN ar_receivable_applications.gl_date%TYPE DEFAULT NULL,
1140       p_ussgl_transaction_code       IN ar_receivable_applications.ussgl_transaction_code%TYPE DEFAULT NULL,
1141       p_customer_trx_line_id	     IN ar_receivable_applications.applied_customer_trx_line_id%TYPE DEFAULT NULL,
1142       p_line_number                  IN ra_customer_trx_lines.line_number%TYPE DEFAULT NULL,
1143       p_show_closed_invoices         IN VARCHAR2 DEFAULT 'N', /* Bug fix 2462013 */
1144       p_called_from                  IN VARCHAR2 DEFAULT NULL,
1145       p_move_deferred_tax            IN VARCHAR2 DEFAULT 'Y',
1146       p_link_to_trx_hist_id          IN ar_receivable_applications.link_to_trx_hist_id%TYPE DEFAULT NULL,
1147       p_attribute_rec                IN attribute_rec_type DEFAULT attribute_rec_const,
1148 	 -- ******* Global Flexfield parameters *******
1149       p_global_attribute_rec         IN global_attribute_rec_type DEFAULT global_attribute_rec_const,
1150       p_comments                     IN ar_receivable_applications.comments%TYPE,
1151       p_payment_set_id               IN ar_receivable_applications.payment_set_id%TYPE DEFAULT NULL,
1152       p_application_ref_type         IN ar_receivable_applications.application_ref_type%TYPE DEFAULT NULL,
1153       p_application_ref_id           IN ar_receivable_applications.application_ref_id%TYPE DEFAULT NULL,
1154       p_application_ref_num          IN ar_receivable_applications.application_ref_num%TYPE DEFAULT NULL,
1155       p_secondary_application_ref_id IN ar_receivable_applications.secondary_application_ref_id%TYPE DEFAULT NULL,
1156       p_application_ref_reason       IN ar_receivable_applications.application_ref_reason%TYPE DEFAULT NULL,
1157       p_customer_reference           IN ar_receivable_applications.customer_reference%TYPE DEFAULT NULL,
1158       p_customer_reason              IN ar_receivable_applications.customer_reason%TYPE DEFAULT NULL,
1159       p_org_id                       IN NUMBER  DEFAULT NULL
1160       ) IS
1161 l_api_name       CONSTANT VARCHAR2(20) := 'Apply';
1162 l_api_version    CONSTANT NUMBER       := 1.0;
1163 l_cash_receipt_id  NUMBER;
1164 l_cr_gl_date       DATE;
1165 l_cr_date          DATE;
1166 l_cr_amount        NUMBER;
1167 l_cr_unapp_amount  NUMBER;
1168 l_cr_currency_code VARCHAR2(15);
1169 l_customer_trx_id  NUMBER(15);
1170 l_installment      NUMBER;
1171 l_customer_trx_line_id NUMBER(15);
1172 l_trx_due_date     DATE;
1173 l_trx_currency_code VARCHAR2(15);
1174 l_trx_gl_date   DATE;
1175 l_apply_gl_date DATE;
1176 l_calc_discount_on_lines_flag  VARCHAR2(1);
1177 l_partial_discount_flag   VARCHAR2(1);
1178 l_allow_overappln_flag VARCHAR2(1);
1179 l_natural_appln_only_flag  VARCHAR2(1);
1180 l_creation_sign VARCHAR2(30);
1181 l_amount_applied NUMBER;
1182 l_amount_applied_from  NUMBER;
1183 l_trans_to_receipt_rate  NUMBER;
1184 l_applied_payment_schedule_id  NUMBER;
1185 l_cr_payment_schedule_id  NUMBER;
1186 l_apply_date  DATE;
1187 l_trx_date  DATE;
1188 l_discount  NUMBER;
1189 l_discount_earned_allowed  NUMBER;
1190 l_discount_max_allowed  NUMBER;
1191 l_term_id  NUMBER;
1192 l_trx_line_amount NUMBER;
1193 l_amount_due_original   NUMBER;
1194 l_amount_due_remaining  NUMBER;
1195 l_discount_earned  NUMBER;
1196 l_discount_unearned  NUMBER;
1197 l_new_amount_due_remaining NUMBER;
1198 --OUT parameters of the entity handler
1199 p_out_rec_application_id NUMBER;
1200 p_acctd_amount_applied_from NUMBER;
1201 p_acctd_amount_applied_to  NUMBER;
1202 l_def_return_status  VARCHAR2(1);
1203 l_val_return_status VARCHAR2(1);
1204 l_app_validation_return_status VARCHAR2(1);
1205 l_def_ids_return_status  VARCHAR2(1);
1206 l_dflex_val_return_status  VARCHAR2(1);
1207 l_attribute_rec         attribute_rec_type;
1208 l_global_attribute_rec  global_attribute_rec_type;
1209 l_remit_bank_acct_use_id    NUMBER;
1210 l_receipt_method_id             NUMBER;
1211 l_gdflex_return_status    VARCHAR2(1);
1212 l_application_ref_id      ar_receivable_applications.application_ref_id%TYPE;
1213 l_application_ref_num     ar_receivable_applications.application_ref_num%TYPE;
1214 l_return_status           VARCHAR2(1);
1215 l_msg_count               NUMBER;
1216 l_msg_data                VARCHAR2(2000);
1217 l_payment_set_id          NUMBER;
1218 l_amount_due_remain_disc  NUMBER;         /* Bug 2535663 */
1219 l_claim_reason_name       VARCHAR2(100);
1220 l_org_return_status VARCHAR2(1);
1221 l_org_id                           NUMBER;
1222 
1223 -- LLCA
1224 l_llca_type		varchar2(1) := NULL;
1225 l_group_id              ra_customer_trx_lines.source_data_key4%TYPE := NULL;
1226 l_line_amount		NUMBER;
1227 l_tax_amount		NUMBER;
1228 l_freight_amount	NUMBER;
1229 l_charges_amount	NUMBER;
1230 l_line_discount		NUMBER;
1231 l_tax_discount		NUMBER;
1232 l_freight_discount	NUMBER;
1233 l_line_items_original   NUMBER;
1234 l_line_items_remaining  NUMBER;
1235 l_tax_original		NUMBER;
1236 l_tax_remaining		NUMBER;
1237 l_freight_original	NUMBER;
1238 l_freight_remaining	NUMBER;
1239 l_rec_charges_charged	NUMBER;
1240 l_rec_charges_remaining NUMBER;
1241 
1242 BEGIN
1243 
1244        /*------------------------------------+
1245         |   Standard start of API savepoint  |
1246         +------------------------------------*/
1247 
1248       SAVEPOINT Apply_PVT;
1249 
1250        /*--------------------------------------------------+
1251         |   Standard call to check for call compatibility  |
1252         +--------------------------------------------------*/
1253 
1254         IF NOT FND_API.Compatible_API_Call(
1255                                             l_api_version,
1256                                             p_api_version,
1257                                             l_api_name,
1258                                             G_PKG_NAME
1259                                           )
1260         THEN
1261               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1262         END IF;
1263 
1264        /*--------------------------------------------------------------+
1265         |   Initialize message list if p_init_msg_list is set to TRUE  |
1266         +--------------------------------------------------------------*/
1267 
1268         IF FND_API.to_Boolean( p_init_msg_list )
1269           THEN
1270               FND_MSG_PUB.initialize;
1271         END IF;
1272 
1273 Original_application_info.cash_receipt_id             := p_cash_receipt_id;
1274 Original_application_info.receipt_number              := p_receipt_number;
1275 Original_application_info.customer_trx_id             := p_customer_trx_id;
1276 Original_application_info.trx_number                  := p_trx_number;
1277 Original_application_info.installment                 := p_installment;
1278 Original_application_info.applied_payment_schedule_id := p_applied_payment_schedule_id;
1279 Original_application_info.customer_trx_line_id        := p_customer_trx_line_id;
1280 Original_application_info.line_number                 := p_line_number;
1281 Original_application_info.amount_applied_from         := p_amount_applied_from;
1282 Original_application_info.trans_to_receipt_rate       := p_trans_to_receipt_rate;
1283 
1284         IF PG_DEBUG in ('Y', 'C') THEN
1285            arp_util.debug('Apply()+ ');
1286         END IF;
1287 
1288        /*-----------------------------------------+
1289         |   Initialize return status to SUCCESS   |
1290         +-----------------------------------------*/
1291         x_return_status := FND_API.G_RET_STS_SUCCESS;
1292 
1293 
1294 
1295 
1296  /* SSA change */
1297        l_org_id            := p_org_id;
1298        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
1299        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
1300                                                 p_return_status =>l_org_return_status);
1301  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1302        x_return_status := FND_API.G_RET_STS_ERROR;
1303  ELSE
1304 
1305 
1306         /*-------------------------------------------------+
1307          | Initialize the profile option package variables |
1308          +-------------------------------------------------*/
1309 
1310            initialize_profile_globals;
1311 
1312 
1313        /*---------------------------------------------+
1314         |   ========== Start of API Body ==========   |
1315         +---------------------------------------------*/
1316 
1317      l_cash_receipt_id        := p_cash_receipt_id;
1318      l_customer_trx_id        := p_customer_trx_id;
1319      l_installment            := p_installment;
1320      l_amount_applied         := p_amount_applied;
1321      l_amount_applied_from    := p_amount_applied_from;
1322      l_trans_to_receipt_rate  := p_trans_to_receipt_rate;
1323      l_discount               := p_discount;
1324      l_apply_date             := trunc(p_apply_date);
1325      l_apply_gl_date          := trunc(p_apply_gl_date);
1326      l_customer_trx_line_id   := p_customer_trx_line_id;
1327      l_applied_payment_schedule_id := p_applied_payment_schedule_id;
1328      l_attribute_rec          := p_attribute_rec;
1329      l_global_attribute_rec   := p_global_attribute_rec;
1330      l_payment_set_id         := p_payment_set_id;
1331 
1332 
1333           /*-----------------------+
1334            |                       |
1335            |ID TO VALUE CONVERSION |
1336            |                       |
1337            +-----------------------*/
1338 
1339         ar_receipt_lib_pvt.Default_appln_ids(
1340                             l_cash_receipt_id,
1341                             p_receipt_number,
1342                             l_customer_trx_id,
1343                             p_trx_number,
1344                             l_customer_trx_line_id, /* Bug fix 3435834 */
1345                             p_line_number,  /* Bug fix 3435834 */
1346                             l_installment,
1347                             l_applied_payment_schedule_id,
1348 			    l_llca_type,
1349 		            l_group_id ,   /* Bug 5284890 */
1350                             l_def_ids_return_status);
1351 
1352 
1353    IF PG_DEBUG in ('Y', 'C') THEN
1354       arp_util.debug('Apply: ' || 'Defaulting Ids Return_status = '||l_def_ids_return_status);
1355    END IF;
1356           /*---------------------+
1357            |                     |
1358            |    DEFAULTING       |
1359            |                     |
1360            +---------------------*/
1361    IF PG_DEBUG in ('Y', 'C') THEN
1362       arp_util.debug('Apply: ' || 'l_amount_applied_from :'||to_char(l_amount_applied_from));
1363    END IF;
1364          ar_receipt_lib_pvt.Default_application_info(
1365               l_cash_receipt_id   ,
1366               l_cr_gl_date,
1367               l_cr_date,
1368               l_cr_amount,
1369               l_cr_unapp_amount,
1370               l_cr_currency_code ,
1371               l_customer_trx_id,
1372               l_installment,
1373               p_show_closed_invoices,
1374               l_customer_trx_line_id,
1375               l_trx_due_date,
1376               l_trx_currency_code,
1377               l_trx_date,
1378               l_trx_gl_date,
1379               l_apply_gl_date,
1380               l_calc_discount_on_lines_flag,
1381               l_partial_discount_flag,
1382               l_allow_overappln_flag,
1383               l_natural_appln_only_flag,
1384               l_creation_sign ,
1385               l_cr_payment_schedule_id ,
1386               l_applied_payment_schedule_id ,
1387               l_term_id ,
1388               l_amount_due_original ,
1389               l_amount_due_remaining,
1390               l_trx_line_amount,
1391               l_discount,
1392               l_apply_date ,
1393               l_discount_max_allowed,
1394               l_discount_earned_allowed,
1395               l_discount_earned,
1396               l_discount_unearned ,
1397               l_new_amount_due_remaining,
1398               l_remit_bank_acct_use_id,
1399               l_receipt_method_id,
1400               l_amount_applied ,
1401               l_amount_applied_from ,
1402               l_trans_to_receipt_rate,
1403       	      l_llca_type,
1404 	      l_line_amount,
1405 	      l_tax_amount,
1406 	      l_freight_amount,
1407 	      l_charges_amount,
1408 	      l_line_discount,
1409 	      l_tax_discount,
1410 	      l_freight_discount,
1411       	      l_line_items_original,
1412 	      l_line_items_remaining,
1413 	      l_tax_original,
1414 	      l_tax_remaining,
1415 	      l_freight_original,
1416 	      l_freight_remaining,
1417 	      l_rec_charges_charged,
1418 	      l_rec_charges_remaining,
1419               p_called_from,
1420               l_def_return_status
1421                );
1422 IF PG_DEBUG in ('Y', 'C') THEN
1423    arp_util.debug('Apply: ' || 'l_amount_applied_from :'||to_char(l_amount_applied_from));
1424    arp_util.debug('Apply: ' || 'l_new_amount_due_remaining :'||to_char(l_new_amount_due_remaining));
1425    arp_util.debug('Apply: ' || 'Defaulting Return_status = '||l_def_return_status);
1426 END IF;
1427           /*---------------------+
1428            |                     |
1429            |    VALIDATION       |
1430            |                     |
1431            +---------------------*/
1432     --The defaulting routine will raise error only if there is an error in
1433     --defaulting for any of the two entities : Cash receipt and Transaction.
1434     --So in this case there is no point in calling the validation routines as
1435     --at least on of the two main entities are invalid.
1436     --The invalid cash receipt is bound to give an invalid transaction error
1437     IF l_def_return_status = FND_API.G_RET_STS_SUCCESS  AND
1438        l_def_ids_return_status = FND_API.G_RET_STS_SUCCESS
1439       THEN
1440           ar_receipt_val_pvt.Validate_Application_info(
1441                       l_apply_date,
1442                       l_cr_date,
1443                       l_trx_date,
1444                       l_apply_gl_date  ,
1445                       l_trx_gl_date,
1446                       l_cr_gl_date,
1447                       l_amount_applied,
1448                       l_applied_payment_schedule_id,
1449                       l_customer_trx_line_id,
1450                       l_trx_line_amount,
1451                       l_creation_sign,
1452                       l_allow_overappln_flag,
1453                       l_natural_appln_only_flag,
1454                       l_discount  ,
1455                       l_amount_due_remaining,
1456                       l_amount_due_original,
1457                       l_trans_to_receipt_rate,
1458                       l_cr_currency_code ,
1459                       l_trx_currency_code,
1460                       l_amount_applied_from ,
1461                       l_cr_unapp_amount ,
1462                       l_partial_discount_flag ,
1463                       l_discount_earned_allowed,
1464                       l_discount_max_allowed ,
1465                       p_move_deferred_tax,
1466       	      	      l_llca_type,
1467 		      l_line_amount,
1468 		      l_tax_amount,
1469 		      l_freight_amount,
1470 		      l_charges_amount,
1471 		      l_line_discount,
1472 		      l_tax_discount,
1473 		      l_freight_discount,
1474 		      l_line_items_original,
1475 		      l_line_items_remaining,
1476 		      l_tax_original,
1477 		      l_tax_remaining,
1478 		      l_freight_original,
1479 		      l_freight_remaining,
1480 		      l_rec_charges_charged,
1481 		      l_rec_charges_remaining,
1482                       l_val_return_status
1483                           );
1484                       IF PG_DEBUG in ('Y', 'C') THEN
1485                          arp_util.debug('Apply: ' || 'Validation Return_status = '||l_val_return_status);
1486                       END IF;
1487     END IF;
1488 
1489         --Validate application reference details passed.
1490         /* Bug 2535663 - subtract discounts from amount due remaining */
1491         l_amount_due_remain_disc := (l_amount_due_remaining -
1492                                        l_discount_earned - l_discount_unearned);
1493 	ar_receipt_val_pvt.validate_application_ref(
1494                               l_applied_payment_schedule_id,
1495                               p_application_ref_type,
1496                               p_application_ref_id,
1497                               p_application_ref_num,
1498                               p_secondary_application_ref_id,
1499                               l_cash_receipt_id,
1500                               l_amount_applied,
1501                               l_amount_due_remain_disc,
1502                               l_cr_currency_code,
1503                               l_trx_currency_code,
1504                               p_application_ref_reason,
1505                               l_app_validation_return_status
1506                                );
1507 
1508         IF PG_DEBUG in ('Y', 'C') THEN
1509            arp_util.debug('Apply: ' || 'Application ref Validation return status :'||l_app_validation_return_status);
1510         END IF;
1511 
1512         --validate and default the flexfields
1513         ar_receipt_lib_pvt.Validate_Desc_Flexfield(
1514                                         l_attribute_rec,
1515                                         'AR_RECEIVABLE_APPLICATIONS',
1516                                         l_dflex_val_return_status
1517                                                 );
1518 
1519 
1520        --default and validate the global descriptive flexfield
1521         jg_ar_receivable_applications.apply(
1522                   p_apply_before_after        => 'BEFORE',
1523                   p_global_attribute_category => l_global_attribute_rec.global_attribute_category,
1524                   p_set_of_books_id           => arp_global.set_of_books_id,
1525                   p_cash_receipt_id           => l_cash_receipt_id,
1526                   p_receipt_date              => l_cr_date,
1527                   p_applied_payment_schedule_id => l_applied_payment_schedule_id,
1528                   p_amount_applied              => l_amount_applied,
1529                   p_unapplied_amount            => (l_cr_unapp_amount - l_amount_applied),
1530                   p_due_date                    => l_trx_due_date,
1531                   p_receipt_method_id           => l_receipt_method_id,
1532                   p_remittance_bank_account_id  => l_remit_bank_acct_use_id,
1533                   p_global_attribute1           => l_global_attribute_rec.global_attribute1,
1534                   p_global_attribute2           => l_global_attribute_rec.global_attribute2,
1535                   p_global_attribute3           => l_global_attribute_rec.global_attribute3,
1536                   p_global_attribute4           => l_global_attribute_rec.global_attribute4,
1537                   p_global_attribute5           => l_global_attribute_rec.global_attribute5,
1538                   p_global_attribute6           => l_global_attribute_rec.global_attribute6,
1539                   p_global_attribute7           => l_global_attribute_rec.global_attribute7,
1540                   p_global_attribute8           => l_global_attribute_rec.global_attribute8,
1541                   p_global_attribute9           => l_global_attribute_rec.global_attribute9,
1542                   p_global_attribute10          => l_global_attribute_rec.global_attribute10,
1543                   p_global_attribute11          => l_global_attribute_rec.global_attribute11,
1544                   p_global_attribute12          => l_global_attribute_rec.global_attribute12,
1545                   p_global_attribute13          => l_global_attribute_rec.global_attribute13,
1546                   p_global_attribute14          => l_global_attribute_rec.global_attribute14,
1547                   p_global_attribute15          => l_global_attribute_rec.global_attribute15,
1548                   p_global_attribute16          => l_global_attribute_rec.global_attribute16,
1549                   p_global_attribute17          => l_global_attribute_rec.global_attribute17,
1550                   p_global_attribute18          => l_global_attribute_rec.global_attribute18,
1551                   p_global_attribute19          => l_global_attribute_rec.global_attribute19,
1552                   p_global_attribute20          => l_global_attribute_rec.global_attribute20,
1553                   p_return_status               => l_gdflex_return_status);
1554 END IF;
1555 
1556        IF l_def_ids_return_status <> FND_API.G_RET_STS_SUCCESS OR
1557           l_val_return_status     <> FND_API.G_RET_STS_SUCCESS OR
1558           l_def_return_status     <> FND_API.G_RET_STS_SUCCESS  OR
1559           l_dflex_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
1560           l_app_validation_return_status <> FND_API.G_RET_STS_SUCCESS OR
1561           l_gdflex_return_status  <> FND_API.G_RET_STS_SUCCESS THEN
1562           x_return_status := FND_API.G_RET_STS_ERROR;
1563        END IF;
1564 
1565        /* Bug fix 3435834
1566           The messages should be retrieved irrespective of the return status */
1567        FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
1568                                         p_count => x_msg_count,
1569                                         p_data  => x_msg_data
1570                                        );
1571 
1572        IF x_return_status <> FND_API.G_RET_STS_SUCCESS
1573          THEN
1574 
1575             ROLLBACK TO Apply_PVT;
1576 
1577              x_return_status := FND_API.G_RET_STS_ERROR ;
1578 
1579              IF PG_DEBUG in ('Y', 'C') THEN
1580                 arp_util.debug('Apply: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
1581              END IF;
1582              Return;
1583         END IF;
1584 
1585       --Dump the input variables to the entity handler
1586     IF PG_DEBUG in ('Y', 'C') THEN
1587        arp_util.debug('Apply: ' || 'l_cr_payment_schedule_id      : '||to_char(l_cr_payment_schedule_id));
1588        arp_util.debug('Apply: ' || 'l_applied_payment_schedule_id : '||to_char(l_applied_payment_schedule_id));
1589        arp_util.debug('Apply: ' || 'l_amount_applied              : '||to_char(l_amount_applied));
1590        arp_util.debug('Apply: ' || 'l_amount_applied_from         : '||to_char(l_amount_applied_from));
1591        arp_util.debug('Apply: ' || 'l_trans_to_receipt_rate       : '||to_char(l_trans_to_receipt_rate));
1592        arp_util.debug('Apply: ' || 'l_trx_currency_code           : '||l_trx_currency_code);
1593        arp_util.debug('Apply: ' || 'l_cr_currency_code            : '||l_cr_currency_code);
1594        arp_util.debug('Apply: ' || 'l_discount_earned             : '||to_char(l_discount_earned));
1595        arp_util.debug('Apply: ' || 'l_discount_unearned           : '||to_char(l_discount_unearned));
1596        arp_util.debug('l_apply_date                  : '||to_char(l_apply_date,'DD-MON-YY'));
1597        arp_util.debug('l_apply_gl_date               : '||to_char(l_apply_gl_date,'DD-MON-YY'));
1598        arp_util.debug('Apply: ' || 'l_customer_trx_line_id        : '||to_char(l_customer_trx_line_id));
1599     END IF;
1600 
1601      --lock the receipt before calling the entity handler
1602        arp_cash_receipts_pkg.nowaitlock_p(p_cr_id => l_cash_receipt_id);
1603 
1604        /* Bug 4042420: lock the payment schedule of the applied transaction */
1605        arp_ps_pkg.nowaitlock_p (p_ps_id => l_applied_payment_schedule_id);
1606 
1607    /* Bug 3773036: Initializing the return status */
1608    l_return_status := FND_API.G_RET_STS_SUCCESS;
1609 
1610    BEGIN
1611      --call the entity handler
1612     arp_process_application.receipt_application(
1613 	p_receipt_ps_id => l_cr_payment_schedule_id ,
1614 	p_invoice_ps_id => l_applied_payment_schedule_id ,
1615         p_amount_applied => l_amount_applied ,
1616         p_amount_applied_from => l_amount_applied_from ,
1617         p_trans_to_receipt_rate => l_trans_to_receipt_rate ,
1618         p_invoice_currency_code => l_trx_currency_code ,
1619         p_receipt_currency_code => l_cr_currency_code ,
1620         p_earned_discount_taken => l_discount_earned ,
1621         p_unearned_discount_taken => l_discount_unearned ,
1622         p_apply_date => l_apply_date ,
1623 	p_gl_date => l_apply_gl_date ,
1624 	p_ussgl_transaction_code => p_ussgl_transaction_code ,
1625 	p_customer_trx_line_id	=> l_customer_trx_line_id ,
1626         p_application_ref_type => p_application_ref_type,
1627         p_application_ref_id => p_application_ref_id,
1628         p_application_ref_num => p_application_ref_num,
1629         p_secondary_application_ref_id => p_secondary_application_ref_id,
1630         p_attribute_category => l_attribute_rec.attribute_category,
1631 	p_attribute1 => l_attribute_rec.attribute1,
1632 	p_attribute2 => l_attribute_rec.attribute2,
1633 	p_attribute3 => l_attribute_rec.attribute3,
1634 	p_attribute4 => l_attribute_rec.attribute4,
1635 	p_attribute5 => l_attribute_rec.attribute5,
1636 	p_attribute6 => l_attribute_rec.attribute6,
1637 	p_attribute7 => l_attribute_rec.attribute7,
1638 	p_attribute8 => l_attribute_rec.attribute8,
1639 	p_attribute9 => l_attribute_rec.attribute9,
1640 	p_attribute10 => l_attribute_rec.attribute10,
1641 	p_attribute11 => l_attribute_rec.attribute11,
1642 	p_attribute12 => l_attribute_rec.attribute12,
1643 	p_attribute13 => l_attribute_rec.attribute13,
1644 	p_attribute14 => l_attribute_rec.attribute14,
1645 	p_attribute15 => l_attribute_rec.attribute15,
1646         p_global_attribute_category => p_global_attribute_rec.global_attribute_category,
1647         p_global_attribute1 => p_global_attribute_rec.global_attribute1,
1648         p_global_attribute2 => p_global_attribute_rec.global_attribute2,
1649         p_global_attribute3 => p_global_attribute_rec.global_attribute3,
1650         p_global_attribute4 => p_global_attribute_rec.global_attribute4,
1651         p_global_attribute5 => p_global_attribute_rec.global_attribute5,
1652         p_global_attribute6 => p_global_attribute_rec.global_attribute6,
1653         p_global_attribute7 => p_global_attribute_rec.global_attribute7,
1654         p_global_attribute8 => p_global_attribute_rec.global_attribute8,
1655         p_global_attribute9 => p_global_attribute_rec.global_attribute9,
1656         p_global_attribute10 => p_global_attribute_rec.global_attribute10,
1657         p_global_attribute11 => p_global_attribute_rec.global_attribute11,
1658         p_global_attribute12 => p_global_attribute_rec.global_attribute12,
1659         p_global_attribute13 => p_global_attribute_rec.global_attribute13,
1660         p_global_attribute14 => p_global_attribute_rec.global_attribute14,
1661         p_global_attribute15 => p_global_attribute_rec.global_attribute15,
1662         p_global_attribute16 => p_global_attribute_rec.global_attribute16,
1663         p_global_attribute17 => p_global_attribute_rec.global_attribute17,
1664         p_global_attribute18 => p_global_attribute_rec.global_attribute18,
1665         p_global_attribute19 => p_global_attribute_rec.global_attribute19,
1666         p_global_attribute20 => p_global_attribute_rec.global_attribute20,
1667         p_comments => p_comments ,
1668 	p_module_name => 'RAPI',
1669 	p_module_version => p_api_version ,
1670         x_application_ref_id => l_application_ref_id,
1671         x_application_ref_num => l_application_ref_num,
1672         x_return_status => l_return_status,
1673         x_msg_count => l_msg_count,
1674         x_msg_data  => l_msg_data,
1675 	p_out_rec_application_id => p_out_rec_application_id ,
1676         p_acctd_amount_applied_from => p_acctd_amount_applied_from,
1677         p_acctd_amount_applied_to => p_acctd_amount_applied_to,
1678         x_claim_reason_name => l_claim_reason_name,
1679 	p_called_from => p_called_from,
1680 	p_move_deferred_tax     => p_move_deferred_tax,
1681         p_link_to_trx_hist_id   => p_link_to_trx_hist_id,
1682         p_amount_due_remaining  => NULL,
1683 	p_payment_set_id        => l_payment_set_id,
1684         p_application_ref_reason => p_application_ref_reason,
1685         p_customer_reference     => p_customer_reference,
1686         p_customer_reason        => p_customer_reason
1687                                );
1688       -- Assign receivable_application_id to package global variable
1689       -- So, it can used to perform follow on operation on given application
1690       apply_out_rec.receivable_application_id := p_out_rec_application_id;
1691 
1692    EXCEPTION
1693      WHEN OTHERS THEN
1694 
1695                /*-------------------------------------------------------+
1696                 |  Handle application errors that result from trapable  |
1697                 |  error conditions. The error messages have already    |
1698                 |  been put on the error stack.                         |
1699                 +-------------------------------------------------------*/
1700 
1701                 IF (SQLCODE = -20001)
1702                 THEN
1703                      ROLLBACK TO Apply_PVT;
1704 
1705                       --  Display_Parameters;
1706                       x_return_status := FND_API.G_RET_STS_ERROR ;
1707                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
1708                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.RECEIPT_APPLICATION '||SQLERRM);
1709                        FND_MSG_PUB.Add;
1710 
1711                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
1712                                                   p_count  =>  x_msg_count,
1713                                                   p_data   => x_msg_data
1714                                                 );
1715                       RETURN;
1716                 ELSE
1717                    RAISE;
1718                 END IF;
1719    END;
1720 
1721    /* Bug 3773036: raising error if return status is not success */
1722    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1723       RAISE FND_API.G_EXC_ERROR;
1724    END IF;
1725    IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1726       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1727    END IF;
1728 
1729       jg_ar_receivable_applications.apply(
1730                   p_apply_before_after        => 'AFTER',
1731                   p_global_attribute_category => l_global_attribute_rec.global_attribute_category,
1732                   p_set_of_books_id           => null,
1733                   p_cash_receipt_id           => null,
1734                   p_receipt_date              => null,
1735                   p_applied_payment_schedule_id => l_applied_payment_schedule_id,
1736                   p_amount_applied              => null,
1737                   p_unapplied_amount            => null,
1738                   p_due_date                    => null,
1739                   p_receipt_method_id           => null,
1740                   p_remittance_bank_account_id  => null,
1741                   p_global_attribute1           => l_global_attribute_rec.global_attribute1,
1742                   p_global_attribute2           => l_global_attribute_rec.global_attribute2,
1743                   p_global_attribute3           => l_global_attribute_rec.global_attribute3,
1744                   p_global_attribute4           => l_global_attribute_rec.global_attribute4,
1745                   p_global_attribute5           => l_global_attribute_rec.global_attribute5,
1746                   p_global_attribute6           => l_global_attribute_rec.global_attribute6,
1747                   p_global_attribute7           => l_global_attribute_rec.global_attribute7,
1748                   p_global_attribute8           => l_global_attribute_rec.global_attribute8,
1749                   p_global_attribute9           => l_global_attribute_rec.global_attribute9,
1750                   p_global_attribute10          => l_global_attribute_rec.global_attribute10,
1751                   p_global_attribute11          => l_global_attribute_rec.global_attribute11,
1752                   p_global_attribute12          => l_global_attribute_rec.global_attribute12,
1753                   p_global_attribute13          => l_global_attribute_rec.global_attribute13,
1754                   p_global_attribute14          => l_global_attribute_rec.global_attribute14,
1755                   p_global_attribute15          => l_global_attribute_rec.global_attribute15,
1756                   p_global_attribute16          => l_global_attribute_rec.global_attribute16,
1757                   p_global_attribute17          => l_global_attribute_rec.global_attribute17,
1758                   p_global_attribute18          => l_global_attribute_rec.global_attribute18,
1759                   p_global_attribute19          => l_global_attribute_rec.global_attribute19,
1760                   p_global_attribute20          => l_global_attribute_rec.global_attribute20,
1761                   p_return_status               => l_gdflex_return_status);
1762        /*--------------------------------+
1763         |   Standard check of p_commit   |
1764         +--------------------------------*/
1765 
1766         IF FND_API.To_Boolean( p_commit )
1767         THEN
1768             IF PG_DEBUG in ('Y', 'C') THEN
1769                arp_util.debug('Apply: ' || 'committing');
1770             END IF;
1771               Commit;
1772         END IF;
1773         IF PG_DEBUG in ('Y', 'C') THEN
1774            arp_util.debug('Apply ()- ');
1775         END IF;
1776 EXCEPTION
1777        WHEN FND_API.G_EXC_ERROR THEN
1778 
1779                 IF PG_DEBUG in ('Y', 'C') THEN
1780                    arp_util.debug('Apply: ' || SQLCODE, G_MSG_ERROR);
1781                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
1782                 END IF;
1783 
1784                 ROLLBACK TO Apply_PVT;
1785                 x_return_status := FND_API.G_RET_STS_ERROR ;
1786 
1787                -- Display_Parameters;
1788 
1789                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
1790                                            p_count       =>      x_msg_count,
1791                                            p_data        =>      x_msg_data
1792                                          );
1793 
1794         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1795 
1796                 IF PG_DEBUG in ('Y', 'C') THEN
1797                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
1798                 END IF;
1799                 ROLLBACK TO Apply_PVT;
1800                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1801 
1802                --  Display_Parameters;
1803 
1804                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
1805                                            p_count       =>      x_msg_count,
1806                                            p_data        =>      x_msg_data
1807                                          );
1808 
1809         WHEN OTHERS THEN
1810 
1811                /*-------------------------------------------------------+
1812                 |  Handle application errors that result from trapable  |
1813                 |  error conditions. The error messages have already    |
1814                 |  been put on the error stack.                         |
1815                 +-------------------------------------------------------*/
1816 
1817                 IF (SQLCODE = -20001)
1818                 THEN
1819 
1820                       ROLLBACK TO Apply_PVT;
1821 
1822                       --If only one error message on the stack,
1823                       --retrive it
1824 
1825                       x_return_status := FND_API.G_RET_STS_ERROR ;
1826                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
1827                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','APPLY : '||SQLERRM);
1828                       FND_MSG_PUB.Add;
1829 
1830                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
1831                                                  p_count  =>  x_msg_count,
1832                                                  p_data   => x_msg_data
1833                                                 );
1834 
1835                       RETURN;
1836 
1837                 ELSE
1838                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1839                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
1840                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','APPLY : '||SQLERRM);
1841                       FND_MSG_PUB.Add;
1842                 END IF;
1843 
1844                 IF PG_DEBUG in ('Y', 'C') THEN
1845                    arp_util.debug('Apply: ' || SQLCODE, G_MSG_ERROR);
1846                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
1847                 END IF;
1848 
1849                 ROLLBACK TO Apply_PVT;
1850 
1851                 IF      FND_MSG_PUB.Check_Msg_Level
1852                 THEN
1853                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
1854                                         l_api_name
1855                                        );
1856                 END IF;
1857 
1858              --   Display_Parameters;
1859 
1860                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
1861                                            p_count       =>      x_msg_count,
1862                                            p_data        =>      x_msg_data
1863                                          );
1864 
1865 END Apply;
1866 
1867 PROCEDURE Apply_In_Detail(
1868 -- Standard API parameters.
1869       p_api_version                 IN  NUMBER,
1870       p_init_msg_list               IN  VARCHAR2 := FND_API.G_FALSE,
1871       p_commit                      IN  VARCHAR2 := FND_API.G_FALSE,
1872       p_validation_level            IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
1873       x_return_status               OUT NOCOPY VARCHAR2,
1874       x_msg_count                   OUT NOCOPY NUMBER,
1875       x_msg_data                    OUT NOCOPY VARCHAR2,
1876  --  Receipt application parameters.
1877       p_cash_receipt_id             IN ar_cash_receipts.cash_receipt_id%TYPE DEFAULT NULL,
1878       p_receipt_number              IN ar_cash_receipts.receipt_number%TYPE DEFAULT NULL,
1879       p_customer_trx_id             IN ra_customer_trx.customer_trx_id%TYPE DEFAULT NULL,
1880       p_trx_number                  IN ra_customer_trx.trx_number%TYPE DEFAULT NULL,
1881       p_installment                 IN ar_payment_schedules.terms_sequence_number%TYPE DEFAULT NULL,
1882       p_applied_payment_schedule_id IN ar_payment_schedules.payment_schedule_id%TYPE DEFAULT NULL,
1883 -- LLCA Parameters
1884       p_llca_type		    IN VARCHAR2 DEFAULT 'S',
1885       p_llca_trx_lines_tbl          IN llca_trx_lines_tbl_type DEFAULT llca_def_trx_lines_tbl_type,
1886       p_group_id                    IN VARCHAR2 DEFAULT NULL,   /* Bug 5284890 */
1887       p_line_amount		    IN ar_receivable_applications.amount_applied%TYPE DEFAULT NULL,
1888       p_tax_amount		    IN ar_receivable_applications.amount_applied%TYPE DEFAULT NULL,
1889       p_freight_amount		    IN ar_receivable_applications.amount_applied%TYPE DEFAULT NULL,
1890       p_charges_amount		    IN ar_receivable_applications.amount_applied%TYPE DEFAULT NULL,
1891       p_amount_applied              IN ar_receivable_applications.amount_applied%TYPE DEFAULT NULL,
1892       p_line_discount               IN NUMBER DEFAULT NULL,
1893       p_tax_discount                IN NUMBER DEFAULT NULL,
1894       p_freight_discount            IN NUMBER DEFAULT NULL,
1895       p_amount_applied_from         IN ar_receivable_applications.amount_applied_from%TYPE DEFAULT NULL,
1896       p_trans_to_receipt_rate       IN ar_receivable_applications.trans_to_receipt_rate%TYPE DEFAULT NULL,
1897       p_discount                    IN ar_receivable_applications.earned_discount_taken%TYPE DEFAULT NULL,
1898       p_apply_date                  IN ar_receivable_applications.apply_date%TYPE DEFAULT NULL,
1899       p_apply_gl_date               IN ar_receivable_applications.gl_date%TYPE DEFAULT NULL,
1900       p_ussgl_transaction_code      IN ar_receivable_applications.ussgl_transaction_code%TYPE DEFAULT NULL,
1901       p_show_closed_invoices        IN VARCHAR2 DEFAULT 'N',
1902       p_called_from                 IN VARCHAR2 DEFAULT NULL,
1903       p_move_deferred_tax           IN VARCHAR2 DEFAULT 'Y',
1904       p_link_to_trx_hist_id         IN ar_receivable_applications.link_to_trx_hist_id%TYPE DEFAULT NULL,
1905       p_attribute_rec               IN attribute_rec_type DEFAULT attribute_rec_const,
1906 	 -- ******* Global Flexfield parameters *******
1907       p_global_attribute_rec        IN global_attribute_rec_type DEFAULT global_attribute_rec_const,
1908       p_comments                    IN ar_receivable_applications.comments%TYPE DEFAULT NULL,
1909       p_payment_set_id              IN ar_receivable_applications.payment_set_id%TYPE DEFAULT NULL,
1910       p_application_ref_type        IN ar_receivable_applications.application_ref_type%TYPE DEFAULT NULL,
1911       p_application_ref_id          IN ar_receivable_applications.application_ref_id%TYPE DEFAULT NULL,
1912       p_application_ref_num         IN ar_receivable_applications.application_ref_num%TYPE DEFAULT NULL,
1913       p_secondary_application_ref_id IN ar_receivable_applications.secondary_application_ref_id%TYPE DEFAULT NULL,
1914       p_application_ref_reason      IN ar_receivable_applications.application_ref_reason%TYPE DEFAULT NULL,
1915       p_customer_reference          IN ar_receivable_applications.customer_reference%TYPE DEFAULT NULL,
1916       p_customer_reason             IN ar_receivable_applications.customer_reason%TYPE DEFAULT NULL,
1917       p_org_id                      IN NUMBER  DEFAULT NULL,
1918       p_line_attribute_rec          IN attribute_rec_type DEFAULT attribute_rec_const
1919       ) IS
1920 
1921 l_api_name			CONSTANT VARCHAR2(20) := 'Apply_In_Detail';
1922 l_api_version			CONSTANT NUMBER       := 1.0;
1923 l_cash_receipt_id		NUMBER;
1924 l_cr_gl_date			DATE;
1925 l_cr_date			DATE;
1926 l_cr_amount			NUMBER;
1927 l_cr_unapp_amount		NUMBER;
1928 l_cr_currency_code		VARCHAR2(15);
1929 l_customer_trx_id		NUMBER(15);
1930 l_installment			NUMBER;
1931 l_customer_trx_line_id		NUMBER(15);
1932 l_trx_due_date			DATE;
1933 l_trx_currency_code		VARCHAR2(15);
1934 l_trx_gl_date			DATE;
1935 l_apply_gl_date			DATE;
1936 l_calc_discount_on_lines_flag	VARCHAR2(1);
1937 l_partial_discount_flag		VARCHAR2(1);
1938 l_allow_overappln_flag		VARCHAR2(1);
1939 l_natural_appln_only_flag	VARCHAR2(1);
1940 l_creation_sign			VARCHAR2(30);
1941 l_amount_applied		NUMBER;
1942 l_amount_applied_from		NUMBER;
1943 l_line_amount			NUMBER;
1944 l_tax_amount    		NUMBER;
1945 l_freight_amount  		NUMBER;
1946 l_charges_amount  		NUMBER;
1947 l_line_discount  		NUMBER;
1948 l_tax_discount  		NUMBER;
1949 l_freight_discount  		NUMBER;
1950 l_trans_to_receipt_rate		NUMBER;
1951 l_applied_payment_schedule_id	NUMBER;
1952 l_cr_payment_schedule_id	NUMBER;
1953 l_apply_date			DATE;
1954 l_trx_date			DATE;
1955 l_discount			NUMBER;
1956 l_discount_earned_allowed	NUMBER;
1957 l_discount_max_allowed		NUMBER;
1958 l_term_id			NUMBER;
1959 l_trx_line_amount		NUMBER;
1960 l_amount_due_original		NUMBER;
1961 l_amount_due_remaining		NUMBER;
1962 l_discount_earned		NUMBER;
1963 l_discount_unearned		NUMBER;
1964 l_new_amount_due_remaining	NUMBER;
1965 p_out_rec_application_id	NUMBER;
1966 p_acctd_amount_applied_from	NUMBER;
1967 p_acctd_amount_applied_to	NUMBER;
1968 l_def_return_status		VARCHAR2(1);
1969 l_val_return_status		VARCHAR2(1);
1970 l_app_validation_return_status	VARCHAR2(1);
1971 l_def_ids_return_status		VARCHAR2(1);
1972 l_dflex_val_return_status	VARCHAR2(1);
1973 l_attribute_rec			attribute_rec_type;
1974 l_global_attribute_rec		global_attribute_rec_type;
1975 l_remit_bank_acct_use_id	NUMBER;
1976 l_receipt_method_id             NUMBER;
1977 l_gdflex_return_status		VARCHAR2(1);
1978 l_application_ref_id            ar_receivable_applications.application_ref_id%TYPE;
1979 l_application_ref_num           ar_receivable_applications.application_ref_num%TYPE;
1980 l_return_status			VARCHAR2(1);
1981 l_msg_count			NUMBER;
1982 l_msg_data			VARCHAR2(2000);
1983 l_payment_set_id		NUMBER;
1984 l_amount_due_remain_disc	NUMBER;
1985 l_claim_reason_name		VARCHAR2(100);
1986 l_org_return_status		VARCHAR2(1);
1987 l_gt_return_status		VARCHAR2(1);
1988 l_org_id			NUMBER;
1989 
1990 -- LLCA
1991 l_line_items_original		NUMBER;
1992 l_line_items_remaining		NUMBER;
1993 l_tax_original			NUMBER;
1994 l_tax_remaining			NUMBER;
1995 l_freight_original		NUMBER;
1996 l_freight_remaining		NUMBER;
1997 l_rec_charges_charged		NUMBER;
1998 l_rec_charges_remaining		NUMBER;
1999 l_llca_type			VARCHAR2(1);
2000 l_group_id                      ra_customer_trx_lines.source_data_key4%TYPE;
2001 l_sum_amount_applied		NUMBER :=0;
2002 l_sum_disc_earn_allow           NUMBER;
2003 l_sum_disc_max_allow           NUMBER;
2004 l_llca_msg_data			VARCHAR2(2000);
2005 l_llca_return_status		VARCHAR2(1);
2006 l_llca_msg_count		NUMBER;
2007 l_llca_app_msg_data	        VARCHAR2(2000);
2008 l_llca_app_return_status	VARCHAR2(1);
2009 l_llca_app_msg_count		NUMBER;
2010 lfc_msg_data			VARCHAR2(2000);
2011 lfc_return_status		VARCHAR2(1);
2012 lfc_msg_count			NUMBER;
2013 llca_ra_rec			ar_receivable_applications%rowtype;
2014 l_llca_trx_lines_tbl		llca_trx_lines_tbl_type;
2015 l_line_number                   NUMBER;
2016 l_count				NUMBER;
2017 l_ad_dflex_val_return_status    VARCHAR2(1);
2018 l_line_attribute_rec            attribute_rec_type;
2019 BEGIN
2020 
2021      /*------------------------------------+
2022       |   Standard start of API savepoint  |
2023       +------------------------------------*/
2024        SAVEPOINT Apply_In_Detail_PVT;
2025 
2026       /*--------------------------------------------------+
2027        |   Standard call to check for call compatibility  |
2028        +--------------------------------------------------*/
2029         IF NOT FND_API.Compatible_API_Call(
2030                                            l_api_version,
2031                                            p_api_version,
2032                                            l_api_name,
2033                                            G_PKG_NAME
2034                                           )
2035         THEN
2036             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2037         END IF;
2038 
2039       /*--------------------------------------------------------------+
2040        |   Initialize message list if p_init_msg_list is set to TRUE  |
2041        +--------------------------------------------------------------*/
2042 
2043  	IF FND_API.to_Boolean( p_init_msg_list )
2044         THEN
2045               FND_MSG_PUB.initialize;
2046         END IF;
2047 
2048 	Original_application_info.cash_receipt_id      := p_cash_receipt_id;
2049 	Original_application_info.receipt_number       := p_receipt_number;
2050 	Original_application_info.customer_trx_id      := p_customer_trx_id;
2051 	Original_application_info.trx_number           := p_trx_number;
2052 	Original_application_info.installment          := p_installment;
2053 	Original_application_info.customer_trx_line_id := NULL;
2054 	Original_application_info.line_number          := NULL;
2055 	Original_application_info.amount_applied_from:= p_amount_applied_from;
2056 	Original_application_info.trans_to_receipt_rate
2057 			:= p_trans_to_receipt_rate;
2058 	Original_application_info.applied_payment_schedule_id
2059 			:= p_applied_payment_schedule_id;
2060         IF PG_DEBUG in ('Y', 'C') THEN
2061            arp_util.debug('Apply_In_Detail()+ ');
2062         END IF;
2063 
2064        /*-----------------------------------------+
2065         |   Initialize return status to SUCCESS   |
2066         +-----------------------------------------*/
2067         x_return_status := FND_API.G_RET_STS_SUCCESS;
2068 
2069        /* SSA change */
2070         l_org_id            := p_org_id;
2071         l_org_return_status := FND_API.G_RET_STS_SUCCESS;
2072         ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
2073                                                p_return_status =>l_org_return_status);
2074 
2075 IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2076       x_return_status := FND_API.G_RET_STS_ERROR;
2077 ELSE
2078 
2079 
2080        /*-------------------------------------------------+
2081         | Initialize the profile option package variables |
2082         +-------------------------------------------------*/
2083 
2084           initialize_profile_globals;
2085 
2086       /*---------------------------------------------+
2087        |   ===== Dump the Input Parameters ========  |
2088        +---------------------------------------------*/
2089 
2090        IF PG_DEBUG in ('Y', 'C') THEN
2091         arp_util.debug('=======================================================');
2092         arp_util.debug('   Apply_In_Detail (    INPUT PARAMETERS ........)+    ');
2093         arp_util.debug('=======================================================');
2094         arp_util.debug('p_api_version                 =>'||to_char(p_api_version));
2095         arp_util.debug('p_init_msg_list               =>'||p_init_msg_list);
2096         arp_util.debug('p_commit                      =>'||p_commit);
2097         arp_util.debug('p_validation_level            =>'||to_char(p_validation_level));
2098         arp_util.debug('p_cash_receipt_id             =>'||to_char(p_cash_receipt_id));
2099         arp_util.debug('p_receipt_number              =>'||p_receipt_number);
2100         arp_util.debug('p_customer_trx_id             =>'||to_char(p_customer_trx_id));
2101         arp_util.debug('p_trx_number                  =>'||p_trx_number);
2102         arp_util.debug('p_installment                 =>'||p_installment);
2103         arp_util.debug('p_applied_payment_schedule_id =>'||to_char(p_applied_payment_schedule_id));
2104         arp_util.debug('p_llca_type		      =>'||p_llca_type);
2105         arp_util.debug('p_group_id                    =>'||p_group_id);
2106         arp_util.debug('p_line_amount		      =>'||to_char(p_line_amount));
2107         arp_util.debug('p_tax_amount		      =>'||to_char(p_tax_amount));
2108         arp_util.debug('p_freight_amount	      =>'||to_char(p_freight_amount));
2109         arp_util.debug('p_charges_amount	      =>'||to_char(p_charges_amount));
2110         arp_util.debug('p_amount_applied              =>'||to_char(p_amount_applied));
2111         arp_util.debug('p_line_discount               =>'||to_char(p_line_discount));
2112         arp_util.debug('p_tax_discount                =>'||to_char(p_tax_discount));
2113         arp_util.debug('p_freight_discount            =>'||to_char(p_freight_discount));
2114         arp_util.debug('p_amount_applied_from         =>'||to_char(p_amount_applied_from));
2115         arp_util.debug('p_trans_to_receipt_rate       =>'||to_char(p_trans_to_receipt_rate));
2116         arp_util.debug('p_discount                    =>'||to_char(p_discount));
2117         arp_util.debug('p_apply_date                  =>'||to_char(p_apply_date));
2118         arp_util.debug('p_apply_gl_date               =>'||to_char(p_apply_gl_date));
2119         arp_util.debug('p_ussgl_transaction_code      =>'||p_ussgl_transaction_code);
2120         arp_util.debug('p_show_closed_invoices        =>'||p_show_closed_invoices);
2121         arp_util.debug('p_called_from                 =>'||p_called_from);
2122         arp_util.debug('p_move_deferred_tax           =>'||p_move_deferred_tax);
2123         arp_util.debug('p_link_to_trx_hist_id         =>'||p_link_to_trx_hist_id);
2124         arp_util.debug('p_comments                    =>'||p_comments);
2125         arp_util.debug('p_payment_set_id              =>'||p_payment_set_id);
2126         arp_util.debug('p_application_ref_type        =>'||p_application_ref_type);
2127         arp_util.debug('p_application_ref_id          =>'||p_application_ref_id);
2128         arp_util.debug('p_application_ref_num         =>'||p_application_ref_num);
2129         arp_util.debug('p_secondary_application_ref_id=>'||p_secondary_application_ref_id);
2130         arp_util.debug('p_application_ref_reason      =>'||p_application_ref_reason);
2131         arp_util.debug('p_customer_reference          =>'||p_customer_reference);
2132         arp_util.debug('p_customer_reason             =>'||p_customer_reason);
2133         arp_util.debug('p_org_id                      =>'||p_org_id);
2134         arp_util.debug('=======================================================');
2135        END IF;
2136 
2137       /*---------------------------------------------+
2138        |   ========== Start of API Body ==========   |
2139        +---------------------------------------------*/
2140 
2141 
2142      l_cash_receipt_id          := p_cash_receipt_id;
2143      l_customer_trx_id          := p_customer_trx_id;
2144      l_installment              := p_installment;
2145      l_amount_applied           := p_amount_applied;
2146      l_amount_applied_from      := p_amount_applied_from;
2147      l_trans_to_receipt_rate    := p_trans_to_receipt_rate;
2148      l_discount                 := Nvl(p_line_discount,0)+ Nvl(p_tax_discount,0)
2149                                    + Nvl(p_freight_discount,0);
2150      l_apply_date               := trunc(p_apply_date);
2151      l_apply_gl_date            := trunc(p_apply_gl_date);
2152      l_customer_trx_line_id     := NULL;
2153      l_applied_payment_schedule_id := p_applied_payment_schedule_id;
2154      l_attribute_rec            := p_attribute_rec;
2155      l_line_attribute_rec       := p_line_attribute_rec;
2156      l_global_attribute_rec     := p_global_attribute_rec;
2157      l_payment_set_id           := p_payment_set_id;
2158 
2159      l_llca_type                := p_llca_type;
2160      l_group_id                 := p_group_id; /* Bug 5284890 */
2161      l_line_amount              := p_line_amount;
2162      l_tax_amount               := p_tax_amount;
2163      l_freight_amount           := p_freight_amount;
2164      l_charges_amount           := p_charges_amount;
2165      l_line_discount            := p_line_discount;
2166      l_tax_discount             := p_tax_discount;
2167      l_freight_discount         := p_freight_discount;
2168      l_ad_dflex_val_return_status := FND_API.G_RET_STS_SUCCESS;
2169 
2170      ar_receipt_lib_pvt.populate_llca_gt(
2171 	   p_customer_trx_id              => l_customer_trx_id,
2172 	   p_llca_type                    => l_llca_type,
2173            p_llca_trx_lines_tbl		  => p_llca_trx_lines_tbl,
2174 	   p_line_amount		  => l_line_amount,
2175 	   p_tax_amount			  => l_tax_amount,
2176   	   p_freight_amount		  => l_freight_amount,
2177 	   p_charges_amount		  => l_charges_amount,
2178 	   p_line_discount		  => l_line_discount,
2179 	   p_tax_discount		  => l_tax_discount,
2180 	   p_freight_discount		  => l_freight_discount,
2181 	   p_amount_applied		  => l_amount_applied,
2182 	   p_amount_applied_from	  => l_amount_applied_from,
2183            p_return_status                => l_gt_return_status);
2184 
2185    IF PG_DEBUG in ('Y', 'C') THEN
2186       arp_util.debug('Apply_In_Detail: ' || 'Plsql table Return_status = '||l_gt_return_status);
2187    END IF;
2188 
2189        IF l_gt_return_status <> FND_API.G_RET_STS_SUCCESS
2190        THEN
2191             ROLLBACK TO Apply_In_Detail_PVT;
2192             x_return_status := FND_API.G_RET_STS_ERROR ;
2193 
2194 	    IF PG_DEBUG in ('Y', 'C') THEN
2195                 arp_util.debug('Apply_In_Detail: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
2196             END IF;
2197             Return;
2198         END IF;
2199 
2200          /*-----------------------+
2201           |                       |
2202           |ID TO VALUE CONVERSION |
2203           |                       |
2204           +-----------------------*/
2205       ar_receipt_lib_pvt.Default_appln_ids(
2206        p_cash_receipt_id              => l_cash_receipt_id,
2207        p_receipt_number               => p_receipt_number,
2208        p_customer_trx_id              => l_customer_trx_id,
2209        p_trx_number                   => p_trx_number,
2210        p_customer_trx_line_id	      => l_customer_trx_line_id,
2211        p_line_number                  => l_line_number,
2212        p_installment                  => l_installment,
2213        p_applied_payment_schedule_id  => l_applied_payment_schedule_id,
2214        p_llca_type                    => l_llca_type,
2215        p_group_id                     => l_group_id,
2216        p_return_status                => l_def_ids_return_status);
2217 
2218    IF PG_DEBUG in ('Y', 'C') THEN
2219       arp_util.debug('Apply_In_Detail: ' || 'Defaulting Ids Return_status = '||l_def_ids_return_status);
2220    END IF;
2221 
2222  -- Inorder to retained the errors on GT, we need to return the call without rollback.
2223  IF l_def_ids_return_status <> FND_API.G_RET_STS_SUCCESS
2224  Then
2225      select count(1) into l_count from ar_llca_trx_errors_gt
2226      where customer_trx_id = p_customer_trx_id;
2227 
2228      If l_count <> 0 AND p_llca_type = 'L'
2229      THEN
2230              x_return_status := FND_API.G_RET_STS_ERROR ;
2231 
2232              IF PG_DEBUG in ('Y', 'C') THEN
2233                 arp_util.debug('Apply_In_Detail: ' || 'Error(s) occurred in PLSQL table parameters. ');
2234              END IF;
2235              Return;
2236      END IF;
2237   END IF ;
2238           /*---------------------+
2239            |                     |
2240            |    DEFAULTING       |
2241            |                     |
2242            +---------------------*/
2243    IF PG_DEBUG in ('Y', 'C') THEN
2244       arp_util.debug('Apply_In_Detail: ' || 'l_amount_applied_from :'||to_char(l_amount_applied_from));
2245    END IF;
2246          ar_receipt_lib_pvt.Default_application_info(
2247               p_cash_receipt_id             => l_cash_receipt_id   ,
2248               p_cr_gl_date                  => l_cr_gl_date,
2249               p_cr_date                     => l_cr_date,
2250               p_cr_amount                   => l_cr_amount,
2251               p_cr_unapp_amount             => l_cr_unapp_amount,
2252               p_cr_currency_code            => l_cr_currency_code ,
2253               p_customer_trx_id             => l_customer_trx_id,
2254               p_installment                 => l_installment,
2255               p_show_closed_invoices        => p_show_closed_invoices,
2256               p_customer_trx_line_id        => l_customer_trx_line_id,
2257               p_trx_due_date                => l_trx_due_date,
2258               p_trx_currency_code           => l_trx_currency_code,
2259               p_trx_date                    => l_trx_date,
2260               p_trx_gl_date                 => l_trx_gl_date,
2261               p_apply_gl_date               => l_apply_gl_date,
2262               p_calc_discount_on_lines_flag => l_calc_discount_on_lines_flag,
2263               p_partial_discount_flag       => l_partial_discount_flag,
2264               p_allow_overappln_flag        => l_allow_overappln_flag,
2265               p_natural_appln_only_flag     => l_natural_appln_only_flag,
2266               p_creation_sign               => l_creation_sign ,
2267               p_cr_payment_schedule_id      => l_cr_payment_schedule_id ,
2268               p_applied_payment_schedule_id => l_applied_payment_schedule_id ,
2269               p_term_id                     => l_term_id ,
2270               p_amount_due_original         => l_amount_due_original ,
2271 	      p_amount_due_remaining        => l_amount_due_remaining,
2272               p_trx_line_amount             => l_trx_line_amount,
2273               p_discount                    => l_discount,
2274               p_apply_date                  => l_apply_date ,
2275               p_discount_max_allowed        => l_discount_max_allowed,
2276               p_discount_earned_allowed     => l_discount_earned_allowed,
2277               p_discount_earned             => l_discount_earned,
2278               p_discount_unearned           => l_discount_unearned ,
2279               p_new_amount_due_remaining    => l_new_amount_due_remaining,
2280               p_remittance_bank_account_id  => l_remit_bank_acct_use_id,
2281               p_receipt_method_id           => l_receipt_method_id,
2282               p_amount_applied              => l_amount_applied,
2283               p_amount_applied_from         => l_amount_applied_from,
2284               p_trans_to_receipt_rate       => l_trans_to_receipt_rate,
2285 	      p_llca_type		    => l_llca_type,
2286               p_line_amount		    => l_line_amount,
2287               p_tax_amount		    => l_tax_amount,
2288               p_freight_amount		    => l_freight_amount,
2289               p_charges_amount		    => l_charges_amount,
2290               p_line_discount               => l_line_discount,
2291               p_tax_discount                => l_tax_discount,
2292               p_freight_discount            => l_freight_discount,
2293 	      p_line_items_original	    => l_line_items_original,
2294 	      p_line_items_remaining	    => l_line_items_remaining,
2295 	      p_tax_original		    => l_tax_original,
2296 	      p_tax_remaining		    => l_tax_remaining,
2297 	      p_freight_original	    => l_freight_original,
2298 	      p_freight_remaining	    => l_freight_remaining,
2299 	      p_rec_charges_charged	    => l_rec_charges_charged,
2300 	      p_rec_charges_remaining	    => l_rec_charges_remaining,
2301               p_called_from                 => p_called_from,
2302               p_return_status               => l_def_return_status
2303                );
2304 IF PG_DEBUG in ('Y', 'C') THEN
2305    arp_util.debug('Apply_In_Detail: ' || 'l_amount_applied      :'||to_char(l_amount_applied));
2306    arp_util.debug('Apply_In_Detail: ' || 'l_amount_applied_from :'||to_char(l_amount_applied_from));
2307    arp_util.debug('Apply_In_Detail: ' || 'l_new_amount_due_remaining :'||to_char(l_new_amount_due_remaining));
2308    arp_util.debug('Apply_In_Detail: ' || 'Defaulting Return_status = '||l_def_return_status);
2309 END IF;
2310 
2311           /*---------------------+
2312            |                     |
2313            |    VALIDATION       |
2314            |                     |
2315            +---------------------*/
2316  IF l_def_return_status = FND_API.G_RET_STS_SUCCESS  AND
2317     l_def_ids_return_status = FND_API.G_RET_STS_SUCCESS
2318  THEN
2319           ar_receipt_val_pvt.Validate_Application_info(
2320                p_apply_date                  => l_apply_date,
2321                p_cr_date                     => l_cr_date,
2322                p_trx_date                    => l_trx_date,
2323                p_apply_gl_date               => l_apply_gl_date  ,
2324                p_trx_gl_date                 => l_trx_gl_date,
2325                p_cr_gl_date                  => l_cr_gl_date,
2326                p_amount_applied              => l_amount_applied,
2327                p_applied_payment_schedule_id => l_applied_payment_schedule_id,
2328                p_customer_trx_line_id        => l_customer_trx_line_id,
2329                p_inv_line_amount             => l_trx_line_amount,
2330                p_creation_sign               => l_creation_sign,
2331                p_allow_overappln_flag	     => l_allow_overappln_flag,
2332                p_natural_appln_only_flag     => l_natural_appln_only_flag,
2333                p_discount                    => l_discount  ,
2334                p_amount_due_remaining        => l_amount_due_remaining,
2335                p_amount_due_original         => l_amount_due_original,
2336                p_trans_to_receipt_rate       => l_trans_to_receipt_rate,
2337                p_cr_currency_code            => l_cr_currency_code ,
2338                p_trx_currency_code           => l_trx_currency_code,
2339                p_amount_applied_from         => l_amount_applied_from,
2340                p_cr_unapp_amount             => l_cr_unapp_amount ,
2341                p_partial_discount_flag       => l_partial_discount_flag ,
2342                p_discount_earned_allowed     => l_discount_earned_allowed,
2343                p_discount_max_allowed        => l_discount_max_allowed ,
2344                p_move_deferred_tax           => p_move_deferred_tax,
2345  	       p_llca_type		     => l_llca_type,
2346  	       p_line_amount		     => l_line_amount,
2347 	       p_tax_amount		     => l_tax_amount,
2348 	       p_freight_amount		     => l_freight_amount,
2349 	       p_charges_amount		     => l_charges_amount,
2350                p_line_discount               => l_line_discount,
2351                p_tax_discount                => l_tax_discount,
2352                p_freight_discount            => l_freight_discount,
2353 	       p_line_items_original	     => l_line_items_original,
2354 	       p_line_items_remaining	     => l_line_items_remaining,
2355 	       p_tax_original		     => l_tax_original,
2356 	       p_tax_remaining		     => l_tax_remaining,
2357 	       p_freight_original	     => l_freight_original,
2358 	       p_freight_remaining	     => l_freight_remaining,
2359 	       p_rec_charges_charged	     => l_rec_charges_charged,
2360 	       p_rec_charges_remaining	     => l_rec_charges_remaining,
2361                p_return_status               => l_val_return_status
2362                           );
2363 
2364                IF PG_DEBUG in ('Y', 'C') THEN
2365                    arp_util.debug('Apply_In_Detail: ' || 'Validation Return_status = '||l_val_return_status);
2366                END IF;
2367 
2368         --validate and default the flexfields
2369         ar_receipt_lib_pvt.Validate_Desc_Flexfield(
2370                                         l_attribute_rec,
2371                                         'AR_RECEIVABLE_APPLICATIONS',
2372                                         l_dflex_val_return_status
2373                                                 );
2374 
2375 IF PG_DEBUG in ('Y', 'C') THEN
2376    arp_util.debug('Apply_In_Detail: ' || 'l_line_amount         :'||to_char(l_line_amount));
2377    arp_util.debug('Apply_In_Detail: ' || 'l_tax_amount          :'||to_char(l_tax_amount));
2378    arp_util.debug('Apply_In_Detail: ' || 'l_freight_amount      :'||to_char(l_freight_amount));
2379    arp_util.debug('Apply_In_Detail: ' || 'l_charges_amount      :'||to_char(l_charges_amount));
2380    arp_util.debug('Apply_In_Detail: ' || 'l_amount_applied      :'||to_char(l_amount_applied));
2381    arp_util.debug('Apply_In_Detail: ' || 'l_line_discount       :'||to_char(l_line_discount));
2382    arp_util.debug('Apply_In_Detail: ' || 'l_tax_discount        :'||to_char(l_tax_discount));
2383    arp_util.debug('Apply_In_Detail: ' || 'l_freight_discount    :'||to_char(l_freight_discount));
2384    arp_util.debug('Apply_In_Detail: ' || 'l_amount_applied_from :'||to_char(l_amount_applied_from));
2385    arp_util.debug('Apply_In_Detail: ' || 'Validate Return_status = '||l_val_return_status);
2386 End If;
2387 --bug7311231
2388        --validate and default the flexfields
2389        IF l_llca_type = 'S' OR l_llca_type = 'G'
2390         OR (l_llca_type = 'L' AND p_llca_trx_lines_tbl.COUNT = 0) THEN
2391           ar_receipt_lib_pvt.Validate_Desc_Flexfield(
2392                                        l_line_attribute_rec,
2393                                        'AR_ACTIVITY_DETAILS',
2394                                        l_ad_dflex_val_return_status
2395                                                );
2396        END IF;
2397        IF PG_DEBUG in ('Y', 'C') THEN
2398         arp_util.debug('Desc Flexfield Validation return status: '||l_ad_dflex_val_return_status);
2399        END IF;
2400 
2401        -- Validate the LLCA data and populate to AD and GT
2402        -- bug7311231, Modified the parameter list passing the flexfield info.
2403        ar_receipt_val_pvt.validate_llca_insert_ad(
2404 	p_cash_receipt_id        => l_cash_receipt_id
2405 	,p_customer_trx_id       => l_customer_trx_id
2406         ,p_customer_trx_line_id  => l_customer_trx_line_id
2407 	,p_cr_unapp_amount       => l_cr_unapp_amount
2408         ,p_llca_type             => l_llca_type
2409         ,p_group_id              => l_group_id
2410         ,p_line_amount           => l_line_amount
2411 	,p_tax_amount            => l_tax_amount
2412 	,p_freight_amount        => l_freight_amount
2413 	,p_charges_amount        => l_charges_amount
2414 	,p_line_discount         => l_line_discount
2415 	,p_tax_discount          => l_tax_discount
2416 	,p_freight_discount      => l_freight_discount
2417 	,p_amount_applied        => l_amount_applied
2418 	,p_amount_applied_from   => l_amount_applied_from
2419 	,p_trans_to_receipt_rate => l_trans_to_receipt_rate
2420 	,p_invoice_currency_code => l_trx_currency_code
2421 	,p_receipt_currency_code => l_cr_currency_code
2422 	,p_earned_discount       => l_discount_earned_allowed
2423 	,p_unearned_discount     => l_discount_unearned
2424 	,p_max_discount          => l_discount_max_allowed
2425 	,p_line_items_original	 => l_line_items_original
2426 	,p_line_items_remaining	 => l_line_items_remaining
2427 	,p_tax_original		 => l_tax_original
2428 	,p_tax_remaining	 => l_tax_remaining
2429 	,p_freight_original	 => l_freight_original
2430 	,p_freight_remaining	 => l_freight_remaining
2431 	,p_rec_charges_charged	 => l_rec_charges_charged
2432 	,p_rec_charges_remaining => l_rec_charges_remaining
2433 	,p_attribute_category	 => l_line_attribute_rec.attribute_category
2434 	,p_attribute1		 => l_line_attribute_rec.attribute1
2435         ,p_attribute2		 => l_line_attribute_rec.attribute2
2436         ,p_attribute3		 => l_line_attribute_rec.attribute3
2437         ,p_attribute4	  	 => l_line_attribute_rec.attribute4
2438         ,p_attribute5		 => l_line_attribute_rec.attribute5
2439         ,p_attribute6		 => l_line_attribute_rec.attribute6
2440         ,p_attribute7		 => l_line_attribute_rec.attribute7
2441         ,p_attribute8		 => l_line_attribute_rec.attribute8
2442         ,p_attribute9		 => l_line_attribute_rec.attribute9
2443         ,p_attribute10		 => l_line_attribute_rec.attribute10
2444         ,p_attribute11		 => l_line_attribute_rec.attribute11
2445         ,p_attribute12		 => l_line_attribute_rec.attribute12
2446         ,p_attribute13		 => l_line_attribute_rec.attribute13
2447         ,p_attribute14		 => l_line_attribute_rec.attribute14
2448         ,p_attribute15		 => l_line_attribute_rec.attribute15
2449         ,p_comments		 => p_comments
2450 	,p_return_status	 => l_llca_return_status
2451 	,p_msg_count		 => l_llca_msg_count
2452 	,p_msg_data		 => l_llca_msg_data
2453 	);
2454    END IF;
2455 
2456        IF l_llca_return_status    = 'X'
2457        Then
2458              x_return_status := FND_API.G_RET_STS_ERROR ;
2459              Return;
2460        END IF ;
2461 
2462        IF l_def_ids_return_status <> FND_API.G_RET_STS_SUCCESS OR
2463           l_val_return_status     <> FND_API.G_RET_STS_SUCCESS OR
2464           l_def_return_status     <> FND_API.G_RET_STS_SUCCESS  OR
2465           l_dflex_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
2466 	  l_ad_dflex_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
2467           l_app_validation_return_status <> FND_API.G_RET_STS_SUCCESS OR
2468           l_llca_return_status    <> FND_API.G_RET_STS_SUCCESS  THEN
2469           x_return_status := FND_API.G_RET_STS_ERROR;
2470        END IF;
2471 
2472        /* Bug fix 3435834
2473           The messages should be retrieved irrespective of the return status */
2474          FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
2475                                         p_count => x_msg_count,
2476                                         p_data  => x_msg_data
2477                                        );
2478 
2479        IF x_return_status <> FND_API.G_RET_STS_SUCCESS
2480        THEN
2481 
2482             ROLLBACK TO Apply_In_Detail_PVT;
2483 
2484              x_return_status := FND_API.G_RET_STS_ERROR ;
2485 
2486              IF PG_DEBUG in ('Y', 'C') THEN
2487                 arp_util.debug('Apply_In_Detail: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
2488              END IF;
2489              Return;
2490         END IF;
2491 
2492      -- Validate llca and insert application
2493      ar_receipt_val_pvt.validate_llca_insert_app(
2494 	p_cash_receipt_id       => l_cash_receipt_id
2495 	,p_customer_trx_id       => l_customer_trx_id
2496         ,p_disc_earn_allowed     => l_discount_earned_allowed
2497         ,p_disc_max_allowed      => l_discount_max_allowed
2498 	,p_return_status	 => l_llca_app_return_status
2499 	,p_msg_count		 => l_llca_app_msg_count
2500 	,p_msg_data		 => l_llca_app_msg_data
2501          );
2502 
2503 
2504 
2505        --default and validate the global descriptive flexfield
2506         jg_ar_receivable_applications.apply(
2507                   p_apply_before_after        => 'BEFORE',
2508                   p_global_attribute_category => l_global_attribute_rec.global_attribute_category,
2509                   p_set_of_books_id           => arp_global.set_of_books_id,
2510                   p_cash_receipt_id           => l_cash_receipt_id,
2511                   p_receipt_date              => l_cr_date,
2512                   p_applied_payment_schedule_id => l_applied_payment_schedule_id,
2513                   p_amount_applied              => l_sum_amount_applied,
2514                   p_unapplied_amount            => (l_cr_unapp_amount
2515                                     -l_sum_amount_applied),
2516                   p_due_date                    => l_trx_due_date,
2517                   p_receipt_method_id           => l_receipt_method_id,
2518                   p_remittance_bank_account_id  => l_remit_bank_acct_use_id,
2519                   p_global_attribute1           => l_global_attribute_rec.global_attribute1,
2520                   p_global_attribute2           => l_global_attribute_rec.global_attribute2,
2521                   p_global_attribute3           => l_global_attribute_rec.global_attribute3,
2522                   p_global_attribute4           => l_global_attribute_rec.global_attribute4,
2523                   p_global_attribute5           => l_global_attribute_rec.global_attribute5,
2524                   p_global_attribute6           => l_global_attribute_rec.global_attribute6,
2525                   p_global_attribute7           => l_global_attribute_rec.global_attribute7,
2526                   p_global_attribute8           => l_global_attribute_rec.global_attribute8,
2527                   p_global_attribute9           => l_global_attribute_rec.global_attribute9,
2528                   p_global_attribute10          => l_global_attribute_rec.global_attribute10,
2529                   p_global_attribute11          => l_global_attribute_rec.global_attribute11,
2530                   p_global_attribute12          => l_global_attribute_rec.global_attribute12,
2531                   p_global_attribute13          => l_global_attribute_rec.global_attribute13,
2532                   p_global_attribute14          => l_global_attribute_rec.global_attribute14,
2533                   p_global_attribute15          => l_global_attribute_rec.global_attribute15,
2534                   p_global_attribute16          => l_global_attribute_rec.global_attribute16,
2535                   p_global_attribute17          => l_global_attribute_rec.global_attribute17,
2536                   p_global_attribute18          => l_global_attribute_rec.global_attribute18,
2537                   p_global_attribute19          => l_global_attribute_rec.global_attribute19,
2538                   p_global_attribute20          => l_global_attribute_rec.global_attribute20,
2539                   p_return_status               => l_gdflex_return_status);
2540 
2541 END IF;
2542 
2543        IF l_llca_app_return_status <> FND_API.G_RET_STS_SUCCESS OR
2544           l_gdflex_return_status  <> FND_API.G_RET_STS_SUCCESS THEN
2545             x_return_status := FND_API.G_RET_STS_ERROR;
2546        END IF;
2547 
2548        /* Bug fix 3435834
2549           The messages should be retrieved irrespective of the return status */
2550          FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
2551                                         p_count => x_msg_count,
2552                                         p_data  => x_msg_data
2553                                        );
2554 
2555        IF x_return_status <> FND_API.G_RET_STS_SUCCESS
2556          THEN
2557 
2558             ROLLBACK TO Apply_In_Detail_PVT;
2559 
2560              x_return_status := FND_API.G_RET_STS_ERROR ;
2561 
2562              IF PG_DEBUG in ('Y', 'C') THEN
2563                 arp_util.debug('Apply_In_Detail: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
2564              END IF;
2565              Return;
2566         END IF;
2567 
2568       --Dump the input variables to the entity handler
2569     IF PG_DEBUG in ('Y', 'C') THEN
2570        arp_util.debug('Apply_In_Detail: ' || 'l_cr_payment_schedule_id      : '||to_char(l_cr_payment_schedule_id));
2571        arp_util.debug('Apply_In_Detail: ' || 'l_applied_payment_schedule_id : '||to_char(l_applied_payment_schedule_id));
2572        arp_util.debug('Apply_In_Detail: ' || 'l_amount_applied              : '||to_char(l_amount_applied));
2573        arp_util.debug('Apply_In_Detail: ' || 'l_amount_applied_from         : '||to_char(l_amount_applied_from));
2574        arp_util.debug('Apply_In_Detail: ' || 'l_trans_to_receipt_rate       : '||to_char(l_trans_to_receipt_rate));
2575        arp_util.debug('Apply_In_Detail: ' || 'l_trx_currency_code           : '||l_trx_currency_code);
2576        arp_util.debug('Apply_In_Detail: ' || 'l_cr_currency_code            : '||l_cr_currency_code);
2577        arp_util.debug('Apply_In_Detail: ' || 'l_discount_earned             : '||to_char(l_discount_earned));
2578        arp_util.debug('Apply_In_Detail: ' || 'l_discount_unearned           : '||to_char(l_discount_unearned));
2579        arp_util.debug('l_apply_date                  : '||to_char(l_apply_date,'DD-MON-YY'));
2580        arp_util.debug('l_apply_gl_date               : '||to_char(l_apply_gl_date,'DD-MON-YY'));
2581        arp_util.debug('Apply_In_Detail: ' || 'l_customer_trx_line_id        : '||to_char(l_customer_trx_line_id));
2582     END IF;
2583 
2584        --lock the receipt before calling the entity handler
2585        arp_cash_receipts_pkg.nowaitlock_p(p_cr_id => l_cash_receipt_id);
2586 
2587        -- lock the payment schedule of the applied transaction
2588        arp_ps_pkg.nowaitlock_p (p_ps_id => l_applied_payment_schedule_id);
2589 
2590        l_return_status := FND_API.G_RET_STS_SUCCESS;
2591 
2592    BEGIN
2593 	     --call the entity handler
2594 	      arp_process_det_pkg.final_commit(
2595 		p_gl_date  => l_apply_gl_date,
2596  		p_apply_date => l_apply_date ,
2597 	        p_attribute_category => l_attribute_rec.attribute_category,
2598 		p_attribute1 => l_attribute_rec.attribute1,
2599 		p_attribute2 => l_attribute_rec.attribute2,
2600 		p_attribute3 => l_attribute_rec.attribute3,
2601 		p_attribute4 => l_attribute_rec.attribute4,
2602 		p_attribute5 => l_attribute_rec.attribute5,
2603 		p_attribute6 => l_attribute_rec.attribute6,
2604 		p_attribute7 => l_attribute_rec.attribute7,
2605 		p_attribute8 => l_attribute_rec.attribute8,
2606 		p_attribute9 => l_attribute_rec.attribute9,
2607 		p_attribute10 => l_attribute_rec.attribute10,
2608 		p_attribute11 => l_attribute_rec.attribute11,
2609 		p_attribute12 => l_attribute_rec.attribute12,
2610 		p_attribute13 => l_attribute_rec.attribute13,
2611 		p_attribute14 => l_attribute_rec.attribute14,
2612 		p_attribute15 => l_attribute_rec.attribute15,
2613 	        p_global_attribute_category => p_global_attribute_rec.global_attribute_category,
2614 		p_global_attribute1 => p_global_attribute_rec.global_attribute1,
2615 	        p_global_attribute2 => p_global_attribute_rec.global_attribute2,
2616 		p_global_attribute3 => p_global_attribute_rec.global_attribute3,
2617 	        p_global_attribute4 => p_global_attribute_rec.global_attribute4,
2618 		p_global_attribute5 => p_global_attribute_rec.global_attribute5,
2619 	        p_global_attribute6 => p_global_attribute_rec.global_attribute6,
2620 		p_global_attribute7 => p_global_attribute_rec.global_attribute7,
2621 	        p_global_attribute8 => p_global_attribute_rec.global_attribute8,
2622 		p_global_attribute9 => p_global_attribute_rec.global_attribute9,
2623 	        p_global_attribute10 => p_global_attribute_rec.global_attribute10,
2624 		p_global_attribute11 => p_global_attribute_rec.global_attribute11,
2625 	        p_global_attribute12 => p_global_attribute_rec.global_attribute12,
2626 		p_global_attribute13 => p_global_attribute_rec.global_attribute13,
2627 	        p_global_attribute14 => p_global_attribute_rec.global_attribute14,
2628 		p_global_attribute15 => p_global_attribute_rec.global_attribute15,
2629 	        p_global_attribute16 => p_global_attribute_rec.global_attribute16,
2630 	        p_global_attribute17 => p_global_attribute_rec.global_attribute17,
2631 		p_global_attribute18 => p_global_attribute_rec.global_attribute18,
2632 	        p_global_attribute19 => p_global_attribute_rec.global_attribute19,
2633 		p_global_attribute20 => p_global_attribute_rec.global_attribute20,
2634 	        p_comments => p_comments ,
2635 	        p_amount_applied_from => l_amount_applied_from , /* Bug 5438627 */
2636 	        p_trans_to_receipt_rate => l_trans_to_receipt_rate ,
2637 		x_ra_rec             =>llca_ra_rec,
2638 		x_return_status => lfc_return_status,
2639 		x_msg_count => lfc_msg_count,
2640 		x_msg_data  => lfc_msg_data
2641 		);
2642                --Bug fix for 5645383
2643                IF lfc_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2644                  x_return_status := FND_API.G_RET_STS_ERROR;
2645                END IF;
2646                FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
2647                                         p_count => x_msg_count,
2648                                         p_data  => x_msg_data
2649                                        );
2650                IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2651                  ROLLBACK TO Apply_In_Detail_PVT;
2652                  x_return_status := FND_API.G_RET_STS_ERROR ;
2653                  IF PG_DEBUG in ('Y', 'C') THEN
2654                     arp_util.debug('Apply_In_Detail: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
2655                  END IF;
2656                  Return;
2657                END IF;
2658 
2659     -- Assign receivable_application_id to package global variable
2660       -- So, it can used to perform follow on operation on given application
2661 
2662 		apply_out_rec.receivable_application_id := llca_ra_rec.receivable_application_id;
2663 
2664 		update ar_activity_details
2665 	         set source_table = 'RA',
2666 		     source_id = llca_ra_rec.receivable_application_id
2667 	       where source_id is null
2668 	         and nvl(current_activity_flag, 'Y') = 'Y' -- Bug 7241111
2669 	         and cash_receipt_id = l_cash_receipt_id
2670 	         and customer_trx_line_id in (select customer_trx_line_id
2671 						from ra_customer_trx_lines
2672 	                                    where customer_trx_id = l_customer_trx_id);
2673 
2674                 -- Clean the GT Table even though the table get refreshed in the commit stage.
2675                  delete from ar_llca_trx_lines_gt
2676                  where customer_trx_id = l_customer_trx_id;
2677 
2678    EXCEPTION
2679      WHEN OTHERS THEN
2680 
2681                /*-------------------------------------------------------+
2682                 |  Handle application errors that result from trapable  |
2683                 |  error conditions. The error messages have already    |
2684                 |  been put on the error stack.                         |
2685                 +-------------------------------------------------------*/
2686 
2687                 IF (SQLCODE = -20001)
2688                 THEN
2689                      ROLLBACK TO Apply_In_Detail_PVT;
2690 
2691                       --  Display_Parameters;
2692                       x_return_status := FND_API.G_RET_STS_ERROR ;
2693                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
2694                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.RECEIPT_APPLICATION '||SQLERRM);
2695                        FND_MSG_PUB.Add;
2696 
2697                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
2698                                                   p_count  =>  x_msg_count,
2699                                                   p_data   => x_msg_data
2700                                                 );
2701                       RETURN;
2702                 ELSE
2703                      ROLLBACK TO Apply_In_Detail_PVT;
2704                    RAISE;
2705                 END IF;
2706    END;
2707 
2708    /* Bug 3773036: raising error if return status is not success */
2709    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2710       RAISE FND_API.G_EXC_ERROR;
2711    END IF;
2712    IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2713       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2714    END IF;
2715 
2716       jg_ar_receivable_applications.apply(
2717                   p_apply_before_after        => 'AFTER',
2718                   p_global_attribute_category => l_global_attribute_rec.global_attribute_category,
2719                   p_set_of_books_id           => null,
2720                   p_cash_receipt_id           => null,
2721                   p_receipt_date              => null,
2722                   p_applied_payment_schedule_id => l_applied_payment_schedule_id,
2723                   p_amount_applied              => null,
2724                   p_unapplied_amount            => null,
2725                   p_due_date                    => null,
2726                   p_receipt_method_id           => null,
2727                   p_remittance_bank_account_id  => null,
2728                   p_global_attribute1           => l_global_attribute_rec.global_attribute1,
2729                   p_global_attribute2           => l_global_attribute_rec.global_attribute2,
2730                   p_global_attribute3           => l_global_attribute_rec.global_attribute3,
2731                   p_global_attribute4           => l_global_attribute_rec.global_attribute4,
2732                   p_global_attribute5           => l_global_attribute_rec.global_attribute5,
2733                   p_global_attribute6           => l_global_attribute_rec.global_attribute6,
2734                   p_global_attribute7           => l_global_attribute_rec.global_attribute7,
2735                   p_global_attribute8           => l_global_attribute_rec.global_attribute8,
2736                   p_global_attribute9           => l_global_attribute_rec.global_attribute9,
2737                   p_global_attribute10          => l_global_attribute_rec.global_attribute10,
2738                   p_global_attribute11          => l_global_attribute_rec.global_attribute11,
2739                   p_global_attribute12          => l_global_attribute_rec.global_attribute12,
2740                   p_global_attribute13          => l_global_attribute_rec.global_attribute13,
2741                   p_global_attribute14          => l_global_attribute_rec.global_attribute14,
2742                   p_global_attribute15          => l_global_attribute_rec.global_attribute15,
2743                   p_global_attribute16          => l_global_attribute_rec.global_attribute16,
2744                   p_global_attribute17          => l_global_attribute_rec.global_attribute17,
2745                   p_global_attribute18          => l_global_attribute_rec.global_attribute18,
2746                   p_global_attribute19          => l_global_attribute_rec.global_attribute19,
2747                   p_global_attribute20          => l_global_attribute_rec.global_attribute20,
2748                   p_return_status               => l_gdflex_return_status);
2749                --Bug fix for 5645383
2750                IF l_gdflex_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2751                  x_return_status := FND_API.G_RET_STS_ERROR;
2752                END IF;
2753                FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
2754                                         p_count => x_msg_count,
2755                                         p_data  => x_msg_data
2756                                        );
2757                IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2758                  ROLLBACK TO Apply_In_Detail_PVT;
2759                  x_return_status := FND_API.G_RET_STS_ERROR ;
2760                  IF PG_DEBUG in ('Y', 'C') THEN
2761                     arp_util.debug('Apply_In_Detail: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
2762                  END IF;
2763                  Return;
2764                END IF;
2765 
2766        /*--------------------------------+
2767         |   Standard check of p_commit   |
2768         +--------------------------------*/
2769 
2770         IF FND_API.To_Boolean( p_commit )
2771         THEN
2772             IF PG_DEBUG in ('Y', 'C') THEN
2773                arp_util.debug('Apply_In_Detail: ' || 'committing');
2774             END IF;
2775               Commit;
2776         END IF;
2777         IF PG_DEBUG in ('Y', 'C') THEN
2778            arp_util.debug('Apply_In_Detail ()- ');
2779         END IF;
2780 EXCEPTION
2781        WHEN FND_API.G_EXC_ERROR THEN
2782 
2783                 IF PG_DEBUG in ('Y', 'C') THEN
2784                    arp_util.debug('Apply_In_Detail: ' || SQLCODE, G_MSG_ERROR);
2785                    arp_util.debug('Apply_In_Detail: ' || SQLERRM, G_MSG_ERROR);
2786                 END IF;
2787 
2788                 ROLLBACK TO Apply_In_Detail_PVT;
2789                 x_return_status := FND_API.G_RET_STS_ERROR ;
2790 
2791                -- Display_Parameters;
2792 
2793                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
2794                                            p_count       =>      x_msg_count,
2795                                            p_data        =>      x_msg_data
2796                                          );
2797 
2798         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2799 
2800                 IF PG_DEBUG in ('Y', 'C') THEN
2801                    arp_util.debug('Apply_In_Detail: ' || SQLERRM, G_MSG_ERROR);
2802                 END IF;
2803                 ROLLBACK TO Apply_In_Detail_PVT;
2804                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2805 
2806                --  Display_Parameters;
2807 
2808                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
2809                                            p_count       =>      x_msg_count,
2810                                            p_data        =>      x_msg_data
2811                                          );
2812 
2813         WHEN OTHERS THEN
2814 
2815                /*-------------------------------------------------------+
2816                 |  Handle application errors that result from trapable  |
2817                 |  error conditions. The error messages have already    |
2818                 |  been put on the error stack.                         |
2819                 +-------------------------------------------------------*/
2820 
2821                 IF (SQLCODE = -20001)
2822                 THEN
2823 
2824                       ROLLBACK TO Apply_In_Detail_PVT;
2825 
2826                       --If only one error message on the stack,
2827                       --retrive it
2828 
2829                       x_return_status := FND_API.G_RET_STS_ERROR ;
2830                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
2831                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','APPLY : '||SQLERRM);
2832                       FND_MSG_PUB.Add;
2833 
2834                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
2835                                                  p_count  =>  x_msg_count,
2836                                                  p_data   => x_msg_data
2837                                                 );
2838 
2839                       RETURN;
2840 
2841                 ELSE
2842                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2843                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
2844                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','APPLY : '||SQLERRM);
2845                       FND_MSG_PUB.Add;
2846                 END IF;
2847 
2848                 IF PG_DEBUG in ('Y', 'C') THEN
2849                    arp_util.debug('Apply_In_Detail: ' || SQLCODE, G_MSG_ERROR);
2850                    arp_util.debug('Apply_In_Detail: ' || SQLERRM, G_MSG_ERROR);
2851                 END IF;
2852 
2853                 ROLLBACK TO Apply_In_Detail_PVT;
2854 
2855                 IF      FND_MSG_PUB.Check_Msg_Level
2856                 THEN
2857                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
2858                                         l_api_name
2859                                        );
2860                 END IF;
2861 
2862              --   Display_Parameters;
2863 
2864                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
2865                                            p_count       =>      x_msg_count,
2866                                            p_data        =>      x_msg_data
2867                                          );
2868 END Apply_In_Detail;
2869 
2870 PROCEDURE Unapply(
2871       -- Standard API parameters.
2872       p_api_version                  IN  NUMBER,
2873       p_init_msg_list                IN  VARCHAR2 := FND_API.G_FALSE,
2874       p_commit                       IN  VARCHAR2 := FND_API.G_FALSE,
2875       p_validation_level             IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
2876       x_return_status                OUT NOCOPY VARCHAR2,
2877       x_msg_count                    OUT NOCOPY NUMBER,
2878       x_msg_data                     OUT NOCOPY VARCHAR2,
2879    -- *** Receipt Info. parameters *****
2880       p_receipt_number               IN  ar_cash_receipts.receipt_number%TYPE DEFAULT NULL,
2881       p_cash_receipt_id              IN  ar_cash_receipts.cash_receipt_id%TYPE DEFAULT NULL,
2882       p_trx_number                   IN  ra_customer_trx.trx_number%TYPE DEFAULT NULL,
2883       p_customer_trx_id              IN  ra_customer_trx.customer_trx_id%TYPE DEFAULT NULL,
2884       p_installment                  IN  ar_payment_schedules.terms_sequence_number%TYPE DEFAULT NULL,
2885       p_applied_payment_schedule_id  IN  ar_payment_schedules.payment_schedule_id%TYPE DEFAULT NULL,
2886       p_receivable_application_id    IN  ar_receivable_applications.receivable_application_id%TYPE DEFAULT NULL,
2887       p_reversal_gl_date             IN  ar_receivable_applications.reversal_gl_date%TYPE DEFAULT NULL,
2888       p_called_from                  IN  VARCHAR2 DEFAULT NULL,
2889       p_cancel_claim_flag            IN  VARCHAR2 DEFAULT 'Y',
2890       p_org_id             IN NUMBER  DEFAULT NULL
2891       )  IS
2892 l_api_name       CONSTANT VARCHAR2(20) := 'Unapply';
2893 l_api_version    CONSTANT NUMBER       := 1.0;
2894 
2895 l_customer_trx_id              NUMBER;
2896 l_applied_payment_schedule_id  NUMBER;
2897 l_cash_receipt_id              NUMBER;
2898 l_receivable_application_id    NUMBER;
2899 l_reversal_gl_date             DATE;
2900 l_apply_gl_date                DATE;
2901 l_bal_due_remaining            NUMBER;
2902 l_receipt_gl_date              DATE;
2903 l_val_return_status            VARCHAR2(1);
2904 l_derive_ids_ret_status        VARCHAR2(1);
2905 l_glob_return_status           VARCHAR2(1);
2906 l_clm_return_status            VARCHAR2(1);
2907 l_application_ref_type         VARCHAR2(30);
2908 l_secondary_app_ref_id         NUMBER;
2909 l_amount_applied               NUMBER;
2910 l_cr_unapp_amount              NUMBER;/* Added for 3119391 */
2911 l_org_return_status VARCHAR2(1);
2912 l_org_id                           NUMBER;
2913 BEGIN
2914        /*------------------------------------+
2915         |   Standard start of API savepoint  |
2916         +------------------------------------*/
2917 
2918         SAVEPOINT Unapply_PVT;
2919 
2920        /*--------------------------------------------------+
2921         |   Standard call to check for call compatibility  |
2922         +--------------------------------------------------*/
2923 
2924         IF NOT FND_API.Compatible_API_Call(
2925                                             l_api_version,
2926                                             p_api_version,
2927                                             l_api_name,
2928                                             G_PKG_NAME
2929                                           )
2930         THEN
2931               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2932         END IF;
2933        /*-----------------------------------------+
2934         |   Initialize return status to SUCCESS   |
2935         +-----------------------------------------*/
2936 
2937         x_return_status := FND_API.G_RET_STS_SUCCESS;
2938 
2939        /*--------------------------------------------------------------+
2940         |   Initialize message list if p_init_msg_list is set to TRUE  |
2941         +--------------------------------------------------------------*/
2942 
2943         IF FND_API.to_Boolean( p_init_msg_list )
2944           THEN
2945               FND_MSG_PUB.initialize;
2946         END IF;
2947 
2948         IF PG_DEBUG in ('Y', 'C') THEN
2949            arp_util.debug('ar_receipt_api.Unapply()+ ');
2950         END IF;
2951 
2952          Original_unapp_info.trx_number := p_trx_number;
2953          Original_unapp_info.customer_trx_id := p_customer_trx_id;
2954          Original_unapp_info.applied_ps_id := p_applied_payment_schedule_id;
2955          Original_unapp_info.cash_receipt_id := p_cash_receipt_id;
2956          Original_unapp_info.receipt_number := p_receipt_number;
2957          Original_unapp_info.receivable_application_id := p_receivable_application_id;
2958 
2959 /* SSA change */
2960        l_org_id            := p_org_id;
2961        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
2962        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
2963                                                 p_return_status =>l_org_return_status);
2964  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2965        x_return_status := FND_API.G_RET_STS_ERROR;
2966  ELSE
2967 
2968         /*-------------------------------------------------+
2969          | Initialize the profile option package variables |
2970          +-------------------------------------------------*/
2971 
2972            initialize_profile_globals;
2973 
2974        /*---------------------------------------------+
2975         |   ========== Start of API Body ==========   |
2976         +---------------------------------------------*/
2977 
2978         --Assign IN parameter values to local variables
2979         --which are also used as assignment targets.
2980 
2981          l_customer_trx_id := p_customer_trx_id;
2982          l_applied_payment_schedule_id := p_applied_payment_schedule_id;
2983          l_cash_receipt_id   := p_cash_receipt_id;
2984          l_receivable_application_id  := p_receivable_application_id;
2985          l_reversal_gl_date := trunc(p_reversal_gl_date);
2986 
2987 
2988         --Derive the id's for the entered values and if both the
2989         --values and the id's superceed the values
2990 
2991          ar_receipt_lib_pvt.Derive_unapp_ids(
2992                                p_trx_number ,
2993                                l_customer_trx_id ,
2994                                p_installment ,
2995                                l_applied_payment_schedule_id ,
2996                                p_receipt_number ,
2997                                l_cash_receipt_id ,
2998                                l_receivable_application_id,
2999                                p_called_from,
3000                                l_apply_gl_date,
3001                                l_derive_ids_ret_status
3002                                   );
3003          /*Added parameter l_cr_unapp_amount for bug 3119391 */
3004          ar_receipt_lib_pvt.Default_unapp_info(
3005                                 l_receivable_application_id,
3006                                 l_apply_gl_date,
3007                                 l_cash_receipt_id,
3008                                 l_reversal_gl_date,
3009                                 l_receipt_gl_date,
3010 				l_cr_unapp_amount);
3011 
3012 
3013          /*Added parameter l_cr_unapp_amount for bug 3119391 */
3014          ar_receipt_val_pvt.validate_unapp_info(
3015            l_receipt_gl_date,
3016            l_receivable_application_id, /* Bug fix 3266712 */
3017            l_reversal_gl_date,
3018            l_apply_gl_date,
3019            l_cr_unapp_amount,
3020            l_val_return_status);
3021 
3022          -- Bug 2270809
3023          -- If a claim was created with this app, then check the claim status.
3024          -- If not OPEN,CANCELLED,COMPLETE then disallow unapply
3025          SELECT application_ref_type,
3026                 secondary_application_ref_id,
3027                 amount_applied
3028          INTO   l_application_ref_type,
3029                 l_secondary_app_ref_id,
3030                 l_amount_applied
3031          FROM   ar_receivable_applications
3032          WHERE  receivable_application_id = l_receivable_application_id;
3033          IF (l_application_ref_type = 'CLAIM' AND
3034 	     NVL(p_called_from,'RAPI') <> 'TRADE_MANAGEMENT')
3035          THEN
3036            ar_receipt_val_pvt.validate_claim_unapply(
3037                 p_secondary_app_ref_id      =>  l_secondary_app_ref_id,
3038                 p_invoice_ps_id             =>  l_applied_payment_schedule_id ,
3039                 p_customer_trx_id           =>  l_customer_trx_id,
3040                 p_cash_receipt_id           =>  l_cash_receipt_id,
3041                 p_receipt_number            =>  p_receipt_number,
3042                 p_amount_applied            =>  l_amount_applied,
3043                 p_cancel_claim_flag         =>  p_cancel_claim_flag,
3044                 p_return_status             =>  l_clm_return_status);
3045          END IF;
3046 
3047          IF PG_DEBUG in ('Y', 'C') THEN
3048             arp_util.debug('Apply: ' || 'validation return status :'||l_val_return_status);
3049          END IF;
3050 
3051          jg_ar_receivable_applications.Unapply(
3052                                       l_cash_receipt_id,
3053                                       l_applied_payment_schedule_id,
3054 				      l_glob_return_status );
3055 --
3056 END IF;
3057 
3058          IF l_derive_ids_ret_status <> FND_API.G_RET_STS_SUCCESS OR
3059             l_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
3060             l_clm_return_status <> FND_API.G_RET_STS_SUCCESS OR
3061             l_glob_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3062             x_return_status := FND_API.G_RET_STS_ERROR ;
3063          END IF;
3064 
3065         IF x_return_status <> FND_API.G_RET_STS_SUCCESS
3066          THEN
3067 
3068             ROLLBACK TO Unapply_PVT;
3069 
3070              x_return_status := FND_API.G_RET_STS_ERROR ;
3071 
3072              FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
3073                                         p_count => x_msg_count,
3074                                         p_data  => x_msg_data
3075                                        );
3076 
3077              IF PG_DEBUG in ('Y', 'C') THEN
3078                 arp_util.debug('Apply: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
3079              END IF;
3080              Return;
3081         END IF;
3082 
3083         IF PG_DEBUG in ('Y', 'C') THEN
3084            arp_util.debug('Apply: ' || '*******DUMP THE INPUT PARAMETERS ********');
3085            arp_util.debug('Apply: ' || 'l_receivable_application_id :'||to_char(l_receivable_application_id));
3086            arp_util.debug('Apply: ' || 'l_reversal_gl_date :'||to_char(l_reversal_gl_date,'DD-MON-YY'));
3087         END IF;
3088 
3089 	-- LLCA - Delete the activity record if llca exists. We need to modify the LLCA update
3090 	-- logic to preserve the previous record details on AR_ACTIVITY_DETAILS instead of
3091 	--- removing it.  Open bug exist for this issue.
3092 	/*
3093 	delete from ar_activity_details ad
3094 	where ad.cash_receipt_id  = l_cash_receipt_id
3095         and  ad.customer_trx_line_id in
3096 		(select customer_trx_line_id from ra_customer_trx_lines
3097 		 where customer_trx_id = l_customer_trx_id);
3098          */
3099 
3100 -- Bug 7241111 to retain the old application record under activity details
3101 
3102 INSERT INTO AR_ACTIVITY_DETAILS(
3103                                 CASH_RECEIPT_ID,
3104                                 CUSTOMER_TRX_LINE_ID,
3105                                 ALLOCATED_RECEIPT_AMOUNT,
3106                                 AMOUNT,
3107                                 TAX,
3108                                 FREIGHT,
3109                                 CHARGES,
3110                                 LAST_UPDATE_DATE,
3111                                 LAST_UPDATED_BY,
3112                                 LINE_DISCOUNT,
3113                                 TAX_DISCOUNT,
3114                                 FREIGHT_DISCOUNT,
3115                                 LINE_BALANCE,
3116                                 TAX_BALANCE,
3117                                 CREATION_DATE,
3118                                 CREATED_BY,
3119                                 LAST_UPDATE_LOGIN,
3120                                 COMMENTS,
3121                                 APPLY_TO,
3122                                 ATTRIBUTE1,
3123                                 ATTRIBUTE2,
3124                                 ATTRIBUTE3,
3125                                 ATTRIBUTE4,
3126                                 ATTRIBUTE5,
3127                                 ATTRIBUTE6,
3128                                 ATTRIBUTE7,
3129                                 ATTRIBUTE8,
3130                                 ATTRIBUTE9,
3131                                 ATTRIBUTE10,
3132                                 ATTRIBUTE11,
3133                                 ATTRIBUTE12,
3134                                 ATTRIBUTE13,
3135                                 ATTRIBUTE14,
3136                                 ATTRIBUTE15,
3137                                 ATTRIBUTE_CATEGORY,
3138                                 GROUP_ID,
3139                                 REFERENCE1,
3140                                 REFERENCE2,
3141                                 REFERENCE3,
3142                                 REFERENCE4,
3143                                 REFERENCE5,
3144                                 OBJECT_VERSION_NUMBER,
3145                                 CREATED_BY_MODULE,
3146                                 SOURCE_ID,
3147                                 SOURCE_TABLE,
3148                                 LINE_ID,
3149 			        CURRENT_ACTIVITY_FLAG)
3150                         SELECT
3151                                 LLD.CASH_RECEIPT_ID,
3152                                 LLD.CUSTOMER_TRX_LINE_ID,
3153                                 LLD.ALLOCATED_RECEIPT_AMOUNT*-1,
3154                                 LLD.AMOUNT*-1,
3155                                 LLD.TAX*-1,
3156                                 LLD.FREIGHT*-1,
3157                                 LLD.CHARGES*-1,
3158                                 LLD.LAST_UPDATE_DATE,
3159                                 LLD.LAST_UPDATED_BY,
3160                                 LLD.LINE_DISCOUNT,
3161                                 LLD.TAX_DISCOUNT,
3162                                 LLD.FREIGHT_DISCOUNT,
3163                                 LLD.LINE_BALANCE,
3164                                 LLD.TAX_BALANCE,
3165                                 LLD.CREATION_DATE,
3166                                 LLD.CREATED_BY,
3167                                 LLD.LAST_UPDATE_LOGIN,
3168                                 LLD.COMMENTS,
3169                                 LLD.APPLY_TO,
3170                                 LLD.ATTRIBUTE1,
3171                                 LLD.ATTRIBUTE2,
3172                                 LLD.ATTRIBUTE3,
3173                                 LLD.ATTRIBUTE4,
3174                                 LLD.ATTRIBUTE5,
3175                                 LLD.ATTRIBUTE6,
3176                                 LLD.ATTRIBUTE7,
3177                                 LLD.ATTRIBUTE8,
3178                                 LLD.ATTRIBUTE9,
3179                                 LLD.ATTRIBUTE10,
3180                                 LLD.ATTRIBUTE11,
3181                                 LLD.ATTRIBUTE12,
3182                                 LLD.ATTRIBUTE13,
3183                                 LLD.ATTRIBUTE14,
3184                                 LLD.ATTRIBUTE15,
3185                                 LLD.ATTRIBUTE_CATEGORY,
3186                                 LLD.GROUP_ID,
3187                                 LLD.REFERENCE1,
3188                                 LLD.REFERENCE2,
3189                                 LLD.REFERENCE3,
3190                                 LLD.REFERENCE4,
3191                                 LLD.REFERENCE5,
3192                                 LLD.OBJECT_VERSION_NUMBER,
3193                                 LLD.CREATED_BY_MODULE,
3194                                 LLD.SOURCE_ID,
3195                                 LLD.SOURCE_TABLE,
3196                                 ar_Activity_details_s.nextval,
3197                                 'R'
3198                         FROM ar_Activity_details LLD
3199 		        where LLD.cash_receipt_id = l_cash_receipt_id
3200 			and nvl(LLD.CURRENT_ACTIVITY_FLAG, 'Y') = 'Y'
3201 			and  LLD.customer_trx_line_id in
3202 			(select customer_trx_line_id
3203 			from ra_customer_trx_lines
3204 			where customer_trx_id = l_customer_trx_id);
3205 
3206 		   UPDATE ar_Activity_details dtl
3207 		     set CURRENT_ACTIVITY_FLAG = 'N'
3208 			where dtl.cash_receipt_id = l_cash_receipt_id
3209 			and nvl(dtl.CURRENT_ACTIVITY_FLAG, 'Y') = 'Y'
3210 			and  dtl.customer_trx_line_id in
3211 			(select customer_trx_line_id
3212 			from ra_customer_trx_lines
3213 			where customer_trx_id = l_customer_trx_id);
3214 
3215 
3216 
3217 
3218         --lock the receipt before calling the entity handler
3219        arp_cash_receipts_pkg.nowaitlock_p(p_cr_id => l_cash_receipt_id);
3220 
3221        BEGIN
3222         --call the entity handler.
3223           arp_process_application.reverse(
3224                                 l_receivable_application_id,
3225                                 l_reversal_gl_date,
3226                                 trunc(sysdate),
3227                                 'RAPI',
3228                                 p_api_version,
3229                                 l_bal_due_remaining,
3230                                 p_called_from  );
3231        EXCEPTION
3232          WHEN OTHERS THEN
3233 
3234                /*-------------------------------------------------------+
3235                 |  Handle application errors that result from trapable  |
3236                 |  error conditions. The error messages have already    |
3237                 |  been put on the error stack.                         |
3238                 +-------------------------------------------------------*/
3239 
3240                 IF (SQLCODE = -20001)
3241                 THEN
3242                      ROLLBACK TO Unapply_PVT;
3243 
3244                       --  Display_Parameters;
3245                       x_return_status := FND_API.G_RET_STS_ERROR ;
3246                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
3247                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.REVERSE : '||SQLERRM);
3248                        FND_MSG_PUB.Add;
3249 
3250                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
3251                                                   p_count  =>  x_msg_count,
3252                                                   p_data   => x_msg_data
3253                                                 );
3254                       RETURN;
3255                 ELSE
3256                    RAISE;
3257                 END IF;
3258 
3259        END;
3260        /*--------------------------------+
3261         |   Standard check of p_commit   |
3262         +--------------------------------*/
3263 
3264         IF FND_API.To_Boolean( p_commit )
3265         THEN
3266             IF PG_DEBUG in ('Y', 'C') THEN
3267                arp_util.debug('Apply: ' || 'committing');
3268             END IF;
3269               Commit;
3270         END IF;
3271 
3272         IF PG_DEBUG in ('Y', 'C') THEN
3273            arp_util.debug('ar_receipt_api.Unapply ()- ');
3274         END IF;
3275 
3276 
3277 EXCEPTION
3278        WHEN FND_API.G_EXC_ERROR THEN
3279 
3280                 IF PG_DEBUG in ('Y', 'C') THEN
3281                    arp_util.debug('Apply: ' || SQLCODE, G_MSG_ERROR);
3282                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
3283                 END IF;
3284 
3285                 ROLLBACK TO Unapply_PVT;
3286                 x_return_status := FND_API.G_RET_STS_ERROR ;
3287 
3288               --  Display_Parameters;
3289 
3290                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
3291                                            p_count       =>      x_msg_count,
3292                                            p_data        =>      x_msg_data
3293                                          );
3294 
3295         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3296 
3297                 IF PG_DEBUG in ('Y', 'C') THEN
3298                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
3299                 END IF;
3300                 ROLLBACK TO Unapply_PVT;
3301                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3302 
3303                --  Display_Parameters;
3304 
3305                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
3306                                            p_count       =>      x_msg_count,
3307                                            p_data        =>      x_msg_data
3308                                          );
3309 
3310         WHEN OTHERS THEN
3311 
3312                /*-------------------------------------------------------+
3313                 |  Handle application errors that result from trapable  |
3314                 |  error conditions. The error messages have already    |
3315                 |  been put on the error stack.                         |
3316                 +-------------------------------------------------------*/
3317 
3318                 IF (SQLCODE = -20001)
3319                 THEN
3320 
3321                       ROLLBACK TO Unapply_PVT;
3322 
3323                       x_return_status := FND_API.G_RET_STS_ERROR ;
3324                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
3325                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','UNAPPLY : '||SQLERRM);
3326                       FND_MSG_PUB.Add;
3327 
3328                       --If only one error message on the stack,
3329                       --retrive it
3330 
3331                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
3332                                                  p_count  =>  x_msg_count,
3333                                                  p_data   => x_msg_data
3334                                                 );
3335 
3336                       RETURN;
3337 
3338                 ELSE
3339                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3340                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
3341                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','UNAPPLY : '||SQLERRM);
3342                       FND_MSG_PUB.Add;
3343                 END IF;
3344 
3345                 IF PG_DEBUG in ('Y', 'C') THEN
3346                    arp_util.debug('Apply: ' || SQLCODE, G_MSG_ERROR);
3347                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
3348                 END IF;
3349 
3350                 ROLLBACK TO Unapply_PVT;
3351 
3352                 IF      FND_MSG_PUB.Check_Msg_Level
3353                 THEN
3354                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
3355                                         l_api_name
3356                                        );
3357                 END IF;
3358 
3359              --   Display_Parameters;
3360 
3361                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
3362                                            p_count       =>      x_msg_count,
3363                                            p_data        =>      x_msg_data
3364                                          );
3365 
3366 END Unapply;
3367 
3368 
3369 procedure process_payment(
3370 		p_cash_receipt_id     IN  NUMBER,
3371                 p_called_from         IN  VARCHAR2,
3372                 p_response_error_code OUT NOCOPY VARCHAR2,
3373                 x_msg_count           OUT NOCOPY NUMBER,
3374                 x_msg_data            OUT NOCOPY VARCHAR2,
3375 		x_return_status       OUT NOCOPY VARCHAR2
3376                 ) IS
3377 /*
3378   CURSOR rct_info_cur IS
3379      SELECT cr.receipt_number,
3380 	    cr.amount,
3381             cr.currency_code,
3382             rm.merchant_ref,
3383             rc.creation_status,
3384             ba.bank_branch_id,
3385             ba.bank_account_num,
3386 	    ba.bank_account_name,
3387             ba.inactive_date,
3388             cr.unique_reference   --bug 3672953
3389      FROM   ar_cash_receipts cr,
3390             ar_receipt_methods rm,
3391 	    ar_receipt_classes rc,
3392             ap_bank_accounts ba
3393      WHERE  cr.cash_receipt_id = p_cash_receipt_id
3394        AND  cr.customer_bank_account_id = ba.bank_account_id
3395        AND  cr.receipt_method_id = rm.receipt_method_id
3396        and  rm.receipt_class_id = rc.receipt_class_id;
3397 
3398 
3399   rct_info rct_info_cur%ROWTYPE;
3400 
3401   l_cr_rec ar_cash_receipts%ROWTYPE;
3402 
3403 
3404   l_payee_rec		IBY_Payment_Adapter_pub.Payee_Rec_type;
3405   l_customer_rec	IBY_Payment_Adapter_pub.Payer_Rec_type;
3406   l_tangible_rec	IBY_Payment_Adapter_pub.Tangible_Rec_type;
3407   l_pmtreqtrxn_rec	IBY_Payment_Adapter_pub.PmtReqTrxn_Rec_type;
3408   l_pmtinstr_rec        IBY_payment_adapter_pub.PmtInstr_Rec_type;
3409   l_cc_instr_rec        IBY_Payment_Adapter_pub.CreditCardInstr_Rec_Type;
3410   l_reqresp_rec         IBY_Payment_Adapter_pub.ReqResp_rec_type;
3411   l_riskinfo_rec        IBY_Payment_Adapter_pub.RiskInfo_rec_type;
3412 
3413   -- used for capture only:
3414   l_capturetrxn_rec     IBY_Payment_Adapter_pub.CaptureTrxn_rec_type;
3415   l_capresp_rec         IBY_Payment_Adapter_pub.CaptureResp_rec_type;
3416 
3417 
3418   l_payment_server_order_num VARCHAR2(80);
3419   l_action VARCHAR2(80);
3420 
3421   l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
3422   l_msg_count NUMBER;
3423   l_msg_data  VARCHAR2(2000);
3424 */
3425 
3426 BEGIN
3427 
3428 
3429           /* CALLED THE NEW PACKAGE HERE */
3430 
3431 
3432                         process_payment_1(
3433                                p_cash_receipt_id     => p_cash_receipt_id,
3434                                p_called_from         => p_called_from,
3435                                p_response_error_code => p_response_error_code,
3436                                x_msg_count           => x_msg_count,
3437                                x_msg_data            => x_msg_data,
3438                                x_return_status       => x_return_status);
3439 
3440 
3441 
3442 
3443 /*
3444   IF PG_DEBUG in ('Y', 'C') THEN
3445      arp_standard.debug('Apply: ' || 'Entering credit card processing...');
3446   END IF;
3447 
3448   OPEN rct_info_cur;
3449   FETCH rct_info_cur INTO rct_info;
3450 
3451   IF rct_info_cur%FOUND THEN
3452 
3453     -- first check if this is actually a credit card payment,
3454     -- indicated by bank_branch_id being 1 for the customer bank
3455     -- account
3456 
3457     IF rct_info.bank_branch_id = arp_global.CC_BANK_BRANCH_ID THEN
3458 
3459         IF PG_DEBUG in ('Y', 'C') THEN
3460            arp_standard.debug('Apply: ' || 'credit card bank branch is '  ||
3461              arp_global.CC_BANK_BRANCH_ID || ' --> CC acct');
3462         END IF;
3463 
3464         -- determine whether to AUTHORIZE only or to
3465         -- CAPTURE and AUTHORIZE in one step.  This is
3466         -- dependent on the receipt creation status, i.e.,
3467         -- if the receipt is created as remitted or cleared, the
3468         -- funds need to be authorized and captured.  If the
3469         -- receipt is confirmed, the remittance process will
3470         -- handle the capture and at this time we'll only
3471         -- authorize the charges to the credit card.
3472 
3473         if rct_info.creation_status IN ('REMITTED', 'CLEARED') THEN
3474           l_action := 'AUTHANDCAPTURE';
3475         elsif rct_info.creation_status = 'CONFIRMED' THEN
3476           l_action := 'AUTHONLY';
3477         else
3478           IF PG_DEBUG in ('Y', 'C') THEN
3479              arp_standard.debug('Apply: ' || 'ERROR: Creation status is ' || rct_info.creation_status);
3480           END IF;
3481 
3482           FND_MESSAGE.set_name('AR', 'AR_PAY_PROCESS_INVALID_STATUS');
3483           FND_MSG_PUB.Add;
3484 
3485           x_return_status := FND_API.G_RET_STS_ERROR;  -- should never happen
3486           RETURN;
3487         end if;
3488 
3489         -- Step 1: (always performed):
3490         -- authorize credit card charge
3491 
3492 	-- set up payee record:
3493 
3494 	l_payee_rec.payee_id := rct_info.merchant_ref;
3495 
3496 	-- set up payer (=customer) record:
3497 
3498 	l_customer_rec.payer_name := rct_info.bank_account_name;
3499 
3500         -- set up cc instrument record:
3501 
3502         l_cc_instr_rec.cc_num     := rct_info.bank_account_num;
3503         l_cc_instr_rec.cc_ExpDate := rct_info.inactive_date;
3504         l_cc_instr_rec.cc_HolderName := rct_info.bank_account_name;
3505 
3506         -- set the credit card as the payment instrument
3507 
3508         l_pmtinstr_rec.creditcardinstr:= l_cc_instr_rec;
3509 
3510         -- set up 'tangible' record:
3511 
3512         select 'ARI_'||ar_payment_server_ord_num_s.nextval
3513         into l_payment_server_order_num
3514         from dual;
3515 
3516         l_tangible_rec.tangible_id     := l_payment_server_order_num;
3517         l_tangible_rec.tangible_amount := rct_info.amount;
3518         l_tangible_rec.currency_code   := rct_info.currency_code;
3519         l_tangible_rec.refinfo         := rct_info.receipt_number;
3520 
3521 
3522         l_pmtreqtrxn_rec.pmtmode   := 'ONLINE';
3523         l_pmtreqtrxn_rec.auth_type := 'AUTHONLY';
3524 
3525         --Bug 3672953 Also pass unique reference to iPayment APIs
3526         ---Check the value of unique reference and if null then
3527         ---generate the value
3528         l_pmtreqtrxn_rec.TrxnRef := nvl(rct_info.unique_reference,SYS_GUID() );
3529 
3530 
3531         -- call to iPayment API OraPmtReq to authorize funds
3532 
3533         IF PG_DEBUG in ('Y', 'C') THEN
3534            arp_standard.debug('Apply: ' || 'Calling OraPmtReq');
3535            arp_standard.debug('Apply: ' || 'l_pmtreqtrxn_rec.pmtmode: ' || l_pmtreqtrxn_rec.pmtmode);
3536            arp_standard.debug('Apply: ' || 'l_pmtreqtrxn_rec.auth_type: ' || l_pmtreqtrxn_rec.auth_type);
3537 
3538            arp_standard.debug(  'l_pmtreqtrxn_rec.TrxnRef: ' || l_pmtreqtrxn_rec.TrxnRef);
3539 
3540            arp_standard.debug('Apply: ' || 'l_tangible_rec.tangible_id: ' ||  l_payment_server_order_num);
3541            arp_standard.debug('Apply: ' || 'l_tangible_rec.tangible_amount: ' || to_char(l_tangible_rec.tangible_amount) );
3542            arp_standard.debug('Apply: ' || 'l_tangible_rec.currency_code: ' ||l_tangible_rec.currency_code );
3543            arp_standard.debug('Apply: ' || 'l_tangible_rec.refinfo: ' || l_tangible_rec.refinfo);
3544            arp_standard.debug('Apply: ' || 'l_cc_instr_rec.cc_num: ' ||l_cc_instr_rec.cc_num );
3545            arp_standard.debug('Apply: ' || 'l_cc_instr_rec.cc_ExpDate: ' || to_char(l_cc_instr_rec.cc_ExpDate));
3546            arp_standard.debug('Apply: ' || 'l_cc_instr_rec.cc_HolderName: ' || l_cc_instr_rec.cc_HolderName );
3547            arp_standard.debug('Apply: ' || 'l_payee_rec.payee_id: ' ||l_payee_rec.payee_id );
3548            arp_standard.debug('Apply: ' || 'l_customer_rec.payer_name: ' || l_customer_rec.payer_name);
3549         END IF;
3550 
3551         IBY_Payment_Adapter_pub.OraPmtReq(
3552            p_api_version 	=> 1.0,
3553            p_init_msg_list 	=> FND_API.G_TRUE,
3554            p_commit       	=> FND_API.G_FALSE,
3555 	   p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3556 	   p_ecapp_id           => 222,  -- AR product id
3557            x_return_status      => l_return_status,
3558            x_msg_count          => l_msg_count,
3559            x_msg_data           => l_msg_data,
3560            p_payee_rec          => l_payee_rec,
3561            p_payer_rec          => l_customer_rec,
3562            p_pmtinstr_rec       => l_pmtinstr_rec,
3563            p_tangible_rec       => l_tangible_rec,
3564            p_pmtreqtrxn_rec     => l_pmtreqtrxn_rec,
3565 	   p_riskinfo_rec       => l_riskinfo_rec,
3566            x_reqresp_rec        => l_reqresp_rec);
3567 
3568 	 IF PG_DEBUG in ('Y', 'C') THEN
3569 	    arp_standard.debug('Apply: ' || 'l_return_status: ' || l_return_status);
3570 	 END IF;
3571 
3572          x_msg_count           := l_msg_count;
3573          x_msg_data            := l_msg_data;
3574          p_response_error_code := l_reqresp_rec.response.errcode;
3575 
3576          IF PG_DEBUG in ('Y', 'C') THEN
3577             arp_standard.debug('Apply: ' || '-------------------------------------');
3578             arp_standard.debug('Apply: ' || 'l_reqresp_rec.response.errcode: ' || l_reqresp_rec.response.errcode);
3579             arp_standard.debug('Apply: ' || 'l_reqresp_rec.response.errmessage: ' || l_reqresp_rec.response.errmessage);
3580             arp_standard.debug('Apply: ' || 'l_reqresp_rec.errorlocation: ' || l_reqresp_rec.errorlocation);
3581             arp_standard.debug('Apply: ' || 'l_reqresp_rec.beperrcode: ' || l_reqresp_rec.beperrcode);
3582             arp_standard.debug('Apply: ' || 'l_reqresp_rec.beperrmessage: ' || l_reqresp_rec.beperrmessage);
3583             arp_standard.debug('Apply: ' || 'NVL(l_reqresp_rec.response.status,0): ' || to_char(NVL(l_reqresp_rec.response.status,0)));
3584             arp_standard.debug('Apply: ' || 'Authcode: ' || l_reqresp_rec.authcode);
3585             arp_standard.debug('Apply: ' || 'Trxn ID: ' || l_reqresp_rec.Trxn_ID);
3586             arp_standard.debug('Apply: ' || '-------------------------------------');
3587          END IF;
3588 
3589         -- check if call was successful
3590         --Add message to message stack only it it is called from iReceivables
3591         --if not pass the message stack received from iPayment
3592 
3593         IF (NVL(p_called_from,'NONE') = 'IREC') THEN
3594            IF PG_DEBUG in ('Y', 'C') THEN
3595               arp_standard.debug('Apply: ' || 'l_MSG_COUNT=>'||to_char(l_MSG_COUNT));
3596            END IF;
3597            fnd_msg_pub.dump_list;
3598            IF PG_DEBUG in ('Y', 'C') THEN
3599               arp_standard.debug('Apply: ' || 'Errors: ');
3600            END IF;
3601            IF(l_MSG_COUNT=1) THEN
3602               IF PG_DEBUG in ('Y', 'C') THEN
3603                  arp_standard.debug('Apply: ' || l_MSG_DATA);
3604               END IF;
3605            ELSIF(l_MSG_COUNT>1)THEN
3606               LOOP
3607                   l_MSG_DATA:=FND_MSG_PUB.GET(p_encoded=>FND_API.G_FALSE);
3608                   IF (l_MSG_DATA IS NULL)THEN
3609                      EXIT;
3610                   END IF;
3611                   IF PG_DEBUG in ('Y', 'C') THEN
3612                      arp_standard.debug('Apply: ' || l_MSG_DATA);
3613                   END IF;
3614               END LOOP;
3615            END IF;
3616         END IF;
3617 
3618         if (l_return_status <> FND_API.G_RET_STS_SUCCESS)
3619            AND (NVL(p_called_from,'NONE') = 'IREC')  then
3620 
3621           FND_MESSAGE.set_name('AR', 'AR_PAY_PROCESS_AUTHFAILURE');
3622           FND_MSG_PUB.Add;
3623           x_return_status := l_return_status;
3624           RETURN;
3625         elsif (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
3626            --bug 3398538
3627                arp_util.debug('Create_cash_124: ');
3628           FND_MESSAGE.set_name('AR', 'AR_CC_AUTH_FAILED');
3629           FND_MSG_PUB.Add;
3630           x_return_status := l_return_status;
3631           RETURN;
3632         end if;
3633 
3634 
3635         -- update cash receipt with authorization code and
3636         -- payment server order id (tangible id)
3637 
3638         ARP_CASH_RECEIPTS_PKG.set_to_dummy(l_cr_rec);
3639         l_cr_rec.approval_code := l_reqresp_rec.authcode;
3640         l_cr_rec.payment_server_order_num := l_tangible_rec.tangible_id;
3641         ARP_CASH_RECEIPTS_PKG.update_p(l_cr_rec, p_cash_receipt_id);
3642 
3643           IF PG_DEBUG in ('Y', 'C') THEN
3644              arp_standard.debug('Apply: ' || 'CR rec updated with payment server auth code');
3645           END IF;
3646 
3647         -- see if capture is also required
3648 
3649         if (l_action = 'AUTHANDCAPTURE') then
3650 
3651           IF PG_DEBUG in ('Y', 'C') THEN
3652              arp_standard.debug('Apply: ' || 'starting capture...');
3653           END IF;
3654 
3655           -- Step 2: (optional): capture funds
3656 
3657           l_capturetrxn_rec.Trxn_ID := l_reqresp_rec.trxn_id;
3658           l_capturetrxn_rec.PmtMode := 'ONLINE';
3659           l_capturetrxn_rec.currency := rct_info.currency_code;
3660           l_capturetrxn_rec.price := rct_info.amount;
3661 
3662           --Bug 3672953 Also pass unique reference to iPayment APIs
3663           ---Check the value of unique reference and if null then
3664           ---generate the value
3665           l_capturetrxn_rec.TrxnRef:= nvl(rct_info.unique_reference,SYS_GUID());
3666 
3667 
3668 
3669           IBY_Payment_Adapter_pub.OraPmtCapture(
3670                p_api_version 	    => 1.0,
3671                p_init_msg_list 	    => FND_API.G_FALSE,
3672                p_commit       	    => FND_API.G_FALSE,
3673    	       p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3674 	       p_ecapp_id           => 222,  -- AR product id
3675                x_return_status      => l_return_status,
3676                x_msg_count          => l_msg_count,
3677                x_msg_data           => l_msg_data,
3678                p_capturetrxn_rec    => l_capturetrxn_rec,
3679 	       x_capresp_rec        => l_capresp_rec);
3680 
3681 	    IF PG_DEBUG in ('Y', 'C') THEN
3682 	       arp_standard.debug('Apply: ' || 'l_return_status: ' || l_return_status);
3683 	    END IF;
3684 
3685             x_msg_count           := l_msg_count;
3686             x_msg_data            := l_msg_data;
3687             p_response_error_code := l_capresp_rec.response.errcode;
3688 
3689             IF (NVL(p_called_from,'NONE') = 'IREC') THEN
3690                 IF PG_DEBUG in ('Y', 'C') THEN
3691                    arp_standard.debug('Apply: ' || 'l_MSG_COUNT=>'||to_char(l_MSG_COUNT));
3692                 END IF;
3693                 fnd_msg_pub.dump_list;
3694                 IF PG_DEBUG in ('Y', 'C') THEN
3695                    arp_standard.debug('Apply: ' || 'Errors: ');
3696                 END IF;
3697                 IF(l_MSG_COUNT=1) THEN
3698                    IF PG_DEBUG in ('Y', 'C') THEN
3699                       arp_standard.debug('Apply: ' || l_MSG_DATA);
3700                    END IF;
3701                 ELSIF(l_MSG_COUNT>1)THEN
3702                   LOOP
3703                      l_MSG_DATA:=FND_MSG_PUB.GET(p_encoded=>FND_API.G_FALSE);
3704                      IF (l_MSG_DATA IS NULL)THEN
3705                         EXIT;
3706                      END IF;
3707                      IF PG_DEBUG in ('Y', 'C') THEN
3708                         arp_standard.debug('Apply: ' || l_MSG_DATA);
3709                      END IF;
3710                   END LOOP;
3711                 END IF;
3712             END IF;
3713 
3714             IF PG_DEBUG in ('Y', 'C') THEN
3715                arp_standard.debug('Apply: ' || '-------------------------------------');
3716                arp_standard.debug('Apply: ' || 'l_capresp_rec.response.errcode: ' || l_capresp_rec.response.errcode);
3717                arp_standard.debug('Apply: ' || 'l_capresp_rec.response.errmessage: ' || l_capresp_rec.response.errmessage);
3718                arp_standard.debug('Apply: ' || 'l_capresp_rec.errorlocation: ' || l_capresp_rec.errorlocation);
3719                arp_standard.debug('Apply: ' || 'l_capresp_rec.beperrcode: ' || l_capresp_rec.beperrcode);
3720                arp_standard.debug('Apply: ' || 'l_capresp_rec.beperrmessage: ' || l_capresp_rec.beperrmessage);
3721                arp_standard.debug('Apply: ' || 'NVL(l_capresp_rec.response.status,0): ' || to_char(NVL(l_capresp_rec.response.status,0)));
3722                arp_standard.debug('Apply: ' || 'PmtInstr_Type: ' || l_capresp_rec.PmtInstr_Type);
3723                arp_standard.debug('Apply: ' || 'Trxn ID: ' || l_capresp_rec.Trxn_ID);
3724                arp_standard.debug('Apply: ' || '-------------------------------------');
3725             END IF;
3726 
3727            --Add message to message stack only it it is called from iReceivables
3728            --if not pass the message stack received from iPayment
3729 
3730            if (l_return_status <> FND_API.G_RET_STS_SUCCESS) AND (NVL(p_called_from,'NONE') = 'IREC')  then
3731               FND_MESSAGE.set_name('AR', 'AR_PAY_PROCESS_CAPTFAILURE');
3732               FND_MSG_PUB.Add;
3733             --bug 3398538
3734            elsif (l_return_status <>  FND_API.G_RET_STS_SUCCESS ) THEN
3735              FND_MESSAGE.set_name('AR', 'AR_CC_CAPTURE_FAILED');
3736              FND_MSG_PUB.Add;
3737            end if;
3738            x_return_status := l_return_status;
3739 
3740         END IF;  -- if capture required...
3741 
3742       ELSE
3743 
3744         IF PG_DEBUG in ('Y', 'C') THEN
3745            arp_standard.debug('Apply: ' || 'credit card bank branch is not 1 --> no CC acct');
3746         END IF;
3747 
3748         -- currently no processing required
3749 
3750       END IF;
3751 
3752     END IF;
3753 */
3754 
3755 
3756 END process_payment;
3757 
3758 
3759 
3760 -- bichatte payment uptake project
3761 
3762 PROCEDURE process_payment_1(
3763                 p_cash_receipt_id     IN  NUMBER,
3764                 p_called_from         IN  VARCHAR2,
3765                 p_response_error_code OUT NOCOPY VARCHAR2,
3766                 x_msg_count           OUT NOCOPY NUMBER,
3767                 x_msg_data            OUT NOCOPY VARCHAR2,
3768                 x_return_status       OUT NOCOPY VARCHAR2,
3769                 p_payment_trxn_extension_id IN NUMBER DEFAULT NULL
3770                 ) IS
3771 
3772 
3773   CURSOR rct_info_cur IS
3774      SELECT cr.receipt_number,
3775             cr.amount,
3776             cr.currency_code,
3777             rm.PAYMENT_CHANNEL_CODE,       /* NEW ADDED */
3778             rc.creation_status,            /* AR USE */
3779             cr.org_id,
3780             party.party_id,
3781             cr.pay_from_customer,
3782             cr.customer_site_use_id,
3783             cr.payment_trxn_extension_id,
3784             cr.receipt_date
3785      FROM   ar_cash_receipts_all cr,
3786             ar_receipt_methods rm,
3787             ar_receipt_classes rc,
3788             hz_cust_accounts hca,
3789             hz_parties    party
3790      WHERE  cr.cash_receipt_id = p_cash_receipt_id
3791      AND    hca.party_id = party.party_id
3792      AND    hca.cust_account_id = cr.pay_from_customer
3793      AND  cr.receipt_method_id = rm.receipt_method_id
3794      and  rm.receipt_class_id = rc.receipt_class_id;
3795 
3796 
3797 
3798             rct_info    rct_info_cur%ROWTYPE;
3799             l_cr_rec    ar_cash_receipts_all%ROWTYPE;
3800             l_org_type  HR_ALL_ORGANIZATION_UNITS.TYPE%TYPE;
3801             l_action VARCHAR2(80);
3802             l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
3803             l_msg_count NUMBER;
3804             l_msg_data  VARCHAR2(2000);
3805             l_iby_msg_data VARCHAR2(2000);
3806             l_vend_msg_data VARCHAR2(2000);
3807             l_cpy_msg_data VARCHAR2(2000);
3808 
3809 /* DECLARE the variables required for the payment engine (CPY ) all the REC TYPES */
3810            p_trxn_entity_id    NUMBER;
3811            lc_trxn_entity_id   IBY_FNDCPT_COMMON_PUB.Id_tbl_type;
3812 
3813            l_auth_flag         VARCHAR2(1);
3814            l_auth_id           NUMBER;
3815 
3816 /* END DECLARE the variables required for the payment engine (CPY ) all the REC TYPES */
3817 /* DECLARE the variables required for the payment engine (CPY AND AUTH) all the REC TYPES */
3818 
3819             l_payer_rec             IBY_FNDCPT_COMMON_PUB.PayerContext_rec_type;
3820             l_payee_rec             IBY_FNDCPT_TRXN_PUB.PayeeContext_rec_type;
3821             l_trxn_entity_id        NUMBER;
3822             l_auth_attribs_rec      IBY_FNDCPT_TRXN_PUB.AuthAttribs_rec_type;
3823             l_trxn_attribs_rec      IBY_FNDCPT_TRXN_PUB.TrxnExtension_rec_type;
3824             l_amount_rec            IBY_FNDCPT_TRXN_PUB.Amount_rec_type;
3825             l_authresult_rec       IBY_FNDCPT_TRXN_PUB.AuthResult_rec_type; /* OUT AUTH RESULT STRUCTURE */
3826             l_response_rec          IBY_FNDCPT_COMMON_PUB.Result_rec_type;   /* OUT RESPONSE STRUCTURE */
3827             l_entity_id             NUMBER;  -- OUT FROM COPY
3828 /* END DECLARE the variables required for the payment engine (AUTH) all the REC TYPES */
3829 
3830 
3831 /* DECLARE the variables required for the payment engine (SETTLEMENT) all the REC TYPES */
3832             ls_response_rec          IBY_FNDCPT_COMMON_PUB.Result_rec_type;   /* OUT RESPONSE STRUCTURE */
3833             ls_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
3834             ls_msg_count NUMBER;
3835             ls_msg_data  VARCHAR2(2000);
3836             ls_iby_msg_data VARCHAR2(2000);
3837 
3838 /* END DECLARE the variables required for the payment engine (SETTLEMENT) all the REC TYPES */
3839 
3840  /* 7666285 - for passing settlement_date  on returns */
3841            lcr_receipt_attr      IBY_FNDCPT_TRXN_PUB.receiptattribs_rec_type;
3842 
3843 BEGIN
3844 
3845 
3846   IF PG_DEBUG in ('Y', 'C') THEN
3847      arp_standard.debug(  'Entering payment processing...');
3848   END IF;
3849 
3850 
3851   OPEN rct_info_cur;
3852   FETCH rct_info_cur INTO rct_info;
3853 
3854 
3855   IF rct_info_cur%FOUND THEN
3856 
3857 
3858         if rct_info.creation_status IN ('REMITTED', 'CLEARED') THEN
3859           l_action := 'AUTHANDCAPTURE';
3860         elsif rct_info.creation_status = 'CONFIRMED' THEN
3861           l_action := 'AUTHONLY';
3862         else
3863           IF PG_DEBUG in ('Y', 'C') THEN
3864              arp_standard.debug('Apply: ' || 'ERROR: Creation status is ' || rct_info.creation_status);
3865           END IF;
3866 
3867           FND_MESSAGE.set_name('AR', 'AR_PAY_PROCESS_INVALID_STATUS');
3868           FND_MSG_PUB.Add;
3869 
3870           x_return_status := FND_API.G_RET_STS_ERROR;  -- should never happen
3871           RETURN;
3872         end if;
3873 
3874   -- Step 1: (always performed):
3875 
3876           -- set up payee record:
3877           l_payee_rec.org_id   := rct_info.org_id;                            -- receipt's org_id
3878           l_payee_rec.org_type := 'OPERATING_UNIT' ;                                -- ( HR_ORGANIZATION_UNITS )
3879 
3880 
3881         -- set up payer (=customer) record:
3882 
3883         l_payer_rec.Payment_Function := 'CUSTOMER_PAYMENT';
3884         l_payer_rec.Party_Id :=   rct_info.party_id;     -- receipt customer party id mandatory
3885         l_payer_rec.org_id   := rct_info.org_id ;
3886         l_payer_rec.org_type := 'OPERATING_UNIT';
3887         l_payer_rec.Cust_Account_Id :=rct_info.pay_from_customer;  -- receipt customer account_id
3888         l_payer_rec.Account_Site_Id :=rct_info.customer_site_use_id; -- receipt customer site_id
3889 
3890 
3891         if rct_info.customer_site_use_id is NULL  THEN
3892 
3893           l_payer_rec.org_id := NULL;
3894           l_payer_rec.org_type := NULL;
3895 
3896         end if;
3897 
3898         -- set up auth_attribs record:
3899           l_auth_attribs_rec.RiskEval_Enable_Flag := 'N';
3900 
3901         -- set up trxn_attribs record:
3902         l_trxn_attribs_rec.Originating_Application_Id := arp_standard.application_id;
3903         l_trxn_attribs_rec.order_id :=  rct_info.receipt_number;
3904         l_trxn_attribs_rec.Trxn_Ref_Number1 := 'RECEIPT';
3905         l_trxn_attribs_rec.Trxn_Ref_Number2 := p_cash_receipt_id;
3906 
3907         -- set up amounts
3908 
3909         l_amount_rec.value := rct_info.amount;
3910         l_amount_rec.currency_code   := rct_info.currency_code;
3911 
3912         /* 7666285 - settlement_date and settlement_due_date */
3913         lcr_receipt_attr.settlement_date := rct_info.receipt_date;
3914 
3915 
3916         IF PG_DEBUG in ('Y', 'C') THEN
3917            arp_standard.debug(  'check and then call Auth');
3918         END IF;
3919 
3920         -- determine whether to AUTHORIZE
3921 
3922         -- assign the value for payment_trxn_extension record
3923 
3924                  l_trxn_entity_id := rct_info.payment_trxn_extension_id;
3925 
3926         IF PG_DEBUG in ('Y', 'C') THEN
3927            arp_standard.debug(  'Calling get auth for  pmt_trxn_extn_id ');
3928            arp_standard.debug(  'l_trxn_entity_id  '             || to_char(l_trxn_entity_id ) );
3929 
3930        END IF;
3931 
3932              Begin
3933                 SELECT decode(summ.status,   NULL,   'N',   'Y') AUTHORIZED_FLAG
3934                    into l_auth_flag
3935                  FROM iby_trxn_summaries_all summ,
3936                       iby_fndcpt_tx_operations op
3937                 WHERE summ.transactionid = op.transactionid
3938                       AND reqtype = 'ORAPMTREQ'
3939                       AND status IN(0,    100)
3940                       AND trxntypeid IN(2,   3, 20)
3941                       AND op.trxn_extension_id = p_payment_trxn_extension_id
3942                       AND summ.trxnmid =
3943                            (SELECT MAX(trxnmid)
3944                                 FROM iby_trxn_summaries_all
3945                             WHERE transactionid = summ.transactionid
3946                             AND reqtype = 'ORAPMTREQ'
3947                             AND status IN(0, 100)
3948                             AND trxntypeid IN(2,    3,   20));
3949              Exception
3950                when others then
3951                  l_auth_flag := 'N';
3952              End;
3953 
3954               arp_standard.debug ( 'the value of auth_flag is = ' || l_auth_flag);
3955 
3956                 If l_auth_flag = 'Y' then
3957 
3958                   select AUTHORIZATION_ID
3959                    into l_auth_id
3960                    from IBY_TRXN_EXT_AUTHS_V
3961                    where TRXN_EXTENSION_ID = p_payment_trxn_extension_id;
3962 
3963                   arp_standard.debug ( 'the value of auth_id is = ' || l_auth_id);
3964 
3965                   ARP_CASH_RECEIPTS_PKG.set_to_dummy(l_cr_rec);
3966 
3967                   l_cr_rec.approval_code := 'AR'||to_char(l_auth_Id);
3968 
3969                   ARP_CASH_RECEIPTS_PKG.update_p(l_cr_rec, p_cash_receipt_id);
3970 
3971                  arp_standard.debug('CR rec updated with auth_id and auth code ');
3972                 end if;
3973 
3974 
3975 
3976 
3977            IF  l_auth_flag <> 'Y'  then
3978                  arp_standard.debug('auth needs to called');
3979 
3980                IF PG_DEBUG in ('Y', 'C') THEN
3981                   arp_standard.debug(  'Calling get auth for  pmt_trxn_extn_id ');
3982                   arp_standard.debug(  ' l_payee_rec.org_id '           || to_char(l_payee_rec.org_id) );
3983                   arp_standard.debug(  ' l_payee_rec.org_type '         || to_char( l_payee_rec.org_type) );
3984                   arp_standard.debug(  ' l_payer_rec.Payment_Function ' || to_char( l_payer_rec.Payment_Function) );
3985                   arp_standard.debug(  ' l_payer_rec.Party_Id '         || to_char( l_payer_rec.Party_Id) );
3986                   arp_standard.debug(  ' l_payer_rec.org_id '           || to_char(l_payer_rec.org_id) );
3987                   arp_standard.debug(  ' l_payer_rec.org_type  '        || to_char( l_payer_rec.org_type) );
3988                   arp_standard.debug(  'l_payer_rec.Cust_Account_Id '   || to_char(l_payer_rec.Cust_Account_Id) );
3989                   arp_standard.debug(  'l_payer_rec.Account_Site_Id '   || to_char(l_payer_rec.Account_Site_Id) );
3990                   arp_standard.debug(  'l_trxn_entity_id  '             || to_char(l_trxn_entity_id ) );
3991                   arp_standard.debug(  'l_amount_rec.value: ' || to_char(l_amount_rec.value) );
3992                   arp_standard.debug(  'l_amount_rec.currency_code: '   || l_amount_rec.currency_code );
3993 
3994                   arp_standard.debug(  'Calling get_auth for  pmt_trxn_extn_id ');
3995                END IF;
3996 
3997 
3998                  IBY_FNDCPT_TRXN_PUB.Create_Authorization(
3999                          p_api_version        => 1.0,
4000                          p_init_msg_list      => FND_API.G_TRUE,
4001                          x_return_status      => l_return_status,
4002                          x_msg_count          => l_msg_count,
4003                          x_msg_data           => l_msg_data,
4004                          p_payer              => l_payer_rec,
4005                          p_payer_equivalency  => IBY_FNDCPT_COMMON_PUB.G_PAYER_EQUIV_UPWARD,
4006                          p_payee              => l_payee_rec,
4007                          p_trxn_entity_id     => l_trxn_entity_id,
4008                          p_auth_attribs       => l_auth_attribs_rec,
4009                          p_amount             => l_amount_rec,
4010                          x_auth_result        => l_authresult_rec, -- out auth result struct
4011                          x_response           => l_response_rec );   -- out response struct
4012 
4013 
4014                   x_msg_count           := l_msg_count;
4015                   x_msg_data            := l_msg_data;
4016 
4017                         arp_standard.debug('x_return_status  :<' || l_return_status || '>');
4018                         arp_standard.debug('x_msg_count      :<' || l_msg_count || '>');
4019 
4020                   FOR i IN 1..l_msg_count LOOP
4021                       arp_standard.debug('x_msg #' || TO_CHAR(i) || ' = <' ||
4022                       SUBSTR(fnd_msg_pub.get(p_msg_index => i,p_encoded => FND_API.G_FALSE),1,150) || '>');
4023                   END LOOP;
4024 
4025                      IF PG_DEBUG in ('Y', 'C') THEN
4026                         arp_standard.debug(  '-------------------------------------');
4027                         arp_standard.debug(  'l_response_rec.Result_Code:     ' || l_response_rec.Result_Code);
4028                         arp_standard.debug(  'l_response_rec.Result_Category: ' || l_response_rec.Result_Category);
4029                         arp_standard.debug(  'l_response_rec.Result_message : ' || l_response_rec.Result_message );
4030                         arp_standard.debug(  'l_authresult_rec.Auth_Id:     '       || l_authresult_rec.Auth_Id);
4031                         arp_standard.debug(  'l_authresult_rec.Auth_Date: '         || l_authresult_rec.Auth_Date);
4032                         arp_standard.debug(  'l_authresult_rec.Auth_Code:     '     || l_authresult_rec.Auth_Code);
4033                         arp_standard.debug(  'l_authresult_rec.AVS_Code: '          || l_authresult_rec.AVS_Code);
4034                         arp_standard.debug(  'l_authresult_rec.Instr_SecCode_Check:'|| l_authresult_rec.Instr_SecCode_Check);
4035                         arp_standard.debug(  'l_authresult_rec.PaymentSys_Code: '   || l_authresult_rec.PaymentSys_Code);
4036                         arp_standard.debug(  'l_authresult_rec.PaymentSys_Msg: '    || l_authresult_rec.PaymentSys_Msg);
4037                      -- arp_standard.debug(  'l_authresult_rec.Risk_Result: '       || l_authresult_rec.Risk_Result);
4038 
4039                     END IF;
4040 
4041              IF (l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4042               -- update cash receipt with authorization code and
4043 
4044                 ARP_CASH_RECEIPTS_PKG.set_to_dummy(l_cr_rec);
4045                  l_cr_rec.approval_code := l_authresult_rec.Auth_code ||'AR'||to_char(l_authresult_rec.Auth_Id);
4046 
4047                ARP_CASH_RECEIPTS_PKG.update_p(l_cr_rec, p_cash_receipt_id);
4048 
4049                IF PG_DEBUG in ('Y', 'C') THEN
4050                  arp_standard.debug('CR rec updated with auth_id and auth code ');
4051                END IF;
4052 
4053              END IF;
4054 
4055                     -- check if call was successful
4056                     --Add message to message stack only it it is called from iReceivables
4057                     --if not pass the message stack received from iPayment
4058 
4059                     IF (NVL(p_called_from,'NONE') = 'IREC') THEN
4060                           IF PG_DEBUG in ('Y', 'C') THEN
4061                           arp_standard.debug(  'l_MSG_COUNT=>'||to_char(l_MSG_COUNT));
4062                     END IF;
4063                     fnd_msg_pub.dump_list;
4064                     IF PG_DEBUG in ('Y', 'C') THEN
4065                     arp_standard.debug(  'Errors: ');
4066                     END IF;
4067                       IF(l_MSG_COUNT=1) THEN
4068                          IF PG_DEBUG in ('Y', 'C') THEN
4069                           arp_standard.debug(  l_MSG_DATA);
4070                          END IF;
4071                       ELSIF(l_MSG_COUNT>1)THEN
4072                            LOOP
4073                            l_MSG_DATA:=FND_MSG_PUB.GET(p_encoded=>FND_API.G_FALSE);
4074                                IF (l_MSG_DATA IS NULL)THEN
4075                                EXIT;
4076                                END IF;
4077                               IF PG_DEBUG in ('Y', 'C') THEN
4078                               arp_standard.debug(  l_MSG_DATA);
4079                               END IF;
4080                            END LOOP;
4081                       END IF;
4082                     END IF;
4083 
4084                  IF (l_return_status <> FND_API.G_RET_STS_SUCCESS)
4085                  AND (NVL(p_called_from,'NONE') = 'IREC')  then
4086 
4087                   FND_MESSAGE.set_name('AR', 'AR_PAY_PROCESS_AUTHFAILURE');
4088                   FND_MSG_PUB.Add;
4089                   x_return_status := l_return_status;
4090                   RETURN;
4091 
4092                 ELSIF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4093 
4094                  arp_standard.debug('create_cash_126');
4095                   FND_MESSAGE.set_name('AR', 'AR_CC_AUTH_FAILED');
4096                   FND_MSG_PUB.Add;
4097 
4098                      IF  l_response_rec.Result_Code is NOT NULL THEN
4099 
4100                        ---Raise the PAYMENT error code concatenated with the message
4101 
4102                         l_iby_msg_data := substrb( l_response_rec.Result_Code || ': '||
4103                                    l_response_rec.Result_Message , 1, 240);
4104 
4105                         arp_standard.debug(  'l_iby_msg_data: ' || l_iby_msg_data);
4106                         FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
4107                         FND_MESSAGE.SET_TOKEN('GENERIC_TEXT',l_iby_msg_data);
4108 
4109                         FND_MSG_PUB.Add;
4110 
4111                      END IF;
4112 
4113                      IF l_authresult_rec.PaymentSys_Code is not null THEN
4114 
4115                        ---Raise the VENDOR error code concatenated with the message
4116 
4117                         l_vend_msg_data := substrb(l_authresult_rec.PaymentSys_Code || ': '||
4118                                    l_authresult_rec.PaymentSys_Msg , 1, 240 );
4119 
4120                         FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
4121                         FND_MESSAGE.SET_TOKEN('GENERIC_TEXT',l_vend_msg_data);
4122 
4123                       FND_MSG_PUB.Add;
4124 
4125                     END IF;
4126 
4127 
4128                     FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
4129                                p_count  =>  x_msg_count,
4130                                p_data   => x_msg_data );
4131 
4132                     x_return_status := l_return_status;
4133                     RETURN;
4134 
4135                   END IF; /* End the error handling CREATE */
4136 
4137           END IF;  /* END if of auth flag N  */
4138 
4139 
4140     IF l_action = 'AUTHANDCAPTURE' THEN
4141 
4142      arp_standard.debug ( 'CALL THE SETTLEMENT API');
4143                    IF PG_DEBUG in ('Y', 'C') THEN
4144                        arp_standard.debug(  'Calling settlement for  pmt_trxn_extn_id ');
4145                        arp_standard.debug(  ' l_payer_rec.Payment_Function ' || to_char( l_payer_rec.Payment_Function) );
4146                        arp_standard.debug(  ' l_payer_rec.Party_Id '         || to_char( l_payer_rec.Party_Id) );
4147                        arp_standard.debug(  ' l_payer_rec.org_id '           || to_char(l_payer_rec.org_id) );
4148                        arp_standard.debug(  ' l_payer_rec.org_type  '        || to_char( l_payer_rec.org_type) );
4149                        arp_standard.debug(  ' l_payer_rec.Cust_Account_Id '  || to_char(l_payer_rec.Cust_Account_Id) );
4150                        arp_standard.debug(  ' l_payer_rec.Account_Site_Id '  || to_char(l_payer_rec.Account_Site_Id) );
4151                        arp_standard.debug(  ' l_trxn_entity_id  '            || to_char(l_trxn_entity_id ) );
4152                        arp_standard.debug(  ' l_amount_rec.value '          || to_char(l_amount_rec.value) );
4153                        arp_standard.debug(  ' l_amount_rec.currency_code '  || l_amount_rec.currency_code );
4154                        arp_standard.debug(  ' lcr_receipt_attr.settlement_date '  || lcr_receipt_attr.settlement_date );
4155                      END IF;
4156 
4157                        IBY_FNDCPT_TRXN_PUB.Create_Settlement (
4158                              p_api_version        => 1.0,
4159                              p_init_msg_list      => FND_API.G_TRUE,
4160                              x_return_status      => ls_return_status,
4161                              x_msg_count          => ls_msg_count,
4162                              x_msg_data           => ls_msg_data,
4163                              p_payer              => l_payer_rec,
4164                              p_payer_equivalency  => IBY_FNDCPT_COMMON_PUB.G_PAYER_EQUIV_UPWARD,
4165                              p_trxn_entity_id     => l_trxn_entity_id,
4166                              p_amount             => l_amount_rec,
4167                              p_receipt_attribs    => lcr_receipt_attr,
4168                              x_response           => ls_response_rec );   -- out response struct
4169 
4170 
4171 
4172                         arp_standard.debug('x_return_status  :<' || ls_return_status || '>');
4173                         arp_standard.debug('x_msg_count      :<' || ls_msg_count || '>');
4174 
4175                   FOR i IN 1..ls_msg_count LOOP
4176                       arp_standard.debug('x_msg #' || TO_CHAR(i) || ' = <' ||
4177                       SUBSTR(fnd_msg_pub.get(p_msg_index => i,p_encoded => FND_API.G_FALSE),1,150) || '>');
4178                   END LOOP;
4179 
4180                    IF PG_DEBUG in ('Y', 'C') THEN
4181                       arp_standard.debug(  '-------------------------------------');
4182                       arp_standard.debug(  'ls_response_rec.Result_Code:     ' || ls_response_rec.Result_Code);
4183                       arp_standard.debug(  'ls_response_rec.Result_Category: ' || ls_response_rec.Result_Category);
4184                       arp_standard.debug(  'ls_response_rec.Result_message : ' || ls_response_rec.Result_message );
4185 
4186                     END IF;
4187 
4188 
4189                    IF (NVL(p_called_from,'NONE') = 'IREC') THEN
4190                           IF PG_DEBUG in ('Y', 'C') THEN
4191                           arp_standard.debug(  'ls_MSG_COUNT=>'||to_char(ls_MSG_COUNT));
4192                           END IF;
4193                        fnd_msg_pub.dump_list;
4194                     IF PG_DEBUG in ('Y', 'C') THEN
4195                     arp_standard.debug(  'Errors: ');
4196                     END IF;
4197                       IF(ls_MSG_COUNT=1) THEN
4198                          IF PG_DEBUG in ('Y', 'C') THEN
4199                           arp_standard.debug(  ls_MSG_DATA);
4200                          END IF;
4201                       ELSIF(ls_MSG_COUNT>1)THEN
4202                            LOOP
4203                            ls_MSG_DATA:=FND_MSG_PUB.GET(p_encoded=>FND_API.G_FALSE);
4204                                IF (ls_MSG_DATA IS NULL)THEN
4205                                EXIT;
4206                                END IF;
4207                               IF PG_DEBUG in ('Y', 'C') THEN
4208                               arp_standard.debug(  ls_MSG_DATA);
4209                               END IF;
4210                            END LOOP;
4211                       END IF;
4212                     END IF;
4213 
4214               IF (ls_return_status <> FND_API.G_RET_STS_SUCCESS)
4215                     AND (NVL(p_called_from,'NONE') = 'IREC')  then
4216 
4217                       FND_MESSAGE.set_name('AR', 'AR_PAY_PROCESS_AUTHFAILURE');
4218                       FND_MSG_PUB.Add;
4219                       x_return_status := ls_return_status;
4220                     RETURN;
4221 
4222               ELSIF (ls_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4223 
4224 
4225                   FND_MESSAGE.set_name('AR', 'AR_CC_CAPTURE_FAILED');
4226                   FND_MSG_PUB.Add;
4227 
4228                      IF  ls_response_rec.Result_Code is NOT NULL THEN
4229 
4230                        ---Raise the PAYMENT error code concatenated with the message
4231 
4232                         ls_iby_msg_data := substrb( ls_response_rec.Result_Code || ': '||
4233                                    ls_response_rec.Result_Message , 1, 240);
4234 
4235                         arp_standard.debug(  'ls_iby_msg_data: ' || ls_iby_msg_data);
4236                         FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
4237                         FND_MESSAGE.SET_TOKEN('GENERIC_TEXT',ls_iby_msg_data);
4238 
4239                         FND_MSG_PUB.Add;
4240 
4241                      END IF;
4242 
4243 
4244                     FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
4245                                p_count  =>  x_msg_count,
4246                                p_data   => x_msg_data );
4247 
4248                     x_return_status := ls_return_status;
4249                     RETURN;
4250 
4251                 END IF; /* End the error handling */
4252 
4253 
4254     END IF;
4255 
4256 
4257   END IF ; /* rct_info_cur%FOUND */
4258 
4259 EXCEPTION
4260  WHEN others THEN
4261   IF PG_DEBUG in ('Y', 'C') THEN
4262      arp_util.debug('Exception : process_payment_1() ');
4263   END IF;
4264 
4265 END process_payment_1;
4266 
4267 -- bichatte payment uptake project end
4268 
4269 -- bichatte payment uptake strt
4270 
4271 PROCEDURE  Copy_payment_extension(
4272               p_payment_trxn_extension_id         IN NUMBER,
4273               p_customer_id                       IN NUMBER,
4274               p_receipt_method_id                 IN NUMBER,
4275               p_org_id                            IN NUMBER,
4276               p_customer_site_use_id              IN NUMBER,
4277               p_receipt_number                    IN VARCHAR2,
4278               x_msg_count           OUT NOCOPY NUMBER,
4279               x_msg_data            OUT NOCOPY VARCHAR2,
4280               x_return_status       OUT NOCOPY VARCHAR2,
4281               o_payment_trxn_extension_id   OUT NOCOPY NUMBER
4282                  ) IS
4283 
4284             l_payer_rec             IBY_FNDCPT_COMMON_PUB.PayerContext_rec_type;
4285             l_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
4286             l_msg_count                     NUMBER;
4287             l_msg_data                      VARCHAR2(2000);
4288             l_cpy_msg_data                  VARCHAR2(2000);
4289             l_pmt_channel_code              VARCHAR2(20);
4290             l_assignment_id                 NUMBER;
4291             l_trxn_attribs_rec              IBY_FNDCPT_TRXN_PUB.TrxnExtension_rec_type;
4292             l_payment_trxn_extension_id     AR_CASH_RECEIPTS.PAYMENT_TRXN_EXTENSION_ID%TYPE;
4293             p_trxn_entity_id                AR_CASH_RECEIPTS.PAYMENT_TRXN_EXTENSION_ID%TYPE;
4294             l_response_rec                  IBY_FNDCPT_COMMON_PUB.Result_rec_type;
4295             l_party_id                      NUMBER;
4296             lc_trxn_entity_id               IBY_FNDCPT_COMMON_PUB.Id_tbl_type;
4297             l_customer_id                   ar_cash_receipts.pay_from_customer%TYPE;
4298             l_receipt_method_id             ar_cash_receipts.receipt_method_id%TYPE;
4299             l_org_id                        ar_cash_receipts.org_id%TYPE;
4300             l_customer_site_use_id          ar_cash_receipts.customer_site_use_id%TYPE;
4301             l_receipt_number                ar_cash_receipts.receipt_number%TYPE;
4302 
4303 
4304 
4305 BEGIN
4306 
4307 
4308 arp_standard.debug ( 'inside Copy payment trxn ');
4309 
4310      l_customer_id := p_customer_id;
4311      l_receipt_method_id := p_receipt_method_id;
4312      l_org_id := p_org_id;
4313      l_customer_site_use_id := p_customer_site_use_id;
4314      l_receipt_number := p_receipt_number;
4315      l_payment_trxn_extension_id := p_payment_trxn_extension_id;
4316 
4317 
4318      SELECT party.party_id
4319      INTO   l_party_id
4320      FROM   hz_cust_accounts hca,
4321             hz_parties    party
4322      WHERE  hca.party_id = party.party_id
4323      AND    hca.cust_account_id = l_customer_id ;
4324 
4325      SELECT payment_channel_code
4326      INTO   l_pmt_channel_code
4327      from ar_receipt_methods
4328      where receipt_method_id = l_receipt_method_id;
4329 
4330      SELECT INSTR_ASSIGNMENT_ID
4331      INTO  l_assignment_id
4332      from  iby_fndcpt_tx_extensions
4333      where trxn_extension_id = l_payment_trxn_extension_id;
4334 
4335         l_payer_rec.Payment_Function := 'CUSTOMER_PAYMENT';
4336         l_payer_rec.Party_Id :=  l_party_id;                  -- receipt customer party id mandatory
4337         l_payer_rec.org_id   := l_org_id;
4338         l_payer_rec.org_type := 'OPERATING_UNIT';
4339         l_payer_rec.Cust_Account_Id :=l_customer_id ;         -- receipt customer account_id
4340         l_payer_rec.Account_Site_Id :=l_customer_site_use_id; -- receipt customer site_id
4341 
4342         if l_customer_site_use_id is NULL  THEN
4343 
4344           l_payer_rec.org_id := NULL;
4345           l_payer_rec.org_type := NULL;
4346 
4347         end if;
4348 
4349         l_trxn_attribs_rec.Originating_Application_Id := arp_standard.application_id;
4350         l_trxn_attribs_rec.order_id :=  l_receipt_number;
4351         l_trxn_attribs_rec.Trxn_Ref_Number1 := 'RECEIPT';
4352         l_trxn_attribs_rec.Trxn_Ref_Number2 := l_receipt_number;
4353         l_assignment_id := l_assignment_id;
4354         lc_trxn_entity_id(1):= l_payment_trxn_extension_id;
4355 
4356 
4357    arp_standard.debug('l_payer.payment_function :<' || l_payer_rec.payment_function  || '>');
4358    arp_standard.debug('l_payer.Party_Id         :<' || l_payer_rec.Party_Id || '>');
4359    arp_standard.debug('l_payer.Org_Type         :<' || l_payer_rec.Org_Type || '>');
4360    arp_standard.debug('l_payer.Org_id           :<' || l_payer_rec.Org_id || '>');
4361    arp_standard.debug('l_payer.Cust_Account_Id  :<' || l_payer_rec.Cust_Account_Id || '>');
4362    arp_standard.debug('l_trxn_attribs.Originating_Application_Id :<'
4363                                   || l_trxn_attribs_rec.Originating_Application_Id || '>');
4364    arp_standard.debug('l_trxn_attribs.order_id  :<'|| l_trxn_attribs_rec.order_id || '>');
4365    arp_standard.debug('l_assignment_id          :<'|| l_assignment_id || '>');
4366    arp_standard.debug('payment_trx_extension_id          :<'|| l_payment_trxn_extension_id || '>');
4367 
4368                   IBY_FNDCPT_TRXN_PUB.Copy_Transaction_Extension
4369                      ( p_api_version        => 1.0,
4370                        p_init_msg_list      => FND_API.G_TRUE,
4371                        p_commit             => FND_API.G_FALSE,
4372                        x_return_status      => l_return_status,
4373                        x_msg_count          => l_msg_count,
4374                        x_msg_data           => l_msg_data,
4375                        p_payer              => l_payer_rec,
4376                        p_payer_equivalency  => IBY_FNDCPT_COMMON_PUB.G_PAYER_EQUIV_UPWARD,
4377                        p_entities           => lc_trxn_entity_id,
4378                        p_trxn_attribs       => l_trxn_attribs_rec,
4379                        x_entity_id          => p_trxn_entity_id,          -- out parm
4380                        x_response           => l_response_rec             -- out
4381                       );
4382 
4383                  IF l_return_status = FND_API.G_RET_STS_SUCCESS  THEN
4384                          o_payment_trxn_extension_id  := p_trxn_entity_id ;
4385 
4386                            arp_standard.debug('the copied value of trx_entn is ' || o_payment_trxn_extension_id );
4387                  END IF;
4388 
4389 
4390                arp_standard.debug('x_return_status  :<' || l_return_status || '>');
4391                arp_standard.debug('x_msg_count      :<' || l_msg_count || '>');
4392 
4393     FOR i IN 1..l_msg_count LOOP
4394               arp_standard.debug('x_msg #' || TO_CHAR(i) || ' = <' ||
4395               SUBSTR(fnd_msg_pub.get(p_msg_index => i,p_encoded => FND_API.G_FALSE),1,150) || '>');
4396     END LOOP;
4397 
4398 
4399    IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4400 
4401                  arp_standard.debug('create_cash_127');
4402        FND_MESSAGE.set_name('AR', 'AR_CC_AUTH_FAILED');
4403        FND_MSG_PUB.Add;
4404 
4405         IF  l_response_rec.result_Code is NOT NULL THEN
4406 
4407          ---Raise the PAYMENT error code concatenated with the message
4408 
4409           l_cpy_msg_data := substrb( l_response_rec.Result_Code || ': '||
4410                                    l_response_rec.Result_Message , 1, 240);
4411 
4412           arp_standard.debug(  'l_cpy_msg_data: ' || l_cpy_msg_data);
4413           FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
4414           FND_MESSAGE.SET_TOKEN('GENERIC_TEXT',l_cpy_msg_data);
4415 
4416           FND_MSG_PUB.Add;
4417 
4418         END IF;
4419 
4420       FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
4421                                  p_count  =>  x_msg_count,
4422                                  p_data   => x_msg_data );
4423 
4424                   x_return_status := l_return_status;
4425                   RETURN;
4426 
4427    END IF;
4428 
4429 
4430 END Copy_payment_extension;
4431 
4432 -- bichatte payment uptake
4433 PROCEDURE  Create_payment_extension(
4434               p_payment_trxn_extension_id         IN NUMBER,
4435               p_customer_id                       IN NUMBER,
4436               p_receipt_method_id                 IN NUMBER,
4437               p_org_id                            IN NUMBER,
4438               p_customer_site_use_id              IN NUMBER,
4439               p_receipt_number                    IN VARCHAR2,
4440               p_cash_receipt_id                   IN NUMBER,
4441               x_msg_count           OUT NOCOPY NUMBER,
4442               x_msg_data            OUT NOCOPY VARCHAR2,
4443               x_return_status       OUT NOCOPY VARCHAR2,
4444               o_payment_trxn_extension_id   OUT NOCOPY NUMBER
4445                  ) IS
4446 
4447             l_return_status                 VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
4448             l_msg_count                     NUMBER;
4449             l_msg_data                      VARCHAR2(2000);
4450 
4451             l_payer_rec                    IBY_FNDCPT_COMMON_PUB.PayerContext_rec_type;
4452             l_assignment_id             NUMBER;
4453             l_trxn_attribs_rec            IBY_FNDCPT_TRXN_PUB.TrxnExtension_rec_type;
4454             l_extension_id                NUMBER;
4455             l_init_extension_id          NUMBER;
4456             l_result_rec                    IBY_FNDCPT_COMMON_PUB.Result_rec_type;
4457 
4458             l_customer_id                ra_customer_trx.paying_customer_id%type;
4459             l_customer_site_use_id  ra_customer_trx.paying_site_use_id%type;
4460             l_org_id                         ra_customer_trx.org_id%type;
4461             l_party_id                      hz_parties.party_id%type;
4462             l_trx_number                 ra_customer_trx.trx_number%type;
4463             l_payment_channel_code       ar_receipt_methods.payment_channel_code%type;
4464             l_customer_trx_id          ra_customer_trx.customer_trx_id%type;
4465 
4466 
4467     BEGIN
4468 
4469         l_init_extension_id := p_payment_trxn_extension_id;
4470         l_customer_id := p_customer_id;
4471 
4472     IF l_init_extension_id is NOT NULL THEN
4473 
4474                   SELECT INSTR_ASSIGNMENT_ID
4475                    into   l_assignment_id
4476                    FROM   iby_fndcpt_tx_extensions
4477                    where  trxn_extension_id = l_init_extension_id;
4478 
4479 
4480                   SELECT party.party_id
4481                   INTO      l_party_id
4482                   FROM   hz_cust_accounts hca,
4483                                hz_parties    party
4484                   WHERE  hca.party_id = party.party_id
4485                    AND    hca.cust_account_id = l_customer_id ;
4486 
4487                         SELECT payment_channel_code
4488      		INTO   l_payment_channel_code
4489     		 from ar_receipt_methods
4490     		 where receipt_method_id = p_receipt_method_id;
4491 
4492 
4493      /* pouplate values into the variables */
4494 
4495         l_payer_rec.Payment_Function := 'CUSTOMER_PAYMENT';
4496         l_payer_rec.Party_Id :=  l_party_id;                  -- receipt customer party id mandatory
4497         l_payer_rec.org_id   := p_org_id;
4498         l_payer_rec.org_type := 'OPERATING_UNIT';
4499         l_payer_rec.Cust_Account_Id := p_customer_id ;         -- receipt customer account_id
4500         l_payer_rec.Account_Site_Id :=p_customer_site_use_id; -- receipt customer site_id
4501 
4502         if p_customer_site_use_id is NULL  THEN
4503 
4504           l_payer_rec.org_id := NULL;
4505           l_payer_rec.org_type := NULL;
4506 
4507         end if;
4508         l_trxn_attribs_rec.Originating_Application_Id := arp_standard.application_id;
4509         l_trxn_attribs_rec.order_id := p_receipt_number ;
4510         l_trxn_attribs_rec.Trxn_Ref_Number1 := 'RECEIPT';
4511         l_trxn_attribs_rec.Trxn_Ref_Number2 :=  p_cash_receipt_id;
4512         l_assignment_id := l_assignment_id;
4513 
4514 
4515        /* reset the value of l_extension_id */
4516 
4517 
4518 
4519  IBY_FNDCPT_TRXN_PUB.Create_Transaction_Extension
4520    (
4521    p_api_version        =>1.0,
4522    p_init_msg_list      =>FND_API.G_TRUE,
4523    p_commit             =>FND_API.G_FALSE,
4524    x_return_status      =>l_return_status,
4525    x_msg_count          =>l_msg_count,
4526    x_msg_data           =>l_msg_data,
4527    p_payer              =>l_payer_rec,
4528    p_payer_equivalency  =>IBY_FNDCPT_COMMON_PUB.G_PAYER_EQUIV_UPWARD,
4529    p_pmt_channel         =>l_payment_channel_code,
4530    p_instr_assignment   =>l_assignment_id,
4531    p_trxn_attribs           =>l_trxn_attribs_rec,
4532    x_entity_id               =>l_extension_id,
4533    x_response              =>l_result_rec
4534    );
4535 
4536 
4537       IF l_return_status = FND_API.G_RET_STS_SUCCESS  THEN
4538 
4539                        o_payment_trxn_extension_id := l_extension_id;
4540 
4541                 arp_standard.debug(to_char(SQL%ROWCOUNT) || 'PMT_EXTN_ID  row(s) updated.');
4542 
4543       END IF;
4544 
4545     FOR i IN 1..l_msg_count LOOP
4546               arp_standard.debug('x_msg #' || TO_CHAR(i) || ' = <' ||
4547               SUBSTR(fnd_msg_pub.get(p_msg_index => i,p_encoded => FND_API.G_FALSE),1,150) || '>');
4548     END LOOP;
4549 
4550 
4551            IF l_return_status <> FND_API.G_RET_STS_SUCCESS  THEN
4552                     arp_standard.debug('FAILED: ' ||l_result_rec.result_code);
4553                     arp_standard.debug('PARM-l_payer_rec.Payment_Function ' ||l_payer_rec.Payment_Function);
4554                     arp_standard.debug('PARM-l_payer_rec.party_id ' ||l_payer_rec.Party_Id);
4555                     arp_standard.debug('PARM-l_payer_rec.org_id ' ||l_payer_rec.org_id);
4556                     arp_standard.debug('PARM-l_payer_rec.org_type ' ||l_payer_rec.org_type);
4557                     arp_standard.debug('PARM-l_payer_rec.customer_id ' ||l_payer_rec.Cust_Account_Id);
4558                     arp_standard.debug('PARM-l_payer_rec.customer_site_id ' ||l_payer_rec.Account_Site_Id);
4559                     arp_standard.debug('PARM-l_trxn_attribs_rec.Originating_Application_Id ' ||l_trxn_attribs_rec.Originating_Application_Id);
4560                     arp_standard.debug('PARM- l_trxn_attribs_rec.order_id ' ||l_trxn_attribs_rec.order_id);
4561                     arp_standard.debug('PARM-l_trxn_attribs_rec.Trxn_Ref_Number1 ' ||l_trxn_attribs_rec.Trxn_Ref_Number1);
4562                     arp_standard.debug('PARM-l_assignment_id ' ||l_assignment_id);
4563 
4564 
4565                   FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
4566                                  p_count  =>  x_msg_count,
4567                                  p_data   => x_msg_data );
4568 
4569                   x_return_status := l_return_status;
4570                   RETURN;
4571 
4572 
4573            END IF;
4574 
4575 
4576    END IF;  /* payment_trxn_extension_id is not null */
4577 
4578      EXCEPTION
4579         WHEN OTHERS THEN
4580             arp_standard.debug('ERROR IN CREATION ');
4581            RAISE;
4582       END Create_payment_extension;
4583 
4584 
4585 -- bichatte payment uptake end
4586 
4587 PROCEDURE Create_and_apply(
4588 -- Standard API parameters.
4589       p_api_version      IN  NUMBER,
4590       p_init_msg_list    IN  VARCHAR2 := FND_API.G_FALSE,
4591       p_commit           IN  VARCHAR2 := FND_API.G_FALSE,
4592       p_validation_level IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
4593       x_return_status    OUT NOCOPY VARCHAR2,
4594       x_msg_count        OUT NOCOPY NUMBER,
4595       x_msg_data         OUT NOCOPY VARCHAR2,
4596  -- Receipt info. parameters
4597       p_usr_currency_code       IN  VARCHAR2 DEFAULT NULL, --the translated currency code
4598       p_currency_code      IN  ar_cash_receipts.currency_code%TYPE DEFAULT NULL,
4599       p_usr_exchange_rate_type  IN  VARCHAR2 DEFAULT NULL,
4600       p_exchange_rate_type IN  ar_cash_receipts.exchange_rate_type%TYPE DEFAULT NULL,
4601       p_exchange_rate      IN  ar_cash_receipts.exchange_rate%TYPE DEFAULT NULL,
4602       p_exchange_rate_date IN  ar_cash_receipts.exchange_date%TYPE DEFAULT NULL,
4603       p_amount                           IN  ar_cash_receipts.amount%TYPE DEFAULT NULL,
4604       p_factor_discount_amount           IN ar_cash_receipts.factor_discount_amount%TYPE DEFAULT NULL,
4605       p_receipt_number                   IN  ar_cash_receipts.receipt_number%TYPE DEFAULT NULL,
4606       p_receipt_date                     IN  ar_cash_receipts.receipt_date%TYPE DEFAULT NULL,
4607       p_gl_date                          IN  ar_cash_receipt_history.gl_date%TYPE DEFAULT NULL,
4608       p_maturity_date                    IN  DATE DEFAULT NULL,
4609       p_postmark_date                    IN  DATE DEFAULT NULL,
4610       p_customer_id                      IN  ar_cash_receipts.pay_from_customer%TYPE DEFAULT NULL,
4611       /* tca uptake */
4612       p_customer_name                    IN  hz_parties.party_name%TYPE DEFAULT NULL,
4613       p_customer_number                  IN  hz_cust_accounts.account_number%TYPE DEFAULT NULL,
4614       p_customer_bank_account_id         IN  ar_cash_receipts.customer_bank_account_id%TYPE DEFAULT NULL,
4615       /* 6612301 */
4616       p_customer_bank_account_num        IN  iby_ext_bank_accounts_v.bank_account_number%TYPE DEFAULT NULL,
4617       p_customer_bank_account_name       IN  iby_ext_bank_accounts_v.bank_account_name%TYPE DEFAULT NULL,
4618       p_payment_trxn_extension_id        IN   NUMBER DEFAULT NULL,  /* bichatte payment uptake */
4619       p_location                         IN  hz_cust_site_uses.location%TYPE DEFAULT NULL,
4620       p_customer_site_use_id             IN  hz_cust_site_uses.site_use_id%TYPE DEFAULT NULL,
4621       p_default_site_use                 IN VARCHAR2 DEFAULT 'Y',   --bug4448307-4509459
4622       p_customer_receipt_reference       IN  ar_cash_receipts.customer_receipt_reference%TYPE DEFAULT NULL,
4623       p_override_remit_account_flag      IN  ar_cash_receipts.override_remit_account_flag%TYPE DEFAULT NULL,
4624       p_remittance_bank_account_id       IN  ar_cash_receipts.remit_bank_acct_use_id%TYPE DEFAULT NULL,
4625       p_remittance_bank_account_num      IN  ce_bank_accounts.bank_account_num%TYPE DEFAULT NULL,
4626       p_remittance_bank_account_name     IN  ce_bank_accounts.bank_account_name%TYPE DEFAULT NULL,
4627       p_deposit_date                     IN  ar_cash_receipts.deposit_date%TYPE DEFAULT NULL,
4628       p_receipt_method_id                IN  ar_cash_receipts.receipt_method_id%TYPE DEFAULT NULL,
4629       p_receipt_method_name              IN  ar_receipt_methods.name%TYPE DEFAULT NULL,
4630       p_doc_sequence_value               IN  NUMBER   DEFAULT NULL,
4631       p_ussgl_transaction_code           IN  ar_cash_receipts.ussgl_transaction_code%TYPE DEFAULT NULL,
4632       p_anticipated_clearing_date        IN  ar_cash_receipts.anticipated_clearing_date%TYPE DEFAULT NULL,
4633       p_called_from                      IN VARCHAR2 DEFAULT NULL,
4634       p_attribute_rec                    IN attribute_rec_type DEFAULT attribute_rec_const,
4635        -- ******* Global Flexfield parameters *******
4636       p_global_attribute_rec  IN global_attribute_rec_type DEFAULT global_attribute_rec_const,
4637       p_receipt_comments      IN VARCHAR2 DEFAULT NULL,
4638      --   ***  Notes Receivable Additional Information  ***
4639       p_issuer_name           IN ar_cash_receipts.issuer_name%TYPE DEFAULT NULL,
4640       p_issue_date            IN ar_cash_receipts.issue_date%TYPE DEFAULT NULL,
4641       p_issuer_bank_branch_id IN ar_cash_receipts.issuer_bank_branch_id%TYPE DEFAULT NULL,
4642   --  ** OUT NOCOPY variables for Creating receipt
4643       p_cr_id		      OUT NOCOPY ar_cash_receipts.cash_receipt_id%TYPE,
4644    -- Receipt application parameters
4645       p_customer_trx_id         IN ra_customer_trx.customer_trx_id%TYPE DEFAULT NULL,
4646       p_trx_number              IN ra_customer_trx.trx_number%TYPE DEFAULT NULL,
4647       p_installment             IN ar_payment_schedules.terms_sequence_number%TYPE DEFAULT NULL,
4648       p_applied_payment_schedule_id     IN ar_payment_schedules.payment_schedule_id%TYPE DEFAULT NULL,
4649       p_amount_applied          IN ar_receivable_applications.amount_applied%TYPE DEFAULT NULL,
4650       -- this is the allocated receipt amount
4651       p_amount_applied_from     IN ar_receivable_applications.amount_applied_from%TYPE DEFAULT NULL,
4652       p_trans_to_receipt_rate   IN ar_receivable_applications.trans_to_receipt_rate%TYPE DEFAULT NULL,
4653       p_discount                IN ar_receivable_applications.earned_discount_taken%TYPE DEFAULT NULL,
4654       p_apply_date              IN ar_receivable_applications.apply_date%TYPE DEFAULT NULL,
4655       p_apply_gl_date           IN ar_receivable_applications.gl_date%TYPE DEFAULT NULL,
4656       app_ussgl_transaction_code  IN ar_receivable_applications.ussgl_transaction_code%TYPE DEFAULT NULL,
4657       p_customer_trx_line_id	  IN ar_receivable_applications.applied_customer_trx_line_id%TYPE DEFAULT NULL,
4658       p_line_number             IN ra_customer_trx_lines.line_number%TYPE DEFAULT NULL,
4659       p_show_closed_invoices    IN VARCHAR2 DEFAULT 'N', /* Bug fix 2462013 */
4660       p_move_deferred_tax       IN VARCHAR2 DEFAULT 'Y',
4661       p_link_to_trx_hist_id     IN ar_receivable_applications.link_to_trx_hist_id%TYPE DEFAULT NULL,
4662       app_attribute_rec           IN attribute_rec_type DEFAULT attribute_rec_const,
4663   -- ******* Global Flexfield parameters *******
4664       app_global_attribute_rec    IN global_attribute_rec_type DEFAULT global_attribute_rec_const,
4665       app_comments                IN ar_receivable_applications.comments%TYPE DEFAULT NULL,
4666       p_call_payment_processor    IN VARCHAR2 DEFAULT FND_API.G_FALSE,
4667       p_org_id             IN NUMBER  DEFAULT NULL
4668       )  IS
4669 l_cash_receipt_id  NUMBER(15);
4670 l_create_return_status  VARCHAR2(1);
4671 l_create_msg_count NUMBER;
4672 l_create_msg_data  VARCHAR2(2000);
4673 l_payment_trxn_extension_id  ar_cash_receipts.payment_trxn_extension_id%TYPE;
4674 
4675 -- OSTEINME 3/2/2001: added return variable for credit card call
4676 l_cc_return_status VARCHAR2(1);		-- credit card return status
4677 l_response_error_code  VARCHAR2(80);
4678 BEGIN
4679   IF PG_DEBUG in ('Y', 'C') THEN
4680      arp_standard.debug('ar_receipt_api.Create_and_apply ()+');
4681   END IF;
4682 
4683        /*------------------------------------+
4684         |   Standard start of API savepoint  |
4685         +------------------------------------*/
4686 
4687         SAVEPOINT Create_Apply_PVT;
4688       --calling the internal create_cash routine
4689      Create_cash_1(
4690                  p_api_version,
4691                  p_init_msg_list,
4692                  FND_API.G_FALSE,  --p_commit is passed as FND_API.G_FALSE here but the actual value is passed i Apply routine
4693                  FND_API.G_VALID_LEVEL_FULL,
4694                  l_create_return_status,
4695                  l_create_msg_count,
4696                  l_create_msg_data,
4697                  -- Receipt info. parameters
4698                  p_usr_currency_code,
4699                  p_currency_code,
4700                  p_usr_exchange_rate_type,
4701                  p_exchange_rate_type,
4702                  p_exchange_rate,
4703                  p_exchange_rate_date,
4704                  p_amount,
4705                  p_factor_discount_amount,
4706                  p_receipt_number,
4707                  p_receipt_date,
4708                  p_gl_date,
4709                  p_maturity_date,
4710                  p_postmark_date,
4711                  p_customer_id,
4712                  p_customer_name,
4713                  p_customer_number,
4714                  p_customer_bank_account_id,
4715                  p_customer_bank_account_num,
4716                  p_customer_bank_account_name,
4717                  p_payment_trxn_extension_id,
4718                  p_location,
4719                  p_customer_site_use_id,
4720                  p_default_site_use,    --bug4448307-4509459
4721                  p_customer_receipt_reference,
4722                  p_override_remit_account_flag,
4723                  p_remittance_bank_account_id,
4724                  p_remittance_bank_account_num,
4725                  p_remittance_bank_account_name,
4726                  p_deposit_date,
4727                  p_receipt_method_id,
4728                  p_receipt_method_name,
4729                  p_doc_sequence_value,
4730                  p_ussgl_transaction_code,
4731                  p_anticipated_clearing_date,
4732                  p_called_from,
4733                  p_attribute_rec,
4734        -- ******* Global Flexfield parameters *******
4735                  p_global_attribute_rec ,
4736                  p_receipt_comments,
4737       --   ***  Notes Receivable Additional Information  ***
4738                  p_issuer_name,
4739                  p_issue_date,
4740                  p_issuer_bank_branch_id,
4741                  p_customer_trx_id,
4742                  p_trx_number,
4743                  p_installment,
4744                  p_applied_payment_schedule_id,
4745                  'CREATE_AND_APPLY', --used internally to differentiate between create_cash and create_and_apply
4746                  p_org_id,
4747       --   ** OUT NOCOPY variables
4748                  l_cash_receipt_id --out variable
4749                  );
4750           IF PG_DEBUG in ('Y', 'C') THEN
4751              arp_util.debug('Apply: ' || 'Receipt create_return_status '||l_create_return_status);
4752           END IF;
4753            --IF the receipt creation part returns no errors then
4754            --call the application routine.
4755            IF l_create_return_status = FND_API.G_RET_STS_SUCCESS THEN
4756 
4757 
4758               IF l_create_msg_count = 1 THEN
4759                 /* If one message, like warning, then put this back on stack as the
4760                    Create routine must have removed it from the stack and put it on x_msg_data
4761                  */
4762                   FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
4763                   FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','CREATE_AND_APPLY : '||l_create_msg_data);
4764                   FND_MSG_PUB.Add;
4765                END IF;
4766 
4767               Apply( p_api_version                  => p_api_version,
4768                      p_init_msg_list                => FND_API.G_FALSE, --message stack is not initialized here.
4769                      p_commit                       => p_commit,
4770                      p_validation_level             => FND_API.G_VALID_LEVEL_FULL,
4771                      x_return_status                => x_return_status,
4772                      x_msg_count                    => x_msg_count,
4773                      x_msg_data                     => x_msg_data,
4774                      p_cash_receipt_id              => l_cash_receipt_id,
4775                      p_trx_number                   => p_trx_number,
4776                      p_customer_trx_id              => p_customer_trx_id,
4777                      p_installment                  => p_installment,
4778                      p_applied_payment_schedule_id  => p_applied_payment_schedule_id,
4779                      p_amount_applied               => p_amount_applied,
4780                      p_amount_applied_from          => p_amount_applied_from,
4781                      p_trans_to_receipt_rate        => p_trans_to_receipt_rate,
4782                      p_discount                     => p_discount,
4783                      p_apply_date                   => p_apply_date,
4784                      p_apply_gl_date                => p_apply_gl_date,
4785                      p_ussgl_transaction_code       => app_ussgl_transaction_code,
4786                      p_customer_trx_line_id	    => p_customer_trx_line_id,
4787                      p_line_number                  => p_line_number,
4788                      p_show_closed_invoices         => p_show_closed_invoices,
4789                      p_called_from                  => p_called_from,
4790                      p_move_deferred_tax            => p_move_deferred_tax,
4791                      p_link_to_trx_hist_id          => p_link_to_trx_hist_id,
4792                      p_attribute_rec                => app_attribute_rec,
4793                      p_global_attribute_rec         => app_global_attribute_rec,
4794                      p_comments                     => app_comments,
4795                      p_org_id                       => p_org_id
4796                       );
4797 
4798                   --If the application fails then we need to rollback all the changes
4799                   --made in the create() routine also.
4800                   IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4801                      ROLLBACK TO Create_Apply_PVT;
4802                   ELSE
4803 
4804                      -- OSTEINME 3/2/2001: Enhancements for iReceivables
4805                      -- charge credit card if needed.  Note: this only
4806                      -- happens if the receipt and application creation
4807                      -- was successful.  All relevant information for the
4808                      -- payment can be derived from the cash receipt.
4809 
4810                      IF PG_DEBUG in ('Y', 'C') THEN
4811                         arp_standard.debug('Apply: ' || 'Checking p_call_payment_processor: ' || p_call_payment_processor);
4812                      END IF;
4813 
4814                      if (p_call_payment_processor = FND_API.G_TRUE) then
4815 
4816                              l_payment_trxn_extension_id := p_payment_trxn_extension_id;
4817 
4818                          process_payment_1(
4819 		               p_cash_receipt_id     => l_cash_receipt_id,
4820                                p_called_from         => p_called_from,
4821                                p_response_error_code => l_response_error_code,
4822                                x_msg_count           => l_create_msg_count,
4823                                x_msg_data            => l_create_msg_data,
4824 		               x_return_status       => l_cc_return_status,
4825                                p_payment_trxn_extension_id => l_payment_trxn_extension_id);
4826 
4827                        -- If the payment processor call fails, then we
4828                        -- need to rollback all the changes
4829                        -- made in the create() and apply() routines also.
4830 
4831                        IF l_cc_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4832                           ROLLBACK TO Create_Apply_PVT;
4833                           x_return_status := l_cc_return_status;
4834                           return; -- exit back to caller
4835                        END IF;
4836 
4837                      end if;  -- if p_call_payment_processor = fnd_api.g_true
4838 
4839                      -- OSTEINME 2/27/2001: bug 1659048:
4840                      -- need to pass back cr_id
4841                      p_cr_id := l_cash_receipt_id;
4842 
4843                   END IF;
4844             ELSE
4845              x_return_status := l_create_return_status;
4846              x_msg_count     := l_create_msg_count;
4847              x_msg_data      := l_create_msg_data;
4848              Return;
4849             END IF;
4850 
4851   IF PG_DEBUG in ('Y', 'C') THEN
4852      arp_util.debug('ar_receipt_api.Create_and_apply ()-');
4853   END IF;
4854 EXCEPTION
4855  WHEN others THEN
4856   IF PG_DEBUG in ('Y', 'C') THEN
4857      arp_util.debug('Exception : Create_and_apply() ');
4858   END IF;
4859          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4860          FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
4861          FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','CREATE_AND_APPLY : '||SQLERRM);
4862          FND_MSG_PUB.Add;
4863 END Create_and_apply;
4864 
4865 /*=======================================================================
4866  | PUBLIC Procedure Create_Apply_On_Acc
4867  |
4868  | DESCRIPTION
4869  |      This API creates a receipt and applies it on the Account.
4870  |
4871  |      Parematers are the same as in create cash and Apply_on_Account
4872  |      except an extra parameter p_call_payment_processor has been added
4873  |      that should be passed as true for  iPayment to do the credit card
4874  |      processing.
4875  |
4876  |      For Creating receipts and applying to invoices please use
4877  |      Create_and Apply
4878  |      -------------------------------------------------------------------
4879  |
4880  | PSEUDO CODE/LOGIC
4881  |
4882  | PARAMETERS
4883  |
4884  |
4885  | RETURNS
4886  |      nothing
4887  |
4888  | KNOWN ISSUES
4889  |
4890  |
4891  |
4892  | NOTES
4893  |
4894  |
4895  |
4896  | MODIFICATION HISTORY
4897  | Date                  Author         Description of Changes
4898  | 16-FEB-2004           Jyoti Pandey      Created
4899  |                       This API has been created for Collections team
4900  |                       Bug 3398538. This replaces their call to
4901  |                       ar_receipt_api_pub.Create_cash which is not meant
4902  |                       for Credit card receipts
4903  |01-MAR-2004            Bug 3398538 and 3236769.
4904  |                       IMPORTANT:Renaming this API as
4905  |                       Create_Apply_On_Acc from create-cash_cc_internal
4906  |                       Create_cash_cc_internal is being obsoleted.
4907  |
4908  *=======================================================================*/
4909   PROCEDURE Create_Apply_On_Acc(
4910 -- Standard API parameters.
4911       p_api_version      IN  NUMBER,
4912       p_init_msg_list    IN  VARCHAR2 := FND_API.G_FALSE,
4913       p_commit           IN  VARCHAR2 := FND_API.G_FALSE,
4914       p_validation_level IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
4915       x_return_status    OUT NOCOPY VARCHAR2,
4916       x_msg_count        OUT NOCOPY NUMBER,
4917       x_msg_data         OUT NOCOPY VARCHAR2,
4918  -- Receipt info. parameters
4919       p_usr_currency_code  IN  VARCHAR2 DEFAULT NULL, --the translated currency code
4920       p_currency_code      IN  ar_cash_receipts.currency_code%TYPE DEFAULT NULL,
4921       p_usr_exchange_rate_type  IN  VARCHAR2 DEFAULT NULL,
4922       p_exchange_rate_type IN  ar_cash_receipts.exchange_rate_type%TYPE DEFAULT NULL,
4923       p_exchange_rate      IN  ar_cash_receipts.exchange_rate%TYPE DEFAULT NULL,
4924       p_exchange_rate_date IN  ar_cash_receipts.exchange_date%TYPE DEFAULT NULL,
4925       p_amount             IN  ar_cash_receipts.amount%TYPE DEFAULT NULL,
4926       p_factor_discount_amount IN ar_cash_receipts.factor_discount_amount%TYPE
4927                                   DEFAULT NULL,
4928       p_receipt_number    IN  ar_cash_receipts.receipt_number%TYPE DEFAULT NULL,
4929       p_receipt_date      IN  ar_cash_receipts.receipt_date%TYPE DEFAULT NULL,
4930       p_gl_date           IN  ar_cash_receipt_history.gl_date%TYPE DEFAULT NULL,
4931       p_maturity_date     IN  DATE DEFAULT NULL,
4932       p_postmark_date     IN  DATE DEFAULT NULL,
4933       p_customer_id       IN  ar_cash_receipts.pay_from_customer%TYPE DEFAULT NULL,
4934       /* tca uptake */
4935       p_customer_name     IN  hz_parties.party_name%TYPE DEFAULT NULL,
4936       p_customer_number   IN  hz_cust_accounts.account_number%TYPE DEFAULT NULL,
4937       p_customer_bank_account_id    IN  ar_cash_receipts.customer_bank_account_id%TYPE
4938                                         DEFAULT NULL,
4939      /* 6612301 */
4940       p_customer_bank_account_num   IN  iby_ext_bank_accounts_v.bank_account_number%TYPE
4941                                         DEFAULT NULL,
4942       p_customer_bank_account_name  IN  iby_ext_bank_accounts_v.bank_account_name%TYPE
4943                                         DEFAULT NULL,
4944       p_payment_trxn_extension_id    IN NUMBER DEFAULT NULL, /* bichatte payment uptake */
4945       p_location                   IN  hz_cust_site_uses.location%TYPE DEFAULT NULL,
4946       p_customer_site_use_id       IN  hz_cust_site_uses.site_use_id%TYPE DEFAULT NULL,
4947       p_default_site_use                 IN VARCHAR2 DEFAULT 'Y', --bug 4448307-4509459
4948       p_customer_receipt_reference IN  ar_cash_receipts.customer_receipt_reference%TYPE
4949                                        DEFAULT NULL,
4950       p_override_remit_account_flag  IN  ar_cash_receipts.override_remit_account_flag%TYPE
4951                                         DEFAULT NULL,
4952       p_remittance_bank_account_id   IN  NUMBER  DEFAULT NULL,
4953       p_remittance_bank_account_num  IN VARCHAR2 DEFAULT NULL,
4954       p_remittance_bank_account_name IN VARCHAR2 DEFAULT NULL,
4955       p_deposit_date           IN  ar_cash_receipts.deposit_date%TYPE DEFAULT NULL,
4956       p_receipt_method_id      IN  ar_cash_receipts.receipt_method_id%TYPE DEFAULT NULL,
4957       p_receipt_method_name    IN  ar_receipt_methods.name%TYPE DEFAULT NULL,
4958       p_doc_sequence_value     IN  NUMBER   DEFAULT NULL,
4959       p_ussgl_transaction_code IN  ar_cash_receipts.ussgl_transaction_code%TYPE
4960                                    DEFAULT NULL,
4961       p_anticipated_clearing_date IN  ar_cash_receipts.anticipated_clearing_date%TYPE
4962                                       DEFAULT NULL,
4963       p_called_from    IN VARCHAR2 DEFAULT NULL,
4964       p_attribute_rec  IN attribute_rec_type DEFAULT attribute_rec_const,
4965        -- ******* Global Flexfield parameters *******
4966       p_global_attribute_rec  IN global_attribute_rec_type
4967                                  DEFAULT global_attribute_rec_const,
4968       p_receipt_comments      IN VARCHAR2 DEFAULT NULL,
4969      --   ***  Notes Receivable Additional Information  ***
4970       p_issuer_name           IN ar_cash_receipts.issuer_name%TYPE DEFAULT NULL,
4971       p_issue_date            IN ar_cash_receipts.issue_date%TYPE DEFAULT NULL,
4972       p_issuer_bank_branch_id IN ar_cash_receipts.issuer_bank_branch_id%TYPE DEFAULT NULL,
4973   --  ** OUT NOCOPY variables for Creating receipt
4974       p_cr_id                 OUT NOCOPY ar_cash_receipts.cash_receipt_id%TYPE,
4975    -- Receipt application parameters
4976       p_amount_applied   IN ar_receivable_applications.amount_applied%TYPE DEFAULT NULL,
4977       p_apply_date       IN ar_receivable_applications.apply_date%TYPE DEFAULT NULL,
4978       p_apply_gl_date    IN ar_receivable_applications.gl_date%TYPE DEFAULT NULL,
4979       app_ussgl_transaction_code  IN ar_receivable_applications.ussgl_transaction_code%TYPE DEFAULT NULL,
4980       app_attribute_rec  IN attribute_rec_type DEFAULT attribute_rec_const,
4981   -- ******* Global Flexfield parameters *******
4982       app_global_attribute_rec IN global_attribute_rec_type
4983                                   DEFAULT global_attribute_rec_const,
4984       app_comments             IN ar_receivable_applications.comments%TYPE DEFAULT NULL,
4985       p_application_ref_num    IN ar_receivable_applications.application_ref_num%TYPE
4986                                   DEFAULT NULL,
4987       p_secondary_application_ref_id IN
4988                 ar_receivable_applications.secondary_application_ref_id%TYPE DEFAULT NULL,
4989       p_customer_reference IN ar_receivable_applications.customer_reference%TYPE
4990                               DEFAULT NULL,
4991       p_customer_reason IN ar_receivable_applications.customer_reason%TYPE DEFAULT NULL,
4992       p_secondary_app_ref_type IN
4993                 ar_receivable_applications.secondary_application_ref_type%TYPE := null,
4994       p_secondary_app_ref_num IN
4995                 ar_receivable_applications.secondary_application_ref_num%TYPE := null,
4996 
4997       p_call_payment_processor    IN VARCHAR2 DEFAULT FND_API.G_FALSE,
4998       p_org_id                    IN NUMBER  DEFAULT NULL
4999       ) IS
5000 
5001       l_cash_receipt_id   ar_cash_receipts.cash_receipt_id%type;
5002 
5003 l_create_return_status  VARCHAR2(1);
5004 l_create_msg_count NUMBER;
5005 l_create_msg_data  VARCHAR2(2000);
5006 
5007 l_apply_return_status  VARCHAR2(1);
5008 l_apply_msg_count NUMBER;
5009 l_apply_msg_data  VARCHAR2(2000);
5010 
5011 l_cc_return_status      VARCHAR2(1);
5012 
5013 l_api_name       CONSTANT VARCHAR2(20) := 'Create_Apply_On_Acc';
5014 l_api_version    CONSTANT NUMBER       := 1.0;
5015 l_creation_method_code   VARCHAR2(50);
5016 l_response_error_code VARCHAR2(80);
5017 l_payment_trxn_extension_id  ar_cash_receipts.payment_trxn_extension_id%TYPE;
5018 
5019 BEGIN
5020 
5021      IF PG_DEBUG in ('Y', 'C') THEN
5022       arp_standard.debug('ar_receipt_api_pub.Create_Apply_On_Acc() +');
5023      END IF;
5024 
5025 
5026      /*------------------------------------+
5027       |   Standard start of API savepoint  |
5028       +------------------------------------*/
5029       SAVEPOINT Create_Apply_On_Acc;
5030 
5031       /*-----------------------------------------+
5032        |   Initialize return status to SUCCESS   |
5033        +-----------------------------------------*/
5034 
5035         x_return_status := FND_API.G_RET_STS_SUCCESS;
5036 
5037       /*--------------------------------------------------+
5038        |   Standard call to check for call compatibility  |
5039        +--------------------------------------------------*/
5040         IF NOT FND_API.Compatible_API_Call(
5041                                             l_api_version,
5042                                             p_api_version,
5043                                             l_api_name,
5044                                             G_PKG_NAME
5045                                           )
5046         THEN
5047               x_return_status := FND_API.G_RET_STS_ERROR;
5048               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5049         END IF;
5050 
5051        /*--------------------------------------------------------------+
5052         |   Initialize message list if p_init_msg_list is set to TRUE  |
5053         +--------------------------------------------------------------*/
5054         IF FND_API.to_Boolean( p_init_msg_list )
5055           THEN
5056               FND_MSG_PUB.initialize;
5057         END IF;
5058 
5059 
5060         /*-------------------------------------------------+
5061          | Initialize the profile option package variables |
5062          +-------------------------------------------------*/
5063          initialize_profile_globals;
5064 
5065         /*-------------------------------------------------+
5066         | Call the internal routine to create receipt      |
5067         +--------------------------------------------------*/
5068 
5069          Create_cash_1(
5070            -- Standard API parameters.
5071              p_api_version ,
5072              p_init_msg_list,
5073              p_commit,
5074              p_validation_level,
5075              l_create_return_status,
5076              l_create_msg_count,
5077              l_create_msg_data ,
5078              -- Receipt info. parameters
5079              p_usr_currency_code, --the translated currency code
5080              p_currency_code    ,
5081              p_usr_exchange_rate_type ,
5082              p_exchange_rate_type     ,
5083              p_exchange_rate         ,
5084              p_exchange_rate_date    ,
5085              p_amount                ,
5086              p_factor_discount_amount,
5087              p_receipt_number        ,
5088              p_receipt_date          ,
5089              p_gl_date               ,
5090              p_maturity_date         ,
5091              p_postmark_date         ,
5092              p_customer_id           ,
5093              p_customer_name         ,
5094              p_customer_number       ,
5095              p_customer_bank_account_id ,
5096              p_customer_bank_account_num ,
5097              p_customer_bank_account_name ,
5098              p_payment_trxn_extension_id,   -- bichatte payment uptake project
5099              p_location               ,
5100              p_customer_site_use_id   ,
5101              p_default_site_use,    --bug4448307-4509459
5102              p_customer_receipt_reference ,
5103              p_override_remit_account_flag ,
5104              p_remittance_bank_account_id  ,
5105              p_remittance_bank_account_num ,
5106              p_remittance_bank_account_name ,
5107              p_deposit_date             ,
5108              p_receipt_method_id        ,
5109              p_receipt_method_name      ,
5110              p_doc_sequence_value       ,
5111              p_ussgl_transaction_code   ,
5112              p_anticipated_clearing_date ,
5113              p_called_from               ,
5114              p_attribute_rec         ,
5115               -- ******* Global Flexfield parameters *******
5116              p_global_attribute_rec  ,
5117              p_receipt_comments      ,
5118       --   ***  Notes Receivable Additional Information  ***
5119              p_issuer_name    ,
5120              p_issue_date     ,
5121              p_issuer_bank_branch_id  ,
5122       --added  parameters to differentiate between create_cash and create_and_apply
5123              NULL,
5124              NULL,
5125              NULL,
5126              NULL,
5127              'CREATE_CASH',
5128              p_org_id,
5129       --   ** OUT variables
5130              l_cash_receipt_id
5131               );
5132 
5133 
5134            IF PG_DEBUG in ('Y', 'C') THEN
5135              arp_util.debug(  'Receipt create_return_status '||l_create_return_status);
5136           END IF;
5137 
5138 
5139           /*------------------------------------------------------+
5140            | Check the return status from create_cash             |
5141            +------------------------------------------------------*/
5142           IF l_create_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5143 
5144              x_return_status := l_create_return_status;
5145              ROLLBACK TO Create_Apply_On_Acc;
5146              FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
5147                                        p_count => l_create_msg_count,
5148                                        p_data  => l_create_msg_data);
5149 
5150              x_msg_count     := l_create_msg_count;
5151              x_msg_data      := l_create_msg_data;
5152 
5153              RETURN; -- exit back to caller
5154 
5155           ELSE       --l_create_return_status
5156 
5157               IF l_create_msg_count = 1 THEN
5158 
5159                  ---If one message, like warning, then put this back
5160                  ---on stack as the Create routine must have removed
5161                  ---it from the stack and put it on x_msg_data
5162 
5163                  FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
5164                  FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','CREATE_AND_APPLY : '||l_create_msg_data);
5165                  FND_MSG_PUB.Add;
5166               END IF;
5167 
5168         /*------------------------------------------------------+
5169                | Call the internal routine to apply on account receipt|
5170                +------------------------------------------------------*/
5171               Apply_on_account(
5172                 p_api_version               =>  p_api_version,
5173                 p_init_msg_list             =>  p_init_msg_list,
5174                 p_commit                    =>  p_commit,
5175                 p_validation_level          =>  p_validation_level,
5176                 x_return_status             =>  l_apply_return_status ,
5177                 x_msg_count                 =>  l_apply_msg_count,
5178                 x_msg_data                  =>  l_apply_msg_data ,
5179                 p_cash_receipt_id           =>  l_cash_receipt_id,
5180                 p_amount_applied            =>  p_amount_applied,
5181                 p_apply_date                =>  p_apply_date,
5182                 p_apply_gl_date             =>  p_apply_gl_date,
5183                 p_ussgl_transaction_code    =>  p_ussgl_transaction_code,
5184                 p_attribute_rec             =>  app_attribute_rec, /* 5731076 */
5185                 p_global_attribute_rec      =>  p_global_attribute_rec,
5186                 p_comments                  =>  app_comments,
5187                 p_called_from               =>  'RAPI',
5188                 p_application_ref_num       =>  p_application_ref_num,
5189                 p_secondary_application_ref_id => p_secondary_application_ref_id,
5190                 p_customer_reference           => p_customer_reference,
5191                 p_customer_reason              => p_customer_reason,
5192                 p_secondary_app_ref_type       => p_secondary_app_ref_type,
5193                 p_secondary_app_ref_num        => p_secondary_app_ref_num,
5194                 p_org_id                       => p_org_id
5195               );
5196 
5197 
5198               /*------------------------------------------------------+
5199                | Check the return status from Apply_on_account        |
5200                +------------------------------------------------------*/
5201               IF l_apply_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5202                  x_return_status := l_apply_return_status;
5203                  ROLLBACK TO Create_Apply_On_Acc;
5204                  FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
5205                                            p_count => l_apply_msg_count,
5206                                            p_data  => l_apply_msg_data);
5207 
5208                   x_msg_count     := l_apply_msg_count;
5209                   x_msg_data      := l_apply_msg_data;
5210 
5211                  RETURN; -- exit back to caller
5212               ELSE
5213 
5214 
5215                     IF PG_DEBUG in ('Y', 'C') THEN
5216                      arp_util.debug('Create_Apply_On_Acc: ' || 'Checking p_call_payment_processor: ' || p_call_payment_processor);
5217                     END IF;
5218 
5219                     IF ( p_call_payment_processor = FND_API.G_TRUE ) THEN
5220 
5221                         l_payment_trxn_extension_id := p_payment_trxn_extension_id;
5222 
5223                       /*------------------------------------------------------+
5224                        | Call the API to process Credit cards                 |
5225                        +------------------------------------------------------*/
5226                        Process_Payment_1(
5227                        p_cash_receipt_id          => l_cash_receipt_id,
5228                        p_called_from              => p_called_from,
5229                        p_response_error_code     => l_response_error_code,
5230                        x_msg_count               => x_msg_count,
5231                        x_msg_data                => x_msg_data,
5232                        x_return_status           => l_cc_return_status,
5233                        p_payment_trxn_extension_id => l_payment_trxn_extension_id);
5234 
5235                        IF PG_DEBUG in ('Y', 'C') THEN
5236                           arp_util.debug('Create_Apply_On_Acc: ' || 'Process_Credit_card return status: ' || l_cc_return_status);
5237                        END IF;
5238 
5239                        /*------------------------------------------------------+
5240                         | Check the return status from Process_Payment         |
5241                         +------------------------------------------------------*/
5242                        IF l_cc_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5243                           x_return_status := l_cc_return_status;
5244 
5245                           IF PG_DEBUG in ('Y', 'C') THEN
5246                              arp_util.debug('Create_Apply_On_Acc: ' || 'p_payment_response_error_code: ' || l_response_error_code);
5247                           END IF;
5248 
5249                           ROLLBACK TO Create_Apply_On_Acc;
5250                           FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
5251                                       p_count => x_msg_count,
5252                                       p_data  => x_msg_data);
5253                           RETURN; -- exit back to caller
5254                        END IF;
5255 
5256                      END IF;  -- p_call_payment_processor
5257 
5258              END IF;    --l_apply_return_status
5259 
5260           END IF ; ---l_create_return_status
5261 
5262           /*------------------------------------------------------+
5263            | Initialize the OUT parameter p_cr_id
5264            +------------------------------------------------------*/
5265            p_cr_id := l_cash_receipt_id;
5266 
5267        IF PG_DEBUG in ('Y', 'C') THEN
5268           arp_standard.debug('ar_receipt_api_pub.Create_Apply_On_Acc()-');
5269        END IF;
5270 EXCEPTION
5271 WHEN OTHERS THEN
5272 
5273      IF PG_DEBUG in ('Y', 'C') THEN
5274         arp_util.debug('Create_Apply_On_Acc: ' || 'Exception:'|| SQLERRM);
5275      END IF;
5276 
5277      x_return_status :=  FND_API.G_RET_STS_ERROR ;
5278      FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
5279      FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','AR_RECEIPT_API_PUB.Create_Apply_On_Acc:'|| SQLERRM);
5280      FND_MSG_PUB.Add;
5281      ROLLBACK TO Create_Apply_On_Acc;
5282      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
5283                                p_count => x_msg_count,
5284                                p_data  => x_msg_data);
5285 
5286 
5287 END Create_Apply_On_Acc;
5288 
5289 PROCEDURE  reverse_cover(
5290                 p_cash_receipt_id        IN NUMBER,
5291                 p_reversal_category_code IN VARCHAR2,
5292                 p_reversal_gl_date       IN DATE,
5293                 p_reversal_date          IN DATE,
5294                 p_reversal_reason_code   IN VARCHAR2,
5295                 p_reversal_comments      IN VARCHAR2,
5296                 p_attribute_record       IN attribute_rec_type,
5297                 p_type                   IN VARCHAR2,
5298                 p_called_from            IN VARCHAR2,
5299                 p_return_status          OUT NOCOPY VARCHAR2
5300 
5301                  ) IS
5302 l_rev_crh_id            NUMBER;
5303 l_crh_rec		ar_cash_receipt_history%ROWTYPE;
5304 l_cr_rec		ar_cash_receipts%ROWTYPE;
5305 l_ps_rec		ar_payment_schedules%ROWTYPE;
5306 BEGIN
5307   p_return_status := FND_API.G_RET_STS_SUCCESS;
5308        /*-------------------------------------------------+
5309         | Initialize SOB/org dependent variables          |
5310         +-------------------------------------------------*/
5311         arp_global.init_global;
5312         arp_standard.init_standard;
5313 
5314   -- --------------------------------------------------------------
5315   -- First lock existing records from database for update
5316   -- --------------------------------------------------------------
5317 
5318   -- get current cash_receipt_history record:
5319 
5320   l_crh_rec.cash_receipt_id	:= p_cash_receipt_id;
5321   arp_cr_history_pkg.nowaitlock_fetch_f_cr_id(l_crh_rec);
5322 
5323   -- get cash receipt record:
5324 
5325   l_cr_rec.cash_receipt_id 	:= p_cash_receipt_id;
5326   arp_cash_receipts_pkg.nowaitlock_fetch_p(l_cr_rec);
5327 
5328   -- get payment schedule record for cash receipt.
5329   IF p_type = 'CASH' THEN
5330      arp_proc_rct_util.get_ps_rec(l_cr_rec.cash_receipt_id,
5331                                   l_ps_rec);
5332   END IF;
5333 
5334   --we do not do anything with the fetched data as we just need to do the reversal of the receipt.
5335   --the existing routines have been used for locking (reusability)
5336  BEGIN
5337    arp_reverse_receipt.reverse(
5338                 p_cash_receipt_id,
5339                 p_reversal_category_code,
5340                 p_reversal_gl_date,
5341                 p_reversal_date,
5342                 p_reversal_reason_code,
5343                 p_reversal_comments,
5344                 NULL,                   -- clear_batch_id
5345                 p_attribute_record.attribute_category,
5346                 p_attribute_record.attribute1,
5347                 p_attribute_record.attribute2,
5348                 p_attribute_record.attribute3,
5349                 p_attribute_record.attribute4,
5350                 p_attribute_record.attribute5,
5351                 p_attribute_record.attribute6,
5352                 p_attribute_record.attribute7,
5353                 p_attribute_record.attribute8,
5354                 p_attribute_record.attribute9,
5355                 p_attribute_record.attribute10,
5356                 p_attribute_record.attribute11,
5357                 p_attribute_record.attribute12,
5358                 p_attribute_record.attribute13,
5359                 p_attribute_record.attribute14,
5360                 p_attribute_record.attribute15,
5361                 'RAPI',
5362                 '1.0',
5363                 l_rev_crh_id,
5364                 p_called_from);
5365 
5366                /* Bug 4910860
5367                Validate if the accounting entries balance */
5368                arp_balance_check.Check_Recp_Balance(p_cash_receipt_id,NULL,'Y');
5369 
5370  EXCEPTION
5371    WHEN OTHERS THEN
5372 
5373                /*-------------------------------------------------------+
5374                 |  Handle application errors that result from trapable  |
5375                 |  error conditions. The error messages have already    |
5376                 |  been put on the error stack.                         |
5377                 +-------------------------------------------------------*/
5378 
5379                 IF (SQLCODE = -20001)
5380                 THEN
5381 
5382                       p_return_status := FND_API.G_RET_STS_ERROR ;
5383                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
5384                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_REVERSE_RECEIPT.REVERSE : '||SQLERRM);
5385                       FND_MSG_PUB.Add;
5386 
5387                       RETURN;
5388                 ELSE
5389                    RAISE;
5390                 END IF;
5391  END;
5392 EXCEPTION
5393  WHEN others THEN
5394  IF PG_DEBUG in ('Y', 'C') THEN
5395     arp_util.debug('Apply: ' || 'EXCEPTION: reverse_cover()');
5396  END IF;
5397  raise;
5398 END reverse_cover;
5399 
5400 
5401 /* This is standard reversal */
5402 PROCEDURE Reverse(
5403 -- Standard API parameters.
5404       p_api_version             IN  NUMBER,
5405       p_init_msg_list           IN  VARCHAR2 := FND_API.G_FALSE,
5406       p_commit                  IN  VARCHAR2 := FND_API.G_FALSE,
5407       p_validation_level        IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
5408       x_return_status           OUT NOCOPY VARCHAR2,
5409       x_msg_count               OUT NOCOPY NUMBER,
5410       x_msg_data                OUT NOCOPY VARCHAR2,
5411 -- Receipt reversal related parameters
5412       p_cash_receipt_id         IN ar_cash_receipts.cash_receipt_id%TYPE DEFAULT NULL,
5413       p_receipt_number          IN ar_cash_receipts.receipt_number%TYPE DEFAULT NULL,
5414       p_reversal_category_code  IN ar_cash_receipts.reversal_category%TYPE DEFAULT NULL,
5415       p_reversal_category_name  IN ar_lookups.meaning%TYPE DEFAULT NULL,
5416       p_reversal_gl_date        IN ar_cash_receipt_history.reversal_gl_date%TYPE DEFAULT NULL,
5417       p_reversal_date           IN ar_cash_receipts.reversal_date%TYPE DEFAULT NULL,
5418       p_reversal_reason_code    IN ar_cash_receipts.reversal_reason_code%TYPE DEFAULT NULL,
5419       p_reversal_reason_name    IN ar_lookups.meaning%TYPE DEFAULT NULL,
5420       p_reversal_comments       IN ar_cash_receipts.reversal_comments%TYPE DEFAULT NULL,
5421       p_called_from             IN VARCHAR2 DEFAULT NULL,
5422       p_attribute_rec           IN attribute_rec_type DEFAULT attribute_rec_const,
5423       --p_global_attribute_rec    IN global_attribute_rec_type_upd DEFAULT global_attribute_rec_upd_cons
5424       p_global_attribute_rec    IN global_attribute_rec_type DEFAULT global_attribute_rec_const,
5425       p_cancel_claims_flag      IN VARCHAR2 DEFAULT 'Y',
5426       p_org_id             IN NUMBER  DEFAULT NULL
5427        ) IS
5428 l_api_name       CONSTANT VARCHAR2(20) := 'Create_cash';
5429 l_api_version    CONSTANT NUMBER       := 1.0;
5430 
5431 l_std_reversal_possible  VARCHAR2(1);
5432 l_cash_receipt_id        NUMBER;
5433 l_reversal_category_code VARCHAR2(20);
5434 l_reversal_reason_code   VARCHAR2(30);
5435 l_rev_crh_id           NUMBER;
5436 l_receipt_state         VARCHAR2(30);
5437 l_reversal_gl_date      DATE;
5438 l_reversal_date         DATE;
5439 l_receipt_gl_date         DATE;
5440 l_rev_return_status       VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
5441 l_clm_return_status       VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
5442 l_val_return_status       VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
5443 l_dflex_val_return_status VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
5444 l_dflex_def_return_status VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS; /* Bug fix 3539008 */
5445 l_dflex_val1_return_status VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
5446 l_attribute_rec           attribute_rec_type;
5447 --l_global_attribute_rec    global_attribute_rec_type_upd;
5448 l_global_attribute_rec    global_attribute_rec_type;
5449 l_glob_return_status      VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
5450 l_def_id_return_status    VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
5451 l_type                    VARCHAR2(20);
5452 l_rev_cover_return_status       VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
5453 l_msg_count               NUMBER;
5454 l_msg_data                VARCHAR2(2000);
5455 l_org_return_status VARCHAR2(1);
5456 l_org_id                           NUMBER;
5457 BEGIN
5458 
5459        /*------------------------------------+
5460         |   Standard start of API savepoint  |
5461         +------------------------------------*/
5462 
5463         SAVEPOINT Reverse_PVT;
5464 
5465        /*--------------------------------------------------+
5466         |   Standard call to check for call compatibility  |
5467         +--------------------------------------------------*/
5468 
5469         IF NOT FND_API.Compatible_API_Call(
5470                                             l_api_version,
5471                                             p_api_version,
5472                                             l_api_name,
5473                                             G_PKG_NAME
5474                                           )
5475         THEN
5476               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5477         END IF;
5478 
5479        /*--------------------------------------------------------------+
5480         |   Initialize message list if p_init_msg_list is set to TRUE  |
5481         +--------------------------------------------------------------*/
5482 
5483         IF FND_API.to_Boolean( p_init_msg_list )
5484           THEN
5485               FND_MSG_PUB.initialize;
5486         END IF;
5487        /*-----------------------------------------+
5488         |   Initialize return status to SUCCESS   |
5489         +-----------------------------------------*/
5490 
5491         x_return_status := FND_API.G_RET_STS_SUCCESS;
5492 
5493 
5494         IF PG_DEBUG in ('Y', 'C') THEN
5495            arp_util.debug('Apply: ' || 'Reverse()+ ');
5496         END IF;
5497 
5498 
5499 /* SSA change */
5500        l_org_id            := p_org_id;
5501        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
5502        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
5503                                                 p_return_status =>l_org_return_status);
5504  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5505        x_return_status := FND_API.G_RET_STS_ERROR;
5506  ELSE
5507 
5508         /*-------------------------------------------------+
5509          | Initialize the profile option package variables |
5510          +-------------------------------------------------*/
5511 
5512            initialize_profile_globals;
5513 
5514 
5515 
5516        /*---------------------------------------------+
5517         |   ========== Start of API Body ==========   |
5518         +---------------------------------------------*/
5519 
5520         --Assign IN parameter values to local variables
5521         --which are also used as assignment targets.
5522 
5523           l_cash_receipt_id        := p_cash_receipt_id;
5524           l_reversal_category_code := p_reversal_category_code;
5525           l_reversal_reason_code   := p_reversal_reason_code;
5526           l_reversal_gl_date       := trunc(p_reversal_gl_date);
5527           l_reversal_date          := trunc(p_reversal_date);
5528           l_attribute_rec          := p_attribute_rec;
5529           l_global_attribute_rec   := p_global_attribute_rec;
5530 
5531           /*-----------------------+
5532            |                       |
5533            |ID TO VALUE CONVERSION |
5534            |                       |
5535            +-----------------------*/
5536 
5537           ar_receipt_lib_pvt.Derive_reverse_ids(
5538                                       p_receipt_number,
5539                                       l_cash_receipt_id,
5540                                       p_reversal_category_name,
5541                                       l_reversal_category_code,
5542                                       p_reversal_reason_name,
5543                                       l_reversal_reason_code,
5544                                       l_def_id_return_status
5545                                       );
5546           IF PG_DEBUG in ('Y', 'C') THEN
5547              arp_util.debug('Apply: ' || 'l_def_id_return_status  :'||l_def_id_return_status);
5548           END IF;
5549           /*---------------------+
5550            |                     |
5551            |    DEFAULTING       |
5552            |                     |
5553            +---------------------*/
5554 
5555 
5556            ar_receipt_lib_pvt.default_reverse_info(
5557                                        l_cash_receipt_id,
5558                                        l_reversal_gl_date ,
5559                                        l_reversal_date ,
5560                                        l_receipt_state,
5561                                        l_receipt_gl_date,
5562                                        l_type );
5563 
5564           /*---------------------+
5565            |                     |
5566            |    VALIDATION       |
5567            |                     |
5568            +---------------------*/
5569 
5570         ar_receipt_val_pvt.validate_reverse_info(
5571                                         l_cash_receipt_id,
5572                                         l_receipt_gl_date,
5573                                         l_reversal_category_code,
5574                                         l_reversal_reason_code,
5575                                         l_reversal_gl_date,
5576                                         l_reversal_date,
5577                                         l_val_return_status
5578                                           );
5579         IF PG_DEBUG in ('Y', 'C') THEN
5580            arp_util.debug('Apply: ' || 'l_val_return_status  :'||l_val_return_status);
5581         END IF;
5582 
5583       --Check if the reversal is possible.
5584 
5585         ar_receipt_val_pvt.check_std_reversible(
5586                                         p_cash_receipt_id,
5587                                         l_reversal_date, /* Bug fix 3135407 */
5588                                         l_receipt_state,
5589                                         p_called_from,
5590                                         l_std_reversal_possible
5591                                           );
5592 
5593        IF  l_std_reversal_possible <> 'Y' THEN
5594          IF PG_DEBUG in ('Y', 'C') THEN
5595             arp_util.debug('Apply: ' || 'Standard reversal not possible for this receipt');
5596          END IF;
5597          FND_MESSAGE.SET_NAME('AR','AR_RAPI_NON_REVERSIBLE');
5598          FND_MSG_PUB.Add;
5599          l_rev_return_status := FND_API.G_RET_STS_ERROR;
5600        END IF;
5601 
5602     -- Bug 2232366 - check for existence of claims and if they are cancellable
5603        IF arp_reverse_receipt.receipt_has_non_cancel_claims(
5604              p_cr_id => p_cash_receipt_id,
5605              p_include_trx_claims => 'Y')
5606        THEN
5607          FND_MESSAGE.SET_NAME('AR','AR_RW_NO_REVERSAL_WITH_CLAIMS');
5608          FND_MSG_PUB.Add;
5609          l_clm_return_status := FND_API.G_RET_STS_ERROR;
5610        ELSE
5611           arp_reverse_receipt.cancel_claims(
5612              p_cr_id => p_cash_receipt_id
5613            , p_include_trx_claims  => 'Y'
5614            , x_return_status =>  l_clm_return_status
5615            , x_msg_count     =>  l_msg_count
5616            , x_msg_data      =>  l_msg_data);
5617        END IF;
5618 
5619 
5620         --validate and default the flexfields
5621          ar_receipt_lib_pvt.Validate_Desc_Flexfield(
5622                                         l_attribute_rec,
5623                                         'AR_CASH_RECEIPTS',
5624                                         l_dflex_val_return_status
5625                                             );
5626         IF PG_DEBUG in ('Y', 'C') THEN
5627            arp_util.debug('Apply: ' || 'l_dflex_val_return_status  :'||l_dflex_val_return_status);
5628         END IF;
5629 
5630 
5631        /* Bug fix 3539008 */
5632        /* If the descriptive flex field is not passed in or can not be defaulted,
5633           default it from the cash receipt */
5634         IF l_attribute_rec.attribute_category IS NULL THEN
5635           ar_receipt_lib_pvt.Default_Desc_Flexfield(
5636                                         l_attribute_rec,
5637                                         p_cash_receipt_id,
5638                                         l_dflex_def_return_status
5639                                             );
5640           IF l_attribute_rec.attribute_category IS NOT NULL THEN
5641              ar_receipt_lib_pvt.Validate_Desc_Flexfield(
5642                                          l_attribute_rec,
5643                                         'AR_CASH_RECEIPTS',
5644                                         l_dflex_val1_return_status
5645                                             );
5646           END IF;
5647         END IF;
5648         IF PG_DEBUG in ('Y', 'C') THEN
5649            arp_util.debug(  'l_dflex_def_return_status  :'||l_dflex_def_return_status);
5650            arp_util.debug(  'l_dflex_val1_return_status  :'||l_dflex_val1_return_status);
5651         END IF;
5652 
5653         jg_ar_cash_receipts.reverse(
5654                                     l_cash_receipt_id,
5655                                     l_glob_return_status);
5656 
5657         IF PG_DEBUG in ('Y', 'C') THEN
5658            arp_util.debug('Apply: ' || 'l_glob_return_status  :'||l_glob_return_status);
5659         END IF;
5660 END IF;
5661 
5662         IF l_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
5663            l_rev_return_status <> FND_API.G_RET_STS_SUCCESS OR
5664            l_clm_return_status <> FND_API.G_RET_STS_SUCCESS OR
5665            l_dflex_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
5666            l_dflex_def_return_status <> FND_API.G_RET_STS_SUCCESS OR
5667            l_dflex_val1_return_status <> FND_API.G_RET_STS_SUCCESS OR
5668         --   l_glob_return_status  <> FND_API.G_RET_STS_SUCCESS OR
5669            l_def_id_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5670          x_return_status := FND_API.G_RET_STS_ERROR;
5671         END IF;
5672 
5673         IF x_return_status <> FND_API.G_RET_STS_SUCCESS  THEN
5674 
5675             ROLLBACK TO Reverse_PVT;
5676 
5677              x_return_status := FND_API.G_RET_STS_ERROR ;
5678 
5679              FND_MSG_PUB.Count_And_Get(
5680                                         p_count => x_msg_count,
5681                                         p_data  => x_msg_data
5682                                        );
5683 
5684              IF PG_DEBUG in ('Y', 'C') THEN
5685                 arp_util.debug('Apply: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
5686              END IF;
5687              Return;
5688         END IF;
5689 
5690         -- Bug 5438627 : LLCA - Delete the activity record if llca exists. We need to modify the
5691         -- LLCA update logic to preserve the previous record details on AR_ACTIVITY_DETAILS instead
5692         --- of removing it.  Open bug 5397350  exist for this issue.
5693        /*
5694 	delete from ar_activity_details
5695         where cash_receipt_id  = l_cash_receipt_id;
5696 	*/
5697 
5698 -- Bug 7241111 to retain the old application record under activity details
5699 
5700 INSERT INTO AR_ACTIVITY_DETAILS(
5701                                 CASH_RECEIPT_ID,
5702                                 CUSTOMER_TRX_LINE_ID,
5703                                 ALLOCATED_RECEIPT_AMOUNT,
5704                                 AMOUNT,
5705                                 TAX,
5706                                 FREIGHT,
5707                                 CHARGES,
5708                                 LAST_UPDATE_DATE,
5709                                 LAST_UPDATED_BY,
5710                                 LINE_DISCOUNT,
5711                                 TAX_DISCOUNT,
5712                                 FREIGHT_DISCOUNT,
5713                                 LINE_BALANCE,
5714                                 TAX_BALANCE,
5715                                 CREATION_DATE,
5716                                 CREATED_BY,
5717                                 LAST_UPDATE_LOGIN,
5718                                 COMMENTS,
5719                                 APPLY_TO,
5720                                 ATTRIBUTE1,
5721                                 ATTRIBUTE2,
5722                                 ATTRIBUTE3,
5723                                 ATTRIBUTE4,
5724                                 ATTRIBUTE5,
5725                                 ATTRIBUTE6,
5726                                 ATTRIBUTE7,
5727                                 ATTRIBUTE8,
5728                                 ATTRIBUTE9,
5729                                 ATTRIBUTE10,
5730                                 ATTRIBUTE11,
5731                                 ATTRIBUTE12,
5732                                 ATTRIBUTE13,
5733                                 ATTRIBUTE14,
5734                                 ATTRIBUTE15,
5735                                 ATTRIBUTE_CATEGORY,
5736                                 GROUP_ID,
5737                                 REFERENCE1,
5738                                 REFERENCE2,
5739                                 REFERENCE3,
5740                                 REFERENCE4,
5741                                 REFERENCE5,
5742                                 OBJECT_VERSION_NUMBER,
5743                                 CREATED_BY_MODULE,
5744                                 SOURCE_ID,
5745                                 SOURCE_TABLE,
5746                                 LINE_ID,
5747 			        CURRENT_ACTIVITY_FLAG)
5748                         SELECT
5749                                 LLD.CASH_RECEIPT_ID,
5750                                 LLD.CUSTOMER_TRX_LINE_ID,
5751                                 LLD.ALLOCATED_RECEIPT_AMOUNT*-1,
5752                                 LLD.AMOUNT*-1,
5753                                 LLD.TAX*-1,
5754                                 LLD.FREIGHT*-1,
5755                                 LLD.CHARGES*-1,
5756                                 LLD.LAST_UPDATE_DATE,
5757                                 LLD.LAST_UPDATED_BY,
5758                                 LLD.LINE_DISCOUNT,
5759                                 LLD.TAX_DISCOUNT,
5760                                 LLD.FREIGHT_DISCOUNT,
5761                                 LLD.LINE_BALANCE,
5762                                 LLD.TAX_BALANCE,
5763                                 LLD.CREATION_DATE,
5764                                 LLD.CREATED_BY,
5765                                 LLD.LAST_UPDATE_LOGIN,
5766                                 LLD.COMMENTS,
5767                                 LLD.APPLY_TO,
5768                                 LLD.ATTRIBUTE1,
5769                                 LLD.ATTRIBUTE2,
5770                                 LLD.ATTRIBUTE3,
5771                                 LLD.ATTRIBUTE4,
5772                                 LLD.ATTRIBUTE5,
5773                                 LLD.ATTRIBUTE6,
5774                                 LLD.ATTRIBUTE7,
5775                                 LLD.ATTRIBUTE8,
5776                                 LLD.ATTRIBUTE9,
5777                                 LLD.ATTRIBUTE10,
5778                                 LLD.ATTRIBUTE11,
5779                                 LLD.ATTRIBUTE12,
5780                                 LLD.ATTRIBUTE13,
5781                                 LLD.ATTRIBUTE14,
5782                                 LLD.ATTRIBUTE15,
5783                                 LLD.ATTRIBUTE_CATEGORY,
5784                                 LLD.GROUP_ID,
5785                                 LLD.REFERENCE1,
5786                                 LLD.REFERENCE2,
5787                                 LLD.REFERENCE3,
5788                                 LLD.REFERENCE4,
5789                                 LLD.REFERENCE5,
5790                                 LLD.OBJECT_VERSION_NUMBER,
5791                                 LLD.CREATED_BY_MODULE,
5792                                 LLD.SOURCE_ID,
5793                                 LLD.SOURCE_TABLE,
5794                                 ar_Activity_details_s.nextval,
5795                                 'R'
5796                         FROM ar_Activity_details LLD
5797 		        where LLD.cash_receipt_id = l_cash_receipt_id
5798 			and nvl(LLD.CURRENT_ACTIVITY_FLAG, 'Y') = 'Y';
5799 
5800 		   UPDATE ar_Activity_details dtl
5801 		     set CURRENT_ACTIVITY_FLAG = 'N'
5802 			where dtl.cash_receipt_id = l_cash_receipt_id
5803 			and nvl(dtl.CURRENT_ACTIVITY_FLAG, 'Y') = 'Y';
5804 
5805 
5806 
5807      --Call the cover routine for entity handler to reverse the receipt
5808 
5809         reverse_cover(
5810                 l_cash_receipt_id,
5811                 l_reversal_category_code,
5812                 l_reversal_gl_date,
5813                 l_reversal_date,
5814                 l_reversal_reason_code,
5815                 p_reversal_comments,
5816                 l_attribute_rec,
5817                 l_type,
5818                 p_called_from,
5819                 l_rev_cover_return_status
5820                  );
5821         IF l_rev_cover_return_status <> FND_API.G_RET_STS_SUCCESS  THEN
5822 
5823             ROLLBACK TO Reverse_PVT;
5824 
5825              x_return_status := FND_API.G_RET_STS_ERROR ;
5826 
5827              FND_MSG_PUB.Count_And_Get(
5828                                         p_count => x_msg_count,
5829                                         p_data  => x_msg_data
5830                                        );
5831 
5832              IF PG_DEBUG in ('Y', 'C') THEN
5833                 arp_util.debug('Apply: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
5834              END IF;
5835              Return;
5836         ELSE
5837       --Bug 1847350: Added the check on p_commit and then the commit.
5838 
5839        /*--------------------------------+
5840         |   Standard check of p_commit   |
5841         +--------------------------------*/
5842 
5843            IF FND_API.To_Boolean( p_commit )
5844             THEN
5845                 IF PG_DEBUG in ('Y', 'C') THEN
5846                    arp_util.debug('Apply: ' || 'committing');
5847                 END IF;
5848                 Commit;
5849            END IF;
5850 
5851 
5852         END IF;
5853 
5854 
5855 EXCEPTION
5856        WHEN FND_API.G_EXC_ERROR THEN
5857 
5858                 IF PG_DEBUG in ('Y', 'C') THEN
5859                    arp_util.debug('Apply: ' || SQLCODE, G_MSG_ERROR);
5860                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
5861                 END IF;
5862 
5863                 ROLLBACK TO Reverse_PVT;
5864                 x_return_status := FND_API.G_RET_STS_ERROR ;
5865 
5866              --   Display_Parameters;
5867 
5868                 FND_MSG_PUB.Count_And_Get( p_count       =>      x_msg_count,
5869                                            p_data        =>      x_msg_data
5870                                          );
5871 
5872         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5873 
5874                 IF PG_DEBUG in ('Y', 'C') THEN
5875                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
5876                 END IF;
5877                 ROLLBACK TO Reverse_PVT;
5878                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5879 
5880                --  Display_Parameters;
5881 
5882                 FND_MSG_PUB.Count_And_Get( p_count       =>      x_msg_count,
5883                                            p_data        =>      x_msg_data
5884                                          );
5885 
5886         WHEN OTHERS THEN
5887 
5888                /*-------------------------------------------------------+
5889                 |  Handle application errors that result from trapable  |
5890                 |  error conditions. The error messages have already    |
5891                 |  been put on the error stack.                         |
5892                 +-------------------------------------------------------*/
5893 
5894                 IF (SQLCODE = -20001)
5895                 THEN
5896 
5897                       ROLLBACK TO Reverse_PVT;
5898 
5899 
5900                       x_return_status := FND_API.G_RET_STS_ERROR ;
5901                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
5902                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','REVERSE : '||SQLERRM);
5903                       FND_MSG_PUB.Add;
5904 
5905                       --If only one error message on the stack,
5906                       --retrive it
5907 
5908                      FND_MSG_PUB.Count_And_Get( p_count  =>  x_msg_count,
5909                                                  p_data   => x_msg_data
5910                                                 );
5911 
5912                       RETURN;
5913 
5914                 ELSE
5915                      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5916                      FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
5917                      FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','REVERSE : '||SQLERRM);
5918                      FND_MSG_PUB.Add;
5919                 END IF;
5920 
5921                 IF PG_DEBUG in ('Y', 'C') THEN
5922                    arp_util.debug('Apply: ' || SQLCODE, G_MSG_ERROR);
5923                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
5924                 END IF;
5925 
5926                 ROLLBACK TO Reverse_PVT;
5927 
5928                 IF      FND_MSG_PUB.Check_Msg_Level
5929                 THEN
5930                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
5931                                         l_api_name
5932                                        );
5933                 END IF;
5934 
5935              --   Display_Parameters;
5936 
5937                 FND_MSG_PUB.Count_And_Get( p_count       =>      x_msg_count,
5938                                            p_data        =>      x_msg_data
5939                                          );
5940 
5941 
5942 END reverse;
5943 
5944 PROCEDURE Apply_on_account(
5945 -- Standard API parameters.
5946       p_api_version      IN  NUMBER,
5947       p_init_msg_list    IN  VARCHAR2 := FND_API.G_FALSE,
5948       p_commit           IN  VARCHAR2 := FND_API.G_FALSE,
5949       p_validation_level IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
5950       x_return_status    OUT NOCOPY VARCHAR2,
5951       x_msg_count        OUT NOCOPY NUMBER,
5952       x_msg_data         OUT NOCOPY VARCHAR2,
5953   --  Receipt application parameters.
5954       p_cash_receipt_id         IN ar_cash_receipts.cash_receipt_id%TYPE DEFAULT NULL,
5955       p_receipt_number          IN ar_cash_receipts.receipt_number%TYPE DEFAULT NULL,
5956       p_amount_applied          IN ar_receivable_applications.amount_applied%TYPE DEFAULT NULL,
5957       p_apply_date              IN ar_receivable_applications.apply_date%TYPE DEFAULT NULL,
5958       p_apply_gl_date                 IN ar_receivable_applications.gl_date%TYPE DEFAULT NULL,
5959 	  p_ussgl_transaction_code  IN ar_receivable_applications.ussgl_transaction_code%TYPE DEFAULT NULL,
5960       p_attribute_rec      IN attribute_rec_type DEFAULT attribute_rec_const,
5961 	 -- ******* Global Flexfield parameters *******
5962       p_global_attribute_rec IN global_attribute_rec_type DEFAULT global_attribute_rec_const,
5963       p_comments                IN ar_receivable_applications.comments%TYPE DEFAULT NULL,
5964       p_application_ref_num IN ar_receivable_applications.application_ref_num%TYPE,
5965       p_secondary_application_ref_id IN ar_receivable_applications.secondary_application_ref_id%TYPE,
5966       p_customer_reference IN ar_receivable_applications.customer_reference%TYPE,
5967       p_called_from IN VARCHAR2,
5968       p_customer_reason IN ar_receivable_applications.customer_reason%TYPE,
5969       p_secondary_app_ref_type IN
5970                 ar_receivable_applications.secondary_application_ref_type%TYPE := null,
5971       p_secondary_app_ref_num IN
5972                 ar_receivable_applications.secondary_application_ref_num%TYPE := null,
5973       p_org_id             IN NUMBER  DEFAULT NULL
5974 	  ) IS
5975  l_api_name       CONSTANT VARCHAR2(20) := 'Apply_on_acount';
5976  l_api_version    CONSTANT NUMBER       := 1.0;
5977  l_cash_receipt_id NUMBER(15);
5978  l_amount_applied  NUMBER;
5979  l_apply_date      DATE;
5980  l_apply_gl_date   DATE;
5981  l_cr_gl_date      DATE;
5982  l_default_return_status  VARCHAR2(1);
5983  l_validation_return_status  VARCHAR2(1);
5984  l_id_conv_return_status  VARCHAR2(1);
5985  l_cr_unapp_amount  NUMBER;
5986  ln_rec_application_id  NUMBER;
5987  l_cr_date   DATE;
5988  l_cr_payment_schedule_id NUMBER;
5989  l_dflex_val_return_status  VARCHAR2(1);
5990  l_attribute_rec           attribute_rec_type;
5991  l_cr_currency_code     VARCHAR2(15);
5992  l_org_return_status VARCHAR2(1);
5993  l_org_id                           NUMBER;
5994  BEGIN
5995 
5996        /*------------------------------------+
5997         |   Standard start of API savepoint  |
5998         +------------------------------------*/
5999 
6000       SAVEPOINT Apply_on_ac_PVT;
6001 
6002        /*--------------------------------------------------+
6003         |   Standard call to check for call compatibility  |
6004         +--------------------------------------------------*/
6005 
6006         IF NOT FND_API.Compatible_API_Call(
6007                                             l_api_version,
6008                                             p_api_version,
6009                                             l_api_name,
6010                                             G_PKG_NAME
6011                                           )
6012         THEN
6013               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6014         END IF;
6015 
6016        /*--------------------------------------------------------------+
6017         |   Initialize message list if p_init_msg_list is set to TRUE  |
6018         +--------------------------------------------------------------*/
6019 
6020         IF FND_API.to_Boolean( p_init_msg_list )
6021           THEN
6022               FND_MSG_PUB.initialize;
6023         END IF;
6024 
6025         IF PG_DEBUG in ('Y', 'C') THEN
6026            arp_util.debug('Apply_on_account()+ ');
6027         END IF;
6028        /*-----------------------------------------+
6029         |   Initialize return status to SUCCESS   |
6030         +-----------------------------------------*/
6031 
6032         x_return_status := FND_API.G_RET_STS_SUCCESS;
6033 
6034 
6035 
6036 
6037 /* SSA change */
6038        l_org_id            := p_org_id;
6039        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
6040        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
6041                                                 p_return_status =>l_org_return_status);
6042  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6043        x_return_status := FND_API.G_RET_STS_ERROR;
6044  ELSE
6045 
6046 
6047         /*-------------------------------------------------+
6048          | Initialize the profile option package variables |
6049          +-------------------------------------------------*/
6050 
6051            initialize_profile_globals;
6052 
6053 
6054 
6055        /*---------------------------------------------+
6056         |   ========== Start of API Body ==========   |
6057         +---------------------------------------------*/
6058 
6059      l_cash_receipt_id := p_cash_receipt_id;
6060      l_amount_applied  := p_amount_applied;
6061      l_apply_date      := trunc(p_apply_date);
6062      l_apply_gl_date   := trunc(p_apply_gl_date);
6063      l_attribute_rec   := p_attribute_rec;
6064 
6065           /*-----------------------+
6066            |                       |
6067            |ID TO VALUE CONVERSION |
6068            |                       |
6069            +-----------------------*/
6070 
6071    ar_receipt_lib_pvt.Default_cash_receipt_id(
6072                               l_cash_receipt_id ,
6073                               p_receipt_number ,
6074                               l_id_conv_return_status
6075                                );
6076 
6077       IF PG_DEBUG in ('Y', 'C') THEN
6078          arp_util.debug('Apply: ' || 'Defaulting Ids Return_status = '||l_id_conv_return_status);
6079       END IF;
6080           /*---------------------+
6081            |                     |
6082            |    DEFAULTING       |
6083            |                     |
6084            +---------------------*/
6085 
6086       ar_receipt_lib_pvt.Default_on_ac_app_info(
6087                                   l_cash_receipt_id,
6088                                   l_cr_gl_date,
6089                                   l_cr_unapp_amount,
6090                                   l_cr_date,
6091                                   l_cr_payment_schedule_id,
6092                                   l_amount_applied,
6093                                   l_apply_gl_date,
6094                                   l_apply_date,
6095                                   l_cr_currency_code,
6096                                   l_default_return_status
6097                                   );
6098 arp_util.debug('after ar_receipt_lib_pvt.default_on_ac_app_info');
6099           /*---------------------+
6100            |                     |
6101            |    VALIDATION       |
6102            |                     |
6103            +---------------------*/
6104 
6105       ar_receipt_val_pvt.validate_on_ac_app(
6106                               l_cash_receipt_id,
6107                               l_cr_gl_date,
6108                               l_cr_unapp_amount,
6109                               l_cr_date,
6110                               l_cr_payment_schedule_id,
6111                               l_amount_applied,
6112                               l_apply_gl_date,
6113                               l_apply_date,
6114                               l_validation_return_status,
6115                               NULL,
6116 			      p_called_from
6117                                );
6118 arp_util.debug('after ar_receipt_lib_pvt.validate_on_ac_info');
6119 
6120         IF PG_DEBUG in ('Y', 'C') THEN
6121            arp_util.debug('Apply: ' || 'Validation return status :'||l_validation_return_status);
6122         END IF;
6123 
6124          --validate and default the flexfields
6125         ar_receipt_lib_pvt.Validate_Desc_Flexfield(
6126                                         l_attribute_rec,
6127                                         'AR_RECEIVABLE_APPLICATIONS',
6128                                         l_dflex_val_return_status
6129                                                 );
6130 
6131       IF PG_DEBUG in ('Y', 'C') THEN
6132          arp_util.debug('Apply: ' || '*****DUMPING ALL THE ENTITY HANDLER PARAMETERS  ***');
6133          arp_util.debug('Apply: ' || 'l_cr_payment_schedule_id : '||to_char(l_cr_payment_schedule_id));
6134          arp_util.debug('Apply: ' || 'l_amount_applied : '||to_char(l_amount_applied));
6135          arp_util.debug('l_apply_date : '||to_char(l_apply_date,'DD-MON-YY'));
6136          arp_util.debug('l_apply_gl_date : '||to_char(l_apply_gl_date,'DD-MON-YY'));
6137       END IF;
6138 END IF;
6139 
6140 
6141       IF l_validation_return_status <> FND_API.G_RET_STS_SUCCESS  OR
6142          l_default_return_status <> FND_API.G_RET_STS_SUCCESS OR
6143          l_id_conv_return_status <> FND_API.G_RET_STS_SUCCESS OR
6144          l_dflex_val_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6145 
6146           x_return_status :=  FND_API.G_RET_STS_ERROR;
6147       END IF;
6148 
6149       IF x_return_status <> FND_API.G_RET_STS_SUCCESS
6150          THEN
6151 
6152             ROLLBACK TO Apply_on_ac_PVT;
6153 
6154              x_return_status := FND_API.G_RET_STS_ERROR ;
6155 
6156              FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6157                                         p_count => x_msg_count,
6158                                         p_data  => x_msg_data
6159                                        );
6160 
6161              IF PG_DEBUG in ('Y', 'C') THEN
6162                 arp_util.debug('Apply: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
6163              END IF;
6164              Return;
6165        END IF;
6166 
6167        --lock the receipt before calling the entity handler
6168        arp_cash_receipts_pkg.nowaitlock_p(p_cr_id => l_cash_receipt_id);
6169 
6170        --call the entity handler
6171     BEGIN
6172 
6173        arp_process_application.on_account_receipts (
6174                                   p_receipt_ps_id   => l_cr_payment_schedule_id,
6175                                   p_amount_applied  => l_amount_applied,
6176                                   p_apply_date      => l_apply_date,
6177                                   p_gl_date         => l_apply_gl_date,
6178                                   p_comments         => p_comments,
6179                                   p_ussgl_transaction_code => p_ussgl_transaction_code,
6180                                   p_attribute_category=> l_attribute_rec.attribute_category,
6181                                   p_attribute1        => l_attribute_rec.attribute1,
6182                                   p_attribute2        => l_attribute_rec.attribute2,
6183                                   p_attribute3        => l_attribute_rec.attribute3,
6184                                   p_attribute4        => l_attribute_rec.attribute4,
6185                                   p_attribute5        => l_attribute_rec.attribute5,
6186                                   p_attribute6        => l_attribute_rec.attribute6,
6187                                   p_attribute7        => l_attribute_rec.attribute7,
6188                                   p_attribute8        => l_attribute_rec.attribute8,
6189                                   p_attribute9        => l_attribute_rec.attribute9,
6190                                   p_attribute10       => l_attribute_rec.attribute10,
6191                                   p_attribute11       => l_attribute_rec.attribute11,
6192                                   p_attribute12       => l_attribute_rec.attribute12,
6193                                   p_attribute13       => l_attribute_rec.attribute13,
6194                                   p_attribute14       => l_attribute_rec.attribute14,
6195                                   p_attribute15       => l_attribute_rec.attribute15,
6196                                   p_global_attribute1 => p_global_attribute_rec.global_attribute1,
6197                                   p_global_attribute2 => p_global_attribute_rec.global_attribute2,
6198                                   p_global_attribute3 => p_global_attribute_rec.global_attribute3,
6199                                   p_global_attribute4 => p_global_attribute_rec.global_attribute4,
6200                                   p_global_attribute5 => p_global_attribute_rec.global_attribute5,
6201                                   p_global_attribute6 => p_global_attribute_rec.global_attribute6,
6202                                   p_global_attribute7 => p_global_attribute_rec.global_attribute7,
6203                                   p_global_attribute8 => p_global_attribute_rec.global_attribute8,
6204                                   p_global_attribute9 => p_global_attribute_rec.global_attribute9,
6205                                   p_global_attribute10 => p_global_attribute_rec.global_attribute10,
6206                                   p_global_attribute11 => p_global_attribute_rec.global_attribute11,
6207                                   p_global_attribute12 => p_global_attribute_rec.global_attribute12,
6208                                   p_global_attribute13 => p_global_attribute_rec.global_attribute13,
6209                                   p_global_attribute14 => p_global_attribute_rec.global_attribute14,
6210                                   p_global_attribute15 => p_global_attribute_rec.global_attribute15,
6211                                   p_global_attribute16 => p_global_attribute_rec.global_attribute16,
6212                                   p_global_attribute17 => p_global_attribute_rec.global_attribute17,
6213                                   p_global_attribute18 => p_global_attribute_rec.global_attribute18,
6214                                   p_global_attribute19 => p_global_attribute_rec.global_attribute19,
6215                                   p_global_attribute20 => p_global_attribute_rec.global_attribute20,
6216                                   p_global_attribute_category => p_global_attribute_rec.global_attribute_category,
6217                                   p_module_name         => 'RAPI',
6218                                   p_module_version      => p_api_version,
6219                                   -- *** OUT NOCOPY
6220                                   p_out_rec_application_id => ln_rec_application_id,
6221                                   p_application_ref_num => p_application_ref_num,
6222                                   p_secondary_application_ref_id => p_secondary_application_ref_id,
6223                                   p_secondary_app_ref_type => p_secondary_app_ref_type,
6224                                   p_secondary_app_ref_num => p_secondary_app_ref_num,
6225                                   p_customer_reference => p_customer_reference,
6226                                   p_customer_reason => p_customer_reason
6227                                    );
6228 
6229       --Bug 3628401
6230       -- Assign receivable_application_id to package global variable
6231       -- So, it can used to perform follow on operation on given application
6232       g_apply_on_account_out_rec.receivable_application_id :=
6233                                               ln_rec_application_id;
6234 
6235      EXCEPTION
6236        WHEN OTHERS THEN
6237 
6238                /*-------------------------------------------------------+
6239                 |  Handle application errors that result from trapable  |
6240                 |  error conditions. The error messages have already    |
6241                 |  been put on the error stack.                         |
6242                 +-------------------------------------------------------*/
6243 
6244                 IF (SQLCODE = -20001)
6245                 THEN
6246                      ROLLBACK TO Apply_on_ac_PVT;
6247 
6248                       --  Display_Parameters
6249 
6250                       x_return_status := FND_API.G_RET_STS_ERROR ;
6251                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
6252                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.ON_ACCOUNT_RECEIPTS : '||SQLERRM);
6253                        FND_MSG_PUB.Add;
6254 
6255                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
6256                                                   p_count  =>  x_msg_count,
6257                                                   p_data   => x_msg_data
6258                                                 );
6259                       RETURN;
6260                 ELSE
6261                    RAISE;
6262                 END IF;
6263      END;
6264        /*--------------------------------+
6265         |   Standard check of p_commit   |
6266         +--------------------------------*/
6267 
6268         IF FND_API.To_Boolean( p_commit )
6269         THEN
6270             IF PG_DEBUG in ('Y', 'C') THEN
6271                arp_util.debug('Apply: ' || 'committing');
6272             END IF;
6273               Commit;
6274         END IF;
6275         IF PG_DEBUG in ('Y', 'C') THEN
6276            arp_util.debug('Apply_on_account ()- ');
6277         END IF;
6278 EXCEPTION
6279        WHEN FND_API.G_EXC_ERROR THEN
6280 
6281                 IF PG_DEBUG in ('Y', 'C') THEN
6282                    arp_util.debug('Apply: ' || SQLCODE, G_MSG_ERROR);
6283                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
6284                 END IF;
6285 
6286                 ROLLBACK TO Apply_on_ac_PVT;
6287                 x_return_status := FND_API.G_RET_STS_ERROR ;
6288 
6289                -- Display_Parameters;
6290 
6291                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6292                                            p_count       =>      x_msg_count,
6293                                            p_data        =>      x_msg_data
6294                                          );
6295 
6296         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6297 
6298                 IF PG_DEBUG in ('Y', 'C') THEN
6299                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
6300                 END IF;
6301                 ROLLBACK TO Apply_on_ac_PVT;
6302                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6303 
6304                --  Display_Parameters;
6305 
6306                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6307                                            p_count       =>      x_msg_count,
6308                                            p_data        =>      x_msg_data
6309                                          );
6310 
6311         WHEN OTHERS THEN
6312 
6313                /*-------------------------------------------------------+
6314                 |  Handle application errors that result from trapable  |
6315                 |  error conditions. The error messages have already    |
6316                 |  been put on the error stack.                         |
6317                 +-------------------------------------------------------*/
6318 
6319                 IF (SQLCODE = -20001)
6320                 THEN
6321 
6322                       ROLLBACK TO Apply_on_ac_PVT;
6323 
6324                       --If only one error message on the stack,
6325                       --retrive it
6326 
6327                       x_return_status := FND_API.G_RET_STS_ERROR ;
6328                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
6329                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','APPLY_ON_ACCOUNT : '||SQLERRM);
6330                       FND_MSG_PUB.Add;
6331 
6332                       --If only one error message on the stack,
6333                       --retrive it
6334                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6335                                                  p_count  =>  x_msg_count,
6336                                                  p_data   => x_msg_data
6337                                                 );
6338 
6339                       RETURN;
6340 
6341                 ELSE
6342                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6343                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
6344                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','APPLY_ON_ACCOUNT : '||SQLERRM);
6345                       FND_MSG_PUB.Add;
6346                 END IF;
6347 
6348                 IF PG_DEBUG in ('Y', 'C') THEN
6349                    arp_util.debug('Apply: ' || SQLCODE, G_MSG_ERROR);
6350                    arp_util.debug('Apply: ' || SQLERRM, G_MSG_ERROR);
6351                 END IF;
6352 
6353                 ROLLBACK TO Apply_on_ac_PVT;
6354 
6355                 IF      FND_MSG_PUB.Check_Msg_Level
6356                 THEN
6357                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
6358                                         l_api_name
6359                                        );
6360                 END IF;
6361 
6362              --   Display_Parameters;
6363 
6364                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6365                                            p_count       =>      x_msg_count,
6366                                            p_data        =>      x_msg_data
6367                                          );
6368 END Apply_on_account;
6369 
6370 PROCEDURE Unapply_on_account(
6371     -- Standard API parameters.
6372       p_api_version      IN  NUMBER,
6373       p_init_msg_list    IN  VARCHAR2 := FND_API.G_FALSE,
6374       p_commit           IN  VARCHAR2 := FND_API.G_FALSE,
6375       p_validation_level IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
6376       x_return_status    OUT NOCOPY VARCHAR2 ,
6377       x_msg_count        OUT NOCOPY NUMBER ,
6378       x_msg_data         OUT NOCOPY VARCHAR2 ,
6379    -- *** Receipt Info. parameters *****
6380       p_receipt_number   IN  ar_cash_receipts.receipt_number%TYPE DEFAULT NULL,
6381       p_cash_receipt_id  IN  ar_cash_receipts.cash_receipt_id%TYPE DEFAULT NULL,
6382       p_receivable_application_id   IN ar_receivable_applications.receivable_application_id%TYPE DEFAULT NULL,
6383       p_reversal_gl_date IN ar_receivable_applications.reversal_gl_date%TYPE DEFAULT NULL,
6384       p_org_id             IN NUMBER  DEFAULT NULL
6385       ) IS
6386 l_api_name       CONSTANT VARCHAR2(20) := 'Unapply_on_account';
6387 l_api_version    CONSTANT NUMBER       := 1.0;
6388 l_customer_trx_id               NUMBER;
6389 l_cash_receipt_id               NUMBER;
6390 l_receivable_application_id     NUMBER;
6391 l_reversal_gl_date              DATE;
6392 l_apply_gl_date                 DATE;
6393 l_receipt_gl_date               DATE;
6394 l_def_return_status             VARCHAR2(1);
6395 l_val_return_status             VARCHAR2(1);
6396 l_bal_due_remaining             NUMBER;
6397 l_org_return_status VARCHAR2(1);
6398 l_org_id                           NUMBER;
6399 l_cr_unapp_amt                  NUMBER  ; /* Bug fix 3569640 */
6400 BEGIN
6401        /*------------------------------------+
6402         |   Standard start of API savepoint  |
6403         +------------------------------------*/
6404 
6405         SAVEPOINT Unapply_on_ac_PVT;
6406 
6407        /*--------------------------------------------------+
6408         |   Standard call to check for call compatibility  |
6409         +--------------------------------------------------*/
6410 
6411         IF NOT FND_API.Compatible_API_Call(
6412                                             l_api_version,
6413                                             p_api_version,
6414                                             l_api_name,
6415                                             G_PKG_NAME
6416                                           )
6417         THEN
6418               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6419         END IF;
6420 
6421        /*--------------------------------------------------------------+
6422         |   Initialize message list if p_init_msg_list is set to TRUE  |
6423         +--------------------------------------------------------------*/
6424 
6425         IF FND_API.to_Boolean( p_init_msg_list )
6426           THEN
6427               FND_MSG_PUB.initialize;
6428         END IF;
6429 
6430         IF PG_DEBUG in ('Y', 'C') THEN
6431            arp_util.debug('Unapply_on_account: ' || 'ar_receipt_api.Unapply_on_ac()+ ');
6432         END IF;
6433        /*-----------------------------------------+
6434         |   Initialize return status to SUCCESS   |
6435         +-----------------------------------------*/
6436 
6437         x_return_status := FND_API.G_RET_STS_SUCCESS;
6438 
6439 /* SSA change */
6440        l_org_id            := p_org_id;
6441        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
6442        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
6443                                                 p_return_status =>l_org_return_status);
6444  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6445        x_return_status := FND_API.G_RET_STS_ERROR;
6446  ELSE
6447 
6448         /*-------------------------------------------------+
6449          | Initialize the profile option package variables |
6450          +-------------------------------------------------*/
6451 
6452            initialize_profile_globals;
6453 
6454 
6455        /*---------------------------------------------+
6456         |   ========== Start of API Body ==========   |
6457         +---------------------------------------------*/
6458 
6459         --Assign IN parameter values to local variables
6460         --which are also used as assignment targets.
6461 
6462          l_cash_receipt_id   := p_cash_receipt_id;
6463          l_receivable_application_id  := p_receivable_application_id;
6464          l_reversal_gl_date := trunc(p_reversal_gl_date);
6465 
6466 
6467         /*------------------------------------------------+
6468          |  Derive the id's for the entered values.       |
6469          |  If both the values and the ids are specified, |
6470          |  the id's superceed the values                 |
6471          +------------------------------------------------*/
6472 
6473          ar_receipt_lib_pvt.derive_unapp_on_ac_ids(
6474                          p_receipt_number   ,
6475                          l_cash_receipt_id  ,
6476                          l_receivable_application_id ,
6477                          l_apply_gl_date     ,
6478                          l_def_return_status
6479                                );
6480          ar_receipt_lib_pvt.default_unapp_on_ac_act_info(
6481                          l_receivable_application_id ,
6482                          l_apply_gl_date            ,
6483                          l_cash_receipt_id          ,
6484                          l_reversal_gl_date         ,
6485                          l_receipt_gl_date
6486                           );
6487 
6488          ar_receipt_val_pvt.validate_unapp_on_ac_act_info(
6489                                       l_receipt_gl_date,
6490                                       l_receivable_application_id,
6491                                       l_reversal_gl_date,
6492                                       l_apply_gl_date,
6493                                       l_cr_unapp_amt, /* Bug fix 3569640 */
6494                                       l_val_return_status);
6495 
6496          IF PG_DEBUG in ('Y', 'C') THEN
6497             arp_util.debug('Unapply_on_account: ' || 'validation return status :'||l_val_return_status);
6498          END IF;
6499 END IF;
6500 
6501         IF l_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
6502            l_def_return_status <> FND_API.G_RET_STS_SUCCESS  THEN
6503            x_return_status := FND_API.G_RET_STS_ERROR;
6504         END IF;
6505 
6506         IF x_return_status <> FND_API.G_RET_STS_SUCCESS
6507          THEN
6508 
6509             ROLLBACK TO Unapply_on_ac_PVT;
6510 
6511              x_return_status := FND_API.G_RET_STS_ERROR ;
6512 
6513              FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6514                                         p_count => x_msg_count,
6515                                         p_data  => x_msg_data
6516                                        );
6517 
6518              IF PG_DEBUG in ('Y', 'C') THEN
6519                 arp_util.debug('Unapply_on_account: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
6520              END IF;
6521              Return;
6522         END IF;
6523 
6524         IF PG_DEBUG in ('Y', 'C') THEN
6525            arp_util.debug('Unapply_on_account: ' || '*******DUMP THE INPUT PARAMETERS ********');
6526            arp_util.debug('Unapply_on_account: ' || 'l_receivable_application_id :'||to_char(l_receivable_application_id));
6527            arp_util.debug('Unapply_on_account: ' || 'l_reversal_gl_date :'||to_char(l_reversal_gl_date,'DD-MON-YY'));
6528         END IF;
6529 
6530        --lock the receipt before calling the entity handler
6531        arp_cash_receipts_pkg.nowaitlock_p(p_cr_id => l_cash_receipt_id);
6532 
6533 
6534         --call the entity handler.
6535        BEGIN
6536           arp_process_application.reverse(
6537                                 l_receivable_application_id,
6538                                 l_reversal_gl_date,
6539                                 trunc(sysdate),
6540                                 'RAPI',
6541                                 p_api_version,
6542                                 l_bal_due_remaining );
6543        EXCEPTION
6544           WHEN OTHERS THEN
6545 
6546                /*-------------------------------------------------------+
6547                 |  Handle application errors that result from trapable  |
6548                 |  error conditions. The error messages have already    |
6549                 |  been put on the error stack.                         |
6550                 +-------------------------------------------------------*/
6551 
6552                 IF (SQLCODE = -20001)
6553                 THEN
6554                      ROLLBACK TO Unapply_on_ac_PVT;
6555 
6556                       --  Display_Parameters;
6557                       x_return_status := FND_API.G_RET_STS_ERROR ;
6558                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
6559                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.REVERSE : '||SQLERRM);
6560                       FND_MSG_PUB.Add;
6561 
6562                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
6563                                                   p_count  =>  x_msg_count,
6564                                                   p_data   => x_msg_data
6565                                                 );
6566                       RETURN;
6567                 ELSE
6568                    RAISE;
6569                 END IF;
6570        END;
6571 
6572        /*--------------------------------+
6573         |   Standard check of p_commit   |
6574         +--------------------------------*/
6575 
6576         IF FND_API.To_Boolean( p_commit )
6577         THEN
6578             IF PG_DEBUG in ('Y', 'C') THEN
6579                arp_util.debug('Unapply_on_account: ' || 'committing');
6580             END IF;
6581               Commit;
6582         END IF;
6583 
6584         IF PG_DEBUG in ('Y', 'C') THEN
6585            arp_util.debug('Unapply_on_account: ' || 'ar_receipt_api.Unapply_on_ac ()- ');
6586         END IF;
6587 
6588 
6589 EXCEPTION
6590        WHEN FND_API.G_EXC_ERROR THEN
6591 
6592                 IF PG_DEBUG in ('Y', 'C') THEN
6593                    arp_util.debug('Unapply_on_account: ' || SQLCODE, G_MSG_ERROR);
6594                    arp_util.debug('Unapply_on_account: ' || SQLERRM, G_MSG_ERROR);
6595                 END IF;
6596 
6597                 ROLLBACK TO Unapply_on_ac_PVT;
6598                 x_return_status := FND_API.G_RET_STS_ERROR ;
6599 
6600               --  Display_Parameters;
6601 
6602                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6603                                            p_count       =>      x_msg_count,
6604                                            p_data        =>      x_msg_data
6605                                          );
6606 
6607         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6608 
6609                 IF PG_DEBUG in ('Y', 'C') THEN
6610                    arp_util.debug('Unapply_on_account: ' || SQLERRM, G_MSG_ERROR);
6611                 END IF;
6612                 ROLLBACK TO Unapply_on_ac_PVT;
6613                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6614 
6615                --  Display_Parameters;
6616 
6617                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6618                                            p_count       =>      x_msg_count,
6619                                            p_data        =>      x_msg_data
6620                                          );
6621 
6622         WHEN OTHERS THEN
6623 
6624                /*-------------------------------------------------------+
6625                 |  Handle application errors that result from trapable  |
6626                 |  error conditions. The error messages have already    |
6627                 |  been put on the error stack.                         |
6628                 +-------------------------------------------------------*/
6629 
6630                 IF (SQLCODE = -20001)
6631                 THEN
6632 
6633                       ROLLBACK TO Unapply_on_ac_PVT;
6634 
6635                       --If only one error message on the stack,
6636                       --retrive it
6637 
6638                       x_return_status := FND_API.G_RET_STS_ERROR ;
6639                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
6640                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','UNAPPLY_ON_ACCOUNT : '||SQLERRM);
6641                       FND_MSG_PUB.Add;
6642                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6643                                                  p_count  =>  x_msg_count,
6644                                                  p_data   => x_msg_data
6645                                                 );
6646 
6647                       RETURN;
6648 
6649                 ELSE
6650                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6651                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
6652                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','UNAPPLY_ON_ACCOUNT : '||SQLERRM);
6653                       FND_MSG_PUB.Add;
6654                 END IF;
6655 
6656                 IF PG_DEBUG in ('Y', 'C') THEN
6657                    arp_util.debug('Unapply_on_account: ' || SQLCODE, G_MSG_ERROR);
6658                    arp_util.debug('Unapply_on_account: ' || SQLERRM, G_MSG_ERROR);
6659                 END IF;
6660 
6661                 ROLLBACK TO Unapply_on_ac_PVT;
6662 
6663                 IF      FND_MSG_PUB.Check_Msg_Level
6664                 THEN
6665                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
6666                                         l_api_name
6667                                        );
6668                 END IF;
6669 
6670              --   Display_Parameters;
6671 
6672                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6673                                            p_count       =>      x_msg_count,
6674                                            p_data        =>      x_msg_data
6675                                          );
6676 END Unapply_on_account;
6677 
6678 PROCEDURE Unapply_other_account(
6679     -- Standard API parameters.
6680       p_api_version      IN  NUMBER,
6681       p_init_msg_list    IN  VARCHAR2 := FND_API.G_FALSE,
6682       p_commit           IN  VARCHAR2 := FND_API.G_FALSE,
6683       p_validation_level IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
6684       x_return_status    OUT NOCOPY VARCHAR2 ,
6685       x_msg_count        OUT NOCOPY NUMBER ,
6686       x_msg_data         OUT NOCOPY VARCHAR2 ,
6687    -- *** Receipt Info. parameters *****
6688       p_receipt_number   IN  ar_cash_receipts.receipt_number%TYPE,
6689       p_cash_receipt_id  IN  ar_cash_receipts.cash_receipt_id%TYPE,
6690       p_receivable_application_id   IN  ar_receivable_applications.receivable_application_id%TYPE,
6691       p_reversal_gl_date IN ar_receivable_applications.reversal_gl_date%TYPE,
6692       p_cancel_claim_flag         IN  VARCHAR2,
6693       p_called_from               IN  VARCHAR2,
6694       p_org_id             IN NUMBER  DEFAULT NULL
6695       ) IS
6696 l_api_name       CONSTANT VARCHAR2(20) := 'Unapply_on_account';
6697 l_api_version    CONSTANT NUMBER       := 1.0;
6698 l_customer_trx_id               NUMBER;
6699 l_cash_receipt_id               NUMBER;
6700 l_receipt_number                ar_cash_receipts.receipt_number%TYPE;
6701 l_receivable_application_id     NUMBER;
6702 l_applied_ps_id                 NUMBER;
6703 l_sec_app_ref_id                NUMBER;
6704 l_amount_applied                NUMBER;
6705 l_reversal_gl_date              DATE;
6706 l_apply_gl_date                 DATE;
6707 l_receipt_gl_date               DATE;
6708 l_def_return_status             VARCHAR2(1);
6709 l_val_return_status             VARCHAR2(1);
6710 l_clm_return_status             VARCHAR2(1);
6711 l_bal_due_remaining             NUMBER;
6712 l_claim_reason_name             VARCHAR2(100);
6713 l_claim_reason_code_id          NUMBER;
6714 l_claim_number                  VARCHAR2(30);
6715 l_org_return_status VARCHAR2(1);
6716 l_org_id                           NUMBER;
6717 l_cr_unapp_amt                  NUMBER; /* Bug fix 3569640 */
6718 BEGIN
6719        /*------------------------------------+
6720         |   Standard start of API savepoint  |
6721         +------------------------------------*/
6722 
6723         SAVEPOINT Unapply_other_ac_PVT;
6724 
6725        /*--------------------------------------------------+
6726         |   Standard call to check for call compatibility  |
6727         +--------------------------------------------------*/
6728 
6729         IF NOT FND_API.Compatible_API_Call(
6730                               p_current_version_number => l_api_version,
6731                               p_caller_version_number  => p_api_version,
6732                               p_api_name               => l_api_name,
6733                               p_pkg_name               => G_PKG_NAME
6734                               )
6735         THEN
6736               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6737         END IF;
6738 
6739        /*--------------------------------------------------------------+
6740         |   Initialize message list if p_init_msg_list is set to TRUE  |
6741         +--------------------------------------------------------------*/
6742 
6743         IF FND_API.to_Boolean( p_init_msg_list )
6744           THEN
6745               FND_MSG_PUB.initialize;
6746         END IF;
6747 
6748         IF PG_DEBUG in ('Y', 'C') THEN
6749            arp_util.debug('ar_receipt_api.Unapply_other_account()+ ');
6750         END IF;
6751        /*-----------------------------------------+
6752         |   Initialize return status to SUCCESS   |
6753         +-----------------------------------------*/
6754 
6755         x_return_status := FND_API.G_RET_STS_SUCCESS;
6756 
6757 /* SSA change */
6758        l_org_id            := p_org_id;
6759        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
6760        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
6761                                                 p_return_status =>l_org_return_status);
6762  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6763        x_return_status := FND_API.G_RET_STS_ERROR;
6764  ELSE
6765         /*-------------------------------------------------+
6766          | Initialize the profile option package variables |
6767          +-------------------------------------------------*/
6768 
6769            initialize_profile_globals;
6770 
6771 
6772        /*---------------------------------------------+
6773         |   ========== Start of API Body ==========   |
6774         +---------------------------------------------*/
6775 
6776         --Assign IN parameter values to local variables
6777         --which are also used as assignment targets.
6778 
6779          l_cash_receipt_id   := p_cash_receipt_id;
6780          l_receipt_number    := p_receipt_number;
6781          l_receivable_application_id  := p_receivable_application_id;
6782          l_reversal_gl_date := trunc(p_reversal_gl_date);
6783          --At present this routine supports only prepayments
6784          -- Bug 2270879 - updated to allow claims
6785 
6786          SELECT applied_payment_schedule_id
6787               , secondary_application_ref_id
6788               , amount_applied
6789          INTO   l_applied_ps_id
6790               , l_sec_app_ref_id
6791               , l_amount_applied
6792          FROM   ar_receivable_applications
6793          WHERE  receivable_application_id = p_receivable_application_id;
6794 
6795         /*------------------------------------------------+
6796          |  Derive the id's for the entered values.       |
6797          |  If both the values and the ids are specified, |
6798          |  the id's superceed the values                 |
6799          +------------------------------------------------*/
6800 
6801          ar_receipt_lib_pvt.derive_otheraccount_ids(
6802                          p_receipt_number  => p_receipt_number   ,
6803                          p_cash_receipt_id => l_cash_receipt_id  ,
6804                          p_applied_ps_id   => l_applied_ps_id,
6805                          p_receivable_application_id => l_receivable_application_id ,
6806                          p_apply_gl_date             =>  l_apply_gl_date     ,
6807                          p_cr_unapp_amt              =>  l_cr_unapp_amt, /* bug fix 3569640 */
6808                          p_return_status             => l_def_return_status
6809                                );
6810 
6811          ar_receipt_lib_pvt.default_unapp_on_ac_act_info(
6812                          p_receivable_application_id => l_receivable_application_id ,
6813                          p_apply_gl_date             => l_apply_gl_date          ,
6814                          p_cash_receipt_id           => l_cash_receipt_id          ,
6815                          p_reversal_gl_date          => l_reversal_gl_date         ,
6816                          p_receipt_gl_date           => l_receipt_gl_date
6817                           );
6818 
6819          -- Bug 3708728: unapplied amount set to null to prevent validation
6820          -- in the case of a receipt to receipt application
6821 	 -- Bug 3809272 - validation on unapplied also prevented if called
6822 	 -- as part of claim settlement
6823          IF p_called_from IN ('APPLY_OPEN_RECEIPT','TRADE_MANAGEMENT') THEN
6824             l_cr_unapp_amt := NULL;
6825          END IF;
6826          ar_receipt_val_pvt.validate_unapp_on_ac_act_info(
6827                                       p_receipt_gl_date => l_receipt_gl_date,
6828                                       p_receivable_application_id => l_receivable_application_id,
6829                                       p_reversal_gl_date => l_reversal_gl_date,
6830                                       p_apply_gl_date    => l_apply_gl_date,
6831                                       p_cr_unapp_amt     =>  l_cr_unapp_amt, /* bug fix 3569640 */
6832                                       p_return_status    => l_val_return_status);
6833          -- Bug 2270809 , bug 2751910
6834          -- If a claim investigation app, then update the claim.
6835          -- Bug 3708728: update_claim not called if validation errors exist
6836 
6837         IF (l_val_return_status = FND_API.G_RET_STS_SUCCESS AND
6838             l_def_return_status = FND_API.G_RET_STS_SUCCESS AND
6839             l_applied_ps_id = -4 AND
6840              NVL(p_called_from,'RAPI') <> 'TRADE_MANAGEMENT')
6841          THEN
6842            /* Bug 4170060 do not update with claim balance for partial
6843 	      rct to rct applications */
6844            IF (NVL(p_called_from,'RAPI') = 'APPLY_OPEN_RECEIPT' AND
6845 	       NVL(pg_update_claim_amount,0) <> 0) THEN
6846               NULL;
6847 	   ELSE
6848               arp_process_application.update_claim(
6849                 p_claim_id      =>  l_sec_app_ref_id
6850               , p_invoice_ps_id =>  NULL
6851               , p_customer_trx_id => NULL
6852               , p_amount        =>  0
6853               , p_amount_applied => l_amount_applied
6854               , p_apply_date    =>  trunc(SYSDATE)
6855               , p_cash_receipt_id => l_cash_receipt_id
6856               , p_receipt_number => l_receipt_number
6857               , p_action_type   => 'U'
6858               , x_claim_reason_code_id => l_claim_reason_code_id
6859               , x_claim_reason_name => l_claim_reason_name
6860               , x_claim_number => l_claim_number
6861               , x_return_status =>  l_clm_return_status
6862               , x_msg_count     =>  x_msg_count
6863               , x_msg_data      =>  x_msg_data);
6864            END IF;
6865          END IF;
6866 
6867          IF PG_DEBUG in ('Y', 'C') THEN
6868             arp_util.debug('Unapply_other_account: ' || 'validation return status :'||l_val_return_status);
6869          END IF;
6870 END IF;
6871 
6872         IF l_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
6873            l_def_return_status <> FND_API.G_RET_STS_SUCCESS OR
6874            l_clm_return_status <> FND_API.G_RET_STS_SUCCESS  THEN
6875            x_return_status := FND_API.G_RET_STS_ERROR;
6876         END IF;
6877 
6878         IF x_return_status <> FND_API.G_RET_STS_SUCCESS
6879          THEN
6880 
6881             ROLLBACK TO Unapply_other_ac_PVT;
6882 
6883              x_return_status := FND_API.G_RET_STS_ERROR ;
6884 
6885              FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6886                                         p_count => x_msg_count,
6887                                         p_data  => x_msg_data
6888                                        );
6889 
6890              IF PG_DEBUG in ('Y', 'C') THEN
6891                 arp_util.debug('Unapply_other_account: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
6892              END IF;
6893              Return;
6894         END IF;
6895 
6896         IF PG_DEBUG in ('Y', 'C') THEN
6897            arp_util.debug('Unapply_other_account: ' || '*******DUMP THE INPUT PARAMETERS ********');
6898            arp_util.debug('Unapply_other_account: ' || 'l_receivable_application_id :'||to_char(l_receivable_application_id));
6899            arp_util.debug('Unapply_other_account: ' || 'l_reversal_gl_date :'||to_char(l_reversal_gl_date,'DD-MON-YY'));
6900         END IF;
6901 
6902        --lock the receipt before calling the entity handler
6903        arp_cash_receipts_pkg.nowaitlock_p(p_cr_id => l_cash_receipt_id);
6904 
6905 
6906         --call the entity handler.
6907        BEGIN
6908           arp_process_application.reverse(
6909                                 p_ra_id   => l_receivable_application_id,
6910                                 p_reversal_gl_date => l_reversal_gl_date,
6911                                 p_reversal_date => trunc(sysdate),
6912                                 p_module_name => 'RAPI',
6913                                 p_module_version => p_api_version,
6914                                 p_bal_due_remaining => l_bal_due_remaining );
6915        EXCEPTION
6916           WHEN OTHERS THEN
6917 
6918                /*-------------------------------------------------------+
6919                 |  Handle application errors that result from trapable  |
6920                 |  error conditions. The error messages have already    |
6921                 |  been put on the error stack.                         |
6922                 +-------------------------------------------------------*/
6923 
6924                 IF (SQLCODE = -20001)
6925                 THEN
6926                      ROLLBACK TO Unapply_other_ac_PVT;
6927 
6928                       --  Display_Parameters;
6929                       x_return_status := FND_API.G_RET_STS_ERROR ;
6930                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
6931                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.REVERSE : '||SQLERRM);
6932                       FND_MSG_PUB.Add;
6933 
6934                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
6935                                                   p_count  =>  x_msg_count,
6936                                                   p_data   => x_msg_data
6937                                                 );
6938                       RETURN;
6939                 ELSE
6940                    RAISE;
6941                 END IF;
6942        END;
6943 
6944        /*--------------------------------+
6945         |   Standard check of p_commit   |
6946         +--------------------------------*/
6947 
6948         IF FND_API.To_Boolean( p_commit )
6949         THEN
6950             IF PG_DEBUG in ('Y', 'C') THEN
6951                arp_util.debug('Unapply_other_account: ' || 'committing');
6952             END IF;
6953               Commit;
6954         END IF;
6955 
6956         IF PG_DEBUG in ('Y', 'C') THEN
6957            arp_util.debug('ar_receipt_api.Unapply_other_account ()- ');
6958         END IF;
6959 
6960 
6961 EXCEPTION
6962        WHEN FND_API.G_EXC_ERROR THEN
6963 
6964                 IF PG_DEBUG in ('Y', 'C') THEN
6965                    arp_util.debug('Unapply_other_account: ' || SQLCODE, G_MSG_ERROR);
6966                    arp_util.debug('Unapply_other_account: ' || SQLERRM, G_MSG_ERROR);
6967                 END IF;
6968 
6969                 ROLLBACK TO Unapply_other_ac_PVT;
6970                 x_return_status := FND_API.G_RET_STS_ERROR ;
6971 
6972               --  Display_Parameters;
6973 
6974                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6975                                            p_count       =>      x_msg_count,
6976                                            p_data        =>      x_msg_data
6977                                          );
6978 
6979         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6980 
6981                 IF PG_DEBUG in ('Y', 'C') THEN
6982                    arp_util.debug('Unapply_other_account: ' || SQLERRM, G_MSG_ERROR);
6983                 END IF;
6984                 ROLLBACK TO Unapply_other_ac_PVT;
6985                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6986 
6987                --  Display_Parameters;
6988 
6989                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
6990                                            p_count       =>      x_msg_count,
6991                                            p_data        =>      x_msg_data
6992                                          );
6993 
6994         WHEN OTHERS THEN
6995 
6996                /*-------------------------------------------------------+
6997                 |  Handle application errors that result from trapable  |
6998                 |  error conditions. The error messages have already    |
6999                 |  been put on the error stack.                         |
7000                 +-------------------------------------------------------*/
7001 
7002                 IF (SQLCODE = -20001)
7003                 THEN
7004 
7005                       ROLLBACK TO Unapply_other_ac_PVT;
7006 
7007                       --If only one error message on the stack,
7008                       --retrive it
7009 
7010                       x_return_status := FND_API.G_RET_STS_ERROR ;
7011                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
7012                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','UNAPPLY_ON_ACCOUNT : '||SQLERRM);
7013                       FND_MSG_PUB.Add;
7014                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7015                                                  p_count  =>  x_msg_count,
7016                                                  p_data   => x_msg_data
7017                                                 );
7018 
7019                       RETURN;
7020 
7021                 ELSE
7022                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7023                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
7024                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','UNAPPLY_OTHER_ACCOUNT : '||SQLERRM);
7025                       FND_MSG_PUB.Add;
7026                 END IF;
7027 
7028                 IF PG_DEBUG in ('Y', 'C') THEN
7029                    arp_util.debug('Unapply_other_account: ' || SQLCODE, G_MSG_ERROR);
7030                    arp_util.debug('Unapply_other_account: ' || SQLERRM, G_MSG_ERROR);
7031                 END IF;
7032 
7033                 ROLLBACK TO Unapply_other_ac_PVT;
7034 
7035                 IF      FND_MSG_PUB.Check_Msg_Level
7036                 THEN
7037                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
7038                                         l_api_name
7039                                        );
7040                 END IF;
7041 
7042              --   Display_Parameters;
7043 
7044                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7045                                            p_count       =>      x_msg_count,
7046                                            p_data        =>      x_msg_data
7047                                          );
7048 END Unapply_other_account;
7049 
7050 PROCEDURE Activity_application(
7051 -- Standard API parameters.
7052       p_api_version                   IN  NUMBER,
7053       p_init_msg_list                 IN  VARCHAR2 := FND_API.G_FALSE,
7054       p_commit                        IN  VARCHAR2 := FND_API.G_FALSE,
7055       p_validation_level              IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
7056       x_return_status                 OUT NOCOPY VARCHAR2,
7057       x_msg_count                     OUT NOCOPY NUMBER,
7058       x_msg_data                      OUT NOCOPY VARCHAR2,
7059   --  Receipt application parameters.
7060       p_cash_receipt_id               IN ar_cash_receipts.cash_receipt_id%TYPE DEFAULT NULL,
7061       p_receipt_number                IN ar_cash_receipts.receipt_number%TYPE DEFAULT NULL,
7062       p_amount_applied                IN ar_receivable_applications.amount_applied%TYPE DEFAULT NULL,
7063       p_applied_payment_schedule_id   IN ar_payment_schedules.payment_schedule_id%TYPE, --this has no default
7064       p_link_to_customer_trx_id       IN ra_customer_trx.customer_trx_id%TYPE DEFAULT NULL,
7065       p_receivables_trx_id            IN ar_receivable_applications.receivables_trx_id%TYPE,
7066       p_apply_date                    IN ar_receivable_applications.apply_date%TYPE DEFAULT NULL,
7067       p_apply_gl_date                 IN ar_receivable_applications.gl_date%TYPE DEFAULT NULL,
7068       p_ussgl_transaction_code        IN ar_receivable_applications.ussgl_transaction_code%TYPE DEFAULT NULL,
7069       p_attribute_rec                 IN attribute_rec_type DEFAULT attribute_rec_const,
7070 	 -- ******* Global Flexfield parameters *******
7071       p_global_attribute_rec          IN global_attribute_rec_type DEFAULT global_attribute_rec_const,
7072       p_comments                      IN ar_receivable_applications.comments%TYPE DEFAULT NULL,
7073       p_application_ref_type IN OUT NOCOPY
7074                 ar_receivable_applications.application_ref_type%TYPE,
7075       p_application_ref_id IN OUT NOCOPY
7076                 ar_receivable_applications.application_ref_id%TYPE,
7077       p_application_ref_num IN OUT NOCOPY
7078                 ar_receivable_applications.application_ref_num%TYPE,
7079       p_secondary_application_ref_id IN OUT NOCOPY
7080                 ar_receivable_applications.secondary_application_ref_id%TYPE,
7081       p_payment_set_id IN ar_receivable_applications.payment_set_id%TYPE DEFAULT NULL,
7082       p_receivable_application_id OUT NOCOPY ar_receivable_applications.receivable_application_id%TYPE,
7083       p_customer_reference IN ar_receivable_applications.customer_reference%TYPE,
7084       p_val_writeoff_limits_flag     IN VARCHAR2,
7085       p_called_from 		     IN VARCHAR2,
7086       p_netted_receipt_flag          IN VARCHAR2,
7087       p_netted_cash_receipt_id       IN ar_cash_receipts.cash_receipt_id%TYPE,
7088       p_secondary_app_ref_type IN
7089                 ar_receivable_applications.secondary_application_ref_type%TYPE := null,
7090       p_secondary_app_ref_num IN
7091                 ar_receivable_applications.secondary_application_ref_num%TYPE := null,
7092       p_org_id             IN NUMBER  DEFAULT NULL,
7093       p_customer_reason    IN
7094                 ar_receivable_applications.customer_reason%TYPE DEFAULT NULL
7095      ,p_pay_group_lookup_code	IN  FND_LOOKUPS.lookup_code%TYPE
7096      ,p_pay_alone_flag		IN  VARCHAR2
7097      ,p_payment_method_code	IN  ap_invoices.payment_method_code%TYPE
7098      ,p_payment_reason_code	IN  ap_invoices.payment_reason_code%TYPE
7099      ,p_payment_reason_comments	IN  ap_invoices.payment_reason_comments%TYPE
7100      ,p_delivery_channel_code	IN  ap_invoices.delivery_channel_code%TYPE
7101      ,p_remittance_message1	IN  ap_invoices.remittance_message1%TYPE
7102      ,p_remittance_message2	IN  ap_invoices.remittance_message2%TYPE
7103      ,p_remittance_message3	IN  ap_invoices.remittance_message3%TYPE
7104      ,p_party_id		IN  hz_parties.party_id%TYPE
7105      ,p_party_site_id	IN  hz_party_sites.party_site_id%TYPE
7106      ,p_bank_account_id		IN  ar_cash_receipts.customer_bank_account_id%TYPE
7107 	  ) IS
7108 
7109  l_api_name       CONSTANT VARCHAR2(20) := 'Activity_application';
7110  l_api_version    CONSTANT NUMBER       := 1.0;
7111  l_cash_receipt_id NUMBER(15);
7112  l_amount_applied  NUMBER;
7113  l_apply_date      DATE;
7114  l_apply_gl_date   DATE;
7115  l_cr_gl_date      DATE;
7116  l_default_return_status  VARCHAR2(1);
7117  l_validation_return_status  VARCHAR2(1);
7118  l_id_conv_return_status  VARCHAR2(1);
7119  l_cr_unapp_amount  NUMBER;
7120  ln_rec_application_id  NUMBER;
7121  l_cr_date   DATE;
7122  l_cr_payment_schedule_id NUMBER;
7123  l_cr_currency_code VARCHAR2(15);
7124  l_application_ref_type ar_receivable_applications.application_ref_type%TYPE;
7125  l_application_ref_id   ar_receivable_applications.application_ref_id%TYPE;
7126  l_application_ref_num  ar_receivable_applications.application_ref_num%TYPE;
7127  l_secondary_application_ref_id ar_receivable_applications.secondary_application_ref_id%TYPE;
7128  l_secondary_app_ref_type ar_receivable_applications.secondary_application_ref_type%TYPE;
7129  l_secondary_app_ref_num  ar_receivable_applications.secondary_application_ref_num%TYPE;
7130  l_payment_set_id ar_receivable_applications.payment_set_id%TYPE;
7131  l_acctd_amount_applied_from NUMBER;
7132  l_acctd_amount_applied_to   NUMBER;
7133  l_org_return_status VARCHAR2(1);
7134  l_org_id                           NUMBER;
7135 
7136  l_party_id hz_parties.party_id%TYPE;
7137  l_party_name hz_parties.party_name%TYPE;
7138  l_party_number hz_parties.party_number%TYPE;
7139  l_party_site_id hz_party_sites.party_site_id%TYPE;
7140  l_party_address VARCHAR2(360);
7141  l_payment_method_code iby_payment_methods_vl.payment_method_code%TYPE;
7142  l_payment_method_name iby_payment_methods_vl.payment_method_name%TYPE;
7143  l_bank_account_id iby_ext_bank_accounts.ext_bank_account_id%TYPE;
7144  l_bank_account_num iby_ext_bank_accounts.bank_account_num%TYPE;
7145  l_payment_reason_code iby_payment_reasons_vl.payment_reason_code%TYPE;
7146  l_payment_reason_name iby_payment_reasons_vl.meaning%TYPE;
7147  l_delivery_channel_code iby_delivery_channels_vl.delivery_channel_code%TYPE;
7148  l_delivery_channel_name iby_delivery_channels_vl.meaning%TYPE;
7149  l_pay_alone_flag VARCHAR2(1);
7150  l_legal_entity_id ar_cash_receipts.legal_entity_id%TYPE;
7151  l_exchange_rate ar_cash_receipts.exchange_rate%TYPE;
7152  l_exchange_rate_type ar_cash_receipts.exchange_rate_type%TYPE;
7153  l_exchange_date ar_cash_receipts.exchange_date%TYPE;
7154  l_invoice_id ap_invoices.invoice_id%TYPE;
7155  l_dft_ref_return_status  VARCHAR2(1);
7156  l_called_from  varchar2(20); /*5444407*/
7157 
7158  BEGIN
7159 	l_called_from:=p_called_from; /*5444407*/
7160 
7161        /*------------------------------------+
7162         |   Standard start of API savepoint  |
7163         +------------------------------------*/
7164 
7165       SAVEPOINT Activity_app_PVT;
7166 
7167        /*--------------------------------------------------+
7168         |   Standard call to check for call compatibility  |
7169         +--------------------------------------------------*/
7170 
7171         IF NOT FND_API.Compatible_API_Call(
7172                                             l_api_version,
7173                                             p_api_version,
7174                                             l_api_name,
7175                                             G_PKG_NAME
7176                                           )
7177         THEN
7178               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7179         END IF;
7180 
7181        /*--------------------------------------------------------------+
7182         |   Initialize message list if p_init_msg_list is set to TRUE  |
7183         +--------------------------------------------------------------*/
7184 
7185         IF FND_API.to_Boolean( p_init_msg_list )
7186           THEN
7187               FND_MSG_PUB.initialize;
7188         END IF;
7189 
7190         IF PG_DEBUG in ('Y', 'C') THEN
7191            arp_util.debug('Activity_application()+ ');
7192         END IF;
7193        /*-----------------------------------------+
7194         |   Initialize return status to SUCCESS   |
7195         +-----------------------------------------*/
7196 
7197         x_return_status := FND_API.G_RET_STS_SUCCESS;
7198 
7199 
7200 
7201 
7202 /* SSA change */
7203        l_org_id            := p_org_id;
7204        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
7205        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
7206                                                 p_return_status =>l_org_return_status);
7207  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7208        x_return_status := FND_API.G_RET_STS_ERROR;
7209  ELSE
7210         /*-------------------------------------------------+
7211          | Initialize the profile option package variables |
7212          +-------------------------------------------------*/
7213 
7214            initialize_profile_globals;
7215 
7216 
7217        /*---------------------------------------------+
7218         |   ========== Start of API Body ==========   |
7219         +---------------------------------------------*/
7220 
7221      l_cash_receipt_id := p_cash_receipt_id;
7222      l_amount_applied  := p_amount_applied;
7223      l_apply_date      := trunc(p_apply_date);
7224      l_apply_gl_date   := trunc(p_apply_gl_date);
7225      l_application_ref_type         := p_application_ref_type;
7226      l_application_ref_id           := p_application_ref_id;
7227      l_application_ref_num          := p_application_ref_num;
7228      l_secondary_application_ref_id := p_secondary_application_ref_id;
7229      l_secondary_app_ref_type := p_secondary_app_ref_type;
7230      l_secondary_app_ref_num  := p_secondary_app_ref_num;
7231      l_payment_set_id               := p_payment_set_id;
7232      l_party_id		:= p_party_id;
7233      l_party_site_id	:= p_party_site_id;
7234 
7235 
7236           /*-----------------------+
7237            |                       |
7238            |ID TO VALUE CONVERSION |
7239            |                       |
7240            +-----------------------*/
7241 
7242      ar_receipt_lib_pvt.Default_cash_receipt_id(
7243                               l_cash_receipt_id ,
7244                               p_receipt_number ,
7245                               l_id_conv_return_status
7246                                );
7247 
7248      IF PG_DEBUG in ('Y', 'C') THEN
7249         arp_util.debug('Activity_application: ' || 'Default_cash_receipt_i return status :'||l_default_return_status);
7250      END IF;
7251 
7252           /*---------------------+
7253            |                     |
7254            |    DEFAULTING       |
7255            |                     |
7256            +---------------------*/
7257 
7258       ar_receipt_lib_pvt.Default_on_ac_app_info(
7259                                      l_cash_receipt_id,
7260                                      l_cr_gl_date,
7261                                      l_cr_unapp_amount,
7262                                      l_cr_date,
7263                                      l_cr_payment_schedule_id,
7264                                      l_amount_applied,
7265                                      l_apply_gl_date,
7266                                      l_apply_date,
7267                                      l_cr_currency_code,
7268                                      l_default_return_status
7269                                      );
7270 
7271       IF p_applied_payment_schedule_id = -8 THEN
7272          /* Default the refund attributes from IBY */
7273          ar_receipt_lib_pvt.default_refund_attributes(
7274             	 p_cash_receipt_id	=>  l_cash_receipt_id
7275             	,p_customer_trx_id	=>  null
7276 		,p_currency_code	=>  l_cr_currency_code
7277 		,p_amount		=>  l_amount_applied
7278 		,p_party_id		=>  l_party_id
7279 		,p_party_site_id	=>  l_party_site_id
7280 		,x_party_name		=>  l_party_name
7281 		,x_party_number		=>  l_party_number
7282 		,x_party_address	=>  l_party_address
7283 		,x_exchange_rate	=>  l_exchange_rate
7284 		,x_exchange_rate_type	=>  l_exchange_rate_type
7285 		,x_exchange_date	=>  l_exchange_date
7286 		,x_legal_entity_id	=>  l_legal_entity_id
7287 		,x_payment_method_code	=>  l_payment_method_code
7288 		,x_payment_method_name	=>  l_payment_method_name
7289 		,x_bank_account_id	=>  l_bank_account_id
7290           	,x_bank_account_num	=>  l_bank_account_num
7291           	,x_payment_reason_code => l_payment_reason_code
7292           	,x_payment_reason_name => l_payment_reason_name
7293           	,x_delivery_channel_code => l_delivery_channel_code
7294           	,x_delivery_channel_name => l_delivery_channel_name
7295 		,x_pay_alone_flag	=>  l_pay_alone_flag
7296 		,x_return_status	=> l_dft_ref_return_status
7297 		,x_msg_count		=> x_msg_count
7298 		,x_msg_data		=> x_msg_data );
7299 
7300          /* If values have been passed in they should be used instead */
7301          IF p_payment_method_code IS NOT NULL THEN
7302             l_payment_method_code := p_payment_method_code;
7303 	 END IF;
7304          IF p_bank_account_id IS NOT NULL THEN
7305             l_bank_account_id := p_bank_account_id;
7306 	 END IF;
7307          IF p_payment_reason_code IS NOT NULL THEN
7308             l_payment_reason_code := p_payment_reason_code;
7309 	 END IF;
7310          IF p_delivery_channel_code IS NOT NULL THEN
7311             l_delivery_channel_code := p_delivery_channel_code;
7312 	 END IF;
7313          IF p_pay_alone_flag IS NOT NULL THEN
7314             l_pay_alone_flag := p_pay_alone_flag;
7315 	 END IF;
7316 
7317       END IF;
7318 
7319       IF PG_DEBUG in ('Y', 'C') THEN
7320          arp_util.debug('Activity_application: ' || 'Default_on_ac_app_info return status :'||l_default_return_status);
7321          arp_util.debug('Activity_application: ' || 'Default_refund_attributes return status :'||l_dft_ref_return_status);
7322       END IF;
7323           /*---------------------+
7324            |                     |
7325            |    VALIDATION       |
7326            |                     |
7327            +---------------------*/
7328 
7329       ar_receipt_val_pvt.validate_activity_app(
7330                                      p_receivables_trx_id,
7331                                      p_applied_payment_schedule_id,
7332                                      l_cash_receipt_id,
7333                                      l_cr_gl_date,
7334                                      l_cr_unapp_amount,
7335                                      l_cr_date,
7336                                      l_cr_payment_schedule_id,
7337                                      l_amount_applied,
7338                                      l_apply_gl_date,
7339                                      l_apply_date,
7340                                      p_link_to_customer_trx_id,
7341                                      l_cr_currency_code,
7342                                      l_validation_return_status,
7343                                      p_val_writeoff_limits_flag,
7344 				     p_called_from
7345                                      );
7346 
7347       IF PG_DEBUG in ('Y', 'C') THEN
7348          arp_util.debug('Activity_application: ' || 'Validation return status :'||l_validation_return_status);
7349          arp_util.debug('Activity_application: ' || '*****DUMPING ALL THE ENTITY HANDLER PARAMETERS  ***');
7350          arp_util.debug('Activity_application: ' || 'l_cr_payment_schedule_id : '||to_char(l_cr_payment_schedule_id));
7351          arp_util.debug('Activity_application: ' || 'l_amount_applied         : '||to_char(l_amount_applied));
7352          arp_util.debug('Activity_application: ' || 'l_apply_date             : '||to_char(l_apply_date,'DD-MON-YY'));
7353          arp_util.debug('Activity_application: ' || 'l_apply_gl_date          : '||to_char(l_apply_gl_date,'DD-MON-YY'));
7354       END IF;
7355 END IF;
7356 
7357       IF l_validation_return_status <> FND_API.G_RET_STS_SUCCESS  OR
7358          l_default_return_status <> FND_API.G_RET_STS_SUCCESS OR
7359          l_dft_ref_return_status <> FND_API.G_RET_STS_SUCCESS OR
7360          l_id_conv_return_status <> FND_API.G_RET_STS_SUCCESS  THEN
7361 
7362           IF PG_DEBUG in ('Y', 'C') THEN
7363              arp_util.debug('Activity_application: ' || 'Validation FAILED ');
7364           END IF;
7365           x_return_status :=  FND_API.G_RET_STS_ERROR;
7366       END IF;
7367 
7368       IF x_return_status <> FND_API.G_RET_STS_SUCCESS
7369          THEN
7370 
7371             ROLLBACK TO Activity_app_PVT;
7372 
7373              x_return_status := FND_API.G_RET_STS_ERROR ;
7374 
7375              FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7376                                         p_count => x_msg_count,
7377                                         p_data  => x_msg_data
7378                                        );
7379 
7380              IF PG_DEBUG in ('Y', 'C') THEN
7381                 arp_util.debug('Activity_application: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
7382              END IF;
7383              Return;
7384        END IF;
7385 
7386 
7387        --call the entity handler
7388        --BR
7389      BEGIN
7390 
7391          arp_process_application.activity_application (
7392                                   p_receipt_ps_id   => l_cr_payment_schedule_id,
7393                                   p_application_ps_id => p_applied_payment_schedule_id,
7394                                   p_link_to_customer_trx_id => p_link_to_customer_trx_id,
7395                                   p_amount_applied  => l_amount_applied,
7396                                   p_apply_date      => l_apply_date,
7397                                   p_gl_date         => l_apply_gl_date,
7398                                   p_receivables_trx_id => p_receivables_trx_id,
7399                                   p_ussgl_transaction_code => p_ussgl_transaction_code,
7400                                   p_attribute_category=> p_attribute_rec.attribute_category,
7401                                   p_attribute1        => p_attribute_rec.attribute1,
7402                                   p_attribute2        => p_attribute_rec.attribute2,
7403                                   p_attribute3        => p_attribute_rec.attribute3,
7404                                   p_attribute4        => p_attribute_rec.attribute4,
7405                                   p_attribute5        => p_attribute_rec.attribute5,
7406                                   p_attribute6        => p_attribute_rec.attribute6,
7407                                   p_attribute7        => p_attribute_rec.attribute7,
7408                                   p_attribute8        => p_attribute_rec.attribute8,
7409                                   p_attribute9        => p_attribute_rec.attribute9,
7410                                   p_attribute10       => p_attribute_rec.attribute10,
7411                                   p_attribute11       => p_attribute_rec.attribute11,
7412                                   p_attribute12       => p_attribute_rec.attribute12,
7413                                   p_attribute13       => p_attribute_rec.attribute13,
7414                                   p_attribute14       => p_attribute_rec.attribute14,
7415                                   p_attribute15       => p_attribute_rec.attribute15,
7416                                   p_global_attribute1 => p_global_attribute_rec.global_attribute1,
7417                                   p_global_attribute2 => p_global_attribute_rec.global_attribute2,
7418                                   p_global_attribute3 => p_global_attribute_rec.global_attribute3,
7419                                   p_global_attribute4 => p_global_attribute_rec.global_attribute4,
7420                                   p_global_attribute5 => p_global_attribute_rec.global_attribute5,
7421                                   p_global_attribute6 => p_global_attribute_rec.global_attribute6,
7422                                   p_global_attribute7 => p_global_attribute_rec.global_attribute7,
7423                                   p_global_attribute8 => p_global_attribute_rec.global_attribute8,
7424                                   p_global_attribute9 => p_global_attribute_rec.global_attribute9,
7425                                   p_global_attribute10 => p_global_attribute_rec.global_attribute10,
7426                                   p_global_attribute11 => p_global_attribute_rec.global_attribute11,
7427                                   p_global_attribute12 => p_global_attribute_rec.global_attribute12,
7428                                   p_global_attribute13 => p_global_attribute_rec.global_attribute13,
7429                                   p_global_attribute14 => p_global_attribute_rec.global_attribute14,
7430                                   p_global_attribute15 => p_global_attribute_rec.global_attribute15,
7431                                   p_global_attribute16 => p_global_attribute_rec.global_attribute16,
7432                                   p_global_attribute17 => p_global_attribute_rec.global_attribute17,
7433                                   p_global_attribute18 => p_global_attribute_rec.global_attribute18,
7434                                   p_global_attribute19 => p_global_attribute_rec.global_attribute19,
7435                                   p_global_attribute20 => p_global_attribute_rec.global_attribute20,
7436                                   p_global_attribute_category => p_global_attribute_rec.global_attribute_category,
7437                                   p_module_name         => 'RAPI',
7438                                   p_comments         => p_comments,
7439                                   p_application_ref_type => l_application_ref_type,
7440                                   p_application_ref_id   => l_application_ref_id,
7441                                   p_application_ref_num  => l_application_ref_num,
7442                                   p_secondary_application_ref_id   => l_secondary_application_ref_id,
7443                                   p_secondary_app_ref_type => l_secondary_app_ref_type,
7444                                   p_secondary_app_ref_num  => l_secondary_app_ref_num,
7445                                   p_payment_set_id   => l_payment_set_id,
7446                                   p_module_version      => p_api_version,
7447                                   -- *** OUT NOCOPY
7448 				  p_called_from 	   => l_called_from, /*5444407*/
7449                                   p_out_rec_application_id => ln_rec_application_id,
7450                                   p_customer_reference => p_customer_reference,
7451 				  p_netted_receipt_flag => p_netted_receipt_flag,
7452 				  p_netted_cash_receipt_id => p_netted_cash_receipt_id,
7453                                   p_customer_reason => p_customer_reason -- 4145224
7454                                    );
7455 
7456      EXCEPTION
7457        WHEN OTHERS THEN
7458 
7459                /*-------------------------------------------------------+
7460                 |  Handle application errors that result from trapable  |
7461                 |  error conditions. The error messages have already    |
7462                 |  been put on the error stack.                         |
7463                 +-------------------------------------------------------*/
7464 
7465                 IF (SQLCODE = -20001)
7466                 THEN
7467                      ROLLBACK TO Activity_app_PVT;
7468 
7469                       --  Display_Parameters;
7470                       x_return_status := FND_API.G_RET_STS_ERROR ;
7471                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
7472                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.ACTIVITY_APPLICATION : '||SQLERRM);
7473                        FND_MSG_PUB.Add;
7474 
7475                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
7476                                                   p_count  =>  x_msg_count,
7477                                                   p_data   => x_msg_data
7478                                                 );
7479                       RETURN;
7480                 ELSE
7481                    RAISE;
7482                 END IF;
7483 
7484      END;
7485 
7486      /* Call AP API's to create payment request if refund */
7487      IF p_applied_payment_schedule_id = -8 THEN
7488 	BEGIN
7489 	  ar_refunds_pvt.create_refund
7490 		(p_receivable_application_id	=>  ln_rec_application_id
7491 		,p_amount			=>  l_amount_applied
7492 		,p_currency_code		=>  l_cr_currency_code
7493 		,p_exchange_rate		=>  l_exchange_rate
7494 		,p_exchange_rate_type		=>  l_exchange_rate_type
7495 		,p_exchange_date		=>  l_exchange_date
7496 		,p_description			=>  NULL
7497           	,p_pay_group_lookup_code	=>  p_pay_group_lookup_code
7498           	,p_pay_alone_flag		=>  l_pay_alone_flag
7499 		,p_org_id			=>  l_org_id
7500 	  	,p_legal_entity_id        	=>  l_legal_entity_id
7501           	,p_payment_method_code		=>  l_payment_method_code
7502           	,p_payment_reason_code		=>  l_payment_reason_code
7503           	,p_payment_reason_comments	=>  p_payment_reason_comments
7504           	,p_delivery_channel_code	=>  l_delivery_channel_code
7505           	,p_remittance_message1		=>  p_remittance_message1
7506           	,p_remittance_message2		=>  p_remittance_message2
7507           	,p_remittance_message3		=>  p_remittance_message3
7508           	,p_party_id			=>  l_party_id
7509           	,p_party_site_id		=>  l_party_site_id
7510                 ,p_bank_account_id		=>  l_bank_account_id
7511 		,p_called_from			=>  'RECEIPT_API'
7512 		,x_invoice_id			=> l_invoice_id
7513 		,x_return_status		=> x_return_status
7514 		,x_msg_count			=> x_msg_count
7515 		,x_msg_data			=> x_msg_data );
7516 
7517           l_application_ref_id := l_invoice_id;
7518           l_application_ref_num := l_invoice_id;
7519 
7520         EXCEPTION
7521        WHEN OTHERS THEN
7522 
7523                /*-------------------------------------------------------+
7524                 |  Handle application errors that result from trapable  |
7525                 |  error conditions. The error messages have already    |
7526                 |  been put on the error stack.                         |
7527                 +-------------------------------------------------------*/
7528 
7529                 IF (SQLCODE = -20001)
7530                 THEN
7531                      ROLLBACK TO Activity_app_PVT;
7532 
7533                       --  Display_Parameters;
7534                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7535                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
7536                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','AR_REFUNDS_PVT.Create_Refund : '||SQLERRM);
7537                        FND_MSG_PUB.Add;
7538 
7539                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
7540                                                   p_count  =>  x_msg_count,
7541                                                   p_data   => x_msg_data
7542                                                 );
7543                       RETURN;
7544                 ELSE
7545                    RAISE;
7546                 END IF;
7547         END;
7548      END IF;
7549         p_receivable_application_id   := ln_rec_application_id;
7550         p_application_ref_type := l_application_ref_type;
7551         p_application_ref_id   := l_application_ref_id;
7552         p_application_ref_num  := l_application_ref_num;
7553         p_secondary_application_ref_id   := l_secondary_application_ref_id;
7554        /*--------------------------------+
7555         |   Standard check of p_commit   |
7556         +--------------------------------*/
7557 
7558         IF FND_API.To_Boolean( p_commit )
7559         THEN
7560             IF PG_DEBUG in ('Y', 'C') THEN
7561                arp_util.debug('Activity_application: ' || 'committing');
7562             END IF;
7563               Commit;
7564         END IF;
7565         IF PG_DEBUG in ('Y', 'C') THEN
7566            arp_util.debug('Activity_application ()- ');
7567         END IF;
7568 EXCEPTION
7569        WHEN FND_API.G_EXC_ERROR THEN
7570 
7571                 IF PG_DEBUG in ('Y', 'C') THEN
7572                    arp_util.debug('Activity_application: ' || SQLCODE, G_MSG_ERROR);
7573                    arp_util.debug('Activity_application: ' || SQLERRM, G_MSG_ERROR);
7574                 END IF;
7575 
7576                 ROLLBACK TO Activity_app_PVT;
7577                 x_return_status := FND_API.G_RET_STS_ERROR ;
7578 
7579                -- Display_Parameters;
7580 
7581                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7582                                            p_count       =>      x_msg_count,
7583                                            p_data        =>      x_msg_data
7584                                          );
7585 
7586         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7587 
7588                 IF PG_DEBUG in ('Y', 'C') THEN
7589                    arp_util.debug('Activity_application: ' || SQLERRM, G_MSG_ERROR);
7590                 END IF;
7591                 ROLLBACK TO Activity_app_PVT;
7592                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7593 
7594                --  Display_Parameters;
7595 
7596                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7597                                            p_count       =>      x_msg_count,
7598                                            p_data        =>      x_msg_data
7599                                          );
7600 
7601         WHEN OTHERS THEN
7602 
7603                /*-------------------------------------------------------+
7604                 |  Handle application errors that result from trapable  |
7605                 |  error conditions. The error messages have already    |
7606                 |  been put on the error stack.                         |
7607                 +-------------------------------------------------------*/
7608 
7609                 IF (SQLCODE = -20001)
7610                 THEN
7611 
7612                       ROLLBACK TO Activity_app_PVT;
7613 
7614                       --If only one error message on the stack,
7615                       --retrive it
7616 
7617                       x_return_status := FND_API.G_RET_STS_ERROR ;
7618                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
7619                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ACTIVITY_APPLICATION : '||SQLERRM);
7620                       FND_MSG_PUB.Add;
7621 
7622                       --If only one error message on the stack,
7623                       --retrive it
7624 
7625                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7626                                                  p_count  =>  x_msg_count,
7627                                                  p_data   => x_msg_data
7628                                                 );
7629 
7630                       RETURN;
7631 
7632                 ELSE
7633                      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7634                      FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
7635                      FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ACTIVITY_APPLICATION : '||SQLERRM);
7636                      FND_MSG_PUB.Add;
7637                 END IF;
7638 
7639                 IF PG_DEBUG in ('Y', 'C') THEN
7640                    arp_util.debug('Activity_application: ' || SQLCODE, G_MSG_ERROR);
7641                    arp_util.debug('Activity_application: ' || SQLERRM, G_MSG_ERROR);
7642                 END IF;
7643 
7644                 ROLLBACK TO Activity_app_PVT;
7645 
7646                 IF      FND_MSG_PUB.Check_Msg_Level
7647                 THEN
7648                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
7649                                         l_api_name
7650                                        );
7651                 END IF;
7652 
7653              --   Display_Parameters;
7654 
7655                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7656                                            p_count       =>      x_msg_count,
7657                                            p_data        =>      x_msg_data
7658                                          );
7659 END Activity_application;
7660 
7661 PROCEDURE Activity_Unapplication(
7662     -- Standard API parameters.
7663       p_api_version      IN  NUMBER,
7664       p_init_msg_list    IN  VARCHAR2 := FND_API.G_FALSE,
7665       p_commit           IN  VARCHAR2 := FND_API.G_FALSE,
7666       p_validation_level IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
7667       x_return_status    OUT NOCOPY VARCHAR2 ,
7668       x_msg_count        OUT NOCOPY NUMBER ,
7669       x_msg_data         OUT NOCOPY VARCHAR2 ,
7670    -- *** Receipt Info. parameters *****
7671       p_receipt_number   IN  ar_cash_receipts.receipt_number%TYPE DEFAULT NULL,
7672       p_cash_receipt_id  IN  ar_cash_receipts.cash_receipt_id%TYPE DEFAULT NULL,
7673       p_receivable_application_id   IN ar_receivable_applications.receivable_application_id%TYPE DEFAULT NULL,
7674       p_reversal_gl_date IN ar_receivable_applications.reversal_gl_date%TYPE DEFAULT NULL,
7675       p_called_from      IN VARCHAR2,
7676       p_org_id             IN NUMBER  DEFAULT NULL
7677       ) IS
7678 l_api_name       CONSTANT VARCHAR2(20) := 'Activity_unapp';
7679 l_api_version    CONSTANT NUMBER       := 1.0;
7680 l_customer_trx_id               NUMBER;
7681 l_cash_receipt_id               NUMBER;
7682 l_receivable_application_id     NUMBER;
7683 l_reversal_gl_date              DATE;
7684 l_apply_gl_date                 DATE;
7685 l_receipt_gl_date               DATE;
7686 l_def_return_status             VARCHAR2(1);
7687 l_val_return_status             VARCHAR2(1);
7688 l_bal_due_remaining             NUMBER;
7689 l_org_return_status VARCHAR2(1);
7690 l_org_id                           NUMBER;
7691 l_cr_unapp_amt                  NUMBER; /* Bug fix 3569640 */
7692 l_application_ref_id		ar_receivable_applications.application_ref_id%TYPE;
7693 l_applied_ps_id			ar_payment_schedules.payment_schedule_id%TYPE;
7694 l_refund_return_status		VARCHAR2(1);
7695 BEGIN
7696        /*------------------------------------+
7697         |   Standard start of API savepoint  |
7698         +------------------------------------*/
7699 
7700         SAVEPOINT Activity_unapplication_PVT;
7701 
7702        /*--------------------------------------------------+
7703         |   Standard call to check for call compatibility  |
7704         +--------------------------------------------------*/
7705 
7706         IF NOT FND_API.Compatible_API_Call(
7707                                             l_api_version,
7708                                             p_api_version,
7709                                             l_api_name,
7710                                             G_PKG_NAME
7711                                           )
7712         THEN
7713               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7714         END IF;
7715 
7716        /*--------------------------------------------------------------+
7717         |   Initialize message list if p_init_msg_list is set to TRUE  |
7718         +--------------------------------------------------------------*/
7719 
7720         IF FND_API.to_Boolean( p_init_msg_list )
7721           THEN
7722               FND_MSG_PUB.initialize;
7723         END IF;
7724 
7725         IF PG_DEBUG in ('Y', 'C') THEN
7726            arp_util.debug('ar_receipt_api.activity_unapplication()+ ');
7727         END IF;
7728        /*-----------------------------------------+
7729         |   Initialize return status to SUCCESS   |
7730         +-----------------------------------------*/
7731 
7732         x_return_status := FND_API.G_RET_STS_SUCCESS;
7733 
7734 
7735 
7736 /* SSA change */
7737        l_org_id            := p_org_id;
7738        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
7739        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
7740                                                 p_return_status =>l_org_return_status);
7741  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7742        x_return_status := FND_API.G_RET_STS_ERROR;
7743  ELSE
7744 
7745         /*-------------------------------------------------+
7746          | Initialize the profile option package variables |
7747          +-------------------------------------------------*/
7748 
7749            initialize_profile_globals;
7750 
7751 
7752 
7753         Original_activity_unapp_info.cash_receipt_id := p_cash_receipt_id;
7754         Original_activity_unapp_info.receipt_number  := p_receipt_number;
7755         Original_activity_unapp_info.receivable_application_id := p_receivable_application_id;
7756 
7757        /*---------------------------------------------+
7758         |   ========== Start of API Body ==========   |
7759         +---------------------------------------------*/
7760 
7761 
7762         --Assign IN parameter values to local variables
7763         --which are also used as assignment targets.
7764 
7765          l_cash_receipt_id   := p_cash_receipt_id;
7766          l_receivable_application_id  := p_receivable_application_id;
7767          l_reversal_gl_date := trunc(p_reversal_gl_date);
7768 
7769 
7770         /*------------------------------------------------+
7771          |  Derive the id's for the entered values.       |
7772          |  If both the values and the ids are specified, |
7773          |  the id's superceed the values                 |
7774          +------------------------------------------------*/
7775 
7776          ar_receipt_lib_pvt.derive_activity_unapp_ids(
7777                          p_receipt_number   ,
7778                          l_cash_receipt_id  ,
7779                          l_receivable_application_id ,
7780                          p_called_from,
7781                          l_apply_gl_date     ,
7782                          l_cr_unapp_amt, /* Bug fix 3569640 */
7783                          l_def_return_status
7784                                );
7785          ar_receipt_lib_pvt.default_unapp_on_ac_act_info(
7786                          l_receivable_application_id ,
7787                          l_apply_gl_date            ,
7788                          l_cash_receipt_id          ,
7789                          l_reversal_gl_date         ,
7790                          l_receipt_gl_date
7791                           );
7792 
7793          ar_receipt_val_pvt.validate_unapp_on_ac_act_info(
7794                                       l_receipt_gl_date,
7795                                       l_receivable_application_id,
7796                                       l_reversal_gl_date,
7797                                       l_apply_gl_date,
7798                                       l_cr_unapp_amt, /* Bug fix 3569640 */
7799                                       l_val_return_status);
7800 
7801          IF PG_DEBUG in ('Y', 'C') THEN
7802             arp_util.debug('Activity_Unapplication: ' || 'validation return status :'||l_val_return_status);
7803          END IF;
7804 END IF;
7805 
7806 	/* Refunds - check for refund and cancel if refund application */
7807         SELECT applied_payment_schedule_id, application_ref_id
7808 	INTO   l_applied_ps_id, l_application_ref_id
7809 	FROM   ar_receivable_applications
7810   	WHERE  receivable_application_id = l_receivable_application_id;
7811 
7812         IF (l_applied_ps_id = -8 AND p_called_from <> 'AR_REFUNDS_GRP') THEN
7813            ar_refunds_pvt.cancel_refund(
7814 		  p_application_ref_id  => l_application_ref_id
7815 		, p_gl_date		=> l_reversal_gl_date
7816 		, x_return_status	=> l_refund_return_status
7817 		, x_msg_count		=> x_msg_count
7818 		, x_msg_data		=> x_msg_data);
7819         END IF;
7820 
7821 
7822         IF l_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
7823 	   l_refund_return_status <> FND_API.G_RET_STS_SUCCESS OR
7824            l_def_return_status <> FND_API.G_RET_STS_SUCCESS  THEN
7825            x_return_status := FND_API.G_RET_STS_ERROR;
7826         END IF;
7827 
7828         IF x_return_status <> FND_API.G_RET_STS_SUCCESS
7829          THEN
7830 
7831             ROLLBACK TO Activity_unapplication_PVT;
7832 
7833              x_return_status := FND_API.G_RET_STS_ERROR ;
7834 
7835              FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7836                                         p_count => x_msg_count,
7837                                         p_data  => x_msg_data
7838                                        );
7839 
7840              IF PG_DEBUG in ('Y', 'C') THEN
7841                 arp_util.debug('Activity_Unapplication: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
7842              END IF;
7843              Return;
7844         END IF;
7845 
7846         IF PG_DEBUG in ('Y', 'C') THEN
7847            arp_util.debug('Activity_Unapplication: ' || '*******DUMP THE INPUT PARAMETERS ********');
7848            arp_util.debug('Activity_Unapplication: ' || 'l_receivable_application_id :'||to_char(l_receivable_application_id));
7849            arp_util.debug('Activity_Unapplication: ' || 'l_reversal_gl_date :'||to_char(l_reversal_gl_date,'DD-MON-YY'));
7850         END IF;
7851 
7852        --lock the receipt before calling the entity handler
7853        arp_cash_receipts_pkg.nowaitlock_p(p_cr_id => l_cash_receipt_id);
7854 
7855 
7856         --call the entity handler.
7857       BEGIN
7858           arp_process_application.reverse(
7859                                 l_receivable_application_id,
7860                                 l_reversal_gl_date,
7861                                 trunc(sysdate),
7862                                 NVL(p_called_from,'RAPI'), -- Bug 2855180
7863                                 p_api_version,
7864                                 l_bal_due_remaining );
7865       EXCEPTION
7866         WHEN OTHERS THEN
7867 
7868                /*-------------------------------------------------------+
7869                 |  Handle application errors that result from trapable  |
7870                 |  error conditions. The error messages have already    |
7871                 |  been put on the error stack.                         |
7872                 +-------------------------------------------------------*/
7873 
7874                 IF (SQLCODE = -20001)
7875                 THEN
7876                      ROLLBACK TO Activity_unapplication_PVT;
7877 
7878                       --  Display_Parameters;
7879                       x_return_status := FND_API.G_RET_STS_ERROR ;
7880                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
7881                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.REVERSE : '||SQLERRM);
7882                       FND_MSG_PUB.Add;
7883 
7884                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
7885                                                   p_count  =>  x_msg_count,
7886                                                   p_data   => x_msg_data
7887                                                 );
7888                       RETURN;
7889                 ELSE
7890                    RAISE;
7891                 END IF;
7892       END;
7893 
7894        /*--------------------------------+
7895         |   Standard check of p_commit   |
7896         +--------------------------------*/
7897 
7898         IF FND_API.To_Boolean( p_commit )
7899         THEN
7900             IF PG_DEBUG in ('Y', 'C') THEN
7901                arp_util.debug('Activity_Unapplication: ' || 'committing');
7902             END IF;
7903               Commit;
7904         END IF;
7905 
7906         IF PG_DEBUG in ('Y', 'C') THEN
7907            arp_util.debug('ar_receipt_api.Activity_unapplication()- ');
7908         END IF;
7909 
7910 
7911 EXCEPTION
7912        WHEN FND_API.G_EXC_ERROR THEN
7913 
7914                 IF PG_DEBUG in ('Y', 'C') THEN
7915                    arp_util.debug('Activity_Unapplication: ' || SQLCODE, G_MSG_ERROR);
7916                    arp_util.debug('Activity_Unapplication: ' || SQLERRM, G_MSG_ERROR);
7917                 END IF;
7918 
7919                 ROLLBACK TO Activity_unapplication_PVT;
7920                 x_return_status := FND_API.G_RET_STS_ERROR ;
7921 
7922               --  Display_Parameters;
7923 
7924                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7925                                            p_count       =>      x_msg_count,
7926                                            p_data        =>      x_msg_data
7927                                          );
7928 
7929         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7930 
7931                 IF PG_DEBUG in ('Y', 'C') THEN
7932                    arp_util.debug('Activity_Unapplication: ' || SQLERRM, G_MSG_ERROR);
7933                 END IF;
7934                 ROLLBACK TO Activity_unapplication_PVT;
7935                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7936 
7937                --  Display_Parameters;
7938 
7939                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7940                                            p_count       =>      x_msg_count,
7941                                            p_data        =>      x_msg_data
7942                                          );
7943 
7944         WHEN OTHERS THEN
7945 
7946                /*-------------------------------------------------------+
7947                 |  Handle application errors that result from trapable  |
7948                 |  error conditions. The error messages have already    |
7949                 |  been put on the error stack.                         |
7950                 +-------------------------------------------------------*/
7951 
7952                 IF (SQLCODE = -20001)
7953                 THEN
7954 
7955                       ROLLBACK TO Activity_unapplication_PVT;
7956 
7957                       x_return_status := FND_API.G_RET_STS_ERROR ;
7958                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
7959                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ACTIVITY_UNAPPLICATION : '||SQLERRM);
7960                       FND_MSG_PUB.Add;
7961 
7962                       --If only one error message on the stack,
7963                       --retrive it
7964                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7965                                                  p_count  =>  x_msg_count,
7966                                                  p_data   => x_msg_data
7967                                                 );
7968 
7969                       RETURN;
7970 
7971                 ELSE
7972                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7973                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
7974                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ACTIVITY_UNAPPLICATION : '||SQLERRM);
7975                       FND_MSG_PUB.Add;
7976                 END IF;
7977 
7978                 IF PG_DEBUG in ('Y', 'C') THEN
7979                    arp_util.debug('Activity_Unapplication: ' || SQLCODE, G_MSG_ERROR);
7980                    arp_util.debug('Activity_Unapplication: ' || SQLERRM, G_MSG_ERROR);
7981                 END IF;
7982 
7983                 ROLLBACK TO Activity_unapplication_PVT;
7984 
7985                 IF      FND_MSG_PUB.Check_Msg_Level
7986                 THEN
7987                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
7988                                         l_api_name
7989                                        );
7990                 END IF;
7991 
7992              --   Display_Parameters;
7993 
7994                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
7995                                            p_count       =>      x_msg_count,
7996                                            p_data        =>      x_msg_data
7997                                          );
7998 END Activity_unapplication;
7999 
8000 PROCEDURE Apply_other_account(
8001 -- Standard API parameters.
8002       p_api_version      IN  NUMBER,
8003       p_init_msg_list    IN  VARCHAR2 := FND_API.G_FALSE,
8004       p_commit           IN  VARCHAR2 := FND_API.G_FALSE,
8005       p_validation_level IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
8006       x_return_status    OUT NOCOPY VARCHAR2,
8007       x_msg_count        OUT NOCOPY NUMBER,
8008       x_msg_data         OUT NOCOPY VARCHAR2,
8009       p_receivable_application_id OUT NOCOPY ar_receivable_applications.receivable_application_id%TYPE,
8010   --  Receipt application parameters.
8011       p_cash_receipt_id         IN ar_cash_receipts.cash_receipt_id%TYPE DEFAULT NULL,
8012       p_receipt_number          IN ar_cash_receipts.receipt_number%TYPE DEFAULT NULL,
8013       p_amount_applied          IN ar_receivable_applications.amount_applied%TYPE DEFAULT NULL,
8014       p_receivables_trx_id      IN ar_receivable_applications.receivables_trx_id%TYPE DEFAULT NULL,
8015       p_applied_payment_schedule_id      IN ar_receivable_applications.applied_payment_schedule_id%TYPE DEFAULT NULL,
8016       p_apply_date              IN ar_receivable_applications.apply_date%TYPE DEFAULT NULL,
8017       p_apply_gl_date                 IN ar_receivable_applications.gl_date%TYPE DEFAULT NULL,
8018       p_ussgl_transaction_code  IN ar_receivable_applications.ussgl_transaction_code%TYPE DEFAULT NULL,
8019       p_application_ref_type IN ar_receivable_applications.application_ref_type%TYPE DEFAULT NULL,
8020       p_application_ref_id   IN OUT NOCOPY ar_receivable_applications.application_ref_id%TYPE ,
8021       p_application_ref_num  IN OUT NOCOPY ar_receivable_applications.application_ref_num%TYPE ,
8022       p_secondary_application_ref_id IN OUT NOCOPY ar_receivable_applications.secondary_application_ref_id%TYPE ,
8023       p_payment_set_id               IN ar_receivable_applications.payment_set_id%TYPE DEFAULT NULL,
8024       p_attribute_rec      IN attribute_rec_type DEFAULT attribute_rec_const,
8025 	 -- ******* Global Flexfield parameters *******
8026       p_global_attribute_rec IN global_attribute_rec_type DEFAULT global_attribute_rec_const,
8027       p_comments                IN ar_receivable_applications.comments%TYPE DEFAULT NULL,
8028       p_application_ref_reason  IN ar_receivable_applications.application_ref_reason%TYPE DEFAULT NULL,
8029       p_customer_reference      IN ar_receivable_applications.customer_reference%TYPE DEFAULT NULL,
8030       p_customer_reason         IN ar_receivable_applications.customer_reason%TYPE DEFAULT NULL,
8031       p_called_from             IN VARCHAR2,
8032       p_org_id             IN NUMBER  DEFAULT NULL
8033 	  ) IS
8034  l_api_name       CONSTANT VARCHAR2(20) := 'Apply_Other_account';
8035  l_api_version    CONSTANT NUMBER       := 1.0;
8036  l_cash_receipt_id NUMBER(15);
8037  l_amount_applied  NUMBER;
8038  l_apply_date      DATE;
8039  l_apply_gl_date   DATE;
8040  l_cr_gl_date      DATE;
8041  l_default_return_status  VARCHAR2(1);
8042  l_validation_return_status  VARCHAR2(1);
8043  l_app_validation_return_status  VARCHAR2(1);
8044  l_id_conv_return_status  VARCHAR2(1);
8045  l_cr_unapp_amount  NUMBER;
8046  ln_rec_application_id  NUMBER;
8047  l_cr_date   DATE;
8048  l_cr_payment_schedule_id NUMBER;
8049  l_dflex_val_return_status  VARCHAR2(1);
8050  l_attribute_rec           attribute_rec_type;
8051  l_cr_currency_code     VARCHAR2(15);
8052  l_receivables_trx_id           NUMBER;
8053  l_applied_payment_schedule_id       ar_receivable_applications.applied_payment_schedule_id%TYPE;
8054  l_gdflex_return_status             VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
8055  l_global_attribute_rec             global_attribute_rec_type;
8056 
8057  -- Bug # 2707702
8058  l_temp_ref_id  ar_receivable_applications.application_ref_id%TYPE;
8059  l_temp_ref_num ar_receivable_applications.application_ref_num%TYPE;
8060  l_claim_reason_name                VARCHAR2(100);
8061  l_org_return_status VARCHAR2(1);
8062  l_org_id                           NUMBER;
8063 
8064 BEGIN
8065 
8066        /*------------------------------------+
8067         |   Standard start of API savepoint  |
8068         +------------------------------------*/
8069 
8070       SAVEPOINT Apply_other_ac_PVT;
8071 
8072        /*--------------------------------------------------+
8073         |   Standard call to check for call compatibility  |
8074         +--------------------------------------------------*/
8075 
8076         IF NOT FND_API.Compatible_API_Call(
8077                                             l_api_version,
8078                                             p_api_version,
8079                                             l_api_name,
8080                                             G_PKG_NAME
8081                                           )
8082         THEN
8083               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8084         END IF;
8085 
8086        /*--------------------------------------------------------------+
8087         |   Initialize message list if p_init_msg_list is set to TRUE  |
8088         +--------------------------------------------------------------*/
8089 
8090         IF FND_API.to_Boolean( p_init_msg_list )
8091           THEN
8092               FND_MSG_PUB.initialize;
8093         END IF;
8094 
8095         arp_util.debug('Apply_other_account()+ ');
8096        /*-----------------------------------------+
8097         |   Initialize return status to SUCCESS   |
8098         +-----------------------------------------*/
8099 
8100         x_return_status := FND_API.G_RET_STS_SUCCESS;
8101 
8102 
8103 
8104 
8105 /* SSA change */
8106        l_org_id            := p_org_id;
8107        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
8108        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
8109                                                 p_return_status =>l_org_return_status);
8110  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8111        x_return_status := FND_API.G_RET_STS_ERROR;
8112  ELSE
8113         /*-------------------------------------------------+
8114          | Initialize the profile option package variables |
8115          +-------------------------------------------------*/
8116 
8117            initialize_profile_globals;
8118 
8119 
8120        /*---------------------------------------------+
8121         |   ========== Start of API Body ==========   |
8122         +---------------------------------------------*/
8123 
8124      l_cash_receipt_id := p_cash_receipt_id;
8125      l_amount_applied  := p_amount_applied;
8126      l_apply_date      := trunc(p_apply_date);
8127      l_apply_gl_date   := trunc(p_apply_gl_date);
8128      l_attribute_rec   := p_attribute_rec;
8129      l_receivables_trx_id := p_receivables_trx_id;
8130      l_applied_payment_schedule_id := p_applied_payment_schedule_id;
8131      l_global_attribute_rec        := p_global_attribute_rec;
8132           /*-----------------------+
8133            |                       |
8134            |ID TO VALUE CONVERSION |
8135            |                       |
8136            +-----------------------*/
8137 
8138       ar_receipt_lib_pvt.Default_cash_receipt_id(
8139                               l_cash_receipt_id ,
8140                               p_receipt_number ,
8141                               l_id_conv_return_status
8142                                );
8143 
8144       arp_util.debug('Defaulting Ids Return_status = '||l_id_conv_return_status);
8145           /*---------------------+
8146            |                     |
8147            |    DEFAULTING       |
8148            |                     |
8149            +---------------------*/
8150 
8151       ar_receipt_lib_pvt.Default_on_ac_app_info(
8152                                   l_cash_receipt_id,
8153                                   l_cr_gl_date,
8154                                   l_cr_unapp_amount,
8155                                   l_cr_date,
8156                                   l_cr_payment_schedule_id,
8157                                   l_amount_applied,
8158                                   l_apply_gl_date,
8159                                   l_apply_date,
8160                                   l_cr_currency_code,
8161                                   l_default_return_status
8162                                   );
8163 
8164       arp_util.debug('Default_on_ac_app_info return status = '||l_default_return_status);
8165           /*---------------------+
8166            |                     |
8167            |    VALIDATION       |
8168            |                     |
8169            +---------------------*/
8170 
8171        --This routine will validate both validate_on_ac_app and activity
8172        ar_receipt_val_pvt.validate_activity_app(
8173                               l_receivables_trx_id ,
8174                               l_applied_payment_schedule_id,
8175                               l_cash_receipt_id,
8176                               l_cr_gl_date,
8177                               l_cr_unapp_amount,
8178                               l_cr_date ,
8179                               l_cr_payment_schedule_id,
8180                               l_amount_applied,
8181                               l_apply_gl_date,
8182                               l_apply_date ,
8183                               NULL, --p_link_to_customer_trx_id not required
8184                               l_cr_currency_code,
8185                               l_validation_return_status,
8186 			      p_called_from);
8187 
8188 
8189         arp_util.debug('Validation return status :'||l_validation_return_status);
8190 
8191         --Validate application reference details passed.
8192         ar_receipt_val_pvt.validate_application_ref(
8193                               l_applied_payment_schedule_id,
8194                               p_application_ref_type,
8195                               p_application_ref_id,
8196                               p_application_ref_num,
8197                               p_secondary_application_ref_id,
8198                               l_cash_receipt_id,
8199                               l_amount_applied,
8200                               NULL,
8201                               l_cr_currency_code,
8202                               NULL,
8203                               p_application_ref_reason,
8204                               l_app_validation_return_status
8205                                );
8206 
8207         arp_util.debug('Application ref Validation return status :'||l_app_validation_return_status);
8208 
8209 
8210          --validate and default the flexfields
8211         ar_receipt_lib_pvt.Validate_Desc_Flexfield(
8212                                         l_attribute_rec,
8213                                         'AR_RECEIVABLE_APPLICATIONS',
8214                                         l_dflex_val_return_status
8215                                                 );
8216         arp_util.debug('Desc flexfield Validation return status :'||l_dflex_val_return_status);
8217 
8218       --default and validate the global descriptive flexfield
8219         jg_ar_receivable_applications.apply(
8220                   p_apply_before_after        => 'BEFORE',
8221                   p_global_attribute_category => l_global_attribute_rec.global_attribute_category,
8222                   p_set_of_books_id           => arp_global.set_of_books_id,
8223                   p_cash_receipt_id           => l_cash_receipt_id,
8224                   p_receipt_date              => l_cr_date,
8225                   p_applied_payment_schedule_id => l_applied_payment_schedule_id,
8226                   p_amount_applied              => l_amount_applied,
8227                   p_unapplied_amount            => (l_cr_unapp_amount - l_amount_applied),
8228                   p_due_date                    => NULL,
8229                   p_receipt_method_id           => NULL,
8230                   p_remittance_bank_account_id  => NULL,
8231                   p_global_attribute1           => l_global_attribute_rec.global_attribute1,
8232                   p_global_attribute2           => l_global_attribute_rec.global_attribute2,
8233                   p_global_attribute3           => l_global_attribute_rec.global_attribute3,
8234                   p_global_attribute4           => l_global_attribute_rec.global_attribute4,
8235                   p_global_attribute5           => l_global_attribute_rec.global_attribute5,
8236                   p_global_attribute6           => l_global_attribute_rec.global_attribute6,
8237                   p_global_attribute7           => l_global_attribute_rec.global_attribute7,
8238                   p_global_attribute8           => l_global_attribute_rec.global_attribute8,
8239                   p_global_attribute9           => l_global_attribute_rec.global_attribute9,
8240                   p_global_attribute10          => l_global_attribute_rec.global_attribute10,
8241                   p_global_attribute11          => l_global_attribute_rec.global_attribute11,
8242                   p_global_attribute12          => l_global_attribute_rec.global_attribute12,
8243                   p_global_attribute13          => l_global_attribute_rec.global_attribute13,
8244                   p_global_attribute14          => l_global_attribute_rec.global_attribute14,
8245                   p_global_attribute15          => l_global_attribute_rec.global_attribute15,
8246                   p_global_attribute16          => l_global_attribute_rec.global_attribute16,
8247                   p_global_attribute17          => l_global_attribute_rec.global_attribute17,
8248                   p_global_attribute18          => l_global_attribute_rec.global_attribute18,
8249                   p_global_attribute19          => l_global_attribute_rec.global_attribute19,
8250                   p_global_attribute20          => l_global_attribute_rec.global_attribute20,
8251                   p_return_status               => l_gdflex_return_status);
8252 
8253       arp_util.debug('*****DUMPING ALL THE ENTITY HANDLER PARAMETERS  ***');
8254       arp_util.debug('l_cr_payment_schedule_id : '||to_char(l_cr_payment_schedule_id));
8255       arp_util.debug('l_amount_applied : '||to_char(l_amount_applied));
8256       arp_util.debug('l_apply_date : '||to_char(l_apply_date,'DD-MON-YY'));
8257       arp_util.debug('l_apply_gl_date : '||to_char(l_apply_gl_date,'DD-MON-YY'));
8258 END IF;
8259 
8260 
8261       IF l_validation_return_status <> FND_API.G_RET_STS_SUCCESS  OR
8262          l_app_validation_return_status <> FND_API.G_RET_STS_SUCCESS  OR
8263          l_default_return_status <> FND_API.G_RET_STS_SUCCESS OR
8264          l_id_conv_return_status <> FND_API.G_RET_STS_SUCCESS OR
8265          l_dflex_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
8266          l_gdflex_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8267 
8268           x_return_status :=  FND_API.G_RET_STS_ERROR;
8269       END IF;
8270 
8271       IF x_return_status <> FND_API.G_RET_STS_SUCCESS
8272          THEN
8273 
8274             ROLLBACK TO Apply_other_ac_PVT;
8275 
8276              x_return_status := FND_API.G_RET_STS_ERROR ;
8277 
8278              FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
8279                                         p_count => x_msg_count,
8280                                         p_data  => x_msg_data
8281                                        );
8282 
8283              arp_util.debug('Error(s) occurred. Rolling back and setting status to ERROR');
8284              Return;
8285        END IF;
8286 
8287        --lock the receipt before calling the entity handler
8288        arp_cash_receipts_pkg.nowaitlock_p(p_cr_id => l_cash_receipt_id);
8289 
8290        --call the entity handler
8291     BEGIN
8292        arp_process_application.other_account_application (
8293                                   p_receipt_ps_id   => l_cr_payment_schedule_id,
8294                                   p_amount_applied  => l_amount_applied,
8295                                   p_apply_date      => l_apply_date,
8296                                   p_gl_date         => l_apply_gl_date,
8297                                   p_receivables_trx_id =>l_receivables_trx_id,
8298                                   p_applied_ps_id      => p_applied_payment_schedule_id,
8299                                   p_ussgl_transaction_code => p_ussgl_transaction_code,
8300                                   p_application_ref_type => p_application_ref_type,
8301                                   p_application_ref_id   => p_application_ref_id,
8302                                   p_application_ref_num  => p_application_ref_num,
8303                                   p_secondary_application_ref_id => p_secondary_application_ref_id,
8304                                   p_comments             => p_comments,
8305                                   p_attribute_category=> l_attribute_rec.attribute_category,
8306                                   p_attribute1        => l_attribute_rec.attribute1,
8307                                   p_attribute2        => l_attribute_rec.attribute2,
8308                                   p_attribute3        => l_attribute_rec.attribute3,
8309                                   p_attribute4        => l_attribute_rec.attribute4,
8310                                   p_attribute5        => l_attribute_rec.attribute5,
8311                                   p_attribute6        => l_attribute_rec.attribute6,
8312                                   p_attribute7        => l_attribute_rec.attribute7,
8313                                   p_attribute8        => l_attribute_rec.attribute8,
8314                                   p_attribute9        => l_attribute_rec.attribute9,
8315                                   p_attribute10       => l_attribute_rec.attribute10,
8316                                   p_attribute11       => l_attribute_rec.attribute11,
8317                                   p_attribute12       => l_attribute_rec.attribute12,
8318                                   p_attribute13       => l_attribute_rec.attribute13,
8319                                   p_attribute14       => l_attribute_rec.attribute14,
8320                                   p_attribute15       => l_attribute_rec.attribute15,
8321                                   p_global_attribute_category => p_global_attribute_rec.global_attribute_category,
8322                                   p_global_attribute1 => p_global_attribute_rec.global_attribute1,
8323                                   p_global_attribute2 => p_global_attribute_rec.global_attribute2,
8324                                   p_global_attribute3 => p_global_attribute_rec.global_attribute3,
8325                                   p_global_attribute4 => p_global_attribute_rec.global_attribute4,
8326                                   p_global_attribute5 => p_global_attribute_rec.global_attribute5,
8327                                   p_global_attribute6 => p_global_attribute_rec.global_attribute6,
8328                                   p_global_attribute7 => p_global_attribute_rec.global_attribute7,
8329                                   p_global_attribute8 => p_global_attribute_rec.global_attribute8,
8330                                   p_global_attribute9 => p_global_attribute_rec.global_attribute9,
8331                                   p_global_attribute10 => p_global_attribute_rec.global_attribute10,
8332                                   p_global_attribute11 => p_global_attribute_rec.global_attribute11,
8333                                   p_global_attribute12 => p_global_attribute_rec.global_attribute12,
8334                                   p_global_attribute13 => p_global_attribute_rec.global_attribute13,
8335                                   p_global_attribute14 => p_global_attribute_rec.global_attribute14,
8336                                   p_global_attribute15 => p_global_attribute_rec.global_attribute15,
8337                                   p_global_attribute16 => p_global_attribute_rec.global_attribute16,
8338                                   p_global_attribute17 => p_global_attribute_rec.global_attribute17,
8339                                   p_global_attribute18 => p_global_attribute_rec.global_attribute18,
8340                                   p_global_attribute19 => p_global_attribute_rec.global_attribute19,
8341                                   p_global_attribute20 => p_global_attribute_rec.global_attribute20,
8342                                   p_module_name         => 'RAPI',
8343                                   p_module_version      => p_api_version,
8344                                   p_payment_set_id      => p_payment_set_id,
8345                                   -- *** OUT NOCOPY
8346                                   x_application_ref_id     => l_temp_ref_id,
8347                                   x_application_ref_num    => l_temp_ref_num,
8348                                   x_return_status          => x_return_status,
8349                                   x_msg_count              => x_msg_count,
8350                                   x_msg_data               => x_msg_data,
8351                                   p_out_rec_application_id => ln_rec_application_id,
8352                                   p_application_ref_reason => p_application_ref_reason,
8353                                   p_customer_reference     => p_customer_reference,
8354                                   p_customer_reason        => p_customer_reason,
8355                                   x_claim_reason_name      => l_claim_reason_name,
8356 				  p_called_from		   => p_called_from
8357                                    );
8358 
8359      -- The following two lines and some related changes above are done
8360      -- to resolve bug # 2707702.
8361      --
8362      -- ORASHID 13-DEC-2002
8363 
8364      p_application_ref_id  := l_temp_ref_id;
8365      p_application_ref_num := l_temp_ref_num;
8366 
8367      arp_util.debug('Application ID  :'||to_char(ln_rec_application_id));
8368      p_receivable_application_id := ln_rec_application_id;
8369      arp_util.debug('Other Accounting Application return status :'||x_return_status);
8370      EXCEPTION
8371        WHEN OTHERS THEN
8372 
8373                /*-------------------------------------------------------+
8374                 |  Handle application errors that result from trapable  |
8375                 |  error conditions. The error messages have already    |
8376                 |  been put on the error stack.                         |
8377                 +-------------------------------------------------------*/
8378 
8379                 IF (SQLCODE = -20001)
8380                 THEN
8381                      ROLLBACK TO Apply_other_ac_PVT;
8382 
8383                       --  Display_Parameters
8384 
8385                       x_return_status := FND_API.G_RET_STS_ERROR ;
8386                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
8387                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.APPLY_OTHER_ACCOUNT : '||SQLERRM);
8388                        FND_MSG_PUB.Add;
8389 
8390                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
8391                                                   p_count  =>  x_msg_count,
8392                                                   p_data   => x_msg_data
8393                                                 );
8394                       RETURN;
8395                 ELSE
8396                    RAISE;
8397                 END IF;
8398      END;
8399        /*--------------------------------+
8400         |   Standard check of p_commit   |
8401         +--------------------------------*/
8402 
8403         IF FND_API.To_Boolean( p_commit )
8404         THEN
8405             arp_util.debug('committing');
8406               Commit;
8407         END IF;
8408         arp_util.debug('Apply_other_account ()- ');
8409 EXCEPTION
8410        WHEN FND_API.G_EXC_ERROR THEN
8411 
8412                 arp_util.debug(SQLCODE, G_MSG_ERROR);
8413                 arp_util.debug(SQLERRM, G_MSG_ERROR);
8414 
8415                 ROLLBACK TO Apply_other_ac_PVT;
8416                 x_return_status := FND_API.G_RET_STS_ERROR ;
8417 
8418                -- Display_Parameters;
8419 
8420                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
8421                                            p_count       =>      x_msg_count,
8422                                            p_data        =>      x_msg_data
8423                                          );
8424 
8425         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8426 
8427                 arp_util.debug(SQLERRM, G_MSG_ERROR);
8428                 ROLLBACK TO Apply_other_ac_PVT;
8429                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8430 
8431                --  Display_Parameters;
8432 
8433                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
8434                                            p_count       =>      x_msg_count,
8435                                            p_data        =>      x_msg_data
8436                                          );
8437 
8438         WHEN OTHERS THEN
8439 
8440                /*-------------------------------------------------------+
8441                 |  Handle application errors that result from trapable  |
8442                 |  error conditions. The error messages have already    |
8443                 |  been put on the error stack.                         |
8444                 +-------------------------------------------------------*/
8445 
8446                 IF (SQLCODE = -20001)
8447                 THEN
8448 
8449                       ROLLBACK TO Apply_other_ac_PVT;
8450 
8451                       --If only one error message on the stack,
8452                       --retrive it
8453 
8454                       x_return_status := FND_API.G_RET_STS_ERROR ;
8455                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
8456                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','APPLY_OTHER_ACCOUNT : '||SQLERRM);
8457                       FND_MSG_PUB.Add;
8458 
8459                       --If only one error message on the stack,
8460                       --retrive it
8461                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
8462                                                  p_count  =>  x_msg_count,
8463                                                  p_data   => x_msg_data
8464                                                 );
8465 
8466                       RETURN;
8467 
8468                 ELSE
8469                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8470                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
8471                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','APPLY_OTHER_ACCOUNT : '||SQLERRM);
8472                       FND_MSG_PUB.Add;
8473                 END IF;
8474 
8475                 arp_util.debug(SQLCODE, G_MSG_ERROR);
8476                 arp_util.debug(SQLERRM, G_MSG_ERROR);
8477 
8478                 ROLLBACK TO Apply_other_ac_PVT;
8479 
8480                 IF      FND_MSG_PUB.Check_Msg_Level
8481                 THEN
8482                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
8483                                         l_api_name
8484                                        );
8485                 END IF;
8486 
8487              --   Display_Parameters;
8488 
8489                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
8490                                            p_count       =>      x_msg_count,
8491                                            p_data        =>      x_msg_data
8492                                          );
8493         END;
8494 
8495 PROCEDURE create_misc(
8496     -- Standard API parameters.
8497       p_api_version                  IN  NUMBER,
8498       p_init_msg_list                IN  VARCHAR2 := FND_API.G_FALSE,
8499       p_commit                       IN  VARCHAR2 := FND_API.G_FALSE,
8500       p_validation_level             IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
8501       x_return_status                OUT NOCOPY VARCHAR2 ,
8502       x_msg_count                    OUT NOCOPY NUMBER ,
8503       x_msg_data                     OUT NOCOPY VARCHAR2 ,
8504     -- Misc Receipt info. parameters
8505       p_usr_currency_code            IN  VARCHAR2 DEFAULT NULL, --the translated currency code
8506       p_currency_code                IN  VARCHAR2 DEFAULT NULL,
8507       p_usr_exchange_rate_type       IN  VARCHAR2 DEFAULT NULL,
8508       p_exchange_rate_type           IN  VARCHAR2 DEFAULT NULL,
8509       p_exchange_rate                IN  NUMBER   DEFAULT NULL,
8510       p_exchange_rate_date           IN  DATE     DEFAULT NULL,
8511       p_amount                       IN  NUMBER,
8512       p_receipt_number               IN  OUT NOCOPY VARCHAR2 ,
8513       p_receipt_date                 IN  DATE     DEFAULT NULL,
8514       p_gl_date                      IN  DATE     DEFAULT NULL,
8515       p_receivables_trx_id           IN  NUMBER   DEFAULT NULL,
8516       p_activity                     IN  VARCHAR2 DEFAULT NULL,
8517       p_misc_payment_source          IN  VARCHAR2 DEFAULT NULL,
8518       p_tax_code                     IN  VARCHAR2 DEFAULT NULL,
8519       p_vat_tax_id                   IN  VARCHAR2 DEFAULT NULL,
8520       p_tax_rate                     IN  NUMBER   DEFAULT NULL,
8521       p_tax_amount                   IN  NUMBER   DEFAULT NULL,
8522       p_deposit_date                 IN  DATE     DEFAULT NULL,
8523       p_reference_type               IN  VARCHAR2 DEFAULT NULL,
8524       p_reference_num                IN  VARCHAR2 DEFAULT NULL,
8525       p_reference_id                 IN  NUMBER   DEFAULT NULL,
8526       p_remittance_bank_account_id   IN  NUMBER   DEFAULT NULL,
8527       p_remittance_bank_account_num  IN  VARCHAR2 DEFAULT NULL,
8528       p_remittance_bank_account_name IN  VARCHAR2 DEFAULT NULL,
8529       p_receipt_method_id            IN  NUMBER   DEFAULT NULL,
8530       p_receipt_method_name          IN  VARCHAR2 DEFAULT NULL,
8531       p_doc_sequence_value           IN  NUMBER   DEFAULT NULL,
8532       p_ussgl_transaction_code       IN  VARCHAR2 DEFAULT NULL,
8533       p_anticipated_clearing_date    IN  DATE     DEFAULT NULL,
8534       p_attribute_record             IN  attribute_rec_type        DEFAULT attribute_rec_const,
8535       p_global_attribute_record      IN  global_attribute_rec_type DEFAULT global_attribute_rec_const,
8536       p_comments                     IN  VARCHAR2 DEFAULT NULL,
8537       p_org_id                       IN NUMBER  DEFAULT NULL,
8538       p_misc_receipt_id              OUT NOCOPY NUMBER,
8539       p_called_from                  IN VARCHAR2 DEFAULT NULL,
8540       p_payment_trxn_extension_id    IN ar_cash_receipts.payment_trxn_extension_id%TYPE DEFAULT NULL ) /* Bug fix 3619780*/
8541 IS
8542 l_currency_code                ar_cash_receipts.currency_code%TYPE;
8543 l_exchange_rate_type           ar_cash_receipts.exchange_rate_type%TYPE;
8544 l_exchange_rate                NUMBER;
8545 l_exchange_date                DATE;
8546 l_receipt_number               ar_cash_receipts.receipt_number%TYPE;
8547 l_amount                       NUMBER;
8548 l_receipt_date                 DATE;
8549 l_gl_date                      DATE;
8550 l_receivables_trx_id           NUMBER(15);
8551 l_vat_tax_id                   NUMBER(15);
8552 l_tax_rate                     NUMBER;
8553 l_deposit_date                 DATE;
8554 l_reference_id                 NUMBER;
8555 l_remit_bank_acct_use_id       NUMBER(15);
8556 l_receipt_method_id            NUMBER(15);
8557 l_doc_sequence_value           NUMBER(15);
8558 l_doc_sequence_id              NUMBER(15);
8559 l_distribution_set_id          NUMBER(15);
8560 l_anticipated_clearing_date    DATE;
8561 l_row_id                       VARCHAR2(30);
8562 l_attribute_rec                attribute_rec_type;
8563 l_global_attribute_rec         global_attribute_rec_type;
8564 
8565 l_api_name                     CONSTANT VARCHAR2(20) := 'Create_misc';
8566 l_api_version                  CONSTANT NUMBER       := 1.0;
8567 
8568 l_receipt_method_name          VARCHAR2(30);
8569 l_state                        VARCHAR2(30);
8570 l_creation_method              VARCHAR2(1);
8571 l_doc_seq_status               VARCHAR2(1);
8572 l_def_misc_id_return_status    VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
8573 l_misc_def_return_status       VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
8574 l_val_return_status            VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
8575 l_dflex_val_return_status      VARCHAR2(1) DEFAULT FND_API.G_RET_STS_SUCCESS;
8576 l_creation_method_code         ar_receipt_classes.creation_method_code%TYPE;
8577 /* Bug fix 2300268 */
8578 l_tax_account_id               ar_distributions.code_combination_id%TYPE;
8579 /* Bug fix 2742388 */
8580 l_crh_id                       ar_cash_receipt_history.cash_receipt_history_id%TYPE;
8581 l_org_return_status VARCHAR2(1);
8582 l_org_id                           NUMBER;
8583 l_legal_entity_id              NUMBER;  /* R12 LE uptake */
8584 l_payment_trxn_extension_id    ar_cash_receipts.payment_trxn_extension_id%TYPE;
8585 /* bichatte payment uptake */
8586 l_copy_return_status                VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
8587 l_copy_msg_count                    NUMBER;
8588 l_copy_msg_data                     VARCHAR2(2000);
8589 l_copy_pmt_trxn_extension_id        ar_cash_receipts.payment_trxn_extension_id%TYPE; /* bichatte payment uptake project */
8590 l_default_site_use                  VARCHAR2(1);
8591 l_customer_id                       NUMBER(15);
8592 l_customer_site_use_id              NUMBER(15);
8593 BEGIN
8594 
8595  --assigning the parameters to local variables
8596 	l_currency_code                := p_currency_code;
8597 	l_exchange_rate_type           := p_exchange_rate_type;
8598         l_exchange_rate                := p_exchange_rate;
8599         l_exchange_date                := trunc(p_exchange_rate_date);
8600 	l_receipt_number               := p_receipt_number;
8601         l_amount                       := p_amount;
8602 	l_receipt_date                 := trunc(p_receipt_date);
8603 	l_gl_date                      := trunc(p_gl_date);
8604 	l_receivables_trx_id           := p_receivables_trx_id;
8605 	l_vat_tax_id                   := p_vat_tax_id;
8606         l_tax_rate                     := p_tax_rate;
8607 	l_deposit_date                 := trunc(p_deposit_date);
8608 	l_reference_id                 := p_reference_id;
8609 	l_remit_bank_acct_use_id       := p_remittance_bank_account_id;
8610 	l_receipt_method_id            := p_receipt_method_id;
8611         l_receipt_method_name          := p_receipt_method_name;
8612 	l_doc_sequence_value           := p_doc_sequence_value;
8613         l_anticipated_clearing_date    := trunc(p_anticipated_clearing_date);
8614 	l_attribute_rec                := p_attribute_record;
8615 	l_global_attribute_rec         := p_global_attribute_record;
8616         l_payment_trxn_extension_id    := p_payment_trxn_extension_id;  /* payment uptake */
8617 
8618        /*------------------------------------+
8619         |   Standard start of API savepoint  |
8620         +------------------------------------*/
8621 
8622         SAVEPOINT Create_misc_PVT;
8623 
8624        /*--------------------------------------------------+
8625         |   Standard call to check for call compatibility  |
8626         +--------------------------------------------------*/
8627 
8628         IF NOT FND_API.Compatible_API_Call(
8629                                             l_api_version,
8630                                             p_api_version,
8631                                             l_api_name,
8632                                             G_PKG_NAME
8633                                           )
8634         THEN
8635               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8636         END IF;
8637 
8638        /*--------------------------------------------------------------+
8639         |   Initialize message list if p_init_msg_list is set to TRUE  |
8640         +--------------------------------------------------------------*/
8641 
8642         IF FND_API.to_Boolean( p_init_msg_list )
8643           THEN
8644               FND_MSG_PUB.initialize;
8645         END IF;
8646 
8647         IF PG_DEBUG in ('Y', 'C') THEN
8648            arp_util.debug('ar_receipt_api.create_misc()+ ');
8649         END IF;
8650        /*-----------------------------------------+
8651         |   Initialize return status to SUCCESS   |
8652         +-----------------------------------------*/
8653 
8654         x_return_status := FND_API.G_RET_STS_SUCCESS;
8655         l_doc_seq_status := FND_API.G_RET_STS_SUCCESS;
8656 
8657 
8658 
8659 /* SSA change */
8660        l_org_id            := p_org_id;
8661        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
8662        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
8663                                                 p_return_status =>l_org_return_status);
8664  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8665        x_return_status := FND_API.G_RET_STS_ERROR;
8666  ELSE
8667         /*-------------------------------------------------+
8668          | Initialize the profile option package variables |
8669          +-------------------------------------------------*/
8670 
8671            initialize_profile_globals;
8672 
8673        /*---------------------------------------------+
8674         |   ========== Start of API Body ==========   |
8675         +---------------------------------------------*/
8676 
8677    --If any value to id conversion fails then error status is returned
8678 
8679    -- ETAX: (bug 4594101) added l_receipt_date for derivation of tax rates.
8680 
8681        /*-------------------------------+
8682         |   Defaulting Ids from Values  |
8683         +-------------------------------*/
8684 
8685         ar_receipt_lib_pvt.Default_misc_ids(
8686                               p_usr_currency_code  ,
8687                               p_usr_exchange_rate_type,
8688                               p_activity,
8689                               p_reference_type,
8690                               p_reference_num,
8691                               p_tax_code,
8692                               l_receipt_method_name,
8693                               p_remittance_bank_account_name ,
8694                               p_remittance_bank_account_num ,
8695                               l_currency_code ,
8696                               l_exchange_rate_type ,
8697                               l_receivables_trx_id ,
8698                               l_reference_id,
8699                               l_vat_tax_id,
8700                               l_receipt_method_id,
8701                               l_remit_bank_acct_use_id ,
8702                               l_def_misc_id_return_status,
8703                               l_receipt_date
8704                               );
8705 
8706 
8707        /*-------------------------------+
8708         |          Defaulting           |
8709         +-------------------------------*/
8710 
8711         ar_receipt_lib_pvt.Get_misc_defaults(
8712                               l_currency_code,
8713                               l_exchange_rate_type,
8714                               l_exchange_rate,
8715                               l_exchange_date,
8716                               l_amount,
8717                               l_receipt_date,
8718                               l_gl_date,
8719                               l_remit_bank_acct_use_id,
8720                               l_deposit_date,
8721                               l_state,
8722                               l_distribution_set_id,
8723                               l_vat_tax_id,
8724                               l_tax_rate,
8725                               l_receipt_method_id,
8726                               l_receivables_trx_id,
8727                               p_tax_code,
8728                               p_tax_amount,
8729                               l_creation_method_code,
8730                               l_misc_def_return_status
8731                               );
8732 
8733        /*------------------------------------------+
8734         |  Get legal_entity_id                     |
8735         +------------------------------------------*/  /* R12 LE uptake */
8736            l_legal_entity_id := ar_receipt_lib_pvt.get_legal_entity(l_remit_bank_acct_use_id);
8737 
8738        /*-------------------------------+
8739         |         Validation            |
8740         +-------------------------------*/
8741 
8742         ar_receipt_val_pvt.Validate_misc_receipt(
8743                               l_receipt_number,
8744                               l_receipt_method_id,
8745                               l_state,
8746                               l_receipt_date,
8747                               l_gl_date,
8748                               l_deposit_date,
8749                               l_amount,
8750                               p_receivables_trx_id,
8751                               l_receivables_trx_id,
8752                               l_distribution_set_id,
8753                               p_vat_tax_id,
8754                               l_vat_tax_id,
8755                               l_tax_rate,
8756                               p_tax_amount,
8757                               p_reference_num,
8758                               p_reference_id, --original reference_id
8759                               l_reference_id,
8760                               p_reference_type,
8761                               l_remit_bank_acct_use_id,
8762                               l_anticipated_clearing_date,
8763                               l_currency_code,
8764                               l_exchange_rate_type,
8765                               l_exchange_rate,
8766                               l_exchange_date,
8767                               l_doc_sequence_value,
8768                               l_val_return_status
8769                               );
8770 
8771          --validate and default the flexfields
8772          /* Bug fix 3619780 : Don't Validate the descriptive flex field if api is called from
8773            Credit card refund */
8774 
8775          --Bug 4166986  CC_chargeback project
8776          IF NVL(p_called_from ,'X')  NOT IN
8777             ('CC_REFUND','CM_REFUND','CC_CHARGEBACK') THEN
8778             ar_receipt_lib_pvt.Validate_Desc_Flexfield(
8779                               l_attribute_rec,
8780                               'AR_CASH_RECEIPTS',
8781                               l_dflex_val_return_status
8782                               );
8783         END IF;
8784 END IF;
8785 
8786 
8787          IF l_misc_def_return_status <> FND_API.G_RET_STS_SUCCESS OR
8788             l_val_return_status <> FND_API.G_RET_STS_SUCCESS  OR
8789             l_def_misc_id_return_status <> FND_API.G_RET_STS_SUCCESS OR
8790             l_dflex_val_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8791 
8792             x_return_status := FND_API.G_RET_STS_ERROR;
8793          END IF;
8794 
8795         --Call the document sequence routine only if there have been no errors
8796         --reported so far.
8797 	IF NVL(l_creation_method_code, 'X') = 'MANUAL'
8798 		THEN
8799 		   l_creation_method := 'M';
8800 		ELSIF NVL(l_creation_method_code, 'X') = 'AUTOMATIC'
8801 		THEN
8802 		   l_creation_method := 'A';
8803 		ELSE
8804 		   l_creation_method := 'M';
8805         END IF;
8806 
8807           IF PG_DEBUG in ('Y', 'C') THEN
8808                arp_util.debug('create_misc: l_misc_def_return_status = ' || l_misc_def_return_status);
8809                arp_util.debug('create_misc: l_val_return_status = ' || l_val_return_status);
8810                arp_util.debug('create_misc: l_def_misc_id_return_status = ' || l_def_misc_id_return_status);
8811                arp_util.debug('create_misc: l_dflex_val_return_status = ' || l_dflex_val_return_status);
8812                arp_util.debug('create_misc: l_creation_method = ' || l_creation_method );
8813                arp_util.debug('create_misc: arp_global.set_of_books_id = ' || arp_global.set_of_books_id );
8814                arp_util.debug('create_misc: l_receipt_date = ' || l_receipt_date );
8815                arp_util.debug('create_misc: x_return_status = ' || x_return_status );
8816             END IF;
8817         IF x_return_status = FND_API.G_RET_STS_SUCCESS  THEN
8818 
8819            ar_receipt_lib_pvt.get_doc_seq(
8820                               222,
8821                               l_receipt_method_name,
8822                               arp_global.set_of_books_id,
8823                               l_creation_method,
8824                               l_receipt_date,
8825                               l_doc_sequence_value,
8826                               l_doc_sequence_id,
8827                               l_doc_seq_status
8828                                );
8829         END IF;
8830 
8831       --If receipt number has not been passed in the document sequence value is
8832       --used as the receipt number.
8833         IF l_receipt_number IS NULL THEN
8834           IF l_doc_sequence_value IS NOT NULL THEN
8835             l_receipt_number := l_doc_sequence_value;
8836 			-- Copy the Receipt Number in the out NOCOPY parameter
8837 	        p_receipt_number := l_receipt_number;
8838             --warning message
8839              IF FND_MSG_PUB.Check_Msg_Level(G_MSG_SUCCESS)
8840               THEN
8841                  FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_NUM_DFLT_DOC_SEQ');
8842                  FND_MSG_PUB.Add;
8843              END IF;
8844           ELSE
8845             IF PG_DEBUG in ('Y', 'C') THEN
8846                arp_util.debug('create_misc: ' || 'Receipt Number is null ');
8847             END IF;
8848             --raise error message
8849               FND_MESSAGE.SET_NAME('AR','AR_RAPI_RCPT_NUM_NULL');
8850               FND_MSG_PUB.Add;
8851               x_return_status := FND_API.G_RET_STS_ERROR;
8852           END IF;
8853         END IF;
8854 
8855        /*------------------------------------------------------------+
8856         |  If any errors - including validation failures - occurred, |
8857         |  rollback any changes and return an error status.          |
8858         +------------------------------------------------------------*/
8859 
8860          IF (
8861               x_return_status         <> FND_API.G_RET_STS_SUCCESS
8862               OR l_doc_seq_status     <> FND_API.G_RET_STS_SUCCESS
8863              )
8864              THEN
8865 
8866               ROLLBACK TO Create_misc_PVT;
8867 
8868               x_return_status := FND_API.G_RET_STS_ERROR ;
8869 
8870               FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
8871                                         p_count => x_msg_count,
8872                                         p_data  => x_msg_data);
8873 
8874               IF PG_DEBUG in ('Y', 'C') THEN
8875                  arp_util.debug('create_misc: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
8876               END IF;
8877              Return;
8878         END IF;
8879           IF PG_DEBUG in ('Y', 'C') THEN
8880              arp_util.debug('create_misc: ' || 'x_return_status '||x_return_status);
8881           END IF;
8882 
8883         /* Bug fix 2300268
8884            Get the tax account id corresponding to the vat_tax_id */
8885            IF l_vat_tax_id IS NOT NULL THEN
8886             /* 5955921 Replaced select statement */
8887              SELECT tax_account_ccid
8888               INTO   l_tax_account_id
8889              FROM zx_accounts
8890              WHERE  tax_account_entity_id = l_vat_tax_id
8891               AND  tax_account_entity_code = 'RATES'
8892               AND  internal_organization_id = l_org_id;
8893 
8894 	     /*SELECT tax_account_id
8895              INTO   l_tax_account_id
8896              FROM   ar_vat_tax
8897              WHERE  vat_tax_id = l_vat_tax_id;*/
8898           ELSE
8899              l_tax_account_id := NULL;
8900           END IF;
8901        /* End Bug fix 2300268 */
8902 
8903        /*-------------------------------+
8904         |   Call to the Entity Handler  |
8905         +-------------------------------*/
8906           IF PG_DEBUG in ('Y', 'C') THEN
8907              arp_util.debug('create_misc: ' || ' orig payment_trxn_extension_id '||l_payment_trxn_extension_id);
8908           END IF;
8909 /* bichatte payment uptake copy extn start */
8910 
8911       IF ( l_creation_method = 'A' and l_payment_trxn_extension_id is NULL) THEN
8912 
8913                arp_util.debug('Create_cash_122: ' || l_creation_method);
8914                arp_util.debug('Create_cash_122: ' || l_payment_trxn_extension_id );
8915               FND_MESSAGE.SET_NAME('AR','AR_CC_AUTH_FAILED');
8916               FND_MSG_PUB.Add;
8917               x_return_status := FND_API.G_RET_STS_ERROR;
8918       END IF;
8919 
8920       IF l_creation_method = 'A'  THEN
8921           arp_standard.debug('calling copy  Extension....');
8922        /* 5955921 */
8923        select pay_from_customer,customer_site_use_id
8924        into   l_customer_id,l_customer_site_use_id
8925        from ar_cash_receipts
8926        --where payment_trxn_extension_id = l_payment_trxn_extension_id;
8927        --Bug : 6855895
8928        where cash_receipt_id = l_reference_id;
8929 
8930      IF PG_DEBUG in ('Y','C') THEN
8931        arp_util.debug ( 'the value of pmt_trxn_extn_id '|| l_payment_trxn_extension_id);
8932        arp_util.debug ( 'the value of customer_id      '|| l_customer_id);
8933        arp_util.debug ( 'the value of receipt_met_id   '|| l_receipt_method_id);
8934        arp_util.debug ( 'the value of org_id           '|| l_org_id);
8935        arp_util.debug ( 'the value of cust_site_use_id '|| l_customer_site_use_id);
8936        arp_util.debug ( 'the value of rec_number       '|| l_receipt_number);
8937      END IF;
8938 
8939 	   Copy_payment_extension (
8940               p_payment_trxn_extension_id => l_payment_trxn_extension_id,
8941               p_customer_id => l_customer_id,
8942               p_receipt_method_id =>l_receipt_method_id,
8943               p_org_id =>l_org_id,
8944               p_customer_site_use_id  =>l_customer_site_use_id,
8945               p_receipt_number=> l_receipt_number,
8946               x_msg_count => l_copy_msg_count,
8947               x_msg_data => l_copy_msg_data,
8948               x_return_status =>l_copy_return_status,
8949               o_payment_trxn_extension_id =>l_copy_pmt_trxn_extension_id
8950                  );
8951 
8952          IF l_copy_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
8953                arp_util.debug('Create_cash_123: ' );
8954              FND_MESSAGE.set_name('AR', 'AR_CC_AUTH_FAILED');
8955              FND_MSG_PUB.Add;
8956               x_return_status := FND_API.G_RET_STS_ERROR;
8957          END IF;
8958            l_payment_trxn_extension_id := l_copy_pmt_trxn_extension_id;
8959 
8960           arp_standard.debug('calling copy  Extension  end ....');
8961          arp_standard.debug('calling copy  Extension  end ...2'|| to_char(l_copy_pmt_trxn_extension_id));
8962 
8963      END IF;
8964 
8965 
8966 /* Assign NUll to pmt_trxn_extension_id for 'CC_Chargeback trxn'sCC_CHARGEBACK */
8967 
8968        IF p_called_from = 'CC_CHARGEBACK' THEN
8969 
8970            l_copy_pmt_trxn_extension_id := null;
8971          IF PG_DEBUG in ('Y','C') THEN
8972           arp_standard.debug('calling copy  Extension  end ....');
8973          END IF;
8974 
8975        END IF;
8976 
8977 
8978 /* bichatte payment uptake copy extn end */
8979        BEGIN
8980          arp_process_misc_receipts.insert_misc_receipt(
8981 	                       p_currency_code
8982                                        => l_currency_code,
8983 	                       p_amount
8984                                        => p_amount,
8985 	                       p_receivables_trx_id
8986                                        => l_receivables_trx_id,
8987 	                       p_misc_payment_source
8988                                        => p_misc_payment_source,
8989 	                       p_receipt_number
8990                                        => l_receipt_number,
8991 	                       p_receipt_date
8992                                        => l_receipt_date,
8993 	                       p_gl_date
8994                                        => l_gl_date,
8995 	                       p_comments
8996                                        => p_comments,
8997 	                       p_exchange_rate_type
8998                                        => l_exchange_rate_type,
8999 	                       p_exchange_rate
9000                                        => l_exchange_rate,
9001                                p_exchange_date
9002                                        => l_exchange_date,
9003 		               p_batch_id
9004                                        => null,
9005 	                       p_attribute_category
9006                                        => l_attribute_rec.attribute_category,
9007 	                       p_attribute1
9008                                        => l_attribute_rec.attribute1,
9009 	                       p_attribute2
9010                                        => l_attribute_rec.attribute2,
9011 	                       p_attribute3
9012                                        => l_attribute_rec.attribute3,
9013 	                       p_attribute4
9014                                        => l_attribute_rec.attribute4,
9015 	                       p_attribute5
9016                                        => l_attribute_rec.attribute5,
9017 	                       p_attribute6
9018                                        => l_attribute_rec.attribute6,
9019 	                       p_attribute7
9020                                        => l_attribute_rec.attribute7,
9021 	                       p_attribute8
9022                                        => l_attribute_rec.attribute8,
9023 	                       p_attribute9
9024                                        => l_attribute_rec.attribute9,
9025 	                       p_attribute10
9026                                        => l_attribute_rec.attribute10,
9027 	                       p_attribute11
9028                                        => l_attribute_rec.attribute11,
9029 	                       p_attribute12
9030                                        => l_attribute_rec.attribute12,
9031 	                       p_attribute13
9032                                        => l_attribute_rec.attribute13,
9033                                p_attribute14
9034                                        => l_attribute_rec.attribute14,
9035 	                       p_attribute15
9036                                        => l_attribute_rec.attribute15,
9037 	                       p_remittance_bank_account_id
9038                                        => l_remit_bank_acct_use_id,
9039 	                       p_deposit_date
9040                                        => l_deposit_date,
9041 	                       p_receipt_method_id
9042                                        => l_receipt_method_id,
9043 	                       p_doc_sequence_value
9044                                        => l_doc_sequence_value,
9045 	                       p_doc_sequence_id
9046                                        => l_doc_sequence_id,
9047 	                       p_distribution_set_id
9048                                        => l_distribution_set_id,
9049 	                       p_reference_type
9050                                        => p_reference_type,
9051 	                       p_reference_id
9052                                        => l_reference_id,
9053 	                       p_vat_tax_id
9054                                        => l_vat_tax_id,
9055                                p_ussgl_transaction_code
9056                                        => p_ussgl_transaction_code,
9057 	                       p_anticipated_clearing_date
9058                                        => l_anticipated_clearing_date, /* Bug fix 3135407 */
9059 	                       p_global_attribute1
9060                                        => l_global_attribute_rec.global_attribute1,
9061 	                       p_global_attribute2
9062                                        => l_global_attribute_rec.global_attribute2,
9063 	                       p_global_attribute3
9064                                        => l_global_attribute_rec.global_attribute3,
9065 	                       p_global_attribute4
9066                                        => l_global_attribute_rec.global_attribute4,
9067 	                       p_global_attribute5
9068                                        => l_global_attribute_rec.global_attribute5,
9069 	                       p_global_attribute6
9070                                        => l_global_attribute_rec.global_attribute6,
9071 	                       p_global_attribute7
9072                                        => l_global_attribute_rec.global_attribute7,
9073 	                       p_global_attribute8
9074                                        => l_global_attribute_rec.global_attribute8,
9075 	                       p_global_attribute9
9076                                        => l_global_attribute_rec.global_attribute9,
9077 	                       p_global_attribute10
9078                                        => l_global_attribute_rec.global_attribute10,
9079 	                       p_global_attribute11
9080                                        => l_global_attribute_rec.global_attribute11,
9081 	                       p_global_attribute12
9082                                        => l_global_attribute_rec.global_attribute12,
9083 	                       p_global_attribute13
9084                                        => l_global_attribute_rec.global_attribute13,
9085                                p_global_attribute14
9086                                        => l_global_attribute_rec.global_attribute14,
9087 	                       p_global_attribute15
9088                                        => l_global_attribute_rec.global_attribute15,
9089 	                       p_global_attribute16
9090                                        => l_global_attribute_rec.global_attribute16,
9091 	                       p_global_attribute17
9092                                        => l_global_attribute_rec.global_attribute17,
9093 	                       p_global_attribute18
9094                                        => l_global_attribute_rec.global_attribute18,
9095 	                       p_global_attribute19
9096                                        => l_global_attribute_rec.global_attribute19,
9097 	                       p_global_attribute20
9098                                        => l_global_attribute_rec.global_attribute20,
9099 	                       p_global_attribute_category
9100                                      => l_global_attribute_rec.global_attribute_category,
9101  	                       p_cr_id
9102                                        => p_misc_receipt_id,
9103 	                       p_row_id
9104                                        => l_row_id,
9105 	                       p_form_name
9106                                        => 'RAPI',
9107 	                       p_form_version
9108                                        => p_api_version,
9109                                p_tax_rate
9110                                        => l_tax_rate,
9111                                p_gl_tax_acct
9112                                        => l_tax_account_id ,/* Bug fix 2300268 */
9113                                p_crh_id
9114                                        => l_crh_id, /* Bug fix 2742388 */
9115 			       p_legal_entity_id => l_legal_entity_id, /* R12 LE uptake */
9116                                p_payment_trxn_extension_id => l_copy_pmt_trxn_extension_id
9117                                 );
9118        EXCEPTION
9119          WHEN OTHERS THEN
9120 
9121                /*-------------------------------------------------------+
9122                 |  Handle application errors that result from trapable  |
9123                 |  error conditions. The error messages have already    |
9124                 |  been put on the error stack.                         |
9125                 +-------------------------------------------------------*/
9126 
9127                 IF (SQLCODE = -20001)
9128                 THEN
9129                      ROLLBACK TO Create_misc_PVT;
9130 
9131                       --  Display_Parameters;
9132                       x_return_status := FND_API.G_RET_STS_ERROR ;
9133                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
9134                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT',
9135                            'ARP_PROCESS_MISC_RECEIPTS.INSERT_MISC_RECEIPT : '||SQLERRM);
9136                        FND_MSG_PUB.Add;
9137 
9138                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
9139                                                   p_count  =>  x_msg_count,
9140                                                   p_data   => x_msg_data
9141                                                 );
9142                       RETURN;
9143                 ELSE
9144                    RAISE;
9145                 END IF;
9146 
9147          END;
9148 
9149             IF PG_DEBUG in ('Y', 'C') THEN
9150                arp_util.debug('create_misc: ' || 'Misc Receipt id : '||to_char(p_misc_receipt_id));
9151             END IF;
9152 
9153        /*-------------------------------------------------------+
9154         | FND_MSG_PUB.Count_And_Get used  get the count of mesg.|
9155         | in the message stack. If there is only one message in |
9156         | the stack it retrieves this message                   |
9157         +-------------------------------------------------------*/
9158 
9159           FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
9160                                    p_count => x_msg_count,
9161                                    p_data  => x_msg_data
9162                                  );
9163 
9164 
9165        /*--------------------------------+
9166         |   Standard check of p_commit   |
9167         +--------------------------------*/
9168 
9169         IF FND_API.To_Boolean( p_commit )
9170         THEN
9171             IF PG_DEBUG in ('Y', 'C') THEN
9172                arp_util.debug('create_misc: ' || 'committing');
9173             END IF;
9174             Commit;
9175         END IF;
9176 
9177         IF PG_DEBUG in ('Y', 'C') THEN
9178            arp_util.debug('Create_Misc_Receipt()- ');
9179         END IF;
9180 
9181 
9182 
9183 
9184 
9185 
9186 EXCEPTION
9187        WHEN FND_API.G_EXC_ERROR THEN
9188 
9189                 IF PG_DEBUG in ('Y', 'C') THEN
9190                    arp_util.debug('create_misc: ' || SQLCODE, G_MSG_ERROR);
9191                    arp_util.debug('create_misc: ' || SQLERRM, G_MSG_ERROR);
9192                 END IF;
9193 
9194                 ROLLBACK TO Create_misc_PVT;
9195                 x_return_status := FND_API.G_RET_STS_ERROR ;
9196 
9197                 --Display_Parameters;
9198 
9199                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
9200                                            p_count       =>      x_msg_count,
9201                                            p_data        =>      x_msg_data
9202                                          );
9203 
9204         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9205 
9206                 IF PG_DEBUG in ('Y', 'C') THEN
9207                    arp_util.debug('create_misc: ' || SQLERRM, G_MSG_ERROR);
9208                 END IF;
9209                 ROLLBACK TO Create_misc_PVT;
9210                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9211 
9212                --  Display_Parameters;
9213 
9214                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
9215                                            p_count       =>      x_msg_count,
9216                                            p_data        =>      x_msg_data
9217                                          );
9218 
9219         WHEN OTHERS THEN
9220 
9221                /*-------------------------------------------------------+
9222                 |  Handle application errors that result from trapable  |
9223                 |  error conditions. The error messages have already    |
9224                 |  been put on the error stack.                         |
9225                 +-------------------------------------------------------*/
9226 
9227                 IF (SQLCODE = -20001)
9228                 THEN
9229                      ROLLBACK TO Create_misc_PVT;
9230 
9231                       --  Display_Parameters;
9232                       x_return_status := FND_API.G_RET_STS_ERROR ;
9233                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
9234                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','CREATE_MISC : '||SQLERRM);
9235                       FND_MSG_PUB.Add;
9236 
9237                       FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
9238                                                  p_count  =>  x_msg_count,
9239                                                  p_data   => x_msg_data
9240                                                 );
9241                       RETURN;
9242                 ELSE
9243                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9244                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
9245                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','CREATE_MISC : '||SQLERRM);
9246                       FND_MSG_PUB.Add;
9247                 END IF;
9248 
9249                 IF PG_DEBUG in ('Y', 'C') THEN
9250                    arp_util.debug('create_misc: ' || SQLCODE);
9251                    arp_util.debug('create_misc: ' || SQLERRM);
9252                 END IF;
9253 
9254                 ROLLBACK TO Create_misc_PVT;
9255 
9256 
9257                 IF      FND_MSG_PUB.Check_Msg_Level
9258                 THEN
9259                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
9260                                         l_api_name
9261                                        );
9262                 END IF;
9263 
9264              --   Display_Parameters;
9265                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
9266                                            p_count       =>      x_msg_count,
9267                                            p_data        =>      x_msg_data
9268                                          );
9269 
9270 END Create_misc;
9271 
9272 
9273 PROCEDURE set_profile_for_testing(p_profile_doc_seq            VARCHAR2,
9274                                   p_profile_enable_cc          VARCHAR2,
9275                                   p_profile_appln_gl_date_def  VARCHAR2,
9276                                   p_profile_amt_applied_def    VARCHAR2,
9277                                   p_profile_cc_rate_type       VARCHAR2,
9278                                   p_profile_dsp_inv_rate       VARCHAR2,
9279                                   p_profile_create_bk_charges  VARCHAR2,
9280                                   p_profile_def_x_rate_type    VARCHAR2,
9281                                   p_pay_unrelated_inv_flag     VARCHAR2,
9282                                   p_unearned_discount          VARCHAR2) IS
9283 BEGIN
9284 	ar_receipt_lib_pvt.pg_profile_doc_seq           := p_profile_doc_seq;
9285 	ar_receipt_lib_pvt.pg_profile_enable_cc         := p_profile_enable_cc;
9286 	ar_receipt_lib_pvt.pg_profile_appln_gl_date_def := p_profile_appln_gl_date_def;
9287 	ar_receipt_lib_pvt.pg_profile_amt_applied_def   := p_profile_amt_applied_def;
9288 	ar_receipt_lib_pvt.pg_profile_cc_rate_type      := p_profile_cc_rate_type;
9289 	ar_receipt_lib_pvt.pg_profile_dsp_inv_rate      := p_profile_dsp_inv_rate;
9290 	ar_receipt_lib_pvt.pg_profile_create_bk_charges := p_profile_create_bk_charges;
9291 	ar_receipt_lib_pvt.pg_profile_def_x_rate_type   := p_profile_def_x_rate_type;
9292 	arp_global.sysparam.pay_unrelated_invoices_flag := p_pay_unrelated_inv_flag;
9293 	arp_global.sysparam.unearned_discount           := p_unearned_discount;
9294 
9295 
9296 END set_profile_for_testing;
9297 
9298 PROCEDURE Apply_Open_Receipt(
9299 -- Standard API parameters.
9300       p_api_version                  IN  NUMBER,
9301       p_init_msg_list                IN  VARCHAR2,
9302       p_commit                       IN  VARCHAR2,
9303       p_validation_level             IN  NUMBER,
9304       x_return_status                OUT NOCOPY VARCHAR2,
9305       x_msg_count                    OUT NOCOPY NUMBER,
9306       x_msg_data                     OUT NOCOPY VARCHAR2,
9307  --  Receipt application parameters.
9308       p_cash_receipt_id              IN ar_cash_receipts.cash_receipt_id%TYPE,
9309       p_receipt_number               IN ar_cash_receipts.receipt_number%TYPE,
9310       p_applied_payment_schedule_id  IN ar_payment_schedules.payment_schedule_id%TYPE,
9311       p_open_cash_receipt_id         IN ar_cash_receipts.cash_receipt_id%TYPE,
9312       p_open_receipt_number          IN ar_cash_receipts.receipt_number%TYPE,
9313       p_open_rec_app_id              IN ar_receivable_applications.receivable_application_id%TYPE,
9314       p_amount_applied               IN ar_receivable_applications.amount_applied%TYPE,
9315       p_apply_date    IN ar_receivable_applications.apply_date%TYPE,
9316       p_apply_gl_date                IN ar_receivable_applications.gl_date%TYPE,
9317       p_ussgl_transaction_code       IN ar_receivable_applications.ussgl_transaction_code%TYPE,
9318       p_called_from                  IN VARCHAR2 ,
9319       p_attribute_rec                IN attribute_rec_type,
9320 	 -- ******* Global Flexfield parameters *******
9321       p_global_attribute_rec  IN global_attribute_rec_type,
9322       p_comments                     IN ar_receivable_applications.comments%TYPE,
9323       p_org_id             IN NUMBER  DEFAULT NULL,
9324       x_application_ref_num          OUT NOCOPY ar_receivable_applications.application_ref_num%TYPE,
9325       x_receivable_application_id    OUT NOCOPY ar_receivable_applications.receivable_application_id%TYPE,
9326       x_applied_rec_app_id           OUT NOCOPY ar_receivable_applications.receivable_application_id%TYPE,
9327       x_acctd_amount_applied_from    OUT NOCOPY ar_receivable_applications.acctd_amount_applied_from%TYPE,
9328       x_acctd_amount_applied_to      OUT NOCOPY ar_receivable_applications.acctd_amount_applied_to%TYPE
9329       ) IS
9330 l_api_name       CONSTANT VARCHAR2(20) := 'Apply_Open_Receipt';
9331 l_api_version    CONSTANT NUMBER       := 1.0;
9332 l_cash_receipt_id  NUMBER;
9333 l_receipt_number             ar_cash_receipts.receipt_number%TYPE;
9334 l_open_cash_receipt_id    ar_cash_receipts.cash_receipt_id%TYPE;
9335 l_open_receipt_number     ar_cash_receipts.receipt_number%TYPE;
9336 l_applied_payment_schedule_id ar_payment_schedules.payment_schedule_id%TYPE;
9337 l_open_rec_app_id  ar_receivable_applications.receivable_application_id%TYPE;
9338 l_cr_gl_date       DATE;
9339 l_open_cr_gl_date  DATE;
9340 l_cr_date          DATE;
9341 l_last_receipt_date DATE;
9342 l_cr_amount        NUMBER;
9343 l_cr_unapp_amount  NUMBER;
9344 l_cr_currency VARCHAR2(15);
9345 l_open_cr_currency VARCHAR2(15);
9346 l_apply_gl_date DATE;
9347 l_amount_applied NUMBER;
9348 l_reapply_amount NUMBER;
9349 l_open_amount_applied NUMBER;
9350 l_cr_payment_schedule_id  NUMBER;
9351 l_open_applied_ps_id NUMBER;
9352 l_unapplied_cash  NUMBER;
9353 l_open_cr_ps_id           NUMBER;
9354 l_apply_date              DATE;
9355 l_cr_customer_id          NUMBER;
9356 l_open_cr_customer_id     NUMBER;
9357 
9358 l_def_return_status  VARCHAR2(1);
9359 l_val_return_status VARCHAR2(1);
9360 l_dflex_val_return_status  VARCHAR2(1);
9361 l_attribute_rec         attribute_rec_type;
9362 l_global_attribute_rec  global_attribute_rec_type;
9363 l_remi_bank_acct_use_id    NUMBER;
9364 l_receipt_method_id             NUMBER;
9365 l_application_ref_id      ar_receivable_applications.application_ref_id%TYPE;
9366 l_application_ref_num     ar_receivable_applications.application_ref_num%TYPE;
9367 l_secondary_app_ref_id    ar_receivable_applications.secondary_application_ref_id%TYPE;
9368 l_application_ref_reason  ar_receivable_applications.application_ref_reason%TYPE;
9369 l_customer_reference      ar_receivable_applications.customer_reference%TYPE;
9370 l_customer_reason         ar_receivable_applications.customer_reason%TYPE;
9371 l_act_application_ref_id   ar_receivable_applications.application_ref_id%TYPE;
9372 l_act_application_ref_num  ar_receivable_applications.application_ref_num%TYPE;
9373 l_act_application_ref_type ar_receivable_applications.application_ref_type%TYPE;
9374 l_act_secondary_app_ref_id ar_receivable_applications.secondary_application_ref_id%TYPE;
9375 l_reapply_rec_trx_id      ar_receivables_trx.receivables_trx_id%TYPE;
9376 l_netting_rec_trx_id      CONSTANT ar_receivables_trx.receivables_trx_id%TYPE := -16;
9377 l_reapply_rec_app_id      ar_receivable_applications.receivable_application_id%TYPE;
9378 l_net_rec_app_id          ar_receivable_applications.receivable_application_id%TYPE;
9379 l_open_net_rec_app_id     ar_receivable_applications.receivable_application_id%TYPE;
9380 l_return_status           VARCHAR2(1);
9381 l_msg_count               NUMBER;
9382 l_reapply_msg_count       NUMBER;
9383 l_unapply_msg_count       NUMBER;
9384 l_act1_msg_count          NUMBER;
9385 l_act2_msg_count          NUMBER;
9386 l_msg_data                VARCHAR2(2000);
9387 l_called_from		  VARCHAR2(100);
9388 
9389 l_app_rec		  ar_receivable_applications%ROWTYPE;
9390 l_org_return_status VARCHAR2(1);
9391 l_org_id                           NUMBER;
9392 
9393 BEGIN
9394 
9395        /*------------------------------------+
9396         |   Standard start of API savepoint  |
9397         +------------------------------------*/
9398 
9399       SAVEPOINT Apply_Open_Receipt_PVT;
9400 
9401        /*--------------------------------------------------+
9402         |   Standard call to check for call compatibility  |
9403         +--------------------------------------------------*/
9404 
9405         IF NOT FND_API.Compatible_API_Call(
9406                                             l_api_version,
9407                                             p_api_version,
9408                                             l_api_name,
9409                                             G_PKG_NAME
9410                                           )
9411         THEN
9412               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9413         END IF;
9414 
9415        /*--------------------------------------------------------------+
9416         |   Initialize message list if p_init_msg_list is set to TRUE  |
9417         +--------------------------------------------------------------*/
9418 
9419         IF FND_API.to_Boolean( p_init_msg_list )
9420           THEN
9421               FND_MSG_PUB.initialize;
9422         END IF;
9423 
9424 
9425         IF PG_DEBUG in ('Y', 'C') THEN
9426            arp_util.debug('Apply_Open_Receipt ()+ ');
9427         END IF;
9428        /*-----------------------------------------+
9429         |   Initialize return status to SUCCESS   |
9430         +-----------------------------------------*/
9431 
9432         x_return_status := FND_API.G_RET_STS_SUCCESS;
9433 
9434 
9435 
9436 
9437 /* SSA change */
9438        l_org_id            := p_org_id;
9439        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
9440        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
9441                                                 p_return_status =>l_org_return_status);
9442  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9443        x_return_status := FND_API.G_RET_STS_ERROR;
9444  ELSE
9445         /*-------------------------------------------------+
9446          | Initialize the profile option package variables |
9447          +-------------------------------------------------*/
9448 
9449         initialize_profile_globals;
9450 
9451        /*---------------------------------------------+
9452         |   ========== Start of API Body ==========   |
9453         +---------------------------------------------*/
9454 
9455         l_cash_receipt_id        := p_cash_receipt_id;
9456         l_receipt_number         := p_receipt_number;
9457         l_open_cash_receipt_id   := p_open_cash_receipt_id;
9458         l_open_receipt_number    := p_open_receipt_number;
9459         l_open_rec_app_id        := p_open_rec_app_id;
9460         l_applied_payment_schedule_id := p_applied_payment_schedule_id;
9461         l_amount_applied         := p_amount_applied;
9462         l_apply_date             := trunc(p_apply_date);
9463         l_apply_gl_date          := trunc(p_apply_gl_date);
9464         l_attribute_rec          := p_attribute_rec;
9465         l_global_attribute_rec   := p_global_attribute_rec;
9466 
9467 
9468           /*---------------------+
9469            |                     |
9470            |    DEFAULTING       |
9471            |                     |
9472            +---------------------*/
9473 
9474         ar_receipt_lib_pvt.default_open_receipt(
9475                             l_cash_receipt_id,
9476                             l_receipt_number,
9477                             l_applied_payment_schedule_id,
9478                             l_open_cash_receipt_id,
9479                             l_open_receipt_number,
9480                             l_apply_gl_date,
9481                             l_open_rec_app_id,
9482                             l_cr_payment_schedule_id,
9483                             l_last_receipt_date,
9484                             l_open_applied_ps_id,
9485                             l_unapplied_cash,
9486                             l_open_amount_applied,
9487                             l_reapply_rec_trx_id,
9488                             l_application_ref_num,
9489                             l_secondary_app_ref_id,
9490                             l_application_ref_reason,
9491                             l_customer_reference,
9492                             l_customer_reason,
9493                             l_cr_gl_date,
9494                             l_open_cr_gl_date,
9495                             l_cr_currency,
9496                             l_open_cr_currency,
9497                             l_cr_customer_id,
9498                             l_open_cr_customer_id,
9499                             l_def_return_status);
9500 
9501    IF PG_DEBUG in ('Y', 'C') THEN
9502       arp_util.debug('Apply_Open_Receipt: ' || 'Defaulting Ids Return_status = '||l_def_return_status);
9503    END IF;
9504           /*---------------------+
9505            |                     |
9506            |    VALIDATION       |
9507            |                     |
9508            +---------------------*/
9509     --The defaulting routine will raise error only if there is an error in
9510     --defaulting for any of the two receipts.
9511     --So in this case there is no point in calling the validation routines as
9512     --at least one of the two main entities are invalid.
9513 
9514     IF l_def_return_status = FND_API.G_RET_STS_SUCCESS
9515       THEN
9516           ar_receipt_val_pvt.Validate_open_receipt_info(
9517                       p_cash_receipt_id         =>  l_cash_receipt_id
9518                     , p_open_cash_receipt_id    =>  l_open_cash_receipt_id
9519                     , p_apply_date              =>  l_apply_date
9520                     , p_apply_gl_date           =>  l_apply_gl_date
9521                     , p_cr_gl_date              =>  l_cr_gl_date
9522                     , p_open_cr_gl_date         =>  l_open_cr_gl_date
9523                     , p_cr_date                 =>  l_last_receipt_date
9524                     , p_amount_applied          =>  l_amount_applied
9525                     , p_other_amount_applied    =>  l_open_amount_applied
9526                     , p_receipt_currency        =>  l_cr_currency
9527                     , p_open_receipt_currency   =>  l_open_cr_currency
9528                     , p_cr_customer_id          =>  l_cr_customer_id
9529                     , p_open_cr_customer_id     =>  l_open_cr_customer_id
9530                     , p_unapplied_cash          =>  l_unapplied_cash
9531                     , p_called_from             =>  p_called_from -- bug 2897244
9532                     , p_return_status           =>  l_val_return_status);
9533 
9534        IF PG_DEBUG in ('Y', 'C') THEN
9535           arp_util.debug('Apply_Open_Receipt: ' || 'Validation Return_status = '||l_val_return_status);
9536        END IF;
9537     END IF;
9538 
9539          --validate and default the flexfields
9540         ar_receipt_lib_pvt.Validate_Desc_Flexfield(
9541                                         l_attribute_rec,
9542                                         'AR_RECEIVABLE_APPLICATIONS',
9543                                         l_dflex_val_return_status
9544                                                 );
9545 
9546       arp_util.debug('*****DUMPING ALL THE ENTITY HANDLER PARAMETERS  ***');
9547       arp_util.debug('l_cr_payment_schedule_id : '||to_char(l_cr_payment_schedule_id));
9548       arp_util.debug('l_amount_applied : '||to_char(l_amount_applied));
9549       arp_util.debug('l_apply_date : '||to_char(l_apply_date,'DD-MON-YY'));
9550       arp_util.debug('l_apply_gl_date : '||to_char(l_apply_gl_date,'DD-MON-YY'));
9551 END IF;
9552 
9553 
9554       IF l_val_return_status <> FND_API.G_RET_STS_SUCCESS  OR
9555          l_def_return_status <> FND_API.G_RET_STS_SUCCESS OR
9556          l_dflex_val_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9557           x_return_status := FND_API.G_RET_STS_ERROR;
9558        END IF;
9559 
9560        IF x_return_status <> FND_API.G_RET_STS_SUCCESS
9561          THEN
9562 
9563             ROLLBACK TO Apply_Open_Receipt_PVT;
9564 
9565              x_return_status := FND_API.G_RET_STS_ERROR ;
9566 
9567              FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
9568                                         p_count => x_msg_count,
9569                                         p_data  => x_msg_data
9570                                        );
9571 
9572              IF PG_DEBUG in ('Y', 'C') THEN
9573                 arp_util.debug('Apply_Open_Receipt: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
9574              END IF;
9575              Return;
9576         END IF;
9577 
9578       --Dump the input variables to the entity handler
9579     IF PG_DEBUG in ('Y', 'C') THEN
9580        arp_util.debug('Apply_Open_Receipt: ' || 'l_cr_payment_schedule_id      : '||to_char(l_cr_payment_schedule_id));
9581        arp_util.debug('Apply_Open_Receipt: ' || 'l_applied_payment_schedule_id : '||to_char(l_applied_payment_schedule_id));
9582        arp_util.debug('Apply_Open_Receipt: ' || 'l_amount_applied              : '||to_char(l_amount_applied));
9583        arp_util.debug('Apply_Open_Receipt: ' || 'l_cr_currency                 : '||l_cr_currency);
9584        arp_util.debug('Apply_Open_Receipt: ' || 'l_open_cr_currency            : '||l_open_cr_currency);
9585        arp_util.debug('l_apply_date                  : '||to_char(l_apply_date,'DD-MON-YY'));
9586        arp_util.debug('l_apply_gl_date               : '||to_char(l_apply_gl_date,'DD-MON-YY'));
9587     END IF;
9588 
9589      --lock both receipts before calling the entity handlers
9590        arp_cash_receipts_pkg.nowaitlock_p(p_cr_id => l_cash_receipt_id);
9591        arp_cash_receipts_pkg.nowaitlock_p(p_cr_id => l_open_cash_receipt_id);
9592 
9593           /*-------------------------------+
9594            |                               |
9595            |    Unapply open receipt       |
9596            |                               |
9597            +-------------------------------*/
9598      -- 1. Unapply open receipt if on account or claim , reapply any difference
9599 
9600      l_reapply_amount := (l_open_amount_applied + l_amount_applied);
9601      /* bug 5440979 . Passed p_customer_reference in call to Apply_on_account */
9602      pg_update_claim_amount := l_reapply_amount * -1; /* Bug 4170060 */
9603 
9604      IF l_open_applied_ps_id = -1 THEN
9605          Unapply_on_account(
9606               p_api_version               =>  1.0,
9607               p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL,
9608               x_return_status             =>  x_return_status ,
9609               x_msg_count                 =>  l_unapply_msg_count,
9610               x_msg_data                  =>  x_msg_data ,
9611               p_cash_receipt_id           =>  l_open_cash_receipt_id,
9612               p_receivable_application_id =>  l_open_rec_app_id,
9613               p_reversal_gl_date          =>  l_apply_gl_date
9614               );
9615 
9616          IF (x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
9617            IF (l_reapply_amount <> 0) THEN
9618              Apply_on_account(
9619               p_api_version               =>  1.0,
9620               p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL,
9621               x_return_status             =>  x_return_status ,
9622               x_msg_count                 =>  l_reapply_msg_count,
9623               x_msg_data                  =>  x_msg_data ,
9624               p_cash_receipt_id           =>  l_open_cash_receipt_id,
9625               p_amount_applied            =>  l_reapply_amount,
9626               p_apply_date                =>  l_apply_date,
9627               p_apply_gl_date             =>  l_apply_gl_date,
9628               p_ussgl_transaction_code    =>  p_ussgl_transaction_code,
9629               p_attribute_rec             =>  p_attribute_rec,
9630               p_global_attribute_rec      =>  p_global_attribute_rec,
9631               p_comments                  =>  p_comments,
9632               p_customer_reference        =>  l_customer_reference,
9633               p_called_from               =>  'RAPI'
9634 	      );
9635            END IF;
9636          END IF;
9637 
9638      ELSIF l_open_applied_ps_id = -4 THEN
9639 
9640         /* Bug 3708728: APPLY_OPEN_RECEIPT passed to p_called_from to bypass
9641            validation on unapplied amount */
9642 
9643          Unapply_other_account(
9644               p_api_version               =>  1.0,
9645               p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL,
9646               x_return_status             =>  x_return_status ,
9647               x_msg_count                 =>  l_unapply_msg_count ,
9648               x_msg_data                  =>  x_msg_data ,
9649               p_cash_receipt_id           =>  l_open_cash_receipt_id,
9650               p_receivable_application_id =>  l_open_rec_app_id,
9651               p_reversal_gl_date          =>  l_apply_gl_date,
9652               p_org_id                    =>  p_org_id,
9653 	      p_called_from		  =>  'APPLY_OPEN_RECEIPT'
9654       );
9655          IF (x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
9656            IF (l_reapply_amount <> 0) THEN
9657              IF p_called_from = 'ARXRWAPP' THEN
9658                 l_called_from := 'RAPI';
9659 	     ELSE
9660 		l_called_from := p_called_from;
9661 	     END IF;
9662              Apply_other_account(
9663               p_api_version               =>  1.0,
9664               p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL,
9665               x_return_status             =>  x_return_status ,
9666               x_msg_count                 =>  l_reapply_msg_count,
9667               x_msg_data                  =>  x_msg_data ,
9668               p_receivable_application_id =>  l_reapply_rec_app_id,
9669               p_cash_receipt_id           =>  l_open_cash_receipt_id,
9670               p_amount_applied            =>  l_reapply_amount,
9671               p_receivables_trx_id        =>  l_reapply_rec_trx_id,
9672               p_applied_payment_schedule_id => -4,
9673               p_apply_date                =>  l_apply_date,
9674               p_apply_gl_date             =>  l_apply_gl_date,
9675               p_ussgl_transaction_code    =>  p_ussgl_transaction_code,
9676               p_application_ref_type      =>  'CLAIM',
9677               p_application_ref_id        =>  l_application_ref_id,
9678               p_application_ref_num       =>  l_application_ref_num,
9679               p_secondary_application_ref_id => l_secondary_app_ref_id ,
9680               p_attribute_rec             =>  p_attribute_rec,
9681               p_global_attribute_rec      =>  p_global_attribute_rec,
9682               p_comments                  =>  p_comments,
9683               p_application_ref_reason    =>  l_application_ref_reason,
9684               p_customer_reference        =>  l_customer_reference,
9685               p_customer_reason           =>  l_customer_reason,
9686               p_org_id                    =>  p_org_id,
9687 	      p_called_from		  =>  l_called_from
9688 	      );
9689            END IF;
9690          END IF;
9691 
9692      END IF;
9693 
9694           /*------------------------------------------------+
9695            |                                                |
9696            |    Apply open receipt to netting activity      |
9697            |                                                |
9698            +------------------------------------------------*/
9699 
9700      IF (x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
9701         Activity_application(
9702               p_api_version               =>  1.0,
9703               p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL,
9704               x_return_status             =>  x_return_status ,
9705               x_msg_count                 =>  l_act1_msg_count,
9706               x_msg_data                  =>  x_msg_data ,
9707               p_cash_receipt_id           =>  l_open_cash_receipt_id,
9708               p_amount_applied            =>  l_amount_applied * -1,
9709               p_applied_payment_schedule_id => l_cr_payment_schedule_id,
9710               p_receivables_trx_id        =>  l_netting_rec_trx_id,
9711               p_apply_date                =>  l_apply_date,
9712               p_apply_gl_date             =>  l_apply_gl_date,
9713               p_ussgl_transaction_code    =>  p_ussgl_transaction_code,
9714               p_attribute_rec             =>  p_attribute_rec,
9715               p_global_attribute_rec      =>  p_global_attribute_rec,
9716               p_comments                  =>  p_comments,
9717               p_application_ref_type      =>  l_act_application_ref_type,
9718               p_application_ref_id        =>  l_act_application_ref_id,
9719               p_application_ref_num       =>  l_application_ref_num,
9720               p_secondary_application_ref_id => l_secondary_app_ref_id ,
9721               p_receivable_application_id =>  l_open_net_rec_app_id,
9722               p_customer_reference        =>  l_customer_reference, --4145224
9723               p_called_from		  =>  'RAPI',
9724               p_netted_receipt_flag 	  =>  'Y',
9725 	      p_netted_cash_receipt_id    =>  l_open_cash_receipt_id,
9726 	      p_org_id                    =>  p_org_id,
9727               p_customer_reason           =>  l_customer_reason -- 4145224
9728               );
9729      END IF;
9730 
9731           /*------------------------------------------------+
9732            |                                                |
9733            |    Apply netting receipt to netting activity   |
9734            |                                                |
9735            +------------------------------------------------*/
9736 
9737      IF (x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
9738           Activity_application(
9739               p_api_version               =>  1.0,
9740               p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL,
9741               x_return_status             =>  x_return_status ,
9742               x_msg_count                 =>  l_act2_msg_count,
9743               x_msg_data                  =>  x_msg_data ,
9744               p_cash_receipt_id           =>  l_cash_receipt_id,
9745               p_amount_applied            =>  l_amount_applied,
9746               p_applied_payment_schedule_id => l_applied_payment_schedule_id,
9747               p_receivables_trx_id        =>  l_netting_rec_trx_id,
9748               p_apply_date                =>  l_apply_date,
9749               p_apply_gl_date             =>  l_apply_gl_date,
9750               p_ussgl_transaction_code    =>  p_ussgl_transaction_code,
9751               p_attribute_rec             =>  p_attribute_rec,
9752               p_global_attribute_rec      =>  p_global_attribute_rec,
9753               p_comments                  =>  p_comments,
9754               p_application_ref_type      =>  l_act_application_ref_type,
9755               p_application_ref_id        =>  l_act_application_ref_id,
9756               p_application_ref_num       =>  l_application_ref_num,
9757               p_secondary_application_ref_id => l_secondary_app_ref_id ,
9758               p_receivable_application_id =>  l_net_rec_app_id,
9759               p_customer_reference        =>  l_customer_reference, -- 4145224
9760               p_called_from		  =>  p_called_from,
9761               p_netted_receipt_flag 	  =>  'N',
9762 	      p_netted_cash_receipt_id    =>  l_open_cash_receipt_id,
9763 	      p_org_id    =>  p_org_id,
9764               p_customer_reason           =>  l_customer_reason -- 4145224
9765       );
9766      END IF;
9767 
9768      --
9769      -- Setting the applied_rec_app_id on each netting application..
9770      --
9771      IF PG_DEBUG in ('Y', 'C') THEN
9772           arp_util.debug('Apply_Open_Receipt: Updating applications ');
9773      END IF;
9774 
9775      IF (l_net_rec_app_id IS NULL OR l_open_net_rec_app_id IS NULL)
9776      THEN
9777        IF PG_DEBUG in ('Y', 'C') THEN
9778           arp_util.debug('Apply_Open_Receipt: error updating applications ' || SQLERRM);
9779           arp_util.debug('Apply_Open_Receipt: l_net_rec_app_id = ' || l_net_rec_app_id);
9780           arp_util.debug('Apply_Open_Receipt: l_open_net_rec_app_id = ' || l_open_net_rec_app_id);
9781        END IF;
9782        FND_MESSAGE.SET_NAME('AR','AR_RAPI_REC_APP_ID_INVALID');
9783        FND_MSG_PUB.Add;
9784        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9785      END IF;
9786 
9787      BEGIN
9788         arp_app_pkg.fetch_p(l_net_rec_app_id,l_app_rec);
9789         l_app_rec.applied_rec_app_id := l_open_net_rec_app_id;
9790         arp_app_pkg.update_p(l_app_rec);
9791 
9792         x_acctd_amount_applied_from := l_app_rec.acctd_amount_applied_from;
9793         x_acctd_amount_applied_to := l_app_rec.acctd_amount_applied_to;
9794 
9795         arp_app_pkg.fetch_p(l_open_net_rec_app_id,l_app_rec);
9796         l_app_rec.applied_rec_app_id := l_net_rec_app_id;
9797         arp_app_pkg.update_p(l_app_rec);
9798 
9799      EXCEPTION
9800        when others THEN
9801             IF PG_DEBUG in ('Y', 'C') THEN
9802                arp_util.debug('Apply_Open_Receipt: error updating applications ' || SQLCODE, G_MSG_ERROR);
9803                arp_util.debug('Apply_Open_Receipt: ' || SQLERRM, G_MSG_ERROR);
9804             END IF;
9805             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9806             RAISE;
9807      END;
9808 
9809      x_application_ref_num := l_application_ref_num;
9810 
9811      x_msg_count := l_unapply_msg_count + l_reapply_msg_count
9812                     + l_act1_msg_count + l_act2_msg_count;
9813 
9814        /*---------------------------------------------------+
9815         |   Raise exception if return status is not success |
9816         +---------------------------------------------------*/
9817 
9818      IF x_return_status = FND_API.G_RET_STS_ERROR
9819      THEN
9820        RAISE FND_API.G_EXC_ERROR;
9821      ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
9822      THEN
9823        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9824      END IF;
9825 
9826      x_receivable_application_id := l_net_rec_app_id;
9827      x_applied_rec_app_id := l_open_net_rec_app_id;
9828 
9829        /*--------------------------------+
9830         |   Standard check of p_commit   |
9831         +--------------------------------*/
9832 
9833         IF FND_API.To_Boolean( p_commit )
9834         THEN
9835             IF PG_DEBUG in ('Y', 'C') THEN
9836                arp_util.debug('Apply_Open_Receipt: ' || 'committing');
9837             END IF;
9838               Commit;
9839         END IF;
9840         IF PG_DEBUG in ('Y', 'C') THEN
9841            arp_util.debug('Apply_Open_Receipt()- ');
9842         END IF;
9843 EXCEPTION
9844        WHEN FND_API.G_EXC_ERROR THEN
9845 
9846                 IF PG_DEBUG in ('Y', 'C') THEN
9847                    arp_util.debug('Apply_Open_Receipt: ' || SQLCODE, G_MSG_ERROR);
9848                    arp_util.debug('Apply_Open_Receipt: ' || SQLERRM, G_MSG_ERROR);
9849                 END IF;
9850 
9851                 ROLLBACK TO Apply_Open_Receipt_PVT;
9852                 x_return_status := FND_API.G_RET_STS_ERROR ;
9853 
9854                -- Display_Parameters;
9855 
9856                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
9857                                            p_count       =>      x_msg_count,
9858                                            p_data        =>      x_msg_data
9859                                          );
9860 
9861         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9862 
9863                 IF PG_DEBUG in ('Y', 'C') THEN
9864                    arp_util.debug('Apply_Open_Receipt: ' || SQLERRM, G_MSG_ERROR);
9865                 END IF;
9866                 ROLLBACK TO Apply_Open_Receipt_PVT;
9867                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9868 
9869                --  Display_Parameters;
9870 
9871                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
9872                                            p_count       =>      x_msg_count,
9873                                            p_data        =>      x_msg_data
9874                                          );
9875 
9876         WHEN OTHERS THEN
9877 
9878                /*-------------------------------------------------------+
9879                 |  Handle application errors that result from trapable  |
9880                 |  error conditions. The error messages have already    |
9881                 |  been put on the error stack.                         |
9882                 +-------------------------------------------------------*/
9883 
9884                 IF (SQLCODE = -20001)
9885                 THEN
9886 
9887                       ROLLBACK TO Apply_Open_Receipt_PVT;
9888 
9889                       --If only one error message on the stack,
9890                       --retrive it
9891 
9892                       x_return_status := FND_API.G_RET_STS_ERROR ;
9893                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
9894                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','APPLY_Open_Receipt : '||SQLERRM);
9895                       FND_MSG_PUB.Add;
9896 
9897                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
9898                                                  p_count  =>  x_msg_count,
9899                                                  p_data   => x_msg_data
9900                                                 );
9901 
9902                       RETURN;
9903 
9904                 ELSE
9905                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9906                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
9907                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','APPLY_Open_Receipt : '||SQLERRM);
9908                       FND_MSG_PUB.Add;
9909                 END IF;
9910 
9911                 IF PG_DEBUG in ('Y', 'C') THEN
9912                    arp_util.debug('Apply_Open_Receipt: ' || SQLCODE, G_MSG_ERROR);
9913                    arp_util.debug('Apply_Open_Receipt: ' || SQLERRM, G_MSG_ERROR);
9914                 END IF;
9915 
9916                 ROLLBACK TO Apply_Open_Receipt_PVT;
9917 
9918                 IF      FND_MSG_PUB.Check_Msg_Level
9919                 THEN
9920                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
9921                                         l_api_name
9922                                        );
9923                 END IF;
9924 
9925              --   Display_Parameters;
9926 
9927                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
9928                                            p_count       =>      x_msg_count,
9929                                            p_data        =>      x_msg_data
9930                                          );
9931 
9932 END Apply_Open_Receipt;
9933 
9934 PROCEDURE Unapply_Open_Receipt(
9935     -- Standard API parameters.
9936       p_api_version      IN  NUMBER,
9937       p_init_msg_list    IN  VARCHAR2,
9938       p_commit           IN  VARCHAR2,
9939       p_validation_level IN  NUMBER,
9940       x_return_status    OUT NOCOPY VARCHAR2 ,
9941       x_msg_count        OUT NOCOPY NUMBER ,
9942       x_msg_data         OUT NOCOPY VARCHAR2 ,
9943       p_receivable_application_id   IN  ar_receivable_applications.receivable_application_id%TYPE,
9944       p_reversal_gl_date IN ar_receivable_applications.reversal_gl_date%TYPE ,
9945       p_called_from                  IN VARCHAR2,
9946       p_org_id             IN NUMBER  DEFAULT NULL
9947       ) IS
9948 
9949 l_api_name       CONSTANT VARCHAR2(20) := 'Unapply_Open_Receipt';
9950 l_api_version    CONSTANT NUMBER       := 1.0;
9951 l_receivable_application_id     NUMBER;
9952 l_applied_rec_app_id            NUMBER;
9953 l_applied_cash_receipt_id       NUMBER;
9954 l_amount_applied                NUMBER;
9955 l_reversal_gl_date              DATE;
9956 l_act1_msg_count          NUMBER;
9957 l_act2_msg_count          NUMBER;
9958 l_def_return_status             VARCHAR2(1);
9959 l_val_return_status             VARCHAR2(1);
9960 l_org_return_status VARCHAR2(1);
9961 l_org_id                           NUMBER;
9962 
9963 BEGIN
9964        /*------------------------------------+
9965         |   Standard start of API savepoint  |
9966         +------------------------------------*/
9967 
9968         SAVEPOINT Unapply_Open_Receipt_PVT;
9969 
9970        /*--------------------------------------------------+
9971         |   Standard call to check for call compatibility  |
9972         +--------------------------------------------------*/
9973 
9974         IF NOT FND_API.Compatible_API_Call(
9975                               p_current_version_number => l_api_version,
9976                               p_caller_version_number  => p_api_version,
9977                               p_api_name               => l_api_name,
9978                               p_pkg_name               => G_PKG_NAME
9979                               )
9980         THEN
9981               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9982         END IF;
9983 
9984        /*--------------------------------------------------------------+
9985         |   Initialize message list if p_init_msg_list is set to TRUE  |
9986         +--------------------------------------------------------------*/
9987 
9988         IF FND_API.to_Boolean( p_init_msg_list )
9989           THEN
9990               FND_MSG_PUB.initialize;
9991         END IF;
9992 
9993         IF PG_DEBUG in ('Y', 'C') THEN
9994            arp_util.debug('Unapply_Open_Receipt: ' || 'ar_receipt_api.Unapply_Open_Receipt()+ ');
9995         END IF;
9996        /*-----------------------------------------+
9997         |   Initialize return status to SUCCESS   |
9998         +-----------------------------------------*/
9999 
10000         l_def_return_status := FND_API.G_RET_STS_SUCCESS;
10001         l_val_return_status := FND_API.G_RET_STS_SUCCESS;
10002         x_return_status := FND_API.G_RET_STS_SUCCESS;
10003 
10004 
10005 
10006 /* SSA change */
10007        l_org_id            := p_org_id;
10008        l_org_return_status := FND_API.G_RET_STS_SUCCESS;
10009        ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
10010                                                 p_return_status =>l_org_return_status);
10011  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10012        x_return_status := FND_API.G_RET_STS_ERROR;
10013  ELSE
10014         /*-------------------------------------------------+
10015          | Initialize the profile option package variables |
10016          +-------------------------------------------------*/
10017 
10018            initialize_profile_globals;
10019 
10020 
10021 
10022        /*---------------------------------------------+
10023         |   ========== Start of API Body ==========   |
10024         +---------------------------------------------*/
10025 
10026         --Assign IN parameter values to local variables
10027         --which are also used as assignment targets.
10028 
10029          l_receivable_application_id  := p_receivable_application_id;
10030          l_reversal_gl_date := trunc(p_reversal_gl_date);
10031 
10032           /*---------------------+
10033            |                     |
10034            |    DEFAULTING       |
10035            |                     |
10036            +---------------------*/
10037 
10038          ar_receipt_lib_pvt.default_unapp_open_receipt(
10039               p_receivable_application_id  =>  l_receivable_application_id
10040             , x_applied_cash_receipt_id => l_applied_cash_receipt_id
10041             , x_applied_rec_app_id  => l_applied_rec_app_id
10042             , x_amount_applied      => l_amount_applied
10043             , x_return_status => l_def_return_status);
10044 
10045           /*---------------------+
10046            |                     |
10047            |    VALIDATION       |
10048            |                     |
10049            +---------------------*/
10050 
10051          ar_receipt_val_pvt.validate_unapp_open_receipt(
10052               p_applied_cash_receipt_id => l_applied_cash_receipt_id
10053             , p_amount_applied  => l_amount_applied
10054             , p_return_status   => l_val_return_status);
10055 
10056           /*------------------------------------------------+
10057            |                                                |
10058            |    Unapply netting activity on both receipts   |
10059            |                                                |
10060            +------------------------------------------------*/
10061 
10062          IF (l_def_return_status = FND_API.G_RET_STS_SUCCESS AND
10063              l_val_return_status = FND_API.G_RET_STS_SUCCESS)
10064          THEN
10065            Activity_Unapplication(
10066               p_api_version               =>  1.0,
10067               p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL,
10068               x_return_status             =>  x_return_status ,
10069               x_msg_count                 =>  l_act1_msg_count,
10070               x_msg_data                  =>  x_msg_data ,
10071               p_receivable_application_id =>  l_receivable_application_id,
10072               p_reversal_gl_date          =>  l_reversal_gl_date,
10073               p_org_id                    =>  p_org_id,
10074               p_called_from               =>  p_called_from);
10075 
10076            IF x_return_status = FND_API.G_RET_STS_SUCCESS
10077            THEN
10078              Activity_Unapplication(
10079               p_api_version               =>  1.0,
10080               p_validation_level          =>  FND_API.G_VALID_LEVEL_FULL,
10081               x_return_status             =>  x_return_status ,
10082               x_msg_count                 =>  l_act2_msg_count,
10083               x_msg_data                  =>  x_msg_data ,
10084               p_receivable_application_id =>  l_applied_rec_app_id,
10085               p_reversal_gl_date          =>  l_reversal_gl_date,
10086               p_org_id                    =>  p_org_id,
10087               p_called_from               =>  'RAPI');
10088            END IF;
10089          END IF;
10090 
10091 
10092      x_msg_count := l_act1_msg_count + l_act2_msg_count;
10093 END IF;
10094 
10095        /*---------------------------------------------------+
10096         |   Raise exception if return status is not success |
10097         +---------------------------------------------------*/
10098 
10099      IF (l_def_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
10100         x_return_status := l_def_return_status;
10101      ELSIF (l_val_return_status <> FND_API.G_RET_STS_SUCCESS ) THEN
10102         x_return_status := l_val_return_status;
10103      END IF;
10104 
10105      IF x_return_status = FND_API.G_RET_STS_ERROR
10106      THEN
10107        RAISE FND_API.G_EXC_ERROR;
10108      ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
10109      THEN
10110        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10111      END IF;
10112 
10113        /*--------------------------------+
10114         |   Standard check of p_commit   |
10115         +--------------------------------*/
10116 
10117         IF FND_API.To_Boolean( p_commit )
10118         THEN
10119             IF PG_DEBUG in ('Y', 'C') THEN
10120                arp_util.debug('Unapply_Open_Receipt: ' || 'committing');
10121             END IF;
10122               Commit;
10123         END IF;
10124         IF PG_DEBUG in ('Y', 'C') THEN
10125            arp_util.debug('Unapply_Open_Receipt()- ');
10126         END IF;
10127 EXCEPTION
10128        WHEN FND_API.G_EXC_ERROR THEN
10129 
10130                 IF PG_DEBUG in ('Y', 'C') THEN
10131                    arp_util.debug('Unapply_Open_Receipt: ' || SQLCODE, G_MSG_ERROR);
10132                    arp_util.debug('Unapply_Open_Receipt: ' || SQLERRM, G_MSG_ERROR);
10133                 END IF;
10134 
10135                 ROLLBACK TO Unapply_Open_Receipt_PVT;
10136                 x_return_status := FND_API.G_RET_STS_ERROR ;
10137 
10138                -- Display_Parameters;
10139 
10140                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
10141                                            p_count       =>      x_msg_count,
10142                                            p_data        =>      x_msg_data
10143                                          );
10144 
10145         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10146 
10147                 IF PG_DEBUG in ('Y', 'C') THEN
10148                    arp_util.debug('Unapply_Open_Receipt: ' || SQLERRM, G_MSG_ERROR);
10149                 END IF;
10150                 ROLLBACK TO Unapply_Open_Receipt_PVT;
10151                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
10152 
10153                --  Display_Parameters;
10154 
10155                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
10156                                            p_count       =>      x_msg_count,
10157                                            p_data        =>      x_msg_data
10158                                          );
10159 
10160         WHEN OTHERS THEN
10161 
10162                /*-------------------------------------------------------+
10163                 |  Handle application errors that result from trapable  |
10164                 |  error conditions. The error messages have already    |
10165                 |  been put on the error stack.                         |
10166                 +-------------------------------------------------------*/
10167 
10168                 IF (SQLCODE = -20001)
10169                 THEN
10170 
10171                       ROLLBACK TO Unapply_Open_Receipt_PVT;
10172 
10173                       --If only one error message on the stack,
10174                       --retrive it
10175 
10176                       x_return_status := FND_API.G_RET_STS_ERROR ;
10177                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
10178                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','UnaPPLY_Open_Receipt : '||SQLERRM);
10179                       FND_MSG_PUB.Add;
10180 
10181                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
10182                                                  p_count  =>  x_msg_count,
10183                                                  p_data   => x_msg_data
10184                                                 );
10185 
10186                       RETURN;
10187 
10188                 ELSE
10189                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
10190                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
10191                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','UnaPPLY_Open_Receipt : '||SQLERRM);
10192                       FND_MSG_PUB.Add;
10193                 END IF;
10194 
10195                 IF PG_DEBUG in ('Y', 'C') THEN
10196                    arp_util.debug('Unapply_Open_Receipt: ' || SQLCODE, G_MSG_ERROR);
10197                    arp_util.debug('Apply_Open_Receipt: ' || SQLERRM, G_MSG_ERROR);
10198                 END IF;
10199 
10200                 ROLLBACK TO Unapply_Open_Receipt_PVT;
10201 
10202                 IF      FND_MSG_PUB.Check_Msg_Level
10203                 THEN
10204                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
10205                                         l_api_name
10206                                        );
10207                 END IF;
10208 
10209              --   Display_Parameters;
10210 
10211                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
10212                                            p_count       =>      x_msg_count,
10213                                            p_data        =>      x_msg_data
10214                                          );
10215 
10216 END Unapply_Open_Receipt;
10217 
10218 
10219 BEGIN
10220     arp_util.debug('initialization section of ar_receipt_api_pub');
10221 	ar_receipt_lib_pvt.pg_profile_doc_seq             := FND_API.G_MISS_CHAR;
10222 	ar_receipt_lib_pvt.pg_profile_enable_cc           := FND_API.G_MISS_CHAR;
10223 	ar_receipt_lib_pvt.pg_profile_appln_gl_date_def   := FND_API.G_MISS_CHAR;
10224 	ar_receipt_lib_pvt.pg_profile_amt_applied_def     := FND_API.G_MISS_CHAR;
10225 	ar_receipt_lib_pvt.pg_profile_cc_rate_type        := FND_API.G_MISS_CHAR;
10226 	ar_receipt_lib_pvt.pg_profile_dsp_inv_rate        := FND_API.G_MISS_CHAR;
10227 	ar_receipt_lib_pvt.pg_profile_create_bk_charges   := FND_API.G_MISS_CHAR;
10228 	ar_receipt_lib_pvt.pg_profile_def_x_rate_type     := FND_API.G_MISS_CHAR;
10229 
10230 END AR_RECEIPT_API_PUB;