DBA Data[Home] [Help]

APPS.ARP_AUTOAPPLY_API SQL Statements

The following lines contain the word 'select', 'insert', 'update' or 'delete':

Line: 7

  G_LAST_UPDATED_BY        NUMBER    := -222;
Line: 8

  G_LAST_UPDATE_LOGIN      NUMBER    := -222;
Line: 50

  PROCEDURE insert_invoice_recos (p_automatch_id IN NUMBER
                                  , p_use_matching_date IN VARCHAR2
                                  , p_trans_format_str IN VARCHAR2
                                  , p_rem_format_str  IN VARCHAR2
                                  , p_trans_float_str IN VARCHAR2
                                  , p_rem_float_str IN VARCHAR2
                                  , p_worker_number IN NUMBER
                                  , p_request_id IN NUMBER);
Line: 59

  PROCEDURE insert_po_recos (p_automatch_id IN NUMBER
                            , p_use_matching_date IN VARCHAR2
                            , p_trans_format_str IN VARCHAR2
                            , p_rem_format_str  IN VARCHAR2
                            , p_trans_float_str IN VARCHAR2
                            , p_rem_float_str IN VARCHAR2
                            , p_worker_number IN NUMBER
                            , p_request_id IN NUMBER);
Line: 68

  PROCEDURE insert_so_recos (p_automatch_id IN NUMBER
                            , p_use_matching_date IN VARCHAR2
                            , p_trans_format_str IN VARCHAR2
                            , p_rem_format_str  IN VARCHAR2
                            , p_trans_float_str IN VARCHAR2
                            , p_rem_float_str IN VARCHAR2
                            , p_worker_number IN NUMBER
                            , p_request_id IN NUMBER);
Line: 77

  PROCEDURE insert_contract_recos (p_automatch_id IN NUMBER
                                  , p_use_matching_date IN VARCHAR2
                                  , p_trans_format_str IN VARCHAR2
                                  , p_rem_format_str  IN VARCHAR2
                                  , p_trans_float_str IN VARCHAR2
                                  , p_rem_float_str IN VARCHAR2
                                  , p_worker_number IN NUMBER
                                  , p_request_id IN NUMBER);
Line: 86

  PROCEDURE insert_attribute_recos (p_automatch_id IN NUMBER
                                  , p_use_matching_date IN VARCHAR2
                                  , p_trans_format_str IN VARCHAR2
                                  , p_rem_format_str  IN VARCHAR2
                                  , p_trans_float_str IN VARCHAR2
                                  , p_rem_float_str IN VARCHAR2
                                  , p_worker_number IN NUMBER
                                  , p_attribute_number IN VARCHAR2
                                  , p_request_id IN NUMBER);
Line: 96

  PROCEDURE insert_waybill_recos (p_automatch_id IN NUMBER
                                  , p_use_matching_date IN VARCHAR2
                                  , p_trans_format_str IN VARCHAR2
                                  , p_rem_format_str  IN VARCHAR2
                                  , p_trans_float_str IN VARCHAR2
                                  , p_rem_float_str IN VARCHAR2
                                  , p_worker_number IN NUMBER
                                  , p_request_id IN NUMBER);
Line: 105

  PROCEDURE insert_bfb_recos (p_automatch_id IN NUMBER
                                , p_use_matching_date IN VARCHAR2
                                , p_trans_format_str IN VARCHAR2
                                , p_rem_format_str  IN VARCHAR2
                                , p_trans_float_str IN VARCHAR2
                                , p_rem_float_str IN VARCHAR2
                                , p_worker_number IN NUMBER
                                , p_request_id IN NUMBER);
Line: 114

  PROCEDURE insert_reference_recos (p_automatch_id IN NUMBER
                                  , p_use_matching_date IN VARCHAR2
                                  , p_trans_format_str IN VARCHAR2
                                  , p_rem_format_str  IN VARCHAR2
                                  , p_trans_float_str IN VARCHAR2
                                  , p_rem_float_str IN VARCHAR2
                                  , p_worker_number IN NUMBER
                                  , p_request_id IN NUMBER);
Line: 129

  PROCEDURE copy_current_record(  p_current_reco IN OUT NOCOPY selected_recos_table
                                , p_selected_recos IN selected_recos_table
                                , p_index IN NUMBER);
Line: 133

  PROCEDURE process_single_reco(p_current_reco IN OUT NOCOPY selected_recos_table
                                , p_match_resolved_using IN VARCHAR2);
Line: 138

  PROCEDURE populate_reco_line_struct(p_current_reco IN selected_recos_table
                                    , p_match_resolved_using IN VARCHAR2
                                    , p_recommendation_id IN NUMBER
                                    , p_recommendation_reason IN VARCHAR2);
Line: 143

  PROCEDURE insert_recos(p_request_id IN NUMBER);
Line: 211

 *  The function will be called for every insert execution but we need to    *
 *  generate a new sequence only for a new recommendation (not for each line)*
 * MODIFICATION HISTORY -  09/03/2009 - Created by AGHORAKA	     	           *
 *                                                                           *
 +===========================================================================*/

  FUNCTION get_next_reco_id( p_reco_num IN NUMBER)
  RETURN NUMBER IS
  l_reco_id NUMBER;
Line: 222

        SELECT ar_cash_recos_s.nextval
        INTO l_reco_id
        FROM DUAL;
Line: 327

      SELECT string_type_code type,
      string_location_code location,
      DECODE(padding_value_code, 'ZERO',  '[0]',
                                 'SPACE', '[ ]',
                                 'ANY',   '.',
                                 padding_value_code) value,
      DECODE(padding_value_code, 'ANY',  NVL(TO_CHAR(number_of_positions),'9999'),
                                 'ZERO', NVL(TO_CHAR(number_of_positions),'1,'),
                                 'SPACE', NVL(TO_CHAR(number_of_positions),'1,')) position
                                  /* When no of positions is not mentioned replace all the occurences */
      FROM ar_cash_automatch_dtls
      WHERE automatch_id = p_rule_id
      ORDER BY string_type_code ASC, string_location_code ASC, padding_sequence ASC;
Line: 422

 *   1. Delete data from ar_cash_remit_refs_interim, if any data is present. *
 *      The table is truncated at the end of each run. However if any data   *
 *      exists inside the interim event necause of any unhandled exception in*
 *      the previous run, just a precautionary measure to retrunc the table  *
 *   2. Populate ar_cash_Remit_refs_interim with data from ar_cash_remit_refs*
 *      based on the parameters provided to the concurrent program.          *
 *   3. Update the references with status 'AR_AA_RULE_SET_INACTIVE' which    *
 *      are associated to a rule set that is inactive.                       *
 *   4. Spawn the child process or directly call auto_apply_child() process  *
 *      based on the 'No of Instances' parameter.                            *
 *   5. Update the references with status 'AR_AA_SUGG_FOUND'/'AR_AA_NO_MATCH'*
 *      based on the number of receommendations generated for the remittances*
 *      that are not automatically applied.                                  *
 *   6. Update the receipt's WORK_ITEM_EXCEPTION_REASON for the receipts that*
 *      have unapplied remittance lines at the end of the program.           *
 * NOTES -                                                                   *
 *   This program is the starting point for 'AR_AUTOAPPLY_API'. This is      *
 * called from XML report                                                    *
 * MODIFICATION HISTORY -  09/03/2009 - Created by AGHORAKA	     	           *
 *                                                                           *
 +===========================================================================*/

  PROCEDURE auto_apply_master ( P_ERRBUF              OUT NOCOPY VARCHAR2
                              , P_RETCODE             OUT NOCOPY NUMBER
                              , p_org_id              IN NUMBER
                              , p_receipt_no_l        IN VARCHAR2
                              , p_receipt_no_h        IN VARCHAR2
                              , p_batch_name_l        IN VARCHAR2
                              , p_batch_name_h        IN VARCHAR2
                              , p_min_unapp_amt       IN NUMBER
                              , p_receipt_date_l      IN VARCHAR2
                              , p_receipt_date_h      IN VARCHAR2
                              , p_receipt_method_l    IN VARCHAR2
                              , p_receipt_method_h    IN VARCHAR2
                              , p_customer_name_l     IN VARCHAR2
                              , p_customer_name_h     IN VARCHAR2
                              , p_customer_no_l       IN VARCHAR2
                              , p_customer_no_h       IN VARCHAR2
                              , p_batch_id            IN NUMBER
                              , p_transmission_id     IN NUMBER
                              , p_called_from         IN VARCHAR2
                              , p_total_workers       IN NUMBER) IS

      l_insert_stmt   VARCHAR2(30000) := NULL;
Line: 480

      insert_gt       INTEGER;
Line: 481

      l_rows_inserted INTEGER;
Line: 566

      delete_interim_records; /* Call to delete records from interface table */
Line: 570

      G_LAST_UPDATED_BY        := arp_standard.profile.user_id;
Line: 571

      G_LAST_UPDATE_LOGIN      := arp_standard.profile.last_update_login;
Line: 574

      l_insert_stmt := ' INSERT INTO AR_CASH_REMIT_REFS_INTERIM
                       (   REMIT_REFERENCE_ID,
                           RECEIPT_REFERENCE_STATUS,
                           AUTOMATCH_SET_ID,
                           CASH_RECEIPT_ID,
                           REFERENCE_SOURCE,
                           CUSTOMER_ID,
                           CUSTOMER_NUMBER,
                           BANK_ACCOUNT_NUMBER,
                           TRANSIT_ROUTING_NUMBER,
                           INVOICE_REFERENCE,
                           MATCHING_REFERENCE_DATE,
                           INSTALLMENT_REFERENCE,
                           INVOICE_CURRENCY_CODE,
                           AMOUNT_APPLIED,
                           AMOUNT_APPLIED_FROM,
                           TRANS_TO_RECEIPT_RATE,
                           TRANSMISSION_ID,
                           BATCH_ID,
                           WORKER_NUMBER)
                       SELECT ref.REMIT_REFERENCE_ID,
                           ''AR_AM_NEW'',
                           cr.AUTOMATCH_SET_ID,
                           ref.CASH_RECEIPT_ID,
                           ref.REFERENCE_SOURCE,
                           cr.PAY_FROM_CUSTOMER,
                           ref.CUSTOMER_NUMBER,
                           ref.BANK_ACCOUNT_NUMBER,
                           ref.TRANSIT_ROUTING_NUMBER,
                           ref.INVOICE_REFERENCE,
                           ref.MATCHING_REFERENCE_DATE,
                           ref.INSTALLMENT_NUMBER,
                           ref.INVOICE_CURRENCY_CODE,
                           ref.AMOUNT_APPLIED,
                           ref.AMOUNT_APPLIED_FROM,
                           ref.TRANS_TO_RECEIPT_RATE,
                           ref.TRANSMISSION_ID,
                           ref.BATCH_ID,
                           MOD( ref.CASH_RECEIPT_ID, :b_total_workers) + 1';
Line: 715

      l_insert_stmt := l_insert_stmt || l_from_clause || l_where_clause;
Line: 716

      log('Insert Statement : ' || l_insert_stmt);
Line: 717

      insert_gt := dbms_sql.open_cursor;
Line: 718

      dbms_sql.parse (insert_gt,l_insert_stmt,dbms_sql.v7);
Line: 720

      dbms_sql.bind_variable ( insert_gt, ':b_total_workers', p_total_workers);
Line: 722

      dbms_sql.bind_variable ( insert_gt, ':b_transmission_id', p_transmission_id);
Line: 724

      dbms_sql.bind_variable ( insert_gt, ':b_batch_id', p_batch_id);
Line: 727

          dbms_sql.bind_variable ( insert_gt, ':b_receipt_no_l', p_receipt_no_l);
Line: 730

          dbms_sql.bind_variable ( insert_gt, ':b_receipt_no_h', p_receipt_no_h);
Line: 733

          dbms_sql.bind_variable ( insert_gt, ':b_batch_name_l', p_batch_name_l);
Line: 736

          dbms_sql.bind_variable ( insert_gt, ':b_batch_name_h', p_batch_name_h);
Line: 739

          dbms_sql.bind_variable ( insert_gt, ':b_min_unapp_amt', p_min_unapp_amt);
Line: 742

          dbms_sql.bind_variable ( insert_gt, ':b_receipt_date_l', l_receipt_date_low);
Line: 745

          dbms_sql.bind_variable ( insert_gt, ':b_receipt_date_h', l_receipt_date_high);
Line: 748

          dbms_sql.bind_variable ( insert_gt, ':b_receipt_method_l', p_receipt_method_l);
Line: 751

          dbms_sql.bind_variable ( insert_gt, ':b_receipt_method_h', p_receipt_method_h);
Line: 754

          dbms_sql.bind_variable ( insert_gt, ':b_customer_name_l', p_customer_name_l);
Line: 757

          dbms_sql.bind_variable ( insert_gt, ':b_customer_name_h', p_customer_name_h);
Line: 760

          dbms_sql.bind_variable ( insert_gt, ':b_customer_no_l', p_customer_no_l);
Line: 763

          dbms_sql.bind_variable ( insert_gt, ':b_customer_no_h', p_customer_no_h);
Line: 767

      l_rows_inserted := dbms_sql.execute( insert_gt);
Line: 774

      UPDATE  ar_cash_remit_refs_interim cri
      SET     cri.receipt_reference_status = 'AR_AA_RULE_SET_INACTIVE'
      WHERE   cri.cash_receipt_id IN (
      SELECT  distinct cr.cash_receipt_id
      FROM    ar_cash_remit_refs_interim cri1,
              ar_cash_auto_rule_sets aca,
              ar_cash_receipts cr
      WHERE cr.cash_receipt_id = cri1.cash_receipt_id
      AND   cr.automatch_set_id = aca.automatch_set_id
      AND   (cr.receipt_date < NVL(aca.start_date, cr.receipt_date)
            OR cr.receipt_date > NVL(aca.end_date, to_date('31/12/4712','DD/MM/YYYY'))
            OR NVL(aca.active_flag, 'N') = 'N')
      )
      AND   cri.receipt_reference_status = 'AR_AM_NEW';
Line: 791

      UPDATE  ar_cash_remit_refs_interim
      SET     receipt_reference_status = 'AR_AA_RULE_SET_NOT_PASSED'
      WHERE   automatch_set_id IS NULL
      AND     receipt_reference_status = 'AR_AM_NEW';
Line: 796

      UPDATE  ar_cash_remit_refs_interim
      SET     receipt_reference_status = 'AR_AA_AMT_NOT_PASSED'
      WHERE   amount_applied IS NULL
      AND     amount_applied_from IS NULL
      AND     receipt_reference_status = 'AR_AM_NEW';
Line: 802

      /* * Delete Suggestions for the references that will be processed in *
         * the current run. This is to avoid duplicate recommendations     *
         * getting generated and to handle the cases where a refernce no is*
         * changed after the previous run. Refer bug 8396831               * */

      DELETE FROM ar_cash_reco_lines lines
      WHERE EXISTS (
      SELECT 'Suggestion Exists'
      FROM ar_cash_recos rec, ar_cash_remit_refs_interim ref
      WHERE rec.recommendation_id = lines.recommendation_id
      AND   rec.remit_reference_id = ref.remit_reference_id
      AND   ref.receipt_reference_status = 'AR_AM_NEW'
      );
Line: 816

      DELETE FROM ar_cash_recos rec
      WHERE EXISTS(
      SELECT 'Suggestion Exists'
      FROM ar_cash_remit_refs_interim ref
      WHERE rec.remit_reference_id = ref.remit_reference_id
      AND   ref.receipt_reference_status = 'AR_AM_NEW'
      );
Line: 873

      /* * AutoCash Application Process Completed. Now update the receipt_   *
         * reference_status for the unapplied references with either No Match*
         * Found or Suggestions found based on recommendations generated     * */
      UPDATE ar_cash_remit_refs_interim cri
      SET cri.receipt_reference_status = DECODE(
                   ( SELECT 'MATCH_FOUND'
                     FROM ar_cash_recos
                     WHERE remit_reference_id = cri.remit_reference_id
                     AND rownum = 1 ),'MATCH_FOUND','AR_AA_SUGG_FOUND','AR_AA_NO_MATCH')
      WHERE cri.receipt_reference_status = 'AR_AM_NEW';
Line: 884

      UPDATE  ar_cash_remit_refs crr
      SET     crr.receipt_reference_status = (SELECT cri.receipt_reference_status
      FROM    ar_cash_remit_refs_interim cri
      WHERE   crr.remit_reference_id = cri.remit_reference_id
      AND     cri.receipt_reference_status IN ('AR_AA_SUGG_FOUND', 'AR_AA_NO_MATCH', 'AR_AA_RULE_SET_INACTIVE', 'AR_AA_RULE_SET_NOT_PASSED', 'AR_AA_AMT_NOT_PASSED'))
      WHERE   crr.remit_reference_id IN (SELECT cri.remit_reference_id
      FROM    ar_cash_remit_refs_interim cri
      WHERE   crr.remit_reference_id = cri.remit_reference_id
      AND     cri.receipt_reference_status IN ('AR_AA_SUGG_FOUND', 'AR_AA_NO_MATCH', 'AR_AA_RULE_SET_INACTIVE', 'AR_AA_RULE_SET_NOT_PASSED', 'AR_AA_AMT_NOT_PASSED'))
      AND     crr.receipt_reference_status <> 'AR_AA_INV_APPLIED';
Line: 895

      /* * If a receipt has any unapplied remittance line, update the        *
         * receipt work_item_exception_reason with the exception reason      *
         * defined at the AutoMatchRule Setup                                * */
      UPDATE ar_cash_receipts_all cr
      SET WORK_ITEM_EXCEPTION_REASON =
      (SELECT exception_reason
      FROM ar_cash_auto_rule_sets
      WHERE automatch_set_id = cr.automatch_set_id)
      WHERE cash_receipt_id IN
      (SELECT distinct cash_receipt_id
      FROM ar_cash_remit_refs_interim cri
      WHERE receipt_reference_status IN ('AR_AA_NO_MATCH','AR_AA_SUGG_FOUND')
      );
Line: 947

 *   1. The check if a rule is active wrt receipt date is made while inserting
 *      recommendations.                                                     *
 *   2. Recommendations are inserted once per each automatch rule. Meaning if*
 *      a rule R1 is part of two sets S1, S2 and suppose we are processing S1*
 *      first, then for all the references that have either S1 or S2 as rule *
 *      sets recommendations for the rule R1 are generated while processing S1
 *      itself. So there is no need to insert recommendations again while    *
 *      processing S2. Hence whenever a rule is fetched for a rule set, first*
 *      check is made to see if the rule is already processed as part of any *
 *      other rule set.                                                      *
 * MODIFICATION HISTORY -  09/03/2009 - Created by AGHORAKA	     	           *
 *                                                                           *
 +===========================================================================*/

  PROCEDURE auto_apply_child( P_ERRBUF OUT NOCOPY VARCHAR2
                              , P_RETCODE OUT NOCOPY NUMBER
                              , p_worker_number IN NUMBER) IS
      CURSOR  auto_rule_set_cur(p_worker_number IN NUMBER) IS
          SELECT distinct automatch_set_id
          FROM   AR_CASH_REMIT_REFS_INTERIM
          WHERE  worker_number = p_worker_number
          AND    receipt_reference_status = 'AR_AM_NEW';
Line: 971

          SELECT  aca.automatch_id automatch_id,
                  aca.matching_option matching_option,
                  NVL(aca.use_matching_date, 'N') use_matching_date
          FROM    AR_CASH_AUTOMATCHES aca,
                  AR_CASH_AUTOMATCH_RULE_MAP acm
          WHERE   acm.automatch_set_id = p_automatch_set_id
          AND     aca.automatch_id = acm.automatch_id
          AND     NVL(aca.active_flag, 'N') = 'Y'
          ORDER BY acm.priority;
