DBA Data[Home] [Help]

PACKAGE BODY: APPS.AR_CM_APPLICATION_PUB

Source


1 PACKAGE BODY AR_CM_APPLICATION_PUB AS
2 /* $Header: ARXPCMAB.pls 120.11.12020000.4 2012/10/22 09:35:11 vpotti ship $           */
3 
4 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('AFLOG_ENABLED'), 'Y');
5 
6 --Start of comments
7 --API name : Credit Memo Application API
8 --Type     : Public.
9 --Function : Apply and unapply credit memos
10 --Pre-reqs :
11 --
12 -- Notes :
13 --
14 -- Modification History
15 -- Date         Name          Description
16 -- 26-JAN-2005  J Beckett     Created.
17 -- End of comments
18 
19 /* =======================================================================
20  | Global Data Types
21  * ======================================================================*/
22 
23 G_PKG_NAME     CONSTANT VARCHAR2(30) := 'AR_CM_APPLICATION_PUB';
24 
25 G_MSG_UERROR    CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR;
26 G_MSG_ERROR     CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_ERROR;
27 G_MSG_SUCCESS   CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_SUCCESS;
28 G_MSG_HIGH      CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_DEBUG_HIGH;
29 G_MSG_MEDIUM    CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_DEBUG_MEDIUM;
30 G_MSG_LOW       CONSTANT NUMBER         := FND_MSG_PUB.G_MSG_LVL_DEBUG_LOW;
31 
32 PROCEDURE activity_application(
33     -- Standard API parameters.
34       p_api_version                  IN  NUMBER,
35       p_init_msg_list                IN  VARCHAR2,
36       p_commit                       IN  VARCHAR2,
37       p_validation_level             IN  NUMBER,
38       x_return_status                OUT NOCOPY VARCHAR2,
39       x_msg_count                    OUT NOCOPY NUMBER,
40       x_msg_data                     OUT NOCOPY VARCHAR2,
41     -- Credit Memo application parameters.
42       p_customer_trx_id              IN ra_customer_trx.customer_trx_id%TYPE,
43       p_amount_applied               IN ar_receivable_applications.amount_applied%TYPE,
44       p_applied_payment_schedule_id  IN ar_payment_schedules.payment_schedule_id%TYPE, --this has no default
45       p_receivables_trx_id           IN ar_receivable_applications.receivables_trx_id%TYPE, --this has no default
46       p_apply_date                   IN ar_receivable_applications.apply_date%TYPE,
47       p_apply_gl_date                IN ar_receivable_applications.gl_date%TYPE,
48       p_ussgl_transaction_code       IN ar_receivable_applications.ussgl_transaction_code%TYPE,
49       p_attribute_rec                IN attribute_rec_type,
50     -- ******* Global Flexfield parameters *******
51       p_global_attribute_rec         IN global_attribute_rec_type,
52       p_comments                     IN ar_receivable_applications.comments%TYPE,
53       p_chk_approval_limit_flag     IN VARCHAR2,
54       p_application_ref_type IN OUT NOCOPY
55                 ar_receivable_applications.application_ref_type%TYPE,
56       p_application_ref_id IN OUT NOCOPY
57                 ar_receivable_applications.application_ref_id%TYPE,
58       p_application_ref_num IN OUT NOCOPY
59                 ar_receivable_applications.application_ref_num%TYPE,
60       p_receivable_application_id OUT NOCOPY ar_receivable_applications.receivable_application_id%TYPE,
61       p_called_from		    IN VARCHAR2
62      ,p_org_id             	IN  NUMBER
63      ,p_pay_group_lookup_code	IN  FND_LOOKUPS.lookup_code%TYPE
64      ,p_pay_alone_flag		IN  VARCHAR2
65      ,p_payment_method_code	IN  ap_invoices.payment_method_code%TYPE
66      ,p_payment_reason_code	IN  ap_invoices.payment_reason_code%TYPE
67      ,p_payment_reason_comments	IN  ap_invoices.payment_reason_comments%TYPE
68      ,p_delivery_channel_code	IN  ap_invoices.delivery_channel_code%TYPE
69      ,p_remittance_message1	IN  ap_invoices.remittance_message1%TYPE
70      ,p_remittance_message2	IN  ap_invoices.remittance_message2%TYPE
71      ,p_remittance_message3	IN  ap_invoices.remittance_message3%TYPE
72      ,p_party_id		IN  hz_parties.party_id%TYPE
73      ,p_party_site_id		IN  hz_party_sites.party_site_id%TYPE
74      ,p_bank_account_id		IN  ar_cash_receipts.customer_bank_account_id%TYPE
75      ,p_payment_priority	IN  ap_invoices_interface.PAYMENT_PRIORITY%TYPE		--Bug8290172
76      ,p_terms_id		IN  ap_invoices_interface.TERMS_ID%TYPE			--Bug8290172
77       )
78 IS
79  l_api_name       CONSTANT VARCHAR2(20) := 'activity_application';
80  l_api_version    CONSTANT NUMBER       := 1.0;
81  l_customer_trx_id NUMBER;
82  l_applied_ps_id   NUMBER;
83  l_amount_applied  NUMBER;
84  l_cm_unapp_amount  NUMBER;
85  l_apply_date      DATE;
86  l_apply_gl_date   DATE;
87  l_trx_date        DATE;
88  l_cm_gl_date      DATE;
89  l_return_status      VARCHAR2(1);
90  l_def_return_status  VARCHAR2(1);
91  l_val_return_status  VARCHAR2(1);
92  l_id_conv_return_status  VARCHAR2(1);
93  l_cm_currency_code fnd_currencies.currency_code%TYPE;
94  l_rec_app_id  NUMBER;
95  l_cm_ps_id NUMBER;
96  l_cm_receipt_method_id NUMBER;
97  l_application_ref_type ar_receivable_applications.application_ref_type%TYPE;
98  l_application_ref_id   ar_receivable_applications.application_ref_id%TYPE;
99  l_application_ref_num  ar_receivable_applications.application_ref_num%TYPE;
100  l_acctd_amount_applied_from  NUMBER;
101  l_acctd_amount_applied_to    NUMBER;
102  l_msg_count		NUMBER;
103  l_msg_data             VARCHAR2(2000);
104 
105  l_org_return_status VARCHAR2(1);
106  l_org_id                           NUMBER;
107  l_party_id hz_parties.party_id%TYPE;
108  l_party_name hz_parties.party_name%TYPE;
109  l_party_number hz_parties.party_number%TYPE;
110  l_party_site_id hz_party_sites.party_site_id%TYPE;
111  l_party_address VARCHAR2(360);
112  l_payment_method_code iby_payment_methods_vl.payment_method_code%TYPE;
113  l_payment_method_name iby_payment_methods_vl.payment_method_name%TYPE;
114  l_bank_account_id iby_ext_bank_accounts.ext_bank_account_id%TYPE;
115  l_bank_account_num iby_ext_bank_accounts.bank_account_num%TYPE;
116  l_payment_reason_code iby_payment_reasons_vl.payment_reason_code%TYPE;
117  l_payment_reason_name iby_payment_reasons_vl.meaning%TYPE;
118  l_delivery_channel_code iby_delivery_channels_vl.delivery_channel_code%TYPE;
119  l_delivery_channel_name iby_delivery_channels_vl.meaning%TYPE;
120  l_pay_alone_flag VARCHAR2(1);
121  l_legal_entity_id ar_cash_receipts.legal_entity_id%TYPE;
122  l_exchange_rate ar_cash_receipts.exchange_rate%TYPE;
123  l_exchange_rate_type ar_cash_receipts.exchange_rate_type%TYPE;
124  l_exchange_date ar_cash_receipts.exchange_date%TYPE;
125  l_invoice_id ap_invoices.invoice_id%TYPE;
126  l_dft_ref_return_status  VARCHAR2(1);
127 
128   --Bug8290172 Changes Start Here
129   l_term_id			ap_invoices_interface.TERMS_ID%TYPE;
130   l_pay_term_return_status	VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
131   l_pay_priority_return_status	VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
132  --Bug8290172 Changes End Here
133 
134 BEGIN
135 
136        /*------------------------------------+
137         |   Standard start of API savepoint  |
138         +------------------------------------*/
139 
140       SAVEPOINT activity_app_PVT;
141 
142        /*--------------------------------------------------+
143         |   Standard call to check for call compatibility  |
144         +--------------------------------------------------*/
145 
146         IF NOT FND_API.Compatible_API_Call(
147                                             l_api_version,
148                                             p_api_version,
149                                             l_api_name,
150                                             G_PKG_NAME
151                                           )
152         THEN
153               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
154         END IF;
155 
156        /*--------------------------------------------------------------+
157         |   Initialize message list if p_init_msg_list is set to TRUE  |
158         +--------------------------------------------------------------*/
159 
160         IF FND_API.to_Boolean( p_init_msg_list )
161           THEN
162               FND_MSG_PUB.initialize;
163         END IF;
164 
165         IF PG_DEBUG in ('Y', 'C') THEN
166            arp_util.debug('ar_cm_application_pub.activity_application()+ ');
167         END IF;
168 
169        /*-------------------------------------------------+
170         | Initialize SOB/org dependent variables          |
171         +-------------------------------------------------*/
172         arp_global.init_global;
173         arp_standard.init_standard;
174 
175        /*-----------------------------------------+
176         |   Initialize return status to SUCCESS   |
177         +-----------------------------------------*/
178 
179         x_return_status := FND_API.G_RET_STS_SUCCESS;
180 
181         l_org_id            := p_org_id;
182         l_org_return_status := FND_API.G_RET_STS_SUCCESS;
183         ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
184                                                  p_return_status =>l_org_return_status);
185         IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
186            x_return_status := FND_API.G_RET_STS_ERROR;
187 	END IF;
188 
189        /*---------------------------------------------+
190         |   ========== Start of API Body ==========   |
191         +---------------------------------------------*/
192 
193      l_customer_trx_id := p_customer_trx_id;
194      l_applied_ps_id   := p_applied_payment_schedule_id;
195      l_amount_applied  := p_amount_applied;
196      l_apply_date      := trunc(p_apply_date);
197      l_apply_gl_date   := trunc(p_apply_gl_date);
198      l_application_ref_type         := p_application_ref_type;
199      l_application_ref_id           := p_application_ref_id;
200      l_application_ref_num          := p_application_ref_num;
201      l_party_id		:= p_party_id;
202      l_party_site_id	:= p_party_site_id;
203 
204      ar_cm_app_lib_pvt.default_activity_info
205 			( p_customer_trx_id => l_customer_trx_id
206 			, p_cm_ps_id => l_cm_ps_id
207 			, p_cm_currency_code => l_cm_currency_code
208 			, p_cm_gl_date => l_cm_gl_date
209 			, p_cm_unapp_amount => l_cm_unapp_amount
210 			, p_cm_receipt_method_id => l_cm_receipt_method_id
211 			, p_trx_date => l_trx_date
212 			, p_amount_applied => l_amount_applied
213 			, p_apply_date => l_apply_date
214 			, p_apply_gl_date => l_apply_gl_date
215 			, p_return_status => l_def_return_status
216 			);
217 
218      IF p_applied_payment_schedule_id = -8 THEN
219          /* Default the refund attributes from IBY */
220          ar_receipt_lib_pvt.default_refund_attributes(
221             	 p_cash_receipt_id	=>  NULL
222             	,p_customer_trx_id	=>  l_customer_trx_id
223 		,p_currency_code	=>  l_cm_currency_code
224 		,p_amount		=>  l_amount_applied
225 		,p_party_id		=>  l_party_id
226 		,p_party_site_id	=>  l_party_site_id
227 		,x_party_name		=>  l_party_name
228 		,x_party_number		=>  l_party_number
229 		,x_party_address	=>  l_party_address
230 		,x_exchange_rate	=>  l_exchange_rate
231 		,x_exchange_rate_type	=>  l_exchange_rate_type
232 		,x_exchange_date	=>  l_exchange_date
233 		,x_legal_entity_id	=>  l_legal_entity_id
234 		,x_payment_method_code	=>  l_payment_method_code
235 		,x_payment_method_name	=>  l_payment_method_name
236 		,x_bank_account_id	=>  l_bank_account_id
237           	,x_bank_account_num	=>  l_bank_account_num
238           	,x_payment_reason_code => l_payment_reason_code
239           	,x_payment_reason_name => l_payment_reason_name
240           	,x_delivery_channel_code => l_delivery_channel_code
241           	,x_delivery_channel_name => l_delivery_channel_name
242 		,x_pay_alone_flag	=>  l_pay_alone_flag
243 		,x_return_status	=> l_dft_ref_return_status
244 		,x_msg_count		=> x_msg_count
245 		,x_msg_data		=> x_msg_data );
246 
247          /* If values have been passed in they should be used instead */
248          IF p_payment_method_code IS NOT NULL THEN
249             l_payment_method_code := p_payment_method_code;
250 	 ELSIF p_payment_method_code IS NULL AND l_payment_method_code IS NULL THEN/*Bug 8624954*/
251             l_payment_method_code := 'CHECK';
252 	 END IF;
253          IF p_bank_account_id IS NOT NULL THEN
254             l_bank_account_id := p_bank_account_id;
255 	 END IF;
256          IF p_payment_reason_code IS NOT NULL THEN
257             l_payment_reason_code := p_payment_reason_code;
258 	 END IF;
259          IF p_delivery_channel_code IS NOT NULL THEN
260             l_delivery_channel_code := p_delivery_channel_code;
261 	 END IF;
262          IF p_pay_alone_flag IS NOT NULL THEN
263             l_pay_alone_flag := p_pay_alone_flag;
264 	 ELSIF l_pay_alone_flag IS NULL AND p_pay_alone_flag IS NULL THEN /*Bug 8624954*/
265             l_pay_alone_flag := 'N';
266 	 END IF;
267 
268       END IF;
269 
270       IF PG_DEBUG in ('Y', 'C') THEN
271          arp_util.debug('Activity_application: ' || 'Default_activity_info return status :'||l_def_return_status);
272          arp_util.debug('Activity_application: ' || 'Default_refund_attributes return status :'||l_dft_ref_return_status);
273       END IF;
274 
275      ar_cm_app_val_pvt.validate_activity_app
276 		( p_receivables_trx_id => p_receivables_trx_id,
277                   p_applied_ps_id  => l_applied_ps_id,
278                   p_customer_trx_id => l_customer_trx_id,
279                   p_cm_gl_date  => l_cm_gl_date,
280                   p_cm_unapp_amount => l_cm_unapp_amount,
281                   p_trx_date => l_trx_date,
282                   p_amount_applied => l_amount_applied,
283                   p_apply_gl_date => l_apply_gl_date,
284                   p_apply_date => l_apply_date,
285                   p_cm_currency_code => l_cm_currency_code,
286                   p_return_status => l_val_return_status,
287                   p_chk_approval_limit_flag => p_chk_approval_limit_flag,
288                   p_called_from => p_called_from
289                                  );
290 
291       IF PG_DEBUG in ('Y', 'C') THEN
292          arp_util.debug('Activity_application: ' || 'Validation return status :'||l_val_return_status);
293          arp_util.debug('Activity_application: ' || '*****DUMPING ALL THE ENTITY HANDLER PARAMETERS  ***');
294          arp_util.debug('Activity_application: ' || 'l_cm_unapp_amount : '||l_cm_unapp_amount);
295          arp_util.debug('Activity_application: ' || 'l_amount_applied         : '||to_char(l_amount_applied));
296          arp_util.debug('Activity_application: ' || 'l_apply_date             : '||to_char(l_apply_date,'DD-MON-YY'));
297          arp_util.debug('Activity_application: ' || 'l_apply_gl_date          : '||to_char(l_apply_gl_date,'DD-MON-YY'));
298       END IF;
299 
300       -- Bug8290172 Changes Start Here
301       -- Validate Payment Term Id
302       IF p_terms_id IS NOT NULL THEN
303 	      BEGIN
304 		SELECT term_id
305 		INTO l_term_id
306 		FROM ap_terms_bat_pay_terms_v
307 		WHERE term_id = p_terms_id;
308 	      EXCEPTION
309 	      WHEN OTHERS THEN
310 		     l_pay_term_return_status := FND_API.G_RET_STS_ERROR ;
311 
312                      FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
313                      FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','Invalid Payment Term');
314                      FND_MSG_PUB.Add;
315 
316 		     IF PG_DEBUG in ('Y', 'C') THEN
317 			arp_util.debug('Activity_application: ' || 'Invalid Payment Term. Rolling back and setting status to ERROR');
318 		     END IF;
319 	      END;
320       END IF;
321 
322       -- Validate Payment Priority. It Should be between 1 to 99
323       IF p_payment_priority IS NOT NULL THEN
324       	IF p_payment_priority < 1 OR p_payment_priority > 99 THEN
325 
326 	     l_pay_priority_return_status := FND_API.G_RET_STS_ERROR ;
327 
328 	     FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
329 	     FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','Payment Priority Not In Range(1-99)');
330 	     FND_MSG_PUB.Add;
331 
332 	     IF PG_DEBUG in ('Y', 'C') THEN
333 		arp_util.debug('Activity_application: ' || 'Payment Priority Not In Range(1-99). Rolling back and setting status to ERROR');
334 	     END IF;
335 	 END IF;
336       END IF;
337       -- Bug8290172 Changes End Here
338 
339       IF l_val_return_status <> FND_API.G_RET_STS_SUCCESS  OR
340          l_def_return_status <> FND_API.G_RET_STS_SUCCESS OR
341          l_dft_ref_return_status <> FND_API.G_RET_STS_SUCCESS OR
342          l_id_conv_return_status <> FND_API.G_RET_STS_SUCCESS OR
343          l_pay_term_return_status <> FND_API.G_RET_STS_SUCCESS OR	-- Bug8290172
344          l_pay_priority_return_status <> FND_API.G_RET_STS_SUCCESS THEN	-- Bug8290172
345 
346           IF PG_DEBUG in ('Y', 'C') THEN
347              arp_util.debug('Activity_application: ' || 'Validation FAILED ');
348           END IF;
349           x_return_status :=  FND_API.G_RET_STS_ERROR;
350       END IF;
351 
352       IF x_return_status <> FND_API.G_RET_STS_SUCCESS
353          THEN
354 
355             ROLLBACK TO Activity_app_PVT;
356 
357              x_return_status := FND_API.G_RET_STS_ERROR ;
358 
359              FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
360                                         p_count => x_msg_count,
361                                         p_data  => x_msg_data
362                                        );
363 
364              IF PG_DEBUG in ('Y', 'C') THEN
365                 arp_util.debug('Activity_application: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
366              END IF;
367              Return;
368        END IF;
369 
370       -- CM payment schedule locked before calling entity handler
371       arp_ps_pkg.nowaitlock_p (p_ps_id => l_cm_ps_id);
372 
373        --call the entity handler
374       BEGIN
375         arp_process_application.cm_activity_application     (
376 	p_cm_ps_id => l_cm_ps_id
377       , p_application_ps_id => l_applied_ps_id
378       , p_amount_applied => l_amount_applied
379       , p_apply_date                =>  l_apply_date
380       , p_gl_date                   =>  l_apply_gl_date
381       , p_ussgl_transaction_code    =>  p_ussgl_transaction_code
382       , p_attribute_category=> p_attribute_rec.attribute_category
383       , p_attribute1        => p_attribute_rec.attribute1
384       , p_attribute2        => p_attribute_rec.attribute2
385       , p_attribute3        => p_attribute_rec.attribute3
386       , p_attribute4        => p_attribute_rec.attribute4
387       , p_attribute5        => p_attribute_rec.attribute5
388       , p_attribute6        => p_attribute_rec.attribute6
389       , p_attribute7        => p_attribute_rec.attribute7
390       , p_attribute8        => p_attribute_rec.attribute8
391       , p_attribute9        => p_attribute_rec.attribute9
392       , p_attribute10       => p_attribute_rec.attribute10
393       , p_attribute11       => p_attribute_rec.attribute11
394       , p_attribute12       => p_attribute_rec.attribute12
395       , p_attribute13       => p_attribute_rec.attribute13
396       , p_attribute14       => p_attribute_rec.attribute14
397       , p_attribute15       => p_attribute_rec.attribute15
398       , p_global_attribute_category => p_global_attribute_rec.global_attribute_category
399       , p_global_attribute1 => p_global_attribute_rec.global_attribute1
400       , p_global_attribute2 => p_global_attribute_rec.global_attribute2
401       , p_global_attribute3 => p_global_attribute_rec.global_attribute3
402       , p_global_attribute4 => p_global_attribute_rec.global_attribute4
403       , p_global_attribute5 => p_global_attribute_rec.global_attribute5
404       , p_global_attribute6 => p_global_attribute_rec.global_attribute6
405       , p_global_attribute7 => p_global_attribute_rec.global_attribute7
406       , p_global_attribute8 => p_global_attribute_rec.global_attribute8
407       , p_global_attribute9 => p_global_attribute_rec.global_attribute9
408       , p_global_attribute10 => p_global_attribute_rec.global_attribute10
409       , p_global_attribute11 => p_global_attribute_rec.global_attribute11
410       , p_global_attribute12 => p_global_attribute_rec.global_attribute12
411       , p_global_attribute13 => p_global_attribute_rec.global_attribute13
412       , p_global_attribute14 => p_global_attribute_rec.global_attribute14
413       , p_global_attribute15 => p_global_attribute_rec.global_attribute15
414       , p_global_attribute16 => p_global_attribute_rec.global_attribute16
415       , p_global_attribute17 => p_global_attribute_rec.global_attribute17
416       , p_global_attribute18 => p_global_attribute_rec.global_attribute18
417       , p_global_attribute19 => p_global_attribute_rec.global_attribute19
418       , p_global_attribute20 => p_global_attribute_rec.global_attribute20
419       , p_receivables_trx_id		=> p_receivables_trx_id
420       , p_receipt_method_id		=> l_cm_receipt_method_id
421       , p_comments			=> p_comments
422       , p_module_name			=> 'CMAAPI'
423       , p_module_version		=> p_api_version
424       , p_application_ref_id         => l_application_ref_id
425       , p_application_ref_num       =>  l_application_ref_num
426       , p_out_rec_application_id    => l_rec_app_id
427         , p_acctd_amount_applied_from => l_acctd_amount_applied_from
428         , p_acctd_amount_applied_to => l_acctd_amount_applied_to
429       , x_return_status                =>  l_return_status
430       , x_msg_count                    =>  l_msg_count
431       , x_msg_data                     =>  l_msg_data);
432      EXCEPTION
433        WHEN OTHERS THEN
434 
435                /*-------------------------------------------------------+
436                 |  Handle application errors that result from trapable  |
437                 |  error conditions. The error messages have already    |
438                 |  been put on the error stack.                         |
439                 +-------------------------------------------------------*/
440 
441                 IF (SQLCODE = -20001)
442                 THEN
443                      ROLLBACK TO Activity_app_PVT;
444 
445                       --  Display_Parameters;
446                       x_return_status := FND_API.G_RET_STS_ERROR ;
447                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
448                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.CM_ACTIVITY_APPLICATION : '||SQLERRM);
449                        FND_MSG_PUB.Add;
450 
451                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
452                                                   p_count  =>  x_msg_count,
453                                                   p_data   => x_msg_data
454                                                 );
455                       RETURN;
456                 ELSE
457                    RAISE;
458                 END IF;
459 
460      END;
461 
462      /* Call AP API's to create payment request if refund */
463      IF p_applied_payment_schedule_id = -8 THEN
464 	BEGIN
465 	  ar_refunds_pvt.create_refund
466       		(p_receivable_application_id    =>  l_rec_app_id
467 		,p_amount			=>  l_amount_applied
468 		,p_currency_code		=>  l_cm_currency_code
469 		,p_exchange_rate		=>  l_exchange_rate
470 		,p_exchange_rate_type		=>  l_exchange_rate_type
471 		,p_exchange_date		=>  l_exchange_date
472 		,p_description			=>  NULL
473           	,p_pay_group_lookup_code	=>  p_pay_group_lookup_code
474           	,p_pay_alone_flag		=>  l_pay_alone_flag
475 		,p_org_id			=>  l_org_id
476 	  	,p_legal_entity_id        	=>  l_legal_entity_id
477           	,p_payment_method_code		=>  l_payment_method_code
478           	,p_payment_reason_code		=>  l_payment_reason_code
479           	,p_payment_reason_comments	=>  p_payment_reason_comments
480           	,p_delivery_channel_code	=>  l_delivery_channel_code
481           	,p_remittance_message1		=>  p_remittance_message1
482           	,p_remittance_message2		=>  p_remittance_message2
483           	,p_remittance_message3		=>  p_remittance_message3
484           	,p_party_id			=>  l_party_id
485           	,p_party_site_id		=>  l_party_site_id
486                 ,p_bank_account_id		=>  l_bank_account_id
487 		,p_called_from			=>  'CM_APPLICATION_API'
488 		,x_invoice_id			=>  l_invoice_id
489 		,x_return_status		=>  x_return_status
490 		,x_msg_count			=>  x_msg_count
491 		,x_msg_data			=>  x_msg_data
492 		,p_payment_priority		=> NVL(p_payment_priority,99)	--Bug8290172  Default 99 of no Payment Proirity is passed
493 		,p_terms_id			=> p_terms_id		 	--Bug8290172
494 		,p_invoice_date                 => l_apply_date                 --Bug9258845
495 		,p_gl_date => l_apply_gl_date
496 		);
497 
498    /* 6865230 */
499    IF x_return_status <> FND_API.G_RET_STS_SUCCESS
500          THEN
501 
502             ROLLBACK TO Activity_app_PVT;
503 
504              x_return_status := FND_API.G_RET_STS_ERROR ;
505 
506              FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
507                                         p_count => x_msg_count,
508                                         p_data  => x_msg_data
509                                        );
510 
511              IF PG_DEBUG in ('Y', 'C') THEN
512                 arp_util.debug('create_refund: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
513              END IF;
514              Return;
515        END IF;
516 
517        l_application_ref_id := l_invoice_id;
518        l_application_ref_num := l_invoice_id;
519 
520         EXCEPTION
521        WHEN OTHERS THEN
522 
523                /*-------------------------------------------------------+
524                 |  Handle application errors that result from trapable  |
525                 |  error conditions. The error messages have already    |
526                 |  been put on the error stack.                         |
527                 +-------------------------------------------------------*/
528 
529                 IF (SQLCODE = -20001)
530                 THEN
531                      ROLLBACK TO Activity_app_PVT;
532 
533                       --  Display_Parameters;
534                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
535                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
536                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','AR_REFUNDS_PVT.Create_Refund : '||SQLERRM);
537                        FND_MSG_PUB.Add;
538 
539                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
540                                                   p_count  =>  x_msg_count,
541                                                   p_data   => x_msg_data
542                                                 );
543                       RETURN;
544                 ELSE
545                    RAISE;
546                 END IF;
547         END;
548       END IF;
549 
550         p_receivable_application_id   := l_rec_app_id;
551         p_application_ref_type := l_application_ref_type;
552         p_application_ref_id   := l_application_ref_id;
553         p_application_ref_num  := l_application_ref_num;
554 
555        /*--------------------------------+
556         |   Standard check of p_commit   |
557         +--------------------------------*/
558 
559         IF FND_API.To_Boolean( p_commit )
560         THEN
561             IF PG_DEBUG in ('Y', 'C') THEN
562                arp_util.debug('Activity_application: ' || 'committing');
563             END IF;
564               Commit;
565         END IF;
566         IF PG_DEBUG in ('Y', 'C') THEN
567            arp_util.debug('ar_cm_application_pub.Activity_application()- ');
568         END IF;
569 EXCEPTION
570        WHEN FND_API.G_EXC_ERROR THEN
571 
572                 IF PG_DEBUG in ('Y', 'C') THEN
573                    arp_util.debug('Activity_application: ' || SQLCODE, G_MSG_ERROR);
574                    arp_util.debug('Activity_application: ' || SQLERRM, G_MSG_ERROR);
575                 END IF;
576 
577                 ROLLBACK TO Activity_app_PVT;
578                 x_return_status := FND_API.G_RET_STS_ERROR ;
579 
580                -- Display_Parameters;
581 
582                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
583                                            p_count       =>      x_msg_count,
584                                            p_data        =>      x_msg_data
585                                          );
586 
587         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
588 
589                 IF PG_DEBUG in ('Y', 'C') THEN
590                    arp_util.debug('Activity_application: ' || SQLERRM, G_MSG_ERROR);
591                 END IF;
592                 ROLLBACK TO Activity_app_PVT;
593                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
594 
595                --  Display_Parameters;
596 
597                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
598                                            p_count       =>      x_msg_count,
599                                            p_data        =>      x_msg_data
600                                          );
601 
602         WHEN OTHERS THEN
603 
604                /*-------------------------------------------------------+
605                 |  Handle application errors that result from trapable  |
606                 |  error conditions. The error messages have already    |
607                 |  been put on the error stack.                         |
608                 +-------------------------------------------------------*/
609 
610                 IF (SQLCODE = -20001)
611                 THEN
612 
613                       ROLLBACK TO Activity_app_PVT;
614 
615                       --If only one error message on the stack,
616                       --retrive it
617 
618                       x_return_status := FND_API.G_RET_STS_ERROR ;
619                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
620                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ACTIVITY_APPLICATION : '||SQLERRM);
621                       FND_MSG_PUB.Add;
622 
623                       --If only one error message on the stack,
624                       --retrive it
625 
626                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
627                                                  p_count  =>  x_msg_count,
628                                                  p_data   => x_msg_data
629                                                 );
630 
631                       RETURN;
632 
633                 ELSE
634                      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
635                      FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
636                      FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ACTIVITY_APPLICATION : '||SQLERRM);
637                      FND_MSG_PUB.Add;
638                 END IF;
639 
640                 IF PG_DEBUG in ('Y', 'C') THEN
641                    arp_util.debug('Activity_application: ' || SQLCODE, G_MSG_ERROR);
642                    arp_util.debug('Activity_application: ' || SQLERRM, G_MSG_ERROR);
643                 END IF;
644 
645                 ROLLBACK TO Activity_app_PVT;
646 
647                 IF      FND_MSG_PUB.Check_Msg_Level
648                 THEN
649                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
650                                         l_api_name
651                                        );
652                 END IF;
653 
654              --   Display_Parameters;
655 
656                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
657                                            p_count       =>      x_msg_count,
658                                            p_data        =>      x_msg_data
659                                          );
660 END Activity_application;
661 
662 
663 PROCEDURE Activity_unapplication(
664     -- Standard API parameters.
665       p_api_version      IN  NUMBER,
666       p_init_msg_list    IN  VARCHAR2,
667       p_commit           IN  VARCHAR2,
668       p_validation_level IN  NUMBER,
669       x_return_status    OUT NOCOPY VARCHAR2 ,
670       x_msg_count        OUT NOCOPY NUMBER ,
671       x_msg_data         OUT NOCOPY VARCHAR2 ,
672    -- *** Credit Memo Info. parameters *****
673       p_customer_trx_id  IN ra_customer_trx.customer_trx_id%TYPE,
674       p_receivable_application_id   IN ar_receivable_applications.receivable_application_id%TYPE,
675       p_reversal_gl_date IN ar_receivable_applications.reversal_gl_date%TYPE,
676       p_called_from      IN VARCHAR2,
677       p_org_id		 IN NUMBER
678       ) IS
679 l_api_name       CONSTANT VARCHAR2(20) := 'Activity_unapp';
680 l_api_version    CONSTANT NUMBER       := 1.0;
681 l_customer_trx_id               NUMBER;
682 l_receivable_application_id     NUMBER;
683 l_cm_gl_date                    DATE;
684 l_reversal_gl_date              DATE;
685 l_apply_gl_date                 DATE;
686 l_receipt_gl_date               DATE;
687 l_id_return_status              VARCHAR2(1);
688 l_def_return_status             VARCHAR2(1);
689 l_val_return_status             VARCHAR2(1);
690 l_cm_unapp_amt                  NUMBER;
691 l_cm_ps_id                      NUMBER;
692 l_org_id			NUMBER;
693 l_org_return_status		VARCHAR2(1);
694 l_refund_return_status		VARCHAR2(1);
695 l_applied_ps_id			ar_payment_schedules.payment_schedule_id%TYPE;
696 l_application_ref_id		ar_receivable_applications.application_ref_id%TYPE;
697 
698 BEGIN
699        /*------------------------------------+
700         |   Standard start of API savepoint  |
701         +------------------------------------*/
702 
703         SAVEPOINT Activity_unapplication_PVT;
704 
705        /*--------------------------------------------------+
706         |   Standard call to check for call compatibility  |
707         +--------------------------------------------------*/
708 
709         IF NOT FND_API.Compatible_API_Call(
710                                             l_api_version,
711                                             p_api_version,
712                                             l_api_name,
713                                             G_PKG_NAME
714                                           )
715         THEN
716               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
717         END IF;
718 
719        /*--------------------------------------------------------------+
720         |   Initialize message list if p_init_msg_list is set to TRUE  |
721         +--------------------------------------------------------------*/
722 
723         IF FND_API.to_Boolean( p_init_msg_list )
724           THEN
725               FND_MSG_PUB.initialize;
726         END IF;
727 
728         IF PG_DEBUG in ('Y', 'C') THEN
729            arp_util.debug('ar_cm_application_pub.activity_unapplication()+ ');
730         END IF;
731 
732        /*-------------------------------------------------+
733         | Initialize SOB/org dependent variables          |
734         +-------------------------------------------------*/
735         arp_global.init_global;
736         arp_standard.init_standard;
737 
738        /*-----------------------------------------+
739         |   Initialize return status to SUCCESS   |
740         +-----------------------------------------*/
741 
742         x_return_status := FND_API.G_RET_STS_SUCCESS;
743 
744         l_org_id            := p_org_id;
745         l_org_return_status := FND_API.G_RET_STS_SUCCESS;
746         ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
747                                                  p_return_status =>l_org_return_status);
748         IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
749            x_return_status := FND_API.G_RET_STS_ERROR;
750 	END IF;
751        /*---------------------------------------------+
752         |   ========== Start of API Body ==========   |
753         +---------------------------------------------*/
754 
755 
756         --Assign IN parameter values to local variables
757         --which are also used as assignment targets.
758 
759          l_customer_trx_id   := p_customer_trx_id;
760          l_receivable_application_id  := p_receivable_application_id;
761          l_reversal_gl_date := trunc(p_reversal_gl_date);
762 
763 
764         /*------------------------------------------------+
765          |  Derive the IDs for the entered values.        |
766          |  If both the values and the IDs are specified, |
767          |  the IDs supercede the values                  |
768          +------------------------------------------------*/
769 
770          ar_cm_app_lib_pvt.derive_activity_unapp_ids(
771                          NULL   ,
772                          l_customer_trx_id  ,
773                          l_receivable_application_id ,
774                          l_apply_gl_date     ,
775                          l_id_return_status
776                                );
777          ar_cm_app_lib_pvt.default_unapp_activity_info(
778                          l_receivable_application_id ,
779                          l_apply_gl_date            ,
780                          l_customer_trx_id          ,
781                          l_reversal_gl_date         ,
782                          l_cm_gl_date,
783 			 l_cm_ps_id,
784                          l_cm_unapp_amt,
785 			 l_def_return_status
786                           );
787 
788          ar_cm_app_val_pvt.validate_unapp_activity(
789                                       l_cm_gl_date,
790                                       l_receivable_application_id,
791                                       l_reversal_gl_date,
792                                       l_apply_gl_date,
793                                       l_cm_unapp_amt,
794                                       l_val_return_status);
795 
796          IF PG_DEBUG in ('Y', 'C') THEN
797             arp_util.debug('Activity_Unapplication: ' || 'validation return status :'||l_val_return_status);
798          END IF;
799 
800 	/* Refunds - check for refund and cancel if refund application */
801         SELECT applied_payment_schedule_id, application_ref_id
802 	INTO   l_applied_ps_id, l_application_ref_id
803 	FROM   ar_receivable_applications
804   	WHERE  receivable_application_id = l_receivable_application_id;
805 
806         IF (l_applied_ps_id = -8 AND p_called_from <> 'AR_REFUNDS_GRP') THEN
807            ar_refunds_pvt.cancel_refund(
808 		  p_application_ref_id  => l_application_ref_id
809 		, p_gl_date		=> l_reversal_gl_date
810 		, x_return_status	=> l_refund_return_status
811 		, x_msg_count		=> x_msg_count
812 		, x_msg_data		=> x_msg_data);
813         END IF;
814 
815         IF l_val_return_status <> FND_API.G_RET_STS_SUCCESS OR
816            l_id_return_status <> FND_API.G_RET_STS_SUCCESS  OR
817 	   l_refund_return_status <> FND_API.G_RET_STS_SUCCESS  OR
818            l_def_return_status <> FND_API.G_RET_STS_SUCCESS  THEN
819            x_return_status := FND_API.G_RET_STS_ERROR;
820         END IF;
821 
822         IF x_return_status <> FND_API.G_RET_STS_SUCCESS
823          THEN
824 
825             ROLLBACK TO Activity_unapplication_PVT;
826 
827              x_return_status := FND_API.G_RET_STS_ERROR ;
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 
834              IF PG_DEBUG in ('Y', 'C') THEN
835                 arp_util.debug('Activity_Unapplication: ' || 'Error(s) occurred. Rolling back and setting status to ERROR');
836              END IF;
837              Return;
838         END IF;
839 
840         IF PG_DEBUG in ('Y', 'C') THEN
841            arp_util.debug('Activity_Unapplication: ' || '*******DUMP THE INPUT PARAMETERS ********');
842            arp_util.debug('Activity_Unapplication: ' || 'l_receivable_application_id :'||to_char(l_receivable_application_id));
843            arp_util.debug('Activity_Unapplication: ' || 'l_reversal_gl_date :'||to_char(l_reversal_gl_date,'DD-MON-YY'));
844         END IF;
845 
846         -- CM payment schedule locked before calling entity handler
847         arp_ps_pkg.nowaitlock_p (p_ps_id => l_cm_ps_id);
848 
849         --call the entity handler.
850       BEGIN
851           arp_process_application.reverse_cm_app(
852                                 l_receivable_application_id,
853                                 -8,
854                                 l_reversal_gl_date,
855                                 trunc(sysdate),
856                                 'CMAAPI',
857                                 p_api_version,
858 				p_called_from);
859       EXCEPTION
860         WHEN OTHERS THEN
861 
862                /*-------------------------------------------------------+
863                 |  Handle application errors that result from trapable  |
864                 |  error conditions. The error messages have already    |
865                 |  been put on the error stack.                         |
866                 +-------------------------------------------------------*/
867 
868                 IF (SQLCODE = -20001)
869                 THEN
870                      ROLLBACK TO Activity_unapplication_PVT;
871 
872                       --  Display_Parameters;
873                       x_return_status := FND_API.G_RET_STS_ERROR ;
874                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
875                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.REVERSE : '||SQLERRM);
876                       FND_MSG_PUB.Add;
877 
878                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
879                                                   p_count  =>  x_msg_count,
880                                                   p_data   => x_msg_data
881                                                 );
882                       RETURN;
883                 ELSE
884                    RAISE;
885                 END IF;
886       END;
887 
888        /*--------------------------------+
889         |   Standard check of p_commit   |
890         +--------------------------------*/
891 
892         IF FND_API.To_Boolean( p_commit )
893         THEN
894             IF PG_DEBUG in ('Y', 'C') THEN
895                arp_util.debug('Activity_Unapplication: ' || 'committing');
896             END IF;
897               Commit;
898         END IF;
899 
900         IF PG_DEBUG in ('Y', 'C') THEN
901            arp_util.debug('ar_cm_application_pub.Activity_unapplication()- ');
902         END IF;
903 
904 
905 EXCEPTION
906        WHEN FND_API.G_EXC_ERROR THEN
907 
908                 IF PG_DEBUG in ('Y', 'C') THEN
909                    arp_util.debug('Activity_Unapplication: ' || SQLCODE, G_MSG_ERROR);
910                    arp_util.debug('Activity_Unapplication: ' || SQLERRM, G_MSG_ERROR);
911                 END IF;
912 
913                 ROLLBACK TO Activity_unapplication_PVT;
914                 x_return_status := FND_API.G_RET_STS_ERROR ;
915 
916               --  Display_Parameters;
917 
918                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
919                                            p_count       =>      x_msg_count,
920                                            p_data        =>      x_msg_data
921                                          );
922 
923         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
924 
925                 IF PG_DEBUG in ('Y', 'C') THEN
926                    arp_util.debug('Activity_Unapplication: ' || SQLERRM, G_MSG_ERROR);
927                 END IF;
928                 ROLLBACK TO Activity_unapplication_PVT;
929                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
930 
931                --  Display_Parameters;
932 
933                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
934                                            p_count       =>      x_msg_count,
935                                            p_data        =>      x_msg_data
936                                          );
937 
938         WHEN OTHERS THEN
939 
940                /*-------------------------------------------------------+
941                 |  Handle application errors that result from trapable  |
942                 |  error conditions. The error messages have already    |
943                 |  been put on the error stack.                         |
944                 +-------------------------------------------------------*/
945 
946                 IF (SQLCODE = -20001)
947                 THEN
948 
949                       ROLLBACK TO Activity_unapplication_PVT;
950 
951                       x_return_status := FND_API.G_RET_STS_ERROR ;
952                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
953                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ACTIVITY_UNAPPLICATION : '||SQLERRM);
954                       FND_MSG_PUB.Add;
955 
956                       --If only one error message on the stack,
957                       --retrive it
958                       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
959                                                  p_count  =>  x_msg_count,
960                                                  p_data   => x_msg_data
961                                                 );
962 
963                       RETURN;
964 
965                 ELSE
966                       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
967                       FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
968                       FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ACTIVITY_UNAPPLICATION : '||SQLERRM);
969                       FND_MSG_PUB.Add;
970                 END IF;
971 
972                 IF PG_DEBUG in ('Y', 'C') THEN
973                    arp_util.debug('Activity_Unapplication: ' || SQLCODE, G_MSG_ERROR);
974                    arp_util.debug('Activity_Unapplication: ' || SQLERRM, G_MSG_ERROR);
975                 END IF;
976 
977                 ROLLBACK TO Activity_unapplication_PVT;
978 
979                 IF      FND_MSG_PUB.Check_Msg_Level
980                 THEN
981                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
982                                         l_api_name
983                                        );
984                 END IF;
985 
986              --   Display_Parameters;
987 
988                 FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
989                                            p_count       =>      x_msg_count,
990                                            p_data        =>      x_msg_data
991                                          );
992 END Activity_unapplication;
993 
994 
995 --bug 14635750, ER for unapply of regular cm
996 PROCEDURE unapply_regular_cm(
997 		-- Standard API parameters.
998 		p_api_version               IN  NUMBER,
999 		p_init_msg_list             IN  VARCHAR2 := FND_API.G_FALSE,
1000 		p_commit                    IN  VARCHAR2 := FND_API.G_FALSE,
1001 		p_validation_level          IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL,
1002 		x_return_status             OUT NOCOPY VARCHAR2 ,
1003 		x_msg_count                 OUT NOCOPY NUMBER ,
1004 		x_msg_data                  OUT NOCOPY VARCHAR2 ,
1005 		-- *** Credit Memo Info. parameters *****
1006 		p_cm_customer_trx_id        IN	ra_customer_trx.customer_trx_id%TYPE DEFAULT NULL,
1007 		p_cm_trx_number				IN	ra_customer_trx.trx_number%TYPE DEFAULT NULL,
1008 		p_app_customer_trx_id		IN	ra_customer_trx.customer_trx_id%TYPE DEFAULT NULL,
1009 		p_app_trx_number			IN	ra_customer_trx.trx_number%TYPE DEFAULT NULL,
1010 		p_installment				IN	ar_payment_schedules.terms_sequence_number%TYPE DEFAULT NULL,
1011 		p_applied_payment_schedule_id IN	ar_payment_schedules.payment_schedule_id%TYPE DEFAULT NULL,
1012 		p_receivable_application_id IN	ar_receivable_applications.receivable_application_id%TYPE DEFAULT NULL,
1013 		p_reversal_gl_date			IN	ar_receivable_applications.reversal_gl_date%TYPE DEFAULT NULL,
1014 		p_called_from				IN	VARCHAR2 DEFAULT NULL,
1015 		p_org_id             		IN NUMBER  DEFAULT NULL
1016 	)
1017 IS
1018 
1019 	l_api_name       CONSTANT VARCHAR2(20) := 'unapply_regular_cm';
1020 	l_api_version    CONSTANT NUMBER       := 1.0;
1021 
1022 	l_cm_customer_trx_id     		ra_customer_trx.customer_trx_id%TYPE := p_cm_customer_trx_id;
1023 	l_cm_trx_number			 		ra_customer_trx.trx_number%TYPE := p_cm_trx_number;
1024 	l_inv_customer_trx_id    		ra_customer_trx.customer_trx_id%TYPE := p_app_customer_trx_id;
1025 	l_inv_trx_number		 		ra_customer_trx.trx_number%TYPE := p_app_trx_number;
1026 	l_installment			 		ar_payment_schedules.terms_sequence_number%TYPE := p_installment;
1027 	l_applied_payment_schedule_id  ar_payment_schedules.payment_schedule_id%TYPE := p_applied_payment_schedule_id;
1028 	l_receivable_application_id    ar_receivable_applications.receivable_application_id%TYPE := p_receivable_application_id;
1029 	l_reversal_gl_date       		ar_receivable_applications.reversal_gl_date%TYPE := trunc(p_reversal_gl_date);
1030 	l_apply_gl_date          DATE;
1031 	l_cm_gl_date             DATE;
1032 
1033 	l_def_ids_return_status VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
1034 	l_val_return_status     VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
1035 
1036 	l_org_return_status  VARCHAR2(1);
1037 	l_org_id             NUMBER;
1038 
1039 BEGIN
1040 
1041 	IF PG_DEBUG in ('Y', 'C') THEN
1042 	   arp_util.debug('AR_CM_APPLICATION_PUB.unapply_regular_cm()+ ');
1043 	END IF;
1044 
1045 	/*------------------------------------+
1046 	|   Standard start of API savepoint  |
1047 	+------------------------------------*/
1048 	SAVEPOINT Unapply_reg_CM;
1049 
1050 	/*--------------------------------------------------+
1051 	|   Standard call to check for call compatibility  |
1052 	+--------------------------------------------------*/
1053 	IF NOT FND_API.Compatible_API_Call(
1054 										l_api_version,
1055 										p_api_version,
1056 										l_api_name,
1057 										G_PKG_NAME
1058 									  )
1059 	THEN
1060 		  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1061 	END IF;
1062 
1063 
1064 	/*--------------------------------------------------------------+
1065 	|   Initialize message list if p_init_msg_list is set to TRUE  |
1066 	+--------------------------------------------------------------*/
1067 	IF FND_API.to_Boolean( p_init_msg_list )
1068 	  THEN
1069 		  FND_MSG_PUB.initialize;
1070 	END IF;
1071 
1072 
1073 	/*-------------------------------------------------+
1074 	| Initialize SOB/org dependent variables          |
1075 	+-------------------------------------------------*/
1076 	l_org_id            := p_org_id;
1077 	l_org_return_status := FND_API.G_RET_STS_SUCCESS;
1078 	ar_mo_cache_utils.set_org_context_in_api(
1079 		  p_org_id =>l_org_id,
1080 		  p_return_status =>l_org_return_status
1081 	);
1082 
1083 	IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1084 		  x_return_status := FND_API.G_RET_STS_ERROR;
1085 	ELSE
1086 
1087 		/*-----------------------------------------+
1088         |   Initialize return status to SUCCESS   |
1089         +-----------------------------------------*/
1090 
1091         x_return_status := FND_API.G_RET_STS_SUCCESS;
1092        /*---------------------------------------------+
1093         |   ========== Start of API Body ==========   |
1094         +---------------------------------------------*/
1095 
1096         --Derive the id's for the entered values and if both the
1097         --values and the id's superceed the values
1098         ar_cm_val_pvt.default_unapp_ids(
1099 						l_cm_trx_number ,
1100 						l_cm_customer_trx_id ,
1101 						l_inv_trx_number,
1102 						l_inv_customer_trx_id ,
1103 						l_receivable_application_id,
1104 						l_installment ,
1105 						l_applied_payment_schedule_id ,
1106 						l_apply_gl_date,
1107 						l_def_ids_return_status
1108 					);
1109 
1110 		IF PG_DEBUG in ('Y', 'C') THEN
1111 			arp_util.debug('after defaulting the unapp ids');
1112 			arp_util.debug('l_cm_trx_number:'||l_cm_trx_number);
1113 			arp_util.debug('l_cm_customer_trx_id:'||l_cm_customer_trx_id);
1114 			arp_util.debug('l_inv_trx_number:'||l_inv_trx_number);
1115 			arp_util.debug('l_inv_customer_trx_id:'||l_inv_customer_trx_id);
1116 			arp_util.debug('l_receivable_application_id:'||l_receivable_application_id);
1117 			arp_util.debug('l_installment:'||l_installment);
1118 			arp_util.debug('l_applied_payment_schedule_id:'||l_applied_payment_schedule_id);
1119 			arp_util.debug('l_apply_gl_date:'||l_apply_gl_date);
1120 			arp_util.debug('l_def_ids_return_status:'||l_def_ids_return_status);
1121 		END IF;
1122 
1123         ar_cm_val_pvt.default_unapp_info(
1124 						l_receivable_application_id,
1125 						l_apply_gl_date,
1126 						l_cm_customer_trx_id,
1127 						l_reversal_gl_date,
1128 						l_cm_gl_date
1129 					);
1130 
1131 		IF PG_DEBUG in ('Y', 'C') THEN
1132 			arp_util.debug('after defaulting the unapp info');
1133 			arp_util.debug('l_receivable_application_id:'||l_receivable_application_id);
1134 			arp_util.debug('l_apply_gl_date:'||l_apply_gl_date);
1135 			arp_util.debug('l_cm_customer_trx_id: '|| l_cm_customer_trx_id );
1136 			arp_util.debug('l_reversal_gl_date: '|| l_reversal_gl_date );
1137 			arp_util.debug('l_cm_gl_date: '|| l_cm_gl_date );
1138 		END IF;
1139 
1140 
1141 		ar_cm_val_pvt.validate_unapp_info(
1142 							l_cm_gl_date,
1143 							l_receivable_application_id,
1144 							l_reversal_gl_date,
1145 							l_apply_gl_date,
1146 							l_val_return_status
1147 						);
1148 
1149 		IF PG_DEBUG in ('Y', 'C') THEN
1150 			arp_util.debug('after validating the default un app info');
1151 			arp_util.debug(  'validation return status :'||l_val_return_status);
1152 		END IF;
1153 
1154 		IF l_def_ids_return_status <> FND_API.G_RET_STS_SUCCESS OR
1155             l_val_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1156             x_return_status := FND_API.G_RET_STS_ERROR ;
1157         END IF;
1158 
1159     END IF; -- Closing IF for ORG_RET_STATUS
1160 
1161     IF x_return_status <> FND_API.G_RET_STS_SUCCESS
1162 	THEN
1163 
1164 		ROLLBACK TO Unapply_reg_CM;
1165 
1166 		x_return_status := FND_API.G_RET_STS_ERROR ;
1167 
1168 		FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
1169 								p_count => x_msg_count,
1170 								p_data  => x_msg_data
1171 							   );
1172 
1173 		IF PG_DEBUG in ('Y', 'C') THEN
1174 			arp_util.debug(  'Error(s) occurred. Rolling back and setting status to ERROR');
1175 		END IF;
1176 		Return;
1177 	END IF;
1178 
1179 	--call the entity handler.
1180 	BEGIN
1181 		arp_process_application.reverse_cm_app(
1182 							l_receivable_application_id,
1183 							l_applied_payment_schedule_id,
1184 							l_reversal_gl_date,
1185 							trunc(sysdate),
1186 							'CMAPI',
1187 							p_api_version
1188 						);
1189 
1190 	EXCEPTION
1191 		WHEN OTHERS THEN
1192 		   /*-------------------------------------------------------+
1193 			|  Handle application errors that result from trapable  |
1194 			|  error conditions. The error messages have already    |
1195 			|  been put on the error stack.                         |
1196 			+-------------------------------------------------------*/
1197 			IF (SQLCODE = -20001)
1198 			THEN
1199 				ROLLBACK TO Unapply_reg_CM;
1200 
1201 				--  Display_Parameters;
1202 				x_return_status := FND_API.G_RET_STS_ERROR ;
1203 				FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
1204 				FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_PROCESS_APPLICATION.REVERSE_CM_APP : '||SQLERRM);
1205 				FND_MSG_PUB.Add;
1206 
1207 				FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
1208 										  p_count  =>  x_msg_count,
1209 										  p_data   => x_msg_data
1210 										);
1211 				RETURN;
1212 			ELSE
1213 			   RAISE;
1214 			END IF;
1215 	END;
1216 	--end of anon plsql block used for callout
1217 
1218 	/*--------------------------------+
1219 	|   Standard check of p_commit   |
1220 	+--------------------------------*/
1221 	IF FND_API.To_Boolean( p_commit )
1222 	THEN
1223 		IF PG_DEBUG in ('Y', 'C') THEN
1224 		   arp_util.debug('committing');
1225 		END IF;
1226 		Commit;
1227 	END IF;
1228 
1229 	IF PG_DEBUG in ('Y', 'C') THEN
1230 	   arp_util.debug('AR_CM_APPLICATION_PUB.unapply_regular_cm()-');
1231 	END IF;
1232 
1233 EXCEPTION
1234     WHEN FND_API.G_EXC_ERROR THEN
1235 		IF PG_DEBUG in ('Y', 'C') THEN
1236 		   arp_util.debug('Exception in AR_CM_APPLICATION_PUB.unapply_regular_cm - FND_API.G_EXC_ERROR');
1237 		   arp_util.debug(  SQLCODE, G_MSG_ERROR);
1238 		   arp_util.debug(  SQLERRM, G_MSG_ERROR);
1239 		END IF;
1240 
1241 		ROLLBACK TO Unapply_reg_CM;
1242 
1243 		x_return_status := FND_API.G_RET_STS_ERROR ;
1244 		--  Display_Parameters;
1245 		FND_MSG_PUB.Count_And_Get(
1246 				p_encoded => FND_API.G_FALSE,
1247 				p_count => x_msg_count,
1248 				p_data  => x_msg_data
1249 			);
1250 
1251     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1252 		IF PG_DEBUG in ('Y', 'C') THEN
1253 			arp_util.debug('Exception in AR_CM_APPLICATION_PUB.unapply_regular_cm - FND_API.G_EXC_UNEXPECTED_ERROR');
1254 		   arp_util.debug(  SQLERRM, G_MSG_ERROR);
1255 		END IF;
1256 
1257 		ROLLBACK TO Unapply_reg_CM;
1258 
1259 		x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1260 
1261 		--  Display_Parameters;
1262 		FND_MSG_PUB.Count_And_Get(
1263 				p_encoded => FND_API.G_FALSE,
1264 				p_count => x_msg_count,
1265 				p_data  => x_msg_data
1266 			);
1267 
1268     WHEN OTHERS THEN
1269         /*-------------------------------------------------------+
1270 		|  Handle application errors that result from trapable  |
1271 		|  error conditions. The error messages have already    |
1272 		|  been put on the error stack.                         |
1273 		+-------------------------------------------------------*/
1274 
1275 		IF PG_DEBUG in ('Y', 'C') THEN
1276 			arp_util.debug('Exception in AR_CM_APPLICATION_PUB.unapply_regular_cm - OTHERS');
1277 		   arp_util.debug(  SQLCODE, G_MSG_ERROR);
1278 		   arp_util.debug(  SQLERRM, G_MSG_ERROR);
1279 		END IF;
1280 
1281 		IF (SQLCODE = -20001)
1282 		THEN
1283 
1284 			ROLLBACK TO Unapply_reg_CM;
1285 
1286 			x_return_status := FND_API.G_RET_STS_ERROR ;
1287 			FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
1288 			FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','UNAPPLY : '||SQLERRM);
1289 			FND_MSG_PUB.Add;
1290 
1291 			--If only one error message on the stack,retrive it
1292 
1293 			FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
1294 									 p_count  =>  x_msg_count,
1295 									 p_data   => x_msg_data
1296 									);
1297 
1298 			RETURN;
1299 
1300 		ELSE
1301 			x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1302 			FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
1303 			FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','UNAPPLY : '||SQLERRM);
1304 			FND_MSG_PUB.Add;
1305 		END IF;
1306 
1307 
1308 
1309 		ROLLBACK TO Unapply_reg_CM;
1310 
1311 		IF  FND_MSG_PUB.Check_Msg_Level THEN
1312 			FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
1313 								l_api_name
1314 							   );
1315 		END IF;
1316 
1317 		-- Display_Parameters;
1318 
1319 		FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
1320 								   p_count       =>      x_msg_count,
1321 								   p_data        =>      x_msg_data
1322 								 );
1323 
1324 END unapply_regular_cm;
1325 
1326 
1327 END AR_CM_APPLICATION_PUB;