DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_ALLOCATION_PKG

Source


1 PACKAGE BODY ARP_ALLOCATION_PKG  AS
2 /* $Header: ARALLOCB.pls 120.115 2011/04/01 02:31:46 spdixit ship $ */
3 
4 /*=======================================================================+
5  |  Global Constants
6  +=======================================================================*/
7   g_ae_doc_rec                  ae_doc_rec_type;
8   g_ae_event_rec                ae_event_rec_type;
9   g_ae_rule_rec                 ae_rule_rec_type;
10   g_ae_sys_rec                  ae_sys_rec_type;
11   g_ae_curr_rec                 ae_curr_rec_type;
12   g_cust_inv_rec                ra_customer_trx%ROWTYPE;
13 
14   g_ae_summarize_tbl            ae_line_tbl_type;
15   g_ae_empty_line_tbl           ae_line_tbl_type;
16   g_ae_rev_ctr                  BINARY_INTEGER := 0; -- Count of revenue lines
17   g_ae_tax_ctr                  BINARY_INTEGER := 0; -- Count of tax lines
18   g_ae_unearn_rev_ctr           BINARY_INTEGER := 0; -- Count of unearn and unbilled lines
19   g_ae_ctr                      BINARY_INTEGER := 0;
20   g_ae_summ_ctr                 BINARY_INTEGER := 0;
21 
22   g_amount_due_remaining        NUMBER;
23   g_acctd_amount_due_remaining  NUMBER;
24   g_amount_due_original         NUMBER;
25   g_orig_line_amt_alloc         NUMBER;
26   g_orig_line_acctd_amt_alloc   NUMBER;
27   g_sum_unearn_rev_amt          NUMBER;
28   g_sum_unearn_rev_acctd_amt    NUMBER;
29 
30   g_ae_def_tax                  BOOLEAN;
31   g_done_def_tax                BOOLEAN;
32   g_bound_tax                   BOOLEAN;
33   g_bound_freight               BOOLEAN;
34   g_bound_activity              BOOLEAN;
35   g_added_tax                   BOOLEAN;
36   g_ovrrd_code                  VARCHAR2(1);
37   g_id                          NUMBER := 0;
38   g_ed_adj_activity_link        NUMBER := 0;
39   g_uned_activity_link          NUMBER := 0;
40   g_link_ctr                    NUMBER := 0;
41   g_bulk_fetch_rows             NUMBER := 400;
42   g_override1                   VARCHAR2(1);
43   g_override2                   VARCHAR2(1);
44   g_br_cust_trx_line_id         ra_customer_trx_lines.customer_trx_line_id%TYPE;
45   g_simul_app                   VARCHAR2(1);
46   g_ae_code_combination_id_app  NUMBER;
47 
48   g_exec                  VARCHAR2(30);
49   g_receivables_trx_id    NUMBER;
50   g_prim_det_dist_done    VARCHAR2(1) := 'N';
51 
52   g_adj_act_gl_acct_ccid    NUMBER := -9999;
53 
54 /*==============================================================================+
55  | Private Procedure/Function prototypes                                        |
56  +==============================================================================*/
57   PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('AFLOG_ENABLED'), 'N');
58   PG_DEL_FRM_GT_CNT NUMBER := NVL(FND_PROFILE.value('AR_DEL_FRM_GT_CNT'), 1000);
59 
60   PROCEDURE adj_boundary_account (p_receivables_trx_id   IN     NUMBER,
61                                   p_bucket               IN     VARCHAR2,
62                                   p_ctlgd_id             IN     NUMBER,
63                                   x_ccid                 IN OUT NOCOPY NUMBER);
64 
65   PROCEDURE Get_Tax_Curr(p_invoice_id          IN ra_customer_trx.customer_trx_id%TYPE,
66                          p_payment_schedule_id IN ar_payment_schedules.payment_schedule_id%TYPE);
67 
68   PROCEDURE Get_Invoice_Distributions(p_invoice_id     IN NUMBER,
69                                       p_app_rec        IN ar_receivable_applications%ROWTYPE,
70                                       p_adj_rec        IN ar_adjustments%ROWTYPE,
71                                       p_process_ed_adj IN VARCHAR2,
72                                       p_process_uned   IN VARCHAR2,
73                                       p_process_pay    IN VARCHAR2);
74 
75   PROCEDURE Override_Accounts(p_app_rec   IN  ar_receivable_applications%ROWTYPE,
76                               p_adj_rec   IN  ar_adjustments%ROWTYPE,
77                               p_override1 OUT NOCOPY  VARCHAR2,
78                               p_override2 OUT NOCOPY VARCHAR2);
79 
80   PROCEDURE Check_Entry(p_invoice_id   IN  ra_customer_trx.customer_trx_id%TYPE,
81                         p_app_rec      IN  ar_receivable_applications%ROWTYPE,
82                         p_adj_rec      IN  ar_adjustments%ROWTYPE,
83                         p_required     OUT NOCOPY BOOLEAN);
84 
85   PROCEDURE Get_Tax_Link_Id(p_process_ed_adj IN VARCHAR2,
86                             p_process_uned   IN VARCHAR2,
87                             p_process_pay    IN VARCHAR2);
88 
89   FUNCTION  Get_Acctd_Amt(p_amount       IN NUMBER ) RETURN NUMBER;
90 
91   PROCEDURE Process_Amounts(p_app_rec   IN ar_receivable_applications%ROWTYPE,
92                             p_adj_rec   IN ar_adjustments%ROWTYPE);
93 
94   PROCEDURE Doc_Tax_Acct_Rule(p_type_acct IN VARCHAR2                           ,
95                               p_app_rec   IN ar_receivable_applications%ROWTYPE ,
96                               p_adj_rec   IN ar_adjustments%ROWTYPE              );
97 
98   PROCEDURE Init_Amts(p_type_acct    IN VARCHAR2                          ,
99                       p_app_rec      IN ar_receivable_applications%ROWTYPE,
100                       p_adj_rec      IN ar_adjustments%ROWTYPE             );
101 
102   PROCEDURE Gross_To_Activity_GL(p_type_acct      IN VARCHAR2);
103 
104   PROCEDURE Init_Rev_Tax_Tab;
105 
106   PROCEDURE Alloc_Rev_Tax_Amt(p_type_acct IN VARCHAR2);
107 
108   PROCEDURE Set_Taxable_Amt(p_type_acct IN VARCHAR2);
109 
110   PROCEDURE Set_Taxable_Split_Amt(p_type_acct IN VARCHAR2);
111 
112   PROCEDURE Allocate_Tax_To_Rev(p_type_acct   IN VARCHAR2);
113 
114   PROCEDURE Set_Rev_Links(p_type_acct         IN VARCHAR2);
115 
116   PROCEDURE Get_Rules(p_type_acct            IN     VARCHAR2,
117                       p_gl_account_source    OUT NOCOPY   VARCHAR2,
118                       p_tax_code_source      OUT NOCOPY   VARCHAR2,
119                       p_tax_recoverable_flag OUT NOCOPY   VARCHAR2);
120 
121   PROCEDURE Build_Lines;  --(p_type_acct      IN VARCHAR2);
122 
123   PROCEDURE Build_Rev(p_gl_account_source     IN ar_receivables_trx.gl_account_source%TYPE     ,
124                       p_tax_code_source       IN ar_receivables_trx.tax_code_source%TYPE       ,
125                       p_tax_recoverable_flag  IN ar_receivables_trx.tax_recoverable_flag%TYPE  ,
126                       p_ae_line_init_rec      IN ar_ae_alloc_rec_gt%ROWTYPE                       );
127 
128   PROCEDURE Build_Tax(p_tax_code_source       IN ar_receivables_trx.tax_code_source%TYPE       ,
129                       p_tax_recoverable_flag  IN ar_receivables_trx.tax_recoverable_flag%TYPE  ,
130                       p_ae_line_init_rec      IN ar_ae_alloc_rec_gt%ROWTYPE                       );
131 
132   PROCEDURE Build_Charges_Freight_All(p_type_acct         IN VARCHAR2          ,
133                                       p_ae_line_init_rec  IN ar_ae_alloc_rec_gt%ROWTYPE,
134                                       p_build_all         IN BOOLEAN );
135 
136   PROCEDURE Create_Debits_Credits(p_amount               IN NUMBER       ,
137                                   p_acctd_amount         IN NUMBER       ,
138                                   p_taxable_amount       IN NUMBER       ,
139                                   p_taxable_acctd_amount IN NUMBER       ,
140                                   p_from_amount          IN NUMBER       ,
141                                   p_from_acctd_amount    IN NUMBER       ,
142                                   p_ae_line_rec          IN OUT NOCOPY ar_ae_alloc_rec_gt%ROWTYPE,
143                                   p_paired_flag          IN VARCHAR2 DEFAULT NULL,
144                                   p_calling_point        IN VARCHAR2 DEFAULT NULL);
145 
146 
147   PROCEDURE Summarize_Accounting_Lines;
148 
149   PROCEDURE Summarize_Acct_Lines_Hdr_Level;
150 
151   PROCEDURE Assign_Elements(p_ae_line_rec           IN ar_ae_alloc_rec_gt%ROWTYPE);
152 
153   PROCEDURE Insert_Ae_Lines(p_ae_line_tbl IN ar_ae_alloc_rec_gt%ROWTYPE);
154 
155   PROCEDURE Cache_Ae_Lines(p_ae_line_tbl IN ar_ae_alloc_rec_gt%ROWTYPE);
156 
157   PROCEDURE Dump_Alloc_Rev_Tax(p_type      IN VARCHAR2,
158                                p_alloc_rec IN ar_ae_alloc_rec_gt%ROWTYPE);
159 
160   PROCEDURE Dump_Init_Amts(p_type_acct    IN VARCHAR2                            ,
161                            p_app_rec      IN ar_receivable_applications%ROWTYPE  ,
162                            p_adj_rec      IN ar_adjustments%ROWTYPE               );
163 
164   PROCEDURE Dump_Line_Amts(p_ae_line_rec  IN ar_ae_alloc_rec_gt%ROWTYPE);
165 
166   PROCEDURE Dump_Dist_Amts(p_ae_line_rec  ar_distributions%ROWTYPE);
167 
168   FUNCTION source_exec(p_process_ed_adj   IN VARCHAR2,
169                        p_process_uned     IN VARCHAR2,
170                        p_process_pay      IN VARCHAR2,
171                        p_source_table     IN VARCHAR2) RETURN VARCHAR2;
172 
173   FUNCTION source_exec(p_type_acct       IN VARCHAR2,
174                        p_source_table    IN VARCHAR2) RETURN VARCHAR2;
175 
176   FUNCTION the_tax_code_source(p_bucket   IN VARCHAR2) RETURN VARCHAR2;
177 
178   FUNCTION the_tax_recoverable_flag(p_bucket   IN VARCHAR2) RETURN VARCHAR2;
179 
180   FUNCTION the_gl_account_source(p_bucket   IN VARCHAR2) RETURN VARCHAR2;
181 
182   FUNCTION get_adj_act_ccid RETURN NUMBER;
183 
184 --BUG#5245153
185 FUNCTION get_adj_act_ccid RETURN NUMBER
186 IS
187 CURSOR c IS
188 SELECT CODE_COMBINATION_ID
189   FROM ar_adjustments
190  WHERE adjustment_id = g_ae_doc_rec.document_id;
191 BEGIN
192   IF g_adj_act_gl_acct_ccid   = -9999 THEN
193     IF g_ae_doc_rec.source_table = 'ADJ' THEN
194     OPEN c;
195     FETCH c INTO g_adj_act_gl_acct_ccid ;
196     IF c%NOTFOUND THEN
197      g_adj_act_gl_acct_ccid := -1;
198     END IF;
199     CLOSE c;
200     END IF;
201   END IF;
202   RETURN g_adj_act_gl_acct_ccid;
203 END;
204 
205 FUNCTION the_gl_account_source(p_bucket   IN VARCHAR2)
206 RETURN VARCHAR2
207 IS
208     l_res    VARCHAR2(30);
209 BEGIN
210     IF    p_bucket IN ('ADJ_LINE' ,'ADJ_TAX'  , 'ADJ_FRT'  , 'ADJ_CHRG') THEN
211       l_res := g_ae_rule_rec.gl_account_source1;
212     ELSIF p_bucket IN ('APP_LINE' ,'APP_TAX'  , 'APP_FRT'  , 'APP_CHRG') THEN
213       l_res := 'ACTIVITY_GL_ACCOUNT';
214     ELSIF p_bucket IN ('ED_LINE' ,'ED_TAX'  , 'ED_FRT'  , 'ED_CHRG') THEN
215       l_res := g_ae_rule_rec.gl_account_source1;
216     ELSIF p_bucket IN ('UNED_LINE' ,'UNED_TAX'  , 'UNED_FRT'  , 'UNED_CHRG') THEN
217       l_res := g_ae_rule_rec.gl_account_source2;
218     ELSE
219       l_res :=  g_ae_rule_rec.tax_code_source1;
220     END IF;
221     arp_standard.debug(' the_gl_account_source for bucket '||p_bucket||' is '||l_res);
222     RETURN l_res;
223 END;
224 
225 
226 FUNCTION the_tax_code_source
227 (p_bucket   IN VARCHAR2)
228 RETURN VARCHAR2
229 IS
230   l_res     VARCHAR2(30);
231 BEGIN
232   IF    p_bucket IN ('ADJ_LINE' ,'ADJ_TAX'  , 'ADJ_FRT'  , 'ADJ_CHRG') THEN
233      l_res := g_ae_rule_rec.tax_code_source1;
234   ELSIF p_bucket IN ('APP_LINE' ,'APP_TAX'  , 'APP_FRT'  , 'APP_CHRG') THEN
235      l_res :=  'INVOICE';
236   ELSIF p_bucket IN ('ED_LINE' ,'ED_TAX'  , 'ED_FRT'  , 'ED_CHRG') THEN
237      l_res :=  g_ae_rule_rec.tax_code_source1;
238   ELSIF p_bucket IN ('UNED_LINE' ,'UNED_TAX'  , 'UNED_FRT'  , 'UNED_CHRG') THEN
239      l_res := g_ae_rule_rec.tax_code_source2;
240   ELSE
241      l_res :=  g_ae_rule_rec.tax_code_source1;
242   END IF;
243   arp_standard.debug(' the_tax_code_source for bucket '||p_bucket||' is '|| l_res);
244   RETURN l_res;
245 END;
246 
247 FUNCTION the_tax_recoverable_flag
248 (p_bucket   IN VARCHAR2)
249 RETURN VARCHAR2
250 IS
251   l_res    VARCHAR2(30);
252 BEGIN
253  IF p_bucket IN ('ADJ_LINE','ADJ_TAX'  ,'ADJ_FRT'  , 'ADJ_CHRG' ) THEN
254     l_res :=  g_ae_rule_rec.tax_recoverable_flag1;
255  ELSIF p_bucket IN ('APP_LINE','APP_TAX'  ,'APP_FRT'  , 'APP_CHRG' ) THEN
256      l_res := 'Y';
257  ELSIF p_bucket IN ('ED_LINE','ED_TAX'  ,'ED_FRT'  , 'ED_CHRG' ) THEN
258      l_res := g_ae_rule_rec.tax_recoverable_flag1;
259  ELSIF p_bucket IN ('UNED_LINE','UNED_TAX' ,'UNED_FRT' ,'UNED_CHRG') THEN
260      l_res := g_ae_rule_rec.tax_recoverable_flag2;
261  ELSE
262      l_res := g_ae_rule_rec.tax_recoverable_flag1;
263  END IF;
264  arp_standard.debug(' the_tax_recoverable_flag for bucket '||p_bucket||' is '|| l_res);
265  RETURN l_res;
266 END;
267 
268 FUNCTION source_exec
269 (p_process_ed_adj   IN VARCHAR2,
270  p_process_uned     IN VARCHAR2,
271  p_process_pay      IN VARCHAR2,
272  p_source_table     IN VARCHAR2)
273 RETURN VARCHAR2
274 IS
275   l_source_exec  VARCHAR2(30);
276 BEGIN
277   l_source_exec := NULL;
278   arp_standard.debug('source_exec +');
279   arp_standard.debug('  p_process_ed_adj :'||p_process_ed_adj);
280   arp_standard.debug('  p_process_uned   :'||p_process_uned  );
281   arp_standard.debug('  p_process_pay    :'||p_process_pay  );
282   arp_standard.debug('  p_source_table   :'||p_source_table  );
283   IF    p_process_ed_adj = 'Y' AND p_source_table = 'ADJ' THEN
284     l_source_exec := 'ADJ';
285   ELSIF p_process_ed_adj = 'Y' AND p_source_table = 'RA' THEN
286     l_source_exec := 'ED';
287   ELSIF p_process_uned = 'Y' AND p_source_table = 'RA' THEN
288     l_source_exec := 'UNED';
289   ELSIF p_process_pay  = 'Y' AND p_source_table = 'RA' THEN
290     l_source_exec := 'PAY';
291   END IF;
292   arp_standard.debug('  l_source_exec :'||l_source_exec);
293   arp_standard.debug('source_exec -');
294   RETURN l_source_exec;
295 END;
296 
297 
298 FUNCTION source_exec
299 (p_type_acct       IN VARCHAR2,
300  p_source_table     IN VARCHAR2)
301 RETURN VARCHAR2
302 IS
303   l_source_exec  VARCHAR2(30);
304   l_process_ed_adj  VARCHAR2(1) := 'N';
305   l_process_uned    VARCHAR2(1) := 'N';
306   l_process_pay     VARCHAR2(1) := 'N';
307 BEGIN
308   l_source_exec := NULL;
309   arp_standard.debug('source_exec +');
310   arp_standard.debug('  p_type_acct      :'||p_type_acct);
311   arp_standard.debug('  p_source_table   :'||p_source_table  );
312   IF     p_type_acct = 'ED_ADJ' THEN
313      l_process_ed_adj := 'Y';
314   ELSIF  p_type_acct = 'UNED' THEN
315      l_process_uned   := 'Y';
316   ELSIF  p_type_acct = 'PAY' THEN
317      l_process_pay    := 'Y';
318   END IF;
319   l_source_exec := source_exec(p_process_ed_adj   => l_process_ed_Adj,
320                                p_process_uned     => l_process_uned,
321                                p_process_pay      => l_process_pay,
322                                p_source_table     => p_source_table);
323 
324   arp_standard.debug('  l_source_exec :'||l_source_exec);
325   arp_standard.debug('source_exec -');
326   RETURN l_source_exec;
327 END;
328 
329 /* =======================================================================
330  | PROCEDURE Allocate_Tax
331  |
332  | DESCRIPTION
333  |      This procedure is the cover routine which will tax account for
334  |      discounts, adjustments and finance charges. The rule details
335  |      and document, event details are passed to this procedure which will
336  |      help determine the manner in which discounts and adjustments are
337  |      allocated over specific accounts based on Activity Rule.
338  |
339  | PARAMETERS
340  |      p_ae_doc_rec            IN      Document record
341  |      p_ae_event_rec          IN      Event record
342  |      p_app_rec               IN      Application record for discounts
343  |      p_adj_rec               IN      Adjustment record for adjustments
344  |      p_ae_rule_rec           IN      Rule record
345  |      p_ae_line_tbl           OUT     Table with accounting for discounts
346  |                                      or adjustments
347  * ======================================================================*/
348 PROCEDURE Allocate_Tax (
349                   p_ae_doc_rec    IN      ae_doc_rec_type,
350                   p_ae_event_rec  IN      ae_event_rec_type,
351                   p_ae_rule_rec   IN      ae_rule_rec_type,
352                   p_app_rec       IN      ar_receivable_applications%ROWTYPE,
353                   p_cust_inv_rec  IN      ra_customer_trx%ROWTYPE,
354                   p_adj_rec       IN      ar_adjustments%ROWTYPE,
355                   p_ae_ctr        IN OUT NOCOPY  BINARY_INTEGER,
356                   p_ae_line_tbl   IN OUT NOCOPY  ae_line_tbl_type,
357                   p_br_cust_trx_line_id IN ra_customer_trx_lines.customer_trx_line_id%TYPE default NULL,
358                   p_simul_app      IN     VARCHAR2 default NULL,
359                   p_from_llca_call IN     VARCHAR2 DEFAULT 'N',
360                   p_gt_id          IN     NUMBER   DEFAULT NULL,
361                   -- this flag is introduced to indicate if the application need conversion
362                   p_inv_cm         IN     VARCHAR2 DEFAULT 'I'
363                     ) IS
364 
365 l_invoice_id          ra_customer_trx.customer_trx_id%TYPE          ;
366 l_payment_schedule_id ar_payment_schedules.payment_schedule_id%TYPE ;
367 l_ed_adj_acct   VARCHAR2(10)   := 0;
368 l_uned_acct     VARCHAR2(10)   := 0;
369 l_linked_tax    BOOLEAN        := FALSE;
370 l_required      BOOLEAN        := TRUE;
371 l_rev_rec_req   BOOLEAN        := TRUE;
372 l_sum_dist      NUMBER;
373 l_gl_account_source_old     ar_receivables_trx.gl_account_source%TYPE;
374 l_tax_code_source_old       ar_receivables_trx.tax_code_source%TYPE;
375 l_tax_recoverable_flag_old  ar_receivables_trx.tax_recoverable_flag%TYPE;
376 l_process_ed_adj  VARCHAR2(1) := 'N';
377 l_process_uned    VARCHAR2(1) := 'N';
378 l_process_pay     VARCHAR2(1) := 'N';
379 l_le_id           NUMBER;
380 l_msg_count       NUMBER;
381 l_msg_data        VARCHAR2(1024);
382 l_effective_date  DATE;
383 l_return_status   VARCHAR2(10);
384 --{
385 l_line_adj          VARCHAR2(50);
386 l_tax_adj           VARCHAR2(50);
387 l_frt_adj           VARCHAR2(50);
388 l_chrg_adj          VARCHAR2(50);
389 l_cm_app            VARCHAR2(1) := 'N';
390 l_gt_id             NUMBER;
391 l_cnt               NUMBER;
392 l_type              VARCHAR2(30);
393 --}
394 What_kind_of_activity   EXCEPTION;
395 impossible_adjust       EXCEPTION;
396 BEGIN
397 --  IF PG_DEBUG in ('Y', 'C') THEN
398      arp_standard.debug(   'ARP_ALLOCATION_PKG.Allocate_Tax()+');
399 --  END IF;
400 
401 /*----------------------------------------------------------------------------+
402  | Assign globals and get system options info                                 |
403  +----------------------------------------------------------------------------*/
404   g_ae_doc_rec                   := p_ae_doc_rec        ;
405   g_ae_event_rec                 := p_ae_event_rec      ;
406   g_ae_rule_rec                  := p_ae_rule_rec       ;
407 
408   g_cust_inv_rec                 := p_cust_inv_rec      ;
409 
410   g_ae_sys_rec.set_of_books_id   := ARP_ACCT_MAIN.ae_sys_rec.set_of_books_id;
411   g_ae_sys_rec.gain_cc_id        := ARP_ACCT_MAIN.ae_sys_rec.gain_cc_id;
412   g_ae_sys_rec.loss_cc_id        := ARP_ACCT_MAIN.ae_sys_rec.loss_cc_id;
413   g_ae_sys_rec.round_cc_id       := ARP_ACCT_MAIN.ae_sys_rec.round_cc_id;
414   g_ae_sys_rec.coa_id            := ARP_ACCT_MAIN.ae_sys_rec.coa_id;
415   g_ae_sys_rec.base_currency     := ARP_ACCT_MAIN.ae_sys_rec.base_currency;
416   g_ae_sys_rec.base_precision    := ARP_ACCT_MAIN.ae_sys_rec.base_precision;
417   g_ae_sys_rec.base_min_acc_unit := ARP_ACCT_MAIN.ae_sys_rec.base_min_acc_unit;
418 
419   -- MRC TRIGGER replacement
420   -- Initialize a new global variable:
421   g_ae_sys_rec.sob_type          := NVL(ARP_ACCT_MAIN.ae_sys_rec.sob_type,'P');
422 
423   g_ae_code_combination_id_app := p_app_rec.code_combination_id;
424   g_ae_rev_ctr         := 0;
425   g_ae_tax_ctr         := 0;
426   g_ae_unearn_rev_ctr  := 0;
427   g_ae_ctr             := 0;
428   g_ae_summ_ctr        := 0;
429   g_ae_summarize_tbl   := g_ae_empty_line_tbl ;
430 
431   g_amount_due_remaining       := 0;
432   g_acctd_amount_due_remaining := 0;
433   g_amount_due_original        := 0;
434 
435   g_sum_unearn_rev_amt         := 0;
436   g_sum_unearn_rev_acctd_amt   := 0;
437 
438   g_ae_def_tax           := FALSE;
439   g_done_def_tax         := FALSE;
440   g_bound_tax            := FALSE;
441   g_bound_freight        := FALSE;
442   g_bound_activity       := FALSE;
443   g_added_tax            := FALSE;
444   g_ovrrd_code           := '';
445 
446 --  ER :  LIne Level adjustment API- Changed the p_gt_id check , Placed the NVL
447 
448   IF NVL(p_gt_id,0) = 0  THEN
449     SELECT ar_distribution_split_s.NEXTVAL INTO g_id FROM DUAL;
450   ELSE
451     g_id                   := p_gt_id;
452   END IF;
453   arp_standard.debug(' p_gt_id  :'||p_gt_id);
454   arp_standard.debug(' g_id     :'||g_id);
455   --}
456   g_ed_adj_activity_link := 0;
457   g_uned_activity_link   := 0;
458   g_link_ctr             := 0;
459   adj_code_combination_id := '';
460   g_override1            := '';
461   g_override2            := '';
462 
463 --set the BR cust trx line id i.e. line id of assignment on BR document
464   IF p_br_cust_trx_line_id IS NOT NULL THEN
465     g_br_cust_trx_line_id := p_br_cust_trx_line_id;
466 
467   ELSE
468 
469     g_br_cust_trx_line_id := NULL;
470   END IF;
471 
472 --set the Simulation flag
473   IF p_simul_app IS NOT NULL AND p_simul_app = 'Y' THEN
474     g_simul_app  := p_simul_app;
475 
476   ELSE
477     g_simul_app  := NULL;
478 
479   END IF;
480 
481   arp_standard.debug('g_ae_doc_rec+');
482   arp_standard.debug(' g_ae_doc_rec.document_type:'||g_ae_doc_rec.document_type);
483   arp_standard.debug(' g_ae_doc_rec.document_id  :'||g_ae_doc_rec.document_id);
484   arp_standard.debug(' g_ae_doc_rec.accounting_entity_level:'||g_ae_doc_rec.accounting_entity_level);
485   arp_standard.debug(' g_ae_doc_rec.source_table:  '||g_ae_doc_rec.source_table);
486   arp_standard.debug(' g_ae_doc_rec.source_id:     '||g_ae_doc_rec.source_id);
487   arp_standard.debug(' g_ae_doc_rec.source_id_old: '||g_ae_doc_rec.source_id_old);
488   arp_standard.debug(' g_ae_doc_rec.other_flag:    '||g_ae_doc_rec.other_flag);
489   arp_standard.debug(' g_ae_doc_rec.miscel1:       '||g_ae_doc_rec.miscel1);
490   arp_standard.debug(' g_ae_doc_rec.event:         '||g_ae_doc_rec.event);
491   arp_standard.debug(' g_ae_doc_rec.deferred_tax:  '||g_ae_doc_rec.deferred_tax);
492   arp_standard.debug(' g_ae_doc_rec.pay_sched_upd_yn:'||g_ae_doc_rec.pay_sched_upd_yn);
493   arp_standard.debug(' g_ae_doc_rec.pay_sched_upd_cm_yn:'||g_ae_doc_rec.pay_sched_upd_cm_yn);
494   arp_standard.debug(' g_ae_doc_rec.override_source_type:'||g_ae_doc_rec.override_source_type);
495   arp_standard.debug(' g_ae_doc_rec.gl_tax_acct:   '||g_ae_doc_rec.gl_tax_acct);
496   arp_standard.debug(' g_ae_doc_rec.inv_cm_app_mode:'||g_ae_doc_rec.inv_cm_app_mode);
497   arp_standard.debug('g_ae_doc_rec-');
498 
499   arp_standard.debug('g_ae_rule_rec+');
500   arp_standard.debug(' g_ae_rule_rec.gl_account_source1 :'||g_ae_rule_rec.gl_account_source1);
501   arp_standard.debug(' g_ae_rule_rec.tax_code_source1   :'||g_ae_rule_rec.tax_code_source1);
502   arp_standard.debug(' g_ae_rule_rec.tax_recoverable_flag1  :'||g_ae_rule_rec.tax_recoverable_flag1);
503   arp_standard.debug(' g_ae_rule_rec.code_combination_id1   :'||g_ae_rule_rec.code_combination_id1);
504   arp_standard.debug(' g_ae_rule_rec.asset_tax_code1        :'||g_ae_rule_rec.asset_tax_code1);
505   arp_standard.debug(' g_ae_rule_rec.liability_tax_code1    :'||g_ae_rule_rec.liability_tax_code1);
506   arp_standard.debug(' g_ae_rule_rec.act_tax_non_rec_ccid1  :'||g_ae_rule_rec.act_tax_non_rec_ccid1);
507   arp_standard.debug(' g_ae_rule_rec.act_vat_tax_id1        :'||g_ae_rule_rec.act_vat_tax_id1);
508   arp_standard.debug(' g_ae_rule_rec.gl_account_source2 :'||g_ae_rule_rec.gl_account_source2);
509   arp_standard.debug(' g_ae_rule_rec.tax_code_source2   :'||g_ae_rule_rec.tax_code_source2);
510   arp_standard.debug(' g_ae_rule_rec.tax_recoverable_flag2  :'||g_ae_rule_rec.tax_recoverable_flag2);
511   arp_standard.debug(' g_ae_rule_rec.code_combination_id2   :'||g_ae_rule_rec.code_combination_id2);
512   arp_standard.debug(' g_ae_rule_rec.asset_tax_code2        :'||g_ae_rule_rec.asset_tax_code2);
513   arp_standard.debug(' g_ae_rule_rec.liability_tax_code2    :'||g_ae_rule_rec.liability_tax_code2);
514   arp_standard.debug(' g_ae_rule_rec.act_tax_non_rec_ccid2  :'||g_ae_rule_rec.act_tax_non_rec_ccid2);
515   arp_standard.debug(' g_ae_rule_rec.act_vat_tax_id2        :'||g_ae_rule_rec.act_vat_tax_id2);
516   arp_standard.debug('g_ae_rule_rec-');
517 
518   arp_standard.debug('g_ae_event_rec+');
519   arp_standard.debug(' g_ae_event_rec.event_type     :'||g_ae_event_rec.event_type);
520   arp_standard.debug(' g_ae_event_rec.event_id       :'||g_ae_event_rec.event_id);
521   arp_standard.debug(' g_ae_event_rec.event_date     :'||g_ae_event_rec.event_date);
522   arp_standard.debug(' g_ae_event_rec.event_status   :'||g_ae_event_rec.event_status);
523   arp_standard.debug('g_ae_event_rec-');
524 
525 
526 
527 /*----------------------------------------------------------------------------+
528  | Get Invoice for which Tax accounting is required based on Rules            |
529  +----------------------------------------------------------------------------*/
530    IF g_ae_doc_rec.source_table = 'RA' THEN
531       l_invoice_id          := p_app_rec.applied_customer_trx_id;
532       l_payment_schedule_id := p_app_rec.applied_payment_schedule_id;
533 
534       --{CMAPP
535       IF p_app_rec.applied_customer_trx_id = p_app_rec.customer_trx_id THEN
536          l_cm_app := 'Y';
537       END IF;
538       --}
539    ELSE
540       l_invoice_id  := p_adj_rec.customer_trx_id;
541       l_payment_schedule_id := p_adj_rec.payment_schedule_id;
542    END IF;
543 
544 /* Initialize zx */
545    SELECT legal_entity_id
546    INTO   l_le_id
547    FROM   ra_customer_trx
548    WHERE  customer_trx_id = l_invoice_id;
549 
550    zx_api_pub.set_tax_security_context(
551            p_api_version      => 1.0,
552            p_init_msg_list    => 'T',
553            p_commit           => 'F',
554            p_validation_level => NULL,
555            x_return_status    => l_return_status,
556            x_msg_count        => l_msg_count,
557            x_msg_data         => l_msg_data,
558            p_internal_org_id  => arp_standard.sysparm.org_id,
559            p_legal_entity_id  => l_le_id,
560            p_transaction_date => nvl(p_app_rec.apply_date,
561                                      p_adj_rec.apply_date),
562            p_related_doc_date => NULL,
563            p_adjusted_doc_date=> NULL,
564            x_effective_date   => l_effective_date);
565 
566 /*----------------------------------------------------------------------------+
567  | Check whether processing is really required if not then exit               |
568  |                                                                            |
569  | No need to make changes to this procedure as all checking will be done     |
570  | based on primary data.  If revenue recognition has not been run on first   |
571  | iteration, it will be complete by end of first iteration and before        |
572  | second iteration.                                                          |
573  +----------------------------------------------------------------------------*/
574   IF nvl(g_cust_inv_rec.upgrade_method,'NONE') = 'R12_MERGE' AND
575     g_ae_doc_rec.source_table IN ('RA','ADJ') THEN
576     IF PG_DEBUG in ('Y', 'C') THEN
577       arp_standard.debug('Upgrade method is R12_MERGE,check whether further processing is needed');
578     END IF;
579 
580     Check_Entry(p_invoice_id     => l_invoice_id    ,
581 	       p_app_rec        => p_app_rec       ,
582 	       p_adj_rec        => p_adj_rec       ,
583 	       p_required       => l_required    );
584 
585     IF NOT l_required THEN            --processing not required
586       IF PG_DEBUG in ('Y', 'C') THEN
587 	arp_standard.debug('Check_Entry return value false,avoidng call to detailed accting engine.');
588       END IF;
589 
590       GOTO end_process_lbl;
591     END IF;
592   END IF;
593 
594   IF NVL(g_ae_sys_rec.sob_type,'P') = 'P' THEN
595 
596    --  ER :  LIne Level adjustment API- Changed the p_gt_id check , Placed the NVL
597 
598     IF  (p_from_llca_call = 'N'  AND NVL(p_gt_id,0) = 0 ) THEN
599 
600       l_gt_id  := g_id;
601 
602       IF (    (p_app_rec.receivable_application_id IS NOT NULL)
603           AND (p_adj_rec.adjustment_id             IS NOT NULL))
604       THEN
605          arp_standard.debug('Problem is this a application or adjustment allocation ?');
606          arp_standard.debug(' Both receivable_application_id :' ||p_app_rec.receivable_application_id);
607          arp_standard.debug(' and adjustment_id_id           :' ||p_adj_rec.adjustment_id);
608          arp_standard.debug(' are not null');
609          RAISE What_kind_of_activity;
610       ELSIF (p_app_rec.receivable_application_id IS NOT NULL) OR
611             (p_app_rec.receivable_application_id IS NULL       AND
612 			 p_app_rec.applied_customer_trx_id   IS NOT NULL   AND
613 			 p_app_rec.applied_payment_schedule_id IS NOT NULL AND
614 			 (NVL(p_app_rec.amount_applied,0)             <> 0 OR
615               NVL(p_app_rec.acctd_amount_applied_to,0)    <> 0 OR
616               NVL(p_app_rec.line_applied,0)               <> 0 OR
617               NVL(p_app_rec.tax_applied,0)                <> 0 OR
618               NVL(p_app_rec.freight_applied,0)            <> 0 OR
619               NVL(p_app_rec.receivables_charges_applied,0)<> 0 ))
620       THEN
621          IF p_inv_cm = 'C' THEN
622            g_cust_inv_rec.customer_trx_id := p_app_rec.customer_trx_id;
623          ELSE
624            g_cust_inv_rec.customer_trx_id := p_app_rec.applied_customer_trx_id;
625          END IF;
626 
627          ARP_DET_DIST_PKG.exec_revrec_if_required
628           ( p_customer_trx_id  => g_cust_inv_rec.customer_trx_id,
629             p_app_rec          => p_app_rec,
630             p_adj_rec          => p_adj_rec);
631 
632          ARP_DET_DIST_PKG.exec_adj_api_if_required
633            (p_adj_rec          => p_adj_rec,
634             p_app_rec          => p_app_rec,
635             p_ae_rule_rec      => g_ae_rule_rec,
636             p_cust_inv_rec     => g_Cust_inv_rec);
637 
638          ARP_DET_DIST_PKG.set_original_rem_amt
639            (p_customer_trx  => g_cust_inv_rec,
640 		    p_adj_id        => p_adj_rec.adjustment_id,
641 			p_app_id        => p_app_rec.receivable_application_id);
642 
643          ARP_DET_DIST_PKG.Trx_level_direct_cash_apply
644            (p_customer_trx     => g_cust_inv_rec,
645             p_app_rec          => p_app_rec,
646             p_ae_sys_rec       => g_ae_sys_rec,
647             p_gt_id            => l_gt_id,
648             p_inv_cm           => p_inv_cm);
649 
650       ELSIF (p_adj_rec.adjustment_id IS NOT NULL)
651       THEN
652          g_Cust_inv_rec.customer_trx_id := p_adj_rec.customer_trx_id;
653          g_receivables_trx_id           := p_adj_rec.receivables_trx_id;
654 
655          ARP_DET_DIST_PKG.exec_revrec_if_required
656           ( p_customer_trx_id  => p_adj_rec.customer_trx_id,
657             p_app_rec          => p_app_rec,
658             p_adj_rec          => p_adj_rec);
659 
660          ARP_DET_DIST_PKG.exec_adj_api_if_required
661            (p_adj_rec          => p_adj_rec,
662             p_app_rec          => p_app_rec,
663             p_ae_rule_rec      => g_ae_rule_rec,
664             p_cust_inv_rec     => g_Cust_inv_rec);
665 
666          ARP_DET_DIST_PKG.set_original_rem_amt
667            (p_customer_trx  => g_cust_inv_rec,
668 		    p_adj_id        => p_adj_rec.adjustment_id,
669 			p_app_id        => p_app_rec.receivable_application_id);
670 
671 
672          ARP_DET_DIST_PKG.possible_adjust
673            (p_adj_rec           => p_adj_rec,
674             p_ae_rule_rec       => g_ae_rule_rec,
675             p_customer_trx_id   => g_cust_inv_rec.customer_trx_id,
676             x_return_status     => l_return_status,
677             x_line_adj          => l_line_adj,
678             x_tax_adj           => l_tax_adj,
679             x_frt_adj           => l_frt_adj,
680             x_chrg_adj          => l_chrg_adj,
681             p_app_rec           => p_app_rec);
682 
683          arp_standard.debug('  x_line_adj :'||l_line_adj);
684          arp_standard.debug('  x_tax_adj  :'||l_tax_adj);
685          arp_standard.debug('  x_frt_adj  :'||l_frt_adj);
686          arp_standard.debug('  x_chrg_adj :'||l_chrg_adj);
687          arp_standard.debug('  l_return_status :'||l_return_status);
688 
689          IF   l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
690             RAISE impossible_adjust;
691          END IF;
692 
693          ARP_DET_DIST_PKG.Trx_level_direct_adjust
694            (p_customer_trx     => g_cust_inv_rec,
695             p_adj_rec          => p_adj_rec,
696             p_ae_sys_rec       => g_ae_sys_rec,
697             p_gt_id            => l_gt_id);
698 
699       ELSIF (    (p_adj_rec.adjustment_id IS NULL)
700              AND (p_app_rec.receivable_application_id IS NULL)
701 	     AND (NVL(g_simul_app,'N') = 'N')) -- added to avoid check for simulation [bug 8464438]
702       THEN
703          arp_standard.debug('Problem is this a application or adjustment allocation ?');
704          arp_standard.debug(' Both receivable_application_id  and adjustment_id_id  are null');
705          RAISE What_kind_of_activity;
706       END IF;
707       g_prim_det_dist_done := 'Y';
708 
709     ELSE
710 
711       l_gt_id := p_gt_id;
712       g_prim_det_dist_done := 'Y';
713 
714     END IF;
715 
716   END IF;
717 --}
718 
719 /*----------------------------------------------------------------------------+
720  | Get tax rounding rules and currency details                                |
721  +----------------------------------------------------------------------------*/
722    arp_standard.debug(' HYU calling Get_Tax_Curr');
723    --{CMAPP
724    IF l_cm_app <> 'Y' THEN
725      Get_Tax_Curr(p_invoice_id          => l_invoice_id,
726                   p_payment_schedule_id => l_payment_schedule_id);
727    END IF;
728 
729 /*----------------------------------------------------------------------------+
730  | Set the flags for processinf discounts, adjustments and payments           |
731  +----------------------------------------------------------------------------*/
732    IF (((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.earned_discount_taken,0) <> 0))
733        OR ((g_ae_doc_rec.source_table = 'ADJ') AND (nvl(p_adj_rec.amount,0) <> 0))) THEN
734        l_process_ed_adj := 'Y';
735    END IF;
736 
737    IF ((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.unearned_discount_taken,0) <> 0)) THEN
738       l_process_uned := 'Y';
739    END IF;
740 
741    IF (   --def_tax? (g_ae_def_tax) AND (NOT g_done_def_tax) AND
742            (g_ae_doc_rec.source_table = 'RA')
743        AND (nvl(p_app_rec.amount_applied,0) <> 0)) THEN
744        l_process_pay := 'Y';
745    END IF;
746 
747    arp_standard.debug(' l_process_ed_adj:'||l_process_ed_adj);
748    arp_standard.debug(' l_process_uned:'||l_process_uned);
749    arp_standard.debug(' l_process_pay:'||l_process_pay);
750 
751 /*----------------------------------------------------------------------------+
752  | Get Invoice Revenue and Tax distributions for Non Rule Invoice             |
753  +----------------------------------------------------------------------------*/
754    arp_standard.debug(' Calling Get_Invoice_Distributions');
755 
756 
757    Get_Invoice_Distributions(p_invoice_id     => l_invoice_id     ,
758                              p_app_rec        => p_app_rec        ,
759                              p_adj_rec        => p_adj_rec        ,
760                              p_process_ed_adj => l_process_ed_adj ,
761                              p_process_uned   => l_process_uned   ,
762                              p_process_pay    => l_process_pay      );
763    --GOTO end_process_lbl;
764 
765 /*----------------------------------------------------------------------------+
766  | Set tax link ids for Invoices Tax distributions (lines), call this routine |
767  | only if tax really needs to be linked otherwise the link id is null. Now it|
768  | is important to call the Get_Tax_Link_Id routine based on earned or unearn |
769  | discount because ther rules can be different and if one has the rule       |
770  | ACTIVITY then the link basis becomes different as it is off the activity   |
771  | tax code. So this condition. Process for earned discounts, adjustments     |
772  +----------------------------------------------------------------------------*/
773 --    IF l_process_ed_adj = 'Y' THEN
774 --     g_exec := NULL;
775 --     g_exec := source_exec(p_type_acct    => 'ED_ADJ',
776  --                          p_source_table =>  g_ae_doc_rec.source_table);
777 --}
778      /*----------------------------------------------------------------------------+
779       | Process for Earned discounts                                               |
780       +----------------------------------------------------------------------------*/
781         Process_Amounts(p_app_rec   => p_app_rec   ,
782                         p_adj_rec   => p_adj_rec    );
783 
784 --     END IF;
785 
786   /*----------------------------------------------------------------------------+
787    | Set tax link ids for Unearned discounts and link, override revenue using   |
788    | rules on unearned discount activity.                                       |
789    +----------------------------------------------------------------------------*/
790 --     IF l_process_uned = 'Y' THEN
791 --     g_exec := NULL;
792 --     g_exec := source_exec(p_type_acct    => 'UNED',
793 --                           p_source_table =>  g_ae_doc_rec.source_table);
794 --}
795      /*------------------------------------------------------------------------------+
796       | Initialise Revenue table to reuse cells for allocating line amounts          |
797       +------------------------------------------------------------------------------*/
798 -- we should initializing the amount in ar_ae_alloc_rec_gt        Init_Rev_Tax_Tab;
799 
800      /*----------------------------------------------------------------------------+
801       | Process for Unearned discounts                                             |
802       +----------------------------------------------------------------------------*/
803 --        Process_Amounts(p_app_rec   => p_app_rec,
804 --                        p_adj_rec   => p_adj_rec  );
805 
806 --     END IF; --end if unearned discounts
807 
808   /*----------------------------------------------------------------------------+
809    | Set tax link ids for Payment, in this case we default the rule to revenue  |
810    | on invoice, tax code on invoice recoverable and process for deferred tax   |
811    +----------------------------------------------------------------------------*/
812 --    IF l_process_pay = 'Y' THEN
813    /*----------------------------------------------------------------------------+
814     | Since processing for discounts is complete, force set the rules below so   |
815     | that we may process for payments and use a link basis between tax and      |
816     | revenue and calculate deferred taxable using payment non tax amount        |
817     +----------------------------------------------------------------------------*/
818 --      g_ae_rule_rec.gl_account_source1    := 'ACTIVITY_GL_ACCOUNT';
819 --      g_ae_rule_rec.tax_code_source1      := 'INVOICE';
820 --      g_ae_rule_rec.tax_recoverable_flag1 := 'Y';
821 --      g_ae_rule_rec.code_combination_id1  :=  g_ae_rule_rec.receivable_account;
822 --}
823      /*------------------------------------------------------------------------------+
824       | Initialise Revenue table to reuse cells for allocating line amounts          |
825       +------------------------------------------------------------------------------*/
826 -- we should initializing the amount in ar_ae_alloc_rec_gt        Init_Rev_Tax_Tab;
827 --     g_exec := NULL;
828 --     g_exec := source_exec(p_type_acct    => 'PAY',
829 --                           p_source_table =>  g_ae_doc_rec.source_table);
830 --}
831      /*------------------------------------------------------------------------+
832       | Process for  deferred tax for Payments                                 |
833       +------------------------------------------------------------------------*/
834 --        Process_Amounts(p_type_acct => 'PAY'       ,
835 --                        p_app_rec   => p_app_rec   ,
836 --                        p_adj_rec   => p_adj_rec    );
837 --     END IF; --end if payments
838 
839    IF PG_DEBUG in ('Y', 'C') THEN
840       arp_standard.debug( 'upgrade method '||g_cust_inv_rec.upgrade_method);
841    END IF;
842 
843  /*----------------------------------------------------------------------------+
844   | Summarize built accounting lines for revenue and tax to net out accounting |
845   +----------------------------------------------------------------------------*/
846    IF nvl(g_cust_inv_rec.upgrade_method,'NONE') = 'R12_MERGE' AND
847       g_ae_doc_rec.source_table IN ('RA','ADJ') THEN
848      Summarize_Acct_Lines_Hdr_Level;
849    ELSE
850      Summarize_Accounting_Lines;
851 
852      IF g_ae_doc_rec.inv_cm_app_mode = 'C' THEN
853       arp_standard.debug('Updating CM ard to stamp ref_prev_cust_trx_line_id');
854 
855       update ar_distributions ard
856       set ref_prev_cust_trx_line_id = (select previous_customer_trx_line_id
857                                        from ra_customer_trx_lines
858                                        where customer_trx_line_id = ard.ref_customer_trx_line_id)
859       where source_id = g_ae_doc_rec.source_id
860       and source_table = 'RA'
861       and ref_customer_trx_line_id in (select customer_trx_line_id
862                                        from ra_customer_trx_lines ctl_cm,
863                                             ar_receivable_applications ra
864                                        where ra.receivable_application_id = g_ae_doc_rec.source_id
865                                        and ra.customer_trx_id = ctl_cm.customer_trx_id
866                                        and ctl_cm.previous_customer_trx_line_id is not null);
867 
868       l_cnt := sql%rowcount;
869 
870       arp_standard.debug('CM ard rows updated : '||l_cnt);
871 
872       IF l_cnt > 0 THEN
873        arp_standard.debug('Updating INV ard to stamp ref_prev_cust_trx_line_id');
874 
875        update ar_distributions ard
876        set ref_prev_cust_trx_line_id = (select ref_customer_trx_line_id
877                                         from ar_distributions
878                                         where source_id = g_ae_doc_rec.source_id
879                                         and ref_prev_cust_trx_line_id = ard.ref_customer_trx_line_id
880                                         and rownum = 1)
881        where source_id = g_ae_doc_rec.source_id
882        and source_table = 'RA'
883        and ref_customer_trx_line_id in (select customer_trx_line_id
884                                         from ra_customer_trx_lines ctl_inv,
885                                              ar_receivable_applications ra
886                                         where ra.receivable_application_id = g_ae_doc_rec.source_id
887                                         and ra.applied_customer_trx_id = ctl_inv.customer_trx_id);
888        l_cnt := sql%rowcount;
889 
890        arp_standard.debug('INV ard rows updated : '||l_cnt);
891       END IF;
892      END IF;
893    END IF;
894 
895  /*---------------------------------------------------------------------------------+
896   | Assign summarized lines to the the in out table to pass back to calling rountine|
897   +---------------------------------------------------------------------------------*/
898 
899    IF nvl(g_simul_app,'N') = 'Y' THEN
900 
901       IF g_ae_summarize_tbl.EXISTS(g_ae_summ_ctr) THEN
902 
903          p_ae_line_tbl := g_ae_summarize_tbl;
904          p_ae_ctr      := g_ae_summ_ctr;
905 
906       END IF;
907 
908    END IF;
909 
910 
911  /*---------------------------------------------------------------------------------+
912   | Clean up the Global temporary tables.                                           |
913   +---------------------------------------------------------------------------------*/
914     --not required since in cases like mass applications g_id will increment
915 
916    g_br_cust_trx_line_id := NULL;
917    g_simul_app  := NULL;
918    g_adj_act_gl_acct_ccid := -9999;
919 
920    /* * Bug 8689308 : Peroformance issue when multiple applications are made   *
921       * for a single receipt due to piling up of data in GT tables. Hence      *
922       * periodically delete the data from GT tables for every 1000 applications*
923       * for a receipt.                                                         * */
924    IF arp_det_dist_pkg.g_appln_count >= PG_DEL_FRM_GT_CNT THEN
925         IF PG_DEBUG in ('Y', 'C') THEN
926          arp_standard.debug('Exceeded upper limit for maximum number of rows in ra_ar_gt');
927          arp_standard.debug('PG_DEL_FRM_GT_CNT : ' || PG_DEL_FRM_GT_CNT);
928          arp_standard.debug('Clear all gt tables');
929         END IF;
930 
931       arp_det_dist_pkg.g_appln_count := 0;
932 
933       DELETE FROM ra_customer_trx_lines_gt;
934         IF PG_DEBUG in ('Y', 'C') THEN
935          l_cnt := sql%rowcount;
936          arp_standard.debug('No of rows deleted from ra_customer_trx_lines_gt : '||l_cnt);
937         END IF;
938 
939       DELETE FROM ra_ar_gt;
940         IF PG_DEBUG in ('Y', 'C') THEN
941          l_cnt := SQL%ROWCOUNT;
942          arp_standard.debug('No of rows deleted from ra_ar_gt : '||l_cnt);
943         END IF;
944 
945       DELETE FROM ra_ar_amounts_gt;
946         IF PG_DEBUG in ('Y', 'C') THEN
947          l_cnt := SQL%ROWCOUNT;
948          arp_standard.debug('No of rows deleted from ra_ar_amounts_gt : '||l_cnt);
949         END IF;
950 
951       DELETE FROM ar_base_dist_amts_gt;
952         IF PG_DEBUG in ('Y', 'C') THEN
953          l_cnt := SQL%ROWCOUNT;
954          arp_standard.debug('No of rows deleted from ar_base_dist_amts_gt : '||l_cnt);
955         END IF;
956 
957       DELETE FROM ar_line_app_detail_gt;
958         IF PG_DEBUG in ('Y', 'C') THEN
959          l_cnt := SQL%ROWCOUNT;
960          arp_standard.debug('No of rows deleted from ar_line_app_detail_gt : '||l_cnt);
961         END IF;
962 
963       DELETE FROM ar_ae_alloc_rec_gt;
964         IF PG_DEBUG in ('Y', 'C') THEN
965          l_cnt := SQL%ROWCOUNT;
966          arp_standard.debug('No of rows deleted from ar_ae_alloc_rec_gt : '||l_cnt);
967         END IF;
968    END IF;
969 
970 <<end_process_lbl>>
971   IF PG_DEBUG in ('Y', 'C') THEN
972      arp_standard.debug(   'ARP_ALLOCATION_PKG.Allocate_Tax()-');
973   END IF;
974 
975 EXCEPTION
976   WHEN NO_DATA_FOUND THEN
977      IF PG_DEBUG in ('Y', 'C') THEN
978         arp_standard.debug(  'ARP_ALLOCATION_PKG.Allocate_Tax - NO_DATA_FOUND' );
979      END IF;
980      RAISE;
981 
982   WHEN What_kind_of_activity THEN
983      IF PG_DEBUG in ('Y', 'C') THEN
984         arp_standard.debug(  'EXCEPTION: ARP_ALLOCATION_PKG.Allocate_Tax - What_kind_of_activity');
985      END IF;
986      RAISE;
987 
988   WHEN impossible_adjust THEN
989      IF PG_DEBUG in ('Y', 'C') THEN
990         arp_standard.debug(  'EXCEPTION: ARP_ALLOCATION_PKG.Allocate_Tax - impossible_adjust');
991      END IF;
992      RAISE;
993 
994   WHEN OTHERS THEN
995      IF PG_DEBUG in ('Y', 'C') THEN
996         arp_standard.debug(  'EXCEPTION: ARP_ALLOCATION_PKG.Allocate_Tax:'||SQLERRM);
997      END IF;
998      RAISE;
999 
1000 END Allocate_Tax;
1001 
1002 /* =======================================================================
1003  | PROCEDURE Check_Entry
1004  |
1005  | DESCRIPTION
1006  |      This routine checks whether Tax accounting processing is really
1007  |      required and whether revenue recognition needs to be run.
1008  |
1009  | SCOPE - PUBLIC
1010  |
1011  | PARAMETERS
1012  |      p_invoice_id            IN      Invoice identifier
1013  |      p_app_rec               IN      Applications record
1014  |      p_adj_rec               IN      Adjustment record
1015  |      p_required              OUT     Flag indicates whether tax processing
1016  |                                      is required
1017  * ======================================================================*/
1018 PROCEDURE Check_Entry(p_invoice_id  IN  ra_customer_trx.customer_trx_id%TYPE,
1019                       p_app_rec     IN  ar_receivable_applications%ROWTYPE,
1020                       p_adj_rec     IN  ar_adjustments%ROWTYPE,
1021                       p_required    OUT NOCOPY BOOLEAN) IS
1022 
1023 l_dummy       NUMBER;
1024 l_ae_def_tax  BOOLEAN := FALSE;
1025 
1026 BEGIN
1027 
1028   IF PG_DEBUG in ('Y', 'C') THEN
1029    arp_standard.debug(   'ARP_ALLOCATION_PKG.Check_Entry()+');
1030   END IF;
1031 
1032   --Is tax deferred
1033   BEGIN
1034 
1035     IF PG_DEBUG in ('Y', 'C') THEN
1036       arp_standard.debug(  'ARP_ALLOCATION_PKG.Check_Entry - Checking for deferred tax');
1037     END IF;
1038 
1039     select gld.customer_trx_id
1040     into l_dummy
1041     from  ra_cust_trx_line_gl_dist gld
1042     where gld.account_class = 'TAX'
1043     and   gld.customer_trx_id = p_invoice_id
1044     and   gld.collected_tax_ccid IS NOT NULL
1045     group by gld.customer_trx_id;
1046 
1047     l_ae_def_tax := TRUE; --Atleast one deferred tax line exists
1048 
1049     IF PG_DEBUG in ('Y', 'C') THEN
1050       arp_standard.debug(  'ARP_ALLOCATION_PKG.Check_Entry - DEFERRED TAX');
1051     END IF;
1052 
1053   EXCEPTION
1054     WHEN NO_DATA_FOUND THEN
1055       IF PG_DEBUG in ('Y', 'C') THEN
1056       arp_standard.debug(  'ARP_ALLOCATION_PKG.Check_Entry - NO DEFERRED TAX');
1057       END IF;
1058       l_ae_def_tax := FALSE; --Tax is not deferred
1059   END;
1060 
1061   IF PG_DEBUG in ('Y', 'C') THEN
1062     arp_standard.debug(' g_ae_doc_rec.source_table:'||g_ae_doc_rec.source_table);
1063     arp_standard.debug(' p_app_rec.earned_discount_taken:'||p_app_rec.earned_discount_taken);
1064     arp_standard.debug(' p_app_rec.unearned_discount_taken:'||p_app_rec.unearned_discount_taken);
1065     arp_standard.debug(' p_app_rec.amount_applied:'||p_app_rec.amount_applied);
1066   END IF;
1067 
1068   --Set processing required flag
1069   IF ((g_ae_doc_rec.source_table = 'RA')
1070     AND (((nvl(p_app_rec.earned_discount_taken,0) <> 0)
1071 	   OR (nvl(p_app_rec.unearned_discount_taken,0) <> 0))
1072 	   OR ((l_ae_def_tax) AND (nvl(p_app_rec.amount_applied,0) <> 0))))
1073     OR ((g_ae_doc_rec.source_table = 'ADJ') AND (nvl(p_adj_rec.amount,0) <> 0))
1074   THEN
1075     p_required := TRUE;
1076   ELSE
1077     p_required := FALSE;
1078   END IF;
1079 
1080   IF PG_DEBUG in ('Y', 'C') THEN
1081     arp_standard.debug(   'ARP_ALLOCATION_PKG.Check_Entry()-');
1082   END IF;
1083 
1084   EXCEPTION
1085     WHEN OTHERS THEN
1086        IF PG_DEBUG in ('Y', 'C') THEN
1087 	  arp_standard.debug(  'EXCEPTION: ARP_ALLOCATION_PKG.Check_Entry');
1088        END IF;
1089        RAISE;
1090 END Check_Entry;
1091 
1092 /* =======================================================================
1093  | PROCEDURE Get_Tax_Curr
1094  |
1095  | DESCRIPTION
1096  |      This routine gets the Invoice currency details and Tax rounding
1097  |      rules for the same.
1098  |
1099  | SCOPE - PUBLIC
1100  |
1101  | PARAMETERS
1102  |      p_invoice_id            IN      Invoice identifier
1103  * ======================================================================*/
1104 PROCEDURE Get_Tax_Curr(p_invoice_id          IN ra_customer_trx.customer_trx_id%TYPE          ,
1105                        p_payment_schedule_id IN ar_payment_schedules.payment_schedule_id%TYPE   ) IS
1106 
1107 BEGIN
1108   IF PG_DEBUG in ('Y', 'C') THEN
1109      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Tax_Curr()+');
1110      arp_standard.debug( '  p_invoice_id                           = ' || p_invoice_id);
1111      arp_standard.debug( '  p_payment_schedule_id                  = ' || p_payment_schedule_id);
1112   END IF;
1113 
1114 /*----------------------------------------------------------------------------+
1115  | Get Invoice tax rounding rules and currency information                    |
1116  |                                                                            |
1117  | Modified select to get the acctd amount due remaining from the correct     |
1118  | sob (MRC TRIGGER REPLACEMENT)                                              |
1119  +----------------------------------------------------------------------------*/
1120  IF (NVL(g_ae_sys_rec.sob_type,'P') = 'P') THEN
1121       select    fc.precision                        ,
1122                 fc.minimum_accountable_unit         ,
1123                 pay.amount_due_remaining            ,
1124                 pay.acctd_amount_due_remaining      ,
1125                 pay.amount_due_original
1126       into      g_ae_curr_rec.precision                ,
1127                 g_ae_curr_rec.minimum_accountable_unit ,
1128                 g_amount_due_remaining                 ,
1129                 g_acctd_amount_due_remaining           ,
1130                 g_amount_due_original
1131       from ra_customer_trx      ct      ,
1132            ar_payment_schedules pay     ,
1133            fnd_currencies       fc
1134       where ct.customer_trx_id = p_invoice_id
1135       and   pay.customer_trx_id = ct.customer_trx_id
1136       and   pay.payment_schedule_id = p_payment_schedule_id
1137       and   ct.invoice_currency_code = fc.currency_code;
1138   END IF;
1139   IF PG_DEBUG in ('Y', 'C') THEN
1140      arp_standard.debug( ' g_ae_curr_rec.precision                = ' || g_ae_curr_rec.precision);
1141      arp_standard.debug( ' g_ae_curr_rec.minimum_accountable_unit = ' || g_ae_curr_rec.minimum_accountable_unit);
1142      arp_standard.debug( ' g_amount_due_remaining                 = ' || g_amount_due_remaining);
1143      arp_standard.debug( ' g_acctd_amount_due_remaining           = ' || g_acctd_amount_due_remaining);
1144      arp_standard.debug( ' g_amount_due_original                  = ' || g_amount_due_original);
1145      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Tax_Curr()-');
1146   END IF;
1147 
1148 EXCEPTION
1149   WHEN NO_DATA_FOUND THEN
1150      IF PG_DEBUG in ('Y', 'C') THEN
1151         arp_standard.debug('ARP_ALLOCATION_PKG.Get_Tax_Curr - NO_DATA_FOUND' );
1152      END IF;
1153      RAISE;
1154 
1155   WHEN OTHERS THEN
1156      IF PG_DEBUG in ('Y', 'C') THEN
1157         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Get_Tax_Curr');
1158      END IF;
1159      RAISE;
1160 
1161 END Get_Tax_Curr;
1162 
1163 /* =======================================================================
1164  | PROCEDURE Get_Invoice_Distributions
1165  |
1166  | DESCRIPTION
1167  |      Retrieves Revenue and Tax amounts for Invoice and Tax lines,
1168  |      including the non recoverable tax accounts off the tax code or
1169  |      location segment. This routine gets all the base data from the
1170  |      Invoice document so that Tax accounting may be done based on Rules
1171  |      for a activity for adjustments, finance charges or discounts.
1172  |
1173  | SCOPE - PRIVATE
1174  |
1175  | PARAMETERS
1176  |      p_invoice_id            IN      Invoice identifier
1177  |                                      or adjustments
1178  |
1179  | NOTES : THE ORDERING OF THE CURSOR inv_dist_non_rule IS VERY IMPORTANT
1180  * ======================================================================*/
1181 PROCEDURE Get_Invoice_Distributions(
1182                         p_invoice_id           IN      NUMBER,
1183                         p_app_rec              IN      ar_receivable_applications%ROWTYPE,
1184                         p_adj_rec              IN      ar_adjustments%ROWTYPE,
1185                         p_process_ed_adj       IN      VARCHAR2,
1186                         p_process_uned         IN      VARCHAR2,
1187                         p_process_pay          IN      VARCHAR2 ) IS
1188 
1189 CURSOR get_group_data_tax IS
1190 SELECT /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
1191        decode(ae_collected_tax_ccid,
1192               '',ae_account_class,
1193               'DEFTAX')                       ae_account_class      ,
1194        SUM(ae_amount)                         sum_ae_amount         ,
1195        SUM(ae_acctd_amount)                   sum_ae_acctd_amount   ,
1196        max(ae_code_combination_id)            ae_code_combination_id,
1197        max(decode(ae_override_ccid1,'',2,1))  ae_override_ccid1     ,
1198        max(decode(ae_override_ccid2,'',2,1))  ae_override_ccid2     ,
1199        count(ae_account_class)                ae_count
1200 FROM ar_ae_alloc_rec_gt
1201 WHERE ae_id = g_id
1202 GROUP BY decode(ae_collected_tax_ccid,
1203                 '',ae_account_class,
1204                 'DEFTAX');
1205 
1206 CURSOR get_group_data_rev IS
1207 SELECT /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
1208        ae_account_class                       ae_account_class      ,
1209        SUM(ae_amount)                         sum_ae_amount         ,
1210        SUM(ae_acctd_amount)                   sum_ae_acctd_amount   ,
1211        max(ae_code_combination_id)            ae_code_combination_id,
1212        ''                                     ae_override_ccid1     ,
1213        ''                                     ae_override_ccid2     ,
1214        count(ae_account_class)                ae_count
1215 FROM ar_ae_alloc_rec_gt
1216 WHERE ae_id = g_id
1217 AND   ae_account_class <> 'TAX'
1218 GROUP BY ae_account_class;
1219 
1220 CURSOR l_rev_unearn(p_type IN VARCHAR2, p_trx_line_id IN NUMBER) IS
1221 SELECT /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N1) */ *
1222 FROM  ar_ae_alloc_rec_gt
1223 WHERE ae_id = g_id
1224 AND   ae_account_class = p_type
1225 AND   ae_customer_trx_line_id = p_trx_line_id
1226 AND   ae_customer_trx_id = p_invoice_id;
1227 
1228 
1229 l_rev_adj_rec AR_Revenue_Adjustment_PVT.Rev_Adj_Rec_Type;
1230 l_inv_assign  ar_ae_alloc_rec_gt%ROWTYPE;
1231 
1232 l_rev_ctr    BINARY_INTEGER    ;
1233 l_ctr        BINARY_INTEGER    ;
1234 l_ctr1       BINARY_INTEGER    ;
1235 l_ctr2       BINARY_INTEGER    ;
1236 l_ctr3       BINARY_INTEGER    ;
1237 
1238 l_adj_id     NUMBER            ;
1239 l_dist_count NUMBER            ;
1240 l_ae_tax_id  NUMBER            ;
1241 l_adj_number ar_adjustments.adjustment_number%TYPE;
1242 
1243 l_ra_dist_tbl AR_Revenue_Adjustment_PVT.RA_Dist_Tbl_Type;
1244 
1245 l_return_status VARCHAR2(1)   ;
1246 l_msg_count     NUMBER        ;
1247 l_msg_data      VARCHAR2(2000);
1248 l_mesg          VARCHAR2(2000) := '';
1249 
1250 g_ae_alloc_rev_tbl ar_ae_alloc_rec_gt%ROWTYPE;
1251 g_ae_alloc_tax_tbl ar_ae_alloc_rec_gt%ROWTYPE;
1252 l_override1 VARCHAR2(1) := 'N';
1253 l_override2 VARCHAR2(1) := 'N';
1254 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
1255 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
1256 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
1257 
1258 CURSOR crevccid IS
1259 SELECT decode(
1260        max(decode(b.account_class,'REV',b.code_combination_id,0)), -- REV row gets priority
1261        0,max(b.code_combination_id), -- If no REV row, pick max of ccid as usual
1262        max(decode(b.account_class,'REV',b.code_combination_id,0))
1263        ),
1264        ctl.ae_cust_trx_line_gl_dist_id
1265   FROM ra_cust_trx_line_gl_dist b,
1266        ar_ae_alloc_rec_gt       ctl
1267  WHERE ctl.ae_tax_link_id     = b.customer_trx_line_id
1268    AND ctl.ae_account_class   = 'TAX'
1269   GROUP BY ctl.ae_cust_trx_line_gl_dist_id;
1270 
1271 l_ccid_tab     DBMS_SQL.NUMBER_TABLE;
1272 l_ctlgd_tab    DBMS_SQL.NUMBER_TABLE;
1273 
1274 
1275 BEGIN
1276 
1277   IF PG_DEBUG in ('Y', 'C') THEN
1278      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Invoice_Distributions()+');
1279   END IF;
1280 /*------------------------------------------------------------------------------+
1281  | Initialise revenue, tax amounts and accounted amounts                        |
1282  +------------------------------------------------------------------------------*/
1283    g_ae_rule_rec.revenue_amt          := 0;
1284    g_ae_rule_rec.revenue_acctd_amt    := 0;
1285    g_ae_rule_rec.tax_amt              := 0;
1286    g_ae_rule_rec.tax_acctd_amt        := 0;
1287    g_ae_rule_rec.def_tax_amt          := 0;
1288    g_ae_rule_rec.def_tax_acctd_amt    := 0;
1289 
1290  --------------------------------------------------------------------------------
1291  --Override account flags
1292  --------------------------------------------------------------------------------
1293   Override_Accounts(p_app_rec   =>  p_app_rec  ,
1294                     p_adj_rec   =>  p_adj_rec  ,
1295                     p_override1 =>  l_override1,
1296                     p_override2 =>  l_override2 );
1297 
1298 /*----------------------------------------------------------------------------+
1299  | Insert Tax distributions                                                   |
1300  | MRC Trigger Replacement.   Modified to insert currency sensitive columns   |
1301  +----------------------------------------------------------------------------*/
1302    insert into ar_ae_alloc_rec_gt (
1303      ae_id                       ,
1304      ae_account_class            ,
1305      ae_customer_trx_id          ,
1306      ae_customer_trx_line_id     ,
1307      ae_cust_trx_line_gl_dist_id ,
1308      ae_link_to_cust_trx_line_id ,
1309      ae_tax_type                 ,
1310      ae_code_combination_id      ,
1311      ae_collected_tax_ccid       ,
1312      ae_line_amount              ,
1313      ae_amount                   ,
1314      ae_acctd_amount             ,
1315      ae_tax_group_code_id        ,
1316      ae_tax_id                   ,
1317      ae_taxable_amount           ,
1318      ae_taxable_acctd_amount     ,
1319      ae_adj_ccid                 ,
1320      ae_edisc_ccid               ,
1321      ae_unedisc_ccid             ,
1322      ae_finchrg_ccid             ,
1323      ae_adj_non_rec_tax_ccid     ,
1324      ae_edisc_non_rec_tax_ccid   ,
1325      ae_unedisc_non_rec_tax_ccid ,
1326      ae_finchrg_non_rec_tax_ccid ,
1327      ae_override_ccid1           ,
1328      ae_override_ccid2           ,
1329      ae_tax_link_id              , -- link_to_cust_trx_line_id
1330      ae_tax_link_id_ed_adj       , -- link_to_cust_trx_line_id
1331      ae_tax_link_id_uned         , -- link_to_cust_trx_line_id
1332      ae_tax_link_id_act          , -- left null populate later
1333      ae_pro_amt                  ,
1334      ae_pro_acctd_amt            ,
1335      ae_pro_frt_chrg_amt         ,
1336      ae_pro_frt_chrg_acctd_amt   ,
1337      ae_pro_taxable_amt          ,
1338      ae_pro_taxable_acctd_amt    ,
1339      ae_pro_split_taxable_amt       ,
1340      ae_pro_split_taxable_acctd_amt ,
1341      ae_pro_recov_taxable_amt       ,
1342      ae_pro_recov_taxable_acctd_amt ,
1343      ae_pro_def_tax_amt          ,
1344      ae_pro_def_tax_acctd_amt    ,
1345      ae_summarize_flag           ,
1346      ae_counted_flag             ,
1347      ae_autotax                  ,
1348      ae_sum_alloc_amt            ,
1349      ae_sum_alloc_acctd_amt      ,
1350      ae_tax_line_count           ,
1351      ref_account_class                   ,
1352      activity_bucket                      ,
1353      ae_ref_line_id,
1354      ae_from_pro_amt,
1355      ae_from_pro_acctd_amt,
1356      ref_dist_ccid,
1357      ref_mf_dist_flag
1358      )
1359        SELECT
1360           g_id                                      ae_id,
1361           gld.account_class                         ae_account_class,
1362           ctl.customer_trx_id                       ae_customer_trx_id,
1363           ctl.customer_trx_line_id                  ae_customer_trx_line_id,
1364           gld.cust_trx_line_gl_dist_id              ae_cust_trx_line_gl_dist_id ,
1365           nvl(ctl.link_to_cust_trx_line_id,-9999)   ae_link_to_cust_trx_line_id,
1366           decode(ctl.location_segment_id,
1367                      '','VAT',
1368                      'LOC')                         ae_tax_type,
1369           gld.code_combination_id                   ae_code_combination_id,
1370           gld.collected_tax_ccid                    ae_collected_tax_ccid,
1371           ctl.extended_amount                  ae_line_amount,
1372           nvl(gld.amount,0)                    ae_amount,
1373           NVL(gld.acctd_amount,0)                     ae_acctd_amount,
1374           decode(ctl.location_segment_id,
1375                     '',
1376                     decode(nvl(ctl.autotax,'Y'),
1377                           'N', '',
1378                            decode(nvl(line.location_segment_id,line.vat_tax_id),
1379                                 '','',
1380                                 nvl(ctl.location_segment_id, ctl.vat_tax_id),'',
1381                                 nvl(line.location_segment_id,line.vat_tax_id))),
1382                          '')                       ae_tax_group_code_id,
1383           nvl(ctl.location_segment_id,ctl.vat_tax_id) ae_tax_id,
1384           ctl.taxable_amount                          ae_taxable_amount,
1385           arpcurr.functional_amount(nvl(ctl.taxable_amount,0)       ,
1386                        g_ae_sys_rec.base_currency   ,
1387                        g_cust_inv_rec.exchange_rate,
1388                        g_ae_sys_rec.base_precision  ,
1389                        g_ae_sys_rec.base_min_acc_unit) ae_taxable_acctd_amount,
1390           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1391                                         gld.gl_date,
1392                                         'ADJ')         ae_adj_ccid,
1393           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1394                                         gld.gl_date,
1395                                         'EDISC')       ae_edisc_ccid,
1396           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1397                                         gld.gl_date,
1398                                         'UNEDISC')     ae_unedisc_ccid,
1399           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1400                                         gld.gl_date,
1401                                         'FINCHRG')     ae_finchrg_ccid,
1402           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1403                                         gld.gl_date,
1404                                         'ADJ_NON_REC') ae_adj_non_rec_tax_ccid,
1405           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1406                                         gld.gl_date,
1407                                         'EDISC_NON_REC') ae_edisc_non_rec_tax_ccid,
1408           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1409                                         gld.gl_date,
1410                                         'UNEDISC_NON_REC') ae_unedisc_non_rec_tax_ccid,
1411           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1412                                         gld.gl_date,
1413                                         'FINCHRG_NON_REC') ae_finchrg_non_rec_tax_ccid,
1414           decode(g_ae_rule_rec.tax_code_source1,
1415                      'INVOICE', decode(g_ae_rule_rec.tax_recoverable_flag1,
1416                                        'N',
1417                                        decode(g_ae_doc_rec.source_table,
1418                                               'RA',
1419           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1420                                         gld.gl_date,
1421                                         'EDISC_NON_REC'),
1422                                               'ADJ',
1423                                               decode(g_ae_doc_rec.document_type,
1424                                                      'ADJUSTMENT',
1425           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1426                                         gld.gl_date,
1427                                         'ADJ_NON_REC'),
1428                                                     'FINANCE_CHARGES',
1429           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1430                                         gld.gl_date,
1431                                         'FINCHRG_NON_REC'),
1432                                                              ''),
1433                                                         ''),
1434                                            ''),
1435                         'ACTIVITY', g_ae_rule_rec.act_tax_non_rec_ccid1,
1436                         '')                          ae_override_ccid1,
1437               decode(g_ae_rule_rec.tax_code_source2,
1438                          'INVOICE',decode(g_ae_rule_rec.tax_recoverable_flag2,
1439                                           'N', decode(g_ae_doc_rec.source_table,
1440                                                       'RA',
1441           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1442                                         gld.gl_date,
1443                                         'UNEDISC_NON_REC'),
1444                                                       ''),
1445                                           ''),
1446                          'ACTIVITY', g_ae_rule_rec.act_tax_non_rec_ccid2,
1447                          '')                           ae_override_ccid2,
1448               ctl.link_to_cust_trx_line_id             ae_tax_link_id,
1449               ctl.link_to_cust_trx_line_id             ae_tax_link_id_ed_adj,
1450               ctl.link_to_cust_trx_line_id             ae_tax_link_id_uned,
1451               ctl.link_to_cust_trx_line_id             ae_tax_link_id_act,
1452               det.amount                         ae_pro_amt,
1453               det.acctd_amount                   ae_pro_acctd_amt,
1454               0                                  ae_pro_frt_chrg_amt,
1455               0                                  ae_pro_frt_chrg_acctd_amt,
1456               det.taxable_amount                 ae_pro_taxable_amt,
1457               det.taxable_acctd_amount           ae_pro_taxable_acctd_amt,
1458               det.taxable_amount                 ae_pro_split_taxable_amt ,
1459               det.taxable_acctd_amount           ae_pro_split_taxable_acctd_amt,
1460               det.taxable_amount                 ae_pro_recov_taxable_amt,
1461               det.taxable_acctd_amount           ae_pro_recov_taxable_acctd_amt,
1462               0                                  ae_pro_def_tax_amt,
1463               0                                  ae_pro_def_tax_acctd_amt,
1464              'N'                                 ae_summarize_flag,
1465              'N'                                 ae_counted_flag,
1466               ctl.autotax                   ae_autotax,
1467              0                                   ae_sum_alloc_amt,
1468              0                                   ae_sum_alloc_acctd_amt,
1469              Get_Tax_Count(ctl.link_to_cust_trx_line_id) ae_tax_line_count,
1470              det.ref_account_class                       ref_account_class,
1471              det.activity_bucket                          activity_bucket,
1472              det.ref_line_id                     ae_ref_line_id,
1473              det.from_amount                     ae_from_pro_amt,
1474              det.from_acctd_amount               ae_from_pro_acctd_amt,
1475              det.ccid                            ref_dist_ccid,
1476              det.ref_mf_dist_flag
1477        FROM ra_customer_trx_lines     ctl,
1478             ra_cust_trx_line_gl_dist  gld,
1479             ra_customer_trx_lines     line,
1480             ar_line_app_detail_gt     det
1481        where ctl.customer_trx_id = p_invoice_id
1482        and   ctl.line_type = 'TAX'
1483        and   gld.customer_trx_line_id = ctl.customer_trx_line_id
1484        and   gld.account_set_flag = 'N'
1485        and   ctl.link_to_cust_trx_line_id = line.customer_trx_line_id (+)
1486        and   'LINE' = line.line_type (+)
1487        AND   det.ref_customer_trx_id   = ctl.customer_trx_id
1488        AND   det.ref_customer_trx_line_id = ctl.customer_trx_line_id
1489        AND   det.ref_cust_trx_line_gl_dist_id = gld.cust_trx_line_gl_dist_id
1490        AND   det.gt_id                    = g_id
1491        AND   det.ledger_id                = g_ae_sys_rec.set_of_books_id;
1492        /* and   not exists (select 'x'
1493                          from ra_customer_trx_lines ctl1
1494                          where ctl1.customer_trx_id = p_invoice_id
1495                          and   ctl1.autorule_complete_flag = 'N')   */
1496                 /* nvl(tax.location_segment_id,tax.vat_tax_id),
1497                 decode(tax.location_segment_id,
1498                                    '','VAT',
1499                                    'LOC') */
1500 
1501 
1502       arp_standard.debug('p_process_ed_adj:'||p_process_ed_adj);
1503       arp_standard.debug('p_process_uned:'||p_process_uned);
1504 
1505 
1506       IF ((NVL(p_process_ed_adj,'N')        = 'Y' AND
1507            g_ae_rule_rec.gl_account_source1 = 'REVENUE_ON_INVOICE' AND
1508            g_ae_rule_rec.tax_code_source1   = 'NONE') OR
1509          ( NVL(p_process_uned,'N')          = 'Y' AND
1510            g_ae_rule_rec.gl_account_source2 = 'REVENUE_ON_INVOICE' AND
1511            g_ae_rule_rec.tax_code_source2   = 'NONE'))
1512       THEN
1513          OPEN crevccid;
1514          FETCH crevccid BULK COLLECT INTO
1515            l_ccid_tab ,
1516            l_ctlgd_tab;
1517          CLOSE crevccid;
1518          IF l_ctlgd_tab.COUNT > 0 THEN
1519            FORALL k IN l_ccid_tab.FIRST .. l_ccid_tab.LAST
1520            UPDATE ar_ae_alloc_rec_gt
1521               SET ae_code_combination_id = l_ccid_tab(k)
1522             WHERE ae_cust_trx_line_gl_dist_id = l_ctlgd_tab(k);
1523          END IF;
1524      END IF;
1525 
1526 
1527 
1528    insert into ar_ae_alloc_rec_gt (
1529      ae_id                       ,
1530      ae_account_class            ,
1531      ae_customer_trx_id          ,
1532      ae_customer_trx_line_id     ,
1533      ae_cust_trx_line_gl_dist_id ,
1534      ae_link_to_cust_trx_line_id ,
1535      ae_tax_type                 ,
1536      ae_code_combination_id      ,
1537      ae_collected_tax_ccid       ,
1538      ae_line_amount              ,
1539      ae_amount                   ,
1540      ae_acctd_amount             ,
1541      ae_tax_group_code_id        ,
1542      ae_tax_id                   ,
1543      ae_taxable_amount           ,
1544      ae_taxable_acctd_amount     ,
1545      ae_adj_ccid                 ,
1546      ae_edisc_ccid               ,
1547      ae_unedisc_ccid             ,
1548      ae_finchrg_ccid             ,
1549      ae_adj_non_rec_tax_ccid     ,
1550      ae_edisc_non_rec_tax_ccid   ,
1551      ae_unedisc_non_rec_tax_ccid ,
1552      ae_finchrg_non_rec_tax_ccid ,
1553      ae_override_ccid1           ,
1554      ae_override_ccid2           ,
1555      ae_tax_link_id              ,
1556      ae_tax_link_id_ed_adj       ,
1557      ae_tax_link_id_uned         ,
1558      ae_tax_link_id_act          ,
1559      ae_pro_amt                  ,
1560      ae_pro_acctd_amt            ,
1561      ae_pro_frt_chrg_amt         ,
1562      ae_pro_frt_chrg_acctd_amt   ,
1563      ae_pro_taxable_amt          ,
1564      ae_pro_taxable_acctd_amt    ,
1565      ae_pro_split_taxable_amt       ,
1566      ae_pro_split_taxable_acctd_amt ,
1567      ae_pro_recov_taxable_amt       ,
1568      ae_pro_recov_taxable_acctd_amt ,
1569      ae_pro_def_tax_amt          ,
1570      ae_pro_def_tax_acctd_amt    ,
1571      ae_summarize_flag           ,
1572      ae_counted_flag             ,
1573      ae_autotax                  ,
1574      ae_sum_alloc_amt            ,
1575      ae_sum_alloc_acctd_amt      ,
1576      ae_tax_line_count           ,
1577      ref_account_class                   ,
1578      activity_bucket                      ,
1579      AE_REF_LINE_ID,
1580      ae_from_pro_amt,
1581      ae_from_pro_acctd_amt,
1582      ref_dist_ccid,
1583      ref_mf_dist_flag
1584      )
1585        SELECT
1586           g_id                                      ae_id,
1587           'TAX'                                     ae_account_class,
1588           det.ref_customer_trx_id                   ae_customer_trx_id,
1589           det.ref_customer_trx_line_id              ae_customer_trx_line_id,
1590           det.ref_cust_trx_line_gl_dist_id          ae_cust_trx_line_gl_dist_id ,
1591           ''                                        ae_link_to_cust_trx_line_id,
1592           decode(ctl.location_segment_id,
1593                      '','VAT',
1594                      'LOC')                         ae_tax_type,
1595           --''                                        ae_tax_type,
1596           ''                                        ae_code_combination_id,
1597           ''                                        ae_collected_tax_ccid,
1598           ''                                        ae_line_amount,
1599           ''                                        ae_amount,
1600           ''                                        ae_acctd_amount,
1601           decode(ctl.location_segment_id,
1602                     '',
1603                     decode(nvl(ctl.autotax,'Y'),
1604                           'N', '',
1605                            decode(nvl(line.location_segment_id,line.vat_tax_id),
1606                                 '','',
1607                                 nvl(ctl.location_segment_id, ctl.vat_tax_id),'',
1608                                 nvl(line.location_segment_id,line.vat_tax_id))),
1609                          '')                       ae_tax_group_code_id,
1610           nvl(ctl.location_segment_id,ctl.vat_tax_id) ae_tax_id,
1611           --''                                        ae_tax_group_code_id,
1612           --''                                        ae_tax_id,
1613           det.amount                                ae_taxable_amount,
1614           arpcurr.functional_amount(nvl(det.amount,0)       ,
1615                        g_ae_sys_rec.base_currency   ,
1616                        g_cust_inv_rec.exchange_rate,
1617                        g_ae_sys_rec.base_precision  ,
1618                        g_ae_sys_rec.base_min_acc_unit) ae_taxable_acctd_amount,
1619 
1620           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1621                                         null,
1622                                         'ADJ')         ae_adj_ccid,
1623           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1624                                         null,
1625                                         'EDISC')       ae_edisc_ccid,
1626           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1627                                         null,
1628                                         'UNEDISC')     ae_unedisc_ccid,
1629           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1630                                         null,
1631                                         'FINCHRG')     ae_finchrg_ccid,
1632           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1633                                         null,
1634                                         'ADJ_NON_REC') ae_adj_non_rec_tax_ccid,
1635           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1636                                         null,
1637                                         'EDISC_NON_REC') ae_edisc_non_rec_tax_ccid,
1638           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1639                                         null,
1640                                         'UNEDISC_NON_REC') ae_unedisc_non_rec_tax_ccid,
1641           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1642                                         null,
1643                                         'FINCHRG_NON_REC') ae_finchrg_non_rec_tax_ccid,
1644           decode(g_ae_rule_rec.tax_code_source1,
1645                      'INVOICE', decode(g_ae_rule_rec.tax_recoverable_flag1,
1646                                        'N',
1647                                        decode(g_ae_doc_rec.source_table,
1648                                               'RA',
1649           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1650                                         null,
1651                                         'EDISC_NON_REC'),
1652                                               'ADJ',
1653                                               decode(g_ae_doc_rec.document_type,
1654                                                      'ADJUSTMENT',
1655           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1656                                         null,
1657                                         'ADJ_NON_REC'),
1658                                                     'FINANCE_CHARGES',
1659           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1660                                         null,
1661                                         'FINCHRG_NON_REC'),
1662                                                              ''),
1663                                                         ''),
1664                                            ''),
1665                         'ACTIVITY', g_ae_rule_rec.act_tax_non_rec_ccid1,
1666                         '')                          ae_override_ccid1,
1667               decode(g_ae_rule_rec.tax_code_source2,
1668                          'INVOICE',decode(g_ae_rule_rec.tax_recoverable_flag2,
1669                                           'N', decode(g_ae_doc_rec.source_table,
1670                                                       'RA',
1671           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1672                                         null,
1673                                         'UNEDISC_NON_REC'),
1674                                                       ''),
1675                                           ''),
1676                          'ACTIVITY', g_ae_rule_rec.act_tax_non_rec_ccid2,
1677                          '')                           ae_override_ccid2,
1678 
1679 --          ''                                         ae_adj_ccid,
1680 --          ''                                         ae_edisc_ccid,
1681 --          ''                                         ae_unedisc_ccid,
1682 --          ''                                         ae_finchrg_ccid,
1683 --          ''                                         ae_adj_non_rec_tax_ccid,
1684 --          ''                                         ae_edisc_non_rec_tax_ccid,
1685 --          ''                                         ae_unedisc_non_rec_tax_ccid,
1686 --          ''                                         ae_finchrg_non_rec_tax_ccid,
1687 --          ''                                         ae_override_ccid1,
1688 --          ''                                         ae_override_ccid2,
1689           ''                                         ae_tax_link_id,
1690           ''                                         ae_tax_link_id_ed_adj,
1691           ''                                         ae_tax_link_id_uned,
1692           ''                                  ae_tax_link_id_act,
1693           det.amount                          ae_pro_amt,
1694           det.acctd_amount                    ae_pro_acctd_amt,
1695           0                                   ae_pro_frt_chrg_amt,
1696           0                                   ae_pro_frt_chrg_acctd_amt,
1697               det.taxable_amount                 ae_pro_taxable_amt,
1698               det.taxable_acctd_amount           ae_pro_taxable_acctd_amt,
1699               det.taxable_amount                 ae_pro_split_taxable_amt ,
1700               det.taxable_acctd_amount           ae_pro_split_taxable_acctd_amt,
1701               det.taxable_amount                 ae_pro_recov_taxable_amt,
1702               det.taxable_acctd_amount           ae_pro_recov_taxable_acctd_amt,
1703           0                                   ae_pro_def_tax_amt,
1704           0                                   ae_pro_def_tax_acctd_amt,
1705           'N'                                 ae_summarize_flag,
1706           'N'                                 ae_counted_flag,
1707           ''                                  ae_autotax,
1708           0                                   ae_sum_alloc_amt,
1709           0                                   ae_sum_alloc_acctd_amt,
1710           ''                                  ae_tax_line_count,
1711           det.ref_account_class                       ref_account_class,
1712           det.activity_bucket                          activity_bucket,
1713           det.ref_line_id                     ae_line_id,
1714           det.from_amount                     ae_from_pro_amt,
1715           det.from_acctd_amount               ae_from_pro_acctd_amt,
1716           det.ccid                            ref_dist_ccid,
1717           det.ref_mf_dist_flag                ref_mf_dist_flag
1718        FROM ar_line_app_detail_gt  det,
1719             ra_customer_trx_lines  ctl,
1720             ra_customer_trx_lines  line,
1721             ( SELECT customer_trx_id,
1722                      nvl(decode(g_ae_doc_rec.document_type,'ADJUSTMENT',-9999,location_segment_id),-9999) location_segment_id,
1723                      nvl(decode(g_ae_doc_rec.document_type,'ADJUSTMENT',-9999,vat_tax_id),-9999) tax_code_id,
1724 		     min(customer_trx_line_id) customer_trx_line_id
1725 	      FROM ra_customer_trx_lines tax
1726 	      GROUP BY customer_trx_id,
1727                  decode(g_ae_doc_rec.document_type,'ADJUSTMENT',-9999,location_segment_id),
1728                  decode(g_ae_doc_rec.document_type,'ADJUSTMENT',-9999,vat_tax_id) ) tax_link
1729        WHERE det.ref_customer_trx_id = p_invoice_id
1730        AND   gt_id   = g_id
1731        AND   det.ref_customer_trx_id            = tax_link.customer_trx_id
1732        AND   NVL(det.location_segment_id,-9999) = tax_link.location_segment_id
1733        AND   NVL(det.tax_code_id,-9999)         = tax_link.tax_code_id
1734        AND   ctl.customer_trx_line_id           = tax_link.customer_trx_line_id
1735        AND   ctl.customer_trx_id                = det.ref_customer_trx_id
1736        AND   ctl.link_to_cust_trx_line_id       = line.customer_trx_line_id (+)
1737        AND   'LINE'                             = line.line_type (+)
1738        AND   det.ledger_id   = g_ae_sys_rec.set_of_books_id
1739        AND   ref_customer_trx_line_id IN (-8);
1740 --              (-6, --Boundary line : -6
1741 --               -7, --Boundary charge:-7
1742 --               -8, --Boundary tax
1743 --               -9); --Boundary freight
1744 
1745 
1746 
1747 
1748       IF PG_DEBUG in ('Y', 'C') THEN
1749          arp_standard.debug('Get_Invoice_Distributions: ' || 'Tax and Deferred Tax amount accumulators, non recoverable account validation');
1750       END IF;
1751 
1752       for l_get_group_data IN get_group_data_tax LOOP
1753 
1754        /*---------------------------------------------------------------------+
1755         | Validate account setup for Non Recoverable tax accounts for earned  |
1756         | discounts and adjustments                                           |
1757         +---------------------------------------------------------------------*/
1758         /*
1759             IF ((((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.tax_ediscounted,0) <> 0))
1760               OR ((g_ae_doc_rec.source_table = 'ADJ') AND (nvl(p_adj_rec.tax_adjusted,0) <> 0)))
1761               AND (((g_ae_rule_rec.tax_code_source1 = 'INVOICE') AND (g_ae_rule_rec.tax_recoverable_flag1 = 'N'))
1762                    OR (g_ae_rule_rec.tax_code_source1 = 'ACTIVITY'))
1763               AND (l_get_group_data.ae_override_ccid1 = 2)) THEN
1764 
1765                 IF PG_DEBUG in ('Y', 'C') THEN
1766                    arp_standard.debug('Get_Invoice_Distributions: ' || 'Invalid CCid error');
1767                 END IF;
1768                 RAISE invalid_ccid_error;
1769 
1770             END IF;
1771         */
1772 
1773        /*----------------------------------------------------------------+
1774         | Validate account setup for Non Recoverable tax accounts for    |
1775         | unearned discounts                                             |
1776         +----------------------------------------------------------------*/
1777         /*
1778           IF (((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.tax_uediscounted,0) <> 0))
1779               AND (((g_ae_rule_rec.tax_code_source2 = 'INVOICE') AND (g_ae_rule_rec.tax_recoverable_flag2 = 'N'))
1780                    OR (g_ae_rule_rec.tax_code_source2 = 'ACTIVITY'))
1781               AND (l_get_group_data.ae_override_ccid2 = 2)) THEN
1782 
1783               IF PG_DEBUG in ('Y', 'C') THEN
1784                  arp_standard.debug('Get_Invoice_Distributions: ' || 'Invalid CCid error');
1785               END IF;
1786               RAISE invalid_ccid_error;
1787 
1788           END IF;
1789         */
1790 --}
1791        /*-------------------------------------------------------------------+
1792         | Total accumulators for tax and tax accounted amount               |
1793         +-------------------------------------------------------------------*/
1794           g_ae_rule_rec.tax_amt       := g_ae_rule_rec.tax_amt +
1795                                          l_get_group_data.sum_ae_amount;
1796           g_ae_rule_rec.tax_acctd_amt := g_ae_rule_rec.tax_acctd_amt +
1797                                          l_get_group_data.sum_ae_acctd_amount;
1798 
1799       /*----------------------------------------------------------------------+
1800        | Total accumulators for deferred tax and deferred tax accounted amount|
1801        +----------------------------------------------------------------------*/
1802           IF l_get_group_data.ae_account_class = 'DEFTAX' THEN
1803              g_ae_rule_rec.def_tax_amt       := l_get_group_data.sum_ae_amount;
1804              g_ae_rule_rec.def_tax_acctd_amt := l_get_group_data.sum_ae_acctd_amount;
1805           END IF;
1806 
1807        --Assign tax lines
1808           g_ae_tax_ctr := g_ae_tax_ctr + l_get_group_data.ae_count;
1809 
1810           IF PG_DEBUG in ('Y', 'C') THEN
1811              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_tax_ctr = ' || g_ae_tax_ctr);
1812              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.def_tax_amt = ' ||
1813                                 g_ae_rule_rec.def_tax_amt);
1814              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.def_tax_acctd_amt = ' ||
1815                                 g_ae_rule_rec.def_tax_acctd_amt);
1816              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_amt = ' || g_ae_rule_rec.tax_amt);
1817              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_acctd_amt = ' ||
1818                                 g_ae_rule_rec.tax_acctd_amt);
1819           END IF;
1820 
1821      END LOOP;
1822 
1823  -----------------------------------------------------------------------------
1824  --Set the tax link ids
1825  -----------------------------------------------------------------------------
1826  /* Obsolete
1827      Get_Tax_Link_Id(p_process_ed_adj => p_process_ed_adj,
1828                      p_process_uned   => p_process_uned,
1829                      p_process_pay    => p_process_pay);
1830   */
1831    IF PG_DEBUG in ('Y', 'C') THEN
1832       arp_standard.debug('Get_Invoice_Distributions: ' || 'Inserting Revenue lines');
1833    END IF;
1834 
1835 
1836 /*----------------------------------------------------------------------------+
1837  | Insert the Revenue, Unearned, Receivable, Suppense, Tax lines into the     |
1838  | temporary allocation table for computation purposes to build final         |
1839  | accounting                                                                 |
1840  +----------------------------------------------------------------------------*/
1841 --note we have removed the outer join to lines table for REC,
1842 --  since UNBILL and UNEARN
1843 --always have a customer_trx_line_id
1844 
1845   -- MRC Trigger Replacement:  get currency sensitive data
1846    insert into ar_ae_alloc_rec_gt (
1847      ae_id                       ,
1848      ae_account_class            ,
1849      ae_customer_trx_id          ,
1850      ae_customer_trx_line_id     ,
1851      ae_cust_trx_line_gl_dist_id ,
1852      ae_link_to_cust_trx_line_id ,
1853      ae_tax_type                 ,
1854      ae_code_combination_id      ,
1855      ae_collected_tax_ccid       ,
1856      ae_line_amount              ,
1857      ae_amount                   ,
1858      ae_acctd_amount             ,
1859      ae_tax_group_code_id        ,
1860      ae_tax_id                   ,
1861      ae_taxable_amount           ,
1862      ae_taxable_acctd_amount     ,
1863      ae_adj_ccid                 ,
1864      ae_edisc_ccid               ,
1865      ae_unedisc_ccid             ,
1866      ae_finchrg_ccid             ,
1867      ae_adj_non_rec_tax_ccid     ,
1868      ae_edisc_non_rec_tax_ccid   ,
1869      ae_unedisc_non_rec_tax_ccid ,
1870      ae_finchrg_non_rec_tax_ccid ,
1871      ae_override_ccid1           ,
1872      ae_override_ccid2           ,
1873      ae_tax_link_id              ,
1874      ae_tax_link_id_ed_adj       ,
1875      ae_tax_link_id_uned         ,
1876      ae_tax_link_id_act          ,
1877      ae_pro_amt                  ,
1878      ae_pro_acctd_amt            ,
1879      ae_pro_frt_chrg_amt         ,
1880      ae_pro_frt_chrg_acctd_amt   ,
1881      ae_pro_taxable_amt          ,
1882      ae_pro_taxable_acctd_amt    ,
1883      ae_pro_split_taxable_amt      ,
1884      ae_pro_split_taxable_acctd_amt ,
1885      ae_pro_recov_taxable_amt      ,
1886      ae_pro_recov_taxable_acctd_amt ,
1887      ae_pro_def_tax_amt          ,
1888      ae_pro_def_tax_acctd_amt    ,
1889      ae_summarize_flag           ,
1890      ae_counted_flag             ,
1891      ae_autotax                  ,
1892      ae_sum_alloc_amt            ,
1893      ae_sum_alloc_acctd_amt      ,
1894      ae_tax_line_count           ,
1895      ref_account_class                   ,
1896      activity_bucket,
1897      ae_ref_line_id,
1898      ae_from_pro_amt,
1899      ae_from_pro_acctd_amt,
1900      --{ref_dist_ccid
1901      ref_dist_ccid,
1902      ref_mf_dist_flag
1903      --}
1904      )
1905        SELECT g_id                    ae_id,
1906               decode(gld.account_class,
1907                      'REV'     ,'REVEARN',
1908                      'CHARGES' ,'REVEARN',
1909                      'SUSPENSE','REVEARN',
1910                      'UNBILL'  ,'REVUNEARN',
1911                      'UNEARN'  ,'REVUNEARN',
1912                      'FREIGHT' ,'FREIGHT')    ae_account_class,
1913               ctl.customer_trx_id             ae_customer_trx_id,
1914               ctl.customer_trx_line_id        ae_customer_trx_line_id,
1915               gld.cust_trx_line_gl_dist_id    ae_cust_trx_line_gl_dist_id ,
1916               nvl(ctl.link_to_cust_trx_line_id,
1917                   -9999)                      ae_link_to_cust_trx_line_id,
1918               decode(gld.account_class,
1919                      'FREIGHT','FREIGHT',
1920                      'REV')                                ae_tax_type,
1921               gld.code_combination_id         ae_code_combination_id,
1922               gld.collected_tax_ccid          ae_collected_tax_ccid,
1923               decode(gld.account_class,
1924                         'REV', nvl(ctl.revenue_amount,0),
1925                         'FREIGHT', nvl(ctl.revenue_amount,0),
1926                         'SUSPENSE',(ctl.extended_amount -
1927                                     nvl(ctl.revenue_amount,0)),
1928                         ctl.extended_amount)  ae_line_amount,
1929               nvl(gld.amount,0)           ae_amount,
1930               NVL(gld.acctd_amount,0)          ae_acctd_amount,
1931               ''                               ae_tax_group_code_id,
1932               ''                               ae_tax_id,
1933               ''                               ae_taxable_amount,
1934               ''                               ae_taxable_acctd_amount,
1935               ''                               ae_adj_ccid,
1936               ''                               ae_edisc_ccid,
1937               ''                               ae_unedisc_ccid,
1938               ''                               ae_finchrg_ccid,
1939               ''                               ae_adj_non_rec_tax_ccid,
1940               ''                               ae_edisc_non_rec_tax_ccid,
1941               ''                               ae_unedisc_non_rec_tax_ccid,
1942               ''                               ae_finchrg_non_rec_tax_ccid,
1943               decode(l_override1,
1944                          'Y',
1945                          decode(g_ae_rule_rec.gl_account_source1,
1946                                 'ACTIVITY_GL_ACCOUNT',
1947                                     g_ae_rule_rec.code_combination_id1,
1948                                 'TAX_CODE_ON_INVOICE',b5.override_ccid1,
1949                                   ''),
1950                          '')                   ae_override_ccid1,
1951 /*
1952               DECODE(l_override1,
1953                          'Y',
1954                          DECODE( DECODE(det.activity_bucket, 'APP_LINE', 'ACTIVITY_GL_ACCOUNT',
1955                                                 'APP_TAX' , 'ACTIVITY_GL_ACCOUNT',
1956                                                 'APP_FRT' , 'ACTIVITY_GL_ACCOUNT',
1957                                                 'APP_CHRG', 'ACTIVITY_GL_ACCOUNT',
1958                                                   g_ae_rule_rec.gl_account_source1),
1959                                 'ACTIVITY_GL_ACCOUNT',
1960                                         DECODE(det.activity_bucket,'APP_LINE',g_ae_rule_rec.receivable_account,
1961                                                       'APP_TAX' ,g_ae_rule_rec.receivable_account,
1962                                                       'APP_FRT' ,g_ae_rule_rec.receivable_account,
1963                                                       'APP_CHRG',g_ae_rule_rec.receivable_account,
1964                                                             g_ae_rule_rec.code_combination_id1),
1965                                 'TAX_CODE_ON_INVOICE',b5.override_ccid1,
1966                                   ''),
1967                          '')                   ae_override_ccid1,
1968 */
1969               decode(l_override2,
1970                          'Y',
1971                          decode(g_ae_rule_rec.gl_account_source2,
1972                                 'ACTIVITY_GL_ACCOUNT',
1973                                    g_ae_rule_rec.code_combination_id2,
1974                                 'TAX_CODE_ON_INVOICE',b5.override_ccid2,
1975                                  ''),
1976                          '')                   ae_override_ccid2        ,
1977 --              ''                                ae_tax_link_id           ,
1978 --              ''                                ae_tax_link_id_ed_adj    ,
1979 --              ''                                ae_tax_link_id_uned      ,
1980               ctl.customer_trx_line_id          ae_tax_link_id,
1981               ctl.customer_trx_line_id          ae_tax_link_id_ed_adj,
1982               ctl.customer_trx_line_id          ae_tax_link_id_uned,
1983               ctl.customer_trx_line_id          ae_tax_link_id_act       ,
1984               det.amount                        ae_pro_amt               ,
1985               det.acctd_amount                  ae_pro_acctd_amt         ,
1986               0                                 ae_pro_frt_chrg_amt      ,
1987               0                                 ae_pro_frt_chrg_acctd_amt,
1988               det.taxable_amount                 ae_pro_taxable_amt,
1989               det.taxable_acctd_amount           ae_pro_taxable_acctd_amt,
1990               det.taxable_amount                 ae_pro_split_taxable_amt ,
1991               det.taxable_acctd_amount           ae_pro_split_taxable_acctd_amt,
1992               det.taxable_amount                 ae_pro_recov_taxable_amt,
1993               det.taxable_acctd_amount           ae_pro_recov_taxable_acctd_amt,
1994               0                                 ae_pro_def_tax_amt       ,
1995               0                                 ae_pro_def_tax_acctd_amt ,
1996              'N'                                ae_summarize_flag        ,
1997              'N'                                ae_counted_flag          ,
1998              ''                                 ae_autotax               ,
1999              0                                  ae_sum_alloc_amt         ,
2000              0                                  ae_sum_alloc_acctd_amt   ,
2001              0                                  ae_tax_line_count        ,
2002              det.ref_account_class                 ref_account_class,
2003              det.activity_bucket                    activity_bucket,
2004              det.ref_line_id               ae_ref_line_id,
2005              det.from_amount                     ae_from_pro_amt,
2006              det.from_acctd_amount               ae_from_pro_acctd_amt,
2007              --{ref_dist_ccid
2008              det.ccid                            ref_dist_ccid,
2009              det.ref_mf_dist_flag                ref_mf_dist_flag
2010              --}
2011        from ra_customer_trx_lines     ctl,
2012             ra_cust_trx_line_gl_dist  gld,
2013             ar_line_app_detail_gt  det,
2014             (select b4.ae_link_to_cust_trx_line_id ae_link_to_cust_trx_line_id,
2015                     max(decode(g_ae_rule_rec.gl_account_source1,
2016                            'TAX_CODE_ON_INVOICE',
2017                             decode(g_ae_doc_rec.source_table,
2018                                    'RA', b4.ae_edisc_ccid,
2019                                    'ADJ',decode(g_ae_doc_rec.document_type,
2020                                    'ADJUSTMENT', b4.ae_adj_ccid,
2021                                    'FINANCE_CHARGES',b4.ae_finchrg_ccid,
2022                                                                       ''),
2023                                                          ''),
2024                            ''))               override_ccid1,
2025                     max(decode(g_ae_rule_rec.gl_account_source2,
2026                         'TAX_CODE_ON_INVOICE', decode(g_ae_doc_rec.source_table,
2027                                                       'RA', b4.ae_unedisc_ccid,
2028                                                          ''),
2029                            ''))               override_ccid2
2030              from ar_ae_alloc_rec_gt b4
2031              where b4.rowid IN
2032                (select /*+ INDEX(b3 AR_AE_ALLOC_REC_GT_N3) */
2033                      min(b3.rowid)
2034                 from ar_ae_alloc_rec_gt b3
2035                 where b3.ae_id = g_id
2036                 and   b3.ae_account_class = 'TAX'
2037                 and   (((decode(g_ae_doc_rec.source_table,
2038                               'RA', decode(b3.ae_edisc_ccid,
2039                                            '','N',
2040                                            'Y'),
2041                               'ADJ',decode(g_ae_doc_rec.document_type,
2042                                            'ADJUSTMENT', decode(b3.ae_adj_ccid,
2043                                                                 '','N',
2044                                                                 'Y'),
2045                                            'FINANCE_CHARGES',decode(b3.ae_finchrg_ccid,
2046                                                                     '','N',
2047                                                                     'Y')),
2048                               'N')  = 'Y')
2049                           AND (l_override1 = 'Y')
2050                           AND (g_ae_rule_rec.gl_account_source1 = 'TAX_CODE_ON_INVOICE'))
2051                        OR
2052                        ((decode(g_ae_doc_rec.source_table,
2053                                 'RA', decode(b3.ae_unedisc_ccid,
2054                                              '','N',
2055                                              'Y'),
2056                                 'N') = 'Y')
2057                          AND (l_override2 = 'Y')
2058                          AND (g_ae_rule_rec.gl_account_source2 = 'TAX_CODE_ON_INVOICE')))
2059                 group by b3.ae_link_to_cust_trx_line_id)
2060 	      group by b4.ae_link_to_cust_trx_line_id
2061 	     -- Bug 6719986 Added union sql to get the rows for TAX line of zero percentage tax.
2062 	     UNION
2063              select ctl.link_to_cust_trx_line_id ae_link_to_cust_trx_line_id,
2064                     max(decode(g_ae_rule_rec.gl_account_source1,
2065                            'TAX_CODE_ON_INVOICE',
2066                             decode(g_ae_doc_rec.source_table,
2067                                    'RA',  arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
2068                                                                         gld.gl_date,
2069                                                                         'EDISC'),
2070                                    'ADJ',decode(g_ae_doc_rec.document_type,
2071                                                 'ADJUSTMENT',
2072                                           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
2073                                                                         gld.gl_date,
2074                                                                         'ADJ'),
2075                                                 'FINANCE_CHARGES',
2076                                           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
2077                                                                         gld.gl_date,
2078                                                                         'FINCHRG'),
2079                                                                       ''),
2080                                                          ''),
2081                            ''))               override_ccid1,
2082                     max(decode(g_ae_rule_rec.gl_account_source2,
2083                         'TAX_CODE_ON_INVOICE', decode(g_ae_doc_rec.source_table,
2084                                                       'RA',
2085                                           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
2086                                                                         gld.gl_date,
2087                                                                         'UNEDISC'),
2088                                                          ''),
2089                            ''))               override_ccid2
2090             from ra_customer_trx_lines ctl,
2091 	    ra_cust_trx_line_gl_dist  gld
2092             where gld.customer_trx_line_id = ctl.customer_trx_line_id
2093 	    and   gld.account_set_flag = 'N'
2094 	    and   ctl.customer_trx_id = p_invoice_id
2095 	    and   ctl.rowid in
2096 		     (select min(ctl1.rowid)
2097 		      from ra_customer_trx_lines ctl1,
2098 			  (select /*+ INDEX(b3 AR_AE_ALLOC_REC_GT_N3) */
2099 			   count(*) tax_count
2100 			   from ar_ae_alloc_rec_gt b3
2101 			   where b3.ae_id = g_id
2102 			   and   b3.ae_account_class = 'TAX') tx
2103 		      where ctl1.customer_trx_id = p_invoice_id
2104 		      and   ctl1.line_type = 'TAX'
2105 		      and tx.tax_count = 0
2106 		      group by ctl1.link_to_cust_trx_line_id)
2107               group by ctl.link_to_cust_trx_line_id
2108                 ) b5
2109        where ctl.customer_trx_id = p_invoice_id
2110        AND   ctl.line_type  IN ('LINE','FREIGHT','CB','CHARGES')
2111        --and   gld.customer_trx_id = ctl.customer_trx_id
2112        and   gld.customer_trx_line_id = ctl.customer_trx_line_id
2113        AND   gld.account_class IN ('REV','SUSPENSE','UNBILL','UNEARN','FREIGHT','CHARGES')
2114        and   gld.account_set_flag              = 'N'
2115        and   decode(ctl.line_type,
2116                     'FREIGHT', ctl.link_to_cust_trx_line_id,  --first available tax code netexpense account
2117                     ctl.customer_trx_line_id) = b5.ae_link_to_cust_trx_line_id (+)
2118        AND   det.ref_customer_trx_id          = ctl.customer_trx_id
2119        AND   det.ref_customer_trx_line_id     = ctl.customer_trx_line_id
2120        AND   det.ref_cust_trx_line_gl_dist_id = gld.cust_trx_line_gl_dist_id
2121        AND   det.ledger_id                = g_ae_sys_rec.set_of_books_id
2122        AND   det.gt_id                  =  g_id;
2123        /* and   not exists (select 'x'
2124                          from ra_customer_trx_lines ctl1
2125                          where ctl1.customer_trx_id = p_invoice_id
2126                          and   ctl1.autorule_complete_flag = 'N')   */
2127 --       group by decode(gld.account_class,
2128 --                       'REV'     ,'REVEARN',
2129 --                       'SUSPENSE','REVEARN',
2130 --                       'UNBILL'  ,'REVUNEARN',
2131 --                       'UNEARN'  ,'REVUNEARN',
2132 --                       'FREIGHT' ,'FREIGHT'),
2133 --                decode(gld.account_class,
2134 --                       'FREIGHT','FREIGHT',
2135 --                       'REV')                           ,
2136 --                gld.cust_trx_line_gl_dist_id            ,
2137 --                ctl.customer_trx_id                     ,
2138 --                nvl(ctl.link_to_cust_trx_line_id,-9999) ,
2139 --                ctl.customer_trx_line_id                ,
2140 --                gld.code_combination_id                 ,
2141 --                gld.collected_tax_ccid                    ;
2142 --}
2143         /* order by decode(gld.account_class,
2144                        'REV'     ,'REVEARN',
2145                        'SUSPENSE','REVEARN',
2146                        'UNBILL'  ,'REVUNEARN',
2147                        'UNEARN'  ,'REVUNEARN',
2148                        'TAX'     ,'TAX'),
2149                 ctl.customer_trx_id           ,
2150                 ctl.customer_trx_line_id   ; */
2151 
2152 
2153    insert into ar_ae_alloc_rec_gt (
2154      ae_id                       ,
2155      ae_account_class            ,
2156      ae_customer_trx_id          ,
2157      ae_customer_trx_line_id     ,
2158      ae_cust_trx_line_gl_dist_id ,
2159      ae_link_to_cust_trx_line_id ,
2160      ae_tax_type                 ,
2161      ae_code_combination_id      ,
2162      ae_collected_tax_ccid       ,
2163      ae_line_amount              ,
2164      ae_amount                   ,
2165      ae_acctd_amount             ,
2166      ae_tax_group_code_id        ,
2167      ae_tax_id                   ,
2168      ae_taxable_amount           ,
2169      ae_taxable_acctd_amount     ,
2170      ae_adj_ccid                 ,
2171      ae_edisc_ccid               ,
2172      ae_unedisc_ccid             ,
2173      ae_finchrg_ccid             ,
2174      ae_adj_non_rec_tax_ccid     ,
2175      ae_edisc_non_rec_tax_ccid   ,
2176      ae_unedisc_non_rec_tax_ccid ,
2177      ae_finchrg_non_rec_tax_ccid ,
2178      ae_override_ccid1           ,
2179      ae_override_ccid2           ,
2180      ae_tax_link_id              ,
2181      ae_tax_link_id_ed_adj       ,
2182      ae_tax_link_id_uned         ,
2183      ae_tax_link_id_act          ,
2184      ae_pro_amt                  ,
2185      ae_pro_acctd_amt            ,
2186      ae_pro_frt_chrg_amt         ,
2187      ae_pro_frt_chrg_acctd_amt   ,
2188      ae_pro_taxable_amt          ,
2189      ae_pro_taxable_acctd_amt    ,
2190      ae_pro_split_taxable_amt       ,
2191      ae_pro_split_taxable_acctd_amt ,
2192      ae_pro_recov_taxable_amt       ,
2193      ae_pro_recov_taxable_acctd_amt ,
2194      ae_pro_def_tax_amt          ,
2195      ae_pro_def_tax_acctd_amt    ,
2196      ae_summarize_flag           ,
2197      ae_counted_flag             ,
2198      ae_autotax                  ,
2199      ae_sum_alloc_amt            ,
2200      ae_sum_alloc_acctd_amt      ,
2201      ae_tax_line_count           ,
2202      ref_account_class                   ,
2203      activity_bucket                      ,
2204      ae_ref_line_id,
2205      ae_from_pro_amt,
2206      ae_from_pro_acctd_amt,
2207      --{ref_dist_ccid
2208      ref_dist_ccid,
2209      ref_mf_dist_flag
2210      --}
2211      )
2212        SELECT
2213           g_id                                      ae_id,
2214           DECODE(det.ref_account_class,'REV','REVEARN',
2215                                det.ref_account_class)       ae_account_class,
2216           det.ref_customer_trx_id                   ae_customer_trx_id,
2217           det.ref_customer_trx_line_id              ae_customer_trx_line_id,
2218           det.ref_cust_trx_line_gl_dist_id          ae_cust_trx_line_gl_dist_id ,
2219           ''                                        ae_link_to_cust_trx_line_id,
2220           ''                                        ae_tax_type,
2221           ''                                        ae_code_combination_id,
2222           ''                                        ae_collected_tax_ccid,
2223           ''                                        ae_line_amount,
2224           ''                                        ae_amount,
2225           ''                                        ae_acctd_amount,
2226           ''                                        ae_tax_group_code_id,
2227           ''                                        ae_tax_id,
2228           det.amount                                ae_taxable_amount,
2229           arpcurr.functional_amount(nvl(det.amount,0)       ,
2230                        g_ae_sys_rec.base_currency   ,
2231                        g_cust_inv_rec.exchange_rate,
2232                        g_ae_sys_rec.base_precision  ,
2233                        g_ae_sys_rec.base_min_acc_unit) ae_taxable_acctd_amount,
2234           ''                                         ae_adj_ccid,
2235           ''                                         ae_edisc_ccid,
2236           ''                                         ae_unedisc_ccid,
2237           ''                                         ae_finchrg_ccid,
2238           ''                                         ae_adj_non_rec_tax_ccid,
2239           ''                                         ae_edisc_non_rec_tax_ccid,
2240           ''                                         ae_unedisc_non_rec_tax_ccid,
2241           ''                                         ae_finchrg_non_rec_tax_ccid,
2242 	  decode(l_override1,
2243 		     'Y',
2244 		     decode(g_ae_rule_rec.gl_account_source1,
2245 			    'ACTIVITY_GL_ACCOUNT',
2246 				g_ae_rule_rec.code_combination_id1,
2247 			    'TAX_CODE_ON_INVOICE',b5.override_ccid1,
2248 			      ''),
2249 		     '')                             ae_override_ccid1,
2250 	  decode(l_override2,
2251 		     'Y',
2252 		     decode(g_ae_rule_rec.gl_account_source2,
2253 			    'ACTIVITY_GL_ACCOUNT',
2254 			       g_ae_rule_rec.code_combination_id2,
2255 			    'TAX_CODE_ON_INVOICE',b5.override_ccid2,
2256 			     ''),
2257 		     '')                             ae_override_ccid2,
2258           ''                                         ae_tax_link_id,
2259           ''                                         ae_tax_link_id_ed_adj,
2260           ''                                         ae_tax_link_id_uned,
2261           ''                                  ae_tax_link_id_act,
2262           det.amount                          ae_pro_amt,
2263           det.acctd_amount                    ae_pro_acctd_amt,
2264           0                                   ae_pro_frt_chrg_amt,
2265           0                                   ae_pro_frt_chrg_acctd_amt,
2266               det.taxable_amount                 ae_pro_taxable_amt,
2267               det.taxable_acctd_amount           ae_pro_taxable_acctd_amt,
2268               det.taxable_amount                 ae_pro_split_taxable_amt ,
2269               det.taxable_acctd_amount           ae_pro_split_taxable_acctd_amt,
2270               det.taxable_amount                 ae_pro_recov_taxable_amt,
2271               det.taxable_acctd_amount           ae_pro_recov_taxable_acctd_amt,
2272           0                                   ae_pro_def_tax_amt,
2273           0                                   ae_pro_def_tax_acctd_amt,
2274           'N'                                 ae_summarize_flag,
2275           'N'                                 ae_counted_flag,
2276           ''                                  ae_autotax,
2277           0                                   ae_sum_alloc_amt,
2278           0                                   ae_sum_alloc_acctd_amt,
2279           ''                                  ae_tax_line_count,
2280           det.ref_account_class                       ref_account_class,
2281           det.activity_bucket                          activity_bucket,
2282           det.ref_line_id                     ae_ref_line_id,
2283              det.from_amount                     ae_from_pro_amt,
2284              det.from_acctd_amount               ae_from_pro_acctd_amt,
2285              det.ccid                            ref_dist_ccid,
2286              det.ref_mf_dist_flag                ref_mf_dist_flag
2287        FROM  ar_line_app_detail_gt  det,
2288              ra_customer_trx_lines  ctl,
2289 	    ( SELECT customer_trx_id,
2290 	             NVL(location_segment_id,-9999) location_segment_id,
2291 	             NVL(vat_tax_id,-9999) tax_code_id,
2292 	             MIN(NVL(link_to_cust_trx_line_id,customer_trx_line_id)) link_to_cust_trx_line_id
2293 	      FROM ra_customer_trx_lines tax
2294 	      GROUP BY customer_trx_id,
2295 	            location_segment_id,
2296 	            vat_tax_id ) tax_link,
2297            ( select b4.ae_link_to_cust_trx_line_id ae_link_to_cust_trx_line_id,
2298                     max(decode(g_ae_rule_rec.gl_account_source1,
2299                            'TAX_CODE_ON_INVOICE',
2300                             decode(g_ae_doc_rec.source_table,
2301                                    'RA', b4.ae_edisc_ccid,
2302                                    'ADJ',decode(g_ae_doc_rec.document_type,
2303                                    'ADJUSTMENT', b4.ae_adj_ccid,
2304                                    'FINANCE_CHARGES',b4.ae_finchrg_ccid,
2305                                                                       ''),
2306                                                          ''),
2307                            ''))               override_ccid1,
2308                     max(decode(g_ae_rule_rec.gl_account_source2,
2309                         'TAX_CODE_ON_INVOICE', decode(g_ae_doc_rec.source_table,
2310                                                       'RA', b4.ae_unedisc_ccid,
2311                                                          ''),
2312                            ''))               override_ccid2
2313              from ar_ae_alloc_rec_gt b4
2314              where b4.rowid IN
2315                (select /*+ INDEX(b3 AR_AE_ALLOC_REC_GT_N3) */
2316                      min(b3.rowid)
2317                 from ar_ae_alloc_rec_gt b3
2318                 where b3.ae_id = g_id
2319                 and   b3.ae_account_class = 'TAX'
2320                 and   (((decode(g_ae_doc_rec.source_table,
2321                               'RA', decode(b3.ae_edisc_ccid,
2322                                            '','N',
2323                                            'Y'),
2324                               'ADJ',decode(g_ae_doc_rec.document_type,
2325                                            'ADJUSTMENT', decode(b3.ae_adj_ccid,
2326                                                                 '','N',
2327                                                                 'Y'),
2328                                            'FINANCE_CHARGES',decode(b3.ae_finchrg_ccid,
2329                                                                     '','N',
2330                                                                     'Y')),
2331                               'N')  = 'Y')
2332                           AND (l_override1 = 'Y')
2333                           AND (g_ae_rule_rec.gl_account_source1 = 'TAX_CODE_ON_INVOICE'))
2334                        OR
2335                        ((decode(g_ae_doc_rec.source_table,
2336                                 'RA', decode(b3.ae_unedisc_ccid,
2337                                              '','N',
2338                                              'Y'),
2339                                 'N') = 'Y')
2340                          AND (l_override2 = 'Y')
2341                          AND (g_ae_rule_rec.gl_account_source2 = 'TAX_CODE_ON_INVOICE')))
2342                 group by b3.ae_link_to_cust_trx_line_id)
2343 	      group by b4.ae_link_to_cust_trx_line_id
2344 	     -- Bug 6719986 Added union sql to get the rows for TAX line of zero percentage tax.
2345 	     UNION
2346              select ctl.link_to_cust_trx_line_id ae_link_to_cust_trx_line_id,
2347                     max(decode(g_ae_rule_rec.gl_account_source1,
2348                            'TAX_CODE_ON_INVOICE',
2349                             decode(g_ae_doc_rec.source_table,
2350                                    'RA',  arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
2351                                                                         gld.gl_date,
2352                                                                         'EDISC'),
2353                                    'ADJ',decode(g_ae_doc_rec.document_type,
2354                                                 'ADJUSTMENT',
2355                                           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
2356                                                                         gld.gl_date,
2357                                                                         'ADJ'),
2358                                                 'FINANCE_CHARGES',
2359                                           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
2360                                                                         gld.gl_date,
2361                                                                         'FINCHRG'),
2362                                                                       ''),
2363                                                          ''),
2364                            ''))               override_ccid1,
2365                     max(decode(g_ae_rule_rec.gl_account_source2,
2366                         'TAX_CODE_ON_INVOICE', decode(g_ae_doc_rec.source_table,
2367                                                       'RA',
2368                                           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
2369                                                                         gld.gl_date,
2370                                                                         'UNEDISC'),
2371                                                          ''),
2372                            ''))               override_ccid2
2373             from ra_customer_trx_lines ctl,
2374 	    ra_cust_trx_line_gl_dist  gld
2375             where gld.customer_trx_line_id = ctl.customer_trx_line_id
2376 	    and   gld.account_set_flag = 'N'
2377 	    and   ctl.customer_trx_id = p_invoice_id
2378 	    and   ctl.rowid in
2379 		     (select min(ctl1.rowid)
2380 		      from ra_customer_trx_lines ctl1,
2381 			  (select /*+ INDEX(b3 AR_AE_ALLOC_REC_GT_N3) */
2382 			   count(*) tax_count
2383 			   from ar_ae_alloc_rec_gt b3
2384 			   where b3.ae_id = g_id
2385 			   and   b3.ae_account_class = 'TAX') tx
2386 		      where ctl1.customer_trx_id = p_invoice_id
2387 		      and   ctl1.line_type = 'TAX'
2388 		      and tx.tax_count = 0
2389 		      group by ctl1.link_to_cust_trx_line_id)
2390               group by ctl.link_to_cust_trx_line_id
2391                 ) b5
2392        WHERE det.ref_customer_trx_id = p_invoice_id
2393        AND   det.ledger_id           = g_ae_sys_rec.set_of_books_id
2394        AND   gt_id   = g_id
2395        AND   ref_customer_trx_line_id IN (-6,-7,-9)
2396        AND   det.ref_customer_trx_id    = tax_link.customer_trx_id
2397        AND   NVL(det.location_segment_id,-9999) = tax_link.location_segment_id
2398        AND   nvl(det.tax_code_id,-9999) = tax_link.tax_code_id
2399        AND   ctl.customer_trx_line_id   = tax_link.link_to_cust_trx_line_id
2400        AND   ctl.customer_trx_id        = det.ref_customer_trx_id
2401        and   decode(ctl.line_type,
2402                     'FREIGHT', ctl.link_to_cust_trx_line_id,  --first available tax code netexpense account
2403                     ctl.customer_trx_line_id) = b5.ae_link_to_cust_trx_line_id (+);
2404 
2405 --              (-6, --Boundary line : -6
2406 --               -7, --Boundary charge:-7
2407 --               -8, --Boundary tax
2408 --               -9); --Boundary freight
2409 
2410 /*------------------------------------------------------------------------------+
2411  | Sum functions to set amount sums and counts required for decision making     |
2412  +------------------------------------------------------------------------------*/
2413   FOR l_get_group_data IN get_group_data_rev  LOOP
2414 
2415       IF l_get_group_data.ae_account_class = 'REVEARN' THEN
2416 
2417          g_ae_rev_ctr := l_get_group_data.ae_count;
2418 
2419        --Revenue total amounts and accounted amounts accumulator
2420           g_ae_rule_rec.revenue_amt       := l_get_group_data.sum_ae_amount;
2421           g_ae_rule_rec.revenue_acctd_amt := l_get_group_data.sum_ae_acctd_amount;
2422 
2423           IF PG_DEBUG in ('Y', 'C') THEN
2424              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rev_ctr ' || g_ae_rev_ctr);
2425              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.revenue_amt  = ' || g_ae_rule_rec.revenue_amt);
2426              arp_standard.debug('Get_Invoice_Distributions: ' ||
2427                                 'g_ae_rule_rec.revenue_acctd_amt = ' || g_ae_rule_rec.revenue_acctd_amt);
2428           END IF;
2429 
2430       ELSIF l_get_group_data.ae_account_class = 'REVUNEARN' THEN
2431 
2432              g_ae_unearn_rev_ctr        := l_get_group_data.ae_count;
2433 
2434              g_sum_unearn_rev_amt       := l_get_group_data.sum_ae_amount;
2435              g_sum_unearn_rev_acctd_amt := l_get_group_data.sum_ae_acctd_amount;
2436 
2437              IF PG_DEBUG in ('Y', 'C') THEN
2438                 arp_standard.debug('Get_Invoice_Distributions: ' ||
2439                                     'g_sum_unearn_rev_amt       = ' || g_sum_unearn_rev_amt);
2440                 arp_standard.debug('Get_Invoice_Distributions: ' ||
2441                                    'g_sum_unearn_rev_acctd_amt = ' || g_sum_unearn_rev_acctd_amt);
2442              END IF;
2443 
2444 
2445       ELSIF l_get_group_data.ae_account_class = 'REVXREC' THEN
2446 
2447            null;
2448           /* g_ae_rule_rec.receivable_amt        := l_get_group_data.sum_ae_amount;
2449           g_ae_rule_rec.receivable_acctd_amt  := l_get_group_data.sum_ae_acctd_amount;
2450 
2451           g_ae_rule_rec.receivable_account    := l_get_group_data.ae_code_combination_id;
2452 
2453           IF PG_DEBUG in ('Y', 'C') THEN
2454              arp_standard.debug('Get_Invoice_Distributions: ' ||
2455               'g_ae_rule_rec.receivable_amt = ' || g_ae_rule_rec.receivable_amt);
2456              arp_standard.debug('Get_Invoice_Distributions: ' ||
2457               'g_ae_rule_rec.receivable_acctd_amt = ' || g_ae_rule_rec.receivable_acctd_amt);
2458              arp_standard.debug('Get_Invoice_Distributions: ' ||
2459               'g_ae_rule_rec.receivable_account = ' || g_ae_rule_rec.receivable_account);
2460           END IF; */
2461 
2462       END IF;
2463 
2464   END LOOP;
2465 
2466 /*---------------------------------------------------------------------------+
2467  | Total accumulators for Receivable account class, and receivable ccid      |
2468  |                                                                           |
2469  | Modified for MRC TRIGGER REPLACEMENT:  get data from source depending     |
2470  | on the sob type                                                           |
2471  +---------------------------------------------------------------------------*/
2472 
2473   IF (NVL(g_ae_sys_rec.sob_type,'P') =  'P') THEN
2474       SELECT amount,
2475              acctd_amount,
2476              code_combination_id
2477       INTO g_ae_rule_rec.receivable_amt,
2478            g_ae_rule_rec.receivable_acctd_amt,
2479            g_ae_rule_rec.receivable_account
2480       FROM ra_cust_trx_line_gl_dist
2481       where customer_trx_id = p_invoice_id
2482         AND account_class = 'REC'
2483         and latest_rec_flag = 'Y';
2484   END IF;
2485 
2486   IF PG_DEBUG in ('Y', 'C') THEN
2487      arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.receivable_amt = ' ||
2488                       g_ae_rule_rec.receivable_amt);
2489      arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.receivable_acctd_amt = ' ||
2490                       g_ae_rule_rec.receivable_acctd_amt);
2491      arp_standard.debug('Get_Invoice_Distributions: ' ||
2492                         'g_ae_rule_rec.receivable_account = ' || g_ae_rule_rec.receivable_account);
2493   END IF;
2494 
2495 /*------------------------------------------------------------------------------+
2496  | Ascertain as to whether the revenue adjustment api will be used to derive the|
2497  | revenue distributions and the code combinations using autoaccounting, to     |
2498  | allocate the line amounts to gl account source = 'REVENUE ON INVOICE' or     |
2499  | tax code source is none for tax amount.                                      |
2500  +------------------------------------------------------------------------------*/
2501 /*IF ((g_sum_unearn_rev_amt <> 0) OR (g_sum_unearn_rev_acctd_amt <> 0)) THEN
2502        --
2503        --condition as to whether the RAM api will require to be called if gl account
2504        --source is revenue on invoice
2505        --
2506      IF (((g_ae_doc_rec.source_table = 'ADJ')
2507            AND (g_ae_rule_rec.gl_account_source1 = 'REVENUE_ON_INVOICE')
2508                 AND (((nvl(p_adj_rec.line_adjusted,0) + nvl(p_adj_rec.freight_adjusted,0) +
2509                        nvl(p_adj_rec.receivables_charges_adjusted,0)) <> 0)
2510                      OR ((g_ae_rule_rec.tax_code_source1 = 'NONE') AND (nvl(p_adj_rec.tax_adjusted,0) <> 0))
2511                     ))
2512            OR
2513            ((g_ae_doc_rec.source_table = 'RA')
2514             AND (((g_ae_rule_rec.gl_account_source1 = 'REVENUE_ON_INVOICE')
2515                   AND (((nvl(p_app_rec.line_ediscounted,0) + nvl(p_app_rec.freight_ediscounted,0) +
2516                          nvl(p_app_rec.charges_ediscounted,0)) <> 0)
2517                          OR ((g_ae_rule_rec.tax_code_source1 = 'NONE') AND (nvl(p_app_rec.tax_ediscounted,0) <> 0))
2518                       ))
2519                  OR
2520                  ((g_ae_rule_rec.gl_account_source2 = 'REVENUE_ON_INVOICE')
2521                   AND (((nvl(p_app_rec.line_uediscounted,0) + nvl(p_app_rec.freight_uediscounted,0) +
2522                          nvl(p_app_rec.charges_uediscounted,0)) <> 0)
2523                         OR ((g_ae_rule_rec.tax_code_source2 = 'NONE') AND (nvl(p_app_rec.tax_uediscounted,0) <> 0))
2524                       ))
2525                 ))
2526          )
2527      THEN --call revenue adjustment api
2528 */
2529    /*----------------------------------------------------------------------------+
2530     | Call the revenue adjustment api to derive the revenue distributions on the |
2531     | fly, to allocate the amounts for gl account source = revenue on Invoice.   |
2532     +----------------------------------------------------------------------------*/
2533 /*
2534         l_rev_adj_rec.customer_trx_id := p_invoice_id;
2535         l_rev_adj_rec.reason_code     := 'ACCOUNTING';
2536 
2537         AR_Revenue_Adjustment_PVT.Earn_Revenue
2538               (   p_api_version           => 2
2539                  ,p_init_msg_list         => FND_API.G_TRUE
2540                  ,p_commit                => FND_API.G_FALSE
2541                  ,p_validation_level      => FND_API.G_VALID_LEVEL_FULL
2542                  ,x_return_status         => l_return_status
2543                  ,x_msg_count             => l_msg_count
2544                  ,x_msg_data              => l_msg_data
2545                  ,p_rev_adj_rec           => l_rev_adj_rec
2546                  ,x_adjustment_id         => l_adj_id
2547                  ,x_adjustment_number     => l_adj_number
2548                  ,x_dist_count            => l_dist_count
2549                  ,x_ra_dist_tbl           => l_ra_dist_tbl);
2550 
2551         IF l_ra_dist_tbl.EXISTS(l_dist_count) AND (l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2552 
2553             FOR l_ctr2 IN l_ra_dist_tbl.FIRST .. l_ra_dist_tbl.LAST LOOP
2554 
2555               --process revenue distributions only
2556                 IF l_ra_dist_tbl(l_ctr2).account_class = 'REV' THEN
2557 
2558                     l_ae_tax_id := '';
2559 */
2560                 /*----------------------------------------------------------------------------+
2561                  |Unearn lines may have the tax code id set as they are associate with the    |
2562                  |original invoice lines, sweep through the unearned table for invoice lines  |
2563                  |which was the source for deriving the earned revenue using the rev adj api  |
2564                  +----------------------------------------------------------------------------*/
2565 /*
2566                     IF g_ae_unearn_rev_ctr > 0
2567                       AND l_ra_dist_tbl(l_ctr2).account_class = 'REV' THEN
2568 
2569                     --later on convert this into a join between the dist temp table
2570                     --and the ar_ae_alloc_rec_gt table
2571                        FOR g_ae_alloc_unearn_rev_tbl IN
2572                               l_rev_unearn('REVUNEARN', l_ra_dist_tbl(l_ctr2).customer_trx_line_id) LOOP
2573                               l_ae_tax_id := g_ae_alloc_unearn_rev_tbl.ae_tax_id;
2574                               EXIT;
2575 
2576                        END LOOP; --unearned revenue table
2577 
2578                     END IF; --dist class is REV
2579 
2580                 --Assign revenue lines and increment row counter
2581                     g_ae_rev_ctr := g_ae_rev_ctr + 1;
2582 
2583                 --assign elements
2584                     g_ae_alloc_rev_tbl.ae_account_class   := 'REVEARN';
2585                     g_ae_alloc_rev_tbl.ae_tax_id          := l_ae_tax_id;
2586                     g_ae_alloc_rev_tbl.ae_customer_trx_id :=
2587                                                        l_ra_dist_tbl(l_ctr2).customer_trx_id;
2588                     g_ae_alloc_rev_tbl.ae_customer_trx_line_id  :=
2589                                                        l_ra_dist_tbl(l_ctr2).customer_trx_line_id;
2590                     g_ae_alloc_rev_tbl.ae_code_combination_id   :=
2591                                                        l_ra_dist_tbl(l_ctr2).code_combination_id;
2592                     g_ae_alloc_rev_tbl.ae_amount                := l_ra_dist_tbl(l_ctr2).amount;
2593                     g_ae_alloc_rev_tbl.ae_acctd_amount          := l_ra_dist_tbl(l_ctr2).acctd_amount;
2594                     g_ae_alloc_rev_tbl.ae_pro_amt               := 0;
2595                     g_ae_alloc_rev_tbl.ae_pro_acctd_amt         := 0;
2596                     g_ae_alloc_rev_tbl.ae_pro_taxable_amt       := 0;
2597                     g_ae_alloc_rev_tbl.ae_pro_taxable_acctd_amt := 0;
2598                     g_ae_alloc_rev_tbl.ae_pro_split_taxable_amt := '';
2599                     g_ae_alloc_rev_tbl.ae_pro_split_taxable_acctd_amt := '';
2600                     g_ae_alloc_rev_tbl.ae_pro_recov_taxable_amt := '';
2601                     g_ae_alloc_rev_tbl.ae_pro_recov_taxable_acctd_amt := '';
2602                     g_ae_alloc_rev_tbl.ae_pro_def_tax_amt       := 0;
2603                     g_ae_alloc_rev_tbl.ae_pro_def_tax_acctd_amt := 0;
2604                     g_ae_alloc_rev_tbl.ae_counted_flag          := 'N';
2605 
2606                     Assign_Elements(g_ae_alloc_rev_tbl);
2607 
2608                  --Revenue total amounts and accounted amounts accumulator
2609                     g_ae_rule_rec.revenue_amt       :=
2610                           g_ae_rule_rec.revenue_amt + g_ae_alloc_rev_tbl.ae_amount;
2611                     g_ae_rule_rec.revenue_acctd_amt :=
2612                           g_ae_rule_rec.revenue_acctd_amt + g_ae_alloc_rev_tbl.ae_acctd_amount;
2613 
2614                 END IF; --revenue distributions only
2615 
2616             END LOOP; --end loop gl dist table from rev adj
2617 
2618          ELSIF ((l_return_status <> FND_API.G_RET_STS_SUCCESS)
2619                 OR ((l_dist_count = 0) AND (l_return_status = FND_API.G_RET_STS_SUCCESS))) THEN
2620 
2621                IF l_msg_count > 1 THEN
2622                   fnd_msg_pub.reset;
2623                 --get only the first message from the api message stack for forms users
2624                   l_mesg := fnd_msg_pub.get(p_encoded=>FND_API.G_FALSE);
2625                ELSE
2626                   l_mesg := l_msg_data;
2627                END IF;
2628 
2629              --Now set the message token
2630                FND_MESSAGE.SET_NAME('AR', 'GENERIC_MESSAGE');
2631                FND_MESSAGE.SET_TOKEN('GENERIC_TEXT', l_mesg);
2632 
2633                RAISE ram_api_error;
2634 
2635          END IF; --rev adj gl dist table exists and success from api
2636 
2637         --api Error handling
2638 
2639         --independent of the activity rule add the unearned entries to the revenue array,
2640         --to add to the sum of the line amount and accounted amount used as an allocation
2641         --basis to allocate amounts and formulate taxable amounts
2642        ELSE
2643 
2644           IF g_ae_unearn_rev_ctr > 0 THEN
2645 */
2646 --             UPDATE /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3 */ ar_ae_alloc_rec_gt
2647 /*
2648              SET ae_account_class = 'REVEARN'
2649              WHERE ae_id = g_id
2650              AND   ae_account_class = 'REVUNEARN';
2651 
2652           --Add the unearned count to the revenue count
2653              g_ae_rev_ctr := g_ae_rev_ctr + g_ae_unearn_rev_ctr;
2654 
2655           --Add the unearned amounts to the revenue amounts
2656              g_ae_rule_rec.revenue_amt       :=
2657                g_ae_rule_rec.revenue_amt + g_sum_unearn_rev_amt;
2658              g_ae_rule_rec.revenue_acctd_amt :=
2659                g_ae_rule_rec.revenue_acctd_amt + g_sum_unearn_rev_acctd_amt;
2660 
2661           END IF; --unearned revenue entry exists
2662 
2663        END IF; --ram api is called to derive revenue distributions
2664 
2665    END IF; --there exists a non zero unearned amount or accounted amount
2666 */
2667  /*----------------------------------------------------------------------------------+
2668   | Set the tax link id's for the revenue lines - we do this here since ram api is   |
2669   | called after the insert of original revenue, hence all link ids set at one time. |
2670   +----------------------------------------------------------------------------------*/
2671    UPDATE /*+ INDEX(b1 AR_AE_ALLOC_REC_GT_N3) */
2672           ar_ae_alloc_rec_gt b1
2673    SET (b1.ae_tax_link_id, b1.ae_tax_link_id_ed_adj, b1.ae_tax_link_id_uned) =
2674         (select /*+ INDEX(b8 AR_AE_ALLOC_REC_GT_N3) */
2675                 max(b8.ae_tax_link_id)        ae_tax_link_id,
2676                 max(b8.ae_tax_link_id_ed_adj) ae_tax_link_id_ed_adj,
2677                 max(b8.ae_tax_link_id_uned)   ae_tax_link_id_uned
2678              from ar_ae_alloc_rec_gt b8
2679              where b8.ae_id = g_id
2680              and   b8.ae_account_class = 'TAX'
2681              and   b8.ae_link_to_cust_trx_line_id = b1.ae_customer_trx_line_id)
2682    WHERE b1.ae_id = g_id
2683    AND   b1.ae_account_class IN ('REVEARN','REVUNEARN')  --MAINTAINTAXLINKID
2684    AND   EXISTS (select /*+ INDEX(b2 AR_AE_ALLOC_REC_GT_N3) */
2685                         'x'
2686                  from ar_ae_alloc_rec_gt b2
2687                  where b2.ae_id = g_id
2688                  and   b2.ae_account_class = 'TAX'
2689                  and   b2.ae_link_to_cust_trx_line_id = b1.ae_customer_trx_line_id);
2690 
2691  /*----------------------------------------------------------------------------------+
2692   | Get revenue amount totals, and fill up the revenue total table when tax code     |
2693   | source is NONE and there exists a tax amount to be allocated to gl account source|
2694   | Caches revenue amount and accounted amount totals for each Invoice Line, if      |
2695   | already cached then add to existing accumulators, totals for each Invoice        |
2696   | are required to be cached for rule allocating tax to revenue if TAX CODE         |
2697   | source is NONE.                                                                  |
2698   +----------------------------------------------------------------------------------*/
2699 
2700    IF (((g_ae_doc_rec.source_table = 'RA')
2701         AND (((nvl(p_app_rec.tax_ediscounted,0) <> 0) AND (g_ae_rule_rec.tax_code_source1 = 'NONE'))
2702              OR ((nvl(p_app_rec.tax_uediscounted,0) <> 0) AND (g_ae_rule_rec.tax_code_source2 = 'NONE'))))
2703        OR ((g_ae_doc_rec.source_table = 'ADJ') AND (g_ae_rule_rec.tax_code_source1 = 'NONE')
2704             AND (nvl(p_adj_rec.tax_adjusted,0) <> 0))) AND (g_ae_rev_ctr > 0)
2705    THEN
2706 
2707            UPDATE /*+ INDEX(at1 AR_AE_ALLOC_REC_GT_N3) */
2708                   ar_ae_alloc_rec_gt at1
2709            SET    (at1.ae_inv_line         ,
2710                    at1.ae_sum_rev_amt      ,
2711                    at1.ae_sum_rev_acctd_amt,
2712                    at1.ae_count) =
2713                      (SELECT /*+ INDEX(at2 AR_AE_ALLOC_REC_GT_N1) */
2714                              at2.ae_customer_trx_line_id,
2715                              sum(at2.ae_amount),
2716                              sum(at2.ae_acctd_amount),
2717                              count(at2.ae_customer_trx_line_id)
2718                       FROM ar_ae_alloc_rec_gt at2
2719                       WHERE at2.ae_id = g_id
2720                       AND   at2.ae_customer_trx_line_id = at1.ae_customer_trx_line_id
2721                       AND   at2.ae_account_class IN ('REVEARN','REVUNEARN') --MAINTAINTAXLINKID
2722                       GROUP BY at2.ae_customer_trx_line_id)
2723            WHERE at1.ae_id = g_id
2724            AND   at1.ae_account_class IN ('REVEARN','REVUNEARN'); --MAINTAINTAXLINKID
2725 
2726    END IF; --create revenue total table
2727 
2728 
2729    IF PG_DEBUG in ('Y', 'C') THEN
2730       arp_standard.debug('Get_Invoice_Distributions: ' ||
2731                          'g_ae_rule_rec.gl_account_source1    = '||g_ae_rule_rec.gl_account_source1);
2732       arp_standard.debug('Get_Invoice_Distributions: ' ||
2733                          'g_ae_rule_rec.tax_code_source1      = '||g_ae_rule_rec.tax_code_source1);
2734       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_recoverable_flag1 = '||
2735                          g_ae_rule_rec.tax_recoverable_flag1);
2736       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.code_combination_id1  = '||
2737                          g_ae_rule_rec.code_combination_id1);
2738       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.asset_tax_code1       = '||
2739                          g_ae_rule_rec.asset_tax_code1);
2740       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.liability_tax_code1   = '||
2741                          g_ae_rule_rec.liability_tax_code1);
2742       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.act_tax_non_rec_ccid1 = '||
2743                          g_ae_rule_rec.act_tax_non_rec_ccid1);
2744       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.gl_account_source2    = '||
2745                          g_ae_rule_rec.gl_account_source2);
2746       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_code_source2      = '||
2747                          g_ae_rule_rec.tax_code_source2);
2748       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_recoverable_flag2 = '||
2749                          g_ae_rule_rec.tax_recoverable_flag2);
2750       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.code_combination_id2  = '||
2751                          g_ae_rule_rec.code_combination_id2);
2752       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.asset_tax_code2       = '||
2753                          g_ae_rule_rec.asset_tax_code2);
2754       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.liability_tax_code2   = '||
2755                          g_ae_rule_rec.liability_tax_code2);
2756       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.act_tax_non_rec_ccid2 = '||
2757                          g_ae_rule_rec.act_tax_non_rec_ccid2);
2758       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.receivable_account    = '||
2759                          g_ae_rule_rec.receivable_account);
2760       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.receivable_amt        = '||
2761                          g_ae_rule_rec.receivable_amt);
2762       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.receivable_acctd_amt  = '||
2763                          g_ae_rule_rec.receivable_acctd_amt);
2764       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.revenue_amt           = '||
2765                          g_ae_rule_rec.revenue_amt);
2766       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.revenue_acctd_amt     = '||
2767                          g_ae_rule_rec.revenue_acctd_amt);
2768       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_amt               = '||
2769                          g_ae_rule_rec.tax_amt);
2770       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_acctd_amt         = '||
2771                          g_ae_rule_rec.tax_acctd_amt);
2772       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.def_tax_amt           = '||
2773                          g_ae_rule_rec.def_tax_amt);
2774       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.def_tax_acctd_amt     = '||
2775                          g_ae_rule_rec.def_tax_acctd_amt);
2776      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Invoice_Distributions()-');
2777   END IF;
2778 
2779 
2780 EXCEPTION
2781   WHEN invalid_ccid_error THEN
2782      IF PG_DEBUG in ('Y', 'C') THEN
2783         arp_standard.debug('Invalid Tax ccid - ARP_ALLOCATION_PKG.Get_Invoice_Distributions' );
2784      END IF;
2785      fnd_message.set_name('AR','AR_INVALID_TAX_ACCOUNT');
2786      RAISE;
2787 
2788   WHEN ram_api_error THEN
2789      IF PG_DEBUG in ('Y', 'C') THEN
2790         arp_standard.debug('ram_api_error - ARP_ALLOCATION_PKG.Get_Invoice_Distributions' );
2791      END IF;
2792      RAISE;
2793 
2794   WHEN NO_DATA_FOUND THEN
2795      IF PG_DEBUG in ('Y', 'C') THEN
2796         arp_standard.debug('ARP_ALLOCATION_PKG.Get_Invoice_Distributions - NO_DATA_FOUND' );
2797      END IF;
2798      RAISE;
2799 
2800   WHEN OTHERS THEN
2801      IF PG_DEBUG in ('Y', 'C') THEN
2802         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Get_Invoice_Distributions');
2803      END IF;
2804      RAISE;
2805 
2806 END Get_Invoice_Distributions;
2807 
2808 /* ==========================================================================
2809  | PROCEDURE Get_Tax_Link_Id
2810  |
2811  | DESCRIPTION
2812  |      Assigns Tax link Ids to each tax line in the tax table. The Rule
2813  |      assigining a tax link id is if for an Invoice line tax distribution
2814  |      there exists another tax distribution for an different Invoice line
2815  |      with the same Tax codes and count of Tax distributions for these
2816  |      Invoice lines then the Tax distributions of the first Invoice Line
2817  |      can be grouped with the Tax distributions of the second Invoice line.
2818  |      Tax link id can be common across different Invoice documents
2819  |
2820  | SCOPE - PRIVATE
2821  |
2822  | PARAMETERS
2823  |      p_type_acct         IN     Indicates accounting to be done for Earned
2824  |                                 discounts Adjustments or Unearned discounts
2825  *===========================================================================*/
2826 PROCEDURE Get_Tax_Link_Id(p_process_ed_adj IN VARCHAR2,
2827                           p_process_uned   IN VARCHAR2,
2828                           p_process_pay    IN VARCHAR2) IS
2829 
2830 l_link_ctr        BINARY_INTEGER := 0; --Actual Tax link id counter
2831 l_link_ctr1       BINARY_INTEGER := 0; --In case earned and unearned require link id
2832 
2833 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
2834 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
2835 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
2836 
2837 cursor get_tax IS
2838 select /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
2839       ae_link_to_cust_trx_line_id
2840 from ar_ae_alloc_rec_gt
2841 where ae_id = g_id
2842 and ae_account_class = 'TAX'
2843 group by ae_link_to_cust_trx_line_id
2844 order by ae_link_to_cust_trx_line_id;
2845 
2846 inv_line_tbl  DBMS_SQL.NUMBER_TABLE;
2847 
2848 l_last_fetch BOOLEAN := FALSE;
2849 l_mirror_link_ctr VARCHAR2(1) := 'N';
2850 l_set_ed_adj_link VARCHAR2(1) := 'N';
2851 l_set_uned_link   VARCHAR2(1) := 'N';
2852 l_set_pay_link    VARCHAR2(1) := 'N';
2853 
2854 BEGIN
2855    IF PG_DEBUG in ('Y', 'C') THEN
2856       arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Tax_Link_Id()+');
2857    END IF;
2858 
2859    l_link_ctr := g_link_ctr;
2860 
2861   -----------------------------------------------------------------------------
2862   --Set earned discount and adjustment link flag
2863   -----------------------------------------------------------------------------
2864    IF ((p_process_ed_adj = 'Y') AND (g_ed_adj_activity_link = 0)) THEN
2865       l_set_ed_adj_link := 'Y';
2866       IF PG_DEBUG in ('Y', 'C') THEN
2867          arp_standard.debug('Get_Tax_Link_Id: ' || ' l_set_ed_adj_link ' || l_set_ed_adj_link);
2868       END IF;
2869    END IF;
2870 
2871   -----------------------------------------------------------------------------
2872   --Set unearned discount link flag
2873   -----------------------------------------------------------------------------
2874    IF ((p_process_uned = 'Y') AND (g_uned_activity_link = 0)) THEN
2875       l_set_uned_link := 'Y';
2876       IF PG_DEBUG in ('Y', 'C') THEN
2877          arp_standard.debug('Get_Tax_Link_Id: ' || ' l_set_uned_link ' || l_set_uned_link);
2878       END IF;
2879    END IF;
2880 
2881   -----------------------------------------------------------------------------
2882   --Set payment link flag to calculate taxable amounts for deferred tax
2883   -----------------------------------------------------------------------------
2884    IF p_process_pay = 'Y' THEN
2885       l_set_pay_link := 'Y';
2886       IF PG_DEBUG in ('Y', 'C') THEN
2887          arp_standard.debug('Get_Tax_Link_Id: ' || 'l_set_pay_link ' || l_set_pay_link);
2888       END IF;
2889    END IF;
2890 
2891   -----------------------------------------------------------------------------
2892   --Set the mirror link counter since seperate links maintained for discounts
2893   --earned and unearned
2894   -----------------------------------------------------------------------------
2895    IF l_set_ed_adj_link = 'Y' AND l_set_uned_link = 'Y' THEN
2896       l_mirror_link_ctr := 'Y';
2897       l_link_ctr1 := l_link_ctr + g_ae_tax_ctr + 1;
2898       IF PG_DEBUG in ('Y', 'C') THEN
2899          arp_standard.debug('Get_Tax_Link_Id: ' || 'l_mirror_link_ctr ' || l_mirror_link_ctr);
2900          arp_standard.debug('Get_Tax_Link_Id: ' || ' l_link_ctr1 ' || l_link_ctr1);
2901       END IF;
2902    END IF;
2903 
2904   -----------------------------------------------------------------------------
2905   --Verify as to whether links require to be created
2906   -----------------------------------------------------------------------------
2907    IF (g_ae_tax_ctr > 0)
2908       AND ((l_set_ed_adj_link = 'Y') OR (l_set_uned_link = 'Y') OR (l_set_pay_link = 'Y'))
2909    THEN
2910   /*-------------------------------------------------------------------------------+
2911    | Link basis for tax code source other than ACTIVITY tax code is off the tax on |
2912    | the Invoice (Tax distribitions on Invoice) so this processing is required.    |
2913    | Set the tax line counts for each tax distribution associated with Invoice line|
2914    +-------------------------------------------------------------------------------*/
2915       OPEN get_tax;
2916 
2917       IF PG_DEBUG in ('Y', 'C') THEN
2918          arp_standard.debug('Get_Tax_Link_Id: ' || 'Opened get_tax Cursor ');
2919       END IF;
2920 
2921       LOOP
2922       --initialize record
2923 
2924         FETCH get_tax BULK COLLECT INTO
2925         inv_line_tbl
2926         LIMIT g_bulk_fetch_rows;
2927 
2928        IF get_tax%NOTFOUND THEN
2929           l_last_fetch := TRUE;
2930        END IF;
2931 
2932        IF (inv_line_tbl.COUNT = 0) AND (l_last_fetch) THEN
2933          IF PG_DEBUG in ('Y', 'C') THEN
2934             arp_standard.debug('Get_Tax_Link_Id: ' || 'COUNT = 0 and LAST FETCH ');
2935          END IF;
2936          EXIT;
2937        END IF;
2938 
2939        FOR i IN inv_line_tbl.FIRST .. inv_line_tbl.LAST LOOP
2940 
2941         --replace with a function
2942 
2943              IF ((l_set_pay_link = 'Y') OR (l_set_ed_adj_link = 'Y')
2944                   OR ((l_set_uned_link = 'Y') AND (l_mirror_link_ctr = 'N'))) THEN
2945                   l_link_ctr := l_link_ctr + 1;
2946              END IF;
2947 
2948              IF ((l_set_uned_link = 'Y') AND (l_mirror_link_ctr = 'Y')) THEN
2949                 l_link_ctr1 := l_link_ctr1 + 1;
2950              END IF;
2951 
2952              IF PG_DEBUG in ('Y', 'C') THEN
2953                 arp_standard.debug('Get_Tax_Link_Id: ' || 'Link Id l_link_ctr  ' || l_link_ctr);
2954                 arp_standard.debug('Get_Tax_Link_Id: ' || 'Link Id l_link_ctr1 ' || l_link_ctr1);
2955                 arp_standard.debug('Get_Tax_Link_Id: ' ||  'inv_line_tbl(' || i || ') = '|| inv_line_tbl(i));
2956              END IF;
2957 
2958              update /*+ INDEX(art3 AR_AE_ALLOC_REC_GT_N4) */
2959                    ar_ae_alloc_rec_gt art3
2960              set art3.ae_tax_link_id        = decode(l_set_pay_link,
2961                                                      'Y', l_link_ctr,
2962                                                      ''),
2963                  art3.ae_tax_link_id_ed_adj = decode(l_set_ed_adj_link,
2964                                                      'Y', l_link_ctr,
2965                                                      art3.ae_tax_link_id_ed_adj),
2966                  art3.ae_tax_link_id_uned   = decode(l_set_uned_link,
2967                                                      'Y', decode(l_mirror_link_ctr,
2968                                                                  'Y', l_link_ctr1,
2969                                                                  l_link_ctr),
2970                                                      art3.ae_tax_link_id_uned),
2971                  art3.ae_counted_flag       = 'Y'
2972              where ae_id = g_id
2973              and art3.ae_account_class = 'TAX'
2974              and art3.ae_counted_flag = 'N'
2975              and art3.ae_link_to_cust_trx_line_id IN
2976              (select to_line
2977              from(
2978              select /*+ INDEX(art1 AR_AE_ALLOC_REC_GT_N4) INDEX(art2 AR_AE_ALLOC_REC_GT_N2) */
2979                     art1.ae_link_to_cust_trx_line_id            from_line      ,
2980                     art2.ae_link_to_cust_trx_line_id            to_line        ,
2981                     max(art1.ae_tax_line_count)                 tax_line_count ,
2982                     1                                           hit_count
2983              from ar_ae_alloc_rec_gt art1,
2984                   ar_ae_alloc_rec_gt art2
2985              where art1.ae_id = g_id
2986              and   art1.ae_account_class            = 'TAX'
2987              and   art1.ae_account_class            = art2.ae_account_class
2988              and   art1.ae_tax_id                   = art2.ae_tax_id
2989              and   art1.ae_tax_type                 = art2.ae_tax_type
2990              and   art1.ae_tax_line_count           = art2.ae_tax_line_count
2991              and   art1.ae_link_to_cust_trx_line_id = inv_line_tbl(i)
2992              --and   art1.ae_link_to_cust_trx_line_id <> art2.ae_link_to_cust_trx_line_id
2993              and   art2.ae_id = art1.ae_id
2994              and   art1.ae_counted_flag = 'N'
2995              and   art2.ae_counted_flag = 'N'
2996              group by art1.ae_link_to_cust_trx_line_id,
2997                       art2.ae_link_to_cust_trx_line_id,
2998                       art1.ae_tax_id,
2999                       art1.ae_tax_type)
3000              group by from_line, to_line
3001              having sum(hit_count) = max(tax_line_count));
3002 
3003              IF SQL%NOTFOUND THEN
3004                 IF ((l_set_pay_link = 'Y') OR (l_set_ed_adj_link = 'Y')
3005                      OR ((l_set_uned_link = 'Y') AND (l_mirror_link_ctr = 'N'))) THEN
3006                      l_link_ctr := l_link_ctr - 1;
3007                 END IF;
3008 
3009                 IF ((l_set_uned_link = 'Y') AND (l_mirror_link_ctr = 'Y')) THEN
3010                    l_link_ctr1 := l_link_ctr1 - 1;
3011                 END IF;
3012 
3013                 IF PG_DEBUG in ('Y', 'C') THEN
3014                    arp_standard.debug('Get_Tax_Link_Id: ' || 'Reversed Link Id l_link_ctr  ' || l_link_ctr);
3015                    arp_standard.debug('Get_Tax_Link_Id: ' || 'Reversed Link Id l_link_ctr1 ' || l_link_ctr1);
3016                 END IF;
3017              END IF;
3018 
3019          END LOOP; --sweep tax lines to formulate link
3020 
3021        --Exit if Last fetch
3022          IF l_last_fetch THEN
3023             EXIT;
3024          END IF;
3025 
3026         END LOOP; --process revenue tax bulk fetch
3027 
3028         CLOSE get_tax;
3029 
3030    END IF; --End if tax code source is ACTIVITY
3031 
3032    IF PG_DEBUG in ('Y', 'C') THEN
3033       arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Tax_Link_Id()-');
3034    END IF;
3035 
3036 EXCEPTION
3037   WHEN NO_DATA_FOUND THEN
3038      IF PG_DEBUG in ('Y', 'C') THEN
3039         arp_standard.debug('Get_Tax_Link_Id: ' || 'SQLERRM ' || SQLERRM);
3040         arp_standard.debug('ARP_ALLOCATION_PKG.Get_Tax_Link_Id - NO_DATA_FOUND' );
3041      END IF;
3042      RAISE;
3043 
3044   WHEN OTHERS THEN
3045      IF PG_DEBUG in ('Y', 'C') THEN
3046         arp_standard.debug('Get_Tax_Link_Id: ' || 'SQLERRM ' || SQLERRM);
3047         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Get_Tax_Link_Id');
3048      END IF;
3049      RAISE;
3050 
3051 END Get_Tax_Link_Id;
3052 
3053 /* ==========================================================================
3054  | PROCEDURE Override_Accounts
3055  |
3056  | DESCRIPTION
3057  |      Sets the Override account flags for earned discounts, adjustments
3058  |      unearned discounts
3059  |
3060  | SCOPE - PRIVATE
3061  |
3062  | PARAMETERS
3063  |      p_app_rec       IN    Earned and Unearned discount details
3064  |      p_adj_rec       IN    Adjustment details
3065  |      p_override1     IN    Override Earned discount/Adjustment account
3066  |      p_override2     IN    Override Unearned discount account
3067  *==========================================================================*/
3068 PROCEDURE Override_Accounts(p_app_rec   IN  ar_receivable_applications%ROWTYPE,
3069                             p_adj_rec   IN  ar_adjustments%ROWTYPE,
3070                             p_override1 OUT NOCOPY  VARCHAR2,
3071                             p_override2 OUT NOCOPY VARCHAR2 ) IS
3072 BEGIN
3073      /*------------------------------------------------------------------------------+
3074       | Set the override flagse for earned discounts/adjustments, unearned discounts |
3075       | as default FALSE                                                             |
3076       +------------------------------------------------------------------------------*/
3077        p_override1 := 'N';
3078        p_override2 := 'N';
3079 
3080      /*------------------------------------------------------------------------------+
3081       | Override revenue accounts for discounts and adjustments                      |
3082       +------------------------------------------------------------------------------*/
3083        IF g_ae_rule_rec.gl_account_source1 IN ('ACTIVITY_GL_ACCOUNT', 'TAX_CODE_ON_INVOICE') THEN
3084 
3085         /*------------------------------------------------------------------------------+
3086          | Override revenue account using first net expense account from tax for earned |
3087          | discounts.                                                                   |
3088          +------------------------------------------------------------------------------*/
3089           IF (g_ae_doc_rec.source_table = 'RA')
3090               AND (((nvl(p_app_rec.line_ediscounted,0) + nvl(p_app_rec.freight_ediscounted,0) +
3091                     nvl(p_app_rec.charges_ediscounted,0)) <> 0)
3092                    OR ((g_ae_rule_rec.tax_code_source1 = 'NONE') AND (nvl(p_app_rec.tax_ediscounted,0) <> 0)))
3093           THEN
3094              p_override1 := 'Y';
3095 
3096         /*------------------------------------------------------------------------------+
3097          | Override revenue account using first net expense account from tax for        |
3098          | adjustments.                                                                 |
3099          +------------------------------------------------------------------------------*/
3100           ELSIF g_ae_doc_rec.source_table = 'ADJ' AND g_ae_doc_rec.document_type = 'ADJUSTMENT'
3101                AND (((nvl(p_adj_rec.line_adjusted,0) + nvl(p_adj_rec.freight_adjusted,0) +
3102                       nvl(p_adj_rec.receivables_charges_adjusted,0)) <> 0)
3103                     OR ((g_ae_rule_rec.tax_code_source1 = 'NONE') AND (nvl(p_adj_rec.tax_adjusted,0) <> 0)))
3104                THEN
3105                    p_override1 := 'Y';
3106 
3107         /*------------------------------------------------------------------------------+
3108          | Override revenue account using first net expense account from tax for        |
3109          | finance charges.                                                             |
3110          +------------------------------------------------------------------------------*/
3111           ELSIF g_ae_doc_rec.source_table = 'ADJ' AND g_ae_doc_rec.document_type = 'FINANCE_CHARGES'
3112                AND (((nvl(p_adj_rec.line_adjusted,0) + nvl(p_adj_rec.freight_adjusted,0) +
3113                       nvl(p_adj_rec.receivables_charges_adjusted,0)) <> 0)
3114                     OR ((g_ae_rule_rec.tax_code_source1 = 'NONE') AND (nvl(p_adj_rec.tax_adjusted,0) <> 0)))
3115                THEN
3116                   p_override1 := 'Y';
3117           END IF;
3118 
3119        END IF; --End if TAX_CODE_ON_INVOICE for earned discounts, adjustments
3120 
3121      /*------------------------------------------------------------------------------+
3122       | Override revenue account using first net expense account from tax for        |
3123       | unearned discounts.                                                          |
3124       +------------------------------------------------------------------------------*/
3125        IF g_ae_rule_rec.gl_account_source2 IN ('ACTIVITY_GL_ACCOUNT', 'TAX_CODE_ON_INVOICE') THEN
3126           IF (g_ae_doc_rec.source_table = 'RA')
3127               AND (((nvl(p_app_rec.line_uediscounted,0) + nvl(p_app_rec.freight_uediscounted,0) +
3128                      nvl(p_app_rec.charges_uediscounted,0)) <> 0)
3129                    OR ((g_ae_rule_rec.tax_code_source2 = 'NONE') AND (nvl(p_app_rec.tax_uediscounted,0) <> 0)))
3130           THEN
3131              p_override2 := 'Y';
3132 
3133           END IF; --override enable condition
3134 
3135         END IF; --tax code on invoice for unearned discounts
3136 
3137       --set the global variables to be used in Build_Rev
3138         g_override1 := p_override1;
3139         g_override2 := p_override2;
3140 
3141 EXCEPTION
3142   WHEN OTHERS THEN
3143      IF PG_DEBUG in ('Y', 'C') THEN
3144         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Override_Accounts ');
3145      END IF;
3146      RAISE;
3147 
3148 END Override_Accounts;
3149 
3150 /* ==========================================================================
3151  | PROCEDURE Process_Amounts
3152  |
3153  | DESCRIPTION
3154  |      Allocates discounts or adjustments over Revenue and Tax distributions
3155  |      based on Rules set up at receivable activity level.
3156  |
3157  | SCOPE - PRIVATE
3158  |
3159  | PARAMETERS
3160  |      p_type_acct          IN  Flag to indicate accounting for Earned
3161  |                               discounts Adjustments or Unearned discounts
3162  *==========================================================================*/
3163 PROCEDURE Process_Amounts(p_app_rec   IN ar_receivable_applications%ROWTYPE,
3164                           p_adj_rec   IN ar_adjustments%ROWTYPE) IS
3165 
3166 BEGIN
3167 
3168   IF PG_DEBUG in ('Y', 'C') THEN
3169      arp_standard.debug( 'ARP_ALLOCATION_PKG.Process_Amounts()+');
3170   END IF;
3171 
3172 /*------------------------------------------------------------------------------+
3173  | Initialise revenue amounts to allocate for discounts or adjustments          |
3174  +------------------------------------------------------------------------------*/
3175 /*
3176   Init_Amts(p_type_acct         => p_type_acct,
3177             p_app_rec           => p_app_rec  ,
3178             p_adj_rec           => p_adj_rec   );
3179 */
3180 --}
3181 
3182 /*------------------------------------------------------------------------------+
3183  | Build gross to activity gl account, for payments where deferred tax exists   |
3184  | this routine is never called and the else block is executed                  |
3185  +------------------------------------------------------------------------------*/
3186  /*
3187   IF (((p_type_acct = 'ED_ADJ') AND (g_ae_rule_rec.gl_account_source1 = 'ACTIVITY_GL_ACCOUNT')
3188         AND (g_ae_rule_rec.tax_code_source1 = 'NONE'))
3189       OR ((p_type_acct = 'UNED') AND (g_ae_rule_rec.gl_account_source2 = 'ACTIVITY_GL_ACCOUNT')
3190            AND (g_ae_rule_rec.tax_code_source2 = 'NONE')))
3191      AND ((NOT g_ae_def_tax) AND (g_done_def_tax)) THEN
3192 
3193       Gross_To_Activity_GL(p_type_acct => p_type_acct);
3194 
3195   ELSE
3196   */
3197   --}
3198    /*---------------------------------------------------------------------------------+
3199     | Allocate discount, adjustment, finance charge amounts over tax. Tax is allocated|
3200     | if there is either an amount or accounted amount tax base in this case if the   |
3201     | tax rule is NONE then only deferred tax treatment is done with tax going to line|
3202     | buckets for allocation otherwise.                                               |
3203     +---------------------------------------------------------------------------------*/
3204    --  Alloc_Rev_Tax_Amt(p_type_acct => p_type_acct) ;
3205    --}
3206     /*------------------------------------------------------------------------------+
3207      | Allocate Tax to Revenue. When processing reaches this point then it means    |
3208      | there exists zero or non zero revenue lines so allocate tax to revenue. For  |
3209      | payments there is no need to allocate tax to revenue as the deferred tax is  |
3210      | only required to be moved.                                                   |
3211      +------------------------------------------------------------------------------*/
3212 /*
3213       IF ((g_ae_tax_ctr > 0)
3214            AND ((g_ae_rule_rec.tax_amt_alloc <> 0)
3215                 OR (g_ae_rule_rec.tax_acctd_amt_alloc <> 0))
3216            AND ((NOT g_bound_tax) AND (NOT g_bound_activity)))
3217          AND (p_type_acct <> 'PAY')
3218       THEN
3219 
3220          Allocate_Tax_To_Rev(p_type_acct   => p_type_acct);
3221 
3222       END IF;
3223 */
3224    /*---------------------------------------------------------------------------------+
3225     | Set actual link ids for Revenue and Tax lines. Where revenue amounts are zero   |
3226     | this routine will not get executed as there is no revenue to link to, so link id|
3227     | for revenue and tax allocations is null.
3228     +---------------------------------------------------------------------------------*/
3229 -- Set_link_Id needs to be executed every time
3230 -- because it allows to TAX line to tie to the Revenue associated
3231 -- in 11i we use a special sequence to tie tax line to the revenue line alone with summarization
3232 -- in 11iX we use the each tax line should be tied individualy to its revnue line related
3233 -- we use the link_to_cust_trx_line_id
3234 -- The tax link line id only concerns activity on tax invoice disctributions
3235 -- In the case of distribution affected to TAX, the actual link to tax id is
3236 -- link_to_cust_trx_line_id if there is a distribution affected to 'REV' with the
3237 -- ref_customer_trx_line_id equals to the link_to_cust_trx_line_id of the tax distribution
3238 -- In the case of disctribution affected to REV, the actual link to tax id is
3239 -- the ref_customer_trx_line_id if there is a link_to_cust_trx_line_id of a tax distribution
3240 -- equals to it
3241 --
3242 --     IF (g_ae_tax_ctr > 0) AND (g_ae_rev_ctr > 0)
3243 --        AND (NOT g_bound_tax) AND (NOT g_bound_activity) AND (p_type_acct <> 'PAY') THEN
3244 
3245       /*---------------------------------------------------------------------------------+
3246        | Set actual link ids for Revenue lines with non zero amounts or accounted amounts|
3247        +---------------------------------------------------------------------------------*/
3248         Set_Rev_Links(p_type_acct => 'ANYTHING');
3249 
3250 --     END IF;
3251 --}
3252    /*------------------------------------------------------------------------------+
3253     | Build Revenue and Tax Lines based on rules                                   |
3254     +------------------------------------------------------------------------------*/
3255      Build_Lines; --(p_type_acct    => p_type_acct );
3256 
3257 --  END IF; --Gross to activity GL Account
3258 
3259 /*------------------------------------------------------------------------------+
3260  | Only for discount and adjustment amounts to be accounted, the rule columns   |
3261  | need to be updated with Rule used.If there is just a payment then the rule   |
3262  | information is not maintained.                                               |
3263  +------------------------------------------------------------------------------*/
3264   IF (((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.earned_discount_taken,0) <> 0))
3265        OR ((g_ae_doc_rec.source_table = 'ADJ') AND (nvl(p_adj_rec.amount,0) <> 0))
3266        OR ((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.unearned_discount_taken,0) <> 0))) THEN
3267 
3268 /*------------------------------------------------------------------------------+
3269  | For payments do not call the document rule routine as this is not required   |
3270  +------------------------------------------------------------------------------*/
3271    -- This is part of avoiding calling process_amount for ED_ADJ and UNED and PAY
3272    IF    ((g_ae_doc_rec.source_table = 'ADJ') AND (nvl(p_adj_rec.amount,0) <> 0))
3273       OR ((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.earned_discount_taken,0) <> 0))
3274    THEN
3275 
3276      Doc_Tax_Acct_Rule(p_type_acct => 'ED_ADJ',
3277                        p_app_rec   => p_app_rec  ,
3278                        p_adj_rec   => p_adj_rec   );
3279    END IF;
3280 
3281    IF    ((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.unearned_discount_taken,0) <> 0))
3282    THEN
3283      Doc_Tax_Acct_Rule(p_type_acct => 'UNED',
3284                        p_app_rec   => p_app_rec  ,
3285                        p_adj_rec   => p_adj_rec   );
3286    END IF;
3287 
3288   END IF;
3289 
3290   IF PG_DEBUG in ('Y', 'C') THEN
3291      arp_standard.debug( 'ARP_ALLOCATION_PKG.Process_Amounts()-');
3292   END IF;
3293 
3294 EXCEPTION
3295   WHEN OTHERS THEN
3296      IF PG_DEBUG in ('Y', 'C') THEN
3297         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Process_Amounts');
3298      END IF;
3299      RAISE;
3300 
3301 END Process_Amounts;
3302 
3303 /* ==========================================================================
3304  | PROCEDURE Doc_Tax_Acct_Rule
3305  |
3306  | DESCRIPTION
3307  |      Updates the discount or adjustment with the accounting Rule used to
3308  |      allocate the line, tax, freight charges to required accounts. This
3309  |      helps in keeping a history of which rule was used as accounts for a
3310  |      receivable activity can change. The rule used and the amount buckets
3311  |      for the discount or adjustment can then be used to determine the nature
3312  |      of the accounting if required.
3313  |
3314  | SCOPE - PRIVATE
3315  |
3316  | PARAMETERS
3317  |      p_type_acct          IN  Flag to indicate accounting for Earned
3318  |                               discounts Adjustments or Unearned discounts
3319  |      p_app_rec            IN  Receivable Application detail record
3320  |      p_adj_rec            IN  Adjustment detail record
3321  *==========================================================================*/
3322 PROCEDURE Doc_Tax_Acct_Rule(p_type_acct IN VARCHAR2                           ,
3323                             p_app_rec   IN ar_receivable_applications%ROWTYPE ,
3324                             p_adj_rec   IN ar_adjustments%ROWTYPE              ) IS
3325 
3326 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
3327 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
3328 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
3329 l_rule_used            VARCHAR2(3);
3330 
3331 BEGIN
3332 
3333   IF PG_DEBUG in ('Y', 'C') THEN
3334      arp_standard.debug( 'ARP_ALLOCATION_PKG.Doc_Tax_Acct_Rule()+');
3335   END IF;
3336 
3337  /*-------------------------------------------------------------------------------+
3338   | Get Rules so that parent discount or adjustment record can be updated with the|
3339   | Rule details.                                                                 |
3340   +-------------------------------------------------------------------------------*/
3341    Get_Rules(p_type_acct            => p_type_acct,
3342              p_gl_account_source    => l_gl_account_source,
3343              p_tax_code_source      => l_tax_code_source,
3344              p_tax_recoverable_flag => l_tax_recoverable_flag);
3345 
3346  /*---------------------------------------------------------------------------------+
3347   | Set up 1 out of 12 possible Rules that could be setup for a receivable activity.|
3348   +--------------------------------------------------------------------------------*/
3349    IF (l_gl_account_source = 'ACTIVITY_GL_ACCOUNT') AND (l_tax_code_source = 'INVOICE')
3350       AND (l_tax_recoverable_flag = 'Y') THEN
3351 
3352       l_rule_used := '31';
3353 
3354    ELSIF (l_gl_account_source = 'ACTIVITY_GL_ACCOUNT') AND (l_tax_code_source = 'INVOICE')
3355          AND (l_tax_recoverable_flag = 'N') THEN
3356 
3357          l_rule_used := '32';
3358 
3359    ELSIF (l_gl_account_source = 'ACTIVITY_GL_ACCOUNT') AND (l_tax_code_source = 'NONE') THEN
3360 
3361           l_rule_used := '33';
3362 
3363    ELSIF (l_gl_account_source = 'ACTIVITY_GL_ACCOUNT') AND (l_tax_code_source = 'ACTIVITY') THEN
3364 
3365          l_rule_used := '34';
3366 
3367    ELSIF (l_gl_account_source = 'TAX_CODE_ON_INVOICE') AND (l_tax_code_source = 'INVOICE')
3368          AND (l_tax_recoverable_flag = 'Y') THEN
3369 
3370          l_rule_used := '21';
3371 
3372    ELSIF (l_gl_account_source = 'TAX_CODE_ON_INVOICE') AND (l_tax_code_source = 'INVOICE')
3373          AND (l_tax_recoverable_flag = 'N') THEN
3374 
3375          l_rule_used := '22';
3376 
3377    ELSIF (l_gl_account_source = 'TAX_CODE_ON_INVOICE') AND (l_tax_code_source = 'NONE') THEN
3378 
3379          l_rule_used := '23';
3380 
3381    ELSIF (l_gl_account_source = 'TAX_CODE_ON_INVOICE') AND (l_tax_code_source = 'ACTIVITY') THEN
3382 
3383          l_rule_used := '24';
3384 
3385    ELSIF (l_gl_account_source = 'REVENUE_ON_INVOICE') AND (l_tax_code_source = 'INVOICE')
3386          AND (l_tax_recoverable_flag = 'Y') THEN
3387 
3388          l_rule_used := '11';
3389 
3390    ELSIF (l_gl_account_source = 'REVENUE_ON_INVOICE') AND (l_tax_code_source = 'INVOICE')
3391          AND (l_tax_recoverable_flag = 'N') THEN
3392 
3393          l_rule_used := '12';
3394 
3395    ELSIF (l_gl_account_source = 'REVENUE_ON_INVOICE') AND (l_tax_code_source = 'NONE') THEN
3396 
3397          l_rule_used := '13';
3398 
3399    ELSIF (l_gl_account_source = 'REVENUE_ON_INVOICE') AND (l_tax_code_source = 'ACTIVITY') THEN
3400 
3401          l_rule_used := '14';
3402 
3403    END IF;
3404 
3405    l_rule_used := l_rule_used || g_ovrrd_code;
3406 
3407    IF PG_DEBUG in ('Y', 'C') THEN
3408       arp_standard.debug('Doc_Tax_Acct_Rule: ' || 'Rule being used being updated ' || l_rule_used);
3409    END IF;
3410 
3411  /*-------------------------------------------------------------------------------+
3412   | Update the correct bucket with Rule used for a discount or an adjustment      |
3413   +-------------------------------------------------------------------------------*/
3414    IF ((g_ae_doc_rec.source_table = 'RA') AND (p_type_acct = 'ED_ADJ')) THEN
3415 
3416       UPDATE ar_receivable_applications
3417       SET    edisc_tax_acct_rule = l_rule_used
3418       WHERE  receivable_application_id = g_ae_doc_rec.source_id;
3419 
3420    ELSIF ((g_ae_doc_rec.source_table = 'ADJ') AND (p_type_acct = 'ED_ADJ')) THEN
3421          /* Bug 5659539 - When ccid is overriden while creating adjustment thru API,
3422           overriden ccid should be considered, but not from activities setup(adj_code_combination_id)
3423 	This is done by checking if created_from is ADJ_API or not in update stmt. */
3424 
3425          UPDATE ar_adjustments
3426          SET    adj_tax_acct_rule = l_rule_used,
3427                 -- code_combination_id = nvl(adj_code_combination_id, code_combination_id)
3428 		code_combination_id = DECODE(created_from,
3429                                              'ADJ_API', nvl(code_combination_id, adj_code_combination_id),
3430                                              nvl(adj_code_combination_id, code_combination_id)
3431                                             )
3432          WHERE  adjustment_id = g_ae_doc_rec.source_id;
3433 
3434    ELSIF ((g_ae_doc_rec.source_table = 'RA') AND (p_type_acct = 'UNED')) THEN
3435 
3436          UPDATE ar_receivable_applications
3437          SET    unedisc_tax_acct_rule = l_rule_used
3438          WHERE  receivable_application_id = g_ae_doc_rec.source_id;
3439 
3440    END IF;
3441 
3442    IF PG_DEBUG in ('Y', 'C') THEN
3443       arp_standard.debug( 'ARP_ALLOCATION_PKG.Doc_Tax_Acct_Rule()-');
3444    END IF;
3445 
3446 EXCEPTION
3447   WHEN NO_DATA_FOUND THEN
3448      IF PG_DEBUG in ('Y', 'C') THEN
3449         arp_standard.debug('ARP_ALLOCATION_PKG.Doc_Tax_Acct_Rule- NO_DATA_FOUND' );
3450      END IF;
3451      RAISE;
3452 
3453   WHEN OTHERS THEN
3454      IF PG_DEBUG in ('Y', 'C') THEN
3455         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Doc_Tax_Acct_Rule');
3456      END IF;
3457      RAISE;
3458 
3459 END Doc_Tax_Acct_Rule;
3460 
3461 /* ==========================================================================
3462  | PROCEDURE Init_Amts
3463  |
3464  | DESCRIPTION
3465  |      Sets amounts and accounted amount base for line, tax, freight,charges
3466  |      from buckets of the Receivable application or adjustment.
3467  |
3468  | SCOPE - PRIVATE
3469  |
3470  | PARAMETERS
3471  |      p_type_acct          IN  Flag to indicate accounting for Earned
3472  |                               discounts Adjustments or Unearned discounts
3473  *==========================================================================*/
3474 PROCEDURE Init_Amts( p_type_acct    IN VARCHAR2                       ,
3475                      p_app_rec      IN ar_receivable_applications%ROWTYPE,
3476                      p_adj_rec      IN ar_adjustments%ROWTYPE ) IS
3477 
3478 CURSOR chk_tax_chrg(p_cust_id IN NUMBER) IS
3479        select 1
3480        from dual
3481        where exists(select 'x'
3482                     from ar_adjustments
3483                     where type = 'CHARGES'
3484                     and nvl(tax_adjusted,0) <> 0
3485                     and status = 'A'
3486                     and customer_trx_id = p_cust_id);
3487 
3488 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
3489 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
3490 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
3491 l_customer_trx_id NUMBER;
3492 
3493 BEGIN
3494 
3495   IF PG_DEBUG in ('Y', 'C') THEN
3496      arp_standard.debug( 'ARP_ALLOCATION_PKG.Init_Amts()+');
3497   END IF;
3498 
3499 /*---------------------------------------------------------------------------+
3500  | When called from is the Wrapper routine for Bills Receivable, then the    |
3501  | initialization is not required to be done. This is because the bucket     |
3502  | amounts are set by the Wrapper routine                                    |
3503  +---------------------------------------------------------------------------*/
3504     IF (nvl(g_ae_doc_rec.called_from,'NONE') <> 'WRAPPER') THEN
3505 
3506        g_ae_rule_rec.line_amt_alloc            := 0;
3507        g_ae_rule_rec.line_acctd_amt_alloc      := 0;
3508        g_ae_rule_rec.tax_amt_alloc             := 0;
3509        g_ae_rule_rec.tax_acctd_amt_alloc       := 0;
3510        g_ae_rule_rec.freight_amt_alloc         := 0;
3511        g_ae_rule_rec.freight_acctd_amt_alloc   := 0;
3512        g_ae_rule_rec.charges_amt_alloc         := 0;
3513        g_ae_rule_rec.charges_acctd_amt_alloc   := 0;
3514 
3515     END IF;
3516 
3517   g_orig_line_amt_alloc := 0;
3518   g_orig_line_acctd_amt_alloc := 0;
3519 
3520   g_bound_tax      := FALSE;
3521   g_bound_activity := FALSE;
3522   g_added_tax      := FALSE;
3523   g_ovrrd_code     := '';
3524 
3525  /*-------------------------------------------------------------------------------+
3526   | Get Rules so that parent discount or adjustment record can be updated with the|
3527   | Rule details.                                                                 |
3528   +-------------------------------------------------------------------------------*/
3529    Get_Rules(p_type_acct            => p_type_acct,
3530              p_gl_account_source    => l_gl_account_source,
3531              p_tax_code_source      => l_tax_code_source,
3532              p_tax_recoverable_flag => l_tax_recoverable_flag);
3533 
3534 /*------------------------------------------------------------------------------+
3535  | Set amounts and accounted amounts to allocate for Earned discounts           |
3536  +------------------------------------------------------------------------------*/
3537 
3538   IF ((p_type_acct = 'ED_ADJ') AND (g_ae_doc_rec.source_table = 'RA')
3539       AND (nvl(p_app_rec.earned_discount_taken,0) <> 0)) THEN
3540 
3541      g_ae_rule_rec.tax_amt_alloc       := nvl(p_app_rec.tax_ediscounted,0)     * -1;
3542      g_ae_rule_rec.charges_amt_alloc   := nvl(p_app_rec.charges_ediscounted,0) * -1;
3543      g_ae_rule_rec.line_amt_alloc      := nvl(p_app_rec.line_ediscounted,0)    * -1;
3544      g_ae_rule_rec.freight_amt_alloc   := nvl(p_app_rec.freight_ediscounted,0) * -1;
3545 
3546     arp_util.Set_Buckets(
3547                   p_header_acctd_amt   => nvl(p_app_rec.acctd_earned_discount_taken,0) * -1 ,
3548                   p_base_currency      => g_ae_sys_rec.base_currency                        ,
3549                   p_exchange_rate      => g_cust_inv_rec.exchange_rate                      ,
3550                   p_base_precision     => g_ae_sys_rec.base_precision                       ,
3551                   p_base_min_acc_unit  => g_ae_sys_rec.base_min_acc_unit                    ,
3552                   p_tax_amt            => g_ae_rule_rec.tax_amt_alloc                       ,
3553                   p_charges_amt        => g_ae_rule_rec.charges_amt_alloc                   ,
3554                   p_line_amt           => g_ae_rule_rec.line_amt_alloc                      ,
3555                   p_freight_amt        => g_ae_rule_rec.freight_amt_alloc                   ,
3556                   p_tax_acctd_amt      => g_ae_rule_rec.tax_acctd_amt_alloc                 ,
3557                   p_charges_acctd_amt  => g_ae_rule_rec.charges_acctd_amt_alloc             ,
3558                   p_line_acctd_amt     => g_ae_rule_rec.line_acctd_amt_alloc                ,
3559                   p_freight_acctd_amt  => g_ae_rule_rec.freight_acctd_amt_alloc               );
3560 
3561      Dump_Init_Amts(p_type_acct => p_type_acct,
3562                     p_app_rec   => p_app_rec  ,
3563                     p_adj_rec   => p_adj_rec   );
3564 
3565   --Check Accounted Amounts for reconciliation with header accounted amount
3566      IF ((g_ae_rule_rec.line_acctd_amt_alloc  +
3567            g_ae_rule_rec.tax_acctd_amt_alloc     +
3568              g_ae_rule_rec.freight_acctd_amt_alloc  +
3569                 g_ae_rule_rec.charges_acctd_amt_alloc) <> (nvl(p_app_rec.acctd_earned_discount_taken,0) * -1))
3570      THEN
3571 
3572         RAISE rounding_error;
3573 
3574      END IF;
3575 
3576 /*-----------------------------------------------------------------------------------+
3577  | Set amounts and accounted amounts to allocate for Adjustments and Finance charges.|
3578  +-----------------------------------------------------------------------------------*/
3579   ELSIF ((p_type_acct = 'ED_ADJ') AND (g_ae_doc_rec.source_table = 'ADJ')
3580           AND (nvl(p_adj_rec.amount,0) <> 0))  THEN
3581 
3582 /*---------------------------------------------------------------------------+
3583  | When called from the Wrapper routine for Bills Receivable, the            |
3584  | bucket amounts are set by the Wrapper, so its not necessary to set it here|
3585  +---------------------------------------------------------------------------*/
3586      IF (nvl(g_ae_doc_rec.called_from,'NONE') <> 'WRAPPER') THEN
3587 
3588         g_ae_rule_rec.tax_amt_alloc       := nvl(p_adj_rec.tax_adjusted,0);
3589         g_ae_rule_rec.charges_amt_alloc   := nvl(p_adj_rec.receivables_charges_adjusted,0);
3590         g_ae_rule_rec.line_amt_alloc      := nvl(p_adj_rec.line_adjusted,0);
3591         g_ae_rule_rec.freight_amt_alloc   := nvl(p_adj_rec.freight_adjusted,0);
3592 
3593         arp_util.Set_Buckets(
3594                      p_header_acctd_amt   => nvl(p_adj_rec.acctd_amount,0)                     ,
3595                      p_base_currency      => g_ae_sys_rec.base_currency                        ,
3596                      p_exchange_rate      => g_cust_inv_rec.exchange_rate                      ,
3597                      p_base_precision     => g_ae_sys_rec.base_precision                       ,
3598                      p_base_min_acc_unit  => g_ae_sys_rec.base_min_acc_unit                    ,
3599                      p_tax_amt            => g_ae_rule_rec.tax_amt_alloc                       ,
3600                      p_charges_amt        => g_ae_rule_rec.charges_amt_alloc                   ,
3601                      p_line_amt           => g_ae_rule_rec.line_amt_alloc                      ,
3602                      p_freight_amt        => g_ae_rule_rec.freight_amt_alloc                   ,
3603                      p_tax_acctd_amt      => g_ae_rule_rec.tax_acctd_amt_alloc                 ,
3604                      p_charges_acctd_amt  => g_ae_rule_rec.charges_acctd_amt_alloc             ,
3605                      p_line_acctd_amt     => g_ae_rule_rec.line_acctd_amt_alloc                ,
3606                      p_freight_acctd_amt  => g_ae_rule_rec.freight_acctd_amt_alloc               );
3607      END IF;
3608 
3609      Dump_Init_Amts(p_type_acct => p_type_acct,
3610                     p_app_rec   => p_app_rec  ,
3611                     p_adj_rec   => p_adj_rec   );
3612 
3613   --Check Accounted Amounts for reconciliation with header accounted amount
3614      IF ((g_ae_rule_rec.line_acctd_amt_alloc    +
3615              g_ae_rule_rec.tax_acctd_amt_alloc     +
3616                 g_ae_rule_rec.freight_acctd_amt_alloc  +
3617                   g_ae_rule_rec.charges_acctd_amt_alloc) <> nvl(p_adj_rec.acctd_amount,0))
3618      THEN
3619 
3620         RAISE rounding_error;
3621 
3622      END IF;
3623 
3624 /*------------------------------------------------------------------------------+
3625  | Set amounts and accounted amounts to allocate for Earned discounts           |
3626  +------------------------------------------------------------------------------*/
3627   ELSIF ((p_type_acct = 'UNED') AND (g_ae_doc_rec.source_table = 'RA')
3628           AND (nvl(p_app_rec.unearned_discount_taken,0) <> 0)) THEN
3629 
3630      g_ae_rule_rec.tax_amt_alloc       := nvl(p_app_rec.tax_uediscounted,0)     * -1;
3631      g_ae_rule_rec.charges_amt_alloc   := nvl(p_app_rec.charges_uediscounted,0) * -1;
3632      g_ae_rule_rec.line_amt_alloc      := nvl(p_app_rec.line_uediscounted,0)    * -1;
3633      g_ae_rule_rec.freight_amt_alloc   := nvl(p_app_rec.freight_uediscounted,0) * -1;
3634 
3635      arp_util.Set_Buckets(
3636                   p_header_acctd_amt   => nvl(p_app_rec.acctd_unearned_discount_taken,0) * -1 ,
3637                   p_base_currency      => g_ae_sys_rec.base_currency                          ,
3638                   p_exchange_rate      => g_cust_inv_rec.exchange_rate                        ,
3639                   p_base_precision     => g_ae_sys_rec.base_precision                         ,
3640                   p_base_min_acc_unit  => g_ae_sys_rec.base_min_acc_unit                      ,
3641                   p_tax_amt            => g_ae_rule_rec.tax_amt_alloc                         ,
3642                   p_charges_amt        => g_ae_rule_rec.charges_amt_alloc                     ,
3643                   p_line_amt           => g_ae_rule_rec.line_amt_alloc                        ,
3644                   p_freight_amt        => g_ae_rule_rec.freight_amt_alloc                     ,
3645                   p_tax_acctd_amt      => g_ae_rule_rec.tax_acctd_amt_alloc                   ,
3646                   p_charges_acctd_amt  => g_ae_rule_rec.charges_acctd_amt_alloc               ,
3647                   p_line_acctd_amt     => g_ae_rule_rec.line_acctd_amt_alloc                  ,
3648                   p_freight_acctd_amt  => g_ae_rule_rec.freight_acctd_amt_alloc                 );
3649 
3650      Dump_Init_Amts(p_type_acct => p_type_acct,
3651                     p_app_rec   => p_app_rec  ,
3652                     p_adj_rec   => p_adj_rec   );
3653 
3654   --Check Accounted Amounts for reconciliation with header accounted amount
3655      IF ((g_ae_rule_rec.line_acctd_amt_alloc  +
3656             g_ae_rule_rec.tax_acctd_amt_alloc    +
3657               g_ae_rule_rec.freight_acctd_amt_alloc +
3658                 g_ae_rule_rec.charges_acctd_amt_alloc) <> nvl(p_app_rec.acctd_unearned_discount_taken,0) * -1)
3659      THEN
3660 
3661         RAISE rounding_error;
3662 
3663      END IF;
3664 /*------------------------------------------------------------------------------+
3665  | Set amounts and accounted amounts to allocate for Payments for deferred tax  |
3666  +------------------------------------------------------------------------------*/
3667   ELSIF (((p_type_acct = 'PAY') AND (g_ae_def_tax) AND (NOT g_done_def_tax))
3668            AND (nvl(p_app_rec.amount_applied,0) <> 0)) THEN
3669   /*---------------------------------------------------------------------------+
3670    | Only the tax applied is used to move deferred tax from interim to         |
3671    | collected tax account. When called from the Wrapper routine for Bills     |
3672    | Receivable, the bucket amounts are set by the Wrapper, so its not         |
3673    | necessary to set it here                                                  |
3674    +---------------------------------------------------------------------------*/
3675      IF (nvl(g_ae_doc_rec.called_from,'NONE') <> 'WRAPPER') THEN
3676 
3677         g_ae_rule_rec.tax_amt_alloc         := nvl(p_app_rec.tax_applied,0)                 * -1;
3678         g_ae_rule_rec.charges_amt_alloc     := nvl(p_app_rec.receivables_charges_applied,0) * -1;
3679         g_ae_rule_rec.line_amt_alloc        := nvl(p_app_rec.line_applied,0)                * -1;
3680         g_ae_rule_rec.freight_amt_alloc     := nvl(p_app_rec.freight_applied,0)             * -1;
3681 
3682          arp_util.Set_Buckets(
3683                        p_header_acctd_amt   => nvl(p_app_rec.acctd_amount_applied_to,0)       * -1 ,
3684                        p_base_currency      => g_ae_sys_rec.base_currency                          ,
3685                        p_exchange_rate      => g_cust_inv_rec.exchange_rate                        ,
3686                        p_base_precision     => g_ae_sys_rec.base_precision                         ,
3687                        p_base_min_acc_unit  => g_ae_sys_rec.base_min_acc_unit                      ,
3688                        p_tax_amt            => g_ae_rule_rec.tax_amt_alloc                         ,
3689                        p_charges_amt        => g_ae_rule_rec.charges_amt_alloc                     ,
3690                        p_line_amt           => g_ae_rule_rec.line_amt_alloc                        ,
3691                        p_freight_amt        => g_ae_rule_rec.freight_amt_alloc                     ,
3692                        p_tax_acctd_amt      => g_ae_rule_rec.tax_acctd_amt_alloc                   ,
3693                        p_charges_acctd_amt  => g_ae_rule_rec.charges_acctd_amt_alloc               ,
3694                        p_line_acctd_amt     => g_ae_rule_rec.line_acctd_amt_alloc                  ,
3695                        p_freight_acctd_amt  => g_ae_rule_rec.freight_acctd_amt_alloc                 );
3696       END IF;
3697 
3698      Dump_Init_Amts(p_type_acct => p_type_acct,
3699                     p_app_rec   => p_app_rec  ,
3700                     p_adj_rec   => p_adj_rec   );
3701 
3702   --Check Accounted Amounts for reconciliation with header accounted amount
3703      IF ((g_ae_rule_rec.line_acctd_amt_alloc   +
3704             g_ae_rule_rec.tax_acctd_amt_alloc   +
3705               g_ae_rule_rec.freight_acctd_amt_alloc   +
3706                 g_ae_rule_rec.charges_acctd_amt_alloc  )       <> nvl(p_app_rec.acctd_amount_applied_to,0) * -1)
3707      THEN
3708 
3709         RAISE rounding_error;
3710 
3711      END IF;
3712 
3713   END IF;
3714 
3715   IF PG_DEBUG in ('Y', 'C') THEN
3716      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.line_amt_alloc ' || g_ae_rule_rec.line_amt_alloc);
3717      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.line_acctd_amt_alloc ' || g_ae_rule_rec.line_acctd_amt_alloc);
3718      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.tax_amt_alloc ' || g_ae_rule_rec.tax_amt_alloc);
3719      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.tax_acctd_amt_alloc ' || g_ae_rule_rec.tax_acctd_amt_alloc);
3720      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.freight_amt_alloc ' || g_ae_rule_rec.freight_amt_alloc);
3721      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.freight_acctd_amt_alloc ' || g_ae_rule_rec.freight_acctd_amt_alloc);
3722      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.charges_amt_alloc ' || g_ae_rule_rec.charges_amt_alloc);
3723      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.charges_acctd_amt_alloc ' || g_ae_rule_rec.charges_acctd_amt_alloc);
3724   END IF;
3725 
3726 /*------------------------------------------------------------------------------+
3727  | If atleast one charges adjustment exists then add the charge to the line     |
3728  | it as the taxable amount.                                                    |
3729  +------------------------------------------------------------------------------*/
3730  --Get the customer trx id
3731    IF g_ae_doc_rec.source_table = 'RA' THEN
3732       l_customer_trx_id := p_app_rec.applied_customer_trx_id;
3733    ELSE
3734       l_customer_trx_id := p_adj_rec.customer_trx_id;
3735    END IF;
3736 
3737  --Check whether atleast one charges adjustment with tax exists
3738    FOR l_chk_tax_chrg IN chk_tax_chrg(p_cust_id => l_customer_trx_id) LOOP
3739 
3740     --add the charge to line to have a common taxable basis
3741        g_ae_rule_rec.line_amt_alloc := g_ae_rule_rec.line_amt_alloc
3742                                            + g_ae_rule_rec.charges_amt_alloc;
3743 
3744        g_ae_rule_rec.line_acctd_amt_alloc := g_ae_rule_rec.line_acctd_amt_alloc
3745                                                + g_ae_rule_rec.charges_acctd_amt_alloc;
3746 
3747     --set the charge to zero
3748        g_ae_rule_rec.charges_amt_alloc := 0;
3749 
3750        g_ae_rule_rec.charges_acctd_amt_alloc := 0;
3751 
3752       IF PG_DEBUG in ('Y', 'C') THEN
3753          arp_standard.debug('Init_Amts: ' || 'Tax inclusive charges adjustments exist - charge added to line');
3754       END IF;
3755 
3756    END LOOP; --tax inclusive charges
3757 
3758    g_orig_line_amt_alloc := g_ae_rule_rec.line_amt_alloc;
3759    g_orig_line_acctd_amt_alloc := g_ae_rule_rec.line_acctd_amt_alloc;
3760 
3761 /*------------------------------------------------------------------------------+
3762  | Boundary conditions in this case we cant allocate the tax amount or accounted|
3763  | amount over the original tax on Invoice and then add to the revenue for that |
3764  | tax due to TAX_CODE_SOURCE = NONE, hence tax is added to line and allocated. |
3765  | Boundary condition flag is set. Note in this case we do not override the rule|
3766  | When the boundary flag is set the tax link id will be null and taxable for   |
3767  | tax allocations will be derived from the original Invoice                    |
3768  +------------------------------------------------------------------------------*/
3769   IF (((g_ae_rule_rec.tax_amt_alloc <> 0) AND (g_ae_rule_rec.tax_amt = 0))
3770         OR ((g_ae_rule_rec.tax_acctd_amt_alloc <> 0) AND (g_ae_rule_rec.tax_acctd_amt = 0))) THEN
3771 
3772      g_bound_tax := TRUE; --applicable to ACTIVITY also hence set here
3773 
3774      IF (l_tax_code_source IN ('NONE', 'INVOICE')) THEN
3775 
3776          IF (((p_type_acct = 'ED_ADJ') OR (p_type_acct = 'PAY')) AND (l_tax_code_source =  'INVOICE')) THEN
3777 
3778          /* Override the rule may want to use this in the future
3779             g_ae_rule_rec.tax_code_source1   := 'NONE';          */
3780 
3781            g_ovrrd_code := '1';
3782 
3783            IF (p_type_acct = 'PAY') THEN
3784               g_ae_rule_rec.tax_code_source1      := 'NONE';
3785               g_ae_rule_rec.tax_recoverable_flag1 := '';
3786            ELSE
3787               RAISE invalid_allocation_base;
3788            END IF;
3789 
3790          ELSIF ((p_type_acct = 'UNED') AND (l_tax_code_source =  'INVOICE')) THEN
3791 
3792          /* Override the rule may want to use this in the future
3793             g_ae_rule_rec.tax_code_source2   := 'NONE';          */
3794 
3795            g_ovrrd_code := '1'; --indicates rules overriden
3796 
3797            RAISE invalid_allocation_base;
3798 
3799          END IF; --end if type of account and tax code source is Invoice
3800 
3801          g_ae_rule_rec.line_amt_alloc       := g_ae_rule_rec.line_amt_alloc
3802                                                                + g_ae_rule_rec.tax_amt_alloc;
3803          g_ae_rule_rec.line_acctd_amt_alloc := g_ae_rule_rec.line_acctd_amt_alloc
3804                                                                + g_ae_rule_rec.tax_acctd_amt_alloc;
3805          g_added_tax := TRUE;
3806 
3807          IF (g_ovrrd_code IS NULL) THEN
3808             g_ovrrd_code := '2'; --indicates tax added to line
3809          END IF;
3810 
3811      END IF; --end if tax code source is Invoice or None
3812 
3813   END IF; --end if tax base is 0 on original Invoice
3814 
3815 
3816 /*-----------------------------------------------------------------------------------+
3817  | Boundary conditions for activity gl account - gl account source in this case we do|
3818  | not override the rule we just set the boundary flag the tax code source rule will |
3819  | be used as is unless overriden by one of the above conditions. In such a case the |
3820  | link id for tax will be null, and taxable derived from original Invoice.          |
3821  +-----------------------------------------------------------------------------------*/
3822   IF ((((g_ae_rule_rec.line_amt_alloc + g_ae_rule_rec.freight_amt_alloc + g_ae_rule_rec.charges_amt_alloc) <> 0)
3823        AND (g_ae_rule_rec.revenue_amt = 0))
3824         OR (((g_ae_rule_rec.line_acctd_amt_alloc + g_ae_rule_rec.freight_acctd_amt_alloc + g_ae_rule_rec.charges_acctd_amt_alloc) <> 0)
3825              AND (g_ae_rule_rec.revenue_acctd_amt = 0))) THEN
3826 
3827      g_bound_activity := TRUE; --set boundary condition for activity
3828 
3829   /*-----------------------------------------------------------------------------------+
3830    |Add the tax to line bucket so that allocation can be done in the case the procedure|
3831    |Allocate_Tax_To_Rev will not be used as the revenue base on original Invoice is 0  |
3832    +-----------------------------------------------------------------------------------*/
3833      IF ((l_tax_code_source = 'NONE') AND (NOT g_bound_tax)) THEN
3834 
3835         g_ae_rule_rec.line_amt_alloc       := g_ae_rule_rec.line_amt_alloc + g_ae_rule_rec.tax_amt_alloc;
3836         g_ae_rule_rec.line_acctd_amt_alloc := g_ae_rule_rec.line_acctd_amt_alloc + g_ae_rule_rec.tax_acctd_amt_alloc;
3837         g_added_tax := TRUE;
3838 
3839         IF (g_ovrrd_code IS NULL) THEN
3840            g_ovrrd_code := '2';  --indicates tax added to line
3841         END IF;
3842 
3843      END IF; --end if tax code source is NONE
3844 
3845   /*-------------------------------------------------------------------------------------+
3846    |Override the rule for gl account source as line amount or accounted amount cannot be |
3847    |allocated uniformly over revenue amount or accounted amount base on original Invoice.|
3848    +-------------------------------------------------------------------------------------*/
3849      IF (l_gl_account_source IN ('REVENUE_ON_INVOICE', 'TAX_CODE_ON_INVOICE')) THEN
3850 
3851         IF ((p_type_acct = 'ED_ADJ') OR (p_type_acct = 'PAY')) THEN
3852 
3853            /* override the rule on GL Account Source kept for future usage most logical setting
3854               g_ae_rule_rec.gl_account_source1 := 'ACTIVITY_GL_ACCOUNT'; */
3855 
3856            RAISE invalid_allocation_base;
3857 
3858         ELSIF (p_type_acct = 'UNED') THEN
3859 
3860            /* override the rule on GL Account Source kept for future usage most logical setting
3861               g_ae_rule_rec.gl_account_source2 := 'ACTIVITY_GL_ACCOUNT'; */
3862 
3863            RAISE invalid_allocation_base;
3864 
3865         END IF;
3866 
3867         IF (g_ovrrd_code = '1') THEN
3868             null; --dont set code again
3869         ELSIF (g_ovrrd_code = '2') THEN
3870             g_ovrrd_code := 3; --indicates tax added to line and gl account source rule overriden
3871         ELSIF g_ovrrd_code IS NULL THEN
3872             g_ovrrd_code := 4; --indicates gl account source overriden
3873         END IF;
3874 
3875      END IF; --end if revenue on invoice or tax code on invoice
3876 
3877   END IF; --end if revenue amount or accounted amount base is 0
3878 
3879 
3880   IF PG_DEBUG in ('Y', 'C') THEN
3881      arp_standard.debug( 'ARP_ALLOCATION_PKG.Init_Amts()-');
3882   END IF;
3883 
3884 EXCEPTION
3885   WHEN rounding_error THEN
3886        IF PG_DEBUG in ('Y', 'C') THEN
3887           arp_standard.debug('Rounding error: ARP_ALLOCATION_PKG.Init_Amts');
3888        END IF;
3889        fnd_message.set_name('AR','AR_ROUNDING_ERROR');
3890        fnd_message.set_token('ROUTINE','ARP_ALLOCATION_PKG.INIT_AMTS');
3891        RAISE;
3892 
3893   WHEN OTHERS THEN
3894      IF PG_DEBUG in ('Y', 'C') THEN
3895         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Init_Amts');
3896      END IF;
3897      RAISE;
3898 
3899 END Init_Amts;
3900 
3901 /* ==========================================================================
3902  | PROCEDURE Gross_To_Activity_GL
3903  |
3904  | DESCRIPTION
3905  |      This routine creates the standard Gross to Activity GL Account
3906  |      accounting entry.
3907  |
3908  | SCOPE - PRIVATE
3909  |
3910  | PARAMETERS
3911  |      NONE
3912  *==========================================================================*/
3913 PROCEDURE Gross_To_Activity_GL(p_type_acct      IN VARCHAR2) IS
3914 
3915 l_ae_line_init_rec     ar_ae_alloc_rec_gt%ROWTYPE;
3916 
3917 BEGIN
3918 
3919   IF PG_DEBUG in ('Y', 'C') THEN
3920      arp_standard.debug('ARP_ALLOCATION_PKG.Gross_To_Activity_GL()+');
3921   END IF;
3922 
3923  /*----------------------------------------------------------------------------+
3924   | Assign Currency Exchange rate information to initialisation record         |
3925   +----------------------------------------------------------------------------*/
3926    l_ae_line_init_rec.ae_source_id                 := g_ae_doc_rec.source_id               ;
3927    l_ae_line_init_rec.ae_source_table              := g_ae_doc_rec.source_table            ;
3928    l_ae_line_init_rec.ae_currency_code             := g_cust_inv_rec.invoice_currency_code ;
3929    l_ae_line_init_rec.ae_currency_conversion_rate  := g_cust_inv_rec.exchange_rate         ;
3930    l_ae_line_init_rec.ae_currency_conversion_type  := g_cust_inv_rec.exchange_rate_type    ;
3931    l_ae_line_init_rec.ae_currency_conversion_date  := g_cust_inv_rec.exchange_date         ;
3932 
3933    IF (g_cust_inv_rec.drawee_site_use_id IS NOT NULL) THEN --if Bill
3934       l_ae_line_init_rec.ae_third_party_id            := g_cust_inv_rec.drawee_id;
3935       l_ae_line_init_rec.ae_third_party_sub_id        := g_cust_inv_rec.drawee_site_use_id  ;
3936    ELSE
3937       l_ae_line_init_rec.ae_third_party_id            := g_cust_inv_rec.bill_to_customer_id   ;
3938       l_ae_line_init_rec.ae_third_party_sub_id        := g_cust_inv_rec.bill_to_site_use_id   ;
3939    END IF;
3940 
3941  /*----------------------------------------------------------------------------+
3942   | Build accounting for any Charges and Freight amounts to activity GL account|
3943   +----------------------------------------------------------------------------*/
3944    Build_Charges_Freight_All(p_type_acct        => p_type_acct       ,
3945                              p_ae_line_init_rec => l_ae_line_init_rec,
3946                              p_build_all        => TRUE   );
3947 
3948    IF PG_DEBUG in ('Y', 'C') THEN
3949       arp_standard.debug( 'ARP_ALLOCATION_PKG.Gross_To_Activity_GL()-');
3950    END IF;
3951 
3952 EXCEPTION
3953   WHEN OTHERS THEN
3954      IF PG_DEBUG in ('Y', 'C') THEN
3955         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Gross_To_Activity_GL');
3956      END IF;
3957      RAISE;
3958 
3959 END Gross_To_Activity_GL;
3960 
3961 /* ==========================================================================
3962  | PROCEDURE Init_Rev_Tax_Tab
3963  |
3964  | DESCRIPTION
3965  |      Initialise cells in tables which are used for calculations, this is
3966  |      required because an application may require allocation of discounts
3967  |      for earned discounts and then for unearned discounts
3968  |
3969  | SCOPE - PRIVATE
3970  |
3971  | PARAMETERS
3972  |      NONE
3973  *==========================================================================*/
3974 PROCEDURE Init_Rev_Tax_Tab IS
3975 
3976 l_ctr                 BINARY_INTEGER;
3977 
3978 BEGIN
3979   arp_standard.debug('ARP_ALLOCATION_PKG.Init_Rev_Tax_Tab()+');
3980 
3981 /*------------------------------------------------------------------------------+
3982  | Set the prorate amount and summarized flags to null because they may have    |
3983  | been set for earned discounts.                                               |
3984  +------------------------------------------------------------------------------*/
3985 
3986   UPDATE /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3)*/
3987         ar_ae_alloc_rec_gt
3988   SET ae_pro_amt                     = 0  ,
3989       ae_pro_acctd_amt               = 0  ,
3990       ae_pro_frt_chrg_amt            = 0  ,
3991       ae_pro_frt_chrg_acctd_amt      = 0  ,
3992       ae_pro_taxable_amt             = 0  ,
3993       ae_pro_taxable_acctd_amt       = 0  ,
3994       ae_pro_split_taxable_amt       = '' ,
3995       ae_pro_split_taxable_acctd_amt = '' ,
3996       ae_pro_recov_taxable_amt       = '' ,
3997       ae_pro_recov_taxable_acctd_amt = '' ,
3998       ae_pro_def_tax_amt             = 0  ,
3999       ae_pro_def_tax_acctd_amt       = 0  ,
4000       ae_sum_alloc_amt               = 0  ,
4001       ae_sum_alloc_acctd_amt         = 0  ,
4002       ae_tax_link_id_act             = '' ,
4003       ae_counted_flag                = 'N'
4004    WHERE ae_id = g_id;
4005 
4006   arp_standard.debug( 'ARP_ALLOCATION_PKG.Init_Rev_Tax_Tab()-');
4007 
4008 EXCEPTION
4009   WHEN OTHERS THEN
4010      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Init_Rev_Tax_Tab');
4011      RAISE;
4012 
4013 END Init_Rev_Tax_Tab;
4014 
4015 /* ==========================================================================
4016  | FUNCTION Get_Acctd_Amt
4017  |
4018  | DESCRIPTION
4019  |      Returns accounted amount using invoice exchange rate converting to
4020  |      base currency.
4021  |
4022  | SCOPE - PRIVATE
4023  |
4024  | PARAMETERS
4025  |      p_amount             IN  Amount which needs to be converted into
4026  |                               base or functional currency accounted amount
4027  *==========================================================================*/
4028 FUNCTION  Get_Acctd_Amt(p_amount       IN NUMBER ) RETURN NUMBER IS
4029 
4030 l_acctd_amount NUMBER;
4031 
4032 BEGIN
4033 
4034   IF PG_DEBUG in ('Y', 'C') THEN
4035      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Acctd_Amt()-');
4036   END IF;
4037 
4038   l_acctd_amount := arpcurr.functional_amount(p_amount                                   ,
4039                                               g_ae_sys_rec.base_currency                 ,
4040                                               g_cust_inv_rec.exchange_rate               ,
4041                                               g_ae_sys_rec.base_precision                ,
4042                                               g_ae_sys_rec.base_min_acc_unit               );
4043 
4044   IF PG_DEBUG in ('Y', 'C') THEN
4045      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Acctd_Amt()-');
4046   END IF;
4047 
4048   RETURN l_acctd_amount ;
4049 
4050 EXCEPTION
4051   WHEN OTHERS THEN
4052      IF PG_DEBUG in ('Y', 'C') THEN
4053         arp_standard.debug('Get_Acctd_Amt: ' || 'EXCEPTION: ARP_ALLOCATION_PKG.Init_Amts');
4054      END IF;
4055      RAISE;
4056 
4057 END Get_Acctd_Amt;
4058 
4059 /* ==========================================================================
4060  | PROCEDURE Alloc_Rev_Tax_Amt
4061  |
4062  | DESCRIPTION
4063  |      Allocate Tax amount over tax distributions for tax lines
4064  |
4065  | SCOPE - PRIVATE
4066  |
4067  | PARAMETERS
4068  |       None
4069  *==========================================================================*/
4070 PROCEDURE Alloc_Rev_Tax_Amt(p_type_acct IN VARCHAR2) IS
4071 
4072 --Rev variables
4073 l_rev_run_amt_tot             NUMBER := 0    ;
4074 l_rev_run_acctd_amt_tot       NUMBER := 0    ;
4075 l_rev_run_pro_amt_tot         NUMBER := 0    ;
4076 l_rev_run_pro_acctd_amt_tot   NUMBER := 0    ;
4077 
4078 --Frt, chrg variables
4079 l_frt_run_pro_amt_tot         NUMBER := 0    ;
4080 l_frt_run_pro_acctd_amt_tot   NUMBER := 0    ;
4081 
4082 --Tax variables
4083 l_ctr                        BINARY_INTEGER;
4084 l_last_tax                   BINARY_INTEGER := 0;
4085 l_last_def_tax               BINARY_INTEGER := 0;
4086 l_tax_run_amt_tot            NUMBER := 0;
4087 l_tax_run_acctd_amt_tot      NUMBER := 0;
4088 l_tax_run_pro_amt_tot        NUMBER := 0;
4089 l_tax_run_pro_acctd_amt_tot  NUMBER := 0;
4090 l_def_tax_run_amt_tot        NUMBER := 0;
4091 l_def_tax_run_acctd_amt_tot  NUMBER := 0;
4092 l_pro_def_tax_run_amt        NUMBER := 0;
4093 l_pro_def_tax_run_acctd_amt  NUMBER := 0;
4094 l_tax_applied                NUMBER := 0;
4095 l_tax_acctd_applied          NUMBER := 0;
4096 l_rowid                      VARCHAR2(50);
4097 
4098 TYPE g_ae_alloc_type IS RECORD (
4099      l_rowid                   DBMS_SQL.VARCHAR2_TABLE,
4100      ae_account_class          DBMS_SQL.VARCHAR2_TABLE,
4101      ae_amount                 DBMS_SQL.NUMBER_TABLE,
4102      ae_acctd_amount           DBMS_SQL.NUMBER_TABLE,
4103      ae_pro_amt                DBMS_SQL.NUMBER_TABLE,
4104      ae_pro_acctd_amt          DBMS_SQL.NUMBER_TABLE,
4105      ae_taxable_amount         DBMS_SQL.NUMBER_TABLE,
4106      ae_taxable_acctd_amount   DBMS_SQL.NUMBER_TABLE,
4107      ae_pro_frt_chrg_amt       DBMS_SQL.NUMBER_TABLE,
4108      ae_pro_frt_chrg_acctd_amt DBMS_SQL.NUMBER_TABLE,
4109      ae_pro_taxable_amt        DBMS_SQL.NUMBER_TABLE,
4110      ae_pro_taxable_acctd_amt  DBMS_SQL.NUMBER_TABLE,
4111      ae_collected_tax_ccid     DBMS_SQL.NUMBER_TABLE,
4112      ae_pro_def_tax_amt        DBMS_SQL.NUMBER_TABLE,
4113      ae_pro_def_tax_acctd_amt  DBMS_SQL.NUMBER_TABLE
4114 );
4115 
4116 g_ae_alloc_rev_tax_tbl       g_ae_alloc_type;
4117 
4118 CURSOR l_rev_tax_cur IS
4119 SELECT /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
4120        rowid,
4121        ae_account_class,
4122        ae_amount,
4123        ae_acctd_amount,
4124        ae_pro_amt,
4125        ae_pro_acctd_amt,
4126        ae_taxable_amount,
4127        ae_taxable_acctd_amount,
4128        ae_pro_taxable_amt,
4129        ae_pro_taxable_acctd_amt,
4130        ae_pro_frt_chrg_amt,
4131        ae_pro_frt_chrg_acctd_amt,
4132        ae_collected_tax_ccid,
4133        ae_pro_def_tax_amt,
4134        ae_pro_def_tax_acctd_amt
4135 FROM ar_ae_alloc_rec_gt
4136 WHERE ae_id = g_id
4137 AND ae_account_class IS NOT NULL
4138 ORDER BY ae_account_class, ae_customer_trx_line_id;
4139 
4140 l_process_rev      BOOLEAN;
4141 l_process_frt      BOOLEAN;
4142 l_process_tax      BOOLEAN;
4143 l_last_fetch       BOOLEAN;
4144 BEGIN
4145 
4146    IF PG_DEBUG in ('Y', 'C') THEN
4147       arp_standard.debug( 'ARP_ALLOCATION_PKG.Alloc_Rev_Tax_Amt()+');
4148    END IF;
4149 
4150 /*------------------------------------------------------------------------------+
4151  | Allocate discount, adjustment, finance charge amounts over revenue. The check|
4152  | below is required because for PA it appears that amount could be zero but    |
4153  | accounted amount could be present. The revenue amount is allocated only if   |
4154  | there is a valid non zero revenue base to allocate otherwise the line amount |
4155  | would be allocated to the Activity GL account                                |
4156  +------------------------------------------------------------------------------*/
4157    l_process_rev := FALSE;
4158 
4159 /*------------------------------------------------------------------------------+
4160  | Set process revenue flag                                                     |
4161  +------------------------------------------------------------------------------*/
4162    IF (((g_ae_rule_rec.line_amt_alloc <> 0) AND (g_ae_rule_rec.revenue_amt <> 0))
4163        OR ((g_ae_rule_rec.line_acctd_amt_alloc <> 0) AND (g_ae_rule_rec.revenue_acctd_amt <> 0)))
4164       AND (g_ae_rev_ctr > 0) THEN
4165 
4166        l_process_rev := TRUE;
4167        IF PG_DEBUG in ('Y', 'C') THEN
4168           arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'Process Revenue is True ');
4169        END IF;
4170 
4171    END IF;
4172 
4173    l_process_frt := FALSE;
4174 
4175    IF ((((g_ae_rule_rec.freight_amt_alloc + g_ae_rule_rec.charges_amt_alloc) <> 0)
4176       AND (g_ae_rule_rec.revenue_amt <> 0))
4177       OR
4178       (((g_ae_rule_rec.freight_acctd_amt_alloc + g_ae_rule_rec.charges_acctd_amt_alloc) <> 0)
4179         AND (g_ae_rule_rec.revenue_acctd_amt <> 0))) AND (g_ae_rev_ctr > 0) THEN
4180       IF PG_DEBUG in ('Y', 'C') THEN
4181          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'Process Freight is True ');
4182       END IF;
4183       l_process_frt := TRUE;
4184 
4185    END IF;
4186 
4187    l_process_tax := FALSE;
4188 
4189 /*------------------------------------------------------------------------------+
4190  | Set process tax flag                                                         |
4191  +------------------------------------------------------------------------------*/
4192    IF (((g_ae_rule_rec.tax_amt_alloc <> 0) OR (g_ae_rule_rec.tax_acctd_amt_alloc <> 0))
4193        OR ((g_ae_rule_rec.line_amt_alloc <> 0) OR (g_ae_rule_rec.line_acctd_amt_alloc <> 0)))
4194       AND (g_ae_tax_ctr > 0) THEN
4195 
4196       l_process_tax := TRUE;
4197       IF PG_DEBUG in ('Y', 'C') THEN
4198          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'Process Tax is True ');
4199       END IF;
4200 
4201    END IF;
4202 
4203 /*------------------------------------------------------------------------------+
4204  | Set tax applied and accounted and allocate over tax, this is done using a    |
4205  | RATE which is determined based on the total deferred tax over the actual tax |
4206  | The arpcurr round function is used to round to precision for curency of      |
4207  | Invoice and functional currency.                                             |
4208  +------------------------------------------------------------------------------*/
4209    IF ((g_ae_def_tax) AND (NOT g_done_def_tax) AND (p_type_acct = 'PAY'))
4210       AND l_process_tax THEN
4211 
4212       IF g_ae_rule_rec.tax_amt <> 0 THEN --prevent zero divide
4213 
4214          l_tax_applied       := arpcurr.CurrRound(g_ae_rule_rec.tax_amt_alloc    *
4215                                    g_ae_rule_rec.def_tax_amt/g_ae_rule_rec.tax_amt,
4216                                    g_cust_inv_rec.invoice_currency_code);
4217       END IF; --amt not zero
4218 
4219       IF g_ae_rule_rec.tax_acctd_amt <> 0 THEN --prevent zero divide
4220 
4221          l_tax_acctd_applied := arpcurr.CurrRound(g_ae_rule_rec.tax_acctd_amt_alloc   *
4222                                  g_ae_rule_rec.def_tax_acctd_amt/g_ae_rule_rec.tax_acctd_amt,
4223                                  g_ae_sys_rec.base_currency);
4224       END IF; --accounted amt not zero
4225 
4226    END IF; --process Tax
4227 
4228  /*------------------------------------------------------------------------------+
4229   | Loop through tax to allocate tax on discounts and adjustments payments       |
4230   +------------------------------------------------------------------------------*/
4231   IF l_process_rev OR l_process_frt OR l_process_tax THEN
4232 
4233      OPEN l_rev_tax_cur;
4234 
4235      LOOP
4236 
4237       FETCH l_rev_tax_cur BULK COLLECT INTO
4238             g_ae_alloc_rev_tax_tbl.l_rowid,
4239             g_ae_alloc_rev_tax_tbl.ae_account_class,
4240             g_ae_alloc_rev_tax_tbl.ae_amount,
4241             g_ae_alloc_rev_tax_tbl.ae_acctd_amount,
4242             g_ae_alloc_rev_tax_tbl.ae_pro_amt,
4243             g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt,
4244             g_ae_alloc_rev_tax_tbl.ae_taxable_amount,
4245             g_ae_alloc_rev_tax_tbl.ae_taxable_acctd_amount,
4246             g_ae_alloc_rev_tax_tbl.ae_pro_taxable_amt,
4247             g_ae_alloc_rev_tax_tbl.ae_pro_taxable_acctd_amt,
4248             g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt,
4249             g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt,
4250             g_ae_alloc_rev_tax_tbl.ae_collected_tax_ccid,
4251             g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt,
4252             g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt
4253        LIMIT g_bulk_fetch_rows;
4254 
4255        IF l_rev_tax_cur%NOTFOUND THEN
4256           l_last_fetch := TRUE;
4257        END IF;
4258 
4259        IF (g_ae_alloc_rev_tax_tbl.l_rowid.COUNT = 0) AND (l_last_fetch) THEN
4260          IF PG_DEBUG in ('Y', 'C') THEN
4261             arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'COUNT = 0 and LAST FETCH ');
4262          END IF;
4263          EXIT;
4264        END IF;
4265 
4266     FOR i IN g_ae_alloc_rev_tax_tbl.l_rowid.FIRST .. g_ae_alloc_rev_tax_tbl.l_rowid.LAST LOOP
4267 
4268       IF ((l_process_rev) OR (l_process_frt)) AND g_ae_alloc_rev_tax_tbl.ae_account_class(i) = 'REVEARN' THEN
4269 
4270       /*------------------------------------------------------------------------------+
4271        | Maintain running total amounts for Revenue amounts and accounted amounts, set|
4272        | cached flag.
4273        +------------------------------------------------------------------------------*/
4274           l_rev_run_amt_tot       := l_rev_run_amt_tot + g_ae_alloc_rev_tax_tbl.ae_amount(i);
4275           l_rev_run_acctd_amt_tot := l_rev_run_acctd_amt_tot + g_ae_alloc_rev_tax_tbl.ae_acctd_amount(i);
4276 
4277         /*------------------------------------------------------------------------------+
4278          | Allocate revenue for discount or adjustments to each revenue line in Invoice |
4279          | currency. Rev lines 10, 20, 30, 40, Rev Total 100, Discount 10               |
4280          | Line 1  a -> 10 * 10/100  = 1 (allocated)                                    |
4281          |                                                                              |
4282          | Line 2    -> (10 + 20)/100 * 10 = 3                                          |
4283          |         b -> 3 - a = 2 (allocated)                                           |
4284          |                                                                              |
4285          | Line 3    -> (10 + 20 + 30) * 10/100 = 6                                     |
4286          |         c -> 6 - a - b = 3                                                   |
4287          | Line .....                                                                   |
4288          +------------------------------------------------------------------------------*/
4289          IF g_ae_rule_rec.revenue_amt <> 0 THEN
4290 
4291           /*------------------------------------------------------------------------------+
4292            | Process line amounts                                                         |
4293            +------------------------------------------------------------------------------*/
4294             IF l_process_rev THEN
4295             g_ae_alloc_rev_tax_tbl.ae_pro_amt(i) :=
4296             arpcurr.CurrRound(l_rev_run_amt_tot / g_ae_rule_rec.revenue_amt * g_ae_rule_rec.line_amt_alloc,
4297                               g_cust_inv_rec.invoice_currency_code) - l_rev_run_pro_amt_tot;
4298 
4299             IF PG_DEBUG in ('Y', 'C') THEN
4300                arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_amt = '||
4301                                 g_ae_alloc_rev_tax_tbl.ae_pro_amt(i));
4302             END IF;
4303 
4304           /*------------------------------------------------------------------------------+
4305            | Running total for prorated Revenue amount in currency of Invoice             |
4306            +------------------------------------------------------------------------------*/
4307             l_rev_run_pro_amt_tot := l_rev_run_pro_amt_tot
4308                                          + g_ae_alloc_rev_tax_tbl.ae_pro_amt(i);
4309             END IF;
4310 
4311           /*------------------------------------------------------------------------------+
4312            | Process Freight and charges                                                  |
4313            +------------------------------------------------------------------------------*/
4314             IF l_process_frt THEN
4315             g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt(i) :=
4316             arpcurr.CurrRound(l_rev_run_amt_tot / g_ae_rule_rec.revenue_amt *
4317                               (g_ae_rule_rec.freight_amt_alloc + g_ae_rule_rec.charges_amt_alloc),
4318                               g_cust_inv_rec.invoice_currency_code) - l_frt_run_pro_amt_tot;
4319 
4320             IF PG_DEBUG in ('Y', 'C') THEN
4321                arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt('||i||') = '||
4322                                 g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt(i));
4323             END IF;
4324 
4325           /*------------------------------------------------------------------------------+
4326            | Running total for prorated Freight amount in currency of Invoice             |
4327            +------------------------------------------------------------------------------*/
4328             l_frt_run_pro_amt_tot := l_frt_run_pro_amt_tot
4329                                          + g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt(i);
4330             END IF;
4331 
4332          END IF;
4333 
4334         /*------------------------------------------------------------------------------+
4335          | Calculate accounted amount for revenue amount allocated to each revenue line |
4336          +------------------------------------------------------------------------------*/
4337          IF g_ae_rule_rec.revenue_acctd_amt <> 0 THEN
4338 
4339           /*------------------------------------------------------------------------------+
4340            | Process Line accounted amounts                                               |
4341            +------------------------------------------------------------------------------*/
4342             IF l_process_rev THEN
4343             g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i) :=
4344              arpcurr.CurrRound(l_rev_run_acctd_amt_tot / g_ae_rule_rec.revenue_acctd_amt
4345                                * g_ae_rule_rec.line_acctd_amt_alloc, g_ae_sys_rec.base_currency)
4346                                    - l_rev_run_pro_acctd_amt_tot;
4347 
4348              IF PG_DEBUG in ('Y', 'C') THEN
4349                 arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt = '||
4350                                  g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i));
4351              END IF;
4352 
4353           /*------------------------------------------------------------------------------+
4354            | Running total for prorated Revenue accounted amount in base currency         |
4355            +------------------------------------------------------------------------------*/
4356             l_rev_run_pro_acctd_amt_tot := l_rev_run_pro_acctd_amt_tot
4357                                                  + g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i);
4358             END IF;
4359           /*------------------------------------------------------------------------------+
4360            | Process Freight and charges                                                  |
4361            +------------------------------------------------------------------------------*/
4362             IF l_process_frt THEN
4363             g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt(i) :=
4364              arpcurr.CurrRound(l_rev_run_acctd_amt_tot / g_ae_rule_rec.revenue_acctd_amt
4365                                * (g_ae_rule_rec.freight_acctd_amt_alloc + g_ae_rule_rec.charges_acctd_amt_alloc),
4366                                g_ae_sys_rec.base_currency) - l_frt_run_pro_acctd_amt_tot;
4367 
4368              IF PG_DEBUG in ('Y', 'C') THEN
4369                 arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt('||i||') = '||
4370                                  g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt(i));
4371              END IF;
4372 
4373           /*------------------------------------------------------------------------------+
4374            | Running total for prorated Freight accounted amount in base currency         |
4375            +------------------------------------------------------------------------------*/
4376             l_frt_run_pro_acctd_amt_tot := l_frt_run_pro_acctd_amt_tot
4377                                             + g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt(i);
4378 
4379             END IF;
4380          END IF; --revenue accounted amount is not zero
4381 
4382          --Dump_Alloc_Rev_Tax(p_type => 'REV_TAX', p_alloc_rec => g_ae_alloc_rev_tax_tbl);
4383 
4384       END IF; --process revenue
4385 
4386    /*------------------------------------------------------------------------------+
4387     | Allocate tax amounts, deferred tax amounts                                   |
4388     +------------------------------------------------------------------------------*/
4389       IF l_process_tax AND g_ae_alloc_rev_tax_tbl.ae_account_class(i) = 'TAX' THEN
4390 
4391      /*------------------------------------------------------------------------------+
4392       | Maintain running total amounts for Tax amounts and accounted amounts         |
4393       +------------------------------------------------------------------------------*/
4394          l_tax_run_amt_tot       := l_tax_run_amt_tot + g_ae_alloc_rev_tax_tbl.ae_amount(i);
4395          l_tax_run_acctd_amt_tot := l_tax_run_acctd_amt_tot + g_ae_alloc_rev_tax_tbl.ae_acctd_amount(i);
4396 
4397          IF g_ae_rule_rec.tax_amt <> 0 THEN --prevent zero divide
4398            /*------------------------------------------------------------------------------+
4399             | Allocate tax for discount or adjustments to each tax line in Invoice         |
4400             | currency. Tax lines 10, 20, 30, 40, Tax Total 100, Tax on Discount 10        |
4401             | Line 1  a -> 10 * 10/100  = 1 (allocated)                                    |
4402             |                                                                              |
4403             | Line 2    -> (10 + 20)/100 * 10 = 3                                          |
4404             |         b -> 3 - a = 2 (allocated)                                           |
4405             |                                                                              |
4406             | Line 3    -> (10 + 20 + 30) * 10/100 = 6                                     |
4407             |         c -> 6 - a - b = 3                                                   |
4408             | Line .....                                                                   |
4409             +------------------------------------------------------------------------------*/
4410 
4411             g_ae_alloc_rev_tax_tbl.ae_pro_amt(i) :=
4412              arpcurr.CurrRound(l_tax_run_amt_tot / g_ae_rule_rec.tax_amt * g_ae_rule_rec.tax_amt_alloc,
4413                                g_cust_inv_rec.invoice_currency_code) - l_tax_run_pro_amt_tot;
4414 
4415              IF PG_DEBUG in ('Y', 'C') THEN
4416                 arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_amt = '||
4417                                  g_ae_alloc_rev_tax_tbl.ae_pro_amt(i));
4418              END IF;
4419 
4420           /*------------------------------------------------------------------------------+
4421            | Running total for prorated Tax amount in currency of Invoice                 |
4422            +------------------------------------------------------------------------------*/
4423             l_tax_run_pro_amt_tot := l_tax_run_pro_amt_tot + g_ae_alloc_rev_tax_tbl.ae_pro_amt(i);
4424 
4425          END IF;
4426 
4427          IF g_ae_rule_rec.tax_acctd_amt <> 0 THEN --prevent zero divide
4428           /*------------------------------------------------------------------------------+
4429            | Calculate accounted amount for tax amount allocated to each tax line         |
4430            +------------------------------------------------------------------------------*/
4431             g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i) :=
4432             arpcurr.CurrRound(l_tax_run_acctd_amt_tot / g_ae_rule_rec.tax_acctd_amt
4433                               * g_ae_rule_rec.tax_acctd_amt_alloc, g_ae_sys_rec.base_currency)
4434                                      - l_tax_run_pro_acctd_amt_tot;
4435 
4436             IF PG_DEBUG in ('Y', 'C') THEN
4437                arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt = '||
4438                                 g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i));
4439             END IF;
4440 
4441          /*------------------------------------------------------------------------------+
4442           | Running total for prorated Tax accounted amount in base currency             |
4443           +------------------------------------------------------------------------------*/
4444             l_tax_run_pro_acctd_amt_tot := l_tax_run_pro_acctd_amt_tot + g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i);
4445 
4446         END IF;
4447 
4448       /*-------------------------------------------------------------------------------+
4449        | Calculate taxable as "allocated tax amount/actual tax amt * actual taxable"   |
4450        | to get accurate basis for taxable only if a boundary condition exists         |
4451        +-------------------------------------------------------------------------------*/
4452         IF ((g_bound_tax) OR (g_bound_activity))
4453            AND ((g_orig_line_amt_alloc <> 0) OR (g_orig_line_acctd_amt_alloc <> 0)) THEN
4454 
4455            IF g_ae_alloc_rev_tax_tbl.ae_amount(i) <> 0 THEN
4456 
4457               g_ae_alloc_rev_tax_tbl.ae_pro_taxable_amt(i) :=
4458                    arpcurr.CurrRound(g_ae_alloc_rev_tax_tbl.ae_taxable_amount(i) *
4459                                      g_ae_alloc_rev_tax_tbl.ae_pro_amt(i) / g_ae_alloc_rev_tax_tbl.ae_amount(i),
4460                                      g_cust_inv_rec.invoice_currency_code);
4461 
4462               IF PG_DEBUG in ('Y', 'C') THEN
4463                  arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_taxable_amt = '||
4464                                   g_ae_alloc_rev_tax_tbl.ae_pro_taxable_amt(i));
4465               END IF;
4466 
4467            END IF; --end if ae_amount is zero
4468 
4469          /*------------------------------------------------------------------------------+
4470           | Calculate taxable accounted amount                                           |
4471           +------------------------------------------------------------------------------*/
4472            IF g_ae_alloc_rev_tax_tbl.ae_acctd_amount(i) <> 0 THEN
4473 
4474               g_ae_alloc_rev_tax_tbl.ae_pro_taxable_acctd_amt(i) :=
4475                 arpcurr.CurrRound(g_ae_alloc_rev_tax_tbl.ae_taxable_acctd_amount(i) *
4476                              g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i) / g_ae_alloc_rev_tax_tbl.ae_acctd_amount(i),
4477                              g_ae_sys_rec.base_currency);
4478 
4479               IF PG_DEBUG in ('Y', 'C') THEN
4480                  arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_taxable_acctd_amt = '||
4481                                 g_ae_alloc_rev_tax_tbl.ae_pro_taxable_acctd_amt(i));
4482               END IF;
4483 
4484            END IF; --end if ae_acctd_amount is zero
4485 
4486         END IF; --End if Boundary condition
4487 
4488       /*------------------------------------------------------------------------------+
4489        | Allocation of tax based on payment is done for all tax lines only deferred   |
4490        | tax amounts will be moved from interim to collected tax account              |
4491        +------------------------------------------------------------------------------*/
4492         IF (g_ae_doc_rec.source_table = 'RA') AND (g_ae_alloc_rev_tax_tbl.ae_collected_tax_ccid(i) IS NOT NULL)
4493            AND (NOT g_done_def_tax)
4494         THEN
4495          /*------------------------------------------------------------------------------+
4496           | Maintain running total amounts for Revenue amounts and accounted amounts     |
4497           +------------------------------------------------------------------------------*/
4498            l_def_tax_run_amt_tot           := l_def_tax_run_amt_tot +
4499                                                         g_ae_alloc_rev_tax_tbl.ae_amount(i);
4500            l_def_tax_run_acctd_amt_tot     := l_def_tax_run_acctd_amt_tot +
4501                                                         g_ae_alloc_rev_tax_tbl.ae_acctd_amount(i);
4502 
4503          /*---------------------------------------------------------------------------------+
4504           | Calculate deferred tax amount to be moved from interim to collected tax account |
4505           +---------------------------------------------------------------------------------*/
4506            IF g_ae_rule_rec.def_tax_amt <> 0 THEN
4507 
4508               g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt(i) :=
4509                         arpcurr.CurrRound(l_def_tax_run_amt_tot / g_ae_rule_rec.def_tax_amt * l_tax_applied,
4510                                           g_cust_inv_rec.invoice_currency_code) - l_pro_def_tax_run_amt;
4511 
4512               IF PG_DEBUG in ('Y', 'C') THEN
4513                  arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt = '||
4514                                   g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt(i));
4515               END IF;
4516 
4517            /*------------------------------------------------------------------------------+
4518             | Running total for prorated deferred tax amount in currency of Invoice        |
4519             +------------------------------------------------------------------------------*/
4520              l_pro_def_tax_run_amt := l_pro_def_tax_run_amt + g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt(i);
4521 
4522            END IF;
4523 
4524          /*------------------------------------------------------------------------------+
4525           | Calculate deferred tax accounted amount to be moved from interim to collected|
4526           | tax account                                                                  |
4527           +------------------------------------------------------------------------------*/
4528            IF g_ae_rule_rec.def_tax_acctd_amt <> 0 THEN
4529 
4530               g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt(i) :=
4531                 arpcurr.CurrRound(l_def_tax_run_acctd_amt_tot / g_ae_rule_rec.def_tax_acctd_amt
4532                                   * l_tax_acctd_applied, g_ae_sys_rec.base_currency)
4533                                           - l_pro_def_tax_run_acctd_amt;
4534 
4535               IF PG_DEBUG in ('Y', 'C') THEN
4536                  arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt = '||
4537                                   g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt(i));
4538               END IF;
4539 
4540              /*------------------------------------------------------------------------------+
4541               | Running total for prorated deferred tax accounted amount in base currency    |
4542               +------------------------------------------------------------------------------*/
4543                l_pro_def_tax_run_acctd_amt :=
4544                   l_pro_def_tax_run_acctd_amt + g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt(i);
4545 
4546            END IF;
4547 
4548            l_last_def_tax := l_ctr;
4549 
4550         END IF; --End if payment not zero
4551 
4552         --Dump_Alloc_Rev_Tax(p_type => 'REV_TAX', p_alloc_rec => g_ae_alloc_rev_tax_tbl);
4553 
4554         l_last_tax := l_ctr;
4555 
4556       END IF; --process tax
4557 
4558     END LOOP;  --loop bulk fetched rows
4559 
4560    /*------------------------------------------------------------------------------+
4561     | Update the amounts for revenue or tax based on rowid                         |
4562     +------------------------------------------------------------------------------*/
4563       FORALL m IN g_ae_alloc_rev_tax_tbl.l_rowid.FIRST .. g_ae_alloc_rev_tax_tbl.l_rowid.LAST
4564           UPDATE ar_ae_alloc_rec_gt
4565            SET  ae_pro_amt             = g_ae_alloc_rev_tax_tbl.ae_pro_amt(m),
4566              ae_pro_acctd_amt          = g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(m),
4567              ae_pro_taxable_amt        = g_ae_alloc_rev_tax_tbl.ae_pro_taxable_amt(m),
4568              ae_pro_taxable_acctd_amt  = g_ae_alloc_rev_tax_tbl.ae_pro_taxable_acctd_amt(m),
4569              ae_pro_frt_chrg_amt       = g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt(m),
4570              ae_pro_frt_chrg_acctd_amt = g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt(m),
4571              ae_pro_def_tax_amt        = g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt(m),
4572              ae_pro_def_tax_acctd_amt  = g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt(m)
4573           WHERE rowid = g_ae_alloc_rev_tax_tbl.l_rowid(m);
4574 
4575   --Exit if Last fetch
4576     IF l_last_fetch THEN
4577        EXIT;
4578     END IF;
4579 
4580    END LOOP; --process revenue tax bulk fetch
4581 
4582    CLOSE l_rev_tax_cur;
4583 
4584    END IF; --process revenue or tax
4585 
4586  /*---------------------------------------------------------------------------------+
4587   | Call taxable amount routine, to build the taxable amounts and accounted amounts |
4588   +---------------------------------------------------------------------------------*/
4589    IF (NOT g_bound_tax) AND (NOT g_bound_activity) and l_process_tax THEN
4590        Set_Taxable_Amt(p_type_acct => p_type_acct);
4591        Set_Taxable_Split_Amt(p_type_acct => p_type_acct);
4592    END IF;
4593 
4594  /*------------------------------------------------------------------------------+
4595   | Abnormal rounding correction condition this should never happen, however this|
4596   | is a safety mechanism. For payments check rounding only when type acct is PAY|
4597   +------------------------------------------------------------------------------*/
4598    IF ((((l_tax_run_pro_amt_tot <> g_ae_rule_rec.tax_amt_alloc) AND (g_ae_rule_rec.tax_amt <> 0))
4599           OR ((l_tax_run_pro_acctd_amt_tot <> g_ae_rule_rec.tax_acctd_amt_alloc)
4600               AND (g_ae_rule_rec.tax_acctd_amt <> 0)))
4601       OR ((((l_pro_def_tax_run_amt <> l_tax_applied) AND (g_ae_rule_rec.def_tax_amt <> 0))
4602              OR ((l_pro_def_tax_run_acctd_amt <> l_tax_acctd_applied) AND (g_ae_rule_rec.def_tax_acctd_amt <> 0)))
4603               AND (g_ae_def_tax) AND (NOT g_done_def_tax) AND (p_type_acct = 'PAY')))
4604       AND l_process_tax THEN
4605 
4606       IF PG_DEBUG in ('Y', 'C') THEN
4607          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_tax_run_pro_amt_tot : '||l_tax_run_pro_amt_tot||
4608                          ' <> '||'g_ae_rule_rec.tax_amt_alloc : '||g_ae_rule_rec.tax_amt_alloc);
4609          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_tax_run_pro_acctd_amt_tot : '||l_tax_run_pro_acctd_amt_tot||
4610                          ' <> '||'g_ae_rule_rec.tax_acctd_amt_alloc : '||g_ae_rule_rec.tax_acctd_amt_alloc);
4611          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_pro_def_tax_run_amt : '||l_pro_def_tax_run_amt||
4612                          ' <> '||'l_tax_applied : '||l_tax_applied);
4613          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_pro_def_tax_run_acctd_amt : '||l_pro_def_tax_run_acctd_amt||
4614                          ' <> '||'l_tax_acctd_applied : '||l_tax_acctd_applied);
4615       END IF;
4616 
4617       RAISE rounding_error;
4618 
4619    END IF; --tax rounding condition check
4620 
4621 /*------------------------------------------------------------------------------+
4622  | Abnormal rounding correction condition this should never happen, however this|
4623  | is a safety mechanism.                                                       |
4624  +------------------------------------------------------------------------------*/
4625    IF (((l_rev_run_pro_amt_tot <> g_ae_rule_rec.line_amt_alloc) AND (g_ae_rule_rec.revenue_amt <> 0))
4626           OR ((l_rev_run_pro_acctd_amt_tot <> g_ae_rule_rec.line_acctd_amt_alloc)
4627               AND (g_ae_rule_rec.revenue_acctd_amt <>0))) AND l_process_rev THEN
4628 
4629       IF PG_DEBUG in ('Y', 'C') THEN
4630          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_rev_run_pro_amt_tot : '||l_rev_run_pro_amt_tot||
4631                          ' <> '||'g_ae_rule_rec.line_amt_alloc : '||g_ae_rule_rec.line_amt_alloc);
4632          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_rev_run_pro_acctd_amt_tot : '||l_rev_run_pro_acctd_amt_tot||
4633                          ' <> '||'g_ae_rule_rec.line_acctd_amt_alloc : '||g_ae_rule_rec.line_acctd_amt_alloc);
4634       END IF;
4635 
4636       RAISE rounding_error;
4637 
4638    END IF; --check rounding for revenue
4639 
4640  /*------------------------------------------------------------------------------+
4641   | Abnormal rounding correction condition this should never happen, however this|
4642   | is a safety mechanism.                                                       |
4643   +------------------------------------------------------------------------------*/
4644    IF (((l_frt_run_pro_amt_tot <> (g_ae_rule_rec.freight_amt_alloc + g_ae_rule_rec.charges_amt_alloc))
4645         AND (g_ae_rule_rec.revenue_amt <> 0))
4646       OR ((l_frt_run_pro_acctd_amt_tot <>
4647                   (g_ae_rule_rec.freight_acctd_amt_alloc + g_ae_rule_rec.charges_acctd_amt_alloc))
4648            AND (g_ae_rule_rec.revenue_acctd_amt <>0))) AND l_process_frt THEN
4649 
4650       IF PG_DEBUG in ('Y', 'C') THEN
4651          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_frt_run_pro_amt_tot : '||l_frt_run_pro_amt_tot||
4652                          ' <> '||'g_ae_rule_rec.freight_amt_alloc ' || g_ae_rule_rec.freight_amt_alloc ||
4653                          ' + g_ae_rule_rec.charges_amt_alloc ' || g_ae_rule_rec.charges_amt_alloc );
4654          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_frt_run_pro_acctd_amt_tot : '||l_frt_run_pro_acctd_amt_tot||
4655                          ' <> '||'g_ae_rule_rec.freight_acctd_amt_alloc ' || g_ae_rule_rec.freight_acctd_amt_alloc ||
4656                          ' + g_ae_rule_rec.charges_acctd_amt_alloc ' || g_ae_rule_rec.charges_acctd_amt_alloc );
4657       END IF;
4658 
4659       RAISE rounding_error;
4660 
4661    END IF;
4662 
4663  IF PG_DEBUG in ('Y', 'C') THEN
4664     arp_standard.debug( 'ARP_ALLOCATION_PKG.Alloc_Rev_Tax_Amt()-');
4665  END IF;
4666 
4667 EXCEPTION
4668 
4669   WHEN rounding_error THEN
4670      IF PG_DEBUG in ('Y', 'C') THEN
4671         arp_standard.debug('Rounding Error: ARP_ALLOCATION_PKG.Alloc_Rev_Tax_Amt' );
4672      END IF;
4673      fnd_message.set_name('AR','AR_ROUNDING_ERROR');
4674      fnd_message.set_token('ROUTINE','ARP_ALLOCATION_PKG.ALLOC_REV_TAX_AMT');
4675      RAISE;
4676 
4677    WHEN OTHERS THEN
4678       IF PG_DEBUG in ('Y', 'C') THEN
4679          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || SQLERRM);
4680          arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Alloc_Rev_Tax_Amt');
4681       END IF;
4682       RAISE;
4683 
4684 END Alloc_Rev_Tax_Amt;
4685 
4686 /* ==========================================================================
4687  | PROCEDURE Set_Taxable_Amt
4688  |
4689  | DESCRIPTION
4690  |      Derive Taxable amounts for Tax, when this routine is called the tax
4691  |      table has to exist.
4692  |
4693  | SCOPE - PRIVATE
4694  |
4695  | PARAMETERS
4696  |       p_type_acct      IN    Indicates accounting for earned, unearned
4697  |                              discounts or adjustments
4698  *==========================================================================*/
4699 PROCEDURE Set_Taxable_Amt(p_type_acct IN VARCHAR2) IS
4700 
4701 l_ctr                    BINARY_INTEGER;
4702 l_ctr1                   BINARY_INTEGER;
4703 
4704 CURSOR set_taxable_and_link IS
4705 select /*+ INDEX(a2 AR_AE_ALLOC_REC_GT_N3) */
4706        a2.rowid,
4707        a3.link_id,
4708        a3.line_id,
4709        a3.amt,
4710        a3.acctd_amt
4711 from ar_ae_alloc_rec_gt a2,
4712 (select  /*+ INDEX(a1 AR_AE_ALLOC_REC_GT_N3) */
4713          a1.ae_id                   ae_id,
4714          decode(p_type_acct,
4715                 'ED_ADJ', a1.ae_tax_link_id_ed_adj,
4716                 'UNED'  , a1.ae_tax_link_id_uned,
4717                 'PAY'   , a1.ae_tax_link_id)          link_id,
4718          a1.ae_customer_trx_line_id line_id,
4719          sum(a1.ae_pro_amt)         amt,
4720          sum(a1.ae_pro_acctd_amt)   acctd_amt
4721 from ar_ae_alloc_rec_gt a1
4722 where a1.ae_id = g_id
4723 and a1.ae_account_class IN ('REVEARN','REVUNEARN') --MAINTAINLINKTAXID
4724 group by
4725          a1.ae_id,
4726 		 decode(p_type_acct,
4727                 'ED_ADJ', a1.ae_tax_link_id_ed_adj,
4728                 'UNED'  , a1.ae_tax_link_id_uned,
4729                 'PAY'   , a1.ae_tax_link_id),
4730          a1.ae_customer_trx_line_id) a3
4731 where a2.ae_id = g_id
4732 and a3.ae_id = a2.ae_id
4733 and a2.ae_link_to_cust_trx_line_id = a3.line_id
4734 and a2.ae_account_class = 'TAX'
4735 and decode(p_type_acct,
4736            'ED_ADJ', a2.ae_tax_link_id_ed_adj,
4737            'UNED'  , a2.ae_tax_link_id_uned,
4738            'PAY'   , a2.ae_tax_link_id) = a3.link_id
4739 order by a3.link_id, (abs(a3.amt) + abs(a3.acctd_amt)) DESC, a3.line_id;
4740 
4741 TYPE num_type   IS TABLE OF NUMBER       INDEX BY BINARY_INTEGER;
4742 TYPE rowid_type IS TABLE OF VARCHAR2(50) INDEX BY BINARY_INTEGER;
4743 
4744 rowid_tbl      rowid_type;
4745 line_id_tbl    num_type;
4746 link_id_tbl    num_type;
4747 amt_tbl        num_type;
4748 acctd_amt_tbl  num_type;
4749 
4750 l_last_fetch BOOLEAN := FALSE;
4751 
4752 prev_sum_of_amounts NUMBER;
4753 prev_link_id        NUMBER;
4754 
4755 BEGIN
4756 
4757    IF PG_DEBUG in ('Y', 'C') THEN
4758       arp_standard.debug( 'ARP_ALLOCATION_PKG.Set_Taxable_Amt()+');
4759    END IF;
4760 
4761    OPEN set_taxable_and_link;
4762 
4763    prev_link_id := -9999;
4764    prev_sum_of_amounts := 0;
4765 
4766    LOOP
4767     --initialize record
4768 
4769       FETCH set_taxable_and_link BULK COLLECT INTO
4770             rowid_tbl,
4771             link_id_tbl,
4772             line_id_tbl,
4773             amt_tbl,
4774             acctd_amt_tbl
4775         LIMIT g_bulk_fetch_rows;
4776 
4777       IF set_taxable_and_link%NOTFOUND THEN
4778          l_last_fetch := TRUE;
4779       END IF;
4780 
4781       IF (rowid_tbl.COUNT = 0) AND (l_last_fetch) THEN
4782          IF PG_DEBUG in ('Y', 'C') THEN
4783             arp_standard.debug('Set_Taxable_Amt: ' || 'COUNT = 0 and LAST FETCH ');
4784          END IF;
4785          EXIT;
4786       END IF;
4787 
4788       FOR i IN rowid_tbl.FIRST .. rowid_tbl.LAST LOOP
4789 
4790         --Initialize the sum of amounts and link to that of new link line
4791           IF prev_link_id <> link_id_tbl(i) THEN
4792              prev_link_id := link_id_tbl(i);
4793              prev_sum_of_amounts := 0;
4794           END IF;
4795 
4796        --Verify whether there exists a non zero amount, accounted amount for link id
4797           IF  ((abs(amt_tbl(i)) + abs(acctd_amt_tbl(i))) = 0)
4798               AND prev_link_id = link_id_tbl(i)
4799               AND prev_sum_of_amounts = 0 THEN
4800               link_id_tbl(i) := '';
4801           END IF;
4802 
4803           prev_sum_of_amounts := prev_sum_of_amounts + abs(amt_tbl(i)) + abs(acctd_amt_tbl(i));
4804       END LOOP;
4805 
4806     --Bulk update
4807       FORALL m IN rowid_tbl.FIRST .. rowid_tbl.LAST
4808         UPDATE ar_ae_alloc_rec_gt
4809         SET ae_pro_taxable_amt       = amt_tbl(m),
4810             ae_pro_taxable_acctd_amt = acctd_amt_tbl(m),
4811             ae_tax_link_id_act       = link_id_tbl(m)
4812         WHERE rowid = rowid_tbl(m)
4813         AND link_id_tbl(m) IS NOT NULL;
4814 
4815    --Exit if Last fetch
4816       IF l_last_fetch THEN
4817          EXIT;
4818       END IF;
4819 
4820    END LOOP; --Bulk fetch
4821 
4822    CLOSE set_taxable_and_link;
4823 
4824    IF PG_DEBUG in ('Y', 'C') THEN
4825       arp_standard.debug( 'ARP_ALLOCATION_PKG.Set_Taxable_Amt()-');
4826    END IF;
4827 
4828 EXCEPTION
4829   WHEN OTHERS THEN
4830      IF PG_DEBUG in ('Y', 'C') THEN
4831         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Set_Taxable_Amt');
4832      END IF;
4833      RAISE;
4834 
4835 END Set_Taxable_Amt;
4836 
4837 
4838 /* ==========================================================================
4839  | PROCEDURE Set_Taxable_Split_Amt
4840  |
4841  | DESCRIPTION
4842  |      Derive Taxable split amounts for Tax, when this routine is called the
4843  |      table has to exist. Required to ensure that the taxable amount is not
4844  |      overstated if the tax line has splits.
4845  |
4846  | SCOPE - PRIVATE
4847  |
4848  | PARAMETERS
4849  |       p_type_acct      IN    Indicates accounting for earned, unearned
4850  |                              discounts or adjustments
4851  *==========================================================================*/
4852 PROCEDURE Set_Taxable_Split_Amt(p_type_acct IN VARCHAR2) IS
4853 
4854 CURSOR set_taxable_split IS
4855 select /*+ INDEX(a1 AR_AE_ALLOC_REC_GT_N3) */
4856        a1.rowid                       row_id,
4857        a1.ae_link_to_cust_trx_line_id inv_line_id,
4858        a1.ae_tax_id                   tax_id,
4859        a1.ae_tax_type                 tax_type,
4860        a1.ae_code_combination_id      ae_code_combination_id,
4861        a1.ae_collected_tax_ccid       ae_collected_tax_ccid,
4862        a1.ae_pro_taxable_amt          pro_taxable_amt,
4863        a1.ae_pro_taxable_acctd_amt    pro_taxable_acctd_amt,
4864        0                              taxable_amt_split,
4865        0                              taxable_acctd_amt_split,
4866        ''                             taxable_amt_r_split,
4867        ''                             taxable_acctd_amt_r_split
4868 from ar_ae_alloc_rec_gt a1,
4869      (select /*+ INDEX(a2 AR_AE_ALLOC_REC_GT_N3) */
4870              a2.ae_link_to_cust_trx_line_id ae_link_to_cust_trx_line_id,
4871              a2.ae_tax_type                 ae_tax_type,
4872              a2.ae_tax_id                   ae_tax_id
4873       from ar_ae_alloc_rec_gt a2
4874       where a2.ae_id = g_id
4875       and a2.ae_account_class = 'TAX'
4876       group by a2.ae_link_to_cust_trx_line_id,
4877                a2.ae_tax_type,
4878                a2.ae_tax_id
4879       having count(*) > 1) a3
4880 where a1.ae_id = g_id
4881 and a1.ae_account_class = 'TAX'
4882 and a1.ae_link_to_cust_trx_line_id = a3.ae_link_to_cust_trx_line_id
4883 and a1.ae_tax_id = a3.ae_tax_id
4884 and a1.ae_tax_type = a3.ae_tax_type
4885 order by a1.ae_link_to_cust_trx_line_id,
4886          a1.ae_tax_type,
4887          a1.ae_tax_id,
4888          decode(a1.ae_collected_tax_ccid,
4889                 '',2,
4890                 1),
4891          a1.ae_code_combination_id,
4892          a1.ae_collected_tax_ccid;
4893 
4894 TYPE num_type   IS TABLE OF NUMBER       INDEX BY BINARY_INTEGER;
4895 TYPE var_type   IS TABLE OF VARCHAR2(3)  INDEX BY BINARY_INTEGER;
4896 TYPE rowid_type IS TABLE OF VARCHAR2(50) INDEX BY BINARY_INTEGER;
4897 
4898 rowid_tbl                     rowid_type;
4899 line_id_tbl                   num_type;
4900 tax_id_tbl                    num_type;
4901 tax_type_tbl                  var_type;
4902 taxable_amt_tbl               num_type;
4903 taxable_acctd_amt_tbl         num_type;
4904 taxable_amt_split_tbl         num_type;
4905 taxable_acctd_amt_split_tbl   num_type;
4906 taxable_amt_recov_tbl         num_type;
4907 taxable_acctd_amt_recov_tbl   num_type;
4908 tax_ccid_tbl                  num_type;
4909 tax_collected_ccid_tbl        num_type;
4910 
4911 l_last_fetch                  BOOLEAN := FALSE;
4912 l_prev_customer_trx_line_id   NUMBER;
4913 l_prev_tax_id                 NUMBER;
4914 l_prev_tax_type               VARCHAR2(3);
4915 l_prev_ccid                   NUMBER;
4916 l_prev_collected_ccid         NUMBER;
4917 
4918 BEGIN
4919    IF PG_DEBUG in ('Y', 'C') THEN
4920       arp_standard.debug( 'ARP_ALLOCATION_PKG.Set_Taxable_Split_Amt()+');
4921    END IF;
4922 
4923    OPEN set_taxable_split;
4924 
4925    l_prev_customer_trx_line_id := -9999;
4926    l_prev_tax_id := -9999;
4927    l_prev_tax_type := 'XXX';
4928    l_prev_ccid := -9999;
4929    l_prev_collected_ccid := -9999;
4930 
4931    LOOP
4932     --initialize record
4933 
4934       FETCH set_taxable_split BULK COLLECT INTO
4935             rowid_tbl,
4936             line_id_tbl,
4937             tax_id_tbl,
4938             tax_type_tbl,
4939             tax_ccid_tbl,
4940             tax_collected_ccid_tbl,
4941             taxable_amt_tbl,
4942             taxable_acctd_amt_tbl,
4943             taxable_amt_split_tbl,
4944             taxable_acctd_amt_split_tbl,
4945             taxable_amt_recov_tbl,
4946             taxable_acctd_amt_recov_tbl
4947         LIMIT g_bulk_fetch_rows;
4948 
4949       IF set_taxable_split%NOTFOUND THEN
4950          l_last_fetch := TRUE;
4951       END IF;
4952 
4953       IF (rowid_tbl.COUNT = 0) AND (l_last_fetch) THEN
4954          IF PG_DEBUG in ('Y', 'C') THEN
4955             arp_standard.debug('Set_Taxable_Split_Amt: ' || 'COUNT = 0 and LAST FETCH ');
4956          END IF;
4957          EXIT;
4958       END IF;
4959 
4960       FOR i IN rowid_tbl.FIRST .. rowid_tbl.LAST LOOP
4961         /*------------------------------------------------------------------+
4962          | For splits set the second, third splits to 0, so that only the   |
4963          | first split taxable will be used in sum function, therby ensuring|
4964          | that the taxable amount is counted only once in Build_Tax.       |
4965          +------------------------------------------------------------------*/
4966           IF l_prev_customer_trx_line_id = line_id_tbl(i)
4967               AND l_prev_tax_id = tax_id_tbl(i)
4968               AND l_prev_tax_type = tax_type_tbl(i) THEN
4969              taxable_amt_split_tbl(i)       := 0;
4970              taxable_acctd_amt_split_tbl(i) := 0;
4971 
4972              IF l_prev_ccid <> tax_ccid_tbl(i) THEN
4973                 taxable_amt_recov_tbl(i)       :=  taxable_amt_tbl(i);
4974                 taxable_acctd_amt_recov_tbl(i) := taxable_acctd_amt_tbl(i);
4975              END IF;
4976 
4977           ELSE
4978              taxable_amt_split_tbl(i)       := taxable_amt_tbl(i);
4979              taxable_acctd_amt_split_tbl(i) := taxable_acctd_amt_tbl(i);
4980           END IF;
4981 
4982           l_prev_customer_trx_line_id := line_id_tbl(i);
4983           l_prev_tax_id := tax_id_tbl(i);
4984           l_prev_tax_type := tax_type_tbl(i);
4985           l_prev_ccid := tax_ccid_tbl(i);
4986           l_prev_collected_ccid := tax_collected_ccid_tbl(i);
4987 
4988       END LOOP;
4989 
4990     --Bulk update
4991       FORALL m IN rowid_tbl.FIRST .. rowid_tbl.LAST
4992         UPDATE ar_ae_alloc_rec_gt
4993         SET ae_pro_split_taxable_amt       = taxable_amt_split_tbl(m),
4994             ae_pro_split_taxable_acctd_amt = taxable_acctd_amt_split_tbl(m),
4995             ae_pro_recov_taxable_amt       = taxable_amt_recov_tbl(m),
4996             ae_pro_recov_taxable_acctd_amt = taxable_acctd_amt_recov_tbl(m)
4997         WHERE rowid = rowid_tbl(m);
4998 
4999    --Exit if Last fetch
5000       IF l_last_fetch THEN
5001          EXIT;
5002       END IF;
5003 
5004    END LOOP; --Bulk fetch
5005 
5006    CLOSE set_taxable_split;
5007 
5008    IF PG_DEBUG in ('Y', 'C') THEN
5009       arp_standard.debug( 'ARP_ALLOCATION_PKG.Set_Taxable_Split_Amt()-');
5010    END IF;
5011 
5012 EXCEPTION
5013   WHEN OTHERS THEN
5014      IF PG_DEBUG in ('Y', 'C') THEN
5015         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Set_Taxable_Split_Amt');
5016      END IF;
5017      RAISE;
5018 
5019 END Set_Taxable_Split_Amt;
5020 
5021 /* ==========================================================================
5022  | PROCEDURE Allocate_Tax_To_Rev
5023  |
5024  | DESCRIPTION
5025  |      Allocate Tax amount over revenue amounts based on Rule tax code source
5026  |      NONE
5027  |
5028  | SCOPE - PRIVATE
5029  |
5030  | PARAMETERS
5031  |       p_type_acct      IN    Indicates accounting for earned, unearned
5032  |                              discounts or adjustments
5033  *==========================================================================*/
5034 PROCEDURE Allocate_Tax_To_Rev(p_type_acct      IN VARCHAR2) IS
5035 
5036 l_ctr  BINARY_INTEGER := 0;
5037 l_ctr1 BINARY_INTEGER := 0;
5038 l_ctr2 BINARY_INTEGER := 0;
5039 
5040 l_rev_run_amt_tot             NUMBER ;
5041 l_rev_run_pro_amt_tot         NUMBER ;
5042 l_rev_run_acctd_amt_tot       NUMBER ;
5043 l_rev_run_pro_acctd_amt_tot   NUMBER ;
5044 l_tax_amt_pro_rev             NUMBER ;
5045 l_tax_acctd_amt_pro_rev       NUMBER ;
5046 l_weight_amt                  NUMBER ;
5047 l_weight_acctd_amt            NUMBER ;
5048 l_base_amt                    NUMBER ;
5049 l_base_acctd_amt              NUMBER ;
5050 l_prev_tax_cust_trx_line_id   NUMBER ;
5051 l_prev_code_combination_id    NUMBER ;
5052 l_prev_collected_tax_ccid     NUMBER ;
5053 l_prev_tax_amt                NUMBER ;
5054 l_prev_tax_acctd_amt          NUMBER ;
5055 
5056 l_dummy                       VARCHAR2(1);
5057 l_rev_rowid                   VARCHAR2(50);
5058 
5059 l_not_found                   BOOLEAN := FALSE;
5060 
5061 g_ae_alloc_tax_tbl   ar_ae_alloc_rec_gt%ROWTYPE;
5062 g_ae_alloc_rev_tbl   ar_ae_alloc_rec_gt%ROWTYPE;
5063 g_ae_alloc_empty_tbl ar_ae_alloc_rec_gt%ROWTYPE;
5064 
5065 CURSOR alloc_tax_rev IS
5066        SELECT /*+ INDEX(ae1 AR_AE_ALLOC_REC_GT_N3) INDEX(ae2 AR_AE_ALLOC_REC_GT_N1) */
5067               ae2.rowid                          ,
5068               ae1.ae_customer_trx_line_id        ,
5069               ae1.ae_link_to_cust_trx_line_id    ,
5070               ae1.ae_code_combination_id         ,
5071               nvl(ae1.ae_collected_tax_ccid,-9999),
5072               ae1.ae_pro_amt                     ,
5073               ae1.ae_pro_acctd_amt               ,
5074               ae2.ae_sum_rev_amt                 ,
5075               ae2.ae_sum_rev_acctd_amt           ,
5076               ae2.ae_count                       ,
5077               ae2.ae_amount                      ,
5078               ae2.ae_acctd_amount
5079        FROM ar_ae_alloc_rec_gt ae1,
5080             ar_ae_alloc_rec_gt ae2
5081        WHERE ae1.ae_id = g_id
5082        AND   ae1.ae_account_class = 'TAX'
5083        AND   ae2.ae_id = ae1.ae_id
5084        AND   ae2.ae_account_class IN ('REVEARN','REVUNEARN') --MAINTAINTAXLINKID
5085        AND   ae1.ae_link_to_cust_trx_line_id = ae2.ae_customer_trx_line_id
5086        AND   ((ae1.ae_pro_amt <> 0)
5087                OR (ae1.ae_pro_acctd_amt <> 0))
5088        ORDER BY ae1.ae_customer_trx_line_id, ae1.ae_link_to_cust_trx_line_id;
5089 
5090 BEGIN
5091 
5092    IF PG_DEBUG in ('Y', 'C') THEN
5093       arp_standard.debug('Allocate_Tax_To_Rev: ' ||  'ARP_ALLOCATION_PKG.Alloc_Tax_To_Rev()+');
5094    END IF;
5095 
5096    IF (((p_type_acct = 'ED_ADJ') AND (g_ae_rule_rec.tax_code_source1 = 'NONE'))
5097        OR ((p_type_acct = 'UNED') AND (g_ae_rule_rec.tax_code_source2 = 'NONE'))) THEN
5098 
5099     /*------------------------------------------------------------------------------+
5100      | Allocate each tax line to the Revenue lines for an Invoice line              |
5101      +------------------------------------------------------------------------------*/
5102       l_tax_amt_pro_rev              := 0;
5103       l_tax_acctd_amt_pro_rev        := 0;
5104       l_rev_run_amt_tot              := 0;
5105       l_rev_run_acctd_amt_tot        := 0;
5106       l_rev_run_pro_amt_tot          := 0;
5107       l_rev_run_pro_acctd_amt_tot    := 0;
5108       l_prev_tax_amt                 := 0;
5109       l_prev_tax_acctd_amt           := 0;
5110 
5111       OPEN alloc_tax_rev;
5112 
5113       l_prev_tax_cust_trx_line_id := -9999;
5114       l_prev_code_combination_id  := -9999;
5115       l_prev_collected_tax_ccid   := -9999;
5116 
5117       LOOP
5118 
5119       --no need to initialize as a fetch will return a value
5120       --and for last row we want to retain the old values
5121 
5122       --fetch from cursor
5123          FETCH alloc_tax_rev
5124          INTO l_rev_rowid,
5125               g_ae_alloc_tax_tbl.ae_customer_trx_line_id     ,
5126               g_ae_alloc_tax_tbl.ae_link_to_cust_trx_line_id ,
5127               g_ae_alloc_tax_tbl.ae_code_combination_id      ,
5128               g_ae_alloc_tax_tbl.ae_collected_tax_ccid       ,
5129               g_ae_alloc_tax_tbl.ae_pro_amt                  ,
5130               g_ae_alloc_tax_tbl.ae_pro_acctd_amt            ,
5131               g_ae_alloc_rev_tbl.ae_sum_rev_amt              ,
5132               g_ae_alloc_rev_tbl.ae_sum_rev_acctd_amt        ,
5133               g_ae_alloc_rev_tbl.ae_count                    ,
5134               g_ae_alloc_rev_tbl.ae_amount                   ,
5135               g_ae_alloc_rev_tbl.ae_acctd_amount               ;
5136 
5137        --Set cursor not found flag
5138          IF alloc_tax_rev%NOTFOUND THEN
5139             l_not_found := TRUE;
5140          END IF;
5141 
5142        /*------------------------------------------------------------------------------+
5143         | If current tax line not equals previous then check for rounding corrections  |
5144         +------------------------------------------------------------------------------*/
5145          IF ((l_prev_tax_cust_trx_line_id <> g_ae_alloc_tax_tbl.ae_customer_trx_line_id)
5146              OR ((l_prev_tax_cust_trx_line_id = g_ae_alloc_tax_tbl.ae_customer_trx_line_id)
5147                  AND ((l_prev_code_combination_id <> g_ae_alloc_tax_tbl.ae_code_combination_id)
5148                        OR (l_prev_collected_tax_ccid <> g_ae_alloc_tax_tbl.ae_collected_tax_ccid)))
5149              OR (l_not_found))
5150            AND (l_prev_tax_cust_trx_line_id <> -9999) THEN
5151 
5152            /*------------------------------------------------------------------------------+
5153             | Abnormal rounding correction condition this should never happen, however this|
5154             | is a safety mechanism. Check that tax all tax is allocated over revenue lines|
5155             +------------------------------------------------------------------------------*/
5156              IF ((l_rev_run_pro_amt_tot <> l_prev_tax_amt) OR
5157                       (l_rev_run_pro_acctd_amt_tot <> l_prev_tax_acctd_amt))             THEN
5158 
5159                 IF PG_DEBUG in ('Y', 'C') THEN
5160                    arp_standard.debug('Allocate_Tax_To_Rev: ' || 'l_rev_run_pro_amt_tot :'
5161                    || l_rev_run_pro_amt_tot ||
5162                    ' <> ' || 'l_prev_tax_amt :'
5163                    || l_prev_tax_amt);
5164                    arp_standard.debug('Allocate_Tax_To_Rev: ' || 'l_rev_run_pro_acctd_amt_tot :'
5165                    || l_rev_run_pro_acctd_amt_tot ||
5166                    ' <> ' || 'l_prev_tax_acctd_amt :'
5167                    || l_prev_tax_acctd_amt);
5168                 END IF;
5169 
5170                 RAISE rounding_error;
5171 
5172              END IF; --End if rounding error
5173 
5174              l_tax_amt_pro_rev              := 0;
5175              l_tax_acctd_amt_pro_rev        := 0;
5176              l_rev_run_amt_tot              := 0;
5177              l_rev_run_acctd_amt_tot        := 0;
5178              l_rev_run_pro_amt_tot          := 0;
5179              l_rev_run_pro_acctd_amt_tot    := 0;
5180              l_prev_tax_amt                 := 0;
5181              l_prev_tax_acctd_amt           := 0;
5182 
5183           END IF; --prev tax line not equal to current tax line
5184 
5185         --cursor fetch returns not data
5186           IF l_not_found THEN
5187              EXIT; --loop
5188           END IF;
5189 
5190           l_weight_amt                   := 0;
5191           l_weight_acctd_amt             := 0;
5192           l_base_amt                     := 0;
5193           l_base_acctd_amt               := 0;
5194 
5195         /*------------------------------------------------------------------------------+
5196          | Set Revenue amount and accounted amounts totals                              |
5197          +------------------------------------------------------------------------------*/
5198           IF PG_DEBUG in ('Y', 'C') THEN
5199              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'Invoice line                      '
5200                                || g_ae_alloc_tax_tbl.ae_link_to_cust_trx_line_id);
5201              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'g_ae_alloc_rev_tbl.ae_sum_rev_amt '
5202                                || g_ae_alloc_rev_tbl.ae_sum_rev_amt);
5203              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'g_ae_alloc_rev_tbl.ae_sum_rev_acctd_amt '
5204                                || g_ae_alloc_rev_tbl.ae_sum_rev_acctd_amt);
5205              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'Tax amount            =           '
5206                                || g_ae_alloc_tax_tbl.ae_pro_amt);
5207              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'Tax accounted amount  =           '
5208                                || g_ae_alloc_tax_tbl.ae_pro_acctd_amt);
5209           END IF;
5210 
5211        /*------------------------------------------------------------------------------+
5212         | Set base to prorate tax over revenue, this is applicable when for an Invoice |
5213         | Line the revenue amount or accounted amount is zero so equal weights are     |
5214         | to the 0 distributions                                                       |
5215         +------------------------------------------------------------------------------*/
5216           IF g_ae_alloc_rev_tbl.ae_sum_rev_amt = 0 THEN
5217              l_weight_amt := 1;
5218              l_base_amt   := g_ae_alloc_rev_tbl.ae_count;
5219           ELSE
5220              l_weight_amt := g_ae_alloc_rev_tbl.ae_amount;
5221              l_base_amt   := g_ae_alloc_rev_tbl.ae_sum_rev_amt;
5222           END IF;
5223 
5224           IF g_ae_alloc_rev_tbl.ae_sum_rev_acctd_amt = 0 THEN
5225              l_weight_acctd_amt := 1;
5226              l_base_acctd_amt   := g_ae_alloc_rev_tbl.ae_count;
5227           ELSE
5228              l_weight_acctd_amt := g_ae_alloc_rev_tbl.ae_acctd_amount;
5229              l_base_acctd_amt   := g_ae_alloc_rev_tbl.ae_sum_rev_acctd_amt;
5230           END IF;
5231 
5232        /*------------------------------------------------------------------------------+
5233         | Maintain running total amounts for Revenue amounts and accounted amounts     |
5234         +------------------------------------------------------------------------------*/
5235           l_rev_run_amt_tot       := l_rev_run_amt_tot + l_weight_amt;
5236           l_rev_run_acctd_amt_tot := l_rev_run_acctd_amt_tot + l_weight_acctd_amt;
5237 
5238           l_tax_amt_pro_rev := arpcurr.CurrRound(l_rev_run_amt_tot / l_base_amt
5239                                   * g_ae_alloc_tax_tbl.ae_pro_amt,
5240                                   g_cust_inv_rec.invoice_currency_code) - l_rev_run_pro_amt_tot ;
5241 
5242           IF PG_DEBUG in ('Y', 'C') THEN
5243              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'l_tax_amt_pro_rev ' || l_tax_amt_pro_rev);
5244           END IF;
5245 
5246         /*------------------------------------------------------------------------------+
5247          | Running total for prorated Revenue amount in currency of Invoice             |
5248          +------------------------------------------------------------------------------*/
5249            l_rev_run_pro_amt_tot := l_rev_run_pro_amt_tot + l_tax_amt_pro_rev;
5250 
5251         /*------------------------------------------------------------------------------+
5252          | Calculate accounted amount for revenue amount allocated to each revenue line |
5253          +------------------------------------------------------------------------------*/
5254            l_tax_acctd_amt_pro_rev := arpcurr.CurrRound(l_rev_run_acctd_amt_tot /
5255                                         l_base_acctd_amt
5256                                         * g_ae_alloc_tax_tbl.ae_pro_acctd_amt,
5257                                         g_ae_sys_rec.base_currency) - l_rev_run_pro_acctd_amt_tot ;
5258            IF PG_DEBUG in ('Y', 'C') THEN
5259               arp_standard.debug('Allocate_Tax_To_Rev: ' || 'l_tax_acctd_amt_pro_rev ' || l_tax_acctd_amt_pro_rev);
5260            END IF;
5261 
5262         /*------------------------------------------------------------------------------+
5263          | Running total for prorated Revenue accounted amount in base currency         |
5264          +------------------------------------------------------------------------------*/
5265            l_rev_run_pro_acctd_amt_tot := l_rev_run_pro_acctd_amt_tot + l_tax_acctd_amt_pro_rev;
5266 
5267            l_prev_tax_cust_trx_line_id := g_ae_alloc_tax_tbl.ae_customer_trx_line_id;
5268            l_prev_code_combination_id  := g_ae_alloc_tax_tbl.ae_code_combination_id;
5269            l_prev_collected_tax_ccid   := g_ae_alloc_tax_tbl.ae_collected_tax_ccid;
5270            l_prev_tax_amt              := g_ae_alloc_tax_tbl.ae_pro_amt;
5271            l_prev_tax_acctd_amt        := g_ae_alloc_tax_tbl.ae_pro_acctd_amt;
5272 
5273         /*------------------------------------------------------------------------------+
5274          | Add tax allocated amount and accounted amount to revenue amounts allocated   |
5275          +------------------------------------------------------------------------------*/
5276            UPDATE ar_ae_alloc_rec_gt ae1
5277            SET ae1.ae_pro_amt       = ae1.ae_pro_amt + l_tax_amt_pro_rev,
5278                ae1.ae_pro_acctd_amt = ae1.ae_pro_acctd_amt + l_tax_acctd_amt_pro_rev
5279            WHERE ae1.rowid = l_rev_rowid;
5280 
5281       END LOOP; --End loop allocate tax
5282 
5283       CLOSE alloc_tax_rev;
5284 
5285    END IF; --End if TAX_CODE_SOURCE = 'NONE'
5286 
5287    IF PG_DEBUG in ('Y', 'C') THEN
5288       arp_standard.debug('Allocate_Tax_To_Rev: ' ||  'ARP_ALLOCATION_PKG.Alloc_Tax_To_Rev()-');
5289    END IF;
5290 
5291 EXCEPTION
5292   WHEN rounding_error THEN
5293      IF PG_DEBUG in ('Y', 'C') THEN
5294         arp_standard.debug('Allocate_Tax_To_Rev: ' || 'Rounding Error: ARP_ALLOCATION_PKG.Alloc_Tax_To_Rev' );
5295      END IF;
5296      fnd_message.set_name('AR','AR_ROUNDING_ERROR');
5297      fnd_message.set_token('ROUTINE','ARP_ALLOCATION_PKG.ALLOC_TAX_TO_REV');
5298      RAISE;
5299 
5300   WHEN OTHERS THEN
5301      IF PG_DEBUG in ('Y', 'C') THEN
5302         arp_standard.debug('Allocate_Tax_To_Rev: ' || 'EXCEPTION: ARP_ALLOCATION_PKG.Alloc_Tax_To_Rev');
5303      END IF;
5304      RAISE;
5305 
5306 END Allocate_Tax_To_Rev;
5307 
5308 /* ==========================================================================
5309  | PROCEDURE Set_Rev_Links
5310  |
5311  | DESCRIPTION
5312  |      Sets Actual Revenue link id's using rules and the tax link id total
5313  |      table. It is necessary to set the actual link id based on whether
5314  |      a given revenue line has associated tax allocations, if it does not
5315  |      then the link id is null.
5316  |
5317  | SCOPE - PRIVATE
5318  |
5319  | PARAMETERS
5320  |       p_type_acct      IN    Indicates accounting for earned, unearned
5321  |                              discounts or adjustments
5322  *==========================================================================*/
5323 PROCEDURE Set_Rev_Links(p_type_acct      IN VARCHAR2) IS
5324 
5325 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
5326 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
5327 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
5328 
5329 BEGIN
5330 
5331     IF PG_DEBUG in ('Y', 'C') THEN
5332        arp_standard.debug('ARP_ALLOCATION_PKG.Set_Rev_Links()+');
5333     END IF;
5334 
5335    /*-------------------------------------------------------------------------------+
5336     | Set Rules to determine whether Revenue link id needs to be set                |
5337     +-------------------------------------------------------------------------------*/
5338 /*
5339      Get_Rules(p_type_acct            => p_type_acct,
5340                p_gl_account_source    => l_gl_account_source,
5341                p_tax_code_source      => l_tax_code_source,
5342                p_tax_recoverable_flag => l_tax_recoverable_flag);
5343 */
5344 --}
5345     /*--------------------------------------------------------------------------------+
5346      | Set up actual link ids for revenue lines so that at summarization only required|
5347      | number of lines are created.                                                   |
5348      +--------------------------------------------------------------------------------*/
5349 --{HYUDETUPT
5350 --      UPDATE /*+ INDEX(ae1 AR_AE_ALLOC_REC_GT_N3) */
5351 --             ar_ae_alloc_rec_gt ae1
5352 --      SET ae1.ae_tax_link_id_act = decode(p_type_acct,
5353 --                                          'ED_ADJ', ae1.ae_tax_link_id_ed_adj,
5354 --                                          'UNED'  , ae1.ae_tax_link_id_uned  ,
5355 --                                          '')
5356 --      WHERE ae1.ae_id = g_id
5357 --      AND ae1.ae_account_class = 'REVEARN'
5358 --      AND (((ae1.ae_pro_amt <> 0) OR (ae1.ae_pro_acctd_amt <> 0))
5359 --           AND decode(p_type_acct,
5360 --                       'ED_ADJ', decode(ae1.ae_tax_link_id_ed_adj,
5361 --                                        '','N',
5362 --                                        'Y'),
5363 --                       'UNED', decode(ae1.ae_tax_link_id_uned,
5364 --                                      '','N',
5365 --                                      'Y'),
5366 --                       'N') = 'Y')
5367 --      AND EXISTS
5368 --              (SELECT /*+ INDEX(ae1 AR_AE_ALLOC_REC_GT_N3) */
5369 --                      ae2.ae_account_class
5370 --               FROM ar_ae_alloc_rec_gt ae2
5371 --               WHERE ae2.ae_account_class = 'TAX'
5372 --               AND ae2.ae_id = ae1.ae_id
5373 --               AND decode(p_type_acct,
5374 --                          'ED_ADJ', ae2.ae_tax_link_id_ed_adj,
5375 --                          'UNED'  , ae2.ae_tax_link_id_uned  ,
5376 --                          'Y') = decode(p_type_acct,
5377 --                                       'ED_ADJ', ae1.ae_tax_link_id_ed_adj,
5378 --                                       'UNED'  , ae1.ae_tax_link_id_uned  ,
5379 --                                       'N')
5380 --               AND (((l_tax_code_source = 'INVOICE')
5381 --                      OR (l_tax_code_source = 'ACTIVITY'))
5382 --                    OR ((l_tax_code_source = 'NONE')
5383 --                         AND (ae2.ae_collected_tax_ccid IS NOT NULL)
5384 --                         AND ((g_ae_rule_rec.line_amt_alloc <> 0)
5385 --                              OR (g_ae_rule_rec.line_acctd_amt_alloc <> 0)) ))
5386 --              );
5387 --}
5388 -- for a rev distribution ae_tax_link_id_act = ref_customer_trx_line_id
5389 -- if it exists a tax distribution with ae_tax_link_id equal to it
5390 arp_standard.debug('setlink 1');
5391 UPDATE /*+ index( a1 AR_AE_ALLOC_REC_GT_N4 ) */
5392    ar_ae_alloc_rec_gt a1
5393    SET (a1.ae_tax_link_id_act) =
5394        (SELECT /*+ index( a2 AR_AE_ALLOC_REC_GT_N4 ) */
5395             MAX(a2.ae_tax_link_id)
5396           FROM ar_ae_alloc_rec_gt a2
5397          WHERE a2.ae_id = g_id
5398            AND a2.ref_account_class = 'TAX'
5399            AND a1.ae_customer_trx_line_id = a2.ae_tax_link_id)
5400  WHERE a1.ae_id  = g_id
5401    AND a1.ref_account_class IN ('REV','UNEARN','UNBILL');
5402 
5403 /*MAINTAINTAXLINKID
5404 arp_standard.debug('setlink 2');
5405 -- for a tax distribution ae_tax_link_id_act = line_to_cust_trx_line_id
5406 -- if it exists a rev distribution with ref_customer_trx_line_id equal to it
5407 UPDATE ar_ae_alloc_rec_gt a1
5408    SET (a1.ae_tax_link_id_act) =
5409        (SELECT MAX(a2.ae_tax_link_id)
5410           FROM ar_ae_alloc_rec_gt a2
5411          WHERE a2.ae_id = g_id
5412            AND a2.ref_account_class IN ('REV','UNEARN','UNBILL')
5413            AND a1.ae_tax_link_id = a2.ae_customer_trx_line_id)
5414  WHERE a1.ae_id  = g_id
5415    AND a1.ref_account_class = 'TAX';
5416 */
5417 /*
5418 -- We need to update taxable_amount for tax line
5419 -- We need to create 2 buckets
5420 --  1: SUM(ae_pro_amt) where bucket IN (ED_LINE, UNED_LINE, APP_LINE)
5421 --  2: Individual elements ae_pro_amt where bucket IN (ED_TAX, UNED_TAX, APP_TAX)
5422 -- Group by ae_tax_link_id_act
5423 --  3: Proration using the elements 2 and allocation amt in 1
5424 -- ==> ae_pro_taxable_amt, ae_pro_split_taxable_amt,
5425 
5426 UPDATE ar_ae_alloc_rec_gt a1
5427    SET (a1.ae_tax_link_id_act,
5428         a1.AE_PRO_TAXABLE_AMT,
5429         a1.AE_PRO_TAXABLE_ACCTD_AMT,
5430         a1.AE_PRO_SPLIT_TAXABLE_AMT,
5431         a1.AE_PRO_SPLIT_TAXABLE_ACCTD_AMT,
5432         a1.AE_PRO_RECOV_TAXABLE_AMT,
5433         a1.AE_PRO_RECOV_TAXABLE_ACCTD_AMT) =
5434        (SELECT MAX(a2.ae_tax_link_id),
5435                MAX(a2.AE_PRO_AMT),
5436                MAX(a2.AE_PRO_ACCTD_AMT),
5437                MAX(a2.AE_PRO_AMT),
5438                MAX(a2.AE_PRO_ACCTD_AMT),
5439                MAX(a2.AE_PRO_AMT),
5440                MAX(a2.AE_PRO_ACCTD_AMT)
5441           FROM ar_ae_alloc_rec_gt a2
5442          WHERE a2.ae_id = g_id
5443            AND a2.ref_account_class = 'REV'
5444            AND a1.ae_tax_link_id = a2.ae_customer_trx_line_id)
5445  WHERE a1.ae_id  = g_id
5446    AND a1.ref_account_class = 'TAX';
5447 */
5448 
5449 arp_standard.debug(' setlink 3');
5450 --}
5451 
5452     IF PG_DEBUG in ('Y', 'C') THEN
5453        arp_standard.debug('ARP_ALLOCATION_PKG.Set_Rev_Links()-');
5454     END IF;
5455 
5456 EXCEPTION
5457   WHEN OTHERS THEN
5458      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Set_Rev_Links'||SQLERRM);
5459      RAISE;
5460 
5461 END Set_Rev_Links;
5462 
5463 /* ==========================================================================
5464  | PROCEDURE Get_Rules
5465  |
5466  | DESCRIPTION
5467  |      Gets the actual rule for the receivable activity
5468  |
5469  | SCOPE - PRIVATE
5470  |
5471  | PARAMETERS
5472  |    p_type_acct      IN    Indicates earned, unearned discount or adjustment
5473  |                           accounting
5474  *==========================================================================*/
5475 PROCEDURE Get_Rules(p_type_acct            IN     VARCHAR2,
5476                     p_gl_account_source    OUT NOCOPY   VARCHAR2,
5477                     p_tax_code_source      OUT NOCOPY   VARCHAR2,
5478                     p_tax_recoverable_flag OUT NOCOPY   VARCHAR2) IS
5479 
5480 BEGIN
5481    arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Rules()+');
5482 
5483  /*----------------------------------------------------------------------------+
5484   | Set Rules for Discounts, Adjustments and Finance charges                   |
5485   +----------------------------------------------------------------------------*/
5486    IF ((p_type_acct = 'ED_ADJ') OR (p_type_acct = 'PAY')) THEN
5487 
5488       p_gl_account_source    := g_ae_rule_rec.gl_account_source1    ;
5489       p_tax_code_source      := g_ae_rule_rec.tax_code_source1      ;
5490       p_tax_recoverable_flag := g_ae_rule_rec.tax_recoverable_flag1 ;
5491 
5492  /*----------------------------------------------------------------------------+
5493   | Set Rules for Unearned Discounts                                           |
5494   +----------------------------------------------------------------------------*/
5495    ELSIF p_type_acct = 'UNED' THEN
5496 
5497       p_gl_account_source           := g_ae_rule_rec.gl_account_source2    ;
5498       p_tax_code_source             := g_ae_rule_rec.tax_code_source2      ;
5499       p_tax_recoverable_flag        := g_ae_rule_rec.tax_recoverable_flag2 ;
5500 
5501    END IF;
5502 
5503    arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Rules()-');
5504 
5505 EXCEPTION
5506   WHEN OTHERS THEN
5507      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Get_Rules');
5508      RAISE;
5509 
5510 END Get_Rules;
5511 
5512 /* ==========================================================================
5513  | PROCEDURE Build_Lines
5514  |
5515  | DESCRIPTION
5516  |      Build actual accounting entries for Tax accounting for discounts and
5517  |      adjustments
5518  |
5519  | SCOPE - PRIVATE
5520  |
5521  | PARAMETERS
5522  |    p_type_acct      IN    Indicates earned, unearned discount or adjustment
5523  |                           accounting
5524  *==========================================================================*/
5525 PROCEDURE Build_Lines IS --(p_type_acct      IN VARCHAR2) IS
5526 
5527 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
5528 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
5529 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
5530 l_ae_line_init_rec     ar_ae_alloc_rec_gt%ROWTYPE                     ;
5531 
5532 BEGIN
5533 
5534     arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Lines()+');
5535 
5536 --  Get_Rules(p_type_acct            => p_type_acct,
5537 --            p_gl_account_source    => l_gl_account_source,
5538 --            p_tax_code_source      => l_tax_code_source,
5539 --            p_tax_recoverable_flag => l_tax_recoverable_flag);
5540 
5541  /*----------------------------------------------------------------------------+
5542   | Assign Currency Exchange rate information to initialisation record         |
5543   +----------------------------------------------------------------------------*/
5544    l_ae_line_init_rec.ae_id                        := g_id                                 ;
5545    l_ae_line_init_rec.ae_source_id                 := g_ae_doc_rec.source_id               ;
5546    l_ae_line_init_rec.ae_source_table              := g_ae_doc_rec.source_table            ;
5547    l_ae_line_init_rec.ae_currency_code             := g_cust_inv_rec.invoice_currency_code ;
5548    l_ae_line_init_rec.ae_currency_conversion_rate  := g_cust_inv_rec.exchange_rate         ;
5549    l_ae_line_init_rec.ae_currency_conversion_type  := g_cust_inv_rec.exchange_rate_type    ;
5550    l_ae_line_init_rec.ae_currency_conversion_date  := g_cust_inv_rec.exchange_date         ;
5551 
5552 --Set Third party details
5553    IF (g_cust_inv_rec.drawee_site_use_id IS NOT NULL) THEN --if Bill
5554       l_ae_line_init_rec.ae_third_party_id            := g_cust_inv_rec.drawee_id;
5555       l_ae_line_init_rec.ae_third_party_sub_id        := g_cust_inv_rec.drawee_site_use_id  ;
5556    ELSE
5557       l_ae_line_init_rec.ae_third_party_id            := g_cust_inv_rec.bill_to_customer_id   ;
5558       l_ae_line_init_rec.ae_third_party_sub_id        := g_cust_inv_rec.bill_to_site_use_id   ;
5559    END IF;
5560 
5561  /*----------------------------------------------------------------------------+
5562   | Build Revenue lines based on rules. For payments only deferred tax is to be|
5563   | moved so don not call build revenue routine at all                         |
5564   +----------------------------------------------------------------------------*/
5565     Build_Rev(p_gl_account_source     => l_gl_account_source     ,
5566               p_tax_code_source       => l_tax_code_source       ,
5567               p_tax_recoverable_flag  => l_tax_recoverable_flag  ,
5568               p_ae_line_init_rec      => l_ae_line_init_rec        );
5569 
5570  /*----------------------------------------------------------------------------+
5571   | Build Tax lines based on rules, if boundary condition then only deferred   |
5572   | tax lines will be created, with taxable off the original Invoice.          |
5573   +----------------------------------------------------------------------------*/
5574    Build_Tax(p_tax_code_source       => l_tax_code_source       ,
5575              p_tax_recoverable_flag  => l_tax_recoverable_flag  ,
5576              p_ae_line_init_rec      => l_ae_line_init_rec        );
5577 
5578  IF PG_DEBUG in ('Y', 'C') THEN
5579     arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Lines()-');
5580  END IF;
5581 
5582 EXCEPTION
5583   WHEN OTHERS THEN
5584      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Build_Lines');
5585      RAISE;
5586 
5587 END Build_Lines;
5588 
5589 /* ==========================================================================
5590  | PROCEDURE Build_Rev
5591  |
5592  | DESCRIPTION
5593  |    Build actual accounting entries for Revenue or line amounts for
5594  |    discounts and adjustments
5595  |
5596  | SCOPE - PRIVATE
5597  |
5598  | PARAMETERS
5599  |    p_type_acct             IN    Indicates earned, unearned discount or
5600  |                                  adjustment accounting
5601  |    p_gl_account_source     IN    Source of gl account
5602  |    p_tax_recoverable_flag  IN    Indicates whether TAX is recoverable
5603  |    p_ae_line_init_rec      IN    Initialization record contains details
5604  |                                  for exchange rate, source table, id
5605  |                                  common to all accounting entries
5606  *==========================================================================*/
5607 PROCEDURE Build_Rev(p_gl_account_source     IN ar_receivables_trx.gl_account_source%TYPE     ,
5608                     p_tax_code_source       IN ar_receivables_trx.tax_code_source%TYPE       ,
5609                     p_tax_recoverable_flag  IN ar_receivables_trx.tax_recoverable_flag%TYPE  ,
5610                     p_ae_line_init_rec      IN ar_ae_alloc_rec_gt%ROWTYPE                        ) IS
5611 
5612 l_ae_line_rec         ar_ae_alloc_rec_gt%ROWTYPE                  ;
5613 l_ctr                 BINARY_INTEGER                            ;
5614 l_actual_account      ar_distributions.code_combination_id%TYPE ;
5615 l_source_type         ar_distributions.source_type%TYPE         ;
5616 
5617 --PL/SQL table object,this collection is used for bulk processing of inserts
5618 TYPE ae_alloc_rec_gt_tab IS TABLE OF ar_ae_alloc_rec_gt%ROWTYPE INDEX BY BINARY_INTEGER;
5619 l_ae_alloc_rec_gt_tab ae_alloc_rec_gt_tab;
5620 l_ae_alloc_empty_tab  ae_alloc_rec_gt_tab;
5621 
5622 l_last_fetch     BOOLEAN := FALSE;
5623 l_bulk_index     NUMBER := 0;
5624 
5625 l_line_charges_amt          NUMBER := 0;
5626 l_line_charges_acctd_amt    NUMBER := 0;
5627 l_freight_charges_amt       NUMBER := 0;
5628 l_freight_charges_acctd_amt NUMBER := 0;
5629 
5630 /*-------------------------------------------------------------------------------+
5631  | Summarize Revenue lines, this routine would be executed even in the case where|
5632  | revenue base amounts are zero, so the single accounting entry to activity GL  |
5633  | Account is built later, however if tax code source is NONE then there may be  |
5634  | tax allocated to its revenue, hence need to summarize. Note for payments we   |
5635  | dont need to summarize as only deferred tax is moved, However no additional   |
5636  | accounting is required.                                                       |
5637  +-------------------------------------------------------------------------------*/
5638   CURSOR l_summarize_rev IS
5639   SELECT /*+ INDEX(ae1 AR_AE_ALLOC_REC_GT_N3) */
5640          ae1.ae_tax_link_id_act,
5641          ae1.ae_customer_trx_line_id,
5642          ae1.ae_cust_trx_line_gl_dist_id,
5643          ae1.ae_ref_line_id,
5644          ae1.ref_account_class,
5645          ae1.activity_bucket,
5646          ae1.ref_dist_ccid,
5647          ae1.ref_mf_dist_flag,
5648          DECODE(DECODE(ae1.activity_bucket,
5649                  'APP_LINE' , 'ACTIVITY_GL_ACCOUNT',
5650                  'APP_TAX'  , 'ACTIVITY_GL_ACCOUNT',
5651                  'APP_FRT'  , 'ACTIVITY_GL_ACCOUNT',
5652                  'APP_CHRG' , 'ACTIVITY_GL_ACCOUNT',
5653                  'ADJ_LINE' , g_ae_rule_rec.gl_account_source1,
5654                  'ADJ_TAX'  , g_ae_rule_rec.gl_account_source1,
5655                  'ADJ_FRT'  , g_ae_rule_rec.gl_account_source1,
5656                  'ADJ_CHRG' , g_ae_rule_rec.gl_account_source1,
5657                  'ED_LINE'  , g_ae_rule_rec.gl_account_source1,
5658                  'ED_TAX'   , g_ae_rule_rec.gl_account_source1,
5659                  'ED_FRT'   , g_ae_rule_rec.gl_account_source1,
5660                  'ED_CHRG'  , g_ae_rule_rec.gl_account_source1,
5661                  'UNED_LINE', g_ae_rule_rec.gl_account_source2,
5662                  'UNED_TAX' , g_ae_rule_rec.gl_account_source2,
5663                  'UNED_FRT' , g_ae_rule_rec.gl_account_source2,
5664                  'UNED_CHRG', g_ae_rule_rec.gl_account_source2,
5665                               g_ae_rule_rec.gl_account_source1),
5666                 'TAX_CODE_ON_INVOICE',DECODE(ae1.activity_bucket,
5667                                         'ADJ_LINE' , ae1.ae_override_ccid1,
5668                                         'ADJ_TAX'  , ae1.ae_override_ccid1,
5669                                         'ADJ_FRT'  , ae1.ae_override_ccid1,
5670                                         'ADJ_CHRG' , ae1.ae_override_ccid1,
5671                                         'ED_LINE'  , ae1.ae_override_ccid1,
5672                                         'ED_TAX'   , ae1.ae_override_ccid1,
5673                                         'ED_FRT'   , ae1.ae_override_ccid1,
5674                                         'ED_CHRG'  , ae1.ae_override_ccid1,
5675                                                      ae1.ae_override_ccid2),
5676                 'ACTIVITY_GL_ACCOUNT',DECODE(ae1.activity_bucket,
5677                                        'ADJ_LINE' , ae1.ae_override_ccid1,
5678                                        'ADJ_TAX'  , ae1.ae_override_ccid1,
5679                                        'ADJ_FRT'  , ae1.ae_override_ccid1,
5680                                        'ADJ_CHRG' , ae1.ae_override_ccid1,
5681                                        'ED_LINE'  , ae1.ae_override_ccid1,
5682                                        'ED_TAX'   , ae1.ae_override_ccid1,
5683                                        'ED_FRT'   , ae1.ae_override_ccid1,
5684                                        'ED_CHRG'  , ae1.ae_override_ccid1,
5685                                        'APP_LINE' , g_ae_rule_rec.receivable_account,
5686                                        'APP_TAX'  , g_ae_rule_rec.receivable_account,
5687                                        'APP_FRT'  , g_ae_rule_rec.receivable_account,
5688                                        'APP_CHRG' , g_ae_rule_rec.receivable_account,
5689                                                     ae1.ae_override_ccid2),
5690                 'REVENUE_ON_INVOICE',ae1.ae_code_combination_id,
5691                 '') actual_account,
5692          nvl(ae1.ae_pro_amt,0) ae_pro_amt,
5693          nvl(ae1.ae_pro_acctd_amt,0) ae_pro_acctd_amt,
5694          nvl(ae1.ae_pro_frt_chrg_amt,0) ae_pro_frt_chrg_amt,
5695          nvl(ae1.ae_pro_frt_chrg_acctd_amt,0) ae_pro_frt_chrg_acctd_amt,
5696          nvl(ae1.ae_from_pro_amt,0) ae_from_pro_amt,
5697          nvl(ae1.ae_from_pro_acctd_amt,0) ae_from_pro_acctd_amt,
5698          nvl(ae1.ae_from_pro_chrg_amt,0) ae_from_pro_chrg_amt,
5699          nvl(ae1.ae_from_pro_chrg_acctd_amt,0) ae_from_pro_chrg_acctd_amt
5700   FROM   ar_ae_alloc_rec_gt ae1
5701   WHERE ae1.ae_id = g_id
5702   AND   ae1.ae_account_class IN ('REVEARN',
5703                                  'FREIGHT',
5704                                  'REVUNEARN',
5705                                  'CHARGES');
5706 --  AND (((g_exec = 'ADJ')
5707 --       AND (ae_ref_line_id IS NULL))
5708 --       OR (p_type_acct = 'PAY' OR g_exec = 'ED' OR g_exec = 'UNED')) ;
5709 
5710 g_ae_summ_rev_tbl   l_summarize_rev%ROWTYPE;
5711 
5712 TYPE summarize_rev_cur IS TABLE OF l_summarize_rev%ROWTYPE;
5713 g_ae_summ_rev_blk_tbl summarize_rev_cur;
5714 
5715 BEGIN
5716 
5717  arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Rev()+');
5718 
5719  adj_code_combination_id := '';
5720 
5721  /*----------------------------------------------------------------------------+
5722   | Process all revenue lines to be built                                      |
5723   +----------------------------------------------------------------------------*/
5724    arp_standard.debug('Open l_summarize_rev +');
5725 
5726    OPEN l_summarize_rev;
5727 
5728    LOOP
5729    FETCH l_summarize_rev BULK COLLECT INTO g_ae_summ_rev_blk_tbl LIMIT g_bulk_fetch_rows;
5730 
5731    --reinitialize
5732    l_ae_alloc_rec_gt_tab := l_ae_alloc_empty_tab;
5733    l_bulk_index := 0;
5734 
5735       IF l_summarize_rev%NOTFOUND THEN
5736          l_last_fetch := TRUE;
5737       END IF;
5738 
5739    FOR i IN 1..g_ae_summ_rev_blk_tbl.count LOOP
5740       --Assign the record from PL/SQL table to a cursor variable,this variable
5741       --is used for accessing the current record data in the following code
5742       g_ae_summ_rev_tbl := g_ae_summ_rev_blk_tbl(i);
5743       l_actual_account  := g_ae_summ_rev_tbl.actual_account;
5744 
5745 
5746 /*----------------------------------------------------------------------------+
5747  | Populate source type for earned discounts                                  |
5748  +----------------------------------------------------------------------------*/
5749   IF g_ae_summ_rev_tbl.activity_bucket IN ('ED_LINE', 'ED_TAX', 'ED_FRT','ED_CHRG')  THEN
5750      l_source_type                 := 'EDISC' ;
5751     --Set the Activity GL Account
5752     -- l_actual_account := g_ae_rule_rec.code_combination_id1;
5753   /*----------------------------------------------------------------------------+
5754    | Populate source type for finance charges                                   |
5755    +----------------------------------------------------------------------------*/
5756   ELSIF g_ae_summ_rev_tbl.activity_bucket IN ('ADJ_LINE', 'ADJ_TAX', 'ADJ_FRT','ADJ_CHRG') THEN
5757 
5758      IF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
5759            l_source_type              := 'ADJ';
5760      ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
5761            l_source_type              := 'FINCHRG';
5762      END IF;
5763 
5764      IF g_ae_summ_rev_tbl.ae_cust_trx_line_gl_dist_id = -9 THEN
5765      --For adjustment boundary ccid
5766      adj_boundary_account
5767      (p_receivables_trx_id   => g_receivables_trx_id,
5768       p_bucket               => g_ae_summ_rev_tbl.activity_bucket,
5769       p_ctlgd_id             => g_ae_summ_rev_tbl.ae_cust_trx_line_gl_dist_id,
5770       x_ccid                 => l_actual_account);
5771      END IF;
5772 
5773     --Set the Activity GL Account
5774     IF l_actual_account IS NULL THEN
5775      l_actual_account := g_ae_rule_rec.code_combination_id1;
5776     END IF;
5777 
5778 
5779  /*----------------------------------------------------------------------------+
5780   | Populate source type for unearned discounts                                |
5781   +----------------------------------------------------------------------------*/
5782   ELSIF g_ae_summ_rev_tbl.activity_bucket IN ('UNED_LINE', 'UNED_TAX', 'UNED_FRT','UNED_CHRG') THEN
5783      l_source_type                 := 'UNEDISC' ;
5784   END IF;
5785 
5786 
5787 
5788 
5789     /*----------------------------------------------------------------------------+
5790      | Initialize record with exchange rate, source id, table details for new line|
5791      +----------------------------------------------------------------------------*/
5792        l_ae_line_rec                             := p_ae_line_init_rec;
5793        l_ae_line_rec.ae_line_type                := l_source_type;
5794        l_ae_line_rec.ref_dist_ccid               := g_ae_summ_rev_tbl.ref_dist_ccid;
5795        l_ae_line_rec.ref_mf_dist_flag            := g_ae_summ_rev_tbl.ref_mf_dist_flag;
5796 
5797      /*----------------------------------------------------------------------------+
5798       | Set Tax link id                                                            |
5799       +----------------------------------------------------------------------------*/
5800        l_ae_line_rec.ae_tax_link_id              := g_ae_summ_rev_tbl.ae_tax_link_id_act;
5801        l_ae_line_rec.ae_customer_trx_line_id     := g_ae_summ_rev_tbl.ae_customer_trx_line_id;
5802        l_ae_line_rec.ae_cust_trx_line_gl_dist_id := g_ae_summ_rev_tbl.ae_cust_trx_line_gl_dist_id;
5803        l_ae_line_rec.ae_ref_line_id              := g_ae_summ_rev_tbl.ae_ref_line_id;
5804        l_ae_line_rec.ae_tax_link_id              := g_ae_summ_rev_tbl.ae_tax_link_id_act;
5805        l_ae_line_rec.ref_account_class           := g_ae_summ_rev_tbl.ref_account_class;
5806        l_ae_line_rec.activity_bucket             := g_ae_summ_rev_tbl.activity_bucket;
5807        l_ae_line_rec.ae_account                  := g_ae_rule_rec.receivable_account;
5808 
5809 
5810      arp_standard.debug(' g_ae_rule_rec.receivable_account 1:'||g_ae_rule_rec.receivable_account);
5811      arp_standard.debug(' p_gl_account_source :'||p_gl_account_source);
5812      arp_standard.debug(' g_override1 :'||g_override1);
5813      arp_standard.debug(' g_override2 :'||g_override2);
5814      arp_standard.debug(' g_ae_doc_rec.other_flag :'||g_ae_doc_rec.other_flag);
5815      arp_standard.debug(' l_actual_account :'||l_actual_account);
5816 
5817      /*----------------------------------------------------------------------------+
5818       | Use Revenue or Net Expense accounts based on Rules                         |
5819       +----------------------------------------------------------------------------*/
5820        IF ((the_gl_account_source(g_ae_summ_rev_tbl.activity_bucket) = 'ACTIVITY_GL_ACCOUNT') OR
5821            (the_gl_account_source(g_ae_summ_rev_tbl.activity_bucket) = 'TAX_CODE_ON_INVOICE'))
5822           AND (((g_ae_summ_rev_tbl.activity_bucket IN ('ED_LINE','ED_TAX','ED_FRT','ED_CHRG',
5823                                                        'ADJ_FRT','ADJ_CHRG','ADJ_LINE')) --BUG#5245153
5824                  AND (nvl(g_override1,'N') = 'Y'))
5825                 OR (( g_ae_summ_rev_tbl.activity_bucket IN ('UNED_LINE','UNED_TAX','UNED_FRT','UNED_CHRG'))
5826                     AND (nvl(g_override2, 'N') = 'Y'))) THEN
5827 
5828            IF l_actual_account IS NULL THEN
5829               RAISE invalid_ccid_error;
5830            END IF;
5831 
5832          /*------------------------------------------------------------------------------------+
5833           | Substitute balancing segment for Net Expense account or Activity Gl Account        |
5834           | in the case of Deposits and Guarantees the accounting is derived by Autoaccounting |
5835           | there is no need to do further processing for accounting.                          |
5836           +------------------------------------------------------------------------------------*/
5837             IF (g_ae_doc_rec.other_flag IN ('COMMITMENT', 'CHARGEBACK', 'CBREVERSAL')) THEN
5838                arp_standard.debug('Build_Rev: ' || 'Commitment account derived by Autoaccounting');
5839                l_ae_line_rec.ae_account  := l_actual_account;
5840 
5841             ELSE
5842 	    -- Bugfix 1948917.
5843 	    IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
5844                Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id             ,
5845                                p_original_ccid => l_actual_account                ,
5846                                p_subs_ccid     => g_ae_rule_rec.receivable_account,
5847                                p_actual_ccid   => l_ae_line_rec.ae_account             );
5848 
5849           --BUG#5245153  /*Bug 10636631*/
5850           IF (l_ae_line_rec.ae_account <> l_actual_account) AND
5851              (the_gl_account_source(g_ae_summ_rev_tbl.activity_bucket) = 'ACTIVITY_GL_ACCOUNT') AND
5852              (g_ae_summ_rev_tbl.activity_bucket in ( 'ADJ_LINE','ADJ_CHRG') ) AND
5853              (g_ae_doc_rec.source_table = 'ADJ')
5854           THEN
5855              IF get_adj_act_ccid <> l_ae_line_rec.ae_account THEN
5856                BEGIN
5857                 UPDATE ar_adjustments
5858                    SET code_Combination_id = l_ae_line_rec.ae_account
5859                  WHERE adjustment_id = g_ae_doc_rec.document_id;
5860                  g_adj_act_gl_acct_ccid := l_ae_line_rec.ae_account;
5861 
5862 		/* Bug 10636631 : Initialize the global variable to show substituted CCID on Adjustment forms upon save. */
5863                  adj_code_combination_id := l_ae_line_rec.ae_account;
5864                  arp_standard.debug('adj_code_combination_id : ' || adj_code_combination_id);
5865                EXCEPTION
5866                  WHEN no_data_found THEN g_adj_act_gl_acct_ccid := l_ae_line_rec.ae_account;
5867                END;
5868              END IF;
5869           END IF;
5870 
5871 	    ELSE
5872 	       l_ae_line_rec.ae_account := l_actual_account;
5873 	    END IF;
5874 
5875             END IF;
5876 
5877 
5878        ELSIF the_gl_account_source(g_ae_summ_rev_tbl.activity_bucket) = 'REVENUE_ON_INVOICE' THEN
5879                l_ae_line_rec.ae_account := l_actual_account;
5880        ELSIF  the_gl_account_source(g_ae_summ_rev_tbl.activity_bucket) = 'ACTIVITY_GL_ACCOUNT'
5881            AND  g_ae_summ_rev_tbl.activity_bucket IN ('APP_LINE','APP_TAX','APP_FRT','APP_CHRG')
5882                 --{BUG#5122552
5883 -- BUG#5245153 ,'ADJ_FRT','ADJ_CHRG','ADJ_LINE')
5884                 --}
5885            THEN
5886                l_ae_line_rec.ae_account := l_actual_account;
5887        END IF;
5888 
5889       /*----------------------------------------------------------------------------+
5890        | Set the activity ccid which will be stamped on adj.code_combination_id     |
5891        +----------------------------------------------------------------------------*/
5892         IF adj_code_combination_id IS NULL and p_gl_account_source = 'ACTIVITY_GL_ACCOUNT'
5893            AND (((nvl(g_ae_summ_rev_tbl.ae_pro_amt,0) <> 0)
5894                   OR (nvl(g_ae_summ_rev_tbl.ae_pro_acctd_amt,0) <> 0))
5895                 OR ((nvl(g_ae_summ_rev_tbl.ae_pro_frt_chrg_amt,0) <> 0)
5896                      OR (nvl(g_ae_summ_rev_tbl.ae_pro_frt_chrg_acctd_amt,0) <> 0)))
5897         THEN
5898            adj_code_combination_id := l_ae_line_rec.ae_account;
5899         END IF;
5900 
5901        /*----------------------------------------------------------------------------+
5902         | Assign Accounting Debits and Credits based on prorated amount signs        |
5903         +----------------------------------------------------------------------------*/
5904          IF ((nvl(g_ae_summ_rev_tbl.ae_pro_amt,0) <> 0)
5905                                      OR (nvl(g_ae_summ_rev_tbl.ae_pro_acctd_amt,0) <> 0)) THEN
5906 
5907            --IF p_type_acct = 'PAY' THEN
5908            IF g_ae_summ_rev_tbl.activity_bucket IN ('APP_LINE','APP_TAX','APP_FRT','APP_CHRG') THEN
5909 
5910               -- ARALLOCB creates the REC account entries for APPS so for a cash application
5911               -- the accounting needs to be positive CR REC
5912               -- As ARPDDB passed the detail distributions as negative, we need to multiply by -1
5913               Create_Debits_Credits(g_ae_summ_rev_tbl.ae_pro_amt       * -1,
5914                                     g_ae_summ_rev_tbl.ae_pro_acctd_amt * -1,
5915                                     ''                                 ,
5916                                     ''                                 ,
5917                                     g_ae_summ_rev_tbl.ae_from_pro_amt  * -1,
5918                                     g_ae_summ_rev_tbl.ae_from_pro_acctd_amt * -1,
5919                                     l_ae_line_rec );
5920 
5921               l_ae_line_rec.ae_line_type := 'REC';
5922 
5923 	       -- Bug 6598080
5924               IF g_ae_doc_rec.called_from = 'WRAPPER' THEN
5925                  l_ae_line_rec.ae_account := g_ae_code_combination_id_app;
5926               END IF;
5927 
5928             ELSE
5929              -- in the case of ADJ, ARALLOCB creates the Write off accounting
5930              -- for negative adjustment <=> CR REC <=> DB WO, in this case the distribution should negative
5931              -- As ARPDDB passes the detail distributions in the sign of the header adjustment that is negative
5932              -- so no need to multiply by -1
5933 
5934              Create_Debits_Credits(g_ae_summ_rev_tbl.ae_pro_amt        ,
5935                                    g_ae_summ_rev_tbl.ae_pro_acctd_amt  ,
5936                                    ''                                  ,
5937                                    ''                                  ,
5938                                     g_ae_summ_rev_tbl.ae_from_pro_amt       ,
5939                                     g_ae_summ_rev_tbl.ae_from_pro_acctd_amt ,
5940                                    l_ae_line_rec );
5941            END IF;
5942 
5943        /*----------------------------------------------------------------------------+
5944         | Assign built Revenue line to global lines table                            |
5945         +----------------------------------------------------------------------------*/
5946              --Commented out the call,Instead will do bulk binding to insert the records
5947 	     --Assign_Elements(l_ae_line_rec);
5948              l_ae_line_rec.ae_id :=  g_id;
5949 	     l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
5950 
5951 	     l_bulk_index := l_bulk_index + 1;
5952 	     l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
5953 
5954       END IF; --End if ae_pro_amt or ae_pro_acctd_amt is non zero
5955 
5956      END LOOP;--end of pl/sql table loop
5957 
5958      /**In procedure Assign_Elements,g_ae_ctr is incremented for each record
5959         inserted.But no code segment in this package uses the value of the
5960 	variable,thus not incremented the variable here.*/
5961      FORALL i IN l_ae_alloc_rec_gt_tab.first..l_ae_alloc_rec_gt_tab.last
5962 	INSERT INTO ar_ae_alloc_rec_gt VALUES l_ae_alloc_rec_gt_tab(i);
5963 
5964      --Exit Last fetch
5965      IF l_last_fetch THEN
5966             EXIT;
5967      END IF;
5968 
5969        --Now build freight and charges if any
5970 --         IF ((nvl(g_ae_summ_rev_tbl.ae_pro_frt_chrg_amt,0) <> 0)
5971 --                                     OR (nvl(g_ae_summ_rev_tbl.ae_pro_frt_chrg_acctd_amt,0) <> 0)) THEN
5972 --          --Link id null always for freight and charges
5973 --             l_ae_line_rec.ae_tax_link_id := '';
5974 --             IF p_type_acct = 'PAY' THEN
5975            /*----------------------------------------------------------------------------+
5976             | Assign Accounting Debits and Credits based on prorated amount signs        |
5977             +----------------------------------------------------------------------------*/
5978 --                Create_Debits_Credits(g_ae_summ_rev_tbl.ae_pro_chrg_amt * -1          ,
5979 --                                      g_ae_summ_rev_tbl.ae_pro_chrg_acctd_amt * -1    ,
5980 --                                      ''                                         ,
5981 --                                      ''                                         ,
5982 --                                      g_ae_summ_rev_tbl.ae_from_pro_chrg_amt * -1          ,
5983 --                                      g_ae_summ_rev_tbl.ae_from_pro_chrg_acctd_amt * -1    ,
5984 --                                      l_ae_line_rec );
5985 --                l_ae_line_rec.ae_line_type := 'REC';
5986 --
5987 --             ELSE
5988 --}
5989            /*----------------------------------------------------------------------------+
5990             | Assign Accounting Debits and Credits based on prorated amount signs        |
5991             +----------------------------------------------------------------------------*/
5992 --             Create_Debits_Credits(g_ae_summ_rev_tbl.ae_pro_frt_chrg_amt        ,
5993 --                                   g_ae_summ_rev_tbl.ae_pro_frt_chrg_acctd_amt  ,
5994 --                                   ''                                         ,
5995 --                                   ''                                         ,
5996 --                                   --HYU--{
5997 --                                   g_ae_summ_rev_tbl.ae_from_pro_chrg_amt,
5998 --                                   g_ae_summ_rev_tbl.ae_from_pro_chrg_acctd_amt,
5999 --                                   l_ae_line_rec );
6000 --
6001 --           END IF; --HYUDETUPT
6002            --Create the freight and charges
6003 --             Assign_Elements(l_ae_line_rec);
6004 --         END IF; --freight and charges prorates are not zero
6005 
6006    END LOOP;
6007 
6008 -- END IF; --End if Build Revenue lines
6009 
6010   arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Rev()-');
6011 
6012 EXCEPTION
6013   WHEN invalid_ccid_error THEN
6014      arp_standard.debug('Invalid Tax ccid - ARP_ALLOCATION_PKG.Build_Rev' );
6015      fnd_message.set_name('AR','AR_INVALID_TAX_ACCOUNT');
6016      RAISE;
6017 
6018   WHEN OTHERS THEN
6019      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Build_Rev');
6020      RAISE;
6021 
6022 END Build_Rev;
6023 
6024 
6025 /*--------------------------------------------------------------------------+
6026  | Tax distrition ccid substitute ccid                                      |
6027  +--------------------------------------------------------------------------*/
6028 PROCEDURE substite_tax_bal_seg
6029 (p_line_type       IN     VARCHAR2,
6030  p_gas             IN     VARCHAR2,
6031  p_tcs             IN     VARCHAR2,
6032  p_tax_rec_flag    IN     VARCHAR2,
6033  p_ccid            IN     NUMBER,
6034  x_ccid            OUT  NOCOPY  NUMBER)
6035 IS
6036   l_call   VARCHAR2(1) := 'Y';
6037 BEGIN
6038   arp_standard.debug('substite_tax_bal_seg       +');
6039   arp_standard.debug('p_line_type       :'||p_line_type);
6040   arp_standard.debug('p_gas             :'||p_gas);
6041   arp_standard.debug('p_tcs             :'||p_tcs);
6042   arp_standard.debug('p_tax_rec_flag    :'||p_tax_rec_flag);
6043   arp_standard.debug('p_ccid            :'||p_ccid);
6044 
6045   x_ccid  := p_ccid;
6046 
6047    IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
6048 
6049      IF p_line_type IN ('TAX', 'DEFERRED_TAX') THEN
6050          l_call := 'N';
6051      ELSE
6052 
6053        IF     p_gas    = 'ACTIVITY_GL_ACCOUNT' THEN
6054          IF     p_tcs    = 'NONE'                THEN     l_call := 'Y';
6055          ELSIF  p_tcs    = 'ACTIVITY'            THEN     l_call := 'Y';
6056          ELSIF  p_tcs    = 'INVOICE'             THEN
6057              IF   p_tax_rec_flag = 'Y'  THEN  l_call := 'N';
6058              ELSE  l_call := 'Y';
6059              END IF;
6060          END IF;
6061        ELSIF  p_gas    = 'REVENUE_ON_INVOICE'  THEN
6062          IF     p_tcs    = 'NONE'                THEN     l_call := 'N';
6063          ELSIF  p_tcs    = 'ACTIVITY'            THEN     l_call := 'Y';
6064          ELSIF  p_tcs    = 'INVOICE'             THEN
6065              IF   p_tax_rec_flag = 'Y'  THEN  l_call := 'N';
6066              ELSE  l_call := 'Y';
6067              END IF;
6068          END IF;
6069        ELSIF  p_gas    = 'TAX_CODE_ON_INVOICE'  THEN
6070          IF     p_tcs    = 'NONE'                THEN     l_call := 'Y';
6071          ELSIF  p_tcs    = 'ACTIVITY'            THEN     l_call := 'Y';
6072          ELSIF  p_tcs    = 'INVOICE'             THEN
6073              IF   p_tax_rec_flag = 'Y'  THEN  l_call := 'N';
6074              ELSE  l_call := 'Y';
6075              END IF;
6076          END IF;
6077        END IF;
6078     END IF;
6079     IF l_call = 'Y' THEN
6080         Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id             ,
6081                         p_original_ccid => p_ccid                          ,
6082                         p_subs_ccid     => g_ae_rule_rec.receivable_account,
6083                         p_actual_ccid   => x_ccid             );
6084     ELSE
6085        x_ccid := p_ccid;
6086     END IF;
6087   ELSE
6088     x_ccid := p_ccid;
6089   END IF;
6090   arp_standard.debug('x_ccid            :'||x_ccid);
6091   arp_standard.debug('substite_tax_bal_seg       -');
6092 END;
6093 
6094 
6095 /* ==========================================================================
6096  | PROCEDURE Build_Tax
6097  |
6098  | DESCRIPTION
6099  |    Build actual accounting entries for Tax amounts for discounts and
6100  |    adjustments
6101  |
6102  | SCOPE - PRIVATE
6103  |
6104  | PARAMETERS
6105  |    p_type_acct             IN    Indicates earned, unearned discount or
6106  |                                  adjustment accounting
6107  |    p_tax_code_source       IN    Source of gl account
6108  |    p_tax_recoverable_flag  IN    Indicates whether TAX is recoverable
6109  |    p_ae_line_init_rec      IN    Initialization record contains details
6110  |                                  for exchange rate, source table, id
6111  |                                  common to all accounting entries
6112  | History
6113  |   21-NOV-2003  Herve Yu   from_amount_dr      , from_amount_cr
6114  |                           from_acctd_amount_dr, from_acctd_amount_cr
6115  *==========================================================================*/
6116 PROCEDURE Build_Tax(p_tax_code_source       IN ar_receivables_trx.tax_code_source%TYPE       ,
6117                     p_tax_recoverable_flag  IN ar_receivables_trx.tax_recoverable_flag%TYPE  ,
6118                     p_ae_line_init_rec      IN ar_ae_alloc_rec_gt%ROWTYPE                        ) IS
6119 
6120 l_ae_line_rec         ar_ae_alloc_rec_gt%ROWTYPE                  ;
6121 l_ae_line_rec_empty   ar_ae_alloc_rec_gt%ROWTYPE                  ;
6122 l_ctr                 BINARY_INTEGER                            ;
6123 l_actual_account      ar_distributions.code_combination_id%TYPE ;
6124 l_org_inv_tax_code_id ar_distributions.tax_code_id%TYPE         ;
6125 l_org_inv_loc_seg_id  ar_distributions.location_segment_id%TYPE ;
6126 l_taxable_amt         NUMBER;
6127 l_taxable_acctd_amt   NUMBER;
6128 
6129 l_source_type_secondary ar_distributions.source_type_secondary%TYPE;
6130 
6131 --PL/SQL table object,this collection is used for bulk processing of inserts
6132 TYPE ae_alloc_rec_gt_tab IS TABLE OF ar_ae_alloc_rec_gt%ROWTYPE INDEX BY  BINARY_INTEGER;
6133 l_ae_alloc_rec_gt_tab ae_alloc_rec_gt_tab;
6134 l_ae_alloc_empty_tab  ae_alloc_rec_gt_tab;
6135 
6136 l_last_fetch     BOOLEAN := FALSE;
6137 l_bulk_index     NUMBER := 0;
6138 
6139 CURSOR summarize_tax IS
6140 SELECT /*+ INDEX(ae1 AR_AE_ALLOC_REC_GT_N3) */
6141        nvl(ae_pro_amt,0) ae_pro_amt,
6142        nvl(ae_pro_acctd_amt,0) ae_pro_acctd_amt,
6143        nvl(ae_pro_taxable_amt,0) ae_pro_taxable_amt,
6144        nvl(ae_pro_taxable_acctd_amt,0) ae_pro_taxable_acctd_amt,
6145        nvl(ae_from_pro_amt,0) ae_from_pro_amt,
6146        nvl(ae_from_pro_acctd_amt,0) ae_from_pro_acctd_amt,
6147        nvl(ae_pro_split_taxable_amt,nvl(ae_pro_taxable_amt,0)) ae_pro_split_taxable_amt,
6148        nvl(ae_pro_split_taxable_acctd_amt,nvl(ae_pro_taxable_acctd_amt,0)) ae_pro_split_taxable_acctd_amt,
6149        nvl(ae_pro_recov_taxable_amt,nvl(ae_pro_split_taxable_amt,nvl(ae_pro_taxable_amt,0)))
6150        ae_pro_recov_taxable_amt,
6151        nvl(ae_pro_recov_taxable_acctd_amt,nvl(ae_pro_split_taxable_acctd_amt,nvl(ae_pro_taxable_acctd_amt,0)))
6152        ae_pro_recov_taxable_acctd_amt,
6153        DECODE(ae1.ae_collected_tax_ccid,
6154               '', DECODE( DECODE(activity_bucket, 'ADJ_LINE' , g_ae_rule_rec.tax_code_source1,
6155                             'ADJ_TAX'  , g_ae_rule_rec.tax_code_source1,
6156                             'ADJ_FRT'  , g_ae_rule_rec.tax_code_source1,
6157                             'ADJ_CHRG' , g_ae_rule_rec.tax_code_source1,
6158                             'APP_LINE' , 'INVOICE',
6159                             'APP_TAX'  , 'INVOICE',
6160                             'APP_FRT'  , 'INVOICE',
6161                             'APP_CHRG' , 'INVOICE',
6162                             'ED_LINE'  , g_ae_rule_rec.tax_code_source1,
6163                             'ED_TAX'   , g_ae_rule_rec.tax_code_source1,
6164                             'ED_FRT'   , g_ae_rule_rec.tax_code_source1,
6165                             'ED_CHRG'  , g_ae_rule_rec.tax_code_source1,
6166                             'UNED_LINE', g_ae_rule_rec.tax_code_source2,
6167                             'UNED_TAX' , g_ae_rule_rec.tax_code_source2,
6168                             'UNED_FRT' , g_ae_rule_rec.tax_code_source2,
6169                             'UNED_CHRG', g_ae_rule_rec.tax_code_source2,
6170                                          g_ae_rule_rec.tax_code_source1),
6171                         'INVOICE',
6172                         DECODE(DECODE(activity_bucket,
6173                                      'ADJ_LINE' , g_ae_rule_rec.tax_recoverable_flag1,
6174                                      'ADJ_TAX'  , g_ae_rule_rec.tax_recoverable_flag1,
6175                                      'ADJ_FRT'  , g_ae_rule_rec.tax_recoverable_flag1,
6176                                      'ADJ_CHRG' , g_ae_rule_rec.tax_recoverable_flag1,
6177                                      'APP_LINE' , 'Y',
6178                                      'APP_TAX'  , 'Y',
6179                                      'APP_FRT'  , 'Y',
6180                                      'APP_CHRG' , 'Y',
6181                                      'ED_LINE'  , g_ae_rule_rec.tax_recoverable_flag1,
6182                                      'ED_TAX'   , g_ae_rule_rec.tax_recoverable_flag1,
6183                                      'ED_FRT'   , g_ae_rule_rec.tax_recoverable_flag1,
6184                                      'ED_CHRG'  , g_ae_rule_rec.tax_recoverable_flag1,
6185                                      'UNED_LINE', g_ae_rule_rec.tax_recoverable_flag2,
6186                                      'UNED_TAX' , g_ae_rule_rec.tax_recoverable_flag2,
6187                                      'UNED_FRT' , g_ae_rule_rec.tax_recoverable_flag2,
6188                                      'UNED_CHRG', g_ae_rule_rec.tax_recoverable_flag2,
6189                                                   g_ae_rule_rec.tax_recoverable_flag1),
6190                                       'Y', ae1.ae_code_combination_id,
6191                                           ''),
6192                           'NONE',ae1.ae_code_combination_id,
6193                           ''),
6194               ae1.ae_code_combination_id) ae_code_combination_id,
6195        ae1.ae_collected_tax_ccid,
6196        DECODE(ae1.activity_bucket,
6197                     'ADJ_LINE' ,ae1.ae_override_ccid1,
6198                     'ADJ_TAX'  ,ae1.ae_override_ccid1,
6199                     'ADJ_FRT'  ,ae1.ae_override_ccid1,
6200                     'ADJ_CHRG' ,ae1.ae_override_ccid1,
6201                     'ED_LINE'  ,ae1.ae_override_ccid1,
6202                     'ED_TAX'   ,ae1.ae_override_ccid1,
6203                     'ED_FRT'   ,ae1.ae_override_ccid1,
6204                     'ED_CHRG'  ,ae1.ae_override_ccid1,
6205                     'UNED_LINE',ae1.ae_override_ccid2,
6206                     'UNED_TAX' ,ae1.ae_override_ccid2,
6207                     'UNED_FRT' ,ae1.ae_override_ccid2,
6208                     'UNED_CHRG',ae1.ae_override_ccid2,
6209                '') actual_account,
6210        ae1.ae_tax_type,
6211        ae1.ae_tax_id,
6212        NVL(ae1.ae_tax_group_code_id,'') ae_tax_group_code_id,
6213        NVL(ae1.ae_tax_link_id_act,'') ae_tax_link_id_act,
6214        ae1.ae_customer_trx_line_id,
6215        ae1.ae_cust_trx_line_gl_dist_id,
6216        ae1.ae_ref_line_id,
6217        ae1.ref_account_class,
6218        ae1.activity_bucket,
6219        --{ref_dist_ccid
6220        ae1.ref_dist_ccid,
6221        ae1.ref_mf_dist_flag,
6222        --}
6223        ae1.ae_adj_ccid
6224       ,d.code_combination_id actual_tax_ccid
6225       ,ae1.ae_unedisc_ccid
6226       ,ae1.ae_edisc_ccid
6227 FROM ar_ae_alloc_rec_gt ae1,
6228      ra_cust_trx_line_gl_dist d
6229 WHERE ae1.ae_id = g_id
6230 AND ae1.ae_account_class = 'TAX'
6231 AND ae1.ae_cust_trx_line_gl_dist_id = d.cust_trx_line_gl_dist_id(+);
6232 
6233 g_ae_summ_tax_tbl       summarize_tax%ROWTYPE;
6234 
6235 TYPE summarize_tax_cur IS TABLE OF summarize_tax%ROWTYPE;
6236 g_ae_summ_tax_blk_tbl summarize_tax_cur := summarize_tax_cur();
6237 
6238 l_multi_factor      NUMBER := 1;
6239 l_actual_tax_ccid   NUMBER;
6240 BEGIN
6241 
6242  arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Tax()+');
6243  arp_standard.debug( 'p_tax_code_source:'||p_tax_code_source);
6244  arp_standard.debug( 'p_tax_recoverable_flag:'||p_tax_recoverable_flag);
6245 
6246 
6247 
6248 -- IF (((g_ae_rule_rec.tax_amt_alloc <> 0) OR (g_ae_rule_rec.tax_acctd_amt_alloc <> 0))
6249 --    OR ((g_ae_rule_rec.line_amt_alloc <> 0) OR (g_ae_rule_rec.line_acctd_amt_alloc <>0))) THEN
6250  /*------------------------------------------------------------------------------+
6251   | Tax due to activity tax code is independent of the original tax on invoice   |
6252   | hence this tax needs to be built first to the Non Recoverable Account on the |
6253   | activity Tax Code. With the taxable being the line amount or accounted amount|
6254   | This condition occurs only if tax distributions cannot be used for allocation|
6255   +------------------------------------------------------------------------------*/
6256 --   IF ((p_tax_code_source = 'ACTIVITY') AND ((g_bound_tax) OR (g_bound_activity))) THEN
6257       /*--------------------------------------------------------------------------------+
6258        | Initialize record to create new line with exchange rate defaulting from Invoice|
6259        +--------------------------------------------------------------------------------*/
6260 --        l_ae_line_rec := p_ae_line_init_rec;
6261       /*----------------------------------------------------------------------------+
6262        | Assign Tax link id, and tax id for Tax lines                               |
6263        +----------------------------------------------------------------------------*/
6264 --        IF ((g_ae_rule_rec.line_amt_alloc <> 0) OR (g_ae_rule_rec.line_acctd_amt_alloc <> 0)) THEN
6265 --           IF p_type_acct = 'ED_ADJ' THEN
6266 --              l_ae_line_rec.ae_tax_link_id := g_ed_adj_activity_link;
6267 --           ELSIF p_type_acct = 'UNED' THEN
6268 --              l_ae_line_rec.ae_tax_link_id := g_uned_activity_link;
6269 --           END IF;
6270 --        END IF;
6271        /*----------------------------------------------------------------------------+
6272         | Override the tax code id as for rule ACTIVITY the tax code must be from the|
6273         | receivable activity                                                        |
6274         +----------------------------------------------------------------------------*/
6275 --         IF ((p_tax_code_source = 'ACTIVITY') AND (p_type_acct = 'ED_ADJ')) THEN
6276 --            l_ae_line_rec.ae_tax_code_id         := g_ae_rule_rec.act_vat_tax_id1;
6277 --            l_ae_line_rec.ae_location_segment_id := '';
6278 --         ELSIF ((p_tax_code_source = 'ACTIVITY') AND (p_type_acct = 'UNED')) THEN
6279 --            l_ae_line_rec.ae_tax_code_id         := g_ae_rule_rec.act_vat_tax_id2;
6280 --            l_ae_line_rec.ae_location_segment_id := '';
6281 --         END IF;
6282        /*----------------------------------------------------------------------------+
6283         | Populate the source type for earned discounts non recoverable account      |
6284         +----------------------------------------------------------------------------*/
6285 --         IF p_type_acct = 'ED_ADJ' THEN
6286 --            IF g_ae_doc_rec.source_table = 'RA' THEN
6287 --               l_ae_line_rec.ae_line_type    := 'EDISC_NON_REC_TAX' ;
6288           /*----------------------------------------------------------------------------+
6289            | Populate the source type for adjustment non recoverable account            |
6290            +----------------------------------------------------------------------------*/
6291 --            ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
6292 --                  l_ae_line_rec.ae_line_type := 'ADJ_NON_REC_TAX';
6293           /*----------------------------------------------------------------------------+
6294            | Populate the source type for finance charges non recoverable account       |
6295            +----------------------------------------------------------------------------*/
6296 --            ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
6297 --                  l_ae_line_rec.ae_line_type := 'FINCHRG_NON_REC_TAX';
6298 --            END IF;
6299            --Set account
6300 --            l_actual_account := g_ae_rule_rec.act_tax_non_rec_ccid1;
6301         /*----------------------------------------------------------------------------+
6302          | Populate the source type for unearned discounts non recoverable account    |
6303          +----------------------------------------------------------------------------*/
6304 --         ELSIF p_type_acct = 'UNED' THEN
6305 --               l_ae_line_rec.ae_line_type    := 'UNEDISC_NON_REC_TAX' ;
6306               --Set account for unearned discounts
6307 --               l_actual_account := g_ae_rule_rec.act_tax_non_rec_ccid2;
6308 --         END IF;
6309        /*-----------------------------------------------------------------------------+
6310         | Level 2 validation in case the non recoverable tax account setup on activity|
6311         | tax code is null - request user to set it up.                               |
6312         +-----------------------------------------------------------------------------*/
6313 --         IF l_actual_account IS NULL THEN
6314 --            arp_standard.debug('Activity Non Recoverable Account ');
6315 --            RAISE invalid_ccid_error;
6316 --         END IF;
6317        /*----------------------------------------------------------------------------+
6318         | Create first accounting entry for Debit/Credit to Non Recoverable/Override |
6319         | Account, Setup Non Recoverable account for debits/credits                  |
6320         | Substitute balancing segment for Non Recoverable Tax Account               |
6321         +----------------------------------------------------------------------------*/
6322 	-- Bugfix 1948917.
6323 --	IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N'THEN
6324 --          Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id              ,
6325 --                          p_original_ccid => l_actual_account                 ,
6326 --                          p_subs_ccid     => g_ae_rule_rec.receivable_account ,
6327 --                          p_actual_ccid   => l_ae_line_rec.ae_account             );
6328 --	ELSE
6329 --	  l_ae_line_rec.ae_account := l_actual_account;
6330 --	END IF;
6331         /*----------------------------------------------------------------------------+
6332          | Set taxable amounts and accounted amounts, if there is no line amount to   |
6333          | then the taxable is calculated using the original tax line on the invoice  |
6334          +----------------------------------------------------------------------------*/
6335 --          l_taxable_amt        := g_ae_rule_rec.line_amt_alloc;
6336 --          l_taxable_acctd_amt  := g_ae_rule_rec.line_acctd_amt_alloc;
6337         /*----------------------------------------------------------------------------+
6338          | Create Debits/Credits for Non Recoverable Tax Account                      |
6339          +----------------------------------------------------------------------------*/
6340 --           Create_Debits_Credits(g_ae_rule_rec.tax_amt_alloc         ,
6341 --                                 g_ae_rule_rec.tax_acctd_amt_alloc   ,
6342 --                                 l_taxable_amt                       ,
6343 --                                 l_taxable_acctd_amt                 ,
6344 --                                 '','',
6345 --                                 l_ae_line_rec);
6346          /*----------------------------------------------------------------------------+
6347           | Assign Non Recoverable accounting record to lines table                    |
6348           +----------------------------------------------------------------------------*/
6349 --            Assign_Elements(l_ae_line_rec);
6350 --            l_ae_line_rec := l_ae_line_rec_empty;
6351 --   END IF;  --end if Build Tax for ACTIVITY
6352 --}
6353  /*----------------------------------------------------------------------------+
6354   | Populate the source type secondary for receipts/adjustments with tax       |
6355   +----------------------------------------------------------------------------*/
6356 --{HYUDETUPT merge inside the loop part of avoiding triple execution of process_amount
6357 --   IF p_type_acct = 'ED_ADJ' THEN
6358 --      IF g_ae_doc_rec.source_table = 'RA' THEN
6359 --         l_source_type_secondary  := 'EDISC' ;
6360     /*----------------------------------------------------------------------------+
6361      | Populate the source type secondary for adjustments with tax                |
6362      +----------------------------------------------------------------------------*/
6363 --      ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
6364 --            l_source_type_secondary    := 'ADJ';
6365     /*----------------------------------------------------------------------------+
6366      | Populate the source type secondary for finance charges tax inclusive       |
6367      +----------------------------------------------------------------------------*/
6368 --      ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
6369 --            l_source_type_secondary    := 'FINCHRG';
6370 --      END IF;
6371  /*----------------------------------------------------------------------------+
6372   | Populate the source type secondary for unearned discounts                  |
6373   +----------------------------------------------------------------------------*/
6374 --   ELSIF p_type_acct = 'UNED' THEN
6375 --         l_source_type_secondary       := 'UNEDISC' ;
6376 --   END IF; --end if type of account
6377 --}
6378 
6379 
6380  /*----------------------------------------------------------------------------+
6381   | Process all tax lines to be built                                          |
6382   +----------------------------------------------------------------------------*/
6383 
6384     OPEN summarize_tax;
6385 
6386     LOOP
6387     FETCH summarize_tax BULK COLLECT INTO g_ae_summ_tax_blk_tbl LIMIT g_bulk_fetch_rows;
6388 
6389     --reinitialize
6390     l_ae_alloc_rec_gt_tab := l_ae_alloc_empty_tab;
6391     l_bulk_index := 0;
6392 
6393     IF summarize_tax%NOTFOUND THEN
6394       l_last_fetch := TRUE;
6395     END IF;
6396 
6397     FOR i IN 1..g_ae_summ_tax_blk_tbl.count LOOP
6398 
6399        g_ae_summ_tax_tbl := g_ae_summ_tax_blk_tbl(i);
6400 
6401        l_actual_account   := g_ae_summ_tax_tbl.actual_account;
6402        l_actual_tax_ccid  := g_ae_summ_tax_tbl.actual_tax_ccid;
6403 
6404        arp_standard.debug('Tax g_ae_summ_tax_tbl.ae_customer_trx_line_id:'||g_ae_summ_tax_tbl.ae_customer_trx_line_id);
6405 
6406        /*--------------------------------------------------------------------------------+
6407         | Initialize record to create new line with exchange rate defaulting from Invoice|
6408         +--------------------------------------------------------------------------------*/
6409          l_ae_line_rec := p_ae_line_init_rec;
6410 
6411        /*----------------------------------------------------------------------------+
6412         | Assign Tax link id, and tax id for Tax lines                               |
6413         +----------------------------------------------------------------------------*/
6414          l_ae_line_rec.ae_tax_link_id := g_ae_summ_tax_tbl.ae_tax_link_id_act;
6415          l_ae_line_rec.ae_customer_trx_line_id := g_ae_summ_tax_tbl.ae_customer_trx_line_id;
6416          l_ae_line_rec.ae_cust_trx_line_gl_dist_id := g_ae_summ_tax_tbl.ae_cust_trx_line_gl_dist_id;
6417          l_ae_line_rec.ae_ref_line_id := g_ae_summ_tax_tbl.ae_ref_line_id;
6418          l_ae_line_rec.ref_account_class := g_ae_summ_tax_tbl.ref_account_class;
6419          l_ae_line_rec.activity_bucket    := g_ae_summ_tax_tbl.activity_bucket;
6420          --{ref_dist_ccid
6421          l_ae_line_rec.ref_dist_ccid    := g_ae_summ_tax_tbl.ref_dist_ccid;
6422          l_ae_line_rec.ref_mf_dist_flag := g_ae_summ_tax_tbl.ref_mf_dist_flag;
6423          --}
6424 
6425          arp_standard.debug('  g_ae_summ_tax_tbl.activity_bucket :'||g_ae_summ_tax_tbl.activity_bucket);
6426 
6427    --Earned discount
6428    IF g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX') THEN
6429         l_source_type_secondary  := 'EDISC' ;
6430     /*----------------------------------------------------------------------------+
6431      | Populate the source type secondary for adjustments with tax                |
6432      +----------------------------------------------------------------------------*/
6433    -- Adj tax
6434    ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('ADJ_TAX') THEN
6435 
6436       IF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
6437             l_source_type_secondary    := 'ADJ';
6438      /*----------------------------------------------------------------------------+
6439       | Populate the source type secondary for finance charges tax inclusive       |
6440       +----------------------------------------------------------------------------*/
6441       ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
6442             l_source_type_secondary    := 'FINCHRG';
6443       END IF;
6444  /*----------------------------------------------------------------------------+
6445   | Populate the source type secondary for unearned discounts                  |
6446   +----------------------------------------------------------------------------*/
6447    --Uned Disc
6448    ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('UNED_TAX') THEN
6449          l_source_type_secondary       := 'UNEDISC' ;
6450    END IF; --end if type of account
6451    arp_standard.debug('  l_source_type_secondary :'||l_source_type_secondary);
6452 
6453 
6454 
6455    /*----------------------------------------------------------------------------+
6456     | Override the tax code id as for rule ACTIVITY the tax code must be from the|
6457     | receivable activity                                                        |
6458     +----------------------------------------------------------------------------*/
6459     --
6460     -- Setting ae_tax_code_id
6461     --         ae_location_segment_id
6462     --
6463     IF (((the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'ACTIVITY') AND
6464          (g_ae_summ_tax_tbl.activity_bucket IN ('ADJ_TAX', 'ED_TAX'))))
6465               AND (NOT g_bound_tax) AND (NOT g_bound_activity) THEN
6466 
6467             l_ae_line_rec.ae_tax_code_id         := g_ae_rule_rec.act_vat_tax_id1;
6468             l_ae_line_rec.ae_location_segment_id := '';
6469 
6470      ELSIF (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) ='ACTIVITY') AND
6471            (g_ae_summ_tax_tbl.activity_bucket IN ('UNED_TAX'))
6472             AND (NOT g_bound_tax) AND (NOT g_bound_activity) THEN
6473 
6474             l_ae_line_rec.ae_tax_code_id         := g_ae_rule_rec.act_vat_tax_id2;
6475             l_ae_line_rec.ae_location_segment_id := '';
6476 
6477      ELSE
6478             IF g_ae_summ_tax_tbl.ae_tax_type = 'VAT' THEN
6479                l_ae_line_rec.ae_tax_group_code_id   := g_ae_summ_tax_tbl.ae_tax_group_code_id;
6480                l_ae_line_rec.ae_tax_code_id         := g_ae_summ_tax_tbl.ae_tax_id;
6481             ELSE --implies LOC or location based tax
6482                l_ae_line_rec.ae_location_segment_id := g_ae_summ_tax_tbl.ae_tax_id;
6483             END IF;
6484      END IF;
6485 
6486 
6487 
6488 
6489     /*----------------------------------------------------------------------------+
6490      | If tax is recoverable then debit/credit the Interim/Tax Account with       |
6491      | discount, adjustment amount                                                |
6492      +----------------------------------------------------------------------------*/
6493     --
6494     --Set line_type
6495     -- For APP_TAX, ED_TAX, UNED_TAX, ADJ_TAX
6496     -- Tax and Def tax
6497     --
6498     IF     (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'INVOICE')
6499              AND (the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket) = 'Y')
6500              AND (NOT g_bound_tax) THEN
6501 
6502 --          IF    g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX') THEN
6503 --                l_ae_line_rec.ae_line_type    := 'EDISC';
6504 --          ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('UNED_TAX') THEN
6505 --                l_ae_line_rec.ae_line_type    := 'UNEDISC';
6506           /*----------------------------------------------------------------------------+
6507            | Verify whether TAX is deferred                                             |
6508            +----------------------------------------------------------------------------*/
6509           IF g_ae_summ_tax_tbl.ae_collected_tax_ccid IS NULL THEN
6510                 l_ae_line_rec.ae_line_type    := 'TAX';
6511           ELSE --Tax is deferred
6512                 l_ae_line_rec.ae_line_type    := 'DEFERRED_TAX';
6513           END IF;
6514 
6515 
6516           /*--------------------------------------------------------------------------------------+
6517            | Set the source type secondary to indicate TAX/DEFERRED TAX on discounts, adjustments |
6518            +--------------------------------------------------------------------------------------*/
6519           l_ae_line_rec.ae_line_type_secondary  := l_source_type_secondary;
6520 
6521            --Assign account
6522           l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_code_combination_id;
6523 
6524           /*----------------------------------------------------------------------------+
6525            | Create accounting debits or credits as applicable                          |
6526            +----------------------------------------------------------------------------*/
6527          IF g_ae_summ_tax_tbl.activity_bucket IN ('APP_TAX') THEN
6528                --
6529                -- APP_TAX
6530                --
6531 
6532                -- Bug 6598080
6533               IF g_ae_doc_rec.called_from = 'WRAPPER' THEN
6534                  l_ae_line_rec.ae_account := g_ae_code_combination_id_app;
6535               ELSE
6536 	       l_ae_line_rec.ae_account := g_ae_rule_rec.receivable_account;
6537                arp_standard.debug('CCID for APP_TAX l_ae_line_rec.ae_account :'|| l_ae_line_rec.ae_account);
6538               END IF;
6539 
6540                /*----------------------------------------------------------------------------+
6541                 | Create accounting debits or credits as applicable                          |
6542                 +----------------------------------------------------------------------------*/
6543                  -- Application for tax. ARALLOCB creates the CR REC, as ARPDDB passes negative
6544                  -- detail distributions, we need ti multiply by -1 so that the distribution
6545                  -- for ARALLOCB are created as positive <=> CR REC
6546                   Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt       * -1,
6547                                         g_ae_summ_tax_tbl.ae_pro_acctd_amt * -1,
6548                                         g_ae_summ_tax_tbl.ae_pro_recov_taxable_amt      * -1,
6549                                         g_ae_summ_tax_tbl.ae_pro_recov_taxable_acctd_amt* -1,
6550                                         g_ae_summ_tax_tbl.ae_from_pro_amt       * -1,
6551                                         g_ae_summ_tax_tbl.ae_from_pro_acctd_amt * -1,
6552                                         l_ae_line_rec);
6553 
6554                   l_ae_line_rec.ae_line_type    := 'REC';
6555          ELSE
6556               --
6557               --For  ADJ_TAX, ED_TAX, UNED_TAX
6558               --
6559                l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_code_combination_id;
6560                arp_standard.debug('1 CCID for '||g_ae_summ_tax_tbl.activity_bucket||
6561                                   ' l_ae_line_rec.ae_account :'|| l_ae_line_rec.ae_account);
6562 
6563                --Boundary Adj
6564                adj_boundary_account
6565                  (p_receivables_trx_id   => g_receivables_trx_id,
6566                   p_bucket               => g_ae_summ_tax_tbl.ref_account_class,
6567                   p_ctlgd_id             => g_ae_summ_tax_tbl.ae_cust_trx_line_gl_dist_id,
6568                   x_ccid                 => l_ae_line_rec.ae_account);
6569 
6570                arp_standard.debug('2 CCID for '||g_ae_summ_tax_tbl.activity_bucket||
6571                                   ' l_ae_line_rec.ae_account :'|| l_ae_line_rec.ae_account);
6572 
6573               l_actual_account := l_ae_line_rec.ae_account;
6574               substite_tax_bal_seg
6575                (p_line_type       => l_ae_line_rec.ae_line_type,
6576                 p_gas             => the_gl_account_source(g_ae_summ_tax_tbl.activity_bucket),
6577                 p_tcs             => the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket),
6578                 p_tax_rec_flag    => the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket),
6579                 p_ccid            => l_actual_account,
6580                 x_ccid            => l_ae_line_rec.ae_account);
6581 
6582 
6583 --BUG#5245153
6584 --    IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
6585 --                 l_actual_account := l_ae_line_rec.ae_account;
6586 --                 Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id              ,
6587 --                                 p_original_ccid => l_actual_account         ,
6588 --                                 p_subs_ccid     => g_ae_rule_rec.receivable_account ,
6589 --                                 p_actual_ccid   => l_ae_line_rec.ae_account             );
6590 --	END IF;
6591 --}
6592                /*----------------------------------------------------------------------------+
6593                 | Create accounting debits or credits as applicable                          |
6594                 +----------------------------------------------------------------------------*/
6595                  -- Accounting for Activities
6596                  -- ARALLOCB creates the activity side of the accounting <=>
6597                  -- WO for Adjustments : negative ADJ > ARPDDB passes negative detail distributions > ARALLOCB creates DB WO
6598                  -- ED activity accounting for ED: positive ED > ARADDB passes negative detail distributions > ARALLOCB creates DB ED activity
6599                  -- UNED activity accounting for UNED: positive UNED > ARADDB passes negative detail distributions > ARALLOCB creates DB UNED activity
6600                  -- therefor no need to multiply by -1
6601                   Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt               ,
6602                                         g_ae_summ_tax_tbl.ae_pro_acctd_amt         ,
6603                                         g_ae_summ_tax_tbl.ae_pro_recov_taxable_amt ,
6604                                         g_ae_summ_tax_tbl.ae_pro_recov_taxable_acctd_amt ,
6605                                         g_ae_summ_tax_tbl.ae_from_pro_amt          ,
6606                                         g_ae_summ_tax_tbl.ae_from_pro_acctd_amt    ,
6607                                         l_ae_line_rec);
6608         END IF;
6609 
6610         --Assign_Elements(l_ae_line_rec);
6611         l_ae_line_rec.ae_id :=  g_id;
6612 	l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6613 	l_bulk_index := l_bulk_index + 1;
6614 	l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6615 
6616 
6617  ELSIF     --This is only possible for ADJ_TAX, ED_TAX, UNED_TAX
6618            -- Attention Verify why the APP_TAX non_recoverable
6619                (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'INVOICE')
6620            AND (the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket) = 'N')
6621            AND (NOT g_bound_tax)
6622            AND (g_ae_summ_tax_tbl.activity_bucket NOT IN ('APP_TAX'))
6623  THEN
6624       /*----------------------------------------------------------------------------+
6625        | Populate the source type for earned discounts non recoverable account      |
6626        +----------------------------------------------------------------------------*/
6627        --IF p_type_acct = 'ED_ADJ' THEN
6628        --
6629        -- Set line_type
6630        --
6631        IF (g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX','ADJ_TAX' )) THEN
6632 
6633            IF g_ae_doc_rec.source_table = 'RA' THEN
6634               l_ae_line_rec.ae_line_type    := 'EDISC_NON_REC_TAX' ;
6635            /*----------------------------------------------------------------------------+
6636             | Populate the source type for adjustment non recoverable account            |
6637             +----------------------------------------------------------------------------*/
6638            ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
6639               l_ae_line_rec.ae_line_type := 'ADJ_NON_REC_TAX';
6640            /*----------------------------------------------------------------------------+
6641             | Populate the source type for finance charges non recoverable account       |
6642             +----------------------------------------------------------------------------*/
6643            ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
6644               l_ae_line_rec.ae_line_type := 'FINCHRG_NON_REC_TAX';
6645            END IF;
6646 
6647        --Set l_actual_account, set by summarize cursor
6648 
6649        /*----------------------------------------------------------------------------+
6650         | Populate the source type for unearned discounts non recoverable account    |
6651         +----------------------------------------------------------------------------*/
6652                --  ELSIF p_type_acct = 'UNED' THEN
6653        ELSIF (g_ae_summ_tax_tbl.activity_bucket IN ('UNED_LINE','UNED_TAX','UNED_FRT','UNED_CHRG')) THEN
6654            l_ae_line_rec.ae_line_type    := 'UNEDISC_NON_REC_TAX' ;
6655                      --Set l_actual_account, set by summarize cursor
6656        END IF;
6657 
6658        /*-----------------------------------------------------------------------------+
6659         | Level 2 validation in case the non recoverable tax account setup on tax code|
6660         | is null - request user to set it up.                                        |
6661         +-----------------------------------------------------------------------------*/
6662 
6663         IF    l_actual_account IS NULL  THEN --is null
6664                --Boundary Adj
6665               adj_boundary_account
6666               (p_receivables_trx_id   => g_receivables_trx_id,
6667                p_bucket               => g_ae_summ_tax_tbl.activity_bucket,
6668                p_ctlgd_id             => g_ae_summ_tax_tbl.ae_cust_trx_line_gl_dist_id,
6669                x_ccid                 => l_actual_account);
6670             IF l_actual_account IS NULL  THEN
6671                arp_standard.debug('Invoice Non Recoverable Account is NULL');
6672                RAISE invalid_ccid_error;
6673             END IF;
6674         END IF;
6675 
6676 
6677         substite_tax_bal_seg
6678                (p_line_type       => l_ae_line_rec.ae_line_type,
6679                 p_gas             => the_gl_account_source(g_ae_summ_tax_tbl.activity_bucket),
6680                 p_tcs             => the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket),
6681                 p_tax_rec_flag    => the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket),
6682                 p_ccid            => l_actual_account,
6683                 x_ccid            => l_ae_line_rec.ae_account);
6684 
6685 
6686 
6687                /*----------------------------------------------------------------------------+
6688                 | Create first accounting entry for Debit/Credit to Non Recoverable/Override |
6689                 | Account, Setup Non Recoverable account for debits/credits                  |
6690                 | Substitute balancing segment for Non Recoverable Tax Account               |
6691                 +----------------------------------------------------------------------------*/
6692     -- Bugfix 1948917.
6693 --    IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
6694 --                 Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id              ,
6695 --                                 p_original_ccid => l_actual_account                 ,
6696 --                                 p_subs_ccid     => g_ae_rule_rec.receivable_account ,
6697 --                                 p_actual_ccid   => l_ae_line_rec.ae_account             );
6698 --	 ELSE
6699 --		 l_ae_line_rec.ae_account := l_actual_account;
6700 --	 END IF;
6701 
6702 
6703 
6704 
6705         /*----------------------------------------------------------------------------+
6706          | Create Debits/Credits for Non Recoverable Tax Account                      |
6707          +----------------------------------------------------------------------------*/
6708 
6709        arp_standard.debug('Create Debits/Credits for Non Recoverable Tax Account');
6710        -- Creation of activity accounting
6711        -- ADJ UNED ED distributions
6712        -- ARALLOC creates:
6713        -- * ADJ - WO side of accounting <=> negative ADJ => DB activty WO. As ARPDDB passes neg  detail distrib
6714        --       no need to multiply by -1
6715        -- * ED and UNED - Discount Activity side of the accounting. For positive ED UNED, ARPDDB passes negative
6716        --       deatil distributions and ARALLOCB creates DB of Activity, therefore no need to multiply by -1
6717        Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt                    ,
6718                              g_ae_summ_tax_tbl.ae_pro_acctd_amt              ,
6719                              g_ae_summ_tax_tbl.ae_pro_split_taxable_amt      ,
6720                              g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt,
6721                              g_ae_summ_tax_tbl.ae_from_pro_amt               ,
6722                              g_ae_summ_tax_tbl.ae_from_pro_acctd_amt         ,
6723                              l_ae_line_rec);
6724 
6725         /*----------------------------------------------------------------------------+
6726          | Assign Non Recoverable accounting record to lines table                    |
6727          +----------------------------------------------------------------------------*/
6728          --Assign_Elements(l_ae_line_rec);
6729 	 l_bulk_index := l_bulk_index + 1;
6730          l_ae_line_rec.ae_id :=  g_id;
6731 	 l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6732 	 l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6733 
6734 ELSIF   -- Activity Tax accounting not for APP_TAX
6735             (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket)='ACTIVITY')
6736         AND (NOT g_bound_tax) AND (NOT g_bound_activity)
6737       --AND (p_type_acct <> 'PAY')
6738         AND g_ae_summ_tax_tbl.activity_bucket NOT IN ('APP_LINE','APP_TAX','APP_FRT','APP_CHRG') --TCSACT
6739 THEN
6740         /*----------------------------------------------------------------------------+
6741          | Populate the source type for earned discounts non recoverable account      |
6742          +----------------------------------------------------------------------------*/
6743          -- IF p_type_acct = 'ED_ADJ' THEN
6744 
6745          --
6746          -- Set line_type
6747          --
6748          IF g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX','ADJ_TAX')  THEN
6749              IF g_ae_doc_rec.source_table = 'RA' THEN
6750                 l_ae_line_rec.ae_line_type    := 'EDISC_NON_REC_TAX' ;
6751              /*----------------------------------------------------------------------------+
6752               | Populate the source type for adjustment non recoverable account            |
6753               +----------------------------------------------------------------------------*/
6754              ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
6755                 l_ae_line_rec.ae_line_type := 'ADJ_NON_REC_TAX';
6756              /*----------------------------------------------------------------------------+
6757               | Populate the source type for finance charges non recoverable account       |
6758               +----------------------------------------------------------------------------*/
6759              ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
6760                 l_ae_line_rec.ae_line_type := 'FINCHRG_NON_REC_TAX';
6761              END IF;
6762              --Set by summarize cursor l_actual_account := g_ae_rule_rec.act_tax_non_rec_ccid1;
6763              /*----------------------------------------------------------------------------+
6764               | Populate the source type for unearned discounts non recoverable account    |
6765               +----------------------------------------------------------------------------*/
6766               --  ELSIF p_type_acct = 'UNED' THEN
6767          ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('UNED_TAX') THEN
6768              l_ae_line_rec.ae_line_type    := 'UNEDISC_NON_REC_TAX' ;
6769              --Set by summarize cursor l_actual_account := g_ae_rule_rec.act_tax_non_rec_ccid2;
6770          END IF;
6771 
6772 
6773          /*-----------------------------------------------------------------------------+
6774           | Level 2 validation in case the non recoverable tax account setup on activity|
6775           | tax code is null - request user to set it up.                               |
6776           +-----------------------------------------------------------------------------*/
6777          IF l_actual_account IS NULL THEN --is null
6778                 --Boundary Adj
6779                 adj_boundary_account
6780                (p_receivables_trx_id   => g_receivables_trx_id,
6781                 p_bucket               => g_ae_summ_tax_tbl.activity_bucket,
6782                 p_ctlgd_id             => g_ae_summ_tax_tbl.ae_cust_trx_line_gl_dist_id,
6783                 x_ccid                 => l_actual_account );
6784 
6785              IF l_actual_account IS NULL THEN --is null
6786                arp_standard.debug('Activity Non Recoverable Account is NULL');
6787                RAISE invalid_ccid_error;
6788              END IF;
6789 
6790          END IF;
6791 
6792         substite_tax_bal_seg
6793                (p_line_type       => l_ae_line_rec.ae_line_type,
6794                 p_gas             => the_gl_account_source(g_ae_summ_tax_tbl.activity_bucket),
6795                 p_tcs             => the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket),
6796                 p_tax_rec_flag    => the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket),
6797                 p_ccid            => l_actual_account,
6798                 x_ccid            => l_ae_line_rec.ae_account);
6799 
6800 
6801          /*----------------------------------------------------------------------------+
6802           | Create first accounting entry for Debit/Credit to Non Recoverable/Override |
6803           | Account, Setup Non Recoverable account for debits/credits                  |
6804           | Substitute balancing segment for Non Recoverable Tax Account               |
6805           +----------------------------------------------------------------------------*/
6806 	       -- Bugfix 1948917.
6807 --         IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
6808 --               Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id              ,
6809 --                               p_original_ccid => l_actual_account                 ,
6810 --                               p_subs_ccid     => g_ae_rule_rec.receivable_account ,
6811 --                               p_actual_ccid   => l_ae_line_rec.ae_account             );
6812 --         ELSE
6813 --               l_ae_line_rec.ae_account := l_actual_account;
6814 --         END IF;
6815 
6816 
6817          /*----------------------------------------------------------------------------+
6818           | Create Debits/Credits for Non Recoverable Tax Account                      |
6819           +----------------------------------------------------------------------------*/
6820          arp_standard.debug(' Create Debits/Credits for Non Recoverable Tax Account');
6821          -- Creation of activity accounting
6822          -- ADJ UNED ED distributions
6823          -- ARALLOC creates:
6824          -- * ADJ - WO side of accounting <=> negative ADJ => DB activty WO. As ARPDDB passes neg  detail distrib
6825          --       no need to multiply by -1
6826          -- * ED and UNED - Discount Activity side of the accounting. For positive ED UNED, ARPDDB passes negative
6827          --       deatil distributions and ARALLOCB creates DB of Activity, therefore no need to multiply by -1
6828          Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt                 ,
6829                                g_ae_summ_tax_tbl.ae_pro_acctd_amt           ,
6830                                g_ae_summ_tax_tbl.ae_pro_split_taxable_amt   ,
6831                                g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt ,
6832                                g_ae_summ_tax_tbl.ae_from_pro_amt            ,
6833                                g_ae_summ_tax_tbl.ae_from_pro_acctd_amt      ,
6834                                l_ae_line_rec);
6835 
6836          /*----------------------------------------------------------------------------+
6837           | Assign Non Recoverable accounting record to lines table                    |
6838           +----------------------------------------------------------------------------*/
6839          --Assign_Elements(l_ae_line_rec);
6840 	 l_bulk_index := l_bulk_index + 1;
6841          l_ae_line_rec.ae_id :=  g_id;
6842 	 l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6843 	 l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6844 
6845          /*----------------------------------------------------------------------------+
6846           | Assign Non Recoverable accounting record to lines table                    |
6847           +----------------------------------------------------------------------------*/
6848          IF g_ae_summ_tax_tbl.ae_tax_type = 'VAT' THEN
6849             l_ae_line_rec.ae_tax_group_code_id   := g_ae_summ_tax_tbl.ae_tax_group_code_id;
6850             l_ae_line_rec.ae_tax_code_id         := g_ae_summ_tax_tbl.ae_tax_id;
6851          ELSE --implies LOC or location based tax
6852             l_ae_line_rec.ae_location_segment_id := g_ae_summ_tax_tbl.ae_tax_id;
6853          END IF;
6854 
6855 
6856  ELSIF     (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'NONE')
6857              AND (g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX','UNED_TAX',
6858                   'ADJ_TAX'    --BUG#5185726: include ADJ_TAX
6859                   ))
6860              AND (NOT g_bound_tax) THEN
6861 
6862           IF    g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX') THEN
6863                 l_ae_line_rec.ae_line_type    := 'EDISC';
6864           ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('UNED_TAX') THEN
6865                 l_ae_line_rec.ae_line_type    := 'UNEDISC';
6866           ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('ADJ_TAX') THEN --BUG#5185726: include ADJ_TAX
6867                 l_ae_line_rec.ae_line_type    := 'ADJ';
6868           END IF;
6869 
6870           /*----------------------------------------------------------------------------+
6871            | Create accounting debits or credits as applicable                          |
6872            +----------------------------------------------------------------------------*/
6873 
6874 
6875           IF g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX',
6876 		            'ADJ_TAX') --BUG#5185726: include ADJ_TAX
6877           THEN
6878              IF g_ae_rule_rec.gl_account_source1 = 'REVENUE_ON_INVOICE' AND
6879                 g_ae_rule_rec.tax_code_source1   = 'NONE'
6880              THEN
6881                 l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_code_combination_id;
6882              ELSIF g_ae_rule_rec.gl_account_source1 = 'TAX_CODE_ON_INVOICE' AND
6883                    g_ae_rule_rec.tax_code_source1   = 'NONE'
6884              THEN
6885                 IF g_ae_summ_tax_tbl.activity_bucket = 'ADJ_TAX' THEN
6886                    l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_adj_ccid;
6887                 ELSE
6888                    l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_edisc_ccid;
6889                 END IF;
6890              ELSE
6891                 l_ae_line_rec.ae_account := g_ae_rule_rec.code_combination_id1;
6892              END IF;
6893           ELSE
6894              IF g_ae_rule_rec.gl_account_source2 = 'REVENUE_ON_INVOICE' AND
6895                 g_ae_rule_rec.tax_code_source2   = 'NONE'
6896              THEN
6897                 l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_code_combination_id;
6898              ELSIF g_ae_rule_rec.gl_account_source2 = 'TAX_CODE_ON_INVOICE' AND
6899                    g_ae_rule_rec.tax_code_source2   = 'NONE'
6900              THEN
6901                 l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_unedisc_ccid;
6902              ELSE
6903                 l_ae_line_rec.ae_account := g_ae_rule_rec.code_combination_id2;
6904              END IF;
6905           END IF;
6906 
6907         l_actual_account := l_ae_line_rec.ae_account;
6908         substite_tax_bal_seg
6909                (p_line_type       => l_ae_line_rec.ae_line_type,
6910                 p_gas             => the_gl_account_source(g_ae_summ_tax_tbl.activity_bucket),
6911                 p_tcs             => the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket),
6912                 p_tax_rec_flag    => the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket),
6913                 p_ccid            => l_actual_account,
6914                 x_ccid            => l_ae_line_rec.ae_account);
6915 
6916 
6917            /*----------------------------------------------------------------------------+
6918             | Create accounting debits or credits as applicable                          |
6919             +----------------------------------------------------------------------------*/
6920            -- Accounting for Activities
6921            -- ARALLOCB creates the activity side of the accounting <=>
6922            -- WO for Adjustments : negative ADJ > ARPDDB passes negative detail distributions > ARALLOCB creates DB WO
6923            -- ED activity accounting for ED: positive ED > ARADDB passes negative detail distributions > ARALLOCB creates DB ED activity
6924            -- UNED activity accounting for UNED: positive UNED > ARADDB passes negative detail distributions > ARALLOCB creates DB UNED activity
6925            -- therefor no need to multiply by -1
6926            Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt               ,
6927                                  g_ae_summ_tax_tbl.ae_pro_acctd_amt         ,
6928                                  g_ae_summ_tax_tbl.ae_pro_recov_taxable_amt ,
6929                                  g_ae_summ_tax_tbl.ae_pro_recov_taxable_acctd_amt ,
6930                                  g_ae_summ_tax_tbl.ae_from_pro_amt          ,
6931                                  g_ae_summ_tax_tbl.ae_from_pro_acctd_amt    ,
6932                                 l_ae_line_rec);
6933          -- Assign_Elements(l_ae_line_rec);
6934 	 l_bulk_index := l_bulk_index + 1;
6935          l_ae_line_rec.ae_id :=  g_id;
6936 	 l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6937 	 l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6938 
6939   END IF;
6940 
6941 
6942 
6943 
6944 
6945         /*------------------------------------------------------------------------------+
6946          | Create accounting entries for deferred tax where tax is non recoverable. For |
6947          | adjustments in the sign of the receivable, a refund takes place when these   |
6948          | accounting entries are created. Eg -ve adjustment Dr Interim, Cr Collected,  |
6949          | positive adjustments Cr Interim, Dr Collected, users would need to run       |
6950          | reports based on the accounting created, so that they can handle deferred    |
6951          | Invoices when such accounting takes place and create necessary offsetting    |
6952          | adjustments as required. For boundary conditions we dont create regular      |
6953          | accounting entries, but only move the deferred tax due to discount or payment|
6954          | hence the if condtruct using g_bound_tax for the above accounting conditions|
6955          +------------------------------------------------------------------------------*/
6956 
6957      IF (      (g_ae_summ_tax_tbl.ae_collected_tax_ccid IS NOT NULL)
6958            AND  ((   (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'INVOICE')
6959                  AND (the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket) = 'N'))
6960              OR (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'ACTIVITY')
6961              OR (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'NONE')))
6962            AND (g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX','UNED_TAX','ADJ_TAX'))
6963      THEN
6964 
6965          /*--------------------------------------------------------------------------------------+
6966           | Set the source type secondary to indicate TAX/DEFERRED TAX on discounts, adjustments |
6967           +--------------------------------------------------------------------------------------*/
6968           l_ae_line_rec.ae_line_type_secondary  := l_source_type_secondary;
6969 
6970           --{BUG#3509185
6971           /*----------------------------------------------------------------------------------------+
6972            |For BR adjustments set the secondary table, id, type columns as the                     |
6973            |are critical in reconciliation process. Bills has tax code source as NONE               |
6974            |for adjustments against it which ae of type endorsements. Override the above            |
6975            |type secondary if bill line assignment. The setting below is used for BR reconciliation |
6976            |On closure of the bill, and if not set correctly will orphan the deferred tax entries.  |
6977            +----------------------------------------------------------------------------------------*/
6978            IF g_br_cust_trx_line_id IS NOT NULL THEN
6979               IF (     (g_ae_doc_rec.source_table = 'RA')
6980                    AND (g_ae_doc_rec.event = 'MATURITY_DATE')) THEN
6981                      l_ae_line_rec.ae_source_table := 'TH';
6982               END IF;
6983               /*----------------------------------------
6984                |Populate the source table secondary for the accounting created by tax accounting
6985                |engine, because it is important to distinguish tax moved foor a specific exchange
6986                |or transaction on the Bill. ( tax codes and other accounting grouping attributes
6987                |could be common accross different transactions.)
6988                +-------------------------------------------------*/
6989                l_ae_line_rec.ae_source_table_secondary := 'CTL';
6990                l_ae_line_rec.ae_source_id_secondary    := g_br_cust_trx_line_id;
6991                l_ae_line_rec.ae_line_type_secondary   := 'ASSIGNMENT';
6992           END IF; --BR
6993           --}
6994           /*----------------------------------------------------------------------------+
6995            | Create second accounting entry for Debit/Credit to Interim Tax account     |
6996            +----------------------------------------------------------------------------*/
6997           l_ae_line_rec.ae_line_type := 'DEFERRED_TAX' ;
6998           IF  l_actual_tax_ccid IS NOT NULL THEN
6999               l_ae_line_rec.ae_account := l_actual_tax_ccid;
7000           ELSE
7001               l_ae_line_rec.ae_account      := g_ae_summ_tax_tbl.ae_code_combination_id;
7002           END IF;
7003           /*----------------------------------------------------------------------------+
7004            | Create Debits/Credits for Interim Tax account to move deferred tax         |
7005            +----------------------------------------------------------------------------*/
7006            --
7007            -- If ED_TAX or UNED_TAX then the DEFERRED_TAX accounting same sign as ED or UNED TAX distributions
7008            l_multi_factor := 1;
7009 
7010 
7011            Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt               * l_multi_factor,
7012                                  g_ae_summ_tax_tbl.ae_pro_acctd_amt         * l_multi_factor,
7013                                  g_ae_summ_tax_tbl.ae_pro_split_taxable_amt * l_multi_factor,
7014                                  g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt * l_multi_factor,
7015                                  g_ae_summ_tax_tbl.ae_from_pro_amt          * l_multi_factor,
7016                                  g_ae_summ_tax_tbl.ae_from_pro_acctd_amt    * l_multi_factor,
7017                                  l_ae_line_rec);
7018 
7019             /*----------------------------------------------------------------------------+
7020              | Assign debit/credit for Interim tax to build table                         |
7021              +----------------------------------------------------------------------------*/
7022             --Assign_Elements(l_ae_line_rec);
7023 	    l_bulk_index := l_bulk_index + 1;
7024             l_ae_line_rec.ae_id :=  g_id;
7025 	    l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
7026 	    l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
7027 
7028             /*----------------------------------------------------------------------------+
7029              | Create third accounting entry for Debit/Credit to Collected Tax account    |
7030              +----------------------------------------------------------------------------*/
7031             l_ae_line_rec.ae_line_type := 'TAX' ;
7032             l_ae_line_rec.ae_account      := g_ae_summ_tax_tbl.ae_collected_tax_ccid;
7033 
7034             /*----------------------------------------------------------------------------+
7035              | Create Debits/Credits for Collected Tax account to move deferred tax       |
7036              +----------------------------------------------------------------------------*/
7037             Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt   * -1 * l_multi_factor ,
7038                                   g_ae_summ_tax_tbl.ae_pro_acctd_amt * -1 * l_multi_factor,
7039                                   g_ae_summ_tax_tbl.ae_pro_split_taxable_amt * -1* l_multi_factor,
7040                                   g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt * -1* l_multi_factor,
7041                                   g_ae_summ_tax_tbl.ae_from_pro_amt * -1  * l_multi_factor,
7042                                   g_ae_summ_tax_tbl.ae_from_pro_acctd_amt * -1 * l_multi_factor,
7043                                   l_ae_line_rec,
7044                                  'Y');
7045 
7046             /*----------------------------------------------------------------------------+
7047              | Assign debit/credit for Collected Tax account to build table               |
7048              +----------------------------------------------------------------------------*/
7049              --Assign_Elements(l_ae_line_rec);
7050 	     l_bulk_index := l_bulk_index + 1;
7051              l_ae_line_rec.ae_id :=  g_id;
7052 	     l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
7053 	     l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
7054 
7055      END IF;
7056 
7057 
7058 
7059 
7060 
7061      /*----------------------------------------------------------------------------+
7062       | Create Debits/Credits for moving prorate deferred tax as a result of       |
7063       | Payment from Interim Account to Collected Tax account                      |
7064       +----------------------------------------------------------------------------*/
7065    IF ((g_ae_summ_tax_tbl.ae_collected_tax_ccid IS NOT NULL) --is not null
7066               AND (NOT g_done_def_tax)
7067               AND (g_ae_summ_tax_tbl.activity_bucket IN ('APP_TAX'))
7068               AND (nvl(g_ae_doc_rec.document_type,'RECEIPT') <> 'CREDIT_MEMO'))
7069    THEN
7070           l_ae_line_rec := p_ae_line_init_rec; --Initialise record
7071 
7072          /*--------------------------------------------------------------------------------------+
7073           | Set the source type secondary to indicate TAX/DEFERRED TAX on discounts, adjustments |
7074           +--------------------------------------------------------------------------------------*/
7075            l_ae_line_rec.ae_line_type_secondary  := 'PAYMENT';
7076 
7077           --{BUG#3509185
7078           /*------------------------------------------------------------------------------------+
7079            | For Bills receivable adjustments set the secondary table, id, type columns as thes |
7080            | are critical in the reconciliation process. Bills have a Tax code source of NONE   |
7081            | for adjustments against it which are of type endorsments. Override the above line  |
7082            | type secondry if bill line assignment. The setting below is used for BR reconcile  |
7083            | on closure of Bill, and if not set correctly will orphan the deferred tax entries. |
7084            +------------------------------------------------------------------------------------*/
7085             IF g_br_cust_trx_line_id IS NOT NULL THEN
7086                IF ((g_ae_doc_rec.source_table = 'RA')
7087                                    AND (g_ae_doc_rec.event = 'MATURITY_DATE')) THEN
7088                   l_ae_line_rec.ae_source_table := 'TH';
7089                END IF;
7090            /*----------------------------------------------------------------------------------+
7091             | Populate the source table secondary for accounting created by the tax accounting |
7092             | engine, because its is important to distinguish tax moved for a specific exchange|
7093             | or transaction on the Bill. (tax codes and other accounting grouping attributes  |
7094             | could be common across different transactions.)                                  |
7095             +----------------------------------------------------------------------------------*/
7096                l_ae_line_rec.ae_source_table_secondary := 'CTL';
7097                l_ae_line_rec.ae_source_id_secondary    := g_br_cust_trx_line_id;
7098                l_ae_line_rec.ae_line_type_secondary := 'ASSIGNMENT'; --override fefault value
7099             END IF; --end if BR
7100             --}
7101 
7102        --Set the tax code or location segment id
7103           IF g_ae_summ_tax_tbl.ae_tax_type = 'VAT' THEN
7104              l_ae_line_rec.ae_tax_group_code_id := g_ae_summ_tax_tbl.ae_tax_group_code_id;
7105              l_ae_line_rec.ae_tax_code_id := g_ae_summ_tax_tbl.ae_tax_id;
7106           ELSE --implies LOC or location based tax
7107              l_ae_line_rec.ae_location_segment_id := g_ae_summ_tax_tbl.ae_tax_id;
7108           END IF;
7109 
7110           l_ae_line_rec.ae_line_type := 'DEFERRED_TAX' ;
7111           l_ae_line_rec.ae_account   := l_actual_tax_ccid;
7112 
7113         /*-----------------------------------------------------------------------------+
7114          |Deferred tax for payments is not linked to revenue allocations for discounts |
7115          |or adjustments. Set link id for tax moved due to payments to '' as they have |
7116          |no link basis - revenue to link to, they indicate movements due to payments  |
7117          |from the Interim to Collected Tax accounts.                                  |
7118          +-----------------------------------------------------------------------------*/
7119           l_ae_line_rec.ae_tax_link_id := g_ae_summ_tax_tbl.ae_tax_link_id_act;
7120 
7121           Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt           ,
7122                                 g_ae_summ_tax_tbl.ae_pro_acctd_amt     ,
7123                                 g_ae_summ_tax_tbl.ae_pro_split_taxable_amt    ,
7124                                 g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt,
7125                                 --HYU--{
7126                                 g_ae_summ_tax_tbl.ae_from_pro_amt           ,
7127                                 g_ae_summ_tax_tbl.ae_from_pro_acctd_amt     ,
7128                                 --HYU--}
7129                                 l_ae_line_rec);
7130 
7131        /*----------------------------------------------------------------------------+
7132         | Assign debit/credit for Interim Tax account for payments to build table    |
7133         +----------------------------------------------------------------------------*/
7134          --Assign_Elements(l_ae_line_rec);
7135 	 l_bulk_index := l_bulk_index + 1;
7136          l_ae_line_rec.ae_id :=  g_id;
7137 	 l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
7138 	 l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
7139 
7140        /*----------------------------------------------------------------------------+
7141         | Create third accounting entry for Debit/Credit to Collected Tax account    |
7142         +----------------------------------------------------------------------------*/
7143          l_ae_line_rec.ae_line_type := 'TAX' ;
7144          l_ae_line_rec.ae_account      := g_ae_summ_tax_tbl.ae_collected_tax_ccid;
7145 
7146        /*----------------------------------------------------------------------------+
7147         | Create Debits/Credits for Collected Tax account for payment                |
7148         +----------------------------------------------------------------------------*/
7149          Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt * -1               ,
7150                                g_ae_summ_tax_tbl.ae_pro_acctd_amt * -1         ,
7151                                g_ae_summ_tax_tbl.ae_pro_split_taxable_amt  * -1        ,
7152                                g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt * -1   ,
7153                                --HYU--{
7154                                g_ae_summ_tax_tbl.ae_from_pro_amt * -1               ,
7155                                g_ae_summ_tax_tbl.ae_from_pro_acctd_amt * -1         ,
7156                                --HYU--}
7157                                l_ae_line_rec,
7158                                'Y');
7159 
7160        /*----------------------------------------------------------------------------+
7161         | Assign debit/credit for Collected tax account for payments to build table  |
7162         +----------------------------------------------------------------------------*/
7163          --Assign_Elements(l_ae_line_rec);
7164 	 l_bulk_index := l_bulk_index + 1;
7165          l_ae_line_rec.ae_id :=  g_id;
7166 	 l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
7167 	 l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
7168 
7169      END IF; --End if create debits/credits for deferred tax associated with payment
7170 
7171 
7172 
7173 
7174 
7175 
7176        IF   (g_br_cust_trx_line_id IS NOT NULL)      AND
7177             (g_ae_doc_rec.source_table = 'TH')       AND
7178             (g_ae_summ_tax_tbl.activity_bucket  = 'ADJ_TAX')
7179        THEN
7180              Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt               ,
7181                                    g_ae_summ_tax_tbl.ae_pro_acctd_amt         ,
7182                                    g_ae_summ_tax_tbl.ae_pro_recov_taxable_amt ,
7183                                    g_ae_summ_tax_tbl.ae_pro_recov_taxable_acctd_amt,
7184                                    --HYU--{
7185                                    g_ae_summ_tax_tbl.ae_from_pro_amt          ,
7186                                    g_ae_summ_tax_tbl.ae_from_pro_acctd_amt    ,
7187                                    --HYU--}
7188                                    l_ae_line_rec);
7189               --Assign_Elements(l_ae_line_rec);
7190 	      l_bulk_index := l_bulk_index + 1;
7191               l_ae_line_rec.ae_id :=  g_id;
7192 	      l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
7193 	      l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
7194         END IF;
7195         --}
7196 
7197 
7198    END LOOP; --For each Tax line build accounting entries
7199 
7200    /**In procedure Assign_Elements,g_ae_ctr is incremented for each record
7201     inserted.But no code segment in this package uses the value of the
7202     variable,thus not incremented the variable here.*/
7203    FORALL i IN l_ae_alloc_rec_gt_tab.first..l_ae_alloc_rec_gt_tab.last
7204    INSERT INTO ar_ae_alloc_rec_gt VALUES l_ae_alloc_rec_gt_tab(i);
7205 
7206    --Exit Last fetch
7207    IF l_last_fetch THEN
7208 	EXIT;
7209     END IF;
7210 
7211 
7212   END LOOP;
7213 
7214   --close cursor
7215   CLOSE summarize_tax;
7216 
7217  /*------------------------------------------------------------+
7218   | Done processing deferred tax, so mark flag to signify this |
7219   +------------------------------------------------------------*/
7220    --IF p_type_acct = 'PAY' THEN
7221    IF g_ae_summ_tax_tbl.activity_bucket IN ('APP_LINE','APP_TAX','APP_FRT','APP_CHRG') THEN
7222       g_done_def_tax := TRUE;
7223    END IF;
7224 
7225 --END IF; -- Build tax only if a tax amount or line amount exists amount exists
7226         -- i.e. if both line alloc and tax alloc are zero then there is nothing
7227 
7228 
7229 
7230 
7231 
7232 
7233 
7234  arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Tax()-');
7235 
7236 EXCEPTION
7237   WHEN invalid_ccid_error THEN
7238      arp_standard.debug('Invalid Tax ccid - ARP_ALLOCATION_PKG.Build_Tax' );
7239      fnd_message.set_name('AR','AR_INVALID_TAX_ACCOUNT');
7240      RAISE;
7241 
7242   WHEN OTHERS THEN
7243      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Build_Tax');
7244      RAISE;
7245 
7246 END Build_Tax;
7247 
7248 /* ==========================================================================
7249  | PROCEDURE Build_Charges_Freight_All
7250  |
7251  | DESCRIPTION
7252  |    Build actual accounting entries for Charges and Freight accounts
7253  |    to Activity GL Account
7254  |
7255  | SCOPE - PRIVATE
7256  |
7257  | PARAMETERS
7258  |    p_type_acct             IN    Indicates earned, unearned discount or
7259  |                                  adjustment accounting
7260  |    p_ae_line_init_rec      IN    Initialization record contains details
7261  |                                  for exchange rate, source table, id
7262  |                                  common to all accounting entries
7263  |
7264  | NOTES
7265  |    In the code below we add the freight and charges amounts to the line
7266  |    this would already have been done in the Init_Amts routine. These
7267  |    statements are retained they can be removed later if required and are
7268  |    only for reference purposes. In reality the IF construct below which
7269  |    creates the accounting would be true only if p_build_all is true
7270  |    indicating Gross to Activity Gl account. In this case we route the
7271  |    creation through this routine.
7272  |  History
7273  |    21-NOV-2003   Herve Yu   from_amount_dr      ,  from_amount_cr
7274  |                             from_acctd_amount_dr,  from_acctd_amount_cr
7275  *==========================================================================*/
7276 PROCEDURE Build_Charges_Freight_All(p_type_acct         IN VARCHAR2         ,
7277                                     p_ae_line_init_rec  IN ar_ae_alloc_rec_gt%ROWTYPE,
7278                                     p_build_all         IN BOOLEAN ) IS
7279 
7280 l_ae_line_rec         ar_ae_alloc_rec_gt%ROWTYPE;
7281 l_actual_account      ar_distributions.code_combination_id%TYPE ;
7282 l_line_tax_amt        NUMBER := 0;
7283 l_line_tax_acctd_amt  NUMBER := 0;
7284 
7285 BEGIN
7286 
7287   arp_standard.debug('ARP_ALLOCATION_PKG.Build_Charges_Freight_All()+');
7288 
7289   adj_code_combination_id := '';
7290 
7291 /*----------------------------------------------------------------------------------+
7292  | Build All flag indicates that Rule is Gross to Activity GL account for type Acct |
7293  +----------------------------------------------------------------------------------*/
7294   IF ((p_build_all = TRUE) AND (NOT g_added_tax)) THEN
7295 
7296      l_line_tax_amt       := g_ae_rule_rec.line_amt_alloc + g_ae_rule_rec.tax_amt_alloc;
7297      l_line_tax_acctd_amt := g_ae_rule_rec.line_acctd_amt_alloc + g_ae_rule_rec.tax_acctd_amt_alloc;
7298 
7299   ELSIF ((p_build_all = TRUE) AND (g_added_tax)) THEN
7300 
7301       --In this case tax would have been added to line bucket
7302         l_line_tax_amt       := g_ae_rule_rec.line_amt_alloc;
7303         l_line_tax_acctd_amt := g_ae_rule_rec.line_acctd_amt_alloc;
7304 
7305   END IF;
7306 
7307 /*-----------------------------------------------------------------------------+
7308  | If freight or charges exists then create an accounting entry for a sum total|
7309  | or Gross to Activity GL Account                                             |
7310  +-----------------------------------------------------------------------------*/
7311 
7312   IF ((((g_ae_rule_rec.freight_amt_alloc <> 0) OR (g_ae_rule_rec.freight_acctd_amt_alloc <> 0))
7313        OR ((g_ae_rule_rec.charges_amt_alloc <> 0) AND (g_ae_rule_rec.charges_acctd_amt_alloc <> 0)))
7314        OR (p_build_all = TRUE))
7315   THEN
7316 
7317    /*----------------------------------------------------------------------------+
7318     | Initialize record with exchange rate, source id, table details for new line|
7319     +----------------------------------------------------------------------------*/
7320      l_ae_line_rec := p_ae_line_init_rec;
7321 
7322    /*----------------------------------------------------------------------------+
7323     | Populate source type for earned discounts                                  |
7324     +----------------------------------------------------------------------------*/
7325      IF p_type_acct = 'ED_ADJ' THEN
7326 
7327         IF g_ae_doc_rec.source_table = 'RA' THEN
7328 
7329            l_ae_line_rec.ae_line_type    := 'EDISC' ;
7330 
7331       /*----------------------------------------------------------------------------+
7332        | Populate source type for unearned discounts                                |
7333        +----------------------------------------------------------------------------*/
7334         ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
7335 
7336               l_ae_line_rec.ae_line_type := 'ADJ';
7337 
7338       /*----------------------------------------------------------------------------+
7339        | Populate source type for finance charges                                   |
7340        +----------------------------------------------------------------------------*/
7341         ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
7342 
7343               l_ae_line_rec.ae_line_type := 'FINCHRG';
7344 
7345         END IF;
7346 
7347         l_ae_line_rec.ae_account := g_ae_rule_rec.code_combination_id1;
7348 
7349   /*----------------------------------------------------------------------------+
7350    | Populate source type for unearned discounts                                |
7351    +----------------------------------------------------------------------------*/
7352     ELSIF p_type_acct = 'UNED' THEN
7353 
7354           l_ae_line_rec.ae_line_type    := 'UNEDISC';
7355           l_ae_line_rec.ae_account         := g_ae_rule_rec.code_combination_id2;
7356 
7357     END IF;
7358 
7359      l_actual_account := l_ae_line_rec.ae_account;
7360 
7361   /*--------------------------------------------------------------------------------+
7362    | Substitute balancing segment for the Activity GL Account, for deposites we     |
7363    | dont need to substitute the segment as the account is derived by Autoaccounting|
7364    | and should be in the balancing segment as that of the Receivable on Deposit    |
7365    +--------------------------------------------------------------------------------*/
7366     IF (g_ae_doc_rec.other_flag IN ('COMMITMENT', 'CHARGEBACK', 'CBREVERSAL')) THEN
7367        arp_standard.debug('Account derived by Autoaccounting');
7368     ELSE
7369      -- Bugfix 1948917.
7370      IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
7371      Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id             ,
7372                      p_original_ccid => l_actual_account                ,
7373                      p_subs_ccid     => g_ae_rule_rec.receivable_account,
7374                      p_actual_ccid   => l_ae_line_rec.ae_account             );
7375      ELSE
7376       l_ae_line_rec.ae_account := l_actual_account;
7377      END IF;
7378     END IF;
7379 
7380   /*----------------------------------------------------------------------------+
7381    | Set the activity ccid which will be stamped on adj.code_combination_id     |
7382    +----------------------------------------------------------------------------*/
7383     IF adj_code_combination_id IS NULL
7384        AND (((g_ae_rule_rec.freight_amt_alloc + g_ae_rule_rec.charges_amt_alloc +
7385              l_line_tax_amt) <> 0)
7386             OR ((g_ae_rule_rec.freight_acctd_amt_alloc + g_ae_rule_rec.charges_acctd_amt_alloc +
7387                  l_line_tax_acctd_amt) <> 0)) THEN
7388        adj_code_combination_id := l_ae_line_rec.ae_account;
7389     END IF;
7390 
7391   /*----------------------------------------------------------------------------+
7392    | Assign Accounting Debits and Credits based on prorated amount signs        |
7393    +----------------------------------------------------------------------------*/
7394      Create_Debits_Credits(g_ae_rule_rec.freight_amt_alloc       +
7395                            g_ae_rule_rec.charges_amt_alloc       +
7396                            l_line_tax_amt                          ,
7397                            g_ae_rule_rec.freight_acctd_amt_alloc +
7398                            g_ae_rule_rec.charges_acctd_amt_alloc +
7399                            l_line_tax_acctd_amt                    ,
7400                            ''                                      ,
7401                            ''                                      ,
7402                            '','',
7403                            l_ae_line_rec );
7404 
7405   /*----------------------------------------------------------------------------+
7406    | Assign built sum of Charges and Freight amounts to Activity gl account     |
7407    +----------------------------------------------------------------------------*/
7408      Assign_Elements(l_ae_line_rec);
7409 
7410   END IF; --end if p_build_all is TRUE
7411 
7412   arp_standard.debug('ARP_ALLOCATION_PKG.Build_Charges_Freight_All()-');
7413 
7414 EXCEPTION
7415   WHEN OTHERS THEN
7416      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Build_Charges_Freight_All');
7417      RAISE;
7418 
7419 END Build_Charges_Freight_All;
7420 
7421 
7422 /* ==========================================================================
7423  | PROCEDURE Substitute_Ccid
7424  |
7425  | DESCRIPTION
7426  |    Builds the gain, loss, round account based on input parameters
7427  |
7428  | SCOPE - PRIVATE
7429  |
7430  | PARAMETERS
7431  |    p_coa_id                IN    Chart of Accounts id
7432  |    p_original_ccid         IN    Original ccid
7433  |    p_subs_ccid             IN    Substitute ccid
7434  |    p_actual_ccid           OUT   Actual or return ccid
7435  *==========================================================================*/
7436 PROCEDURE Substitute_Ccid(p_coa_id        IN  gl_sets_of_books.chart_of_accounts_id%TYPE        ,
7437                           p_original_ccid IN  ar_system_parameters.code_combination_id_gain%TYPE,
7438                           p_subs_ccid     IN  ar_system_parameters.code_combination_id_gain%TYPE,
7439                           p_actual_ccid   OUT NOCOPY ar_system_parameters.code_combination_id_gain%TYPE) IS
7440 
7441 l_concat_segs           varchar2(240)                                           ;
7442 l_concat_ids            varchar2(2000)                                          ;
7443 l_concat_descs          varchar2(2000)                                          ;
7444 l_arerror               varchar2(2000)                                          ;
7445 l_actual_gain_loss_ccid ar_system_parameters_all.code_combination_id_gain%TYPE  ;
7446 l_ctr                   BINARY_INTEGER                                          ;
7447 
7448 BEGIN
7449 
7450    IF PG_DEBUG in ('Y', 'C') THEN
7451       arp_standard.debug( 'ARP_ALLOCATION_PKG.Substitute_Ccid()+');
7452    END IF;
7453 
7454 /*----------------------------------------------------------------------------+
7455  | Set other in out variables used by flex routine                            |
7456  +----------------------------------------------------------------------------*/
7457    p_actual_ccid           := NULL;
7458    l_actual_gain_loss_ccid := NULL; --must always be derived
7459    l_concat_segs           := NULL;
7460    l_concat_ids            := NULL;
7461    l_concat_descs          := NULL;
7462 
7463 /*----------------------------------------------------------------------------+
7464  | Verify from Cache whether the final ccid for a given combination of chart  |
7465  | of accounts, orig ccid and substitute ccid already exists in cache.        |
7466  +----------------------------------------------------------------------------*/
7467 
7468    IF cache_ctr > 0 THEN
7469       FOR l_ctr IN flex_parms_tbl.FIRST .. flex_parms_tbl.LAST LOOP
7470           IF flex_parms_tbl(l_ctr).coa_id = p_coa_id AND
7471                 flex_parms_tbl(l_ctr).orig_ccid = p_original_ccid  AND
7472                    flex_parms_tbl(l_ctr).subs_ccid = p_subs_ccid THEN --hit found
7473 
7474              l_actual_gain_loss_ccid := flex_parms_tbl(l_ctr).actual_ccid;
7475 
7476              IF PG_DEBUG in ('Y', 'C') THEN
7477                 arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder Cache: Chart of Accounts ' || p_coa_id);
7478                 arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder Cache: Original CCID     ' || p_original_ccid);
7479                 arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder Cache: Substitute CCID   ' || p_subs_ccid);
7480                 arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder Cache: Actual CCID       ' || l_actual_gain_loss_ccid);
7481              END IF;
7482 
7483              EXIT; --exit loop as hit is found
7484 
7485           END IF;
7486       END LOOP;
7487    END IF;
7488 
7489 /*----------------------------------------------------------------------------+
7490  | Derive gain loss account using flex routine                                |
7491  +----------------------------------------------------------------------------*/
7492    IF l_actual_gain_loss_ccid is NULL THEN
7493 
7494       IF NOT ar_flexbuilder_wf_pkg.substitute_balancing_segment (
7495                                               x_arflexnum     => p_coa_id                         ,
7496                                               x_arorigccid    => p_original_ccid                  ,
7497                                               x_arsubsticcid  => p_subs_ccid                      ,
7498                                               x_return_ccid   => l_actual_gain_loss_ccid          ,
7499                                               x_concat_segs   => l_concat_segs                    ,
7500                                               x_concat_ids    => l_concat_ids                     ,
7501                                               x_concat_descrs => l_concat_descs                   ,
7502                                               x_arerror       => l_arerror                          ) THEN
7503 
7504        /*----------------------------------------------------------------------------+
7505         | Invalid account raise user exception                                       |
7506         +----------------------------------------------------------------------------*/
7507          RAISE flex_subs_ccid_error;
7508 
7509       END IF;
7510 
7511     /*----------------------------------------------------------------------------+
7512      | Cache the gain loss account as it has been successfully derived            |
7513      +----------------------------------------------------------------------------*/
7514       cache_ctr := cache_ctr + 1;  --counter is never reset within a success unit
7515       flex_parms_tbl(cache_ctr).coa_id      := p_coa_id;
7516       flex_parms_tbl(cache_ctr).orig_ccid   := p_original_ccid;
7517       flex_parms_tbl(cache_ctr).subs_ccid   := p_subs_ccid;
7518       flex_parms_tbl(cache_ctr).actual_ccid := l_actual_gain_loss_ccid;
7519 
7520       IF PG_DEBUG in ('Y', 'C') THEN
7521          arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder : Chart of Accounts ' || p_coa_id);
7522          arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder : Original CCID     ' || p_original_ccid);
7523          arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder : Substitute CCID   ' || p_subs_ccid);
7524          arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder : Actual CCID       ' || l_actual_gain_loss_ccid);
7525       END IF;
7526 
7527    END IF;
7528 
7529    p_actual_ccid := l_actual_gain_loss_ccid;
7530 
7531    IF PG_DEBUG in ('Y', 'C') THEN
7532       arp_standard.debug( 'ARP_ALLOCATION_PKG.Substitute_Ccid()-');
7533    END IF;
7534 
7535 EXCEPTION
7536 WHEN flex_subs_ccid_error  THEN
7537      IF PG_DEBUG in ('Y', 'C') THEN
7538         arp_standard.debug('Flexbuilder error: ARP_ALLOCATION_PKG.Substitute_Ccid');
7539         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Chart of Accounts ' || p_coa_id);
7540         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Original CCID     ' || p_original_ccid);
7541         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Substitute CCID   ' || p_subs_ccid);
7542         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Actual CCID       ' || l_actual_gain_loss_ccid);
7543      END IF;
7544      fnd_message.set_name('AR','AR_FLEX_CCID_ERROR');
7545      fnd_message.set_token('COA',TO_CHAR(p_coa_id));
7546      fnd_message.set_token('ORG_CCID',TO_CHAR(p_original_ccid));
7547      fnd_message.set_token('SUB_CCID',TO_CHAR(p_subs_ccid));
7548      RAISE;
7549 
7550 WHEN OTHERS THEN
7551      IF PG_DEBUG in ('Y', 'C') THEN
7552         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Substitute_Ccid');
7553         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Chart of Accounts ' || p_coa_id);
7554         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Original CCID     ' || p_original_ccid);
7555         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Substitute CCID   ' || p_subs_ccid);
7556         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Actual CCID       ' || l_actual_gain_loss_ccid);
7557      END IF;
7558      RAISE;
7559 
7560 END Substitute_Ccid;
7561 
7562 /* ==========================================================================
7563  | PROCEDURE Create_Debits_Credits
7564  |
7565  | DESCRIPTION
7566  |    Populates accounting debit and credit amounts and taxable amounts based
7567  |    on signs
7568  |
7569  | NOTES
7570  |    Them amounts and accounted amounts are always in the same sign as the
7571  |    other or zero. When both the amounts and accounted amounts are zero,
7572  |    the taxable amount or taxable accounted amount are used to determine
7573  |    the debits and credit entries.
7574  |
7575  |    This routine is never called when both amount and taxable amounts are
7576  |    zero.
7577  |
7578  | SCOPE - PRIVATE
7579  |
7580  | PARAMETERS
7581  |    p_amount                IN    Amount
7582  |    p_acctd_amount          IN    Accounted Amount
7583  |    p_taxable_amount        IN    Taxable Amount
7584  |    p_taxable_acctd_amount  IN    Taxable Accounted Amount
7585  |    p_ae_line_rec           OUT   Line record
7586  | History
7587  |  21-NOV-2003   Herve Yu    from_amount_dr      , from_amount_cr
7588  |                            from_acctd_amount_dr, from_acctd_amount_cr
7589  *==========================================================================*/
7590 PROCEDURE Create_Debits_Credits(p_amount               IN NUMBER       ,
7591                                 p_acctd_amount         IN NUMBER       ,
7592                                 p_taxable_amount       IN NUMBER       ,
7593                                 p_taxable_acctd_amount IN NUMBER       ,
7594                                 --HYU--{
7595                                 p_from_amount               IN NUMBER       ,
7596                                 p_from_acctd_amount         IN NUMBER       ,
7597                                 --HYU--}
7598                                 p_ae_line_rec          IN OUT NOCOPY ar_ae_alloc_rec_gt%ROWTYPE,
7599                                 p_paired_flag          IN VARCHAR2 DEFAULT NULL,
7600                                 p_calling_point        IN VARCHAR2 DEFAULT NULL) IS
7601 
7602 l_taxable_set BOOLEAN;
7603 
7604 BEGIN
7605    arp_standard.debug( 'ARP_ALLOCATION_PKG.Create_Debits_Credits()+');
7606 
7607    arp_standard.debug('p_amount ' || p_amount);
7608    arp_standard.debug('p_acctd_amount ' || p_acctd_amount);
7609    arp_standard.debug('p_taxable_amount' || p_taxable_amount);
7610    arp_standard.debug('p_taxable_acctd_amount' || p_taxable_acctd_amount);
7611    arp_standard.debug('p_calling_point       ' || p_calling_point);
7612 
7613    l_taxable_set := FALSE;
7614 
7615  /*----------------------------------------------------------------------------+
7616   | Set negativity indicator for 0 amounts and taxable amounts based on sign of|
7617   | the receivable. When summarizing finally just use the indicator do not set |
7618   +----------------------------------------------------------------------------*/
7619    IF ((p_amount = 0) AND (p_acctd_amount = 0) AND (nvl(p_taxable_amount,0) = 0)
7620       AND (nvl(p_taxable_acctd_amount,0) = 0) AND (nvl(p_calling_point,'X') <> 'SUMMARIZE')) THEN
7621 
7622       arp_standard.debug('g_ae_rule_rec.line_amt_alloc ' || g_ae_rule_rec.line_amt_alloc);
7623       arp_standard.debug('g_ae_rule_rec.tax_amt_alloc ' || g_ae_rule_rec.tax_amt_alloc);
7624       arp_standard.debug('p_paired_flag ' || p_paired_flag);
7625 
7626     --set negativity indicator for debits nullvalue indicates a credit
7627       IF ((g_ae_rule_rec.line_amt_alloc + g_ae_rule_rec.tax_amt_alloc) < 0) THEN
7628          p_ae_line_rec.ae_neg_ind := -1;
7629 
7630          IF nvl(p_paired_flag, 'N') = 'Y' THEN
7631             p_ae_line_rec.ae_neg_ind := NULL; --Create Credit to Collected tax account
7632          END IF;
7633 
7634          arp_standard.debug('p_ae_line_rec.ae_neg_ind < 0 condition ' || p_ae_line_rec.ae_neg_ind);
7635 
7636       ELSE
7637          p_ae_line_rec.ae_neg_ind := NULL; --set to create Credits
7638 
7639          IF nvl(p_paired_flag, 'N') = 'Y' THEN
7640             p_ae_line_rec.ae_neg_ind := -1;  --Create Debit to collected tax account
7641          END IF;
7642 
7643         IF PG_DEBUG in ('Y', 'C') THEN
7644          arp_standard.debug('p_ae_line_rec.ae_neg_ind >= 0 condition ' || p_ae_line_rec.ae_neg_ind);
7645         END IF;
7646 
7647       END IF; --default is null
7648 
7649    END IF; --end if negativity indicator set condition
7650 
7651  /*----------------------------------------------------------------------------+
7652   | Create accounting amount and taxable amount debits based on signs          |
7653   +----------------------------------------------------------------------------*/
7654    IF ((p_amount < 0) OR (p_acctd_amount < 0)) THEN
7655       p_ae_line_rec.ae_entered_dr           := abs(p_amount)              ;
7656       p_ae_line_rec.ae_accounted_dr         := abs(p_acctd_amount)        ;
7657 
7658       p_ae_line_rec.ae_entered_cr           := NULL                       ;
7659       p_ae_line_rec.ae_accounted_cr         := NULL                       ;
7660       p_ae_line_rec.ae_from_amount_dr       := abs(p_from_amount)              ;
7661       p_ae_line_rec.ae_from_acctd_amount_dr := abs(p_from_acctd_amount)        ;
7662       p_ae_line_rec.ae_from_amount_cr       := NULL                       ;
7663       p_ae_line_rec.ae_from_acctd_amount_cr := NULL                       ;
7664 
7665  --Tax amounts are zero, however taxable amounts used to determine Dr, Cr
7666    ELSIF (((p_amount = 0) AND (p_acctd_amount = 0))
7667           AND ((nvl(p_taxable_amount,0) <> 0) OR (nvl(p_taxable_acctd_amount,0) <> 0))) THEN
7668  /*----------------------------------------------------------------------------+
7669   | Create accounting amount and taxable amount credits based on sign          |
7670   +----------------------------------------------------------------------------*/
7671       IF ((nvl(p_taxable_amount,0) < 0) OR (nvl(p_taxable_acctd_amount,0) < 0)) THEN
7672          p_ae_line_rec.ae_entered_dr           := abs(p_amount)              ;
7673          p_ae_line_rec.ae_accounted_dr         := abs(p_acctd_amount)        ;
7674          p_ae_line_rec.ae_from_amount_dr       := abs(p_from_amount)              ;
7675          p_ae_line_rec.ae_from_acctd_amount_dr := abs(p_from_acctd_amount)        ;
7676 
7677          IF p_ae_line_rec.ae_line_type IN ('TAX','DEFERRED_TAX','ADJ_NON_REC_TAX',
7678                                            'EDISC_NON_REC_TAX','UNEDISC_NON_REC_TAX',
7679                                            'FINCHRG_NON_REC_TAX')
7680          THEN
7681             l_taxable_set := TRUE;
7682             p_ae_line_rec.ae_taxable_entered_dr   := abs(p_taxable_amount);
7683             p_ae_line_rec.ae_taxable_accounted_dr := abs(p_taxable_acctd_amount);
7684          END IF;
7685 
7686          p_ae_line_rec.ae_entered_cr           := NULL                       ;
7687          p_ae_line_rec.ae_accounted_cr         := NULL                       ;
7688          p_ae_line_rec.ae_taxable_entered_cr   := NULL                       ;
7689          p_ae_line_rec.ae_taxable_accounted_cr := NULL                       ;
7690          p_ae_line_rec.ae_from_amount_cr       := NULL                       ;
7691          p_ae_line_rec.ae_from_acctd_amount_cr := NULL                       ;
7692 
7693       ELSE  --Create Credits
7694          p_ae_line_rec.ae_entered_cr           := abs(p_amount)              ;
7695          p_ae_line_rec.ae_accounted_cr         := abs(p_acctd_amount)        ;
7696          p_ae_line_rec.ae_from_amount_cr       := abs(p_from_amount)         ;
7697          p_ae_line_rec.ae_from_acctd_amount_cr := abs(p_from_acctd_amount)   ;
7698 
7699          --{need to comment this out for taxable amount in the case of application
7700          --IF p_ae_line_rec.ae_line_type IN ('TAX','DEFERRED_TAX','ADJ_NON_REC_TAX',
7701          --                                  'EDISC_NON_REC_TAX','UNEDISC_NON_REC_TAX',
7702          --                                  'FINCHRG_NON_REC_TAX')
7703          --THEN
7704          --   l_taxable_set := TRUE;
7705            p_ae_line_rec.ae_taxable_entered_cr   := abs(p_taxable_amount);
7706            p_ae_line_rec.ae_taxable_accounted_cr := abs(p_taxable_acctd_amount);
7707          --END IF;
7708 
7709          p_ae_line_rec.ae_entered_dr           := NULL                       ;
7710          p_ae_line_rec.ae_accounted_dr         := NULL                       ;
7711          p_ae_line_rec.ae_taxable_entered_dr   := NULL                       ;
7712          p_ae_line_rec.ae_taxable_accounted_dr := NULL                       ;
7713          p_ae_line_rec.ae_from_amount_dr       := NULL              ;
7714          p_ae_line_rec.ae_from_acctd_amount_dr := NULL              ;
7715 
7716       END IF; --line amount is negative for Debits
7717 
7718  /*------------------------------------------------------------------------------------------+
7719   |Tax amounts are zero, taxable amounts are also zero use negativity indicator for Dr and Cr|
7720   |we need to do this for tax lines, and only when called from the final Summary routine.    |
7721   +------------------------------------------------------------------------------------------*/
7722    ELSIF (((p_amount = 0) AND (p_acctd_amount = 0))
7723           AND (nvl(p_taxable_amount,0) = 0) AND (nvl(p_taxable_acctd_amount,0) = 0)
7724           AND (nvl(p_calling_point, 'X') = 'SUMMARIZE')) THEN
7725 
7726           IF (nvl(p_ae_line_rec.ae_neg_ind,0) < 0) THEN --Create Debits
7727              p_ae_line_rec.ae_entered_dr           := abs(p_amount)               ;
7728              p_ae_line_rec.ae_accounted_dr         := abs(p_acctd_amount)         ;
7729              p_ae_line_rec.ae_taxable_entered_dr   := abs(p_taxable_amount)       ;
7730              p_ae_line_rec.ae_taxable_accounted_dr := abs(p_taxable_acctd_amount) ;
7731              p_ae_line_rec.ae_from_amount_dr       := abs(p_from_amount)              ;
7732              p_ae_line_rec.ae_from_acctd_amount_dr := abs(p_from_acctd_amount)        ;
7733 
7734              l_taxable_set := TRUE;
7735 
7736              p_ae_line_rec.ae_entered_cr           := NULL                       ;
7737              p_ae_line_rec.ae_accounted_cr         := NULL                       ;
7738              p_ae_line_rec.ae_taxable_entered_cr   := NULL                       ;
7739              p_ae_line_rec.ae_taxable_accounted_cr := NULL                       ;
7740              p_ae_line_rec.ae_from_amount_cr       := NULL                       ;
7741              p_ae_line_rec.ae_from_acctd_amount_cr := NULL                       ;
7742 
7743           ELSE --create Credits for accounting entry
7744              p_ae_line_rec.ae_entered_cr           := abs(p_amount)              ;
7745              p_ae_line_rec.ae_accounted_cr         := abs(p_acctd_amount)        ;
7746              p_ae_line_rec.ae_taxable_entered_cr   := abs(p_taxable_amount)      ;
7747              p_ae_line_rec.ae_taxable_accounted_cr := abs(p_taxable_acctd_amount);
7748              p_ae_line_rec.ae_from_amount_cr       := abs(p_from_amount)         ;
7749              p_ae_line_rec.ae_from_acctd_amount_cr := abs(p_from_acctd_amount)   ;
7750              l_taxable_set := TRUE;
7751 
7752              p_ae_line_rec.ae_entered_dr           := NULL                       ;
7753              p_ae_line_rec.ae_accounted_dr         := NULL                       ;
7754              p_ae_line_rec.ae_taxable_entered_dr   := NULL                       ;
7755              p_ae_line_rec.ae_taxable_accounted_dr := NULL                       ;
7756              p_ae_line_rec.ae_from_amount_dr       := NULL        ;
7757              p_ae_line_rec.ae_from_acctd_amount_dr := NULL        ;
7758 
7759           END IF; --negativity indicator
7760 
7761    ELSE --create Credits
7762  /*----------------------------------------------------------------------------+
7763   | Create accounting amount and taxable amount credits based on sign          |
7764   +----------------------------------------------------------------------------*/
7765       p_ae_line_rec.ae_entered_cr           := abs(p_amount)              ;
7766       p_ae_line_rec.ae_accounted_cr         := abs(p_acctd_amount)        ;
7767 
7768       p_ae_line_rec.ae_entered_dr           := NULL                       ;
7769       p_ae_line_rec.ae_accounted_dr         := NULL                       ;
7770       p_ae_line_rec.ae_from_amount_cr       := abs(p_from_amount)              ;
7771       p_ae_line_rec.ae_from_acctd_amount_cr := abs(p_from_acctd_amount)        ;
7772       p_ae_line_rec.ae_from_amount_dr       := NULL                       ;
7773       p_ae_line_rec.ae_from_acctd_amount_dr := NULL                       ;
7774 
7775    END IF; --sign of amounts
7776 
7777  /*----------------------------------------------------------------------------+
7778   | Set the taxable amounts and accounted amounts                              |
7779   +----------------------------------------------------------------------------*/
7780  -- This need to be executed every time???
7781  --  IF (p_ae_line_rec.ae_line_type IN ('TAX', 'DEFERRED_TAX', 'ADJ_NON_REC_TAX',
7782  --                                    'EDISC_NON_REC_TAX','UNEDISC_NON_REC_TAX',
7783  --                                    'FINCHRG_NON_REC_TAX')
7784  --     AND (NOT l_taxable_set)) THEN
7785 
7786  --      l_taxable_set := TRUE;
7787 
7788       IF ((nvl(p_taxable_amount,0) < 0) OR (nvl(p_taxable_acctd_amount,0) < 0))
7789       THEN
7790           p_ae_line_rec.ae_taxable_entered_dr   := abs(p_taxable_amount)       ;
7791           p_ae_line_rec.ae_taxable_accounted_dr := abs(p_taxable_acctd_amount) ;
7792           p_ae_line_rec.ae_taxable_entered_cr   := NULL                        ;
7793           p_ae_line_rec.ae_taxable_accounted_cr := NULL                        ;
7794 
7795       ELSIF (((nvl(p_taxable_amount,0) = 0) AND (nvl(p_taxable_acctd_amount,0) = 0))
7796              AND ((p_amount <> 0) OR (p_acctd_amount <> 0)))
7797       THEN
7798              IF ((p_amount < 0) OR (p_acctd_amount < 0)) THEN
7799                 p_ae_line_rec.ae_taxable_entered_dr    := abs(p_taxable_amount);
7800                 p_ae_line_rec.ae_taxable_accounted_dr  := abs(p_taxable_acctd_amount);
7801                 p_ae_line_rec.ae_taxable_entered_cr    := NULL;
7802                 p_ae_line_rec.ae_taxable_accounted_cr  := NULL;
7803              ELSE
7804                 p_ae_line_rec.ae_taxable_entered_cr    := abs(p_taxable_amount);
7805                 p_ae_line_rec.ae_taxable_accounted_cr  := abs(p_taxable_acctd_amount);
7806                 p_ae_line_rec.ae_taxable_entered_dr    := NULL;
7807                 p_ae_line_rec.ae_taxable_accounted_dr  := NULL;
7808              END IF;
7809       ELSE
7810           p_ae_line_rec.ae_taxable_entered_cr   := abs(p_taxable_amount)       ;
7811           p_ae_line_rec.ae_taxable_accounted_cr := abs(p_taxable_acctd_amount) ;
7812           p_ae_line_rec.ae_taxable_entered_dr   := NULL                        ;
7813           p_ae_line_rec.ae_taxable_accounted_dr := NULL                        ;
7814 
7815       END IF; --sign of taxable amount
7816 
7817 --   END IF; --if line type is tax and taxable amount is not set
7818 --   arp_standard.debug('Start Credit Debit Dump');
7819 --   Dump_Line_Amts(p_ae_line_rec);
7820 --   arp_standard.debug('End Credit Debit Dump');
7821 
7822    arp_standard.debug( 'ARP_ALLOCATION_PKG.Create_Debits_Credits()-');
7823 
7824 EXCEPTION
7825 WHEN OTHERS THEN
7826      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Create_Debits_Credits');
7827      RAISE;
7828 
7829 END Create_Debits_Credits;
7830 
7831 /* ==========================================================================
7832  | PROCEDURE Summarize_Accounting_Lines
7833  |
7834  | DESCRIPTION
7835  |    Net out the accounting for earned discounts, unearned discounts and
7836  |    payments, for adjustment (includes finance charges) multiple layers
7837  |    of accounting lines will not exist however this routine will be
7838  |    executed.
7839  |
7840  | SCOPE - PRIVATE
7841  |
7842  | PARAMETERS
7843  |    NONE
7844  | History
7845  |   21-NOV-2003  Herve Yu    from_amount_dr       , from_amount_cr,
7846  |                            from_acctd_amount_dr , from_acctd_amount_cr
7847  *==========================================================================*/
7848 PROCEDURE Summarize_Accounting_Lines IS
7849 
7850 l_ctr1           BINARY_INTEGER;
7851 l_ctr2           BINARY_INTEGER;
7852 --BUG#3509185  l_summ_ctr       BINARY_INTEGER := 0;
7853 l_ent_amt        NUMBER;
7854 l_ent_acctd_amt  NUMBER;
7855 l_txb_amt        NUMBER;
7856 l_txb_acctd_amt  NUMBER;
7857 l_from_ent_amt        NUMBER;
7858 l_from_ent_acctd_amt  NUMBER;
7859 l_ae_line_rec    ar_ae_alloc_rec_gt%ROWTYPE;
7860 l_ae_empty_rec   ar_ae_alloc_rec_gt%ROWTYPE;
7861 
7862 CURSOR summarize_lines IS
7863 SELECT /*+ INDEX(a1 AR_AE_ALLOC_REC_GT_N3) */
7864        NVL(a1.ae_entered_dr,0)           * -1 + NVL(a1.ae_entered_cr,0),
7865        NVL(a1.ae_accounted_dr,0)         * -1 + NVL(a1.ae_accounted_cr,0),
7866 --       SUM(NVL(ae_pro_amt,0)),
7867 --       SUM(NVL(ae_pro_acctd_amt,0)),
7868        NVL(a1.ae_taxable_entered_dr,0)   * -1 + NVL(a1.ae_taxable_entered_cr,0),
7869        NVL(a1.ae_taxable_accounted_dr,0) * -1 + NVL(a1.ae_taxable_accounted_cr,0),
7870        NVL(a1.ae_from_amount_dr,0)       * -1 + NVL(a1.ae_from_amount_cr,0),
7871        NVL(a1.ae_from_acctd_amount_dr,0) * -1 + NVL(a1.ae_from_acctd_amount_cr,0),
7872        a1.ae_line_type,
7873        a1.ae_line_type_secondary,
7874        a1.ae_source_id,
7875        a1.ae_source_table,
7876        a1.ae_account,
7877        a1.ae_source_id_secondary,
7878        a1.ae_source_table_secondary,
7879        a1.ae_currency_code,
7880        a1.ae_currency_conversion_rate,
7881        a1.ae_currency_conversion_type,
7882        a1.ae_currency_conversion_date,
7883        a1.ae_third_party_id,
7884        a1.ae_third_party_sub_id,
7885        a1.ae_tax_group_code_id,
7886        a1.ae_tax_code_id,
7887        a1.ae_location_segment_id,
7888        a1.ae_tax_link_id,
7889        decode(a1.ae_neg_ind,
7890               -1, decode(Retain_Neg_Ind(a1.rowid),
7891                          1, a1.ae_neg_ind,
7892                          ''),
7893               a1.ae_neg_ind),
7894        a1.ae_reversed_source_id,
7895        DECODE(a1.ae_cust_trx_line_gl_dist_id,0 ,''
7896                                             ,-1,'',a1.ae_customer_trx_line_id),
7897        DECODE(a1.ae_cust_trx_line_gl_dist_id,0 ,''
7898                                             ,-1,'',a1.ae_cust_trx_line_gl_dist_id),
7899        a1.ae_ref_line_id,
7900        a1.ref_account_class,
7901        a1.activity_bucket,
7902        a1.ref_dist_ccid,
7903        a1.ref_mf_dist_flag
7904 FROM ar_ae_alloc_rec_gt a1
7905 WHERE a1.ae_id = g_id
7906 AND a1.ae_summarize_flag = 'N'
7907 AND a1.ae_account_class IS NULL
7908 AND (NVL(a1.ae_entered_dr,0) <> 0 OR NVL(a1.ae_entered_cr,0) <> 0 OR
7909      NVL(a1.ae_accounted_dr,0) <> 0 OR NVL(a1.ae_accounted_cr,0) <> 0  /*6321537*/
7910      OR (a1.ae_line_type IN ('TAX','DEFERRED_TAX','EDISC_NON_REC_TAX',
7911                              'UNEDISC_NON_REC_TAX','ADJ_NON_REC_TAX',
7912                               'FINCHRG_NON_REC_TAX')
7913       AND (NVL(a1.ae_entered_dr,0) = 0 AND NVL(a1.ae_entered_cr,0) = 0 AND
7914            NVL(a1.ae_accounted_dr,0) = 0 AND NVL(a1.ae_accounted_cr,0) = 0)));
7915 --AND NVL(ae_pro_amt,0) <> 0
7916 --}
7917 /*
7918 GROUP BY  a1.ae_customer_trx_line_id,
7919           a1.ae_cust_trx_line_gl_dist_id,
7920           a1.ae_ref_line_id,
7921           a1.ae_line_type,
7922           a1.ae_line_type_secondary,
7923           a1.ae_source_id,
7924           a1.ae_source_table,
7925           a1.ae_account,
7926           a1.ae_source_id_secondary,
7927           a1.ae_source_table_secondary,
7928           a1.ae_currency_code,
7929           a1.ae_currency_conversion_rate,
7930           a1.ae_currency_conversion_type,
7931           a1.ae_currency_conversion_date,
7932           a1.ae_third_party_id,
7933           a1.ae_third_party_sub_id,
7934           a1.ae_tax_group_code_id,
7935           a1.ae_tax_code_id,
7936           a1.ae_location_segment_id,
7937           a1.ae_tax_link_id,
7938           decode(a1.ae_neg_ind,
7939                  -1, decode(Retain_Neg_Ind(a1.rowid),
7940                             1, a1.ae_neg_ind,
7941                             ''),
7942                  a1.ae_neg_ind),
7943           a1.ae_reversed_source_id,
7944 --{HYUDETUPT
7945           a1.ref_account_class,
7946           a1.activity_bucket
7947 --}
7948  ORDER BY  decode(a1.ae_line_type,
7949                 'EDISC'              ,-6,
7950                 'ADJ'                ,-6,
7951                 'FINCHRG'            ,-6,
7952                 'UNEDISC'            ,-6,
7953                 a1.ae_tax_link_id),
7954           decode(a1.ae_line_type,
7955                  'EDISC_NON_REC_TAX' , -5,
7956                  'ADJ_NON_REC_TAX'    ,-5,
7957                  'FINCHRG_NON_REC_TAX',-5,
7958                  'UNEDISC_NON_REC_TAX',-5,
7959                  'DEFERRED_TAX',decode(a1.ae_line_type_secondary,
7960                                        'EDISC'  , -4,
7961                                        'ADJ'    , -4,
7962                                        'FINCHRG', -4,
7963                                        'UNEDISC', -4,
7964                                        -2),
7965                  'TAX'         ,decode(a1.ae_line_type_secondary,
7966                                        'EDISC'  , -3,
7967                                        'ADJ'    , -3,
7968                                        'FINCHRG', -3,
7969                                        'UNEDISC', -3,
7970                                        -1),
7971                 a1.ae_tax_link_id);
7972 */
7973 
7974 BEGIN
7975     arp_standard.debug( 'ARP_ALLOCATION_PKG.Summarize_Accounting_Lines()+');
7976  /*------------------------------------------------------------------------------+
7977   |Summarize Accounting entries for revenue and tax to net out accounting entries|
7978   |because the table g_ae_line_tbl contains accounting for earned discounts,     |
7979   |unearned discounts and payments, so the requirement for another level of      |
7980   |summarization for netting                                                     |
7981   +------------------------------------------------------------------------------*/
7982    --Bulk processing of distribution records [Bug 6454022]
7983    IF NVL(g_simul_app,'N') = 'N' THEN
7984      IF (NVL(g_ae_sys_rec.sob_type,'P') = 'P') THEN
7985 
7986 	INSERT INTO  ar_distributions (
7987 		line_id,
7988 		source_id,
7989 		source_table,
7990 		source_type,
7991 		source_type_secondary,
7992 		code_combination_id,
7993 		amount_dr,
7994 		amount_cr,
7995 		acctd_amount_dr,
7996 		acctd_amount_cr,
7997 		created_by,
7998 		creation_date,
7999 		last_updated_by,
8000 		last_update_date,
8001 		last_update_login,
8002 		source_id_secondary,
8003 		source_table_secondary,
8004 		currency_code        ,
8005 		currency_conversion_rate,
8006 		currency_conversion_type,
8007 		currency_conversion_date,
8008 		third_party_id,
8009 		third_party_sub_id,
8010 		tax_code_id,
8011 		location_segment_id,
8012 		taxable_entered_dr,
8013 		taxable_entered_cr,
8014 		taxable_accounted_dr,
8015 		taxable_accounted_cr,
8016 		tax_link_id,
8017 		reversed_source_id,
8018 		tax_group_code_id,
8019 		org_id,
8020 		ref_customer_trx_line_id,
8021 		ref_cust_trx_line_gl_dist_id,
8022 		ref_line_id,
8023 		from_amount_dr,
8024 		from_amount_cr,
8025 		from_acctd_amount_dr,
8026 		from_acctd_amount_cr,
8027 		ref_account_class,
8028 		activity_bucket,
8029 		ref_dist_ccid,
8030 		ref_mf_dist_flag
8031   	 )
8032 	 SELECT   ar_distributions_s.nextval,
8033 		  al.ae_source_id,
8034 		  al.ae_source_table,
8035 		  al.ae_line_type,
8036 		  al.ae_line_type_secondary,
8037 		  al.ae_account,
8038 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN  abs(amount)
8039 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
8040 		         CASE WHEN (nvl(taxable_amount,0) < 0) OR
8041 				   (nvl(taxable_acctd_amount,0) < 0) THEN abs(amount)
8042 			      WHEN (nvl(taxable_amount,0) = 0) AND
8043 				   (nvl(taxable_acctd_amount,0) = 0) AND
8044 				   (nvl(ae_neg_ind,0) < 0) THEN abs(amount)
8045 			      ELSE null END
8046 		  ELSE  null END  ae_entered_dr,
8047 
8048 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN null
8049 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
8050 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
8051 				   (nvl(taxable_acctd_amount,0) < 0) THEN null
8052 			      WHEN (nvl(taxable_amount,0) = 0) AND
8053 				   (nvl(taxable_acctd_amount,0) = 0) AND
8054 				   (nvl(ae_neg_ind,0) < 0) THEN null
8055 			 ELSE abs(amount) END
8056 		  ELSE abs(amount) END ae_entered_cr,
8057 
8058 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN abs(acctd_amount)
8059 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
8060 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
8061 			           (nvl(taxable_acctd_amount,0) < 0) THEN abs(acctd_amount)
8062 			      WHEN (nvl(taxable_amount,0) = 0) AND
8063 				   (nvl(taxable_acctd_amount,0) = 0) AND
8064 				   (nvl(ae_neg_ind,0) < 0) THEN abs(acctd_amount)
8065 			 ELSE null END
8066 		  ELSE null END ae_accounted_dr,
8067 
8068 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN null
8069 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
8070 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
8071 				   (nvl(taxable_acctd_amount,0) < 0) THEN null
8072 			      WHEN (nvl(taxable_amount,0) = 0) AND
8073 				   (nvl(taxable_acctd_amount,0) = 0) AND
8074 				   (nvl(ae_neg_ind,0) < 0) THEN null
8075 			 ELSE abs(acctd_amount) END
8076 		  ELSE abs(acctd_amount) END ae_accounted_cr,
8077 
8078 		  arp_standard.profile.user_id,
8079 		  SYSDATE,
8080 		  arp_standard.profile.user_id,
8081 		  SYSDATE,
8082 		  arp_standard.profile.last_update_login,
8083 		  al.ae_source_id_secondary,
8084 		  al.ae_source_table_secondary,
8085 		  al.ae_currency_code,
8086 		  al.ae_currency_conversion_rate,
8087 		  al.ae_currency_conversion_type,
8088 		  al.ae_currency_conversion_date,
8089 		  al.ae_third_party_id,
8090 		  al.ae_third_party_sub_id,
8091 		  al.ae_tax_code_id,
8092 		  al.ae_location_segment_id,
8093 		  CASE WHEN ( nvl(taxable_amount,0) < 0) OR
8094 			    ( nvl(taxable_acctd_amount,0) < 0) THEN abs(taxable_amount)
8095 		       WHEN ( nvl(taxable_amount,0) = 0 ) AND
8096 			    ( nvl(taxable_acctd_amount,0) = 0 ) AND
8097 			    ( ( amount < 0 ) OR ( acctd_amount < 0 )) THEN abs(taxable_amount)
8098 		  ELSE	null END  ae_taxable_entered_dr,
8099 
8100 		  CASE WHEN ( nvl(taxable_amount,0) < 0) OR
8101 			    ( nvl(taxable_acctd_amount,0) < 0) THEN null
8102 		       WHEN ( nvl(taxable_amount,0) = 0 ) AND
8103 			    ( nvl(taxable_acctd_amount,0) = 0 ) AND
8104 			    ( ( amount < 0 ) OR ( acctd_amount < 0 ))THEN null
8105 		  ELSE abs(taxable_amount) END ae_taxable_entered_cr,
8106 
8107 		  CASE WHEN ( nvl(taxable_amount,0) < 0) OR
8108 			    ( nvl(taxable_acctd_amount,0) < 0)THEN abs(taxable_acctd_amount)
8109 		       WHEN ( nvl(taxable_amount,0) = 0 ) AND
8110 			    ( nvl(taxable_acctd_amount,0) = 0 ) AND
8111 			    ( ( amount < 0 ) OR ( acctd_amount < 0 ))THEN abs(taxable_acctd_amount)
8112 		  ELSE null END ae_taxable_accounted_dr,
8113 
8114 		  CASE WHEN ( nvl(taxable_amount,0) < 0) OR
8115 			    ( nvl(taxable_acctd_amount,0) < 0)THEN null
8116 		       WHEN ( nvl(taxable_amount,0) = 0 ) AND
8117 			    ( nvl(taxable_acctd_amount,0) = 0 ) AND
8118 			    ( ( amount < 0 ) OR ( acctd_amount < 0 ))THEN null
8119 		  ELSE	abs(taxable_acctd_amount) END ae_taxable_accounted_cr ,
8120 		  al.ae_tax_link_id,
8121 		  al.ae_reversed_source_id,
8122 		  al.ae_tax_group_code_id,
8123 		  arp_standard.sysparm.org_id, /* SSA changes anuj */
8124 		  al.ae_customer_trx_line_id,
8125 		  al.ae_cust_trx_line_gl_dist_id,
8126 		  al.ae_ref_line_id,
8127 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN abs(from_amount)
8128 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
8129 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
8130 				   (nvl(taxable_acctd_amount,0) < 0)THEN abs(from_amount)
8131 			      WHEN (nvl(taxable_amount,0) = 0) AND
8132 				   (nvl(taxable_acctd_amount,0) = 0) AND
8133 				   (nvl(ae_neg_ind,0) < 0) THEN abs(from_amount)
8134 			      ELSE null END
8135 		  ELSE null END ae_from_amount_dr,
8136 
8137 		  CASE WHEN (amount < 0) OR (acctd_amount < 0)THEN null
8138 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
8139 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
8140 				   (nvl(taxable_acctd_amount,0) < 0) THEN null
8141 			      WHEN (nvl(taxable_amount,0) = 0) AND
8142 				   (nvl(taxable_acctd_amount,0) = 0) AND
8143 				   (nvl(ae_neg_ind,0) < 0)THEN null
8144 			 ELSE abs(from_amount) END
8145 		  ELSE abs(from_amount) END ae_from_amount_cr,
8146 
8147 		  CASE WHEN (amount < 0) OR (acctd_amount < 0)THEN abs(from_acctd_amount)
8148 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
8149 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
8150 				   (nvl(taxable_acctd_amount,0) < 0)THEN abs(from_acctd_amount)
8151 			      WHEN (nvl(taxable_amount,0) = 0) AND
8152 				   (nvl(taxable_acctd_amount,0) = 0) AND
8153 				   (nvl(ae_neg_ind,0) < 0)THEN abs(from_acctd_amount)
8154 			 ELSE null END
8155 		  ELSE null END ae_from_acctd_amount_dr,
8156 
8157 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN null
8158 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
8159 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
8160 				   (nvl(taxable_acctd_amount,0) < 0)THEN null
8161 			      WHEN (nvl(taxable_amount,0) = 0) AND
8162 				   (nvl(taxable_acctd_amount,0) = 0) AND
8163 				   (nvl(ae_neg_ind,0) < 0)THEN null
8164 			 ELSE abs(from_acctd_amount) END
8165 		  ELSE abs(from_acctd_amount) END ae_from_acctd_amount_cr,
8166 		  al.ref_account_class,
8167 		  al.activity_bucket,
8168 		  al.ref_dist_ccid,
8169 		  al.ref_mf_dist_flag
8170 	  from
8171 	  ( --Defined it as a subquery as some of the values[Amount columns] derived in this
8172 	    --  select are used for further calculations in the main select.
8173 	    SELECT /*+ INDEX(a1 AR_AE_ALLOC_REC_GT_N3) */
8174 		   NVL(a1.ae_entered_dr,0)           * -1 + NVL(a1.ae_entered_cr,0) amount,
8175 		   NVL(a1.ae_accounted_dr,0)         * -1 + NVL(a1.ae_accounted_cr,0) acctd_amount,
8176 		   NVL(a1.ae_taxable_entered_dr,0)   * -1 + NVL(a1.ae_taxable_entered_cr,0)  taxable_amount,
8177 		   NVL(a1.ae_taxable_accounted_dr,0) * -1 + NVL(a1.ae_taxable_accounted_cr,0) taxable_acctd_amount,
8178 		   NVL(a1.ae_from_amount_dr,0)       * -1 + NVL(a1.ae_from_amount_cr,0)  from_amount,
8179 		   NVL(a1.ae_from_acctd_amount_dr,0) * -1 + NVL(a1.ae_from_acctd_amount_cr,0) from_acctd_amount,
8180 		   a1.ae_line_type,
8181 		   a1.ae_line_type_secondary,
8182 		   a1.ae_source_id,
8183 		   a1.ae_source_table,
8184 		   a1.ae_account,
8185 		   a1.ae_source_id_secondary,
8186 		   a1.ae_source_table_secondary,
8187 		   a1.ae_currency_code,
8188 		   a1.ae_currency_conversion_rate,
8189 		   a1.ae_currency_conversion_type,
8190 		   a1.ae_currency_conversion_date,
8191 		   a1.ae_third_party_id,
8192 		   a1.ae_third_party_sub_id,
8193 		   a1.ae_tax_group_code_id,
8194 		   a1.ae_tax_code_id,
8195 		   a1.ae_location_segment_id,
8196 		   a1.ae_tax_link_id,
8197 		   decode(a1.ae_neg_ind,
8198 			  -1, decode(Retain_Neg_Ind(a1.rowid),
8199 				     1, a1.ae_neg_ind,
8200 				     ''),
8201 			  a1.ae_neg_ind) ae_neg_ind,
8202 		   a1.ae_reversed_source_id,
8203 		   DECODE(a1.ae_cust_trx_line_gl_dist_id,0 ,''
8204 							,-1,'',a1.ae_customer_trx_line_id)
8205 							ae_customer_trx_line_id,
8206 		   DECODE(a1.ae_cust_trx_line_gl_dist_id,0 ,''
8207 							,-1,'',a1.ae_cust_trx_line_gl_dist_id)
8208 							ae_cust_trx_line_gl_dist_id,
8209 		   a1.ae_ref_line_id,
8210 		   a1.ref_account_class,
8211 		   a1.activity_bucket,
8212 		   a1.ref_dist_ccid,
8213 		   a1.ref_mf_dist_flag
8214 	    FROM ar_ae_alloc_rec_gt a1
8215 	    WHERE a1.ae_id = g_id
8216 	    AND a1.ae_summarize_flag = 'N'
8217 	    AND a1.ae_account_class IS NULL
8218 	    AND (NVL(a1.ae_entered_dr,0) <> 0 OR NVL(a1.ae_entered_cr,0) <> 0
8219               OR NVL(a1.ae_accounted_dr,0) <> 0 OR NVL(a1.ae_accounted_cr,0) <> 0
8220               OR (a1.ae_line_type IN ('TAX','DEFERRED_TAX','EDISC_NON_REC_TAX',
8221                      'UNEDISC_NON_REC_TAX','ADJ_NON_REC_TAX','FINCHRG_NON_REC_TAX')
8222                 AND (NVL(a1.ae_entered_dr,0) = 0 AND NVL(a1.ae_entered_cr,0) = 0 AND
8223                      NVL(a1.ae_accounted_dr,0) = 0 AND NVL(a1.ae_accounted_cr,0) = 0)))
8224 	  ) al;
8225      END IF;
8226    --If the simulation flag is set to Y then continue with the existing logic
8227    ELSE
8228    OPEN summarize_lines;
8229 
8230    LOOP
8231      /*----------------------------------------------------------------------------+
8232       | Initialise summarize record                                                |
8233       +----------------------------------------------------------------------------*/
8234        l_ae_line_rec := l_ae_empty_rec ;
8235 
8236        FETCH summarize_lines
8237        INTO l_ent_amt       ,
8238             l_ent_acctd_amt ,
8239             l_txb_amt       ,
8240             l_txb_acctd_amt ,
8241             l_from_ent_amt      ,
8242             l_from_ent_acctd_amt,
8243             l_ae_line_rec.ae_line_type,
8244             l_ae_line_rec.ae_line_type_secondary,
8245             l_ae_line_rec.ae_source_id,
8246             l_ae_line_rec.ae_source_table,
8247             l_ae_line_rec.ae_account,
8248             l_ae_line_rec.ae_source_id_secondary,
8249             l_ae_line_rec.ae_source_table_secondary,
8250             l_ae_line_rec.ae_currency_code,
8251             l_ae_line_rec.ae_currency_conversion_rate,
8252             l_ae_line_rec.ae_currency_conversion_type,
8253             l_ae_line_rec.ae_currency_conversion_date,
8254             l_ae_line_rec.ae_third_party_id,
8255             l_ae_line_rec.ae_third_party_sub_id,
8256             l_ae_line_rec.ae_tax_group_code_id,
8257             l_ae_line_rec.ae_tax_code_id,
8258             l_ae_line_rec.ae_location_segment_id,
8259             l_ae_line_rec.ae_tax_link_id,
8260             l_ae_line_rec.ae_neg_ind,
8261             l_ae_line_rec.ae_reversed_source_id,
8262             l_ae_line_rec.ae_customer_trx_line_id,
8263             l_ae_line_rec.ae_cust_trx_line_gl_dist_id,
8264             l_ae_line_rec.ae_ref_line_id,
8265             l_ae_line_rec.ref_account_class,
8266             l_ae_line_rec.activity_bucket,
8267 			l_ae_line_rec.ref_dist_ccid,
8268             l_ae_line_rec.ref_mf_dist_flag;
8269 
8270        --Set cursor not found flag
8271          IF summarize_lines%NOTFOUND THEN
8272             EXIT;
8273          END IF;
8274 
8275        /*------------------------------------------------------------------------------+
8276         | Build final debits and credits for entered amount. It is important to route  |
8277         | through the Create Debits and Credits function, rather than directly insert  |
8278         | into the ar_ae_alloc_rec_gt.                                                   |
8279         +------------------------------------------------------------------------------*/
8280          Create_Debits_Credits(l_ent_amt, l_ent_acctd_amt,
8281                                l_txb_amt, l_txb_acctd_amt,
8282                                l_from_ent_amt, l_from_ent_acctd_amt,
8283                                l_ae_line_rec,
8284                                NULL, 'SUMMARIZE');
8285 
8286          --BUG#3509185 l_summ_ctr := l_summ_ctr + 1;
8287          l_ae_line_rec.ae_summarize_flag := 'Y';
8288 
8289          Dump_Line_Amts(l_ae_line_rec);
8290 
8291          --BUG#3509185  Insert_Ae_Lines(p_ae_line_tbl => l_ae_line_rec);
8292          --{BUG#3509185
8293            Cache_Ae_Lines(p_ae_line_tbl => l_ae_line_rec);
8294          --}
8295 
8296 
8297    END LOOP; --End loop summarize lines
8298 
8299    CLOSE summarize_lines;
8300    END IF;
8301 
8302    --g_ae_summ_ctr := l_summ_ctr;
8303 
8304    arp_standard.debug( 'ARP_ALLOCATION_PKG.Summarize_Accounting_Lines()-');
8305 
8306 EXCEPTION
8307 WHEN OTHERS THEN
8308      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Summarize_Accounting_Lines:'||SQLERRM);
8309      RAISE;
8310 
8311 END Summarize_Accounting_Lines;
8312 
8313 
8314 /* ==========================================================================
8315  | PROCEDURE Summarize_Acct_Lines_Hdr_Level
8316  |
8317  | DESCRIPTION
8318  |    Net out the accounting for earned discounts, unearned discounts and
8319  |    payments, for adjustment (includes finance charges) multiple layers
8320  |    of accounting lines will not exist however this routine will be
8321  |    executed.
8322  |
8323  | SCOPE - PRIVATE
8324  |
8325  | PARAMETERS
8326  |    NONE
8327  *==========================================================================*/
8328 PROCEDURE Summarize_Acct_Lines_Hdr_Level IS
8329 
8330 l_ctr1           BINARY_INTEGER;
8331 l_ctr2           BINARY_INTEGER;
8332 l_ent_amt        NUMBER;
8333 l_ent_acctd_amt  NUMBER;
8334 l_txb_amt        NUMBER;
8335 l_txb_acctd_amt  NUMBER;
8336 l_from_ent_amt        NUMBER;
8337 l_from_ent_acctd_amt  NUMBER;
8338 l_ae_line_rec    ar_ae_alloc_rec_gt%ROWTYPE;
8339 l_ae_empty_rec   ar_ae_alloc_rec_gt%ROWTYPE;
8340 
8341 CURSOR summarize_lines IS
8342 SELECT /*+ INDEX(a1 AR_AE_ALLOC_REC_GT_N3) */
8343        SUM(NVL(a1.ae_entered_dr,0)           * -1 + NVL(a1.ae_entered_cr,0)),
8344        SUM(NVL(a1.ae_accounted_dr,0)         * -1 + NVL(a1.ae_accounted_cr,0)),
8345        SUM(NVL(a1.ae_taxable_entered_dr,0)   * -1 + NVL(a1.ae_taxable_entered_cr,0)),
8346        SUM(NVL(a1.ae_taxable_accounted_dr,0) * -1 + NVL(a1.ae_taxable_accounted_cr,0)),
8347        SUM(NVL(a1.ae_from_amount_dr,0)       * -1 + NVL(a1.ae_from_amount_cr,0)),
8348        SUM(NVL(a1.ae_from_acctd_amount_dr,0) * -1 + NVL(a1.ae_from_acctd_amount_cr,0)),
8349        a1.ae_line_type,
8350        a1.ae_line_type_secondary,
8351        a1.ae_source_id,
8352        a1.ae_source_table,
8353        a1.ae_account,
8354        a1.ae_source_id_secondary,
8355        a1.ae_source_table_secondary,
8356        a1.ae_currency_code,
8357        a1.ae_currency_conversion_rate,
8358        a1.ae_currency_conversion_type,
8359        a1.ae_currency_conversion_date,
8360        a1.ae_third_party_id,
8361        a1.ae_third_party_sub_id,
8362        a1.ae_tax_group_code_id,
8363        a1.ae_tax_code_id,
8364        a1.ae_location_segment_id,
8365        a1.ae_tax_link_id,
8366        decode(a1.ae_neg_ind,
8367               -1, decode(Retain_Neg_Ind(a1.rowid),
8368                          1, a1.ae_neg_ind,
8369                          ''),
8370               a1.ae_neg_ind),
8371        a1.ae_reversed_source_id
8372 FROM ar_ae_alloc_rec_gt a1
8373 WHERE a1.ae_id = g_id
8374 AND a1.ae_summarize_flag = 'N'
8375 AND a1.ae_account_class IS NULL
8376 GROUP BY  a1.ae_line_type,
8377           a1.ae_line_type_secondary,
8378           a1.ae_source_id,
8379           a1.ae_source_table,
8380           a1.ae_account,
8381           a1.ae_source_id_secondary,
8382           a1.ae_source_table_secondary,
8383           a1.ae_currency_code,
8384           a1.ae_currency_conversion_rate,
8385           a1.ae_currency_conversion_type,
8386           a1.ae_currency_conversion_date,
8387           a1.ae_third_party_id,
8388           a1.ae_third_party_sub_id,
8389           a1.ae_tax_group_code_id,
8390           a1.ae_tax_code_id,
8391           a1.ae_location_segment_id,
8392           a1.ae_tax_link_id,
8393           decode(a1.ae_neg_ind,
8394                  -1, decode(Retain_Neg_Ind(a1.rowid),
8395                             1, a1.ae_neg_ind,
8396                             ''),
8397                  a1.ae_neg_ind),
8398           a1.ae_reversed_source_id
8399  ORDER BY  decode(a1.ae_line_type,
8400                 'EDISC'              ,-6,
8401                 'ADJ'                ,-6,
8402                 'FINCHRG'            ,-6,
8403                 'UNEDISC'            ,-6,
8404                 a1.ae_tax_link_id),
8405           decode(a1.ae_line_type,
8406                  'EDISC_NON_REC_TAX' , -5,
8407                  'ADJ_NON_REC_TAX'    ,-5,
8408                  'FINCHRG_NON_REC_TAX',-5,
8409                  'UNEDISC_NON_REC_TAX',-5,
8410                  'DEFERRED_TAX',decode(a1.ae_line_type_secondary,
8411                                        'EDISC'  , -4,
8412                                        'ADJ'    , -4,
8413                                        'FINCHRG', -4,
8414                                        'UNEDISC', -4,
8415                                        -2),
8416                  'TAX'         ,decode(a1.ae_line_type_secondary,
8417                                        'EDISC'  , -3,
8418                                        'ADJ'    , -3,
8419                                        'FINCHRG', -3,
8420                                        'UNEDISC', -3,
8421                                        -1),
8422                 a1.ae_tax_link_id);
8423 
8424 BEGIN
8425 
8426    arp_standard.debug( 'ARP_ALLOCATION_PKG.Summarize_Acct_Lines_Hdr_Level()+');
8427 
8428  /*------------------------------------------------------------------------------+
8429   |Summarize Accounting entries for revenue and tax to net out accounting entries|
8430   |because the table g_ae_line_tbl contains accounting for earned discounts,     |
8431   |unearned discounts and payments, so the requirement for another level of      |
8432   |summarization for netting                                                     |
8433   +------------------------------------------------------------------------------*/
8434    OPEN summarize_lines;
8435 
8436    LOOP
8437      /*----------------------------------------------------------------------------+
8438       | Initialise summarize record                                                |
8439       +----------------------------------------------------------------------------*/
8440        l_ae_line_rec := l_ae_empty_rec ;
8441 
8442        FETCH summarize_lines
8443        INTO l_ent_amt       ,
8444             l_ent_acctd_amt ,
8445             l_txb_amt       ,
8446             l_txb_acctd_amt ,
8447             l_from_ent_amt      ,
8448             l_from_ent_acctd_amt,
8449             l_ae_line_rec.ae_line_type,
8450             l_ae_line_rec.ae_line_type_secondary,
8451             l_ae_line_rec.ae_source_id,
8452             l_ae_line_rec.ae_source_table,
8453             l_ae_line_rec.ae_account,
8454             l_ae_line_rec.ae_source_id_secondary,
8455             l_ae_line_rec.ae_source_table_secondary,
8456             l_ae_line_rec.ae_currency_code,
8457             l_ae_line_rec.ae_currency_conversion_rate,
8458             l_ae_line_rec.ae_currency_conversion_type,
8459             l_ae_line_rec.ae_currency_conversion_date,
8460             l_ae_line_rec.ae_third_party_id,
8461             l_ae_line_rec.ae_third_party_sub_id,
8462             l_ae_line_rec.ae_tax_group_code_id,
8463             l_ae_line_rec.ae_tax_code_id,
8464             l_ae_line_rec.ae_location_segment_id,
8465             l_ae_line_rec.ae_tax_link_id,
8466             l_ae_line_rec.ae_neg_ind,
8467             l_ae_line_rec.ae_reversed_source_id;
8468 
8469        --Set cursor not found flag
8470          IF summarize_lines%NOTFOUND THEN
8471             EXIT;
8472          END IF;
8473 
8474        /*------------------------------------------------------------------------------+
8475         | Build final debits and credits for entered amount. It is important to route  |
8476         | through the Create Debits and Credits function, rather than directly insert  |
8477         | into the ar_ae_alloc_rec_gt.                                                   |
8478         +------------------------------------------------------------------------------*/
8479          Create_Debits_Credits(l_ent_amt, l_ent_acctd_amt,
8480                                l_txb_amt, l_txb_acctd_amt,
8481                                l_from_ent_amt, l_from_ent_acctd_amt,
8482                                l_ae_line_rec,
8483                                NULL, 'SUMMARIZE');
8484 
8485          l_ae_line_rec.ae_summarize_flag := 'Y';
8486 
8487          IF nvl(g_simul_app,'N') = 'N' THEN
8488             Insert_Ae_Lines(p_ae_line_tbl => l_ae_line_rec);
8489 
8490          ELSIF  nvl(g_simul_app,'N') = 'Y' THEN
8491             Cache_Ae_Lines(p_ae_line_tbl => l_ae_line_rec);
8492          END IF;
8493 
8494 	 IF PG_DEBUG IN ('Y','C') THEN
8495 	   Dump_Line_Amts(l_ae_line_rec);
8496 	 END IF;
8497 
8498    END LOOP; --End loop summarize lines
8499 
8500    CLOSE summarize_lines;
8501 
8502    arp_standard.debug( 'ARP_ALLOCATION_PKG.Summarize_Acct_Lines_Hdr_Level()-');
8503 
8504 EXCEPTION
8505 WHEN OTHERS THEN
8506      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Summarize_Acct_Lines_Hdr_Level');
8507      RAISE;
8508 
8509 END Summarize_Acct_Lines_Hdr_Level;
8510 
8511 
8512 /* ==========================================================================
8513  | PROCEDURE Get_Tax_Count
8514  |
8515  | DESCRIPTION
8516  |    Gets tax code, location count for a invoice line.
8517  |
8518  | SCOPE - PRIVATE
8519  |
8520  | PARAMETERS
8521  |    NONE
8522  *==========================================================================*/
8523 FUNCTION Get_Tax_Count(p_invoice_line_id IN NUMBER) RETURN NUMBER IS
8524 
8525 l_count  NUMBER := 0;
8526 
8527 BEGIN
8528 
8529    select sum(a1.tax_count)
8530    into l_count
8531    from (select 1                                  tax_count
8532          from ra_customer_trx_lines a2
8533          where a2.line_type = 'TAX'
8534          and   a2.link_to_cust_trx_line_id = p_invoice_line_id
8535          group by nvl(a2.location_segment_id, a2.vat_tax_id),
8536                   decode(a2.location_segment_id,
8537                          '', 'VAT',
8538                          'LOC')
8539         ) a1;
8540 
8541    RETURN(l_count);
8542 
8543 EXCEPTION
8544    WHEN NO_DATA_FOUND THEN
8545         RAISE;
8546    WHEN OTHERS THEN
8547         RAISE;
8548 
8549 END Get_Tax_Count;
8550 
8551 /* ==========================================================================
8552  | PROCEDURE Retain_Neg_Ind
8553  |
8554  | DESCRIPTION
8555  |    Ascertains as to whether there is another non zero line into which the
8556  |    0 amount line (amount, accounted amount, taxable amount, taxable
8557  |    accounted amount) can get merged
8558  |
8559  | SCOPE - PRIVATE
8560  |
8561  | PARAMETERS
8562  |    NONE
8563  *==========================================================================*/
8564 FUNCTION Retain_Neg_Ind(p_rowid IN ROWID) RETURN NUMBER IS
8565 
8566 l_amount NUMBER := 0;
8567 
8568 BEGIN
8569 
8570      SELECT /*+ INDEX(a1 AR_AE_ALLOC_REC_GT_N3) */
8571             ABS(SUM(NVL(a1.ae_entered_dr,0)           * -1 + NVL(a1.ae_entered_cr,0))) +
8572             ABS(SUM(NVL(a1.ae_accounted_dr,0)         * -1 + NVL(a1.ae_accounted_cr,0))) +
8573             ABS(SUM(NVL(a1.ae_taxable_entered_dr,0)   * -1 + NVL(a1.ae_taxable_entered_cr,0))) +
8574             ABS(SUM(NVL(a1.ae_taxable_accounted_dr,0) * -1 + NVL(a1.ae_taxable_accounted_cr,0)))
8575      INTO l_amount
8576      FROM ar_ae_alloc_rec_gt a1,
8577           ar_ae_alloc_rec_gt a2
8578      WHERE a1.ae_id = g_id
8579      AND a1.ae_summarize_flag = 'N'
8580      AND a1.ae_account_class IS NULL
8581      AND a2.rowid = p_rowid
8582      AND a1.rowid <> a2.rowid
8583      AND nvl(a1.ae_line_type, '-99') = nvl(a2.ae_line_type, '-99')
8584      AND nvl(a1.ae_line_type_secondary, '-99') = nvl(a2.ae_line_type_secondary, '-99')
8585      AND nvl(a1.ae_source_id,-9999) = nvl(a2.ae_source_id,-9999)
8586      AND nvl(a1.ae_source_table,'-99') = nvl(a2.ae_source_table,'-99')
8587      AND nvl(a1.ae_account,-9999) = nvl(a2.ae_account,-9999)
8588      AND nvl(a1.ae_source_id_secondary,-9999) = nvl(a2.ae_source_id_secondary,-9999)
8589      AND nvl(a1.ae_source_table_secondary,'-99') = nvl(a2.ae_source_table_secondary,'-99')
8590      AND nvl(a1.ae_currency_code, '-99') = nvl(a2.ae_currency_code, '-99')
8591      AND nvl(a1.ae_currency_conversion_rate, -9999) = nvl(a2.ae_currency_conversion_rate, -9999)
8592      AND nvl(a1.ae_currency_conversion_type,'-99') = nvl(a2.ae_currency_conversion_type,'-99')
8593      AND nvl(a1.ae_currency_conversion_date,to_date('01-01-1949','DD-MM-YYYY'))
8594               = nvl(a2.ae_currency_conversion_date,to_date('01-01-1949','DD-MM-YYYY'))
8595      AND nvl(a1.ae_third_party_id, -9999) = nvl(a2.ae_third_party_id, -9999)
8596      AND nvl(a1.ae_third_party_sub_id, -9999) = nvl(a2.ae_third_party_sub_id, -9999)
8597      AND nvl(a1.ae_tax_group_code_id, -9999) = nvl(a2.ae_tax_group_code_id, -9999)
8598      AND nvl(a1.ae_tax_code_id, -9999) = nvl(a2.ae_tax_code_id, -9999)
8599      AND nvl(a1.ae_location_segment_id, -9999) = nvl(a2.ae_location_segment_id, -9999)
8600      AND nvl(a1.ae_tax_link_id, -9999) = nvl(a2.ae_tax_link_id, -9999)
8601      AND nvl(a1.ae_reversed_source_id,-9999) = nvl(a2.ae_reversed_source_id,-9999);
8602 
8603      IF l_amount <> 0 THEN
8604         RETURN(0);
8605      ELSE
8606         RETURN(1);
8607      END IF;
8608 
8609 EXCEPTION
8610 WHEN NO_DATA_FOUND THEN
8611      RETURN(1);
8612 WHEN OTHERS THEN
8613      RAISE;
8614 
8615 END Retain_Neg_Ind;
8616 
8617 /* ==========================================================================
8618  | PROCEDURE Assign_Elements
8619  |
8620  | DESCRIPTION
8621  |    Assign revenue or tax lines built to global table which will eventually
8622  |    be summarized
8623  |
8624  | SCOPE - PRIVATE
8625  |
8626  | PARAMETERS
8627  |    NONE
8628  | Hidtory
8629  |   21-NOV-2003   Herve Yu  from_amount_dr      , from_amount_cr,
8630  |                           from_acctd_amount_dr, from_acctd_amount_cr
8631  *==========================================================================*/
8632 PROCEDURE Assign_Elements(p_ae_line_rec           IN ar_ae_alloc_rec_gt%ROWTYPE) IS
8633 
8634 BEGIN
8635    IF PG_DEBUG IN ('Y','C') THEN
8636     arp_standard.debug( 'ARP_ALLOCATION_PKG.Assign_Elements()+');
8637    END IF;
8638    /*------------------------------------------------------+
8639     | Store AE Line elements in AE Lines temp table        |
8640     +------------------------------------------------------*/
8641     g_ae_ctr := g_ae_ctr +1;
8642 
8643     insert into ar_ae_alloc_rec_gt
8644     ( ae_id,
8645       ae_account_class            ,
8646       ae_customer_trx_id          ,
8647       ae_customer_trx_line_id     ,
8648       ae_link_to_cust_trx_line_id ,
8649       ae_tax_type                 ,
8650       ae_code_combination_id      ,
8651       ae_collected_tax_ccid       ,
8652       ae_line_amount              ,
8653       ae_amount                   ,
8654       ae_acctd_amount             ,
8655       ae_taxable_amount           ,
8656       ae_taxable_acctd_amount     ,
8657       ae_adj_ccid                 ,
8658       ae_edisc_ccid               ,
8659       ae_unedisc_ccid             ,
8660       ae_finchrg_ccid             ,
8661       ae_adj_non_rec_tax_ccid     ,
8662       ae_edisc_non_rec_tax_ccid   ,
8663       ae_unedisc_non_rec_tax_ccid ,
8664       ae_finchrg_non_rec_tax_ccid ,
8665       ae_override_ccid1           ,
8666       ae_override_ccid2           ,
8667       ae_tax_link_id              ,
8668       ae_tax_link_id_ed_adj       ,
8669       ae_tax_link_id_uned         ,
8670       ae_tax_link_id_act          ,
8671       ae_pro_amt                  ,
8672       ae_pro_acctd_amt            ,
8673       ae_pro_chrg_amt             ,
8674       ae_pro_chrg_acctd_amt       ,
8675       ae_pro_taxable_amt          ,
8676       ae_pro_taxable_acctd_amt    ,
8677       ae_counted_flag             ,
8678       ae_autotax                  ,
8679       ae_sum_alloc_amt            ,
8680       ae_sum_alloc_acctd_amt      ,
8681       ae_tax_line_count           ,
8682       ae_line_type                   ,
8683       ae_line_type_secondary         ,
8684       ae_source_id                   ,
8685       ae_source_table                ,
8686       ae_account                     ,
8687       ae_entered_dr                  ,
8688       ae_entered_cr                  ,
8689       ae_accounted_dr                ,
8690       ae_accounted_cr                ,
8691       ae_source_id_secondary         ,
8692       ae_source_table_secondary      ,
8693       ae_currency_code               ,
8694       ae_currency_conversion_rate    ,
8695       ae_currency_conversion_type    ,
8696       ae_currency_conversion_date    ,
8697       ae_third_party_id              ,
8698       ae_third_party_sub_id          ,
8699       ae_tax_group_code_id           ,
8700       ae_tax_code_id                 ,
8701       ae_location_segment_id         ,
8702       ae_taxable_entered_dr          ,
8703       ae_taxable_entered_cr          ,
8704       ae_taxable_accounted_dr        ,
8705       ae_taxable_accounted_cr        ,
8706       ae_reversed_source_id          ,
8707       ae_neg_ind                     ,
8708       ae_summarize_flag              ,
8709       ae_cust_trx_line_gl_dist_id    ,
8710       ae_ref_line_id                 ,
8711       ae_from_amount_dr              ,
8712       ae_from_amount_cr              ,
8713       ae_from_acctd_amount_dr        ,
8714       ae_from_acctd_amount_cr        ,
8715       ref_account_class,
8716       activity_bucket,
8717       ref_dist_ccid,
8718       ref_mf_dist_flag
8719       )
8720 VALUES
8721     ( g_id,
8722       p_ae_line_rec.ae_account_class            ,
8723       p_ae_line_rec.ae_customer_trx_id          ,
8724       p_ae_line_rec.ae_customer_trx_line_id     ,
8725       p_ae_line_rec.ae_link_to_cust_trx_line_id ,
8726       p_ae_line_rec.ae_tax_type                 ,
8727       p_ae_line_rec.ae_code_combination_id      ,
8728       p_ae_line_rec.ae_collected_tax_ccid       ,
8729       p_ae_line_rec.ae_line_amount              ,
8730       p_ae_line_rec.ae_amount                   ,
8731       p_ae_line_rec.ae_acctd_amount             ,
8732       p_ae_line_rec.ae_taxable_amount           ,
8733       p_ae_line_rec.ae_taxable_acctd_amount     ,
8734       p_ae_line_rec.ae_adj_ccid                 ,
8735       p_ae_line_rec.ae_edisc_ccid               ,
8736       p_ae_line_rec.ae_unedisc_ccid             ,
8737       p_ae_line_rec.ae_finchrg_ccid             ,
8738       p_ae_line_rec.ae_adj_non_rec_tax_ccid     ,
8739       p_ae_line_rec.ae_edisc_non_rec_tax_ccid   ,
8740       p_ae_line_rec.ae_unedisc_non_rec_tax_ccid ,
8741       p_ae_line_rec.ae_finchrg_non_rec_tax_ccid ,
8742       p_ae_line_rec.ae_override_ccid1           ,
8743       p_ae_line_rec.ae_override_ccid2           ,
8744       p_ae_line_rec.ae_tax_link_id              ,
8745       p_ae_line_rec.ae_tax_link_id_ed_adj       ,
8746       p_ae_line_rec.ae_tax_link_id_uned         ,
8747       p_ae_line_rec.ae_tax_link_id_act          ,
8748       p_ae_line_rec.ae_pro_amt                  ,
8749       p_ae_line_rec.ae_pro_acctd_amt            ,
8750       p_ae_line_rec.ae_pro_chrg_amt             ,
8751       p_ae_line_rec.ae_pro_chrg_acctd_amt       ,
8752       p_ae_line_rec.ae_pro_taxable_amt          ,
8753       p_ae_line_rec.ae_pro_taxable_acctd_amt    ,
8754       p_ae_line_rec.ae_counted_flag             ,
8755       p_ae_line_rec.ae_autotax                  ,
8756       p_ae_line_rec.ae_sum_alloc_amt            ,
8757       p_ae_line_rec.ae_sum_alloc_acctd_amt      ,
8758       p_ae_line_rec.ae_tax_line_count           ,
8759       p_ae_line_rec.ae_line_type                   ,
8760       p_ae_line_rec.ae_line_type_secondary         ,
8761       p_ae_line_rec.ae_source_id                   ,
8762       p_ae_line_rec.ae_source_table                ,
8763       p_ae_line_rec.ae_account                     ,
8764       p_ae_line_rec.ae_entered_dr                  ,
8765       p_ae_line_rec.ae_entered_cr                  ,
8766       p_ae_line_rec.ae_accounted_dr                ,
8767       p_ae_line_rec.ae_accounted_cr                ,
8768       p_ae_line_rec.ae_source_id_secondary         ,
8769       p_ae_line_rec.ae_source_table_secondary      ,
8770       p_ae_line_rec.ae_currency_code               ,
8771       p_ae_line_rec.ae_currency_conversion_rate    ,
8772       p_ae_line_rec.ae_currency_conversion_type    ,
8773       p_ae_line_rec.ae_currency_conversion_date    ,
8774       p_ae_line_rec.ae_third_party_id              ,
8775       p_ae_line_rec.ae_third_party_sub_id          ,
8776       p_ae_line_rec.ae_tax_group_code_id           ,
8777       p_ae_line_rec.ae_tax_code_id                 ,
8778       p_ae_line_rec.ae_location_segment_id         ,
8779       p_ae_line_rec.ae_taxable_entered_dr          ,
8780       p_ae_line_rec.ae_taxable_entered_cr          ,
8781       p_ae_line_rec.ae_taxable_accounted_dr        ,
8782       p_ae_line_rec.ae_taxable_accounted_cr        ,
8783       p_ae_line_rec.ae_reversed_source_id          ,
8784       p_ae_line_rec.ae_neg_ind                     ,
8785       NVL(p_ae_line_rec.ae_summarize_flag,'N')     ,
8786       p_ae_line_rec.ae_cust_trx_line_gl_dist_id    ,
8787       p_ae_line_rec.ae_ref_line_id                 ,
8788       p_ae_line_rec.ae_from_amount_dr              ,
8789       p_ae_line_rec.ae_from_amount_cr              ,
8790       p_ae_line_rec.ae_from_acctd_amount_dr        ,
8791       p_ae_line_rec.ae_from_acctd_amount_cr        ,
8792       p_ae_line_rec.ref_account_class,
8793       p_ae_line_rec.activity_bucket,
8794       p_ae_line_rec.ref_dist_ccid,
8795       p_ae_line_rec.ref_mf_dist_flag
8796       );
8797 
8798     Dump_Line_Amts(p_ae_line_rec);
8799 
8800    IF PG_DEBUG IN ('Y','C') THEN
8801     arp_standard.debug( 'ARP_ALLOCATION_PKG.Assign_Elements()-');
8802    END IF;
8803 EXCEPTION
8804   WHEN OTHERS THEN
8805      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Assign_Elements');
8806      RAISE;
8807 
8808 END Assign_Elements;
8809 
8810 /*========================================================================
8811  | PRIVATE PROCEDURE Insert_Ae_Lines
8812  |
8813  | DESCRIPTION
8814  |      Inserts into AR_DISTRIBUTIONS accounting lines
8815  |      ----------------------------------------------
8816  |      Calls the table handler for AR_DISTRIBUTIONS to insert accounting
8817  |      for a given document into the underlying table.
8818  |
8819  | PARAMETERS
8820  |      p_ae_line_tbl   IN      Accounting lines table
8821  | History
8822  |   24-NOV-2003  Herve Yu     Distributions in the receipt currency
8823  *=======================================================================*/
8824 PROCEDURE Insert_Ae_Lines(p_ae_line_tbl IN ar_ae_alloc_rec_gt%ROWTYPE) IS
8825 
8826   l_ae_line_rec         ar_distributions%ROWTYPE;
8827   l_ae_line_rec_empty   ar_distributions%ROWTYPE;
8828   l_dummy               ar_distributions.line_id%TYPE;
8829 
8830 BEGIN
8831 
8832   arp_standard.debug( 'ARP_ALLOCATION_PKG.Insert_Ae_Lines()+');
8833 
8834 -- Initialize
8835    l_ae_line_rec := l_ae_line_rec_empty;
8836 
8837 -- Assign AE Line elements
8838    l_ae_line_rec.source_type              :=  p_ae_line_tbl.ae_line_type;
8839    l_ae_line_rec.source_type_secondary    :=  p_ae_line_tbl.ae_line_type_secondary;
8840    l_ae_line_rec.source_id                :=  p_ae_line_tbl.ae_source_id;
8841    l_ae_line_rec.source_table             :=  p_ae_line_tbl.ae_source_table;
8842    l_ae_line_rec.code_combination_id      :=  p_ae_line_tbl.ae_account;
8843    l_ae_line_rec.amount_dr                :=  p_ae_line_tbl.ae_entered_dr;
8844    l_ae_line_rec.amount_cr                :=  p_ae_line_tbl.ae_entered_cr;
8845    l_ae_line_rec.acctd_amount_dr          :=  p_ae_line_tbl.ae_accounted_dr;
8846    l_ae_line_rec.acctd_amount_cr          :=  p_ae_line_tbl.ae_accounted_cr;
8847    l_ae_line_rec.from_amount_dr           :=  p_ae_line_tbl.ae_from_amount_dr;
8848    l_ae_line_rec.from_amount_cr           :=  p_ae_line_tbl.ae_from_amount_cr;
8849    l_ae_line_rec.from_acctd_amount_dr     :=  p_ae_line_tbl.ae_from_acctd_amount_dr;
8850    l_ae_line_rec.from_acctd_amount_cr     :=  p_ae_line_tbl.ae_from_acctd_amount_cr;
8851    l_ae_line_rec.source_id_secondary      :=  p_ae_line_tbl.ae_source_id_secondary;
8852    l_ae_line_rec.source_table_secondary   :=  p_ae_line_tbl.ae_source_table_secondary;
8853    l_ae_line_rec.currency_code            :=  p_ae_line_tbl.ae_currency_code;
8854    l_ae_line_rec.currency_conversion_rate :=  p_ae_line_tbl.ae_currency_conversion_rate;
8855    l_ae_line_rec.currency_conversion_type :=  p_ae_line_tbl.ae_currency_conversion_type;
8856    l_ae_line_rec.currency_conversion_date :=  p_ae_line_tbl.ae_currency_conversion_date;
8857    l_ae_line_rec.third_party_id           :=  p_ae_line_tbl.ae_third_party_id;
8858    l_ae_line_rec.third_party_sub_id       :=  p_ae_line_tbl.ae_third_party_sub_id;
8859    l_ae_line_rec.tax_group_code_id        :=  p_ae_line_tbl.ae_tax_group_code_id;
8860    l_ae_line_rec.tax_code_id              :=  p_ae_line_tbl.ae_tax_code_id;
8861    l_ae_line_rec.location_segment_id      :=  p_ae_line_tbl.ae_location_segment_id;
8862    l_ae_line_rec.taxable_entered_dr       :=  p_ae_line_tbl.ae_taxable_entered_dr;
8863    l_ae_line_rec.taxable_entered_cr       :=  p_ae_line_tbl.ae_taxable_entered_cr;
8864    l_ae_line_rec.taxable_accounted_dr     :=  p_ae_line_tbl.ae_taxable_accounted_dr;
8865    l_ae_line_rec.taxable_accounted_cr     :=  p_ae_line_tbl.ae_taxable_accounted_cr;
8866    l_ae_line_rec.tax_link_id              :=  p_ae_line_tbl.ae_tax_link_id;
8867    l_ae_line_rec.reversed_source_id       :=  p_ae_line_tbl.ae_reversed_source_id;
8868    l_ae_line_rec.ref_account_class                :=  p_ae_line_tbl.ref_account_class;
8869    l_ae_line_rec.activity_bucket                   :=  p_ae_line_tbl.activity_bucket;
8870    l_ae_line_rec.ref_customer_trx_line_id :=  p_ae_line_tbl.ae_customer_trx_line_id;
8871    l_ae_line_rec.ref_cust_trx_line_gl_dist_id :=  p_ae_line_tbl.ae_cust_trx_line_gl_dist_id;
8872    l_ae_line_rec.ref_dist_ccid    :=  p_ae_line_tbl.ref_dist_ccid;
8873    l_ae_line_rec.ref_mf_dist_flag :=  p_ae_line_tbl.ref_mf_dist_flag;
8874 
8875    l_ae_line_rec.ref_line_id              :=  p_ae_line_tbl.ae_ref_line_id;
8876 
8877    Dump_Dist_Amts(l_ae_line_rec);
8878 
8879 --Insert into ar_distributions
8880    IF (NVL(g_ae_sys_rec.sob_type,'P') = 'P') THEN
8881 
8882        arp_distributions_pkg.insert_p(l_ae_line_rec, l_dummy);
8883 --{BUG#4301323
8884 --   ELSE
8885         /* need to insert records into the MRC table.  Calling new
8886            mrc engine */
8887 
8888                -- before we call the ar_mrc_engine, we need the line_id of
8889         -- the primary row.   If the Source type is EXCH_GAIN, EXCH_LOSS
8890         -- or CURR_ROUND, use a new line_id from the sequence.
8891 --  arp_standard.debug('source type = ' || l_ae_line_rec.source_type);
8892 --        if (l_ae_line_rec.source_type = 'EXCH_GAIN' or
8893 --           l_ae_line_rec.source_type = 'EXCH_LOSS' or
8894 --           l_ae_line_rec.source_type = 'CURR_ROUND' )  THEN
8895 --           select  ar_distributions_s.nextval
8896 --             into l_ae_line_rec.line_id
8897 --           from dual;
8898 --        ELSE
8899 --         BEGIN
8900 --           select line_id
8901 --             into l_ae_line_rec.line_id
8902 --             from ar_distributions
8903 --            where source_id = l_ae_line_rec.source_id
8904 --              and source_table = l_ae_line_rec.source_table
8905 --              and source_type = l_ae_line_rec.source_type
8906 --              and (source_type_secondary =
8907 --                           l_ae_line_rec.source_type_secondary
8908 --                   OR source_type_secondary IS NULL)
8909 --              and ( amount_dr = l_ae_line_rec.amount_dr OR
8910 --                    amount_dr IS NULL)
8911 --              and ( amount_cr = l_ae_line_rec.amount_cr OR
8912 --                    amount_cr IS NULL)
8913 --              and code_combination_id = l_ae_line_rec.code_combination_id;
8914 --           EXCEPTION
8915 --            WHEN OTHERS THEN
8916 --             arp_standard.debug('Can not determine Line id so created new one');
8917 --             select  ar_distributions_s.nextval
8918 --               into l_ae_line_rec.line_id
8919 --               from dual;
8920 --           END;
8921 --           arp_standard.debug('line id = ' || to_char(l_ae_line_rec.line_id));
8922 --        END IF;
8923 --        arp_standard.debug('before calling mrc_acct_main');
8924 --        arp_mrc_acct_main.insert_mrc_dis_data
8925 --                     (l_ae_line_rec,
8926 --                      arp_acct_main.ae_sys_rec.set_of_books_id);
8927 --}
8928    END IF;
8929    arp_standard.debug( 'ARP_ACCT_MAIN.Insert_Ae_Lines()-');
8930 
8931 END Insert_Ae_Lines;
8932 
8933 
8934 /*========================================================================
8935  | PRIVATE PROCEDURE Cache_Ae_Lines
8936  |
8937  | DESCRIPTION
8938  |      Inserts into AR_DISTRIBUTIONS accounting lines
8939  |      ----------------------------------------------
8940  |      Calls the table handler for AR_DISTRIBUTIONS to insert accounting
8941  |      for a given document into the underlying table.
8942  |
8943  | PARAMETERS
8944  |      p_ae_line_tbl   IN      Accounting lines table
8945  |
8946  *=======================================================================*/
8947 PROCEDURE Cache_Ae_Lines(p_ae_line_tbl IN ar_ae_alloc_rec_gt%ROWTYPE) IS
8948 
8949 BEGIN
8950 
8951 
8952   IF PG_DEBUG in ('Y', 'C') THEN
8953      arp_standard.debug( 'ARP_ALLOCATION_PKG.Cache_Ae_Lines()+');
8954   END IF;
8955 
8956   g_ae_summ_ctr := g_ae_summ_ctr + 1;
8957 
8958   g_ae_summarize_tbl(g_ae_summ_ctr).ae_line_type               :=  p_ae_line_tbl.ae_line_type;
8959   g_ae_summarize_tbl(g_ae_summ_ctr).source_id                  :=  p_ae_line_tbl.ae_source_id;
8960   g_ae_summarize_tbl(g_ae_summ_ctr).source_table               :=  p_ae_line_tbl.ae_source_table;
8961   g_ae_summarize_tbl(g_ae_summ_ctr).account                    :=  p_ae_line_tbl.ae_account;
8962   g_ae_summarize_tbl(g_ae_summ_ctr).entered_dr                 :=  p_ae_line_tbl.ae_entered_dr;
8963   g_ae_summarize_tbl(g_ae_summ_ctr).entered_cr                 :=  p_ae_line_tbl.ae_entered_cr;
8964   g_ae_summarize_tbl(g_ae_summ_ctr).accounted_dr               :=  p_ae_line_tbl.ae_accounted_dr;
8965   g_ae_summarize_tbl(g_ae_summ_ctr).accounted_cr               :=  p_ae_line_tbl.ae_accounted_cr;
8966   g_ae_summarize_tbl(g_ae_summ_ctr).ae_line_type_secondary     :=  p_ae_line_tbl.ae_line_type_secondary;
8967   g_ae_summarize_tbl(g_ae_summ_ctr).source_id_secondary        :=  p_ae_line_tbl.ae_source_id_secondary;
8968   g_ae_summarize_tbl(g_ae_summ_ctr).source_table_secondary     :=  p_ae_line_tbl.ae_source_table_secondary;
8969   g_ae_summarize_tbl(g_ae_summ_ctr).currency_code              :=  p_ae_line_tbl.ae_currency_code;
8970   g_ae_summarize_tbl(g_ae_summ_ctr).currency_conversion_rate   :=  p_ae_line_tbl.ae_currency_conversion_rate;
8971   g_ae_summarize_tbl(g_ae_summ_ctr).currency_conversion_type   :=  p_ae_line_tbl.ae_currency_conversion_type;
8972   g_ae_summarize_tbl(g_ae_summ_ctr).currency_conversion_date   :=  p_ae_line_tbl.ae_currency_conversion_date;
8973   g_ae_summarize_tbl(g_ae_summ_ctr).third_party_id             :=  p_ae_line_tbl.ae_third_party_id;
8974   g_ae_summarize_tbl(g_ae_summ_ctr).third_party_sub_id         :=  p_ae_line_tbl.ae_third_party_sub_id;
8975   g_ae_summarize_tbl(g_ae_summ_ctr).tax_group_code_id          :=  p_ae_line_tbl.ae_tax_group_code_id;
8976   g_ae_summarize_tbl(g_ae_summ_ctr).tax_code_id                :=  p_ae_line_tbl.ae_tax_code_id;
8977   g_ae_summarize_tbl(g_ae_summ_ctr).location_segment_id        :=  p_ae_line_tbl.ae_location_segment_id;
8978   g_ae_summarize_tbl(g_ae_summ_ctr).taxable_entered_dr         :=  p_ae_line_tbl.ae_taxable_entered_dr;
8979   g_ae_summarize_tbl(g_ae_summ_ctr).taxable_entered_cr         :=  p_ae_line_tbl.ae_taxable_entered_cr;
8980   g_ae_summarize_tbl(g_ae_summ_ctr).taxable_accounted_dr       :=  p_ae_line_tbl.ae_taxable_accounted_dr;
8981   g_ae_summarize_tbl(g_ae_summ_ctr).taxable_accounted_cr       :=  p_ae_line_tbl.ae_taxable_accounted_cr;
8982   g_ae_summarize_tbl(g_ae_summ_ctr).tax_link_id                :=  p_ae_line_tbl.ae_tax_link_id;
8983   g_ae_summarize_tbl(g_ae_summ_ctr).reversed_source_id         :=  p_ae_line_tbl.ae_reversed_source_id;
8984   g_ae_summarize_tbl(g_ae_summ_ctr).ref_customer_trx_line_id   :=  p_ae_line_tbl.ae_customer_trx_line_id;
8985   g_ae_summarize_tbl(g_ae_summ_ctr).ref_cust_trx_line_gl_dist_id:=  p_ae_line_tbl.ae_cust_trx_line_gl_dist_id;
8986   g_ae_summarize_tbl(g_ae_summ_ctr).ref_line_id                :=  p_ae_line_tbl.ae_ref_line_id;
8987   g_ae_summarize_tbl(g_ae_summ_ctr).activity_bucket            :=  p_ae_line_tbl.activity_bucket;
8988   g_ae_summarize_tbl(g_ae_summ_ctr).ref_account_class          :=  p_ae_line_tbl.ref_account_class;
8989   g_ae_summarize_tbl(g_ae_summ_ctr).ref_dist_ccid              :=  p_ae_line_tbl.ref_dist_ccid;
8990   g_ae_summarize_tbl(g_ae_summ_ctr).ref_mf_dist_flag           :=  p_ae_line_tbl.ref_mf_dist_flag;
8991   g_ae_summarize_tbl(g_ae_summ_ctr).from_amount_dr             :=  p_ae_line_tbl.ae_from_amount_dr;
8992   g_ae_summarize_tbl(g_ae_summ_ctr).from_amount_cr             :=  p_ae_line_tbl.ae_from_amount_cr;
8993   g_ae_summarize_tbl(g_ae_summ_ctr).from_acctd_amount_dr       :=  p_ae_line_tbl.ae_from_acctd_amount_dr;
8994   g_ae_summarize_tbl(g_ae_summ_ctr).from_acctd_amount_cr       :=  p_ae_line_tbl.ae_from_acctd_amount_cr;
8995 
8996 
8997 
8998   IF PG_DEBUG in ('Y', 'C') THEN
8999      arp_standard.debug( 'ARP_ALLOCATION_PKG.Cache_Ae_Lines()-');
9000   END IF;
9001 
9002 EXCEPTION
9003 WHEN OTHERS THEN
9004   IF PG_DEBUG in ('Y', 'C') THEN
9005      arp_standard.debug( 'OTHERS EXCEPTION : ARP_ALLOCATION_PKG.Cache_Ae_Lines()');
9006   END IF;
9007 
9008 END Cache_Ae_Lines;
9009 
9010 /*========================================================================
9011  | PRIVATE FUNCTION  Set_Adj_CCID
9012  |
9013  | DESCRIPTION
9014  |      Sets the global Variable adj_code_combination_id to NULL
9015  |      Gets the global Variable adj_code_combination_id
9016  |      Used by library ARXTWADJ.pld
9017  |
9018  | PARAMETERS
9019  |      p_action   IN VARCHAR2
9020  *=======================================================================*/
9021 FUNCTION Set_Adj_CCID(p_action IN VARCHAR2) RETURN NUMBER IS
9022 
9023 BEGIN
9024    IF PG_DEBUG in ('Y', 'C') THEN
9025       arp_standard.debug( 'ARP_ACCT_MAIN.Set_Adj_CCID()+');
9026    END IF;
9027 
9028    IF p_action = 'S' THEN --Set
9029       adj_code_combination_id := '';
9030    END IF;
9031 
9032    IF PG_DEBUG in ('Y', 'C') THEN
9033       arp_standard.debug('Set_Adj_CCID: ' ||  'adj_code_combination_id ' || adj_code_combination_id);
9034    END IF;
9035 
9036    RETURN(adj_code_combination_id);
9037 
9038    IF PG_DEBUG in ('Y', 'C') THEN
9039       arp_standard.debug( 'ARP_ACCT_MAIN.Set_Adj_CCID()-');
9040    END IF;
9041 
9042 EXCEPTION
9043 WHEN OTHERS THEN
9044     IF PG_DEBUG in ('Y', 'C') THEN
9045        arp_standard.debug( 'Exception : ARP_ACCT_MAIN.Set_Adj_CCID');
9046     END IF;
9047     RAISE;
9048 
9049 END Set_Adj_CCID;
9050 
9051 
9052 /* ==========================================================================
9053  | PROCEDURE Dump_Alloc_Rev_Tax
9054  |
9055  | DESCRIPTION
9056  |    Dumps data stored in the Allocation Revenue or Tax table
9057  |
9058  | SCOPE - PRIVATE
9059  |
9060  | PARAMETERS
9061  |    p_type                 IN      Indicates Rev or Tax
9062  |    p_alloc_rec            IN      Allocation details Record
9063  *==========================================================================*/
9064 PROCEDURE Dump_Alloc_Rev_Tax(p_type IN VARCHAR2, p_alloc_rec IN ar_ae_alloc_rec_gt%ROWTYPE) IS
9065 
9066 BEGIN
9067    IF PG_DEBUG in ('Y', 'C') THEN
9068       arp_standard.debug( 'ARP_ALLOCATION_PKG.Dump_Alloc_Rev_Tax()+');
9069       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_id = ' ||
9070                                      p_alloc_rec.ae_id);
9071       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_account_class = ' ||
9072                                      p_alloc_rec.ae_account_class);
9073       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_customer_trx_id = ' ||
9074                                      p_alloc_rec.ae_customer_trx_id);
9075       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_customer_trx_line_id = ' ||
9076                                      p_alloc_rec.ae_customer_trx_line_id);
9077       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_link_to_cust_trx_line_id= ' ||
9078                                      p_alloc_rec.ae_link_to_cust_trx_line_id);
9079       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_type= ' ||
9080                                      p_alloc_rec.ae_tax_type);
9081       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_code_combination_id= ' ||
9082                                      p_alloc_rec.ae_code_combination_id);
9083       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_collected_tax_ccid= ' ||
9084                                      p_alloc_rec.ae_collected_tax_ccid);
9085       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_line_amount= ' ||
9086                                      p_alloc_rec.ae_line_amount);
9087       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_amount= ' ||
9088                                      p_alloc_rec.ae_amount);
9089       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_acctd_amount= ' ||
9090                                      p_alloc_rec.ae_acctd_amount);
9091       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_group_code_id= ' ||
9092                                      p_alloc_rec.ae_tax_group_code_id);
9093       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_id= ' ||
9094                                      p_alloc_rec.ae_tax_id);
9095       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_taxable_amount= ' ||
9096                                      p_alloc_rec.ae_taxable_amount);
9097       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_taxable_acctd_amount= ' ||
9098                                      p_alloc_rec.ae_taxable_acctd_amount);
9099       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_adj_ccid= ' ||
9100                                      p_alloc_rec.ae_adj_ccid);
9101       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_edisc_ccid= ' ||
9102                                      p_alloc_rec.ae_edisc_ccid);
9103       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_unedisc_ccid= ' ||
9104                                      p_alloc_rec.ae_unedisc_ccid);
9105       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_finchrg_ccid= ' ||
9106                                      p_alloc_rec.ae_finchrg_ccid);
9107       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_adj_non_rec_tax_ccid= ' ||
9108                                      p_alloc_rec.ae_adj_non_rec_tax_ccid);
9109       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_edisc_non_rec_tax_ccid= ' ||
9110                                      p_alloc_rec.ae_edisc_non_rec_tax_ccid);
9111       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_unedisc_non_rec_tax_ccid= ' ||
9112                                      p_alloc_rec.ae_unedisc_non_rec_tax_ccid);
9113       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_finchrg_non_rec_tax_ccid= ' ||
9114                                      p_alloc_rec.ae_finchrg_non_rec_tax_ccid);
9115       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_override_ccid1= ' ||
9116                                      p_alloc_rec.ae_override_ccid1);
9117       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_override_ccid2= ' ||
9118                                      p_alloc_rec.ae_override_ccid2);
9119       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_link_id= ' ||
9120                                      p_alloc_rec.ae_tax_link_id);
9121       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_link_id_ed_adj= ' ||
9122                                      p_alloc_rec.ae_tax_link_id_ed_adj);
9123       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_link_id_uned= ' ||
9124                                      p_alloc_rec.ae_tax_link_id_uned);
9125       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_link_id_act= ' ||
9126                                      p_alloc_rec.ae_tax_link_id_act);
9127       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_amt= ' ||
9128                                      p_alloc_rec.ae_pro_amt);
9129       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_acctd_amt= ' ||
9130                                      p_alloc_rec.ae_pro_acctd_amt);
9131       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_taxable_amt= ' ||
9132                                      p_alloc_rec.ae_pro_taxable_amt);
9133       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_taxable_acctd_amt= ' ||
9134                                      p_alloc_rec.ae_pro_taxable_acctd_amt);
9135       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_def_tax_amt= ' ||
9136                                      p_alloc_rec.ae_pro_def_tax_amt);
9137       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_def_tax_acctd_amt= ' ||
9138                                      p_alloc_rec.ae_pro_def_tax_acctd_amt);
9139       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_summarize_flag= ' ||
9140                                      p_alloc_rec.ae_summarize_flag);
9141       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_counted_flag  = ' ||
9142                                      p_alloc_rec.ae_counted_flag);
9143       arp_standard.debug( 'ARP_ALLOCATION_PKG.Dump_Alloc_Rev_Tax()-');
9144    END IF;
9145 EXCEPTION
9146   WHEN OTHERS THEN
9147      IF PG_DEBUG in ('Y', 'C') THEN
9148         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Dump_Alloc_Rev_Tax');
9149      END IF;
9150      RAISE;
9151 
9152 END Dump_Alloc_Rev_Tax;
9153 
9154 /* ==========================================================================
9155  | PROCEDURE Dump_Init_Amts
9156  |
9157  | DESCRIPTION
9158  |    Dumps data derived by the Init_Amts routine for Revenue and Tax
9159  |
9160  | SCOPE - PRIVATE
9161  |
9162  | PARAMETERS
9163  |    p_type_acct            IN      Type of accounting
9164  |    p_app_rec              IN      Receivable application record
9165  |    p_adj_rec              IN      Adjustment Record
9166  *==========================================================================*/
9167 PROCEDURE Dump_Init_Amts(p_type_acct    IN VARCHAR2                          ,
9168                          p_app_rec      IN ar_receivable_applications%ROWTYPE,
9169                          p_adj_rec      IN ar_adjustments%ROWTYPE             ) IS
9170 BEGIN
9171    IF PG_DEBUG in ('Y', 'C') THEN
9172       arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Init_Amts()+');
9173       arp_standard.debug('Dump_Init_Amts: ' || 'p_type_acct = ' || p_type_acct);
9174    END IF;
9175 
9176    IF p_type_acct = 'ED_ADJ' AND g_ae_doc_rec.source_table = 'RA' THEN
9177 
9178       IF PG_DEBUG in ('Y', 'C') THEN
9179          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.earned_discount_taken       = ' || p_app_rec.earned_discount_taken);
9180          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.acctd_earned_discount_taken = ' || p_app_rec.acctd_earned_discount_taken);
9181          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.line_ediscounted            = ' || p_app_rec.line_ediscounted);
9182          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.tax_ediscounted             = ' || p_app_rec.tax_ediscounted);
9183          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.freight_ediscounted         = ' || p_app_rec.freight_ediscounted);
9184          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.charges_ediscounted         = ' || p_app_rec.charges_ediscounted);
9185       END IF;
9186 
9187    ELSIF p_type_acct = 'PAY' AND g_ae_doc_rec.source_table = 'RA' THEN
9188 
9189       IF PG_DEBUG in ('Y', 'C') THEN
9190          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.amount_applied              = ' || p_app_rec.amount_applied);
9191          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.acctd_amount_applied_to     = ' || p_app_rec.acctd_amount_applied_to);
9192          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.line_applied                = ' || p_app_rec.line_applied);
9193          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.tax_applied                 = ' || p_app_rec.tax_applied);
9194          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.freight_applied             = ' || p_app_rec.freight_applied);
9195          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.receivables_charges_applied = ' || p_app_rec.receivables_charges_applied);
9196       END IF;
9197 
9198    ELSIF p_type_acct = 'ED_ADJ' AND g_ae_doc_rec.source_table = 'ADJ' THEN
9199 
9200          IF PG_DEBUG in ('Y', 'C') THEN
9201             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.amount                   = ' || p_adj_rec.amount);
9202             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.acctd_amount             = ' || p_adj_rec.acctd_amount);
9203             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.line_adjusted            = ' || p_adj_rec.line_adjusted);
9204             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.tax_adjusted             = ' || p_adj_rec.tax_adjusted);
9205             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.freight_adjusted         = ' || p_adj_rec.freight_adjusted);
9206             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.receivables_charges_adjusted = '
9207                                                            || p_adj_rec.receivables_charges_adjusted);
9208          END IF;
9209 
9210    ELSIF p_type_acct = 'UNED' AND g_ae_doc_rec.source_table = 'RA' THEN
9211 
9212       IF PG_DEBUG in ('Y', 'C') THEN
9213          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.unearned_discount_taken       = ' || p_app_rec.unearned_discount_taken);
9214          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.acctd_unearned_discount_taken = ' || p_app_rec.acctd_unearned_discount_taken);
9215          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.line_uediscounted             = ' || p_app_rec.line_uediscounted);
9216          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.tax_uediscounted              = ' || p_app_rec.tax_uediscounted);
9217          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.freight_uediscounted          = ' || p_app_rec.freight_uediscounted);
9218          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.charges_uediscounted          = ' || p_app_rec.charges_uediscounted);
9219       END IF;
9220 
9221    END IF;
9222 
9223    IF PG_DEBUG in ('Y', 'C') THEN
9224       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.line_amt_alloc            = '||g_ae_rule_rec.line_amt_alloc);
9225       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.line_acctd_amt_alloc      = '||g_ae_rule_rec.line_acctd_amt_alloc);
9226       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.tax_amt_alloc             = '||g_ae_rule_rec.tax_amt_alloc);
9227       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.tax_acctd_amt_alloc       = '||g_ae_rule_rec.tax_acctd_amt_alloc);
9228       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.freight_amt_alloc         = '||g_ae_rule_rec.freight_amt_alloc);
9229       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.freight_acctd_amt_alloc   = '||g_ae_rule_rec.freight_acctd_amt_alloc);
9230       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.charges_amt_alloc         = '||g_ae_rule_rec.charges_amt_alloc);
9231       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.charges_acctd_amt_alloc   = '||g_ae_rule_rec.charges_acctd_amt_alloc);
9232       arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Init_Amts()-');
9233    END IF;
9234 
9235 EXCEPTION
9236   WHEN OTHERS THEN
9237      IF PG_DEBUG in ('Y', 'C') THEN
9238         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Dump_Init_Amts');
9239      END IF;
9240      RAISE;
9241 
9242 END Dump_Init_Amts;
9243 
9244 /* ==========================================================================
9245  | PROCEDURE Dump_Line_Amts
9246  |
9247  | DESCRIPTION
9248  |    Dumps data accounting line data
9249  |
9250  | SCOPE - PRIVATE
9251  |
9252  | PARAMETERS
9253  |    p_ae_line_rec          IN      Accounting lines record
9254  *==========================================================================*/
9255 PROCEDURE Dump_Line_Amts(p_ae_line_rec  IN ar_ae_alloc_rec_gt%ROWTYPE) IS
9256 
9257 BEGIN
9258 
9259        arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Line_Amts()+');
9260        arp_standard.debug('Dump_Line_Amts: ' || 'ae_id = ' || p_ae_line_rec.ae_id);
9261        arp_standard.debug('Dump_Line_Amts: ' || 'ae_line_type = ' || p_ae_line_rec.ae_line_type);
9262        arp_standard.debug('Dump_Line_Amts: ' || 'ae_line_type_secondary = ' || p_ae_line_rec.ae_line_type_secondary);
9263        arp_standard.debug('Dump_Line_Amts: ' || 'ae_source_id    = ' || p_ae_line_rec.ae_source_id);
9264        arp_standard.debug('Dump_Line_Amts: ' || 'ae_source_table = ' || p_ae_line_rec.ae_source_table);
9265        arp_standard.debug('Dump_Line_Amts: ' || 'ae_account      = ' || p_ae_line_rec.ae_account);
9266        arp_standard.debug('Dump_Line_Amts: ' || 'ae_entered_dr   = ' || p_ae_line_rec.ae_entered_dr);
9267        arp_standard.debug('Dump_Line_Amts: ' || 'ae_entered_cr   = ' || p_ae_line_rec.ae_entered_cr);
9268        arp_standard.debug('Dump_Line_Amts: ' || 'ae_accounted_dr = ' || p_ae_line_rec.ae_accounted_dr);
9269        arp_standard.debug('Dump_Line_Amts: ' || 'ae_accounted_cr = ' || p_ae_line_rec.ae_accounted_cr);
9270        arp_standard.debug('Dump_Line_Amts: ' || 'ae_source_id_secondary = ' || p_ae_line_rec.ae_source_id_secondary);
9271        arp_standard.debug('Dump_Line_Amts: ' || 'ae_source_table_secondary = ' || p_ae_line_rec.ae_source_table_secondary);
9272        arp_standard.debug('Dump_Line_Amts: ' || 'ae_currency_code = ' || p_ae_line_rec.ae_currency_code);
9273        arp_standard.debug('Dump_Line_Amts: ' || 'ae_currency_conversion_rate = ' || p_ae_line_rec.ae_currency_conversion_rate);
9274        arp_standard.debug('Dump_Line_Amts: ' || 'ae_currency_conversion_type = ' || p_ae_line_rec.ae_currency_conversion_type);
9275        arp_standard.debug('Dump_Line_Amts: ' || 'ae_currency_conversion_date = ' || p_ae_line_rec.ae_currency_conversion_date);
9276        arp_standard.debug('Dump_Line_Amts: ' || 'ae_third_party_id           = ' || p_ae_line_rec.ae_third_party_id);
9277        arp_standard.debug('Dump_Line_Amts: ' || 'ae_third_party_sub_id       = ' || p_ae_line_rec.ae_third_party_sub_id);
9278        arp_standard.debug('Dump_Line_Amts: ' || 'ae_tax_group_code_id        = ' || p_ae_line_rec.ae_tax_group_code_id);
9279        arp_standard.debug('Dump_Line_Amts: ' || 'ae_tax_code_id              = ' || p_ae_line_rec.ae_tax_code_id);
9280        arp_standard.debug('Dump_Line_Amts: ' || 'ae_location_segment_id      = ' || p_ae_line_rec.ae_location_segment_id);
9281        arp_standard.debug('Dump_Line_Amts: ' || 'ae_taxable_entered_dr       = ' || p_ae_line_rec.ae_taxable_entered_dr);
9282        arp_standard.debug('Dump_Line_Amts: ' || 'ae_taxable_entered_cr       = ' || p_ae_line_rec.ae_taxable_entered_cr);
9283        arp_standard.debug('Dump_Line_Amts: ' || 'ae_taxable_accounted_dr     = ' || p_ae_line_rec.ae_taxable_accounted_dr);
9284        arp_standard.debug('Dump_Line_Amts: ' || 'ae_taxable_accounted_cr     = ' || p_ae_line_rec.ae_taxable_accounted_cr);
9285        arp_standard.debug('Dump_Line_Amts: ' || 'ae_tax_link_id              = ' || p_ae_line_rec.ae_tax_link_id);
9286        arp_standard.debug('Dump_Line_Amts: ' || 'ae_reversed_source_id       = ' || p_ae_line_rec.ae_reversed_source_id);
9287        arp_standard.debug('Dump_Line_Amts: ' || 'ae_summarize_flag           = ' || p_ae_line_rec.ae_summarize_flag);
9288        arp_standard.debug('Dump_Line_Amts: ' || 'ae_neg_ind               = ' || p_ae_line_rec.ae_neg_ind);
9289 
9290        arp_standard.debug('Dump_Line_Amts: ' || 'activity_bucket                   = ' || p_ae_line_rec.activity_bucket);
9291        arp_standard.debug('Dump_Line_Amts: ' || 'ref_account_class                = ' || p_ae_line_rec.ref_account_class);
9292        arp_standard.debug('Dump_Line_Amts: ' || 'ref_customer_trx_line_id = ' || p_ae_line_rec.ae_customer_trx_line_id);
9293        arp_standard.debug('Dump_Line_Amts: ' || 'ref_cust_trx_line_gl_dist_id= ' || p_ae_line_rec.ae_cust_trx_line_gl_dist_id);
9294        arp_standard.debug('Dump_Line_Amts: ' || 'ref_line_id              = ' || p_ae_line_rec.ae_ref_line_id);
9295        arp_standard.debug('Dump_Line_Amts: ' || 'ref_dist_ccid            = ' || p_ae_line_rec.ref_dist_ccid);
9296        arp_standard.debug('Dump_Line_Amts: ' || 'ref_mf_dist_flag         = ' || p_ae_line_rec.ref_mf_dist_flag);
9297 
9298       arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Line_Amts()-');
9299 
9300 EXCEPTION
9301   WHEN OTHERS THEN
9302      IF PG_DEBUG in ('Y', 'C') THEN
9303         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Dump_Line_Amts');
9304      END IF;
9305      RAISE;
9306 
9307 END Dump_Line_Amts;
9308 
9309 /* ==========================================================================
9310  | PROCEDURE Dump_Dist_Amts
9311  |
9312  | DESCRIPTION
9313  |    Dumps data accounting line data
9314  |
9315  | SCOPE - PRIVATE
9316  |
9317  | PARAMETERS
9318  |    p_ae_line_rec          IN      Accounting lines record
9319  *==========================================================================*/
9320 PROCEDURE Dump_Dist_Amts(p_ae_line_rec  IN ar_distributions%ROWTYPE) IS
9321 BEGIN
9322 
9323     IF PG_DEBUG in ('Y', 'C') THEN
9324        arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Dist_Amts()+');
9325        arp_standard.debug('Dump_Dist_Amts: ' || 'ae_id                    = ' || g_id);
9326        arp_standard.debug('Dump_Dist_Amts: ' || 'source_type              = ' || p_ae_line_rec.source_type);
9327        arp_standard.debug('Dump_Dist_Amts: ' || 'source_type_secondary    = ' || p_ae_line_rec.source_type_secondary);
9328        arp_standard.debug('Dump_Dist_Amts: ' || 'source_id                = ' || p_ae_line_rec.source_id);
9329        arp_standard.debug('Dump_Dist_Amts: ' || 'source_table             = ' || p_ae_line_rec.source_table);
9330        arp_standard.debug('Dump_Dist_Amts: ' || 'code_combination_id      = ' || p_ae_line_rec.code_combination_id);
9331        arp_standard.debug('Dump_Dist_Amts: ' || 'amount_dr                = ' || p_ae_line_rec.amount_dr);
9332        arp_standard.debug('Dump_Dist_Amts: ' || 'amount_cr                = ' || p_ae_line_rec.amount_cr);
9333        arp_standard.debug('Dump_Dist_Amts: ' || 'acctd_amount_dr          = ' || p_ae_line_rec.acctd_amount_dr);
9334        arp_standard.debug('Dump_Dist_Amts: ' || 'acctd_amount_cr          = ' || p_ae_line_rec.acctd_amount_cr);
9335        arp_standard.debug('Dump_Dist_Amts: ' || 'source_id_secondary      = ' || p_ae_line_rec.source_id_secondary);
9336        arp_standard.debug('Dump_Dist_Amts: ' || 'source_table_secondary   = ' || p_ae_line_rec.source_table_secondary);
9337        arp_standard.debug('Dump_Dist_Amts: ' || 'currency_code            = ' || p_ae_line_rec.currency_code);
9338        arp_standard.debug('Dump_Dist_Amts: ' || 'currency_conversion_rate = ' || p_ae_line_rec.currency_conversion_rate);
9339        arp_standard.debug('Dump_Dist_Amts: ' || 'currency_conversion_type = ' || p_ae_line_rec.currency_conversion_type);
9340        arp_standard.debug('Dump_Dist_Amts: ' || 'currency_conversion_date = ' || p_ae_line_rec.currency_conversion_date);
9341        arp_standard.debug('Dump_Dist_Amts: ' || 'third_party_id           = ' || p_ae_line_rec.third_party_id);
9342        arp_standard.debug('Dump_Dist_Amts: ' || 'third_party_sub_id       = ' || p_ae_line_rec.third_party_sub_id);
9343        arp_standard.debug('Dump_Dist_Amts: ' || 'tax_group_code_id        = ' || p_ae_line_rec.tax_group_code_id);
9344        arp_standard.debug('Dump_Dist_Amts: ' || 'tax_code_id              = ' || p_ae_line_rec.tax_code_id);
9345        arp_standard.debug('Dump_Dist_Amts: ' || 'location_segment_id      = ' || p_ae_line_rec.location_segment_id);
9346        arp_standard.debug('Dump_Dist_Amts: ' || 'taxable_entered_dr       = ' || p_ae_line_rec.taxable_entered_dr);
9347        arp_standard.debug('Dump_Dist_Amts: ' || 'taxable_entered_cr       = ' || p_ae_line_rec.taxable_entered_cr);
9348        arp_standard.debug('Dump_Dist_Amts: ' || 'taxable_accounted_dr     = ' || p_ae_line_rec.taxable_accounted_dr);
9349        arp_standard.debug('Dump_Dist_Amts: ' || 'taxable_accounted_cr     = ' || p_ae_line_rec.taxable_accounted_cr);
9350        arp_standard.debug('Dump_Dist_Amts: ' || 'tax_link_id              = ' || p_ae_line_rec.tax_link_id);
9351        arp_standard.debug('Dump_Dist_Amts: ' || 'reversed_source_id       = ' || p_ae_line_rec.reversed_source_id);
9352        arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Dist_Amts()-');
9353     END IF;
9354 
9355 EXCEPTION
9356   WHEN OTHERS THEN
9357      IF PG_DEBUG in ('Y', 'C') THEN
9358         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Dump_Dist_Amts');
9359      END IF;
9360      RAISE;
9361 
9362 END Dump_Dist_Amts;
9363 
9364 PROCEDURE adj_boundary_account
9365 (p_receivables_trx_id   IN     NUMBER,
9366  p_bucket               IN     VARCHAR2,
9367  p_ctlgd_id             IN     NUMBER,
9368  x_ccid                 IN OUT NOCOPY NUMBER)
9369 IS
9370 FUNCTION fct_adj_ccid
9371   (p_bucket                IN VARCHAR2,
9372    p_customer_trx_id       IN NUMBER,
9373    p_gl_account_source     IN VARCHAR2,
9374    p_code_combination_id   IN NUMBER,
9375    p_tax_code_source       IN VARCHAR2,
9376    p_receivables_trx_id    IN NUMBER)
9377 RETURN NUMBER
9378  IS
9379    CURSOR c1 IS
9380    SELECT arp_etax_util.get_tax_account(tax.tax_rate_id,
9381                                         null,
9382                                         'ADJ',
9383                                         'TAX_RATE'),
9384           arp_etax_util.get_tax_account(tax.tax_rate_id,
9385                                         null,
9386                                         'ADJ_NON_REC',
9387                                         'TAX_RATE'),
9388           rt.tax_recoverable_flag
9389      FROM ar_receivables_trx  rt,
9390           zx_sco_rates        tax
9391     WHERE rt.receivables_trx_id = p_receivables_trx_id
9392       AND rt.asset_tax_code        = tax.tax_rate_code
9393       AND sysdate between nvl(tax.effective_from, sysdate) AND
9394                           nvl(tax.effective_to, sysdate);
9395 
9396    CURSOR c1_le(p_legal_entity_id NUMBER) IS
9397    SELECT arp_etax_util.get_tax_account(tax.tax_rate_id,
9398                                         null,
9399                                         'ADJ',
9400                                         'TAX_RATE'),
9401           arp_etax_util.get_tax_account(tax.tax_rate_id,
9402                                         null,
9403                                         'ADJ_NON_REC',
9404                                         'TAX_RATE'),
9405           rt.tax_recoverable_flag
9406      FROM ar_receivables_trx    rt,
9407           ar_rec_trx_le_details rtd,
9408           zx_sco_rates          tax
9409     WHERE rt.receivables_trx_id = p_receivables_trx_id
9410       AND rtd.receivables_trx_id (+) = rt.receivables_trx_id
9411       AND rtd.legal_entity_id (+)    = p_legal_entity_id
9412       AND nvl(rtd.asset_tax_code, rt.asset_tax_code)
9413                                      = tax.tax_rate_code
9414       AND trunc(sysdate) between nvl(tax.effective_from, trunc(sysdate)) AND
9415                                  nvl(tax.effective_to, trunc(sysdate));
9416 
9417    CURSOR c2 IS
9418    SELECT arp_etax_util.get_tax_account(tl.customer_trx_line_id,
9419                                         trunc(sysdate),
9420                                         'ADJ',
9421                                         'TAX_LINE'),
9422           arp_etax_util.get_tax_account(tl.customer_trx_line_id,
9423                                         trunc(sysdate),
9424                                         'FINCHRG',
9425                                         'TAX_LINE')
9426      FROM ra_customer_trx_lines   tl
9427     WHERE tl.customer_trx_id   = p_customer_trx_id
9428       AND tl.line_type         = 'TAX'
9429       AND tl.tax_line_id IS NOT NULL;
9430 
9431    l_adj_ccid                   NUMBER;
9432    l_finchrg_ccid               NUMBER;
9433    l_adj_non_rec_tax_ccid       NUMBER;
9434    l_tax_recoverable_flag       VARCHAR2(10);
9435    l_le_id                      NUMBER;
9436  BEGIN
9437    arp_standard.debug('fct_adj_ccid +');
9438    arp_standard.debug('   p_bucket             :'||p_bucket);
9439    arp_standard.debug('   p_customer_trx_id    :'||p_customer_trx_id);
9440    arp_standard.debug('   p_gl_account_source  :'||p_gl_account_source);
9441    arp_standard.debug('   p_code_combination_id:'||p_code_combination_id);
9442    arp_standard.debug('   p_tax_code_source    :'||p_tax_code_source);
9443    arp_standard.debug('   p_receivables_trx_id :'||p_receivables_trx_id);
9444 
9445    /* Determine if LE is required and fetch from target
9446       transaction */
9447    IF arp_legal_entity_util.is_le_subscriber
9448    THEN
9449       /* get LE from target trx */
9450       SELECT legal_entity_id
9451       INTO   l_le_id
9452       FROM   ra_customer_trx
9453       WHERE  customer_trx_id = p_customer_trx_id;
9454    ELSE
9455       /* Not required/used, set to null */
9456       l_le_id := NULL;
9457    END IF;
9458 
9459    -- Tax boundary
9460    IF p_bucket = 'ADJ_TAX' THEN
9461      arp_standard.debug('  Tax boundary');
9462      IF p_tax_code_source = 'ACTIVITY' THEN
9463         arp_standard.debug('    tax_code_source ACTIVITY');
9464 
9465         IF l_le_id IS NOT NULL
9466         THEN
9467            OPEN c1_le(l_le_id);
9468            FETCH c1_le INTO l_adj_ccid,
9469                      l_adj_non_rec_tax_ccid,
9470                      l_tax_recoverable_flag;
9471            CLOSE c1_le;
9472         ELSE
9473            OPEN c1;
9474            FETCH c1 INTO l_adj_ccid,
9475                      l_adj_non_rec_tax_ccid,
9476                      l_tax_recoverable_flag;
9477            CLOSE c1;
9478         END IF;
9479         arp_standard.debug('      l_adj_ccid     :'||l_adj_ccid);
9480         arp_standard.debug('      l_adj_non_rec_tax_ccid     :'||l_adj_non_rec_tax_ccid);
9481         arp_standard.debug('      l_tax_recoverable_flag     :'||l_tax_recoverable_flag);
9482         IF l_tax_recoverable_flag = 'N' THEN
9483             arp_standard.debug('       Returning  l_adj_non_rec_tax_ccid :'||l_adj_non_rec_tax_ccid);
9484             RETURN l_adj_non_rec_tax_ccid;
9485         ELSE
9486             arp_standard.debug('       Returning  l_adj_ccid :'||l_adj_ccid);
9487             RETURN l_adj_ccid;
9488         END IF;
9489      ELSIF p_tax_code_source = 'INVOICE' THEN
9490         -- unexpected situation no tax line on invoice
9491         arp_standard.debug('   tax code source INVOICE unexpected');
9492         arp_standard.debug('       Returning  -8');
9493         RETURN -8;
9494      ELSIF p_tax_code_source = 'NONE' THEN
9495         --unexpected situation inpossible for tax adjustment
9496         arp_standard.debug('   tax code source NONE unexpected');
9497         arp_standard.debug('       Returning  -8');
9498         RETURN -8;
9499      END IF;
9500    -- Line boundary
9501    ELSIF p_bucket = 'ADJ_LINE' THEN
9502      arp_standard.debug('  Line boundary');
9503      IF p_gl_account_source = 'ACTIVITY_GL_ACCOUNT' THEN
9504         arp_standard.debug('    gl_acount_source ACTIVITY_GL_ACCOUNT');
9505         arp_standard.debug('      Returning p_code_combination_id:'||p_code_combination_id);
9506         RETURN p_code_combination_id;
9507      ELSIF p_gl_account_source = 'TAX_CODE_ON_INVOICE' THEN
9508         arp_standard.debug('    gl_acount_source TAX_CODE_ON_INVOICE');
9509         OPEN c2;
9510         FETCH c2 INTO l_adj_ccid,
9511                       l_finchrg_ccid;
9512         CLOSE c2;
9513         arp_standard.debug('      l_adj_ccid     :'||l_adj_ccid);
9514         arp_standard.debug('      l_finchrg_ccid :'||l_finchrg_ccid);
9515         arp_standard.debug('       Returning  l_adj_ccid :'||l_adj_ccid);
9516         RETURN l_adj_ccid;
9517      ELSIF p_gl_account_source = 'REVENUE_ON_INVOICE' THEN
9518         -- Unexpected situation - no invoice line
9519         arp_standard.debug('   gl account source REVENUE_ON_INVOICE unexpected');
9520         arp_standard.debug('       Returning  -6');
9521         RETURN -6;
9522      END IF;
9523    -- Chrg boundary
9524    ELSIF p_bucket = 'ADJ_CHRG' THEN
9525      arp_standard.debug('  Charges boundary');
9526      IF p_gl_account_source = 'ACTIVITY_GL_ACCOUNT' THEN
9527         arp_standard.debug('    gl_acount_source ACTIVITY_GL_ACCOUNT');
9528         arp_standard.debug('      Returning p_code_combination_id:'||p_code_combination_id);
9529         RETURN p_code_combination_id;
9530      ELSIF p_gl_account_source = 'TAX_CODE_ON_INVOICE' THEN
9531         OPEN c2;
9532         FETCH c2 INTO l_adj_ccid,
9533                       l_finchrg_ccid;
9534         CLOSE c2;
9535         arp_standard.debug('      l_adj_ccid     :'||l_adj_ccid);
9536         arp_standard.debug('      l_finchrg_ccid :'||l_finchrg_ccid);
9537         arp_standard.debug('       Returning  l_finchrg_ccid :'||l_finchrg_ccid);
9538         RETURN l_finchrg_ccid;
9539      ELSIF p_gl_account_source = 'REVENUE_ON_INVOICE' THEN
9540         --Chrg boundary <=> no original amount invoice line so REVENUE_ON_INVOICE is unexpected
9541         arp_standard.debug('   gl account source REVENUE_ON_INVOICE unexpected');
9542         arp_standard.debug('       Returning  -7');
9543         RETURN -7;
9544      END IF;
9545    -- Chrg boundary
9546    ELSIF p_bucket = 'ADJ_FRT' THEN
9547      arp_standard.debug('  Charges boundary');
9548      IF p_gl_account_source = 'ACTIVITY_GL_ACCOUNT' THEN
9549         arp_standard.debug('    gl_acount_source ACTIVITY_GL_ACCOUNT');
9550         arp_standard.debug('      Returning p_code_combination_id:'||p_code_combination_id);
9551         RETURN p_code_combination_id;
9552      ELSIF p_gl_account_source = 'TAX_CODE_ON_INVOICE' THEN
9553         OPEN c2;
9554         FETCH c2 INTO l_adj_ccid,
9555                       l_finchrg_ccid;
9556         CLOSE c2;
9557         arp_standard.debug('      l_adj_ccid     :'||l_adj_ccid);
9558         arp_standard.debug('      l_finchrg_ccid :'||l_finchrg_ccid);
9559         arp_standard.debug('       Returning  L_ADJ_CCID :'||l_adj_ccid);
9560         RETURN l_adj_ccid;
9561      ELSIF p_gl_account_source = 'REVENUE_ON_INVOICE' THEN
9562         --Frt boundary <=> no original amount invoice line so REVENUE_ON_INVOICE is unexpected
9563         arp_standard.debug('   gl account source REVENUE_ON_INVOICE unexpected');
9564         arp_standard.debug('       Returning  -9');
9565         RETURN -9;
9566      END IF;
9567    END IF;
9568 END;
9569 BEGIN
9570 --   IF     g_ae_summ_tax_tbl.bucket IN ('ADJ_LINE','ADJ_TAX','ADJ_FRT','ADJ_CHRG')
9571 --      AND g_ae_summ_rev_tbl.ae_cust_trx_line_gl_dist_id IN (-6,-7,-8,-9)
9572  arp_standard.debug('  p_receivables_trx_id  :'||  p_receivables_trx_id);
9573  arp_standard.debug('  p_bucket              :'||  p_bucket);
9574  arp_standard.debug('  p_ctlgd_id            :'||  p_ctlgd_id );
9575   -- bug#5016123
9576   -- No Accounting should be created for the adj the ccid should keep the receivable ccid
9577   -- of the initial doc - but the distributions generated by the shadow adjustment
9578   -- should serve as template for the Transaction history
9579   -- BR shadow adjustment
9580    IF  p_receivables_trx_id  = -15 THEN
9581       x_ccid := NULL;
9582    ELSIF  p_bucket IN ('ADJ_LINE','ADJ_TAX','ADJ_FRT','ADJ_CHRG')
9583 --      AND p_ctlgd_id IN (-6,-7,-8,-9)
9584    THEN
9585       x_ccid := fct_adj_ccid (p_bucket            => p_bucket,
9586                               p_customer_trx_id   => g_Cust_inv_rec.customer_trx_id,
9587                               p_gl_account_source => g_ae_rule_rec.gl_account_source1,
9588                               p_code_combination_id => g_ae_rule_rec.code_combination_id1,
9589                               p_tax_code_source    => g_ae_rule_rec.tax_code_source1,
9590                               p_receivables_trx_id  => p_receivables_trx_id);
9591    arp_standard.debug('  x_ccid            :'||  x_ccid );
9592    END IF;
9593 END;
9594 
9595 
9596 END ARP_ALLOCATION_PKG;