DBA Data[Home] [Help]

APPS.ARP_CREDIT_MEMO_MODULE SQL Statements

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

Line: 97

net_rev_index BINARY_INTEGER;	-- keeps track of next row to insert
Line: 139

  insert_dist		VARCHAR2(1),
  insert_cma		VARCHAR2(1),
  insert_offset		VARCHAR2(1),
  check_gl_date		VARCHAR2(1)  -- for crediting rel9 immed invoices
);
Line: 147

cm_sched_index BINARY_INTEGER := 0;	-- keeps track of next row to insert
Line: 176

TYPE cm_sched_insert_dist_tab_type IS
  TABLE OF cm_schedule_rec.insert_dist%type
  INDEX BY BINARY_INTEGER;
Line: 179

cm_sched_insert_dist_t cm_sched_insert_dist_tab_type;
Line: 180

null_cm_sched_insert_dist CONSTANT cm_sched_insert_dist_tab_type :=
    cm_sched_insert_dist_t;
Line: 183

TYPE cm_sched_insert_cma_tab_type IS
  TABLE OF cm_schedule_rec.insert_cma%type
  INDEX BY BINARY_INTEGER;
Line: 186

cm_sched_insert_cma_t cm_sched_insert_cma_tab_type;
Line: 187

null_cm_sched_insert_cma CONSTANT cm_sched_insert_cma_tab_type :=
    cm_sched_insert_cma_t;
Line: 191

  TABLE OF cm_schedule_rec.insert_offset%type
  INDEX BY BINARY_INTEGER;
Line: 193

cm_sched_insert_offset_t cm_sched_ins_offset_tab_type;
Line: 194

null_cm_sched_insert_offset CONSTANT cm_sched_ins_offset_tab_type :=
    cm_sched_insert_offset_t;
Line: 226

TYPE select_rec_type IS RECORD
(
  customer_trx_line_id		BINARY_INTEGER,
  prev_cust_trx_line_id		BINARY_INTEGER,
  allow_overapp_flag	ra_cust_trx_types.allow_overapplication_flag%type,
  cm_amount			NUMBER,
  credit_method_for_rules	VARCHAR2(1),
  last_period_to_credit   	NUMBER,
  currency_code			ra_customer_trx.invoice_currency_code%type,
  inv_acct_rule_duration	NUMBER,
  allow_not_open_flag		VARCHAR2(1),
  partial_period_flag		VARCHAR2(1),
  cm_gl_date			DATE,
  invoice_quantity		NUMBER,
  cm_quantity			NUMBER,
  invoice_sign			NUMBER, -- 3198525 from char(1) to number
  cm_sign			NUMBER, -- 3198525 from char(1) to number
  rule_start_date		DATE,  -- output only
  rule_end_date		        DATE,  -- output only
  cm_acct_rule_duration		NUMBER, -- output only
  inv_unit_price                NUMBER, -- 4621029
  cm_unit_price                 NUMBER  -- 4621029
);
Line: 267

nonrule_insert_dist_c    	INTEGER;
Line: 268

nonrule_update_lines_c  	INTEGER;
Line: 269

nonrule_update_dist_c  	        INTEGER;
Line: 270

nonrule_update_dist2_c  	INTEGER;
Line: 272

rule_select_cm_lines_c  	INTEGER;
Line: 273

rule_update_cm_lines_c 		INTEGER;
Line: 274

rule_insert_dist_c 		INTEGER;
Line: 275

rule_insert_cma_c 		INTEGER;
Line: 279

delete_header_dist_c		INTEGER;
Line: 280

delete_line_dist_c		INTEGER;
Line: 281

delete_header_cma_c		INTEGER;
Line: 282

delete_line_cma_c		INTEGER;
Line: 283

update_header_lines_c		INTEGER;
Line: 284

update_lines_c			INTEGER;
Line: 340

    close_cursor( nonrule_insert_dist_c );
Line: 341

    close_cursor( nonrule_update_lines_c );
Line: 342

    close_cursor( nonrule_update_dist_c );
Line: 343

    close_cursor( nonrule_update_dist2_c );
Line: 345

    close_cursor( rule_select_cm_lines_c );
Line: 346

    close_cursor( rule_update_cm_lines_c );
Line: 347

    close_cursor( rule_insert_dist_c );
Line: 348

    close_cursor( rule_insert_cma_c );
Line: 354

PROCEDURE insert_into_error_table(
	p_interface_line_id binary_integer,
	p_message_text varchar2,
	p_invalid_value varchar2 )  IS

BEGIN

    INSERT INTO ra_interface_errors
    (interface_line_id,
     message_text,
     invalid_value,
     org_id)
    VALUES
    (p_interface_line_id,
     p_message_text,
     p_invalid_value,
     arp_standard.sysparm.org_id);
Line: 372

END insert_into_error_table;
Line: 489

PROCEDURE build_update_mode_sql(
        p_delete_header_dist_c	 	IN OUT NOCOPY INTEGER,
        p_delete_line_dist_c	 	IN OUT NOCOPY INTEGER,
        p_delete_header_cma_c	 	IN OUT NOCOPY INTEGER,
        p_delete_line_cma_c	 	IN OUT NOCOPY INTEGER,
        p_update_header_lines_c	 	IN OUT NOCOPY INTEGER,
        p_update_lines_c	 	IN OUT NOCOPY INTEGER  ) IS

    l_delete_header_dist_sql   	VARCHAR2(1000);
Line: 498

    l_delete_line_dist_sql   	VARCHAR2(1000);
Line: 499

    l_delete_header_cma_sql   	VARCHAR2(1000);
Line: 500

    l_delete_line_cma_sql   	VARCHAR2(1000);
Line: 501

    l_update_header_lines_sql   VARCHAR2(1000);
Line: 502

    l_update_lines_sql   	VARCHAR2(1000);
Line: 507

    print_fcn_label( 'arp_credit_memo_module.build_update_mode_sql()+' );
Line: 509

    l_delete_header_dist_sql :=
'DELETE from ra_cust_trx_line_gl_dist
WHERE  customer_trx_id = :customer_trx_id
and    account_class    <> ''REC''
and    account_set_flag = ''N'' ';
Line: 516

    l_delete_header_dist_sql := l_delete_header_dist_sql ||
          ' RETURNING cust_trx_line_gl_dist_id INTO :gl_dist_key_value ';
Line: 519

    debug(l_delete_header_dist_sql);
Line: 520

    debug('  len(delete_header_dist_sql) = '||
          to_char(lengthb(l_delete_header_dist_sql)));
Line: 524

    l_delete_line_dist_sql :=
'DELETE from ra_cust_trx_line_gl_dist
WHERE  customer_trx_line_id = :customer_trx_line_id
AND    account_set_flag = ''N'' ';
Line: 530

    l_delete_line_dist_sql := l_delete_line_dist_sql ||
          ' RETURNING cust_trx_line_gl_dist_id INTO :gl_dist_key_value ';
Line: 533

    debug(l_delete_line_dist_sql);
Line: 534

    debug('  len(delete_line_dist_sql) = '||
          to_char(lengthb(l_delete_line_dist_sql)));
Line: 537

    l_delete_header_cma_sql :=
'DELETE from ar_credit_memo_amounts
WHERE  customer_trx_line_id in
(SELECT customer_trx_line_id
 FROM   ra_customer_trx_lines
 WHERE  line_type = ''LINE''
 and    customer_trx_id = :customer_trx_id)';
Line: 545

    debug(l_delete_header_cma_sql);
Line: 546

    debug('  len(delete_header_cma_sql) = '||
          to_char(lengthb(l_delete_header_cma_sql)));
Line: 549

    l_delete_line_cma_sql :=
'DELETE from ar_credit_memo_amounts
WHERE  customer_trx_line_id = :customer_trx_line_id';
Line: 553

    debug(l_delete_line_cma_sql);
Line: 554

    debug('  len(delete_line_cma_sql) = '||
          to_char(lengthb(l_delete_line_cma_sql)));
Line: 557

    l_update_header_lines_sql :=
'UPDATE ra_customer_trx_lines
SET
rule_start_date = null,
rule_end_date = null,
accounting_rule_duration = null
WHERE  customer_trx_id = :customer_trx_id
and    line_type       = ''LINE'' ';
Line: 566

    debug(l_update_header_lines_sql);
Line: 567

    debug('  len(update_header_lines_sql) = '||
          to_char(lengthb(l_update_header_lines_sql)));
Line: 570

    l_update_lines_sql :=
'UPDATE ra_customer_trx_lines
SET
rule_start_date = null,
rule_end_date = null,
accounting_rule_duration = null
WHERE  customer_trx_line_id = :customer_trx_line_id';
Line: 578

    debug(l_update_lines_sql);
Line: 579

    debug('  len(update_lines_sql) = '||
          to_char(lengthb(l_update_lines_sql)));
Line: 587

	debug( '  Parsing update mode stmts', MSG_LEVEL_DEBUG );
Line: 589

        p_delete_header_dist_c := dbms_sql.open_cursor;
Line: 590

        dbms_sql.parse( p_delete_header_dist_c, l_delete_header_dist_sql,
                        dbms_sql.v7 );
Line: 593

        p_delete_line_dist_c := dbms_sql.open_cursor;
Line: 594

        dbms_sql.parse( p_delete_line_dist_c, l_delete_line_dist_sql,
                        dbms_sql.v7 );
Line: 597

        p_delete_header_cma_c := dbms_sql.open_cursor;
Line: 598

        dbms_sql.parse( p_delete_header_cma_c, l_delete_header_cma_sql,
                        dbms_sql.v7 );
Line: 601

        p_delete_line_cma_c := dbms_sql.open_cursor;
Line: 602

        dbms_sql.parse( p_delete_line_cma_c, l_delete_line_cma_sql,
                        dbms_sql.v7 );
Line: 605

        p_update_header_lines_c := dbms_sql.open_cursor;
Line: 606

        dbms_sql.parse( p_update_header_lines_c, l_update_header_lines_sql,
                        dbms_sql.v7 );
Line: 609

        p_update_lines_c := dbms_sql.open_cursor;
Line: 610

        dbms_sql.parse( p_update_lines_c, l_update_lines_sql,
                        dbms_sql.v7 );
Line: 615

          debug( 'EXCEPTION: Error parsing update mode stmts' );
Line: 620

    print_fcn_label( 'arp_credit_memo_module.build_update_mode_sql()-' );
Line: 624

        debug( 'EXCEPTION: arp_credit_memo_module.build_update_mode_sql()' );
Line: 627

END build_update_mode_sql;
Line: 656

                               to set IDs during insert.
*/

----------------------------------------------------------------------------
PROCEDURE build_nonrule_sql(
	p_system_info 		IN arp_trx_global.system_info_rec_type,
        p_profile_info 		IN arp_trx_global.profile_rec_type,
        p_cm_control 		IN control_rec_type,
        p_nonrule_insert_dist_c 	IN OUT NOCOPY INTEGER,
        p_nonrule_update_lines_c 	IN OUT NOCOPY INTEGER,
        p_nonrule_update_dist_c 	IN OUT NOCOPY INTEGER,
        p_nonrule_update_dist2_c 	IN OUT NOCOPY INTEGER  ) IS


    l_nonrule_insert_dist_sql   VARCHAR2(32767);
Line: 671

    l_nonrule_update_lines_sql  VARCHAR2(1000);
Line: 672

    l_nonrule_update_dist_sql   VARCHAR2(1000);
Line: 673

    l_nonrule_update_dist2_sql  VARCHAR2(2000);
Line: 815

    l_nonrule_insert_dist_sql :=
'INSERT into ra_cust_trx_line_gl_dist
(
  /* gl_dist_id used to be here - now populated by BRI trigger */
  customer_trx_id,               /* credit memo customer_trx_id */
  customer_trx_line_id,          /* credit memo customer_trx_line_id */
  cust_trx_line_salesrep_id,     /* id for cm srep line credited */
  request_id,
  set_of_books_id,
  last_update_date,
  last_updated_by,
  creation_date,
  created_by,
  last_update_login,
  program_application_id,
  program_id,
  program_update_date,
  account_class,               /* account class for the invoice */
                               /* assignment being credited */
  account_set_flag,
  percent,
  amount ,
  acctd_amount,
  gl_date,
  code_combination_id,
  posting_control_id,
  collected_tax_ccid,
  ussgl_transaction_code,      /*Bug 2246098*/
  revenue_adjustment_id,       /* Bug 2543675 - RAM id copied to CM dist */
  rec_offset_flag,              /* Bug 2560036 - non-collectible trans */
  org_id
) ';
Line: 848

    l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql || CRLF ||
