DBA Data[Home] [Help]

PACKAGE BODY: APPS.IBY_PAYGROUP_PUB

Source


1 PACKAGE BODY IBY_PAYGROUP_PUB AS
2 /*$Header: ibypaygb.pls 120.108.12010000.13 2008/12/02 20:56:39 pschalla ship $*/
3 
4  --
5  -- Declare global variables
6  --
7  G_PKG_NAME CONSTANT VARCHAR2(30) := 'IBY_PAYGROUP_PUB';
8 
9  --
10  -- List of document statuses that are used / set in this
11  -- module (payment creation flow).
12  --
13  DOC_STATUS_VALIDATED    CONSTANT VARCHAR2(100) := 'VALIDATED';
14  DOC_STATUS_PAY_CREATED  CONSTANT VARCHAR2(100) := 'PAYMENT_CREATED';
15  DOC_STATUS_CA_FAILED    CONSTANT VARCHAR2(100) := 'FAILED_BY_CALLING_APP';
16  DOC_STATUS_RELN_FAIL    CONSTANT VARCHAR2(100) := 'FAILED_BY_RELATED_DOCUMENT';
17  DOC_STATUS_PAY_VAL_FAIL CONSTANT VARCHAR2(100) := 'PAYMENT_FAILED_VALIDATION';
18  DOC_STATUS_FAIL_BY_REJLVL CONSTANT VARCHAR2(100)
19                                                 := 'FAILED_BY_REJECTION_LEVEL';
20 
21  --
22  -- List of payment statuses that are used / set in this
23  -- module (payment creation flow).
24  --
25  PAY_STATUS_REJECTED       CONSTANT VARCHAR2(100) := 'REJECTED';
26  PAY_STATUS_FAIL_VALID     CONSTANT VARCHAR2(100) := 'FAILED_VALIDATION';
27  PAY_STATUS_CREATED        CONSTANT VARCHAR2(100) := 'CREATED';
28  PAY_STATUS_CA_FAILED      CONSTANT VARCHAR2(100) := 'FAILED_BY_CALLING_APP';
29  PAY_STATUS_FAIL_BY_REJLVL CONSTANT VARCHAR2(100)
30                                                 := 'FAILED_BY_REJECTION_LEVEL';
31  PAY_STATUS_MODIFIED       CONSTANT VARCHAR2(100) := 'MODIFIED';
32 
33  --
34  -- List of payment request statuses that are set in this
35  -- module (payment creation flow).
36  --
37  REQ_STATUS_PAY_CRTD      CONSTANT VARCHAR2(100) := 'PAYMENTS_CREATED';
38  REQ_STATUS_FAIL_PAY_CR   CONSTANT VARCHAR2(100) := 'FAILED_PAYMENT_VALIDATION';
39  REQ_STATUS_USER_REVW     CONSTANT VARCHAR2(100) := 'PENDING_REVIEW';
40  REQ_STATUS_USER_REVW_ERR CONSTANT VARCHAR2(100) :=
41                                        'PENDING_REVIEW_PMT_VAL_ERRORS';
42  REQ_STATUS_RETRY_PMT_CREAT CONSTANT VARCHAR2(100)
43                                                  := 'RETRY_PAYMENT_CREATION';
44 
45  --
46  -- List of rejection level system options  that are possible for
47  -- this module (payment creation flow).
48  --
49  REJ_LVL_REQUEST  CONSTANT VARCHAR2(100) := 'REQUEST';
50  REJ_LVL_PAYMENT  CONSTANT VARCHAR2(100) := 'PAYMENT';
51  REJ_LVL_NONE     CONSTANT VARCHAR2(100) := 'NONE';
52 
53  -- Transaction types (for inserting into IBY_TRANSACTION_ERRORS table)
54  TRXN_TYPE_DOC   CONSTANT VARCHAR2(100) := 'DOCUMENT_PAYABLE';
55  TRXN_TYPE_PMT   CONSTANT VARCHAR2(100) := 'PAYMENT';
56 
57  --
58  -- Forward declarations
59  --
60  PROCEDURE print_debuginfo(
61               p_module     IN VARCHAR2,
62               p_debug_text IN VARCHAR2,
63               p_debug_level IN VARCHAR2  DEFAULT FND_LOG.LEVEL_STATEMENT
64               );
65 
66  -- Get Employee Full Name from HR tables
67 
68  FUNCTION Get_Employee_Full_Name(p_person_id IN NUMBER,p_party_id IN NUMBER,p_party_name IN VARCHAR2)
69  RETURN VARCHAR2
70  IS
71      l_module_name           CONSTANT VARCHAR2(200) := G_PKG_NAME || '.Get_Employee_Full_Name';
72      p_emp_full_name VARCHAR2(500);
73  BEGIN
74      print_debuginfo(l_module_name, 'Fetching employee full name from HR for party_id ' || p_party_id || ' person_id ' || p_person_id);
75 
76      SELECT per.full_name
77      INTO p_emp_full_name
78      FROM per_all_people_f per
79      WHERE per.person_id = p_person_id
80       AND per.party_id = p_party_id
81       AND sysdate BETWEEN per.effective_start_date
82       AND per.effective_end_date;
83 
84      RETURN p_emp_full_name;
85 
86  EXCEPTION
87   WHEN OTHERS THEN
88      print_debuginfo(l_module_name, 'EXCEPTION : Unable to fetch employee full name from HR');
89      RETURN p_party_name;
90 
91  END Get_Employee_Full_Name;
92 
93 /*--------------------------------------------------------------------
94  | NAME:
95  |     createPayments
96  |
97  | PURPOSE:
98  |     Entry point for payment creation (F6) flow. All payment grouping
99  |     rules are handled within this method.
100  |
101  |
102  | PARAMETERS:
103  |     IN
104  |
105  |
106  |     OUT
107  |
108  |
109  | RETURNS:
110  |
111  | NOTES:
112  |
113  *---------------------------------------------------------------------*/
114  PROCEDURE createPayments(
115      p_payment_request_id         IN IBY_PAY_SERVICE_REQUESTS.
116                                          payment_service_request_id%TYPE,
117      p_pmt_rejection_level        IN IBY_INTERNAL_PAYERS_ALL.
118                                          payment_rejection_level_code%TYPE,
119      p_review_proposed_pmts_flag  IN IBY_INTERNAL_PAYERS_ALL.
120                                          require_prop_pmts_review_flag%TYPE,
121      x_return_status      IN OUT NOCOPY VARCHAR2)
122  IS
123 
124  l_module_name           CONSTANT VARCHAR2(200) := G_PKG_NAME ||
125                                                        '.createPayments';
126 
127  l_ca_payreq_cd            VARCHAR2(255) := '';
128  l_ca_id                   NUMBER(15)    := 0;
129  l_all_pmts_success_flag   BOOLEAN       := FALSE;
130  l_all_pmts_failed_flag    BOOLEAN       := FALSE;
131 
132  /* rejection level system options */
133  l_rejection_level         VARCHAR2(200);
134  l_review_pmts_flag        VARCHAR2(1)   := 'N';
135 
136  l_paymentTab        IBY_PAYGROUP_PUB.paymentTabType;
137  l_docsInPmtTab      IBY_PAYGROUP_PUB.docsInPaymentTabType;
138 
139  /* these two are passed to calling app via hook */
140  l_hookPaymentTab    IBY_PAYGROUP_PUB.hookPaymentTabType;
141  l_hookDocsInPmtTab  IBY_PAYGROUP_PUB.hookDocsInPaymentTabType;
142 
143  /* payment request imposed limits on payments */
144  l_payReqCriteria    IBY_PAYGROUP_PUB.payReqImposedCriteria;
145 
146  /* central bank reporting requirements imposed on each payment */
147  --l_cbrTab            IBY_PAYGROUP_PUB.centralBankReportTabType;
148 
149  /* holds the error messages against failed documents */
150  l_docErrorTab       IBY_VALIDATIONSETS_PUB.docErrorTabType;
151  l_docTokenTab       IBY_VALIDATIONSETS_PUB.trxnErrTokenTabType;
152 
153  l_payreq_status     VARCHAR2(500);
154 
155  BEGIN
156 
157      print_debuginfo(l_module_name, 'ENTER');
158 
159      print_debuginfo(l_module_name, 'Payment request id : '
160          || p_payment_request_id);
161 
162      l_payreq_status := get_payreq_status(p_payment_request_id);
163 
164      print_debuginfo(l_module_name, 'Payment request status: '
165          || l_payreq_status);
166 
167      /*
168       * Do not perform grouping if the payments are being
169       * re-built.
170       *
171       * Fix for bug 4946181.
172       */
173 
174      /*
175       * Fix for bug 5331527:
176       *
177       * PPRs in REQ_STATUS_PEN_REV_PMT_VAL need to be
178       * rebuilt.
179       *
180       * Therefore, call rebuildPayments(..) for such PPRs.
181       */
182      IF (l_payreq_status <> REQ_STATUS_RETRY_PMT_CREAT AND
183          l_payreq_status <> REQ_STATUS_USER_REVW_ERR)  THEN
184 
185          /*
186           * Group the documents of the payment request into payments.
187           */
188          performDocumentGrouping(p_payment_request_id, l_paymentTab,
189              l_docsInPmtTab, l_ca_id, l_ca_payreq_cd, l_payReqCriteria
190 --          ,l_cbrTab
191             );
192 
193          print_debuginfo(l_module_name, 'After grouping '
194              || l_paymentTab.COUNT   || ' payment(s) were created from '
195              || l_docsInPmtTab.COUNT || ' document(s) for payment request '
196              || p_payment_request_id || '.');
197 
198      ELSE
199 
200          print_debuginfo(l_module_name, 'Not performing grouping '
201              || 'for payment request '
202              || p_payment_request_id
203              || ' as payments are being re-built ..');
204 
205          rebuildPayments(p_payment_request_id, l_paymentTab, l_docsInPmtTab,
206              l_ca_id, l_ca_payreq_cd, l_payReqCriteria
207 --         , l_cbrTab
208            );
209 
210      END IF;
211 
212      /*
213       * Fix for bug 5357948:
214       *
215       * Search for common attributes of the documents
216       * of each payment, and set the corresponding
217       * attribute on the parent payment itself if
218       * such a common attribute is found.
219       */
220      sweepCommonPmtAttributes(l_paymentTab, l_docsInPmtTab);
221 
222      /*
223       * Set the default attributes for the created payments.
224       */
225      initializePmts(l_paymentTab);
226 
227      /*
228       * Handle credit memos
229       */
230 
231      /*
232       * Update: Credit memo handling is now performed at the calling
233       * application itself.
234       * - rameshsh, 3/29/2005
235       */
236      --printDocsInPmtTab(l_docsInPmtTab);
237      --performCreditMemoHandling(l_paymentTab, l_docsInPmtTab);
238      --printDocsInPmtTab(l_docsInPmtTab);
239 
240      /*
241       * Maturity date calculation
242       *
243       * For bills payable
244       *   a.k.a promissory notes
245       *   a.k.a future dated payments
246       */
247      performMaturityDateCalculation(l_paymentTab, l_docsInPmtTab);
248 
249      /*
250       * Fix for bug 5935493:
251       *
252       * Payment validations are dependent upon finding the
253       * payments in the IBY_PAYMENTS_ALL table. Therefore, insert
254       * the payments from the PLSQL table into the
255       * IBY_PAYMENTS_ALL table.
256       *
257       * Central bank reporting could be implemented via a hook
258       * that is expecting payments to be populated in
259       * IBY_PAYMENTS_ALL table. Therefore, payments need to
260       * inserted before performing central bank reporting as well.
261       */
262      IF (l_payreq_status <> REQ_STATUS_RETRY_PMT_CREAT AND
263          l_payreq_status <> REQ_STATUS_USER_REVW_ERR)  THEN
264 
265          insertPayments(l_paymentTab);
266 
267          /*
268           * Fix for bug 5972585:
269           *
270           * Update the documents payable that are part of the created
271           * payments with payment ids.
272           *
273           * This is normally done in performDBUpdates(..) call at
274           * the end of this method. However, some customers might
275           * want to do custom validations on documents that are
276           * part of the created payments. Therefore, update the
277           * documents with payment ids before the payment validation
278           * call.
279           *
280           * We do this even before central bank reporting because
281           * again the customer could potentially want to retrieve
282           * documents that are part of the created payments in the
283           * central bank reporting hook.
284           */
285          updateDocsWithPaymentID(l_docsInPmtTab);
286 
287      END IF;
288 
289      /*
290       * Fix for bug 5972585:
291       *
292       * Update the payments table with audit data before
293       * calling central bank reporting or payment validations.
294       *
295       * This is because the customer could have implemented
296       * custom central bank reporting or custom payment
297       * validations that could depend upon the denormalized
298       * payment attributes being present in the the payments
299       * table.
300       */
301 
302      /*
303       * Along with the payment we insert the audit data for the
304       * payment as well. These are denormalized data from payment
305       * related tables like payee, payer, payee bank, payer bank
306       * etc.
307       *
308       * This information is also used by the extract and format
309       * logic.
310       */
311      auditPaymentData(l_paymentTab);
312 
313      /*
314       * Fix for bug 5337671:
315       *
316       * Perform central bank reporting prior to calling
317       * payment validations.
318       *
319       * This is because some validations are dependent upon
320       * whether the payment has the 'declare payment flag'
321       * set to 'Y'.
322       */
323 
324      /*
325       * Perform declarations / central bank reporting.
326       */
327      performCentralBankReporting(
328          l_paymentTab,
329          l_docsInPmtTab
330 --      ,l_cbrTab
331          );
332 
333      /*
334       * Fix for bug 5414562:
335       *
336       * When payments are being rebuilt, attributes of the
337       * payment could have been modified by the rebuild method.
338       *
339       * These need to be reflected in the database because
340       * the validation set reads the payment data from the
341       * database.
342       *
343       * Therefore, update the payments before calling
344       * payment validations.
345       *
346       * Fix for bug 5935493:
347       *
348       * After central bank reporting is completed,
349       * the declare payments flag could be set on some/all
350       * payments by the hook. Therefore, we need to
351       * update the payments in IBY_PAYMENTS_ALL table
352       * so that this flag is accessible for validation.
353       */
354      updatePayments(l_paymentTab);
355 
356      /*
357       * Payment validations
358       */
359      applyPaymentValidationSets(l_paymentTab, l_docsInPmtTab,
360          l_docErrorTab, l_docTokenTab);
361 
362      /*
363       * Payment request imposed validations
364       */
365      performPayReqBasedValidations(l_payReqCriteria, l_paymentTab,
366          l_docsInPmtTab, l_docErrorTab, l_docTokenTab);
367 
368 
369      /*
370       * Fix for bug 5608359:
371       *
372       * As per performance team, running a 10,000 trxn PPR
373       * causes the Build Program to run in an infinite loop
374       * in the performPmtGrpNumberValidation(..).
375       *
376       * The validation performed in performPmtGrpNumberValidation(..)
377       * is optional. In case, AP's pre-grouped payment count
378       * does not match the payment count generated by IBY, ideally
379       * we should flag the PPR with an error.
380       *
381       * However, this can be handled as an exception after the
382       * fact also. The PPR could be terminated and the issue
383       * diagnozed to see who is at fault.
384       *
385       * For this reason, the call to this validation is commented out
386       * at present. We will come back to this issue and re-investigate
387       * at a later date to see if an infinite loop is really occuring.
388       *
389       * - rameshsh 18-OCT-2006.
390       *
391       */
392 
393      /*
394       * Payment grouping number validation.
395       */
396      --performPmtGrpNumberValidation(l_paymentTab, l_docsInPmtTab,
397      --    l_docErrorTab, l_docTokenTab);
398 
399      /*
400       * Payment validation might have failed some payments (and by
401       * cascade, the documents that were part of these payments).
402       *
403       * Make sure to fail related documents whenever documents are
404       * failed.
405       */
406      adjustSisterDocsAndPmts(l_paymentTab, l_docsInPmtTab, l_docErrorTab,
407          l_docTokenTab);
408 
409      /*
410       * Call post-payment creation hook. This hook will pass
411       * the created payments to the calling application for
412       * approval/adjustment.
413       *
414       * The adjusted payments are read back and inserted into
415       * IBY_PAYMENTS_LL table.
416       *
417       * This is a general hook that is called for all other
418       * products except AP. For AP special hooks are called
419       * below.
420       */
421 
422      IF (l_ca_id <> 200) THEN
423 
424          /*
425           * Only successful payments are passed to be passed to
426           * to the calling application via the hook / callout.
427           *
428           * From the existing list of all payments, create new data
429           * structures that only store successful payments. This
430           * 'success only' list of payments will be passed to the
431           * calling application.
432           *
433           * This method writes the payment data to global temp tables.
434           */
435          performPreHookProcess(l_ca_payreq_cd, l_ca_id, l_paymentTab,
436              l_docsInPmtTab, l_hookPaymentTab, l_hookDocsInPmtTab);
437 
438          /*
439           * Hook to call external application for implementation of the
440           * following functionality:
441           *
442           * 1. Bank charge calculation
443           * 2. Tax withtholding
444           *
445           * Any other miscellaneous correction of payment/document data
446           * is also allowed in the hook.
447           */
448          callHook(p_payment_request_id);
449 
450          /*
451           * The external app may decide not to pay a document(s)
452           * within a payment, or may decide not to make a payment(s).
453           * In such cases, the external app will set the 'don't pay flag'
454           * and provide a 'don't pay reason' at the document / payment
455           * level (as appropriate) in the provided data structures.
456           *
457           * If a document(s) is marked as don't pay, then we must
458           * adjust the payment amount appropriately.
459           *
460           * Also, some documents are related via 'payment grouping number'.
461           * All documents that are related must be failed and their
462           * constituent payment amounts must be adjusted
463           *
464           * This method reads the payment data from global temp tables.
465           */
466          performPostHookProcess(l_paymentTab, l_docsInPmtTab, l_hookPaymentTab,
467              l_hookDocsInPmtTab, l_docErrorTab, l_docTokenTab);
468 
469      END IF; -- if calling product <> AP
470 
471      /*
472       * SPECIAL HOOKS FOR AP:
473       *
474       * + Perform special handling for Japanese bank charges.
475       * + Invoke extended witholdings procedure (AWT)
476       */
477      IF (l_ca_id = 200) THEN
478 
479          handleJapaneseBankCharges(l_ca_payreq_cd, l_ca_id,
480              l_paymentTab, l_docsInPmtTab, l_docErrorTab, l_docTokenTab);
481 
482          performExtendedWitholding(l_ca_payreq_cd, l_ca_id,
483              l_paymentTab, l_docsInPmtTab, l_docErrorTab, l_docTokenTab);
484 
485      END IF;
486 
487      /*
488       * Flag payments that require separate remittance
489       * advice.
490       */
491      flagSeparateRemitAdvicePmts(l_paymentTab, l_docsInPmtTab);
492 
493      /*
494       * Get the rejection level system option and pass
495       * it to subsequent methods.
496       */
497 
498      /*
499       * Fix for bug 5302318:
500       *
501       * Request level system option overrides enterprise
502       * level setting.
503       */
504 
505      /*
506       * If the user has provided the pmt rejection level on
507       * the request use it.
508       *
509       * If not, get the rejection level system option at the
510       * enterprose level.
511       */
512      IF (p_pmt_rejection_level IS NOT NULL) THEN
513 
514          /*
515           * Use the payment rejection level passed in
516           * with the payment service request (if available).
517           */
518          l_rejection_level := p_pmt_rejection_level;
519          print_debuginfo(l_module_name, 'Using payment rejection '
520              || 'level [request-level setting]: '
521              || l_rejection_level
522              );
523 
524      ELSE
525 
526          /*
527           * If the payment rejection level is not passed
528           * in with the payment service request, derive
529           * the payment rejection level setting at the
530           * enterprise level.
531           */
532          l_rejection_level := getPmtRejLevelSysOption();
533          print_debuginfo(l_module_name, 'Using payment rejection '
534              || 'level [neterprise-level setting]: '
535              || l_rejection_level
536              );
537 
538      END IF;
539 
540      /*
541       * Get the review payments flag system option at
542       * the enterprise level. This may be overridden if
543       * request level review pmts flag is populated.
544       */
545      l_review_pmts_flag := getReviewPmtsSysOption();
546 
547      /*
548       * Adjust the review payments flag based on the request level
549       * parameter. If the request level parameter (if provided)
550       * will override the system level setting.
551       */
552      print_debuginfo(l_module_name, 'Review payments flag '
553          || '[enterprise-level setting]: '
554          || l_review_pmts_flag
555          );
556 
557      /*
558       * Fix for bug 5021996:
559       *
560       * There are two flags related to review of proposed payments -
561       * a. enterprise level setting (use by default).
562       * b. request level parameter (overrides default).
563       *
564       * If user has not provided this flag as a request level
565       * parameter, use the enterprise wide setting for this flag.
566       *
567       * Possible values for request level review proposed payment flag:
568       * 1. Y - Stop payments for review
569       * 2. N - Do not stop payments for review
570       * 3. X - User has not provided a value, use enterprise setting.
571       */
572      IF (UPPER(p_review_proposed_pmts_flag) = 'Y'  OR
573          UPPER(p_review_proposed_pmts_flag) = 'N') THEN
574 
575          print_debuginfo(l_module_name, 'Review payments flag '
576              || '[request-level setting]: '
577              || p_review_proposed_pmts_flag
578              || ' (this will override enterprise level setting)'
579              );
580 
581          l_review_pmts_flag := p_review_proposed_pmts_flag;
582 
583      END IF;
584 
585      print_debuginfo(l_module_name, 'Final review payments flag '
586          || 'setting: '
587          || l_review_pmts_flag
588          );
589 
590      /*
591       * All payments for this payment request have been
592       * created and stored in a PLSQL table. Now write these
593       * payments to the database.
594       *
595       * Similarly, update the documents table by providing a
596       * payment id to each document.
597       */
598 
599      /*
600       * Pass in the current ppr status. This is used
601       * to determine the next status in some cases.
602       */
603      x_return_status := l_payreq_status;
604 
605      performDBUpdates(p_payment_request_id, l_rejection_level,
606          l_review_pmts_flag, l_paymentTab,
607          l_docsInPmtTab, l_all_pmts_success_flag,
608          l_all_pmts_failed_flag, x_return_status,
609          l_docErrorTab, l_docTokenTab
610          );
611 
612      /*
613       * Finally, raise business events to inform the calling app
614       * if any payments have failed.
615       *
616       * Note: this should be the last call after database records
617       * have been inserted / updated. This is because you cannot
618       * 'rollback' a business event once raised.
619       */
620      raiseBizEvents(p_payment_request_id, l_ca_payreq_cd, l_ca_id,
621          l_rejection_level, l_review_pmts_flag,
622          l_all_pmts_success_flag, l_all_pmts_failed_flag);
623 
624      print_debuginfo(l_module_name, 'EXIT');
625 
626  END createPayments;
627 
628 /*--------------------------------------------------------------------
629  | NAME:
630  |     performDocumentGrouping
631  |
632  | PURPOSE:
633  |
634  |
635  |
636  | PARAMETERS:
637  |     IN
638  |
639  |
640  |     OUT
641  |
642  |
643  | RETURNS:
644  |
645  | NOTES:
646  |
647  *---------------------------------------------------------------------*/
648  PROCEDURE performDocumentGrouping(
649      p_payment_request_id   IN IBY_PAY_SERVICE_REQUESTS.
650                                          payment_service_request_id%TYPE,
651      x_paymentTab           IN OUT NOCOPY IBY_PAYGROUP_PUB.paymentTabType,
652      x_docsInPmtTab         IN OUT NOCOPY IBY_PAYGROUP_PUB.docsInPaymentTabType,
653      x_ca_id                IN OUT NOCOPY IBY_PAY_SERVICE_REQUESTS.
654                                               calling_app_id%TYPE,
655      x_ca_payreq_cd         IN OUT NOCOPY IBY_PAY_SERVICE_REQUESTS.
656                                               call_app_pay_service_req_code
657                                               %TYPE,
658      x_payReqCriteria       IN OUT NOCOPY IBY_PAYGROUP_PUB.
659                                               payReqImposedCriteria
660 
661 -- , x_cbrTab               IN OUT NOCOPY IBY_PAYGROUP_PUB.
662 --                                             centralBankReportTabType
663      )
664  IS
665  l_module_name           CONSTANT VARCHAR2(200) := G_PKG_NAME ||
666                                                    '.performDocumentGrouping';
667  l_first_record          VARCHAR2(1)   := 'Y';
668  l_payment_id            NUMBER(15)    := 0;
669  l_payment_total         NUMBER(15)    := 0;
670  l_payment_docs_cnt      NUMBER(10)    := 0;
671  l_sup_msg_flag          VARCHAR2(1)   := 'N';
672  l_bnk_chg_brer_flag     VARCHAR2(1)   := 'N';
673  l_delv_chnl_flag        VARCHAR2(1)   := 'N';
674  l_express_pmt_flag      VARCHAR2(1)   := 'N';
675  l_pmt_details_flag      VARCHAR2(1)   := 'N';
676  l_pmt_details_len       NUMBER(15)    := 0;
677  l_pmt_det_formula       VARCHAR2(2000):= 0;
678  l_max_docs_flag         VARCHAR2(1)   := 'N';
679  l_max_docs_limit        NUMBER(15)    := 0;
680  l_docs_in_pmt_count     NUMBER(15)    := 0;
681  l_unique_remit_id_flag  VARCHAR2(1)   := 'N';
682  l_pmt_reason_flag       VARCHAR2(1)   := 'N';
683  l_due_date_flag         VARCHAR2(1)   := 'N';
684  l_prom_note_flag        VARCHAR2(1)   := 'N';
685 
686  l_profile_id            IBY_PAYMENT_PROFILES.payment_profile_id%type;
687 
688  /*
689   * Used to substitute null values in date comparisons.
690   * It is assumed that year 5100 = doomsday.
691   */
692  l_impossible_date       DATE := TO_DATE('01/01/5100 10:25:55',
693                                      'MM/DD/YYYY HH24:MI:SS');
694 
695  l_bank_inst1_code       IBY_PAYMENTS_ALL.bank_instruction1_code%TYPE;
696  l_bank_inst2_code       IBY_PAYMENTS_ALL.bank_instruction1_code%TYPE;
697  l_pmt_txt_msg1          IBY_PAYMENTS_ALL.payment_text_message1%TYPE;
698  l_pmt_txt_msg2          IBY_PAYMENTS_ALL.payment_text_message2%TYPE;
699  l_pmt_txt_msg3          IBY_PAYMENTS_ALL.payment_text_message3%TYPE;
700 
701  l_processing_type       VARCHAR2(100);  -- CHECK | ELECTRONIC
702  l_calc_doc_detail       VARCHAR2(1000); -- Stores details for a single doc
703 
704  /* these are related to central bank reporting */
705  l_decl_option             VARCHAR2(100) := '';
706  l_decl_only_fx_flag       VARCHAR2(1)   := '';
707  l_decl_curr_fx_rate_type  VARCHAR2(255) := '';
708  l_decl_curr_code          VARCHAR2(10)  := '';
709  l_decl_threshold_amount   NUMBER(15)    := 0;
710 /*
711  l_cbrRec                  IBY_PAYGROUP_PUB.centralBankReportRecType;
712  l_cbrTab                  IBY_PAYGROUP_PUB.centralBankReportTabType;
713  l_cbr_rec_exists          BOOLEAN       := FALSE;
714 */
715 
716  l_ca_payreq_cd            VARCHAR2(255) := '';
717  l_ca_id                   NUMBER(15)    := 0;
718  l_all_pmts_success_flag   BOOLEAN       := FALSE;
719  l_all_pmts_failed_flag    BOOLEAN       := FALSE;
720  l_le_id                   NUMBER(15)    := 0;
721 
722  /* rejection level system options */
723  l_rejection_level         VARCHAR2(200);
724  l_review_pmts_flag        VARCHAR2(1)   := 'N';
725 
726  l_paymentRec        IBY_PAYMENTS_ALL%ROWTYPE;
727  l_docsInPmtRec      IBY_PAYGROUP_PUB.docsInPaymentRecType;
728 
729  /*
730   * These two are related data structures. Each row in paymentTabType
731   * PLSQL table is used in inserting a row into the IBY_PAYMENTS_ALL
732   * table.
733   *
734   * Since the IBY_PAYMENTS_ALL table does not contain a document id, a
735   * separate data structure is needed to keep track of the documents
736   * that are part of a payment. This information is tracked in the
737   * docsInPaymentTabType table. The rows in docsInPaymentTabType are
738   * used to update the rows in IBY_DOCS_PAYABLE_ALL table with
739   * payment ids.
740   *
741   *            x_paymentTab                        x_docsInPmtTab
742   *       (insert into IBY_PAYMENTS_ALL)          (update IBY_DOCS_PAYABLE)
743   * /-------------------------------------\       /------------\
744   * |Payment|Payment|..|Payment|Docs   |..|       |Payment|Doc |
745   * |Id     |Profile|..|Amount |Payable|..|       |Id     |Id  |
746   * |       |Id     |..|       |Count  |..|       |       |    |
747   * |-------------------------------------|       |------------|
748   * |   4000|     10|  |    550|      3|  |       |   4000| 501|
749   * |       |       |  |       |       |  |       |   4000| 504|
750   * |       |       |  |       |       |  |       |   4000| 505|
751   * |-------|-------|--|-------|-------|--|       |-------|----|
752   * |   4001|     10|  |1063.70|     19|  |       |   4001| 502|
753   * |       |       |  |       |       |  |       |   4001| 509|
754   * |       |       |  |       |       |  |       |   4001| 511|
755   * |       |       |  |       |       |  |       |   4001| 523|
756   * |       |       |  |       |       |  |       |     : |  : |
757   * |-------|-------|--|-------|-------|--|       |-------|----|
758   * |    :  |     : |  |    :  |     : |  |       |     : |  : |
759   * \_______|_______|__|_______|_______|__/       \_______|____/
760   *
761   * Combining these two structures into one structure is messy
762   * because you cannot directly use the combined data structure for
763   * bulk updates.
764   */
765 
766  /* these two are passed to calling app via hook */
767  l_hookPaymentTab    IBY_PAYGROUP_PUB.hookPaymentTabType;
768  l_hookDocsInPmtTab  IBY_PAYGROUP_PUB.hookDocsInPaymentTabType;
769 
770  /* holds the error messages against failed documents */
771  l_docErrorTab       IBY_VALIDATIONSETS_PUB.docErrorTabType;
772 
773  /* holds list of internal bank accounts with corresponding legal entity id */
774  l_bankAcctLETab     bankAccountLETabType;
775 
776  /* previous document attributes */
777  prev_doc_id                 iby_docs_payable_all.document_payable_id%TYPE;
778  prev_ca_id                  iby_docs_payable_all.calling_app_id%TYPE;
779  prev_ca_doc_id1             iby_docs_payable_all.
780                                  calling_app_doc_unique_ref1%TYPE;
781  prev_ca_doc_id2             iby_docs_payable_all.
782                                  calling_app_doc_unique_ref2%TYPE;
783  prev_ca_doc_id3             iby_docs_payable_all.
784                                  calling_app_doc_unique_ref3%TYPE;
785  prev_ca_doc_id4             iby_docs_payable_all.
786                                  calling_app_doc_unique_ref4%TYPE;
787  prev_ca_doc_id5             iby_docs_payable_all.
788                                  calling_app_doc_unique_ref5%TYPE;
789  prev_pp_tt_cd               iby_docs_payable_all.pay_proc_trxn_type_code%TYPE;
790  prev_pmt_grp_num            iby_docs_payable_all.payment_grouping_number%TYPE;
791  prev_pmt_method_cd          iby_docs_payable_all.payment_method_code%TYPE;
792  prev_doc_pmt_currency       iby_docs_payable_all.payment_currency_code%TYPE;
793  prev_doc_pmt_amount         iby_docs_payable_all.payment_amount%TYPE;
794  prev_int_bank_acct_id       iby_docs_payable_all.
795                                  internal_bank_account_id%TYPE;
796  prev_ext_bank_acct_id       iby_docs_payable_all.
797                                  external_bank_account_id%TYPE;
798  prev_payee_id               iby_docs_payable_all.payee_party_id%TYPE;
799  prev_payee_site_id          iby_docs_payable_all.party_site_id%TYPE;
800  prev_supplier_site_id       iby_docs_payable_all.supplier_site_id%TYPE;
801  prev_remit_loc_id           iby_docs_payable_all.remit_to_location_id%TYPE;
802  prev_ext_payee_id           iby_docs_payable_all.ext_payee_id%TYPE;
803  prev_org_id                 iby_docs_payable_all.org_id%TYPE;
804  prev_org_type               iby_docs_payable_all.org_type%TYPE;
805  prev_profile_id             iby_docs_payable_all.payment_profile_id%TYPE;
806  prev_pay_alone_flag         iby_docs_payable_all.exclusive_payment_flag%TYPE;
807  prev_supplier_msg1          iby_docs_payable_all.remittance_message1%TYPE;
808  prev_supplier_msg2          iby_docs_payable_all.remittance_message2%TYPE;
809  prev_supplier_msg3          iby_docs_payable_all.remittance_message3%TYPE;
810  prev_bnk_chg_bearer         iby_docs_payable_all.bank_charge_bearer%TYPE;
811  prev_delivery_channel       iby_docs_payable_all.delivery_channel_code%TYPE;
812  prev_settle_priority        iby_docs_payable_all.settlement_priority%TYPE;
813  prev_uri                    iby_docs_payable_all.
814                                  unique_remittance_identifier%TYPE;
815  prev_uri_checkdigits        iby_docs_payable_all.uri_check_digit%TYPE;
816  prev_pmt_reason_code        iby_docs_payable_all.payment_reason_code%TYPE;
817  prev_pmt_reason_commts      iby_docs_payable_all.payment_reason_comments%TYPE;
818  prev_pmt_function           iby_docs_payable_all.payment_function%TYPE;
819  prev_pmt_date               iby_docs_payable_all.payment_date%TYPE;
820  prev_pmt_due_date           iby_docs_payable_all.payment_due_date%TYPE;
821  prev_benef_party            iby_docs_payable_all.beneficiary_party%TYPE;
822 
823  /* payee (employee) address related fields */
824  prev_address_source         iby_docs_payable_all.address_source%TYPE;
825  prev_emp_add_code           iby_docs_payable_all.employee_address_code%TYPE;
826  prev_emp_person_id          iby_docs_payable_all.employee_person_id%TYPE;
827  prev_emp_addr_id            iby_docs_payable_all.employee_address_id%TYPE;
828 
829  prev_emp_pmt_flag           iby_docs_payable_all.employee_payment_flag%TYPE;
830 
831  /*TPP-Start*/
832  prev_inv_payee_party_id         iby_docs_payable_all.inv_payee_party_id%TYPE;
833  prev_inv_party_site_id          iby_docs_payable_all.inv_party_site_id%TYPE;
834  prev_inv_supplier_site_id       iby_docs_payable_all.inv_supplier_site_id%TYPE;
835  prev_ext_inv_payee_id           iby_docs_payable_all.ext_inv_payee_id%TYPE;
836  prev_inv_beneficiary_party            iby_docs_payable_all.inv_beneficiary_party%TYPE;
837 
838  curr_inv_payee_party_id         iby_docs_payable_all.inv_payee_party_id%TYPE;
839  curr_inv_party_site_id          iby_docs_payable_all.inv_party_site_id%TYPE;
840  curr_inv_supplier_site_id       iby_docs_payable_all.inv_supplier_site_id%TYPE;
841  curr_ext_inv_payee_id           iby_docs_payable_all.ext_inv_payee_id%TYPE;
842  curr_inv_beneficiary_party            iby_docs_payable_all.inv_beneficiary_party%TYPE;
843  /*TPP-End*/
844 
845 
846  /* current document attributes */
847  curr_doc_id                 iby_docs_payable_all.document_payable_id%TYPE;
848  curr_ca_id                  iby_docs_payable_all.calling_app_id%TYPE;
849  curr_ca_doc_id1             iby_docs_payable_all.
850                                  calling_app_doc_unique_ref1%TYPE;
851  curr_ca_doc_id2             iby_docs_payable_all.
852                                  calling_app_doc_unique_ref2%TYPE;
853  curr_ca_doc_id3             iby_docs_payable_all.
854                                  calling_app_doc_unique_ref3%TYPE;
855  curr_ca_doc_id4             iby_docs_payable_all.
856                                  calling_app_doc_unique_ref4%TYPE;
857  curr_ca_doc_id5             iby_docs_payable_all.
858                                  calling_app_doc_unique_ref5%TYPE;
859  curr_pp_tt_cd               iby_docs_payable_all.pay_proc_trxn_type_code%TYPE;
860  curr_pmt_grp_num            iby_docs_payable_all.payment_grouping_number%TYPE;
861  curr_pmt_method_cd          iby_docs_payable_all.payment_method_code%TYPE;
862  curr_doc_pmt_currency       iby_docs_payable_all.payment_currency_code%TYPE;
863  curr_doc_pmt_amount         iby_docs_payable_all.payment_amount%TYPE;
864  curr_int_bank_acct_id       iby_docs_payable_all.
865                                  internal_bank_account_id%TYPE;
866  curr_ext_bank_acct_id       iby_docs_payable_all.
867                                  external_bank_account_id%TYPE;
868  curr_org_id                 iby_docs_payable_all.org_id%TYPE;
869  curr_org_type               iby_docs_payable_all.org_type%TYPE;
870  curr_payee_id               iby_docs_payable_all.payee_party_id%TYPE;
871  curr_payee_site_id          iby_docs_payable_all.party_site_id%TYPE;
872  curr_supplier_site_id       iby_docs_payable_all.supplier_site_id%TYPE;
873  curr_remit_loc_id           iby_docs_payable_all.remit_to_location_id%TYPE;
874  curr_ext_payee_id           iby_docs_payable_all.ext_payee_id%TYPE;
875  curr_profile_id             iby_docs_payable_all.payment_profile_id%TYPE;
876  curr_pay_alone_flag         iby_docs_payable_all.exclusive_payment_flag%TYPE;
877  curr_supplier_msg1          iby_docs_payable_all.remittance_message1%TYPE;
878  curr_supplier_msg2          iby_docs_payable_all.remittance_message2%TYPE;
879  curr_supplier_msg3          iby_docs_payable_all.remittance_message3%TYPE;
880  curr_bnk_chg_bearer         iby_docs_payable_all.bank_charge_bearer%TYPE;
881  curr_delivery_channel       iby_docs_payable_all.delivery_channel_code%TYPE;
882  curr_settle_priority        iby_docs_payable_all.settlement_priority%TYPE;
883  curr_uri                    iby_docs_payable_all.
884                                  unique_remittance_identifier%TYPE;
885  curr_uri_checkdigits        iby_docs_payable_all.uri_check_digit%TYPE;
886  curr_pmt_reason_code        iby_docs_payable_all.payment_reason_code%TYPE;
887  curr_pmt_reason_commts      iby_docs_payable_all.payment_reason_comments%TYPE;
888  curr_pmt_function           iby_docs_payable_all.payment_function%TYPE;
889  curr_pmt_date               iby_docs_payable_all.payment_date%TYPE;
890  curr_pmt_due_date           iby_docs_payable_all.payment_due_date%TYPE;
891  curr_benef_party            iby_docs_payable_all.beneficiary_party%TYPE;
892 
893  /* payee (employee) address related fields */
894  curr_address_source         iby_docs_payable_all.address_source%TYPE;
895  curr_emp_add_code           iby_docs_payable_all.employee_address_code%TYPE;
896  curr_emp_person_id          iby_docs_payable_all.employee_person_id%TYPE;
897  curr_emp_addr_id            iby_docs_payable_all.employee_address_id%TYPE;
898 
899  curr_emp_pmt_flag           iby_docs_payable_all.employee_payment_flag%TYPE;
900 
901  /* these two are passed to the external application hook */
902  l_amount_withheld           iby_docs_payable_all.amount_withheld%TYPE;
903 
904  /* used for maturity date calculation */
905  l_discount_date             iby_docs_payable_all.discount_date%TYPE;
906 
907  /* passed to hook */
908  l_discount_amount           iby_docs_payable_all.payment_curr_discount_taken
909                                                       %TYPE;
910  l_trx_line_index            BINARY_INTEGER;
911  l_no_rec_in_ppr             BOOLEAN;
912  G_LINES_PER_FETCH           CONSTANT  NUMBER:= 1000;
913 -- G_LINES_PER_FETCH           CONSTANT  NUMBER:= 3;
914 
915  /*
916   * This cursor up will pick up all valid documents for the specified
917   * payment request. The select statement will order the documents
918   * based on grouping criteria.
919   *
920   * As the grouping is operation is commutative, there is no
921   * need to group the documents by any particular order (in
922   * other words, grouping 1 followed by grouping 2, will lead
923   * to the same results as grouping 2 followed by grouping 1).
924   *
925   * Important Note:
926   * Always ensure that there is a corresponding order by
927   * clause for each grouping criterion that you wish to use.
928   * This is required in order to create minimum possible
929   * payments from a given set of documents.
930   *
931   *
932   * Populate legal entity ids for each of the created payments.
933   * The legal entity id on the payment is a special attribute.
934   * It is not a grouping rule, instead it has to be derived
935   * from the internal bank account and populated.
936   *
937   */
938  CURSOR c_documents(p_payment_request_id VARCHAR2)
939  IS
940  SELECT prq.call_app_pay_service_req_code,   -- Callers pay req id
941         docs.document_payable_id,            -- Unique doc id asgn by IBY
942         docs.calling_app_id,                 --| The caller
943         docs.calling_app_doc_unique_ref1,    --| uses these
944         docs.calling_app_doc_unique_ref2,    --| seven fields
945         docs.calling_app_doc_unique_ref3,    --| to uniquely
946         docs.calling_app_doc_unique_ref4,    --| identify
947         docs.calling_app_doc_unique_ref5,    --| a document
948         docs.pay_proc_trxn_type_code,        --| payable
949         docs.payment_grouping_number,        -- Identifies related docs
950         docs.payment_method_code,            -- Payment method
951         docs.internal_bank_account_id,       -- Internal bank account id
952         docs.external_bank_account_id,       -- External bank account id
953         docs.payment_profile_id,             -- Payment profile id
954         docs.org_id,
955         docs.org_type,
956         docs.payment_function,
957         docs.ext_payee_id,                   -- Ext payee id (payee context)
958         docs.payee_party_id,
959         docs.party_site_id,
960         docs.supplier_site_id,
961         docs.remit_to_location_id,
962         docs.amount_withheld,                -- Passed to hook for adjustmt
963         ext_payee.bank_instruction1_code,    --|
964         ext_payee.bank_instruction2_code,    --| These fields are
965         ext_payee.payment_text_message1,     --| derived from the
966         ext_payee.payment_text_message2,     --| payee.
967         ext_payee.payment_text_message3,     --|
968         docs.payment_currency_code,
969         docs.payment_amount,
970         docs.payment_date,
971         docs.exclusive_payment_flag,
972         docs.bank_charge_bearer,             -- Bank charge bearer
973         docs.delivery_channel_code,          -- Delivery channel
974         docs.settlement_priority,
975         docs.remittance_message1,            -- Supplier message 1
976         docs.remittance_message2,            -- Supplier message 2
977         docs.remittance_message3,            -- Supplier message 3
978         docs.unique_remittance_identifier,
979         docs.uri_check_digit,
980         docs.payment_reason_code,
981         docs.payment_reason_comments,
982         docs.payment_due_date,
983         docs.discount_date,
984         docs.payment_curr_discount_taken,
985         docs.beneficiary_party,
986         docs.address_source,                 --
987         docs.employee_address_code,          -- Employee address
988         docs.employee_person_id,             -- related fields
989         docs.employee_address_id,            --
990         docs.employee_payment_flag,
991         pcr.group_by_remittance_message,
992         pcr.group_by_bank_charge_bearer,
993         pcr.group_by_delivery_channel,
994         pcr.group_by_settle_priority_flag,
995         pcr.group_by_payment_details_flag,
996         DECODE(pcr.payment_details_length_limit, NULL,
997             -1, pcr.payment_details_length_limit),
998         pcr.payment_details_formula,
999         pcr.group_by_max_documents_flag,     -- Max documents per payment flag
1000         pcr.max_documents_per_payment,       -- Max documents per payment value
1001         pcr.group_by_unique_remit_id_flag,
1002         pcr.group_by_payment_reason,
1003         pcr.group_by_due_date_flag,
1004         ppf.processing_type,
1005         ppf.declaration_option,
1006         ppf.dcl_only_foreign_curr_pmt_flag,
1007         ppf.declaration_curr_fx_rate_type,
1008         ppf.declaration_currency_code,
1009         ppf.declaration_threshold_amount,
1010         DECODE(prq.maximum_payment_amount, NULL, -1,
1011             prq.maximum_payment_amount),
1012         DECODE(prq.minimum_payment_amount, NULL, -1,
1013             prq.minimum_payment_amount),
1014         prq.allow_zero_payments_flag,
1015         pmt_mthd.support_bills_payable_flag,
1016         iba.account_owner_org_id,
1017 	/*TPP-Start*/
1018         docs.inv_payee_party_id,
1019         docs.inv_party_site_id,
1020         docs.inv_supplier_site_id,
1021         docs.inv_beneficiary_party,
1022         docs.ext_inv_payee_id
1023 	/*TPP-Start*/
1024  FROM IBY_DOCS_PAYABLE_ALL       docs,
1025       IBY_PMT_CREATION_RULES     pcr,
1026       IBY_PAYMENT_PROFILES       ppf,
1027       IBY_PAY_SERVICE_REQUESTS   prq,
1028       IBY_EXTERNAL_PAYEES_ALL    ext_payee,
1029       IBY_PAYMENT_METHODS_B      pmt_mthd,
1030       CE_BANK_ACCOUNTS           iba
1031  WHERE  prq.payment_service_request_id  = p_payment_request_id
1032  AND    docs.payment_service_request_id = prq.payment_service_request_id
1033  AND    docs.payment_profile_id         = ppf.payment_profile_id
1034  AND    ppf.system_profile_code         = pcr.system_profile_code(+)
1035  AND    docs.document_status            = DOC_STATUS_VALIDATED
1036  AND    docs.ext_payee_id               = ext_payee.ext_payee_id
1037  AND    docs.payment_method_code        = pmt_mthd.payment_method_code
1038  AND    docs.internal_bank_account_id   = iba.bank_account_id
1039  ORDER BY
1040         docs.exclusive_payment_flag,
1041         docs.org_id,
1042         docs.org_type,
1043         ext_payee.ext_payee_id,        -- \
1044         docs.payment_currency_code,    -- |
1045         docs.payment_method_code,      -- |
1046         docs.payment_profile_id,       -- |
1047         docs.payment_grouping_number,  -- |
1048         docs.internal_bank_account_id, -- | Ensure that the grouping
1049         docs.external_bank_account_id, -- | rules below follow the same
1050         docs.payment_function,         -- | ordering as this order by
1051         docs.payment_date,             -- | clause.
1052         docs.remit_to_location_id,     -- |
1053         docs.beneficiary_party,        -- |
1054         docs.address_source,           -- |
1055         docs.employee_address_code,    -- |
1056         docs.employee_person_id,       -- |
1057         docs.employee_address_id,      -- |
1058         docs.employee_payment_flag,    -- |
1059         docs.bank_charge_bearer,       -- |
1060         docs.delivery_channel_code,    -- | This order must be followed
1061         docs.settlement_priority,      -- | for creating minimum number
1062         docs.remittance_message1,      -- | of payments.
1063         docs.remittance_message2,      -- |
1064         docs.remittance_message3,      -- /
1065         docs.unique_remittance_identifier,
1066         docs.uri_check_digit,
1067         docs.payment_reason_code,
1068         docs.payment_reason_comments,
1069         docs.payment_due_date
1070         ;
1071 
1072  /*
1073   * Cursor to pick up all possible internal bank accounts
1074   * along with their corresponding legal entity ids.
1075   *
1076  CURSOR c_bank_acct_le_list
1077  IS
1078  SELECT
1079      iba.bank_account_id       int_bank_account,
1080      iba.account_owner_org_id  legal_entity_id
1081  FROM
1082      CE_BANK_ACCOUNTS iba
1083  ;
1084   */
1085 
1086  BEGIN
1087 
1088      print_debuginfo(l_module_name, 'ENTER');
1089 
1090      print_debuginfo(l_module_name, 'Payment Request Id : '||
1091          p_payment_request_id);
1092 
1093      l_no_rec_in_ppr := TRUE;
1094      /*
1095       * Pick up all the documents of the payment request.
1096       */
1097      OPEN  c_documents(p_payment_request_id);
1098      LOOP
1099         iby_disburse_submit_pub_pkg.delete_docspayTab;
1100         FETCH c_documents BULK COLLECT INTO
1101            iby_disburse_submit_pub_pkg.docspayTab.call_app_pay_service_req_code,
1102            iby_disburse_submit_pub_pkg.docspayTab.document_payable_id,
1103            iby_disburse_submit_pub_pkg.docspayTab.calling_app_id,
1104            iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref1,
1105            iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref2,
1106            iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref3,
1107            iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref4,
1108            iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref5,
1109            iby_disburse_submit_pub_pkg.docspayTab.pay_proc_trxn_type_code,
1110            iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number,
1111            iby_disburse_submit_pub_pkg.docspayTab.payment_method_code,
1112            iby_disburse_submit_pub_pkg.docspayTab.internal_bank_account_id,
1113            iby_disburse_submit_pub_pkg.docspayTab.external_bank_account_id,
1114            iby_disburse_submit_pub_pkg.docspayTab.payment_profile_id,
1115            iby_disburse_submit_pub_pkg.docspayTab.org_id,
1116            iby_disburse_submit_pub_pkg.docspayTab.org_type,
1117            iby_disburse_submit_pub_pkg.docspayTab.payment_function,
1118            iby_disburse_submit_pub_pkg.docspayTab.ext_payee_id,
1119            iby_disburse_submit_pub_pkg.docspayTab.payee_party_id,
1120            iby_disburse_submit_pub_pkg.docspayTab.party_site_id,
1121            iby_disburse_submit_pub_pkg.docspayTab.supplier_site_id,
1122            iby_disburse_submit_pub_pkg.docspayTab.remit_to_location_id,
1123            iby_disburse_submit_pub_pkg.docspayTab.amount_withheld,
1124            iby_disburse_submit_pub_pkg.docspayTab.bank_instruction1_code,
1125            iby_disburse_submit_pub_pkg.docspayTab.bank_instruction2_code,
1126            iby_disburse_submit_pub_pkg.docspayTab.payment_text_message1,
1127            iby_disburse_submit_pub_pkg.docspayTab.payment_text_message2,
1128            iby_disburse_submit_pub_pkg.docspayTab.payment_text_message3,
1129            iby_disburse_submit_pub_pkg.docspayTab.payment_currency_code,
1130            iby_disburse_submit_pub_pkg.docspayTab.payment_amount,
1131            iby_disburse_submit_pub_pkg.docspayTab.payment_date,
1132            iby_disburse_submit_pub_pkg.docspayTab.exclusive_payment_flag,
1133            iby_disburse_submit_pub_pkg.docspayTab.bank_charge_bearer,
1134            iby_disburse_submit_pub_pkg.docspayTab.delivery_channel_code,
1135            iby_disburse_submit_pub_pkg.docspayTab.settlement_priority,
1136            iby_disburse_submit_pub_pkg.docspayTab.remittance_message1,
1137            iby_disburse_submit_pub_pkg.docspayTab.remittance_message2,
1138            iby_disburse_submit_pub_pkg.docspayTab.remittance_message3,
1139            iby_disburse_submit_pub_pkg.docspayTab.unique_remittance_identifier,
1140            iby_disburse_submit_pub_pkg.docspayTab.uri_check_digit,
1141            iby_disburse_submit_pub_pkg.docspayTab.payment_reason_code,
1142            iby_disburse_submit_pub_pkg.docspayTab.payment_reason_comments,
1143            iby_disburse_submit_pub_pkg.docspayTab.payment_due_date,
1144            iby_disburse_submit_pub_pkg.docspayTab.discount_date,
1145            iby_disburse_submit_pub_pkg.docspayTab.payment_curr_discount_taken,
1146            iby_disburse_submit_pub_pkg.docspayTab.beneficiary_party,
1147            iby_disburse_submit_pub_pkg.docspayTab.address_source,
1148            iby_disburse_submit_pub_pkg.docspayTab.employee_address_code,
1149            iby_disburse_submit_pub_pkg.docspayTab.employee_person_id,
1150            iby_disburse_submit_pub_pkg.docspayTab.employee_address_id,
1151            iby_disburse_submit_pub_pkg.docspayTab.employee_payment_flag,
1152            iby_disburse_submit_pub_pkg.docspayTab.group_by_remittance_message,
1153            iby_disburse_submit_pub_pkg.docspayTab.group_by_bank_charge_bearer,
1154            iby_disburse_submit_pub_pkg.docspayTab.group_by_delivery_channel,
1155            iby_disburse_submit_pub_pkg.docspayTab.group_by_settle_priority_flag,
1156            iby_disburse_submit_pub_pkg.docspayTab.group_by_payment_details_flag,
1157            iby_disburse_submit_pub_pkg.docspayTab.payment_details_length_limit,
1158            iby_disburse_submit_pub_pkg.docspayTab.payment_details_formula,
1159            iby_disburse_submit_pub_pkg.docspayTab.group_by_max_documents_flag,
1160            iby_disburse_submit_pub_pkg.docspayTab.max_documents_per_payment,
1161            iby_disburse_submit_pub_pkg.docspayTab.group_by_unique_remit_id_flag,
1162            iby_disburse_submit_pub_pkg.docspayTab.group_by_payment_reason,
1163            iby_disburse_submit_pub_pkg.docspayTab.group_by_due_date_flag,
1164            iby_disburse_submit_pub_pkg.docspayTab.processing_type,
1165            iby_disburse_submit_pub_pkg.docspayTab.declaration_option,
1166            iby_disburse_submit_pub_pkg.docspayTab.dcl_only_foreign_curr_pmt_flag,
1167            iby_disburse_submit_pub_pkg.docspayTab.declaration_curr_fx_rate_type,
1168            iby_disburse_submit_pub_pkg.docspayTab.declaration_currency_code,
1169            iby_disburse_submit_pub_pkg.docspayTab.declaration_threshold_amount,
1170            iby_disburse_submit_pub_pkg.docspayTab.maximum_payment_amount,
1171            iby_disburse_submit_pub_pkg.docspayTab.minimum_payment_amount,
1172            iby_disburse_submit_pub_pkg.docspayTab.allow_zero_payments_flag,
1173            iby_disburse_submit_pub_pkg.docspayTab.support_bills_payable_flag,
1174            iby_disburse_submit_pub_pkg.docspayTab.iba_legal_entity_id,
1175            iby_disburse_submit_pub_pkg.docspayTab.inv_payee_party_id,
1176            iby_disburse_submit_pub_pkg.docspayTab.inv_party_site_id,
1177            iby_disburse_submit_pub_pkg.docspayTab.inv_supplier_site_id,
1178            iby_disburse_submit_pub_pkg.docspayTab.inv_beneficiary_party,
1179            iby_disburse_submit_pub_pkg.docspayTab.ext_inv_payee_id
1180         LIMIT G_LINES_PER_FETCH;
1181 
1182      /*
1183       * Loop through all the fetched documents, grouping them
1184       * into payments.
1185       */
1186         FOR l_trx_line_index IN nvl(iby_disburse_submit_pub_pkg.docspayTab.pay_proc_trxn_type_code.FIRST,0) .. nvl(iby_disburse_submit_pub_pkg.docspayTab.pay_proc_trxn_type_code.LAST,-99)
1187         LOOP
1188            l_no_rec_in_ppr := FALSE;
1189 
1190            print_debuginfo(l_module_name, 'Processed document payable id: '
1191             || to_char(iby_disburse_submit_pub_pkg.docspayTab.document_payable_id(l_trx_line_index)));
1192 
1193          /* these two used in raising biz events */
1194          /* they will not vary within a request */
1195          l_ca_id                := iby_disburse_submit_pub_pkg.docspayTab.calling_app_id(l_trx_line_index);
1196          l_ca_payreq_cd         := iby_disburse_submit_pub_pkg.docspayTab.call_app_pay_service_req_code(l_trx_line_index);
1197          l_delv_chnl_flag       := iby_disburse_submit_pub_pkg.docspayTab.group_by_delivery_channel(l_trx_line_index);
1198          l_express_pmt_flag     := iby_disburse_submit_pub_pkg.docspayTab.group_by_settle_priority_flag(l_trx_line_index);
1199          l_bnk_chg_brer_flag    := iby_disburse_submit_pub_pkg.docspayTab.group_by_bank_charge_bearer(l_trx_line_index);
1200          l_sup_msg_flag         := iby_disburse_submit_pub_pkg.docspayTab.group_by_remittance_message(l_trx_line_index);
1201          l_pmt_details_flag     := iby_disburse_submit_pub_pkg.docspayTab.group_by_payment_details_flag(l_trx_line_index);
1202          l_pmt_details_len      := iby_disburse_submit_pub_pkg.docspayTab.payment_details_length_limit(l_trx_line_index);
1203          l_pmt_det_formula      := iby_disburse_submit_pub_pkg.docspayTab.payment_details_formula(l_trx_line_index);
1204          l_processing_type      := iby_disburse_submit_pub_pkg.docspayTab.processing_type(l_trx_line_index);
1205 
1206          l_max_docs_flag        := iby_disburse_submit_pub_pkg.docspayTab.group_by_max_documents_flag(l_trx_line_index);
1207          l_max_docs_limit       := iby_disburse_submit_pub_pkg.docspayTab.max_documents_per_payment(l_trx_line_index);
1208          l_unique_remit_id_flag := iby_disburse_submit_pub_pkg.docspayTab.group_by_unique_remit_id_flag(l_trx_line_index);
1209          l_pmt_reason_flag      := iby_disburse_submit_pub_pkg.docspayTab.group_by_payment_reason(l_trx_line_index);
1210          l_due_date_flag        := iby_disburse_submit_pub_pkg.docspayTab.group_by_due_date_flag(l_trx_line_index);
1211          l_prom_note_flag       := iby_disburse_submit_pub_pkg.docspayTab.support_bills_payable_flag(l_trx_line_index);
1212 
1213          l_decl_option            := iby_disburse_submit_pub_pkg.docspayTab.declaration_option(l_trx_line_index);
1214          l_decl_only_fx_flag      := iby_disburse_submit_pub_pkg.docspayTab.dcl_only_foreign_curr_pmt_flag(l_trx_line_index);
1215          l_decl_curr_fx_rate_type := iby_disburse_submit_pub_pkg.docspayTab.declaration_curr_fx_rate_type(l_trx_line_index);
1216          l_decl_curr_code         := iby_disburse_submit_pub_pkg.docspayTab.declaration_currency_code(l_trx_line_index);
1217          l_decl_threshold_amount  := iby_disburse_submit_pub_pkg.docspayTab.declaration_threshold_amount(l_trx_line_index);
1218 
1219          /*
1220           * Store the payment request imposed limits in the output
1221           * parameter. These will be used for validation on the
1222           * created payments.
1223           */
1224          x_payReqCriteria.max_pmt_amt_limit    :=
1225                                      iby_disburse_submit_pub_pkg.docspayTab.maximum_payment_amount(l_trx_line_index);
1226          x_payReqCriteria.min_pmt_amt_limit    :=
1227                                      iby_disburse_submit_pub_pkg.docspayTab.minimum_payment_amount(l_trx_line_index);
1228          x_payReqCriteria.allow_zero_pmts_flag :=
1229                                      iby_disburse_submit_pub_pkg.docspayTab.allow_zero_payments_flag(l_trx_line_index);
1230 
1231          /*
1232           * Store the central bank reporting related fields in
1233           * the CBR record.
1234          l_cbrRec.decl_option            := l_decl_option;
1235          l_cbrRec.decl_only_fx_flag      := l_decl_only_fx_flag;
1236          l_cbrRec.decl_curr_fx_rate_type := l_decl_curr_fx_rate_type;
1237          l_cbrRec.decl_curr_code         := l_decl_curr_code;
1238          l_cbrRec.decl_threshold_amount  := l_decl_threshold_amount;
1239           */
1240 
1241          /* these are payee related fields */
1242          l_bank_inst1_code := iby_disburse_submit_pub_pkg.docspayTab.bank_instruction1_code(l_trx_line_index);
1243          l_bank_inst2_code := iby_disburse_submit_pub_pkg.docspayTab.bank_instruction2_code(l_trx_line_index);
1244          l_pmt_txt_msg1    := iby_disburse_submit_pub_pkg.docspayTab.payment_text_message1(l_trx_line_index);
1245          l_pmt_txt_msg2    := iby_disburse_submit_pub_pkg.docspayTab.payment_text_message2(l_trx_line_index);
1246          l_pmt_txt_msg3    := iby_disburse_submit_pub_pkg.docspayTab.payment_text_message3(l_trx_line_index);
1247 
1248          /* these are passed onto the hook for adjustment by ext app */
1249          l_amount_withheld    := iby_disburse_submit_pub_pkg.docspayTab.amount_withheld(l_trx_line_index);
1250 
1251          /* used for maturity date calculation */
1252          l_discount_date    := iby_disburse_submit_pub_pkg.docspayTab.discount_date(l_trx_line_index);
1253 
1254          l_discount_amount  := iby_disburse_submit_pub_pkg.docspayTab.payment_curr_discount_taken(l_trx_line_index);
1255 
1256          /*
1257           * Log all the fetched document fields
1258           *
1259          print_debuginfo(l_module_name,
1260              'Fetched document data for document:' || curr_doc_id
1261              || ', calling app id: '        || curr_ca_id
1262              || ', calling app doc id1: '   || curr_ca_doc_id1
1263              || ', calling app doc id2: '   || curr_ca_doc_id2
1264              || ', calling app doc id3: '   || curr_ca_doc_id3
1265              || ', calling app doc id4: '   || curr_ca_doc_id4
1266              || ', calling app doc id5: '   || curr_ca_doc_id5
1267              || ', calling app trxn type: ' || curr_pp_tt_cd
1268              || ', payment grp number: '    || curr_pmt_grp_num
1269              || ', payment method: '        || curr_pmt_method_cd
1270              || ', internal bank account: ' || curr_int_bank_acct_id
1271              || ', external bank account: ' || curr_ext_bank_acct_id
1272              || ', profile: '               || curr_profile_id
1273              || ', payee: '                 || curr_payee_id
1274              || ', payee site: '            || curr_payee_site_id
1275              || ', supplier site: '         || curr_supplier_site_id
1276              || ', remit loc id: '          || curr_remit_loc_id
1277              || ', ext payee: '             || curr_ext_payee_id
1278              || ', org: '                   || curr_org_id
1279              || ', org type: '              || curr_org_type
1280              );
1281 
1282          print_debuginfo(l_module_name,
1283              'Fetched document data for document:' || curr_doc_id
1284              || ', payment currency: '      || curr_doc_pmt_currency
1285              || ', payment amount: '        || curr_doc_pmt_amount
1286              || ', pay alone flag: '        || iby_disburse_submit_pub_pkg.docspayTab.exclusive_payment_flag(l_trx_line_index)
1287              || ', bank charge bearer: '    || curr_bnk_chg_bearer
1288              || ', delivery channel: '      || curr_delivery_channel
1289              || ', supplier message 1: '    || curr_supplier_msg1
1290              || ', supplier message 2: '    || curr_supplier_msg2
1291              || ', supplier message 3: '    || curr_supplier_msg3
1292              || ', uri: '                   || curr_uri
1293              || ', uri checkdigits: '       || curr_uri_checkdigits
1294              || ', pmt reason code: '       || curr_pmt_reason_code
1295              || ', pmt reason comments: '   || curr_pmt_reason_commts
1296              || ', pmt function: '          || curr_pmt_function
1297              || ', pmt date: '              || curr_pmt_date
1298              || ', pmt due date: '          || curr_pmt_due_date
1299              || ', benef party: '           || curr_benef_party
1300              );
1301 
1302          print_debuginfo(l_module_name,
1303              'Fetched document data for document:' || curr_doc_id
1304              || ', address source: '        || curr_address_source
1305              || ', emp addr code: '         || curr_emp_add_code
1306              || ', emp person id: '         || curr_emp_person_id
1307              || ', emp addr id: '           || curr_emp_addr_id
1308              || ', emp pmt flag: '          || curr_emp_pmt_flag
1309              );
1310 
1311          print_debuginfo(l_module_name,
1312              'Fetched flags for document:'  || curr_doc_id
1313              || ', pmt details flag: '      || l_pmt_details_flag
1314              || ', pmt details length: '    || l_pmt_details_len
1315              || ', pmt details formula: '   || l_pmt_det_formula
1316              || ', max docs flag: '         || l_max_docs_flag
1317              || ', max docs limit: '        || l_max_docs_limit
1318              || ', uri flag: '              || l_unique_remit_id_flag
1319              || ', pmt reason flag: '       || l_pmt_reason_flag
1320              || ', express pmt flag: '      || l_express_pmt_flag
1321              || ', delv channel flag: '     || l_delv_chnl_flag
1322              || ', pmt due date flag: '     || l_due_date_flag
1323              || ', prom note flag: '        || l_prom_note_flag
1324              );
1325           */
1326 
1327          IF (l_first_record = 'Y') THEN
1328             prev_doc_id            := iby_disburse_submit_pub_pkg.docspayTab.document_payable_id(l_trx_line_index);
1329             prev_ca_id             := iby_disburse_submit_pub_pkg.docspayTab.calling_app_id(l_trx_line_index);
1330             prev_ca_doc_id1        := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref1(l_trx_line_index);
1331             prev_ca_doc_id2        := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref2(l_trx_line_index);
1332             prev_ca_doc_id3        := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref3(l_trx_line_index);
1333             prev_ca_doc_id4        := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref4(l_trx_line_index);
1334             prev_ca_doc_id5        := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref5(l_trx_line_index);
1335             prev_pp_tt_cd          := iby_disburse_submit_pub_pkg.docspayTab.pay_proc_trxn_type_code(l_trx_line_index);
1336             prev_pmt_grp_num       := iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index);
1337             prev_pmt_method_cd     := iby_disburse_submit_pub_pkg.docspayTab.payment_method_code(l_trx_line_index);
1338             prev_int_bank_acct_id  := iby_disburse_submit_pub_pkg.docspayTab.internal_bank_account_id(l_trx_line_index);
1339             prev_ext_bank_acct_id  := iby_disburse_submit_pub_pkg.docspayTab.external_bank_account_id(l_trx_line_index);
1340             prev_profile_id        := iby_disburse_submit_pub_pkg.docspayTab.payment_profile_id(l_trx_line_index);
1341             prev_org_id            := iby_disburse_submit_pub_pkg.docspayTab.org_id(l_trx_line_index);
1342             prev_org_type          := iby_disburse_submit_pub_pkg.docspayTab.org_type(l_trx_line_index);
1343             prev_payee_id          := iby_disburse_submit_pub_pkg.docspayTab.payee_party_id(l_trx_line_index);
1344             prev_payee_site_id     := iby_disburse_submit_pub_pkg.docspayTab.party_site_id(l_trx_line_index);
1345             prev_remit_loc_id      := iby_disburse_submit_pub_pkg.docspayTab.remit_to_location_id(l_trx_line_index);
1346             prev_ext_payee_id      := iby_disburse_submit_pub_pkg.docspayTab.ext_payee_id(l_trx_line_index);
1347             prev_doc_pmt_currency  := iby_disburse_submit_pub_pkg.docspayTab.payment_currency_code(l_trx_line_index);
1348             prev_doc_pmt_amount    := iby_disburse_submit_pub_pkg.docspayTab.payment_amount(l_trx_line_index);
1349             prev_pay_alone_flag    := iby_disburse_submit_pub_pkg.docspayTab.exclusive_payment_flag(l_trx_line_index);
1350             prev_bnk_chg_bearer    := iby_disburse_submit_pub_pkg.docspayTab.bank_charge_bearer(l_trx_line_index);
1351             prev_delivery_channel  := iby_disburse_submit_pub_pkg.docspayTab.delivery_channel_code(l_trx_line_index);
1352             prev_settle_priority   := iby_disburse_submit_pub_pkg.docspayTab.settlement_priority(l_trx_line_index);
1353             prev_supplier_msg1     := iby_disburse_submit_pub_pkg.docspayTab.remittance_message1(l_trx_line_index);
1354             prev_supplier_msg2     := iby_disburse_submit_pub_pkg.docspayTab.remittance_message2(l_trx_line_index);
1355             prev_supplier_msg3     := iby_disburse_submit_pub_pkg.docspayTab.remittance_message3(l_trx_line_index);
1356             prev_uri               := iby_disburse_submit_pub_pkg.docspayTab.unique_remittance_identifier(l_trx_line_index);
1357             prev_uri_checkdigits   := iby_disburse_submit_pub_pkg.docspayTab.uri_check_digit(l_trx_line_index);
1358             prev_pmt_reason_code   := iby_disburse_submit_pub_pkg.docspayTab.payment_reason_code(l_trx_line_index);
1359             prev_pmt_reason_commts := iby_disburse_submit_pub_pkg.docspayTab.payment_reason_comments(l_trx_line_index);
1360             prev_pmt_function      := iby_disburse_submit_pub_pkg.docspayTab.payment_function(l_trx_line_index);
1361             prev_pmt_date          := iby_disburse_submit_pub_pkg.docspayTab.payment_date(l_trx_line_index);
1362             prev_pmt_due_date      := iby_disburse_submit_pub_pkg.docspayTab.payment_due_date(l_trx_line_index);
1363             prev_benef_party       := iby_disburse_submit_pub_pkg.docspayTab.beneficiary_party(l_trx_line_index);
1364             prev_address_source    := iby_disburse_submit_pub_pkg.docspayTab.address_source(l_trx_line_index);
1365             prev_emp_add_code      := iby_disburse_submit_pub_pkg.docspayTab.employee_address_code(l_trx_line_index);
1366             prev_emp_person_id     := iby_disburse_submit_pub_pkg.docspayTab.employee_person_id(l_trx_line_index);
1367             prev_emp_addr_id       := iby_disburse_submit_pub_pkg.docspayTab.employee_address_id(l_trx_line_index);
1368             prev_emp_pmt_flag      := iby_disburse_submit_pub_pkg.docspayTab.employee_payment_flag(l_trx_line_index);
1369  prev_inv_payee_party_id      := iby_disburse_submit_pub_pkg.docspayTab.inv_payee_party_id(l_trx_line_index);
1370  prev_inv_party_site_id      := iby_disburse_submit_pub_pkg.docspayTab.inv_party_site_id(l_trx_line_index);
1371  prev_inv_supplier_site_id      := iby_disburse_submit_pub_pkg.docspayTab.inv_supplier_site_id(l_trx_line_index);
1372  prev_inv_beneficiary_party      := iby_disburse_submit_pub_pkg.docspayTab.inv_beneficiary_party(l_trx_line_index);
1373  prev_ext_inv_payee_id      := iby_disburse_submit_pub_pkg.docspayTab.ext_inv_payee_id(l_trx_line_index);
1374 
1375          END IF;
1376 
1377          /*
1378           * We have just fetched a new document for this payment request.
1379           * We will either insert this document into a new payment or
1380           * we will be inserting this document into the currently running
1381           * payment.
1382           *
1383           * In either case, we need to insert this doc into a payment.
1384           * So pre-populate the payment record with attributes of
1385           * this document. This is because the payment takes on the
1386           * attributes of it's constituent documents.
1387           *
1388           * Note: For user defined grouping rules, we will
1389           * have to populate the payment attribute only if
1390           * the user has turned on grouping by that attribute.
1391           */
1392 
1393          /* Only pre-fill hardcoded grouping rule attributes */
1394          l_paymentRec.payment_profile_id       := iby_disburse_submit_pub_pkg.docspayTab.payment_profile_id(l_trx_line_index);
1395          l_paymentRec.payment_service_request_id
1396                                                := p_payment_request_id;
1397          l_paymentRec.internal_bank_account_id := iby_disburse_submit_pub_pkg.docspayTab.internal_bank_account_id(l_trx_line_index);
1398          l_paymentRec.ext_payee_id             := iby_disburse_submit_pub_pkg.docspayTab.ext_payee_id(l_trx_line_index);
1399          l_paymentRec.payee_party_id           := iby_disburse_submit_pub_pkg.docspayTab.payee_party_id(l_trx_line_index);
1400          l_paymentRec.party_site_id            := iby_disburse_submit_pub_pkg.docspayTab.party_site_id(l_trx_line_index);
1401          l_paymentRec.supplier_site_id         := iby_disburse_submit_pub_pkg.docspayTab.supplier_site_id(l_trx_line_index);
1402 
1403 	 l_paymentRec.ext_inv_payee_id             := iby_disburse_submit_pub_pkg.docspayTab.ext_inv_payee_id(l_trx_line_index);
1404          l_paymentRec.inv_payee_party_id           := iby_disburse_submit_pub_pkg.docspayTab.inv_payee_party_id(l_trx_line_index);
1405          l_paymentRec.inv_party_site_id            := iby_disburse_submit_pub_pkg.docspayTab.inv_party_site_id(l_trx_line_index);
1406          l_paymentRec.inv_supplier_site_id         := iby_disburse_submit_pub_pkg.docspayTab.inv_supplier_site_id(l_trx_line_index);
1407          l_paymentRec.inv_beneficiary_party         := iby_disburse_submit_pub_pkg.docspayTab.inv_beneficiary_party(l_trx_line_index);
1408 
1409          l_paymentRec.remit_to_location_id     := iby_disburse_submit_pub_pkg.docspayTab.remit_to_location_id(l_trx_line_index);
1410          l_paymentRec.payment_currency_code    := iby_disburse_submit_pub_pkg.docspayTab.payment_currency_code(l_trx_line_index);
1411          l_paymentRec.org_id                   := iby_disburse_submit_pub_pkg.docspayTab.org_id(l_trx_line_index);
1412          l_paymentRec.org_type                 := iby_disburse_submit_pub_pkg.docspayTab.org_type(l_trx_line_index);
1413          l_paymentRec.payment_method_code      := iby_disburse_submit_pub_pkg.docspayTab.payment_method_code(l_trx_line_index);
1414          l_paymentRec.payment_function         := iby_disburse_submit_pub_pkg.docspayTab.payment_function(l_trx_line_index);
1415          l_paymentRec.payment_date             := iby_disburse_submit_pub_pkg.docspayTab.payment_date(l_trx_line_index);
1416          l_paymentRec.beneficiary_party        := iby_disburse_submit_pub_pkg.docspayTab.beneficiary_party(l_trx_line_index);
1417          l_paymentRec.address_source           := iby_disburse_submit_pub_pkg.docspayTab.address_source(l_trx_line_index);
1418          l_paymentRec.employee_address_code    := iby_disburse_submit_pub_pkg.docspayTab.employee_address_code(l_trx_line_index);
1419          l_paymentRec.employee_person_id       := iby_disburse_submit_pub_pkg.docspayTab.employee_person_id(l_trx_line_index);
1420          l_paymentRec.employee_address_id      := iby_disburse_submit_pub_pkg.docspayTab.employee_address_id(l_trx_line_index);
1421          l_paymentRec.employee_payment_flag    := iby_disburse_submit_pub_pkg.docspayTab.employee_payment_flag(l_trx_line_index);
1422          l_paymentRec.legal_entity_id          := iby_disburse_submit_pub_pkg.docspayTab.iba_legal_entity_id(l_trx_line_index);
1423 
1424          /*
1425           * Fix for bug 5524116:
1426           *
1427           * Group by external bank account needs to be made a
1428           * hardcoded grouping rule regardless of processing type.
1429           *
1430           * This is because is some obscure formats e.g., Italy Wire
1431           * payments, the payment is actually sent to the bank as
1432           * a printed payment letter even though the payment method
1433           * is wire.
1434           */
1435          l_paymentRec.external_bank_account_id := iby_disburse_submit_pub_pkg.docspayTab.external_bank_account_id(l_trx_line_index);
1436 
1437          /*
1438           * Fix for bug 5334177:
1439           *
1440           * Setting the bills payable flag on the payment is not
1441           * related to user-defined grouping rules.
1442           *
1443           * Set this flag here along with other hardcoded
1444           * grouping rules.
1445           */
1446 
1447          /*
1448           * Set the promissory note flag for this payment
1449           * if the payment method of this payment supports
1450           * promissory notes.
1451           *
1452           * The promissory note flag indicates that this
1453           * payment is (possibly) a future dated payment.
1454           */
1455          IF (l_prom_note_flag = 'Y') THEN
1456 
1457              l_paymentRec.bill_payable_flag := 'Y';
1458 
1459          ELSE
1460 
1461              /*
1462               * Fix for bug 5442899
1463               *
1464               * Explicitly set bill payable flag to 'N'
1465               * in case payment method does not
1466               * support bills payable.
1467               */
1468              l_paymentRec.bill_payable_flag := 'N';
1469 
1470          END IF;
1471 
1472          /*
1473           * If a document has the payment grouping number attribute
1474           * set, then it is a part of a pre-grouped payment (document
1475           * grouped into payment at the calling app itself).
1476           *
1477           * Grouping by payment grouping number is a hardcoded grouping
1478           * rule below. Therefore, it is guaranteed that a payment will
1479           * only contain documents with the same payment grouping number.
1480           *
1481           * For, such payments set the 'pregrouped payment flag' to 'Y'.
1482           */
1483          IF (iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index) IS NOT NULL) THEN
1484 
1485              l_paymentRec.pregrouped_payment_flag := 'Y';
1486 
1487          ELSE
1488 
1489              /*
1490               * Fix for bug 5529924:
1491               *
1492               * Explicitly set the pregrouped payment flag to 'N'
1493               * in case the grouping number on the document
1494               * is null.
1495               */
1496              l_paymentRec.pregrouped_payment_flag := 'N';
1497 
1498          END IF;
1499 
1500          /* payee related fields */
1501          /* grouping by payee context is a hardcoded grouping rule */
1502          l_paymentRec.bank_instruction1_code   := l_bank_inst1_code;
1503          l_paymentRec.bank_instruction2_code   := l_bank_inst2_code;
1504          l_paymentRec.payment_text_message1    := l_pmt_txt_msg1;
1505          l_paymentRec.payment_text_message2    := l_pmt_txt_msg2;
1506          l_paymentRec.payment_text_message3    := l_pmt_txt_msg3;
1507 
1508          /*
1509           * Pre-fill grouping rule attributes for user defined
1510           * grouping rules (that are enabled by the user).
1511           *
1512           * It is necessary to pre-fill user defined grouping
1513           * attributes before the grouping rules are triggered
1514           * because we don't know which user defined grouping rules
1515           * are going to get triggered first, and once a rule is
1516           * triggered all rules below it are skipped. So it is too
1517           * late to populate grouping attributes within the grouping
1518           * rule itself.
1519           */
1520 
1521          /*
1522           * We pre-fill the user-defined attributes only when the
1523           * 'payment grouping number' attribute is null. This is
1524           * because the user-defined grouping rules are never triggered
1525           * if the 'payment grouping number' attribute is not null.
1526           */
1527 
1528          IF (iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index) IS NULL) THEN
1529 
1530              /*-----------------------------------------------------
1531              IF (l_payee_add_eft_flag = 'Y') THEN
1532                  IF (l_processing_type = 'ELECTRONIC') THEN
1533 
1534                      --
1535                      -- RAMESH:
1536                      -- Temporarily commented out. Current data model
1537                      -- does not support grouping by payee party id and payee
1538                      -- party site as separate grouping rules because of
1539                      -- calling app constraints.
1540                      --
1541                      -- At the moment, the grouping is at the level of
1542                      -- payee context which includes (payee party id, payee
1543                      -- party site id, supplier site id, org id, org type)
1544                      -- etc.
1545                      --
1546                      -- In the future when more fine grained grouping
1547                      -- can be supported, uncomment this code.
1548                      --
1549 
1550                      --l_paymentRec.payee_party_site_id := iby_disburse_submit_pub_pkg.docspayTab.party_site_id(l_trx_line_index);
1551                      --l_paymentRec.payee_id := iby_disburse_submit_pub_pkg.docspayTab.payee_party_id(l_trx_line_index);
1552 
1553                  END IF;
1554              END IF;
1555              -----------------------------------------------------*/
1556 
1557              IF (l_bnk_chg_brer_flag = 'Y') THEN
1558                  l_paymentRec.bank_charge_bearer := iby_disburse_submit_pub_pkg.docspayTab.bank_charge_bearer(l_trx_line_index);
1559              END IF;
1560 
1561              IF (l_delv_chnl_flag = 'Y') THEN
1562                  l_paymentRec.delivery_channel_code := iby_disburse_submit_pub_pkg.docspayTab.delivery_channel_code(l_trx_line_index);
1563              END IF;
1564 
1565              IF (l_express_pmt_flag = 'Y') THEN
1566                  l_paymentRec.settlement_priority := iby_disburse_submit_pub_pkg.docspayTab.settlement_priority(l_trx_line_index);
1567              END IF;
1568 
1569              IF (l_sup_msg_flag = 'Y') THEN
1570 
1571                  /*
1572                   * Fix for bug 5534537:
1573                   *
1574                   * Assign correct remittance messages to the
1575                   * payment.
1576                   */
1577                  l_paymentRec.remittance_message1 := iby_disburse_submit_pub_pkg.docspayTab.remittance_message1(l_trx_line_index);
1578                  l_paymentRec.remittance_message2 := iby_disburse_submit_pub_pkg.docspayTab.remittance_message2(l_trx_line_index);
1579                  l_paymentRec.remittance_message3 := iby_disburse_submit_pub_pkg.docspayTab.remittance_message3(l_trx_line_index);
1580 
1581              END IF;
1582 
1583              IF (l_unique_remit_id_flag = 'Y') THEN
1584                  l_paymentRec.unique_remittance_identifier := iby_disburse_submit_pub_pkg.docspayTab.unique_remittance_identifier(l_trx_line_index);
1585                  l_paymentRec.uri_check_digit := iby_disburse_submit_pub_pkg.docspayTab.uri_check_digit(l_trx_line_index);
1586              END IF;
1587 
1588              IF (l_due_date_flag = 'Y') THEN
1589                  l_paymentRec.payment_due_date := iby_disburse_submit_pub_pkg.docspayTab.payment_due_date(l_trx_line_index);
1590              END IF;
1591 
1592              IF (l_pmt_reason_flag = 'Y') THEN
1593                  l_paymentRec.payment_reason_code     := iby_disburse_submit_pub_pkg.docspayTab.payment_reason_code(l_trx_line_index);
1594                  l_paymentRec.payment_reason_comments := iby_disburse_submit_pub_pkg.docspayTab.payment_reason_comments(l_trx_line_index);
1595              END IF;
1596 
1597          END IF; -- if payment_grouping_number(l_trx_line_index) IS NULL
1598 
1599          /*
1600           * Pre-fill the document record with the details
1601           * of the current document.
1602           */
1603          l_docsInPmtRec.document_id         := iby_disburse_submit_pub_pkg.docspayTab.document_payable_id(l_trx_line_index);
1604          l_docsInPmtRec.document_amount     := iby_disburse_submit_pub_pkg.docspayTab.payment_amount(l_trx_line_index);
1605          l_docsInPmtRec.document_currency   := iby_disburse_submit_pub_pkg.docspayTab.payment_currency_code(l_trx_line_index);
1606          l_docsInPmtRec.payment_grp_num     := iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index);
1607          l_docsInPmtRec.calling_app_id      := iby_disburse_submit_pub_pkg.docspayTab.calling_app_id(l_trx_line_index);
1608          l_docsInPmtRec.calling_app_doc_id1 := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref1(l_trx_line_index);
1609          l_docsInPmtRec.calling_app_doc_id2 := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref2(l_trx_line_index);
1610          l_docsInPmtRec.calling_app_doc_id3 := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref3(l_trx_line_index);
1611          l_docsInPmtRec.calling_app_doc_id4 := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref4(l_trx_line_index);
1612          l_docsInPmtRec.calling_app_doc_id5 := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref5(l_trx_line_index);
1613          l_docsInPmtRec.pay_proc_ttype_cd   := iby_disburse_submit_pub_pkg.docspayTab.pay_proc_trxn_type_code(l_trx_line_index);
1614 
1615          /* these to are passed onto the hook for adjustment by ext app */
1616          l_docsInPmtRec.amount_withheld     := l_amount_withheld;
1617 
1618          /* used for maturity date calculation */
1619          l_docsInPmtRec.discount_date       := l_discount_date;
1620          l_docsInPmtRec.pmt_due_date        := iby_disburse_submit_pub_pkg.docspayTab.payment_due_date(l_trx_line_index);
1621 
1622          /* used in common attribute sweep */
1623          l_docsInPmtRec.delivery_channel    := iby_disburse_submit_pub_pkg.docspayTab.delivery_channel_code(l_trx_line_index);
1624          l_docsInPmtRec.unique_remit_id     := iby_disburse_submit_pub_pkg.docspayTab.unique_remittance_identifier(l_trx_line_index);
1625          l_docsInPmtRec.uri_ckdigits        := iby_disburse_submit_pub_pkg.docspayTab.uri_check_digit(l_trx_line_index);
1626 
1627          l_docsInPmtRec.int_bank_acct_id    := iby_disburse_submit_pub_pkg.docspayTab.internal_bank_account_id(l_trx_line_index);
1628          l_docsInPmtRec.ext_payee_id        := iby_disburse_submit_pub_pkg.docspayTab.ext_payee_id(l_trx_line_index);
1629          l_docsInPmtRec.payee_id            := iby_disburse_submit_pub_pkg.docspayTab.payee_party_id(l_trx_line_index);
1630          l_docsInPmtRec.payee_site_id       := iby_disburse_submit_pub_pkg.docspayTab.party_site_id(l_trx_line_index);
1631          l_docsInPmtRec.supplier_site_id    := iby_disburse_submit_pub_pkg.docspayTab.party_site_id(l_trx_line_index);
1632 
1633          l_docsInPmtRec.ext_inv_payee_id        := iby_disburse_submit_pub_pkg.docspayTab.ext_inv_payee_id(l_trx_line_index);
1634          l_docsInPmtRec.inv_payee_party_id            := iby_disburse_submit_pub_pkg.docspayTab.inv_payee_party_id(l_trx_line_index);
1635          l_docsInPmtRec.inv_party_site_id       := iby_disburse_submit_pub_pkg.docspayTab.inv_party_site_id(l_trx_line_index);
1636          l_docsInPmtRec.inv_supplier_site_id    := iby_disburse_submit_pub_pkg.docspayTab.inv_supplier_site_id(l_trx_line_index);
1637 
1638 	 l_docsInPmtRec.org_id              := iby_disburse_submit_pub_pkg.docspayTab.org_id(l_trx_line_index);
1639          l_docsInPmtRec.org_type            := iby_disburse_submit_pub_pkg.docspayTab.org_type(l_trx_line_index);
1640          l_docsInPmtRec.ext_bank_acct_id    := iby_disburse_submit_pub_pkg.docspayTab.external_bank_account_id(l_trx_line_index);
1641          l_docsInPmtRec.pmt_curr_discount   := l_discount_amount;
1642 
1643          /*
1644           * Calculate payment details from the document based on
1645           * the user defined formula. This will be used in a
1646           * user-defined grouping rule below.
1647           */
1648          IF (l_pmt_details_flag = 'Y') THEN
1649              l_calc_doc_detail := getDocDetails(iby_disburse_submit_pub_pkg.docspayTab.document_payable_id(l_trx_line_index),
1650                                       l_pmt_det_formula);
1651 
1652              print_debuginfo(l_module_name, 'Calculated doc detail for '
1653                  || 'doc id '
1654                  || curr_doc_id
1655                  || ': '
1656                  || l_calc_doc_detail
1657                  );
1658 
1659          ELSE
1660              l_calc_doc_detail := null;
1661          END IF;
1662 
1663          /*
1664           * The length of the payment details field in IBY_PAYMENTS_ALL
1665           * table is 240 chars. This imposes a hard limit for the
1666           * length of payment details that can be stored against a
1667           * payment.
1668           *
1669           * If the user has specified a field length (for payment
1670           * details) greater than the column width, reset the field
1671           * length to the column limit.
1672           */
1673          IF (l_pmt_details_flag = 'Y') THEN
1674              IF (l_pmt_details_len > 240) THEN
1675 
1676                  print_debuginfo(l_module_name, 'User specified payment '
1677                      || 'details field length = '
1678                      || l_pmt_details_len
1679                      || '. This is greater than the max column length '
1680                      || '(currently 240 chars). Payment details will be '
1681                      || 'filled to the maximum column length.'
1682                      );
1683 
1684                  l_pmt_details_len := 240;
1685 
1686              /*
1687               * If user has not specified any value at all,
1688               * default the payment details field length.
1689               */
1690              ELSIF (l_pmt_details_len = -1) THEN
1691 
1692                  /*
1693                   * Fix for bug 5362662:
1694                   *
1695                   * If user has not specified a length for the payment
1696                   * details field, but has turned on grouping by
1697                   * payment details, then limit the payment details
1698                   * field to the max allowed column length.
1699                   */
1700                  print_debuginfo(l_module_name, 'User specified payment '
1701                      || 'details field length is null. '
1702                      || 'Payment details will be filled to the maximum '
1703                      || 'column length (currently 240 chars).'
1704                      );
1705 
1706                  l_pmt_details_len := 240;
1707 
1708              END IF;
1709          END IF;
1710 
1711          /*-- HARDCODED GROUPING RULES START HERE --*/
1712 
1713          /*
1714           * Grouping Step 1: Pay Alone Flag (these are exclusive
1715           * payments where one document is paid by one payment).
1716           */
1717          IF (UPPER(iby_disburse_submit_pub_pkg.docspayTab.exclusive_payment_flag(l_trx_line_index)) = 'Y') THEN
1718 
1719              print_debuginfo(l_module_name, 'Grouping by pay alone flag '
1720                  || 'triggered for document '
1721                  || curr_doc_id);
1722 
1723              insertDocIntoPayment(l_paymentRec, x_paymentTab,
1724                  l_calc_doc_detail, true, l_payment_id,
1725                  x_docsInPmtTab, l_docsInPmtRec,
1726                  l_docs_in_pmt_count);
1727 
1728              GOTO label_finish_iteration;
1729 
1730          END IF;
1731 
1732          /*
1733           * Grouping Step 2: Organization ID And Organization Type
1734           */
1735          IF (prev_org_id <> iby_disburse_submit_pub_pkg.docspayTab.org_id(l_trx_line_index))     OR
1736             (NVL(prev_org_type, 0) <> NVL(iby_disburse_submit_pub_pkg.docspayTab.org_type(l_trx_line_index), 0)) THEN
1737 
1738              print_debuginfo(l_module_name, 'Grouping by organization '
1739                  || 'id/type triggered for document '
1740                  || curr_doc_id);
1741 
1742              insertDocIntoPayment(l_paymentRec, x_paymentTab,
1743                  l_calc_doc_detail, true, l_payment_id,
1744                  x_docsInPmtTab, l_docsInPmtRec,
1745                  l_docs_in_pmt_count);
1746 
1747              GOTO label_finish_iteration;
1748 
1749          END IF;
1750 
1751 	 /*
1752           * Grouping Step 2.5: Group By Inv Payee Context (external inv payee id)
1753           */
1754          IF (prev_ext_inv_payee_id <> iby_disburse_submit_pub_pkg.docspayTab.ext_inv_payee_id(l_trx_line_index)) THEN
1755 
1756              print_debuginfo(l_module_name, 'Grouping by payee context '
1757                  || 'triggered for document '
1758                  || curr_doc_id);
1759 
1760              insertDocIntoPayment(l_paymentRec, x_paymentTab,
1761                  l_calc_doc_detail, true, l_payment_id,
1762                  x_docsInPmtTab, l_docsInPmtRec,
1763                  l_docs_in_pmt_count);
1764 
1765              GOTO label_finish_iteration;
1766 
1767          END IF;
1768 
1769          /*
1770           * Grouping Step 3: Group By Payee Context (external payee id)
1771           */
1772          IF (prev_ext_payee_id <> iby_disburse_submit_pub_pkg.docspayTab.ext_payee_id(l_trx_line_index)) THEN
1773 
1774              print_debuginfo(l_module_name, 'Grouping by payee context '
1775                  || 'triggered for document '
1776                  || curr_doc_id);
1777 
1778              insertDocIntoPayment(l_paymentRec, x_paymentTab,
1779                  l_calc_doc_detail, true, l_payment_id,
1780                  x_docsInPmtTab, l_docsInPmtRec,
1781                  l_docs_in_pmt_count);
1782 
1783              GOTO label_finish_iteration;
1784 
1785          END IF;
1786 
1787          /*
1788           * Grouping Step 4: Payment Currency
1789           */
1790          IF (prev_doc_pmt_currency <> iby_disburse_submit_pub_pkg.docspayTab.payment_currency_code(l_trx_line_index)) THEN
1791 
1792              print_debuginfo(l_module_name, 'Grouping by payment '
1793                  || 'currency triggered for document '
1794                  || curr_doc_id);
1795 
1796              insertDocIntoPayment(l_paymentRec, x_paymentTab,
1797                  l_calc_doc_detail, true, l_payment_id,
1798                  x_docsInPmtTab, l_docsInPmtRec,
1799                  l_docs_in_pmt_count);
1800 
1801              GOTO label_finish_iteration;
1802 
1803          END IF;
1804 
1805          /*
1806           * Grouping Step 5: Payment Method
1807           */
1808          IF (prev_pmt_method_cd <> iby_disburse_submit_pub_pkg.docspayTab.payment_method_code(l_trx_line_index)) THEN
1809 
1810              print_debuginfo(l_module_name, 'Grouping by payment '
1811                  || 'method triggered for document '
1812                  || curr_doc_id);
1813 
1814              insertDocIntoPayment(l_paymentRec, x_paymentTab,
1815                  l_calc_doc_detail, true, l_payment_id,
1816                  x_docsInPmtTab, l_docsInPmtRec,
1817                  l_docs_in_pmt_count);
1818 
1819              GOTO label_finish_iteration;
1820 
1821          END IF;
1822 
1823          /*
1824           * Grouping Step 6: Payment Profile ID
1825           */
1826          IF (prev_profile_id <> iby_disburse_submit_pub_pkg.docspayTab.payment_profile_id(l_trx_line_index)) THEN
1827 
1828              print_debuginfo(l_module_name, 'Grouping by payment '
1829                  || 'profile triggered for document '
1830                  || curr_doc_id);
1831 
1832              insertDocIntoPayment(l_paymentRec, x_paymentTab,
1833                  l_calc_doc_detail, true, l_payment_id,
1834                  x_docsInPmtTab, l_docsInPmtRec,
1835                  l_docs_in_pmt_count);
1836 
1837              GOTO label_finish_iteration;
1838 
1839          END IF;
1840 
1841          /*
1842           * NOTE:
1843           * Grouping by payment grouping number is a special type
1844           * of hardcoded grouping rule. All documents payable that
1845           * have the same payment grouping number must always be
1846           * grouped into the same payment.
1847           *
1848           * In order to achive this, two things have to happen:
1849           * a. None of the hardcoded grouping rules should
1850           *    be triggered when a document payable has a non-null
1851           *    value for payment grouping number.
1852           *
1853           * b. None of the user defined grouping rules must be applied
1854           *    for a document payable with a non-null value for
1855           *    payment grouping number.
1856           *
1857           * It is guaranteed by the calling application that all
1858           * documents that have a non-null value for the payment
1859           * grouping number will have the same values for all the
1860           * hardcoded grouping attributes. This will satisfy (a).
1861           *
1862           * The grouping logic below will not apply any user-defined
1863           * grouping rules if a document payable has a non-null value
1864           * for the payment grouping number. This will satisfy (b).
1865           */
1866 
1867          /*
1868           * Grouping Step 7: Payment Grouping Number
1869           */
1870          IF (NVL(prev_pmt_grp_num, 0) <> NVL(iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index), 0)) THEN
1871 
1872              print_debuginfo(l_module_name, 'Grouping by payment '
1873                  || 'grouping number triggered for document '
1874                  || curr_doc_id);
1875 
1876              insertDocIntoPayment(l_paymentRec, x_paymentTab,
1877                  l_calc_doc_detail, true, l_payment_id,
1878                  x_docsInPmtTab, l_docsInPmtRec,
1879                  l_docs_in_pmt_count);
1880 
1881              GOTO label_finish_iteration;
1882 
1883          END IF;
1884 
1885          /*
1886           * Grouping Step 8: Internal Bank Account ID
1887           */
1888          IF (prev_int_bank_acct_id <> iby_disburse_submit_pub_pkg.docspayTab.internal_bank_account_id(l_trx_line_index)) THEN
1889 
1890              print_debuginfo(l_module_name, 'Grouping by internal bank '
1891                  || 'account triggered for document '
1892                  || curr_doc_id);
1893 
1894              insertDocIntoPayment(l_paymentRec, x_paymentTab,
1895                  l_calc_doc_detail, true, l_payment_id,
1896                  x_docsInPmtTab, l_docsInPmtRec,
1897                  l_docs_in_pmt_count);
1898 
1899              GOTO label_finish_iteration;
1900 
1901          END IF;
1902 
1903          /*
1904           * Grouping Step 9: External Bank Account ID
1905           *
1906           * Fix for bug 5524116:
1907           *
1908           * Grouping by ext bank account now made a hardcoded
1909           * grouping rule regardless of processing type.
1910           */
1911          IF (NVL(prev_ext_bank_acct_id, 0) <>
1912              NVL(iby_disburse_submit_pub_pkg.docspayTab.external_bank_account_id(l_trx_line_index), 0)) THEN
1913 
1914              print_debuginfo(l_module_name, 'Grouping by external '
1915                  || 'bank account triggered for document '
1916                  || curr_doc_id);
1917 
1918              l_paymentRec.external_bank_account_id := iby_disburse_submit_pub_pkg.docspayTab.external_bank_account_id(l_trx_line_index);
1919 
1920              insertDocIntoPayment(l_paymentRec, x_paymentTab,
1921                  l_calc_doc_detail, true, l_payment_id,
1922                  x_docsInPmtTab, l_docsInPmtRec,
1923                  l_docs_in_pmt_count);
1924 
1925              GOTO label_finish_iteration;
1926 
1927          END IF;
1928 
1929          /*
1930           * Grouping Step 10: Group By Payee Address (check only)
1931           *
1932           * One payee site can only have one address. Therefore, it is
1933           * sufficient to group by payee sites in order to group by
1934           * payee addresses.
1935           */
1936 
1937          /*-----------------------------------------------------
1938          IF (l_processing_type = 'PRINTED') THEN
1939              IF (prev_payee_site_id <> iby_disburse_submit_pub_pkg.docspayTab.party_site_id(l_trx_line_index)) THEN
1940 
1941                  print_debuginfo(l_module_name, 'Grouping by payee '
1942                      || 'address (check) triggered for document '
1943                      || curr_doc_id);
1944 
1945                  --
1946                  -- RAMESH:
1947                  -- Temporarily commented out. Current data model
1948                  -- does not support grouping by payee party id and payee
1949                  -- party site as separate grouping rules because of
1950                  -- calling app constraints.
1951                  --
1952                  -- At the moment, the grouping is at the level of
1953                  -- payee context which includes (payee party id, payee
1954                  -- party site id, supplier site id, org id, org type)
1955                  -- etc.
1956                  --
1957                  -- In the future when more fine grained grouping
1958                  -- can be supported, uncomment this code.
1959                  --
1960 
1961                  --l_paymentRec.payee_party_site_id := iby_disburse_submit_pub_pkg.docspayTab.party_site_id(l_trx_line_index);
1962 
1963                  insertDocIntoPayment(l_paymentRec, x_paymentTab,
1964                      l_calc_doc_detail, true, l_payment_id,
1965                      x_docsInPmtTab, l_docsInPmtRec,
1966                      l_docs_in_pmt_count);
1967 
1968                  GOTO label_finish_iteration;
1969              END IF;
1970          END IF;
1971          -----------------------------------------------------*/
1972 
1973          /*
1974           * Grouping Step 11: Payment Function
1975           */
1976          IF (prev_pmt_function <> iby_disburse_submit_pub_pkg.docspayTab.payment_function(l_trx_line_index)) THEN
1977 
1978              print_debuginfo(l_module_name, 'Grouping by payment '
1979                  || 'function triggered for document '
1980                  || curr_doc_id);
1981 
1982              insertDocIntoPayment(l_paymentRec, x_paymentTab,
1983                  l_calc_doc_detail, true, l_payment_id,
1984                  x_docsInPmtTab, l_docsInPmtRec,
1985                  l_docs_in_pmt_count);
1986 
1987              GOTO label_finish_iteration;
1988 
1989          END IF;
1990 
1991          /*
1992           * Grouping Step 12: Payment Date
1993           */
1994          IF (prev_pmt_date <> iby_disburse_submit_pub_pkg.docspayTab.payment_date(l_trx_line_index)) THEN
1995 
1996              print_debuginfo(l_module_name, 'Grouping by payment '
1997                  || 'date triggered for document '
1998                  || curr_doc_id);
1999 
2000              insertDocIntoPayment(l_paymentRec, x_paymentTab,
2001                  l_calc_doc_detail, true, l_payment_id,
2002                  x_docsInPmtTab, l_docsInPmtRec,
2003                  l_docs_in_pmt_count);
2004 
2005              GOTO label_finish_iteration;
2006 
2007          END IF;
2008 
2009          /*
2010           * Grouping Step 13: Remit To Location Id
2011           */
2012          IF (NVL(prev_remit_loc_id, '0') <> NVL(iby_disburse_submit_pub_pkg.docspayTab.remit_to_location_id(l_trx_line_index), '0')) THEN
2013 
2014              print_debuginfo(l_module_name, 'Grouping by remit to '
2015                  || 'location id triggered for document '
2016                  || curr_doc_id);
2017 
2018              insertDocIntoPayment(l_paymentRec, x_paymentTab,
2019                  l_calc_doc_detail, true, l_payment_id,
2020                  x_docsInPmtTab, l_docsInPmtRec,
2021                  l_docs_in_pmt_count);
2022 
2023              GOTO label_finish_iteration;
2024 
2025          END IF;
2026 
2027          /*
2028           * Grouping Step 14: Beneficiary Party
2029           */
2030          IF (NVL(prev_benef_party, '0') <> NVL(iby_disburse_submit_pub_pkg.docspayTab.beneficiary_party(l_trx_line_index), '0')) THEN
2031 
2032              print_debuginfo(l_module_name, 'Grouping by beneficiary '
2033                  || 'party triggered for document '
2034                  || curr_doc_id);
2035 
2036              insertDocIntoPayment(l_paymentRec, x_paymentTab,
2037                  l_calc_doc_detail, true, l_payment_id,
2038                  x_docsInPmtTab, l_docsInPmtRec,
2039                  l_docs_in_pmt_count);
2040 
2041              GOTO label_finish_iteration;
2042 
2043          END IF;
2044 
2045          /*
2046           * Grouping Step 15: Group By Space Left On Stub (check only)
2047           *
2048           * This will be handled in the payment creation flow (F8) because
2049           * the check related details are picked up in that flow.
2050           */
2051 
2052          /*
2053           * Grouping Step 16: Payee Address (Employee Address)
2054           *
2055           * TECHNICALLY REQUIRED, FUNCTIONALLY NOT REQUIRED -
2056           * Grouping by payee address is really not necessary because
2057           * we do grouping by payee context (which implicitly groups
2058           * by payee address as well assuming a 1:1 relationship between
2059           * a payee and his address).
2060           *
2061           * The reason we have this grouping rule here is to populate
2062           * these columns on the payment. These columns are used by
2063           * the extract / format.
2064           */
2065          IF (NVL(prev_address_source, '0') <> NVL(iby_disburse_submit_pub_pkg.docspayTab.address_source(l_trx_line_index), '0') OR
2066              NVL(prev_emp_add_code,   '0') <> NVL(iby_disburse_submit_pub_pkg.docspayTab.employee_address_code(l_trx_line_index),   '0') OR
2067              NVL(prev_emp_person_id,  '0') <> NVL(iby_disburse_submit_pub_pkg.docspayTab.employee_person_id(l_trx_line_index),  '0') OR
2068              NVL(prev_emp_addr_id,    '0') <> NVL(iby_disburse_submit_pub_pkg.docspayTab.employee_address_id(l_trx_line_index),    '0'))
2069              THEN
2070 
2071              print_debuginfo(l_module_name, 'Grouping by payee (employee) '
2072                  || 'address triggered for document '
2073                  || curr_doc_id);
2074 
2075              insertDocIntoPayment(l_paymentRec, x_paymentTab,
2076                  l_calc_doc_detail, true, l_payment_id,
2077                  x_docsInPmtTab, l_docsInPmtRec,
2078                  l_docs_in_pmt_count);
2079 
2080              GOTO label_finish_iteration;
2081 
2082          END IF;
2083 
2084          /*
2085           * Grouping Step 17: Employee Payment Flag
2086           *
2087           * TECHINICALLY REQUIRED, FUNCTIONALLY NOT REQUIRED -
2088           * The reason we have this grouping rule here is to populate
2089           * the employee payment flag on the payment.
2090           */
2091          IF (NVL(prev_emp_pmt_flag, '0') <> NVL(iby_disburse_submit_pub_pkg.docspayTab.employee_payment_flag(l_trx_line_index), '0'))
2092              THEN
2093 
2094              print_debuginfo(l_module_name, 'Grouping by employee '
2095                  || 'payment flag triggered for document '
2096                  || curr_doc_id);
2097 
2098              insertDocIntoPayment(l_paymentRec, x_paymentTab,
2099                  l_calc_doc_detail, true, l_payment_id,
2100                  x_docsInPmtTab, l_docsInPmtRec,
2101                  l_docs_in_pmt_count);
2102 
2103              GOTO label_finish_iteration;
2104 
2105          END IF;
2106 
2107          /*-- USER DEFINED GROUPING RULES START HERE --*/
2108 
2109          /*
2110           * NOTE:
2111           * None of the user defined grouping rules should be
2112           * applied to a document if it has a payment grouping
2113           * number attribute set.
2114           *
2115           * This is because all documents with the same payment
2116           * grouping number must always be grouped into the
2117           * same payment.
2118           */
2119 
2120          /*
2121           * Grouping Step 18: Payee Address (electronic)
2122           *
2123           * These are payments where the payment between
2124           * Oracle Payments and the bank is done electronically,
2125           * but the delivery channel from the bank to the
2126           * payee is by check.
2127           */
2128 
2129          /*-----------------------------------------
2130          IF (l_payee_add_eft_flag = 'Y') THEN
2131              IF (l_processing_type = 'ELECTRONIC') THEN
2132 
2133                  IF (NVL(prev_payee_site_id, 0) <> NVL(iby_disburse_submit_pub_pkg.docspayTab.party_site_id(l_trx_line_index), 0))
2134                      THEN
2135 
2136                      print_debuginfo(l_module_name, 'Grouping by payee '
2137                          || 'address (electronic) triggered for document '
2138                          || curr_doc_id);
2139 
2140                      --
2141                      -- RAMESH:
2142                      -- Temporarily commented out. Current data model
2143                      -- does not support grouping by payee party id and payee
2144                      -- party site as separate grouping rules because of
2145                      -- calling app constraints.
2146                      --
2147                      -- At the moment, the grouping is at the level of
2148                      -- payee context which includes (payee party id, payee
2149                      -- party site id, supplier site id, org id, org type)
2150                      -- etc.
2151                      --
2152                      -- In the future when more fine grained grouping
2153                      -- can be supported, uncomment this code.
2154                      --
2155 
2156                      insertDocIntoPayment(l_paymentRec, x_paymentTab,
2157                          l_calc_doc_detail, true, l_payment_id,
2158                          x_docsInPmtTab, l_docsInPmtRec,
2159                          l_docs_in_pmt_count);
2160 
2161                      GOTO label_finish_iteration;
2162                  END IF;
2163              END IF;
2164          END IF;
2165          -------------------------------------------*/
2166 
2167          /*
2168           * Grouping Step 19: Bank Charge Bearer
2169           */
2170          IF (iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index) IS NULL) THEN
2171 
2172              IF (l_bnk_chg_brer_flag = 'Y') THEN
2173 
2174                  IF (NVL(prev_bnk_chg_bearer, 0) <>
2175                      NVL(iby_disburse_submit_pub_pkg.docspayTab.bank_charge_bearer(l_trx_line_index), 0)) THEN
2176 
2177                      print_debuginfo(l_module_name, 'Grouping by bank '
2178                          || 'charge bearer triggered for document '
2179                          || curr_doc_id);
2180 
2181                      insertDocIntoPayment(l_paymentRec, x_paymentTab,
2182                          l_calc_doc_detail, true, l_payment_id,
2183                          x_docsInPmtTab, l_docsInPmtRec,
2184                          l_docs_in_pmt_count);
2185 
2186                      GOTO label_finish_iteration;
2187 
2188                  END IF;
2189 
2190              END IF;
2191 
2192          END IF;
2193 
2194          /*
2195           * Grouping Step 20: Delivery Channel
2196           */
2197          IF (iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index) IS NULL) THEN
2198 
2199              IF (l_delv_chnl_flag = 'Y') THEN
2200 
2201                  IF (NVL(prev_delivery_channel, 0) <>
2202                      NVL(iby_disburse_submit_pub_pkg.docspayTab.delivery_channel_code(l_trx_line_index), 0)) THEN
2203 
2204                      print_debuginfo(l_module_name, 'Grouping by '
2205                          || 'delivery channel triggered for document '
2206                          || curr_doc_id);
2207 
2208                      insertDocIntoPayment(l_paymentRec, x_paymentTab,
2209                          l_calc_doc_detail, true, l_payment_id,
2210                          x_docsInPmtTab, l_docsInPmtRec,
2211                          l_docs_in_pmt_count);
2212 
2213                      GOTO label_finish_iteration;
2214 
2215                  END IF;
2216 
2217              END IF;
2218 
2219          END IF;
2220 
2221          /*
2222           * Grouping Step 21: Express Payment
2223           */
2224          IF (iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index) IS NULL) THEN
2225 
2226              IF (l_express_pmt_flag = 'Y') THEN
2227 
2228                  IF (NVL(prev_settle_priority, 0) <>
2229                      NVL(iby_disburse_submit_pub_pkg.docspayTab.settlement_priority(l_trx_line_index), 0)) THEN
2230 
2231                      print_debuginfo(l_module_name, 'Grouping by '
2232                          || 'express payment triggered for document '
2233                          || curr_doc_id);
2234 
2235                      insertDocIntoPayment(l_paymentRec, x_paymentTab,
2236                          l_calc_doc_detail, true, l_payment_id,
2237                          x_docsInPmtTab, l_docsInPmtRec,
2238                          l_docs_in_pmt_count);
2239 
2240                      GOTO label_finish_iteration;
2241 
2242                  END IF;
2243 
2244              END IF;
2245 
2246          END IF;
2247 
2248          /*
2249           * Grouping Step 22: Supplier Message
2250           */
2251          IF (iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index) IS NULL) THEN
2252 
2253              IF (l_sup_msg_flag = 'Y') THEN
2254 
2255                  IF (NVL(prev_supplier_msg1, 0) <>
2256                          NVL(iby_disburse_submit_pub_pkg.docspayTab.remittance_message1(l_trx_line_index), 0) OR
2257                      NVL(prev_supplier_msg2, 0) <>
2258                          NVL(iby_disburse_submit_pub_pkg.docspayTab.remittance_message2(l_trx_line_index), 0) OR
2259                      NVL(prev_supplier_msg3, 0) <>
2260                          NVL(iby_disburse_submit_pub_pkg.docspayTab.remittance_message3(l_trx_line_index), 0)) THEN
2261 
2262                      print_debuginfo(l_module_name, 'Grouping by '
2263                          || 'supplier message triggered for document '
2264                          || curr_doc_id);
2265 
2266                      insertDocIntoPayment(l_paymentRec, x_paymentTab,
2267                          l_calc_doc_detail, true, l_payment_id,
2268                          x_docsInPmtTab, l_docsInPmtRec,
2269                          l_docs_in_pmt_count);
2270 
2271                      GOTO label_finish_iteration;
2272 
2273                  END IF;
2274 
2275              END IF;
2276 
2277          END IF;
2278 
2279          /*
2280           * Grouping Step 23: Unique Remittance Identifier
2281           *
2282           * Note: It is sufficient to check if the unique remittance
2283           * identifiers (uri's) are different between two documents.
2284           * No need to check if the uri checkdigits are different.
2285           * Grouping is on the basis of uri's only.
2286           */
2287          IF (iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index) IS NULL) THEN
2288 
2289              IF (l_unique_remit_id_flag = 'Y') THEN
2290 
2291                  IF (NVL(prev_uri, 0) <> NVL(iby_disburse_submit_pub_pkg.docspayTab.unique_remittance_identifier(l_trx_line_index), 0) OR
2292                      NVL(prev_uri_checkdigits, 0) <>
2293                          NVL(iby_disburse_submit_pub_pkg.docspayTab.uri_check_digit(l_trx_line_index), 0)) THEN
2294 
2295                      print_debuginfo(l_module_name, 'Grouping by '
2296                          || 'unique remit id triggered for document '
2297                          || curr_doc_id);
2298 
2299                      insertDocIntoPayment(l_paymentRec, x_paymentTab,
2300                          l_calc_doc_detail, true, l_payment_id,
2301                          x_docsInPmtTab, l_docsInPmtRec,
2302                          l_docs_in_pmt_count);
2303 
2304                      GOTO label_finish_iteration;
2305 
2306                  END IF;
2307 
2308              END IF;
2309 
2310          END IF;
2311 
2312          /*
2313           * Grouping Step 24: Payment Reason Code / Payment Reason Comments
2314           */
2315          IF (iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index) IS NULL) THEN
2316 
2317              IF (l_pmt_reason_flag = 'Y') THEN
2318 
2319                  IF (NVL(prev_pmt_reason_code, 0)   <>
2320                          NVL(iby_disburse_submit_pub_pkg.docspayTab.payment_reason_code(l_trx_line_index), 0)  OR
2321                      NVL(prev_pmt_reason_commts, 0) <>
2322                          NVL(iby_disburse_submit_pub_pkg.docspayTab.payment_reason_comments(l_trx_line_index), 0)) THEN
2323 
2324                      print_debuginfo(l_module_name, 'Grouping by '
2325                          || 'payment reason code / comments triggered '
2326                          || 'for document '
2327                          || curr_doc_id);
2328 
2329                      insertDocIntoPayment(l_paymentRec, x_paymentTab,
2330                          l_calc_doc_detail, true, l_payment_id,
2331                          x_docsInPmtTab, l_docsInPmtRec,
2332                          l_docs_in_pmt_count);
2333 
2334                      GOTO label_finish_iteration;
2335 
2336                  END IF;
2337 
2338              END IF;
2339 
2340          END IF;
2341 
2342          /*
2343           * Grouping Step 25: Payment Due Date
2344           */
2345          IF (iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index) IS NULL) THEN
2346 
2347              IF (l_due_date_flag = 'Y') THEN
2348 
2349                  IF (NVL(prev_pmt_due_date, l_impossible_date) <>
2350                      NVL(iby_disburse_submit_pub_pkg.docspayTab.payment_due_date(l_trx_line_index), l_impossible_date)) THEN
2351 
2352                      print_debuginfo(l_module_name, 'Grouping by '
2353                          || 'payment due date triggered for document '
2354                          || curr_doc_id);
2355 
2356                      insertDocIntoPayment(l_paymentRec, x_paymentTab,
2357                          l_calc_doc_detail, true, l_payment_id,
2358                          x_docsInPmtTab, l_docsInPmtRec,
2359                          l_docs_in_pmt_count);
2360 
2361                      GOTO label_finish_iteration;
2362 
2363                  END IF;
2364 
2365              END IF;
2366 
2367          END IF;
2368 
2369          /*
2370           * Grouping Step 26: Max Documents Per Payment
2371           */
2372          IF (iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index) IS NULL) THEN
2373 
2374              IF (l_max_docs_flag = 'Y') THEN
2375 
2376                  IF (l_docs_in_pmt_count = l_max_docs_limit) THEN
2377 
2378                      print_debuginfo(l_module_name, 'Grouping by '
2379                          || 'max docs per payment triggered for document '
2380                          || curr_doc_id);
2381 
2382                      insertDocIntoPayment(l_paymentRec, x_paymentTab,
2383                          l_calc_doc_detail, true, l_payment_id,
2384                          x_docsInPmtTab, l_docsInPmtRec,
2385                          l_docs_in_pmt_count);
2386 
2387                      GOTO label_finish_iteration;
2388 
2389                  END IF;
2390 
2391              END IF;
2392 
2393          END IF;
2394 
2395          /*
2396           * Grouping Step 27: Break Payments According To Payment Details
2397           *                   Field Length
2398           */
2399          IF (iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index) IS NULL) THEN
2400 
2401              IF (l_pmt_details_flag = 'Y') THEN
2402 
2403                  IF (LENGTH(l_paymentRec.payment_details || l_calc_doc_detail)
2404                      > l_pmt_details_len) THEN
2405 
2406                      print_debuginfo(l_module_name, 'Grouping by '
2407                          || 'payment details length triggered for document '
2408                          || curr_doc_id);
2409 
2410                      insertDocIntoPayment(l_paymentRec, x_paymentTab,
2411                          l_calc_doc_detail, true, l_payment_id,
2412                          x_docsInPmtTab, l_docsInPmtRec,
2413                          l_docs_in_pmt_count);
2414 
2415                      GOTO label_finish_iteration;
2416 
2417                  END IF;
2418 
2419              END IF;
2420 
2421          END IF;
2422 
2423          /*
2424           * End Of Grouping:
2425           * If a document reaches here, it means that this document
2426           * is similar to the previous document as far a grouping
2427           * criteria is concerned.
2428           *
2429           * Add this document to the currently running payment.
2430           */
2431          print_debuginfo(l_module_name, 'No grouping rules '
2432              || 'were triggered for document '
2433              || curr_doc_id);
2434 
2435          insertDocIntoPayment(l_paymentRec, x_paymentTab,
2436              l_calc_doc_detail, false, l_payment_id,
2437              x_docsInPmtTab, l_docsInPmtRec,
2438              l_docs_in_pmt_count);
2439 
2440          <<label_finish_iteration>>
2441 
2442          /*
2443           * CBR Handling:
2444           *
2445           * For each payment, store the central bank reporting
2446           * related fields along with the payment id in the
2447           * CBR table.
2448           *
2449           * This table will be passed as an output param to
2450           * the routine that sets the 'declare payment flag'
2451           * on the payment based on the CBR fields.
2452           *
2453           * We just created a payment. If this payment
2454           * does not already exist in the CBR list, add
2455           * it to the list.
2456           */
2457           l_profile_id := iby_disburse_submit_pub_pkg.docspayTab.payment_profile_id(l_trx_line_index);
2458 /*  Bug 5709596 */
2459           IF NOT(iby_disburse_submit_pub_pkg.paymentProfilesTab.exists(l_profile_id)) THEN
2460              iby_disburse_submit_pub_pkg.set_profile_attribs(l_profile_id);
2461           END IF;
2462 
2463          /*
2464           * Lastly, before going into the next iteration
2465           * of the loop copy all the current grouping criteria
2466           * into 'prev' fields so that we can compare these
2467           * fields with the next record.
2468           *
2469           * No need to copy the current values into the previous ones for
2470           * the first record because we have already done it at the beginning.
2471           *
2472           *
2473           * Fix for bug 5463733:
2474           *
2475           * Reset the prev pmt grouping number here, else
2476           * the next document may incorrectly be grouped
2477           * into another payment.
2478           */
2479 
2480          IF (l_first_record <> 'Y') THEN
2481             prev_doc_id            := iby_disburse_submit_pub_pkg.docspayTab.document_payable_id(l_trx_line_index);
2482             prev_ca_id             := iby_disburse_submit_pub_pkg.docspayTab.calling_app_id(l_trx_line_index);
2483             prev_ca_doc_id1        := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref1(l_trx_line_index);
2484             prev_ca_doc_id2        := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref2(l_trx_line_index);
2485             prev_ca_doc_id3        := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref3(l_trx_line_index);
2486             prev_ca_doc_id4        := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref4(l_trx_line_index);
2487             prev_ca_doc_id5        := iby_disburse_submit_pub_pkg.docspayTab.calling_app_doc_unique_ref5(l_trx_line_index);
2488             prev_pp_tt_cd          := iby_disburse_submit_pub_pkg.docspayTab.pay_proc_trxn_type_code(l_trx_line_index);
2489             prev_pmt_grp_num       := iby_disburse_submit_pub_pkg.docspayTab.payment_grouping_number(l_trx_line_index);
2490             prev_pmt_method_cd     := iby_disburse_submit_pub_pkg.docspayTab.payment_method_code(l_trx_line_index);
2491             prev_int_bank_acct_id  := iby_disburse_submit_pub_pkg.docspayTab.internal_bank_account_id(l_trx_line_index);
2492             prev_ext_bank_acct_id  := iby_disburse_submit_pub_pkg.docspayTab.external_bank_account_id(l_trx_line_index);
2493             prev_profile_id        := iby_disburse_submit_pub_pkg.docspayTab.payment_profile_id(l_trx_line_index);
2494             prev_org_id            := iby_disburse_submit_pub_pkg.docspayTab.org_id(l_trx_line_index);
2495             prev_org_type          := iby_disburse_submit_pub_pkg.docspayTab.org_type(l_trx_line_index);
2496             prev_payee_id          := iby_disburse_submit_pub_pkg.docspayTab.payee_party_id(l_trx_line_index);
2497             prev_payee_site_id     := iby_disburse_submit_pub_pkg.docspayTab.party_site_id(l_trx_line_index);
2498             prev_remit_loc_id      := iby_disburse_submit_pub_pkg.docspayTab.remit_to_location_id(l_trx_line_index);
2499             prev_ext_payee_id      := iby_disburse_submit_pub_pkg.docspayTab.ext_payee_id(l_trx_line_index);
2500             prev_doc_pmt_currency  := iby_disburse_submit_pub_pkg.docspayTab.payment_currency_code(l_trx_line_index);
2501             prev_doc_pmt_amount    := iby_disburse_submit_pub_pkg.docspayTab.payment_amount(l_trx_line_index);
2502             prev_pay_alone_flag    := iby_disburse_submit_pub_pkg.docspayTab.exclusive_payment_flag(l_trx_line_index);
2503             prev_bnk_chg_bearer    := iby_disburse_submit_pub_pkg.docspayTab.bank_charge_bearer(l_trx_line_index);
2504             prev_delivery_channel  := iby_disburse_submit_pub_pkg.docspayTab.delivery_channel_code(l_trx_line_index);
2505             prev_settle_priority   := iby_disburse_submit_pub_pkg.docspayTab.settlement_priority(l_trx_line_index);
2506             prev_supplier_msg1     := iby_disburse_submit_pub_pkg.docspayTab.remittance_message1(l_trx_line_index);
2507             prev_supplier_msg2     := iby_disburse_submit_pub_pkg.docspayTab.remittance_message2(l_trx_line_index);
2508             prev_supplier_msg3     := iby_disburse_submit_pub_pkg.docspayTab.remittance_message3(l_trx_line_index);
2509             prev_uri               := iby_disburse_submit_pub_pkg.docspayTab.unique_remittance_identifier(l_trx_line_index);
2510             prev_uri_checkdigits   := iby_disburse_submit_pub_pkg.docspayTab.uri_check_digit(l_trx_line_index);
2511             prev_pmt_reason_code   := iby_disburse_submit_pub_pkg.docspayTab.payment_reason_code(l_trx_line_index);
2512             prev_pmt_reason_commts := iby_disburse_submit_pub_pkg.docspayTab.payment_reason_comments(l_trx_line_index);
2513             prev_pmt_function      := iby_disburse_submit_pub_pkg.docspayTab.payment_function(l_trx_line_index);
2514             prev_pmt_date          := iby_disburse_submit_pub_pkg.docspayTab.payment_date(l_trx_line_index);
2515             prev_pmt_due_date      := iby_disburse_submit_pub_pkg.docspayTab.payment_due_date(l_trx_line_index);
2516             prev_benef_party       := iby_disburse_submit_pub_pkg.docspayTab.beneficiary_party(l_trx_line_index);
2517             prev_address_source    := iby_disburse_submit_pub_pkg.docspayTab.address_source(l_trx_line_index);
2518             prev_emp_add_code      := iby_disburse_submit_pub_pkg.docspayTab.employee_address_code(l_trx_line_index);
2519             prev_emp_person_id     := iby_disburse_submit_pub_pkg.docspayTab.employee_person_id(l_trx_line_index);
2520             prev_emp_addr_id       := iby_disburse_submit_pub_pkg.docspayTab.employee_address_id(l_trx_line_index);
2521             prev_emp_pmt_flag      := iby_disburse_submit_pub_pkg.docspayTab.employee_payment_flag(l_trx_line_index);
2522 
2523             prev_inv_payee_party_id          := iby_disburse_submit_pub_pkg.docspayTab.inv_payee_party_id(l_trx_line_index);
2524             prev_inv_party_site_id     := iby_disburse_submit_pub_pkg.docspayTab.inv_party_site_id(l_trx_line_index);
2525             prev_inv_beneficiary_party      := iby_disburse_submit_pub_pkg.docspayTab.inv_beneficiary_party(l_trx_line_index);
2526             prev_ext_inv_payee_id      := iby_disburse_submit_pub_pkg.docspayTab.ext_inv_payee_id(l_trx_line_index);
2527 
2528          END IF;
2529 
2530          /*
2531           *  Remember to reset the first record flag before going
2532           *  into the next iteration.
2533           */
2534          IF (l_first_record = 'Y') THEN
2535              l_first_record := 'N';
2536          END IF;
2537 
2538          print_debuginfo(l_module_name, '+----------------------------------+');
2539 
2540         END LOOP; -- for documents cursor
2541 
2542         EXIT WHEN c_documents%NOTFOUND;
2543 
2544      END LOOP; -- for documents cursor
2545 
2546      iby_disburse_submit_pub_pkg.delete_docspayTab;
2547 
2548      CLOSE c_documents;
2549 
2550      /*
2551       * Exit if no documents were found.
2552       */
2553      IF (l_no_rec_in_ppr) THEN
2554          print_debuginfo(l_module_name, 'No documents payable were '
2555              || 'retrieved from DB for payment request '
2556              || p_payment_request_id
2557              || '. Exiting document grouping ..');
2558 
2559          print_debuginfo(l_module_name, 'EXIT');
2560          RETURN;
2561      END IF;
2562 
2563 
2564      /*
2565       * Populate legal entity ids for each of the created payments.
2566       *
2567       * The legal entity id on the payment is a special attribute.
2568       * It is not a grouping rule, instead it has to be derived
2569       * from the internal bank account and populated.
2570       */
2571 
2572      print_debuginfo(l_module_name, 'Created '
2573          || x_paymentTab.COUNT   || ' payment(s) from '
2574          || x_docsInPmtTab.COUNT || ' document(s) for payment request '
2575          || p_payment_request_id || '.');
2576 
2577      /*
2578       * Finally, return the payments created by grouping to the caller.
2579       */
2580      x_ca_id        := l_ca_id;
2581      x_ca_payreq_cd := l_ca_payreq_cd;
2582 
2583      print_debuginfo(l_module_name, 'EXIT');
2584 
2585  END performDocumentGrouping;
2586 
2587 /*--------------------------------------------------------------------
2588  | NAME:
2589  |     insertDocIntoPayment
2590  |
2591  | PURPOSE:
2592  |     Inserts a given document into a currently running payment
2593  |     or into a new payment as per given flag.
2594  |
2595  |     This method is called by every grouping rule to add
2596  |     a given document into a current payment/new payment.
2597  |
2598  | PARAMETERS:
2599  |     IN
2600  |
2601  |
2602  |     OUT
2603  |
2604  |
2605  | RETURNS:
2606  |
2607  | NOTES:
2608  |
2609  *---------------------------------------------------------------------*/
2610  PROCEDURE insertDocIntoPayment(
2611      x_paymentRec            IN OUT NOCOPY IBY_PAYMENTS_ALL%ROWTYPE,
2612      x_paymentTab            IN OUT NOCOPY paymentTabType,
2613      p_calcDocInfo           IN            VARCHAR2,
2614      p_newPaymentFlag        IN            BOOLEAN,
2615      x_currentPaymentId      IN OUT NOCOPY IBY_PAYMENTS_ALL.payment_id%TYPE,
2616      x_docsInPmtTab          IN OUT NOCOPY docsInPaymentTabType,
2617      x_docsInPmtRec          IN OUT NOCOPY docsInPaymentRecType,
2618      x_docsInPmtCount        IN OUT NOCOPY NUMBER
2619      )
2620  IS
2621  l_module_name    CONSTANT VARCHAR2(200) := G_PKG_NAME ||
2622                                                 '.insertDocIntoPayment';
2623 
2624  BEGIN
2625 
2626      print_debuginfo(l_module_name, 'ENTER');
2627 
2628      /*
2629       * GROUPING LOGIC IS IN IF-ELSE BLOCK BELOW:
2630       *
2631       * Irrespective of whether this document is part of
2632       * an existing payment or whether it should be part
2633       * of a new payment, ensure that the PLSQL payments
2634       * table is updated with the details of this document
2635       * within this if-else block.
2636       *
2637       * We need to do this each time we enter this procedure
2638       * because this might well be the last document in
2639       * in the payment request, and this procedure may
2640       * not be called again for this payment request. So
2641       * the PLSQL payments table should always be up-to-date
2642       * when it exits this procedure.
2643       */
2644      IF (p_newPaymentFlag = true) THEN
2645 
2646          /*
2647           * This is a new payment; Get an id for this payment
2648           */
2649          getNextPaymentID(x_currentPaymentID);
2650 
2651          /*
2652           * Create a new payment record using the incoming
2653           * document as a constituent, and insert this record
2654           * into the PLSQL payments table.
2655           */
2656          x_paymentRec.payment_id              :=  x_currentPaymentId;
2657 
2658          x_docsInPmtCount                     := 1;
2659 
2660          x_paymentRec.payment_amount          := x_docsInPmtRec.document_amount;
2661 
2662          x_paymentRec.discount_amount_taken   := x_docsInPmtRec.
2663                                                      pmt_curr_discount;
2664 
2665          x_paymentRec.payment_details         := p_calcDocInfo;
2666 
2667          x_paymentTab(x_paymentTab.COUNT + 1) := x_paymentRec;
2668 
2669          print_debuginfo(l_module_name,' Inserted document : '
2670              || x_docsInPmtRec.document_id || ' into new payment: '
2671              || x_currentPaymentId);
2672 
2673          /*
2674           * Assign the payment id of the new payment to this
2675           * doc, and insert the doc into the docs array.
2676           */
2677          x_docsInPmtRec.payment_id            := x_paymentRec.payment_id;
2678          x_docsInPmtTab(x_docsInPmtTab.COUNT + 1) := x_docsInPmtRec;
2679 
2680      ELSE
2681 
2682          /*
2683           * This means we need to add the incoming document to
2684           * the current  payment.
2685           */
2686 
2687          /*
2688           * First check special case: Payments PLSQL table is empty
2689           *
2690           * If the PLSQL table for payments is empty, we have to
2691           * initialize it by inserting a dummy record. This dummy
2692           * record will get overwritten below.
2693           */
2694          IF (x_paymentTab.COUNT = 0) THEN
2695 
2696              getNextPaymentID(x_currentPaymentID);
2697 
2698              x_paymentRec.payment_id              := x_currentPaymentId;
2699 
2700              x_docsInPmtCount                     := 0;
2701 
2702              x_paymentRec.payment_amount          := 0;
2703              x_paymentRec.discount_amount_taken   :=  0;
2704              x_paymentRec.payment_details         := '';
2705 
2706              /*
2707               * Insert the first record into the table. This
2708               * is a dummy record.
2709               */
2710              x_paymentTab(x_paymentTab.COUNT + 1) := x_paymentRec;
2711 
2712              print_debuginfo(l_module_name, 'Created a new payment: '
2713                  || x_currentPaymentId);
2714 
2715          END IF;
2716 
2717          /*
2718           * The incoming document should be part of the current payment.
2719           * So add the document amount to the current payment
2720           * record and increment the document count for the current
2721           * payment record.
2722           */
2723          x_paymentRec.payment_id     :=  x_currentPaymentId;
2724 
2725          x_docsInPmtCount            := x_docsInPmtCount + 1;
2726 
2727          x_paymentRec.payment_amount :=
2728              x_paymentRec.payment_amount + x_docsInPmtRec.document_amount;
2729 
2730          x_paymentRec.discount_amount_taken   :=
2731              x_paymentRec.discount_amount_taken
2732                  + x_docsInPmtRec.pmt_curr_discount;
2733 
2734          /*
2735           * The payment details information is passed in by the caller.
2736           * If this value is null, it means that the caller does not
2737           * wish to use the grouping rule 'break payments according to
2738           * space available under payment details'. In such a case, do not
2739           * append the payment details from each document into the payment.
2740           */
2741          IF (p_calcDocInfo IS NOT NULL) THEN
2742 
2743              x_paymentRec.payment_details :=
2744                  x_paymentRec.payment_details || p_calcDocInfo;
2745 
2746          END IF;
2747 
2748          /*
2749           * Overwrite the current payment record in the
2750           * PLSQL payments table with the updated record.
2751           */
2752          x_paymentTab(x_paymentTab.COUNT) := x_paymentRec;
2753 
2754          print_debuginfo(l_module_name, 'Inserted document : '
2755              || x_docsInPmtRec.document_id || ' into existing payment: '
2756              || x_currentPaymentId);
2757 
2758          /*
2759           * Assign the payment id of the current payment to this
2760           * doc, and insert the doc into the docs array.
2761           */
2762          x_docsInPmtRec.payment_id        := x_paymentRec.payment_id;
2763          x_docsInPmtTab(x_docsInPmtTab.COUNT + 1) := x_docsInPmtRec;
2764 
2765      END IF;
2766 
2767      print_debuginfo(l_module_name, 'EXIT');
2768 
2769  END insertDocIntoPayment;
2770 
2771 /*--------------------------------------------------------------------
2772  | NAME:
2773  |     insertPayments
2774  |
2775  | PURPOSE:
2776  |     Performs a bulk insert of all created payments from PLSQL
2777  |     table into IBY_PAYMENTS_ALL table.
2778  |
2779  | PARAMETERS:
2780  |     IN
2781  |
2782  |
2783  |     OUT
2784  |
2785  |
2786  | RETURNS:
2787  |
2788  | NOTES:
2789  |
2790  *---------------------------------------------------------------------*/
2791   PROCEDURE insertPayments(
2792      p_paymentTab    IN paymentTabType
2793      )
2794  IS
2795  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME || '.insertPayments';
2796  TYPE tax_regis_num IS TABLE OF iby_payments_all.payee_tax_registration_num%TYPE INDEX BY BINARY_INTEGER;
2797  l_tax_registration_num tax_regis_num;
2798  BEGIN
2799 
2800      print_debuginfo(l_module_name, 'ENTER');
2801 
2802      /* Normally, this shouldn't happen */
2803      IF (p_paymentTab.COUNT = 0) THEN
2804          print_debuginfo(l_module_name, 'After grouping, no payments'
2805              || ' were found to insert into IBY_PAYMENTS_ALL table.'
2806              || ' Possible data corruption issue.');
2807          RETURN;
2808      END IF;
2809 
2810      /*
2811       * Copy values for each column into its corresponding column
2812       * array. These column arrays will be used in the bulk insert.
2813       */
2814      FOR i in p_paymentTab.FIRST..p_paymentTab.LAST LOOP
2815 
2816          paymentTab.payment_id(i)
2817              := p_paymentTab(i).payment_id;
2818          paymentTab.payment_method_code(i)
2819              := p_paymentTab(i).payment_method_code;
2820          paymentTab.payment_service_request_id(i)
2821              := p_paymentTab(i).payment_service_request_id;
2822          paymentTab.process_type(i)
2823              := NVL(p_paymentTab(i).process_type, 'STANDARD');
2824          paymentTab.payment_status(i)
2825              := NVL(p_paymentTab(i).payment_status, 'CREATED');
2826          paymentTab.payments_complete_flag(i)
2827              := NVL(p_paymentTab(i).payments_complete_flag, 'N');
2828          paymentTab.payment_function(i)
2829              := p_paymentTab(i).payment_function;
2830          paymentTab.payment_amount(i)
2831              := p_paymentTab(i).payment_amount;
2832          paymentTab.payment_currency_code(i)
2833              := p_paymentTab(i).payment_currency_code;
2834          paymentTab.bill_payable_flag(i)
2835              := NVL(p_paymentTab(i).bill_payable_flag, 'N');
2836          paymentTab.exclusive_payment_flag(i)
2837              := NVL(p_paymentTab(i).exclusive_payment_flag, 'N');
2838          paymentTab.sep_remit_advice_req_flag(i)
2839              := NVL(p_paymentTab(i).separate_remit_advice_req_flag, 'N');
2840          paymentTab.internal_bank_account_id(i)
2841              := p_paymentTab(i).internal_bank_account_id;
2842          paymentTab.org_id(i)
2843              := p_paymentTab(i).org_id;
2844          paymentTab.org_type(i)
2845              := p_paymentTab(i).org_type;
2846          paymentTab.legal_entity_id(i)
2847              := p_paymentTab(i).legal_entity_id;
2848          paymentTab.declare_payment_flag(i)
2849              := NVL(p_paymentTab(i).declare_payment_flag, 'N');
2850          paymentTab.delivery_channel_code(i)
2851              := p_paymentTab(i).delivery_channel_code;
2852          paymentTab.ext_payee_id(i)
2853              := p_paymentTab(i).ext_payee_id;
2854          paymentTab.payment_instruction_id(i)
2855              := p_paymentTab(i).payment_instruction_id;
2856          paymentTab.payment_profile_id(i)
2857              := p_paymentTab(i).payment_profile_id;
2858          paymentTab.pregrouped_payment_flag(i)
2859              := NVL(p_paymentTab(i).pregrouped_payment_flag, 'N');
2860          paymentTab.stop_confirmed_flag(i)
2861              := NVL(p_paymentTab(i).stop_confirmed_flag, 'N');
2862          paymentTab.stop_released_flag(i)
2863              := NVL(p_paymentTab(i).stop_released_flag, 'N');
2864          paymentTab.stop_request_placed_flag(i)
2865              := NVL(p_paymentTab(i).stop_request_placed_flag, 'N');
2866          paymentTab.created_by(i)
2867              := NVL(p_paymentTab(i).created_by, fnd_global.user_id);
2868          paymentTab.creation_date(i)
2869              := NVL(p_paymentTab(i).creation_date, sysdate);
2870          paymentTab.last_updated_by(i)
2871              := NVL(p_paymentTab(i).last_updated_by, fnd_global.user_id);
2872          paymentTab.last_update_login(i)
2873              := NVL(p_paymentTab(i).last_update_login, fnd_global.user_id);
2874          paymentTab.last_update_date(i)
2875              := NVL(p_paymentTab(i).last_update_date, sysdate);
2876          paymentTab.object_version_number(i)
2877              := NVL(p_paymentTab(i).object_version_number, 1);
2878          paymentTab.payee_party_id(i)
2879              := p_paymentTab(i).payee_party_id;
2880          paymentTab.party_site_id(i)
2881              := p_paymentTab(i).party_site_id;
2882          paymentTab.supplier_site_id(i)
2883              := p_paymentTab(i).supplier_site_id;
2884          paymentTab.payment_reason_code(i)
2885              := p_paymentTab(i).payment_reason_code;
2886          paymentTab.payment_reason_comments(i)
2887              := p_paymentTab(i).payment_reason_comments;
2888          paymentTab.payment_date(i)
2889              := p_paymentTab(i).payment_date;
2890          paymentTab.anticipated_value_date(i)
2891              := p_paymentTab(i).anticipated_value_date;
2892          paymentTab.declaration_amount(i)
2893              := p_paymentTab(i).declaration_amount;
2894          paymentTab.declaration_currency_code(i)
2895              := p_paymentTab(i).declaration_currency_code;
2896          paymentTab.discount_amount_taken(i)
2897              := p_paymentTab(i).discount_amount_taken;
2898          paymentTab.payment_details(i)
2899              := p_paymentTab(i).payment_details;
2900          paymentTab.bank_charge_bearer(i)
2901              := p_paymentTab(i).bank_charge_bearer;
2902          paymentTab.bank_charge_amount(i)
2903              := p_paymentTab(i).bank_charge_amount;
2904          paymentTab.settlement_priority(i)
2905              := p_paymentTab(i).settlement_priority;
2906          paymentTab.remittance_message1(i)
2907              := p_paymentTab(i).remittance_message1;
2908          paymentTab.remittance_message2(i)
2909              := p_paymentTab(i).remittance_message2;
2910          paymentTab.remittance_message3(i)
2911              := p_paymentTab(i).remittance_message3;
2912          paymentTab.payment_reference_number(i)
2913              := p_paymentTab(i).payment_reference_number;
2914          paymentTab.paper_document_number(i)
2915              := p_paymentTab(i).paper_document_number;
2916          paymentTab.bank_assigned_ref_code(i)
2917              := p_paymentTab(i).bank_assigned_ref_code;
2918          paymentTab.external_bank_account_id(i)
2919              := p_paymentTab(i).external_bank_account_id;
2920          paymentTab.unique_remittance_identifier(i)
2921              := p_paymentTab(i).unique_remittance_identifier;
2922          paymentTab.uri_check_digit(i)
2923              := p_paymentTab(i).uri_check_digit;
2924          paymentTab.bank_instruction1_code(i)
2925              := p_paymentTab(i).bank_instruction1_code;
2926          paymentTab.bank_instruction2_code(i)
2927              := p_paymentTab(i).bank_instruction2_code;
2928          paymentTab.bank_instruction_details(i)
2929              := p_paymentTab(i).bank_instruction_details;
2930          paymentTab.payment_text_message1(i)
2931              := p_paymentTab(i).payment_text_message1;
2932          paymentTab.payment_text_message2(i)
2933              := p_paymentTab(i).payment_text_message2;
2934          paymentTab.payment_text_message3(i)
2935              := p_paymentTab(i).payment_text_message3;
2936          paymentTab.maturity_date(i)
2937              := p_paymentTab(i).maturity_date;
2938          paymentTab.payment_due_date(i)
2939              := p_paymentTab(i).payment_due_date;
2940          paymentTab.document_category_code(i)
2941              := p_paymentTab(i).document_category_code;
2942          paymentTab.document_sequence_id(i)
2943              := p_paymentTab(i).document_sequence_id;
2944          paymentTab.document_sequence_value(i)
2945              := p_paymentTab(i).document_sequence_value;
2946          paymentTab.beneficiary_party(i)
2947              := p_paymentTab(i).beneficiary_party;
2948          paymentTab.stop_confirmed_by(i)
2949              := p_paymentTab(i).stop_confirmed_by;
2950          paymentTab.stop_confirm_date(i)
2951              := p_paymentTab(i).stop_confirm_date;
2952          paymentTab.stop_confirm_reason(i)
2953              := p_paymentTab(i).stop_confirm_reason;
2954          paymentTab.stop_confirm_reference(i)
2955              := p_paymentTab(i).stop_confirm_reference;
2956          paymentTab.stop_released_by(i)
2957              := p_paymentTab(i).stop_released_by;
2958          paymentTab.stop_release_date(i)
2959              := p_paymentTab(i).stop_release_date;
2960          paymentTab.stop_release_reason(i)
2961              := p_paymentTab(i).stop_release_reason;
2962          paymentTab.stop_release_reference(i)
2963              := p_paymentTab(i).stop_release_reference;
2964          paymentTab.stop_request_date(i)
2965              := p_paymentTab(i).stop_request_date;
2966          paymentTab.stop_request_placed_by(i)
2967              := p_paymentTab(i).stop_request_placed_by;
2968          paymentTab.stop_request_reason(i)
2969              := p_paymentTab(i).stop_request_reason;
2970          paymentTab.stop_request_reference(i)
2971              := p_paymentTab(i).stop_request_reference;
2972          paymentTab.voided_by(i)
2973              := p_paymentTab(i).voided_by;
2974          paymentTab.void_date(i)
2975              := p_paymentTab(i).void_date;
2976          paymentTab.void_reason(i)
2977              := p_paymentTab(i).void_reason;
2978          paymentTab.remit_to_location_id(i)
2979              := p_paymentTab(i).remit_to_location_id;
2980          paymentTab.completed_pmts_group_id(i)
2981              := p_paymentTab(i).completed_pmts_group_id;
2982          paymentTab.attribute_category(i)
2983              := p_paymentTab(i).attribute_category;
2984          paymentTab.attribute1(i)
2985              := p_paymentTab(i).attribute1;
2986          paymentTab.attribute2(i)
2987              := p_paymentTab(i).attribute2;
2988          paymentTab.attribute3(i)
2989              := p_paymentTab(i).attribute3;
2990          paymentTab.attribute4(i)
2991              := p_paymentTab(i).attribute4;
2992          paymentTab.attribute5(i)
2993              := p_paymentTab(i).attribute5;
2994          paymentTab.attribute6(i)
2995              := p_paymentTab(i).attribute6;
2996          paymentTab.attribute7(i)
2997              := p_paymentTab(i).attribute7;
2998          paymentTab.attribute8(i)
2999              := p_paymentTab(i).attribute8;
3000          paymentTab.attribute9(i)
3001              := p_paymentTab(i).attribute9;
3002          paymentTab.attribute10(i)
3003              := p_paymentTab(i).attribute10;
3004          paymentTab.attribute11(i)
3005              := p_paymentTab(i).attribute11;
3006          paymentTab.attribute12(i)
3007              := p_paymentTab(i).attribute12;
3008          paymentTab.attribute13(i)
3009              := p_paymentTab(i).attribute13;
3010          paymentTab.attribute14(i)
3011              := p_paymentTab(i).attribute14;
3012          paymentTab.attribute15(i)
3013              := p_paymentTab(i).attribute15;
3014          paymentTab.ext_branch_number(i)
3015              := p_paymentTab(i).ext_branch_number;
3016          paymentTab.ext_bank_number(i)
3017              := p_paymentTab(i).ext_bank_number;
3018          paymentTab.ext_bank_account_name(i)
3019              := p_paymentTab(i).ext_bank_account_name;
3020          paymentTab.ext_bank_account_number(i)
3021              := p_paymentTab(i).ext_bank_account_number;
3022          paymentTab.ext_bank_account_type(i)
3023              := p_paymentTab(i).ext_bank_account_type;
3024          paymentTab.ext_bank_account_iban_number(i)
3025              := p_paymentTab(i).ext_bank_account_iban_number;
3026          paymentTab.payee_name(i)
3027              := p_paymentTab(i).payee_name;
3028          paymentTab.payee_address1(i)
3029              := p_paymentTab(i).payee_address1;
3030          paymentTab.payee_address2(i)
3031              := p_paymentTab(i).payee_address2;
3032          paymentTab.payee_address3(i)
3033              := p_paymentTab(i).payee_address3;
3034          paymentTab.payee_address4(i)
3035              := p_paymentTab(i).payee_address4;
3036          paymentTab.payee_city(i)
3037              := p_paymentTab(i).payee_city;
3038          paymentTab.payee_postal_code(i)
3039              := p_paymentTab(i).payee_postal_code;
3040          paymentTab.payee_state(i)
3041              := p_paymentTab(i).payee_state;
3042          paymentTab.payee_province(i)
3043              := p_paymentTab(i).payee_province;
3044          paymentTab.payee_county(i)
3045              := p_paymentTab(i).payee_county;
3046          paymentTab.payee_country(i)
3047              := p_paymentTab(i).payee_country;
3048          paymentTab.remit_advice_delivery_method(i)
3049              := p_paymentTab(i).remit_advice_delivery_method;
3050          paymentTab.remit_advice_email(i)
3051              := p_paymentTab(i).remit_advice_email;
3052          paymentTab.remit_advice_fax(i)
3053              := p_paymentTab(i).remit_advice_fax;
3054          paymentTab.address_source(i)
3055              := p_paymentTab(i).address_source;
3056          paymentTab.employee_address_code(i)
3057              := p_paymentTab(i).employee_address_code;
3058          paymentTab.employee_person_id(i)
3059              := p_paymentTab(i).employee_person_id;
3060          paymentTab.employee_address_id(i)
3061              := p_paymentTab(i).employee_address_id;
3062          paymentTab.employee_payment_flag(i)
3063              := p_paymentTab(i).employee_payment_flag;
3064 
3065 paymentTab.ext_inv_payee_id(i)
3066              := p_paymentTab(i).ext_inv_payee_id;
3067 paymentTab.inv_payee_party_id(i)
3068              := p_paymentTab(i).inv_payee_party_id;
3069 paymentTab.inv_party_site_id(i)
3070              := p_paymentTab(i).inv_party_site_id;
3071 paymentTab.inv_supplier_site_id(i)
3072              := p_paymentTab(i).inv_supplier_site_id;
3073 paymentTab.inv_beneficiary_party(i)
3074              := p_paymentTab(i).inv_beneficiary_party;
3075 paymentTab.inv_payee_name(i)
3076              := p_paymentTab(i).inv_payee_name;
3077 paymentTab.inv_payee_address1(i)
3078              := p_paymentTab(i).inv_payee_address1;
3079 paymentTab.inv_payee_address2(i)
3080              := p_paymentTab(i).inv_payee_address2;
3081 paymentTab.inv_payee_address3(i)
3082              := p_paymentTab(i).inv_payee_address3;
3083 paymentTab.inv_payee_address4(i)
3084              := p_paymentTab(i).inv_payee_address4;
3085 paymentTab.inv_payee_city(i)
3086              := p_paymentTab(i).inv_payee_city;
3087 paymentTab.inv_payee_postal_code(i)
3088              := p_paymentTab(i).inv_payee_postal_code;
3089 paymentTab.inv_payee_state(i)
3090              := p_paymentTab(i).inv_payee_state;
3091 paymentTab.inv_payee_province(i)
3092              := p_paymentTab(i).inv_payee_province;
3093 paymentTab.inv_payee_county(i)
3094              := p_paymentTab(i).inv_payee_county;
3095 paymentTab.inv_payee_country(i)
3096              := p_paymentTab(i).inv_payee_country;
3097 paymentTab.inv_payee_party_name(i)
3098              := p_paymentTab(i).inv_payee_party_name;
3099 paymentTab.inv_payee_le_reg_num(i)
3100              := p_paymentTab(i).inv_payee_le_reg_num;
3101 paymentTab.inv_payee_tax_reg_num(i)
3102              := p_paymentTab(i).inv_payee_tax_reg_num;
3103 paymentTab.inv_payee_address_concat(i)
3104              := p_paymentTab(i).inv_payee_address_concat;
3105 paymentTab.inv_beneficiary_name(i)
3106              := p_paymentTab(i).inv_beneficiary_name;
3107 paymentTab.inv_payee_party_number(i)
3108              := p_paymentTab(i).inv_payee_party_number;
3109 paymentTab.inv_payee_alternate_name(i)
3110              := p_paymentTab(i).inv_payee_alternate_name;
3111 paymentTab.inv_payee_site_alt_name(i)
3112              := p_paymentTab(i).inv_payee_site_alt_name;
3113 paymentTab.inv_payee_supplier_number(i)
3114              := p_paymentTab(i).inv_payee_supplier_number;
3115 paymentTab.inv_payee_first_party_ref(i)
3116              := p_paymentTab(i).inv_payee_first_party_ref;
3117 paymentTab.ext_bnk_acct_ownr_inv_prty_id(i)
3118              := p_paymentTab(i).ext_bnk_acct_ownr_inv_prty_id;
3119 paymentTab.ext_bnk_branch_inv_prty_id(i)
3120              := p_paymentTab(i).ext_bnk_branch_inv_prty_id;
3121 paymentTab.ext_bnk_acct_ownr_inv_prty_nme(i)
3122              := p_paymentTab(i).ext_bnk_acct_ownr_inv_prty_nme;
3123 paymentTab.inv_payee_party_attr_cat(i)
3124              := p_paymentTab(i).inv_payee_party_attr_cat;
3125 paymentTab.inv_payee_supplier_attr_cat(i)
3126              := p_paymentTab(i).inv_payee_supplier_attr_cat;
3127 paymentTab.inv_payee_spplr_site_attr_cat(i)
3128              := p_paymentTab(i).inv_payee_spplr_site_attr_cat;
3129 paymentTab.inv_payee_supplier_site_name(i)
3130              := p_paymentTab(i).inv_payee_supplier_site_name;
3131 paymenttab.inv_payee_spplr_site_alt_name(i)
3132              := p_paymenttab(i).inv_payee_spplr_site_alt_name;
3133 paymenttab.inv_payee_supplier_id(i)
3134              := p_paymenttab(i).inv_payee_supplier_id;
3135 
3136 
3137        print_debuginfo(l_module_name, 'for loop value of i::'||i);
3138 	 l_tax_registration_num(i) := NULL;
3139           --Bug 7215055
3140 	  IF p_paymentTab(i).supplier_site_id IS NOT NULL
3141 	  THEN
3142 	    BEGIN
3143               SELECT vat_registration_num
3144               INTO l_tax_registration_num(i)
3145               FROM ap_supplier_sites_all
3146               WHERE vendor_site_id = p_paymentTab(i).supplier_site_id;
3147 
3148 	    EXCEPTION
3149 	      WHEN NO_DATA_FOUND
3150 		THEN
3151 		 BEGIN
3152                   SELECT asup.vat_registration_num
3153                   INTO l_tax_registration_num(i)
3154                   FROM ap_suppliers asup
3155 	          ,    ap_supplier_sites_all asups
3156                   WHERE asups.vendor_site_id = p_paymentTab(i).supplier_site_id
3157 	          AND   asup.vendor_id = asups.vendor_site_id;
3158 		 EXCEPTION
3159 		   WHEN OTHERS THEN
3160                    l_tax_registration_num(i) := NULL;
3161 		   print_debuginfo(l_module_name, 'VAT_EXCEPTION::'||sqlerrm);
3162 		 END;
3163 	      WHEN OTHERS THEN
3164                 l_tax_registration_num(i) := NULL;
3165 		       print_debuginfo(l_module_name, 'VAT_EXCEPTION::'||sqlerrm);
3166 	    END;
3167 	  END IF;
3168 
3169      END LOOP;
3170 
3171 
3172      /*
3173       * Bulk insert into IBY_PAYMENTS_ALL table using the
3174       * named columns syntax. This avoids any dependencies on
3175       * column order.
3176       */
3177      FORALL i in nvl(paymentTab.payment_id.FIRST,0) .. nvl(paymentTab.payment_id.LAST,-99)
3178      INSERT INTO IBY_PAYMENTS_ALL
3179              (
3180              payment_id,
3181              payment_method_code,
3182              payment_service_request_id,
3183              process_type,
3184              payment_status,
3185              payments_complete_flag,
3186              payment_function,
3187              payment_amount,
3188              payment_currency_code,
3189              bill_payable_flag,
3190              exclusive_payment_flag,
3191              separate_remit_advice_req_flag,
3192              internal_bank_account_id,
3193              org_id,
3194              org_type,
3195              legal_entity_id,
3196              declare_payment_flag,
3197              delivery_channel_code,
3198              ext_payee_id,
3199              payment_instruction_id,
3200              payment_profile_id,
3201              pregrouped_payment_flag,
3202              stop_confirmed_flag,
3203              stop_released_flag,
3204              stop_request_placed_flag,
3205              created_by,
3206              creation_date,
3207              last_updated_by,
3208              last_update_login,
3209              last_update_date,
3210              object_version_number,
3211              payee_party_id,
3212              party_site_id,
3213              supplier_site_id,
3214              payment_reason_code,
3215              payment_reason_comments,
3216              payment_date,
3217              anticipated_value_date,
3218              declaration_amount,
3219              declaration_currency_code,
3220              discount_amount_taken,
3221              payment_details,
3222              bank_charge_bearer,
3223              bank_charge_amount,
3224              settlement_priority,
3225              remittance_message1,
3226              remittance_message2,
3227              remittance_message3,
3228              payment_reference_number,
3229              paper_document_number,
3230              bank_assigned_ref_code,
3231              external_bank_account_id,
3232              unique_remittance_identifier,
3233              uri_check_digit,
3234              bank_instruction1_code,
3235              bank_instruction2_code,
3236              bank_instruction_details,
3237              payment_text_message1,
3238              payment_text_message2,
3239              payment_text_message3,
3240              maturity_date,
3241              payment_due_date,
3242              document_category_code,
3243              document_sequence_id,
3244              document_sequence_value,
3245              beneficiary_party,
3246              stop_confirmed_by,
3247              stop_confirm_date,
3248              stop_confirm_reason,
3249              stop_confirm_reference,
3250              stop_released_by,
3251              stop_release_date,
3252              stop_release_reason,
3253              stop_release_reference,
3254              stop_request_date,
3255              stop_request_placed_by,
3256              stop_request_reason,
3257              stop_request_reference,
3258              voided_by,
3259              void_date,
3260              void_reason,
3261              remit_to_location_id,
3262              completed_pmts_group_id,
3263              attribute_category,
3264              attribute1,
3265              attribute2,
3266              attribute3,
3267              attribute4,
3268              attribute5,
3269              attribute6,
3270              attribute7,
3271              attribute8,
3272              attribute9,
3273              attribute10,
3274              attribute11,
3275              attribute12,
3276              attribute13,
3277              attribute14,
3278              attribute15,
3279              ext_branch_number,
3280              ext_bank_number,
3281              ext_bank_account_name,
3282              ext_bank_account_number,
3283              ext_bank_account_type,
3284              ext_bank_account_iban_number,
3285              payee_name,
3286              payee_address1,
3287              payee_address2,
3288              payee_address3,
3289              payee_address4,
3290              payee_city,
3291              payee_postal_code,
3292              payee_state,
3293              payee_province,
3294              payee_county,
3295              payee_country,
3296              remit_advice_delivery_method,
3297              remit_advice_email,
3298              remit_advice_fax,
3299              address_source,
3300              employee_address_code,
3301              employee_person_id,
3302              employee_address_id,
3303              employee_payment_flag,
3304 	     payee_tax_registration_num,
3305 		ext_inv_payee_id ,
3306 		inv_payee_party_id ,
3307 		inv_party_site_id ,
3308 		inv_supplier_site_id ,
3309 		inv_beneficiary_party ,
3310 		inv_payee_name ,
3311 		inv_payee_address1 ,
3312 		inv_payee_address2 ,
3313 		inv_payee_address3 ,
3314 		inv_payee_address4 ,
3315 		inv_payee_city ,
3316 		inv_payee_postal_code ,
3317 		inv_payee_state ,
3318 		inv_payee_province ,
3319 		inv_payee_county ,
3320 		inv_payee_country ,
3321 		inv_payee_party_name ,
3322 		inv_payee_le_reg_num,
3323 		inv_payee_tax_reg_num,
3324 		inv_payee_address_concat ,
3325 		inv_beneficiary_name ,
3326 		inv_payee_party_number ,
3327 		inv_payee_alternate_name ,
3328 		inv_payee_site_alt_name,
3329 		inv_payee_supplier_number ,
3330 		inv_payee_first_party_ref,
3331 		ext_bnk_acct_ownr_inv_prty_id,
3332 		ext_bnk_branch_inv_prty_id,
3333 		ext_bnk_acct_ownr_inv_prty_nme,
3334 		inv_payee_party_attr_cat,
3335 		inv_payee_supplier_attr_cat,
3336 		inv_payee_spplr_site_attr_cat,
3337 		inv_payee_supplier_site_name ,
3338 		inv_payee_spplr_site_alt_name,
3339 		inv_payee_supplier_id
3340              )
3341          VALUES
3342              (
3343              paymentTab.payment_id(i),
3344              paymentTab.payment_method_code(i),
3345              paymentTab.payment_service_request_id(i),
3346              paymentTab.process_type(i),
3347              paymentTab.payment_status(i),
3348              paymentTab.payments_complete_flag(i),
3349              paymentTab.payment_function(i),
3350              paymentTab.payment_amount(i),
3351              paymentTab.payment_currency_code(i),
3352              paymentTab.bill_payable_flag(i),
3353              paymentTab.exclusive_payment_flag(i),
3354              paymentTab.sep_remit_advice_req_flag(i),
3355              paymentTab.internal_bank_account_id(i),
3356              paymentTab.org_id(i),
3357              paymentTab.org_type(i),
3358              paymentTab.legal_entity_id(i),
3359              paymentTab.declare_payment_flag(i),
3360              paymentTab.delivery_channel_code(i),
3361              paymentTab.ext_payee_id(i),
3362              paymentTab.payment_instruction_id(i),
3363              paymentTab.payment_profile_id(i),
3364              paymentTab.pregrouped_payment_flag(i),
3365              paymentTab.stop_confirmed_flag(i),
3366              paymentTab.stop_released_flag(i),
3367              paymentTab.stop_request_placed_flag(i),
3368              paymentTab.created_by(i),
3369              paymentTab.creation_date(i),
3370              paymentTab.last_updated_by(i),
3371              paymentTab.last_update_login(i),
3372              paymentTab.last_update_date(i),
3373              paymentTab.object_version_number(i),
3374              paymentTab.payee_party_id(i),
3375              paymentTab.party_site_id(i),
3376              paymentTab.supplier_site_id(i),
3377              paymentTab.payment_reason_code(i),
3378              paymentTab.payment_reason_comments(i),
3379              paymentTab.payment_date(i),
3380              paymentTab.anticipated_value_date(i),
3381              paymentTab.declaration_amount(i),
3382              paymentTab.declaration_currency_code(i),
3383              paymentTab.discount_amount_taken(i),
3384              paymentTab.payment_details(i),
3385              paymentTab.bank_charge_bearer(i),
3386              paymentTab.bank_charge_amount(i),
3387              paymentTab.settlement_priority(i),
3388              paymentTab.remittance_message1(i),
3389              paymentTab.remittance_message2(i),
3390              paymentTab.remittance_message3(i),
3391              paymentTab.payment_reference_number(i),
3392              paymentTab.paper_document_number(i),
3393              paymentTab.bank_assigned_ref_code(i),
3394              paymentTab.external_bank_account_id(i),
3395              paymentTab.unique_remittance_identifier(i),
3396              paymentTab.uri_check_digit(i),
3397              paymentTab.bank_instruction1_code(i),
3398              paymentTab.bank_instruction2_code(i),
3399              paymentTab.bank_instruction_details(i),
3400              paymentTab.payment_text_message1(i),
3401              paymentTab.payment_text_message2(i),
3402              paymentTab.payment_text_message3(i),
3403              paymentTab.maturity_date(i),
3404              paymentTab.payment_due_date(i),
3405              paymentTab.document_category_code(i),
3406              paymentTab.document_sequence_id(i),
3407              paymentTab.document_sequence_value(i),
3408              paymentTab.beneficiary_party(i),
3409              paymentTab.stop_confirmed_by(i),
3410              paymentTab.stop_confirm_date(i),
3411              paymentTab.stop_confirm_reason(i),
3412              paymentTab.stop_confirm_reference(i),
3413              paymentTab.stop_released_by(i),
3414              paymentTab.stop_release_date(i),
3415              paymentTab.stop_release_reason(i),
3416              paymentTab.stop_release_reference(i),
3417              paymentTab.stop_request_date(i),
3418              paymentTab.stop_request_placed_by(i),
3419              paymentTab.stop_request_reason(i),
3420              paymentTab.stop_request_reference(i),
3421              paymentTab.voided_by(i),
3422              paymentTab.void_date(i),
3423              paymentTab.void_reason(i),
3424              paymentTab.remit_to_location_id(i),
3425              paymentTab.completed_pmts_group_id(i),
3426              paymentTab.attribute_category(i),
3427              paymentTab.attribute1(i),
3428              paymentTab.attribute2(i),
3429              paymentTab.attribute3(i),
3430              paymentTab.attribute4(i),
3431              paymentTab.attribute5(i),
3432              paymentTab.attribute6(i),
3433              paymentTab.attribute7(i),
3434              paymentTab.attribute8(i),
3435              paymentTab.attribute9(i),
3436              paymentTab.attribute10(i),
3437              paymentTab.attribute11(i),
3438              paymentTab.attribute12(i),
3439              paymentTab.attribute13(i),
3440              paymentTab.attribute14(i),
3441              paymentTab.attribute15(i),
3442              paymentTab.ext_branch_number(i),
3443              paymentTab.ext_bank_number(i),
3444              paymentTab.ext_bank_account_name(i),
3445              paymentTab.ext_bank_account_number(i),
3446              paymentTab.ext_bank_account_type(i),
3447              paymentTab.ext_bank_account_iban_number(i),
3448              paymentTab.payee_name(i),
3449              paymentTab.payee_address1(i),
3450              paymentTab.payee_address2(i),
3451              paymentTab.payee_address3(i),
3452              paymentTab.payee_address4(i),
3453              paymentTab.payee_city(i),
3454              paymentTab.payee_postal_code(i),
3455              paymentTab.payee_state(i),
3456              paymentTab.payee_province(i),
3457              paymentTab.payee_county(i),
3458              paymentTab.payee_country(i),
3459              paymentTab.remit_advice_delivery_method(i),
3460              paymentTab.remit_advice_email(i),
3461              paymentTab.remit_advice_fax(i),
3462              paymentTab.address_source(i),
3463              paymentTab.employee_address_code(i),
3464              paymentTab.employee_person_id(i),
3465              paymentTab.employee_address_id(i),
3466              paymentTab.employee_payment_flag(i),
3467 	     l_tax_registration_num(i),
3468 paymentTab.ext_inv_payee_id(i),
3469 paymentTab.inv_payee_party_id(i),
3470 paymentTab.inv_party_site_id(i),
3471 paymentTab.inv_supplier_site_id(i),
3472 paymentTab.inv_beneficiary_party(i),
3473 paymentTab.inv_payee_name(i),
3474 paymentTab.inv_payee_address1(i),
3475 paymentTab.inv_payee_address2(i),
3476 paymentTab.inv_payee_address3(i),
3477 paymentTab.inv_payee_address4(i),
3478 paymentTab.inv_payee_city(i),
3479 paymentTab.inv_payee_postal_code(i),
3480 paymentTab.inv_payee_state(i),
3481 paymentTab.inv_payee_province(i),
3482 paymentTab.inv_payee_county(i),
3483 paymentTab.inv_payee_country(i),
3484 paymentTab.inv_payee_party_name(i),
3485 paymentTab.inv_payee_le_reg_num(i),
3486 paymentTab.inv_payee_tax_reg_num(i),
3487 paymentTab.inv_payee_address_concat(i),
3488 paymentTab.inv_beneficiary_name(i),
3489 paymentTab.inv_payee_party_number(i),
3490 paymentTab.inv_payee_alternate_name(i),
3491 paymentTab.inv_payee_site_alt_name(i),
3492 paymentTab.inv_payee_supplier_number(i),
3493 paymentTab.inv_payee_first_party_ref(i),
3494 paymentTab.ext_bnk_acct_ownr_inv_prty_id(i),
3495 paymentTab.ext_bnk_branch_inv_prty_id(i),
3496 paymentTab.ext_bnk_acct_ownr_inv_prty_nme(i),
3497 paymentTab.inv_payee_party_attr_cat(i),
3498 paymentTab.inv_payee_supplier_attr_cat(i),
3499 paymentTab.inv_payee_spplr_site_attr_cat(i),
3500 paymentTab.inv_payee_supplier_site_name(i),
3501 paymenttab.inv_payee_spplr_site_alt_name(i),
3502 paymenttab.inv_payee_supplier_id(i)
3503 
3504              );
3505 
3506              delete_paymentTab;
3507 
3508      print_debuginfo(l_module_name, 'EXIT');
3509 
3510  END insertPayments;
3511 
3512 /*--------------------------------------------------------------------
3513  | NAME:
3514  |
3515  | PURPOSE:
3516  |     This procedure is used to free up the memory used by
3517  |     global memory structure
3518  |
3519  | PARAMETERS:
3520  |
3521  |     NONE
3522  |
3523  | RETURNS:
3524  |
3525  | NOTES:
3526  |
3527  *---------------------------------------------------------------------*/
3528  PROCEDURE delete_paymentTab IS
3529  l_module_name     CONSTANT VARCHAR2(200) := G_PKG_NAME ||
3530                                        '.delete_paymentTab';
3531   BEGIN
3532 
3533      print_debuginfo(l_module_name, 'ENTER');
3534              paymentTab.payment_id.delete;
3535              paymentTab.payment_method_code.delete;
3536              paymentTab.payment_service_request_id.delete;
3537              paymentTab.process_type.delete;
3538              paymentTab.payment_status.delete;
3539              paymentTab.payments_complete_flag.delete;
3540              paymentTab.payment_function.delete;
3541              paymentTab.payment_amount.delete;
3542              paymentTab.payment_currency_code.delete;
3543              paymentTab.bill_payable_flag.delete;
3544              paymentTab.exclusive_payment_flag.delete;
3545              paymentTab.sep_remit_advice_req_flag.delete;
3546              paymentTab.internal_bank_account_id.delete;
3547              paymentTab.org_id.delete;
3548              paymentTab.org_type.delete;
3549              paymentTab.legal_entity_id.delete;
3550              paymentTab.declare_payment_flag.delete;
3551              paymentTab.delivery_channel_code.delete;
3552              paymentTab.ext_payee_id.delete;
3553              paymentTab.payment_instruction_id.delete;
3554              paymentTab.payment_profile_id.delete;
3555              paymentTab.pregrouped_payment_flag.delete;
3556              paymentTab.stop_confirmed_flag.delete;
3557              paymentTab.stop_released_flag.delete;
3558              paymentTab.stop_request_placed_flag.delete;
3559              paymentTab.created_by.delete;
3560              paymentTab.creation_date.delete;
3561              paymentTab.last_updated_by.delete;
3562              paymentTab.last_update_login.delete;
3563              paymentTab.last_update_date.delete;
3564              paymentTab.object_version_number.delete;
3565              paymentTab.payee_party_id.delete;
3566              paymentTab.party_site_id.delete;
3567              paymentTab.supplier_site_id.delete;
3568              paymentTab.payment_reason_code.delete;
3569              paymentTab.payment_reason_comments.delete;
3570              paymentTab.payment_date.delete;
3571              paymentTab.anticipated_value_date.delete;
3572              paymentTab.declaration_amount.delete;
3573              paymentTab.declaration_currency_code.delete;
3574              paymentTab.discount_amount_taken.delete;
3575              paymentTab.payment_details.delete;
3576              paymentTab.bank_charge_bearer.delete;
3577              paymentTab.bank_charge_amount.delete;
3578              paymentTab.settlement_priority.delete;
3579              paymentTab.remittance_message1.delete;
3580              paymentTab.remittance_message2.delete;
3581              paymentTab.remittance_message3.delete;
3582              paymentTab.payment_reference_number.delete;
3583              paymentTab.paper_document_number.delete;
3584              paymentTab.bank_assigned_ref_code.delete;
3585              paymentTab.external_bank_account_id.delete;
3586              paymentTab.unique_remittance_identifier.delete;
3587              paymentTab.uri_check_digit.delete;
3588              paymentTab.bank_instruction1_code.delete;
3589              paymentTab.bank_instruction2_code.delete;
3590              paymentTab.bank_instruction_details.delete;
3591              paymentTab.payment_text_message1.delete;
3592              paymentTab.payment_text_message2.delete;
3593              paymentTab.payment_text_message3.delete;
3594              paymentTab.maturity_date.delete;
3595              paymentTab.payment_due_date.delete;
3596              paymentTab.document_category_code.delete;
3597              paymentTab.document_sequence_id.delete;
3598              paymentTab.document_sequence_value.delete;
3599              paymentTab.beneficiary_party.delete;
3600              paymentTab.stop_confirmed_by.delete;
3601              paymentTab.stop_confirm_date.delete;
3602              paymentTab.stop_confirm_reason.delete;
3603              paymentTab.stop_confirm_reference.delete;
3604              paymentTab.stop_released_by.delete;
3605              paymentTab.stop_release_date.delete;
3606              paymentTab.stop_release_reason.delete;
3607              paymentTab.stop_release_reference.delete;
3608              paymentTab.stop_request_date.delete;
3609              paymentTab.stop_request_placed_by.delete;
3610              paymentTab.stop_request_reason.delete;
3611              paymentTab.stop_request_reference.delete;
3612              paymentTab.voided_by.delete;
3613              paymentTab.void_date.delete;
3614              paymentTab.void_reason.delete;
3615              paymentTab.remit_to_location_id.delete;
3616              paymentTab.completed_pmts_group_id.delete;
3617              paymentTab.attribute_category.delete;
3618              paymentTab.attribute1.delete;
3619              paymentTab.attribute2.delete;
3620              paymentTab.attribute3.delete;
3621              paymentTab.attribute4.delete;
3622              paymentTab.attribute5.delete;
3623              paymentTab.attribute6.delete;
3624              paymentTab.attribute7.delete;
3625              paymentTab.attribute8.delete;
3626              paymentTab.attribute9.delete;
3627              paymentTab.attribute10.delete;
3628              paymentTab.attribute11.delete;
3629              paymentTab.attribute12.delete;
3630              paymentTab.attribute13.delete;
3631              paymentTab.attribute14.delete;
3632              paymentTab.attribute15.delete;
3633              paymentTab.ext_branch_number.delete;
3634              paymentTab.ext_bank_number.delete;
3635              paymentTab.ext_bank_account_name.delete;
3636              paymentTab.ext_bank_account_number.delete;
3637              paymentTab.ext_bank_account_type.delete;
3638              paymentTab.ext_bank_account_iban_number.delete;
3639              paymentTab.payee_name.delete;
3640              paymentTab.payee_address1.delete;
3641              paymentTab.payee_address2.delete;
3642              paymentTab.payee_address3.delete;
3643              paymentTab.payee_address4.delete;
3644              paymentTab.payee_city.delete;
3645              paymentTab.payee_postal_code.delete;
3646              paymentTab.payee_state.delete;
3647              paymentTab.payee_province.delete;
3648              paymentTab.payee_county.delete;
3649              paymentTab.payee_country.delete;
3650              paymentTab.remit_advice_delivery_method.delete;
3651              paymentTab.remit_advice_email.delete;
3652              paymentTab.remit_advice_fax.delete;
3653              paymentTab.address_source.delete;
3654              paymentTab.employee_address_code.delete;
3655              paymentTab.employee_person_id.delete;
3656              paymentTab.employee_address_id.delete;
3657              paymentTab.employee_payment_flag.delete;
3658 	        paymentTab.inv_payee_party_id.delete;
3659                 paymentTab.inv_party_site_id.delete;
3660                 paymentTab.inv_supplier_site_id.delete;
3661                 paymentTab.inv_beneficiary_party.delete;
3662                 paymentTab.ext_inv_payee_id.delete;
3663 		paymentTab.inv_payee_name.delete;
3664 		paymentTab.inv_payee_address1.delete;
3665 		paymentTab.inv_payee_address2.delete;
3666 		paymentTab.inv_payee_address3.delete;
3667 		paymentTab.inv_payee_address4.delete;
3668 		paymentTab.inv_payee_city.delete;
3669 		paymentTab.inv_payee_postal_code.delete;
3670 		paymentTab.inv_payee_state.delete;
3671 		paymentTab.inv_payee_province.delete;
3672 		paymentTab.inv_payee_county.delete;
3673 		paymentTab.inv_payee_country.delete;
3674 		paymentTab.inv_payee_party_name.delete;
3675 		paymentTab.inv_payee_le_reg_num.delete;
3676 		paymentTab.inv_payee_tax_reg_num.delete;
3677 		paymentTab.inv_payee_address_concat.delete;
3678 		paymentTab.inv_beneficiary_name.delete;
3679 		paymentTab.inv_payee_party_number.delete;
3680 		paymentTab.inv_payee_alternate_name.delete;
3681 		paymentTab.inv_payee_site_alt_name.delete;
3682 		paymentTab.inv_payee_supplier_number.delete;
3683 		paymentTab.inv_payee_first_party_ref.delete;
3684 		paymentTab.ext_bnk_acct_ownr_inv_prty_id.delete;
3685 		paymentTab.ext_bnk_branch_inv_prty_id.delete;
3686 		paymentTab.ext_bnk_acct_ownr_inv_prty_nme.delete;
3687 		paymentTab.inv_payee_party_attr_cat.delete;
3688 		paymentTab.inv_payee_supplier_attr_cat.delete;
3689 		paymentTab.inv_payee_spplr_site_attr_cat.delete;
3690 		paymentTab.inv_payee_supplier_site_name.delete;
3691 		paymentTab.inv_payee_spplr_site_alt_name.delete;
3692 		paymentTab.inv_payee_supplier_id.delete;
3693 
3694      print_debuginfo(l_module_name, 'EXIT');
3695 
3696  END delete_paymentTab;
3697 
3698 /*--------------------------------------------------------------------
3699  | NAME:
3700  |     updatePayments
3701  |
3702  | PURPOSE:
3703  |     Performs an update of all created payments from PLSQL
3704  |     table into IBY_PAYMENTS_ALL table.
3705  |
3706  |     The created payments have already been inserted into
3707  |     IBY_PAYMENTS_ALL after grouping. So we only need to update
3708  |     certain fields of the payment that have been changed
3709  |     after the grouping was performed.
3710  |
3711  | PARAMETERS:
3712  |     IN
3713  |
3714  |
3715  |     OUT
3716  |
3717  |
3718  | RETURNS:
3719  |
3720  | NOTES:
3721  |
3722  *---------------------------------------------------------------------*/
3723  PROCEDURE updatePayments(
3724      p_paymentTab    IN paymentTabType
3725      )
3726  IS
3727  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME || '.updatePayments';
3728 
3729  BEGIN
3730 
3731      print_debuginfo(l_module_name, 'ENTER');
3732 
3733      /* Normally, this shouldn't happen */
3734      IF (p_paymentTab.COUNT = 0) THEN
3735          print_debuginfo(l_module_name, 'No payments'
3736              || ' were found to update IBY_PAYMENTS_ALL table.');
3737          RETURN;
3738      END IF;
3739 
3740      FOR i in p_paymentTab.FIRST..p_paymentTab.LAST LOOP
3741 
3742          UPDATE
3743              IBY_PAYMENTS_ALL
3744          SET
3745              payment_status       = p_paymentTab(i).payment_status,
3746              payment_amount       = p_paymentTab(i).payment_amount,
3747 	     discount_amount_taken = p_paymentTab(i).discount_amount_taken,
3748              declare_payment_flag = p_paymentTab(i).declare_payment_flag,
3749              declaration_amount   = p_paymentTab(i).declaration_amount,
3750              bank_charge_amount   = p_paymentTab(i).bank_charge_amount,
3751              separate_remit_advice_req_flag
3752                                   = p_paymentTab(i).
3753                                         separate_remit_advice_req_flag
3754          WHERE
3755              payment_id = p_paymentTab(i).payment_id
3756          ;
3757 
3758      END LOOP;
3759 
3760      print_debuginfo(l_module_name, 'EXIT');
3761 
3762  END updatePayments;
3763 
3764 /*--------------------------------------------------------------------
3765  | NAME:
3766  |     getNextPaymentID
3767  |
3768  | PURPOSE:
3769  |     Returns the next payment id from a sequence. These ids are
3770  |     used to uniquely number payments.
3771  |
3772  | PARAMETERS:
3773  |     IN
3774  |
3775  |
3776  |     OUT
3777  |
3778  |
3779  | RETURNS:
3780  |
3781  | NOTES:
3782  |
3783  *---------------------------------------------------------------------*/
3784  PROCEDURE getNextPaymentID(
3785      x_paymentID IN OUT NOCOPY IBY_PAYMENTS_ALL.payment_id%TYPE
3786      )
3787  IS
3788 
3789  BEGIN
3790 
3791      SELECT IBY_PAYMENTS_ALL_S.nextval INTO x_paymentID
3792          FROM DUAL;
3793 
3794  END getNextPaymentID;
3795 
3796 /*--------------------------------------------------------------------
3797  | NAME:
3798  |     updateDocsWithPaymentID
3799  |
3800  | PURPOSE:
3801  |     Updates individual documents in IBY_DOCS_PAYABLE_ALL table
3802  |     with payment ids from given PLSQL table.
3803  |
3804  | PARAMETERS:
3805  |     IN
3806  |     p_docsInPmtTab -
3807  |
3808  |     OUT
3809  |
3810  |
3811  | RETURNS:
3812  |
3813  | NOTES:
3814  |
3815  *---------------------------------------------------------------------*/
3816  PROCEDURE updateDocsWithPaymentID(
3817      p_docsInPmtTab  IN docsInPaymentTabType
3818      )
3819  IS
3820  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
3821                                              '.updateDocsWithPaymentID';
3822 
3823  BEGIN
3824 
3825      print_debuginfo(l_module_name, 'ENTER');
3826 
3827      /* Normally, this should not happen */
3828      IF (p_docsInPmtTab.COUNT = 0) THEN
3829          print_debuginfo(l_module_name, 'After grouping, no '
3830              || 'payments/documents provided to update '
3831              || 'IBY_DOCS_PAYABLE_ALL table. Possible data '
3832              || 'corruption issue.');
3833          RETURN;
3834      END IF;
3835 
3836      /*
3837       * Update the documents. We cannot use bulk update here
3838       * because the bulk update syntax does not allow us to
3839       * reference individual fields of the PL/SQL record.
3840       *
3841       * By default, set the formatting payment it for a
3842       * document same as it's payment id; the payment
3843       * instruction creation program will update the
3844       * formatting payment id for overflow documents.
3845       *
3846       * TBD: Is there any way to optimize this update?
3847       */
3848      FOR i in p_docsInPmtTab.FIRST..p_docsInPmtTab.LAST LOOP
3849 
3850          print_debuginfo(l_module_name, 'Payment: '
3851              || p_docsInPmtTab(i).payment_id || ', document: '
3852              || p_docsInPmtTab(i).document_id);
3853 
3854          UPDATE
3855              IBY_DOCS_PAYABLE_ALL
3856          SET
3857              payment_id            = p_docsInPmtTab(i).payment_id,
3858              formatting_payment_id = p_docsInPmtTab(i).payment_id,
3859              document_status       = p_docsInPmtTab(i).document_status,
3860 
3861              /*
3862               * Document payment amount might have gotten
3863               * changed during credit memo handling. So we
3864               * need to update document amount from PLSQL
3865               * table.
3866               */
3867              payment_amount       = p_docsInPmtTab(i).document_amount,
3868 	     payment_curr_discount_taken = p_docsInPmtTab(i).pmt_curr_discount,
3869 
3870              /*
3871               * Bank charge amount and amount withheld might have
3872               * been supplied by external hook.
3873               */
3874              amount_withheld       = p_docsInPmtTab(i).amount_withheld,
3875 
3876              /*
3877               * Fix for bug 4405981:
3878               *
3879               * The straight through flag should be set to 'N',
3880               * if the document was rejected / required manual
3881               * intervention.
3882               */
3883              straight_through_flag =
3884                     DECODE(
3885                            p_docsInPmtTab(i).document_status,
3886                            DOC_STATUS_CA_FAILED,      'N',
3887                            DOC_STATUS_RELN_FAIL,      'N',
3888                            DOC_STATUS_FAIL_BY_REJLVL, 'N',
3889                            DOC_STATUS_PAY_VAL_FAIL,   'N',
3890                            'Y'
3891                            )
3892          WHERE
3893              document_payable_id   = p_docsInPmtTab(i).document_id
3894          ;
3895 
3896      END LOOP;
3897 
3898      print_debuginfo(l_module_name, 'EXIT');
3899 
3900  END updateDocsWithPaymentID;
3901 
3902 /*--------------------------------------------------------------------
3903  | NAME:
3904  |     performCentralBankReporting
3905  |
3906  | PURPOSE:
3907  |     Checks if a payment needs to be reported to the central bank.
3908  |     If central bank reporting is needed, this method will set the
3909  |     'declare payment flag' on the payment.
3910  |
3911  | PARAMETERS:
3912  |     IN
3913  |
3914  |     OUT
3915  |
3916  |
3917  | RETURNS:
3918  |
3919  | NOTES:
3920  |
3921  *---------------------------------------------------------------------*/
3922  PROCEDURE performCentralBankReporting(
3923      x_paymentTab             IN OUT NOCOPY paymentTabType,
3924      x_docsInPmtTab           IN OUT NOCOPY docsInPaymentTabType
3925 --  ,     p_cbrTab                 IN            centralBankReportTabType
3926      )
3927  IS
3928 
3929  l_module_name    CONSTANT VARCHAR2(200) := G_PKG_NAME
3930                                              || '.performCentralBankReporting';
3931  l_pmt_fx_amount  iby_payments_all.payment_amount%TYPE := -1;
3932  l_return_status  VARCHAR2(200);
3933  l_declare_flag   VARCHAR2(1);
3934  l_profile_id     IBY_PAYMENT_PROFILES.payment_profile_id%type;
3935 
3936  BEGIN
3937 
3938      print_debuginfo(l_module_name, 'ENTER');
3939 
3940      /* for each payment */
3941      FOR i in  x_paymentTab.FIRST ..  x_paymentTab.LAST LOOP
3942 
3943         l_profile_id :=  x_paymentTab(i).payment_profile_id;
3944         IF NOT(iby_disburse_submit_pub_pkg.paymentProfilesTab.exists(l_profile_id)) THEN
3945            iby_disburse_submit_pub_pkg.set_profile_attribs(l_profile_id);
3946         END IF;
3947 
3948            /*
3949             * Check if the payment needs to be declared to the
3950             * central bank.
3951             */
3952         IF (iby_disburse_submit_pub_pkg.paymentProfilesTab(l_profile_id).declaration_option =
3953                      'DECLARE_DIRECT_TO_CENTRAL_BANK') THEN
3954 
3955               /*
3956                * Perform central bank reporting only for non-failed
3957                * payments.
3958                */
3959                IF (x_paymentTab(i).payment_status <>
3960                    PAY_STATUS_REJECTED) THEN
3961 
3962                   /*
3963                    * Fix for bug 5221692:
3964                    *
3965                    * User can customize the logic to determine
3966                    * whether central bank reporting is required
3967                    * by implementing the default stub function
3968                    * IBY_EXTENSIBILITY_CALLOUTS_PUB.
3969                    *     isCentralBankReportingRequired(..).
3970                    *
3971                    * If this stub is implemented use the value
3972                    * returned by the stub, else use the declaration
3973                    * threshold amount to determine whether to report
3974                    * payment to central bank.
3975                    */
3976                   print_debuginfo(l_module_name, 'Attempting to '
3977                              || 'invoke customizable function '
3978                              || 'IBY_EXTENSIBILITY_CALLOUTS_PUB.'
3979                              || 'isCentralBankReportingRequired(..) for '
3980                              || 'payment '
3981                              || x_paymentTab(i).payment_id
3982                              );
3983 
3984                   l_declare_flag := IBY_EXTENSIBILITY_CALLOUTS_PUB.
3985                                          isCentralBankReportingRequired(
3986                                             x_paymentTab(i).payment_id,
3987                                             l_return_status
3988                                             );
3989 
3990                   IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
3991 
3992                      print_debuginfo(l_module_name, 'Callout '
3993                                  || 'IBY_EXTENSIBILITY_CALLOUTS_PUB.'
3994                                  || 'isCentralBankReportingRequired(..) for '
3995                                  || 'payment '
3996                                  || x_paymentTab(i).payment_id
3997                                  || 'returned: '
3998                                  || l_return_status
3999                                  || '. Raising exception ..',
4000                                  FND_LOG.LEVEL_UNEXPECTED
4001                                  );
4002 
4003                      /*
4004                       * This should never happen. Raise an
4005                       * exception.
4006                       */
4007                      APP_EXCEPTION.RAISE_EXCEPTION;
4008 
4009                   ELSE
4010 
4011                      /*
4012                       * If callout returns 'Y' / 'N', use the return
4013                       * value. Else consider that the callout has
4014                       * not been implemented and use the threshold
4015                       * amount to determine whether to declare the
4016                       * payment.
4017                       */
4018                      IF (l_declare_flag = 'Y' OR
4019                          l_declare_flag = 'N') THEN
4020 
4021                         /*
4022                          * Set the declare flag specified by the
4023                          * callout.
4024                          */
4025                         print_debuginfo(l_module_name,
4026                                      'Flag returned by '
4027                                      || 'callout: '
4028                                      || l_declare_flag
4029                                      );
4030                         x_paymentTab(i).declare_payment_flag :=
4031                                      l_declare_flag;
4032 
4033                      ELSE
4034 
4035                         print_debuginfo(l_module_name,  'Stubbed '
4036                                      || 'customizable function '
4037                                      || 'IBY_EXTENSIBILITY_CALLOUTS_PUB.'
4038                                      || 'isCentralBankReportingRequired(..) '
4039                                      || 'not implemented. Checking whether '
4040                                      || 'payment amount exceeds declaration '
4041                                      || 'threshold ..'
4042                                      );
4043 
4044                         /*
4045                          * If we reached here, it means user has
4046                          * not implemented custom logic for
4047                          * determining whether central bank
4048                          * reporting is needed.
4049                          *
4050                          * We will determine whether central
4051                          * bank reporting is needed by checking
4052                          * if the payment exceeds user specified
4053                          * amount threshold.
4054                          */
4055 
4056                          /*
4057                          * Step 2:
4058                          * Calculate the reporting amount at the
4059                          * payment level. Reporting amount is the
4060                          * payment amount converted to reporting
4061                          * currency.
4062                          *
4063                          * NOTE: We call a GL API to perform the
4064                          * currency conversion.
4065                          */
4066 
4067        --
4068        -- Use the dummy call for testing purposes.
4069        --
4070        --
4071        --dummyGLAPI(
4072        --    sysdate,                         /* IN:  exchange rate date */
4073        --    x_paymentTab(i).payment_amount,  /* IN:  source amount */
4074        --    x_paymentTab(i).payment_currency,/* IN:  source currency */
4075        --    p_cbrTab(j).decl_curr_code,      /* IN:  target currency */
4076        --    p_cbrTab(j).decl_curr_fx_rate_type, /* IN:  exchange rate type */
4077        --    l_pmt_fx_amount               /* OUT: amt in target currency */
4078        --    );
4079 
4080                          print_debuginfo(l_module_name,
4081                                      'Payment #: '
4082                                      || i
4083                                      || ', payment amount: '
4084                                      || x_paymentTab(i).payment_amount
4085                                      || ', payment currency: '
4086                                      || x_paymentTab(i).payment_currency_code
4087                                      || ', declaration currency: '
4088                                      || iby_disburse_submit_pub_pkg.paymentProfilesTab(l_profile_id).declaration_currency_code
4089                                      || ', exchange rate type: '
4090                                      || iby_disburse_submit_pub_pkg.paymentProfilesTab(l_profile_id).declaration_curr_fx_rate_type
4091                                      );
4092 
4093                                  /* OUT: target amount */
4094                          l_pmt_fx_amount := getFxAmount(
4095 
4096                                      /* IN:  source currency */
4097                                      x_paymentTab(i).payment_currency_code,
4098 
4099                                      /* IN:  target currency */
4100                                      iby_disburse_submit_pub_pkg.paymentProfilesTab(l_profile_id).declaration_currency_code,
4101 
4102                                      /* IN:  exchange rate date */
4103                                      sysdate,
4104 
4105                                      /* IN:  exchange rate type */
4106                                      iby_disburse_submit_pub_pkg.paymentProfilesTab(l_profile_id).declaration_curr_fx_rate_type,
4107 
4108                                      /* IN:  source amount */
4109                                      x_paymentTab(i).payment_amount
4110                                      );
4111 
4112                          print_debuginfo(l_module_name,
4113                                      'Declaration amount '
4114                                      || 'in reporting currency '
4115                                      || iby_disburse_submit_pub_pkg.paymentProfilesTab(l_profile_id).declaration_currency_code
4116                                      || ' derived from calling from GL API: '
4117                                      || l_pmt_fx_amount
4118                                      );
4119 
4120                          print_debuginfo(l_module_name,
4121                                      'Declaration amount '
4122                                      || 'threshold in reporting currency: '
4123                                      || iby_disburse_submit_pub_pkg.paymentProfilesTab(l_profile_id).declaration_threshold_amount
4124                                      );
4125 
4126                          IF (l_pmt_fx_amount = -1) THEN
4127 
4128                             /*
4129                              * Error occurred in call to GL API
4130                              * to convert amount. Raise exception.
4131                              */
4132                              print_debuginfo(l_module_name, 'Reporting '
4133                                          || 'amount is -1. '
4134                                          || 'Raising exception.',
4135                                          FND_LOG.LEVEL_UNEXPECTED
4136                                          );
4137 
4138                              APP_EXCEPTION.RAISE_EXCEPTION;
4139 
4140                          ELSE
4141 
4142                              x_paymentTab(i).declaration_amount :=
4143                                          l_pmt_fx_amount;
4144 
4145                          END IF;
4146 
4147                          /*
4148                           * Step 3:
4149                           * If the reporting amount on the payment is
4150                           * greater than the reporting threshold, set
4151                           * the 'report payment flag' on the payment.
4152                           */
4153 
4154                          /*
4155                           * Set the declare flag based on amount
4156                           * threshold.
4157                           */
4158 
4159                          IF (l_pmt_fx_amount >
4160                              iby_disburse_submit_pub_pkg.paymentProfilesTab(l_profile_id).declaration_threshold_amount) THEN
4161 
4162                             x_paymentTab(i).declare_payment_flag := 'Y';
4163 
4164                          ELSE
4165 
4166                             x_paymentTab(i).declare_payment_flag := 'N';
4167 
4168                          END IF;
4169 
4170                      END IF; -- if declare flag = Y / N
4171 
4172                   END IF; -- if return status <> SUCCESS
4173 
4174                   print_debuginfo(l_module_name, 'Payment #: '
4175                              || i
4176                              || ', declaration flag: '
4177                              || x_paymentTab(i).declare_payment_flag
4178                              );
4179 
4180                END IF; -- if payment has not failed validation
4181 
4182         END IF; -- if declaration option is set
4183 
4184      END LOOP; -- for each payment
4185 
4186      print_debuginfo(l_module_name, 'EXIT');
4187 
4188      EXCEPTION
4189 
4190          WHEN OTHERS THEN
4191              print_debuginfo(l_module_name, 'Fatal: Exception when attempting '
4192                  || 'to call GL API ''gl_currency_api.convert_amount()''.',
4193                  FND_LOG.LEVEL_UNEXPECTED
4194                  );
4195 
4196              print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE,
4197                  FND_LOG.LEVEL_UNEXPECTED);
4198              print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM,
4199                  FND_LOG.LEVEL_UNEXPECTED);
4200 
4201              /*
4202               * Propogate exception to caller.
4203               */
4204              RAISE;
4205 
4206  END performCentralBankReporting;
4207 
4208 /*--------------------------------------------------------------------
4209  | NAME:
4210  |     getFxAmount
4211  |
4212  | PURPOSE:
4213  |     Calls a GL API to convert a payment amount to the given
4214  |     currency.
4215  |
4216  | PARAMETERS:
4217  |     IN
4218  |
4219  |
4220  |     OUT
4221  |
4222  |
4223  | RETURNS:
4224  |
4225  | NOTES:
4226  |     No exception will be raised in this function if the call to
4227  |     GL API to convert the amount fails; Instead '-1' will be
4228  |     for the amount. The caller should recognize this and raise
4229  |     an exception.
4230  |
4231  *---------------------------------------------------------------------*/
4232  FUNCTION getFxAmount(
4233      p_source_currency   IN VARCHAR2,
4234      p_target_currency   IN VARCHAR2,
4235      p_exch_rate_date    IN DATE,
4236      p_exch_rate_type    IN VARCHAR2,
4237      p_source_amount     IN NUMBER
4238      ) RETURN NUMBER
4239 
4240  IS
4241  l_module_name  CONSTANT VARCHAR2(200)  := G_PKG_NAME || '.getFxAmount';
4242  l_fx_amount    NUMBER(15);
4243 
4244  BEGIN
4245 
4246     /*
4247      * Cannot log anywhere within this function because that
4248      * violates pragma restrict_references!
4249      */
4250 
4251     l_fx_amount := gl_currency_api.convert_amount(
4252                        p_source_currency,
4253                        p_target_currency,
4254                        p_exch_rate_date,
4255                        p_exch_rate_type,
4256                        p_source_amount
4257                        );
4258 
4259     RETURN l_fx_amount;
4260 
4261     EXCEPTION
4262 
4263         WHEN OTHERS THEN
4264              /*
4265               * The GL convert_amount() API enforces pragma
4266               * restrict_references. So we cannot raise an
4267               * exception here.
4268               *
4269               * Instead log the exception and pass -1 as the
4270               * amount. The caller should recognize that -1
4271               * indicates an exception occured.
4272               */
4273              l_fx_amount := -1;
4274 
4275              RETURN l_fx_amount;
4276 
4277  END getFxAmount;
4278 
4279 /*--------------------------------------------------------------------
4280  | NAME:
4281  |     performPaymentValidations
4282  |
4283  | PURPOSE:
4284  |     A wrapper method to call payment validation sets.
4285  |
4286  | PARAMETERS:
4287  |     IN
4288  |
4289  |     OUT
4290  |
4291  |
4292  | RETURNS:
4293  |
4294  | NOTES:
4295  |
4296  *---------------------------------------------------------------------*/
4297  PROCEDURE performPaymentValidations(
4298      x_paymentTab    IN OUT NOCOPY paymentTabType,
4299      x_docsInPmtTab  IN OUT NOCOPY docsInPaymentTabType,
4300      x_docErrorTab   IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.docErrorTabType,
4301      x_errTokenTab   IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.trxnErrTokenTabType
4302      )
4303  IS
4304 
4305  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
4306                                              || '.performPaymentValidation';
4307 
4308  BEGIN
4309 
4310      print_debuginfo(l_module_name, 'ENTER');
4311 
4312      applyPaymentValidationSets(x_paymentTab, x_docsInPmtTab,
4313          x_docErrorTab, x_errTokenTab);
4314 
4315      print_debuginfo(l_module_name, 'EXIT');
4316 
4317  END performPaymentValidations;
4318 
4319 /*--------------------------------------------------------------------
4320  | NAME:
4321  |     performPayReqBasedValidations
4322  |
4323  | PURPOSE:
4324  |     Validates created payments using payment request imposed
4325  |     criteria. These validations can be considered 'core' payment
4326  |     validations because they will always be applied.
4327  |
4328  |     The validations linked to the validation sets will be applied
4329  |     in addition to these core validations.
4330  |
4331  | PARAMETERS:
4332  |     IN
4333  |
4334  |     OUT
4335  |
4336  |
4337  | RETURNS:
4338  |
4339  | NOTES:
4340  |
4341  *---------------------------------------------------------------------*/
4342  PROCEDURE performPayReqBasedValidations(
4343      p_payReqCriteria  IN            payReqImposedCriteria,
4344      x_paymentTab      IN OUT NOCOPY paymentTabType,
4345      x_docsInPmtTab    IN OUT NOCOPY docsInPaymentTabType,
4346      x_docErrorTab     IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.docErrorTabType,
4347      x_errTokenTab     IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.trxnErrTokenTabType
4348      )
4349  IS
4350 
4351  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
4352                                              '.performPayReqBasedValidations';
4353  l_max_pmt_limit         NUMBER(15);
4354  l_min_pmt_limit         NUMBER(15);
4355  l_allow_zero_pmts_flag  VARCHAR2(1) := 'N';
4356 
4357  l_ce_min_pmt_ctrl       NUMBER(15);
4358  l_ce_max_pmt_ctrl       NUMBER(15);
4359 
4360  l_doc_err_rec   IBY_TRANSACTION_ERRORS%ROWTYPE;
4361 
4362  l_error_code    VARCHAR2(100);
4363  l_error_msg     VARCHAR2(500);
4364  l_token_rec     IBY_TRXN_ERROR_TOKENS%ROWTYPE;
4365 
4366  l_profile_id    IBY_PAYMENT_PROFILES.payment_profile_id%type;
4367  l_disabled_ppp  VARCHAR2(1) := 'N';
4368  BEGIN
4369 
4370      print_debuginfo(l_module_name, 'ENTER');
4371 
4372      l_max_pmt_limit        := p_payReqCriteria.max_pmt_amt_limit;
4373      l_min_pmt_limit        := p_payReqCriteria.min_pmt_amt_limit;
4374      l_allow_zero_pmts_flag := p_payReqCriteria.allow_zero_pmts_flag;
4375 
4376      print_debuginfo(l_module_name, 'Max payment amount limit: '
4377          || l_max_pmt_limit);
4378      print_debuginfo(l_module_name, 'Min payment amount limit: '
4379          || l_min_pmt_limit);
4380      print_debuginfo(l_module_name, 'Zero payments flag: '
4381          || l_allow_zero_pmts_flag);
4382 
4383      /*
4384       * For all payments:
4385       * If payment has not already failed validation,
4386       * then check payment against payment request
4387       * imposed conditions. These are:
4388       *
4389       * MAX PAYMENT AMOUNT
4390       * MIN PAYMENT AMOUNT
4391       * ZERO PAYMENTS ALLOWED FLAG
4392       */
4393      FOR i in x_paymentTab.FIRST ..  x_paymentTab.LAST LOOP
4394 
4395          IF (x_paymentTab(i).payment_status = PAY_STATUS_CREATED) THEN
4396 
4397 /*  Bug 5997589
4398     Ensuring that payments with disabled payment profiles are failed.
4399 */
4400 /*  Bug 5709596 */
4401             l_profile_id := x_paymentTab(i).payment_profile_id;
4402             IF NOT(iby_disburse_submit_pub_pkg.paymentProfilesTab.exists(l_profile_id)) THEN
4403                iby_disburse_submit_pub_pkg.set_profile_attribs(l_profile_id);
4404             END IF;
4405 
4406             IF (iby_disburse_submit_pub_pkg.paymentProfilesTab(l_profile_id).inactive_date
4407                 IS NOT NULL
4408                AND
4409                 iby_disburse_submit_pub_pkg.paymentProfilesTab(l_profile_id).inactive_date
4410                 <= sysdate) THEN
4411                l_disabled_ppp := 'Y';
4412             ELSE
4413                l_disabled_ppp := 'N';
4414             END IF;
4415 
4416             IF (l_disabled_ppp = 'Y') THEN
4417                 x_paymentTab(i).payment_status
4418                      := PAY_STATUS_REJECTED;
4419 
4420                print_debuginfo(l_module_name, 'Failed payment '
4421                      || x_paymentTab(i).payment_id
4422                      || ' because disabled PPP '
4423                      || x_paymentTab(i).payment_profile_id
4424                      || ' is associate with it.'
4425                      );
4426 
4427                l_error_code := 'IBY_PMT_INVALID_PROFILE';
4428                FND_MESSAGE.set_name('IBY', l_error_code);
4429 
4430                FND_MESSAGE.SET_TOKEN('PMT_NUMBER',
4431                      x_paymentTab(i).payment_id,
4432                      FALSE);
4433 
4434                l_token_rec.token_name  := 'PMT_NUMBER';
4435                l_token_rec.token_value := x_paymentTab(i).payment_id;
4436                x_errTokenTab(x_errTokenTab.COUNT + 1) := l_token_rec;
4437 
4438                  /*
4439                   * Once we fail a payment, we need to create
4440                   * an error record and insert this record
4441                   * into the errors table.
4442                   */
4443                IBY_BUILD_UTILS_PKG.createPmtErrorRecord(
4444                      x_paymentTab(i).payment_id,
4445                      x_paymentTab(i).payment_status,
4446                      l_error_code,
4447                      FND_MESSAGE.get,
4448                      l_doc_err_rec
4449                      );
4450 
4451                IBY_VALIDATIONSETS_PUB.insertIntoErrorTable(
4452                      l_doc_err_rec, x_docErrorTab, x_errTokenTab);
4453 
4454                  /* fail the docs of this payment */
4455                  failDocsOfPayment(x_paymentTab(i).payment_id,
4456                      DOC_STATUS_PAY_VAL_FAIL, x_docsInPmtTab,
4457                      x_docErrorTab, x_errTokenTab);
4458             END IF;  -- l_disabled_ppp
4459 
4460             IF (l_max_pmt_limit <> -1 AND
4461                  x_paymentTab(i).payment_amount > l_max_pmt_limit) THEN
4462 
4463                  x_paymentTab(i).payment_status
4464                      := PAY_STATUS_REJECTED;
4465 
4466                  print_debuginfo(l_module_name, 'Failed payment '
4467                      || x_paymentTab(i).payment_id
4468                      || ' because payment amount '
4469                      || x_paymentTab(i).payment_amount
4470                      || ' exceeds limit '
4471                      || l_max_pmt_limit
4472                      || ' imposed by payment request.'
4473                      );
4474 
4475                  l_error_code := 'IBY_PMT_ABOVE_LIMIT';
4476                  FND_MESSAGE.set_name('IBY', l_error_code);
4477 
4478                  FND_MESSAGE.SET_TOKEN('PMT_AMOUNT',
4479                      x_paymentTab(i).payment_amount,
4480                      FALSE);
4481 
4482                  l_token_rec.token_name  := 'PMT_AMOUNT';
4483                  l_token_rec.token_value := x_paymentTab(i).payment_amount;
4484                  x_errTokenTab(x_errTokenTab.COUNT + 1) := l_token_rec;
4485 
4486                  FND_MESSAGE.SET_TOKEN('UPPER_LIMIT',
4487                      l_max_pmt_limit,
4488                      FALSE);
4489 
4490                  l_token_rec.token_name  := 'UPPER_LIMIT';
4491                  l_token_rec.token_value := l_max_pmt_limit;
4492                  x_errTokenTab(x_errTokenTab.COUNT + 1) := l_token_rec;
4493 
4494                  /*
4495                   * Once we fail a payment, we need to create
4496                   * an error record and insert this record
4497                   * into the errors table.
4498                   */
4499                  IBY_BUILD_UTILS_PKG.createPmtErrorRecord(
4500                      x_paymentTab(i).payment_id,
4501                      x_paymentTab(i).payment_status,
4502                      l_error_code,
4503                      FND_MESSAGE.get,
4504                      l_doc_err_rec
4505                      );
4506 
4507                  IBY_VALIDATIONSETS_PUB.insertIntoErrorTable(
4508                      l_doc_err_rec, x_docErrorTab, x_errTokenTab);
4509 
4510                  /* fail the docs of this payment */
4511                  failDocsOfPayment(x_paymentTab(i).payment_id,
4512                      DOC_STATUS_PAY_VAL_FAIL, x_docsInPmtTab,
4513                      x_docErrorTab, x_errTokenTab);
4514 
4515              END IF;
4516 
4517              IF (l_min_pmt_limit <> -1 AND
4518                  x_paymentTab(i).payment_amount < l_min_pmt_limit) THEN
4519 
4520                  x_paymentTab(i).payment_status
4521                      := PAY_STATUS_REJECTED;
4522 
4523                  print_debuginfo(l_module_name, 'Failed payment '
4524                      || x_paymentTab(i).payment_id
4525                      || ' because payment amount '
4526                      || x_paymentTab(i).payment_amount
4527                      || ' is lower than limit '
4528                      || l_min_pmt_limit
4529                      || ' imposed by payment request.'
4530                      );
4531 
4532                  l_error_code := 'IBY_PMT_BELOW_LIMIT';
4533                  FND_MESSAGE.set_name('IBY', l_error_code);
4534 
4535                  FND_MESSAGE.SET_TOKEN('PMT_AMOUNT',
4536                      x_paymentTab(i).payment_amount,
4537                      FALSE);
4538 
4539                  l_token_rec.token_name  := 'PMT_AMOUNT';
4540                  l_token_rec.token_value := x_paymentTab(i).payment_amount;
4541                  x_errTokenTab(x_errTokenTab.COUNT + 1) := l_token_rec;
4542 
4543                  FND_MESSAGE.SET_TOKEN('LOWER_LIMIT',
4544                      l_min_pmt_limit,
4545                      FALSE);
4546 
4547                  l_token_rec.token_name  := 'LOWER_LIMIT';
4548                  l_token_rec.token_value := l_min_pmt_limit;
4549                  x_errTokenTab(x_errTokenTab.COUNT + 1) := l_token_rec;
4550 
4551                  /*
4552                   * Once we fail a payment, we need to create
4553                   * an error record and insert this record
4554                   * into the errors table.
4555                   */
4556                  IBY_BUILD_UTILS_PKG.createPmtErrorRecord(
4557                      x_paymentTab(i).payment_id,
4558                      x_paymentTab(i).payment_status,
4559                      l_error_code,
4560                      FND_MESSAGE.get,
4561                      l_doc_err_rec
4562                      );
4563 
4564                  IBY_VALIDATIONSETS_PUB.insertIntoErrorTable(
4565                      l_doc_err_rec, x_docErrorTab, x_errTokenTab);
4566 
4567                  /* fail the docs of this payment */
4568                  failDocsOfPayment(x_paymentTab(i).payment_id,
4569                      DOC_STATUS_PAY_VAL_FAIL, x_docsInPmtTab,
4570                      x_docErrorTab, x_errTokenTab);
4571 
4572              END IF;
4573 
4574              IF (UPPER(l_allow_zero_pmts_flag) <> 'Y' AND
4575                  x_paymentTab(i).payment_amount = 0) THEN
4576 
4577                  x_paymentTab(i).payment_status
4578                      := PAY_STATUS_REJECTED;
4579 
4580                  print_debuginfo(l_module_name, 'Failed payment '
4581                      || x_paymentTab(i).payment_id
4582                      || ' because payment amount '
4583                      || x_paymentTab(i).payment_amount
4584                      || ' is zero. Zero payments not'
4585                      || ' allowed by this payment request.'
4586                      );
4587 
4588                  l_error_code := 'IBY_ZERO_PMT_NOT_ALLOWED';
4589                  FND_MESSAGE.set_name('IBY', l_error_code);
4590 
4591                  /*
4592                   * Once we fail a payment, we need to create
4593                   * an error record and insert this record
4594                   * into the errors table.
4595                   */
4596                  IBY_BUILD_UTILS_PKG.createPmtErrorRecord(
4597                      x_paymentTab(i).payment_id,
4598                      x_paymentTab(i).payment_status,
4599                      l_error_code,
4600                      FND_MESSAGE.get,
4601                      l_doc_err_rec
4602                      );
4603 
4604                  IBY_VALIDATIONSETS_PUB.insertIntoErrorTable(
4605                      l_doc_err_rec, x_docErrorTab, x_errTokenTab);
4606 
4607                  /* fail the docs of this payment */
4608                  failDocsOfPayment(x_paymentTab(i).payment_id,
4609                      DOC_STATUS_PAY_VAL_FAIL, x_docsInPmtTab,
4610                      x_docErrorTab, x_errTokenTab);
4611 
4612              END IF;
4613 
4614 
4615              /*
4616               * Fix for bug 5760241:
4617               *
4618               * Validate the payment for maximum and minimum
4619               * payment amounts based on the internal bank
4620               * account payment controls set in CE.
4621               */
4622 
4623              /*
4624               * First, get the minimum and maximum payment
4625               * control amount values from CE for the int
4626               * bank account on the payment.
4627               */
4628              SELECT
4629                  min_check_amount,
4630                  max_check_amount
4631              INTO
4632                  l_ce_min_pmt_ctrl,
4633                  l_ce_max_pmt_ctrl
4634              FROM
4635                  CE_BANK_ACCOUNTS
4636              WHERE
4637                  bank_account_id =  x_paymentTab(i).internal_bank_account_id
4638              ;
4639 
4640              print_debuginfo(l_module_name, 'Payment id: '
4641                  || x_paymentTab(i).payment_id
4642                  || ', int bank account id: '
4643                  || x_paymentTab(i).internal_bank_account_id
4644                  || ', CE min pmt amount control: '
4645                  || l_ce_min_pmt_ctrl
4646                  || ', CE max pmt amount control: '
4647                  || l_ce_max_pmt_ctrl
4648                  );
4649 
4650              /*
4651               * Next, validate payment against minimum
4652               * payment amount control.
4653               */
4654              IF (l_ce_min_pmt_ctrl IS NOT NULL AND
4655                  x_paymentTab(i).payment_amount < l_ce_min_pmt_ctrl) THEN
4656 
4657                  x_paymentTab(i).payment_status
4658                      := PAY_STATUS_REJECTED;
4659 
4660                  print_debuginfo(l_module_name, 'Failed payment '
4661                      || x_paymentTab(i).payment_id
4662                      || ' because payment amount '
4663                      || x_paymentTab(i).payment_amount
4664                      || ' is lower than limit '
4665                      || l_ce_min_pmt_ctrl
4666                      || ' imposed by Cash Management (CE).'
4667                      );
4668 
4669                  l_error_code := 'IBY_PMT_BELOW_CE_LIMIT';
4670                  FND_MESSAGE.set_name('IBY', l_error_code);
4671 
4672                  FND_MESSAGE.SET_TOKEN('PMT_AMOUNT',
4673                      x_paymentTab(i).payment_amount,
4674                      FALSE);
4675 
4676                  l_token_rec.token_name  := 'PMT_AMOUNT';
4677                  l_token_rec.token_value := x_paymentTab(i).payment_amount;
4678                  x_errTokenTab(x_errTokenTab.COUNT + 1) := l_token_rec;
4679 
4680                  FND_MESSAGE.SET_TOKEN('LOWER_LIMIT',
4681                      l_ce_min_pmt_ctrl,
4682                      FALSE);
4683 
4684                  l_token_rec.token_name  := 'LOWER_LIMIT';
4685                  l_token_rec.token_value := l_ce_min_pmt_ctrl;
4686                  x_errTokenTab(x_errTokenTab.COUNT + 1) := l_token_rec;
4687 
4688                  /*
4689                   * Once we fail a payment, we need to create
4690                   * an error record and insert this record
4691                   * into the errors table.
4692                   */
4693                  IBY_BUILD_UTILS_PKG.createPmtErrorRecord(
4694                      x_paymentTab(i).payment_id,
4695                      x_paymentTab(i).payment_status,
4696                      l_error_code,
4697                      FND_MESSAGE.get,
4698                      l_doc_err_rec
4699                      );
4700 
4701                  IBY_VALIDATIONSETS_PUB.insertIntoErrorTable(
4702                      l_doc_err_rec, x_docErrorTab, x_errTokenTab);
4703 
4704                  /* fail the docs of this payment */
4705                  failDocsOfPayment(x_paymentTab(i).payment_id,
4706                      DOC_STATUS_PAY_VAL_FAIL, x_docsInPmtTab,
4707                      x_docErrorTab, x_errTokenTab);
4708 
4709              END IF;
4710 
4711              /*
4712               * Next, validate payment against maximum
4713               * payment amount control.
4714               */
4715              IF (l_ce_max_pmt_ctrl IS NOT NULL AND
4716                  x_paymentTab(i).payment_amount > l_ce_max_pmt_ctrl) THEN
4717 
4718                  x_paymentTab(i).payment_status
4719                      := PAY_STATUS_REJECTED;
4720 
4721                  print_debuginfo(l_module_name, 'Failed payment '
4722                      || x_paymentTab(i).payment_id
4723                      || ' because payment amount '
4724                      || x_paymentTab(i).payment_amount
4725                      || ' is greater than limit '
4726                      || l_ce_max_pmt_ctrl
4727                      || ' imposed by Cash Management (CE).'
4728                      );
4729 
4730                  l_error_code := 'IBY_PMT_ABOVE_CE_LIMIT';
4731                  FND_MESSAGE.set_name('IBY', l_error_code);
4732 
4733                  FND_MESSAGE.SET_TOKEN('PMT_AMOUNT',
4734                      x_paymentTab(i).payment_amount,
4735                      FALSE);
4736 
4737                  l_token_rec.token_name  := 'PMT_AMOUNT';
4738                  l_token_rec.token_value := x_paymentTab(i).payment_amount;
4739                  x_errTokenTab(x_errTokenTab.COUNT + 1) := l_token_rec;
4740 
4741                  FND_MESSAGE.SET_TOKEN('UPPER_LIMIT',
4742                      l_ce_max_pmt_ctrl,
4743                      FALSE);
4744 
4745                  l_token_rec.token_name  := 'UPPER_LIMIT';
4746                  l_token_rec.token_value := l_ce_max_pmt_ctrl;
4747                  x_errTokenTab(x_errTokenTab.COUNT + 1) := l_token_rec;
4748 
4749                  /*
4750                   * Once we fail a payment, we need to create
4751                   * an error record and insert this record
4752                   * into the errors table.
4753                   */
4754                  IBY_BUILD_UTILS_PKG.createPmtErrorRecord(
4755                      x_paymentTab(i).payment_id,
4756                      x_paymentTab(i).payment_status,
4757                      l_error_code,
4758                      FND_MESSAGE.get,
4759                      l_doc_err_rec
4760                      );
4761 
4762                  IBY_VALIDATIONSETS_PUB.insertIntoErrorTable(
4763                      l_doc_err_rec, x_docErrorTab, x_errTokenTab);
4764 
4765                  /* fail the docs of this payment */
4766                  failDocsOfPayment(x_paymentTab(i).payment_id,
4767                      DOC_STATUS_PAY_VAL_FAIL, x_docsInPmtTab,
4768                      x_docErrorTab, x_errTokenTab);
4769 
4770              END IF;
4771 
4772          END IF; -- if pmt status = CREATED
4773 
4774      END LOOP;
4775 
4776      print_debuginfo(l_module_name, 'EXIT');
4777 
4778  END performPayReqBasedValidations;
4779 
4780 /*--------------------------------------------------------------------
4781  | NAME:
4782  |     performPmtGrpNumberValidation
4783  |
4784  | PURPOSE:
4785  |
4786  |
4787  |
4788  | PARAMETERS:
4789  |     IN
4790  |
4791  |     OUT
4792  |
4793  |
4794  | RETURNS:
4795  |
4796  | NOTES:
4797  |
4798  *---------------------------------------------------------------------*/
4799  PROCEDURE performPmtGrpNumberValidation(
4800      x_paymentTab     IN OUT NOCOPY paymentTabType,
4801      x_docsInPmtTab   IN OUT NOCOPY docsInPaymentTabType,
4802      x_docErrorTab    IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.docErrorTabType,
4803      x_errTokenTab    IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.trxnErrTokenTabType
4804      )
4805  IS
4806 
4807  l_orig_pmt_id       IBY_PAYMENTS_ALL.payment_id%TYPE;
4808  l_orig_doc_id       IBY_DOCS_PAYABLE_ALL.document_payable_id%TYPE;
4809  l_orig_pmt_grp_num  IBY_DOCS_PAYABLE_ALL.payment_grouping_number%TYPE;
4810 
4811  l_compare_pmt_id    IBY_PAYMENTS_ALL.payment_id%TYPE;
4812  l_compare_doc_id    IBY_DOCS_PAYABLE_ALL.document_payable_id%TYPE;
4813 
4814  l_doc_err_rec       IBY_TRANSACTION_ERRORS%ROWTYPE;
4815 
4816  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
4817                                              '.performPmtGrpNumberValidation';
4818 
4819  BEGIN
4820 
4821      print_debuginfo(l_module_name, 'ENTER');
4822 
4823      /*
4824       * All documents payable with the same payment grouping number
4825       * must be part of a single payment.
4826       *
4827       * When grouping documents into payments, if multiple payments
4828       * have documents with the same payment grouping number, it means
4829       * that documents with the same payment grouping number have been
4830       * distributed across payments. This is not allowed; In this
4831       * situation, fail all the payments that have documents with the
4832       * same payment grouping number, and fail their constituent documents
4833       * as well.
4834       */
4835 
4836      /* for each created payment in request */
4837      FOR i in x_paymentTab.FIRST .. x_paymentTab.LAST LOOP
4838 
4839          /*
4840           * Use only non-failed payments for checking the
4841           * payment grouping number.
4842           */
4843          IF (x_paymentTab(i).payment_status = PAY_STATUS_CREATED) THEN
4844 
4845              l_orig_pmt_id := x_paymentTab(i).payment_id;
4846 
4847              /*
4848               * Scan through all available documents to find documents
4849               * that are part of this non-failed payment
4850               */
4851              IF (x_docsInPmtTab.COUNT > 0) THEN
4852              FOR j in x_docsInPmtTab.FIRST .. x_docsInPmtTab.LAST LOOP
4853 
4854                  /* if we found a document of this payment */
4855                  IF (x_paymentTab(i).payment_id =
4856                      x_docsInPmtTab(j).payment_id) THEN
4857 
4858                      /*
4859                       * Store this document id and the payment grouping
4860                       * number of this document for comparison with
4861                       * the documents of other payments.
4862                       */
4863                      l_orig_doc_id      := x_docsInPmtTab(j).document_id;
4864                      l_orig_pmt_grp_num := x_docsInPmtTab(j).payment_grp_num;
4865 
4866                      /*
4867                       * From the list of created payments, find the next
4868                       * payment that is different from the payment that
4869                       * we began with.
4870                       */
4871                      FOR k in x_paymentTab.FIRST .. x_paymentTab.LAST LOOP
4872 
4873                          /*
4874                           * If we found a payment different from the original
4875                           * one make sure that it is non-failed payment.
4876                           */
4877                          IF (x_paymentTab(k).payment_id <> l_orig_pmt_id AND
4878                              x_paymentTab(k).payment_status =
4879                                  PAY_STATUS_CREATED) THEN
4880 
4881                              /* this means we got a new non-failed payment */
4882                              /* get the documents of this payment */
4883 
4884                              FOR m in x_docsInPmtTab.FIRST ..
4885                                  x_docsInPmtTab.LAST LOOP
4886 
4887                                  /*
4888                                   * We found a document of this new
4889                                   * non-failed payment
4890                                   */
4891                                  IF (x_docsInPmtTab(m).payment_id =
4892                                      x_paymentTab(k).payment_id) THEN
4893 
4894                                      /*
4895                                       * Check if the document of this
4896                                       * payment has the same payment grouping
4897                                       * number as the document of the
4898                                       * original payment
4899                                       */
4900                                      IF (x_docsInPmtTab(m).payment_grp_num =
4901                                          l_orig_pmt_grp_num) THEN
4902 
4903                                          /*
4904                                           * If so, we found documents across
4905                                           * payments sharing the same payment
4906                                           * grouping number. This is an error.
4907                                           * Log all the details.
4908                                           */
4909                                          l_compare_pmt_id :=
4910                                              x_paymentTab(k).payment_id;
4911 
4912                                          l_compare_doc_id := x_docsInPmtTab(m).
4913                                                                  document_id;
4914 
4915                                          print_debuginfo(l_module_name,
4916                                              'Document '
4917                                              || l_compare_doc_id
4918                                              || ' of payment '
4919                                              || l_compare_pmt_id
4920                                              || ' has the same payment '
4921                                              || 'grouping number '
4922                                              || l_orig_pmt_grp_num
4923                                              || ', as document '
4924                                              || l_orig_doc_id
4925                                              || ' of payment '
4926                                              || l_orig_pmt_id
4927                                              || '. This means that two '
4928                                              || 'documents with the same '
4929                                              || 'payment grouping '
4930                                              || 'number are grouped in '
4931                                              || 'different payments. '
4932                                              || 'Both payments '
4933                                              || 'will be failed.'
4934                                              );
4935 
4936                                          /*
4937                                           * Fail both payments
4938                                           */
4939 
4940                                          /* 1: fail the new payment */
4941                                          x_paymentTab(k).payment_status :=
4942                                              PAY_STATUS_REJECTED;
4943 
4944                                          /*
4945                                           * Once we fail a payment, we need
4946                                           * to create an error record and
4947                                           * insert this record into
4948                                           * the errors table.
4949                                           */
4950                                          IBY_BUILD_UTILS_PKG.createErrorRecord(
4951                                              TRXN_TYPE_PMT,
4952                                              x_paymentTab(k).payment_id,
4953                                              x_paymentTab(k).payment_status,
4954                                              NULL,
4955                                              x_paymentTab(k).payment_id,
4956                                              NULL,
4957                                              NULL,
4958                                              NULL,
4959                                              NULL,
4960                                              NULL,
4961                                              l_doc_err_rec,
4962                                              x_errTokenTab
4963                                              );
4964 
4965                                          IBY_VALIDATIONSETS_PUB.
4966                                              insertIntoErrorTable(
4967                                                  l_doc_err_rec, x_docErrorTab,
4968                                                  x_errTokenTab);
4969 
4970                                          /* fail the docs of this payment */
4971                                          failDocsOfPayment(
4972                                              x_paymentTab(k).payment_id,
4973                                              DOC_STATUS_PAY_VAL_FAIL,
4974                                              x_docsInPmtTab,
4975                                              x_docErrorTab,
4976                                              x_errTokenTab);
4977 
4978                                          /* 2: fail the original payment */
4979                                          x_paymentTab(i).payment_status :=
4980                                              PAY_STATUS_REJECTED;
4981 
4982                                          /*
4983                                           * Once we fail a payment, we need
4984                                           * to create an error record and
4985                                           * insert this record into
4986                                           * the errors table.
4987                                           */
4988                                          IBY_BUILD_UTILS_PKG.createErrorRecord(
4989                                              TRXN_TYPE_PMT,
4990                                              x_paymentTab(i).payment_id,
4991                                              x_paymentTab(i).payment_status,
4992                                              NULL,
4993                                              x_paymentTab(i).payment_id,
4994                                              NULL,
4995                                              NULL,
4996                                              NULL,
4997                                              NULL,
4998                                              NULL,
4999                                              l_doc_err_rec,
5000                                              x_errTokenTab
5001                                              );
5002 
5003                                          IBY_VALIDATIONSETS_PUB.
5004                                              insertIntoErrorTable(
5005                                                  l_doc_err_rec, x_docErrorTab,
5006                                                  x_errTokenTab);
5007 
5008                                          /* fail the docs of this payment */
5009                                          failDocsOfPayment(
5010                                              x_paymentTab(i).payment_id,
5011                                              DOC_STATUS_PAY_VAL_FAIL,
5012                                              x_docsInPmtTab,
5013                                              x_docErrorTab,
5014                                              x_errTokenTab);
5015 
5016                                      END IF; -- if payment grp num matched
5017 
5018                                  END IF; -- if doc belongs to this payment
5019 
5020                              END LOOP; -- loop through all documents
5021 
5022                          END IF; -- if this is a new payment
5023 
5024                      END LOOP; -- loop through all payments
5025 
5026                  END IF; -- if document has the same payment id
5027 
5028              END LOOP; -- loop through all documents
5029              END IF; -- if documents count > 0
5030 
5031          END IF; -- if payment is not in failed status
5032 
5033      END LOOP; -- for each payment in request
5034 
5035      print_debuginfo(l_module_name, 'EXIT');
5036 
5037  END performPmtGrpNumberValidation;
5038 
5039 /*--------------------------------------------------------------------
5040  | NAME:
5041  |     performPreHookProcess
5042  |
5043  | PURPOSE:
5044  |     Inserts all successfuly validated payments into
5045  |     IBY_HOOK_PAYMENTS_TEMP. This is a temporary table that the
5046  |     calling app can access to see what payments have been
5047  |     created. The calling app can also update the payments / fail
5048  |     the payments in the temporary table, and the payment creation
5049  |     program will update it's payments accordingly.
5050  |
5051  | PARAMETERS:
5052  |     IN
5053  |
5054  |     OUT
5055  |
5056  |
5057  | RETURNS:
5058  |
5059  | NOTES:
5060  |
5061  *---------------------------------------------------------------------*/
5062  PROCEDURE performPreHookProcess(
5063      p_cap_payreq_cd     IN            VARCHAR2,
5064      p_cap_id            IN            NUMBER,
5065      x_paymentTab        IN OUT NOCOPY paymentTabType,
5066      x_docsInPmtTab      IN OUT NOCOPY docsInPaymentTabType,
5067      x_hookPaymentTab    IN OUT NOCOPY hookPaymentTabType,
5068      x_hookDocsInPmtTab  IN OUT NOCOPY hookDocsInPaymentTabType
5069      )
5070  IS
5071 
5072  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME || '.performPreHookProcess';
5073  l_succPayIndx NUMBER := 1;
5074  l_succDocIndx NUMBER := 1;
5075 
5076 /*
5077  l_calling_app_ids                 t_calling_app_id;
5078  l_call_app_pay_service_req_cds    t_call_app_pay_service_req_cd;
5079  l_payment_service_request_ids     t_payment_service_request_id;
5080  l_payment_ids                     t_payment_id;
5081  l_payment_amounts                 t_payment_amount;
5082  l_payment_currency_codes          t_payment_currency_code;
5083  l_dont_pay_flags                  t_dont_pay_flag;
5084  l_dont_pay_reason_codes           t_dont_pay_reason_code;
5085  l_dont_pay_descriptions           t_dont_pay_description;
5086  l_internal_bank_account_ids       t_internal_bank_account_id;
5087  l_ext_payee_ids                   t_ext_payee_id;
5088  l_payee_party_ids                 t_payee_party_id;
5089  l_party_site_ids                  t_party_site_id;
5090  l_supplier_site_ids               t_supplier_site_id;
5091  l_org_ids                         t_org_id;
5092  l_org_types                       t_org_type;
5093  l_external_bank_account_ids       t_external_bank_account_id;
5094  l_discount_amount_takens          t_discount_amount_taken;
5095  l_payment_dates                   t_payment_date;
5096  l_bank_charge_amounts             t_bank_charge_amount;
5097  l_created_bys                     t_created_by;
5098  l_creation_dates                  t_creation_date;
5099  l_last_updated_bys                t_last_updated_by;
5100  l_last_update_dates               t_last_update_date;
5101  l_last_update_logins              t_last_update_login;
5102  l_object_version_numbers          t_object_version_number;
5103 */
5104 
5105  TYPE t_pmt_id IS TABLE OF
5106      IBY_HOOK_DOCS_IN_PMT_T.payment_id%TYPE
5107      INDEX BY BINARY_INTEGER;
5108  TYPE t_document_payable_id IS TABLE OF
5109      IBY_HOOK_DOCS_IN_PMT_T.document_payable_id%TYPE
5110      INDEX BY BINARY_INTEGER;
5111  TYPE t_call_app_id IS TABLE OF
5112      IBY_HOOK_DOCS_IN_PMT_T.calling_app_id%TYPE
5113      INDEX BY BINARY_INTEGER;
5114  TYPE t_calling_app_doc_unique_ref1 IS TABLE OF
5115      IBY_HOOK_DOCS_IN_PMT_T.calling_app_doc_unique_ref1%TYPE
5116      INDEX BY BINARY_INTEGER;
5117  TYPE t_calling_app_doc_unique_ref2 IS TABLE OF
5118      IBY_HOOK_DOCS_IN_PMT_T.calling_app_doc_unique_ref2%TYPE
5119      INDEX BY BINARY_INTEGER;
5120  TYPE t_calling_app_doc_unique_ref3 IS TABLE OF
5121      IBY_HOOK_DOCS_IN_PMT_T.calling_app_doc_unique_ref3%TYPE
5122      INDEX BY BINARY_INTEGER;
5123  TYPE t_calling_app_doc_unique_ref4 IS TABLE OF
5124      IBY_HOOK_DOCS_IN_PMT_T.calling_app_doc_unique_ref4%TYPE
5125      INDEX BY BINARY_INTEGER;
5126  TYPE t_calling_app_doc_unique_ref5 IS TABLE OF
5127      IBY_HOOK_DOCS_IN_PMT_T.calling_app_doc_unique_ref5%TYPE
5128      INDEX BY BINARY_INTEGER;
5129  TYPE t_document_amount IS TABLE OF
5130      IBY_HOOK_DOCS_IN_PMT_T.document_amount%TYPE
5131      INDEX BY BINARY_INTEGER;
5132  TYPE t_document_currency_code IS TABLE OF
5133      IBY_HOOK_DOCS_IN_PMT_T.document_currency_code%TYPE
5134      INDEX BY BINARY_INTEGER;
5135  TYPE t_amount_withheld IS TABLE OF
5136      IBY_HOOK_DOCS_IN_PMT_T.amount_withheld%TYPE
5137      INDEX BY BINARY_INTEGER;
5138  TYPE t_dont_pay_flg IS TABLE OF
5139      IBY_HOOK_DOCS_IN_PMT_T.dont_pay_flag%TYPE
5140      INDEX BY BINARY_INTEGER;
5141  TYPE t_dont_pay_reason_cd IS TABLE OF
5142      IBY_HOOK_DOCS_IN_PMT_T.dont_pay_reason_code%TYPE
5143      INDEX BY BINARY_INTEGER;
5144  TYPE t_dont_pay_desc IS TABLE OF
5145      IBY_HOOK_DOCS_IN_PMT_T.dont_pay_description%TYPE
5146      INDEX BY BINARY_INTEGER;
5147  TYPE t_pay_proc_trxn_type_code IS TABLE OF
5148      IBY_HOOK_DOCS_IN_PMT_T.pay_proc_trxn_type_code%TYPE
5149      INDEX BY BINARY_INTEGER;
5150  TYPE t_int_bank_account_id IS TABLE OF
5151      IBY_HOOK_DOCS_IN_PMT_T.internal_bank_account_id%TYPE
5152      INDEX BY BINARY_INTEGER;
5153  TYPE t_external_payee_id IS TABLE OF
5154      IBY_HOOK_DOCS_IN_PMT_T.ext_payee_id%TYPE
5155      INDEX BY BINARY_INTEGER;
5156  TYPE t_payeeparty_id IS TABLE OF
5157      IBY_HOOK_DOCS_IN_PMT_T.payee_party_id%TYPE
5158      INDEX BY BINARY_INTEGER;
5159  TYPE t_partysite_id IS TABLE OF
5160      IBY_HOOK_DOCS_IN_PMT_T.party_site_id%TYPE
5161      INDEX BY BINARY_INTEGER;
5162  TYPE t_suppliersite_id IS TABLE OF
5163      IBY_HOOK_DOCS_IN_PMT_T.supplier_site_id%TYPE
5164      INDEX BY BINARY_INTEGER;
5165  TYPE t_orgid IS TABLE OF
5166      IBY_HOOK_DOCS_IN_PMT_T.org_id%TYPE
5167      INDEX BY BINARY_INTEGER;
5168  TYPE t_orgtype IS TABLE OF
5169      IBY_HOOK_DOCS_IN_PMT_T.org_type%TYPE
5170      INDEX BY BINARY_INTEGER;
5171  TYPE t_ext_bank_account_id IS TABLE OF
5172      IBY_HOOK_DOCS_IN_PMT_T.external_bank_account_id%TYPE
5173      INDEX BY BINARY_INTEGER;
5174  TYPE t_payment_curr_discount_taken IS TABLE OF
5175      IBY_HOOK_DOCS_IN_PMT_T.payment_curr_discount_taken%TYPE
5176      INDEX BY BINARY_INTEGER;
5177  TYPE t_crtd_by IS TABLE OF
5178      IBY_HOOK_DOCS_IN_PMT_T.created_by%TYPE
5179      INDEX BY BINARY_INTEGER;
5180  TYPE t_crt_date IS TABLE OF
5181      IBY_HOOK_DOCS_IN_PMT_T.creation_date%TYPE
5182      INDEX BY BINARY_INTEGER;
5183  TYPE t_last_updt_by IS TABLE OF
5184      IBY_HOOK_DOCS_IN_PMT_T.last_updated_by%TYPE
5185      INDEX BY BINARY_INTEGER;
5186  TYPE t_last_updt_date IS TABLE OF
5187      IBY_HOOK_DOCS_IN_PMT_T.last_update_date%TYPE
5188      INDEX BY BINARY_INTEGER;
5189  TYPE t_last_updt_login IS TABLE OF
5190      IBY_HOOK_DOCS_IN_PMT_T.last_update_login%TYPE
5191      INDEX BY BINARY_INTEGER;
5192  TYPE t_object_ver_number IS TABLE OF
5193      IBY_HOOK_DOCS_IN_PMT_T.object_version_number%TYPE
5194      INDEX BY BINARY_INTEGER;
5195 
5196  l_payment_id                       t_pmt_id;
5197  l_document_payable_id              t_document_payable_id;
5198  l_calling_app_id                   t_call_app_id;
5199  l_calling_app_doc_unique_ref1      t_calling_app_doc_unique_ref1;
5200  l_calling_app_doc_unique_ref2      t_calling_app_doc_unique_ref2;
5201  l_calling_app_doc_unique_ref3      t_calling_app_doc_unique_ref3;
5202  l_calling_app_doc_unique_ref4      t_calling_app_doc_unique_ref4;
5203  l_calling_app_doc_unique_ref5      t_calling_app_doc_unique_ref5;
5204  l_document_amount                  t_document_amount;
5205  l_document_currency_code           t_document_currency_code;
5206  l_amount_withheld                  t_amount_withheld;
5207  l_dont_pay_flag                    t_dont_pay_flg;
5208  l_dont_pay_reason_code             t_dont_pay_reason_cd;
5209  l_dont_pay_description             t_dont_pay_desc;
5210  l_pay_proc_trxn_type_code          t_pay_proc_trxn_type_code;
5211  l_internal_bank_account_id         t_int_bank_account_id;
5212  l_ext_payee_id                     t_external_payee_id;
5213  l_payee_party_id                   t_payeeparty_id;
5214  l_party_site_id                    t_partysite_id;
5215  l_supplier_site_id                 t_suppliersite_id;
5216  l_org_id                           t_orgid;
5217  l_org_type                         t_orgtype;
5218  l_external_bank_account_id         t_external_bank_account_id;
5219  l_payment_curr_discount_taken      t_payment_curr_discount_taken;
5220  l_created_by                       t_crtd_by;
5221  l_creation_date                    t_crt_date;
5222  l_last_updated_by                  t_last_updt_by;
5223  l_last_update_date                 t_last_updt_date;
5224  l_last_update_login                t_last_updt_login;
5225  l_object_version_number            t_object_ver_number;
5226 
5227 
5228  BEGIN
5229 
5230      print_debuginfo(l_module_name, 'ENTER');
5231 
5232      /*
5233       * Only successful payments need to be sent to the hook. The
5234       * loop below filters the failed payments/documents.
5235       */
5236      delete_paymentTab;
5237 
5238      FOR i in x_paymentTab.FIRST ..  x_paymentTab.LAST LOOP
5239 
5240          IF (x_paymentTab(i).payment_status <> PAY_STATUS_REJECTED) THEN
5241 
5242              /*
5243               * Copy successful payments into 'hook payments' array.
5244               */
5245              paymentTab.calling_app_id(l_succPayIndx) :=
5246                  p_cap_id;
5247              paymentTab.call_app_pay_service_req_cd(l_succPayIndx) :=
5248                  p_cap_payreq_cd;
5249              paymentTab.payment_service_request_id(l_succPayIndx) :=
5250                  x_paymentTab(i).payment_service_request_id;
5251              paymentTab.payment_id(l_succPayIndx) :=
5252                  x_paymentTab(i).payment_id;
5253              paymentTab.payment_amount(l_succPayIndx) :=
5254                  x_paymentTab(i).payment_amount;
5255              paymentTab.payment_currency_code(l_succPayIndx) :=
5256                  x_paymentTab(i).payment_currency_code;
5257              paymentTab.dont_pay_flg(l_succPayIndx) := 'N';
5258              paymentTab.dont_pay_reason_cd(l_succPayIndx) := null;
5259              paymentTab.dont_pay_desc(l_succPayIndx) := null;
5260              paymentTab.internal_bank_account_id(l_succPayIndx) :=
5261                  x_paymentTab(i).internal_bank_account_id;
5262              paymentTab.external_bank_account_id(l_succPayIndx) :=
5263                  x_paymentTab(i).external_bank_account_id;
5264              paymentTab.ext_payee_id(l_succPayIndx) :=
5265                  x_paymentTab(i).ext_payee_id;
5266              paymentTab.payee_party_id(l_succPayIndx) :=
5267                  x_paymentTab(i).payee_party_id;
5268              paymentTab.party_site_id(l_succPayIndx) :=
5269                  x_paymentTab(i).party_site_id;
5270              paymentTab.supplier_site_id(l_succPayIndx) :=
5271                  x_paymentTab(i).supplier_site_id;
5272              paymentTab.org_id(l_succPayIndx) :=
5273                  x_paymentTab(i).org_id;
5274              paymentTab.org_type(l_succPayIndx) :=
5275                  x_paymentTab(i).org_type;
5276              paymentTab.discount_amount_taken(l_succPayIndx) :=
5277                  x_paymentTab(i).discount_amount_taken;
5278              paymentTab.payment_date(l_succPayIndx) :=
5279                  x_paymentTab(i).payment_date;
5280              paymentTab.bank_charge_amount(l_succPayIndx) :=
5281                  x_paymentTab(i).bank_charge_amount;
5282              paymentTab.created_by(l_succPayIndx) :=
5283                  x_paymentTab(i).created_by;
5284              paymentTab.creation_date(l_succPayIndx) :=
5285                  x_paymentTab(i).creation_date;
5286              paymentTab.last_updated_by(l_succPayIndx) :=
5287                  x_paymentTab(i).last_updated_by;
5288              paymentTab.last_update_date(l_succPayIndx) :=
5289                  x_paymentTab(i).last_update_date;
5290              paymentTab.last_update_login(l_succPayIndx) :=
5291                  x_paymentTab(i).last_update_login;
5292              paymentTab.object_version_number(l_succPayIndx) :=
5293                  x_paymentTab(i).object_version_number;
5294 
5295              IF (x_docsInPmtTab.COUNT > 0) THEN
5296              FOR j in  x_docsInPmtTab.FIRST ..  x_docsInPmtTab.LAST LOOP
5297 
5298                  /*
5299                   * Some payments may contain failed documents (example,
5300                   * documents that have been failed by cascade because
5301                   * some related document has failed).
5302                   *
5303                   * Make sure that the hook only gets successful payments
5304                   * and successful documents within that payment.
5305                   */
5306                  IF (x_docsInPmtTab(j).payment_id =
5307                      x_paymentTab(i).payment_id AND
5308                          x_docsInPmtTab(j).document_status
5309                              = DOC_STATUS_PAY_CREATED) THEN
5310                      /*
5311                       * Copy documents of successful payment into
5312                       * 'hook documents' array.
5313                       */
5314                      x_hookDocsInPmtTab(l_succDocIndx).payment_id
5315                          := x_docsInPmtTab(j).payment_id;
5316                      x_hookDocsInPmtTab(l_succDocIndx).document_payable_id
5317                          := x_docsInPmtTab(j).document_id;
5318                      x_hookDocsInPmtTab(l_succDocIndx).calling_app_id
5319                          := x_docsInPmtTab(j).calling_app_id;
5320                      x_hookDocsInPmtTab(l_succDocIndx).
5321                          calling_app_doc_unique_ref1
5322                              := x_docsInPmtTab(j).calling_app_doc_id1;
5323                      x_hookDocsInPmtTab(l_succDocIndx).
5324                          calling_app_doc_unique_ref2
5325                              := x_docsInPmtTab(j).calling_app_doc_id2;
5326                      x_hookDocsInPmtTab(l_succDocIndx).
5327                          calling_app_doc_unique_ref3
5328                              := x_docsInPmtTab(j).calling_app_doc_id3;
5329                      x_hookDocsInPmtTab(l_succDocIndx).
5330                          calling_app_doc_unique_ref4
5331                              := x_docsInPmtTab(j).calling_app_doc_id4;
5332                      x_hookDocsInPmtTab(l_succDocIndx).
5333                          calling_app_doc_unique_ref5
5334                              := x_docsInPmtTab(j).calling_app_doc_id5;
5335                      x_hookDocsInPmtTab(l_succDocIndx).document_amount
5336                          := x_docsInPmtTab(j).document_amount;
5337                      x_hookDocsInPmtTab(l_succDocIndx).document_currency_code
5338                          := x_docsInPmtTab(j).document_currency;
5339                      x_hookDocsInPmtTab(l_succDocIndx).amount_withheld
5340                          := x_docsInPmtTab(j).amount_withheld;
5341                      x_hookDocsInPmtTab(l_succDocIndx).pay_proc_trxn_type_code
5342                          := x_docsInPmtTab(j).pay_proc_ttype_cd;
5343 
5344                      x_hookDocsInPmtTab(l_succDocIndx).internal_bank_account_id
5345                          := x_docsInPmtTab(j).int_bank_acct_id;
5346                      x_hookDocsInPmtTab(l_succDocIndx).ext_payee_id
5347                          := x_docsInPmtTab(j).ext_payee_id;
5348                      x_hookDocsInPmtTab(l_succDocIndx).payee_party_id
5349                          := x_docsInPmtTab(j).payee_id;
5350                      x_hookDocsInPmtTab(l_succDocIndx).party_site_id
5351                          := x_docsInPmtTab(j).payee_site_id;
5352                      x_hookDocsInPmtTab(l_succDocIndx).supplier_site_id
5353                          := x_docsInPmtTab(j).supplier_site_id;
5354                      x_hookDocsInPmtTab(l_succDocIndx).org_id
5355                          := x_docsInPmtTab(j).org_id;
5356                      x_hookDocsInPmtTab(l_succDocIndx).org_type
5357                          := x_docsInPmtTab(j).org_type;
5358                      x_hookDocsInPmtTab(l_succDocIndx).external_bank_account_id
5359                          := x_docsInPmtTab(j).ext_bank_acct_id;
5360                      x_hookDocsInPmtTab(l_succDocIndx).
5361                          payment_curr_discount_taken
5362                              := x_docsInPmtTab(j).pmt_curr_discount;
5363 
5364 
5365                      /*
5366                       * If we copied a document, increment document
5367                       * counter before next iteration.
5368                       */
5369                      l_succDocIndx := l_succDocIndx + 1;
5370 
5371                  END IF;
5372 
5373              END LOOP;
5374              END IF; -- if document count > 0
5375 
5376              /*
5377               * If we copied a payment, increment payment
5378               * counter before next iteration.
5379               */
5380              l_succPayIndx := l_succPayIndx + 1;
5381 
5382          END IF;
5383 
5384      END LOOP;
5385 
5386      print_debuginfo(l_module_name, '# orig pmts: '
5387          || x_paymentTab.COUNT || ', # hook pmts: '
5388          || paymentTab.calling_app_id.COUNT);
5389 
5390      /*
5391       * The hook data is passed to the external app via global
5392       * temp tables IBY_HOOK_PAYMENTS_TEMP and IBY_HOOK_DOCS_IN_PMT_TEMP.
5393       * Populate these temp tables with data from our hook data
5394       * structures.
5395       */
5396      IF (paymentTab.calling_app_id.COUNT > 0) THEN
5397          print_debuginfo(l_module_name, 'Populating global temp tables with'
5398              || ' hook data.');
5399      ELSE
5400          print_debuginfo(l_module_name, 'No payments to pass to hook. '
5401              || 'Exiting ..');
5402          print_debuginfo(l_module_name, 'EXIT');
5403          RETURN;
5404      END IF;
5405 
5406      /*
5407       * First delete any existing records in these
5408       * temp tables so that they do not interfere with
5409       * our processing.
5410       */
5411      print_debuginfo(l_module_name, 'Deleting existing records in '
5412          || 'hook tables .. ');
5413      DELETE IBY_HOOK_DOCS_IN_PMT_T;
5414      DELETE IBY_HOOK_PAYMENTS_T;
5415      print_debuginfo(l_module_name, 'Finished deleting records from '
5416          || 'hook tables .. ');
5417 
5418      /*
5419       * Create arrays for each column of the IBY_HOOK_PAYMENTS_T table.
5420       * These arrays will be used in the bulk insert.
5421       */
5422      IF (x_hookDocsInPmtTab.COUNT > 0) THEN
5423      FOR i in x_hookDocsInPmtTab.FIRST..x_hookDocsInPmtTab.LAST LOOP
5424 
5425          l_payment_id(i)
5426              := x_hookDocsInPmtTab(i).payment_id;
5427          l_document_payable_id(i)
5428              := x_hookDocsInPmtTab(i).document_payable_id;
5429          l_calling_app_id(i)
5430              := x_hookDocsInPmtTab(i).calling_app_id;
5431          l_calling_app_doc_unique_ref1(i)
5432              := x_hookDocsInPmtTab(i).calling_app_doc_unique_ref1;
5433          l_calling_app_doc_unique_ref2(i)
5434              := x_hookDocsInPmtTab(i).calling_app_doc_unique_ref2;
5435          l_calling_app_doc_unique_ref3(i)
5436              := x_hookDocsInPmtTab(i).calling_app_doc_unique_ref3;
5437          l_calling_app_doc_unique_ref4(i)
5438              := x_hookDocsInPmtTab(i).calling_app_doc_unique_ref4;
5439          l_calling_app_doc_unique_ref5(i)
5440              := x_hookDocsInPmtTab(i).calling_app_doc_unique_ref5;
5441          l_document_amount(i)
5442              := x_hookDocsInPmtTab(i).document_amount;
5443          l_document_currency_code(i)
5444              := x_hookDocsInPmtTab(i).document_currency_code;
5445          l_amount_withheld(i)
5446              := NVL(x_hookDocsInPmtTab(i).amount_withheld, 0);
5447          l_dont_pay_flag(i)
5448              := NVL(x_hookDocsInPmtTab(i).dont_pay_flag, 'N');
5449          l_dont_pay_reason_code(i)
5450              := x_hookDocsInPmtTab(i).dont_pay_reason_code;
5451          l_dont_pay_description(i)
5452              := x_hookDocsInPmtTab(i).dont_pay_description;
5453          l_pay_proc_trxn_type_code(i)
5454              := x_hookDocsInPmtTab(i).pay_proc_trxn_type_code;
5455          l_internal_bank_account_id(i)
5456              := x_hookDocsInPmtTab(i).internal_bank_account_id;
5457          l_ext_payee_id(i)
5458              := x_hookDocsInPmtTab(i).ext_payee_id;
5459          l_payee_party_id(i)
5460              := x_hookDocsInPmtTab(i).payee_party_id;
5461          l_party_site_id(i)
5462              := x_hookDocsInPmtTab(i).party_site_id;
5463          l_supplier_site_id(i)
5464              := x_hookDocsInPmtTab(i).supplier_site_id;
5465          l_org_id(i)
5466              := x_hookDocsInPmtTab(i).org_id;
5467          l_org_type(i)
5468              := x_hookDocsInPmtTab(i).org_type;
5469          l_external_bank_account_id(i)
5470              := x_hookDocsInPmtTab(i).external_bank_account_id;
5471          l_payment_curr_discount_taken(i)
5472              := x_hookDocsInPmtTab(i).payment_curr_discount_taken;
5473          l_created_by(i)
5474              := NVL(x_hookDocsInPmtTab(i).created_by, fnd_global.user_id);
5475          l_creation_date(i)
5476              := NVL(x_hookDocsInPmtTab(i).creation_date, sysdate);
5477          l_last_updated_by(i)
5478              := NVL(x_hookDocsInPmtTab(i).last_updated_by, fnd_global.user_id);
5479          l_last_update_date(i)
5480              := NVL(x_hookDocsInPmtTab(i).last_update_date, sysdate);
5481          l_last_update_login(i)
5482              := NVL(x_hookDocsInPmtTab(i).last_update_login,
5483                     fnd_global.user_id);
5484          l_object_version_number(i)
5485              := NVL(x_hookDocsInPmtTab(i).object_version_number, 1);
5486 
5487      END LOOP;
5488      END IF; -- if document count > 0
5489 
5490 
5491      /* insert documents */
5492      /*
5493       * Bulk insert records into IBY_HOOK_DOCS_IN_PMT_T using named
5494       * columns. This will avoid any dependency in column order.
5495       */
5496      IF (x_hookDocsInPmtTab.COUNT > 0) THEN
5497      FORALL i in x_hookDocsInPmtTab.FIRST..x_hookDocsInPmtTab.LAST
5498          INSERT INTO IBY_HOOK_DOCS_IN_PMT_T
5499              (
5500              payment_id,
5501              document_payable_id,
5502              calling_app_id,
5503              calling_app_doc_unique_ref1,
5504              calling_app_doc_unique_ref2,
5505              calling_app_doc_unique_ref3,
5506              calling_app_doc_unique_ref4,
5507              calling_app_doc_unique_ref5,
5508              document_amount,
5509              document_currency_code,
5510              amount_withheld,
5511              dont_pay_flag,
5512              dont_pay_reason_code,
5513              dont_pay_description,
5514              pay_proc_trxn_type_code,
5515              internal_bank_account_id,
5516              ext_payee_id,
5517              payee_party_id,
5518              party_site_id,
5519              supplier_site_id,
5520              org_id,
5521              org_type,
5522              external_bank_account_id,
5523              payment_curr_discount_taken,
5524              created_by,
5525              creation_date,
5526              last_updated_by,
5527              last_update_date,
5528              last_update_login,
5529              object_version_number
5530              )
5531          VALUES
5532              (
5533              l_payment_id(i),
5534              l_document_payable_id(i),
5535              l_calling_app_id(i),
5536              l_calling_app_doc_unique_ref1(i),
5537              l_calling_app_doc_unique_ref2(i),
5538              l_calling_app_doc_unique_ref3(i),
5539              l_calling_app_doc_unique_ref4(i),
5540              l_calling_app_doc_unique_ref5(i),
5541              l_document_amount(i),
5542              l_document_currency_code(i),
5543              l_amount_withheld(i),
5544              l_dont_pay_flag(i),
5545              l_dont_pay_reason_code(i),
5546              l_dont_pay_description(i),
5547              l_pay_proc_trxn_type_code(i),
5548              l_internal_bank_account_id(i),
5549              l_ext_payee_id(i),
5550              l_payee_party_id(i),
5551              l_party_site_id(i),
5552              l_supplier_site_id(i),
5553              l_org_id(i),
5554              l_org_type(i),
5555              l_external_bank_account_id(i),
5556              l_payment_curr_discount_taken(i),
5557              l_created_by(i),
5558              l_creation_date(i),
5559              l_last_updated_by(i),
5560              l_last_update_date(i),
5561              l_last_update_login(i),
5562              l_object_version_number(i)
5563              )
5564              ;
5565      END IF; -- if hook document count > 0
5566 
5567      /*
5568       * Bulk insert records into IBY_HOOK_PAYMENTS_T using named
5569       * columns. This will avoid any dependency in column order.
5570       */
5571 
5572 --     FOR i in paymentTab.calling_app_id.FIRST .. paymentTab.calling_app_id.LAST
5573 
5574      FORALL i in paymentTab.calling_app_id.FIRST .. paymentTab.calling_app_id.LAST
5575          INSERT INTO IBY_HOOK_PAYMENTS_T
5576              (
5577              calling_app_id,
5578              call_app_pay_service_req_code,
5579              payment_service_request_id,
5580              payment_id,
5581              payment_amount,
5582              payment_currency_code,
5583              dont_pay_flag,
5584              dont_pay_reason_code,
5585              dont_pay_description,
5586              internal_bank_account_id,
5587              ext_payee_id,
5588              payee_party_id,
5589              party_site_id,
5590              supplier_site_id,
5591              org_id,
5592              org_type,
5593              external_bank_account_id,
5594              discount_amount_taken,
5595              payment_date,
5596              bank_charge_amount,
5597              created_by,
5598              creation_date,
5599              last_updated_by,
5600              last_update_date,
5601              last_update_login,
5602              object_version_number
5603              )
5604              VALUES
5605              (
5606              paymentTab.calling_app_id(i),
5607              paymentTab.call_app_pay_service_req_cd(i),
5608              paymentTab.payment_service_request_id(i),
5609              paymentTab.payment_id(i),
5610              paymentTab.payment_amount(i),
5611              paymentTab.payment_currency_code(i),
5612              paymentTab.dont_pay_flg(i),
5613              paymentTab.dont_pay_reason_cd(i),
5614              paymentTab.dont_pay_desc(i),
5615              paymentTab.internal_bank_account_id(i),
5616              paymentTab.ext_payee_id(i),
5617              paymentTab.payee_party_id(i),
5618              paymentTab.party_site_id(i),
5619              paymentTab.supplier_site_id(i),
5620              paymentTab.org_id(i),
5621              paymentTab.org_type(i),
5622              paymentTab.external_bank_account_id(i),
5623              paymentTab.discount_amount_taken(i),
5624              paymentTab.payment_date(i),
5625              paymentTab.bank_charge_amount(i),
5626              paymentTab.created_by(i),
5627              paymentTab.creation_date(i),
5628              paymentTab.last_updated_by(i),
5629              paymentTab.last_update_date(i),
5630              paymentTab.last_update_login(i),
5631              paymentTab.object_version_number(i)
5632              );
5633 
5634      print_debuginfo(l_module_name, 'Finished populating global temp tables.');
5635 
5636 --     delete_paymentTab;
5637 
5638      print_debuginfo(l_module_name, 'Global temp tables were'
5639          || ' populated with '
5640          || x_hookDocsInPmtTab.COUNT || ' docs and '
5641          || paymentTab.calling_app_id.COUNT   || ' payments.');
5642 
5643      /* log the data that we pass to the hook */
5644      print_debuginfo(l_module_name, 'List of docs/pmts passed to hook: ');
5645 
5646      IF (x_hookDocsInPmtTab.COUNT > 0) THEN
5647      FOR i in x_hookDocsInPmtTab.FIRST..x_hookDocsInPmtTab.LAST LOOP
5648          print_debuginfo(l_module_name, 'Payment: '
5649              || x_hookDocsInPmtTab(i).payment_id
5650              || ', document: '
5651              || x_hookDocsInPmtTab(i).document_payable_id
5652              || ', document amt: '
5653              || x_hookDocsInPmtTab(i).document_amount
5654              || ', document curr: '
5655              || x_hookDocsInPmtTab(i).document_currency_code
5656              );
5657      END LOOP;
5658      END IF;
5659 
5660      print_debuginfo(l_module_name, x_hookDocsInPmtTab.COUNT
5661          || ' docs and '
5662          || paymentTab.calling_app_id.COUNT || ' payments'
5663          || ' were inserted into global temp tables.'
5664          );
5665 
5666      print_debuginfo(l_module_name, 'EXIT');
5667 
5668  END performPreHookProcess;
5669 
5670 /*--------------------------------------------------------------------
5671  | NAME:
5672  |     performPostHookProcess
5673  |
5674  | PURPOSE:
5675  |     Copies back the payments from the IBY_HOOK_PAYMENTS_TEMP temporary
5676  |     table back to the created payments PLSQL table.
5677  |
5678  |
5679  | PARAMETERS:
5680  |     IN
5681  |
5682  |     OUT
5683  |
5684  |
5685  | RETURNS:
5686  |
5687  | NOTES:
5688  |
5689  *---------------------------------------------------------------------*/
5690  PROCEDURE performPostHookProcess(
5691      x_paymentTab        IN OUT NOCOPY paymentTabType,
5692      x_docsInPmtTab      IN OUT NOCOPY docsInPaymentTabType,
5693      x_hookPaymentTab    IN OUT NOCOPY hookPaymentTabType,
5694      x_hookDocsInPmtTab  IN OUT NOCOPY hookDocsInPaymentTabType,
5695      x_docErrorTab       IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.docErrorTabType,
5696      x_errTokenTab       IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.
5697                                            trxnErrTokenTabType
5698      )
5699  IS
5700 
5701  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
5702                                              '.performPostHookProcess';
5703  l_succPayIndx NUMBER := 1;
5704  l_succDocIndx NUMBER := 1;
5705  l_preHookPaymentTab   hookPaymentTabType;
5706  l_preHook_cntr        integer;
5707  l_preHookDocsInPmtTab hookDocsInPaymentTabType;
5708  l_str         VARCHAR2(2000);
5709 
5710  l_doc_err_rec   IBY_TRANSACTION_ERRORS%ROWTYPE;
5711 
5712  BEGIN
5713 
5714      print_debuginfo(l_module_name, 'ENTER');
5715 
5716      IF (paymentTab.calling_app_id.COUNT = 0) THEN
5717          print_debuginfo(l_module_name, 'No payments were retrieved '
5718              || 'from hook. Exiting ..');
5719          print_debuginfo(l_module_name, 'EXIT');
5720          RETURN;
5721      END IF;
5722 
5723      /*
5724       * Copy all the data that we passed to the hook into a
5725       * backup data structure. We will diff this backup
5726       * structure with the data struct that returns from
5727       * the hook and log all the differences as 'payment
5728       * changes made by hook'.
5729       */
5730 /*    Bug 6347886
5731      FOR i in x_hookPaymentTab.FIRST .. x_hookPaymentTab.LAST LOOP
5732          l_preHookPaymentTab(l_preHookPaymentTab.COUNT + 1)
5733              := x_hookPaymentTab(i);
5734      END LOOP;
5735 */
5736      l_preHook_cntr := 1;
5737      FOR i in paymentTab.calling_app_id.FIRST .. paymentTab.calling_app_id.LAST
5738 LOOP
5739          l_preHookPaymentTab(l_preHook_cntr).calling_app_id
5740          := PaymentTab.calling_app_id(i);
5741          l_preHookPaymentTab(l_preHook_cntr).call_app_pay_service_req_code
5742          := PaymentTab.call_app_pay_service_req_cd(i);
5743          l_preHookPaymentTab(l_preHook_cntr).payment_service_request_id
5744          := PaymentTab.payment_service_request_id(i);
5745          l_preHookPaymentTab(l_preHook_cntr).payment_id
5746          := PaymentTab.payment_id(i);
5747          l_preHookPaymentTab(l_preHook_cntr).payment_amount
5748          := PaymentTab.payment_amount(i);
5749          l_preHookPaymentTab(l_preHook_cntr).payment_currency_code
5750          := PaymentTab.payment_currency_code(i);
5751          l_preHookPaymentTab(l_preHook_cntr).dont_pay_flag
5752          := PaymentTab.dont_pay_flg(i);
5753          l_preHookPaymentTab(l_preHook_cntr).dont_pay_reason_code
5754          := PaymentTab.dont_pay_reason_cd(i);
5755          l_preHookPaymentTab(l_preHook_cntr).dont_pay_description
5756          := PaymentTab.dont_pay_desc(i);
5757          l_preHookPaymentTab(l_preHook_cntr).internal_bank_account_id
5758          := PaymentTab.internal_bank_account_id(i);
5759          l_preHookPaymentTab(l_preHook_cntr).ext_payee_id
5760          := PaymentTab.ext_payee_id(i);
5761          l_preHookPaymentTab(l_preHook_cntr).payee_party_id
5762          := PaymentTab.payee_party_id(i);
5763          l_preHookPaymentTab(l_preHook_cntr).party_site_id
5764          := PaymentTab.party_site_id(i);
5765          l_preHookPaymentTab(l_preHook_cntr).supplier_site_id
5766          := PaymentTab.supplier_site_id(i);
5767          l_preHookPaymentTab(l_preHook_cntr).org_id
5768          := PaymentTab.org_id(i);
5769          l_preHookPaymentTab(l_preHook_cntr).org_type
5770          := PaymentTab.org_type(i);
5771          l_preHookPaymentTab(l_preHook_cntr).external_bank_account_id
5772          := PaymentTab.external_bank_account_id(i);
5773          l_preHookPaymentTab(l_preHook_cntr).discount_amount_taken
5774          := PaymentTab.discount_amount_taken(i);
5775          l_preHookPaymentTab(l_preHook_cntr).payment_date
5776          := PaymentTab.payment_date(i);
5777          l_preHookPaymentTab(l_preHook_cntr).bank_charge_amount
5778          := PaymentTab.bank_charge_amount(i);
5779          l_preHookPaymentTab(l_preHook_cntr).created_by
5780          := PaymentTab.created_by(i);
5781          l_preHookPaymentTab(l_preHook_cntr).creation_date
5782          := PaymentTab.creation_date(i);
5783          l_preHookPaymentTab(l_preHook_cntr).last_updated_by
5784          := PaymentTab.last_updated_by(i);
5785          l_preHookPaymentTab(l_preHook_cntr).last_update_date
5786          := PaymentTab.last_update_date(i);
5787          l_preHookPaymentTab(l_preHook_cntr).last_update_login
5788          := PaymentTab.last_update_login(i);
5789          l_preHookPaymentTab(l_preHook_cntr).object_version_number
5790          := PaymentTab.object_version_number(i);
5791          l_preHook_cntr := l_preHook_cntr + 1;
5792      END LOOP;
5793 
5794 /* Bug 6347886 */
5795 
5796      IF (x_hookDocsInPmtTab.COUNT > 0) THEN
5797      FOR i in x_hookDocsInPmtTab.FIRST .. x_hookDocsInPmtTab.LAST LOOP
5798          l_preHookDocsInPmtTab(l_preHookDocsInPmtTab.COUNT + 1)
5799              := x_hookDocsInPmtTab(i);
5800      END LOOP;
5801      END IF;
5802 
5803      /*
5804       * Read the documents and payments from the global temp
5805       * tables. The calling app might have updated them.
5806       */
5807      getAdjustedPaymentData(x_hookPaymentTab, x_hookDocsInPmtTab);
5808 
5809      print_debuginfo(l_module_name, x_hookDocsInPmtTab.COUNT
5810          || ' docs and '
5811          || x_hookPaymentTab.COUNT || ' payments'
5812          || ' were read from global temp tables.'
5813          );
5814 
5815      /*
5816       * The external hook can change document and payment
5817       * amounts, or change document and payment statuses.
5818       *
5819       * However, the external hook should not 'add to' or
5820       * 'remove from', the documents and payments that were
5821       * passed to the hook.
5822       *
5823       * Check the document and payment count. If the hook
5824       * has made any changes, the abort the program as it
5825       * a fatal error.
5826       */
5827 
5828      IF (x_hookPaymentTab.COUNT <> l_preHookPaymentTab.COUNT) THEN
5829 
5830          print_debuginfo(l_module_name, 'Fatal error - Payment '
5831              || 'count mismatch after hook operation. '
5832              || '# payments passed to hook: '
5833              || l_preHookPaymentTab.COUNT
5834              || ', # payments returning from hook: '
5835              || x_hookPaymentTab.COUNT,
5836              FND_LOG.LEVEL_UNEXPECTED
5837              );
5838 
5839          print_debuginfo(l_module_name, 'Aborting program ..',
5840              FND_LOG.LEVEL_UNEXPECTED);
5841 
5842          APP_EXCEPTION.RAISE_EXCEPTION;
5843 
5844      END IF;
5845 
5846      IF (x_hookDocsInPmtTab.COUNT <> l_preHookDocsInPmtTab.COUNT) THEN
5847 
5848          print_debuginfo(l_module_name, 'Fatal error - Document '
5849              || 'count mismatch after hook operation. '
5850              || '# documents passed to hook: '
5851              || l_preHookDocsInPmtTab.COUNT
5852              || ', # documents returning from hook: '
5853              || x_hookDocsInPmtTab.COUNT,
5854              FND_LOG.LEVEL_UNEXPECTED
5855              );
5856 
5857          print_debuginfo(l_module_name, 'Aborting program ..',
5858              FND_LOG.LEVEL_UNEXPECTED);
5859 
5860          APP_EXCEPTION.RAISE_EXCEPTION;
5861 
5862      END IF;
5863 
5864      /*
5865       * Diff the pre-hook and post-hook structures and log
5866       * the values that were changed by the hook.
5867       */
5868      print_debuginfo(l_module_name, 'List of changes made by the hook: ');
5869 
5870      /* Diff of payments */
5871      FOR i in x_hookPaymentTab.FIRST .. x_hookPaymentTab.LAST LOOP
5872 
5873          l_str := NULL;
5874 
5875          IF (x_hookPaymentTab(i).payment_amount <>
5876              l_preHookPaymentTab(i).payment_amount ) THEN
5877              l_str := 'Pmt amount: ' || x_hookPaymentTab(i).payment_amount;
5878          END IF;
5879 
5880          IF (x_hookPaymentTab(i).dont_pay_flag <>
5881              l_preHookPaymentTab(i).dont_pay_flag) THEN
5882              l_str := l_str || ' Dont pay flag: '
5883                           || x_hookPaymentTab(i).dont_pay_flag;
5884          END IF;
5885 
5886          IF (l_str IS NOT NULL) THEN
5887              l_str := 'Pmt Id: ' || x_hookPaymentTab(i).payment_id
5888                           || ' ' || l_str;
5889              print_debuginfo(l_module_name, l_str);
5890          END IF;
5891 
5892      END LOOP;
5893 
5894      /* Diff of documents */
5895      IF (x_hookDocsInPmtTab.COUNT > 0) THEN
5896      FOR i in x_hookDocsInPmtTab.FIRST .. x_hookDocsInPmtTab.LAST LOOP
5897 
5898          l_str := NULL;
5899 
5900          IF (x_hookDocsInPmtTab(i).document_amount <>
5901              l_preHookDocsInPmtTab(i).document_amount) THEN
5902              l_str := 'Doc amount: ' || x_hookDocsInPmtTab(i).document_amount;
5903          END IF;
5904 
5905          IF (x_hookDocsInPmtTab(i).amount_withheld <>
5906              l_preHookDocsInPmtTab(i).amount_withheld) THEN
5907              l_str := l_str || ', Amount withheld: '
5908                           || x_hookDocsInPmtTab(i).amount_withheld;
5909          END IF;
5910 
5911          IF (x_hookDocsInPmtTab(i).dont_pay_flag <>
5912              l_preHookDocsInPmtTab(i).dont_pay_flag) THEN
5913              l_str := l_str || ', Dont pay flag: '
5914                           || x_hookDocsInPmtTab(i).dont_pay_flag;
5915          END IF;
5916 
5917          IF (l_str IS NOT NULL) THEN
5918              l_str := 'Doc Id: ' || x_hookDocsInPmtTab(i).document_payable_id
5919                           || ' ' || l_str;
5920              print_debuginfo(l_module_name, l_str);
5921          END IF;
5922 
5923      END LOOP;
5924      END IF; -- if hook docs count > 0
5925 
5926      /*
5927       * Ideally, when the calling app sets the 'dont pay flag'
5928       * for a payment, it should also do the following:
5929       *
5930       * 1. Set payment amount to zero.
5931       *
5932       * 2. Set 'dont pay flag' for all docs that were part of
5933       *    this payment.
5934       *
5935       * But the calling app does not do (1) and (2). Instead these
5936       * steps are handled by the build program (to make life simpler
5937       * for the calling app).
5938       *
5939       * The loop below handles these steps.
5940       */
5941      FOR i in x_hookPaymentTab.FIRST ..  x_hookPaymentTab.LAST LOOP
5942 
5943          IF (UPPER(x_hookPaymentTab(i).dont_pay_flag) = 'Y') THEN
5944 
5945              /*
5946               * Since the payment has failed, all docs in this
5947               * payment also need to be failed.
5948               *
5949               * Set the 'dont pay flag' to true for all docs
5950               * that were part of this payment.
5951               */
5952              IF (x_hookDocsInPmtTab.COUNT > 0) THEN
5953              FOR j in x_hookDocsInPmtTab.FIRST .. x_hookDocsInPmtTab.LAST LOOP
5954 
5955                  IF (x_hookDocsInPmtTab(j).payment_id =
5956                      x_hookPaymentTab(i).payment_id) THEN
5957 
5958                      x_hookDocsInPmtTab(j).dont_pay_flag := 'Y';
5959 
5960                  END IF;
5961 
5962              END LOOP;
5963              END IF;
5964 
5965              /*
5966               * Set the payment amount to zero.
5967               *
5968               * Remember, Payment Amount =
5969               *               SUM (successful document amounts)
5970               */
5971              x_hookPaymentTab(i).payment_amount := 0;
5972 
5973          END IF;
5974 
5975      END LOOP;
5976 
5977      /*
5978       * STEP 1:
5979       *
5980       * Update our existing payments and documents data structures
5981       * with the values from the hook - the external app may have adjusted
5982       * some payment amounts for bank charges, tax withholding etc.
5983       *
5984       * These must be reflected into our existing data structures before
5985       * we update the documents and payments tables with them.
5986       */
5987 
5988      /* update payments */
5989      FOR i in x_hookPaymentTab.FIRST ..  x_hookPaymentTab.LAST LOOP
5990 
5991          FOR j in x_paymentTab.FIRST ..  x_paymentTab.LAST LOOP
5992 
5993              IF (x_hookPaymentTab(i).payment_id = x_paymentTab(j).payment_id)
5994                  THEN
5995 
5996                  /*
5997                   * Copy from 'hook payments' array to original
5998                   * payments array.
5999                   */
6000                  IF (x_paymentTab(j).payment_amount <>
6001                      x_hookPaymentTab(i).payment_amount) THEN
6002 
6003                      print_debuginfo(l_module_name, 'Amount for payment '
6004                          || x_hookPaymentTab(i).payment_id
6005                          || ' was changed by calling app'
6006                          || ' from '  || x_paymentTab(j).payment_amount
6007                          || ' to ' || x_hookPaymentTab(i).payment_amount
6008                          );
6009 
6010                      x_paymentTab(j).payment_amount :=
6011                          x_hookPaymentTab(i).payment_amount;
6012 
6013                  END IF;
6014 
6015                  /*
6016                   * If the external app has populated the bank charge
6017                   * amount via the hook, update the bank charge
6018                   * amount attribute on the payment to reflect this.
6019                   */
6020                  IF (x_paymentTab(j).bank_charge_amount <>
6021                      x_hookPaymentTab(i).bank_charge_amount) THEN
6022 
6023                      print_debuginfo(l_module_name, 'Bank charge amount '
6024                          || 'for payment '
6025                          || x_hookPaymentTab(i).payment_id
6026                          || ' was changed by calling app'
6027                          || ' from '  || x_paymentTab(j).bank_charge_amount
6028                          || ' to ' || x_hookPaymentTab(i).bank_charge_amount
6029                          );
6030 
6031                      x_paymentTab(j).bank_charge_amount :=
6032                          x_hookPaymentTab(i).bank_charge_amount;
6033 
6034                  END IF;
6035 
6036 		 /*
6037 		  * If the external app has populated the discount
6038 		  * amount via the hook, update the discount
6039 		  * amount attribute on the payment to reflect this.
6040 		  */
6041 		 IF (x_paymentTab(j).discount_amount_taken <>
6042 		     x_hookPaymentTab(i).discount_amount_taken) THEN
6043 
6044 		     print_debuginfo(l_module_name, 'Discount Amount Taken '
6045 		 	  || 'for payment '
6046 		 	  || x_hookPaymentTab(i).payment_id
6047 		 	  || ' was changed by calling app'
6048 		 	  || ' from '  || x_paymentTab(j).discount_amount_taken
6049 		 	  || ' to ' || x_hookPaymentTab(i).discount_amount_taken
6050 		 	  );
6051 
6052 		     x_paymentTab(j).discount_amount_taken :=
6053 		 	  x_hookPaymentTab(i).discount_amount_taken;
6054 
6055 		 END IF;
6056 
6057                  /*
6058                   * Adjust payment status if necessary.
6059                   */
6060                  IF (UPPER(x_hookPaymentTab(i).dont_pay_flag) = 'Y') THEN
6061 
6062                      x_paymentTab(j).payment_status := PAY_STATUS_CA_FAILED;
6063 
6064                      print_debuginfo(l_module_name, 'Payment '
6065                          || x_paymentTab(j).payment_id
6066                          || ' was failed by calling app.');
6067 
6068                      /*
6069                       * Once we fail a payment, we need to create
6070                       * an error record and insert this record
6071                       * into the errors table.
6072                       */
6073                      IBY_BUILD_UTILS_PKG.createErrorRecord(
6074                          TRXN_TYPE_PMT,
6075                          x_paymentTab(j).payment_id,
6076                          x_paymentTab(j).payment_status,
6077                          NULL,
6078                          x_paymentTab(j).payment_id,
6079                          NULL,
6080                          NULL,
6081                          NULL,
6082                          NULL,
6083                          NULL,
6084                          l_doc_err_rec,
6085                          x_errTokenTab
6086                          );
6087 
6088                  IBY_VALIDATIONSETS_PUB.insertIntoErrorTable(
6089                      l_doc_err_rec, x_docErrorTab, x_errTokenTab);
6090 
6091                  /* fail the docs of this payment */
6092                  failDocsOfPayment(x_paymentTab(j).payment_id,
6093                      DOC_STATUS_PAY_VAL_FAIL, x_docsInPmtTab,
6094                      x_docErrorTab, x_errTokenTab);
6095 
6096 
6097                  END IF;
6098 
6099              END IF;
6100 
6101          END LOOP;
6102 
6103      END LOOP;
6104 
6105      /* update documents */
6106      IF (x_hookDocsInPmtTab.COUNT > 0) THEN
6107      FOR i in x_hookDocsInPmtTab.FIRST ..  x_hookDocsInPmtTab.LAST LOOP
6108 
6109          FOR j in x_docsInPmtTab.FIRST ..  x_docsInPmtTab.LAST LOOP
6110 
6111              IF (x_hookDocsInPmtTab(i).document_payable_id =
6112                  x_docsInPmtTab(j).document_id)
6113                  THEN
6114 
6115                  /*
6116                   * Copy from 'hook documents' array to original
6117                   * documents array.
6118                   */
6119                  IF (x_docsInPmtTab(j).document_amount <>
6120                      x_hookDocsInPmtTab(i).document_amount) THEN
6121 
6122                      print_debuginfo(l_module_name, 'Amount for document '
6123                          || x_hookDocsInPmtTab(i).document_payable_id
6124                          || ' was changed by calling app'
6125                          || ' from ' || x_docsInPmtTab(j).document_amount
6126                          ||  ' to '  || x_hookDocsInPmtTab(i).document_amount
6127                          );
6128 
6129                      x_docsInPmtTab(j).document_amount :=
6130                          x_hookDocsInPmtTab(i).document_amount;
6131 
6132                      x_docsInPmtTab(j).amount_withheld :=
6133                          x_hookDocsInPmtTab(i).amount_withheld;
6134 
6135                  END IF;
6136 
6137                  /*
6138                   * Copy from 'hook documents' array to original
6139                   * documents array.
6140                   */
6141                  IF (x_docsInPmtTab(j).pmt_curr_discount <>
6142                      x_hookDocsInPmtTab(i).payment_curr_discount_taken) THEN
6143 
6144                      print_debuginfo(l_module_name, 'Discount for document '
6145                 	  || x_hookDocsInPmtTab(i).document_payable_id
6146                 	  || ' was changed by calling app'
6147                 	  || ' from ' || x_docsInPmtTab(j).pmt_curr_discount
6148                 	  ||  ' to '  || x_hookDocsInPmtTab(i).payment_curr_discount_taken
6149                 	  );
6150 
6151                      x_docsInPmtTab(j).pmt_curr_discount :=
6152                 	  x_hookDocsInPmtTab(i).payment_curr_discount_taken;
6153 
6154                  END IF;
6155 
6156                  /*
6157                   * Adjust document status if necessary.
6158                   */
6159                  IF (UPPER(x_hookDocsInPmtTab(i).dont_pay_flag) = 'Y') THEN
6160 
6161                      x_docsInPmtTab(j).document_status := DOC_STATUS_CA_FAILED;
6162 
6163                      print_debuginfo(l_module_name, 'Document '
6164                          || x_hookDocsInPmtTab(i).document_payable_id
6165                          || ' was failed by calling app.');
6166 
6167                      /*
6168                       * Once we fail a doc, we must add a corresponding
6169                       * error message to the error table.
6170                       */
6171                      IBY_BUILD_UTILS_PKG.createErrorRecord(
6172                          TRXN_TYPE_DOC,
6173                          x_docsInPmtTab(j).document_id,
6174                          x_docsInPmtTab(j).document_status,
6175                          x_docsInPmtTab(j).calling_app_id,
6176                          x_docsInPmtTab(j).calling_app_doc_id1,
6177                          x_docsInPmtTab(j).calling_app_doc_id2,
6178                          x_docsInPmtTab(j).calling_app_doc_id3,
6179                          x_docsInPmtTab(j).calling_app_doc_id4,
6180                          x_docsInPmtTab(j).calling_app_doc_id5,
6181                          x_docsInPmtTab(j).pay_proc_ttype_cd,
6182                          l_doc_err_rec,
6183                          x_errTokenTab
6184                          );
6185 
6186                      IBY_VALIDATIONSETS_PUB.insertIntoErrorTable(
6187                          l_doc_err_rec, x_docErrorTab, x_errTokenTab);
6188 
6189                  END IF;
6190 
6191              END IF;
6192 
6193          END LOOP;
6194 
6195      END LOOP;
6196      END IF;
6197 
6198      /*
6199       * STEP 2:
6200       *
6201       * The external app may not wish to pay certain documents.
6202       * For such cases, make sure to fail the sister docs that are
6203       * related to this document via the 'payment grouping number'.
6204       * Each time a sister doc is failed, adjust the payment amount
6205       * of the payment that contains this document to reflect this.
6206       */
6207      adjustSisterDocsAndPmts(x_paymentTab, x_docsInPmtTab,
6208          x_docErrorTab, x_errTokenTab);
6209 
6210      print_debuginfo(l_module_name, 'EXIT');
6211 
6212  END performPostHookProcess;
6213 
6214 /*--------------------------------------------------------------------
6215  | NAME:
6216  |     adjustSisterDocsAndPmts
6217  |
6218  | PURPOSE:
6219  |     If payment fails validation, then all it's constituent documents
6220  |     must also be failed. This is done in the validation flow itself.
6221  |
6222  |     However, one document may be related to another document by the
6223  |     payment grouping number. All related documents must be failed together.
6224  |     Therefore, if a document is failed in a payment, we must scan
6225  |     all payments for it's related documents and fail them also.
6226  |     Further, when a document is failed, the amount on the payment
6227  |     must be reduced accordingly.
6228  |
6229  |     This method handles such cascade failures.
6230  |
6231  | PARAMETERS:
6232  |     IN
6233  |
6234  |     OUT
6235  |
6236  |
6237  | RETURNS:
6238  |
6239  | NOTES:
6240  |
6241  *---------------------------------------------------------------------*/
6242  PROCEDURE adjustSisterDocsAndPmts(
6243      x_paymentTab     IN OUT NOCOPY paymentTabType,
6244      x_docsInPmtTab   IN OUT NOCOPY docsInPaymentTabType,
6245      x_docErrorTab    IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.docErrorTabType,
6246      x_errTokenTab    IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.trxnErrTokenTabType
6247      )
6248  IS
6249 
6250  l_doc_err_rec   IBY_TRANSACTION_ERRORS%ROWTYPE;
6251 
6252  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
6253                                              '.adjustSisterDocsAndPmts';
6254 
6255  BEGIN
6256 
6257      print_debuginfo(l_module_name, 'ENTER');
6258 
6259      IF (x_docsInPmtTab.COUNT > 0) THEN
6260      FOR i in x_docsInPmtTab.FIRST ..  x_docsInPmtTab.LAST LOOP
6261 
6262          /*
6263           * The loop below implements logic for cascade failure of documents.
6264           * Cascade failure occurs when one doc is failed for some reason,
6265           * and so, it's related docs must also be failed.
6266           *
6267           * We enter this loop looking for documents that are:
6268           * a. Failed for some reason
6269           * b. Not already failed by cascade failure
6270           *
6271           * Condition (b) is important otherwise, a doc that has been failed
6272           * by cascade earlier will continue to trigger off another round
6273           * of cascade failures. This cyclic effect is prevented by (b).
6274           */
6275          IF (x_docsInPmtTab(i).document_status <> DOC_STATUS_PAY_CREATED AND
6276              x_docsInPmtTab(i).document_status <> DOC_STATUS_RELN_FAIL)
6277              THEN
6278 
6279              FOR j in x_docsInPmtTab.FIRST ..  x_docsInPmtTab.LAST LOOP
6280 
6281                  /*
6282                   * For all documents which share this 'payment grouping
6283                   * number' do:
6284                   *
6285                   * a. Set the doc to 'failed' status
6286                   *
6287                   * b. Adjust the payment total on the
6288                   *    payment which contained this
6289                   *    document.
6290                   *
6291                   * Note: Take care to skip the triggering doc itself
6292                   * from this loop because this doc is already in
6293                   * failed status and the payment amount already adjusted.
6294                   */
6295 
6296                  /* skip the triggering doc */
6297                  IF (x_docsInPmtTab(i).document_id <>
6298                      x_docsInPmtTab(j).document_id) THEN
6299 
6300                      /*
6301                       * skip docs that we have already failed in an
6302                       * earlier iteration of this loop
6303                       */
6304                      IF (x_docsInPmtTab(j).document_status <>
6305                          DOC_STATUS_RELN_FAIL) THEN
6306 
6307                          IF (x_docsInPmtTab(i).payment_grp_num =
6308                              x_docsInPmtTab(j).payment_grp_num) THEN
6309 
6310                              /* a. fail document */
6311                              x_docsInPmtTab(j).document_status
6312                                  := DOC_STATUS_RELN_FAIL;
6313 
6314                              print_debuginfo(l_module_name, 'Cascaded doc '
6315                                  || 'failure: '
6316                                  || x_docsInPmtTab(i).document_id
6317                                  || ' -> '
6318                                  || x_docsInPmtTab(j).document_id
6319                                  || '. Related by '
6320                                  || x_docsInPmtTab(i).payment_grp_num
6321                                  || '.'
6322                                  );
6323 
6324                              /*
6325                               * Once we fail a doc, we must add a corresponding
6326                               * error message to the error table.
6327                               */
6328                              IBY_BUILD_UTILS_PKG.createErrorRecord(
6329                                  TRXN_TYPE_DOC,
6330                                  x_docsInPmtTab(j).document_id,
6331                                  x_docsInPmtTab(j).document_status,
6332                                  x_docsInPmtTab(j).calling_app_id,
6333                                  x_docsInPmtTab(j).calling_app_doc_id1,
6334                                  x_docsInPmtTab(j).calling_app_doc_id2,
6335                                  x_docsInPmtTab(j).calling_app_doc_id3,
6336                                  x_docsInPmtTab(j).calling_app_doc_id4,
6337                                  x_docsInPmtTab(j).calling_app_doc_id5,
6338                                  x_docsInPmtTab(j).pay_proc_ttype_cd,
6339                                  l_doc_err_rec,
6340                                  x_errTokenTab
6341                                  );
6342 
6343                              IBY_VALIDATIONSETS_PUB.insertIntoErrorTable(
6344                                  l_doc_err_rec, x_docErrorTab, x_errTokenTab);
6345 
6346                              /* b. adjust payment amount */
6347                              FOR k in x_paymentTab.FIRST ..  x_paymentTab.LAST
6348                                  LOOP
6349 
6350                                  IF (x_paymentTab(k).payment_id =
6351                                      x_docsInPmtTab(j).payment_id) THEN
6352 
6353                                      print_debuginfo(l_module_name,
6354                                          'For payment id '
6355                                          || x_paymentTab(k).payment_id
6356                                          || ', orig payment amount: '
6357                                          || x_paymentTab(k).payment_amount);
6358 
6359                                      x_paymentTab(k).payment_amount :=
6360                                          x_paymentTab(k).payment_amount
6361                                              - x_docsInPmtTab(j).
6362                                                    document_amount;
6363 
6364                                      print_debuginfo(l_module_name, 'Payment'
6365                                          || ' amount reduced by '
6366                                          || x_docsInPmtTab(j).document_amount
6367                                          || '. New payment amount: '
6368                                          || x_paymentTab(k).payment_amount);
6369 
6370                                      /*
6371                                       * If we have adjusted the payment amount
6372                                       * in the if-condition we can exit the
6373                                       * loop.
6374                                       */
6375                                      EXIT WHEN (1=1);
6376 
6377                                  END IF;
6378 
6379                              END LOOP; -- loop for adjusting payment amount
6380 
6381                          END IF;
6382 
6383                      END IF;
6384 
6385                  END IF;
6386 
6387              END LOOP; -- loop for scanning sister documents
6388 
6389          END IF;
6390 
6391      END LOOP; -- loop through all documents looking for failed docs
6392      END IF; -- if document count > 0
6393 
6394      print_debuginfo(l_module_name, 'EXIT');
6395 
6396  END adjustSisterDocsAndPmts;
6397 
6398 /*--------------------------------------------------------------------
6399  | NAME:
6400  |     getDocDetails
6401  |
6402  | PURPOSE:
6403  |     Returns the payment details calculated as per given formula on
6404  |     a given document.
6405  |
6406  |     The formula should be a SQL expression involving the attributes
6407  |     of the document - e.g., 'doc id || doc date'.
6408  |
6409  | PARAMETERS:
6410  |     IN
6411  |
6412  |     OUT
6413  |
6414  |
6415  | RETURNS:
6416  |
6417  | NOTES:
6418  |
6419  *---------------------------------------------------------------------*/
6420  FUNCTION getDocDetails(
6421      p_documentID         IN NUMBER,
6422      p_pmtDetailsFormula  IN VARCHAR2
6423      )
6424      RETURN VARCHAR2
6425  IS
6426  l_module_name      CONSTANT VARCHAR2(200)  := G_PKG_NAME || '.getDocDetails';
6427  l_calc_doc_details VARCHAR2(2000) := '';
6428  l_sql_string       VARCHAR2(4000) := '';
6429 
6430  BEGIN
6431 
6432      print_debuginfo(l_module_name, 'ENTER');
6433 
6434      l_sql_string := 'SELECT ' || p_pmtDetailsFormula || ' FROM '
6435                          || 'IBY_DOCS_PAYABLE_ALL WHERE '
6436                          || 'document_payable_id = :bind_var';
6437 
6438      EXECUTE IMMEDIATE l_sql_string INTO l_calc_doc_details USING p_documentID;
6439 
6440      print_debuginfo(l_module_name, 'EXIT');
6441 
6442      RETURN l_calc_doc_details;
6443 
6444  END getDocDetails;
6445 
6446 /*--------------------------------------------------------------------
6447  | NAME:
6448  |     callHook
6449  |
6450  | PURPOSE:
6451  |     Makes a call to the calling app specific hook. The calling
6452  |     app is expected to implement the hook using the naming
6453  |     convention <Application Short Name>_paymentsAdjustHook().
6454  |
6455  |
6456  | PARAMETERS:
6457  |     IN
6458  |
6459  |     OUT
6460  |
6461  |
6462  | RETURNS:
6463  |
6464  | NOTES:
6465  |
6466  *---------------------------------------------------------------------*/
6467  PROCEDURE callHook(
6468      p_payreq_id       IN IBY_PAY_SERVICE_REQUESTS.
6469                               payment_service_request_id%type
6470      )
6471  IS
6472 
6473  l_module_name      CONSTANT VARCHAR2(200)  := G_PKG_NAME || '.callHook';
6474  l_app_short_name   VARCHAR2(200);
6475 
6476  l_pkg_prefix       VARCHAR2(100);
6477  l_pkg_suffix       VARCHAR2(100) := '_PMT_CALLOUT_PKG';
6478  l_pkg_name         VARCHAR2(200);
6479 
6480  l_hook_name        VARCHAR2(500);
6481  l_stmt             VARCHAR2(1000);
6482 
6483  /*
6484   * Implementing the hook is optional for the calling app.
6485   * If the calling app does not implement the hook, then
6486   * the call to the hook will result in ORA-06576 error.
6487   *
6488   * There is no exception name associated with this code, so
6489   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
6490   * exception occurs, it is not fatal: we log the error and
6491   * proceed.
6492   *
6493   * If, on the other hand, the calling app implements the
6494   * hook, but the hook throws an exception, it is fatal
6495   * and we must abort the program (this will be caught
6496   * in WHEN OTHERS block).
6497   */
6498  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
6499  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
6500 
6501  BEGIN
6502 
6503      print_debuginfo(l_module_name, 'ENTER');
6504      print_debuginfo(l_module_name, 'Provided payment request id: '
6505          || p_payreq_id);
6506 
6507      /*
6508       * The hook method implemented by the calling app will
6509       * of the form:
6510       * <Application Short Name>_paymentsAdjustHook()
6511       *
6512       * First, get the application short name from the calling
6513       * application id.
6514       */
6515      BEGIN
6516 
6517          SELECT
6518              fnd.application_short_name
6519          INTO
6520              l_app_short_name
6521          FROM
6522              FND_APPLICATION fnd,
6523              IBY_PAY_SERVICE_REQUESTS req
6524          WHERE
6525              fnd.application_id             = req.calling_app_id AND
6526              req.payment_service_request_id = p_payreq_id
6527          ;
6528 
6529      EXCEPTION
6530          WHEN OTHERS THEN
6531              print_debuginfo(l_module_name, 'Fatal: Exception when attempting '
6532                  || 'to get calling application short name. Provided payment '
6533                  || 'request id: ' || p_payreq_id, FND_LOG.LEVEL_UNEXPECTED);
6534              print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE,
6535                  FND_LOG.LEVEL_UNEXPECTED);
6536              print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM,
6537                  FND_LOG.LEVEL_UNEXPECTED);
6538 
6539              /*
6540               * Propogate exception to caller.
6541               */
6542              RAISE;
6543      END;
6544 
6545      print_debuginfo(l_module_name, 'Retrieved app short name: '
6546          || l_app_short_name);
6547 
6548      /*
6549       * When we invoke the hook procedure in the external app,
6550       * we must use the hook signature as <package name>.<procedure name>.
6551       *
6552       * For some applications, the application short name cannot
6553       * be directly used in forming the package name.
6554       *
6555       * Example, for AP, the application short name is 'SQLAP',
6556       * but the AP packages begin as 'AP_XXXX'. Therefore,
6557       * we will convert the application short names into package
6558       * prefixes here.
6559       */
6560      CASE l_app_short_name
6561          WHEN 'SQLAP' THEN
6562              l_pkg_prefix := 'AP';
6563          ELSE
6564              l_pkg_prefix := l_app_short_name;
6565      END CASE;
6566 
6567      l_pkg_name := l_pkg_prefix || l_pkg_suffix;
6568 
6569      print_debuginfo(l_module_name, 'Constructed package name: '
6570          || l_pkg_name);
6571 
6572      /*
6573       * Now try to call the external app's implementation of the hook.
6574       * The calling app may or may not have implemented the hook, so
6575       * it's not fatal if the implementation does not exist.
6576       */
6577      l_hook_name := l_pkg_name || '.' || 'paymentsAdjustHook';
6578 
6579      print_debuginfo(l_module_name, 'Attempting to call hook: '
6580          || l_hook_name);
6581 
6582      l_stmt := 'CALL '|| l_hook_name || '()';
6583 
6584      BEGIN
6585 
6586          EXECUTE IMMEDIATE (l_stmt);
6587 
6588      EXCEPTION
6589 
6590          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
6591              print_debuginfo(l_module_name, 'Hook "' || l_hook_name
6592                  || '" not implemented by calling app '
6593                  || l_app_short_name || '.');
6594 
6595              print_debuginfo(l_module_name, 'Skipping hook call.');
6596 
6597          WHEN OTHERS THEN
6598              print_debuginfo(l_module_name, 'Fatal: External app hook '''
6599                  || l_hook_name  || ''', generated exception. Please '
6600                  || ' contact implementor of hook in product '
6601                  || l_app_short_name || ' for fix.',
6602                  FND_LOG.LEVEL_UNEXPECTED);
6603 
6604              print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE,
6605                  FND_LOG.LEVEL_UNEXPECTED);
6606              print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM,
6607                  FND_LOG.LEVEL_UNEXPECTED);
6608 
6609              /*
6610               * Propogate exception to caller.
6611               */
6612              RAISE;
6613      END;
6614 
6615      print_debuginfo(l_module_name, 'EXIT');
6616 
6617  END callHook;
6618 
6619 /*--------------------------------------------------------------------
6620  | NAME:
6621  |     getAdjustedPaymentData
6622  |
6623  | PURPOSE:
6624  |     Reads the data updated in the global temp tables by the calling app.
6625  |     The calling app would have updated the data when the external
6626  |     application hook was called. This method reads in the updated
6627  |     data.
6628  |
6629  | PARAMETERS:
6630  |     IN
6631  |
6632  |     OUT
6633  |
6634  |
6635  | RETURNS:
6636  |
6637  | NOTES:
6638  |
6639  *---------------------------------------------------------------------*/
6640  PROCEDURE getAdjustedPaymentData(
6641      x_hookPaymentTab      IN OUT NOCOPY hookPaymentTabType,
6642      x_hookDocsInPmtTab    IN OUT NOCOPY hookDocsInPaymentTabType
6643      )
6644  IS
6645 
6646  l_module_name  CONSTANT VARCHAR2(200) := G_PKG_NAME ||
6647                                               '.getAdjustedPaymentData';
6648 
6649      TYPE t_calling_app_id IS TABLE OF
6650      IBY_HOOK_PAYMENTS_T.calling_app_id%TYPE
6651      INDEX BY BINARY_INTEGER;
6652      TYPE t_call_app_pay_srv_req_code IS TABLE OF
6653      IBY_HOOK_PAYMENTS_T.call_app_pay_service_req_code%TYPE
6654      INDEX BY BINARY_INTEGER;
6655      TYPE t_payment_service_request_id IS TABLE OF
6656      IBY_HOOK_PAYMENTS_T.payment_service_request_id%TYPE
6657      INDEX BY BINARY_INTEGER;
6658      TYPE t_payment_id IS TABLE OF
6659      IBY_HOOK_PAYMENTS_T.payment_id%TYPE
6660      INDEX BY BINARY_INTEGER;
6661      TYPE t_payment_amount IS TABLE OF
6662      IBY_HOOK_PAYMENTS_T.payment_amount%TYPE
6663      INDEX BY BINARY_INTEGER;
6664      TYPE t_payment_currency_code IS TABLE OF
6665      IBY_HOOK_PAYMENTS_T.payment_currency_code%TYPE
6666      INDEX BY BINARY_INTEGER;
6667      TYPE t_dont_pay_flag IS TABLE OF
6668      IBY_HOOK_PAYMENTS_T.dont_pay_flag%TYPE
6669      INDEX BY BINARY_INTEGER;
6670      TYPE t_dont_pay_reason_code IS TABLE OF
6671      IBY_HOOK_PAYMENTS_T.dont_pay_reason_code%TYPE
6672      INDEX BY BINARY_INTEGER;
6673      TYPE t_dont_pay_description IS TABLE OF
6674      IBY_HOOK_PAYMENTS_T.dont_pay_description%TYPE
6675      INDEX BY BINARY_INTEGER;
6676      TYPE t_internal_bank_account_id IS TABLE OF
6677      IBY_HOOK_PAYMENTS_T.internal_bank_account_id%TYPE
6678      INDEX BY BINARY_INTEGER;
6679      TYPE t_ext_payee_id IS TABLE OF
6680      IBY_HOOK_PAYMENTS_T.ext_payee_id%TYPE
6681      INDEX BY BINARY_INTEGER;
6682      TYPE t_payee_party_id IS TABLE OF
6683      IBY_HOOK_PAYMENTS_T.payee_party_id%TYPE
6684      INDEX BY BINARY_INTEGER;
6685      TYPE t_party_site_id IS TABLE OF
6686      IBY_HOOK_PAYMENTS_T.party_site_id%TYPE
6687      INDEX BY BINARY_INTEGER;
6688      TYPE t_supplier_site_id IS TABLE OF
6689      IBY_HOOK_PAYMENTS_T.supplier_site_id%TYPE
6690      INDEX BY BINARY_INTEGER;
6691      TYPE t_org_id IS TABLE OF
6692      IBY_HOOK_PAYMENTS_T.org_id%TYPE
6693      INDEX BY BINARY_INTEGER;
6694      TYPE t_org_type IS TABLE OF
6695      IBY_HOOK_PAYMENTS_T.org_type%TYPE
6696      INDEX BY BINARY_INTEGER;
6697      TYPE t_external_bank_account_id IS TABLE OF
6698      IBY_HOOK_PAYMENTS_T.external_bank_account_id%TYPE
6699      INDEX BY BINARY_INTEGER;
6700      TYPE t_discount_amount_taken IS TABLE OF
6701      IBY_HOOK_PAYMENTS_T.discount_amount_taken%TYPE
6702      INDEX BY BINARY_INTEGER;
6703      TYPE t_payment_date IS TABLE OF
6704      IBY_HOOK_PAYMENTS_T.payment_date%TYPE
6705      INDEX BY BINARY_INTEGER;
6706      TYPE t_bank_charge_amount IS TABLE OF
6707      IBY_HOOK_PAYMENTS_T.bank_charge_amount%TYPE
6708      INDEX BY BINARY_INTEGER;
6709      TYPE t_created_by IS TABLE OF
6710      IBY_HOOK_PAYMENTS_T.created_by%TYPE
6711      INDEX BY BINARY_INTEGER;
6712      TYPE t_creation_date IS TABLE OF
6713      IBY_HOOK_PAYMENTS_T.creation_date%TYPE
6714      INDEX BY BINARY_INTEGER;
6715      TYPE t_last_updated_by IS TABLE OF
6716      IBY_HOOK_PAYMENTS_T.last_updated_by%TYPE
6717      INDEX BY BINARY_INTEGER;
6718      TYPE t_last_update_date IS TABLE OF
6719      IBY_HOOK_PAYMENTS_T.last_update_date%TYPE
6720      INDEX BY BINARY_INTEGER;
6721      TYPE t_last_update_login IS TABLE OF
6722      IBY_HOOK_PAYMENTS_T.last_update_login%TYPE
6723      INDEX BY BINARY_INTEGER;
6724      TYPE t_object_version_number IS TABLE OF
6725      IBY_HOOK_PAYMENTS_T.object_version_number%TYPE
6726      INDEX BY BINARY_INTEGER;
6727 
6728     l_calling_app_id                          t_calling_app_id;
6729     l_call_app_pay_srv_req_code               t_call_app_pay_srv_req_code;
6730     l_payment_service_request_id              t_payment_service_request_id;
6731     l_payment_id                              t_payment_id;
6732     l_payment_amount                          t_payment_amount;
6733     l_payment_currency_code                   t_payment_currency_code;
6734     l_dont_pay_flag                           t_dont_pay_flag;
6735     l_dont_pay_reason_code                    t_dont_pay_reason_code;
6736     l_dont_pay_description                    t_dont_pay_description;
6737     l_internal_bank_account_id                t_internal_bank_account_id;
6738     l_ext_payee_id                            t_ext_payee_id;
6739     l_payee_party_id                          t_payee_party_id;
6740     l_party_site_id                           t_party_site_id;
6741     l_supplier_site_id                        t_supplier_site_id;
6742     l_org_id                                  t_org_id;
6743     l_org_type                                t_org_type;
6744     l_external_bank_account_id                t_external_bank_account_id;
6745     l_discount_amount_taken                   t_discount_amount_taken;
6746     l_payment_date                            t_payment_date;
6747     l_bank_charge_amount                      t_bank_charge_amount;
6748     l_created_by                              t_created_by;
6749     l_creation_date                           t_creation_date;
6750     l_last_updated_by                         t_last_updated_by;
6751     l_last_update_date                        t_last_update_date;
6752     l_last_update_login                       t_last_update_login;
6753     l_object_version_number                   t_object_version_number;
6754 
6755 
6756  /* payments cursor */
6757  CURSOR c_payment_data
6758  IS
6759  SELECT
6760      calling_app_id,
6761      call_app_pay_service_req_code,
6762      payment_service_request_id,
6763      payment_id,
6764      payment_amount,
6765      payment_currency_code,
6766      dont_pay_flag,
6767      dont_pay_reason_code,
6768      dont_pay_description,
6769      internal_bank_account_id,
6770      ext_payee_id,
6771      payee_party_id,
6772      party_site_id,
6773      supplier_site_id,
6774      org_id,
6775      org_type,
6776      external_bank_account_id,
6777      discount_amount_taken,
6778      payment_date,
6779      bank_charge_amount,
6780      created_by,
6781      creation_date,
6782      last_updated_by,
6783      last_update_date,
6784      last_update_login,
6785      object_version_number
6786  FROM IBY_HOOK_PAYMENTS_T;
6787 
6788      TYPE ty_payment_id IS TABLE OF
6789      IBY_HOOK_DOCS_IN_PMT_T.payment_id%TYPE
6790      INDEX BY BINARY_INTEGER;
6791      TYPE ty_document_payable_id IS TABLE OF
6792      IBY_HOOK_DOCS_IN_PMT_T.document_payable_id%TYPE
6793      INDEX BY BINARY_INTEGER;
6794      TYPE ty_calling_app_id IS TABLE OF
6795      IBY_HOOK_DOCS_IN_PMT_T.calling_app_id%TYPE
6796      INDEX BY BINARY_INTEGER;
6797      TYPE ty_calling_app_doc_unique_ref1 IS TABLE OF
6798      IBY_HOOK_DOCS_IN_PMT_T.calling_app_doc_unique_ref1%TYPE
6799      INDEX BY BINARY_INTEGER;
6800      TYPE ty_calling_app_doc_unique_ref2 IS TABLE OF
6801      IBY_HOOK_DOCS_IN_PMT_T.calling_app_doc_unique_ref2%TYPE
6802      INDEX BY BINARY_INTEGER;
6803      TYPE ty_calling_app_doc_unique_ref3 IS TABLE OF
6804      IBY_HOOK_DOCS_IN_PMT_T.calling_app_doc_unique_ref3%TYPE
6805      INDEX BY BINARY_INTEGER;
6806      TYPE ty_calling_app_doc_unique_ref4 IS TABLE OF
6807      IBY_HOOK_DOCS_IN_PMT_T.calling_app_doc_unique_ref4%TYPE
6808      INDEX BY BINARY_INTEGER;
6809      TYPE ty_calling_app_doc_unique_ref5 IS TABLE OF
6810      IBY_HOOK_DOCS_IN_PMT_T.calling_app_doc_unique_ref5%TYPE
6811      INDEX BY BINARY_INTEGER;
6812      TYPE ty_document_amount IS TABLE OF
6813      IBY_HOOK_DOCS_IN_PMT_T.document_amount%TYPE
6814      INDEX BY BINARY_INTEGER;
6815      TYPE ty_document_currency_code IS TABLE OF
6816      IBY_HOOK_DOCS_IN_PMT_T.document_currency_code%TYPE
6817      INDEX BY BINARY_INTEGER;
6818      TYPE ty_amount_withheld IS TABLE OF
6819      IBY_HOOK_DOCS_IN_PMT_T.amount_withheld%TYPE
6820      INDEX BY BINARY_INTEGER;
6821      TYPE ty_dont_pay_flag IS TABLE OF
6822      IBY_HOOK_DOCS_IN_PMT_T.dont_pay_flag%TYPE
6823      INDEX BY BINARY_INTEGER;
6824      TYPE ty_dont_pay_reason_code IS TABLE OF
6825      IBY_HOOK_DOCS_IN_PMT_T.dont_pay_reason_code%TYPE
6826      INDEX BY BINARY_INTEGER;
6827      TYPE ty_dont_pay_description IS TABLE OF
6828      IBY_HOOK_DOCS_IN_PMT_T.dont_pay_description%TYPE
6829      INDEX BY BINARY_INTEGER;
6830      TYPE ty_pay_proc_trxn_type_code IS TABLE OF
6831      IBY_HOOK_DOCS_IN_PMT_T.pay_proc_trxn_type_code%TYPE
6832      INDEX BY BINARY_INTEGER;
6833      TYPE ty_internal_bank_account_id IS TABLE OF
6834      IBY_HOOK_DOCS_IN_PMT_T.internal_bank_account_id%TYPE
6835      INDEX BY BINARY_INTEGER;
6836      TYPE ty_ext_payee_id IS TABLE OF
6837      IBY_HOOK_DOCS_IN_PMT_T.ext_payee_id%TYPE
6838      INDEX BY BINARY_INTEGER;
6839      TYPE ty_payee_party_id IS TABLE OF
6840      IBY_HOOK_DOCS_IN_PMT_T.payee_party_id%TYPE
6841      INDEX BY BINARY_INTEGER;
6842      TYPE ty_party_site_id IS TABLE OF
6843      IBY_HOOK_DOCS_IN_PMT_T.party_site_id%TYPE
6844      INDEX BY BINARY_INTEGER;
6845      TYPE ty_supplier_site_id IS TABLE OF
6846      IBY_HOOK_DOCS_IN_PMT_T.supplier_site_id%TYPE
6847      INDEX BY BINARY_INTEGER;
6848      TYPE ty_org_id IS TABLE OF
6849      IBY_HOOK_DOCS_IN_PMT_T.org_id%TYPE
6850      INDEX BY BINARY_INTEGER;
6851      TYPE ty_org_type IS TABLE OF
6852      IBY_HOOK_DOCS_IN_PMT_T.org_type%TYPE
6853      INDEX BY BINARY_INTEGER;
6854      TYPE ty_external_bank_account_id IS TABLE OF
6855      IBY_HOOK_DOCS_IN_PMT_T.external_bank_account_id%TYPE
6856      INDEX BY BINARY_INTEGER;
6857      TYPE ty_payment_curr_discount_taken IS TABLE OF
6858      IBY_HOOK_DOCS_IN_PMT_T.payment_curr_discount_taken%TYPE
6859      INDEX BY BINARY_INTEGER;
6860      TYPE ty_created_by IS TABLE OF
6861      IBY_HOOK_DOCS_IN_PMT_T.created_by%TYPE
6862      INDEX BY BINARY_INTEGER;
6863      TYPE ty_creation_date IS TABLE OF
6864      IBY_HOOK_DOCS_IN_PMT_T.creation_date%TYPE
6865      INDEX BY BINARY_INTEGER;
6866      TYPE ty_last_updated_by IS TABLE OF
6867      IBY_HOOK_DOCS_IN_PMT_T.last_updated_by%TYPE
6868      INDEX BY BINARY_INTEGER;
6869      TYPE ty_last_update_date IS TABLE OF
6870      IBY_HOOK_DOCS_IN_PMT_T.last_update_date%TYPE
6871      INDEX BY BINARY_INTEGER;
6872      TYPE ty_last_update_login IS TABLE OF
6873      IBY_HOOK_DOCS_IN_PMT_T.last_update_login%TYPE
6874      INDEX BY BINARY_INTEGER;
6875      TYPE ty_object_version_number IS TABLE OF
6876      IBY_HOOK_DOCS_IN_PMT_T.object_version_number%TYPE
6877      INDEX BY BINARY_INTEGER;
6878 
6879 
6880     ll_payment_id                           ty_payment_id;
6881     ll_document_payable_id                  ty_document_payable_id;
6882     ll_calling_app_id                       ty_calling_app_id;
6883     ll_calling_app_doc_unique_ref1          ty_calling_app_doc_unique_ref1;
6884     ll_calling_app_doc_unique_ref2          ty_calling_app_doc_unique_ref2;
6885     ll_calling_app_doc_unique_ref3          ty_calling_app_doc_unique_ref3;
6886     ll_calling_app_doc_unique_ref4          ty_calling_app_doc_unique_ref4;
6887     ll_calling_app_doc_unique_ref5          ty_calling_app_doc_unique_ref5;
6888     ll_document_amount                      ty_document_amount;
6889     ll_document_currency_code               ty_document_currency_code;
6890     ll_amount_withheld                      ty_amount_withheld;
6891     ll_dont_pay_flag                        ty_dont_pay_flag;
6892     ll_dont_pay_reason_code                 ty_dont_pay_reason_code;
6893     ll_dont_pay_description                 ty_dont_pay_description;
6894     ll_pay_proc_trxn_type_code              ty_pay_proc_trxn_type_code;
6895     ll_internal_bank_account_id             ty_internal_bank_account_id;
6896     ll_ext_payee_id                         ty_ext_payee_id;
6897     ll_payee_party_id                       ty_payee_party_id;
6898     ll_party_site_id                        ty_party_site_id;
6899     ll_supplier_site_id                     ty_supplier_site_id;
6900     ll_org_id                               ty_org_id;
6901     ll_org_type                             ty_org_type;
6902     ll_external_bank_account_id             ty_external_bank_account_id;
6903     ll_payment_curr_discount_taken          ty_payment_curr_discount_taken;
6904     ll_created_by                           ty_created_by;
6905     ll_creation_date                        ty_creation_date;
6906     ll_last_updated_by                      ty_last_updated_by;
6907     ll_last_update_date                     ty_last_update_date;
6908     ll_last_update_login                    ty_last_update_login;
6909     ll_object_version_number                ty_object_version_number;
6910 
6911 
6912  /* documents cursor */
6913  CURSOR c_document_data
6914  IS
6915  SELECT
6916      payment_id,
6917      document_payable_id,
6918      calling_app_id,
6919      calling_app_doc_unique_ref1,
6920      calling_app_doc_unique_ref2,
6921      calling_app_doc_unique_ref3,
6922      calling_app_doc_unique_ref4,
6923      calling_app_doc_unique_ref5,
6924      document_amount,
6925      document_currency_code,
6926      amount_withheld,
6927      dont_pay_flag,
6928      dont_pay_reason_code,
6929      dont_pay_description,
6930      pay_proc_trxn_type_code,
6931      internal_bank_account_id,
6932      ext_payee_id,
6933      payee_party_id,
6934      party_site_id,
6935      supplier_site_id,
6936      org_id,
6937      org_type,
6938      external_bank_account_id,
6939      payment_curr_discount_taken,
6940      created_by,
6941      creation_date,
6942      last_updated_by,
6943      last_update_date,
6944      last_update_login,
6945      object_version_number
6946  FROM IBY_HOOK_DOCS_IN_PMT_T
6947  ORDER BY document_payable_id;
6948 
6949  BEGIN
6950 
6951      print_debuginfo(l_module_name, 'Fetching data '
6952          || 'from global temp tables');
6953 
6954      /*
6955       * Read in the payments and documents from the global
6956       * temp tables and store them into data structures.
6957       */
6958      OPEN  c_payment_data;
6959      FETCH c_payment_data BULK COLLECT INTO
6960          l_calling_app_id,
6961          l_call_app_pay_srv_req_code,
6962          l_payment_service_request_id,
6963          l_payment_id,
6964          l_payment_amount,
6965          l_payment_currency_code,
6966          l_dont_pay_flag,
6967          l_dont_pay_reason_code,
6968          l_dont_pay_description,
6969          l_internal_bank_account_id,
6970          l_ext_payee_id,
6971          l_payee_party_id,
6972          l_party_site_id,
6973          l_supplier_site_id,
6974          l_org_id,
6975          l_org_type,
6976          l_external_bank_account_id,
6977          l_discount_amount_taken,
6978          l_payment_date,
6979          l_bank_charge_amount,
6980          l_created_by,
6981          l_creation_date,
6982          l_last_updated_by,
6983          l_last_update_date,
6984          l_last_update_login,
6985          l_object_version_number
6986          ;
6987      CLOSE c_payment_data;
6988 
6989      IF (l_calling_app_id.COUNT <> 0) THEN
6990          FOR i IN l_calling_app_id.FIRST .. l_calling_app_id.LAST LOOP
6991              x_hookPaymentTab(i).calling_app_id :=
6992                  l_calling_app_id(i);
6993              x_hookPaymentTab(i).call_app_pay_service_req_code :=
6994                  l_call_app_pay_srv_req_code(i);
6995              x_hookPaymentTab(i).payment_service_request_id :=
6996                  l_payment_service_request_id(i);
6997              x_hookPaymentTab(i).payment_id :=
6998                  l_payment_id(i);
6999              x_hookPaymentTab(i).payment_amount :=
7000                  l_payment_amount(i);
7001              x_hookPaymentTab(i).payment_currency_code :=
7002                  l_payment_currency_code(i);
7003              x_hookPaymentTab(i).dont_pay_flag :=
7004                  l_dont_pay_flag(i);
7005              x_hookPaymentTab(i).dont_pay_reason_code :=
7006                  l_dont_pay_reason_code(i);
7007              x_hookPaymentTab(i).dont_pay_description :=
7008                  l_dont_pay_description(i);
7009              x_hookPaymentTab(i).internal_bank_account_id :=
7010                  l_internal_bank_account_id(i);
7011              x_hookPaymentTab(i).ext_payee_id :=
7012                  l_ext_payee_id(i);
7013              x_hookPaymentTab(i).payee_party_id :=
7014                  l_payee_party_id(i);
7015              x_hookPaymentTab(i).party_site_id :=
7016                  l_party_site_id(i);
7017              x_hookPaymentTab(i).supplier_site_id :=
7018                  l_supplier_site_id(i);
7019              x_hookPaymentTab(i).org_id :=
7020                  l_org_id(i);
7021              x_hookPaymentTab(i).org_type :=
7022                  l_org_type(i);
7023              x_hookPaymentTab(i).external_bank_account_id :=
7024                  l_external_bank_account_id(i);
7025              x_hookPaymentTab(i).discount_amount_taken :=
7026                  l_discount_amount_taken(i);
7027              x_hookPaymentTab(i).payment_date :=
7028                  l_payment_date(i);
7029              x_hookPaymentTab(i).bank_charge_amount :=
7030                  l_bank_charge_amount(i);
7031              x_hookPaymentTab(i).created_by :=
7032                  l_created_by(i);
7033              x_hookPaymentTab(i).creation_date :=
7034                  l_creation_date(i);
7035              x_hookPaymentTab(i).last_updated_by :=
7036                  l_last_updated_by(i);
7037              x_hookPaymentTab(i).last_update_date :=
7038                  l_last_update_date(i);
7039              x_hookPaymentTab(i).last_update_login :=
7040                  l_last_update_login(i);
7041              x_hookPaymentTab(i).object_version_number :=
7042                  l_object_version_number(i);
7043          END LOOP;
7044      END IF;
7045 
7046      OPEN  c_document_data;
7047      FETCH c_document_data BULK COLLECT INTO
7048          ll_payment_id,
7049          ll_document_payable_id,
7050          ll_calling_app_id,
7051          ll_calling_app_doc_unique_ref1,
7052          ll_calling_app_doc_unique_ref2,
7053          ll_calling_app_doc_unique_ref3,
7054          ll_calling_app_doc_unique_ref4,
7055          ll_calling_app_doc_unique_ref5,
7056          ll_document_amount,
7057          ll_document_currency_code,
7058          ll_amount_withheld,
7059          ll_dont_pay_flag,
7060          ll_dont_pay_reason_code,
7061          ll_dont_pay_description,
7062          ll_pay_proc_trxn_type_code,
7063          ll_internal_bank_account_id,
7064          ll_ext_payee_id,
7065          ll_payee_party_id,
7066          ll_party_site_id,
7067          ll_supplier_site_id,
7068          ll_org_id,
7069          ll_org_type,
7070          ll_external_bank_account_id,
7071          ll_payment_curr_discount_taken,
7072          ll_created_by,
7073          ll_creation_date,
7074          ll_last_updated_by,
7075          ll_last_update_date,
7076          ll_last_update_login,
7077          ll_object_version_number
7078          ;
7079      CLOSE c_document_data;
7080 
7081      IF (ll_payment_id.COUNT <> 0) THEN
7082          FOR i IN ll_payment_id.FIRST .. ll_payment_id.LAST LOOP
7083          x_hookDocsInPmtTab(i).payment_id :=
7084              ll_payment_id(i);
7085          x_hookDocsInPmtTab(i).document_payable_id :=
7086              ll_document_payable_id(i);
7087          x_hookDocsInPmtTab(i).calling_app_id :=
7088              ll_calling_app_id(i);
7089          x_hookDocsInPmtTab(i).calling_app_doc_unique_ref1 :=
7090              ll_calling_app_doc_unique_ref1(i);
7091          x_hookDocsInPmtTab(i).calling_app_doc_unique_ref2 :=
7092              ll_calling_app_doc_unique_ref2(i);
7093          x_hookDocsInPmtTab(i).calling_app_doc_unique_ref3 :=
7094              ll_calling_app_doc_unique_ref3(i);
7095          x_hookDocsInPmtTab(i).calling_app_doc_unique_ref4 :=
7096              ll_calling_app_doc_unique_ref4(i);
7097          x_hookDocsInPmtTab(i).calling_app_doc_unique_ref5 :=
7098              ll_calling_app_doc_unique_ref5(i);
7099          x_hookDocsInPmtTab(i).document_amount :=
7100              ll_document_amount(i);
7101          x_hookDocsInPmtTab(i).document_currency_code :=
7102              ll_document_currency_code(i);
7103          x_hookDocsInPmtTab(i).amount_withheld :=
7104              ll_amount_withheld(i);
7105          x_hookDocsInPmtTab(i).dont_pay_flag :=
7106              ll_dont_pay_flag(i);
7107          x_hookDocsInPmtTab(i).dont_pay_reason_code :=
7108              ll_dont_pay_reason_code(i);
7109          x_hookDocsInPmtTab(i).dont_pay_description :=
7110              ll_dont_pay_description(i);
7111          x_hookDocsInPmtTab(i).pay_proc_trxn_type_code :=
7112              ll_pay_proc_trxn_type_code(i);
7113          x_hookDocsInPmtTab(i).internal_bank_account_id :=
7114              ll_internal_bank_account_id(i);
7115          x_hookDocsInPmtTab(i).ext_payee_id :=
7116              ll_ext_payee_id(i);
7117          x_hookDocsInPmtTab(i).payee_party_id :=
7118              ll_payee_party_id(i);
7119          x_hookDocsInPmtTab(i).party_site_id :=
7120              ll_party_site_id(i);
7121          x_hookDocsInPmtTab(i).supplier_site_id :=
7122              ll_supplier_site_id(i);
7123          x_hookDocsInPmtTab(i).org_id :=
7124              ll_org_id(i);
7125          x_hookDocsInPmtTab(i).org_type :=
7126              ll_org_type(i);
7127          x_hookDocsInPmtTab(i).external_bank_account_id :=
7128              ll_external_bank_account_id(i);
7129          x_hookDocsInPmtTab(i).payment_curr_discount_taken :=
7130              ll_payment_curr_discount_taken(i);
7131          x_hookDocsInPmtTab(i).created_by :=
7132              ll_created_by(i);
7133          x_hookDocsInPmtTab(i).creation_date :=
7134              ll_creation_date(i);
7135          x_hookDocsInPmtTab(i).last_updated_by :=
7136              ll_last_updated_by(i);
7137          x_hookDocsInPmtTab(i).last_update_date :=
7138              ll_last_update_date(i);
7139          x_hookDocsInPmtTab(i).last_update_login :=
7140              ll_last_update_login(i);
7141          x_hookDocsInPmtTab(i).object_version_number :=
7142              ll_object_version_number(i);
7143          END LOOP;
7144      END IF;
7145 
7146      print_debuginfo(l_module_name, 'Finished fetching docs and '
7147          || 'payments from global temp tables.');
7148 
7149  END getAdjustedPaymentData;
7150 
7151 
7152 /*--------------------------------------------------------------------
7153  | NAME:
7154  |     failDocsOfPayment
7155  |
7156  | PURPOSE:
7157  |     For a given payment id, this method sets all the documents of
7158  |     that payment to 'failed' status in the PLSQL table of documents.
7159  |     The actual failure status to set for the document is passed
7160  |     in as an input parameter.
7161  |
7162  | PARAMETERS:
7163  |     IN
7164  |
7165  |     OUT
7166  |
7167  |
7168  | RETURNS:
7169  |
7170  | NOTES:
7171  |
7172  *---------------------------------------------------------------------*/
7173  PROCEDURE failDocsOfPayment(
7174      p_paymentId      IN            IBY_PAYMENTS_ALL.payment_id%TYPE,
7175      p_docStatus      IN            IBY_DOCS_PAYABLE_ALL.
7176                                           document_status%TYPE,
7177      x_docsInPmtTab   IN OUT NOCOPY docsInPaymentTabType,
7178      x_docErrorTab    IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.docErrorTabType,
7179      x_errTokenTab    IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.trxnErrTokenTabType
7180      )
7181  IS
7182  l_module_name      CONSTANT VARCHAR2(200)  := G_PKG_NAME ||
7183                                                    '.failDocsOfPayment';
7184  l_calc_doc_details VARCHAR2(2000) := '';
7185  l_sql_string       VARCHAR2(4000) := '';
7186 
7187  l_doc_err_rec      IBY_TRANSACTION_ERRORS%ROWTYPE;
7188 
7189  BEGIN
7190 
7191      print_debuginfo(l_module_name, 'ENTER');
7192 
7193      /*
7194       * Loop through docs, failing the ones that have the
7195       * have given payment id.
7196       */
7197      FOR j in  x_docsInPmtTab.FIRST ..  x_docsInPmtTab.LAST LOOP
7198 
7199          IF (x_docsInPmtTab(j).payment_id =
7200              p_paymentId) THEN
7201 
7202              x_docsInPmtTab(j).document_status
7203                  := p_docStatus;
7204 
7205              print_debuginfo(l_module_name, 'Failed document '
7206                  || x_docsInPmtTab(j).document_id
7207                  || ' because payment '
7208                  || p_paymentId
7209                  || ' has failed.');
7210 
7211              /*
7212               * Once we fail a doc, we must add a corresponding
7213               * error message to the error table.
7214               */
7215              IBY_BUILD_UTILS_PKG.createErrorRecord(
7216                  TRXN_TYPE_DOC,
7217                  x_docsInPmtTab(j).document_id,
7218                  p_docStatus,
7219                  x_docsInPmtTab(j).calling_app_id,
7220                  x_docsInPmtTab(j).calling_app_doc_id1,
7221                  x_docsInPmtTab(j).calling_app_doc_id2,
7222                  x_docsInPmtTab(j).calling_app_doc_id3,
7223                  x_docsInPmtTab(j).calling_app_doc_id4,
7224                  x_docsInPmtTab(j).calling_app_doc_id5,
7225                  x_docsInPmtTab(j).pay_proc_ttype_cd,
7226                  l_doc_err_rec,
7227                  x_errTokenTab
7228                  );
7229 
7230              /*
7231               * Add error record to errors table.
7232               */
7233              IBY_VALIDATIONSETS_PUB.insertIntoErrorTable(
7234                  l_doc_err_rec, x_docErrorTab, x_errTokenTab);
7235 
7236          END IF;
7237 
7238      END LOOP;
7239 
7240      print_debuginfo(l_module_name, 'EXIT');
7241 
7242  END failDocsOfPayment;
7243 
7244 /*--------------------------------------------------------------------
7245  | NAME:
7246  |     flagSeparateRemitAdvicePmts
7247  |
7248  | PURPOSE:
7249  |     Determines whether a separate remittance advice is required
7250  |     for a payment. If so, this method will set the 'separate remit
7251  |     advice flag' on the payment.
7252  |
7253  | PARAMETERS:
7254  |     IN
7255  |
7256  |
7257  |     OUT
7258  |
7259  |
7260  | RETURNS:
7261  |
7262  | NOTES:
7263  |
7264  *---------------------------------------------------------------------*/
7265  PROCEDURE flagSeparateRemitAdvicePmts(
7266      x_paymentTab    IN OUT NOCOPY paymentTabType,
7267      p_docsInPmtTab  IN            docsInPaymentTabType
7268      )
7269  IS
7270  l_module_name VARCHAR2(200) := G_PKG_NAME || '.flagSeparateRemitAdvicePmts';
7271 
7272  l_doc_count_limit          NUMBER(15);
7273  l_pmt_details_length_limit NUMBER(15);
7274  l_remitAdvicesSetupTab     remitAdviceRecTab;
7275  l_successDocCountTab       successDocsCountPerPmtTab;
7276 
7277  /* maps profile ids to system profile codes */
7278  l_profile_map        IBY_BUILD_UTILS_PKG.profileIdToCodeMapTabType;
7279  l_profile_code       IBY_PAYMENT_PROFILES.system_profile_code%TYPE;
7280 
7281  /*
7282   * Remittance advices cursor --  Only pick those
7283   * records that have the format attched... there
7284   * will be records in this table even if no format
7285   * is attached from the UI (the defaults are just accepted)
7286   */
7287  CURSOR c_remit_advices
7288  IS
7289  SELECT
7290      advice.system_profile_code,
7291      DECODE(advice.document_count_limit, NULL, -1,
7292          advice.document_count_limit),
7293      DECODE(advice.payment_details_length_limit, NULL, -1,
7294          advice.payment_details_length_limit)
7295  FROM
7296      IBY_REMIT_ADVICE_SETUP advice
7297  WHERE
7298      remittance_advice_format_code IS NOT NULL;
7299 
7300  BEGIN
7301 
7302      print_debuginfo(l_module_name, 'ENTER');
7303 
7304      /*
7305       * Read in the payments and documents from the global
7306       * temp tables and store them into data structures.
7307       */
7308      OPEN  c_remit_advices;
7309      FETCH c_remit_advices BULK COLLECT INTO l_remitAdvicesSetupTab;
7310      CLOSE c_remit_advices;
7311 
7312      print_debuginfo(l_module_name, 'Fetched '
7313          || l_remitAdvicesSetupTab.COUNT
7314          || ' records from the remittance advice setup table.');
7315 
7316      IF (l_remitAdvicesSetupTab.COUNT = 0) THEN
7317          print_debuginfo(l_module_name, 'No remittance advice information '
7318              || 'has been setup. Separate remittance advice will '
7319              || 'not be generated for any payment in this request. '
7320              || 'Exiting ..'
7321              );
7322          print_debuginfo(l_module_name, 'EXIT');
7323          RETURN;
7324      END IF;
7325 
7326      /*
7327       * For each payment, get the count of successful documents.
7328       * This is required to determine whether a separate remittance
7329       * needs to be flagged for that payment.
7330       */
7331      getNumSuccessDocsPerPayment(x_paymentTab, p_docsInPmtTab,
7332          l_successDocCountTab);
7333 
7334      /*
7335       * Get the mapping between profile ids and system profile codes.
7336       *
7337       * Individual payments contain the profile id
7338       * as an attribute. However, the config tables like
7339       * reit advice setup etc. contain settings
7340       * based on system profile codes.
7341       *
7342       * Therefore, we need this mapping for operations that
7343       * take place below.
7344       */
7345      IBY_BUILD_UTILS_PKG.getProfileMap(l_profile_map);
7346 
7347      /*
7348       * Loop through all the created payments, flagging any payments
7349       * that require a separate remittance advice.
7350       */
7351      FOR i in  x_paymentTab.FIRST ..  x_paymentTab.LAST LOOP
7352 
7353          IF (x_paymentTab(i).payment_status = PAY_STATUS_CREATED) THEN
7354 
7355              l_profile_code := IBY_BUILD_UTILS_PKG.getProfileCodeFromId(
7356                                    x_paymentTab(i).payment_profile_id,
7357                                    l_profile_map
7358                                    );
7359 
7360              FOR j in l_remitAdvicesSetupTab.FIRST ..
7361                           l_remitAdvicesSetupTab.LAST LOOP
7362 
7363                  IF (l_profile_code =
7364                          l_remitAdvicesSetupTab(j).payment_profile_cd) THEN
7365 
7366                      /*
7367                       * Check if this payment meets any of the remittance
7368                       * conditions. If yes, set the separate remittance
7369                       * advice flag to 'Y' for this payment.
7370                       */
7371 
7372                      /* max documents count limit condition */
7373                      IF (l_remitAdvicesSetupTab(j).doc_count_limit <> -1) THEN
7374 
7375                          /*
7376                           * The l_successDocCountTab table and the
7377                           * x_paymentTab table will have the exact same
7378                           * number of records, with the payment ids
7379                           * stored in the same order.
7380                           *
7381                           * Therefore, as far as payment ids are concerned
7382                           * we can use these tables interchangeably.
7383                           * Hence, we can directly pick up the count of
7384                           * successful docs for payment x_paymentTab(i)
7385                           * from l_successDocCountTab(i).
7386                           */
7387                          print_debuginfo(l_module_name, 'For payment '
7388                              || x_paymentTab(i).payment_id
7389                              || ' # success docs: '
7390                              || l_successDocCountTab(i).success_docs_count
7391                              || ', remittance advice doc limit: '
7392                              || l_remitAdvicesSetupTab(j).doc_count_limit
7393                              );
7394 
7395                          IF (l_successDocCountTab(i).success_docs_count
7396                              > l_remitAdvicesSetupTab(j).
7397                                    doc_count_limit) THEN
7398                              print_debuginfo(l_module_name, 'Setting '
7399                                  || ' separate remittance advice flag '
7400                                  || ' for payment '
7401                                  || x_paymentTab(i).payment_id
7402                                  );
7403                              x_paymentTab(i).separate_remit_advice_req_flag
7404                                  := 'Y';
7405                          END IF;
7406 
7407                      END IF;
7408 
7409                      /* payment details length limit comndition */
7410                      IF (l_remitAdvicesSetupTab(j).
7411                                 pmt_details_len_limit <> -1) THEN
7412 
7413                          print_debuginfo(l_module_name, 'For payment '
7414                              || x_paymentTab(i).payment_id
7415                              || ' pmt details length: '
7416                              || LENGTH(x_paymentTab(i).payment_details)
7417                              || ', length limit as per remit advc setup: '
7418                              || l_remitAdvicesSetupTab(j).
7419                                     pmt_details_len_limit
7420                              );
7421 
7422                          IF (LENGTH(x_paymentTab(i).payment_details)
7423                              > l_remitAdvicesSetupTab(j).
7424                                    pmt_details_len_limit) THEN
7425                              print_debuginfo(l_module_name, 'Setting '
7426                                  || 'separate remittance advice flag '
7427                                  || 'for payment '
7428                                  || x_paymentTab(i).payment_id
7429                                  );
7430                              x_paymentTab(i).separate_remit_advice_req_flag
7431                                  := 'Y';
7432                          END IF;
7433 
7434                      END IF;
7435 
7436                      /* both conditions unmet */
7437                      IF (l_remitAdvicesSetupTab(j).doc_count_limit = -1 AND
7438                          l_remitAdvicesSetupTab(j).pmt_details_len_limit = -1)
7439                          THEN
7440                          /*
7441                           * No remittance conditions have been set up
7442                           * for this profile, even though there is a
7443                           * remittance advice setup record. This means
7444                           * that remittance advice needs to be generated
7445                           * unconditionally for this payment.
7446                           */
7447                          print_debuginfo(l_module_name, 'Unconditionally '
7448                              || 'setting separate remittance advice flag '
7449                              || 'for payment '
7450                              || x_paymentTab(i).payment_id
7451                              );
7452                          x_paymentTab(i).separate_remit_advice_req_flag
7453                              := 'Y';
7454                      END IF;
7455 
7456                      /* Log if we are going to set set the remit flag */
7457                      IF (UPPER(x_paymentTab(i).separate_remit_advice_req_flag)
7458                          <> 'Y') THEN
7459                          print_debuginfo(l_module_name, 'Not setting '
7460                              || 'separate remittance advice flag '
7461                              || 'for payment '
7462                              || x_paymentTab(i).payment_id
7463                              );
7464                      END IF;
7465 
7466                  END IF; -- payment profile matches remit advc setup
7467 
7468              END LOOP; -- for each remittance advice setup
7469 
7470          END IF; -- if payment status = created
7471 
7472      END LOOP; -- for each payment
7473 
7474      print_debuginfo(l_module_name, 'EXIT');
7475 
7476  END flagSeparateRemitAdvicePmts;
7477 
7478 /*--------------------------------------------------------------------
7479  | NAME:
7480  |     getCountOfSuccessDocsPerPayment
7481  |
7482  | PURPOSE:
7483  |     Gets the count of successful documents for each payment.
7484  |
7485  | PARAMETERS:
7486  |     IN
7487  |
7488  |
7489  |     OUT
7490  |
7491  |
7492  | RETURNS:
7493  |
7494  | NOTES:
7495  |
7496  *---------------------------------------------------------------------*/
7497  PROCEDURE getNumSuccessDocsPerPayment(
7498      x_paymentTab          IN OUT NOCOPY paymentTabType,
7499      p_docsInPmtTab        IN            docsInPaymentTabType,
7500      x_successDocCountTab  IN OUT NOCOPY successDocsCountPerPmtTab
7501      )
7502  IS
7503  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
7504                                              '.getNumSuccessDocsPerPayment';
7505  l_successDocCountRec successDocsCountPerPmtRec;
7506 
7507  BEGIN
7508 
7509      print_debuginfo(l_module_name, 'ENTER');
7510 
7511      FOR i in  x_paymentTab.FIRST ..  x_paymentTab.LAST LOOP
7512 
7513          /*
7514           * Initialize the success docs count record with the
7515           * given payment id.
7516           */
7517          l_successDocCountRec.payment_id := x_paymentTab(i).payment_id;
7518          l_successDocCountRec.success_docs_count := 0;
7519 
7520          IF (p_docsInPmtTab.COUNT > 0) THEN
7521          FOR j in p_docsInPmtTab.FIRST .. p_docsInPmtTab.LAST LOOP
7522 
7523              IF (x_paymentTab(i).payment_id = p_docsInPmtTab(j).payment_id)
7524                  THEN
7525 
7526                  /*
7527                   * For each successful document for a particular
7528                   * payment, increment the success count.
7529                   *
7530                   * Successful documents will have their status set
7531                   * to 'PAYMENT_CREATED'.
7532                   */
7533                  IF (p_docsInPmtTab(j).document_status = DOC_STATUS_PAY_CREATED)
7534                      THEN
7535 
7536                      l_successDocCountRec.success_docs_count :=
7537                          l_successDocCountRec.success_docs_count + 1;
7538 
7539                  END IF;
7540 
7541              END IF;
7542 
7543          END LOOP; -- for all docs in payment
7544          END IF;
7545 
7546          /*
7547           * Add success docs count record to table and
7548           * do the same process for next payment.
7549           */
7550          x_successDocCountTab(x_successDocCountTab.COUNT + 1) :=
7551              l_successDocCountRec;
7552 
7553      END LOOP;  -- for all payments
7554 
7555      print_debuginfo(l_module_name, 'EXIT');
7556 
7557  END getNumSuccessDocsPerPayment;
7558 
7559 /*--------------------------------------------------------------------
7560  | NAME:
7561  |     performDBUpdates
7562  |
7563  | PURPOSE:
7564  |     This is the top level method that is called by the
7565  |     payment creation program to:
7566  |         1. insert payments to DB
7567  |         2. update documents with payment id
7568  |         3. update status of payment request
7569  |
7570  |     This method will read the 'rejection level' system option
7571  |     and do updates accordingly.
7572  |
7573  | PARAMETERS:
7574  |     IN
7575  |
7576  |
7577  |     OUT
7578  |
7579  |
7580  | RETURNS:
7581  |
7582  |
7583  | NOTES:
7584  |
7585  *---------------------------------------------------------------------*/
7586  PROCEDURE performDBUpdates(
7587      p_payreq_id          IN            IBY_PAY_SERVICE_REQUESTS.
7588                                           payment_service_request_id%type,
7589      p_rej_level          IN            VARCHAR2,
7590      p_review_pmts_flag   IN            VARCHAR2,
7591      x_paymentTab         IN OUT NOCOPY paymentTabType,
7592      x_docsInPmtTab       IN OUT NOCOPY docsInPaymentTabType,
7593      x_allPmtsSuccessFlag IN OUT NOCOPY BOOLEAN,
7594      x_allPmtsFailedFlag  IN OUT NOCOPY BOOLEAN,
7595      x_return_status      IN OUT NOCOPY VARCHAR2,
7596      x_docErrorTab        IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.docErrorTabType,
7597      x_errTokenTab        IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.
7598                                             trxnErrTokenTabType
7599      )
7600  IS
7601  l_module_name       CONSTANT VARCHAR2(200) := G_PKG_NAME ||
7602                                                    '.performDBUpdates';
7603  l_allsuccess_flag   BOOLEAN := TRUE;
7604  l_allfailed_flag    BOOLEAN := TRUE;
7605  l_request_status    VARCHAR2(200);
7606  l_orig_req_status   VARCHAR2(200);
7607 
7608  l_doc_err_rec       IBY_TRANSACTION_ERRORS%ROWTYPE;
7609  l_triggering_pmt_id IBY_PAYMENTS_ALL.payment_id%TYPE;
7610 
7611  BEGIN
7612 
7613      print_debuginfo(l_module_name, 'ENTER');
7614 
7615      /*
7616       * Store the original ppr status as it is useful in
7617       * determining the next status in some cases.
7618       */
7619      l_orig_req_status := x_return_status;
7620      print_debuginfo(l_module_name, 'Original request status = '
7621          || l_orig_req_status
7622          );
7623 
7624      /*
7625       * Print the rejection level system option
7626       */
7627      print_debuginfo(l_module_name, 'Rejection level system option set to: '
7628          || p_rej_level);
7629 
7630      /*
7631       * Find out whether all the payments within this
7632       * payment request have 'success' status. This
7633       * information is used below.
7634       */
7635      FOR i in  x_paymentTab.FIRST ..  x_paymentTab.LAST LOOP
7636          IF (x_paymentTab(i).payment_status <> PAY_STATUS_CREATED) THEN
7637              l_triggering_pmt_id := x_paymentTab(i).payment_id;
7638              l_allsuccess_flag := FALSE;
7639              print_debuginfo(l_module_name, 'At least one payment has '
7640                  || 'failed validation.');
7641              EXIT WHEN (1=1);
7642          END IF;
7643      END LOOP;
7644 
7645      /*
7646       * Check if all payments have failed for this
7647       * payment request. This information is used below.
7648       */
7649      FOR i in  x_paymentTab.FIRST .. x_paymentTab.LAST LOOP
7650          IF (x_paymentTab(i).payment_status = PAY_STATUS_CREATED) THEN
7651              l_allfailed_flag := FALSE;
7652              print_debuginfo(l_module_name, 'At least one payment has '
7653                  || 'has been successfully validated.');
7654              EXIT WHEN (1=1);
7655          END IF;
7656      END LOOP;
7657 
7658      /*
7659       * Update the status of the payments/documents
7660       * as per the rejection level (if necessary).
7661       */
7662      IF (p_rej_level = REJ_LVL_REQUEST) THEN
7663 
7664          IF (l_allsuccess_flag = FALSE) THEN
7665              /*
7666               * This means that at least one payment in this
7667               * payment request has failed.
7668               *
7669               * For 'request' rejection level:
7670               * If any payment in the request fails validation,
7671               * the entire payment request should be rejected;
7672               * So fail all payments in this payment request.
7673               */
7674              print_debuginfo(l_module_name, 'Failing all payments and '
7675                      || 'documents for payment request '
7676                      || p_payreq_id);
7677              FOR i in  x_paymentTab.FIRST ..  x_paymentTab.LAST LOOP
7678 
7679                  IF (x_paymentTab(i).payment_status = PAY_STATUS_CREATED) THEN
7680 
7681                      x_paymentTab(i).payment_status :=
7682                          PAY_STATUS_FAIL_BY_REJLVL;
7683 
7684                      /*
7685                       * Once we fail a payment, we need to create
7686                       * an error record and insert this record
7687                       * into the errors table.
7688                       */
7689                      IBY_BUILD_UTILS_PKG.createErrorRecord(
7690                          TRXN_TYPE_PMT,
7691                          x_paymentTab(i).payment_id,
7692                          x_paymentTab(i).payment_status,
7693                          NULL,
7694                          x_paymentTab(i).payment_id,
7695                          NULL,
7696                          NULL,
7697                          NULL,
7698                          NULL,
7699                          NULL,
7700                          l_doc_err_rec,
7701                          x_errTokenTab,
7702                          l_triggering_pmt_id
7703                          );
7704 
7705                      IBY_VALIDATIONSETS_PUB.insertIntoErrorTable(
7706                          l_doc_err_rec, x_docErrorTab, x_errTokenTab);
7707 
7708                      failDocsOfPayment(x_paymentTab(i).payment_id,
7709                          DOC_STATUS_PAY_VAL_FAIL,
7710                          x_docsInPmtTab, x_docErrorTab,
7711                          x_errTokenTab);
7712 
7713                  END IF;
7714 
7715              END LOOP;
7716 
7717              /* set the status of the payment request to failed */
7718              l_request_status := REQ_STATUS_FAIL_PAY_CR;
7719 
7720          ELSE
7721 
7722              IF (UPPER(p_review_pmts_flag) = 'Y') THEN
7723 
7724                  IF (l_orig_req_status = 'RETRY_PAYMENT_CREATION') THEN
7725 
7726                      /*
7727                       * Fix for bug 5209530:
7728                       *
7729                       * If the pmts of a payment request have already
7730                       * been reviewed and the payment request has
7731                       * re-entered the pmt creation flow, then the pmts
7732                       * will again been validated.
7733                       *
7734                       * If all pmts are successful, do not again ask the
7735                       * user to review the payments, instead allow
7736                       * the ppr to proceed formward.
7737                       *
7738                       * For example, when documents of a pmt(s) are
7739                       * dismissed and the pmts have been re-validated,
7740                       * and there are no errors allow the ppr to
7741                       * proceed forward instead of setting it for
7742                       * user review again.
7743                       */
7744                      l_request_status := REQ_STATUS_PAY_CRTD;
7745 
7746                      print_debuginfo(l_module_name, 'Payment request is '
7747                          || 're-entering payment creation and all payments '
7748                          || 'are valid. User need not review pmts again.'
7749                          );
7750 
7751                      print_debuginfo(l_module_name, 'Setting payment '
7752                          || 'request status to '
7753                          || l_request_status
7754                          || '.'
7755                          );
7756 
7757                  ELSE
7758 
7759 
7760                      /*
7761                       * If review proposed payments flag is set, then
7762                       * move successful payment requests also to
7763                       * 'pending review' status.
7764                       */
7765                      l_request_status := REQ_STATUS_USER_REVW;
7766                      print_debuginfo(l_module_name, 'Review proposed payments '
7767                          || 'flag has been set. Setting status of successful '
7768                          || 'request to pending review.');
7769 
7770                  END IF;
7771 
7772              ELSE
7773 
7774                  /* set the status of the payment request to pmts created */
7775                  l_request_status := REQ_STATUS_PAY_CRTD;
7776 
7777              END IF;
7778 
7779          END IF;
7780 
7781      ELSIF (p_rej_level = REJ_LVL_PAYMENT) THEN
7782 
7783          /*
7784           * Check if all payments in the request have failed.
7785           */
7786          IF (l_allfailed_flag = TRUE) THEN
7787 
7788              l_request_status := REQ_STATUS_FAIL_PAY_CR;
7789 
7790          ELSE
7791 
7792              /*
7793               * At least one payment in the request was
7794               * successful.
7795               */
7796              IF (UPPER(p_review_pmts_flag) = 'Y') THEN
7797 
7798                  IF (l_orig_req_status = 'RETRY_PAYMENT_CREATION') THEN
7799 
7800                      /*
7801                       * Fix for bug 5209530:
7802                       *
7803                       * If the pmts of a payment request have already
7804                       * been reviewed and the payment request has
7805                       * re-entered the pmt creation flow, then the pmts
7806                       * will again been validated.
7807                       *
7808                       * If all pmts are successful, do not again ask the
7809                       * user to review the payments, instead allow
7810                       * the ppr to proceed formward.
7811                       *
7812                       * For example, when documents of a pmt(s) are
7813                       * dismissed and the pmts have been re-validated,
7814                       * and there are no errors allow the ppr to
7815                       * proceed forward instead of setting it for
7816                       * user review again.
7817                       */
7818                      l_request_status := REQ_STATUS_PAY_CRTD;
7819 
7820                      print_debuginfo(l_module_name, 'Payment request is '
7821                          || 're-entering payment creation and all payments '
7822                          || 'are valid. User need not review pmts again.'
7823                          );
7824 
7825                      print_debuginfo(l_module_name, 'Setting payment '
7826                          || 'request status to '
7827                          || l_request_status
7828                          || '.'
7829                          );
7830 
7831                  ELSE
7832 
7833                      /*
7834                       * If review proposed payments flag is set, then
7835                       * move successful payment requests also to
7836                       * 'pending review' status.
7837                       */
7838                      l_request_status := REQ_STATUS_USER_REVW;
7839 
7840                      print_debuginfo(l_module_name, 'Review proposed payments '
7841                          || 'flag has been set. Setting status of successful '
7842                          || 'request to pending review.');
7843 
7844                  END IF;
7845 
7846              ELSE
7847 
7848                  /* set the status of the payment request to pmts created */
7849                  l_request_status := REQ_STATUS_PAY_CRTD;
7850 
7851                  print_debuginfo(l_module_name, 'Review proposed payments '
7852                      || 'flag has not been set. Setting status of successful '
7853                      || 'request to created.');
7854 
7855              END IF;
7856 
7857          END IF;
7858 
7859      ELSIF (p_rej_level = REJ_LVL_NONE) THEN
7860 
7861          IF (l_allsuccess_flag = TRUE) THEN
7862 
7863              IF (UPPER(p_review_pmts_flag) = 'Y') THEN
7864 
7865                  IF (l_orig_req_status = 'RETRY_PAYMENT_CREATION') THEN
7866 
7867                      /*
7868                       * Fix for bug 5209530:
7869                       *
7870                       * If the pmts of a payment request have already
7871                       * been reviewed and the payment request has
7872                       * re-entered the pmt creation flow, then the pmts
7873                       * will again been validated.
7874                       *
7875                       * If all pmts are successful, do not again ask the
7876                       * user to review the payments, instead allow
7877                       * the ppr to proceed formward.
7878                       *
7879                       * For example, when documents of a pmt(s) are
7880                       * dismissed and the pmts have been re-validated,
7881                       * and there are no errors allow the ppr to
7882                       * proceed forward instead of setting it for
7883                       * user review again.
7884                       */
7885                      l_request_status := REQ_STATUS_PAY_CRTD;
7886 
7887                      print_debuginfo(l_module_name, 'Payment request is '
7888                          || 're-entering payment creation and all payments '
7889                          || 'are valid. User need not review pmts again.'
7890                          );
7891 
7892                      print_debuginfo(l_module_name, 'Setting payment '
7893                          || 'request status to '
7894                          || l_request_status
7895                          || '.'
7896                          );
7897 
7898                  ELSE
7899 
7900                      l_request_status := REQ_STATUS_USER_REVW;
7901 
7902                  END IF;
7903 
7904              ELSE
7905 
7906                  l_request_status := REQ_STATUS_PAY_CRTD;
7907 
7908              END IF;
7909 
7910          ELSE
7911 
7912              /*
7913               * If control come here, it means that there
7914               * are some payments that failed validation
7915               * and the rejecttion level is set to 'NONE'.
7916               *
7917               * This means that we should not kick back
7918               * the underlying documents back to AP. Instead,
7919               * we should set the PPR status to
7920               * REQ_STATUS_USER_REVW_ERR. This will allow
7921               * the user to review the failed payments
7922               * in the UI and take corrective action.
7923               */
7924              l_request_status := REQ_STATUS_USER_REVW_ERR;
7925 
7926              /*
7927               * Fix for bug 6709700:
7928               *
7929               * By default, when a payment fails validation
7930               * it is set to PAY_STATUS_REJECTED status. This indicates
7931               * a hard failure and the constituent documents of the
7932               * payment will be kicked back to the calling app.
7933               *
7934               * However, when th rejection level is set to NONE,
7935               * the user wishes to review the failed payments.
7936               * Therefore, we should soft-fail the payments
7937               * that fail validation and not kick back the
7938               * underlying documents back to the calling app.
7939               *
7940               * For the soft failure of the ayment, we use
7941               * a separate status - PAY_STATUS_FAIL_VALID.
7942               *
7943               * Update all the payments that are in PAY_STATUS_REJECTED
7944               * status to PAY_STATUS_FAIL_VALID.
7945               */
7946              FOR j in  x_paymentTab.FIRST ..  x_paymentTab.LAST LOOP
7947 
7948                  IF (x_paymentTab(j).payment_status = PAY_STATUS_REJECTED) THEN
7949 
7950                      x_paymentTab(j).payment_status :=
7951                          PAY_STATUS_FAIL_VALID;
7952 
7953                      print_debuginfo(l_module_name, 'Updating payment '
7954                          || x_paymentTab(j).payment_id
7955                          || ' to soft failure status.'
7956                          );
7957 
7958                  END IF;
7959 
7960              END LOOP;
7961 
7962          END IF; -- if all success == true
7963 
7964      ELSE
7965 
7966          print_debuginfo(l_module_name, 'Unknown rejection level: '
7967              || p_rej_level
7968              || '. Aborting payment creation ..',
7969              FND_LOG.LEVEL_UNEXPECTED
7970              );
7971 
7972          APP_EXCEPTION.RAISE_EXCEPTION;
7973 
7974      END IF;
7975 
7976      /*
7977       * All payments for this payment request have been
7978       * created and stored in a PLSQL table. Now write these
7979       * payments to the database.
7980       */
7981      updatePayments(x_paymentTab);
7982 
7983      /*
7984       * Update the documents table by providing a payment id to
7985       * each document.
7986       */
7987      updateDocsWithPaymentID(x_docsInPmtTab);
7988 
7989      /*
7990       * If any payments/documents were failed, the IBY_TRANSACTION_
7991       * ERRORS table must be populated with the corresponding error
7992       * messages.
7993       */
7994      IBY_VALIDATIONSETS_PUB.insert_transaction_errors('N', x_docErrorTab,
7995          x_errTokenTab);
7996 
7997      /*
7998       * Update the status of the payment request.
7999       */
8000      print_debuginfo(l_module_name, 'Updating status of payment request '
8001          || p_payreq_id || ' to ' || l_request_status || '.');
8002 
8003      UPDATE
8004          iby_pay_service_requests
8005      SET
8006          payment_service_request_status = l_request_status
8007      WHERE
8008          payment_service_request_id = p_payreq_id
8009      ;
8010 
8011      /* Pass back the request status to the caller */
8012      x_return_status := l_request_status;
8013 
8014      /*
8015       * Pass the 'all payments success' and 'all payments
8016       * failed' flags back to the caller.
8017       *
8018       * These flag will be used in raising business events.
8019       */
8020      x_allPmtsSuccessFlag := l_allsuccess_flag;
8021      x_allPmtsFailedFlag  := l_allfailed_flag;
8022 
8023      print_debuginfo(l_module_name, 'EXIT');
8024 
8025  EXCEPTION
8026 
8027      WHEN OTHERS THEN
8028          print_debuginfo(l_module_name, 'Fatal: Exception when updating '
8029              || 'payment request/payment/document status after payment '
8030              || 'creation. All changes will be rolled back. Payment request '
8031              || 'id is ' || p_payreq_id, FND_LOG.LEVEL_UNEXPECTED);
8032          print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE,
8033              FND_LOG.LEVEL_UNEXPECTED);
8034          print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM,
8035              FND_LOG.LEVEL_UNEXPECTED);
8036 
8037          /*
8038           * Propogate exception to caller.
8039           */
8040          RAISE;
8041 
8042  END performDBUpdates;
8043 
8044 /*--------------------------------------------------------------------
8045  | NAME:
8046  |     applyPaymentValidationSets
8047  |
8048  | PURPOSE:
8049  |     Invokes the validation sets applicable to every payment. The
8050  |     status of the payment will be set to 'validation failed' if
8051  |     the payment fails any invoked validation.
8052  |
8053  |
8054  | PARAMETERS:
8055  |     IN
8056  |
8057  |     OUT
8058  |
8059  |
8060  | RETURNS:
8061  |
8062  | NOTES:
8063  |
8064  *---------------------------------------------------------------------*/
8065  PROCEDURE applyPaymentValidationSets(
8066      x_paymentTab    IN OUT NOCOPY paymentTabType,
8067      x_docsInPmtTab  IN OUT NOCOPY docsInPaymentTabType,
8068      x_docErrorTab   IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.docErrorTabType,
8069      x_errTokenTab   IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.trxnErrTokenTabType
8070      )
8071  IS
8072 
8073  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
8074                                              '.applyPaymentValidationSets';
8075  l_valSetsTab  IBY_PAYGROUP_PUB.paymentValSetsTab;
8076  l_stmt        VARCHAR2(200);
8077  l_result      NUMBER := 0;
8078  l_check_val   BOOLEAN := FALSE;
8079 
8080  /*
8081   * Pick up all validation sets applicable to a particular payment.
8082   */
8083 
8084  /*
8085   * Pick up all validation sets applicable to a particular payment.
8086   */
8087  CURSOR  c_pmt_val_sets(p_payment_id IBY_PAYMENTS_ALL.payment_id%TYPE)
8088  IS
8089  SELECT DISTINCT
8090      val_asgn.validation_assignment_id,
8091      val_asgn.val_assignment_entity_type,
8092      val.validation_set_display_name,
8093      val.validation_set_code,
8094      val.validation_code_package,
8095      val.validation_code_entry_point
8096  FROM
8097      IBY_VALIDATION_SETS_VL    val,
8098      IBY_VAL_ASSIGNMENTS       val_asgn,
8099      IBY_PAYMENTS_ALL          pmts,
8100      IBY_SYS_PMT_PROFILES_B    sys_prof,
8101      IBY_ACCT_PMT_PROFILES_B   acct_prof,
8102      IBY_TRANSMIT_CONFIGS_B    txconf,
8103      IBY_TRANSMIT_PROTOCOLS_B  txproto,
8104      CE_BANK_ACCOUNTS          iba,
8105      CE_BANK_BRANCHES_V        iba_branch
8106  WHERE
8107      pmts.payment_id               = p_payment_id
8108      AND pmts.internal_bank_account_id = iba.bank_account_id
8109      AND iba_branch.branch_party_id    = iba.bank_branch_id
8110      AND val.validation_set_code   = val_asgn.validation_set_code
8111      AND val.validation_level_code = 'PAYMENT'
8112      AND (val_asgn.val_assignment_entity_type    = 'METHOD'
8113               AND val_asgn.assignment_entity_id  =
8114                       pmts.payment_method_code
8115           OR val_asgn.val_assignment_entity_type = 'INTBANKACCOUNT'
8116               AND val_asgn.assignment_entity_id  =
8117                   pmts.internal_bank_account_id
8118           OR val_asgn.val_assignment_entity_type = 'FORMAT'
8119               AND val_asgn.assignment_entity_id  =
8120                   sys_prof.payment_format_code
8121           OR val_asgn.val_assignment_entity_type = 'BANK'
8122               AND val_asgn.assignment_entity_id  =
8123                   sys_prof.bepid
8124           OR val_asgn.val_assignment_entity_type = 'TRANSPROTOCOL'
8125               AND val_asgn.assignment_entity_id  =
8126                   txconf.transmit_protocol_code
8127           )
8128      AND pmts.payment_profile_id        = acct_prof.payment_profile_id(+)
8129      AND acct_prof.transmit_configuration_id
8130                                         = txconf.transmit_configuration_id(+)
8131      AND txconf.transmit_protocol_code  = txproto.transmit_protocol_code(+)
8132      AND sys_prof.system_profile_code(+) = acct_prof.system_profile_code
8133      AND NVL(val_asgn.inactive_date, sysdate+1) > sysdate
8134 
8135      /*
8136       * Fix for bug 4997133:
8137       *
8138       * Pick up all applicable validation sets that match
8139       * the payment method and country code on this payment.
8140       * (if payment method /country code on validation
8141       * set is null, it means that the validation set is
8142       * applicable to all payment methods / countries).
8143       */
8144      AND (NVL(pmts.payment_method_code, '0') =
8145              NVL(val_asgn.payment_method_code, '0') OR
8146              val_asgn.payment_method_code IS NULL
8147          )
8148      AND (iba_branch.country = val_asgn.territory_code OR
8149          val_asgn.territory_code IS NULL
8150          )
8151      ;
8152 
8153  BEGIN
8154 
8155      print_debuginfo(l_module_name, 'ENTER');
8156 
8157      FOR i in  x_paymentTab.FIRST ..  x_paymentTab.LAST LOOP
8158 
8159          /*
8160           * Pick up the validation sets applicable to this
8161           * payment.
8162           */
8163          OPEN  c_pmt_val_sets(x_paymentTab(i).payment_id);
8164          FETCH c_pmt_val_sets BULK COLLECT INTO l_valSetsTab;
8165          CLOSE c_pmt_val_sets;
8166 
8167          IF (l_valSetsTab.COUNT = 0) THEN
8168 
8169              /*
8170               * Fix for bug 5742548:
8171               *
8172               * If no validation sets are applicable this
8173               * time round, then this payment will pass
8174               * validation.
8175               *
8176               * But it is possible that this payment has
8177               * failed validation earlier, in which cas
8178               * the old errors will still be active against
8179               * this payment.
8180               *
8181               * Inactivate the old errors so that the UI
8182               * correctly shows that there are no active
8183               * validation errors against this payment
8184               * at this time.
8185               */
8186              IBY_BUILD_UTILS_PKG.inactivateOldErrors(
8187                  x_paymentTab(i).payment_id,
8188                  TRXN_TYPE_PMT
8189                  );
8190 
8191              print_debuginfo(l_module_name, 'No validation sets were '
8192                  || 'linked to payment '
8193                  || x_paymentTab(i).payment_id
8194                  || ' Skipping payment '
8195                  || 'validations for this payment..');
8196 
8197              print_debuginfo(l_module_name, '+-------------------------+');
8198 
8199          ELSE
8200 
8201              FOR j in l_valSetsTab.FIRST .. l_valSetsTab.LAST LOOP
8202 
8203                  /*
8204                   * Fix for bug 5440434:
8205                   *
8206                   * Before doing any validations, set any
8207                   * existing validation error messages that
8208                   * exist against this payment to 'inactive'
8209                   * status in the IBY_TRANSACTION_ERRORS table.
8210                   *
8211                   * Unless we do this, the old errors will
8212                   * continue to show up against this payment
8213                   * in the IBY UI even if the payment is validated
8214                   * successfully this time round.
8215                   */
8216                  IBY_BUILD_UTILS_PKG.inactivateOldErrors(
8217                      x_paymentTab(i).payment_id,
8218                      TRXN_TYPE_PMT
8219                      );
8220 
8221                  print_debuginfo(l_module_name, 'Applicable validation set ('
8222                      || l_valSetsTab(j).val_assign_entity_type
8223                      || ') : '
8224                      || l_valSetsTab(j).val_set_name
8225                      );
8226 
8227                  /*
8228                   * Dynamically call the validation set applicable
8229                   * to the current payment.
8230                   */
8231                  l_stmt := 'CALL '
8232                                || l_valSetsTab(j).val_code_pkg
8233                                || '.'
8234                                || l_valSetsTab(j).val_code_entry_pt
8235                                || '(:1,:2,:3,:4,:5)';
8236 
8237                  print_debuginfo(l_module_name, 'Calling ' || l_stmt);
8238 
8239                  EXECUTE IMMEDIATE (l_stmt) USING
8240                      IN l_valSetsTab(j).val_assign_id,
8241                      IN l_valSetsTab(j).val_set_code,
8242                      IN x_paymentTab(i).payment_id,
8243                      IN 'N',
8244                      OUT l_result;
8245 
8246                  print_debuginfo(l_module_name, 'Finished executing '
8247                      || l_stmt);
8248 
8249                  print_debuginfo(l_module_name, 'Result: '
8250                      || l_result);
8251 
8252                  /*
8253                   * If payment fails validation, then:
8254                   * 1. Set the status of the payment to failed
8255                   * 2. Fail all docs of this payment
8256                   */
8257                  IF (l_result <> 0) THEN
8258 
8259                      /*
8260                       * Check if the payment has already been failed.
8261                       */
8262                      l_check_val := checkIfPmtAlreadyFailed(
8263                                         x_paymentTab(i).payment_id,
8264                                         x_paymentTab
8265                                         );
8266 
8267                      /*
8268                       * If a payment has not been failed before,
8269                       * fail the payment and its child documents.
8270                       *
8271                       * Otherwise, skip this step.
8272                       */
8273                      IF (l_check_val = FALSE) THEN
8274 
8275                          print_debuginfo(l_module_name, 'Payment '
8276                              || x_paymentTab(i).payment_id
8277                              || ' failed validation.');
8278 
8279                          x_paymentTab(i).payment_status
8280                              := PAY_STATUS_REJECTED;
8281 
8282                          /*
8283                           * Validation error messages against the payment
8284                           * will be handled by the validation set itself.
8285                           * So no need to create an error record for the
8286                           * payment here.
8287                           */
8288 
8289                          /*
8290                           * For the failed payment, set the status of its
8291                           * child documents to failed as well.
8292                           */
8293 
8294                          failDocsOfPayment(x_paymentTab(i).payment_id,
8295                              DOC_STATUS_PAY_VAL_FAIL, x_docsInPmtTab,
8296                              x_docErrorTab, x_errTokenTab);
8297 
8298                      ELSE
8299 
8300                          print_debuginfo(l_module_name, 'Payment '
8301                              || x_paymentTab(i).payment_id
8302                              || ' has already failed validation.'
8303                              || '. No need to fail this payment and '
8304                              || 'its child documents again.'
8305                              );
8306 
8307                      END IF; -- if l_check_val = FALSE
8308 
8309                  END IF; -- result <> 0
8310 
8311                  print_debuginfo(l_module_name, '+-------------------------+');
8312 
8313              END LOOP; -- for each val set applicable to this payment
8314 
8315          END IF; -- if count of val sets <> 0
8316 
8317      END LOOP; -- for each payment in this payment request
8318 
8319      print_debuginfo(l_module_name, 'EXIT');
8320 
8321  END applyPaymentValidationSets;
8322 
8323 /*--------------------------------------------------------------------
8324  | NAME:
8325  |     raiseBizEvents
8326  |
8327  | PURPOSE:
8328  |     Raises business events (if necessary) to inform the calling
8329  |     app of failed documents (which were failed because their payments
8330  |     were failed).
8331  |
8332  | PARAMETERS:
8333  |     IN
8334  |
8335  |     OUT
8336  |
8337  |
8338  | RETURNS:
8339  |
8340  | NOTES:
8341  |
8342  *---------------------------------------------------------------------*/
8343  PROCEDURE raiseBizEvents(
8344      p_payreq_id          IN            VARCHAR2,
8345      p_cap_payreq_cd      IN            VARCHAR2,
8346      p_cap_id             IN            NUMBER,
8347      p_rej_level          IN            VARCHAR2,
8348      p_review_pmts_flag   IN            VARCHAR2,
8349      p_allPmtsSuccessFlag IN            BOOLEAN,
8350      p_allPmtsFailedFlag  IN            BOOLEAN
8351      )
8352  IS
8353 
8354  l_module_name   CONSTANT VARCHAR2(200) := G_PKG_NAME || '.raiseBizEvents';
8355  l_xml_clob      CLOB;
8356  l_event_name    VARCHAR2(200);
8357  l_event_key     NUMBER;
8358  l_param_names   JTF_VARCHAR2_TABLE_300;
8359  l_param_vals    JTF_VARCHAR2_TABLE_300;
8360 
8361  l_return_status  VARCHAR2(500);
8362  l_rej_doc_id_list     IBY_DISBURSE_UI_API_PUB_PKG.docPayIDTab;
8363  l_rej_doc_status_list IBY_DISBURSE_UI_API_PUB_PKG.docPayStatusTab;
8364 
8365  BEGIN
8366 
8367      print_debuginfo(l_module_name, 'ENTER');
8368      print_debuginfo(l_module_name, 'Payreq id: '
8369          || p_payreq_id);
8370 
8371      /*
8372       * Print the rejection level system option
8373       */
8374      print_debuginfo(l_module_name, 'Rejection level system option: '
8375          || p_rej_level);
8376 
8377      /*
8378       * These tables are used to pass event keys
8379       * to the business event.
8380       */
8381      l_param_names := JTF_VARCHAR2_TABLE_300();
8382      l_param_vals  := JTF_VARCHAR2_TABLE_300();
8383 
8384      /*
8385       * The event key uniquely identifies a specific
8386       * occurance of an event. Therefore, it should be
8387       * a sequence number.
8388       */
8389      SELECT IBY_EVENT_KEY_S.nextval INTO l_event_key
8390      FROM DUAL;
8391 
8392      IF (p_rej_level = REJ_LVL_REQUEST) THEN
8393 
8394          /*
8395           * For request level rejections, even if one
8396           * payment within the request fails, then the
8397           * entire payment request should be failed.
8398           */
8399 
8400          IF (p_allPmtsSuccessFlag <> TRUE) THEN
8401 
8402              /*
8403               * Invoke the callout API with the payment request id.
8404               * This API should trigger the calling application to
8405               * fail the payment request and all it's associated
8406               * docs.
8407               */
8408              print_debuginfo(l_module_name, 'Going to invoke API call '
8409                  || 'remove_payment_request()');
8410 
8411              /*
8412               * Invoke API to inform calling application
8413               * about the rejected payment request. This API
8414               * will remove all the payments and documents payable
8415               * in this payment request from the processing cycle
8416               * and inform the calling application about this fact.
8417               */
8418              IBY_DISBURSE_UI_API_PUB_PKG.remove_payment_request (
8419                  p_payreq_id,
8420                  l_return_status
8421                  );
8422 
8423              print_debuginfo(l_module_name, 'Return status of '
8424                  || 'remove_payment_request() API call: '
8425                  || l_return_status
8426                  );
8427 
8428              IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
8429 
8430                  print_debuginfo(l_module_name, 'API call did not succeed. '
8431                      || 'Aborting build program .. ',
8432                      FND_LOG.LEVEL_UNEXPECTED
8433                      );
8434 
8435                  APP_EXCEPTION.RAISE_EXCEPTION;
8436 
8437              END IF;
8438 
8439 
8440          ELSE
8441 
8442              /*
8443               * This means all payments were successfully
8444               * created for this payment service request.
8445               *
8446               * Notify the user only of 'review proposed
8447               * payments flag' has been set.
8448               */
8449 
8450              IF (UPPER(p_review_pmts_flag) = 'Y') THEN
8451 
8452                  /*
8453                   * Raise a business event with the payment request id.
8454                   * This business event should trigger the calling app
8455                   * to launch a workflow for the user to review the
8456                   * proposed payments.
8457                   */
8458                  l_event_name :=
8459                      'oracle.apps.iby.buildprogram.validation.notify_user';
8460 
8461                  l_param_names.EXTEND;
8462                  l_param_vals.EXTEND;
8463                  l_param_names(1) := 'calling_app_id';
8464                  l_param_vals(1)  := p_cap_id;
8465 
8466                  l_param_names.EXTEND;
8467                  l_param_vals.EXTEND;
8468                  l_param_names(1) := 'pay_service_request_id';
8469                  l_param_vals(1)  := p_cap_payreq_cd;
8470 
8471                  print_debuginfo(l_module_name, 'Going to raise biz event '
8472                      || l_event_name);
8473 
8474                  iby_workflow_pvt.raise_biz_event(l_event_name, l_event_key,
8475                      l_param_names, l_param_vals);
8476 
8477                  print_debuginfo(l_module_name, 'Raised biz event '
8478                      || l_event_name || ' with key '
8479                      || l_event_key  || '.');
8480 
8481              ELSE
8482 
8483                  print_debuginfo(l_module_name, 'Not raising biz event '
8484                      || l_event_name || '. Reason: All payments '
8485                      || 'were valid.');
8486 
8487              END IF;
8488 
8489          END IF;
8490 
8491      ELSIF (p_rej_level = REJ_LVL_PAYMENT) THEN
8492 
8493          /*
8494           * Check if all payments in the request have failed;
8495           * in that case, fail the request and invoke a callout
8496           * API to inform the calling app that all payments (and
8497           * so documents) in the request have been failed.
8498           *
8499           * The calling app should use this information to unlock
8500           * those documents and re-submit them in a new request
8501           * after making suitable changed after making suitable
8502           * changes.
8503           */
8504          IF (p_allPmtsFailedFlag = TRUE) THEN
8505 
8506              /*
8507               * Invoke the callout API with the payment request id.
8508               * This API should trigger the calling application to
8509               * fail the payment request and all it's associated
8510               * docs.
8511               */
8512              print_debuginfo(l_module_name, 'Going to invoke API call '
8513                  || 'remove_payment_request()');
8514 
8515              /*
8516               * Invoke API to inform calling application
8517               * about the rejected payment request. This API
8518               * will remove all the documents payable in this
8519               * payment request from the processing cycle and
8520               * inform the calling application about this fact.
8521               */
8522              IBY_DISBURSE_UI_API_PUB_PKG.remove_payment_request (
8523                  p_payreq_id,
8524                  l_return_status
8525                  );
8526 
8527              print_debuginfo(l_module_name, 'Return status of '
8528                  || 'remove_payment_request() API call: '
8529                  || l_return_status
8530                  );
8531 
8532              IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
8533 
8534                  print_debuginfo(l_module_name, 'API call did not succeed. '
8535                      || 'Aborting build program .. ',
8536                      FND_LOG.LEVEL_UNEXPECTED
8537                      );
8538 
8539                  APP_EXCEPTION.RAISE_EXCEPTION;
8540 
8541              END IF;
8542 
8543          ELSE
8544 
8545              /*
8546               * If at lease one payment has succeeded and the
8547               * 'review proposed payments' flag is set to 'Y',
8548               * then, we must raise the notify user business
8549               * event.
8550               */
8551              l_event_name :=
8552                  'oracle.apps.iby.buildprogram.validation.notify_user';
8553 
8554              print_debuginfo(l_module_name, 'Going to raise biz event '
8555                  || l_event_name);
8556 
8557              /*
8558               * Raise this business event only if the 'review
8559               * proposed payments' flag is set to 'Y'.
8560               */
8561              IF (UPPER(p_review_pmts_flag) = 'Y') THEN
8562 
8563                  /*
8564                   * Raise a business event with the payment request id.
8565                   * This business event should trigger the calling app
8566                   * to launch a workflow for the user to review the
8567                   * proposed payments.
8568                   */
8569                  l_event_name :=
8570                      'oracle.apps.iby.buildprogram.validation.notify_user';
8571 
8572                  l_param_names.EXTEND;
8573                  l_param_vals.EXTEND;
8574                  l_param_names(1) := 'calling_app_id';
8575                  l_param_vals(1)  := p_cap_id;
8576 
8577                  l_param_names.EXTEND;
8578                  l_param_vals.EXTEND;
8579                  l_param_names(1) := 'pay_service_request_id';
8580                  l_param_vals(1)  := p_cap_payreq_cd;
8581 
8582                  print_debuginfo(l_module_name, 'Going to raise biz event '
8583                      || l_event_name);
8584 
8585                  iby_workflow_pvt.raise_biz_event(l_event_name, l_event_key,
8586                      l_param_names, l_param_vals);
8587 
8588                  print_debuginfo(l_module_name, 'Raised biz event '
8589                      || l_event_name || ' with key '
8590                      || l_event_key  || '.');
8591 
8592              END IF; -- review payments flag == 'Y'
8593 
8594                  /*
8595                   * Invoke a callout API with the list of failed
8596                   * documents (derived from the payments). This
8597                   * API call should trigger the calling app
8598                   * to fail these docs.
8599                   */
8600                  print_debuginfo(l_module_name, 'Going to invoke API '
8601                      || 'remove_documents_payable()');
8602 
8603                  /*
8604                   * Select all docs that:
8605                   * 1. Have the given pay req id
8606                   * 2. Are not in 'payments_created' status
8607                   * 3. Were updated in the process of payment creation
8608                   *    (some docs might have failed earlier in document
8609                   *    validation flow. We don't want to pick them up)
8610                   */
8611                  getRejectedDocs(p_payreq_id, l_rej_doc_id_list,
8612                      l_rej_doc_status_list);
8613 
8614                  IF (l_rej_doc_id_list.COUNT = 0) THEN
8615 
8616                      print_debuginfo(l_module_name, 'Not invoking API '
8617                          || 'remove_documents_payable() because all '
8618                          || 'payments were successfully '
8619                          || 'created. So no failed documents '
8620                          || 'to notify.'
8621                          );
8622 
8623                  ELSE
8624 
8625                      print_debuginfo(l_module_name, 'Printing list of failed '
8626                          || 'documents');
8627 
8628                      FOR i IN l_rej_doc_id_list.FIRST ..
8629                          l_rej_doc_id_list.LAST LOOP
8630 
8631                          print_debuginfo(l_module_name, 'Doc id: '
8632                              || l_rej_doc_id_list(i)
8633                              || ', doc status: '
8634                              || l_rej_doc_status_list(i)
8635                              );
8636 
8637                      END LOOP;
8638 
8639                      print_debuginfo(l_module_name, 'Going to invoke '
8640                          || 'remove_documents_payable() API'
8641                          );
8642 
8643                      /*
8644                       * Invoke API to inform calling application
8645                       * about the rejected documents.
8646                       */
8647                      IBY_DISBURSE_UI_API_PUB_PKG.remove_documents_payable (
8648                          l_rej_doc_id_list,
8649                          l_rej_doc_status_list,
8650                          l_return_status
8651                          );
8652 
8653                      print_debuginfo(l_module_name, 'Return status of '
8654                          || 'remove_documents_payable() API call: '
8655                          || l_return_status
8656                          );
8657 
8658                      IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
8659 
8660                          print_debuginfo(l_module_name, 'API call did not '
8661                              || 'succeed. Aborting build program .. ',
8662                              FND_LOG.LEVEL_UNEXPECTED
8663                              );
8664 
8665                          APP_EXCEPTION.RAISE_EXCEPTION;
8666 
8667                      END IF;
8668 
8669                  END IF; -- if rejected doc count != 0
8670 
8671          END IF; -- all payments failed flag = TRUE
8672 
8673      ELSIF (p_rej_level = REJ_LVL_NONE) THEN
8674 
8675          IF (p_allPmtsSuccessFlag <> TRUE) THEN
8676 
8677              /*
8678               * Raise a business event with the payment request id.
8679               * This business event should trigger the calling app
8680               * to launch a workflow for the user to review the
8681               * proposed payments.
8682               */
8683              l_event_name :=
8684                  'oracle.apps.iby.buildprogram.validation.notify_user_error';
8685 
8686              l_param_names.EXTEND;
8687              l_param_vals.EXTEND;
8688              l_param_names(1) := 'calling_app_id';
8689              l_param_vals(1)  := p_cap_id;
8690 
8691              l_param_names.EXTEND;
8692              l_param_vals.EXTEND;
8693              l_param_names(1) := 'pay_service_request_id';
8694              l_param_vals(1)  := p_cap_payreq_cd;
8695 
8696              print_debuginfo(l_module_name, 'Going to raise biz event '
8697                  || l_event_name);
8698 
8699              iby_workflow_pvt.raise_biz_event(l_event_name, l_event_key,
8700                  l_param_names, l_param_vals);
8701 
8702              print_debuginfo(l_module_name, 'Raised biz event '
8703                  || l_event_name || ' with key '
8704                  || l_event_key  || '.');
8705 
8706          ELSE
8707 
8708              /*
8709               * This means that all payments were successfully
8710               * created for this payment service request.
8711               */
8712 
8713              IF (UPPER(p_review_pmts_flag) = 'Y') THEN
8714 
8715                  /*
8716                   * Raise a business event with the payment request id.
8717                   * This business event should trigger the calling app
8718                   * to launch a workflow for the user to review the
8719                   * proposed payments.
8720                   */
8721 
8722                  l_event_name :=
8723                      'oracle.apps.iby.buildprogram.validation.notify_user';
8724 
8725                  l_param_names.EXTEND;
8726                  l_param_vals.EXTEND;
8727                  l_param_names(1) := 'calling_app_id';
8728                  l_param_vals(1)  := p_cap_id;
8729 
8730                  l_param_names.EXTEND;
8731                  l_param_vals.EXTEND;
8732                  l_param_names(1) := 'pay_service_request_id';
8733                  l_param_vals(1)  := p_cap_payreq_cd;
8734 
8735                  print_debuginfo(l_module_name, 'Going to raise biz event '
8736                      || l_event_name);
8737 
8738                  iby_workflow_pvt.raise_biz_event(l_event_name, l_event_key,
8739                      l_param_names, l_param_vals);
8740 
8741                  print_debuginfo(l_module_name, 'Raised biz event '
8742                      || l_event_name || ' with key '
8743                      || l_event_key  || '.');
8744 
8745              ELSE
8746 
8747                  print_debuginfo(l_module_name, 'Not raising biz event '
8748                      || l_event_name || ' because all payments were '
8749                      || 'successfully created. So no failed documents '
8750                      || 'to notify.'
8751                      );
8752 
8753              END IF;
8754 
8755          END IF;
8756 
8757      ELSE
8758 
8759          print_debuginfo(l_module_name, 'Unknown rejection level: '
8760              || p_rej_level
8761              || '. Aborting payment creation ..',
8762              FND_LOG.LEVEL_UNEXPECTED
8763              );
8764 
8765          APP_EXCEPTION.RAISE_EXCEPTION;
8766 
8767      END IF;
8768 
8769      print_debuginfo(l_module_name, 'EXIT');
8770 
8771  EXCEPTION
8772      WHEN OTHERS THEN
8773          print_debuginfo(l_module_name, 'Fatal: Exception when attempting '
8774              || 'to raise business event.', FND_LOG.LEVEL_UNEXPECTED);
8775          print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE,
8776              FND_LOG.LEVEL_UNEXPECTED);
8777          print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM,
8778              FND_LOG.LEVEL_UNEXPECTED);
8779 
8780          /*
8781           * Propogate exception to caller.
8782           */
8783          RAISE;
8784 
8785  END raiseBizEvents;
8786 
8787 /*--------------------------------------------------------------------
8788  | NAME:
8789  |     checkIfPmtAlreadyFailed
8790  |
8791  | PURPOSE:
8792  |     Checks if a payment has already failed validation. If yes, this
8793  |     method returns TRUE; else, it return FALSE.
8794  |
8795  | PARAMETERS:
8796  |     IN
8797  |
8798  |     OUT
8799  |
8800  |
8801  | RETURNS:
8802  |
8803  | NOTES:
8804  |
8805  *---------------------------------------------------------------------*/
8806  FUNCTION checkIfPmtAlreadyFailed(
8807      p_paymentId   IN   IBY_PAYMENTS_ALL.payment_id%TYPE,
8808      p_paymentTab  IN   paymentTabType
8809      )
8810  RETURN BOOLEAN
8811  IS
8812  l_ret_val BOOLEAN := FALSE;
8813  BEGIN
8814 
8815      FOR i IN p_paymentTab.FIRST .. p_paymentTab.LAST LOOP
8816 
8817          IF (p_paymentTab(i).payment_id = p_paymentId) THEN
8818 
8819              IF (p_paymentTab(i).payment_status = PAY_STATUS_REJECTED) THEN
8820 
8821                  l_ret_val := TRUE;
8822                  EXIT;
8823 
8824              END IF;
8825 
8826          END IF;
8827 
8828      END LOOP;
8829 
8830      RETURN l_ret_val;
8831 
8832  END checkIfPmtAlreadyFailed;
8833 
8834 /*--------------------------------------------------------------------
8835  | NAME:
8836  |     checkIfPmtAlreadyAdded
8837  |
8838  | PURPOSE:
8839  |     Checks if a payment has already been added to the list of
8840  |     negative amount payments that qualify for credit memo adjustment.
8841  |     If yes, this method returns TRUE; else, it return FALSE.
8842  |
8843  | PARAMETERS:
8844  |     IN
8845  |
8846  |     OUT
8847  |
8848  |
8849  | RETURNS:
8850  |
8851  | NOTES:
8852  |
8853  *---------------------------------------------------------------------*/
8854  FUNCTION checkIfPmtAlreadyAdded(
8855      p_paymentId   IN   IBY_PAYMENTS_ALL.payment_id%TYPE,
8856      p_paymentTab  IN   pmtIdsTab
8857      )
8858  RETURN BOOLEAN
8859  IS
8860  l_ret_val BOOLEAN := FALSE;
8861  BEGIN
8862 
8863      /* return false if no records in negative payments array */
8864      IF (p_paymentTab.COUNT = 0) THEN
8865 
8866          l_ret_val := FALSE;
8867 
8868          RETURN l_ret_val;
8869 
8870      END IF;
8871 
8872      /*
8873       * Loop through negative payments list, scanning for
8874       * given payment id
8875       */
8876      FOR i IN p_paymentTab.FIRST .. p_paymentTab.LAST LOOP
8877 
8878          IF (p_paymentTab(i) = p_paymentId) THEN
8879 
8880              l_ret_val := TRUE;
8881              EXIT;
8882 
8883          END IF;
8884 
8885      END LOOP;
8886 
8887      RETURN l_ret_val;
8888 
8889  END checkIfPmtAlreadyAdded;
8890 
8891 /*--------------------------------------------------------------------
8892  | NAME:
8893  |     getXMLClob
8894  |
8895  | PURPOSE:
8896  |     Returns an XML clob with the list of documents that satisfy
8897  |     the executed select condition.
8898  |
8899  | PARAMETERS:
8900  |     IN
8901  |
8902  |     OUT
8903  |
8904  |
8905  | RETURNS:
8906  |
8907  | NOTES:
8908  |
8909  |     XML generation from PLSQL is evolving rapidly.
8910  |
8911  |     The code below uses DBMS_XMLQuery() to generate XML
8912  |     from a SELECT statement.
8913  |
8914  |     DBMS_XMLQuery() uses Java code internally, and is slow.
8915  |
8916  |     Better ways to generate XML are:
8917  |     1. DBMS_XMLGEN
8918  |        DBMS_XMLGEN is a built-in package in C. It is fast. However,
8919  |        it is supported only in Oracle 9i and above.
8920  |
8921  |     2. SQLX
8922  |        This is the new emerging standard for SQL -> XML.
8923  |        It is both fast and easy. However, only Oracle 9i and
8924  |        above.
8925  |
8926  *---------------------------------------------------------------------*/
8927  FUNCTION getXMLClob(
8928      p_payreq_id     IN VARCHAR2
8929      )
8930      RETURN CLOB
8931  IS
8932  l_module_name  CONSTANT VARCHAR2(200)  := G_PKG_NAME || '.getXMLClob';
8933  l_xml_clob     CLOB := NULL;
8934 
8935  l_ctx          DBMS_XMLQuery.ctxType;
8936  l_sql          VARCHAR2(2000);
8937  l_sqlcode      NUMBER;
8938  l_sqlerrm      VARCHAR2(300);
8939 
8940  BEGIN
8941 
8942      print_debuginfo(l_module_name, 'ENTER');
8943 
8944      /*
8945       * Note:
8946       *
8947       * Replace DBMS_XMLQuery with DBMS_XMLGEN or SQLX
8948       * when Oracle 9i is minimum requirement in tech
8949       * stack (see notes above).
8950       */
8951 
8952      /*
8953       * Select all docs that:
8954       * 1. Have the given pay req id
8955       * 2. Are not in 'payments_created' status
8956       * 3. Were updated in the process of payment creation
8957       *    (some docs might have failed earlier in document
8958       *    validation flow. We don't want to pick them up).
8959       */
8960      l_sql := 'SELECT calling_app_id, '
8961                   || 'calling_app_doc_unique_ref1'
8962                   || 'calling_app_doc_unique_ref2'
8963                   || 'calling_app_doc_unique_ref3'
8964                   || 'calling_app_doc_unique_ref4'
8965                   || 'calling_app_doc_unique_ref5'
8966                   || 'pay_proc_trxn_type_id '
8967                   || 'FROM IBY_DOCS_PAYABLE_ALL '
8968                   || 'WHERE payment_service_request_id = :payreq_id '
8969                   || 'AND  document_status <> :doc_status '
8970                   || 'AND  payment_id IS NOT NULL';
8971 
8972      l_ctx := DBMS_XMLQuery.newContext(l_sql);
8973      DBMS_XMLQuery.setBindValue(l_ctx, 'payreq_id', p_payreq_id);
8974      DBMS_XMLQuery.setBindValue(l_ctx, 'doc_status', DOC_STATUS_PAY_CREATED);
8975      DBMS_XMLQuery.useNullAttributeIndicator(l_ctx, TRUE);
8976 
8977      /* raise an exception if no rows were found */
8978      DBMS_XMLQuery.setRaiseException(l_ctx, TRUE);
8979      DBMS_XMLQuery.setRaiseNoRowsException(l_ctx, TRUE);
8980      DBMS_XMLQuery.propagateOriginalException(l_ctx, TRUE);
8981 
8982      l_xml_clob := DBMS_XMLQuery.getXML(l_ctx);
8983      DBMS_XMLQuery.closeContext(l_ctx);
8984 
8985      print_debuginfo(l_module_name, 'EXIT');
8986 
8987      RETURN l_xml_clob;
8988 
8989  EXCEPTION
8990 
8991      WHEN OTHERS THEN
8992 
8993          DBMS_XMLQuery.getExceptionContent(l_ctx, l_sqlcode, l_sqlerrm);
8994          print_debuginfo(l_module_name, 'SQL code: '   || l_sqlcode);
8995          print_debuginfo(l_module_name, 'SQL err msg: '|| l_sqlerrm);
8996 
8997          /*
8998           * Do not raise exception if no rows found.
8999           * It means all payments were successful.
9000           * return NULL clob to caller.
9001           *
9002           * 1403 = NO_DATA_FOUND
9003           *
9004           * Note: We are unable to explicitly catch the
9005           * NO_DATA_FOUND exception here because the caller
9006           * raises some other exception. So we have to check
9007           * value of the original error code instead.
9008           */
9009          IF (l_sqlcode = 1403) THEN
9010              print_debuginfo(l_module_name, 'No rows were returned for query;'
9011                  || ' Returning null xml clob.');
9012              RETURN NULL;
9013          END IF;
9014 
9015          print_debuginfo(l_module_name, 'Fatal: Exception when attempting '
9016              || 'to raise business event.', FND_LOG.LEVEL_UNEXPECTED);
9017          print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE,
9018              FND_LOG.LEVEL_UNEXPECTED);
9019          print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM,
9020              FND_LOG.LEVEL_UNEXPECTED);
9021 
9022          /*
9023           * Propogate exception to caller.
9024           */
9025          RAISE;
9026 
9027  END getXMLClob;
9028 
9029 /*--------------------------------------------------------------------
9030  | NAME:
9031  |     getRejectedDocs
9032  |
9033  | PURPOSE:
9034  |     Performs a database query to get all failed documents which
9035  |     are part of payments created for the given payment request.
9036  |     These failed documents are put into data structure and
9037  |     returned to the caller.
9038  |
9039  |
9040  | PARAMETERS:
9041  |     IN
9042  |
9043  |     OUT
9044  |
9045  |
9046  | RETURNS:
9047  |
9048  | NOTES:
9049  |
9050  |
9051  *---------------------------------------------------------------------*/
9052  PROCEDURE getRejectedDocs(
9053      p_payreq_id    IN VARCHAR2,
9054      x_docIDTab     IN OUT NOCOPY IBY_DISBURSE_UI_API_PUB_PKG.docPayIDTab,
9055      x_docStatusTab IN OUT NOCOPY IBY_DISBURSE_UI_API_PUB_PKG.docPayStatusTab
9056      )
9057  IS
9058  l_module_name  CONSTANT VARCHAR2(200) := G_PKG_NAME || '.getRejectedDocs';
9059  l_rej_docs_list IBY_VALIDATIONSETS_PUB.rejectedDocTabType;
9060 
9061  /*
9062   * Cursor to get list of failed documents which are part of payments
9063   * created for the given payment service request.
9064   */
9065  CURSOR c_rejected_docs (p_payreq_id IBY_PAY_SERVICE_REQUESTS.
9066                                          payment_service_request_id%TYPE)
9067  IS
9068  SELECT
9069      doc.document_payable_id,
9070      doc.document_status
9071  FROM
9072      IBY_DOCS_PAYABLE_ALL doc
9073  WHERE
9074      doc.payment_service_request_id = p_payreq_id AND
9075      doc.document_status <> DOC_STATUS_PAY_CREATED AND
9076      doc.payment_id IS NOT NULL
9077  ;
9078 
9079  BEGIN
9080 
9081      print_debuginfo(l_module_name, 'ENTER');
9082 
9083      /*
9084       * Pick up all rejected documents for this payment request.
9085       */
9086      OPEN  c_rejected_docs(p_payreq_id);
9087      FETCH c_rejected_docs BULK COLLECT INTO l_rej_docs_list;
9088      CLOSE c_rejected_docs;
9089 
9090      /*
9091       * Separate out the document ids and the document statuses.
9092       * This is because the rejection API expects these as
9093       * separate arrays.
9094       */
9095      IF (l_rej_docs_list.COUNT <> 0) THEN
9096          FOR i IN l_rej_docs_list.FIRST .. l_rej_docs_list.LAST LOOP
9097              x_docIDTab(i) := l_rej_docs_list(i).doc_id;
9098          END LOOP;
9099 
9100          FOR i IN l_rej_docs_list.FIRST .. l_rej_docs_list.LAST LOOP
9101              x_docStatusTab(i) := l_rej_docs_list(i).doc_status;
9102          END LOOP;
9103      END IF;
9104 
9105      print_debuginfo(l_module_name, 'EXIT');
9106 
9107  END getRejectedDocs;
9108 
9109 /*--------------------------------------------------------------------
9110  | NAME:
9111  |     dummyGLAPI
9112  |
9113  | PURPOSE:
9114  |     Dummy method; to be used for testing purposes. Used to simulate
9115  |     GL API for currency conversion.
9116  |
9117  | PARAMETERS:
9118  |     IN
9119  |
9120  |     OUT
9121  |
9122  |
9123  | RETURNS:
9124  |
9125  | NOTES:
9126  |
9127  *---------------------------------------------------------------------*/
9128  PROCEDURE dummyGLAPI(
9129      p_exch_date          IN         DATE,
9130      p_source_amount      IN         NUMBER,
9131      p_source_curr        IN         VARCHAR2,
9132      p_decl_curr          IN         VARCHAR2,
9133      p_decl_fx_rate_type  IN         VARCHAR2,
9134      x_decl_amount        OUT NOCOPY NUMBER)
9135  IS
9136 
9137  BEGIN
9138 
9139      x_decl_amount := p_source_amount * 2;
9140 
9141  END dummyGLAPI;
9142 
9143 /*--------------------------------------------------------------------
9144  | NAME:
9145  |     dummy_paymentsAdjustHook
9146  |
9147  | PURPOSE:
9148  |     Dummy method; to be used for testing purposes. Used to simulate
9149  |     calling app hook.
9150  |
9151  | PARAMETERS:
9152  |     IN
9153  |
9154  |     OUT
9155  |
9156  |
9157  | RETURNS:
9158  |
9159  | NOTES:
9160  |
9161  *---------------------------------------------------------------------*/
9162  PROCEDURE dummy_paymentsAdjustHook(
9163      x_paymentTab      IN OUT NOCOPY hookPaymentTabType,
9164      x_docsInPmtTab    IN OUT NOCOPY hookDocsInPaymentTabType
9165      )
9166  IS
9167 
9168  BEGIN
9169 
9170      x_docsInPmtTab(5).dont_pay_flag        := 'Y';
9171      x_docsInPmtTab(5).dont_pay_reason_code := 'chumma failed';
9172 
9173      /*
9174       * If we fail a doc, make sure to adjust the payment amount
9175       */
9176      FOR i in x_paymentTab.FIRST ..  x_paymentTab.LAST LOOP
9177 
9178          IF (x_docsInPmtTab(5).payment_id =
9179              x_paymentTab(i).payment_id) THEN
9180 
9181              x_paymentTab(i).payment_amount :=
9182                  x_paymentTab(i).payment_amount
9183                      - x_docsInPmtTab(5).document_amount;
9184 
9185          END IF;
9186 
9187      END LOOP;
9188 
9189  END dummy_paymentsAdjustHook;
9190 
9191 /*--------------------------------------------------------------------
9192  | NAME:
9193  |     dummy_ruleFunction
9194  |
9195  | PURPOSE:
9196  |     Dummy method; to be used for testing purposes.
9197  |     You will need to register this function with an event
9198  |     subscription for this function to be called.
9199  |
9200  |     Used to simulate calling app's business event handler.
9201  |
9202  | PARAMETERS:
9203  |     IN
9204  |
9205  |     OUT
9206  |
9207  |
9208  | RETURNS:
9209  |
9210  | NOTES:
9211  |
9212  *---------------------------------------------------------------------*/
9213  FUNCTION dummy_ruleFunction(
9214      p_subscription IN            RAW,
9215      p_event        IN OUT NOCOPY WF_EVENT_T
9216      )
9217      RETURN VARCHAR2
9218  IS
9219 
9220  l_module_name     CONSTANT VARCHAR2(200) := G_PKG_NAME ||
9221                                                  '.dummy_ruleFunction';
9222  l_parameter_list  wf_parameter_list_t := wf_parameter_list_t();
9223  l_parameter_t     wf_parameter_t:= wf_parameter_t(NULL, NULL);
9224  l_parameter_name  l_parameter_t.name%TYPE;
9225  l_parameter_value l_parameter_t.value%TYPE;
9226  l_clob            CLOB;
9227  i                 PLS_INTEGER;
9228 
9229  BEGIN
9230 
9231      print_debuginfo(l_module_name, 'ENTER');
9232 
9233      l_parameter_list := p_event.getParameterList();
9234 
9235      IF (l_parameter_list IS NOT NULL) THEN
9236          i := l_parameter_list.FIRST;
9237          WHILE (i <= l_parameter_list.LAST) LOOP
9238              l_parameter_name  := NULL;
9239              l_parameter_value := NULL;
9240 
9241              l_parameter_name  := l_parameter_list(i).getName();
9242              l_parameter_value := l_parameter_list(i).getValue();
9243 
9244              print_debuginfo(l_module_name, 'Param Name: '
9245                  || l_parameter_name || ', Param Value: '
9246                  || l_parameter_value
9247                  );
9248 
9249              i := l_parameter_list.NEXT(i);
9250          END LOOP;
9251      END IF;
9252 
9253      l_clob := p_event.getEventData();
9254 
9255      IF (l_clob IS NOT NULL) THEN
9256          print_debuginfo(l_module_name, 'Clob is not null');
9257          IBY_BUILD_UTILS_PKG.printXMLClob(l_clob);
9258      END IF;
9259 
9260      print_debuginfo(l_module_name, 'EXIT');
9261 
9262      RETURN 'SUCCESS';
9263 
9264  END dummy_ruleFunction;
9265 
9266 /*--------------------------------------------------------------------
9267  | NAME:
9268  |     getReviewPmtsSysOption
9269  |
9270  | PURPOSE:
9271  |     Gets the review payment flag system option.
9272  |
9273  | PARAMETERS:
9274  |     IN
9275  |
9276  |
9277  |     OUT
9278  |
9279  |
9280  | RETURNS:
9281  |
9282  | NOTES:
9283  |
9284  *---------------------------------------------------------------------*/
9285  FUNCTION getReviewPmtsSysOption RETURN VARCHAR2
9286  IS
9287  l_revw_pmt_sys_option VARCHAR2(200);
9288 
9289  l_sys_options_tab sysOptionsTabType;
9290  l_print_var       NUMBER        := -1;
9291 
9292  l_module_name     CONSTANT VARCHAR2(200) := G_PKG_NAME ||
9293                                                  '.getReviewPmtsSysOption';
9294 
9295  CURSOR c_sys_options
9296  IS
9297  SELECT
9298      NULL,
9299      sysoptions.require_prop_pmts_review_flag
9300  FROM
9301      IBY_INTERNAL_PAYERS_ALL sysoptions
9302  WHERE
9303      sysoptions.org_id IS NULL
9304  ;
9305 
9306  BEGIN
9307 
9308      print_debuginfo(l_module_name, 'ENTER');
9309 
9310      /*
9311       * The 'review proposed payments' flag indicates whether
9312       * all payments (including successful ones) require
9313       *  user review.
9314       */
9315 
9316      /*
9317       * Fetch the system options for the given org
9318       */
9319      OPEN  c_sys_options;
9320      FETCH c_sys_options BULK COLLECT INTO l_sys_options_tab;
9321      CLOSE c_sys_options;
9322 
9323      IF (l_sys_options_tab.COUNT = 0) THEN
9324 
9325          /*
9326           * This means that the review payment flag
9327           * is not set at the enterprise level.
9328           *
9329           * Enterprise level review payment flag (i.e., with org
9330           * id set to null) is expected to be seeded.
9331           *
9332           * Raise an exception and abort processing.
9333           */
9334          print_debuginfo(l_module_name, 'Review pmts flag '
9335              || 'system option is not set at enterprise level. '
9336              || 'It is mandatory to '
9337              || 'setup review pmts flag at enterprise level '
9338              || 'Raising exception.. ',
9339              FND_LOG.LEVEL_UNEXPECTED
9340              );
9341 
9342          APP_EXCEPTION.RAISE_EXCEPTION;
9343 
9344      ELSIF (l_sys_options_tab.COUNT <> 1) THEN
9345 
9346          /*
9347           * This means that there are multiple review payment
9348           * flags set at the enterprise level.
9349           * We don't know which one to use.
9350           *
9351           * Raise an exception and abort processing.
9352           */
9353          print_debuginfo(l_module_name, 'Multiple review payment flag '
9354              || 'system options are set at '
9355              || 'enterprise level. It is mandatory to '
9356              || 'setup only one review payment flag sys option '
9357              || 'at enterprise level. '
9358              || 'Raising exception.. ',
9359              FND_LOG.LEVEL_UNEXPECTED
9360              );
9361 
9362          APP_EXCEPTION.RAISE_EXCEPTION;
9363 
9364      ELSE
9365 
9366          /*
9367           * Return the retrieved enterprise level
9368           * review payment flag system option.
9369           */
9370          l_revw_pmt_sys_option  := l_sys_options_tab(1).revw_flag;
9371 
9372      END IF; -- if l_sys_options_tab.COUNT = 0
9373 
9374      print_debuginfo(l_module_name, 'Returning payment rejection '
9375          || 'level: '
9376          || l_revw_pmt_sys_option
9377          );
9378 
9379      print_debuginfo(l_module_name, 'EXIT');
9380 
9381      RETURN l_revw_pmt_sys_option ;
9382 
9383      EXCEPTION
9384          WHEN OTHERS THEN
9385 
9386              /*
9387               * In case of an exception, return NULL.
9388               */
9389              print_debuginfo(l_module_name, 'Exception thrown '
9390                  || 'when attempting to get review payment flag '
9391                  || 'at enterprise level. Returning NULL.'
9392                  );
9393 
9394             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
9395             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
9396 
9397             l_revw_pmt_sys_option := NULL;
9398             print_debuginfo(l_module_name, 'EXIT');
9399 
9400             RETURN l_revw_pmt_sys_option ;
9401 
9402  END getReviewPmtsSysOption;
9403 
9404 /*--------------------------------------------------------------------
9405  | NAME:
9406  |     getPmtRejLevelSysOption
9407  |
9408  | PURPOSE:
9409  |     Gets the payment rejection level system option.
9410  |
9411  |     The handling of payment validation failures is dependent
9412  |     upon the rejection level setting.
9413  |
9414  | PARAMETERS:
9415  |     IN
9416  |
9417  |
9418  |     OUT
9419  |
9420  |
9421  | RETURNS:
9422  |
9423  | NOTES:
9424  |
9425  *---------------------------------------------------------------------*/
9426  FUNCTION getPmtRejLevelSysOption RETURN VARCHAR2
9427  IS
9428  l_rej_level_sys_option VARCHAR2(200);
9429 
9430  l_sys_options_tab sysOptionsTabType;
9431  l_print_var       NUMBER        := -1;
9432 
9433  l_module_name     CONSTANT VARCHAR2(200) := G_PKG_NAME ||
9434                                                  '.getPmtRejLevelSysOption';
9435 
9436  CURSOR c_sys_options
9437  IS
9438  SELECT
9439      sysoptions.payment_rejection_level_code,
9440      NULL
9441  FROM
9442      IBY_INTERNAL_PAYERS_ALL sysoptions
9443  WHERE
9444      sysoptions.org_id IS NULL
9445  ;
9446 
9447  BEGIN
9448 
9449      print_debuginfo(l_module_name, 'ENTER');
9450 
9451      /*
9452       * The rejection level system option specifies
9453       * what is to be rejected if a payment fails
9454       * validation.
9455       */
9456 
9457      /*
9458       * Fetch the system options for the given org
9459       */
9460      OPEN  c_sys_options;
9461      FETCH c_sys_options BULK COLLECT INTO l_sys_options_tab;
9462      CLOSE c_sys_options;
9463 
9464      IF (l_sys_options_tab.COUNT = 0) THEN
9465 
9466          /*
9467           * This means that the payment rejection level
9468           * is not set at the enterprise level.
9469           *
9470           * Enterprise level rejection levels (i.e., with org
9471           * id set to null) are expected to be seeded.
9472           *
9473           * Raise an exception and abort processing.
9474           */
9475          print_debuginfo(l_module_name, 'Payment rejection level '
9476              || 'system option is not set at enterprise level. '
9477              || 'It is mandatory to '
9478              || 'setup rejection levels at enterprise level '
9479              || 'Raising exception.. ',
9480              FND_LOG.LEVEL_UNEXPECTED
9481              );
9482 
9483          APP_EXCEPTION.RAISE_EXCEPTION;
9484 
9485      ELSIF (l_sys_options_tab.COUNT <> 1) THEN
9486 
9487          /*
9488           * This means that there are multiple payment
9489           * rejection levels set at the enterprise level.
9490           * We don't know which one to use.
9491           *
9492           * Raise an exception and abort processing.
9493           */
9494          print_debuginfo(l_module_name, 'Multiple payment rejection '
9495              || 'level system options are set at '
9496              || 'enterprise level. It is mandatory to '
9497              || 'setup only one payment rejection level '
9498              || 'at enterprise level. '
9499              || 'Raising exception.. ',
9500              FND_LOG.LEVEL_UNEXPECTED
9501              );
9502 
9503          APP_EXCEPTION.RAISE_EXCEPTION;
9504 
9505      ELSE
9506 
9507          /*
9508           * Return the retrieved enterprise level
9509           * payment rejection level system option.
9510           */
9511          l_rej_level_sys_option := l_sys_options_tab(1).rej_level;
9512 
9513      END IF; -- if l_sys_options_tab.COUNT = 0
9514 
9515      print_debuginfo(l_module_name, 'Returning payment rejection '
9516          || 'level: '
9517          || l_rej_level_sys_option
9518          );
9519 
9520      print_debuginfo(l_module_name, 'EXIT');
9521 
9522      RETURN l_rej_level_sys_option;
9523 
9524      EXCEPTION
9525          WHEN OTHERS THEN
9526 
9527              /*
9528               * In case of an exception, return NULL.
9529               */
9530              print_debuginfo(l_module_name, 'Exception thrown '
9531                  || 'when attempting to get payment rejection '
9532                  || 'level. Returning NULL.'
9533                  );
9534 
9535             print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE);
9536             print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM);
9537 
9538             l_rej_level_sys_option := NULL;
9539             print_debuginfo(l_module_name, 'EXIT');
9540 
9541             RETURN l_rej_level_sys_option;
9542 
9543  END getPmtRejLevelSysOption;
9544 
9545 /*--------------------------------------------------------------------
9546  | NAME:
9547  |     performCreditMemoHandling
9548  |
9549  | PURPOSE:
9550  |
9551  |
9552  |
9553  | PARAMETERS:
9554  |     IN
9555  |
9556  |     OUT
9557  |
9558  |
9559  | RETURNS:
9560  |
9561  | NOTES:
9562  |
9563  *---------------------------------------------------------------------*/
9564  PROCEDURE performCreditMemoHandling(
9565      x_paymentTab    IN OUT NOCOPY paymentTabType,
9566      x_docsInPmtTab  IN OUT NOCOPY docsInPaymentTabType
9567      )
9568  IS
9569  l_module_name         CONSTANT VARCHAR2(200) := G_PKG_NAME ||
9570                                                  '.performCreditMemoHandling';
9571  l_negative_pmts_tab   pmtIdsTab;
9572  BEGIN
9573 
9574      print_debuginfo(l_module_name, 'ENTER');
9575 
9576      /*
9577       * A payment qualifies for credit memo adjustments
9578       * if it satisfies the following criteria:
9579       *
9580       * a. Payment amount is negative
9581       * b. Payment contains at least one positive
9582       *    document payable
9583       *
9584       * a. and b. together imply that the payment is not
9585       * payalone, so this condition need not be explicitly
9586       * checked.
9587       *
9588       * Form the payment request, get a list of payments
9589       * that qualify for credit memo adjustments.
9590       */
9591      getListOfQualifyingNegPmts(l_negative_pmts_tab,
9592          x_paymentTab, x_docsInPmtTab);
9593 
9594      IF (l_negative_pmts_tab.COUNT = 0) THEN
9595 
9596          print_debuginfo(l_module_name, 'No payments qualified for '
9597              || 'credit memo adjustment in this request. '
9598              || 'Skipping credit memo handling .. '
9599              );
9600 
9601          print_debuginfo(l_module_name, 'EXIT');
9602 
9603          RETURN;
9604 
9605      END IF;
9606 
9607      /*
9608       * Attempt to bring negative payments to zero by adjusting
9609       * credit memo amounts.
9610       */
9611      adjustCreditMemosWithinPmt(l_negative_pmts_tab, x_paymentTab,
9612          x_docsInPmtTab);
9613 
9614      print_debuginfo(l_module_name, 'EXIT');
9615 
9616  END performCreditMemoHandling;
9617 
9618 /*--------------------------------------------------------------------
9619  | NAME:
9620  |     adjustCreditMemosWithinPmt
9621  |
9622  | PURPOSE:
9623  |
9624  |
9625  |
9626  | PARAMETERS:
9627  |     IN
9628  |
9629  |     OUT
9630  |
9631  |
9632  | RETURNS:
9633  |
9634  | NOTES:
9635  |
9636  *---------------------------------------------------------------------*/
9637  PROCEDURE adjustCreditMemosWithinPmt(
9638      p_qualifyingPmtsTab  IN pmtIdsTab,
9639      x_paymentTab         IN OUT NOCOPY paymentTabType,
9640      x_docsInPmtTab       IN OUT NOCOPY docsInPaymentTabType
9641      )
9642  IS
9643 
9644  l_module_name     CONSTANT VARCHAR2(200) := G_PKG_NAME ||
9645                                                  '.adjustCreditMemosWithinPmt';
9646 
9647  l_pos_docs_array  docsInPaymentTabType;
9648  l_neg_docs_array  docsInPaymentTabType;
9649  l_running_total   NUMBER := 0;
9650 
9651  BEGIN
9652 
9653      print_debuginfo(l_module_name, 'ENTER');
9654 
9655      /*
9656       * For each payment that qualifies for credit memo
9657       * adjustment, separate out the positive and negative
9658       * documents payable of that payment into two distinct
9659       * arrays.
9660       */
9661      FOR i IN p_qualifyingPmtsTab.FIRST .. p_qualifyingPmtsTab.LAST LOOP
9662 
9663          FOR j IN x_docsInPmtTab.FIRST .. x_docsInPmtTab.LAST LOOP
9664 
9665              IF (x_docsInPmtTab(j).payment_id =
9666                  p_qualifyingPmtsTab(i)) THEN
9667 
9668                  IF (x_docsInPmtTab(j).document_amount < 0) THEN
9669 
9670                      /* array of negative documents */
9671                      l_neg_docs_array(l_neg_docs_array.COUNT + 1) :=
9672                          x_docsInPmtTab(j);
9673 
9674                  ELSE
9675 
9676                      /* array of positive documents */
9677                      l_pos_docs_array(l_pos_docs_array.COUNT + 1) :=
9678                          x_docsInPmtTab(j);
9679 
9680                  END IF;
9681 
9682              END IF;
9683 
9684          END LOOP;
9685 
9686      END LOOP;
9687 
9688      /* uncomment for debugging purposes */
9689      /*-------------------------------------------
9690      FOR i IN l_neg_docs_array.FIRST .. l_neg_docs_array.LAST LOOP
9691          print_debuginfo(l_module_name, 'Payment id: '
9692              || l_neg_docs_array(i).payment_id
9693              || ', neg doc id: '
9694              || l_neg_docs_array(i).document_id
9695              || ', neg doc amount: '
9696              || l_neg_docs_array(i).document_amount
9697              );
9698      END LOOP;
9699 
9700      FOR i IN l_pos_docs_array.FIRST .. l_pos_docs_array.LAST LOOP
9701          print_debuginfo(l_module_name, 'Payment id: '
9702              || l_pos_docs_array(i).payment_id
9703              || ', pos doc id: '
9704              || l_pos_docs_array(i).document_id
9705              || ', pos doc amount: '
9706              || l_pos_docs_array(i).document_amount
9707              );
9708      END LOOP;
9709      ---------------------------------------------*/
9710 
9711      /*
9712       * For each qualifying payment, perform credit memo
9713       * adjustment.
9714       */
9715      FOR i IN p_qualifyingPmtsTab.FIRST .. p_qualifyingPmtsTab.LAST LOOP
9716 
9717          /* initialize running total for this payment */
9718          l_running_total := 0;
9719 
9720          /*
9721           * Sum up all the positive documents of this payment
9722           * and store the total in l_running_total.
9723           */
9724          FOR j IN l_pos_docs_array.FIRST .. l_pos_docs_array.LAST LOOP
9725 
9726              IF (l_pos_docs_array(j).payment_id =
9727                  p_qualifyingPmtsTab(i)) THEN
9728 
9729                  l_running_total := l_running_total +
9730                                         l_pos_docs_array(j).document_amount;
9731 
9732              END IF;
9733 
9734          END LOOP;
9735 
9736          print_debuginfo(l_module_name, 'Running total of positive docs '
9737              || 'for payment '
9738              || p_qualifyingPmtsTab(i)
9739              || ' is: '
9740              || l_running_total
9741              );
9742 
9743          /*
9744           * We now have the running total of all positive documents
9745           * within this payment stored in l_running_total.
9746           */
9747 
9748          /*
9749           * Start adjusting negative documents (credit memos)
9750           * one-by-one for this payment.
9751           */
9752          FOR k in l_neg_docs_array.FIRST .. l_neg_docs_array.LAST LOOP
9753 
9754              IF (l_neg_docs_array(k).payment_id =
9755                  p_qualifyingPmtsTab(i)) THEN
9756 
9757                  print_debuginfo(l_module_name, 'Adding neg doc id: '
9758                      || l_neg_docs_array(k).document_id
9759                      || ' with amount '
9760                      || l_neg_docs_array(k).document_amount
9761                      || ' to running total.'
9762                      );
9763 
9764                  l_running_total := l_running_total +
9765                                         l_neg_docs_array(k).document_amount;
9766 
9767                  print_debuginfo(l_module_name, 'Now running total : '
9768                      || l_running_total
9769                      );
9770 
9771                  IF (l_running_total < 0) THEN
9772 
9773                      /* Running total  negative implies that
9774                       * the last added negative amount document,
9775                       * has exceeded the sum of all available
9776                       * positive documents.
9777                       *
9778                       * Adjust the last added negative
9779                       * document payment amount such that
9780                       * adding this negative amount will bring
9781                       * the running total exactly to zero.
9782                       */
9783 
9784                      l_neg_docs_array(k).document_amount :=
9785                          l_neg_docs_array(k).document_amount
9786                              - l_running_total;
9787 
9788                      print_debuginfo(l_module_name, 'Adusting neg doc id '
9789                          || l_neg_docs_array(k).document_id
9790                          || ' to amount '
9791                          || l_neg_docs_array(k).document_amount
9792                          );
9793 
9794                      /*
9795                       * Zero out all remaining negative documents
9796                       * for this payment.
9797                       */
9798                      FOR m in k+1 .. l_neg_docs_array.LAST
9799                          LOOP
9800 
9801                          IF (l_neg_docs_array(m).payment_id =
9802                              p_qualifyingPmtsTab(i)) THEN
9803 
9804                              l_neg_docs_array(m).document_amount := 0;
9805 
9806                              print_debuginfo(l_module_name, 'Zeroing out '
9807                                  || ' document id '
9808                                  || l_neg_docs_array(m).document_id
9809                                  || ' of payment '
9810                                  || l_neg_docs_array(m).payment_id
9811                                  );
9812 
9813                          END IF;
9814 
9815                      END LOOP;
9816 
9817                      /*
9818                       * If running total has gone negative,
9819                       * and we have finished zeroing out the
9820                       * rest of the negative documents for
9821                       * this payment, it means that credit
9822                       * memo adjustment for this payment is
9823                       * complete. Exit the loop and continue
9824                       * with the next payment.
9825                       */
9826                      EXIT;
9827 
9828                  END IF; -- if running total has gone negative
9829 
9830              END IF; -- if payment id of this document matches
9831 
9832          END LOOP; -- for each negative doc for this payment
9833 
9834          /*
9835           * Reflect the document and payment amount adjustments
9836           * that were made during credit memo handling for
9837           * this payment back to the original documents and
9838           * payments array -  x_paymentTab and x_docsInPmtTab
9839           */
9840          refreshDocAndPmtAmounts(p_qualifyingPmtsTab(i),
9841              l_neg_docs_array, x_paymentTab, x_docsInPmtTab);
9842 
9843      END LOOP; -- for each negative payment
9844 
9845      print_debuginfo(l_module_name, 'EXIT');
9846 
9847  END adjustCreditMemosWithinPmt;
9848 
9849 /*--------------------------------------------------------------------
9850  | NAME:
9851  |     performMaturityDateCalculation
9852  |
9853  | PURPOSE:
9854  |
9855  |
9856  |
9857  | PARAMETERS:
9858  |     IN
9859  |
9860  |     OUT
9861  |
9862  |
9863  | RETURNS:
9864  |
9865  | NOTES:
9866  |
9867  *---------------------------------------------------------------------*/
9868  PROCEDURE performMaturityDateCalculation(
9869      x_paymentTab    IN OUT NOCOPY paymentTabType,
9870      p_docsInPmtTab  IN            docsInPaymentTabType
9871      )
9872  IS
9873  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
9874                                              '.performMaturityDateCalculation';
9875  l_maturity_days_tab      pmtMethodMaturityDaysTab;
9876  l_maturity_days_offset   IBY_PAYMENT_METHODS_VL.maturity_date_offset_days%TYPE;
9877  l_maturity_date_set_flag BOOLEAN := FALSE;
9878  l_temp_discount_date     DATE := NULL;
9879  l_temp_due_date          DATE := NULL;
9880 
9881  BEGIN
9882 
9883      print_debuginfo(l_module_name, 'ENTER');
9884 
9885      /*
9886       * Load up all the payment methods that support future
9887       * dated payments along with their maturity offset days.
9888       * We will be adding the offset days (if available) to the
9889       * payment date to arrive at the maturity date.
9890       */
9891      loadPmtMethodMaturityDays(l_maturity_days_tab);
9892 
9893      IF (l_maturity_days_tab.COUNT = 0) THEN
9894 
9895          print_debuginfo(l_module_name, 'None of the seeded payment '
9896              || 'methods support bills payable (future dated payments). '
9897              || 'Skipping maturity date calculation .. '
9898              );
9899 
9900          print_debuginfo(l_module_name, 'EXIT');
9901 
9902          RETURN;
9903 
9904      END IF; -- if maturity days tab count == 0
9905 
9906      /*
9907       * Loop through all the payments, setting maturity dates
9908       * for each future dated payment.
9909       */
9910      FOR i IN x_paymentTab.FIRST .. x_paymentTab.LAST LOOP
9911 
9912          /* initialize flag before each iteration */
9913          l_maturity_date_set_flag := FALSE;
9914 
9915          /*
9916           * For each payment that is a future dated payment
9917           * determine the maturity date.
9918           */
9919          IF (x_paymentTab(i).bill_payable_flag = 'Y') THEN
9920 
9921              print_debuginfo(l_module_name, 'Payment '
9922                  || x_paymentTab(i).payment_id
9923                  || ' is a future dated payment'
9924                  );
9925 
9926              /*
9927               * ATTEMPT 1:
9928               *
9929               * Try to get the maturity date offset days from the
9930               * payment method and add this to the payment date on
9931               * the payment to arrive at the maturity date.
9932               */
9933              l_maturity_days_offset := getMaturityDaysForPmtMethod(
9934                                            x_paymentTab(i).payment_method_code,
9935                                            l_maturity_days_tab
9936                                            );
9937 
9938              IF (l_maturity_days_offset <> -1) THEN
9939 
9940                  print_debuginfo(l_module_name, 'Adding '
9941                      || l_maturity_days_offset
9942                      || ' offset days to payment date to arrive at '
9943                      || 'maturity date.'
9944                      );
9945 
9946                  x_paymentTab(i).maturity_date :=
9947                      x_paymentTab(i).payment_date + l_maturity_days_offset;
9948 
9949                  l_maturity_date_set_flag := TRUE;
9950 
9951              ELSE
9952 
9953                  print_debuginfo(l_module_name, 'Maturity date offset not '
9954                      || 'set on payment method '
9955                      || x_paymentTab(i).payment_method_code
9956                      || ' (so maturity date needs to be calculated).'
9957                      );
9958 
9959                  l_maturity_date_set_flag := FALSE;
9960 
9961              END IF;
9962 
9963              /*
9964               * ATTEMPT II:
9965               *
9966               * Set the maturity date based on the earliest
9967               * available discount date from the documents
9968               * on the payment.
9969               *
9970               * Note: Maturity date is also calculated from
9971               * the payment due date in ATTEMPT III below.
9972               *
9973               * The value set of the maturity date will be
9974               * the earliest of (discount date, payment due date).
9975               */
9976 
9977              /* initialize before each iteration */
9978              l_temp_discount_date := NULL;
9979 
9980              IF (l_maturity_date_set_flag = FALSE) THEN
9981 
9982                  /*
9983                   * In this loop, we attempt to find the first available
9984                   * discount date after the payment date for the
9985                   * current payment.
9986                   */
9987                  FOR j IN p_docsInPmtTab.FIRST .. p_docsInPmtTab.LAST LOOP
9988 
9989                      IF (x_paymentTab(i).payment_id =
9990                          p_docsInPmtTab(j).payment_id) THEN
9991 
9992                          IF (p_docsInPmtTab(j).discount_date IS NOT NULL AND
9993                              p_docsInPmtTab(j).discount_date >=
9994                                  x_paymentTab(i).payment_date) THEN
9995 
9996                              IF (l_temp_discount_date IS NULL) THEN
9997 
9998                                  /*
9999                                   * if 'l_temp_discount_date' is not yet
10000                                   * initialized, set it to the first available
10001                                   * discount date value.
10002                                   */
10003                                  l_temp_discount_date :=
10004                                      p_docsInPmtTab(j).discount_date;
10005 
10006                              ELSE
10007 
10008                                  /*
10009                                   * We already have a value for
10010                                   * 'l_temp_discount_date'; Update the value of
10011                                   * 'l_temp_discount_date' only if the new
10012                                   * discount date occurs earlier.
10013                                   */
10014 
10015                                  IF (p_docsInPmtTab(j).discount_date <
10016                                      l_temp_discount_date) THEN
10017 
10018                                      l_temp_discount_date :=
10019                                          p_docsInPmtTab(j).discount_date;
10020 
10021                                  END IF;
10022 
10023                              END IF; -- if temp discount date is not set
10024 
10025                          END IF; -- if discount date is available
10026 
10027                      END IF; -- if document belongs to the current payment
10028 
10029                  END LOOP; -- for each document
10030 
10031                  /*
10032                   * The earliest available discount date is one
10033                   * candidate for the maturity date.
10034                   */
10035                  print_debuginfo(l_module_name, 'Earliest available '
10036                      || 'discount date for payment '
10037                      || x_paymentTab(i).payment_id
10038                      || ' is '
10039                      || l_temp_discount_date
10040                      );
10041 
10042              END IF; -- if maturity date is not set for this payment
10043 
10044              /*
10045               * ATTEMPT III:
10046               *
10047               * Set the maturity date based on the earliest
10048               * available payment due date from the documents
10049               * on the payment.
10050               */
10051 
10052              /* initialize before each iteration */
10053              l_temp_due_date := NULL;
10054 
10055              IF (l_maturity_date_set_flag = FALSE) THEN
10056 
10057                  /*
10058                   * In this loop, we attempt to find the first available
10059                   * due date after the payment date for the current
10060                   * payment.
10061                   */
10062                  FOR j IN p_docsInPmtTab.FIRST .. p_docsInPmtTab.LAST LOOP
10063 
10064                      IF (x_paymentTab(i).payment_id =
10065                          p_docsInPmtTab(j).payment_id) THEN
10066 
10067                          IF (p_docsInPmtTab(j).pmt_due_date IS NOT NULL AND
10068                              p_docsInPmtTab(j).pmt_due_date >=
10069                                  x_paymentTab(i).payment_date) THEN
10070 
10071                              IF (l_temp_due_date IS NULL) THEN
10072 
10073                                  /*
10074                                   * if 'l_temp_due_date' is not yet initialized,
10075                                   * set it to the first available due
10076                                   * date value.
10077                                   */
10078                                  l_temp_due_date := p_docsInPmtTab(j).
10079                                                         pmt_due_date;
10080 
10081                              ELSE
10082 
10083                                  /*
10084                                   * We already have a value for
10085                                   * 'l_temp_due_date';
10086                                   * Update the value of 'l_temp_due_date'
10087                                   * only if the new due date occurs
10088                                   * earlier.
10089                                   */
10090 
10091                                  IF (p_docsInPmtTab(j).pmt_due_date <
10092                                      l_temp_due_date) THEN
10093 
10094                                      l_temp_due_date := p_docsInPmtTab(j).
10095                                                             pmt_due_date;
10096 
10097                                  END IF;
10098 
10099                              END IF; -- if temp due date is not set
10100 
10101                          END IF; -- if due date is available
10102 
10103                      END IF; -- if document belongs to the current payment
10104 
10105                  END LOOP; -- for each document
10106 
10107                  /*
10108                   * The earliest available due date is the
10109                   * other candidate for the maturity date.
10110                   */
10111                  print_debuginfo(l_module_name, 'Earliest available '
10112                      || 'due date for payment '
10113                      || x_paymentTab(i).payment_id
10114                      || ' is '
10115                      || l_temp_due_date
10116                      );
10117 
10118                  /*
10119                   * Fix for bug 5334177:
10120                   *
10121                   * Maturity Date = EARLIEST (ATTEMPT II, ATTEMPT III)
10122                   *
10123                   * Set the calculated maturity date on the
10124                   * payment as follows:
10125                   *
10126                   * 1. Maturity date is earliest of
10127                   *    (discount date, due date).
10128                   *
10129                   * 2. If either value is not available
10130                   *    use the other.
10131                   *
10132                   * 3. If both values are not available
10133                   *    use the payment date on the
10134                   *    payment as the maturity date.
10135                   */
10136 
10137                  IF (l_temp_discount_date IS NOT NULL AND
10138                      l_temp_due_date      IS NOT NULL) THEN
10139 
10140                      IF (l_temp_discount_date <= l_temp_due_date) THEN
10141 
10142                          print_debuginfo(l_module_name, 'Setting '
10143                              || 'maturity date to discount date '
10144                              || l_temp_discount_date
10145                              || ' for payment '
10146                              || x_paymentTab(i).payment_id
10147                              || ' because it is earlier '
10148                              || 'than due date '
10149                              || l_temp_due_date
10150                              );
10151 
10152                          x_paymentTab(i).maturity_date :=
10153                              l_temp_discount_date;
10154 
10155                          l_maturity_date_set_flag := TRUE;
10156 
10157                      ELSE
10158 
10159                          print_debuginfo(l_module_name, 'Setting '
10160                              || 'maturity date to due date '
10161                              || l_temp_due_date
10162                              || ' for payment '
10163                              || x_paymentTab(i).payment_id
10164                              || ' because it is earlier '
10165                              || 'than discount date '
10166                              || l_temp_discount_date
10167                              );
10168 
10169                          x_paymentTab(i).maturity_date := l_temp_due_date;
10170 
10171                          l_maturity_date_set_flag := TRUE;
10172 
10173                      END IF;
10174 
10175                  ELSIF (l_temp_discount_date IS NULL AND
10176                         l_temp_due_date      IS NULL) THEN
10177 
10178                          print_debuginfo(l_module_name, 'Defaulting '
10179                              || 'maturity date to payment date '
10180                              || x_paymentTab(i).payment_date
10181                              || ' for payment '
10182                              || x_paymentTab(i).payment_id
10183                              || ' because both discount and due '
10184                              || 'dates are not available.'
10185                              );
10186 
10187                          x_paymentTab(i).maturity_date :=
10188                              x_paymentTab(i).payment_date;
10189 
10190                          l_maturity_date_set_flag := TRUE;
10191 
10192                  ELSE
10193 
10194                      /*
10195                       * If we come here, it means that only
10196                       * one of the discount date or the due
10197                       * date is available.
10198                       */
10199                      IF (l_temp_discount_date IS NOT NULL) THEN
10200 
10201                          print_debuginfo(l_module_name, 'Setting '
10202                              || 'maturity date to discount date '
10203                              || l_temp_discount_date
10204                              || ' for payment '
10205                              || x_paymentTab(i).payment_id
10206                              || ' because no due date available.'
10207                              );
10208 
10209                          x_paymentTab(i).maturity_date :=
10210                              l_temp_discount_date;
10211 
10212                          l_maturity_date_set_flag := TRUE;
10213 
10214                      ELSE
10215 
10216                          print_debuginfo(l_module_name, 'Setting '
10217                              || 'maturity date to due date '
10218                              || l_temp_due_date
10219                              || ' for payment '
10220                              || x_paymentTab(i).payment_id
10221                              || ' because no discount date available.'
10222                              );
10223 
10224                          x_paymentTab(i).maturity_date := l_temp_due_date;
10225 
10226                          l_maturity_date_set_flag := TRUE;
10227 
10228                      END IF;
10229 
10230                  END IF; -- if discount date and due date are not null
10231 
10232              END IF; -- if maturity date is not set for this payment
10233 
10234              /*
10235               * If all our efforts to calculate maturity date for
10236               * this payment have failed, log it.
10237               */
10238              IF (l_maturity_date_set_flag = FALSE) THEN
10239 
10240                  print_debuginfo(l_module_name, 'Maturity date '
10241                      || 'could not be derived for payment '
10242                      || x_paymentTab(i).payment_id
10243                      );
10244 
10245                  /*
10246                   * This should never happen; raise an
10247                   * exception.
10248                   */
10249                  print_debuginfo(l_module_name, 'Maturity date is a must '
10250                      || 'for a bill payable. Aborting ..',
10251                      FND_LOG.LEVEL_UNEXPECTED
10252                      );
10253 
10254                  APP_EXCEPTION.RAISE_EXCEPTION;
10255 
10256              END IF;
10257 
10258          ELSE
10259 
10260              print_debuginfo(l_module_name, 'Payment '
10261                  || x_paymentTab(i).payment_id
10262                  || ' is not a future dated payment'
10263                  );
10264 
10265          END IF; -- if bills payable flag on pmt is 'Y'
10266 
10267      END LOOP; -- for each payment
10268 
10269      print_debuginfo(l_module_name, 'EXIT');
10270 
10271  END performMaturityDateCalculation;
10272 
10273 /*--------------------------------------------------------------------
10274  | NAME:
10275  |     loadPmtMethodMaturityDays
10276  |
10277  | PURPOSE:
10278  |
10279  |
10280  |
10281  | PARAMETERS:
10282  |     IN
10283  |
10284  |     OUT
10285  |
10286  |
10287  | RETURNS:
10288  |
10289  | NOTES:
10290  |
10291  *---------------------------------------------------------------------*/
10292  PROCEDURE loadPmtMethodMaturityDays(
10293      x_maturityDaysTab    IN OUT NOCOPY pmtMethodMaturityDaysTab
10294      )
10295  IS
10296  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
10297                                              '.loadPmtMethodMaturityDays';
10298 
10299  CURSOR c_maturity_days
10300  IS
10301  SELECT
10302      payment_method_code,
10303      maturity_date_offset_days
10304  FROM
10305      IBY_PAYMENT_METHODS_VL
10306  WHERE
10307      support_bills_payable_flag = 'Y'
10308  ;
10309 
10310  BEGIN
10311 
10312      print_debuginfo(l_module_name, 'ENTER');
10313 
10314      OPEN  c_maturity_days;
10315      FETCH c_maturity_days BULK COLLECT INTO x_maturityDaysTab;
10316      CLOSE c_maturity_days;
10317 
10318      print_debuginfo(l_module_name, 'Fetched '
10319          || x_maturityDaysTab.COUNT
10320          || ' records.'
10321          );
10322 
10323      print_debuginfo(l_module_name, 'EXIT');
10324 
10325  END loadPmtMethodMaturityDays;
10326 
10327 /*--------------------------------------------------------------------
10328  | NAME:
10329  |     getMaturityDaysForPmtMethod
10330  |
10331  | PURPOSE:
10332  |
10333  |
10334  |
10335  | PARAMETERS:
10336  |     IN
10337  |
10338  |     OUT
10339  |
10340  |
10341  | RETURNS:
10342  |
10343  | NOTES:
10344  |
10345  *---------------------------------------------------------------------*/
10346  FUNCTION getMaturityDaysForPmtMethod(
10347      p_pmt_method_code    IN  IBY_PAYMENTS_ALL.payment_method_code%TYPE,
10348      p_maturityDaysTab    IN  pmtMethodMaturityDaysTab
10349      ) RETURN NUMBER
10350  IS
10351  l_maturity_days_offset IBY_PAYMENT_METHODS_VL.maturity_date_offset_days%TYPE
10352      := -1;
10353  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
10354                                              '.getMaturityDaysForPmtMethod';
10355  BEGIN
10356 
10357      IF (p_maturityDaysTab.COUNT = 0) THEN
10358 
10359          l_maturity_days_offset := -1;
10360          RETURN l_maturity_days_offset;
10361 
10362      END IF;
10363 
10364      FOR i IN p_maturityDaysTab.FIRST .. p_maturityDaysTab.LAST LOOP
10365 
10366          IF (p_pmt_method_code = p_maturityDaysTab(i).pmt_method_cd) THEN
10367 
10368              l_maturity_days_offset := p_maturityDaysTab(i).
10369                                            maturity_offset_days;
10370 
10371              RETURN l_maturity_days_offset;
10372 
10373          END IF;
10374 
10375      END LOOP;
10376 
10377      /*
10378       * Return -1 if we could not find the given
10379       * payment method.
10380       */
10381 
10382      l_maturity_days_offset := -1;
10383 
10384      print_debuginfo(l_module_name, 'Given payment method code '
10385          || p_pmt_method_code
10386          || ' was not found in list of payment methods which '
10387          || 'support future dated payments. Returning -1.'
10388          );
10389 
10390      RETURN l_maturity_days_offset;
10391 
10392  END getMaturityDaysForPmtMethod;
10393 
10394 
10395 /*--------------------------------------------------------------------
10396  | NAME:
10397  |     refreshDocAndPmtAmounts
10398  |
10399  | PURPOSE:
10400  |     This method is called once per payment after credit memo
10401  |     adjustments for that payment are complete. This method
10402  |     will refresh the document and payment amounts PLSQL tables
10403  |     to reflect the credit memo adjustments that were performed.
10404  |
10405  | PARAMETERS:
10406  |     IN
10407  |
10408  |     OUT
10409  |
10410  |
10411  | RETURNS:
10412  |
10413  | NOTES:
10414  |
10415  *---------------------------------------------------------------------*/
10416  PROCEDURE refreshDocAndPmtAmounts(
10417      p_adjustedPmtId      IN            IBY_PAYMENTS_ALL.payment_id%TYPE,
10418      p_adjustedNegDocsTab IN            docsInPaymentTabType,
10419      x_paymentTab         IN OUT NOCOPY paymentTabType,
10420      x_docsInPmtTab       IN OUT NOCOPY docsInPaymentTabType
10421      )
10422  IS
10423  BEGIN
10424 
10425      /*
10426       * We have just finished making credit memo adjustments
10427       * for a particular payment.
10428       *
10429       * Adjusted negative values would be stored in
10430       * l_neg_docs_array (Positive values are not touched
10431       * by the credit memo adjustment logic). Copy back
10432       * these adjusted values into x_docsInPmtTab and
10433       * x_paymentTab so that these changes are reflected
10434       * back to the caller.
10435       *
10436       * The adjusted values in x_docsInPmtTab and x_paymentTab
10437       * will be used in updating the database.
10438       */
10439      FOR n in p_adjustedNegDocsTab.FIRST .. p_adjustedNegDocsTab.LAST LOOP
10440 
10441          IF (p_adjustedNegDocsTab(n).payment_id = p_adjustedPmtId) THEN
10442 
10443              FOR p in x_docsInPmtTab.FIRST .. x_docsInPmtTab.LAST LOOP
10444 
10445                  IF (x_docsInPmtTab(p).payment_id = p_adjustedPmtId) THEN
10446 
10447                      IF (p_adjustedNegDocsTab(n).document_id =
10448                          x_docsInPmtTab(p).document_id) THEN
10449 
10450                          /*
10451                           * Store back the adjusted amount value
10452                           * for this document.
10453                           */
10454                          x_docsInPmtTab(p).document_amount :=
10455                              p_adjustedNegDocsTab(n).document_amount;
10456 
10457                          /*
10458                           * Since there will only be one document with
10459                           * a particular document id in x_docsInPmtTab,
10460                           * we can exit the loop as soon as we find it.
10461                           */
10462                          EXIT;
10463 
10464                      END IF;
10465 
10466                  END IF;
10467 
10468              END LOOP; -- for each doc in x_docsInPmtTab
10469 
10470          END IF;
10471 
10472      END LOOP; -- for each doc in p_adjustedNegDocsTab
10473 
10474      /*
10475       * After adjusting the document amount, we must make sure
10476       * adjust the payment amount as well:
10477       *
10478       * payment amount = SUM (document amounts)
10479       */
10480      FOR q in x_paymentTab.FIRST .. x_paymentTab.LAST LOOP
10481 
10482          IF (x_paymentTab(q).payment_id = p_adjustedPmtId) THEN
10483 
10484              /* reset payment amount to zero */
10485              x_paymentTab(q).payment_amount := 0;
10486 
10487              /*
10488               * Set payment amount to sum of all document amounts
10489               * that constitute this payment.
10490               */
10491              FOR r in x_docsInPmtTab.FIRST .. x_docsInPmtTab.LAST LOOP
10492 
10493                  IF (x_docsInPmtTab(r).payment_id = p_adjustedPmtId) THEN
10494 
10495                      x_paymentTab(q).payment_amount :=
10496                          x_paymentTab(q).payment_amount
10497                              + x_docsInPmtTab(r).document_amount;
10498 
10499                  END IF;
10500 
10501              END LOOP;
10502 
10503              /*
10504               * Since there will only be one payment in x_paymentTab
10505               * we can exit the loop as soon as we find it.
10506               */
10507              EXIT;
10508 
10509          END IF;
10510 
10511      END LOOP;
10512 
10513  END refreshDocAndPmtAmounts;
10514 
10515 /*--------------------------------------------------------------------
10516  | NAME:
10517  |     getListOfQualifyingNegPmts
10518  |
10519  | PURPOSE:
10520  |
10521  |
10522  |
10523  | PARAMETERS:
10524  |     IN
10525  |
10526  |     OUT
10527  |
10528  |
10529  | RETURNS:
10530  |
10531  | NOTES:
10532  |
10533  *---------------------------------------------------------------------*/
10534  PROCEDURE getListOfQualifyingNegPmts(
10535      x_negPmtsTab    IN OUT NOCOPY pmtIdsTab,
10536      p_paymentTab    IN paymentTabType,
10537      p_docsInPmtTab  IN docsInPaymentTabType
10538      )
10539  IS
10540  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
10541                                              '.getListOfQualifyingNegPmts';
10542  l_already_added_flag BOOLEAN := FALSE;
10543  BEGIN
10544 
10545      /*
10546       * Find all payments that satisfy the following criteria:
10547       * 1. Payment amount must be negative
10548       * 2. Payment must contain at least one positive document
10549       *    payable.
10550       * 3. Payment must not be pay alone.
10551       *
10552       * 1. and 2. imply that the payment is not pay alone, so
10553       * condition 3. need not be explicitly checked.
10554       *
10555       * Payments that satisfy these criteria qualify for
10556       * credit memo adjustment.
10557       */
10558      FOR j IN p_paymentTab.FIRST .. p_paymentTab.LAST LOOP
10559 
10560          /* Payment amount is negative */
10561          IF (p_paymentTab(j).payment_amount < 0) THEN
10562 
10563              FOR k IN p_docsInPmtTab.FIRST .. p_docsInPmtTab.LAST LOOP
10564 
10565                  IF (p_docsInPmtTab(k).payment_id =
10566                      p_paymentTab(j).payment_id) THEN
10567 
10568                      /* Pmt contains a positive document */
10569                      IF (p_docsInPmtTab(k).document_amount > 0) THEN
10570 
10571                          /*
10572                           * This payment satisfies the criteria
10573                           * for credit memo adjustment.
10574                           */
10575 
10576                          /*
10577                           * Check if payment already in list
10578                           * of qualifying payments
10579                           */
10580                          l_already_added_flag := checkIfPmtAlreadyAdded(
10581                                                      p_paymentTab(j).
10582                                                          payment_id,
10583                                                      x_negPmtsTab);
10584 
10585                          /*
10586                           * If payment not already in qualifying
10587                           * list, add payment to the list.
10588                           */
10589                          IF (l_already_added_flag = FALSE) THEN
10590 
10591                              x_negPmtsTab(x_negPmtsTab.COUNT + 1)
10592                                  := p_paymentTab(j).payment_id;
10593 
10594                              print_debuginfo(l_module_name, 'Payment id '
10595                                  || p_paymentTab(j).payment_id
10596                                  || ' qualifies for '
10597                                  || 'credit memo adjustment.'
10598                                  );
10599 
10600                          END IF;
10601 
10602                      END IF; -- if doc amount greater than zero
10603 
10604                  END IF; -- if document matches the payment id
10605 
10606              END LOOP; -- loop through all documents of this payment
10607 
10608          END IF; -- if payment amount less than zero
10609 
10610      END LOOP; -- loop through all payments
10611 
10612  END getListOfQualifyingNegPmts;
10613 
10614 /*--------------------------------------------------------------------
10615  | NAME:
10616  |     populateLEsOnPmts
10617  |
10618  | PURPOSE:
10619  |
10620  |
10621  | PARAMETERS:
10622  |     IN
10623  |
10624  |
10625  |     OUT
10626  |
10627  |
10628  | RETURNS:
10629  |
10630  | NOTES:
10631  |
10632  *---------------------------------------------------------------------*/
10633  PROCEDURE populateLEsOnPmts(
10634      x_paymentTab      IN OUT NOCOPY paymentTabType,
10635      p_bankAccountLEs  IN            bankAccountLETabType
10636      )
10637  IS
10638  BEGIN
10639 
10640      IF (x_paymentTab.COUNT = 0 OR p_bankAccountLEs.COUNT = 0) THEN
10641          RETURN;
10642      END IF;
10643 
10644      /*
10645       * Loop through all the created payments, searching for
10646       * internal bank account ids. If we find a match, assign the
10647       * legal entity id on the internal bank account to the payment.
10648       */
10649      FOR i IN x_paymentTab.FIRST .. x_paymentTab.LAST LOOP
10650 
10651          FOR j in p_bankAccountLEs.FIRST .. p_bankAccountLEs.LAST LOOP
10652 
10653              IF (x_paymentTab(i).internal_bank_account_id =
10654                  p_bankAccountLEs(j).int_bank_acct_id) THEN
10655 
10656                  x_paymentTab(i).legal_entity_id := p_bankAccountLEs(j).le_id;
10657 
10658              END IF;
10659 
10660          END LOOP;
10661 
10662      END LOOP;
10663 
10664  END populateLEsOnPmts;
10665 
10666 /*--------------------------------------------------------------------
10667  | NAME:
10668  |     printDocsInPmtTab
10669  |
10670  | PURPOSE:
10671  |     Prints out the documents and payments within the docsInPaymentTabType
10672  |     PLSQL table. To be used for debug purposes.
10673  |
10674  | PARAMETERS:
10675  |     IN
10676  |
10677  |     OUT
10678  |
10679  |
10680  | RETURNS:
10681  |
10682  | NOTES:
10683  |
10684  *---------------------------------------------------------------------*/
10685  PROCEDURE printDocsInPmtTab(
10686      p_docsInPmtTab    IN docsInPaymentTabType
10687      )
10688  IS
10689  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME || '.printDocsInPmtTab';
10690  BEGIN
10691 
10692      IF (p_docsInPmtTab.COUNT = 0) THEN
10693          RETURN;
10694      END IF;
10695 
10696      print_debuginfo(l_module_name, 'List of docs/pmts: ');
10697 
10698      FOR i in p_docsInPmtTab.FIRST..p_docsInPmtTab.LAST LOOP
10699          print_debuginfo(l_module_name, 'Payment: '
10700              || p_docsInPmtTab(i).payment_id
10701              || ', document: '
10702              || p_docsInPmtTab(i).document_id
10703              || ', document amt: '
10704              || p_docsInPmtTab(i).document_amount
10705              || ', document curr: '
10706              || p_docsInPmtTab(i).document_currency
10707              );
10708      END LOOP;
10709 
10710  END printDocsInPmtTab;
10711 
10712 /*--------------------------------------------------------------------
10713  | NAME:
10714  |     handleJapaneseBankCharges
10715  |
10716  | PURPOSE:
10717  |
10718  |
10719  |
10720  | PARAMETERS:
10721  |     IN
10722  |
10723  |     OUT
10724  |
10725  |
10726  | RETURNS:
10727  |
10728  | NOTES:
10729  |
10730  *---------------------------------------------------------------------*/
10731  PROCEDURE handleJapaneseBankCharges(
10732      p_cap_payreq_cd  IN            VARCHAR2,
10733      p_cap_id         IN            NUMBER,
10734      x_paymentTab     IN OUT NOCOPY paymentTabType,
10735      x_docsInPmtTab   IN OUT NOCOPY docsInPaymentTabType,
10736      x_docErrorTab    IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.docErrorTabType,
10737      x_errTokenTab    IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.trxnErrTokenTabType
10738      )
10739  IS
10740 
10741  /* these two are passed to calling app via hook */
10742  l_hookPaymentTab    IBY_PAYGROUP_PUB.hookPaymentTabType;
10743  l_hookDocsInPmtTab  IBY_PAYGROUP_PUB.hookDocsInPaymentTabType;
10744 
10745  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
10746                                              '.handleJapaneseBankCharges';
10747 
10748  l_hook_name        VARCHAR2(500);
10749  l_stmt             VARCHAR2(1000);
10750 
10751  /* used in invocation of callout procedure */
10752  l_api_version    CONSTANT NUMBER := 1.0;
10753  l_msg_count      NUMBER;
10754  l_msg_data       VARCHAR2(2000);
10755 
10756  l_return_status  VARCHAR2(500);
10757 
10758  /*
10759   * Package name can be hardcoded since we know that this hook
10760   * is only called by AP.
10761   */
10762  l_pkg_name    CONSTANT VARCHAR2(100) := 'AP_PMT_CALLOUT_PKG';
10763 
10764  /*
10765   * Implementing the hook is optional for the calling app.
10766   * If the calling app does not implement the hook, then
10767   * the call to the hook will result in ORA-06576 error.
10768   *
10769   * There is no exception name associated with this code, so
10770   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
10771   * exception occurs, it is not fatal: we log the error and
10772   * proceed.
10773   *
10774   * If, on the other hand, the calling app implements the
10775   * hook, but the hook throws an exception, it is fatal
10776   * and we must abort the program (this will be caught
10777   * in WHEN OTHERS block).
10778   */
10779  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
10780  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
10781 
10782  BEGIN
10783 
10784      print_debuginfo(l_module_name, 'ENTER');
10785 
10786      /*
10787       * Populate the hook tables with payment data.
10788       */
10789      performPreHookProcess(p_cap_payreq_cd, p_cap_id, x_paymentTab,
10790          x_docsInPmtTab, l_hookPaymentTab, l_hookDocsInPmtTab);
10791 
10792      /*
10793       * Now try to call the external app's implementation of the hook.
10794       * The calling app may or may not have implemented the hook, so
10795       * it's not fatal if the implementation does not exist.
10796       */
10797      l_hook_name := l_pkg_name || '.' || 'ap_JapanBankChargeHook';
10798 
10799      print_debuginfo(l_module_name, 'Attempting to call hook: '
10800          || l_hook_name);
10801 
10802      l_stmt := 'CALL '|| l_hook_name || '(:1, :2, :3, :4, :5, :6)';
10803 
10804      BEGIN
10805 
10806          EXECUTE IMMEDIATE
10807              (l_stmt)
10808          USING
10809              IN  l_api_version,
10810              IN  FND_API.G_FALSE,
10811              IN  FND_API.G_FALSE,
10812              OUT l_return_status,
10813              OUT l_msg_count,
10814              OUT l_msg_data
10815          ;
10816 
10817      EXCEPTION
10818 
10819          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
10820              print_debuginfo(l_module_name, 'Japanese bank charge hook "'
10821                  || l_hook_name
10822                  || '" not implemented by external application.'
10823                  );
10824 
10825              print_debuginfo(l_module_name, 'Skipping hook call.');
10826 
10827          WHEN OTHERS THEN
10828              print_debuginfo(l_module_name, 'Fatal: Japanese bank charge '
10829                  || 'hook ''' || l_hook_name
10830                  || ''', generated exception.',
10831                  FND_LOG.LEVEL_UNEXPECTED
10832                  );
10833 
10834              print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE,
10835                  FND_LOG.LEVEL_UNEXPECTED);
10836              print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM,
10837                  FND_LOG.LEVEL_UNEXPECTED);
10838 
10839              /*
10840               * Propogate exception to caller.
10841               */
10842              RAISE;
10843      END;
10844 
10845      /*
10846       * If Japanese bank charge hook returned an error response,
10847       * raise an exception.
10848       */
10849      IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
10850 
10851          print_debuginfo(l_module_name, 'Fatal: Japanese bank charge hook '''
10852              || l_hook_name  || ''' returned error status code: '
10853              || l_return_status,
10854              FND_LOG.LEVEL_UNEXPECTED
10855              );
10856 
10857          print_debuginfo(l_module_name, 'Raising exception .. ',
10858              FND_LOG.LEVEL_UNEXPECTED);
10859 
10860          APP_EXCEPTION.RAISE_EXCEPTION;
10861 
10862      END IF;
10863 
10864      /*
10865       * Read back the adjusted payment data.
10866       */
10867      performPostHookProcess(x_paymentTab, x_docsInPmtTab, l_hookPaymentTab,
10868          l_hookDocsInPmtTab, x_docErrorTab, x_errTokenTab);
10869 
10870      print_debuginfo(l_module_name, 'EXIT');
10871 
10872  END handleJapaneseBankCharges;
10873 
10874 /*--------------------------------------------------------------------
10875  | NAME:
10876  |     performExtendedWitholding
10877  |
10878  | PURPOSE:
10879  |
10880  |
10881  |
10882  | PARAMETERS:
10883  |     IN
10884  |
10885  |     OUT
10886  |
10887  |
10888  | RETURNS:
10889  |
10890  | NOTES:
10891  |
10892  *---------------------------------------------------------------------*/
10893  PROCEDURE performExtendedWitholding(
10894      p_cap_payreq_cd  IN            VARCHAR2,
10895      p_cap_id         IN            NUMBER,
10896      x_paymentTab     IN OUT NOCOPY paymentTabType,
10897      x_docsInPmtTab   IN OUT NOCOPY docsInPaymentTabType,
10898      x_docErrorTab    IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.docErrorTabType,
10899      x_errTokenTab    IN OUT NOCOPY IBY_VALIDATIONSETS_PUB.trxnErrTokenTabType
10900      )
10901  IS
10902 
10903  /* these two are passed to calling app via hook */
10904  l_hookPaymentTab    IBY_PAYGROUP_PUB.hookPaymentTabType;
10905  l_hookDocsInPmtTab  IBY_PAYGROUP_PUB.hookDocsInPaymentTabType;
10906 
10907  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME ||
10908                                              '.performExtendedWitholding';
10909 
10910  l_hook_name        VARCHAR2(500);
10911  l_stmt             VARCHAR2(1000);
10912 
10913  /* used in invocation of callout procedure */
10914  l_api_version    CONSTANT NUMBER := 1.0;
10915  l_msg_count      NUMBER;
10916  l_msg_data       VARCHAR2(2000);
10917 
10918  l_return_status  VARCHAR2(500);
10919 
10920  /*
10921   * Package name can be hardcoded since we know that this hook
10922   * is only called by AP.
10923   */
10924  l_pkg_name    CONSTANT VARCHAR2(100) := 'AP_AWT_CALLOUT_PKG';
10925 
10926  /*
10927   * Implementing the hook is optional for the calling app.
10928   * If the calling app does not implement the hook, then
10929   * the call to the hook will result in ORA-06576 error.
10930   *
10931   * There is no exception name associated with this code, so
10932   * we create one called 'PROCEDURE_NOT_IMPLEMENTED'. If this
10933   * exception occurs, it is not fatal: we log the error and
10934   * proceed.
10935   *
10936   * If, on the other hand, the calling app implements the
10937   * hook, but the hook throws an exception, it is fatal
10938   * and we must abort the program (this will be caught
10939   * in WHEN OTHERS block).
10940   */
10941  PROCEDURE_NOT_IMPLEMENTED EXCEPTION;
10942  PRAGMA EXCEPTION_INIT(PROCEDURE_NOT_IMPLEMENTED, -6576);
10943 
10944  BEGIN
10945 
10946      print_debuginfo(l_module_name, 'ENTER');
10947 
10948      /*
10949       * Populate the hook tables with payment data.
10950       */
10951      performPreHookProcess(p_cap_payreq_cd, p_cap_id, x_paymentTab,
10952          x_docsInPmtTab, l_hookPaymentTab, l_hookDocsInPmtTab);
10953 
10954      /*
10955       * Now try to call the external app's implementation of the hook.
10956       * The calling app may or may not have implemented the hook, so
10957       * it's not fatal if the implementation does not exist.
10958       */
10959      l_hook_name := l_pkg_name || '.' || 'zx_paymentsAdjustHook';
10960 
10961      print_debuginfo(l_module_name, 'Attempting to call hook: '
10962          || l_hook_name);
10963 
10964      l_stmt := 'CALL '|| l_hook_name || '(:1, :2, :3, :4, :5, :6)';
10965 
10966      BEGIN
10967 
10968          EXECUTE IMMEDIATE
10969              (l_stmt)
10970          USING
10971              IN  l_api_version,
10972              IN  FND_API.G_FALSE,
10973              IN  FND_API.G_FALSE,
10974              OUT l_return_status,
10975              OUT l_msg_count,
10976              OUT l_msg_data
10977          ;
10978 
10979      EXCEPTION
10980 
10981          WHEN PROCEDURE_NOT_IMPLEMENTED THEN
10982              print_debuginfo(l_module_name, 'Extended withholding hook "'
10983                  || l_hook_name
10984                  || '" not implemented by external application.'
10985                  );
10986 
10987              print_debuginfo(l_module_name, 'Skipping hook call.');
10988 
10989          WHEN OTHERS THEN
10990              print_debuginfo(l_module_name, 'Fatal: Extended witholding hook '''
10991                  || l_hook_name  || ''', generated exception. ',
10992                  FND_LOG.LEVEL_UNEXPECTED
10993                  );
10994 
10995              print_debuginfo(l_module_name, 'SQL code: '   || SQLCODE,
10996                  FND_LOG.LEVEL_UNEXPECTED);
10997              print_debuginfo(l_module_name, 'SQL err msg: '|| SQLERRM,
10998                  FND_LOG.LEVEL_UNEXPECTED);
10999 
11000              /*
11001               * Propogate exception to caller.
11002               */
11003              RAISE;
11004      END;
11005 
11006      /*
11007       * If extended withholding hook returned an error response,
11008       * raise an exception.
11009       */
11010      IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
11011 
11012          print_debuginfo(l_module_name, 'Fatal: Extended witholding hook '''
11013              || l_hook_name  || ''' returned error status code: '
11014              || l_return_status,
11015              FND_LOG.LEVEL_UNEXPECTED
11016              );
11017 
11018          print_debuginfo(l_module_name, 'Raising exception .. ',
11019              FND_LOG.LEVEL_UNEXPECTED);
11020 
11021          APP_EXCEPTION.RAISE_EXCEPTION;
11022 
11023      END IF;
11024 
11025      /*
11026       * Read back the adjusted payment data.
11027       */
11028      performPostHookProcess(x_paymentTab, x_docsInPmtTab, l_hookPaymentTab,
11029          l_hookDocsInPmtTab, x_docErrorTab, x_errTokenTab);
11030 
11031      print_debuginfo(l_module_name, 'EXIT');
11032 
11033  END performExtendedWitholding;
11034 
11035 /*--------------------------------------------------------------------
11036  | NAME:
11037  |     initializePmts
11038  |
11039  | PURPOSE:
11040  |     Sets the default attributes for a created payment such as
11041  |     payment status, process type etc.
11042  |
11043  | PARAMETERS:
11044  |     IN
11045  |
11046  |
11047  |     OUT
11048  |
11049  |
11050  | RETURNS:
11051  |
11052  | NOTES:
11053  |
11054  *---------------------------------------------------------------------*/
11055  PROCEDURE initializePmts(
11056      x_paymentTab      IN OUT NOCOPY paymentTabType
11057      )
11058  IS
11059  BEGIN
11060 
11061      IF (x_paymentTab.COUNT = 0) THEN
11062          RETURN;
11063      END IF;
11064 
11065      FOR i IN x_paymentTab.FIRST .. x_paymentTab.LAST LOOP
11066 
11067          x_paymentTab(i).payment_status                 := 'CREATED';
11068          x_paymentTab(i).process_type                   := 'STANDARD';
11069 
11070          /*
11071           * Fix for bug 5115270:
11072           *
11073           * For all flags, take care not to overwite the flag
11074           * already set.
11075           */
11076          x_paymentTab(i).payments_complete_flag         :=
11077              NVL(x_paymentTab(i).payments_complete_flag,         'N');
11078          x_paymentTab(i).bill_payable_flag              :=
11079              NVL(x_paymentTab(i).bill_payable_flag,              'N');
11080          x_paymentTab(i).exclusive_payment_flag         :=
11081              NVL(x_paymentTab(i).exclusive_payment_flag,         'N');
11082          x_paymentTab(i).separate_remit_advice_req_flag :=
11083              NVL(x_paymentTab(i).separate_remit_advice_req_flag, 'N');
11084          x_paymentTab(i).declare_payment_flag           :=
11085              NVL(x_paymentTab(i).declare_payment_flag,           'N');
11086          x_paymentTab(i).pregrouped_payment_flag        :=
11087              NVL(x_paymentTab(i).pregrouped_payment_flag,        'N');
11088          x_paymentTab(i).stop_confirmed_flag            :=
11089              NVL(x_paymentTab(i).stop_confirmed_flag,            'N');
11090          x_paymentTab(i).stop_released_flag             :=
11091              NVL(x_paymentTab(i).stop_released_flag,             'N');
11092          x_paymentTab(i).stop_request_placed_flag       :=
11093              NVL(x_paymentTab(i).stop_request_placed_flag,       'N');
11094 
11095          x_paymentTab(i).created_by                     := fnd_global.user_id;
11096          x_paymentTab(i).creation_date                  := sysdate;
11097          x_paymentTab(i).last_updated_by                := fnd_global.user_id;
11098          x_paymentTab(i).last_update_login              := fnd_global.user_id;
11099          x_paymentTab(i).last_update_date               := sysdate;
11100          x_paymentTab(i).object_version_number          := 1;
11101 
11102      END LOOP;
11103 
11104  END initializePmts;
11105 
11106 /*--------------------------------------------------------------------
11107  | NAME:
11108  |     get_payreq_status
11109  |
11110  |
11111  | PURPOSE:
11112  |
11113  |
11114  | PARAMETERS:
11115  |     IN
11116  |
11117  |
11118  |     OUT
11119  |
11120  |
11121  | RETURNS:
11122  |
11123  | NOTES:
11124  |
11125  *---------------------------------------------------------------------*/
11126  FUNCTION get_payreq_status (
11127      l_payreq_id IN IBY_PAY_SERVICE_REQUESTS.payment_service_request_id%TYPE)
11128      RETURN VARCHAR2
11129  IS
11130 
11131  l_payreq_status     VARCHAR2(100);
11132  l_module_name       CONSTANT VARCHAR2(200) := G_PKG_NAME ||
11133                                                    '.get_payreq_status';
11134 
11135  BEGIN
11136 
11137      SELECT
11138          payment_service_request_status
11139      INTO
11140          l_payreq_status
11141      FROM
11142          IBY_PAY_SERVICE_REQUESTS
11143      WHERE
11144          payment_service_request_id = l_payreq_id;
11145 
11146      RETURN l_payreq_status;
11147 
11148  EXCEPTION
11149      WHEN OTHERS THEN
11150          print_debuginfo(l_module_name, 'Exception occured when '
11151              || 'retrieving payment request status for '
11152              || 'payment request '
11153              || l_payreq_id
11154              );
11155          print_debuginfo(l_module_name, 'SQLCODE: ' || SQLCODE);
11156          print_debuginfo(l_module_name, 'SQLERRM: ' || SQLERRM);
11157          print_debuginfo(l_module_name, 'Returning NULL for status');
11158          RETURN NULL;
11159 
11160  END get_payreq_status;
11161 
11162 /*--------------------------------------------------------------------
11163  | NAME:
11164  |     rebuildPayments
11165  |
11166  | PURPOSE:
11167  |
11168  |
11169  |
11170  | PARAMETERS:
11171  |     IN
11172  |
11173  |
11174  |     OUT
11175  |
11176  |
11177  | RETURNS:
11178  |
11179  | NOTES:
11180  |
11181  *---------------------------------------------------------------------*/
11182  PROCEDURE rebuildPayments(
11183      p_payment_request_id   IN IBY_PAY_SERVICE_REQUESTS.
11184                                          payment_service_request_id%TYPE,
11185      x_paymentTab           IN OUT NOCOPY IBY_PAYGROUP_PUB.paymentTabType,
11186      x_docsInPmtTab         IN OUT NOCOPY IBY_PAYGROUP_PUB.
11187                                               docsInPaymentTabType,
11188      x_ca_id                IN OUT NOCOPY IBY_PAY_SERVICE_REQUESTS.
11189                                               calling_app_id%TYPE,
11190      x_ca_payreq_cd         IN OUT NOCOPY IBY_PAY_SERVICE_REQUESTS.
11191                                               call_app_pay_service_req_code
11192                                               %TYPE,
11193      x_payReqCriteria       IN OUT NOCOPY IBY_PAYGROUP_PUB.
11194                                               payReqImposedCriteria
11195 --  ,x_cbrTab               IN OUT NOCOPY IBY_PAYGROUP_PUB.
11196 --                                             centralBankReportTabType
11197      )
11198  IS
11199  l_module_name           CONSTANT VARCHAR2(200) := G_PKG_NAME ||
11200                                                    '.rebuildPayments';
11201 
11202  l_ca_payreq_cd            VARCHAR2(255) := '';
11203  l_ca_id                   NUMBER(15)    := 0;
11204 
11205  l_paymentTab        IBY_PAYGROUP_PUB.paymentTabType;
11206 
11207  /*
11208   * Stores the documents payable for all pmts in request.
11209   */
11210  l_docsInPmtTab      IBY_PAYGROUP_PUB.docsInPaymentTabType;
11211 
11212  /*
11213   * Stores the documents payable for a single payment.
11214   */
11215  l_docsForPmtTab     IBY_PAYGROUP_PUB.docsInPaymentTabType;
11216 
11217  l_pmt_amount        IBY_PAYMENTS_ALL.payment_amount%TYPE;
11218 
11219  /*
11220   * For storing central bank reporting information related to
11221   * each payment.
11222   */
11223 -- l_cbrRec            IBY_PAYGROUP_PUB.centralBankReportRecType;
11224 -- l_cbrTab            IBY_PAYGROUP_PUB.centralBankReportTabType;
11225 
11226  /*
11227   * Cursor to pick up payments.
11228   */
11229  CURSOR c_payments(p_payment_request_id NUMBER)
11230  IS
11231  SELECT
11232      *
11233  FROM
11234      IBY_PAYMENTS_ALL
11235  WHERE
11236      payment_service_request_id = p_payment_request_id AND
11237 
11238      /*
11239       * For the rebuild:
11240       * Only select payments that are in non-failed
11241       * status, or payments that are soft failed
11242       * ('FAILED_VALIDATION') status here. Do not
11243       * select payments that are is 'REJECTED' status
11244       * here as the underlying documents of rejected
11245       * payments would have already been kicked back to AP.
11246       */
11247      payment_status IN
11248          (
11249          PAY_STATUS_CREATED,
11250          PAY_STATUS_MODIFIED,
11251          PAY_STATUS_FAIL_VALID
11252          )
11253      ;
11254 
11255  /*
11256   * Cursor to pick up documents.
11257   */
11258  CURSOR c_documents(p_payment_request_id NUMBER, p_payment_id NUMBER)
11259  IS
11260  SELECT
11261      payment_id,
11262      document_payable_id,
11263      calling_app_id,
11264      calling_app_doc_unique_ref1,
11265      calling_app_doc_unique_ref2,
11266      calling_app_doc_unique_ref3,
11267      calling_app_doc_unique_ref4,
11268      calling_app_doc_unique_ref5,
11269      pay_proc_trxn_type_code,
11270      payment_amount,
11271      payment_currency_code,
11272      payment_grouping_number,
11273      document_status,
11274      amount_withheld,
11275      payment_due_date,
11276      discount_date,
11277      internal_bank_account_id,
11278      ext_payee_id,
11279      payee_party_id,
11280      party_site_id,
11281      supplier_site_id,
11282      org_id,
11283      org_type,
11284      external_bank_account_id,
11285      payment_curr_discount_taken,
11286      delivery_channel_code,
11287      unique_remittance_identifier,
11288      uri_check_digit,
11289      inv_payee_party_id,
11290      inv_party_site_id,
11291      inv_supplier_site_id,
11292      inv_beneficiary_party,
11293      ext_inv_payee_id,
11294      relationship_id
11295  FROM
11296      IBY_DOCS_PAYABLE_ALL
11297  WHERE
11298      payment_service_request_id = p_payment_request_id    AND
11299      payment_id                 = p_payment_id            AND
11300      document_status IN
11301      (
11302      DOC_STATUS_PAY_CREATED,
11303      DOC_STATUS_PAY_VAL_FAIL
11304      )
11305      ;
11306 
11307  /*
11308   * Cursor to pick up central bank reporting
11309   * related information.
11310  CURSOR c_cbr(p_payment_id NUMBER)
11311  IS
11312  SELECT
11313      pmt.payment_id,
11314      prof.declaration_option,
11315      prof.dcl_only_foreign_curr_pmt_flag,
11316      prof.declaration_curr_fx_rate_type,
11317      prof.declaration_currency_code,
11318      prof.declaration_threshold_amount
11319  FROM
11320      IBY_PAYMENTS_ALL pmt,
11321      IBY_PAYMENT_PROFILES prof
11322  WHERE
11323      pmt.payment_id = p_payment_id AND
11324      pmt.payment_profile_id = prof.payment_profile_id
11325      ;
11326   */
11327 
11328  BEGIN
11329 
11330      print_debuginfo(l_module_name, 'ENTER');
11331 
11332      /*
11333       * Pick up all the payments of the payment request.
11334       */
11335      OPEN  c_payments(p_payment_request_id);
11336      FETCH c_payments BULK COLLECT INTO l_paymentTab;
11337      CLOSE c_payments;
11338 
11339      /*
11340       * Exit if no payments were found.
11341       */
11342      IF (l_paymentTab.COUNT = 0) THEN
11343 
11344          print_debuginfo(l_module_name, 'No payments were '
11345              || 'retrieved from DB for payment request '
11346              || p_payment_request_id
11347              || '. Exiting rebuild ..');
11348 
11349          print_debuginfo(l_module_name, 'EXIT');
11350 
11351          RETURN;
11352 
11353      ELSE
11354 
11355          print_debuginfo(l_module_name, '# valid payments '
11356              || 'retrieved from DB for payment request '
11357              || p_payment_request_id
11358              || ' = '
11359              || l_paymentTab.COUNT);
11360 
11361      END IF;
11362 
11363      /*
11364       * Pick up all the valid documents for the above payments.
11365       */
11366      FOR i IN l_paymentTab.FIRST .. l_paymentTab.LAST LOOP
11367 
11368          /*
11369           * Pick up all the documents of the payment request.
11370           */
11371          OPEN  c_documents(p_payment_request_id, l_paymentTab(i).payment_id);
11372          FETCH c_documents BULK COLLECT INTO l_docsForPmtTab;
11373          CLOSE c_documents;
11374 
11375          IF (l_docsForPmtTab.COUNT > 0) THEN
11376 
11377              print_debuginfo(l_module_name, 'Picked up '
11378                  || l_docsForPmtTab.COUNT
11379                  || ' documents for payment '
11380                  || l_paymentTab(i).payment_id
11381                  );
11382 
11383               /*
11384                * Sum up the payment amount from all the
11385                * document amounts.
11386                */
11387               l_pmt_amount := 0;
11388               FOR j IN l_docsForPmtTab.FIRST .. l_docsForPmtTab.LAST LOOP
11389 
11390                   l_pmt_amount := l_pmt_amount +
11391                                       l_docsForPmtTab(j).document_amount;
11392 
11393               END LOOP;
11394 
11395               print_debuginfo(l_module_name, 'Adjusting payment amount '
11396                   || 'for payment '
11397                   || l_paymentTab(i).payment_id
11398                   || ' from '
11399                   || l_paymentTab(i).payment_amount
11400                   || ' to '
11401                   || l_pmt_amount
11402                   );
11403 
11404               l_paymentTab(i).payment_amount := l_pmt_amount;
11405 
11406               FOR q IN l_docsForPmtTab.FIRST .. l_docsForPmtTab.LAST LOOP
11407 
11408                   /*
11409                    * We are now assembling the payments that need to
11410                    * be re-built along with their child documents.
11411                    *
11412                    * These payments will be re-validated. If any
11413                    * payment fails validation, its child documents
11414                    * will be failed as well and the documents' status
11415                    * will be set to 'PAYMENT_FAILED_VALIDATION'.
11416                    *
11417                    * At this point, as we are assembling the
11418                    * documents payable, we need to re-initialize the
11419                    * status of these documents. By default, set the
11420                    * status of these documents to 'PAYMENT_CREATED'.
11421                    *
11422                    * These documents will be failed downstream if their
11423                    * parent payment fails validation for some reason.
11424                    */
11425                   l_docsForPmtTab(q).document_status := DOC_STATUS_PAY_CREATED;
11426 
11427               END LOOP;
11428 
11429               /*
11430                * If we reached here, it means that we were able to
11431                * find the documents payable associated with the
11432                * current payment.
11433                *
11434                * Store this payment and the corresponding documents
11435                * into the l_docsInPmtTab array.
11436                */
11437               FOR k IN l_docsForPmtTab.FIRST .. l_docsForPmtTab.LAST LOOP
11438 
11439                   l_docsInPmtTab(l_docsInPmtTab.COUNT + 1) :=
11440                       l_docsForPmtTab(k);
11441 
11442               END LOOP;
11443 
11444          ELSE
11445 
11446              print_debuginfo(l_module_name, 'No documents '
11447                  || 'were picked up for payment '
11448                  || l_paymentTab(i).payment_id
11449                  );
11450 
11451              print_debuginfo(l_module_name, 'All valid documents '
11452                  || 'of payment '
11453                  || l_paymentTab(i).payment_id
11454                  || ' have been removed. Setting payment '
11455                  || 'amount to zero.'
11456                  );
11457 
11458              l_paymentTab(i).payment_amount := 0;
11459 
11460          END IF;
11461 
11462      END LOOP;
11463 
11464      /*
11465       * Pick up details of this payment request.
11466       */
11467      SELECT
11468          calling_app_id,
11469          call_app_pay_service_req_code
11470      INTO
11471          l_ca_id,
11472          l_ca_payreq_cd
11473      FROM
11474          IBY_PAY_SERVICE_REQUESTS
11475      WHERE
11476          PAYMENT_SERVICE_REQUEST_ID = p_payment_request_id
11477      ;
11478      print_debuginfo(l_module_name, 'For payment request '
11479          || p_payment_request_id
11480          || ' calling app id is '
11481          || l_ca_id
11482          || ' and call app pay service req code is '
11483          || l_ca_payreq_cd
11484          );
11485 
11486     /*
11487      * Fix for bug 5587110:
11488      *
11489      * Fetch the central bank reporting related
11490      * data based on the profile of the retrieved
11491      * payments.
11492      *
11493      * Without this information we cannot perform
11494      * central bank reporting.
11495     FOR i IN l_paymentTab.FIRST .. l_paymentTab.LAST LOOP
11496 
11497         OPEN  c_cbr(l_paymentTab(i).payment_id);
11498         FETCH c_cbr INTO l_cbrRec;
11499         CLOSE c_cbr;
11500 
11501         l_cbrTab(l_cbrTab.COUNT + 1) := l_cbrRec;
11502 
11503     END LOOP;
11504      */
11505 
11506      /*
11507       * Finally, return the payments created by grouping to the caller.
11508       */
11509      x_paymentTab   := l_paymentTab;
11510      x_docsInPmtTab := l_docsInPmtTab;
11511      x_ca_id        := l_ca_id;
11512      x_ca_payreq_cd := l_ca_payreq_cd;
11513 --     x_cbrTab       := l_cbrTab;
11514 
11515      print_debuginfo(l_module_name, 'EXIT');
11516 
11517  END rebuildPayments;
11518 
11519 /*--------------------------------------------------------------------
11520  | NAME:
11521  |     auditPaymentData
11522  |
11523  | PURPOSE:
11524  |
11525  |
11526  |
11527  | PARAMETERS:
11528  |     IN
11529  |
11530  |
11531  |     OUT
11532  |
11533  |
11534  | RETURNS:
11535  |
11536  | NOTES:
11537  |
11538  |     See bug 5399951 for history behind some of the SQL joins
11539  |
11540  *---------------------------------------------------------------------*/
11541  PROCEDURE auditPaymentData(
11542      p_paymentTab      IN paymentTabType
11543      )
11544  IS
11545  l_module_name      CONSTANT VARCHAR2(200) := G_PKG_NAME ||
11546                                                   '.auditPaymentData';
11547 
11548  CURSOR c_audit_columns (p_payment_id  NUMBER)
11549  IS
11550  SELECT
11551 
11552   /* PAYMENT RELATED */
11553   pmt.payment_id,                                                    -- 01
11554 
11555   /* PAYER */
11556   payer_party.party_number,            -- payer party number
11557   '',                                  -- payer party site name
11558   payer.name,                          -- payer name
11559   IBY_FD_EXTRACT_GEN_PVT.              -- payer tax reg number
11560       Get_FP_TaxRegistration
11561           (
11562           payer.legal_entity_id
11563           ),
11564   payer.registration_number,           -- payer le reg number
11565   payer.party_id,                      -- payer_party_id
11566   payer.location_id,                   -- payer_location_id
11567   payer_party.attribute_category,      -- payer party attr category
11568   payer.attribute_category,            -- payer LE attr category     -- 10
11569 
11570   /* PAYER SPECIAL FIELDS */
11571   '',                                  -- payer abbrev agency code
11572   '',                                  -- payer federal us employer id
11573 
11574   /* PAYER BANK */
11575   iba_bnk_branch.bank_name,            -- payer bank name
11576   iba_bnk_branch.bank_number,          -- payer bank number
11577   iba_bnk_branch.branch_number,        -- payer bank branch number
11578   iba_bnk_branch.bank_branch_name,     -- payer bank branch name
11579   iba_bnk_branch.eft_swift_code,       -- payer bank swift code
11580   iba.bank_account_num,                -- payer bank account num
11581   iba.bank_account_name,               -- payer bank account name
11582   iba.iban_number,                     -- payer bank acct iban number -- 20
11583   iba.agency_location_code,            -- payer bank agency location code
11584   iba_bnk_branch.branch_party_id,      -- payer bank branch party id
11585   iba_bnk_branch.bank_name_alt,        -- payer bank alt name
11586   iba_bnk_branch.bank_branch_name_alt, -- payer bank branch alt name
11587   iba.bank_account_name_alt,           -- payer bank alt account name
11588   iba.bank_account_num_electronic,     -- payer bank account num electronic
11589   iba_branch_party_site.location_id,   -- payer bank branch location id
11590   iba_bnk_branch.eft_user_number,      -- payer bank branch eft user num
11591 
11592   /* PAYEE */
11593   payee.party_number,                  -- payee party number
11594 
11595   /* Fix for bug 7391524
11596    * Full Name when Payee is an Employee is fetch from HR tables
11597    * This is just as a temporary fix, until TCA overcomes it architecture limitations
11598    */
11599   DECODE(pmt.employee_person_id,
11600    NULL,payee.party_name,
11601    Get_Employee_Full_Name(pmt.employee_person_id,pmt.payee_party_id,payee.party_name)) payee_party_name,                    -- payee party name
11602 
11603   /*
11604    * Fix for bug 5466979:
11605    *
11606    * Payee name is always the same as the payee party name.
11607    */
11608   DECODE(pmt.employee_person_id,
11609    NULL,payee.party_name,
11610    Get_Employee_Full_Name(pmt.employee_person_id,pmt.payee_party_id,payee.party_name)) payee_name,                    -- payee name
11611 
11612   DECODE(                              -- payee alternate name
11613         payee.party_type,
11614         'ORGANIZATION',
11615             payee.organization_name_phonetic,
11616         'PERSON',
11617             TRIM(payee.person_first_name_phonetic
11618                 || ' '
11619                 || payee.person_last_name_phonetic)
11620         ),
11621 
11622   /* PAYEE ADDRESS */
11623   payee_addr.add_line1,                -- payee add line1
11624   payee_addr.add_line2,                -- payee add line2
11625   payee_addr.add_line3,                -- payee add line3
11626   payee_addr.add_line4,                -- payee add line4
11627   payee_addr.city,                     -- payee city
11628   payee_addr.county,                   -- payee county
11629   payee_addr.province,                 -- payee province
11630   payee_addr.state,                    -- payee state                -- 40
11631   payee_addr.country,                  -- payee country
11632   payee_addr.postal_code,              -- payee postal code
11633   payee_addr.add_concat,               -- payee address concat
11634 
11635   /*
11636    * Fix for bug 5466979:
11637    *
11638    * Beneficiary name should be the same as the payee party name
11639    * except when beneficiary party is not null.
11640    *
11641    * If beneficiary party is not null, the beneficiary name
11642    * should be the hz_parties.party_name corresponding to the
11643    * beneficiary_party.
11644    */
11645   DECODE (pmt.beneficiary_party,
11646               NULL,
11647               payee.party_name,
11648               (SELECT
11649                    benef_party.party_name
11650                FROM
11651                    HZ_PARTIES benef_party
11652                WHERE
11653                    benef_party.party_id=pmt.beneficiary_party
11654               )
11655            ),                          -- beneficiary name
11656 
11657   payee.attribute_category,            -- payee party attr category
11658   payee_site.attribute_category,       -- payee supplier site attr category
11659   payee_site.party_site_name,          -- payee supplier site name
11660 
11661   /* VENDOR RELATED */
11662   vendor_site.vendor_site_code_alt,    -- payee site alternate name
11663   vendor.segment1,                     -- payee supplier number
11664   vendor.customer_num,                 -- payee first party reference  -- 50
11665   vendor.attribute_category,           -- payee supplier attr category
11666   vendor.vendor_id,                    -- payee supplier id
11667 
11668   /* PAYEE SPECIAL FIELDS */
11669   IBY_FD_EXTRACT_GEN_PVT.              -- payee tax registration number
11670       Get_Payee_TaxRegistration
11671       (vendor.vendor_id,pmt.supplier_site_id),
11672 
11673   /*
11674    * Fix for bug 5468251:
11675    *
11676    * Use AP_SUPPLIERS.NUM_1099 for the payee
11677    * LE registration number.
11678    */
11679 
11680   /*
11681    * Fix for bug 5475920:
11682    *
11683    * Payments can be made for employees or suppliers.
11684    *
11685    * For employees, the payee le reg num (i.e., taxpayer id)
11686    * is stored in PER_PEOPLE_X.
11687    *
11688    * For suppliers, the payee le reg num (i.e., taxpayer id)
11689    * is stored in AP_SUPPLIERS.
11690    *
11691    * Switch between these two tables depending upon payee type
11692    * to get the payee le reg num
11693    */
11694    /*
11695    * Fix for bug 5501968:
11696    *
11697    * In R12, Federal supports thid party payments i.e., payments
11698    * to third parties who may not be employees / suppliers.
11699    *
11700    * For this reason, we cannot assume that tax payer id for
11701    * all non-employees will be stored in AP_SUPPLIERS.
11702    *
11703    * However, we know that all third parties / suppliers will
11704    * have a row in HZ_PARTIES. Therefore, pick up the taxpayer
11705    * id from HZ_PARTIES as this always works.
11706    */
11707    /*
11708    * Fix for bug 5501968:
11709    *
11710    * In R12, Federal supports thid party payments i.e., payments
11711    */
11712    decode(upper(vendor.vendor_type_lookup_code),
11713           'EMPLOYEE', per.national_identifier,
11714           'CONTRACTOR',decode(vendor.organization_type_lookup_code,
11715                               'INDIVIDUAL',vendor.individual_1099,
11716                               'FOREIGN INDIVIDUAL',vendor.individual_1099,
11717    		              'PARTNERSHIP',vendor.individual_1099,
11718                               'FOREIGN PARTNERSHIP',vendor.individual_1099,
11719                               vendor.num_1099),
11720  	  vendor.num_1099),            -- payee Tax Identification Number
11721 
11722   /* PAYEE BANK */
11723   eba.bank_name,                       -- payee bank name
11724   eba.bank_number,                     -- payee bank number
11725   eba.branch_number,                   -- payee bank branch number
11726   eba.bank_branch_name,                -- payee bank branch name
11727 
11728   /*
11729    * Fix for bug 5586882:
11730    *
11731    * Populate the external bank account using
11732    * IBY_EXT_BANK_ACCOUNTS.bank_account_num
11733    * so that we show the unmasked bank account number
11734    */
11735   ext_ba_table.bank_account_num,       -- payee bank account number
11736 
11737   eba.bank_account_name,               -- payee bank account name    -- 60
11738   eba.iban_number,                     -- payee bank account IBAN
11739   eba.eft_swift_code,                  -- payee bank swift code
11740   eba.bank_account_type,               -- payee bank account type
11741   eba.payment_factor_flag,             -- payee bank payment factor flag
11742   eba.primary_acct_owner_party_id,     -- payee bank owner party id
11743   eba.branch_party_id,                 -- payee bank branch party id
11744   eba_bank_branch.bank_name_alt,       -- payee bank name alt
11745   eba_bank_branch.bank_branch_name_alt, -- payee bank branch name alt
11746   eba.alternate_account_name,          -- payee bank alt account name
11747   eba.bank_account_num_electronic,     -- payee bank electronic acct num -- 70
11748   eba.bank_branch_address_id,          -- payee bank branch location id
11749   eba.primary_acct_owner_name,         -- payee bank primary acct owner name
11750 
11751   /* REMITTANCE ADVICE RELATED */
11752   remit_advice.remit_advice_delivery_method,  -- remittance advice delv method
11753   '',                                  -- remittance advice email
11754   '',                                  -- remittance advice fax
11755 
11756   /* DELIVERY CHANNEL RELATED */
11757   deliv.format_value,
11758 
11759   /* DECLARATION REPORT RELATED */
11760   prof.declaration_curr_fx_rate_type,
11761   '',                                  -- declaration format
11762 
11763   /* PROFILE RELATED */
11764   prof.payment_profile_name,           -- account profile name       -- 80
11765   prof.system_profile_name,            -- system profile name
11766 
11767   /* PAYMENT REASON */
11768   pmt_reason.format_value,
11769 
11770   /* BANK INSTRUCTION */
11771   bank_instr1.format_value,
11772   bank_instr2.format_value,
11773 
11774   /* ORG */
11775   org.name,
11776 
11777   /* RFC */
11778   rfc_ca.class_code,
11779 
11780   /* REQUEST */
11781   payreq.call_app_pay_service_req_code,-- ppr name
11782   fnd_app.application_name,             -- source product            -- 88
11783 
11784 
11785 
11786       /*TPP-Start*/
11787 invpayee.party_name 		inv_payee_name,
11788 null		inv_payee_address1,
11789 null		inv_payee_address2,
11790 null		inv_payee_address3,
11791 null		inv_payee_address4,
11792 null		inv_payee_city,
11793 null		inv_payee_postal_code,
11794 null		inv_payee_state,
11795 null		inv_payee_province,
11796 null		inv_payee_county,
11797 null		inv_payee_country,
11798 invpayee.party_name 		inv_payee_party_name,
11799 
11800 decode(upper(invvendor.vendor_type_lookup_code),
11801           'CONTRACTOR',decode(invvendor.organization_type_lookup_code,
11802                               'INDIVIDUAL',invvendor.individual_1099,
11803                               'FOREIGN INDIVIDUAL',invvendor.individual_1099,
11804    		              'PARTNERSHIP',invvendor.individual_1099,
11805                               'FOREIGN PARTNERSHIP',invvendor.individual_1099,
11806                               invvendor.num_1099),
11807  	  invvendor.num_1099)	inv_payee_le_reg_num,
11808 
11809 IBY_FD_EXTRACT_GEN_PVT.
11810  Get_Payee_TaxRegistration (invvendor.vendor_id,pmt.inv_supplier_site_id)
11811 				inv_payee_tax_reg_num,
11812 
11813 null 	inv_payee_address_concat,
11814 DECODE (pmt.inv_beneficiary_party,
11815               NULL,
11816               invpayee.party_name,
11817               (SELECT
11818                    benef_party.party_name
11819                FROM
11820                    HZ_PARTIES benef_party
11821                WHERE
11822                    benef_party.party_id=pmt.inv_beneficiary_party
11823               )
11824            ) 			inv_beneficiary_name,
11825 
11826 invpayee.party_number 		inv_payee_party_number,
11827 invpayee.organization_name_phonetic	inv_payee_alternate_name,
11828 invvendor_site.vendor_site_code_alt	inv_payee_site_alt_name,
11829 invvendor.segment1			inv_payee_supplier_number,
11830 invvendor.customer_num			inv_payee_first_party_ref,
11831 
11832 null ext_bnk_acct_ownr_inv_prty_id,
11833 null ext_bnk_branch_inv_prty_id,
11834 null ext_bnk_acct_ownr_inv_prty_nme,
11835 
11836 invpayee.attribute_category		inv_payee_party_attr_cat,
11837 invvendor.attribute_category		inv_payee_supplier_attr_cat,
11838 invpayee_site.attribute_category	inv_payee_spplr_site_attr_cat,
11839 invpayee_site.party_site_name		inv_payee_supplier_site_name,
11840 invvendor_site.vendor_site_code_alt	inv_payee_spplr_site_alt_name,
11841 invvendor.vendor_id			inv_payee_supplier_id
11842       /*TPP-End*/
11843 
11844 
11845  FROM
11846 
11847   /* Payment related */
11848   IBY_PAYMENTS_ALL         pmt,
11849 
11850   /* Profile related */
11851   IBY_PAYMENT_PROFILES     prof,
11852 
11853   /* Payer */
11854   XLE_FIRSTPARTY_INFORMATION_V  payer,
11855   HZ_PARTIES               payer_party,
11856 
11857   /* Payer bank */
11858   CE_BANK_ACCOUNTS         iba,
11859   CE_BANK_BRANCHES_V       iba_bnk_branch,
11860   HZ_PARTY_SITES           iba_branch_party_site,
11861 
11862   /* Payee */
11863   HZ_PARTIES               payee,
11864   HZ_PARTIES               invpayee,
11865 
11866   /* Vendor related */
11867   AP_SUPPLIERS               vendor,
11868   AP_SUPPLIER_SITES_ALL      vendor_site,
11869   AP_SUPPLIERS               invvendor,
11870   AP_SUPPLIER_SITES_ALL      invvendor_site,
11871 
11872   /* Employee related */
11873   PER_PEOPLE_X             per,
11874 
11875   /* Payee site */
11876   HZ_PARTY_SITES           payee_site,
11877   HZ_PARTY_SITES           invpayee_site,
11878 
11879   /* Payee bank */
11880   IBY_EXT_BANK_ACCOUNTS_V  eba,
11881   IBY_EXT_BANK_ACCOUNTS    ext_ba_table,
11882   CE_BANK_BRANCHES_V       eba_bank_branch,
11883 
11884   /* Remit advice related */
11885   IBY_REMIT_ADVICE_SETUP   remit_advice,
11886 
11887   /* Payment service request related */
11888   IBY_PAY_SERVICE_REQUESTS payreq,
11889   FND_APPLICATION_ALL_VIEW fnd_app,
11890 
11891   /* Delivery channel related */
11892   IBY_DELIVERY_CHANNELS_VL deliv,
11893 
11894   /* Payment reason related */
11895   IBY_PAYMENT_REASONS_VL   pmt_reason,
11896 
11897   /* Bank instruction related */
11898   IBY_BANK_INSTRUCTIONS_VL bank_instr1,
11899   IBY_BANK_INSTRUCTIONS_VL bank_instr2,
11900 
11901   /* Org related */
11902   HR_ALL_ORGANIZATION_UNITS org,
11903 
11904   /* RFC */
11905   HZ_CODE_ASSIGNMENTS          rfc_ca,
11906 
11907   /*
11908    * Fix for bug 5928084:
11909    *
11910    * Derive address of employee type payees using
11911    * PER_ADDRESSES or HR_LOCATIONS.
11912    *
11913    * The select statement below dynamically creates the payee
11914    * address table. The dynamically created table is named
11915    * as payee_add. Columns from this table are referenced in the
11916    * outer select.
11917    *
11918    * The payee address can be either a
11919    *  - supplier address, or
11920    *  - employee address
11921    *
11922    * We decide whether we should pick up supplier addresses or
11923    * employee addresses using the 'address source' field on the
11924    * payment.
11925    *
11926    * 'address source' can contain two values
11927    *  - TCA: supplier address
11928    *  - HR : employee address
11929    *
11930    * Supplier addresses are present in TCA and are located using
11931    * remit to location id column on the payment.
11932    *
11933    * Employee addresses can be in either
11934    *   - PER_ADDRESSES (home addresses)
11935    *   - HR_LOCATIONS  (office addresses)
11936    * We use the 'employee address code' field on the payment to
11937    * decide which table to use.
11938    *
11939    * The select below uses decode clause to perform the
11940    * if else logic to retrieve the address from the right
11941    * column.
11942    *
11943    * Roughly, the decode clauses are used to effect the following
11944    * logic -
11945    *
11946    * for each add field:
11947    * if (pmt.address_source = 'TCA')
11948    * {
11949    *   -- get the address field from HZ_LOCATIONS --
11950    * }
11951    * else
11952    * {
11953    *     -- this is an employee address --
11954    *     if (pmt.employee_address_code = 'HOME')
11955    *     {
11956    *         -- get the address field from PER_ADDRESSES --
11957    *
11958    *         The select logic for this has been lifted from
11959    *         AP's expense report import program.
11960    *     }
11961    *     else
11962    *     {
11963    *         -- get the address field from HR_LOCATIONS --
11964    *
11965    *         The select logic for this has been lifted from
11966    *         <APDEV>/patch/115/sql/apwexptb.pls.
11967    *     }
11968    * }
11969    */
11970   (
11971      SELECT
11972 
11973       /* payee add line1 */
11974       DECODE(
11975         pmt.address_source,
11976 
11977         -- supplier address line 1
11978         'TCA', payee_loc.address1,
11979 
11980         -- employee add line 1
11981         DECODE
11982         (
11983           pmt.employee_address_code,
11984 
11985           -- employee home addr line 1
11986           'HOME', per_addr.address_line1,
11987 
11988           -- employee office addr line 1
11989           'OFFICE',per_loc.address_line_1,
11990 
11991           -- employee provisional addr line 1
11992           'PROVISIONAL', per_addr_prov.address_line1,
11993 
11994           --address code not specified
11995           DECODE (per_addr.address_id,
11996                  NULL, per_loc.address_line_1,
11997                  per_addr.address_line1)
11998          )
11999 
12000         ) add_line1,
12001 
12002       /* payee add line2 */
12003       DECODE(
12004         pmt.address_source,
12005 
12006         -- supplier address line 2
12007         'TCA', payee_loc.address2,
12008 
12009         -- employee add line 2
12010         DECODE
12011         (
12012           pmt.employee_address_code,
12013 
12014           -- employee home addr line 2
12015           'HOME', per_addr.address_line2,
12016 
12017           -- employee office addr line 2
12018           'OFFICE',per_loc.address_line_2,
12019 
12020           -- employee provisional addr line 2
12021           'PROVISIONAL', per_addr_prov.address_line2,
12022 
12023           --address code not specified
12024           DECODE (per_addr.address_id,
12025                  NULL, per_loc.address_line_2,
12026                  per_addr.address_line2)
12027          )
12028         ) add_line2,
12029 
12030 
12031       /* payee add line3 */
12032       DECODE(
12033         pmt.address_source,
12034 
12035         -- supplier address line 3
12036         'TCA', payee_loc.address3,
12037 
12038         -- employee add line 3
12039         DECODE
12040         (
12041           pmt.employee_address_code,
12042 
12043           -- employee home addr line 3
12044           'HOME', per_addr.address_line3,
12045 
12046           -- employee office addr line 3
12047           'OFFICE',per_loc.address_line_3,
12048 
12049           -- employee provisional addr line 3
12050           'PROVISIONAL', per_addr_prov.address_line3,
12051 
12052           --address code not specified
12053           DECODE (per_addr.address_id,
12054                  NULL, per_loc.address_line_3,
12055                  per_addr.address_line3)
12056          )
12057 
12058         ) add_line3,
12059 
12060 
12061       /* payee add line4 */
12062       DECODE(
12063         pmt.address_source,
12064 
12065         -- supplier address line 4
12066         'TCA', payee_loc.address4,
12067 
12068         -- employee home/office addr line 4 (not available)
12069         null
12070 
12071         ) add_line4,
12072 
12073       /* payee city */
12074       DECODE(
12075         pmt.address_source,
12076 
12077         -- supplier city
12078         'TCA', payee_loc.city,
12079 
12080         -- employee city
12081         DECODE
12082         (
12083           pmt.employee_address_code,
12084 
12085           -- employee home city
12086           'HOME', per_addr.town_or_city,
12087 
12088           -- employee office city
12089           'OFFICE', per_loc.town_or_city,
12090 
12091           -- employee provisional city
12092           'PROVISIONAL', per_addr_prov.town_or_city,
12093 
12094           -- address code not specified
12095           DECODE (per_addr.address_id,
12096                  NULL, per_loc.town_or_city,
12097                  per_addr.town_or_city)
12098           )
12099 
12100         ) city,
12101 
12102 
12103       /* payee county */
12104       DECODE(
12105         pmt.address_source,
12106 
12107         -- supplier county
12108         'TCA', payee_loc.county,
12109 
12110         -- employee county
12111         (
12112         DECODE(
12113           pmt.employee_address_code,
12114 
12115           -- employee home county
12116           'HOME',
12117           DECODE(
12118             per_addr.style,
12119             'US',     NVL(per_addr.region_1,   ''),
12120             'US_GLB', NVL(per_addr.region_1,   ''),
12121             'IE',     NVL(ap_web_db_expline_pkg.
12122                               getcountyprovince(
12123                                   per_addr.style,
12124                                   per_addr.region_1),
12125                         ''),
12126             'IE_GLB', NVL(ap_web_db_expline_pkg.
12127                               getcountyprovince(
12128                                   per_addr.style,
12129                                   per_addr.region_1),
12130                         ''),
12131             'GB',     NVL(ap_web_db_expline_pkg.
12132                               getcountyprovince(
12133                                   per_addr.style,
12134                                   per_addr.region_1),
12135                         ''),
12136             ''),
12137 
12138           -- employee office county
12139           'OFFICE',
12140           DECODE(
12141             per_loc.style,
12142             'US',      NVL(per_loc.region_1,   ''),
12143             'US_GLB',  NVL(per_loc.region_1,   ''),
12144             'IE',      NVL(ap_web_db_expline_pkg.
12145                                getcountyprovince(
12146                                    per_loc.style,
12147                                    per_loc.region_1),
12148                          ''),
12149             'IE_GLB',  NVL(ap_web_db_expline_pkg.
12150                                getcountyprovince(
12151                                    per_loc.style,
12152                                    per_loc.region_1),
12153                          ''),
12154             'GB',      NVL(ap_web_db_expline_pkg.
12155                                getcountyprovince(
12156                                    per_loc.style,
12157                                    per_loc.region_1),
12158                          ''),
12159             ''),
12160 
12161 
12162           -- employee provisional county
12163           'PROVISIONAL',
12164           DECODE(
12165             per_addr_prov.style,
12166             'US',     NVL(per_addr_prov.region_1,   ''),
12167             'US_GLB', NVL(per_addr_prov.region_1,   ''),
12168             'IE',     NVL(ap_web_db_expline_pkg.
12169                               getcountyprovince(
12170                                   per_addr_prov.style,
12171                                   per_addr_prov.region_1),
12172                         ''),
12173             'IE_GLB', NVL(ap_web_db_expline_pkg.
12174                               getcountyprovince(
12175                                   per_addr_prov.style,
12176                                   per_addr_prov.region_1),
12177                         ''),
12178             'GB',     NVL(ap_web_db_expline_pkg.
12179                               getcountyprovince(
12180                                   per_addr_prov.style,
12181                                   per_addr_prov.region_1),
12182                         ''),
12183             ''),
12184 
12185 
12186             --address code not specified
12187             decode(per_addr.address_id,
12188             NULL,DECODE(
12189             per_loc.style,
12190             'US',      NVL(per_loc.region_1,   ''),
12191             'US_GLB',  NVL(per_loc.region_1,   ''),
12192             'IE',      NVL(ap_web_db_expline_pkg.
12193                                getcountyprovince(
12194                                    per_loc.style,
12195                                    per_loc.region_1),
12196                          ''),
12197             'IE_GLB',  NVL(ap_web_db_expline_pkg.
12198                                getcountyprovince(
12199                                    per_loc.style,
12200                                    per_loc.region_1),
12201                          ''),
12202             'GB',      NVL(ap_web_db_expline_pkg.
12203                                getcountyprovince(
12204                                    per_loc.style,
12205                                    per_loc.region_1),
12206                          ''),
12207             ''),
12208              DECODE(
12209             per_addr.style,
12210             'US',     NVL(per_addr.region_1,   ''),
12211             'US_GLB', NVL(per_addr.region_1,   ''),
12212             'IE',     NVL(ap_web_db_expline_pkg.
12213                               getcountyprovince(
12214                                   per_addr.style,
12215                                   per_addr.region_1),
12216                         ''),
12217             'IE_GLB', NVL(ap_web_db_expline_pkg.
12218                               getcountyprovince(
12219                                   per_addr.style,
12220                                   per_addr.region_1),
12221                         ''),
12222             'GB',     NVL(ap_web_db_expline_pkg.
12223                               getcountyprovince(
12224                                   per_addr.style,
12225                                   per_addr.region_1),
12226                         ''),
12227             ''))
12228             )
12229           )
12230         ) county,
12231 
12232       /* payee province */
12233       DECODE(
12234         pmt.address_source,
12235 
12236         -- supplier province
12237         'TCA', payee_loc.province,
12238 
12239         -- employee province
12240         (
12241         DECODE(
12242 
12243           pmt.employee_address_code,
12244 
12245           -- employee home province
12246           'HOME',
12247           DECODE(per_addr.style,
12248             'US',      '',
12249             'US_GLB',  '',
12250             'IE',      '',
12251             'IE_GLB',  '',
12252             'GB',      '',
12253             'CA',      NVL(per_addr.region_1,   ''),
12254             'CA_GLB',  NVL(per_addr.region_1,   ''),
12255             'JP',      NVL(per_addr.region_1,   ''),
12256             NVL(ap_web_db_expline_pkg.
12257                     getcountyprovince(
12258                         per_addr.style,
12259                         per_addr.region_1),
12260               '')
12261             ),
12262 
12263           -- employee office province
12264           'OFFICE',
12265           DECODE(per_loc.style,
12266             'US',      '',
12267             'US_GLB',  '',
12268             'IE',      '',
12269             'IE_GLB',  '',
12270             'GB',      '',
12271             'CA',      NVL(per_loc.region_1,   ''),
12272             'CA_GLB',  NVL(per_loc.region_1,   ''),
12273             'JP',      NVL(per_loc.region_1,   ''),
12274             NVL(ap_web_db_expline_pkg.
12275                     getcountyprovince(
12276                         per_loc.style,
12277                         per_loc.region_1),
12278               '')
12279               ),
12280 
12281           -- employee provisional province
12282           'PROVISIONAL',
12283           DECODE(per_addr_prov.style,
12284             'US',      '',
12285             'US_GLB',  '',
12286             'IE',      '',
12287             'IE_GLB',  '',
12288             'GB',      '',
12289             'CA',      NVL(per_addr_prov.region_1,   ''),
12290             'CA_GLB',  NVL(per_addr_prov.region_1,   ''),
12291             'JP',      NVL(per_addr_prov.region_1,   ''),
12292             NVL(ap_web_db_expline_pkg.
12293                     getcountyprovince(
12294                         per_addr_prov.style,
12295                         per_addr_prov.region_1),
12296               '')
12297             ),
12298 
12299             --address code not specified
12300             decode(per_addr.address_id,
12301             NULL,DECODE(per_loc.style,
12302             'US',      '',
12303             'US_GLB',  '',
12304             'IE',      '',
12305             'IE_GLB',  '',
12306             'GB',      '',
12307             'CA',      NVL(per_loc.region_1,   ''),
12308             'CA_GLB',  NVL(per_loc.region_1,   ''),
12309             'JP',      NVL(per_loc.region_1,   ''),
12310             NVL(ap_web_db_expline_pkg.
12311                     getcountyprovince(
12312                         per_loc.style,
12313                         per_loc.region_1),
12314               '')
12315               ),
12316              DECODE(per_addr.style,
12317             'US',      '',
12318             'US_GLB',  '',
12319             'IE',      '',
12320             'IE_GLB',  '',
12321             'GB',      '',
12322             'CA',      NVL(per_addr.region_1,   ''),
12323             'CA_GLB',  NVL(per_addr.region_1,   ''),
12324             'JP',      NVL(per_addr.region_1,   ''),
12325             NVL(ap_web_db_expline_pkg.
12326                     getcountyprovince(
12327                         per_addr.style,
12328                         per_addr.region_1),
12329               '')
12330             ))
12331             )
12332           )
12333         ) province,
12334 
12335       /* payee state */
12336       DECODE(
12337         pmt.address_source,
12338 
12339         -- supplier state
12340         'TCA', payee_loc.state,
12341 
12342          -- employee state
12343          (
12344          DECODE(
12345            pmt.employee_address_code,
12346 
12347            -- employee home state
12348            'HOME',
12349            DECODE(per_addr.style,
12350              'CA',     '',
12351              'CA_GLB', '',
12352              NVL(per_addr.region_2,   '')),
12353 
12354            -- employee office state
12355            'OFFICE',
12356            DECODE(per_loc.style,
12357              'CA',     '',
12358              'CA_GLB', '',
12359              NVL(per_loc.region_2, '')),
12360 
12361            -- employee provisional state
12362            'PROVISIONAL',
12363            DECODE(per_addr_prov.style,
12364              'CA',     '',
12365              'CA_GLB', '',
12366              NVL(per_addr_prov.region_2,   '')),
12367            --address code not specified
12368            decode(per_addr.address_id,
12369            NULL,DECODE(per_loc.style,
12370              'CA',     '',
12371              'CA_GLB', '',
12372               NVL(per_loc.region_2, '')),
12373             DECODE(per_addr.style,
12374              'CA',     '',
12375              'CA_GLB', '',
12376               NVL(per_addr.region_2,   '')))
12377            )
12378          )
12379        ) state,
12380 
12381      /* payee country */
12382       DECODE(
12383         pmt.address_source,
12384 
12385         -- supplier country
12386         'TCA', payee_loc.country,
12387 
12388         -- employee country
12389         (
12390         DECODE(
12391           pmt.employee_address_code,
12392 
12393           -- employee home country
12394           'HOME', per_addr.country,
12395 
12396           -- employee office country
12397           'OFFICE',per_loc.country,
12398 
12399           -- employee provisional country
12400           'PROVISIONAL', per_addr_prov.country,
12401 
12402           --address code not specified
12403           DECODE (per_addr.address_id,
12404                NULL, per_loc.country,
12405                per_addr.country
12406                )
12407           )
12408         )
12409         ) country,
12410 
12411       /* payee postal code */
12412       DECODE(
12413         pmt.address_source,
12414 
12415         -- supplier postal code
12416         'TCA', payee_loc.postal_code,
12417 
12418         -- employee postal code
12419         (
12420         DECODE(
12421           pmt.employee_address_code,
12422 
12423           -- employee home postal code
12424           'HOME', per_addr.postal_code,
12425 
12426           -- employee office postal code
12427           'OFFICE',per_loc.postal_code,
12428 
12429           -- employee provisional postal code
12430           'PROVISIONAL', per_addr_prov.postal_code,
12431 
12432           --address code not specified
12433           DECODE (per_addr.address_id,
12434                NULL, per_loc.postal_code,
12435                per_addr.postal_code
12436                )
12437           )
12438         )
12439         ) postal_code,
12440 
12441 
12442       /* payee address concat */
12443       DECODE(
12444         pmt.address_source,
12445 
12446         -- supplier address concat
12447         'TCA',
12448         payee_loc.address1
12449           || ', '
12450           || payee_loc.address2
12451           || ', '
12452           || payee_loc.address3
12453           || ', '
12454           || payee_loc.city
12455           || ', '
12456           || payee_loc.state
12457           || ', '
12458           || payee_loc.country
12459           || ', '
12460           || payee_loc.postal_code,
12461 
12462         -- employee address concat
12463         (
12464         DECODE(
12465           pmt.employee_address_code,
12466 
12467           -- employee home address concat
12468           'HOME',
12469           per_addr.address_line1
12470             || ', '
12471             || per_addr.address_line2
12472             || ', '
12473             || per_addr.address_line3
12474             || ', '
12475             || per_addr.town_or_city
12476             || ', '
12477             || DECODE(
12478                  per_addr.style,
12479                  'CA',     '',
12480                  'CA_GLB', '',
12481                  NVL(per_addr.region_2, '')
12482                  )
12483             || ', '
12484             || per_addr.country
12485             || ', '
12486             || per_addr.postal_code,
12487 
12488           -- employee office address concat
12489           'OFFICE',
12490           per_loc.address_line_1
12491             || ', '
12492             || per_loc.address_line_2
12493             || ', '
12494             || per_loc.address_line_3
12495             || ', '
12496             || per_loc.town_or_city
12497             || ', '
12498             || DECODE(
12499                  per_loc.style,
12500                  'CA',     '',
12501                  'CA_GLB', '',
12502                  NVL(per_loc.region_2, '')
12503                  )
12504             || ', '
12505             || per_loc.country
12506             || ', '
12507             || per_loc.postal_code,
12508 
12509           -- employee provisional address concat
12510           'PROVISIONAL',
12511           per_addr_prov.address_line1
12512             || ', '
12513             || per_addr_prov.address_line2
12514             || ', '
12515             || per_addr_prov.address_line3
12516             || ', '
12517             || per_addr_prov.town_or_city
12518             || ', '
12519             || DECODE(
12520                  per_addr_prov.style,
12521                  'CA',     '',
12522                  'CA_GLB', '',
12523                  NVL(per_addr_prov.region_2, '')
12524                  )
12525             || ', '
12526             || per_addr_prov.country
12527             || ', '
12528             || per_addr_prov.postal_code,
12529 
12530           -- address code not specified
12531           DECODE (per_addr.address_id,
12532                NULL, per_loc.address_line_1
12533             || ', '
12534             || per_loc.address_line_2
12535             || ', '
12536             || per_loc.address_line_3
12537             || ', '
12538             || per_loc.town_or_city
12539             || ', '
12540             || DECODE(
12541                  per_loc.style,
12542                  'CA',     '',
12543                  'CA_GLB', '',
12544                  NVL(per_loc.region_2, '')
12545                  )
12546             || ', '
12547             || per_loc.country
12548             || ', '
12549             || per_loc.postal_code,
12550 
12551               per_addr.address_line1
12552             || ', '
12553             || per_addr.address_line2
12554             || ', '
12555             || per_addr.address_line3
12556             || ', '
12557             || per_addr.town_or_city
12558             || ', '
12559             || DECODE(
12560                  per_addr.style,
12561                  'CA',     '',
12562                  'CA_GLB', '',
12563                  NVL(per_addr.region_2, '')
12564                  )
12565             || ', '
12566             || per_addr.country
12567             || ', '
12568             || per_addr.postal_code)
12569           )
12570         )
12571       ) add_concat
12572 
12573   FROM
12574       IBY_PAYMENTS_ALL         pmt,
12575 
12576       /* Employee address related */
12577       HR_LOCATIONS             per_loc,
12578       PER_ADDRESSES            per_addr,
12579       PER_ALL_ASSIGNMENTS_F    per_assgn,
12580       PER_ADDRESSES            per_addr_prov,
12581 
12582       /* Supplier address related */
12583       HZ_LOCATIONS             payee_loc
12584   WHERE
12585     pmt.payment_id                     = p_payment_id
12586     AND pmt.employee_person_id         = per_addr.person_id(+)
12587     AND per_addr.primary_flag(+) = 'Y'
12588     AND SYSDATE BETWEEN
12589             per_addr.date_from(+)
12590             AND NVL(per_addr.date_to(+), SYSDATE+1)
12591     AND pmt.employee_person_id         = per_addr_prov.person_id(+)
12592     AND per_addr_prov.address_type(+) = 'M'
12593     AND SYSDATE BETWEEN
12594             per_addr_prov.date_from(+)
12595             AND NVL(per_addr_prov.date_to(+), SYSDATE+1)
12596     AND pmt.employee_person_id         = per_assgn.person_id(+)
12597     AND per_assgn.location_id          = per_loc.location_id(+)
12598     AND per_assgn.primary_flag(+)      = 'Y'
12599     AND (TRUNC(SYSDATE) BETWEEN
12600             per_assgn.effective_start_date(+)
12601             AND per_assgn.effective_end_date(+)
12602         )
12603     AND pmt.remit_to_location_id       = payee_loc.location_id(+)
12604   ) payee_addr
12605 
12606  WHERE
12607 
12608   /* payment related */
12609   pmt.payment_id                     = p_payment_id
12610 
12611   /* payer */
12612   AND pmt.legal_entity_id            = payer.legal_entity_id
12613   AND payer.party_id                 = payer_party.party_id
12614 
12615   /* payer bank */
12616   AND pmt.internal_bank_account_id   = iba.bank_account_id
12617   AND iba_bnk_branch.branch_party_id = iba.bank_branch_id
12618 
12619   /* payer bank location */
12620   AND iba_bnk_branch.branch_party_id = iba_branch_party_site.party_id(+)
12621 
12622   /* payee */
12623   AND pmt.payee_party_id             = payee.party_id
12624 
12625   /* payee site */
12626   AND pmt.party_site_id              = payee_site.party_site_id(+)
12627 
12628   /* inv payee */
12629   AND pmt.inv_payee_party_id             = invpayee.party_id(+)
12630 
12631   /* inv payee site */
12632   AND pmt.inv_party_site_id              = invpayee_site.party_site_id(+)
12633 
12634   /* vendor related */
12635   AND pmt.payee_party_id             = vendor.party_id(+)
12636   AND pmt.supplier_site_id           = vendor_site.vendor_site_id(+)
12637   AND vendor.vendor_id               = vendor_site.vendor_id       /*Bug 7323072*/
12638 
12639 
12640   /* inv vendor related */
12641   AND pmt.inv_payee_party_id             = invvendor.party_id(+)
12642   AND pmt.inv_supplier_site_id           = invvendor_site.vendor_site_id(+)
12643   AND nvl(vendor.vendor_id,-99)      = nvl(vendor_site.vendor_id,-99)      /*Bug 7323072*/
12644 
12645   /* employee related */
12646   AND pmt.employee_person_id         = per.person_id(+)
12647 
12648   /* payee bank */
12649   AND pmt.external_bank_account_id   = eba.ext_bank_account_id(+)
12650   AND eba.bank_party_id              = eba_bank_branch.bank_party_id(+)
12651   AND eba.branch_party_id            = eba_bank_branch.branch_party_id(+)
12652 
12653   /*
12654    * Fix for bug 5658982:
12655    *
12656    * We want to use outer join with the eba table because the
12657    * ext bank account id is optional on the payment.
12658    *
12659    * For the same reason, we must use outer join with
12660    * ext_ba_table table also.
12661    */
12662   AND ext_ba_table.ext_bank_account_id(+) = eba.ext_bank_account_id
12663 
12664   /* profile and remit advice */
12665   AND pmt.payment_profile_id         = prof.payment_profile_id
12666   AND prof.system_profile_code       = remit_advice.system_profile_code
12667 
12668   /* payment service request related */
12669   AND payreq.payment_service_request_id  = pmt.payment_service_request_id
12670   AND fnd_app.application_id         = payreq.calling_app_id
12671 
12672   /* delivery channel related */
12673   AND pmt.delivery_channel_code      = deliv.delivery_channel_code(+)
12674 
12675   /* payment reason */
12676   AND pmt.payment_reason_code        = pmt_reason.payment_reason_code(+)
12677 
12678   /* bank instruction */
12679   AND prof.bank_instruction1_code    = bank_instr1.bank_instruction_code(+)
12680   AND prof.bank_instruction2_code    = bank_instr2.bank_instruction_code(+)
12681 
12682   /* Org related */
12683   AND pmt.org_id                     = org.organization_id
12684 
12685   /* RFC related */
12686   AND rfc_ca.owner_table_name(+)     = 'HZ_PARTIES'
12687   AND rfc_ca.class_category(+)       = 'RFC_IDENTIFIER'
12688   AND rfc_ca.owner_table_id(+)       = iba.bank_branch_id
12689   ;
12690 
12691 /* cursor added for the bug:7183572*/
12692  CURSOR c_ce_audit_columns (p_payment_id  NUMBER)
12693  IS
12694  SELECT
12695 
12696   /* PAYMENT RELATED */
12697   pmt.payment_id,                                                    -- 01
12698 
12699   /* PAYER */
12700   payer_party.party_number,            -- payer party number
12701   '',                                  -- payer party site name
12702   payer.name,                          -- payer name
12703   IBY_FD_EXTRACT_GEN_PVT.              -- payer tax reg number
12704       Get_FP_TaxRegistration
12705           (
12706           payer.legal_entity_id
12707           ),
12708   payer.registration_number,           -- payer le reg number
12709   payer.party_id,                      -- payer_party_id
12710   payer.location_id,                   -- payer_location_id
12711   payer_party.attribute_category,      -- payer party attr category
12712   payer.attribute_category,            -- payer LE attr category     -- 10
12713 
12714   /* PAYER SPECIAL FIELDS */
12715   '',                                  -- payer abbrev agency code
12716   '',                                  -- payer federal us employer id
12717 
12718   /* PAYER BANK */
12719   iba_bnk_branch.bank_name,            -- payer bank name
12720   iba_bnk_branch.bank_number,          -- payer bank number
12721   iba_bnk_branch.branch_number,        -- payer bank branch number
12722   iba_bnk_branch.bank_branch_name,     -- payer bank branch name
12723   iba_bnk_branch.eft_swift_code,       -- payer bank swift code
12724   iba.bank_account_num,                -- payer bank account num
12725   iba.bank_account_name,               -- payer bank account name
12726   iba.iban_number,                     -- payer bank acct iban number -- 20
12727   iba.agency_location_code,            -- payer bank agency location code
12728   iba_bnk_branch.branch_party_id,      -- payer bank branch party id
12729   iba_bnk_branch.bank_name_alt,        -- payer bank alt name
12730   iba_bnk_branch.bank_branch_name_alt, -- payer bank branch alt name
12731   iba.bank_account_name_alt,           -- payer bank alt account name
12732   iba.bank_account_num_electronic,     -- payer bank account num electronic
12733   iba_branch_party_site.location_id,   -- payer bank branch location id
12734   iba_bnk_branch.eft_user_number,      -- payer bank branch eft user num
12735 
12736   /* PAYEE */
12737   payee.party_number,                  -- payee party number
12738 
12739   /* Fix for bug 7391524
12740    * Full Name when Payee is an Employee is fetch from HR tables
12741    * This is just as a temporary fix, until TCA overcomes it architecture limitations
12742    */
12743   DECODE(pmt.employee_person_id,
12744    NULL,payee.party_name,
12745    Get_Employee_Full_Name(pmt.employee_person_id,pmt.payee_party_id,payee.party_name)) payee_party_name,  -- payee party name
12746 
12747   /*
12748    * Fix for bug 5466979:
12749    *
12750    * Payee name is always the same as the payee party name.
12751    */
12752   DECODE(pmt.employee_person_id,
12753    NULL,payee.party_name,
12754    Get_Employee_Full_Name(pmt.employee_person_id,pmt.payee_party_id,payee.party_name)) payee_name,  --payee name
12755 
12756   DECODE(                              -- payee alternate name
12757         payee.party_type,
12758         'ORGANIZATION',
12759             payee.organization_name_phonetic,
12760         'PERSON',
12761             TRIM(payee.person_first_name_phonetic
12762                 || ' '
12763                 || payee.person_last_name_phonetic)
12764         ),
12765 
12766   /* PAYEE ADDRESS */
12767   payee_addr.add_line1,                -- payee add line1
12768   payee_addr.add_line2,                -- payee add line2
12769   payee_addr.add_line3,                -- payee add line3
12770   payee_addr.add_line4,                -- payee add line4
12771   payee_addr.city,                     -- payee city
12772   payee_addr.county,                   -- payee county
12773   payee_addr.province,                 -- payee province
12774   payee_addr.state,                    -- payee state                -- 40
12775   payee_addr.country,                  -- payee country
12776   payee_addr.postal_code,              -- payee postal code
12777   payee_addr.add_concat,               -- payee address concat
12778 
12779   /*
12780    * Fix for bug 5466979:
12781    *
12782    * Beneficiary name should be the same as the payee party name
12783    * except when beneficiary party is not null.
12784    *
12785    * If beneficiary party is not null, the beneficiary name
12786    * should be the hz_parties.party_name corresponding to the
12787    * beneficiary_party.
12788    */
12789   DECODE (pmt.beneficiary_party,
12790               NULL,
12791               payee.party_name,
12792               (SELECT
12793                    benef_party.party_name
12794                FROM
12795                    HZ_PARTIES benef_party
12796                WHERE
12797                    benef_party.party_id=pmt.beneficiary_party
12798               )
12799            ),                          -- beneficiary name
12800 
12801   payee.attribute_category,            -- payee party attr category
12802   payee_site.attribute_category,       -- payee supplier site attr category
12803   payee_site.party_site_name,          -- payee supplier site name
12804 
12805   /* VENDOR RELATED */
12806   vendor_site.vendor_site_code_alt,    -- payee site alternate name
12807   vendor.segment1,                     -- payee supplier number
12808   vendor.customer_num,                 -- payee first party reference  -- 50
12809   vendor.attribute_category,           -- payee supplier attr category
12810   vendor.vendor_id,                    -- payee supplier id
12811 
12812   /* PAYEE SPECIAL FIELDS */
12813   IBY_FD_EXTRACT_GEN_PVT.              -- payee tax registration number
12814        Get_Payee_TaxRegistration
12815       (vendor.vendor_id,pmt.supplier_site_id),
12816 
12817   /*
12818    * Fix for bug 5468251:
12819    *
12820    * Use AP_SUPPLIERS.NUM_1099 for the payee
12821    * LE registration number.
12822    */
12823 
12824   /*
12825    * Fix for bug 5475920:
12826    *
12827    * Payments can be made for employees or suppliers.
12828    *
12829    * For employees, the payee le reg num (i.e., taxpayer id)
12830    * is stored in PER_PEOPLE_X.
12831    *
12832    * For suppliers, the payee le reg num (i.e., taxpayer id)
12833    * is stored in AP_SUPPLIERS.
12834    *
12835    * Switch between these two tables depending upon payee type
12836    * to get the payee le reg num
12837    */
12838    /*
12839    * Fix for bug 5501968:
12840    *
12841    * In R12, Federal supports thid party payments i.e., payments
12842    * to third parties who may not be employees / suppliers.
12843    *
12844    * For this reason, we cannot assume that tax payer id for
12845    * all non-employees will be stored in AP_SUPPLIERS.
12846    *
12847    * However, we know that all third parties / suppliers will
12848    * have a row in HZ_PARTIES. Therefore, pick up the taxpayer
12849    * id from HZ_PARTIES as this always works.
12850    */
12851    /*
12852    * Fix for bug 5501968:
12853    *
12854    * In R12, Federal supports thid party payments i.e., payments
12855    */
12856    decode(upper(vendor.vendor_type_lookup_code),
12857           'EMPLOYEE', per.national_identifier,
12858           'CONTRACTOR',decode(vendor.organization_type_lookup_code,
12859                               'INDIVIDUAL',vendor.individual_1099,
12860                               'FOREIGN INDIVIDUAL',vendor.individual_1099,
12861    		              'PARTNERSHIP',vendor.individual_1099,
12862                               'FOREIGN PARTNERSHIP',vendor.individual_1099,
12863                               vendor.num_1099),
12864  	  vendor.num_1099),            -- payee Tax Identification Number
12865 
12866   /* PAYEE BANK */
12867   eba.bank_name,                       -- payee bank name
12868   eba.bank_number,                     -- payee bank number
12869   eba.branch_number,                   -- payee bank branch number
12870   eba.bank_branch_name,                -- payee bank branch name
12871 
12872   /*
12873    * Fix for bug 5586882:
12874    *
12875    * Populate the external bank account using
12876    * IBY_EXT_BANK_ACCOUNTS.bank_account_num
12877    * so that we show the unmasked bank account number
12878    */
12879   ext_ba_table.bank_account_num,       -- payee bank account number
12880 
12881   eba.bank_account_name,               -- payee bank account name    -- 60
12882   eba.iban_number,                     -- payee bank account IBAN
12883   eba.eft_swift_code,                  -- payee bank swift code
12884   eba.bank_account_type,               -- payee bank account type
12885   eba.payment_factor_flag,             -- payee bank payment factor flag
12886   eba.primary_acct_owner_party_id,     -- payee bank owner party id
12887   eba.branch_party_id,                 -- payee bank branch party id
12888   eba_bank_branch.bank_name_alt,       -- payee bank name alt
12889   eba_bank_branch.bank_branch_name_alt, -- payee bank branch name alt
12890   eba.alternate_account_name,          -- payee bank alt account name
12891   eba.bank_account_num_electronic,     -- payee bank electronic acct num -- 70
12892   eba.bank_branch_address_id,          -- payee bank branch location id
12893   eba.primary_acct_owner_name,         -- payee bank primary acct owner name
12894 
12895   /* REMITTANCE ADVICE RELATED */
12896   remit_advice.remit_advice_delivery_method,  -- remittance advice delv method
12897   '',                                  -- remittance advice email
12898   '',                                  -- remittance advice fax
12899 
12900   /* DELIVERY CHANNEL RELATED */
12901   deliv.format_value,
12902 
12903   /* DECLARATION REPORT RELATED */
12904   prof.declaration_curr_fx_rate_type,
12905   '',                                  -- declaration format
12906 
12907   /* PROFILE RELATED */
12908   prof.payment_profile_name,           -- account profile name       -- 80
12909   prof.system_profile_name,            -- system profile name
12910 
12911   /* PAYMENT REASON */
12912   pmt_reason.format_value,
12913 
12914   /* BANK INSTRUCTION */
12915   bank_instr1.format_value,
12916   bank_instr2.format_value,
12917 
12918   /* LE */
12919   le.name,
12920 
12921   /* RFC */
12922   rfc_ca.class_code,
12923 
12924   /* REQUEST */
12925   payreq.call_app_pay_service_req_code,-- ppr name
12926   fnd_app.application_name,             -- source product            -- 88
12927 
12928 
12929 
12930       /*TPP-Start*/
12931 invpayee.party_name 		inv_payee_name,
12932 null		inv_payee_address1,
12933 null		inv_payee_address2,
12934 null		inv_payee_address3,
12935 null		inv_payee_address4,
12936 null		inv_payee_city,
12937 null		inv_payee_postal_code,
12938 null		inv_payee_state,
12939 null		inv_payee_province,
12940 null		inv_payee_county,
12941 null		inv_payee_country,
12942 invpayee.party_name 		inv_payee_party_name,
12943 
12944 decode(upper(invvendor.vendor_type_lookup_code),
12945           'CONTRACTOR',decode(invvendor.organization_type_lookup_code,
12946                               'INDIVIDUAL',invvendor.individual_1099,
12947                               'FOREIGN INDIVIDUAL',invvendor.individual_1099,
12948    		              'PARTNERSHIP',invvendor.individual_1099,
12949                               'FOREIGN PARTNERSHIP',invvendor.individual_1099,
12950                               invvendor.num_1099),
12951  	  invvendor.num_1099)	inv_payee_le_reg_num,
12952 
12953 IBY_FD_EXTRACT_GEN_PVT.
12954  Get_Payee_TaxRegistration (invvendor.vendor_id,pmt.inv_supplier_site_id)
12955 				inv_payee_tax_reg_num,
12956 
12957 null 	inv_payee_address_concat,
12958 DECODE (pmt.inv_beneficiary_party,
12959               NULL,
12960               invpayee.party_name,
12961               (SELECT
12962                    benef_party.party_name
12963                FROM
12964                    HZ_PARTIES benef_party
12965                WHERE
12966                    benef_party.party_id=pmt.inv_beneficiary_party
12967               )
12968            ) 			inv_beneficiary_name,
12969 
12970 invpayee.party_number 		inv_payee_party_number,
12971 invpayee.organization_name_phonetic	inv_payee_alternate_name,
12972 invvendor_site.vendor_site_code_alt	inv_payee_site_alt_name,
12973 invvendor.segment1			inv_payee_supplier_number,
12974 invvendor.customer_num			inv_payee_first_party_ref,
12975 
12976 null ext_bnk_acct_ownr_inv_prty_id,
12977 null ext_bnk_branch_inv_prty_id,
12978 null ext_bnk_acct_ownr_inv_prty_nme,
12979 
12980 invpayee.attribute_category		inv_payee_party_attr_cat,
12981 invvendor.attribute_category		inv_payee_supplier_attr_cat,
12982 invpayee_site.attribute_category	inv_payee_spplr_site_attr_cat,
12983 invpayee_site.party_site_name		inv_payee_supplier_site_name,
12984 invvendor_site.vendor_site_code_alt	inv_payee_spplr_site_alt_name,
12985 invvendor.vendor_id			inv_payee_supplier_id
12986       /*TPP-End*/
12987 
12988  FROM
12989 
12990   /* Payment related */
12991   IBY_PAYMENTS_ALL         pmt,
12992 
12993   /* Profile related */
12994   IBY_PAYMENT_PROFILES     prof,
12995 
12996   /* Payer */
12997   XLE_FIRSTPARTY_INFORMATION_V  payer,
12998   HZ_PARTIES               payer_party,
12999 
13000   /* Payer bank */
13001   CE_BANK_ACCOUNTS         iba,
13002   CE_BANK_BRANCHES_V       iba_bnk_branch,
13003   HZ_PARTY_SITES           iba_branch_party_site,
13004 
13005   /* Payee */
13006   HZ_PARTIES               payee,
13007   HZ_PARTIES               invpayee,
13008 
13009   /* Vendor related */
13010   AP_SUPPLIERS               vendor,
13011   AP_SUPPLIER_SITES_ALL      vendor_site,
13012 
13013   /* Inv Vendor related */
13014   AP_SUPPLIERS               invvendor,
13015   AP_SUPPLIER_SITES_ALL      invvendor_site,
13016 
13017   /* Employee related */
13018   PER_PEOPLE_X             per,
13019 
13020   /* Payee site */
13021   HZ_PARTY_SITES           payee_site,
13022   HZ_PARTY_SITES           invpayee_site,
13023 
13024   /* Payee bank */
13025   IBY_EXT_BANK_ACCOUNTS_V  eba,
13026   IBY_EXT_BANK_ACCOUNTS    ext_ba_table,
13027   CE_BANK_BRANCHES_V       eba_bank_branch,
13028 
13029   /* Remit advice related */
13030   IBY_REMIT_ADVICE_SETUP   remit_advice,
13031 
13032   /* Payment service request related */
13033   IBY_PAY_SERVICE_REQUESTS payreq,
13034   FND_APPLICATION_ALL_VIEW fnd_app,
13035 
13036   /* Delivery channel related */
13037   IBY_DELIVERY_CHANNELS_VL deliv,
13038 
13039   /* Payment reason related */
13040   IBY_PAYMENT_REASONS_VL   pmt_reason,
13041 
13042   /* Bank instruction related */
13043   IBY_BANK_INSTRUCTIONS_VL bank_instr1,
13044   IBY_BANK_INSTRUCTIONS_VL bank_instr2,
13045 
13046   /* Legal Entity related */
13047   xle_entity_profiles le,
13048 
13049   /* RFC */
13050   HZ_CODE_ASSIGNMENTS          rfc_ca,
13051 
13052   /*
13053    * Fix for bug 5928084:
13054    *
13055    * Derive address of employee type payees using
13056    * PER_ADDRESSES or HR_LOCATIONS.
13057    *
13058    * The select statement below dynamically creates the payee
13059    * address table. The dynamically created table is named
13060    * as payee_add. Columns from this table are referenced in the
13061    * outer select.
13062    *
13063    * The payee address can be either a
13064    *  - supplier address, or
13065    *  - employee address
13066    *
13067    * We decide whether we should pick up supplier addresses or
13068    * employee addresses using the 'address source' field on the
13069    * payment.
13070    *
13071    * 'address source' can contain two values
13072    *  - TCA: supplier address
13073    *  - HR : employee address
13074    *
13075    * Supplier addresses are present in TCA and are located using
13076    * remit to location id column on the payment.
13077    *
13078    * Employee addresses can be in either
13079    *   - PER_ADDRESSES (home addresses)
13080    *   - HR_LOCATIONS  (office addresses)
13081    * We use the 'employee address code' field on the payment to
13082    * decide which table to use.
13083    *
13084    * The select below uses decode clause to perform the
13085    * if else logic to retrieve the address from the right
13086    * column.
13087    *
13088    * Roughly, the decode clauses are used to effect the following
13089    * logic -
13090    *
13091    * for each add field:
13092    * if (pmt.address_source = 'TCA')
13093    * {
13094    *   -- get the address field from HZ_LOCATIONS --
13095    * }
13096    * else
13097    * {
13098    *     -- this is an employee address --
13099    *     if (pmt.employee_address_code = 'HOME')
13100    *     {
13101    *         -- get the address field from PER_ADDRESSES --
13102    *
13103    *         The select logic for this has been lifted from
13104    *         AP's expense report import program.
13105    *     }
13106    *     else
13107    *     {
13108    *         -- get the address field from HR_LOCATIONS --
13109    *
13110    *         The select logic for this has been lifted from
13111    *         <APDEV>/patch/115/sql/apwexptb.pls.
13112    *     }
13113    * }
13114    */
13115   (
13116      SELECT
13117 
13118       /* payee add line1 */
13119       DECODE(
13120         pmt.address_source,
13121 
13122         -- supplier address line 1
13123         'TCA', payee_loc.address1,
13124 
13125         -- employee add line 1
13126         DECODE
13127         (
13128           pmt.employee_address_code,
13129 
13130           -- employee home addr line 1
13131           'HOME', per_addr.address_line1,
13132 
13133           -- employee office addr line 1
13134           'OFFICE',per_loc.address_line_1,
13135 
13136           -- employee provisional addr line 1
13137           'PROVISIONAL', per_addr_prov.address_line1,
13138 
13139           --address code not specified
13140           DECODE (per_addr.address_id,
13141                  NULL, per_loc.address_line_1,
13142                  per_addr.address_line1)
13143          )
13144 
13145         ) add_line1,
13146 
13147       /* payee add line2 */
13148       DECODE(
13149         pmt.address_source,
13150 
13151         -- supplier address line 2
13152         'TCA', payee_loc.address2,
13153 
13154         -- employee add line 2
13155         DECODE
13156         (
13157           pmt.employee_address_code,
13158 
13159           -- employee home addr line 2
13160           'HOME', per_addr.address_line2,
13161 
13162           -- employee office addr line 2
13163           'OFFICE',per_loc.address_line_2,
13164 
13165           -- employee provisional addr line 2
13166           'PROVISIONAL', per_addr_prov.address_line2,
13167 
13168           --address code not specified
13169           DECODE (per_addr.address_id,
13170                  NULL, per_loc.address_line_2,
13171                  per_addr.address_line2)
13172          )
13173         ) add_line2,
13174 
13175 
13176       /* payee add line3 */
13177       DECODE(
13178         pmt.address_source,
13179 
13180         -- supplier address line 3
13181         'TCA', payee_loc.address3,
13182 
13183         -- employee add line 3
13184         DECODE
13185         (
13186           pmt.employee_address_code,
13187 
13188           -- employee home addr line 3
13189           'HOME', per_addr.address_line3,
13190 
13191           -- employee office addr line 3
13192           'OFFICE',per_loc.address_line_3,
13193 
13194           -- employee provisional addr line 3
13195           'PROVISIONAL', per_addr_prov.address_line3,
13196 
13197           --address code not specified
13198           DECODE (per_addr.address_id,
13199                  NULL, per_loc.address_line_3,
13200                  per_addr.address_line3)
13201          )
13202 
13203         ) add_line3,
13204 
13205 
13206       /* payee add line4 */
13207       DECODE(
13208         pmt.address_source,
13209 
13210         -- supplier address line 4
13211         'TCA', payee_loc.address4,
13212 
13213         -- employee home/office addr line 4 (not available)
13214         null
13215 
13216         ) add_line4,
13217 
13218       /* payee city */
13219       DECODE(
13220         pmt.address_source,
13221 
13222         -- supplier city
13223         'TCA', payee_loc.city,
13224 
13225         -- employee city
13226         DECODE
13227         (
13228           pmt.employee_address_code,
13229 
13230           -- employee home city
13231           'HOME', per_addr.town_or_city,
13232 
13233           -- employee office city
13234           'OFFICE', per_loc.town_or_city,
13235 
13236           -- employee provisional city
13237           'PROVISIONAL', per_addr_prov.town_or_city,
13238 
13239           -- address code not specified
13240           DECODE (per_addr.address_id,
13241                  NULL, per_loc.town_or_city,
13242                  per_addr.town_or_city)
13243           )
13244 
13245         ) city,
13246 
13247 
13248       /* payee county */
13249       DECODE(
13250         pmt.address_source,
13251 
13252         -- supplier county
13253         'TCA', payee_loc.county,
13254 
13255         -- employee county
13256         (
13257         DECODE(
13258           pmt.employee_address_code,
13259 
13260           -- employee home county
13261           'HOME',
13262           DECODE(
13263             per_addr.style,
13264             'US',     NVL(per_addr.region_1,   ''),
13265             'US_GLB', NVL(per_addr.region_1,   ''),
13266             'IE',     NVL(ap_web_db_expline_pkg.
13267                               getcountyprovince(
13268                                   per_addr.style,
13269                                   per_addr.region_1),
13270                         ''),
13271             'IE_GLB', NVL(ap_web_db_expline_pkg.
13272                               getcountyprovince(
13273                                   per_addr.style,
13274                                   per_addr.region_1),
13275                         ''),
13276             'GB',     NVL(ap_web_db_expline_pkg.
13277                               getcountyprovince(
13278                                   per_addr.style,
13279                                   per_addr.region_1),
13280                         ''),
13281             ''),
13282 
13283           -- employee office county
13284           'OFFICE',
13285           DECODE(
13286             per_loc.style,
13287             'US',      NVL(per_loc.region_1,   ''),
13288             'US_GLB',  NVL(per_loc.region_1,   ''),
13289             'IE',      NVL(ap_web_db_expline_pkg.
13290                                getcountyprovince(
13291                                    per_loc.style,
13292                                    per_loc.region_1),
13293                          ''),
13294             'IE_GLB',  NVL(ap_web_db_expline_pkg.
13295                                getcountyprovince(
13296                                    per_loc.style,
13297                                    per_loc.region_1),
13298                          ''),
13299             'GB',      NVL(ap_web_db_expline_pkg.
13300                                getcountyprovince(
13301                                    per_loc.style,
13302                                    per_loc.region_1),
13303                          ''),
13304             ''),
13305 
13306 
13307           -- employee provisional county
13308           'PROVISIONAL',
13309           DECODE(
13310             per_addr_prov.style,
13311             'US',     NVL(per_addr_prov.region_1,   ''),
13312             'US_GLB', NVL(per_addr_prov.region_1,   ''),
13313             'IE',     NVL(ap_web_db_expline_pkg.
13314                               getcountyprovince(
13315                                   per_addr_prov.style,
13316                                   per_addr_prov.region_1),
13317                         ''),
13318             'IE_GLB', NVL(ap_web_db_expline_pkg.
13319                               getcountyprovince(
13320                                   per_addr_prov.style,
13321                                   per_addr_prov.region_1),
13322                         ''),
13323             'GB',     NVL(ap_web_db_expline_pkg.
13324                               getcountyprovince(
13325                                   per_addr_prov.style,
13326                                   per_addr_prov.region_1),
13327                         ''),
13328             ''),
13329 
13330 
13331             --address code not specified
13332             decode(per_addr.address_id,
13333             NULL,DECODE(
13334             per_loc.style,
13335             'US',      NVL(per_loc.region_1,   ''),
13336             'US_GLB',  NVL(per_loc.region_1,   ''),
13337             'IE',      NVL(ap_web_db_expline_pkg.
13338                                getcountyprovince(
13339                                    per_loc.style,
13340                                    per_loc.region_1),
13341                          ''),
13342             'IE_GLB',  NVL(ap_web_db_expline_pkg.
13343                                getcountyprovince(
13344                                    per_loc.style,
13345                                    per_loc.region_1),
13346                          ''),
13347             'GB',      NVL(ap_web_db_expline_pkg.
13348                                getcountyprovince(
13349                                    per_loc.style,
13350                                    per_loc.region_1),
13351                          ''),
13352             ''),
13353              DECODE(
13354             per_addr.style,
13355             'US',     NVL(per_addr.region_1,   ''),
13356             'US_GLB', NVL(per_addr.region_1,   ''),
13357             'IE',     NVL(ap_web_db_expline_pkg.
13358                               getcountyprovince(
13359                                   per_addr.style,
13360                                   per_addr.region_1),
13361                         ''),
13362             'IE_GLB', NVL(ap_web_db_expline_pkg.
13363                               getcountyprovince(
13364                                   per_addr.style,
13365                                   per_addr.region_1),
13366                         ''),
13367             'GB',     NVL(ap_web_db_expline_pkg.
13368                               getcountyprovince(
13369                                   per_addr.style,
13370                                   per_addr.region_1),
13371                         ''),
13372             ''))
13373             )
13374           )
13375         ) county,
13376 
13377       /* payee province */
13378       DECODE(
13379         pmt.address_source,
13380 
13381         -- supplier province
13382         'TCA', payee_loc.province,
13383 
13384         -- employee province
13385         (
13386         DECODE(
13387 
13388           pmt.employee_address_code,
13389 
13390           -- employee home province
13391           'HOME',
13392           DECODE(per_addr.style,
13393             'US',      '',
13394             'US_GLB',  '',
13395             'IE',      '',
13396             'IE_GLB',  '',
13397             'GB',      '',
13398             'CA',      NVL(per_addr.region_1,   ''),
13399             'CA_GLB',  NVL(per_addr.region_1,   ''),
13400             'JP',      NVL(per_addr.region_1,   ''),
13401             NVL(ap_web_db_expline_pkg.
13402                     getcountyprovince(
13403                         per_addr.style,
13404                         per_addr.region_1),
13405               '')
13406             ),
13407 
13408           -- employee office province
13409           'OFFICE',
13410           DECODE(per_loc.style,
13411             'US',      '',
13412             'US_GLB',  '',
13413             'IE',      '',
13414             'IE_GLB',  '',
13415             'GB',      '',
13416             'CA',      NVL(per_loc.region_1,   ''),
13417             'CA_GLB',  NVL(per_loc.region_1,   ''),
13418             'JP',      NVL(per_loc.region_1,   ''),
13419             NVL(ap_web_db_expline_pkg.
13420                     getcountyprovince(
13421                         per_loc.style,
13422                         per_loc.region_1),
13423               '')
13424               ),
13425 
13426           -- employee provisional province
13427           'PROVISIONAL',
13428           DECODE(per_addr_prov.style,
13429             'US',      '',
13430             'US_GLB',  '',
13431             'IE',      '',
13432             'IE_GLB',  '',
13433             'GB',      '',
13434             'CA',      NVL(per_addr_prov.region_1,   ''),
13435             'CA_GLB',  NVL(per_addr_prov.region_1,   ''),
13436             'JP',      NVL(per_addr_prov.region_1,   ''),
13437             NVL(ap_web_db_expline_pkg.
13438                     getcountyprovince(
13439                         per_addr_prov.style,
13440                         per_addr_prov.region_1),
13441               '')
13442             ),
13443 
13444             --address code not specified
13445             decode(per_addr.address_id,
13446             NULL,DECODE(per_loc.style,
13447             'US',      '',
13448             'US_GLB',  '',
13449             'IE',      '',
13450             'IE_GLB',  '',
13451             'GB',      '',
13452             'CA',      NVL(per_loc.region_1,   ''),
13453             'CA_GLB',  NVL(per_loc.region_1,   ''),
13454             'JP',      NVL(per_loc.region_1,   ''),
13455             NVL(ap_web_db_expline_pkg.
13456                     getcountyprovince(
13457                         per_loc.style,
13458                         per_loc.region_1),
13459               '')
13460               ),
13461              DECODE(per_addr.style,
13462             'US',      '',
13463             'US_GLB',  '',
13464             'IE',      '',
13465             'IE_GLB',  '',
13466             'GB',      '',
13467             'CA',      NVL(per_addr.region_1,   ''),
13468             'CA_GLB',  NVL(per_addr.region_1,   ''),
13469             'JP',      NVL(per_addr.region_1,   ''),
13470             NVL(ap_web_db_expline_pkg.
13471                     getcountyprovince(
13472                         per_addr.style,
13473                         per_addr.region_1),
13474               '')
13475             ))
13476             )
13477           )
13478         ) province,
13479 
13480       /* payee state */
13481       DECODE(
13482         pmt.address_source,
13483 
13484         -- supplier state
13485         'TCA', payee_loc.state,
13486 
13487          -- employee state
13488          (
13489          DECODE(
13490            pmt.employee_address_code,
13491 
13492            -- employee home state
13493            'HOME',
13494            DECODE(per_addr.style,
13495              'CA',     '',
13496              'CA_GLB', '',
13497              NVL(per_addr.region_2,   '')),
13498 
13499            -- employee office state
13500            'OFFICE',
13501            DECODE(per_loc.style,
13502              'CA',     '',
13503              'CA_GLB', '',
13504              NVL(per_loc.region_2, '')),
13505 
13506            -- employee provisional state
13507            'PROVISIONAL',
13508            DECODE(per_addr_prov.style,
13509              'CA',     '',
13510              'CA_GLB', '',
13511              NVL(per_addr_prov.region_2,   '')),
13512 
13513            --address code not specified
13514            decode(per_addr.address_id,
13515            NULL,DECODE(per_loc.style,
13516              'CA',     '',
13517              'CA_GLB', '',
13518               NVL(per_loc.region_2, '')),
13519             DECODE(per_addr.style,
13520              'CA',     '',
13521              'CA_GLB', '',
13522               NVL(per_addr.region_2,   '')))
13523            )
13524          )
13525        ) state,
13526 
13527      /* payee country */
13528       DECODE(
13529         pmt.address_source,
13530 
13531         -- supplier country
13532         'TCA', payee_loc.country,
13533 
13534         -- employee country
13535         (
13536         DECODE(
13537           pmt.employee_address_code,
13538 
13539           -- employee home country
13540           'HOME', per_addr.country,
13541 
13542           -- employee office country
13543           'OFFICE',per_loc.country,
13544 
13545           -- employee provisional country
13546           'PROVISIONAL', per_addr_prov.country,
13547 
13548           --address code not specified
13549           DECODE (per_addr.address_id,
13550                NULL, per_loc.country,
13551                per_addr.country
13552                )
13553           )
13554         )
13555         ) country,
13556 
13557       /* payee postal code */
13558       DECODE(
13559         pmt.address_source,
13560 
13561         -- supplier postal code
13562         'TCA', payee_loc.postal_code,
13563 
13564         -- employee postal code
13565         (
13566         DECODE(
13567           pmt.employee_address_code,
13568 
13569           -- employee home postal code
13570           'HOME', per_addr.postal_code,
13571 
13572           -- employee office postal code
13573           'OFFICE',per_loc.postal_code,
13574 
13575           -- employee provisional postal code
13576           'PROVISIONAL', per_addr_prov.postal_code,
13577 
13578           --address code not specified
13579           DECODE (per_addr.address_id,
13580                NULL, per_loc.postal_code,
13581                per_addr.postal_code
13582                )
13583           )
13584         )
13585         ) postal_code,
13586 
13587 
13588       /* payee address concat */
13589       DECODE(
13590         pmt.address_source,
13591 
13592         -- supplier address concat
13593         'TCA',
13594         payee_loc.address1
13595           || ', '
13596           || payee_loc.address2
13597           || ', '
13598           || payee_loc.address3
13599           || ', '
13600           || payee_loc.city
13601           || ', '
13602           || payee_loc.state
13603           || ', '
13604           || payee_loc.country
13605           || ', '
13606           || payee_loc.postal_code,
13607 
13608         -- employee address concat
13609         (
13610         DECODE(
13611           pmt.employee_address_code,
13612 
13613           -- employee home address concat
13614           'HOME',
13615           per_addr.address_line1
13616             || ', '
13617             || per_addr.address_line2
13618             || ', '
13619             || per_addr.address_line3
13620             || ', '
13621             || per_addr.town_or_city
13622             || ', '
13623             || DECODE(
13624                  per_addr.style,
13625                  'CA',     '',
13626                  'CA_GLB', '',
13627                  NVL(per_addr.region_2, '')
13628                  )
13629             || ', '
13630             || per_addr.country
13631             || ', '
13632             || per_addr.postal_code,
13633 
13634           -- employee office address concat
13635           'OFFICE',
13636           per_loc.address_line_1
13637             || ', '
13638             || per_loc.address_line_2
13639             || ', '
13640             || per_loc.address_line_3
13641             || ', '
13642             || per_loc.town_or_city
13643             || ', '
13644             || DECODE(
13645                  per_loc.style,
13646                  'CA',     '',
13647                  'CA_GLB', '',
13648                  NVL(per_loc.region_2, '')
13649                  )
13650             || ', '
13651             || per_loc.country
13652             || ', '
13653             || per_loc.postal_code,
13654 
13655           -- employee provisional address concat
13656           'PROVISIONAL',
13657           per_addr_prov.address_line1
13658             || ', '
13659             || per_addr_prov.address_line2
13660             || ', '
13661             || per_addr_prov.address_line3
13662             || ', '
13663             || per_addr_prov.town_or_city
13664             || ', '
13665             || DECODE(
13666                  per_addr_prov.style,
13667                  'CA',     '',
13668                  'CA_GLB', '',
13669                  NVL(per_addr_prov.region_2, '')
13670                  )
13671             || ', '
13672             || per_addr_prov.country
13673             || ', '
13674             || per_addr_prov.postal_code,
13675           -- address code not specified
13676           DECODE (per_addr.address_id,
13677                NULL, per_loc.address_line_1
13678             || ', '
13679             || per_loc.address_line_2
13680             || ', '
13681             || per_loc.address_line_3
13682             || ', '
13683             || per_loc.town_or_city
13684             || ', '
13685             || DECODE(
13686                  per_loc.style,
13687                  'CA',     '',
13688                  'CA_GLB', '',
13689                  NVL(per_loc.region_2, '')
13690                  )
13691             || ', '
13692             || per_loc.country
13693             || ', '
13694             || per_loc.postal_code,
13695 
13696               per_addr.address_line1
13697             || ', '
13698             || per_addr.address_line2
13699             || ', '
13700             || per_addr.address_line3
13701             || ', '
13702             || per_addr.town_or_city
13703             || ', '
13704             || DECODE(
13705                  per_addr.style,
13706                  'CA',     '',
13707                  'CA_GLB', '',
13708                  NVL(per_addr.region_2, '')
13709                  )
13710             || ', '
13711             || per_addr.country
13712             || ', '
13713             || per_addr.postal_code)
13714           )
13715         )
13716       ) add_concat
13717 
13718   FROM
13719       IBY_PAYMENTS_ALL         pmt,
13720 
13721       /* Employee address related */
13722       HR_LOCATIONS             per_loc,
13723       PER_ADDRESSES            per_addr,
13724       PER_ALL_ASSIGNMENTS_F    per_assgn,
13725       PER_ADDRESSES            per_addr_prov,
13726 
13727       /* Supplier address related */
13728       HZ_LOCATIONS             payee_loc
13729   WHERE
13730     pmt.payment_id                     = p_payment_id
13731     AND pmt.employee_person_id         = per_addr.person_id(+)
13732     AND per_addr.primary_flag(+) = 'Y'
13733     AND SYSDATE BETWEEN
13734             per_addr.date_from(+)
13735             AND NVL(per_addr.date_to(+), SYSDATE+1)
13736     AND pmt.employee_person_id         = per_addr_prov.person_id(+)
13737     AND per_addr_prov.address_type(+) = 'M'
13738     AND SYSDATE BETWEEN
13739             per_addr_prov.date_from(+)
13740             AND NVL(per_addr_prov.date_to(+), SYSDATE+1)
13741     AND pmt.employee_person_id         = per_assgn.person_id(+)
13742     AND per_assgn.location_id          = per_loc.location_id(+)
13743     AND per_assgn.primary_flag(+)      = 'Y'
13744     AND (TRUNC(SYSDATE) BETWEEN
13745             per_assgn.effective_start_date(+)
13746             AND per_assgn.effective_end_date(+)
13747         )
13748     AND pmt.remit_to_location_id       = payee_loc.location_id(+)
13749   ) payee_addr
13750 
13751  WHERE
13752 
13753   /* payment related */
13754   pmt.payment_id                     = p_payment_id
13755 
13756   /* payer */
13757   AND pmt.legal_entity_id            = payer.legal_entity_id
13758   AND payer.party_id                 = payer_party.party_id
13759 
13760   /* payer bank */
13761   AND pmt.internal_bank_account_id   = iba.bank_account_id
13762   AND iba_bnk_branch.branch_party_id = iba.bank_branch_id
13763 
13764   /* payer bank location */
13765   AND iba_bnk_branch.branch_party_id = iba_branch_party_site.party_id(+)
13766 
13767   /* payee */
13768   AND pmt.payee_party_id             = payee.party_id
13769 
13770   /* payee site */
13771   AND pmt.party_site_id              = payee_site.party_site_id(+)
13772 
13773 
13774   /* inv payee */
13775   AND pmt.inv_payee_party_id             = invpayee.party_id(+)
13776 
13777   /* inv payee site */
13778   AND pmt.inv_party_site_id              = invpayee_site.party_site_id(+)
13779 
13780   /* vendor related */
13781   AND pmt.payee_party_id             = vendor.party_id(+)
13782   AND pmt.supplier_site_id           = vendor_site.vendor_site_id(+)
13783   AND vendor.vendor_id               = vendor_site.vendor_id       /*Bug 7323072*/
13784 
13785 
13786   /* inv vendor related */
13787   AND pmt.inv_payee_party_id             = invvendor.party_id(+)
13788   AND pmt.inv_supplier_site_id           = invvendor_site.vendor_site_id(+)
13789   AND nvl(vendor.vendor_id,-99)      = nvl(vendor_site.vendor_id,-99)        /*Bug 7323072*/
13790 
13791   /* employee related */
13792   AND pmt.employee_person_id         = per.person_id(+)
13793 
13794   /* payee bank */
13795   AND pmt.external_bank_account_id   = eba.ext_bank_account_id(+)
13796   AND eba.bank_party_id              = eba_bank_branch.bank_party_id(+)
13797   AND eba.branch_party_id            = eba_bank_branch.branch_party_id(+)
13798 
13799   /*
13800    * Fix for bug 5658982:
13801    *
13802    * We want to use outer join with the eba table because the
13803    * ext bank account id is optional on the payment.
13804    *
13805    * For the same reason, we must use outer join with
13806    * ext_ba_table table also.
13807    */
13808   AND ext_ba_table.ext_bank_account_id(+) = eba.ext_bank_account_id
13809 
13810   /* profile and remit advice */
13811   AND pmt.payment_profile_id         = prof.payment_profile_id
13812   AND prof.system_profile_code       = remit_advice.system_profile_code
13813 
13814   /* payment service request related */
13815   AND payreq.payment_service_request_id  = pmt.payment_service_request_id
13816   AND fnd_app.application_id         = payreq.calling_app_id
13817 
13818   /* delivery channel related */
13819   AND pmt.delivery_channel_code      = deliv.delivery_channel_code(+)
13820 
13821   /* payment reason */
13822   AND pmt.payment_reason_code        = pmt_reason.payment_reason_code(+)
13823 
13824   /* bank instruction */
13825   AND prof.bank_instruction1_code    = bank_instr1.bank_instruction_code(+)
13826   AND prof.bank_instruction2_code    = bank_instr2.bank_instruction_code(+)
13827 
13828   /* Org related */
13829   AND pmt.org_id                     = le.legal_entity_id
13830 
13831   /* RFC related */
13832   AND rfc_ca.owner_table_name(+)     = 'HZ_PARTIES'
13833   AND rfc_ca.class_category(+)       = 'RFC_IDENTIFIER'
13834   AND rfc_ca.owner_table_id(+)       = iba.bank_branch_id
13835   ;
13836 
13837 
13838 
13839  l_pmtAuditRec      IBY_PAYGROUP_PUB.paymentAuditRecType;
13840  l_pmtAuditTab      IBY_PAYGROUP_PUB.paymentAuditTabType;
13841 
13842  BEGIN
13843 
13844      print_debuginfo(l_module_name, 'ENTER');
13845 
13846      IF (p_paymentTab.COUNT = 0) THEN
13847 
13848          print_debuginfo(l_module_name, 'Provided list of payments is '
13849              || 'empty. Exiting ..'
13850              );
13851          print_debuginfo(l_module_name, 'EXIT');
13852          RETURN;
13853 
13854      END IF;
13855 
13856      FOR i IN p_paymentTab.FIRST .. p_paymentTab.LAST LOOP
13857 
13858          /*
13859           * Pick up all the audit-related data for this
13860           * payment.
13861           *
13862           *Condition below is added for Bug:7183572
13863           */
13864 	 IF(p_paymentTab(i).payment_function='CASH_PAYMENT') THEN
13865 
13866 		 OPEN  c_ce_audit_columns(p_paymentTab(i).payment_id);
13867 		 FETCH c_ce_audit_columns INTO l_pmtAuditRec;
13868 		 CLOSE c_ce_audit_columns;
13869          ELSE
13870 		 OPEN  c_audit_columns(p_paymentTab(i).payment_id);
13871 		 FETCH c_audit_columns INTO l_pmtAuditRec;
13872 		 CLOSE c_audit_columns;
13873          END IF;
13874 
13875          print_debuginfo(l_module_name, 'Payment id: '
13876              || l_pmtAuditRec.payment_id
13877              || ', payee name: '
13878              || ''''
13879              || l_pmtAuditRec.payee_name
13880              || ''''
13881              || ', payer legal entity name: '
13882              || ''''
13883              || l_pmtAuditRec.payer_legal_name
13884              || ''''
13885              || ', payer bank name: '
13886              || ''''
13887              || l_pmtAuditRec.payer_bank_name
13888              || ''''
13889              || ', trading partner name: '
13890              || ''''
13891              || l_pmtAuditRec.inv_payee_party_name
13892              || ''''
13893              );
13894 
13895          /*
13896           * Add this audit record to our table of audit
13897           * records.
13898           */
13899          l_pmtAuditTab(l_pmtAuditTab.COUNT + 1) := l_pmtAuditRec;
13900 
13901      END LOOP;
13902 
13903      /*
13904       * Update the IBY_PAYMENTS_ALL table using the retrieved
13905       * audit information.
13906       */
13907      insertAuditData(l_pmtAuditTab);
13908 
13909      print_debuginfo(l_module_name, 'EXIT');
13910 
13911  END auditPaymentData;
13912 
13913 /*--------------------------------------------------------------------
13914  | NAME:
13915  |     insertAuditData
13916  |
13917  | PURPOSE:
13918  |
13919  |
13920  | PARAMETERS:
13921  |     IN
13922  |
13923  |
13924  |     OUT
13925  |
13926  |
13927  | RETURNS:
13928  |
13929  | NOTES:
13930  |
13931  *---------------------------------------------------------------------*/
13932  PROCEDURE insertAuditData(
13933      p_auditPmtTab    IN paymentAuditTabType
13934      )
13935  IS
13936 
13937  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME || '.insertAuditData';
13938 
13939  BEGIN
13940 
13941      print_debuginfo(l_module_name, 'ENTER');
13942 
13943      /* Normally, this shouldn't happen */
13944      IF (p_auditPmtTab.COUNT = 0) THEN
13945 
13946          print_debuginfo(l_module_name, 'No audit data '
13947              || 'was provided to update IBY_PAYMENTS_ALL table. '
13948              || 'Exiting ..'
13949              );
13950 
13951          print_debuginfo(l_module_name, 'EXIT');
13952 
13953          RETURN;
13954 
13955      END IF;
13956 
13957 
13958      FOR i IN p_auditPmtTab.FIRST .. p_auditPmtTab.LAST LOOP
13959 
13960          UPDATE
13961              IBY_PAYMENTS_ALL
13962          SET
13963              payer_party_number             = p_auditPmtTab(i).
13964                                                   payer_party_number,
13965              payer_party_site_name          = p_auditPmtTab(i).
13966                                                   payer_party_site_name,
13967              payer_legal_entity_name        = p_auditPmtTab(i).payer_legal_name,
13968              payer_tax_registration_num     = p_auditPmtTab(i).
13969                                                   payer_tax_reg_number,
13970              payer_le_registration_num      = p_auditPmtTab(i).
13971                                                   payer_le_reg_number,
13972              payer_party_id                 = p_auditPmtTab(i).
13973                                                   payer_party_id,
13974              payer_location_id              = p_auditPmtTab(i).
13975                                                   payer_location_id,
13976              payer_party_attr_category      = p_auditPmtTab(i).
13977                                                   payer_party_attr_cat,
13978              payer_le_attr_category         = p_auditPmtTab(i).
13979                                                   payer_le_attr_cat,
13980              payer_abbreviated_agency_code  = p_auditPmtTab(i).
13981                                                   payer_abbrev_agency_code,
13982              payer_federal_us_employer_id   = p_auditPmtTab(i).
13983                                                   payer_us_employer_id,
13984              int_bank_name                  = p_auditPmtTab(i).payer_bank_name,
13985              int_bank_number                = p_auditPmtTab(i).
13986                                                   payer_bank_number,
13987              int_bank_branch_number         = p_auditPmtTab(i).
13988                                                   payer_bank_branch_number,
13989              int_bank_branch_name           = p_auditPmtTab(i).
13990                                                   payer_bank_branch_name,
13991              int_eft_swift_code             = p_auditPmtTab(i).
13992                                                   payer_bank_swift_code,
13993              int_bank_account_number        = p_auditPmtTab(i).
13994                                                   payer_bank_acct_num,
13995              int_bank_account_name          = p_auditPmtTab(i).
13996                                                   payer_bank_acct_name,
13997              int_bank_account_iban          = p_auditPmtTab(i).
13998                                                   payer_bank_acct_iban,
13999              int_bank_acct_agency_loc_code  = p_auditPmtTab(i).
14000                                                   payer_bank_agency_loc_code,
14001              int_bank_branch_party_id       = p_auditPmtTab(i).
14002                                                   payer_bank_branch_party_id,
14003              int_bank_alt_name              = p_auditPmtTab(i).
14004                                                   payer_bank_alt_name,
14005              int_bank_branch_alt_name       = p_auditPmtTab(i).
14006                                                   payer_bank_branch_alt_name,
14007              int_bank_account_alt_name      = p_auditPmtTab(i).
14008                                                   payer_bank_alt_account_name,
14009              int_bank_account_num_elec      = p_auditPmtTab(i).
14010                                                   payer_bank_account_num_elec,
14011              int_bank_branch_location_id    = p_auditPmtTab(i).
14012                                                   payer_bank_branch_location_id,
14013              int_bank_branch_eft_user_num   = p_auditPmtTab(i).
14014                                               payer_bank_branch_eft_user_num,
14015              payee_party_number             = p_auditPmtTab(i).
14016                                                   payee_party_number,
14017              payee_party_name               = p_auditPmtTab(i).payee_party_name,
14018              payee_name                     = p_auditPmtTab(i).payee_name,
14019              payee_alternate_name           = p_auditPmtTab(i).payee_name_alternate,     -- Bug 6175102
14020              payee_address1                 = p_auditPmtTab(i).payee_add_line_1,
14021              payee_address2                 = p_auditPmtTab(i).payee_add_line_2,
14022              payee_address3                 = p_auditPmtTab(i).payee_add_line_3,
14023              payee_address4                 = p_auditPmtTab(i).payee_add_line_4,
14024              payee_city                     = p_auditPmtTab(i).payee_city,
14025              payee_county                   = p_auditPmtTab(i).payee_county,
14026              payee_province                 = p_auditPmtTab(i).payee_province,
14027              payee_state                    = p_auditPmtTab(i).payee_state,
14028              payee_country                  = p_auditPmtTab(i).payee_country,
14029              payee_postal_code              = p_auditPmtTab(i).
14030                                                   payee_postal_code,
14031              payee_address_concat           = p_auditPmtTab(i).
14032                                                   payee_address_concat,
14033              beneficiary_name               = p_auditPmtTab(i).
14034                                                   beneficiary_name,
14035              payee_party_attr_category      = p_auditPmtTab(i).
14036                                                   payee_party_attr_cat,
14037              payee_spplr_site_attr_category = p_auditPmtTab(i).
14038                                                   payee_supplier_site_attr_cat,
14039              payee_supplier_site_name       = p_auditPmtTab(i).
14040                                                   payee_supplier_site_name,
14041              payee_site_alternate_name      = p_auditPmtTab(i).
14042                                                   payee_site_name_alternate,
14043              payee_supplier_number          = p_auditPmtTab(i).
14044                                                   payee_supplier_number,
14045              payee_first_party_reference    = p_auditPmtTab(i).
14046                                                   payee_first_party_ref,
14047              payee_supplier_attr_category   = p_auditPmtTab(i).
14048                                                   payee_supplier_attr_cat,
14049              payee_supplier_id              = p_auditPmtTab(i).
14050                                                   payee_supplier_id,
14051              payee_tax_registration_num     = p_auditPmtTab(i).
14052                                                   payee_tax_reg_number,
14053              payee_le_registration_num      = p_auditPmtTab(i).
14054                                                   payee_le_reg_number,
14055              ext_bank_name                  = p_auditPmtTab(i).payee_bank_name,
14056              ext_bank_number                = p_auditPmtTab(i).
14057                                                   payee_bank_number,
14058              ext_branch_number              = p_auditPmtTab(i).
14059                                                   payee_bank_branch_number,
14060              ext_bank_branch_name           = p_auditPmtTab(i).
14061                                                   payee_bank_branch_name,
14062              ext_bank_account_number        = p_auditPmtTab(i).
14063                                                   payee_bank_acct_number,
14064              ext_bank_account_name          = p_auditPmtTab(i).
14065                                                   payee_bank_acct_name,
14066              ext_bank_account_iban_number   = p_auditPmtTab(i).
14067                                                   payee_bank_acct_iban,
14068              ext_eft_swift_code             = p_auditPmtTab(i).
14069                                                   payee_bank_swift_code,
14070              ext_bank_account_type          = p_auditPmtTab(i).
14071                                                   payee_bank_acct_type,
14072              ext_bank_acct_pmt_factor_flag  = p_auditPmtTab(i).
14073                                                  payee_bank_payment_factor_flag,
14074              ext_bank_acct_owner_party_id   = p_auditPmtTab(i).
14075                                                   payee_bank_owner_party_id,
14076              ext_bank_branch_party_id       = p_auditPmtTab(i).
14077                                                   payee_bank_branch_party_id,
14078              ext_bank_alt_name              = p_auditPmtTab(i).
14079                                                   payee_bank_name_alt,
14080              ext_bank_branch_alt_name       = p_auditPmtTab(i).
14081                                                   payee_bank_branch_name_alt,
14082              ext_bank_account_alt_name      = p_auditPmtTab(i).
14083                                                   payee_bank_alt_account_name,
14084              ext_bank_account_num_elec      = p_auditPmtTab(i).
14085                                               payee_bank_electronic_acct_num,
14086              ext_bank_branch_location_id    = p_auditPmtTab(i).
14087                                                   payee_bank_branch_location_id,
14088              ext_bank_acct_owner_party_name = p_auditPmtTab(i).
14089                                                   payee_bank_acct_owner_name,
14090              remit_advice_delivery_method   = p_auditPmtTab(i).
14091                                                   remit_advice_delivery_method,
14092              remit_advice_email             = p_auditPmtTab(i).
14093                                                   remit_advice_email,
14094              remit_advice_fax               = p_auditPmtTab(i).
14095                                                   remit_advice_fax,
14096 
14097              /*
14098               * Fix for bug 5522421:
14099               *
14100               * sra_delivery_method is the same as
14101               * remit_advice_delivery_method and is
14102               * being obsoleted.
14103               */
14104              --sra_delivery_method            = p_auditPmtTab(i).
14105              --                                     remit_advice_delv_method,
14106 
14107              delivery_channel_format_value  = p_auditPmtTab(i).
14108                                                   delivery_channel_format,
14109              declaration_exch_rate_type     = p_auditPmtTab(i).
14110                                                   decl_curr_fx_rate_type,
14111              declaration_format             = p_auditPmtTab(i).
14112                                                   declaration_format,
14113              payment_profile_acct_name      = p_auditPmtTab(i).
14114                                                   payment_acct_profile_name,
14115              payment_profile_sys_name       = p_auditPmtTab(i).
14116                                                   payment_sys_profile_name,
14117              payment_reason_format_value    = p_auditPmtTab(i).
14118                                                   payment_reason_format,
14119              bank_instruction1_format_value = p_auditPmtTab(i).
14120                                                   bank_instr1_format,
14121              bank_instruction2_format_value = p_auditPmtTab(i).
14122                                                   bank_instr2_format,
14123              org_name                       = p_auditPmtTab(i).org_name,
14124              int_bank_branch_rfc_identifier = p_auditPmtTab(i).
14125                                                   payer_bank_branch_rfc_id,
14126              payment_process_request_name   = p_auditPmtTab(i).ppr_name,
14127              source_product                 = p_auditPmtTab(i).source_product,
14128 
14129 /*TPP-Start*/
14130 inv_payee_name = p_auditPmtTab(i).inv_payee_name,
14131 inv_payee_address1 = p_auditPmtTab(i).inv_payee_address1,
14132 inv_payee_address2 = p_auditPmtTab(i).inv_payee_address2,
14133 inv_payee_address3 = p_auditPmtTab(i).inv_payee_address3,
14134 inv_payee_address4 = p_auditPmtTab(i).inv_payee_address4,
14135 inv_payee_city = p_auditPmtTab(i).inv_payee_city,
14136 inv_payee_postal_code = p_auditPmtTab(i).inv_payee_postal_code,
14137 inv_payee_state = p_auditPmtTab(i).inv_payee_state,
14138 inv_payee_province = p_auditPmtTab(i).inv_payee_province,
14139 inv_payee_county = p_auditPmtTab(i).inv_payee_county,
14140 inv_payee_country = p_auditPmtTab(i).inv_payee_country,
14141 inv_payee_party_name = p_auditPmtTab(i).inv_payee_party_name,
14142 inv_payee_le_reg_num = p_auditPmtTab(i).inv_payee_le_reg_num,
14143 inv_payee_tax_reg_num = p_auditPmtTab(i).inv_payee_tax_reg_num,
14144 inv_payee_address_concat = p_auditPmtTab(i).inv_payee_address_concat,
14145 inv_beneficiary_name = p_auditPmtTab(i).inv_beneficiary_name,
14146 inv_payee_party_number = p_auditPmtTab(i).inv_payee_party_number,
14147 inv_payee_alternate_name = p_auditPmtTab(i).inv_payee_alternate_name,
14148 inv_payee_site_alt_name = p_auditPmtTab(i).inv_payee_site_alt_name,
14149 inv_payee_supplier_number = p_auditPmtTab(i).inv_payee_supplier_number,
14150 inv_payee_first_party_ref = p_auditPmtTab(i).inv_payee_first_party_ref,
14151 ext_bnk_acct_ownr_inv_prty_id = p_auditPmtTab(i).ext_bnk_acct_ownr_inv_prty_id,
14152 ext_bnk_branch_inv_prty_id = p_auditPmtTab(i).ext_bnk_branch_inv_prty_id,
14153 ext_bnk_acct_ownr_inv_prty_nme = p_auditPmtTab(i).ext_bnk_acct_ownr_inv_prty_nme,
14154 inv_payee_party_attr_cat = p_auditPmtTab(i).inv_payee_party_attr_cat,
14155 inv_payee_supplier_attr_cat = p_auditPmtTab(i).inv_payee_supplier_attr_cat,
14156 inv_payee_spplr_site_attr_cat = p_auditPmtTab(i).inv_payee_spplr_site_attr_cat,
14157 inv_payee_supplier_site_name = p_auditPmtTab(i).inv_payee_supplier_site_name,
14158 inv_payee_spplr_site_alt_name = p_auditPmtTab(i).inv_payee_spplr_site_alt_name,
14159 inv_payee_supplier_id = p_auditPmtTab(i).inv_payee_supplier_id
14160 /*TPP-End*/
14161 
14162          WHERE
14163              payment_id                     = p_auditPmtTab(i).payment_id
14164          ;
14165 
14166      END LOOP;
14167 
14168      print_debuginfo(l_module_name, 'EXIT');
14169 
14170  END insertAuditData;
14171 
14172 /*--------------------------------------------------------------------
14173  | NAME:
14174  |     sweepCommonPmtAttributes
14175  |
14176  | PURPOSE:
14177  |     Scan all the documents of each payment for specific common
14178  |     attributes.
14179  |
14180  |     If such a common attribute is found, set the correspoding
14181  |     attribute on the parent payment to the same value.
14182  |
14183  | PARAMETERS:
14184  |     IN
14185  |
14186  |
14187  |     OUT
14188  |
14189  |
14190  | RETURNS:
14191  |
14192  | NOTES:
14193  |
14194  *---------------------------------------------------------------------*/
14195  PROCEDURE sweepCommonPmtAttributes (
14196      x_paymentTab      IN OUT NOCOPY IBY_PAYGROUP_PUB.paymentTabType,
14197      x_docsInPmtTab    IN OUT NOCOPY IBY_PAYGROUP_PUB.docsInPaymentTabType
14198      )
14199  IS
14200  l_module_name CONSTANT VARCHAR2(200) := G_PKG_NAME
14201                                              || '.sweepCommonPmtAttributes';
14202  l_first_record           BOOLEAN     := TRUE;
14203 
14204  l_due_date_common_flag   BOOLEAN     := TRUE;
14205  l_temp_due_date          DATE        := NULL;
14206 
14207  l_delv_chnl_common_flag   BOOLEAN    := TRUE;
14208  l_temp_delv_chnl          VARCHAR2(200);
14209 
14210  l_uri_common_flag         BOOLEAN    := TRUE;
14211  l_temp_uri                VARCHAR2(200);
14212  l_temp_uri_ckdigits       VARCHAR2(200);
14213 
14214  BEGIN
14215 
14216      print_debuginfo(l_module_name, 'ENTER');
14217 
14218      /*
14219       * For each payment:
14220       *
14221       * Search if all the docs of this payment contain
14222       * the same attribute, then populate this
14223       * attribute on the payment itself.
14224       */
14225 
14226      /*
14227       * Sweep for payment due date.
14228       */
14229      FOR i IN x_paymentTab.FIRST .. x_paymentTab.LAST LOOP
14230 
14231          /*
14232           * Initialize for each payment iteration.
14233           */
14234          l_due_date_common_flag := TRUE;
14235          l_temp_due_date        := NULL;
14236          l_first_record         := TRUE;
14237 
14238          /*
14239           * Only attempt to derive the payment due date if the
14240           * payment does not already have a due date set.
14241           */
14242          IF (x_paymentTab(i).payment_due_date IS NULL) THEN
14243 
14244              FOR j IN x_docsInPmtTab.FIRST .. x_docsInPmtTab.LAST LOOP
14245 
14246                  IF (x_paymentTab(i).payment_id = x_docsInPmtTab(j).payment_id)
14247                  THEN
14248 
14249                      /*
14250                       * If this is the first document for the payment
14251                       * in consideration, initialize the temp due
14252                       * date with the due date on the current doc.
14253                       */
14254                      IF (l_first_record = TRUE) THEN
14255 
14256                          l_temp_due_date := x_docsInPmtTab(j).pmt_due_date;
14257 
14258                      ELSE
14259 
14260                          /*
14261                           * If the due date on the current document does not
14262                           * match the due date on the previously stored
14263                           * document, then the due date is not common across
14264                           * all docs of this payment.
14265                           */
14266                          IF (l_temp_due_date <> x_docsInPmtTab(j).pmt_due_date)
14267                          THEN
14268 
14269                              l_due_date_common_flag := FALSE;
14270 
14271                              /*
14272                               * Exit iteration for this payment.
14273                               */
14274                              EXIT;
14275 
14276                          END IF; -- if due date does not match
14277 
14278                      END IF; -- if first record
14279 
14280                  END IF; -- if doc is for current payment
14281 
14282                  /*
14283                   * Reset the first record flag.
14284                   */
14285                  /*
14286                   * Fix for bug 5529924:
14287                   *
14288                   * When checking for the first record, also
14289                   * check that that the due date variable
14290                   * is not null. Otherwise, we might be
14291                   * setting the first record flag based
14292                   * on some document unrelated to this payment.
14293                   */
14294                  IF (l_temp_due_date IS NOT NULL) THEN
14295                      IF (l_first_record = TRUE) THEN
14296                          l_first_record := FALSE;
14297                      END IF;
14298                  END IF;
14299 
14300              END LOOP; -- for all docs
14301 
14302              IF (l_due_date_common_flag = TRUE) THEN
14303 
14304                  /*
14305                   * This means that all the docs of this payment
14306                   * have the same due date.
14307                   *
14308                   * Set this value on the payment (if not null)
14309                   */
14310                  IF (l_temp_due_date IS NOT NULL) THEN
14311 
14312                      print_debuginfo(l_module_name, 'All documents of payment '
14313                          || x_paymentTab(i).payment_id
14314                          || ' have the same payment due date '
14315                          || l_temp_due_date
14316                          || '. Hence setting this attribute on the '
14317                          || 'payment itself.'
14318                          );
14319 
14320                      x_paymentTab(i).payment_due_date := l_temp_due_date;
14321 
14322                  ELSE
14323 
14324                      print_debuginfo(l_module_name, 'Not setting pmt due date '
14325                          || 'for pmt id: '
14326                          || x_paymentTab(i).payment_id
14327                          );
14328 
14329                  END IF; -- if due date is not null
14330 
14331              END IF; -- if due date common flag set to TRUE
14332 
14333          END IF; -- if pmt due date is null
14334 
14335      END LOOP; -- for all pmts
14336 
14337      /*
14338       * Fix for bug 5462976:
14339       *
14340       * Sweep for delivery channel.
14341       */
14342      FOR i IN x_paymentTab.FIRST .. x_paymentTab.LAST LOOP
14343 
14344          /*
14345           * Initialize for each payment iteration.
14346           */
14347          l_delv_chnl_common_flag := TRUE;
14348          l_temp_delv_chnl        := NULL;
14349          l_first_record          := TRUE;
14350 
14351          /*
14352           * Only attempt to derive the delivery channel if the
14353           * payment does not already have a delivery channel set.
14354           */
14355          IF (x_paymentTab(i).delivery_channel_code IS NULL) THEN
14356 
14357              FOR j IN x_docsInPmtTab.FIRST .. x_docsInPmtTab.LAST LOOP
14358 
14359                  IF (x_paymentTab(i).payment_id = x_docsInPmtTab(j).payment_id)
14360                  THEN
14361 
14362                      /*
14363                       * If this is the first document for the payment
14364                       * in consideration, initialize the temp delivery
14365                       * channel with the delv channel on the current doc.
14366                       */
14367                      IF (l_first_record = TRUE) THEN
14368 
14369                          l_temp_delv_chnl := x_docsInPmtTab(j).delivery_channel;
14370 
14371                      ELSE
14372 
14373                          /*
14374                           * If the delv chnl on the current document does not
14375                           * match the delv chnl on the previously stored
14376                           * document, then the delv chnl is not common across
14377                           * all docs of this payment.
14378                           */
14379                          IF (l_temp_delv_chnl <>
14380                              x_docsInPmtTab(j).delivery_channel) THEN
14381 
14382                              l_delv_chnl_common_flag := FALSE;
14383 
14384                              /*
14385                               * Exit iteration for this payment.
14386                               */
14387                              EXIT;
14388 
14389                          END IF; -- if delv chnl does not match
14390 
14391                      END IF; -- if first record
14392 
14393                  END IF; -- if doc is for current payment
14394 
14395                  /*
14396                   * Reset the first record flag.
14397                   */
14398                  /*
14399                   * Fix for bug 5529924:
14400                   *
14401                   * When checking for the first record, also
14402                   * check that that the delivery channel variable
14403                   * is not null. Otherwise, we might be
14404                   * setting the first record flag based
14405                   * on some document unrelated to this payment.
14406                   */
14407                  IF (l_temp_delv_chnl IS NOT NULL) THEN
14408                      IF (l_first_record = TRUE) THEN
14409                          l_first_record := FALSE;
14410                      END IF;
14411                  END IF;
14412 
14413              END LOOP; -- for all docs
14414 
14415              IF (l_delv_chnl_common_flag = TRUE) THEN
14416 
14417                  /*
14418                   * This means that all the docs of this payment
14419                   * have the same delivery channel.
14420                   *
14421                   * Set this value on the payment (if not null)
14422                   */
14423                  IF (l_temp_delv_chnl IS NOT NULL) THEN
14424 
14425                      print_debuginfo(l_module_name, 'All documents of payment '
14426                          || x_paymentTab(i).payment_id
14427                          || ' have the same delivery channel code '
14428                          || l_temp_delv_chnl
14429                          || '. Hence setting this attribute on the '
14430                          || 'payment itself.'
14431                          );
14432 
14433                      x_paymentTab(i).delivery_channel_code := l_temp_delv_chnl;
14434 
14435                  ELSE
14436 
14437                      print_debuginfo(l_module_name, 'Not setting delivery '
14438                          || 'channel for pmt id: '
14439                          || x_paymentTab(i).payment_id
14440                          );
14441 
14442                  END IF; -- if delv chnl is not null
14443 
14444              END IF; -- if delv chnl common flag set to TRUE
14445 
14446          END IF; -- if delv chnl is null
14447 
14448      END LOOP; -- for all pmts
14449 
14450      /*
14451       * Fix for bug 5586678:
14452       *
14453       * Sweep for unique remittance identifier.
14454       */
14455      FOR i IN x_paymentTab.FIRST .. x_paymentTab.LAST LOOP
14456 
14457          /*
14458           * Initialize for each payment iteration.
14459           */
14460          l_uri_common_flag       := TRUE;
14461          l_temp_uri              := NULL;
14462          l_temp_uri_ckdigits     := NULL;
14463          l_first_record          := TRUE;
14464 
14465          /*
14466           * Only attempt to derive the unique remit identifier if
14467           * the payment does not already have a unique remit id
14468           * set.
14469           */
14470          IF (x_paymentTab(i).unique_remittance_identifier IS NULL) THEN
14471 
14472              FOR j IN x_docsInPmtTab.FIRST .. x_docsInPmtTab.LAST LOOP
14473 
14474                  IF (x_paymentTab(i).payment_id = x_docsInPmtTab(j).payment_id)
14475                  THEN
14476 
14477                      /*
14478                       * If this is the first document for the payment
14479                       * in consideration, initialize the temp URI
14480                       * with the URI on the current doc.
14481                       */
14482                      IF (l_first_record = TRUE) THEN
14483 
14484                          l_temp_uri          := x_docsInPmtTab(j).
14485                                                     unique_remit_id;
14486 
14487                          l_temp_uri_ckdigits := x_docsInPmtTab(j).
14488                                                     uri_ckdigits;
14489 
14490                      ELSE
14491 
14492                          /*
14493                           * If the URI on the current document does not
14494                           * match the URI on the previously stored
14495                           * document, then the URI is not common across
14496                           * all docs of this payment.
14497                           */
14498 
14499                          IF (l_temp_uri <>
14500                              x_docsInPmtTab(j).unique_remit_id) THEN
14501 
14502                              l_uri_common_flag := FALSE;
14503 
14504                              /*
14505                               * Exit iteration for this payment.
14506                               */
14507                              EXIT;
14508 
14509                          END IF; -- if URI does not match
14510 
14511                      END IF; -- if first record
14512 
14513                  END IF; -- if doc is for current payment
14514 
14515                  /*
14516                   * Reset the first record flag.
14517                   */
14518                  /*
14519                   * When checking for the first record, also
14520                   * check that that the URI variable
14521                   * is not null. Otherwise, we might be
14522                   * setting the first record flag based
14523                   * on some document unrelated to this payment.
14524                   */
14525                  IF (l_temp_uri IS NOT NULL) THEN
14526                      IF (l_first_record = TRUE) THEN
14527                          l_first_record := FALSE;
14528                      END IF;
14529                  END IF;
14530 
14531              END LOOP; -- for all docs
14532 
14533              IF (l_uri_common_flag = TRUE) THEN
14534 
14535                  /*
14536                   * This means that all the docs of this payment
14537                   * have the same unique remit id.
14538                   *
14539                   * Set this value on the payment (if not null)
14540                   */
14541                  IF (l_temp_uri IS NOT NULL) THEN
14542 
14543                      print_debuginfo(l_module_name, 'All documents of payment '
14544                          || x_paymentTab(i).payment_id
14545                          || ' have the same unique remit id '
14546                          || l_temp_uri
14547                          || '. Hence setting this attribute on the '
14548                          || 'payment itself.'
14549                          );
14550 
14551                      x_paymentTab(i).unique_remittance_identifier :=
14552                          l_temp_uri;
14553                      x_paymentTab(i).uri_check_digit :=
14554                          l_temp_uri_ckdigits;
14555 
14556                  ELSE
14557 
14558                      print_debuginfo(l_module_name, 'Not setting unique '
14559                          || 'remit id for pmt id: '
14560                          || x_paymentTab(i).payment_id
14561                          );
14562 
14563                  END IF; -- if unique remit id is not null
14564 
14565              END IF; -- if unique remit id common flag set to TRUE
14566 
14567          END IF; -- if unique remit id is null
14568 
14569      END LOOP; -- for all pmts
14570 
14571      print_debuginfo(l_module_name, 'EXIT');
14572 
14573  END sweepCommonPmtAttributes;
14574 
14575 /*--------------------------------------------------------------------
14576  | NAME:
14577  |     print_debuginfo
14578  |
14579  | PURPOSE:
14580  |     This procedure prints the debug message to the concurrent manager
14581  |     log file.
14582  |
14583  | PARAMETERS:
14584  |     IN
14585  |      p_debug_text - The debug message to be printed
14586  |
14587  |     OUT
14588  |
14589  |
14590  | RETURNS:
14591  |
14592  | NOTES:
14593  |
14594  *---------------------------------------------------------------------*/
14595  PROCEDURE print_debuginfo(
14596      p_module      IN VARCHAR2,
14597      p_debug_text  IN VARCHAR2,
14598      p_debug_level IN VARCHAR2  DEFAULT FND_LOG.LEVEL_STATEMENT
14599      )
14600  IS
14601  l_default_debug_level VARCHAR2(200) := FND_LOG.LEVEL_STATEMENT;
14602  BEGIN
14603 
14604      /*
14605       * Set the debug level to the value passed in
14606       * (provided this value is not null).
14607       */
14608      IF (p_debug_level IS NOT NULL) THEN
14609          l_default_debug_level := p_debug_level;
14610      END IF;
14611 
14612      /*
14613       * Write the debug message to the concurrent manager log file.
14614       */
14615      IF (l_default_debug_level >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
14616          iby_build_utils_pkg.print_debuginfo(p_module, p_debug_text,
14617              p_debug_level);
14618      END IF;
14619 
14620  END print_debuginfo;
14621 
14622 
14623 END IBY_PAYGROUP_PUB;