Line: 1015

                    insert_invoice_recos(l_automatch_id,
                                         l_use_matching_date,
                                         l_trans_format_str,
                                         l_rem_format_str,
                                         l_trans_float_str,
                                         l_rem_float_str,
                                         l_worker_number,
                                         p_request_id);
Line: 1024

                    insert_so_recos(l_automatch_id,
                                         l_use_matching_date,
                                         l_trans_format_str,
                                         l_rem_format_str,
                                         l_trans_float_str,
                                         l_rem_float_str,
                                         l_worker_number,
                                         p_request_id);
Line: 1033

                    insert_po_recos(l_automatch_id,
                                         l_use_matching_date,
                                         l_trans_format_str,
                                         l_rem_format_str,
                                         l_trans_float_str,
                                         l_rem_float_str,
                                         l_worker_number,
                                         p_request_id);
Line: 1042

                    insert_bfb_recos(l_automatch_id,
                                         l_use_matching_date,
                                         l_trans_format_str,
                                         l_rem_format_str,
                                         l_trans_float_str,
                                         l_rem_float_str,
                                         l_worker_number,
                                         p_request_id);
Line: 1051

                    insert_waybill_recos(l_automatch_id,
                                         l_use_matching_date,
                                         l_trans_format_str,
                                         l_rem_format_str,
                                         l_trans_float_str,
                                         l_rem_float_str,
                                         l_worker_number,
                                         p_request_id);
Line: 1060

                    insert_attribute_recos(l_automatch_id,
                                         l_use_matching_date,
                                         l_trans_format_str,
                                         l_rem_format_str,
                                         l_trans_float_str,
                                         l_rem_float_str,
                                         l_worker_number,
                                         substr(l_matching_option, 12),
                                         p_request_id);
Line: 1070

                    insert_contract_recos(l_automatch_id,
                                         l_use_matching_date,
                                         l_trans_format_str,
                                         l_rem_format_str,
                                         l_trans_float_str,
                                         l_rem_float_str,
                                         l_worker_number,
                                         p_request_id);
Line: 1079

                    insert_reference_recos(l_automatch_id,
                                         l_use_matching_date,
                                         l_trans_format_str,
                                         l_rem_format_str,
                                         l_trans_float_str,
                                         l_rem_float_str,
                                         l_worker_number,
                                         p_request_id);
Line: 1107

 *     INSERT_INVOICE_RECOS()                                                *
 * DESCRIPTION                                                               *
 *   Inserts recommendations for transaction numbers                         *
 * SCOPE - LOCAL                                                             *
 * ARGUMENTS                                                                 *
 *              IN  : p_automatch_id Automatch Rule Identifier               *
 *                    p_use_matching_date Use Matching Date [ALWAYS/For      *
 *                    Duplicates/NULL]                                       *
 *                    p_trans_format_str Transaction Number Format String    *
 *                    p_rem_format_str Reference Number Format String        *
 *                    p_worker_number Current Worker Number                  *
 *                    p_request_id Request ID                                *
 *              OUT : None                                                   *
 *                                                                           *
 * RETURNS      NONE                    				                             *
 * ALGORITHM                                                                 *
 *   1. For all open transactions satisfying all the setup conditions calculate
 *      the matching score of transaction number with the reference number   *
 *      given in the remittance lines (ar_cash_remit_refs_all)               *
 *   2. If match_score > suggested threshold value specified at the AutoMatch*
 *      setup, insert into ar_cash_recos, ar_cash_reco_lines as a recommenda *
 *      -tion.                                                               *
 * NOTES -                                                                   *
 *   1. Tables with _ALL is used in INSERT statement as multi-table insert is*
 *      not possible on secured synonyms (ar_cash_recos and ar_cash_reco_lines)
 *   2. If pay_unrelated_customer is set to 'Yes' or the reference/receipt is*
 *      unidentified then transactions for all the customers are considered. *
 *      Otherwise only the transactions related to the paying customer of the*
 *      receipt are considered.                                              *
 *   3. An invoice can have multiple installments; which means there is a    *
