DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_AUTO_ACCOUNTING

Source


1 PACKAGE BODY arp_auto_accounting AS
2 /* $Header: ARTEAACB.pls 120.60.12020000.6 2013/04/10 11:59:08 dgaurab ship $ */
3 
4 ------------------------------------------------------------------------
5 -- Inherited from other packages
6 ------------------------------------------------------------------------
7 
8 --
9 -- Linefeed character
10 --
11 PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('AFLOG_ENABLED'), 'N');
12 CRLF            CONSTANT VARCHAR2(1) := arp_global.CRLF;
13 
14 YES			CONSTANT VARCHAR2(1) := arp_global.YES;
15 NO			CONSTANT VARCHAR2(1) := arp_global.NO;
16 
17 MSG_LEVEL_BASIC 	CONSTANT BINARY_INTEGER := arp_global.MSG_LEVEL_BASIC;
18 MSG_LEVEL_TIMING 	CONSTANT BINARY_INTEGER := arp_global.MSG_LEVEL_TIMING;
19 MSG_LEVEL_DEBUG 	CONSTANT BINARY_INTEGER := arp_global.MSG_LEVEL_DEBUG;
20 MSG_LEVEL_DEBUG2 	CONSTANT BINARY_INTEGER := arp_global.MSG_LEVEL_DEBUG2;
21 MSG_LEVEL_DEVELOP 	CONSTANT BINARY_INTEGER := arp_global.MSG_LEVEL_DEVELOP;
22 
23 MAX_CURSOR_CACHE_SIZE   CONSTANT BINARY_INTEGER := 20;
24 MAX_SEGMENT_CACHE_SIZE  CONSTANT BINARY_INTEGER := 1000;
25 MAX_ARRAY_SIZE          CONSTANT BINARY_INTEGER := 1000;
26 STARTING_INDEX          CONSTANT BINARY_INTEGER := 1;
27 
28 /* Bug 2142306 - Limits for hash and linear cache */
29 MAX_LINEAR_CACHE_SIZE   CONSTANT BINARY_INTEGER := 1000;
30 MAX_HASH_CACHE_SIZE     CONSTANT BINARY_INTEGER := 4000;
31 HASH_START              CONSTANT NUMBER := 16384;
32 HASH_MAX                CONSTANT NUMBER := 1000000;
33 
34 G_MAX_DATE              CONSTANT DATE:= arp_global.G_MAX_DATE;
35 G_MIN_DATE              CONSTANT DATE:= to_date('01-01-1952','DD-MM-YYYY');
36 G_SYS_DATE              CONSTANT DATE:= TRUNC(SYSDATE);
37 -- This record holds general information used by autoaccounting and
38 -- credit memo module.  Passed as argument to most functions/procs.
39 --
40 system_info arp_trx_global.system_info_rec_type :=
41 	arp_trx_global.system_info;
42 
43 --
44 -- This record holds profile information used by autoaccounting and
45 -- credit memo module.  Passed as argument to most functions/procs.
46 --
47 profile_info arp_trx_global.profile_rec_type :=
48 	arp_trx_global.profile_info;
49 
50 --
51 -- This record holds accounting flexfield information used by
52 -- autoaccounting and the credit memo module.  Passed as argument to
53 -- most functions/procs.
54 --
55 flex_info arp_trx_global.acct_flex_info_rec_type :=
56 	arp_trx_global.flex_info;
57 
58 ------------------------------------------------------------------------
59 -- Private types
60 ------------------------------------------------------------------------
61 TYPE autoacc_rec_type IS RECORD
62 (
63   type          ra_account_defaults.type%type,
64   segment       ra_account_default_segments.segment%type,
65   table_name    ra_account_default_segments.table_name%type,
66   constant      ra_account_default_segments.constant%type
67 );
68 
69 --
70 -- Autoaccounting definintion cache
71 --
72 REV             CONSTANT VARCHAR2(10) := 'REV';
73 REC             CONSTANT VARCHAR2(10) := 'REC';
74 FREIGHT         CONSTANT VARCHAR2(10) := 'FREIGHT';
75 TAX             CONSTANT VARCHAR2(10) := 'TAX';
76 UNBILL          CONSTANT VARCHAR2(10) := 'UNBILL';
77 UNEARN          CONSTANT VARCHAR2(10) := 'UNEARN';
78 SUSPENSE        CONSTANT VARCHAR2(10) := 'SUSPENSE';
79 CHARGES         CONSTANT VARCHAR2(10) := 'CHARGES';
80 
81 /* Bug 2142306: added variables for linear search if collision occurs
82                 during hash checks
83 */
84 tab_size                 NUMBER     := 0;
85 h_tab_size               NUMBER     := 0;
86 
87 --
88 -- Maximum of 30 enabled segments for the accounting flex
89 -- so the gap between offsets is sufficient
90 --
91 rev_offset      CONSTANT BINARY_INTEGER := 0;
92 rec_offset      CONSTANT BINARY_INTEGER := 50;
93 frt_offset      CONSTANT BINARY_INTEGER := 100;
94 tax_offset      CONSTANT BINARY_INTEGER := 150;
95 unbill_offset   CONSTANT BINARY_INTEGER := 200;
96 unearn_offset   CONSTANT BINARY_INTEGER := 250;
97 suspense_offset CONSTANT BINARY_INTEGER := 300;
98 --
99 rev_count       BINARY_INTEGER := 0;
100 rec_count       BINARY_INTEGER := 0;
101 frt_count       BINARY_INTEGER := 0;
102 tax_count       BINARY_INTEGER := 0;
103 unbill_count    BINARY_INTEGER := 0;
104 unearn_count    BINARY_INTEGER := 0;
105 suspense_count  BINARY_INTEGER := 0;
106 --
107 TYPE segment_table_type IS
108     TABLE OF ra_account_default_segments.segment%type
109     INDEX BY BINARY_INTEGER;
110 --
111 TYPE table_table_type IS
112     TABLE OF ra_account_default_segments.table_name%type
113     INDEX BY BINARY_INTEGER;
114 --
115 TYPE const_table_type IS
116     TABLE OF ra_account_default_segments.constant%type
117     INDEX BY BINARY_INTEGER;
118 --
119 autoacc_def_segment_t segment_table_type;
120 autoacc_def_table_t   table_table_type;
121 autoacc_def_const_t   const_table_type;
122 
123 --
124 -- trx_type cache
125 --
126 TYPE trx_type_rev_table_type IS
127     TABLE OF BINARY_INTEGER
128     INDEX BY BINARY_INTEGER;
129 trx_type_rev_t trx_type_rev_table_type;
130 
131 TYPE trx_type_rec_table_type IS
132     TABLE OF BINARY_INTEGER
133     INDEX BY BINARY_INTEGER;
134 trx_type_rec_t trx_type_rec_table_type;
135 
136 TYPE trx_type_frt_table_type IS
137     TABLE OF BINARY_INTEGER
138     INDEX BY BINARY_INTEGER;
139 trx_type_frt_t trx_type_frt_table_type;
140 
141 TYPE trx_type_tax_table_type IS
142     TABLE OF BINARY_INTEGER
143     INDEX BY BINARY_INTEGER;
144 trx_type_tax_t trx_type_tax_table_type;
145 
146 TYPE trx_type_unbill_table_type IS
147     TABLE OF BINARY_INTEGER
148     INDEX BY BINARY_INTEGER;
149 trx_type_unbill_t trx_type_unbill_table_type;
150 
151 TYPE trx_type_unearn_table_type IS
152     TABLE OF BINARY_INTEGER
153     INDEX BY BINARY_INTEGER;
154 trx_type_unearn_t trx_type_unearn_table_type;
155 
156 TYPE trx_type_suspense_table_type IS
157     TABLE OF BINARY_INTEGER
158     INDEX BY BINARY_INTEGER;
159 trx_type_suspense_t trx_type_suspense_table_type;
160 
161 --
162 -- site_uses cache
163 --
164 TYPE site_use_rev_table_type IS
165     TABLE OF BINARY_INTEGER
166     INDEX BY VARCHAR2(100);
167 site_use_rev_t site_use_rev_table_type;
168 
169 TYPE site_use_rec_table_type IS
170     TABLE OF BINARY_INTEGER
171     INDEX BY  VARCHAR2(100);
172 site_use_rec_t site_use_rec_table_type;
173 
174 TYPE site_use_frt_table_type IS
175     TABLE OF BINARY_INTEGER
176     INDEX BY  VARCHAR2(100);
177 site_use_frt_t site_use_frt_table_type;
178 
179 TYPE site_use_tax_table_type IS
180     TABLE OF BINARY_INTEGER
181     INDEX BY  VARCHAR2(100);
182 site_use_tax_t site_use_tax_table_type;
183 
184 TYPE site_use_unbill_table_type IS
185     TABLE OF BINARY_INTEGER
186     INDEX BY  VARCHAR2(100);
187 site_use_unbill_t site_use_unbill_table_type;
188 
189 TYPE site_use_unearn_table_type IS
190     TABLE OF BINARY_INTEGER
191     INDEX BY  VARCHAR2(100);
192 site_use_unearn_t site_use_unearn_table_type;
193 
194 TYPE site_use_suspense_table_type IS
195     TABLE OF BINARY_INTEGER
196     INDEX BY  VARCHAR2(100);
197 site_use_suspense_t site_use_suspense_table_type;
198 
199 --
200 --
201 -- salesrep cache
202 --
203 TYPE salesrep_rev_table_type IS
204     TABLE OF BINARY_INTEGER
205     INDEX BY BINARY_INTEGER;
206 salesrep_rev_t salesrep_rev_table_type;
207 
208 TYPE salesrep_rec_table_type IS
209     TABLE OF BINARY_INTEGER
210     INDEX BY BINARY_INTEGER;
211 salesrep_rec_t salesrep_rec_table_type;
212 
213 TYPE salesrep_frt_table_type IS
214     TABLE OF BINARY_INTEGER
215     INDEX BY BINARY_INTEGER;
216 salesrep_frt_t salesrep_frt_table_type;
217 
218 --
219 -- inv_item cache
220 --
221 TYPE inv_item_rec_type IS RECORD (
222      inventory_item_id      mtl_system_items.inventory_item_id%TYPE,
223      warehouse_id           mtl_system_items.organization_id%TYPE  ,
224      item_type              mtl_system_items.item_type%TYPE,
225      sales_account          mtl_system_items.sales_account%TYPE
226     );
227 
228 TYPE inv_item_rev_table_type IS
229      TABLE OF inv_item_rec_type
230      INDEX BY VARCHAR2(1000);
231 
232 inv_item_rev_t inv_item_rev_table_type;
233 
234 g_item_ctr BINARY_INTEGER :=0;
235 
236 --
237 -- memo_line cache
238 --
239 TYPE memo_line_rev_table_type IS
240     TABLE OF BINARY_INTEGER
241     INDEX BY BINARY_INTEGER;
242 memo_line_rev_t memo_line_rev_table_type;
243 
244 --
245 -- code combination,segment and date caches
246 --
247 TYPE autoacc_cache_seg_type IS
248      TABLE OF  varchar2(929)
249      INDEX BY  BINARY_INTEGER;
250 
251 TYPE autoacc_cache_id_type IS
252      TABLE OF  BINARY_INTEGER
253      INDEX BY  BINARY_INTEGER;
254 
255 TYPE autoacc_cache_date_type IS
256      TABLE OF  DATE
257      INDEX BY  BINARY_INTEGER;
258 
259 TYPE segment_type IS
260      TABLE OF  gl_code_combinations.segment1%type
261      INDEX BY  BINARY_INTEGER;
262 
263 TYPE cursor_attr_tbl_type IS
264      TABLE OF VARCHAR2(100)
265      INDEX BY BINARY_INTEGER;
266 
267 TYPE cursor_tbl_type IS
268      TABLE OF BINARY_INTEGER
269      INDEX BY BINARY_INTEGER;
270 
271 --
272 -- Misc
273 --
274 
275 -- To store segment values for binding
276 --
277 TYPE seg_table_type IS
278   TABLE OF gl_code_combinations.segment1%type
279   INDEX BY binary_integer;
280 --
281 TYPE ccid_rec_type IS RECORD
282 (
283     trx_type_ccid_rev           BINARY_INTEGER := -1,
284     trx_type_ccid_rec           BINARY_INTEGER := -1,
285     trx_type_ccid_frt           BINARY_INTEGER := -1,
286     trx_type_ccid_tax           BINARY_INTEGER := -1,
287     trx_type_ccid_unbill        BINARY_INTEGER := -1,
288     trx_type_ccid_unearn        BINARY_INTEGER := -1,
289     trx_type_ccid_suspense      BINARY_INTEGER := -1,
290     salesrep_ccid_rev           BINARY_INTEGER := -1,
291     salesrep_ccid_rec           BINARY_INTEGER := -1,
292     salesrep_ccid_frt           BINARY_INTEGER := -1,
293     lineitem_ccid_rev           BINARY_INTEGER := -1,
294     tax_ccid_tax                BINARY_INTEGER := -1,
295     agreecat_ccid_rev           BINARY_INTEGER := -1,
296     interim_tax_ccid            BINARY_INTEGER := -1,
297     site_use_ccid_rev           BINARY_INTEGER := -1,
298     site_use_ccid_rec           BINARY_INTEGER := -1,
299     site_use_ccid_frt           BINARY_INTEGER := -1,
300     site_use_ccid_tax           BINARY_INTEGER := -1,
301     site_use_ccid_unbill        BINARY_INTEGER := -1,
302     site_use_ccid_unearn        BINARY_INTEGER := -1,
303     site_use_ccid_suspense      BINARY_INTEGER := -1
304 );
305 
306 --
307 -- To hold values fetched from the Select stmt
308 --
309 TYPE select_rec_type IS RECORD
310 (
311   customer_trx_id                     NUMBER,
312   customer_trx_line_id                NUMBER,
313   cust_trx_line_salesrep_id           NUMBER,
314   line_amount                         NUMBER,
315   accounted_line_amount               NUMBER,
316   percent                             NUMBER,
317   amount                              NUMBER,
318   acctd_amount                        NUMBER,
319   account_class                       VARCHAR2(20),
320   account_set_flag                    VARCHAR2(1),
321   cust_trx_type_id                    BINARY_INTEGER,
322   allow_not_open_flag                 VARCHAR2(1),
323   concatenated_segments               VARCHAR2(240),
324   code_combination_id                 BINARY_INTEGER,
325   gl_date                             VARCHAR2(12),     -- Julian format
326   original_gl_date                    VARCHAR2(12),     -- Julian format
327   ussgl_trx_code                      VARCHAR2(30),
328   ussgl_trx_code_context              VARCHAR2(30),
329   salesrep_id                         NUMBER,
330   inventory_item_id                   NUMBER,
331   memo_line_id                        NUMBER,
332   default_tax_ccid                    BINARY_INTEGER,
333   interim_tax_ccid                    BINARY_INTEGER,
334   int_concatenated_segments           VARCHAR2(240),
335   int_code_combination_id             BINARY_INTEGER,
336   site_use_id                         NUMBER,
337   warehouse_id                        NUMBER,
338   link_to_cust_trx_line_id            NUMBER  -- 1651593
339 );
340 
341 --
342 -- To hold values fetched from the Select stmt
343 --
344 TYPE select_rec_tab IS RECORD
345 (
346   customer_trx_id                     DBMS_SQL.NUMBER_TABLE,
347   customer_trx_line_id                DBMS_SQL.NUMBER_TABLE,
348   cust_trx_line_salesrep_id           DBMS_SQL.NUMBER_TABLE,
349   line_amount                         DBMS_SQL.NUMBER_TABLE,
350   accounted_line_amount               DBMS_SQL.NUMBER_TABLE,
351   percent                             DBMS_SQL.NUMBER_TABLE,
352   amount                              DBMS_SQL.NUMBER_TABLE,
353   acctd_amount                        DBMS_SQL.NUMBER_TABLE,
354   account_class                       DBMS_SQL.VARCHAR2_TABLE,
355   account_set_flag                    DBMS_SQL.VARCHAR2_TABLE,
356   cust_trx_type_id                    DBMS_SQL.NUMBER_TABLE,
357   allow_not_open_flag                 DBMS_SQL.VARCHAR2_TABLE,
358   concatenated_segments               DBMS_SQL.VARCHAR2_TABLE,
359   code_combination_id                 DBMS_SQL.NUMBER_TABLE,
360   gl_date                             DBMS_SQL.VARCHAR2_TABLE,     -- Julian format
361   original_gl_date                    DBMS_SQL.VARCHAR2_TABLE,     -- Julian format
362   ussgl_trx_code                      DBMS_SQL.VARCHAR2_TABLE,
363   ussgl_trx_code_context              DBMS_SQL.VARCHAR2_TABLE,
364   salesrep_id                         DBMS_SQL.NUMBER_TABLE,
365   inventory_item_id                   DBMS_SQL.NUMBER_TABLE,
366   memo_line_id                        DBMS_SQL.NUMBER_TABLE,
367   default_tax_ccid                    DBMS_SQL.NUMBER_TABLE,
368   interim_tax_ccid                    DBMS_SQL.NUMBER_TABLE,
369   int_concatenated_segments           DBMS_SQL.VARCHAR2_TABLE,
370   int_code_combination_id             DBMS_SQL.NUMBER_TABLE,
371   site_use_id                         DBMS_SQL.NUMBER_TABLE,
372   warehouse_id                        DBMS_SQL.NUMBER_TABLE,
373   link_to_cust_trx_line_id            DBMS_SQL.NUMBER_TABLE -- 1651593
374 );
375 
376 g_select_rec_tab                      select_rec_tab;
377 /* Bug-2178723 : Cached the values of detail_posting_allowed_flag and summary_flag
378                  in pl/sql table to avoid the high execution count  */
379 
380 TYPE code_comb_rec_type IS RECORD
381   ( detail_posting_flag  gl_code_combinations.detail_posting_allowed_flag%TYPE,
382     summary_flag         gl_code_combinations.summary_flag%TYPE);
383 
384 TYPE t_ar_code_comb_table IS TABLE OF code_comb_rec_type
385     INDEX BY BINARY_INTEGER;
386 
387 pg_ar_code_comb_rec t_ar_code_comb_table;
388 
389 -- set invalid segvalue to null
390 --
391 INVALID_SEGMENT CONSTANT VARCHAR2(20) := '';
392 
393 --
394 -- Cursor handles
395 --
396 
397 --
398 -- CCID Validation date
399 --
400 validation_date  DATE := TRUNC(SYSDATE);
401 
402 
403 -- User-defined exceptions
404 --
405 invalid_account_class		EXCEPTION;
406 invalid_table_name              EXCEPTION;     -- in autoacc def
407 item_and_memo_both_not_null     EXCEPTION;
408 error_defaulting_gl_date	EXCEPTION;
409 
410 
411 --
412 -- Translated error messages
413 --
414 MSG_COMPLETE_REV_ACCOUNT 	varchar2(2000);
415 MSG_COMPLETE_REC_ACCOUNT 	varchar2(2000);
416 MSG_COMPLETE_FRT_ACCOUNT 	varchar2(2000);
417 MSG_COMPLETE_TAX_ACCOUNT 	varchar2(2000);
418 MSG_COMPLETE_CHARGES_ACCOUNT 	varchar2(2000);
419 MSG_COMPLETE_OFFSET_ACCOUNT 	varchar2(2000);
420 MSG_COMPLETE_INT_TAX_ACCOUNT	varchar2(2000);
421 
422 MSG_FLEX_POSTING_NOT_ALLOWED	varchar2(2000);
423 MSG_FLEX_NO_PARENT_ALLOWED	varchar2(2000);
424 
425 
426 I               CONSTANT VARCHAR2(1) := 'I';
427 U               CONSTANT VARCHAR2(1) := 'U';
428 D               CONSTANT VARCHAR2(1) := 'D';
429 G               CONSTANT VARCHAR2(1) := 'G';
430 
431 -- code combination segment, ID, Start and End Date caches
432 -- bug 2142306: revised cache and linear tables for autoaccounting
433 
434 autoacc_hash_id_cache           autoacc_cache_id_type;
435 autoacc_hash_seg_cache          autoacc_cache_seg_type;
436 autoacc_hash_st_date_cache      autoacc_cache_date_type;
437 autoacc_hash_end_date_cache     autoacc_cache_date_type;
438 
439 autoacc_lin_id_cache            autoacc_cache_id_type;
440 autoacc_lin_seg_cache           autoacc_cache_seg_type;
441 autoacc_lin_st_date_cache       autoacc_cache_date_type;
442 autoacc_lin_end_date_cache      autoacc_cache_date_type;
443 
444 cursor_attr_cache               cursor_attr_tbl_type;
445 cursor_cache                    cursor_tbl_type;
446 
447 segment1_cache    segment_type;
448 segment2_cache    segment_type;
449 segment3_cache    segment_type;
450 segment4_cache    segment_type;
451 segment5_cache    segment_type;
452 segment6_cache    segment_type;
453 segment7_cache    segment_type;
454 segment8_cache    segment_type;
455 segment9_cache    segment_type;
456 segment10_cache   segment_type;
457 segment11_cache   segment_type;
458 segment12_cache   segment_type;
459 segment13_cache   segment_type;
460 segment14_cache   segment_type;
461 segment15_cache   segment_type;
462 segment16_cache   segment_type;
463 segment17_cache   segment_type;
464 segment18_cache   segment_type;
465 segment19_cache   segment_type;
466 segment20_cache   segment_type;
467 segment21_cache   segment_type;
468 segment22_cache   segment_type;
469 segment23_cache   segment_type;
470 segment24_cache   segment_type;
471 segment25_cache   segment_type;
472 segment26_cache   segment_type;
473 segment27_cache   segment_type;
474 segment28_cache   segment_type;
475 segment29_cache   segment_type;
476 segment30_cache   segment_type;
477 
478 /* Bug 2560036 - Collectibility results table and flags */
479 /* Table for recording collectibility results */
480 t_collect                ar_revenue_management_pvt.long_number_table;
481 /* flag that indicates if collectibility is enabled */
482 g_test_collectibility    boolean;
483 /* flag that indicates if collectibility has already
484    been called in current session */
485 g_called_collectibility  boolean := FALSE;
486 
487 ----------------------------------------------------------------------------
488 -- Covers
489 ----------------------------------------------------------------------------
490 PROCEDURE debug( p_line IN VARCHAR2 ) IS
491 BEGIN
492     IF PG_DEBUG IN ('C','Y')
493     THEN
494        arp_util.debug( p_line );
495     END IF;
496 END;
497 
498 PROCEDURE debug( p_str VARCHAR2, p_print_level BINARY_INTEGER ) IS
499 BEGIN
500     IF PG_DEBUG IN ('C','Y')
501     THEN
502        arp_util.debug( p_str, p_print_level );
503     END IF;
504 END;
505 
506 PROCEDURE enable_debug IS
507 BEGIN
508   arp_util.enable_debug;
509 END;
510 
511 PROCEDURE disable_debug IS
512 BEGIN
513   arp_util.disable_debug;
514 END;
515 
516 PROCEDURE print_fcn_label( p_label VARCHAR2 ) IS
517 BEGIN
518    IF PG_DEBUG IN ('C', 'Y')
519    THEN
520       arp_util.print_fcn_label( p_label );
521    END IF;
522 END;
523 
524 PROCEDURE print_fcn_label2( p_label VARCHAR2 ) IS
525 BEGIN
526    IF PG_DEBUG IN ('C', 'Y')
527    THEN
528      arp_util.print_fcn_label2( p_label );
529    END IF;
530 END;
531 
532 PROCEDURE close_cursor( p_cursor_handle IN OUT NOCOPY INTEGER ) IS
533 BEGIN
534     arp_util.close_cursor( p_cursor_handle );
535 END;
536 
537 
538 
539 ----------------------------------------------------------------------------
540 -- Procedures and Functions
541 ----------------------------------------------------------------------------
542 
543 
544 PROCEDURE insert_into_error_table(
545 	p_interface_line_id number,
546 	p_message_text varchar2,
547 	p_invalid_value varchar2 )  IS
548 
549 BEGIN
550 
551     INSERT INTO ra_interface_errors
552     (interface_line_id,
553      message_text,
554      invalid_value,
555      org_id)
556     VALUES
557     (p_interface_line_id,
558      p_message_text,
559      p_invalid_value,
560      ARP_STANDARD.sysparm.org_id);
561 
562 END insert_into_error_table;
563 
564 PROCEDURE put_message_on_stack(
565 	p_interface_line_id number,
566 	p_message_text varchar2,
567 	p_invalid_value varchar2,
568         p_request_id    binary_integer )  IS
569 
570 BEGIN
571         IF ( p_request_id IS NOT NULL ) THEN
572         IF ( p_request_id > 0)
573         THEN
574            IF p_interface_line_id < 0 THEN
575 
576 	      -- Since, we cannot derive the receivables account for a invoice,
577 	      -- we insert error for each invoice line.
578 
579 	      FOR c01_rec IN (select interface_line_id from ra_interface_lines_gt
580 			      WHERE customer_trx_id = -1 * p_interface_line_id
581 			      AND   request_id      = p_request_id ) LOOP
582                  insert_into_error_table(
583                                  c01_rec.interface_line_id,
584                                  p_message_text,
585                                  p_invalid_value );
586 
587               END LOOP;
588 	   ELSE
589               insert_into_error_table(
590                               p_interface_line_id,
591                               p_message_text,
592                               p_invalid_value );
593            END IF;
594 	-- the following code has been added by bsarkar to log the
595 	-- error for Invoice API. Request_id will be always -ve
596    	-- for invoice api and instead of logging into the standard
597 	-- error table it will log into global error table for
598 	-- invoice api.
599 
600         ELSIF (p_request_id < 0 )
601 	THEN
602 	     -- for Invoice API request id will be always -ve
603 	     -- get the details for which line autoaccounting failed.
604 	    FOR invRec IN ( select trx_header_id,trx_line_id
605 		            from ar_trx_lines_gt
606 			    where request_id = p_request_id
607 			    and   customer_trx_line_id = p_interface_line_id
608 			    UNION
609 			    select trx_header_id, -99
610 			    from ar_trx_header_gt
611 			    where request_id = p_request_id
612 			    and   customer_trx_id = -1 * p_interface_line_id )		   loop
613             	insert into ar_trx_errors_gt
614                     	(trx_header_id,
615 			 trx_line_id,
616 			 error_message,
617                      	 invalid_value) values
618                         ( invRec.trx_header_id,
619 			  decode(invRec.trx_line_id,-99,null,invRec.trx_line_id),
620 			  p_message_text,
621                           p_invalid_value);
622 	   end loop;
623         END IF;
624         ELSE
625 
626             FND_MESSAGE.set_name( 'AR', 'GENERIC_MESSAGE' );
627             FND_MESSAGE.set_token( 'GENERIC_TEXT', p_message_text );
628 
629         END IF;
630 END put_message_on_stack;
631 
632 
633 ----------------------------------------------------------------------------
634 PROCEDURE get_error_message_text is
635 
636     l_application_id  NUMBER := 222;
637     l_msg_name	   VARCHAR2(100);
638 
639 BEGIN
640 
641     print_fcn_label( 'arp_auto_accounting.get_error_message_text()+' );
642 
643     l_msg_name := '1360';
644     fnd_message.set_name('AR', l_msg_name);
645     MSG_COMPLETE_REV_ACCOUNT := fnd_message.get;
646 
647     ----
648     l_msg_name := '1350';
649     fnd_message.set_name('AR', l_msg_name);
650     MSG_COMPLETE_REC_ACCOUNT := fnd_message.get;
651 
652     ----
653     l_msg_name := '1370';
654     fnd_message.set_name('AR', l_msg_name);
655     MSG_COMPLETE_FRT_ACCOUNT := fnd_message.get;
656 
657     ----
658     l_msg_name := 'I-120';
659     fnd_message.set_name('AR', l_msg_name);
660     MSG_COMPLETE_TAX_ACCOUNT := fnd_message.get;
661 
662     ----
663     l_msg_name := '1365';
664     fnd_message.set_name('AR', l_msg_name);
665     MSG_COMPLETE_CHARGES_ACCOUNT := fnd_message.get;
666 
667     ----
668     l_msg_name := 'AR_AUTOACC_COMPLETE_OFFSET';
669     fnd_message.set_name('AR', l_msg_name);
670     MSG_COMPLETE_OFFSET_ACCOUNT := fnd_message.get;
671 
672     ----
673     l_msg_name := 'RA_POSTING_NOT_ALLOWED';
674     fnd_message.set_name('AR', l_msg_name);
675     MSG_FLEX_POSTING_NOT_ALLOWED := fnd_message.get;
676 
677     ----
678     l_msg_name := 'FLEX-No Parent';
679     fnd_message.set_name('AR', l_msg_name);
680     MSG_FLEX_NO_PARENT_ALLOWED := fnd_message.get;
681 
682     ----
683     l_msg_name := 'AR_COMPLETE_INT_TAX_ACCOUNT';
684     fnd_message.set_name('AR', l_msg_name);
685     MSG_COMPLETE_INT_TAX_ACCOUNT := fnd_message.get;
686 
687     -- print
688     debug( '  MSG_COMPLETE_REV_ACCOUNT='||MSG_COMPLETE_REV_ACCOUNT,
689 	MSG_LEVEL_DEBUG );
690     debug( '  MSG_COMPLETE_REC_ACCOUNT='||MSG_COMPLETE_REC_ACCOUNT,
691 	MSG_LEVEL_DEBUG );
692     debug( '  MSG_COMPLETE_FRT_ACCOUNT='||MSG_COMPLETE_FRT_ACCOUNT,
693 	MSG_LEVEL_DEBUG );
694     debug( '  MSG_COMPLETE_TAX_ACCOUNT='||MSG_COMPLETE_TAX_ACCOUNT,
695 	MSG_LEVEL_DEBUG );
696     debug( '  MSG_COMPLETE_INT_TAX_ACCOUNT='||MSG_COMPLETE_INT_TAX_ACCOUNT,
697 	MSG_LEVEL_DEBUG );
698     debug( '  MSG_COMPLETE_OFFSET_ACCOUNT='||MSG_COMPLETE_OFFSET_ACCOUNT,
699 	MSG_LEVEL_DEBUG );
700 
701     debug( '  MSG_FLEX_POSTING_NOT_ALLOWED='||MSG_FLEX_POSTING_NOT_ALLOWED,
702 	MSG_LEVEL_DEBUG );
703     debug( '  MSG_FLEX_NO_PARENT_ALLOWED='||MSG_FLEX_NO_PARENT_ALLOWED,
704 	MSG_LEVEL_DEBUG );
705 
706     print_fcn_label( 'arp_auto_accounting.get_error_message_text()-' );
707 
708 EXCEPTION
709     WHEN OTHERS THEN
710         debug('EXCEPTION: arp_auto_accounting.get_error_message_text()');
711         RAISE;
712 END get_error_message_text;
713 
714 ----------------------------------------------------------------------------
715 PROCEDURE expand_account_class( p_account_class IN OUT NOCOPY VARCHAR2 ) IS
716 BEGIN
717 
718     --
719     -- Adjust account_class to proper string
720     --
721     IF( substrb(p_account_class, 1, 3) = 'REV' ) THEN
722         p_account_class := REV;
723     ELSIF( substrb(p_account_class, 1, 3) = 'REC' ) THEN
724         p_account_class := REC;
725     ELSIF( substrb(p_account_class, 1, 3) = 'FRE' ) THEN
726         p_account_class := FREIGHT;
727     ELSIF( substrb(p_account_class, 1, 3) = 'TAX' ) THEN
728         p_account_class := TAX;
729     ELSIF( substrb(p_account_class, 1, 3) = 'UNB' ) THEN
730         p_account_class := UNBILL;
731     ELSIF( substrb(p_account_class, 1, 3) = 'UNE' ) THEN
732         p_account_class := UNEARN;
733     ELSIF( substrb(p_account_class, 1, 3) = 'SUS' ) THEN
734         p_account_class := SUSPENSE;
735     ELSIF( substrb(p_account_class, 1, 3) = 'CHA' ) THEN
736         p_account_class := CHARGES ;
737     END IF;
738 
739 END expand_account_class;
740 
741 ----------------------------------------------------------------------------
742 PROCEDURE dump_info IS
743 BEGIN
744 
745     -- sys info
746     debug( '  coa_id='||to_char(system_info.chart_of_accounts_id),
747 	    MSG_LEVEL_DEBUG);
748     debug( '  curr='||system_info.base_currency, MSG_LEVEL_DEBUG);
749     debug( '  prec='||to_char(system_info.base_precision), MSG_LEVEL_DEBUG);
750     debug( '  mau='||to_char(system_info.base_min_acc_unit), MSG_LEVEL_DEBUG);
751 
752     IF( system_info.rev_based_on_salesrep ) THEN
753         debug( '  rev_based_on_salesrep=TRUE', MSG_LEVEL_DEBUG );
754     ELSE
755         debug( '  rev_based_on_salesrep=FALSE', MSG_LEVEL_DEBUG );
756     END IF;
757 
758     IF( system_info.tax_based_on_salesrep ) THEN
759         debug( '  tax_based_on_salesrep=TRUE', MSG_LEVEL_DEBUG );
760     ELSE
761         debug( '  tax_based_on_salesrep=FALSE', MSG_LEVEL_DEBUG );
762     END IF;
763 
764     IF( system_info.unbill_based_on_salesrep ) THEN
765         debug( '  unbill_based_on_salesrep=TRUE', MSG_LEVEL_DEBUG );
766     ELSE
767         debug( '  unbill_based_on_salesrep=FALSE', MSG_LEVEL_DEBUG );
768     END IF;
769 
770     IF( system_info.unearn_based_on_salesrep ) THEN
771         debug( '  unearn_based_on_salesrep=TRUE', MSG_LEVEL_DEBUG );
772     ELSE
773         debug( '  unearn_based_on_salesrep=FALSE', MSG_LEVEL_DEBUG );
774     END IF;
775 
776     IF( system_info.suspense_based_on_salesrep ) THEN
777         debug( '  suspense_based_on_salesrep=TRUE', MSG_LEVEL_DEBUG );
778     ELSE
779         debug( '  suspense_based_on_salesrep=FALSE', MSG_LEVEL_DEBUG );
780     END IF;
781 
782 
783     -- profile info
784     debug( '  login_id='||profile_info.conc_login_id, MSG_LEVEL_DEBUG );
785     debug( '  program_id='||profile_info.conc_program_id, MSG_LEVEL_DEBUG );
786     debug( '  user_id='||profile_info.user_id, MSG_LEVEL_DEBUG );
787     debug( '  use_inv_acct='||profile_info.use_inv_acct_for_cm_flag,
788            MSG_LEVEL_DEBUG );
789 
790     /*Bug 8640674*/
791     IF(arp_global.sysparam.org_id IS NOT NULL) THEN
792     debug( '  org_id='||oe_profile.value('SO_ORGANIZATION_ID',arp_global.sysparam.org_id),
793                MSG_LEVEL_DEBUG );
794     END IF;
795 
796     -- flex info
797     debug( '  nsegs='||to_char(flex_info.number_segments), MSG_LEVEL_DEBUG);
798     debug( '  delim='||flex_info.delim, MSG_LEVEL_DEBUG);
799 
800 EXCEPTION
801     WHEN OTHERS THEN
802         debug('EXCEPTION: arp_auto_accounting.dump_info()', MSG_LEVEL_BASIC);
803         debug(SQLERRM, MSG_LEVEL_BASIC);
804         RAISE;
805 END dump_info;
806 
807 ----------------------------------------------------------------------------
808 --
809 -- PROCEDURE NAME:  dump_ccid_record
810 --
811 -- DECSRIPTION:
812 --   Prints contents of the ccid record
813 --
814 -- ARGUMENTS:
815 --      IN:
816 --        ccid_record
817 --
818 --      IN/OUT:
819 --
820 --      OUT:
821 --
822 -- NOTES:
823 --
824 -- HISTORY:
825 --
826 PROCEDURE dump_ccid_record( p_ccid_record IN ccid_rec_type ) IS
827 BEGIN
828     print_fcn_label( 'arp_auto_accounting.dump_ccid_record()+' );
829 
830     debug( '  Dumping CCID record:', MSG_LEVEL_DEBUG );
831 
832     debug( '  trx_type_ccid_rev=' ||
833            to_char(p_ccid_record.trx_type_ccid_rev ), MSG_LEVEL_DEBUG );
834     debug( '  trx_type_ccid_rec=' ||
835            to_char(p_ccid_record.trx_type_ccid_rec ), MSG_LEVEL_DEBUG );
836     debug( '  trx_type_ccid_frt=' ||
837            to_char(p_ccid_record.trx_type_ccid_frt ), MSG_LEVEL_DEBUG );
838     debug( '  trx_type_ccid_tax=' ||
839            to_char(p_ccid_record.trx_type_ccid_tax ), MSG_LEVEL_DEBUG );
840     debug( '  trx_type_ccid_unbill=' ||
841            to_char(p_ccid_record.trx_type_ccid_unbill ), MSG_LEVEL_DEBUG );
842     debug( '  trx_type_ccid_unearn=' ||
843            to_char(p_ccid_record.trx_type_ccid_unearn ), MSG_LEVEL_DEBUG );
844     debug( '  trx_type_ccid_suspense=' ||
845            to_char(p_ccid_record.trx_type_ccid_suspense ), MSG_LEVEL_DEBUG );
846     debug( '  site_use_ccid_rev=' ||
847            to_char(p_ccid_record.site_use_ccid_rev ), MSG_LEVEL_DEBUG );
848     debug( '  site_use_ccid_rec=' ||
849            to_char(p_ccid_record.site_use_ccid_rec ), MSG_LEVEL_DEBUG );
850     debug( '  site_use_ccid_frt=' ||
851            to_char(p_ccid_record.site_use_ccid_frt ), MSG_LEVEL_DEBUG );
852     debug( '  site_use_ccid_tax=' ||
853            to_char(p_ccid_record.site_use_ccid_tax ), MSG_LEVEL_DEBUG );
854     debug( '  site_use_ccid_unbill=' ||
855            to_char(p_ccid_record.site_use_ccid_unbill ), MSG_LEVEL_DEBUG );
856     debug( '  site_use_ccid_unearn=' ||
857            to_char(p_ccid_record.site_use_ccid_unearn ), MSG_LEVEL_DEBUG );
858     debug( '  site_use_ccid_suspense=' ||
859            to_char(p_ccid_record.site_use_ccid_suspense ), MSG_LEVEL_DEBUG );
860     debug( '  salesrep_ccid_rev=' ||
861            to_char(p_ccid_record.salesrep_ccid_rev ), MSG_LEVEL_DEBUG );
862     debug( '  salesrep_ccid_rec=' ||
863            to_char(p_ccid_record.salesrep_ccid_rec ), MSG_LEVEL_DEBUG );
864     debug( '  salesrep_ccid_frt=' ||
865            to_char(p_ccid_record.salesrep_ccid_frt ), MSG_LEVEL_DEBUG );
866     debug( '  lineitem_ccid_rev=' ||
867            to_char(p_ccid_record.lineitem_ccid_rev ), MSG_LEVEL_DEBUG );
868     debug( '  tax_ccid_tax=' ||
869            to_char(p_ccid_record.tax_ccid_tax ), MSG_LEVEL_DEBUG );
870     debug( '  agreecat_ccid_rev=' ||
871            to_char(p_ccid_record.agreecat_ccid_rev ), MSG_LEVEL_DEBUG );
872     debug( '  interim_tax_ccid=' ||
873            to_char(p_ccid_record.interim_tax_ccid ), MSG_LEVEL_DEBUG );
874 
875     print_fcn_label( 'arp_auto_accounting.dump_ccid_record()-' );
876 
877 END dump_ccid_record;
878 
879 ----------------------------------------------------------------------------
880 --
881 -- PROCEDURE NAME:  load_autoacc_def
882 --
883 -- DECSRIPTION:
884 --   Retrieves the following autoaccounting information for each
885 --   account class:
886 --     - segment column name
887 --     - table name
888 --     - constant
889 --   and stores them in plsql tables for future use by autoaccounting.
890 --   Called on package initialization.
891 --
892 -- ARGUMENTS:
893 --      IN:
894 --
895 --      IN/OUT:
896 --
897 --      OUT:
898 --
899 -- NOTES:
900 --
901 -- HISTORY:
902 --
903 PROCEDURE load_autoacc_def IS
904     l_rev_index         BINARY_INTEGER := rev_offset;
905     l_rec_index         BINARY_INTEGER := rec_offset;
906     l_frt_index         BINARY_INTEGER := frt_offset;
907     l_tax_index         BINARY_INTEGER := tax_offset;
908     l_unbill_index      BINARY_INTEGER := unbill_offset;
909     l_unearn_index      BINARY_INTEGER := unearn_offset;
910     l_suspense_index    BINARY_INTEGER := suspense_offset;
911     --begin anuj
912 /* Multi-Org Access Control Changes for SSA;Begin;anukumar;11/01/2002*/
913     i_cnt BINARY_INTEGER :=0;
914 /* Multi-Org Access Control Changes for SSA;Begin;anukumar;11/01/2002*/
915    --end anuj
916 
917     CURSOR autoacc IS
918     SELECT
919       ad.type type,
920       ads.segment segment,
921       upper(ads.table_name) table_name,
922       ads.constant constant
923     FROM
924       ra_account_default_segments ads,
925       ra_account_defaults ad
926     WHERE ad.gl_default_id = ads.gl_default_id
927     AND   ad.org_id = NVL(arp_global.sysparam.org_id, ad.org_id)
928     AND   ad.type in
929     (
930      'REV', 'REC', 'FREIGHT', 'TAX', 'UNBILL', 'UNEARN', 'SUSPENSE'
931     )
932     ORDER BY
933       type,
934       segment_num;
935 
936 
937     PROCEDURE load( p_table_index 	IN OUT NOCOPY BINARY_INTEGER,
938                     p_cnt 		IN OUT NOCOPY BINARY_INTEGER,
939                     p_autoacc_rec 	IN autoacc%rowtype) IS
940     BEGIN
941         autoacc_def_segment_t(p_table_index) := p_autoacc_rec.segment;
942         autoacc_def_table_t(p_table_index) := p_autoacc_rec.table_name;
943         autoacc_def_const_t(p_table_index):= p_autoacc_rec.constant;
944         p_table_index := p_table_index + 1;
945         p_cnt := p_cnt + 1;
946     END;
947 
948 BEGIN
949     print_fcn_label( 'arp_auto_accounting.load_autoacc_def()+' );
950 --begin anuj
951 /* Multi-Org Access Control Changes for SSA;Begin;anukumar;11/01/2002*/
952     rev_count      := 0;
953     rec_count      := 0;
954     frt_count      := 0;
955     tax_count      := 0;
956     unbill_count   := 0;
957     unearn_count   := 0;
958     suspense_count := 0;
959 
960     i_cnt := 0;
961     while i_cnt <=300 LOOP
962 
963     If (autoacc_def_segment_t.exists(i_cnt)) then
964         autoacc_def_segment_t.delete;
965     End if;
966     If (autoacc_def_table_t.exists(i_cnt) ) then
967       autoacc_def_table_t.delete;
968     End if;
969     If (autoacc_def_const_t.exists(i_cnt) ) then
970         autoacc_def_const_t.delete;
971     End if;
972     i_cnt := 50+i_cnt;
973     End Loop;
974 /* Multi-Org Access Control Changes for SSA;Begin;anukumar;11/01/2002*/
975 --end anuj
976 
977     FOR autoacc_rec IN autoacc LOOP
978         IF( autoacc_rec.type in (REV, CHARGES) ) then
979           load(l_rev_index, rev_count, autoacc_rec);
980         ELSIF( autoacc_rec.type = REC ) then
981           load(l_rec_index, rec_count, autoacc_rec);
982         ELSIF( autoacc_rec.type = FREIGHT ) then
983           load(l_frt_index, frt_count, autoacc_rec);
984         ELSIF( autoacc_rec.type = TAX ) then
985           load(l_tax_index, tax_count, autoacc_rec);
986         ELSIF( autoacc_rec.type = UNBILL ) then
987           load(l_unbill_index, unbill_count, autoacc_rec);
988         ELSIF( autoacc_rec.type = UNEARN ) then
989           load(l_unearn_index, unearn_count, autoacc_rec);
990         ELSIF( autoacc_rec.type = SUSPENSE ) then
991           load(l_suspense_index, suspense_count, autoacc_rec);
992         END IF;
993     END LOOP;
994 
995     print_fcn_label( 'arp_auto_accounting.load_autoacc_def()-' );
996 
997 EXCEPTION
998     WHEN OTHERS THEN
999         debug('EXCEPTION: arp_auto_accounting.load_autoacc_def()',
1000 	      MSG_LEVEL_BASIC);
1001         debug(SQLERRM, MSG_LEVEL_BASIC);
1002         RAISE;
1003 END load_autoacc_def ;
1004 
1005 ----------------------------------------------------------------------------
1006 --
1007 -- FUNCTION NAME:  query_autoacc_def
1008 --
1009 -- DECSRIPTION:
1010 --   Determines whether any of a given account class segments are based
1011 --   on a given table.
1012 --
1013 -- ARGUMENTS:
1014 --      IN:
1015 --        account_class:
1016 --          'REV', 'REC', 'FRE', 'TAX', 'UNB', 'UNE', 'SUS'
1017 --          'CHA'
1018 --        table_name
1019 --
1020 --      IN/OUT:
1021 --
1022 --      OUT:
1023 --
1024 -- RETURNS:
1025 --   TRUE if account class is based on specified table
1026 --   FALSE otherwise
1027 --
1028 -- NOTES:
1029 --   if account_class = 'ALL', check all seven account classes in cache
1030 --   else check particular account class in cache
1031 --
1032 --
1033 -- HISTORY:
1034 --
1035 FUNCTION query_autoacc_def( p_account_class 	IN VARCHAR2,
1036                             p_table_name 	IN VARCHAR2 )
1037     RETURN BOOLEAN IS
1038 
1039     retval BOOLEAN;
1040     l_account_class             VARCHAR2(20);
1041 
1042     FUNCTION search_table( p_offset 	IN BINARY_INTEGER,
1043                            p_cnt 	IN BINARY_INTEGER )
1044         RETURN BOOLEAN is
1045     BEGIN
1046         FOR i IN 0..p_cnt-1 LOOP
1047             IF( autoacc_def_table_t( p_offset + i ) = p_table_name ) THEN
1048                 return TRUE;
1049             END IF;
1050         END LOOP;
1051 
1052         RETURN false;
1053     END;
1054 
1055 BEGIN
1056     print_fcn_label( 'arp_auto_accounting.query_autoacc_def()+' );
1057 
1058     g_error_buffer := NULL;
1059 
1060     --
1061     -- Adjust account_class to proper string
1062     --
1063     l_account_class := p_account_class;
1064     expand_account_class( l_account_class );
1065 
1066     IF l_account_class = 'ALL' THEN
1067         retval := query_autoacc_def(REV, p_table_name) OR
1068                 query_autoacc_def(REC, p_table_name) OR
1069                 query_autoacc_def(FREIGHT, p_table_name) OR
1070                 query_autoacc_def(TAX, p_table_name) OR
1071                 query_autoacc_def(UNBILL, p_table_name) OR
1072                 query_autoacc_def(UNEARN, p_table_name) OR
1073                 query_autoacc_def(SUSPENSE, p_table_name);
1074     ELSE
1075         IF l_account_class in (REV, CHARGES) THEN
1076             retval := search_table( rev_offset, rev_count);
1077         ELSIF l_account_class = REC THEN
1078             retval := search_table( rec_offset, rec_count);
1079         ELSIF l_account_class = FREIGHT THEN
1080             retval := search_table( frt_offset, frt_count);
1081         ELSIF l_account_class = TAX THEN
1082             retval := search_table( tax_offset, tax_count);
1083         ELSIF l_account_class = UNBILL THEN
1084             retval := search_table( unbill_offset, unbill_count);
1085         ELSIF l_account_class = UNEARN THEN
1086             retval := search_table( unearn_offset, unearn_count);
1087         ELSIF l_account_class = SUSPENSE THEN
1088             retval := search_table( suspense_offset, suspense_count);
1089         ELSE
1090 	    g_error_buffer := 'Invalid account class';
1091 	    debug( 'EXCEPTION: '||g_error_buffer, MSG_LEVEL_BASIC );
1092             RAISE invalid_account_class;
1093         END IF;
1094     END IF;
1095 
1096     print_fcn_label( 'arp_auto_accounting.query_autoacc_def()-' );
1097 
1098     RETURN retval;
1099 
1100 
1101 EXCEPTION
1102     WHEN OTHERS THEN
1103         debug('EXCEPTION: arp_auto_accounting.query_autoacc_def('
1104               || p_account_class || ', '
1105               || p_table_name ||')', MSG_LEVEL_BASIC);
1106         debug(SQLERRM, MSG_LEVEL_BASIC);
1107         RAISE;
1108 END query_autoacc_def;
1109 
1110 
1111 ----------------------------------------------------------------------------
1112 --
1113 -- PROCEDURE NAME:  get_trx_type_ccids
1114 --
1115 -- DECSRIPTION:
1116 --   Retrieves default ccids from the table ra_cust_trx_types
1117 --   for a given trx type.
1118 --
1119 -- ARGUMENTS:
1120 --      IN:
1121 --        trx_type_id
1122 --
1123 --      IN/OUT:
1124 --        ccid_rev
1125 --        ccid_rec
1126 --        ccid_frt
1127 --        ccid_tax
1128 --        ccid_unbill
1129 --        ccid_unearn
1130 --        ccid_suspense
1131 --
1132 --      OUT:
1133 --
1134 -- NOTES:
1135 --
1136 -- HISTORY:
1137 --
1138 PROCEDURE get_trx_type_ccids( p_trx_type_id 	IN BINARY_INTEGER,
1139                                p_ccid_rev 	IN OUT NOCOPY BINARY_INTEGER,
1140                                p_ccid_rec 	IN OUT NOCOPY BINARY_INTEGER,
1141                                p_ccid_frt 	IN OUT NOCOPY BINARY_INTEGER,
1142                                p_ccid_tax 	IN OUT NOCOPY BINARY_INTEGER,
1143                                p_ccid_unbill 	IN OUT NOCOPY BINARY_INTEGER,
1144                                p_ccid_unearn 	IN OUT NOCOPY BINARY_INTEGER,
1145                                p_ccid_suspense 	IN OUT NOCOPY BINARY_INTEGER)  IS
1146 BEGIN
1147     print_fcn_label2( 'arp_auto_accounting.get_trx_type_ccids()+' );
1148 
1149     --
1150     -- initialize
1151     --
1152     p_ccid_rev := -1;
1153     p_ccid_rec := -1;
1154     p_ccid_frt := -1;
1155     p_ccid_tax := -1;
1156     p_ccid_unbill := -1;
1157     p_ccid_unearn := -1;
1158     p_ccid_suspense := -1;
1159 
1160 
1161     BEGIN
1162         -- see if available in cache
1163         --
1164         p_ccid_rev := trx_type_rev_t( p_trx_type_id );
1165         p_ccid_rec := trx_type_rec_t( p_trx_type_id );
1166         p_ccid_frt := trx_type_frt_t( p_trx_type_id );
1167         p_ccid_tax := trx_type_tax_t( p_trx_type_id );
1168         p_ccid_unbill := trx_type_unbill_t( p_trx_type_id );
1169         p_ccid_unearn := trx_type_unearn_t( p_trx_type_id );
1170         p_ccid_suspense := trx_type_suspense_t( p_trx_type_id );
1171 
1172         debug( '  cache hit: trx_type_id='||to_char(p_trx_type_id),
1173                MSG_LEVEL_DEBUG );
1174 
1175     EXCEPTION
1176         WHEN NO_DATA_FOUND THEN
1177             -- else, get it from the database
1178             --
1179             debug( '  cache miss: trx_type_id='||to_char(p_trx_type_id),
1180                    MSG_LEVEL_DEBUG );
1181 
1182             SELECT
1183               nvl(gl_id_rev,-1),
1184               nvl(gl_id_rec,-1),
1185               nvl(gl_id_freight,-1),
1186               nvl(gl_id_tax,-1),
1187               nvl(gl_id_unbilled,-1),
1188               nvl(gl_id_unearned,-1),
1189               nvl(gl_id_clearing,-1)
1190             INTO
1191               p_ccid_rev,
1192               p_ccid_rec,
1193               p_ccid_frt,
1194               p_ccid_tax,
1195               p_ccid_unbill,
1196               p_ccid_unearn,
1197               p_ccid_suspense
1198             FROM ra_cust_trx_types
1199             WHERE cust_trx_type_id = p_trx_type_id;
1200 
1201             -- update cache
1202 	    trx_type_rev_t( p_trx_type_id ) := p_ccid_rev;
1203             trx_type_rec_t( p_trx_type_id ) := p_ccid_rec;
1204             trx_type_frt_t( p_trx_type_id ) := p_ccid_frt;
1205             trx_type_tax_t( p_trx_type_id ) := p_ccid_tax;
1206             trx_type_unbill_t( p_trx_type_id ) := p_ccid_unbill;
1207             trx_type_unearn_t( p_trx_type_id ) := p_ccid_unearn;
1208             trx_type_suspense_t( p_trx_type_id ) := p_ccid_suspense;
1209     END;
1210 
1211 
1212     print_fcn_label2( 'arp_auto_accounting.get_trx_type_ccids()-' );
1213 
1214 EXCEPTION
1215     WHEN NO_DATA_FOUND THEN
1216 	debug('arp_auto_accounting.get_trx_type_ccids(): no data found',
1217 	      MSG_LEVEL_DEBUG);
1218     WHEN OTHERS THEN
1219         debug('EXCEPTION: arp_auto_accounting.get_trx_type_ccids('
1220               || to_char(p_trx_type_id) ||')', MSG_LEVEL_BASIC);
1221         debug(SQLERRM, MSG_LEVEL_BASIC);
1222         RAISE;
1223 END get_trx_type_ccids;
1224 
1225 
1226 ----------------------------------------------------------------------------
1227 --
1228 -- PROCEDURE NAME:  get_site_use_ccids
1229 --
1230 -- DECSRIPTION:
1231 --   Retrieves default ccids from the table hz_cust_site_uses
1232 --   for a given bill to site use id.
1233 --
1234 -- ARGUMENTS:
1235 --      IN:
1236 --        site_use_id
1237 --
1238 --      IN/OUT:
1239 --        ccid_rev
1240 --        ccid_rec
1241 --        ccid_frt
1242 --        ccid_tax
1243 --        ccid_unbill
1244 --        ccid_unearn
1245 --        ccid_suspense
1246 --
1247 --      OUT:
1248 --
1249 -- NOTES:
1250 --
1251 -- HISTORY:
1252 --
1253 PROCEDURE get_site_use_ccids( p_site_use_id 	IN number,
1254                                p_ccid_rev 	IN OUT NOCOPY BINARY_INTEGER,
1255                                p_ccid_rec 	IN OUT NOCOPY BINARY_INTEGER,
1256                                p_ccid_frt 	IN OUT NOCOPY BINARY_INTEGER,
1257                                p_ccid_tax 	IN OUT NOCOPY BINARY_INTEGER,
1258                                p_ccid_unbill 	IN OUT NOCOPY BINARY_INTEGER,
1259                                p_ccid_unearn 	IN OUT NOCOPY BINARY_INTEGER,
1260                                p_ccid_suspense 	IN OUT NOCOPY BINARY_INTEGER)  IS
1261 BEGIN
1262     print_fcn_label2( 'arp_auto_accounting.get_site_use_ccids()+' );
1263 
1264     --
1265     -- initialize
1266     --
1267     p_ccid_rev := -1;
1268     p_ccid_rec := -1;
1269     p_ccid_frt := -1;
1270     p_ccid_tax := -1;
1271     p_ccid_unbill := -1;
1272     p_ccid_unearn := -1;
1273     p_ccid_suspense := -1;
1274 
1275 
1276     BEGIN
1277         -- see if available in cache
1278         --
1279         p_ccid_rev := site_use_rev_t( p_site_use_id );
1280         p_ccid_rec := site_use_rec_t( p_site_use_id );
1281         p_ccid_frt := site_use_frt_t( p_site_use_id );
1282         p_ccid_tax := site_use_tax_t( p_site_use_id );
1283         p_ccid_unbill := site_use_unbill_t( p_site_use_id );
1284         p_ccid_unearn := site_use_unearn_t( p_site_use_id );
1285         p_ccid_suspense := site_use_suspense_t( p_site_use_id );
1286 
1287         debug( '  cache hit: site_use_id='||to_char(p_site_use_id),
1288                MSG_LEVEL_DEBUG );
1289 
1290     EXCEPTION
1291         WHEN NO_DATA_FOUND THEN
1292             -- else, get it from the database
1293             --
1294             debug( '  cache miss: site_use_id='||to_char(p_site_use_id),
1295                    MSG_LEVEL_DEBUG );
1296 
1297             SELECT
1298               nvl(gl_id_rev,-1),
1299               nvl(gl_id_rec,-1),
1300               nvl(gl_id_freight,-1),
1301               nvl(gl_id_tax,-1),
1302               nvl(gl_id_unbilled,-1),
1303               nvl(gl_id_unearned,-1),
1304               nvl(gl_id_clearing,-1)
1305             INTO
1306               p_ccid_rev,
1307               p_ccid_rec,
1308               p_ccid_frt,
1309               p_ccid_tax,
1310               p_ccid_unbill,
1311               p_ccid_unearn,
1312               p_ccid_suspense
1313             FROM hz_cust_site_uses
1314             WHERE site_use_id = p_site_use_id;
1315 
1316             -- update cache
1317 	    site_use_rev_t( p_site_use_id ) := p_ccid_rev;
1318             site_use_rec_t( p_site_use_id ) := p_ccid_rec;
1319             site_use_frt_t( p_site_use_id ) := p_ccid_frt;
1320             site_use_tax_t( p_site_use_id ) := p_ccid_tax;
1321             site_use_unbill_t( p_site_use_id ) := p_ccid_unbill;
1322             site_use_unearn_t( p_site_use_id ) := p_ccid_unearn;
1323             site_use_suspense_t( p_site_use_id ) := p_ccid_suspense;
1324     END;
1325 
1326 
1327     print_fcn_label2( 'arp_auto_accounting.get_site_use_ccids()-' );
1328 
1329 EXCEPTION
1330     WHEN NO_DATA_FOUND THEN
1331 	debug('arp_auto_accounting.get_site_use_ccids(): no data found',
1332 	      MSG_LEVEL_DEBUG);
1333     WHEN OTHERS THEN
1334         debug('EXCEPTION: arp_auto_accounting.get_site_use_ccids('
1335               || to_char(p_site_use_id) ||')', MSG_LEVEL_BASIC);
1336         debug(SQLERRM, MSG_LEVEL_BASIC);
1337         RAISE;
1338 END get_site_use_ccids;
1339 
1340 ----------------------------------------------------------------------------
1341 --
1342 -- PROCEDURE NAME:  get_salesrep_ccids
1343 --
1344 -- DECSRIPTION:
1345 --   Retrieves default ccids from the table ra_salesreps
1346 --   for a given salesrep_id.
1347 --
1348 -- ARGUMENTS:
1349 --      IN:
1350 --        salesrep_id
1351 --
1352 --      IN/OUT:
1353 --        ccid_rev
1354 --        ccid_rec
1355 --        ccid_frt
1356 --
1357 --      OUT:
1358 --
1359 -- NOTES:
1360 --
1361 -- HISTORY:
1362 --
1363 --
1364 PROCEDURE get_salesrep_ccids( p_salesrep_id 	IN BINARY_INTEGER,
1365                                p_ccid_rev 	IN OUT NOCOPY BINARY_INTEGER,
1366                                p_ccid_rec 	IN OUT NOCOPY BINARY_INTEGER,
1367                                p_ccid_frt 	IN OUT NOCOPY BINARY_INTEGER ) IS
1368 BEGIN
1369 
1370     print_fcn_label2( 'arp_auto_accounting.get_salesrep_ccids()+' );
1371 
1372     p_ccid_rev := -1;
1373     p_ccid_rec := -1;
1374     p_ccid_frt := -1;
1375 
1376     BEGIN
1377         -- see if available in cache
1378         --
1379         p_ccid_rev := salesrep_rev_t( p_salesrep_id );
1380         p_ccid_rec := salesrep_rec_t( p_salesrep_id );
1381         p_ccid_frt := salesrep_frt_t( p_salesrep_id );
1382 
1383         debug( '  cache hit: salesrep_id='||to_char(p_salesrep_id),
1384                MSG_LEVEL_DEBUG );
1385 
1386     EXCEPTION
1387         WHEN NO_DATA_FOUND THEN
1388             -- else, get it from the database
1389             --
1390             debug( '  cache miss: salesrep_id='||to_char(p_salesrep_id),
1391                    MSG_LEVEL_DEBUG );
1392 
1393             SELECT
1394               nvl(gl_id_rev,-1),
1395               nvl(gl_id_rec,-1),
1396               nvl(gl_id_freight,-1)
1397             INTO p_ccid_rev, p_ccid_rec, p_ccid_frt
1398             FROM ra_salesreps
1399             WHERE salesrep_id = p_salesrep_id;
1400 
1401             -- update cache
1402 	    salesrep_rev_t( p_salesrep_id ) := p_ccid_rev;
1403             salesrep_rec_t( p_salesrep_id ) := p_ccid_rec;
1404             salesrep_frt_t( p_salesrep_id ) := p_ccid_frt;
1405 
1406     END;
1407 
1408     print_fcn_label2( 'arp_auto_accounting.get_salesrep_ccids()-' );
1409 
1410 EXCEPTION
1411     WHEN NO_DATA_FOUND THEN
1412 	debug('arp_auto_accounting.get_salesrep_ccids(): no data found',
1413 	      MSG_LEVEL_DEBUG);
1414     WHEN OTHERS THEN
1415         debug('EXCEPTION: arp_auto_accounting.get_salesrep_ccids('
1416               || to_char(p_salesrep_id) ||')', MSG_LEVEL_BASIC);
1417         debug(SQLERRM, MSG_LEVEL_BASIC);
1418         RAISE;
1419 END get_salesrep_ccids;
1420 
1421 ----------------------------------------------------------------------------
1422 --
1423 -- PROCEDURE NAME:  get_inv_item_ccids
1424 --
1425 -- DECSRIPTION:
1426 --   Retrieves default ccids from the table mtl_system_items
1427 --   for a given item_id.
1428 --
1429 -- ARGUMENTS:
1430 --      IN:
1431 --        profile_info
1432 --        item_id
1433 --
1434 --      IN/OUT:
1435 --        ccid_rev
1436 --        inv_item_type
1437 --
1438 --      OUT:
1439 --
1440 -- NOTES:
1441 --
1442 -- HISTORY:
1443 --
1444 --
1445 PROCEDURE get_inv_item_ccids( p_profile_info 	IN
1446 	                          arp_trx_global.profile_rec_type,
1447                               p_inv_item_id 	IN BINARY_INTEGER,
1448                               p_warehouse_id    IN BINARY_INTEGER,
1449                               p_ccid_rev IN 	OUT NOCOPY BINARY_INTEGER,
1450                               p_inv_item_type 	IN OUT NOCOPY
1451                                   mtl_system_items.item_type%TYPE ) IS
1452 l_ctr BINARY_INTEGER;
1453 l_hit BOOLEAN := FALSE;
1454 t_warehouse_id BINARY_INTEGER;
1455 
1456 BEGIN
1457 
1458     print_fcn_label2( 'arp_auto_accounting.get_inv_item_ccids()+' );
1459 
1460     p_ccid_rev := -1;
1461     p_inv_item_type := NULL;
1462     t_warehouse_id  := nvl(p_warehouse_id,
1463                                          to_number(oe_profile.value('SO_ORGANIZATION_ID',arp_global.sysparam.org_id)));
1464     BEGIN
1465        --
1466        -- see if available in cache
1467        --
1468 
1469 
1470                IF ((inv_item_rev_t(p_inv_item_id || ':' || t_warehouse_id).inventory_item_id = p_inv_item_id)
1471                   AND (inv_item_rev_t(p_inv_item_id || ':' || t_warehouse_id).warehouse_id
1472                               = t_warehouse_id))
1473                THEN
1474 
1475                  l_hit           := TRUE;
1476                  p_inv_item_type := inv_item_rev_t(p_inv_item_id || ':' || t_warehouse_id).item_type;
1477                  p_ccid_rev      := inv_item_rev_t(p_inv_item_id || ':' || t_warehouse_id).sales_account;
1478 
1479                  debug( '  cache hit: Item Id='||to_char(p_inv_item_id),
1480                         MSG_LEVEL_DEBUG );
1481                  debug( '  cache hit: Warehouse_id='||
1482                        t_warehouse_id,
1483                         MSG_LEVEL_DEBUG );
1484                  debug('Index is: ' || p_inv_item_id || ':' || t_warehouse_id);
1485                  debug( '  cache hit: Item Type='|| p_inv_item_type,
1486                         MSG_LEVEL_DEBUG );
1487                  debug( '  cache hit: revenue account='||to_char(p_ccid_rev),
1488                         MSG_LEVEL_DEBUG );
1489 
1490 
1491                END IF; --end if inventory_item_id:warehouse_id key matches
1492 
1493 
1494        --
1495        --Raise explicitly exception as item warehouse id combination did not exist in
1496        --cache, hence get from the database
1497        --
1498 
1499         IF NOT l_hit THEN
1500            RAISE NO_DATA_FOUND;
1501         END IF;
1502 
1503     EXCEPTION
1504         WHEN NO_DATA_FOUND THEN
1505             -- else, get it from the database
1506             --
1507             debug( '  cache miss: inv_item_id='||to_char(p_inv_item_id),
1508                    MSG_LEVEL_DEBUG );
1509             debug( '  cache miss: warehouse_id='||to_char(p_warehouse_id),
1510                    MSG_LEVEL_DEBUG );
1511 
1512             SELECT nvl(sales_account, -1), nvl(item_type, '~')
1513             INTO   p_ccid_rev,
1514                    p_inv_item_type
1515             FROM   mtl_system_items
1516             WHERE  organization_id
1517                        = t_warehouse_id
1518             AND    inventory_item_id = p_inv_item_id;
1519 
1520             -- update cache
1521             debug( 'Inserting into the cache: ');
1522             debug('Index is: ' || p_inv_item_id || ':' || t_warehouse_id);
1523             inv_item_rev_t(p_inv_item_id || ':' || t_warehouse_id).inventory_item_id := p_inv_item_id;
1524 
1525             inv_item_rev_t(p_inv_item_id || ':' || t_warehouse_id).warehouse_id
1526                         := t_warehouse_id;
1527             inv_item_rev_t(p_inv_item_id || ':' || t_warehouse_id).item_type := p_inv_item_type;
1528             --debug('Inventory Item Type is: ' || p_inv_item_type);
1529             inv_item_rev_t(p_inv_item_id || ':' || t_warehouse_id).sales_account := p_ccid_rev;
1530             --debug('Sales Account: ' || p_ccid_rev);
1531 
1532     END;
1533 
1534     print_fcn_label2( 'arp_auto_accounting.get_inv_item_ccids()-' );
1535 
1536 EXCEPTION
1537     WHEN NO_DATA_FOUND THEN
1538 	debug('arp_auto_accounting.get_inv_item_ccids(): no data found',
1539 	      MSG_LEVEL_DEBUG);
1540     WHEN OTHERS THEN
1541         debug('EXCEPTION: arp_auto_accounting.get_inv_item_ccids('
1542               || to_char(p_inv_item_id) ||')', MSG_LEVEL_BASIC);
1543         debug(SQLERRM, MSG_LEVEL_BASIC);
1544         RAISE;
1545 END get_inv_item_ccids;
1546 
1547 ----------------------------------------------------------------------------
1548 --
1549 -- PROCEDURE NAME:  get_memo_line_ccids
1550 --
1551 -- DECSRIPTION:
1552 --   Retrieves default ccids from the table ar_memo_lines
1553 --   for a given memo_line_id.
1554 --
1555 -- ARGUMENTS:
1556 --      IN:
1557 --        memo_line_id
1558 --
1559 --      IN/OUT:
1560 --        ccid_rev
1561 --
1562 --      OUT:
1563 --
1564 -- NOTES:
1565 --
1566 -- HISTORY:
1567 --
1568 --
1569 PROCEDURE get_memo_line_ccids( p_memo_line_id 	IN BINARY_INTEGER,
1570                                p_ccid_rev 	IN OUT NOCOPY BINARY_INTEGER ) IS
1571 BEGIN
1572 
1573     print_fcn_label2( 'arp_auto_accounting.get_memo_line_ccids()+' );
1574 
1575     p_ccid_rev := -1;
1576 
1577     BEGIN
1578         -- see if available in cache
1579         --
1580         p_ccid_rev := memo_line_rev_t( p_memo_line_id );
1581 
1582         debug( '  cache hit: memo_line_id='||to_char(p_memo_line_id),
1583                MSG_LEVEL_DEBUG );
1584 
1585     EXCEPTION
1586         WHEN NO_DATA_FOUND THEN
1587             -- else, get it from the database
1588             --
1589             debug( '  cache miss: memo_line_id='||to_char(p_memo_line_id),
1590                    MSG_LEVEL_DEBUG );
1591 
1592             SELECT nvl(gl_id_rev,-1)
1593             INTO p_ccid_rev
1594             FROM ar_memo_lines
1595             WHERE memo_line_id = p_memo_line_id;
1596 
1597             -- update cache
1598 	    memo_line_rev_t( p_memo_line_id ) := p_ccid_rev;
1599 
1600     END;
1601 
1602     print_fcn_label2( 'arp_auto_accounting.get_memo_line_ccids()-' );
1603 
1604 EXCEPTION
1605     WHEN NO_DATA_FOUND THEN
1606 	debug('arp_auto_accounting.get_memo_line_ccids(): no data found',
1607 	      MSG_LEVEL_DEBUG);
1608     WHEN OTHERS THEN
1609         debug('EXCEPTION: arp_auto_accounting.get_memo_line_ccids('
1610               || to_char(p_memo_line_id) ||')', MSG_LEVEL_BASIC);
1611         debug(SQLERRM, MSG_LEVEL_BASIC);
1612         RAISE;
1613 END get_memo_line_ccids;
1614 
1615 ----------------------------------------------------------------------------
1616 --
1617 -- PROCEDURE NAME:  get_agreecat_ccids
1618 --
1619 -- DECSRIPTION:
1620 --   Retrieves default ccids for agreement/category if autoaccounting for
1621 --   revenue is based on agreement/category
1622 --
1623 -- ARGUMENTS:
1624 --      IN:
1625 --        profile_info
1626 --        line_id
1627 --
1628 --      IN/OUT:
1629 --
1630 --      OUT:
1631 --        ccid_rev
1632 --
1633 -- NOTES:
1634 --
1635 -- HISTORY:
1636 --
1637 --
1638 PROCEDURE get_agreecat_ccids( p_profile_info 	IN
1639                                   arp_trx_global.profile_rec_type,
1640                               p_line_id 	IN number,
1641 			      p_warehouse_id    IN BINARY_INTEGER,       --Bug#1639334
1642                               p_ccid_rev 	OUT NOCOPY BINARY_INTEGER ) IS
1643 BEGIN
1644 
1645     print_fcn_label2( 'arp_auto_accounting.get_agreecat_ccids()+' );
1646 
1647     p_ccid_rev := -1;
1648 
1649     SELECT nvl(c.code_combination_id,-1)
1650     INTO   p_ccid_rev
1651     FROM
1652       ra_customer_trx t,
1653       ra_customer_trx_lines l,
1654       mtl_item_categories i,
1655       so_agreements a,
1656       ra_account_combinations c
1657     WHERE  t.customer_trx_id      = l.customer_trx_id
1658     AND    l.customer_trx_line_id = p_line_id
1659     AND    t.agreement_id         = a.agreement_id(+)
1660     AND    l.inventory_item_id    = i.inventory_item_id(+)
1661     AND    i.organization_id(+)
1662                      = nvl(p_warehouse_id,
1663                            to_number(oe_profile.value('SO_ORGANIZATION_ID',arp_global.sysparam.org_id)))     --Bug#1639334
1664     AND    i.category_set_id(+)   = 1
1665     AND    to_char(nvl(i.category_id, -1)) = c.value1
1666     AND    nvl(a.agreement_type_code, -1) = nvl(c.value2 , -1);
1667 
1668     print_fcn_label2( 'arp_auto_accounting.get_agreecat_ccids()-' );
1669 
1670 EXCEPTION
1671     WHEN NO_DATA_FOUND THEN
1672 	debug('arp_auto_accounting.get_agreecat_ccids(): no data found',
1673 	      MSG_LEVEL_DEBUG);
1674     WHEN OTHERS THEN
1675         debug('EXCEPTION: arp_auto_accounting.get_agreecat_ccids('
1676               || to_char(p_line_id) ||')', MSG_LEVEL_BASIC);
1677         debug(SQLERRM, MSG_LEVEL_BASIC);
1678         RAISE;
1679 END get_agreecat_ccids;
1680 
1681 /* ------------------------------------------------------------------------ */
1682 /*      Finds combination_id for given segment values.                      */
1683 /*      If validation date is NULL checks all cross-validation rules.       */
1684 /*      Returns TRUE if combination valid, or FALSE and sets error message  */
1685 /*      on server using FND_MESSAGE if invalid.                             */
1686 /* ------------------------------------------------------------------------ */
1687 FUNCTION get_combination_id(application_short_name    IN  VARCHAR2,
1688                            key_flex_code        IN  VARCHAR2,
1689                            structure_number     IN  NUMBER,
1690                            validation_date      IN  DATE,
1691                            concat_segments      IN  VARCHAR2,
1692                            combination_id       OUT NOCOPY NUMBER)
1693                                                             RETURN BOOLEAN IS
1694   r_value BOOLEAN := FALSE;
1695   BEGIN
1696 
1697 --  Initialize messages, debugging, and number of sql strings
1698 --
1699  print_fcn_label( 'arp_auto_accounting.get_combination_id()+' );
1700 
1701     r_value := FND_FLEX_KEYVAL.validate_segs('CREATE_COMBINATION',
1702         application_short_name, key_flex_code, structure_number,
1703 	concat_segments, 'V',
1704         validation_date);
1705     if( r_value ) then
1706       combination_id := FND_FLEX_KEYVAL.combination_id;
1707       print_fcn_label( 'arp_auto_accounting.get_combination_id()-' );
1708       return(r_value);
1709     end if;
1710 
1711     return(r_value);
1712 
1713   EXCEPTION
1714      WHEN OTHERS THEN
1715          FND_MESSAGE.set_name( 'AR', 'GENERIC_MESSAGE' );
1716          FND_MESSAGE.set_token( 'GENERIC_TEXT', 'get_combination_id() exception: '||SQLERRM );
1717          return(FALSE);
1718 
1719 END get_combination_id;
1720 
1721 /* ------------------------------------------------------------------------ */
1722 /*      Overloaded version of above for user with individual segments.      */
1723 /* ------------------------------------------------------------------------ */
1724 
1725   FUNCTION get_combination_id(application_short_name    IN  VARCHAR2,
1726                            key_flex_code        IN  VARCHAR2,
1727                            structure_number     IN  NUMBER,
1728                            validation_date      IN  DATE,
1729                            n_segments           IN  NUMBER,
1730                            segments             IN  FND_FLEX_EXT.SegmentArray,
1731                            combination_id       OUT NOCOPY NUMBER)
1732                                                             RETURN BOOLEAN IS
1733     sepchar     VARCHAR2(1);
1734     catsegs     VARCHAR2(2000);
1735 
1736   BEGIN
1737     print_fcn_label( 'arp_auto_accounting.get_combination_id1()+' );
1738 
1739 --  Concatenate the input segments, then send them to the other function.
1740 --
1741     sepchar := fnd_flex_ext.get_delimiter(application_short_name, key_flex_code,
1742                              structure_number);
1743     if(sepchar is not null) then
1744       print_fcn_label( 'arp_auto_accounting.get_combination_id1()-' );
1745       return(get_combination_id(application_short_name, key_flex_code,
1746                          structure_number, validation_date,
1747                          FND_FLEX_EXT.concatenate_segments(n_segments, segments, sepchar),
1748                          combination_id));
1749     end if;
1750     return(FALSE);
1751 
1752   EXCEPTION
1753      WHEN OTHERS THEN
1754          FND_MESSAGE.set_name( 'AR', 'GENERIC_MESSAGE' );
1755          FND_MESSAGE.set_token( 'GENERIC_TEXT', 'get_combination_id() exception: '||SQLERRM );
1756          return(FALSE);
1757 
1758 END get_combination_id;
1759 
1760 ----------------------------------------------------------------------------
1761 PROCEDURE define_columns( p_select_c   IN INTEGER,
1762                           p_select_rec IN select_rec_type) IS
1763 BEGIN
1764     print_fcn_label2( 'arp_auto_accounting.define_columns()+' );
1765 
1766     dbms_sql.define_column( p_select_c, 1, p_select_rec.customer_trx_id );
1767     dbms_sql.define_column( p_select_c, 2, p_select_rec.customer_trx_line_id );
1768     dbms_sql.define_column( p_select_c, 3,
1769                             p_select_rec.cust_trx_line_salesrep_id );
1770     dbms_sql.define_column( p_select_c, 4, p_select_rec.line_amount );
1771     dbms_sql.define_column( p_select_c, 5,
1772                             p_select_rec.accounted_line_amount );
1773     dbms_sql.define_column( p_select_c, 6, p_select_rec.percent );
1774     dbms_sql.define_column( p_select_c, 7, p_select_rec.amount );
1775     dbms_sql.define_column( p_select_c, 8, p_select_rec.acctd_amount );
1776     dbms_sql.define_column( p_select_c, 9, p_select_rec.account_class, 20 );
1777     dbms_sql.define_column( p_select_c, 10, p_select_rec.account_set_flag, 1 );
1778     dbms_sql.define_column( p_select_c, 11, p_select_rec.cust_trx_type_id );
1779     dbms_sql.define_column( p_select_c, 12,
1780                             p_select_rec.allow_not_open_flag, 1 );
1781     dbms_sql.define_column( p_select_c, 13,
1782                             p_select_rec.concatenated_segments, 240 );
1783     dbms_sql.define_column( p_select_c, 14, p_select_rec.code_combination_id );
1784     dbms_sql.define_column( p_select_c, 15, p_select_rec.gl_date, 12 );
1785     dbms_sql.define_column( p_select_c, 16,
1786                             p_select_rec.original_gl_date, 12 );
1787     dbms_sql.define_column( p_select_c, 17, p_select_rec.ussgl_trx_code, 30 );
1788     dbms_sql.define_column( p_select_c, 18,
1789                             p_select_rec.ussgl_trx_code_context, 30 );
1790     dbms_sql.define_column( p_select_c, 19, p_select_rec.salesrep_id );
1791     dbms_sql.define_column( p_select_c, 20, p_select_rec.inventory_item_id );
1792     dbms_sql.define_column( p_select_c, 21, p_select_rec.memo_line_id );
1793     dbms_sql.define_column( p_select_c, 22, p_select_rec.default_tax_ccid );
1794     dbms_sql.define_column( p_select_c, 23, p_select_rec.interim_tax_ccid );
1795     dbms_sql.define_column( p_select_c, 24, p_select_rec.site_use_id);
1796     dbms_sql.define_column( p_select_c, 25, p_select_rec.warehouse_id);
1797 
1798     print_fcn_label2( 'arp_auto_accounting.define_columns()-' );
1799 EXCEPTION
1800     WHEN OTHERS THEN
1801         debug('EXCEPTION: arp_auto_accounting.define_columns()',
1802 		MSG_LEVEL_BASIC);
1803         debug(SQLERRM, MSG_LEVEL_BASIC);
1804         RAISE;
1805 END define_columns;
1806 ----------------------------------------------------------------------------
1807 
1808 PROCEDURE define_arrays( p_select_c   IN INTEGER,
1809                           p_select_tab IN select_rec_tab) IS
1810 BEGIN
1811     print_fcn_label2( 'arp_auto_accounting.define_arrays()+' );
1812 
1813     dbms_sql.define_array( p_select_c, 1, p_select_tab.customer_trx_id, MAX_ARRAY_SIZE, STARTING_INDEX );
1814     dbms_sql.define_array( p_select_c, 2, p_select_tab.customer_trx_line_id, MAX_ARRAY_SIZE, STARTING_INDEX );
1815     dbms_sql.define_array( p_select_c, 3,
1816                             p_select_tab.cust_trx_line_salesrep_id, MAX_ARRAY_SIZE, STARTING_INDEX );
1817     dbms_sql.define_array( p_select_c, 4, p_select_tab.line_amount, MAX_ARRAY_SIZE, STARTING_INDEX );
1818     dbms_sql.define_array( p_select_c, 5,
1819                             p_select_tab.accounted_line_amount, MAX_ARRAY_SIZE, STARTING_INDEX );
1820     dbms_sql.define_array( p_select_c, 6, p_select_tab.percent, MAX_ARRAY_SIZE, STARTING_INDEX );
1821     dbms_sql.define_array( p_select_c, 7, p_select_tab.amount, MAX_ARRAY_SIZE, STARTING_INDEX );
1822     dbms_sql.define_array( p_select_c, 8, p_select_tab.acctd_amount, MAX_ARRAY_SIZE, STARTING_INDEX );
1823     dbms_sql.define_array( p_select_c, 9, p_select_tab.account_class, MAX_ARRAY_SIZE, STARTING_INDEX );
1824     dbms_sql.define_array( p_select_c, 10, p_select_tab.account_set_flag, MAX_ARRAY_SIZE, STARTING_INDEX );
1825     dbms_sql.define_array( p_select_c, 11, p_select_tab.cust_trx_type_id, MAX_ARRAY_SIZE, STARTING_INDEX );
1826     dbms_sql.define_array( p_select_c, 12,
1827                             p_select_tab.allow_not_open_flag, MAX_ARRAY_SIZE, STARTING_INDEX);
1828     dbms_sql.define_array( p_select_c, 13,
1829                             p_select_tab.concatenated_segments, MAX_ARRAY_SIZE, STARTING_INDEX );
1830     dbms_sql.define_array( p_select_c, 14, p_select_tab.code_combination_id, MAX_ARRAY_SIZE, STARTING_INDEX );
1831     dbms_sql.define_array( p_select_c, 15, p_select_tab.gl_date, MAX_ARRAY_SIZE, STARTING_INDEX );
1832     dbms_sql.define_array( p_select_c, 16,
1833                             p_select_tab.original_gl_date, MAX_ARRAY_SIZE, STARTING_INDEX );
1834     dbms_sql.define_array( p_select_c, 17, p_select_tab.ussgl_trx_code, MAX_ARRAY_SIZE, STARTING_INDEX );
1835     dbms_sql.define_array( p_select_c, 18,
1836                             p_select_tab.ussgl_trx_code_context, MAX_ARRAY_SIZE, STARTING_INDEX );
1837     dbms_sql.define_array( p_select_c, 19, p_select_tab.salesrep_id, MAX_ARRAY_SIZE, STARTING_INDEX );
1838     dbms_sql.define_array( p_select_c, 20, p_select_tab.inventory_item_id, MAX_ARRAY_SIZE, STARTING_INDEX );
1839     dbms_sql.define_array( p_select_c, 21, p_select_tab.memo_line_id, MAX_ARRAY_SIZE, STARTING_INDEX );
1840     dbms_sql.define_array( p_select_c, 22, p_select_tab.default_tax_ccid, MAX_ARRAY_SIZE, STARTING_INDEX );
1841     dbms_sql.define_array( p_select_c, 23, p_select_tab.interim_tax_ccid, MAX_ARRAY_SIZE, STARTING_INDEX );
1842     dbms_sql.define_array( p_select_c, 24, p_select_tab.site_use_id, MAX_ARRAY_SIZE, STARTING_INDEX);
1843     dbms_sql.define_array( p_select_c, 25, p_select_tab.warehouse_id, MAX_ARRAY_SIZE, STARTING_INDEX);
1844  -- 1651593
1845     dbms_sql.define_array( p_select_c, 26, p_select_tab.link_to_cust_trx_line_id, MAX_ARRAY_SIZE, STARTING_INDEX );
1846 
1847     print_fcn_label2( 'arp_auto_accounting.define_arrays()-' );
1848 EXCEPTION
1849     WHEN OTHERS THEN
1850         debug('EXCEPTION: arp_auto_accounting.define_arrays()',
1851 		MSG_LEVEL_BASIC);
1852         debug(SQLERRM, MSG_LEVEL_BASIC);
1853         RAISE;
1854 END define_arrays;
1855 
1856 
1857 ----------------------------------------------------------------------------
1858 --
1859 -- FUNCTION NAME:  build_select_sql
1860 --
1861 -- DECSRIPTION:
1862 --
1863 -- ARGUMENTS:
1864 --      IN:
1865 --        system_info
1866 --        profile_info
1867 --        account_class
1868 --        customer_trx_id
1869 --        customer_trx_line_id
1870 --        cust_trx_line_salesrep_id
1871 --        request_id
1872 --        gl_date
1873 --        original_gl_date
1874 --        total_trx_amount
1875 --        code_combination_id
1876 --        force_account_set_no
1877 --        cust_trx_type_id
1878 --        primary_salesrep_id
1879 --        inventory_item_id
1880 --        memo_line_id
1881 --
1882 --      IN/OUT:
1883 --
1884 --      OUT:
1885 --
1886 -- RETURNS:
1887 --   select statement
1888 --
1889 -- NOTES:
1890 --
1891 -- HISTORY:
1892 --    14-FEB-97  C. Tomberg  Modified to use bind variables
1893 --
1894 FUNCTION build_select_sql( p_system_info 		IN
1895                              arp_trx_global.system_info_rec_type,
1896                            p_profile_info 		IN
1897                              arp_trx_global.profile_rec_type,
1898                            p_account_class 		IN VARCHAR2,
1899                            p_customer_trx_id 		IN BINARY_INTEGER,
1900                            p_customer_trx_line_id 	IN number,
1901                            p_cust_trx_line_salesrep_id 	IN number,
1902                            p_request_id 		IN BINARY_INTEGER,
1903                            p_gl_date 			IN DATE,
1904                            p_original_gl_date 		IN DATE,
1905                            p_total_trx_amount 		IN NUMBER,
1906                            p_code_combination_id 	IN BINARY_INTEGER,
1907                            p_force_account_set_no 	IN VARCHAR2,
1908                            p_cust_trx_type_id 		IN BINARY_INTEGER,
1909                            p_primary_salesrep_id 	IN BINARY_INTEGER,
1910                            p_inventory_item_id 		IN BINARY_INTEGER,
1911                            p_memo_line_id 		IN BINARY_INTEGER,
1912 			   p_use_unearn_srep_dependency IN BOOLEAN DEFAULT FALSE)
1913   RETURN VARCHAR2 IS
1914 
1915     l_based_on_salesrep_flag    BOOLEAN := FALSE;
1916 
1917     l_select_stmt               VARCHAR2(32767);
1918 
1919     l_amount_fragment           VARCHAR2(200);
1920     l_ccid_fragment             VARCHAR2(100);
1921     l_decode_fragment           VARCHAR2(1000);
1922     l_line_type_fragment        VARCHAR2(100);
1923     l_rule_id_fragment          VARCHAR2(100);
1924     l_rule_fragment             VARCHAR2(700);
1925     l_tax_table_fragment        VARCHAR2(100);
1926 
1927     l_gl_date_attribute         VARCHAR2(1000);
1928     l_orig_gl_date_attribute    VARCHAR2(2000);
1929     l_salesrep_attributes1      VARCHAR2(10000);
1930     l_salesrep_attributes2      VARCHAR2(1000);
1931     l_tax_attribute             VARCHAR2(512);
1932 
1933     l_gl_dist_table             VARCHAR2(100);
1934     l_interface_lines_table     VARCHAR2(100);
1935     l_inv_gl_dist_table         VARCHAR2(100);
1936     l_salesreps_table           VARCHAR2(100);
1937     l_tax_table                 VARCHAR2(100);
1938 
1939     l_based_on_salesrep_pred    VARCHAR2(500);
1940     l_cm_module_pred            VARCHAR2(500);
1941     l_interface_table_pred      VARCHAR2(500);
1942     l_inv_rec_pred              VARCHAR2(500);
1943     l_line_id_pred              VARCHAR2(500);
1944     l_line_salesrep_id_pred     VARCHAR2(100);
1945     l_prevent_dup_rec_pred      VARCHAR2(2000);
1946     l_request_id_pred           VARCHAR2(2000);
1947     l_suspense_pred             VARCHAR2(100);
1948     l_tax_pred                  VARCHAR2(500);
1949     l_trx_id_pred               VARCHAR2(100);
1950     l_base_precision		fnd_currencies.precision%type;
1951     l_base_min_acc_unit         VARCHAR2(20);
1952 
1953 BEGIN
1954 
1955     print_fcn_label( 'arp_auto_accounting.build_select_sql()+' );
1956 
1957     l_base_precision     := ARP_GLOBAL.base_precision;
1958 
1959     /* 9222866 - this variable used as a literal in dynamic sql.
1960          so the value NULL must be represented as 'NULL' and numeric
1961          values returned as themselves.  */
1962 
1963 -- Bug 15939049
1964     l_base_min_acc_unit  := NVL(TO_CHAR(ARP_GLOBAL.base_min_acc_unit),'');
1965     l_base_min_acc_unit  := 'to_number('''||l_base_min_acc_unit||''')';
1966 
1967     ------------------------------------------------------------------------
1968     -- Initialize building blocks
1969     ------------------------------------------------------------------------
1970     debug( '  Initialize fragments', MSG_LEVEL_DEVELOP );
1971 
1972     l_amount_fragment := 'nvl(ctl.revenue_amount, ctl.extended_amount)';
1973     l_rule_id_fragment := '= nvl(ct.invoicing_rule_id, -10)';
1974     l_tax_table_fragment := 'ctl';
1975     l_tax_attribute := CRLF ||'to_number(''''),to_number(''''),';
1976 
1977     ------------------------------------------------------------------------
1978     ------------------------------------------------------------------------
1979     -- Construct "building blocks" for the Select statement:
1980     --     string fragments
1981     --     attributes
1982     --     table names
1983     --     predicates
1984     ------------------------------------------------------------------------
1985     ------------------------------------------------------------------------
1986 
1987     ------------------------------------------------------------------------
1988     -- Construct fragments
1989     ------------------------------------------------------------------------
1990 
1991     -- If the force account set no flag is Y,
1992     -- then always treat this line as a non account set distribution
1993     -- otherwise, treat it normally.
1994     debug('  If the force account set no flag is Y', MSG_LEVEL_DEVELOP);
1995 
1996 
1997     l_rule_fragment :=
1998 'decode( NVL(:force_account_set_no, ''N''),
1999         ''N'', ct.invoicing_rule_id,
2000              decode(''' ||p_account_class || ''',
2001                     ''UNBILL'', ct.invoicing_rule_id,
2002                     ''UNEARN'', ct.invoicing_rule_id,
2003                               decode(nvl(ctl.accounting_rule_duration, 0),
2004                                     1, decode(nvl(ctl.autorule_duration_processed, 0),
2005                                               0, ct.invoicing_rule_id,
2006                                                  null),
2007                                          ct.invoicing_rule_id
2008                                     )
2009                    )
2010       )';
2011 
2012 
2013     ------------------------------------------------------------------------
2014     -- Construct code_combination_id fragment
2015     ------------------------------------------------------------------------
2016     debug('  Construct code_combination_id fragment', MSG_LEVEL_DEVELOP);
2017 
2018     IF( p_code_combination_id IS NULL ) THEN
2019 
2020         -- IF   Use Invoice Accounting is Yes,
2021         -- AND  the account class is Receivable,
2022         -- AND  no CCID was passed in
2023         -- THEN use the invoice's receivable CCID
2024         IF( p_profile_info.use_inv_acct_for_cm_flag = 'Y'
2025             AND p_account_class = REC ) THEN
2026 
2027             l_ccid_fragment :=
2028 'nvl(lgd_inv_rec.code_combination_id, to_number(''''))';
2029 
2030         ELSE
2031 
2032             l_ccid_fragment := 'to_number('''')';
2033 
2034         END IF;
2035     ELSE
2036 
2037         l_ccid_fragment := ' :code_combination_id ';
2038 
2039     END IF;
2040 
2041 
2042     ------------------------------------------------------------------------
2043     -- account_class
2044     ------------------------------------------------------------------------
2045     debug('  account_class', MSG_LEVEL_DEVELOP);
2046 
2047     IF( p_account_class = REV ) THEN
2048 
2049         IF(p_system_info.rev_based_on_salesrep
2050 	    OR (P_use_unearn_srep_dependency
2051 	        AND p_system_info.unearn_based_on_salesrep)) THEN
2052             l_based_on_salesrep_flag := TRUE;
2053         END IF;
2054 
2055         l_line_type_fragment := '= ''LINE''';
2056         l_decode_fragment :=
2057 'decode(' || l_rule_fragment || ',
2058          null, decode(ctl.extended_amount,
2059                       0, 1,
2060                       ctl.revenue_amount / ctl.extended_amount),
2061          1
2062         ) *';
2063 
2064     ELSIF( p_account_class = REC ) THEN
2065 
2066         l_amount_fragment :=
2067 'decode(:total_trx_amount,
2068        NULL, to_number(''''),
2069              to_char( :total_trx_amount )
2070       )' || CRLF;
2071 
2072         l_line_type_fragment := '(+) = ''~!''';
2073 
2074 
2075     ELSIF( p_account_class = FREIGHT ) THEN
2076 
2077         l_line_type_fragment := '= ''FREIGHT''';
2078         l_tax_table_fragment := 'ctl_line';
2079 
2080     ELSIF( p_account_class = TAX ) THEN
2081 
2082         IF( p_system_info.tax_based_on_salesrep ) THEN
2083             l_based_on_salesrep_flag := TRUE;
2084         END IF;
2085 
2086         l_line_type_fragment := '= ''TAX''';
2087         l_tax_table_fragment := 'ctl_line';
2088 
2089        /* 4558268 - Replaced source of tax and interim tax ccids */
2090         l_tax_attribute := CRLF ||
2091 'arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
2092                 rgd.gl_date, ''TAX''),'
2093           || CRLF ||
2094 'arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
2095                 rgd.gl_date, ''INTERIM''),';
2096 
2097     ELSIF( p_account_class = UNBILL ) THEN
2098         /* Bug 2354293 - Consider UNEARN/UNBILL tied to salesreps
2099            if REV is... This prevents multiple UNEARN lines with 100%
2100            for a single REV line. */
2101         IF( p_system_info.unbill_based_on_salesrep or
2102             p_system_info.rev_based_on_salesrep) THEN
2103             l_based_on_salesrep_flag := TRUE;
2104         END IF;
2105 
2106         l_line_type_fragment := '= ''LINE''';
2107         l_rule_id_fragment := '= -3';
2108 
2109     ELSIF( p_account_class = UNEARN ) THEN
2110         /* Bug 2354293 */
2111         IF( p_system_info.unearn_based_on_salesrep or
2112             p_system_info.rev_based_on_salesrep) THEN
2113             l_based_on_salesrep_flag := TRUE;
2114         END IF;
2115 
2116         l_line_type_fragment := '= ''LINE''';
2117         l_rule_id_fragment := '= -2';
2118 
2119     ELSIF( p_account_class = SUSPENSE ) THEN
2120 
2121         IF( p_system_info.suspense_based_on_salesrep ) THEN
2122             l_based_on_salesrep_flag := TRUE;
2123         END IF;
2124 
2125         l_line_type_fragment := '= ''LINE''';
2126         l_amount_fragment := '(ctl.extended_amount - ctl.revenue_amount)';
2127         l_decode_fragment :=
2128 'decode(' || l_rule_fragment ||',
2129          null, decode( (ctl.extended_amount),
2130                        0, 1,
2131                        (ctl.extended_amount - ctl.revenue_amount) /
2132                          ctl.extended_amount
2133                      ),
2134          1
2135        ) * ';
2136 
2137     ELSIF( p_account_class = CHARGES ) THEN
2138 
2139         IF( p_system_info.rev_based_on_salesrep ) THEN
2140             l_based_on_salesrep_flag := TRUE;
2141         END IF;
2142 
2143         l_line_type_fragment := '= ''CHARGES''';
2144 
2145     END IF;
2146 
2147     ------------------------------------------------------------------------
2148     -- Construct select attribute strings
2149     ------------------------------------------------------------------------
2150     debug('  Build select attribute strings', MSG_LEVEL_DEVELOP);
2151 
2152     IF( l_based_on_salesrep_flag ) THEN
2153 
2154         debug('  l_salesrep_attributes1', MSG_LEVEL_DEBUG);
2155         debug('  l_rule_fragment='||l_rule_fragment, MSG_LEVEL_DEBUG);
2156         debug('  l_amount_fragment='||l_amount_fragment, MSG_LEVEL_DEBUG);
2157         debug('  l_decode_fragment='||l_decode_fragment, MSG_LEVEL_DEBUG);
2158 
2159         l_salesrep_attributes1 := CRLF ||
2160 'ctls.cust_trx_line_salesrep_id,  /* cust_trx_line_salesrep_id */
2161 decode(' || l_rule_fragment || ',
2162        NULL, ' || l_amount_fragment || ',
2163        to_number('''')),                               /* line_amount */
2164 decode(' || l_rule_fragment || ',
2165        NULL, decode(' || l_base_min_acc_unit || ',
2166                     NULL, round( (' || l_amount_fragment || '*
2167                                   nvl(ct.exchange_rate, 1)),
2168                                  ' || l_base_precision || '),
2169                     round((' || l_amount_fragment || ' *
2170                            nvl(ct.exchange_rate, 1)) /
2171                            ' || l_base_min_acc_unit || ')
2172                                      * ' || l_base_min_acc_unit || '
2173                    ),
2174        to_number('''')),                     /* accounted_line_amount */
2175 round(' || l_decode_fragment || '
2176       decode(ctls.salesrep_id,
2177              NULL, 100,
2178              nvl(ctls.revenue_percent_split, 0)),
2179       4),       /* percent */
2180 decode(' || l_rule_fragment || ',
2181        NULL,
2182        decode(fc_foreign.minimum_accountable_unit,
2183               NULL, round ( (' || l_amount_fragment || ' *
2184                              decode(ctls.salesrep_id,
2185                                     NULL, 100,
2186                                     nvl(ctls.revenue_percent_split, 0))/ 100),
2187                            fc_foreign.precision),
2188               round (  (' || l_amount_fragment || ' *
2189                          decode(ctls.salesrep_id,
2190                                 NULL, 100,
2191                                 nvl(ctls.revenue_percent_split,0)) / 100) /
2192                          fc_foreign.minimum_accountable_unit )  *
2193                 fc_foreign.minimum_accountable_unit ),
2194        to_number('''')),                                      /* amount */
2195 decode(' || l_rule_fragment || ',
2196         NULL,
2197         decode (' || l_base_min_acc_unit || ',
2198                 NULL,
2199                 round (decode(fc_foreign.minimum_accountable_unit,
2200                               NULL,
2201                               round ((' || l_amount_fragment || ' *
2202                                        decode(ctls.salesrep_id,
2203                                               NULL, 100,
2204                                               nvl(ctls.revenue_percent_split,
2205                                                   0)) / 100),
2206                                       fc_foreign.precision),
2207                               round ((' || l_amount_fragment || ' *
2208                                       decode(ctls.salesrep_id,
2209                                              NULL, 100,
2210                                              nvl(ctls.revenue_percent_split,0))
2211                                               / 100) /
2212                                      fc_foreign.minimum_accountable_unit ) *
2213                                 fc_foreign.minimum_accountable_unit
2214                              ) * nvl(ct.exchange_rate, 1),
2215                        ' || l_base_precision || ' ),
2216                 round ((decode( fc_foreign.minimum_accountable_unit,
2217                                 NULL, round((' || l_amount_fragment || ' *
2218                                        decode(ctls.salesrep_id,
2219                                               NULL, 100,
2220                                               nvl(ctls.revenue_percent_split,
2221                                                   0)) / 100),
2222                                             fc_foreign.precision),
2223                                 round((' || l_amount_fragment || ' *
2224                                         decode(ctls.salesrep_id,
2225                                                NULL, 100,
2226                                              nvl(ctls.revenue_percent_split,0))
2227                                                / 100) /
2228                                       fc_foreign.minimum_accountable_unit )  *
2229                                   fc_foreign.minimum_accountable_unit
2230                               ) * nvl(ct.exchange_rate, 1) )
2231                          / ' || l_base_min_acc_unit || ' )  *
2232                   ' || l_base_min_acc_unit || '
2233                ),
2234         to_number('''')),                               /* acctd_amount */';
2235 
2236 
2237         debug('  l_salesrep_attributes2', MSG_LEVEL_DEVELOP);
2238 
2239         l_salesrep_attributes2 := CRLF ||
2240 'nvl(ctl.default_ussgl_transaction_code,
2241     ct.default_ussgl_transaction_code),     /* ussgl_trx_code */
2242 nvl(ctl.default_ussgl_trx_code_context,
2243     ct.default_ussgl_trx_code_context), /* ussgl_trx_code_cntxt*/
2244 ctls.salesrep_id,                              /* salesrep_id */
2245 '|| l_tax_table_fragment ||'.inventory_item_id,  /* inventory_item_id */
2246 '|| l_tax_table_fragment ||'.memo_line_id,      /* memo_line_id */';
2247 
2248 
2249     ELSE  -- not based on salesreps...
2250 
2251         l_salesrep_attributes1 := CRLF ||
2252 'to_number(''''),                   /* cust_trx_line_salesrep_id */
2253 decode(' || l_rule_fragment || ',
2254         NULL, ' || l_amount_fragment || ',
2255         to_number('''')),                       /* line_amount */
2256 decode(' || l_rule_fragment || ',
2257         NULL, decode(' || l_base_min_acc_unit || ',
2258                      NULL, round((' || l_amount_fragment || ' *
2259                                    nvl(ct.exchange_rate, 1)),
2260                                    ' || l_base_precision || '),
2261                      round((' || l_amount_fragment || ' *
2262                              nvl(ct.exchange_rate, 1)) /
2263                              ' || l_base_min_acc_unit || ')
2264                        * ' || l_base_min_acc_unit || '
2265                     ),
2266         to_number('''')),             /* accounted_line_amount */
2267 round(' || l_decode_fragment || ' 100, 4),    /* percent */
2268 decode(' || l_rule_fragment || ',
2269         NULL, ' || l_amount_fragment || ',
2270         to_number('''') ),                       /* amount */
2271 decode(' || l_rule_fragment || ',
2272         NULL, decode( ' || l_base_min_acc_unit || ',
2273                       NULL, round ((' || l_amount_fragment || ' *
2274                                      nvl(ct.exchange_rate, 1)),
2275                                    ' || l_base_precision || '),
2276                       round((' || l_amount_fragment || ' *
2277                                 nvl(ct.exchange_rate, 1)) /
2278                                 ' || l_base_min_acc_unit || ' )  *
2279                         ' || l_base_min_acc_unit || '
2280                     ),
2281         to_number('''')),                        /* acctd_amt */';
2282 
2283 
2284         l_salesrep_attributes2 := CRLF ||
2285 'nvl(ctl.default_ussgl_transaction_code,
2286     ct.default_ussgl_transaction_code),     /* ussgl_trx_code */
2287 nvl(ctl.default_ussgl_trx_code_context,
2288     ct.default_ussgl_trx_code_context), /* ussgl_trx_code_cntxt*/
2289 ct.primary_salesrep_id,                        /* salesrep_id */
2290 '|| l_tax_table_fragment ||'.inventory_item_id, /* inventory_item_id */
2291 '|| l_tax_table_fragment || '.memo_line_id,     /* memo_line_id */';
2292 
2293     END IF; -- if based on salesreps
2294 
2295     debug( '  len(l_salesrep_attributes1)=' ||
2296            to_char(LENGTHB(l_salesrep_attributes1)),
2297            MSG_LEVEL_DEBUG );
2298     debug( '  len(l_salesrep_attributes2)=' ||
2299            to_char(LENGTHB(l_salesrep_attributes2)),
2300            MSG_LEVEL_DEBUG );
2301 
2302 
2303     ------------------------------------------------------------------------
2304     -- Construct gl_date attribute string
2305     ------------------------------------------------------------------------
2306     debug('  Construct gl_date attribute string', MSG_LEVEL_DEVELOP);
2307 
2308     IF( p_gl_date IS NULL ) THEN
2309         /* 5590182 */
2310         /* 5921925 - removed request_id > 0 condition */
2311         IF( p_account_class = REC AND p_request_id IS NOT NULL)
2312         THEN
2313 
2314             l_gl_date_attribute := CRLF ||
2315 'to_char(ril.gl_date, ''J''),                         /* gl_date */';
2316 
2317         ELSE
2318             IF( p_account_class = REC AND
2319                 p_request_id IS NULL)
2320             THEN
2321 
2322                 l_gl_date_attribute := CRLF ||
2323 'to_char(rgd.gl_date, ''J''),                         /* gl_date */';
2324 
2325             ELSE
2326 
2327                 l_gl_date_attribute := CRLF ||
2328 'decode('|| l_rule_fragment ||',
2329        NULL, to_char(rgd.gl_date, ''J''),
2330        '''' ),                                 /* gl_date */';
2331 
2332             END IF;
2333         END IF;
2334     ELSE  -- p_gl_date NOT NULL
2335         IF( p_account_class = REC ) THEN
2336 
2337             l_gl_date_attribute := CRLF ||
2338 'to_char(nvl(rgd.gl_date, :gl_date), ''J''), /* gl_date */';
2339 
2340         ELSE
2341             l_gl_date_attribute := CRLF ||
2342 'decode('|| l_rule_fragment ||',
2343         NULL, to_char(nvl(rgd.gl_date, :gl_date), ''J''),
2344         '''' ),                                 /* gl_date */';
2345 
2346         END IF;
2347 
2348 
2349     END IF;
2350 
2351     ------------------------------------------------------------------------
2352     -- Construct original_gl_date attribute string
2353     ------------------------------------------------------------------------
2354     debug('  Construct original_gl_date attribute string',
2355           MSG_LEVEL_DEVELOP);
2356 
2357     l_orig_gl_date_attribute := CRLF ||
2358 'decode( :original_gl_date,
2359         NULL, decode(' || l_rule_fragment || ',
2360                      NULL, to_char(rgd.original_gl_date, ''J''),
2361                             '''' ),
2362               decode(' || l_rule_fragment || ',
2363                       NULL, to_char(nvl(rgd.original_gl_date,
2364                               :original_gl_date), ''J''),
2365                             '''' )
2366      ),                              /* orig_gl_date */';
2367 
2368 
2369     ------------------------------------------------------------------------
2370     -- Construct table strings
2371     ------------------------------------------------------------------------
2372     debug('  tables', MSG_LEVEL_DEVELOP);
2373 
2374     IF( p_account_class = REC
2375         AND p_gl_date IS NULL
2376         AND p_request_id IS NOT NULL)
2377     THEN
2378         /* 5921925 - modified gl_date logic for invoice API */
2379         IF  p_request_id > 0
2380         THEN
2381            l_interface_lines_table := CRLF ||'ra_interface_lines_gt ril,';
2382         ELSE
2383            l_interface_lines_table := CRLF ||'ar_trx_header_gt ril,';
2384         END IF;
2385     END IF;
2386 
2387     IF( p_profile_info.use_inv_acct_for_cm_flag = 'Y'
2388         AND p_account_class = REC
2389         AND p_code_combination_id IS NULL ) THEN
2390 
2391         l_inv_gl_dist_table := CRLF ||'ra_cust_trx_line_gl_dist lgd_inv_rec,';
2392 
2393     END IF;
2394 
2395     IF( l_based_on_salesrep_flag ) THEN
2396 
2397         l_salesreps_table := CRLF ||'ra_cust_trx_line_salesreps ctls,';
2398 
2399     END IF;
2400 
2401     IF( p_account_class in ( TAX, FREIGHT ) )  THEN
2402 
2403      /* 4558268 - Removed tax tables */
2404         l_tax_table := CRLF ||
2405 'ra_customer_trx_lines ctl_line,';
2406 
2407     END IF;
2408 
2409     IF( p_account_class = REC )  THEN
2410 
2411         l_gl_dist_table := CRLF ||'ra_cust_trx_line_gl_dist lgd,';
2412 
2413     END IF;
2414 
2415     ------------------------------------------------------------------------
2416     -- Construct predicates
2417     ------------------------------------------------------------------------
2418 
2419     ------------------------------------------------------------------------
2420     -- Prevent AutoAccounting from creating records that should
2421     -- be created by the credit memo module.
2422     ------------------------------------------------------------------------
2423 
2424     debug('  Prevent AutoAccounting from creating records', MSG_LEVEL_DEVELOP);
2425 
2426     IF( p_profile_info.use_inv_acct_for_cm_flag = 'Y'
2427         AND p_account_class <> REC ) THEN
2428 
2429         l_cm_module_pred := CRLF ||
2430 '          /* Prevent AutoAccounting from creating records that should
2431              be created by the credit memo module. */
2432 AND        (ct.previous_customer_trx_id    is null';
2433 
2434         IF( p_code_combination_id IS NOT NULL
2435             AND p_account_class = FREIGHT ) THEN
2436 
2437             l_cm_module_pred := l_cm_module_pred || '
2438             or (ct.invoicing_rule_id is null)';
2439 
2440         END IF;
2441 
2442         l_cm_module_pred := l_cm_module_pred || ')';
2443 
2444     END IF;
2445 
2446     ------------------------------------------------------------------------
2447     -- Prevent duplicate records from being created
2448     ------------------------------------------------------------------------
2449     debug('  Prevent duplicate records from being created', MSG_LEVEL_DEVELOP);
2450 
2451     IF( p_account_class = REC ) THEN
2452 
2453         l_prevent_dup_rec_pred := CRLF ||
2454 '           /* prevent duplicate records from being created */
2455 AND        ct.customer_trx_id             = lgd.customer_trx_id(+)
2456 AND        ''REC''                          = lgd.account_class(+)
2457 AND        decode(ct.invoicing_rule_id,
2458                   NULL, ''N'',
2459                         ''Y'' )             = lgd.account_set_flag(+)
2460 AND        lgd.customer_trx_id            is null';
2461 
2462     ELSE
2463         l_prevent_dup_rec_pred := CRLF ||
2464 '           /* prevent duplicate records from being created */
2465 AND        not exists
2466                (SELECT ';
2467 -- bug 7557904
2468 IF( p_request_id IS NOT NULL ) THEN
2469 	l_prevent_dup_rec_pred := l_prevent_dup_rec_pred || '   /*+ INDEX (lgd RA_CUST_TRX_LINE_GL_DIST_N10)*/ ';
2470 ELSE
2471         l_prevent_dup_rec_pred := l_prevent_dup_rec_pred || '   /*+ INDEX (lgd RA_CUST_TRX_LINE_GL_DIST_N1)*/ ';
2472 END IF;
2473 
2474 l_prevent_dup_rec_pred := l_prevent_dup_rec_pred || '
2475                   ''distribution exists''
2476                 FROM   ra_cust_trx_line_gl_dist lgd
2477                 WHERE  ctl.customer_trx_id      = lgd.customer_trx_id
2478                 AND    ctl.customer_trx_line_id = lgd.customer_trx_line_id ';
2479 
2480 
2481         IF( p_request_id IS NOT NULL ) THEN
2482             l_prevent_dup_rec_pred := l_prevent_dup_rec_pred || '
2483                 and    lgd.request_id =
2484                          :request_id1 ';
2485         END IF;
2486 
2487 
2488         IF( p_cust_trx_line_salesrep_id IS NOT NULL ) THEN
2489             l_prevent_dup_rec_pred := l_prevent_dup_rec_pred || '
2490                 and    lgd.cust_trx_line_salesrep_id  =
2491                          :cust_trx_line_salesrep_id ';
2492         END IF;
2493 
2494         l_prevent_dup_rec_pred := l_prevent_dup_rec_pred || '
2495                 and    '''|| p_account_class ||'''    = lgd.account_class
2496                 and    decode(ct.invoicing_rule_id,
2497                               NULL, ''N'',
2498                               ''Y'' )             = lgd.account_set_flag
2499                )';
2500 
2501     END IF;
2502 
2503     ------------------------------------------------------------------------
2504     -- Create Tax predicate
2505     ------------------------------------------------------------------------
2506     debug('  Create Tax predicate', MSG_LEVEL_DEVELOP);
2507 
2508     IF( p_account_class in (TAX, FREIGHT) ) THEN
2509 
2510         /* 4558268 - Removed joins for tax tables */
2511 
2512         l_tax_pred := CRLF ||
2513 'AND        ctl.link_to_cust_trx_line_id   =
2514                                             ctl_line.customer_trx_line_id(+)';
2515 
2516     END IF;
2517 
2518     ------------------------------------------------------------------------
2519     -- Create Suspense predicate
2520     ------------------------------------------------------------------------
2521     debug('  Create suspense predicate', MSG_LEVEL_DEVELOP);
2522 
2523     IF( p_account_class = SUSPENSE ) THEN
2524 
2525         l_suspense_pred := CRLF ||
2526 'AND        (ctl.extended_amount - ctl.revenue_amount) <> 0';
2527 
2528 
2529     END IF;
2530 
2531     ------------------------------------------------------------------------
2532     -- Create predicate that is based on salesrep flag
2533     ------------------------------------------------------------------------
2534     debug('  Create predicate that is based on salesrep flag',
2535 		MSG_LEVEL_DEVELOP);
2536 
2537     IF( l_based_on_salesrep_flag ) THEN
2538 
2539         -- bug2963903 : Added condition for revenue_percent_split
2540         l_based_on_salesrep_pred := CRLF ||
2541 'AND        ctl.customer_trx_id           = ctls.customer_trx_id(+)
2542 AND        nvl(ctl.link_to_cust_trx_line_id,
2543                ctl.customer_trx_line_id)  = ctls.customer_trx_line_id(+)
2544 AND        (ctls.revenue_percent_split is not null
2545                 or ctls.customer_trx_line_id is null)' ;
2546 
2547     END IF;
2548 
2549     IF( p_account_class = REC
2550         AND p_gl_date IS NULL
2551         AND p_request_id IS NOT NULL)
2552     THEN
2553         IF p_request_id > 0
2554         THEN
2555 
2556      /* 5169215 - a cartesian join between ra_interface_lines and
2557           ra_customer_trx was resulting in incorrect GL_DATE on
2558           REC distributions.  This relates to bug 4483951, but the fix
2559           for that does not entirely resolve the problem */
2560         l_interface_table_pred := CRLF ||
2561 'AND        ril.rowid =  (SELECT /*+ no_unnest */ min(ril2.rowid)
2562                          FROM   ra_interface_lines_gt ril2
2563                          WHERE  ril2.customer_trx_id = ct.customer_trx_id
2564                          AND    ril2.link_to_line_id is null)
2565 AND        ril.customer_trx_id = ct.customer_trx_id ';
2566 
2567         ELSE
2568         /* 5921925 - invoice api gl_date issues */
2569         l_interface_table_pred := CRLF ||
2570 'AND        ril.customer_trx_id =  ct.customer_trx_id';
2571         END IF;
2572     END IF;
2573 
2574     ------------------------------------------------------------------------
2575     -- request_id
2576     ------------------------------------------------------------------------
2577     IF( p_request_id IS NOT NULL ) THEN
2578 
2579     /* Bug 2116064 - Added 'is not null' condition */
2580         l_request_id_pred := CRLF ||
2581 'AND        ct.request_id                = :request_id';
2582 
2583         l_request_id_pred := l_request_id_pred || CRLF ||
2584 'AND        ctl.request_id (+) = :request_id';  -- 7039838
2585 
2586         l_request_id_pred := l_request_id_pred || CRLF ||
2587 'AND        ct.request_id is not null';
2588 
2589     END IF;
2590 
2591     ------------------------------------------------------------------------
2592     -- customer_trx_id
2593     ------------------------------------------------------------------------
2594     IF( p_customer_trx_id IS NOT NULL ) THEN
2595 
2596         l_trx_id_pred := CRLF ||
2597 'AND        ct.customer_trx_id             = :customer_trx_id';
2598 
2599     END IF;
2600 
2601     ------------------------------------------------------------------------
2602     -- customer_trx_line_id
2603     ------------------------------------------------------------------------
2604     IF( p_customer_trx_line_id IS NOT NULL ) THEN
2605 
2606         l_line_id_pred := CRLF ||
2607 'AND        (ctl.customer_trx_line_id       =  :customer_trx_line_id
2608             OR
2609             ctl.link_to_cust_trx_line_id   = :customer_trx_line_id';
2610 
2611 /* Bug 1793936 - Creating extra UNEARN/UNBILL rows under certain circumstances
2612 
2613 --        IF( p_customer_trx_id IS NOT NULL
2614 --            OR p_request_id IS NOT NULL ) THEN
2615 
2616 --            l_line_id_pred := l_line_id_pred || '
2617 -- OR            ctl.link_to_cust_trx_line_id   is null';
2618 --
2619 --        END IF;
2620 */
2621         l_line_id_pred := l_line_id_pred || ')';
2622 
2623     END IF;
2624 
2625     IF( p_cust_trx_line_salesrep_id IS NOT NULL
2626         AND l_based_on_salesrep_flag ) THEN
2627 
2628         l_line_salesrep_id_pred := CRLF ||
2629 'AND        ctls.cust_trx_line_salesrep_id  = :cust_trx_line_salesrep_id';
2630 
2631     END IF;
2632 
2633 
2634     ------------------------------------------------------------------------
2635     -- IF   Use Invoice Accounting is Yes,
2636     -- AND  the account class is Receivable,
2637     -- AND  no CCID was passed in
2638     -- THEN join to the invoice's receivable record for credit memos
2639     --      to get the invoice's receivable CCID
2640     ------------------------------------------------------------------------
2641     IF( p_profile_info.use_inv_acct_for_cm_flag = 'Y'
2642         AND p_account_class = REC
2643         AND p_code_combination_id IS NULL ) THEN
2644 
2645         l_inv_rec_pred := CRLF ||
2646 '        /* Join to the invoice receivable record to get the CCID */
2647 AND        ct.previous_customer_trx_id    = lgd_inv_rec.customer_trx_id(+)
2648 AND        lgd_inv_rec.account_class(+)   = ''REC''
2649 AND        lgd_inv_rec.latest_rec_flag(+) = ''Y''';
2650 
2651     END IF;
2652 
2653     ------------------------------------------------------------------------
2654     -- Put it all together
2655     ------------------------------------------------------------------------
2656     debug('  Put it all together ', MSG_LEVEL_DEVELOP);
2657 
2658     /* 7039838 - changed hints for FT tuning */
2659     IF p_request_id IS NOT NULL
2660     THEN
2661        l_select_stmt :=
2662            'SELECT /*+ leading(ct) index(ct,RA_CUSTOMER_TRX_N15) index(ctl,RA_CUSTOMER_TRX_LINES_N4) use_hash(ctl) */ ' || CRLF;
2663     ELSE
2664        l_select_stmt := 'SELECT' || CRLF;
2665     END IF;
2666 
2667     l_select_stmt :=  l_select_stmt ||
2668 'ct.customer_trx_id,                        /* customer_trx_id */
2669 ctl.customer_trx_line_id,             /* customer_trx_line_id */'
2670 || l_salesrep_attributes1
2671 || CRLF ||
2672 '''' || p_account_class || ''',                   /* account class */
2673 decode('|| l_rule_fragment ||',
2674        NULL, ''N'',
2675        ''Y'' ),                       /* account_set_flag */
2676 ct.cust_trx_type_id,                      /* cust_trx_type_id */
2677 decode(ct.invoicing_rule_id,
2678        -3, ''Y'',
2679        ''N''),                       /* allow_not_open_flag */
2680 to_char(''''),                         /* concatenated segments */'
2681 || CRLF
2682 || l_ccid_fragment ||',        /* code_combination_id */'
2683 || l_gl_date_attribute
2684 || l_orig_gl_date_attribute
2685 || l_salesrep_attributes2
2686 || l_tax_attribute
2687 || 'ct.bill_to_site_use_id, /* Billing site id */ '
2688 || l_tax_table_fragment ||'.warehouse_id /* Warehouse id */ '
2689 || ', ctl.link_to_cust_trx_line_id /* 1651593 - tax errors */'
2690 || CRLF
2691 ||'FROM'
2692 || l_interface_lines_table
2693 || l_inv_gl_dist_table
2694 || l_salesreps_table
2695 || l_tax_table
2696 || CRLF ||
2697 'fnd_currencies fc_foreign,'
2698 || l_gl_dist_table
2699 || CRLF ||
2700 'ra_cust_trx_line_gl_dist rgd,
2701 ra_customer_trx_lines ctl,
2702 ra_customer_trx ct
2703 WHERE      ct.customer_trx_id             = ctl.customer_trx_id(+)
2704 AND        ct.invoice_currency_code       = fc_foreign.currency_code'
2705 || l_cm_module_pred
2706 || l_prevent_dup_rec_pred
2707 || CRLF ||
2708 'AND        ct.customer_trx_id             = rgd.customer_trx_id(+)
2709 AND        ''REC''                          = rgd.account_class(+)
2710 AND        ''N''                            = rgd.account_set_flag(+)
2711 AND        ctl.line_type '|| l_line_type_fragment
2712 || l_tax_pred
2713 || CRLF ||
2714 'and        nvl(ct.invoicing_rule_id,
2715               -10)                      '|| l_rule_id_fragment
2716 || l_suspense_pred
2717 || l_based_on_salesrep_pred
2718 || l_interface_table_pred
2719 || l_request_id_pred
2720 || l_trx_id_pred
2721 || l_line_id_pred
2722 || l_line_salesrep_id_pred
2723 || l_inv_rec_pred ;
2724 
2725     debug( l_select_stmt, MSG_LEVEL_DEBUG );
2726     debug( '  len(l_select_stmt)=' ||
2727                         to_char(LENGTHB(l_select_stmt)), MSG_LEVEL_DEBUG );
2728 
2729     print_fcn_label( 'arp_auto_accounting.build_select_sql()-' );
2730     RETURN l_select_stmt;
2731 
2732 
2733 
2734 EXCEPTION
2735     WHEN OTHERS THEN
2736         debug('EXCEPTION: arp_auto_accounting.build_select_sql()',
2737 	      MSG_LEVEL_BASIC);
2738         debug(SQLERRM);
2739         RAISE;
2740 END build_select_sql;
2741 
2742 
2743 ----------------------------------------------------------------------------
2744 --
2745 -- FUNCTION NAME:  add_segments_to_cache
2746 --
2747 -- DECSRIPTION:
2748 --   Addes the segment values for a given ccid to the segment value caches.
2749 --
2750 -- ARGUMENTS:
2751 --      IN:
2752 --        ccid
2753 --        segment_number (from the column name 'SEGMENTxx')
2754 --
2755 --      IN/OUT:
2756 --
2757 --      OUT:
2758 --        p_desired_segment
2759 --
2760 -- RETURNS:
2761 --      segment value.  NULL if data not found.
2762 --
2763 -- NOTES:
2764 --   exception raised if no rows found
2765 --   I did not use record to contain these table values in order to be
2766 --   backward compatible with earlier versions of PL/SQL that did not allow
2767 --   tables of records.
2768 --
2769 -- HISTORY:
2770 --
2771 --
2772 PROCEDURE add_segments_to_cache( p_ccid             IN binary_integer,
2773                                  p_segment_number   IN binary_integer,
2774                                  p_desired_segment OUT NOCOPY varchar2) IS
2775 
2776   l_segment1    varchar2(30);
2777   l_segment2    varchar2(30);
2778   l_segment3    varchar2(30);
2779   l_segment4    varchar2(30);
2780   l_segment5    varchar2(30);
2781   l_segment6    varchar2(30);
2782   l_segment7    varchar2(30);
2783   l_segment8    varchar2(30);
2784   l_segment9    varchar2(30);
2785   l_segment10   varchar2(30);
2786   l_segment11   varchar2(30);
2787   l_segment12   varchar2(30);
2788   l_segment13   varchar2(30);
2789   l_segment14   varchar2(30);
2790   l_segment15   varchar2(30);
2791   l_segment16   varchar2(30);
2792   l_segment17   varchar2(30);
2793   l_segment18   varchar2(30);
2794   l_segment19   varchar2(30);
2795   l_segment20   varchar2(30);
2796   l_segment21   varchar2(30);
2797   l_segment22   varchar2(30);
2798   l_segment23   varchar2(30);
2799   l_segment24   varchar2(30);
2800   l_segment25   varchar2(30);
2801   l_segment26   varchar2(30);
2802   l_segment27   varchar2(30);
2803   l_segment28   varchar2(30);
2804   l_segment29   varchar2(30);
2805   l_segment30   varchar2(30);
2806 
2807 BEGIN
2808 
2809     print_fcn_label2( 'arp_auto_accounting.add_segments_to_cache()+' );
2810 
2811     SELECT segment1,
2812            segment2,
2813            segment3,
2814            segment4,
2815            segment5,
2816            segment6,
2817            segment7,
2818            segment8,
2819            segment9,
2820            segment10,
2821            segment11,
2822            segment12,
2823            segment13,
2824            segment14,
2825            segment15,
2826            segment16,
2827            segment17,
2828            segment18,
2829            segment19,
2830            segment20,
2831            segment21,
2832            segment22,
2833            segment23,
2834            segment24,
2835            segment25,
2836            segment26,
2837            segment27,
2838            segment28,
2839            segment29,
2840            segment30,
2841     DECODE(p_segment_number,
2842            1, segment1,
2843            2, segment2,
2844            3, segment3,
2845            4, segment4,
2846            5, segment5,
2847            6, segment6,
2848            7, segment7,
2849            8, segment8,
2850            9, segment9,
2851            10, segment10,
2852            11, segment11,
2853            12, segment12,
2854            13, segment13,
2855            14, segment14,
2856            15, segment15,
2857            16, segment16,
2858            17, segment17,
2859            18, segment18,
2860            19, segment19,
2861            20, segment20,
2862            21, segment21,
2863            22, segment22,
2864            23, segment23,
2865            24, segment24,
2866            25, segment25,
2867            26, segment26,
2868            27, segment27,
2869            28, segment28,
2870            29, segment29,
2871            30, segment30, null)
2872     INTO   l_segment1,
2873            l_segment2,
2874            l_segment3,
2875            l_segment4,
2876            l_segment5,
2877            l_segment6,
2878            l_segment7,
2879            l_segment8,
2880            l_segment9,
2881            l_segment10,
2882            l_segment11,
2883            l_segment12,
2884            l_segment13,
2885            l_segment14,
2886            l_segment15,
2887            l_segment16,
2888            l_segment17,
2889            l_segment18,
2890            l_segment19,
2891            l_segment20,
2892            l_segment21,
2893            l_segment22,
2894            l_segment23,
2895            l_segment24,
2896            l_segment25,
2897            l_segment26,
2898            l_segment27,
2899            l_segment28,
2900            l_segment29,
2901            l_segment30,
2902            p_desired_segment
2903     FROM   gl_code_combinations
2904     WHERE  code_combination_id = p_ccid;
2905 
2906 
2907   /*--------------------------------------------------+
2908    |  Add the selected segments to the segment cache  |
2909    |  only if the cache is not already full.          |
2910    +--------------------------------------------------*/
2911 
2912    IF ( segment1_cache.count <= MAX_SEGMENT_CACHE_SIZE )
2913    THEN
2914          segment1_cache(p_ccid) := l_segment1;
2915          segment2_cache(p_ccid) := l_segment2;
2916          segment3_cache(p_ccid) := l_segment3;
2917          segment4_cache(p_ccid) := l_segment4;
2918          segment5_cache(p_ccid) := l_segment5;
2919          segment6_cache(p_ccid) := l_segment6;
2920          segment7_cache(p_ccid) := l_segment7;
2921          segment8_cache(p_ccid) := l_segment8;
2922          segment9_cache(p_ccid) := l_segment9;
2923          segment10_cache(p_ccid) := l_segment10;
2924          segment11_cache(p_ccid) := l_segment11;
2925          segment12_cache(p_ccid) := l_segment12;
2926          segment13_cache(p_ccid) := l_segment13;
2927          segment14_cache(p_ccid) := l_segment14;
2928          segment15_cache(p_ccid) := l_segment15;
2929          segment16_cache(p_ccid) := l_segment16;
2930          segment17_cache(p_ccid) := l_segment17;
2931          segment18_cache(p_ccid) := l_segment18;
2932          segment19_cache(p_ccid) := l_segment19;
2933          segment20_cache(p_ccid) := l_segment20;
2934          segment21_cache(p_ccid) := l_segment21;
2935          segment22_cache(p_ccid) := l_segment22;
2936          segment23_cache(p_ccid) := l_segment23;
2937          segment24_cache(p_ccid) := l_segment24;
2938          segment25_cache(p_ccid) := l_segment25;
2939          segment26_cache(p_ccid) := l_segment26;
2940          segment27_cache(p_ccid) := l_segment27;
2941          segment28_cache(p_ccid) := l_segment28;
2942          segment29_cache(p_ccid) := l_segment29;
2943          segment30_cache(p_ccid) := l_segment30;
2944    END IF;
2945 
2946    print_fcn_label2( 'arp_auto_accounting.add_segments_to_cache()-' );
2947 
2948    EXCEPTION
2949      WHEN OTHERS THEN
2950          debug( 'EXCEPTION: arp_auto_accounting.add_segments_to_cache()',
2951 	        MSG_LEVEL_BASIC );
2952          debug(SQLERRM, MSG_LEVEL_BASIC);
2953          RAISE;
2954 
2955 END;
2956 
2957 
2958 ----------------------------------------------------------------------------
2959 --
2960 -- FUNCTION NAME:  get_segment_from_glcc
2961 --
2962 -- DECSRIPTION:
2963 --   Retrieves a GL code combination segment for a ccid
2964 --
2965 -- ARGUMENTS:
2966 --      IN:
2967 --        ccid
2968 --        segment_number (from the column name 'SEGMENTxx')
2969 --
2970 --      IN/OUT:
2971 --
2972 --      OUT:
2973 --
2974 -- RETURNS:
2975 --      segment value.  NULL if data not found.
2976 --
2977 -- NOTES:
2978 --   exception raised if no rows found
2979 --
2980 -- HISTORY:
2981 --
2982 --
2983 FUNCTION get_segment_from_glcc( p_ccid 		 IN BINARY_INTEGER,
2984                                 p_segment_number IN BINARY_INTEGER )
2985   RETURN VARCHAR2 IS
2986 
2987     l_segment_value VARCHAR2(25);
2988   i        BINARY_INTEGER := 0;
2989   l_bool   boolean;
2990   l_ccid   BINARY_INTEGER;
2991 
2992   l_desired_segment     varchar2(30);
2993 
2994 BEGIN
2995 
2996      print_fcn_label2( 'arp_auto_accounting.get_segment_from_glcc()+' );
2997 
2998   BEGIN
2999 
3000            if (p_segment_number = 1)
3001            then return(segment1_cache(p_ccid));
3002            elsif (p_segment_number = 2)
3003               then return(segment2_cache(p_ccid));
3004            elsif (p_segment_number = 3)
3005               then return(segment3_cache(p_ccid));
3006            elsif (p_segment_number = 4)
3007               then return(segment4_cache(p_ccid));
3008            elsif (p_segment_number = 5)
3009               then return(segment5_cache(p_ccid));
3010            elsif (p_segment_number = 6)
3011               then return(segment6_cache(p_ccid));
3012            elsif (p_segment_number = 7)
3013               then return(segment7_cache(p_ccid));
3014            elsif (p_segment_number = 8)
3015               then return(segment8_cache(p_ccid));
3016            elsif (p_segment_number = 9)
3017               then return(segment9_cache(p_ccid));
3018            elsif (p_segment_number = 10)
3019               then return(segment10_cache(p_ccid));
3020            elsif (p_segment_number = 11)
3021               then return(segment11_cache(p_ccid));
3022            elsif (p_segment_number = 12)
3023               then return(segment12_cache(p_ccid));
3024            elsif (p_segment_number = 13)
3025               then return(segment13_cache(p_ccid));
3026            elsif (p_segment_number = 14)
3027               then return(segment14_cache(p_ccid));
3028            elsif (p_segment_number = 15)
3029               then return(segment15_cache(p_ccid));
3030            elsif (p_segment_number = 16)
3031               then return(segment16_cache(p_ccid));
3032            elsif (p_segment_number = 17)
3033               then return(segment17_cache(p_ccid));
3034            elsif (p_segment_number = 18)
3035               then return(segment18_cache(p_ccid));
3036            elsif (p_segment_number = 19)
3037               then return(segment19_cache(p_ccid));
3038            elsif (p_segment_number = 20)
3039               then return(segment20_cache(p_ccid));
3040            elsif (p_segment_number = 21)
3041               then return(segment21_cache(p_ccid));
3042            elsif (p_segment_number = 22)
3043               then return(segment22_cache(p_ccid));
3044            elsif (p_segment_number = 23)
3045               then return(segment23_cache(p_ccid));
3046            elsif (p_segment_number = 24)
3047               then return(segment24_cache(p_ccid));
3048            elsif (p_segment_number = 25)
3049               then return(segment25_cache(p_ccid));
3050            elsif (p_segment_number = 26)
3051               then return(segment26_cache(p_ccid));
3052            elsif (p_segment_number = 27)
3053               then return(segment27_cache(p_ccid));
3054            elsif (p_segment_number = 28)
3055               then return(segment28_cache(p_ccid));
3056            elsif (p_segment_number = 29)
3057               then return(segment29_cache(p_ccid));
3058            elsif (p_segment_number = 30)
3059               then return(segment30_cache(p_ccid));
3060            end if;
3061 
3062 
3063 EXCEPTION
3064   WHEN NO_DATA_FOUND
3065    THEN
3066 
3067      /*--------------------------------------------------------------+
3068       |  The ccid was not in the cache.                              |
3069       |  Select the segments from gl_code_combinations and add them  |
3070       |   to the cache if it is not already full.                    |
3071       +--------------------------------------------------------------*/
3072 
3073       add_segments_to_cache(p_ccid, p_segment_number,l_desired_segment);
3074 
3075       debug('getting segment ' || p_segment_number ||
3076             'for ccid ' || p_ccid ||
3077             ' from gl_code_combinations', MSG_LEVEL_DEBUG);
3078 
3079       print_fcn_label2( 'arp_auto_accounting.get_segment_from_glcc()-' );
3080       RETURN(l_desired_segment);
3081 
3082    WHEN OTHERS THEN RAISE;
3083 
3084  END;
3085 
3086 
3087 EXCEPTION
3088     WHEN NO_DATA_FOUND THEN
3089 	debug('arp_auto_accounting.get_segment_from_glcc(): no data found',
3090 	      MSG_LEVEL_DEBUG);
3091         RETURN NULL;
3092     WHEN OTHERS THEN
3093 /*        debug('EXCEPTION: arp_auto_accounting.get_segment_from_glcc('
3094               || to_char(p_ccid) || ', '
3095               || to_char(p_segment_number) || ')', MSG_LEVEL_BASIC); */
3096         debug(SQLERRM, MSG_LEVEL_BASIC);
3097         RAISE;
3098 END get_segment_from_glcc;
3099 
3100 ----------------------------------------------------------------------------
3101 --
3102 -- FUNCTION NAME:  Get_Ccid_From_Cache
3103 --
3104 -- DECSRIPTION:
3105 --   Retrieves valid GL code combination from the cache or from the AOL
3106 --   API routine if the value is not yet in the cache.
3107 --
3108 -- ARGUMENTS:
3109 --      IN:
3110 --        p_system_info
3111 --        p_concat_segments
3112 --        p_segment_table
3113 --        p_segment_cnt
3114 --        p_account_class
3115 --
3116 --      IN/OUT:
3117 --
3118 --      OUT:
3119 --        p_result
3120 --
3121 -- RETURNS:
3122 --        ccid
3123 --
3124 -- NOTES:
3125 --
3126 -- HISTORY:
3127 /*   21-APR-03   MRAYMOND   2142306   Rewrote this routine to use hash indexes
3128                                       in place of linear scans.  If a collision
3129                                       occurs, we will log the second (and
3130                                       successive colliding accounts in a linear
3131                                       table.
3132 
3133                                       Also note that this version stores all
3134                                       ccids (REV, REC, etc) in a single table
3135                                       rather than having separate tables for
3136                                       each.
3137 */
3138 
3139 FUNCTION Get_Ccid_From_Cache( p_system_info 	 IN
3140                                  arp_trx_global.system_info_rec_type,
3141                               p_concat_segments  IN  varchar2,
3142                               p_segment_table    IN  fnd_flex_ext.SegmentArray,
3143                               p_segment_cnt      IN  BINARY_INTEGER,
3144                               p_account_class    IN
3145                                  ra_cust_trx_line_gl_dist.account_class%type,
3146                               p_result           OUT NOCOPY BOOLEAN
3147                             ) RETURN BINARY_INTEGER IS
3148 
3149   hash_value        NUMBER;
3150   tab_indx          BINARY_INTEGER := 0;
3151   found             BOOLEAN := FALSE;
3152   valid             BOOLEAN := FALSE;
3153   l_ccid            BINARY_INTEGER;
3154 
3155 BEGIN
3156 
3157    print_fcn_label2( 'arp_auto_accounting.get_ccid_from_cache()+' );
3158 
3159   /*----------------------------------------------------------------+
3160    |  Rewritten for bug 2142306 (23-Jan-02) - performance issues    |
3161    |                                                                |
3162    |  Search the cache for the concantenated segments.              |
3163    |  Return the ccid if it is in the cache.                        |
3164    |                                                                |
3165    |  If not found in cache, search the linear table (where ccid's  |
3166    |  will go if collision on the hash table occurs).               |
3167    |                                                                |
3168    |  A NO_DATA_FOUND exception will be generated if the segments   |
3169    |  are not found in either table. This will result in a call to  |
3170    |  the flexfield api to get the ccid and puts it in the cache    |
3171    |  table if no collision  occurs (and the cache is not already   |
3172    |  full) or the linear table if the hash value already exists in |
3173    |  the hash table (and its not full).  The number of rows in the |
3174    |  linear table should be very small!                            |
3175    +----------------------------------------------------------------*/
3176 
3177    hash_value := dbms_utility.get_hash_value(p_concat_segments,
3178                      HASH_START, HASH_MAX);
3179 
3180 
3181    /* The following flow looks like this:
3182 
3183    IF hash exists
3184      IF concatenated segs at hash are same
3185        IF date is valid
3186          Use ccid from hash table
3187        ENDIF
3188      ELSE concatenated segs not same (collision)
3189        LOOP through linear table
3190          IF concatenated segs at linear are same
3191            IF date is valid
3192               Use ccid from linear table
3193            ENDIF
3194          ELSE
3195            make room in linear table for new ccid
3196          ENDIF
3197        END LOOP
3198      ENDIF
3199    ENDIF
3200 
3201    IF the hash for the segments is not found in the hash table,
3202    then it is a new ccid and it is added to the table.
3203    */
3204 
3205 
3206    IF autoacc_hash_seg_cache.exists(hash_value) THEN
3207 
3208        IF autoacc_hash_seg_cache(hash_value) = p_concat_segments THEN
3209 
3210             found := TRUE;
3211 
3212 	    IF (validation_date BETWEEN autoacc_hash_st_date_cache(hash_value)
3213                                   and autoacc_hash_end_date_cache(hash_value))
3214             THEN
3215 	        l_ccid := autoacc_hash_id_cache(hash_value);
3216 	        valid := TRUE;
3217             END IF;
3218 
3219        ELSE     --- collision has occurred
3220             tab_indx := 1;  -- start at top of linear table and search for match
3221 
3222             WHILE ((tab_indx <= tab_size) AND (not found)) LOOP
3223 
3224 	      IF autoacc_lin_seg_cache(tab_indx) = p_concat_segments THEN
3225 
3226                  found := TRUE;
3227 
3228 	         IF (validation_date BETWEEN autoacc_lin_st_date_cache(tab_indx)
3229                                        and autoacc_lin_end_date_cache(tab_indx))
3230                  THEN
3231 	            l_ccid := autoacc_lin_id_cache(tab_indx);
3232                     valid := TRUE;
3233 	         END IF;
3234 
3235               ELSE
3236 
3237                  tab_indx := tab_indx + 1;
3238 
3239 	      END IF;
3240 
3241             END LOOP;
3242 
3243        END IF;
3244 
3245    END IF;
3246 
3247       /*-------------------------------------------------+
3248        |  Return the ccid if it was found in the cache.  |
3249        +-------------------------------------------------*/
3250        IF FOUND THEN
3251           debug('found ccid ' || l_ccid  || ' for concatenated segs: ' ||
3252                      p_concat_segments || ' in the cache', MSG_LEVEL_DEBUG);
3253           IF VALID THEN
3254              p_result := TRUE;
3255              print_fcn_label2( 'arp_auto_accounting.get_ccid_from_cache()-' );
3256              RETURN( l_ccid );
3257           ELSE
3258              debug('  ccid ' || l_ccid || ' not valid for date ' ||
3259                                            validation_date );
3260              p_result := FALSE;
3261              print_fcn_label2('arp_auto_accounting.get_ccid_from_cache()-');
3262              RETURN(NULL);
3263           END IF;
3264        ELSE
3265 	 debug('Getting concatenated segs: ' ||
3266                       p_concat_segments || ' using the flexfield api',
3267                      MSG_LEVEL_DEBUG);
3268 
3269          IF (get_combination_id(
3270                         'SQLGL',
3271                         'GL#',
3272                         p_system_info.chart_of_accounts_id,
3273                         validation_date,     -- CCID validation date
3274                         p_segment_cnt,
3275                         p_segment_table,
3276                         l_ccid ) )
3277          THEN
3278 
3279              /*---------------------------------------------------+
3280               |  Add the ccid to the cache                        |
3281               |  Store in hash table if hash_value does not exist |
3282               |  Otherwise store in linear table.                 |
3283               +---------------------------------------------------*/
3284 
3285            IF autoacc_hash_seg_cache.exists(hash_value) then
3286 
3287               IF tab_size < MAX_LINEAR_CACHE_SIZE
3288               THEN
3289       	         tab_size := tab_size + 1;
3290 	         autoacc_lin_id_cache(tab_size)       := l_ccid;
3291 	         autoacc_lin_seg_cache(tab_size)      := p_concat_segments;
3292                  autoacc_lin_st_date_cache(tab_size)  :=
3293                      nvl(fnd_flex_keyval.start_date, g_min_date);
3294                  autoacc_lin_end_date_cache(tab_size) :=
3295                      nvl(fnd_flex_keyval.end_date, g_max_date);
3296               END IF;
3297        	   ELSE
3298              IF h_tab_size < MAX_HASH_CACHE_SIZE
3299              THEN
3300                 h_tab_size := h_tab_size + 1;
3301 	        autoacc_hash_id_cache(hash_value)       := l_ccid;
3302 	        autoacc_hash_seg_cache(hash_value)      := p_concat_segments;
3303 	        autoacc_hash_st_date_cache(hash_value)  :=
3304                     nvl(fnd_flex_keyval.start_date, g_min_date);
3305                 autoacc_hash_end_date_cache(hash_value) :=
3306                     nvl(fnd_flex_keyval.end_date, g_max_date);
3307              END IF;
3308 	   END IF;
3309               p_result := TRUE;
3310               print_fcn_label2( 'arp_auto_accounting.get_ccid_from_cache()-' );
3311               RETURN(l_ccid);
3312          ELSE
3313               p_result := FALSE;
3314               print_fcn_label2( 'arp_auto_accounting.get_ccid_from_cache()-' );
3315               RETURN(NULL);
3316          END IF;
3317    END IF;
3318    EXCEPTION
3319    WHEN OTHERS THEN
3320          debug( 'EXCEPTION: arp_auto_accounting.get_ccid_from_cache_cache()',
3321                 MSG_LEVEL_BASIC );
3322          debug(SQLERRM, MSG_LEVEL_BASIC);
3323          RAISE;
3324 END;
3325 
3326 ----------------------------------------------------------------------------
3327 --
3328 -- FUNCTION NAME:  search_glcc_for_ccid
3329 --
3330 -- DECSRIPTION:
3331 --   Retrieves valid GL code combination based on passed segment values.
3332 --
3333 -- ARGUMENTS:
3334 --      IN:
3335 --        p_system_info
3336 --        p_segment_table
3337 --        p_segment_cnt -- # enabled segments
3338 --        p_account_class
3339 --        p_concat_segments
3340 --
3341 --      IN/OUT:
3342 --
3343 --      OUT:
3344 --
3345 -- RETURNS:
3346 --        ccid
3347 --
3348 -- NOTES:
3349 --
3350 -- HISTORY:
3351 --   14-FEB-97  C. Tomberg  Added p_account_class and p_concat_segments params.
3352 --
3353 --
3354 FUNCTION search_glcc_for_ccid( p_system_info 	 IN
3355                                  arp_trx_global.system_info_rec_type,
3356                                p_segment_table   IN fnd_flex_ext.SegmentArray,
3357                                p_segment_cnt  	 IN BINARY_INTEGER,
3358                                p_account_class   IN
3359                                  ra_cust_trx_line_gl_dist.account_class%type,
3360                                p_concat_segments IN VARCHAR2 )
3361 
3362   RETURN BINARY_INTEGER  IS
3363 
3364     l_ignore 			INTEGER;
3365     l_ccid   			BINARY_INTEGER;
3366     l_detail_posting_flag	VARCHAR2(1);
3367     l_summary_flag		VARCHAR2(1);
3368     l_bool			BOOLEAN;
3369 
3370 BEGIN
3371 
3372     print_fcn_label2( 'arp_auto_accounting.search_glcc_for_ccid()+' );
3373 
3374     --
3375     -- part 1:  call the ccid cache or the AOL api to validate and dynamically
3376     --          insert ccid
3377     --
3378 
3379 
3380    /*------------------------------------------------------------------+
3381     |  If the  p_concat_segments or p_account_class parameters are     |
3382     |  null, do not use the cache.                                     |
3383     |  This logic exists to maintain backward compatibility with the   |
3384     |  original function spec.                                         |
3385     |                                                                  |
3386     |  Otherwise, get the ccid from the caches of already validiated   |
3387     |  code combinations.                                              |
3388     +------------------------------------------------------------------*/
3389 
3390     IF   (
3391               p_concat_segments  IS NOT NULL
3392           AND p_account_class    IS NOT NULL)
3393     THEN
3394          l_ccid := Get_Ccid_From_Cache( p_system_info,
3395                                         p_concat_segments,
3396                                         p_segment_table,
3397                                         p_segment_cnt,
3398                                         p_account_class,
3399                                         l_bool);
3400     ELSE
3401          l_bool := get_combination_id(
3402                       'SQLGL',
3403                       'GL#',
3404                       p_system_info.chart_of_accounts_id,
3405                       validation_date,     -- CCID validation date
3406                       p_segment_cnt,
3407                       p_segment_table,
3408                       l_ccid );
3409 
3410     END IF;
3411 
3412     IF( l_bool = FALSE ) THEN
3413 	------------------------------------------------------------------
3414 	-- Failed to retrieve a valid ccid or
3415 	-- unable to dynamically create a ccid
3416 	------------------------------------------------------------------
3417 	g_error_buffer := fnd_message.get;
3418         debug( g_error_buffer, MSG_LEVEL_BASIC );
3419 
3420         print_fcn_label2( 'arp_auto_accounting.search_glcc_for_ccid()-' );
3421         RETURN -1;
3422 
3423     END IF;
3424 
3425     --
3426     -- part 2: check special validation
3427     --         detail_posting_flag
3428     --         summary_flag
3429     --
3430     BEGIN
3431 /* Bug-2178723 : Caching the values of detail_posting_allowed_flag and summary_flag
3432                  in pl/sql table to avoid the high execution count  */
3433         IF pg_ar_code_comb_rec.EXISTS(l_ccid) THEN
3434               l_detail_posting_flag := pg_ar_code_comb_rec(l_ccid).detail_posting_flag;
3435               l_summary_flag        :=  pg_ar_code_comb_rec(l_ccid).summary_flag;
3436 
3437         ELSE
3438 
3439             SELECT detail_posting_allowed_flag,
3440                    summary_flag
3441             INTO   l_detail_posting_flag,
3442                    l_summary_flag
3443             FROM   gl_code_combinations
3444             WHERE  code_combination_id = l_ccid;
3445 
3446             pg_ar_code_comb_rec(l_ccid).detail_posting_flag := l_detail_posting_flag ;
3447             pg_ar_code_comb_rec(l_ccid).summary_flag := l_summary_flag ;
3448 
3449         END IF;
3450 
3451         IF( l_detail_posting_flag = NO ) THEN
3452 
3453                 g_error_buffer := MSG_FLEX_POSTING_NOT_ALLOWED;
3454                 debug( MSG_FLEX_POSTING_NOT_ALLOWED, MSG_LEVEL_BASIC);
3455                 print_fcn_label2( 'arp_auto_accounting.search_glcc_for_ccid()-' );
3456                 RETURN -1;
3457 
3458          ELSIF( l_summary_flag = YES ) THEN
3459 
3460                 g_error_buffer := MSG_FLEX_NO_PARENT_ALLOWED;
3461                 debug( MSG_FLEX_NO_PARENT_ALLOWED, MSG_LEVEL_BASIC);
3462                 print_fcn_label2( 'arp_auto_accounting.search_glcc_for_ccid()-' );
3463                 RETURN -1;
3464 
3465           END IF;
3466 
3467             print_fcn_label2( 'arp_auto_accounting.search_glcc_for_ccid()-' );
3468             RETURN l_ccid;
3469 
3470     EXCEPTION
3471 	WHEN NO_DATA_FOUND  THEN
3472             RETURN -1;
3473         WHEN OTHERS THEN
3474             debug( 'Error in binding ccid_reader', MSG_LEVEL_BASIC );
3475             debug(SQLERRM, MSG_LEVEL_BASIC);
3476             RAISE;
3477     END;
3478 
3479     print_fcn_label2( 'arp_auto_accounting.search_glcc_for_ccid()-' );
3480 
3481 EXCEPTION
3482     WHEN OTHERS THEN
3483         debug('EXCEPTION: arp_auto_accounting.search_glcc_for_ccid('
3484               || to_char(p_segment_cnt) ||')', MSG_LEVEL_BASIC);
3485         debug(SQLERRM, MSG_LEVEL_BASIC);
3486         RAISE;
3487 END search_glcc_for_ccid;
3488 
3489  /*---------------------------------------------------------------------+
3490   |  This overloaded version of the function exists to preserve         |
3491   |  backward compatibility with the original function specification.   |
3492   +---------------------------------------------------------------------*/
3493 
3494 
3495 FUNCTION search_glcc_for_ccid( p_system_info 	 IN
3496                                  arp_trx_global.system_info_rec_type,
3497                                p_segment_table   IN fnd_flex_ext.SegmentArray,
3498                                p_segment_cnt  	 IN BINARY_INTEGER )
3499          RETURN BINARY_INTEGER  IS
3500 
3501 BEGIN
3502        RETURN(
3503                 search_glcc_for_ccid(
3504                                       p_system_info,
3505                                       p_segment_table,
3506                                       p_segment_cnt,
3507                                       NULL,
3508                                       NULL
3509                                     )
3510              );
3511 
3512 END search_glcc_for_ccid;
3513 
3514 
3515 
3516 ----------------------------------------------------------------------------
3517 --
3518 -- FUNCTION NAME:  Find_Cursor_In_Cache
3519 --
3520 -- DECSRIPTION:
3521 --                 Searches the cursor cache for an open cursor that matches
3522 --                 the conditions in the key.
3523 --
3524 -- ARGUMENTS:
3525 --      IN:
3526 --                 p_key
3527 --
3528 --      IN/OUT:
3529 --
3530 --      OUT:
3531 --
3532 -- RETURNS:
3533 --   Cursor number
3534 --
3535 -- NOTES:
3536 --
3537 -- HISTORY:
3538 --    14-FEB-97  C. Tomberg  Created
3539 --
3540 
3541 FUNCTION Find_Cursor_In_Cache ( p_key  IN VARCHAR2 ) RETURN BINARY_INTEGER IS
3542 BEGIN
3543 
3544            print_fcn_label2( 'arp_auto_accounting.Find_Cursor_In_Cache()+' );
3545 
3546            FOR l_index IN 1..cursor_attr_cache.count LOOP
3547 
3548                IF ( cursor_attr_cache(l_index) = p_key )
3549                THEN
3550                       print_fcn_label2(
3551                                'arp_auto_accounting.Find_Cursor_In_Cache()-' );
3552                       RETURN( l_index );
3553                END IF;
3554 
3555            END LOOP;
3556 
3557 
3558             print_fcn_label2( 'arp_auto_accounting.Find_Cursor_In_Cache()-' );
3559 
3560            RETURN(NULL);
3561 
3562 EXCEPTION
3563     WHEN OTHERS THEN
3564         debug('EXCEPTION: arp_auto_accounting.Find_Cursor_In_Cache()',
3565 	      MSG_LEVEL_BASIC);
3566         debug(SQLERRM, MSG_LEVEL_BASIC);
3567         RAISE;
3568 END;
3569 
3570 ----------------------------------------------------------------------------
3571 --
3572 -- FUNCTION NAME:  Get_Select_Cursor
3573 --
3574 -- DECSRIPTION:
3575 --                 Returns a cursor for the select statement.
3576 --                 Multiple cursors are maintained for different combinations
3577 --                 of input parameters. These criteria are encoded in the
3578 --                 key which points to the appropriate record in the cursor
3579 --                 cache. If the cursor is not found in the cache,
3580 --                 a select statement is built and parsed, and the new
3581 --                 cursor is added to the cache if the cache is not full.
3582 --
3583 -- ARGUMENTS:
3584 --      IN:
3585 --              p_system_info
3586 --              p_profile_info
3587 --              p_account_class
3588 --              p_customer_trx_id
3589 --              p_customer_trx_line_id
3590 --              p_cust_trx_line_salesrep_id
3591 --              p_request_id
3592 --              p_gl_date
3593 --              p_original_gl_date
3594 --              p_total_trx_amount
3595 --              p_code_combination_id
3596 --              p_force_account_set_no
3597 --              p_cust_trx_type_id
3598 --              p_primary_salesrep_id
3599 --              p_inventory_item_id
3600 --              p_memo_line_id
3601 --
3602 --      IN/OUT:
3603 --
3604 --      OUT:
3605 --              p_keep_cursor_open_flag   - if the cursor is in the cache or
3606 --                                          was added to the cache, don't
3607 --                                          close it after its first use.
3608 --
3609 -- RETURNS:
3610 --   Cursor number
3611 --
3612 -- NOTES:
3613 --
3614 -- HISTORY:
3615 --    14-FEB-97  C. Tomberg  Created
3616 --
3617 
3618 FUNCTION Get_Select_Cursor(
3619                            p_system_info 		IN
3620                              arp_trx_global.system_info_rec_type,
3621                            p_profile_info 		IN
3622                              arp_trx_global.profile_rec_type,
3623                            p_account_class 		IN VARCHAR2,
3624                            p_customer_trx_id 		IN BINARY_INTEGER,
3625                            p_customer_trx_line_id 	IN number,
3626                            p_cust_trx_line_salesrep_id 	IN number,
3627                            p_request_id 		IN BINARY_INTEGER,
3628                            p_gl_date 			IN DATE,
3629                            p_original_gl_date 		IN DATE,
3630                            p_total_trx_amount 		IN NUMBER,
3631                            p_code_combination_id 	IN BINARY_INTEGER,
3632                            p_force_account_set_no 	IN VARCHAR2,
3633                            p_cust_trx_type_id 		IN BINARY_INTEGER,
3634                            p_primary_salesrep_id 	IN BINARY_INTEGER,
3635                            p_inventory_item_id 		IN BINARY_INTEGER,
3636                            p_memo_line_id 		IN BINARY_INTEGER,
3637 			   p_use_unearn_srep_dependency IN BOOLEAN DEFAULT FALSE,
3638                            p_keep_cursor_open_flag     OUT NOCOPY BOOLEAN )
3639           RETURN BINARY_INTEGER IS
3640 
3641     l_select_rec    select_rec_type;
3642     l_select_tab    select_rec_tab;
3643     l_key           VARCHAR2(100);
3644     l_select_c      BINARY_INTEGER;
3645     l_cursor_index  BINARY_INTEGER;
3646     l_cursor        BINARY_INTEGER;
3647     l_ignore   	    INTEGER;
3648 
3649 BEGIN
3650 
3651        print_fcn_label2( 'arp_auto_accounting.Get_Select_Cursor()+' );
3652 
3653        p_keep_cursor_open_flag := TRUE;
3654 
3655       /*----------------------------------+
3656        |  Construct the cursor cache key  |
3657        +----------------------------------*/
3658 
3659        l_key := p_account_class || '-';
3660 
3661        IF (p_code_combination_id  IS NOT NULL)
3662        THEN l_key := l_key || 'Y-';
3663        ELSE l_key := l_key || 'N-';
3664        END IF;
3665 
3666        IF (p_gl_date  IS NOT NULL)
3667        THEN l_key := l_key || 'Y-';
3668        ELSE l_key := l_key || 'N-';
3669        END IF;
3670 
3671        IF (p_request_id  IS NOT NULL)
3672        THEN l_key := l_key || 'Y-';
3673        ELSE l_key := l_key || 'N-';
3674        END IF;
3675 
3676        IF (p_cust_trx_line_salesrep_id  IS NOT NULL)
3677        THEN l_key := l_key || 'Y-';
3678        ELSE l_key := l_key || 'N-';
3679        END IF;
3680 
3681        IF (p_customer_trx_id  IS NOT NULL)
3682        THEN l_key := l_key || 'Y-';
3683        ELSE l_key := l_key || 'N-';
3684        END IF;
3685 
3686        IF (p_customer_trx_line_id  IS NOT NULL)
3687        THEN l_key := l_key || 'Y-';
3688        ELSE l_key := l_key || 'N-';
3689        END IF;
3690 
3691        /*added for cursor index 9112739*/
3692        IF (p_use_unearn_srep_dependency  )
3693        THEN l_key := l_key || 'Y-';
3694        ELSE l_key := l_key || 'N-';
3695        END IF;
3696 
3697 
3698       /*----------------------------------------------------+
3699        |  Attempt to get the cursor from the cursor cache.  |
3700        +----------------------------------------------------*/
3701 
3702        l_cursor_index := Find_Cursor_In_Cache(l_key);
3703 
3704 
3705       /*---------------------------------------------------+
3706        |  If the cursor was found, return it immediately.  |
3707        +---------------------------------------------------*/
3708 
3709        IF  (l_cursor_index IS NOT NULL)
3710        THEN
3711 
3712              print_fcn_label2( 'arp_auto_accounting.Get_Select_Cursor()-' );
3713 
3714              l_cursor := cursor_cache( l_cursor_index );
3715 
3716              debug('Found cursor in cache:  key ' || l_key ||
3717                    '  cursor index: ' || l_cursor_index ||
3718                    '  cursor number: ' ||
3719                    l_cursor, MSG_LEVEL_DEBUG);
3720 
3721              RETURN( l_cursor );
3722        END IF;
3723 
3724 
3725       /*----------------------------------------------+
3726        |  If the cursor was not found in the cache,   |
3727        |  construct and parse the select statement.   |
3728        +----------------------------------------------*/
3729 
3730        debug('Reparsing cursor that was not found in the cache. Key: ' ||
3731              l_key,
3732              MSG_LEVEL_DEBUG);
3733 
3734 
3735        DECLARE
3736                 l_select_stmt VARCHAR2(32767);
3737                 l_cache_index BINARY_INTEGER;
3738 
3739        BEGIN
3740 
3741                 l_select_c := dbms_sql.open_cursor;
3742 
3743                 l_cache_index :=  cursor_attr_cache.count + 1;
3744 
3745                /*----------------------------------------------------+
3746                 |  Add the new cursor to the cache if the cache is   |
3747                 |  not already full.                                 |
3748                 +----------------------------------------------------*/
3749 
3750                 IF ( l_cache_index <= MAX_CURSOR_CACHE_SIZE )
3751                 THEN
3752                       cursor_attr_cache( l_cache_index ) := l_key;
3753                       cursor_cache( l_cache_index )      := l_select_c;
3754 
3755                       p_keep_cursor_open_flag := TRUE;
3756 
3757                 ELSE  p_keep_cursor_open_flag := FALSE;
3758                 END IF;
3759 
3760 /*Bug 9112739*/
3761                 l_select_stmt := build_select_sql( p_system_info,
3762                                                p_profile_info,
3763                                                p_account_class,
3764                                                p_customer_trx_id,
3765                                                p_customer_trx_line_id,
3766                                                p_cust_trx_line_salesrep_id,
3767                                                p_request_id,
3768                                                p_gl_date,
3769                                                p_original_gl_date,
3770                                                p_total_trx_amount,
3771                                                p_code_combination_id,
3772                                                p_force_account_set_no,
3773                                                p_cust_trx_type_id,
3774                                                p_primary_salesrep_id,
3775                                                p_inventory_item_id,
3776                                                p_memo_line_id,
3777 					       p_use_unearn_srep_dependency);
3778 
3779                 --
3780                 -- add Order By clause
3781                 --
3782 
3783                 l_select_stmt := l_select_stmt || CRLF ||
3784 'ORDER BY  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12';
3785 
3786                 ------------------------------------------------------------
3787                 -- Parse
3788                 ------------------------------------------------------------
3789 		debug( '  Parsing select stmt', MSG_LEVEL_DEBUG );
3790 
3791                 dbms_sql.parse( l_select_c, l_select_stmt, dbms_sql.v7);
3792 
3793 
3794                 ------------------------------------------------------------
3795                 -- Define Column Arrays
3796                 ------------------------------------------------------------
3797                 define_arrays( l_select_c, l_select_tab );
3798 
3799 
3800        EXCEPTION
3801                 WHEN OTHERS THEN
3802                   debug( 'Error constructing/parsing select cursor',
3803                          MSG_LEVEL_BASIC );
3804                   debug(SQLERRM, MSG_LEVEL_BASIC);
3805                   RAISE;
3806 
3807        END;
3808 
3809        print_fcn_label2( 'arp_auto_accounting.Get_Select_Cursor()-' );
3810 
3811        RETURN( l_select_c );
3812 
3813 
3814 EXCEPTION
3815     WHEN OTHERS THEN
3816 
3817         debug('EXCEPTION: arp_auto_accounting.Get_Select_Cursor()',
3818 	      MSG_LEVEL_BASIC);
3819         debug(SQLERRM, MSG_LEVEL_BASIC);
3820         RAISE;
3821 
3822 END;
3823 
3824 ----------------------------------------------------------------------------
3825 --
3826 -- PROCEDURE NAME:  Bind_Variable
3827 --
3828 -- DECSRIPTION:
3829 --                 Bind a variable into the specified cursor.
3830 --                 Ignore the 'ORA-01006 - Bind variable doesd not exist'
3831 --                 error.
3832 --                 This routine is overloaded to deal with different datatypes.
3833 --
3834 -- ARGUMENTS:
3835 --      IN:
3836 --                 p_cursor
3837 --                 p_bind_variable
3838 --                 p_value
3839 --
3840 --      IN/OUT:
3841 --
3842 --      OUT:
3843 --
3844 -- RETURNS:
3845 --
3846 -- NOTES:
3847 --
3848 -- HISTORY:
3849 --    14-FEB-97  C. Tomberg  Created
3850 --
3851 
3852 PROCEDURE Bind_Variable( p_cursor         IN INTEGER,
3853                          p_bind_variable  IN VARCHAR2,
3854                          p_value          IN VARCHAR2
3855                        ) IS
3856 BEGIN
3857           dbms_sql.bind_variable( p_cursor,
3858                                   p_bind_variable,
3859                                   p_value );
3860 
3861 EXCEPTION
3862       WHEN OTHERS THEN
3863           IF (SQLCODE = -1006)
3864           THEN NULL;
3865           ELSE RAISE;
3866           END IF;
3867 
3868 END;
3869 
3870 PROCEDURE Bind_Variable( p_cursor         IN INTEGER,
3871                          p_bind_variable  IN VARCHAR2,
3872                          p_value          IN INTEGER
3873                        ) IS
3874 BEGIN
3875           dbms_sql.bind_variable( p_cursor,
3876                                   p_bind_variable,
3877                                   p_value );
3878 
3879 EXCEPTION
3880       WHEN OTHERS THEN
3881           IF (SQLCODE = -1006)
3882           THEN NULL;
3883           ELSE RAISE;
3884           END IF;
3885 
3886 END;
3887 
3888 
3889 PROCEDURE Bind_Variable( p_cursor         IN INTEGER,
3890                          p_bind_variable  IN VARCHAR2,
3891                          p_value          IN DATE
3892                        ) IS
3893 BEGIN
3894           dbms_sql.bind_variable( p_cursor,
3895                                   p_bind_variable,
3896                                   p_value );
3897 
3898 EXCEPTION
3899       WHEN OTHERS THEN
3900           IF (SQLCODE = -1006)
3901           THEN NULL;
3902           ELSE RAISE;
3903           END IF;
3904 
3905 END;
3906 
3907 
3908 ----------------------------------------------------------------------------
3909 --
3910 -- PROCEDURE NAME:  Bind_All_Variables
3911 --
3912 -- DECSRIPTION:
3913 --                 Bind all possible variable into the select cursor.
3914 --                 If the cursor is invalid, rebuild and reparse the
3915 --                 select statement and pass the new cursor value back
3916 --                 to the calling function.
3917 --
3918 -- ARGUMENTS:
3919 --      IN:
3920 --                 p_system_info
3921 --                 p_profile_info
3922 --                 p_account_class
3923 --                 p_customer_trx_id
3924 --                 p_customer_trx_line_id
3925 --                 p_cust_trx_line_salesrep_id
3926 --                 p_request_id
3927 --                 p_gl_date
3928 --                 p_original_gl_date
3929 --                 p_total_trx_amount
3930 --                 p_code_combination_id
3931 --                 p_force_account_set_no
3932 --                 p_cust_trx_type_id
3933 --                 p_primary_salesrep_id
3934 --                 p_inventory_item_id
3935 --                 p_memo_line_id
3936 --
3937 --      IN/OUT:
3938 --                 p_cursor
3939 --
3940 --      OUT:
3941 --                 p_keep_cursor_open_flag
3942 --
3943 -- RETURNS:
3944 --
3945 -- NOTES:
3946 --
3947 -- HISTORY:
3948 --    14-FEB-97  C. Tomberg  Created
3949 --
3950 
3951 PROCEDURE Bind_All_Variables(
3952                             p_cursor                    IN OUT NOCOPY BINARY_INTEGER,
3953                             p_system_info 		IN
3954                              arp_trx_global.system_info_rec_type,
3955                             p_profile_info 		IN
3956                              arp_trx_global.profile_rec_type,
3957                             p_account_class 		IN VARCHAR2,
3958                             p_customer_trx_id 		IN BINARY_INTEGER,
3959                             p_customer_trx_line_id 	IN NUMBER,
3960                             p_cust_trx_line_salesrep_id IN NUMBER,
3961                             p_request_id 		IN BINARY_INTEGER,
3962                             p_gl_date 			IN DATE,
3963                             p_original_gl_date 		IN DATE,
3964                             p_total_trx_amount 		IN NUMBER,
3965                             p_code_combination_id 	IN BINARY_INTEGER,
3966                             p_force_account_set_no 	IN VARCHAR2,
3967                             p_cust_trx_type_id 		IN BINARY_INTEGER,
3968                             p_primary_salesrep_id 	IN BINARY_INTEGER,
3969                             p_inventory_item_id 	IN BINARY_INTEGER,
3970                             p_memo_line_id 		IN BINARY_INTEGER,
3971                             p_keep_cursor_open_flag IN OUT NOCOPY BOOLEAN
3972                             ) IS
3973 
3974 BEGIN
3975 
3976         print_fcn_label2( 'arp_auto_accounting.Bind_All_Variables()+' );
3977 
3978         BEGIN
3979            Bind_Variable(
3980                           p_cursor,
3981                           ':force_account_set_no',
3982                           p_force_account_set_no
3983                         );
3984 
3985         EXCEPTION
3986 
3987           /*-----------------------------------------------------------+
3988            |  If the cursor is invalid, the first bind will fail.      |
3989            |  in that case, recreate and reparse the SQL statement     |
3990            |  and continue processing. The new cursor is passed back   |
3991            |  to the calling routine since it is an IN/OUT parameter.  |
3992    	   +-----------------------------------------------------------*/
3993 
3994            WHEN INVALID_CURSOR THEN
3995 
3996               debug('Handling INVALID_CURSOR exception by reparsing.',
3997                     MSG_LEVEL_DEBUG);
3998 /*Bug9112739*/
3999               p_cursor := Get_Select_Cursor(
4000                                              system_info,
4001                                              profile_info,
4002                                              p_account_class,
4003                                              p_customer_trx_id,
4004                                              p_customer_trx_line_id,
4005                                              p_cust_trx_line_salesrep_id,
4006                                              p_request_id,
4007                                              p_gl_date,
4008                                              p_original_gl_date,
4009                                              p_total_trx_amount,
4010                                              p_code_combination_id,
4011                                              p_force_account_set_no,
4012                                              p_cust_trx_type_id,
4013                                              p_primary_salesrep_id,
4014                                              p_inventory_item_id,
4015                                              p_memo_line_id,
4016 					     FALSE,
4017                                              p_keep_cursor_open_flag);
4018 
4019               Bind_Variable(
4020                              p_cursor,
4021                              ':force_account_set_no',
4022                              p_force_account_set_no
4023                            );
4024 
4025            WHEN OTHERS THEN RAISE;
4026         END;
4027 
4028 
4029         Bind_Variable(
4030                        p_cursor,
4031                        ':total_trx_amount',
4032                        p_total_trx_amount
4033                      );
4034 
4035 
4036         Bind_Variable(
4037                        p_cursor,
4038                        ':original_gl_date',
4039                        p_original_gl_date
4040                      );
4041 
4042         Bind_Variable(
4043                        p_cursor,
4044                        ':gl_date',
4045                        p_gl_date
4046                      );
4047 
4048 
4049         Bind_Variable(
4050                        p_cursor,
4051                        ':code_combination_id',
4052                        p_code_combination_id
4053                      );
4054 
4055         Bind_Variable(
4056                        p_cursor,
4057                        ':request_id1',
4058                        p_request_id
4059                      );
4060 
4061 
4062 	Bind_Variable(
4063                        p_cursor,
4064                        ':cust_trx_line_salesrep_id',
4065                        p_cust_trx_line_salesrep_id
4066                      );
4067 
4068         Bind_Variable(
4069                        p_cursor,
4070                        ':request_id',
4071                        p_request_id
4072                      );
4073 
4074 
4075         Bind_Variable(
4076                        p_cursor,
4077                        ':customer_trx_id',
4078                        p_customer_trx_id
4079                      );
4080 
4081 
4082         Bind_Variable(
4083                        p_cursor,
4084                        ':customer_trx_line_id',
4085                        p_customer_trx_line_id
4086                      );
4087 
4088         Bind_Variable(
4089                        p_cursor,
4090                        ':base_currency',
4091                        p_system_info.base_currency
4092                      );
4093 
4094 
4095         print_fcn_label2( 'arp_auto_accounting.Bind_All_Variables()-' );
4096 
4097 EXCEPTION
4098     WHEN OTHERS THEN
4099         debug('EXCEPTION: arp_auto_accounting.Bind_All_Variables()',
4100 		MSG_LEVEL_BASIC);
4101         debug(SQLERRM, MSG_LEVEL_BASIC);
4102         RAISE;
4103 END;
4104 
4105 ----------------------------------------------------------------------------
4106 --
4107 -- PROCEDURE NAME:  get_default_ccids
4108 --
4109 -- DECSRIPTION:
4110 --   Gets default ccids for all possible tables which autoaccounting
4111 --   may be based.
4112 --
4113 -- ARGUMENTS:
4114 --      IN:
4115 --        profile_info
4116 --        account_class
4117 --        line_id
4118 --        trx_type_id
4119 --        salesrep_id
4120 --        inv_item_id
4121 --        memo_line_id
4122 --
4123 --      IN/OUT:
4124 --        ccid_record
4125 --        inv_item_type
4126 --
4127 --      OUT:
4128 --
4129 -- NOTES:
4130 --
4131 -- HISTORY:
4132 --
4133 PROCEDURE get_default_ccids( p_profile_info 	IN
4134                                arp_trx_global.profile_rec_type,
4135                              p_account_class 	IN VARCHAR2,
4136                              p_line_id 		IN NUMBER,
4137                              p_trx_type_id 	IN BINARY_INTEGER,
4138                              p_salesrep_id 	IN BINARY_INTEGER,
4139                              p_inv_item_id 	IN BINARY_INTEGER,
4140                              p_memo_line_id 	IN BINARY_INTEGER,
4141                              p_site_use_id      IN NUMBER,
4142                              p_warehouse_id     IN BINARY_INTEGER,
4143                              p_ccid_record 	IN OUT NOCOPY ccid_rec_type,
4144                              p_inv_item_type 	IN OUT NOCOPY
4145                                mtl_system_items.item_type%TYPE )  IS
4146 
4147 
4148 BEGIN
4149     print_fcn_label2( 'arp_auto_accounting.get_default_ccids()+' );
4150 
4151     --
4152     -- trx type
4153     --
4154     IF( p_trx_type_id is NOT NULL ) THEN
4155         get_trx_type_ccids( p_trx_type_id,
4156                              p_ccid_record.trx_type_ccid_rev,
4157                              p_ccid_record.trx_type_ccid_rec,
4158                              p_ccid_record.trx_type_ccid_frt,
4159                              p_ccid_record.trx_type_ccid_tax,
4160                              p_ccid_record.trx_type_ccid_unbill,
4161                              p_ccid_record.trx_type_ccid_unearn,
4162                              p_ccid_record.trx_type_ccid_suspense );
4163 
4164     END IF;
4165 
4166     --
4167     -- billing site ccids
4168     --
4169     IF( p_site_use_id is NOT NULL ) THEN
4170         get_site_use_ccids( p_site_use_id,
4171                              p_ccid_record.site_use_ccid_rev,
4172                              p_ccid_record.site_use_ccid_rec,
4173                              p_ccid_record.site_use_ccid_frt,
4174                              p_ccid_record.site_use_ccid_tax,
4175                              p_ccid_record.site_use_ccid_unbill,
4176                              p_ccid_record.site_use_ccid_unearn,
4177                              p_ccid_record.site_use_ccid_suspense );
4178 
4179     END IF;
4180 
4181     --
4182     -- salesrep
4183     --
4184     IF( p_salesrep_id is NOT NULL ) THEN
4185         get_salesrep_ccids( p_salesrep_id,
4186                              p_ccid_record.salesrep_ccid_rev,
4187                              p_ccid_record.salesrep_ccid_rec,
4188                              p_ccid_record.salesrep_ccid_frt );
4189     END IF;
4190 
4191     --
4192     -- lineitem
4193     --
4194     IF( p_inv_item_id is NOT NULL ) THEN
4195 
4196         get_inv_item_ccids( p_profile_info,
4197                             p_inv_item_id,
4198                             p_warehouse_id,
4199                             p_ccid_record.lineitem_ccid_rev,
4200                             p_inv_item_type );
4201 
4202     ELSIF( p_memo_line_id is NOT NULL ) THEN
4203 
4204         get_memo_line_ccids( p_memo_line_id,
4205                              p_ccid_record.lineitem_ccid_rev );
4206     END IF;
4207 
4208     --
4209     -- agreement/category
4210     -- Only default  if type REV/CHARGES and REV autoacc def
4211     -- is based on table 'AGREEMENT/CATEGORY'
4212     --
4213     IF( p_account_class in (REV, CHARGES) AND
4214         query_autoacc_def( REV, 'AGREEMENT/CATEGORY' ) ) THEN
4215 
4216         get_agreecat_ccids( p_profile_info,
4217                             p_line_id,
4218 			    p_warehouse_id,        --Bug#1639334
4219                             p_ccid_record.agreecat_ccid_rev );
4220     END IF;
4221 
4222     print_fcn_label2( 'arp_auto_accounting.get_default_ccids()-' );
4223 
4224 EXCEPTION
4225     WHEN OTHERS THEN
4226         debug('EXCEPTION: arp_auto_accounting.get_default_ccids('
4227               || p_account_class || ', '
4228               || to_char(p_line_id) || ', '
4229               || to_char(p_trx_type_id) || ', '
4230               || to_char(p_salesrep_id) || ', '
4231               || to_char(p_inv_item_id) || ', '
4232               || to_char(p_memo_line_id)|| ', '
4233               || to_char(p_site_use_id) || ', '
4234               || to_char(p_warehouse_id)||')', MSG_LEVEL_BASIC);
4235         debug(SQLERRM, MSG_LEVEL_BASIC);
4236         RAISE;
4237 END get_default_ccids;
4238 
4239 ----------------------------------------------------------------------------
4240 --
4241 -- PROCEDURE NAME:  assemble_code_combination
4242 --
4243 -- DECSRIPTION:
4244 --
4245 --
4246 -- ARGUMENTS:
4247 --      IN:
4248 --        system_info
4249 --        flex_info
4250 --        account_class
4251 --        ccid_record
4252 --        inv_item_type
4253 --
4254 --      IN/OUT:
4255 --        ccid
4256 --        assembled_segments
4257 --        int_ccid (interim tax account ccid)
4258 --        int_concat_segments (interim tax concatenated segments)
4259 --      OUT:
4260 --
4261 -- NOTES:
4262 --
4263 --
4264 -- HISTORY:
4265 --
4266 --
4267 PROCEDURE assemble_code_combination(
4268                   p_system_info IN arp_trx_global.system_info_rec_type,
4269                   p_flex_info  IN arp_trx_global.acct_flex_info_rec_type,
4270                   p_account_class IN VARCHAR2,
4271                   p_ccid_record IN CCID_REC_TYPE,
4272                   p_inv_item_type IN mtl_system_items.item_type%TYPE,
4273                   p_ccid IN OUT NOCOPY BINARY_INTEGER,
4274                   p_concat_segments IN OUT NOCOPY VARCHAR2,
4275                   p_int_ccid IN OUT NOCOPY BINARY_INTEGER,
4276                   p_int_concat_segments IN OUT NOCOPY VARCHAR2 ) IS
4277 
4278     l_table_offset        BINARY_INTEGER;
4279     l_cnt                 BINARY_INTEGER;
4280     l_concat_segments     VARCHAR2(800);
4281     l_int_concat_segments VARCHAR2(800);
4282     l_seg                 ra_account_default_segments.segment%type;
4283     l_const               ra_account_default_segments.constant%type;
4284     l_tbl                 ra_account_default_segments.table_name%type;
4285     l_ccid                BINARY_INTEGER;
4286     l_seg_num             BINARY_INTEGER;
4287     l_seg_value           gl_code_combinations.segment1%type;
4288     l_int_seg_value       gl_code_combinations.segment1%type;
4289     l_delim               VARCHAR2(1);
4290 
4291     -- to store segment values for binding
4292     --
4293     l_seg_table fnd_flex_ext.SegmentArray;
4294     l_int_seg_table fnd_flex_ext.SegmentArray;
4295 
4296 BEGIN
4297 
4298     print_fcn_label2( 'arp_auto_accounting.assemble_code_combination()+' );
4299 
4300     -- get offset, count for account class (to access plsql tables)
4301     --
4302     IF( p_account_class in (REV, CHARGES) ) then
4303         --
4304         -- Charges uses autoacc definition for Revenue
4305         --
4306         IF( p_inv_item_type = 'FRT' ) then
4307             --
4308             -- use autoacc definition for FREIGHT
4309             -- if inv item is of type 'FRT'
4310             --
4311             l_table_offset := frt_offset;
4312             l_cnt := frt_count;
4313         ELSE
4314             --
4315             -- use autoacc definition for REVENUE
4316             --
4317             l_table_offset := rev_offset;
4318             l_cnt := rev_count;
4319         END IF;
4320     ELSIF( p_account_class = REC ) then
4321         l_table_offset := rec_offset;
4322         l_cnt := rec_count;
4323     ELSIF( p_account_class = FREIGHT ) then
4324         l_table_offset := frt_offset;
4325         l_cnt := frt_count;
4326     ELSIF( p_account_class = TAX ) then
4327         l_table_offset := tax_offset;
4328         l_cnt := tax_count;
4329     ELSIF( p_account_class = UNBILL ) then
4330         l_table_offset := unbill_offset;
4331         l_cnt := unbill_count;
4332     ELSIF( p_account_class = UNEARN ) then
4333         l_table_offset := unearn_offset;
4334         l_cnt := unearn_count;
4335     ELSIF( p_account_class = SUSPENSE ) then
4336         l_table_offset := suspense_offset;
4337         l_cnt := suspense_count;
4338     ELSE
4339 	g_error_buffer := 'Invalid account class';
4340 	debug( 'EXCEPTION: '||g_error_buffer, MSG_LEVEL_BASIC );
4341         RAISE invalid_account_class;
4342     END IF;
4343 
4344     -- loop for each enabled segment
4345     --
4346     FOR i IN 0..l_cnt - 1 LOOP
4347         l_const := autoacc_def_const_t(l_table_offset + i);
4348         l_tbl := autoacc_def_table_t(l_table_offset + i);
4349         l_seg := autoacc_def_segment_t(l_table_offset + i);
4350         l_ccid := -1;
4351 
4352         IF( i = 0 ) THEN
4353             l_delim := null;
4354         ELSE
4355             l_delim := p_flex_info.delim;
4356         END IF;
4357 
4358         IF( l_const is NOT NULL ) THEN
4359             --
4360             -- constant
4361             --
4362             l_concat_segments := l_concat_segments || l_delim
4363                                || l_const;
4364             l_seg_table(i+1) := l_const;
4365 
4366             --for deferred tax
4367             l_int_concat_segments := l_int_concat_segments || l_delim
4368                                || l_const;
4369             l_int_seg_table(i+1) := l_const;
4370 
4371         ELSIF( l_tbl is NOT NULL ) THEN
4372             --
4373             -- table-based
4374             --
4375             IF( l_tbl = 'RA_CUST_TRX_TYPES' ) THEN
4376                 --
4377                 -- For all account classes except REC
4378                 --
4379                 IF p_account_class in (REV, CHARGES) THEN
4380 
4381                    IF( p_inv_item_type = 'FRT' ) THEN
4382                        --
4383                        -- use autoacc definition for FREIGHT
4384                        -- if inv item is of type 'FRT'
4385                        --
4386                         l_ccid := p_ccid_record.trx_type_ccid_frt;
4387                    ELSE
4388                         l_ccid := p_ccid_record.trx_type_ccid_rev;
4389                    END IF;
4390                 ELSIF p_account_class = REC THEN
4391                       l_ccid := p_ccid_record.trx_type_ccid_rec;
4392                 ELSIF p_account_class = FREIGHT THEN
4393                       l_ccid := p_ccid_record.trx_type_ccid_frt;
4394                 ELSIF p_account_class = TAX THEN
4395                       l_ccid := p_ccid_record.trx_type_ccid_tax;
4396                 ELSIF p_account_class = UNBILL THEN
4397                       l_ccid := p_ccid_record.trx_type_ccid_unbill;
4398                 ELSIF p_account_class = UNEARN THEN
4399                       l_ccid := p_ccid_record.trx_type_ccid_unearn;
4400                 ELSIF p_account_class = SUSPENSE THEN
4401                       l_ccid := p_ccid_record.trx_type_ccid_suspense;
4402                 END IF;
4403 
4404             ELSIF( l_tbl = 'RA_SITE_USES' ) THEN
4405                 --
4406                 -- For all account classes except REC
4407                 --
4408                 IF p_account_class in (REV, CHARGES) THEN
4409 
4410                    IF( p_inv_item_type = 'FRT' ) THEN
4411                        --
4412                        -- use autoacc definition for FREIGHT
4413                        -- if inv item is of type 'FRT'
4414                        --
4415                         l_ccid := p_ccid_record.site_use_ccid_frt;
4416                    ELSE
4417                         l_ccid := p_ccid_record.site_use_ccid_rev;
4418                    END IF;
4419                 ELSIF p_account_class = REC THEN
4420                       l_ccid := p_ccid_record.site_use_ccid_rec;
4421                 ELSIF p_account_class = FREIGHT THEN
4422                       l_ccid := p_ccid_record.site_use_ccid_frt;
4423                 ELSIF p_account_class = TAX THEN
4424                       l_ccid := p_ccid_record.site_use_ccid_tax;
4425                 ELSIF p_account_class = UNBILL THEN
4426                       l_ccid := p_ccid_record.site_use_ccid_unbill;
4427                 ELSIF p_account_class = UNEARN THEN
4428                       l_ccid := p_ccid_record.site_use_ccid_unearn;
4429                 ELSIF p_account_class = SUSPENSE THEN
4430                       l_ccid := p_ccid_record.site_use_ccid_suspense;
4431                 END IF;
4432 
4433             ELSIF( l_tbl = 'RA_SALESREPS' ) THEN
4434                 --
4435                 -- For all account classes
4436                 --
4437                 IF p_account_class in (REV, CHARGES) THEN
4438 
4439                    IF( p_inv_item_type = 'FRT' ) THEN
4440                        --
4441                        -- use autoacc definition for FREIGHT
4442                        -- if inv item is of type 'FRT'
4443                        --
4444                         l_ccid := p_ccid_record.salesrep_ccid_frt;
4445                    ELSE
4446                         l_ccid := p_ccid_record.salesrep_ccid_rev;
4447                    END IF;
4448 
4449                 /* Bug 2396754 - swapped salesrep_ccid_rec
4450                    for salesrep_ccid_rev for UNBILL - it was
4451                    an apparent typo */
4452 
4453                 ELSIF p_account_class = REC THEN
4454                     l_ccid := p_ccid_record.salesrep_ccid_rec;
4455                 ELSIF p_account_class = FREIGHT THEN
4456                     l_ccid := p_ccid_record.salesrep_ccid_frt;
4457                 ELSIF p_account_class = TAX THEN
4458                     l_ccid := p_ccid_record.salesrep_ccid_rev;
4459                 ELSIF p_account_class = UNBILL THEN
4460                     l_ccid := p_ccid_record.salesrep_ccid_rev;
4461                 ELSIF p_account_class = UNEARN THEN
4462                     l_ccid := p_ccid_record.salesrep_ccid_rev;
4463                 ELSIF p_account_class = SUSPENSE THEN
4464                     l_ccid := p_ccid_record.salesrep_ccid_rev;
4465                 END IF;
4466             ELSIF( l_tbl = 'RA_STD_TRX_LINES' ) THEN
4467                 --
4468                 -- For all account classes except REC
4469                 --
4470                 IF p_account_class in (REV, CHARGES) THEN
4471                     l_ccid := p_ccid_record.lineitem_ccid_rev;
4472                 ELSIF p_account_class = FREIGHT THEN
4473                     l_ccid := p_ccid_record.lineitem_ccid_rev;
4474                 ELSIF p_account_class = TAX THEN
4475                     l_ccid := p_ccid_record.lineitem_ccid_rev;
4476                 ELSIF p_account_class = UNBILL THEN
4477                     l_ccid := p_ccid_record.lineitem_ccid_rev;
4478                 ELSIF p_account_class = UNEARN THEN
4479                     l_ccid := p_ccid_record.lineitem_ccid_rev;
4480                 ELSIF p_account_class = SUSPENSE THEN
4481                     l_ccid := p_ccid_record.lineitem_ccid_rev;
4482                 END IF;
4483             ELSIF( l_tbl = 'RA_TAXES' ) THEN
4484                 --
4485                 -- For TAX account class only
4486                 --
4487                 IF p_account_class = TAX THEN
4488                     l_ccid := p_ccid_record.tax_ccid_tax;
4489                 END IF;
4490             ELSIF( l_tbl = 'AGREEMENT/CATEGORY' ) THEN
4491                 --
4492                 -- For REV, CHARGES account classes
4493                 --
4494                 IF p_account_class in (REV, CHARGES) THEN
4495                     l_ccid := p_ccid_record.agreecat_ccid_rev;
4496                 END IF;
4497             ELSE
4498 	        g_error_buffer := 'Invalid table name: '||l_tbl;
4499 	        debug( 'EXCEPTION: '||g_error_buffer, MSG_LEVEL_BASIC );
4500                 RAISE invalid_table_name;
4501             END IF;
4502 
4503             l_seg_num := TO_NUMBER(SUBSTRB(l_seg, LENGTHB('SEGMENT') + 1));
4504 
4505             -- Only get segment if have valid ccid
4506             --
4507             IF( l_ccid = -1 ) THEN
4508                   l_seg_value := INVALID_SEGMENT;
4509             ELSE
4510                 l_seg_value := get_segment_from_glcc( l_ccid, l_seg_num );
4511 
4512 		IF( l_seg_value IS NULL ) THEN
4513 		    --
4514 		    -- assign invalid segment value if no data found
4515 		    --
4516 		    l_seg_value := INVALID_SEGMENT;
4517 		END IF;
4518             END IF;
4519 
4520             l_concat_segments := l_concat_segments || l_delim || l_seg_value;
4521             l_seg_table(i+1) := l_seg_value;
4522 
4523             --
4524             -- Derive the interim tax account segments for deferred tax
4525             --
4526             IF (p_account_class = 'TAX')
4527                    AND (p_ccid_record.interim_tax_ccid IS NOT NULL) THEN
4528 
4529                IF l_tbl = 'RA_TAXES' THEN
4530                   -- Only get segment if have valid ccid
4531                   --
4532                   IF( p_ccid_record.interim_tax_ccid = -1) THEN
4533                       l_int_seg_value := INVALID_SEGMENT;
4534                   ELSE
4535                      l_int_seg_value :=
4536                         get_segment_from_glcc( p_ccid_record.interim_tax_ccid, l_seg_num);
4537 
4538                      IF ( l_int_seg_value IS NULL ) THEN
4539                         --
4540                         -- assign invalid segment value if no data found
4541                         --
4542                         l_int_seg_value := INVALID_SEGMENT;
4543                      END IF;
4544                   END IF;
4545 
4546                ELSE
4547                   l_int_seg_value := l_seg_value;
4548                END IF;
4549 
4550                l_int_concat_segments := l_int_concat_segments || l_delim || l_int_seg_value;
4551                l_int_seg_table(i+1)  := l_int_seg_value;
4552 
4553             END IF; -- if account class is TAX
4554 
4555         END IF;  -- if const is not null
4556     END LOOP;
4557 
4558     -- call ccid reader
4559     p_ccid := search_glcc_for_ccid(
4560                                      system_info,
4561                                      l_seg_table,
4562                                      l_cnt,
4563                                      p_account_class,
4564                                      l_concat_segments );
4565 
4566     -- return concat segs, and ccid
4567     p_concat_segments := l_concat_segments;
4568 
4569     -- call ccid reader for interim tax account
4570     IF (p_account_class = 'TAX')
4571            AND (p_ccid_record.interim_tax_ccid IS NOT NULL) THEN
4572         p_int_ccid := search_glcc_for_ccid(
4573                                          system_info,
4574                                          l_int_seg_table,
4575                                          l_cnt,
4576                                          p_account_class,
4577                                          l_int_concat_segments );
4578 
4579         -- return concat segs, and ccid for interim tax account
4580         p_int_concat_segments := l_int_concat_segments;
4581     END IF;
4582 
4583     print_fcn_label2( 'arp_auto_accounting.assemble_code_combination()-' );
4584 
4585 EXCEPTION
4586     WHEN OTHERS THEN
4587         debug('EXCEPTION: arp_auto_accounting.assemble_code_combination('
4588               || p_account_class || ')', MSG_LEVEL_BASIC);
4589         debug(SQLERRM, MSG_LEVEL_BASIC);
4590         RAISE;
4591 END assemble_code_combination;
4592 
4593 ----------------------------------------------------------------------------
4594 --
4595 -- PROCEDURE NAME:  flex_manager
4596 --
4597 -- DECSRIPTION:
4598 --   Entry point for flexfield assembly.
4599 --
4600 -- ARGUMENTS:
4601 --      IN:
4602 --        account_class
4603 --        line_id
4604 --        trx_type_id
4605 --        salesrep_id
4606 --        inv_item_id
4607 --        memo_line_id
4608 --        site_use_id
4609 --        warehouse_id
4610 --        ccid_tax
4611 --
4612 --      IN/OUT:
4613 --        ccid
4614 --        concat_segments
4615 --        int_ccid (Interim tax account ccid)
4616 --        int_concat_segments (Interim tax account concatenated segments)
4617 --
4618 -- RETURNS:
4619 --   1 if success, 0 otherwise
4620 --
4621 -- NOTES:
4622 --
4623 -- HISTORY:
4624 --
4625 PROCEDURE flex_manager( p_account_class IN VARCHAR2,
4626                        p_line_id IN NUMBER,
4627                        p_trx_type_id IN BINARY_INTEGER,
4628                        p_salesrep_id IN BINARY_INTEGER,
4629                        p_inv_item_id IN BINARY_INTEGER,
4630                        p_memo_line_id IN BINARY_INTEGER,
4631                        p_ccid_tax IN BINARY_INTEGER,
4632                        p_int_ccid_tax IN BINARY_INTEGER,
4633                        p_site_use_id IN NUMBER,
4634                        p_warehouse_id IN BINARY_INTEGER,
4635                        p_ccid IN OUT NOCOPY BINARY_INTEGER,
4636                        p_concat_segments IN OUT NOCOPY VARCHAR2,
4637                        p_int_ccid  IN OUT NOCOPY BINARY_INTEGER,
4638                        p_int_concat_segments IN OUT NOCOPY VARCHAR2 )  IS
4639 
4640     l_ccid_record ccid_rec_type;
4641     l_inv_item_type   mtl_system_items.item_type%TYPE;
4642 
4643     PROCEDURE print_params IS
4644     BEGIN
4645         debug('EXCEPTION: arp_auto_accounting.flex_manager('
4646               || p_account_class         || ', '
4647               || to_char(p_line_id)      || ', '
4648               || to_char(p_trx_type_id)  || ', '
4649               || to_char(p_salesrep_id)  || ', '
4650               || to_char(p_inv_item_id)  || ', '
4651               || to_char(p_memo_line_id) || ', '
4652               || to_char(p_ccid_tax)     || ', '
4653               || to_char(p_int_ccid_tax) || ', '
4654               || to_char(p_site_use_id)  || ', '
4655               || to_char(p_warehouse_id) || ')',
4656               MSG_LEVEL_DEBUG);
4657 
4658     END;
4659 
4660 BEGIN
4661 
4662     print_fcn_label( 'arp_auto_accounting.flex_manager()+' );
4663 
4664     debug( '  account_class='||p_account_class, MSG_LEVEL_DEBUG );
4665     debug( '  line_id='||to_char(p_line_id), MSG_LEVEL_DEBUG );
4666     debug( '  trx_type_id='||to_char(p_trx_type_id), MSG_LEVEL_DEBUG );
4667     debug( '  salesrep_id='||to_char(p_salesrep_id), MSG_LEVEL_DEBUG );
4668     debug( '  inv_item_id='||to_char(p_inv_item_id), MSG_LEVEL_DEBUG );
4669     debug( '  memo_line_id='||to_char(p_memo_line_id), MSG_LEVEL_DEBUG );
4670     debug( '  ccid_tax='||to_char(p_ccid_tax), MSG_LEVEL_DEBUG );
4671     debug( '  int_ccid_tax='||to_char(p_int_ccid_tax), MSG_LEVEL_DEBUG );
4672     debug( '  site_use_id='||to_char(p_site_use_id), MSG_LEVEL_DEBUG );
4673     debug( '  warehouse_id='||to_char(p_warehouse_id), MSG_LEVEL_DEBUG );
4674 
4675     --
4676     -- Initialize
4677     --
4678     p_concat_segments := NULL;
4679     p_ccid := -1;
4680 
4681     --
4682     -- deferred tax is optional so initialize interim tax to null
4683     --
4684 
4685     p_int_concat_segments := NULL;
4686     p_int_ccid := NULL;
4687 
4688     --
4689     -- Validate inv_item_id, memo_line_id: at least one must be NULL
4690     --
4691     IF( p_inv_item_id is NOT NULL AND p_memo_line_id is NOT NULL ) THEN
4692         --
4693         -- error condition
4694         g_error_buffer := 'Either item id or memo line id must be null';
4695 	debug( 'EXCEPTION: '||g_error_buffer, MSG_LEVEL_BASIC );
4696         RAISE item_and_memo_both_not_null;
4697     END IF;
4698 
4699     --
4700     --
4701     --
4702     get_default_ccids( profile_info,
4703                        p_account_class,
4704                        p_line_id,
4705                        p_trx_type_id,
4706                        p_salesrep_id,
4707                        p_inv_item_id,
4708                        p_memo_line_id,
4709                        p_site_use_id,
4710                        p_warehouse_id,
4711                        l_ccid_record,
4712                        l_inv_item_type );
4713 
4714     --
4715     -- Add tax ccid to the ccid record
4716     --
4717     IF( p_ccid_tax is null) THEN
4718         l_ccid_record.tax_ccid_tax := -1;
4719     ELSE
4720         l_ccid_record.tax_ccid_tax := p_ccid_tax;
4721     END IF;
4722 
4723     --
4724     -- Add interim tax ccid to the ccid record values null or ccid
4725     --
4726 
4727     l_ccid_record.interim_tax_ccid := p_int_ccid_tax;
4728 
4729     -- Dump ccid record, item type
4730     --
4731     dump_ccid_record( l_ccid_record );
4732 
4733     debug( '  inv_item_type='||l_inv_item_type, MSG_LEVEL_DEBUG );
4734 
4735     --
4736     -- Assemble segments and get ccid
4737     --
4738     assemble_code_combination( system_info,
4739                                flex_info,
4740                                p_account_class,
4741                                l_ccid_record,
4742                                l_inv_item_type,
4743                                p_ccid,
4744                                p_concat_segments,
4745                                p_int_ccid,
4746                                p_int_concat_segments );
4747 
4748 
4749     debug( '  ccid= '||to_char(p_ccid), MSG_LEVEL_DEBUG );
4750     debug( '  concat_segs= '||p_concat_segments, MSG_LEVEL_DEBUG );
4751 
4752     debug( '  interim_tax_ccid= '||to_char(p_int_ccid), MSG_LEVEL_DEBUG );
4753     debug( '  interim_tax_concat_segs= '||p_int_concat_segments, MSG_LEVEL_DEBUG );
4754 
4755     print_fcn_label( 'arp_auto_accounting.flex_manager()-' );
4756 
4757 EXCEPTION
4758     WHEN OTHERS THEN
4759         print_params;
4760         debug(SQLERRM, MSG_LEVEL_BASIC);
4761         RAISE;
4762 
4763 END flex_manager;
4764 
4765 
4766 ----------------------------------------------------------------------------
4767 --
4768 -- FUNCTION NAME:  build_delete_sql
4769 --
4770 -- DECSRIPTION:
4771 --
4772 -- ARGUMENTS:
4773 --      IN:
4774 --        system_info
4775 --        profile_info
4776 --        account_class
4777 --        customer_trx_id
4778 --        customer_trx_line_id
4779 --        cust_trx_line_salesrep_id
4780 --        request_id
4781 --
4782 --      IN/OUT:
4783 --
4784 --      OUT:
4785 --
4786 -- RETURNS:
4787 --   delete sql
4788 --
4789 -- NOTES:
4790 --
4791 -- HISTORY:
4792 --
4793 FUNCTION build_delete_sql( p_system_info 		IN
4794                              arp_trx_global.system_info_rec_type,
4795                            p_profile_info 		IN
4796                              arp_trx_global.profile_rec_type,
4797                            p_account_class 		IN VARCHAR2,
4798                            p_customer_trx_id 		IN BINARY_INTEGER,
4799                            p_customer_trx_line_id 	IN NUMBER,
4800                            p_cust_trx_line_salesrep_id 	IN NUMBER,
4801                            p_request_id 		IN BINARY_INTEGER )
4802   RETURN VARCHAR2 IS
4803 
4804     l_delete_stmt               VARCHAR2(1000);
4805     l_account_class_pred        VARCHAR2(500);
4806     l_request_id_pred           VARCHAR2(500);
4807     l_ctid_pred                 VARCHAR2(500);
4808     l_ctlid_pred                VARCHAR2(500);
4809     l_ctlsid_pred               VARCHAR2(500);
4810 
4811 BEGIN
4812     print_fcn_label( 'arp_auto_accounting.build_delete_sql()+' );
4813 
4814     --
4815     -- account_class
4816     --
4817     IF( p_account_class = REV ) THEN
4818         l_account_class_pred := CRLF ||
4819 'AND account_class in (''REV'', ''UNBILL'', ''UNEARN'')';
4820     ELSE
4821         l_account_class_pred := CRLF ||
4822 'AND account_class = ''' || p_account_class || '''';
4823     END IF;
4824 
4825     --
4826     -- request_id
4827     --
4828     IF( p_request_id IS NOT NULL ) THEN
4829         l_request_id_pred := CRLF ||
4830 'AND request_id = ' || to_char( p_request_id );
4831     END IF;
4832 
4833     --
4834     -- customer_trx_id
4835     --
4836     IF( p_customer_trx_id IS NOT NULL ) THEN
4837         l_ctid_pred := CRLF ||
4838 'AND customer_trx_id = ' || to_char( p_customer_trx_id );
4839 
4840       /*9112739 Added IF clause code to prevent UNEARN deletion*/
4841       IF( p_account_class = UNEARN ) THEN
4842 
4843         l_ctid_pred := CRLF ||
4844 'AND customer_trx_id in
4845 (
4846   SELECT customer_trx_id
4847   FROM   ra_customer_trx ct
4848   WHERE  (ct.customer_trx_id  = '
4849             || to_char( p_customer_trx_id ) || CRLF ||
4850   '  AND nvl(ct.invoicing_rule_id,-10) = -2 )' || CRLF ||
4851 ')';
4852 
4853 
4854       END IF;
4855 
4856     END IF;
4857 
4858     --
4859     -- customer_trx_line_id
4860     --
4861     IF( p_customer_trx_line_id IS NOT NULL ) THEN
4862 /*9112739 Added IF-ELSE clause code to prevent UNEARN deletion*/
4863 
4864       IF( p_account_class = UNEARN ) THEN
4865 
4866         l_ctlid_pred := CRLF ||
4867 'AND customer_trx_line_id in
4868 (
4869   SELECT customer_trx_line_id
4870   FROM   ra_customer_trx_lines ctl, ra_customer_trx ct
4871   WHERE  ct.customer_trx_id=ctl.customer_trx_id AND nvl(ct.invoicing_rule_id,-10) = -2 AND (ctl.customer_trx_line_id  = '
4872             || to_char( p_customer_trx_line_id ) || CRLF ||
4873 '          or ctl.link_to_cust_trx_line_id = '
4874             || to_char( p_customer_trx_line_id ) || ')' || CRLF ||
4875 ')';
4876 
4877       ELSE
4878 
4879         l_ctlid_pred := CRLF ||
4880 'AND customer_trx_line_id in
4881 (
4882   SELECT customer_trx_line_id
4883   FROM   ra_customer_trx_lines ctl
4884   WHERE  (ctl.customer_trx_line_id  = '
4885             || to_char( p_customer_trx_line_id ) || CRLF ||
4886 '          or ctl.link_to_cust_trx_line_id = '
4887             || to_char( p_customer_trx_line_id ) || ')' || CRLF ||
4888 ')';
4889 
4890       END IF;
4891 
4892     END IF;
4893 
4894     --
4895     -- salesrep_id
4896     --
4897 
4898     IF( p_cust_trx_line_salesrep_id IS NOT NULL ) THEN
4899 
4900     /* Bug 2524140 - When REV accounts based on salesrep,
4901        autoaccounting creating multiple TAX rows
4902        with alternating +100/-100 percentages. */
4903 
4904         IF ((p_account_class = 'TAX' AND
4905              p_system_info.tax_based_on_salesrep) OR
4906              p_account_class <> 'TAX') THEN
4907 
4908         l_ctlsid_pred := CRLF ||
4909 'AND cust_trx_line_salesrep_id = ' || to_char(p_cust_trx_line_salesrep_id ) ;
4910 
4911         END IF;
4912 
4913     END IF;
4914 
4915     --
4916     -- Construct the Delete Statement
4917     --
4918     l_delete_stmt :=
4919 'DELETE from ra_cust_trx_line_gl_dist gd
4920 WHERE gl_posted_date is null'
4921 || l_account_class_pred
4922 || l_request_id_pred
4923 || l_ctid_pred
4924 || l_ctlid_pred
4925 || l_ctlsid_pred
4926 || CRLF ||
4927 'AND account_set_flag = (SELECT decode(ct.invoicing_rule_id,
4928                                       NULL, ''N'',
4929                                       ''Y'')
4930                         FROM   ra_customer_trx ct
4931                         WHERE  ct.customer_trx_id = gd.customer_trx_id)';
4932 
4933 
4934     debug( l_delete_stmt, MSG_LEVEL_DEBUG );
4935     debug( '  len(l_delete_stmt)=' || to_char(LENGTHB(l_delete_stmt)),
4936            MSG_LEVEL_DEBUG );
4937 
4938     print_fcn_label( 'arp_auto_accounting.build_delete_sql()-' );
4939 
4940     RETURN l_delete_stmt;
4941 
4942 
4943 EXCEPTION
4944     WHEN OTHERS THEN
4945         debug('EXCEPTION: arp_auto_accounting.build_delete_sql()',
4946 	      MSG_LEVEL_BASIC);
4947         debug(SQLERRM, MSG_LEVEL_BASIC);
4948         RAISE;
4949 END build_delete_sql;
4950 
4951 
4952 ----------------------------------------------------------------------------
4953 PROCEDURE get_column_values( p_select_c   IN  INTEGER,
4954                              p_select_rec OUT NOCOPY select_rec_type ) IS
4955 BEGIN
4956     print_fcn_label2( 'arp_auto_accounting.get_column_values()+' );
4957 
4958     dbms_sql.column_value( p_select_c, 1, p_select_rec.customer_trx_id );
4959     dbms_sql.column_value( p_select_c, 2, p_select_rec.customer_trx_line_id );
4960     dbms_sql.column_value( p_select_c, 3,
4961                            p_select_rec.cust_trx_line_salesrep_id );
4962     dbms_sql.column_value( p_select_c, 4, p_select_rec.line_amount );
4963     dbms_sql.column_value( p_select_c, 5,
4964                            p_select_rec.accounted_line_amount );
4965     dbms_sql.column_value( p_select_c, 6, p_select_rec.percent );
4966     dbms_sql.column_value( p_select_c, 7, p_select_rec.amount );
4967     dbms_sql.column_value( p_select_c, 8, p_select_rec.acctd_amount );
4968     dbms_sql.column_value( p_select_c, 9, p_select_rec.account_class );
4969     dbms_sql.column_value( p_select_c, 10, p_select_rec.account_set_flag );
4970     dbms_sql.column_value( p_select_c, 11, p_select_rec.cust_trx_type_id );
4971     dbms_sql.column_value( p_select_c, 12,
4972                            p_select_rec.allow_not_open_flag );
4973     dbms_sql.column_value( p_select_c, 13,
4974                            p_select_rec.concatenated_segments );
4975     dbms_sql.column_value( p_select_c, 14, p_select_rec.code_combination_id );
4976     dbms_sql.column_value( p_select_c, 15, p_select_rec.gl_date );
4977     dbms_sql.column_value( p_select_c, 16, p_select_rec.original_gl_date );
4978     dbms_sql.column_value( p_select_c, 17, p_select_rec.ussgl_trx_code );
4979     dbms_sql.column_value( p_select_c, 18,
4980                            p_select_rec.ussgl_trx_code_context );
4981     dbms_sql.column_value( p_select_c, 19, p_select_rec.salesrep_id );
4982     dbms_sql.column_value( p_select_c, 20, p_select_rec.inventory_item_id );
4983     dbms_sql.column_value( p_select_c, 21, p_select_rec.memo_line_id );
4984     dbms_sql.column_value( p_select_c, 22, p_select_rec.default_tax_ccid );
4985     dbms_sql.column_value( p_select_c, 23, p_select_rec.interim_tax_ccid );
4986     dbms_sql.column_value( p_select_c, 24, p_select_rec.site_use_id );
4987     dbms_sql.column_value( p_select_c, 25, p_select_rec.warehouse_id );
4988     -- 1651593
4989     dbms_sql.column_value( p_select_c, 26, p_select_rec.link_to_cust_trx_line_id);
4990 
4991     /* 5148504 - return null value when interim_tax_ccid comes
4992          back from SELECT as -1 */
4993     IF p_select_rec.interim_tax_ccid = -1
4994     THEN
4995        p_select_rec.interim_tax_ccid := NULL;
4996     END IF;
4997 
4998     print_fcn_label2( 'arp_auto_accounting.get_column_values()-' );
4999 EXCEPTION
5000     WHEN OTHERS THEN
5001         debug('EXCEPTION: arp_auto_accounting.get_column_values()',
5002 		MSG_LEVEL_BASIC);
5003         debug(SQLERRM, MSG_LEVEL_BASIC);
5004         RAISE;
5005 END get_column_values;
5006 
5007 ----------------------------------------------------------------------------
5008 PROCEDURE get_column_values( p_select_c   IN  INTEGER,
5009                              p_select_tab OUT NOCOPY select_rec_tab ) IS
5010 BEGIN
5011     print_fcn_label2( 'arp_auto_accounting.get_column_values(tab)+' );
5012 
5013     dbms_sql.column_value( p_select_c, 1, p_select_tab.customer_trx_id );
5014     dbms_sql.column_value( p_select_c, 2, p_select_tab.customer_trx_line_id );
5015     dbms_sql.column_value( p_select_c, 3,
5016                            p_select_tab.cust_trx_line_salesrep_id );
5017     dbms_sql.column_value( p_select_c, 4, p_select_tab.line_amount );
5018     dbms_sql.column_value( p_select_c, 5,
5019                            p_select_tab.accounted_line_amount );
5020     dbms_sql.column_value( p_select_c, 6, p_select_tab.percent );
5021     dbms_sql.column_value( p_select_c, 7, p_select_tab.amount );
5022     dbms_sql.column_value( p_select_c, 8, p_select_tab.acctd_amount );
5023     dbms_sql.column_value( p_select_c, 9, p_select_tab.account_class );
5024     dbms_sql.column_value( p_select_c, 10, p_select_tab.account_set_flag );
5025     dbms_sql.column_value( p_select_c, 11, p_select_tab.cust_trx_type_id );
5026     dbms_sql.column_value( p_select_c, 12,
5027                            p_select_tab.allow_not_open_flag );
5028     dbms_sql.column_value( p_select_c, 13,
5029                            p_select_tab.concatenated_segments );
5030     dbms_sql.column_value( p_select_c, 14, p_select_tab.code_combination_id );
5031     dbms_sql.column_value( p_select_c, 15, p_select_tab.gl_date );
5032     dbms_sql.column_value( p_select_c, 16, p_select_tab.original_gl_date );
5033     dbms_sql.column_value( p_select_c, 17, p_select_tab.ussgl_trx_code );
5034     dbms_sql.column_value( p_select_c, 18,
5035                            p_select_tab.ussgl_trx_code_context );
5036     dbms_sql.column_value( p_select_c, 19, p_select_tab.salesrep_id );
5037     dbms_sql.column_value( p_select_c, 20, p_select_tab.inventory_item_id );
5038     dbms_sql.column_value( p_select_c, 21, p_select_tab.memo_line_id );
5039     dbms_sql.column_value( p_select_c, 22, p_select_tab.default_tax_ccid );
5040     dbms_sql.column_value( p_select_c, 23, p_select_tab.interim_tax_ccid );
5041     dbms_sql.column_value( p_select_c, 24, p_select_tab.site_use_id );
5042     dbms_sql.column_value( p_select_c, 25, p_select_tab.warehouse_id );
5043     -- 1651593
5044     dbms_sql.column_value( p_select_c, 26, p_select_tab.link_to_cust_trx_line_id);
5045 
5046     print_fcn_label2( 'arp_auto_accounting.get_column_values(tab)-' );
5047 EXCEPTION
5048     WHEN OTHERS THEN
5049         debug('EXCEPTION: arp_auto_accounting.get_column_values()',
5050 		MSG_LEVEL_BASIC);
5051         debug(SQLERRM, MSG_LEVEL_BASIC);
5052         RAISE;
5053 END get_column_values;
5054 
5055 
5056 ----------------------------------------------------------------------------
5057 PROCEDURE correct_rounding_errors( select_record 	IN OUT NOCOPY select_rec_type,
5058                                    total_percent 	IN OUT NOCOPY NUMBER,
5059                                    total_amount 	IN OUT NOCOPY NUMBER,
5060                                    total_acctd_amount 	IN OUT NOCOPY NUMBER) IS
5061 BEGIN
5062     print_fcn_label2( 'arp_auto_accounting.correct_rounding_errors()+' );
5063 
5064     -- update totals
5065 
5066     total_percent := total_percent + select_record.percent;
5067     total_amount := total_amount + select_record.amount;
5068     total_acctd_amount := total_acctd_amount + select_record.acctd_amount;
5069 
5070     -- check total percent
5071     IF( total_percent = 100 ) THEN
5072 
5073         -- entered amount
5074         select_record.amount := select_record.amount +
5075                               ( select_record.line_amount - total_amount );
5076         total_amount := 0;
5077         total_percent := 0;
5078 
5079         -- acctd amount
5080         select_record.acctd_amount :=
5081 			select_record.acctd_amount +
5082                 		( select_record.accounted_line_amount -
5083                                   total_acctd_amount );
5084         total_acctd_amount := 0;
5085 
5086     ELSIF( total_percent = 0 ) THEN
5087 
5088         -- entered amount
5089         select_record.amount := select_record.amount - total_amount;
5090         total_amount := 0;
5091         total_percent := 0;
5092 
5093         -- acctd amount
5094         select_record.acctd_amount := select_record.acctd_amount -
5095                                       total_acctd_amount;
5096         total_acctd_amount := 0;
5097 
5098     END IF;
5099 
5100     print_fcn_label2( 'arp_auto_accounting.correct_rounding_errors()-' );
5101 
5102 EXCEPTION
5103     WHEN OTHERS THEN
5104         debug('EXCEPTION: arp_auto_accounting.correct_rounding_errors()',
5105 		MSG_LEVEL_BASIC);
5106         debug(SQLERRM, MSG_LEVEL_BASIC);
5107         RAISE;
5108 END correct_rounding_errors;
5109 
5110 
5111 ----------------------------------------------------------------------------
5112 PROCEDURE insert_dist_row( p_system_info  IN
5113                               arp_trx_global.system_info_rec_type,
5114                             p_profile_info IN
5115                               arp_trx_global.profile_rec_type,
5116                             p_request_id   IN BINARY_INTEGER,
5117                             p_select_tab   IN select_rec_tab,
5118 			    p_low  IN NUMBER,
5119                             p_high IN NUMBER )  IS
5120 
5121 l_gl_dist_key_value_list gl_ca_utility_pkg.r_key_value_arr;   /* mrc */
5122 --Bug#2750340
5123 l_xla_event      arp_xla_events.xla_events_type;
5124 inext            NUMBER := 0;
5125 inow             NUMBER := 0;
5126 
5127 BEGIN
5128     print_fcn_label2( 'arp_auto_accounting.insert_dist_row()+' );
5129 
5130    /* Bug 2560036 - modified insert to set rec_offset_flag in
5131       support of directly inserted UNEARN rows for RAM-C */
5132 
5133    FORALL i IN p_low..p_high
5134     INSERT into ra_cust_trx_line_gl_dist
5135     (
5136       cust_trx_line_gl_dist_id,
5137       created_by,
5138       creation_date,
5139       last_updated_by,
5140       last_update_date,
5141       set_of_books_id,
5142       request_id,
5143       customer_trx_id,
5144       customer_trx_line_id,
5145       cust_trx_line_salesrep_id,
5146       percent,
5147       amount,
5148       acctd_amount,
5149       account_class,
5150       account_set_flag,
5151       concatenated_segments,
5152       code_combination_id,
5153       gl_date,
5154       original_gl_date,
5155       ussgl_transaction_code,
5156       ussgl_transaction_code_context,
5157       posting_control_id,
5158       latest_rec_flag,
5159       collected_tax_concat_seg,
5160       collected_tax_ccid,
5161       rec_offset_flag
5162       ,org_id
5163     )
5164     VALUES
5165     (
5166       ra_cust_trx_line_gl_dist_s.nextval,
5167       p_profile_info.user_id,
5168       sysdate,
5169       p_profile_info.user_id,
5170       sysdate,
5171       p_system_info.system_parameters.set_of_books_id,
5172       p_request_id,
5173       p_select_tab.customer_trx_id(i),
5174       p_select_tab.customer_trx_line_id(i),
5175       p_select_tab.cust_trx_line_salesrep_id(i),
5176       round(nvl(p_select_tab.percent(i), 0), 4),
5177       decode(p_select_tab.account_set_flag(i),
5178 	     'Y', null, p_select_tab.amount(i)),
5179       decode(p_select_tab.account_set_flag(i),
5180              'Y', null, p_select_tab.acctd_amount(i)),
5181       p_select_tab.account_class(i),
5182       p_select_tab.account_set_flag(i),
5183       decode(p_select_tab.int_code_combination_id(i),
5184              '', decode(p_select_tab.code_combination_id(i),
5185                         -1, p_select_tab.concatenated_segments(i),
5186                         NULL ),
5187              -1, p_select_tab.int_concatenated_segments(i),
5188              NULL),
5189       decode(p_select_tab.int_code_combination_id(i),
5190              '', p_select_tab.code_combination_id(i),
5191              p_select_tab.int_code_combination_id(i)),
5192       to_date(p_select_tab.gl_date(i), 'J'),
5193       to_date(p_select_tab.original_gl_date(i), 'J'),
5194       p_select_tab.ussgl_trx_code(i),
5195       p_select_tab.ussgl_trx_code_context(i),
5196       -3,
5197       decode( p_select_tab.account_class(i),
5198               'REC', 'Y',
5199               NULL),
5200       decode(p_select_tab.int_code_combination_id(i),
5201              '',NULL,
5202              decode(p_select_tab.code_combination_id(i),
5203                      -1, p_select_tab.concatenated_segments(i),
5204                     NULL)),
5205       decode(p_select_tab.int_code_combination_id(i),
5206              '',NULL,
5207              p_select_tab.code_combination_id(i)),
5208       DECODE(p_select_tab.account_set_flag(i), 'Y', NULL,
5209         DECODE(p_select_tab.account_class(i), 'UNEARN', 'Y', NULL))
5210        ,arp_standard.sysparm.org_id --anuj
5211     )
5212    RETURNING cust_trx_line_gl_dist_id
5213    BULK COLLECT INTO l_gl_dist_key_value_list;
5214 
5215    /* only insert the MRC gl_dist data if this has been called from
5216       forms.  For autoinv this insert is handled differently by
5217       request_id.
5218       -- Added by Bsarkar
5219       The g_called_from is introduced to stop the call for Invoice Creation
5220       API. In case AUTO_ACCOUNTING is called from Tax engine for Invoice API
5221       this variable
5222       will have different value and won't execute the MRC call. The MRC call
5223       for invoice creation API is handled based on request Id and this call is
5224       not required. */
5225 
5226 
5227    IF (p_request_id IS NULL AND g_called_from = 'FORMS' ) THEN
5228          IF PG_DEBUG in ('Y', 'C') THEN
5229             arp_util.debug('calling mrc engine for insertion of gl dist data');
5230          END IF;
5231       --BUG#2750340
5232        FOR i IN p_low .. p_high LOOP
5233          inext := p_select_tab.customer_trx_id(i);
5234          IF inext <> inow THEN
5235            l_xla_event.xla_from_doc_id  := p_select_tab.customer_trx_id(i);
5236            l_xla_event.xla_to_doc_id    := p_select_tab.customer_trx_id(i);
5237            l_xla_event.xla_req_id       := NULL;
5238            l_xla_event.xla_dist_id      := NULL;
5239            l_xla_event.xla_doc_table    := 'CT';
5240            l_xla_event.xla_doc_event    := NULL;
5241            l_xla_event.xla_mode         := 'O';
5242            l_xla_event.xla_call         := 'B';
5243 
5244            ARP_XLA_EVENTS.CREATE_EVENTS(p_xla_ev_rec => l_xla_event );
5245            inow := inext;
5246          END IF;
5247       END LOOP;
5248    END IF;
5249 
5250     print_fcn_label2( 'arp_auto_accounting.insert_dist_row()-' );
5251 EXCEPTION
5252     WHEN OTHERS THEN
5253         debug('EXCEPTION: arp_auto_accounting.insert_dist_row()',
5254 		MSG_LEVEL_BASIC);
5255         debug(SQLERRM, MSG_LEVEL_BASIC);
5256         RAISE;
5257 END insert_dist_row;
5258 
5259 ----------------------------------------------------------------------------
5260 PROCEDURE dump_select_rec( p_select_rec IN select_rec_type ) IS
5261 BEGIN
5262     print_fcn_label2( 'arp_auto_accounting.dump_select_rec()+' );
5263 
5264     debug( '  Dumping select record: ', MSG_LEVEL_DEBUG );
5265     debug( '  customer_trx_id='
5266            || to_char( p_select_rec.customer_trx_id ), MSG_LEVEL_DEBUG );
5267     debug( '  customer_trx_line_id='
5268            || to_char( p_select_rec.customer_trx_line_id ), MSG_LEVEL_DEBUG );
5269     debug( '  customer_trx_line_salesrep_id='
5270           || to_char( p_select_rec.cust_trx_line_salesrep_id ),
5271           MSG_LEVEL_DEBUG );
5272     debug( '  line_amount='
5273            || to_char( p_select_rec.line_amount ), MSG_LEVEL_DEBUG );
5274     debug( '  accounted_line_amount='
5275            || to_char( p_select_rec.accounted_line_amount ), MSG_LEVEL_DEBUG );
5276     debug( '  percent='
5277            || to_char( p_select_rec.percent ), MSG_LEVEL_DEBUG );
5278     debug( '  amount='
5279            || to_char( p_select_rec.amount ), MSG_LEVEL_DEBUG );
5280     debug( '  acctd_amount='
5281            || to_char( p_select_rec.acctd_amount ), MSG_LEVEL_DEBUG );
5282     debug( '  account_class=' || p_select_rec.account_class, MSG_LEVEL_DEBUG );
5283     debug( '  account_set_flag=' || p_select_rec.account_set_flag,
5284            MSG_LEVEL_DEBUG );
5285     debug( '  cust_trx_type_id='
5286            || to_char( p_select_rec.cust_trx_type_id ), MSG_LEVEL_DEBUG );
5287     debug( '  allow_not_open_flag=' ||
5288            p_select_rec.allow_not_open_flag, MSG_LEVEL_DEBUG );
5289     debug( '  concatenated_segments='
5290            || p_select_rec.concatenated_segments, MSG_LEVEL_DEBUG );
5291     debug( '  code_combination_id='
5292            || to_char( p_select_rec.code_combination_id ), MSG_LEVEL_DEBUG );
5293     debug( '  gl_date=' || p_select_rec.gl_date, MSG_LEVEL_DEBUG );
5294     debug( '  original_gl_date=' || p_select_rec.original_gl_date,
5295            MSG_LEVEL_DEBUG );
5296     debug( '  ussgl_trx_code=' || p_select_rec.ussgl_trx_code, MSG_LEVEL_DEBUG );
5297     debug( '  ussgl_trx_code_context='
5298            || p_select_rec.ussgl_trx_code_context, MSG_LEVEL_DEBUG );
5299     debug( '  salesrep_id='
5300            || to_char( p_select_rec.salesrep_id ), MSG_LEVEL_DEBUG );
5301     debug( '  inventory_item_id='
5302            || to_char( p_select_rec.inventory_item_id ), MSG_LEVEL_DEBUG );
5303     debug( '  memo_line_id='
5304            || to_char( p_select_rec.memo_line_id ), MSG_LEVEL_DEBUG );
5305     debug( '  default_tax_ccid='
5306            || to_char( p_select_rec.default_tax_ccid ), MSG_LEVEL_DEBUG );
5307     debug( '  interim_tax_ccid='
5308            || to_char( p_select_rec.interim_tax_ccid ), MSG_LEVEL_DEBUG );
5309     debug( '  site_use_id='
5310            || to_char( p_select_rec.site_use_id ), MSG_LEVEL_DEBUG );
5311     debug( '  warehouse_id='
5312            || to_char( p_select_rec.warehouse_id ), MSG_LEVEL_DEBUG );
5313 
5314     print_fcn_label2( 'arp_auto_accounting.dump_select_rec()-' );
5315 
5316 EXCEPTION
5317     WHEN OTHERS THEN
5318         debug('EXCEPTION: arp_auto_accounting.dump_select_rec()',
5319 		MSG_LEVEL_BASIC);
5320         debug(SQLERRM, MSG_LEVEL_BASIC);
5321 END dump_select_rec;
5322 
5323 ----------------------------------------------------------------------------
5324 PROCEDURE dump_select_tab( p_select_tab IN select_rec_tab, p_low IN NUMBER, p_high IN NUMBER ) IS
5325 BEGIN
5326     print_fcn_label2( 'arp_auto_accounting.dump_select_tab()+' );
5327 
5328 /* bug 1532372 - changed parameter from l_rows_fetched to l_low and l_high
5329                  this apparently corrects the 1000 REC row limit that
5330                  we encountered during benchmarking.  */
5331 
5332    FOR i in p_low..p_high LOOP
5333     debug( '  Dumping select record: [' || i ||']', MSG_LEVEL_DEBUG );
5334     debug( '  customer_trx_id='
5335            || to_char( p_select_tab.customer_trx_id(i) ), MSG_LEVEL_DEBUG );
5336     debug( '  customer_trx_line_id='
5337            || to_char( p_select_tab.customer_trx_line_id (i)), MSG_LEVEL_DEBUG );
5338     debug( '  customer_trx_line_salesrep_id='
5339           || to_char( p_select_tab.cust_trx_line_salesrep_id(i)),
5340           MSG_LEVEL_DEBUG );
5341     debug( '  line_amount='
5342            || to_char( p_select_tab.line_amount(i) ), MSG_LEVEL_DEBUG );
5343     debug( '  accounted_line_amount='
5344            || to_char( p_select_tab.accounted_line_amount (i)), MSG_LEVEL_DEBUG );
5345     debug( '  percent='
5346            || to_char( p_select_tab.percent(i) ), MSG_LEVEL_DEBUG );
5347     debug( '  amount='
5348            || to_char( p_select_tab.amount(i) ), MSG_LEVEL_DEBUG );
5349     debug( '  acctd_amount='
5350            || to_char( p_select_tab.acctd_amount(i) ), MSG_LEVEL_DEBUG );
5351     debug( '  account_class=' || p_select_tab.account_class(i), MSG_LEVEL_DEBUG );
5352     debug( '  account_set_flag=' || p_select_tab.account_set_flag(i),
5353            MSG_LEVEL_DEBUG );
5354     debug( '  cust_trx_type_id='
5355            || to_char( p_select_tab.cust_trx_type_id(i) ), MSG_LEVEL_DEBUG );
5356     debug( '  allow_not_open_flag=' ||
5357            p_select_tab.allow_not_open_flag(i), MSG_LEVEL_DEBUG );
5358     debug( '  concatenated_segments='
5359            || p_select_tab.concatenated_segments(i), MSG_LEVEL_DEBUG );
5360     debug( '  code_combination_id='
5361            || to_char( p_select_tab.code_combination_id (i)), MSG_LEVEL_DEBUG );
5362     debug( '  gl_date=' || p_select_tab.gl_date(i), MSG_LEVEL_DEBUG );
5363     debug( '  original_gl_date=' || p_select_tab.original_gl_date(i),
5364            MSG_LEVEL_DEBUG );
5365     debug( '  ussgl_trx_code=' || p_select_tab.ussgl_trx_code(i), MSG_LEVEL_DEBUG );
5366     debug( '  ussgl_trx_code_context='
5367            || p_select_tab.ussgl_trx_code_context(i), MSG_LEVEL_DEBUG );
5368     debug( '  salesrep_id='
5369            || to_char( p_select_tab.salesrep_id (i)), MSG_LEVEL_DEBUG );
5370     debug( '  inventory_item_id='
5371            || to_char( p_select_tab.inventory_item_id (i)), MSG_LEVEL_DEBUG );
5372     debug( '  memo_line_id='
5373            || to_char( p_select_tab.memo_line_id (i)), MSG_LEVEL_DEBUG );
5374     debug( '  default_tax_ccid='
5375            || to_char( p_select_tab.default_tax_ccid(i) ), MSG_LEVEL_DEBUG );
5376     debug( '  interim_tax_ccid='
5377            || to_char( p_select_tab.interim_tax_ccid(i) ), MSG_LEVEL_DEBUG );
5378     debug( '  site_use_id='
5379            || to_char( p_select_tab.site_use_id (i)), MSG_LEVEL_DEBUG );
5380     debug( '  warehouse_id='
5381            || to_char( p_select_tab.warehouse_id (i)), MSG_LEVEL_DEBUG );
5382    END LOOP;
5383 
5384     print_fcn_label2( 'arp_auto_accounting.dump_select_tab()-' );
5385 
5386 EXCEPTION
5387     WHEN OTHERS THEN
5388         debug('EXCEPTION: arp_auto_accounting.dump_select_tab()',
5389 		MSG_LEVEL_BASIC);
5390         debug(SQLERRM, MSG_LEVEL_BASIC);
5391 END dump_select_tab;
5392 ----------------------------------------------------------------------------
5393 
5394 FUNCTION get_select_rec(  p_select_tab IN select_rec_tab, p_cnt IN NUMBER )
5395 RETURN select_rec_type AS
5396 
5397    p_select_rec select_rec_type;
5398    i INTEGER:=0;
5399 
5400 BEGIN
5401 
5402    print_fcn_label2( 'arp_auto_accounting.get_select_rec(tab)+' );
5403     debug( '  p_cnt='
5404            || p_cnt, MSG_LEVEL_DEBUG );
5405    i := 1;
5406    p_select_rec.customer_trx_id                     := p_select_tab.customer_trx_id(p_cnt);
5407    i := i+1;
5408    p_select_rec.customer_trx_line_id                := p_select_tab.customer_trx_line_id(p_cnt);
5409    i := i+1;
5410    p_select_rec.cust_trx_line_salesrep_id           := p_select_tab.cust_trx_line_salesrep_id(p_cnt);
5411    i := i+1;
5412    p_select_rec.line_amount                         := p_select_tab.line_amount(p_cnt);
5413    i := i+1; --5
5414    p_select_rec.accounted_line_amount               := p_select_tab.accounted_line_amount(p_cnt);
5415    i := i+1;
5416    p_select_rec.percent                             := p_select_tab.percent(p_cnt);
5417    i := i+1;
5418    p_select_rec.amount                              := p_select_tab.amount(p_cnt);
5419    i := i+1;
5420    p_select_rec.acctd_amount                        := p_select_tab.acctd_amount(p_cnt);
5421    i := i+1;
5422    p_select_rec.account_class                       := p_select_tab.account_class(p_cnt);
5423    i := i+1; --10
5424    p_select_rec.account_set_flag                    := p_select_tab.account_set_flag(p_cnt);
5425    i := i+1;
5426    p_select_rec.cust_trx_type_id                    := p_select_tab.cust_trx_type_id(p_cnt);
5427    i := i+1;
5428    p_select_rec.allow_not_open_flag                 := p_select_tab.allow_not_open_flag(p_cnt);
5429    i := i+1;
5430    p_select_rec.concatenated_segments               := p_select_tab.concatenated_segments(p_cnt);
5431    i := i+1;
5432    p_select_rec.code_combination_id                 := p_select_tab.code_combination_id(p_cnt);
5433    i := i+1; --15
5434    p_select_rec.gl_date                             := p_select_tab.gl_date(p_cnt);
5435    i := i+1;
5436    p_select_rec.original_gl_date                    := p_select_tab.original_gl_date(p_cnt);
5437    i := i+1;
5438    p_select_rec.ussgl_trx_code                      := p_select_tab.ussgl_trx_code(p_cnt);
5439    i := i+1;
5440    p_select_rec.ussgl_trx_code_context              := p_select_tab.ussgl_trx_code_context(p_cnt);
5441    i := i+1;
5442    p_select_rec.salesrep_id                         := p_select_tab.salesrep_id(p_cnt);
5443    i := i+1; --20
5444    p_select_rec.inventory_item_id                   := p_select_tab.inventory_item_id(p_cnt);
5445    i := i+1;
5446    p_select_rec.memo_line_id                        := p_select_tab.memo_line_id(p_cnt);
5447    i := i+1;
5448    p_select_rec.default_tax_ccid                    := p_select_tab.default_tax_ccid(p_cnt);
5449    i := i+1;
5450 
5451    /* 5148504 - Insure that interim_tax_ccid returns as null
5452        if etax/arp_etax_util returns it as -1.  Otherwise, this
5453        artificially acts as if it is deferred tax. */
5454    IF p_select_tab.interim_tax_ccid(p_cnt) = -1
5455    THEN
5456       p_select_rec.interim_tax_ccid := NULL;
5457    ELSE
5458       p_select_rec.interim_tax_ccid := p_select_tab.interim_tax_ccid(p_cnt);
5459    END IF;
5460    i := i+1; --24
5461 --   p_select_rec.int_concatenated_segments           := p_select_tab.int_concatenated_segments(p_cnt);
5462    i := i+1; --25
5463 --   p_select_rec.int_code_combination_id             := p_select_tab.int_code_combination_id(p_cnt);
5464    i := i+1;
5465    p_select_rec.site_use_id                         := p_select_tab.site_use_id(p_cnt);
5466    i := i+1;
5467    p_select_rec.warehouse_id                        := p_select_tab.warehouse_id(p_cnt);
5468    -- 1651593
5469    i := i+1;
5470    p_select_rec.link_to_cust_trx_line_id            := p_select_tab.link_to_cust_trx_line_id(p_cnt);
5471 
5472    return(p_select_rec);
5473 
5474 EXCEPTION
5475     WHEN OTHERS THEN
5476         debug('EXCEPTION: arp_auto_accounting.get_select_rec():'|| i, MSG_LEVEL_BASIC);
5477         debug(SQLERRM, MSG_LEVEL_BASIC);
5478         RAISE;
5479 
5480 END;
5481 
5482 ----------------------------------------------------------------------------
5483 PROCEDURE process_line( p_system_info 		IN
5484                           arp_trx_global.system_info_rec_type,
5485                         p_select_rec  		IN OUT NOCOPY select_rec_type,
5486                         p_total_percent 	IN OUT NOCOPY NUMBER,
5487                         p_total_amount 		IN OUT NOCOPY NUMBER,
5488                         p_total_acctd_amount 	IN OUT NOCOPY NUMBER,
5489                         p_failure_count	 	IN OUT NOCOPY BINARY_INTEGER,
5490                         p_mode                  IN VARCHAR2,
5491                         p_request_id            IN BINARY_INTEGER )  IS
5492 
5493     l_boolean  			BOOLEAN;
5494     l_default_gl_date 		DATE;
5495     l_default_rule_used 	VARCHAR2(50);
5496     l_error_message		VARCHAR2(256);
5497 
5498 BEGIN
5499     --
5500     -- Default gl date if in closed period
5501     --
5502     debug( '  Defaulting gl_date', MSG_LEVEL_DEBUG );
5503 
5504     IF( p_select_rec.gl_date IS NOT NULL ) THEN
5505 
5506         l_boolean :=
5507             arp_standard.validate_and_default_gl_date
5508                 ( to_date(p_select_rec.gl_date, 'J'),
5509                   to_date(p_select_rec.original_gl_date, 'J'),
5510                   NULL,
5511                   NULL,
5512                   NULL,
5513                   NULL,
5514                   NULL,
5515                   NULL,
5516                   p_select_rec.allow_not_open_flag,
5517                   NULL,
5518                   p_system_info.system_parameters.set_of_books_id,
5519                   222,
5520                   l_default_gl_date,
5521                   l_default_rule_used,
5522                   l_error_message );
5523 
5524         p_select_rec.gl_date := to_char( l_default_gl_date, 'J' );
5525 
5526         IF( l_boolean ) THEN
5527             debug( '  Using default gl date of ' ||
5528                    to_char( l_default_gl_date ), MSG_LEVEL_DEBUG );
5529             debug( '  derived by rule ' ||
5530                    l_default_rule_used, MSG_LEVEL_DEBUG );
5531 
5532         ELSE
5533             --
5534             -- defaulting gl_date failure
5535             --
5536             g_error_buffer := l_error_message;
5537 	    debug( 'EXCEPTION: '||g_error_buffer, MSG_LEVEL_BASIC );
5538             RAISE error_defaulting_gl_date;
5539         END IF;
5540     END IF;
5541 
5542     --
5543     -- Correct rounding errors
5544     --
5545     debug( '  Correcting rounding errors', MSG_LEVEL_DEBUG );
5546 
5547     correct_rounding_errors( p_select_rec,
5548                              p_total_percent,
5549                              p_total_amount,
5550                              p_total_acctd_amount );
5551 
5552     --
5553     -- Set the CCID validation date
5554     --
5555  /* Bug 2142306 - added NVL function */
5556     validation_date := NVL(TO_DATE(p_select_rec.gl_date, 'J'), G_SYS_DATE);
5557 
5558 
5559 /* Bug 2560036 - Change account class to 'UNEARN' if
5560    the transaction fails collectibility */
5561 
5562     IF (p_select_rec.account_class = 'REV' AND
5563         p_select_rec.account_set_flag = 'N' AND
5564         g_test_collectibility) THEN
5565 
5566         /* Bug 3440172/3446698 - Conflict between autoaccounting
5567            and collectibility causing imported DMs and on acct
5568            CMs to be missing REV distributions */
5569         /* Bug 4693399 - manually entered transactions can also be deferred
5570 	   by contingencies */
5571        /*Bug 9112739 added for run in update mode */
5572        IF (p_mode in ( 'I' ,'U') AND
5573            t_collect.EXISTS(p_select_rec.customer_trx_line_id))
5574        THEN
5575           IF (t_collect(p_select_rec.customer_trx_line_id) =
5576                     ar_revenue_management_pvt.defer) THEN
5577 
5578              p_select_rec.account_class := 'UNEARN';
5579              p_select_rec.code_combination_id := NULL;
5580 
5581           END IF;
5582 
5583        END IF;
5584 
5585 /*  We originally toyed with implementing
5586     collectibility for when a user maintains (or changes)
5587     a transaction.  But it was decided that for the initial
5588     go of this, a user steps outside of collectibility if they
5589     make any changes to a collectibility-deferred transaction. */
5590 
5591     END IF;
5592 
5593 /* End bug 2560036 */
5594 
5595     --
5596     -- Call Flex manager
5597     --
5598     IF( p_select_rec.code_combination_id IS NULL ) THEN
5599 
5600         flex_manager( p_select_rec.account_class,
5601                       p_select_rec.customer_trx_line_id,
5602                       p_select_rec.cust_trx_type_id,
5603                       p_select_rec.salesrep_id,
5604                       p_select_rec.inventory_item_id,
5605                       p_select_rec.memo_line_id,
5606                       p_select_rec.default_tax_ccid,
5607                       p_select_rec.interim_tax_ccid,
5608                       p_select_rec.site_use_id,
5609                       p_select_rec.warehouse_id,
5610                       p_select_rec.code_combination_id,
5611                       p_select_rec.concatenated_segments,
5612                       p_select_rec.int_code_combination_id,
5613                       p_select_rec.int_concatenated_segments );
5614     END IF;
5615 
5616     IF( p_select_rec.code_combination_id = -1 ) THEN
5617 
5618         -- keep track of # rows where ccid was not found
5619         -- if > 0, then need to call AOL dynamic insert
5620         -- on the client-side
5621         --
5622         p_failure_count	 := nvl(p_failure_count, 0) + 1;
5623 
5624         debug('process_line:  Failure count : '||to_char(p_failure_count),
5625               MSG_LEVEL_DEBUG);
5626     END IF;
5627 
5628     --
5629     --Increment failure count if invalid ccid for interim tax account
5630     --
5631 
5632     IF( p_select_rec.int_code_combination_id = -1 ) THEN
5633 
5634         p_failure_count	 := nvl(p_failure_count, 0) + 1;
5635 
5636         debug('process_line:  Failure count : '||to_char(p_failure_count),
5637               MSG_LEVEL_DEBUG);
5638     END IF;
5639 
5640 END process_line;
5641 
5642 
5643 --
5644 --
5645 -- FUNCTION NAME:  do_autoaccounting
5646 --
5647 -- DECSRIPTION:
5648 --   Server-side entry point for autoaccounting.
5649 --   This is a cover function which calls the procedure do_autoaccounting
5650 --   and exists for backward compatibility.  New programs should use
5651 --   the procedure instead of the function.
5652 --
5653 -- ARGUMENTS:
5654 --      IN:
5655 --        mode:  May be I(nsert), U(pdate), D(elete), or (G)et
5656 --        account_class:  REC, REV, FREIGHT, TAX, UNBILL, UNEARN, SUSPENSE,
5657 --                        CHARGES
5658 --        customer_trx_id:  NULL if not applicable
5659 --        customer_trx_line_id:  NULL if not applicable
5660 --        cust_trx_line_salesrep_id:  NULL if not applicable
5661 --        request_id:  NULL if not applicable
5662 --        gl_date:  GL date of the account assignment
5663 --        original_gl_date:  Original GL date
5664 --        total_trx_amount:  For Receivable account only
5665 --        passed_ccid:  Code comination ID to use if supplied
5666 --        force_account_set_no:
5667 --        cust_trx_type_id:
5668 --        primary_salesrep_id
5669 --        inventory_item_id
5670 --        memo_line_id
5671 --        msg_level
5672 --
5673 --      IN/OUT:
5674 --        ccid
5675 --        concat_segments
5676 --        num_dist_rows_failed
5677 --        errorbuf
5678 --
5679 --      OUT:
5680 --
5681 -- RETURNS:
5682 --   1 if no errors in deriving ccids and creating distributions,
5683 --   0 if one or more rows where ccid could not be found,
5684 --   Exception raised if SQL error or other fatal error.
5685 --
5686 -- NOTES:
5687 --
5688 -- HISTORY:
5689 --
5690 FUNCTION do_autoaccounting( p_mode IN VARCHAR2,
5691                             p_account_class IN VARCHAR2,
5692                             p_customer_trx_id IN NUMBER,
5693                             p_customer_trx_line_id IN NUMBER,
5694                             p_cust_trx_line_salesrep_id IN NUMBER,
5695                             p_request_id IN NUMBER,
5696                             p_gl_date IN DATE,
5697                             p_original_gl_date IN DATE,
5698                             p_total_trx_amount IN NUMBER,
5699                             p_passed_ccid IN NUMBER,
5700                             p_force_account_set_no IN VARCHAR2,
5701                             p_cust_trx_type_id IN NUMBER,
5702                             p_primary_salesrep_id IN NUMBER,
5703                             p_inventory_item_id IN NUMBER,
5704                             p_memo_line_id IN NUMBER,
5705                             p_ccid IN OUT NOCOPY NUMBER,
5706                             p_concat_segments IN OUT NOCOPY VARCHAR2,
5707                             p_num_failed_dist_rows IN OUT NOCOPY NUMBER,
5708                             p_errorbuf IN OUT NOCOPY VARCHAR2,
5709                             p_msg_level IN NUMBER  )
5710   RETURN NUMBER IS
5711 
5712     l_temp                      BINARY_INTEGER;
5713 
5714 BEGIN
5715 
5716     g_errorbuf := NULL;
5717 
5718     --------------------------------------------------------------------------
5719     -- Set message level for debugging
5720     --------------------------------------------------------------------------
5721     system_info.msg_level := p_msg_level;
5722     arp_global.msg_level := p_msg_level;
5723 
5724     print_fcn_label( 'arp_auto_accounting.do_autoaccounting_cover()+ ' );
5725 
5726     --------------------------------------------------------------------------
5727     -- Initialize
5728     --------------------------------------------------------------------------
5729     p_errorbuf := NULL;
5730 
5731     do_autoaccounting( p_mode,
5732                        p_account_class,
5733                        p_customer_trx_id,
5734                        p_customer_trx_line_id,
5735                        p_cust_trx_line_salesrep_id,
5736                        p_request_id,
5737                        p_gl_date,
5738                        p_original_gl_date,
5739                        p_total_trx_amount,
5740                        p_passed_ccid,
5741                        p_force_account_set_no,
5742                        p_cust_trx_type_id,
5743                        p_primary_salesrep_id,
5744                        p_inventory_item_id,
5745                        p_memo_line_id,
5746                        p_ccid,
5747                        p_concat_segments,
5748                        p_num_failed_dist_rows );
5749 
5750 
5751     print_fcn_label( 'arp_auto_accounting.do_autoaccounting_cover()- ' );
5752 
5753     IF (( p_mode = G AND p_ccid = -1 )
5754         OR
5755         ( p_request_id IS NOT NULL AND p_num_failed_dist_rows > 0)) THEN
5756 
5757 	RETURN 0;	-- no ccid was created
5758 
5759     END IF;
5760 
5761     RETURN 1;
5762 
5763 
5764 EXCEPTION
5765     WHEN no_ccid THEN
5766 	RETURN 0;	-- could not get valid ccid, failure
5767 
5768     WHEN NO_DATA_FOUND THEN
5769   	RETURN 1;  	-- treat this as success
5770 
5771     WHEN OTHERS THEN
5772 	g_errorbuf := g_error_buffer;
5773         debug(SQLERRM, MSG_LEVEL_BASIC);
5774         RAISE;
5775 END do_autoaccounting;
5776 
5777 
5778 ----------------------------------------------------------------------------
5779 PROCEDURE do_autoaccounting_internal(
5780 			    p_mode 			IN VARCHAR2,
5781                             p_account_class 		IN VARCHAR2,
5782                             p_customer_trx_id 		IN NUMBER,
5783                             p_customer_trx_line_id 	IN NUMBER,
5784                             p_cust_trx_line_salesrep_id IN NUMBER,
5785                             p_request_id 		IN NUMBER,
5786                             p_gl_date 			IN DATE,
5787                             p_original_gl_date 		IN DATE,
5788                             p_total_trx_amount 		IN NUMBER,
5789                             p_passed_ccid 		IN NUMBER,
5790                             p_force_account_set_no 	IN VARCHAR2,
5791                             p_cust_trx_type_id 		IN NUMBER,
5792                             p_primary_salesrep_id 	IN NUMBER,
5793                             p_inventory_item_id 	IN NUMBER,
5794                             p_memo_line_id 		IN NUMBER,
5795                             p_site_use_id               IN NUMBER,
5796                             p_warehouse_id              IN NUMBER,
5797                             p_ccid 			IN OUT NOCOPY NUMBER,
5798                             p_concat_segments 		IN OUT NOCOPY VARCHAR2,
5799                             p_failure_count	 	IN OUT NOCOPY NUMBER )
5800 IS
5801 
5802 
5803     l_select_rec select_rec_type;
5804     l_select_tab select_rec_tab;
5805     l_null_rec   CONSTANT select_rec_type := l_select_rec;
5806 
5807     -- Cursors
5808     --
5809     l_select_c INTEGER;
5810     l_delete_c INTEGER;
5811 
5812     --
5813     -- Running totals
5814     --
5815     l_total_percent 		NUMBER := 0;
5816     l_total_amount 		NUMBER := 0;
5817     l_total_acctd_amount 	NUMBER := 0;
5818 
5819     l_rows_fetched 	           NUMBER := 0;
5820 
5821     l_ignore   			INTEGER;
5822     l_boolean  			BOOLEAN;
5823     l_first_fetch		BOOLEAN;
5824     l_temp                      BINARY_INTEGER;
5825     l_keep_cursor_open_flag     BOOLEAN := FALSE;
5826 
5827     l_low                       INTEGER:=0;
5828     l_high                      INTEGER:=0;
5829 
5830     gl_dist_array               dbms_sql.number_table;    /* MRC */
5831     l_error_count               NUMBER := 0;
5832     l_use_unearn_srep_dependency BOOLEAN := FALSE;
5833 
5834     /*9112739*/
5835     l_collect                ar_revenue_management_pvt.long_number_table;
5836     cursor c_trx_lines(p_customer_trx_id in number) IS
5837     select customer_trx_line_id from ra_customer_trx_lines
5838     where customer_trx_id = p_customer_trx_id
5839     and line_type = 'LINE';
5840 
5841 BEGIN
5842 
5843     print_fcn_label( 'arp_auto_accounting.do_autoaccounting_internal()+' );
5844    --begin anuj
5845 /* Multi-Org Access Control Changes for SSA;Begin;anukumar;11/01/2002*/
5846    init;
5847 /* Multi-Org Access Control Changes for SSA;Begin;anukumar;11/01/2002*/
5848     --end anuj
5849 
5850 
5851     SAVEPOINT ar_auto_accounting;
5852 
5853     --------------------------------------------------------------------------
5854     -- Process modes
5855     --------------------------------------------------------------------------
5856     IF( p_mode = G ) THEN
5857         --
5858         -- Get mode, populate record immediately
5859         --
5860         l_select_rec := l_null_rec;     -- start with null record
5861 
5862         l_select_rec.customer_trx_line_id := p_customer_trx_line_id;
5863         l_select_rec.account_class := p_account_class;
5864         l_select_rec.cust_trx_type_id := p_cust_trx_type_id;
5865         l_select_rec.salesrep_id := p_primary_salesrep_id;
5866         l_select_rec.inventory_item_id := p_inventory_item_id;
5867         l_select_rec.memo_line_id := p_memo_line_id;
5868         l_select_rec.site_use_id := p_site_use_id;
5869         l_select_rec.warehouse_id := p_warehouse_id;
5870 
5871 	dump_select_rec( l_select_rec );
5872 
5873         --------------------------------------------------------------------
5874         -- Default gl date if in closed period
5875         -- Correct rounding errors
5876         -- Call Flex mgr
5877         --------------------------------------------------------------------
5878 
5879         process_line( system_info,
5880                       l_select_rec,
5881                       l_total_percent,
5882                       l_total_amount,
5883                       l_total_acctd_amount,
5884                       p_failure_count,
5885                       p_mode,
5886                       p_request_id );
5887 
5888         --------------------------------------------------------------------
5889         -- Update IN OUT NOCOPY parameters for output to Form fields
5890         --------------------------------------------------------------------
5891         p_ccid := l_select_rec.code_combination_id;
5892         p_concat_segments := l_select_rec.concatenated_segments;
5893 
5894     ELSE -- I, U or D modes
5895 
5896 
5897         IF( p_mode in (U, D) ) THEN
5898     	    --
5899     	    -- Delete distributions in Update and Delete mode
5900 	    --
5901 
5902             ----------------------------------------------------------------
5903             -- Construct delete stmt
5904             ----------------------------------------------------------------
5905             DECLARE
5906                 l_delete_stmt VARCHAR2(32767);
5907 
5908             BEGIN
5909 
5910               l_delete_c := dbms_sql.open_cursor;
5911               l_delete_stmt := build_delete_sql( system_info,
5912                                                profile_info,
5913                                                p_account_class,
5914                                                p_customer_trx_id,
5915                                                p_customer_trx_line_id,
5916                                                p_cust_trx_line_salesrep_id,
5917                                                p_request_id );
5918 
5919               l_delete_stmt := l_delete_stmt ||
5920                  ' RETURNING cust_trx_line_gl_dist_id INTO :gl_dist_key_value ';
5921 
5922               dbms_sql.parse( l_delete_c, l_delete_stmt, dbms_sql.v7 );
5923 
5924              /*-----------------------+
5925               | bind output variable  |
5926               +-----------------------*/
5927               dbms_sql.bind_array(l_delete_c,':gl_dist_key_value',
5928                                   gl_dist_array);
5929 
5930 
5931             EXCEPTION
5932                 WHEN OTHERS THEN
5933                   debug( 'Error constructing/parsing delete cursor',
5934                          MSG_LEVEL_BASIC );
5935                   debug(SQLERRM, MSG_LEVEL_BASIC);
5936                   RAISE;
5937 
5938             END;
5939 
5940             ----------------------------------------------------------------
5941             -- Delete distributions
5942             ----------------------------------------------------------------
5943             debug( '  Deleting distributions', MSG_LEVEL_DEBUG );
5944 
5945             BEGIN
5946                 l_ignore := dbms_sql.execute( l_delete_c );
5947 
5948                 debug( to_char(l_ignore) || ' row(s) deleted',
5949 			MSG_LEVEL_DEBUG );
5950 
5951                 IF ( l_ignore > 0) THEN
5952                    /*------------------------------------------+
5953                     | get RETURNING COLUMN into OUT NOCOPY bind array |
5954                     +------------------------------------------*/
5955 
5956                     dbms_sql.variable_value( l_delete_c, ':gl_dist_key_value',
5957                                  gl_dist_array);
5958 
5959                     IF PG_DEBUG in ('Y', 'C') THEN
5960                        arp_standard.debug('do_autoaccounting: ' || 'before loop for MRC processing...');
5961                     END IF;
5962                     FOR I in gl_dist_array.FIRST .. gl_dist_array.LAST LOOP
5963                    /*---------------------------------------------------------+
5964                     | call mrc engine to delete from ra_cust_trx_line_gl_dist |
5965                     +---------------------------------------------------------*/
5966                     IF PG_DEBUG in ('Y', 'C') THEN
5967                        arp_standard.debug('do_autoaccounting: ' || 'before calling maintain_mrc ');
5968                        arp_standard.debug('do_autoaccounting: ' || 'gl dist array('||to_char(I) || ') = ' ||
5969                                         to_char(gl_dist_array(I)));
5970                     END IF;
5971 
5972                     ar_mrc_engine.maintain_mrc_data(
5973                         p_event_mode       => 'DELETE',
5974                         p_table_name       => 'RA_CUST_TRX_LINE_GL_DIST',
5975                         p_mode             => 'SINGLE',
5976                         p_key_value        => gl_dist_array(I));
5977                   END LOOP;
5978                 END IF;
5979 
5980                 close_cursor( l_delete_c );
5981 
5982             EXCEPTION
5983                 WHEN OTHERS THEN
5984                     debug( 'Error executing delete stmt', MSG_LEVEL_BASIC );
5985                     debug(SQLERRM, MSG_LEVEL_BASIC);
5986                     RAISE;
5987 
5988             END;
5989 
5990 
5991         END IF;  -- if mode = U, D
5992 
5993         IF( p_mode in (I, U) ) THEN
5994 
5995             /* Bug 2560036 - Call collectibility when in INSERT mode
5996                only. */
5997 
5998             /*9112739 Added for run in update mode*/
5999 
6000             IF (p_mode IN ( 'I' ,'U') AND
6001                 p_account_class in ('REV','ALL') AND
6002                 g_test_collectibility AND
6003                 NOT g_called_collectibility) THEN
6004 
6005               IF PG_DEBUG in ('Y', 'C') THEN
6006                 arp_util.debug('  testing collectibility...');
6007               END IF;
6008 
6009               -- the following logic was enhanced to distinguish the
6010               -- call to revenue management from invoice api and others.
6011               -- if it is being called from invoice api then we would
6012               -- like to call by passing the source and not skip
6013               -- it if it was called before.
6014               --
6015               -- ORASHID 22-Sep-2004
6016 
6017               IF (g_called_from = 'AR_INVOICE_API') THEN
6018                 t_collect := ar_revenue_management_pvt.line_collectibility(
6019                   p_request_id => p_request_id,
6020                   p_source     => g_called_from,
6021                   x_error_count=> l_error_count);
6022              /* Bug 4693399 - manually entered transactions can also be deferred
6023 	        by contingencies */
6024               ELSIF (p_request_id IS NULL and p_customer_trx_line_id IS NOT NULL) THEN
6025                   t_collect := ar_revenue_management_pvt.line_collectibility(
6026                    p_request_id => p_request_id,
6027                    x_error_count=> l_error_count,
6028                    p_customer_trx_line_id=> p_customer_trx_line_id);
6029 
6030 		   /*9112739*/
6031                    IF (p_mode in ( 'U') AND
6032                    t_collect.EXISTS(p_customer_trx_line_id))
6033                    THEN
6034                           IF (t_collect(p_customer_trx_line_id) =
6035                             ar_revenue_management_pvt.defer) THEN
6036                                  l_use_unearn_srep_dependency := TRUE;
6037                           END IF;
6038                    END IF;
6039 
6040 
6041               ELSIF (p_request_id is NULL and p_customer_trx_id IS NOT NULL and p_customer_trx_line_id IS NULL) THEN
6042                 /* 9112739 Added for trx level run for each line*/
6043                 FOR i in c_trx_lines (p_customer_trx_id)
6044                 LOOP
6045                    l_collect := ar_revenue_management_pvt.line_collectibility(
6046                    p_request_id => p_request_id,
6047                    x_error_count=> l_error_count,
6048                    p_customer_trx_line_id=> i.customer_trx_line_id);
6049 		   t_collect(i.customer_trx_line_id) := l_collect(i.customer_trx_line_id);
6050 
6051                    IF (p_mode in ( 'U') AND
6052                    t_collect.EXISTS(i.customer_trx_line_id))
6053                    THEN
6054                           IF (t_collect(i.customer_trx_line_id) =
6055                             ar_revenue_management_pvt.defer) THEN
6056                                  l_use_unearn_srep_dependency := TRUE;
6057                           END IF;
6058                    END IF;
6059                 END LOOP;
6060               ELSE
6061                 IF (NOT g_called_collectibility) THEN
6062                   g_called_collectibility := TRUE;
6063                   t_collect := ar_revenue_management_pvt.line_collectibility(
6064                    p_request_id => p_request_id,
6065                    x_error_count=> l_error_count);
6066                 END IF;
6067 
6068               END IF;
6069 
6070               /* Bug 3879222 - Increase p_failure_count when
6071                  collectibility rejects contingency rows in autoinv */
6072               IF (l_error_count > 0)
6073               THEN
6074 
6075                  IF PG_DEBUG in ('Y','C')
6076                  THEN
6077                    arp_util.debug('failed contingencies = ' || l_error_count);
6078                  END IF;
6079 
6080                  p_failure_count := nvl(p_failure_count,0) +
6081                                     nvl(l_error_count,0);
6082               END IF;
6083 
6084             ELSE
6085 
6086                 IF PG_DEBUG in ('Y', 'C') THEN
6087                    arp_standard.debug('do_autoaccounting: ' || '  collectibility bypassed...');
6088                 END IF;
6089 
6090             END IF;
6091 
6092             /* End bug 2560036 */
6093 
6094     	    --
6095     	    -- Insert distributions in Insert and Update mode
6096 	    --
6097 
6098             --
6099             -- Fetch records using select stmt
6100             --
6101             -- Bug 853040
6102 /*Bug 2034221:Added 'Freight' also in the clause to prevent the NULL value
6103               passed for ccid.
6104 */
6105             IF p_passed_ccid IS NOT NULL AND
6106                p_account_class in('REC','FREIGHT') THEN
6107                    p_ccid := p_passed_ccid;
6108             END IF;
6109             IF arp_auto_accounting.g_deposit_flag = 'Y' and
6110                p_account_class = 'REV' THEN
6111                p_ccid := p_passed_ccid;
6112             END IF;
6113 
6114 
6115             l_select_c := Get_Select_Cursor(
6116                                              system_info,
6117                                              profile_info,
6118                                              p_account_class,
6119                                              p_customer_trx_id,
6120                                              p_customer_trx_line_id,
6121                                              p_cust_trx_line_salesrep_id,
6122                                              p_request_id,
6123                                              p_gl_date,
6124                                              p_original_gl_date,
6125                                              p_total_trx_amount,
6126                                              p_ccid,
6127                                              p_force_account_set_no,
6128                                              p_cust_trx_type_id,
6129                                              p_primary_salesrep_id,
6130                                              p_inventory_item_id,
6131                                              p_memo_line_id,
6132 					     l_use_unearn_srep_dependency,
6133                                              l_keep_cursor_open_flag);
6134 
6135 
6136              Bind_All_Variables(
6137                                  l_select_c,
6138                                  system_info,
6139                                  profile_info,
6140                                  p_account_class,
6141                                  p_customer_trx_id,
6142                                  p_customer_trx_line_id,
6143                                  p_cust_trx_line_salesrep_id,
6144                                  p_request_id,
6145                                  p_gl_date,
6146                                  p_original_gl_date,
6147                                  p_total_trx_amount,
6148                                  p_passed_ccid,
6149                                  p_force_account_set_no,
6150                                  p_cust_trx_type_id,
6151                                  p_primary_salesrep_id,
6152                                  p_inventory_item_id,
6153                                  p_memo_line_id,
6154                                  l_keep_cursor_open_flag);
6155 
6156 
6157 
6158  	     -- Initialize totals
6159          l_total_percent := 0;
6160          l_total_amount := 0;
6161          l_total_acctd_amount := 0;
6162 
6163          l_first_fetch := TRUE;
6164 
6165          ----------------------------------------------------------------
6166          -- Execute select stmt
6167          ----------------------------------------------------------------
6168          BEGIN
6169 
6170             debug( '  Executing select stmt', MSG_LEVEL_DEBUG );
6171             --
6172             -- Execute
6173             --
6174             l_ignore := dbms_sql.execute( l_select_c );
6175 
6176          EXCEPTION
6177             WHEN OTHERS THEN
6178                debug( 'Error executing select cursor', MSG_LEVEL_BASIC );
6179                debug(SQLERRM, MSG_LEVEL_BASIC);
6180                RAISE;
6181          END;
6182 
6183          ---------------------------------------------------------------
6184          -- Fetch rows
6185          ---------------------------------------------------------------
6186          debug( '  Fetching select stmt', MSG_LEVEL_DEBUG );
6187 
6188          LOOP  -- Main Cursor Loop
6189 
6190             BEGIN
6191                --
6192                -- Each call to the fetch_rows will fetch MAX_ARRAY_SIZE rows of data
6193                -- If no. of rows are < MAX_ARRAY_SIZE then exit loop after processing.
6194                --
6195 
6196                l_rows_fetched := dbms_sql.fetch_rows( l_select_c );
6197 
6198 	       l_low := l_high + 1;
6199 	       l_high:= l_high + l_rows_fetched;
6200 
6201 
6202                IF l_rows_fetched > 0 THEN
6203 
6204     		  debug( '  Fetched a row :('|| l_rows_fetched || ')', MSG_LEVEL_DEBUG );
6205                   debug( '  l_low  : '|| l_low , MSG_LEVEL_DEBUG );
6206                   debug( '  l_high : '|| l_high , MSG_LEVEL_DEBUG );
6207 
6208 
6209        		  l_first_fetch := FALSE;
6210 
6211                   l_select_rec := l_null_rec;
6212 
6213 
6214                   get_column_values( l_select_c, l_select_tab );
6215 
6216 /* bug 1532372 - changed parameter from l_rows_fetched to l_low and l_high
6217                  this apparently corrects the 1000 REC row limit that
6218                  we encountered during benchmarking.  */
6219 
6220                   dump_select_tab( l_select_tab, l_low, l_high );
6221 
6222                   IF l_rows_fetched < MAX_ARRAY_SIZE THEN
6223                      --
6224                      -- no more rows to fetch
6225                      --
6226                      debug( '  Done fetching(if)', MSG_LEVEL_DEBUG );
6227 
6228                      IF ( l_keep_cursor_open_flag = FALSE )THEN
6229                         close_cursor( l_select_c );
6230                      END IF;
6231 
6232                   END IF;
6233                ELSE
6234                   --
6235                   -- no more rows to fetch
6236                   --
6237                   debug( '  Done fetching(else)', MSG_LEVEL_DEBUG );
6238 
6239                   IF ( l_keep_cursor_open_flag = FALSE )THEN
6240                      close_cursor( l_select_c );
6241                   END IF;
6242 
6243        		      -- No rows selected
6244 
6245   		          IF (l_first_fetch) THEN
6246 
6247     			     debug( '  raising NO_DATA_FOUND', MSG_LEVEL_DEBUG );
6248 			         RAISE NO_DATA_FOUND;
6249 
6250    			      END IF;
6251 
6252                   EXIT;  -- Exit out NOCOPY of loop
6253 
6254                END IF; -- Rows Fetched
6255 
6256             EXCEPTION
6257 	           WHEN NO_DATA_FOUND THEN
6258          	      RAISE;
6259                WHEN OTHERS THEN
6260                   debug( 'Error fetching select cursor', MSG_LEVEL_BASIC );
6261                   debug(SQLERRM, MSG_LEVEL_BASIC);
6262                   RAISE;
6263             END;
6264 
6265 
6266             -----------------------------------------------------------
6267             -- Default gl date if in closed period
6268             -- Correct rounding errors
6269             -- Call Flex mgr
6270             -----------------------------------------------------------
6271             FOR i IN l_low..l_high LOOP
6272 
6273                l_select_rec := get_select_rec (l_select_tab, i);
6274 
6275                process_line( system_info,
6276                              l_select_rec,
6277                              l_total_percent,
6278                              l_total_amount,
6279                              l_total_acctd_amount,
6280                              p_failure_count,
6281                              p_mode,
6282                              p_request_id );
6283 
6284                -- copy out NOCOPY parameters back to array
6285 
6286                l_select_tab.int_concatenated_segments(i) := l_select_rec.int_concatenated_segments;
6287                l_select_tab.int_code_combination_id(i)   := l_select_rec.int_code_combination_id;
6288                l_select_tab.code_combination_id(i)   := l_select_rec.code_combination_id;
6289                l_select_tab.concatenated_segments(i)   := l_select_rec.concatenated_segments;
6290 
6291             /* Bug 2560036 - Move account class back in case
6292                we overrode it inside process_line */
6293             l_select_tab.account_class(i) := l_select_rec.account_class;
6294 
6295             END LOOP;
6296 
6297             -----------------------------------------------------------
6298             -- Insert row
6299             -----------------------------------------------------------
6300             BEGIN
6301                insert_dist_row( system_info,
6302                                 profile_info,
6303                                 p_request_id,
6304                                 l_select_tab,
6305                                 l_low,
6306 				l_high);
6307             EXCEPTION
6308                WHEN OTHERS THEN
6309                   debug( 'Error inserting distributions', MSG_LEVEL_BASIC );
6310                   debug(SQLERRM, MSG_LEVEL_BASIC);
6311                   RAISE;
6312             END;
6313 
6314 
6315             -----------------------------------------------------------
6316             -- Insert into error table if called from autoinvoice
6317   		    -- and didn't get ccid
6318             -----------------------------------------------------------
6319             FOR i IN l_low..l_high LOOP
6320 
6321                l_select_rec := get_select_rec (l_select_tab, i);
6322 
6323                -- Get CCID's from tables as get_select_rec will not copy these
6324 
6325                l_select_rec.int_concatenated_segments := l_select_tab.int_concatenated_segments(i);
6326                l_select_rec.int_code_combination_id := l_select_tab.int_code_combination_id(i);
6327                l_select_rec.code_combination_id := l_select_tab.code_combination_id(i);
6328                l_select_rec.concatenated_segments := l_select_tab.concatenated_segments(i);
6329 
6330 
6331   		       IF ( l_select_rec.code_combination_id = -1 ) THEN
6332 
6333                   IF ( p_account_class = REV ) THEN
6334 
6335    			         put_message_on_stack(l_select_rec.customer_trx_line_id,
6336                          				  MSG_COMPLETE_REV_ACCOUNT,
6337                         				  l_select_rec.concatenated_segments,
6338                                           p_request_id );
6339 
6340    		          ELSIF( p_account_class = REC ) THEN
6341 
6342                      -- Put -ve customer trx id in the errors table if
6343     			     -- AutoAccounting is unable to derive the REC account.
6344        			     -- Validation Report will report this error for the first
6345 			         -- line of the Trx
6346 
6347        			     put_message_on_stack(-1 * l_select_rec.customer_trx_id,
6348 				                           MSG_COMPLETE_REC_ACCOUNT,
6349 				                           l_select_rec.concatenated_segments,
6350                                            p_request_id );
6351 
6352 	              ELSIF( p_account_class = FREIGHT ) THEN
6353 
6354 		             put_message_on_stack(
6355 				                           l_select_rec.customer_trx_line_id,
6356 				                           MSG_COMPLETE_FRT_ACCOUNT,
6357 				                           l_select_rec.concatenated_segments,
6358                                            p_request_id );
6359 
6360 	              ELSIF( p_account_class = TAX ) THEN
6361 /* 1651593 - Point errors to parent line if one is available */
6362           		     put_message_on_stack(
6363                                    NVL(l_select_rec.link_to_cust_trx_line_id,
6364     			               l_select_rec.customer_trx_line_id),
6365                             			   MSG_COMPLETE_TAX_ACCOUNT,
6366                         	       l_select_rec.concatenated_segments,
6367                                            p_request_id );
6368 
6369    		          ELSIF( p_account_class = CHARGES ) THEN
6370 
6371        		         put_message_on_stack(
6372 				                           l_select_rec.customer_trx_line_id,
6373                         				   MSG_COMPLETE_CHARGES_ACCOUNT,
6374                         				   l_select_rec.concatenated_segments,
6375                                            p_request_id );
6376 
6377       		      ELSIF( p_account_class in (UNBILL, UNEARN, SUSPENSE)) THEN
6378 
6379        		         put_message_on_stack(
6380                         				   l_select_rec.customer_trx_line_id,
6381                         				   MSG_COMPLETE_OFFSET_ACCOUNT,
6382                         				   l_select_rec.concatenated_segments,
6383                                            p_request_id );
6384 
6385    		          END IF;
6386 
6387    		       END IF;
6388 
6389                IF (p_account_class = 'TAX') THEN
6390 
6391                   --Invalid interim tax account
6392                   IF l_select_rec.int_code_combination_id = -1 THEN
6393                   /* 1651593 - Point tax lines to parent line for error */
6394                      put_message_on_stack(
6395                                      NVL(l_select_rec.link_to_cust_trx_line_id,
6396                                           l_select_rec.customer_trx_line_id),
6397                                           MSG_COMPLETE_INT_TAX_ACCOUNT,
6398                                           l_select_rec.int_concatenated_segments,
6399                                           p_request_id );
6400 
6401                   END IF;
6402 
6403                END IF;
6404 
6405             END LOOP; -- For all records in an array
6406 
6407             EXIT WHEN l_rows_fetched < MAX_ARRAY_SIZE; -- Exit from the loop if no. of rows fetched < array size
6408 
6409          END LOOP;
6410 
6411       END IF;  -- IF( p_mode in (I, U) )
6412 
6413    END IF;  -- IF( p_mode = G )
6414 
6415 
6416    -- Check if failed to get any ccids
6417    --
6418    debug( '  p_failure_count='||to_char(p_failure_count), MSG_LEVEL_DEBUG);
6419 
6420    IF ( l_keep_cursor_open_flag = FALSE ) THEN
6421       close_cursor( l_select_c );
6422    END IF;
6423 
6424    close_cursor( l_delete_c );
6425 
6426 
6427    print_fcn_label( 'arp_auto_accounting.do_autoaccounting_internal()-' );
6428 
6429 
6430 EXCEPTION
6431     WHEN NO_DATA_FOUND THEN
6432 
6433          IF ( l_keep_cursor_open_flag = FALSE )
6434          THEN  close_cursor( l_select_c );
6435          END IF;
6436 
6437         close_cursor( l_delete_c );
6438 
6439 	IF( p_mode = G ) THEN
6440 	    NULL;	-- Don't raise for Get mode, otherwise the
6441 			-- IN/OUT vars ccid, concat_segments do not
6442 			-- get populated.
6443 	ELSE
6444 	    RAISE;
6445 	END IF;
6446 
6447     WHEN OTHERS THEN
6448         debug( 'EXCEPTION: arp_auto_accounting.do_autoaccounting_internal()',
6449 	       MSG_LEVEL_BASIC );
6450         debug(SQLERRM, MSG_LEVEL_BASIC);
6451 
6452         close_cursor( l_select_c );
6453         close_cursor( l_delete_c );
6454 
6455 	ROLLBACK TO ar_auto_accounting;
6456         RAISE;
6457 
6458 END do_autoaccounting_internal;
6459 
6460 ----------------------------------------------------------------------------
6461 --
6462 -- PROCEDURE NAME:  do_autoaccounting
6463 --
6464 -- DECSRIPTION:
6465 --   Server-side entry point for autoaccounting.
6466 --
6467 -- ARGUMENTS:
6468 --      IN:
6469 --        mode:  May be I(nsert), U(pdate), D(elete), or (G)et
6470 --        account_class:  REC, REV, FREIGHT, TAX, UNBILL, UNEARN, SUSPENSE,
6471 --                        CHARGES
6472 --        customer_trx_id:  NULL if not applicable
6473 --        customer_trx_line_id:  NULL if not applicable (G)
6474 --        cust_trx_line_salesrep_id:  NULL if not applicable
6475 --        request_id:  NULL if not applicable
6476 --        gl_date:  GL date of the account assignment
6477 --        original_gl_date:  Original GL date
6478 --        total_trx_amount:  For Receivable account only
6479 --        passed_ccid:  Code comination ID to use if supplied
6480 --        force_account_set_no:
6481 --        cust_trx_type_id (G)
6482 --        primary_salesrep_id (G)
6483 --        inventory_item_id (G)
6484 --        memo_line_id (G)
6485 --
6486 --      IN/OUT:
6487 --        ccid
6488 --        concat_segments
6489 --        failure_count
6490 --
6491 --      OUT:
6492 --
6493 -- NOTES:
6494 --   If mode is not (G)et, raises the exception
6495 --   arp_auto_accounting.no_ccid if autoaccounting could not derive a
6496 --   valid code combination.  The public variable g_error_buffer is
6497 --   populated for more information.  In (G)et mode, check the value
6498 --   assigned to p_ccid.  If it is -1, then no ccid was found.
6499 --
6500 --   Raises the exception NO_DATA_FOUND if no rows were selected for
6501 --   processing.
6502 --
6503 --   Exception raised if Oracle error.
6504 --   App_exception is raised for all other fatal errors and a message
6505 --   is put on the AOL stack.  The public variable g_error_buffer is
6506 --   populated for both types of errors.
6507 --
6508 -- HISTORY:
6509 --
6510 --
6511 PROCEDURE do_autoaccounting( p_mode 			IN VARCHAR2,
6512                             p_account_class 		IN VARCHAR2,
6513                             p_customer_trx_id 		IN NUMBER,
6514                             p_customer_trx_line_id 	IN NUMBER,
6515                             p_cust_trx_line_salesrep_id IN NUMBER,
6516                             p_request_id 		IN NUMBER,
6517                             p_gl_date 			IN DATE,
6518                             p_original_gl_date 		IN DATE,
6519                             p_total_trx_amount 		IN NUMBER,
6520                             p_passed_ccid 		IN NUMBER,
6521                             p_force_account_set_no 	IN VARCHAR2,
6522                             p_cust_trx_type_id 		IN NUMBER,
6523                             p_primary_salesrep_id 	IN NUMBER,
6524                             p_inventory_item_id 	IN NUMBER,
6525                             p_memo_line_id 		IN NUMBER,
6526                             p_ccid 			IN OUT NOCOPY NUMBER,
6527                             p_concat_segments 		IN OUT NOCOPY VARCHAR2,
6528                             p_failure_count	 	IN OUT NOCOPY NUMBER )
6529 IS
6530 
6531 
6532     l_select_rec select_rec_type;
6533     l_null_rec   CONSTANT select_rec_type := l_select_rec;
6534 
6535     l_ignore   			INTEGER;
6536     l_boolean  			BOOLEAN;
6537     l_temp                      BINARY_INTEGER;
6538     l_account_class             VARCHAR2(20);
6539 
6540 BEGIN
6541 
6542     print_fcn_label( 'arp_auto_accounting.do_autoaccounting()+' );
6543 
6544 /*
6545 ar_transaction_pub.debug('arp_auto_accounting.do_autoaccounting()+',
6546       FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR);
6547 */
6548     g_error_buffer := NULL;
6549 
6550     --
6551     -- Set message level for debugging
6552     --
6553     system_info.msg_level := arp_global.msg_level;
6554 
6555     debug( '  mode='||p_mode, MSG_LEVEL_DEBUG );
6556     debug( '  account_class='||p_account_class, MSG_LEVEL_DEBUG );
6557     debug( '  customer_trx_id='||to_char(p_customer_trx_id), MSG_LEVEL_DEBUG );
6558     debug( '  customer_trx_line_id='||to_char(p_customer_trx_line_id),
6559            MSG_LEVEL_DEBUG );
6560     debug( '  cust_trx_line_salesrep_id='||
6561 	   to_char(p_cust_trx_line_salesrep_id),
6562            MSG_LEVEL_DEBUG );
6563     debug( '  request_id='||to_char(p_request_id), MSG_LEVEL_DEBUG );
6564     debug( '  gl_date='||to_char(p_gl_date,'MM/DD/YYYY'), MSG_LEVEL_DEBUG );
6565     debug( '  original_gl_date='||to_char(p_original_gl_date,'MM/DD/YYYY'), MSG_LEVEL_DEBUG );
6566     debug( '  total_trx_amount='||to_char(p_total_trx_amount),
6567 		MSG_LEVEL_DEBUG );
6568     debug( '  passed_ccid='||to_char(p_passed_ccid), MSG_LEVEL_DEBUG );
6569     debug( '  force_account_set_no='||p_force_account_set_no,
6570 		MSG_LEVEL_DEBUG );
6571     debug( '  cust_trx_type_id='||to_char(p_cust_trx_type_id),
6572 		MSG_LEVEL_DEBUG );
6573     debug( '  primary_salesrep_id='||to_char(p_primary_salesrep_id),
6574        MSG_LEVEL_DEBUG );
6575     debug( '  inventory_item_id='||to_char(p_inventory_item_id),
6576 		MSG_LEVEL_DEBUG );
6577     debug( '  memo_line_id='||to_char(p_memo_line_id), MSG_LEVEL_DEBUG );
6578     debug( '  msg_level='||to_char(system_info.msg_level), MSG_LEVEL_DEBUG );
6579 
6580     --
6581     -- Initialize
6582     --
6583     -- p_failure_count := 0;
6584 
6585     --
6586     -- Adjust account_class to proper string
6587     --
6588     l_account_class := p_account_class;
6589     expand_account_class( l_account_class );
6590 
6591     IF( l_account_class = 'ALL' ) THEN
6592 
6593         DECLARE
6594             l_no_rows_rev 		BOOLEAN := FALSE;
6595             l_no_rows_rec 		BOOLEAN := FALSE;
6596             l_no_rows_freight 		BOOLEAN := FALSE;
6597             l_no_rows_tax 		BOOLEAN := FALSE;
6598             l_no_rows_unbill 		BOOLEAN := FALSE;
6599             l_no_rows_unearn 		BOOLEAN := FALSE;
6600 	    l_no_rows_suspense		BOOLEAN := FALSE;
6601             l_no_rows_charges		BOOLEAN := FALSE;
6602             l_invoicing_rule_id         ra_customer_trx.invoicing_rule_id%type;
6603             l_create_clearing_flag  ra_batch_sources.create_clearing_flag%type;
6604             l_line_type                 ra_customer_trx_lines.line_type%type;
6605         BEGIN
6606 
6607             debug( '  Processing ALL mode...', MSG_LEVEL_DEBUG );
6608 
6609             IF ( p_customer_trx_id IS NOT NULL)
6610             THEN
6611                   SELECT invoicing_rule_id,
6612                          create_clearing_flag
6613                   INTO   l_invoicing_rule_id,
6614                          l_create_clearing_flag
6615                   FROM   ra_customer_trx  t,
6616                          ra_batch_sources b
6617                   WHERE  customer_trx_id =  p_customer_trx_id
6618                   AND    t.batch_source_id = b.batch_source_id;
6619             END IF;
6620 
6621             IF ( p_customer_trx_line_id IS NOT NULL )
6622             THEN
6623                  SELECT line_type
6624                  INTO   l_line_type
6625                  FROM   ra_customer_trx_lines
6626                  WHERE  customer_trx_line_id =  p_customer_trx_line_id;
6627             END IF;
6628 
6629 	    BEGIN
6630 	        do_autoaccounting_internal(
6631 			p_mode,
6632                         REC,
6633                         p_customer_trx_id,
6634                         p_customer_trx_line_id,
6635                         p_cust_trx_line_salesrep_id,
6636                         p_request_id,
6637                         p_gl_date,
6638                         p_original_gl_date,
6639                         p_total_trx_amount,
6640                         p_passed_ccid,
6641                         p_force_account_set_no,
6642                         p_cust_trx_type_id,
6643                         p_primary_salesrep_id,
6644                         p_inventory_item_id,
6645                         p_memo_line_id,
6646                         '','',
6647                         p_ccid,
6648                         p_concat_segments,
6649                         p_failure_count );
6650 	    EXCEPTION
6651 		WHEN NO_DATA_FOUND THEN
6652 		    l_no_rows_rec := TRUE;
6653 		WHEN OTHERS THEN
6654 		    RAISE;
6655 	    END;
6656 
6657             BEGIN
6658 		do_autoaccounting_internal(
6659 			p_mode,
6660                         REV,
6661                         p_customer_trx_id,
6662                         p_customer_trx_line_id,
6663                         p_cust_trx_line_salesrep_id,
6664                         p_request_id,
6665                         p_gl_date,
6666                         p_original_gl_date,
6667                         p_total_trx_amount,
6668                         p_passed_ccid,
6669                         p_force_account_set_no,
6670                         p_cust_trx_type_id,
6671                         p_primary_salesrep_id,
6672                         p_inventory_item_id,
6673                         p_memo_line_id,
6674                         '','',
6675                         p_ccid,
6676                         p_concat_segments,
6677                         p_failure_count );
6678 	    EXCEPTION
6679 		WHEN NO_DATA_FOUND THEN
6680 		    l_no_rows_rev := TRUE;
6681 		WHEN OTHERS THEN
6682 		    RAISE;
6683 	    END;
6684 
6685             IF ( NVL(l_line_type, 'CHARGES') = 'CHARGES')
6686             THEN
6687                  BEGIN
6688                      do_autoaccounting_internal(
6689                              p_mode,
6690                              CHARGES,
6691                              p_customer_trx_id,
6692                              p_customer_trx_line_id,
6693                              p_cust_trx_line_salesrep_id,
6694                              p_request_id,
6695                              p_gl_date,
6696                              p_original_gl_date,
6697                              p_total_trx_amount,
6698                              p_passed_ccid,
6699                              p_force_account_set_no,
6700                              p_cust_trx_type_id,
6701                              p_primary_salesrep_id,
6702                              p_inventory_item_id,
6703                              p_memo_line_id,
6704                              '','',
6705                              p_ccid,
6706                              p_concat_segments,
6707                              p_failure_count );
6708                  EXCEPTION
6709                      WHEN NO_DATA_FOUND THEN
6710                          l_no_rows_charges := TRUE;
6711                      WHEN OTHERS THEN
6712                          RAISE;
6713                  END;
6714             END IF;
6715 
6716 	    BEGIN
6717 		do_autoaccounting_internal(
6718 			p_mode,
6719                         TAX,
6720                         p_customer_trx_id,
6721                         p_customer_trx_line_id,
6722                         p_cust_trx_line_salesrep_id,
6723                         p_request_id,
6724                         p_gl_date,
6725                         p_original_gl_date,
6726                         p_total_trx_amount,
6727                         p_passed_ccid,
6728                         p_force_account_set_no,
6729                         p_cust_trx_type_id,
6730                         p_primary_salesrep_id,
6731                         p_inventory_item_id,
6732                         p_memo_line_id,
6733                         '','',
6734                         p_ccid,
6735                         p_concat_segments,
6736                         p_failure_count );
6737 	    EXCEPTION
6738 		WHEN NO_DATA_FOUND THEN
6739 		    l_no_rows_tax := TRUE;
6740 		WHEN OTHERS THEN
6741 		    RAISE;
6742 	    END;
6743 
6744 	    BEGIN
6745                 do_autoaccounting_internal(
6746 			p_mode,
6747                         FREIGHT,
6748                         p_customer_trx_id,
6749                         p_customer_trx_line_id,
6750                         p_cust_trx_line_salesrep_id,
6751                         p_request_id,
6752                         p_gl_date,
6753                         p_original_gl_date,
6754                         p_total_trx_amount,
6755                         p_passed_ccid,
6756                         p_force_account_set_no,
6757                         p_cust_trx_type_id,
6758                         p_primary_salesrep_id,
6759                         p_inventory_item_id,
6760                         p_memo_line_id,
6761                         '','',
6762                         p_ccid,
6763                         p_concat_segments,
6764                         p_failure_count );
6765 	    EXCEPTION
6766 		WHEN NO_DATA_FOUND THEN
6767 		    l_no_rows_freight := TRUE;
6768 		WHEN OTHERS THEN
6769 		    RAISE;
6770 	    END;
6771 
6772             IF (  NVL(l_create_clearing_flag, 'Y') = 'Y' )
6773             THEN
6774 
6775                  BEGIN
6776                      do_autoaccounting_internal(
6777                              p_mode,
6778                              SUSPENSE,
6779                              p_customer_trx_id,
6780                              p_customer_trx_line_id,
6781                              p_cust_trx_line_salesrep_id,
6782                              p_request_id,
6783                              p_gl_date,
6784                              p_original_gl_date,
6785                              p_total_trx_amount,
6786                              p_passed_ccid,
6787                              p_force_account_set_no,
6788                              p_cust_trx_type_id,
6789                              p_primary_salesrep_id,
6790                              p_inventory_item_id,
6791                              p_memo_line_id,
6792                              '','',
6793                              p_ccid,
6794                              p_concat_segments,
6795                              p_failure_count );
6796                  EXCEPTION
6797                      WHEN NO_DATA_FOUND THEN
6798                          l_no_rows_suspense := TRUE;
6799                      WHEN OTHERS THEN
6800                          RAISE;
6801                  END;
6802 
6803             END IF;
6804 
6805             BEGIN
6806                 do_autoaccounting_internal(
6807                              p_mode,
6808                              UNBILL,
6809                              p_customer_trx_id,
6810                              p_customer_trx_line_id,
6811                              p_cust_trx_line_salesrep_id,
6812                              p_request_id,
6813                              p_gl_date,
6814                              p_original_gl_date,
6815                              p_total_trx_amount,
6816                              p_passed_ccid,
6817                              p_force_account_set_no,
6818                              p_cust_trx_type_id,
6819                              p_primary_salesrep_id,
6820                              p_inventory_item_id,
6821                              p_memo_line_id,
6822                              '','',
6823                              p_ccid,
6824                              p_concat_segments,
6825                              p_failure_count );
6826             EXCEPTION
6827                      WHEN NO_DATA_FOUND THEN
6828                          l_no_rows_unbill := TRUE;
6829                      WHEN OTHERS THEN
6830                          RAISE;
6831             END;
6832 
6833             BEGIN
6834                 do_autoaccounting_internal(
6835                              p_mode,
6836                              UNEARN,
6837                              p_customer_trx_id,
6838                              p_customer_trx_line_id,
6839                              p_cust_trx_line_salesrep_id,
6840                              p_request_id,
6841                              p_gl_date,
6842                              p_original_gl_date,
6843                              p_total_trx_amount,
6844                              p_passed_ccid,
6845                              p_force_account_set_no,
6846                              p_cust_trx_type_id,
6847                              p_primary_salesrep_id,
6848                              p_inventory_item_id,
6849                              p_memo_line_id,
6850                              '','',
6851                              p_ccid,
6852                              p_concat_segments,
6853                              p_failure_count );
6854             EXCEPTION
6855                      WHEN NO_DATA_FOUND THEN
6856                          l_no_rows_unearn := TRUE;
6857                      WHEN OTHERS THEN
6858                          RAISE;
6859             END;
6860 
6861             IF ( l_no_rows_rev
6862 		 AND l_no_rows_rec
6863 		 AND l_no_rows_freight
6864 		 AND l_no_rows_tax
6865 		 AND l_no_rows_unbill
6866 		 AND l_no_rows_unearn
6867 		 AND l_no_rows_suspense
6868 		 AND l_no_rows_charges ) THEN
6869 
6870 		debug( '  raising NO_DATA_FOUND', MSG_LEVEL_DEBUG );
6871 	        RAISE NO_DATA_FOUND;
6872 
6873             END IF;
6874 
6875         EXCEPTION
6876 	    WHEN NO_DATA_FOUND THEN
6877 		RAISE;
6878             WHEN OTHERS THEN
6879                 debug( 'Error processing ALL account class',
6880                            MSG_LEVEL_BASIC );
6881                 RAISE;
6882         END;
6883 
6884 
6885     ELSE	-- not ALL mode
6886 
6887         do_autoaccounting_internal(
6888 			p_mode,
6889                         l_account_class,
6890                         p_customer_trx_id,
6891                         p_customer_trx_line_id,
6892                         p_cust_trx_line_salesrep_id,
6893                         p_request_id,
6894                         p_gl_date,
6895                         p_original_gl_date,
6896                         p_total_trx_amount,
6897                         p_passed_ccid,
6898                         p_force_account_set_no,
6899                         p_cust_trx_type_id,
6900                         p_primary_salesrep_id,
6901                         p_inventory_item_id,
6902                         p_memo_line_id,
6903                         '','',
6904                         p_ccid,
6905                         p_concat_segments,
6906                         p_failure_count );
6907 
6908 
6909     END IF;  -- ALL mode
6910 
6911 
6912     -- Check if failed to get any ccids
6913     --
6914     debug( '  p_failure_count='||to_char(p_failure_count) ,
6915 		MSG_LEVEL_DEBUG);
6916 
6917     IF( p_failure_count > 0 ) THEN
6918 
6919 	debug( '  raising no_ccid', MSG_LEVEL_DEBUG );
6920         RAISE no_ccid;
6921 
6922     END IF;
6923 
6924 /*ar_transaction_pub.debug('arp_auto_accounting.do_autoaccounting()-',
6925       FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR);
6926 */
6927     print_fcn_label( 'arp_auto_accounting.do_autoaccounting()-' );
6928 
6929 
6930 EXCEPTION
6931     WHEN no_ccid OR NO_DATA_FOUND THEN
6932 
6933 	IF( p_mode = G OR
6934 	    p_request_id IS NOT NULL ) THEN
6935 
6936 	    NULL;	-- Don't raise for Get mode or Autoinvoice,
6937 			-- otherwise the IN/OUT variables
6938 			-- ccid, concat_segments and failure_count
6939 			-- do not get populated.
6940 	ELSE
6941 	    RAISE;
6942 	END IF;
6943 
6944     WHEN OTHERS THEN
6945         debug( 'EXCEPTION: arp_auto_accounting.do_autoaccounting()',
6946 	       MSG_LEVEL_BASIC );
6947         debug(SQLERRM, MSG_LEVEL_BASIC);
6948 
6949 	IF( sqlcode = 1 ) THEN
6950 	    --
6951 	    -- User-defined exception
6952 	    --
6953 	    FND_MESSAGE.set_name( 'AR', 'GENERIC_MESSAGE' );
6954 	    FND_MESSAGE.set_token( 'GENERIC_TEXT', g_error_buffer );
6955 	    APP_EXCEPTION.raise_exception;
6956 
6957 	ELSE
6958 	    --
6959 	    -- Oracle error
6960 	    --
6961 	    g_error_buffer := SQLERRM;
6962 
6963 	    RAISE;
6964 
6965 	END IF;
6966 
6967 END do_autoaccounting;
6968 
6969 
6970 ----------------------------------------------------------------------------
6971 --
6972 -- PROCEDURE NAME:  do_autoaccounting
6973 --
6974 -- DECSRIPTION:
6975 --   Overloaded procedure when autoaccounting is called in G or Get mode
6976 --   as warehouse id is required to be passed in and bill_to_site_use_id
6977 --   is implicitly derived.
6978 --
6979 -- ARGUMENTS:
6980 --      IN:
6981 --        mode:  May be (G)et only as the routine is written for the same
6982 --        account_class:  REC, REV, FREIGHT, TAX, UNBILL, UNEARN, SUSPENSE,
6983 --                        CHARGES
6984 --        customer_trx_id:  NULL if not applicable
6985 --        customer_trx_line_id:  NULL if not applicable (G)
6986 --        cust_trx_line_salesrep_id:  NULL if not applicable
6987 --        request_id:  NULL if not applicable
6988 --        gl_date:  GL date of the account assignment
6989 --        original_gl_date:  Original GL date
6990 --        total_trx_amount:  For Receivable account only
6991 --        passed_ccid:  Code comination ID to use if supplied
6992 --        force_account_set_no:
6993 --        cust_trx_type_id (G)
6994 --        primary_salesrep_id (G)
6995 --        inventory_item_id (G)
6996 --        memo_line_id (G)
6997 --        warehouse_id (G)
6998 --
6999 --      IN/OUT:
7000 --        ccid
7001 --        concat_segments
7002 --        failure_count
7003 --
7004 --      OUT:
7005 --
7006 -- NOTES:
7007 --   If mode is not (G)et, raises the exception
7008 --   arp_auto_accounting.no_ccid if autoaccounting could not derive a
7009 --   valid code combination.  The public variable g_error_buffer is
7010 --   populated for more information.  In (G)et mode, check the value
7011 --   assigned to p_ccid.  If it is -1, then no ccid was found.
7012 --
7013 --   Raises the exception NO_DATA_FOUND if no rows were selected for
7014 --   processing.
7015 --
7016 --   Exception raised if Oracle error.
7017 --   App_exception is raised for all other fatal errors and a message
7018 --   is put on the AOL stack.  The public variable g_error_buffer is
7019 --   populated for both types of errors.
7020 --
7021 --   Never call this routine for ALL classes as this was specifically
7022 --   written to work in Get mode, but will also work in other modes
7023 --   provided the account class is not ALL
7024 -- HISTORY:
7025 --
7026 --
7027 PROCEDURE do_autoaccounting( p_mode 			IN VARCHAR2,
7028                             p_account_class 		IN VARCHAR2,
7029                             p_customer_trx_id 		IN NUMBER,
7030                             p_customer_trx_line_id 	IN NUMBER,
7031                             p_cust_trx_line_salesrep_id IN NUMBER,
7032                             p_request_id 		IN NUMBER,
7033                             p_gl_date 			IN DATE,
7034                             p_original_gl_date 		IN DATE,
7035                             p_total_trx_amount 		IN NUMBER,
7036                             p_passed_ccid 		IN NUMBER,
7037                             p_force_account_set_no 	IN VARCHAR2,
7038                             p_cust_trx_type_id 		IN NUMBER,
7039                             p_primary_salesrep_id 	IN NUMBER,
7040                             p_inventory_item_id 	IN NUMBER,
7041                             p_memo_line_id 		IN NUMBER,
7042                             p_warehouse_id              IN NUMBER,
7043                             p_ccid 			IN OUT NOCOPY NUMBER,
7044                             p_concat_segments 		IN OUT NOCOPY VARCHAR2,
7045                             p_failure_count	 	IN OUT NOCOPY NUMBER )
7046 IS
7047 
7048     l_account_class             VARCHAR2(20);
7049     l_bill_to_site_use_id       NUMBER;
7050 
7051 BEGIN
7052 
7053     print_fcn_label( 'arp_auto_accounting.do_autoaccounting overloaded get mode()+' );
7054 
7055     g_error_buffer := NULL;
7056 
7057     l_bill_to_site_use_id := '';
7058 
7059     --
7060     -- Set message level for debugging
7061     --
7062     system_info.msg_level := arp_global.msg_level;
7063 
7064     debug( '  mode='||p_mode, MSG_LEVEL_DEBUG );
7065     debug( '  account_class='||p_account_class, MSG_LEVEL_DEBUG );
7066     debug( '  customer_trx_id='||to_char(p_customer_trx_id), MSG_LEVEL_DEBUG );
7067     debug( '  customer_trx_line_id='||to_char(p_customer_trx_line_id),
7068            MSG_LEVEL_DEBUG );
7069     debug( '  cust_trx_line_salesrep_id='||
7070 	   to_char(p_cust_trx_line_salesrep_id),
7071            MSG_LEVEL_DEBUG );
7072     debug( '  request_id='||to_char(p_request_id), MSG_LEVEL_DEBUG );
7073     debug( '  gl_date='||to_char(p_gl_date,'MM/DD/YYYY'), MSG_LEVEL_DEBUG );
7074     debug( '  original_gl_date='||to_char(p_original_gl_date,'MM/DD/YYYY'), MSG_LEVEL_DEBUG );
7075     debug( '  total_trx_amount='||to_char(p_total_trx_amount),
7076 		MSG_LEVEL_DEBUG );
7077     debug( '  passed_ccid='||to_char(p_passed_ccid), MSG_LEVEL_DEBUG );
7078     debug( '  force_account_set_no='||p_force_account_set_no,
7079 		MSG_LEVEL_DEBUG );
7080     debug( '  cust_trx_type_id='||to_char(p_cust_trx_type_id),
7081 		MSG_LEVEL_DEBUG );
7082     debug( '  primary_salesrep_id='||to_char(p_primary_salesrep_id),
7083        MSG_LEVEL_DEBUG );
7084     debug( '  inventory_item_id='||to_char(p_inventory_item_id),
7085 		MSG_LEVEL_DEBUG );
7086     debug( '  memo_line_id='||to_char(p_memo_line_id), MSG_LEVEL_DEBUG );
7087     debug( '  warehouse_id='||to_char(p_warehouse_id), MSG_LEVEL_DEBUG );
7088     debug( '  msg_level='||to_char(system_info.msg_level), MSG_LEVEL_DEBUG );
7089 
7090    --
7091    -- Adjust account_class to proper string
7092    --
7093     l_account_class := p_account_class;
7094     expand_account_class( l_account_class );
7095 
7096    --
7097    --Get the billing site use id so this does not require to be passed
7098    --as a parameter to the do_autoaccounting procedure
7099    --
7100     IF ( p_customer_trx_id IS NOT NULL) THEN
7101           SELECT t.bill_to_site_use_id
7102           INTO   l_bill_to_site_use_id
7103           FROM   ra_customer_trx  t
7104           WHERE  t.customer_trx_id =  p_customer_trx_id;
7105     END IF;
7106 
7107    --
7108    -- Mode will always be get as this routine is specifically
7109    -- written for the same
7110    --
7111     do_autoaccounting_internal(
7112 	      p_mode,
7113               l_account_class,
7114               p_customer_trx_id,
7115               p_customer_trx_line_id,
7116               p_cust_trx_line_salesrep_id,
7117               p_request_id,
7118               p_gl_date,
7119               p_original_gl_date,
7120               p_total_trx_amount,
7121               p_passed_ccid,
7122               p_force_account_set_no,
7123               p_cust_trx_type_id,
7124               p_primary_salesrep_id,
7125               p_inventory_item_id,
7126               p_memo_line_id,
7127               l_bill_to_site_use_id,
7128               p_warehouse_id,
7129               p_ccid,
7130               p_concat_segments,
7131               p_failure_count );
7132 
7133     -- Check if failed to get any ccids
7134     --
7135     debug( '  p_failure_count='||to_char(p_failure_count) ,
7136 		MSG_LEVEL_DEBUG);
7137 
7138     IF( p_failure_count > 0 ) THEN
7139 
7140 	debug( '  raising no_ccid', MSG_LEVEL_DEBUG );
7141         RAISE no_ccid;
7142 
7143     END IF;
7144 
7145     print_fcn_label( 'arp_auto_accounting.do_autoaccounting() overloaded get mode -' );
7146 
7147 
7148 EXCEPTION
7149     WHEN no_ccid OR NO_DATA_FOUND THEN
7150 
7151 	IF( p_mode = G OR
7152 	    p_request_id IS NOT NULL ) THEN
7153 
7154 	    NULL;	-- Don't raise for Get mode or Autoinvoice,
7155 			-- otherwise the IN/OUT variables
7156 			-- ccid, concat_segments and failure_count
7157 			-- do not get populated.
7158 	ELSE
7159 	    RAISE;
7160 	END IF;
7161 
7162     WHEN OTHERS THEN
7163         debug( 'EXCEPTION: arp_auto_accounting.do_autoaccounting()',
7164 	       MSG_LEVEL_BASIC );
7165         debug(SQLERRM, MSG_LEVEL_BASIC);
7166 
7167 	IF( sqlcode = 1 ) THEN
7168 	    --
7169 	    -- User-defined exception
7170 	    --
7171 	    FND_MESSAGE.set_name( 'AR', 'GENERIC_MESSAGE' );
7172 	    FND_MESSAGE.set_token( 'GENERIC_TEXT', g_error_buffer );
7173 	    APP_EXCEPTION.raise_exception;
7174 
7175 	ELSE
7176 	    --
7177 	    -- Oracle error
7178 	    --
7179 	    g_error_buffer := SQLERRM;
7180 
7181 	    RAISE;
7182 
7183 	END IF;
7184 
7185 END do_autoaccounting;
7186 
7187 ------------------------------------------------------------------------------
7188 --  Test programs
7189 ------------------------------------------------------------------------------
7190 PROCEDURE test_load IS
7191 BEGIN
7192     -- enable_debug;
7193     --
7194     -- dump REV
7195     --
7196     debug('**REV**');
7197     FOR i IN 0..rev_count-1 LOOP
7198         debug(autoacc_def_segment_t( rev_offset + i ));
7199         debug(autoacc_def_table_t( rev_offset + i ));
7200         debug(autoacc_def_const_t( rev_offset + i ));
7201     END LOOP;
7202     --
7203     -- dump REC
7204     --
7205     debug('**REC**');
7206     FOR i in 0..rev_count-1 LOOP
7207         debug(autoacc_def_segment_t( rec_offset + i ));
7208         debug(autoacc_def_table_t( rec_offset + i ));
7209         debug(autoacc_def_const_t( rec_offset + i ));
7210     END LOOP;
7211     --
7212     -- dump FRT
7213     --
7214     debug('**FREIGHT**');
7215     FOR i in 0..rev_count-1 LOOP
7216         debug(autoacc_def_segment_t( frt_offset + i ));
7217         debug(autoacc_def_table_t( frt_offset + i ));
7218         debug(autoacc_def_const_t( frt_offset + i ));
7219     END LOOP;
7220     --
7221     -- dump TAX
7222     --
7223     debug('**TAX**');
7224     FOR i in 0..rev_count-1 LOOP
7225         debug(autoacc_def_segment_t( tax_offset + i ));
7226         debug(autoacc_def_table_t( tax_offset + i ));
7227         debug(autoacc_def_const_t( tax_offset + i ));
7228     END LOOP;
7229     --
7230     -- dump UNBILL
7231     --
7232     debug('**UNBILL**');
7233     FOR i in 0..rev_count-1 LOOP
7234         debug(autoacc_def_segment_t( unbill_offset + i ));
7235         debug(autoacc_def_table_t( unbill_offset + i ));
7236         debug(autoacc_def_const_t( unbill_offset + i ));
7237     END LOOP;
7238     --
7239     -- dump UNEARN
7240     --
7241     debug('**UNEARN**');
7242     FOR i in 0..rev_count-1 LOOP
7243         debug(autoacc_def_segment_t( unearn_offset + i ));
7244         debug(autoacc_def_table_t( unearn_offset + i ));
7245         debug(autoacc_def_const_t( unearn_offset + i ));
7246     END LOOP;
7247     --
7248     -- dump SUSPENSE
7249     --
7250     debug('**SUSPENSE**');
7251     FOR i in 0..rev_count-1 LOOP
7252         debug(autoacc_def_segment_t( suspense_offset + i ));
7253         debug(autoacc_def_table_t( suspense_offset + i ));
7254         debug(autoacc_def_const_t( suspense_offset + i ));
7255     END LOOP;
7256 
7257 EXCEPTION
7258     WHEN OTHERS THEN
7259         debug(SQLERRM);
7260         debug('arp_auto_accounting.test_load()');
7261         RAISE;
7262 END test_load;
7263 
7264 ----------------------------------------------------------------------------
7265 PROCEDURE test_query( p_account_class IN VARCHAR2,
7266                       p_table_name IN VARCHAR2 ) IS
7267 BEGIN
7268     -- enable_debug;
7269     IF( query_autoacc_def(p_account_class, p_table_name)) THEN
7270         debug('YES');
7271     ELSE
7272         debug('NO');
7273     END IF;
7274 
7275 EXCEPTION
7276     WHEN OTHERS THEN
7277         debug('arp_auto_accounting.test_query()');
7278         debug(SQLERRM);
7279         RAISE;
7280 END test_query;
7281 
7282 ----------------------------------------------------------------------------
7283 PROCEDURE test_find( p_trx_type_id IN NUMBER,
7284                      p_salesrep_id IN NUMBER,
7285                      p_inv_item_id IN NUMBER,
7286                      p_memo_line_id IN NUMBER) IS
7287 
7288   l_ccid_rev BINARY_INTEGER;
7289   l_ccid_rec BINARY_INTEGER;
7290   l_ccid_frt BINARY_INTEGER;
7291   l_ccid_tax BINARY_INTEGER;
7292   l_ccid_unbill BINARY_INTEGER;
7293   l_ccid_unearn BINARY_INTEGER;
7294   l_ccid_suspense BINARY_INTEGER;
7295   l_inv_item_type mtl_system_items.item_type%TYPE;
7296 
7297 BEGIN
7298     -- enable_debug;
7299 
7300     if( p_trx_type_id <> -1 ) then
7301         get_trx_type_ccids(p_trx_type_id, l_ccid_rev, l_ccid_rec, l_ccid_frt,
7302                            l_ccid_tax, l_ccid_unbill, l_ccid_unearn,
7303                            l_ccid_suspense);
7304         debug( 'TRX_TYPE_ID='||to_char(p_trx_type_id)||
7305                             ' rev:'||to_char(l_ccid_rev)||
7306                             ' rec:'||to_char(l_ccid_rec)||
7307                ' frt:'||to_char(l_ccid_frt)||' tax:'||to_char(l_ccid_tax)||
7308                ' unbill:'||to_char(l_ccid_unbill)||
7309                ' unearn:'||to_char(l_ccid_unearn)||
7310                ' suspense:'||to_char(l_ccid_suspense) );
7311 
7312     end if;
7313 
7314     if( p_salesrep_id <> -1 ) then
7315         get_salesrep_ccids(p_salesrep_id, l_ccid_rev, l_ccid_rec, l_ccid_frt);
7316         debug( 'SALESREP_ID='||to_char(p_salesrep_id)||
7317                             ' rev:'||to_char(l_ccid_rev)||
7318                             ' rec:'||to_char(l_ccid_rec)||
7319                            ' frt:'||to_char(l_ccid_frt));
7320     end if;
7321 
7322     if( p_inv_item_id <> -1 ) then
7323         get_inv_item_ccids(profile_info,
7324                             p_inv_item_id,
7325                             '',
7326                             l_ccid_rev, l_inv_item_type );
7327         debug( 'ORG_ID='||oe_profile.value('SO_ORGANIZATION_ID',arp_global.sysparam.org_id)||
7328                             ' INV_ITEM_ID='||to_char(p_inv_item_id)||
7329                             ' rev:'||to_char(l_ccid_rev) );
7330     end if;
7331 
7332     if( p_memo_line_id <> -1 ) then
7333         get_memo_line_ccids(p_memo_line_id, l_ccid_rev);
7334         debug( ' MEMO_LINE_ID='||to_char(p_memo_line_id)||
7335                             ' rev:'||to_char(l_ccid_rev) );
7336     end if;
7337 
7338 EXCEPTION
7339     WHEN OTHERS THEN
7340         debug('arp_auto_accounting.test_find()');
7341         debug(SQLERRM);
7342         RAISE;
7343 END test_find;
7344 
7345 ----------------------------------------------------------------------------
7346 PROCEDURE test_assembly IS
7347 
7348     l_ccid_record ccid_rec_type;
7349     l_concat varchar2(800);
7350     l_ccid  binary_integer;
7351     l_int_concat varchar2(800);
7352     l_int_ccid  binary_integer;
7353     l_inv_item_type mtl_system_items.item_type%TYPE;
7354 
7355 BEGIN
7356 
7357     -- enable_debug;
7358 
7359     l_ccid_record.trx_type_ccid_rev := 1098;
7360     l_ccid_record.trx_type_ccid_rec := 1137;
7361     l_ccid_record.trx_type_ccid_frt := 1137;
7362     l_ccid_record.trx_type_ccid_tax := 1137;
7363     l_ccid_record.trx_type_ccid_unbill := 1137;
7364     l_ccid_record.trx_type_ccid_unearn := 1137;
7365     l_ccid_record.trx_type_ccid_suspense := 1137;
7366     l_ccid_record.salesrep_ccid_rev := 1173;
7367     l_ccid_record.salesrep_ccid_rec := 1137;
7368     l_ccid_record.salesrep_ccid_frt := 1137;
7369     l_ccid_record.lineitem_ccid_rev := 1232;
7370     l_ccid_record.tax_ccid_tax := 1137;
7371     l_ccid_record.agreecat_ccid_rev := 1137;
7372 
7373     assemble_code_combination( system_info,
7374                                flex_info,
7375                                REV,
7376                                l_ccid_record,
7377                                l_inv_item_type,
7378                                l_ccid,
7379                                l_concat,
7380                                l_int_ccid,
7381                                l_int_concat );
7382     debug(l_concat);
7383     debug(to_char(l_ccid));
7384 
7385     debug(l_int_concat);
7386     debug(to_char(l_int_ccid));
7387 
7388 EXCEPTION
7389     WHEN OTHERS THEN
7390         debug('arp_auto_accounting.test_assembly()');
7391         debug(SQLERRM);
7392         RAISE;
7393 END test_assembly;
7394 
7395 ----------------------------------------------------------------------------
7396 PROCEDURE test_harness IS
7397 
7398     l_segs VARCHAR2(200);
7399     l_ccid BINARY_INTEGER;
7400     l_int_segs VARCHAR2(200);
7401     l_int_ccid BINARY_INTEGER;
7402     l_errorbuf VARCHAR2(512);
7403     l_x BINARY_INTEGER;
7404 
7405 BEGIN
7406 
7407     -- enable_debug;
7408 
7409     -- Invalid account class
7410     --
7411     BEGIN
7412         flex_manager( 'REVX', '', 1011, 1000, '', '', '', '', '','',
7413                                   l_ccid,
7414                                   l_segs,
7415                                   l_int_ccid,
7416                                   l_int_segs );
7417     EXCEPTION
7418       WHEN invalid_account_class THEN
7419         debug('invalid account class test PASSED');
7420       WHEN OTHERS THEN
7421         debug('invalid account class test FAILED');
7422     END;
7423 
7424 
7425     -- Invalid trx_type_id
7426     --
7427     BEGIN
7428         flex_manager( REV, '', -1011, 1000, '', '', '', '', '','',
7429                                   l_ccid,
7430                                   l_segs,
7431                                   l_int_ccid,
7432                                   l_int_segs );
7433 
7434         debug('invalid trx_type_id test FAILED');
7435 
7436     EXCEPTION
7437       WHEN NO_DATA_FOUND THEN
7438         debug('invalid trx_type_id test PASSED');
7439       WHEN OTHERS THEN
7440         debug('invalid trx_type_id test FAILED');
7441     END;
7442 
7443 
7444     -- Invalid salesrep_id
7445     --
7446     BEGIN
7447         flex_manager( REV, '', 1011, -1000, '', '', '', '', '','',
7448                                   l_ccid,
7449                                   l_segs,
7450                                   l_int_ccid,
7451                                   l_int_segs );
7452 
7453         debug('invalid salesrep_id test FAILED');
7454 
7455     EXCEPTION
7456       WHEN NO_DATA_FOUND THEN
7457         debug('invalid salesrep_id test PASSED');
7458       WHEN OTHERS THEN
7459         debug('invalid salesrep_id test FAILED');
7460     END;
7461 
7462 
7463     -- Invalid inv_item_id
7464     --
7465     BEGIN
7466         flex_manager( REV, '', 1011, 1000, -1, '', '', '', '','',
7467                                   l_ccid,
7468                                   l_segs,
7469                                   l_int_ccid,
7470                                   l_int_segs );
7471 
7472         debug('invalid inv_item_id test FAILED');
7473 
7474     EXCEPTION
7475       WHEN NO_DATA_FOUND THEN
7476         debug('invalid inv_item_id test PASSED');
7477       WHEN OTHERS THEN
7478         debug('invalid inv_item_id test FAILED');
7479     END;
7480 
7481 
7482     -- Invalid memo_line_id
7483     --
7484     BEGIN
7485         flex_manager( REV, '', 1011, 1000, '', -1, '', '', '','',
7486                                   l_ccid,
7487                                   l_segs,
7488                                   l_int_ccid,
7489                                   l_int_segs );
7490 
7491         debug('invalid memo_line_id test FAILED');
7492 
7493     EXCEPTION
7494       WHEN NO_DATA_FOUND THEN
7495         debug('invalid memo_line_id test PASSED');
7496       WHEN OTHERS THEN
7497         debug('invalid memo_line_id test FAILED');
7498     END;
7499 
7500 
7501     -- Pass both inv_item_id and memo_line_id
7502     --
7503     BEGIN
7504         flex_manager( REV, '', 1011, 1000, -1, -1, '', '', '','',
7505                                   l_ccid,
7506                                   l_segs,
7507                                   l_int_ccid,
7508                                   l_int_segs );
7509 
7510         debug('item_and_memo_both_not_null test FAILED');
7511 
7512     EXCEPTION
7513       WHEN item_and_memo_both_not_null THEN
7514         debug('item_and_memo_both_not_null test PASSED');
7515       WHEN OTHERS THEN
7516         debug('item_and_memo_both_not_null test FAILED');
7517     END;
7518 
7519 
7520     -- Invalid ccid_tax
7521     --
7522     BEGIN
7523         flex_manager( TAX, '', 1011, 1000, '', '', 1, '', '','',
7524                                   l_ccid,
7525                                   l_segs,
7526                                   l_int_ccid,
7527                                   l_int_segs );
7528 
7529         debug('invalid ccid_tax test FAILED');
7530 
7531     EXCEPTION
7532       WHEN NO_DATA_FOUND THEN
7533         debug('invalid ccid_tax test PASSED');
7534       WHEN OTHERS THEN
7535         debug('invalid ccid_tax test FAILED');
7536     END;
7537 
7538 
7539     -- Test REV (ccid not found)
7540     --
7541     BEGIN
7542         flex_manager( REV, '', 1011, 1000, '', '', '', '', '','',
7543                                   l_ccid,
7544                                   l_segs,
7545                                   l_int_ccid,
7546                                   l_int_segs );
7547 
7548         IF( l_ccid = -1 AND l_segs = '01.100.4300.0.000.000' ) THEN
7549             debug('REV (ccid not found) test PASSED');
7550         ELSE
7551             debug('REV (ccid not found) test FAILED');
7552         END IF;
7553 
7554     EXCEPTION
7555       WHEN OTHERS THEN
7556         debug('REV (ccid not found) test FAILED');
7557     END;
7558 
7559 
7560     -- Test REV (ccid found)
7561     --
7562     BEGIN
7563         flex_manager( REV, '', 1011, 1000, 1, '', '', '', '','',
7564                                   l_ccid,
7565                                   l_segs,
7566                                   l_int_ccid,
7567                                   l_int_segs );
7568 
7569         IF( l_ccid = 1137 AND l_segs = '01.100.4300.000.000.000' ) THEN
7570             debug('REV (ccid found) test PASSED');
7571         ELSE
7572             debug('REV (ccid found) test FAILED');
7573         END IF;
7574 
7575     EXCEPTION
7576       WHEN OTHERS THEN
7577         debug('REV (ccid found) test FAILED');
7578     END;
7579 
7580 
7581     -- Test REV (ccid found)
7582     --
7583     BEGIN
7584         flex_manager( REV, '', 1011, 1000, '', 1001, '', '', '','',
7585                                   l_ccid,
7586                                   l_segs,
7587                                   l_int_ccid,
7588                                   l_int_segs );
7589 
7590         IF( l_ccid = 1137 AND l_segs = '01.100.4300.000.000.000' ) THEN
7591             debug('REV (ccid found) test PASSED');
7592         ELSE
7593             debug('REV (ccid found) test FAILED');
7594         END IF;
7595 
7596     EXCEPTION
7597       WHEN OTHERS THEN
7598         debug('REV (ccid found) test FAILED');
7599     END;
7600 
7601 
7602     -- Test CHARGES (ccid found)
7603     --
7604     BEGIN
7605         flex_manager( CHARGES, '', 1011, 1000, 1, '', '', '', '','',
7606                                   l_ccid,
7607                                   l_segs,
7608                                   l_int_ccid,
7609                                   l_int_segs );
7610 
7611         IF( l_ccid = 1137 AND l_segs = '01.100.4300.000.000.000' ) THEN
7612             debug('CHARGES (ccid found) test PASSED');
7613         ELSE
7614             debug('CHARGES (ccid found) test FAILED');
7615         END IF;
7616 
7617     EXCEPTION
7618       WHEN OTHERS THEN
7619         debug('CHARGES (ccid found) test FAILED');
7620     END;
7621 
7622 
7623     -- Test FREIGHT (ccid found)
7624     --
7625     BEGIN
7626         flex_manager( FREIGHT, '', 1011, 1000, 1, '', '', '', '','',
7627                                   l_ccid,
7628                                   l_segs,
7629                                   l_int_ccid,
7630                                   l_int_segs );
7631 
7632         IF( l_ccid = 1098 AND l_segs = '01.100.5650.000.000.000' ) THEN
7633             debug('FREIGHT (ccid found) test PASSED');
7634         ELSE
7635             debug('FREIGHT (ccid found) test FAILED');
7636         END IF;
7637 
7638     EXCEPTION
7639       WHEN OTHERS THEN
7640         debug('FREIGHT (ccid found) test FAILED');
7641     END;
7642 
7643 
7644     -- Test TAX (ccid found)
7645     --
7646     BEGIN
7647         flex_manager( TAX, '', 1011, 1001, 1, '', 1098, '', '','',
7648                                   l_ccid,
7649                                   l_segs,
7650                                   l_int_ccid,
7651                                   l_int_segs );
7652 
7653         IF( l_ccid = 1215 AND l_segs = '01.500.5650.000.000.000' ) THEN
7654             debug('TAX (ccid found) test PASSED');
7655         ELSE
7656             debug('TAX (ccid found) test FAILED');
7657         END IF;
7658 
7659     EXCEPTION
7660       WHEN OTHERS THEN
7661         debug('TAX (ccid found) test FAILED');
7662     END;
7663 
7664 
7665     -- Test UNBILL (ccid found)
7666     --
7667     BEGIN
7668         flex_manager( UNBILL, '', 1011, 1001, 1, '', '', '', '','',
7669                                   l_ccid,
7670                                   l_segs,
7671                                   l_int_ccid,
7672                                   l_int_segs );
7673 
7674         IF( l_ccid = 1213 AND l_segs = '01.500.1103.000.000.000' ) THEN
7675             debug('UNBILL (ccid found) test PASSED');
7676         ELSE
7677             debug('UNBILL (ccid found) test FAILED');
7678         END IF;
7679 
7680     EXCEPTION
7681       WHEN OTHERS THEN
7682         debug('UNBILL (ccid found) test FAILED');
7683     END;
7684 
7685     -- Test UNEARN (ccid found)
7686     --
7687     BEGIN
7688         flex_manager( UNEARN, '', 1011, 1001, 1, '', '', '', '','',
7689                                   l_ccid,
7690                                   l_segs,
7691                                   l_int_ccid,
7692                                   l_int_segs );
7693 
7694         IF( l_ccid = 1216 AND l_segs = '01.500.4400.000.000.000' ) THEN
7695             debug('UNEARN (ccid found) test PASSED');
7696         ELSE
7697             debug('UNEARN (ccid found) test FAILED');
7698         END IF;
7699 
7700     EXCEPTION
7701       WHEN OTHERS THEN
7702         debug('UNEARN (ccid found) test FAILED');
7703     END;
7704 
7705     -- Test SUSPENSE (ccid found)
7706     --
7707     BEGIN
7708         flex_manager( SUSPENSE, '', 1011, 1000, 2, '', '', '', '','',
7709                                   l_ccid,
7710                                   l_segs,
7711                                   l_int_ccid,
7712                                   l_int_segs );
7713 
7714         IF( l_ccid = 1111 AND l_segs = '01.100.5999.000.000.000' ) THEN
7715             debug('SUSPENSE (ccid found) test PASSED');
7716         ELSE
7717             debug('SUSPENSE (ccid found) test FAILED');
7718         END IF;
7719 
7720     EXCEPTION
7721       WHEN OTHERS THEN
7722         debug('SUSPENSE (ccid found) test FAILED');
7723     END;
7724 
7725 EXCEPTION
7726     WHEN OTHERS THEN
7727         debug('arp_auto_accounting.test_harness()');
7728         debug(SQLERRM);
7729         RAISE;
7730 END test_harness;
7731 
7732 ----------------------------------------------------------------------------
7733 PROCEDURE test_wes IS
7734 
7735     l_segs VARCHAR2(200);
7736     l_ccid BINARY_INTEGER;
7737     l_int_segs VARCHAR2(200);
7738     l_int_ccid BINARY_INTEGER;
7739     l_errorbuf VARCHAR2(512);
7740     l_x BINARY_INTEGER;
7741 
7742 BEGIN
7743 
7744     -- enable_debug;
7745 
7746     -- Invalid account class
7747     --
7748 
7749     BEGIN
7750         null;
7751         flex_manager( 'REVX', '', 1011, 1000, '', '', '', '', '','', l_ccid, l_segs, l_int_ccid, l_int_segs );
7752 
7753     EXCEPTION
7754       WHEN invalid_account_class THEN
7755         debug('invalid account class test PASSED');
7756       WHEN OTHERS THEN
7757         debug('invalid account class test FAILED');
7758 
7759     END;
7760 
7761 
7762 EXCEPTION
7763     WHEN OTHERS THEN
7764         debug('arp_auto_accounting.test_harness()');
7765         debug(SQLERRM);
7766         RAISE;
7767 END test_wes ;
7768 
7769 
7770 ----------------------------------------------------------------------------
7771 PROCEDURE test_build_sql IS
7772 
7773     select_stmt VARCHAR2(32767);
7774     delete_stmt VARCHAR2(32767);
7775     mycursor integer;
7776 
7777 BEGIN
7778 
7779     -- enable_debug;
7780     select_stmt :=
7781      build_select_sql(system_info, profile_info,
7782                       REV, 1, 2, 3, 12,
7783                       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL );
7784 
7785 
7786     disable_debug;
7787 
7788 EXCEPTION
7789     WHEN OTHERS THEN
7790         debug('EXCEPTION: arp_auto_accounting.test_build_sql()');
7791         debug(SQLERRM);
7792         RAISE;
7793 END test_build_sql;
7794 
7795 ----------------------------------------------------------------------------
7796 PROCEDURE test_do_autoacc IS
7797 
7798     ccid BINARY_INTEGER;
7799     concat_segments VARCHAR2(1000);
7800     x binary_integer;
7801     y binary_integer;
7802     errorbuf VARCHAR2(1000);
7803 
7804 BEGIN
7805 
7806     -- enable_debug;
7807 
7808     debug('Insert Mode');
7809     do_autoaccounting('I', REV, 1072, 1511, null, null,
7810                        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7811                        ccid, concat_segments, y );
7812 
7813     debug('Update Mode');
7814     do_autoaccounting('U', REV, 1, 2, 3, 12,
7815                        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7816                        ccid, concat_segments, y );
7817 
7818     debug('Delete Mode');
7819     do_autoaccounting('D', REV, 1, 2, 3, 12,
7820                        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7821                        ccid, concat_segments, y );
7822 
7823     debug('Get Mode');
7824     do_autoaccounting('G', REV, 1, 2, 3, 12,
7825                        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7826                        ccid, concat_segments, y );
7827 
7828     debug('Insert Mode: ALL');
7829     do_autoaccounting('I', 'ALL', 1072, 1511, null, null,
7830                        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7831                        ccid, concat_segments, y );
7832 
7833     disable_debug;
7834 
7835 EXCEPTION
7836     WHEN OTHERS THEN
7837         debug('EXCEPTION: arp_auto_accounting.test_do_autoacc()');
7838         debug(SQLERRM);
7839         RAISE;
7840 END test_do_autoacc;
7841 
7842 
7843 
7844 
7845 ----------------------------------------------------------------------------
7846 -- Constructor code
7847 ----------------------------------------------------------------------------
7848 --begin anuj
7849 /* Multi-Org Access Control Changes for SSA;Begin;anukumar;11/01/2002*/
7850 PROCEDURE init is
7851 BEGIN
7852     print_fcn_label( 'arp_auto_accounting.constructor()+' );
7853 
7854     ------------------------------------------------------------------------
7855     -- Load autoaccounting definition into plsql tables
7856     ------------------------------------------------------------------------
7857     load_autoacc_def;
7858     system_info  := arp_trx_global.system_info;
7859 
7860     /* Bug 2560036 - determine if collectibility is enabled */
7861     g_test_collectibility :=
7862          ar_revenue_management_pvt.revenue_management_enabled;
7863 
7864     ------------------------------------------------------------------------
7865     -- Additional system info
7866     ------------------------------------------------------------------------
7867     BEGIN
7868 
7869         system_info.rev_based_on_salesrep :=
7870             query_autoacc_def( REV, 'RA_SALESREPS' );
7871 
7872         system_info.tax_based_on_salesrep :=
7873             query_autoacc_def( TAX, 'RA_SALESREPS' );
7874 
7875         system_info.unbill_based_on_salesrep :=
7876             query_autoacc_def( UNBILL, 'RA_SALESREPS' );
7877 
7878         system_info.unearn_based_on_salesrep :=
7879             query_autoacc_def( UNEARN, 'RA_SALESREPS' );
7880 
7881         system_info.suspense_based_on_salesrep :=
7882             query_autoacc_def( SUSPENSE, 'RA_SALESREPS' );
7883 
7884 
7885     EXCEPTION
7886         WHEN OTHERS THEN
7887             arp_util.debug('Error getting system information');
7888             RAISE;
7889     END;
7890 
7891 
7892     get_error_message_text;
7893 
7894     dump_info;
7895 
7896     print_fcn_label( 'arp_auto_accounting.constructor()-' );
7897 
7898 EXCEPTION
7899     WHEN OTHERS THEN
7900         debug('EXCEPTION: arp_auto_accounting.constructor');
7901         debug(SQLERRM);
7902         RAISE;
7903 END init;
7904 BEGIN
7905 init;
7906 
7907 END arp_auto_accounting;