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