DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_DEDUCTION_COVER2

Source


1 PACKAGE BODY ARP_DEDUCTION_COVER2 AS
2 /* $Header: ARXDC2VB.pls 120.2 2005/10/30 03:59:28 appldev noship $ */
3 
4 /*=======================================================================+
5  |  Package Global Constants
6  +=======================================================================*/
7   G_PKG_NAME     CONSTANT VARCHAR2(30) := 'ARP_DEDUCTION_COVER2';
8   PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('AFLOG_ENABLED'), 'N');
9 
10 /*========================================================================
11  | Prototype Declarations Procedures
12  *=======================================================================*/
13 
14 
15 /*========================================================================
16  | Prototype Declarations Functions
17  *=======================================================================*/
18 
19 /*========================================================================
20  | PUBLIC PROCEDURE reapply_credit_memo
21  |
22  | DESCRIPTION
23  |      ----------------------------------------
24  |      This procedure calls entity handlers to unapply and reapply a
25  |      credit memo with a revised amount applied.
26  |      Typically for an on account credit memo used to settle
27  |	more than 1 deduction on the same receipt.
28  |
29  | CALLED FROM PROCEDURES/FUNCTIONS (local to this package body)
30  |
31  | CALLS PROCEDURES/FUNCTIONS (local to this package body)
32  |
33  | PARAMETERS
34  |      p_customer_trx_id    IN    Transaction  being reapplied
35  |      p_cash_receipt_id    IN    Receipt to which it is applied
36  |      p_amount_applied     IN    New amount applied
37  |      p_init_msg_list      IN
38  |      x_return_status      OUT NOCOPY
39  |      x_msg_count          OUT NOCOPY
40  |      x_msg_data           OUT NOCOPY
41  |
42  | KNOWN ISSUES                                                                  |
43  | NOTES:  This is a group API intended for use by Trade Management only
44  |
45  | MODIFICATION HISTORY
46  | Date          Author            Description of Changes
47  | 01-APR-2005   jbeckett          Created
48  |
49  *=======================================================================*/
50 PROCEDURE reapply_credit_memo(
51                 p_customer_trx_id IN  NUMBER,
52                 p_cash_receipt_id IN  NUMBER,
53                 p_amount_applied  IN  NUMBER,
54                 p_init_msg_list   IN  VARCHAR2 DEFAULT FND_API.G_FALSE,
55                 x_return_status   OUT NOCOPY VARCHAR2,
56                 x_msg_count       OUT NOCOPY NUMBER,
57                 x_msg_data        OUT NOCOPY VARCHAR2)
58 IS
59    l_receipt_number                ar_cash_receipts.receipt_number%TYPE;
60    l_receipt_date                  DATE;
61    l_currency_code                 ar_cash_receipts.currency_code%TYPE;
62    l_cr_gl_date                    DATE;
63    l_cr_payment_schedule_id        NUMBER;
64    l_customer_id                   NUMBER;
65    l_ra_id			   NUMBER;
66    l_ra_rec                        ar_receivable_applications%ROWTYPE;
67    l_default_gl_date               DATE;
68    l_defaulting_rule_used          VARCHAR2(100);
69    l_bal_due_remaining             NUMBER;
70    l_attribute_rec                 ar_receipt_api_pub.attribute_rec_type;
71    l_global_attribute_rec          ar_receipt_api_pub.global_attribute_rec_type;
72 
73    l_error_message                 VARCHAR2(2000);
74    l_error_count                   NUMBER := 0;
75    l_msg_count                     NUMBER;
76    l_msg_data                      VARCHAR2(2000);
77    l_return_status                 VARCHAR2(1);
78    l_api_name                      CONSTANT VARCHAR2(30)
79                                             := 'reapply_credit_memo';
80 
81 BEGIN
82 
83   IF PG_DEBUG in ('Y', 'C') THEN
84      arp_standard.debug('ARP_DEDUCTION_COVER2.reapply_credit_memo()+');
85   END IF;
86 
87   -- Standard Start of API savepoint
88   SAVEPOINT	reapply_credit_memo_pvt;
89   -- Initialize message list if p_init_msg_list is set to TRUE.
90   IF FND_API.to_Boolean( p_init_msg_list )
91   THEN
92     FND_MSG_PUB.initialize;
93   END IF;
94   --  Initialize API return status to success
95   x_return_status := FND_API.G_RET_STS_SUCCESS;
96 
97   /*  initialize global variables */
98   arp_global.init_global;
99   arp_standard.init_standard;
100 
101  /*---------------------------------------------------------------------+
102   | 1) Check that a valid receipt has been passed                       |
103   +---------------------------------------------------------------------*/
104   IF NOT arp_deduction_cover.receipt_valid
105        (p_cash_receipt_id          =>  p_cash_receipt_id,
106         x_receipt_number           =>  l_receipt_number,
107         x_receipt_date             =>  l_receipt_date,
108         x_cr_gl_date               =>  l_cr_gl_date,
109         x_customer_id              =>  l_customer_id,
110 	x_currency_code            =>  l_currency_code,
111         x_cr_payment_schedule_id   =>  l_cr_payment_schedule_id)
112   THEN
113     l_error_count := l_error_count + 1;
114   END IF;
115 
116  /*---------------------------------------------------------------------+
117   | 2) Check that a valid applied credit memo has been passed           |
118   +---------------------------------------------------------------------*/
119   BEGIN
120     SELECT ra.receivable_application_id
121     INTO   l_ra_id
122     FROM   ar_receivable_applications ra
123          , ar_payment_schedules ps
124     WHERE  ps.payment_schedule_id = ra.applied_payment_schedule_id
125     AND    ra.cash_receipt_id =  p_cash_receipt_id
126     AND    ra.status = 'APP'
127     AND    ps.customer_trx_id = p_customer_trx_id
128     AND    ra.display = 'Y'
129     AND    ps.class = 'CM';
130   EXCEPTION
131     WHEN OTHERS THEN
132       FND_MESSAGE.SET_NAME('AR','ARTA_PAYMENT_SCHEDULE_NO_FOUND');
133       FND_MSG_PUB.Add;
134       l_error_count := l_error_count + 1;
135   END;
136 
137   arp_app_pkg.fetch_p( l_ra_id, l_ra_rec );
138   l_ra_rec.amount_applied_from := p_amount_applied * l_ra_rec.trans_to_receipt_rate;
139 
140  /*---------------------------------------------------------------------+
141   | 3) Default the GL date
142   +---------------------------------------------------------------------*/
143   IF (arp_util.validate_and_default_gl_date(
144                 nvl(l_ra_rec.gl_date,trunc(sysdate)),
145                 NULL,
146                 l_ra_rec.gl_date,
147                 NULL,
148                 NULL,
149                 NULL,
150                 NULL,
151                 NULL,
152                 'N',
153                 NULL,
154                 arp_global.set_of_books_id,
155                 222,
156                 l_default_gl_date,
157                 l_defaulting_rule_used,
158                 l_error_message) = TRUE) THEN
159            NULL;
160   ELSE
161          --we were not able to default the gl_date
162          --message put on the stack
163            FND_MESSAGE.SET_NAME('AR', 'GENERIC_MESSAGE');
164            FND_MESSAGE.SET_TOKEN('GENERIC_TEXT', l_error_message);
165            FND_MSG_PUB.Add;
166            l_error_count := l_error_count + 1;
167   END IF;
168 
169  /*---------------------------------------------------------------------+
170   | 4) Raise error if validation errors found                           |
171   +---------------------------------------------------------------------*/
172   IF (l_error_count > 0)
173   THEN
174     RAISE FND_API.G_EXC_ERROR;
175   END IF;
176 
177  /*---------------------------------------------------------------------+
178   | 5) Call the entity handler to unapply                               |
179   +---------------------------------------------------------------------*/
180   --lock the receipt before calling the entity handler
181        arp_cash_receipts_pkg.nowaitlock_p(p_cr_id => p_cash_receipt_id);
182   /* lock the payment schedule of the applied transaction */
183        arp_ps_pkg.nowaitlock_p (p_ps_id => l_ra_rec.applied_payment_schedule_id);
184 
185 
186   BEGIN
187     --call the entity handler.
188     arp_process_application.reverse(
189                                 l_ra_id,
190                                 l_default_gl_date,
191                                 trunc(sysdate),
192                                 'RAPI',
193                                 1.0,
194                                 l_bal_due_remaining,
195                                 'TRADE_MANAGEMENT'  );
196   EXCEPTION
197       WHEN OTHERS THEN
198 
199                /*-------------------------------------------------------+
200                 |  Handle application errors that result from trapable  |
201                 |  error conditions. The error messages have already    |
202                 |  been put on the error stack.                         |
203                 +-------------------------------------------------------*/
204 
205                 IF (SQLCODE = -20001)
206                 THEN
207                      ROLLBACK TO reapply_credit_memo_pvt;
208 
209                       --  Display_Parameters;
210                       x_return_status := FND_API.G_RET_STS_ERROR ;
211                        FND_MESSAGE.SET_NAME ('AR','GENERIC_MESSAGE');
212                        FND_MESSAGE.SET_TOKEN('GENERIC_TEXT','ARP_DEDUCTION_COVER2.reapply_credit_memo : '||SQLERRM);
213                        FND_MSG_PUB.Add;
214 
215                        FND_MSG_PUB.Count_And_Get( p_encoded => FND_API.G_FALSE,
216                                                   p_count  =>  x_msg_count,
217                                                   p_data   => x_msg_data
218                                                 );
219                       RETURN;
220                 ELSE
221                    RAISE;
222                 END IF;
223 
224        END;
225 
226  /*---------------------------------------------------------------------+
227   | 6) Call the receipt API to reapply                               |
228   +---------------------------------------------------------------------*/
229       l_attribute_rec.attribute_category := l_ra_rec.attribute_category;
230       l_attribute_rec.attribute1 := l_ra_rec.attribute1;
231       l_attribute_rec.attribute2 := l_ra_rec.attribute2;
232       l_attribute_rec.attribute3 := l_ra_rec.attribute3;
233       l_attribute_rec.attribute4 := l_ra_rec.attribute4;
234       l_attribute_rec.attribute5 := l_ra_rec.attribute5;
235       l_attribute_rec.attribute6 := l_ra_rec.attribute6;
236       l_attribute_rec.attribute7 := l_ra_rec.attribute7;
237       l_attribute_rec.attribute8 := l_ra_rec.attribute8;
238       l_attribute_rec.attribute9 := l_ra_rec.attribute9;
239       l_attribute_rec.attribute10 := l_ra_rec.attribute10;
240       l_attribute_rec.attribute11 := l_ra_rec.attribute11;
241       l_attribute_rec.attribute12 := l_ra_rec.attribute12;
242       l_attribute_rec.attribute13 := l_ra_rec.attribute13;
243       l_attribute_rec.attribute14 := l_ra_rec.attribute14;
244       l_attribute_rec.attribute15 := l_ra_rec.attribute15;
245 
246       l_global_attribute_rec.global_attribute_category := l_ra_rec.global_attribute_category;
247       l_global_attribute_rec.global_attribute1 := l_ra_rec.global_attribute1;
248       l_global_attribute_rec.global_attribute2 := l_ra_rec.global_attribute2;
249       l_global_attribute_rec.global_attribute3 := l_ra_rec.global_attribute3;
250       l_global_attribute_rec.global_attribute4 := l_ra_rec.global_attribute4;
251       l_global_attribute_rec.global_attribute5 := l_ra_rec.global_attribute5;
252       l_global_attribute_rec.global_attribute6 := l_ra_rec.global_attribute6;
253       l_global_attribute_rec.global_attribute7 := l_ra_rec.global_attribute7;
254       l_global_attribute_rec.global_attribute8 := l_ra_rec.global_attribute8;
255       l_global_attribute_rec.global_attribute9 := l_ra_rec.global_attribute9;
256       l_global_attribute_rec.global_attribute10 := l_ra_rec.global_attribute10;
257       l_global_attribute_rec.global_attribute11 := l_ra_rec.global_attribute11;
258       l_global_attribute_rec.global_attribute12 := l_ra_rec.global_attribute12;
259       l_global_attribute_rec.global_attribute13 := l_ra_rec.global_attribute13;
260       l_global_attribute_rec.global_attribute14 := l_ra_rec.global_attribute14;
261       l_global_attribute_rec.global_attribute15 := l_ra_rec.global_attribute15;
262       l_global_attribute_rec.global_attribute16 := l_ra_rec.global_attribute16;
263       l_global_attribute_rec.global_attribute17 := l_ra_rec.global_attribute17;
264       l_global_attribute_rec.global_attribute18 := l_ra_rec.global_attribute18;
265       l_global_attribute_rec.global_attribute19 := l_ra_rec.global_attribute19;
266       l_global_attribute_rec.global_attribute20 := l_ra_rec.global_attribute20;
267 
268       ar_receipt_api_pub.apply     (
269       p_api_version                  =>  1.0,
270       p_init_msg_list                =>  FND_API.G_FALSE,
271       x_return_status                =>  l_return_status,
272       x_msg_count                    =>  l_msg_count,
273       x_msg_data                     =>  l_msg_data,
274       p_cash_receipt_id              =>  p_cash_receipt_id,
275       p_customer_trx_id              =>  p_customer_trx_id,
276       p_amount_applied               =>  p_amount_applied,
277       p_amount_applied_from          =>  l_ra_rec.amount_applied_from,
278       p_trans_to_receipt_rate        =>  l_ra_rec.trans_to_receipt_rate ,
279       p_apply_date                   =>  l_ra_rec.apply_date,
280       p_apply_gl_date                =>  l_default_gl_date,
281       p_called_from		     =>  'TRADE_MANAGEMENT',
282       p_attribute_rec                =>  l_attribute_rec,
283       p_global_attribute_rec         =>  l_global_attribute_rec,
284       p_comments                     =>  l_ra_rec.comments);
285   IF l_return_status = 'E'
286   THEN
287       RAISE FND_API.G_EXC_ERROR;
288   ELSIF l_return_status = 'U'
289   THEN
290       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
291   END IF;
292 
293   IF PG_DEBUG in ('Y', 'C') THEN
294      arp_standard.debug('ARP_DEDUCTION_COVER2.reapply_credit_memo()-');
295   END IF;
296 
297 EXCEPTION
298     WHEN FND_API.G_EXC_ERROR THEN
299                 ROLLBACK TO reapply_credit_memo_pvt;
300                 x_return_status := FND_API.G_RET_STS_ERROR ;
301                 FND_MSG_PUB.Count_And_Get
302                            (p_encoded => FND_API.G_FALSE,
303                             p_count   => x_msg_count,
304                             p_data    => x_msg_data);
305     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
306                 IF PG_DEBUG in ('Y', 'C') THEN
307                    arp_standard.debug('reapply_credit_memo: ' || 'Unexpected error '||sqlerrm||
308                 ' at arp_deduction_cover2.reapply_credit_memo()+');
309                 END IF;
310                 ROLLBACK TO reapply_credit_memo_pvt;
311                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
312                 FND_MSG_PUB.Count_And_Get
313                            (p_encoded => FND_API.G_FALSE,
314                             p_count   => x_msg_count,
315                             p_data    => x_msg_data);
316     WHEN OTHERS THEN
317                 IF (SQLCODE = -20001)
318                 THEN
319                   IF PG_DEBUG in ('Y', 'C') THEN
320                      arp_util.debug('reapply_credit_memo: ' || '20001 error '||
321                     ' at arp_deduction_cover2.reapply_credit_memo()+');
322                   END IF;
323                  x_return_status := FND_API.G_RET_STS_ERROR ;
324                ELSE
325                  IF PG_DEBUG in ('Y', 'C') THEN
326                     arp_util.debug('reapply_credit_memo: ' || 'Unexpected error '||sqlerrm||
327                    ' at arp_deduction_cover2.reapply_credit_memo()+');
328                  END IF;
329                  x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
330                  IF    FND_MSG_PUB.Check_Msg_Level
331                        (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
332                  THEN
333                        FND_MSG_PUB.Add_Exc_Msg
334                        (       G_PKG_NAME          ,
335                                l_api_name
336                        );
337                  END IF;
338                END IF;
339                ROLLBACK TO reapply_credit_memo_pvt;
340                FND_MSG_PUB.Count_And_Get
341                           (p_encoded => FND_API.G_FALSE,
342                            p_count   => x_msg_count,
343                            p_data    => x_msg_data);
344 END reapply_credit_memo;
345 
346 
347 END ARP_DEDUCTION_COVER2;