DBA Data[Home] [Help]

PACKAGE BODY: APPS.AR_INVOICE_API_PUB

Source


1 PACKAGE BODY AR_INVOICE_API_PUB AS
2 /* $Header: ARXPINVB.pls 120.26.12010000.2 2008/11/12 10:12:46 ankuagar ship $ */
3 
4 pg_debug VARCHAR2(1) := nvl(fnd_profile.value('AFLOG_ENABLED'),'N');
5 
6 g_one_time_init_org   number;
7 g_single_invoice boolean := FALSE;
8 
9 PROCEDURE populate_header  (
10     p_trx_header_tbl            IN          trx_header_tbl_type,
11     p_trx_system_param_rec      IN          AR_INVOICE_DEFAULT_PVT.trx_system_parameters_rec_type,
12     p_trx_profile_rec           IN          AR_INVOICE_DEFAULT_PVT.trx_profile_rec_type,
13     p_batch_source_rec          IN          batch_source_rec_type,
14     x_errmsg                    OUT NOCOPY  VARCHAR2,
15     x_return_status             OUT NOCOPY  VARCHAR2) IS
16 
17 BEGIN
18 
19   x_return_status := FND_API.G_RET_STS_SUCCESS;
20   IF pg_debug = 'Y' THEN
21     ar_invoice_utils.debug ('AR_INVOICE_API_PUB.populate_header(+)' );
22     ar_invoice_utils.debug ('All Default Values ' );
23     ar_invoice_utils.debug ('Set of Books Id '||
24       p_trx_system_param_rec.set_of_books_id);
25     ar_invoice_utils.debug ('Trx Currency '||
26       p_trx_system_param_rec.base_currency_code);
27     ar_invoice_utils.debug ('Batch Source '||
28       p_trx_profile_rec.ar_ra_batch_source);
29     ar_invoice_utils.debug ('GL Date '|| p_batch_source_rec.default_date);
30     ar_invoice_utils.debug ('Exchange Rate Type '
31        || p_trx_profile_rec.default_exchange_rate_type);
32   END IF;
33 
34   -- First populate the global header table with the user parameter.
35   ar_trx_global_process_header.insert_row(
36     p_trx_header_tbl   => p_trx_header_tbl,
37     p_batch_source_rec => p_batch_source_rec,
38     x_errmsg           => x_errmsg,
39     x_return_status    => x_return_status);
40 
41   IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
42     RETURN;
43   END IF;
44 
45    /* 5921925 - Removed defaulting UPDATE statement and integrated it
46        into ar_trx_global_process_header.insert_row */
47 
48    IF pg_debug = 'Y' THEN
49      ar_invoice_utils.debug ('AR_INVOICE_API_PUB.populate_header(-)' );
50    END IF;
51 
52    EXCEPTION
53      WHEN OTHERS THEN
54       x_errmsg := 'Error in AR_INVOICE_API_PUB.populate_header '||sqlerrm;
55       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
56       RETURN;
57 
58 END;
59 
60 PROCEDURE populate_lines  (
61   p_trx_lines_tbl        trx_line_tbl_type,
62   p_trx_system_param_rec ar_invoice_default_pvt.trx_system_parameters_rec_type,
63   x_errmsg                    OUT NOCOPY  VARCHAR2,
64   x_return_status             OUT NOCOPY  VARCHAR2) IS
65 
66   l_customer_trx_line_id          NUMBER;
67 
68   CURSOR clink IS
69     SELECT customer_trx_line_id, link_to_trx_line_id, trx_line_id,
70 	   link_to_cust_trx_line_id
71     FROM   ar_trx_lines_gt
72     WHERE  link_to_trx_line_id IS NOT NULL
73     AND    line_type in ( 'TAX', 'FREIGHT');
74 
75   CURSOR line (p_link_line_id NUMBER) IS
76     SELECT customer_trx_line_id
77     FROM   ar_trx_lines_gt
78     WHERE  trx_line_id = p_link_line_id;
79 
80 BEGIN
81 
82     IF pg_debug = 'Y' THEN
83       ar_invoice_utils.debug ('AR_INVOICE_API_PUB.populate_lines(+)' );
84     END IF;
85 
86     x_return_status := FND_API.G_RET_STS_SUCCESS;
87 
88     AR_TRX_GLOBAL_PROCESS_LINES.INSERT_ROW(
89             p_trx_lines_tbl     => p_trx_lines_tbl,
90             x_errmsg            => x_errmsg,
91             x_return_status    => x_return_status);
92 
93     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
94     THEN
95         return;
96     END IF;
97 
98     -- check if there are any freight and tax lines and whether it is linked
99     -- to any of the lines or not.
100     -- need to do bulk enable
101 
102     /* 5921925 - considered rewriting this, but I don't see an easy way
103        to do anything since both trx_id and trx_line_id are dynamically assigned
104        in the GT inserts. */
105 
106     FOR clinkRec IN clink
107     LOOP
108         OPEN line(clinkRec.link_to_trx_line_id);
109         FETCH line INTO l_customer_trx_line_id;
110         CLOSE line;
111 
112         IF pg_debug = 'Y' THEN
113           ar_invoice_utils.debug ('Cust Trx Line Id in Link Loop ' ||
114             l_customer_trx_line_id);
115           ar_invoice_utils.debug ('Link to  Line Id in Link Loop ' ||
116             clinkRec.link_to_trx_line_id );
117           ar_invoice_utils.debug ('Trx Line Id in Link Loop ' ||
118             clinkRec.trx_line_id );
119         END IF;
120 
121         UPDATE ar_trx_lines_gt gt
122             set link_to_cust_trx_line_id = l_customer_trx_line_id
123         WHERE  customer_trx_line_id = clinkRec.customer_trx_line_id;
124 
125     END LOOP;
126 
127     UPDATE ar_trx_lines_gt lgt
128     SET (customer_trx_id, trx_date, org_id, set_of_books_id,currency_code) =
129       ( SELECT hgt.customer_trx_id, hgt.trx_date, hgt.org_id,
130                hgt.set_of_books_id, trx_currency
131         FROM ar_trx_header_gt hgt
132         WHERE lgt.trx_header_id = trx_header_id),
133         request_id   =  AR_INVOICE_TABLE_HANDLER.g_request_id;
134 
135     IF pg_debug = 'Y'
136     THEN
137          ar_invoice_utils.debug ('AR_INVOICE_API_PUB.populate_lines(-)' );
138     END IF;
139 
140     EXCEPTION
141         WHEN OTHERS THEN
142             x_errmsg := 'Error in AR_INVOICE_API_PUB.populate_lines '||sqlerrm;
143             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
144             return;
145 END;
146 
147 
148 PROCEDURE populate_distributions  (
149     p_trx_dist_tbl         IN trx_dist_tbl_type,
150     p_batch_source_rec     IN batch_source_rec_type,
151     p_trx_system_param_rec IN
152       ar_invoice_default_pvt.trx_system_parameters_rec_type,
153     x_errmsg               OUT NOCOPY  VARCHAR2,
154     x_return_status        OUT NOCOPY  VARCHAR2 ) IS
155 
156   -- To work around an issue in 8i.
157   -- ORASHID
158   -- 16-OCT-2003
159 
160   null_column NUMBER := NULL;
161 
162 BEGIN
163 
164   IF pg_debug = 'Y' THEN
165     ar_invoice_utils.debug ('AR_INVOICE_API_PUB.populate_distributions(+)' );
166   END IF;
167 
168   x_return_status := FND_API.G_RET_STS_SUCCESS;
169 
170   --populate global dist. table
171   ar_trx_global_process_dist.insert_row(
172     p_trx_dist_tbl      =>  p_trx_dist_tbl,
173     x_errmsg            =>  x_errmsg,
174     x_return_status     =>  x_return_status);
175 
176   IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
177     return;
178   END IF;
179 
180   /* 5921925 - Should remove this but it would require us to rewrite
181      the internals of ar_trx_global_process_dist.insert_row */
182   UPDATE ar_trx_dist_gt dist
183   SET set_of_books_id = nvl(set_of_books_id,
184         p_trx_system_param_rec.set_of_books_id),
185         request_id   =  AR_INVOICE_TABLE_HANDLER.g_request_id,
186         /* gl_date = nvl(gl_date, nvl(p_batch_source_rec.default_date,
187         sysdate)),
188         Bug 3361235*/
189      (customer_trx_id, customer_trx_line_id, trx_header_id, gl_date) =
190      (
191        SELECT line.customer_trx_id,
192               line.customer_trx_line_id,
193               line.trx_header_id,
194               h.gl_date
195        FROM   ar_trx_lines_gt line,
196               ar_trx_header_gt h
197        WHERE  line.trx_line_id = dist.trx_line_id
198        AND    line.trx_header_id = h.trx_header_id
199        UNION
200        SELECT h.customer_trx_id, null_column, h.trx_header_id, h.gl_date
201        FROM   ar_trx_header_gt h
202        WHERE  h.trx_header_id = dist.trx_header_id
203        AND    dist.account_class = 'REC');
204 
205 
206   IF pg_debug = 'Y' THEN
207     ar_invoice_utils.debug ('AR_INVOICE_API_PUB.populate_distributions(-)' );
208   END IF;
209 
210   EXCEPTION
211     WHEN OTHERS THEN
212       x_errmsg := 'Error in AR_INVOICE_API_PUB.populate distributions'
213         || sqlerrm;
214       x_return_status := fnd_api.g_ret_sts_unexp_error;
215       RETURN;
216 
217 END populate_distributions;
218 
219 
220 PROCEDURE populate_salescredits  (
221     p_trx_salescredits_tbl              IN   trx_salescredits_tbl_type,
222     p_trx_system_param_rec              IN   AR_INVOICE_DEFAULT_PVT.trx_system_parameters_rec_type,
223     x_errmsg                            OUT NOCOPY  VARCHAR2,
224     x_return_status                     OUT NOCOPY  VARCHAR2 ) IS
225 
226 BEGIN
227     IF pg_debug = 'Y'
228     THEN
229          ar_invoice_utils.debug ('AR_INVOICE_API_PUB.populate_salescredits(+)' );
230     END IF;
231     x_return_status := FND_API.G_RET_STS_SUCCESS;
232     --populate global dist. table
233     AR_TRX_GLOBAL_PROCESS_SALESCR.INSERT_ROW(
234             p_trx_salescredits_tbl      =>  p_trx_salescredits_tbl,
235             x_errmsg                    =>  x_errmsg,
236             x_return_status             =>  x_return_status
237             );
238 
239     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
240     THEN
241      return;
242     END IF;
243 
244     /* 5921925 - Should remove this.  Have to rewrite
245         ar_trx_global_process_salescr.insert_row */
246     UPDATE ar_trx_salescredits_gt sc
247         SET org_id = nvl(org_id, p_trx_system_param_rec.org_id),
248             request_id   =  AR_INVOICE_TABLE_HANDLER.g_request_id,
249             (customer_trx_id, customer_trx_line_id, trx_header_id) =  (
250                     SELECT line.customer_trx_id, line.customer_trx_line_id,
251                            trx_header_id
252                     FROM  ar_trx_lines_gt line
253                     WHERE line.trx_line_id = sc.trx_line_id
254                     AND rownum = 1);
255 
256 
257     IF pg_debug = 'Y'
258     THEN
259          ar_invoice_utils.debug ('AR_INVOICE_API_PUB.populate_salescredits(-)' );
260     END IF;
261     EXCEPTION
262             WHEN OTHERS THEN
263                 x_errmsg := 'Error in AR_INVOICE_API_PUB.populate sales credits '||sqlerrm;
264                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
265                 return;
266 
267 END populate_salescredits;
268 
269 -- new subroutine introduced for "Payment Based Revenue Managment" project
270 -- ORASHID 20-September-2004
271 
272 PROCEDURE populate_contingencies  (
273  p_trx_contingencies_tbl trx_contingencies_tbl_type,
274  p_trx_system_param_rec  ar_invoice_default_pvt.trx_system_parameters_rec_type,
275  x_errmsg        OUT NOCOPY  VARCHAR2,
276  x_return_status OUT NOCOPY  VARCHAR2 ) IS
277 
278 BEGIN
279 
280   IF pg_debug = 'Y' THEN
281     ar_invoice_utils.debug ('AR_INVOICE_API_PUB.populate_contingencies(+)' );
282   END IF;
283 
284   x_return_status := FND_API.G_RET_STS_SUCCESS;
285   -- populate global contingencies table
286 
287   ar_trx_global_process_cont.insert_row
288   (
289     p_trx_contingencies_tbl =>  p_trx_contingencies_tbl,
290     x_errmsg               =>  x_errmsg,
291     x_return_status        =>  x_return_status
292   );
293 
294   IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
295     RETURN;
296   END IF;
297 
298   /* 5921925 - Should remove this.. Have to rewrite
299      ar_trx_global_process_cont.insert_row */
300   UPDATE ar_trx_contingencies_gt tcg
301   SET    org_id       = nvl(org_id, p_trx_system_param_rec.org_id),
302          request_id   = ar_invoice_table_handler.g_request_id,
303          trx_header_id = (SELECT trx_header_id
304                           FROM  ar_trx_lines_gt tlg
305                           WHERE tlg.trx_line_id = tcg.trx_line_id
306                           AND   rownum = 1);
307 
308   IF pg_debug = 'Y' THEN
309     ar_invoice_utils.debug ('ar_invoice_api_pub.populate_contingencies(-)' );
310   END IF;
311 
312   EXCEPTION
313     WHEN OTHERS THEN
314       x_errmsg := 'error ar_invoice_api_pub.populate_contingencies: '||sqlerrm;
315       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
316       RETURN;
317 
318 END populate_contingencies;
319 
320 PROCEDURE clean_gt IS
321 
322 BEGIN
323 	delete from ar_trx_header_gt;
324 	delete from ar_trx_lines_gt;
325 	delete from ar_trx_dist_gt;
326 	delete from ar_trx_salescredits_gt;
327 	--delete from ar_trx_errors_gt;
328 	DELETE FROM ZX_TRX_HEADERS_GT;
329 	DELETE FROM ZX_TRANSACTION_LINES_GT;
330 	DELETE FROM ZX_IMPORT_TAX_LINES_GT;
331 END;
332 
333 PROCEDURE clean_tmp_gt IS
334 
335 BEGIN
336 	delete from ar_trx_header_tmp_gt;
337 	delete from ar_trx_lines_tmp_gt;
338 	delete from ar_trx_dist_tmp_gt;
339 	delete from ar_trx_salescredits_tmp_gt;
340 	--delete from ar_trx_errors_gt;
341 END;
342 
343 PROCEDURE CREATE_INVOICE(
344     p_api_version           IN      	NUMBER,
345     p_init_msg_list         IN      	VARCHAR2 := FND_API.G_FALSE,
346     p_commit                IN      	VARCHAR2 := FND_API.G_FALSE,
347     p_batch_source_rec      IN      	batch_source_rec_type DEFAULT NULL,
348     p_trx_header_tbl        IN      	trx_header_tbl_type,
349     p_trx_lines_tbl         IN      	trx_line_tbl_type,
350     p_trx_dist_tbl          IN          trx_dist_tbl_type,
351     p_trx_salescredits_tbl  IN          trx_salescredits_tbl_type,
352     p_trx_contingencies_tbl IN          trx_contingencies_tbl_type,
353     x_return_status         OUT NOCOPY  VARCHAR2,
354     x_msg_count             OUT NOCOPY  NUMBER,
355     x_msg_data              OUT NOCOPY  VARCHAR2) IS
356 
357     l_api_name       CONSTANT  VARCHAR2(30) := 'CREATE_INVOICE';
358     l_api_version    CONSTANT NUMBER       := 1.0;
359 
360     l_trx_system_parameters_rec   AR_INVOICE_DEFAULT_PVT.trx_system_parameters_rec_type;
361     l_trx_profile_rec             AR_INVOICE_DEFAULT_PVT.trx_profile_rec_type;
362     l_trx_date                    ra_customer_trx.trx_date%type;
363     x_errmsg                      VARCHAR2(2000);
364     l_batch_id                    NUMBER;
365 --anuj
366   cursor org_cur is
367      select org_id
368      from ar_trx_header_tmp_gt
369      group by org_id;
370     l_trx_header_tbl      trx_header_tbl_type;
371     l_trx_lines_tbl       trx_line_tbl_type;
372     l_trx_dist_tbl          trx_dist_tbl_type;
373     l_trx_salescredits_tbl trx_salescredits_tbl_type;
374     l_org_return_status VARCHAR2(1);
375     l_org_id                           NUMBER;
376 --anuj
377 
378 BEGIN
379 
380     IF pg_debug = 'Y'
381     THEN
382          ar_invoice_utils.debug ('AR_INVOICE_API_PUB.CREATE_INVOICE(2)(+)' );
383     END IF;
384 
385     SAVEPOINT Create_Invoice;
386     /*--------------------------------------------------+
387      |   Standard call to check for call compatibility  |
388     +--------------------------------------------------*/
389 
390     IF NOT FND_API.Compatible_API_Call(
394                       G_PKG_NAME
391                       l_api_version,
392                       p_api_version,
393                       l_api_name,
395                                 )
396     THEN
397         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
398     END IF;
399 
400        /*--------------------------------------------------------------+
401         |   Initialize message list if p_init_msg_list is set to TRUE  |
402         +--------------------------------------------------------------*/
403 
404     IF FND_API.to_Boolean( p_init_msg_list )
405     THEN
406         FND_MSG_PUB.initialize;
407     END IF;
408 
409 --anuj
410 clean_tmp_gt;
411 AR_TRX_GLOBAL_PROCESS_TMP.INSERT_ROWS (
412     p_trx_header_tbl    =>p_trx_header_tbl,
413     p_trx_lines_tbl     =>p_trx_lines_tbl,
414     p_trx_dist_tbl      =>p_trx_dist_tbl,
415     p_trx_salescredits_tbl  =>p_trx_salescredits_tbl,
416     x_errmsg     =>x_errmsg,
417     x_return_status  =>x_return_status);
418 
419 
420     IF pg_debug = 'Y'
421     THEN
422          ar_invoice_utils.debug ('Before looping thru invoice headers....' );
423          ar_invoice_utils.debug ('x_return_status = '|| x_return_status);
424          ar_invoice_utils.debug ('x_errmsg = '|| x_errmsg);
425     END IF;
426 
427  FOR org_rec in org_cur LOOP
428    AR_TRX_GLOBAL_PROCESS_TMP.GET_ROWS (
429     p_org_id    => org_rec.org_id,
430     p_trx_header_tbl       =>  l_trx_header_tbl,
431     p_trx_lines_tbl        => l_trx_lines_tbl,
432     p_trx_dist_tbl         =>  l_trx_dist_tbl,
433     p_trx_salescredits_tbl => l_trx_salescredits_tbl,
434     x_errmsg     => x_errmsg,
435     x_return_status => x_return_status);
436 
437          ar_invoice_utils.debug ('Looping thru invoice headers....' );
438          ar_invoice_utils.debug ('x_return_status = '|| x_return_status);
439          ar_invoice_utils.debug ('x_errmsg = '|| x_errmsg);
440 
441     l_org_id            := org_rec.org_id;
442     l_org_return_status := FND_API.G_RET_STS_SUCCESS;
443     ar_mo_cache_utils.set_org_context_in_api(p_org_id =>l_org_id,
444                                              p_return_status =>l_org_return_status);
445     ar_invoice_utils.debug ('l_org_id = '|| l_org_id);
446     ar_invoice_utils.debug ('l_org_return_status = '|| l_org_return_status);
447 
448     /* 6006015 - this logic differs from 11.5 because
449        the transactions are processed by org.  So we
450        have to init each time the org changes */
451     IF g_one_time_init_org <> l_org_id
452     THEN
453        ar_invoice_default_pvt.get_system_parameters(
454             p_trx_system_parameters_rec => l_trx_system_parameters_rec,
455             x_errmsg                    =>  x_errmsg,
456             x_return_status             =>  x_return_status);
457 
458        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
459        THEN
460            ROLLBACK to Create_Invoice;
461            x_msg_data := x_errmsg;
462            return;
463        END IF;
464 
465        -- Get the default values from profile options;
466        ar_invoice_default_pvt.Get_profile_values(
467            p_trx_profile_rec       =>   l_trx_profile_rec,
468            x_errmsg                =>  x_errmsg,
469            x_return_status         =>  x_return_status)   ;
470 
471        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
472        THEN
473            ROLLBACK to Create_Invoice;
474            x_msg_data := x_errmsg;
475            return;
476        END IF;
477     END IF;
478 
479  IF l_org_return_status <> FND_API.G_RET_STS_SUCCESS THEN
480        x_return_status := FND_API.G_RET_STS_ERROR;
481  ELSE
482 
483     -- first clean all global temporary tables to start with
484     clean_gt;
485 
486     IF (l_trx_header_tbl.COUNT = 0) OR (l_trx_lines_tbl.COUNT = 0) THEN
487       ROLLBACK to Create_Invoice;
488       x_return_status := fnd_api.g_ret_sts_unexp_error;
489       x_msg_data := arp_standard.fnd_message('AR_INAPI_TABLES_EMPTY');
490       RETURN;
491     END IF;
492 
493     IF pg_debug = 'Y'
494     THEN
495          ar_invoice_utils.debug ('Calling Default Rtn from ar_invoice_pub(-)' );
496          ar_invoice_utils.debug ('Create Batch(+)' );
497     END IF;
498 
499     /* 5921925 - only create a batch if they are not calling in single invoice
500         mode */
501     IF NOT g_single_invoice
502     THEN
503        -- Create a batch
504        AR_INVOICE_TABLE_HANDLER.create_batch(
505             p_trx_system_parameters_rec     => l_trx_system_parameters_rec,
506             p_trx_profile_rec               => l_trx_profile_rec,
507             p_batch_source_rec              => p_batch_source_rec,
508             p_batch_id                      => l_batch_id,
509             x_errmsg                        => x_errmsg,
510             x_return_status                 => x_return_status);
511 
512        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
513        THEN
514           ROLLBACK to Create_Invoice;
515           x_msg_data := x_errmsg;
516           return;
517        END IF;
518     ELSE
519        /* 5921925 - insure that we have a viable request_id for this batch
520            even if its only one transaction */
521        SELECT ra_batches_s.nextval * -1
522        INTO   ar_invoice_table_handler.g_request_id
526     --first popolate the global temp. table based on user passed variables and
523        FROM   dual;
524     END IF;
525 
527     -- default system parameters and profile values.
528     IF pg_debug = 'Y'
529     THEN
530          ar_invoice_utils.debug ('Create Batch(-)' );
531          ar_invoice_utils.debug ('populate header(+)' );
532     END IF;
533     populate_header( p_trx_header_tbl        => l_trx_header_tbl,
534                      p_trx_system_param_rec  => l_trx_system_parameters_rec,
535                      p_trx_profile_rec       => l_trx_profile_rec,
536                      p_batch_source_rec      => p_batch_source_rec,
537                      x_errmsg                =>  x_errmsg,
538                      x_return_status         =>  x_return_status);
539     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
540     THEN
541         ROLLBACK to Create_Invoice;
542         x_msg_data := x_errmsg;
543         return;
544     END IF;
545     IF pg_debug = 'Y'
546     THEN
547          ar_invoice_utils.debug ('populate header (-)' );
548          ar_invoice_utils.debug ('populate lines (+)' );
549     END IF;
550 
551     populate_lines ( p_trx_lines_tbl => l_trx_lines_tbl,
552                      p_trx_system_param_rec => l_trx_system_parameters_rec,
553                      x_errmsg                =>  x_errmsg,
554                      x_return_status         =>  x_return_status);
555 
556     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
557       ROLLBACK to Create_Invoice;
558       x_msg_data := x_errmsg;
559       RETURN;
560     END IF;
561 
562     -- Check for validations that spans across header and lines.
563 
564     IF pg_debug = 'Y' THEN
565       ar_invoice_utils.debug ('validate_master_detail' );
566     END IF;
567 
568     ar_invoice_utils.validate_master_detail
569       ( x_errmsg        =>  x_errmsg,
570         x_return_status =>  x_return_status);
571 
572     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
573       ROLLBACK to Create_Invoice;
574       x_msg_data := x_errmsg;
575       RETURN;
576     END IF;
577 
578     IF pg_debug = 'Y'
579     THEN
580          ar_invoice_utils.debug ('populate lines (-)' );
581          ar_invoice_utils.debug ('populate distributions (+)' );
582     END IF;
583 
584     /* 5921925 - only execute if there are rows */
585     IF p_trx_dist_tbl.count > 0
586     THEN
587        g_dist_exist := TRUE;
588        populate_distributions (p_trx_dist_tbl         => p_trx_dist_tbl,
589                             p_batch_source_rec     => p_batch_source_rec,
590                             p_trx_system_param_rec => l_trx_system_parameters_rec,
591                             x_errmsg                =>  x_errmsg,
592                             x_return_status         =>  x_return_status );
593 
594        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
595        THEN
596            ROLLBACK to Create_Invoice;
597            x_msg_data := x_errmsg;
598            return;
599        END IF;
600     ELSE
601        g_dist_exist := FALSE;
602     END IF;
603 
604     IF pg_debug = 'Y'
605     THEN
606          ar_invoice_utils.debug ('populate distributions (-)' );
607          ar_invoice_utils.debug ('populate sales credits (+)' );
608     END IF;
609 
610     /* 5921925 - only execute if rows exist */
611     IF p_trx_salescredits_tbl.count > 0
612     THEN
613        g_sc_exist := TRUE;
614        populate_salescredits  (p_trx_salescredits_tbl => p_trx_salescredits_tbl,
615                             p_trx_system_param_rec => l_trx_system_parameters_rec,
616                             x_errmsg                =>  x_errmsg,
617                             x_return_status         =>  x_return_status );
618 
619        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
620        THEN
621            ROLLBACK to Create_Invoice;
622            x_msg_data := x_errmsg;
623            return;
624        END IF;
625     ELSE
626        g_sc_exist := FALSE;
627     END IF;
628 
629     IF pg_debug = 'Y' THEN
630       ar_invoice_utils.debug('populate sales credits(-)' );
631       ar_invoice_utils.debug('populate contingencies(+)' );
632     END IF;
633 
634     /* 5921925 - only populate if rows exist */
635     IF p_trx_contingencies_tbl.count > 0
636     THEN
637        g_cont_exist := TRUE;
638        populate_contingencies(
639           p_trx_contingencies_tbl => p_trx_contingencies_tbl,
640           p_trx_system_param_rec  => l_trx_system_parameters_rec,
641           x_errmsg                => x_errmsg,
642           x_return_status         => x_return_status);
643 
644        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
645        THEN
646            ROLLBACK to Create_Invoice;
647            x_msg_data := x_errmsg;
648            return;
649        END IF;
650     ELSE
651        g_cont_exist := FALSE;
652     END IF;
653 
654     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
655       ROLLBACK TO Create_Invoice;
656       x_msg_data := x_errmsg;
657       RETURN;
658     END IF;
659 
660     -- ORASHID 20-Sep-2004
661     -- END
662 
663     IF pg_debug = 'Y'  THEN
667     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
664          ar_invoice_utils.debug ('populate contingencies(-)' );
665     END IF;
666 
668       ROLLBACK to Create_Invoice;
669       x_msg_data := x_errmsg;
670       RETURN;
671     END IF;
672 
673    -- Validate all inter-dependent parameters
674     IF pg_debug = 'Y'
675     THEN
676       ar_invoice_utils.debug ('validate_dependend_parameter' );
677     END IF;
678     ar_invoice_utils.validate_dependent_parameters
679             ( p_trx_system_param_rec => l_trx_system_parameters_rec,
680               x_errmsg               =>  x_errmsg,
681               x_return_status        =>  x_return_status);
682 
683     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
684     THEN
685         ROLLBACK to Create_Invoice;
686         x_msg_data := x_errmsg;
687         return;
688     END IF;
689 
690     -- Now validate all the values which user has passed and populate
691     -- any dependent fields.
692     IF pg_debug = 'Y'
693     THEN
694          ar_invoice_utils.debug ('validate_header from ar_invoice_pub (+)' );
695     END IF;
696 
697     ar_invoice_utils.validate_header
698             ( p_trx_system_param_rec   => l_trx_system_parameters_rec,
699               p_trx_profile_rec        => l_trx_profile_rec,
700               x_errmsg                 =>  x_errmsg,
701               x_return_status          =>  x_return_status);
702 
703     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
704     THEN
705         ROLLBACK to Create_Invoice;
706         x_msg_data := x_errmsg;
707         return;
708     END IF;
709 
710 
711     IF pg_debug = 'Y'
712     THEN
713          ar_invoice_utils.debug ('validate_header from ar_invoice_pub (-)' );
714          ar_invoice_utils.debug ('validate_lines from ar_invoice_pub (+)' );
715     END IF;
716 
717     ar_invoice_utils.validate_lines
718         ( x_errmsg            =>  x_errmsg,
719           x_return_status     =>  x_return_status);
720 
721     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
722     THEN
723         ROLLBACK to Create_Invoice;
724         x_msg_data := x_errmsg;
725         return;
726     END IF;
727     IF pg_debug = 'Y'
728     THEN
729          ar_invoice_utils.debug ('validate_lines from ar_invoice_pub (-)' );
730          ar_invoice_utils.debug ('validate_distributions from ar_invoice_pub (+)' );
731     END IF;
732 
733     /* 5921925 - prevent this call if no distributions passed */
734     IF g_dist_exist
735     THEN
736        ar_invoice_utils.validate_distributions
737         ( p_trx_system_parameters_rec => l_trx_system_parameters_rec,
738           x_errmsg            =>  x_errmsg,
739           x_return_status     =>  x_return_status);
740 
741        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
742        THEN
743            ROLLBACK to Create_Invoice;
744            x_msg_data := x_errmsg;
745            return;
746        END IF;
747     END IF;
748 
749     IF pg_debug = 'Y'
750     THEN
751          ar_invoice_utils.debug ('validate_distributions from ar_invoice_pub (-)' );
752          ar_invoice_utils.debug ('validate_salescredits from ar_invoice_pub (+)' );
753     END IF;
754 
755     IF g_sc_exist
756     THEN
757        ar_invoice_utils.validate_salescredits(
758          p_trx_system_param_rec => l_trx_system_parameters_rec,
759          x_errmsg              => x_errmsg,
760          x_return_status       => x_return_status);
761 
762        IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
763        THEN
764            ROLLBACK to Create_Invoice;
765            x_msg_data := x_errmsg;
766            return;
767        END IF;
768     END IF;
769 
770     IF pg_debug = 'Y'
771     THEN
772          ar_invoice_utils.debug ('validate_salescredits from ar_invoice_pub (-)' );
773          ar_invoice_utils.debug ('vaidate_gdf (+) ');
774     END IF;
775 
776     ar_invoice_utils.validate_gdf(
777       p_request_id          => AR_INVOICE_TABLE_HANDLER.g_request_id,
778       x_errmsg              => x_errmsg,
779       x_return_status       => x_return_status);
780 
781     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
782     THEN
783         ROLLBACK to Create_Invoice;
784         x_msg_data := x_errmsg;
785         return;
786     END IF;
787     IF pg_debug = 'Y'
788     THEN
789          ar_invoice_utils.debug ('validate_gdf (-)' );
790          ar_invoice_utils.debug ('Calling Table Handler ar_invoice_table_handler.insert_row (+) ');
791     END IF;
792 
793     AR_INVOICE_TABLE_HANDLER.insert_row(
794             p_trx_system_parameters_rec =>  l_trx_system_parameters_rec,
795             p_trx_profile_rec           =>  l_trx_profile_rec,
796             p_batch_source_rec          =>  p_batch_source_rec,
797             x_errmsg                    =>  x_errmsg,
798             x_return_status             =>  x_return_status)  ;
799 
800     IF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR
801     THEN
802         ROLLBACK to Create_Invoice;
803         x_msg_data := x_errmsg;
804         return;
805     END IF;
806 
807     -- Standard check of p_commit.
811     END IF;
808     IF FND_API.To_Boolean( p_commit )
809     THEN
810       COMMIT;
812  END IF;
813 
814  END LOOP; --anuj
815 
816 
817 --{Call creation of the events should outside the loop as not org striped
818 --
819  ar_invoice_utils.debug ('Call creation of XLA events in bulk mode +' );
820  ar_invoice_utils.debug ('  Using the request_id :'||AR_INVOICE_TABLE_HANDLER.g_request_id);
821 
822  arp_xla_events.Create_Events_Req(p_request_id =>  AR_INVOICE_TABLE_HANDLER.g_request_id,
823                                      p_doc_table  => 'CT',
824                                      p_mode       => 'B',
825                                      p_call       => 'B');
826  ar_invoice_utils.debug ('Call creation of XLA events in bulk mode -' );
827 --}
828 
829 
830     IF pg_debug = 'Y'
831     THEN
832          ar_invoice_utils.debug ('Calling Table Handler ar_invoice_table_handler.insert_row (-) ');
833          ar_invoice_utils.debug ('ar_invoice_api_pub.create_invoice(-)' );
834     END IF;
835 
836 
837 END CREATE_INVOICE;
838 
839 PROCEDURE CREATE_SINGLE_INVOICE(
840     p_api_version           IN      	NUMBER,
841     p_init_msg_list         IN      	VARCHAR2 := FND_API.G_FALSE,
842     p_commit                IN      	VARCHAR2 := FND_API.G_FALSE,
843     p_batch_source_rec      IN      	batch_source_rec_type DEFAULT NULL,
844     p_trx_header_tbl        IN      	trx_header_tbl_type,
845     p_trx_lines_tbl         IN      	trx_line_tbl_type,
846     p_trx_dist_tbl          IN          trx_dist_tbl_type,
847     p_trx_salescredits_tbl  IN          trx_salescredits_tbl_type,
848     p_trx_contingencies_tbl IN          trx_contingencies_tbl_type,
849     x_customer_trx_id       OUT NOCOPY  NUMBER,
850     x_return_status         OUT NOCOPY  VARCHAR2,
851     x_msg_count             OUT NOCOPY  NUMBER,
852     x_msg_data              OUT NOCOPY  VARCHAR2) IS
853 
854     l_api_name       CONSTANT  VARCHAR2(30) := 'CREATE_INVOICE';
855     l_api_version    CONSTANT  NUMBER       := 1.0;
856     l_no_of_records            NUMBER := 0;
857 
858 
859 BEGIN
860     IF pg_debug = 'Y'
861     THEN
862          ar_invoice_utils.debug ('AR_INVOICE_API_PUB.CREATE_SINGLE_INVOICE(2)(+)' );
863     END IF;
864 
865     x_return_status := FND_API.G_RET_STS_SUCCESS;
866 
867     l_no_of_records := p_trx_header_tbl.COUNT;
868 
869     /* 5921925 - global variable to track single invoice call */
870     g_single_invoice := TRUE;
871 
872     IF ( nvl(l_no_of_records,0) > 1 )
873     THEN
874         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
875         x_msg_data      := arp_standard.fnd_message('AR_INAPI_MULTIPLE_HEADERS');
876         return;
877     END IF;
878 
879 
880     CREATE_INVOICE (
881         p_api_version           =>  p_api_version,
882         p_init_msg_list         =>  p_init_msg_list,
883         p_commit                =>  p_commit,
884         p_batch_source_rec      =>  p_batch_source_rec,
885         p_trx_header_tbl        =>  p_trx_header_tbl,
886         p_trx_lines_tbl         =>  p_trx_lines_tbl,
887         p_trx_dist_tbl          =>  p_trx_dist_tbl,
888         p_trx_salescredits_tbl  =>  p_trx_salescredits_tbl,
889         p_trx_contingencies_tbl =>  p_trx_contingencies_tbl,
890         x_return_status         =>  x_return_status,
891         x_msg_count             =>  x_msg_count,
892         x_msg_data              =>  x_msg_data);
893 
894 
895     IF x_return_status = FND_API.G_RET_STS_SUCCESS
896     THEN
897         -- get the value of cust_trx_id
898         x_customer_trx_id := AR_INVOICE_API_PUB.g_customer_trx_id;
899     END IF;
900 
901     IF pg_debug = 'Y'
902     THEN
903          ar_invoice_utils.debug ('AR_INVOICE_API_PUB.CREATE_SINGLE_INVOICE(-)' );
904     END IF;
905 
906 END CREATE_SINGLE_INVOICE;
907 
908 
909 -- added the overloaded procedures to make the api backward compatible
910 -- ORASHID
911 -- 11-OCT-2004
912 
913 PROCEDURE create_invoice(
914     p_api_version           IN      	NUMBER,
915     p_init_msg_list         IN      	VARCHAR2 := FND_API.G_FALSE,
916     p_commit                IN      	VARCHAR2 := FND_API.G_FALSE,
917     p_batch_source_rec      IN      	batch_source_rec_type DEFAULT NULL,
918     p_trx_header_tbl        IN      	trx_header_tbl_type,
919     p_trx_lines_tbl         IN      	trx_line_tbl_type,
920     p_trx_dist_tbl          IN          trx_dist_tbl_type,
921     p_trx_salescredits_tbl  IN          trx_salescredits_tbl_type,
922     x_return_status         OUT NOCOPY  VARCHAR2,
923     x_msg_count             OUT NOCOPY  NUMBER,
924     x_msg_data              OUT NOCOPY  VARCHAR2) IS
925 
926     l_trx_contingencies_tbl trx_contingencies_tbl_type;
927 
928 BEGIN
929 
930   IF pg_debug = 'Y'  THEN
931     ar_invoice_utils.debug ('AR_INVOICE_API_PUB.CREATE_SINGLE_INVOICE(+)' );
932   END IF;
933 
934   -- call the api with a null trx_contingencies_tbl
935 
936   create_invoice (
937     p_api_version           =>  p_api_version,
938     p_init_msg_list         =>  p_init_msg_list,
939     p_commit                =>  p_commit,
940     p_batch_source_rec      =>  p_batch_source_rec,
941     p_trx_header_tbl        =>  p_trx_header_tbl,
945     p_trx_contingencies_tbl =>  l_trx_contingencies_tbl,
942     p_trx_lines_tbl         =>  p_trx_lines_tbl,
943     p_trx_dist_tbl          =>  p_trx_dist_tbl,
944     p_trx_salescredits_tbl  =>  p_trx_salescredits_tbl,
946     x_return_status         =>  x_return_status,
947     x_msg_count             =>  x_msg_count,
948     x_msg_data              =>  x_msg_data);
949 
950   IF pg_debug = 'Y' THEN
951     ar_invoice_utils.debug ('AR_INVOICE_API_PUB.CREATE_SINGLE_INVOICE(-)' );
952   END IF;
953 
954 END create_invoice;
955 
956 
957 PROCEDURE create_single_invoice (
958     p_api_version           IN      	NUMBER,
959     p_init_msg_list         IN      	VARCHAR2 := FND_API.G_FALSE,
960     p_commit                IN      	VARCHAR2 := FND_API.G_FALSE,
961     p_batch_source_rec      IN      	batch_source_rec_type DEFAULT NULL,
962     p_trx_header_tbl        IN      	trx_header_tbl_type,
963     p_trx_lines_tbl         IN      	trx_line_tbl_type,
964     p_trx_dist_tbl          IN          trx_dist_tbl_type,
965     p_trx_salescredits_tbl  IN          trx_salescredits_tbl_type,
966     x_customer_trx_id       OUT NOCOPY  NUMBER,
967     x_return_status         OUT NOCOPY  VARCHAR2,
968     x_msg_count             OUT NOCOPY  NUMBER,
969     x_msg_data              OUT NOCOPY  VARCHAR2) IS
970 
971     l_trx_contingencies_tbl trx_contingencies_tbl_type;
972 
973 BEGIN
974 
975   IF pg_debug = 'Y'  THEN
976     ar_invoice_utils.debug ('AR_INVOICE_API_PUB.CREATE_SINGLE_INVOICE(+)' );
977   END IF;
978 
979   -- call the api with a null trx_contingencies_tbl
980 
981   create_single_invoice (
982     p_api_version           =>  p_api_version,
983     p_init_msg_list         =>  p_init_msg_list,
984     p_commit                =>  p_commit,
985     p_batch_source_rec      =>  p_batch_source_rec,
986     p_trx_header_tbl        =>  p_trx_header_tbl,
987     p_trx_lines_tbl         =>  p_trx_lines_tbl,
988     p_trx_dist_tbl          =>  p_trx_dist_tbl,
989     p_trx_salescredits_tbl  =>  p_trx_salescredits_tbl,
990     p_trx_contingencies_tbl =>  l_trx_contingencies_tbl,
991     x_customer_trx_id       =>  x_customer_trx_id,
992     x_return_status         =>  x_return_status,
993     x_msg_count             =>  x_msg_count,
994     x_msg_data              =>  x_msg_data);
995 
996   IF pg_debug = 'Y' THEN
997     ar_invoice_utils.debug ('AR_INVOICE_API_PUB.CREATE_SINGLE_INVOICE(-)' );
998   END IF;
999 
1000 END create_single_invoice;
1001 
1002 -- Bug 7194381 Start
1003 
1004 /*===========================================================================+
1005  | PROCEDURE                                                                 |
1006  |              Cache_Transaction_Type                                       |
1007  |                                                                           |
1008  | DESCRIPTION                                                               |
1009  |              Caches each transaction type when it is first used so that   |
1010  |              type values can easily be accessed later and so that the     |
1011  |              type record does not have to be fetched from the database    |
1012  |              for future transactions.                                     |
1013  |              The whole table is not cached at startup because it might be |
1014  |              very large on some sites, and because it is likely that a    |
1015  |              few types will be used many times.                           |
1016  |                                                                           |
1017  | SCOPE - PRIVATE                                                           |
1018  |                                                                           |
1019  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
1020  |                                                                           |
1021  | ARGUMENTS  : IN:                                                          |
1022  |                    p_context_rec					     |
1023  |                    p_cust_trx_type_id                                     |
1024  |              OUT:                                                         |
1025  |          IN/ OUT:                                                         |
1026  |                                                                           |
1027  | RETURNS    : NONE                                                         |
1028  |                                                                           |
1029  | NOTES                                                                     |
1030  |                                                                           |
1031  | MODIFICATION HISTORY                                                      |
1035 
1032  |    mpsingh   20-JUN-08  Created                                           |
1033  |                                                                           |
1034  +===========================================================================*/
1036 PROCEDURE Cache_Transaction_Type(
1037                                   p_context_rec       IN  Context_Rec_Type,
1038                                   p_cust_trx_type_id  IN
1039                                     ra_cust_trx_types.cust_trx_type_id%type
1040                                 ) IS
1041 
1042        l_temp        BINARY_INTEGER;
1043        l_dummy_type  ra_cust_trx_types%rowtype;
1044 
1045 BEGIN
1046 
1047         ar_invoice_utils.debug('Cache_Transaction_Type()+');
1048 
1049        IF ( p_cust_trx_type_id IS NOT NULL )
1050        THEN
1051 
1052             /*------------------------------------------------------+
1053              |  Add the current transaction type to the type cache  |
1054              |  if it does not already exist in the cache.          |
1055              +------------------------------------------------------*/
1056 
1057              IF ( NOT Type_Cache_Tbl.EXISTS( p_cust_trx_type_id ) )
1058              THEN
1059 
1060                   BEGIN
1061                           SELECT *
1062                           INTO   Type_Cache_Tbl( p_cust_trx_type_id )
1063                           FROM   ra_cust_trx_types
1064                           WHERE  cust_trx_type_id =  p_cust_trx_type_id;
1065 
1066                            ar_invoice_utils.debug('Transaction Type: ' ||
1067                                 Type_Cache_Tbl( p_cust_trx_type_id ).name ||
1068                                 ' found.');
1069 
1070                   EXCEPTION
1071                     WHEN NO_DATA_FOUND
1072                          THEN
1073                             /*---------------------------------------------+
1074                              |  If the type does not exist, assign a       |
1075                              |  null type record to its place in the cache |
1076                              |  to avoid NO_DATA_FOUND errors later on.    |
1077                              |                                             |
1078                              |  The invalid data will be caught later in   |
1079                              |  the validation routines.                   |
1080                              +---------------------------------------------*/
1081 
1082                              Type_Cache_Tbl( p_cust_trx_type_id ) :=
1083                                    l_dummy_type;
1084                               ar_invoice_utils.debug('Transaction Type not found');
1085 
1086                     WHEN OTHERS THEN
1087                          RAISE;
1088                   END;
1089 
1090              END IF;
1091 
1092        END IF;
1093 
1094         ar_invoice_utils.debug('Cache_Transaction_type()-');
1095 
1096 EXCEPTION
1097     WHEN NO_DATA_FOUND THEN NULL;
1098 
1099     WHEN OTHERS THEN
1100       ar_invoice_utils.debug('EXCEPTION: Cache_Transaction_Type() ');
1101       ar_invoice_utils.debug('p_cust_trx_type_id  =  ' ||
1102                     TO_CHAR(p_cust_trx_type_id));
1103      RAISE;
1104 
1105 END Cache_Transaction_Type;
1106 
1107 /*===========================================================================+
1108  | PROCEDURE     Get_Flags                                                   |
1109  |                                                                           |
1110  | DESCRIPTION                                                               |
1111  |   Gets information about the transaction that is used in determining if   |
1112  |   it can be updated.                                                      |
1113  |                                                                           |
1114  | SCOPE - PRIVATE                                                           |
1115  |                                                                           |
1116  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
1117  |                                                                           |
1118  | ARGUMENTS  : IN:                                                          |
1119  |                   p_context_rec                                           |
1120  |              OUT:                                                         |
1121  |                   p_dm_reversal 					     |
1122  |                   p_cb 						     |
1123  |          IN/ OUT:                                                         |
1124  |                   p_trx_rec 						     |
1125  |                   p_type_rec 					     |
1126  |                   p_posted_flag 					     |
1127  |                   p_activity_flag 					     |
1128  |                   p_printed_flag 					     |
1129  |                   p_rev_recog_run_flag 				     |
1130  |                                                                           |
1131  | RETURNS    : None			                                     |
1132  |                                                                           |
1133  | NOTES                                                                     |
1134  |                                                                           |
1135  | MODIFICATION HISTORY                                                      |
1136  |  mpsingh   20-jun-2008 Created
1137  |                                                                           |
1138  +===========================================================================*/
1139 
1140 PROCEDURE Get_Flags(
1141                              p_context_rec   IN  Context_Rec_Type,
1142                              p_trx_rec       IN OUT NOCOPY  ra_customer_trx%rowtype,
1143                              p_type_rec      IN OUT NOCOPY  ra_cust_trx_types%rowtype,
1144                              p_posted_flag   IN OUT NOCOPY  VARCHAR2,
1145                              p_activity_flag IN OUT NOCOPY  VARCHAR2,
1149                              p_cb               OUT NOCOPY VARCHAR2
1146                              p_printed_flag  IN OUT NOCOPY  VARCHAR2,
1147                              p_rev_recog_run_flag  IN OUT NOCOPY VARCHAR2,
1148                              p_dm_reversal      OUT NOCOPY VARCHAR2,
1150                               ) IS
1151 
1152         l_index  BINARY_INTEGER;
1153 
1154 BEGIN
1155 
1156         ar_invoice_utils.debug('Get_Flags()+');
1157 
1158         p_posted_flag         := 'N';
1159         p_activity_flag       := 'N';
1160         p_dm_reversal         := 'N';
1161         p_cb                  := 'N';
1162         p_printed_flag        := 'N';
1163         p_rev_recog_run_flag  := 'N';
1164 
1165         p_posted_flag := arpt_sql_func_util.get_posted_flag(
1166                                        p_trx_rec.customer_trx_id,
1167                                        p_type_rec.post_to_gl,
1168                                        p_trx_rec.complete_flag );
1169 
1170         p_activity_flag := arpt_sql_func_util.get_activity_flag(
1171                                  p_trx_rec.customer_trx_id,
1172                                  p_type_rec.accounting_affect_flag,
1173                                  p_trx_rec.complete_flag,
1174                                  p_type_rec.type,
1175                                  p_trx_rec.initial_customer_trx_id,
1176                                  p_trx_rec.previous_customer_trx_id
1177                                  );
1178 
1179         IF ( NVL(p_trx_rec.printing_count, 0) > 0 )
1180         THEN p_printed_flag := 'Y';
1181         END IF;
1182 
1183        /*--------------------------------------------------------------------+
1184         | Determine if Revenue Recognition has been run for any of the lines |
1185 	+--------------------------------------------------------------------*/
1186 
1187         FOR l_index IN 1..G_lines_tbl.count LOOP
1188 
1189             IF ( G_lines_tbl(l_index).autorule_duration_processed > 0 )
1190             THEN p_rev_recog_run_flag := 'Y';
1191             END IF;
1192 
1193         END LOOP;
1194 
1195         ar_invoice_utils.debug('.  posted_flag           = ' ||
1196                                  p_posted_flag);
1197 
1198         ar_invoice_utils.debug('.  activity_flag         = ' ||
1199                                  p_activity_flag);
1200 
1201         ar_invoice_utils.debug('.  printed_flag          = ' ||
1202                                  p_printed_flag);
1203 
1204         ar_invoice_utils.debug('.  p_rev_recog_run_flag  = ' ||
1205                                  p_rev_recog_run_flag);
1206 
1207         ar_invoice_utils.debug('.  Class                 = ' ||
1208                                  p_type_rec.type);
1209 
1210         ar_invoice_utils.debug('.  created_from          = ' ||
1211                                  p_trx_rec.created_from);
1212 
1213         IF ( p_trx_rec.created_from  IN ('ARXREV', 'REL9_ARXREV') )
1214         THEN
1215              p_dm_reversal := 'Y';
1216         END IF;
1217 
1218         IF ( p_type_rec.type = 'CB' )
1219         THEN
1220              p_cb := 'Y';
1221         END IF;
1222 
1223         ar_invoice_utils.debug('Get_Flags()-');
1224 
1225 EXCEPTION
1226    WHEN OTHERS THEN
1227 
1228         ar_invoice_utils.debug('EXCEPTION: Get_Flags()');
1229         RAISE;
1230 
1231 END Get_Flags;
1232 
1233 /*===========================================================================+
1234  | PROCEDURE     Validate_Delete_Transaction                                 |
1235  |                                                                           |
1236  | DESCRIPTION                                                               |
1237  |		 Validates that the information provided for a transaction   |
1238  |		 deletion does not violate any of the validation rules.      |
1239  |                                                                           |
1240  | SCOPE - PRIVATE                                                           |
1241  |                                                                           |
1242  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
1243  |                                                                           |
1244  | ARGUMENTS  : IN:                                                          |
1245  |                   p_customer_trx_id                                       |
1246  |                   p_trx_rec                                               |
1247  |                   p_type_rec                                              |
1248  |              OUT:                                                         |
1249  |		     p_return_status					     |
1250  |          IN/ OUT:                                                         |
1251  |                                                                           |
1252  | RETURNS    : NONE                                                         |
1253  |                                                                           |
1254  | NOTES                                                                     |
1255  |                                                                           |
1256  | MODIFICATION HISTORY                                                      |
1257  |    mpsingh   20-Jun-08  Created                                   |
1258  |                                                                           |
1259  +===========================================================================*/
1260 
1261 PROCEDURE Validate_Delete_Transaction(
1262                p_customer_trx_id  IN      ra_customer_trx.customer_trx_id%type,
1263                p_trx_rec          IN OUT NOCOPY  ra_customer_trx%rowtype,
1264                p_type_rec         IN OUT NOCOPY  ra_cust_trx_types%rowtype,
1265                p_return_status    OUT NOCOPY     VARCHAR2
1266                                      ) IS
1267 
1271   l_activity_flag        VARCHAR2(1);
1268   l_context_rec          Context_Rec_Type;
1269   l_dummy                BINARY_INTEGER;
1270   l_posted_flag          VARCHAR2(1);
1272   l_printed_flag         VARCHAR2(1);
1273   l_rev_recog_run_flag   VARCHAR2(1);
1274   l_dm_reversal_flag     VARCHAR2(1);
1275   l_cb_flag              VARCHAR2(1);
1276 
1277 BEGIN
1278 
1279         ar_invoice_utils.debug('Validate_Delete_Transaction()+');
1280 
1281         p_return_status := FND_API.G_RET_STS_SUCCESS;
1282 
1283        /*------------------------------------------------------------+
1284         |  Get the header record for the transaction to be deleted.  |
1285 	+------------------------------------------------------------*/
1286 
1287         BEGIN
1288                 arp_ct_pkg.fetch_p(
1289                                       p_trx_rec,
1290                                       p_customer_trx_id
1291                                    );
1292 
1293         EXCEPTION
1294           WHEN NO_DATA_FOUND THEN
1295               p_return_status := FND_API.G_RET_STS_ERROR;
1296 
1297               arp_trx_validate.Add_To_Error_List(
1298                           p_mode              => 'PL/SQL',
1299                           P_error_count       => l_dummy,
1300                           p_customer_trx_id   => p_customer_trx_id,
1301                           p_trx_number        => null,
1302                           p_line_number       => null,
1303                           p_other_line_number => null,
1304                           p_message_name      => 'AR_TAPI_TRANS_NOT_EXIST',
1305                           p_token_name_1      => 'CUSTOMER_TRX_ID',
1306                           p_token_1           => p_customer_trx_id );
1307 
1308                RAISE;
1309 
1310           WHEN OTHERS THEN RAISE;
1311         END;
1312 
1313        /*--------------------------------------------------------------------+
1314         | The invoice_deletion_flag must be Y for transactions to be deleted |
1315 	+--------------------------------------------------------------------*/
1316 
1317         ar_invoice_utils.debug('.  invoice_deletion_flag = ' ||
1318                                  arp_global.sysparam.invoice_deletion_flag);
1319 
1320         IF ( arp_global.sysparam.invoice_deletion_flag <> 'Y' )
1321         THEN
1322 
1323               p_return_status := FND_API.G_RET_STS_ERROR;
1324 
1325               arp_trx_validate.Add_To_Error_List(
1326                           p_mode              => 'PL/SQL',
1327                           P_error_count       => l_dummy,
1328                           p_customer_trx_id   => p_trx_rec.customer_trx_id,
1329                           p_trx_number        => p_trx_rec.trx_number,
1330                           p_line_number       => null,
1331                           p_other_line_number => null,
1332                           p_message_name      => 'AR_CANT_DELETE_IF_COMPLETE');
1333 
1334         ELSE
1335 
1336 				Cache_Transaction_Type(
1337                                                    l_context_rec,
1338                                                    p_trx_rec.cust_trx_type_id
1339                                                        );
1340 
1341               p_type_rec := Type_Cache_Tbl(
1342                               p_trx_rec.cust_trx_type_id
1343                                           );
1344 
1345              /*--------------------------------------------------------------+
1346               |  If the transaction is complete, it must be made incomplete  |
1347               |  before it can be deleted.                                   |
1348               |                                                              |
1349               |  A transaction can only be made incomplete if:               |
1350               |    o It is not a chargeback           and                    |
1351               |    o It is not a debit memo reversal  and                    |
1352               |    o It has not been posted to GL     and                    |
1353               |    o There is no activity against it.                        |
1354               +--------------------------------------------------------------*/
1355 
1356               ar_invoice_utils.debug('.  complete_flag         = ' ||
1357                                        p_trx_rec.complete_flag);
1358 
1359               IF ( p_trx_rec.complete_flag = 'Y' )
1360               THEN
1361 
1362                     Get_Flags(
1363                                l_context_rec,
1364                                p_trx_rec,
1365                                p_type_rec,
1366                                l_posted_flag,
1367                                l_activity_flag,
1368                                l_printed_flag,
1369                                l_rev_recog_run_flag,
1370                                l_dm_reversal_flag,
1371                                l_cb_flag
1372                              );
1373 
1374                     IF   ( l_dm_reversal_flag = 'Y' )
1375                     THEN
1376                          p_return_status := FND_API.G_RET_STS_ERROR;
1377 
1378                          arp_trx_validate.Add_To_Error_List(
1379                               p_mode              => 'PL/SQL',
1380                               p_error_count       => l_dummy,
1381                               p_customer_trx_id   => p_trx_rec.customer_trx_id,
1382                               p_trx_number        => p_trx_rec.trx_number,
1383                               p_line_number       => null,
1384                               p_other_line_number => null,
1385                               p_message_name      =>
1386                                                  'AR_TAPI_CANT_DELETE_DM_REV');
1387 
1388                     END IF;
1389 
1390                     IF ( l_cb_flag = 'Y' )
1391                     THEN
1395                               p_mode              => 'PL/SQL',
1392                          p_return_status := FND_API.G_RET_STS_ERROR;
1393 
1394                          arp_trx_validate.Add_To_Error_List(
1396                               p_error_count       => l_dummy,
1397                               p_customer_trx_id   => p_trx_rec.customer_trx_id,
1398                               p_trx_number        => p_trx_rec.trx_number,
1399                               p_line_number       => null,
1400                               p_other_line_number => null,
1401                               p_message_name      => 'AR_TAPI_CANT_DELETE_CB');
1402 
1403                     END IF;
1404 
1405                     IF ( l_posted_flag  = 'Y' )
1406                     THEN
1407                          p_return_status := FND_API.G_RET_STS_ERROR;
1408 
1409                          arp_trx_validate.Add_To_Error_List(
1410                               p_mode              => 'PL/SQL',
1411                               p_error_count       => l_dummy,
1412                               p_customer_trx_id   => p_trx_rec.customer_trx_id,
1413                               p_trx_number        => p_trx_rec.trx_number,
1414                               p_line_number       => null,
1415                               p_other_line_number => null,
1416                               p_message_name      =>
1417                                                  'AR_ALL_CANT_DELETE_IF_POSTED');
1418 
1419                     END IF;
1420 
1421                     IF ( l_activity_flag = 'Y' )
1422                     THEN
1423                          p_return_status := FND_API.G_RET_STS_ERROR;
1424 
1425                          arp_trx_validate.Add_To_Error_List(
1426                               p_mode              => 'PL/SQL',
1427                               p_error_count       => l_dummy,
1428                               p_customer_trx_id   => p_trx_rec.customer_trx_id,
1429                               p_trx_number        => p_trx_rec.trx_number,
1430                               p_line_number       => null,
1431                               p_other_line_number => null,
1432                               p_message_name      =>
1433                                                'AR_TAPI_CANT_DELETE_ACTIVITY');
1434 
1435                     END IF;
1436 
1437 
1438               END IF;  -- complete case
1439 
1440        END IF;
1441 
1442        ar_invoice_utils.debug('Validate_Delete_Transaction()-');
1443 
1444 EXCEPTION
1445    WHEN OTHERS THEN
1446         ar_invoice_utils.debug('EXCEPTION:  Validate_Delete_Transaction()');
1447         ar_invoice_utils.debug('p_customer_trx_id = ' ||
1448                                  p_customer_trx_id);
1449 
1450         RAISE;
1451 
1452 END Validate_Delete_Transaction;
1453 /*===========================================================================+
1454  | PROCEDURE                                                                 |
1455  |              Delete_Trxn_Extn_Details                                     |
1456  |                                                                           |
1457  | DESCRIPTION                                                               |
1458  |              This is the main routine that deletes the                    |
1459  |              PAYMENT_TRXN_EXTENSION details of transactions.              |
1460  |                                                                           |
1461  | SCOPE - PUBLIC                                                            |
1462  |                                                                           |
1463  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
1464  |     IBY_FNDCPT_TRXN_PUB.delete_transaction_extension                      |
1465  |                                                                           |
1466  | ARGUMENTS  : IN:                                                          |
1467  |                   p_trx_rec                                               |
1468  |              OUT:                                                         |
1469  |                   p_return_status                                         |
1470  |                                                                           |
1471  | RETURNS    : NONE                                                         |
1472  |                                                                           |
1473  | NOTES                                                                     |
1474  |                                                                           |
1475  | MODIFICATION HISTORY                                                      |
1476  |    mpsingh   20-jun-2008  Created                                   |
1477  |                                                                           |
1478  +===========================================================================*/
1479 
1480 PROCEDURE Delete_Trxn_Extn_Details(p_trx_rec IN ra_customer_trx%rowtype,
1481                                    p_return_status    OUT NOCOPY     VARCHAR2
1482 ) IS
1483 
1484     l_payer_rec            IBY_FNDCPT_COMMON_PUB.payercontext_rec_type;
1485     l_trxn_attribs_rec     IBY_FNDCPT_TRXN_PUB.trxnextension_rec_type;
1486     l_response             IBY_FNDCPT_COMMON_PUB.result_rec_type;
1487     l_payment_channel      ar_receipt_methods.payment_channel_code%type;
1488     x_msg_count		   NUMBER;
1489     x_msg_data		   VARCHAR2(240);
1490     x_return_status	   VARCHAR2(240);
1491     l_payment_trxn_extn_id ra_customer_trx.PAYMENT_TRXN_EXTENSION_ID%TYPE;
1492     l_fnd_api_constants_rec     ar_bills_main.fnd_api_constants_type     := ar_bills_main.get_fnd_api_constants_rec;
1493     Begin
1494         ar_invoice_utils.debug('AR_INVOICE_API_PUB.Delete_Trxn_Extn_Details()+ ');
1495 
1496 	p_return_status := FND_API.G_RET_STS_SUCCESS;
1497 
1498 
1499         x_msg_count          := NULL;
1500         x_msg_data           := NULL;
1501         x_return_status      := l_fnd_api_constants_rec.G_RET_STS_SUCCESS;
1505         l_payer_rec.cust_account_id                   := p_trx_rec.BILL_TO_CUSTOMER_ID;
1502         l_payer_rec.party_id :=  arp_trx_defaults_3.get_party_Id(p_trx_rec.BILL_TO_CUSTOMER_ID);
1503         l_payer_rec.payment_function                  := 'CUSTOMER_PAYMENT';
1504         l_payer_rec.org_type                          := 'OPERATING_UNIT';
1506         l_payer_rec.org_id                            := p_trx_rec.ORG_ID;
1507         l_payer_rec.account_site_id                   := p_trx_rec.BILL_TO_SITE_USE_ID;
1508         l_payment_trxn_extn_id 			      := p_trx_rec.PAYMENT_TRXN_EXTENSION_ID;
1509 
1510            /*-------------------------+
1511             |   Call the IBY API      |
1512             +-------------------------*/
1513             ar_invoice_utils.debug('Call TO IBY API ()+ ');
1514 
1515 
1516             IBY_FNDCPT_TRXN_PUB.delete_transaction_extension(
1517                p_api_version           => 1.0,
1518                p_init_msg_list         => AR_BILLS_MAIN.GTRUE,
1519                p_commit                => AR_BILLS_MAIN.GFALSE,
1520                x_return_status         => x_return_status,
1521                x_msg_count             => x_msg_count,
1522                x_msg_data              => x_msg_data,
1523                p_payer                 => l_payer_rec,
1524                p_payer_equivalency     => 'UPWARD',
1525                p_entity_id             => l_payment_trxn_extn_id,
1526                x_response              => l_response);
1527 
1528 
1529 
1530     IF x_return_status = FND_API.G_RET_STS_SUCCESS
1531     THEN
1532        ar_invoice_utils.debug('Payment_Trxn_Extension_Id : ' || l_payment_trxn_extn_id);
1533     Else
1534       ar_invoice_utils.debug('Errors Reported by IBY API:Delete Transaction Extension ');
1535        p_return_status := FND_API.G_RET_STS_ERROR;
1536     END IF;
1537 EXCEPTION
1538      WHEN OTHERS THEN
1539        ar_invoice_utils.debug('exception in AR_INVOICE_API_PUB.Delete_Trxn_Extn_Detail ');
1540        p_return_status := FND_API.G_RET_STS_ERROR;
1541 END Delete_Trxn_Extn_Details;
1542 
1543 
1544 /*===========================================================================+
1545  | PROCEDURE                                                                 |
1546  |              Delete_Transaction                                           |
1547  |                                                                           |
1548  | DESCRIPTION                                                               |
1549  |              This is the main routine that deletes the transactions.      |
1550  |              It is called once for each transaction to be deleted.        |
1551  |                                                                           |
1552  | SCOPE - PUBLIC                                                            |
1553  |                                                                           |
1554  | EXETERNAL PROCEDURES/FUNCTIONS ACCESSED                                   |
1555  |     ar_transaction_val2_pub.Validate_Delete_Transaction                   |
1556  |     arp_process_header.update_header                                      |
1557  |     arp_process_header_post_commit.post_commit                            |
1558  |     arp_trx_validate.Add_To_Error_List                                    |
1559  |     arp_process_header.delete_header                                      |
1560  |     arp_util.disable_debug                                                |
1561  |     arp_util.enable_debug                                                 |
1562  |     fnd_api.compatible_api_call                                           |
1563  |     fnd_api.g_exc_unexpected_error                                        |
1564  |     fnd_api.g_ret_sts_error                                               |
1565  |     fnd_api.g_ret_sts_error                                               |
1566  |     fnd_api.g_ret_sts_success                                             |
1567  |     fnd_api.to_boolean                                                    |
1568  |     fnd_msg_pub.check_msg_level                                           |
1569  |     fnd_msg_pub.count_and_get                                             |
1570  |     fnd_msg_pub.initialize                                                |
1571  |                                                                           |
1572  | ARGUMENTS  : IN:                                                          |
1573  |                   p_api_name                                              |
1574  |                   p_api_version                                           |
1575  |                   p_init_msg_list                                         |
1576  |                   p_commit                                                |
1577  |                   p_validation_level                                      |
1578  |                   p_customer_trx_id                                       |
1579  |              OUT:                                                         |
1580  |                   p_return_status                                         |
1581  |          IN/ OUT:                                                         |
1582  |                   p_msg_count                                             |
1583  |                   p_msg_data                                              |
1584  |                   p_errors                                                |
1585  |                                                                           |
1586  | RETURNS    : NONE                                                         |
1587  |                                                                           |
1588  | NOTES                                                                     |
1589  |                                                                           |
1590  | MODIFICATION HISTORY                                                      |
1591  |    mpsingh   20-jun-2008  Created                                   |
1592  |                                                                           |
1593  +===========================================================================*/
1594 
1598      p_init_msg_list             IN  varchar2 := FND_API.G_FALSE,
1595 PROCEDURE Delete_Transaction(
1596      p_api_name                  IN  varchar2,
1597      p_api_version               IN  number,
1599      p_commit                    IN  varchar2 := FND_API.G_FALSE,
1600      p_validation_level          IN  varchar2 := FND_API.G_VALID_LEVEL_FULL,
1601      p_customer_trx_id           IN  ra_customer_trx.customer_trx_id%type,
1602      p_return_status            OUT NOCOPY  varchar2,
1603      p_msg_count             IN OUT NOCOPY  NUMBER,
1604      p_msg_data              IN OUT NOCOPY  varchar2,
1605      p_errors                IN OUT NOCOPY  arp_trx_validate.Message_Tbl_Type
1606                   ) IS
1607 
1608   l_api_name             CONSTANT VARCHAR2(20) := 'Delete_Transaction';
1609   l_api_version          CONSTANT NUMBER       := 1.0;
1610   l_message              VARCHAR2(1000);
1611   l_return_status        VARCHAR2(10);
1612   l_trx_rec              ra_customer_trx%rowtype;
1613   l_type_rec             ra_cust_trx_types%rowtype;
1614   l_commitment_rec       arp_process_commitment.commitment_rec_type;
1615   l_gl_date              ra_cust_trx_line_gl_dist.gl_date%type;
1616   l_amount               ra_cust_trx_line_gl_dist.amount%type;
1617   l_dummy                BINARY_INTEGER;
1618   l_update_status        VARCHAR2(50)  := 'OK';
1619   l_delete_status        VARCHAR2(50)  := 'OK';
1620   l_validation_status    VARCHAR2(10);
1621   l_delete_pmt_ext_status VARCHAR2(10);
1622 
1623   PROCEDURE Display_Parameters IS
1624   BEGIN
1625         ar_invoice_utils.debug('p_api_name              = ' || p_api_name);
1626         ar_invoice_utils.debug('p_api_version           = ' || p_api_version);
1627         ar_invoice_utils.debug('p_init_msg_list         = ' || p_init_msg_list);
1628         ar_invoice_utils.debug('p_commit                = ' || p_commit);
1629         ar_invoice_utils.debug('p_validation_level      = ' || p_validation_level);
1630         ar_invoice_utils.debug('p_customer_trx_id       = ' || p_customer_trx_id);
1631   END;
1632 
1633 BEGIN
1634 
1635 
1636        /*------------------------------------+
1637         |   Standard start of API savepoint  |
1638         +------------------------------------*/
1639 
1640         SAVEPOINT Delete_Transaction_Pub;
1641 
1642        /*--------------------------------------------------+
1643         |   Standard call to check for call compatibility  |
1644         +--------------------------------------------------*/
1645 
1646         IF NOT FND_API.Compatible_API_Call(
1647                                             l_api_version,
1648                                             p_api_version,
1649                                             l_api_name,
1650                                             G_PKG_NAME
1651                                           )
1652         THEN
1653               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1654         END IF;
1655 
1656        /*--------------------------------------------------------------+
1657         |   Initialize message list if p_init_msg_list is set to TRUE  |
1658         +--------------------------------------------------------------*/
1659 
1660         IF FND_API.to_Boolean( p_init_msg_list )
1661         THEN
1662               FND_MSG_PUB.initialize;
1663               arp_trx_validate.pg_message_tbl.DELETE;
1664         END IF;
1665 
1666         /*-----------------------------------------+
1667         |   Initialize return status to SUCCESS   |
1668         +-----------------------------------------*/
1669 
1670         l_return_status := FND_API.G_RET_STS_SUCCESS;
1671 
1672        /*---------------------------------------------+
1673         |   ========== Start of API Body ==========   |
1674         +---------------------------------------------*/
1675 
1676        	 ar_invoice_utils.debug('AR_INVOICE_API_PUB.Delete_Transaction(+)' );
1677 
1678 
1679                                Validate_Delete_Transaction(
1680                                                             p_customer_trx_id,
1681                                                             l_trx_rec,
1682                                                             l_type_rec,
1683                                                             l_validation_status
1684                                                            );
1685 
1686         IF ( l_validation_status = FND_API.G_RET_STS_SUCCESS )
1687         THEN
1688               IF ( l_trx_rec.complete_flag = 'Y' )
1689               THEN
1690 
1691 		IF pg_debug = 'Y'
1692 		THEN
1693 		 ar_invoice_utils.debug('Making the transaction incomplete ' );
1694 		END IF;
1695 
1696                         /*-------------------------------------------+
1697                          |  Try to make the transaction incomplete.  |
1698                          +-------------------------------------------*/
1699 
1700                          SELECT trunc(gl_date),
1701                                 amount
1702                          INTO   l_gl_date,
1703                                 l_amount
1704                          FROM   ra_cust_trx_line_gl_dist
1705                          WHERE  customer_trx_id = p_customer_trx_id
1706                          AND    account_class   = 'REC'
1707                          AND    latest_rec_flag = 'Y';
1708 
1709                          	IF pg_debug = 'Y'
1710 				THEN
1711 				 ar_invoice_utils.debug ('.  gl_date	= ' ||  TO_CHAR(l_gl_date, 'DD-MON-YYYY'));
1712 				END IF;
1713 
1714                         	IF pg_debug = 'Y'
1715 				THEN
1716 				 ar_invoice_utils.debug ('.  amount                = ' ||TO_CHAR(l_amount));
1717 				END IF;
1718 
1719                          l_trx_rec.complete_flag := 'N';
1720 
1721                          arp_process_header.update_header(
1722                                p_api_name,
1723                                p_api_version,
1724                                l_trx_rec,
1725                                p_customer_trx_id,
1726                                l_amount,
1727                                l_type_rec.type,
1728                                l_gl_date,
1729                                l_trx_rec.initial_customer_trx_id,
1730                                l_commitment_rec,
1731                                l_type_rec.accounting_affect_flag,
1732                                'Y',
1733                                FALSE,
1734                                FALSE,
1735                                NULL,
1736                                NULL,
1737                                l_update_status);
1738 
1739 
1740                         /*-----------------------+
1741                          |  Do postcommit logic  |
1742                          +-----------------------*/
1743 
1744 			 IF pg_debug = 'Y'
1745 			 THEN
1746 			  ar_invoice_utils.debug('post_commit()+ ');
1747 			 END IF;
1748 
1749                          arp_process_header_post_commit.post_commit(
1750                                       'ARTPTRXB',
1751                                       p_api_version,
1752                                       p_customer_trx_id,
1753                                       l_trx_rec.previous_customer_trx_id,
1754                                       'N',
1755                                       l_type_rec.accounting_affect_flag,
1756                                       NULL,
1757                                       l_type_rec.creation_sign,
1758                                       l_type_rec.allow_overapplication_flag,
1759                                       l_type_rec.natural_application_only_flag,
1760                                       NULL,
1761                                       'PL/SQL'
1762                                       );
1763 
1764 
1765                          IF pg_debug = 'Y'
1766 			 THEN
1767 			  ar_invoice_utils.debug('post_commit()- ');
1768 			 END IF;
1769 
1770               END IF;   -- end complete_flag = 'Y' case
1771 
1772              /*-----------------------------------------------------+
1773               |  Delete the transaction if no errors have occurred  |
1774               |  in the previous steps.                             |
1775               +-----------------------------------------------------*/
1776 
1777               IF pg_debug = 'Y'
1778 	      THEN
1779 		ar_invoice_utils.debug ('.  update_status         = ' || l_update_status);
1780 	      END IF;
1781 
1782               IF   (
1783                         l_return_status = FND_API.G_RET_STS_SUCCESS
1784                     AND l_update_status = 'OK'
1785                    )
1786               THEN
1787 			IF pg_debug = 'Y'
1788 			THEN
1789 				ar_invoice_utils.debug('Deleting the transaction');
1790 			END IF;
1791 
1792 
1793                         IF l_trx_rec.payment_trxn_extension_id IS NOT NULL THEN
1794 				Delete_Trxn_Extn_Details(l_trx_rec,
1795 						       l_delete_pmt_ext_status);
1796                         ELSE
1797 			   l_delete_pmt_ext_status := FND_API.G_RET_STS_SUCCESS;
1798 			END IF;
1799 
1800 			IF  l_delete_pmt_ext_status = FND_API.G_RET_STS_SUCCESS THEN
1801 
1802 			      arp_process_header.delete_header(
1803 								p_api_name,
1804 								p_api_version,
1805 								p_customer_trx_id,
1806 								l_type_rec.type,
1807 								l_delete_status);
1808                         END IF;
1809 
1810               END IF;
1811 
1812         END IF;  -- validation was successfull case
1813 
1814        /*-------------------------------------------------------------------+
1815         |  Get any messages that have been put on the regular message stack |
1816         |  and add them to the error list.                                  |
1817         +-------------------------------------------------------------------*/
1818 
1819         l_message := fnd_message.get;
1820 
1821         WHILE l_message IS NOT NULL LOOP
1822 
1823               arp_trx_validate.Add_To_Error_List(
1824                               p_mode              => 'PL/SQL',
1825                               P_error_count       => l_dummy,
1826                               p_customer_trx_id   => null,
1827                               p_trx_number        => null,
1828                               p_line_number       => null,
1829                               p_other_line_number => null,
1830                               p_message_name      => 'GENERIC_MESSAGE',
1831                               p_token_name_1      => 'GENERIC_TEXT',
1832                               p_token_1           => l_message );
1833 
1834               l_message := fnd_message.get;
1835 
1836         END LOOP;
1837 
1838 
1839        /*-------------------------------------------+
1840         |   ========== End of API Body ==========   |
1841         +-------------------------------------------*/
1842 
1843         p_return_status := l_return_status;
1844 
1845        /*---------------------------------------------------+
1846         |  Get message count and if 1, return message data  |
1847         +---------------------------------------------------*/
1848 
1849         FND_MSG_PUB.Count_And_Get(
1850                                    p_count => p_msg_count,
1851                                    p_data  => p_msg_data
1852                                  );
1853 
1854 
1855        /*------------------------------------------------------------+
1856         |  If any errors - including validation failures - occurred, |
1857         |  rollback any changes and return an error status.          |
1858         +------------------------------------------------------------*/
1859 
1860         IF   (
1861                   NVL( arp_trx_validate.pg_message_tbl.COUNT, 0)  > 0
1862                OR l_update_status <> 'OK'
1863                OR l_delete_status <> 'OK'
1864 	       OR l_delete_pmt_ext_status <> FND_API.G_RET_STS_SUCCESS
1865                OR l_return_status <> FND_API.G_RET_STS_SUCCESS
1866              )
1867         THEN
1868 
1869              p_errors := arp_trx_validate.pg_message_tbl;
1870 
1871              ROLLBACK TO Delete_Transaction_PUB;
1872 
1873              p_return_status := FND_API.G_RET_STS_ERROR ;
1874 
1875              	   IF pg_debug = 'Y'
1876 		   THEN
1877 		     ar_invoice_utils.debug ('Error(s) occurred. Rolling back and setting status to ERROR');
1878   		     ar_invoice_utils.debug ('Number Of Messages:  ' ||  TO_CHAR( arp_trx_validate.pg_message_tbl.COUNT) );
1879 		   END IF;
1880 
1881 
1882 
1883         END IF;
1884 
1885        /*--------------------------------+
1886         |   Standard check of p_commit   |
1887         +--------------------------------*/
1888 
1889         IF FND_API.To_Boolean( p_commit )
1890         THEN  ar_invoice_utils.debug('committing');
1891               Commit;
1892         END IF;
1893 
1894         	 ar_invoice_utils.debug('AR_INVOICE_API_PUB.Delete_Transaction(-)' );
1895 
1896 
1897 EXCEPTION
1898        WHEN NO_DATA_FOUND THEN
1899 
1900                 ROLLBACK TO Delete_Transaction_PUB;
1901                 p_return_status := FND_API.G_RET_STS_ERROR;
1902                 Display_Parameters;
1903 
1904                 FND_MSG_PUB.Count_And_Get( p_count       =>      p_msg_count,
1905                                            p_data        =>      p_msg_data
1906                                          );
1907 
1908        WHEN FND_API.G_EXC_ERROR THEN
1909 		 ar_invoice_utils.debug(SQLCODE);
1910 		 ar_invoice_utils.debug(SQLERRM);
1911 
1912 
1913                 ROLLBACK TO Delete_Transaction_PUB;
1914                 p_return_status := FND_API.G_RET_STS_ERROR ;
1915                 Display_Parameters;
1916 
1917                 FND_MSG_PUB.Count_And_Get( p_count       =>      p_msg_count,
1918                                            p_data        =>      p_msg_data
1919                                          );
1920 
1921         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1922 
1923 		 ar_invoice_utils.debug(SQLCODE);
1924 		 ar_invoice_utils.debug(SQLERRM);
1925                 ROLLBACK TO Delete_Transaction_PUB;
1926                 p_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1927                 Display_Parameters;
1928 
1929                 FND_MSG_PUB.Count_And_Get( p_count       =>      p_msg_count,
1930                                            p_data        =>      p_msg_data
1931                                          );
1932 
1933         WHEN OTHERS THEN
1934 		 ar_invoice_utils.debug(SQLCODE);
1935 		 ar_invoice_utils.debug(SQLERRM);
1936                /*-------------------------------------------------------+
1937                 |  Handle application errors that result from trapable  |
1938                 |  error conditions. The error messages have already    |
1939                 |  been put on the error stack.                         |
1940                 +-------------------------------------------------------*/
1941 
1942                 IF (SQLCODE = -20001)
1943                 THEN
1944                       p_errors := arp_trx_validate.pg_message_tbl;
1945 
1946                       ROLLBACK TO Delete_Transaction_PUB;
1947 
1948                       p_return_status := FND_API.G_RET_STS_ERROR ;
1949 
1950 			 ar_invoice_utils.debug('Completion validation error(s) occurred. ' ||
1951                             'Rolling back and setting status to ERROR');
1952 
1953                       RETURN;
1954 
1955                 ELSE NULL;
1956                 END IF;
1957 
1958                 ROLLBACK TO Delete_Transaction_PUB;
1959 
1960                 p_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1961                 Display_Parameters;
1962 
1963                 IF      FND_MSG_PUB.Check_Msg_Level
1964                 THEN
1965                 FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,
1966                                         l_api_name
1967                                        );
1968                 END IF;
1969 
1970                 FND_MSG_PUB.Count_And_Get( p_count       =>      p_msg_count,
1971                                            p_data        =>      p_msg_data
1972                                          );
1973 
1974 
1975 END Delete_Transaction;
1976 
1977 
1978 -- Bug 7194381 End
1979 
1980 BEGIN
1981    g_one_time_init_org := -98;
1982 END AR_INVOICE_API_PUB;