'  /* Non Receivable account case */
SELECT
/* Bug 4029814 - removed gl_dist mock logic because of unique constraints */
ctl.customer_trx_id,
ctl.customer_trx_line_id,
ctls.cust_trx_line_salesrep_id,
ct.request_id,
ct.set_of_books_id,
sysdate,
ct.last_updated_by,
sysdate,
ct.created_by,
ct.last_update_login,
ct.program_application_id,            /* program_appl_id */
ct.program_id,                        /* program_id */
sysdate,                              /* program_update_date */
prev_ctlgd.account_class,
''N'',                                /* account set for rules flag */
decode(ctl.extended_amount,
       0, prev_ctlgd.percent,
       round(((decode(foreign_fc.minimum_accountable_unit,
                      null, round( ' || CRLF ||
l_amount_fragment || CRLF ||
'                                  , precision),
                      round( ' || CRLF ||
l_amount_fragment || CRLF ||
'                           / foreign_fc.minimum_accountable_unit) *
                      foreign_fc.minimum_accountable_unit
                      ) /
               decode(ctl.extended_amount, 0, 1, ctl.extended_amount)
               ) *  decode(ctl.extended_amount, 0, 0, 1)
             ) * 100, 4)
       ),                                             /* percent */
decode(foreign_fc.minimum_accountable_unit,
       null, round( ' || CRLF ||
l_amount_fragment || CRLF ||
'                   , precision),
       round( ' || CRLF ||
l_amount_fragment || CRLF ||
'              / foreign_fc.minimum_accountable_unit) *
       foreign_fc.minimum_accountable_unit
       ),                                           /* amount */';
Line: 900

        l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql || CRLF ||
'  round(decode(foreign_fc.minimum_accountable_unit,
               null, round(' || CRLF ||
l_amount_fragment || CRLF ||
'                             , precision),
               round( ' || CRLF ||
l_amount_fragment || CRLF ||
'                      / foreign_fc.minimum_accountable_unit) *
               foreign_fc.minimum_accountable_unit
               ) * nvl(ct.exchange_rate, 1),
         ' || p_system_info.base_precision || CRLF ||
'       ),                                       /* acctd_amount */';
Line: 915

        l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql || CRLF ||
'  round(decode(foreign_fc.minimum_accountable_unit,
               null, round( ' || CRLF ||
l_amount_fragment || CRLF ||
'                             , precision),
               round( ' || CRLF ||
l_amount_fragment || CRLF ||
'                      / foreign_fc.minimum_accountable_unit) *
               foreign_fc.minimum_accountable_unit
               ) * nvl(ct.exchange_rate, 1) / ' ||
fnd_number.number_to_canonical(system_info.base_min_acc_unit) || CRLF ||
'        ) * ' || fnd_number.number_to_canonical(system_info.base_min_acc_unit) || ',    /* acctd_amount */' ;
Line: 931

        l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql || CRLF ||
'  rec_ctlgd.gl_date,
  prev_ctlgd.code_combination_id,
 -3,
 prev_ctlgd.collected_tax_ccid,
 ct.default_ussgl_transaction_code,      /*Bug 2246098*/
 prev_ctlgd.revenue_adjustment_id,  /* Bug 2543675 */
 prev_ctlgd.rec_offset_flag,         /* Bug 2560036 */
 ct.org_id
FROM
fnd_currencies foreign_fc,
ar_lookups al_rules,
ra_customer_trx ct,
ra_customer_trx_lines ctl,
ra_cust_trx_line_gl_dist ctlgd,
ra_cust_trx_line_gl_dist rec_ctlgd,     /* cm rec dist */
ra_cust_trx_line_salesreps ctls,
ra_customer_trx prev_ct,
ra_customer_trx_lines prev_ctl,
ra_cust_trx_line_gl_dist prev_ctlgd,
ra_cust_trx_line_gl_dist prev_ctlgd2   /* inv rec dist */
WHERE  ct.customer_trx_id          = ctl.customer_trx_id
and    ctl.customer_trx_line_id    = ctlgd.customer_trx_line_id(+)
       /* only look at invoices without an invoicing rule */
and    al_rules.lookup_code        = ''N''
       /* join to the credit memo receivable account dist */
and    ct.customer_trx_id          = rec_ctlgd.customer_trx_id(+)
and    rec_ctlgd.account_class(+)           = ''REC''
and   rec_ctlgd.latest_rec_flag(+)         = ''Y''
       /* get currency information */
and    ct.invoice_currency_code    = foreign_fc.currency_code
       /* join to the invoice */
and    ctl.previous_customer_trx_line_id
                                   = prev_ctl.customer_trx_line_id
and    prev_ctl.customer_trx_id    = prev_ctlgd2.customer_trx_id
and    prev_ctl.customer_trx_line_id
                                 = prev_ctlgd.customer_trx_line_id
and    prev_ctl.customer_trx_id  = prev_ct.customer_trx_id
       /* join for cust_trx_line_salesrep_id */';
Line: 977

        l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql || CRLF ||
'and    :cm_customer_trx_id_3 = ctls.customer_trx_id(+)';
Line: 984

        l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql || CRLF ||
'and    :request_id_3 = ctls.request_id(+)';
Line: 991

    l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql || CRLF ||
'and    prev_ctlgd.cust_trx_line_salesrep_id
                        =   ctls.prev_cust_trx_line_salesrep_id(+)
       /* do not duplicate existing records */
and    ctlgd.account_set_flag(+)            = ''N''
and    ctlgd.customer_trx_id                is null
and    ctl.previous_customer_trx_line_id    is not null
and    al_rules.lookup_type                 =  ''YES/NO''
     /* Use the presence of an invoicing rule to determine if the invoice has
        accounting rules, not the presence of an UNEARN/UNBILL distribution */
and    al_rules.lookup_code = DECODE(prev_ct.invoicing_rule_id,NULL,''N'',''Y'')
     /* Do not backout account sets for rules records.
        However, do use the account set record if this
        is a header frt only CM against an invoice with rules. */
and    prev_ctlgd.account_set_flag  =
       decode(prev_ct.invoicing_rule_id, '''', ''N'', decode(al_rules.lookup_code,''N'',''N'',''Y''))
       /* insure that prev_ctlgd2 is the invoice rec record */
and    prev_ctlgd2.customer_trx_line_id+0   is null
and    prev_ctlgd2.account_class  = ''REC''
and    prev_ctlgd2.account_set_flag = al_rules.lookup_code
       /* only reverse records in the invoice header gl date */
and    (prev_ctl.accounting_rule_id is null
        OR
        nvl(prev_ctlgd.gl_date,
           nvl(prev_ctlgd2.gl_date,
               to_date(''2415386'', ''J'')) ) =
            nvl(prev_ctlgd2.gl_date,  to_date(''2415386'',
                                              ''J'')) )
       /* Accept all distributions for tax, freight and rec AND
          non revenue distributions with the same sign as the
          line.  This includes invoices that do not use rules and
          unbilled or unearned account in the invoice GL date
          from which revenue is reclassed. */
and    (
         prev_ctl.line_type <> ''LINE''
       OR
         (prev_ctl.line_type        = ''LINE'' AND
           prev_ctlgd.account_class = ''SUSPENSE'')
       OR
         ( prev_ctlgd.account_class NOT IN (''REV'',''UNEARN'') AND
           sign(prev_ctlgd.amount) =
              sign(prev_ctl.extended_amount))
       OR
         ( prev_ctl.accounting_rule_id is null OR
             al_rules.lookup_code = ''N'')
       )
and    decode(prev_ctlgd.account_class,
             ''SUSPENSE'', ctl.revenue_amount - ctl.extended_amount,
                         1) <> 0 ' || CRLF || l_where_pred;
Line: 1045

      l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql || CRLF ||
'and    (ar_revenue_management_pvt.line_collectible(prev_ctl.customer_trx_id,
                                               prev_ctl.customer_trx_line_id)
         IN (1,2) or
         prev_ctlgd.revenue_adjustment_id is null)';
Line: 1053

l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql  || CRLF ||
'UNION
/*     Receivable account case */
SELECT
/* Bug 4029814 - removed gl_dist mock logic because of unique constraints */
ctl.customer_trx_id,
to_number(NULL),                 /* customer_trx_line_id */
to_number(NULL),            /* cust_trx_line_salesrep_id */
max(ctl.request_id),
max(ctl.set_of_books_id),
max(ctl.last_update_date),
max(ctl.last_updated_by),
max(ctl.creation_date),
max(ctl.created_by),
max(ctl.last_update_login),
max(ctl.program_application_id),      /* program_appl_id */
max(ctl.program_id),                       /* program_id */
sysdate,                        /* program_update_date */
''REC'',                                  /* account class */
''N'',                                 /* account_set_flag */
100,                                          /* percent */
sum(ctl.extended_amount),                      /* amount */
sum( ' ;
Line: 1080

        l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql || CRLF ||
'    round(ctl.extended_amount * nvl(ct.exchange_rate, 1), ' ||
p_system_info.base_precision || ')';
Line: 1086

        l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql || CRLF ||
'    round(ctl.extended_amount * nvl(ct.exchange_rate, 1) / ' ||
fnd_number.number_to_canonical(p_system_info.base_min_acc_unit) || ') * ' ||
fnd_number.number_to_canonical(p_system_info.base_min_acc_unit) ;
Line: 1093

        l_nonrule_insert_dist_sql := l_nonrule_insert_dist_sql || CRLF ||
'    ),                                /* accounted amount */
cm_rec.gl_date,
inv_rec.code_combination_id,
-3,
inv_rec.collected_tax_ccid,
ct.default_ussgl_transaction_code,  /*Bug 2246098*/
inv_rec.revenue_adjustment_id,      /* Bug 2543675 */
null,                               /* Bug 2560036 */
ct.org_id
FROM
ra_customer_trx ct,
ar_lookups al_rules,
ra_cust_trx_line_gl_dist inv_rec,
ra_cust_trx_line_gl_dist cm_rec,
ra_cust_trx_line_gl_dist lgd,
ra_customer_trx_lines ctl
WHERE  ct.customer_trx_id  = ctl.customer_trx_id
and    ctl.customer_trx_id = lgd.customer_trx_id(+)
and    ''REC''               = lgd.account_class(+)
and    ''N''                 = lgd.account_set_flag(+)
and    ctl.customer_trx_id = cm_rec.customer_trx_id
and    ''REC''               = cm_rec.account_class
and    ''Y''                 = cm_rec.account_set_flag
and    lgd.customer_trx_id is null
       /* only create receivable records if the real invoice
          receivable record exists.                     */
and    ct.previous_customer_trx_id  = inv_rec.customer_trx_id
and    ''REC''               = inv_rec.account_class
and    ''N''                 = inv_rec.account_set_flag
and    al_rules.lookup_type                 =  ''YES/NO''
/* Use the presence of an invoicing rule to determine if the invoice has
   accounting rules, not the presence of an UNEARN or UNBILL distribution */
and    al_rules.lookup_code = DECODE(ct.invoicing_rule_id,NULL,''N'',''Y'')
and    al_rules.lookup_code = ''N'' ' || CRLF ||
l_rec_where_pred || CRLF ||
'GROUP BY
ctl.customer_trx_id,
inv_rec.cust_trx_line_gl_dist_id,
inv_rec.gl_date,
cm_rec.gl_date,
inv_rec.code_combination_id,
inv_rec.collected_tax_ccid,
ct.default_ussgl_transaction_code, /*Bug 2246098*/
inv_rec.revenue_adjustment_id,    /* Bug 2543675 */
ct.org_id'; /* 4156400 */
Line: 1140

    debug(l_nonrule_insert_dist_sql);
Line: 1141

    debug('  len(nonrule_insert_dist_sql) = '||
          to_char(lengthb(l_nonrule_insert_dist_sql)));
Line: 1189

    l_nonrule_update_lines_sql :=
'UPDATE ra_customer_trx_lines ctl
SET    autorule_complete_flag = '''',
       autorule_duration_processed = accounting_rule_duration
WHERE  ctl.accounting_rule_id is not null' || CRLF ||
l_where_pred || CRLF ||
'and   ctl.autorule_complete_flag||'''' = ''N''
and   exists
(
 SELECT ''x''
 FROM ra_cust_trx_line_gl_dist d
 WHERE d.customer_trx_id  = ctl.customer_trx_id
 and   d.account_class    = ''REC''
 and   d.account_set_flag = ''N''
)';
Line: 1207

    debug(l_nonrule_update_lines_sql);
Line: 1208

    debug('  len(nonrule_update_lines_sql) = '||
          to_char(lengthb(l_nonrule_update_lines_sql)));
Line: 1214

    l_nonrule_update_dist_sql :=
'UPDATE ra_cust_trx_line_gl_dist d
SET latest_rec_flag    = ''N''
WHERE account_class    = ''REC''
and d.latest_rec_flag  = ''Y''
and d.account_set_flag = ''Y''
and d.customer_trx_id in
(
 SELECT ctl.customer_trx_id
 FROM ra_customer_trx_lines ctl
 WHERE 1 = 1' || CRLF ||
l_where_pred || CRLF ||
')
and exists
(
 SELECT 1
 FROM ra_cust_trx_line_gl_dist d2
 WHERE d2.account_class   = ''REC''
 and   d2.latest_rec_flag = ''Y''
 and   d2.customer_trx_id = d.customer_trx_id
 and   d.rowid <> d2.rowid
)';
Line: 1239

    debug(l_nonrule_update_dist_sql);
Line: 1240

    debug('  len(nonrule_update_dist_sql) = '||
          to_char(lengthb(l_nonrule_update_dist_sql)));
Line: 1263

    l_nonrule_update_dist2_sql :=
'UPDATE ra_cust_trx_line_gl_dist lgd
set
(
 amount,
 acctd_amount
) =
(
 SELECT
 (ctl.extended_amount - sum(lgd2.amount) ) + lgd.amount,' || CRLF ||
' (' || l_amount_fragment || CRLF ||
'    - sum(lgd2.acctd_amount)) + lgd.acctd_amount
 FROM
 ra_customer_trx_lines ctl,
 ra_customer_trx ct,
 ra_cust_trx_line_gl_dist lgd2
 WHERE ctl.customer_trx_line_id = lgd2.customer_trx_line_id
 and   ctl.customer_trx_line_id = lgd.customer_trx_line_id
 and   ct.customer_trx_id = ctl.customer_trx_id
 GROUP BY
 ctl.customer_trx_line_id,
 ctl.line_number,
 ctl.extended_amount,
 ct.exchange_rate
)
WHERE lgd.cust_trx_line_gl_dist_id in
(
 SELECT min(cust_trx_line_gl_dist_id)
 from
 ra_customer_trx_lines ctl,
 ra_customer_trx ct,
 ra_cust_trx_line_gl_dist lgd3
 where ctl.customer_trx_line_id = lgd3.customer_trx_line_id';
Line: 1299

l_nonrule_update_dist2_sql := l_nonrule_update_dist2_sql || CRLF ||
' and   ctl.request_id = :request_id';
Line: 1305

l_nonrule_update_dist2_sql := l_nonrule_update_dist2_sql || CRLF ||
' and   ctl.customer_trx_id = :cm_customer_trx_id';
Line: 1310

    l_nonrule_update_dist2_sql := l_nonrule_update_dist2_sql || CRLF ||
' and   ct.customer_trx_id = ctl.customer_trx_id
 GROUP BY
 ctl.customer_trx_line_id,
 ctl.line_number,
 ctl.extended_amount
 HAVING
 (
  sum(lgd3.amount) <> ctl.extended_amount ) or
  (sum(lgd3.acctd_amount) <>
      sum( ' || CRLF ||
l_amount_fragment || CRLF ||
'         )
  )
)' ;
Line: 1328

    debug(l_nonrule_update_dist2_sql);
Line: 1329

    debug('  len(nonrule_update_dist2_sql) = '||
          to_char(lengthb(l_nonrule_update_dist2_sql)));
Line: 1338

        p_nonrule_insert_dist_c := dbms_sql.open_cursor;
Line: 1339

        dbms_sql.parse( p_nonrule_insert_dist_c, l_nonrule_insert_dist_sql,
                        dbms_sql.v7 );
Line: 1342

        p_nonrule_update_lines_c := dbms_sql.open_cursor;
Line: 1343

        dbms_sql.parse( p_nonrule_update_lines_c, l_nonrule_update_lines_sql,
                        dbms_sql.v7 );
Line: 1346

        p_nonrule_update_dist_c := dbms_sql.open_cursor;
Line: 1347

        dbms_sql.parse( p_nonrule_update_dist_c, l_nonrule_update_dist_sql,
                        dbms_sql.v7 );
Line: 1350

        p_nonrule_update_dist2_c := dbms_sql.open_cursor;
Line: 1351

        dbms_sql.parse( p_nonrule_update_dist2_c, l_nonrule_update_dist2_sql,
                        dbms_sql.v7 );
Line: 1399

        p_rule_select_cm_lines_c 	IN OUT NOCOPY INTEGER,
        p_rule_update_cm_lines_c 	IN OUT NOCOPY INTEGER,
        p_rule_insert_dist_c 		IN OUT NOCOPY INTEGER,
        p_rule_insert_cma_c 		IN OUT NOCOPY INTEGER ) IS

    l_rule_select_cm_lines_sql  VARCHAR2(5000);
Line: 1405

    l_rule_update_cm_lines_sql  VARCHAR2(2000);
Line: 1406

    l_rule_insert_dist_sql      VARCHAR2(32767);
Line: 1407

    l_rule_insert_cma_sql       VARCHAR2(2000);
Line: 1459

    l_rule_select_cm_lines_sql :=
'SELECT
cm.customer_trx_line_id,
cm.previous_customer_trx_line_id,
inv_type.allow_overapplication_flag,
cm.revenue_amount,
decode(cmt.credit_method_for_rules,
       ''LIFO'',    ''L'',
       ''PRORATE'', ''P'',
       ''UNIT'',    ''U''),
decode(cmt.credit_method_for_rules,
       ''UNIT'', nvl(cm.last_period_to_credit,
                   nvl(inv.accounting_rule_duration,
                       rule.occurrences)),
       0),
cmt.invoice_currency_code,
/* If the invoice is a Release 9 immediate invoice
   with rules, set the duration to -1 so that it can
   be processed specially. */
decode(inv_trx.created_from || inv_rec.gl_date ||
       nvl(inv.accounting_rule_duration,
           rule.occurrences),
       ''RAXTRX_REL9'' || inv_rev.gl_date || ''1'', ''-1'',
        nvl(inv.accounting_rule_duration,
            rule.occurrences)),
''Y'',
cm_rec.gl_date,
decode(sign( nvl(inv.quantity_invoiced, 0)),
       sign(inv.extended_amount), nvl(inv.quantity_invoiced, 0),
       nvl(inv.quantity_invoiced, 0) * -1 ),
decode(sign( nvl(cm.quantity_credited, 0)),
       sign(cm.extended_amount), nvl(cm.quantity_credited, 0),
       nvl(cm.quantity_credited, 0) * -1 ),
to_char(sign(inv.revenue_amount)),
to_char(sign(cm.revenue_amount)),
CASE
   WHEN rule.type IN (''A'', ''ACC_DUR'') THEN
      ''N''
   ELSE
      ''Y''
   END  partial_period_flag,
inv.unit_selling_price,
cm.unit_selling_price
FROM
ra_rules rule,
ra_cust_trx_line_gl_dist cm_rec,
ra_cust_trx_line_gl_dist inv_rec,
ra_cust_trx_line_gl_dist inv_rev,
ra_cust_trx_types inv_type,
ra_customer_trx inv_trx,
ra_customer_trx_lines inv,
ra_customer_trx cmt,
ra_customer_trx_lines cm
WHERE cm.previous_customer_trx_line_id = inv.customer_trx_line_id
and   inv.customer_trx_id      = inv_trx.customer_trx_id
and   inv_trx.cust_trx_type_id = inv_type.cust_trx_type_id
and   cm.customer_trx_id       = cmt.customer_trx_id
and   inv.accounting_rule_id   = rule.rule_id
and   cm.customer_trx_id       = cm_rec.customer_trx_id
and   inv_trx.customer_trx_id  = inv_rec.customer_trx_id
and   inv_rec.account_class    = ''REC''
and   inv_rec.latest_rec_flag  = ''Y''
and   cm_rec.account_class     = ''REC''
and   cm_rec.account_set_flag  = ''Y''
and   cm.rule_start_date       is null
and   cm.line_type             = ''LINE''
and   inv_rev.cust_trx_line_gl_dist_id =
(
  SELECT nvl(min(inv_dist2.cust_trx_line_gl_dist_id),
             inv_rec.cust_trx_line_gl_dist_id)
  FROM   ra_cust_trx_line_gl_dist inv_dist2
  WHERE  inv.customer_trx_line_id = inv_dist2.customer_trx_line_id
  and    inv_dist2.account_set_flag = ''N''
  and    inv_dist2.account_class IN (''REV'',''UNEARN'')
)
/* Do not create distributions for immediate lines if they
   already exist. */
and not exists
(
  SELECT ''dists exist''
  FROM   ra_cust_trx_line_gl_dist subdist
  WHERE  subdist.customer_trx_line_id = cm.customer_trx_line_id
  and    nvl(inv.accounting_rule_duration, 1) = 1
  and    subdist.account_class IN (''REV'',''UNEARN'')
  and    account_set_flag      = ''N''
) ' || CRLF ||
l_where_pred || CRLF ||
'ORDER BY
cm.previous_customer_trx_line_id,
cmt.trx_date,
cm_rec.gl_date,
cm.customer_trx_line_id';
Line: 1554

    debug(l_rule_select_cm_lines_sql);
Line: 1555

    debug('  len(rule_select_cm_lines_sql) = '||
          to_char(lengthb(l_rule_select_cm_lines_sql)));
Line: 1563

    l_rule_update_cm_lines_sql :=
'UPDATE ra_customer_trx_lines l
SET
l.rule_start_date = :rule_start_date,
l.rule_end_date = :rule_end_date,
l.accounting_rule_duration   = :cm_acct_rule_duration,
l.last_period_to_credit      = decode(:credit_method,
                                    ''U'', :last_period_to_credit,
                                    l.last_period_to_credit)
WHERE  l.customer_trx_line_id       = :customer_trx_line_id
and    :rule_start_date is not null
and    :cm_acct_rule_duration is not null ';
Line: 1580

    debug(l_rule_update_cm_lines_sql);
Line: 1581

    debug('  len(rule_update_cm_lines_sql) = '||
          to_char(lengthb(l_rule_update_cm_lines_sql)));
Line: 1588

    l_rule_insert_dist_sql :=
'INSERT INTO ra_cust_trx_line_gl_dist
(
  cust_trx_line_gl_dist_id,
  created_by,
  creation_date,
  last_updated_by,
  last_update_date,
  last_update_login,
  program_application_id,
  program_id,
  program_update_date,
  request_id,
  posting_control_id,
  customer_trx_id,
  customer_trx_line_id,
  cust_trx_line_salesrep_id,
  gl_date,
  original_gl_date,
  set_of_books_id,
  code_combination_id,
  concatenated_segments,
  account_class,
  account_set_flag,
  amount,
  acctd_amount,
  percent,
  ussgl_transaction_code,
  ussgl_transaction_code_context,
  comments,
  attribute_category,
  attribute1,
  attribute2,
  attribute3,
  attribute4,
  attribute5,
  attribute6,
  attribute7,
  attribute8,
  attribute9,
  attribute10,
  attribute11,
  attribute12,
  attribute13,
  attribute14,
  attribute15,
  collected_tax_ccid,
  collected_tax_concat_seg,
  revenue_adjustment_id,     /* Bug 2543675 */
  org_id
)';
Line: 1659

        l_rule_insert_dist_sql := l_rule_insert_dist_sql || CRLF ||
'(SELECT /*+ ORDERED
             INDEX (ara.ragt ar_revenue_assignments_gt_n1) */
ra_cust_trx_line_gl_dist_s.nextval,  /* cust_trx_line_dist_id */
ctl.created_by,                      /* created_by */
sysdate,                             /* creation_date */
ctl.last_updated_by,                 /* last_updated_by */
sysdate,                             /* last_update_date */
ctl.last_update_login,               /* last_update_login */
ctl.program_application_id,          /* program_application_id */
ctl.program_id,                      /* program_id */
sysdate,                             /* program_update_date */
ctl.request_id,                      /* request _id */
-3,
ctl.customer_trx_id,                 /* customer_trx_id */
:customer_trx_line_id,               /* customer_trx_line_id */
ctls.cust_trx_line_salesrep_id,      /* cust_trx_line_srep_id */
/* Bug 2142941 - use lgd.gl_date and lgd.original_gl_date instead of bind
   variables :gl_date and :original_gl_date */
/* Bug 2194742 - Used bind variable for gl_date */
/* 6129294 - Honor inv gl_date for RAM dists when possible */
DECODE(lgd.revenue_adjustment_id, NULL, :gl_date,
    DECODE(:gl_date_2, lgd.gl_date, :gl_date_3,
         NVL(arp_credit_memo_module.get_valid_date(
                                     lgd.gl_date,
                                     ct.invoicing_rule_id,
                                     lgd.set_of_books_id),
     :gl_date_4))),                    /* gl_date */
lgd.original_gl_date,                /* original_gl_date */
lgd.set_of_books_id,                 /* set_of_books_id */
lgd.code_combination_id,             /* code_combination_id */
lgd.concatenated_segments,           /* concatenated_segments */
lgd.account_class,                   /* account class */
''N'',                                 /* account_set_flag */
decode( fc.minimum_accountable_unit,
         NULL, round( (:amount * ' || l_temp || ' ),
                      fc.precision),
               round( (:amount_1 * ' || l_temp || ' ) /
                      fc.minimum_accountable_unit ) *
               fc.minimum_accountable_unit
       ) * decode(lgd.account_class,
                  ''REV'',  1,
                         -1),         /* amount */';
Line: 1706

            l_rule_insert_dist_sql := l_rule_insert_dist_sql || CRLF ||
'round( decode( fc.minimum_accountable_unit,
               null, round((:amount_2 * ' || l_temp || ' ),
                           fc.precision),
               round((:amount_3 * ' || l_temp || ' ) /
                     fc.minimum_accountable_unit)
                 * fc.minimum_accountable_unit )
        * nvl(ct.exchange_rate, 1) , ' || p_system_info.base_precision ||
      ' )';
Line: 1718

            l_rule_insert_dist_sql := l_rule_insert_dist_sql || CRLF ||
'(round((decode( fc.minimum_accountable_unit,
                 null, round((:amount_2 * ' || l_temp || ' ),
                             fc.precision),
                 round((:amount_3 * ' || l_temp || ' ) /
                       fc.minimum_accountable_unit)
                   * fc.minimum_accountable_unit )
        * nvl(ct.exchange_rate, 1)
         ) / ' || fnd_number.number_to_canonical(p_system_info.base_min_acc_unit) || ' ) *' || CRLF ||
              fnd_number.number_to_canonical(p_system_info.base_min_acc_unit) || ')';
Line: 1731

        l_rule_insert_dist_sql := l_rule_insert_dist_sql || CRLF ||
'       * decode(lgd.account_class,
                 ''REV'',  1,
                 -1),         /* acctd_amount */
decode(lgd.account_class,
       ''UNBILL'',  -1 * round( ( (:amount_4 * ' || l_temp || ' )
                                / decode(ctl.revenue_amount,
                                        0, 1, ctl.revenue_amount)
                                ) * 100, 4),
       ''UNEARN'',  -1 * round( ( (:amount_5 * ' || l_temp || ' )
                                / decode(ctl.revenue_amount,
                                         0, 1, ctl.revenue_amount)
                                ) * 100, 4),
                       round( ( (:amount_6 * ' || l_temp || ' )
                                / decode(ctl.revenue_amount,
                                        0, 1, ctl.revenue_amount)
                                ) * 100, 4)
       ),                            /* percent */
ct.default_ussgl_transaction_code,   /* ussgl_trx_code  */
ct.default_ussgl_trx_code_context,   /* ussgl_trx_code_context */
NULL,                                /* comments */
NULL,                                /* attribute_category */
NULL,                                /* attribute1 */
NULL,                                /* attribute2 */
NULL,                                /* attribute3 */
NULL,                                /* attribute4 */
NULL,                                /* attribute5 */
NULL,                                /* attribute6 */
NULL,                                /* attribute7 */
NULL,                                /* attribute8 */
NULL,                                /* attribute9 */
NULL,                                /* attribute10 */
NULL,                                /* attribute11 */
NULL,                                /* attribute12 */
NULL,                                /* attribute13 */
NULL,                                /* attribute14 */
NULL,                                /* attribute15 */
lgd.collected_tax_ccid,              /* collected tax */
lgd.collected_tax_concat_seg,        /* collected tax seg */
lgd.revenue_adjustment_id,           /* revenue_adjustment_id */ /*Bug 2543675*/
ct.org_id
FROM
ra_customer_trx_lines ctl,
ra_customer_trx ct,
fnd_currencies fc,
ar_revenue_assignments_v ara /* Bug 2347001 */,
ra_cust_trx_line_gl_dist inv_rec,
ra_cust_trx_line_gl_dist lgd,
ra_cust_trx_line_salesreps ctls
WHERE  lgd.customer_trx_line_id      =  ctl.previous_customer_trx_line_id
and    ctl.previous_customer_trx_line_id = ara.customer_trx_line_id
and    ara.session_id                in (:session_id, -99) /**Bug 2347001 */
and    ara.gl_date  = nvl(lgd.original_gl_date, lgd.gl_date)
and    ara.account_class             = lgd.account_class
and    ara.period_set_name           = :period_set_name /* 4254587 */
and    ct.customer_trx_id            = ctl.customer_trx_id
and    inv_rec.customer_trx_id       = ctl.previous_customer_trx_id
and    inv_rec.account_class         = ''REC''
and    inv_rec.latest_rec_flag       = ''Y''
       /* Bug 2899714 */
and    lgd.cust_trx_line_salesrep_id = ctls.prev_cust_trx_line_salesrep_id(+)
       /*7147479*/
and    ctls.customer_trx_line_id(+)  = :customer_trx_line_id_1
and    ctl.customer_trx_line_id      = :customer_trx_line_id_2
and    fc.currency_code              = ct.invoice_currency_code
and    lgd.account_set_flag          = ''N''
and    ( (lgd.account_class in (''REV'', ''UNEARN'', ''UNBILL'')  and
         :insert_offset_1 = ''Y'' ) or
         (lgd.account_class = ''REV''  and :insert_offset_2 = ''N'' ) )
       /* inv_dist_exists is set to "F" when crediting a release 9
          immediate invoice. In this case, the cm gl_date may not correspond
          to any inv gl_date, so the date check cannot be done. */
/* Bug 2142941 - include join onto lgd.original_gl_date */
and    (
         ( trunc(ara.gl_date)   = lgd.original_gl_date AND
           lgd.original_gl_date = :original_gl_date_1)
        OR
          :check_gl_date_1 = ''N''
       )
/* Bug 2535023 - Revamped fixes from bugs 1936152 and 2354805
   so that the insert now relies upon rec_offset_flag instead
   of that and-not stuff.  Forced routine to only
   copy conventional distributions. */
/* Bug 2543675 - include RAM distributions */
and    lgd.rec_offset_flag is null';
Line: 1823

      l_rule_insert_dist_sql := l_rule_insert_dist_sql || CRLF ||
'and    (ar_revenue_management_pvt.line_collectible(ctl.previous_customer_trx_id,
                                             ctl.previous_customer_trx_line_id)
         IN (1,2) or
         lgd.revenue_adjustment_id is null)';
Line: 1831

   l_rule_insert_dist_sql := l_rule_insert_dist_sql || CRLF || ')';
Line: 1840

        l_rule_insert_dist_sql := l_rule_insert_dist_sql || CRLF ||
' (SELECT
ra_cust_trx_line_gl_dist_s.nextval,  /* cust_trx_line_dist_id */
ct.created_by,                       /* created_by */
sysdate,                             /* creation_date */
ct.last_updated_by,                  /* last_updated_by */
sysdate,                             /* last_update_date */
ct.last_update_login,                /* last_update_login */
ct.program_application_id,           /* program_application_id */
ct.program_id,                       /* program_id */
sysdate,                             /* program_update_date */
ct.request_id,                       /* request _id */
-3,
ct.customer_trx_id,                  /* customer_trx_id */
:customer_trx_line_id,               /* customer_trx_line_id */
lgd.cust_trx_line_salesrep_id,       /* cust_trx_line_srep_id */
:gl_date,                            /* gl_date */
:original_gl_date,                   /* original_gl_date */
lgd.set_of_books_id,                 /* set_of_books_id */
lgd.code_combination_id,             /* code_combination_id */
lgd.concatenated_segments,           /* concatenated_segments */
lgd.account_class,                   /* account class */
''N'',                                 /* account_set_flag */
decode( fc.minimum_accountable_unit,
        NULL, round( (:amount * (lgd.percent / 100) ),
                     fc.precision),
              round( (:amount_1 * (lgd.percent / 100) ) /
                     fc.minimum_accountable_unit ) *
              fc.minimum_accountable_unit
      ) * decode(lgd.account_class,
                 ''REV'',  1,
                        -1),         /* amount */ ';
Line: 1876

            l_rule_insert_dist_sql := l_rule_insert_dist_sql || CRLF ||
'round( decode( fc.minimum_accountable_unit,
                null, round((:amount_2 * (lgd.percent / 100) ),
                            fc.precision),
                round((:amount_3 * (lgd.percent / 100) ) /
                      fc.minimum_accountable_unit)
                  * fc.minimum_accountable_unit )
        * nvl(ct.exchange_rate, 1), ' || p_system_info.base_precision ||
      ' )';
Line: 1888

            l_rule_insert_dist_sql := l_rule_insert_dist_sql || CRLF ||
'(round((decode( fc.minimum_accountable_unit,
                 null, round((:amount_2 * (lgd.percent / 100) ),
                             fc.precision),
                 round((:amount_3 * (lgd.percent / 100) ) /
                       fc.minimum_accountable_unit)
                   * fc.minimum_accountable_unit )
         * nvl(ct.exchange_rate, 1)
         ) / ' || fnd_number.number_to_canonical(p_system_info.base_min_acc_unit) || ' ) *' || CRLF ||
              fnd_number.number_to_canonical(p_system_info.base_min_acc_unit) || ')';
Line: 1901

        l_rule_insert_dist_sql := l_rule_insert_dist_sql || CRLF ||
'       * decode(lgd.account_class,
                  ''REV'',  1,
                  -1),        /* acctd_amount */
decode(lgd.account_class,
       ''UNBILL'',  -1 * round( ( (:amount_4 * (lgd.percent / 100) )
                                / decode(ctl.revenue_amount,
                                        0, 1, ctl.revenue_amount)
                                ) * 100, 4),
       ''UNEARN'',  -1 * round( ( (:amount_5 * (lgd.percent / 100) )
                                / decode(ctl.revenue_amount,
                                         0, 1, ctl.revenue_amount)
                                ) * 100, 4),
                       round( ( (:amount_6 * (lgd.percent / 100) )
                                / decode(ctl.revenue_amount,
                                        0, 1, ctl.revenue_amount)
                                ) * 100, 4)
       ),                            /* percent */
ct.default_ussgl_transaction_code,   /* ussgl_trx_code  */
ct.default_ussgl_trx_code_context,   /* ussgl_trx_code_context */
lgd.comments,                   /* comments */
lgd.attribute_category,         /* attribute_category */
lgd.attribute1,                 /* attribute1 */
lgd.attribute2,                 /* attribute2 */
lgd.attribute3,                 /* attribute3 */
lgd.attribute4,                 /* attribute4 */
lgd.attribute5,                 /* attribute5 */
lgd.attribute6,                 /* attribute6 */
lgd.attribute7,                 /* attribute7 */
lgd.attribute8,                 /* attribute8 */
lgd.attribute9,                 /* attribute9 */
lgd.attribute10,                /* attribute10 */
lgd.attribute11,                /* attribute11 */
lgd.attribute12,                /* attribute12 */
lgd.attribute13,                /* attribute13 */
lgd.attribute14,                /* attribute14 */
lgd.attribute15,                /* attribute1 */
lgd.collected_tax_ccid,         /* collected tax */
lgd.collected_tax_concat_seg,   /* collected tax seg */
lgd.revenue_adjustment_id,      /* revenue_adjustment_id */ /*Bug 2543675*/
ct.org_id
FROM
ra_cust_trx_line_gl_dist lgd,
fnd_currencies fc,
ra_customer_trx_lines ctl,
ra_customer_trx ct,
ra_customer_trx_lines ictl,
ra_rules ir
WHERE  lgd.customer_trx_id        = ct.customer_trx_id
and    lgd.customer_trx_line_id   = :customer_trx_line_id_1
and    lgd.customer_trx_line_id   = ctl.customer_trx_line_id
and    fc.currency_code           = ct.invoice_currency_code
and    account_set_flag           = ''Y''
and    ( (lgd.account_class in (''REV'', ''UNEARN'', ''UNBILL'')  and
         :insert_offset_1 = ''Y'' ) or
         (lgd.account_class = ''REV''  and :insert_offset_2 = ''N'' ) )
/* Bug 2559653 - generate nothing for deferred rules
    unless it is an ARREARS transaction */
and    ctl.previous_customer_trx_line_id = ictl.customer_trx_line_id
and    ictl.accounting_rule_id = ir.rule_id
and    (nvl(ir.deferred_revenue_flag, ''N'') = ''N'' or
        ct.invoicing_rule_id = -3)
/* no-effect pred, for binding purposes */
and    :check_gl_date_1 = :check_gl_date_2
and    :gl_date_1                   is not null ';
Line: 1974

      l_rule_insert_dist_sql := l_rule_insert_dist_sql || CRLF ||
'and   ar_revenue_management_pvt.line_collectible(ctl.previous_customer_trx_id,
                  ctl.previous_customer_trx_line_id) IN (1,2) ';
Line: 1980

   l_rule_insert_dist_sql := l_rule_insert_dist_sql || ')';
Line: 1988

    debug( l_rule_insert_dist_sql);
Line: 1989

    debug('  len(rule_insert_dist_sql) = '||
          to_char(lengthb(l_rule_insert_dist_sql)));
Line: 1995

    l_rule_insert_cma_sql :=
'INSERT INTO ar_credit_memo_amounts
(
 credit_memo_amount_id,
 last_updated_by,
 last_update_date,
 last_update_login,
 created_by,
 creation_date,
 customer_trx_line_id,
 gl_date,
 amount,
 program_application_id,
 program_id,
 program_update_date,
 request_id
)
SELECT
ar_credit_memo_amounts_s.nextval,       /* credit_memo_amount_id */' || CRLF ||
p_profile_info.user_id || ',                      /* last_updated_by */
sysdate,                                 /* last_update_date */' || CRLF ||
p_profile_info.conc_login_id || ',          /* last_update_login */' || CRLF ||
p_profile_info.user_id || ',             /* created_by */
sysdate,                                      /* creation_date */
:customer_trx_line_id,                        /* customer_trx_line_id */
:gl_date,                                     /* gl_date */
:amount,                                      /* amount */' || CRLF ||
profile_info.application_id || ',                /* program_application_id */'
|| CRLF || profile_info.conc_program_id || ',               /* program_id */
sysdate,                                      /* program_update_date */';
Line: 2027

        l_rule_insert_cma_sql := l_rule_insert_cma_sql || CRLF ||
'0' || '     /* request_id */';
Line: 2030

        l_rule_insert_cma_sql := l_rule_insert_cma_sql || CRLF ||
':request_id      /* request_id */';
Line: 2034

    l_rule_insert_cma_sql := l_rule_insert_cma_sql || CRLF ||
'FROM   dual ';
Line: 2040

    debug(l_rule_insert_cma_sql);
Line: 2041

    debug('  len(rule_insert_cma_sql) = '||
          to_char(lengthb(l_rule_insert_cma_sql)));
Line: 2050

        p_rule_select_cm_lines_c := dbms_sql.open_cursor;
Line: 2051

        dbms_sql.parse( p_rule_select_cm_lines_c, l_rule_select_cm_lines_sql,
                        dbms_sql.v7 );
Line: 2054

        debug(' parsed p_rule_select_cm_lines_c');
Line: 2056

        p_rule_update_cm_lines_c := dbms_sql.open_cursor;
Line: 2057

        dbms_sql.parse( p_rule_update_cm_lines_c, l_rule_update_cm_lines_sql,
                        dbms_sql.v7 );
Line: 2060

        debug(' parsed p_rule_update_cm_lines_c');
Line: 2062

        p_rule_insert_dist_c := dbms_sql.open_cursor;
Line: 2063

        dbms_sql.parse( p_rule_insert_dist_c, l_rule_insert_dist_sql,
                        dbms_sql.v7 );
Line: 2066

        debug(' parsed p_rule_insert_dist_c');
Line: 2068

        p_rule_insert_cma_c := dbms_sql.open_cursor;
Line: 2069

        dbms_sql.parse( p_rule_insert_cma_c, l_rule_insert_cma_sql,
                        dbms_sql.v7 );
Line: 2072

        debug(' parsed p_rule_insert_cma_c');
Line: 2128

'SELECT
distinct
cnr.previous_customer_trx_line_id,
cnr.gl_date,
cnr.amount,
cnr.net_unit_price,
decode(trx.created_from,
       ''RAXTRX_REL9'', ''Y'',
       decode(lgd.customer_trx_id,
              NULL,  ''N'',
              ''Y'')
       )      /* inv dist exists */
FROM ra_cust_trx_line_gl_dist lgd,
     ra_customer_trx trx,
     ar_cm_net_revenue_form cnr
WHERE  cnr.previous_customer_trx_line_id  = :start_prev_ctlid
and    cnr.previous_customer_trx_line_id  =  lgd.customer_trx_line_id(+)
and    cnr.previous_customer_trx_id       =  trx.customer_trx_id
and    cnr.gl_date                        =  lgd.original_gl_date(+)
and    ''N''                              =  lgd.account_set_flag(+)
and    cnr.period_set_name                = :period_set_name
ORDER BY
   cnr.previous_customer_trx_line_id,
   cnr.gl_date';
Line: 2382

       SELECT cust_trx_line_gl_dist_id
       FROM   ra_cust_trx_line_gl_dist gld
       WHERE  gld.customer_trx_id = p_trx_id
       AND    gld.customer_trx_line_id =
                 nvl(p_trx_line_id, gld.customer_trx_line_id);
Line: 2402

                       nonrule_insert_dist_c,
                       nonrule_update_lines_c,
                       nonrule_update_dist_c,
                       nonrule_update_dist2_c );
Line: 2414

           /* INSERT */
           dbms_sql.bind_variable( nonrule_insert_dist_c,
                                   'cm_customer_trx_id_1',
                                   p_cm_control.customer_trx_id );
Line: 2419

           dbms_sql.bind_variable( nonrule_insert_dist_c,
                                   'cm_customer_trx_id_2',
                                   p_cm_control.customer_trx_id );
Line: 2422

           /* UPDATE LINE */
           dbms_sql.bind_variable( nonrule_update_lines_c,
                                   'cm_customer_trx_id_1',
                                   p_cm_control.customer_trx_id );
Line: 2427

           /* UPDATE DIST */
           dbms_sql.bind_variable( nonrule_update_dist_c,
                                   'cm_customer_trx_id_1',
                                   p_cm_control.customer_trx_id );
Line: 2435

           dbms_sql.bind_variable( nonrule_insert_dist_c,
                                   'cm_customer_trx_id_3',
                                   p_cm_control.customer_trx_id );
Line: 2442

          debug( 'EXCEPTION: Error in binding nonrule_insert_dist_c' );
Line: 2447

        dbms_sql.bind_variable( nonrule_update_dist2_c,
                                'cm_customer_trx_id',
                                p_cm_control.customer_trx_id );
Line: 2452

          debug( 'EXCEPTION: Error in binding nonrule_update_dist2_c' );
Line: 2459

           /* INSERT */
           dbms_sql.bind_variable( nonrule_insert_dist_c,
	                           'request_id_1',
	   	  		   p_cm_control.request_id );
Line: 2463

           dbms_sql.bind_variable( nonrule_insert_dist_c,
	                           'request_id_2',
	   	  		   p_cm_control.request_id );
Line: 2466

           /* UPDATE LINES */
           dbms_sql.bind_variable( nonrule_update_lines_c,
                                   'request_id_1',
                                   p_cm_control.request_id );
Line: 2470

           /* UPDATE DISTS */
           dbms_sql.bind_variable( nonrule_update_dist_c,
                                   'request_id_1',
                                   p_cm_control.request_id );
Line: 2479

           dbms_sql.bind_variable( nonrule_insert_dist_c,
	                           'request_id_3',
	   	  		   p_cm_control.request_id );
Line: 2486

          debug( 'EXCEPTION: Error in binding nonrule_insert_dist_c' );
Line: 2491

      dbms_sql.bind_variable( nonrule_update_dist2_c,
                              'request_id',
			      p_cm_control.request_id );
Line: 2496

          debug( 'EXCEPTION: Error in binding nonrule_update_dist2_c' );
Line: 2506

           /* INSERT */
           dbms_sql.bind_variable( nonrule_insert_dist_c,
                                   'cm_cust_trx_line_id_1',
                                   p_cm_control.customer_trx_line_id );
Line: 2510

           dbms_sql.bind_variable( nonrule_insert_dist_c,
                                   'cm_cust_trx_line_id_2',
                                   p_cm_control.customer_trx_line_id );
Line: 2513

           dbms_sql.bind_variable( nonrule_insert_dist_c,
                                   'cm_cust_trx_line_id_3',
                                   p_cm_control.customer_trx_line_id );
Line: 2516

           dbms_sql.bind_variable( nonrule_insert_dist_c,
                                   'cm_cust_trx_line_id_4',
                                   p_cm_control.customer_trx_line_id );
Line: 2519

           /* UPDATE LINES */
           dbms_sql.bind_variable( nonrule_update_lines_c,
                                   'cm_cust_trx_line_id_1',
                                   p_cm_control.customer_trx_line_id );
Line: 2523

           dbms_sql.bind_variable( nonrule_update_lines_c,
                                   'cm_cust_trx_line_id_2',
                                   p_cm_control.customer_trx_line_id );
Line: 2526

           /* UPDATE DISTS */
           dbms_sql.bind_variable( nonrule_update_dist_c,
                                   'cm_cust_trx_line_id_1',
                                   p_cm_control.customer_trx_line_id );
Line: 2530

           dbms_sql.bind_variable( nonrule_update_dist_c,
                                   'cm_cust_trx_line_id_2',
                                   p_cm_control.customer_trx_line_id );
Line: 2536

          debug( 'EXCEPTION: Error in binding nonrule_insert_dist_c' );
Line: 2545

    debug( '  Inserting distributions', MSG_LEVEL_DEBUG );
Line: 2548

        l_ignore := dbms_sql.execute( nonrule_insert_dist_c );
Line: 2549

        close_cursor( nonrule_insert_dist_c );
Line: 2551

        debug( to_char(l_ignore) || ' row(s) inserted', MSG_LEVEL_DEBUG );
Line: 2557

          debug( 'EXCEPTION: Error executing insert dist stmt' );
Line: 2567

        l_ignore := dbms_sql.execute( nonrule_update_lines_c );
Line: 2568

        close_cursor( nonrule_update_lines_c );
Line: 2569

        debug( to_char(l_ignore) || ' row(s) updated', MSG_LEVEL_DEBUG );
Line: 2573

          debug( 'EXCEPTION: Error executing update lines stmt' );
Line: 2583

        l_ignore := dbms_sql.execute( nonrule_update_dist_c );
Line: 2584

        close_cursor( nonrule_update_dist_c );
Line: 2586

        debug( to_char(l_ignore) || ' row(s) updated', MSG_LEVEL_DEBUG );
Line: 2592

          debug( 'EXCEPTION: Error executing update dist stmt' );
Line: 2602

        l_ignore := dbms_sql.execute( nonrule_update_dist2_c );
Line: 2603

        close_cursor( nonrule_update_dist2_c );
Line: 2605

        debug( to_char(l_ignore) || ' row(s) updated', MSG_LEVEL_DEBUG );
Line: 2611

          debug( 'EXCEPTION: Error executing update stmt' );
Line: 2647

PROCEDURE define_select_columns(
	p_select_c   IN INTEGER,
        p_select_rec IN OUT NOCOPY select_rec_type ) IS

BEGIN

    print_fcn_label2( 'arp_credit_memo_module.define_select_columns()+' );
Line: 2655

    dbms_sql.define_column( p_select_c, 1, p_select_rec.customer_trx_line_id );
Line: 2656

    dbms_sql.define_column( p_select_c, 2,
                            p_select_rec.prev_cust_trx_line_id );
Line: 2658

    dbms_sql.define_column( p_select_c, 3,
                            p_select_rec.allow_overapp_flag, 1 );
Line: 2660

    dbms_sql.define_column( p_select_c, 4, p_select_rec.cm_amount );
Line: 2661

    dbms_sql.define_column( p_select_c, 5,
                            p_select_rec.credit_method_for_rules, 1 );
Line: 2663

    dbms_sql.define_column( p_select_c, 6,
                            p_select_rec.last_period_to_credit );
Line: 2665

    dbms_sql.define_column( p_select_c, 7, p_select_rec.currency_code, 15 );
Line: 2666

    dbms_sql.define_column( p_select_c, 8,
                            p_select_rec.inv_acct_rule_duration );
Line: 2668

    dbms_sql.define_column( p_select_c, 9,
                            p_select_rec.allow_not_open_flag, 1 );
Line: 2670

    dbms_sql.define_column( p_select_c, 10, p_select_rec.cm_gl_date );
Line: 2671

    dbms_sql.define_column( p_select_c, 11, p_select_rec.invoice_quantity );
Line: 2672

    dbms_sql.define_column( p_select_c, 12,
                            p_select_rec.cm_quantity );
Line: 2674

    dbms_sql.define_column( p_select_c, 13,
                            p_select_rec.invoice_sign);
Line: 2676

    dbms_sql.define_column( p_select_c, 14, p_select_rec.cm_sign);
Line: 2677

    dbms_sql.define_column( p_select_c, 15, p_select_rec.partial_period_flag,1);
Line: 2679

    dbms_sql.define_column( p_select_c, 16, p_select_rec.inv_unit_price);
Line: 2680

    dbms_sql.define_column( p_select_c, 17, p_select_rec.cm_unit_price);
Line: 2683

    print_fcn_label2( 'arp_credit_memo_module.define_select_columns()-' );
Line: 2687

        debug('EXCEPTION: arp_credit_memo_module.define_select_columns()');
Line: 2689

END define_select_columns;
Line: 2713

PROCEDURE get_select_column_values(
	p_select_c   IN INTEGER,
        p_select_rec IN OUT NOCOPY select_rec_type ) IS
BEGIN

    print_fcn_label2( 'arp_credit_memo_module.get_select_column_values()+' );
Line: 2720

    dbms_sql.column_value( p_select_c, 1, p_select_rec.customer_trx_line_id );
Line: 2721

    dbms_sql.column_value( p_select_c, 2, p_select_rec.prev_cust_trx_line_id );
Line: 2722

    dbms_sql.column_value( p_select_c, 3,
                           p_select_rec.allow_overapp_flag );
Line: 2724

    dbms_sql.column_value( p_select_c, 4, p_select_rec.cm_amount );
Line: 2725

    dbms_sql.column_value( p_select_c, 5,
                           p_select_rec.credit_method_for_rules );
Line: 2727

    dbms_sql.column_value( p_select_c, 6, p_select_rec.last_period_to_credit );
Line: 2728

    dbms_sql.column_value( p_select_c, 7, p_select_rec.currency_code );
Line: 2729

    dbms_sql.column_value( p_select_c, 8,
                           p_select_rec.inv_acct_rule_duration );
Line: 2731

    dbms_sql.column_value( p_select_c, 9, p_select_rec.allow_not_open_flag );
Line: 2732

    dbms_sql.column_value( p_select_c, 10, p_select_rec.cm_gl_date );
Line: 2733

    dbms_sql.column_value( p_select_c, 11, p_select_rec.invoice_quantity );
Line: 2734

    dbms_sql.column_value( p_select_c, 12,
                           p_select_rec.cm_quantity );
Line: 2736

    dbms_sql.column_value( p_select_c, 13,
                           p_select_rec.invoice_sign );
Line: 2738

    dbms_sql.column_value( p_select_c, 14, p_select_rec.cm_sign );
Line: 2739

    dbms_sql.column_value( p_select_c, 15, p_select_rec.partial_period_flag);
Line: 2741

    dbms_sql.column_value( p_select_c, 16, p_select_rec.inv_unit_price);
Line: 2742

    dbms_sql.column_value( p_select_c, 17, p_select_rec.cm_unit_price);
Line: 2746

    print_fcn_label2( 'arp_credit_memo_module.get_select_column_values()-' );
Line: 2750

        debug('EXCEPTION: arp_credit_memo_module.get_select_column_values()');
Line: 2752

END get_select_column_values;
Line: 2775

PROCEDURE dump_select_rec( p_select_rec IN select_rec_type ) IS
BEGIN

    print_fcn_label2( 'arp_credit_memo_module.dump_select_rec()+' );
Line: 2780

    debug( '  Dumping select record: ', MSG_LEVEL_DEBUG );
Line: 2782

           || to_char( p_select_rec.customer_trx_line_id ), MSG_LEVEL_DEBUG );
Line: 2784

           || to_char( p_select_rec.prev_cust_trx_line_id ), MSG_LEVEL_DEBUG );
Line: 2785

    debug( '  allow_overapp_flag=' || p_select_rec.allow_overapp_flag ,
          MSG_LEVEL_DEBUG );
Line: 2788

           || to_char( p_select_rec.cm_amount ), MSG_LEVEL_DEBUG );
Line: 2790

           || p_select_rec.credit_method_for_rules, MSG_LEVEL_DEBUG );
Line: 2792

           || to_char( p_select_rec.last_period_to_credit ), MSG_LEVEL_DEBUG );
Line: 2793

    debug( '  currency_code=' || p_select_rec.currency_code, MSG_LEVEL_DEBUG );
Line: 2795

          || to_char( p_select_rec.inv_acct_rule_duration ), MSG_LEVEL_DEBUG );
Line: 2797

           p_select_rec.allow_not_open_flag, MSG_LEVEL_DEBUG );
Line: 2798

    debug( '  cm_gl_date=' || to_char( p_select_rec.cm_gl_date ),
           MSG_LEVEL_DEBUG );
Line: 2801

           || to_char( p_select_rec.invoice_quantity ), MSG_LEVEL_DEBUG );
Line: 2803

           to_char( p_select_rec.cm_quantity ), MSG_LEVEL_DEBUG );
Line: 2805

           || p_select_rec.invoice_sign, MSG_LEVEL_DEBUG );
Line: 2807

           || p_select_rec.cm_sign, MSG_LEVEL_DEBUG );
Line: 2809

           || p_select_rec.inv_unit_price, MSG_LEVEL_DEBUG );
Line: 2811

           || p_select_rec.cm_unit_price, MSG_LEVEL_DEBUG );
Line: 2814

           || p_select_rec.partial_period_flag, MSG_LEVEL_DEBUG );
Line: 2816

    print_fcn_label2( 'arp_credit_memo_module.dump_select_rec()-' );
Line: 2820

        debug( 'EXCEPTION: arp_credit_memo_module.dump_select_rec()' );
Line: 2822

END dump_select_rec;
Line: 2908

      SELECT invline.accounting_rule_id,
             invline.rule_start_date,
             cmline.extended_amount line_amount
      FROM   ra_customer_trx_lines_all cmline,
             ra_customer_trx_lines_all invline
      WHERE  cmline.previous_customer_trx_line_id =
             invline.customer_trx_line_id
      AND    cmline.customer_trx_line_id = p_cm_line_id;
Line: 2929

      distribution lines will be inserted in ascending order of
      gl_date when the rules method is 'PRORATE'. */
   /* Bug 2136455: Added 'REVERSE' for all rule methods */

    FOR i in REVERSE 0..cm_sched_index - 1 LOOP

        debug('  customer_trx_line_id='||cm_sched_ctlid_t( i ),
                MSG_LEVEL_DEBUG);
Line: 2942

        debug('  insert_offset='||cm_sched_insert_offset_t( i ),
                MSG_LEVEL_DEBUG);
Line: 2948

        IF( cm_sched_insert_dist_t( i ) = YES ) THEN
            -------------------------------------------------------------
            -- Insert into ra_cust_trx_line_gl_dist
            -------------------------------------------------------------
            -------------------------------------------------------------
            -- Bind vars
            -------------------------------------------------------------
            BEGIN
                debug( '  Binding rule_insert_dist_c', MSG_LEVEL_DEBUG );
Line: 2958

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'customer_trx_line_id',
                                        cm_sched_ctlid_t( i ) );
Line: 2962

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'customer_trx_line_id_1',
                                        cm_sched_ctlid_t( i ) );
Line: 2965

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'gl_date',
                                        cm_sched_gl_date_t( i ) );
Line: 2968

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'amount',
                                        cm_sched_amount_t( i ) );
Line: 2971

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'amount_1',
                                        cm_sched_amount_t( i ) );
Line: 2974

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'amount_2',
                                        cm_sched_amount_t( i ) );
Line: 2977

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'amount_3',
                                        cm_sched_amount_t( i ) );
Line: 2980

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'amount_4',
                                        cm_sched_amount_t( i ) );
Line: 2983

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'amount_5',
                                        cm_sched_amount_t( i ) );
Line: 2986

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'amount_6',
                                        cm_sched_amount_t( i ) );
Line: 2989

                /*7147479 changed bind variable insert_offset_1*/
                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'insert_offset_1',
                                        cm_sched_insert_offset_t( i ) );
Line: 2993

                /*7147479 added extra bind variable insert_offset_2*/
                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'insert_offset_2',
                                        cm_sched_insert_offset_t( i ) );
Line: 2998

                 dbms_sql.bind_variable( rule_insert_dist_c,
                                          'check_gl_date_1',
                                          cm_sched_check_gl_date_t( i ) );
Line: 3007

                   dbms_sql.bind_variable( rule_insert_dist_c,
                                        'period_set_name',
                                        system_info.period_set_name );
Line: 3012

                   dbms_sql.bind_variable( rule_insert_dist_c,
                                           'session_id',
                                           g_session_id);
Line: 3017

		   dbms_sql.bind_variable( rule_insert_dist_c,
                                           'customer_trx_line_id_2',
                                           cm_sched_ctlid_t( i ) );
Line: 3020

                   dbms_sql.bind_variable( rule_insert_dist_c,
                                           'original_gl_date_1',
                                           cm_sched_orig_gl_date_t( i ) );
Line: 3023

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'gl_date_2',
                                        cm_sched_gl_date_t( i ) );
Line: 3026

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'gl_date_3',
                                        cm_sched_gl_date_t( i ) );
Line: 3029

                dbms_sql.bind_variable( rule_insert_dist_c,
                                        'gl_date_4',
                                        cm_sched_gl_date_t( i ) );
Line: 3034

		   /* in SELECT portion of statement */
                   dbms_sql.bind_variable( rule_insert_dist_c,
                                           'original_gl_date',
                                           cm_sched_orig_gl_date_t( i ) );
Line: 3039

                   dbms_sql.bind_variable( rule_insert_dist_c,
                                           'gl_date_1',
                                           cm_sched_gl_date_t( i ) );
Line: 3043

                   dbms_sql.bind_variable( rule_insert_dist_c,
                                           'check_gl_date_2',
                                           cm_sched_check_gl_date_t( i ) );
Line: 3050

                debug( 'EXCEPTION: Error in binding rule_insert_dist_c' );
Line: 3058

             arp_standard.debug( '  Inserting distributions');
Line: 3059

                l_ignore := dbms_sql.execute( rule_insert_dist_c );
Line: 3062

                debug( 'EXCEPTION: Error executing insert dist stmt' );
Line: 3067

        IF( cm_sched_insert_cma_t( i ) = YES ) THEN
            -------------------------------------------------------------
            -- Insert into ar_credit_memo_amounts
            -------------------------------------------------------------

           /*--------------------------------------------------------------
            | Bug # 2988282 - ORASHID: 07-07-2003
            | If the credit memo line amount is zero and the corresponding
            | invoice line is rule based then use the invoice line's
            | rule start date as the gl date, otherwise continue with
            | exisitng flow.
            +--------------------------------------------------------------*/

            OPEN lines(cm_sched_ctlid_t(i));
Line: 3099

            dbms_sql.bind_variable( rule_insert_cma_c,
                                    'customer_trx_line_id',
                                    cm_sched_ctlid_t( i ) );
Line: 3102

            dbms_sql.bind_variable( rule_insert_cma_c,
                                    'gl_date',
                                    l_original_gl_date);
Line: 3105

            dbms_sql.bind_variable( rule_insert_cma_c,
                                    'amount',
                                    cm_sched_amount_t( i ) );
Line: 3113

                debug( '  Inserting CM amounts', MSG_LEVEL_DEBUG );
Line: 3114

                l_ignore := dbms_sql.execute( rule_insert_cma_c );
Line: 3115

                debug( to_char(l_ignore) || ' row(s) inserted',
                       MSG_LEVEL_DEBUG );
Line: 3120

                debug( 'EXCEPTION: Error executing insert cma stmt' );
Line: 3147

    cm_sched_insert_dist_t:=null_cm_sched_insert_dist;
Line: 3148

    cm_sched_insert_cma_t:=null_cm_sched_insert_cma;
Line: 3149

    cm_sched_insert_offset_t:=null_cm_sched_insert_offset;
Line: 3196

PROCEDURE insert_cm_schedule(
	p_line_id 		IN BINARY_INTEGER,
	p_gl_date 		IN DATE,
	p_orig_gl_date 		IN DATE,
	p_amount 		IN NUMBER,
	p_insert_dist_flag 	IN VARCHAR,
	p_insert_cma_flag 	IN VARCHAR,
	p_insert_offset_flag 	IN VARCHAR,
	p_check_gl_date_flag 	IN VARCHAR )  IS

    l_index BINARY_INTEGER;
Line: 3210

    print_fcn_label('arp_credit_memo_module.insert_cm_schedule()+' );
Line: 3221

    debug('  p_insert_dist-flag='||p_insert_dist_flag, MSG_LEVEL_DEBUG);
Line: 3222

    debug('  p_insert_cma_flag='||p_insert_cma_flag, MSG_LEVEL_DEBUG);
Line: 3223

    debug('  p_insert_offset_flag='||p_insert_offset_flag, MSG_LEVEL_DEBUG);
Line: 3231

    cm_sched_insert_dist_t( cm_sched_index ) 	:= p_insert_dist_flag;
Line: 3232

    cm_sched_insert_cma_t( cm_sched_index ) 	:= p_insert_cma_flag;
Line: 3233

    cm_sched_insert_offset_t( cm_sched_index ) 	:= p_insert_offset_flag;
Line: 3238

    print_fcn_label('arp_credit_memo_module.insert_cm_schedule()-' );
Line: 3243

        debug( 'EXCEPTION: arp_credit_memo_module.insert_cm_schedule('
		|| to_char(p_line_id) || ', '
		|| to_char(p_gl_date) || ', '
		|| to_char(p_orig_gl_date) || ', '
		|| to_char(p_amount) || ', '
		|| p_insert_dist_flag || ', '
		|| p_insert_cma_flag || ', '
		|| p_insert_offset_flag || ', '
		|| p_check_gl_date_flag || ')' );
Line: 3254

END insert_cm_schedule;
Line: 3282

FUNCTION update_cm_schedule(
	p_line_id 	IN BINARY_INTEGER,
	p_gl_date 	IN DATE,
	p_amount 	IN NUMBER )

    RETURN BOOLEAN  IS

    l_index BINARY_INTEGER;
Line: 3293

    print_fcn_label('arp_credit_memo_module.update_cm_schedule()+' );
Line: 3299

        print_fcn_label('arp_credit_memo_module.update_cm_schedule()-' );
Line: 3310

    print_fcn_label('arp_credit_memo_module.update_cm_schedule()-' );
Line: 3315

        debug( 'EXCEPTION: arp_credit_memo_module.update_cm_schedule('
		|| to_char(p_line_id) || ', '
		|| to_char(p_gl_date) || ', '
		|| to_char(p_amount) || ')' );
Line: 3321

END update_cm_schedule;
Line: 3360

	p_insert_dist_flag 	IN VARCHAR,
	p_insert_cma_flag 	IN VARCHAR,
	p_insert_offset_flag 	IN VARCHAR,
	p_check_gl_date_flag 	IN VARCHAR )
IS

    l_gl_date DATE;
Line: 3384

    debug('  p_insert_dist_flag='||p_insert_dist_flag, MSG_LEVEL_DEBUG);
Line: 3385

    debug('  p_insert_cma_flag='||p_insert_cma_flag, MSG_LEVEL_DEBUG);
Line: 3412

          SELECT invoicing_rule_id
          INTO l_rule_id
          FROM ra_customer_trx ct, ra_customer_trx_lines ctl
          WHERE ct.customer_trx_id = ctl.customer_trx_id
          AND ctl.customer_trx_line_id = p_line_id;
Line: 3449

        insert_cm_schedule( p_line_id,
			    l_gl_date,
			    p_gl_date,  -- orig_gl_date
			    p_amount,
			    p_insert_dist_flag,
			    p_insert_cma_flag,
			    p_insert_offset_flag,
			    p_check_gl_date_flag );
Line: 3462

        l_bool := update_cm_schedule( p_line_id,
			     	      p_gl_date,
			     	      p_amount );
Line: 3469

	    -- gl_date not in cm schedule tables, update the database
	    ----------------------------------------------------------------
	    BEGIN
		debug( '  Updating ar_credit_memo_amounts table',
			MSG_LEVEL_DEBUG );
Line: 3475

	        UPDATE ar_credit_memo_amounts
	        SET amount = amount + p_amount
	        WHERE customer_trx_line_id = p_line_id
	        and gl_date = p_gl_date;
Line: 3480

		debug( SQL%ROWCOUNT||' row(s) updated', MSG_LEVEL_DEBUG );
Line: 3484

		    -- Update successful
		    --
		    -- Only create REV dist,
		    -- don't create cm amounts rec
		    ----------------------------------------------------
		    debug( '  Update successful', MSG_LEVEL_DEBUG );
Line: 3490

                    insert_cm_schedule( p_line_id,
			     	        l_gl_date,
			     	        p_gl_date,  -- orig_gl_date
			     	        p_amount,
			     	        p_insert_dist_flag,
				        NO,  -- p_insert_cma_flag
				        NO,  -- p_insert_offset_flag
				        YES  -- p_check_gl_date_flag
                                      );
Line: 3502

        	    -- No cma record to update.
        	    -- Entire cm line is an overapplication
		    -- Insert a cma record into array and increment
		    -- cm_acct_rule_duration
                    -- Create REV and UNEARN dist
		    ----------------------------------------------------

		    debug( '  Update unsuccessful', MSG_LEVEL_DEBUG );
Line: 3512

        	    insert_cm_schedule( p_line_id,
			    		l_gl_date,
			    		p_gl_date,  -- orig_gl_date
			    		p_amount,
			    		p_insert_dist_flag,
			    		YES,        -- p_insert_cma_flag
			    		YES,        -- p_insert_offset_flag
			    		YES         -- p_check_gl_date_flag
			  	      );
Line: 3553

		|| p_insert_dist_flag || ', '
		|| p_insert_cma_flag || ', '
		|| p_insert_offset_flag || ', '
		|| p_check_gl_date_flag || ')' );
Line: 3732

PROCEDURE update_net_revenue(
	p_index 	IN BINARY_INTEGER,
	p_amount 	IN NUMBER ) IS

BEGIN

    net_rev_amount_t( p_index ) := p_amount;
Line: 3742

        debug( 'EXCEPTION: arp_credit_memo_module.update_net_revenue('
		|| to_char(p_index) || ')' );
Line: 3746

END update_net_revenue;
Line: 3750

PROCEDURE update_net_rev_unit(
	p_index 	IN BINARY_INTEGER,
	p_amount 	IN NUMBER ) IS

BEGIN

    net_rev_unit_t( p_index ) := p_amount;
Line: 3760

        debug( 'EXCEPTION: arp_credit_memo_module.update_net_rev_unit('
		|| to_char(p_index) || ')' );
Line: 3764

END update_net_rev_unit;
Line: 3769

	p_select_rec  		IN OUT NOCOPY select_rec_type,
	p_start_index 		IN BINARY_INTEGER,
	p_end_index 		IN BINARY_INTEGER,
	p_cm_amt_remaining 	IN OUT NOCOPY NUMBER ) IS

    l_amount 			NUMBER;
Line: 3823

    		-- Update rule_start_date and rule_duration
        	-----------------------------------------------------
		p_select_rec.rule_start_date :=
		    get_net_rev_gl_date( p_start_index );
Line: 3827

		p_select_rec.cm_acct_rule_duration := i - p_start_index + 1;
Line: 3832

                          p_select_rec.cm_amount )) THEN

                    -----------------------------------------------------
		    -- cm amount < amt remaining: use total cm amount
                    -----------------------------------------------------
                    l_prorate_total_amount := p_select_rec.cm_amount;
Line: 3864

				    p_select_rec.currency_code );
Line: 3887

	-- Update cm amount remaining
        -------------------------------------------------------------
        p_cm_amt_remaining := p_cm_amt_remaining - l_period_cm_amount;
Line: 3895

	-- Update net revenue amount
        -------------------------------------------------------------
	update_net_revenue( i, get_net_rev_amount(i) + l_period_cm_amount );
Line: 3901

            update_net_rev_unit( i,
                                 get_net_rev_unit(i) +
                                   (l_period_cm_amount /
                                    p_select_rec.invoice_quantity) );
Line: 3909

	-- Insert into cm schedule array
	-- (mode=I, array)
	----------------------------------------------------------
	process_cm_schedule( 'I',
	     		     p_select_rec.customer_trx_line_id,
	     		     get_net_rev_gl_date(i),
	     		     l_period_cm_amount,
	     		     p_select_rec.allow_not_open_flag,
	     		     get_net_rev_dist_exists( i ),
	     		     YES,	-- insert_cma_flag
	     		     YES, 	-- insert_offset_flag
	     		     YES	-- check_gl_date_flag
	   		   );
Line: 3943

	p_select_rec  		IN OUT NOCOPY select_rec_type,
	p_start_index 		IN BINARY_INTEGER,
	p_end_index 		IN BINARY_INTEGER,
	p_cm_amt_remaining 	IN OUT NOCOPY NUMBER ) IS

    l_amount NUMBER;
Line: 3971

          to use true sign(amt) from p_select_rec instead.

          Bug 3198525 - Revised p_select_rec sign variables to be numbers
          to resolve 10g certification issue.  That meant that we no
          longer needed to convert the char signs to numbers. */

        -------------------------------------------------------------
        -- If the net amount remaining in this period is zero, or
        -- the invoice is aleady overapplied in this period
        -- then go to previous period
        -------------------------------------------------------------
	IF( NOT ( get_net_rev_amount(i) = 0 OR
                  SIGN( get_net_rev_amount(i) ) <>
		    SIGN(p_select_rec.invoice_sign) ) ) THEN

	    IF( SIGN( get_net_rev_amount(i) ) =
                SIGN( get_net_rev_amount(i) + p_cm_amt_remaining ) ) THEN

                ------------------------------------------------------------
                -- The array amount + the cm_remaining is the same sign
                -- as the array amount. Therefore, this is a partial
                -- application of this period and no more processing will
                -- be done for this credit memo line.
                -- Set array_amount = array_amount + cm_remaining
                ------------------------------------------------------------
		debug( '  Partial application case', MSG_LEVEL_DEBUG );
Line: 4000

		-- Update net revenue amount
       	        ------------------------------------------------------------
		update_net_revenue( i, get_net_rev_amount(i) +
				       p_cm_amt_remaining );
Line: 4006

		-- Update rule_start_date, rule_duration
		-- Only increment if not (net=0 or overapp)
       	        ------------------------------------------------------------
		p_select_rec.rule_start_date := get_net_rev_gl_date( i );
Line: 4010

		p_select_rec.cm_acct_rule_duration :=
			p_select_rec.cm_acct_rule_duration + 1;
Line: 4015

		-- Insert into cm schedule array
		-- (mode=I, array)
		----------------------------------------------------------
		process_cm_schedule(
			     'I',
	     		     p_select_rec.customer_trx_line_id,
	     		     get_net_rev_gl_date( i ),
	     		     p_cm_amt_remaining,
	     		     p_select_rec.allow_not_open_flag,
	     		     get_net_rev_dist_exists( i ),
	     		     YES,	-- insert_cma_flag
	     		     YES, 	-- insert_offset_flag
	     		     YES	-- check_gl_date_flag
	   		   );
Line: 4047

		-- Update remaining_amount
       	        ------------------------------------------------------------
                p_cm_amt_remaining := p_cm_amt_remaining +
					get_net_rev_amount(i);
Line: 4055

		p_select_rec.cm_acct_rule_duration :=
			p_select_rec.cm_acct_rule_duration + 1;
Line: 4059

		-- Insert into cm schedule array
		-- (mode=I, array)
		----------------------------------------------------------
		process_cm_schedule(
			     'I',
	     		     p_select_rec.customer_trx_line_id,
	     		     get_net_rev_gl_date( i ),
	     		     -get_net_rev_amount( i ),
	     		     p_select_rec.allow_not_open_flag,
	     		     get_net_rev_dist_exists( i ),
	     		     YES,	-- insert_cma_flag
	     		     YES, 	-- insert_offset_flag
	     		     YES	-- check_gl_date_flag
	   		   );
Line: 4075

		-- Update net revenue amount
       	        ------------------------------------------------------------
		update_net_revenue( i, 0 );
Line: 4086

		    p_select_rec.rule_start_date := get_net_rev_gl_date( i );
Line: 4112

	p_select_rec  		IN OUT NOCOPY select_rec_type,
	p_start_index 		IN BINARY_INTEGER,
	p_end_index 		IN BINARY_INTEGER,
	p_cm_amt_remaining 	IN OUT NOCOPY NUMBER ) IS

    l_amount            NUMBER;
Line: 4145

          to use true sign(amt) from p_select_rec instead.

          Bug 3198525 - Original code converted char sign to
          number.  To avoid 10g cert issue, we revised sql
          and structure to store sign as number.*/

        IF( NOT ( get_net_rev_amount(i) = 0 OR
                  SIGN( get_net_rev_amount(i) ) <>
		    SIGN(p_select_rec.invoice_sign) ) ) THEN

	    l_period_cm_amount :=
                arp_util.CurrRound( p_select_rec.cm_quantity *
					get_net_rev_unit(i),
				    p_select_rec.currency_code );
Line: 4193

			p_select_rec.last_period_to_credit -
			TRUNC( p_select_rec.last_period_to_credit );
Line: 4201

					    p_select_rec.currency_code );
Line: 4236

	    -- Update rule_start_date, rule_duration
            -------------------------------------------------------------
	    p_select_rec.rule_start_date := get_net_rev_gl_date( i );
Line: 4239

	    p_select_rec.cm_acct_rule_duration :=
			p_select_rec.cm_acct_rule_duration + 1;
Line: 4243

	    -- Insert into cm schedule array
	    -- (mode=I, array)
	    ----------------------------------------------------------
	    process_cm_schedule( 'I',
				 p_select_rec.customer_trx_line_id,
	     		     	 get_net_rev_gl_date( i ),
	     		     	 l_period_cm_amount,
	     		     	 p_select_rec.allow_not_open_flag,
	     		     	 get_net_rev_dist_exists( i ),
	     		     	 YES,	-- insert_cma_flag
	     		     	 YES, 	-- insert_offset_flag
	     		     	 YES	-- check_gl_date_flag
	   		       );
Line: 4259

	    -- Update net revenue amount
            -------------------------------------------------------------
	    update_net_revenue( i, get_net_rev_amount(i) +
				   l_period_cm_amount );
Line: 4317

        p_select_rec  	IN OUT NOCOPY select_rec_type ) IS

    l_start_index BINARY_INTEGER;
Line: 4332

    l_cm_amt_remaining := p_select_rec.cm_amount;
Line: 4333

    p_select_rec.cm_acct_rule_duration := 0;
Line: 4341

                              p_select_rec.prev_cust_trx_line_id );
Line: 4356

    IF( p_select_rec.inv_acct_rule_duration <> -1 AND
        net_rev_index <> p_select_rec.inv_acct_rule_duration AND
        p_select_rec.cm_amount <> 0 ) THEN

        --
        -- Error: missing_periods
        --
        debug( '  raising missing_periods', MSG_LEVEL_DEBUG );
Line: 4372

    IF p_select_rec.partial_period_flag = 'Y' THEN
       p_select_rec.rule_end_date := get_net_rev_gl_date( net_rev_index - 1);
Line: 4375

       p_select_rec.rule_end_date := null;
Line: 4384

    IF( p_select_rec.credit_method_for_rules = UNIT ) THEN

        l_end_index := CEIL( p_select_rec.last_period_to_credit ) - 1;
Line: 4390

        IF ABS(p_select_rec.inv_unit_price) -
           ABS(p_select_rec.cm_unit_price) < 0
        THEN
           debug( '  raising cm_unit_overapp, ' ||
                p_select_rec.inv_unit_price || ' vs ' ||
                p_select_rec.cm_unit_price, MSG_LEVEL_DEBUG );
Line: 4415

    IF( p_select_rec.invoice_sign = p_select_rec.cm_sign OR
        p_select_rec.cm_sign = 0 OR
        p_select_rec.invoice_sign = 0) THEN

	debug( '  Overapplication case', MSG_LEVEL_DEBUG );
Line: 4424

        IF( p_select_rec.allow_overapp_flag = NO AND
            p_select_rec.cm_sign <> 0 ) THEN

            --
            -- Error: overapplication not allowed
            --
            debug( '  overapp_not_allowed', MSG_LEVEL_DEBUG );
Line: 4445

        IF( p_select_rec.credit_method_for_rules = LIFO ) THEN

            l_overapp_index := l_start_index;
Line: 4449

        ELSIF( p_select_rec.credit_method_for_rules = UNIT ) THEN

            l_overapp_index := l_end_index;
Line: 4453

        ELSIF( p_select_rec.credit_method_for_rules = PRORATE ) THEN

            -----------------------------------------------------------------
            -- Find gl_date in net rev array which matches cm header gl_date
	    --   (search backwards)
            -----------------------------------------------------------------
            IF( find_net_revenue( p_select_rec.prev_cust_trx_line_id,
                                      p_select_rec.cm_gl_date,
				      l_overapp_index ) = FALSE ) THEN

	    	p_select_rec.rule_start_date := p_select_rec.cm_gl_date;
Line: 4464

	    	p_select_rec.cm_acct_rule_duration := 1;
Line: 4471

		     		     p_select_rec.customer_trx_line_id,
		     		     p_select_rec.cm_gl_date,
		     		     p_select_rec.cm_amount,
		     		     p_select_rec.allow_not_open_flag,
		     		     NO,	-- insert_dist_flag
		     		     YES,	-- insert_cma_flag
		     		     NO, 	-- insert_offset_flag
		     		     NO		-- check_gl_date_flag
		   		   );
Line: 4505

	-- Update net rev array: amount
        ---------------------------------------------------------------
        update_net_revenue( l_overapp_index,
                            get_net_rev_amount( l_overapp_index ) +
                              p_select_rec.cm_amount );
Line: 4512

	-- Update rule_start_date = net rev gl_date, acct_rule_duration = 1
        -------------------------------------------------------------------
	p_select_rec.rule_start_date := get_net_rev_gl_date( l_overapp_index );
Line: 4515

	p_select_rec.cm_acct_rule_duration := 1;
Line: 4518

	-- Update cm schedule array:  (mode=T, array)
	----------------------------------------------------------
	process_cm_schedule( 'U',
	     		     p_select_rec.customer_trx_line_id,
	     		     get_net_rev_gl_date( l_overapp_index ),
	     		     p_select_rec.cm_amount,
	     		     p_select_rec.allow_not_open_flag,
	     		     get_net_rev_dist_exists( l_overapp_index ),
	     		     YES,	-- insert_cma_flag
	     		     YES, 	-- insert_offset_flag
	     		     YES	-- check_gl_date_flag
	   		   );
Line: 4543

	IF( p_select_rec.inv_acct_rule_duration = -1 ) THEN

	    ----------------------------------------------------------
	    -- Insert into cm schedule array:  (mode=I, F)
	    ----------------------------------------------------------
	    process_cm_schedule( 'I',
	     		         p_select_rec.customer_trx_line_id,
	     		         p_select_rec.cm_gl_date,
	     		         p_select_rec.cm_amount,
	     		         p_select_rec.allow_not_open_flag,
	     		         YES,       -- insert_dist_flag
	     		         YES,	-- insert_cma_flag
	     		         YES, 	-- insert_offset_flag
	     		         NO		-- check_gl_date_flag
	   		       );
Line: 4564

	    p_select_rec.rule_start_date :=  p_select_rec.cm_gl_date;
Line: 4565

	    p_select_rec.cm_acct_rule_duration := 1;
Line: 4582

	IF( p_select_rec.credit_method_for_rules = PRORATE ) THEN

            process_prorate_cm( p_select_rec,
				l_start_index,
				l_end_index,
				l_cm_amt_remaining );
Line: 4589

	ELSIF( p_select_rec.credit_method_for_rules = LIFO ) THEN

	    process_lifo_cm( 	p_select_rec,
				l_start_index,
				l_end_index,
				l_cm_amt_remaining );
Line: 4596

        ELSIF( p_select_rec.credit_method_for_rules = UNIT ) THEN

	    process_unit_cm( 	p_select_rec,
				l_start_index,
				l_end_index,
				l_cm_amt_remaining );
Line: 4630

	    IF( p_select_rec.credit_method_for_rules = LIFO ) THEN

                l_overapp_index := l_start_index;
Line: 4637

            	p_select_rec.rule_start_date :=
			get_net_rev_gl_date( l_overapp_index );
Line: 4642

                IF( find_net_revenue( p_select_rec.prev_cust_trx_line_id,
                                      p_select_rec.rule_start_date,
				      l_overapp_index ) = FALSE ) THEN

                 -- Bug Fix 624157
                 -- Do not raise an exception, instead populate rule_start_date
                 -- with the credit memo gl_date and  rule_durration = 1
                 /**********************************************************
		    --
		    -- ERROR: No net revenue
		    --
	    	    debug( '  raising no_net_revenue',
			   MSG_LEVEL_DEBUG );
Line: 4661

                p_select_rec.rule_start_date := p_select_rec.cm_gl_date;
Line: 4662

                p_select_rec.cm_acct_rule_duration := 1;
Line: 4678

	    -- Update net rev array
            -------------------------------------------------------------
            update_net_revenue( l_overapp_index,
				get_net_rev_amount( l_overapp_index ) +
                        	  l_cm_amt_remaining );
Line: 4685

	    -- Update cm schedule array:  (mode=U, array)
	    ----------------------------------------------------------
	    process_cm_schedule( 'U',
	     		         p_select_rec.customer_trx_line_id,
	     		         get_net_rev_gl_date( l_overapp_index ),
	     		         l_cm_amt_remaining,
	     		         p_select_rec.allow_not_open_flag,
	     		         get_net_rev_dist_exists( l_overapp_index ),
	     		         YES,	-- insert_cma_flag
	     		         YES, 	-- insert_offset_flag
	     		         YES	-- check_gl_date_flag
	   		       );
Line: 4756

    l_select_rec select_rec_type;
Line: 4757

    l_null_rec   CONSTANT select_rec_type := l_select_rec;
Line: 4772

                      rule_select_cm_lines_c,
                      rule_update_cm_lines_c,
                      rule_insert_dist_c,
                      rule_insert_cma_c );
Line: 4777

    define_select_columns( rule_select_cm_lines_c, l_select_rec );
Line: 4812

        dbms_sql.bind_variable( rule_select_cm_lines_c,
                                'cm_cust_trx_line_id',
                                p_cm_control.customer_trx_line_id );
Line: 4817

          debug( 'EXCEPTION: Error in binding rule_select_cm_lines_c' );
Line: 4824

        dbms_sql.bind_variable( rule_select_cm_lines_c,
                                'cm_customer_trx_id',
                                p_cm_control.customer_trx_id );
Line: 4829

          debug( 'EXCEPTION: Error in binding rule_select_cm_lines_c' );
Line: 4834

        dbms_sql.bind_variable( rule_select_cm_lines_c,
	                        'request_id',
				p_cm_control.request_id );
Line: 4839

	  debug( 'EXCEPTION: Error in binding rule_select_cm_lines_c' );
Line: 4848

      dbms_sql.bind_variable( rule_insert_cma_c,
                              'request_id',
			      p_cm_control.request_id );
Line: 4853

          debug( 'EXCEPTION: Error in binding rule_insert_cma_c' );
Line: 4885

    debug( '  Executing select sql', MSG_LEVEL_DEBUG );
Line: 4888

        l_ignore := dbms_sql.execute( rule_select_cm_lines_c );
Line: 4892

          debug( 'EXCEPTION: Error executing select cm lines sql' );
Line: 4901

    debug( '  Fetching select stmt', MSG_LEVEL_DEBUG );
Line: 4907

            IF dbms_sql.fetch_rows( rule_select_cm_lines_c ) > 0  THEN

		debug('  fetched a row', MSG_LEVEL_DEBUG );
Line: 4916

                l_select_rec := l_null_rec;
Line: 4917

                get_select_column_values( rule_select_cm_lines_c,
                                          l_select_rec );
Line: 4920

                dump_select_rec( l_select_rec );
Line: 4930

		    -- No rows selected
                    --------------------------------------------------------
		    debug( '  raising NO_DATA_FOUND', MSG_LEVEL_DEBUG );
Line: 4943

	        PROCEDURE insert_error_if_autoinv IS
                BEGIN
	            insert_into_error_table(
			l_select_rec.customer_trx_line_id,
                        g_error_buffer,
			NULL );
Line: 4954

                IF g_prev_ctlid <> l_select_rec.prev_cust_trx_line_id
                THEN
                   debug('write arrays to db and clear arrays...');
Line: 4957

                   g_prev_ctlid := l_select_rec.prev_cust_trx_line_id;
Line: 4965

                              l_select_rec );
Line: 4976

		      insert_error_if_autoinv;
Line: 4990

		debug('  Binding variables for update', MSG_LEVEL_DEBUG);
Line: 4992

			l_select_rec.customer_trx_line_id,
			MSG_LEVEL_DEBUG);
Line: 4995

			to_char(l_select_rec.rule_start_date),
			MSG_LEVEL_DEBUG);
Line: 4998

			to_char(l_select_rec.rule_end_date),
			MSG_LEVEL_DEBUG);
Line: 5001

			l_select_rec.cm_acct_rule_duration,
			MSG_LEVEL_DEBUG);
Line: 5004

                dbms_sql.bind_variable( rule_update_cm_lines_c,
                                        'rule_start_date',
                                        l_select_rec.rule_start_date );
Line: 5007

                dbms_sql.bind_variable( rule_update_cm_lines_c,
                                        'rule_end_date',
                                        l_select_rec.rule_end_date );
Line: 5010

                dbms_sql.bind_variable( rule_update_cm_lines_c,
                                        'cm_acct_rule_duration',
                                        l_select_rec.cm_acct_rule_duration );
Line: 5013

                dbms_sql.bind_variable( rule_update_cm_lines_c,
                                        'credit_method',
                                        l_select_rec.credit_method_for_rules);
Line: 5016

                dbms_sql.bind_variable( rule_update_cm_lines_c,
                                        'last_period_to_credit',
                                        l_select_rec.last_period_to_credit );
Line: 5019

                dbms_sql.bind_variable( rule_update_cm_lines_c,
                                        'customer_trx_line_id',
                                        l_select_rec.customer_trx_line_id );
Line: 5024

                  debug('EXCEPTION: Error in binding rule_update_cm_lines_c');
Line: 5034

                l_ignore := dbms_sql.execute( rule_update_cm_lines_c );
Line: 5036

                debug( to_char(l_ignore) || ' row(s) updated',
                       MSG_LEVEL_DEBUG );
Line: 5041

                  debug( 'EXCEPTION: Error executing update lines stmt' );
Line: 5052

		p_rule_start_date := l_select_rec.rule_start_date;
Line: 5054

				l_select_rec.cm_acct_rule_duration;
Line: 5070

            debug( 'EXCEPTION: Error fetching select cm lines' );
Line: 5210

           SELECT ps.payment_schedule_id,
                  int.previous_customer_trx_id,
                  int.interface_line_id,
		  ps.selected_for_receipt_batch_id
           FROM   ra_interface_lines int,
                  ar_payment_schedules ps
           WHERE  int.request_id = p_request_id
           AND    int.previous_customer_trx_id IS NOT NULL
           AND    int.previous_customer_trx_id = ps.customer_trx_id ;
Line: 5223

          PROCEDURE insert_errors(p_selected_for_rcpt_batch_id IN NUMBER) IS
		l_jgzz_product_code VARCHAR2(100);
Line: 5229

		(p_selected_for_rcpt_batch_id = -999) then
                        g_error_buffer := MSG_INV_LOCKED_BY_JL;
Line: 5232

                        insert_into_error_table( l_interface_line_id ,
                                       g_error_buffer,
                                       NULL ) ;
Line: 5238

              		insert_into_error_table( l_interface_line_id ,
                                       g_error_buffer,
                                       NULL ) ;
Line: 5252

                SELECT 'Y'
                INTO   l_locked
                FROM   ar_payment_schedules
                WHERE  payment_schedule_id = i.payment_schedule_id
                --AND    selected_for_receipt_batch_id IS NULL  /* Bug fix 3142217 */
                --Commented The Above Line And Added The Following 3 Lines For Bug Fix 6339084
                /*Bug Fix 6339084 Starts */
                AND (selected_for_receipt_batch_id IS NULL
                 OR (selected_for_receipt_batch_id IS NOT NULL
                 AND NVL(global_attribute20,'~XX~X') = 'COLLECTION'))
              /*Ends*/

                FOR UPDATE OF payment_schedule_id NOWAIT ;
Line: 5271

                    insert_errors(i.selected_for_receipt_batch_id);
Line: 5276

                    insert_errors(i.selected_for_receipt_batch_id);
Line: 5336

                 IF (arp_rounding.insert_round_records(
                         p_request_id,
                         p_customer_trx_id,
                         rows_processed,
                         error_message,
                         0,
                         0,
                         'CM', -- this doesn't matter!
                         arp_global.sysparam.TRX_HEADER_ROUND_CCID) <> 0)
                 THEN
                   debug('arp_rounding.insert_round_rows returned FALSE');
Line: 5352

                   debug('arp_rounding.insert_round_records raised EXCEPTION');
Line: 5373

            SELECT decode( trx.invoicing_rule_id, null, 'N', 'Y')
            INTO   l_rule_flag
            FROM   ra_customer_trx trx
            WHERE  trx.customer_trx_id   = p_customer_trx_id;
Line: 5380

	    debug( 'EXCEPTION: Error in selecting into l_rule_flag' );
Line: 5443

       update the gl_dist_id this late.  MRC calls will have to occur
       inline */

    close_cursors;
Line: 5602

	debug( '  Update mode', MSG_LEVEL_DEBUG );
Line: 5604

	IF( NOT( dbms_sql.is_open( delete_header_dist_c ) AND
		 dbms_sql.is_open( delete_line_dist_c ) AND
		 dbms_sql.is_open( delete_header_cma_c ) AND
		 dbms_sql.is_open( delete_line_cma_c ) AND
		 dbms_sql.is_open( update_header_lines_c ) AND
		 dbms_sql.is_open( update_lines_c ) ) )  THEN

    	    ----------------------------------------------------------------
	    -- Build dynamic sql
    	    ----------------------------------------------------------------
	    build_update_mode_sql(
		delete_header_dist_c,
		delete_line_dist_c,
		delete_header_cma_c,
		delete_line_cma_c,
		update_header_lines_c,
		update_lines_c );
Line: 5629

	    -- Delete distributions, credit_memo_amounts
	    -- and update lines (rule_start_date, accounting_rule_duration)
    	    ----------------------------------------------------------------
	    IF( p_customer_trx_line_id IS NOT NULL ) THEN

	        ---------------------------------------------------------------
	        -- Line-level processing
                ---------------------------------------------------------------
		debug( '  Line-level processing', MSG_LEVEL_DEBUG );
Line: 5643

			delete_line_dist_c,
			'customer_trx_line_id',
			p_customer_trx_line_id );
Line: 5648

			delete_line_cma_c,
			'customer_trx_line_id',
			p_customer_trx_line_id );
Line: 5653

			update_lines_c,
			'customer_trx_line_id',
			p_customer_trx_line_id );
Line: 5660

                dbms_sql.bind_array(delete_line_dist_c, ':gl_dist_key_value',
                                    gl_line_dist_array);
Line: 5667

                debug( '  Executing delete dist sql', MSG_LEVEL_DEBUG );
Line: 5669

                    l_ignore := dbms_sql.execute( delete_line_dist_c );
Line: 5671

                    debug( to_char(l_ignore) || ' row(s) deleted',
		           MSG_LEVEL_DEBUG );
Line: 5678

                    dbms_sql.variable_value( delete_line_dist_c,
                                            ':gl_dist_key_value',
                                            gl_line_dist_array);
Line: 5683

                      debug( 'EXCEPTION: Error executing delete dist sql' );
Line: 5687

                debug( '  Executing delete cma sql', MSG_LEVEL_DEBUG );
Line: 5689

                    l_ignore := dbms_sql.execute( delete_line_cma_c );
Line: 5691

                    debug( to_char(l_ignore) || ' row(s) deleted',
		           MSG_LEVEL_DEBUG );
Line: 5695

                      debug( 'EXCEPTION: Error executing delete cma sql' );
Line: 5699

                debug( '  Executing update lines sql', MSG_LEVEL_DEBUG );
Line: 5701

                    l_ignore := dbms_sql.execute( update_lines_c );
Line: 5703

                    debug( to_char(l_ignore) || ' row(s) updated',
		           MSG_LEVEL_DEBUG );
Line: 5707

                      debug( 'EXCEPTION: Error executing update lines sql' );
Line: 5722

			delete_header_dist_c,
			'customer_trx_id',
			p_customer_trx_id );
Line: 5727

			delete_header_cma_c,
			'customer_trx_id',
			p_customer_trx_id );
Line: 5732

			update_header_lines_c,
			'customer_trx_id',
			p_customer_trx_id );
Line: 5739

                dbms_sql.bind_array(delete_header_dist_c, ':gl_dist_key_value',
                                    gl_header_dist_array);
Line: 5745

                debug( '  Executing delete dist sql', MSG_LEVEL_DEBUG );
Line: 5747

                    l_ignore := dbms_sql.execute( delete_header_dist_c );
Line: 5749

                    debug( to_char(l_ignore) || ' row(s) deleted',
		           MSG_LEVEL_DEBUG );
Line: 5755

                    dbms_sql.variable_value( delete_header_dist_c,
                                            ':gl_dist_key_value',
                                            gl_header_dist_array);
Line: 5760

                      debug( 'EXCEPTION: Error executing delete dist sql' );
Line: 5764

                debug( '  Executing delete cma sql', MSG_LEVEL_DEBUG );
Line: 5766

                    l_ignore := dbms_sql.execute( delete_header_cma_c );
Line: 5768

                    debug( to_char(l_ignore) || ' row(s) deleted',
		           MSG_LEVEL_DEBUG );
Line: 5772

                      debug( 'EXCEPTION: Error executing delete cma sql' );
Line: 5776

                debug( '  Executing update lines sql', MSG_LEVEL_DEBUG );
Line: 5778

                    l_ignore := dbms_sql.execute( update_header_lines_c );
Line: 5780

                    debug( to_char(l_ignore) || ' row(s) updated',
		           MSG_LEVEL_DEBUG );
Line: 5784

                      debug( 'EXCEPTION: Error executing update lines sql' );
Line: 5954

                       nonrule_insert_dist_c,
                       nonrule_update_lines_c,
                       nonrule_update_dist_c,
                       nonrule_update_dist2_c );
Line: 5968

                       nonrule_insert_dist_c,
                       nonrule_update_lines_c,
                       nonrule_update_dist_c,
                       nonrule_update_dist2_c );
Line: 5982

                       nonrule_insert_dist_c,
                       nonrule_update_lines_c,
                       nonrule_update_dist_c,
                       nonrule_update_dist2_c );
Line: 6036

                      rule_select_cm_lines_c,
                      rule_update_cm_lines_c,
                      rule_insert_dist_c,
                      rule_insert_cma_c );
Line: 6050

                      rule_select_cm_lines_c,
                      rule_update_cm_lines_c,
                      rule_insert_dist_c,
                      rule_insert_cma_c );
Line: 6064

                      rule_select_cm_lines_c,
                      rule_update_cm_lines_c,
                      rule_insert_dist_c,
                      rule_insert_cma_c );
Line: 6128

PROCEDURE test_build_update_mode_sql is

  l_cm_control control_rec_type;
Line: 6137

    build_update_mode_sql(
		delete_header_dist_c,
		delete_line_dist_c,
		delete_header_cma_c,
		delete_line_cma_c,
		update_header_lines_c,
		update_lines_c );