DBA Data[Home] [Help]

PACKAGE BODY: APPS.IBY_DISBURSE_UI_API_PUB_PKG

Source


1 PACKAGE BODY IBY_DISBURSE_UI_API_PUB_PKG AS
2 /*$Header: ibydapib.pls 120.225.12020000.5 2012/10/04 12:01:53 asarada ship $*/
3 
4  /*
5   * Declare global variables
6   */
7  G_PKG_NAME CONSTANT VARCHAR2(30) := 'IBY_DISBURSE_UI_API_PUB_PKG';
8  G_CURRENT_RUNTIME_LEVEL      CONSTANT NUMBER       := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
9 G_LEVEL_STATEMENT            CONSTANT NUMBER       := FND_LOG.LEVEL_STATEMENT;
10 
11  /*
12   * List of document statuses that are used / set in this
13   * module.
14   */
15  DOC_STATUS_PMT_REMOVED  CONSTANT VARCHAR2(100) := 'REMOVED_PAYMENT_REMOVED';
16  DOC_STATUS_PMT_STOPPED  CONSTANT VARCHAR2(100) := 'REMOVED_PAYMENT_STOPPED';
17  DOC_STATUS_PMT_VOIDED   CONSTANT VARCHAR2(100) := 'REMOVED_PAYMENT_VOIDED';
18  DOC_STATUS_PMT_SPOILED  CONSTANT VARCHAR2(100) := 'REMOVED_PAYMENT_SPOILED';
19  DOC_STATUS_INS_TERM     CONSTANT VARCHAR2(100) :=
20                              'REMOVED_INSTRUCTION_TERMINATED';
21  DOC_STATUS_REQ_TERM     CONSTANT VARCHAR2(100) :=
22                              'REMOVED_REQUEST_TERMINATED';
23  DOC_STATUS_VALID        CONSTANT VARCHAR2(100) := 'VALIDATED';
24  DOC_STATUS_PAY_CREAT    CONSTANT VARCHAR2(100) := 'PAYMENT_CREATED';
25  DOC_STATUS_REJECTED     CONSTANT VARCHAR2(100) := 'REJECTED';
26  DOC_STATUS_REMOVED      CONSTANT VARCHAR2(100) := 'REMOVED';
27  DOC_STATUS_VOID_SETUP   CONSTANT VARCHAR2(100) := 'VOID_BY_SETUP';
28  DOC_STATUS_FAIL_CA      CONSTANT VARCHAR2(100) := 'FAILED_BY_CALLING_APP';
29 
30  /*
31   * List of payment statuses that are used / set in this
32   * module.
33   */
34  PAY_STATUS_INS_TERM       CONSTANT VARCHAR2(100) :=
35                                'REMOVED_INSTRUCTION_TERMINATED';
36  PAY_STATUS_REQ_TERM       CONSTANT VARCHAR2(100) :=
37                                'REMOVED_REQUEST_TERMINATED';
38  PAY_STATUS_CREATED        CONSTANT VARCHAR2(100) := 'CREATED';
39  PAY_STATUS_MODIFIED       CONSTANT VARCHAR2(100) := 'MODIFIED';
40  PAY_STATUS_MOD_BNK_ACC    CONSTANT VARCHAR2(100) :=
41                                'MODIFIED_PAYEE_BANK_ACCOUNT';
42  PAY_STATUS_INS_CREAT      CONSTANT VARCHAR2(100) := 'INSTRUCTION_CREATED';
43  PAY_STATUS_VOID           CONSTANT VARCHAR2(100) := 'VOID';
44  PAY_STATUS_REPRINT        CONSTANT VARCHAR2(100) := 'READY_TO_REPRINT';
45  PAY_STATUS_SPOILED        CONSTANT VARCHAR2(100) := 'REMOVED_DOCUMENT_SPOILED';
46  PAY_STATUS_ISSUED         CONSTANT VARCHAR2(100) := 'ISSUED';
47  PAY_STATUS_SUB_FOR_PRINT  CONSTANT VARCHAR2(100) := 'SUBMITTED_FOR_PRINTING';
48  PAY_STATUS_FORMATTED      CONSTANT VARCHAR2(100) := 'FORMATTED';
49  PAY_STATUS_TRANSMITTED    CONSTANT VARCHAR2(100) := 'TRANSMITTED';
50  PAY_STATUS_ACK            CONSTANT VARCHAR2(100) := 'ACKNOWLEDGED';
51  PAY_STATUS_BNK_VALID      CONSTANT VARCHAR2(100) := 'BANK_VALIDATED';
52  PAY_STATUS_PAID           CONSTANT VARCHAR2(100) := 'PAID';
53  PAY_STATUS_REMOVED        CONSTANT VARCHAR2(100) := 'REMOVED';
54  PAY_STATUS_VOID_SETUP     CONSTANT VARCHAR2(100) := 'VOID_BY_SETUP';
55  PAY_STATUS_VOID_OVERFLOW  CONSTANT VARCHAR2(100) := 'VOID_BY_OVERFLOW';
56  PAY_STATUS_STOPPED        CONSTANT VARCHAR2(100) := 'REMOVED_PAYMENT_STOPPED';
57  PAY_STATUS_SETUP_REPRINT  CONSTANT VARCHAR2(100) := 'VOID_BY_SETUP_REPRINT';
58  PAY_STATUS_OVERFLOW_REPRINT
59                            CONSTANT VARCHAR2(100) := 'VOID_BY_OVERFLOW_REPRINT';
60  PAY_STATUS_REJECTED       CONSTANT VARCHAR2(100) := 'REJECTED';
61  /*
62   * List of payment instruction statuses that are used / set in this
63   * module.
64   */
65  INS_STATUS_READY_TO_PRINT  CONSTANT VARCHAR2(100) :=
66                                          'CREATED_READY_FOR_PRINTING';
67  INS_STATUS_READY_TO_FORMAT CONSTANT VARCHAR2(100) :=
68                                          'CREATED_READY_FOR_FORMATTING';
69  INS_STATUS_FORMAT_TO_PRINT CONSTANT VARCHAR2(100) :=
70                                          'FORMATTED_READY_FOR_PRINTING';
71  INS_STATUS_PRINTED         CONSTANT VARCHAR2(100) :=
72                                          'PRINTED';
73  INS_STATUS_TERMINATED      CONSTANT VARCHAR2(100) := 'TERMINATED';
74  INS_STATUS_FORMATTED       CONSTANT VARCHAR2(100) := 'FORMATTED';
75  INS_STATUS_FORMATTED_ELEC  CONSTANT VARCHAR2(100) := 'FORMATTED_ELECTRONIC';
76  INS_STATUS_TRANSMITTED     CONSTANT VARCHAR2(100) := 'TRANSMITTED';
77 
78  /*
79   * List of payment request statuses that are used / set in this
80   * module.
81   */
82  REQ_STATUS_TERMINATED  CONSTANT VARCHAR2(100) := 'TERMINATED';
83  REQ_STATUS_COMPLETED  CONSTANT VARCHAR2(100) := 'COMPLETED';
84 
85  /*
86   * Paper document usage reasons.
87   */
88  DOC_USE_SPOILED     CONSTANT VARCHAR2(100) := 'SPOILED';
89  DOC_USE_ISSUED      CONSTANT VARCHAR2(100) := 'ISSUED';
90 
91  /*
92   * Payment completion code.
93   */
94  PMT_COMPLETE_YES    CONSTANT VARCHAR2(10) := 'YES';
95 
96  /*
97   * List of valid processing types on the payment profile.
98   */
99  P_TYPE_PRINTED      CONSTANT VARCHAR2(100) := 'PRINTED';
100  P_TYPE_ELECTRONIC   CONSTANT VARCHAR2(100) := 'ELECTRONIC';
101 
102  /*
103   * List of process types.
104   */
105  PROCESS_TYPE_IMMEDIATE CONSTANT VARCHAR2(100) := 'IMMEDIATE';
106  PROCESS_TYPE_STANDARD  CONSTANT VARCHAR2(100) := 'STANDARD';
107  PROCESS_TYPE_MANUAL    CONSTANT VARCHAR2(100) := 'MANUAL';
108 
109 
110 
111    /* Bug 9404359  */
112   /* added for caching the external_bank_acct_id*/
113   TYPE t_ext_acct_id_rec_type IS RECORD(
114     ext_bank_account_id  iby_ext_bank_accounts.ext_bank_account_id%TYPE
115    );
116   TYPE t_ext_acct_id_tbl_type IS TABLE OF t_ext_acct_id_rec_type INDEX BY VARCHAR2(2000);
117   g_ext_acct_id_tbl  t_ext_acct_id_tbl_type;
118 
119 
120 
121 /*--------------------------------------------------------------------
122  | NAME:
123  |     payment_stop_request
124  |
125  |
126  | PURPOSE:For Initiating the payment stop process. This procedure should be
127  | called only by AP. This should not be called by IBY.
128  |
129  |
130  | PARAMETERS:
131  |     IN      p_payment_id      -- id of the payment.
132  |             p_requested_by   -- User id of person who issued the void request.
133  |                                 This id will be stored as an attribute of the
134  |                                 payment.
135  |             p_request_reason
136  |             p_request_reference
137  |             p_request_date
138  |
139  |     OUT
140  |            x_return_status - Result of the API call:
141  |                                FND_API.G_RET_STS_SUCCESS indicates that a
142  |                                  callout was invoked successfully.
143  |                                  In this case the caller must COMMIT
144  |                                  the status change.
145  |
146  |                                FND_API.G_RET_STS_UNEXP_ERROR (or other) indicates
147  |                                  that API did not complete successfully.
148  |                                  In this case, the caller must issue a
149  |                                  ROLLBACK to undo all status changes.
150  |             x_msg_count
151  |             x_msg_data
152  |
153  |
154  | NOTES:
155  |
156  *---------------------------------------------------------------------*/
157  PROCEDURE payment_stop_request (
158         p_payment_id		     IN  NUMBER,
159         p_requested_by       IN  NUMBER,
160         p_request_reason     IN  VARCHAR2,
161         p_request_reference  IN  VARCHAR2,
162         p_request_date       IN  DATE,
163         x_return_status	  OUT nocopy VARCHAR2,
164         x_msg_count		    OUT nocopy NUMBER,
165         x_msg_data		    OUT nocopy VARCHAR2)
166   IS
167        l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
168                                          || '.payment_stop_request';
169 
170   BEGIN
171           IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
172 	          print_debuginfo(l_module_name, 'ENTER');
173 
174           END IF;
175           UPDATE iby_payments_all
176           SET STOP_REQUEST_PLACED_FLAG = 'Y',
177               STOP_REQUEST_PLACED_BY = p_requested_by,
178               STOP_REQUEST_REASON = p_request_reason,
179               STOP_REQUEST_REFERENCE= p_request_reference,
180               STOP_REQUEST_DATE = p_request_date,
181               STOP_RELEASED_FLAG = 'N',
182               STOP_RELEASED_BY = NULL,
183               STOP_RELEASE_DATE = NULL,
184               STOP_RELEASE_REASON = NULL,
185               STOP_RELEASE_REFERENCE = NULL
186 
187           WHERE
188               PAYMENT_ID = p_payment_id;
189 
190          x_return_status := FND_API.G_RET_STS_SUCCESS;
191            IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
192 	           print_debuginfo(l_module_name, 'EXIT');
193 
194            END IF;
195           EXCEPTION
196             WHEN OTHERS THEN
197                   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
198                    x_msg_count := 1;
199                    x_msg_data := substr(SQLERRM,1,25);
200 
201                IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
202 	               print_debuginfo(l_module_name, 'EXIT');
203                END IF;
204   END payment_stop_request;
205 
206 
207   /*--------------------------------------------------------------------
208  | NAME:
209  |     payment_stop_release
210  |
211  |
212  | PURPOSE:For Releasing the stop request . This procedure should be
213  | called only by AP. This should not be called by IBY.
214  |
215  |
216  | PARAMETERS:
217  |     IN      p_payment_id      -- payment id
218  |             p_released_by   -- User id of person who issued the void request.
219  |                                 This id will be stored as an attribute of the
220  |                                 payment.
221  |             p_release_reason
222  |             p_release_reference
223  |             p_release_date
224  |
225  |     OUT     x_return_status - Result of the API call:
226  |                                FND_API.G_RET_STS_SUCCESS indicates that a
227  |                                  callout was invoked successfully.
228  |                                  In this case the caller must COMMIT
229  |                                  the status change.
230  |
231  |                                FND_API.G_RET_STS_UNEXP_ERROR (or other) indicates
232  |                                  that API did not complete successfully.
233  |                                  In this case, the caller must issue a
234  |                                  ROLLBACK to undo all status changes.
235  |             x_msg_count
236  |             x_msg_data
237  |
238  |
239  | NOTES:
240  |
241  *---------------------------------------------------------------------*/
242  PROCEDURE payment_stop_release (
243         p_payment_id		     IN  NUMBER,
244         p_released_by        IN  NUMBER,
245         p_release_reason     IN  VARCHAR2,
246         p_release_reference  IN  VARCHAR2,
247         p_release_date       IN  DATE,
248         x_return_status	     OUT nocopy VARCHAR2,
249         x_msg_count		       OUT nocopy NUMBER,
250         x_msg_data		       OUT nocopy VARCHAR2)
251   IS
252      l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
253                                          || '.payment_stop_release';
254   BEGIN
255           IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
256 	          print_debuginfo(l_module_name, 'ENTER');
257 
258           END IF;
259           UPDATE iby_payments_all
260           SET STOP_RELEASED_FLAG = 'Y',
261               STOP_RELEASED_BY = p_released_by,
262               STOP_RELEASE_DATE = p_release_date,
263               STOP_RELEASE_REASON = p_release_reason,
264               STOP_RELEASE_REFERENCE = p_release_reference
265 
266           WHERE
267               PAYMENT_ID = p_payment_id;
268 
269          x_return_status := FND_API.G_RET_STS_SUCCESS;
270            IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
271 	           print_debuginfo(l_module_name, 'EXIT');
272 
273            END IF;
274           EXCEPTION
275             WHEN OTHERS THEN
276                   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
277                    x_msg_count := 1;
278                    x_msg_data := substr(SQLERRM,1,25);
279 
280                IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
281 	               print_debuginfo(l_module_name, 'EXIT');
282 
283                END IF;
284   END payment_stop_release;
285 
286 
287 
288 
289 
290 /*--------------------------------------------------------------------
291  | NAME:
292  |     remove_document_payable
293  |
294  | PURPOSE:
295  |     Invokes the callout of the calling app to remove a submitted
296  |     document payable from the payment processing cycle.
297  |
298  |     The calling application can free up the removed document, and
299  |     make it available for submission in a future payment request.
300  |
301  | PARAMETERS:
302  |     IN
303  |       p_doc_id        - ID of the document payable to be removed.
304  |       p_doc_status    - Current status of the document payable to
305  |                         be removed.
306  |
307  |     OUT
308  |       x_return_status - Result of the API call:
309  |                         FND_API.G_RET_STS_SUCCESS indicates that a
310  |                           callout was invoked successfully.
311  |                           In this case the caller must COMMIT
312  |                           the status change.
313  |
314  |                         FND_API.G_RET_STS_ERROR (or other) indicates
315  |                           that API did not complete successfully.
316  |                           In this case, the caller must issue a
317  |                           ROLLBACK to undo all status changes.
318  |
319  | RETURNS:
320  |
321  | NOTES:
322  |   Internal API, not for public use.
323  |
324  |   This API will not do a COMMIT. It is the the callers responsbility
325  |   to perform COMMIT / ROLLBACK depending upon the returned status.
326  |
327  |   The callout invoked must be handled synchronously by the
328  |   calling application. So the COMMIT / ROLLBACK should affect
329  |   the changes made to the database by the calling application as
330  |   well. This will ensure that IBY and the calling application are
331  |   in sync w.r.t. to the specified document payable.
332  |
333  *---------------------------------------------------------------------*/
334  PROCEDURE remove_document_payable (
335      p_doc_id         IN NUMBER,
336      p_doc_status     IN VARCHAR2,
337      x_return_status  OUT NOCOPY VARCHAR2
338      )
339  IS
340 
341  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
342                                          || '.remove_document_payable';
343 
344  l_rejection_id   NUMBER(15);
345 
346  /* used in forming callout procedure name */
347  l_calling_app_id NUMBER;
348  l_app_short_name VARCHAR2(200);
349  l_pkg_name       VARCHAR2(200);
350  l_callout_name   VARCHAR2(500);
351  l_stmt           VARCHAR2(1000);
352 
353  /* used in invocation of callout procedure */
354  l_api_version    CONSTANT NUMBER := 1.0;
355  l_msg_count      NUMBER;
356  l_msg_data       VARCHAR2(2000);
357 
358  /*
359   * Implementing the callout is optional for the calling app.
360   * If the calling app does not implement the hook, then
361   * the call to the hook will result in ORA-06576 error.
362   *
363   * There is no exception name associated with this code, so
364   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
365   * exception occurs, it is not fatal: we log the error and
366   * proceed.
367   *
368   * If, on the other hand, the calling app implements the
369   * callout, but the callout throws an exception, it is fatal
370   * and we must abort the program (this will be caught
371   * in WHEN OTHERS block).
372   */
373  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
374  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
375 
376  BEGIN
377 
378      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
379 	     print_debuginfo(l_module_name, 'ENTER');
380 
381      END IF;
382      FND_MSG_PUB.initialize;
383 
384      /*
385       * STATUS CHANGE:
386       *
387       * UI Responsibility:
388       * document status = REMOVED
389       *
390       * API Responsibility:
391       * NONE
392       */
393 
394      /*
395       * Get the next available rejected document group id.
396       */
397      SELECT
398          IBY_REJECTED_DOCS_GROUP_S.NEXTVAL
399      INTO
400          l_rejection_id
401      FROM
402          DUAL
403      ;
404 
405      /*
406       * Update the removed document with the rejected document
407       * group id. The calling application will identify rejected
408       * documents using this id.
409       */
410      UPDATE
411          IBY_DOCS_PAYABLE_ALL
412      SET
413          rejected_docs_group_id = l_rejection_id
414      WHERE
415          document_payable_id = p_doc_id        AND
416          document_status     = p_doc_status
417      ;
418 
419      /*
420       * Get the application name of the calling app. This
421       * will be used in the callout.
422       */
423      SELECT
424          fnd.application_short_name
425      INTO
426          l_app_short_name
427      FROM
428          FND_APPLICATION      fnd,
429          IBY_DOCS_PAYABLE_ALL doc
430      WHERE
431          fnd.application_id      = doc.calling_app_id AND
432          doc.document_payable_id = p_doc_id
433      ;
434 
435      /*
436       * Get the constructed package name to use in the
437       * call out.
438       */
439      l_pkg_name := construct_callout_pkg_name(l_app_short_name);
440 
441      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
442 	     print_debuginfo(l_module_name, 'Constructed package name: '
443 	         || l_pkg_name);
444 
445      END IF;
446      IF (l_pkg_name IS NULL) THEN
447 
448             print_debuginfo(l_module_name, 'Package name is null. '
449 	             || 'Raising exception.');
450 
451 
452          APP_EXCEPTION.RAISE_EXCEPTION;
453 
454      END IF;
455 
456 
457      /*
458       * Now try to call the external app's implementation of the hook.
459       * The calling app may or may not have implemented the hook, so
460       * it's not fatal if the implementation does not exist.
461       */
462      l_callout_name := l_pkg_name || '.' || 'documents_payable_rejected';
463 
464      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
465 	     print_debuginfo(l_module_name, 'Attempting to invoke callout: '
466 	         || l_callout_name);
467 
468      END IF;
469      l_stmt := 'CALL '|| l_callout_name || '(:1, :2, :3, :4, :5, :6, :7)';
470 
471      BEGIN
472 
473          EXECUTE IMMEDIATE
474              (l_stmt)
475          USING
476              IN  l_api_version,
477              IN  FND_API.G_FALSE,
478              IN  FND_API.G_FALSE,
479              OUT x_return_status,
480              OUT l_msg_count,
481              OUT l_msg_data,
482              IN  l_rejection_id
483          ;
484 
485          /*
486           * If the called procedure did not return success,
487           * raise an exception.
488           */
489          IF (x_return_status IS NULL OR
490              x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
491 
492 
493 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
494 	                 || l_callout_name
495 	                 || ''', returned failure status - '
496 	                 || x_return_status
497 	                 || '. Raising exception.'
498 	                 );
499 
500 
501              APP_EXCEPTION.RAISE_EXCEPTION;
502 
503          END IF;
504 
505      EXCEPTION
506 
507          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
508              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
509 	             print_debuginfo(l_module_name, 'Callout "' || l_callout_name
510 	                 || '" not implemented by calling app '
511 	                 || l_app_short_name || '.');
512 
513 	             print_debuginfo(l_module_name, 'Skipping hook call.');
514 
515 
516              END IF;
517          WHEN OTHERS THEN
518 
519 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
520 	                 || l_callout_name
521 	                 || ''', generated exception.'
522 	                 );
523 
524 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
525 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
526 
527              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
528 
529              /*
530               * Propogate exception to caller.
531               */
532              RAISE;
533      END;
534 
535      x_return_status := FND_API.G_RET_STS_SUCCESS;
536 
537      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
538 	     print_debuginfo(l_module_name, 'EXIT');
539 
540      END IF;
541  EXCEPTION
542 
543      WHEN OTHERS THEN
544 
545          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
546 	         print_debuginfo(l_module_name, 'Exception occured when '
547 	             || 'removing document payable '
548 	             || p_doc_id
549 	             || ', with status '
550 	             || p_doc_status
551 	             );
552 
553 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
554 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
555          END IF;
556          FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
557          x_return_status := FND_API.G_RET_STS_ERROR;
558 
559          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
560 	         print_debuginfo(l_module_name, 'EXIT');
561 
562          END IF;
563  END remove_document_payable;
564 
565 /*--------------------------------------------------------------------
566  | NAME:
567  |     remove_documents_payable
568  |
569  | PURPOSE:
570  |     Invokes a series of callouts to the calling app to remove
571  |     a set of submitted documents payable from the payment processing
572  |     cycle.
573  |
574  |     The calling application can free up the removed documents, and
575  |     make them available for submission in future payment request(s).
576  |
577  | PARAMETERS:
578  |     IN
579  |       p_doc_list        - IDs of the documents payable to be removed.
580  |                           This should be an array of document payable ids.
581  |
582  |       p_doc_status_list - Current statuses of the documents payable to
583  |                           be removed. This should be an array of statuses.
584  |
585  |     OUT
586  |       x_return_status   - Result of the API call:
587  |                         FND_API.G_RET_STS_SUCCESS indicates that all
588  |                           callouts were invoked successfully.
589  |                           In this case the caller must COMMIT
590  |                           the status changes.
591  |
592  |                         FND_API.G_RET_STS_ERROR (or other) indicates
593  |                           that at least one event did not complete
594  |                           successfully. In this case, the caller must
595  |                           issue a ROLLBACK to undo all status changes.
596  |
597  | RETURNS:
598  |
599  | NOTES:
600  |   This API will not do a COMMIT. It is the the callers responsbility
601  |   to perform COMMIT / ROLLBACK depending upon the returned status.
602  |
603  |   The callouts invoked must be handled synchronously by the
604  |   calling application. So the COMMIT / ROLLBACK should affect
605  |   the changes made to the database by the calling application as
606  |   well. This will ensure that IBY and the calling application are
607  |   in sync w.r.t. to the specified documents payable.
608  |
609  *---------------------------------------------------------------------*/
610  PROCEDURE remove_documents_payable (
611      p_doc_list         IN docPayIDTab,
612      p_doc_status_list  IN docPayStatusTab,
613      x_return_status    OUT NOCOPY VARCHAR2
614      )
615  IS
616 
617  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
618                                          || '.remove_documents_payable';
619 
620  l_rejection_id   NUMBER(15);
621  l_calling_app_id NUMBER;
622  l_app_short_name VARCHAR2(200);
623  l_pkg_name       VARCHAR2(200);
624  l_callout_name   VARCHAR2(500);
625  l_stmt           VARCHAR2(1000);
626  l_api_version    CONSTANT NUMBER := 1.0;
627  l_msg_count      NUMBER;
628  l_msg_data       VARCHAR2(2000);
629  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
630  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
631 
632  BEGIN
633 
634      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
635 	     print_debuginfo(l_module_name, 'ENTER');
636 
637      END IF;
638      FND_MSG_PUB.initialize;
639 
640      IF (p_doc_list.COUNT = 0 OR p_doc_status_list.COUNT = 0) THEN
641 
642          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
643 	         print_debuginfo(l_module_name, 'Error: List of document '
644 	             || 'payable ids/statuses is empty'
645 	             || '. Returning failure response .. '
646 	             );
647 
648          END IF;
649          x_return_status := FND_API.G_RET_STS_ERROR;
650 
651          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
652 	         print_debuginfo(l_module_name, 'EXIT');
653 
654          END IF;
655          RETURN;
656 
657      END IF;
658 
659      IF (p_doc_list.COUNT <> p_doc_status_list.COUNT) THEN
660 
661          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
662 	         print_debuginfo(l_module_name, 'Error: List of document '
663 	             || 'payable ids must match list of document payable '
664 	             || 'statuses'
665 	             || '. Returning failure response .. '
666 	             );
667 
668          END IF;
669          x_return_status := FND_API.G_RET_STS_ERROR;
670 
671          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
672 	         print_debuginfo(l_module_name, 'EXIT');
673 
674          END IF;
675          RETURN;
676 
677      END IF;
678 
679      /*
680       * Start processing the documents payable, one-by-one.
681       */
682 
683      /*
684       * Bug 12668050
685       */
686 
687      /*
688       * Get the next available rejected document group id.
689       */
690      SELECT
691          IBY_REJECTED_DOCS_GROUP_S.NEXTVAL
692      INTO
693          l_rejection_id
694      FROM
695          DUAL
696      ;
697      FOR i IN p_doc_list.FIRST .. p_doc_list.LAST LOOP
698 
699 
700          UPDATE
701            IBY_DOCS_PAYABLE_ALL
702          SET
703            rejected_docs_group_id = l_rejection_id
704          WHERE
705            document_payable_id = p_doc_list(i) AND
706            document_status     = p_doc_status_list(i)
707          ;
708 
709          /*remove_document_payable (
710              p_doc_list(i),
711              p_doc_status_list(i),
712              x_return_status
713              );*/
714 
715 
716      END LOOP;
717 
718      /*
719       * Check if the call to remove the document
720       * payable succeeded.
721       */
722 
723      SELECT
724          distinct fnd.application_short_name
725      INTO
726          l_app_short_name
727      FROM
728          FND_APPLICATION      fnd,
729          IBY_DOCS_PAYABLE_ALL doc
730      WHERE
731          fnd.application_id      = doc.calling_app_id AND
732          doc.rejected_docs_group_id = l_rejection_id
733      ;
734 
735      /*
736       * Get the constructed package name to use in the
737       * call out.
738       */
739      l_pkg_name := construct_callout_pkg_name(l_app_short_name);
740 
741      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
742 	     print_debuginfo(l_module_name, 'Constructed package name: '
743 	         || l_pkg_name);
744 
745      END IF;
746      IF (l_pkg_name IS NULL) THEN
747 
748 
749 	         print_debuginfo(l_module_name, 'Package name is null. '
750 	             || 'Raising exception.');
751 
752 
753          APP_EXCEPTION.RAISE_EXCEPTION;
754 
755      END IF;
756 
757 
758      /*
759       * Now try to call the external app's implementation of the hook.
760       * The calling app may or may not have implemented the hook, so
761       * it's not fatal if the implementation does not exist.
762       */
763      l_callout_name := l_pkg_name || '.' || 'documents_payable_rejected';
764 
765      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
766 	     print_debuginfo(l_module_name, 'Attempting to invoke callout: '
767 	         || l_callout_name);
768 
769      END IF;
770      l_stmt := 'CALL '|| l_callout_name || '(:1, :2, :3, :4, :5, :6, :7)';
771 
772 
773      BEGIN
774 
775          EXECUTE IMMEDIATE
776              (l_stmt)
777          USING
778              IN  l_api_version,
779              IN  FND_API.G_FALSE,
780              IN  FND_API.G_FALSE,
781              OUT x_return_status,
782              OUT l_msg_count,
783              OUT l_msg_data,
784              IN  l_rejection_id
785          ;
786         /*
787           * If the called procedure did not return success,
788           * raise an exception.
789           */
790          IF (x_return_status IS NULL OR
791              x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
792 
793 
794 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
795 	                 || l_callout_name
796 	                 || ''', returned failure status - '
797 	                 || x_return_status
798 	                 || '. Raising exception.'
799 	                 );
800 
801 
802              APP_EXCEPTION.RAISE_EXCEPTION;
803 
804          END IF;
805 
806      EXCEPTION
807 
808          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
809              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
810 	             print_debuginfo(l_module_name, 'Callout "' || l_callout_name
811 	                 || '" not implemented by calling app '
812 	                 || l_app_short_name || '.');
813 
814 	             print_debuginfo(l_module_name, 'Skipping hook call.');
815 
816 
817              END IF;
818          WHEN OTHERS THEN
819 
820 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
821 	                 || l_callout_name
822 	                 || ''', generated exception.'
823 	                 );
824 
825 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
826 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
827 
828              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
829 
830              /*
831               * Propogate exception to caller.
832               */
833              RAISE;
834      END;
835 
836      x_return_status := FND_API.G_RET_STS_SUCCESS;
837 
838      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
839 	     print_debuginfo(l_module_name, 'Returning success response ..');
840 
841 	     print_debuginfo(l_module_name, 'EXIT');
842 
843      END IF;
844 
845      EXCEPTION
846 
847      WHEN OTHERS THEN
848 
849          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
850 	         print_debuginfo(l_module_name, 'Exception occured when '
851 	             || 'removing documents payable '
852 	             );
853 
854 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
855 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
856          END IF;
857          FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
858          x_return_status := FND_API.G_RET_STS_ERROR;
859 
860          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
861 	         print_debuginfo(l_module_name, 'EXIT');
862 
863          END IF;
864  END remove_documents_payable;
865 
866 /*--------------------------------------------------------------------
867  | NAME:
868  |     remove_payment
869  |
870  |
871  | PURPOSE:
872  |     Invokes a callout of the calling app to remove a set of
873  |     submitted documents payable from the payment processing
874  |     cycle.
875  |
876  |     The calling application can free up the removed documents, and
877  |     make them available for submission in future payment request(s).
878  |
879  | PARAMETERS:
880  |     IN
881  |       p_pmt_id          - ID of the payment to be removed.
882  |
883  |       p_pmt_status_list - Current status of the payment to
884  |                           be removed.
885  |
886  |     OUT
887  |       x_return_status   - Result of the API call:
888  |                         FND_API.G_RET_STS_SUCCESS indicates that
889  |                           the callout was invoked successfully.
890  |                           In this case the caller must COMMIT
891  |                           the status changes.
892  |
893  |                         FND_API.G_RET_STS_ERROR (or other) indicates
894  |                           that tha API did not complete successfully.
895  |                           In this case, the caller must issue a
896  |                           ROLLBACK to undo all status changes.
897  |
898  | RETURNS:
899  |
900  | NOTES:
901  |   Internal API, not for public use.
902  |
903  |   This API will not do a COMMIT. It is the the callers responsibility
904  |   to perform COMMIT / ROLLBACK depending upon the returned status.
905  |
906  |   The callout invoked must be handled synchronously by the
907  |   calling application. So the COMMIT / ROLLBACK should affect
908  |   the changes made to the database by the calling application as
909  |   well. This will ensure that IBY and the calling application are
910  |   in sync w.r.t. to the specified documents payable.
911  |
912  *---------------------------------------------------------------------*/
913  PROCEDURE remove_payment (
914      p_pmt_id         IN NUMBER,
915      p_pmt_status     IN VARCHAR2,
916      x_return_status  OUT NOCOPY VARCHAR2
917      )
918  IS
919 
920  l_module_name    CONSTANT VARCHAR2(200) := G_PKG_NAME || '.remove_payment';
921 
922  l_rejection_id   NUMBER(15);
923 
924  /* used in forming callout procedure name */
925  l_calling_app_id NUMBER;
926  l_app_short_name VARCHAR2(200);
927  l_pkg_name       VARCHAR2(200);
928  l_callout_name   VARCHAR2(500);
929  l_stmt           VARCHAR2(1000);
930 
931  /* used in invocation of callout procedure */
932  l_api_version    CONSTANT NUMBER := 1.0;
933  l_msg_count      NUMBER;
934  l_msg_data       VARCHAR2(2000);
935 
936  /*
937   * Implementing the callout is optional for the calling app.
938   * If the calling app does not implement the hook, then
939   * the call to the hook will result in ORA-06576 error.
940   *
941   * There is no exception name associated with this code, so
942   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
943   * exception occurs, it is not fatal: we log the error and
944   * proceed.
945   *
946   * If, on the other hand, the calling app implements the
947   * callout, but the callout throws an exception, it is fatal
948   * and we must abort the program (this will be caught
949   * in WHEN OTHERS block).
950   */
951  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
952  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
953 
954  BEGIN
955 
956      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
957 	     print_debuginfo(l_module_name, 'ENTER');
958 
959      END IF;
960      FND_MSG_PUB.initialize;
961 
962      /*
963       * STATUS CHANGE:
964       *
965       * UI Responsibility:
966       * payment status = REMOVED
967       *
968       * API Responsibility:
969       * document_status = REMOVED_PAYMENT_REMOVED
970       */
971      UPDATE
972          IBY_DOCS_PAYABLE_ALL
973      SET
974          document_status = DOC_STATUS_PMT_REMOVED,
975 
976          /*
977           * Fix for bug 4405981:
978           *
979           * The straight through flag should be set to 'N',
980           * if the document was rejected / required manual
981           * intervention.
982           */
983          straight_through_flag = 'N'
984      WHERE
985          payment_id = p_pmt_id
986      ;
987 
988      /*
989       * Get the next available rejected document group id.
990       */
991      SELECT
992          IBY_REJECTED_DOCS_GROUP_S.NEXTVAL
993      INTO
994          l_rejection_id
995      FROM
996          DUAL
997      ;
998 
999      /*
1000       * Update the removed documents with the rejected document
1001       * group id. The calling application will identify rejected
1002       * documents using this id.
1003       */
1004      UPDATE
1005          IBY_DOCS_PAYABLE_ALL
1006      SET
1007          rejected_docs_group_id = l_rejection_id
1008      WHERE
1009          payment_id = p_pmt_id
1010      ;
1011 
1012      /*
1013       * Get the application name of the calling app. This
1014       * will be used in the callout.
1015       */
1016      SELECT
1017          fnd.application_short_name
1018      INTO
1019          l_app_short_name
1020      FROM
1021          FND_APPLICATION           fnd,
1022          IBY_PAYMENTS_ALL          pmt,
1023          IBY_PAY_SERVICE_REQUESTS  req
1024      WHERE
1025          fnd.application_id             = req.calling_app_id             AND
1026          req.payment_service_request_id = pmt.payment_service_request_id AND
1027          pmt.payment_id                 = p_pmt_id
1028      ;
1029 
1030      /*
1031       * Get the constructed package name to use in the
1032       * call out.
1033       */
1034      l_pkg_name := construct_callout_pkg_name(l_app_short_name);
1035 
1036      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1037 	     print_debuginfo(l_module_name, 'Constructed package name: '
1038 	         || l_pkg_name);
1039 
1040      END IF;
1041      IF (l_pkg_name IS NULL) THEN
1042 
1043 
1044 	         print_debuginfo(l_module_name, 'Package name is null. '
1045 	             || 'Raising exception.');
1046 
1047 
1048          APP_EXCEPTION.RAISE_EXCEPTION;
1049 
1050      END IF;
1051 
1052      /*
1053       * Now try to call the external app's implementation of the hook.
1054       * The calling app may or may not have implemented the hook, so
1055       * it's not fatal if the implementation does not exist.
1056       */
1057      l_callout_name := l_pkg_name || '.' || 'documents_payable_rejected';
1058 
1059      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1060 	     print_debuginfo(l_module_name, 'Attempting to invoke callout: '
1061 	         || l_callout_name);
1062 
1063      END IF;
1064      l_stmt := 'CALL '|| l_callout_name || '(:1, :2, :3, :4, :5, :6, :7)';
1065 
1066      BEGIN
1067 
1068          EXECUTE IMMEDIATE
1069              (l_stmt)
1070          USING
1071              IN  l_api_version,
1072              IN  FND_API.G_FALSE,
1073              IN  FND_API.G_FALSE,
1074              OUT x_return_status,
1075              OUT l_msg_count,
1076              OUT l_msg_data,
1077              IN  l_rejection_id
1078          ;
1079 
1080          /*
1081           * If the called procedure did not return success,
1082           * raise an exception.
1083           */
1084          IF (x_return_status IS NULL OR
1085              x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1086 
1087 
1088 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
1089 	                 || l_callout_name
1090 	                 || ''', returned failure status - '
1091 	                 || x_return_status
1092 	                 || '. Raising exception.'
1093 	                 );
1094 
1095 
1096              APP_EXCEPTION.RAISE_EXCEPTION;
1097 
1098          END IF;
1099 
1100      EXCEPTION
1101 
1102          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
1103              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1104 	             print_debuginfo(l_module_name, 'Callout "' || l_callout_name
1105 	                 || '" not implemented by calling app '
1106 	                 || l_app_short_name || '.');
1107 
1108 	             print_debuginfo(l_module_name, 'Skipping hook call.');
1109 
1110 
1111              END IF;
1112          WHEN OTHERS THEN
1113 
1114 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
1115 	                 || l_callout_name
1116 	                 || ''', generated exception.'
1117 	                 );
1118 
1119 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
1120 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
1121 
1122              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
1123 
1124              /*
1125               * Propogate exception to caller.
1126               */
1127              RAISE;
1128      END;
1129 
1130 
1131      x_return_status := FND_API.G_RET_STS_SUCCESS;
1132 
1133      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1134 	     print_debuginfo(l_module_name, 'EXIT');
1135 
1136      END IF;
1137  EXCEPTION
1138 
1139      WHEN OTHERS THEN
1140 
1141          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1142 	         print_debuginfo(l_module_name, 'Exception occured when '
1143 	             || 'removing payment '
1144 	             || p_pmt_id
1145 	             || ', with status '
1146 	             || p_pmt_status
1147 	             );
1148 
1149 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
1150 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
1151          END IF;
1152          FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
1153 
1154          x_return_status := FND_API.G_RET_STS_ERROR;
1155 
1156          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1157 	         print_debuginfo(l_module_name, 'EXIT');
1158 
1159          END IF;
1160  END remove_payment;
1161 
1162 /*--------------------------------------------------------------------
1163  | NAME:
1164  |     remove_payments
1165  |
1166  | PURPOSE:
1167  |     Invokes a series of callouts of the calling app to remove
1168  |     a set of submitted documents payable from the payment processing
1169  |     cycle.
1170  |
1171  |     The calling application can free up the removed documents, and
1172  |     make them available for submission in future payment request(s).
1173  |
1174  | PARAMETERS:
1175  |     IN
1176  |       p_pmt_list        - IDs of the payments to be removed.
1177  |                           This should be an array of payment ids. All
1178  |                           the child documents payable of each of the
1179  |                           specified payments will be removed.
1180  |
1181  |       p_pmt_status_list - Current statuses of the payments to
1182  |                           be removed. This should be an array of statuses.
1183  |
1184  |     OUT
1185  |       x_return_status   - Result of the API call:
1186  |                         FND_API.G_RET_STS_SUCCESS indicates that all
1187  |                           the callouts were invoked successfully.
1188  |                           In this case the caller must COMMIT
1189  |                           the status changes.
1190  |
1191  |                         FND_API.G_RET_STS_ERROR (or other) indicates
1192  |                           that at least one event did not complete
1193  |                           successfully. In this case, the caller must
1194  |                           issue a ROLLBACK to undo all status changes.
1195  |
1196  | RETURNS:
1197  |
1198  | NOTES:
1199  |   Internal API, not for public use.
1200  |
1201  |   This API will not do a COMMIT. It is the the callers responsbility
1202  |   to perform COMMIT / ROLLBACK depending upon the returned status.
1203  |
1204  |   The callouts invoked must be handled synchronously by the
1205  |   calling application. So the COMMIT / ROLLBACK should affect
1206  |   the changes made to the database by the calling application as
1207  |   well. This will ensure that IBY and the calling application are
1208  |   in sync w.r.t. to the specified documents payable.
1209  |
1210  *---------------------------------------------------------------------*/
1211  PROCEDURE remove_payments (
1212      p_pmt_list         IN pmtIDTab,
1213      p_pmt_status_list  IN pmtStatusTab,
1214      x_return_status    OUT NOCOPY VARCHAR2
1215      )
1216  IS
1217 
1218  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME || '.remove_payments';
1219 
1220  BEGIN
1221 
1222      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1223 	     print_debuginfo(l_module_name, 'ENTER');
1224 
1225      END IF;
1226      FND_MSG_PUB.initialize;
1227 
1228      IF (p_pmt_list.COUNT = 0 OR p_pmt_status_list.COUNT = 0) THEN
1229 
1230          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1231 	         print_debuginfo(l_module_name, 'Error: List of payment '
1232 	             || 'payable ids/statuses is empty'
1233 	             || '. Returning failure response .. '
1234 	             );
1235 
1236          END IF;
1237          x_return_status := FND_API.G_RET_STS_ERROR;
1238 
1239          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1240 	         print_debuginfo(l_module_name, 'EXIT');
1241 
1242          END IF;
1243          RETURN;
1244 
1245      END IF;
1246 
1247      IF (p_pmt_list.COUNT <> p_pmt_status_list.COUNT) THEN
1248 
1249          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1250 	         print_debuginfo(l_module_name, 'Error: List of payment '
1251 	             || 'ids must match list of payment statuses. '
1252 	             || 'Returning failure response .. '
1253 	             );
1254 
1255          END IF;
1256          x_return_status := FND_API.G_RET_STS_ERROR;
1257 
1258          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1259 	         print_debuginfo(l_module_name, 'EXIT');
1260 
1261          END IF;
1262          RETURN;
1263 
1264      END IF;
1265 
1266      /*
1267       * Start processing the payments, one-by-one.
1268       */
1269      FOR i IN p_pmt_list.FIRST .. p_pmt_list.LAST LOOP
1270 
1271          remove_payment (
1272              p_pmt_list(i),
1273              p_pmt_status_list(i),
1274              x_return_status
1275              );
1276 
1277          /*
1278           * Check if the call to remove the payment succeeded.
1279           */
1280          IF (x_return_status IS NULL OR
1281 	     x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1282 
1283              /*
1284               * Even if a single call to remove a payment
1285               * failed, return failure for the entire API request.
1286               */
1287              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1288 	             print_debuginfo(l_module_name, 'Removing of payment '
1289 	                 || p_pmt_list(i)
1290 	                 || ' failed.'
1291 	                 );
1292 
1293 	             print_debuginfo(l_module_name, 'EXIT');
1294 
1295              END IF;
1296              /*
1297               * It is the callers responsibility to rollback
1298               * all the changes.
1299               */
1300              RETURN;
1301 
1302          END IF;
1303 
1304      END LOOP;
1305 
1306      x_return_status := FND_API.G_RET_STS_SUCCESS;
1307 
1308      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1309 	     print_debuginfo(l_module_name, 'Returning success response ..');
1310 
1311 	     print_debuginfo(l_module_name, 'EXIT');
1312 
1313      END IF;
1314  END remove_payments;
1315 
1316 /*--------------------------------------------------------------------
1317  | NAME:
1318  |     remove_payment_request
1319  |
1320  | PURPOSE:
1321  |     Invokes a series of callouts of the calling app to remove
1322  |     all the submitted documents payable of the given payment request
1323  |     from the payment processing cycle.
1324  |
1325  |     The calling application can free up the removed documents, and
1326  |     make them available for submission in future payment request(s).
1327  |
1328  | PARAMETERS:
1329  |     IN
1330  |       p_payreq_id       - ID of the payment request which must be removed.
1331  |                           All documents payable associated with the
1332  |                           specified payment request will be removed.
1333  |
1334  |     OUT
1335  |       x_return_status   - Result of the API call:
1336  |                         FND_API.G_RET_STS_SUCCESS indicates that all
1337  |                           the callouts were invoked successfully.
1338  |                           In this case the caller must COMMIT
1339  |                           the status changes.
1340  |
1341  |                         FND_API.G_RET_STS_ERROR (or other) indicates
1342  |                           that at least one event did not complete
1343  |                           successfully. In this case, the caller must
1344  |                           issue a ROLLBACK to undo all status changes.
1345  |
1346  | RETURNS:
1347  |
1348  | NOTES:
1349  |   Internal API, not for public use.
1350  |
1351  |   This API will not do a COMMIT. It is the the callers responsbility
1352  |   to perform COMMIT / ROLLBACK depending upon the returned status.
1353  |
1354  |   The callouts invoked must be handled synchronously by the
1355  |   calling application. So the COMMIT / ROLLBACK should affect
1356  |   the changes made to the database by the calling application as
1357  |   well. This will ensure that IBY and the calling application are
1358  |   in sync w.r.t. to the specified documents payable.
1359  |
1360  *---------------------------------------------------------------------*/
1361  PROCEDURE remove_payment_request (
1362      p_payreq_id        IN  NUMBER,
1363      x_return_status    OUT NOCOPY VARCHAR2
1364      )
1365  IS
1366 
1367  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
1368                                              '.remove_payment_request';
1369 
1370  l_docs_list        IBY_VALIDATIONSETS_PUB.rejectedDocTabType;
1371  l_doc_id_list      docPayIDTab;
1372  l_doc_status_list  docPayStatusTab;
1373 
1374  /*
1375   * Cursor to get list of documents along with their statuses for
1376   * the given payment service request.
1377   */
1378  CURSOR c_docs (p_payreq_id IBY_PAY_SERVICE_REQUESTS.
1379                                 payment_service_request_id%TYPE)
1380  IS
1381  SELECT
1382      doc.document_payable_id,
1383      doc.document_status
1384  FROM
1385      IBY_DOCS_PAYABLE_ALL doc
1386  WHERE
1387      doc.payment_service_request_id = p_payreq_id
1388  ;
1389 
1390  BEGIN
1391 
1392      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1393 	     print_debuginfo(l_module_name, 'ENTER');
1394 
1395      END IF;
1396      FND_MSG_PUB.initialize;
1397 
1398      /*
1399       * Pick up all documents for this payment request.
1400       */
1401      OPEN  c_docs(p_payreq_id);
1402      FETCH c_docs BULK COLLECT INTO l_docs_list;
1403      CLOSE c_docs;
1404 
1405      IF (l_docs_list.COUNT <> 0) THEN
1406 
1407          /*
1408           * Separate out the payment ids and the payment statuses.
1409           * This is because the rejection API expects these as
1410           * separate arrays.
1411           */
1412          FOR i IN l_docs_list.FIRST .. l_docs_list.LAST LOOP
1413              l_doc_id_list(i) := l_docs_list(i).doc_id;
1414          END LOOP;
1415 
1416          FOR i IN l_docs_list.FIRST .. l_docs_list.LAST LOOP
1417              l_doc_status_list(i) := l_docs_list(i).doc_status;
1418          END LOOP;
1419 
1420          /*
1421           * Now, remove all the documents of this request.
1422           */
1423          remove_documents_payable (
1424              l_doc_id_list,
1425              l_doc_status_list,
1426              x_return_status
1427              );
1428 
1429          /*
1430           * Check if the call to remove the documents succeeded.
1431           */
1432          IF (x_return_status IS NULL OR
1433 	     x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1434 
1435              /*
1436               * Even if a single call to remove a document
1437               * failed, return failure for the entire API request.
1438               */
1439              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1440 	             print_debuginfo(l_module_name, 'Removing of documents '
1441 	                 || 'for payment request '
1442 	                 || p_payreq_id
1443 	                 || ' failed.'
1444 	                 );
1445 
1446 	             print_debuginfo(l_module_name, 'EXIT');
1447 
1448              END IF;
1449              /*
1450               * It is the callers responsibility to rollback
1451               * all the changes.
1452               */
1453              RETURN;
1454 
1455          END IF;
1456 
1457      ELSE
1458 
1459          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1460 	         print_debuginfo(l_module_name, 'No documents were found for '
1461 	             || 'payment request id '
1462 	             || p_payreq_id
1463 	             || '. Skipping ..'
1464 	             );
1465 
1466          END IF;
1467      END IF;
1468 
1469      x_return_status := FND_API.G_RET_STS_SUCCESS;
1470 
1471      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1472 	     print_debuginfo(l_module_name, 'Returning success response ..');
1473 
1474 	     print_debuginfo(l_module_name, 'EXIT');
1475 
1476      END IF;
1477  END remove_payment_request;
1478 
1479 /*--------------------------------------------------------------------
1480  | NAME:
1481  |     stop_payment
1482  |
1483  |
1484  | PURPOSE:
1485  |     Invokes a callout of the calling app to remove a set of
1486  |     submitted documents payable from the payment processing
1487  |     cycle.
1488  |
1489  |     The calling application can free up the removed documents, and
1490  |     make them available for submission in future payment request(s).
1491  |
1492  | PARAMETERS:
1493  |     IN
1494  |       p_pmt_id          - ID of the payment to be stopped.
1495  |
1496  |       p_pmt_status_list - Current status of the payment to
1497  |                           be stopped.
1498  |
1499  |     OUT
1500  |       x_return_status   - Result of the API call:
1501  |                         FND_API.G_RET_STS_SUCCESS indicates that the
1502  |                           callout was invoked successfully.
1503  |                           In this case the caller must COMMIT
1504  |                           the status changes.
1505  |
1506  |                         FND_API.G_RET_STS_ERROR (or other) indicates
1507  |                           that tha API did not complete successfully.
1508  |                           In this case, the caller must issue a
1509  |                           ROLLBACK to undo all status changes.
1510  |
1511  | RETURNS:
1512  |
1513  | NOTES:
1514  |   Internal API, not for public use.
1515  |
1516  |   This API will not do a COMMIT. It is the the callers responsibility
1517  |   to perform COMMIT / ROLLBACK depending upon the returned status.
1518  |
1519  |   The callouts invoked must be handled synchronously by the
1520  |   calling application. So the COMMIT / ROLLBACK should affect
1521  |   the changes made to the database by the calling application as
1522  |   well. This will ensure that IBY and the calling application are
1523  |   in sync w.r.t. to the specified documents payable.
1524  |
1525  *---------------------------------------------------------------------*/
1526  PROCEDURE stop_payment (
1527      p_pmt_id         IN NUMBER,
1528      p_pmt_status     IN VARCHAR2,
1529      x_return_status  OUT NOCOPY VARCHAR2
1530      )
1531  IS
1532 
1533 l_module_name    CONSTANT VARCHAR2(200) := G_PKG_NAME ||'.stop_payment ';
1534 
1535  l_rejection_id   NUMBER(15);
1536 
1537  /* used in forming callout procedure name */
1538  l_calling_app_id NUMBER;
1539  l_app_short_name VARCHAR2(200);
1540  l_pkg_name       VARCHAR2(200);
1541  l_callout_name   VARCHAR2(500);
1542  l_stmt           VARCHAR2(1000);
1543 
1544  /* used in invocation of callout procedure */
1545  l_api_version    CONSTANT NUMBER := 1.0;
1546  l_msg_count      NUMBER;
1547  l_msg_data       VARCHAR2(2000);
1548 
1549 
1550 l_instr_id       IBY_PAY_INSTRUCTIONS_ALL.payment_instruction_id%TYPE;
1551 l_valid_pmts_count NUMBER;
1552 
1553  /*
1554   * Implementing the callout is optional for the calling app.
1555   * If the calling app does not implement the hook, then
1556   * the call to the hook will result in ORA-06576 error.
1557   *
1558   * There is no exception name associated with this code, so
1559   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
1560   * exception occurs, it is not fatal: we log the error and
1561   * proceed.
1562   *
1563   * If, on the other hand, the calling app implements the
1564   * callout, but the callout throws an exception, it is fatal
1565   * and we must abort the program (this will be caught
1566   * in WHEN OTHERS block).
1567   */
1568  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
1569  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
1570 
1571  BEGIN
1572 
1573      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1574 	     print_debuginfo(l_module_name, 'ENTER');
1575 
1576      END IF;
1577      FND_MSG_PUB.initialize;
1578 
1579      /*
1580       * STATUS CHANGE:
1581       *
1582       * UI Responsibility:
1583       * payment status = REMOVED_PAYMENT_STOPPED
1584       *
1585       * API Responsibility:
1586       * document_status = REMOVED_PAYMENT_STOPPED
1587       */
1588      UPDATE
1589          IBY_DOCS_PAYABLE_ALL
1590      SET
1591          document_status = DOC_STATUS_PMT_STOPPED
1592      WHERE
1593          payment_id = p_pmt_id
1594      ;
1595 
1596      /*
1597       * Get the next available rejected document group id.
1598       */
1599      SELECT
1600          IBY_REJECTED_DOCS_GROUP_S.NEXTVAL
1601      INTO
1602          l_rejection_id
1603      FROM
1604          DUAL
1605      ;
1606 
1607      /*
1608       * Update the removed documents with the rejected document
1609       * group id. The calling application will identify rejected
1610       * documents using this id.
1611       */
1612      UPDATE
1613          IBY_DOCS_PAYABLE_ALL
1614      SET
1615          rejected_docs_group_id = l_rejection_id
1616      WHERE
1617          payment_id = p_pmt_id
1618      ;
1619 
1620 /* Bug 6609931 */
1621 BEGIN
1622 	SELECT
1623 	   instr.payment_instruction_id
1624 	INTO
1625 	   l_instr_id
1626 	FROM
1627 	  IBY_PAY_INSTRUCTIONS_ALL instr,
1628 	  IBY_PAYMENTS_ALL pmt
1629 	WHERE
1630 	  instr.payment_instruction_id = pmt.payment_instruction_id AND
1631 	  pmt.payment_id = p_pmt_id;
1632    EXCEPTION
1633      WHEN NO_DATA_FOUND THEN
1634            l_instr_id:=null;
1635  END;
1636 
1637 /* Bug 7028817*/
1638 /* If the payment is stopped before instruction is created,
1639 should by pass the below logic.
1640 So, having condition based on the instruction id*/
1641 
1642 IF  l_instr_id IS NOT NULL THEN
1643 
1644 	-- get the count of valid payments- which are not stopped, voided, removed
1645 	-- and overfloW/setup payment- these will not have payment reference number
1646 	   SELECT
1647 	       count(*)
1648 	  INTO
1649 	      l_valid_pmts_count
1650 	  FROM
1651 	      IBY_PAYMENTS_ALL pmt
1652 	  WHERE
1653 	    pmt.payment_instruction_id = l_instr_id AND
1654 	    pmt.payment_reference_number is not null AND
1655 	    pmt.payment_status NOT IN
1656 	      (PAY_STATUS_VOID, PAY_STATUS_REMOVED, PAY_STATUS_STOPPED, PAY_STATUS_REJECTED);
1657 
1658 
1659 	print_debuginfo(l_module_name , 'The number of valid payments : ' ||
1660 	l_valid_pmts_count);
1661 
1662 
1663 	  IF (l_valid_pmts_count = 0) THEN
1664 		/*
1665 		 * * Set the payment instruction status to TERMINATED
1666 		 * * because no valid payments now exist for this
1667 		 * * payment instruction.
1668 		 * */
1669 
1670 		UPDATE
1671 		     IBY_PAY_INSTRUCTIONS_ALL
1672 		SET
1673 		    payment_instruction_status = INS_STATUS_TERMINATED
1674 		WHERE
1675 		    payment_instruction_id = l_instr_id;
1676 
1677 			/* Also since the Payment Instruction is terminated
1678 			 * we should be unlocking the payment document which this
1679 			 * instruction may be locking to make it available for other
1680 			 * Done per bug 6852606
1681 			 */
1682 			 print_debuginfo(l_module_name, 'Trying to unlock the payment document as PI termination: ');
1683 			 UPDATE
1684 			     CE_PAYMENT_DOCUMENTS
1685 			 SET
1686 			     payment_instruction_id = NULL,
1687 			     /* Bug 6707369
1688 			      * If some of the documents are skipped, the payment
1689 			      * document's last issued check number must be updated
1690 			      */
1691 			     last_issued_document_number = nvl(
1692 				     (SELECT MAX(pmt.paper_document_number)
1693 				      FROM iby_payments_all pmt
1694 				      WHERE pmt.payment_instruction_id = l_instr_id)
1695 				      ,last_issued_document_number
1696 				      )
1697 			 WHERE
1698 			     payment_instruction_id = l_instr_id;
1699 			 print_debuginfo(l_module_name, 'Payment document unlocking successful for PI : ' || l_instr_id);
1700 
1701 	  END IF;
1702 
1703  END IF;
1704  /* ending if for the condition based on instruction id(Bug 7028817)*/
1705 
1706 
1707      /*
1708       * Get the application name of the calling app. This
1709       * will be used in the callout.
1710       */
1711      SELECT
1712          fnd.application_short_name
1713      INTO
1714          l_app_short_name
1715      FROM
1716          FND_APPLICATION           fnd,
1717          IBY_PAYMENTS_ALL          pmt,
1718          IBY_PAY_SERVICE_REQUESTS  req
1719      WHERE
1720          fnd.application_id             = req.calling_app_id             AND
1721          req.payment_service_request_id = pmt.payment_service_request_id AND
1722          pmt.payment_id                 = p_pmt_id
1723      ;
1724 
1725      /*
1726       * Get the constructed package name to use in the
1727       * call out.
1728       */
1729      l_pkg_name := construct_callout_pkg_name(l_app_short_name);
1730 
1731      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1732 	     print_debuginfo(l_module_name, 'Constructed package name: '
1733 	         || l_pkg_name);
1734 
1735      END IF;
1736      IF (l_pkg_name IS NULL) THEN
1737 
1738 
1739 	         print_debuginfo(l_module_name, 'Package name is null. '
1740 	             || 'Raising exception.');
1741 
1742 
1743          APP_EXCEPTION.RAISE_EXCEPTION;
1744 
1745      END IF;
1746 
1747      /*
1748       * Now try to call the external app's implementation of the hook.
1749       * The calling app may or may not have implemented the hook, so
1750       * it's not fatal if the implementation does not exist.
1751       */
1752      l_callout_name := l_pkg_name || '.' || 'documents_payable_rejected';
1753 
1754      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1755 	     print_debuginfo(l_module_name, 'Attempting to invoke callout: '
1756 	         || l_callout_name);
1757 
1758      END IF;
1759      l_stmt := 'CALL '|| l_callout_name || '(:1, :2, :3, :4, :5, :6, :7)';
1760 
1761      BEGIN
1762 
1763          EXECUTE IMMEDIATE
1764              (l_stmt)
1765          USING
1766              IN  l_api_version,
1767              IN  FND_API.G_FALSE,
1768              IN  FND_API.G_FALSE,
1769              OUT x_return_status,
1770              OUT l_msg_count,
1771              OUT l_msg_data,
1772              IN  l_rejection_id
1773          ;
1774 
1775          /*
1776           * If the called procedure did not return success,
1777           * raise an exception.
1778           */
1779          IF (x_return_status IS NULL OR
1780              x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1781 
1782 
1783 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
1784 	                 || l_callout_name
1785 	                 || ''', returned failure status - '
1786 	                 || x_return_status
1787 	                 || '. Raising exception.'
1788 	                 );
1789 
1790 
1791              APP_EXCEPTION.RAISE_EXCEPTION;
1792 
1793          END IF;
1794 
1795      EXCEPTION
1796 
1797          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
1798              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1799 	             print_debuginfo(l_module_name, 'Callout "' || l_callout_name
1800 	                 || '" not implemented by calling app '
1801 	                 || l_app_short_name || '.');
1802 
1803 	             print_debuginfo(l_module_name, 'Skipping hook call.');
1804 
1805              END IF;
1806          WHEN OTHERS THEN
1807 
1808 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
1809 	                 || l_callout_name
1810 	                 || ''', generated exception.'
1811 	                 );
1812 
1813 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
1814 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
1815 
1816              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
1817 
1818              /*
1819               * Propogate exception to caller.
1820               */
1821              RAISE;
1822      END;
1823 
1824      x_return_status := FND_API.G_RET_STS_SUCCESS;
1825 
1826      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1827 	     print_debuginfo(l_module_name, 'EXIT');
1828 
1829      END IF;
1830  EXCEPTION
1831 
1832      WHEN OTHERS THEN
1833 
1834          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1835 	         print_debuginfo(l_module_name, 'Exception occured when '
1836 	             || 'stopping payment '
1837 	             || p_pmt_id
1838 	             || ', with status '
1839 	             || p_pmt_status
1840 	             );
1841 
1842 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
1843 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
1844          END IF;
1845          FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
1846 
1847          x_return_status := FND_API.G_RET_STS_ERROR;
1848 
1849          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1850 	         print_debuginfo(l_module_name, 'EXIT');
1851 
1852          END IF;
1853  END stop_payment;
1854 
1855 /*--------------------------------------------------------------------
1856  | NAME:
1857  |     stop_payments
1858  |
1859  | PURPOSE:
1860  |     Invokes a series of callouts of the calling app to remove
1861  |     a set of submitted documents payable from the payment processing
1862  |     cycle.
1863  |
1864  |     The calling application can free up the removed documents, and
1865  |     make them available for submission in future payment request(s).
1866  |
1867  | PARAMETERS:
1868  |     IN
1869  |       p_pmt_list        - IDs of the payments to be stopped.
1870  |                           This should be an array of payment ids. All
1871  |                           the child documents payable of each of the
1872  |                           specified payments will be removed.
1873  |
1874  |       p_pmt_status_list - Current statuses of the payments to
1875  |                           be stopped. This should be an array of statuses.
1876  |
1877  |     OUT
1878  |       x_return_status   - Result of the API call:
1879  |                         FND_API.G_RET_STS_SUCCESS indicates that all
1880  |                           the callouts were invoked successfully.
1881  |                           In this case the caller must COMMIT
1882  |                           the status changes.
1883  |
1884  |                         FND_API.G_RET_STS_ERROR (or other) indicates
1885  |                           that at least one event did not complete
1886  |                           successfully. In this case, the caller must
1887  |                           issue a ROLLBACK to undo all status changes.
1888  |
1889  | RETURNS:
1890  |
1891  | NOTES:
1892  |   Internal API, not for public use.
1893  |
1894  |   This API will not do a COMMIT. It is the the callers responsbility
1895  |   to perform COMMIT / ROLLBACK depending upon the returned status.
1896  |
1897  |   The callouts invoked must be handled synchronously by the
1898  |   calling application. So the COMMIT / ROLLBACK should affect
1899  |   the changes made to the database by the calling application as
1900  |   well. This will ensure that IBY and the calling application are
1901  |   in sync w.r.t. to the specified documents payable.
1902  |
1903  *---------------------------------------------------------------------*/
1904  PROCEDURE stop_payments (
1905      p_pmt_list         IN pmtIDTab,
1906      p_pmt_status_list  IN pmtStatusTab,
1907      x_return_status    OUT NOCOPY VARCHAR2
1908      )
1909  IS
1910 
1911  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME || '.stop_payments';
1912 
1913  BEGIN
1914 
1915      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1916 	     print_debuginfo(l_module_name, 'ENTER');
1917 
1918      END IF;
1919      FND_MSG_PUB.initialize;
1920 
1921      IF (p_pmt_list.COUNT = 0 OR p_pmt_status_list.COUNT = 0) THEN
1922 
1923          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1924 	         print_debuginfo(l_module_name, 'Error: List of payment '
1925 	             || 'payable ids/statuses is empty'
1926 	             || '. Returning failure response .. '
1927 	             );
1928 
1929          END IF;
1930          x_return_status := FND_API.G_RET_STS_ERROR;
1931 
1932          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1933 	         print_debuginfo(l_module_name, 'EXIT');
1934 
1935          END IF;
1936          RETURN;
1937 
1938      END IF;
1939 
1940      IF (p_pmt_list.COUNT <> p_pmt_status_list.COUNT) THEN
1941 
1942          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1943 	         print_debuginfo(l_module_name, 'Error: List of payment '
1944 	             || 'ids must match list of payment statuses. '
1945 	             || 'Returning failure response .. '
1946 	             );
1947 
1948          END IF;
1949          x_return_status := FND_API.G_RET_STS_ERROR;
1950 
1951          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1952 	         print_debuginfo(l_module_name, 'EXIT');
1953 
1954          END IF;
1955          RETURN;
1956 
1957      END IF;
1958 
1959      /*
1960       * Start processing the payments, one-by-one.
1961       */
1962      FOR i IN p_pmt_list.FIRST .. p_pmt_list.LAST LOOP
1963 
1964          stop_payment (
1965              p_pmt_list(i),
1966              p_pmt_status_list(i),
1967              x_return_status
1968              );
1969 
1970          /*
1971           * Check if the call to stop the payment succeeded.
1972           */
1973          IF (x_return_status IS NULL OR
1974 	     x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1975 
1976              /*
1977               * Even if a single call to stop a payment
1978               * failed, return failure for the entire API request.
1979               */
1980              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1981 	             print_debuginfo(l_module_name, 'Stopping of payment '
1982 	                 || p_pmt_list(i)
1983 	                 || ' failed.'
1984 	                 );
1985 
1986 	             print_debuginfo(l_module_name, 'EXIT');
1987 
1988              END IF;
1989              /*
1990               * It is the callers responsibility to rollback
1991               * all the changes.
1992               */
1993              RETURN;
1994 
1995          END IF;
1996 
1997      END LOOP;
1998 
1999      x_return_status := FND_API.G_RET_STS_SUCCESS;
2000 
2001      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2002 	     print_debuginfo(l_module_name, 'Returning success response ..');
2003 
2004 	     print_debuginfo(l_module_name, 'EXIT');
2005 
2006      END IF;
2007  END stop_payments;
2008 
2009 /*--------------------------------------------------------------------
2010  | NAME:
2011  |     void_payment
2012  |
2013  | PURPOSE:
2014  |     Sets the status of the specified payment id to VOID in the IBY
2015  |     tables. This API is used in the AP void and reissue flow to keep
2016  |     IBY and AP tables in sync.
2017  |
2018  |     This API is meant for AP only and should not be used by the IBY UI.
2019  |     There is a separate API for voiding payments from the IBY UI - see
2020  |     void_pmt_internal(..) method.
2021  |
2022  | PARAMETERS:
2023  |     IN
2024  |       p_api_version   - Version of the API.
2025  |
2026  |       p_init_msg_list - Standard API parameter; default as FND_API.G_FALSE
2027  |
2028  |       p_pmt_id        - ID of the payment to be voided. This id will map
2029  |                         to IBY_PAYMENTS_ALL.PAYMENT_ID column.
2030  |
2031  |       p_voided_by     - User id of person who issued the void request.
2032  |                         This id will be stored as an attribute of the
2033  |                         payment.
2034  |
2035  |       p_void_date     - Date on which the void request was made.
2036  |
2037  |       p_void_reason   - Reason why this payment needs to be voided.
2038  |
2039  |     OUT
2040  |       x_return_status - Standard return status. Possible values are:
2041  |                         FND_API.G_RET_STS_SUCCESS
2042  |                           API completed successfully. Caller must
2043  |                           now COMMIT the database changes.
2044  |
2045  |                         FND_API.G_RET_STS_ERROR
2046  |                           API call failed. Caller must ROLLBACK any
2047  |                           base changes.
2048  |
2049  |       x_msg_count     - Standard msg count
2050  |
2051  |       x_msg_data      - Standard msg data
2052  |
2053  | RETURNS:
2054  |
2055  | NOTES:
2056  |  Public API.
2057  |
2058  |  This API will not do a COMMIT. It is the the callers responsbility
2059  |  to perform COMMIT / ROLLBACK depending upon the returned status.
2060  |
2061  *---------------------------------------------------------------------*/
2062  PROCEDURE void_payment (
2063      p_api_version    IN NUMBER,
2064      p_init_msg_list  IN VARCHAR2 DEFAULT FND_API.G_FALSE,
2065      p_pmt_id         IN NUMBER,
2066      p_voided_by      IN NUMBER,
2067      p_void_date      IN DATE,
2068      p_void_reason    IN VARCHAR2,
2069      x_return_status  OUT NOCOPY VARCHAR2,
2070      x_msg_count      OUT NOCOPY NUMBER,
2071      x_msg_data       OUT NOCOPY VARCHAR2
2072      )
2073  IS
2074 
2075  l_module_name   CONSTANT VARCHAR2(200) := G_PKG_NAME || '.void_payment';
2076  l_calling_app_id NUMBER;
2077 
2078  l_api_version   CONSTANT NUMBER        := 1.0;
2079  l_api_name      CONSTANT VARCHAR2(100) := 'void_payment';
2080 
2081  l_instr_id       IBY_PAY_INSTRUCTIONS_ALL.payment_instruction_id%TYPE;
2082  l_valid_pmts_count
2083                   NUMBER;
2084 
2085  BEGIN
2086 
2087      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2088 	     print_debuginfo(l_module_name, 'ENTER');
2089 
2090 	     print_debuginfo(l_module_name, 'Parameters passed - '
2091 	         || 'payment id: '
2092 	         || p_pmt_id
2093 	         || ', voided by: '
2094 	         || p_voided_by
2095 	         || ', void date: '
2096 	         || p_void_date
2097 	         || ', void reason: '
2098 	         || p_void_reason
2099 	         );
2100 
2101      END IF;
2102      /* Standard call to check for API compatibility */
2103      IF NOT FND_API.Compatible_API_Call(
2104                         l_api_version,
2105                         p_api_version,
2106                         l_api_name,
2107                         G_PKG_NAME) THEN
2108 
2109          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2110 
2111      END IF;
2112 
2113      /* Initialize message list if p_init_msg_list is set to TRUE */
2114      IF FND_API.to_boolean(p_init_msg_list) THEN
2115          FND_MSG_PUB.initialize;
2116      END IF;
2117 
2118      /* Initialize return status */
2119      x_return_status := FND_API.G_RET_STS_SUCCESS;
2120 
2121      /*
2122       * STEP I:
2123       *
2124       * Void the specified payment.
2125       */
2126      UPDATE
2127          IBY_PAYMENTS_ALL
2128      SET
2129          payment_status = PAY_STATUS_VOID,
2130          voided_by      = p_voided_by,
2131          void_date      = p_void_date,
2132          void_reason    = p_void_reason,
2133          last_update_date = sysdate,
2134          last_updated_by = NVL(FND_GLOBAL.user_id,-1),
2135          last_update_login = NVL(FND_GLOBAL.login_id, -1),
2136 	 positive_pay_file_created_flag = NULL
2137      WHERE
2138          payment_id = p_pmt_id
2139      ;
2140 
2141      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2142 	     print_debuginfo(l_module_name, 'Payment '
2143 	         || p_pmt_id
2144 	         || ' set to void status ..'
2145 	         );
2146 
2147      END IF;
2148      /*
2149       * STEP II:
2150       *
2151       * Void the documents linked to this payment.
2152       */
2153      UPDATE
2154          IBY_DOCS_PAYABLE_ALL
2155      SET
2156          document_status = DOC_STATUS_PMT_VOIDED,
2157          last_update_date = sysdate,
2158          last_updated_by = NVL(FND_GLOBAL.user_id,-1),
2159          last_update_login = NVL(FND_GLOBAL.login_id, -1)
2160      WHERE
2161          payment_id = p_pmt_id
2162      ;
2163 
2164      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2165 	     print_debuginfo(l_module_name, 'Documents of payment '
2166 	         || p_pmt_id
2167 	         || ' set to void status ..'
2168 	         );
2169 
2170      END IF;
2171      /*
2172       * STEP III:
2173       *
2174       * Fix for bug 5017119:
2175       *
2176       * After voiding this payment, check the payment instruction
2177       * to see whether any non-voided / non-removed payments
2178       * exist for this instruction.
2179       *
2180       * If this payment was the last valid payment on the instruction,
2181       * and we just voided it, then set the status of the instruction
2182       * to TERMINATED to signify that this is the final status for
2183       * the instruction.
2184       */
2185 
2186      /*
2187       * Fix for bug 5108035:
2188       *
2189       * Don't assume that every payment to be voided will have an
2190       * associated payment instruction id; a manual payment will
2191       * not have a payment instruction id for example.
2192       *
2193       *
2194       * Therefore, if we run into an exception when attempting to
2195       * get the payment instruction of the payment to be voided,
2196       * treat it as non-fatal.
2197       */
2198      BEGIN
2199 
2200          SELECT
2201              instr.payment_instruction_id
2202          INTO
2203              l_instr_id
2204          FROM
2205              IBY_PAY_INSTRUCTIONS_ALL instr,
2206              IBY_PAYMENTS_ALL         pmt
2207          WHERE
2208              instr.payment_instruction_id = pmt.payment_instruction_id AND
2209              pmt.payment_id = p_pmt_id
2210          ;
2211 
2212          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2213 	         print_debuginfo(l_module_name, 'Provided payment '
2214 	             || p_pmt_id
2215 	             || ' has payment instruction id '
2216 	             || l_instr_id
2217 	             || '. Checking this instruction for any remaining '
2218 	             || 'valid payments ..'
2219 	             );
2220 
2221          END IF;
2222          SELECT
2223              count(*)
2224          INTO
2225              l_valid_pmts_count
2226          FROM
2227              IBY_PAYMENTS_ALL pmt
2228          WHERE
2229              pmt.payment_instruction_id = l_instr_id AND
2230              pmt.payment_status NOT IN
2231                  (
2232                   PAY_STATUS_VOID,
2233                   PAY_STATUS_REMOVED
2234                  )
2235          ;
2236 
2237          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2238 	         print_debuginfo(l_module_name, 'Number of remaining valid payments '
2239 	             || 'for pmt instruction '
2240 	             || l_instr_id
2241 	             || ' is '
2242 	             || l_valid_pmts_count
2243 	             );
2244 
2245          END IF;
2246          IF (l_valid_pmts_count = 0) THEN
2247 
2248              /*
2249               * Set the payment instruction status to TERMINATED
2250               * because no valid payments now exist for this
2251               * payment instruction.
2252               */
2253              UPDATE
2254                  IBY_PAY_INSTRUCTIONS_ALL
2255              SET
2256                  payment_instruction_status = INS_STATUS_TERMINATED,
2257                  last_update_date = sysdate,
2258                  last_updated_by = NVL(FND_GLOBAL.user_id,-1),
2259                  last_update_login = NVL(FND_GLOBAL.login_id, -1)
2260              WHERE
2261                  payment_instruction_id = l_instr_id
2262              ;
2263 
2264              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2265 	             print_debuginfo(l_module_name, 'Status of payment instruction '
2266 	                 || l_instr_id
2267 	                 || ' updated to TERMINATED because it has no remaining '
2268 	                 || 'valid payments.'
2269 	                 );
2270 
2271              END IF;
2272 	/* Also since the Payment Instruction is terminated
2273 	 * we should be unlocking the payment document which this
2274 	 * instruction may be locking to make it available for other
2275 	 * Done per bug 6852606
2276 	 */
2277          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2278 	         print_debuginfo(l_module_name, 'Trying to unlock the payment document as PI termination: ');
2279          END IF;
2280 	 UPDATE
2281 	     CE_PAYMENT_DOCUMENTS
2282 	 SET
2283 	     payment_instruction_id = NULL,
2284 	     /* Bug 6707369
2285 	      * If some of the documents are skipped, the payment
2286 	      * document's last issued check number must be updated
2287 	      */
2288 	     last_issued_document_number = nvl(
2289 		     (SELECT MAX(pmt.paper_document_number)
2290 		      FROM iby_payments_all pmt
2291 		      WHERE pmt.payment_instruction_id = l_instr_id)
2292 		      ,last_issued_document_number
2293 		      )
2294 	 WHERE
2295 	     payment_instruction_id = l_instr_id;
2296          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2297 	         print_debuginfo(l_module_name, 'Payment document unlocking successful for PI : ' || l_instr_id);
2298 
2299          END IF;
2300          END IF;
2301 
2302      EXCEPTION
2303 
2304          WHEN OTHERS THEN
2305 
2306              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2307 	             print_debuginfo(l_module_name, 'Non-fatal: Exception occured '
2308 	                 || 'when attempting to get parent payment instruction of '
2309 	                 || 'payment '
2310 	                 || p_pmt_id
2311 	                 );
2312 
2313 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
2314 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
2315 
2316              END IF;
2317      END;
2318 
2319      /*
2320       * Standard call to get message count and if count is 1, get
2321       * message info.
2322       */
2323      FND_MSG_PUB.Count_And_Get(
2324                      p_encoded => FND_API.G_FALSE,
2325                      p_count   => x_msg_count,
2326                      p_data    => x_msg_data
2327                      );
2328 
2329      x_return_status := FND_API.G_RET_STS_SUCCESS;
2330      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2331 	     print_debuginfo(l_module_name, 'Returning success response ..');
2332 
2333 	     print_debuginfo(l_module_name, 'EXIT');
2334 
2335      END IF;
2336      EXCEPTION
2337 
2338          WHEN OTHERS THEN
2339 
2340              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2341 	             print_debuginfo(l_module_name, 'Exception occured when '
2342 	                 || 'attempting to void payment id '
2343 	                 || p_pmt_id
2344 	                 );
2345 
2346 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
2347 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
2348              END IF;
2349              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
2350 
2351              x_return_status := FND_API.G_RET_STS_ERROR;
2352 
2353              FND_MSG_PUB.Count_And_Get(
2354                              p_encoded => FND_API.G_FALSE,
2355                              p_count   => x_msg_count,
2356                              p_data    => x_msg_data
2357                              );
2358 
2359              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2360 	             print_debuginfo(l_module_name, 'Returning error response ..');
2361 	             print_debuginfo(l_module_name, 'EXIT');
2362 
2363              END IF;
2364              RETURN;
2365 
2366  END void_payment;
2367 
2368 /*--------------------------------------------------------------------
2369  | NAME:
2370  |     void_pmt_internal
2371  |
2372  | PURPOSE:
2373  |     Sets the status of the specified payment id to VOID in the IBY
2374  |     tables. This API is used in the IBY UI to void a payment.
2375  |
2376  |     This API is meant for IBY only and should not be used by AP.
2377  |     There is a separate public API for voiding payments from AP - see
2378  |     void_payment(..) method.
2379  |
2380  | PARAMETERS:
2381  |     IN
2382  |       p_api_version   - Version of the API.
2383  |
2384  |       p_init_msg_list - Standard API parameter; default as FND_API.G_FALSE
2385  |
2386  |       p_pmt_id        - ID of the payment to be voided. This id will map
2387  |                         to IBY_PAYMENTS_ALL.PAYMENT_ID column.
2388  |
2389  |       p_voided_by     - User id of person who issued the void request.
2390  |                         This id will be stored as an attribute of the
2391  |                         payment.
2392  |
2393  |       p_void_date     - Date on which the void request was made.
2394  |
2395  |       p_void_reason   - Reason why this payment needs to be voided.
2396  |
2397  |     OUT
2398  |       x_return_status - Standard return status. Possible values are:
2399  |                         FND_API.G_RET_STS_SUCCESS
2400  |                           API completed successfully. Caller must
2401  |                           now COMMIT the database changes.
2402  |
2403  |                         FND_API.G_RET_STS_ERROR
2404  |                           API call failed. Caller must ROLLBACK any
2405  |                           base changes.
2406  |
2407  |       x_msg_count     - Standard msg count
2408  |
2409  |       x_msg_data      - Standard msg data
2410  |
2411  | RETURNS:
2412  |
2413  | NOTES:
2414  |  Internal API, not for public use.
2415  |
2416  |  This API will not do a COMMIT. It is the the callers responsbility
2417  |  to perform COMMIT / ROLLBACK depending upon the returned status.
2418  |
2419  *---------------------------------------------------------------------*/
2420  PROCEDURE void_pmt_internal (
2421      p_api_version    IN NUMBER,
2422      p_init_msg_list  IN VARCHAR2 DEFAULT FND_API.G_FALSE,
2423      p_pmt_id         IN NUMBER,
2424      p_voided_by      IN NUMBER,
2425      p_void_date      IN DATE,
2426      p_void_reason    IN VARCHAR2,
2427      x_return_status  OUT NOCOPY VARCHAR2,
2428      x_msg_count      OUT NOCOPY NUMBER,
2429      x_msg_data       OUT NOCOPY VARCHAR2
2430      )
2431  IS
2432 
2433  l_module_name   CONSTANT VARCHAR2(200) := G_PKG_NAME || '.void_pmt_internal';
2434  l_calling_app_id NUMBER;
2435 
2436  l_api_version   CONSTANT NUMBER        := 1.0;
2437  l_api_name      CONSTANT VARCHAR2(100) := 'void_pmt_internal';
2438 
2439  /* used in forming callout procedure name */
2440  l_app_short_name VARCHAR2(200);
2441  l_pkg_name       VARCHAR2(200);
2442  l_callout_name   VARCHAR2(500);
2443  l_stmt           VARCHAR2(1000);
2444 
2445  /* used in invocation of callout procedure */
2446  l_msg_count      NUMBER;
2447  l_msg_data       VARCHAR2(2000);
2448 
2449  l_ret_flag       VARCHAR2(1) := 'Y';
2450 
2451  l_test           VARCHAR2(2000);
2452  l_org            NUMBER(15);
2453 
2454  l_instr_id       IBY_PAY_INSTRUCTIONS_ALL.payment_instruction_id%TYPE;
2455 
2456  l_valid_pmts_count NUMBER;
2457  l_curr_pmt_status  VARCHAR2(200);
2458 
2459  /*
2460   * Implementing the callout is optional for the calling app.
2461   * If the calling app does not implement the hook, then
2462   * the call to the hook will result in ORA-06576 error.
2463   *
2464   * There is no exception name associated with this code, so
2465   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
2466   * exception occurs, it is not fatal: we log the error and
2467   * proceed.
2468   *
2469   * If, on the other hand, the calling app implements the
2470   * callout, but the callout throws an exception, it is fatal
2471   * and we must abort the program (this will be caught
2472   * in WHEN OTHERS block).
2473   */
2474  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
2475  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
2476 
2477  BEGIN
2478 
2479      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2480 	     print_debuginfo(l_module_name, 'ENTER');
2481 
2482      END IF;
2483      /*
2484       * Set the apps context. This is necessary so that
2485       * the calling application can see the org based
2486       * tables associated with this payment.
2487       *
2488       * See bug 4945922.
2489       */
2490      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2491 	     print_debuginfo(l_module_name, 'Setting apps context .. ');
2492 
2493      END IF;
2494      fnd_global.APPS_INITIALIZE(
2495          user_id      => fnd_global.user_id,
2496          resp_id      => fnd_global.resp_id,
2497          resp_appl_id => fnd_global.resp_appl_id
2498          );
2499 
2500      mo_global.init(fnd_global.application_short_name);
2501 
2502      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2503 	     print_debuginfo(l_module_name, 'Apps context set [user: '
2504 	         || fnd_global.user_id
2505 	         || ', responsbility id: '
2506 	         || fnd_global.resp_id
2507 	         || ', responsibility application id: '
2508 	         || fnd_global.resp_appl_id
2509 	         || ']'
2510 	         );
2511 
2512 	     print_debuginfo(l_module_name, 'Apps context [app short name: '
2513 	         || fnd_global.application_short_name
2514 	         || ']'
2515 	         );
2516 
2517      END IF;
2518     /*--- RYAN TEST CODE ---*/
2519     /*---- start ----*/
2520     /*
2521     begin
2522       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2523 	      print_debuginfo(l_module_name, 'Start RYAN test');
2524       END IF;
2525       select 'row found in moac synonym'
2526       into l_test
2527       from ap_invoices
2528       where invoice_id = 10045;
2529       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2530 	      print_debuginfo(l_module_name, 'End RYAN test: l_test = ' || l_test);
2531       END IF;
2532     exception
2533       when no_data_found then
2534       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2535 	      print_debuginfo('RYAN TEST: ap_pmt_callout_pkg',
2536 	          'in no data found exception');
2537       END IF;
2538     end;
2539     */
2540     /*---- end ----*/
2541 
2542     /*--- YING TEST CODE ---*/
2543     /*---- start ----*/
2544     /*
2545     begin
2546       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2547 	      print_debuginfo(l_module_name, 'Start YING test');
2548       END IF;
2549       select organization_id
2550       into l_org
2551       from ce_security_profiles_v
2552       where organization_type='OPERATING_UNIT';
2553       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2554 	      print_debuginfo(l_module_name, 'End YING test: l_org = ' || l_org);
2555       END IF;
2556     exception
2557       when no_data_found then
2558      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2559 	      print_debuginfo('YING TEST: org id',
2560 	          'in no data found exception');
2561       END IF;
2562 
2563     end;
2564     */
2565     /*---- end ----*/
2566 
2567      /* Standard call to check for API compatibility */
2568      IF NOT FND_API.Compatible_API_Call(
2569                         l_api_version,
2570                         p_api_version,
2571                         l_api_name,
2572                         G_PKG_NAME) THEN
2573 
2574          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2575 
2576      END IF;
2577 
2578      /* Initialize message list if p_init_msg_list is set to TRUE */
2579      IF FND_API.to_boolean(p_init_msg_list) THEN
2580          FND_MSG_PUB.initialize;
2581      END IF;
2582 
2583      /* Initialize return status */
2584      x_return_status := FND_API.G_RET_STS_SUCCESS;
2585 
2586      /*
2587       * Fix for bug 5623941:
2588       *
2589       * STEP I:
2590       *
2591       * Before doing anything check if the payment is already
2592       * voided. If it is already voided, we don't need to do anything;
2593       * just exit.
2594       */
2595 
2596      /*
2597       * Get the current status of the
2598       * payment into l_curr_pmt_status
2599       */
2600      SELECT
2601          pmt.payment_status
2602      INTO
2603          l_curr_pmt_status
2604      FROM
2605          IBY_PAYMENTS_ALL pmt
2606      WHERE
2607          payment_id = p_pmt_id
2608      ;
2609 
2610      IF (l_curr_pmt_status = PAY_STATUS_VOID) THEN
2611 
2612          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2613 	         print_debuginfo(l_module_name, 'Payment '
2614 	             || p_pmt_id
2615 	             || ' is already voided. Skipping .. '
2616 	             );
2617 
2618          END IF;
2619          x_return_status := FND_API.G_RET_STS_SUCCESS;
2620 
2621          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2622 	         print_debuginfo(l_module_name, 'Returning success response ..');
2623 	         print_debuginfo(l_module_name, 'EXIT');
2624 
2625          END IF;
2626          RETURN;
2627 
2628      END IF;
2629 
2630      /*
2631       * STEP II:
2632       *
2633       * Check if the external application allows this payment
2634       * to be voided. If not there is no point in proceeding
2635       * further.
2636       */
2637      is_void_allowed (
2638          p_api_version,
2639          FND_API.G_FALSE,
2640          p_pmt_id,
2641          l_ret_flag,
2642          x_return_status,
2643          x_msg_count,
2644          x_msg_data
2645          );
2646 
2647      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2648 	     print_debuginfo(l_module_name, 'Void allowed flag '
2649 	          || 'after call to is_void_allowed() API: '
2650 	          || l_ret_flag
2651 	          );
2652 
2653      END IF;
2654      /*
2655       * If the void allowed flag is not set to 'Y',
2656       * exit the procedure with an error.
2657       */
2658      IF (UPPER(l_ret_flag) <> 'Y') THEN
2659 
2660          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2661 	         print_debuginfo(l_module_name, 'Void not allowed. '
2662 	             || 'Returning failure response.'
2663 	             );
2664 
2665          END IF;
2666          x_return_status := FND_API.G_RET_STS_ERROR;
2667 
2668          FND_MSG_PUB.Count_And_Get(
2669              p_encoded => FND_API.G_FALSE,
2670              p_count   => x_msg_count,
2671              p_data    => x_msg_data
2672              );
2673 
2674          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2675 	         print_debuginfo(l_module_name, 'EXIT');
2676 
2677          END IF;
2678          RETURN;
2679 
2680      END IF;
2681 
2682      /*
2683       * STEP III:
2684       *
2685       * Void the specified payment.
2686       */
2687      UPDATE
2688          IBY_PAYMENTS_ALL
2689      SET
2690          payment_status = PAY_STATUS_VOID,
2691          voided_by      = p_voided_by,
2692          void_date      = p_void_date,
2693          void_reason    = p_void_reason,
2694          last_update_date = sysdate,     --Bug 13999452
2695          last_updated_by = NVL(FND_GLOBAL.user_id,-1),
2696          last_update_login = NVL(FND_GLOBAL.login_id, -1),
2697 	 positive_pay_file_created_flag = NULL
2698      WHERE
2699          payment_id = p_pmt_id
2700      ;
2701 
2702      /*
2703       * STEP IV:
2704       *
2705       * Void the documents linked to this payment.
2706       */
2707      UPDATE
2708          IBY_DOCS_PAYABLE_ALL
2709      SET
2710          document_status = DOC_STATUS_PMT_VOIDED,
2711          last_update_date = sysdate,
2712          last_updated_by = NVL(FND_GLOBAL.user_id,-1),
2713          last_update_login = NVL(FND_GLOBAL.login_id, -1)
2714      WHERE
2715          payment_id = p_pmt_id
2716      ;
2717 
2718      /*
2719       * STEP V:
2720       *
2721       * Fix for bug 5017119:
2722       *
2723       * After voiding this payment, check the payment instruction
2724       * to see whether any non-voided / non-removed payments
2725       * exist for this instruction.
2726       *
2727       * If this payment was the last valid payment on the instruction,
2728       * and we just voided it, then set the status of the instruction
2729       * to TERMINATED to signify that this is the final status for
2730       * the instruction.
2731       */
2732 
2733      /*
2734       * Fix for bug 8729551:
2735       *
2736       * Don't assume that every payment to be voided will have an
2737       * associated payment instruction id; a manual payment will
2738       * not have a payment instruction id for example.
2739       *
2740       *
2741       * Therefore, if we run into an exception when attempting to
2742       * get the payment instruction of the payment to be voided,
2743       * treat it as non-fatal.
2744       */
2745 
2746      BEGIN
2747 	     SELECT
2748 		 instr.payment_instruction_id
2749 	     INTO
2750 		 l_instr_id
2751 	     FROM
2752 		 IBY_PAY_INSTRUCTIONS_ALL instr,
2753 		 IBY_PAYMENTS_ALL         pmt
2754 	     WHERE
2755 		 instr.payment_instruction_id = pmt.payment_instruction_id AND
2756 		 pmt.payment_id = p_pmt_id
2757 	     ;
2758 
2759 	     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2760 		     print_debuginfo(l_module_name, 'Provided payment '
2761 			 || p_pmt_id
2762 			 || ' has payment instruction id '
2763 			 || l_instr_id
2764 			 || '. Checking this instruction for any remaining valid payments ..'
2765 			 );
2766 
2767 	     END IF;
2768 	     SELECT
2769 		 count(*)
2770 	     INTO
2771 		 l_valid_pmts_count
2772 	     FROM
2773 		 IBY_PAYMENTS_ALL pmt
2774 	     WHERE
2775 		 pmt.payment_instruction_id = l_instr_id AND
2776 		 pmt.payment_status NOT IN
2777 		     (
2778 		      PAY_STATUS_VOID,
2779 		      PAY_STATUS_REMOVED
2780 		     )
2781 	     ;
2782 
2783 	     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2784 		     print_debuginfo(l_module_name, 'Number of remaining valid payments '
2785 			 || 'for pmt instruction '
2786 			 || l_instr_id
2787 			 || ' is '
2788 			 || l_valid_pmts_count
2789 			 );
2790 
2791 	     END IF;
2792 	     IF (l_valid_pmts_count = 0) THEN
2793 
2794 		 /*
2795 		  * Set the payment instruction status to TERMINATED
2796 		  * because no valid payments now exist for this
2797 		  * payment instruction.
2798 		  */
2799 		 UPDATE
2800 		     IBY_PAY_INSTRUCTIONS_ALL
2801 		 SET
2802              payment_instruction_status = INS_STATUS_TERMINATED,
2803              last_update_date = sysdate,
2804              last_updated_by = NVL(FND_GLOBAL.user_id,-1),
2805              last_update_login = NVL(FND_GLOBAL.login_id, -1)
2806 		 WHERE
2807 		     payment_instruction_id = l_instr_id
2808 		 ;
2809 
2810 		 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2811 			 print_debuginfo(l_module_name, 'Status of payment instruction '
2812 			     || l_instr_id
2813 			     || ' updated to TERMINATED because it has no remaining '
2814 			     || 'valid payments.'
2815 			     );
2816 
2817 		 END IF;
2818 		/* Also since the Payment Instruction is terminated
2819 		 * we should be unlocking the payment document which this
2820 		 * instruction may be locking to make it available for other
2821 		 * Done per bug 6852606
2822 		 */
2823 		 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2824 			 print_debuginfo(l_module_name, 'Trying to unlock the payment document as PI termination: ');
2825 		 END IF;
2826 		 UPDATE
2827 		     CE_PAYMENT_DOCUMENTS
2828 		 SET
2829 		     payment_instruction_id = NULL,
2830 		     /* Bug 6707369
2831 		      * If some of the documents are skipped, the payment
2832 		      * document's last issued check number must be updated
2833 		      */
2834 		     last_issued_document_number = nvl(
2835 			     (SELECT MAX(pmt.paper_document_number)
2836 			      FROM iby_payments_all pmt
2837 			      WHERE pmt.payment_instruction_id = l_instr_id)
2838 			      ,last_issued_document_number
2839 			      )
2840 		 WHERE
2841 		     payment_instruction_id = l_instr_id;
2842 		 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2843 			 print_debuginfo(l_module_name, 'Payment document unlocking successful for PI : ' || l_instr_id);
2844 
2845 
2846 		 END IF;
2847 	     END IF;
2848      EXCEPTION
2849 
2850          WHEN OTHERS THEN
2851 
2852              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2853 	             print_debuginfo(l_module_name, 'Non-fatal: Exception occured '
2854 	                 || 'when attempting to get parent payment instruction of '
2855 	                 || 'payment '
2856 	                 || p_pmt_id
2857 	                 );
2858 
2859 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
2860 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
2861 
2862              END IF;
2863      END;
2864 
2865      /*
2866       * STEP VI:
2867       *
2868       * Call external application hook to inform it of
2869       * the voided payments.
2870       */
2871 
2872      /*
2873       * Get the application name of the calling app. This
2874       * will be used in the callout.
2875       */
2876      SELECT
2877          fnd.application_short_name
2878      INTO
2879          l_app_short_name
2880      FROM
2881          FND_APPLICATION           fnd,
2882          IBY_PAYMENTS_ALL          pmt,
2883          IBY_PAY_SERVICE_REQUESTS  req
2884      WHERE
2885          fnd.application_id             = req.calling_app_id             AND
2886          req.payment_service_request_id = pmt.payment_service_request_id AND
2887          pmt.payment_id                 = p_pmt_id
2888      ;
2889 
2890      /*
2891       * Get the constructed package name to use in the
2892       * call out.
2893       */
2894      l_pkg_name := construct_callout_pkg_name(l_app_short_name);
2895 
2896      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2897 	     print_debuginfo(l_module_name, 'Constructed package name: '
2898 	         || l_pkg_name);
2899 
2900      END IF;
2901      IF (l_pkg_name IS NULL) THEN
2902 
2903 
2904 	         print_debuginfo(l_module_name, 'Package name is null. '
2905 	             || 'Raising exception.');
2906 
2907 
2908          APP_EXCEPTION.RAISE_EXCEPTION;
2909 
2910      END IF;
2911 
2912      /*
2913       * Now try to call the external app's implementation of the hook.
2914       * The calling app may or may not have implemented the hook, so
2915       * it's not fatal if the implementation does not exist.
2916       */
2917      l_callout_name := l_pkg_name || '.' || 'payment_voided';
2918 
2919      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2920 	     print_debuginfo(l_module_name, 'Attempting to invoke callout: '
2921 	         || l_callout_name);
2922 
2923      END IF;
2924      --l_stmt := 'CALL '|| l_callout_name
2925      --                     || '(:1, :2, :3, :4, :5, :6, :7, :8)';
2926 
2927      l_stmt := 'BEGIN '|| l_callout_name
2928                           || '('
2929                           || 'p_api_version   => :1, '
2930                           || 'p_payment_id    => :2, '
2931                           || 'p_void_date     => :3, '
2932                           || 'p_init_msg_list => :4, '
2933                           || 'p_commit        => :5, '
2934                           || 'x_return_status => :6, '
2935                           || 'x_msg_count     => :7, '
2936                           || 'x_msg_data      => :8  '
2937                           || '); END;'
2938                           ;
2939 
2940      BEGIN
2941 
2942          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2943 	         print_debuginfo(l_module_name, 'Params passed to callout - ');
2944 	         print_debuginfo(l_module_name, 'p_pmt_id: '    || p_pmt_id);
2945 	         print_debuginfo(l_module_name, 'p_void_date: ' || p_void_date);
2946 
2947          END IF;
2948          EXECUTE IMMEDIATE
2949              (l_stmt)
2950          USING
2951              IN  l_api_version,
2952              IN  p_pmt_id,
2953              IN  p_void_date,
2954              IN  FND_API.G_FALSE,
2955              IN  FND_API.G_FALSE,   /* commit flag */
2956              OUT x_return_status,
2957              OUT l_msg_count,
2958              OUT l_msg_data
2959          ;
2960 
2961          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2962 	         print_debuginfo(l_module_name, 'Finished invoking callout ..');
2963 	         print_debuginfo(l_module_name, 'Status returned by callout: '
2964 	             || x_return_status);
2965 
2966          END IF;
2967          /*
2968           * If the called procedure did not return success,
2969           * raise an exception.
2970           */
2971          IF (x_return_status IS NULL OR
2972              x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
2973 
2974 
2975 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
2976 	                 || l_callout_name
2977 	                 || ''', returned failure status - '
2978 	                 || x_return_status
2979 	                 || '. Raising exception.'
2980 	                 );
2981 
2982 
2983              APP_EXCEPTION.RAISE_EXCEPTION;
2984 
2985          END IF;
2986 
2987      EXCEPTION
2988 
2989          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
2990              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2991 	             print_debuginfo(l_module_name, 'Callout "' || l_callout_name
2992 	                 || '" not implemented by calling app '
2993 	                 || l_app_short_name || '.');
2994 
2995 	             print_debuginfo(l_module_name, 'Skipping hook call.');
2996 
2997              END IF;
2998          WHEN OTHERS THEN
2999 
3000 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
3001 	                 || l_callout_name
3002 	                 || ''', generated exception.'
3003 	                 );
3004 
3005 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
3006 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
3007 
3008              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
3009 
3010              /*
3011               * Propogate exception to caller.
3012               */
3013              RAISE;
3014      END;
3015 
3016      /*
3017       * Standard call to get message count and if count is 1, get
3018       * message info.
3019       */
3020      FND_MSG_PUB.Count_And_Get(
3021                      p_encoded => FND_API.G_FALSE,
3022                      p_count   => x_msg_count,
3023                      p_data    => x_msg_data
3024                      );
3025 
3026      x_return_status := FND_API.G_RET_STS_SUCCESS;
3027      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3028 	     print_debuginfo(l_module_name, 'Returning success response ..');
3029 
3030 	     print_debuginfo(l_module_name, 'EXIT');
3031 
3032      END IF;
3033  EXCEPTION
3034      WHEN OTHERS THEN
3035 
3036          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3037 	         print_debuginfo(l_module_name, 'Exception occured when '
3038 	             || 'attempting to void payment id '
3039 	             || p_pmt_id
3040 	             );
3041 
3042 	         print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
3043 	         print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
3044          END IF;
3045          FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
3046 
3047          x_return_status := FND_API.G_RET_STS_ERROR;
3048 
3049          FND_MSG_PUB.Count_And_Get(
3050              p_encoded => FND_API.G_FALSE,
3051              p_count   => x_msg_count,
3052              p_data    => x_msg_data
3053              );
3054 
3055          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3056 	         print_debuginfo(l_module_name, 'Returning error response ..');
3057 	         print_debuginfo(l_module_name, 'EXIT');
3058 
3059          END IF;
3060          RETURN;
3061 
3062  END void_pmt_internal;
3063 
3064 /*--------------------------------------------------------------------
3065  | NAME:
3066  |     is_void_allowed
3067  |
3068  |
3069  | PURPOSE:
3070  |
3071  |
3072  | PARAMETERS:
3073  |     IN
3074  |
3075  |
3076  |     OUT
3077  |
3078  |
3079  | RETURNS:
3080  |
3081  | NOTES:
3082  |
3083  *---------------------------------------------------------------------*/
3084  PROCEDURE is_void_allowed (
3085      p_api_version    IN NUMBER,
3086      p_init_msg_list  IN VARCHAR2 DEFAULT FND_API.G_FALSE,
3087      p_pmt_id         IN NUMBER,
3088      x_return_flag    OUT NOCOPY VARCHAR2,   /* 'Y'/'N' flag */
3089      x_return_status  OUT NOCOPY VARCHAR2,
3090      x_msg_count      OUT NOCOPY NUMBER,
3091      x_msg_data       OUT NOCOPY VARCHAR2
3092      )
3093  IS
3094 
3095  l_module_name    CONSTANT VARCHAR2(200) := G_PKG_NAME || '.is_void_allowed';
3096 
3097  /* used in forming callout procedure name */
3098  l_app_short_name VARCHAR2(200);
3099  l_pkg_name       VARCHAR2(200);
3100  l_callout_name   VARCHAR2(500);
3101  l_stmt           VARCHAR2(1000);
3102 
3103  /* used in invocation of callout procedure */
3104  l_api_version    CONSTANT NUMBER := 1.0;
3105  l_msg_count      NUMBER;
3106  l_msg_data       VARCHAR2(2000);
3107 
3108  l_api_name      CONSTANT VARCHAR2(100) := 'is_void_allowed';
3109 
3110  /*
3111   * Implementing the callout is optional for the calling app.
3112   * If the calling app does not implement the hook, then
3113   * the call to the hook will result in ORA-06576 error.
3114   *
3115   * There is no exception name associated with this code, so
3116   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
3117   * exception occurs, it is not fatal: we log the error and
3118   * proceed.
3119   *
3120   * If, on the other hand, the calling app implements the
3121   * callout, but the callout throws an exception, it is fatal
3122   * and we must abort the program (this will be caught
3123   * in WHEN OTHERS block).
3124   */
3125  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
3126  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
3127 
3128  BEGIN
3129 
3130      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3131 	     print_debuginfo(l_module_name, 'ENTER');
3132 
3133      END IF;
3134      /* Standard call to check for API compatibility */
3135      IF NOT FND_API.Compatible_API_Call(
3136                         l_api_version,
3137                         p_api_version,
3138                         l_api_name,
3139                         G_PKG_NAME) THEN
3140 
3141          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3142 
3143      END IF;
3144 
3145      /* Initialize message list if p_init_msg_list is set to TRUE */
3146      IF FND_API.to_boolean(p_init_msg_list) THEN
3147          FND_MSG_PUB.initialize;
3148      END IF;
3149 
3150      /* Initialize return status */
3151      x_return_status := FND_API.G_RET_STS_SUCCESS;
3152 
3153      /*
3154       * Get the application name of the calling app. This
3155       * will be used in the callout.
3156       */
3157      SELECT
3158          fnd.application_short_name
3159      INTO
3160          l_app_short_name
3161      FROM
3162          FND_APPLICATION           fnd,
3163          IBY_PAYMENTS_ALL          pmt,
3164          IBY_PAY_SERVICE_REQUESTS  req
3165      WHERE
3166          fnd.application_id             = req.calling_app_id             AND
3167          req.payment_service_request_id = pmt.payment_service_request_id AND
3168          pmt.payment_id                 = p_pmt_id
3169      ;
3170 
3171      /*
3172       * Get the constructed package name to use in the
3173       * call out.
3174       */
3175      l_pkg_name := construct_callout_pkg_name(l_app_short_name);
3176 
3177      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3178 	     print_debuginfo(l_module_name, 'Constructed package name: '
3179 	         || l_pkg_name);
3180 
3181      END IF;
3182      IF (l_pkg_name IS NULL) THEN
3183 
3184 
3185 	         print_debuginfo(l_module_name, 'Package name is null. '
3186 	             || 'Raising exception.');
3187 
3188 
3189          APP_EXCEPTION.RAISE_EXCEPTION;
3190 
3191      END IF;
3192 
3193      /*
3194       * Now try to call the external app's implementation of the hook.
3195       * The calling app may or may not have implemented the hook, so
3196       * it's not fatal if the implementation does not exist.
3197       */
3198      l_callout_name := l_pkg_name || '.' || 'void_payment_allowed';
3199 
3200      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3201 	     print_debuginfo(l_module_name, 'Attempting to invoke callout: '
3202 	         || l_callout_name);
3203 
3204      END IF;
3205      l_stmt := 'CALL '|| l_callout_name || '(:1, :2, :3, :4, :5, :6, :7, :8)';
3206 
3207      BEGIN
3208 
3209          EXECUTE IMMEDIATE
3210              (l_stmt)
3211          USING
3212              IN  l_api_version,
3213              IN  FND_API.G_FALSE,
3214              IN  FND_API.G_FALSE,
3215              IN  p_pmt_id,
3216              OUT x_return_flag,
3217              OUT x_return_status,
3218              OUT l_msg_count,
3219              OUT l_msg_data
3220          ;
3221 
3222          /*
3223           * If the called procedure did not return success,
3224           * raise an exception.
3225           */
3226          IF (x_return_status IS NULL OR
3227              x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
3228 
3229 
3230 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
3231 	                 || l_callout_name
3232 	                 || ''', returned failure status - '
3233 	                 || x_return_status
3234 	                 || '. Raising exception.'
3235 	                 );
3236 
3237 
3238              APP_EXCEPTION.RAISE_EXCEPTION;
3239 
3240          END IF;
3241 
3242      EXCEPTION
3243 
3244          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
3245              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3246 	             print_debuginfo(l_module_name, 'Callout "' || l_callout_name
3247 	                 || '" not implemented by calling app '
3248 	                 || l_app_short_name || '.');
3249 
3250 	             print_debuginfo(l_module_name, 'Skipping hook call.');
3251 
3252              END IF;
3253              /*
3254               * Fix for bug 5083294:
3255               *
3256               * Default the return flag to 'Y' (meaning
3257               * void is allowed) in case the calling
3258               * application has not implemented this
3259               * callout.
3260               */
3261              x_return_flag := 'Y';
3262 
3263              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3264 	             print_debuginfo(l_module_name, 'Defaulting x_return_flag to '
3265 	                 || ''''
3266 	                 || x_return_flag
3267 	                 || ''''
3268 	                 || ' [Meaning: void is allowed by default]'
3269 	                 );
3270 
3271              END IF;
3272          WHEN OTHERS THEN
3273 
3274 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
3275 	                 || l_callout_name
3276 	                 || ''', generated exception.'
3277 	                 );
3278 
3279 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
3280 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
3281 
3282              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
3283 
3284              /*
3285               * Propogate exception to caller.
3286               */
3287              RAISE;
3288      END;
3289 
3290      x_return_status := FND_API.G_RET_STS_SUCCESS;
3291 
3292      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3293 	     print_debuginfo(l_module_name, 'EXIT');
3294 
3295      END IF;
3296  EXCEPTION
3297 
3298      WHEN OTHERS THEN
3299 
3300          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3301 	         print_debuginfo(l_module_name, 'Exception occured when '
3302 	             || 'checking voidability of payment '
3303 	             || p_pmt_id
3304 	             );
3305 
3306 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
3307 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
3308          END IF;
3309          FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
3310 
3311          x_return_status := FND_API.G_RET_STS_ERROR;
3312 
3313          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3314 	         print_debuginfo(l_module_name, 'EXIT');
3315 
3316          END IF;
3317  END is_void_allowed;
3318 
3319 /*--------------------------------------------------------------------
3320  | NAME:
3321  |     void_pmts_internal
3322  |
3323  | PURPOSE:
3324  |     Invokes a series of callouts of the calling app to remove
3325  |     a set of submitted documents payable from the payment processing
3326  |     cycle.
3327  |
3328  |     The calling application can free up the removed documents, and
3329  |     make them available for submission in future payment request(s).
3330  |
3331  | PARAMETERS:
3332  |     IN
3333  |       p_pmt_list        - IDs of the payments to be stopped.
3334  |                           This should be an array of payment ids. All
3335  |                           the child documents payable of each of the
3336  |                           specified payments will be removed.
3337  |
3338  |       p_pmt_status_list - Current statuses of the payments to
3339  |                           be stopped. This should be an array of statuses.
3340  |
3341  |       p_voided_by       - User id of person who issued the void request.
3342  |                           This id will be stored as an attribute of the
3343  |                           payment.
3344  |
3345  |       p_void_date       - Date on which the void request was made.
3346  |
3347  |       p_void_reason     - Reason why this payment needs to be voided.
3348  |
3349  |     OUT
3350  |       x_return_status   - Result of the API call:
3351  |                         FND_API.G_RET_STS_SUCCESS indicates that all
3352  |                           the callouts were invoked successfully.
3353  |                           In this case the caller must COMMIT
3354  |                           the status changes.
3355  |
3356  |                         FND_API.G_RET_STS_ERROR (or other) indicates
3357  |                           that at least one event did not complete
3358  |                           successfully. In this case, the caller must
3359  |                           issue a ROLLBACK to undo all status changes.
3360  |
3361  | RETURNS:
3362  |
3363  | NOTES:
3364  |   Internal API, not for public use.
3365  |
3366  |   This API will not do a COMMIT. It is the the callers responsbility
3367  |   to perform COMMIT / ROLLBACK depending upon the returned status.
3368  |
3369  |   The callouts invoked must be handled synchronously by the
3370  |   calling application. So the COMMIT / ROLLBACK should affect
3371  |   the changes made to the database by the calling application as
3372  |   well. This will ensure that IBY and the calling application are
3373  |   in sync w.r.t. to the specified documents payable.
3374  |
3375  *---------------------------------------------------------------------*/
3376  PROCEDURE void_pmts_internal (
3377      p_instr_id         IN NUMBER,
3378      p_voided_by        IN NUMBER,
3379      p_void_date        IN DATE,
3380      p_void_reason      IN VARCHAR2,
3381      x_return_status    OUT NOCOPY VARCHAR2
3382      )
3383  IS
3384 
3385  l_module_name   CONSTANT VARCHAR2(200) := G_PKG_NAME || '.void_pmts_internal';
3386 
3387  l_api_version   CONSTANT NUMBER        := 1.0;
3388  l_msg_count     NUMBER;
3389  l_msg_data      VARCHAR2(2000);
3390 
3391  l_pmts_list     pmtIDTab;
3392  l_pmt_date DATE;
3393 
3394  CURSOR c_payments (p_instr_id IN NUMBER)
3395  IS
3396  SELECT
3397      payment_id
3398  FROM
3399      IBY_PAYMENTS_ALL
3400  WHERE
3401      payment_instruction_id = p_instr_id  AND
3402      payments_complete_flag = 'Y'
3403  ;
3404 
3405  BEGIN
3406 
3407      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3408 	     print_debuginfo(l_module_name, 'ENTER');
3409 
3410      END IF;
3411      FND_MSG_PUB.initialize;
3412 
3413      /* Bug: 8692538
3414       *
3415       * Validation to make sure that void date is not prior to payment date
3416       */
3417      /* Bug 9850931 - Truncating the payment date before checking it with
3418      void date as void date is not stored with timestamps. */
3419      SELECT Max(trunc(payment_date))
3420      INTO l_pmt_date
3421      FROM iby_payments_all
3422      WHERE payment_instruction_id = p_instr_id  AND
3423      payments_complete_flag = 'Y';
3424 
3425      IF(p_void_date < l_pmt_date) THEN
3426            x_return_status := FND_API.G_RET_STS_ERROR;
3427            fnd_message.set_name('IBY', 'IBY_VOID_DATE_BEF_PMT_DATE');
3428             fnd_message.set_Token('PMT_DATE', l_pmt_date);
3429             fnd_msg_pub.add;
3430            RETURN;
3431      END IF;
3432 
3433 
3434 
3435      /*
3436       * Pick up all qualifying payments of the payment instruction.
3437       */
3438      OPEN  c_payments(p_instr_id);
3439      FETCH c_payments BULK COLLECT INTO l_pmts_list;
3440      CLOSE c_payments;
3441 
3442      /*
3443       * Return failure if no payments were found.
3444       */
3445      IF (l_pmts_list.COUNT = 0) THEN
3446 
3447          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3448 	         print_debuginfo(l_module_name, 'No completed payments '
3449 	             || ' were found for payment instruction id '
3450 	             || p_instr_id
3451 	             );
3452 
3453          END IF;
3454          x_return_status := FND_API.G_RET_STS_ERROR;
3455 
3456          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3457 	         print_debuginfo(l_module_name, 'EXIT');
3458 
3459          END IF;
3460          RETURN;
3461 
3462      END IF;
3463 
3464      /*
3465       * Start processing the payments, one-by-one.
3466       */
3467      FOR i IN l_pmts_list.FIRST .. l_pmts_list.LAST LOOP
3468 
3469          void_pmt_internal (
3470              l_api_version,
3471              FND_API.G_FALSE,
3472              l_pmts_list(i),
3473              p_voided_by,
3474              p_void_date,
3475              p_void_reason,
3476              x_return_status,
3477              l_msg_count,
3478              l_msg_data
3479              );
3480 
3481          /*
3482           * Check if the call to stop the payment succeeded.
3483           */
3484          IF (x_return_status IS NULL OR
3485 	     x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
3486 
3487              /*
3488               * Even if a single call to remove a payment
3489               * failed, return failure for the entire API request.
3490               */
3491              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3492 	             print_debuginfo(l_module_name, 'Voiding of completed payment '
3493 	                 || l_pmts_list(i)
3494 	                 || ' failed.'
3495 	                 );
3496 
3497 	             print_debuginfo(l_module_name, 'EXIT');
3498 
3499              END IF;
3500              /*
3501               * It is the callers responsibility to rollback
3502               * all the changes.
3503               */
3504              RETURN;
3505 
3506          END IF;
3507 
3508      END LOOP;
3509 
3510      /*
3511       * If we reached here, it means that all payments
3512       * of the given payment instruction have been
3513       * voided successfully.
3514       */
3515      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3516 	     print_debuginfo(l_module_name, 'All payments of payment '
3517 	         || 'instruction id '
3518 	         || p_instr_id
3519 	         || ' voided successfully.'
3520 	         );
3521 
3522      END IF;
3523      /*
3524       * Fix for bug 5017119:
3525       *
3526       * After voiding all the payments of an instruction,
3527       * change the instruction status to TERMINATED
3528       * so that user cannot take any further action on the
3529       * payment instruction.
3530       */
3531      UPDATE
3532          iby_pay_instructions_all
3533      SET
3534          payment_instruction_status = INS_STATUS_TERMINATED,
3535          last_update_date = sysdate,
3536          last_updated_by = NVL(FND_GLOBAL.user_id,-1),
3537          last_update_login = NVL(FND_GLOBAL.login_id, -1)
3538      WHERE
3539          payment_instruction_id = p_instr_id
3540      ;
3541 
3542      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3543 	     print_debuginfo(l_module_name, 'Status of payment instruction '
3544 	         || p_instr_id
3545 	         || ' set to TERMINATED because all payments have been voided.'
3546 	         );
3547 
3548      END IF;
3549      x_return_status := FND_API.G_RET_STS_SUCCESS;
3550 
3551      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3552 	     print_debuginfo(l_module_name, 'Returning success response ..');
3553 
3554 	     print_debuginfo(l_module_name, 'EXIT');
3555 
3556      END IF;
3557  END void_pmts_internal;
3558 
3559 /*--------------------------------------------------------------------
3560  | NAME:
3561  |     validate_paper_doc_number
3562  |
3563  | PURPOSE:
3564  |     Validates that the given paper document number is valid for the
3565  |     given payment document (check stock) id.
3566  |
3567  | PARAMETERS:
3568  |     IN
3569  |       p_api_version   -  Version of the API.
3570  |
3571  |       p_init_msg_list - Standard API parameter; default as FND_API.G_FALSE
3572  |
3573  |       p_payment_doc_id
3574  |                       - ID of the payment document (check stock) to use.
3575  |
3576  |     IN/OUT
3577  |       x_paper_doc_num - Paper document number (check number) to use.
3578  |                         If a value is provided, this value will be validated.
3579  |                         If no value (i.e., NULL) is provided, then the
3580  |                         next available paper document number will be
3581  |                         returned as an output parameter.
3582  |
3583  |     OUT
3584  |       x_return_status - Standard return status. Possible values are:
3585  |                         FND_API.G_RET_STS_SUCCESS
3586  |                           The given paper document number is valid
3587  |                           for the given payment document.
3588  |
3589  |                           It is possible that there were warnings.
3590  |                           Please unwind the FND message stack to
3591  |                           check if there were any warnings to display
3592  |                           to the user.
3593  |
3594  |                         FND_API.G_RET_STS_ERROR
3595  |                           The given paper document number is invalid.
3596  |                           Possible reasons are the paper document number
3597  |                           has already been used, or the paper document
3598  |                           number is not in the valid range for the given
3599  |                           payment document.
3600  |
3601  |       x_msg_count     - Standard msg count
3602  |
3603  |       x_msg_data      - Standard msg data
3604  |
3605  | RETURNS:
3606  |
3607  | NOTES:
3608  |  Public API.
3609  |
3610  |   This API will not do a COMMIT/ROLLBACK. There are no database
3611  |   state changes implemented by this API. It is a read only API.
3612  |
3613  *---------------------------------------------------------------------*/
3614  PROCEDURE validate_paper_doc_number (
3615      p_api_version         IN NUMBER,
3616      p_init_msg_list       IN VARCHAR2 DEFAULT FND_API.G_FALSE,
3617      p_payment_doc_id      IN NUMBER,
3618      x_paper_doc_num       IN OUT NOCOPY NUMBER,
3619      x_return_status       OUT NOCOPY VARCHAR2,
3620      x_msg_count           OUT NOCOPY NUMBER,
3621      x_msg_data            OUT NOCOPY VARCHAR2,
3622      show_warn_msgs_flag   IN VARCHAR2 DEFAULT FND_API.G_TRUE
3623      )
3624  IS
3625 
3626  l_module_name   CONSTANT VARCHAR2(200) := G_PKG_NAME
3627                                            || '.validate_paper_doc_number';
3628 
3629  l_api_version   CONSTANT NUMBER        := 1.0;
3630  l_api_name      CONSTANT VARCHAR2(100) := 'validate_paper_doc_number';
3631 
3632  l_error_code    VARCHAR2(3000);
3633 
3634  l_last_used_check_num   NUMBER := 0;
3635  l_next_check_number     NUMBER := 0;
3636 
3637  l_first_avail_check_num NUMBER := 0;
3638  l_last_avail_check_num  NUMBER := 0;
3639  l_pmt_doc_name          VARCHAR2(200) := '';
3640  l_used_paper_doc_number NUMBER := 0;
3641 
3642  l_flag                  BOOLEAN;
3643 
3644  BEGIN
3645 
3646      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3647 	     print_debuginfo(l_module_name, 'ENTER');
3648 
3649      END IF;
3650      /* Standard call to check for API compatibility */
3651      IF NOT FND_API.Compatible_API_Call(
3652                         l_api_version,
3653                         p_api_version,
3654                         l_api_name,
3655                         G_PKG_NAME) THEN
3656 
3657          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3658 
3659      END IF;
3660 
3661      /* Initialize message list if p_init_msg_list is set to TRUE */
3662      IF FND_API.to_boolean(p_init_msg_list) THEN
3663          FND_MSG_PUB.initialize;
3664      END IF;
3665 
3666      /* Initialize return status */
3667      x_return_status := FND_API.G_RET_STS_SUCCESS;
3668 
3669 
3670      /*
3671       * Pull up the details of the paper stock, like the
3672       * last used check number and the last available
3673       * check number.
3674       */
3675      BEGIN
3676 
3677          SELECT
3678              NVL(last_issued_document_number,     0),
3679              NVL(first_available_document_num,    0),
3680              NVL(last_available_document_number, -1),
3681              payment_document_name
3682          INTO
3683              l_last_used_check_num,
3684              l_first_avail_check_num,
3685              l_last_avail_check_num,
3686              l_pmt_doc_name
3687          FROM
3688              CE_PAYMENT_DOCUMENTS
3689          WHERE
3690              payment_document_id = p_payment_doc_id
3691          ;
3692 
3693      EXCEPTION
3694 
3695          WHEN NO_DATA_FOUND THEN
3696 
3697              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3698 	             print_debuginfo(l_module_name, 'No payment document '
3699 	                 || 'with payment document id '
3700 	                 || p_payment_doc_id
3701 	                 || ' was found in CE_PAYMENT_DOCUMENTS table.'
3702 	                 );
3703 
3704 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
3705 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
3706              END IF;
3707              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
3708 
3709              x_return_status := FND_API.G_RET_STS_ERROR;
3710 
3711              l_error_code := 'IBY_INS_NO_PMT_DOC';
3712              FND_MESSAGE.set_name('IBY', l_error_code);
3713 
3714              FND_MESSAGE.SET_TOKEN('PMT_DOC_ID',
3715                  p_payment_doc_id,
3716                  FALSE);
3717 
3718              FND_MSG_PUB.ADD;
3719 
3720              FND_MSG_PUB.COUNT_AND_GET(
3721                  p_count => x_msg_count,
3722                  p_data  => x_msg_data
3723                  );
3724 
3725              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3726 	             print_debuginfo(l_module_name, 'Returning error response ..');
3727 
3728 	             print_debuginfo(l_module_name, 'EXIT');
3729 
3730              END IF;
3731              RETURN;
3732 
3733 
3734          WHEN OTHERS THEN
3735 
3736              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3737 	             print_debuginfo(l_module_name, 'Exception occured when '
3738 	                 || 'attempting to get details of payment document id '
3739 	                 || p_payment_doc_id
3740 	                 );
3741 
3742 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
3743 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
3744 
3745              END IF;
3746              x_return_status := FND_API.G_RET_STS_ERROR;
3747 
3748              l_error_code := 'IBY_PMT_DOC_EXCEPTION';
3749              FND_MESSAGE.set_name('IBY', l_error_code);
3750 
3751              FND_MESSAGE.SET_TOKEN('PMT_DOC_ID',
3752                  p_payment_doc_id,
3753                  FALSE);
3754 
3755              FND_MSG_PUB.ADD;
3756 
3757              FND_MSG_PUB.COUNT_AND_GET(
3758                  p_count => x_msg_count,
3759                  p_data  => x_msg_data
3760                  );
3761 
3762              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3763 	             print_debuginfo(l_module_name, 'Returning error response ..');
3764 
3765 	             print_debuginfo(l_module_name, 'EXIT');
3766 
3767              END IF;
3768              RETURN;
3769 
3770      END;
3771 
3772      /*
3773       * If we reached here, it means that we were able to get
3774       * the details of the provided payment document.
3775       */
3776 
3777      /*
3778       * If a null value is provided for the paper document number,
3779       * we have to return the next available paper document number
3780       * and exit.
3781       */
3782      IF (x_paper_doc_num IS NULL) THEN
3783 
3784          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3785 	         print_debuginfo(l_module_name, 'Paper document number '
3786 	             || 'has not been provided. Generating ..'
3787 	             );
3788 
3789          END IF;
3790          /*
3791           * Loop through the available paper documents until
3792           * we get a paper document number that is -
3793           *
3794           * 1. Not used
3795           * 2. Not greater than the last available document number.
3796           *
3797           * If both these conditions are not satisfied, return an
3798           * error response.
3799           */
3800          x_paper_doc_num := -1;
3801          l_error_code    := NULL;
3802 
3803          l_next_check_number := l_last_used_check_num;
3804 
3805          WHILE (x_paper_doc_num < 0 AND l_error_code IS NULL) LOOP
3806 
3807              /*
3808               * Increment the check number by one and check
3809               * if it unused. If it is unused, we can return
3810               * this check number, else we need to increment
3811               * check number by one more and try again in a
3812               * loop.
3813               */
3814              l_next_check_number := l_next_check_number + 1;
3815 
3816              x_paper_doc_num := l_next_check_number;
3817 
3818              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3819 	             print_debuginfo(l_module_name, 'Generated paper document '
3820 	                 || 'number is '
3821 	                 || x_paper_doc_num
3822 	                 );
3823 
3824              END IF;
3825              /*
3826               * Fix for bug 5005222:
3827               *
3828               * If last available paper document number is not
3829               * specified by the user (-1), assume that there
3830               * are an infinite number of checks available for
3831               * numbering [no upper bound].
3832               */
3833              /* check that we are not out of paper stock */
3834              IF (x_paper_doc_num         > l_last_avail_check_num AND
3835                  l_last_avail_check_num <> -1)                    THEN
3836 
3837                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3838 	                 print_debuginfo(l_module_name, 'Last available '
3839 	                     || 'paper document number is '
3840 	                     || l_last_avail_check_num
3841 	                     );
3842 
3843 	                 print_debuginfo(l_module_name, 'Provided payment document '
3844 	                     || p_payment_doc_id
3845 	                     || ' is exhausted. No more paper documents are '
3846 	                     || 'available for issue.'
3847 	                     );
3848 
3849                  END IF;
3850                  x_return_status := FND_API.G_RET_STS_SUCCESS;
3851 
3852 		 --Bug 8367408 : Paper document number should be reset per AP
3853 		 x_paper_doc_num := NULL;
3854 
3855                  l_error_code := 'IBY_CHECK_STOCK_EXHTD_REENTER';
3856                  FND_MESSAGE.set_name('IBY', l_error_code);
3857 
3858                  FND_MSG_PUB.ADD;
3859 
3860                  FND_MSG_PUB.COUNT_AND_GET(
3861                      p_count => x_msg_count,
3862                      p_data  => x_msg_data
3863                      );
3864 
3865                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3866 	                 print_debuginfo(l_module_name, 'Returning error response ..');
3867 
3868 
3869                  END IF;
3870              ELSE
3871 
3872                  /*
3873                   * Check if this paper document number has already
3874                   * been used.
3875                   */
3876                  l_flag := checkIfDocUsed(x_paper_doc_num, p_payment_doc_id);
3877 
3878                  IF (l_flag = TRUE) THEN
3879 
3880                      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3881 	                     print_debuginfo(l_module_name, 'Generated paper document '
3882 	                         || 'number '
3883 	                         || x_paper_doc_num
3884 	                         || ' has been used. Generating next available '
3885 	                         || 'paper doc number ..'
3886 	                         );
3887 
3888                      END IF;
3889                      /* this will cause the while loop to iterate */
3890                      x_paper_doc_num := -1;
3891 
3892                  ELSE
3893 
3894                      x_return_status := FND_API.G_RET_STS_SUCCESS;
3895 
3896                      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3897 	                     print_debuginfo(l_module_name, 'Generated paper document '
3898 	                         || 'number '
3899 	                         || x_paper_doc_num
3900 	                         || ' is unused.'
3901 	                         );
3902 
3903 	                     print_debuginfo(l_module_name, 'Returning success '
3904 	                         || 'response ..');
3905 
3906                      END IF;
3907                  END IF; -- if l_flag <> true
3908 
3909              END IF; -- if x_paper_doc_num < l_last_avail_check_num
3910 
3911          END LOOP;
3912 
3913          /* exit at this point as we will not be doing any validations */
3914          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3915 	         print_debuginfo(l_module_name, 'EXIT');
3916 
3917          END IF;
3918          RETURN;
3919 
3920      END IF; -- if x_paper_doc_num is null
3921 
3922      /*
3923       * Start the paper document number validations.
3924       */
3925 
3926      /*
3927       * Provided paper document number cannot be below
3928       * first available paper doc number.
3929       */
3930      IF (x_paper_doc_num < l_first_avail_check_num) THEN
3931 
3932              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3933 	             print_debuginfo(l_module_name, 'Provided paper doc number '
3934 	                 || x_paper_doc_num
3935 	                 || ' is below first available document number '
3936 	                 || l_first_avail_check_num
3937 	                 );
3938 
3939              END IF;
3940              x_return_status := FND_API.G_RET_STS_ERROR;
3941 
3942              l_error_code := 'IBY_DOC_NUM_BELOW_ALLOWED';
3943              FND_MESSAGE.set_name('IBY', l_error_code);
3944 
3945              FND_MESSAGE.SET_TOKEN('FIRST_AVAILABLE_DOC_NUM',
3946                  l_first_avail_check_num,
3947                  FALSE);
3948 
3949              FND_MSG_PUB.ADD;
3950 
3951              FND_MSG_PUB.COUNT_AND_GET(
3952                  p_count => x_msg_count,
3953                  p_data  => x_msg_data
3954                  );
3955 
3956              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3957 	             print_debuginfo(l_module_name, 'Returning error response ..');
3958 
3959 	             print_debuginfo(l_module_name, 'EXIT');
3960 
3961              END IF;
3962              RETURN;
3963 
3964      END IF;
3965 
3966      /*
3967       * Fix for bug 5005222:
3968       *
3969       * If last available paper document number is not
3970       * specified by the user (-1), assume that there
3971       * are an infinite number of checks available for
3972       * numbering [no upper bound].
3973       */
3974 
3975      /*
3976       * Provided paper document number cannot be above
3977       * last available paper doc number.
3978       */
3979      IF (x_paper_doc_num         > l_last_avail_check_num AND
3980          l_last_avail_check_num <> -1)                    THEN
3981 
3982              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3983 	             print_debuginfo(l_module_name, 'Last available '
3984 	                 || 'paper document number is '
3985 	                 || l_last_avail_check_num
3986 	                 );
3987 
3988 	             print_debuginfo(l_module_name, 'Provided paper doc number '
3989 	                 || x_paper_doc_num
3990 	                 || ' is above last available document number '
3991 	                 || l_last_avail_check_num
3992 	                 );
3993 
3994              END IF;
3995              x_return_status := FND_API.G_RET_STS_ERROR;
3996 
3997              l_error_code := 'IBY_DOC_NUM_ABOVE_ALLOWED';
3998              FND_MESSAGE.set_name('IBY', l_error_code);
3999 
4000              FND_MESSAGE.SET_TOKEN('LAST_AVAILABLE_DOC_NUM',
4001                  l_last_avail_check_num,
4002                  FALSE);
4003 
4004              FND_MSG_PUB.ADD;
4005 
4006              FND_MSG_PUB.COUNT_AND_GET(
4007                  p_count => x_msg_count,
4008                  p_data  => x_msg_data
4009                  );
4010 
4011              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4012 	             print_debuginfo(l_module_name, 'Returning error response ..');
4013 
4014 	             print_debuginfo(l_module_name, 'EXIT');
4015 
4016              END IF;
4017              RETURN;
4018 
4019      END IF;
4020 
4021      /*
4022       * Provided paper document number should not be below
4023       * last issued paper doc number (warning).
4024       */
4025      IF (x_paper_doc_num < l_last_used_check_num) THEN
4026 
4027          /*
4028           * This check will only result in a warning message.
4029           * Ignore this check, if the show warnings flag is
4030           * set to false.
4031           *
4032           * This method can be called multiple times in the
4033           * single payments flow, this could result in a large
4034           * number of error messages being put into the message
4035           * stack and displayed to the user.
4036           *
4037           * By setting the show warnings flag to 'false' for
4038           * the single payments flow, the number of potential
4039           * messages displayed to the user is reduced.
4040           */
4041          IF (show_warn_msgs_flag = FND_API.G_TRUE) THEN
4042 
4043              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4044 	             print_debuginfo(l_module_name, 'Warning: Provided paper '
4045 	                 || 'doc number '
4046 	                 || x_paper_doc_num
4047 	                 || ' is below last issued document number '
4048 	                 || l_last_used_check_num
4049 	                 );
4050 
4051              END IF;
4052              l_error_code := 'IBY_DOC_NUM_BELOW_ISSUED';
4053              FND_MESSAGE.set_name('IBY', l_error_code);
4054 
4055              FND_MSG_PUB.ADD;
4056 
4057              FND_MSG_PUB.COUNT_AND_GET(
4058                  p_count => x_msg_count,
4059                  p_data  => x_msg_data
4060                  );
4061 
4062          END IF;
4063 
4064      END IF;
4065 
4066      /*
4067       * Check if this paper document number has already
4068       * been used.
4069       */
4070      l_flag := checkIfDocUsed(x_paper_doc_num, p_payment_doc_id);
4071 
4072      IF (l_flag = TRUE) THEN
4073 
4074          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4075 	         print_debuginfo(l_module_name, 'Provided paper document '
4076 	             || 'number '
4077 	             || x_paper_doc_num
4078 	             || ' has already been used.'
4079 	             );
4080 
4081          END IF;
4082          x_return_status := FND_API.G_RET_STS_ERROR;
4083 
4084          l_error_code := 'IBY_DOC_NUM_USED';
4085          FND_MESSAGE.set_name('IBY', l_error_code);
4086 
4087          FND_MSG_PUB.ADD;
4088 
4089          FND_MSG_PUB.COUNT_AND_GET(
4090              p_count => x_msg_count,
4091              p_data  => x_msg_data
4092              );
4093 
4094          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4095 	         print_debuginfo(l_module_name, 'Returning error response ..');
4096 
4097 	         print_debuginfo(l_module_name, 'EXIT');
4098 
4099          END IF;
4100          RETURN;
4101 
4102      ELSE
4103 
4104          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4105 	         print_debuginfo(l_module_name, 'Provided paper document '
4106 	             || 'number '
4107 	             || x_paper_doc_num
4108 	             || ' is unused.'
4109 	             );
4110 
4111          END IF;
4112      END IF; -- if l_flag <> true
4113 
4114      /*
4115       * If we reached here, it means that both the paper document
4116       * number and the payment document id are valid.
4117       */
4118      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4119 	     print_debuginfo(l_module_name, 'Returning success response ..');
4120 
4121      END IF;
4122      x_return_status := FND_API.G_RET_STS_SUCCESS;
4123 
4124      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4125 	     print_debuginfo(l_module_name, 'EXIT');
4126 
4127      END IF;
4128  END validate_paper_doc_number;
4129 
4130 
4131 
4132 /*--------------------------------------------------------------------
4133  | NAME:
4134  |     validate_payment_document
4135  |
4136  | PURPOSE:
4137  |     Validates that the given payment document.
4138  |
4139  | PARAMETERS:
4140  |     IN
4141  |       p_api_version   -  Version of the API.
4142  |
4143  |       p_init_msg_list - Standard API parameter; default as FND_API.G_FALSE
4144  |
4145  |       p_payment_doc_id
4146  |                       - ID of the payment document (check stock) to use.
4147  |
4148  |    OUT
4149  |       x_return_status - Standard return status. Possible values are:
4150  |                         FND_API.G_RET_STS_SUCCESS
4151  |                           The given payment document is valid
4152  |                           and it is not locked by any of Instructions or
4153  |                           with single payment.
4154  |
4155  |
4156  |
4157  |                         FND_API.G_RET_STS_ERROR
4158  |                           The given payment document is already locked.
4159  |                           Possible reasons are the payement document
4160  |                           has already locked by any Instruction, or
4161  |                           for single payment by another user.
4162  |
4163  |       x_msg_count     - Standard msg count
4164  |
4165  |       x_msg_data      - Standard msg data
4166  |
4167  | RETURNS:
4168  |
4169  | NOTES:
4170  |  Public API.
4171  |
4172  |   This API will not do a COMMIT/ROLLBACK. There are no database
4173  |   state changes implemented by this API. It is a read only API.
4174  |
4175  *---------------------------------------------------------------------*/
4176  PROCEDURE validate_payment_document (
4177      p_api_version         IN NUMBER,
4178      p_init_msg_list       IN VARCHAR2 DEFAULT FND_API.G_FALSE,
4179      p_payment_doc_id      IN NUMBER,
4180      x_return_status       OUT NOCOPY VARCHAR2,
4181      x_msg_count           OUT NOCOPY NUMBER,
4182      x_msg_data            OUT NOCOPY VARCHAR2
4183      )
4184  IS
4185  l_module_name   CONSTANT VARCHAR2(200) := G_PKG_NAME
4186                                            || '.validate_payment_document';
4187 
4188  l_api_version   CONSTANT NUMBER        := 1.0;
4189  l_api_name      CONSTANT VARCHAR2(100) := 'validate_payment_document';
4190 
4191  l_error_code    VARCHAR2(3000);
4192  l_pmt_doc_name  VARCHAR2(200) := '';
4193  l_pmt_doc_name_dup  VARCHAR2(200) := '';
4194  l_pmt_instr_id  NUMBER(15);
4195  BEGIN
4196     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4197 	    print_debuginfo(l_module_name, 'ENTER');
4198 	    print_debuginfo(l_module_name, 'p_payment_doc_id:'||p_payment_doc_id);
4199 
4200     END IF;
4201      /* Standard call to check for API compatibility */
4202      IF NOT FND_API.Compatible_API_Call(
4203                         l_api_version,
4204                         p_api_version,
4205                         l_api_name,
4206                         G_PKG_NAME) THEN
4207 
4208          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4209 
4210      END IF;
4211 
4212      /* Initialize message list if p_init_msg_list is set to TRUE */
4213      IF FND_API.to_boolean(p_init_msg_list) THEN
4214          FND_MSG_PUB.initialize;
4215      END IF;
4216 
4217      /* Initialize return status */
4218      x_return_status := FND_API.G_RET_STS_SUCCESS;
4219 
4220 
4221       /*
4222       * Pull up the name of the paper stock
4223       */
4224     BEGIN
4225          SELECT
4226              payment_document_name,  payment_instruction_id
4227          INTO
4228              l_pmt_doc_name, l_pmt_instr_id
4229          FROM
4230              CE_PAYMENT_DOCUMENTS
4231          WHERE
4232              payment_document_id = p_payment_doc_id;
4233      EXCEPTION
4234 
4235          WHEN NO_DATA_FOUND THEN
4236 
4237              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4238 	             print_debuginfo(l_module_name, 'No payment document '
4239 	                 || 'with payment document id '
4240 	                 || p_payment_doc_id
4241 	                 || ' was found in CE_PAYMENT_DOCUMENTS table.'
4242 	                 );
4243 
4244 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
4245 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
4246              END IF;
4247              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
4248 
4249              x_return_status := FND_API.G_RET_STS_ERROR;
4250 
4251              l_error_code := 'IBY_INS_NO_PMT_DOC';
4252              FND_MESSAGE.set_name('IBY', l_error_code);
4253 
4254              FND_MESSAGE.SET_TOKEN('PMT_DOC_ID',
4255                  p_payment_doc_id,
4256                  FALSE);
4257 
4258              FND_MSG_PUB.ADD;
4259 
4260              FND_MSG_PUB.COUNT_AND_GET(
4261                  p_count => x_msg_count,
4262                  p_data  => x_msg_data
4263                  );
4264 
4265              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4266 	             print_debuginfo(l_module_name, 'Returning error response ..');
4267 
4268 	             print_debuginfo(l_module_name, 'EXIT');
4269 
4270              END IF;
4271              RETURN;
4272 
4273 
4274          WHEN OTHERS THEN
4275 
4276              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4277 	             print_debuginfo(l_module_name, 'Exception occured when '
4278 	                 || 'attempting to get details of payment document id '
4279 	                 || p_payment_doc_id
4280 	                 );
4281 
4282 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
4283 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
4284 
4285              END IF;
4286              x_return_status := FND_API.G_RET_STS_ERROR;
4287 
4288              l_error_code := 'IBY_PMT_DOC_EXCEPTION';
4289              FND_MESSAGE.set_name('IBY', l_error_code);
4290 
4291              FND_MESSAGE.SET_TOKEN('PMT_DOC_ID',
4292                  p_payment_doc_id,
4293                  FALSE);
4294 
4295              FND_MSG_PUB.ADD;
4296 
4297              FND_MSG_PUB.COUNT_AND_GET(
4298                  p_count => x_msg_count,
4299                  p_data  => x_msg_data
4300                  );
4301 
4302              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4303 	             print_debuginfo(l_module_name, 'Returning error response ..');
4304 
4305 	             print_debuginfo(l_module_name, 'EXIT');
4306 
4307              END IF;
4308              RETURN;
4309 
4310      END;
4311 
4312 
4313   /*
4314    * If payment document is already locked by any instruction
4315    * error message would be thrown.
4316    */
4317 
4318     IF (l_pmt_instr_id <> NULL) THEN
4319                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4320 	                 print_debuginfo(l_module_name, 'Payment document '
4321 	                 || 'with payment document id '
4322 	                 || p_payment_doc_id
4323 	                 || ' with name'
4324 	                 || l_pmt_doc_name
4325 	                 || ' is locked by instruction'
4326 	                 || l_pmt_instr_id
4327 	                 );
4328                  END IF;
4329              x_return_status := FND_API.G_RET_STS_ERROR;
4330 
4331              l_error_code := 'IBY_PAY_DOC_ALREADY_USE';
4332              FND_MESSAGE.set_name('IBY', l_error_code);
4333 
4334              FND_MESSAGE.SET_TOKEN('PAY_INSTRUCTION',
4335                  l_pmt_instr_id,
4336                  FALSE);
4337              FND_MSG_PUB.ADD;
4338 
4339              FND_MSG_PUB.COUNT_AND_GET(
4340                  p_count => x_msg_count,
4341                  p_data  => x_msg_data
4342                  );
4343 
4344              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4345 	             print_debuginfo(l_module_name, 'Returning error response ..');
4346 
4347 	             print_debuginfo(l_module_name, 'EXIT');
4348 
4349              END IF;
4350              RETURN;
4351 
4352     END IF;
4353 
4354 
4355 
4356      /*
4357       * LOCK THE PAYMENT DOCUMENT
4358       * If payment document not found, then it means that it is locked
4359       * by another user for single payment.
4360       */
4361     BEGIN
4362          SELECT
4363              payment_document_name
4364          INTO
4365              l_pmt_doc_name_dup
4366          FROM
4367              CE_PAYMENT_DOCUMENTS
4368          WHERE
4369              payment_document_id = p_payment_doc_id
4370          FOR UPDATE SKIP LOCKED;
4371           EXCEPTION
4372 
4373          WHEN NO_DATA_FOUND THEN
4374 
4375              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4376 	             print_debuginfo(l_module_name, 'Payment document '
4377 	                 || 'with payment document id '
4378 	                 || p_payment_doc_id
4379 	                 || 'is locked by another user for single payment.'
4380 	                 );
4381 
4382 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
4383 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
4384              END IF;
4385              --FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
4386 
4387              x_return_status := FND_API.G_RET_STS_ERROR;
4388 
4389              l_error_code := 'IBY_PMT_DOC_UNAVAILABLE';
4390              FND_MESSAGE.set_name('IBY', l_error_code);
4391 
4392              FND_MSG_PUB.ADD;
4393 
4394              FND_MSG_PUB.COUNT_AND_GET(
4395                  p_count => x_msg_count,
4396                  p_data  => x_msg_data
4397                  );
4398 
4399              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4400 	             print_debuginfo(l_module_name, 'Returning error response ..');
4401 
4402 	             print_debuginfo(l_module_name, 'EXIT');
4403 
4404              END IF;
4405              RETURN;
4406 
4407 
4408          WHEN OTHERS THEN
4409 
4410              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4411 	             print_debuginfo(l_module_name, 'Exception occured when '
4412 	                 || 'attempting to get details of payment document id '
4413 	                 || p_payment_doc_id
4414 	                 );
4415 
4416 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
4417 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
4418 
4419              END IF;
4420              x_return_status := FND_API.G_RET_STS_ERROR;
4421 
4422              l_error_code := 'IBY_PMT_DOC_EXCEPTION';
4423              FND_MESSAGE.set_name('IBY', l_error_code);
4424 
4425              FND_MESSAGE.SET_TOKEN('PMT_DOC_ID',
4426                  p_payment_doc_id,
4427                  FALSE);
4428 
4429              FND_MSG_PUB.ADD;
4430 
4431              FND_MSG_PUB.COUNT_AND_GET(
4432                  p_count => x_msg_count,
4433                  p_data  => x_msg_data
4434                  );
4435 
4436              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4437 	             print_debuginfo(l_module_name, 'Returning error response ..');
4438 
4439 	             print_debuginfo(l_module_name, 'EXIT');
4440 
4441              END IF;
4442              RETURN;
4443 
4444      END;
4445      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4446 	     print_debuginfo(l_module_name, 'EXIT');
4447 
4448      END IF;
4449  END validate_payment_document;
4450 
4451 
4452 /*--------------------------------------------------------------------
4453  | NAME:
4454  |     terminate_pmt_instruction
4455  |
4456  |
4457  | PURPOSE:
4458  |
4459  |
4460  | PARAMETERS:
4461  |     IN
4462  |
4463  |
4464  |     OUT
4465  |
4466  |
4467  | RETURNS:
4468  |
4469  | NOTES:
4470  |   Internal API, not for public use.
4471  |
4472  | UPDATE ON MAY-04-2006, rameshsh
4473  | This method was not performing a COMMIT earlier. Due to bug 5206672
4474  | a COMMIT has been added in the code before invoking unlock_pmt_entity(..).
4475  *---------------------------------------------------------------------*/
4476  PROCEDURE terminate_pmt_instruction (
4477      p_instr_id       IN NUMBER,
4478      p_instr_status   IN VARCHAR2,
4479      x_return_status  OUT NOCOPY VARCHAR2
4480      )
4481  IS
4482 
4483  l_module_name    CONSTANT VARCHAR2(200) := G_PKG_NAME
4484                                             || '.terminate_pmt_instruction';
4485 
4486  l_rejection_id   NUMBER(15);
4487 
4488  /* used in forming callout procedure name */
4489  l_calling_app_id NUMBER;
4490  l_pkg_name       VARCHAR2(200);
4491  l_pckg_name      VARCHAR2(200) := 'AP_AWT_CALLOUT_PKG';
4492  l_callout_name   VARCHAR2(500);
4493  l_stmt           VARCHAR2(1000);
4494 
4495  /* used in invocation of callout procedure */
4496  l_api_version    CONSTANT NUMBER := 1.0;
4497  l_msg_count      NUMBER;
4498  l_msg_data       VARCHAR2(2000);
4499 
4500  l_appNamesTab    appNamesTab;
4501  l_appIdsTab      appIdsTab;
4502 
4503  l_pmt_doc_id     NUMBER(15);
4504  l_pmt_doc_name   VARCHAR2(2000);
4505 
4506  l_flag           BOOLEAN := FALSE;
4507  l_error_code     VARCHAR2(3000);
4508 
4509  l_ret_status     VARCHAR2(300);
4510 
4511  l_valid_pmts_count NUMBER;
4512 
4513  l_max_paper_document_number NUMBER;
4514  l_min_paper_document_number NUMBER;
4515  l_number_of_payments NUMBER;
4516  l_last_issued NUMBER;
4517  l_document_locked BOOLEAN := TRUE;
4518  l_last_issued_modified NUMBER;
4519  l_irregular_document_numbers EXCEPTION;
4520  l_request_status VARCHAR2(30);
4521 
4522  /*
4523   * Cursor to pick up names of all calling applications
4524   * associated with a particular payment instruction.
4525   */
4526  CURSOR c_app_names (p_instr_id NUMBER)
4527  IS
4528  SELECT DISTINCT
4529      fnd.application_short_name
4530  FROM
4531      FND_APPLICATION          fnd,
4532      IBY_PAYMENTS_ALL         pmt,
4533      IBY_PAY_SERVICE_REQUESTS req,
4534      IBY_PAY_INSTRUCTIONS_ALL ins
4535  WHERE
4536      pmt.payment_instruction_id     = ins.payment_instruction_id     AND
4537      req.payment_service_request_id = pmt.payment_service_request_id AND
4538      fnd.application_id             = req.calling_app_id             AND
4539      ins.payment_instruction_id     = p_instr_id
4540  ;
4541 
4542  /*
4543   * Cursor to pick up ids all calling applications
4544   * associated with a particular payment instruction.
4545   */
4546  CURSOR c_app_ids (p_instr_id NUMBER)
4547  IS
4548  SELECT
4549      fnd.application_id
4550  FROM
4551      FND_APPLICATION          fnd,
4552      IBY_PAYMENTS_ALL         pmt,
4553      IBY_PAY_SERVICE_REQUESTS req,
4554      IBY_PAY_INSTRUCTIONS_ALL ins
4555  WHERE
4556      pmt.payment_instruction_id     = ins.payment_instruction_id     AND
4557      req.payment_service_request_id = pmt.payment_service_request_id AND
4558      fnd.application_id             = req.calling_app_id             AND
4559      ins.payment_instruction_id     = p_instr_id
4560  ;
4561 
4562  /*
4563   * Implementing the callout is optional for the calling app.
4564   * If the calling app does not implement the hook, then
4565   * the call to the hook will result in ORA-06576 error.
4566   *
4567   * There is no exception name associated with this code, so
4568   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
4569   * exception occurs, it is not fatal: we log the error and
4570   * proceed.
4571   *
4572   * If, on the other hand, the calling app implements the
4573   * callout, but the callout throws an exception, it is fatal
4574   * and we must abort the program (this will be caught
4575   * in WHEN OTHERS block).
4576   */
4577  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
4578  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
4579 
4580  BEGIN
4581 
4582      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4583 	     print_debuginfo(l_module_name, 'ENTER');
4584 
4585      END IF;
4586      FND_MSG_PUB.initialize;
4587 
4588      /*
4589       * STATUS CHANGE:
4590       *
4591       * API Responsibility:
4592       * instruction status = TERMINATED
4593       * payment_status  = REMOVED_INSTRUCTION_TERMINATED
4594       * document_status = REMOVED_INSTRUCTION_TERMINATED
4595       *
4596       */
4597 
4598      /*
4599       * STEP 1:
4600       *
4601       * If the given payment instruction is locked, it means
4602       * that some concurrent program is acting upon the
4603       * pmt instruction at the moment.
4604       *
4605       * To prevent data corruption do not allow the payment
4606       * instruction to be terminated.
4607       */
4608      l_flag := checkIfPmtEntityLocked(p_instr_id, 'PAYMENT_INSTRUCTION');
4609 
4610     /* for bug 6196551 */
4611         SELECT
4612              count(*)
4613          INTO
4614              l_valid_pmts_count
4615          FROM
4616              IBY_PAYMENTS_ALL pmt
4617          WHERE
4618              pmt.payment_instruction_id = p_instr_id AND
4619              pmt.payment_status NOT IN
4620                  (
4621                   PAY_STATUS_VOID,
4622                   PAY_STATUS_REMOVED,
4623 		  PAY_STATUS_STOPPED
4624                  );
4625 
4626     IF (l_valid_pmts_count=0) THEN
4627     l_flag := FALSE;
4628     END IF;
4629 
4630     SELECT get_conc_request_status(request_id)
4631     INTO l_request_status
4632     FROM iby_pay_instructions_all
4633     WHERE payment_instruction_id = p_instr_id;
4634 
4635     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4636 	    print_debuginfo(l_module_name, 'Request Status::'
4637 	             || l_request_status
4638 	             );
4639 
4640     END IF;
4641      IF (l_flag = TRUE  AND l_request_status <> 'SUCCESS' AND l_request_status <> 'ERROR') THEN
4642 
4643          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4644 	         print_debuginfo(l_module_name, 'Payment instruction '
4645 	             || p_instr_id
4646 	             || ' has been locked by a concurrent program.'
4647 	             || ' It cannot be terminated at present.'
4648 	             );
4649 
4650          END IF;
4651          x_return_status := FND_API.G_RET_STS_ERROR;
4652 
4653          l_error_code := 'IBY_INS_LOCKED';
4654          FND_MESSAGE.set_name('IBY', l_error_code);
4655          FND_MSG_PUB.ADD;
4656 
4657          --FND_MSG_PUB.COUNT_AND_GET(
4658          --    p_count => x_msg_count,
4659          --    p_data  => x_msg_data
4660          --    );
4661 
4662          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4663 	         print_debuginfo(l_module_name, 'Returning error response ..');
4664 
4665 	         print_debuginfo(l_module_name, 'EXIT');
4666 
4667          END IF;
4668          RETURN;
4669 
4670      ELSE
4671 
4672          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4673 	         print_debuginfo(l_module_name, 'Payment instruction '
4674 	             || p_instr_id
4675 	             || ' is not locked.'
4676 	             );
4677 
4678          END IF;
4679      END IF;
4680 
4681      /*
4682       * STEP 1.1:
4683       *
4684       * Before terminating payment instruction, call AP hook for voiding
4685       * withholding certificates.
4686       */
4687 
4688      l_callout_name := l_pckg_name || '.' || 'zx_witholdingCertificatesHook';
4689 
4690      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4691 	     print_debuginfo(l_module_name, 'Attempting to call hook for voiding '
4692 	         || 'withholding certificates: '
4693 	         || l_callout_name);
4694 
4695      END IF;
4696      l_stmt := 'CALL '|| l_callout_name || '(:1, :2, :3, :4, :5, :6, :7, :8)';
4697 
4698      BEGIN
4699 
4700          EXECUTE IMMEDIATE
4701              (l_stmt)
4702          USING
4703              IN  p_instr_id,
4704              IN  'TERMINATE',
4705              IN  l_api_version,
4706              IN  FND_API.G_FALSE,
4707              IN  FND_API.G_FALSE,
4708              OUT x_return_status,
4709              OUT l_msg_count,
4710              OUT l_msg_data
4711          ;
4712 
4713          /*
4714           * If the called procedure did not return success,
4715           * raise an exception.
4716           */
4717          IF (x_return_status IS NULL OR
4718              x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4719 
4720 
4721 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
4722 	                 || l_callout_name
4723 	                 || ''', returned failure status - '
4724 	                 || x_return_status
4725 	                 || '. Raising exception.',
4726 	                 FND_LOG.LEVEL_UNEXPECTED
4727 	                 );
4728 
4729 
4730              l_error_code := 'IBY_INS_AWT_CERT_HOOK_FAILED';
4731              FND_MESSAGE.set_name('IBY', l_error_code);
4732 
4733              APP_EXCEPTION.RAISE_EXCEPTION;
4734 
4735          END IF;
4736 
4737      EXCEPTION
4738 
4739          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
4740              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4741 	             print_debuginfo(l_module_name, 'Callout "' || l_callout_name
4742 	                 || '" not implemented by calling app - AP'
4743 	                 );
4744 
4745 	             print_debuginfo(l_module_name, 'Skipping hook call.');
4746 
4747              END IF;
4748          WHEN OTHERS THEN
4749 
4750 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
4751 	                 || l_callout_name
4752 	                 || ''', generated exception.',
4753 	                 FND_LOG.LEVEL_UNEXPECTED
4754 	                 );
4755 
4756 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE,
4757 	                 FND_LOG.LEVEL_UNEXPECTED);
4758 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM,
4759 	                 FND_LOG.LEVEL_UNEXPECTED);
4760 
4761 
4762              /*
4763               * Propogate exception to caller.
4764               */
4765              RAISE;
4766      END;
4767 
4768 
4769 
4770      /*
4771       * STEP 2:
4772       *
4773       * Update payment instruction, payment and document statuses
4774       * to 'terminated'.
4775       */
4776      UPDATE
4777          iby_pay_instructions_all
4778      SET
4779          payment_instruction_status = INS_STATUS_TERMINATED
4780      WHERE
4781          payment_instruction_id = p_instr_id
4782      ;
4783 
4784      BEGIN
4785 
4786          UPDATE
4787              IBY_PAYMENTS_ALL
4788          SET
4789              payment_status = PAY_STATUS_INS_TERM
4790          WHERE
4791              payment_instruction_id = p_instr_id     AND
4792              (
4793               payment_status <> PAY_STATUS_REMOVED       AND
4794               payment_status <> PAY_STATUS_VOID_SETUP    AND
4795               payment_status <> PAY_STATUS_VOID_OVERFLOW AND
4796               payment_status <> PAY_STATUS_SPOILED       AND
4797               payment_status <> PAY_STATUS_STOPPED       AND
4798               payment_status <> PAY_STATUS_INS_TERM      AND
4799               payment_status <> PAY_STATUS_REQ_TERM      AND
4800               payment_status <> PAY_STATUS_VOID          AND
4801               payment_status <> PAY_STATUS_ACK           AND
4802               payment_status <> PAY_STATUS_BNK_VALID     AND
4803               payment_status <> PAY_STATUS_PAID
4804              )
4805          ;
4806 
4807      EXCEPTION
4808 
4809          WHEN NO_DATA_FOUND THEN
4810              /*
4811               * Handle gracefully the situation where no payments
4812               * exist for the given request.
4813               */
4814              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4815 	             print_debuginfo(l_module_name, 'No payments in valid '
4816 	                 || 'status exist for payment instruction '
4817 	                 || p_instr_id
4818 	                 );
4819 
4820              END IF;
4821          WHEN OTHERS THEN
4822 
4823 	             print_debuginfo(l_module_name, 'Fatal: Exception occured '
4824 	                 || 'when attempting to update status of payments '
4825 	                 || 'for payment instruction '
4826 	                 || p_instr_id
4827 	                 || '. Aborting program ..'
4828 	                 );
4829 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
4830 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
4831 
4832              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
4833 
4834              /*
4835               * Propogate exception to caller.
4836               */
4837              RAISE;
4838 
4839      END;
4840 
4841      BEGIN
4842 
4843          UPDATE
4844              IBY_DOCS_PAYABLE_ALL
4845          SET
4846              document_status = DOC_STATUS_INS_TERM,
4847 
4848              /*
4849               * Fix for bug 4405981:
4850               *
4851               * The straight through flag should be set to 'N',
4852               * if the document was rejected / required manual
4853               * intervention.
4854               */
4855              straight_through_flag = 'N'
4856 
4857          WHERE
4858              payment_id IN
4859                  (
4860                   SELECT
4861                       payment_id
4862                   FROM
4863                       IBY_PAYMENTS_ALL
4864                   WHERE
4865                       payment_instruction_id = p_instr_id   AND
4866                       payment_status = PAY_STATUS_INS_TERM
4867                  )
4868                  AND
4869                  (
4870                   document_status <> DOC_STATUS_REJECTED    AND
4871                   document_status <> DOC_STATUS_REMOVED     AND
4872                   document_status <> DOC_STATUS_PMT_REMOVED AND
4873                   document_status <> DOC_STATUS_PMT_STOPPED AND
4874                   document_status <> DOC_STATUS_REQ_TERM    AND
4875                   document_status <> DOC_STATUS_INS_TERM    AND
4876                   document_status <> DOC_STATUS_VOID_SETUP  AND
4877                   document_status <> DOC_STATUS_PMT_VOIDED
4878                  )
4879          ;
4880 
4881     EXCEPTION
4882 
4883          WHEN NO_DATA_FOUND THEN
4884              /*
4885               * Handle gracefully the situation where no documents
4886               * exist for the given payment instruciton.
4887               */
4888              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4889 	             print_debuginfo(l_module_name, 'No payments exist for payment '
4890 	                 || 'instruction '
4891 	                 || p_instr_id
4892 	                 );
4893              END IF;
4894              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
4895 
4896          WHEN OTHERS THEN
4897 
4898 	             print_debuginfo(l_module_name, 'Fatal: Exception occured '
4899 	                 || 'when attempting to update status of payments '
4900 	                 || 'for payment instruction '
4901 	                 || p_instr_id
4902 	                 || '. Aborting program ..'
4903 	                 );
4904 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
4905 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
4906 
4907              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
4908 
4909              /*
4910               * Propogate exception to caller.
4911               */
4912              RAISE;
4913 
4914      END;
4915 
4916 
4917 
4918      /*
4919       * STEP 3:
4920       * Restore the Unused Check numbers and
4921       * Unlock the payment document that has been locked by this payment
4922       * instruction.
4923       */
4924      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4925 	     print_debuginfo(l_module_name, 'Restoring the Unused Check numbers '
4926 	         || 'by payment instruction '
4927 	         || p_instr_id
4928 	         );
4929 
4930      END IF;
4931 
4932 /*Restoring the Unused Check numbers*/
4933 /*Modified for Bug 7325373*/
4934      BEGIN
4935          BEGIN
4936                 SELECT last_issued_document_number INTO l_last_issued
4937                 FROM ce_payment_documents WHERE payment_instruction_id = p_instr_id;
4938              EXCEPTION
4939                 WHEN No_Data_Found THEN
4940                   l_document_locked := FALSE;
4941               IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4942 	              print_debuginfo(l_module_name, 'Payment Document is not locked by the instruction '
4943 	             || p_instr_id
4944 	             );
4945               END IF;
4946          END;
4947 
4948 	 /* If document is locked*/
4949          IF(l_document_locked = TRUE) THEN
4950 
4951 		SELECT Max(paper_document_number), Min(paper_document_number), Count(*)
4952 		INTO l_max_paper_document_number, l_min_paper_document_number, l_number_of_payments
4953 		FROM iby_payments_all WHERE payment_instruction_id = p_instr_id;
4954 
4955                 l_last_issued_modified := l_last_issued - l_number_of_payments;
4956 
4957 		 IF ((l_last_issued <> l_max_paper_document_number)
4958 		    OR ((l_max_paper_document_number - l_min_paper_document_number) <>  (l_number_of_payments - 1)))
4959 		 THEN
4960 		       RAISE l_irregular_document_numbers;
4961 		 END IF;
4962 
4963 		 UPDATE
4964 		     CE_PAYMENT_DOCUMENTS
4965 		 SET
4966 		     last_issued_document_number = l_last_issued_modified
4967 		 WHERE
4968 		     payment_instruction_id = p_instr_id
4969 		 RETURNING
4970 		     payment_document_id,
4971 		     payment_document_name
4972 		 INTO
4973 		     l_pmt_doc_id,
4974 		     l_pmt_doc_name
4975 		 ;
4976 
4977 		     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4978 			     print_debuginfo(l_module_name, 'Check number restored for Payment document id '
4979 			     || l_pmt_doc_id
4980 			     || ' with name '
4981 			     || l_pmt_doc_name
4982 			     );
4983 
4984 
4985 		     END IF;
4986 
4987                /* Updating payments table to clear the paper document numbers Bug 8412987*/
4988 	       /* bug : 8787079 Including 'Void_by_overflow' and 'Void_by_setup' statuses */
4989 		 UPDATE
4990 		     IBY_PAYMENTS_ALL
4991 		 SET
4992 		     paper_document_number = null
4993 		 WHERE
4994 		     payment_instruction_id = p_instr_id     AND
4995 		     payment_status in
4996 		                    (PAY_STATUS_INS_TERM,
4997 				     PAY_STATUS_VOID_SETUP,
4998                                      PAY_STATUS_VOID_OVERFLOW)
4999 		 ;
5000 
5001 		     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5002 			     print_debuginfo(l_module_name, 'nulled out paper document numbers in iby_payments_all');
5003 
5004 		     END IF;
5005         END IF;
5006      EXCEPTION
5007         WHEN l_irregular_document_numbers THEN
5008 
5009 	            print_debuginfo(l_module_name, 'Fatal: Exception occured '
5010 	             || 'while restoring the used check numbers.'
5011 	             || p_instr_id
5012 	             );
5013 
5014             RAISE;
5015 
5016         WHEN OTHERS THEN
5017              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5018 	             print_debuginfo(l_module_name, 'Non-Fatal: Exception occured '
5019 	             || 'while restoring the used check numbers. '
5020 	             || p_instr_id
5021 	             );
5022 
5023              END IF;
5024      END;
5025 
5026      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5027 	     print_debuginfo(l_module_name, 'Unlocking payment document locked '
5028 	         || 'by payment instruction '
5029 	         || p_instr_id
5030 	         );
5031      END IF;
5032 
5033 /* Unlocking the payment document*/
5034      BEGIN
5035 
5036          UPDATE
5037              CE_PAYMENT_DOCUMENTS
5038          SET
5039              payment_instruction_id = NULL
5040          WHERE
5041              payment_instruction_id = p_instr_id
5042          RETURNING
5043              payment_document_id,
5044              payment_document_name
5045          INTO
5046              l_pmt_doc_id,
5047              l_pmt_doc_name
5048          ;
5049 
5050          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5051 	         print_debuginfo(l_module_name, 'Payment document id '
5052 	             || l_pmt_doc_id
5053 	             || ' with name '
5054 	             || l_pmt_doc_name
5055 	             || ' unlocked successfully.'
5056 	             );
5057 
5058          END IF;
5059      EXCEPTION
5060          WHEN OTHERS THEN
5061 
5062          /*
5063           * This is a no-fatal exception. We will log the exception
5064           * and return.
5065           */
5066          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5067 	         print_debuginfo(l_module_name, 'Non-Fatal: Exception occured '
5068 	             || 'when unlocking pmt document locked by pmt instruction '
5069 	             || p_instr_id
5070 	             );
5071 
5072 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
5073 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
5074          END IF;
5075          FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
5076 
5077      END;
5078 
5079      /*
5080       * STEP 4:
5081       *
5082       * Invoke callouts to inform calling applications about the
5083       * terminated documents payable.
5084       */
5085 
5086      /*
5087       * Pick up the names of all the applications which have
5088       * payments in the current payment instruction.
5089       *
5090       * Remember, one payment instruction can contain payments
5091       * across multiple calling applications.
5092       */
5093      OPEN  c_app_names (p_instr_id);
5094      FETCH c_app_names BULK COLLECT INTO l_appNamesTab;
5095      CLOSE c_app_names;
5096 
5097      /*
5098       * Pick up the ids of all the applications which have
5099       * payments in the current payment instruction.
5100       *
5101       * Remember, one payment instruction can contain payments
5102       * across multiple calling applications.
5103       */
5104      OPEN  c_app_ids (p_instr_id);
5105      FETCH c_app_ids BULK COLLECT INTO l_appIdsTab;
5106      CLOSE c_app_ids;
5107 
5108      /*
5109       * This should normally never happen.
5110       */
5111      IF (l_appIdsTab.COUNT = 0) THEN
5112 
5113 
5114 	         print_debuginfo(l_module_name, 'No calling application ids '
5115 	             || 'were fetched for payment instruction '
5116 	             || p_instr_id
5117 	             || ' Possible data corruption. Raising exception ..'
5118 	             );
5119 
5120 
5121          APP_EXCEPTION.RAISE_EXCEPTION;
5122 
5123      END IF;
5124 
5125      /*
5126       * Loop through all the application names, invoking the
5127       * callout of each application.
5128       */
5129      FOR i IN l_appNamesTab.FIRST .. l_appNamesTab.LAST LOOP
5130 
5131          /*
5132           * Get the constructed package name to use in the
5133           * call out.
5134           */
5135          l_pkg_name := construct_callout_pkg_name(l_appNamesTab(i));
5136 
5137          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5138 	         print_debuginfo(l_module_name, 'Constructed package name: '
5139 	             || l_pkg_name);
5140 
5141          END IF;
5142          IF (l_pkg_name IS NULL) THEN
5143 
5144 
5145 	             print_debuginfo(l_module_name, 'Package name is null. '
5146 	                 || 'Raising exception.');
5147 
5148 
5149              APP_EXCEPTION.RAISE_EXCEPTION;
5150 
5151          END IF;
5152 
5153          /*
5154           * Get the next available rejected document group id.
5155           */
5156          SELECT
5157              IBY_REJECTED_DOCS_GROUP_S.NEXTVAL
5158          INTO
5159              l_rejection_id
5160          FROM
5161              DUAL
5162          ;
5163 
5164          /*
5165           * Update the terminated documents for this calling app
5166           * with the rejected document group id. The calling
5167           * application will identify rejected documents using
5168           * this id.
5169           */
5170          UPDATE
5171              IBY_DOCS_PAYABLE_ALL
5172          SET
5173              rejected_docs_group_id = l_rejection_id
5174          WHERE
5175              document_status  = DOC_STATUS_INS_TERM AND
5176              calling_app_id   = l_appIdsTab(i)      AND
5177              payment_id IN
5178                  (SELECT
5179                       payment_id
5180                   FROM
5181                       IBY_PAYMENTS_ALL
5182                   WHERE
5183                       payment_instruction_id = p_instr_id    AND
5184                       payment_status = PAY_STATUS_INS_TERM
5185                   )
5186          ;
5187 
5188          /*
5189           * Now try to call the external app's implementation of the hook.
5190           * The calling app may or may not have implemented the hook, so
5191           * it's not fatal if the implementation does not exist.
5192           */
5193          l_callout_name := l_pkg_name || '.' || 'documents_payable_rejected';
5194 
5195          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5196 	         print_debuginfo(l_module_name, 'Attempting to invoke callout: '
5197 	             || l_callout_name);
5198 
5199          END IF;
5200          l_stmt := 'CALL '|| l_callout_name || '(:1, :2, :3, :4, :5, :6, :7)';
5201 
5202          BEGIN
5203 
5204              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5205 	             print_debuginfo(l_module_name, 'Parameter passed to callout - '
5206 	                 || 'l_rejection_id: '
5207 	                 || l_rejection_id
5208 	                 );
5209 
5210              END IF;
5211              EXECUTE IMMEDIATE
5212                  (l_stmt)
5213              USING
5214                  IN  l_api_version,
5215                  IN  FND_API.G_FALSE,
5216                  IN  FND_API.G_FALSE,
5217                  OUT x_return_status,
5218                  OUT l_msg_count,
5219                  OUT l_msg_data,
5220                  IN  l_rejection_id
5221              ;
5222 
5223              /*
5224               * If the called procedure did not return success,
5225               * raise an exception.
5226               */
5227              IF (x_return_status IS NULL OR
5228                  x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5229 
5230 
5231 	                 print_debuginfo(l_module_name, 'Fatal: External app callout '''
5232 	                     || l_callout_name
5233 	                     || ''', returned failure status - '
5234 	                     || x_return_status
5235 	                     || '. Raising exception.'
5236 	                     );
5237 
5238 
5239                  APP_EXCEPTION.RAISE_EXCEPTION;
5240 
5241              END IF;
5242 
5243          EXCEPTION
5244 
5245              WHEN PROCEDURE_NOT_IMPLEMENTED THEN
5246                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5247 	                 print_debuginfo(l_module_name, 'Callout "' || l_callout_name
5248 	                     || '" not implemented by calling app '
5249 	                     || l_appNamesTab(i) || '.');
5250 
5251 	                 print_debuginfo(l_module_name, 'Skipping hook call.');
5252 
5253 
5254                  END IF;
5255              WHEN OTHERS THEN
5256 
5257 	                 print_debuginfo(l_module_name, 'Fatal: External app callout '''
5258 	                     || l_callout_name
5259 	                     || ''', generated exception.'
5260 	                     );
5261 
5262 	                 print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
5263 	                 print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
5264 
5265                  FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
5266 
5267                  /*
5268                   * Propogate exception to caller.
5269                   */
5270                  RAISE;
5271 
5272          END;
5273 
5274      END LOOP;
5275 
5276      /*
5277       * STEP 5:
5278       *
5279       * Clean up the pmt instruction by unstamping it. In case the
5280       * concurrent request that was handling this pmt instruction
5281       * had aborted with an exception, this step will clean up the data.
5282       */
5283 
5284      /*
5285       * Fix for bug 5206672:
5286       *
5287       * If we reached here, then the payment instruction has
5288       * been updated to TERMINATED status and the calling app
5289       * has been informed.
5290       *
5291       * Perform a COMMIT here before calling unlock_pmt_entity(..)
5292       * otherwise a deadlock ensues.
5293       */
5294      COMMIT;
5295 
5296      unlock_pmt_entity(
5297          p_instr_id,
5298          'PAYMENT_INSTRUCTION',
5299          l_ret_status
5300          );
5301 
5302      x_return_status := FND_API.G_RET_STS_SUCCESS;
5303 
5304      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5305 	     print_debuginfo(l_module_name, 'EXIT');
5306 
5307      END IF;
5308  EXCEPTION
5309 
5310      WHEN OTHERS THEN
5311 
5312          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5313 	         print_debuginfo(l_module_name, 'Exception occured when '
5314 	             || 'terminating payment instruction '
5315 	             || p_instr_id
5316 	             || ', with status '
5317 	             || p_instr_status
5318 	             );
5319 
5320 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
5321 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
5322          END IF;
5323          FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
5324 
5325          x_return_status := FND_API.G_RET_STS_ERROR;
5326 
5327          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5328 	         print_debuginfo(l_module_name, 'EXIT');
5329 
5330          END IF;
5331  END terminate_pmt_instruction;
5332 
5333 /*--------------------------------------------------------------------
5334  | NAME:
5335  |     terminate_pmt_request
5336  |
5337  |
5338  | PURPOSE:
5339  |
5340  |
5341  | PARAMETERS:
5342  |     IN
5343  |
5344  |
5345  |     OUT
5346  |
5347  |
5348  | RETURNS:
5349  |
5350  | NOTES:
5351  |   Internal API, not for public use.
5352  |
5353  | UPDATE ON MAY-05-2006, rameshsh
5354  | This method was not performing a COMMIT earlier. Due to bug 5206672
5355  | a COMMIT has been added in the code before invoking unlock_pmt_entity(..).
5356  |
5357  *---------------------------------------------------------------------*/
5358  PROCEDURE terminate_pmt_request (
5359      p_req_id         IN NUMBER,
5360      p_req_status     IN VARCHAR2,
5361      x_return_status  OUT NOCOPY VARCHAR2
5362      )
5363  IS
5364 
5365  l_module_name    CONSTANT VARCHAR2(200) := G_PKG_NAME
5366                                             || '.terminate_pmt_request';
5367  l_straight_thr_proc VARCHAR2(1);
5368 
5369  l_rejection_id   NUMBER(15);
5370 
5371  /* used in forming callout procedure name */
5372  l_calling_app_id NUMBER;
5373  l_app_short_name VARCHAR2(200);
5374  l_pkg_name       VARCHAR2(200);
5375  l_callout_name   VARCHAR2(500);
5376  l_stmt           VARCHAR2(1000);
5377 
5378  /* used in invocation of callout procedure */
5379  l_api_version    CONSTANT NUMBER := 1.0;
5380  l_msg_count      NUMBER;
5381  l_msg_data       VARCHAR2(2000);
5382 
5383  l_flag           BOOLEAN := FALSE;
5384 
5385  l_error_code     VARCHAR2(3000);
5386  l_ret_status     VARCHAR2(300);
5387  l_allowed VARCHAR2(20):= 'YES';
5388  l_fd_count       NUMBER;
5389 
5390 CURSOR pmt_instructions(l_ppr_id number)
5391 is
5392 select instr.payment_instruction_id, instr.payment_instruction_status
5393 from iby_pay_instructions_all instr
5394 where exists (select 'Payment' from iby_payments_all pmt
5395 where pmt.payment_service_request_id = l_ppr_id
5396 and pmt.payment_instruction_id = instr.payment_instruction_id);
5397 
5398 
5399 
5400  /*
5401   * Implementing the callout is optional for the calling app.
5402   * If the calling app does not implement the hook, then
5403   * the call to the hook will result in ORA-06576 error.
5404   *
5405   * There is no exception name associated with this code, so
5406   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
5407   * exception occurs, it is not fatal: we log the error and
5408   * proceed.
5409   *
5410   * If, on the other hand, the calling app implements the
5411   * callout, but the callout throws an exception, it is fatal
5412   * and we must abort the program (this will be caught
5413   * in WHEN OTHERS block).
5414   */
5415  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
5416  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
5417 
5418  BEGIN
5419 
5420      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5421 	     print_debuginfo(l_module_name, 'ENTER');
5422 
5423 	     print_debuginfo(l_module_name, 'Parameters passed - '
5424 	         || 'payment request id: '
5425 	         || p_req_id
5426 	         || ', payment request status: '
5427 	         || p_req_status
5428 	         );
5429 
5430      END IF;
5431      FND_MSG_PUB.initialize;
5432 
5433      /*
5434       * STATUS CHANGE:
5435       *
5436       * API Responsibility:
5437       * request status  = TERMINATED
5438       * payment_status  = REMOVED_REQUEST_TERMINATED
5439       * document_status = REMOVED_REQUEST_TERMINATED
5440       *
5441       */
5442 
5443      /*
5444       * STEP 1:
5445       *
5446       * If the given payment request is locked, it means
5447       * that some concurrent program is acting upon the
5448       * ppr at the moment.
5449       *
5450       * To prevent data corruption do not allow the payment
5451       * request to be terminated.
5452       */
5453      l_flag := checkIfPmtEntityLocked(p_req_id, 'PAYMENT_REQUEST');
5454 
5455      IF (l_flag = TRUE) THEN
5456 
5457          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5458 	         print_debuginfo(l_module_name, 'Payment request '
5459 	             || p_req_id
5460 	             || ' has been locked by a concurrent program.'
5461 	             || ' It cannot be terminated at present.'
5462 	             );
5463 
5464          END IF;
5465          x_return_status := FND_API.G_RET_STS_ERROR;
5466 
5467          l_error_code := 'IBY_PPR_LOCKED';
5468          FND_MESSAGE.set_name('IBY', l_error_code);
5469          FND_MSG_PUB.ADD;
5470 
5471          --FND_MSG_PUB.COUNT_AND_GET(
5472          --    p_count => x_msg_count,
5473          --    p_data  => x_msg_data
5474          --    );
5475 
5476          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5477 	         print_debuginfo(l_module_name, 'Returning error response ..');
5478 
5479 	         print_debuginfo(l_module_name, 'EXIT');
5480 
5481          END IF;
5482          RETURN;
5483 
5484      ELSE
5485 
5486          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5487 	         print_debuginfo(l_module_name, 'Payment request '
5488 	             || p_req_id
5489 	             || ' is not locked.'
5490 	             );
5491 
5492          END IF;
5493      END IF;
5494 
5495      /*
5496       * Fix for bug  5112559:
5497       *
5498       * Before performing any action, first check if any of
5499       * the payments of this ppr are part of a payment instruction,
5500       * if so, do not allow the ppr to be terminated.
5501       *
5502       *
5503       * Fix for Bug 9277808:
5504       *
5505       */
5506      l_flag := checkIfPmtInInstExists(p_req_id);
5507 
5508      IF (l_flag = TRUE) THEN
5509 
5510          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5511 	         print_debuginfo(l_module_name, 'The payment request '
5512 	             || p_req_id
5513 	             || ' contains at least one payment that is '
5514 	             || 'part of a payment instruction. Payment '
5515 	             || 'request cannot be terminated at this stage.'
5516 	             );
5517 
5518          END IF;
5519 
5520          SELECT CREATE_PMT_INSTRUCTIONS_FLAG INTO l_straight_thr_proc
5521 	 FROM IBY_PAY_SERVICE_REQUESTS
5522 	 WHERE PAYMENT_SERVICE_REQUEST_ID = p_req_id;
5523 
5524          /* If instructions are not created for this batch alone,
5525 	  * this ppr cannot be terminated.
5526 	  * User has to proceed with the termination of individual
5527 	  * instructions
5528 	  */
5529 	 IF l_straight_thr_proc <> 'Y' THEN
5530 		 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5531 			 print_debuginfo(l_module_name, 'Instructions may contain payments related'||
5532 			                                ' to other PPRs');
5533 			 print_debuginfo(l_module_name, 'Returning error message..');
5534 
5535 		 END IF;
5536          x_return_status := FND_API.G_RET_STS_ERROR;
5537          l_error_code := 'IBY_PPR_TERM_NOT_ALLOWED';
5538          FND_MESSAGE.set_name('IBY', l_error_code);
5539          FND_MSG_PUB.ADD;
5540 
5541 	 ELSE
5542 	        /* Sanity Check to confirm if all the instructions
5543 		 * could be terminated
5544 		 */
5545 		FOR pmt_instr in pmt_instructions(p_req_id) loop
5546                         l_allowed := IBY_FD_USER_API_PUB.Pmt_Instr_Terminate_Allowed(
5547 		                           pmt_instr.payment_instruction_id);
5548 			IF(l_allowed = 'NO') THEN
5549 				 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5550 					 print_debuginfo(l_module_name, 'Payment instruction '||
5551 									pmt_instr.payment_instruction_id||
5552 									' can not be terminated');
5553 					 print_debuginfo(l_module_name, 'Returning error message..');
5554 
5555 				 END IF;
5556 			         x_return_status := FND_API.G_RET_STS_ERROR;
5557 				 l_error_code := 'IBY_PPR_TERM_NOT_ALLOWED';
5558 				 FND_MESSAGE.set_name('IBY', l_error_code);
5559 				 FND_MSG_PUB.ADD;
5560 				 return;
5561 			END IF;
5562 		end loop;
5563 
5564 
5565 	        /* Terminating each Payment instruction created for this PPR
5566 		 */
5567                 FOR pmt_instr in pmt_instructions(p_req_id) loop
5568                        terminate_pmt_instruction(pmt_instr.payment_instruction_id,
5569 		                                 pmt_instr.payment_instruction_status,
5570 						 x_return_status);
5571                        IF(x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5572 				 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5573 					 print_debuginfo(l_module_name, 'Exception while terminating Payment instruction '||
5574 									pmt_instr.payment_instruction_id);
5575 					 print_debuginfo(l_module_name, 'Returning error message..');
5576 
5577 				 END IF;
5578 		            x_return_status := FND_API.G_RET_STS_ERROR;
5579 			    return;
5580 		       END IF;
5581 		end loop;
5582 
5583 	 END IF;
5584 
5585        COMMIT;
5586 
5587      x_return_status := FND_API.G_RET_STS_SUCCESS;
5588 
5589      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5590 	     print_debuginfo(l_module_name, 'EXIT');
5591 
5592      END IF;
5593 
5594 
5595      RETURN;
5596 
5597      END IF;
5598 
5599      /*
5600       * STEP 2:
5601       *
5602       * Set the request status to TERMINATED.
5603       */
5604      UPDATE
5605          iby_pay_service_requests
5606      SET
5607          payment_service_request_status = REQ_STATUS_TERMINATED,
5608 	 last_update_date = sysdate,
5609 	 LAST_UPDATED_BY = NVL(FND_GLOBAL.user_id,-1),
5610 	 LAST_UPDATE_LOGIN = NVL(FND_GLOBAL.login_id, -1)
5611      WHERE
5612          payment_service_request_id = p_req_id
5613      ;
5614   Select count(1)   --Bug 14335525
5615   into l_fd_count
5616   from iby_fd_docs_payable_v
5617   where payment_service_request_id = p_req_id;
5618 
5619   IF l_fd_count <> 0 THEN
5620 
5621      BEGIN
5622 
5623          UPDATE
5624              IBY_PAYMENTS_ALL
5625          SET
5626              payment_status = PAY_STATUS_REQ_TERM
5627          WHERE
5628              payment_service_request_id = p_req_id       AND
5629              (
5630               payment_status <> PAY_STATUS_REMOVED       AND
5631               payment_status <> PAY_STATUS_VOID_SETUP    AND
5632               payment_status <> PAY_STATUS_VOID_OVERFLOW AND
5633               payment_status <> PAY_STATUS_SPOILED       AND
5634               payment_status <> PAY_STATUS_STOPPED       AND
5635               payment_status <> PAY_STATUS_INS_TERM      AND
5636               payment_status <> PAY_STATUS_REQ_TERM      AND
5637               payment_status <> PAY_STATUS_VOID          AND
5638               payment_status <> PAY_STATUS_ACK           AND
5639               payment_status <> PAY_STATUS_BNK_VALID     AND
5640               payment_status <> PAY_STATUS_PAID
5641              )
5642          ;
5643 
5644      EXCEPTION
5645 
5646          WHEN NO_DATA_FOUND THEN
5647              /*
5648               * Handle gracefully the situation where no payments
5649               * exist for the given request.
5650               */
5651              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5652 	             print_debuginfo(l_module_name, 'No payments exist for payment '
5653 	                 || 'request '
5654 	                 || p_req_id
5655 	                 );
5656 
5657 
5658              END IF;
5659          WHEN OTHERS THEN
5660 
5661 	             print_debuginfo(l_module_name, 'Fatal: Exception occured '
5662 	                 || 'when attempting to update status of payments '
5663 	                 || 'for payment request '
5664 	                 || p_req_id
5665 	                 || '. Aborting program ..'
5666 	                 );
5667 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
5668 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
5669 
5670              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
5671 
5672              /*
5673               * Propogate exception to caller.
5674               */
5675              RAISE;
5676 
5677      END;
5678 
5679      /*
5680       * STEP 3:
5681       *
5682       * Set status of documents to TERMINATED.
5683       */
5684      BEGIN
5685 
5686          UPDATE
5687              IBY_DOCS_PAYABLE_ALL
5688          SET
5689              document_status = DOC_STATUS_REQ_TERM,
5690 
5691              /*
5692               * Fix for bug 4405981:
5693               *
5694               * The straight through flag should be set to 'N',
5695               * if the document was rejected / required manual
5696               * intervention.
5697               */
5698              straight_through_flag = 'N'
5699          WHERE
5700              payment_service_request_id = p_req_id   AND
5701              (
5702               document_status <> DOC_STATUS_REJECTED    AND
5703               document_status <> DOC_STATUS_REMOVED     AND
5704               document_status <> DOC_STATUS_PMT_REMOVED AND
5705               document_status <> DOC_STATUS_PMT_STOPPED AND
5706               document_status <> DOC_STATUS_REQ_TERM    AND
5707               document_status <> DOC_STATUS_INS_TERM    AND
5708               document_status <> DOC_STATUS_VOID_SETUP  AND
5709               document_status <> DOC_STATUS_PMT_VOIDED
5710              )
5711          ;
5712 
5713      EXCEPTION
5714 
5715          WHEN NO_DATA_FOUND THEN
5716 
5717              /*
5718               * Handle gracefully the situation where no documents payable
5719               * exist for the given request.
5720               */
5721              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5722 	             print_debuginfo(l_module_name, 'No docs payable exist for payment '
5723 	                 || 'request '
5724 	                 || p_req_id
5725 	                 || '. Exiting ..'
5726 	                 );
5727 
5728              END IF;
5729              /*
5730               * If no documents payable exist for the given request,
5731               * it is not worth proceeding further.
5732               *
5733               * Return success status to the caller.
5734               */
5735              x_return_status := FND_API.G_RET_STS_SUCCESS;
5736 
5737              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5738 	             print_debuginfo(l_module_name, 'EXIT');
5739              END IF;
5740              RETURN;
5741 
5742          WHEN OTHERS THEN
5743 
5744 	             print_debuginfo(l_module_name, 'Fatal: Exception occured '
5745 	                 || 'when attempting to update status of documents '
5746 	                 || 'payable for payment request '
5747 	                 || p_req_id
5748 	                 || '. Aborting program ..'
5749 	                 );
5750 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
5751 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
5752 
5753              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
5754 
5755              /*
5756               * Propogate exception to caller.
5757               */
5758              RAISE;
5759 
5760      END;
5761 
5762      /*
5763       * STEP 4:
5764       *
5765       * Inform calling application about rejected documents.
5766       */
5767 
5768      /*
5769       * Get the next available rejected document group id.
5770       */
5771      SELECT
5772          IBY_REJECTED_DOCS_GROUP_S.NEXTVAL
5773      INTO
5774          l_rejection_id
5775      FROM
5776          DUAL
5777      ;
5778 
5779      /*
5780       * Update the terminated documents with the rejected document
5781       * group id. The calling application will identify rejected
5782       * documents using this id.
5783       */
5784      UPDATE
5785          IBY_DOCS_PAYABLE_ALL
5786      SET
5787          rejected_docs_group_id = l_rejection_id
5788      WHERE
5789          document_status            = DOC_STATUS_REQ_TERM AND
5790          payment_service_request_id = p_req_id
5791      ;
5792 
5793      /*
5794       * Get the application name of the calling app. This
5795       * will be used in the callout.
5796       */
5797      SELECT
5798          fnd.application_short_name
5799      INTO
5800          l_app_short_name
5801      FROM
5802          FND_APPLICATION          fnd,
5803          IBY_PAY_SERVICE_REQUESTS req
5804      WHERE
5805          fnd.application_id             = req.calling_app_id AND
5806          req.payment_service_request_id = p_req_id
5807      ;
5808 
5809      /*
5810       * Get the constructed package name to use in the
5811       * call out.
5812       */
5813      l_pkg_name := construct_callout_pkg_name(l_app_short_name);
5814 
5815      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5816 	     print_debuginfo(l_module_name, 'Constructed package name: '
5817 	         || l_pkg_name);
5818 
5819      END IF;
5820      IF (l_pkg_name IS NULL) THEN
5821 
5822 
5823 	         print_debuginfo(l_module_name, 'Package name is null. '
5824 	             || 'Raising exception.');
5825 
5826 
5827          APP_EXCEPTION.RAISE_EXCEPTION;
5828 
5829      END IF;
5830 
5831      /*
5832       * Now try to call the external app's implementation of the hook.
5833       * The calling app may or may not have implemented the hook, so
5834       * it's not fatal if the implementation does not exist.
5835       */
5836      l_callout_name := l_pkg_name || '.' || 'documents_payable_rejected';
5837 
5838      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5839 	     print_debuginfo(l_module_name, 'Attempting to invoke callout: '
5840 	         || l_callout_name);
5841 
5842 	     print_debuginfo(l_module_name, 'Parameter(s) passed to callout - '
5843 	         || 'rejection id: '
5844 	         || l_rejection_id
5845 	         );
5846 
5847      END IF;
5848      l_stmt := 'CALL '|| l_callout_name || '(:1, :2, :3, :4, :5, :6, :7)';
5849 
5850      BEGIN
5851 
5852          EXECUTE IMMEDIATE
5853              (l_stmt)
5854          USING
5855              IN  l_api_version,
5856              IN  FND_API.G_FALSE,
5857              IN  FND_API.G_FALSE,
5858              OUT x_return_status,
5859              OUT l_msg_count,
5860              OUT l_msg_data,
5861              IN  l_rejection_id
5862          ;
5863 
5864          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5865 	         print_debuginfo(l_module_name, 'Parameter(s) returned by callout - '
5866 	         || 'x_return_status: '
5867 	         || x_return_status
5868 	         );
5869 
5870          END IF;
5871          /*
5872           * If the called procedure did not return success,
5873           * raise an exception.
5874           */
5875          IF (x_return_status IS NULL OR
5876              x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5877 
5878 
5879 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
5880 	                 || l_callout_name
5881 	                 || ''', returned failure status - '
5882 	                 || x_return_status
5883 	                 || '. Raising exception.'
5884 	                 );
5885 
5886 
5887              APP_EXCEPTION.RAISE_EXCEPTION;
5888 
5889          END IF;
5890 
5891      EXCEPTION
5892 
5893          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
5894              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5895 	             print_debuginfo(l_module_name, 'Callout "' || l_callout_name
5896 	                 || '" not implemented by calling app '
5897 	                 || l_app_short_name || '.');
5898 
5899 	             print_debuginfo(l_module_name, 'Skipping hook call.');
5900 
5901 
5902              END IF;
5903          WHEN OTHERS THEN
5904 
5905 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
5906 	                 || l_callout_name
5907 	                 || ''', generated exception.'
5908 	                 );
5909 
5910 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
5911 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
5912 
5913              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
5914 
5915              /*
5916               * Propogate exception to caller.
5917               */
5918              RAISE;
5919      END;
5920 
5921      /*
5922       * STEP 5:
5923       *
5924       * Clean up the ppr by unstamping it. In case the
5925       * concurrent request that was handling this ppr
5926       * had aborted with an exception, this step will
5927       * clean up the data.
5928       */
5929 
5930      /*
5931       * Fix for bug 5206672:
5932       *
5933       * If we reached here, then the payment request has
5934       * been updated to TERMINATED status and the calling app
5935       * has been informed.
5936       *
5937       * Perform a COMMIT here before calling unlock_pmt_entity(..)
5938       * otherwise a deadlock ensues.
5939       */
5940      COMMIT;
5941 
5942      unlock_pmt_entity(
5943          p_req_id,
5944          'PAYMENT_REQUEST',
5945          l_ret_status
5946          );
5947 
5948      x_return_status := FND_API.G_RET_STS_SUCCESS;
5949 
5950      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5951 	     print_debuginfo(l_module_name, 'EXIT');
5952 
5953      END IF;
5954  END IF;
5955  EXCEPTION
5956 
5957      WHEN OTHERS THEN
5958 
5959          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5960 	         print_debuginfo(l_module_name, 'Exception occured when '
5961 	             || 'terminating payment request '
5962 	             || p_req_id
5963 	             || ', with status '
5964 	             || p_req_status
5965 	             );
5966 
5967 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
5968 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
5969          END IF;
5970          FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
5971 
5972          x_return_status := FND_API.G_RET_STS_ERROR;
5973 
5974          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5975 	         print_debuginfo(l_module_name, 'EXIT');
5976 
5977          END IF;
5978  END terminate_pmt_request;
5979 
5980 /*--------------------------------------------------------------------
5981  | NAME:
5982  |     resubmit_pmt_request
5983  |
5984  |
5985  | PURPOSE:
5986  |
5987  |
5988  | PARAMETERS:
5989  |     IN
5990  |     p_payreq_id     The id of the payment request that needs to be
5991  |                     re-processed by the Build Program.
5992  |     OUT
5993  |     x_conc_req_id   The id of the concurrent request that was launched
5994  |                     by this API. The user will have to check this
5995  |                     concurrent request status via SRS UI to know the
5996  |                     result of the concurrent request. This concurrent
5997  |                     request will invoke the Build Program for this
5998  |                     payment request.
5999  |     x_error_buf     message buffer that stores the cause of the error.
6000  |     x_return_status '-1' will be returned in the case of an error
6001  |                     '0' will be returned if the request completed
6002  |                     successfully.
6003  |
6004  | RETURNS:
6005  |
6006  | NOTES:
6007  |     This method will perform a COMMIT after each functional
6008  |     flow is complete. The caller should have finished all database
6009  |     operations before making a call this method. The caller should
6010  |     not call commit after invoking this method.
6011  |
6012  *---------------------------------------------------------------------*/
6013  PROCEDURE resubmit_pmt_request (
6014      p_payreq_id     IN NUMBER,
6015      x_conc_req_id   IN OUT NOCOPY NUMBER,
6016      x_error_buf     IN OUT NOCOPY VARCHAR2,
6017      x_return_status IN OUT NOCOPY NUMBER
6018      )
6019  IS
6020 
6021  l_module_name   CONSTANT VARCHAR2(200) := G_PKG_NAME
6022                                            || '.resubmit_pmt_request';
6023 
6024  l_req_attribs   IBY_PAY_SERVICE_REQUESTS%ROWTYPE;
6025  l_ret_status    VARCHAR2(200);
6026 
6027     l_icx_numeric_characters   VARCHAR2(30); -- Bug 6411356
6028     l_bool_val   boolean;  -- Bug 6411356
6029 
6030  BEGIN
6031 
6032      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6033 	     print_debuginfo(l_module_name, 'ENTER');
6034 
6035      END IF;
6036      FND_MSG_PUB.initialize;
6037 
6038      /*
6039       * STATUS CHANGE:
6040       *
6041       * UI Responsibility:
6042       * request status = RETRY_DOCUMENT_VALIDATION    |
6043       *                  RETRY_PAYMENT_CREATION
6044       * payment_status = MODIFIED                     |
6045       *                  MODIFIED_PAYEE_BANK_ACCOUNT
6046       *
6047       * API Responsibility:
6048       * NONE
6049       */
6050 
6051      /*
6052       * Pick up the attributes of this request; these are
6053       * params like rejection level settings, review
6054       * payments setting etc.
6055       */
6056      SELECT
6057          payment_service_request_id,
6058          calling_app_id,
6059          call_app_pay_service_req_code,
6060          payment_service_request_status,
6061          process_type,
6062          allow_zero_payments_flag,
6063          created_by,
6064          creation_date,
6065          last_updated_by,
6066          last_update_date,
6067          object_version_number,
6068          last_update_login,
6069          internal_bank_account_id,
6070          payment_profile_id,
6071          maximum_payment_amount,
6072          minimum_payment_amount,
6073          document_rejection_level_code,
6074          payment_rejection_level_code,
6075          require_prop_pmts_review_flag,
6076          org_type,
6077          attribute_category,
6078          attribute1,
6079          attribute2,
6080          attribute3,
6081          attribute4,
6082          attribute5,
6083          attribute6,
6084          attribute7,
6085          attribute8,
6086          attribute9,
6087          attribute10,
6088          attribute11,
6089          attribute12,
6090          attribute13,
6091          attribute14,
6092          attribute15,
6093          create_pmt_instructions_flag,
6094          payment_document_id,
6095          request_id
6096      INTO
6097          l_req_attribs
6098      FROM
6099          IBY_PAY_SERVICE_REQUESTS
6100      WHERE
6101          PAYMENT_SERVICE_REQUEST_ID = p_payreq_id
6102      ;
6103 
6104      /*
6105       * Now resubmit the payment request by invoking
6106       * the Build Program functional flows again for
6107       * this request.
6108       *
6109       * This method will begin processing the payment
6110       * request from the last processed point. Therefore,
6111       * we need not bother about the last processed point
6112       * here.
6113       *
6114       * This will launch a concurrent request for processing
6115       * the payment request. We will return the concurrent
6116       * request id back to the user.
6117       */
6118 
6119 
6120     --Bug 6411356
6121     --below code added to set the current nls character setting
6122     --before submitting a child requests.
6123     fnd_profile.get('ICX_NUMERIC_CHARACTERS',l_icx_numeric_characters);
6124     l_bool_val:= FND_REQUEST.SET_OPTIONS( numeric_characters => l_icx_numeric_characters);
6125 
6126      x_conc_req_id := FND_REQUEST.SUBMIT_REQUEST(
6127                      'IBY',
6128                      'IBYBUILD',
6129                      '',
6130                      '',
6131                      FALSE,
6132                      ''||l_req_attribs.calling_app_id||'',
6133                      ''||l_req_attribs.call_app_pay_service_req_code||'',
6134                      ''||l_req_attribs.internal_bank_account_id||'',
6135                      ''||l_req_attribs.payment_profile_id||'',
6136                      ''||l_req_attribs.allow_zero_payments_flag||'',
6137                      ''||l_req_attribs.maximum_payment_amount||'',
6138                      ''||l_req_attribs.minimum_payment_amount||'',
6139                      ''||l_req_attribs.document_rejection_level_code||'',
6140                      ''||l_req_attribs.payment_rejection_level_code||'',
6141                      ''||l_req_attribs.require_prop_pmts_review_flag||'',
6142                      'N',
6143                      '', '', '', '', '', '', '', '', '', '',
6144                      '', '', '', '', '', '', '', '', '', '',
6145                      '', '', '', '', '', '', '', '', '', '',
6146                      '', '', '', '', '', '', '', '', '', '',
6147                      '', '', '', '', '', '', '', '', '', '',
6148                      '', '', '', '', '', '', '', '', '', '',
6149                      '', '', '', '', '', '', '', '', '', '',
6150                      '', '', '', '', '', '', '', '', '', '',
6151                      '', '', '', '', '', '', ''
6152                      );
6153 
6154      IF (x_conc_req_id = 0) THEN
6155 
6156 
6157 	         print_debuginfo(l_module_name, 'Concurrent program request failed.');
6158 
6159          APP_EXCEPTION.RAISE_EXCEPTION;
6160 
6161      ELSE
6162 
6163          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6164 	         print_debuginfo(l_module_name, 'The concurrent request was '
6165 	             || 'launched successfully. '
6166 	             || 'Check concurrent request id :'
6167 	             || to_char(x_conc_req_id)
6168 	             );
6169 
6170          END IF;
6171          /*
6172           * Lock the payment request so that the user is not
6173           * allowed to take any action upon this payment
6174           * request until the concurrent request just launched
6175           * has completed.
6176           */
6177          lock_pmt_entity(
6178              p_payreq_id,
6179              'PAYMENT_REQUEST',
6180              x_conc_req_id,
6181              l_ret_status
6182              );
6183 
6184          /*
6185           * If we are unable to lock the payment request, abort.
6186           */
6187          IF (l_ret_status <> FND_API.G_RET_STS_SUCCESS) THEN
6188 
6189 
6190 	             print_debuginfo(l_module_name, 'Unable to lock payment '
6191 	                 || 'process request: '
6192 	                 || p_payreq_id
6193 	                 || '.'
6194 	                 );
6195 
6196 
6197              APP_EXCEPTION.RAISE_EXCEPTION;
6198 
6199          END IF;
6200 
6201      END IF;
6202 
6203      x_return_status := 0;
6204      x_error_buf := 'BUILD PROGRAM INVOKED - RESUBMITTING PAYMENT'
6205          || ' REQUEST SUCCEEDED';
6206      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6207 	     print_debuginfo(l_module_name, 'EXIT');
6208 
6209      END IF;
6210  EXCEPTION
6211 
6212    WHEN OTHERS THEN
6213 
6214      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6215 	     print_debuginfo(l_module_name, 'Exception occured when resubmitting '
6216 	         || 'payment request id '
6217 	         || p_payreq_id
6218 	         );
6219 	     print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
6220 	     print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
6221      END IF;
6222      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
6223 
6224      x_error_buf := 'BUILD PROGRAM ERROR - RESUBMITTING PAYMENT'
6225          || ' REQUEST FAILED';
6226 
6227      x_return_status := -1;
6228 
6229      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6230 	     print_debuginfo(l_module_name, 'EXIT');
6231 
6232      END IF;
6233  END resubmit_pmt_request;
6234 
6235 /*--------------------------------------------------------------------
6236  | NAME:
6237  |     resubmit_instruction
6238  |
6239  |
6240  | PURPOSE:
6241  |
6242  |
6243  | PARAMETERS:
6244  |     IN
6245  |
6246  |
6247  |     OUT
6248  |
6249  |
6250  | RETURNS:
6251  |
6252  | NOTES:
6253  |   Internal API, not for public use.
6254  |
6255  *---------------------------------------------------------------------*/
6256  PROCEDURE resubmit_instruction (
6257      p_ins_id        IN NUMBER,
6258      x_conc_req_id   IN OUT NOCOPY NUMBER,
6259      x_error_buf     IN OUT NOCOPY VARCHAR2,
6260      x_return_status IN OUT NOCOPY NUMBER
6261      )
6262  IS
6263 
6264  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME || '.resubmit_instruction';
6265  l_str_ret_status       VARCHAR2(200);
6266  l_icx_numeric_characters   VARCHAR2(30); -- Bug 6411356
6267  l_bool_val   boolean;  -- Bug 6411356
6268 
6269  BEGIN
6270 
6271      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6272 	     print_debuginfo(l_module_name, 'ENTER');
6273 
6274      END IF;
6275      FND_MSG_PUB.initialize;
6276 
6277      /*
6278       * STATUS CHANGE:
6279       *
6280       * UI Responsibility:
6281       * instruction status = RETRY_CREATION
6282       *
6283       * API Responsibility:
6284       * NONE
6285       */
6286 
6287      /*
6288       * This will launch a concurrent request for processing
6289       * the payment instruction. We will return the concurrent
6290       * request id back to the user.
6291       */
6292 
6293 
6294     --Bug 6411356
6295     --below code added to set the current nls character setting
6296     --before submitting a child requests.
6297     fnd_profile.get('ICX_NUMERIC_CHARACTERS',l_icx_numeric_characters);
6298     l_bool_val:= FND_REQUEST.SET_OPTIONS( numeric_characters => l_icx_numeric_characters);
6299 
6300 
6301      x_conc_req_id := FND_REQUEST.SUBMIT_REQUEST(
6302                      'IBY',
6303                      'IBYREPICP',
6304                      '',
6305                      '',
6306                      FALSE,
6307                      ''||p_ins_id||'',
6308                      '', '', '', '', '', '', '', '', '', '',
6309                      '', '', '', '', '', '', '', '', '', '',
6310                      '', '', '', '', '', '', '', '', '', '',
6311                      '', '', '', '', '', '', '', '', '', '',
6312                      '', '', '', '', '', '', '', '', '', '',
6313                      '', '', '', '', '', '', '', '', '', '',
6314                      '', '', '', '', '', '', '', '', '', '',
6315                      '', '', '', '', '', '', '', '', '', '',
6316                      '', '', '', '', '', '', '', '', '', '',
6317                      '', '', '', '', '', '', ''
6318                      );
6319 
6320      IF (x_conc_req_id = 0) THEN
6321 
6322 
6323 	         print_debuginfo(l_module_name, 'Concurrent program request failed.');
6324 
6325          APP_EXCEPTION.RAISE_EXCEPTION;
6326 
6327      ELSE
6328 
6329          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6330 	         print_debuginfo(l_module_name, 'The concurrent request was '
6331 	             || 'launched successfully. '
6332 	             || 'Check concurrent request id :'
6333 	             || to_char(x_conc_req_id)
6334 	             );
6335 
6336          END IF;
6337          /*
6338           * Lock the payment instruction so that the user is not
6339           * allowed to take any action upon this payment
6340           * instruction until the concurrent request just launched
6341           * has completed.
6342           */
6343 
6344          /*
6345           * Fix for bug 5206725:
6346           *
6347           * Pass a VARCHAR data type for the return status
6348           * instead of a NUMBER data type.
6349           */
6350          lock_pmt_entity(
6351              p_ins_id,
6352              'PAYMENT_INSTRUCTION',
6353              x_conc_req_id,
6354              l_str_ret_status
6355              );
6356 
6357          IF (l_str_ret_status <> FND_API.G_RET_STS_SUCCESS) THEN
6358 
6359 
6360 	             print_debuginfo(l_module_name, 'Unable to lock payment '
6361 	                 || 'instruction: '
6362 	                 || p_ins_id
6363 	                 || '.'
6364 	                 );
6365 
6366 
6367              APP_EXCEPTION.RAISE_EXCEPTION;
6368 
6369          END IF;
6370 
6371      END IF;
6372 
6373      x_return_status := 0;
6374      x_error_buf := 'PICP INVOKED - RESUBMITTING PAYMENT'
6375          || ' INSTRUCTION SUCCEEDED';
6376 
6377      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6378 	     print_debuginfo(l_module_name, 'EXIT');
6379 
6380      END IF;
6381  EXCEPTION
6382 
6383    WHEN OTHERS THEN
6384 
6385      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6386 	     print_debuginfo(l_module_name, 'Exception occured when resubmitting '
6387 	         || 'payment instruction '
6388 	         || p_ins_id
6389 	         );
6390 	     print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
6391 	     print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
6392      END IF;
6393      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
6394 
6395      x_error_buf := 'PICP ERROR - RESUBMITTING PAYMENT'
6396          || ' INSTRUCTION FAILED';
6397 
6398      x_return_status := -1;
6399 
6400      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6401 	     print_debuginfo(l_module_name, 'EXIT');
6402 
6403      END IF;
6404  END resubmit_instruction;
6405 
6406 /*--------------------------------------------------------------------
6407  | NAME:
6408  |     reprint_prenum_pmt_documents
6409  |
6410  |
6411  | PURPOSE:
6412  |     This API should be called if it is required to re-print some of the
6413  |     paper documents of a payment instruction. This API should only
6414  |     be called with the list of paper documents that were spoilt (not
6415  |     with the list of paper documents that were skipped, issued etc.).
6416  |
6417  |     The list of used paper documents numbers will be used to insert
6418  |     records into the IBY_USED_PAYMENT_DOCS table.
6419  |
6420  |     The list of new paper document numbers will be used to update the
6421  |     IBY_PAYMENTS_ALL table with the new paper document numbers for
6422  |     the corresponding paments. The payment status will then be set to
6423  |     READY_FOR_REPRINT.
6424  |
6425  |     Finally, this API will invoke the paper printing flow.
6426  |
6427  |     This method should only be invoked for reprinting payment documents
6428  |     that are prenumbered (paper stock type is 'prenumbered'). For
6429  |     reprinting payment documents that are on blank stock use the method
6430  |     reprint_blank_pmt_documents().
6431  |
6432  | PARAMETERS:
6433  |     IN
6434  |       p_instr_id      - ID of the payment instruction, for which some
6435  |                         payments need to be re-printed.
6436  |       p_pmt_doc_id    - The payment document id (check stock) which
6437  |                         is to be used for re-printing.
6438  |       p_pmt_list      - List of payments that are affected by the
6439  |                         re-print. These payments will be updated with
6440  |                         new paper document numbers (provided by the user).
6441  |       p_new_ppr_docs_list
6442  |                       - List of new paper document numbers to print
6443  |                         the provided payments on.
6444  |       p_old_ppr_docs_list
6445  |                       - List of previously used paper document numbers.
6446  |                         These will be inserted into IBY_USED_PAYMENT_DOCS
6447  |                         table indicating that they were spoiled.
6448  |       p_printer_name  - Printer to use for re-printing payments.
6449  |
6450  |
6451  |     OUT
6452  |       x_return_status - Result of the API call:
6453  |                         FND_API.G_RET_STS_SUCCESS indicates that the
6454  |                           reprint process was triggered successfully.
6455  |                           In this case the caller must COMMIT
6456  |                           the status change.
6457  |
6458  |                         FND_API.G_RET_STS_ERROR (or other) indicates
6459  |                           that API did not complete successfully.
6460  |                           In this case, the caller must issue a
6461  |                           ROLLBACK to undo all status changes.
6462  |
6463  | RETURNS:
6464  |
6465  | NOTES:
6466  |   Internal API, not for public use.
6467  |
6468  *---------------------------------------------------------------------*/
6469  PROCEDURE reprint_prenum_pmt_documents(
6470      p_instr_id          IN NUMBER,
6471      p_pmt_doc_id        IN NUMBER,
6472      p_pmt_list          IN pmtIDTab,
6473      p_new_ppr_docs_list IN pmtDocsTab,
6474      p_old_ppr_docs_list IN pmtDocsTab,
6475      p_printer_name      IN VARCHAR2,
6476      x_return_status     OUT NOCOPY VARCHAR2
6477      )
6478  IS
6479 
6480  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
6481                                          || '.reprint_prenum_pmt_documents';
6482 
6483  l_pkg_name    CONSTANT VARCHAR2(100) := 'AP_AWT_CALLOUT_PKG';
6484 
6485  l_callout_name     VARCHAR2(500);
6486  l_stmt             VARCHAR2(1000);
6487 
6488  /* used in invocation of callout procedure */
6489  l_api_version      CONSTANT NUMBER := 1.0;
6490  l_msg_count        NUMBER;
6491  l_msg_data         VARCHAR2(2000);
6492 
6493  l_curr_pmt_status  VARCHAR2(200);
6494  l_temp_status      VARCHAR2(200);
6495 
6496  l_last_list_num    NUMBER;
6497  l_last_doc_num     NUMBER;
6498 
6499  /*
6500   * Implementing the hook is optional for the calling app.
6501   * If the calling app does not implement the hook, then
6502   * the call to the hook will result in ORA-06576 error.
6503   *
6504   * There is no exception name associated with this code, so
6505   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
6506   * exception occurs, it is not fatal: we log the error and
6507   * proceed.
6508   *
6509   * If, on the other hand, the calling app implements the
6510   * hook, but the hook throws an exception, it is fatal
6511   * and we must abort the program (this will be caught
6512   * in WHEN OTHERS block).
6513   */
6514  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
6515  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
6516 
6517  BEGIN
6518 
6519      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6520 	     print_debuginfo(l_module_name, 'ENTER');
6521 
6522      END IF;
6523      FND_MSG_PUB.initialize;
6524 
6525      /*
6526       * STATUS CHANGE:
6527       *
6528       * UI Responsibility:
6529       * NONE
6530       *
6531       * API Responsibility:
6532       * paper document use reason = SPOILED
6533       * payment status = READY_TO_REPRINT
6534       */
6535 
6536      IF (p_pmt_list.COUNT          = 0  OR
6537          p_new_ppr_docs_list.COUNT = 0  OR
6538          p_old_ppr_docs_list.COUNT = 0) THEN
6539 
6540          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6541 	         print_debuginfo(l_module_name, 'Error: List of used paper '
6542 	             || 'document numbers/new paper document numbers/payment '
6543 	             || 'ids is empty'
6544 	             || '. Returning failure response .. '
6545 	             );
6546 
6547          END IF;
6548          x_return_status := FND_API.G_RET_STS_ERROR;
6549 
6550          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6551 	         print_debuginfo(l_module_name, 'EXIT');
6552 
6553          END IF;
6554          RETURN;
6555 
6556      END IF;
6557 
6558      IF (p_pmt_list.COUNT <> p_new_ppr_docs_list.COUNT  OR
6559          p_pmt_list.COUNT <> p_old_ppr_docs_list.COUNT) THEN
6560 
6561          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6562 	         print_debuginfo(l_module_name, 'Error: List of paper '
6563 	             || 'doc numbers must match list of payment ids. '
6564 	             || 'Returning failure response .. '
6565 	             );
6566 
6567          END IF;
6568          x_return_status := FND_API.G_RET_STS_ERROR;
6569 
6570          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6571 	         print_debuginfo(l_module_name, 'EXIT');
6572 
6573          END IF;
6574          RETURN;
6575 
6576      END IF;
6577 
6578      /*
6579       * STEP 0 .
6580       *
6581       * Setting the printer name supplied
6582       * while reprinting the payment
6583       * instruction
6584       *
6585       */
6586 
6587 	UPDATE iby_pay_instructions_all ins
6588 	SET ins.printer_name = p_printer_name
6589 	WHERE ins.payment_instruction_id = p_instr_id;
6590 
6591 
6592      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6593 	     print_debuginfo(l_module_name, 'Set printer name as '
6594 	         || p_printer_name
6595 	         || ' for payment instruction id  '
6596 	         || p_instr_id
6597 	         );
6598 
6599      END IF;
6600        /*A commit is necessary to avoid deadlock situations*/
6601        COMMIT;
6602 
6603      /*
6604       * STEP 1:
6605       *
6606       * Update the IBY_USED_PAYMENT_DOCS table with the list of
6607       * used paper document numbers. Since the user invokes this API
6608       * to reprint the paper documents, it follows that the earlier
6609       * used paper document was spoilt. Therefore, set the status of
6610       * the earlier used paper document to SPOILED.
6611       */
6612 
6613      FOR i IN p_old_ppr_docs_list.FIRST .. p_old_ppr_docs_list.LAST LOOP
6614 
6615          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6616 	         print_debuginfo(l_module_name, 'Updating paper doc number '
6617 	             || p_old_ppr_docs_list(i)
6618 	             || ' of payment document id '
6619 	             || p_pmt_doc_id
6620 	             || ' to spoiled status ..'
6621 	             );
6622 
6623          END IF;
6624          INSERT INTO IBY_USED_PAYMENT_DOCS (
6625              PAYMENT_DOCUMENT_ID,
6626              USED_DOCUMENT_NUMBER,
6627              DATE_USED,
6628              DOCUMENT_USE,
6629              CREATED_BY,
6630              CREATION_DATE,
6631              LAST_UPDATED_BY,
6632              LAST_UPDATE_DATE,
6633              LAST_UPDATE_LOGIN,
6634              OBJECT_VERSION_NUMBER
6635              )
6636          VALUES (
6637              p_pmt_doc_id,
6638              p_old_ppr_docs_list(i),
6639              sysdate,
6640              DOC_USE_SPOILED,
6641              fnd_global.user_id,
6642              sysdate,
6643              fnd_global.user_id,
6644              sysdate,
6645              fnd_global.login_id,
6646              1
6647              );
6648 
6649      END LOOP;
6650 
6651      /*
6652       * STEP 2:
6653       *
6654       * Update the IBY_PAYMENTS_ALL table with the list of
6655       * new paper document numbers. The payment ids and new
6656       * paper document numbers have been provided in matching
6657       * order. The status of the payment also needs to be
6658       * updated to READY_FOR_REPRINT.
6659       */
6660 
6661      FOR i IN p_new_ppr_docs_list.FIRST .. p_new_ppr_docs_list.LAST LOOP
6662 
6663          /*
6664           * Get the current status of the
6665           * payment into l_curr_pmt_status
6666           */
6667          SELECT
6668              pmt.payment_status
6669          INTO
6670              l_curr_pmt_status
6671          FROM
6672              IBY_PAYMENTS_ALL pmt
6673          WHERE
6674              payment_id = p_pmt_list(i)
6675          ;
6676 
6677          /*
6678           * For debug purposes.
6679           */
6680          IF (l_curr_pmt_status = PAY_STATUS_VOID_SETUP) THEN
6681 
6682              l_temp_status := PAY_STATUS_SETUP_REPRINT;
6683 
6684          ELSIF (l_curr_pmt_status = PAY_STATUS_VOID_OVERFLOW) THEN
6685 
6686              l_temp_status := PAY_STATUS_OVERFLOW_REPRINT;
6687 
6688          ELSE
6689 
6690              l_temp_status := PAY_STATUS_REPRINT;
6691 
6692          END IF;
6693 
6694          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6695 	         print_debuginfo(l_module_name, 'Updating status of payment id '
6696 	             || p_pmt_list(i)
6697 	             || ' from '
6698 	             || l_curr_pmt_status
6699 	             || ' to '
6700 	             || l_temp_status
6701 	             );
6702 
6703          END IF;
6704          /*
6705           * Set the payment status to READY_TO_REPRINT
6706           *
6707           * Void and overflow payments are identified by
6708           * their statuses, so we do not want to blindly
6709           * overwrite ther statuses. Instead, set their
6710           * status to special statuses that indicate the
6711           * payment is a setup / overflow payment and
6712           * it must be reprinted.
6713           */
6714          UPDATE
6715              IBY_PAYMENTS_ALL
6716          SET
6717              paper_document_number  = p_new_ppr_docs_list(i),
6718              payment_status         = DECODE(
6719                                           l_curr_pmt_status,
6720                                           PAY_STATUS_VOID_SETUP,
6721                                           PAY_STATUS_SETUP_REPRINT,
6722                                           PAY_STATUS_VOID_OVERFLOW,
6723                                           PAY_STATUS_OVERFLOW_REPRINT,
6724                                           PAY_STATUS_REPRINT
6725                                           )
6726          WHERE
6727              payment_id             = p_pmt_list(i) AND
6728              payment_instruction_id = p_instr_id
6729          ;
6730 
6731      END LOOP;
6732 
6733 
6734      /*
6735       * STEP 2A:
6736       *
6737       * Fix for bug 5470041:
6738       *
6739       * Update the last issued document number
6740       * in CE_PAYMENT_DOCUMENTS table using the
6741       * greatest document number from the user
6742       * provided list of new document numbers
6743       * for reprinting.
6744       */
6745 
6746      l_last_list_num := -1;
6747 
6748      FOR i IN p_new_ppr_docs_list.FIRST .. p_new_ppr_docs_list.LAST LOOP
6749 
6750          IF (p_new_ppr_docs_list(i) > l_last_list_num) THEN
6751 
6752              l_last_list_num := p_new_ppr_docs_list(i);
6753 
6754          END IF;
6755 
6756      END LOOP;
6757 
6758      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6759 	     print_debuginfo(l_module_name, 'Greatest paper document number '
6760 	         || 'derived from provided list: '
6761 	         || l_last_list_num
6762 	         );
6763 
6764      END IF;
6765      /*
6766       * This update uses an extra security check - we
6767       * only update the last issued doc number if the
6768       * provided list contains a doc number greater
6769       * than what is already stored in the database.
6770       */
6771      UPDATE
6772          CE_PAYMENT_DOCUMENTS
6773      SET
6774          last_issued_document_number =
6775              GREATEST(l_last_list_num, last_issued_document_number)
6776      WHERE
6777          payment_document_id         = p_pmt_doc_id
6778      RETURNING
6779          last_issued_document_number
6780      INTO
6781          l_last_doc_num
6782      ;
6783 
6784      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6785 	     print_debuginfo(l_module_name, 'Payment document id '
6786 	         || p_pmt_doc_id
6787 	         || ' updated with last issued doc number set to: '
6788 	         || l_last_doc_num
6789 	         );
6790 
6791      END IF;
6792      /*
6793       * STEP 3:
6794       *
6795       * Invoke the hook that handles witholding certificates.
6796       * Every time a paper document is PRINTED | REPRINTED |
6797       * SPOILED, the witholding certificates should be in sync.
6798       */
6799      l_callout_name := l_pkg_name || '.' || 'zx_witholdingCertificatesHook';
6800 
6801      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6802 	     print_debuginfo(l_module_name, 'Attempting to call hook: '
6803 	         || l_callout_name);
6804 
6805      END IF;
6806      l_stmt := 'CALL '|| l_callout_name || '(:1, :2, :3, :4, :5, :6, :7, :8)';
6807 
6808      BEGIN
6809 
6810 	  iby_debug_pub.log(debug_msg => 'Enter withholding(printed) pkg:'||l_callout_name||': Timestamp:'  || systimestamp,
6811 		       debug_level => FND_LOG.LEVEL_PROCEDURE,
6812 		       module => l_module_name);
6813 
6814          EXECUTE IMMEDIATE
6815              (l_stmt)
6816          USING
6817              IN  p_instr_id,
6818              IN  'REPRINT',
6819              IN  l_api_version,
6820              IN  FND_API.G_FALSE,
6821              IN  FND_API.G_FALSE,
6822              OUT x_return_status,
6823              OUT l_msg_count,
6824              OUT l_msg_data
6825          ;
6826 
6827 	  iby_debug_pub.log(debug_msg => 'Exit withholding(printed) pkg:'||l_callout_name||': Timestamp:'  || systimestamp,
6828 	       debug_level => FND_LOG.LEVEL_PROCEDURE,
6829 	       module => l_module_name);
6830 
6831          /*
6832           * If the called procedure did not return success,
6833           * raise an exception.
6834           */
6835          IF (x_return_status IS NULL OR
6836              x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
6837 
6838 
6839 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
6840 	                 || l_callout_name
6841 	                 || ''', returned failure status - '
6842 	                 || x_return_status
6843 	                 || '. Raising exception.'
6844 	                 );
6845 
6846 
6847              APP_EXCEPTION.RAISE_EXCEPTION;
6848 
6849          END IF;
6850 
6851      EXCEPTION
6852 
6853          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
6854              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6855 	             print_debuginfo(l_module_name, 'Callout "' || l_callout_name
6856 	                 || '" not implemented by calling app - AP'
6857 	                 );
6858 
6859 	             print_debuginfo(l_module_name, 'Skipping hook call.');
6860 
6861 
6862              END IF;
6863          WHEN OTHERS THEN
6864 
6865 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
6866 	                 || l_callout_name
6867 	                 || ''', generated exception.'
6868 	                 );
6869 
6870 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
6871 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
6872 
6873              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
6874 
6875              /*
6876               * Propogate exception to caller.
6877               */
6878              RAISE;
6879      END;
6880 
6881 
6882 
6883      /*
6884       * STEP 4:
6885       *
6886       * Invoke the print routine to re-print these payments.
6887       */
6888      IBY_FD_POST_PICP_PROGS_PVT.
6889          Run_Post_PI_Programs(
6890              p_instr_id,
6891              'Y'
6892              );
6893 
6894      x_return_status := FND_API.G_RET_STS_SUCCESS;
6895 
6896      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6897 	     print_debuginfo(l_module_name, 'Returning success response ..');
6898 
6899 	     print_debuginfo(l_module_name, 'EXIT');
6900 
6901      END IF;
6902  EXCEPTION
6903      WHEN OTHERS THEN
6904 
6905      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6906 	     print_debuginfo(l_module_name, 'Exception occured when '
6907 	         || 'performing re-print for payment instruction '
6908 	         || p_instr_id
6909 	         );
6910 
6911 	     print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
6912 	     print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
6913      END IF;
6914      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
6915 
6916      x_return_status := FND_API.G_RET_STS_ERROR;
6917 
6918      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6919 	     print_debuginfo(l_module_name, 'EXIT');
6920 
6921      END IF;
6922  END reprint_prenum_pmt_documents;
6923 
6924 
6925 /*--------------------------------------------------------------------
6926  | NAME:
6927  |     reprint_blank_pmt_documents
6928  |
6929  |
6930  | PURPOSE:
6931  |     This API should be called if it is required to re-print some of the
6932  |     paper documents of a payment instruction. This API should only
6933  |     be called with the list of paper documents that were spoilt (not
6934  |     with the list of paper documents that were skipped, issued etc.).
6935  |
6936  |     The payment status will then be set to READY_FOR_REPRINT. No paper
6937  |     document number adjustment is required because the same paper
6938  |     document can be reused for bank paper stock. Only the status of the
6939  |     payment will be changed, the exiting paper document number on the
6940  |     payment will be untoched.
6941  |
6942  |     Finally, this API will invoke the paper printing flow.
6943  |
6944  |     This method should only be invoked for reprinting payment
6945  |     documents that are printed on blank paper stock (not for
6946  |     paper stock type that is 'prenumbered'). For reprinting
6947  |     payment documents that are on prenumbered stock use the method
6948  |     reprint_prenum_pmt_documents().
6949  |
6950  | PARAMETERS:
6951  |     IN
6952  |       p_instr_id      - ID of the payment instruction, for which some
6953  |                         payments need to be re-printed.
6954  |       p_pmt_list      - List of payments that are affected by the
6955  |                         re-print. The status of these payments will be
6956  |                         updated to indicate they are ready for reprint.
6957  |       p_printer_name  - Printer to use for re-printing payments.
6958  |
6959  |
6960  |     OUT
6961  |       x_return_status - Result of the API call:
6962  |                         FND_API.G_RET_STS_SUCCESS indicates that the
6963  |                           reprint process was triggered successfully.
6964  |                           In this case the caller must COMMIT
6965  |                           the status change.
6966  |
6967  |                         FND_API.G_RET_STS_ERROR (or other) indicates
6968  |                           that API did not complete successfully.
6969  |                           In this case, the caller must issue a
6970  |                           ROLLBACK to undo all status changes.
6971  |
6972  | RETURNS:
6973  |
6974  | NOTES:
6975  |   Internal API, not for public use.
6976  |
6977  *---------------------------------------------------------------------*/
6978  PROCEDURE reprint_blank_pmt_documents(
6979      p_instr_id          IN NUMBER,
6980      p_pmt_list          IN pmtIDTab,
6981      p_printer_name      IN VARCHAR2,
6982      x_return_status     OUT NOCOPY VARCHAR2
6983      )
6984  IS
6985 
6986  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
6987                                          || '.reprint_blank_pmt_documents';
6988 
6989  l_curr_pmt_status  VARCHAR2(200);
6990 
6991  BEGIN
6992 
6993      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6994 	     print_debuginfo(l_module_name, 'ENTER');
6995 
6996      END IF;
6997      FND_MSG_PUB.initialize;
6998 
6999      /*
7000       * STEP 0 .
7001       *
7002       * Setting the printer name supplied
7003       * while reprinting the payment
7004       * instruction
7005       *
7006       */
7007 
7008 	UPDATE iby_pay_instructions_all ins
7009 	SET ins.printer_name = p_printer_name
7010 	WHERE ins.payment_instruction_id = p_instr_id;
7011 
7012 
7013      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7014 	     print_debuginfo(l_module_name, 'Set printer name as '
7015 	         || p_printer_name
7016 	         || ' for payment instruction id  '
7017 	         || p_instr_id
7018 	         );
7019 
7020      END IF;
7021        /*A commit is necessary to avoid deadlock situations*/
7022        COMMIT;
7023 
7024      /*
7025       * STEP 1:
7026       *
7027       * Check whether any payments have been provided.
7028       */
7029      IF (p_pmt_list.COUNT = 0) THEN
7030 
7031          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7032 	         print_debuginfo(l_module_name, 'Error: List of payment ids '
7033 	             || 'is empty'
7034 	             || '. Returning failure response .. '
7035 	             );
7036 
7037          END IF;
7038          x_return_status := FND_API.G_RET_STS_ERROR;
7039 
7040          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7041 	         print_debuginfo(l_module_name, 'EXIT');
7042 
7043          END IF;
7044          RETURN;
7045 
7046      END IF;
7047 
7048      /*
7049       * STEP 2:
7050       *
7051       * Update the IBY_PAYMENTS_ALL table with the new status.
7052       * The status of the payment needs to be READY_FOR_REPRINT.
7053       */
7054      FOR i IN p_pmt_list.FIRST .. p_pmt_list.LAST LOOP
7055 
7056          /*
7057           * Get the current status of the
7058           * payment into l_curr_pmt_status
7059           */
7060          SELECT
7061              pmt.payment_status
7062          INTO
7063              l_curr_pmt_status
7064          FROM
7065              IBY_PAYMENTS_ALL pmt
7066          WHERE
7067              payment_id = p_pmt_list(i)
7068          ;
7069 
7070 
7071          /*
7072           * Set the payment status to READY_TO_REPRINT
7073           *
7074           * Void and overflow payments are identified by
7075           * their statuses, so we do not want to blindly
7076           * overwrite ther statuses. Instead, set their
7077           * status to special statuses that indicate the
7078           * payment is a setup / overflow payment and
7079           * it must be reprinted.
7080           */
7081          UPDATE
7082              IBY_PAYMENTS_ALL
7083          SET
7084              payment_status         = DECODE(
7085                                           l_curr_pmt_status,
7086                                           PAY_STATUS_VOID_SETUP,
7087                                           PAY_STATUS_SETUP_REPRINT,
7088                                           PAY_STATUS_VOID_OVERFLOW,
7089                                           PAY_STATUS_OVERFLOW_REPRINT,
7090                                           PAY_STATUS_REPRINT
7091                                           )
7092          WHERE
7093              payment_id             = p_pmt_list(i) AND
7094              payment_instruction_id = p_instr_id
7095          ;
7096 
7097      END LOOP;
7098 
7099 
7100      /*
7101       * STEP 3:
7102       *
7103       * Invoke the print routine to re-print these payments.
7104       */
7105      IBY_FD_POST_PICP_PROGS_PVT.
7106          Run_Post_PI_Programs(
7107              p_instr_id,
7108              'Y'
7109              );
7110 
7111      x_return_status := FND_API.G_RET_STS_SUCCESS;
7112 
7113      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7114 	     print_debuginfo(l_module_name, 'Returning success response ..');
7115 
7116 	     print_debuginfo(l_module_name, 'EXIT');
7117 
7118      END IF;
7119  EXCEPTION
7120      WHEN OTHERS THEN
7121 
7122      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7123 	     print_debuginfo(l_module_name, 'Exception occured when '
7124 	         || 'performing re-print for payment instruction '
7125 	         || p_instr_id
7126 	         );
7127 
7128 	     print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
7129 	     print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
7130      END IF;
7131      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
7132 
7133      x_return_status := FND_API.G_RET_STS_ERROR;
7134 
7135      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7136 	     print_debuginfo(l_module_name, 'EXIT');
7137 
7138      END IF;
7139  END reprint_blank_pmt_documents;
7140 
7141 /*--------------------------------------------------------------------
7142  | NAME:
7143  |     reprint_payment_instruction
7144  |
7145  | PURPOSE:
7146  |     Reprints all the payment documents associated with a payment
7147  |     instruction.
7148  |
7149  |     Note that in this case, no renumbering of the paper documents
7150  |     is required. This API is equivalent to making a fresh print call
7151  |     for a payment instruction (only requirement is that the payments of
7152  |     the payment instruction be numbered). We simply send the payment
7153  |     instruction for printing with the existing payment document numbers.
7154  |
7155  |     Do not call this API if printing for the payment instruction has
7156  |     already been attempted earlier.
7157  |
7158  | PARAMETERS:
7159  |     IN
7160  |
7161  |
7162  |     OUT
7163  |
7164  |
7165  | RETURNS:
7166  |
7167  | NOTES:
7168  |   Internal API, not for public use.
7169  |
7170  |   This procedure performs a COMMIT.
7171  |
7172  *---------------------------------------------------------------------*/
7173  PROCEDURE reprint_payment_instruction (
7174      p_instr_id          IN NUMBER,
7175      p_printer_name      IN VARCHAR2,
7176      x_return_status     OUT NOCOPY VARCHAR2
7177      )
7178  IS
7179 
7180  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
7181                                          || '.reprint_payment_instruction';
7182 
7183  BEGIN
7184 
7185      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7186 	     print_debuginfo(l_module_name, 'ENTER');
7187 
7188      END IF;
7189      FND_MSG_PUB.initialize;
7190 
7191      /*
7192       * STEP 1:
7193       *
7194       * Update the status of the payments of the given
7195       * instruction to indicate that they must be reprinted.
7196       */
7197      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7198 	     print_debuginfo(l_module_name, 'Updating statuses of the '
7199 	         || 'payments of payment instruction '
7200 	         || p_instr_id
7201 	         || ' to formatted ..'
7202 	         );
7203 
7204      END IF;
7205      UPDATE
7206          IBY_PAYMENTS_ALL
7207      SET
7208          payment_status = PAY_STATUS_FORMATTED
7209      WHERE
7210          payment_instruction_id = p_instr_id AND
7211          PAYMENT_SERVICE_REQUEST_ID <> -1
7212      ;
7213 
7214      /*
7215       * STEP 2:
7216       *
7217       * Update the status of the payment instruction
7218       * to indicate that it must be re-printed.
7219       */
7220      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7221 	     print_debuginfo(l_module_name, 'Updating status of '
7222 	         || 'payment instruction '
7223 	         || p_instr_id
7224 	         || ' to formatted - ready for printing ..'
7225 	         );
7226      END IF;
7227      UPDATE
7228          IBY_PAY_INSTRUCTIONS_ALL
7229      SET
7230          payment_instruction_status = INS_STATUS_FORMAT_TO_PRINT
7231      WHERE
7232          payment_instruction_id = p_instr_id
7233      ;
7234 
7235      /*
7236       * This commit is necessary. Otherwise a deadlocked
7237       * condition is created when we try to update the same
7238       * payment instruction with the concurrent request id
7239       * (for handling intermediate statuses).
7240       */
7241      COMMIT;
7242 
7243 
7244      /*
7245       * STEP 3:
7246       *
7247       * Invoke the print routine to re-print these payments.
7248       */
7249 
7250      /*
7251       * Note that reprint flag is 'N' in this case;
7252       * this is to treat the re-print as a fresh print.
7253       */
7254      IBY_FD_POST_PICP_PROGS_PVT.
7255          Run_Post_PI_Programs(
7256              p_instr_id,
7257              'N'
7258              );
7259 
7260      x_return_status := FND_API.G_RET_STS_SUCCESS;
7261 
7262      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7263 	     print_debuginfo(l_module_name, 'Returning success response ..');
7264 
7265 	     print_debuginfo(l_module_name, 'EXIT');
7266 
7267      END IF;
7268  EXCEPTION
7269      WHEN OTHERS THEN
7270 
7271      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7272 	     print_debuginfo(l_module_name, 'Exception occured when '
7273 	         || 'performing re-print for payment instruction '
7274 	         || p_instr_id
7275 	         );
7276 
7277 	     print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
7278 	     print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
7279      END IF;
7280      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
7281 
7282      x_return_status := FND_API.G_RET_STS_ERROR;
7283 
7284      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7285 	     print_debuginfo(l_module_name, 'EXIT');
7286 
7287      END IF;
7288  END reprint_payment_instruction;
7289 
7290 
7291 /*--------------------------------------------------------------------
7292  | NAME:
7293  |     finalize_print_status
7294  |
7295  |
7296  | PURPOSE:
7297  |     Records the final print status for a set of paper documents.
7298  |
7299  |     This is an overloaded method. See the other method signature
7300  |     for complete documentation.
7301  |
7302  | PARAMETERS:
7303  |     IN
7304  |
7305  |
7306  |     OUT
7307  |
7308  |
7309  | RETURNS:
7310  |
7311  | NOTES:
7312  |   Internal API, not for public use.
7313  |
7314  *---------------------------------------------------------------------*/
7315  PROCEDURE finalize_print_status(
7316      p_instr_id         IN NUMBER,
7317      p_pmt_doc_id       IN NUMBER,
7318      p_used_docs_list   IN paperDocNumTab,
7319      x_return_status    OUT NOCOPY VARCHAR2
7320      )
7321  IS
7322  BEGIN
7323 
7324      finalize_print_status(
7325          p_instr_id,
7326          p_pmt_doc_id,
7327          p_used_docs_list,
7328          FALSE,
7329          x_return_status
7330          );
7331 
7332  END finalize_print_status;
7333 
7334 /*--------------------------------------------------------------------
7335  | NAME:
7336  |     finalize_print_status
7337  |
7338  |
7339  | PURPOSE:
7340  |     Records the final print status for a set of paper documents.
7341  |
7342  |     This is an overloaded method. See the other method signature
7343  |     for complete documentation.
7344  |
7345  | PARAMETERS:
7346  |     IN
7347  |
7348  |
7349  |     OUT
7350  |
7351  |
7352  | RETURNS:
7353  |
7354  | NOTES:
7355  |   Internal API, not for public use.
7356  |
7357  *---------------------------------------------------------------------*/
7358  PROCEDURE finalize_print_status(
7359      p_instr_id         IN NUMBER,
7360      p_pmt_doc_id       IN NUMBER,
7361      p_used_docs_list   IN paperDocNumTab,
7362      p_used_pmts_list   IN paperDocNumTab,
7363      x_return_status    OUT NOCOPY VARCHAR2
7364      )
7365  IS
7366  BEGIN
7367      finalize_print_status(
7368          p_instr_id,
7369          p_pmt_doc_id,
7370          p_used_docs_list,
7371          p_used_pmts_list,
7372          FALSE,
7373          x_return_status
7374          );
7375 
7376  END finalize_print_status;
7377 /*--------------------------------------------------------------------
7378  | NAME:
7379  |     finalize_print_status
7380  |
7381  |
7382  | PURPOSE:
7383  |     Records the final print status for a set of paper documents.
7384  |
7385  |     This is an overloaded method. See the other method signature
7386  |     for complete documentation.
7387  |
7388  | PARAMETERS:
7389  |     IN
7390  |
7391  |
7392  |     OUT
7393  |
7394  |
7395  | RETURNS:
7396  |
7397  | NOTES:
7398  |   Internal API, not for public use.
7399  |
7400  *---------------------------------------------------------------------*/
7401  PROCEDURE finalize_print_status(
7402      p_instr_id           IN NUMBER,
7403      p_pmt_doc_id         IN NUMBER,
7404      p_used_docs_list     IN paperDocNumTab,
7405      p_used_pmts_list     IN paperDocNumTab,
7406      p_submit_postive_pay IN BOOLEAN,
7407      x_return_status      OUT NOCOPY VARCHAR2
7408      )
7409 IS
7410  p_skipped_docs_list paperDocNumTab;
7411  BEGIN
7412  p_skipped_docs_list(1) := -1;
7413 
7414      finalize_print_status(
7415          p_instr_id,
7416          p_pmt_doc_id,
7417          p_used_docs_list,
7418          p_used_pmts_list,
7419 	 p_skipped_docs_list,
7420 	 p_submit_postive_pay,
7421          x_return_status
7422          );
7423 
7424  END finalize_print_status;
7425 
7426 /*--------------------------------------------------------------------
7427  | NAME:
7428  |     finalize_print_status
7429  |
7430  | PURPOSE:
7431  |     Records the final print status for a set of paper documents. This
7432  |     API will insert the given set of paper document numbers into the
7433  |     IBY_USED_PAYMENT_DOCS table along with the usage reason indicating
7434  |     whether the paper document was spoiled.
7435  |
7436  |     Note that this API should only be invoked with the list of spoiled
7437  |     paper documents. The UI should directly handle the successfully
7438  |     printed paper documents. For performance reasons, this API is
7439  |     designed only to handle the spoiled paper document case.
7440  |
7441  |     This API will set the usage reason for each provided document
7442  |     to 'SPOILED' in the IBY_USED_PAYMENT_DOCS table, and it will also
7443  |     set the status of the corresponding payment to
7444  |     REMOVED_DOCUMENT_SPOILED.
7445  |
7446  |     Then, this API will set the status of the successfully printed
7447  |     payments to ISSUED status and then invoke the 'mark complete'
7448  |     API to mark the payments of this payment instruction as complete.
7449  |
7450  | PARAMETERS:
7451  |     IN
7452  |       p_pmt_doc_id      - The payment document id (check stock id)
7453  |                           of the given list of paper documents.
7454  |       p_used_docs_list  - The list of paper documents that have been
7455  |                           used for printing
7456  |
7457  |       p_submit_postive_pay
7458  |                         - Flag indicating whether positive pay file
7459  |                           report needs to be launched after finalizing
7460  |                           the payments.
7461  |
7462  |     OUT
7463  |       x_return_status   - Result of the API call:
7464  |                           FND_API.G_RET_STS_SUCCESS indicates that the
7465  |                           finalization process completed raised
7466  |                           successfully. In this case the caller must
7467  |                           COMMIT the status change.
7468  |
7469  |                           FND_API.G_RET_STS_ERROR (or other) indicates
7470  |                           that API did not complete successfully.
7471  |                           In this case, the caller must issue a
7472  |                           ROLLBACK to undo all status changes.
7473  |
7474  | RETURNS:
7475  |
7476  | NOTES:
7477  |   Internal API, not for public use.
7478  |
7479  *---------------------------------------------------------------------*/
7480  PROCEDURE finalize_print_status(
7481      p_instr_id           IN NUMBER,
7482      p_pmt_doc_id         IN NUMBER,
7483      p_used_docs_list     IN paperDocNumTab,
7484      p_submit_postive_pay IN BOOLEAN,
7485      x_return_status      OUT NOCOPY VARCHAR2
7486      )
7487  IS
7488  p_used_pmts_list paperDocNumTab;
7489  p_skipped_docs_list paperDocNumTab;
7490  BEGIN
7491  p_used_pmts_list(1) := -1;
7492  p_skipped_docs_list(1) := -1;
7493 
7494        finalize_final_print_status(
7495                p_instr_id,
7496                p_pmt_doc_id,
7497                p_used_docs_list,
7498                p_used_pmts_list,
7499 	       p_skipped_docs_list,
7500 	       p_submit_postive_pay,
7501                x_return_status
7502                );
7503 
7504  END finalize_print_status;
7505 
7506 
7507 /*--------------------------------------------------------------------
7508  | NAME:
7509  |     finalize_print_status
7510  |
7511  | PURPOSE:
7512  |     Records the final print status for a set of paper documents. This
7513  |     API will insert the given set of paper document numbers into the
7514  |     IBY_USED_PAYMENT_DOCS table along with the usage reason indicating
7515  |     whether the paper document was spoiled.
7516  |
7517  |     Note that this API should only be invoked with the list of spoiled
7518  |     paper documents. The UI should directly handle the successfully
7519  |     printed paper documents. For performance reasons, this API is
7520  |     designed only to handle the spoiled paper document case.
7521  |
7522  |     This API will set the usage reason for each provided document
7523  |     to 'SPOILED' in the IBY_USED_PAYMENT_DOCS table, and it will also
7524  |     set the status of the corresponding payment to
7525  |     REMOVED_DOCUMENT_SPOILED.
7526  |
7527  |     Then, this API will set the status of the successfully printed
7528  |     payments to ISSUED status and then invoke the 'mark complete'
7529  |     API to mark the payments of this payment instruction as complete.
7530  |
7531  | PARAMETERS:
7532  |     IN
7533  |       p_pmt_doc_id      - The payment document id (check stock id)
7534  |                           of the given list of paper documents.
7535  |       p_used_docs_list  - The list of paper documents that have been
7536  |                           used for printing
7537  |       p_use_reason_list - The list of paper document usage reasons. This
7538  |                           list will contain a lookup code that specifies
7539  |                           whether the paper document was correctly
7540  |                           printed or not. Possible values include
7541  |                           ISSUED | SPOILED. SKIPPED will never be a
7542  |                           provided reason because skipped documents have
7543  |                           successfully printed (only numbering is wrong).
7544  |       p_submit_postive_pay
7545  |                         - Flag indicating whether positive pay file
7546  |                           report needs to be launched after finalizing
7547  |                           the payments.
7548  |
7549  |     OUT
7550  |       x_return_status   - Result of the API call:
7551  |                           FND_API.G_RET_STS_SUCCESS indicates that the
7552  |                           finalization process completed raised
7553  |                           successfully. In this case the caller must
7554  |                           COMMIT the status change.
7555  |
7556  |                           FND_API.G_RET_STS_ERROR (or other) indicates
7557  |                           that API did not complete successfully.
7558  |                           In this case, the caller must issue a
7559  |                           ROLLBACK to undo all status changes.
7560  |
7561  | RETURNS:
7562  |
7563  | NOTES:
7564  |   Internal API, not for public use.
7565  |
7566  *---------------------------------------------------------------------*/
7567  PROCEDURE finalize_print_status(
7568      p_instr_id           IN NUMBER,
7569      p_pmt_doc_id         IN NUMBER,
7570      p_used_docs_list     IN paperDocNumTab,
7571      p_used_pmts_list     IN paperDocNumTab,
7572      p_skipped_docs_list  IN paperDocNumTab,
7573      p_submit_postive_pay IN BOOLEAN,
7574      x_return_status      OUT NOCOPY VARCHAR2
7575      )
7576  IS
7577 
7578  BEGIN
7579 
7580           finalize_final_print_status(
7581                p_instr_id,
7582                p_pmt_doc_id,
7583                p_used_docs_list,
7584                p_used_pmts_list,
7585 	       p_skipped_docs_list,
7586 	       p_submit_postive_pay,
7587                x_return_status
7588                );
7589  END finalize_print_status;
7590 
7591 /*--------------------------------------------------------------------
7592  | NAME:
7593  |     finalize_instr_print_status
7594  |
7595  | PURPOSE:
7596  |     Records the final print status for all the paper documents that
7597  |     are part of a payment instruction.
7598  |
7599  |     This is an overloaded method. See the other method signature for
7600  |     complete documentation.
7601  |
7602  | PARAMETERS:
7603  |     IN
7604  |
7605  |
7606  |     OUT
7607  |
7608  |
7609  | RETURNS:
7610  |
7611  | NOTES:
7612  |   Internal API, not for public use.
7613  |
7614  *---------------------------------------------------------------------*/
7615  PROCEDURE finalize_instr_print_status(
7616      p_instr_id       IN NUMBER,
7617      x_return_status  OUT NOCOPY VARCHAR2
7618      )
7619  IS
7620  BEGIN
7621 
7622      finalize_instr_print_status(
7623          p_instr_id,
7624          FALSE,
7625          x_return_status
7626          );
7627 
7628  END finalize_instr_print_status;
7629 
7630 /*--------------------------------------------------------------------
7631  | NAME:
7632  |     finalize_instr_print_status
7633  |
7634  | PURPOSE:
7635  |     Records the final print status for all the paper documents that
7636  |     are part of a payment instruction. This API will insert the paper
7637  |     document numbers linked to each payment in the instruction into the
7638  |     IBY_USED_PAYMENT_DOCS table along with the usage reason indicating
7639  |     that the paper document was successfully printed.
7640  |
7641  |     This API should only invoked when *all* the payments that are part
7642  |     of a payment instruction have been printed successfully.
7643  |
7644  |     This API is a light weight alternative to the finalize_print_status()
7645  |     API because the caller does not have to provide the list of used
7646  |     payment documents along with the usage reason. This API derives the
7647  |     list of used payment documents from the payments on the instruction
7648  |     and it sets the usage reason for each payment document as 'issued'.
7649  |
7650  | PARAMETERS:
7651  |     IN
7652  |       p_instr_id        - The payment instruction id for which all
7653  |                           checks were successfully printed.
7654  |
7655  |       p_submit_postive_pay
7656  |                         - Flag indicating whether positive pay file
7657  |                           report needs to be launched after finalizing
7658  |                           the payments.
7659  |
7660  |     OUT
7661  |       x_return_status   - Result of the API call:
7662  |                           FND_API.G_RET_STS_SUCCESS indicates that the
7663  |                           finalization process completed raised
7664  |                           successfully. In this case the caller must
7665  |                           COMMIT the status change.
7666  |
7667  |                           FND_API.G_RET_STS_ERROR (or other) indicates
7668  |                           that API did not complete successfully.
7669  |                           In this case, the caller must issue a
7670  |                           ROLLBACK to undo all status changes.
7671  |
7672  | RETURNS:
7673  |
7674  | NOTES:
7675  |   Internal API, not for public use.
7676  |
7677  *---------------------------------------------------------------------*/
7678  PROCEDURE finalize_instr_print_status(
7679      p_instr_id           IN NUMBER,
7680      p_submit_postive_pay IN BOOLEAN,
7681      x_return_status      OUT NOCOPY VARCHAR2
7682      )
7683  IS
7684  p_pmt_doc_id NUMBER := NULL;
7685  p_used_docs_list paperDocNumTab;
7686  p_used_pmts_list paperDocNumTab;
7687  p_skipped_pmts_list paperDocNumTab;
7688  BEGIN
7689  p_used_docs_list(1) := -1;
7690  p_used_pmts_list(1) := -1;
7691  p_skipped_pmts_list(1) := -1;
7692 
7693 	  -- Bug 10634813 Start - Fetching payment_document_id before call
7694 	  select payment_document_id
7695 	  into p_pmt_doc_id
7696 	  from iby_pay_instructions_all
7697 	  where payment_instruction_id = p_instr_id;
7698 	  -- Bug 10634813 End.
7699 
7700           finalize_final_print_status(
7701                p_instr_id,
7702                p_pmt_doc_id,
7703                p_used_docs_list,
7704                p_used_pmts_list,
7705 	       p_skipped_pmts_list,
7706 	       p_submit_postive_pay,
7707                x_return_status
7708                );
7709 
7710  END finalize_instr_print_status;
7711 
7712  /*--------------------------------------------------------------------
7713  | NAME:
7714  |     finalize_final_print_status
7715  |
7716  | PURPOSE:
7717  |     Records the final print status for a set of paper documents. This
7718  |     API will insert the given set of paper document numbers into the
7719  |     IBY_USED_PAYMENT_DOCS table along with the usage reason indicating
7720  |     whether the paper document was spoiled.
7721  |
7722  |     Note that this API should only be invoked with the list of spoiled
7723  |     paper documents. The UI should directly handle the successfully
7724  |     printed paper documents. For performance reasons, this API is
7725  |     designed only to handle the spoiled paper document case.
7726  |
7727  |     This API will set the usage reason for each provided document
7728  |     to 'SPOILED' in the IBY_USED_PAYMENT_DOCS table, and it will also
7729  |     set the status of the corresponding payment to
7730  |     REMOVED_DOCUMENT_SPOILED.
7731  |
7732  |     Then, this API will set the status of the successfully printed
7733  |     payments to ISSUED status and then invoke the 'mark complete'
7734  |     API to mark the payments of this payment instruction as complete.
7735  |
7736  | PARAMETERS:
7737  |     IN
7738  |       p_pmt_doc_id      - The payment document id (check stock id)
7739  |                           of the given list of paper documents.
7740  |       p_used_docs_list  - The list of paper documents that have been
7741  |                           used for printing
7742  |       p_use_reason_list - The list of paper document usage reasons. This
7743  |                           list will contain a lookup code that specifies
7744  |                           whether the paper document was correctly
7745  |                           printed or not. Possible values include
7746  |                           ISSUED | SPOILED. SKIPPED will never be a
7747  |                           provided reason because skipped documents have
7748  |                           successfully printed (only numbering is wrong).
7749  |       p_submit_postive_pay
7750  |                         - Flag indicating whether positive pay file
7751  |                           report needs to be launched after finalizing
7752  |                           the payments.
7753  |
7754  |     OUT
7755  |       x_return_status   - Result of the API call:
7756  |                           FND_API.G_RET_STS_SUCCESS indicates that the
7757  |                           finalization process completed raised
7758  |                           successfully. In this case the caller must
7759  |                           COMMIT the status change.
7760  |
7761  |                           FND_API.G_RET_STS_ERROR (or other) indicates
7762  |                           that API did not complete successfully.
7763  |                           In this case, the caller must issue a
7764  |                           ROLLBACK to undo all status changes.
7765  |
7766  | RETURNS:
7767  |
7768  | NOTES:
7769  |   Internal API, not for public use.
7770  |
7771  *---------------------------------------------------------------------*/
7772  PROCEDURE finalize_final_print_status(
7773      p_instr_id           IN NUMBER,
7774      p_pmt_doc_id         IN NUMBER,
7775      p_used_docs_list     IN paperDocNumTab,
7776      p_used_pmts_list     IN paperDocNumTab,
7777      p_skipped_docs_list  IN paperDocNumTab,
7778      p_submit_postive_pay IN BOOLEAN,
7779      x_return_status      OUT NOCOPY VARCHAR2
7780      )
7781  IS
7782  l_request_id  NUMBER;
7783  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
7784                                          || '.finalize_final_print_status';
7785  l_used_docs_list VARCHAR2(4000) := NULL;
7786  l_used_pmts_list VARCHAR2(4000) := NULL;
7787  l_skipped_docs_list VARCHAR2(4000) := NULL;
7788 
7789  l_submit_postive_pay VARCHAR2(20);
7790  l_request_status BOOLEAN := FALSE;
7791  l_req_status VARCHAR2(30);
7792  BEGIN
7793 
7794        IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7795 	     print_debuginfo(l_module_name, 'ENTER');
7796       END IF;
7797 
7798 -- Bug 9781541
7799 -- Avoid the passing of used documents concatenated string as a parameter
7800 --
7801 --         IF(p_used_docs_list(1) <> -1) THEN
7802 --           FOR i IN p_used_docs_list.FIRST .. p_used_docs_list.LAST LOOP
7803 --            l_used_docs_list := l_used_docs_list||p_used_docs_list(i)||'$';
7804 --           END LOOP;
7805 --           IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7806 --	     print_debuginfo(l_module_name, 'l_used_docs_list: '|| l_used_docs_list);
7807 --           END IF;
7808 --         ELSE
7809 --         l_used_docs_list := NULL;
7810 --         END IF;
7811 --
7812 --         IF(p_used_pmts_list(1) <> -1) THEN
7813 --           FOR i IN p_used_pmts_list.FIRST .. p_used_pmts_list.LAST LOOP
7814 --            l_used_pmts_list := l_used_pmts_list||p_used_pmts_list(i)||'$';
7815 --           END LOOP;
7816 --           IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7817 --	     print_debuginfo(l_module_name, 'l_used_pmts_list: '|| l_used_pmts_list);
7818 --           END IF;
7819 --         ELSE
7820 --         l_used_pmts_list := NULL;
7821 --         END IF;
7822 
7823 	 IF(p_skipped_docs_list(1) <> -1) THEN
7824            FOR i IN p_skipped_docs_list.FIRST .. p_skipped_docs_list.LAST LOOP
7825             l_skipped_docs_list := l_skipped_docs_list||p_skipped_docs_list(i)||'$';
7826            END LOOP;
7827            IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7828 	     print_debuginfo(l_module_name, 'l_skipped_docs_list: '|| l_skipped_docs_list);
7829            END IF;
7830          ELSE
7831          l_skipped_docs_list := NULL;
7832          END IF;
7833 
7834 
7835          IF (p_submit_postive_pay = TRUE) THEN
7836           l_submit_postive_pay := 'TRUE';
7837          ELSE
7838           l_submit_postive_pay := 'FALSE';
7839          END IF;
7840 
7841         SELECT request_id INTO l_request_id
7842         FROM iby_pay_instructions_all
7843         WHERE payment_instruction_id = p_instr_id;
7844 
7845          IF(l_request_id IS NOT NULL) THEN
7846 
7847               l_req_status :=  get_conc_request_status(l_request_id);
7848 
7849               IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7850                     print_debuginfo(l_module_name, 'Request Status::'
7851                              || l_req_status
7852                              );
7853               END IF;
7854 
7855               IF(l_req_status = 'ERROR') THEN
7856                     IBY_DISBURSE_UI_API_PUB_PKG.unlock_pmt_entity(
7857                                          p_instr_id ,
7858                                          'PAYMENT_INSTRUCTION',
7859                                          x_return_status
7860                                          );
7861 
7862                     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7863                         RAISE FND_API.G_EXC_ERROR;
7864                     ELSE
7865                       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7866                         print_debuginfo(l_module_name, 'unlock_pmt_entity() API returned success');
7867                       END IF;
7868                     END IF;
7869              END IF;
7870          END IF;
7871   IF(l_request_id IS NULL OR l_req_status = 'ERROR')  THEN
7872 
7873  l_request_id := FND_REQUEST.SUBMIT_REQUEST
7874 	    (
7875 	      'IBY',
7876 	      'IBY_FD_RECORD_PRINT_STATUS',
7877 	      null,  -- description
7878 	      null,  -- start_time
7879 	      FALSE, -- sub_request
7880 	      p_instr_id,
7881 	      p_pmt_doc_id,
7882 	      l_used_docs_list,
7883               l_used_pmts_list,
7884               l_submit_postive_pay,
7885               l_skipped_docs_list, '', '', '', '', '', '', '',
7886 	      '', '', '', '', '', '', '', '',
7887 	      '', '', '', '', '', '', '', '',
7888 	      '', '', '', '', '', '', '', '',
7889 	      '', '', '', '', '', '', '', '',
7890 	      '', '', '', '', '', '', '', '',
7891 	      '', '', '', '', '', '', '', '',
7892 	      '', '', '', '', '', '', '', '',
7893 	      '', '', '', '', '', '', '', '',
7894 	      '', '', '', '', '', '', '', '',
7895 	      '', '', '', '', '', '', '', '',
7896 	      '', '', '', '', '', '', ''
7897 	    );
7898 
7899             IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7900               print_debuginfo(l_module_name, 'Calling the lock_pmt_entity() API to lock instruction: ' || p_instr_id
7901                                     || ' for the extract/formatting/printing/delivery program');
7902             END IF;
7903 
7904       IBY_DISBURSE_UI_API_PUB_PKG.lock_pmt_entity(
7905              p_object_id         => p_instr_id,
7906              p_object_type       => 'PAYMENT_INSTRUCTION',
7907              p_conc_request_id   => l_request_id,
7908              x_return_status     => x_return_status
7909              );
7910 
7911       IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
7912         RAISE FND_API.G_EXC_ERROR;
7913       ELSE
7914 
7915         IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7916           print_debuginfo(l_module_name, 'lock_pmt_entity() API returned success');
7917         END IF;
7918       END IF;
7919 
7920  END IF;
7921       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7922 	     print_debuginfo(l_module_name, 'EXIT');
7923       END IF;
7924 
7925 END finalize_final_print_status;
7926  /*--------------------------------------------------------------------
7927  | NAME:
7928  |   record_print_status
7929  |
7930  | PURPOSE:
7931  |     Records the final print status for a set of paper documents. This
7932  |     API will insert the given set of paper document numbers into the
7933  |     IBY_USED_PAYMENT_DOCS table along with the usage reason indicating
7934  |     whether the paper document was spoiled.
7935  |
7936  |     Note that this API should only be invoked with the list of spoiled
7937  |     paper documents. The UI should directly handle the successfully
7938  |     printed paper documents. For performance reasons, this API is
7939  |     designed only to handle the spoiled paper document case.
7940  |
7941  |     This API will set the usage reason for each provided document
7942  |     to 'SPOILED' in the IBY_USED_PAYMENT_DOCS table, and it will also
7943  |     set the status of the corresponding payment to
7944  |     REMOVED_DOCUMENT_SPOILED.
7945  |
7946  |     Then, this API will set the status of the successfully printed
7947  |     payments to ISSUED status and then invoke the 'mark complete'
7948  |     API to mark the payments of this payment instruction as complete.
7949  |
7950  | PARAMETERS:
7951  |     IN
7952  |       p_pmt_doc_id      - The payment document id (check stock id)
7953  |                           of the given list of paper documents.
7954  |       p_used_docs_list  - The list of paper documents that have been
7955  |                           used for printing
7956  |       p_use_reason_list - The list of paper document usage reasons. This
7957  |                           list will contain a lookup code that specifies
7958  |                           whether the paper document was correctly
7959  |                           printed or not. Possible values include
7960  |                           ISSUED | SPOILED. SKIPPED will never be a
7961  |                           provided reason because skipped documents have
7962  |                           successfully printed (only numbering is wrong).
7963  |       p_submit_postive_pay
7964  |                         - Flag indicating whether positive pay file
7965  |                           report needs to be launched after finalizing
7966  |                           the payments.
7967  |       p_args8 - p_args100
7968  |        These 93 parameters are mandatory for any stored procedure
7969  |        that is submitted from Oracle Forms as a concurrent request
7970  |        (to get the total number of args to the concurrent procedure
7971  |         to 100).
7972  |
7973  | OUT
7974  |
7975  |    x_errbuf
7976  |    x_retcode
7977  |
7978  |        These two are mandatory output paramaters for a concurrent
7979  |        program. They will store the error message and error code
7980  |        to indicate a successful/failed run of the concurrent request.
7981  |
7982  | RETURNS:
7983  |
7984  | NOTES:
7985  |   Internal API, not for public use.
7986  |
7987  *---------------------------------------------------------------------*/
7988  PROCEDURE record_print_status(
7989      x_errbuf                     OUT NOCOPY VARCHAR2,
7990      x_retcode                    OUT NOCOPY VARCHAR2,
7991      p_instr_id           IN NUMBER,
7992      p_pmt_doc_id         IN NUMBER,
7993      p_used_docs_list     IN VARCHAR2,
7994      p_used_pmts_list     IN VARCHAR2,
7995      p_submit_postive_pay IN VARCHAR2,
7996      p_skipped_docs_list  IN VARCHAR2 DEFAULT NULL,
7997      p_arg7  IN VARCHAR2 DEFAULT NULL,
7998      p_arg8  IN VARCHAR2 DEFAULT NULL, p_arg9  IN VARCHAR2 DEFAULT NULL,
7999      p_arg10  IN VARCHAR2 DEFAULT NULL, p_arg11  IN VARCHAR2 DEFAULT NULL,
8000      p_arg12  IN VARCHAR2 DEFAULT NULL, p_arg13  IN VARCHAR2 DEFAULT NULL,
8001      p_arg14  IN VARCHAR2 DEFAULT NULL, p_arg15  IN VARCHAR2 DEFAULT NULL,
8002      p_arg16  IN VARCHAR2 DEFAULT NULL, p_arg17  IN VARCHAR2 DEFAULT NULL,
8003      p_arg18  IN VARCHAR2 DEFAULT NULL, p_arg19  IN VARCHAR2 DEFAULT NULL,
8004      p_arg20  IN VARCHAR2 DEFAULT NULL, p_arg21  IN VARCHAR2 DEFAULT NULL,
8005      p_arg22  IN VARCHAR2 DEFAULT NULL, p_arg23  IN VARCHAR2 DEFAULT NULL,
8006      p_arg24  IN VARCHAR2 DEFAULT NULL, p_arg25  IN VARCHAR2 DEFAULT NULL,
8007      p_arg26  IN VARCHAR2 DEFAULT NULL, p_arg27  IN VARCHAR2 DEFAULT NULL,
8008      p_arg28  IN VARCHAR2 DEFAULT NULL, p_arg29  IN VARCHAR2 DEFAULT NULL,
8009      p_arg30  IN VARCHAR2 DEFAULT NULL, p_arg31  IN VARCHAR2 DEFAULT NULL,
8010      p_arg32  IN VARCHAR2 DEFAULT NULL, p_arg33  IN VARCHAR2 DEFAULT NULL,
8011      p_arg34  IN VARCHAR2 DEFAULT NULL, p_arg35  IN VARCHAR2 DEFAULT NULL,
8012      p_arg36  IN VARCHAR2 DEFAULT NULL, p_arg37  IN VARCHAR2 DEFAULT NULL,
8013      p_arg38  IN VARCHAR2 DEFAULT NULL, p_arg39  IN VARCHAR2 DEFAULT NULL,
8014      p_arg40  IN VARCHAR2 DEFAULT NULL, p_arg41  IN VARCHAR2 DEFAULT NULL,
8015      p_arg42  IN VARCHAR2 DEFAULT NULL, p_arg43  IN VARCHAR2 DEFAULT NULL,
8016      p_arg44  IN VARCHAR2 DEFAULT NULL, p_arg45  IN VARCHAR2 DEFAULT NULL,
8017      p_arg46  IN VARCHAR2 DEFAULT NULL, p_arg47  IN VARCHAR2 DEFAULT NULL,
8018      p_arg48  IN VARCHAR2 DEFAULT NULL, p_arg49  IN VARCHAR2 DEFAULT NULL,
8019      p_arg50  IN VARCHAR2 DEFAULT NULL, p_arg51  IN VARCHAR2 DEFAULT NULL,
8020      p_arg52  IN VARCHAR2 DEFAULT NULL, p_arg53  IN VARCHAR2 DEFAULT NULL,
8021      p_arg54  IN VARCHAR2 DEFAULT NULL, p_arg55  IN VARCHAR2 DEFAULT NULL,
8022      p_arg56  IN VARCHAR2 DEFAULT NULL, p_arg57  IN VARCHAR2 DEFAULT NULL,
8023      p_arg58  IN VARCHAR2 DEFAULT NULL, p_arg59  IN VARCHAR2 DEFAULT NULL,
8024      p_arg60  IN VARCHAR2 DEFAULT NULL, p_arg61  IN VARCHAR2 DEFAULT NULL,
8025      p_arg62  IN VARCHAR2 DEFAULT NULL, p_arg63  IN VARCHAR2 DEFAULT NULL,
8026      p_arg64  IN VARCHAR2 DEFAULT NULL, p_arg65  IN VARCHAR2 DEFAULT NULL,
8027      p_arg66  IN VARCHAR2 DEFAULT NULL, p_arg67  IN VARCHAR2 DEFAULT NULL,
8028      p_arg68  IN VARCHAR2 DEFAULT NULL, p_arg69  IN VARCHAR2 DEFAULT NULL,
8029      p_arg70  IN VARCHAR2 DEFAULT NULL, p_arg71  IN VARCHAR2 DEFAULT NULL,
8030      p_arg72  IN VARCHAR2 DEFAULT NULL, p_arg73  IN VARCHAR2 DEFAULT NULL,
8031      p_arg74  IN VARCHAR2 DEFAULT NULL, p_arg75  IN VARCHAR2 DEFAULT NULL,
8032      p_arg76  IN VARCHAR2 DEFAULT NULL, p_arg77  IN VARCHAR2 DEFAULT NULL,
8033      p_arg78  IN VARCHAR2 DEFAULT NULL, p_arg79  IN VARCHAR2 DEFAULT NULL,
8034      p_arg80  IN VARCHAR2 DEFAULT NULL, p_arg81  IN VARCHAR2 DEFAULT NULL,
8035      p_arg82  IN VARCHAR2 DEFAULT NULL, p_arg83  IN VARCHAR2 DEFAULT NULL,
8036      p_arg84  IN VARCHAR2 DEFAULT NULL, p_arg85  IN VARCHAR2 DEFAULT NULL,
8037      p_arg86  IN VARCHAR2 DEFAULT NULL, p_arg87  IN VARCHAR2 DEFAULT NULL,
8038      p_arg88  IN VARCHAR2 DEFAULT NULL, p_arg89  IN VARCHAR2 DEFAULT NULL,
8039      p_arg90  IN VARCHAR2 DEFAULT NULL, p_arg91  IN VARCHAR2 DEFAULT NULL,
8040      p_arg92  IN VARCHAR2 DEFAULT NULL, p_arg93  IN VARCHAR2 DEFAULT NULL,
8041      p_arg94  IN VARCHAR2 DEFAULT NULL, p_arg95  IN VARCHAR2 DEFAULT NULL,
8042      p_arg96  IN VARCHAR2 DEFAULT NULL, p_arg97  IN VARCHAR2 DEFAULT NULL,
8043      p_arg98  IN VARCHAR2 DEFAULT NULL, p_arg99  IN VARCHAR2 DEFAULT NULL,
8044      p_arg100 IN VARCHAR2 DEFAULT NULL
8045      )
8046  IS
8047   l_request_id            NUMBER;
8048   l_return_status      VARCHAR2(4000);
8049   l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
8050                                          || '.record_print_status';
8051  l_paper_doc_num IBY_PAYMENTS_ALL.paper_document_number%TYPE;
8052  l_payment_status_list  docPayStatusTab;
8053  l_pmtDocsTab  paperDocNumTab;
8054   l_skipped_document_number IBY_PAYMENTS_ALL.paper_document_number%TYPE;
8055   l_pmt_doc_id     NUMBER(15);
8056 
8057  /* used in invocation of callout procedure */
8058  l_api_version    CONSTANT NUMBER := 1.0;
8059  l_msg_count      NUMBER;
8060  l_first_doc_number      NUMBER;
8061  l_last_doc_number      NUMBER;
8062  l_msg_data       VARCHAR2(2000);
8063  l_pkg_name       CONSTANT VARCHAR2(100) := 'AP_AWT_CALLOUT_PKG';
8064  l_callout_name   VARCHAR2(500);
8065  l_stmt           VARCHAR2(1000);
8066 
8067  /* Bug #6508530 -used for changing status for spoiled payment, document */
8068  l_doc_id_list    docPayIDTab;
8069  l_doc_status_list  docPayStatusTab;
8070 
8071  l_submit_postive_pay BOOLEAN;
8072  l_used_docs_list paperDocNumTab;
8073  l_used_pmts_list paperDocNumTab;
8074  l_skipped_docs_list paperDocNumTab;
8075 
8076  t_used_docs_list VARCHAR2(4000);
8077  t_used_pmts_list VARCHAR2(4000);
8078  t_skipped_docs_list VARCHAR2(4000);
8079 
8080  l_str  VARCHAR2(4000) := NULL;
8081  l_count NUMBER := 0;
8082  j NUMBER := 1;
8083 
8084  x_return_status_remove_doc    VARCHAR2(200);
8085 
8086  CURSOR c_docs (p_pmt_id IBY_PAYMENTS_ALL.
8087                                 payment_id%TYPE)
8088  IS
8089  SELECT
8090      doc.document_payable_id,
8091      doc.document_status
8092  FROM
8093      IBY_DOCS_PAYABLE_ALL doc
8094  WHERE
8095      doc.payment_id = p_pmt_id
8096  AND doc.document_status <> DOC_STATUS_VOID_SETUP --Bug 14661977
8097 
8098  ;
8099 
8100 
8101  /*
8102   * Cursor to pick up all the payment document numbers that
8103   * are linked to payments of a particular payment
8104   * instruction.
8105   */
8106 
8107  /*
8108   * Fix for bug 4887500.
8109   *
8110   * When finalizing the status of a payment instruction, the payment
8111   * status may not be in the right status for single payments.
8112   *
8113   * This is because the Post-PICP program performs the status change
8114   * for the payment and since it runs as a separate conc request, it
8115   * may not have completed by the time the single payments API
8116   * calls this post-PICP program and immediately starts to finalize
8117   * the payment status.
8118   *
8119   * Therefore, the best approach would be to not consider the payment
8120   * status when the payment is a single payment (i.e., process type of
8121   * the payment is 'immediate').
8122   */
8123 
8124  CURSOR c_pmt_docs (p_instr_id NUMBER)
8125  IS
8126  SELECT
8127      pmt.paper_document_number
8128  FROM
8129      IBY_PAYMENTS_ALL pmt
8130  WHERE
8131      pmt.payment_instruction_id = p_instr_id    AND
8132          (
8133              (
8134               pmt.payment_status         = PAY_STATUS_FORMATTED      OR
8135               pmt.payment_status         = PAY_STATUS_SUB_FOR_PRINT
8136              )
8137              OR
8138              (
8139               pmt.process_type           = PROCESS_TYPE_IMMEDIATE
8140              )
8141          )
8142  ;
8143 
8144 -- Bug 9781541
8145 -- Cursor for collecting used documents from the payments all table
8146  CURSOR c_used_payments (p_instr_id NUMBER)
8147  IS
8148        	 SELECT payment_status, paper_document_number, payment_id
8149 	 FROM IBY_PAYMENTS_ALL
8150 	 WHERE payment_instruction_id = p_instr_id
8151 	 ORDER BY paper_document_number, payment_id;
8152 
8153  /*
8154   * Implementing the hook is optional for the calling app.
8155   * If the calling app does not implement the hook, then
8156   * the call to the hook will result in ORA-06576 error.
8157   *
8158   * There is no exception name associated with this code, so
8159   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
8160   * exception occurs, it is not fatal: we log the error and
8161   * proceed.
8162   *
8163   * If, on the other hand, the calling app implements the
8164   * hook, but the hook throws an exception, it is fatal
8165   * and we must abort the program (this will be caught
8166   * in WHEN OTHERS block).
8167   */
8168  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
8169  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
8170 
8171  BEGIN
8172 
8173    iby_debug_pub.log(debug_msg => 'Enter Record Print Status: Timestamp:'  || systimestamp,
8174 		       debug_level => FND_LOG.LEVEL_PROCEDURE,
8175 		       module => l_module_name);
8176 
8177      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8178 	     print_debuginfo(l_module_name, 'ENTER');
8179      END IF;
8180      FND_MSG_PUB.initialize;
8181 
8182      IF (p_submit_postive_pay = 'TRUE') THEN
8183           l_submit_postive_pay := TRUE;
8184          ELSE
8185           l_submit_postive_pay := FALSE;
8186          END IF;
8187 
8188       /*
8189       * STEP 1.5:
8190       *
8191       * Managing skipped documents
8192       * Finding out which of the documents are skipped
8193       *
8194       */
8195  IF(p_skipped_docs_list IS NOT NULL) THEN
8196          t_skipped_docs_list := p_skipped_docs_list;
8197       WHILE (LENGTH(t_skipped_docs_list) <> 0) LOOP
8198 
8199           l_count := instr(t_skipped_docs_list, '$');
8200           l_str := substr(t_skipped_docs_list,1,l_count-1);
8201           l_skipped_docs_list(j) := to_number(l_str);
8202           t_skipped_docs_list := substr(t_skipped_docs_list, l_count+1);
8203           l_str := NULL;
8204           l_count := 0;
8205                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8206 			 print_debuginfo(l_module_name, 'l_skipped_docs_list(j): '|| l_skipped_docs_list(j));
8207 		 END IF;
8208           j := j+1;
8209        END LOOP;
8210 
8211  FOR i IN l_skipped_docs_list.FIRST .. l_skipped_docs_list.LAST LOOP
8212          BEGIN
8213 
8214                 -- check if it already exists in the used_documents_table
8215 		SELECT cedocs.used_document_number
8216    	        INTO  l_skipped_document_number
8217 		FROM iby_used_payment_docs cedocs
8218 		WHERE cedocs.payment_document_id = p_pmt_doc_id
8219 		 AND cedocs.used_document_number = l_skipped_docs_list(i);
8220 
8221 	 EXCEPTION
8222 	 WHEN NO_DATA_FOUND THEN
8223 
8224                  -- inserting the document as skipped
8225                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8226 	                 print_debuginfo(l_module_name, 'inserting as skipped  ' || l_skipped_docs_list(i));
8227                  END IF;
8228 		 INSERT INTO IBY_USED_PAYMENT_DOCS (
8229 		     PAYMENT_DOCUMENT_ID,
8230 		     USED_DOCUMENT_NUMBER,
8231 		     DATE_USED,
8232 		     DOCUMENT_USE,
8233 		     CREATED_BY,
8234 		     CREATION_DATE,
8235 		     LAST_UPDATED_BY,
8236 		     LAST_UPDATE_DATE,
8237 		     LAST_UPDATE_LOGIN,
8238 		     OBJECT_VERSION_NUMBER
8239 		     )
8240 		 VALUES (
8241 		     p_pmt_doc_id,
8242 		     l_skipped_docs_list(i),
8243 		     sysdate,
8244 		     'SKIPPED',
8245 		     fnd_global.user_id,
8246 		     sysdate,
8247 		     fnd_global.user_id,
8248 		     sysdate,
8249 		     fnd_global.login_id,
8250 		     1
8251 		     );
8252 	 END;
8253 
8254  END LOOP;
8255 
8256  END IF;
8257 
8258 -- Bug 9781541
8259 -- Collecting used documents from the payments all table into widely used arrays, hence rest of the code remains unchanged
8260 
8261      print_debuginfo(l_module_name, 'Bug 9781541 - Collecting used documents from the payments all');
8262      OPEN  c_used_payments(p_instr_id);
8263      FETCH c_used_payments BULK COLLECT INTO l_payment_status_list, l_used_docs_list, l_used_pmts_list;
8264      CLOSE c_used_payments;
8265 
8266 -- Bug 9781541
8267 -- No parsing needed as we already collected used docs above. Also passed parameter is NULL, dont have a dollar concatenated string
8268 --
8269 -- j := 1;
8270 -- IF(p_used_docs_list IS NOT NULL) THEN
8271 --
8272 --  t_used_docs_list := p_used_docs_list;
8273 --
8274 --       WHILE (LENGTH(t_used_docs_list) <> 0) LOOP
8275 --          l_count := instr(t_used_docs_list, '$');
8276 --          l_str := substr(t_used_docs_list,1,l_count-1);
8277 --          l_used_docs_list(j) := to_number(l_str);
8278 --          t_used_docs_list := substr(t_used_docs_list, l_count+1);
8279 --          l_str := NULL;
8280 --          l_count := 0;
8281 --                 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8282 --			 print_debuginfo(l_module_name, 'l_used_docs_list(j): '|| l_used_docs_list(j));
8283 --		 END IF;
8284 --          j := j+1;
8285 --       END LOOP;
8286 --
8287 --     /*
8288 --      * STATUS CHANGE:
8289 --      *
8290 --      * UI Responsibility:
8291 --      * NONE
8292 --      *
8293 --      * API Responsibility:
8294 --      * payment instruction status = PRINTED
8295 --      *
8296 --      * payment status = REMOVED_DOCUMENT_SPOILED (for spoiled payments)
8297 --      * payment status = ISSUED (for successful payments)
8298 --      */
8299 --
8300 --	     IF (l_used_docs_list.COUNT = 0) THEN
8301 --
8302 --		 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8303 --			 print_debuginfo(l_module_name, 'Error: List of used paper '
8304 --			     || 'document numbers is empty'
8305 --			     || '. Returning failure response .. '
8306 --			     );
8307 --
8308 --		 END IF;
8309 --		 l_return_status := FND_API.G_RET_STS_ERROR;
8310 --
8311 --		 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8312 --			 print_debuginfo(l_module_name, 'EXIT');
8313 --		 END IF;
8314 --		 RETURN;
8315 --
8316 --	     END IF;
8317 --
8318 --
8319 --      -- looping through the complete document list
8320 --  IF(p_used_pmts_list IS NOT NULL) THEN
8321 --
8322 --      l_str := NULL;
8323 --      l_count := 0;
8324 --      j := 1;
8325 --      t_used_pmts_list := p_used_pmts_list;
8326 --
8327 --          WHILE (LENGTH(t_used_pmts_list) <> 0) LOOP
8328 --            l_count := instr(t_used_pmts_list, '$');
8329 --            l_str := substr(t_used_pmts_list,1,l_count-1);
8330 --            l_used_pmts_list(j) := to_number(l_str);
8331 --            t_used_pmts_list := substr(t_used_pmts_list, l_count+1);
8332 --            l_str := NULL;
8333 --            l_count := 0;
8334 --                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8335 --			 print_debuginfo(l_module_name, 'l_used_pmts_list(j): '|| l_used_pmts_list(j));
8336 --		 END IF;
8337 --            j := j+1;
8338 --         END LOOP;
8339 --
8340 --      j := 1;
8341 
8342      /*
8343       * STEP 1:
8344       *
8345       * Start processing the used paper documents, one-by-one.
8346       *
8347       * This API will only be called with the list of spoiled
8348       * paper documents, so we can set the usage reason for
8349       * each paper document to 'spoiled'.
8350       */
8351 
8352 
8353      FOR i IN l_used_docs_list.FIRST .. l_used_docs_list.LAST LOOP
8354 
8355          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8356 	         print_debuginfo(l_module_name, 'Paper Document Number = ' || l_used_docs_list(i));
8357 	         print_debuginfo(l_module_name, 'Payment ID : Payment Status = ' || l_used_pmts_list(i) || ':' || l_payment_status_list(i));
8358 
8359          END IF;
8360 	 BEGIN
8361 
8362 		-- checking if the document is already skipped earlier
8363 		SELECT cedocs.used_document_number
8364    	        INTO  l_skipped_document_number
8365 		FROM iby_used_payment_docs cedocs
8366 		WHERE cedocs.payment_document_id = p_pmt_doc_id
8367 		 AND cedocs.used_document_number = l_used_docs_list(i)
8368 		 AND cedocs.document_use = 'SKIPPED';
8369 
8370 	 EXCEPTION
8371 	 WHEN NO_DATA_FOUND THEN
8372 
8373 		 --if the document is not skipped earlier insert new
8374 		 INSERT INTO IBY_USED_PAYMENT_DOCS (
8375 		     PAYMENT_DOCUMENT_ID,
8376 		     USED_DOCUMENT_NUMBER,
8377 		     DATE_USED,
8378 		     DOCUMENT_USE,
8379 		     CREATED_BY,
8380 		     CREATION_DATE,
8381 		     LAST_UPDATED_BY,
8382 		     LAST_UPDATE_DATE,
8383 		     LAST_UPDATE_LOGIN,
8384 		     OBJECT_VERSION_NUMBER
8385 		     )
8386 		 VALUES (
8387 		     p_pmt_doc_id,
8388 		     l_used_docs_list(i),
8389 		     sysdate,
8390 		     decode(l_payment_status_list(i),'REMOVED_DOCUMENT_SPOILED','SPOILED','ISSUED'),
8391 		     fnd_global.user_id,
8392 		     sysdate,
8393 		     fnd_global.user_id,
8394 		     sysdate,
8395 		     fnd_global.login_id,
8396 		     1
8397 		     );
8398 
8399                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8400 	                 print_debuginfo(l_module_name, 'Document number not found, inserting new');
8401                  END IF;
8402 	 END;
8403 
8404          -- updating the document_use in any case
8405 	 UPDATE IBY_USED_PAYMENT_DOCS
8406 	 SET DOCUMENT_USE = decode(l_payment_status_list(i),'REMOVED_DOCUMENT_SPOILED','SPOILED','ISSUED')
8407 	 WHERE payment_document_id = p_pmt_doc_id
8408 	 AND used_document_number = l_used_docs_list(i);
8409 
8410 	 print_debuginfo(l_module_name, 'Found skipped document number, updating document usage');
8411 
8412       END LOOP;
8413 
8414       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8415 	     print_debuginfo(l_module_name, 'Finished updating document use table.');
8416 
8417       END IF;
8418 
8419      /*
8420       * STEP 2:
8421       *
8422       * Pick up all payments for this payment instruction
8423       * that have the given paper document number one-by-one.
8424       *
8425       * For each such payment, set the payment status to 'SPOILED'.
8426       */
8427      BEGIN
8428 
8429 	 -- looping through the entire document list
8430          FOR i IN l_used_docs_list.FIRST .. l_used_docs_list.LAST LOOP
8431 
8432 
8433    	  print_debuginfo(l_module_name, 'Payment ID : Paper Doc Num : Payment Status = ' || l_used_pmts_list(i) || ' : ' || l_used_docs_list(i) || ' : ' || l_payment_status_list(i));
8434 
8435 	  -- only invoices for those paper_documents which are marked as spoiled are unlocked here
8436 	  IF ( l_payment_status_list(i) = 'REMOVED_DOCUMENT_SPOILED') THEN
8437 
8438 	     l_paper_doc_num := l_used_docs_list(i);
8439 	     print_debuginfo(l_module_name, 'In the loop, spoiled document number : ('||i||') :' || l_used_pmts_list(i) );
8440 
8441 -- Bug 9781541
8442 -- No point in doing this, becoz IBY_PAYMENTS_ALL is our source of data for spoiled checks
8443 --
8444 --             UPDATE
8445 --                 IBY_PAYMENTS_ALL
8446 --             SET
8447 --                 payment_status = PAY_STATUS_SPOILED
8448 --             WHERE
8449 --                 payment_instruction_id = p_instr_id          AND
8450 --                 payment_id   = l_used_pmts_list(i)
8451 --             ;
8452 
8453 
8454             IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8455 	            print_debuginfo(l_module_name, 'Finished updating payments table');
8456             END IF;
8457              UPDATE
8458 	         IBY_DOCS_PAYABLE_ALL
8459              SET
8460                  document_status = DOC_STATUS_PMT_SPOILED
8461              WHERE
8462                   payment_id = l_used_pmts_list(i)
8463              AND DOCUMENT_STATUS <> 'VOID_BY_SETUP' --Bug 14661977
8464              ;
8465 
8466 	      print_debuginfo(l_module_name, 'Finished updating document payable table');
8467 
8468             OPEN  c_docs(l_used_pmts_list(i));
8469             FETCH c_docs BULK COLLECT INTO l_doc_id_list, l_doc_status_list;
8470             CLOSE c_docs;
8471 
8472            IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8473 	           print_debuginfo(l_module_name, 'The number of doc ids for this '
8474 	                                          ||'payment' ||l_doc_id_list.COUNT);
8475 
8476            END IF;
8477 	   if (l_doc_id_list.COUNT > 0) then /*Bug 14661977 if there would be no documents
8478                        for the payments which are overflown or of setup*/
8479 	   remove_documents_payable (
8480                      l_doc_id_list,
8481 		     l_doc_status_list,
8482 		     x_return_status_remove_doc
8483 		     );
8484 	IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8485 		print_debuginfo(l_module_name,'Inside loop,after callling'
8486 		|| 'remove_documents_payable');
8487 
8488 
8489 	END IF;
8490 	    IF (x_return_status_remove_doc <> FND_API.G_RET_STS_SUCCESS) THEN
8491 
8492 		     /*
8493   		      * Even if a single call to remove a document
8494      		      * failed, return failure for the entire
8495      		      * API request.
8496   		      */
8497 		     print_debuginfo(l_module_name, 'Removing of documents '
8498 			 || 'for payment instruction id '
8499 			 || p_instr_id
8500 			 || ' failed.'
8501 			 );
8502 
8503 		     print_debuginfo(l_module_name, 'EXIT');
8504 
8505 		     /*
8506   		      * It is the callers responsibility to rollback
8507        		      * all the changes.
8508        		      */
8509 		     RETURN;
8510 
8511 	    END IF;
8512      END IF; --Bug 14661977
8513 	 END IF;
8514 
8515         END LOOP;
8516 
8517      EXCEPTION
8518          WHEN OTHERS THEN
8519 
8520 	             print_debuginfo(l_module_name, 'Fatal: Exception occured '
8521 	                 || 'when attempting to update payment with payment '
8522 	                 || 'instruction id '
8523 	                 || p_instr_id
8524 	                 || ' and paper document number '
8525 	                 || l_paper_doc_num
8526 	                 );
8527 
8528 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
8529 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
8530 
8531              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
8532 
8533              /*
8534               * Propogate exception to caller.
8535               */
8536              RAISE;
8537 
8538      END;
8539 
8540      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8541 	     print_debuginfo(l_module_name, 'Finished updating payments table.');
8542 
8543      END IF;
8544 
8545      /*
8546       * STEP 3:
8547       *
8548       * Invoke the hook that handles witholding certificates.
8549       * Every time a paper document is PRINTED | REPRINTED |
8550       * SPOILED, the witholding certificates should be in sync.
8551       */
8552      l_callout_name := l_pkg_name || '.' || 'zx_witholdingCertificatesHook';
8553 
8554      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8555 	     print_debuginfo(l_module_name, 'Attempting to call hook: '
8556 	         || l_callout_name);
8557 
8558      END IF;
8559      l_stmt := 'CALL '|| l_callout_name || '(:1, :2, :3, :4, :5, :6, :7, :8)';
8560 
8561      BEGIN
8562 
8563      iby_debug_pub.log(debug_msg => 'Enter withholding pkg:'||l_callout_name||': Timestamp:'  || systimestamp,
8564 	       debug_level => FND_LOG.LEVEL_PROCEDURE,
8565 	       module => l_module_name);
8566 
8567          EXECUTE IMMEDIATE
8568              (l_stmt)
8569          USING
8570              IN  p_instr_id,
8571              IN  'SPOILED',
8572              IN  l_api_version,
8573              IN  FND_API.G_FALSE,
8574              IN  FND_API.G_FALSE,
8575              OUT l_return_status,
8576              OUT l_msg_count,
8577              OUT l_msg_data
8578          ;
8579 
8580     iby_debug_pub.log(debug_msg => 'Exit withholding pkg:'||l_callout_name||': Timestamp:'  || systimestamp,
8581        debug_level => FND_LOG.LEVEL_PROCEDURE,
8582        module => l_module_name);
8583 
8584          /*
8585           * If the called procedure did not return success,
8586           * raise an exception.
8587           */
8588          IF (l_return_status IS NULL OR
8589              l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
8590 
8591 
8592 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
8593 	                 || l_callout_name
8594 	                 || ''', returned failure status - '
8595 	                 || l_return_status
8596 	                 || '. Raising exception.'
8597 	                 );
8598 
8599 
8600              APP_EXCEPTION.RAISE_EXCEPTION;
8601 
8602          END IF;
8603 
8604      EXCEPTION
8605 
8606          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
8607              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8608 	             print_debuginfo(l_module_name, 'Callout "' || l_callout_name
8609 	                 || '" not implemented by calling app - AP'
8610 	                 );
8611 
8612 	             print_debuginfo(l_module_name, 'Skipping hook call.');
8613 
8614 
8615              END IF;
8616          WHEN OTHERS THEN
8617 
8618 	             print_debuginfo(l_module_name, 'Fatal: External app callout '''
8619 	                 || l_callout_name
8620 	                 || ''', generated exception.'
8621 	                 );
8622 
8623 	             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
8624 	             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
8625 
8626              FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
8627 
8628              /*
8629               * Propogate exception to caller.
8630               */
8631              RAISE;
8632      END;
8633 
8634      /*
8635       * STEP 4:
8636       *
8637       * Change the status of the successful payments of this instruction
8638       * to ISSUED status. The ISSUED status is the final status for a
8639       * paper payment; the payment must be in ISSUED status in order to
8640       * be marked complete.
8641       */
8642      UPDATE
8643          IBY_PAYMENTS_ALL
8644      SET
8645          payment_status = PAY_STATUS_ISSUED
8646      WHERE
8647          payment_instruction_id = p_instr_id     AND
8648          (
8649              payment_status IN
8650              (
8651              PAY_STATUS_FORMATTED,
8652              PAY_STATUS_SUB_FOR_PRINT
8653              )
8654              OR
8655              (
8656              process_type = PROCESS_TYPE_IMMEDIATE
8657              )
8658          )
8659      ;
8660 
8661      /*
8662       * STEP 5:
8663       *
8664       * Mark all the payments of the given payment instruction
8665       * as complete.
8666       */
8667      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8668 	     print_debuginfo(l_module_name, 'Attempting to mark payments of '
8669 	         || 'payment instruction '
8670 	         || p_instr_id
8671 	         || ' as complete.'
8672 	         );
8673 
8674      END IF;
8675      mark_all_pmts_complete (
8676          p_instr_id,
8677          l_submit_postive_pay,
8678          l_return_status
8679          );
8680 
8681      IF (l_return_status IS NULL OR
8682          l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
8683 
8684          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8685 	         print_debuginfo(l_module_name, 'Error status '
8686 	             || l_return_status
8687 	             || ' was received from the mark_all_pmts_complete() '
8688 	             || 'method. Raising exception.'
8689 	             );
8690 
8691          END IF;
8692 
8693 
8694          IF(p_used_docs_list IS NOT NULL) THEN
8695 		 l_return_status := FND_API.G_RET_STS_ERROR;
8696 
8697 			 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8698 				 print_debuginfo(l_module_name, 'EXIT');
8699 
8700 			 END IF;
8701 		 RETURN;
8702          ELSE
8703 	  APP_EXCEPTION.RAISE_EXCEPTION;
8704 	 END IF;
8705 
8706      ELSE
8707 
8708          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8709 	         print_debuginfo(l_module_name, 'Successfully maked payments '
8710 	             || 'as complete.'
8711 	             );
8712 
8713          END IF;
8714      END IF;
8715 
8716      /*
8717       * STEP 6:
8718       *
8719       * Change the status of the payment instruction to PRINTED.
8720       */
8721      UPDATE
8722          IBY_PAY_INSTRUCTIONS_ALL
8723      SET
8724          payment_instruction_status = INS_STATUS_PRINTED
8725      WHERE
8726          payment_instruction_id = p_instr_id
8727      ;
8728 
8729      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8730 	     print_debuginfo(l_module_name, 'Successfully set payment '
8731 	         || 'instruction status to PRINTED.'
8732 	         );
8733 
8734      END IF;
8735      l_return_status := FND_API.G_RET_STS_SUCCESS;
8736      COMMIT;
8737                               /*
8738                               * The payment instruction was possibly locked
8739                               * by the UI. Unlock it if possible.
8740                               */
8741                              IBY_DISBURSE_UI_API_PUB_PKG.unlock_pmt_entity(
8742                                  p_instr_id ,
8743                                  'PAYMENT_INSTRUCTION',
8744                                  l_return_status
8745                                  );
8746      x_errbuf := 'RECORD PRINT STATUS COMPLETED SUCCESSFULLY';
8747      x_retcode := '0';
8748 
8749      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8750 	     print_debuginfo(l_module_name, 'Returning success response ..');
8751 
8752 	     print_debuginfo(l_module_name, 'EXIT');
8753 
8754      END IF;
8755 
8756   iby_debug_pub.log(debug_msg => 'Exit Record Print Status : Timestamp:'  || systimestamp,
8757 		       debug_level => FND_LOG.LEVEL_PROCEDURE,
8758 		       module => l_module_name);
8759  EXCEPTION
8760      WHEN OTHERS THEN
8761 
8762      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8763 	     print_debuginfo(l_module_name, 'Exception occured when '
8764 	         || 'recording final print status for payment instruction '
8765 	         || p_instr_id
8766 	         );
8767 
8768 	     print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
8769 	     print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
8770      END IF;
8771      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
8772 
8773      l_return_status := FND_API.G_RET_STS_ERROR;
8774      ROLLBACK;
8775       x_errbuf := 'RECORD PRINT STATUS COMPLETED IN ERRORS';
8776       x_retcode := '-1';
8777                              /*
8778                               * The payment instruction was possibly locked
8779                               * by the UI. Unlock it if possible.
8780                               */
8781                              IBY_DISBURSE_UI_API_PUB_PKG.unlock_pmt_entity(
8782                                  p_instr_id ,
8783                                  'PAYMENT_INSTRUCTION',
8784                                  l_return_status
8785                                  );
8786 
8787      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8788 	     print_debuginfo(l_module_name, 'EXIT');
8789 
8790      END IF;
8791  END record_print_status;
8792 
8793 /*--------------------------------------------------------------------
8794  | NAME:
8795  |     mark_all_pmts_complete
8796  |
8797  | PURPOSE:
8798  |     Marks all payments of the given payment instruction as complete.
8799  |
8800  |     This method overloads the other mark_all_pmts_complete(..)
8801  |     See the other method for complete documentation.
8802  |
8803  | NOTES:
8804  |     Internal API, not for public use.
8805  |
8806  |     This method does not COMMIT/ROLLBACK. It is the callers responsibility
8807  |     to COMMIT/ROLLBACK depending upon the return status.
8808  |
8809  *---------------------------------------------------------------------*/
8810  PROCEDURE mark_all_pmts_complete (
8811      p_instr_id       IN NUMBER,
8812      x_return_status  OUT NOCOPY VARCHAR2
8813      )
8814  IS
8815 
8816  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
8817                                          || '.mark_all_pmts_complete';
8818  BEGIN
8819 
8820      /*
8821       * Call mark_all_pmts_complete(..) with the submit
8822       * positive pay file report flag set to 'no' by
8823       * default.
8824       */
8825      mark_all_pmts_complete(p_instr_id, FALSE, x_return_status);
8826 
8827  END mark_all_pmts_complete;
8828 
8829 /*--------------------------------------------------------------------
8830  | NAME:
8831  |     mark_all_pmts_complete
8832  |
8833  | PURPOSE:
8834  |     Marks all payments of the given payment instruction as complete.
8835  |     Also marks the status of the payment instruction to complete.
8836  |
8837  |     Only payments that are in 'valid' status will be marked complete,
8838  |     others will be left untouched. The 'valid' status for a payment
8839  |     is dependent upon whether it is a printed payment or an electronic
8840  |     payment. Here is list of valid statuses by processing type:
8841  |
8842  |     Processing     Valid statuses for payments
8843  |     Type
8844  |     ----------     ---------------------------
8845  |     PRINTED        ISSUED
8846  |
8847  |     ELECTRONIC     FORMATTED
8848  |                    TRANSMITTED
8849  |                    ACKNOWLEDGED
8850  |                    BANK_VALIDATED
8851  |                    PAID
8852  |
8853  |     Therefore, the caller must ensure that they set the payment
8854  |     status to valid status for all the payments that are to be
8855  |     marked complete *before* calling this API.
8856  |
8857  |     Once payments are marked as complete, the calling application
8858  |     hook 'payments_completed()' is invoked to inform the calling
8859  |     app that their payments have been marked complete.
8860  |
8861  | PARAMETERS:
8862  |     IN
8863  |       p_instr_id        - The payment instruction id for which the
8864  |                           payments need to be marked complete.
8865  |
8866  |       p_submit_postive_pay
8867  |                         - Flag indicating whether positive pay file
8868  |                           report needs to be launched after marking
8869  |                           the payments complete.
8870  |
8871  |     OUT
8872  |       x_return_status   - Result of the API call:
8873  |                           FND_API.G_RET_STS_SUCCESS indicates that
8874  |                           the mark complete process finished
8875  |                           successfully. In this case the caller must
8876  |                           COMMIT the status change.
8877  |
8878  |                           FND_API.G_RET_STS_ERROR (or other) indicates
8879  |                           that API did not complete successfully.
8880  |                           In this case, the caller must issue a
8881  |                           ROLLBACK to undo all status changes.
8882  |
8883  | RETURNS:
8884  |
8885  | NOTES:
8886  |     Internal API, not for public use.
8887  |
8888  |     This method does not COMMIT/ROLLBACK. It is the callers responsibility
8889  |     to COMMIT/ROLLBACK depending upon the return status.
8890  *---------------------------------------------------------------------*/
8891  PROCEDURE mark_all_pmts_complete (
8892      p_instr_id           IN NUMBER,
8893      p_submit_postive_pay IN BOOLEAN,
8894      x_return_status      OUT NOCOPY VARCHAR2
8895      )
8896  IS
8897 
8898  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
8899                                          || '.mark_all_pmts_complete';
8900 
8901  l_completion_id   NUMBER(15);
8902  l_processing_type VARCHAR2(200);
8903  l_process_type    VARCHAR2(200);
8904  l_completion_point VARCHAR2(30);
8905 
8906  /* used in forming callout procedure name */
8907  l_calling_app_id NUMBER;
8908  l_app_short_name VARCHAR2(200);
8909  l_pkg_name       VARCHAR2(200);
8910  l_callout_name   VARCHAR2(500);
8911  l_stmt           VARCHAR2(1000);
8912 
8913  /* used in invocation of callout procedure */
8914  l_api_version    CONSTANT NUMBER := 1.0;
8915  l_msg_count      NUMBER;
8916  l_msg_data       VARCHAR2(2000);
8917 
8918  l_appNamesTab    appNamesTab;
8919  l_appIdsTab      appIdsTab;
8920 
8921  l_pprIdsTab      pprIdsTab;
8922 
8923  l_pmts_complete_code   VARCHAR2(1000);
8924 
8925  l_pmt_doc_id     NUMBER(15);
8926  l_pmt_doc_name   VARCHAR2(2000);
8927 
8928  l_doc_count      NUMBER(15);
8929  l_pmt_count      NUMBER(15);
8930  l_incomplete_pmts  NUMBER(15);
8931 
8932  /*
8933   * These variables are related to kicking off automatic
8934   * reports.
8935   */
8936  l_profile_id     IBY_PAYMENT_PROFILES.
8937                       payment_profile_id%TYPE;
8938  l_auto_pi_reg_submit_flag
8939                   IBY_SYS_PMT_PROFILES_B.
8940                       automatic_pi_reg_submit%TYPE;
8941  l_auto_sra_submit_flag
8942                   IBY_REMIT_ADVICE_SETUP.
8943                       automatic_sra_submit_flag%TYPE;
8944  l_remit_format_code
8945                   IBY_REMIT_ADVICE_SETUP.
8946                       remittance_advice_format_code%TYPE;
8947  l_pos_pay_format IBY_PAYMENT_PROFILES.
8948                       positive_pay_format_code%TYPE;
8949  l_pi_reg_format  IBY_SYS_PMT_PROFILES_B.
8950                       pi_register_format%TYPE;
8951 
8952 
8953  l_conc_req_id    NUMBER(15);
8954 
8955  l_icx_numeric_characters   VARCHAR2(30); -- Bug 6411356
8956  l_bool_val   boolean;  -- Bug 6411356
8957 
8958  l_error_code     IBY_TRANSACTION_ERRORS.error_code%TYPE;
8959  /*
8960   * Cursor to pick up all the distinct PPR ids for the set
8961   * of payments which are with this instruction.
8962   *
8963   * Added for bug 7505803
8964   *
8965   */
8966   CURSOR c_ppr_ids (p_instr_id NUMBER)
8967   IS
8968   SELECT DISTINCT
8969          PMT.PAYMENT_SERVICE_REQUEST_ID
8970   FROM IBY_PAYMENTS_ALL PMT
8971   WHERE PMT.PAYMENT_INSTRUCTION_ID = p_instr_id;
8972 
8973 
8974  /*
8975   * Cursor to pick up all calling applications associated
8976   * with a particular payment instruction.
8977   */
8978 
8979  /*
8980   * Fix for bug 4901075.
8981   *
8982   * Added distinct clause to select statement so that duplicate
8983   * calling app ids are eliminated.
8984   *
8985   * Otherwise, the call out could be invoked multiple times
8986   * per calling app.
8987   */
8988  CURSOR c_app_names (p_instr_id NUMBER)
8989  IS
8990  SELECT DISTINCT
8991      fnd.application_short_name
8992  INTO
8993      l_app_short_name
8994  FROM
8995      FND_APPLICATION          fnd,
8996      IBY_PAYMENTS_ALL         pmt,
8997      IBY_PAY_SERVICE_REQUESTS req,
8998      IBY_PAY_INSTRUCTIONS_ALL ins
8999  WHERE
9000      pmt.payment_instruction_id     = ins.payment_instruction_id     AND
9001      req.payment_service_request_id = pmt.payment_service_request_id AND
9002      fnd.application_id             = req.calling_app_id             AND
9003      ins.payment_instruction_id     = p_instr_id
9004  ;
9005 
9006  /*
9007   * Cursor to pick up ids all calling applications
9008   * associated with a particular payment instruction.
9009   */
9010  CURSOR c_app_ids (p_instr_id NUMBER)
9011  IS
9012  SELECT
9013      fnd.application_id
9014  FROM
9015      FND_APPLICATION          fnd,
9016      IBY_PAYMENTS_ALL         pmt,
9017      IBY_PAY_SERVICE_REQUESTS req,
9018      IBY_PAY_INSTRUCTIONS_ALL ins
9019  WHERE
9020      pmt.payment_instruction_id     = ins.payment_instruction_id     AND
9021      req.payment_service_request_id = pmt.payment_service_request_id AND
9022      fnd.application_id             = req.calling_app_id             AND
9023      ins.payment_instruction_id     = p_instr_id
9024  ;
9025 
9026  /* EFT Paper Document Generation Begin*/
9027  /*Bug 9376894*/
9028  /*CURSOR c_pmt_docs (p_instr_id NUMBER)
9029  IS
9030  SELECT
9031      pmt.paper_document_number
9032  FROM
9033      IBY_PAYMENTS_ALL pmt
9034  WHERE
9035      pmt.payment_instruction_id = p_instr_id    AND
9036      pmt.payment_status         = PAY_STATUS_FORMATTED;*/
9037 
9038 /* EFT Paper Document Generation End */
9039 
9040 
9041 
9042  /*
9043   * Implementing the callout is optional for the calling app.
9044   * If the calling app does not implement the hook, then
9045   * the call to the hook will result in ORA-06576 error.
9046   *
9047   * There is no exception name associated with this code, so
9048   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
9049   * exception occurs, it is not fatal: we log the error and
9050   * proceed.
9051   *
9052   * If, on the other hand, the calling app implements the
9053   * callout, but the callout throws an exception, it is fatal
9054   * and we must abort the program (this will be caught
9055   * in WHEN OTHERS block).
9056   */
9057  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
9058  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
9059 
9060  BEGIN
9061 
9062      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9063 	     print_debuginfo(l_module_name, 'ENTER');
9064 
9065      END IF;
9066      FND_MSG_PUB.initialize;
9067 
9068       iby_debug_pub.log(debug_msg => 'Enter Mark Complete: Timestamp:'  || systimestamp,
9069 		       debug_level => FND_LOG.LEVEL_PROCEDURE,
9070 		       module => l_module_name);
9071 
9072      /*
9073       * STEP 0 :
9074       *
9075       * Set the apps context. This is necessary so that
9076       * the calling application can see the org based
9077       * tables associated with these payments.
9078       *
9079       * See bugs 9229224, 4945922.
9080       */
9081 
9082      /*IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9083              print_debuginfo(l_module_name, 'Setting apps context .. ');
9084      END IF;
9085 
9086      fnd_global.APPS_INITIALIZE(
9087          user_id      => fnd_global.user_id,
9088          resp_id      => fnd_global.resp_id,
9089          resp_appl_id => fnd_global.resp_appl_id
9090          );
9091 
9092      mo_global.init(fnd_global.application_short_name);
9093 
9094      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9095              print_debuginfo(l_module_name, 'Apps context set [user: '
9096                  || fnd_global.user_id
9097                  || ', responsbility id: '
9098                  || fnd_global.resp_id
9099                  || ', responsibility application id: '
9100                  || fnd_global.resp_appl_id
9101                  || ']'
9102                  );
9103 
9104              print_debuginfo(l_module_name, 'Apps context [app short name: '
9105                  || fnd_global.application_short_name
9106                  || ']'
9107                  );
9108      END IF;*/
9109 
9110 
9111      /*
9112       * STEP I:
9113       *
9114       * Get the processing type of this payment instruction.
9115       * The processing type is used as a criterion in selecting
9116       * which payments of this payment instruction are updated
9117       * to completed status.
9118       */
9119       -- Bug: 9851821
9120       -- Introduced row level locking for instructions
9121       -- This will ensure multiple checks are not made for the
9122       -- same instruction
9123      SELECT
9124          prof.processing_type,
9125          inst.payments_complete_code,
9126          inst.process_type
9127      INTO
9128          l_processing_type,
9129          l_pmts_complete_code,
9130          l_process_type
9131      FROM
9132          IBY_PAYMENT_PROFILES     prof,
9133          IBY_PAY_INSTRUCTIONS_ALL inst
9134      WHERE
9135          prof.payment_profile_id     = inst.payment_profile_id  AND
9136          inst.payment_instruction_id = p_instr_id
9137      FOR UPDATE OF inst.payment_instruction_id NOWAIT ;
9138 
9139      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9140 	     print_debuginfo(l_module_name, 'For payment instruction '
9141 	         || p_instr_id
9142 	         || ', payment complete code: '
9143 	         || l_pmts_complete_code
9144 	         || ' and process type: '
9145 	         || l_process_type
9146 	         );
9147 
9148      END IF;
9149      /*
9150       * STEP II:
9151       *
9152       * Check whether we need to mark payments complete
9153       * for the payments of this payment instruction.
9154       *
9155       * Payments should be marked complete should be called
9156       * *except* when:
9157       *
9158       * A. The payments of this payment instruction have already
9159       * been marked complete.
9160       */
9161      IF (UPPER(l_pmts_complete_code) = 'YES') THEN
9162 
9163          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9164 	         print_debuginfo(l_module_name, 'Not performing mark pmts '
9165 	             || 'complete for payment instruction '
9166 	             || p_instr_id
9167 	             || ' because payments already marked complete '
9168 	             || 'for this instruction.'
9169 	             );
9170 
9171          END IF;
9172          x_return_status := FND_API.G_RET_STS_SUCCESS;
9173          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9174 	         print_debuginfo(l_module_name, 'Returning success response ..');
9175 
9176 	         print_debuginfo(l_module_name, 'EXIT');
9177 
9178          END IF;
9179          RETURN;
9180 
9181      END IF;
9182 
9183      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9184 	     print_debuginfo(l_module_name, 'Processing type of payment instruction '
9185 	         || p_instr_id
9186 	         || ' is '
9187 	         || l_processing_type
9188 	         );
9189 
9190      END IF;
9191      /*
9192       * STEP III:
9193       *
9194       * Mark all payments of this payment instruction as
9195       * completed.
9196       */
9197      IF (l_processing_type = P_TYPE_PRINTED) THEN
9198 
9199          UPDATE
9200              IBY_PAYMENTS_ALL
9201          SET
9202              payments_complete_flag = 'Y'
9203          WHERE
9204              payment_instruction_id = p_instr_id
9205 	 AND payment_status         = PAY_STATUS_ISSUED
9206          ;
9207 
9208      ELSE
9209 
9210          /* electronic processing type */
9211 	 l_completion_point := IBY_DISBURSE_UI_API_PUB_PKG.Get_Pmt_Completion_Point(p_instr_id);
9212 
9213          IF (l_process_type <> PROCESS_TYPE_IMMEDIATE) THEN
9214 
9215              /* batch payment(s) */
9216 
9217              UPDATE
9218                  IBY_PAYMENTS_ALL
9219              SET
9220                  payments_complete_flag = 'Y'
9221              WHERE
9222                  payment_instruction_id = p_instr_id
9223 	     AND (payment_status IN
9224                      (
9225                      PAY_STATUS_FORMATTED,
9226                      PAY_STATUS_TRANSMITTED,
9227                      PAY_STATUS_ACK,
9228                      PAY_STATUS_BNK_VALID,
9229                      PAY_STATUS_PAID
9230                      )
9231 	          OR
9232 	          (payment_status = PAY_STATUS_INS_CREAT AND l_completion_point ='CREATED')
9233 	         )
9234              ;
9235 
9236          ELSE
9237 
9238              /* quick/single payment */
9239 
9240              /*
9241               * The Post-PICP program is responsible for
9242               * setting the payment status to FORMATTED /
9243               * TRANSMITTED for electronic payments.
9244               *
9245               * However, when mark_all_payments(..) is called
9246               * for single payments, the Post-PICP would have
9247               * not yet executed (because it runs as a separate
9248               * conc program). Therefore, we set the payment
9249               * complete flag to 'Y' here even through the
9250               * payment is not technically complete.
9251               *
9252               * This special handling is applicable only to
9253               * single payments.
9254               */
9255 
9256              /*
9257               * Fix for bug 5179474:
9258               *
9259               * The PICP will now set the payment status to
9260               * FORMATTED without waiting for the Post-PICP
9261               * to complete.
9262               *
9263               * Reason for this change is so that the payment
9264               * status and the payment complete flag are in
9265               * sync (as in batch payments scenario).
9266               *
9267               * Therefore, mark the payment complete if the
9268               * payment status is FORMATTED.
9269               */
9270 
9271 	     l_completion_point := IBY_DISBURSE_UI_API_PUB_PKG.Get_Pmt_Completion_Point(p_instr_id);
9272 
9273              UPDATE
9274                  IBY_PAYMENTS_ALL
9275              SET
9276                  payments_complete_flag = 'Y'
9277              WHERE
9278                  payment_instruction_id = p_instr_id
9279 	     AND (payment_status IN
9280                      (
9281                      PAY_STATUS_FORMATTED
9282                      )
9283 	          OR
9284 	          (payment_status = PAY_STATUS_INS_CREAT AND l_completion_point ='CREATED')
9285 	         )
9286              ;
9287 
9288          END IF;
9289 
9290      END IF;
9291 
9292      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9293 	     print_debuginfo(l_module_name, 'Finished marking payments of '
9294 	         || 'payment instruction '
9295 	         || p_instr_id
9296 	         || ' as complete.'
9297 	         );
9298 
9299      END IF;
9300      /*
9301       * STEP IV:
9302       *
9303       * Get the 'process type' if this payment instruction.
9304       *
9305       * If this payment instruction has been created as part
9306       * of single payments flow, the process type will be
9307       * IMMEDIATE, else it will be STANDARD.
9308       *
9309       * We need to know the process type because we should not be
9310       * invoking the callout for quick payments (as these are
9311       * automatically marked as complete by AP).
9312       */
9313 
9314      /*
9315       * Fix for bug 4923416.
9316       *
9317       * Get the process type from the payment instruction itself
9318       * instead of deriving it from the payments of the instruction.
9319       */
9320      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9321 	     print_debuginfo(l_module_name, 'Process type for pmt instruction '
9322 	         || p_instr_id
9323 	         || ' is '
9324 	         || l_process_type
9325 	         );
9326 
9327      END IF;
9328      /*
9329       * STEP V:
9330       *
9331       * Mark the given payment instruction as completed.
9332       */
9333 
9334      UPDATE
9335          IBY_PAY_INSTRUCTIONS_ALL
9336      SET
9337          payments_complete_code = PMT_COMPLETE_YES
9338      WHERE
9339          payment_instruction_id = p_instr_id
9340      ;
9341 
9342      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9343 	     print_debuginfo(l_module_name, 'Finished marking payment instruction '
9344 	         || p_instr_id
9345 	         || ' as complete.'
9346 	         );
9347 
9348      END IF;
9349      /*
9350       * Pick up the names of all the applications which have
9351       * payments in the current payment instruction.
9352       *
9353       * Remember, one payment instruction can contain payments
9354       * across multiple calling applications.
9355       */
9356      OPEN  c_app_names (p_instr_id);
9357      FETCH c_app_names BULK COLLECT INTO l_appNamesTab;
9358      CLOSE c_app_names;
9359 
9360      /*
9361       * Pick up the ids of all the applications which have
9362       * payments in the current payment instruction.
9363       *
9364       * Remember, one payment instruction can contain payments
9365       * across multiple calling applications.
9366       */
9367      OPEN  c_app_ids (p_instr_id);
9368      FETCH c_app_ids BULK COLLECT INTO l_appIdsTab;
9369      CLOSE c_app_ids;
9370 
9371      /*
9372       * STEP VI:
9373       *
9374       * Loop through all the application names, invoking the
9375       * callout of each application.
9376       */
9377      FOR i IN l_appNamesTab.FIRST .. l_appNamesTab.LAST LOOP
9378 
9379          /*
9380           * Get the constructed package name to use in the
9381           * call out.
9382           */
9383          l_pkg_name := construct_callout_pkg_name(l_appNamesTab(i));
9384 
9385          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9386 	         print_debuginfo(l_module_name, 'Constructed package name: '
9387 	             || l_pkg_name);
9388 
9389          END IF;
9390          IF (l_pkg_name IS NULL) THEN
9391 
9392 
9393 	             print_debuginfo(l_module_name, 'Package name is null. '
9394 	                 || 'Raising exception.');
9395 
9396 
9397              APP_EXCEPTION.RAISE_EXCEPTION;
9398 
9399          END IF;
9400 
9401 
9402          /*
9403           * Get the next available completed payments group id.
9404           */
9405          SELECT
9406              IBY_COMPLETED_PMTS_GROUP_S.NEXTVAL
9407          INTO
9408              l_completion_id
9409          FROM
9410              DUAL
9411          ;
9412 
9413          /*
9414           * Update the completed payments for this calling app
9415           * with the completed document group id. The calling
9416           * application will identify completed documents using
9417           * this id.
9418           */
9419          IF (l_processing_type = P_TYPE_PRINTED) THEN
9420 
9421              UPDATE
9422                  IBY_PAYMENTS_ALL     pmt
9423              SET
9424                  pmt.completed_pmts_group_id = l_completion_id
9425              WHERE
9426                  pmt.payment_instruction_id = p_instr_id         AND
9427                  pmt.payments_complete_flag = 'Y'                AND
9428                  payment_status             = PAY_STATUS_ISSUED  AND
9429                  pmt.payment_id IN
9430                      (SELECT
9431                           doc.payment_id
9432                       FROM
9433                           IBY_DOCS_PAYABLE_ALL doc
9434                       WHERE
9435                           doc.payment_id     = pmt.payment_id AND
9436                           doc.calling_app_id = l_appIdsTab(i)
9437                       )
9438              ;
9439 
9440          ELSE
9441 
9442 	     l_completion_point := IBY_DISBURSE_UI_API_PUB_PKG.Get_Pmt_Completion_Point(p_instr_id);
9443 
9444              UPDATE
9445                  IBY_PAYMENTS_ALL     pmt
9446              SET
9447                  pmt.completed_pmts_group_id = l_completion_id
9448              WHERE
9449                  pmt.payment_instruction_id = p_instr_id         AND
9450                  pmt.payments_complete_flag = 'Y'                AND
9451                  (payment_status IN
9452                      (
9453                      PAY_STATUS_FORMATTED,
9454                      PAY_STATUS_TRANSMITTED,
9455                      PAY_STATUS_ACK,
9456                      PAY_STATUS_BNK_VALID,
9457                      PAY_STATUS_PAID
9458                      )
9459 	          OR
9460 	          (payment_status = PAY_STATUS_INS_CREAT AND l_completion_point ='CREATED')
9461 	         )
9462 		 AND pmt.payment_id IN
9463                      (SELECT
9464                           doc.payment_id
9465                       FROM
9466                           IBY_DOCS_PAYABLE_ALL doc
9467                       WHERE
9468                           doc.payment_id     = pmt.payment_id AND
9469                           doc.calling_app_id = l_appIdsTab(i)
9470                       )
9471              ;
9472 
9473          END IF;
9474 
9475          /*
9476           * DEBUG PRINT FOR PAYMENTS
9477           *
9478           * Print list of payments marked complete. This is
9479           * very important for debugging AP-IBY boundry issues.
9480           */
9481          IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
9482                print_completed_pmts(l_completion_id);
9483          END IF;
9484 
9485          /*
9486           * Update the documents of the completed payments with
9487           * the completed document group id. This will allow the
9488           * calling app to select the completed documents directly
9489           * if they so wish.
9490           */
9491          UPDATE
9492              IBY_DOCS_PAYABLE_ALL doc
9493          SET
9494              doc.completed_pmts_group_id = l_completion_id
9495          WHERE
9496              doc.document_status <> 'REMOVED' AND /* Bug 6388935- removed
9497 document handling */
9498              doc.payment_id IN
9499                  (SELECT
9500                       pmt.payment_id
9501                   FROM
9502                       IBY_PAYMENTS_ALL pmt
9503                   WHERE
9504                       pmt.completed_pmts_group_id = l_completion_id
9505                   )
9506          ;
9507 
9508          /*
9509           * DEBUG PRINT FOR DOCUMENTS
9510           *
9511           * Print list of documents payable marked complete. This is
9512           * very important for debugging AP-IBY boundry issues.
9513           */
9514          IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
9515                print_completed_docs(l_completion_id);
9516          END IF;
9517 
9518          /*
9519           * For single payments, do not invoke the callout because
9520           * single payments are automatically confirmed by AP.
9521           *
9522           * Only invoke callout where process type is not immediate.
9523           */
9524          IF (l_process_type <> PROCESS_TYPE_IMMEDIATE) THEN
9525 
9526              /*
9527               * Now try to call the external app's implementation of the hook.
9528               * The calling app may or may not have implemented the hook, so
9529               * it's not fatal if the implementation does not exist.
9530               */
9531              l_callout_name := l_pkg_name || '.' || 'payments_completed';
9532 
9533              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9534 	             print_debuginfo(l_module_name, 'Attempting to invoke callout: '
9535 	                 || l_callout_name, FND_LOG.LEVEL_UNEXPECTED);
9536 
9537              END IF;
9538              SELECT
9539                  count(*)
9540              INTO
9541                  l_pmt_count
9542              FROM
9543                  IBY_PAYMENTS_ALL pmt
9544              WHERE
9545                  pmt.completed_pmts_group_id = l_completion_id
9546              ;
9547 
9548              SELECT
9549                  count(*)
9550              INTO
9551                  l_doc_count
9552              FROM
9553                  IBY_DOCS_PAYABLE_ALL doc
9554              WHERE
9555                  doc.completed_pmts_group_id = l_completion_id
9556              ;
9557 
9558              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9559 	             print_debuginfo(l_module_name, 'Params passed to callout - '
9560 	                 || 'completed pmts group id: '
9561 	                 || l_completion_id,
9562 	                 FND_LOG.LEVEL_UNEXPECTED
9563 	                 );
9564 
9565 	             print_debuginfo(l_module_name, 'Params not passed to callout - '
9566 	                 || 'completed pmts count: '
9567 	                 || l_pmt_count
9568 	                 || ', completed docs count: '
9569 	                 || l_doc_count
9570 	                 );
9571 
9572              END IF;
9573              l_stmt := 'BEGIN '|| l_callout_name
9574                               || '(:1, :2, :3, :4, :5, :6, :7);
9575 		       EXCEPTION
9576 				WHEN OTHERS THEN
9577 					ROLLBACK;
9578 					RAISE;
9579 		       END;';
9580 
9581              BEGIN
9582 
9583 		  iby_debug_pub.log(debug_msg => 'Enter Callout pkg:'||l_callout_name||': Timestamp:'  || systimestamp,
9584 		       debug_level => FND_LOG.LEVEL_PROCEDURE,
9585 		       module => l_module_name);
9586 
9587                  EXECUTE IMMEDIATE
9588                      (l_stmt)
9589                  USING
9590                      IN  l_api_version,
9591                      IN  FND_API.G_FALSE,
9592                      IN  FND_API.G_FALSE,
9593                      OUT x_return_status,
9594                      OUT l_msg_count,
9595                      OUT l_msg_data,
9596                      IN  l_completion_id
9597                  ;
9598 
9599 		  iby_debug_pub.log(debug_msg => 'Exit Callout pkg:'||l_callout_name||': Timestamp:'  || systimestamp,
9600 		       debug_level => FND_LOG.LEVEL_PROCEDURE,
9601 		       module => l_module_name);
9602 
9603 
9604                  /*
9605                   * If the called procedure did not return success,
9606                   * raise an exception.
9607                   */
9608                  IF (x_return_status IS NULL OR
9609                      x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
9610 
9611 
9612 	                     print_debuginfo(l_module_name, 'Fatal: External app '
9613 	                         || 'callout '''
9614 	                         || l_callout_name
9615 	                         || ''', returned failure status - '
9616 	                         || x_return_status
9617 	                         || '. Raising exception.',
9618 	                         FND_LOG.LEVEL_UNEXPECTED
9619 	                         );
9620 
9621 
9622                      APP_EXCEPTION.RAISE_EXCEPTION;
9623 
9624                  END IF;
9625 
9626              EXCEPTION
9627 
9628                  WHEN PROCEDURE_NOT_IMPLEMENTED THEN
9629                      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9630 	                     print_debuginfo(l_module_name, 'Callout "'
9631 	                         || l_callout_name
9632 	                         || '" not implemented by calling app '
9633 	                         || l_app_short_name || '.');
9634 
9635 	                     print_debuginfo(l_module_name, 'Skipping hook call.');
9636 
9637                      END IF;
9638                  WHEN OTHERS THEN
9639 
9640 	                     print_debuginfo(l_module_name, 'Fatal: External app '
9641 	                         || 'callout '''
9642 	                         || l_callout_name
9643 	                         || ''', generated exception.',
9644 	                         FND_LOG.LEVEL_UNEXPECTED
9645 	                         );
9646 
9647 	                     print_debuginfo(l_module_name, 'SQL code: '
9648 	                         || SQLCODE, FND_LOG.LEVEL_UNEXPECTED);
9649 	                     print_debuginfo(l_module_name, 'SQL err msg: '
9650 	                         || SQLERRM, FND_LOG.LEVEL_UNEXPECTED);
9651 
9652 
9653                      /*
9654                       * Fix for bug 5608142:
9655                       *
9656                       * When mark complete API fails, it is most likely
9657                       * because document sequencing has not been setup
9658                       * correctly.
9659                       *
9660                       * Populate a message on the message stack so that
9661                       * this message can be displayed to the user.
9662                       */
9663                      l_error_code := 'IBY_COMPL_CALLOUT_ERROR';
9664                      FND_MESSAGE.set_name('IBY', l_error_code);
9665 
9666                      FND_MESSAGE.SET_TOKEN('CALLOUT',
9667                          l_callout_name,
9668                          FALSE);
9669                      /*
9670                       * Set the message on the error stack
9671                       * to display in UI (in case of direct
9672                       * API call from UI).
9673                       */
9674                      FND_MSG_PUB.ADD;
9675 
9676                      /*
9677                       * Set the error message on the concurrent
9678                       * program output file (to warn user that
9679                       * the hook failed).
9680                       */
9681                      FND_FILE.PUT_LINE(FND_FILE.OUTPUT, FND_MESSAGE.GET);
9682 
9683                      --FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
9684 
9685                      /*
9686                       * Propogate exception to caller.
9687                       */
9688                      RAISE;
9689 
9690              END;
9691 
9692          ELSE
9693 
9694              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9695 	             print_debuginfo(l_module_name, 'Not attempting to invoke callout '
9696 	                 || 'because this is a single payment.');
9697 
9698              END IF;
9699          END IF;
9700 
9701      END LOOP;
9702 
9703      /* Added for Bug: 7505803
9704       * New Status "COMPLETED" is added for Payment process Request.
9705       * Step VI (A):
9706       *
9707       * Mark Payment Process Requests as complete if all the payments that are not
9708       * rejected, removed are marked as complete.
9709       *
9710       * Get the list of PPRs whose payments are paid with this instruction and
9711       * find the list of PPRs for which all the payments are marked as complete.
9712       *
9713       * Note: An instruction may consists of payments belonging to different payment
9714       * process requests.
9715       *
9716       */
9717 
9718      /*
9719       * Pick up the distinct ppr ids for the
9720       * payments in the current payment instruction.
9721       *
9722       * Remember, one payment instruction can contain payments
9723       * across multiple PPRs.
9724       */
9725      OPEN  c_ppr_ids (p_instr_id);
9726      FETCH c_ppr_ids BULK COLLECT INTO l_pprIdsTab;
9727      CLOSE c_ppr_ids;
9728 
9729 
9730   IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9731 	  print_debuginfo(l_module_name, 'Changing Status of PPR to COMPLETED');
9732 
9733   END IF;
9734      /*
9735       * Loop through all the pprIds, to check whether all the payments
9736       * of that PPR are marked as complete.
9737       */
9738      FOR i IN l_pprIdsTab.FIRST .. l_pprIdsTab.LAST LOOP
9739      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9740 	     print_debuginfo(l_module_name, 'PPR ID::'||l_pprIdsTab(i));
9741      END IF;
9742          select count(*)
9743          into l_incomplete_pmts
9744          from iby_payments_all where
9745          payment_service_request_id = l_pprIdsTab(i)
9746          and payments_complete_flag = 'N'
9747          and payment_status not in
9748                                    ('REMOVED_INSTRUCTION_TERMINATED',
9749                                     'REMOVED',
9750                                     'REMOVED_PAYMENT_STOPPED',
9751                                     'REMOVED_DOCUMENT_SPOILED',
9752                                     'REJECTED',
9753                                     'FAILED_BY_CALLING_APP',
9754                                     'FAILED_BY_REJECTION_LEVEL',
9755                                     'FAILED_VALIDATION',
9756                                     'INSTRUCTION_FAILED_VALIDATION',
9757                                     'REMOVED_DOCUMENT_SKIPPED');
9758 
9759         if l_incomplete_pmts = 0 then
9760         IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9761 	        print_debuginfo(l_module_name, 'Changing Status of PPR ::'||l_pprIdsTab(i)||' as COMPLETED');
9762         END IF;
9763         update iby_pay_service_requests
9764         set payment_service_request_status = REQ_STATUS_COMPLETED,
9765 	last_update_date = sysdate,
9766         LAST_UPDATED_BY = NVL(FND_GLOBAL.user_id,-1),
9767 	LAST_UPDATE_LOGIN = NVL(FND_GLOBAL.login_id, -1)
9768         where payment_service_request_id = l_pprIdsTab(i);
9769         end if;
9770 
9771      END LOOP;
9772      /*
9773       * Step VII:
9774       *
9775       * Unlock the payment document that has been locked by this payment
9776       * instruction.
9777       */
9778 
9779      /*
9780       * For single payments, the payment document is not locked.
9781       * Consequently, there is no need to unlock it either.
9782       */
9783      IF (l_process_type <> PROCESS_TYPE_IMMEDIATE) THEN
9784 
9785          IF (l_processing_type = P_TYPE_PRINTED OR l_processing_type = P_TYPE_ELECTRONIC) THEN
9786 
9787              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9788 	             print_debuginfo(l_module_name, 'Unlocking payment document '
9789 	                 || 'locked by payment instruction '
9790 	                 || p_instr_id
9791 	             );
9792 
9793              END IF;
9794              BEGIN
9795 	       iby_debug_pub.log(debug_msg => 'Enter Paper Document Numbering: Timestamp:'  || systimestamp,
9796 		       debug_level => FND_LOG.LEVEL_PROCEDURE,
9797 		       module => l_module_name);
9798 
9799                 /* EFT Paper DOCUMENT NUMBER BEGIN*/
9800                 /* Bug 9376894*/
9801                  IF(l_processing_type = p_type_electronic) THEN
9802 
9803                    SELECT payment_document_id
9804                    INTO l_pmt_doc_id
9805                    FROM iby_pay_instructions_all
9806                    WHERE payment_instruction_id = p_instr_id;
9807 
9808                    IF(l_pmt_doc_id is not null) THEN
9809 
9810                       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9811 	                 print_debuginfo(l_module_name, 'Processing Type for Payment Instuction '
9812 	                   || p_instr_id ||' is '||p_type_electronic||
9813                            ' and Payment document ID is ' ||l_pmt_doc_id
9814 	               );
9815 
9816                       END IF;
9817 
9818                       INSERT INTO IBY_USED_PAYMENT_DOCS (
9819                                     PAYMENT_DOCUMENT_ID,
9820                                     USED_DOCUMENT_NUMBER,
9821                                     DATE_USED,
9822                                     DOCUMENT_USE,
9823                                     CREATED_BY,
9824                                     CREATION_DATE,
9825                                     LAST_UPDATED_BY,
9826                                     LAST_UPDATE_DATE,
9827                                     LAST_UPDATE_LOGIN,
9828                                     OBJECT_VERSION_NUMBER
9829                             )
9830                            (SELECT l_pmt_doc_id PAYMENT_DOCUMENT_ID,
9831                                    paper_document_number USED_DOCUMENT_NUMBER,
9832                                    sysdate DATE_USED,
9833                                    PAY_STATUS_ISSUED DOCUMENT_USE,
9834                                    fnd_global.user_id CREATED_BY,
9835                                    sysdate CREATION_DATE,
9836                                    fnd_global.user_id LAST_UPDATED_BY,
9837                                    sysdate LAST_UPDATE_DATE,
9838                                    fnd_global.login_id LAST_UPDATE_LOGIN,
9839                                    1 OBJECT_VERSION_NUMBER
9840                             FROM iby_payments_all
9841                             WHERE payment_instruction_id = p_instr_id AND
9842                             payment_status = PAY_STATUS_FORMATTED AND paper_document_number is not null);
9843                    END IF;
9844 
9845                  END IF;
9846 
9847                  /* EFT DOCUMENT NUMBER END*/
9848 		 iby_debug_pub.log(debug_msg => 'Exit Paper Document Numbering: Timestamp:'  || systimestamp,
9849 		       debug_level => FND_LOG.LEVEL_PROCEDURE,
9850 		       module => l_module_name);
9851                  UPDATE
9852                      CE_PAYMENT_DOCUMENTS
9853                  SET
9854                      payment_instruction_id = NULL,
9855 		     /* Bug 6707369
9856 		      * If some of the documents are skipped, the payment
9857 		      * document's last issued check number must be updated
9858 		      */
9859 		     last_issued_document_number = nvl(
9860 		     (SELECT MAX(pmt.paper_document_number)
9861 		      FROM iby_payments_all pmt
9862 		      WHERE pmt.payment_instruction_id = p_instr_id)
9863 		      ,last_issued_document_number
9864 		      )
9865                  WHERE
9866                      payment_instruction_id = p_instr_id
9867                  RETURNING
9868                      payment_document_id,
9869                      payment_document_name
9870                  INTO
9871                      l_pmt_doc_id,
9872                      l_pmt_doc_name
9873                  ;
9874 
9875                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9876 	                 print_debuginfo(l_module_name, 'Payment document id '
9877 	                     || ''''
9878 	                     || l_pmt_doc_id
9879 	                     || ''''
9880 	                     || ' with name '
9881 	                     || ''''
9882 	                     || l_pmt_doc_name
9883 	                     || ''''
9884 	                     || ' unlocked successfully.'
9885 	                     );
9886 
9887                  END IF;
9888              EXCEPTION
9889                  WHEN OTHERS THEN
9890 
9891                  /*
9892                   * This is a non-fatal exception. We will log the exception
9893                   * and return.
9894                   */
9895                  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9896 	                 print_debuginfo(l_module_name, 'Non-Fatal: Exception occured '
9897 	                     || 'when unlocking pmt document locked by pmt instruction '
9898 	                     || p_instr_id
9899 	                     );
9900 
9901 	                 print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
9902 	                 print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
9903 
9904                  END IF;
9905              END;
9906 
9907          /*ELSE
9908 
9909              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9910 	             print_debuginfo(l_module_name, 'Not attempting to unlock '
9911 	                 || 'payment document because this is an electronic '
9912 	                 || 'instruction'
9913 	                 );
9914 
9915              END IF;*/
9916          END IF; -- if processing type = PRINTED OR Electronic
9917 
9918      ELSE
9919 
9920          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9921 	         print_debuginfo(l_module_name, 'Not attempting to unlock '
9922 	             || 'payment document because this is a single '
9923 	             || 'payment (payment document was never locked)'
9924 	             );
9925 
9926          END IF;
9927      END IF; -- process type <> IMMEDIATE
9928 
9929      /*
9930       * Step VIII:
9931       *
9932       * Invoke payment related reports if necessary.
9933       *
9934       * These reports are:
9935       *   + Final payment instruction register
9936       *   + Separate remittance advice report
9937       */
9938 
9939     --Bug 6411356
9940     --below code added to set the current nls character setting
9941     --before submitting a child requests.
9942     fnd_profile.get('ICX_NUMERIC_CHARACTERS',l_icx_numeric_characters);
9943 
9944      /*
9945       *
9946       * Get the profile id for this payment instruction
9947       * along with the report related settings.
9948       */
9949      SELECT
9950          prof.payment_profile_id,
9951          prof.positive_pay_format_code,
9952          sys_prof.automatic_pi_reg_submit,
9953          sys_prof.pi_register_format,
9954          remit.automatic_sra_submit_flag,
9955          remit.remittance_advice_format_code
9956      INTO
9957          l_profile_id,
9958          l_pos_pay_format,
9959          l_auto_pi_reg_submit_flag,
9960          l_pi_reg_format,
9961          l_auto_sra_submit_flag,
9962          l_remit_format_code
9963      FROM
9964          IBY_PAYMENT_PROFILES     prof,
9965          IBY_SYS_PMT_PROFILES_B   sys_prof,
9966          IBY_PAY_INSTRUCTIONS_ALL inst,
9967          IBY_REMIT_ADVICE_SETUP   remit
9968      WHERE
9969          prof.payment_profile_id      = inst.payment_profile_id   AND
9970          sys_prof.system_profile_code = prof.system_profile_code  AND
9971          remit.system_profile_code    = prof.system_profile_code  AND
9972          inst.payment_instruction_id  = p_instr_id;
9973 
9974      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9975 	     print_debuginfo(l_module_name, 'For payment instruction '
9976 	         || p_instr_id
9977 	         || ', profile id: '
9978 	         || l_profile_id
9979 	         || ', auto pi register submit flag: '
9980 	         || l_auto_pi_reg_submit_flag
9981 	         || ', pi register format: '
9982 	         || l_pi_reg_format
9983 	         || ', auto remit advice submit flag: '
9984 	         || l_auto_sra_submit_flag
9985 	         || ', remittance advice format: '
9986 	         || l_remit_format_code
9987 	         || ', positive pay format: '
9988 	         || l_pos_pay_format
9989 	         );
9990 
9991      END IF;
9992      /*
9993       * Launch the Final Payment Instruction Register report.
9994       */
9995 
9996      /*
9997       * Fix for bug 5732799:
9998       *
9999       * Pass the payment instruction register format as a
10000       * param when launching the payment instruction register
10001       * report.
10002       */
10003      IF (UPPER(l_auto_pi_reg_submit_flag) = 'Y' AND
10004          l_pi_reg_format IS NOT NULL) THEN
10005 
10006          /*
10007           * Fix for bug 5407120:
10008           *
10009           * Before kicking off reports / formats make sure that
10010           * the numeric characters delimiter is correctly set
10011           * by using FND_REQUEST.SET_OPTIONS(..).
10012           *
10013           * The argument provided to this method is based on
10014           * the lookup ICX_NUMERIC_CHARACTERS.
10015           *
10016           * Otherwise, the num delimiter would be picked up
10017           * based on NLS territory and could cause problems.
10018           *
10019           * E.g., $10000.52 would be displayed as $10.000,52 for
10020           * PL territory and this causes problems to XML publisher.
10021           */
10022          l_bool_val := FND_REQUEST.SET_OPTIONS(
10023                            numeric_characters => l_icx_numeric_characters
10024                            );
10025 
10026          l_conc_req_id := FND_REQUEST.SUBMIT_REQUEST(
10027                              'IBY',
10028                              'IBY_FD_FINAL_PMT_REGISTER',
10029                              '',
10030                              '',
10031                              FALSE,
10032                              ''||p_instr_id||'',
10033                              ''||l_pi_reg_format||'',
10034                              '', '', '', '', '', '', '', '', '',
10035                              '', '', '', '', '', '', '', '', '', '',
10036                              '', '', '', '', '', '', '', '', '', '',
10037                              '', '', '', '', '', '', '', '', '', '',
10038                              '', '', '', '', '', '', '', '', '', '',
10039                              '', '', '', '', '', '', '', '', '', '',
10040                              '', '', '', '', '', '', '', '', '', '',
10041                              '', '', '', '', '', '', '', '', '', '',
10042                              '', '', '', '', '', '', '', '', '', '',
10043                              '', '', '', '', '', '', ''
10044                              );
10045 
10046          IF (l_conc_req_id = 0) THEN
10047 
10048 
10049 	             print_debuginfo(l_module_name, 'Request to launch payment '
10050 	                 || 'instruction register report concurrent program failed.');
10051 
10052              APP_EXCEPTION.RAISE_EXCEPTION;
10053 
10054          ELSE
10055 
10056              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10057 	             print_debuginfo(l_module_name, 'Payment instruction register '
10058 	                 || 'report was launched successfully. '
10059 	                 || 'Check concurrent request id :'
10060 	                 || to_char(l_conc_req_id)
10061 	                 );
10062 
10063              END IF;
10064          END IF;
10065 
10066      END IF;
10067 
10068      /*
10069       * Launch the Separate Remittance Advice report.
10070       */
10071      IF (UPPER(l_auto_sra_submit_flag) = 'Y') THEN
10072 
10073          l_bool_val := FND_REQUEST.SET_OPTIONS(
10074                            numeric_characters => l_icx_numeric_characters
10075                            );
10076 
10077          l_conc_req_id := FND_REQUEST.SUBMIT_REQUEST(
10078                              'IBY',
10079                              'IBY_FD_SRA_FORMAT',
10080                              '',
10081                              '',
10082                              FALSE,
10083                              ''||p_instr_id||'','','',
10084                              ''||l_remit_format_code||'',
10085                              '', '', '', '', '', '', '', '', '',
10086                              '', '', '', '', '', '', '', '', '', '',
10087                              '', '', '', '', '', '', '', '', '', '',
10088                              '', '', '', '', '', '', '', '', '', '',
10089                              '', '', '', '', '', '', '', '', '', '',
10090                              '', '', '', '', '', '', '', '', '', '',
10091                              '', '', '', '', '', '', '', '', '', '',
10092                              '', '', '', '', '', '', '', '', '', '',
10093                              '', '', '', '', '', '', '', '', '', '',
10094                              '', '', '', '', '', '', ''
10095                              );
10096 
10097          IF (l_conc_req_id = 0) THEN
10098 
10099 
10100 	             print_debuginfo(l_module_name, 'Request to launch separate '
10101 	                 || 'remittance advice report concurrent program failed.');
10102 
10103              APP_EXCEPTION.RAISE_EXCEPTION;
10104 
10105          ELSE
10106 
10107              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10108 	             print_debuginfo(l_module_name, 'Separate remittance advice '
10109 	                 || 'report was launched successfully. '
10110 	                 || 'Check concurrent request id :'
10111 	                 || to_char(l_conc_req_id)
10112 	                 );
10113 
10114              END IF;
10115          END IF;
10116 
10117      END IF;
10118 
10119 
10120      /*
10121       * Launch the Positive Pay program.
10122       */
10123      IF (p_submit_postive_pay = TRUE AND l_pos_pay_format IS NOT NULL) THEN
10124 
10125          l_bool_val := FND_REQUEST.SET_OPTIONS(
10126                            numeric_characters => l_icx_numeric_characters
10127                            );
10128 
10129          l_conc_req_id := FND_REQUEST.SUBMIT_REQUEST(
10130                              'IBY',
10131                              'IBY_FD_POS_PAY_FORMAT',
10132                              '',
10133                              '',
10134                              FALSE,
10135 
10136                              '', -- do not supply profile
10137                              '', -- do not supply from pmt date
10138                              '', -- do not supply to pmt date
10139                              ''||p_instr_id||'',
10140 
10141                              '', '', '', '', '', '', '',
10142                              '', '', '', '', '', '', '', '', '', '',
10143                              '', '', '', '', '', '', '', '', '', '',
10144                              '', '', '', '', '', '', '', '', '', '',
10145                              '', '', '', '', '', '', '', '', '', '',
10146                              '', '', '', '', '', '', '', '', '', '',
10147                              '', '', '', '', '', '', '', '', '', '',
10148                              '', '', '', '', '', '', '', '', '', '',
10149                              '', '', '', '', '', '', '', '', '', '',
10150                              '', '', '', '', '', '', ''
10151                              );
10152 
10153          IF (l_conc_req_id = 0) THEN
10154 
10155 
10156 	             print_debuginfo(l_module_name, 'Request to launch positive '
10157 	                 || 'pay concurrent program failed.');
10158 
10159              APP_EXCEPTION.RAISE_EXCEPTION;
10160 
10161          ELSE
10162 
10163              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10164 	             print_debuginfo(l_module_name, 'Positive pay '
10165 	                 || 'program was launched successfully. '
10166 	                 || 'Check concurrent request id :'
10167 	                 || to_char(l_conc_req_id)
10168 	                 );
10169 
10170              END IF;
10171          END IF;
10172 
10173      ELSE
10174 
10175          /*
10176           * In case we did not launch the positive pay
10177           * program, mention why.
10178           */
10179          IF (p_submit_postive_pay = FALSE) THEN
10180 
10181              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10182 	             print_debuginfo(l_module_name, 'Positive pay '
10183 	                 || 'program not launched because submit '
10184 	                 || 'positive pay flag was not set.'
10185 	                 );
10186 
10187              END IF;
10188          ELSE
10189 
10190              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10191 	             print_debuginfo(l_module_name, 'Positive pay '
10192 	                 || 'program not launched because positive '
10193 	                 || 'pay format was not set on profile id '
10194 	                 || l_profile_id
10195 	                 );
10196 
10197              END IF;
10198          END IF;
10199 
10200      END IF;
10201 
10202      x_return_status := FND_API.G_RET_STS_SUCCESS;
10203 
10204      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10205 	     print_debuginfo(l_module_name, 'Returning success response ..');
10206 
10207 	     print_debuginfo(l_module_name, 'EXIT');
10208 
10209      END IF;
10210  EXCEPTION
10211 
10212      WHEN OTHERS THEN
10213 
10214      --IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10215 	     print_debuginfo(l_module_name, 'Exception occured when '
10216 	         || 'marking payments complete for payment instruction '
10217 	         || p_instr_id
10218 	         );
10219 
10220 	     print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
10221 	     print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
10222      --END IF;  Bug 14335525
10223     -- FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
10224 
10225      x_return_status := FND_API.G_RET_STS_ERROR;
10226 
10227      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10228 	     print_debuginfo(l_module_name, 'EXIT');
10229 
10230      END IF;
10231  END mark_all_pmts_complete;
10232 
10233 /*--------------------------------------------------------------------
10234  | NAME:
10235  |     transmit_pmt_instruction
10236  |
10237  |
10238  | PURPOSE:
10239  |
10240  |
10241  | PARAMETERS:
10242  |     IN
10243  |
10244  |
10245  |     OUT
10246  |
10247  |
10248  | RETURNS:
10249  |
10250  | NOTES:
10251  |     Internal API, not for public use.
10252  |
10253  *---------------------------------------------------------------------*/
10254  PROCEDURE transmit_pmt_instruction (
10255      p_instr_id         IN NUMBER,
10256      p_trans_status     IN VARCHAR2,
10257      p_error_code       IN VARCHAR2,
10258      p_error_msg        IN VARCHAR2,
10259      x_return_status    OUT NOCOPY VARCHAR2
10260      )
10261  IS
10262 
10263  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
10264                                          || '.transmit_pmt_instruction';
10265 
10266  BEGIN
10267 
10268      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10269 	     print_debuginfo(l_module_name, 'ENTER');
10270      END IF;
10271      FND_MSG_PUB.initialize;
10272 
10273      x_return_status := FND_API.G_RET_STS_SUCCESS;
10274 
10275      -- Update payment transaction statuses (instruction, payments, etc.)
10276      IBY_FD_POST_PICP_PROGS_PVT.Post_Results(
10277          p_instr_id,
10278          p_trans_status,
10279          'N',
10280          x_return_status
10281          );
10282 
10283      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10284 	     print_debuginfo(l_module_name, 'Return status from Post_Results(..): '
10285 	         || x_return_status
10286 	         );
10287 
10288      END IF;
10289      if p_trans_status = 'TRANSMISSION_FAILED' then
10290         IBY_FD_POST_PICP_PROGS_PVT.Insert_Transmission_Error(p_instr_id,
10291                                                              p_error_code,
10292                                                              p_error_msg);
10293      end if;
10294 
10295      IF (x_return_status IS NULL OR
10296          x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
10297          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10298 	         print_debuginfo(l_module_name, 'Returning error response: '
10299 	             || x_return_status
10300 	             );
10301          END IF;
10302      ELSE
10303          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10304 	         print_debuginfo(l_module_name, 'Returning success response ..');
10305          END IF;
10306      END IF;
10307 
10308      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10309 	     print_debuginfo(l_module_name, 'EXIT');
10310 
10311      END IF;
10312  EXCEPTION
10313      WHEN OTHERS THEN
10314 
10315      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10316 	     print_debuginfo(l_module_name, 'Exception occured when '
10317 	         || 'performing transmit for payment instruction '
10318 	         || p_instr_id
10319 	         );
10320 
10321 	     print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
10322 	     print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
10323      END IF;
10324      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
10325 
10326      x_return_status := FND_API.G_RET_STS_ERROR;
10327 
10328      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10329 	     print_debuginfo(l_module_name, 'EXIT');
10330 
10331      END IF;
10332  END transmit_pmt_instruction;
10333 
10334 /*--------------------------------------------------------------------
10335  | NAME:
10336  |     construct_callout_pkg_name
10337  |
10338  |
10339  | PURPOSE:
10340  |
10341  |
10342  | PARAMETERS:
10343  |     IN
10344  |
10345  |
10346  |     OUT
10347  |
10348  |
10349  | RETURNS:
10350  |
10351  | NOTES:
10352  |
10353  *---------------------------------------------------------------------*/
10354  FUNCTION construct_callout_pkg_name (
10355      p_app_short_name IN VARCHAR2
10356      ) RETURN VARCHAR2
10357  IS
10358 
10359  l_pkg_prefix    VARCHAR2(100);
10360  l_pkg_suffix    VARCHAR2(100) := '_PMT_CALLOUT_PKG';
10361  l_pkg_name      VARCHAR2(200);
10362 
10363  l_module_name   CONSTANT VARCHAR2(200)  := G_PKG_NAME ||
10364                                             '.construct_callout_pkg_name';
10365 
10366  BEGIN
10367 
10368      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10369 	     print_debuginfo(l_module_name, 'ENTER');
10370 	     print_debuginfo(l_module_name, 'Provided app short name: '
10371 	         || p_app_short_name);
10372 
10373      END IF;
10374      IF (p_app_short_name IS NULL) THEN
10375 
10376          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10377 	         print_debuginfo(l_module_name, 'No app short name provided. '
10378 	             || 'Returning null ..'
10379 	         );
10380 
10381 	         print_debuginfo(l_module_name, 'EXIT');
10382 
10383          END IF;
10384          RETURN NULL;
10385 
10386      END IF;
10387 
10388      /*
10389       * When we invoke the hook procedure in the external app,
10390       * we must use the hook signature as <package name>.<procedure name>.
10391       *
10392       * For some applications, the application short name cannot
10393       * be directly used in forming the package name.
10394       *
10395       * Example, for AP, the application short name is 'SQLAP',
10396       * but the AP packages begin as 'AP_XXXX'. Therefore,
10397       * we will convert the application short names into package
10398       * prefixes here.
10399       */
10400      CASE p_app_short_name
10401          WHEN 'SQLAP' THEN
10402              l_pkg_prefix := 'AP';
10403          ELSE
10404              l_pkg_prefix := p_app_short_name;
10405      END CASE;
10406 
10407      l_pkg_name := l_pkg_prefix || l_pkg_suffix;
10408 
10409      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10410 	     print_debuginfo(l_module_name, 'Constructed package name: '
10411 	         || l_pkg_name);
10412 
10413 	     print_debuginfo(l_module_name, 'EXIT');
10414 
10415      END IF;
10416      RETURN l_pkg_name;
10417 
10418  END construct_callout_pkg_name;
10419 
10420 /*--------------------------------------------------------------------
10421  | NAME:
10422  |     print_debuginfo
10423  |
10424  | PURPOSE:
10425  |
10426  |
10427  | PARAMETERS:
10428  |     IN
10429  |
10430  |
10431  |     OUT
10432  |
10433  |
10434  | RETURNS:
10435  |
10436  | NOTES:
10437  |
10438  *---------------------------------------------------------------------*/
10439  PROCEDURE print_debuginfo(
10440      p_module      IN VARCHAR2,
10441      p_debug_text  IN VARCHAR2,
10442      p_debug_level IN VARCHAR2  DEFAULT FND_LOG.LEVEL_STATEMENT
10443      )
10444  IS
10445  l_default_debug_level VARCHAR2(200) := FND_LOG.LEVEL_STATEMENT;
10446  BEGIN
10447 
10448      /*
10449       * Set the debug level to the value passed in
10450       * (provided this value is not null).
10451       */
10452      IF (p_debug_level IS NOT NULL) THEN
10453          l_default_debug_level := p_debug_level;
10454      END IF;
10455 
10456      /*
10457       * Write the debug message to the concurrent manager log file.
10458       */
10459      --IF (l_default_debug_level >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
10460 
10461 		 iby_debug_pub.log(debug_msg => p_module||':'||p_debug_text,debug_level => p_debug_level, module => p_module);   --Bug 14335525
10462 
10463      --END IF;
10464 
10465  END print_debuginfo;
10466 
10467 /*--------------------------------------------------------------------
10468  | NAME:  get_message_text
10469  |
10470  |
10471  | PURPOSE:  This function will return the transalated message text
10472  |           for validation errors stored in iby_transaction_errors
10473  |           This function should be used from the UI pages.
10474  |
10475  | PARAMETERS:
10476  |     IN  p_transaction_error_id - Transaction error id for the error
10477  |     IN  p_error_code           - Error code.  This paramester is required
10478  |                                  so that the function does not hit the
10479  |                                  iby_transaction_errors table again.
10480  |
10481  | RETURNS: translated message_text
10482  |
10483  | NOTES:
10484  |
10485  *---------------------------------------------------------------------*/
10486  FUNCTION get_message_text (p_transaction_error_id IN NUMBER,
10487                             p_error_code           IN VARCHAR2)
10488  RETURN VARCHAR2 AS
10489   l_error_mes   varchar2(4000);
10490 
10491   TYPE l_token_info_rec IS RECORD(
10492     token_name          iby_trxn_error_tokens.token_name%TYPE,
10493     token_value         iby_trxn_error_tokens.token_value%TYPE,
10494     lookup_type_source  iby_trxn_error_tokens.lookup_type_source%TYPE,
10495     meaning             fnd_lookups.meaning%TYPE);
10496 
10497   TYPE l_token_info_tab IS TABLE OF l_token_info_rec;
10498 
10499   l_token_info  l_token_info_tab;
10500 
10501   CURSOR token_info(c_trxn_error_id IN iby_transaction_errors.transaction_error_id%TYPE) IS
10502   SELECT t.token_name, t.token_value, t.lookup_type_source, l.meaning
10503     FROM iby_trxn_error_tokens t, fnd_lookups l
10504    WHERE t.transaction_error_id = (c_trxn_error_id)
10505      AND t.lookup_type_source = l.lookup_type(+)
10506      AND t.token_value = l.lookup_code(+);
10507 
10508  BEGIN
10509 
10510    IF (p_transaction_error_id IS NOT NULL AND
10511        p_error_code IS NOT NULL) THEN
10512 
10513      FND_MESSAGE.SET_NAME('IBY', p_error_code);
10514 
10515      -- get token info if any.
10516      BEGIN
10517        OPEN token_info(p_transaction_error_id);
10518        FETCH token_info
10519        BULK COLLECT INTO l_token_info;
10520        CLOSE token_info;
10521      EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
10522      END;
10523 
10524      IF (l_token_info.COUNT > 0) THEN
10525        FOR j IN l_token_info.FIRST..l_token_info.LAST LOOP
10526          IF (l_token_info(j).lookup_type_source IS NULL) THEN
10527            FND_MESSAGE.SET_TOKEN(l_token_info(j).token_name, l_token_info(j).token_value);
10528          ELSE
10529            FND_MESSAGE.SET_TOKEN(l_token_info(j).token_name, l_token_info(j).meaning);
10530          END IF;
10531 
10532        END LOOP;
10533      END IF;
10534 
10535    END IF;
10536 
10537    l_error_mes := FND_MESSAGE.GET;
10538    RETURN  l_error_mes;
10539 
10540  END get_message_text;
10541 
10542 /*--------------------------------------------------------------------
10543  | NAME:  get_message_list
10544  |
10545  |
10546  | PURPOSE:  This function will return the transalated message text
10547  |           for validation errors stored in iby_transaction_errors for an
10548  |           specific transaction.  This function will return the list of
10549  |           messages in html format.
10550  |           This function should be used from the UI pages.
10551  |
10552  | PARAMETERS:
10553  |     IN  p_transaction_id   - Transaction id (instruction, payment or document id)
10554  |     IN  p_transaction_type - Transaction type.
10555  |                              (PAYMENT, PAYMENT INSTRUCTION, etc.)
10556  |
10557  | RETURNS: translated list of messages for a transaction in html format
10558  |
10559  | NOTES:
10560  |
10561  *---------------------------------------------------------------------*/
10562  FUNCTION get_message_list (p_transaction_id   IN NUMBER,
10563                             p_transaction_type IN VARCHAR2)
10564  RETURN VARCHAR2 AS
10565 
10566    l_contact_mes   varchar2(4000);
10567    TYPE l_mes_info_rec IS RECORD (
10568      trxn_error_id       iby_transaction_errors.transaction_error_id%TYPE,
10569      error_code          iby_transaction_errors.error_code%TYPE);
10570 
10571    TYPE l_token_info_rec IS RECORD(
10572      token_name          iby_trxn_error_tokens.token_name%TYPE,
10573      token_value         iby_trxn_error_tokens.token_value%TYPE,
10574      lookup_type_source  iby_trxn_error_tokens.lookup_type_source%TYPE,
10575      meaning             fnd_lookups.meaning%TYPE);
10576 
10577    TYPE l_mes_info_tab IS TABLE OF l_mes_info_rec;
10578    TYPE l_token_info_tab IS TABLE OF l_token_info_rec;
10579 
10580    l_mes_info    l_mes_info_tab;
10581    l_token_info  l_token_info_tab;
10582 
10583    CURSOR trxn_errors IS
10584    SELECT transaction_error_id, error_code
10585      FROM iby_transaction_errors
10586     WHERE transaction_id = p_transaction_id
10587       AND transaction_type = p_transaction_type
10588       AND error_status = 'ACTIVE';
10589 
10590    CURSOR token_info(c_trxn_error_id IN iby_transaction_errors.transaction_error_id%TYPE) IS
10591    SELECT t.token_name, t.token_value, t.lookup_type_source, l.meaning
10592      FROM iby_trxn_error_tokens t, fnd_lookups l
10593     WHERE t.transaction_error_id = (c_trxn_error_id)
10594       AND t.lookup_type_source = l.lookup_type(+)
10595       AND t.token_value = l.lookup_code(+);
10596 
10597  BEGIN
10598     BEGIN
10599       OPEN trxn_errors;
10600       FETCH trxn_errors
10601       BULK COLLECT INTO l_mes_info;
10602       CLOSE trxn_errors;
10603     EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
10604     END;
10605 
10606    IF (l_mes_info.COUNT > 0) THEN
10607      l_contact_mes := '<OL>';
10608      FOR i IN l_mes_info.FIRST..l_mes_info.LAST LOOP
10609        FND_MESSAGE.SET_NAME('IBY',l_mes_info(i).error_code);
10610 
10611        -- get token info if any.
10612        BEGIN
10613          OPEN token_info(l_mes_info(i).trxn_error_id);
10614          FETCH token_info
10615          BULK COLLECT INTO l_token_info;
10616          CLOSE token_info;
10617        EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
10618        END;
10619 
10620        IF (l_token_info.COUNT > 0) THEN
10621          FOR j IN l_token_info.FIRST..l_token_info.LAST LOOP
10622            IF (l_token_info(j).lookup_type_source IS NULL) THEN
10623              FND_MESSAGE.SET_TOKEN(l_token_info(j).token_name, l_token_info(j).token_value);
10624            ELSE
10625              FND_MESSAGE.SET_TOKEN(l_token_info(j).token_name, l_token_info(j).meaning);
10626            END IF;
10627 
10628          END LOOP;
10629        END IF;
10630 
10631        l_contact_mes := l_contact_mes||'<LI>'||FND_MESSAGE.GET||'</LI>';
10632 
10633      END LOOP;
10634      l_contact_mes := l_contact_mes||'</OL>';
10635 
10636    END IF;
10637    RETURN l_contact_mes;
10638  END get_message_list;
10639 
10640 /*--------------------------------------------------------------------
10641  | NAME:
10642  |     perform_check_print
10643  |
10644  | PURPOSE: This procedure calls the IBY_CHECKNUMBER_PUB.performCheckNumbering
10645  |          procedure to lock the payment document and number the payments
10646  |
10647  |
10648  | PARAMETERS:
10649  |     IN  p_instruction_id    -  Payment Instruction to print
10650  |     IN  p_pmt_document_id   -  Payment document id used to print
10651  |                                payment instruction
10652  |     IN  p_printer_name      -  Printer defined by the user
10653  |
10654  |     OUT x_return_status     -  Return status (S, E, U)
10655  |     OUT x_return_message    -  This error code will indicate if there
10656  |                                is any error during the numbering of the
10657  |                                payment instruction or if the payment
10658  |                                document cannot be locked.
10659  |
10660  | NOTES:  This procedure is only called from the Print UI since it will
10661  |         number the complete payment instruction.
10662  |
10663  | IMPORTANT NOTE:
10664  |         This procedure originally did not perform a COMMIT. However,
10665  |         after unexpected behaviors started occuring after lock/unlock
10666  |         APIs for handling intermediate statuses were introduced.
10667  |
10668  |         These APIs stamp a conc request id against a pmt entity
10669  |         (such as a pmt request or pmt instruction) and perform a commit.
10670  |
10671  |         If that underlying pmt entity has already been updated, but not
10672  |         committed a deadlock condition ensues and this API exits with
10673  |         error 'ORA-00060: deadlock detected while waiting for resource'.
10674  |
10675  |         Therefore, a COMMIT is performed after the payment instruction
10676  |         is updated in this method. The Post-PICP API will lock the
10677  |         same payment instruction by stamping the conc request id on it.
10678  |         This operation does not fail now because the changes made to the
10679  |         payment instruction have already been committed.
10680  |
10681  |         See bug 5195769 for an example of this deadlock scenario.
10682  |
10683  *---------------------------------------------------------------------*/
10684  PROCEDURE perform_check_print(
10685              p_instruction_id    IN NUMBER,
10686              p_pmt_document_id   IN NUMBER,
10687              p_printer_name      IN VARCHAR2,
10688              x_return_status     IN OUT NOCOPY VARCHAR2,
10689              x_return_message    IN OUT NOCOPY VARCHAR2) IS
10690 
10691  l_module_name  CONSTANT VARCHAR2(200) := G_PKG_NAME|| '.perform_check_print';
10692  l_instr_status IBY_PAY_INSTRUCTIONS_ALL.payment_instruction_status%TYPE;
10693 
10694  l_error_code   VARCHAR2(3000);
10695 
10696  l_msg_count    NUMBER;
10697  l_msg_data     VARCHAR2(4000);
10698 
10699  BEGIN
10700 
10701      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10702 	     print_debuginfo(l_module_name, 'ENTER');
10703 
10704      END IF;
10705      FND_MSG_PUB.initialize;
10706 
10707      /*
10708       * Get the status of the provided payment instruction.
10709       */
10710      BEGIN
10711 
10712          SELECT
10713              payment_instruction_status
10714          INTO
10715              l_instr_status
10716          FROM
10717              IBY_PAY_INSTRUCTIONS_ALL
10718          WHERE
10719              payment_instruction_id = p_instruction_id
10720          ;
10721 
10722      EXCEPTION
10723          WHEN OTHERS THEN
10724 
10725 
10726 	             print_debuginfo(l_module_name, 'Fatal: Exception occured '
10727 	                 || 'when attempting to get status of payment '
10728 	                 || 'instruction '
10729 	                 || p_instruction_id
10730 	                 || '. Aborting ..'
10731 	                 );
10732 
10733 
10734              l_error_code := 'IBY_INS_NOT_FOUND';
10735              FND_MESSAGE.set_name('IBY', l_error_code);
10736 
10737              FND_MESSAGE.SET_TOKEN('INS_ID',
10738                  p_instruction_id,
10739                  FALSE);
10740              FND_MSG_PUB.add;
10741              x_return_message := FND_MESSAGE.get;
10742 
10743              /*
10744               * Propogate exception.
10745               */
10746              RAISE;
10747      END;
10748 
10749      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10750 	     print_debuginfo(l_module_name, 'Status of payment instruction '
10751 	         || p_instruction_id
10752 	         || ' is '
10753 	         || l_instr_status
10754 	         );
10755 
10756      END IF;
10757      /*
10758       * First check whether the provided payment instruction
10759       * is in the right status.
10760       */
10761      IF (l_instr_status <> INS_STATUS_READY_TO_PRINT  AND
10762          l_instr_status <> INS_STATUS_READY_TO_FORMAT AND
10763          l_instr_status <> INS_STATUS_FORMAT_TO_PRINT AND
10764          l_instr_status <> PAY_STATUS_CREATED) THEN
10765 
10766 
10767 	             print_debuginfo(l_module_name, 'Payment instruction '
10768 	                 || p_instruction_id
10769 	                 || ' is in status '
10770 	                 || l_instr_status
10771 	                 || '. Cannot print this instruction ..'
10772 	                 );
10773 
10774 
10775              l_error_code := 'IBY_INS_NOT_PRINT_STATUS';
10776              FND_MESSAGE.set_name('IBY', l_error_code);
10777 
10778              FND_MESSAGE.SET_TOKEN('INS_ID',
10779                  p_instruction_id,
10780                  FALSE);
10781 
10782              FND_MESSAGE.SET_TOKEN('BAD_STATUS',
10783                  l_instr_status,
10784                  FALSE);
10785               FND_MSG_PUB.add;
10786              x_return_message := FND_MESSAGE.get;
10787 
10788              APP_EXCEPTION.RAISE_EXCEPTION;
10789 
10790      END IF;
10791 
10792      /*
10793       * If we reached here, the payment instruction is the
10794       * correct status.
10795       *
10796       * Update the printer associated with this payment instruction
10797       * with the provided printer name. The Post-PICP modules
10798       * will use this printer for printing the checks.
10799       */
10800      UPDATE
10801          IBY_PAY_INSTRUCTIONS_ALL
10802      SET
10803          printer_name = p_printer_name
10804      WHERE
10805          payment_instruction_id = p_instruction_id
10806      ;
10807 
10808      /*
10809       * Fix for bug 5195769:
10810       *
10811       * This commit is necessary. Otherwise a deadlocked
10812       * condition is created when we try to update the same
10813       * payment instruction with the concurrent request id
10814       * (for handling intermediate statuses).
10815       */
10816      COMMIT;
10817 
10818      /*
10819       * Lastly invoke numbering and/or printing depending
10820       * upon the status of the payment instruction.
10821       */
10822      IF (l_instr_status = INS_STATUS_FORMAT_TO_PRINT) THEN
10823 
10824          /*
10825           * This means that this payment instruction is
10826           * already formatted. Only printing is required.
10827           */
10828          BEGIN
10829 
10830              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10831 	             print_debuginfo(l_module_name, 'Going to invoke '
10832 	                 || 'extract and format operation for payment instruction '
10833 	                 || p_instruction_id
10834 	                 );
10835 
10836              END IF;
10837              IBY_FD_POST_PICP_PROGS_PVT.
10838                  Run_Post_PI_Programs(
10839                      p_instruction_id,
10840                      'N'
10841                      );
10842 
10843              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10844 	             print_debuginfo(l_module_name, 'Extract and format '
10845 	                 || 'operation completed successfully.'
10846 	                 );
10847 
10848              END IF;
10849          EXCEPTION
10850              WHEN OTHERS THEN
10851 
10852 
10853 	             print_debuginfo(l_module_name, 'Extract and '
10854 	                 || 'format operation generated '
10855 	                 || 'exception for payment instruction '
10856 	                 || p_instruction_id
10857 	                 );
10858 
10859 
10860              l_error_code := 'IBY_INS_BACKEND_ERROR';
10861              FND_MESSAGE.set_name('IBY', l_error_code);
10862 
10863              FND_MESSAGE.SET_TOKEN('INS_ID',
10864                  p_instruction_id,
10865                  FALSE);
10866               FND_MSG_PUB.add;
10867              x_return_message := FND_MESSAGE.get;
10868 
10869              /*
10870               * Propagate exception.
10871               */
10872              RAISE;
10873 
10874          END;
10875 
10876      ELSE
10877 
10878         /* Bug 10322160 - Check numbering should be performed only if
10879 	 * payment document is provided. */
10880 	 IF(p_pmt_document_id IS NOT NULL) THEN
10881 			 /*
10882 			  * This means that this payment instruction is not
10883 			  * yet numbered. Invoke the numbering flow.
10884 			  */
10885 			print_debuginfo(l_module_name, 'Performing Check Numbering : 1');
10886 			 IBY_CHECKNUMBER_PUB.performCheckNumbering(
10887 			     p_instruction_id,
10888 			     p_pmt_document_id,
10889 			     NULL,
10890 			     x_return_status,
10891 			     x_return_message,
10892 			     l_msg_count,
10893 			     l_msg_data
10894 			     );
10895 
10896 			 IF (x_return_status IS NULL OR
10897 			     x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
10898 
10899 
10900 				     print_debuginfo(l_module_name, 'Check numbering '
10901 					 || 'operation failed for payment instruction '
10902 					 || p_instruction_id
10903 					 || ' with error message: '
10904 					 || x_return_message
10905 					 );
10906 
10907 
10908 			     APP_EXCEPTION.RAISE_EXCEPTION;
10909 
10910 			 END IF;
10911 
10912 	 END IF;
10913 
10914          /*
10915           * After numbering, invoke the printing flow.
10916           */
10917          BEGIN
10918 
10919              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10920 	             print_debuginfo(l_module_name, 'Invoking extract and '
10921 	                 || 'format operation for payment instruction '
10922 	                 || p_instruction_id
10923 	                 );
10924 
10925              END IF;
10926              IBY_FD_POST_PICP_PROGS_PVT.
10927                  Run_Post_PI_Programs(
10928                      p_instruction_id,
10929                      'N'
10930                      );
10931 
10932              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10933 	             print_debuginfo(l_module_name, 'Extract and format '
10934 	                 || 'operation completed successfully'
10935 	                 );
10936 
10937              END IF;
10938          EXCEPTION
10939              WHEN OTHERS THEN
10940 
10941              l_error_code := 'IBY_INS_BACKEND_ERROR';
10942              FND_MESSAGE.set_name('IBY', l_error_code);
10943 
10944              FND_MESSAGE.SET_TOKEN('INS_ID',
10945                  p_instruction_id,
10946                  FALSE);
10947              FND_MSG_PUB.add;
10948              x_return_message := FND_MESSAGE.get;
10949 
10950 
10951 	             print_debuginfo(l_module_name, 'Extract and '
10952 	                 || 'format operation generated '
10953 	                 || 'exception for payment instruction '
10954 	                 || p_instruction_id
10955 	                 );
10956 
10957 
10958              /*
10959               * Propagate exception.
10960               */
10961              RAISE;
10962 
10963          END;
10964 
10965      END IF;
10966 
10967      /*
10968       * Return success.
10969       */
10970      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10971 	     print_debuginfo(l_module_name, 'Returning success response ..');
10972      END IF;
10973      x_return_status := FND_API.G_RET_STS_SUCCESS;
10974 
10975      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10976 	     print_debuginfo(l_module_name, 'EXIT');
10977 
10978      END IF;
10979  EXCEPTION
10980      WHEN OTHERS THEN
10981 
10982      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10983 	     print_debuginfo(l_module_name, 'Exception occured when '
10984 	         || 'performing check print'
10985 	         );
10986 
10987 	     print_debuginfo(l_module_name, 'SQL code: '
10988 	         || SQLCODE);
10989 	     print_debuginfo(l_module_name, 'SQL err msg: '
10990 	         || SQLERRM);
10991      END IF;
10992      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
10993 
10994      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10995 	     print_debuginfo(l_module_name, 'Returning failure response .. '
10996 	         );
10997 
10998      END IF;
10999      /*
11000       * The error message would have already been set.
11001       * Just set the error status here.
11002       */
11003      x_return_status := FND_API.G_RET_STS_ERROR;
11004 
11005      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11006 	     print_debuginfo(l_module_name, 'EXIT');
11007 
11008      END IF;
11009  END perform_check_print;
11010 
11011 /*--------------------------------------------------------------------
11012  | NAME:
11013  |     checkIfDocUsed
11014  |
11015  | PURPOSE:
11016  |     Checks if a given paper document number has already been used.
11017  |     A used paper document implies that a check has already been
11018  |     printed for the given paper document number.
11019  |
11020  |
11021  | PARAMETERS:
11022  |     IN
11023  |       p_paper_doc_num   - Paper document number (check number) to verify.
11024  |
11025  |       p_pmt_document_id - Payment document id (check stock) that this
11026  |                           paper document number belongs to.
11027  |     OUT
11028  |       NONE
11029  |
11030  | RETURNS:
11031  |       TRUE  - if the paper document number has already been used.
11032  |       FALSE - if the paper document number has not been used.
11033  |
11034  | NOTES:
11035  |
11036  *---------------------------------------------------------------------*/
11037  FUNCTION checkIfDocUsed(
11038      p_paper_doc_num     IN NUMBER,
11039      p_pmt_document_id   IN NUMBER
11040      ) RETURN BOOLEAN
11041  IS
11042 
11043  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
11044                                          || '.checkIfDocUsed';
11045 
11046  l_flag BOOLEAN;
11047  l_used_paper_doc_number NUMBER := 0;
11048 
11049  BEGIN
11050 
11051      /*
11052       * Check if this paper document number has already
11053       * been used by searching the the IBY_USED_PAYMENT_DOCS
11054       * table.
11055       */
11056      SELECT
11057          used_document_number
11058      INTO
11059          l_used_paper_doc_number
11060      FROM
11061          IBY_USED_PAYMENT_DOCS
11062      WHERE
11063          payment_document_id  = p_pmt_document_id     AND
11064          used_document_number = p_paper_doc_num AND
11065 	 document_use <> 'SKIPPED'
11066      ;
11067 
11068      /*
11069       * If we reached here, it means that a row has been
11070       * found in the IBY_USED_PAYMENT_DOCS table for the
11071       * given (paper doc number, payment document id)
11072       * combination. This means that the paper document
11073       * is used.
11074       *
11075       * Set the return flag to true.
11076       */
11077      l_flag := TRUE;
11078 
11079      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11080 	     print_debuginfo(l_module_name, 'Returning TRUE.');
11081      END IF;
11082      RETURN l_flag;
11083 
11084  EXCEPTION
11085 
11086      WHEN NO_DATA_FOUND THEN
11087 
11088          /* now rows means success */
11089          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11090 	         print_debuginfo(l_module_name, 'Paper document '
11091 	             || 'number '
11092 	             || p_paper_doc_num
11093 	             || ' is unused.'
11094 	             );
11095 
11096          END IF;
11097          /* set the return flag to false */
11098          l_flag := FALSE;
11099 
11100          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11101 	         print_debuginfo(l_module_name, 'Returning FALSE.');
11102          END IF;
11103          RETURN l_flag;
11104 
11105      WHEN OTHERS THEN
11106 
11107 
11108 	         print_debuginfo(l_module_name, 'Exception occured when '
11109 	             || 'attempting to get details of paper document '
11110 	             || p_paper_doc_num
11111 	             || ' from IBY_USED_PAYMENT_DOCS table.'
11112 	             );
11113 
11114 	         print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
11115 	         print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
11116 
11117 	         print_debuginfo(l_module_name, 'Unexpected error. '
11118 	             || 'Raising exception ..');
11119 
11120 
11121          APP_EXCEPTION.RAISE_EXCEPTION;
11122 
11123  END checkIfDocUsed;
11124 
11125 /*--------------------------------------------------------------------
11126  | NAME:
11127  |     insert_conc_request
11128  |
11129  | PURPOSE:
11130  |     Inserts a given concurrent request id into the
11131  |     IBY_PROCESS_CONC_REQUESTS table for audit purposes.
11132  |
11133  | PARAMETERS:
11134  |     IN
11135  |     p_object_id         The id of the payment entity. This can be
11136  |                         a payment id, a payment request id or a payment
11137  |                         instruction id.
11138  |
11139  |     p_object_type       The type of the payment entity. This can be
11140  |                         one of the following
11141  |                             PAYMENT
11142  |                             PAYMENT_REQUEST
11143  |                             PAYMENT_INSTRUCTION
11144  |
11145  |     p_conc_request_id   The concurrent request id.
11146  |
11147  |     p_completed_flag    Flag indicating whether the concurrent request
11148  |                         has completed.
11149  |
11150  |     OUT
11151  |     x_return_status     Return status (S, E, U)
11152  |
11153  | RETURNS:
11154  |
11155  | NOTES:
11156  |
11157  *---------------------------------------------------------------------*/
11158  PROCEDURE insert_conc_request(
11159              p_object_id         IN NUMBER,
11160              p_object_type       IN VARCHAR2,
11161              p_conc_request_id   IN NUMBER,
11162              p_completed_flag    IN VARCHAR2 DEFAULT 'N',
11163              x_return_status     IN OUT NOCOPY VARCHAR2
11164              )
11165  IS
11166 
11167  l_module_name  CONSTANT VARCHAR2(200) := G_PKG_NAME|| '.insert_conc_request';
11168  l_dup_count NUMBER;
11169 
11170  BEGIN
11171 
11172      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11173 	     print_debuginfo(l_module_name, 'ENTER');
11174 
11175 	     print_debuginfo(l_module_name, 'Inserting conc request id '
11176 	         || p_conc_request_id
11177 	         || ' into IBY_PROCESS_CONC_REQUESTS for object id '
11178 	         || p_object_id
11179 	         || ' with object type '
11180 	         || p_object_type
11181 	         );
11182 
11183      END IF;
11184 
11185      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11186 	     print_debuginfo(l_module_name, 'Checking if a duplicate conc request record exists');
11187      END IF;
11188 
11189      SELECT COUNT(*) dup_count
11190      INTO l_dup_count
11191      FROM IBY_PROCESS_CONC_REQUESTS
11192      WHERE OBJECT_ID = p_object_id
11193      AND OBJECT_TYPE = p_object_type
11194      AND REQUEST_ID = p_conc_request_id
11195      ;
11196 
11197      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11198 	     print_debuginfo(l_module_name, 'Concurrent request l_dup_count : ' || l_dup_count);
11199      END IF;
11200 
11201 
11202      IF (l_dup_count > 0) THEN
11203 
11204 	     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11205 		     print_debuginfo(l_module_name, 'By passing conc request insertion');
11206 	     END IF;
11207 
11208      ELSE
11209 
11210 	     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11211 		     print_debuginfo(l_module_name, 'Inserting conc request');
11212 	     END IF;
11213 
11214 
11215 	     INSERT INTO IBY_PROCESS_CONC_REQUESTS
11216 		 (
11217 		 OBJECT_ID,             /* 1 */
11218 		 OBJECT_TYPE,
11219 		 REQUEST_ID,
11220 		 COMPLETED_FLAG,
11221 		 CREATED_BY,            /* 5 */
11222 		 CREATION_DATE,
11223 		 LAST_UPDATED_BY,
11224 		 LAST_UPDATE_DATE,
11225 		 LAST_UPDATE_LOGIN,
11226 		 OBJECT_VERSION_NUMBER  /* 10 */
11227 		 )
11228 		 VALUES
11229 		 (
11230 		 p_object_id,           /* 1 */
11231 		 p_object_type,
11232 		 p_conc_request_id,
11233 		 p_completed_flag,
11234 		 fnd_global.user_id,    /* 5 */
11235 		 sysdate,
11236 		 fnd_global.user_id,
11237 		 sysdate,
11238 		 fnd_global.login_id,
11239 		 1                      /* 10 */
11240 		 )
11241 		 ;
11242      END IF;
11243 
11244      x_return_status := FND_API.G_RET_STS_SUCCESS;
11245      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11246 	     print_debuginfo(l_module_name, 'EXIT');
11247 
11248      END IF;
11249  EXCEPTION
11250      WHEN OTHERS THEN
11251 
11252          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11253 	         print_debuginfo(l_module_name, 'Exception occured when '
11254 	             || 'inserting conc request id '
11255 	             || p_conc_request_id
11256 	             || ' into IBY_PROCESS_CONC_REQUESTS for object id '
11257 	             || p_object_id
11258 	             || ' with object type '
11259 	             || p_object_type
11260 	             || '.'
11261 	             );
11262 
11263 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
11264 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
11265 
11266          END IF;
11267          FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
11268          x_return_status := FND_API.G_RET_STS_ERROR;
11269 
11270          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11271 	         print_debuginfo(l_module_name, 'EXIT');
11272 
11273          END IF;
11274  END insert_conc_request;
11275 
11276 /*--------------------------------------------------------------------
11277  | NAME:
11278  |     lock_pmt_entity
11279  |
11280  | PURPOSE:
11281  |     Stamps the given payment entity with the given concurrent
11282  |     request id to lock the entity.
11283  |
11284  |     The IBY UI will not allow any follow on operation on a payment
11285  |     entity that is locked.
11286  |
11287  |     Locking/unlocking is the mechanism by which to preserve
11288  |     data integrity of payment entities which are in transient
11289  |     statuses.
11290  |
11291  |     This method also inserts the provided concurrent request into
11292  |     IBY_PROCESS_CONC_REQUESTS table for audit purposes.
11293  |
11294  | PARAMETERS:
11295  |     IN
11296  |     p_object_id         The id of the payment entity. This can be
11297  |                         a payment id, a payment request id or a payment
11298  |                         instruction id.
11299  |
11300  |     p_object_type       The type of the payment entity. This can be
11301  |                         one of the following
11302  |                             PAYMENT
11303  |                             PAYMENT_REQUEST
11304  |                             PAYMENT_INSTRUCTION
11305  |
11306  |     p_conc_request_id   The concurrent request id to stamp the payment
11307  |                         entity with. If NULL is provided, the value of
11308  |                         FND_GLOBAL.CONC_REQUEST_ID will be used.
11309  |
11310  |     OUT
11311  |     x_return_status     Return status (S, E, U)
11312  |
11313  | RETURNS:
11314  |
11315  | NOTES:
11316  |     This method is implemented as an autonomous transaction
11317  |     so that a COMMIT can be performed on the payment entity
11318  |     without side effects on the main transaction.
11319  |
11320  *---------------------------------------------------------------------*/
11321  PROCEDURE lock_pmt_entity(
11322              p_object_id         IN NUMBER,
11323              p_object_type       IN VARCHAR2,
11324              p_conc_request_id   IN NUMBER DEFAULT NULL,
11325              x_return_status     IN OUT NOCOPY VARCHAR2
11326              )
11327  IS
11328  PRAGMA AUTONOMOUS_TRANSACTION;
11329 
11330  l_module_name     CONSTANT VARCHAR2(200) := G_PKG_NAME|| '.lock_pmt_entity';
11331  l_conc_request_id NUMBER;
11332  l_template_type_code xdo_templates_b.template_type_code%type := null;
11333  l_default_output_type xdo_templates_b.default_output_type%type := null;
11334 
11335  BEGIN
11336 
11337      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11338 	     print_debuginfo(l_module_name, 'ENTER');
11339 
11340      END IF;
11341      FND_MSG_PUB.initialize;
11342 
11343      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11344 	     print_debuginfo(l_module_name, 'Input params - '
11345 	         || 'object id: '
11346 	         || p_object_id
11347 	         || ', object type: '
11348 	         || p_object_type
11349 	         );
11350 
11351      END IF;
11352      /*
11353       * STEP 1:
11354       *
11355       * If the concurrent request id has not been provided,
11356       * derive it from the current context.
11357       */
11358      IF (p_conc_request_id IS NULL) THEN
11359 
11360          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11361 	         print_debuginfo(l_module_name, 'Concurrent request id not '
11362 	             || 'provided. Deriving from context ..'
11363 	             );
11364 
11365          END IF;
11366          l_conc_request_id := FND_GLOBAL.conc_request_id;
11367 
11368      ELSE
11369 
11370          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11371 	         print_debuginfo(l_module_name, 'Concurrent request id '
11372 	             || 'provided as input param: '
11373 	             || p_conc_request_id
11374 	             );
11375 
11376          END IF;
11377          l_conc_request_id := p_conc_request_id;
11378 
11379      END IF;
11380 
11381      /*
11382       * STEP 2:
11383       *
11384       * Stamp the given entity with the provided concurrent request id.
11385       */
11386      IF (p_object_type = 'PAYMENT_REQUEST') THEN
11387 
11388          UPDATE
11389              IBY_PAY_SERVICE_REQUESTS req
11390          SET
11391              req.request_id = l_conc_request_id,
11392 	     last_update_date = sysdate,
11393              LAST_UPDATED_BY = NVL(FND_GLOBAL.user_id,-1),
11394 	     LAST_UPDATE_LOGIN = NVL(FND_GLOBAL.login_id, -1)
11395          WHERE
11396              req.payment_service_request_id = p_object_id
11397          ;
11398 
11399      ELSIF (p_object_type = 'PAYMENT_INSTRUCTION') THEN
11400         IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11401 	        print_debuginfo(l_module_name, 'Value of the Payment Instruction Id is: ' || p_object_id);
11402         END IF;
11403      BEGIN
11404         SELECT  temp.template_type_code, temp.default_output_type
11405           INTO l_template_type_code, l_default_output_type
11406           FROM iby_pay_instructions_all ins,
11407                iby_payment_profiles pp,
11408                iby_formats_b format,
11409                XDO_TEMPLATES_B temp
11410            WHERE ins.payment_instruction_id  = p_object_id
11411            AND ins.payment_profile_id        = pp.payment_profile_id
11412            AND format.FORMAT_CODE            = pp.PAYMENT_FORMAT_CODE
11413            AND format.FORMAT_TEMPLATE_CODE   = temp.template_code;
11414      EXCEPTION
11415         WHEN OTHERS THEN
11416             IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11417 	            print_debuginfo(l_module_name, 'SQL Query giving Other Exception Setting Default Values for the variables');
11418             END IF;
11419             l_template_type_code := null;
11420             l_default_output_type := null;
11421      END;
11422 
11423          IF(l_template_type_code = 'RTF' AND l_default_output_type = 'EXCEL') THEN
11424             IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11425 	            print_debuginfo(l_module_name, 'Updating the output file type to' || l_default_output_type);
11426             END IF;
11427              UPDATE FND_CONCURRENT_REQUESTS
11428              SET output_file_type = 'XLS'
11429              WHERE request_id = l_conc_request_id;
11430          END IF;
11431 
11432          UPDATE
11433              IBY_PAY_INSTRUCTIONS_ALL inst
11434          SET
11435              inst.request_id = l_conc_request_id
11436          WHERE
11437              inst.payment_instruction_id = p_object_id
11438          ;
11439 
11440      ELSIF (p_object_type = 'PAYMENT') THEN
11441 
11442          UPDATE
11443              IBY_PAYMENTS_ALL pmt
11444          SET
11445              pmt.request_id = l_conc_request_id
11446          WHERE
11447              pmt.payment_id = p_object_id
11448          ;
11449 
11450      ELSE
11451 
11452 
11453 	         print_debuginfo(l_module_name, 'Unknown payment entity type '
11454 	             || p_object_type
11455 	             || ' provided. Raising exception .. '
11456 	             );
11457 
11458 
11459          APP_EXCEPTION.RAISE_EXCEPTION;
11460 
11461      END IF;
11462 
11463      /*
11464       * STEP 3:
11465       *
11466       * Insert concurrent request into audit table.
11467       */
11468 
11469      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11470 	     print_debuginfo(l_module_name, 'Inserting cp into audit table');
11471 
11472      END IF;
11473      insert_conc_request(
11474              p_object_id,
11475              p_object_type,
11476              l_conc_request_id,
11477              'N',
11478              x_return_status
11479              );
11480 
11481      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11482 	     print_debuginfo(l_module_name, 'Return status from '
11483 	         || 'insert_conc_request(..) call = '
11484 	         || x_return_status
11485 	         );
11486 
11487 
11488      END IF;
11489      IF (x_return_status IS NULL OR
11490          x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
11491 
11492          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11493 	         print_debuginfo(l_module_name, 'Audit of conc request '
11494 	             || l_conc_request_id
11495 	             || ' failed. Returning error response .. '
11496 	             );
11497 
11498          END IF;
11499          /*
11500           * Undo any database changes made in this method.
11501           */
11502          ROLLBACK;
11503 
11504          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11505 	         print_debuginfo(l_module_name, 'EXIT');
11506          END IF;
11507          RETURN;
11508 
11509      END IF;
11510 
11511      /*
11512       * STEP 4:
11513       *
11514       * Finally, perform a COMMIT to lock the payment entity
11515       * with a concurrent request id.
11516       */
11517      COMMIT;
11518 
11519      x_return_status := FND_API.G_RET_STS_SUCCESS;
11520      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11521 	     print_debuginfo(l_module_name, 'Returning success response ..');
11522 
11523 	     print_debuginfo(l_module_name, 'EXIT');
11524 
11525      END IF;
11526  EXCEPTION
11527      WHEN OTHERS THEN
11528 
11529      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11530 	     print_debuginfo(l_module_name, 'Exception occured when locking '
11531 	         || 'pmt entity.');
11532 
11533 	     print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
11534 	     print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
11535 
11536      END IF;
11537      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
11538 
11539      ROLLBACK;
11540 
11541      x_return_status := FND_API.G_RET_STS_ERROR;
11542 
11543      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11544 	     print_debuginfo(l_module_name, 'EXIT');
11545 
11546      END IF;
11547  END lock_pmt_entity;
11548 
11549 /*--------------------------------------------------------------------
11550  | NAME:
11551  |     unlock_pmt_entity
11552  |
11553  | PURPOSE:
11554  |     Nulls out the concurrent request id on given payment entity
11555  |     to unlock the entity.
11556  |
11557  |     The IBY UI will not allow any follow on operation on a payment
11558  |     entity that is locked.
11559  |
11560  |     Locking/unlocking is the mechanism by which to preserve
11561  |     data integrity of payment entities which are in transient
11562  |     statuses.
11563  |
11564  | PARAMETERS:
11565  |     IN
11566  |     p_object_id         The id of the payment entity. This can be
11567  |                         a payment id, a payment request id or a payment
11568  |                         instruction id.
11569  |
11570  |     p_object_type       The type of the payment entity. This can be
11571  |                         one of the following
11572  |                             PAYMENT
11573  |                             PAYMENT_REQUEST
11574  |                             PAYMENT_INSTRUCTION
11575  |
11576  |     OUT
11577  |     x_return_status     Return status (S, E, U)
11578  |
11579  | RETURNS:
11580  |
11581  | NOTES:
11582  |     This method is implemented as an autonomous transaction
11583  |     so that a COMMIT can be performed on the payment entity
11584  |     without side effects on the main transaction.
11585  |
11586  *---------------------------------------------------------------------*/
11587  PROCEDURE unlock_pmt_entity(
11588              p_object_id         IN NUMBER,
11589              p_object_type       IN VARCHAR2,
11590              x_return_status     IN OUT NOCOPY VARCHAR2
11591              )
11592  IS
11593  PRAGMA AUTONOMOUS_TRANSACTION;
11594 
11595  l_module_name     CONSTANT VARCHAR2(200) := G_PKG_NAME|| '.unlock_pmt_entity';
11596 
11597  BEGIN
11598 
11599      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11600 	     print_debuginfo(l_module_name, 'ENTER');
11601 
11602 	     print_debuginfo(l_module_name, 'Input params - '
11603 	         || 'object id: '
11604 	         || p_object_id
11605 	         || ', object type: '
11606 	         || p_object_type
11607 	         );
11608 
11609      END IF;
11610      /*
11611       * STEP 1:
11612       *
11613       * Unstamp the given entity with the provided concurrent request id.
11614       */
11615      IF (p_object_type = 'PAYMENT_REQUEST') THEN
11616 
11617          UPDATE
11618              IBY_PAY_SERVICE_REQUESTS req
11619          SET
11620              req.request_id = NULL,
11621 	     last_update_date = sysdate,
11622              LAST_UPDATED_BY = NVL(FND_GLOBAL.user_id,-1),
11623 	     LAST_UPDATE_LOGIN = NVL(FND_GLOBAL.login_id, -1)
11624          WHERE
11625              req.payment_service_request_id = p_object_id
11626          ;
11627 
11628      ELSIF (p_object_type = 'PAYMENT_INSTRUCTION') THEN
11629 
11630          UPDATE
11631              IBY_PAY_INSTRUCTIONS_ALL inst
11632          SET
11633              inst.request_id = NULL
11634          WHERE
11635              inst.payment_instruction_id = p_object_id
11636          ;
11637 
11638      ELSIF (p_object_type = 'PAYMENT') THEN
11639 
11640          UPDATE
11641              IBY_PAYMENTS_ALL pmt
11642          SET
11643              pmt.request_id = NULL
11644          WHERE
11645              pmt.payment_id = p_object_id
11646          ;
11647 
11648      ELSE
11649 
11650 
11651 	         print_debuginfo(l_module_name, 'Unknown payment entity type '
11652 	             || p_object_type
11653 	             || ' provided. Raising exception .. '
11654 	             );
11655 
11656 
11657          APP_EXCEPTION.RAISE_EXCEPTION;
11658 
11659      END IF;
11660 
11661      /*
11662       * STEP 3:
11663       *
11664       * Finally, perform a COMMIT to unlock the payment entity
11665       * with a concurrent request id.
11666       */
11667      COMMIT;
11668 
11669      x_return_status := FND_API.G_RET_STS_SUCCESS;
11670      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11671 	     print_debuginfo(l_module_name, 'Returning success response ..');
11672 
11673 	     print_debuginfo(l_module_name, 'EXIT');
11674 
11675      END IF;
11676  EXCEPTION
11677      WHEN OTHERS THEN
11678 
11679      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11680 	     print_debuginfo(l_module_name, 'Exception occured when unlocking '
11681 	         || 'pmt entity.');
11682 
11683 	     print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
11684 	     print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
11685 
11686      END IF;
11687      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
11688 
11689      ROLLBACK;
11690 
11691      x_return_status := FND_API.G_RET_STS_ERROR;
11692 
11693      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11694 	     print_debuginfo(l_module_name, 'EXIT');
11695 
11696      END IF;
11697  END unlock_pmt_entity;
11698 
11699 /*--------------------------------------------------------------------
11700  | NAME:
11701  |     populatePaymentFunctions
11702  |
11703  |
11704  | PURPOSE:
11705  |     Populate the payment functions that user has access to
11706  |
11707  | PARAMETERS:
11708  |     IN
11709  |
11710  |
11711  |     OUT
11712  |
11713  |
11714  | RETURNS:
11715  |
11716  | NOTES:
11717  |
11718  *---------------------------------------------------------------------*/
11719  PROCEDURE populatePaymentFunctions(
11720              x_return_status     IN OUT NOCOPY VARCHAR2,
11721              x_return_message    IN OUT NOCOPY VARCHAR2) IS
11722 
11723  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
11724                                          || '.populatePaymentFunctions';
11725 l_func varchar2(30);
11726 l_bool boolean;
11727 l_count number;
11728 cursor c_pay_function is
11729 select lookup_code
11730 from fnd_lookups
11731 where lookup_type='IBY_PAYMENT_FUNCTIONS';
11732 
11733 begin
11734 
11735 FND_MSG_PUB.initialize;
11736 
11737 -- first check whether the payment function is populated or not
11738   begin
11739    select count(1)
11740     into l_count
11741    from IBY_USER_PAY_FUNS_GT;
11742    exception
11743       WHEN OTHERS THEN
11744 
11745          /* now rows means success */
11746          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11747 	         print_debuginfo(l_module_name, 'Exception in select ');
11748          END IF;
11749           null;
11750      end;
11751 
11752    if(l_count<>0) then
11753         x_return_status := FND_API.G_RET_STS_SUCCESS;
11754         return;
11755    end if;
11756 
11757     OPEN  c_pay_function;
11758     loop
11759      FETCH c_pay_function INTO l_func;
11760      exit when c_pay_function%NOTFOUND;
11761       -- check security
11762     l_bool := fnd_function.test_instance(
11763    'IBY_' || l_func,
11764    null,
11765    null,
11766    null,
11767    null,
11768    null,
11769    null,
11770    fnd_global.user_name);
11771 
11772    if(l_bool) then
11773    --print_debuginfo(l_module_name, 'User has access to:' || l_func);
11774     insert into IBY_USER_PAY_FUNS_GT(payment_function)
11775     values(l_func);
11776 	end if;
11777       end loop;
11778       close c_pay_function;
11779 
11780    x_return_status := FND_API.G_RET_STS_SUCCESS;
11781         return;
11782    EXCEPTION
11783 
11784     WHEN OTHERS THEN
11785      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11786 	     print_debuginfo(l_module_name, 'SQL err msg: '
11787 	       || SQLERRM);
11788 	     print_debuginfo(l_module_name, 'Returning failure response .. '
11789 	       );
11790      END IF;
11791      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
11792      /*
11793       * The error message would have already been set.
11794       * Just set the error status here.
11795       */
11796      x_return_status := FND_API.G_RET_STS_ERROR;
11797 
11798      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11799 	     print_debuginfo(l_module_name, 'EXIT');
11800 
11801      END IF;
11802 end populatePaymentFunctions;
11803 
11804 
11805 
11806 /*--------------------------------------------------------------------
11807  | NAME:
11808  |     checkUserFunctionAccess
11809  |
11810  |
11811  | PURPOSE:
11812  |     Check whether the user has access to a particular function
11813  |
11814  | PARAMETERS:
11815  |     IN
11816  |
11817  |
11818  |     OUT
11819  |
11820  |
11821  | RETURNS:
11822  |
11823  | NOTES:
11824  |
11825  *---------------------------------------------------------------------*/
11826 FUNCTION checkUserFunctionAccess(p_function_name in VARCHAR2) return VARCHAR2
11827 IS
11828   l_boolean BOOLEAN := FALSE;
11829   l_result VARCHAR2(1);
11830   l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
11831                                          || '.checkUserFunctionAccess';
11832 begin
11833   IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11834 	  print_debuginfo(l_module_name, 'Begin checkUserFunctionAccess');
11835 	  print_debuginfo(l_module_name, 'Checking user function access' || p_function_name);
11836 
11837   END IF;
11838   l_boolean := FND_FUNCTION.TEST(p_function_name);
11839 
11840   IF ( l_boolean = TRUE ) THEN
11841      l_result := 'Y';
11842   ELSE
11843      l_result := 'N';
11844   END IF;
11845 
11846   IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11847 	  print_debuginfo(l_module_name, 'End checkUserFunctionAccess');
11848   END IF;
11849   RETURN l_result;
11850 
11851 EXCEPTION
11852 WHEN OTHERS THEN
11853   IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11854 	  print_debuginfo(l_module_name, 'checkUserFunctionAccess failed');
11855   END IF;
11856   l_result := 'N';
11857   RETURN l_result;
11858 
11859 END checkUserFunctionAccess;
11860 
11861 
11862 /*--------------------------------------------------------------------
11863  | NAME:
11864  |     checkUserAccess
11865  |
11866  |
11867  | PURPOSE:
11868  |     Check whether the user has access to all the org. or payment function
11869  |      of a particular object.
11870  |
11871  | PARAMETERS:
11872  |     IN
11873  |
11874  |
11875  |     OUT
11876  |
11877  |
11878  | RETURNS:
11879  |
11880  | NOTES:
11881  |
11882  *---------------------------------------------------------------------*/
11883 PROCEDURE checkUserAccess(
11884              p_object_id         IN NUMBER,
11885              p_object_type       IN VARCHAR2,
11886              x_access_flag       IN OUT NOCOPY VARCHAR2,
11887              x_return_status     IN OUT NOCOPY VARCHAR2,
11888              x_return_message    IN OUT NOCOPY VARCHAR2)
11889 IS
11890  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
11891                                          || '.checkUserAccess';
11892 
11893  l_function_count NUMBER:=0;
11894  l_org_count NUMBER:=0;
11895  l_msg_count NUMBER:=0;
11896  l_message_code VARCHAR2(30);
11897 begin
11898 
11899 -- initiate the return parameter
11900 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11901 	print_debuginfo(l_module_name, 'ENTER');
11902 
11903 END IF;
11904 FND_MSG_PUB.initialize;
11905 populatePaymentFunctions(x_return_status, x_return_message);
11906 x_access_flag:='N';
11907 
11908  /* Initialize return status */
11909 x_return_status := FND_API.G_RET_STS_SUCCESS;
11910 
11911 select count(1)
11912 into l_function_count
11913 from iby_process_functions pfun
11914 where object_id=p_object_id
11915 and   object_type=p_object_type
11916 and pfun.payment_function not in
11917        (select payment_function
11918         from IBY_USER_PAY_FUNS_GT
11919         );
11920 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11921 	print_debuginfo(l_module_name, 'Number of Missing Payment Function:' ||l_function_count);
11922 
11923 END IF;
11924 select count(1)
11925 into l_org_count
11926 from iby_process_orgs porg
11927 where object_id=p_object_id
11928 and   object_type=p_object_type
11929 and porg.org_id not in
11930        (select  organization_id
11931         from ce_security_profiles_v cep
11932         where cep.organization_type=porg.org_type
11933         );
11934 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11935 	print_debuginfo(l_module_name, 'Number of Missing Org:' ||l_org_count);
11936 END IF;
11937 	-- check the user access
11938     if(l_function_count<>0 OR l_org_count <>0) THEN
11939             IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11940             print_debuginfo(l_module_name, 'User doesnt have full access to the payment request or payment instruction');
11941             END IF;
11942 
11943   -- return error message for different object type
11944 
11945        if(p_object_type='PAYMENT_REQUEST') then
11946          l_message_code :='IBY_FD_ACCESS_REQUEST_ERROR';
11947        else
11948          l_message_code :='IBY_FD_ACCESS_INSTR_ERROR';
11949 
11950        end if;
11951 
11952 
11953         FND_MESSAGE.set_name('IBY', l_message_code);
11954         FND_MSG_PUB.add;
11955 
11956         x_return_status := FND_API.G_RET_STS_ERROR;
11957 
11958         FND_MSG_PUB.Count_And_Get(
11959                                   p_encoded => FND_API.G_FALSE,
11960                                   p_count   => l_msg_count,
11961                                   p_data    => x_return_message
11962                                   );
11963 
11964              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11965 	             print_debuginfo(l_module_name, 'Returning error response ..');
11966 	             print_debuginfo(l_module_name, 'Error count:' || l_msg_count);
11967 	             print_debuginfo(l_module_name, 'Error Text:' || x_return_message);
11968 	             print_debuginfo(l_module_name, 'EXIT');
11969              END IF;
11970    end if;
11971 
11972 EXCEPTION
11973 WHEN OTHERS THEN
11974    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11975 	   print_debuginfo(l_module_name, 'SQL err msg: '
11976 	       || SQLERRM);
11977 	     print_debuginfo(l_module_name, 'Returning failure response .. '
11978 	       );
11979      END IF;
11980      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
11981      /*
11982       * The error message would have already been set.
11983       * Just set the error status here.
11984       */
11985      x_return_status := FND_API.G_RET_STS_ERROR;
11986 
11987      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11988 	     print_debuginfo(l_module_name, 'EXIT');
11989      END IF;
11990 end checkUserAccess;
11991 
11992 /*--------------------------------------------------------------------
11993  | NAME:
11994  |     checkIfAllPmtsTerminated
11995  |
11996  | PURPOSE:
11997  |     Checks if all payments of a given payment instruction
11998  |     have been terminated.
11999  |
12000  | PARAMETERS:
12001  |     IN
12002  |       p_instr_id   - Payment instruction id to verify.
12003  |
12004  |     OUT
12005  |       NONE
12006  |
12007  | RETURNS:
12008  |       TRUE  - if all payments have been terminated.
12009  |       FALSE - if at least one non-terminated payment exists.
12010  |
12011  | NOTES:
12012  |
12013  *---------------------------------------------------------------------*/
12014  FUNCTION checkIfAllPmtsTerminated(
12015      p_instr_id          IN NUMBER
12016      ) RETURN BOOLEAN
12017  IS
12018 
12019  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
12020                                          || '.checkIfAllPmtsTerminated';
12021 
12022  l_flag             BOOLEAN := FALSE;
12023 
12024  l_all_pmts_count   NUMBER;
12025  l_term_pmts_count  NUMBER;
12026 
12027  BEGIN
12028 
12029      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12030 	     print_debuginfo(l_module_name, 'ENTER');
12031 
12032      END IF;
12033      SELECT
12034          COUNT(*)
12035      INTO
12036          l_all_pmts_count
12037      FROM
12038          IBY_PAYMENTS_ALL pmt
12039      WHERE
12040          pmt.payment_instruction_id = p_instr_id
12041      ;
12042 
12043 
12044      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12045 	     print_debuginfo(l_module_name, 'Count of all payments in pmt instr '
12046 	         || p_instr_id
12047 	         || ' = '
12048 	         || l_all_pmts_count
12049 	         );
12050 
12051      END IF;
12052      SELECT
12053          COUNT(*)
12054      INTO
12055          l_term_pmts_count
12056      FROM
12057          IBY_PAYMENTS_ALL pmt
12058      WHERE
12059          pmt.payment_instruction_id = p_instr_id
12060      AND
12061          pmt.payment_status = PAY_STATUS_REQ_TERM
12062      ;
12063 
12064      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12065 	     print_debuginfo(l_module_name, 'Count of terminated payments in '
12066 	         || 'pmt instr '
12067 	         || p_instr_id
12068 	         || ' = '
12069 	         || l_all_pmts_count
12070 	         );
12071 
12072      END IF;
12073      /*
12074       * If all payments have been terminated return TRUE.
12075       */
12076      IF (l_all_pmts_count = l_term_pmts_count) THEN
12077 
12078          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12079 	         print_debuginfo(l_module_name, 'All payments in pmt instr '
12080 	             || p_instr_id
12081 	             || ' have been terminated. Returning TRUE.'
12082 	             );
12083 
12084          END IF;
12085          l_flag := TRUE;
12086 
12087      ELSE
12088 
12089          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12090 	         print_debuginfo(l_module_name, 'At least one payment in pmt instr '
12091 	             || p_instr_id
12092 	             || ' has not been terminated. Returning FALSE.'
12093 	             );
12094 
12095          END IF;
12096          l_flag := FALSE;
12097 
12098      END IF;
12099 
12100      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12101 	     print_debuginfo(l_module_name, 'EXIT');
12102 
12103      END IF;
12104      RETURN l_flag;
12105 
12106  EXCEPTION
12107 
12108      WHEN OTHERS THEN
12109 
12110          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12111 	         print_debuginfo(l_module_name, 'Exception occured when '
12112 	             || 'checking if all pmts of pmt instruction '
12113 	             || p_instr_id
12114 	             || ' were terminated.'
12115 	             );
12116 
12117 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
12118 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
12119 
12120          END IF;
12121          l_flag := FALSE;
12122          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12123 	         print_debuginfo(l_module_name, 'Returning FALSE.');
12124 	         print_debuginfo(l_module_name, 'EXIT');
12125 
12126          END IF;
12127          RETURN l_flag;
12128 
12129  END checkIfAllPmtsTerminated;
12130 
12131 /*--------------------------------------------------------------------
12132  | NAME:
12133  |     checkIfPmtInInstExists
12134  |
12135  | PURPOSE:
12136  |     Checks if all payments of a given payment instruction
12137  |     have been terminated.
12138  |
12139  | PARAMETERS:
12140  |     IN
12141  |       p_payreq_id - Payment request to verify.
12142  |
12143  |     OUT
12144  |       NONE
12145  |
12146  | RETURNS:
12147  |       TRUE  - if at least one payment of the ppr is part of
12148  |                   an instruction.
12149  |       FALSE - if none of the payments of the ppr are part of
12150  |                   an instruction.
12151  |
12152  | NOTES:
12153  |       This method is meant exclusively to be used by the
12154  |       terminate_pmt_request(..) API. Do not use call this
12155  |       method for other general purposes.
12156  |
12157  |
12158  |       -- IMPORTANT --
12159  |       This method locks rows in the IBY_PAYMENTS_ALL table and
12160  |       expects that the caller will commit/rollback to release lock.
12161  |
12162  *---------------------------------------------------------------------*/
12163  FUNCTION checkIfPmtInInstExists(
12164      p_payreq_id          IN NUMBER
12165      ) RETURN BOOLEAN
12166  IS
12167 
12168  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
12169                                          || '.checkIfPmtInInstExists';
12170 
12171  l_flag             BOOLEAN := FALSE;
12172  l_test             VARCHAR2(200);
12173 
12174  l_all_pmts_count   NUMBER;
12175  l_term_pmts_count  NUMBER;
12176 
12177  BEGIN
12178 
12179      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12180 	     print_debuginfo(l_module_name, 'ENTER');
12181 
12182      END IF;
12183      /*
12184       * This method is called by terminate_pmt_request(..)
12185       * to check if any of the payments are part of a payment
12186       * instruction.
12187       *
12188       * TIME T1: This method returns FALSE
12189       *          implying that none of the
12190       *          payments of the ppr are part
12191       *          of any payment instruction.
12192       *
12193       * TIME T2: PICP runs and picks up payments
12194       *          of which some payments are from this
12195       *          ppr.
12196       *
12197       * TIME T3: terminate_pmt_request(..) terminates
12198       *          a ppr based on the return value of
12199       *          this method. Now, we have an payment
12200       *          instruction that contains a payment
12201       *          from this ppr that has been terminated.
12202       *          This situation is wrong and should never
12203       *          occur.
12204       *
12205       * To prevent this situation, we lock the payments
12206       * table first, before attempting to terminate
12207       * a ppr.
12208       *
12209       * This lock will get released when the UI peforms a
12210       * COMMIT / ROLLBACK action based on the return value
12211       * of the terminate_pmt_request(..) API.
12212       */
12213 
12214      /*
12215       * Instead of locking the entire table only lock the
12216       * rows that we are interested in (explicit locking).
12217       */
12218      -- LOCK TABLE IBY_PAYMENTS_ALL IN EXCLUSIVE MODE;
12219 
12220      SELECT
12221          pmt.payment_id
12222      INTO
12223          l_test
12224      FROM
12225          IBY_PAYMENTS_ALL pmt
12226      WHERE
12227          pmt.payment_instruction_id IS NOT NULL
12228      AND
12229          pmt.payment_service_request_id = p_payreq_id
12230      FOR UPDATE
12231      ;
12232 
12233      /*
12234       * Now check whether the payments of this request
12235       * are linked to any payment instructions.
12236       */
12237      SELECT
12238          'TRUE'
12239      INTO
12240          l_test
12241      FROM
12242          DUAL
12243      WHERE
12244          EXISTS
12245          (
12246           SELECT
12247               pmt.payment_id
12248           FROM
12249               IBY_PAYMENTS_ALL pmt
12250           WHERE
12251               pmt.payment_instruction_id IS NOT NULL
12252           AND
12253               pmt.payment_service_request_id = p_payreq_id
12254           )
12255      ;
12256 
12257      IF (l_test = 'TRUE') THEN
12258          l_flag := TRUE;
12259          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12260 	         print_debuginfo(l_module_name, 'Returning flag as TRUE.');
12261          END IF;
12262      ELSE
12263          l_flag := FALSE;
12264          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12265 	         print_debuginfo(l_module_name, 'Returning flag as FALSE.');
12266          END IF;
12267      END IF;
12268 
12269      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12270 	     print_debuginfo(l_module_name, 'EXIT');
12271 
12272      END IF;
12273      RETURN l_flag;
12274 
12275  EXCEPTION
12276 
12277      WHEN NO_DATA_FOUND THEN
12278 
12279          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12280 	         print_debuginfo(l_module_name, 'Payment request id '
12281 	             || p_payreq_id
12282 	             || ' does not contain any payments.'
12283 	             );
12284 
12285          END IF;
12286          /*
12287           * This ppr only contains documents and no payments.
12288           *
12289           * Therefore, allow this ppr to be terminated by
12290           * returning FALSE.
12291           */
12292          l_flag := FALSE;
12293          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12294 	         print_debuginfo(l_module_name, 'Returning FALSE.');
12295 	         print_debuginfo(l_module_name, 'EXIT');
12296 
12297          END IF;
12298          RETURN l_flag;
12299 
12300      WHEN OTHERS THEN
12301 
12302          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12303 	         print_debuginfo(l_module_name, 'Exception occured when '
12304 	             || 'checking if any pmts of pmt request '
12305 	             || p_payreq_id
12306 	             || ' were part of a pmt instruction.'
12307 	             );
12308 
12309 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
12310 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
12311 
12312          END IF;
12313          /*
12314           * In case of an exception, play safe and return TRUE.
12315           *
12316           * The terminate_pmt_request(..) API will not allow the
12317           * request to be terminated.
12318           */
12319          l_flag := TRUE;
12320          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12321 	         print_debuginfo(l_module_name, 'Returning TRUE.');
12322 	         print_debuginfo(l_module_name, 'EXIT');
12323 
12324          END IF;
12325          RETURN l_flag;
12326 
12327  END checkIfPmtInInstExists;
12328 
12329 /*--------------------------------------------------------------------
12330  | NAME:
12331  |     finalize_electronic_instr
12332  |
12333  |
12334  | PURPOSE:
12335  |
12336  |
12337  | PARAMETERS:
12338  |     IN
12339  |
12340  |
12341  |     OUT
12342  |
12343  |
12344  | RETURNS:
12345  |
12346  | NOTES:
12347  |     Internal API, not for public use.
12348  |
12349  *---------------------------------------------------------------------*/
12350  PROCEDURE finalize_electronic_instr(
12351              p_instr_id          IN NUMBER,
12352              x_return_status     IN OUT NOCOPY VARCHAR2
12353              )
12354  IS
12355 
12356  l_module_name     CONSTANT VARCHAR2(200)
12357                                 := G_PKG_NAME ||
12358                                        '.finalize_electronic_instr';
12359  l_completion_point       VARCHAR2(30);
12360  l_flag            BOOLEAN := FALSE;
12361 
12362  BEGIN
12363 
12364      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12365 	     print_debuginfo(l_module_name, 'ENTER');
12366      END IF;
12367      FND_MSG_PUB.initialize;
12368 
12369      /*
12370       * STEP 1:
12371       *
12372       * Check if payment instruction transmission is outside
12373       * the system.
12374       *
12375       * Update the payment instruction and payment statuses
12376       * to final statuses accordingly.
12377       */
12378      l_flag := checkIfInstrXmitOutsideSystem(p_instr_id);
12379      l_completion_point := IBY_DISBURSE_UI_API_PUB_PKG.Get_Pmt_Completion_Point(p_instr_id);
12380 
12381      IF (l_completion_point <> 'CREATED') THEN
12382 	     IF (l_flag = TRUE) THEN
12383 
12384 		 UPDATE
12385 		     IBY_PAY_INSTRUCTIONS_ALL inst
12386 		 SET
12387 		     inst.payment_instruction_status = INS_STATUS_FORMATTED_ELEC
12388 		 WHERE
12389 		     inst.payment_instruction_id = p_instr_id
12390 		 ;
12391 
12392 		 UPDATE
12393 		     IBY_PAYMENTS_ALL pmt
12394 		 SET
12395 		     pmt.payment_status = PAY_STATUS_FORMATTED
12396 		 WHERE
12397 		     pmt.payment_instruction_id = p_instr_id
12398 		 ;
12399 
12400 	     ELSE
12401 
12402 		 UPDATE
12403 		     IBY_PAY_INSTRUCTIONS_ALL inst
12404 		 SET
12405 		     inst.payment_instruction_status = INS_STATUS_TRANSMITTED
12406 		 WHERE
12407 		     inst.payment_instruction_id = p_instr_id
12408 		 ;
12409 
12410 		 UPDATE
12411 		     IBY_PAYMENTS_ALL pmt
12412 		 SET
12413 		     pmt.payment_status = PAY_STATUS_TRANSMITTED
12414 		 WHERE
12415 		     pmt.payment_instruction_id = p_instr_id
12416 		 ;
12417 
12418 	     END IF;
12419 
12420      END IF;
12421 
12422      /*
12423       * STEP 2:
12424       *
12425       * Mark the payment instruction and payments complete.
12426       */
12427      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12428 	     print_debuginfo(l_module_name,
12429 	         'Invoking mark payments complete API ..');
12430 
12431      END IF;
12432      mark_all_pmts_complete(p_instr_id, x_return_status);
12433 
12434      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12435 	     print_debuginfo(l_module_name, 'Returning status: '
12436 	         || x_return_status
12437 	         );
12438 
12439 	     print_debuginfo(l_module_name, 'EXIT');
12440 
12441      END IF;
12442  EXCEPTION
12443      WHEN OTHERS THEN
12444 
12445          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12446 	         print_debuginfo(l_module_name, 'Exception occured when '
12447 	             || 'finalizing electronic payment instruction '
12448 	             || p_instr_id
12449 	             || '.'
12450 	             );
12451 
12452 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
12453 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
12454 
12455          END IF;
12456          x_return_status := FND_API.G_RET_STS_ERROR;
12457 
12458          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12459 	         print_debuginfo(l_module_name, 'Returning error status: '
12460 	             || x_return_status
12461 	             );
12462 	         print_debuginfo(l_module_name, 'EXIT');
12463 
12464          END IF;
12465  END finalize_electronic_instr;
12466 
12467 /*--------------------------------------------------------------------
12468  | NAME:
12469  |     checkIfInstrXmitOutsideSystem
12470  |
12471  |
12472  | PURPOSE:
12473  |
12474  |
12475  | PARAMETERS:
12476  |     IN
12477  |
12478  |
12479  |     OUT
12480  |
12481  |
12482  | RETURNS:
12483  |
12484  | NOTES:
12485  |     Internal API, not for public use.
12486  |
12487  *---------------------------------------------------------------------*/
12488  FUNCTION checkIfInstrXmitOutsideSystem(
12489      p_instr_id          IN NUMBER
12490      ) RETURN BOOLEAN
12491  IS
12492 
12493  l_module_name CONSTANT VARCHAR2(200)
12494                             := G_PKG_NAME
12495                                    || '.checkIfInstrXmitOutsideSystem';
12496 
12497  l_flag             BOOLEAN := FALSE;
12498  l_test             VARCHAR2(200);
12499 
12500  BEGIN
12501 
12502      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12503 	     print_debuginfo(l_module_name, 'ENTER');
12504 
12505      END IF;
12506      /*
12507       * If the transmit configuration id linked to the profile
12508       * on a payment instruction is null, it means that transmit
12509       * configuration has not been set up for that profile.
12510       *
12511       * This implies that the payment instruction needs to be
12512       * transmitted outside the system (i.e., using an external
12513       * application).
12514       */
12515      SELECT
12516          'TRUE'
12517      INTO
12518          l_test
12519      FROM
12520          DUAL
12521      WHERE
12522          EXISTS
12523          (
12524           SELECT
12525               instr.payment_instruction_id
12526           FROM
12527               IBY_PAY_INSTRUCTIONS_ALL instr,
12528               IBY_PAYMENT_PROFILES     prof
12529           WHERE
12530               instr.payment_instruction_id = p_instr_id
12531           AND
12532               prof.payment_profile_id = instr.payment_profile_id
12533           AND
12534               prof.transmit_configuration_id IS NULL
12535           )
12536      ;
12537 
12538      IF (l_test = 'TRUE') THEN
12539          l_flag := TRUE;
12540          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12541 	         print_debuginfo(l_module_name, 'Transmission is outside the system.');
12542 	         print_debuginfo(l_module_name, 'Returning flag as TRUE.');
12543          END IF;
12544      ELSE
12545          l_flag := FALSE;
12546          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12547 	         print_debuginfo(l_module_name, 'Transmission is inside the system.');
12548 	         print_debuginfo(l_module_name, 'Returning flag as FALSE.');
12549          END IF;
12550      END IF;
12551 
12552      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12553 	     print_debuginfo(l_module_name, 'EXIT');
12554 
12555      END IF;
12556      RETURN l_flag;
12557 
12558  EXCEPTION
12559 
12560      WHEN OTHERS THEN
12561 
12562          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12563 	         print_debuginfo(l_module_name, 'Exception occured when '
12564 	             || 'checking if instruction '
12565 	             || p_instr_id
12566 	             || ' needs to be transmitted using external system(s).'
12567 	             );
12568 
12569 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
12570 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
12571 
12572          END IF;
12573          /*
12574           * In case of an exception, play safe and return TRUE.
12575           *
12576           * The user will have to transmit the payment instruction
12577           * outside of IBY using some external application.
12578           */
12579          l_flag := FALSE; /* bug 9663534 If the above SQL doesn't return any row it will throw no_data_found Exception. In that case and in case of any exception l_flag nust be FALSE.*/
12580          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12581 	         print_debuginfo(l_module_name, 'Transmission will have to be '
12582 	             || 'outside the system (irrespective of setting).');
12583 	         print_debuginfo(l_module_name, 'Returning TRUE.');
12584 	         print_debuginfo(l_module_name, 'EXIT');
12585 
12586          END IF;
12587          RETURN l_flag;
12588 
12589  END checkIfInstrXmitOutsideSystem;
12590 
12591 /*--------------------------------------------------------------------
12592  | NAME:
12593  |     checkIfPmtEntityLocked
12594  |
12595  |
12596  | PURPOSE:
12597  |     Checks if a given payment entity is locked (i.e., stamped
12598  |     with a concurrent request id).
12599  |
12600  | PARAMETERS:
12601  |     IN
12602  |
12603  |
12604  |     OUT
12605  |
12606  |
12607  | RETURNS:
12608  |
12609  | NOTES:
12610  |     Internal API, not for public use.
12611  |
12612  *---------------------------------------------------------------------*/
12613  FUNCTION checkIfPmtEntityLocked(
12614      p_object_id         IN NUMBER,
12615      p_object_type       IN VARCHAR2
12616      ) RETURN BOOLEAN
12617  IS
12618  l_module_name CONSTANT VARCHAR2(200)
12619                             := G_PKG_NAME || '.checkIfPmtEntityLocked';
12620 
12621  l_flag             BOOLEAN := FALSE;
12622  l_test             VARCHAR2(200);
12623 
12624  BEGIN
12625 
12626      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12627 	     print_debuginfo(l_module_name, 'ENTER');
12628 
12629      END IF;
12630      IF (p_object_type = 'PAYMENT_REQUEST') THEN
12631 
12632          SELECT
12633              'TRUE'
12634          INTO
12635              l_test
12636          FROM
12637              DUAL
12638          WHERE
12639              EXISTS
12640              (
12641              SELECT
12642                  req.payment_service_request_id
12643              FROM
12644                  IBY_PAY_SERVICE_REQUESTS req
12645              WHERE
12646                  req.payment_service_request_id = p_object_id
12647              AND
12648                  req.request_id IS NOT NULL
12649              )
12650          ;
12651 
12652      ELSIF (p_object_type = 'PAYMENT_INSTRUCTION') THEN
12653 
12654          SELECT
12655              'TRUE'
12656          INTO
12657              l_test
12658          FROM
12659              DUAL
12660          WHERE
12661              EXISTS
12662              (
12663              SELECT
12664                  inst.payment_instruction_id
12665              FROM
12666                  IBY_PAY_INSTRUCTIONS_ALL inst
12667              WHERE
12668                  inst.payment_instruction_id = p_object_id
12669              AND
12670                  inst.request_id IS NOT NULL
12671              )
12672          ;
12673 
12674      ELSIF (p_object_type = 'PAYMENT') THEN
12675 
12676          SELECT
12677              'TRUE'
12678          INTO
12679              l_test
12680          FROM
12681              DUAL
12682          WHERE
12683              EXISTS
12684              (
12685              SELECT
12686                  pmt.payment_id
12687              FROM
12688                  IBY_PAYMENTS_ALL pmt
12689              WHERE
12690                  pmt.payment_id = p_object_id
12691              AND
12692                  pmt.request_id IS NOT NULL
12693              )
12694          ;
12695 
12696      ELSE
12697 
12698 
12699 	         print_debuginfo(l_module_name, 'Unknown payment entity type '
12700 	             || p_object_type
12701 	             || ' provided. Raising exception .. '
12702 	             );
12703 
12704 
12705          APP_EXCEPTION.RAISE_EXCEPTION;
12706 
12707      END IF;
12708 
12709      IF (l_test = 'TRUE') THEN
12710          l_flag := TRUE;
12711          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12712 	         print_debuginfo(l_module_name, 'Returning flag as TRUE.');
12713          END IF;
12714      ELSE
12715          l_flag := FALSE;
12716          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12717 	         print_debuginfo(l_module_name, 'Returning flag as FALSE.');
12718          END IF;
12719      END IF;
12720 
12721      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12722 	     print_debuginfo(l_module_name, 'EXIT');
12723 
12724      END IF;
12725      RETURN l_flag;
12726 
12727  EXCEPTION
12728 
12729      /*
12730       * Fix for bug 5195694:
12731       *
12732       * If no row was found, it means that the
12733       * payment entity is not locked.
12734       */
12735      WHEN NO_DATA_FOUND THEN
12736 
12737          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12738 	         print_debuginfo(l_module_name, 'Pmt entity id '
12739 	             || p_object_id
12740 	             || ' of type '
12741 	             || p_object_type
12742 	             || ' is not locked.'
12743 	             );
12744 
12745          END IF;
12746          l_flag := FALSE;
12747          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12748 	         print_debuginfo(l_module_name, 'Returning FALSE.');
12749 	         print_debuginfo(l_module_name, 'EXIT');
12750 
12751          END IF;
12752          RETURN l_flag;
12753 
12754      WHEN OTHERS THEN
12755 
12756          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12757 	         print_debuginfo(l_module_name, 'Exception occured when '
12758 	             || 'checking if pmt entity id '
12759 	             || p_object_id
12760 	             || ' of type '
12761 	             || p_object_type
12762 	             || ' is locked.'
12763 	             );
12764 
12765 	         print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
12766 	         print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
12767 
12768          END IF;
12769          /*
12770           * In case of an exception, play safe and return TRUE.
12771           *
12772           * The user will assume that the pmt entity is locked.
12773           */
12774          l_flag := TRUE;
12775          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12776 	         print_debuginfo(l_module_name, 'Treating payment entity as locked.');
12777 	         print_debuginfo(l_module_name, 'Returning TRUE.');
12778 	         print_debuginfo(l_module_name, 'EXIT');
12779 
12780          END IF;
12781          RETURN l_flag;
12782 
12783  END checkIfPmtEntityLocked;
12784 
12785  -- The procedure to submit a request set
12786  /*--------------------------------------------------------------------
12787  | NAME:
12788  |     submit_masking_req_set
12789  |
12790  |
12791  | PURPOSE:
12792  |
12793  | PARAMETERS:
12794  |     IN
12795  |
12796  |
12797  |     OUT
12798  |
12799  |
12800  | RETURNS:
12801  |
12802  | NOTES:
12803  |
12804  *---------------------------------------------------------------------*/
12805  PROCEDURE submit_masking_req_set (
12806      x_request_id      OUT NOCOPY NUMBER,
12807      x_return_status   OUT NOCOPY VARCHAR2,
12808      x_msg_count       OUT NOCOPY NUMBER,
12809      x_msg_data        OUT NOCOPY VARCHAR2
12810  ) IS
12811  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME || '.submit_masking_req_set';
12812 
12813  l_api_version    CONSTANT NUMBER := 1.0;
12814  l_msg_count      NUMBER;
12815  l_msg_data       VARCHAR2(2000);
12816 
12817  l_phase          VARCHAR2(200);
12818  l_success BOOLEAN;
12819  submit_failed EXCEPTION;
12820 
12821  BEGIN
12822     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12823 	    print_debuginfo(l_module_name, 'ENTER');
12824 
12825     END IF;
12826     x_return_status := FND_API.G_RET_STS_SUCCESS;
12827     FND_MSG_PUB.initialize;
12828 
12829     /* set the context for the request set IBY_SECURITY_MASKING_REQ_SET */
12830     l_success := fnd_submit.set_request_set('IBY', 'IBY_SECURITY_MASKING_REQ_SET');
12831     l_phase := 'After set request set.';
12832 
12833         if ( l_success ) then
12834 
12835            /* submit program IBY_CREDITCARD_MASKING which is in stage STAGE1 */
12836            l_success := fnd_submit.submit_program('IBY','IBY_CREDITCARD_MASKING', 'STAGE1',
12837                           '','','','','','','','','','','','','','','','','','','','',
12838                           '','','','','','','','','','','','','','','','','','','','',
12839                           '','','','','','','','','','','','','','','','','','','','',
12840                           '','','','','','','','','','','','','','','','','','','','',
12841                           '','','','','','','','','','','','','','','','','','','','');
12842            l_phase := 'After submit program 1.';
12843            if ( not l_success ) then
12844               raise submit_failed;
12845            end if;
12846 
12847            /* submit program IBY_EXT_BANKACCT_MASKING which is in stage STAGE1 */
12848            l_success := fnd_submit.submit_program('IBY','IBY_EXT_BANKACCT_MASKING', 'STAGE1',
12849                           '','','','','','','','','','','','','','','','','','','','',
12850                           '','','','','','','','','','','','','','','','','','','','',
12851                           '','','','','','','','','','','','','','','','','','','','',
12852                           '','','','','','','','','','','','','','','','','','','','',
12853                           '','','','','','','','','','','','','','','','','','','','');
12854            l_phase := 'After submit program 2.';
12855            if ( not l_success ) then
12856               raise submit_failed;
12857            end if;
12858 
12859            /*  Submit the Request set  */
12860            x_request_id := fnd_submit.submit_set(null,FALSE);
12861            l_phase := 'After submit request set.';
12862 
12863        else
12864            l_phase := 'Failed after set request set.';
12865            raise submit_failed;
12866        end if;
12867  EXCEPTION
12868    WHEN submit_failed THEN
12869      x_return_status := FND_API.G_RET_STS_ERROR;
12870      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12871 	     print_debuginfo(l_module_name, 'Exception submit_failed - ' || l_phase);
12872      END IF;
12873      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
12874 
12875    WHEN others THEN
12876      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12877      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12878 	     print_debuginfo(l_module_name, 'Exception others.');
12879 	     print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
12880 	     print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
12881      END IF;
12882      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
12883 
12884  END submit_masking_req_set;
12885 
12886  -- The procedure to submit a request set
12887  /*--------------------------------------------------------------------
12888  | NAME:
12889  |     submit_decrypt_req_set
12890  |
12891  |
12892  | PURPOSE:
12893  |
12894  | PARAMETERS:
12895  |     IN
12896  |
12897  |
12898  |     OUT
12899  |
12900  |
12901  | RETURNS:
12902  |
12903  | NOTES:
12904  |
12905  *---------------------------------------------------------------------*/
12906  PROCEDURE submit_decrypt_req_set (
12907      x_request_id      OUT NOCOPY NUMBER,
12908      x_return_status   OUT NOCOPY VARCHAR2,
12909      x_msg_count       OUT NOCOPY NUMBER,
12910      x_msg_data        OUT NOCOPY VARCHAR2
12911  ) IS
12912  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME || '.submit_decrypt_req_set';
12913 
12914  l_api_version    CONSTANT NUMBER := 1.0;
12915  l_msg_count      NUMBER;
12916  l_msg_data       VARCHAR2(2000);
12917 
12918  l_phase          VARCHAR2(200);
12919  l_success BOOLEAN;
12920  submit_failed EXCEPTION;
12921 
12922  BEGIN
12923     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12924 	    print_debuginfo(l_module_name, 'ENTER');
12925 
12926     END IF;
12927     x_return_status := FND_API.G_RET_STS_SUCCESS;
12928     FND_MSG_PUB.initialize;
12929 
12930     /* set the context for the request set IBY_SECURITY_DECRYPT_REQ_SET */
12931     l_success := fnd_submit.set_request_set('IBY', 'IBY_SECURITY_DECRYPT_REQ_SET');
12932     l_phase := 'After set request set.';
12933 
12934         if ( l_success ) then
12935 
12936            /* submit program IBY_CREDITCARD_DECRYPTION which is in stage STAGE1 */
12937            l_success := fnd_submit.submit_program('IBY','IBY_CREDITCARD_DECRYPTION', 'STAGE1',
12938                           '','','','','','','','','','','','','','','','','','','','',
12939                           '','','','','','','','','','','','','','','','','','','','',
12940                           '','','','','','','','','','','','','','','','','','','','',
12941                           '','','','','','','','','','','','','','','','','','','','',
12942                           '','','','','','','','','','','','','','','','','','','','');
12943            l_phase := 'After submit program 1.';
12944            if ( not l_success ) then
12945               raise submit_failed;
12946            end if;
12947 
12948            /* submit program IBY_EXT_BANKACCT_DECRYPTION which is in stage STAGE1 */
12949            l_success := fnd_submit.submit_program('IBY','IBY_EXT_BANKACCT_DECRYPTION', 'STAGE1',
12950                           '','','','','','','','','','','','','','','','','','','','',
12951                           '','','','','','','','','','','','','','','','','','','','',
12952                           '','','','','','','','','','','','','','','','','','','','',
12953                           '','','','','','','','','','','','','','','','','','','','',
12954                           '','','','','','','','','','','','','','','','','','','','');
12955            l_phase := 'After submit program 2.';
12956            if ( not l_success ) then
12957               raise submit_failed;
12958            end if;
12959 
12960            /* submit program IBY_TRXN_EXTENSION_DECRYPTION which is in stage STAGE1 */
12961            l_success := fnd_submit.submit_program('IBY','IBY_TRXN_EXTENSION_DECRYPTION', 'STAGE1',
12962                           '','','','','','','','','','','','','','','','','','','','',
12963                           '','','','','','','','','','','','','','','','','','','','',
12964                           '','','','','','','','','','','','','','','','','','','','',
12965                           '','','','','','','','','','','','','','','','','','','','',
12966                           '','','','','','','','','','','','','','','','','','','','');
12967            l_phase := 'After submit program 3.';
12968            if ( not l_success ) then
12969               raise submit_failed;
12970            end if;
12971 
12972            /* submit program IBY_TX_CREDITCARD_DECRYPTION which is in stage STAGE1 */
12973            l_success := fnd_submit.submit_program('IBY','IBY_TX_CREDITCARD_DECRYPTION', 'STAGE1',
12974                           '','','','','','','','','','','','','','','','','','','','',
12975                           '','','','','','','','','','','','','','','','','','','','',
12976                           '','','','','','','','','','','','','','','','','','','','',
12977                           '','','','','','','','','','','','','','','','','','','','',
12978                           '','','','','','','','','','','','','','','','','','','','');
12979            l_phase := 'After submit program 4.';
12980            if ( not l_success ) then
12981               raise submit_failed;
12982            end if;
12983 
12984            /*  Submit the Request set  */
12985            x_request_id := fnd_submit.submit_set(null,FALSE);
12986            l_phase := 'After submit request set.';
12987 
12988        else
12989            l_phase := 'Failed after set request set.';
12990            raise submit_failed;
12991        end if;
12992  EXCEPTION
12993    WHEN submit_failed THEN
12994      x_return_status := FND_API.G_RET_STS_ERROR;
12995      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12996 	     print_debuginfo(l_module_name, 'Exception submit_failed - ' || l_phase);
12997      END IF;
12998      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
12999 
13000    WHEN others THEN
13001      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13002      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13003 	     print_debuginfo(l_module_name, 'Exception others.');
13004 	     print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
13005 	     print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
13006      END IF;
13007      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
13008 
13009  END submit_decrypt_req_set;
13010 
13011 /*--------------------------------------------------------------------
13012  | NAME:
13013  |     get_conc_request_status
13014  |
13015  | PURPOSE:
13016  |     Get the concurrent request id status using the FND API.
13017  |
13018  | PARAMETERS:
13019  |     IN     x_request_id
13020  |
13021  | RETURNS: x_request_status (SUCCESS, ERROR, PENDING)
13022  |
13023  | NOTES:
13024  |     Internal API, not for public use.
13025  |     This API is used by the FD Dashboard to determine if a request
13026  |     has terminated with error.
13027  |
13028  *---------------------------------------------------------------------*/
13029  FUNCTION get_conc_request_status (
13030      x_request_id     IN NUMBER)
13031  RETURN VARCHAR2 AS
13032 
13033    v_call_status   BOOLEAN;
13034    v_req_phase_t   VARCHAR2(80);
13035    v_req_status_t  VARCHAR2(80);
13036    v_req_phase_c   VARCHAR2(30);
13037    v_req_status_c  VARCHAR2(30);
13038    v_message       VARCHAR2(240);
13039    v_request_id    iby_payments_all.request_id%TYPE;
13040 
13041    x_return_status VARCHAR2(30) := 'PENDING';
13042 
13043  BEGIN
13044    IF (x_request_id IS NOT NULL) THEN
13045      v_request_id := x_request_id;
13046      -- call FND API to get request status
13047      v_call_status := FND_CONCURRENT.get_request_status(
13048                       v_request_id,
13049                       null, null,
13050                       v_req_phase_t, v_req_status_t,
13051                       v_req_phase_c, v_req_status_c,
13052                       v_message);
13053 
13054      IF (v_call_status) THEN
13055 
13056        IF (v_req_phase_c = 'COMPLETE') THEN
13057          IF (v_req_status_c IN ('ERROR', 'CANCELLED', 'TERMINATED')) THEN
13058            x_return_status := 'ERROR';
13059          END IF;
13060        END IF;
13061      ELSE
13062        x_return_status := 'ERROR';
13063      END IF;
13064    ELSE
13065      x_return_status := 'SUCCESS';
13066    END IF;
13067 
13068    RETURN x_return_status;
13069  END get_conc_request_status;
13070 
13071 /*--------------------------------------------------------------------
13072  | NAME:
13073  |     print_completed_pmts
13074  |
13075  |
13076  | PURPOSE:
13077  |     Prints list of payments marked complete using the provided
13078  |     completion group id.
13079  |
13080  |     This function is used for debugging purposes.
13081  |
13082  | PARAMETERS:
13083  |     IN
13084  |
13085  |
13086  |     OUT
13087  |
13088  |
13089  | RETURNS:
13090  |
13091  | NOTES:
13092  |     Internal API, not for public use.
13093  |
13094  *---------------------------------------------------------------------*/
13095  PROCEDURE print_completed_pmts(
13096              p_completion_id          IN NUMBER
13097              )
13098  IS
13099  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
13100                                          || '.print_completed_pmts';
13101 
13102  l_pmts_list     pmtIDTab;
13103 
13104  /*
13105   * Cursor to pick up all payment ids associated with the
13106   * provided completion id.
13107   */
13108  CURSOR c_pmt_ids (p_complete_id NUMBER)
13109  IS
13110  SELECT
13111      pmt.payment_id
13112  FROM
13113      IBY_PAYMENTS_ALL         pmt
13114  WHERE
13115      pmt.completed_pmts_group_id = p_complete_id
13116  ;
13117 
13118  BEGIN
13119 
13120      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13121 	     print_debuginfo(l_module_name, 'ENTER');
13122 	     print_debuginfo(l_module_name, 'completion id: ' || p_completion_id);
13123 
13124      END IF;
13125      FND_MSG_PUB.initialize;
13126 
13127      OPEN  c_pmt_ids (p_completion_id);
13128      FETCH c_pmt_ids BULK COLLECT INTO l_pmts_list;
13129      CLOSE c_pmt_ids;
13130 
13131      IF (l_pmts_list.COUNT > 0) THEN
13132 
13133          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13134 	         print_debuginfo(l_module_name, 'List of completed payments - ');
13135 
13136          END IF;
13137          FOR i IN l_pmts_list.FIRST .. l_pmts_list.LAST LOOP
13138 
13139              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13140 	             print_debuginfo(l_module_name, 'pmt id: ' || l_pmts_list(i));
13141 
13142              END IF;
13143          END LOOP;
13144 
13145      ELSE
13146 
13147          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13148 	         print_debuginfo(l_module_name, 'No completed payments '
13149 	             || 'were retrieved using completion id '
13150 	             || p_completion_id
13151 	             );
13152 
13153          END IF;
13154      END IF;
13155 
13156      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13157 	     print_debuginfo(l_module_name, 'EXIT');
13158 
13159      END IF;
13160  EXCEPTION
13161      WHEN OTHERS THEN
13162 
13163      /*
13164       * This is a non-fatal exception. We will log the exception
13165       * and return.
13166       */
13167      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13168 	     print_debuginfo(l_module_name, 'Non-Fatal: Exception occured '
13169 	         || 'when trying to print completed payments using '
13170 	         || 'completion id: '
13171 	         || p_completion_id
13172 	         );
13173 
13174 	     print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
13175 	     print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
13176      END IF;
13177      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
13178 
13179  END print_completed_pmts;
13180 
13181 
13182 /*--------------------------------------------------------------------
13183  | NAME:
13184  |     print_completed_docs
13185  |
13186  |
13187  | PURPOSE:
13188  |     Prints list of documents payable marked complete using the
13189  |     provided completion group id.
13190  |
13191  |     This function is used for debugging purposes.
13192  |
13193  | PARAMETERS:
13194  |     IN
13195  |
13196  |
13197  |     OUT
13198  |
13199  |
13200  | RETURNS:
13201  |
13202  | NOTES:
13203  |     Internal API, not for public use.
13204  |
13205  *---------------------------------------------------------------------*/
13206  PROCEDURE print_completed_docs(
13207              p_completion_id          IN NUMBER
13208              )
13209  IS
13210  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
13211                                          || '.print_completed_docs';
13212 
13213  l_docs_list     docPayIDTab;
13214 
13215  /*
13216   * Cursor to pick up all document ids associated with the
13217   * provided completion id.
13218   */
13219  CURSOR c_doc_ids (p_complete_id NUMBER)
13220  IS
13221  SELECT
13222      doc.document_payable_id
13223  FROM
13224      IBY_DOCS_PAYABLE_ALL        doc
13225  WHERE
13226      doc.completed_pmts_group_id = p_complete_id
13227  ;
13228 
13229  BEGIN
13230 
13231      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13232 	     print_debuginfo(l_module_name, 'ENTER');
13233 	     print_debuginfo(l_module_name, 'completion id: ' || p_completion_id);
13234 
13235      END IF;
13236      FND_MSG_PUB.initialize;
13237 
13238      OPEN  c_doc_ids (p_completion_id);
13239      FETCH c_doc_ids BULK COLLECT INTO l_docs_list;
13240      CLOSE c_doc_ids;
13241 
13242      IF (l_docs_list.COUNT > 0) THEN
13243 
13244          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13245 	         print_debuginfo(l_module_name, 'List of completed documents - ');
13246 
13247          END IF;
13248          FOR i IN l_docs_list.FIRST .. l_docs_list.LAST LOOP
13249 
13250              IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13251 	             print_debuginfo(l_module_name, 'doc id: ' || l_docs_list(i));
13252 
13253              END IF;
13254          END LOOP;
13255 
13256      ELSE
13257 
13258          IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13259 	         print_debuginfo(l_module_name, 'No completed documents '
13260 	             || 'were retrieved using completion id '
13261 	             || p_completion_id
13262 	             );
13263 
13264          END IF;
13265      END IF;
13266 
13267      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13268 	     print_debuginfo(l_module_name, 'EXIT');
13269 
13270      END IF;
13271  EXCEPTION
13272      WHEN OTHERS THEN
13273 
13274      /*
13275       * This is a non-fatal exception. We will log the exception
13276       * and return.
13277       */
13278      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13279 	     print_debuginfo(l_module_name, 'Non-Fatal: Exception occured '
13280 	         || 'when trying to print completed documents using '
13281 	         || 'completion id: '
13282 	         || p_completion_id
13283 	         );
13284 
13285 	     print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
13286 	     print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
13287      END IF;
13288      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
13289 
13290  END print_completed_docs;
13291 
13292 
13293 /*--------------------------------------------------------------------
13294  | NAME:
13295  |     is_security_function_valid
13296  |
13297  | PURPOSE:
13298  |     This API will return Y or N is the security function passed
13299  |     is assigned to the user.  This function wraps the
13300  |     FND_FUNCTION.TEST API.
13301  |
13302  | PARAMETERS:
13303  |     IN     x_security_function_name
13304  |
13305  | RETURNS: x_function_valid (Y, N)
13306  |
13307  | NOTES:
13308  |     Internal API, not for public use.
13309  |     This API is used by the taks list page to determine if a user is
13310  |     available to do the setup for the Shared, FD or FC tasks.
13311  |
13312  *---------------------------------------------------------------------*/
13313  FUNCTION is_security_function_valid (
13314      x_security_function_name     IN VARCHAR2)
13315  RETURN VARCHAR2 AS
13316 
13317    x_return_status VARCHAR2(30) := 'N';
13318 
13319  BEGIN
13320 
13321    IF fnd_function.test(x_security_function_name) THEN
13322      x_return_status := 'Y';
13323    END IF;
13324 
13325    RETURN x_return_status;
13326  END is_security_function_valid;
13327 
13328 
13329 
13330   /*--------------------------------------------------------------------
13331  | NAME:
13332  |    Rejected_user_acc
13333  |
13334  |
13335  | PURPOSE:
13336  |     Checks whether the user has access to all the org. or payment function
13337  |      of rejected or removed entities ( documents or payments) for a
13338  |      given payment service request.
13339  |
13340  | PARAMETERS:
13341  |     IN
13342  |
13343  |
13344  |     OUT
13345  |
13346  |
13347  | RETURNS:
13348  |
13349  | NOTES:
13350  |     Internal API, not for public use.
13351  |
13352  *---------------------------------------------------------------------*/
13353  PROCEDURE Rejected_user_acc(
13354              p_pay_service_request_id  IN NUMBER,
13355              x_inaccessible_entities OUT NOCOPY VARCHAR2,
13356              x_return_status     IN OUT NOCOPY VARCHAR2,
13357              x_return_message    IN OUT NOCOPY VARCHAR2)
13358   IS
13359  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
13360                                          || '.Rejected_user_acc';
13361 
13362  l_doc_function_count NUMBER:=0;
13363  l_doc_org_count NUMBER:=0;
13364  l_pmt_org_count NUMBER:=0;
13365 BEGIN
13366 
13367 
13368 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13369 	print_debuginfo(l_module_name, 'ENTER');
13370 END IF;
13371 --Initializing the out parameter
13372  x_inaccessible_entities :='N';
13373 
13374 FND_MSG_PUB.initialize;
13375 
13376 /*Initializing security through CE for populating the
13377    ce related global tables*/
13378 CEP_STANDARD.init_security();
13379 
13380 populatePaymentFunctions(x_return_status, x_return_message);
13381 
13382  /* Initialize return status */
13383 x_return_status := FND_API.G_RET_STS_SUCCESS;
13384 
13385 select count(1)
13386 into l_doc_function_count
13387 FROM  IBY_DOCS_PAYABLE_ALL docs
13388 where  docs.document_status IN
13389         ( 'REJECTED',
13390           'FAILED_BY_RELATED_DOCUMENT',
13391           'FAILED_BY_REJECTION_LEVEL',
13392           'FAILED_BY_CALLING_APP',
13393           'REMOVED',
13394           'REMOVED_PAYMENT_REMOVED',
13395           'REMOVED_REQUEST_TERMINATED',
13396           'REMOVED_INSTRUCTION_TERMINATED',
13397           'REMOVED_PAYMENT_STOPPED',
13398           'REMOVED_PAYMENT_VOIDED')
13399 AND docs.payment_service_request_id = p_pay_service_request_id
13400 and docs.payment_function not in
13401        (select payment_function
13402         from IBY_USER_PAY_FUNS_GT
13403         );
13404 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13405 	print_debuginfo(l_module_name, 'Number of Missing Payment Function for rejected documents:' ||l_doc_function_count);
13406 
13407 END IF;
13408 select count(1)
13409 into l_doc_org_count
13410 FROM  IBY_DOCS_PAYABLE_ALL docs
13411 where  docs.document_status IN
13412         ( 'REJECTED',
13413           'FAILED_BY_RELATED_DOCUMENT',
13414           'FAILED_BY_REJECTION_LEVEL',
13415           'FAILED_BY_CALLING_APP',
13416           'REMOVED',
13417           'REMOVED_PAYMENT_REMOVED',
13418           'REMOVED_REQUEST_TERMINATED',
13419           'REMOVED_INSTRUCTION_TERMINATED',
13420           'REMOVED_PAYMENT_STOPPED',
13421           'REMOVED_PAYMENT_VOIDED')
13422 AND docs.payment_service_request_id = p_pay_service_request_id
13423 and docs.org_id not in
13424        (select  organization_id
13425         from ce_security_profiles_v cep
13426         where cep.organization_type=docs.org_type
13427         );
13428 
13429 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13430 	print_debuginfo(l_module_name, 'Number of Missing Orgs for rejected documents:' ||l_doc_org_count);
13431 
13432 
13433 END IF;
13434 select count(1)
13435 into  l_pmt_org_count
13436 FROM  iby_payments_all payments
13437 where  payments.payment_status IN
13438                         ('FAILED_BY_REJECTION_LEVEL',
13439                         'FAILED_BY_CALLING_APP',
13440                         'REMOVED',
13441                         'REMOVED_REQUEST_TERMINATED',
13442                         'REMOVED_INSTRUCTION_TERMINATED',
13443                         'REMOVED_DOCUMENT_SPOILED',
13444                         'REMOVED_PAYMENT_STOPPED',
13445                         'VOID',
13446                         'REJECTED',
13447                         'FAILED_VALIDATION')
13448 AND payments.payment_service_request_id = p_pay_service_request_id
13449 and payments.org_id not in
13450        (select  organization_id
13451         from ce_security_profiles_v cep
13452         where cep.organization_type=payments.org_type
13453         );
13454     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13455 	    print_debuginfo(l_module_name, 'Number of Missing Org for rejected payments:' ||l_pmt_org_count);
13456 
13457     END IF;
13458 -- check the user access
13459 if(l_doc_function_count<>0 OR l_doc_org_count <>0 OR l_pmt_org_count <>0) THEN
13460 
13461    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13462 	   print_debuginfo(l_module_name, 'User doesnt have full access to the Rejected documents or Rejected Payments for a given payment process Request');
13463 
13464    END IF;
13465    x_inaccessible_entities:='Y';
13466    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13467 	   print_debuginfo(l_module_name, 'Returning Y for x_inaccessible_entities');
13468 
13469    END IF;
13470 end if;
13471    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13472 	   print_debuginfo(l_module_name, 'EXIT');
13473 
13474    END IF;
13475 EXCEPTION
13476 WHEN OTHERS THEN
13477    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13478 	   print_debuginfo(l_module_name, 'SQL err msg: '
13479 	       || SQLERRM);
13480 	     print_debuginfo(l_module_name, 'Returning failure response .. '
13481 	       );
13482      END IF;
13483      FND_MSG_PUB.add_exc_msg(G_PKG_NAME, l_module_name, null);
13484      /*
13485       * The error message would have already been set.
13486       * Just set the error status here.
13487       */
13488      x_return_status := FND_API.G_RET_STS_ERROR;
13489 
13490      IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13491 	     print_debuginfo(l_module_name, 'EXIT');
13492 
13493 
13494      END IF;
13495  END Rejected_user_acc;
13496 
13497 /*--------------------------------------------------------------------
13498  | NAME:
13499  |     get_vendor_id
13500  |
13501  |
13502  | PURPOSE:
13503  |     Get the vendor_id from AP tables based on the party_id
13504  |
13505  | PARAMETERS:
13506  |     IN
13507  |
13508  |
13509  |     OUT
13510  |
13511  |
13512  | RETURNS:
13513  |
13514  | NOTES:
13515  |
13516  *---------------------------------------------------------------------*/
13517  PROCEDURE get_vendor_id(
13518              p_party_id  IN NUMBER,
13519              x_vendor_id OUT NOCOPY NUMBER)
13520  IS
13521  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
13522                                          || '.get_vendor_id';
13523  BEGIN
13524  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13525  print_debuginfo(l_module_name, 'ENTER');
13526  END IF;
13527 --Initializing the out parameter
13528  x_vendor_id :=0;
13529  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13530  print_debuginfo(l_module_name, 'Calling AP tables to get the vendor_id');
13531  END IF;
13532  -- Calling AP tables to get the vendor_id
13533      BEGIN
13534        SELECT vendor_id into x_vendor_id
13535        from AP_SUPPLIERS
13536        where party_id = p_party_id;
13537      EXCEPTION
13538       WHEN NO_DATA_FOUND THEN
13539       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13540       print_debuginfo(l_module_name, 'Query returned no rows for vendor_id');
13541       END IF;
13542       WHEN OTHERS THEN
13543       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13544       print_debuginfo(l_module_name, 'Exception occured in the query');
13545       END IF;
13546      END;
13547   print_debuginfo(l_module_name, 'EXIT');
13548 
13549  END get_vendor_id;
13550 
13551  /*--------------------------------------------------------------------
13552  | NAME:
13553  |     get_default_bank_acct
13554  |
13555  |
13556  | PURPOSE:
13557  |     Get the default_bank_acct from IBY API
13558  |
13559  | PARAMETERS:
13560  |     IN
13561  |
13562  |
13563  |     OUT
13564  |
13565  |
13566  | RETURNS:
13567  |
13568  | NOTES:
13569  |
13570  *---------------------------------------------------------------------*/
13571  PROCEDURE get_default_bank_acct(
13572              currency_code   iby_ext_bank_accounts.currency_code%TYPE,
13573 	     Payee_Party_Id         IBY_EXTERNAL_PAYEES_ALL.payee_party_id%TYPE,
13574 	     Payee_Party_Site_Id    IBY_EXTERNAL_PAYEES_ALL.party_site_id%TYPE,
13575 	     Supplier_Site_Id       IBY_EXTERNAL_PAYEES_ALL.supplier_site_id%TYPE,
13576 	     Payer_Org_Id           IBY_EXTERNAL_PAYEES_ALL.org_id%TYPE,
13577 	     Payer_Org_Type         IBY_EXTERNAL_PAYEES_ALL.org_type%TYPE,
13578 	     Payment_Function       IBY_EXTERNAL_PAYEES_ALL.payment_function%TYPE,
13579 	     old_ext_bank_acct_id       IBY_EXT_BANK_ACCOUNTS.ext_bank_account_id%TYPE,
13580 	     x_default_bank_acct_id OUT NOCOPY iby_ext_bank_accounts.ext_bank_account_id%TYPE)
13581  IS
13582  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
13583                                          || '.get_default_bank_acct';
13584  l_trxn_attributes_rec IBY_DISBURSEMENT_COMP_PUB.Trxn_Attributes_Rec_Type;
13585  l_return_status             VARCHAR2(1);
13586  l_msg_count                 NUMBER(15);
13587  l_msg_data                  VARCHAR2(2000);
13588  l_payee_bankaccounts_tbl    IBY_DISBURSEMENT_COMP_PUB.Payee_BankAccount_Tab_Type;
13589 
13590  BEGIN
13591  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13592  print_debuginfo(l_module_name, 'ENTER');
13593  END IF;
13594 
13595 IF ((g_ext_acct_id_tbl.EXISTS(Supplier_Site_Id||currency_code))) THEN
13596 
13597 	 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13598 	 print_debuginfo(l_module_name, 'External Bank Account Id found in cache for Supplier Site id:'||
13599 	 Supplier_Site_Id||' and Currency code:'||currency_code);
13600 	 END IF;
13601 	 	x_default_bank_acct_id := g_ext_acct_id_tbl(Supplier_Site_Id||currency_code).ext_bank_account_id;
13602 
13603 ELSE
13604 
13605  --l_trxn_attributes_rec.Application_Id := Application_Id;
13606  l_trxn_attributes_rec.Payer_Legal_Entity_Id := null;
13607  l_trxn_attributes_rec.Payer_Org_Id := Payer_Org_Id;
13608  l_trxn_attributes_rec.Payer_Org_Type := Payer_Org_Type;
13609  l_trxn_attributes_rec.Payee_Party_Id := Payee_Party_Id;
13610  l_trxn_attributes_rec.Payee_Party_Site_Id := Payee_Party_Site_Id;
13611  l_trxn_attributes_rec.Supplier_Site_Id := Supplier_Site_Id;
13612  l_trxn_attributes_rec.Pay_Proc_Trxn_Type_Code := null;
13613  l_trxn_attributes_rec.Payment_Currency := currency_code;
13614  l_trxn_attributes_rec.Payment_Amount := null;
13615  l_trxn_attributes_rec.Payment_Function := Payment_Function;
13616  IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13617  print_debuginfo(l_module_name, 'Payee party Id:: '||Payee_Party_Id);
13618  print_debuginfo(l_module_name, 'Payee party site Id:: '||Payee_Party_Site_Id);
13619  print_debuginfo(l_module_name, 'Supplier site Id:: '||Supplier_Site_Id);
13620  print_debuginfo(l_module_name, 'Org Id:: '||Payer_Org_Id);
13621  print_debuginfo(l_module_name, 'Org Type:: '||Payer_Org_Type);
13622  print_debuginfo(l_module_name, 'Payment Function:: '||Payment_Function);
13623  print_debuginfo(l_module_name, 'currency code:: '||currency_code);
13624  print_debuginfo(l_module_name, 'Calling IBY_DISBURSEMENT_COMP_PUB.Get_Applicable_Payee_Acc_list to get the default bank account');
13625  END IF;
13626 
13627  x_default_bank_acct_id := null;
13628 IBY_DISBURSEMENT_COMP_PUB.Get_Applicable_Payee_Acc_list
13629       (p_api_version			=>  1.0,
13630        p_init_msg_list			=>  FND_API.G_TRUE,
13631        p_trxn_attributes_rec		=>  l_trxn_attributes_rec,
13632        x_return_status			=>  l_return_status,
13633        x_msg_count			=>  l_msg_count,
13634        x_msg_data			=>  l_msg_data,
13635        x_payee_bankaccounts_tbl         =>  l_payee_bankaccounts_tbl);
13636 
13637 	IF l_payee_bankaccounts_tbl.COUNT <> 0 THEN
13638 
13639 	    for i in l_payee_bankaccounts_tbl.first .. l_payee_bankaccounts_tbl.last loop
13640 		IF l_payee_bankaccounts_tbl(i).Payee_BankAccount_Id = old_ext_bank_acct_id THEN
13641 			IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13642 			print_debuginfo(l_module_name, 'Old bank account is still valid');
13643 			print_debuginfo(l_module_name, 'External Bank account Id:: '||l_payee_bankaccounts_tbl(i).Payee_BankAccount_Id);
13644 			print_debuginfo(l_module_name, 'External Bank Account Num:: '||l_payee_bankaccounts_tbl(i).Payee_BankAccount_Num);
13645 			print_debuginfo(l_module_name, 'External bank Account Name:: '||l_payee_bankaccounts_tbl(i).Payee_BankAccount_Name);
13646 			END IF;
13647 			x_default_bank_acct_id := l_payee_bankaccounts_tbl(i).Payee_BankAccount_Id;
13648 		END IF;
13649 	    end loop;
13650 
13651 	    IF x_default_bank_acct_id is null THEN
13652 		x_default_bank_acct_id := l_payee_bankaccounts_tbl(l_payee_bankaccounts_tbl.first).Payee_BankAccount_Id;
13653 		IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13654 			print_debuginfo(l_module_name, 'Old bank account is not valid');
13655 			print_debuginfo(l_module_name, 'External Bank account Id:: '||l_payee_bankaccounts_tbl(l_payee_bankaccounts_tbl.first).Payee_BankAccount_Id);
13656 			print_debuginfo(l_module_name, 'External Bank Account Num:: '||l_payee_bankaccounts_tbl(l_payee_bankaccounts_tbl.first).Payee_BankAccount_Num);
13657 			print_debuginfo(l_module_name, 'External bank Account Name:: '||l_payee_bankaccounts_tbl(l_payee_bankaccounts_tbl.first).Payee_BankAccount_Name);
13658 		END IF;
13659 	    END IF;
13660 
13661 	g_ext_acct_id_tbl(Supplier_Site_Id||currency_code).ext_bank_account_id := x_default_bank_acct_id;
13662 	ELSE
13663 	     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13664 	         print_debuginfo(l_module_name, 'No Valid Bank account');
13665 	     END IF;
13666 	END IF;
13667 
13668 END IF;
13669 
13670 
13671 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13672 print_debuginfo(l_module_name, 'Returned External Bank account Id:: '||x_default_bank_acct_id);
13673 print_debuginfo(l_module_name, 'EXIT');
13674 END IF;
13675 END get_default_bank_acct;
13676 
13677 Procedure initialize IS
13678  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
13679                                          || '.initialize';
13680 BEGIN
13681 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13682 print_debuginfo(l_module_name, 'Enter');
13683 END IF;
13684      g_ext_acct_id_tbl.DELETE;
13685 IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13686 print_debuginfo(l_module_name, 'EXIT');
13687 END IF;
13688 END initialize;
13689 
13690 
13691 FUNCTION Get_Pmt_Completion_Point (
13692      p_instruction_id           IN   NUMBER
13693 ) RETURN VARCHAR2 IS
13694 
13695   l_completion_point VARCHAR2(30);
13696 
13697     CURSOR l_ins_csr (p_payment_instruction_id IN NUMBER) IS
13698     SELECT pp.MARK_COMPLETE_EVENT
13699       FROM iby_pay_instructions_all ins,
13700            iby_payment_profiles pp
13701      WHERE ins.payment_instruction_id = p_payment_instruction_id
13702        AND ins.payment_profile_id = pp.payment_profile_id;
13703 
13704 BEGIN
13705 
13706     OPEN l_ins_csr(p_instruction_id);
13707     FETCH l_ins_csr INTO l_completion_point;
13708 
13709   RETURN l_completion_point;
13710 
13711 END Get_Pmt_Completion_Point;
13712 
13713 
13714 END IBY_DISBURSE_UI_API_PUB_PKG;
13715