Line: 1147

  PROCEDURE insert_invoice_recos (p_automatch_id IN NUMBER
                                  , p_use_matching_date IN VARCHAR2
                                  , p_trans_format_str IN VARCHAR2
                                  , p_rem_format_str  IN VARCHAR2
                                  , p_trans_float_str IN VARCHAR2
                                  , p_rem_float_str IN VARCHAR2
                                  , p_worker_number IN NUMBER
                                  , p_request_id IN NUMBER) IS
  CURSOR select_recos IS
        SELECT         ref.remit_reference_id remit_reference_id,
                       ref.amount_applied ref_amount_applied,
                       ref.amount_applied_from ref_amount_applied_from,
                       ref.trans_to_receipt_rate ref_trans_to_receipt_rate,
                       ref.cash_receipt_id cash_receipt_id,
                       cr.pay_from_customer pay_from_customer,
                       cr.customer_site_use_id cr_customer_site_use_id,
                       ps.customer_trx_id customer_trx_id,
                       ps.customer_id customer_id,
                       ps.customer_site_use_id customer_site_use_id,
                       ps.trx_number resolved_matching_number,
                       ps.terms_sequence_number terms_sequence_number,
                       decode(am.match_date_by,
                        'INT_HDR_ATT1', fnd_conc_date.string_to_date(trx.interface_header_attribute1),
                        'INT_HDR_ATT10', fnd_conc_date.string_to_date(trx.interface_header_attribute10),
                        'INT_HDR_ATT11', fnd_conc_date.string_to_date(trx.interface_header_attribute11),
                        'INT_HDR_ATT12', fnd_conc_date.string_to_date(trx.interface_header_attribute12),
                        'INT_HDR_ATT13', fnd_conc_date.string_to_date(trx.interface_header_attribute13),
                        'INT_HDR_ATT14', fnd_conc_date.string_to_date(trx.interface_header_attribute14),
                        'INT_HDR_ATT15', fnd_conc_date.string_to_date(trx.interface_header_attribute15),
                        'INT_HDR_ATT2', fnd_conc_date.string_to_date(trx.interface_header_attribute2),
                        'INT_HDR_ATT3', fnd_conc_date.string_to_date(trx.interface_header_attribute3),
                        'INT_HDR_ATT4', fnd_conc_date.string_to_date(trx.interface_header_attribute4),
                        'INT_HDR_ATT5', fnd_conc_date.string_to_date(trx.interface_header_attribute5),
                        'INT_HDR_ATT6', fnd_conc_date.string_to_date(trx.interface_header_attribute6),
                        'INT_HDR_ATT7', fnd_conc_date.string_to_date(trx.interface_header_attribute7),
                        'INT_HDR_ATT8', fnd_conc_date.string_to_date(trx.interface_header_attribute8),
                        'INT_HDR_ATT9', fnd_conc_date.string_to_date(trx.interface_header_attribute9),
                        'PURCH_ORDER_DATE', trx.purchase_order_date,
                        'TRANS_DATE', trx.trx_date,
                        NULL)  resolved_matching_date,
                       ps.trx_date trx_date,
                       ps.class resolved_matching_class,
                       ps.invoice_currency_code resolved_match_currency,
                       ps.amount_due_original amount_due_original,
                       ps.amount_due_remaining amount_due_remaining,
                       ps.discount_taken_earned discount_taken_earned,
                       ps.discount_taken_unearned discount_taken_unearned,
                       ARPCURR.CURRROUND(ps.amount_due_remaining, ps.invoice_currency_code ) amount_applied,
                       ROUND(NVL(ref.trans_to_receipt_rate,
                                 DECODE(ps.invoice_currency_code, cr.currency_code, NULL,
                                           NVL( ARP_AUTOAPPLY_API.get_cross_curr_rate(
                                                      ref.amount_applied,
                                                      ref.amount_applied_from,
                                                      ps.invoice_currency_code,
                                                      cr.currency_code
                                                      )
                                                , GL_CURRENCY_API.GET_RATE_SQL(
                                                        ps.invoice_currency_code,
                                                        cr.currency_code,
                                                        cr.receipt_date,
                                                       arp_standard.sysparm.CROSS_CURRENCY_RATE_TYPE )
                                              )
                                       )
                                ),38) trans_to_receipt_rate,
                       NULL amount_applied_from, -- will be calculated later for xcurr app.
                       ps.payment_schedule_id payment_schedule_id,
                       NULL cons_inv_id,         -- Not used here. Useful for BFBs. So null value selected.
                       UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(ps.trx_number, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                          REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) match_score_value,
                       ps.org_id org_id,
                       ps.term_id term_id,
                       am.automatch_id automatch_id,
                       am.use_matching_date use_matching_date,
                       am.use_matching_amount use_matching_amount,
                       am.auto_match_threshold auto_match_threshold,
                       amp.priority priority,
                       cr.currency_code receipt_currency_code,
                       cr.receipt_date receipt_date,
                       ctt.allow_overapplication_flag allow_overapplication_flag,
                       tr.partial_discount_flag partial_discount_flag,
                       RANK() OVER (PARTITION BY ps.trx_number, ps.customer_site_use_id,
                                    ref.remit_reference_id, ps.customer_trx_id
                                    ORDER BY ps.payment_schedule_id) AS  reco_num
        FROM           ar_cash_automatches am,
                       ar_cash_automatch_rule_map amp,
                       ar_cash_remit_refs_interim ref,
                       ar_cash_receipts cr,
                       ar_payment_schedules ps,
                       ra_customer_trx trx,
                       ra_cust_trx_types ctt,
                       ra_terms tr
        WHERE          am.automatch_id               = p_automatch_id
        AND            amp.automatch_id              = am.automatch_id
        AND            amp.automatch_set_id          = ref.automatch_set_id
        AND            ref.worker_number             = p_worker_number
        AND            ref.receipt_reference_status  = 'AR_AM_NEW'
        AND            cr.cash_receipt_id            = ref.cash_receipt_id
        AND            cr.receipt_date BETWEEN NVL(am.start_date, cr.receipt_date)
                                       AND NVL(am.end_date, to_date('31/12/4712','DD/MM/YYYY'))
        AND            ps.trx_number IS NOT NULL
        AND            ps.selected_for_receipt_batch_id IS NULL
        AND            UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(ps.trx_number, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                          REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) >= am.sugg_match_threshold
        AND            ps.class                     NOT IN ('PMT', 'GUAR')
        AND            ps.payment_schedule_id        > 0
        AND            ps.status                    = 'OP'
        AND            ps.terms_sequence_number     = NVL(ref.installment_reference,
                                                          ps.terms_sequence_number)
        AND            ps.customer_id IN (SELECT  DECODE(ARP_STANDARD.sysparm.pay_unrelated_invoices_flag,'Y', ps.customer_id,
                                                               NVL(cr.pay_from_customer, ps.customer_id))
                                          FROM    DUAL
                                          UNION   ALL
                                          SELECT  related_cust_account_id
                                          FROM    hz_cust_acct_relate_all rel
                                          WHERE   rel.cust_account_id = cr.pay_from_customer
                                          AND     rel.bill_to_flag    = 'Y'
                                          AND     rel.status          = 'A'
                                          AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y'
                                          UNION   ALL
                                          SELECT  rel.related_cust_account_id
                                          FROM    ar_paying_relationships_v rel,
                                                  hz_cust_accounts acc
                                          WHERE   acc.cust_account_id = cr.pay_from_customer
                                          AND     acc.party_id        = rel.party_id
                                          AND     cr.receipt_date   >= effective_start_date
                                          AND     cr.receipt_date   <= effective_end_date
                                          AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y' )
        AND           trx.customer_trx_id           = ps.customer_trx_id
        AND           tr.term_id(+)                 = ps.term_id
        AND           ps.cust_trx_type_id           = ctt.cust_trx_type_id;
Line: 1277

    l_selected_recos              selected_recos_table;
Line: 1278

    l_current_reco                selected_recos_table;
Line: 1285

          log('arp_autoapply_api.insert_invoice_recos(+)');
Line: 1289

    OPEN select_recos;
Line: 1291

      FETCH select_recos BULK COLLECT INTO l_selected_recos LIMIT G_MAX_ARRAY_SIZE;
Line: 1292

      log('Count : '||l_selected_recos.COUNT);
Line: 1293

      IF l_selected_recos.COUNT = 0 THEN
        IF l_current_reco.count > 0 THEN
          process_single_reco(l_current_reco
                              , 'INVOICE');
Line: 1297

          l_current_reco.DELETE;
Line: 1298

          insert_recos(p_request_id);
Line: 1303

      l_current_fetch_count := l_selected_recos.COUNT;
Line: 1307

          insert_recos(p_request_id);
Line: 1316

            copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 1319

            IF l_current_reco(l_current_reco_line).reco_num < l_selected_recos(l_outer_index).reco_num THEN
              log('Else-If');
Line: 1321

              copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 1333

          insert_recos(p_request_id);
Line: 1339

        l_current_reco.DELETE;
Line: 1344

      log('arp_autoapply_api.insert_invoice_recos(-)');
Line: 1348

          log('Exception from arp_autoapply_api.insert_invoice_recos');
Line: 1351

  END insert_invoice_recos;
Line: 1355

 *     INSERT_PO_RECOS()                                                     *
 * DESCRIPTION                                                               *
 *   Inserts recommendations for Purchase Orders                             *
 * SCOPE - LOCAL                                                             *
 * ARGUMENTS                                                                 *
 *              IN  : p_automatch_id Automatch Rule Identifier               *
 *                    p_use_matching_date Use Matching Date [ALWAYS/For      *
 *                    Duplicates/NULL]                                       *
 *                    p_trans_format_str Transaction Number Format String    *
 *                    p_rem_format_str Reference Number Format String        *
 *                    p_worker_number Current Worker Number                  *
 *                    p_request_id Request ID                                *
 *              OUT : None                                                   *
 *                                                                           *
 * RETURNS      NONE                    				                             *
 * ALGORITHM                                                                 *
 *   1. For all open POs satisfying all the setup conditions calculate       *
 *      the matching score of purchase order number with the reference number*
 *      given in the remittance lines (ar_cash_remit_refs_all)               *
 *   2. If match_score > suggested threshold value specified at the AutoMatch*
 *      setup, insert into ar_cash_recos, ar_cash_reco_lines as a recommenda *
 *      -tion.                                                               *
 * NOTES -                                                                   *
 *   1. Tables with _ALL is used in INSERT statement as multi-table insert is*
 *      not possible on secured synonyms (ar_cash_recos and ar_cash_reco_lines)
 *   2. If pay_unrelated_customer is set to 'Yes' or the reference/receipt is*
 *      unidentified then transactions for all the customers are considered. *
 *      Otherwise only the transactions related to the paying customer of the*
 *      receipt are considered.                                              *
 *   3. A PO can have multiple invoices; which means there is a possibility  *
Line: 1395

  PROCEDURE insert_po_recos (p_automatch_id IN NUMBER
                            , p_use_matching_date IN VARCHAR2
                            , p_trans_format_str IN VARCHAR2
                            , p_rem_format_str  IN VARCHAR2
                            , p_trans_float_str IN VARCHAR2
                            , p_rem_float_str IN VARCHAR2
                            , p_worker_number IN NUMBER
                            , p_request_id IN NUMBER) IS
  CURSOR select_recos IS
    SELECT         ref.remit_reference_id remit_reference_id,
                         ref.amount_applied ref_amount_applied,
                         ref.amount_applied_from ref_amount_applied_from,
                         ref.trans_to_receipt_rate ref_trans_to_receipt_rate,
                         ref.cash_receipt_id cash_receipt_id,
                         cr.pay_from_customer pay_from_customer,
                         cr.customer_site_use_id cr_customer_site_use_id,
                         ps.customer_trx_id customer_trx_id,
                         ps.customer_id,
                         ps.customer_site_use_id customer_site_use_id,
                         trx.purchase_order resolved_matching_number,
                         ps.terms_sequence_number terms_sequence_number,
                         decode(am.match_date_by,
                                'INT_HDR_ATT1', fnd_conc_date.string_to_date(trx.interface_header_attribute1),
                                'INT_HDR_ATT10', fnd_conc_date.string_to_date(trx.interface_header_attribute10),
                                'INT_HDR_ATT11', fnd_conc_date.string_to_date(trx.interface_header_attribute11),
                                'INT_HDR_ATT12', fnd_conc_date.string_to_date(trx.interface_header_attribute12),
                                'INT_HDR_ATT13', fnd_conc_date.string_to_date(trx.interface_header_attribute13),
                                'INT_HDR_ATT14', fnd_conc_date.string_to_date(trx.interface_header_attribute14),
                                'INT_HDR_ATT15', fnd_conc_date.string_to_date(trx.interface_header_attribute15),
                                'INT_HDR_ATT2', fnd_conc_date.string_to_date(trx.interface_header_attribute2),
                                'INT_HDR_ATT3', fnd_conc_date.string_to_date(trx.interface_header_attribute3),
                                'INT_HDR_ATT4', fnd_conc_date.string_to_date(trx.interface_header_attribute4),
                                'INT_HDR_ATT5', fnd_conc_date.string_to_date(trx.interface_header_attribute5),
                                'INT_HDR_ATT6', fnd_conc_date.string_to_date(trx.interface_header_attribute6),
                                'INT_HDR_ATT7', fnd_conc_date.string_to_date(trx.interface_header_attribute7),
                                'INT_HDR_ATT8', fnd_conc_date.string_to_date(trx.interface_header_attribute8),
                                'INT_HDR_ATT9', fnd_conc_date.string_to_date(trx.interface_header_attribute9),
                                'PURCH_ORDER_DATE', trx.purchase_order_date,
                                'TRANS_DATE', trx.trx_date, NULL)  resolved_matching_date,
                         ps.trx_date trx_date,
                         ps.class resolved_matching_class,
                         trx.invoice_currency_code resolved_match_currency,
                         ps.amount_due_original amount_due_original,
                         ps.amount_due_remaining amount_due_remaining,
                         ps.discount_taken_earned discount_taken_earned,
                         ps.discount_taken_unearned discount_taken_unearned,
                         ARPCURR.CURRROUND(ps.amount_due_remaining, ps.invoice_currency_code ) amount_applied,
                         ROUND(NVL(ref.trans_to_receipt_rate,
                                 DECODE(ps.invoice_currency_code, cr.currency_code, NULL,
                                           NVL( ARP_AUTOAPPLY_API.get_cross_curr_rate(
                                                      ref.amount_applied,
                                                      ref.amount_applied_from,
                                                      ps.invoice_currency_code,
                                                      cr.currency_code
                                                      )
                                                , GL_CURRENCY_API.GET_RATE_SQL(
                                                        ps.invoice_currency_code,
                                                        cr.currency_code,
                                                        cr.receipt_date,
                                                       arp_standard.sysparm.CROSS_CURRENCY_RATE_TYPE )
                                              )
                                       )
                                ),38) trans_to_receipt_rate,
                         NULL amount_applied_from, -- will be calculated later for xcurr app.
                         ps.payment_schedule_id,
                         NULL cons_inv_id,         -- Not used here. Useful for BFBs. So null value selected.
                         UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(trx.purchase_order, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                            REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) match_score_value,
                         ps.org_id,
                         ps.term_id term_id,
                         am.automatch_id,
                         am.use_matching_date use_matching_date,
                         am.use_matching_amount use_matching_amount,
                         am.auto_match_threshold auto_match_threshold,
                         amp.priority priority,
                         cr.currency_code receipt_currency_code,
                         cr.receipt_date,
                         ctt.allow_overapplication_flag allow_overapplication_flag,
                         tr.partial_discount_flag partial_discount_flag,
                         RANK() OVER (PARTITION BY trx.purchase_order, ps.customer_site_use_id,
                                      ref.remit_reference_id, ps.customer_trx_id
                              ORDER BY ps.payment_schedule_id) AS  reco_num
          FROM           ar_cash_automatches am,
                         ar_cash_automatch_rule_map amp,
                         ar_cash_remit_refs_interim ref,
                         ar_cash_receipts cr,
                         ra_customer_trx trx,
                         ar_payment_schedules ps,
                         ra_cust_trx_types ctt,
                         ra_terms tr
          WHERE          am.automatch_id               = p_automatch_id
          AND            amp.automatch_id              = am.automatch_id
          AND            amp.automatch_set_id          = ref.automatch_set_id
          AND            ref.worker_number             = p_worker_number
          AND            ref.receipt_reference_status  = 'AR_AM_NEW'
          AND            cr.cash_receipt_id            = ref.cash_receipt_id
          AND            cr.receipt_date BETWEEN NVL(am.start_date, cr.receipt_date)
                                         AND NVL(am.end_date, to_date('31/12/4712','DD/MM/YYYY'))
          AND            trx.purchase_order              IS NOT NULL
          AND            UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(trx.purchase_order, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                            REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) >= am.sugg_match_threshold
          AND            ps.customer_trx_id            = trx.customer_trx_id
          AND            ps.selected_for_receipt_batch_id IS NULL
          AND            ps.class                     NOT IN ('PMT', 'GUAR')
          AND            ps.payment_schedule_id        > 0
          AND            ps.status                      = 'OP'
          AND            ps.customer_id IN (SELECT  DECODE(ARP_STANDARD.sysparm.pay_unrelated_invoices_flag,'Y', ps.customer_id,
                                                                 NVL(cr.pay_from_customer, ps.customer_id))
                                            FROM    DUAL
                                            UNION   ALL
                                            SELECT  related_cust_account_id
                                            FROM    hz_cust_acct_relate_all rel
                                            WHERE   rel.cust_account_id = cr.pay_from_customer
                                            AND     rel.bill_to_flag    = 'Y'
                                            AND     rel.status          = 'A'
                                            AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y'
                                            UNION   ALL
                                            SELECT  rel.related_cust_account_id
                                            FROM    ar_paying_relationships_v rel,
                                                    hz_cust_accounts acc
                                            WHERE   acc.cust_account_id = cr.pay_from_customer
                                            AND     acc.party_id        = rel.party_id
                                            AND     cr.receipt_date    >= effective_start_date
                                            AND     cr.receipt_date    <= effective_end_date
                                            AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y' )
        AND           tr.term_id(+)                 = ps.term_id
        AND           ps.cust_trx_type_id           = ctt.cust_trx_type_id;
Line: 1522

    l_selected_recos              selected_recos_table;
Line: 1523

    l_current_reco                selected_recos_table;
Line: 1530

        log('arp_autoapply_api.insert_po_recos(+)');
Line: 1532

    OPEN select_recos;
Line: 1534

      FETCH select_recos BULK COLLECT INTO l_selected_recos LIMIT G_MAX_ARRAY_SIZE;
Line: 1535

      log('Count : '||l_selected_recos.COUNT);
Line: 1536

      IF l_selected_recos.COUNT = 0 THEN
        IF l_current_reco.count > 0 THEN
          process_single_reco(l_current_reco
                              , 'PURCHASE ORDER');
Line: 1540

          l_current_reco.DELETE;
Line: 1541

          insert_recos(p_request_id);
Line: 1546

      l_current_fetch_count := l_selected_recos.COUNT;
Line: 1550

          insert_recos(p_request_id);
Line: 1558

            copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 1561

            IF l_current_reco(l_current_reco_line).reco_num < l_selected_recos(l_outer_index).reco_num THEN
              log('Else-If');
Line: 1563

              copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 1574

          insert_recos(p_request_id);
Line: 1580

        l_current_reco.DELETE;
Line: 1584

      log('arp_autoapply_api.insert_po_recos(-)');
Line: 1588

          log('Exception from arp_autoapply_api.insert_po_recos');
Line: 1591

  END insert_po_recos;
Line: 1595

 *     INSERT_SO_RECOS()                                                     *
 * DESCRIPTION                                                               *
 *   Inserts recommendations for Sales Orders                                *
 * SCOPE - LOCAL                                                             *
 * ARGUMENTS                                                                 *
 *              IN  : p_automatch_id Automatch Rule Identifier               *
 *                    p_use_matching_date Use Matching Date [ALWAYS/For      *
 *                    Duplicates/NULL]                                       *
 *                    p_trans_format_str Transaction Number Format String    *
 *                    p_rem_format_str Reference Number Format String        *
 *                    p_worker_number Current Worker Number                  *
 *                    p_request_id Request ID                                *
 *              OUT : None                                                   *
 *                                                                           *
 * RETURNS      NONE                    				                             *
 * ALGORITHM                                                                 *
 *   1. For all open SOs satisfying all the setup conditions calculate       *
 *      the matching score of sales order number with the reference number   *
 *      given in the remittance lines (ar_cash_remit_refs_all)               *
 *   2. If match_score > suggested threshold value specified at the AutoMatch*
 *      setup, insert into ar_cash_recos, ar_cash_reco_lines as a recommenda *
 *      -tion.                                                               *
 * NOTES -                                                                   *
 *   1. Tables with _ALL is used in INSERT statement as multi-table insert is*
 *      not possible on secured synonyms (ar_cash_recos and ar_cash_reco_lines)
 *   2. If pay_unrelated_customer is set to 'Yes' or the reference/receipt is*
 *      unidentified then transactions for all the customers are considered. *
 *      Otherwise only the transactions related to the paying customer of the*
 *      receipt are considered.                                              *
 *   3. A SO can have multiple invoices; which means there is a possibility  *
Line: 1635

  PROCEDURE insert_so_recos (p_automatch_id IN NUMBER
                            , p_use_matching_date IN VARCHAR2
                            , p_trans_format_str IN VARCHAR2
                            , p_rem_format_str  IN VARCHAR2
                            , p_trans_float_str IN VARCHAR2
                            , p_rem_float_str IN VARCHAR2
                            , p_worker_number IN NUMBER
                            , p_request_id IN NUMBER) IS
        CURSOR select_recos IS
      SELECT             ref.remit_reference_id remit_reference_id,
                         ref.amount_applied ref_amount_applied,
                         ref.amount_applied_from ref_amount_applied_from,
                         ref.trans_to_receipt_rate ref_trans_to_receipt_rate,
                         ref.cash_receipt_id cash_receipt_id,
                         cr.pay_from_customer pay_from_customer,
                         cr.customer_site_use_id cr_customer_site_use_id,
                         ps.customer_trx_id customer_trx_id,
                         ps.customer_id,
                         ps.customer_site_use_id customer_site_use_id,
                         lin.sales_order resolved_matching_number,
                         ps.terms_sequence_number terms_sequence_number,
                         decode(am.match_date_by,
                          'INT_HDR_ATT1', fnd_conc_date.string_to_date(trx.interface_header_attribute1),
                          'INT_HDR_ATT10', fnd_conc_date.string_to_date(trx.interface_header_attribute10),
                          'INT_HDR_ATT11', fnd_conc_date.string_to_date(trx.interface_header_attribute11),
                          'INT_HDR_ATT12', fnd_conc_date.string_to_date(trx.interface_header_attribute12),
                          'INT_HDR_ATT13', fnd_conc_date.string_to_date(trx.interface_header_attribute13),
                          'INT_HDR_ATT14', fnd_conc_date.string_to_date(trx.interface_header_attribute14),
                          'INT_HDR_ATT15', fnd_conc_date.string_to_date(trx.interface_header_attribute15),
                          'INT_HDR_ATT2', fnd_conc_date.string_to_date(trx.interface_header_attribute2),
                          'INT_HDR_ATT3', fnd_conc_date.string_to_date(trx.interface_header_attribute3),
                          'INT_HDR_ATT4', fnd_conc_date.string_to_date(trx.interface_header_attribute4),
                          'INT_HDR_ATT5', fnd_conc_date.string_to_date(trx.interface_header_attribute5),
                          'INT_HDR_ATT6', fnd_conc_date.string_to_date(trx.interface_header_attribute6),
                          'INT_HDR_ATT7', fnd_conc_date.string_to_date(trx.interface_header_attribute7),
                          'INT_HDR_ATT8', fnd_conc_date.string_to_date(trx.interface_header_attribute8),
                          'INT_HDR_ATT9', fnd_conc_date.string_to_date(trx.interface_header_attribute9),
                          'PURCH_ORDER_DATE', trx.purchase_order_date,
                          'TRANS_DATE', trx.trx_date,
                          NULL)  resolved_matching_date,
                         ps.trx_date trx_date,
                         ps.class resolved_matching_class,
                         ps.invoice_currency_code resolved_match_currency,
                         ps.amount_due_original amount_due_original,
                         ps.amount_due_remaining amount_due_remaining,
                         ps.discount_taken_earned discount_taken_earned,
                         ps.discount_taken_unearned discount_taken_unearned,
                         ARPCURR.CURRROUND(ps.amount_due_remaining, ps.invoice_currency_code ) amount_applied,
                         ROUND(NVL(ref.trans_to_receipt_rate,
                                   DECODE(ps.invoice_currency_code, cr.currency_code, NULL,
                                             NVL( ARP_AUTOAPPLY_API.get_cross_curr_rate(
                                                        ref.amount_applied,
                                                        ref.amount_applied_from,
                                                        ps.invoice_currency_code,
                                                        cr.currency_code
                                                        )
                                                  , GL_CURRENCY_API.GET_RATE_SQL(
                                                          ps.invoice_currency_code,
                                                          cr.currency_code,
                                                          cr.receipt_date,
                                                         arp_standard.sysparm.CROSS_CURRENCY_RATE_TYPE )
                                                )
                                         )
                                  ),38) trans_to_receipt_rate,
                         NULL amount_applied_from, -- will be calculated later for xcurr app.
                         ps.payment_schedule_id payment_schedule_id,
                         NULL cons_inv_id,         -- Not used here. Useful for BFBs. So null value selected.
                         UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(lin.sales_order, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                            REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) match_score_value,
                         ps.org_id,
                         ps.term_id term_id,
                         am.automatch_id automatch_id,
                         am.use_matching_date use_matching_date,
                         am.use_matching_amount use_matching_amount,
                         am.auto_match_threshold auto_match_threshold,
                         amp.priority priority,
                         cr.currency_code receipt_currency_code,
                         cr.receipt_date receipt_date,
                         ctt.allow_overapplication_flag allow_overapplication_flag,
                         tr.partial_discount_flag partial_discount_flag,
                         RANK() OVER (PARTITION BY lin.sales_order, ps.customer_site_use_id,
                                      ref.remit_reference_id, ps.customer_trx_id
                              ORDER BY ps.payment_schedule_id) AS  reco_num
          FROM           ar_cash_automatches am,
                         ar_cash_automatch_rule_map amp,
                         ar_cash_remit_refs_interim ref,
                         ar_cash_receipts cr,
                         ra_customer_trx_lines lin,
                         ar_payment_schedules ps,
                         ra_customer_trx trx,
                         ra_cust_trx_types ctt,
                        ra_terms tr
          WHERE          am.automatch_id               = p_automatch_id
          AND            amp.automatch_id              = am.automatch_id
          AND            amp.automatch_set_id          = ref.automatch_set_id
          AND            ref.worker_number             = p_worker_number
          AND            ref.receipt_reference_status  = 'AR_AM_NEW'
          AND            cr.cash_receipt_id            = ref.cash_receipt_id
          AND            cr.receipt_date BETWEEN NVL(am.start_date, cr.receipt_date)
                                         AND NVL(am.end_date, to_date('31/12/4712','DD/MM/YYYY'))
          AND            lin.interface_line_context   <> 'OKS CONTRACTS'
          AND            lin.sales_order                 IS NOT NULL
          AND            UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(lin.sales_order, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                            REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) >= am.sugg_match_threshold
          AND            ps.customer_trx_id            = lin.customer_trx_id
          AND            trx.customer_trx_id           = ps.customer_trx_id
                        /* Added to fetch values from Header Attributes */
          AND            ps.selected_for_receipt_batch_id IS NULL
          AND            ps.class                     NOT IN ('PMT', 'GUAR')
          AND            ps.payment_schedule_id        > 0
          AND            ps.status                      = 'OP'
          AND            ps.customer_id IN (SELECT  DECODE(ARP_STANDARD.sysparm.pay_unrelated_invoices_flag,'Y', ps.customer_id,
                                                                 NVL(cr.pay_from_customer, ps.customer_id))
                                            FROM    DUAL
                                            UNION   ALL
                                            SELECT  related_cust_account_id
                                            FROM    hz_cust_acct_relate_all rel
                                            WHERE   rel.cust_account_id = cr.pay_from_customer
                                            AND     rel.bill_to_flag    = 'Y'
                                            AND     rel.status          = 'A'
                                            AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y'
                                            UNION   ALL
                                            SELECT  rel.related_cust_account_id
                                            FROM    ar_paying_relationships_v rel,
                                                    hz_cust_accounts acc
                                            WHERE   acc.cust_account_id = cr.pay_from_customer
                                            AND     acc.party_id        = rel.party_id
                                            AND     cr.receipt_date    >= effective_start_date
                                            AND     cr.receipt_date    <= effective_end_date
                                            AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y' )
          AND           tr.term_id(+)                 = ps.term_id
          AND           ps.cust_trx_type_id           = ctt.cust_trx_type_id;
Line: 1767

    l_selected_recos              selected_recos_table;
Line: 1768

    l_current_reco                selected_recos_table;
Line: 1775

      log('arp_autoapply_api.insert_so_recos(+)');
Line: 1777

    OPEN select_recos;
Line: 1779

      FETCH select_recos BULK COLLECT INTO l_selected_recos LIMIT G_MAX_ARRAY_SIZE;
Line: 1780

      log('Count : '||l_selected_recos.COUNT);
Line: 1781

      IF l_selected_recos.COUNT = 0 THEN
        IF l_current_reco.count > 0 THEN
          process_single_reco(l_current_reco
                              , 'SALES ORDER');
Line: 1785

          l_current_reco.DELETE;
Line: 1786

          insert_recos(p_request_id);
Line: 1791

      l_current_fetch_count := l_selected_recos.COUNT;
Line: 1795

          insert_recos(p_request_id);
Line: 1803

            copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 1806

            IF l_current_reco(l_current_reco_line).reco_num < l_selected_recos(l_outer_index).reco_num THEN
              copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 1818

          insert_recos(p_request_id);
Line: 1824

        l_current_reco.DELETE;
Line: 1828

      log('arp_autoapply_api.insert_so_recos(-)');
Line: 1832

          log('Exception from arp_autoapply_api.insert_so_recos');
Line: 1835

  END insert_so_recos;
Line: 1839

 *     INSERT_CONTRACT_RECOS()                                               *
 * DESCRIPTION                                                               *
 *   Inserts recommendations for Sales Contracts                             *
 * SCOPE - LOCAL                                                             *
 * ARGUMENTS                                                                 *
 *              IN  : p_automatch_id Automatch Rule Identifier               *
 *                    p_use_matching_date Use Matching Date [ALWAYS/For      *
 *                    Duplicates/NULL]                                       *
 *                    p_trans_format_str Transaction Number Format String    *
 *                    p_rem_format_str Reference Number Format String        *
 *                    p_worker_number Current Worker Number                  *
 *                    p_request_id Request ID                                *
 *              OUT : None                                                   *
 *                                                                           *
 * RETURNS      NONE                    				                             *
 * ALGORITHM                                                                 *
 *   1. For all open contracts satisfying all the setup conditions calculate *
 *      the matching score of contract number with the reference number   *
 *      given in the remittance lines (ar_cash_remit_refs_all)               *
 *   2. If match_score > suggested threshold value specified at the AutoMatch*
 *      setup, insert into ar_cash_recos, ar_cash_reco_lines as a recommenda *
 *      -tion.                                                               *
 * NOTES -                                                                   *
 *   1. Tables with _ALL is used in INSERT statement as multi-table insert is*
 *      not possible on secured synonyms (ar_cash_recos and ar_cash_reco_lines)
 *   2. If pay_unrelated_customer is set to 'Yes' or the reference/receipt is*
 *      unidentified then transactions for all the customers are considered. *
 *      Otherwise only the transactions related to the paying customer of the*
 *      receipt are considered.                                              *
 *   3. A Contract can have multiple invoices;which means there is a possibility
Line: 1879

  PROCEDURE insert_contract_recos (p_automatch_id IN NUMBER
                                  , p_use_matching_date IN VARCHAR2
                                  , p_trans_format_str IN VARCHAR2
                                  , p_rem_format_str  IN VARCHAR2
                                  , p_trans_float_str IN VARCHAR2
                                  , p_rem_float_str IN VARCHAR2
                                  , p_worker_number IN NUMBER
                                  , p_request_id IN NUMBER) IS
        CURSOR select_recos IS
      SELECT             ref.remit_reference_id remit_reference_id,
                         ref.amount_applied ref_amount_applied,
                         ref.amount_applied_from ref_amount_applied_from,
                         ref.trans_to_receipt_rate ref_trans_to_receipt_rate,
                         ref.cash_receipt_id cash_receipt_id,
                         cr.pay_from_customer pay_from_customer,
                         cr.customer_site_use_id cr_customer_site_use_id,
                         ps.customer_trx_id customer_trx_id,
                         ps.customer_id,
                         ps.customer_site_use_id customer_site_use_id,
                         lin.sales_order resolved_matching_number,
                         ps.terms_sequence_number terms_sequence_number,
                         decode(am.match_date_by,
                          'INT_HDR_ATT1', fnd_conc_date.string_to_date(trx.interface_header_attribute1),
                          'INT_HDR_ATT10', fnd_conc_date.string_to_date(trx.interface_header_attribute10),
                          'INT_HDR_ATT11', fnd_conc_date.string_to_date(trx.interface_header_attribute11),
                          'INT_HDR_ATT12', fnd_conc_date.string_to_date(trx.interface_header_attribute12),
                          'INT_HDR_ATT13', fnd_conc_date.string_to_date(trx.interface_header_attribute13),
                          'INT_HDR_ATT14', fnd_conc_date.string_to_date(trx.interface_header_attribute14),
                          'INT_HDR_ATT15', fnd_conc_date.string_to_date(trx.interface_header_attribute15),
                          'INT_HDR_ATT2', fnd_conc_date.string_to_date(trx.interface_header_attribute2),
                          'INT_HDR_ATT3', fnd_conc_date.string_to_date(trx.interface_header_attribute3),
                          'INT_HDR_ATT4', fnd_conc_date.string_to_date(trx.interface_header_attribute4),
                          'INT_HDR_ATT5', fnd_conc_date.string_to_date(trx.interface_header_attribute5),
                          'INT_HDR_ATT6', fnd_conc_date.string_to_date(trx.interface_header_attribute6),
                          'INT_HDR_ATT7', fnd_conc_date.string_to_date(trx.interface_header_attribute7),
                          'INT_HDR_ATT8', fnd_conc_date.string_to_date(trx.interface_header_attribute8),
                          'INT_HDR_ATT9', fnd_conc_date.string_to_date(trx.interface_header_attribute9),
                          'PURCH_ORDER_DATE', trx.purchase_order_date,
                          'TRANS_DATE', trx.trx_date,
                          NULL)  resolved_matching_date,
                         ps.trx_date trx_date,
                         ps.class resolved_matching_class,
                         ps.invoice_currency_code resolved_match_currency,
                         ps.amount_due_original amount_due_original,
                         ps.amount_due_remaining amount_due_remaining,
                         ps.discount_taken_earned discount_taken_earned,
                         ps.discount_taken_unearned discount_taken_unearned,
                         ARPCURR.CURRROUND(ps.amount_due_remaining, ps.invoice_currency_code ) amount_applied,
                         ROUND(NVL(ref.trans_to_receipt_rate,
                                   DECODE(ps.invoice_currency_code, cr.currency_code, NULL,
                                             NVL( ARP_AUTOAPPLY_API.get_cross_curr_rate(
                                                        ref.amount_applied,
                                                        ref.amount_applied_from,
                                                        ps.invoice_currency_code,
                                                        cr.currency_code
                                                        )
                                                  , GL_CURRENCY_API.GET_RATE_SQL(
                                                          ps.invoice_currency_code,
                                                          cr.currency_code,
                                                          cr.receipt_date,
                                                         arp_standard.sysparm.CROSS_CURRENCY_RATE_TYPE )
                                                )
                                         )
                                  ),38) trans_to_receipt_rate,
                         NULL amount_applied_from, -- will be calculated later for xcurr app.
                         ps.payment_schedule_id payment_schedule_id,
                         NULL cons_inv_id,         -- Not used here. Useful for BFBs. So null value selected.
                         UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(lin.sales_order, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                            REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) match_score_value,
                         ps.org_id,
                         ps.term_id term_id,
                         am.automatch_id automatch_id,
                         am.use_matching_date use_matching_date,
                         am.use_matching_amount use_matching_amount,
                         am.auto_match_threshold auto_match_threshold,
                         amp.priority priority,
                         cr.currency_code receipt_currency_code,
                         cr.receipt_date receipt_date,
                         ctt.allow_overapplication_flag allow_overapplication_flag,
                         tr.partial_discount_flag partial_discount_flag,
                         RANK() OVER (PARTITION BY lin.sales_order, ps.customer_site_use_id,
                                      ref.remit_reference_id, ps.customer_trx_id
                              ORDER BY ps.payment_schedule_id) AS  reco_num
          FROM           ar_cash_automatches am,
                         ar_cash_automatch_rule_map amp,
                         ar_cash_remit_refs_interim ref,
                         ar_cash_receipts cr,
                         ra_customer_trx_lines lin,
                         ar_payment_schedules ps,
                         ra_customer_trx trx,
                         ra_cust_trx_types ctt,
                         ra_terms tr
          WHERE          am.automatch_id               = p_automatch_id
          AND            amp.automatch_id              = am.automatch_id
          AND            amp.automatch_set_id          = ref.automatch_set_id
          AND            ref.worker_number             = p_worker_number
          AND            ref.receipt_reference_status  = 'AR_AM_NEW'
          AND            cr.cash_receipt_id            = ref.cash_receipt_id
          AND            cr.receipt_date BETWEEN NVL(am.start_date, cr.receipt_date)
                                         AND NVL(am.end_date, to_date('31/12/4712','DD/MM/YYYY'))
          AND            lin.interface_line_context    = 'OKS CONTRACTS'
          AND            lin.sales_order                 IS NOT NULL
          AND            UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(lin.sales_order, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                            REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) >= am.sugg_match_threshold
          AND            ps.customer_trx_id            = lin.customer_trx_id
          AND            trx.customer_trx_id           = ps.customer_trx_id
                    /* Added to fetch the date from Header Attribute Columns */
          AND            ps.selected_for_receipt_batch_id IS NULL
          AND            ps.class                     NOT IN ('PMT', 'GUAR')
          AND            ps.payment_schedule_id        > 0
          AND            ps.status                      = 'OP'
          AND            ps.customer_id IN (SELECT  DECODE(ARP_STANDARD.sysparm.pay_unrelated_invoices_flag,'Y', ps.customer_id,
                                                                 NVL(cr.pay_from_customer, ps.customer_id))
                                            FROM    DUAL
                                            UNION   ALL
                                            SELECT  related_cust_account_id
                                            FROM    hz_cust_acct_relate_all rel
                                            WHERE   rel.cust_account_id = cr.pay_from_customer
                                            AND     rel.bill_to_flag    = 'Y'
                                            AND     rel.status          = 'A'
                                            AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y'
                                            UNION   ALL
                                            SELECT  rel.related_cust_account_id
                                            FROM    ar_paying_relationships_v rel,
                                                    hz_cust_accounts acc
                                            WHERE   acc.cust_account_id = cr.pay_from_customer
                                            AND     acc.party_id        = rel.party_id
                                            AND     cr.receipt_date    >= effective_start_date
                                            AND     cr.receipt_date    <= effective_end_date
                                            AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y' )
          AND           tr.term_id(+)                 = ps.term_id
          AND           ps.cust_trx_type_id           = ctt.cust_trx_type_id;
Line: 2011

    l_selected_recos              selected_recos_table;
Line: 2012

    l_current_reco                selected_recos_table;
Line: 2019

      log('arp_autoapply_api.insert_contract_recos(+)');
Line: 2021

    OPEN select_recos;
Line: 2023

      FETCH select_recos BULK COLLECT INTO l_selected_recos LIMIT G_MAX_ARRAY_SIZE;
Line: 2024

      log('Count : '||l_selected_recos.COUNT);
Line: 2025

      IF l_selected_recos.COUNT = 0 THEN
        IF l_current_reco.count > 0 THEN
          process_single_reco(l_current_reco
                              , 'SERVICE CONTRACT');
Line: 2029

          l_current_reco.DELETE;
Line: 2030

          insert_recos(p_request_id);
Line: 2035

      l_current_fetch_count := l_selected_recos.COUNT;
Line: 2039

          insert_recos(p_request_id);
Line: 2047

            copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 2050

            IF l_current_reco(l_current_reco_line).reco_num < l_selected_recos(l_outer_index).reco_num THEN
              copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 2062

          insert_recos(p_request_id);
Line: 2068

        l_current_reco.DELETE;
Line: 2072

        log('arp_autoapply_api.insert_contract_recos(-)');
Line: 2076

          log('Exception from arp_autoapply_api.insert_contract_recos');
Line: 2079

  END insert_contract_recos;
Line: 2083

 *     INSERT_ATTRIBUTE_RECOS()                                              *
 * DESCRIPTION                                                               *
 *   Inserts recommendations for transaction numbers (Matched with interface *
 *   header attribute)                                                       *
 * SCOPE - LOCAL                                                             *
 * ARGUMENTS                                                                 *
 *              IN  : p_automatch_id Automatch Rule Identifier               *
 *                    p_use_matching_date Use Matching Date [ALWAYS/For      *
 *                    Duplicates/NULL]                                       *
 *                    p_trans_format_str Transaction Number Format String    *
 *                    p_rem_format_str Reference Number Format String        *
 *                    p_worker_number Current Worker Number                  *
 *                    p_attribute_number Header Attribute Number that has to *
 *                     be matches with (1-16)                                *
 *                    p_request_id Request ID                                *
 *              OUT : None                                                   *
 *                                                                           *
 * RETURNS      NONE                    				                             *
 * ALGORITHM                                                                 *
 *   1. For all open transactions satisfying all the setup conditions calculate
 *      the matching score of header attribute value with the reference number
 *      given in the remittance lines (ar_cash_remit_refs_all)               *
 *   2. If match_score > suggested threshold value specified at the AutoMatch*
 *      setup, insert into ar_cash_recos, ar_cash_reco_lines as a recommenda *
 *      -tion.                                                               *
 * NOTES -                                                                   *
 *   1. Tables with _ALL is used in INSERT statement as multi-table insert is*
 *      not possible on secured synonyms (ar_cash_recos and ar_cash_reco_lines)
 *   2. If pay_unrelated_customer is set to 'Yes' or the reference/receipt is*
 *      unidentified then transactions for all the customers are considered. *
 *      Otherwise only the transactions related to the paying customer of the*
 *      receipt are considered.                                              *
 *   3. An invoice can have multiple installments; which means there is a    *
Line: 2126

  PROCEDURE insert_attribute_recos (p_automatch_id IN NUMBER
                                  , p_use_matching_date IN VARCHAR2
                                  , p_trans_format_str IN VARCHAR2
                                  , p_rem_format_str  IN VARCHAR2
                                  , p_trans_float_str IN VARCHAR2
                                  , p_rem_float_str IN VARCHAR2
                                  , p_worker_number IN NUMBER
                                  , p_attribute_number IN VARCHAR2
                                  , p_request_id IN NUMBER) IS
  l_sel_stmt  VARCHAR2(12000) := 'SELECT     ref.remit_reference_id remit_reference_id,
                             ref.amount_applied ref_amount_applied,
                             ref.amount_applied_from ref_amount_applied_from,
                             ref.trans_to_receipt_rate ref_trans_to_receipt_rate,
                             ref.cash_receipt_id cash_receipt_id,
                             cr.pay_from_customer pay_from_customer,
                             cr.customer_site_use_id cr_customer_site_use_id,
                             ps.customer_trx_id customer_trx_id,
                             ps.customer_id,
                             ps.customer_site_use_id customer_site_use_id,
                             trx.trx_number resolved_matching_number,
                             ps.terms_sequence_number terms_sequence_number,
                             decode(am.match_date_by,
                            ''INT_HDR_ATT1'', fnd_conc_date.string_to_date(trx.interface_header_attribute1),
                            ''INT_HDR_ATT10'', fnd_conc_date.string_to_date(trx.interface_header_attribute10),
                            ''INT_HDR_ATT11'', fnd_conc_date.string_to_date(trx.interface_header_attribute11),
                            ''INT_HDR_ATT12'', fnd_conc_date.string_to_date(trx.interface_header_attribute12),
                            ''INT_HDR_ATT13'', fnd_conc_date.string_to_date(trx.interface_header_attribute13),
                            ''INT_HDR_ATT14'', fnd_conc_date.string_to_date(trx.interface_header_attribute14),
                            ''INT_HDR_ATT15'', fnd_conc_date.string_to_date(trx.interface_header_attribute15),
                            ''INT_HDR_ATT2'', fnd_conc_date.string_to_date(trx.interface_header_attribute2),
                            ''INT_HDR_ATT3'', fnd_conc_date.string_to_date(trx.interface_header_attribute3),
                            ''INT_HDR_ATT4'', fnd_conc_date.string_to_date(trx.interface_header_attribute4),
                            ''INT_HDR_ATT5'', fnd_conc_date.string_to_date(trx.interface_header_attribute5),
                            ''INT_HDR_ATT6'', fnd_conc_date.string_to_date(trx.interface_header_attribute6),
                            ''INT_HDR_ATT7'', fnd_conc_date.string_to_date(trx.interface_header_attribute7),
                            ''INT_HDR_ATT8'', fnd_conc_date.string_to_date(trx.interface_header_attribute8),
                            ''INT_HDR_ATT9'', fnd_conc_date.string_to_date(trx.interface_header_attribute9),
                            ''PURCH_ORDER_DATE'', trx.purchase_order_date,
                            ''TRANS_DATE'', trx.trx_date,
                            NULL)  resolved_matching_date,
                             ps.trx_date trx_date,
                             ps.class resolved_matching_class,
                             ps.invoice_currency_code resolved_match_currency,
                             ps.amount_due_original amount_due_original,
                             ps.amount_due_remaining amount_due_remaining,
                             ps.discount_taken_earned discount_taken_earned,
                             ps.discount_taken_unearned discount_taken_unearned,
                             ARPCURR.CURRROUND(ps.amount_due_remaining, ps.invoice_currency_code ) amount_applied,
                             ROUND(NVL(ref.trans_to_receipt_rate,
                                 DECODE(ps.invoice_currency_code, cr.currency_code, NULL,
                                           NVL( ARP_AUTOAPPLY_API.get_cross_curr_rate(
                                                      ref.amount_applied,
                                                      ref.amount_applied_from,
                                                      ps.invoice_currency_code,
                                                      cr.currency_code
                                                      )
                                                , GL_CURRENCY_API.GET_RATE_SQL(
                                                        ps.invoice_currency_code,
                                                        cr.currency_code,
                                                        cr.receipt_date,
                                                       ar_setup.value(''AR_CROSS_CURRENCY_RATE_TYPE'',null) )
                                              )
                                       )
                                      ),38) trans_to_receipt_rate,
                             NULL amount_applied_from, -- will be calculated later for xcurr app.
                             ps.payment_schedule_id payment_schedule_id,
                             NULL cons_inv_id,         -- Not used here. Useful for BFBs. So null value selected.
                             UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(trx.interface_header_attribute'|| p_attribute_number ||', :b_trans_format_str, ''\2''), :b_trans_float_str, ''''),
                                                                REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, :b_rem_format_str, ''\2''), :b_rem_float_str, '''')) match_score_value,
                             ps.org_id org_id,
                             ps.term_id term_id,
                             am.automatch_id automatch_id,
                             am.use_matching_date use_matching_date,
                             am.use_matching_amount use_matching_amount,
                             am.auto_match_threshold auto_match_threshold,
                             amp.priority priority,
                             cr.currency_code receipt_currency_code,
                             cr.receipt_date receipt_date,
                             ctt.allow_overapplication_flag allow_overapplication_flag,
                             tr.partial_discount_flag partial_discount_flag,
                             RANK() OVER (PARTITION BY trx.interface_header_attribute' || p_attribute_number ||', ps.customer_site_use_id,
                                          ref.remit_reference_id, ps.customer_trx_id
                                  ORDER BY ps.payment_schedule_id) AS  reco_num
              FROM           ar_cash_automatches am,
                             ar_cash_automatch_rule_map amp,
                             ar_cash_remit_refs_interim ref,
                             ar_cash_receipts cr,
                             ra_customer_trx trx,
                             ar_payment_schedules ps,
                             ra_cust_trx_types ctt,
                             ra_terms tr
              WHERE          am.automatch_id               = :b_automatch_id
              AND            amp.automatch_id              = am.automatch_id
              AND            amp.automatch_set_id          = ref.automatch_set_id
              AND            ref.worker_number             = :b_worker_number
              AND            ref.receipt_reference_status  = ''AR_AM_NEW''
              AND            cr.cash_receipt_id            = ref.cash_receipt_id
              AND            cr.receipt_date BETWEEN NVL(am.start_date, cr.receipt_date)
                                             AND NVL(am.end_date, to_date(''31/12/4712'',''DD/MM/YYYY''))
              AND            trx.interface_header_attribute'|| p_attribute_number || ' IS NOT NULL
              AND            UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(trx.interface_header_attribute'|| p_attribute_number ||', :b_trans_format_str, ''\2''), :b_trans_float_str, ''''),
                                                                REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, :b_rem_format_str, ''\2''), :b_rem_float_str, '''')) >= am.sugg_match_threshold
              AND            ps.customer_trx_id            = trx.customer_trx_id
              AND            ps.selected_for_receipt_batch_id IS NULL
              AND            ps.class                     NOT IN (''PMT'', ''GUAR'')
              AND            ps.status                     = ''OP''
              AND            ps.terms_sequence_number     = NVL(ref.installment_reference,
                                                          ps.terms_sequence_number)
              AND            ps.payment_schedule_id        > 0
              AND            ps.customer_id IN (SELECT  DECODE(:b_pay_unrelated_invoices_flag,''Y'', ps.customer_id,
                                                                     NVL(cr.pay_from_customer, ps.customer_id))
                                                FROM    DUAL
                                                UNION   ALL
                                                SELECT  related_cust_account_id
                                                FROM    hz_cust_acct_relate_all rel
                                                WHERE   rel.cust_account_id = cr.pay_from_customer
                                                AND     rel.bill_to_flag    = ''Y''
                                                AND     rel.status          = ''A''
                                                AND     :b_pay_unrelated_invoices_flag <> ''Y''
                                                UNION   ALL
                                                SELECT  rel.related_cust_account_id
                                                FROM    ar_paying_relationships_v rel,
                                                        hz_cust_accounts acc
                                                WHERE   acc.cust_account_id = cr.pay_from_customer
                                                AND     acc.party_id        = rel.party_id
                                                AND     cr.receipt_date    >= effective_start_date
                                                AND     cr.receipt_date    <= effective_end_date
                                                AND     :b_pay_unrelated_invoices_flag <> ''Y'' )
              AND           trx.customer_trx_id           = ps.customer_trx_id
              AND           tr.term_id(+)                 = ps.term_id
              AND           ps.cust_trx_type_id           = ctt.cust_trx_type_id';
Line: 2258

  TYPE SelectRecoType IS REF CURSOR;
Line: 2259

  select_recos                  SelectRecoType;
Line: 2260

  l_selected_recos              selected_recos_table;
Line: 2261

  l_current_reco                selected_recos_table;
Line: 2268

        log('arp_autoapply_api.insert_attribute_recos(+)');
Line: 2272

    OPEN select_recos FOR l_sel_stmt USING p_trans_format_str,
                                           p_trans_float_str,
                                           p_rem_format_str,
                                           p_rem_float_str,
                                           p_automatch_id,
                                           p_worker_number,
                                           p_trans_format_str,
                                           p_trans_float_str,
                                           p_rem_format_str,
                                           p_rem_float_str,
                                           ARP_STANDARD.sysparm.pay_unrelated_invoices_flag,
                                           ARP_STANDARD.sysparm.pay_unrelated_invoices_flag,
                                           ARP_STANDARD.sysparm.pay_unrelated_invoices_flag;
Line: 2286

      FETCH select_recos BULK COLLECT INTO l_selected_recos LIMIT G_MAX_ARRAY_SIZE;
Line: 2287

      log('Count : '||l_selected_recos.COUNT);
Line: 2288

      IF l_selected_recos.COUNT = 0 THEN
        IF l_current_reco.count > 0 THEN
          process_single_reco(l_current_reco
                              , 'INTERFACE HEADER ATTRIBUTE'||p_attribute_number);
Line: 2292

          l_current_reco.DELETE;
Line: 2293

          insert_recos(p_request_id);
Line: 2298

      l_current_fetch_count := l_selected_recos.COUNT;
Line: 2302

          insert_recos(p_request_id);
Line: 2311

            copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 2314

            IF l_current_reco(l_current_reco_line).reco_num < l_selected_recos(l_outer_index).reco_num THEN
              log('Else-If');
Line: 2316

              copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 2328

          insert_recos(p_request_id);
Line: 2334

        l_current_reco.DELETE;
Line: 2339

        log('arp_autoapply_api.insert_attribute_recos(-)');
Line: 2343

          log('Exception from arp_autoapply_api.insert_attribute_recos');
Line: 2346

  END insert_attribute_recos;
Line: 2350

 *     INSERT_WAYBILL_RECOS()                                                *
 * DESCRIPTION                                                               *
 *   Inserts recommendations for Waybill Numbers                             *
 * SCOPE - LOCAL                                                             *
 * ARGUMENTS                                                                 *
 *              IN  : p_automatch_id Automatch Rule Identifier               *
 *                    p_use_matching_date Use Matching Date [ALWAYS/For      *
 *                    Duplicates/NULL]                                       *
 *                    p_trans_format_str Transaction Number Format String    *
 *                    p_rem_format_str Reference Number Format String        *
 *                    p_worker_number Current Worker Number                  *
 *                    p_request_id Request ID                                *
 *              OUT : None                                                   *
 *                                                                           *
 * RETURNS      NONE                    				                             *
 * ALGORITHM                                                                 *
 *   1. For all open way bills satisfying all the setup conditions calculate *
 *      the matching score of way bill number with the reference number      *
 *      given in the remittance lines (ar_cash_remit_refs_all)               *
 *   2. If match_score > suggested threshold value specified at the AutoMatch*
 *      setup, insert into ar_cash_recos, ar_cash_reco_lines as a recommenda *
 *      -tion.                                                               *
 * NOTES -                                                                   *
 *   1. Tables with _ALL is used in INSERT statement as multi-table insert is*
 *      not possible on secured synonyms (ar_cash_recos and ar_cash_reco_lines)
 *   2. If pay_unrelated_customer is set to 'Yes' or the reference/receipt is*
 *      unidentified then transactions for all the customers are considered. *
 *      Otherwise only the transactions related to the paying customer of the*
 *      receipt are considered.                                              *
 *   3. ar_cash_recos contains header level information like resolved        *
 *      number(way bill number)etc., where as ar_cash_reco_lines contains the*
 *      sepecific ps information for the resolved transaction.               *
 *                                                                           *
 * MODIFICATION HISTORY -  09/03/2009 - Created by AGHORAKA	     	           *
 *                                                                           *
 +===========================================================================*/

  PROCEDURE insert_waybill_recos (p_automatch_id IN NUMBER
                                  , p_use_matching_date IN VARCHAR2
                                  , p_trans_format_str IN VARCHAR2
                                  , p_rem_format_str  IN VARCHAR2
                                  , p_trans_float_str IN VARCHAR2
                                  , p_rem_float_str IN VARCHAR2
                                  , p_worker_number IN NUMBER
                                  , p_request_id IN NUMBER) IS
    CURSOR select_recos IS
      SELECT             ref.remit_reference_id remit_reference_id,
                         ref.amount_applied ref_amount_applied,
                         ref.amount_applied_from ref_amount_applied_from,
                         ref.trans_to_receipt_rate ref_trans_to_receipt_rate,
                         ref.cash_receipt_id cash_receipt_id,
                         cr.pay_from_customer pay_from_customer,
                         cr.customer_site_use_id cr_customer_site_use_id,
                         ps.customer_trx_id customer_trx_id,
                         ps.customer_id,
                         ps.customer_site_use_id customer_site_use_id,
                         trx.waybill_number resolved_matching_number,
                         ps.terms_sequence_number terms_sequence_number,
                         decode(am.match_date_by,
                          'INT_HDR_ATT1', fnd_conc_date.string_to_date(trx.interface_header_attribute1),
                          'INT_HDR_ATT10', fnd_conc_date.string_to_date(trx.interface_header_attribute10),
                          'INT_HDR_ATT11', fnd_conc_date.string_to_date(trx.interface_header_attribute11),
                          'INT_HDR_ATT12', fnd_conc_date.string_to_date(trx.interface_header_attribute12),
                          'INT_HDR_ATT13', fnd_conc_date.string_to_date(trx.interface_header_attribute13),
                          'INT_HDR_ATT14', fnd_conc_date.string_to_date(trx.interface_header_attribute14),
                          'INT_HDR_ATT15', fnd_conc_date.string_to_date(trx.interface_header_attribute15),
                          'INT_HDR_ATT2', fnd_conc_date.string_to_date(trx.interface_header_attribute2),
                          'INT_HDR_ATT3', fnd_conc_date.string_to_date(trx.interface_header_attribute3),
                          'INT_HDR_ATT4', fnd_conc_date.string_to_date(trx.interface_header_attribute4),
                          'INT_HDR_ATT5', fnd_conc_date.string_to_date(trx.interface_header_attribute5),
                          'INT_HDR_ATT6', fnd_conc_date.string_to_date(trx.interface_header_attribute6),
                          'INT_HDR_ATT7', fnd_conc_date.string_to_date(trx.interface_header_attribute7),
                          'INT_HDR_ATT8', fnd_conc_date.string_to_date(trx.interface_header_attribute8),
                          'INT_HDR_ATT9', fnd_conc_date.string_to_date(trx.interface_header_attribute9),
                          'PURCH_ORDER_DATE', trx.purchase_order_date,
                          'TRANS_DATE', trx.trx_date,
                          NULL)  resolved_matching_date,
                         ps.trx_date trx_date,
                         ps.class resolved_matching_class,
                         ps.invoice_currency_code resolved_match_currency,
                         ps.amount_due_original amount_due_original,
                         ps.amount_due_remaining amount_due_remaining,
                         ps.discount_taken_earned discount_taken_earned,
                         ps.discount_taken_unearned discount_taken_unearned,
                         ARPCURR.CURRROUND(ps.amount_due_remaining, ps.invoice_currency_code ) amount_applied,
                         ROUND(NVL(ref.trans_to_receipt_rate,
                                   DECODE(ps.invoice_currency_code, cr.currency_code, NULL,
                                             NVL( ARP_AUTOAPPLY_API.get_cross_curr_rate(
                                                        ref.amount_applied,
                                                        ref.amount_applied_from,
                                                        ps.invoice_currency_code,
                                                        cr.currency_code
                                                        )
                                                  , GL_CURRENCY_API.GET_RATE_SQL(
                                                          ps.invoice_currency_code,
                                                          cr.currency_code,
                                                          cr.receipt_date,
                                                         arp_standard.sysparm.CROSS_CURRENCY_RATE_TYPE )
                                                )
                                         )
                                  ),38) trans_to_receipt_rate,
                         NULL amount_applied_from, -- will be calculated later for xcurr app.
                         ps.payment_schedule_id payment_schedule_id,
                         NULL cons_inv_id,         -- Not used here. Useful for BFBs. So null value selected.
                         UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(trx.waybill_number, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                            REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) match_score_value,
                         ps.org_id,
                         ps.term_id term_id,
                         am.automatch_id,
                         am.use_matching_date use_matching_date,
                         am.use_matching_amount use_matching_amount,
                         am.auto_match_threshold auto_match_threshold,
                         amp.priority priority,
                         cr.currency_code receipt_currency_code,
                         cr.receipt_date,
                         ctt.allow_overapplication_flag allow_overapplication_flag,
                         tr.partial_discount_flag partial_discount_flag,
                         RANK() OVER (PARTITION BY trx.waybill_number, ps.customer_site_use_id,
                                      ref.remit_reference_id, ps.customer_trx_id
                              ORDER BY ps.payment_schedule_id) AS  reco_num
          FROM           ar_cash_automatches am,
                         ar_cash_automatch_rule_map amp,
                         ar_cash_remit_refs_interim ref,
                         ar_cash_receipts cr,
                         ra_customer_trx trx,
                         ar_payment_schedules ps,
                         ra_cust_trx_types ctt,
                         ra_terms tr
          WHERE          am.automatch_id               = p_automatch_id
          AND            amp.automatch_id              = am.automatch_id
          AND            amp.automatch_set_id          = ref.automatch_set_id
          AND            ref.worker_number             = p_worker_number
          AND            ref.receipt_reference_status  = 'AR_AM_NEW'
          AND            cr.cash_receipt_id            = ref.cash_receipt_id
          AND            cr.receipt_date BETWEEN NVL(am.start_date, cr.receipt_date)
                                         AND NVL(am.end_date, to_date('31/12/4712','DD/MM/YYYY'))
          AND            trx.waybill_number              IS NOT NULL
          AND            UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(trx.waybill_number, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                            REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) >= am.sugg_match_threshold
          AND            ps.customer_trx_id            = trx.customer_trx_id
          AND            ps.selected_for_receipt_batch_id IS NULL
          AND            ps.class                     NOT IN ('PMT', 'GUAR')
          AND            ps.payment_schedule_id        > 0
          AND            ps.status                      = 'OP'
          AND            ps.customer_id IN (SELECT  DECODE(ARP_STANDARD.sysparm.pay_unrelated_invoices_flag,'Y', ps.customer_id,
                                                                 NVL(cr.pay_from_customer, ps.customer_id))
                                            FROM    DUAL
                                            UNION   ALL
                                            SELECT  related_cust_account_id
                                            FROM    hz_cust_acct_relate_all rel
                                            WHERE   rel.cust_account_id = cr.pay_from_customer
                                            AND     rel.bill_to_flag    = 'Y'
                                            AND     rel.status          = 'A'
                                            AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y'
                                            UNION   ALL
                                            SELECT  rel.related_cust_account_id
                                            FROM    ar_paying_relationships_v rel,
                                                    hz_cust_accounts acc
                                            WHERE   acc.cust_account_id = cr.pay_from_customer
                                            AND     acc.party_id        = rel.party_id
                                            AND     cr.receipt_date    >= effective_start_date
                                            AND     cr.receipt_date    <= effective_end_date
                                            AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y' )
          AND           tr.term_id(+)                 = ps.term_id
          AND           ps.cust_trx_type_id           = ctt.cust_trx_type_id;
Line: 2515

    l_selected_recos              selected_recos_table;
Line: 2516

    l_current_reco                selected_recos_table;
Line: 2523

        log('arp_autoapply_api.insert_waybill_recos(+)');
Line: 2525

    OPEN select_recos;
Line: 2527

      FETCH select_recos BULK COLLECT INTO l_selected_recos LIMIT G_MAX_ARRAY_SIZE;
Line: 2528

      log('Count : '||l_selected_recos.COUNT);
Line: 2529

      IF l_selected_recos.COUNT = 0 THEN
        IF l_current_reco.count > 0 THEN
          process_single_reco(l_current_reco
                              , 'WAYBILL NUMBER');
Line: 2533

          l_current_reco.DELETE;
Line: 2534

          insert_recos(p_request_id);
Line: 2539

      l_current_fetch_count := l_selected_recos.COUNT;
Line: 2543

          insert_recos(p_request_id);
Line: 2551

            copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 2554

            IF l_current_reco(l_current_reco_line).reco_num < l_selected_recos(l_outer_index).reco_num THEN
              copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 2566

          insert_recos(p_request_id);
Line: 2572

        l_current_reco.DELETE;
Line: 2576

        log('arp_autoapply_api.insert_waybill_recos(-)');
Line: 2580

          log('Exception from arp_autoapply_api.insert_waybill_recos');
Line: 2583

  END insert_waybill_recos;
Line: 2587

 *     INSERT_BFB_RECOS()                                                    *
 * DESCRIPTION                                                               *
 *   Inserts recommendations for Balance Forward Bills                       *
 * SCOPE - LOCAL                                                             *
 * ARGUMENTS                                                                 *
 *              IN  : p_automatch_id Automatch Rule Identifier               *
 *                    p_use_matching_date Use Matching Date [ALWAYS/For      *
 *                    Duplicates/NULL]                                       *
 *                    p_trans_format_str Transaction Number Format String    *
 *                    p_rem_format_str Reference Number Format String        *
 *                    p_worker_number Current Worker Number                  *
 *                    p_request_id Request ID                                *
 *              OUT : None                                                   *
 *                                                                           *
 * RETURNS      NONE                    				                             *
 * ALGORITHM                                                                 *
 *   1. For all open bfbs satisfying all the setup conditions calculate      *
 *      the matching score of bfb number with the reference number           *
 *      given in the remittance lines (ar_cash_remit_refs_all)               *
 *   2. If match_score > suggested threshold value specified at the AutoMatch*
 *      setup, insert into ar_cash_recos, ar_cash_reco_lines as a recommenda *
 *      -tion.                                                               *
 * NOTES -                                                                   *
 *   1. Tables with _ALL is used in INSERT statement as multi-table insert is*
 *      not possible on secured synonyms (ar_cash_recos and ar_cash_reco_lines)
 *   2. If pay_unrelated_customer is set to 'Yes' or the reference/receipt is*
 *      unidentified then transactions for all the customers are considered. *
 *      Otherwise only the transactions related to the paying customer of the*
 *      receipt are considered.                                              *
 *   3. A bfb can have multiple invoices; which means there is a possibility *
Line: 2627

  PROCEDURE insert_bfb_recos (p_automatch_id IN NUMBER
                                , p_use_matching_date IN VARCHAR2
                                , p_trans_format_str IN VARCHAR2
                                , p_rem_format_str  IN VARCHAR2
                                , p_trans_float_str IN VARCHAR2
                                , p_rem_float_str IN VARCHAR2
                                , p_worker_number IN NUMBER
                                , p_request_id IN NUMBER) IS
    CURSOR select_recos IS
        SELECT           ref.remit_reference_id remit_reference_id,
                         ref.amount_applied ref_amount_applied,
                         ref.amount_applied_from ref_amount_applied_from,
                         ref.trans_to_receipt_rate ref_trans_to_receipt_rate,
                         ref.cash_receipt_id cash_receipt_id,
                         cr.pay_from_customer pay_from_customer,
                         cr.customer_site_use_id cr_customer_site_use_id,
                         ps.customer_trx_id customer_trx_id,
                         ci.customer_id,
                         ci.site_use_id customer_site_use_id,
                         ci.cons_billing_number resolved_matching_number,
                         ps.terms_sequence_number terms_sequence_number,
                         decode(am.match_date_by, 'BAL_FWD_BILL_DATE', trunc(ci.billing_date), NULL) resolved_matching_date,
                         ps.trx_date trx_date,
                         ps.class resolved_matching_class,
                         ci.currency_code resolved_match_currency,
                         ps.amount_due_original amount_due_original,
                         ps.amount_due_remaining amount_due_remaining,
                         ps.discount_taken_earned discount_taken_earned,
                         ps.discount_taken_unearned discount_taken_unearned,
                         ARPCURR.CURRROUND(ps.amount_due_remaining, ps.invoice_currency_code ) amount_applied,
                         ROUND(NVL(ref.trans_to_receipt_rate,
                                 DECODE(ps.invoice_currency_code, cr.currency_code, NULL,
                                           NVL( ARP_AUTOAPPLY_API.get_cross_curr_rate(
                                                      ref.amount_applied,
                                                      ref.amount_applied_from,
                                                      ps.invoice_currency_code,
                                                      cr.currency_code
                                                      )
                                                , GL_CURRENCY_API.GET_RATE_SQL(
                                                        ps.invoice_currency_code,
                                                        cr.currency_code,
                                                        cr.receipt_date,
                                                       arp_standard.sysparm.CROSS_CURRENCY_RATE_TYPE )
                                              )
                                       )
                                ),38) trans_to_receipt_rate,
                         NULL amount_applied_from, -- will be calculated later for xcurr app.
                         ps.payment_schedule_id payment_schedule_id,
                         ci.cons_inv_id cons_inv_id,
                         UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(ci.cons_billing_number, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                            REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) match_score_value,
                         ci.org_id,
                         ps.term_id term_id,
                         am.automatch_id automatch_id,
                         am.use_matching_date use_matching_date,
                         am.use_matching_amount use_matching_amount,
                         am.auto_match_threshold auto_match_threshold,
                         amp.priority priority,
                         cr.currency_code receipt_currency_code,
                         cr.receipt_date receipt_date,
                         ctt.allow_overapplication_flag allow_overapplication_flag,
                         tr.partial_discount_flag partial_discount_flag,
                         RANK() OVER (PARTITION BY ci.cons_billing_number, ci.site_use_id, ref.remit_reference_id
                              ORDER BY ps.due_date, ps.payment_schedule_id) AS  reco_num
          FROM           ar_cash_automatches am,
                         ar_cash_automatch_rule_map amp,
                         ar_cash_remit_refs_interim ref,
                         ar_cash_receipts cr,
                         ar_cons_inv ci,
                         ar_payment_schedules ps,
                         ra_customer_trx trx,
                         ra_cust_trx_types ctt,
                         ra_terms tr
          WHERE          am.automatch_id               = p_automatch_id
          AND            amp.automatch_id              = am.automatch_id
          AND            amp.automatch_set_id          = ref.automatch_set_id
          AND            ref.worker_number             = p_worker_number
          AND            ref.receipt_reference_status  = 'AR_AM_NEW'
          AND            cr.cash_receipt_id            = ref.cash_receipt_id
          AND            cr.receipt_date BETWEEN NVL(am.start_date, cr.receipt_date)
                                         AND NVL(am.end_date, to_date('31/12/4712','DD/MM/YYYY'))
          AND            UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(ci.cons_billing_number, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                            REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, ''))  >= am.sugg_match_threshold
          AND            ps.cons_inv_id                = ci.cons_inv_id
          AND            ps.selected_for_receipt_batch_id IS NULL
          AND            ps.class                     NOT IN ('PMT', 'GUAR')
          AND            ps.payment_schedule_id        > 0
          AND            ps.status                      = 'OP'
          AND            ps.customer_id IN (SELECT  DECODE(ARP_STANDARD.sysparm.pay_unrelated_invoices_flag,'Y', ps.customer_id,
                                                                 NVL(cr.pay_from_customer, ps.customer_id))
                                            FROM    DUAL
                                            UNION   ALL
                                            SELECT  related_cust_account_id
                                            FROM    hz_cust_acct_relate_all rel
                                            WHERE   rel.cust_account_id = cr.pay_from_customer
                                            AND     rel.bill_to_flag    = 'Y'
                                            AND     rel.status          = 'A'
                                            AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y'
                                            UNION   ALL
                                            SELECT  rel.related_cust_account_id
                                            FROM    ar_paying_relationships_v rel,
                                                    hz_cust_accounts acc
                                            WHERE   acc.cust_account_id = cr.pay_from_customer
                                            AND     acc.party_id        = rel.party_id
                                            AND     cr.receipt_date    >= effective_start_date
                                            AND     cr.receipt_date    <= effective_end_date
                                            AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y'  )
          AND           trx.customer_trx_id           = ps.customer_trx_id
          AND           tr.term_id(+)                 = ps.term_id
          AND           ps.cust_trx_type_id           = ctt.cust_trx_type_id;
Line: 2737

    l_selected_recos              selected_recos_table;
Line: 2738

    l_current_reco                selected_recos_table;
Line: 2745

        log('arp_autoapply_api.insert_bfb_recos(+)');
Line: 2748

    OPEN select_recos;
Line: 2750

      FETCH select_recos BULK COLLECT INTO l_selected_recos LIMIT G_MAX_ARRAY_SIZE;
Line: 2751

      log('Count : '||l_selected_recos.COUNT);
Line: 2752

      IF l_selected_recos.COUNT = 0 THEN
        IF l_current_reco.count > 0 THEN
          process_single_reco(l_current_reco
                              , 'BALANCE FORWARD BILL');
Line: 2756

          l_current_reco.DELETE;
Line: 2757

          insert_recos(p_request_id);
Line: 2762

      l_current_fetch_count := l_selected_recos.COUNT;
Line: 2766

          insert_recos(p_request_id);
Line: 2775

            copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 2778

            IF l_current_reco(l_current_reco_line).reco_num < l_selected_recos(l_outer_index).reco_num THEN
              log('Else-If');
Line: 2780

              copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 2792

          insert_recos(p_request_id);
Line: 2798

        l_current_reco.DELETE;
Line: 2803

        log('arp_autoapply_api.insert_bfb_recos(-)');
Line: 2807

          log('Exception from arp_autoapply_api.insert_bfb_recos');
Line: 2810

  END insert_bfb_recos;
Line: 2813

 *     INSERT_REFERENCE_RECOS()                                              *
 * DESCRIPTION                                                               *
 *   Inserts recommendations for transaction numbers (Matched with reference *
 *   number ra_customer_trx.ct_reference)                                    *
 * SCOPE - LOCAL                                                             *
 * ARGUMENTS                                                                 *
 *              IN  : p_automatch_id Automatch Rule Identifier               *
 *                    p_use_matching_date Use Matching Date [ALWAYS/For      *
 *                    Duplicates/NULL]                                       *
 *                    p_trans_format_str Transaction Number Format String    *
 *                    p_rem_format_str Reference Number Format String        *
 *                    p_worker_number Current Worker Number                  *
 *                    p_request_id Request ID                                *
 *              OUT : None                                                   *
 *                                                                           *
 * RETURNS      NONE                    				                             *
 * ALGORITHM                                                                 *
 *   1. For all open transactions satisfying all the setup conditions calculate
 *      the matching score of trx reference number with the reference number *
 *      given in the remittance lines (ar_cash_remit_refs_all)               *
 *   2. If match_score > suggested threshold value specified at the AutoMatch*
 *      setup, insert into ar_cash_recos, ar_cash_reco_lines as a recommenda *
 *      -tion.                                                               *
 * NOTES -                                                                   *
 *   1. Tables with _ALL is used in INSERT statement as multi-table insert is*
 *      not possible on secured synonyms (ar_cash_recos and ar_cash_reco_lines)
 *   2. If pay_unrelated_customer is set to 'Yes' or the reference/receipt is*
 *      unidentified then transactions for all the customers are considered. *
 *      Otherwise only the transactions related to the paying customer of the*
 *      receipt are considered.                                              *
 *   3. An invoice can have multiple installments; which means there is a    *
Line: 2854

  PROCEDURE insert_reference_recos (p_automatch_id IN NUMBER
                                  , p_use_matching_date IN VARCHAR2
                                  , p_trans_format_str IN VARCHAR2
                                  , p_rem_format_str  IN VARCHAR2
                                  , p_trans_float_str IN VARCHAR2
                                  , p_rem_float_str IN VARCHAR2
                                  , p_worker_number IN NUMBER
                                  , p_request_id IN NUMBER) IS
    CURSOR select_recos IS
      SELECT         ref.remit_reference_id remit_reference_id,
                       ref.amount_applied ref_amount_applied,
                       ref.amount_applied_from ref_amount_applied_from,
                       ref.trans_to_receipt_rate ref_trans_to_receipt_rate,
                       ref.cash_receipt_id cash_receipt_id,
                       cr.pay_from_customer pay_from_customer,
                       cr.customer_site_use_id cr_customer_site_use_id,
                       ps.customer_trx_id customer_trx_id,
                       ps.customer_id,
                       ps.customer_site_use_id customer_site_use_id,
                       ps.trx_number resolved_matching_number,
                       ps.terms_sequence_number terms_sequence_number,
                       decode(am.match_date_by,
                        'INT_HDR_ATT1', fnd_conc_date.string_to_date(trx.interface_header_attribute1),
                        'INT_HDR_ATT10', fnd_conc_date.string_to_date(trx.interface_header_attribute10),
                        'INT_HDR_ATT11', fnd_conc_date.string_to_date(trx.interface_header_attribute11),
                        'INT_HDR_ATT12', fnd_conc_date.string_to_date(trx.interface_header_attribute12),
                        'INT_HDR_ATT13', fnd_conc_date.string_to_date(trx.interface_header_attribute13),
                        'INT_HDR_ATT14', fnd_conc_date.string_to_date(trx.interface_header_attribute14),
                        'INT_HDR_ATT15', fnd_conc_date.string_to_date(trx.interface_header_attribute15),
                        'INT_HDR_ATT2', fnd_conc_date.string_to_date(trx.interface_header_attribute2),
                        'INT_HDR_ATT3', fnd_conc_date.string_to_date(trx.interface_header_attribute3),
                        'INT_HDR_ATT4', fnd_conc_date.string_to_date(trx.interface_header_attribute4),
                        'INT_HDR_ATT5', fnd_conc_date.string_to_date(trx.interface_header_attribute5),
                        'INT_HDR_ATT6', fnd_conc_date.string_to_date(trx.interface_header_attribute6),
                        'INT_HDR_ATT7', fnd_conc_date.string_to_date(trx.interface_header_attribute7),
                        'INT_HDR_ATT8', fnd_conc_date.string_to_date(trx.interface_header_attribute8),
                        'INT_HDR_ATT9', fnd_conc_date.string_to_date(trx.interface_header_attribute9),
                        'PURCH_ORDER_DATE', trx.purchase_order_date,
                        'TRANS_DATE', trx.trx_date,
                        NULL)  resolved_matching_date,
                       ps.trx_date trx_date,
                       ps.class resolved_matching_class,
                       ps.invoice_currency_code resolved_match_currency,
                       ps.amount_due_original amount_due_original,
                       ps.amount_due_remaining amount_due_remaining,
                       ps.discount_taken_earned discount_taken_earned,
                       ps.discount_taken_unearned discount_taken_unearned,
                       ARPCURR.CURRROUND(ps.amount_due_remaining, ps.invoice_currency_code ) amount_applied,
                       ROUND(NVL(ref.trans_to_receipt_rate,
                                 DECODE(ps.invoice_currency_code, cr.currency_code, NULL,
                                           NVL( ARP_AUTOAPPLY_API.get_cross_curr_rate(
                                                      ref.amount_applied,
                                                      ref.amount_applied_from,
                                                      ps.invoice_currency_code,
                                                      cr.currency_code
                                                      )
                                                , GL_CURRENCY_API.GET_RATE_SQL(
                                                        ps.invoice_currency_code,
                                                        cr.currency_code,
                                                        cr.receipt_date,
                                                       arp_standard.sysparm.CROSS_CURRENCY_RATE_TYPE )
                                              )
                                       )
                                ),38) trans_to_receipt_rate,
                       NULL amount_applied_from, -- will be calculated later for xcurr app.
                       ps.payment_schedule_id payment_schedule_id,
                       NULL cons_inv_id,         -- Not used here. Useful for BFBs. So null value selected.
                       UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(trx.ct_reference, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                          REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) match_score_value,
                       ps.org_id,
                       ps.term_id term_id,
                       am.automatch_id automatch_id,
                       am.use_matching_date use_matching_date,
                       am.use_matching_amount use_matching_amount,
                       am.auto_match_threshold auto_match_threshold,
                       amp.priority priority,
                       cr.currency_code receipt_currency_code,
                       cr.receipt_date receipt_date,
                       ctt.allow_overapplication_flag allow_overapplication_flag,
                       tr.partial_discount_flag partial_discount_flag,
                       RANK() OVER (PARTITION BY trx.ct_reference, ps.customer_site_use_id,
                                    ref.remit_reference_id, ps.customer_trx_id
                            ORDER BY ps.payment_schedule_id) AS  reco_num
        FROM           ar_cash_automatches am,
                       ar_cash_automatch_rule_map amp,
                       ar_cash_remit_refs_interim ref,
                       ar_cash_receipts cr,
                       ar_payment_schedules ps,
                       ra_customer_trx trx,
                       ra_cust_trx_types ctt,
                       ra_terms tr
        WHERE          am.automatch_id               = p_automatch_id
        AND            amp.automatch_id              = am.automatch_id
        AND            amp.automatch_set_id          = ref.automatch_set_id
        AND            ref.worker_number             = p_worker_number
        AND            ref.receipt_reference_status = 'AR_AM_NEW'
        AND            cr.cash_receipt_id            = ref.cash_receipt_id
        AND            cr.receipt_date BETWEEN NVL(am.start_date, cr.receipt_date)
                                       AND NVL(am.end_date, to_date('31/12/4712','DD/MM/YYYY'))
        AND            trx.ct_reference              IS NOT NULL
        AND            UTL_MATCH.edit_distance_similarity(REGEXP_REPLACE(REGEXP_REPLACE(trx.ct_reference, p_trans_format_str, '\2'), p_trans_float_str, ''),
                                                          REGEXP_REPLACE(REGEXP_REPLACE(ref.invoice_reference, p_rem_format_str, '\2'), p_rem_float_str, '')) >= am.sugg_match_threshold
        AND            ps.customer_trx_id            = trx.customer_trx_id
        AND            ps.selected_for_receipt_batch_id IS NULL
        AND            ps.class                     NOT IN ('PMT', 'GUAR')
        AND            ps.payment_schedule_id        > 0
        AND            ps.status                    = 'OP'
        AND            ps.terms_sequence_number     = NVL(ref.installment_reference,
                                                          ps.terms_sequence_number)
        AND            ps.customer_id IN (SELECT  DECODE(ARP_STANDARD.sysparm.pay_unrelated_invoices_flag,'Y', ps.customer_id,
                                                               NVL(cr.pay_from_customer, ps.customer_id))
                                          FROM    DUAL
                                          UNION   ALL
                                          SELECT  related_cust_account_id
                                          FROM    hz_cust_acct_relate_all rel
                                          WHERE   rel.cust_account_id = cr.pay_from_customer
                                          AND     rel.bill_to_flag    = 'Y'
                                          AND     rel.status          = 'A'
                                          AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y'
                                          UNION   ALL
                                          SELECT  rel.related_cust_account_id
                                          FROM    ar_paying_relationships_v rel,
                                                  hz_cust_accounts acc
                                          WHERE   acc.cust_account_id = cr.pay_from_customer
                                          AND     acc.party_id        = rel.party_id
                                          AND     cr.receipt_date   >= effective_start_date
                                          AND     cr.receipt_date   <= effective_end_date
                                          AND     ARP_STANDARD.sysparm.pay_unrelated_invoices_flag <> 'Y' )
        AND           tr.term_id(+)                 = ps.term_id
        AND           ps.cust_trx_type_id           = ctt.cust_trx_type_id;
Line: 2984

    l_selected_recos              selected_recos_table;
Line: 2985

    l_current_reco                selected_recos_table;
Line: 2992

          log('arp_autoapply_api.insert_reference_recos(+)');
Line: 2994

      OPEN select_recos;
Line: 2996

      FETCH select_recos BULK COLLECT INTO l_selected_recos LIMIT G_MAX_ARRAY_SIZE;
Line: 2997

      log('Count : '||l_selected_recos.COUNT);
Line: 2998

      IF l_selected_recos.COUNT = 0 THEN
        IF l_current_reco.count > 0 THEN
          process_single_reco(l_current_reco
                              , 'REFERENCE NUMBER');
Line: 3002

          l_current_reco.DELETE;
Line: 3003

          insert_recos(p_request_id);
Line: 3008

      l_current_fetch_count := l_selected_recos.COUNT;
Line: 3012

          insert_recos(p_request_id);
Line: 3020

            copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 3023

            IF l_current_reco(l_current_reco_line).reco_num < l_selected_recos(l_outer_index).reco_num THEN
              copy_current_record(l_current_reco, l_selected_recos, l_outer_index);
Line: 3035

          insert_recos(p_request_id);
Line: 3041

        l_current_reco.DELETE;
Line: 3045

      log('arp_autoapply_api.insert_reference_recos(-)');
Line: 3049

          log('Exception from arp_autoapply_api.insert_reference_recos');
Line: 3052

  END insert_reference_recos;
Line: 3093

 *      If the same transaction is selected for different references.        *
 *  11. AR_AA_DUPLICATE_RECOS : Header Level Validation                      *
 *      If two recommendations with same number is selected for a reference. *
 *  12. AR_AA_MULT_RECOS : Header Level Validation                           *
 *      If more than one recommendations are valid for a reference.          *
 *  13. AR_AA_CUST_NOT_UNIQUE : Header Level Validation                      *
 *      If all the recommendations does not belong to a same customer in case*
 *      if the receipt is unidentified.                                      *
 *  Finally at the end of validation the valid payment schedules selected    *
 *  for application are locked. This is done to counter the possibility of a *
 *  deadlock if the process is run with multiple workers. In such a case a PS*
 *  may be selected for different references for different workers.          *
 *  Final valid status at the end of validation : AR_AA_INV_LOCKED           *
 * NOTES -                                                                   *
 *   1. Validate Recos is called once per each worker.                       *
 *
 * MODIFICATION HISTORY -  09/03/2009 - Created by AGHORAKA	     	           *
 *                                                                           *
 +===========================================================================*/
PROCEDURE validate_trx_recos( p_req_id IN NUMBER
                              , p_worker_number IN NUMBER) IS
    TYPE psid_tab IS TABLE OF ar_payment_schedules.payment_schedule_id%TYPE INDEX BY PLS_INTEGER;
Line: 3127

     UPDATE ar_cash_recos rec
     SET rec.match_reason_code = 'AR_AA_DATE_MISMATCH'
     WHERE rec.request_id = p_req_id
     AND    match_reason_code      = 'AR_AM_INV_THRESHOLD'
     AND EXISTS ( SELECT 'Date Not Matching'
                 FROM ar_cash_automatches am,
                      ar_cash_remit_refs_interim ref
                 WHERE am.automatch_id = rec.automatch_id
                 AND   ref.remit_reference_id = rec.remit_reference_id
                 AND   am.use_matching_date = 'ALWAYS'
                 AND   trunc(rec.resolved_matching_date) <> NVL(ref.matching_reference_date, to_date('31/12/4712','DD/MM/YYYY'))
               );
Line: 3140

          log('No. of recos updated to Date Mismatch: ' || SQL%ROWCOUNT );
Line: 3143

     UPDATE ar_cash_reco_lines l
     SET    recommendation_reason  = 'AR_AA_DATE_MISMATCH'
     WHERE  recommendation_id     IN (SELECT recommendation_id
                                     FROM   ar_cash_recos r
                                     WHERE    match_reason_code        = 'AR_AA_DATE_MISMATCH'
                                     AND    request_id               = l.request_id)
     AND    request_id             = p_req_id;
Line: 3152

          log('No. of reco lines updated to Date Mismatch: ' || SQL%ROWCOUNT );
Line: 3157

     /*UPDATE ar_cash_recos rec
     SET rec.match_reason_code = 'AR_AA_AMOUNT_MISMATCH'
     WHERE rec.request_id = p_req_id
     AND   match_reason_code      = 'AR_AM_INV_THRESHOLD'
     AND EXISTS ( SELECT ref.remit_reference_id
                FROM  ar_cash_reco_lines lin,
                      ar_cash_automatches am,
                      ar_cash_remit_refs_interim ref
                WHERE lin.request_id = p_req_id
                AND   am.automatch_id = rec.automatch_id
                AND   am.use_matching_amount = 'ALWAYS'
                AND   rec.recommendation_id = lin.recommendation_id
                AND   ref.remit_reference_id = rec.remit_reference_id
                GROUP BY ref.remit_reference_id, NVL(ref.amount_applied, ARPCURR.CURRROUND((ref.amount_applied_from / NVL(lin.trans_to_receipt_rate, 1)), lin.receipt_currency_code))
                HAVING SUM(lin.amount_applied) <> NVL(ref.amount_applied, ARPCURR.CURRROUND((ref.amount_applied_from / NVL(lin.trans_to_receipt_rate, 1)), lin.receipt_currency_code))
               );
Line: 3175

          log('No. of recos updated to Amount Mismatch: ' || SQL%ROWCOUNT );
Line: 3178

      UPDATE ar_cash_reco_lines l
      SET    recommendation_reason  = 'AR_AA_AMOUNT_MISMATCH'
      WHERE  recommendation_id     IN (SELECT recommendation_id
                                     FROM   ar_cash_recos r
                                     WHERE    match_reason_code        = 'AR_AA_AMOUNT_MISMATCH'
                                     AND    request_id               = l.request_id)
      AND    request_id             = p_req_id;
Line: 3187

          log('No. of reco lines updated to Amount Mismatch: ' || SQL%ROWCOUNT );
Line: 3191

     UPDATE ar_cash_reco_lines l
     SET    recommendation_reason  = 'AR_AA_CURR_NO_MATCH'
     WHERE  EXISTS                   (SELECT 'Inconsistent Currency'
                                      FROM   ar_cash_recos rec,
                                             ar_cash_remit_refs_interim ref,
                                             ar_payment_schedules ps
                                      WHERE  rec.recommendation_id    = l.recommendation_id
                                      AND    ref.remit_reference_id   = rec.remit_reference_id
                                      AND    ref.worker_number        = p_worker_number
                                      AND    ps.payment_schedule_id   = l.payment_schedule_id
                                      AND    ps.invoice_currency_code<> NVL(ref.invoice_currency_code,
                                                                            ps.invoice_currency_code))
     AND    request_id             = p_req_id
     AND    recommendation_reason  = 'AR_AM_INV_THRESHOLD';
Line: 3207

          log('No. of recos updated to No Currency Match: ' || SQL%ROWCOUNT );
Line: 3212

     UPDATE ar_cash_reco_lines l
     SET    recommendation_reason  = 'AR_AA_NO_XCURR_RATE'
     WHERE  EXISTS                   (SELECT 'No X Rate Info'
                                      FROM   ar_payment_schedules ps
                                      WHERE  l.payment_schedule_id    = ps.payment_schedule_id
                                      AND    l.receipt_currency_code <> ps.invoice_currency_code
                                      AND    (l.trans_to_receipt_rate  IS NULL
                                             OR l.trans_to_receipt_rate = -1))
     AND    request_id             = p_req_id
     AND    recommendation_reason  = 'AR_AM_INV_THRESHOLD';
Line: 3224

          log('No. of recos updated to No Exchange Rate: ' || SQL%ROWCOUNT );
Line: 3228

     UPDATE ar_cash_reco_lines l
     SET    recommendation_reason  = 'AR_AA_INVALID_RATE'
     WHERE (EXISTS                   (SELECT 'Same Currency'
                                      FROM   ar_payment_schedules ps
                                      WHERE  l.payment_schedule_id    = ps.payment_schedule_id
                                      AND    l.receipt_currency_code  = ps.invoice_currency_code
                                      AND    l.trans_to_receipt_rate  IS NOT NULL)
       OR   EXISTS                   (SELECT 'Wrong rate for fixed rate currency'
                                      FROM   ar_payment_schedules ps
                                      WHERE  l.payment_schedule_id    = ps.payment_schedule_id
                                      AND    GL_CURRENCY_API.IS_FIXED_RATE(ps.invoice_currency_code,
                                                                           l.receipt_currency_code,
                                                                           l.receipt_date) = 'Y'
                                      AND    l.trans_to_receipt_rate <> ROUND(GL_CURRENCY_API.GET_RATE_SQL(
                                                                                   ps.invoice_currency_code,
                                                                                   l.receipt_currency_code,
                                                                                   l.receipt_date,
                                                                                   null), 38)))
     AND    request_id             = p_req_id
     AND    recommendation_reason  = 'AR_AM_INV_THRESHOLD';
Line: 3250

          log('No. of recos updated to Invalid Rate: ' || SQL%ROWCOUNT );
Line: 3254

     UPDATE ar_cash_reco_lines l
     SET    recommendation_reason  = (SELECT CASE  WHEN SIGN(l.amount_applied*ps.amount_due_remaining) = -1
                                                                         THEN 'AR_AA_NAT_APP_VIO'
                                                   ELSE recommendation_reason
                                             END
                                      FROM   ar_payment_schedules ps
                                      WHERE  ps.payment_schedule_id = l.payment_schedule_id)
     WHERE    request_id             = p_req_id
     AND    recommendation_reason  = 'AR_AM_INV_THRESHOLD';
Line: 3265

          log('No. of recos updated to Nat Appn Vio/Over Appn: ' || SQL%ROWCOUNT );
Line: 3269

     UPDATE ar_cash_reco_lines l
     SET    recommendation_reason  = 'AR_AA_MUL_APP_TRX'
     WHERE (   EXISTS               (SELECT 'PS already Applied'
                                      FROM   ar_cash_recos rec,
                                             ar_cash_remit_refs_interim ref,
                                             ar_receivable_applications ra
                                      WHERE  rec.recommendation_id    = l.recommendation_id
                                      AND    ref.remit_reference_id   = rec.remit_reference_id
                                      AND    ra.cash_receipt_id       = ref.cash_receipt_id
                                      AND    ref.worker_number        = p_worker_number
                                      AND    l.payment_schedule_id    = ra.applied_payment_schedule_id
                                      AND    ra.display               = 'Y')
           )
     AND    request_id             = p_req_id
     AND    recommendation_reason  = 'AR_AM_INV_THRESHOLD';
Line: 3286

          log('No. of recos updated to Multiple Application on same receipt: ' || SQL%ROWCOUNT );
Line: 3289

     /* Prevent application if the same PS is selected for any other reco
        Since we are validating per worker, at the end of validation there is
        a chance that same PS is selected by two receipts from diff workers */
     /* UPDATE ar_cash_reco_lines l
     SET    recommendation_reason  = 'AR_AA_MUL_RECO_TRX'
     WHERE  EXISTS                   (SELECT 'PS eligible for more than one reference'
                                      FROM   ar_cash_reco_lines l1,
                                             ar_cash_recos rec,
                                             ar_cash_recos rec1
                                      WHERE  l.payment_schedule_id    = l1.payment_schedule_id
                                      AND    l.recommendation_id     <> l1.recommendation_id
                                      AND    rec.recommendation_id = l.recommendation_id
                                      AND    rec1.recommendation_id = l1.recommendation_id
                                      AND    rec.remit_reference_id <> rec1.remit_reference_id
                                      AND    l1.recommendation_reason = 'AR_AM_INV_THRESHOLD'
                                      AND    l1.request_id         = p_req_id)
     AND    request_id             = p_req_id
     AND    recommendation_reason  = 'AR_AM_INV_THRESHOLD';
Line: 3309

          log('No. of recos updated to Same Trx for multiple Recos: ' || SQL%ROWCOUNT );
Line: 3312

     UPDATE ar_cash_recos rec
     SET    rec.match_reason_code = 'AR_AA_DUPLICATE_RECOS'
     WHERE  rec.recommendation_id IN
            (SELECT  recommendation_id
            FROM    ar_cash_recos
            WHERE   request_id = p_req_id
            AND     (resolved_matching_number, match_resolved_using, remit_reference_id) IN
            (
            SELECT  resolved_matching_number, match_resolved_using, remit_reference_id
            FROM    ar_cash_recos rec
            WHERE   rec.request_id = p_req_id
            AND     rec.match_reason_code      = 'AR_AM_INV_THRESHOLD'
            GROUP BY resolved_matching_number, match_resolved_using, remit_reference_id
            HAVING COUNT(*) > 1
            )
            MINUS
            SELECT  recommendation_id
            FROM    ar_cash_recos rec
            WHERE   request_id = p_req_id
            AND     (resolved_matching_number, match_resolved_using, remit_reference_id, resolved_matching_date) IN
            (
            SELECT  resolved_matching_number, match_resolved_using, remit_reference_id, resolved_matching_date
            FROM    ar_cash_recos rec1
            WHERE   request_id = p_req_id
            AND     (resolved_matching_number, match_resolved_using, remit_reference_id) IN
            (
            SELECT  resolved_matching_number, match_resolved_using, remit_reference_id
            FROM    ar_cash_recos rec
            WHERE   rec.request_id = p_req_id
            GROUP BY resolved_matching_number, match_resolved_using, remit_reference_id
            HAVING COUNT(*) > 1
            )
            AND     trunc(rec1.resolved_matching_date) = (SELECT  decode(am.use_matching_date,
                                                            'DUPLICATE', nvl(ref.matching_reference_date, rec1.resolved_matching_date),
                                                            rec1.resolved_matching_date)
                                              FROM    ar_cash_remit_refs_interim ref,
                                                      ar_cash_automatches am
                                              WHERE   ref.worker_number = p_worker_number
                                              AND     ref.remit_reference_id = rec1.remit_reference_id
                                              AND     am.automatch_id = rec1.automatch_id)
             AND     EXISTS ( SELECT ref.remit_reference_id
                            FROM  ar_cash_reco_lines lin,
                                  ar_cash_automatches am,
                                  ar_cash_remit_refs_interim ref
                            WHERE lin.request_id = p_req_id
                            AND   am.automatch_id = rec.automatch_id
                            AND   am.use_matching_amount = 'DUPLICATE'
                            AND   rec.recommendation_id = lin.recommendation_id
                            AND   ref.remit_reference_id = rec1.remit_reference_id
                            GROUP BY ref.remit_reference_id, NVL(ref.amount_applied, ARPCURR.CURRROUND((ref.amount_applied_from / NVL(lin.trans_to_receipt_rate, 1)), lin.receipt_currency_code))
                            HAVING SUM(lin.amount_applied) = NVL(ref.amount_applied, ARPCURR.CURRROUND((ref.amount_applied_from / NVL(lin.trans_to_receipt_rate, 1)), lin.receipt_currency_code))
                           )
            GROUP BY resolved_matching_number, match_resolved_using, remit_reference_id, resolved_matching_date
            HAVING count(*) = 1
            )
            AND    rec.match_reason_code      = 'AR_AM_INV_THRESHOLD'
            )
     AND    rec.request_id             = p_req_id
     AND    rec.match_reason_code      = 'AR_AM_INV_THRESHOLD';
Line: 3373

          log('No. of recos updated to Duplicate Recos: ' || SQL%ROWCOUNT );
Line: 3376

     UPDATE ar_cash_recos rec
     SET    rec.match_reason_code  = 'AR_AA_DUPLICATE_RECOS'
     WHERE  request_id = p_req_id
     AND (resolved_matching_number, match_resolved_using, remit_reference_id )
     IN  ( SELECT  resolved_matching_number,
                      match_resolved_using     ,
                      remit_reference_id
             FROM     ar_cash_recos rec
             WHERE    rec.request_id        = p_req_id
                  AND rec.match_reason_code = 'AR_AM_INV_THRESHOLD'
             GROUP BY resolved_matching_number,
                      match_resolved_using    ,
                      remit_reference_id
             HAVING   COUNT(*) > 1);
Line: 3392

          log('No. of recos updated to Duplicate Recos: ' || SQL%ROWCOUNT );
Line: 3397

      SELECT rec.recommendation_id,
             rec.remit_reference_id,
             rec.resolved_matching_date,
             ref.matching_reference_date,
             sum(NVL(lin.amount_applied, 0)) amount_applied,
             sum(NVL(lin.discount_taken_earned, 0)) discount_taken_earned,
             ps.amount_due_remaining,
             lin.customer_trx_id,
             lin.receipt_date,
             am.use_matching_date,
             am.use_matching_amount
      FROM   ar_cash_recos rec,
             ar_cash_reco_lines lin,
             ar_cash_remit_refs_interim ref,
             ar_cash_automatches am,
             ar_payment_schedules ps
      WHERE  rec.request_id = p_req_id
      AND    rec.match_reason_code  = 'AR_AA_DUPLICATE_RECOS'
      AND    ref.remit_reference_id = rec.remit_reference_id
      AND    ref.worker_number = p_worker_number
      AND    lin.recommendation_id = rec.recommendation_id
      AND    am.automatch_id = rec.automatch_id
      AND    ps.customer_trx_id = lin.customer_trx_id
      GROUP BY rec.recommendation_id,
               rec.remit_reference_id,
               rec.resolved_matching_date,
               ref.matching_reference_date,
               ps.amount_due_remaining,
               lin.customer_trx_id,
               lin.receipt_date,
               am.use_matching_date,
               am.use_matching_amount
      ORDER BY rec.remit_reference_id, rec.recommendation_id;
Line: 3466

          l_rm_frm_dup_rec.DELETE(l_rm_frm_dup_count-1);
Line: 3476

         UPDATE ar_cash_recos
         SET    match_reason_code = 'AR_AM_INV_THRESHOLD'
         WHERE  request_id = p_req_id
         AND    match_reason_code = 'AR_AA_DUPLICATE_RECOS'
         AND    recommendation_id = l_rm_frm_dup_rec(i);
Line: 3487

     UPDATE ar_cash_reco_lines l
      SET    recommendation_reason  = 'AR_AA_DUPLICATE_RECOS'
      WHERE  recommendation_id     IN (SELECT recommendation_id
                                     FROM   ar_cash_recos r
                                     WHERE    match_reason_code        = 'AR_AA_DUPLICATE_RECOS'
                                     AND    request_id               = l.request_id)
      AND    request_id             = p_req_id;
Line: 3496

          log('No. of reco lines updated to Duplicate Recos: ' || SQL%ROWCOUNT );
Line: 3500

     UPDATE ar_cash_recos rec
     SET    match_reason_code      = 'AR_AA_CUST_NOT_UNIQUE'
     WHERE  remit_reference_id       IN (SELECT remit_reference_id
                                      FROM   ar_cash_remit_refs_interim ref1
                                      WHERE  cash_receipt_id IN (
                                            SELECT cr.cash_receipt_id
                                            FROM   ar_cash_receipts cr,
                                                   ar_cash_remit_refs_interim ref,
                                                   ar_cash_recos rec,
                                                   ar_cash_reco_lines recl
                                            WHERE  cr.autoapply_flag          = 'Y'
                                            AND    cr.pay_from_customer         IS NULL
                                            AND    cr.cash_receipt_id         = ref.cash_receipt_id
                                            AND    ref.remit_reference_id     = rec.remit_reference_id
                                            AND    ref.worker_number          = p_worker_number
                                            AND    recl.recommendation_id     = rec.recommendation_id
                                            AND    recl.recommendation_reason = 'AR_AM_INV_THRESHOLD'
                                            AND    EXISTS (SELECT 'Reco of Different Customer'
                                                           FROM   ar_cash_remit_refs_interim ref2,
                                                                  ar_cash_recos rec1,
                                                                  ar_cash_reco_lines recl1
                                                           WHERE  ref2.cash_receipt_id        = ref.cash_receipt_id
                                                           AND    rec1.remit_reference_id     = ref2.remit_reference_id
                                                           AND    recl1.recommendation_id     = rec1.recommendation_id
                                                           AND    recl1.recommendation_reason = 'AR_AM_INV_THRESHOLD'
                                                           AND    rec.recommendation_id      <> rec1.recommendation_id
                                                           AND    rec.pay_from_customer      <> rec1.pay_from_customer
                                                           AND    rec1.request_id             = p_req_id
                                                           AND    ref2.worker_number          = p_worker_number))
                                            AND    ref1.worker_number             = p_worker_number)
     AND    match_reason_code      = 'AR_AM_INV_THRESHOLD'
     AND    request_id             = p_req_id;
Line: 3533

        log('No. of recos updated to Non Unique Customer: ' || SQL%ROWCOUNT );
Line: 3536

     UPDATE ar_cash_reco_lines l
     SET    recommendation_reason  = 'AR_AA_CUST_NOT_UNIQUE'
     WHERE  recommendation_id     IN (SELECT recommendation_id
                                      FROM   ar_cash_recos
                                      WHERE  match_reason_code      = 'AR_AA_CUST_NOT_UNIQUE'
                                      AND    request_id             = p_req_id)
     AND    request_id             = p_req_id
     AND    recommendation_reason  = 'AR_AM_INV_THRESHOLD';
Line: 3546

     UPDATE ar_cash_recos rec
     SET    match_reason_code = 'AR_AA_MULT_RECOS'
     WHERE  EXISTS         (SELECT 'Multiple Recos'
                                FROM   ar_cash_recos rec1,
                                       ar_cash_reco_lines lin
                                WHERE  rec1.remit_reference_id   = rec.remit_reference_id
                                AND    rec1.recommendation_id   <> rec.recommendation_id
                                AND    lin.recommendation_id     = rec1.recommendation_id
                                AND    lin.recommendation_reason = 'AR_AM_INV_THRESHOLD'
                                AND    (CASE
                                        WHEN rec1.match_score_value > rec.match_score_value THEN 'T'
                                        WHEN rec1.match_score_value = rec.match_score_value THEN
                                          CASE WHEN rec1.priority >= rec.priority THEN 'T'
                                          END
                                        END) = 'T'
                                AND    lin.request_id             = p_req_id)
     AND   EXISTS              (SELECT 'Applicable Reco Exist'
                                FROM   ar_cash_reco_lines lin
                                WHERE  lin.recommendation_id = rec.recommendation_id
                                AND    lin.recommendation_reason = 'AR_AM_INV_THRESHOLD'
                                AND    lin.request_id             = p_req_id)
     AND    request_id             = p_req_id
     AND    match_reason_code      = 'AR_AM_INV_THRESHOLD';
Line: 3571

          log('No. of recos updated to Multiple Recos: ' || SQL%ROWCOUNT );
Line: 3574

     UPDATE ar_cash_reco_lines l
     SET    recommendation_reason = 'AR_AA_MULT_RECOS'
     WHERE  EXISTS             (SELECT 'Many Types of Recos'
                                FROM   ar_cash_recos rec
                                WHERE  l.recommendation_id       = rec.recommendation_id
                                AND    rec.match_reason_code     = 'AR_AA_MULT_RECOS'
                                AND    rec.request_id             = p_req_id)
     AND    recommendation_reason = 'AR_AM_INV_THRESHOLD'
     AND    request_id             = p_req_id;
Line: 3584

          log('No. of reco lines updated to Multiple Recos: ' || SQL%ROWCOUNT );
Line: 3588

     UPDATE ar_cash_recos rec
     SET    match_reason_code      = 'AR_AA_REMIT_EXCEEDED'
     WHERE  remit_reference_id       IN
                              (SELECT remit_reference_id
                              FROM   ar_cash_remit_refs_interim
                              WHERE  cash_receipt_id IN (
                                    SELECT ps.cash_receipt_id
                                    FROM   ar_payment_schedules ps,
                                           ar_cash_receipts cr,
                                           ar_cash_remit_refs_interim ref,
                                           ar_cash_recos rec,
                                           ar_cash_reco_lines recl
                                    WHERE  ps.cash_receipt_id         = cr.cash_receipt_id
                                    AND    ps.cash_receipt_id         = ref.cash_receipt_id
                                    AND    ref.remit_reference_id     = rec.remit_reference_id
                                    AND    ref.worker_number          = p_worker_number
                                    AND    recl.recommendation_id     = rec.recommendation_id
                                    AND    recl.recommendation_reason = 'AR_AM_INV_THRESHOLD'
                                    AND    recl.request_id            = p_req_id
                                    GROUP BY ps.cash_receipt_id, ps.amount_due_remaining
                                    HAVING ps.amount_due_remaining*-1 < SUM(NVL(recl.amount_applied_from,
                                                                                 recl.amount_applied))))
     AND    match_reason_code      = 'AR_AM_INV_THRESHOLD'
     AND    request_id             = p_req_id;
Line: 3613

        log('No. of recos updated to Remittance amount exceeded: ' || SQL%ROWCOUNT );
Line: 3616

     UPDATE ar_cash_reco_lines l
     SET    recommendation_reason  = 'AR_AA_REMIT_EXCEEDED'
     WHERE  recommendation_id     IN (SELECT recommendation_id
                                      FROM   ar_cash_recos
                                      WHERE  match_reason_code      = 'AR_AA_REMIT_EXCEEDED'
                                      AND    request_id             = p_req_id)
     AND    request_id             = p_req_id
     AND    recommendation_reason  = 'AR_AM_INV_THRESHOLD';
Line: 3625

          log('No. of reco lines updated to Remittance amount exceeded: ' || SQL%ROWCOUNT );
Line: 3628

    /* * Update the unidentified cash receipts with the customer number of *
       * the valid recommendations.                                        * */

    DECLARE
    CURSOR unid_receipts IS
      SELECT distinct cash_receipt_id
      FROM ar_cash_remit_refs_interim
      WHERE worker_number = p_worker_number
      AND customer_id IS NULL;
Line: 3648

      SELECT decode(count(distinct rec.pay_from_customer),
                        1, max(rec.pay_from_customer),
                        NULL)
      INTO l_customer_id
      FROM ar_cash_recos rec,
           ar_cash_remit_refs_interim ref
      WHERE rec.request_id = p_req_id
      AND   ref.cash_receipt_id = l_cash_receipt_id
      AND   rec.remit_reference_id = ref.remit_reference_id
      AND   ref.worker_number = p_worker_number
      AND   rec.match_reason_code = 'AR_AM_INV_THRESHOLD';
Line: 3663

        AR_RECEIPT_UPDATE_API_PUB.update_receipt_unid_to_unapp(
        p_api_version                  => 1.0,
        x_return_status                => v_return_status,
        x_msg_count                    => v_msg_count,
        x_msg_data                     => v_msg_data,
        --p_commit                       => FND_API.G_TRUE,
        p_cash_receipt_id              => l_cash_receipt_id,
        p_pay_from_customer            => l_customer_id,
        x_status                       => v_status
        );
Line: 3676

          UPDATE ar_cash_recos
          SET     match_reason_code      = 'AR_AA_CUST_UNID'
          WHERE  remit_reference_id  IN (SELECT ref1.remit_reference_id
                                          FROM   ar_cash_remit_refs_interim ref1
                                          WHERE  ref1.cash_receipt_id = l_cash_receipt_id)
          AND    match_reason_code      = 'AR_AM_INV_THRESHOLD'
          AND    request_id             = p_req_id;
Line: 3686

    UPDATE ar_cash_reco_lines l
    SET    recommendation_reason  = 'AR_AA_CUST_UNID'
    WHERE  recommendation_id     IN (SELECT recommendation_id
                                    FROM   ar_cash_recos
                                    WHERE  match_reason_code      = 'AR_AA_CUST_UNID'
                                    AND    request_id             = p_req_id)
    AND    request_id             = p_req_id
    AND    recommendation_reason  = 'AR_AM_INV_THRESHOLD';
Line: 3696

    SELECT ps.payment_schedule_id
     BULK COLLECT INTO locked_ps_records
     FROM   ar_payment_schedules ps,
            ar_cash_reco_lines lines
     WHERE  lines.request_id = p_req_id
     AND    lines.recommendation_reason = 'AR_AM_INV_THRESHOLD'
     AND    ps.payment_schedule_id = lines.payment_schedule_id
     FOR UPDATE OF ps.amount_due_remaining SKIP LOCKED;
Line: 3706

       UPDATE ar_cash_reco_lines
       SET    recommendation_reason = 'AR_AA_INV_LOCKED'
       WHERE  request_id = p_req_id
       AND    recommendation_reason = 'AR_AM_INV_THRESHOLD'
       AND    payment_schedule_id = locked_ps_records(i);
Line: 3727

 *   Apply all valid recommendations and update the reference with resolved  *
 *   matching numbers.                                                       *
 * SCOPE - LOCAL                                                             *
 * ARGUMENTS                                                                 *
 *              IN  : p_worker_number Current Worker Number                  *
 *                    p_req_id Request ID                                    *
 *              OUT : None                                                   *
 *                                                                           *
 * RETURNS      NONE                    				                             *
 * ALGORITHM                                                                 *
 *  1. Select Valid Recommendation lines ( with status 'AR_AA_INV_LOCKED')   *
 *  2. For all recommendation lines                                          *
 *  3. Select Next Recommendation Line                                       *
 *  4. Apply the transaction                                                 *
 *  5. Compute the remaining balace for the reference                        *
 *  6. If balance > 0 go to Step 3.                                          *
 *  7. Update the referene with Resolved matching number, currency etc .     *
 *  8. Delete the recommendations for the references that were (automatically)
 *     applied.                                                              *
 * NOTES -                                                                   *
 *   1. APPLY_TRX_RECOS is called once per each worker.                      *
 *                                                                           *
 * MODIFICATION HISTORY -  09/03/2009 - Created by AGHORAKA	     	           *
 *                                                                           *
 +===========================================================================*/

  PROCEDURE apply_trx_recos(p_req_id         IN NUMBER
                            , p_worker_number  IN NUMBER)  IS

     l_return_status        VARCHAR2(10);
Line: 3771

      SELECT  distinct rec.remit_reference_id
      FROM    ar_cash_recos rec
      WHERE rec.request_id = p_req_id
      AND   rec.match_reason_code = 'AR_AM_INV_THRESHOLD'
      AND   rec.match_resolved_using     <> 'BALANCE FORWARD BILL';
Line: 3778

      SELECT  ref.cash_receipt_id,
              rec.remit_reference_id,
              NVL(ref.amount_applied, ARPCURR.CURRROUND((ref.amount_applied_from / NVL(lin.trans_to_receipt_rate, 1)), lin.receipt_currency_code)) ref_amount_applied,
              lin.amount_applied,
              lin.payment_schedule_id,
              lin.amount_applied_from,
              lin.trans_to_receipt_rate,
              lin.recommendation_id,
              lin.line_number,
              lin.receipt_currency_code,
              rec.resolved_match_currency
       FROM   ar_cash_remit_refs_interim ref,
              ar_cash_recos rec,
              ar_cash_reco_lines lin
       WHERE rec.remit_reference_id = p_remit_reference_id
       AND   ref.remit_reference_id        = rec.remit_reference_id
       AND   rec.recommendation_id         = lin.recommendation_id
       AND   ref.worker_number             = p_worker_number
       AND   lin.recommendation_reason     = 'AR_AA_INV_LOCKED'
       AND   rec.match_resolved_using     <> 'BALANCE FORWARD BILL'
       AND   lin.request_id                = p_req_id
       ORDER BY lin.recommendation_id, lin.line_number;
Line: 3802

      SELECT  rec.remit_reference_id,
              rec.cons_inv_id,
              rec.recommendation_id,
              ref.amount_applied,
              ref.amount_applied_from,
              ref.cash_receipt_id,
              cr.currency_code
      FROM   ar_cash_recos rec,
             ar_cash_remit_refs_interim ref,
             ar_cash_receipts cr
      WHERE rec.request_id = p_req_id
      AND   rec.match_reason_code = 'AR_AM_INV_THRESHOLD'
      AND   rec.match_resolved_using = 'BALANCE FORWARD BILL'
      AND   ref.remit_reference_id = rec.remit_reference_id
      AND   ref.worker_number = p_worker_number
      AND   cr.cash_receipt_id = ref.cash_receipt_id;
Line: 3820

      SELECT  lin.recommendation_id,
              lin.line_number,
              lin.payment_schedule_id,
              lin.customer_trx_id,
              lin.amount_applied,
              lin.amount_applied_from,
              lin.trans_to_receipt_rate,
              ps.invoice_currency_code
      FROM  ar_cash_reco_lines lin,
            ar_payment_schedules ps
      WHERE lin.recommendation_id = p_reco_id
      AND   lin.request_id                = p_req_id
      AND   lin.recommendation_reason     = 'AR_AA_INV_LOCKED'
      AND   ps.payment_schedule_id        = lin.payment_schedule_id
      ORDER BY lin.recommendation_id, lin.line_number;
Line: 3884

          UPDATE ar_cash_reco_lines
          SET    recommendation_reason   = 'AR_AA_REC_APP_IN_ERROR'
          WHERE  recommendation_id       = app_line.recommendation_id
          AND    line_number             = app_line.line_number;
Line: 3961

            UPDATE ar_cash_reco_lines
            SET    recommendation_reason   = 'AR_AA_REC_APP_IN_ERROR'
            WHERE  recommendation_id       = bfb_line.recommendation_id
            AND    line_number             = bfb_line.line_number;
Line: 3984

     UPDATE ar_cash_remit_refs ref
     SET   (receipt_reference_status,
            resolved_matching_number,
            auto_applied,
            match_score_value,
            resolved_matching_date,
            invoice_currency_code,
            match_resolved_using)      =(SELECT 'AR_AA_INV_APPLIED',
                                          rec.resolved_matching_number,
                                          'Y',
                                          rec.match_score_value,
                                          rec.resolved_matching_date,
                                          rec.resolved_match_currency,
                                          rec.automatch_id
                                     FROM   ar_cash_recos rec,
                                            ar_cash_reco_lines lin
                                     WHERE  ref.remit_reference_id = rec.remit_reference_id
                                     AND    lin.recommendation_id  = rec.recommendation_id
                                     AND    rec.request_id          = p_req_id
                                     AND    recommendation_type    = 'TRX'
                                     AND    lin.recommendation_reason  = 'AR_AA_INV_LOCKED'
                                     AND    rownum =1)
      WHERE  EXISTS                  (SELECT 'Found Match'
                                     FROM   ar_cash_recos rec,
                                            ar_cash_reco_lines lin
                                     WHERE  ref.remit_reference_id = rec.remit_reference_id
                                     AND    lin.recommendation_id  = rec.recommendation_id
                                     AND    lin.request_id          = p_req_id
                                     AND    recommendation_type    = 'TRX'
                                     AND    lin.recommendation_reason  = 'AR_AA_INV_LOCKED');
Line: 4015

          log('No. of References updated with Resolved Matching Number: ' || SQL%ROWCOUNT );
Line: 4018

     DELETE FROM ar_cash_reco_lines lin
     WHERE  EXISTS                  (SELECT 'Delete Recos'
                                     FROM   ar_cash_remit_refs ref,
                                            ar_cash_recos rec
                                     WHERE  ref.receipt_reference_status = 'AR_AA_INV_APPLIED'
                                     AND    lin.recommendation_id    = rec.recommendation_id
                                     AND    rec.remit_reference_id   = ref.remit_reference_id)
     AND    request_id          = p_req_id;
Line: 4027

     DELETE FROM ar_cash_recos rec
     WHERE  EXISTS                  (SELECT 'Delete Recos'
                                     FROM   ar_cash_remit_refs ref
                                     WHERE  ref.receipt_reference_status = 'AR_AA_INV_APPLIED'
                                     AND    rec.remit_reference_id   = ref.remit_reference_id)
     AND    request_id          = p_req_id
     AND    recommendation_type    = 'TRX';
Line: 4036

          log('No. of Recos deleted: ' || SQL%ROWCOUNT );
Line: 4048

  PROCEDURE copy_current_record(  p_current_reco IN OUT NOCOPY selected_recos_table
                              , p_selected_recos IN selected_recos_table
                              , p_index IN NUMBER) IS
  i NUMBER;
Line: 4058

    p_current_reco(i).remit_reference_id           := p_selected_recos(p_index).remit_reference_id;
Line: 4059

    p_current_reco(i).ref_amount_applied           := p_selected_recos(p_index).ref_amount_applied;
Line: 4060

    p_current_reco(i).ref_amount_applied_from      := p_selected_recos(p_index).ref_amount_applied_from;
Line: 4061

    p_current_reco(i).ref_trans_to_receipt_rate    := p_selected_recos(p_index).ref_trans_to_receipt_rate;
Line: 4062

    p_current_reco(i).payment_schedule_id          := p_selected_recos(p_index).payment_schedule_id;
Line: 4063

    p_current_reco(i).amount_applied               := p_selected_recos(p_index).amount_applied;
Line: 4064

    p_current_reco(i).amount_applied_from          := p_selected_recos(p_index).amount_applied_from;
Line: 4065

    p_current_reco(i).cash_receipt_id              := p_selected_recos(p_index).cash_receipt_id;
Line: 4066

    p_current_reco(i).pay_from_customer            := p_selected_recos(p_index).pay_from_customer;
Line: 4067

    p_current_reco(i).cr_customer_site_use_id      := p_selected_recos(p_index).cr_customer_site_use_id;
Line: 4068

    p_current_reco(i).amount_due_original          := p_selected_recos(p_index).amount_due_original;
Line: 4069

    p_current_reco(i).amount_due_remaining         := p_selected_recos(p_index).amount_due_remaining;
Line: 4070

    p_current_reco(i).discount_taken_earned        := p_selected_recos(p_index).discount_taken_earned;
Line: 4071

    p_current_reco(i).discount_taken_unearned      := p_selected_recos(p_index).discount_taken_unearned;
Line: 4072

    p_current_reco(i).customer_trx_id              := p_selected_recos(p_index).customer_trx_id;
Line: 4073

    p_current_reco(i).customer_id                  := p_selected_recos(p_index).customer_id;
Line: 4074

    p_current_reco(i).customer_site_use_id         := p_selected_recos(p_index).customer_site_use_id;
Line: 4075

    p_current_reco(i).resolved_matching_number     := p_selected_recos(p_index).resolved_matching_number;
Line: 4076

    p_current_reco(i).terms_sequence_number        := p_selected_recos(p_index).terms_sequence_number;
Line: 4077

    p_current_reco(i).resolved_matching_date       := p_selected_recos(p_index).resolved_matching_date;
Line: 4078

    p_current_reco(i).trx_date                     := p_selected_recos(p_index).trx_date;
Line: 4079

    p_current_reco(i).resolved_matching_class      := p_selected_recos(p_index).resolved_matching_class;
Line: 4080

    p_current_reco(i).resolved_match_currency      := p_selected_recos(p_index).resolved_match_currency;
Line: 4081

    p_current_reco(i).amount_applied               := p_selected_recos(p_index).amount_applied;
Line: 4082

    p_current_reco(i).amount_applied_from          := p_selected_recos(p_index).amount_applied_from;
Line: 4083

    p_current_reco(i).trans_to_receipt_rate        := p_selected_recos(p_index).trans_to_receipt_rate;
Line: 4084

    p_current_reco(i).payment_schedule_id          := p_selected_recos(p_index).payment_schedule_id;
Line: 4085

    p_current_reco(i).match_score_value            := p_selected_recos(p_index).match_score_value;
Line: 4086

    p_current_reco(i).org_id                       := p_selected_recos(p_index).org_id;
Line: 4087

    p_current_reco(i).term_id                      := p_selected_recos(p_index).term_id;
Line: 4088

    p_current_reco(i).automatch_id                 := p_selected_recos(p_index).automatch_id;
Line: 4089

    p_current_reco(i).use_matching_date            := p_selected_recos(p_index).use_matching_date;
Line: 4090

    p_current_reco(i).use_matching_amount          := p_selected_recos(p_index).use_matching_amount;
Line: 4091

    p_current_reco(i).auto_match_threshold         := p_selected_recos(p_index).auto_match_threshold;
Line: 4092

    p_current_reco(i).priority                     := p_selected_recos(p_index).priority;
Line: 4093

    p_current_reco(i).receipt_currency_code        := p_selected_recos(p_index).receipt_currency_code;
Line: 4094

    p_current_reco(i).receipt_date                 := p_selected_recos(p_index).receipt_date;
Line: 4095

    p_current_reco(i).allow_overapplication_flag   := p_selected_recos(p_index).allow_overapplication_flag;
Line: 4096

    p_current_reco(i).partial_discount_flag        := p_selected_recos(p_index).partial_discount_flag;
Line: 4097

    p_current_reco(i).reco_num                     := p_selected_recos(p_index).reco_num;
Line: 4103

  PROCEDURE process_single_reco(p_current_reco IN OUT NOCOPY selected_recos_table
                                , p_match_resolved_using IN VARCHAR2) IS
    l_block_index              NUMBER;
Line: 4129

    SELECT  ar_cash_recos_s.nextval
    INTO    l_recommendation_id
    FROM    dual;
Line: 4262

  PROCEDURE populate_reco_line_struct(p_current_reco IN selected_recos_table
                                    , p_match_resolved_using IN VARCHAR2
                                    , p_recommendation_id IN NUMBER
                                    , p_recommendation_reason IN VARCHAR2) IS
  l_index NUMBER;
Line: 4322

    reco_id_arr.DELETE;
Line: 4323

    remit_ref_id_arr.DELETE;
Line: 4324

    customer_id_arr.DELETE;
Line: 4325

    customer_site_use_id_arr.DELETE;
Line: 4326

    resolved_matching_number_arr.DELETE;
Line: 4327

    resolved_matching_date_arr.DELETE;
Line: 4328

    resolved_matching_class_arr.DELETE;
Line: 4329

    resolved_match_currency_arr.DELETE;
Line: 4330

    match_resolved_using_arr.DELETE;
Line: 4331

    cons_inv_id_arr.DELETE;
Line: 4332

    match_score_value_arr.DELETE;
Line: 4333

    match_reason_code_arr.DELETE;
Line: 4334

    org_id_arr.DELETE;
Line: 4335

    priority_arr.DELETE;
Line: 4336

    reco_num_arr.DELETE;
Line: 4337

    customer_trx_id_arr.DELETE;
Line: 4338

    payment_schedule_id_arr.DELETE;
Line: 4339

    amount_applied_arr.DELETE;
Line: 4340

    amount_applied_from_arr.DELETE;
Line: 4341

    trans_to_receipt_rate_arr.DELETE;
Line: 4342

    receipt_currency_code_arr.DELETE;
Line: 4343

    receipt_date_arr.DELETE;
Line: 4344

    recommendation_reason_arr.DELETE;
Line: 4345

    discount_taken_earned_arr.DELETE;
Line: 4346

    discount_taken_unearned_arr.DELETE;
Line: 4357

  PROCEDURE insert_recos(p_request_id IN NUMBER) IS
    l_reco_index NUMBER;
Line: 4365

      log('insert_recos()+');
Line: 4370

        INSERT
        INTO ar_cash_recos_all (
                   recommendation_id,
                   recommendation_type,
                   recommendation_source,
                   remit_reference_id,
                   pay_from_customer,
                   customer_site_use_id,
                   resolved_matching_number,
                   resolved_matching_date,
                   resolved_matching_class,
                   resolved_match_currency,
                   cons_inv_id,
                   match_resolved_using,
                   match_score_value,
                   match_reason_code,
                   recommendation_status,
                   autoapply_status,
                   org_id,
                   created_by,
                   creation_date,
                   last_updated_by,
                   last_update_date,
                   last_update_login,
                   program_application_id,
                   program_id,
                   program_update_date,
                   request_id,
                   automatch_id,
                   priority)
        VALUES   (reco_id_arr(l_reco_index),
                 'TRX',
                 'AUTOMATCH',
                 remit_ref_id_arr(l_reco_index),
                 customer_id_arr(l_reco_index),
                 customer_site_use_id_arr(l_reco_index),
                 resolved_matching_number_arr(l_reco_index),
                 resolved_matching_date_arr(l_reco_index),
                 resolved_matching_class_arr(l_reco_index),
                 resolved_match_currency_arr(l_reco_index),
                 cons_inv_id_arr(l_reco_index),
                 match_resolved_using_arr(l_reco_index),
                 match_score_value_arr(l_reco_index),
                 match_reason_code_arr(l_reco_index),
                 'CREATED',
                 'NONE',
                 org_id_arr(l_reco_index),
                 g_created_by,
                 SYSDATE,
                 g_last_updated_by,
                 SYSDATE,
                 g_last_update_login,
                 g_program_application_id,
                 g_program_id,
                 SYSDATE,
                 p_request_id,
                 automatch_id_arr(l_reco_index),
                 priority_arr(l_reco_index));
Line: 4432

      INSERT INTO ar_cash_reco_lines_all (
                 recommendation_id,
                 line_number,
                 customer_trx_id,
                 payment_schedule_id,
                 amount_applied,
                 amount_applied_from,
                 trans_to_receipt_rate,
                 receipt_currency_code,
                 receipt_date,
                 org_id,
                 created_by,
                 creation_date,
                 last_updated_by,
                 last_update_date,
                 last_update_login,
                 program_application_id,
                 program_id,
                 program_update_date,
                 request_id,
                 recommendation_reason,
                 discount_taken_earned)
      SELECT     reco_id_arr(l_reco_line_index),
                 reco_num_arr(l_reco_line_index),
                 customer_trx_id_arr(l_reco_line_index),
                 payment_schedule_id_arr(l_reco_line_index),
                 amount_applied_arr(l_reco_line_index),
                 amount_applied_from_arr(l_reco_line_index),
                 trans_to_receipt_rate_arr(l_reco_line_index),
                 receipt_currency_code_arr(l_reco_line_index),
                 receipt_date_arr(l_reco_line_index),
                 org_id_arr(l_reco_line_index),
                 g_created_by,
                 SYSDATE,
                 g_last_updated_by,
                 SYSDATE,
                 g_last_update_login,
                 g_program_application_id,
                 g_program_id,
                 SYSDATE,
                 p_request_id,
                 match_reason_code_arr(l_reco_line_index),
                 discount_taken_earned_arr(l_reco_line_index)
      FROM DUAL;
Line: 4477

      log('insert_recos()+');
Line: 4481

          log('Exception from arp_autoapply_api.insert_recos');
Line: 4484

  END insert_recos;
Line: 4573

  SELECT NVL(NVL(site.discount_terms, cust.discount_terms),'Y')
  INTO  l_allow_discount
  FROM
    hz_customer_profiles      cust
  , hz_customer_profiles      site
  WHERE
        cust.cust_account_id          = l_customer_id
  AND   cust.site_use_id              IS NULL
  AND   site.cust_account_id (+)      = cust.cust_account_id
  AND   site.site_use_id (+)          = l_bill_to_site_use_id;
Line: 4584

  SELECT NVL(NVL(site.discount_grace_days, cust.discount_grace_days),0)
  INTO  l_grace_days
  FROM
    hz_customer_profiles 	cust
  , hz_customer_profiles 	site
  , hz_cust_accounts		cust_acct
  WHERE
    	  cust_acct.cust_account_id 	= l_customer_id
  AND   cust.cust_account_id 		= cust_acct.cust_account_id
  AND   cust.site_use_id 		IS NULL
  AND   site.cust_account_id (+) 	= cust_acct.cust_account_id
  AND   site.site_use_id (+) 		= NVL(l_BILL_TO_SITE_USE_ID, -4444);
Line: 4731

 *     DELETE_INTERIM_RECORDS()                                              *
 * DESCRIPTION                                                               *
 *   Delete records from ar_cash_remit_refs_interim.                         *
 * SCOPE - LOCAL                                                             *
 * ARGUMENTS                                                                 *
 *              IN  : None                                                   *
 *              OUT : None                                                   *
 *                                                                           *
 * RETURNS      NONE                    				                             *
 * ALGORITHM                                                                 *
 *  1. Delete records from ar_cash_remit_refs_interim.                       *
 * NOTES -                                                                   *
 *   1. This is called from the XML report                                   *
 *                                                                           *
 * MODIFICATION HISTORY -  09/03/2009 - Created by AGHORAKA	     	           *
 *                                                                           *
 +===========================================================================*/
  PROCEDURE delete_interim_records IS
  BEGIN
     IF (PG_DEBUG IN ('Y', 'C')) THEN
          log('arp_autoapply_api.delete_interim_records()+' );
Line: 4753

    DELETE FROM ar_cash_remit_refs_interim;
Line: 4755

          log('No. of records deleted: ' || SQL%ROWCOUNT );
Line: 4758

          log('arp_autoapply_api.delete_interim_records(-)' );
Line: 4762

          log('Exception from arp_autoapply_api.delete_interim_records');
Line: 4765

  END delete_interim_records;