DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_ALLOCATION_PKG

Source


1 PACKAGE BODY ARP_ALLOCATION_PKG  AS
2 /* $Header: ARALLOCB.pls 120.76.12010000.6 2008/12/02 05:42:50 mpsingh 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 
59   PROCEDURE adj_boundary_account (p_receivables_trx_id   IN     NUMBER,
60                                   p_bucket               IN     VARCHAR2,
61                                   p_ctlgd_id             IN     NUMBER,
62                                   x_ccid                 IN OUT NOCOPY NUMBER);
63 
64   PROCEDURE Get_Tax_Curr(p_invoice_id          IN ra_customer_trx.customer_trx_id%TYPE,
65                          p_payment_schedule_id IN ar_payment_schedules.payment_schedule_id%TYPE);
66 
67   PROCEDURE Get_Invoice_Distributions(p_invoice_id     IN NUMBER,
68                                       p_app_rec        IN ar_receivable_applications%ROWTYPE,
69                                       p_adj_rec        IN ar_adjustments%ROWTYPE,
70                                       p_process_ed_adj IN VARCHAR2,
71                                       p_process_uned   IN VARCHAR2,
72                                       p_process_pay    IN VARCHAR2);
73 
74   PROCEDURE Override_Accounts(p_app_rec   IN  ar_receivable_applications%ROWTYPE,
75                               p_adj_rec   IN  ar_adjustments%ROWTYPE,
76                               p_override1 OUT NOCOPY  VARCHAR2,
77                               p_override2 OUT NOCOPY VARCHAR2);
78 
79   PROCEDURE Check_Entry(p_invoice_id   IN  ra_customer_trx.customer_trx_id%TYPE,
80                         p_app_rec      IN  ar_receivable_applications%ROWTYPE,
81                         p_adj_rec      IN  ar_adjustments%ROWTYPE,
82                         p_rev_rec_req  OUT NOCOPY BOOLEAN,
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 Assign_Elements(p_ae_line_rec           IN ar_ae_alloc_rec_gt%ROWTYPE);
150 
151   PROCEDURE Insert_Ae_Lines(p_ae_line_tbl IN ar_ae_alloc_rec_gt%ROWTYPE);
152 
153   PROCEDURE Cache_Ae_Lines(p_ae_line_tbl IN ar_ae_alloc_rec_gt%ROWTYPE);
154 
155   PROCEDURE Dump_Alloc_Rev_Tax(p_type      IN VARCHAR2,
156                                p_alloc_rec IN ar_ae_alloc_rec_gt%ROWTYPE);
157 
158   PROCEDURE Dump_Init_Amts(p_type_acct    IN VARCHAR2                            ,
159                            p_app_rec      IN ar_receivable_applications%ROWTYPE  ,
160                            p_adj_rec      IN ar_adjustments%ROWTYPE               );
161 
162   PROCEDURE Dump_Line_Amts(p_ae_line_rec  IN ar_ae_alloc_rec_gt%ROWTYPE);
163 
164   PROCEDURE Dump_Dist_Amts(p_ae_line_rec  ar_distributions%ROWTYPE);
165 
166   FUNCTION source_exec(p_process_ed_adj   IN VARCHAR2,
167                        p_process_uned     IN VARCHAR2,
168                        p_process_pay      IN VARCHAR2,
169                        p_source_table     IN VARCHAR2) RETURN VARCHAR2;
170 
171   FUNCTION source_exec(p_type_acct       IN VARCHAR2,
172                        p_source_table    IN VARCHAR2) RETURN VARCHAR2;
173 
174   FUNCTION the_tax_code_source(p_bucket   IN VARCHAR2) RETURN VARCHAR2;
175 
176   FUNCTION the_tax_recoverable_flag(p_bucket   IN VARCHAR2) RETURN VARCHAR2;
177 
178   FUNCTION the_gl_account_source(p_bucket   IN VARCHAR2) RETURN VARCHAR2;
179 
180   FUNCTION get_adj_act_ccid RETURN NUMBER;
181 
182 --BUG#5245153
183 FUNCTION get_adj_act_ccid RETURN NUMBER
184 IS
185 CURSOR c IS
186 SELECT CODE_COMBINATION_ID
187   FROM ar_adjustments
188  WHERE adjustment_id = g_ae_doc_rec.document_id;
189 BEGIN
190   IF g_adj_act_gl_acct_ccid   = -9999 THEN
191     IF g_ae_doc_rec.source_table = 'ADJ' THEN
192     OPEN c;
193     FETCH c INTO g_adj_act_gl_acct_ccid ;
194     IF c%NOTFOUND THEN
195      g_adj_act_gl_acct_ccid := -1;
196     END IF;
197     CLOSE c;
198     END IF;
199   END IF;
200   RETURN g_adj_act_gl_acct_ccid;
201 END;
202 
203 FUNCTION the_gl_account_source(p_bucket   IN VARCHAR2)
204 RETURN VARCHAR2
205 IS
206     l_res    VARCHAR2(30);
207 BEGIN
208     IF    p_bucket IN ('ADJ_LINE' ,'ADJ_TAX'  , 'ADJ_FRT'  , 'ADJ_CHRG') THEN
209       l_res := g_ae_rule_rec.gl_account_source1;
210     ELSIF p_bucket IN ('APP_LINE' ,'APP_TAX'  , 'APP_FRT'  , 'APP_CHRG') THEN
211       l_res := 'ACTIVITY_GL_ACCOUNT';
212     ELSIF p_bucket IN ('ED_LINE' ,'ED_TAX'  , 'ED_FRT'  , 'ED_CHRG') THEN
213       l_res := g_ae_rule_rec.gl_account_source1;
214     ELSIF p_bucket IN ('UNED_LINE' ,'UNED_TAX'  , 'UNED_FRT'  , 'UNED_CHRG') THEN
215       l_res := g_ae_rule_rec.gl_account_source2;
216     ELSE
217       l_res :=  g_ae_rule_rec.tax_code_source1;
218     END IF;
219     arp_standard.debug(' the_gl_account_source for bucket '||p_bucket||' is '||l_res);
220     RETURN l_res;
221 END;
222 
223 
224 FUNCTION the_tax_code_source
225 (p_bucket   IN VARCHAR2)
226 RETURN VARCHAR2
227 IS
228   l_res     VARCHAR2(30);
229 BEGIN
230   IF    p_bucket IN ('ADJ_LINE' ,'ADJ_TAX'  , 'ADJ_FRT'  , 'ADJ_CHRG') THEN
231      l_res := g_ae_rule_rec.tax_code_source1;
232   ELSIF p_bucket IN ('APP_LINE' ,'APP_TAX'  , 'APP_FRT'  , 'APP_CHRG') THEN
233      l_res :=  'INVOICE';
234   ELSIF p_bucket IN ('ED_LINE' ,'ED_TAX'  , 'ED_FRT'  , 'ED_CHRG') THEN
235      l_res :=  g_ae_rule_rec.tax_code_source1;
236   ELSIF p_bucket IN ('UNED_LINE' ,'UNED_TAX'  , 'UNED_FRT'  , 'UNED_CHRG') THEN
237      l_res := g_ae_rule_rec.tax_code_source2;
238   ELSE
239      l_res :=  g_ae_rule_rec.tax_code_source1;
240   END IF;
241   arp_standard.debug(' the_tax_code_source for bucket '||p_bucket||' is '|| l_res);
242   RETURN l_res;
243 END;
244 
245 FUNCTION the_tax_recoverable_flag
246 (p_bucket   IN VARCHAR2)
247 RETURN VARCHAR2
248 IS
249   l_res    VARCHAR2(30);
250 BEGIN
251  IF p_bucket IN ('ADJ_LINE','ADJ_TAX'  ,'ADJ_FRT'  , 'ADJ_CHRG' ) THEN
252     l_res :=  g_ae_rule_rec.tax_recoverable_flag1;
253  ELSIF p_bucket IN ('APP_LINE','APP_TAX'  ,'APP_FRT'  , 'APP_CHRG' ) THEN
254      l_res := 'Y';
255  ELSIF p_bucket IN ('ED_LINE','ED_TAX'  ,'ED_FRT'  , 'ED_CHRG' ) THEN
256      l_res := g_ae_rule_rec.tax_recoverable_flag1;
257  ELSIF p_bucket IN ('UNED_LINE','UNED_TAX' ,'UNED_FRT' ,'UNED_CHRG') THEN
258      l_res := g_ae_rule_rec.tax_recoverable_flag2;
259  ELSE
260      l_res := g_ae_rule_rec.tax_recoverable_flag1;
261  END IF;
262  arp_standard.debug(' the_tax_recoverable_flag for bucket '||p_bucket||' is '|| l_res);
263  RETURN l_res;
264 END;
265 
266 FUNCTION source_exec
267 (p_process_ed_adj   IN VARCHAR2,
268  p_process_uned     IN VARCHAR2,
269  p_process_pay      IN VARCHAR2,
270  p_source_table     IN VARCHAR2)
271 RETURN VARCHAR2
272 IS
273   l_source_exec  VARCHAR2(30);
274 BEGIN
275   l_source_exec := NULL;
276   arp_standard.debug('source_exec +');
277   arp_standard.debug('  p_process_ed_adj :'||p_process_ed_adj);
278   arp_standard.debug('  p_process_uned   :'||p_process_uned  );
279   arp_standard.debug('  p_process_pay    :'||p_process_pay  );
280   arp_standard.debug('  p_source_table   :'||p_source_table  );
281   IF    p_process_ed_adj = 'Y' AND p_source_table = 'ADJ' THEN
282     l_source_exec := 'ADJ';
283   ELSIF p_process_ed_adj = 'Y' AND p_source_table = 'RA' THEN
284     l_source_exec := 'ED';
285   ELSIF p_process_uned = 'Y' AND p_source_table = 'RA' THEN
286     l_source_exec := 'UNED';
287   ELSIF p_process_pay  = 'Y' AND p_source_table = 'RA' THEN
288     l_source_exec := 'PAY';
289   END IF;
290   arp_standard.debug('  l_source_exec :'||l_source_exec);
291   arp_standard.debug('source_exec -');
292   RETURN l_source_exec;
293 END;
294 
295 
296 FUNCTION source_exec
297 (p_type_acct       IN VARCHAR2,
298  p_source_table     IN VARCHAR2)
299 RETURN VARCHAR2
300 IS
301   l_source_exec  VARCHAR2(30);
302   l_process_ed_adj  VARCHAR2(1) := 'N';
303   l_process_uned    VARCHAR2(1) := 'N';
304   l_process_pay     VARCHAR2(1) := 'N';
305 BEGIN
306   l_source_exec := NULL;
307   arp_standard.debug('source_exec +');
308   arp_standard.debug('  p_type_acct      :'||p_type_acct);
309   arp_standard.debug('  p_source_table   :'||p_source_table  );
310   IF     p_type_acct = 'ED_ADJ' THEN
311      l_process_ed_adj := 'Y';
312   ELSIF  p_type_acct = 'UNED' THEN
313      l_process_uned   := 'Y';
314   ELSIF  p_type_acct = 'PAY' THEN
315      l_process_pay    := 'Y';
316   END IF;
317   l_source_exec := source_exec(p_process_ed_adj   => l_process_ed_Adj,
318                                p_process_uned     => l_process_uned,
319                                p_process_pay      => l_process_pay,
320                                p_source_table     => p_source_table);
321 
322   arp_standard.debug('  l_source_exec :'||l_source_exec);
323   arp_standard.debug('source_exec -');
324   RETURN l_source_exec;
325 END;
326 
327 /* =======================================================================
328  | PROCEDURE Allocate_Tax
329  |
330  | DESCRIPTION
331  |      This procedure is the cover routine which will tax account for
332  |      discounts, adjustments and finance charges. The rule details
333  |      and document, event details are passed to this procedure which will
334  |      help determine the manner in which discounts and adjustments are
335  |      allocated over specific accounts based on Activity Rule.
336  |
337  | PARAMETERS
338  |      p_ae_doc_rec            IN      Document record
339  |      p_ae_event_rec          IN      Event record
340  |      p_app_rec               IN      Application record for discounts
341  |      p_adj_rec               IN      Adjustment record for adjustments
342  |      p_ae_rule_rec           IN      Rule record
343  |      p_ae_line_tbl           OUT     Table with accounting for discounts
344  |                                      or adjustments
345  * ======================================================================*/
346 PROCEDURE Allocate_Tax (
347                   p_ae_doc_rec    IN      ae_doc_rec_type,
348                   p_ae_event_rec  IN      ae_event_rec_type,
349                   p_ae_rule_rec   IN      ae_rule_rec_type,
350                   p_app_rec       IN      ar_receivable_applications%ROWTYPE,
351                   p_cust_inv_rec  IN      ra_customer_trx%ROWTYPE,
352                   p_adj_rec       IN      ar_adjustments%ROWTYPE,
353                   p_ae_ctr        IN OUT NOCOPY  BINARY_INTEGER,
354                   p_ae_line_tbl   IN OUT NOCOPY  ae_line_tbl_type,
355                   p_br_cust_trx_line_id IN ra_customer_trx_lines.customer_trx_line_id%TYPE default NULL,
356                   p_simul_app      IN     VARCHAR2 default NULL,
357                   p_from_llca_call IN     VARCHAR2 DEFAULT 'N',
358                   p_gt_id          IN     NUMBER   DEFAULT NULL,
359                   -- this flag is introduced to indicate if the application need conversion
360                   p_inv_cm         IN     VARCHAR2 DEFAULT 'I'
361                     ) IS
362 
363 l_invoice_id          ra_customer_trx.customer_trx_id%TYPE          ;
364 l_payment_schedule_id ar_payment_schedules.payment_schedule_id%TYPE ;
365 l_ed_adj_acct   VARCHAR2(10)   := 0;
366 l_uned_acct     VARCHAR2(10)   := 0;
367 l_linked_tax    BOOLEAN        := FALSE;
368 l_required      BOOLEAN        := TRUE;
369 l_rev_rec_req   BOOLEAN        := TRUE;
370 l_sum_dist      NUMBER;
371 l_gl_account_source_old     ar_receivables_trx.gl_account_source%TYPE;
372 l_tax_code_source_old       ar_receivables_trx.tax_code_source%TYPE;
373 l_tax_recoverable_flag_old  ar_receivables_trx.tax_recoverable_flag%TYPE;
374 l_process_ed_adj  VARCHAR2(1) := 'N';
375 l_process_uned    VARCHAR2(1) := 'N';
376 l_process_pay     VARCHAR2(1) := 'N';
377 l_le_id           NUMBER;
378 l_msg_count       NUMBER;
379 l_msg_data        VARCHAR2(1024);
380 l_effective_date  DATE;
381 l_return_status   VARCHAR2(10);
382 --{
383 l_line_adj          VARCHAR2(50);
384 l_tax_adj           VARCHAR2(50);
385 l_frt_adj           VARCHAR2(50);
386 l_chrg_adj          VARCHAR2(50);
387 l_cm_app            VARCHAR2(1) := 'N';
388 l_gt_id             NUMBER;
389 l_type              VARCHAR2(30);
390 --}
391 What_kind_of_activity   EXCEPTION;
392 impossible_adjust       EXCEPTION;
393 BEGIN
394 --  IF PG_DEBUG in ('Y', 'C') THEN
395      arp_standard.debug(   'ARP_ALLOCATION_PKG.Allocate_Tax()+');
396 --  END IF;
397 
398 /*----------------------------------------------------------------------------+
399  | Assign globals and get system options info                                 |
400  +----------------------------------------------------------------------------*/
401   g_ae_doc_rec                   := p_ae_doc_rec        ;
402   g_ae_event_rec                 := p_ae_event_rec      ;
403   g_ae_rule_rec                  := p_ae_rule_rec       ;
404 
405   g_cust_inv_rec                 := p_cust_inv_rec      ;
406 
407   g_ae_sys_rec.set_of_books_id   := ARP_ACCT_MAIN.ae_sys_rec.set_of_books_id;
408   g_ae_sys_rec.gain_cc_id        := ARP_ACCT_MAIN.ae_sys_rec.gain_cc_id;
409   g_ae_sys_rec.loss_cc_id        := ARP_ACCT_MAIN.ae_sys_rec.loss_cc_id;
410   g_ae_sys_rec.round_cc_id       := ARP_ACCT_MAIN.ae_sys_rec.round_cc_id;
411   g_ae_sys_rec.coa_id            := ARP_ACCT_MAIN.ae_sys_rec.coa_id;
412   g_ae_sys_rec.base_currency     := ARP_ACCT_MAIN.ae_sys_rec.base_currency;
413   g_ae_sys_rec.base_precision    := ARP_ACCT_MAIN.ae_sys_rec.base_precision;
414   g_ae_sys_rec.base_min_acc_unit := ARP_ACCT_MAIN.ae_sys_rec.base_min_acc_unit;
415 
416   -- MRC TRIGGER replacement
417   -- Initialize a new global variable:
418   g_ae_sys_rec.sob_type          := NVL(ARP_ACCT_MAIN.ae_sys_rec.sob_type,'P');
419 
420   g_ae_code_combination_id_app := p_app_rec.code_combination_id;
421   g_ae_rev_ctr         := 0;
422   g_ae_tax_ctr         := 0;
423   g_ae_unearn_rev_ctr  := 0;
424   g_ae_ctr             := 0;
425   g_ae_summ_ctr        := 0;
426   g_ae_summarize_tbl   := g_ae_empty_line_tbl ;
427 
428   g_amount_due_remaining       := 0;
429   g_acctd_amount_due_remaining := 0;
430   g_amount_due_original        := 0;
431 
432   g_sum_unearn_rev_amt         := 0;
433   g_sum_unearn_rev_acctd_amt   := 0;
434 
435   g_ae_def_tax           := FALSE;
436   g_done_def_tax         := FALSE;
437   g_bound_tax            := FALSE;
438   g_bound_freight        := FALSE;
439   g_bound_activity       := FALSE;
440   g_added_tax            := FALSE;
441   g_ovrrd_code           := '';
442 
443 --  ER :  LIne Level adjustment API- Changed the p_gt_id check , Placed the NVL
444 
445   IF NVL(p_gt_id,0) = 0  THEN
446     SELECT ar_distribution_split_s.NEXTVAL INTO g_id FROM DUAL;
447   ELSE
448     g_id                   := p_gt_id;
449   END IF;
450   arp_standard.debug(' p_gt_id  :'||p_gt_id);
451   arp_standard.debug(' g_id     :'||g_id);
452   --}
453   g_ed_adj_activity_link := 0;
454   g_uned_activity_link   := 0;
455   g_link_ctr             := 0;
456   adj_code_combination_id := '';
457   g_override1            := '';
458   g_override2            := '';
459 
460 --set the BR cust trx line id i.e. line id of assignment on BR document
461   IF p_br_cust_trx_line_id IS NOT NULL THEN
462     g_br_cust_trx_line_id := p_br_cust_trx_line_id;
463 
464   ELSE
465 
466     g_br_cust_trx_line_id := NULL;
467   END IF;
468 
469 --set the Simulation flag
470   IF p_simul_app IS NOT NULL AND p_simul_app = 'Y' THEN
471     g_simul_app  := p_simul_app;
472 
473   ELSE
474     g_simul_app  := NULL;
475 
476   END IF;
477 
478   arp_standard.debug('g_ae_doc_rec+');
479   arp_standard.debug(' g_ae_doc_rec.document_type:'||g_ae_doc_rec.document_type);
480   arp_standard.debug(' g_ae_doc_rec.document_id  :'||g_ae_doc_rec.document_id);
481   arp_standard.debug(' g_ae_doc_rec.accounting_entity_level:'||g_ae_doc_rec.accounting_entity_level);
482   arp_standard.debug(' g_ae_doc_rec.source_table:  '||g_ae_doc_rec.source_table);
483   arp_standard.debug(' g_ae_doc_rec.source_id:     '||g_ae_doc_rec.source_id);
484   arp_standard.debug(' g_ae_doc_rec.source_id_old: '||g_ae_doc_rec.source_id_old);
485   arp_standard.debug(' g_ae_doc_rec.other_flag:    '||g_ae_doc_rec.other_flag);
486   arp_standard.debug(' g_ae_doc_rec.miscel1:       '||g_ae_doc_rec.miscel1);
487   arp_standard.debug(' g_ae_doc_rec.event:         '||g_ae_doc_rec.event);
488   arp_standard.debug(' g_ae_doc_rec.deferred_tax:  '||g_ae_doc_rec.deferred_tax);
489   arp_standard.debug(' g_ae_doc_rec.pay_sched_upd_yn:'||g_ae_doc_rec.pay_sched_upd_yn);
490   arp_standard.debug(' g_ae_doc_rec.pay_sched_upd_cm_yn:'||g_ae_doc_rec.pay_sched_upd_cm_yn);
491   arp_standard.debug(' g_ae_doc_rec.override_source_type:'||g_ae_doc_rec.override_source_type);
492   arp_standard.debug(' g_ae_doc_rec.gl_tax_acct:   '||g_ae_doc_rec.gl_tax_acct);
493   arp_standard.debug(' g_ae_doc_rec.inv_cm_app_mode:'||g_ae_doc_rec.inv_cm_app_mode);
494   arp_standard.debug('g_ae_doc_rec-');
495 
496   arp_standard.debug('g_ae_rule_rec+');
497   arp_standard.debug(' g_ae_rule_rec.gl_account_source1 :'||g_ae_rule_rec.gl_account_source1);
498   arp_standard.debug(' g_ae_rule_rec.tax_code_source1   :'||g_ae_rule_rec.tax_code_source1);
499   arp_standard.debug(' g_ae_rule_rec.tax_recoverable_flag1  :'||g_ae_rule_rec.tax_recoverable_flag1);
500   arp_standard.debug(' g_ae_rule_rec.code_combination_id1   :'||g_ae_rule_rec.code_combination_id1);
501   arp_standard.debug(' g_ae_rule_rec.asset_tax_code1        :'||g_ae_rule_rec.asset_tax_code1);
502   arp_standard.debug(' g_ae_rule_rec.liability_tax_code1    :'||g_ae_rule_rec.liability_tax_code1);
503   arp_standard.debug(' g_ae_rule_rec.act_tax_non_rec_ccid1  :'||g_ae_rule_rec.act_tax_non_rec_ccid1);
504   arp_standard.debug(' g_ae_rule_rec.act_vat_tax_id1        :'||g_ae_rule_rec.act_vat_tax_id1);
505   arp_standard.debug(' g_ae_rule_rec.gl_account_source2 :'||g_ae_rule_rec.gl_account_source2);
506   arp_standard.debug(' g_ae_rule_rec.tax_code_source2   :'||g_ae_rule_rec.tax_code_source2);
507   arp_standard.debug(' g_ae_rule_rec.tax_recoverable_flag2  :'||g_ae_rule_rec.tax_recoverable_flag2);
508   arp_standard.debug(' g_ae_rule_rec.code_combination_id2   :'||g_ae_rule_rec.code_combination_id2);
509   arp_standard.debug(' g_ae_rule_rec.asset_tax_code2        :'||g_ae_rule_rec.asset_tax_code2);
510   arp_standard.debug(' g_ae_rule_rec.liability_tax_code2    :'||g_ae_rule_rec.liability_tax_code2);
511   arp_standard.debug(' g_ae_rule_rec.act_tax_non_rec_ccid2  :'||g_ae_rule_rec.act_tax_non_rec_ccid2);
512   arp_standard.debug(' g_ae_rule_rec.act_vat_tax_id2        :'||g_ae_rule_rec.act_vat_tax_id2);
513   arp_standard.debug('g_ae_rule_rec-');
514 
515   arp_standard.debug('g_ae_event_rec+');
516   arp_standard.debug(' g_ae_event_rec.event_type     :'||g_ae_event_rec.event_type);
517   arp_standard.debug(' g_ae_event_rec.event_id       :'||g_ae_event_rec.event_id);
518   arp_standard.debug(' g_ae_event_rec.event_date     :'||g_ae_event_rec.event_date);
519   arp_standard.debug(' g_ae_event_rec.event_status   :'||g_ae_event_rec.event_status);
520   arp_standard.debug('g_ae_event_rec-');
521 
522 
523 
524 /*----------------------------------------------------------------------------+
525  | Get Invoice for which Tax accounting is required based on Rules            |
526  +----------------------------------------------------------------------------*/
527    IF g_ae_doc_rec.source_table = 'RA' THEN
528       l_invoice_id          := p_app_rec.applied_customer_trx_id;
529       l_payment_schedule_id := p_app_rec.applied_payment_schedule_id;
530 
531       --{CMAPP
532       IF p_app_rec.applied_customer_trx_id = p_app_rec.customer_trx_id THEN
533          l_cm_app := 'Y';
534       END IF;
535       --}
536    ELSE
537       l_invoice_id  := p_adj_rec.customer_trx_id;
538       l_payment_schedule_id := p_adj_rec.payment_schedule_id;
539    END IF;
540 
541 /* Initialize zx */
542    SELECT legal_entity_id
543    INTO   l_le_id
544    FROM   ra_customer_trx
545    WHERE  customer_trx_id = l_invoice_id;
546 
547    zx_api_pub.set_tax_security_context(
548            p_api_version      => 1.0,
549            p_init_msg_list    => 'T',
550            p_commit           => 'F',
551            p_validation_level => NULL,
552            x_return_status    => l_return_status,
553            x_msg_count        => l_msg_count,
554            x_msg_data         => l_msg_data,
555            p_internal_org_id  => arp_standard.sysparm.org_id,
556            p_legal_entity_id  => l_le_id,
557            p_transaction_date => nvl(p_app_rec.apply_date,
558                                      p_adj_rec.apply_date),
559            p_related_doc_date => NULL,
560            p_adjusted_doc_date=> NULL,
561            x_effective_date   => l_effective_date);
562 
563 /*----------------------------------------------------------------------------+
564  | Check whether processing is really required if not then exit               |
565  |                                                                            |
566  | No need to make changes to this procedure as all checking will be done     |
567  | based on primary data.  If revenue recognition has not been run on first   |
568  | iteration, it will be complete by end of first iteration and before        |
569  | second iteration.                                                          |
570  +----------------------------------------------------------------------------*/
571 /*
572    Check_Entry(p_invoice_id     => l_invoice_id    ,
573                p_app_rec        => p_app_rec       ,
574                p_adj_rec        => p_adj_rec       ,
575                p_rev_rec_req    => l_rev_rec_req   ,
576                p_required       => l_required    );
577 
578 l_required := TRUE;
579    IF NOT l_required THEN            --processing not required
580       GOTO end_process_lbl;
581    ELSIF l_rev_rec_req THEN          --revenue recognition required to be run
582    arp_standard.debug(' HYU calling ARP_AUTO_RULE.create_distributions');
583          l_sum_dist := ARP_AUTO_RULE.create_distributions('N','N',l_invoice_id);
584          IF PG_DEBUG in ('Y', 'C') THEN
585             arp_standard.debug(  'Completed running revenue recognition for Transaction');
586          END IF;
587    END IF;
588 --}
589 */
590 l_required := TRUE;
591 
592   IF NVL(g_ae_sys_rec.sob_type,'P') = 'P' THEN
593 
594    --  ER :  LIne Level adjustment API- Changed the p_gt_id check , Placed the NVL
595 
596     IF  (p_from_llca_call = 'N'  AND NVL(p_gt_id,0) = 0 ) THEN
597 
598       l_gt_id  := g_id;
599 
600       IF (    (p_app_rec.receivable_application_id IS NOT NULL)
601           AND (p_adj_rec.adjustment_id             IS NOT NULL))
602       THEN
603          arp_standard.debug('Problem is this a application or adjustment allocation ?');
604          arp_standard.debug(' Both receivable_application_id :' ||p_app_rec.receivable_application_id);
605          arp_standard.debug(' and adjustment_id_id           :' ||p_adj_rec.adjustment_id);
606          arp_standard.debug(' are not null');
607          RAISE What_kind_of_activity;
608       ELSIF (p_app_rec.receivable_application_id IS NOT NULL) OR
609             (p_app_rec.receivable_application_id IS NULL       AND
610 			 p_app_rec.applied_customer_trx_id   IS NOT NULL   AND
611 			 p_app_rec.applied_payment_schedule_id IS NOT NULL AND
612 			 (NVL(p_app_rec.amount_applied,0)             <> 0 OR
613               NVL(p_app_rec.acctd_amount_applied_to,0)    <> 0 OR
614               NVL(p_app_rec.line_applied,0)               <> 0 OR
615               NVL(p_app_rec.tax_applied,0)                <> 0 OR
616               NVL(p_app_rec.freight_applied,0)            <> 0 OR
617               NVL(p_app_rec.receivables_charges_applied,0)<> 0 ))
618       THEN
619          IF p_inv_cm = 'C' THEN
620            g_cust_inv_rec.customer_trx_id := p_app_rec.customer_trx_id;
621          ELSE
622            g_cust_inv_rec.customer_trx_id := p_app_rec.applied_customer_trx_id;
623          END IF;
624 
625          ARP_DET_DIST_PKG.exec_revrec_if_required
626           ( p_customer_trx_id  => g_cust_inv_rec.customer_trx_id,
627             p_app_rec          => p_app_rec,
628             p_adj_rec          => p_adj_rec);
629 
630          ARP_DET_DIST_PKG.exec_adj_api_if_required
631            (p_adj_rec          => p_adj_rec,
632             p_app_rec          => p_app_rec,
633             p_ae_rule_rec      => g_ae_rule_rec,
634             p_cust_inv_rec     => g_Cust_inv_rec);
635 
636          ARP_DET_DIST_PKG.set_original_rem_amt
637            (p_customer_trx  => g_cust_inv_rec,
638 		    p_adj_id        => p_adj_rec.adjustment_id,
639 			p_app_id        => p_app_rec.receivable_application_id);
640 
641          ARP_DET_DIST_PKG.Trx_level_direct_cash_apply
642            (p_customer_trx     => g_cust_inv_rec,
643             p_app_rec          => p_app_rec,
644             p_ae_sys_rec       => g_ae_sys_rec,
645             p_gt_id            => l_gt_id,
646             p_inv_cm           => p_inv_cm);
647 
648       ELSIF (p_adj_rec.adjustment_id IS NOT NULL)
649       THEN
650          g_Cust_inv_rec.customer_trx_id := p_adj_rec.customer_trx_id;
651          g_receivables_trx_id           := p_adj_rec.receivables_trx_id;
652 
653          ARP_DET_DIST_PKG.exec_revrec_if_required
654           ( p_customer_trx_id  => p_adj_rec.customer_trx_id,
655             p_app_rec          => p_app_rec,
656             p_adj_rec          => p_adj_rec);
657 
658          ARP_DET_DIST_PKG.exec_adj_api_if_required
659            (p_adj_rec          => p_adj_rec,
660             p_app_rec          => p_app_rec,
661             p_ae_rule_rec      => g_ae_rule_rec,
662             p_cust_inv_rec     => g_Cust_inv_rec);
663 
664          ARP_DET_DIST_PKG.set_original_rem_amt
665            (p_customer_trx  => g_cust_inv_rec,
666 		    p_adj_id        => p_adj_rec.adjustment_id,
667 			p_app_id        => p_app_rec.receivable_application_id);
668 
669 
670          ARP_DET_DIST_PKG.possible_adjust
671            (p_adj_rec           => p_adj_rec,
672             p_ae_rule_rec       => g_ae_rule_rec,
673             p_customer_trx_id   => g_cust_inv_rec.customer_trx_id,
674             x_return_status     => l_return_status,
675             x_line_adj          => l_line_adj,
676             x_tax_adj           => l_tax_adj,
677             x_frt_adj           => l_frt_adj,
678             x_chrg_adj          => l_chrg_adj,
679             p_app_rec           => p_app_rec);
680 
681          arp_standard.debug('  x_line_adj :'||l_line_adj);
682          arp_standard.debug('  x_tax_adj  :'||l_tax_adj);
683          arp_standard.debug('  x_frt_adj  :'||l_frt_adj);
684          arp_standard.debug('  x_chrg_adj :'||l_chrg_adj);
685          arp_standard.debug('  l_return_status :'||l_return_status);
686 
687          IF   l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
688             RAISE impossible_adjust;
689          END IF;
690 
691          ARP_DET_DIST_PKG.Trx_level_direct_adjust
692            (p_customer_trx     => g_cust_inv_rec,
693             p_adj_rec          => p_adj_rec,
694             p_ae_sys_rec       => g_ae_sys_rec,
695             p_gt_id            => l_gt_id);
696 
697       ELSIF (    (p_adj_rec.adjustment_id IS NULL)
698              AND (p_app_rec.receivable_application_id IS NULL))
699       THEN
700          arp_standard.debug('Problem is this a application or adjustment allocation ?');
701          arp_standard.debug(' Both receivable_application_id  and adjustment_id_id  are null');
702          RAISE What_kind_of_activity;
703       END IF;
704       g_prim_det_dist_done := 'Y';
705 
706     ELSE
707 
708       l_gt_id := p_gt_id;
709       g_prim_det_dist_done := 'Y';
710 
711     END IF;
712 
713   END IF;
714 --}
715 
716 /*----------------------------------------------------------------------------+
717  | Get tax rounding rules and currency details                                |
718  +----------------------------------------------------------------------------*/
719    arp_standard.debug(' HYU calling Get_Tax_Curr');
720    --{CMAPP
721    IF l_cm_app <> 'Y' THEN
722      Get_Tax_Curr(p_invoice_id          => l_invoice_id,
723                   p_payment_schedule_id => l_payment_schedule_id);
724    END IF;
725 
726 /*----------------------------------------------------------------------------+
727  | Set the flags for processinf discounts, adjustments and payments           |
728  +----------------------------------------------------------------------------*/
729    IF (((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.earned_discount_taken,0) <> 0))
730        OR ((g_ae_doc_rec.source_table = 'ADJ') AND (nvl(p_adj_rec.amount,0) <> 0))) THEN
731        l_process_ed_adj := 'Y';
732    END IF;
733 
734    IF ((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.unearned_discount_taken,0) <> 0)) THEN
735       l_process_uned := 'Y';
736    END IF;
737 
738    IF (   --def_tax? (g_ae_def_tax) AND (NOT g_done_def_tax) AND
739            (g_ae_doc_rec.source_table = 'RA')
740        AND (nvl(p_app_rec.amount_applied,0) <> 0)) THEN
741        l_process_pay := 'Y';
742    END IF;
743 
744    arp_standard.debug(' l_process_ed_adj:'||l_process_ed_adj);
745    arp_standard.debug(' l_process_uned:'||l_process_uned);
746    arp_standard.debug(' l_process_pay:'||l_process_pay);
747 
748 /*----------------------------------------------------------------------------+
749  | Get Invoice Revenue and Tax distributions for Non Rule Invoice             |
750  +----------------------------------------------------------------------------*/
751    arp_standard.debug(' Calling Get_Invoice_Distributions');
752 
753 
754    Get_Invoice_Distributions(p_invoice_id     => l_invoice_id     ,
755                              p_app_rec        => p_app_rec        ,
756                              p_adj_rec        => p_adj_rec        ,
757                              p_process_ed_adj => l_process_ed_adj ,
758                              p_process_uned   => l_process_uned   ,
759                              p_process_pay    => l_process_pay      );
760    --GOTO end_process_lbl;
761 
762 /*----------------------------------------------------------------------------+
763  | Set tax link ids for Invoices Tax distributions (lines), call this routine |
764  | only if tax really needs to be linked otherwise the link id is null. Now it|
765  | is important to call the Get_Tax_Link_Id routine based on earned or unearn |
766  | discount because ther rules can be different and if one has the rule       |
767  | ACTIVITY then the link basis becomes different as it is off the activity   |
768  | tax code. So this condition. Process for earned discounts, adjustments     |
769  +----------------------------------------------------------------------------*/
770 --    IF l_process_ed_adj = 'Y' THEN
771 --     g_exec := NULL;
772 --     g_exec := source_exec(p_type_acct    => 'ED_ADJ',
773  --                          p_source_table =>  g_ae_doc_rec.source_table);
774 --}
775      /*----------------------------------------------------------------------------+
776       | Process for Earned discounts                                               |
777       +----------------------------------------------------------------------------*/
778         Process_Amounts(p_app_rec   => p_app_rec   ,
779                         p_adj_rec   => p_adj_rec    );
780 
781 --     END IF;
782 
783   /*----------------------------------------------------------------------------+
784    | Set tax link ids for Unearned discounts and link, override revenue using   |
785    | rules on unearned discount activity.                                       |
786    +----------------------------------------------------------------------------*/
787 --     IF l_process_uned = 'Y' THEN
788 --     g_exec := NULL;
789 --     g_exec := source_exec(p_type_acct    => 'UNED',
790 --                           p_source_table =>  g_ae_doc_rec.source_table);
791 --}
792      /*------------------------------------------------------------------------------+
793       | Initialise Revenue table to reuse cells for allocating line amounts          |
794       +------------------------------------------------------------------------------*/
795 -- we should initializing the amount in ar_ae_alloc_rec_gt        Init_Rev_Tax_Tab;
796 
797      /*----------------------------------------------------------------------------+
798       | Process for Unearned discounts                                             |
799       +----------------------------------------------------------------------------*/
800 --        Process_Amounts(p_app_rec   => p_app_rec,
801 --                        p_adj_rec   => p_adj_rec  );
802 
803 --     END IF; --end if unearned discounts
804 
805   /*----------------------------------------------------------------------------+
806    | Set tax link ids for Payment, in this case we default the rule to revenue  |
807    | on invoice, tax code on invoice recoverable and process for deferred tax   |
808    +----------------------------------------------------------------------------*/
809 --    IF l_process_pay = 'Y' THEN
810    /*----------------------------------------------------------------------------+
811     | Since processing for discounts is complete, force set the rules below so   |
812     | that we may process for payments and use a link basis between tax and      |
813     | revenue and calculate deferred taxable using payment non tax amount        |
814     +----------------------------------------------------------------------------*/
815 --      g_ae_rule_rec.gl_account_source1    := 'ACTIVITY_GL_ACCOUNT';
816 --      g_ae_rule_rec.tax_code_source1      := 'INVOICE';
817 --      g_ae_rule_rec.tax_recoverable_flag1 := 'Y';
818 --      g_ae_rule_rec.code_combination_id1  :=  g_ae_rule_rec.receivable_account;
819 --}
820      /*------------------------------------------------------------------------------+
821       | Initialise Revenue table to reuse cells for allocating line amounts          |
822       +------------------------------------------------------------------------------*/
823 -- we should initializing the amount in ar_ae_alloc_rec_gt        Init_Rev_Tax_Tab;
824 --     g_exec := NULL;
825 --     g_exec := source_exec(p_type_acct    => 'PAY',
826 --                           p_source_table =>  g_ae_doc_rec.source_table);
827 --}
828      /*------------------------------------------------------------------------+
829       | Process for  deferred tax for Payments                                 |
830       +------------------------------------------------------------------------*/
831 --        Process_Amounts(p_type_acct => 'PAY'       ,
832 --                        p_app_rec   => p_app_rec   ,
833 --                        p_adj_rec   => p_adj_rec    );
834 --     END IF; --end if payments
835 
836  /*----------------------------------------------------------------------------+
837   | Summarize built accounting lines for revenue and tax to net out accounting |
838   +----------------------------------------------------------------------------*/
839    Summarize_Accounting_Lines;
840 
841  /*---------------------------------------------------------------------------------+
842   | Assign summarized lines to the the in out table to pass back to calling rountine|
843   +---------------------------------------------------------------------------------*/
844 
845    IF nvl(g_simul_app,'N') = 'Y' THEN
846 
847       IF g_ae_summarize_tbl.EXISTS(g_ae_summ_ctr) THEN
848 
849          p_ae_line_tbl := g_ae_summarize_tbl;
850          p_ae_ctr      := g_ae_summ_ctr;
851 
852       END IF;
853 
854    END IF;
855 
856 
857  /*---------------------------------------------------------------------------------+
858   | Clean up the Global temporary tables.                                           |
859   +---------------------------------------------------------------------------------*/
860     --not required since in cases like mass applications g_id will increment
861 
862    g_br_cust_trx_line_id := NULL;
863    g_simul_app  := NULL;
864    g_adj_act_gl_acct_ccid := -9999;
865 
866 <<end_process_lbl>>
867   IF PG_DEBUG in ('Y', 'C') THEN
868      arp_standard.debug(   'ARP_ALLOCATION_PKG.Allocate_Tax()-');
869   END IF;
870 
871 EXCEPTION
872   WHEN NO_DATA_FOUND THEN
873      IF PG_DEBUG in ('Y', 'C') THEN
874         arp_standard.debug(  'ARP_ALLOCATION_PKG.Allocate_Tax - NO_DATA_FOUND' );
875      END IF;
876      RAISE;
877 
878   WHEN What_kind_of_activity THEN
879      IF PG_DEBUG in ('Y', 'C') THEN
880         arp_standard.debug(  'EXCEPTION: ARP_ALLOCATION_PKG.Allocate_Tax - What_kind_of_activity');
881      END IF;
882      RAISE;
883 
884   WHEN impossible_adjust THEN
885      IF PG_DEBUG in ('Y', 'C') THEN
886         arp_standard.debug(  'EXCEPTION: ARP_ALLOCATION_PKG.Allocate_Tax - impossible_adjust');
887      END IF;
888      RAISE;
889 
890   WHEN OTHERS THEN
891      IF PG_DEBUG in ('Y', 'C') THEN
892         arp_standard.debug(  'EXCEPTION: ARP_ALLOCATION_PKG.Allocate_Tax:'||SQLERRM);
893      END IF;
894      RAISE;
895 
896 END Allocate_Tax;
897 
898 /* =======================================================================
899  | PROCEDURE Check_Entry
900  |
901  | DESCRIPTION
902  |      This routine checks whether Tax accounting processing is really
903  |      required and whether revenue recognition needs to be run.
904  |
905  | SCOPE - PUBLIC
906  |
907  | PARAMETERS
908  |      p_invoice_id            IN      Invoice identifier
909  |      p_app_rec               IN      Applications record
910  |      p_adj_rec               IN      Adjustment record
911  |      p_rev_rec_req           OUT     Flag indicates whether rev rec
912  |                                      needs to be run
913  |      p_required              OUT     Flag indicates whether tax processing
914  |                                      is required
915  * ======================================================================*/
916 PROCEDURE Check_Entry(p_invoice_id  IN  ra_customer_trx.customer_trx_id%TYPE,
917                       p_app_rec     IN  ar_receivable_applications%ROWTYPE,
918                       p_adj_rec     IN  ar_adjustments%ROWTYPE,
919                       p_rev_rec_req OUT NOCOPY BOOLEAN,
920                       p_required    OUT NOCOPY BOOLEAN) IS
921 
922 l_dummy NUMBER;
923 BEGIN
924 
925   IF PG_DEBUG in ('Y', 'C') THEN
926      arp_standard.debug(   'ARP_ALLOCATION_PKG.Check_Entry()+');
927   END IF;
928 
929   --Is tax deferred
930    BEGIN
931 
932       IF PG_DEBUG in ('Y', 'C') THEN
933          arp_standard.debug(  'ARP_ALLOCATION_PKG.Check_Entry - Checking for deferred tax');
934       END IF;
935 
936       select gld.customer_trx_id
937       into l_dummy
938       from  ra_cust_trx_line_gl_dist gld
939       where gld.account_class = 'TAX'
940       and   gld.customer_trx_id = p_invoice_id
941       and   gld.collected_tax_ccid IS NOT NULL
942       group by gld.customer_trx_id;
943 
944       g_ae_def_tax := TRUE; --Atleast one deferred tax line exists
945 
946       IF PG_DEBUG in ('Y', 'C') THEN
947          arp_standard.debug(  'ARP_ALLOCATION_PKG.Check_Entry - DEFERRED TAX');
948       END IF;
949 
950    EXCEPTION
951    WHEN NO_DATA_FOUND THEN
952         IF PG_DEBUG in ('Y', 'C') THEN
953            arp_standard.debug(  'ARP_ALLOCATION_PKG.Check_Entry - NO DEFERRED TAX');
954         END IF;
955         g_ae_def_tax := FALSE; --Tax is not deferred
956    END;
957 
958   --Has revenue been recognized, no need to check whether invoice is complete - implied
959    BEGIN
960 
961       IF PG_DEBUG in ('Y', 'C') THEN
962          arp_standard.debug(  'ARP_ALLOCATION_PKG.Check_Entry - Check whether Rev Recognition is to be Run');
963       END IF;
964 
965       select ctl.customer_trx_id
966       into l_dummy
967       from ra_customer_trx_lines ctl
968       where ctl.customer_trx_id = p_invoice_id
969       and ctl.autorule_complete_flag||'' = 'N'
970       group by ctl.customer_trx_id;
971 
972       p_rev_rec_req := TRUE;
973 
974      IF PG_DEBUG in ('Y', 'C') THEN
975         arp_standard.debug(  'ARP_ALLOCATION_PKG.Check_Entry - Rev Recognition to be Run');
976      END IF;
977 
978    EXCEPTION
979    WHEN NO_DATA_FOUND THEN
980         IF PG_DEBUG in ('Y', 'C') THEN
981            arp_standard.debug(  'ARP_ALLOCATION_PKG.Check_Entry - Rev Recognition not required to be Run');
982         END IF;
983         p_rev_rec_req := FALSE;
984    END;
985 
986    arp_standard.debug(' g_ae_doc_rec.source_table:'||g_ae_doc_rec.source_table);
987    arp_standard.debug(' p_app_rec.earned_discount_taken:'||p_app_rec.earned_discount_taken);
988    arp_standard.debug(' p_app_rec.unearned_discount_taken:'||p_app_rec.unearned_discount_taken);
989    arp_standard.debug(' p_app_rec.amount_applied:'||p_app_rec.amount_applied);
990 
991 
992   --Set processing required flag
993    IF ((g_ae_doc_rec.source_table = 'RA')
994       AND (((nvl(p_app_rec.earned_discount_taken,0) <> 0)
995              OR (nvl(p_app_rec.unearned_discount_taken,0) <> 0))
996              OR ((g_ae_def_tax) AND (nvl(p_app_rec.amount_applied,0) <> 0))))
997       OR ((g_ae_doc_rec.source_table = 'ADJ') AND (nvl(p_adj_rec.amount,0) <> 0))
998    THEN
999       p_required := TRUE;
1000    ELSE
1001       p_required := FALSE;
1002    END IF;
1003 
1004  /*------------------------------------------------------------------------------+
1005   | Set flag to indicate that no deferred tax processing is required, for though |
1006   | deferred tax lines exist, there is no tax to be moved due to the payment.    |
1007   +------------------------------------------------------------------------------*/
1008    IF ((g_ae_def_tax) AND (nvl(p_app_rec.amount_applied,0) <> 0) AND (g_ae_doc_rec.source_table = 'RA')) THEN
1009       g_done_def_tax := FALSE;
1010    ELSE --no need to evoke logic for deferred tax processing
1011       g_done_def_tax := TRUE;
1012    END IF;
1013 
1014    IF PG_DEBUG in ('Y', 'C') THEN
1015       arp_standard.debug(   'ARP_ALLOCATION_PKG.Check_Entry()-');
1016    END IF;
1017 
1018 EXCEPTION
1019   WHEN OTHERS THEN
1020      IF PG_DEBUG in ('Y', 'C') THEN
1021         arp_standard.debug(  'EXCEPTION: ARP_ALLOCATION_PKG.Check_Entry');
1022      END IF;
1023      RAISE;
1024 
1025 END Check_Entry;
1026 
1027 /* =======================================================================
1028  | PROCEDURE Get_Tax_Curr
1029  |
1030  | DESCRIPTION
1031  |      This routine gets the Invoice currency details and Tax rounding
1032  |      rules for the same.
1033  |
1034  | SCOPE - PUBLIC
1035  |
1036  | PARAMETERS
1037  |      p_invoice_id            IN      Invoice identifier
1038  * ======================================================================*/
1039 PROCEDURE Get_Tax_Curr(p_invoice_id          IN ra_customer_trx.customer_trx_id%TYPE          ,
1040                        p_payment_schedule_id IN ar_payment_schedules.payment_schedule_id%TYPE   ) IS
1041 
1042 BEGIN
1043   IF PG_DEBUG in ('Y', 'C') THEN
1044      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Tax_Curr()+');
1045      arp_standard.debug( '  p_invoice_id                           = ' || p_invoice_id);
1046      arp_standard.debug( '  p_payment_schedule_id                  = ' || p_payment_schedule_id);
1047   END IF;
1048 
1049 /*----------------------------------------------------------------------------+
1050  | Get Invoice tax rounding rules and currency information                    |
1051  |                                                                            |
1052  | Modified select to get the acctd amount due remaining from the correct     |
1053  | sob (MRC TRIGGER REPLACEMENT)                                              |
1054  +----------------------------------------------------------------------------*/
1055  IF (NVL(g_ae_sys_rec.sob_type,'P') = 'P') THEN
1056       select    fc.precision                        ,
1057                 fc.minimum_accountable_unit         ,
1058                 pay.amount_due_remaining            ,
1059                 pay.acctd_amount_due_remaining      ,
1060                 pay.amount_due_original
1061       into      g_ae_curr_rec.precision                ,
1062                 g_ae_curr_rec.minimum_accountable_unit ,
1063                 g_amount_due_remaining                 ,
1064                 g_acctd_amount_due_remaining           ,
1065                 g_amount_due_original
1066       from ra_customer_trx      ct      ,
1067            ar_payment_schedules pay     ,
1068            fnd_currencies       fc
1069       where ct.customer_trx_id = p_invoice_id
1070       and   pay.customer_trx_id = ct.customer_trx_id
1071       and   pay.payment_schedule_id = p_payment_schedule_id
1072       and   ct.invoice_currency_code = fc.currency_code;
1073   END IF;
1074   IF PG_DEBUG in ('Y', 'C') THEN
1075      arp_standard.debug( ' g_ae_curr_rec.precision                = ' || g_ae_curr_rec.precision);
1076      arp_standard.debug( ' g_ae_curr_rec.minimum_accountable_unit = ' || g_ae_curr_rec.minimum_accountable_unit);
1077      arp_standard.debug( ' g_amount_due_remaining                 = ' || g_amount_due_remaining);
1078      arp_standard.debug( ' g_acctd_amount_due_remaining           = ' || g_acctd_amount_due_remaining);
1079      arp_standard.debug( ' g_amount_due_original                  = ' || g_amount_due_original);
1080      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Tax_Curr()-');
1081   END IF;
1082 
1083 EXCEPTION
1084   WHEN NO_DATA_FOUND THEN
1085      IF PG_DEBUG in ('Y', 'C') THEN
1086         arp_standard.debug('ARP_ALLOCATION_PKG.Get_Tax_Curr - NO_DATA_FOUND' );
1087      END IF;
1088      RAISE;
1089 
1090   WHEN OTHERS THEN
1091      IF PG_DEBUG in ('Y', 'C') THEN
1092         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Get_Tax_Curr');
1093      END IF;
1094      RAISE;
1095 
1096 END Get_Tax_Curr;
1097 
1098 /* =======================================================================
1099  | PROCEDURE Get_Invoice_Distributions
1100  |
1101  | DESCRIPTION
1102  |      Retrieves Revenue and Tax amounts for Invoice and Tax lines,
1103  |      including the non recoverable tax accounts off the tax code or
1104  |      location segment. This routine gets all the base data from the
1105  |      Invoice document so that Tax accounting may be done based on Rules
1106  |      for a activity for adjustments, finance charges or discounts.
1107  |
1108  | SCOPE - PRIVATE
1109  |
1110  | PARAMETERS
1111  |      p_invoice_id            IN      Invoice identifier
1112  |                                      or adjustments
1113  |
1114  | NOTES : THE ORDERING OF THE CURSOR inv_dist_non_rule IS VERY IMPORTANT
1115  * ======================================================================*/
1116 PROCEDURE Get_Invoice_Distributions(
1117                         p_invoice_id           IN      NUMBER,
1118                         p_app_rec              IN      ar_receivable_applications%ROWTYPE,
1119                         p_adj_rec              IN      ar_adjustments%ROWTYPE,
1120                         p_process_ed_adj       IN      VARCHAR2,
1121                         p_process_uned         IN      VARCHAR2,
1122                         p_process_pay          IN      VARCHAR2 ) IS
1123 
1124 CURSOR get_group_data_tax IS
1125 SELECT /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
1126        decode(ae_collected_tax_ccid,
1127               '',ae_account_class,
1128               'DEFTAX')                       ae_account_class      ,
1129        SUM(ae_amount)                         sum_ae_amount         ,
1130        SUM(ae_acctd_amount)                   sum_ae_acctd_amount   ,
1131        max(ae_code_combination_id)            ae_code_combination_id,
1132        max(decode(ae_override_ccid1,'',2,1))  ae_override_ccid1     ,
1133        max(decode(ae_override_ccid2,'',2,1))  ae_override_ccid2     ,
1134        count(ae_account_class)                ae_count
1135 FROM ar_ae_alloc_rec_gt
1136 WHERE ae_id = g_id
1137 GROUP BY decode(ae_collected_tax_ccid,
1138                 '',ae_account_class,
1139                 'DEFTAX');
1140 
1141 CURSOR get_group_data_rev IS
1142 SELECT /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
1143        ae_account_class                       ae_account_class      ,
1144        SUM(ae_amount)                         sum_ae_amount         ,
1145        SUM(ae_acctd_amount)                   sum_ae_acctd_amount   ,
1146        max(ae_code_combination_id)            ae_code_combination_id,
1147        ''                                     ae_override_ccid1     ,
1148        ''                                     ae_override_ccid2     ,
1149        count(ae_account_class)                ae_count
1150 FROM ar_ae_alloc_rec_gt
1151 WHERE ae_id = g_id
1152 AND   ae_account_class <> 'TAX'
1153 GROUP BY ae_account_class;
1154 
1155 CURSOR l_rev_unearn(p_type IN VARCHAR2, p_trx_line_id IN NUMBER) IS
1156 SELECT /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N1) */ *
1157 FROM  ar_ae_alloc_rec_gt
1158 WHERE ae_id = g_id
1159 AND   ae_account_class = p_type
1160 AND   ae_customer_trx_line_id = p_trx_line_id
1161 AND   ae_customer_trx_id = p_invoice_id;
1162 
1163 
1164 l_rev_adj_rec AR_Revenue_Adjustment_PVT.Rev_Adj_Rec_Type;
1165 l_inv_assign  ar_ae_alloc_rec_gt%ROWTYPE;
1166 
1167 l_rev_ctr    BINARY_INTEGER    ;
1168 l_ctr        BINARY_INTEGER    ;
1169 l_ctr1       BINARY_INTEGER    ;
1170 l_ctr2       BINARY_INTEGER    ;
1171 l_ctr3       BINARY_INTEGER    ;
1172 
1173 l_adj_id     NUMBER            ;
1174 l_dist_count NUMBER            ;
1175 l_ae_tax_id  NUMBER            ;
1176 l_adj_number ar_adjustments.adjustment_number%TYPE;
1177 
1178 l_ra_dist_tbl AR_Revenue_Adjustment_PVT.RA_Dist_Tbl_Type;
1179 
1180 l_return_status VARCHAR2(1)   ;
1181 l_msg_count     NUMBER        ;
1182 l_msg_data      VARCHAR2(2000);
1183 l_mesg          VARCHAR2(2000) := '';
1184 
1185 g_ae_alloc_rev_tbl ar_ae_alloc_rec_gt%ROWTYPE;
1186 g_ae_alloc_tax_tbl ar_ae_alloc_rec_gt%ROWTYPE;
1187 l_override1 VARCHAR2(1) := 'N';
1188 l_override2 VARCHAR2(1) := 'N';
1189 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
1190 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
1191 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
1192 
1193 CURSOR crevccid IS
1194 SELECT MAX(b.code_combination_id),
1195        ctl.ae_cust_trx_line_gl_dist_id
1196   FROM ra_cust_trx_line_gl_dist b,
1197        ar_ae_alloc_rec_gt       ctl
1198  WHERE ctl.ae_tax_link_id     = b.customer_trx_line_id
1199    AND ctl.ae_account_class   = 'TAX'
1200   GROUP BY ctl.ae_cust_trx_line_gl_dist_id;
1201 
1202 l_ccid_tab     DBMS_SQL.NUMBER_TABLE;
1203 l_ctlgd_tab    DBMS_SQL.NUMBER_TABLE;
1204 
1205 
1206 BEGIN
1207 
1208   IF PG_DEBUG in ('Y', 'C') THEN
1209      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Invoice_Distributions()+');
1210   END IF;
1211 /*------------------------------------------------------------------------------+
1212  | Initialise revenue, tax amounts and accounted amounts                        |
1213  +------------------------------------------------------------------------------*/
1214    g_ae_rule_rec.revenue_amt          := 0;
1215    g_ae_rule_rec.revenue_acctd_amt    := 0;
1216    g_ae_rule_rec.tax_amt              := 0;
1217    g_ae_rule_rec.tax_acctd_amt        := 0;
1218    g_ae_rule_rec.def_tax_amt          := 0;
1219    g_ae_rule_rec.def_tax_acctd_amt    := 0;
1220 
1221  --------------------------------------------------------------------------------
1222  --Override account flags
1223  --------------------------------------------------------------------------------
1224   Override_Accounts(p_app_rec   =>  p_app_rec  ,
1225                     p_adj_rec   =>  p_adj_rec  ,
1226                     p_override1 =>  l_override1,
1227                     p_override2 =>  l_override2 );
1228 
1229 /*----------------------------------------------------------------------------+
1230  | Insert Tax distributions                                                   |
1231  | MRC Trigger Replacement.   Modified to insert currency sensitive columns   |
1232  +----------------------------------------------------------------------------*/
1233    insert into ar_ae_alloc_rec_gt (
1234      ae_id                       ,
1235      ae_account_class            ,
1236      ae_customer_trx_id          ,
1237      ae_customer_trx_line_id     ,
1238      ae_cust_trx_line_gl_dist_id ,
1239      ae_link_to_cust_trx_line_id ,
1240      ae_tax_type                 ,
1241      ae_code_combination_id      ,
1242      ae_collected_tax_ccid       ,
1243      ae_line_amount              ,
1244      ae_amount                   ,
1245      ae_acctd_amount             ,
1246      ae_tax_group_code_id        ,
1247      ae_tax_id                   ,
1248      ae_taxable_amount           ,
1249      ae_taxable_acctd_amount     ,
1250      ae_adj_ccid                 ,
1251      ae_edisc_ccid               ,
1252      ae_unedisc_ccid             ,
1253      ae_finchrg_ccid             ,
1254      ae_adj_non_rec_tax_ccid     ,
1255      ae_edisc_non_rec_tax_ccid   ,
1256      ae_unedisc_non_rec_tax_ccid ,
1257      ae_finchrg_non_rec_tax_ccid ,
1258      ae_override_ccid1           ,
1259      ae_override_ccid2           ,
1260      ae_tax_link_id              , -- link_to_cust_trx_line_id
1261      ae_tax_link_id_ed_adj       , -- link_to_cust_trx_line_id
1262      ae_tax_link_id_uned         , -- link_to_cust_trx_line_id
1263      ae_tax_link_id_act          , -- left null populate later
1264      ae_pro_amt                  ,
1265      ae_pro_acctd_amt            ,
1266      ae_pro_frt_chrg_amt         ,
1267      ae_pro_frt_chrg_acctd_amt   ,
1268      ae_pro_taxable_amt          ,
1269      ae_pro_taxable_acctd_amt    ,
1270      ae_pro_split_taxable_amt       ,
1271      ae_pro_split_taxable_acctd_amt ,
1272      ae_pro_recov_taxable_amt       ,
1273      ae_pro_recov_taxable_acctd_amt ,
1274      ae_pro_def_tax_amt          ,
1275      ae_pro_def_tax_acctd_amt    ,
1276      ae_summarize_flag           ,
1277      ae_counted_flag             ,
1278      ae_autotax                  ,
1279      ae_sum_alloc_amt            ,
1280      ae_sum_alloc_acctd_amt      ,
1281      ae_tax_line_count           ,
1282      ref_account_class                   ,
1283      activity_bucket                      ,
1284      ae_ref_line_id,
1285      ae_from_pro_amt,
1286      ae_from_pro_acctd_amt,
1287      ref_dist_ccid,
1288      ref_mf_dist_flag
1289      )
1290        SELECT
1291           g_id                                      ae_id,
1292           gld.account_class                         ae_account_class,
1293           ctl.customer_trx_id                       ae_customer_trx_id,
1294           ctl.customer_trx_line_id                  ae_customer_trx_line_id,
1295           gld.cust_trx_line_gl_dist_id              ae_cust_trx_line_gl_dist_id ,
1296           nvl(ctl.link_to_cust_trx_line_id,-9999)   ae_link_to_cust_trx_line_id,
1297           decode(ctl.location_segment_id,
1298                      '','VAT',
1299                      'LOC')                         ae_tax_type,
1300           gld.code_combination_id                   ae_code_combination_id,
1301           gld.collected_tax_ccid                    ae_collected_tax_ccid,
1302           ctl.extended_amount                  ae_line_amount,
1303           nvl(gld.amount,0)                    ae_amount,
1304           NVL(gld.acctd_amount,0)                     ae_acctd_amount,
1305           decode(ctl.location_segment_id,
1306                     '',
1307                     decode(nvl(ctl.autotax,'Y'),
1308                           'N', '',
1309                            decode(nvl(line.location_segment_id,line.vat_tax_id),
1310                                 '','',
1311                                 nvl(ctl.location_segment_id, ctl.vat_tax_id),'',
1312                                 nvl(line.location_segment_id,line.vat_tax_id))),
1313                          '')                       ae_tax_group_code_id,
1314           nvl(ctl.location_segment_id,ctl.vat_tax_id) ae_tax_id,
1315           ctl.taxable_amount                          ae_taxable_amount,
1316           arpcurr.functional_amount(nvl(ctl.taxable_amount,0)       ,
1317                        g_ae_sys_rec.base_currency   ,
1318                        g_cust_inv_rec.exchange_rate,
1319                        g_ae_sys_rec.base_precision  ,
1320                        g_ae_sys_rec.base_min_acc_unit) ae_taxable_acctd_amount,
1321           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1322                                         gld.gl_date,
1323                                         'ADJ')         ae_adj_ccid,
1324           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1325                                         gld.gl_date,
1326                                         'EDISC')       ae_edisc_ccid,
1327           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1328                                         gld.gl_date,
1329                                         'UNEDISC')     ae_unedisc_ccid,
1330           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1331                                         gld.gl_date,
1332                                         'FINCHRG')     ae_finchrg_ccid,
1333           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1334                                         gld.gl_date,
1335                                         'ADJ_NON_REC') ae_adj_non_rec_tax_ccid,
1336           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1337                                         gld.gl_date,
1338                                         'EDISC_NON_REC') ae_edisc_non_rec_tax_ccid,
1339           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1340                                         gld.gl_date,
1341                                         'UNEDISC_NON_REC') ae_unedisc_non_rec_tax_ccid,
1342           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1343                                         gld.gl_date,
1344                                         'FINCHRG_NON_REC') ae_finchrg_non_rec_tax_ccid,
1345           decode(g_ae_rule_rec.tax_code_source1,
1346                      'INVOICE', decode(g_ae_rule_rec.tax_recoverable_flag1,
1347                                        'N',
1348                                        decode(g_ae_doc_rec.source_table,
1349                                               'RA',
1350           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1351                                         gld.gl_date,
1352                                         'EDISC_NON_REC'),
1353                                               'ADJ',
1354                                               decode(g_ae_doc_rec.document_type,
1355                                                      'ADJUSTMENT',
1356           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1357                                         gld.gl_date,
1358                                         'ADJ_NON_REC'),
1359                                                     'FINANCE_CHARGES',
1360           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1361                                         gld.gl_date,
1362                                         'FINCHRG_NON_REC'),
1363                                                              ''),
1364                                                         ''),
1365                                            ''),
1366                         'ACTIVITY', g_ae_rule_rec.act_tax_non_rec_ccid1,
1367                         '')                          ae_override_ccid1,
1368               decode(g_ae_rule_rec.tax_code_source2,
1369                          'INVOICE',decode(g_ae_rule_rec.tax_recoverable_flag2,
1370                                           'N', decode(g_ae_doc_rec.source_table,
1371                                                       'RA',
1372           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1373                                         gld.gl_date,
1374                                         'UNEDISC_NON_REC'),
1375                                                       ''),
1376                                           ''),
1377                          'ACTIVITY', g_ae_rule_rec.act_tax_non_rec_ccid2,
1378                          '')                           ae_override_ccid2,
1379               ctl.link_to_cust_trx_line_id             ae_tax_link_id,
1380               ctl.link_to_cust_trx_line_id             ae_tax_link_id_ed_adj,
1381               ctl.link_to_cust_trx_line_id             ae_tax_link_id_uned,
1382               ctl.link_to_cust_trx_line_id             ae_tax_link_id_act,
1383               det.amount                         ae_pro_amt,
1384               det.acctd_amount                   ae_pro_acctd_amt,
1385               0                                  ae_pro_frt_chrg_amt,
1386               0                                  ae_pro_frt_chrg_acctd_amt,
1387               det.taxable_amount                 ae_pro_taxable_amt,
1388               det.taxable_acctd_amount           ae_pro_taxable_acctd_amt,
1389               det.taxable_amount                 ae_pro_split_taxable_amt ,
1390               det.taxable_acctd_amount           ae_pro_split_taxable_acctd_amt,
1391               det.taxable_amount                 ae_pro_recov_taxable_amt,
1392               det.taxable_acctd_amount           ae_pro_recov_taxable_acctd_amt,
1393               0                                  ae_pro_def_tax_amt,
1394               0                                  ae_pro_def_tax_acctd_amt,
1395              'N'                                 ae_summarize_flag,
1396              'N'                                 ae_counted_flag,
1397               ctl.autotax                   ae_autotax,
1398              0                                   ae_sum_alloc_amt,
1399              0                                   ae_sum_alloc_acctd_amt,
1400              Get_Tax_Count(ctl.link_to_cust_trx_line_id) ae_tax_line_count,
1401              det.ref_account_class                       ref_account_class,
1402              det.activity_bucket                          activity_bucket,
1403              det.ref_line_id                     ae_ref_line_id,
1404              det.from_amount                     ae_from_pro_amt,
1405              det.from_acctd_amount               ae_from_pro_acctd_amt,
1406              det.ccid                            ref_dist_ccid,
1407              det.ref_mf_dist_flag
1408        FROM ra_customer_trx_lines     ctl,
1409             ra_cust_trx_line_gl_dist  gld,
1410             ra_customer_trx_lines     line,
1411             ar_line_app_detail_gt     det
1412        where ctl.customer_trx_id = p_invoice_id
1413        and   ctl.line_type = 'TAX'
1414        and   gld.customer_trx_line_id = ctl.customer_trx_line_id
1415        and   gld.account_set_flag = 'N'
1416        and   ctl.link_to_cust_trx_line_id = line.customer_trx_line_id (+)
1417        and   'LINE' = line.line_type (+)
1418        AND   det.ref_customer_trx_id   = ctl.customer_trx_id
1419        AND   det.ref_customer_trx_line_id = ctl.customer_trx_line_id
1420        AND   det.ref_cust_trx_line_gl_dist_id = gld.cust_trx_line_gl_dist_id
1421        AND   det.gt_id                    = g_id
1422        AND   det.ledger_id                = g_ae_sys_rec.set_of_books_id;
1423        /* and   not exists (select 'x'
1424                          from ra_customer_trx_lines ctl1
1425                          where ctl1.customer_trx_id = p_invoice_id
1426                          and   ctl1.autorule_complete_flag = 'N')   */
1427                 /* nvl(tax.location_segment_id,tax.vat_tax_id),
1428                 decode(tax.location_segment_id,
1429                                    '','VAT',
1430                                    'LOC') */
1431 
1432 
1433       arp_standard.debug('p_process_ed_adj:'||p_process_ed_adj);
1434       arp_standard.debug('p_process_uned:'||p_process_uned);
1435 
1436 
1437       IF ((NVL(p_process_ed_adj,'N')        = 'Y' AND
1438            g_ae_rule_rec.gl_account_source1 = 'REVENUE_ON_INVOICE' AND
1439            g_ae_rule_rec.tax_code_source1   = 'NONE') OR
1440          ( NVL(p_process_uned,'N')          = 'Y' AND
1441            g_ae_rule_rec.gl_account_source2 = 'REVENUE_ON_INVOICE' AND
1442            g_ae_rule_rec.tax_code_source2   = 'NONE'))
1443       THEN
1444          OPEN crevccid;
1445          FETCH crevccid BULK COLLECT INTO
1446            l_ccid_tab ,
1447            l_ctlgd_tab;
1448          CLOSE crevccid;
1449          IF l_ctlgd_tab.COUNT > 0 THEN
1450            FORALL k IN l_ccid_tab.FIRST .. l_ccid_tab.LAST
1451            UPDATE ar_ae_alloc_rec_gt
1452               SET ae_code_combination_id = l_ccid_tab(k)
1453             WHERE ae_cust_trx_line_gl_dist_id = l_ctlgd_tab(k);
1454          END IF;
1455      END IF;
1456 
1457 
1458 
1459    insert into ar_ae_alloc_rec_gt (
1460      ae_id                       ,
1461      ae_account_class            ,
1462      ae_customer_trx_id          ,
1463      ae_customer_trx_line_id     ,
1464      ae_cust_trx_line_gl_dist_id ,
1465      ae_link_to_cust_trx_line_id ,
1466      ae_tax_type                 ,
1467      ae_code_combination_id      ,
1468      ae_collected_tax_ccid       ,
1469      ae_line_amount              ,
1470      ae_amount                   ,
1471      ae_acctd_amount             ,
1472      ae_tax_group_code_id        ,
1473      ae_tax_id                   ,
1474      ae_taxable_amount           ,
1475      ae_taxable_acctd_amount     ,
1476      ae_adj_ccid                 ,
1477      ae_edisc_ccid               ,
1478      ae_unedisc_ccid             ,
1479      ae_finchrg_ccid             ,
1480      ae_adj_non_rec_tax_ccid     ,
1481      ae_edisc_non_rec_tax_ccid   ,
1482      ae_unedisc_non_rec_tax_ccid ,
1483      ae_finchrg_non_rec_tax_ccid ,
1484      ae_override_ccid1           ,
1485      ae_override_ccid2           ,
1486      ae_tax_link_id              ,
1487      ae_tax_link_id_ed_adj       ,
1488      ae_tax_link_id_uned         ,
1489      ae_tax_link_id_act          ,
1490      ae_pro_amt                  ,
1491      ae_pro_acctd_amt            ,
1492      ae_pro_frt_chrg_amt         ,
1493      ae_pro_frt_chrg_acctd_amt   ,
1494      ae_pro_taxable_amt          ,
1495      ae_pro_taxable_acctd_amt    ,
1496      ae_pro_split_taxable_amt       ,
1497      ae_pro_split_taxable_acctd_amt ,
1498      ae_pro_recov_taxable_amt       ,
1499      ae_pro_recov_taxable_acctd_amt ,
1500      ae_pro_def_tax_amt          ,
1501      ae_pro_def_tax_acctd_amt    ,
1502      ae_summarize_flag           ,
1503      ae_counted_flag             ,
1504      ae_autotax                  ,
1505      ae_sum_alloc_amt            ,
1506      ae_sum_alloc_acctd_amt      ,
1507      ae_tax_line_count           ,
1508      ref_account_class                   ,
1509      activity_bucket                      ,
1510      AE_REF_LINE_ID,
1511      ae_from_pro_amt,
1512      ae_from_pro_acctd_amt,
1513      ref_dist_ccid,
1514      ref_mf_dist_flag
1515      )
1516        SELECT
1517           g_id                                      ae_id,
1518           'TAX'                                     ae_account_class,
1519           det.ref_customer_trx_id                   ae_customer_trx_id,
1520           det.ref_customer_trx_line_id              ae_customer_trx_line_id,
1521           det.ref_cust_trx_line_gl_dist_id          ae_cust_trx_line_gl_dist_id ,
1522           ''                                        ae_link_to_cust_trx_line_id,
1523           ''                                        ae_tax_type,
1524           ''                                        ae_code_combination_id,
1525           ''                                        ae_collected_tax_ccid,
1526           ''                                        ae_line_amount,
1527           ''                                        ae_amount,
1528           ''                                        ae_acctd_amount,
1529           ''                                        ae_tax_group_code_id,
1530           ''                                        ae_tax_id,
1531           det.amount                                ae_taxable_amount,
1532           arpcurr.functional_amount(nvl(det.amount,0)       ,
1533                        g_ae_sys_rec.base_currency   ,
1534                        g_cust_inv_rec.exchange_rate,
1535                        g_ae_sys_rec.base_precision  ,
1536                        g_ae_sys_rec.base_min_acc_unit) ae_taxable_acctd_amount,
1537           ''                                         ae_adj_ccid,
1538           ''                                         ae_edisc_ccid,
1539           ''                                         ae_unedisc_ccid,
1540           ''                                         ae_finchrg_ccid,
1541           ''                                         ae_adj_non_rec_tax_ccid,
1542           ''                                         ae_edisc_non_rec_tax_ccid,
1543           ''                                         ae_unedisc_non_rec_tax_ccid,
1544           ''                                         ae_finchrg_non_rec_tax_ccid,
1545           ''                                         ae_override_ccid1,
1546           ''                                         ae_override_ccid2,
1547           ''                                         ae_tax_link_id,
1548           ''                                         ae_tax_link_id_ed_adj,
1549           ''                                         ae_tax_link_id_uned,
1550           ''                                  ae_tax_link_id_act,
1551           det.amount                          ae_pro_amt,
1552           det.acctd_amount                    ae_pro_acctd_amt,
1553           0                                   ae_pro_frt_chrg_amt,
1554           0                                   ae_pro_frt_chrg_acctd_amt,
1555               det.taxable_amount                 ae_pro_taxable_amt,
1556               det.taxable_acctd_amount           ae_pro_taxable_acctd_amt,
1557               det.taxable_amount                 ae_pro_split_taxable_amt ,
1558               det.taxable_acctd_amount           ae_pro_split_taxable_acctd_amt,
1559               det.taxable_amount                 ae_pro_recov_taxable_amt,
1560               det.taxable_acctd_amount           ae_pro_recov_taxable_acctd_amt,
1561           0                                   ae_pro_def_tax_amt,
1562           0                                   ae_pro_def_tax_acctd_amt,
1563           'N'                                 ae_summarize_flag,
1564           'N'                                 ae_counted_flag,
1565           ''                                  ae_autotax,
1566           0                                   ae_sum_alloc_amt,
1567           0                                   ae_sum_alloc_acctd_amt,
1568           ''                                  ae_tax_line_count,
1569           det.ref_account_class                       ref_account_class,
1570           det.activity_bucket                          activity_bucket,
1571           det.ref_line_id                     ae_line_id,
1572           det.from_amount                     ae_from_pro_amt,
1573           det.from_acctd_amount               ae_from_pro_acctd_amt,
1574           det.ccid                            ref_dist_ccid,
1575           det.ref_mf_dist_flag                ref_mf_dist_flag
1576        FROM ar_line_app_detail_gt  det
1577        WHERE det.ref_customer_trx_id = p_invoice_id
1578        AND   gt_id   = g_id
1579        AND   det.ledger_id   = g_ae_sys_rec.set_of_books_id
1580        AND   ref_customer_trx_line_id IN (-8);
1581 --              (-6, --Boundary line : -6
1582 --               -7, --Boundary charge:-7
1583 --               -8, --Boundary tax
1584 --               -9); --Boundary freight
1585 
1586 
1587 
1588 
1589       IF PG_DEBUG in ('Y', 'C') THEN
1590          arp_standard.debug('Get_Invoice_Distributions: ' || 'Tax and Deferred Tax amount accumulators, non recoverable account validation');
1591       END IF;
1592 
1593       for l_get_group_data IN get_group_data_tax LOOP
1594 
1595        /*---------------------------------------------------------------------+
1596         | Validate account setup for Non Recoverable tax accounts for earned  |
1597         | discounts and adjustments                                           |
1598         +---------------------------------------------------------------------*/
1599         /*
1600             IF ((((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.tax_ediscounted,0) <> 0))
1601               OR ((g_ae_doc_rec.source_table = 'ADJ') AND (nvl(p_adj_rec.tax_adjusted,0) <> 0)))
1602               AND (((g_ae_rule_rec.tax_code_source1 = 'INVOICE') AND (g_ae_rule_rec.tax_recoverable_flag1 = 'N'))
1603                    OR (g_ae_rule_rec.tax_code_source1 = 'ACTIVITY'))
1604               AND (l_get_group_data.ae_override_ccid1 = 2)) THEN
1605 
1606                 IF PG_DEBUG in ('Y', 'C') THEN
1607                    arp_standard.debug('Get_Invoice_Distributions: ' || 'Invalid CCid error');
1608                 END IF;
1609                 RAISE invalid_ccid_error;
1610 
1611             END IF;
1612         */
1613 
1614        /*----------------------------------------------------------------+
1615         | Validate account setup for Non Recoverable tax accounts for    |
1616         | unearned discounts                                             |
1617         +----------------------------------------------------------------*/
1618         /*
1619           IF (((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.tax_uediscounted,0) <> 0))
1620               AND (((g_ae_rule_rec.tax_code_source2 = 'INVOICE') AND (g_ae_rule_rec.tax_recoverable_flag2 = 'N'))
1621                    OR (g_ae_rule_rec.tax_code_source2 = 'ACTIVITY'))
1622               AND (l_get_group_data.ae_override_ccid2 = 2)) THEN
1623 
1624               IF PG_DEBUG in ('Y', 'C') THEN
1625                  arp_standard.debug('Get_Invoice_Distributions: ' || 'Invalid CCid error');
1626               END IF;
1627               RAISE invalid_ccid_error;
1628 
1629           END IF;
1630         */
1631 --}
1632        /*-------------------------------------------------------------------+
1633         | Total accumulators for tax and tax accounted amount               |
1634         +-------------------------------------------------------------------*/
1635           g_ae_rule_rec.tax_amt       := g_ae_rule_rec.tax_amt +
1636                                          l_get_group_data.sum_ae_amount;
1637           g_ae_rule_rec.tax_acctd_amt := g_ae_rule_rec.tax_acctd_amt +
1638                                          l_get_group_data.sum_ae_acctd_amount;
1639 
1640       /*----------------------------------------------------------------------+
1641        | Total accumulators for deferred tax and deferred tax accounted amount|
1642        +----------------------------------------------------------------------*/
1643           IF l_get_group_data.ae_account_class = 'DEFTAX' THEN
1644              g_ae_rule_rec.def_tax_amt       := l_get_group_data.sum_ae_amount;
1645              g_ae_rule_rec.def_tax_acctd_amt := l_get_group_data.sum_ae_acctd_amount;
1646           END IF;
1647 
1648        --Assign tax lines
1649           g_ae_tax_ctr := g_ae_tax_ctr + l_get_group_data.ae_count;
1650 
1651           IF PG_DEBUG in ('Y', 'C') THEN
1652              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_tax_ctr = ' || g_ae_tax_ctr);
1653              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.def_tax_amt = ' ||
1654                                 g_ae_rule_rec.def_tax_amt);
1655              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.def_tax_acctd_amt = ' ||
1656                                 g_ae_rule_rec.def_tax_acctd_amt);
1657              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_amt = ' || g_ae_rule_rec.tax_amt);
1658              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_acctd_amt = ' ||
1659                                 g_ae_rule_rec.tax_acctd_amt);
1660           END IF;
1661 
1662      END LOOP;
1663 
1664  -----------------------------------------------------------------------------
1665  --Set the tax link ids
1666  -----------------------------------------------------------------------------
1667  /* Obsolete
1668      Get_Tax_Link_Id(p_process_ed_adj => p_process_ed_adj,
1669                      p_process_uned   => p_process_uned,
1670                      p_process_pay    => p_process_pay);
1671   */
1672    IF PG_DEBUG in ('Y', 'C') THEN
1673       arp_standard.debug('Get_Invoice_Distributions: ' || 'Inserting Revenue lines');
1674    END IF;
1675 
1676 
1677 /*----------------------------------------------------------------------------+
1678  | Insert the Revenue, Unearned, Receivable, Suppense, Tax lines into the     |
1679  | temporary allocation table for computation purposes to build final         |
1680  | accounting                                                                 |
1681  +----------------------------------------------------------------------------*/
1682 --note we have removed the outer join to lines table for REC,
1683 --  since UNBILL and UNEARN
1684 --always have a customer_trx_line_id
1685 
1686   -- MRC Trigger Replacement:  get currency sensitive data
1687    insert into ar_ae_alloc_rec_gt (
1688      ae_id                       ,
1689      ae_account_class            ,
1690      ae_customer_trx_id          ,
1691      ae_customer_trx_line_id     ,
1692      ae_cust_trx_line_gl_dist_id ,
1693      ae_link_to_cust_trx_line_id ,
1694      ae_tax_type                 ,
1695      ae_code_combination_id      ,
1696      ae_collected_tax_ccid       ,
1697      ae_line_amount              ,
1698      ae_amount                   ,
1699      ae_acctd_amount             ,
1700      ae_tax_group_code_id        ,
1701      ae_tax_id                   ,
1702      ae_taxable_amount           ,
1703      ae_taxable_acctd_amount     ,
1704      ae_adj_ccid                 ,
1705      ae_edisc_ccid               ,
1706      ae_unedisc_ccid             ,
1707      ae_finchrg_ccid             ,
1708      ae_adj_non_rec_tax_ccid     ,
1709      ae_edisc_non_rec_tax_ccid   ,
1710      ae_unedisc_non_rec_tax_ccid ,
1711      ae_finchrg_non_rec_tax_ccid ,
1712      ae_override_ccid1           ,
1713      ae_override_ccid2           ,
1714      ae_tax_link_id              ,
1715      ae_tax_link_id_ed_adj       ,
1716      ae_tax_link_id_uned         ,
1717      ae_tax_link_id_act          ,
1718      ae_pro_amt                  ,
1719      ae_pro_acctd_amt            ,
1720      ae_pro_frt_chrg_amt         ,
1721      ae_pro_frt_chrg_acctd_amt   ,
1722      ae_pro_taxable_amt          ,
1723      ae_pro_taxable_acctd_amt    ,
1724      ae_pro_split_taxable_amt      ,
1725      ae_pro_split_taxable_acctd_amt ,
1726      ae_pro_recov_taxable_amt      ,
1727      ae_pro_recov_taxable_acctd_amt ,
1728      ae_pro_def_tax_amt          ,
1729      ae_pro_def_tax_acctd_amt    ,
1730      ae_summarize_flag           ,
1731      ae_counted_flag             ,
1732      ae_autotax                  ,
1733      ae_sum_alloc_amt            ,
1734      ae_sum_alloc_acctd_amt      ,
1735      ae_tax_line_count           ,
1736      ref_account_class                   ,
1737      activity_bucket,
1738      ae_ref_line_id,
1739      ae_from_pro_amt,
1740      ae_from_pro_acctd_amt,
1741      --{ref_dist_ccid
1742      ref_dist_ccid,
1743      ref_mf_dist_flag
1744      --}
1745      )
1746        SELECT g_id                    ae_id,
1747               decode(gld.account_class,
1748                      'REV'     ,'REVEARN',
1749                      'CHARGES' ,'REVEARN',
1750                      'SUSPENSE','REVEARN',
1751                      'UNBILL'  ,'REVUNEARN',
1752                      'UNEARN'  ,'REVUNEARN',
1753                      'FREIGHT' ,'FREIGHT')    ae_account_class,
1754               ctl.customer_trx_id             ae_customer_trx_id,
1755               ctl.customer_trx_line_id        ae_customer_trx_line_id,
1756               gld.cust_trx_line_gl_dist_id    ae_cust_trx_line_gl_dist_id ,
1757               nvl(ctl.link_to_cust_trx_line_id,
1758                   -9999)                      ae_link_to_cust_trx_line_id,
1759               decode(gld.account_class,
1760                      'FREIGHT','FREIGHT',
1761                      'REV')                                ae_tax_type,
1762               gld.code_combination_id         ae_code_combination_id,
1763               gld.collected_tax_ccid          ae_collected_tax_ccid,
1764               decode(gld.account_class,
1765                         'REV', nvl(ctl.revenue_amount,0),
1766                         'FREIGHT', nvl(ctl.revenue_amount,0),
1767                         'SUSPENSE',(ctl.extended_amount -
1768                                     nvl(ctl.revenue_amount,0)),
1769                         ctl.extended_amount)  ae_line_amount,
1770               nvl(gld.amount,0)           ae_amount,
1771               NVL(gld.acctd_amount,0)          ae_acctd_amount,
1772               ''                               ae_tax_group_code_id,
1773               ''                               ae_tax_id,
1774               ''                               ae_taxable_amount,
1775               ''                               ae_taxable_acctd_amount,
1776               ''                               ae_adj_ccid,
1777               ''                               ae_edisc_ccid,
1778               ''                               ae_unedisc_ccid,
1779               ''                               ae_finchrg_ccid,
1780               ''                               ae_adj_non_rec_tax_ccid,
1781               ''                               ae_edisc_non_rec_tax_ccid,
1782               ''                               ae_unedisc_non_rec_tax_ccid,
1783               ''                               ae_finchrg_non_rec_tax_ccid,
1784               decode(l_override1,
1785                          'Y',
1786                          decode(g_ae_rule_rec.gl_account_source1,
1787                                 'ACTIVITY_GL_ACCOUNT',
1788                                     g_ae_rule_rec.code_combination_id1,
1789                                 'TAX_CODE_ON_INVOICE',b5.override_ccid1,
1790                                   ''),
1791                          '')                   ae_override_ccid1,
1792 /*
1793               DECODE(l_override1,
1794                          'Y',
1795                          DECODE( DECODE(det.activity_bucket, 'APP_LINE', 'ACTIVITY_GL_ACCOUNT',
1796                                                 'APP_TAX' , 'ACTIVITY_GL_ACCOUNT',
1797                                                 'APP_FRT' , 'ACTIVITY_GL_ACCOUNT',
1798                                                 'APP_CHRG', 'ACTIVITY_GL_ACCOUNT',
1799                                                   g_ae_rule_rec.gl_account_source1),
1800                                 'ACTIVITY_GL_ACCOUNT',
1801                                         DECODE(det.activity_bucket,'APP_LINE',g_ae_rule_rec.receivable_account,
1802                                                       'APP_TAX' ,g_ae_rule_rec.receivable_account,
1803                                                       'APP_FRT' ,g_ae_rule_rec.receivable_account,
1804                                                       'APP_CHRG',g_ae_rule_rec.receivable_account,
1805                                                             g_ae_rule_rec.code_combination_id1),
1806                                 'TAX_CODE_ON_INVOICE',b5.override_ccid1,
1807                                   ''),
1808                          '')                   ae_override_ccid1,
1809 */
1810               decode(l_override2,
1811                          'Y',
1812                          decode(g_ae_rule_rec.gl_account_source2,
1813                                 'ACTIVITY_GL_ACCOUNT',
1814                                    g_ae_rule_rec.code_combination_id2,
1815                                 'TAX_CODE_ON_INVOICE',b5.override_ccid2,
1816                                  ''),
1817                          '')                   ae_override_ccid2        ,
1818 --              ''                                ae_tax_link_id           ,
1819 --              ''                                ae_tax_link_id_ed_adj    ,
1820 --              ''                                ae_tax_link_id_uned      ,
1821               ctl.customer_trx_line_id          ae_tax_link_id,
1822               ctl.customer_trx_line_id          ae_tax_link_id_ed_adj,
1823               ctl.customer_trx_line_id          ae_tax_link_id_uned,
1824               ctl.customer_trx_line_id          ae_tax_link_id_act       ,
1825               det.amount                        ae_pro_amt               ,
1826               det.acctd_amount                  ae_pro_acctd_amt         ,
1827               0                                 ae_pro_frt_chrg_amt      ,
1828               0                                 ae_pro_frt_chrg_acctd_amt,
1829               det.taxable_amount                 ae_pro_taxable_amt,
1830               det.taxable_acctd_amount           ae_pro_taxable_acctd_amt,
1831               det.taxable_amount                 ae_pro_split_taxable_amt ,
1832               det.taxable_acctd_amount           ae_pro_split_taxable_acctd_amt,
1833               det.taxable_amount                 ae_pro_recov_taxable_amt,
1834               det.taxable_acctd_amount           ae_pro_recov_taxable_acctd_amt,
1835               0                                 ae_pro_def_tax_amt       ,
1836               0                                 ae_pro_def_tax_acctd_amt ,
1837              'N'                                ae_summarize_flag        ,
1838              'N'                                ae_counted_flag          ,
1839              ''                                 ae_autotax               ,
1840              0                                  ae_sum_alloc_amt         ,
1841              0                                  ae_sum_alloc_acctd_amt   ,
1842              0                                  ae_tax_line_count        ,
1843              det.ref_account_class                 ref_account_class,
1844              det.activity_bucket                    activity_bucket,
1845              det.ref_line_id               ae_ref_line_id,
1846              det.from_amount                     ae_from_pro_amt,
1847              det.from_acctd_amount               ae_from_pro_acctd_amt,
1848              --{ref_dist_ccid
1849              det.ccid                            ref_dist_ccid,
1850              det.ref_mf_dist_flag                ref_mf_dist_flag
1851              --}
1852        from ra_customer_trx_lines     ctl,
1853             ra_cust_trx_line_gl_dist  gld,
1854             ar_line_app_detail_gt  det,
1855             (select b4.ae_link_to_cust_trx_line_id ae_link_to_cust_trx_line_id,
1856                     max(decode(g_ae_rule_rec.gl_account_source1,
1857                            'TAX_CODE_ON_INVOICE',
1858                             decode(g_ae_doc_rec.source_table,
1859                                    'RA', b4.ae_edisc_ccid,
1860                                    'ADJ',decode(g_ae_doc_rec.document_type,
1861                                    'ADJUSTMENT', b4.ae_adj_ccid,
1862                                    'FINANCE_CHARGES',b4.ae_finchrg_ccid,
1863                                                                       ''),
1864                                                          ''),
1865                            ''))               override_ccid1,
1866                     max(decode(g_ae_rule_rec.gl_account_source2,
1867                         'TAX_CODE_ON_INVOICE', decode(g_ae_doc_rec.source_table,
1868                                                       'RA', b4.ae_unedisc_ccid,
1869                                                          ''),
1870                            ''))               override_ccid2
1871              from ar_ae_alloc_rec_gt b4
1872              where b4.rowid IN
1873                (select /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
1874                      min(b3.rowid)
1875                 from ar_ae_alloc_rec_gt b3
1876                 where b3.ae_id = g_id
1877                 and   b3.ae_account_class = 'TAX'
1878                 and   (((decode(g_ae_doc_rec.source_table,
1879                               'RA', decode(b3.ae_edisc_ccid,
1880                                            '','N',
1881                                            'Y'),
1882                               'ADJ',decode(g_ae_doc_rec.document_type,
1883                                            'ADJUSTMENT', decode(b3.ae_adj_ccid,
1884                                                                 '','N',
1885                                                                 'Y'),
1886                                            'FINANCE_CHARGES',decode(b3.ae_finchrg_ccid,
1887                                                                     '','N',
1888                                                                     'Y')),
1889                               'N')  = 'Y')
1890                           AND (l_override1 = 'Y')
1891                           AND (g_ae_rule_rec.gl_account_source1 = 'TAX_CODE_ON_INVOICE'))
1892                        OR
1893                        ((decode(g_ae_doc_rec.source_table,
1894                                 'RA', decode(b3.ae_unedisc_ccid,
1895                                              '','N',
1896                                              'Y'),
1897                                 'N') = 'Y')
1898                          AND (l_override2 = 'Y')
1899                          AND (g_ae_rule_rec.gl_account_source2 = 'TAX_CODE_ON_INVOICE')))
1900                 group by b3.ae_link_to_cust_trx_line_id)
1901 	      group by b4.ae_link_to_cust_trx_line_id
1902 	     -- Bug 6719986 Added union sql to get the rows for TAX line of zero percentage tax.
1903 	     UNION
1904              select ctl.link_to_cust_trx_line_id ae_link_to_cust_trx_line_id,
1905                     max(decode(g_ae_rule_rec.gl_account_source1,
1906                            'TAX_CODE_ON_INVOICE',
1907                             decode(g_ae_doc_rec.source_table,
1908                                    'RA',  arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1909                                                                         gld.gl_date,
1910                                                                         'EDISC'),
1911                                    'ADJ',decode(g_ae_doc_rec.document_type,
1912                                                 'ADJUSTMENT',
1913                                           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1914                                                                         gld.gl_date,
1915                                                                         'ADJ'),
1916                                                 'FINANCE_CHARGES',
1917                                           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1918                                                                         gld.gl_date,
1919                                                                         'FINCHRG'),
1920                                                                       ''),
1921                                                          ''),
1922                            ''))               override_ccid1,
1923                     max(decode(g_ae_rule_rec.gl_account_source2,
1924                         'TAX_CODE_ON_INVOICE', decode(g_ae_doc_rec.source_table,
1925                                                       'RA',
1926                                           arp_etax_util.get_tax_account(ctl.customer_trx_line_id,
1927                                                                         gld.gl_date,
1928                                                                         'UNEDISC'),
1929                                                          ''),
1930                            ''))               override_ccid2
1931             from ra_customer_trx_lines ctl,
1932 	    ra_cust_trx_line_gl_dist  gld
1933             where gld.customer_trx_line_id = ctl.customer_trx_line_id
1934 	    and   gld.account_set_flag = 'N'
1935 	    and   ctl.customer_trx_id = p_invoice_id
1936 	    and   ctl.rowid in
1937 		     (select min(ctl1.rowid)
1938 		      from ra_customer_trx_lines ctl1,
1939 			  (select /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
1940 			   count(*) tax_count
1941 			   from ar_ae_alloc_rec_gt b3
1942 			   where b3.ae_id = g_id
1943 			   and   b3.ae_account_class = 'TAX') tx
1944 		      where ctl1.customer_trx_id = p_invoice_id
1945 		      and   ctl1.line_type = 'TAX'
1946 		      and tx.tax_count = 0
1947 		      group by ctl1.link_to_cust_trx_line_id)
1948               group by ctl.link_to_cust_trx_line_id
1949                 ) b5
1950        where ctl.customer_trx_id = p_invoice_id
1951        AND   ctl.line_type  IN ('LINE','FREIGHT','CB','CHARGES')
1952        --and   gld.customer_trx_id = ctl.customer_trx_id
1953        and   gld.customer_trx_line_id = ctl.customer_trx_line_id
1954        AND   gld.account_class IN ('REV','SUSPENSE','UNBILL','UNEARN','FREIGHT','CHARGES')
1955        and   gld.account_set_flag              = 'N'
1956        and   decode(ctl.line_type,
1957                     'FREIGHT', ctl.link_to_cust_trx_line_id,  --first available tax code netexpense account
1958                     ctl.customer_trx_line_id) = b5.ae_link_to_cust_trx_line_id (+)
1959        AND   det.ref_customer_trx_id          = ctl.customer_trx_id
1960        AND   det.ref_customer_trx_line_id     = ctl.customer_trx_line_id
1961        AND   det.ref_cust_trx_line_gl_dist_id = gld.cust_trx_line_gl_dist_id
1962        AND   det.ledger_id                = g_ae_sys_rec.set_of_books_id
1963        AND   det.gt_id                  =  g_id;
1964        /* and   not exists (select 'x'
1965                          from ra_customer_trx_lines ctl1
1966                          where ctl1.customer_trx_id = p_invoice_id
1967                          and   ctl1.autorule_complete_flag = 'N')   */
1968 --       group by decode(gld.account_class,
1969 --                       'REV'     ,'REVEARN',
1970 --                       'SUSPENSE','REVEARN',
1971 --                       'UNBILL'  ,'REVUNEARN',
1972 --                       'UNEARN'  ,'REVUNEARN',
1973 --                       'FREIGHT' ,'FREIGHT'),
1974 --                decode(gld.account_class,
1975 --                       'FREIGHT','FREIGHT',
1976 --                       'REV')                           ,
1977 --                gld.cust_trx_line_gl_dist_id            ,
1978 --                ctl.customer_trx_id                     ,
1979 --                nvl(ctl.link_to_cust_trx_line_id,-9999) ,
1980 --                ctl.customer_trx_line_id                ,
1981 --                gld.code_combination_id                 ,
1982 --                gld.collected_tax_ccid                    ;
1983 --}
1984         /* order by decode(gld.account_class,
1985                        'REV'     ,'REVEARN',
1986                        'SUSPENSE','REVEARN',
1987                        'UNBILL'  ,'REVUNEARN',
1988                        'UNEARN'  ,'REVUNEARN',
1989                        'TAX'     ,'TAX'),
1990                 ctl.customer_trx_id           ,
1991                 ctl.customer_trx_line_id   ; */
1992 
1993 
1994    insert into ar_ae_alloc_rec_gt (
1995      ae_id                       ,
1996      ae_account_class            ,
1997      ae_customer_trx_id          ,
1998      ae_customer_trx_line_id     ,
1999      ae_cust_trx_line_gl_dist_id ,
2000      ae_link_to_cust_trx_line_id ,
2001      ae_tax_type                 ,
2002      ae_code_combination_id      ,
2003      ae_collected_tax_ccid       ,
2004      ae_line_amount              ,
2005      ae_amount                   ,
2006      ae_acctd_amount             ,
2007      ae_tax_group_code_id        ,
2008      ae_tax_id                   ,
2009      ae_taxable_amount           ,
2010      ae_taxable_acctd_amount     ,
2011      ae_adj_ccid                 ,
2012      ae_edisc_ccid               ,
2013      ae_unedisc_ccid             ,
2014      ae_finchrg_ccid             ,
2015      ae_adj_non_rec_tax_ccid     ,
2016      ae_edisc_non_rec_tax_ccid   ,
2017      ae_unedisc_non_rec_tax_ccid ,
2018      ae_finchrg_non_rec_tax_ccid ,
2019      ae_override_ccid1           ,
2020      ae_override_ccid2           ,
2021      ae_tax_link_id              ,
2022      ae_tax_link_id_ed_adj       ,
2023      ae_tax_link_id_uned         ,
2024      ae_tax_link_id_act          ,
2025      ae_pro_amt                  ,
2026      ae_pro_acctd_amt            ,
2027      ae_pro_frt_chrg_amt         ,
2028      ae_pro_frt_chrg_acctd_amt   ,
2029      ae_pro_taxable_amt          ,
2030      ae_pro_taxable_acctd_amt    ,
2031      ae_pro_split_taxable_amt       ,
2032      ae_pro_split_taxable_acctd_amt ,
2033      ae_pro_recov_taxable_amt       ,
2034      ae_pro_recov_taxable_acctd_amt ,
2035      ae_pro_def_tax_amt          ,
2036      ae_pro_def_tax_acctd_amt    ,
2037      ae_summarize_flag           ,
2038      ae_counted_flag             ,
2039      ae_autotax                  ,
2040      ae_sum_alloc_amt            ,
2041      ae_sum_alloc_acctd_amt      ,
2042      ae_tax_line_count           ,
2043      ref_account_class                   ,
2044      activity_bucket                      ,
2045      ae_ref_line_id,
2046      ae_from_pro_amt,
2047      ae_from_pro_acctd_amt,
2048      --{ref_dist_ccid
2049      ref_dist_ccid,
2050      ref_mf_dist_flag
2051      --}
2052      )
2053        SELECT
2054           g_id                                      ae_id,
2055           DECODE(det.ref_account_class,'REV','REVEARN',
2056                                det.ref_account_class)       ae_account_class,
2057           det.ref_customer_trx_id                   ae_customer_trx_id,
2058           det.ref_customer_trx_line_id              ae_customer_trx_line_id,
2059           det.ref_cust_trx_line_gl_dist_id          ae_cust_trx_line_gl_dist_id ,
2060           ''                                        ae_link_to_cust_trx_line_id,
2061           ''                                        ae_tax_type,
2062           ''                                        ae_code_combination_id,
2063           ''                                        ae_collected_tax_ccid,
2064           ''                                        ae_line_amount,
2065           ''                                        ae_amount,
2066           ''                                        ae_acctd_amount,
2067           ''                                        ae_tax_group_code_id,
2068           ''                                        ae_tax_id,
2069           det.amount                                ae_taxable_amount,
2070           arpcurr.functional_amount(nvl(det.amount,0)       ,
2071                        g_ae_sys_rec.base_currency   ,
2072                        g_cust_inv_rec.exchange_rate,
2073                        g_ae_sys_rec.base_precision  ,
2074                        g_ae_sys_rec.base_min_acc_unit) ae_taxable_acctd_amount,
2075           ''                                         ae_adj_ccid,
2076           ''                                         ae_edisc_ccid,
2077           ''                                         ae_unedisc_ccid,
2078           ''                                         ae_finchrg_ccid,
2079           ''                                         ae_adj_non_rec_tax_ccid,
2080           ''                                         ae_edisc_non_rec_tax_ccid,
2081           ''                                         ae_unedisc_non_rec_tax_ccid,
2082           ''                                         ae_finchrg_non_rec_tax_ccid,
2083           ''                                         ae_override_ccid1,
2084           ''                                         ae_override_ccid2,
2085           ''                                         ae_tax_link_id,
2086           ''                                         ae_tax_link_id_ed_adj,
2087           ''                                         ae_tax_link_id_uned,
2088           ''                                  ae_tax_link_id_act,
2089           det.amount                          ae_pro_amt,
2090           det.acctd_amount                    ae_pro_acctd_amt,
2091           0                                   ae_pro_frt_chrg_amt,
2092           0                                   ae_pro_frt_chrg_acctd_amt,
2093               det.taxable_amount                 ae_pro_taxable_amt,
2094               det.taxable_acctd_amount           ae_pro_taxable_acctd_amt,
2095               det.taxable_amount                 ae_pro_split_taxable_amt ,
2096               det.taxable_acctd_amount           ae_pro_split_taxable_acctd_amt,
2097               det.taxable_amount                 ae_pro_recov_taxable_amt,
2098               det.taxable_acctd_amount           ae_pro_recov_taxable_acctd_amt,
2099           0                                   ae_pro_def_tax_amt,
2100           0                                   ae_pro_def_tax_acctd_amt,
2101           'N'                                 ae_summarize_flag,
2102           'N'                                 ae_counted_flag,
2103           ''                                  ae_autotax,
2104           0                                   ae_sum_alloc_amt,
2105           0                                   ae_sum_alloc_acctd_amt,
2106           ''                                  ae_tax_line_count,
2107           det.ref_account_class                       ref_account_class,
2108           det.activity_bucket                          activity_bucket,
2109           det.ref_line_id                     ae_ref_line_id,
2110              det.from_amount                     ae_from_pro_amt,
2111              det.from_acctd_amount               ae_from_pro_acctd_amt,
2112              det.ccid                            ref_dist_ccid,
2113              det.ref_mf_dist_flag                ref_mf_dist_flag
2114        FROM ar_line_app_detail_gt  det
2115        WHERE det.ref_customer_trx_id = p_invoice_id
2116        AND   det.ledger_id           = g_ae_sys_rec.set_of_books_id
2117        AND   gt_id   = g_id
2118        AND   ref_customer_trx_line_id IN (-6,-7,-9);
2119 --              (-6, --Boundary line : -6
2120 --               -7, --Boundary charge:-7
2121 --               -8, --Boundary tax
2122 --               -9); --Boundary freight
2123 
2124 /*------------------------------------------------------------------------------+
2125  | Sum functions to set amount sums and counts required for decision making     |
2126  +------------------------------------------------------------------------------*/
2127   FOR l_get_group_data IN get_group_data_rev  LOOP
2128 
2129       IF l_get_group_data.ae_account_class = 'REVEARN' THEN
2130 
2131          g_ae_rev_ctr := l_get_group_data.ae_count;
2132 
2133        --Revenue total amounts and accounted amounts accumulator
2134           g_ae_rule_rec.revenue_amt       := l_get_group_data.sum_ae_amount;
2135           g_ae_rule_rec.revenue_acctd_amt := l_get_group_data.sum_ae_acctd_amount;
2136 
2137           IF PG_DEBUG in ('Y', 'C') THEN
2138              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rev_ctr ' || g_ae_rev_ctr);
2139              arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.revenue_amt  = ' || g_ae_rule_rec.revenue_amt);
2140              arp_standard.debug('Get_Invoice_Distributions: ' ||
2141                                 'g_ae_rule_rec.revenue_acctd_amt = ' || g_ae_rule_rec.revenue_acctd_amt);
2142           END IF;
2143 
2144       ELSIF l_get_group_data.ae_account_class = 'REVUNEARN' THEN
2145 
2146              g_ae_unearn_rev_ctr        := l_get_group_data.ae_count;
2147 
2148              g_sum_unearn_rev_amt       := l_get_group_data.sum_ae_amount;
2149              g_sum_unearn_rev_acctd_amt := l_get_group_data.sum_ae_acctd_amount;
2150 
2151              IF PG_DEBUG in ('Y', 'C') THEN
2152                 arp_standard.debug('Get_Invoice_Distributions: ' ||
2153                                     'g_sum_unearn_rev_amt       = ' || g_sum_unearn_rev_amt);
2154                 arp_standard.debug('Get_Invoice_Distributions: ' ||
2155                                    'g_sum_unearn_rev_acctd_amt = ' || g_sum_unearn_rev_acctd_amt);
2156              END IF;
2157 
2158 
2159       ELSIF l_get_group_data.ae_account_class = 'REVXREC' THEN
2160 
2161            null;
2162           /* g_ae_rule_rec.receivable_amt        := l_get_group_data.sum_ae_amount;
2163           g_ae_rule_rec.receivable_acctd_amt  := l_get_group_data.sum_ae_acctd_amount;
2164 
2165           g_ae_rule_rec.receivable_account    := l_get_group_data.ae_code_combination_id;
2166 
2167           IF PG_DEBUG in ('Y', 'C') THEN
2168              arp_standard.debug('Get_Invoice_Distributions: ' ||
2169               'g_ae_rule_rec.receivable_amt = ' || g_ae_rule_rec.receivable_amt);
2170              arp_standard.debug('Get_Invoice_Distributions: ' ||
2171               'g_ae_rule_rec.receivable_acctd_amt = ' || g_ae_rule_rec.receivable_acctd_amt);
2172              arp_standard.debug('Get_Invoice_Distributions: ' ||
2173               'g_ae_rule_rec.receivable_account = ' || g_ae_rule_rec.receivable_account);
2174           END IF; */
2175 
2176       END IF;
2177 
2178   END LOOP;
2179 
2180 /*---------------------------------------------------------------------------+
2181  | Total accumulators for Receivable account class, and receivable ccid      |
2182  |                                                                           |
2183  | Modified for MRC TRIGGER REPLACEMENT:  get data from source depending     |
2184  | on the sob type                                                           |
2185  +---------------------------------------------------------------------------*/
2186 
2187   IF (NVL(g_ae_sys_rec.sob_type,'P') =  'P') THEN
2188       SELECT amount,
2189              acctd_amount,
2190              code_combination_id
2191       INTO g_ae_rule_rec.receivable_amt,
2192            g_ae_rule_rec.receivable_acctd_amt,
2193            g_ae_rule_rec.receivable_account
2194       FROM ra_cust_trx_line_gl_dist
2195       where customer_trx_id = p_invoice_id
2196         AND account_class = 'REC'
2197         and latest_rec_flag = 'Y';
2198   END IF;
2199 
2200   IF PG_DEBUG in ('Y', 'C') THEN
2201      arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.receivable_amt = ' ||
2202                       g_ae_rule_rec.receivable_amt);
2203      arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.receivable_acctd_amt = ' ||
2204                       g_ae_rule_rec.receivable_acctd_amt);
2205      arp_standard.debug('Get_Invoice_Distributions: ' ||
2206                         'g_ae_rule_rec.receivable_account = ' || g_ae_rule_rec.receivable_account);
2207   END IF;
2208 
2209 /*------------------------------------------------------------------------------+
2210  | Ascertain as to whether the revenue adjustment api will be used to derive the|
2211  | revenue distributions and the code combinations using autoaccounting, to     |
2212  | allocate the line amounts to gl account source = 'REVENUE ON INVOICE' or     |
2213  | tax code source is none for tax amount.                                      |
2214  +------------------------------------------------------------------------------*/
2215 /*IF ((g_sum_unearn_rev_amt <> 0) OR (g_sum_unearn_rev_acctd_amt <> 0)) THEN
2216        --
2217        --condition as to whether the RAM api will require to be called if gl account
2218        --source is revenue on invoice
2219        --
2220      IF (((g_ae_doc_rec.source_table = 'ADJ')
2221            AND (g_ae_rule_rec.gl_account_source1 = 'REVENUE_ON_INVOICE')
2222                 AND (((nvl(p_adj_rec.line_adjusted,0) + nvl(p_adj_rec.freight_adjusted,0) +
2223                        nvl(p_adj_rec.receivables_charges_adjusted,0)) <> 0)
2224                      OR ((g_ae_rule_rec.tax_code_source1 = 'NONE') AND (nvl(p_adj_rec.tax_adjusted,0) <> 0))
2225                     ))
2226            OR
2227            ((g_ae_doc_rec.source_table = 'RA')
2228             AND (((g_ae_rule_rec.gl_account_source1 = 'REVENUE_ON_INVOICE')
2229                   AND (((nvl(p_app_rec.line_ediscounted,0) + nvl(p_app_rec.freight_ediscounted,0) +
2230                          nvl(p_app_rec.charges_ediscounted,0)) <> 0)
2231                          OR ((g_ae_rule_rec.tax_code_source1 = 'NONE') AND (nvl(p_app_rec.tax_ediscounted,0) <> 0))
2232                       ))
2233                  OR
2234                  ((g_ae_rule_rec.gl_account_source2 = 'REVENUE_ON_INVOICE')
2235                   AND (((nvl(p_app_rec.line_uediscounted,0) + nvl(p_app_rec.freight_uediscounted,0) +
2236                          nvl(p_app_rec.charges_uediscounted,0)) <> 0)
2237                         OR ((g_ae_rule_rec.tax_code_source2 = 'NONE') AND (nvl(p_app_rec.tax_uediscounted,0) <> 0))
2238                       ))
2239                 ))
2240          )
2241      THEN --call revenue adjustment api
2242 */
2243    /*----------------------------------------------------------------------------+
2244     | Call the revenue adjustment api to derive the revenue distributions on the |
2245     | fly, to allocate the amounts for gl account source = revenue on Invoice.   |
2246     +----------------------------------------------------------------------------*/
2247 /*
2248         l_rev_adj_rec.customer_trx_id := p_invoice_id;
2249         l_rev_adj_rec.reason_code     := 'ACCOUNTING';
2250 
2251         AR_Revenue_Adjustment_PVT.Earn_Revenue
2252               (   p_api_version           => 2
2253                  ,p_init_msg_list         => FND_API.G_TRUE
2254                  ,p_commit                => FND_API.G_FALSE
2255                  ,p_validation_level      => FND_API.G_VALID_LEVEL_FULL
2256                  ,x_return_status         => l_return_status
2257                  ,x_msg_count             => l_msg_count
2258                  ,x_msg_data              => l_msg_data
2259                  ,p_rev_adj_rec           => l_rev_adj_rec
2260                  ,x_adjustment_id         => l_adj_id
2261                  ,x_adjustment_number     => l_adj_number
2262                  ,x_dist_count            => l_dist_count
2263                  ,x_ra_dist_tbl           => l_ra_dist_tbl);
2264 
2265         IF l_ra_dist_tbl.EXISTS(l_dist_count) AND (l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2266 
2267             FOR l_ctr2 IN l_ra_dist_tbl.FIRST .. l_ra_dist_tbl.LAST LOOP
2268 
2269               --process revenue distributions only
2270                 IF l_ra_dist_tbl(l_ctr2).account_class = 'REV' THEN
2271 
2272                     l_ae_tax_id := '';
2273 */
2274                 /*----------------------------------------------------------------------------+
2275                  |Unearn lines may have the tax code id set as they are associate with the    |
2276                  |original invoice lines, sweep through the unearned table for invoice lines  |
2277                  |which was the source for deriving the earned revenue using the rev adj api  |
2278                  +----------------------------------------------------------------------------*/
2279 /*
2280                     IF g_ae_unearn_rev_ctr > 0
2281                       AND l_ra_dist_tbl(l_ctr2).account_class = 'REV' THEN
2282 
2283                     --later on convert this into a join between the dist temp table
2284                     --and the ar_ae_alloc_rec_gt table
2285                        FOR g_ae_alloc_unearn_rev_tbl IN
2286                               l_rev_unearn('REVUNEARN', l_ra_dist_tbl(l_ctr2).customer_trx_line_id) LOOP
2287                               l_ae_tax_id := g_ae_alloc_unearn_rev_tbl.ae_tax_id;
2288                               EXIT;
2289 
2290                        END LOOP; --unearned revenue table
2291 
2292                     END IF; --dist class is REV
2293 
2294                 --Assign revenue lines and increment row counter
2295                     g_ae_rev_ctr := g_ae_rev_ctr + 1;
2296 
2297                 --assign elements
2298                     g_ae_alloc_rev_tbl.ae_account_class   := 'REVEARN';
2299                     g_ae_alloc_rev_tbl.ae_tax_id          := l_ae_tax_id;
2300                     g_ae_alloc_rev_tbl.ae_customer_trx_id :=
2301                                                        l_ra_dist_tbl(l_ctr2).customer_trx_id;
2302                     g_ae_alloc_rev_tbl.ae_customer_trx_line_id  :=
2303                                                        l_ra_dist_tbl(l_ctr2).customer_trx_line_id;
2304                     g_ae_alloc_rev_tbl.ae_code_combination_id   :=
2305                                                        l_ra_dist_tbl(l_ctr2).code_combination_id;
2306                     g_ae_alloc_rev_tbl.ae_amount                := l_ra_dist_tbl(l_ctr2).amount;
2307                     g_ae_alloc_rev_tbl.ae_acctd_amount          := l_ra_dist_tbl(l_ctr2).acctd_amount;
2308                     g_ae_alloc_rev_tbl.ae_pro_amt               := 0;
2309                     g_ae_alloc_rev_tbl.ae_pro_acctd_amt         := 0;
2310                     g_ae_alloc_rev_tbl.ae_pro_taxable_amt       := 0;
2311                     g_ae_alloc_rev_tbl.ae_pro_taxable_acctd_amt := 0;
2312                     g_ae_alloc_rev_tbl.ae_pro_split_taxable_amt := '';
2313                     g_ae_alloc_rev_tbl.ae_pro_split_taxable_acctd_amt := '';
2314                     g_ae_alloc_rev_tbl.ae_pro_recov_taxable_amt := '';
2315                     g_ae_alloc_rev_tbl.ae_pro_recov_taxable_acctd_amt := '';
2316                     g_ae_alloc_rev_tbl.ae_pro_def_tax_amt       := 0;
2317                     g_ae_alloc_rev_tbl.ae_pro_def_tax_acctd_amt := 0;
2318                     g_ae_alloc_rev_tbl.ae_counted_flag          := 'N';
2319 
2320                     Assign_Elements(g_ae_alloc_rev_tbl);
2321 
2322                  --Revenue total amounts and accounted amounts accumulator
2323                     g_ae_rule_rec.revenue_amt       :=
2324                           g_ae_rule_rec.revenue_amt + g_ae_alloc_rev_tbl.ae_amount;
2325                     g_ae_rule_rec.revenue_acctd_amt :=
2326                           g_ae_rule_rec.revenue_acctd_amt + g_ae_alloc_rev_tbl.ae_acctd_amount;
2327 
2328                 END IF; --revenue distributions only
2329 
2330             END LOOP; --end loop gl dist table from rev adj
2331 
2332          ELSIF ((l_return_status <> FND_API.G_RET_STS_SUCCESS)
2333                 OR ((l_dist_count = 0) AND (l_return_status = FND_API.G_RET_STS_SUCCESS))) THEN
2334 
2335                IF l_msg_count > 1 THEN
2336                   fnd_msg_pub.reset;
2337                 --get only the first message from the api message stack for forms users
2338                   l_mesg := fnd_msg_pub.get(p_encoded=>FND_API.G_FALSE);
2339                ELSE
2340                   l_mesg := l_msg_data;
2341                END IF;
2342 
2343              --Now set the message token
2344                FND_MESSAGE.SET_NAME('AR', 'GENERIC_MESSAGE');
2345                FND_MESSAGE.SET_TOKEN('GENERIC_TEXT', l_mesg);
2346 
2347                RAISE ram_api_error;
2348 
2349          END IF; --rev adj gl dist table exists and success from api
2350 
2351         --api Error handling
2352 
2353         --independent of the activity rule add the unearned entries to the revenue array,
2354         --to add to the sum of the line amount and accounted amount used as an allocation
2355         --basis to allocate amounts and formulate taxable amounts
2356        ELSE
2357 
2358           IF g_ae_unearn_rev_ctr > 0 THEN
2359 */
2360 --             UPDATE /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3 */ ar_ae_alloc_rec_gt
2361 /*
2362              SET ae_account_class = 'REVEARN'
2363              WHERE ae_id = g_id
2364              AND   ae_account_class = 'REVUNEARN';
2365 
2366           --Add the unearned count to the revenue count
2367              g_ae_rev_ctr := g_ae_rev_ctr + g_ae_unearn_rev_ctr;
2368 
2369           --Add the unearned amounts to the revenue amounts
2370              g_ae_rule_rec.revenue_amt       :=
2371                g_ae_rule_rec.revenue_amt + g_sum_unearn_rev_amt;
2372              g_ae_rule_rec.revenue_acctd_amt :=
2373                g_ae_rule_rec.revenue_acctd_amt + g_sum_unearn_rev_acctd_amt;
2374 
2375           END IF; --unearned revenue entry exists
2376 
2377        END IF; --ram api is called to derive revenue distributions
2378 
2379    END IF; --there exists a non zero unearned amount or accounted amount
2380 */
2381  /*----------------------------------------------------------------------------------+
2382   | Set the tax link id's for the revenue lines - we do this here since ram api is   |
2383   | called after the insert of original revenue, hence all link ids set at one time. |
2384   +----------------------------------------------------------------------------------*/
2385    UPDATE /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
2386           ar_ae_alloc_rec_gt b1
2387    SET (b1.ae_tax_link_id, b1.ae_tax_link_id_ed_adj, b1.ae_tax_link_id_uned) =
2388         (select /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
2389                 max(b8.ae_tax_link_id)        ae_tax_link_id,
2390                 max(b8.ae_tax_link_id_ed_adj) ae_tax_link_id_ed_adj,
2391                 max(b8.ae_tax_link_id_uned)   ae_tax_link_id_uned
2392              from ar_ae_alloc_rec_gt b8
2393              where b8.ae_id = g_id
2394              and   b8.ae_account_class = 'TAX'
2395              and   b8.ae_link_to_cust_trx_line_id = b1.ae_customer_trx_line_id)
2396    WHERE b1.ae_id = g_id
2397    AND   b1.ae_account_class IN ('REVEARN','REVUNEARN')  --MAINTAINTAXLINKID
2398    AND   EXISTS (select /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
2399                         'x'
2400                  from ar_ae_alloc_rec_gt b2
2401                  where b2.ae_id = g_id
2402                  and   b2.ae_account_class = 'TAX'
2403                  and   b2.ae_link_to_cust_trx_line_id = b1.ae_customer_trx_line_id);
2404 
2405  /*----------------------------------------------------------------------------------+
2406   | Get revenue amount totals, and fill up the revenue total table when tax code     |
2407   | source is NONE and there exists a tax amount to be allocated to gl account source|
2408   | Caches revenue amount and accounted amount totals for each Invoice Line, if      |
2409   | already cached then add to existing accumulators, totals for each Invoice        |
2410   | are required to be cached for rule allocating tax to revenue if TAX CODE         |
2411   | source is NONE.                                                                  |
2412   +----------------------------------------------------------------------------------*/
2413 
2414    IF (((g_ae_doc_rec.source_table = 'RA')
2415         AND (((nvl(p_app_rec.tax_ediscounted,0) <> 0) AND (g_ae_rule_rec.tax_code_source1 = 'NONE'))
2416              OR ((nvl(p_app_rec.tax_uediscounted,0) <> 0) AND (g_ae_rule_rec.tax_code_source2 = 'NONE'))))
2417        OR ((g_ae_doc_rec.source_table = 'ADJ') AND (g_ae_rule_rec.tax_code_source1 = 'NONE')
2418             AND (nvl(p_adj_rec.tax_adjusted,0) <> 0))) AND (g_ae_rev_ctr > 0)
2419    THEN
2420 
2421            UPDATE /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
2422                   ar_ae_alloc_rec_gt at1
2423            SET    (at1.ae_inv_line         ,
2424                    at1.ae_sum_rev_amt      ,
2425                    at1.ae_sum_rev_acctd_amt,
2426                    at1.ae_count) =
2427                      (SELECT /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N1) */
2428                              at2.ae_customer_trx_line_id,
2429                              sum(at2.ae_amount),
2430                              sum(at2.ae_acctd_amount),
2431                              count(at2.ae_customer_trx_line_id)
2432                       FROM ar_ae_alloc_rec_gt at2
2433                       WHERE at2.ae_id = g_id
2434                       AND   at2.ae_customer_trx_line_id = at1.ae_customer_trx_line_id
2435                       AND   at2.ae_account_class IN ('REVEARN','REVUNEARN') --MAINTAINTAXLINKID
2436                       GROUP BY at2.ae_customer_trx_line_id)
2437            WHERE at1.ae_id = g_id
2438            AND   at1.ae_account_class IN ('REVEARN','REVUNEARN'); --MAINTAINTAXLINKID
2439 
2440    END IF; --create revenue total table
2441 
2442 
2443    IF PG_DEBUG in ('Y', 'C') THEN
2444       arp_standard.debug('Get_Invoice_Distributions: ' ||
2445                          'g_ae_rule_rec.gl_account_source1    = '||g_ae_rule_rec.gl_account_source1);
2446       arp_standard.debug('Get_Invoice_Distributions: ' ||
2447                          'g_ae_rule_rec.tax_code_source1      = '||g_ae_rule_rec.tax_code_source1);
2448       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_recoverable_flag1 = '||
2449                          g_ae_rule_rec.tax_recoverable_flag1);
2450       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.code_combination_id1  = '||
2451                          g_ae_rule_rec.code_combination_id1);
2452       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.asset_tax_code1       = '||
2453                          g_ae_rule_rec.asset_tax_code1);
2454       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.liability_tax_code1   = '||
2455                          g_ae_rule_rec.liability_tax_code1);
2456       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.act_tax_non_rec_ccid1 = '||
2457                          g_ae_rule_rec.act_tax_non_rec_ccid1);
2458       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.gl_account_source2    = '||
2459                          g_ae_rule_rec.gl_account_source2);
2460       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_code_source2      = '||
2461                          g_ae_rule_rec.tax_code_source2);
2462       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_recoverable_flag2 = '||
2463                          g_ae_rule_rec.tax_recoverable_flag2);
2464       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.code_combination_id2  = '||
2465                          g_ae_rule_rec.code_combination_id2);
2466       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.asset_tax_code2       = '||
2467                          g_ae_rule_rec.asset_tax_code2);
2468       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.liability_tax_code2   = '||
2469                          g_ae_rule_rec.liability_tax_code2);
2470       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.act_tax_non_rec_ccid2 = '||
2471                          g_ae_rule_rec.act_tax_non_rec_ccid2);
2472       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.receivable_account    = '||
2473                          g_ae_rule_rec.receivable_account);
2474       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.receivable_amt        = '||
2475                          g_ae_rule_rec.receivable_amt);
2476       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.receivable_acctd_amt  = '||
2477                          g_ae_rule_rec.receivable_acctd_amt);
2478       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.revenue_amt           = '||
2479                          g_ae_rule_rec.revenue_amt);
2480       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.revenue_acctd_amt     = '||
2481                          g_ae_rule_rec.revenue_acctd_amt);
2482       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_amt               = '||
2483                          g_ae_rule_rec.tax_amt);
2484       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.tax_acctd_amt         = '||
2485                          g_ae_rule_rec.tax_acctd_amt);
2486       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.def_tax_amt           = '||
2487                          g_ae_rule_rec.def_tax_amt);
2488       arp_standard.debug('Get_Invoice_Distributions: ' || 'g_ae_rule_rec.def_tax_acctd_amt     = '||
2489                          g_ae_rule_rec.def_tax_acctd_amt);
2490      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Invoice_Distributions()-');
2491   END IF;
2492 
2493 
2494 EXCEPTION
2495   WHEN invalid_ccid_error THEN
2496      IF PG_DEBUG in ('Y', 'C') THEN
2497         arp_standard.debug('Invalid Tax ccid - ARP_ALLOCATION_PKG.Get_Invoice_Distributions' );
2498      END IF;
2499      fnd_message.set_name('AR','AR_INVALID_TAX_ACCOUNT');
2500      RAISE;
2501 
2502   WHEN ram_api_error THEN
2503      IF PG_DEBUG in ('Y', 'C') THEN
2504         arp_standard.debug('ram_api_error - ARP_ALLOCATION_PKG.Get_Invoice_Distributions' );
2505      END IF;
2506      RAISE;
2507 
2508   WHEN NO_DATA_FOUND THEN
2509      IF PG_DEBUG in ('Y', 'C') THEN
2510         arp_standard.debug('ARP_ALLOCATION_PKG.Get_Invoice_Distributions - NO_DATA_FOUND' );
2511      END IF;
2512      RAISE;
2513 
2514   WHEN OTHERS THEN
2515      IF PG_DEBUG in ('Y', 'C') THEN
2516         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Get_Invoice_Distributions');
2517      END IF;
2518      RAISE;
2519 
2520 END Get_Invoice_Distributions;
2521 
2522 /* ==========================================================================
2523  | PROCEDURE Get_Tax_Link_Id
2524  |
2525  | DESCRIPTION
2526  |      Assigns Tax link Ids to each tax line in the tax table. The Rule
2527  |      assigining a tax link id is if for an Invoice line tax distribution
2528  |      there exists another tax distribution for an different Invoice line
2529  |      with the same Tax codes and count of Tax distributions for these
2530  |      Invoice lines then the Tax distributions of the first Invoice Line
2531  |      can be grouped with the Tax distributions of the second Invoice line.
2532  |      Tax link id can be common across different Invoice documents
2533  |
2534  | SCOPE - PRIVATE
2535  |
2536  | PARAMETERS
2537  |      p_type_acct         IN     Indicates accounting to be done for Earned
2538  |                                 discounts Adjustments or Unearned discounts
2539  *===========================================================================*/
2540 PROCEDURE Get_Tax_Link_Id(p_process_ed_adj IN VARCHAR2,
2541                           p_process_uned   IN VARCHAR2,
2542                           p_process_pay    IN VARCHAR2) IS
2543 
2544 l_link_ctr        BINARY_INTEGER := 0; --Actual Tax link id counter
2545 l_link_ctr1       BINARY_INTEGER := 0; --In case earned and unearned require link id
2546 
2547 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
2548 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
2549 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
2550 
2551 cursor get_tax IS
2552 select /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
2553       ae_link_to_cust_trx_line_id
2554 from ar_ae_alloc_rec_gt
2555 where ae_id = g_id
2556 and ae_account_class = 'TAX'
2557 group by ae_link_to_cust_trx_line_id
2558 order by ae_link_to_cust_trx_line_id;
2559 
2560 inv_line_tbl  DBMS_SQL.NUMBER_TABLE;
2561 
2562 l_last_fetch BOOLEAN := FALSE;
2563 l_mirror_link_ctr VARCHAR2(1) := 'N';
2564 l_set_ed_adj_link VARCHAR2(1) := 'N';
2565 l_set_uned_link   VARCHAR2(1) := 'N';
2566 l_set_pay_link    VARCHAR2(1) := 'N';
2567 
2568 BEGIN
2569    IF PG_DEBUG in ('Y', 'C') THEN
2570       arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Tax_Link_Id()+');
2571    END IF;
2572 
2573    l_link_ctr := g_link_ctr;
2574 
2575   -----------------------------------------------------------------------------
2576   --Set earned discount and adjustment link flag
2577   -----------------------------------------------------------------------------
2578    IF ((p_process_ed_adj = 'Y') AND (g_ed_adj_activity_link = 0)) THEN
2579       l_set_ed_adj_link := 'Y';
2580       IF PG_DEBUG in ('Y', 'C') THEN
2581          arp_standard.debug('Get_Tax_Link_Id: ' || ' l_set_ed_adj_link ' || l_set_ed_adj_link);
2582       END IF;
2583    END IF;
2584 
2585   -----------------------------------------------------------------------------
2586   --Set unearned discount link flag
2587   -----------------------------------------------------------------------------
2588    IF ((p_process_uned = 'Y') AND (g_uned_activity_link = 0)) THEN
2589       l_set_uned_link := 'Y';
2590       IF PG_DEBUG in ('Y', 'C') THEN
2591          arp_standard.debug('Get_Tax_Link_Id: ' || ' l_set_uned_link ' || l_set_uned_link);
2592       END IF;
2593    END IF;
2594 
2595   -----------------------------------------------------------------------------
2596   --Set payment link flag to calculate taxable amounts for deferred tax
2597   -----------------------------------------------------------------------------
2598    IF p_process_pay = 'Y' THEN
2599       l_set_pay_link := 'Y';
2600       IF PG_DEBUG in ('Y', 'C') THEN
2601          arp_standard.debug('Get_Tax_Link_Id: ' || 'l_set_pay_link ' || l_set_pay_link);
2602       END IF;
2603    END IF;
2604 
2605   -----------------------------------------------------------------------------
2606   --Set the mirror link counter since seperate links maintained for discounts
2607   --earned and unearned
2608   -----------------------------------------------------------------------------
2609    IF l_set_ed_adj_link = 'Y' AND l_set_uned_link = 'Y' THEN
2610       l_mirror_link_ctr := 'Y';
2611       l_link_ctr1 := l_link_ctr + g_ae_tax_ctr + 1;
2612       IF PG_DEBUG in ('Y', 'C') THEN
2613          arp_standard.debug('Get_Tax_Link_Id: ' || 'l_mirror_link_ctr ' || l_mirror_link_ctr);
2614          arp_standard.debug('Get_Tax_Link_Id: ' || ' l_link_ctr1 ' || l_link_ctr1);
2615       END IF;
2616    END IF;
2617 
2618   -----------------------------------------------------------------------------
2619   --Verify as to whether links require to be created
2620   -----------------------------------------------------------------------------
2621    IF (g_ae_tax_ctr > 0)
2622       AND ((l_set_ed_adj_link = 'Y') OR (l_set_uned_link = 'Y') OR (l_set_pay_link = 'Y'))
2623    THEN
2624   /*-------------------------------------------------------------------------------+
2625    | Link basis for tax code source other than ACTIVITY tax code is off the tax on |
2626    | the Invoice (Tax distribitions on Invoice) so this processing is required.    |
2627    | Set the tax line counts for each tax distribution associated with Invoice line|
2628    +-------------------------------------------------------------------------------*/
2629       OPEN get_tax;
2630 
2631       IF PG_DEBUG in ('Y', 'C') THEN
2632          arp_standard.debug('Get_Tax_Link_Id: ' || 'Opened get_tax Cursor ');
2633       END IF;
2634 
2635       LOOP
2636       --initialize record
2637 
2638         FETCH get_tax BULK COLLECT INTO
2639         inv_line_tbl
2640         LIMIT g_bulk_fetch_rows;
2641 
2642        IF get_tax%NOTFOUND THEN
2643           l_last_fetch := TRUE;
2644        END IF;
2645 
2646        IF (inv_line_tbl.COUNT = 0) AND (l_last_fetch) THEN
2647          IF PG_DEBUG in ('Y', 'C') THEN
2648             arp_standard.debug('Get_Tax_Link_Id: ' || 'COUNT = 0 and LAST FETCH ');
2649          END IF;
2650          EXIT;
2651        END IF;
2652 
2653        FOR i IN inv_line_tbl.FIRST .. inv_line_tbl.LAST LOOP
2654 
2655         --replace with a function
2656 
2657              IF ((l_set_pay_link = 'Y') OR (l_set_ed_adj_link = 'Y')
2658                   OR ((l_set_uned_link = 'Y') AND (l_mirror_link_ctr = 'N'))) THEN
2659                   l_link_ctr := l_link_ctr + 1;
2660              END IF;
2661 
2662              IF ((l_set_uned_link = 'Y') AND (l_mirror_link_ctr = 'Y')) THEN
2663                 l_link_ctr1 := l_link_ctr1 + 1;
2664              END IF;
2665 
2666              IF PG_DEBUG in ('Y', 'C') THEN
2667                 arp_standard.debug('Get_Tax_Link_Id: ' || 'Link Id l_link_ctr  ' || l_link_ctr);
2668                 arp_standard.debug('Get_Tax_Link_Id: ' || 'Link Id l_link_ctr1 ' || l_link_ctr1);
2669                 arp_standard.debug('Get_Tax_Link_Id: ' ||  'inv_line_tbl(' || i || ') = '|| inv_line_tbl(i));
2670              END IF;
2671 
2672              update /*+ INDEX(art3 AR_AE_ALLOC_REC_GT_N4) */
2673                    ar_ae_alloc_rec_gt art3
2674              set art3.ae_tax_link_id        = decode(l_set_pay_link,
2675                                                      'Y', l_link_ctr,
2676                                                      ''),
2677                  art3.ae_tax_link_id_ed_adj = decode(l_set_ed_adj_link,
2678                                                      'Y', l_link_ctr,
2679                                                      art3.ae_tax_link_id_ed_adj),
2680                  art3.ae_tax_link_id_uned   = decode(l_set_uned_link,
2681                                                      'Y', decode(l_mirror_link_ctr,
2682                                                                  'Y', l_link_ctr1,
2683                                                                  l_link_ctr),
2684                                                      art3.ae_tax_link_id_uned),
2685                  art3.ae_counted_flag       = 'Y'
2686              where ae_id = g_id
2687              and art3.ae_account_class = 'TAX'
2688              and art3.ae_counted_flag = 'N'
2689              and art3.ae_link_to_cust_trx_line_id IN
2690              (select to_line
2691              from(
2692              select /*+ INDEX(art1 AR_AE_ALLOC_REC_GT_N4) INDEX(art2 AR_AE_ALLOC_REC_GT_N2) */
2693                     art1.ae_link_to_cust_trx_line_id            from_line      ,
2694                     art2.ae_link_to_cust_trx_line_id            to_line        ,
2695                     max(art1.ae_tax_line_count)                 tax_line_count ,
2696                     1                                           hit_count
2697              from ar_ae_alloc_rec_gt art1,
2698                   ar_ae_alloc_rec_gt art2
2699              where art1.ae_id = g_id
2700              and   art1.ae_account_class            = 'TAX'
2701              and   art1.ae_account_class            = art2.ae_account_class
2702              and   art1.ae_tax_id                   = art2.ae_tax_id
2703              and   art1.ae_tax_type                 = art2.ae_tax_type
2704              and   art1.ae_tax_line_count           = art2.ae_tax_line_count
2705              and   art1.ae_link_to_cust_trx_line_id = inv_line_tbl(i)
2706              --and   art1.ae_link_to_cust_trx_line_id <> art2.ae_link_to_cust_trx_line_id
2707              and   art2.ae_id = art1.ae_id
2708              and   art1.ae_counted_flag = 'N'
2709              and   art2.ae_counted_flag = 'N'
2710              group by art1.ae_link_to_cust_trx_line_id,
2711                       art2.ae_link_to_cust_trx_line_id,
2712                       art1.ae_tax_id,
2713                       art1.ae_tax_type)
2714              group by from_line, to_line
2715              having sum(hit_count) = max(tax_line_count));
2716 
2717              IF SQL%NOTFOUND THEN
2718                 IF ((l_set_pay_link = 'Y') OR (l_set_ed_adj_link = 'Y')
2719                      OR ((l_set_uned_link = 'Y') AND (l_mirror_link_ctr = 'N'))) THEN
2720                      l_link_ctr := l_link_ctr - 1;
2721                 END IF;
2722 
2723                 IF ((l_set_uned_link = 'Y') AND (l_mirror_link_ctr = 'Y')) THEN
2724                    l_link_ctr1 := l_link_ctr1 - 1;
2725                 END IF;
2726 
2727                 IF PG_DEBUG in ('Y', 'C') THEN
2728                    arp_standard.debug('Get_Tax_Link_Id: ' || 'Reversed Link Id l_link_ctr  ' || l_link_ctr);
2729                    arp_standard.debug('Get_Tax_Link_Id: ' || 'Reversed Link Id l_link_ctr1 ' || l_link_ctr1);
2730                 END IF;
2731              END IF;
2732 
2733          END LOOP; --sweep tax lines to formulate link
2734 
2735        --Exit if Last fetch
2736          IF l_last_fetch THEN
2737             EXIT;
2738          END IF;
2739 
2740         END LOOP; --process revenue tax bulk fetch
2741 
2742         CLOSE get_tax;
2743 
2744    END IF; --End if tax code source is ACTIVITY
2745 
2746    IF PG_DEBUG in ('Y', 'C') THEN
2747       arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Tax_Link_Id()-');
2748    END IF;
2749 
2750 EXCEPTION
2751   WHEN NO_DATA_FOUND THEN
2752      IF PG_DEBUG in ('Y', 'C') THEN
2753         arp_standard.debug('Get_Tax_Link_Id: ' || 'SQLERRM ' || SQLERRM);
2754         arp_standard.debug('ARP_ALLOCATION_PKG.Get_Tax_Link_Id - NO_DATA_FOUND' );
2755      END IF;
2756      RAISE;
2757 
2758   WHEN OTHERS THEN
2759      IF PG_DEBUG in ('Y', 'C') THEN
2760         arp_standard.debug('Get_Tax_Link_Id: ' || 'SQLERRM ' || SQLERRM);
2761         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Get_Tax_Link_Id');
2762      END IF;
2763      RAISE;
2764 
2765 END Get_Tax_Link_Id;
2766 
2767 /* ==========================================================================
2768  | PROCEDURE Override_Accounts
2769  |
2770  | DESCRIPTION
2771  |      Sets the Override account flags for earned discounts, adjustments
2772  |      unearned discounts
2773  |
2774  | SCOPE - PRIVATE
2775  |
2776  | PARAMETERS
2777  |      p_app_rec       IN    Earned and Unearned discount details
2778  |      p_adj_rec       IN    Adjustment details
2779  |      p_override1     IN    Override Earned discount/Adjustment account
2780  |      p_override2     IN    Override Unearned discount account
2781  *==========================================================================*/
2782 PROCEDURE Override_Accounts(p_app_rec   IN  ar_receivable_applications%ROWTYPE,
2783                             p_adj_rec   IN  ar_adjustments%ROWTYPE,
2784                             p_override1 OUT NOCOPY  VARCHAR2,
2785                             p_override2 OUT NOCOPY VARCHAR2 ) IS
2786 BEGIN
2787      /*------------------------------------------------------------------------------+
2788       | Set the override flagse for earned discounts/adjustments, unearned discounts |
2789       | as default FALSE                                                             |
2790       +------------------------------------------------------------------------------*/
2791        p_override1 := 'N';
2792        p_override2 := 'N';
2793 
2794      /*------------------------------------------------------------------------------+
2795       | Override revenue accounts for discounts and adjustments                      |
2796       +------------------------------------------------------------------------------*/
2797        IF g_ae_rule_rec.gl_account_source1 IN ('ACTIVITY_GL_ACCOUNT', 'TAX_CODE_ON_INVOICE') THEN
2798 
2799         /*------------------------------------------------------------------------------+
2800          | Override revenue account using first net expense account from tax for earned |
2801          | discounts.                                                                   |
2802          +------------------------------------------------------------------------------*/
2803           IF (g_ae_doc_rec.source_table = 'RA')
2804               AND (((nvl(p_app_rec.line_ediscounted,0) + nvl(p_app_rec.freight_ediscounted,0) +
2805                     nvl(p_app_rec.charges_ediscounted,0)) <> 0)
2806                    OR ((g_ae_rule_rec.tax_code_source1 = 'NONE') AND (nvl(p_app_rec.tax_ediscounted,0) <> 0)))
2807           THEN
2808              p_override1 := 'Y';
2809 
2810         /*------------------------------------------------------------------------------+
2811          | Override revenue account using first net expense account from tax for        |
2812          | adjustments.                                                                 |
2813          +------------------------------------------------------------------------------*/
2814           ELSIF g_ae_doc_rec.source_table = 'ADJ' AND g_ae_doc_rec.document_type = 'ADJUSTMENT'
2815                AND (((nvl(p_adj_rec.line_adjusted,0) + nvl(p_adj_rec.freight_adjusted,0) +
2816                       nvl(p_adj_rec.receivables_charges_adjusted,0)) <> 0)
2817                     OR ((g_ae_rule_rec.tax_code_source1 = 'NONE') AND (nvl(p_adj_rec.tax_adjusted,0) <> 0)))
2818                THEN
2819                    p_override1 := 'Y';
2820 
2821         /*------------------------------------------------------------------------------+
2822          | Override revenue account using first net expense account from tax for        |
2823          | finance charges.                                                             |
2824          +------------------------------------------------------------------------------*/
2825           ELSIF g_ae_doc_rec.source_table = 'ADJ' AND g_ae_doc_rec.document_type = 'FINANCE_CHARGES'
2826                AND (((nvl(p_adj_rec.line_adjusted,0) + nvl(p_adj_rec.freight_adjusted,0) +
2827                       nvl(p_adj_rec.receivables_charges_adjusted,0)) <> 0)
2828                     OR ((g_ae_rule_rec.tax_code_source1 = 'NONE') AND (nvl(p_adj_rec.tax_adjusted,0) <> 0)))
2829                THEN
2830                   p_override1 := 'Y';
2831           END IF;
2832 
2833        END IF; --End if TAX_CODE_ON_INVOICE for earned discounts, adjustments
2834 
2835      /*------------------------------------------------------------------------------+
2836       | Override revenue account using first net expense account from tax for        |
2837       | unearned discounts.                                                          |
2838       +------------------------------------------------------------------------------*/
2839        IF g_ae_rule_rec.gl_account_source2 IN ('ACTIVITY_GL_ACCOUNT', 'TAX_CODE_ON_INVOICE') THEN
2840           IF (g_ae_doc_rec.source_table = 'RA')
2841               AND (((nvl(p_app_rec.line_uediscounted,0) + nvl(p_app_rec.freight_uediscounted,0) +
2842                      nvl(p_app_rec.charges_uediscounted,0)) <> 0)
2843                    OR ((g_ae_rule_rec.tax_code_source2 = 'NONE') AND (nvl(p_app_rec.tax_uediscounted,0) <> 0)))
2844           THEN
2845              p_override2 := 'Y';
2846 
2847           END IF; --override enable condition
2848 
2849         END IF; --tax code on invoice for unearned discounts
2850 
2851       --set the global variables to be used in Build_Rev
2852         g_override1 := p_override1;
2853         g_override2 := p_override2;
2854 
2855 EXCEPTION
2856   WHEN OTHERS THEN
2857      IF PG_DEBUG in ('Y', 'C') THEN
2858         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Override_Accounts ');
2859      END IF;
2860      RAISE;
2861 
2862 END Override_Accounts;
2863 
2864 /* ==========================================================================
2865  | PROCEDURE Process_Amounts
2866  |
2867  | DESCRIPTION
2868  |      Allocates discounts or adjustments over Revenue and Tax distributions
2869  |      based on Rules set up at receivable activity level.
2870  |
2871  | SCOPE - PRIVATE
2872  |
2873  | PARAMETERS
2874  |      p_type_acct          IN  Flag to indicate accounting for Earned
2875  |                               discounts Adjustments or Unearned discounts
2876  *==========================================================================*/
2877 PROCEDURE Process_Amounts(p_app_rec   IN ar_receivable_applications%ROWTYPE,
2878                           p_adj_rec   IN ar_adjustments%ROWTYPE) IS
2879 
2880 BEGIN
2881 
2882   IF PG_DEBUG in ('Y', 'C') THEN
2883      arp_standard.debug( 'ARP_ALLOCATION_PKG.Process_Amounts()+');
2884   END IF;
2885 
2886 /*------------------------------------------------------------------------------+
2887  | Initialise revenue amounts to allocate for discounts or adjustments          |
2888  +------------------------------------------------------------------------------*/
2889 /*
2890   Init_Amts(p_type_acct         => p_type_acct,
2891             p_app_rec           => p_app_rec  ,
2892             p_adj_rec           => p_adj_rec   );
2893 */
2894 --}
2895 
2896 /*------------------------------------------------------------------------------+
2897  | Build gross to activity gl account, for payments where deferred tax exists   |
2898  | this routine is never called and the else block is executed                  |
2899  +------------------------------------------------------------------------------*/
2900  /*
2901   IF (((p_type_acct = 'ED_ADJ') AND (g_ae_rule_rec.gl_account_source1 = 'ACTIVITY_GL_ACCOUNT')
2902         AND (g_ae_rule_rec.tax_code_source1 = 'NONE'))
2903       OR ((p_type_acct = 'UNED') AND (g_ae_rule_rec.gl_account_source2 = 'ACTIVITY_GL_ACCOUNT')
2904            AND (g_ae_rule_rec.tax_code_source2 = 'NONE')))
2905      AND ((NOT g_ae_def_tax) AND (g_done_def_tax)) THEN
2906 
2907       Gross_To_Activity_GL(p_type_acct => p_type_acct);
2908 
2909   ELSE
2910   */
2911   --}
2912    /*---------------------------------------------------------------------------------+
2913     | Allocate discount, adjustment, finance charge amounts over tax. Tax is allocated|
2914     | if there is either an amount or accounted amount tax base in this case if the   |
2915     | tax rule is NONE then only deferred tax treatment is done with tax going to line|
2916     | buckets for allocation otherwise.                                               |
2917     +---------------------------------------------------------------------------------*/
2918    --  Alloc_Rev_Tax_Amt(p_type_acct => p_type_acct) ;
2919    --}
2920     /*------------------------------------------------------------------------------+
2921      | Allocate Tax to Revenue. When processing reaches this point then it means    |
2922      | there exists zero or non zero revenue lines so allocate tax to revenue. For  |
2923      | payments there is no need to allocate tax to revenue as the deferred tax is  |
2924      | only required to be moved.                                                   |
2925      +------------------------------------------------------------------------------*/
2926 /*
2927       IF ((g_ae_tax_ctr > 0)
2928            AND ((g_ae_rule_rec.tax_amt_alloc <> 0)
2929                 OR (g_ae_rule_rec.tax_acctd_amt_alloc <> 0))
2930            AND ((NOT g_bound_tax) AND (NOT g_bound_activity)))
2931          AND (p_type_acct <> 'PAY')
2932       THEN
2933 
2934          Allocate_Tax_To_Rev(p_type_acct   => p_type_acct);
2935 
2936       END IF;
2937 */
2938    /*---------------------------------------------------------------------------------+
2939     | Set actual link ids for Revenue and Tax lines. Where revenue amounts are zero   |
2940     | this routine will not get executed as there is no revenue to link to, so link id|
2941     | for revenue and tax allocations is null.
2942     +---------------------------------------------------------------------------------*/
2943 -- Set_link_Id needs to be executed every time
2944 -- because it allows to TAX line to tie to the Revenue associated
2945 -- in 11i we use a special sequence to tie tax line to the revenue line alone with summarization
2946 -- in 11iX we use the each tax line should be tied individualy to its revnue line related
2947 -- we use the link_to_cust_trx_line_id
2948 -- The tax link line id only concerns activity on tax invoice disctributions
2949 -- In the case of distribution affected to TAX, the actual link to tax id is
2950 -- link_to_cust_trx_line_id if there is a distribution affected to 'REV' with the
2951 -- ref_customer_trx_line_id equals to the link_to_cust_trx_line_id of the tax distribution
2952 -- In the case of disctribution affected to REV, the actual link to tax id is
2953 -- the ref_customer_trx_line_id if there is a link_to_cust_trx_line_id of a tax distribution
2954 -- equals to it
2955 --
2956 --     IF (g_ae_tax_ctr > 0) AND (g_ae_rev_ctr > 0)
2957 --        AND (NOT g_bound_tax) AND (NOT g_bound_activity) AND (p_type_acct <> 'PAY') THEN
2958 
2959       /*---------------------------------------------------------------------------------+
2960        | Set actual link ids for Revenue lines with non zero amounts or accounted amounts|
2961        +---------------------------------------------------------------------------------*/
2962         Set_Rev_Links(p_type_acct => 'ANYTHING');
2963 
2964 --     END IF;
2965 --}
2966    /*------------------------------------------------------------------------------+
2967     | Build Revenue and Tax Lines based on rules                                   |
2968     +------------------------------------------------------------------------------*/
2969      Build_Lines; --(p_type_acct    => p_type_acct );
2970 
2971 --  END IF; --Gross to activity GL Account
2972 
2973 /*------------------------------------------------------------------------------+
2974  | Only for discount and adjustment amounts to be accounted, the rule columns   |
2975  | need to be updated with Rule used.If there is just a payment then the rule   |
2976  | information is not maintained.                                               |
2977  +------------------------------------------------------------------------------*/
2978   IF (((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.earned_discount_taken,0) <> 0))
2979        OR ((g_ae_doc_rec.source_table = 'ADJ') AND (nvl(p_adj_rec.amount,0) <> 0))
2980        OR ((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.unearned_discount_taken,0) <> 0))) THEN
2981 
2982 /*------------------------------------------------------------------------------+
2983  | For payments do not call the document rule routine as this is not required   |
2984  +------------------------------------------------------------------------------*/
2985    -- This is part of avoiding calling process_amount for ED_ADJ and UNED and PAY
2986    IF    ((g_ae_doc_rec.source_table = 'ADJ') AND (nvl(p_adj_rec.amount,0) <> 0))
2987       OR ((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.earned_discount_taken,0) <> 0))
2988    THEN
2989 
2990      Doc_Tax_Acct_Rule(p_type_acct => 'ED_ADJ',
2991                        p_app_rec   => p_app_rec  ,
2992                        p_adj_rec   => p_adj_rec   );
2993    END IF;
2994 
2995    IF    ((g_ae_doc_rec.source_table = 'RA') AND (nvl(p_app_rec.unearned_discount_taken,0) <> 0))
2996    THEN
2997      Doc_Tax_Acct_Rule(p_type_acct => 'UNED',
2998                        p_app_rec   => p_app_rec  ,
2999                        p_adj_rec   => p_adj_rec   );
3000    END IF;
3001 
3002   END IF;
3003 
3004   IF PG_DEBUG in ('Y', 'C') THEN
3005      arp_standard.debug( 'ARP_ALLOCATION_PKG.Process_Amounts()-');
3006   END IF;
3007 
3008 EXCEPTION
3009   WHEN OTHERS THEN
3010      IF PG_DEBUG in ('Y', 'C') THEN
3011         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Process_Amounts');
3012      END IF;
3013      RAISE;
3014 
3015 END Process_Amounts;
3016 
3017 /* ==========================================================================
3018  | PROCEDURE Doc_Tax_Acct_Rule
3019  |
3020  | DESCRIPTION
3021  |      Updates the discount or adjustment with the accounting Rule used to
3022  |      allocate the line, tax, freight charges to required accounts. This
3023  |      helps in keeping a history of which rule was used as accounts for a
3024  |      receivable activity can change. The rule used and the amount buckets
3025  |      for the discount or adjustment can then be used to determine the nature
3026  |      of the accounting if required.
3027  |
3028  | SCOPE - PRIVATE
3029  |
3030  | PARAMETERS
3031  |      p_type_acct          IN  Flag to indicate accounting for Earned
3032  |                               discounts Adjustments or Unearned discounts
3033  |      p_app_rec            IN  Receivable Application detail record
3034  |      p_adj_rec            IN  Adjustment detail record
3035  *==========================================================================*/
3036 PROCEDURE Doc_Tax_Acct_Rule(p_type_acct IN VARCHAR2                           ,
3037                             p_app_rec   IN ar_receivable_applications%ROWTYPE ,
3038                             p_adj_rec   IN ar_adjustments%ROWTYPE              ) IS
3039 
3040 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
3041 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
3042 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
3043 l_rule_used            VARCHAR2(3);
3044 
3045 BEGIN
3046 
3047   IF PG_DEBUG in ('Y', 'C') THEN
3048      arp_standard.debug( 'ARP_ALLOCATION_PKG.Doc_Tax_Acct_Rule()+');
3049   END IF;
3050 
3051  /*-------------------------------------------------------------------------------+
3052   | Get Rules so that parent discount or adjustment record can be updated with the|
3053   | Rule details.                                                                 |
3054   +-------------------------------------------------------------------------------*/
3055    Get_Rules(p_type_acct            => p_type_acct,
3056              p_gl_account_source    => l_gl_account_source,
3057              p_tax_code_source      => l_tax_code_source,
3058              p_tax_recoverable_flag => l_tax_recoverable_flag);
3059 
3060  /*---------------------------------------------------------------------------------+
3061   | Set up 1 out of 12 possible Rules that could be setup for a receivable activity.|
3062   +--------------------------------------------------------------------------------*/
3063    IF (l_gl_account_source = 'ACTIVITY_GL_ACCOUNT') AND (l_tax_code_source = 'INVOICE')
3064       AND (l_tax_recoverable_flag = 'Y') THEN
3065 
3066       l_rule_used := '31';
3067 
3068    ELSIF (l_gl_account_source = 'ACTIVITY_GL_ACCOUNT') AND (l_tax_code_source = 'INVOICE')
3069          AND (l_tax_recoverable_flag = 'N') THEN
3070 
3071          l_rule_used := '32';
3072 
3073    ELSIF (l_gl_account_source = 'ACTIVITY_GL_ACCOUNT') AND (l_tax_code_source = 'NONE') THEN
3074 
3075           l_rule_used := '33';
3076 
3077    ELSIF (l_gl_account_source = 'ACTIVITY_GL_ACCOUNT') AND (l_tax_code_source = 'ACTIVITY') THEN
3078 
3079          l_rule_used := '34';
3080 
3081    ELSIF (l_gl_account_source = 'TAX_CODE_ON_INVOICE') AND (l_tax_code_source = 'INVOICE')
3082          AND (l_tax_recoverable_flag = 'Y') THEN
3083 
3084          l_rule_used := '21';
3085 
3086    ELSIF (l_gl_account_source = 'TAX_CODE_ON_INVOICE') AND (l_tax_code_source = 'INVOICE')
3087          AND (l_tax_recoverable_flag = 'N') THEN
3088 
3089          l_rule_used := '22';
3090 
3091    ELSIF (l_gl_account_source = 'TAX_CODE_ON_INVOICE') AND (l_tax_code_source = 'NONE') THEN
3092 
3093          l_rule_used := '23';
3094 
3095    ELSIF (l_gl_account_source = 'TAX_CODE_ON_INVOICE') AND (l_tax_code_source = 'ACTIVITY') THEN
3096 
3097          l_rule_used := '24';
3098 
3099    ELSIF (l_gl_account_source = 'REVENUE_ON_INVOICE') AND (l_tax_code_source = 'INVOICE')
3100          AND (l_tax_recoverable_flag = 'Y') THEN
3101 
3102          l_rule_used := '11';
3103 
3104    ELSIF (l_gl_account_source = 'REVENUE_ON_INVOICE') AND (l_tax_code_source = 'INVOICE')
3105          AND (l_tax_recoverable_flag = 'N') THEN
3106 
3107          l_rule_used := '12';
3108 
3109    ELSIF (l_gl_account_source = 'REVENUE_ON_INVOICE') AND (l_tax_code_source = 'NONE') THEN
3110 
3111          l_rule_used := '13';
3112 
3113    ELSIF (l_gl_account_source = 'REVENUE_ON_INVOICE') AND (l_tax_code_source = 'ACTIVITY') THEN
3114 
3115          l_rule_used := '14';
3116 
3117    END IF;
3118 
3119    l_rule_used := l_rule_used || g_ovrrd_code;
3120 
3121    IF PG_DEBUG in ('Y', 'C') THEN
3122       arp_standard.debug('Doc_Tax_Acct_Rule: ' || 'Rule being used being updated ' || l_rule_used);
3123    END IF;
3124 
3125  /*-------------------------------------------------------------------------------+
3126   | Update the correct bucket with Rule used for a discount or an adjustment      |
3127   +-------------------------------------------------------------------------------*/
3128    IF ((g_ae_doc_rec.source_table = 'RA') AND (p_type_acct = 'ED_ADJ')) THEN
3129 
3130       UPDATE ar_receivable_applications
3131       SET    edisc_tax_acct_rule = l_rule_used
3132       WHERE  receivable_application_id = g_ae_doc_rec.source_id;
3133 
3134    ELSIF ((g_ae_doc_rec.source_table = 'ADJ') AND (p_type_acct = 'ED_ADJ')) THEN
3135          /* Bug 5659539 - When ccid is overriden while creating adjustment thru API,
3136           overriden ccid should be considered, but not from activities setup(adj_code_combination_id)
3137 	This is done by checking if created_from is ADJ_API or not in update stmt. */
3138 
3139          UPDATE ar_adjustments
3140          SET    adj_tax_acct_rule = l_rule_used,
3141                 -- code_combination_id = nvl(adj_code_combination_id, code_combination_id)
3142 		code_combination_id = DECODE(created_from,
3143                                              'ADJ_API', nvl(code_combination_id, adj_code_combination_id),
3144                                              nvl(adj_code_combination_id, code_combination_id)
3145                                             )
3146          WHERE  adjustment_id = g_ae_doc_rec.source_id;
3147 
3148    ELSIF ((g_ae_doc_rec.source_table = 'RA') AND (p_type_acct = 'UNED')) THEN
3149 
3150          UPDATE ar_receivable_applications
3151          SET    unedisc_tax_acct_rule = l_rule_used
3152          WHERE  receivable_application_id = g_ae_doc_rec.source_id;
3153 
3154    END IF;
3155 
3156    IF PG_DEBUG in ('Y', 'C') THEN
3157       arp_standard.debug( 'ARP_ALLOCATION_PKG.Doc_Tax_Acct_Rule()-');
3158    END IF;
3159 
3160 EXCEPTION
3161   WHEN NO_DATA_FOUND THEN
3162      IF PG_DEBUG in ('Y', 'C') THEN
3163         arp_standard.debug('ARP_ALLOCATION_PKG.Doc_Tax_Acct_Rule- NO_DATA_FOUND' );
3164      END IF;
3165      RAISE;
3166 
3167   WHEN OTHERS THEN
3168      IF PG_DEBUG in ('Y', 'C') THEN
3169         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Doc_Tax_Acct_Rule');
3170      END IF;
3171      RAISE;
3172 
3173 END Doc_Tax_Acct_Rule;
3174 
3175 /* ==========================================================================
3176  | PROCEDURE Init_Amts
3177  |
3178  | DESCRIPTION
3179  |      Sets amounts and accounted amount base for line, tax, freight,charges
3180  |      from buckets of the Receivable application or adjustment.
3181  |
3182  | SCOPE - PRIVATE
3183  |
3184  | PARAMETERS
3185  |      p_type_acct          IN  Flag to indicate accounting for Earned
3186  |                               discounts Adjustments or Unearned discounts
3187  *==========================================================================*/
3188 PROCEDURE Init_Amts( p_type_acct    IN VARCHAR2                       ,
3189                      p_app_rec      IN ar_receivable_applications%ROWTYPE,
3190                      p_adj_rec      IN ar_adjustments%ROWTYPE ) IS
3191 
3192 CURSOR chk_tax_chrg(p_cust_id IN NUMBER) IS
3193        select 1
3194        from dual
3195        where exists(select 'x'
3196                     from ar_adjustments
3197                     where type = 'CHARGES'
3198                     and nvl(tax_adjusted,0) <> 0
3199                     and status = 'A'
3200                     and customer_trx_id = p_cust_id);
3201 
3202 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
3203 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
3204 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
3205 l_customer_trx_id NUMBER;
3206 
3207 BEGIN
3208 
3209   IF PG_DEBUG in ('Y', 'C') THEN
3210      arp_standard.debug( 'ARP_ALLOCATION_PKG.Init_Amts()+');
3211   END IF;
3212 
3213 /*---------------------------------------------------------------------------+
3214  | When called from is the Wrapper routine for Bills Receivable, then the    |
3215  | initialization is not required to be done. This is because the bucket     |
3216  | amounts are set by the Wrapper routine                                    |
3217  +---------------------------------------------------------------------------*/
3218     IF (nvl(g_ae_doc_rec.called_from,'NONE') <> 'WRAPPER') THEN
3219 
3220        g_ae_rule_rec.line_amt_alloc            := 0;
3221        g_ae_rule_rec.line_acctd_amt_alloc      := 0;
3222        g_ae_rule_rec.tax_amt_alloc             := 0;
3223        g_ae_rule_rec.tax_acctd_amt_alloc       := 0;
3224        g_ae_rule_rec.freight_amt_alloc         := 0;
3225        g_ae_rule_rec.freight_acctd_amt_alloc   := 0;
3226        g_ae_rule_rec.charges_amt_alloc         := 0;
3227        g_ae_rule_rec.charges_acctd_amt_alloc   := 0;
3228 
3229     END IF;
3230 
3231   g_orig_line_amt_alloc := 0;
3232   g_orig_line_acctd_amt_alloc := 0;
3233 
3234   g_bound_tax      := FALSE;
3235   g_bound_activity := FALSE;
3236   g_added_tax      := FALSE;
3237   g_ovrrd_code     := '';
3238 
3239  /*-------------------------------------------------------------------------------+
3240   | Get Rules so that parent discount or adjustment record can be updated with the|
3241   | Rule details.                                                                 |
3242   +-------------------------------------------------------------------------------*/
3243    Get_Rules(p_type_acct            => p_type_acct,
3244              p_gl_account_source    => l_gl_account_source,
3245              p_tax_code_source      => l_tax_code_source,
3246              p_tax_recoverable_flag => l_tax_recoverable_flag);
3247 
3248 /*------------------------------------------------------------------------------+
3249  | Set amounts and accounted amounts to allocate for Earned discounts           |
3250  +------------------------------------------------------------------------------*/
3251 
3252   IF ((p_type_acct = 'ED_ADJ') AND (g_ae_doc_rec.source_table = 'RA')
3253       AND (nvl(p_app_rec.earned_discount_taken,0) <> 0)) THEN
3254 
3255      g_ae_rule_rec.tax_amt_alloc       := nvl(p_app_rec.tax_ediscounted,0)     * -1;
3256      g_ae_rule_rec.charges_amt_alloc   := nvl(p_app_rec.charges_ediscounted,0) * -1;
3257      g_ae_rule_rec.line_amt_alloc      := nvl(p_app_rec.line_ediscounted,0)    * -1;
3258      g_ae_rule_rec.freight_amt_alloc   := nvl(p_app_rec.freight_ediscounted,0) * -1;
3259 
3260     arp_util.Set_Buckets(
3261                   p_header_acctd_amt   => nvl(p_app_rec.acctd_earned_discount_taken,0) * -1 ,
3262                   p_base_currency      => g_ae_sys_rec.base_currency                        ,
3263                   p_exchange_rate      => g_cust_inv_rec.exchange_rate                      ,
3264                   p_base_precision     => g_ae_sys_rec.base_precision                       ,
3265                   p_base_min_acc_unit  => g_ae_sys_rec.base_min_acc_unit                    ,
3266                   p_tax_amt            => g_ae_rule_rec.tax_amt_alloc                       ,
3267                   p_charges_amt        => g_ae_rule_rec.charges_amt_alloc                   ,
3268                   p_line_amt           => g_ae_rule_rec.line_amt_alloc                      ,
3269                   p_freight_amt        => g_ae_rule_rec.freight_amt_alloc                   ,
3270                   p_tax_acctd_amt      => g_ae_rule_rec.tax_acctd_amt_alloc                 ,
3271                   p_charges_acctd_amt  => g_ae_rule_rec.charges_acctd_amt_alloc             ,
3272                   p_line_acctd_amt     => g_ae_rule_rec.line_acctd_amt_alloc                ,
3273                   p_freight_acctd_amt  => g_ae_rule_rec.freight_acctd_amt_alloc               );
3274 
3275      Dump_Init_Amts(p_type_acct => p_type_acct,
3276                     p_app_rec   => p_app_rec  ,
3277                     p_adj_rec   => p_adj_rec   );
3278 
3279   --Check Accounted Amounts for reconciliation with header accounted amount
3280      IF ((g_ae_rule_rec.line_acctd_amt_alloc  +
3281            g_ae_rule_rec.tax_acctd_amt_alloc     +
3282              g_ae_rule_rec.freight_acctd_amt_alloc  +
3283                 g_ae_rule_rec.charges_acctd_amt_alloc) <> (nvl(p_app_rec.acctd_earned_discount_taken,0) * -1))
3284      THEN
3285 
3286         RAISE rounding_error;
3287 
3288      END IF;
3289 
3290 /*-----------------------------------------------------------------------------------+
3291  | Set amounts and accounted amounts to allocate for Adjustments and Finance charges.|
3292  +-----------------------------------------------------------------------------------*/
3293   ELSIF ((p_type_acct = 'ED_ADJ') AND (g_ae_doc_rec.source_table = 'ADJ')
3294           AND (nvl(p_adj_rec.amount,0) <> 0))  THEN
3295 
3296 /*---------------------------------------------------------------------------+
3297  | When called from the Wrapper routine for Bills Receivable, the            |
3298  | bucket amounts are set by the Wrapper, so its not necessary to set it here|
3299  +---------------------------------------------------------------------------*/
3300      IF (nvl(g_ae_doc_rec.called_from,'NONE') <> 'WRAPPER') THEN
3301 
3302         g_ae_rule_rec.tax_amt_alloc       := nvl(p_adj_rec.tax_adjusted,0);
3303         g_ae_rule_rec.charges_amt_alloc   := nvl(p_adj_rec.receivables_charges_adjusted,0);
3304         g_ae_rule_rec.line_amt_alloc      := nvl(p_adj_rec.line_adjusted,0);
3305         g_ae_rule_rec.freight_amt_alloc   := nvl(p_adj_rec.freight_adjusted,0);
3306 
3307         arp_util.Set_Buckets(
3308                      p_header_acctd_amt   => nvl(p_adj_rec.acctd_amount,0)                     ,
3309                      p_base_currency      => g_ae_sys_rec.base_currency                        ,
3310                      p_exchange_rate      => g_cust_inv_rec.exchange_rate                      ,
3311                      p_base_precision     => g_ae_sys_rec.base_precision                       ,
3312                      p_base_min_acc_unit  => g_ae_sys_rec.base_min_acc_unit                    ,
3313                      p_tax_amt            => g_ae_rule_rec.tax_amt_alloc                       ,
3314                      p_charges_amt        => g_ae_rule_rec.charges_amt_alloc                   ,
3315                      p_line_amt           => g_ae_rule_rec.line_amt_alloc                      ,
3316                      p_freight_amt        => g_ae_rule_rec.freight_amt_alloc                   ,
3317                      p_tax_acctd_amt      => g_ae_rule_rec.tax_acctd_amt_alloc                 ,
3318                      p_charges_acctd_amt  => g_ae_rule_rec.charges_acctd_amt_alloc             ,
3319                      p_line_acctd_amt     => g_ae_rule_rec.line_acctd_amt_alloc                ,
3320                      p_freight_acctd_amt  => g_ae_rule_rec.freight_acctd_amt_alloc               );
3321      END IF;
3322 
3323      Dump_Init_Amts(p_type_acct => p_type_acct,
3324                     p_app_rec   => p_app_rec  ,
3325                     p_adj_rec   => p_adj_rec   );
3326 
3327   --Check Accounted Amounts for reconciliation with header accounted amount
3328      IF ((g_ae_rule_rec.line_acctd_amt_alloc    +
3329              g_ae_rule_rec.tax_acctd_amt_alloc     +
3330                 g_ae_rule_rec.freight_acctd_amt_alloc  +
3331                   g_ae_rule_rec.charges_acctd_amt_alloc) <> nvl(p_adj_rec.acctd_amount,0))
3332      THEN
3333 
3334         RAISE rounding_error;
3335 
3336      END IF;
3337 
3338 /*------------------------------------------------------------------------------+
3339  | Set amounts and accounted amounts to allocate for Earned discounts           |
3340  +------------------------------------------------------------------------------*/
3341   ELSIF ((p_type_acct = 'UNED') AND (g_ae_doc_rec.source_table = 'RA')
3342           AND (nvl(p_app_rec.unearned_discount_taken,0) <> 0)) THEN
3343 
3344      g_ae_rule_rec.tax_amt_alloc       := nvl(p_app_rec.tax_uediscounted,0)     * -1;
3345      g_ae_rule_rec.charges_amt_alloc   := nvl(p_app_rec.charges_uediscounted,0) * -1;
3346      g_ae_rule_rec.line_amt_alloc      := nvl(p_app_rec.line_uediscounted,0)    * -1;
3347      g_ae_rule_rec.freight_amt_alloc   := nvl(p_app_rec.freight_uediscounted,0) * -1;
3348 
3349      arp_util.Set_Buckets(
3350                   p_header_acctd_amt   => nvl(p_app_rec.acctd_unearned_discount_taken,0) * -1 ,
3351                   p_base_currency      => g_ae_sys_rec.base_currency                          ,
3352                   p_exchange_rate      => g_cust_inv_rec.exchange_rate                        ,
3353                   p_base_precision     => g_ae_sys_rec.base_precision                         ,
3354                   p_base_min_acc_unit  => g_ae_sys_rec.base_min_acc_unit                      ,
3355                   p_tax_amt            => g_ae_rule_rec.tax_amt_alloc                         ,
3356                   p_charges_amt        => g_ae_rule_rec.charges_amt_alloc                     ,
3357                   p_line_amt           => g_ae_rule_rec.line_amt_alloc                        ,
3358                   p_freight_amt        => g_ae_rule_rec.freight_amt_alloc                     ,
3359                   p_tax_acctd_amt      => g_ae_rule_rec.tax_acctd_amt_alloc                   ,
3360                   p_charges_acctd_amt  => g_ae_rule_rec.charges_acctd_amt_alloc               ,
3361                   p_line_acctd_amt     => g_ae_rule_rec.line_acctd_amt_alloc                  ,
3362                   p_freight_acctd_amt  => g_ae_rule_rec.freight_acctd_amt_alloc                 );
3363 
3364      Dump_Init_Amts(p_type_acct => p_type_acct,
3365                     p_app_rec   => p_app_rec  ,
3366                     p_adj_rec   => p_adj_rec   );
3367 
3368   --Check Accounted Amounts for reconciliation with header accounted amount
3369      IF ((g_ae_rule_rec.line_acctd_amt_alloc  +
3370             g_ae_rule_rec.tax_acctd_amt_alloc    +
3371               g_ae_rule_rec.freight_acctd_amt_alloc +
3372                 g_ae_rule_rec.charges_acctd_amt_alloc) <> nvl(p_app_rec.acctd_unearned_discount_taken,0) * -1)
3373      THEN
3374 
3375         RAISE rounding_error;
3376 
3377      END IF;
3378 /*------------------------------------------------------------------------------+
3379  | Set amounts and accounted amounts to allocate for Payments for deferred tax  |
3380  +------------------------------------------------------------------------------*/
3381   ELSIF (((p_type_acct = 'PAY') AND (g_ae_def_tax) AND (NOT g_done_def_tax))
3382            AND (nvl(p_app_rec.amount_applied,0) <> 0)) THEN
3383   /*---------------------------------------------------------------------------+
3384    | Only the tax applied is used to move deferred tax from interim to         |
3385    | collected tax account. When called from the Wrapper routine for Bills     |
3386    | Receivable, the bucket amounts are set by the Wrapper, so its not         |
3387    | necessary to set it here                                                  |
3388    +---------------------------------------------------------------------------*/
3389      IF (nvl(g_ae_doc_rec.called_from,'NONE') <> 'WRAPPER') THEN
3390 
3391         g_ae_rule_rec.tax_amt_alloc         := nvl(p_app_rec.tax_applied,0)                 * -1;
3392         g_ae_rule_rec.charges_amt_alloc     := nvl(p_app_rec.receivables_charges_applied,0) * -1;
3393         g_ae_rule_rec.line_amt_alloc        := nvl(p_app_rec.line_applied,0)                * -1;
3394         g_ae_rule_rec.freight_amt_alloc     := nvl(p_app_rec.freight_applied,0)             * -1;
3395 
3396          arp_util.Set_Buckets(
3397                        p_header_acctd_amt   => nvl(p_app_rec.acctd_amount_applied_to,0)       * -1 ,
3398                        p_base_currency      => g_ae_sys_rec.base_currency                          ,
3399                        p_exchange_rate      => g_cust_inv_rec.exchange_rate                        ,
3400                        p_base_precision     => g_ae_sys_rec.base_precision                         ,
3401                        p_base_min_acc_unit  => g_ae_sys_rec.base_min_acc_unit                      ,
3402                        p_tax_amt            => g_ae_rule_rec.tax_amt_alloc                         ,
3403                        p_charges_amt        => g_ae_rule_rec.charges_amt_alloc                     ,
3404                        p_line_amt           => g_ae_rule_rec.line_amt_alloc                        ,
3405                        p_freight_amt        => g_ae_rule_rec.freight_amt_alloc                     ,
3406                        p_tax_acctd_amt      => g_ae_rule_rec.tax_acctd_amt_alloc                   ,
3407                        p_charges_acctd_amt  => g_ae_rule_rec.charges_acctd_amt_alloc               ,
3408                        p_line_acctd_amt     => g_ae_rule_rec.line_acctd_amt_alloc                  ,
3409                        p_freight_acctd_amt  => g_ae_rule_rec.freight_acctd_amt_alloc                 );
3410       END IF;
3411 
3412      Dump_Init_Amts(p_type_acct => p_type_acct,
3413                     p_app_rec   => p_app_rec  ,
3414                     p_adj_rec   => p_adj_rec   );
3415 
3416   --Check Accounted Amounts for reconciliation with header accounted amount
3417      IF ((g_ae_rule_rec.line_acctd_amt_alloc   +
3418             g_ae_rule_rec.tax_acctd_amt_alloc   +
3419               g_ae_rule_rec.freight_acctd_amt_alloc   +
3420                 g_ae_rule_rec.charges_acctd_amt_alloc  )       <> nvl(p_app_rec.acctd_amount_applied_to,0) * -1)
3421      THEN
3422 
3423         RAISE rounding_error;
3424 
3425      END IF;
3426 
3427   END IF;
3428 
3429   IF PG_DEBUG in ('Y', 'C') THEN
3430      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.line_amt_alloc ' || g_ae_rule_rec.line_amt_alloc);
3431      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.line_acctd_amt_alloc ' || g_ae_rule_rec.line_acctd_amt_alloc);
3432      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.tax_amt_alloc ' || g_ae_rule_rec.tax_amt_alloc);
3433      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.tax_acctd_amt_alloc ' || g_ae_rule_rec.tax_acctd_amt_alloc);
3434      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.freight_amt_alloc ' || g_ae_rule_rec.freight_amt_alloc);
3435      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.freight_acctd_amt_alloc ' || g_ae_rule_rec.freight_acctd_amt_alloc);
3436      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.charges_amt_alloc ' || g_ae_rule_rec.charges_amt_alloc);
3437      arp_standard.debug('Init_Amts: ' || 'g_ae_rule_rec.charges_acctd_amt_alloc ' || g_ae_rule_rec.charges_acctd_amt_alloc);
3438   END IF;
3439 
3440 /*------------------------------------------------------------------------------+
3441  | If atleast one charges adjustment exists then add the charge to the line     |
3442  | it as the taxable amount.                                                    |
3443  +------------------------------------------------------------------------------*/
3444  --Get the customer trx id
3445    IF g_ae_doc_rec.source_table = 'RA' THEN
3446       l_customer_trx_id := p_app_rec.applied_customer_trx_id;
3447    ELSE
3448       l_customer_trx_id := p_adj_rec.customer_trx_id;
3449    END IF;
3450 
3451  --Check whether atleast one charges adjustment with tax exists
3452    FOR l_chk_tax_chrg IN chk_tax_chrg(p_cust_id => l_customer_trx_id) LOOP
3453 
3454     --add the charge to line to have a common taxable basis
3455        g_ae_rule_rec.line_amt_alloc := g_ae_rule_rec.line_amt_alloc
3456                                            + g_ae_rule_rec.charges_amt_alloc;
3457 
3458        g_ae_rule_rec.line_acctd_amt_alloc := g_ae_rule_rec.line_acctd_amt_alloc
3459                                                + g_ae_rule_rec.charges_acctd_amt_alloc;
3460 
3461     --set the charge to zero
3462        g_ae_rule_rec.charges_amt_alloc := 0;
3463 
3464        g_ae_rule_rec.charges_acctd_amt_alloc := 0;
3465 
3466       IF PG_DEBUG in ('Y', 'C') THEN
3467          arp_standard.debug('Init_Amts: ' || 'Tax inclusive charges adjustments exist - charge added to line');
3468       END IF;
3469 
3470    END LOOP; --tax inclusive charges
3471 
3472    g_orig_line_amt_alloc := g_ae_rule_rec.line_amt_alloc;
3473    g_orig_line_acctd_amt_alloc := g_ae_rule_rec.line_acctd_amt_alloc;
3474 
3475 /*------------------------------------------------------------------------------+
3476  | Boundary conditions in this case we cant allocate the tax amount or accounted|
3477  | amount over the original tax on Invoice and then add to the revenue for that |
3478  | tax due to TAX_CODE_SOURCE = NONE, hence tax is added to line and allocated. |
3479  | Boundary condition flag is set. Note in this case we do not override the rule|
3480  | When the boundary flag is set the tax link id will be null and taxable for   |
3481  | tax allocations will be derived from the original Invoice                    |
3482  +------------------------------------------------------------------------------*/
3483   IF (((g_ae_rule_rec.tax_amt_alloc <> 0) AND (g_ae_rule_rec.tax_amt = 0))
3484         OR ((g_ae_rule_rec.tax_acctd_amt_alloc <> 0) AND (g_ae_rule_rec.tax_acctd_amt = 0))) THEN
3485 
3486      g_bound_tax := TRUE; --applicable to ACTIVITY also hence set here
3487 
3488      IF (l_tax_code_source IN ('NONE', 'INVOICE')) THEN
3489 
3490          IF (((p_type_acct = 'ED_ADJ') OR (p_type_acct = 'PAY')) AND (l_tax_code_source =  'INVOICE')) THEN
3491 
3492          /* Override the rule may want to use this in the future
3493             g_ae_rule_rec.tax_code_source1   := 'NONE';          */
3494 
3495            g_ovrrd_code := '1';
3496 
3497            IF (p_type_acct = 'PAY') THEN
3498               g_ae_rule_rec.tax_code_source1      := 'NONE';
3499               g_ae_rule_rec.tax_recoverable_flag1 := '';
3500            ELSE
3501               RAISE invalid_allocation_base;
3502            END IF;
3503 
3504          ELSIF ((p_type_acct = 'UNED') AND (l_tax_code_source =  'INVOICE')) THEN
3505 
3506          /* Override the rule may want to use this in the future
3507             g_ae_rule_rec.tax_code_source2   := 'NONE';          */
3508 
3509            g_ovrrd_code := '1'; --indicates rules overriden
3510 
3511            RAISE invalid_allocation_base;
3512 
3513          END IF; --end if type of account and tax code source is Invoice
3514 
3515          g_ae_rule_rec.line_amt_alloc       := g_ae_rule_rec.line_amt_alloc
3516                                                                + g_ae_rule_rec.tax_amt_alloc;
3517          g_ae_rule_rec.line_acctd_amt_alloc := g_ae_rule_rec.line_acctd_amt_alloc
3518                                                                + g_ae_rule_rec.tax_acctd_amt_alloc;
3519          g_added_tax := TRUE;
3520 
3521          IF (g_ovrrd_code IS NULL) THEN
3522             g_ovrrd_code := '2'; --indicates tax added to line
3523          END IF;
3524 
3525      END IF; --end if tax code source is Invoice or None
3526 
3527   END IF; --end if tax base is 0 on original Invoice
3528 
3529 
3530 /*-----------------------------------------------------------------------------------+
3531  | Boundary conditions for activity gl account - gl account source in this case we do|
3532  | not override the rule we just set the boundary flag the tax code source rule will |
3533  | be used as is unless overriden by one of the above conditions. In such a case the |
3534  | link id for tax will be null, and taxable derived from original Invoice.          |
3535  +-----------------------------------------------------------------------------------*/
3536   IF ((((g_ae_rule_rec.line_amt_alloc + g_ae_rule_rec.freight_amt_alloc + g_ae_rule_rec.charges_amt_alloc) <> 0)
3537        AND (g_ae_rule_rec.revenue_amt = 0))
3538         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)
3539              AND (g_ae_rule_rec.revenue_acctd_amt = 0))) THEN
3540 
3541      g_bound_activity := TRUE; --set boundary condition for activity
3542 
3543   /*-----------------------------------------------------------------------------------+
3544    |Add the tax to line bucket so that allocation can be done in the case the procedure|
3545    |Allocate_Tax_To_Rev will not be used as the revenue base on original Invoice is 0  |
3546    +-----------------------------------------------------------------------------------*/
3547      IF ((l_tax_code_source = 'NONE') AND (NOT g_bound_tax)) THEN
3548 
3549         g_ae_rule_rec.line_amt_alloc       := g_ae_rule_rec.line_amt_alloc + g_ae_rule_rec.tax_amt_alloc;
3550         g_ae_rule_rec.line_acctd_amt_alloc := g_ae_rule_rec.line_acctd_amt_alloc + g_ae_rule_rec.tax_acctd_amt_alloc;
3551         g_added_tax := TRUE;
3552 
3553         IF (g_ovrrd_code IS NULL) THEN
3554            g_ovrrd_code := '2';  --indicates tax added to line
3555         END IF;
3556 
3557      END IF; --end if tax code source is NONE
3558 
3559   /*-------------------------------------------------------------------------------------+
3560    |Override the rule for gl account source as line amount or accounted amount cannot be |
3561    |allocated uniformly over revenue amount or accounted amount base on original Invoice.|
3562    +-------------------------------------------------------------------------------------*/
3563      IF (l_gl_account_source IN ('REVENUE_ON_INVOICE', 'TAX_CODE_ON_INVOICE')) THEN
3564 
3565         IF ((p_type_acct = 'ED_ADJ') OR (p_type_acct = 'PAY')) THEN
3566 
3567            /* override the rule on GL Account Source kept for future usage most logical setting
3568               g_ae_rule_rec.gl_account_source1 := 'ACTIVITY_GL_ACCOUNT'; */
3569 
3570            RAISE invalid_allocation_base;
3571 
3572         ELSIF (p_type_acct = 'UNED') THEN
3573 
3574            /* override the rule on GL Account Source kept for future usage most logical setting
3575               g_ae_rule_rec.gl_account_source2 := 'ACTIVITY_GL_ACCOUNT'; */
3576 
3577            RAISE invalid_allocation_base;
3578 
3579         END IF;
3580 
3581         IF (g_ovrrd_code = '1') THEN
3582             null; --dont set code again
3583         ELSIF (g_ovrrd_code = '2') THEN
3584             g_ovrrd_code := 3; --indicates tax added to line and gl account source rule overriden
3585         ELSIF g_ovrrd_code IS NULL THEN
3586             g_ovrrd_code := 4; --indicates gl account source overriden
3587         END IF;
3588 
3589      END IF; --end if revenue on invoice or tax code on invoice
3590 
3591   END IF; --end if revenue amount or accounted amount base is 0
3592 
3593 
3594   IF PG_DEBUG in ('Y', 'C') THEN
3595      arp_standard.debug( 'ARP_ALLOCATION_PKG.Init_Amts()-');
3596   END IF;
3597 
3598 EXCEPTION
3599   WHEN rounding_error THEN
3600        IF PG_DEBUG in ('Y', 'C') THEN
3601           arp_standard.debug('Rounding error: ARP_ALLOCATION_PKG.Init_Amts');
3602        END IF;
3603        fnd_message.set_name('AR','AR_ROUNDING_ERROR');
3604        fnd_message.set_token('ROUTINE','ARP_ALLOCATION_PKG.INIT_AMTS');
3605        RAISE;
3606 
3607   WHEN OTHERS THEN
3608      IF PG_DEBUG in ('Y', 'C') THEN
3609         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Init_Amts');
3610      END IF;
3611      RAISE;
3612 
3613 END Init_Amts;
3614 
3615 /* ==========================================================================
3616  | PROCEDURE Gross_To_Activity_GL
3617  |
3618  | DESCRIPTION
3619  |      This routine creates the standard Gross to Activity GL Account
3620  |      accounting entry.
3621  |
3622  | SCOPE - PRIVATE
3623  |
3624  | PARAMETERS
3625  |      NONE
3626  *==========================================================================*/
3627 PROCEDURE Gross_To_Activity_GL(p_type_acct      IN VARCHAR2) IS
3628 
3629 l_ae_line_init_rec     ar_ae_alloc_rec_gt%ROWTYPE;
3630 
3631 BEGIN
3632 
3633   IF PG_DEBUG in ('Y', 'C') THEN
3634      arp_standard.debug('ARP_ALLOCATION_PKG.Gross_To_Activity_GL()+');
3635   END IF;
3636 
3637  /*----------------------------------------------------------------------------+
3638   | Assign Currency Exchange rate information to initialisation record         |
3639   +----------------------------------------------------------------------------*/
3640    l_ae_line_init_rec.ae_source_id                 := g_ae_doc_rec.source_id               ;
3641    l_ae_line_init_rec.ae_source_table              := g_ae_doc_rec.source_table            ;
3642    l_ae_line_init_rec.ae_currency_code             := g_cust_inv_rec.invoice_currency_code ;
3643    l_ae_line_init_rec.ae_currency_conversion_rate  := g_cust_inv_rec.exchange_rate         ;
3644    l_ae_line_init_rec.ae_currency_conversion_type  := g_cust_inv_rec.exchange_rate_type    ;
3645    l_ae_line_init_rec.ae_currency_conversion_date  := g_cust_inv_rec.exchange_date         ;
3646 
3647    IF (g_cust_inv_rec.drawee_site_use_id IS NOT NULL) THEN --if Bill
3648       l_ae_line_init_rec.ae_third_party_id            := g_cust_inv_rec.drawee_id;
3649       l_ae_line_init_rec.ae_third_party_sub_id        := g_cust_inv_rec.drawee_site_use_id  ;
3650    ELSE
3651       l_ae_line_init_rec.ae_third_party_id            := g_cust_inv_rec.bill_to_customer_id   ;
3652       l_ae_line_init_rec.ae_third_party_sub_id        := g_cust_inv_rec.bill_to_site_use_id   ;
3653    END IF;
3654 
3655  /*----------------------------------------------------------------------------+
3656   | Build accounting for any Charges and Freight amounts to activity GL account|
3657   +----------------------------------------------------------------------------*/
3658    Build_Charges_Freight_All(p_type_acct        => p_type_acct       ,
3659                              p_ae_line_init_rec => l_ae_line_init_rec,
3660                              p_build_all        => TRUE   );
3661 
3662    IF PG_DEBUG in ('Y', 'C') THEN
3663       arp_standard.debug( 'ARP_ALLOCATION_PKG.Gross_To_Activity_GL()-');
3664    END IF;
3665 
3666 EXCEPTION
3667   WHEN OTHERS THEN
3668      IF PG_DEBUG in ('Y', 'C') THEN
3669         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Gross_To_Activity_GL');
3670      END IF;
3671      RAISE;
3672 
3673 END Gross_To_Activity_GL;
3674 
3675 /* ==========================================================================
3676  | PROCEDURE Init_Rev_Tax_Tab
3677  |
3678  | DESCRIPTION
3679  |      Initialise cells in tables which are used for calculations, this is
3680  |      required because an application may require allocation of discounts
3681  |      for earned discounts and then for unearned discounts
3682  |
3683  | SCOPE - PRIVATE
3684  |
3685  | PARAMETERS
3686  |      NONE
3687  *==========================================================================*/
3688 PROCEDURE Init_Rev_Tax_Tab IS
3689 
3690 l_ctr                 BINARY_INTEGER;
3691 
3692 BEGIN
3693   arp_standard.debug('ARP_ALLOCATION_PKG.Init_Rev_Tax_Tab()+');
3694 
3695 /*------------------------------------------------------------------------------+
3696  | Set the prorate amount and summarized flags to null because they may have    |
3697  | been set for earned discounts.                                               |
3698  +------------------------------------------------------------------------------*/
3699 
3700   UPDATE /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3)*/
3701         ar_ae_alloc_rec_gt
3702   SET ae_pro_amt                     = 0  ,
3703       ae_pro_acctd_amt               = 0  ,
3704       ae_pro_frt_chrg_amt            = 0  ,
3705       ae_pro_frt_chrg_acctd_amt      = 0  ,
3706       ae_pro_taxable_amt             = 0  ,
3707       ae_pro_taxable_acctd_amt       = 0  ,
3708       ae_pro_split_taxable_amt       = '' ,
3709       ae_pro_split_taxable_acctd_amt = '' ,
3710       ae_pro_recov_taxable_amt       = '' ,
3711       ae_pro_recov_taxable_acctd_amt = '' ,
3712       ae_pro_def_tax_amt             = 0  ,
3713       ae_pro_def_tax_acctd_amt       = 0  ,
3714       ae_sum_alloc_amt               = 0  ,
3715       ae_sum_alloc_acctd_amt         = 0  ,
3716       ae_tax_link_id_act             = '' ,
3717       ae_counted_flag                = 'N'
3718    WHERE ae_id = g_id;
3719 
3720   arp_standard.debug( 'ARP_ALLOCATION_PKG.Init_Rev_Tax_Tab()-');
3721 
3722 EXCEPTION
3723   WHEN OTHERS THEN
3724      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Init_Rev_Tax_Tab');
3725      RAISE;
3726 
3727 END Init_Rev_Tax_Tab;
3728 
3729 /* ==========================================================================
3730  | FUNCTION Get_Acctd_Amt
3731  |
3732  | DESCRIPTION
3733  |      Returns accounted amount using invoice exchange rate converting to
3734  |      base currency.
3735  |
3736  | SCOPE - PRIVATE
3737  |
3738  | PARAMETERS
3739  |      p_amount             IN  Amount which needs to be converted into
3740  |                               base or functional currency accounted amount
3741  *==========================================================================*/
3742 FUNCTION  Get_Acctd_Amt(p_amount       IN NUMBER ) RETURN NUMBER IS
3743 
3744 l_acctd_amount NUMBER;
3745 
3746 BEGIN
3747 
3748   IF PG_DEBUG in ('Y', 'C') THEN
3749      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Acctd_Amt()-');
3750   END IF;
3751 
3752   l_acctd_amount := arpcurr.functional_amount(p_amount                                   ,
3753                                               g_ae_sys_rec.base_currency                 ,
3754                                               g_cust_inv_rec.exchange_rate               ,
3755                                               g_ae_sys_rec.base_precision                ,
3756                                               g_ae_sys_rec.base_min_acc_unit               );
3757 
3758   IF PG_DEBUG in ('Y', 'C') THEN
3759      arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Acctd_Amt()-');
3760   END IF;
3761 
3762   RETURN l_acctd_amount ;
3763 
3764 EXCEPTION
3765   WHEN OTHERS THEN
3766      IF PG_DEBUG in ('Y', 'C') THEN
3767         arp_standard.debug('Get_Acctd_Amt: ' || 'EXCEPTION: ARP_ALLOCATION_PKG.Init_Amts');
3768      END IF;
3769      RAISE;
3770 
3771 END Get_Acctd_Amt;
3772 
3773 /* ==========================================================================
3774  | PROCEDURE Alloc_Rev_Tax_Amt
3775  |
3776  | DESCRIPTION
3777  |      Allocate Tax amount over tax distributions for tax lines
3778  |
3779  | SCOPE - PRIVATE
3780  |
3781  | PARAMETERS
3782  |       None
3783  *==========================================================================*/
3784 PROCEDURE Alloc_Rev_Tax_Amt(p_type_acct IN VARCHAR2) IS
3785 
3786 --Rev variables
3787 l_rev_run_amt_tot             NUMBER := 0    ;
3788 l_rev_run_acctd_amt_tot       NUMBER := 0    ;
3789 l_rev_run_pro_amt_tot         NUMBER := 0    ;
3790 l_rev_run_pro_acctd_amt_tot   NUMBER := 0    ;
3791 
3792 --Frt, chrg variables
3793 l_frt_run_pro_amt_tot         NUMBER := 0    ;
3794 l_frt_run_pro_acctd_amt_tot   NUMBER := 0    ;
3795 
3796 --Tax variables
3797 l_ctr                        BINARY_INTEGER;
3798 l_last_tax                   BINARY_INTEGER := 0;
3799 l_last_def_tax               BINARY_INTEGER := 0;
3800 l_tax_run_amt_tot            NUMBER := 0;
3801 l_tax_run_acctd_amt_tot      NUMBER := 0;
3802 l_tax_run_pro_amt_tot        NUMBER := 0;
3803 l_tax_run_pro_acctd_amt_tot  NUMBER := 0;
3804 l_def_tax_run_amt_tot        NUMBER := 0;
3805 l_def_tax_run_acctd_amt_tot  NUMBER := 0;
3806 l_pro_def_tax_run_amt        NUMBER := 0;
3807 l_pro_def_tax_run_acctd_amt  NUMBER := 0;
3808 l_tax_applied                NUMBER := 0;
3809 l_tax_acctd_applied          NUMBER := 0;
3810 l_rowid                      VARCHAR2(50);
3811 
3812 TYPE g_ae_alloc_type IS RECORD (
3813      l_rowid                   DBMS_SQL.VARCHAR2_TABLE,
3814      ae_account_class          DBMS_SQL.VARCHAR2_TABLE,
3815      ae_amount                 DBMS_SQL.NUMBER_TABLE,
3816      ae_acctd_amount           DBMS_SQL.NUMBER_TABLE,
3817      ae_pro_amt                DBMS_SQL.NUMBER_TABLE,
3818      ae_pro_acctd_amt          DBMS_SQL.NUMBER_TABLE,
3819      ae_taxable_amount         DBMS_SQL.NUMBER_TABLE,
3820      ae_taxable_acctd_amount   DBMS_SQL.NUMBER_TABLE,
3821      ae_pro_frt_chrg_amt       DBMS_SQL.NUMBER_TABLE,
3822      ae_pro_frt_chrg_acctd_amt DBMS_SQL.NUMBER_TABLE,
3823      ae_pro_taxable_amt        DBMS_SQL.NUMBER_TABLE,
3824      ae_pro_taxable_acctd_amt  DBMS_SQL.NUMBER_TABLE,
3825      ae_collected_tax_ccid     DBMS_SQL.NUMBER_TABLE,
3826      ae_pro_def_tax_amt        DBMS_SQL.NUMBER_TABLE,
3827      ae_pro_def_tax_acctd_amt  DBMS_SQL.NUMBER_TABLE
3828 );
3829 
3830 g_ae_alloc_rev_tax_tbl       g_ae_alloc_type;
3831 
3832 CURSOR l_rev_tax_cur IS
3833 SELECT /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
3834        rowid,
3835        ae_account_class,
3836        ae_amount,
3837        ae_acctd_amount,
3838        ae_pro_amt,
3839        ae_pro_acctd_amt,
3840        ae_taxable_amount,
3841        ae_taxable_acctd_amount,
3842        ae_pro_taxable_amt,
3843        ae_pro_taxable_acctd_amt,
3844        ae_pro_frt_chrg_amt,
3845        ae_pro_frt_chrg_acctd_amt,
3846        ae_collected_tax_ccid,
3847        ae_pro_def_tax_amt,
3848        ae_pro_def_tax_acctd_amt
3849 FROM ar_ae_alloc_rec_gt
3850 WHERE ae_id = g_id
3851 AND ae_account_class IS NOT NULL
3852 ORDER BY ae_account_class, ae_customer_trx_line_id;
3853 
3854 l_process_rev      BOOLEAN;
3855 l_process_frt      BOOLEAN;
3856 l_process_tax      BOOLEAN;
3857 l_last_fetch       BOOLEAN;
3858 BEGIN
3859 
3860    IF PG_DEBUG in ('Y', 'C') THEN
3861       arp_standard.debug( 'ARP_ALLOCATION_PKG.Alloc_Rev_Tax_Amt()+');
3862    END IF;
3863 
3864 /*------------------------------------------------------------------------------+
3865  | Allocate discount, adjustment, finance charge amounts over revenue. The check|
3866  | below is required because for PA it appears that amount could be zero but    |
3867  | accounted amount could be present. The revenue amount is allocated only if   |
3868  | there is a valid non zero revenue base to allocate otherwise the line amount |
3869  | would be allocated to the Activity GL account                                |
3870  +------------------------------------------------------------------------------*/
3871    l_process_rev := FALSE;
3872 
3873 /*------------------------------------------------------------------------------+
3874  | Set process revenue flag                                                     |
3875  +------------------------------------------------------------------------------*/
3876    IF (((g_ae_rule_rec.line_amt_alloc <> 0) AND (g_ae_rule_rec.revenue_amt <> 0))
3877        OR ((g_ae_rule_rec.line_acctd_amt_alloc <> 0) AND (g_ae_rule_rec.revenue_acctd_amt <> 0)))
3878       AND (g_ae_rev_ctr > 0) THEN
3879 
3880        l_process_rev := TRUE;
3881        IF PG_DEBUG in ('Y', 'C') THEN
3882           arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'Process Revenue is True ');
3883        END IF;
3884 
3885    END IF;
3886 
3887    l_process_frt := FALSE;
3888 
3889    IF ((((g_ae_rule_rec.freight_amt_alloc + g_ae_rule_rec.charges_amt_alloc) <> 0)
3890       AND (g_ae_rule_rec.revenue_amt <> 0))
3891       OR
3892       (((g_ae_rule_rec.freight_acctd_amt_alloc + g_ae_rule_rec.charges_acctd_amt_alloc) <> 0)
3893         AND (g_ae_rule_rec.revenue_acctd_amt <> 0))) AND (g_ae_rev_ctr > 0) THEN
3894       IF PG_DEBUG in ('Y', 'C') THEN
3895          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'Process Freight is True ');
3896       END IF;
3897       l_process_frt := TRUE;
3898 
3899    END IF;
3900 
3901    l_process_tax := FALSE;
3902 
3903 /*------------------------------------------------------------------------------+
3904  | Set process tax flag                                                         |
3905  +------------------------------------------------------------------------------*/
3906    IF (((g_ae_rule_rec.tax_amt_alloc <> 0) OR (g_ae_rule_rec.tax_acctd_amt_alloc <> 0))
3907        OR ((g_ae_rule_rec.line_amt_alloc <> 0) OR (g_ae_rule_rec.line_acctd_amt_alloc <> 0)))
3908       AND (g_ae_tax_ctr > 0) THEN
3909 
3910       l_process_tax := TRUE;
3911       IF PG_DEBUG in ('Y', 'C') THEN
3912          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'Process Tax is True ');
3913       END IF;
3914 
3915    END IF;
3916 
3917 /*------------------------------------------------------------------------------+
3918  | Set tax applied and accounted and allocate over tax, this is done using a    |
3919  | RATE which is determined based on the total deferred tax over the actual tax |
3920  | The arpcurr round function is used to round to precision for curency of      |
3921  | Invoice and functional currency.                                             |
3922  +------------------------------------------------------------------------------*/
3923    IF ((g_ae_def_tax) AND (NOT g_done_def_tax) AND (p_type_acct = 'PAY'))
3924       AND l_process_tax THEN
3925 
3926       IF g_ae_rule_rec.tax_amt <> 0 THEN --prevent zero divide
3927 
3928          l_tax_applied       := arpcurr.CurrRound(g_ae_rule_rec.tax_amt_alloc    *
3929                                    g_ae_rule_rec.def_tax_amt/g_ae_rule_rec.tax_amt,
3930                                    g_cust_inv_rec.invoice_currency_code);
3931       END IF; --amt not zero
3932 
3933       IF g_ae_rule_rec.tax_acctd_amt <> 0 THEN --prevent zero divide
3934 
3935          l_tax_acctd_applied := arpcurr.CurrRound(g_ae_rule_rec.tax_acctd_amt_alloc   *
3936                                  g_ae_rule_rec.def_tax_acctd_amt/g_ae_rule_rec.tax_acctd_amt,
3937                                  g_ae_sys_rec.base_currency);
3938       END IF; --accounted amt not zero
3939 
3940    END IF; --process Tax
3941 
3942  /*------------------------------------------------------------------------------+
3943   | Loop through tax to allocate tax on discounts and adjustments payments       |
3944   +------------------------------------------------------------------------------*/
3945   IF l_process_rev OR l_process_frt OR l_process_tax THEN
3946 
3947      OPEN l_rev_tax_cur;
3948 
3949      LOOP
3950 
3951       FETCH l_rev_tax_cur BULK COLLECT INTO
3952             g_ae_alloc_rev_tax_tbl.l_rowid,
3953             g_ae_alloc_rev_tax_tbl.ae_account_class,
3954             g_ae_alloc_rev_tax_tbl.ae_amount,
3955             g_ae_alloc_rev_tax_tbl.ae_acctd_amount,
3956             g_ae_alloc_rev_tax_tbl.ae_pro_amt,
3957             g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt,
3958             g_ae_alloc_rev_tax_tbl.ae_taxable_amount,
3959             g_ae_alloc_rev_tax_tbl.ae_taxable_acctd_amount,
3960             g_ae_alloc_rev_tax_tbl.ae_pro_taxable_amt,
3961             g_ae_alloc_rev_tax_tbl.ae_pro_taxable_acctd_amt,
3962             g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt,
3963             g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt,
3964             g_ae_alloc_rev_tax_tbl.ae_collected_tax_ccid,
3965             g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt,
3966             g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt
3967        LIMIT g_bulk_fetch_rows;
3968 
3969        IF l_rev_tax_cur%NOTFOUND THEN
3970           l_last_fetch := TRUE;
3971        END IF;
3972 
3973        IF (g_ae_alloc_rev_tax_tbl.l_rowid.COUNT = 0) AND (l_last_fetch) THEN
3974          IF PG_DEBUG in ('Y', 'C') THEN
3975             arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'COUNT = 0 and LAST FETCH ');
3976          END IF;
3977          EXIT;
3978        END IF;
3979 
3980     FOR i IN g_ae_alloc_rev_tax_tbl.l_rowid.FIRST .. g_ae_alloc_rev_tax_tbl.l_rowid.LAST LOOP
3981 
3982       IF ((l_process_rev) OR (l_process_frt)) AND g_ae_alloc_rev_tax_tbl.ae_account_class(i) = 'REVEARN' THEN
3983 
3984       /*------------------------------------------------------------------------------+
3985        | Maintain running total amounts for Revenue amounts and accounted amounts, set|
3986        | cached flag.
3987        +------------------------------------------------------------------------------*/
3988           l_rev_run_amt_tot       := l_rev_run_amt_tot + g_ae_alloc_rev_tax_tbl.ae_amount(i);
3989           l_rev_run_acctd_amt_tot := l_rev_run_acctd_amt_tot + g_ae_alloc_rev_tax_tbl.ae_acctd_amount(i);
3990 
3991         /*------------------------------------------------------------------------------+
3992          | Allocate revenue for discount or adjustments to each revenue line in Invoice |
3993          | currency. Rev lines 10, 20, 30, 40, Rev Total 100, Discount 10               |
3994          | Line 1  a -> 10 * 10/100  = 1 (allocated)                                    |
3995          |                                                                              |
3996          | Line 2    -> (10 + 20)/100 * 10 = 3                                          |
3997          |         b -> 3 - a = 2 (allocated)                                           |
3998          |                                                                              |
3999          | Line 3    -> (10 + 20 + 30) * 10/100 = 6                                     |
4000          |         c -> 6 - a - b = 3                                                   |
4001          | Line .....                                                                   |
4002          +------------------------------------------------------------------------------*/
4003          IF g_ae_rule_rec.revenue_amt <> 0 THEN
4004 
4005           /*------------------------------------------------------------------------------+
4006            | Process line amounts                                                         |
4007            +------------------------------------------------------------------------------*/
4008             IF l_process_rev THEN
4009             g_ae_alloc_rev_tax_tbl.ae_pro_amt(i) :=
4010             arpcurr.CurrRound(l_rev_run_amt_tot / g_ae_rule_rec.revenue_amt * g_ae_rule_rec.line_amt_alloc,
4011                               g_cust_inv_rec.invoice_currency_code) - l_rev_run_pro_amt_tot;
4012 
4013             IF PG_DEBUG in ('Y', 'C') THEN
4014                arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_amt = '||
4015                                 g_ae_alloc_rev_tax_tbl.ae_pro_amt(i));
4016             END IF;
4017 
4018           /*------------------------------------------------------------------------------+
4019            | Running total for prorated Revenue amount in currency of Invoice             |
4020            +------------------------------------------------------------------------------*/
4021             l_rev_run_pro_amt_tot := l_rev_run_pro_amt_tot
4022                                          + g_ae_alloc_rev_tax_tbl.ae_pro_amt(i);
4023             END IF;
4024 
4025           /*------------------------------------------------------------------------------+
4026            | Process Freight and charges                                                  |
4027            +------------------------------------------------------------------------------*/
4028             IF l_process_frt THEN
4029             g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt(i) :=
4030             arpcurr.CurrRound(l_rev_run_amt_tot / g_ae_rule_rec.revenue_amt *
4031                               (g_ae_rule_rec.freight_amt_alloc + g_ae_rule_rec.charges_amt_alloc),
4032                               g_cust_inv_rec.invoice_currency_code) - l_frt_run_pro_amt_tot;
4033 
4034             IF PG_DEBUG in ('Y', 'C') THEN
4035                arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt('||i||') = '||
4036                                 g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt(i));
4037             END IF;
4038 
4039           /*------------------------------------------------------------------------------+
4040            | Running total for prorated Freight amount in currency of Invoice             |
4041            +------------------------------------------------------------------------------*/
4042             l_frt_run_pro_amt_tot := l_frt_run_pro_amt_tot
4043                                          + g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt(i);
4044             END IF;
4045 
4046          END IF;
4047 
4048         /*------------------------------------------------------------------------------+
4049          | Calculate accounted amount for revenue amount allocated to each revenue line |
4050          +------------------------------------------------------------------------------*/
4051          IF g_ae_rule_rec.revenue_acctd_amt <> 0 THEN
4052 
4053           /*------------------------------------------------------------------------------+
4054            | Process Line accounted amounts                                               |
4055            +------------------------------------------------------------------------------*/
4056             IF l_process_rev THEN
4057             g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i) :=
4058              arpcurr.CurrRound(l_rev_run_acctd_amt_tot / g_ae_rule_rec.revenue_acctd_amt
4059                                * g_ae_rule_rec.line_acctd_amt_alloc, g_ae_sys_rec.base_currency)
4060                                    - l_rev_run_pro_acctd_amt_tot;
4061 
4062              IF PG_DEBUG in ('Y', 'C') THEN
4063                 arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt = '||
4064                                  g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i));
4065              END IF;
4066 
4067           /*------------------------------------------------------------------------------+
4068            | Running total for prorated Revenue accounted amount in base currency         |
4069            +------------------------------------------------------------------------------*/
4070             l_rev_run_pro_acctd_amt_tot := l_rev_run_pro_acctd_amt_tot
4071                                                  + g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i);
4072             END IF;
4073           /*------------------------------------------------------------------------------+
4074            | Process Freight and charges                                                  |
4075            +------------------------------------------------------------------------------*/
4076             IF l_process_frt THEN
4077             g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt(i) :=
4078              arpcurr.CurrRound(l_rev_run_acctd_amt_tot / g_ae_rule_rec.revenue_acctd_amt
4079                                * (g_ae_rule_rec.freight_acctd_amt_alloc + g_ae_rule_rec.charges_acctd_amt_alloc),
4080                                g_ae_sys_rec.base_currency) - l_frt_run_pro_acctd_amt_tot;
4081 
4082              IF PG_DEBUG in ('Y', 'C') THEN
4083                 arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt('||i||') = '||
4084                                  g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt(i));
4085              END IF;
4086 
4087           /*------------------------------------------------------------------------------+
4088            | Running total for prorated Freight accounted amount in base currency         |
4089            +------------------------------------------------------------------------------*/
4090             l_frt_run_pro_acctd_amt_tot := l_frt_run_pro_acctd_amt_tot
4091                                             + g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt(i);
4092 
4093             END IF;
4094          END IF; --revenue accounted amount is not zero
4095 
4096          --Dump_Alloc_Rev_Tax(p_type => 'REV_TAX', p_alloc_rec => g_ae_alloc_rev_tax_tbl);
4097 
4098       END IF; --process revenue
4099 
4100    /*------------------------------------------------------------------------------+
4101     | Allocate tax amounts, deferred tax amounts                                   |
4102     +------------------------------------------------------------------------------*/
4103       IF l_process_tax AND g_ae_alloc_rev_tax_tbl.ae_account_class(i) = 'TAX' THEN
4104 
4105      /*------------------------------------------------------------------------------+
4106       | Maintain running total amounts for Tax amounts and accounted amounts         |
4107       +------------------------------------------------------------------------------*/
4108          l_tax_run_amt_tot       := l_tax_run_amt_tot + g_ae_alloc_rev_tax_tbl.ae_amount(i);
4109          l_tax_run_acctd_amt_tot := l_tax_run_acctd_amt_tot + g_ae_alloc_rev_tax_tbl.ae_acctd_amount(i);
4110 
4111          IF g_ae_rule_rec.tax_amt <> 0 THEN --prevent zero divide
4112            /*------------------------------------------------------------------------------+
4113             | Allocate tax for discount or adjustments to each tax line in Invoice         |
4114             | currency. Tax lines 10, 20, 30, 40, Tax Total 100, Tax on Discount 10        |
4115             | Line 1  a -> 10 * 10/100  = 1 (allocated)                                    |
4116             |                                                                              |
4117             | Line 2    -> (10 + 20)/100 * 10 = 3                                          |
4118             |         b -> 3 - a = 2 (allocated)                                           |
4119             |                                                                              |
4120             | Line 3    -> (10 + 20 + 30) * 10/100 = 6                                     |
4121             |         c -> 6 - a - b = 3                                                   |
4122             | Line .....                                                                   |
4123             +------------------------------------------------------------------------------*/
4124 
4125             g_ae_alloc_rev_tax_tbl.ae_pro_amt(i) :=
4126              arpcurr.CurrRound(l_tax_run_amt_tot / g_ae_rule_rec.tax_amt * g_ae_rule_rec.tax_amt_alloc,
4127                                g_cust_inv_rec.invoice_currency_code) - l_tax_run_pro_amt_tot;
4128 
4129              IF PG_DEBUG in ('Y', 'C') THEN
4130                 arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_amt = '||
4131                                  g_ae_alloc_rev_tax_tbl.ae_pro_amt(i));
4132              END IF;
4133 
4134           /*------------------------------------------------------------------------------+
4135            | Running total for prorated Tax amount in currency of Invoice                 |
4136            +------------------------------------------------------------------------------*/
4137             l_tax_run_pro_amt_tot := l_tax_run_pro_amt_tot + g_ae_alloc_rev_tax_tbl.ae_pro_amt(i);
4138 
4139          END IF;
4140 
4141          IF g_ae_rule_rec.tax_acctd_amt <> 0 THEN --prevent zero divide
4142           /*------------------------------------------------------------------------------+
4143            | Calculate accounted amount for tax amount allocated to each tax line         |
4144            +------------------------------------------------------------------------------*/
4145             g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i) :=
4146             arpcurr.CurrRound(l_tax_run_acctd_amt_tot / g_ae_rule_rec.tax_acctd_amt
4147                               * g_ae_rule_rec.tax_acctd_amt_alloc, g_ae_sys_rec.base_currency)
4148                                      - l_tax_run_pro_acctd_amt_tot;
4149 
4150             IF PG_DEBUG in ('Y', 'C') THEN
4151                arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt = '||
4152                                 g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i));
4153             END IF;
4154 
4155          /*------------------------------------------------------------------------------+
4156           | Running total for prorated Tax accounted amount in base currency             |
4157           +------------------------------------------------------------------------------*/
4158             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);
4159 
4160         END IF;
4161 
4162       /*-------------------------------------------------------------------------------+
4163        | Calculate taxable as "allocated tax amount/actual tax amt * actual taxable"   |
4164        | to get accurate basis for taxable only if a boundary condition exists         |
4165        +-------------------------------------------------------------------------------*/
4166         IF ((g_bound_tax) OR (g_bound_activity))
4167            AND ((g_orig_line_amt_alloc <> 0) OR (g_orig_line_acctd_amt_alloc <> 0)) THEN
4168 
4169            IF g_ae_alloc_rev_tax_tbl.ae_amount(i) <> 0 THEN
4170 
4171               g_ae_alloc_rev_tax_tbl.ae_pro_taxable_amt(i) :=
4172                    arpcurr.CurrRound(g_ae_alloc_rev_tax_tbl.ae_taxable_amount(i) *
4173                                      g_ae_alloc_rev_tax_tbl.ae_pro_amt(i) / g_ae_alloc_rev_tax_tbl.ae_amount(i),
4174                                      g_cust_inv_rec.invoice_currency_code);
4175 
4176               IF PG_DEBUG in ('Y', 'C') THEN
4177                  arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_taxable_amt = '||
4178                                   g_ae_alloc_rev_tax_tbl.ae_pro_taxable_amt(i));
4179               END IF;
4180 
4181            END IF; --end if ae_amount is zero
4182 
4183          /*------------------------------------------------------------------------------+
4184           | Calculate taxable accounted amount                                           |
4185           +------------------------------------------------------------------------------*/
4186            IF g_ae_alloc_rev_tax_tbl.ae_acctd_amount(i) <> 0 THEN
4187 
4188               g_ae_alloc_rev_tax_tbl.ae_pro_taxable_acctd_amt(i) :=
4189                 arpcurr.CurrRound(g_ae_alloc_rev_tax_tbl.ae_taxable_acctd_amount(i) *
4190                              g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(i) / g_ae_alloc_rev_tax_tbl.ae_acctd_amount(i),
4191                              g_ae_sys_rec.base_currency);
4192 
4193               IF PG_DEBUG in ('Y', 'C') THEN
4194                  arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_taxable_acctd_amt = '||
4195                                 g_ae_alloc_rev_tax_tbl.ae_pro_taxable_acctd_amt(i));
4196               END IF;
4197 
4198            END IF; --end if ae_acctd_amount is zero
4199 
4200         END IF; --End if Boundary condition
4201 
4202       /*------------------------------------------------------------------------------+
4203        | Allocation of tax based on payment is done for all tax lines only deferred   |
4204        | tax amounts will be moved from interim to collected tax account              |
4205        +------------------------------------------------------------------------------*/
4206         IF (g_ae_doc_rec.source_table = 'RA') AND (g_ae_alloc_rev_tax_tbl.ae_collected_tax_ccid(i) IS NOT NULL)
4207            AND (NOT g_done_def_tax)
4208         THEN
4209          /*------------------------------------------------------------------------------+
4210           | Maintain running total amounts for Revenue amounts and accounted amounts     |
4211           +------------------------------------------------------------------------------*/
4212            l_def_tax_run_amt_tot           := l_def_tax_run_amt_tot +
4213                                                         g_ae_alloc_rev_tax_tbl.ae_amount(i);
4214            l_def_tax_run_acctd_amt_tot     := l_def_tax_run_acctd_amt_tot +
4215                                                         g_ae_alloc_rev_tax_tbl.ae_acctd_amount(i);
4216 
4217          /*---------------------------------------------------------------------------------+
4218           | Calculate deferred tax amount to be moved from interim to collected tax account |
4219           +---------------------------------------------------------------------------------*/
4220            IF g_ae_rule_rec.def_tax_amt <> 0 THEN
4221 
4222               g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt(i) :=
4223                         arpcurr.CurrRound(l_def_tax_run_amt_tot / g_ae_rule_rec.def_tax_amt * l_tax_applied,
4224                                           g_cust_inv_rec.invoice_currency_code) - l_pro_def_tax_run_amt;
4225 
4226               IF PG_DEBUG in ('Y', 'C') THEN
4227                  arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt = '||
4228                                   g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt(i));
4229               END IF;
4230 
4231            /*------------------------------------------------------------------------------+
4232             | Running total for prorated deferred tax amount in currency of Invoice        |
4233             +------------------------------------------------------------------------------*/
4234              l_pro_def_tax_run_amt := l_pro_def_tax_run_amt + g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt(i);
4235 
4236            END IF;
4237 
4238          /*------------------------------------------------------------------------------+
4239           | Calculate deferred tax accounted amount to be moved from interim to collected|
4240           | tax account                                                                  |
4241           +------------------------------------------------------------------------------*/
4242            IF g_ae_rule_rec.def_tax_acctd_amt <> 0 THEN
4243 
4244               g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt(i) :=
4245                 arpcurr.CurrRound(l_def_tax_run_acctd_amt_tot / g_ae_rule_rec.def_tax_acctd_amt
4246                                   * l_tax_acctd_applied, g_ae_sys_rec.base_currency)
4247                                           - l_pro_def_tax_run_acctd_amt;
4248 
4249               IF PG_DEBUG in ('Y', 'C') THEN
4250                  arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt = '||
4251                                   g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt(i));
4252               END IF;
4253 
4254              /*------------------------------------------------------------------------------+
4255               | Running total for prorated deferred tax accounted amount in base currency    |
4256               +------------------------------------------------------------------------------*/
4257                l_pro_def_tax_run_acctd_amt :=
4258                   l_pro_def_tax_run_acctd_amt + g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt(i);
4259 
4260            END IF;
4261 
4262            l_last_def_tax := l_ctr;
4263 
4264         END IF; --End if payment not zero
4265 
4266         --Dump_Alloc_Rev_Tax(p_type => 'REV_TAX', p_alloc_rec => g_ae_alloc_rev_tax_tbl);
4267 
4268         l_last_tax := l_ctr;
4269 
4270       END IF; --process tax
4271 
4272     END LOOP;  --loop bulk fetched rows
4273 
4274    /*------------------------------------------------------------------------------+
4275     | Update the amounts for revenue or tax based on rowid                         |
4276     +------------------------------------------------------------------------------*/
4277       FORALL m IN g_ae_alloc_rev_tax_tbl.l_rowid.FIRST .. g_ae_alloc_rev_tax_tbl.l_rowid.LAST
4278           UPDATE ar_ae_alloc_rec_gt
4279            SET  ae_pro_amt             = g_ae_alloc_rev_tax_tbl.ae_pro_amt(m),
4280              ae_pro_acctd_amt          = g_ae_alloc_rev_tax_tbl.ae_pro_acctd_amt(m),
4281              ae_pro_taxable_amt        = g_ae_alloc_rev_tax_tbl.ae_pro_taxable_amt(m),
4282              ae_pro_taxable_acctd_amt  = g_ae_alloc_rev_tax_tbl.ae_pro_taxable_acctd_amt(m),
4283              ae_pro_frt_chrg_amt       = g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_amt(m),
4284              ae_pro_frt_chrg_acctd_amt = g_ae_alloc_rev_tax_tbl.ae_pro_frt_chrg_acctd_amt(m),
4285              ae_pro_def_tax_amt        = g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_amt(m),
4286              ae_pro_def_tax_acctd_amt  = g_ae_alloc_rev_tax_tbl.ae_pro_def_tax_acctd_amt(m)
4287           WHERE rowid = g_ae_alloc_rev_tax_tbl.l_rowid(m);
4288 
4289   --Exit if Last fetch
4290     IF l_last_fetch THEN
4291        EXIT;
4292     END IF;
4293 
4294    END LOOP; --process revenue tax bulk fetch
4295 
4296    CLOSE l_rev_tax_cur;
4297 
4298    END IF; --process revenue or tax
4299 
4300  /*---------------------------------------------------------------------------------+
4301   | Call taxable amount routine, to build the taxable amounts and accounted amounts |
4302   +---------------------------------------------------------------------------------*/
4303    IF (NOT g_bound_tax) AND (NOT g_bound_activity) and l_process_tax THEN
4304        Set_Taxable_Amt(p_type_acct => p_type_acct);
4305        Set_Taxable_Split_Amt(p_type_acct => p_type_acct);
4306    END IF;
4307 
4308  /*------------------------------------------------------------------------------+
4309   | Abnormal rounding correction condition this should never happen, however this|
4310   | is a safety mechanism. For payments check rounding only when type acct is PAY|
4311   +------------------------------------------------------------------------------*/
4312    IF ((((l_tax_run_pro_amt_tot <> g_ae_rule_rec.tax_amt_alloc) AND (g_ae_rule_rec.tax_amt <> 0))
4313           OR ((l_tax_run_pro_acctd_amt_tot <> g_ae_rule_rec.tax_acctd_amt_alloc)
4314               AND (g_ae_rule_rec.tax_acctd_amt <> 0)))
4315       OR ((((l_pro_def_tax_run_amt <> l_tax_applied) AND (g_ae_rule_rec.def_tax_amt <> 0))
4316              OR ((l_pro_def_tax_run_acctd_amt <> l_tax_acctd_applied) AND (g_ae_rule_rec.def_tax_acctd_amt <> 0)))
4317               AND (g_ae_def_tax) AND (NOT g_done_def_tax) AND (p_type_acct = 'PAY')))
4318       AND l_process_tax THEN
4319 
4320       IF PG_DEBUG in ('Y', 'C') THEN
4321          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_tax_run_pro_amt_tot : '||l_tax_run_pro_amt_tot||
4322                          ' <> '||'g_ae_rule_rec.tax_amt_alloc : '||g_ae_rule_rec.tax_amt_alloc);
4323          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_tax_run_pro_acctd_amt_tot : '||l_tax_run_pro_acctd_amt_tot||
4324                          ' <> '||'g_ae_rule_rec.tax_acctd_amt_alloc : '||g_ae_rule_rec.tax_acctd_amt_alloc);
4325          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_pro_def_tax_run_amt : '||l_pro_def_tax_run_amt||
4326                          ' <> '||'l_tax_applied : '||l_tax_applied);
4327          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_pro_def_tax_run_acctd_amt : '||l_pro_def_tax_run_acctd_amt||
4328                          ' <> '||'l_tax_acctd_applied : '||l_tax_acctd_applied);
4329       END IF;
4330 
4331       RAISE rounding_error;
4332 
4333    END IF; --tax rounding condition check
4334 
4335 /*------------------------------------------------------------------------------+
4336  | Abnormal rounding correction condition this should never happen, however this|
4337  | is a safety mechanism.                                                       |
4338  +------------------------------------------------------------------------------*/
4339    IF (((l_rev_run_pro_amt_tot <> g_ae_rule_rec.line_amt_alloc) AND (g_ae_rule_rec.revenue_amt <> 0))
4340           OR ((l_rev_run_pro_acctd_amt_tot <> g_ae_rule_rec.line_acctd_amt_alloc)
4341               AND (g_ae_rule_rec.revenue_acctd_amt <>0))) AND l_process_rev THEN
4342 
4343       IF PG_DEBUG in ('Y', 'C') THEN
4344          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_rev_run_pro_amt_tot : '||l_rev_run_pro_amt_tot||
4345                          ' <> '||'g_ae_rule_rec.line_amt_alloc : '||g_ae_rule_rec.line_amt_alloc);
4346          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_rev_run_pro_acctd_amt_tot : '||l_rev_run_pro_acctd_amt_tot||
4347                          ' <> '||'g_ae_rule_rec.line_acctd_amt_alloc : '||g_ae_rule_rec.line_acctd_amt_alloc);
4348       END IF;
4349 
4350       RAISE rounding_error;
4351 
4352    END IF; --check rounding for revenue
4353 
4354  /*------------------------------------------------------------------------------+
4355   | Abnormal rounding correction condition this should never happen, however this|
4356   | is a safety mechanism.                                                       |
4357   +------------------------------------------------------------------------------*/
4358    IF (((l_frt_run_pro_amt_tot <> (g_ae_rule_rec.freight_amt_alloc + g_ae_rule_rec.charges_amt_alloc))
4359         AND (g_ae_rule_rec.revenue_amt <> 0))
4360       OR ((l_frt_run_pro_acctd_amt_tot <>
4361                   (g_ae_rule_rec.freight_acctd_amt_alloc + g_ae_rule_rec.charges_acctd_amt_alloc))
4362            AND (g_ae_rule_rec.revenue_acctd_amt <>0))) AND l_process_frt THEN
4363 
4364       IF PG_DEBUG in ('Y', 'C') THEN
4365          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_frt_run_pro_amt_tot : '||l_frt_run_pro_amt_tot||
4366                          ' <> '||'g_ae_rule_rec.freight_amt_alloc ' || g_ae_rule_rec.freight_amt_alloc ||
4367                          ' + g_ae_rule_rec.charges_amt_alloc ' || g_ae_rule_rec.charges_amt_alloc );
4368          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || 'l_frt_run_pro_acctd_amt_tot : '||l_frt_run_pro_acctd_amt_tot||
4369                          ' <> '||'g_ae_rule_rec.freight_acctd_amt_alloc ' || g_ae_rule_rec.freight_acctd_amt_alloc ||
4370                          ' + g_ae_rule_rec.charges_acctd_amt_alloc ' || g_ae_rule_rec.charges_acctd_amt_alloc );
4371       END IF;
4372 
4373       RAISE rounding_error;
4374 
4375    END IF;
4376 
4377  IF PG_DEBUG in ('Y', 'C') THEN
4378     arp_standard.debug( 'ARP_ALLOCATION_PKG.Alloc_Rev_Tax_Amt()-');
4379  END IF;
4380 
4381 EXCEPTION
4382 
4383   WHEN rounding_error THEN
4384      IF PG_DEBUG in ('Y', 'C') THEN
4385         arp_standard.debug('Rounding Error: ARP_ALLOCATION_PKG.Alloc_Rev_Tax_Amt' );
4386      END IF;
4387      fnd_message.set_name('AR','AR_ROUNDING_ERROR');
4388      fnd_message.set_token('ROUTINE','ARP_ALLOCATION_PKG.ALLOC_REV_TAX_AMT');
4389      RAISE;
4390 
4391    WHEN OTHERS THEN
4392       IF PG_DEBUG in ('Y', 'C') THEN
4393          arp_standard.debug('Alloc_Rev_Tax_Amt: ' || SQLERRM);
4394          arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Alloc_Rev_Tax_Amt');
4395       END IF;
4396       RAISE;
4397 
4398 END Alloc_Rev_Tax_Amt;
4399 
4400 /* ==========================================================================
4401  | PROCEDURE Set_Taxable_Amt
4402  |
4403  | DESCRIPTION
4404  |      Derive Taxable amounts for Tax, when this routine is called the tax
4405  |      table has to exist.
4406  |
4407  | SCOPE - PRIVATE
4408  |
4409  | PARAMETERS
4410  |       p_type_acct      IN    Indicates accounting for earned, unearned
4411  |                              discounts or adjustments
4412  *==========================================================================*/
4413 PROCEDURE Set_Taxable_Amt(p_type_acct IN VARCHAR2) IS
4414 
4415 l_ctr                    BINARY_INTEGER;
4416 l_ctr1                   BINARY_INTEGER;
4417 
4418 CURSOR set_taxable_and_link IS
4419 select /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
4420        a2.rowid,
4421        a3.link_id,
4422        a3.line_id,
4423        a3.amt,
4424        a3.acctd_amt
4425 from ar_ae_alloc_rec_gt a2,
4426 (select  /*+ INDEX(ar_ae_alloc_rec_gt AR_AE_ALLOC_REC_GT_N3) */
4427          a1.ae_id                   ae_id,
4428          decode(p_type_acct,
4429                 'ED_ADJ', a1.ae_tax_link_id_ed_adj,
4430                 'UNED'  , a1.ae_tax_link_id_uned,
4431                 'PAY'   , a1.ae_tax_link_id)          link_id,
4432          a1.ae_customer_trx_line_id line_id,
4433          sum(a1.ae_pro_amt)         amt,
4434          sum(a1.ae_pro_acctd_amt)   acctd_amt
4435 from ar_ae_alloc_rec_gt a1
4436 where a1.ae_id = g_id
4437 and a1.ae_account_class IN ('REVEARN','REVUNEARN') --MAINTAINLINKTAXID
4438 group by
4439          a1.ae_id,
4440 		 decode(p_type_acct,
4441                 'ED_ADJ', a1.ae_tax_link_id_ed_adj,
4442                 'UNED'  , a1.ae_tax_link_id_uned,
4443                 'PAY'   , a1.ae_tax_link_id),
4444          a1.ae_customer_trx_line_id) a3
4445 where a2.ae_id = g_id
4446 and a3.ae_id = a2.ae_id
4447 and a2.ae_link_to_cust_trx_line_id = a3.line_id
4448 and a2.ae_account_class = 'TAX'
4449 and decode(p_type_acct,
4450            'ED_ADJ', a2.ae_tax_link_id_ed_adj,
4451            'UNED'  , a2.ae_tax_link_id_uned,
4452            'PAY'   , a2.ae_tax_link_id) = a3.link_id
4453 order by a3.link_id, (abs(a3.amt) + abs(a3.acctd_amt)) DESC, a3.line_id;
4454 
4455 TYPE num_type   IS TABLE OF NUMBER       INDEX BY BINARY_INTEGER;
4456 TYPE rowid_type IS TABLE OF VARCHAR2(50) INDEX BY BINARY_INTEGER;
4457 
4458 rowid_tbl      rowid_type;
4459 line_id_tbl    num_type;
4460 link_id_tbl    num_type;
4461 amt_tbl        num_type;
4462 acctd_amt_tbl  num_type;
4463 
4464 l_last_fetch BOOLEAN := FALSE;
4465 
4466 prev_sum_of_amounts NUMBER;
4467 prev_link_id        NUMBER;
4468 
4469 BEGIN
4470 
4471    IF PG_DEBUG in ('Y', 'C') THEN
4472       arp_standard.debug( 'ARP_ALLOCATION_PKG.Set_Taxable_Amt()+');
4473    END IF;
4474 
4475    OPEN set_taxable_and_link;
4476 
4477    prev_link_id := -9999;
4478    prev_sum_of_amounts := 0;
4479 
4480    LOOP
4481     --initialize record
4482 
4483       FETCH set_taxable_and_link BULK COLLECT INTO
4484             rowid_tbl,
4485             link_id_tbl,
4486             line_id_tbl,
4487             amt_tbl,
4488             acctd_amt_tbl
4489         LIMIT g_bulk_fetch_rows;
4490 
4491       IF set_taxable_and_link%NOTFOUND THEN
4492          l_last_fetch := TRUE;
4493       END IF;
4494 
4495       IF (rowid_tbl.COUNT = 0) AND (l_last_fetch) THEN
4496          IF PG_DEBUG in ('Y', 'C') THEN
4497             arp_standard.debug('Set_Taxable_Amt: ' || 'COUNT = 0 and LAST FETCH ');
4498          END IF;
4499          EXIT;
4500       END IF;
4501 
4502       FOR i IN rowid_tbl.FIRST .. rowid_tbl.LAST LOOP
4503 
4504         --Initialize the sum of amounts and link to that of new link line
4505           IF prev_link_id <> link_id_tbl(i) THEN
4506              prev_link_id := link_id_tbl(i);
4507              prev_sum_of_amounts := 0;
4508           END IF;
4509 
4510        --Verify whether there exists a non zero amount, accounted amount for link id
4511           IF  ((abs(amt_tbl(i)) + abs(acctd_amt_tbl(i))) = 0)
4512               AND prev_link_id = link_id_tbl(i)
4513               AND prev_sum_of_amounts = 0 THEN
4514               link_id_tbl(i) := '';
4515           END IF;
4516 
4517           prev_sum_of_amounts := prev_sum_of_amounts + abs(amt_tbl(i)) + abs(acctd_amt_tbl(i));
4518       END LOOP;
4519 
4520     --Bulk update
4521       FORALL m IN rowid_tbl.FIRST .. rowid_tbl.LAST
4522         UPDATE ar_ae_alloc_rec_gt
4523         SET ae_pro_taxable_amt       = amt_tbl(m),
4524             ae_pro_taxable_acctd_amt = acctd_amt_tbl(m),
4525             ae_tax_link_id_act       = link_id_tbl(m)
4526         WHERE rowid = rowid_tbl(m)
4527         AND link_id_tbl(m) IS NOT NULL;
4528 
4529    --Exit if Last fetch
4530       IF l_last_fetch THEN
4531          EXIT;
4532       END IF;
4533 
4534    END LOOP; --Bulk fetch
4535 
4536    CLOSE set_taxable_and_link;
4537 
4538    IF PG_DEBUG in ('Y', 'C') THEN
4539       arp_standard.debug( 'ARP_ALLOCATION_PKG.Set_Taxable_Amt()-');
4540    END IF;
4541 
4542 EXCEPTION
4543   WHEN OTHERS THEN
4544      IF PG_DEBUG in ('Y', 'C') THEN
4545         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Set_Taxable_Amt');
4546      END IF;
4547      RAISE;
4548 
4549 END Set_Taxable_Amt;
4550 
4551 
4552 /* ==========================================================================
4553  | PROCEDURE Set_Taxable_Split_Amt
4554  |
4555  | DESCRIPTION
4556  |      Derive Taxable split amounts for Tax, when this routine is called the
4557  |      table has to exist. Required to ensure that the taxable amount is not
4558  |      overstated if the tax line has splits.
4559  |
4560  | SCOPE - PRIVATE
4561  |
4562  | PARAMETERS
4563  |       p_type_acct      IN    Indicates accounting for earned, unearned
4564  |                              discounts or adjustments
4565  *==========================================================================*/
4566 PROCEDURE Set_Taxable_Split_Amt(p_type_acct IN VARCHAR2) IS
4567 
4568 CURSOR set_taxable_split IS
4569 select /*+ INDEX(a1 AR_AE_ALLOC_REC_GT_N3) */
4570        a1.rowid                       row_id,
4571        a1.ae_link_to_cust_trx_line_id inv_line_id,
4572        a1.ae_tax_id                   tax_id,
4573        a1.ae_tax_type                 tax_type,
4574        a1.ae_code_combination_id      ae_code_combination_id,
4575        a1.ae_collected_tax_ccid       ae_collected_tax_ccid,
4576        a1.ae_pro_taxable_amt          pro_taxable_amt,
4577        a1.ae_pro_taxable_acctd_amt    pro_taxable_acctd_amt,
4578        0                              taxable_amt_split,
4579        0                              taxable_acctd_amt_split,
4580        ''                             taxable_amt_r_split,
4581        ''                             taxable_acctd_amt_r_split
4582 from ar_ae_alloc_rec_gt a1,
4583      (select /*+ INDEX(a2 AR_AE_ALLOC_REC_GT_N3) */
4584              a2.ae_link_to_cust_trx_line_id ae_link_to_cust_trx_line_id,
4585              a2.ae_tax_type                 ae_tax_type,
4586              a2.ae_tax_id                   ae_tax_id
4587       from ar_ae_alloc_rec_gt a2
4588       where a2.ae_id = g_id
4589       and a2.ae_account_class = 'TAX'
4590       group by a2.ae_link_to_cust_trx_line_id,
4591                a2.ae_tax_type,
4592                a2.ae_tax_id
4593       having count(*) > 1) a3
4594 where a1.ae_id = g_id
4595 and a1.ae_account_class = 'TAX'
4596 and a1.ae_link_to_cust_trx_line_id = a3.ae_link_to_cust_trx_line_id
4597 and a1.ae_tax_id = a3.ae_tax_id
4598 and a1.ae_tax_type = a3.ae_tax_type
4599 order by a1.ae_link_to_cust_trx_line_id,
4600          a1.ae_tax_type,
4601          a1.ae_tax_id,
4602          decode(a1.ae_collected_tax_ccid,
4603                 '',2,
4604                 1),
4605          a1.ae_code_combination_id,
4606          a1.ae_collected_tax_ccid;
4607 
4608 TYPE num_type   IS TABLE OF NUMBER       INDEX BY BINARY_INTEGER;
4609 TYPE var_type   IS TABLE OF VARCHAR2(3)  INDEX BY BINARY_INTEGER;
4610 TYPE rowid_type IS TABLE OF VARCHAR2(50) INDEX BY BINARY_INTEGER;
4611 
4612 rowid_tbl                     rowid_type;
4613 line_id_tbl                   num_type;
4614 tax_id_tbl                    num_type;
4615 tax_type_tbl                  var_type;
4616 taxable_amt_tbl               num_type;
4617 taxable_acctd_amt_tbl         num_type;
4618 taxable_amt_split_tbl         num_type;
4619 taxable_acctd_amt_split_tbl   num_type;
4620 taxable_amt_recov_tbl         num_type;
4621 taxable_acctd_amt_recov_tbl   num_type;
4622 tax_ccid_tbl                  num_type;
4623 tax_collected_ccid_tbl        num_type;
4624 
4625 l_last_fetch                  BOOLEAN := FALSE;
4626 l_prev_customer_trx_line_id   NUMBER;
4627 l_prev_tax_id                 NUMBER;
4628 l_prev_tax_type               VARCHAR2(3);
4629 l_prev_ccid                   NUMBER;
4630 l_prev_collected_ccid         NUMBER;
4631 
4632 BEGIN
4633    IF PG_DEBUG in ('Y', 'C') THEN
4634       arp_standard.debug( 'ARP_ALLOCATION_PKG.Set_Taxable_Split_Amt()+');
4635    END IF;
4636 
4637    OPEN set_taxable_split;
4638 
4639    l_prev_customer_trx_line_id := -9999;
4640    l_prev_tax_id := -9999;
4641    l_prev_tax_type := 'XXX';
4642    l_prev_ccid := -9999;
4643    l_prev_collected_ccid := -9999;
4644 
4645    LOOP
4646     --initialize record
4647 
4648       FETCH set_taxable_split BULK COLLECT INTO
4649             rowid_tbl,
4650             line_id_tbl,
4651             tax_id_tbl,
4652             tax_type_tbl,
4653             tax_ccid_tbl,
4654             tax_collected_ccid_tbl,
4655             taxable_amt_tbl,
4656             taxable_acctd_amt_tbl,
4657             taxable_amt_split_tbl,
4658             taxable_acctd_amt_split_tbl,
4659             taxable_amt_recov_tbl,
4660             taxable_acctd_amt_recov_tbl
4661         LIMIT g_bulk_fetch_rows;
4662 
4663       IF set_taxable_split%NOTFOUND THEN
4664          l_last_fetch := TRUE;
4665       END IF;
4666 
4667       IF (rowid_tbl.COUNT = 0) AND (l_last_fetch) THEN
4668          IF PG_DEBUG in ('Y', 'C') THEN
4669             arp_standard.debug('Set_Taxable_Split_Amt: ' || 'COUNT = 0 and LAST FETCH ');
4670          END IF;
4671          EXIT;
4672       END IF;
4673 
4674       FOR i IN rowid_tbl.FIRST .. rowid_tbl.LAST LOOP
4675         /*------------------------------------------------------------------+
4676          | For splits set the second, third splits to 0, so that only the   |
4677          | first split taxable will be used in sum function, therby ensuring|
4678          | that the taxable amount is counted only once in Build_Tax.       |
4679          +------------------------------------------------------------------*/
4680           IF l_prev_customer_trx_line_id = line_id_tbl(i)
4681               AND l_prev_tax_id = tax_id_tbl(i)
4682               AND l_prev_tax_type = tax_type_tbl(i) THEN
4683              taxable_amt_split_tbl(i)       := 0;
4684              taxable_acctd_amt_split_tbl(i) := 0;
4685 
4686              IF l_prev_ccid <> tax_ccid_tbl(i) THEN
4687                 taxable_amt_recov_tbl(i)       :=  taxable_amt_tbl(i);
4688                 taxable_acctd_amt_recov_tbl(i) := taxable_acctd_amt_tbl(i);
4689              END IF;
4690 
4691           ELSE
4692              taxable_amt_split_tbl(i)       := taxable_amt_tbl(i);
4693              taxable_acctd_amt_split_tbl(i) := taxable_acctd_amt_tbl(i);
4694           END IF;
4695 
4696           l_prev_customer_trx_line_id := line_id_tbl(i);
4697           l_prev_tax_id := tax_id_tbl(i);
4698           l_prev_tax_type := tax_type_tbl(i);
4699           l_prev_ccid := tax_ccid_tbl(i);
4700           l_prev_collected_ccid := tax_collected_ccid_tbl(i);
4701 
4702       END LOOP;
4703 
4704     --Bulk update
4705       FORALL m IN rowid_tbl.FIRST .. rowid_tbl.LAST
4706         UPDATE ar_ae_alloc_rec_gt
4707         SET ae_pro_split_taxable_amt       = taxable_amt_split_tbl(m),
4708             ae_pro_split_taxable_acctd_amt = taxable_acctd_amt_split_tbl(m),
4709             ae_pro_recov_taxable_amt       = taxable_amt_recov_tbl(m),
4710             ae_pro_recov_taxable_acctd_amt = taxable_acctd_amt_recov_tbl(m)
4711         WHERE rowid = rowid_tbl(m);
4712 
4713    --Exit if Last fetch
4714       IF l_last_fetch THEN
4715          EXIT;
4716       END IF;
4717 
4718    END LOOP; --Bulk fetch
4719 
4720    CLOSE set_taxable_split;
4721 
4722    IF PG_DEBUG in ('Y', 'C') THEN
4723       arp_standard.debug( 'ARP_ALLOCATION_PKG.Set_Taxable_Split_Amt()-');
4724    END IF;
4725 
4726 EXCEPTION
4727   WHEN OTHERS THEN
4728      IF PG_DEBUG in ('Y', 'C') THEN
4729         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Set_Taxable_Split_Amt');
4730      END IF;
4731      RAISE;
4732 
4733 END Set_Taxable_Split_Amt;
4734 
4735 /* ==========================================================================
4736  | PROCEDURE Allocate_Tax_To_Rev
4737  |
4738  | DESCRIPTION
4739  |      Allocate Tax amount over revenue amounts based on Rule tax code source
4740  |      NONE
4741  |
4742  | SCOPE - PRIVATE
4743  |
4744  | PARAMETERS
4745  |       p_type_acct      IN    Indicates accounting for earned, unearned
4746  |                              discounts or adjustments
4747  *==========================================================================*/
4748 PROCEDURE Allocate_Tax_To_Rev(p_type_acct      IN VARCHAR2) IS
4749 
4750 l_ctr  BINARY_INTEGER := 0;
4751 l_ctr1 BINARY_INTEGER := 0;
4752 l_ctr2 BINARY_INTEGER := 0;
4753 
4754 l_rev_run_amt_tot             NUMBER ;
4755 l_rev_run_pro_amt_tot         NUMBER ;
4756 l_rev_run_acctd_amt_tot       NUMBER ;
4757 l_rev_run_pro_acctd_amt_tot   NUMBER ;
4758 l_tax_amt_pro_rev             NUMBER ;
4759 l_tax_acctd_amt_pro_rev       NUMBER ;
4760 l_weight_amt                  NUMBER ;
4761 l_weight_acctd_amt            NUMBER ;
4762 l_base_amt                    NUMBER ;
4763 l_base_acctd_amt              NUMBER ;
4764 l_prev_tax_cust_trx_line_id   NUMBER ;
4765 l_prev_code_combination_id    NUMBER ;
4766 l_prev_collected_tax_ccid     NUMBER ;
4767 l_prev_tax_amt                NUMBER ;
4768 l_prev_tax_acctd_amt          NUMBER ;
4769 
4770 l_dummy                       VARCHAR2(1);
4771 l_rev_rowid                   VARCHAR2(50);
4772 
4773 l_not_found                   BOOLEAN := FALSE;
4774 
4775 g_ae_alloc_tax_tbl   ar_ae_alloc_rec_gt%ROWTYPE;
4776 g_ae_alloc_rev_tbl   ar_ae_alloc_rec_gt%ROWTYPE;
4777 g_ae_alloc_empty_tbl ar_ae_alloc_rec_gt%ROWTYPE;
4778 
4779 CURSOR alloc_tax_rev IS
4780        SELECT /*+ INDEX(ae1 AR_AE_ALLOC_REC_GT_N3) INDEX(ae2 AR_AE_ALLOC_REC_GT_N1) */
4781               ae2.rowid                          ,
4782               ae1.ae_customer_trx_line_id        ,
4783               ae1.ae_link_to_cust_trx_line_id    ,
4784               ae1.ae_code_combination_id         ,
4785               nvl(ae1.ae_collected_tax_ccid,-9999),
4786               ae1.ae_pro_amt                     ,
4787               ae1.ae_pro_acctd_amt               ,
4788               ae2.ae_sum_rev_amt                 ,
4789               ae2.ae_sum_rev_acctd_amt           ,
4790               ae2.ae_count                       ,
4791               ae2.ae_amount                      ,
4792               ae2.ae_acctd_amount
4793        FROM ar_ae_alloc_rec_gt ae1,
4794             ar_ae_alloc_rec_gt ae2
4795        WHERE ae1.ae_id = g_id
4796        AND   ae1.ae_account_class = 'TAX'
4797        AND   ae2.ae_id = ae1.ae_id
4798        AND   ae2.ae_account_class IN ('REVEARN','REVUNEARN') --MAINTAINTAXLINKID
4799        AND   ae1.ae_link_to_cust_trx_line_id = ae2.ae_customer_trx_line_id
4800        AND   ((ae1.ae_pro_amt <> 0)
4801                OR (ae1.ae_pro_acctd_amt <> 0))
4802        ORDER BY ae1.ae_customer_trx_line_id, ae1.ae_link_to_cust_trx_line_id;
4803 
4804 BEGIN
4805 
4806    IF PG_DEBUG in ('Y', 'C') THEN
4807       arp_standard.debug('Allocate_Tax_To_Rev: ' ||  'ARP_ALLOCATION_PKG.Alloc_Tax_To_Rev()+');
4808    END IF;
4809 
4810    IF (((p_type_acct = 'ED_ADJ') AND (g_ae_rule_rec.tax_code_source1 = 'NONE'))
4811        OR ((p_type_acct = 'UNED') AND (g_ae_rule_rec.tax_code_source2 = 'NONE'))) THEN
4812 
4813     /*------------------------------------------------------------------------------+
4814      | Allocate each tax line to the Revenue lines for an Invoice line              |
4815      +------------------------------------------------------------------------------*/
4816       l_tax_amt_pro_rev              := 0;
4817       l_tax_acctd_amt_pro_rev        := 0;
4818       l_rev_run_amt_tot              := 0;
4819       l_rev_run_acctd_amt_tot        := 0;
4820       l_rev_run_pro_amt_tot          := 0;
4821       l_rev_run_pro_acctd_amt_tot    := 0;
4822       l_prev_tax_amt                 := 0;
4823       l_prev_tax_acctd_amt           := 0;
4824 
4825       OPEN alloc_tax_rev;
4826 
4827       l_prev_tax_cust_trx_line_id := -9999;
4828       l_prev_code_combination_id  := -9999;
4829       l_prev_collected_tax_ccid   := -9999;
4830 
4831       LOOP
4832 
4833       --no need to initialize as a fetch will return a value
4834       --and for last row we want to retain the old values
4835 
4836       --fetch from cursor
4837          FETCH alloc_tax_rev
4838          INTO l_rev_rowid,
4839               g_ae_alloc_tax_tbl.ae_customer_trx_line_id     ,
4840               g_ae_alloc_tax_tbl.ae_link_to_cust_trx_line_id ,
4841               g_ae_alloc_tax_tbl.ae_code_combination_id      ,
4842               g_ae_alloc_tax_tbl.ae_collected_tax_ccid       ,
4843               g_ae_alloc_tax_tbl.ae_pro_amt                  ,
4844               g_ae_alloc_tax_tbl.ae_pro_acctd_amt            ,
4845               g_ae_alloc_rev_tbl.ae_sum_rev_amt              ,
4846               g_ae_alloc_rev_tbl.ae_sum_rev_acctd_amt        ,
4847               g_ae_alloc_rev_tbl.ae_count                    ,
4848               g_ae_alloc_rev_tbl.ae_amount                   ,
4849               g_ae_alloc_rev_tbl.ae_acctd_amount               ;
4850 
4851        --Set cursor not found flag
4852          IF alloc_tax_rev%NOTFOUND THEN
4853             l_not_found := TRUE;
4854          END IF;
4855 
4856        /*------------------------------------------------------------------------------+
4857         | If current tax line not equals previous then check for rounding corrections  |
4858         +------------------------------------------------------------------------------*/
4859          IF ((l_prev_tax_cust_trx_line_id <> g_ae_alloc_tax_tbl.ae_customer_trx_line_id)
4860              OR ((l_prev_tax_cust_trx_line_id = g_ae_alloc_tax_tbl.ae_customer_trx_line_id)
4861                  AND ((l_prev_code_combination_id <> g_ae_alloc_tax_tbl.ae_code_combination_id)
4862                        OR (l_prev_collected_tax_ccid <> g_ae_alloc_tax_tbl.ae_collected_tax_ccid)))
4863              OR (l_not_found))
4864            AND (l_prev_tax_cust_trx_line_id <> -9999) THEN
4865 
4866            /*------------------------------------------------------------------------------+
4867             | Abnormal rounding correction condition this should never happen, however this|
4868             | is a safety mechanism. Check that tax all tax is allocated over revenue lines|
4869             +------------------------------------------------------------------------------*/
4870              IF ((l_rev_run_pro_amt_tot <> l_prev_tax_amt) OR
4871                       (l_rev_run_pro_acctd_amt_tot <> l_prev_tax_acctd_amt))             THEN
4872 
4873                 IF PG_DEBUG in ('Y', 'C') THEN
4874                    arp_standard.debug('Allocate_Tax_To_Rev: ' || 'l_rev_run_pro_amt_tot :'
4875                    || l_rev_run_pro_amt_tot ||
4876                    ' <> ' || 'l_prev_tax_amt :'
4877                    || l_prev_tax_amt);
4878                    arp_standard.debug('Allocate_Tax_To_Rev: ' || 'l_rev_run_pro_acctd_amt_tot :'
4879                    || l_rev_run_pro_acctd_amt_tot ||
4880                    ' <> ' || 'l_prev_tax_acctd_amt :'
4881                    || l_prev_tax_acctd_amt);
4882                 END IF;
4883 
4884                 RAISE rounding_error;
4885 
4886              END IF; --End if rounding error
4887 
4888              l_tax_amt_pro_rev              := 0;
4889              l_tax_acctd_amt_pro_rev        := 0;
4890              l_rev_run_amt_tot              := 0;
4891              l_rev_run_acctd_amt_tot        := 0;
4892              l_rev_run_pro_amt_tot          := 0;
4893              l_rev_run_pro_acctd_amt_tot    := 0;
4894              l_prev_tax_amt                 := 0;
4895              l_prev_tax_acctd_amt           := 0;
4896 
4897           END IF; --prev tax line not equal to current tax line
4898 
4899         --cursor fetch returns not data
4900           IF l_not_found THEN
4901              EXIT; --loop
4902           END IF;
4903 
4904           l_weight_amt                   := 0;
4905           l_weight_acctd_amt             := 0;
4906           l_base_amt                     := 0;
4907           l_base_acctd_amt               := 0;
4908 
4909         /*------------------------------------------------------------------------------+
4910          | Set Revenue amount and accounted amounts totals                              |
4911          +------------------------------------------------------------------------------*/
4912           IF PG_DEBUG in ('Y', 'C') THEN
4913              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'Invoice line                      '
4914                                || g_ae_alloc_tax_tbl.ae_link_to_cust_trx_line_id);
4915              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'g_ae_alloc_rev_tbl.ae_sum_rev_amt '
4916                                || g_ae_alloc_rev_tbl.ae_sum_rev_amt);
4917              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'g_ae_alloc_rev_tbl.ae_sum_rev_acctd_amt '
4918                                || g_ae_alloc_rev_tbl.ae_sum_rev_acctd_amt);
4919              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'Tax amount            =           '
4920                                || g_ae_alloc_tax_tbl.ae_pro_amt);
4921              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'Tax accounted amount  =           '
4922                                || g_ae_alloc_tax_tbl.ae_pro_acctd_amt);
4923           END IF;
4924 
4925        /*------------------------------------------------------------------------------+
4926         | Set base to prorate tax over revenue, this is applicable when for an Invoice |
4927         | Line the revenue amount or accounted amount is zero so equal weights are     |
4928         | to the 0 distributions                                                       |
4929         +------------------------------------------------------------------------------*/
4930           IF g_ae_alloc_rev_tbl.ae_sum_rev_amt = 0 THEN
4931              l_weight_amt := 1;
4932              l_base_amt   := g_ae_alloc_rev_tbl.ae_count;
4933           ELSE
4934              l_weight_amt := g_ae_alloc_rev_tbl.ae_amount;
4935              l_base_amt   := g_ae_alloc_rev_tbl.ae_sum_rev_amt;
4936           END IF;
4937 
4938           IF g_ae_alloc_rev_tbl.ae_sum_rev_acctd_amt = 0 THEN
4939              l_weight_acctd_amt := 1;
4940              l_base_acctd_amt   := g_ae_alloc_rev_tbl.ae_count;
4941           ELSE
4942              l_weight_acctd_amt := g_ae_alloc_rev_tbl.ae_acctd_amount;
4943              l_base_acctd_amt   := g_ae_alloc_rev_tbl.ae_sum_rev_acctd_amt;
4944           END IF;
4945 
4946        /*------------------------------------------------------------------------------+
4947         | Maintain running total amounts for Revenue amounts and accounted amounts     |
4948         +------------------------------------------------------------------------------*/
4949           l_rev_run_amt_tot       := l_rev_run_amt_tot + l_weight_amt;
4950           l_rev_run_acctd_amt_tot := l_rev_run_acctd_amt_tot + l_weight_acctd_amt;
4951 
4952           l_tax_amt_pro_rev := arpcurr.CurrRound(l_rev_run_amt_tot / l_base_amt
4953                                   * g_ae_alloc_tax_tbl.ae_pro_amt,
4954                                   g_cust_inv_rec.invoice_currency_code) - l_rev_run_pro_amt_tot ;
4955 
4956           IF PG_DEBUG in ('Y', 'C') THEN
4957              arp_standard.debug('Allocate_Tax_To_Rev: ' || 'l_tax_amt_pro_rev ' || l_tax_amt_pro_rev);
4958           END IF;
4959 
4960         /*------------------------------------------------------------------------------+
4961          | Running total for prorated Revenue amount in currency of Invoice             |
4962          +------------------------------------------------------------------------------*/
4963            l_rev_run_pro_amt_tot := l_rev_run_pro_amt_tot + l_tax_amt_pro_rev;
4964 
4965         /*------------------------------------------------------------------------------+
4966          | Calculate accounted amount for revenue amount allocated to each revenue line |
4967          +------------------------------------------------------------------------------*/
4968            l_tax_acctd_amt_pro_rev := arpcurr.CurrRound(l_rev_run_acctd_amt_tot /
4969                                         l_base_acctd_amt
4970                                         * g_ae_alloc_tax_tbl.ae_pro_acctd_amt,
4971                                         g_ae_sys_rec.base_currency) - l_rev_run_pro_acctd_amt_tot ;
4972            IF PG_DEBUG in ('Y', 'C') THEN
4973               arp_standard.debug('Allocate_Tax_To_Rev: ' || 'l_tax_acctd_amt_pro_rev ' || l_tax_acctd_amt_pro_rev);
4974            END IF;
4975 
4976         /*------------------------------------------------------------------------------+
4977          | Running total for prorated Revenue accounted amount in base currency         |
4978          +------------------------------------------------------------------------------*/
4979            l_rev_run_pro_acctd_amt_tot := l_rev_run_pro_acctd_amt_tot + l_tax_acctd_amt_pro_rev;
4980 
4981            l_prev_tax_cust_trx_line_id := g_ae_alloc_tax_tbl.ae_customer_trx_line_id;
4982            l_prev_code_combination_id  := g_ae_alloc_tax_tbl.ae_code_combination_id;
4983            l_prev_collected_tax_ccid   := g_ae_alloc_tax_tbl.ae_collected_tax_ccid;
4984            l_prev_tax_amt              := g_ae_alloc_tax_tbl.ae_pro_amt;
4985            l_prev_tax_acctd_amt        := g_ae_alloc_tax_tbl.ae_pro_acctd_amt;
4986 
4987         /*------------------------------------------------------------------------------+
4988          | Add tax allocated amount and accounted amount to revenue amounts allocated   |
4989          +------------------------------------------------------------------------------*/
4990            UPDATE ar_ae_alloc_rec_gt ae1
4991            SET ae1.ae_pro_amt       = ae1.ae_pro_amt + l_tax_amt_pro_rev,
4992                ae1.ae_pro_acctd_amt = ae1.ae_pro_acctd_amt + l_tax_acctd_amt_pro_rev
4993            WHERE ae1.rowid = l_rev_rowid;
4994 
4995       END LOOP; --End loop allocate tax
4996 
4997       CLOSE alloc_tax_rev;
4998 
4999    END IF; --End if TAX_CODE_SOURCE = 'NONE'
5000 
5001    IF PG_DEBUG in ('Y', 'C') THEN
5002       arp_standard.debug('Allocate_Tax_To_Rev: ' ||  'ARP_ALLOCATION_PKG.Alloc_Tax_To_Rev()-');
5003    END IF;
5004 
5005 EXCEPTION
5006   WHEN rounding_error THEN
5007      IF PG_DEBUG in ('Y', 'C') THEN
5008         arp_standard.debug('Allocate_Tax_To_Rev: ' || 'Rounding Error: ARP_ALLOCATION_PKG.Alloc_Tax_To_Rev' );
5009      END IF;
5010      fnd_message.set_name('AR','AR_ROUNDING_ERROR');
5011      fnd_message.set_token('ROUTINE','ARP_ALLOCATION_PKG.ALLOC_TAX_TO_REV');
5012      RAISE;
5013 
5014   WHEN OTHERS THEN
5015      IF PG_DEBUG in ('Y', 'C') THEN
5016         arp_standard.debug('Allocate_Tax_To_Rev: ' || 'EXCEPTION: ARP_ALLOCATION_PKG.Alloc_Tax_To_Rev');
5017      END IF;
5018      RAISE;
5019 
5020 END Allocate_Tax_To_Rev;
5021 
5022 /* ==========================================================================
5023  | PROCEDURE Set_Rev_Links
5024  |
5025  | DESCRIPTION
5026  |      Sets Actual Revenue link id's using rules and the tax link id total
5027  |      table. It is necessary to set the actual link id based on whether
5028  |      a given revenue line has associated tax allocations, if it does not
5029  |      then the link id is null.
5030  |
5031  | SCOPE - PRIVATE
5032  |
5033  | PARAMETERS
5034  |       p_type_acct      IN    Indicates accounting for earned, unearned
5035  |                              discounts or adjustments
5036  *==========================================================================*/
5037 PROCEDURE Set_Rev_Links(p_type_acct      IN VARCHAR2) IS
5038 
5039 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
5040 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
5041 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
5042 
5043 BEGIN
5044 
5045     IF PG_DEBUG in ('Y', 'C') THEN
5046        arp_standard.debug('ARP_ALLOCATION_PKG.Set_Rev_Links()+');
5047     END IF;
5048 
5049    /*-------------------------------------------------------------------------------+
5050     | Set Rules to determine whether Revenue link id needs to be set                |
5051     +-------------------------------------------------------------------------------*/
5052 /*
5053      Get_Rules(p_type_acct            => p_type_acct,
5054                p_gl_account_source    => l_gl_account_source,
5055                p_tax_code_source      => l_tax_code_source,
5056                p_tax_recoverable_flag => l_tax_recoverable_flag);
5057 */
5058 --}
5059     /*--------------------------------------------------------------------------------+
5060      | Set up actual link ids for revenue lines so that at summarization only required|
5061      | number of lines are created.                                                   |
5062      +--------------------------------------------------------------------------------*/
5063 --{HYUDETUPT
5064 --      UPDATE /*+ INDEX(ae1 AR_AE_ALLOC_REC_GT_N3) */
5065 --             ar_ae_alloc_rec_gt ae1
5066 --      SET ae1.ae_tax_link_id_act = decode(p_type_acct,
5067 --                                          'ED_ADJ', ae1.ae_tax_link_id_ed_adj,
5068 --                                          'UNED'  , ae1.ae_tax_link_id_uned  ,
5069 --                                          '')
5070 --      WHERE ae1.ae_id = g_id
5071 --      AND ae1.ae_account_class = 'REVEARN'
5072 --      AND (((ae1.ae_pro_amt <> 0) OR (ae1.ae_pro_acctd_amt <> 0))
5073 --           AND decode(p_type_acct,
5074 --                       'ED_ADJ', decode(ae1.ae_tax_link_id_ed_adj,
5075 --                                        '','N',
5076 --                                        'Y'),
5077 --                       'UNED', decode(ae1.ae_tax_link_id_uned,
5078 --                                      '','N',
5079 --                                      'Y'),
5080 --                       'N') = 'Y')
5081 --      AND EXISTS
5082 --              (SELECT /*+ INDEX(ae1 AR_AE_ALLOC_REC_GT_N3) */
5083 --                      ae2.ae_account_class
5084 --               FROM ar_ae_alloc_rec_gt ae2
5085 --               WHERE ae2.ae_account_class = 'TAX'
5086 --               AND ae2.ae_id = ae1.ae_id
5087 --               AND decode(p_type_acct,
5088 --                          'ED_ADJ', ae2.ae_tax_link_id_ed_adj,
5089 --                          'UNED'  , ae2.ae_tax_link_id_uned  ,
5090 --                          'Y') = decode(p_type_acct,
5091 --                                       'ED_ADJ', ae1.ae_tax_link_id_ed_adj,
5092 --                                       'UNED'  , ae1.ae_tax_link_id_uned  ,
5093 --                                       'N')
5094 --               AND (((l_tax_code_source = 'INVOICE')
5095 --                      OR (l_tax_code_source = 'ACTIVITY'))
5096 --                    OR ((l_tax_code_source = 'NONE')
5097 --                         AND (ae2.ae_collected_tax_ccid IS NOT NULL)
5098 --                         AND ((g_ae_rule_rec.line_amt_alloc <> 0)
5099 --                              OR (g_ae_rule_rec.line_acctd_amt_alloc <> 0)) ))
5100 --              );
5101 --}
5102 -- for a rev distribution ae_tax_link_id_act = ref_customer_trx_line_id
5103 -- if it exists a tax distribution with ae_tax_link_id equal to it
5104 arp_standard.debug('setlink 1');
5105 UPDATE /*+ index( a1 AR_AE_ALLOC_REC_GT_N4 ) */
5106    ar_ae_alloc_rec_gt a1
5107    SET (a1.ae_tax_link_id_act) =
5108        (SELECT /*+ index( a1 AR_AE_ALLOC_REC_GT_N4 ) */
5109             MAX(a2.ae_tax_link_id)
5110           FROM ar_ae_alloc_rec_gt a2
5111          WHERE a2.ae_id = g_id
5112            AND a2.ref_account_class = 'TAX'
5113            AND a1.ae_customer_trx_line_id = a2.ae_tax_link_id)
5114  WHERE a1.ae_id  = g_id
5115    AND a1.ref_account_class IN ('REV','UNEARN','UNBILL');
5116 
5117 /*MAINTAINTAXLINKID
5118 arp_standard.debug('setlink 2');
5119 -- for a tax distribution ae_tax_link_id_act = line_to_cust_trx_line_id
5120 -- if it exists a rev distribution with ref_customer_trx_line_id equal to it
5121 UPDATE ar_ae_alloc_rec_gt a1
5122    SET (a1.ae_tax_link_id_act) =
5123        (SELECT MAX(a2.ae_tax_link_id)
5124           FROM ar_ae_alloc_rec_gt a2
5125          WHERE a2.ae_id = g_id
5126            AND a2.ref_account_class IN ('REV','UNEARN','UNBILL')
5127            AND a1.ae_tax_link_id = a2.ae_customer_trx_line_id)
5128  WHERE a1.ae_id  = g_id
5129    AND a1.ref_account_class = 'TAX';
5130 */
5131 /*
5132 -- We need to update taxable_amount for tax line
5133 -- We need to create 2 buckets
5134 --  1: SUM(ae_pro_amt) where bucket IN (ED_LINE, UNED_LINE, APP_LINE)
5135 --  2: Individual elements ae_pro_amt where bucket IN (ED_TAX, UNED_TAX, APP_TAX)
5136 -- Group by ae_tax_link_id_act
5137 --  3: Proration using the elements 2 and allocation amt in 1
5138 -- ==> ae_pro_taxable_amt, ae_pro_split_taxable_amt,
5139 
5140 UPDATE ar_ae_alloc_rec_gt a1
5141    SET (a1.ae_tax_link_id_act,
5142         a1.AE_PRO_TAXABLE_AMT,
5143         a1.AE_PRO_TAXABLE_ACCTD_AMT,
5144         a1.AE_PRO_SPLIT_TAXABLE_AMT,
5145         a1.AE_PRO_SPLIT_TAXABLE_ACCTD_AMT,
5146         a1.AE_PRO_RECOV_TAXABLE_AMT,
5147         a1.AE_PRO_RECOV_TAXABLE_ACCTD_AMT) =
5148        (SELECT MAX(a2.ae_tax_link_id),
5149                MAX(a2.AE_PRO_AMT),
5150                MAX(a2.AE_PRO_ACCTD_AMT),
5151                MAX(a2.AE_PRO_AMT),
5152                MAX(a2.AE_PRO_ACCTD_AMT),
5153                MAX(a2.AE_PRO_AMT),
5154                MAX(a2.AE_PRO_ACCTD_AMT)
5155           FROM ar_ae_alloc_rec_gt a2
5156          WHERE a2.ae_id = g_id
5157            AND a2.ref_account_class = 'REV'
5158            AND a1.ae_tax_link_id = a2.ae_customer_trx_line_id)
5159  WHERE a1.ae_id  = g_id
5160    AND a1.ref_account_class = 'TAX';
5161 */
5162 
5163 arp_standard.debug(' setlink 3');
5164 --}
5165 
5166     IF PG_DEBUG in ('Y', 'C') THEN
5167        arp_standard.debug('ARP_ALLOCATION_PKG.Set_Rev_Links()-');
5168     END IF;
5169 
5170 EXCEPTION
5171   WHEN OTHERS THEN
5172      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Set_Rev_Links'||SQLERRM);
5173      RAISE;
5174 
5175 END Set_Rev_Links;
5176 
5177 /* ==========================================================================
5178  | PROCEDURE Get_Rules
5179  |
5180  | DESCRIPTION
5181  |      Gets the actual rule for the receivable activity
5182  |
5183  | SCOPE - PRIVATE
5184  |
5185  | PARAMETERS
5186  |    p_type_acct      IN    Indicates earned, unearned discount or adjustment
5187  |                           accounting
5188  *==========================================================================*/
5189 PROCEDURE Get_Rules(p_type_acct            IN     VARCHAR2,
5190                     p_gl_account_source    OUT NOCOPY   VARCHAR2,
5191                     p_tax_code_source      OUT NOCOPY   VARCHAR2,
5192                     p_tax_recoverable_flag OUT NOCOPY   VARCHAR2) IS
5193 
5194 BEGIN
5195    arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Rules()+');
5196 
5197  /*----------------------------------------------------------------------------+
5198   | Set Rules for Discounts, Adjustments and Finance charges                   |
5199   +----------------------------------------------------------------------------*/
5200    IF ((p_type_acct = 'ED_ADJ') OR (p_type_acct = 'PAY')) THEN
5201 
5202       p_gl_account_source    := g_ae_rule_rec.gl_account_source1    ;
5203       p_tax_code_source      := g_ae_rule_rec.tax_code_source1      ;
5204       p_tax_recoverable_flag := g_ae_rule_rec.tax_recoverable_flag1 ;
5205 
5206  /*----------------------------------------------------------------------------+
5207   | Set Rules for Unearned Discounts                                           |
5208   +----------------------------------------------------------------------------*/
5209    ELSIF p_type_acct = 'UNED' THEN
5210 
5211       p_gl_account_source           := g_ae_rule_rec.gl_account_source2    ;
5212       p_tax_code_source             := g_ae_rule_rec.tax_code_source2      ;
5213       p_tax_recoverable_flag        := g_ae_rule_rec.tax_recoverable_flag2 ;
5214 
5215    END IF;
5216 
5217    arp_standard.debug( 'ARP_ALLOCATION_PKG.Get_Rules()-');
5218 
5219 EXCEPTION
5220   WHEN OTHERS THEN
5221      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Get_Rules');
5222      RAISE;
5223 
5224 END Get_Rules;
5225 
5226 /* ==========================================================================
5227  | PROCEDURE Build_Lines
5228  |
5229  | DESCRIPTION
5230  |      Build actual accounting entries for Tax accounting for discounts and
5231  |      adjustments
5232  |
5233  | SCOPE - PRIVATE
5234  |
5235  | PARAMETERS
5236  |    p_type_acct      IN    Indicates earned, unearned discount or adjustment
5237  |                           accounting
5238  *==========================================================================*/
5239 PROCEDURE Build_Lines IS --(p_type_acct      IN VARCHAR2) IS
5240 
5241 l_gl_account_source    ar_receivables_trx.gl_account_source%TYPE    ;
5242 l_tax_code_source      ar_receivables_trx.tax_code_source%TYPE      ;
5243 l_tax_recoverable_flag ar_receivables_trx.tax_recoverable_flag%TYPE ;
5244 l_ae_line_init_rec     ar_ae_alloc_rec_gt%ROWTYPE                     ;
5245 
5246 BEGIN
5247 
5248     arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Lines()+');
5249 
5250 --  Get_Rules(p_type_acct            => p_type_acct,
5251 --            p_gl_account_source    => l_gl_account_source,
5252 --            p_tax_code_source      => l_tax_code_source,
5253 --            p_tax_recoverable_flag => l_tax_recoverable_flag);
5254 
5255  /*----------------------------------------------------------------------------+
5256   | Assign Currency Exchange rate information to initialisation record         |
5257   +----------------------------------------------------------------------------*/
5258    l_ae_line_init_rec.ae_id                        := g_id                                 ;
5259    l_ae_line_init_rec.ae_source_id                 := g_ae_doc_rec.source_id               ;
5260    l_ae_line_init_rec.ae_source_table              := g_ae_doc_rec.source_table            ;
5261    l_ae_line_init_rec.ae_currency_code             := g_cust_inv_rec.invoice_currency_code ;
5262    l_ae_line_init_rec.ae_currency_conversion_rate  := g_cust_inv_rec.exchange_rate         ;
5263    l_ae_line_init_rec.ae_currency_conversion_type  := g_cust_inv_rec.exchange_rate_type    ;
5264    l_ae_line_init_rec.ae_currency_conversion_date  := g_cust_inv_rec.exchange_date         ;
5265 
5266 --Set Third party details
5267    IF (g_cust_inv_rec.drawee_site_use_id IS NOT NULL) THEN --if Bill
5268       l_ae_line_init_rec.ae_third_party_id            := g_cust_inv_rec.drawee_id;
5269       l_ae_line_init_rec.ae_third_party_sub_id        := g_cust_inv_rec.drawee_site_use_id  ;
5270    ELSE
5271       l_ae_line_init_rec.ae_third_party_id            := g_cust_inv_rec.bill_to_customer_id   ;
5272       l_ae_line_init_rec.ae_third_party_sub_id        := g_cust_inv_rec.bill_to_site_use_id   ;
5273    END IF;
5274 
5275  /*----------------------------------------------------------------------------+
5276   | Build Revenue lines based on rules. For payments only deferred tax is to be|
5277   | moved so don not call build revenue routine at all                         |
5278   +----------------------------------------------------------------------------*/
5279     Build_Rev(p_gl_account_source     => l_gl_account_source     ,
5280               p_tax_code_source       => l_tax_code_source       ,
5281               p_tax_recoverable_flag  => l_tax_recoverable_flag  ,
5282               p_ae_line_init_rec      => l_ae_line_init_rec        );
5283 
5284  /*----------------------------------------------------------------------------+
5285   | Build Tax lines based on rules, if boundary condition then only deferred   |
5286   | tax lines will be created, with taxable off the original Invoice.          |
5287   +----------------------------------------------------------------------------*/
5288    Build_Tax(p_tax_code_source       => l_tax_code_source       ,
5289              p_tax_recoverable_flag  => l_tax_recoverable_flag  ,
5290              p_ae_line_init_rec      => l_ae_line_init_rec        );
5291 
5292  IF PG_DEBUG in ('Y', 'C') THEN
5293     arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Lines()-');
5294  END IF;
5295 
5296 EXCEPTION
5297   WHEN OTHERS THEN
5298      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Build_Lines');
5299      RAISE;
5300 
5301 END Build_Lines;
5302 
5303 /* ==========================================================================
5304  | PROCEDURE Build_Rev
5305  |
5306  | DESCRIPTION
5307  |    Build actual accounting entries for Revenue or line amounts for
5308  |    discounts and adjustments
5309  |
5310  | SCOPE - PRIVATE
5311  |
5312  | PARAMETERS
5313  |    p_type_acct             IN    Indicates earned, unearned discount or
5314  |                                  adjustment accounting
5315  |    p_gl_account_source     IN    Source of gl account
5316  |    p_tax_recoverable_flag  IN    Indicates whether TAX is recoverable
5317  |    p_ae_line_init_rec      IN    Initialization record contains details
5318  |                                  for exchange rate, source table, id
5319  |                                  common to all accounting entries
5320  *==========================================================================*/
5321 PROCEDURE Build_Rev(p_gl_account_source     IN ar_receivables_trx.gl_account_source%TYPE     ,
5322                     p_tax_code_source       IN ar_receivables_trx.tax_code_source%TYPE       ,
5323                     p_tax_recoverable_flag  IN ar_receivables_trx.tax_recoverable_flag%TYPE  ,
5324                     p_ae_line_init_rec      IN ar_ae_alloc_rec_gt%ROWTYPE                        ) IS
5325 
5326 l_ae_line_rec         ar_ae_alloc_rec_gt%ROWTYPE                  ;
5327 l_ctr                 BINARY_INTEGER                            ;
5328 l_actual_account      ar_distributions.code_combination_id%TYPE ;
5329 l_source_type         ar_distributions.source_type%TYPE         ;
5330 
5331 --PL/SQL table object,this collection is used for bulk processing of inserts
5332 TYPE ae_alloc_rec_gt_tab IS TABLE OF ar_ae_alloc_rec_gt%ROWTYPE INDEX BY BINARY_INTEGER;
5333 l_ae_alloc_rec_gt_tab ae_alloc_rec_gt_tab;
5334 l_ae_alloc_empty_tab  ae_alloc_rec_gt_tab;
5335 
5336 l_last_fetch     BOOLEAN := FALSE;
5337 l_bulk_index     NUMBER := 0;
5338 
5339 l_line_charges_amt          NUMBER := 0;
5340 l_line_charges_acctd_amt    NUMBER := 0;
5341 l_freight_charges_amt       NUMBER := 0;
5342 l_freight_charges_acctd_amt NUMBER := 0;
5343 
5344 /*-------------------------------------------------------------------------------+
5345  | Summarize Revenue lines, this routine would be executed even in the case where|
5346  | revenue base amounts are zero, so the single accounting entry to activity GL  |
5347  | Account is built later, however if tax code source is NONE then there may be  |
5348  | tax allocated to its revenue, hence need to summarize. Note for payments we   |
5349  | dont need to summarize as only deferred tax is moved, However no additional   |
5350  | accounting is required.                                                       |
5351  +-------------------------------------------------------------------------------*/
5352   CURSOR l_summarize_rev IS
5353   SELECT /*+ INDEX(ae1 AR_AE_ALLOC_REC_GT_N3) */
5354          ae1.ae_tax_link_id_act,
5355          ae1.ae_customer_trx_line_id,
5356          ae1.ae_cust_trx_line_gl_dist_id,
5357          ae1.ae_ref_line_id,
5358          ae1.ref_account_class,
5359          ae1.activity_bucket,
5360          ae1.ref_dist_ccid,
5361          ae1.ref_mf_dist_flag,
5362          DECODE(DECODE(ae1.activity_bucket,
5363                  'APP_LINE' , 'ACTIVITY_GL_ACCOUNT',
5364                  'APP_TAX'  , 'ACTIVITY_GL_ACCOUNT',
5365                  'APP_FRT'  , 'ACTIVITY_GL_ACCOUNT',
5366                  'APP_CHRG' , 'ACTIVITY_GL_ACCOUNT',
5367                  'ADJ_LINE' , g_ae_rule_rec.gl_account_source1,
5368                  'ADJ_TAX'  , g_ae_rule_rec.gl_account_source1,
5369                  'ADJ_FRT'  , g_ae_rule_rec.gl_account_source1,
5370                  'ADJ_CHRG' , g_ae_rule_rec.gl_account_source1,
5371                  'ED_LINE'  , g_ae_rule_rec.gl_account_source1,
5372                  'ED_TAX'   , g_ae_rule_rec.gl_account_source1,
5373                  'ED_FRT'   , g_ae_rule_rec.gl_account_source1,
5374                  'ED_CHRG'  , g_ae_rule_rec.gl_account_source1,
5375                  'UNED_LINE', g_ae_rule_rec.gl_account_source2,
5376                  'UNED_TAX' , g_ae_rule_rec.gl_account_source2,
5377                  'UNED_FRT' , g_ae_rule_rec.gl_account_source2,
5378                  'UNED_CHRG', g_ae_rule_rec.gl_account_source2,
5379                               g_ae_rule_rec.gl_account_source1),
5380                 'TAX_CODE_ON_INVOICE',DECODE(ae1.activity_bucket,
5381                                         'ADJ_LINE' , ae1.ae_override_ccid1,
5382                                         'ADJ_TAX'  , ae1.ae_override_ccid1,
5383                                         'ADJ_FRT'  , ae1.ae_override_ccid1,
5384                                         'ADJ_CHRG' , ae1.ae_override_ccid1,
5385                                         'ED_LINE'  , ae1.ae_override_ccid1,
5386                                         'ED_TAX'   , ae1.ae_override_ccid1,
5387                                         'ED_FRT'   , ae1.ae_override_ccid1,
5388                                         'ED_CHRG'  , ae1.ae_override_ccid1,
5389                                                      ae1.ae_override_ccid2),
5390                 'ACTIVITY_GL_ACCOUNT',DECODE(ae1.activity_bucket,
5391                                        'ADJ_LINE' , ae1.ae_override_ccid1,
5392                                        'ADJ_TAX'  , ae1.ae_override_ccid1,
5393                                        'ADJ_FRT'  , ae1.ae_override_ccid1,
5394                                        'ADJ_CHRG' , ae1.ae_override_ccid1,
5395                                        'ED_LINE'  , ae1.ae_override_ccid1,
5396                                        'ED_TAX'   , ae1.ae_override_ccid1,
5397                                        'ED_FRT'   , ae1.ae_override_ccid1,
5398                                        'ED_CHRG'  , ae1.ae_override_ccid1,
5399                                        'APP_LINE' , g_ae_rule_rec.receivable_account,
5400                                        'APP_TAX'  , g_ae_rule_rec.receivable_account,
5401                                        'APP_FRT'  , g_ae_rule_rec.receivable_account,
5402                                        'APP_CHRG' , g_ae_rule_rec.receivable_account,
5403                                                     ae1.ae_override_ccid2),
5404                 'REVENUE_ON_INVOICE',ae1.ae_code_combination_id,
5405                 '') actual_account,
5406          nvl(ae1.ae_pro_amt,0) ae_pro_amt,
5407          nvl(ae1.ae_pro_acctd_amt,0) ae_pro_acctd_amt,
5408          nvl(ae1.ae_pro_frt_chrg_amt,0) ae_pro_frt_chrg_amt,
5409          nvl(ae1.ae_pro_frt_chrg_acctd_amt,0) ae_pro_frt_chrg_acctd_amt,
5410          nvl(ae1.ae_from_pro_amt,0) ae_from_pro_amt,
5411          nvl(ae1.ae_from_pro_acctd_amt,0) ae_from_pro_acctd_amt,
5412          nvl(ae1.ae_from_pro_chrg_amt,0) ae_from_pro_chrg_amt,
5413          nvl(ae1.ae_from_pro_chrg_acctd_amt,0) ae_from_pro_chrg_acctd_amt
5414   FROM   ar_ae_alloc_rec_gt ae1
5415   WHERE ae1.ae_id = g_id
5416   AND   ae1.ae_account_class IN ('REVEARN',
5417                                  'FREIGHT',
5418                                  'REVUNEARN',
5419                                  'CHARGES');
5420 --  AND (((g_exec = 'ADJ')
5421 --       AND (ae_ref_line_id IS NULL))
5422 --       OR (p_type_acct = 'PAY' OR g_exec = 'ED' OR g_exec = 'UNED')) ;
5423 
5424 g_ae_summ_rev_tbl   l_summarize_rev%ROWTYPE;
5425 
5426 TYPE summarize_rev_cur IS TABLE OF l_summarize_rev%ROWTYPE;
5427 g_ae_summ_rev_blk_tbl summarize_rev_cur;
5428 
5429 BEGIN
5430 
5431  arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Rev()+');
5432 
5433  adj_code_combination_id := '';
5434 
5435  /*----------------------------------------------------------------------------+
5436   | Process all revenue lines to be built                                      |
5437   +----------------------------------------------------------------------------*/
5438    arp_standard.debug('Open l_summarize_rev +');
5439 
5440    OPEN l_summarize_rev;
5441 
5442    LOOP
5443    FETCH l_summarize_rev BULK COLLECT INTO g_ae_summ_rev_blk_tbl LIMIT g_bulk_fetch_rows;
5444 
5445    --reinitialize
5446    l_ae_alloc_rec_gt_tab := l_ae_alloc_empty_tab;
5447    l_bulk_index := 0;
5448 
5449       IF l_summarize_rev%NOTFOUND THEN
5450          l_last_fetch := TRUE;
5451       END IF;
5452 
5453    FOR i IN 1..g_ae_summ_rev_blk_tbl.count LOOP
5454       --Assign the record from PL/SQL table to a cursor variable,this variable
5455       --is used for accessing the current record data in the following code
5456       g_ae_summ_rev_tbl := g_ae_summ_rev_blk_tbl(i);
5457       l_actual_account  := g_ae_summ_rev_tbl.actual_account;
5458 
5459 
5460 /*----------------------------------------------------------------------------+
5461  | Populate source type for earned discounts                                  |
5462  +----------------------------------------------------------------------------*/
5463   IF g_ae_summ_rev_tbl.activity_bucket IN ('ED_LINE', 'ED_TAX', 'ED_FRT','ED_CHRG')  THEN
5464      l_source_type                 := 'EDISC' ;
5465     --Set the Activity GL Account
5466     -- l_actual_account := g_ae_rule_rec.code_combination_id1;
5467   /*----------------------------------------------------------------------------+
5468    | Populate source type for finance charges                                   |
5469    +----------------------------------------------------------------------------*/
5470   ELSIF g_ae_summ_rev_tbl.activity_bucket IN ('ADJ_LINE', 'ADJ_TAX', 'ADJ_FRT','ADJ_CHRG') THEN
5471 
5472      IF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
5473            l_source_type              := 'ADJ';
5474      ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
5475            l_source_type              := 'FINCHRG';
5476      END IF;
5477 
5478      IF g_ae_summ_rev_tbl.ae_cust_trx_line_gl_dist_id = -9 THEN
5479      --For adjustment boundary ccid
5480      adj_boundary_account
5481      (p_receivables_trx_id   => g_receivables_trx_id,
5482       p_bucket               => g_ae_summ_rev_tbl.activity_bucket,
5483       p_ctlgd_id             => g_ae_summ_rev_tbl.ae_cust_trx_line_gl_dist_id,
5484       x_ccid                 => l_actual_account);
5485      END IF;
5486 
5487     --Set the Activity GL Account
5488     IF l_actual_account IS NULL THEN
5489      l_actual_account := g_ae_rule_rec.code_combination_id1;
5490     END IF;
5491 
5492 
5493  /*----------------------------------------------------------------------------+
5494   | Populate source type for unearned discounts                                |
5495   +----------------------------------------------------------------------------*/
5496   ELSIF g_ae_summ_rev_tbl.activity_bucket IN ('UNED_LINE', 'UNED_TAX', 'UNED_FRT','UNED_CHRG') THEN
5497      l_source_type                 := 'UNEDISC' ;
5498   END IF;
5499 
5500 
5501 
5502 
5503     /*----------------------------------------------------------------------------+
5504      | Initialize record with exchange rate, source id, table details for new line|
5505      +----------------------------------------------------------------------------*/
5506        l_ae_line_rec                             := p_ae_line_init_rec;
5507        l_ae_line_rec.ae_line_type                := l_source_type;
5508        l_ae_line_rec.ref_dist_ccid               := g_ae_summ_rev_tbl.ref_dist_ccid;
5509        l_ae_line_rec.ref_mf_dist_flag            := g_ae_summ_rev_tbl.ref_mf_dist_flag;
5510 
5511      /*----------------------------------------------------------------------------+
5512       | Set Tax link id                                                            |
5513       +----------------------------------------------------------------------------*/
5514        l_ae_line_rec.ae_tax_link_id              := g_ae_summ_rev_tbl.ae_tax_link_id_act;
5515        l_ae_line_rec.ae_customer_trx_line_id     := g_ae_summ_rev_tbl.ae_customer_trx_line_id;
5516        l_ae_line_rec.ae_cust_trx_line_gl_dist_id := g_ae_summ_rev_tbl.ae_cust_trx_line_gl_dist_id;
5517        l_ae_line_rec.ae_ref_line_id              := g_ae_summ_rev_tbl.ae_ref_line_id;
5518        l_ae_line_rec.ae_tax_link_id              := g_ae_summ_rev_tbl.ae_tax_link_id_act;
5519        l_ae_line_rec.ref_account_class           := g_ae_summ_rev_tbl.ref_account_class;
5520        l_ae_line_rec.activity_bucket             := g_ae_summ_rev_tbl.activity_bucket;
5521        l_ae_line_rec.ae_account                  := g_ae_rule_rec.receivable_account;
5522 
5523 
5524      arp_standard.debug(' g_ae_rule_rec.receivable_account 1:'||g_ae_rule_rec.receivable_account);
5525      arp_standard.debug(' p_gl_account_source :'||p_gl_account_source);
5526      arp_standard.debug(' g_override1 :'||g_override1);
5527      arp_standard.debug(' g_override2 :'||g_override2);
5528      arp_standard.debug(' g_ae_doc_rec.other_flag :'||g_ae_doc_rec.other_flag);
5529      arp_standard.debug(' l_actual_account :'||l_actual_account);
5530 
5531      /*----------------------------------------------------------------------------+
5532       | Use Revenue or Net Expense accounts based on Rules                         |
5533       +----------------------------------------------------------------------------*/
5534        IF ((the_gl_account_source(g_ae_summ_rev_tbl.activity_bucket) = 'ACTIVITY_GL_ACCOUNT') OR
5535            (the_gl_account_source(g_ae_summ_rev_tbl.activity_bucket) = 'TAX_CODE_ON_INVOICE'))
5536           AND (((g_ae_summ_rev_tbl.activity_bucket IN ('ED_LINE','ED_TAX','ED_FRT','ED_CHRG',
5537                                                        'ADJ_FRT','ADJ_CHRG','ADJ_LINE')) --BUG#5245153
5538                  AND (nvl(g_override1,'N') = 'Y'))
5539                 OR (( g_ae_summ_rev_tbl.activity_bucket IN ('UNED_LINE','UNED_TAX','UNED_FRT','UNED_CHRG'))
5540                     AND (nvl(g_override2, 'N') = 'Y'))) THEN
5541 
5542            IF l_actual_account IS NULL THEN
5543               RAISE invalid_ccid_error;
5544            END IF;
5545 
5546          /*------------------------------------------------------------------------------------+
5547           | Substitute balancing segment for Net Expense account or Activity Gl Account        |
5548           | in the case of Deposits and Guarantees the accounting is derived by Autoaccounting |
5549           | there is no need to do further processing for accounting.                          |
5550           +------------------------------------------------------------------------------------*/
5551             IF (g_ae_doc_rec.other_flag IN ('COMMITMENT', 'CHARGEBACK', 'CBREVERSAL')) THEN
5552                arp_standard.debug('Build_Rev: ' || 'Commitment account derived by Autoaccounting');
5553                l_ae_line_rec.ae_account  := l_actual_account;
5554 
5555             ELSE
5556 	    -- Bugfix 1948917.
5557 	    IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
5558                Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id             ,
5559                                p_original_ccid => l_actual_account                ,
5560                                p_subs_ccid     => g_ae_rule_rec.receivable_account,
5561                                p_actual_ccid   => l_ae_line_rec.ae_account             );
5562 
5563           --BUG#5245153
5564           IF (l_ae_line_rec.ae_account <> l_actual_account) AND
5565              (the_gl_account_source(g_ae_summ_rev_tbl.activity_bucket) = 'ACTIVITY_GL_ACCOUNT') AND
5566              (g_ae_summ_rev_tbl.activity_bucket = 'ADJ_LINE') AND
5567              (g_ae_doc_rec.source_table = 'ADJ')
5568           THEN
5569              IF get_adj_act_ccid <> l_ae_line_rec.ae_account THEN
5570                BEGIN
5571                 UPDATE ar_adjustments
5572                    SET code_Combination_id = l_ae_line_rec.ae_account
5573                  WHERE adjustment_id = g_ae_doc_rec.document_id;
5574                  g_adj_act_gl_acct_ccid := l_ae_line_rec.ae_account;
5575                EXCEPTION
5576                  WHEN no_data_found THEN g_adj_act_gl_acct_ccid := l_ae_line_rec.ae_account;
5577                END;
5578              END IF;
5579           END IF;
5580 
5581 	    ELSE
5582 	       l_ae_line_rec.ae_account := l_actual_account;
5583 	    END IF;
5584 
5585             END IF;
5586 
5587 
5588        ELSIF the_gl_account_source(g_ae_summ_rev_tbl.activity_bucket) = 'REVENUE_ON_INVOICE' THEN
5589                l_ae_line_rec.ae_account := l_actual_account;
5590        ELSIF  the_gl_account_source(g_ae_summ_rev_tbl.activity_bucket) = 'ACTIVITY_GL_ACCOUNT'
5591            AND  g_ae_summ_rev_tbl.activity_bucket IN ('APP_LINE','APP_TAX','APP_FRT','APP_CHRG')
5592                 --{BUG#5122552
5593 -- BUG#5245153 ,'ADJ_FRT','ADJ_CHRG','ADJ_LINE')
5594                 --}
5595            THEN
5596                l_ae_line_rec.ae_account := l_actual_account;
5597        END IF;
5598 
5599       /*----------------------------------------------------------------------------+
5600        | Set the activity ccid which will be stamped on adj.code_combination_id     |
5601        +----------------------------------------------------------------------------*/
5602         IF adj_code_combination_id IS NULL and p_gl_account_source = 'ACTIVITY_GL_ACCOUNT'
5603            AND (((nvl(g_ae_summ_rev_tbl.ae_pro_amt,0) <> 0)
5604                   OR (nvl(g_ae_summ_rev_tbl.ae_pro_acctd_amt,0) <> 0))
5605                 OR ((nvl(g_ae_summ_rev_tbl.ae_pro_frt_chrg_amt,0) <> 0)
5606                      OR (nvl(g_ae_summ_rev_tbl.ae_pro_frt_chrg_acctd_amt,0) <> 0)))
5607         THEN
5608            adj_code_combination_id := l_ae_line_rec.ae_account;
5609         END IF;
5610 
5611        /*----------------------------------------------------------------------------+
5612         | Assign Accounting Debits and Credits based on prorated amount signs        |
5613         +----------------------------------------------------------------------------*/
5614          IF ((nvl(g_ae_summ_rev_tbl.ae_pro_amt,0) <> 0)
5615                                      OR (nvl(g_ae_summ_rev_tbl.ae_pro_acctd_amt,0) <> 0)) THEN
5616 
5617            --IF p_type_acct = 'PAY' THEN
5618            IF g_ae_summ_rev_tbl.activity_bucket IN ('APP_LINE','APP_TAX','APP_FRT','APP_CHRG') THEN
5619 
5620               -- ARALLOCB creates the REC account entries for APPS so for a cash application
5621               -- the accounting needs to be positive CR REC
5622               -- As ARPDDB passed the detail distributions as negative, we need to multiply by -1
5623               Create_Debits_Credits(g_ae_summ_rev_tbl.ae_pro_amt       * -1,
5624                                     g_ae_summ_rev_tbl.ae_pro_acctd_amt * -1,
5625                                     ''                                 ,
5626                                     ''                                 ,
5627                                     g_ae_summ_rev_tbl.ae_from_pro_amt  * -1,
5628                                     g_ae_summ_rev_tbl.ae_from_pro_acctd_amt * -1,
5629                                     l_ae_line_rec );
5630 
5631               l_ae_line_rec.ae_line_type := 'REC';
5632 
5633 	       -- Bug 6598080
5634               IF g_ae_doc_rec.called_from = 'WRAPPER' THEN
5635                  l_ae_line_rec.ae_account := g_ae_code_combination_id_app;
5636               END IF;
5637 
5638             ELSE
5639              -- in the case of ADJ, ARALLOCB creates the Write off accounting
5640              -- for negative adjustment <=> CR REC <=> DB WO, in this case the distribution should negative
5641              -- As ARPDDB passes the detail distributions in the sign of the header adjustment that is negative
5642              -- so no need to multiply by -1
5643 
5644              Create_Debits_Credits(g_ae_summ_rev_tbl.ae_pro_amt        ,
5645                                    g_ae_summ_rev_tbl.ae_pro_acctd_amt  ,
5646                                    ''                                  ,
5647                                    ''                                  ,
5648                                     g_ae_summ_rev_tbl.ae_from_pro_amt       ,
5649                                     g_ae_summ_rev_tbl.ae_from_pro_acctd_amt ,
5650                                    l_ae_line_rec );
5651            END IF;
5652 
5653        /*----------------------------------------------------------------------------+
5654         | Assign built Revenue line to global lines table                            |
5655         +----------------------------------------------------------------------------*/
5656              --Commented out the call,Instead will do bulk binding to insert the records
5657 	     --Assign_Elements(l_ae_line_rec);
5658              l_ae_line_rec.ae_id :=  g_id;
5659 	     l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
5660 
5661 	     l_bulk_index := l_bulk_index + 1;
5662 	     l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
5663 
5664       END IF; --End if ae_pro_amt or ae_pro_acctd_amt is non zero
5665 
5666      END LOOP;--end of pl/sql table loop
5667 
5668      /**In procedure Assign_Elements,g_ae_ctr is incremented for each record
5669         inserted.But no code segment in this package uses the value of the
5670 	variable,thus not incremented the variable here.*/
5671      FORALL i IN l_ae_alloc_rec_gt_tab.first..l_ae_alloc_rec_gt_tab.last
5672 	INSERT INTO ar_ae_alloc_rec_gt VALUES l_ae_alloc_rec_gt_tab(i);
5673 
5674      --Exit Last fetch
5675      IF l_last_fetch THEN
5676             EXIT;
5677      END IF;
5678 
5679        --Now build freight and charges if any
5680 --         IF ((nvl(g_ae_summ_rev_tbl.ae_pro_frt_chrg_amt,0) <> 0)
5681 --                                     OR (nvl(g_ae_summ_rev_tbl.ae_pro_frt_chrg_acctd_amt,0) <> 0)) THEN
5682 --          --Link id null always for freight and charges
5683 --             l_ae_line_rec.ae_tax_link_id := '';
5684 --             IF p_type_acct = 'PAY' THEN
5685            /*----------------------------------------------------------------------------+
5686             | Assign Accounting Debits and Credits based on prorated amount signs        |
5687             +----------------------------------------------------------------------------*/
5688 --                Create_Debits_Credits(g_ae_summ_rev_tbl.ae_pro_chrg_amt * -1          ,
5689 --                                      g_ae_summ_rev_tbl.ae_pro_chrg_acctd_amt * -1    ,
5690 --                                      ''                                         ,
5691 --                                      ''                                         ,
5692 --                                      g_ae_summ_rev_tbl.ae_from_pro_chrg_amt * -1          ,
5693 --                                      g_ae_summ_rev_tbl.ae_from_pro_chrg_acctd_amt * -1    ,
5694 --                                      l_ae_line_rec );
5695 --                l_ae_line_rec.ae_line_type := 'REC';
5696 --
5697 --             ELSE
5698 --}
5699            /*----------------------------------------------------------------------------+
5700             | Assign Accounting Debits and Credits based on prorated amount signs        |
5701             +----------------------------------------------------------------------------*/
5702 --             Create_Debits_Credits(g_ae_summ_rev_tbl.ae_pro_frt_chrg_amt        ,
5703 --                                   g_ae_summ_rev_tbl.ae_pro_frt_chrg_acctd_amt  ,
5704 --                                   ''                                         ,
5705 --                                   ''                                         ,
5706 --                                   --HYU--{
5707 --                                   g_ae_summ_rev_tbl.ae_from_pro_chrg_amt,
5708 --                                   g_ae_summ_rev_tbl.ae_from_pro_chrg_acctd_amt,
5709 --                                   l_ae_line_rec );
5710 --
5711 --           END IF; --HYUDETUPT
5712            --Create the freight and charges
5713 --             Assign_Elements(l_ae_line_rec);
5714 --         END IF; --freight and charges prorates are not zero
5715 
5716    END LOOP;
5717 
5718 -- END IF; --End if Build Revenue lines
5719 
5720   arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Rev()-');
5721 
5722 EXCEPTION
5723   WHEN invalid_ccid_error THEN
5724      arp_standard.debug('Invalid Tax ccid - ARP_ALLOCATION_PKG.Build_Rev' );
5725      fnd_message.set_name('AR','AR_INVALID_TAX_ACCOUNT');
5726      RAISE;
5727 
5728   WHEN OTHERS THEN
5729      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Build_Rev');
5730      RAISE;
5731 
5732 END Build_Rev;
5733 
5734 
5735 /*--------------------------------------------------------------------------+
5736  | Tax distrition ccid substitute ccid                                      |
5737  +--------------------------------------------------------------------------*/
5738 PROCEDURE substite_tax_bal_seg
5739 (p_line_type       IN     VARCHAR2,
5740  p_gas             IN     VARCHAR2,
5741  p_tcs             IN     VARCHAR2,
5742  p_tax_rec_flag    IN     VARCHAR2,
5743  p_ccid            IN     NUMBER,
5744  x_ccid            OUT  NOCOPY  NUMBER)
5745 IS
5746   l_call   VARCHAR2(1) := 'Y';
5747 BEGIN
5748   arp_standard.debug('substite_tax_bal_seg       +');
5749   arp_standard.debug('p_line_type       :'||p_line_type);
5750   arp_standard.debug('p_gas             :'||p_gas);
5751   arp_standard.debug('p_tcs             :'||p_tcs);
5752   arp_standard.debug('p_tax_rec_flag    :'||p_tax_rec_flag);
5753   arp_standard.debug('p_ccid            :'||p_ccid);
5754 
5755   x_ccid  := p_ccid;
5756 
5757    IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
5758 
5759      IF p_line_type IN ('TAX', 'DEFERRED_TAX') THEN
5760          l_call := 'N';
5761      ELSE
5762 
5763        IF     p_gas    = 'ACTIVITY_GL_ACCOUNT' THEN
5764          IF     p_tcs    = 'NONE'                THEN     l_call := 'Y';
5765          ELSIF  p_tcs    = 'ACTIVITY'            THEN     l_call := 'Y';
5766          ELSIF  p_tcs    = 'INVOICE'             THEN
5767              IF   p_tax_rec_flag = 'Y'  THEN  l_call := 'N';
5768              ELSE  l_call := 'Y';
5769              END IF;
5770          END IF;
5771        ELSIF  p_gas    = 'REVENUE_ON_INVOICE'  THEN
5772          IF     p_tcs    = 'NONE'                THEN     l_call := 'N';
5773          ELSIF  p_tcs    = 'ACTIVITY'            THEN     l_call := 'Y';
5774          ELSIF  p_tcs    = 'INVOICE'             THEN
5775              IF   p_tax_rec_flag = 'Y'  THEN  l_call := 'N';
5776              ELSE  l_call := 'Y';
5777              END IF;
5778          END IF;
5779        ELSIF  p_gas    = 'TAX_CODE_ON_INVOICE'  THEN
5780          IF     p_tcs    = 'NONE'                THEN     l_call := 'Y';
5781          ELSIF  p_tcs    = 'ACTIVITY'            THEN     l_call := 'Y';
5782          ELSIF  p_tcs    = 'INVOICE'             THEN
5783              IF   p_tax_rec_flag = 'Y'  THEN  l_call := 'N';
5784              ELSE  l_call := 'Y';
5785              END IF;
5786          END IF;
5787        END IF;
5788     END IF;
5789     IF l_call = 'Y' THEN
5790         Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id             ,
5791                         p_original_ccid => p_ccid                          ,
5792                         p_subs_ccid     => g_ae_rule_rec.receivable_account,
5793                         p_actual_ccid   => x_ccid             );
5794     ELSE
5795        x_ccid := p_ccid;
5796     END IF;
5797   ELSE
5798     x_ccid := p_ccid;
5799   END IF;
5800   arp_standard.debug('x_ccid            :'||x_ccid);
5801   arp_standard.debug('substite_tax_bal_seg       -');
5802 END;
5803 
5804 
5805 /* ==========================================================================
5806  | PROCEDURE Build_Tax
5807  |
5808  | DESCRIPTION
5809  |    Build actual accounting entries for Tax amounts for discounts and
5810  |    adjustments
5811  |
5812  | SCOPE - PRIVATE
5813  |
5814  | PARAMETERS
5815  |    p_type_acct             IN    Indicates earned, unearned discount or
5816  |                                  adjustment accounting
5817  |    p_tax_code_source       IN    Source of gl account
5818  |    p_tax_recoverable_flag  IN    Indicates whether TAX is recoverable
5819  |    p_ae_line_init_rec      IN    Initialization record contains details
5820  |                                  for exchange rate, source table, id
5821  |                                  common to all accounting entries
5822  | History
5823  |   21-NOV-2003  Herve Yu   from_amount_dr      , from_amount_cr
5824  |                           from_acctd_amount_dr, from_acctd_amount_cr
5825  *==========================================================================*/
5826 PROCEDURE Build_Tax(p_tax_code_source       IN ar_receivables_trx.tax_code_source%TYPE       ,
5827                     p_tax_recoverable_flag  IN ar_receivables_trx.tax_recoverable_flag%TYPE  ,
5828                     p_ae_line_init_rec      IN ar_ae_alloc_rec_gt%ROWTYPE                        ) IS
5829 
5830 l_ae_line_rec         ar_ae_alloc_rec_gt%ROWTYPE                  ;
5831 l_ae_line_rec_empty   ar_ae_alloc_rec_gt%ROWTYPE                  ;
5832 l_ctr                 BINARY_INTEGER                            ;
5833 l_actual_account      ar_distributions.code_combination_id%TYPE ;
5834 l_org_inv_tax_code_id ar_distributions.tax_code_id%TYPE         ;
5835 l_org_inv_loc_seg_id  ar_distributions.location_segment_id%TYPE ;
5836 l_taxable_amt         NUMBER;
5837 l_taxable_acctd_amt   NUMBER;
5838 
5839 l_source_type_secondary ar_distributions.source_type_secondary%TYPE;
5840 
5841 --PL/SQL table object,this collection is used for bulk processing of inserts
5842 TYPE ae_alloc_rec_gt_tab IS TABLE OF ar_ae_alloc_rec_gt%ROWTYPE INDEX BY  BINARY_INTEGER;
5843 l_ae_alloc_rec_gt_tab ae_alloc_rec_gt_tab;
5844 l_ae_alloc_empty_tab  ae_alloc_rec_gt_tab;
5845 
5846 l_last_fetch     BOOLEAN := FALSE;
5847 l_bulk_index     NUMBER := 0;
5848 
5849 CURSOR summarize_tax IS
5850 SELECT /*+ INDEX(ae1 AR_AE_ALLOC_REC_GT_N3) */
5851        nvl(ae_pro_amt,0) ae_pro_amt,
5852        nvl(ae_pro_acctd_amt,0) ae_pro_acctd_amt,
5853        nvl(ae_pro_taxable_amt,0) ae_pro_taxable_amt,
5854        nvl(ae_pro_taxable_acctd_amt,0) ae_pro_taxable_acctd_amt,
5855        nvl(ae_from_pro_amt,0) ae_from_pro_amt,
5856        nvl(ae_from_pro_acctd_amt,0) ae_from_pro_acctd_amt,
5857        nvl(ae_pro_split_taxable_amt,nvl(ae_pro_taxable_amt,0)) ae_pro_split_taxable_amt,
5858        nvl(ae_pro_split_taxable_acctd_amt,nvl(ae_pro_taxable_acctd_amt,0)) ae_pro_split_taxable_acctd_amt,
5859        nvl(ae_pro_recov_taxable_amt,nvl(ae_pro_split_taxable_amt,nvl(ae_pro_taxable_amt,0)))
5860        ae_pro_recov_taxable_amt,
5861        nvl(ae_pro_recov_taxable_acctd_amt,nvl(ae_pro_split_taxable_acctd_amt,nvl(ae_pro_taxable_acctd_amt,0)))
5862        ae_pro_recov_taxable_acctd_amt,
5863        DECODE(ae1.ae_collected_tax_ccid,
5864               '', DECODE( DECODE(activity_bucket, 'ADJ_LINE' , g_ae_rule_rec.tax_code_source1,
5865                             'ADJ_TAX'  , g_ae_rule_rec.tax_code_source1,
5866                             'ADJ_FRT'  , g_ae_rule_rec.tax_code_source1,
5867                             'ADJ_CHRG' , g_ae_rule_rec.tax_code_source1,
5868                             'APP_LINE' , 'INVOICE',
5869                             'APP_TAX'  , 'INVOICE',
5870                             'APP_FRT'  , 'INVOICE',
5871                             'APP_CHRG' , 'INVOICE',
5872                             'ED_LINE'  , g_ae_rule_rec.tax_code_source1,
5873                             'ED_TAX'   , g_ae_rule_rec.tax_code_source1,
5874                             'ED_FRT'   , g_ae_rule_rec.tax_code_source1,
5875                             'ED_CHRG'  , g_ae_rule_rec.tax_code_source1,
5876                             'UNED_LINE', g_ae_rule_rec.tax_code_source2,
5877                             'UNED_TAX' , g_ae_rule_rec.tax_code_source2,
5878                             'UNED_FRT' , g_ae_rule_rec.tax_code_source2,
5879                             'UNED_CHRG', g_ae_rule_rec.tax_code_source2,
5880                                          g_ae_rule_rec.tax_code_source1),
5881                         'INVOICE',
5882                         DECODE(DECODE(activity_bucket,
5883                                      'ADJ_LINE' , g_ae_rule_rec.tax_recoverable_flag1,
5884                                      'ADJ_TAX'  , g_ae_rule_rec.tax_recoverable_flag1,
5885                                      'ADJ_FRT'  , g_ae_rule_rec.tax_recoverable_flag1,
5886                                      'ADJ_CHRG' , g_ae_rule_rec.tax_recoverable_flag1,
5887                                      'APP_LINE' , 'Y',
5888                                      'APP_TAX'  , 'Y',
5889                                      'APP_FRT'  , 'Y',
5890                                      'APP_CHRG' , 'Y',
5891                                      'ED_LINE'  , g_ae_rule_rec.tax_recoverable_flag1,
5892                                      'ED_TAX'   , g_ae_rule_rec.tax_recoverable_flag1,
5893                                      'ED_FRT'   , g_ae_rule_rec.tax_recoverable_flag1,
5894                                      'ED_CHRG'  , g_ae_rule_rec.tax_recoverable_flag1,
5895                                      'UNED_LINE', g_ae_rule_rec.tax_recoverable_flag2,
5896                                      'UNED_TAX' , g_ae_rule_rec.tax_recoverable_flag2,
5897                                      'UNED_FRT' , g_ae_rule_rec.tax_recoverable_flag2,
5898                                      'UNED_CHRG', g_ae_rule_rec.tax_recoverable_flag2,
5899                                                   g_ae_rule_rec.tax_recoverable_flag1),
5900                                       'Y', ae1.ae_code_combination_id,
5901                                           ''),
5902                           'NONE',ae1.ae_code_combination_id,
5903                           ''),
5904               ae1.ae_code_combination_id) ae_code_combination_id,
5905        ae1.ae_collected_tax_ccid,
5906        DECODE(ae1.activity_bucket,
5907                     'ADJ_LINE' ,ae1.ae_override_ccid1,
5908                     'ADJ_TAX'  ,ae1.ae_override_ccid1,
5909                     'ADJ_FRT'  ,ae1.ae_override_ccid1,
5910                     'ADJ_CHRG' ,ae1.ae_override_ccid1,
5911                     'ED_LINE'  ,ae1.ae_override_ccid1,
5912                     'ED_TAX'   ,ae1.ae_override_ccid1,
5913                     'ED_FRT'   ,ae1.ae_override_ccid1,
5914                     'ED_CHRG'  ,ae1.ae_override_ccid1,
5915                     'UNED_LINE',ae1.ae_override_ccid2,
5916                     'UNED_TAX' ,ae1.ae_override_ccid2,
5917                     'UNED_FRT' ,ae1.ae_override_ccid2,
5918                     'UNED_CHRG',ae1.ae_override_ccid2,
5919                '') actual_account,
5920        ae1.ae_tax_type,
5921        ae1.ae_tax_id,
5922        NVL(ae1.ae_tax_group_code_id,'') ae_tax_group_code_id,
5923        NVL(ae1.ae_tax_link_id_act,'') ae_tax_link_id_act,
5924        ae1.ae_customer_trx_line_id,
5925        ae1.ae_cust_trx_line_gl_dist_id,
5926        ae1.ae_ref_line_id,
5927        ae1.ref_account_class,
5928        ae1.activity_bucket,
5929        --{ref_dist_ccid
5930        ae1.ref_dist_ccid,
5931        ae1.ref_mf_dist_flag,
5932        --}
5933        ae1.ae_adj_ccid
5934       ,d.code_combination_id actual_tax_ccid
5935       ,ae1.ae_unedisc_ccid
5936       ,ae1.ae_edisc_ccid
5937 FROM ar_ae_alloc_rec_gt ae1,
5938      ra_cust_trx_line_gl_dist d
5939 WHERE ae1.ae_id = g_id
5940 AND ae1.ae_account_class = 'TAX'
5941 AND ae1.ae_cust_trx_line_gl_dist_id = d.cust_trx_line_gl_dist_id(+);
5942 
5943 g_ae_summ_tax_tbl       summarize_tax%ROWTYPE;
5944 
5945 TYPE summarize_tax_cur IS TABLE OF summarize_tax%ROWTYPE;
5946 g_ae_summ_tax_blk_tbl summarize_tax_cur := summarize_tax_cur();
5947 
5948 l_multi_factor      NUMBER := 1;
5949 l_actual_tax_ccid   NUMBER;
5950 BEGIN
5951 
5952  arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Tax()+');
5953  arp_standard.debug( 'p_tax_code_source:'||p_tax_code_source);
5954  arp_standard.debug( 'p_tax_recoverable_flag:'||p_tax_recoverable_flag);
5955 
5956 
5957 
5958 -- IF (((g_ae_rule_rec.tax_amt_alloc <> 0) OR (g_ae_rule_rec.tax_acctd_amt_alloc <> 0))
5959 --    OR ((g_ae_rule_rec.line_amt_alloc <> 0) OR (g_ae_rule_rec.line_acctd_amt_alloc <>0))) THEN
5960  /*------------------------------------------------------------------------------+
5961   | Tax due to activity tax code is independent of the original tax on invoice   |
5962   | hence this tax needs to be built first to the Non Recoverable Account on the |
5963   | activity Tax Code. With the taxable being the line amount or accounted amount|
5964   | This condition occurs only if tax distributions cannot be used for allocation|
5965   +------------------------------------------------------------------------------*/
5966 --   IF ((p_tax_code_source = 'ACTIVITY') AND ((g_bound_tax) OR (g_bound_activity))) THEN
5967       /*--------------------------------------------------------------------------------+
5968        | Initialize record to create new line with exchange rate defaulting from Invoice|
5969        +--------------------------------------------------------------------------------*/
5970 --        l_ae_line_rec := p_ae_line_init_rec;
5971       /*----------------------------------------------------------------------------+
5972        | Assign Tax link id, and tax id for Tax lines                               |
5973        +----------------------------------------------------------------------------*/
5974 --        IF ((g_ae_rule_rec.line_amt_alloc <> 0) OR (g_ae_rule_rec.line_acctd_amt_alloc <> 0)) THEN
5975 --           IF p_type_acct = 'ED_ADJ' THEN
5976 --              l_ae_line_rec.ae_tax_link_id := g_ed_adj_activity_link;
5977 --           ELSIF p_type_acct = 'UNED' THEN
5978 --              l_ae_line_rec.ae_tax_link_id := g_uned_activity_link;
5979 --           END IF;
5980 --        END IF;
5981        /*----------------------------------------------------------------------------+
5982         | Override the tax code id as for rule ACTIVITY the tax code must be from the|
5983         | receivable activity                                                        |
5984         +----------------------------------------------------------------------------*/
5985 --         IF ((p_tax_code_source = 'ACTIVITY') AND (p_type_acct = 'ED_ADJ')) THEN
5986 --            l_ae_line_rec.ae_tax_code_id         := g_ae_rule_rec.act_vat_tax_id1;
5987 --            l_ae_line_rec.ae_location_segment_id := '';
5988 --         ELSIF ((p_tax_code_source = 'ACTIVITY') AND (p_type_acct = 'UNED')) THEN
5989 --            l_ae_line_rec.ae_tax_code_id         := g_ae_rule_rec.act_vat_tax_id2;
5990 --            l_ae_line_rec.ae_location_segment_id := '';
5991 --         END IF;
5992        /*----------------------------------------------------------------------------+
5993         | Populate the source type for earned discounts non recoverable account      |
5994         +----------------------------------------------------------------------------*/
5995 --         IF p_type_acct = 'ED_ADJ' THEN
5996 --            IF g_ae_doc_rec.source_table = 'RA' THEN
5997 --               l_ae_line_rec.ae_line_type    := 'EDISC_NON_REC_TAX' ;
5998           /*----------------------------------------------------------------------------+
5999            | Populate the source type for adjustment non recoverable account            |
6000            +----------------------------------------------------------------------------*/
6001 --            ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
6002 --                  l_ae_line_rec.ae_line_type := 'ADJ_NON_REC_TAX';
6003           /*----------------------------------------------------------------------------+
6004            | Populate the source type for finance charges non recoverable account       |
6005            +----------------------------------------------------------------------------*/
6006 --            ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
6007 --                  l_ae_line_rec.ae_line_type := 'FINCHRG_NON_REC_TAX';
6008 --            END IF;
6009            --Set account
6010 --            l_actual_account := g_ae_rule_rec.act_tax_non_rec_ccid1;
6011         /*----------------------------------------------------------------------------+
6012          | Populate the source type for unearned discounts non recoverable account    |
6013          +----------------------------------------------------------------------------*/
6014 --         ELSIF p_type_acct = 'UNED' THEN
6015 --               l_ae_line_rec.ae_line_type    := 'UNEDISC_NON_REC_TAX' ;
6016               --Set account for unearned discounts
6017 --               l_actual_account := g_ae_rule_rec.act_tax_non_rec_ccid2;
6018 --         END IF;
6019        /*-----------------------------------------------------------------------------+
6020         | Level 2 validation in case the non recoverable tax account setup on activity|
6021         | tax code is null - request user to set it up.                               |
6022         +-----------------------------------------------------------------------------*/
6023 --         IF l_actual_account IS NULL THEN
6024 --            arp_standard.debug('Activity Non Recoverable Account ');
6025 --            RAISE invalid_ccid_error;
6026 --         END IF;
6027        /*----------------------------------------------------------------------------+
6028         | Create first accounting entry for Debit/Credit to Non Recoverable/Override |
6029         | Account, Setup Non Recoverable account for debits/credits                  |
6030         | Substitute balancing segment for Non Recoverable Tax Account               |
6031         +----------------------------------------------------------------------------*/
6032 	-- Bugfix 1948917.
6033 --	IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N'THEN
6034 --          Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id              ,
6035 --                          p_original_ccid => l_actual_account                 ,
6036 --                          p_subs_ccid     => g_ae_rule_rec.receivable_account ,
6037 --                          p_actual_ccid   => l_ae_line_rec.ae_account             );
6038 --	ELSE
6039 --	  l_ae_line_rec.ae_account := l_actual_account;
6040 --	END IF;
6041         /*----------------------------------------------------------------------------+
6042          | Set taxable amounts and accounted amounts, if there is no line amount to   |
6043          | then the taxable is calculated using the original tax line on the invoice  |
6044          +----------------------------------------------------------------------------*/
6045 --          l_taxable_amt        := g_ae_rule_rec.line_amt_alloc;
6046 --          l_taxable_acctd_amt  := g_ae_rule_rec.line_acctd_amt_alloc;
6047         /*----------------------------------------------------------------------------+
6048          | Create Debits/Credits for Non Recoverable Tax Account                      |
6049          +----------------------------------------------------------------------------*/
6050 --           Create_Debits_Credits(g_ae_rule_rec.tax_amt_alloc         ,
6051 --                                 g_ae_rule_rec.tax_acctd_amt_alloc   ,
6052 --                                 l_taxable_amt                       ,
6053 --                                 l_taxable_acctd_amt                 ,
6054 --                                 '','',
6055 --                                 l_ae_line_rec);
6056          /*----------------------------------------------------------------------------+
6057           | Assign Non Recoverable accounting record to lines table                    |
6058           +----------------------------------------------------------------------------*/
6059 --            Assign_Elements(l_ae_line_rec);
6060 --            l_ae_line_rec := l_ae_line_rec_empty;
6061 --   END IF;  --end if Build Tax for ACTIVITY
6062 --}
6063  /*----------------------------------------------------------------------------+
6064   | Populate the source type secondary for receipts/adjustments with tax       |
6065   +----------------------------------------------------------------------------*/
6066 --{HYUDETUPT merge inside the loop part of avoiding triple execution of process_amount
6067 --   IF p_type_acct = 'ED_ADJ' THEN
6068 --      IF g_ae_doc_rec.source_table = 'RA' THEN
6069 --         l_source_type_secondary  := 'EDISC' ;
6070     /*----------------------------------------------------------------------------+
6071      | Populate the source type secondary for adjustments with tax                |
6072      +----------------------------------------------------------------------------*/
6073 --      ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
6074 --            l_source_type_secondary    := 'ADJ';
6075     /*----------------------------------------------------------------------------+
6076      | Populate the source type secondary for finance charges tax inclusive       |
6077      +----------------------------------------------------------------------------*/
6078 --      ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
6079 --            l_source_type_secondary    := 'FINCHRG';
6080 --      END IF;
6081  /*----------------------------------------------------------------------------+
6082   | Populate the source type secondary for unearned discounts                  |
6083   +----------------------------------------------------------------------------*/
6084 --   ELSIF p_type_acct = 'UNED' THEN
6085 --         l_source_type_secondary       := 'UNEDISC' ;
6086 --   END IF; --end if type of account
6087 --}
6088 
6089 
6090  /*----------------------------------------------------------------------------+
6091   | Process all tax lines to be built                                          |
6092   +----------------------------------------------------------------------------*/
6093 
6094     OPEN summarize_tax;
6095 
6096     LOOP
6097     FETCH summarize_tax BULK COLLECT INTO g_ae_summ_tax_blk_tbl LIMIT g_bulk_fetch_rows;
6098 
6099     --reinitialize
6100     l_ae_alloc_rec_gt_tab := l_ae_alloc_empty_tab;
6101     l_bulk_index := 0;
6102 
6103     IF summarize_tax%NOTFOUND THEN
6104       l_last_fetch := TRUE;
6105     END IF;
6106 
6107     FOR i IN 1..g_ae_summ_tax_blk_tbl.count LOOP
6108 
6109        g_ae_summ_tax_tbl := g_ae_summ_tax_blk_tbl(i);
6110 
6111        l_actual_account   := g_ae_summ_tax_tbl.actual_account;
6112        l_actual_tax_ccid  := g_ae_summ_tax_tbl.actual_tax_ccid;
6113 
6114        arp_standard.debug('Tax g_ae_summ_tax_tbl.ae_customer_trx_line_id:'||g_ae_summ_tax_tbl.ae_customer_trx_line_id);
6115 
6116        /*--------------------------------------------------------------------------------+
6117         | Initialize record to create new line with exchange rate defaulting from Invoice|
6118         +--------------------------------------------------------------------------------*/
6119          l_ae_line_rec := p_ae_line_init_rec;
6120 
6121        /*----------------------------------------------------------------------------+
6122         | Assign Tax link id, and tax id for Tax lines                               |
6123         +----------------------------------------------------------------------------*/
6124          l_ae_line_rec.ae_tax_link_id := g_ae_summ_tax_tbl.ae_tax_link_id_act;
6125          l_ae_line_rec.ae_customer_trx_line_id := g_ae_summ_tax_tbl.ae_customer_trx_line_id;
6126          l_ae_line_rec.ae_cust_trx_line_gl_dist_id := g_ae_summ_tax_tbl.ae_cust_trx_line_gl_dist_id;
6127          l_ae_line_rec.ae_ref_line_id := g_ae_summ_tax_tbl.ae_ref_line_id;
6128          l_ae_line_rec.ref_account_class := g_ae_summ_tax_tbl.ref_account_class;
6129          l_ae_line_rec.activity_bucket    := g_ae_summ_tax_tbl.activity_bucket;
6130          --{ref_dist_ccid
6131          l_ae_line_rec.ref_dist_ccid    := g_ae_summ_tax_tbl.ref_dist_ccid;
6132          l_ae_line_rec.ref_mf_dist_flag := g_ae_summ_tax_tbl.ref_mf_dist_flag;
6133          --}
6134 
6135          arp_standard.debug('  g_ae_summ_tax_tbl.activity_bucket :'||g_ae_summ_tax_tbl.activity_bucket);
6136 
6137    --Earned discount
6138    IF g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX') THEN
6139         l_source_type_secondary  := 'EDISC' ;
6140     /*----------------------------------------------------------------------------+
6141      | Populate the source type secondary for adjustments with tax                |
6142      +----------------------------------------------------------------------------*/
6143    -- Adj tax
6144    ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('ADJ_TAX') THEN
6145 
6146       IF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
6147             l_source_type_secondary    := 'ADJ';
6148      /*----------------------------------------------------------------------------+
6149       | Populate the source type secondary for finance charges tax inclusive       |
6150       +----------------------------------------------------------------------------*/
6151       ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
6152             l_source_type_secondary    := 'FINCHRG';
6153       END IF;
6154  /*----------------------------------------------------------------------------+
6155   | Populate the source type secondary for unearned discounts                  |
6156   +----------------------------------------------------------------------------*/
6157    --Uned Disc
6158    ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('UNED_TAX') THEN
6159          l_source_type_secondary       := 'UNEDISC' ;
6160    END IF; --end if type of account
6161    arp_standard.debug('  l_source_type_secondary :'||l_source_type_secondary);
6162 
6163 
6164 
6165    /*----------------------------------------------------------------------------+
6166     | Override the tax code id as for rule ACTIVITY the tax code must be from the|
6167     | receivable activity                                                        |
6168     +----------------------------------------------------------------------------*/
6169     --
6170     -- Setting ae_tax_code_id
6171     --         ae_location_segment_id
6172     --
6173     IF (((the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'ACTIVITY') AND
6174          (g_ae_summ_tax_tbl.activity_bucket IN ('ADJ_TAX', 'ED_TAX'))))
6175               AND (NOT g_bound_tax) AND (NOT g_bound_activity) THEN
6176 
6177             l_ae_line_rec.ae_tax_code_id         := g_ae_rule_rec.act_vat_tax_id1;
6178             l_ae_line_rec.ae_location_segment_id := '';
6179 
6180      ELSIF (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) ='ACTIVITY') AND
6181            (g_ae_summ_tax_tbl.activity_bucket IN ('UNED_TAX'))
6182             AND (NOT g_bound_tax) AND (NOT g_bound_activity) THEN
6183 
6184             l_ae_line_rec.ae_tax_code_id         := g_ae_rule_rec.act_vat_tax_id2;
6185             l_ae_line_rec.ae_location_segment_id := '';
6186 
6187      ELSE
6188             IF g_ae_summ_tax_tbl.ae_tax_type = 'VAT' THEN
6189                l_ae_line_rec.ae_tax_group_code_id   := g_ae_summ_tax_tbl.ae_tax_group_code_id;
6190                l_ae_line_rec.ae_tax_code_id         := g_ae_summ_tax_tbl.ae_tax_id;
6191             ELSE --implies LOC or location based tax
6192                l_ae_line_rec.ae_location_segment_id := g_ae_summ_tax_tbl.ae_tax_id;
6193             END IF;
6194      END IF;
6195 
6196 
6197 
6198 
6199     /*----------------------------------------------------------------------------+
6200      | If tax is recoverable then debit/credit the Interim/Tax Account with       |
6201      | discount, adjustment amount                                                |
6202      +----------------------------------------------------------------------------*/
6203     --
6204     --Set line_type
6205     -- For APP_TAX, ED_TAX, UNED_TAX, ADJ_TAX
6206     -- Tax and Def tax
6207     --
6208     IF     (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'INVOICE')
6209              AND (the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket) = 'Y')
6210              AND (NOT g_bound_tax) THEN
6211 
6212 --          IF    g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX') THEN
6213 --                l_ae_line_rec.ae_line_type    := 'EDISC';
6214 --          ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('UNED_TAX') THEN
6215 --                l_ae_line_rec.ae_line_type    := 'UNEDISC';
6216           /*----------------------------------------------------------------------------+
6217            | Verify whether TAX is deferred                                             |
6218            +----------------------------------------------------------------------------*/
6219           IF g_ae_summ_tax_tbl.ae_collected_tax_ccid IS NULL THEN
6220                 l_ae_line_rec.ae_line_type    := 'TAX';
6221           ELSE --Tax is deferred
6222                 l_ae_line_rec.ae_line_type    := 'DEFERRED_TAX';
6223           END IF;
6224 
6225 
6226           /*--------------------------------------------------------------------------------------+
6227            | Set the source type secondary to indicate TAX/DEFERRED TAX on discounts, adjustments |
6228            +--------------------------------------------------------------------------------------*/
6229           l_ae_line_rec.ae_line_type_secondary  := l_source_type_secondary;
6230 
6231            --Assign account
6232           l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_code_combination_id;
6233 
6234           /*----------------------------------------------------------------------------+
6235            | Create accounting debits or credits as applicable                          |
6236            +----------------------------------------------------------------------------*/
6237          IF g_ae_summ_tax_tbl.activity_bucket IN ('APP_TAX') THEN
6238                --
6239                -- APP_TAX
6240                --
6241 
6242                -- Bug 6598080
6243               IF g_ae_doc_rec.called_from = 'WRAPPER' THEN
6244                  l_ae_line_rec.ae_account := g_ae_code_combination_id_app;
6245               ELSE
6246 	       l_ae_line_rec.ae_account := g_ae_rule_rec.receivable_account;
6247                arp_standard.debug('CCID for APP_TAX l_ae_line_rec.ae_account :'|| l_ae_line_rec.ae_account);
6248               END IF;
6249 
6250                /*----------------------------------------------------------------------------+
6251                 | Create accounting debits or credits as applicable                          |
6252                 +----------------------------------------------------------------------------*/
6253                  -- Application for tax. ARALLOCB creates the CR REC, as ARPDDB passes negative
6254                  -- detail distributions, we need ti multiply by -1 so that the distribution
6255                  -- for ARALLOCB are created as positive <=> CR REC
6256                   Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt       * -1,
6257                                         g_ae_summ_tax_tbl.ae_pro_acctd_amt * -1,
6258                                         g_ae_summ_tax_tbl.ae_pro_recov_taxable_amt      * -1,
6259                                         g_ae_summ_tax_tbl.ae_pro_recov_taxable_acctd_amt* -1,
6260                                         g_ae_summ_tax_tbl.ae_from_pro_amt       * -1,
6261                                         g_ae_summ_tax_tbl.ae_from_pro_acctd_amt * -1,
6262                                         l_ae_line_rec);
6263 
6264                   l_ae_line_rec.ae_line_type    := 'REC';
6265          ELSE
6266               --
6267               --For  ADJ_TAX, ED_TAX, UNED_TAX
6268               --
6269                l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_code_combination_id;
6270                arp_standard.debug('1 CCID for '||g_ae_summ_tax_tbl.activity_bucket||
6271                                   ' l_ae_line_rec.ae_account :'|| l_ae_line_rec.ae_account);
6272 
6273                --Boundary Adj
6274                adj_boundary_account
6275                  (p_receivables_trx_id   => g_receivables_trx_id,
6276                   p_bucket               => g_ae_summ_tax_tbl.ref_account_class,
6277                   p_ctlgd_id             => g_ae_summ_tax_tbl.ae_cust_trx_line_gl_dist_id,
6278                   x_ccid                 => l_ae_line_rec.ae_account);
6279 
6280                arp_standard.debug('2 CCID for '||g_ae_summ_tax_tbl.activity_bucket||
6281                                   ' l_ae_line_rec.ae_account :'|| l_ae_line_rec.ae_account);
6282 
6283               l_actual_account := l_ae_line_rec.ae_account;
6284               substite_tax_bal_seg
6285                (p_line_type       => l_ae_line_rec.ae_line_type,
6286                 p_gas             => the_gl_account_source(g_ae_summ_tax_tbl.activity_bucket),
6287                 p_tcs             => the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket),
6288                 p_tax_rec_flag    => the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket),
6289                 p_ccid            => l_actual_account,
6290                 x_ccid            => l_ae_line_rec.ae_account);
6291 
6292 
6293 --BUG#5245153
6294 --    IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
6295 --                 l_actual_account := l_ae_line_rec.ae_account;
6296 --                 Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id              ,
6297 --                                 p_original_ccid => l_actual_account         ,
6298 --                                 p_subs_ccid     => g_ae_rule_rec.receivable_account ,
6299 --                                 p_actual_ccid   => l_ae_line_rec.ae_account             );
6300 --	END IF;
6301 --}
6302                /*----------------------------------------------------------------------------+
6303                 | Create accounting debits or credits as applicable                          |
6304                 +----------------------------------------------------------------------------*/
6305                  -- Accounting for Activities
6306                  -- ARALLOCB creates the activity side of the accounting <=>
6307                  -- WO for Adjustments : negative ADJ > ARPDDB passes negative detail distributions > ARALLOCB creates DB WO
6308                  -- ED activity accounting for ED: positive ED > ARADDB passes negative detail distributions > ARALLOCB creates DB ED activity
6309                  -- UNED activity accounting for UNED: positive UNED > ARADDB passes negative detail distributions > ARALLOCB creates DB UNED activity
6310                  -- therefor no need to multiply by -1
6311                   Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt               ,
6312                                         g_ae_summ_tax_tbl.ae_pro_acctd_amt         ,
6313                                         g_ae_summ_tax_tbl.ae_pro_recov_taxable_amt ,
6314                                         g_ae_summ_tax_tbl.ae_pro_recov_taxable_acctd_amt ,
6315                                         g_ae_summ_tax_tbl.ae_from_pro_amt          ,
6316                                         g_ae_summ_tax_tbl.ae_from_pro_acctd_amt    ,
6317                                         l_ae_line_rec);
6318         END IF;
6319 
6320         --Assign_Elements(l_ae_line_rec);
6321         l_ae_line_rec.ae_id :=  g_id;
6322 	l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6323 	l_bulk_index := l_bulk_index + 1;
6324 	l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6325 
6326 
6327  ELSIF     --This is only possible for ADJ_TAX, ED_TAX, UNED_TAX
6328            -- Attention Verify why the APP_TAX non_recoverable
6329                (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'INVOICE')
6330            AND (the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket) = 'N')
6331            AND (NOT g_bound_tax)
6332            AND (g_ae_summ_tax_tbl.activity_bucket NOT IN ('APP_TAX'))
6333  THEN
6334       /*----------------------------------------------------------------------------+
6335        | Populate the source type for earned discounts non recoverable account      |
6336        +----------------------------------------------------------------------------*/
6337        --IF p_type_acct = 'ED_ADJ' THEN
6338        --
6339        -- Set line_type
6340        --
6341        IF (g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX','ADJ_TAX' )) THEN
6342 
6343            IF g_ae_doc_rec.source_table = 'RA' THEN
6344               l_ae_line_rec.ae_line_type    := 'EDISC_NON_REC_TAX' ;
6345            /*----------------------------------------------------------------------------+
6346             | Populate the source type for adjustment non recoverable account            |
6347             +----------------------------------------------------------------------------*/
6348            ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
6349               l_ae_line_rec.ae_line_type := 'ADJ_NON_REC_TAX';
6350            /*----------------------------------------------------------------------------+
6351             | Populate the source type for finance charges non recoverable account       |
6352             +----------------------------------------------------------------------------*/
6353            ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
6354               l_ae_line_rec.ae_line_type := 'FINCHRG_NON_REC_TAX';
6355            END IF;
6356 
6357        --Set l_actual_account, set by summarize cursor
6358 
6359        /*----------------------------------------------------------------------------+
6360         | Populate the source type for unearned discounts non recoverable account    |
6361         +----------------------------------------------------------------------------*/
6362                --  ELSIF p_type_acct = 'UNED' THEN
6363        ELSIF (g_ae_summ_tax_tbl.activity_bucket IN ('UNED_LINE','UNED_TAX','UNED_FRT','UNED_CHRG')) THEN
6364            l_ae_line_rec.ae_line_type    := 'UNEDISC_NON_REC_TAX' ;
6365                      --Set l_actual_account, set by summarize cursor
6366        END IF;
6367 
6368        /*-----------------------------------------------------------------------------+
6369         | Level 2 validation in case the non recoverable tax account setup on tax code|
6370         | is null - request user to set it up.                                        |
6371         +-----------------------------------------------------------------------------*/
6372 
6373         IF    l_actual_account IS NULL  THEN --is null
6374                --Boundary Adj
6375               adj_boundary_account
6376               (p_receivables_trx_id   => g_receivables_trx_id,
6377                p_bucket               => g_ae_summ_tax_tbl.activity_bucket,
6378                p_ctlgd_id             => g_ae_summ_tax_tbl.ae_cust_trx_line_gl_dist_id,
6379                x_ccid                 => l_actual_account);
6380             IF l_actual_account IS NULL  THEN
6381                arp_standard.debug('Invoice Non Recoverable Account is NULL');
6382                RAISE invalid_ccid_error;
6383             END IF;
6384         END IF;
6385 
6386 
6387         substite_tax_bal_seg
6388                (p_line_type       => l_ae_line_rec.ae_line_type,
6389                 p_gas             => the_gl_account_source(g_ae_summ_tax_tbl.activity_bucket),
6390                 p_tcs             => the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket),
6391                 p_tax_rec_flag    => the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket),
6392                 p_ccid            => l_actual_account,
6393                 x_ccid            => l_ae_line_rec.ae_account);
6394 
6395 
6396 
6397                /*----------------------------------------------------------------------------+
6398                 | Create first accounting entry for Debit/Credit to Non Recoverable/Override |
6399                 | Account, Setup Non Recoverable account for debits/credits                  |
6400                 | Substitute balancing segment for Non Recoverable Tax Account               |
6401                 +----------------------------------------------------------------------------*/
6402     -- Bugfix 1948917.
6403 --    IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
6404 --                 Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id              ,
6405 --                                 p_original_ccid => l_actual_account                 ,
6406 --                                 p_subs_ccid     => g_ae_rule_rec.receivable_account ,
6407 --                                 p_actual_ccid   => l_ae_line_rec.ae_account             );
6408 --	 ELSE
6409 --		 l_ae_line_rec.ae_account := l_actual_account;
6410 --	 END IF;
6411 
6412 
6413 
6414 
6415         /*----------------------------------------------------------------------------+
6416          | Create Debits/Credits for Non Recoverable Tax Account                      |
6417          +----------------------------------------------------------------------------*/
6418 
6419        arp_standard.debug('Create Debits/Credits for Non Recoverable Tax Account');
6420        -- Creation of activity accounting
6421        -- ADJ UNED ED distributions
6422        -- ARALLOC creates:
6423        -- * ADJ - WO side of accounting <=> negative ADJ => DB activty WO. As ARPDDB passes neg  detail distrib
6424        --       no need to multiply by -1
6425        -- * ED and UNED - Discount Activity side of the accounting. For positive ED UNED, ARPDDB passes negative
6426        --       deatil distributions and ARALLOCB creates DB of Activity, therefore no need to multiply by -1
6427        Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt                    ,
6428                              g_ae_summ_tax_tbl.ae_pro_acctd_amt              ,
6429                              g_ae_summ_tax_tbl.ae_pro_split_taxable_amt      ,
6430                              g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt,
6431                              g_ae_summ_tax_tbl.ae_from_pro_amt               ,
6432                              g_ae_summ_tax_tbl.ae_from_pro_acctd_amt         ,
6433                              l_ae_line_rec);
6434 
6435         /*----------------------------------------------------------------------------+
6436          | Assign Non Recoverable accounting record to lines table                    |
6437          +----------------------------------------------------------------------------*/
6438          --Assign_Elements(l_ae_line_rec);
6439 	 l_bulk_index := l_bulk_index + 1;
6440          l_ae_line_rec.ae_id :=  g_id;
6441 	 l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6442 	 l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6443 
6444 ELSIF   -- Activity Tax accounting not for APP_TAX
6445             (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket)='ACTIVITY')
6446         AND (NOT g_bound_tax) AND (NOT g_bound_activity)
6447       --AND (p_type_acct <> 'PAY')
6448         AND g_ae_summ_tax_tbl.activity_bucket NOT IN ('APP_LINE','APP_TAX','APP_FRT','APP_CHRG') --TCSACT
6449 THEN
6450         /*----------------------------------------------------------------------------+
6451          | Populate the source type for earned discounts non recoverable account      |
6452          +----------------------------------------------------------------------------*/
6453          -- IF p_type_acct = 'ED_ADJ' THEN
6454 
6455          --
6456          -- Set line_type
6457          --
6458          IF g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX','ADJ_TAX')  THEN
6459              IF g_ae_doc_rec.source_table = 'RA' THEN
6460                 l_ae_line_rec.ae_line_type    := 'EDISC_NON_REC_TAX' ;
6461              /*----------------------------------------------------------------------------+
6462               | Populate the source type for adjustment non recoverable account            |
6463               +----------------------------------------------------------------------------*/
6464              ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
6465                 l_ae_line_rec.ae_line_type := 'ADJ_NON_REC_TAX';
6466              /*----------------------------------------------------------------------------+
6467               | Populate the source type for finance charges non recoverable account       |
6468               +----------------------------------------------------------------------------*/
6469              ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
6470                 l_ae_line_rec.ae_line_type := 'FINCHRG_NON_REC_TAX';
6471              END IF;
6472              --Set by summarize cursor l_actual_account := g_ae_rule_rec.act_tax_non_rec_ccid1;
6473              /*----------------------------------------------------------------------------+
6474               | Populate the source type for unearned discounts non recoverable account    |
6475               +----------------------------------------------------------------------------*/
6476               --  ELSIF p_type_acct = 'UNED' THEN
6477          ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('UNED_TAX') THEN
6478              l_ae_line_rec.ae_line_type    := 'UNEDISC_NON_REC_TAX' ;
6479              --Set by summarize cursor l_actual_account := g_ae_rule_rec.act_tax_non_rec_ccid2;
6480          END IF;
6481 
6482 
6483          /*-----------------------------------------------------------------------------+
6484           | Level 2 validation in case the non recoverable tax account setup on activity|
6485           | tax code is null - request user to set it up.                               |
6486           +-----------------------------------------------------------------------------*/
6487          IF l_actual_account IS NULL THEN --is null
6488                 --Boundary Adj
6489                 adj_boundary_account
6490                (p_receivables_trx_id   => g_receivables_trx_id,
6491                 p_bucket               => g_ae_summ_tax_tbl.activity_bucket,
6492                 p_ctlgd_id             => g_ae_summ_tax_tbl.ae_cust_trx_line_gl_dist_id,
6493                 x_ccid                 => l_actual_account );
6494 
6495              IF l_actual_account IS NULL THEN --is null
6496                arp_standard.debug('Activity Non Recoverable Account is NULL');
6497                RAISE invalid_ccid_error;
6498              END IF;
6499 
6500          END IF;
6501 
6502         substite_tax_bal_seg
6503                (p_line_type       => l_ae_line_rec.ae_line_type,
6504                 p_gas             => the_gl_account_source(g_ae_summ_tax_tbl.activity_bucket),
6505                 p_tcs             => the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket),
6506                 p_tax_rec_flag    => the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket),
6507                 p_ccid            => l_actual_account,
6508                 x_ccid            => l_ae_line_rec.ae_account);
6509 
6510 
6511          /*----------------------------------------------------------------------------+
6512           | Create first accounting entry for Debit/Credit to Non Recoverable/Override |
6513           | Account, Setup Non Recoverable account for debits/credits                  |
6514           | Substitute balancing segment for Non Recoverable Tax Account               |
6515           +----------------------------------------------------------------------------*/
6516 	       -- Bugfix 1948917.
6517 --         IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
6518 --               Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id              ,
6519 --                               p_original_ccid => l_actual_account                 ,
6520 --                               p_subs_ccid     => g_ae_rule_rec.receivable_account ,
6521 --                               p_actual_ccid   => l_ae_line_rec.ae_account             );
6522 --         ELSE
6523 --               l_ae_line_rec.ae_account := l_actual_account;
6524 --         END IF;
6525 
6526 
6527          /*----------------------------------------------------------------------------+
6528           | Create Debits/Credits for Non Recoverable Tax Account                      |
6529           +----------------------------------------------------------------------------*/
6530          arp_standard.debug(' Create Debits/Credits for Non Recoverable Tax Account');
6531          -- Creation of activity accounting
6532          -- ADJ UNED ED distributions
6533          -- ARALLOC creates:
6534          -- * ADJ - WO side of accounting <=> negative ADJ => DB activty WO. As ARPDDB passes neg  detail distrib
6535          --       no need to multiply by -1
6536          -- * ED and UNED - Discount Activity side of the accounting. For positive ED UNED, ARPDDB passes negative
6537          --       deatil distributions and ARALLOCB creates DB of Activity, therefore no need to multiply by -1
6538          Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt                 ,
6539                                g_ae_summ_tax_tbl.ae_pro_acctd_amt           ,
6540                                g_ae_summ_tax_tbl.ae_pro_split_taxable_amt   ,
6541                                g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt ,
6542                                g_ae_summ_tax_tbl.ae_from_pro_amt            ,
6543                                g_ae_summ_tax_tbl.ae_from_pro_acctd_amt      ,
6544                                l_ae_line_rec);
6545 
6546          /*----------------------------------------------------------------------------+
6547           | Assign Non Recoverable accounting record to lines table                    |
6548           +----------------------------------------------------------------------------*/
6549          --Assign_Elements(l_ae_line_rec);
6550 	 l_bulk_index := l_bulk_index + 1;
6551          l_ae_line_rec.ae_id :=  g_id;
6552 	 l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6553 	 l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6554 
6555          /*----------------------------------------------------------------------------+
6556           | Assign Non Recoverable accounting record to lines table                    |
6557           +----------------------------------------------------------------------------*/
6558          IF g_ae_summ_tax_tbl.ae_tax_type = 'VAT' THEN
6559             l_ae_line_rec.ae_tax_group_code_id   := g_ae_summ_tax_tbl.ae_tax_group_code_id;
6560             l_ae_line_rec.ae_tax_code_id         := g_ae_summ_tax_tbl.ae_tax_id;
6561          ELSE --implies LOC or location based tax
6562             l_ae_line_rec.ae_location_segment_id := g_ae_summ_tax_tbl.ae_tax_id;
6563          END IF;
6564 
6565 
6566  ELSIF     (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'NONE')
6567              AND (g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX','UNED_TAX',
6568                   'ADJ_TAX'    --BUG#5185726: include ADJ_TAX
6569                   ))
6570              AND (NOT g_bound_tax) THEN
6571 
6572           IF    g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX') THEN
6573                 l_ae_line_rec.ae_line_type    := 'EDISC';
6574           ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('UNED_TAX') THEN
6575                 l_ae_line_rec.ae_line_type    := 'UNEDISC';
6576           ELSIF g_ae_summ_tax_tbl.activity_bucket IN ('ADJ_TAX') THEN --BUG#5185726: include ADJ_TAX
6577                 l_ae_line_rec.ae_line_type    := 'ADJ';
6578           END IF;
6579 
6580           /*----------------------------------------------------------------------------+
6581            | Create accounting debits or credits as applicable                          |
6582            +----------------------------------------------------------------------------*/
6583 
6584 
6585           IF g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX',
6586 		            'ADJ_TAX') --BUG#5185726: include ADJ_TAX
6587           THEN
6588              IF g_ae_rule_rec.gl_account_source1 = 'REVENUE_ON_INVOICE' AND
6589                 g_ae_rule_rec.tax_code_source1   = 'NONE'
6590              THEN
6591                 l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_code_combination_id;
6592              ELSIF g_ae_rule_rec.gl_account_source1 = 'TAX_CODE_ON_INVOICE' AND
6593                    g_ae_rule_rec.tax_code_source1   = 'NONE'
6594              THEN
6595                 IF g_ae_summ_tax_tbl.activity_bucket = 'ADJ_TAX' THEN
6596                    l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_adj_ccid;
6597                 ELSE
6598                    l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_edisc_ccid;
6599                 END IF;
6600              ELSE
6601                 l_ae_line_rec.ae_account := g_ae_rule_rec.code_combination_id1;
6602              END IF;
6603           ELSE
6604              IF g_ae_rule_rec.gl_account_source2 = 'REVENUE_ON_INVOICE' AND
6605                 g_ae_rule_rec.tax_code_source2   = 'NONE'
6606              THEN
6607                 l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_code_combination_id;
6608              ELSIF g_ae_rule_rec.gl_account_source2 = 'TAX_CODE_ON_INVOICE' AND
6609                    g_ae_rule_rec.tax_code_source2   = 'NONE'
6610              THEN
6611                 l_ae_line_rec.ae_account := g_ae_summ_tax_tbl.ae_unedisc_ccid;
6612              ELSE
6613                 l_ae_line_rec.ae_account := g_ae_rule_rec.code_combination_id2;
6614              END IF;
6615           END IF;
6616 
6617         l_actual_account := l_ae_line_rec.ae_account;
6618         substite_tax_bal_seg
6619                (p_line_type       => l_ae_line_rec.ae_line_type,
6620                 p_gas             => the_gl_account_source(g_ae_summ_tax_tbl.activity_bucket),
6621                 p_tcs             => the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket),
6622                 p_tax_rec_flag    => the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket),
6623                 p_ccid            => l_actual_account,
6624                 x_ccid            => l_ae_line_rec.ae_account);
6625 
6626 
6627            /*----------------------------------------------------------------------------+
6628             | Create accounting debits or credits as applicable                          |
6629             +----------------------------------------------------------------------------*/
6630            -- Accounting for Activities
6631            -- ARALLOCB creates the activity side of the accounting <=>
6632            -- WO for Adjustments : negative ADJ > ARPDDB passes negative detail distributions > ARALLOCB creates DB WO
6633            -- ED activity accounting for ED: positive ED > ARADDB passes negative detail distributions > ARALLOCB creates DB ED activity
6634            -- UNED activity accounting for UNED: positive UNED > ARADDB passes negative detail distributions > ARALLOCB creates DB UNED activity
6635            -- therefor no need to multiply by -1
6636            Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt               ,
6637                                  g_ae_summ_tax_tbl.ae_pro_acctd_amt         ,
6638                                  g_ae_summ_tax_tbl.ae_pro_recov_taxable_amt ,
6639                                  g_ae_summ_tax_tbl.ae_pro_recov_taxable_acctd_amt ,
6640                                  g_ae_summ_tax_tbl.ae_from_pro_amt          ,
6641                                  g_ae_summ_tax_tbl.ae_from_pro_acctd_amt    ,
6642                                 l_ae_line_rec);
6643          -- Assign_Elements(l_ae_line_rec);
6644 	 l_bulk_index := l_bulk_index + 1;
6645          l_ae_line_rec.ae_id :=  g_id;
6646 	 l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6647 	 l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6648 
6649   END IF;
6650 
6651 
6652 
6653 
6654 
6655         /*------------------------------------------------------------------------------+
6656          | Create accounting entries for deferred tax where tax is non recoverable. For |
6657          | adjustments in the sign of the receivable, a refund takes place when these   |
6658          | accounting entries are created. Eg -ve adjustment Dr Interim, Cr Collected,  |
6659          | positive adjustments Cr Interim, Dr Collected, users would need to run       |
6660          | reports based on the accounting created, so that they can handle deferred    |
6661          | Invoices when such accounting takes place and create necessary offsetting    |
6662          | adjustments as required. For boundary conditions we dont create regular      |
6663          | accounting entries, but only move the deferred tax due to discount or payment|
6664          | hence the if condtruct using g_bound_tax for the above accounting conditions|
6665          +------------------------------------------------------------------------------*/
6666 
6667      IF (      (g_ae_summ_tax_tbl.ae_collected_tax_ccid IS NOT NULL)
6668            AND  ((   (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'INVOICE')
6669                  AND (the_tax_recoverable_flag(g_ae_summ_tax_tbl.activity_bucket) = 'N'))
6670              OR (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'ACTIVITY')
6671              OR (the_tax_code_source(g_ae_summ_tax_tbl.activity_bucket) = 'NONE')))
6672            AND (g_ae_summ_tax_tbl.activity_bucket IN ('ED_TAX','UNED_TAX','ADJ_TAX'))
6673      THEN
6674 
6675          /*--------------------------------------------------------------------------------------+
6676           | Set the source type secondary to indicate TAX/DEFERRED TAX on discounts, adjustments |
6677           +--------------------------------------------------------------------------------------*/
6678           l_ae_line_rec.ae_line_type_secondary  := l_source_type_secondary;
6679 
6680           --{BUG#3509185
6681           /*----------------------------------------------------------------------------------------+
6682            |For BR adjustments set the secondary table, id, type columns as the                     |
6683            |are critical in reconciliation process. Bills has tax code source as NONE               |
6684            |for adjustments against it which ae of type endorsements. Override the above            |
6685            |type secondary if bill line assignment. The setting below is used for BR reconciliation |
6686            |On closure of the bill, and if not set correctly will orphan the deferred tax entries.  |
6687            +----------------------------------------------------------------------------------------*/
6688            IF g_br_cust_trx_line_id IS NOT NULL THEN
6689               IF (     (g_ae_doc_rec.source_table = 'RA')
6690                    AND (g_ae_doc_rec.event = 'MATURITY_DATE')) THEN
6691                      l_ae_line_rec.ae_source_table := 'TH';
6692               END IF;
6693               /*----------------------------------------
6694                |Populate the source table secondary for the accounting created by tax accounting
6695                |engine, because it is important to distinguish tax moved foor a specific exchange
6696                |or transaction on the Bill. ( tax codes and other accounting grouping attributes
6697                |could be common accross different transactions.)
6698                +-------------------------------------------------*/
6699                l_ae_line_rec.ae_source_table_secondary := 'CTL';
6700                l_ae_line_rec.ae_source_id_secondary    := g_br_cust_trx_line_id;
6701                l_ae_line_rec.ae_line_type_secondary   := 'ASSIGNMENT';
6702           END IF; --BR
6703           --}
6704           /*----------------------------------------------------------------------------+
6705            | Create second accounting entry for Debit/Credit to Interim Tax account     |
6706            +----------------------------------------------------------------------------*/
6707           l_ae_line_rec.ae_line_type := 'DEFERRED_TAX' ;
6708           IF  l_actual_tax_ccid IS NOT NULL THEN
6709               l_ae_line_rec.ae_account := l_actual_tax_ccid;
6710           ELSE
6711               l_ae_line_rec.ae_account      := g_ae_summ_tax_tbl.ae_code_combination_id;
6712           END IF;
6713           /*----------------------------------------------------------------------------+
6714            | Create Debits/Credits for Interim Tax account to move deferred tax         |
6715            +----------------------------------------------------------------------------*/
6716            --
6717            -- If ED_TAX or UNED_TAX then the DEFERRED_TAX accounting same sign as ED or UNED TAX distributions
6718            l_multi_factor := 1;
6719 
6720 
6721            Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt               * l_multi_factor,
6722                                  g_ae_summ_tax_tbl.ae_pro_acctd_amt         * l_multi_factor,
6723                                  g_ae_summ_tax_tbl.ae_pro_split_taxable_amt * l_multi_factor,
6724                                  g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt * l_multi_factor,
6725                                  g_ae_summ_tax_tbl.ae_from_pro_amt          * l_multi_factor,
6726                                  g_ae_summ_tax_tbl.ae_from_pro_acctd_amt    * l_multi_factor,
6727                                  l_ae_line_rec);
6728 
6729             /*----------------------------------------------------------------------------+
6730              | Assign debit/credit for Interim tax to build table                         |
6731              +----------------------------------------------------------------------------*/
6732             --Assign_Elements(l_ae_line_rec);
6733 	    l_bulk_index := l_bulk_index + 1;
6734             l_ae_line_rec.ae_id :=  g_id;
6735 	    l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6736 	    l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6737 
6738             /*----------------------------------------------------------------------------+
6739              | Create third accounting entry for Debit/Credit to Collected Tax account    |
6740              +----------------------------------------------------------------------------*/
6741             l_ae_line_rec.ae_line_type := 'TAX' ;
6742             l_ae_line_rec.ae_account      := g_ae_summ_tax_tbl.ae_collected_tax_ccid;
6743 
6744             /*----------------------------------------------------------------------------+
6745              | Create Debits/Credits for Collected Tax account to move deferred tax       |
6746              +----------------------------------------------------------------------------*/
6747             Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt   * -1 * l_multi_factor ,
6748                                   g_ae_summ_tax_tbl.ae_pro_acctd_amt * -1 * l_multi_factor,
6749                                   g_ae_summ_tax_tbl.ae_pro_split_taxable_amt -1* l_multi_factor,
6750                                   g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt * -1* l_multi_factor,
6751                                   g_ae_summ_tax_tbl.ae_from_pro_amt * -1  * l_multi_factor,
6752                                   g_ae_summ_tax_tbl.ae_from_pro_acctd_amt * -1 * l_multi_factor,
6753                                   l_ae_line_rec,
6754                                  'Y');
6755 
6756             /*----------------------------------------------------------------------------+
6757              | Assign debit/credit for Collected Tax account to build table               |
6758              +----------------------------------------------------------------------------*/
6759              --Assign_Elements(l_ae_line_rec);
6760 	     l_bulk_index := l_bulk_index + 1;
6761              l_ae_line_rec.ae_id :=  g_id;
6762 	     l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6763 	     l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6764 
6765      END IF;
6766 
6767 
6768 
6769 
6770 
6771      /*----------------------------------------------------------------------------+
6772       | Create Debits/Credits for moving prorate deferred tax as a result of       |
6773       | Payment from Interim Account to Collected Tax account                      |
6774       +----------------------------------------------------------------------------*/
6775    IF ((g_ae_summ_tax_tbl.ae_collected_tax_ccid IS NOT NULL) --is not null
6776               AND (NOT g_done_def_tax)
6777               AND (g_ae_summ_tax_tbl.activity_bucket IN ('APP_TAX')))
6778    THEN
6779           l_ae_line_rec := p_ae_line_init_rec; --Initialise record
6780 
6781          /*--------------------------------------------------------------------------------------+
6782           | Set the source type secondary to indicate TAX/DEFERRED TAX on discounts, adjustments |
6783           +--------------------------------------------------------------------------------------*/
6784            l_ae_line_rec.ae_line_type_secondary  := 'PAYMENT';
6785 
6786           --{BUG#3509185
6787           /*------------------------------------------------------------------------------------+
6788            | For Bills receivable adjustments set the secondary table, id, type columns as thes |
6789            | are critical in the reconciliation process. Bills have a Tax code source of NONE   |
6790            | for adjustments against it which are of type endorsments. Override the above line  |
6791            | type secondry if bill line assignment. The setting below is used for BR reconcile  |
6792            | on closure of Bill, and if not set correctly will orphan the deferred tax entries. |
6793            +------------------------------------------------------------------------------------*/
6794             IF g_br_cust_trx_line_id IS NOT NULL THEN
6795                IF ((g_ae_doc_rec.source_table = 'RA')
6796                                    AND (g_ae_doc_rec.event = 'MATURITY_DATE')) THEN
6797                   l_ae_line_rec.ae_source_table := 'TH';
6798                END IF;
6799            /*----------------------------------------------------------------------------------+
6800             | Populate the source table secondary for accounting created by the tax accounting |
6801             | engine, because its is important to distinguish tax moved for a specific exchange|
6802             | or transaction on the Bill. (tax codes and other accounting grouping attributes  |
6803             | could be common across different transactions.)                                  |
6804             +----------------------------------------------------------------------------------*/
6805                l_ae_line_rec.ae_source_table_secondary := 'CTL';
6806                l_ae_line_rec.ae_source_id_secondary    := g_br_cust_trx_line_id;
6807                l_ae_line_rec.ae_line_type_secondary := 'ASSIGNMENT'; --override fefault value
6808             END IF; --end if BR
6809             --}
6810 
6811        --Set the tax code or location segment id
6812           IF g_ae_summ_tax_tbl.ae_tax_type = 'VAT' THEN
6813              l_ae_line_rec.ae_tax_group_code_id := g_ae_summ_tax_tbl.ae_tax_group_code_id;
6814              l_ae_line_rec.ae_tax_code_id := g_ae_summ_tax_tbl.ae_tax_id;
6815           ELSE --implies LOC or location based tax
6816              l_ae_line_rec.ae_location_segment_id := g_ae_summ_tax_tbl.ae_tax_id;
6817           END IF;
6818 
6819           l_ae_line_rec.ae_line_type := 'DEFERRED_TAX' ;
6820           l_ae_line_rec.ae_account   := l_actual_tax_ccid;
6821 
6822         /*-----------------------------------------------------------------------------+
6823          |Deferred tax for payments is not linked to revenue allocations for discounts |
6824          |or adjustments. Set link id for tax moved due to payments to '' as they have |
6825          |no link basis - revenue to link to, they indicate movements due to payments  |
6826          |from the Interim to Collected Tax accounts.                                  |
6827          +-----------------------------------------------------------------------------*/
6828           l_ae_line_rec.ae_tax_link_id := '';
6829 
6830           Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt           ,
6831                                 g_ae_summ_tax_tbl.ae_pro_acctd_amt     ,
6832                                 g_ae_summ_tax_tbl.ae_pro_split_taxable_amt    ,
6833                                 g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt,
6834                                 --HYU--{
6835                                 g_ae_summ_tax_tbl.ae_from_pro_amt           ,
6836                                 g_ae_summ_tax_tbl.ae_from_pro_acctd_amt     ,
6837                                 --HYU--}
6838                                 l_ae_line_rec);
6839 
6840        /*----------------------------------------------------------------------------+
6841         | Assign debit/credit for Interim Tax account for payments to build table    |
6842         +----------------------------------------------------------------------------*/
6843          --Assign_Elements(l_ae_line_rec);
6844 	 l_bulk_index := l_bulk_index + 1;
6845          l_ae_line_rec.ae_id :=  g_id;
6846 	 l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6847 	 l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6848 
6849        /*----------------------------------------------------------------------------+
6850         | Create third accounting entry for Debit/Credit to Collected Tax account    |
6851         +----------------------------------------------------------------------------*/
6852          l_ae_line_rec.ae_line_type := 'TAX' ;
6853          l_ae_line_rec.ae_account      := g_ae_summ_tax_tbl.ae_collected_tax_ccid;
6854 
6855        /*----------------------------------------------------------------------------+
6856         | Create Debits/Credits for Collected Tax account for payment                |
6857         +----------------------------------------------------------------------------*/
6858          Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt * -1               ,
6859                                g_ae_summ_tax_tbl.ae_pro_acctd_amt * -1         ,
6860                                g_ae_summ_tax_tbl.ae_pro_split_taxable_amt  * -1        ,
6861                                g_ae_summ_tax_tbl.ae_pro_split_taxable_acctd_amt * -1   ,
6862                                --HYU--{
6863                                g_ae_summ_tax_tbl.ae_from_pro_amt * -1               ,
6864                                g_ae_summ_tax_tbl.ae_from_pro_acctd_amt * -1         ,
6865                                --HYU--}
6866                                l_ae_line_rec,
6867                                'Y');
6868 
6869        /*----------------------------------------------------------------------------+
6870         | Assign debit/credit for Collected tax account for payments to build table  |
6871         +----------------------------------------------------------------------------*/
6872          --Assign_Elements(l_ae_line_rec);
6873 	 l_bulk_index := l_bulk_index + 1;
6874          l_ae_line_rec.ae_id :=  g_id;
6875 	 l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6876 	 l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6877 
6878      END IF; --End if create debits/credits for deferred tax associated with payment
6879 
6880 
6881 
6882 
6883 
6884 
6885        IF   (g_br_cust_trx_line_id IS NOT NULL)      AND
6886             (g_ae_doc_rec.source_table = 'TH')       AND
6887             (g_ae_summ_tax_tbl.activity_bucket  = 'ADJ_TAX')
6888        THEN
6889              Create_Debits_Credits(g_ae_summ_tax_tbl.ae_pro_amt               ,
6890                                    g_ae_summ_tax_tbl.ae_pro_acctd_amt         ,
6891                                    g_ae_summ_tax_tbl.ae_pro_recov_taxable_amt ,
6892                                    g_ae_summ_tax_tbl.ae_pro_recov_taxable_acctd_amt,
6893                                    --HYU--{
6894                                    g_ae_summ_tax_tbl.ae_from_pro_amt          ,
6895                                    g_ae_summ_tax_tbl.ae_from_pro_acctd_amt    ,
6896                                    --HYU--}
6897                                    l_ae_line_rec);
6898               --Assign_Elements(l_ae_line_rec);
6899 	      l_bulk_index := l_bulk_index + 1;
6900               l_ae_line_rec.ae_id :=  g_id;
6901 	      l_ae_line_rec.ae_summarize_flag := NVL(l_ae_line_rec.ae_summarize_flag,'N');
6902 	      l_ae_alloc_rec_gt_tab( l_bulk_index ) := l_ae_line_rec;
6903         END IF;
6904         --}
6905 
6906 
6907    END LOOP; --For each Tax line build accounting entries
6908 
6909    /**In procedure Assign_Elements,g_ae_ctr is incremented for each record
6910     inserted.But no code segment in this package uses the value of the
6911     variable,thus not incremented the variable here.*/
6912    FORALL i IN l_ae_alloc_rec_gt_tab.first..l_ae_alloc_rec_gt_tab.last
6913    INSERT INTO ar_ae_alloc_rec_gt VALUES l_ae_alloc_rec_gt_tab(i);
6914 
6915    --Exit Last fetch
6916    IF l_last_fetch THEN
6917 	EXIT;
6918     END IF;
6919 
6920 
6921   END LOOP;
6922 
6923   --close cursor
6924   CLOSE summarize_tax;
6925 
6926  /*------------------------------------------------------------+
6927   | Done processing deferred tax, so mark flag to signify this |
6928   +------------------------------------------------------------*/
6929    --IF p_type_acct = 'PAY' THEN
6930    IF g_ae_summ_tax_tbl.activity_bucket IN ('APP_LINE','APP_TAX','APP_FRT','APP_CHRG') THEN
6931       g_done_def_tax := TRUE;
6932    END IF;
6933 
6934 --END IF; -- Build tax only if a tax amount or line amount exists amount exists
6935         -- i.e. if both line alloc and tax alloc are zero then there is nothing
6936 
6937 
6938 
6939 
6940 
6941 
6942 
6943  arp_standard.debug( 'ARP_ALLOCATION_PKG.Build_Tax()-');
6944 
6945 EXCEPTION
6946   WHEN invalid_ccid_error THEN
6947      arp_standard.debug('Invalid Tax ccid - ARP_ALLOCATION_PKG.Build_Tax' );
6948      fnd_message.set_name('AR','AR_INVALID_TAX_ACCOUNT');
6949      RAISE;
6950 
6951   WHEN OTHERS THEN
6952      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Build_Tax');
6953      RAISE;
6954 
6955 END Build_Tax;
6956 
6957 /* ==========================================================================
6958  | PROCEDURE Build_Charges_Freight_All
6959  |
6960  | DESCRIPTION
6961  |    Build actual accounting entries for Charges and Freight accounts
6962  |    to Activity GL Account
6963  |
6964  | SCOPE - PRIVATE
6965  |
6966  | PARAMETERS
6967  |    p_type_acct             IN    Indicates earned, unearned discount or
6968  |                                  adjustment accounting
6969  |    p_ae_line_init_rec      IN    Initialization record contains details
6970  |                                  for exchange rate, source table, id
6971  |                                  common to all accounting entries
6972  |
6973  | NOTES
6974  |    In the code below we add the freight and charges amounts to the line
6975  |    this would already have been done in the Init_Amts routine. These
6976  |    statements are retained they can be removed later if required and are
6977  |    only for reference purposes. In reality the IF construct below which
6978  |    creates the accounting would be true only if p_build_all is true
6979  |    indicating Gross to Activity Gl account. In this case we route the
6980  |    creation through this routine.
6981  |  History
6982  |    21-NOV-2003   Herve Yu   from_amount_dr      ,  from_amount_cr
6983  |                             from_acctd_amount_dr,  from_acctd_amount_cr
6984  *==========================================================================*/
6985 PROCEDURE Build_Charges_Freight_All(p_type_acct         IN VARCHAR2         ,
6986                                     p_ae_line_init_rec  IN ar_ae_alloc_rec_gt%ROWTYPE,
6987                                     p_build_all         IN BOOLEAN ) IS
6988 
6989 l_ae_line_rec         ar_ae_alloc_rec_gt%ROWTYPE;
6990 l_actual_account      ar_distributions.code_combination_id%TYPE ;
6991 l_line_tax_amt        NUMBER := 0;
6992 l_line_tax_acctd_amt  NUMBER := 0;
6993 
6994 BEGIN
6995 
6996   arp_standard.debug('ARP_ALLOCATION_PKG.Build_Charges_Freight_All()+');
6997 
6998   adj_code_combination_id := '';
6999 
7000 /*----------------------------------------------------------------------------------+
7001  | Build All flag indicates that Rule is Gross to Activity GL account for type Acct |
7002  +----------------------------------------------------------------------------------*/
7003   IF ((p_build_all = TRUE) AND (NOT g_added_tax)) THEN
7004 
7005      l_line_tax_amt       := g_ae_rule_rec.line_amt_alloc + g_ae_rule_rec.tax_amt_alloc;
7006      l_line_tax_acctd_amt := g_ae_rule_rec.line_acctd_amt_alloc + g_ae_rule_rec.tax_acctd_amt_alloc;
7007 
7008   ELSIF ((p_build_all = TRUE) AND (g_added_tax)) THEN
7009 
7010       --In this case tax would have been added to line bucket
7011         l_line_tax_amt       := g_ae_rule_rec.line_amt_alloc;
7012         l_line_tax_acctd_amt := g_ae_rule_rec.line_acctd_amt_alloc;
7013 
7014   END IF;
7015 
7016 /*-----------------------------------------------------------------------------+
7017  | If freight or charges exists then create an accounting entry for a sum total|
7018  | or Gross to Activity GL Account                                             |
7019  +-----------------------------------------------------------------------------*/
7020 
7021   IF ((((g_ae_rule_rec.freight_amt_alloc <> 0) OR (g_ae_rule_rec.freight_acctd_amt_alloc <> 0))
7022        OR ((g_ae_rule_rec.charges_amt_alloc <> 0) AND (g_ae_rule_rec.charges_acctd_amt_alloc <> 0)))
7023        OR (p_build_all = TRUE))
7024   THEN
7025 
7026    /*----------------------------------------------------------------------------+
7027     | Initialize record with exchange rate, source id, table details for new line|
7028     +----------------------------------------------------------------------------*/
7029      l_ae_line_rec := p_ae_line_init_rec;
7030 
7031    /*----------------------------------------------------------------------------+
7032     | Populate source type for earned discounts                                  |
7033     +----------------------------------------------------------------------------*/
7034      IF p_type_acct = 'ED_ADJ' THEN
7035 
7036         IF g_ae_doc_rec.source_table = 'RA' THEN
7037 
7038            l_ae_line_rec.ae_line_type    := 'EDISC' ;
7039 
7040       /*----------------------------------------------------------------------------+
7041        | Populate source type for unearned discounts                                |
7042        +----------------------------------------------------------------------------*/
7043         ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
7044 
7045               l_ae_line_rec.ae_line_type := 'ADJ';
7046 
7047       /*----------------------------------------------------------------------------+
7048        | Populate source type for finance charges                                   |
7049        +----------------------------------------------------------------------------*/
7050         ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
7051 
7052               l_ae_line_rec.ae_line_type := 'FINCHRG';
7053 
7054         END IF;
7055 
7056         l_ae_line_rec.ae_account := g_ae_rule_rec.code_combination_id1;
7057 
7058   /*----------------------------------------------------------------------------+
7059    | Populate source type for unearned discounts                                |
7060    +----------------------------------------------------------------------------*/
7061     ELSIF p_type_acct = 'UNED' THEN
7062 
7063           l_ae_line_rec.ae_line_type    := 'UNEDISC';
7064           l_ae_line_rec.ae_account         := g_ae_rule_rec.code_combination_id2;
7065 
7066     END IF;
7067 
7068      l_actual_account := l_ae_line_rec.ae_account;
7069 
7070   /*--------------------------------------------------------------------------------+
7071    | Substitute balancing segment for the Activity GL Account, for deposites we     |
7072    | dont need to substitute the segment as the account is derived by Autoaccounting|
7073    | and should be in the balancing segment as that of the Receivable on Deposit    |
7074    +--------------------------------------------------------------------------------*/
7075     IF (g_ae_doc_rec.other_flag IN ('COMMITMENT', 'CHARGEBACK', 'CBREVERSAL')) THEN
7076        arp_standard.debug('Account derived by Autoaccounting');
7077     ELSE
7078      -- Bugfix 1948917.
7079      IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
7080      Substitute_Ccid(p_coa_id        => g_ae_sys_rec.coa_id             ,
7081                      p_original_ccid => l_actual_account                ,
7082                      p_subs_ccid     => g_ae_rule_rec.receivable_account,
7083                      p_actual_ccid   => l_ae_line_rec.ae_account             );
7084      ELSE
7085       l_ae_line_rec.ae_account := l_actual_account;
7086      END IF;
7087     END IF;
7088 
7089   /*----------------------------------------------------------------------------+
7090    | Set the activity ccid which will be stamped on adj.code_combination_id     |
7091    +----------------------------------------------------------------------------*/
7092     IF adj_code_combination_id IS NULL
7093        AND (((g_ae_rule_rec.freight_amt_alloc + g_ae_rule_rec.charges_amt_alloc +
7094              l_line_tax_amt) <> 0)
7095             OR ((g_ae_rule_rec.freight_acctd_amt_alloc + g_ae_rule_rec.charges_acctd_amt_alloc +
7096                  l_line_tax_acctd_amt) <> 0)) THEN
7097        adj_code_combination_id := l_ae_line_rec.ae_account;
7098     END IF;
7099 
7100   /*----------------------------------------------------------------------------+
7101    | Assign Accounting Debits and Credits based on prorated amount signs        |
7102    +----------------------------------------------------------------------------*/
7103      Create_Debits_Credits(g_ae_rule_rec.freight_amt_alloc       +
7104                            g_ae_rule_rec.charges_amt_alloc       +
7105                            l_line_tax_amt                          ,
7106                            g_ae_rule_rec.freight_acctd_amt_alloc +
7107                            g_ae_rule_rec.charges_acctd_amt_alloc +
7108                            l_line_tax_acctd_amt                    ,
7109                            ''                                      ,
7110                            ''                                      ,
7111                            '','',
7112                            l_ae_line_rec );
7113 
7114   /*----------------------------------------------------------------------------+
7115    | Assign built sum of Charges and Freight amounts to Activity gl account     |
7116    +----------------------------------------------------------------------------*/
7117      Assign_Elements(l_ae_line_rec);
7118 
7119   END IF; --end if p_build_all is TRUE
7120 
7121   arp_standard.debug('ARP_ALLOCATION_PKG.Build_Charges_Freight_All()-');
7122 
7123 EXCEPTION
7124   WHEN OTHERS THEN
7125      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Build_Charges_Freight_All');
7126      RAISE;
7127 
7128 END Build_Charges_Freight_All;
7129 
7130 
7131 /* ==========================================================================
7132  | PROCEDURE Substitute_Ccid
7133  |
7134  | DESCRIPTION
7135  |    Builds the gain, loss, round account based on input parameters
7136  |
7137  | SCOPE - PRIVATE
7138  |
7139  | PARAMETERS
7140  |    p_coa_id                IN    Chart of Accounts id
7141  |    p_original_ccid         IN    Original ccid
7142  |    p_subs_ccid             IN    Substitute ccid
7143  |    p_actual_ccid           OUT   Actual or return ccid
7144  *==========================================================================*/
7145 PROCEDURE Substitute_Ccid(p_coa_id        IN  gl_sets_of_books.chart_of_accounts_id%TYPE        ,
7146                           p_original_ccid IN  ar_system_parameters.code_combination_id_gain%TYPE,
7147                           p_subs_ccid     IN  ar_system_parameters.code_combination_id_gain%TYPE,
7148                           p_actual_ccid   OUT NOCOPY ar_system_parameters.code_combination_id_gain%TYPE) IS
7149 
7150 l_concat_segs           varchar2(240)                                           ;
7151 l_concat_ids            varchar2(2000)                                          ;
7152 l_concat_descs          varchar2(2000)                                          ;
7153 l_arerror               varchar2(2000)                                          ;
7154 l_actual_gain_loss_ccid ar_system_parameters_all.code_combination_id_gain%TYPE  ;
7155 l_ctr                   BINARY_INTEGER                                          ;
7156 
7157 BEGIN
7158 
7159    IF PG_DEBUG in ('Y', 'C') THEN
7160       arp_standard.debug( 'ARP_ALLOCATION_PKG.Substitute_Ccid()+');
7161    END IF;
7162 
7163 /*----------------------------------------------------------------------------+
7164  | Set other in out variables used by flex routine                            |
7165  +----------------------------------------------------------------------------*/
7166    p_actual_ccid           := NULL;
7167    l_actual_gain_loss_ccid := NULL; --must always be derived
7168    l_concat_segs           := NULL;
7169    l_concat_ids            := NULL;
7170    l_concat_descs          := NULL;
7171 
7172 /*----------------------------------------------------------------------------+
7173  | Verify from Cache whether the final ccid for a given combination of chart  |
7174  | of accounts, orig ccid and substitute ccid already exists in cache.        |
7175  +----------------------------------------------------------------------------*/
7176 
7177    IF cache_ctr > 0 THEN
7178       FOR l_ctr IN flex_parms_tbl.FIRST .. flex_parms_tbl.LAST LOOP
7179           IF flex_parms_tbl(l_ctr).coa_id = p_coa_id AND
7180                 flex_parms_tbl(l_ctr).orig_ccid = p_original_ccid  AND
7181                    flex_parms_tbl(l_ctr).subs_ccid = p_subs_ccid THEN --hit found
7182 
7183              l_actual_gain_loss_ccid := flex_parms_tbl(l_ctr).actual_ccid;
7184 
7185              IF PG_DEBUG in ('Y', 'C') THEN
7186                 arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder Cache: Chart of Accounts ' || p_coa_id);
7187                 arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder Cache: Original CCID     ' || p_original_ccid);
7188                 arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder Cache: Substitute CCID   ' || p_subs_ccid);
7189                 arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder Cache: Actual CCID       ' || l_actual_gain_loss_ccid);
7190              END IF;
7191 
7192              EXIT; --exit loop as hit is found
7193 
7194           END IF;
7195       END LOOP;
7196    END IF;
7197 
7198 /*----------------------------------------------------------------------------+
7199  | Derive gain loss account using flex routine                                |
7200  +----------------------------------------------------------------------------*/
7201    IF l_actual_gain_loss_ccid is NULL THEN
7202 
7203       IF NOT ar_flexbuilder_wf_pkg.substitute_balancing_segment (
7204                                               x_arflexnum     => p_coa_id                         ,
7205                                               x_arorigccid    => p_original_ccid                  ,
7206                                               x_arsubsticcid  => p_subs_ccid                      ,
7207                                               x_return_ccid   => l_actual_gain_loss_ccid          ,
7208                                               x_concat_segs   => l_concat_segs                    ,
7209                                               x_concat_ids    => l_concat_ids                     ,
7210                                               x_concat_descrs => l_concat_descs                   ,
7211                                               x_arerror       => l_arerror                          ) THEN
7212 
7213        /*----------------------------------------------------------------------------+
7214         | Invalid account raise user exception                                       |
7215         +----------------------------------------------------------------------------*/
7216          RAISE flex_subs_ccid_error;
7217 
7218       END IF;
7219 
7220     /*----------------------------------------------------------------------------+
7221      | Cache the gain loss account as it has been successfully derived            |
7222      +----------------------------------------------------------------------------*/
7223       cache_ctr := cache_ctr + 1;  --counter is never reset within a success unit
7224       flex_parms_tbl(cache_ctr).coa_id      := p_coa_id;
7225       flex_parms_tbl(cache_ctr).orig_ccid   := p_original_ccid;
7226       flex_parms_tbl(cache_ctr).subs_ccid   := p_subs_ccid;
7227       flex_parms_tbl(cache_ctr).actual_ccid := l_actual_gain_loss_ccid;
7228 
7229       IF PG_DEBUG in ('Y', 'C') THEN
7230          arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder : Chart of Accounts ' || p_coa_id);
7231          arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder : Original CCID     ' || p_original_ccid);
7232          arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder : Substitute CCID   ' || p_subs_ccid);
7233          arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder : Actual CCID       ' || l_actual_gain_loss_ccid);
7234       END IF;
7235 
7236    END IF;
7237 
7238    p_actual_ccid := l_actual_gain_loss_ccid;
7239 
7240    IF PG_DEBUG in ('Y', 'C') THEN
7241       arp_standard.debug( 'ARP_ALLOCATION_PKG.Substitute_Ccid()-');
7242    END IF;
7243 
7244 EXCEPTION
7245 WHEN flex_subs_ccid_error  THEN
7246      IF PG_DEBUG in ('Y', 'C') THEN
7247         arp_standard.debug('Flexbuilder error: ARP_ALLOCATION_PKG.Substitute_Ccid');
7248         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Chart of Accounts ' || p_coa_id);
7249         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Original CCID     ' || p_original_ccid);
7250         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Substitute CCID   ' || p_subs_ccid);
7251         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Actual CCID       ' || l_actual_gain_loss_ccid);
7252      END IF;
7253      fnd_message.set_name('AR','AR_FLEX_CCID_ERROR');
7254      fnd_message.set_token('COA',TO_CHAR(p_coa_id));
7255      fnd_message.set_token('ORG_CCID',TO_CHAR(p_original_ccid));
7256      fnd_message.set_token('SUB_CCID',TO_CHAR(p_subs_ccid));
7257      RAISE;
7258 
7259 WHEN OTHERS THEN
7260      IF PG_DEBUG in ('Y', 'C') THEN
7261         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Substitute_Ccid');
7262         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Chart of Accounts ' || p_coa_id);
7263         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Original CCID     ' || p_original_ccid);
7264         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Substitute CCID   ' || p_subs_ccid);
7265         arp_standard.debug('Substitute_Ccid: ' || 'Flexbuilder error: Actual CCID       ' || l_actual_gain_loss_ccid);
7266      END IF;
7267      RAISE;
7268 
7269 END Substitute_Ccid;
7270 
7271 /* ==========================================================================
7272  | PROCEDURE Create_Debits_Credits
7273  |
7274  | DESCRIPTION
7275  |    Populates accounting debit and credit amounts and taxable amounts based
7276  |    on signs
7277  |
7278  | NOTES
7279  |    Them amounts and accounted amounts are always in the same sign as the
7280  |    other or zero. When both the amounts and accounted amounts are zero,
7281  |    the taxable amount or taxable accounted amount are used to determine
7282  |    the debits and credit entries.
7283  |
7284  |    This routine is never called when both amount and taxable amounts are
7285  |    zero.
7286  |
7287  | SCOPE - PRIVATE
7288  |
7289  | PARAMETERS
7290  |    p_amount                IN    Amount
7291  |    p_acctd_amount          IN    Accounted Amount
7292  |    p_taxable_amount        IN    Taxable Amount
7293  |    p_taxable_acctd_amount  IN    Taxable Accounted Amount
7294  |    p_ae_line_rec           OUT   Line record
7295  | History
7296  |  21-NOV-2003   Herve Yu    from_amount_dr      , from_amount_cr
7297  |                            from_acctd_amount_dr, from_acctd_amount_cr
7298  *==========================================================================*/
7299 PROCEDURE Create_Debits_Credits(p_amount               IN NUMBER       ,
7300                                 p_acctd_amount         IN NUMBER       ,
7301                                 p_taxable_amount       IN NUMBER       ,
7302                                 p_taxable_acctd_amount IN NUMBER       ,
7303                                 --HYU--{
7304                                 p_from_amount               IN NUMBER       ,
7305                                 p_from_acctd_amount         IN NUMBER       ,
7306                                 --HYU--}
7307                                 p_ae_line_rec          IN OUT NOCOPY ar_ae_alloc_rec_gt%ROWTYPE,
7308                                 p_paired_flag          IN VARCHAR2 DEFAULT NULL,
7309                                 p_calling_point        IN VARCHAR2 DEFAULT NULL) IS
7310 
7311 l_taxable_set BOOLEAN;
7312 
7313 BEGIN
7314    arp_standard.debug( 'ARP_ALLOCATION_PKG.Create_Debits_Credits()+');
7315 
7316    arp_standard.debug('p_amount ' || p_amount);
7317    arp_standard.debug('p_acctd_amount ' || p_acctd_amount);
7318    arp_standard.debug('p_taxable_amount' || p_taxable_amount);
7319    arp_standard.debug('p_taxable_acctd_amount' || p_taxable_acctd_amount);
7320    arp_standard.debug('p_calling_point       ' || p_calling_point);
7321 
7322    l_taxable_set := FALSE;
7323 
7324  /*----------------------------------------------------------------------------+
7325   | Set negativity indicator for 0 amounts and taxable amounts based on sign of|
7326   | the receivable. When summarizing finally just use the indicator do not set |
7327   +----------------------------------------------------------------------------*/
7328    IF ((p_amount = 0) AND (p_acctd_amount = 0) AND (nvl(p_taxable_amount,0) = 0)
7329       AND (nvl(p_taxable_acctd_amount,0) = 0) AND (nvl(p_calling_point,'X') <> 'SUMMARIZE')) THEN
7330 
7331       arp_standard.debug('g_ae_rule_rec.line_amt_alloc ' || g_ae_rule_rec.line_amt_alloc);
7332       arp_standard.debug('g_ae_rule_rec.tax_amt_alloc ' || g_ae_rule_rec.tax_amt_alloc);
7333       arp_standard.debug('p_paired_flag ' || p_paired_flag);
7334 
7335     --set negativity indicator for debits nullvalue indicates a credit
7336       IF ((g_ae_rule_rec.line_amt_alloc + g_ae_rule_rec.tax_amt_alloc) < 0) THEN
7337          p_ae_line_rec.ae_neg_ind := -1;
7338 
7339          IF nvl(p_paired_flag, 'N') = 'Y' THEN
7340             p_ae_line_rec.ae_neg_ind := NULL; --Create Credit to Collected tax account
7341          END IF;
7342 
7343          arp_standard.debug('p_ae_line_rec.ae_neg_ind < 0 condition ' || p_ae_line_rec.ae_neg_ind);
7344 
7345       ELSE
7346          p_ae_line_rec.ae_neg_ind := NULL; --set to create Credits
7347 
7348          IF nvl(p_paired_flag, 'N') = 'Y' THEN
7349             p_ae_line_rec.ae_neg_ind := -1;  --Create Debit to collected tax account
7350          END IF;
7351 
7352         IF PG_DEBUG in ('Y', 'C') THEN
7353          arp_standard.debug('p_ae_line_rec.ae_neg_ind >= 0 condition ' || p_ae_line_rec.ae_neg_ind);
7354         END IF;
7355 
7356       END IF; --default is null
7357 
7358    END IF; --end if negativity indicator set condition
7359 
7360  /*----------------------------------------------------------------------------+
7361   | Create accounting amount and taxable amount debits based on signs          |
7362   +----------------------------------------------------------------------------*/
7363    IF ((p_amount < 0) OR (p_acctd_amount < 0)) THEN
7364       p_ae_line_rec.ae_entered_dr           := abs(p_amount)              ;
7365       p_ae_line_rec.ae_accounted_dr         := abs(p_acctd_amount)        ;
7366 
7367       p_ae_line_rec.ae_entered_cr           := NULL                       ;
7368       p_ae_line_rec.ae_accounted_cr         := NULL                       ;
7369       p_ae_line_rec.ae_from_amount_dr       := abs(p_from_amount)              ;
7370       p_ae_line_rec.ae_from_acctd_amount_dr := abs(p_from_acctd_amount)        ;
7371       p_ae_line_rec.ae_from_amount_cr       := NULL                       ;
7372       p_ae_line_rec.ae_from_acctd_amount_cr := NULL                       ;
7373 
7374  --Tax amounts are zero, however taxable amounts used to determine Dr, Cr
7375    ELSIF (((p_amount = 0) AND (p_acctd_amount = 0))
7376           AND ((nvl(p_taxable_amount,0) <> 0) OR (nvl(p_taxable_acctd_amount,0) <> 0))) THEN
7377  /*----------------------------------------------------------------------------+
7378   | Create accounting amount and taxable amount credits based on sign          |
7379   +----------------------------------------------------------------------------*/
7380       IF ((nvl(p_taxable_amount,0) < 0) OR (nvl(p_taxable_acctd_amount,0) < 0)) THEN
7381          p_ae_line_rec.ae_entered_dr           := abs(p_amount)              ;
7382          p_ae_line_rec.ae_accounted_dr         := abs(p_acctd_amount)        ;
7383          p_ae_line_rec.ae_from_amount_dr       := abs(p_from_amount)              ;
7384          p_ae_line_rec.ae_from_acctd_amount_dr := abs(p_from_acctd_amount)        ;
7385 
7386          IF p_ae_line_rec.ae_line_type IN ('TAX','DEFERRED_TAX','ADJ_NON_REC_TAX',
7387                                            'EDISC_NON_REC_TAX','UNEDISC_NON_REC_TAX',
7388                                            'FINCHRG_NON_REC_TAX')
7389          THEN
7390             l_taxable_set := TRUE;
7391             p_ae_line_rec.ae_taxable_entered_dr   := abs(p_taxable_amount);
7392             p_ae_line_rec.ae_taxable_accounted_dr := abs(p_taxable_acctd_amount);
7393          END IF;
7394 
7395          p_ae_line_rec.ae_entered_cr           := NULL                       ;
7396          p_ae_line_rec.ae_accounted_cr         := NULL                       ;
7397          p_ae_line_rec.ae_taxable_entered_cr   := NULL                       ;
7398          p_ae_line_rec.ae_taxable_accounted_cr := NULL                       ;
7399          p_ae_line_rec.ae_from_amount_cr       := NULL                       ;
7400          p_ae_line_rec.ae_from_acctd_amount_cr := NULL                       ;
7401 
7402       ELSE  --Create Credits
7403          p_ae_line_rec.ae_entered_cr           := abs(p_amount)              ;
7404          p_ae_line_rec.ae_accounted_cr         := abs(p_acctd_amount)        ;
7405          p_ae_line_rec.ae_from_amount_cr       := abs(p_from_amount)         ;
7406          p_ae_line_rec.ae_from_acctd_amount_cr := abs(p_from_acctd_amount)   ;
7407 
7408          --{need to comment this out for taxable amount in the case of application
7409          --IF p_ae_line_rec.ae_line_type IN ('TAX','DEFERRED_TAX','ADJ_NON_REC_TAX',
7410          --                                  'EDISC_NON_REC_TAX','UNEDISC_NON_REC_TAX',
7411          --                                  'FINCHRG_NON_REC_TAX')
7412          --THEN
7413          --   l_taxable_set := TRUE;
7414            p_ae_line_rec.ae_taxable_entered_cr   := abs(p_taxable_amount);
7415            p_ae_line_rec.ae_taxable_accounted_cr := abs(p_taxable_acctd_amount);
7416          --END IF;
7417 
7418          p_ae_line_rec.ae_entered_dr           := NULL                       ;
7419          p_ae_line_rec.ae_accounted_dr         := NULL                       ;
7420          p_ae_line_rec.ae_taxable_entered_dr   := NULL                       ;
7421          p_ae_line_rec.ae_taxable_accounted_dr := NULL                       ;
7422          p_ae_line_rec.ae_from_amount_dr       := NULL              ;
7423          p_ae_line_rec.ae_from_acctd_amount_dr := NULL              ;
7424 
7425       END IF; --line amount is negative for Debits
7426 
7427  /*------------------------------------------------------------------------------------------+
7428   |Tax amounts are zero, taxable amounts are also zero use negativity indicator for Dr and Cr|
7429   |we need to do this for tax lines, and only when called from the final Summary routine.    |
7430   +------------------------------------------------------------------------------------------*/
7431    ELSIF (((p_amount = 0) AND (p_acctd_amount = 0))
7432           AND (nvl(p_taxable_amount,0) = 0) AND (nvl(p_taxable_acctd_amount,0) = 0)
7433           AND (nvl(p_calling_point, 'X') = 'SUMMARIZE')) THEN
7434 
7435           IF (nvl(p_ae_line_rec.ae_neg_ind,0) < 0) THEN --Create Debits
7436              p_ae_line_rec.ae_entered_dr           := abs(p_amount)               ;
7437              p_ae_line_rec.ae_accounted_dr         := abs(p_acctd_amount)         ;
7438              p_ae_line_rec.ae_taxable_entered_dr   := abs(p_taxable_amount)       ;
7439              p_ae_line_rec.ae_taxable_accounted_dr := abs(p_taxable_acctd_amount) ;
7440              p_ae_line_rec.ae_from_amount_dr       := abs(p_from_amount)              ;
7441              p_ae_line_rec.ae_from_acctd_amount_dr := abs(p_from_acctd_amount)        ;
7442 
7443              l_taxable_set := TRUE;
7444 
7445              p_ae_line_rec.ae_entered_cr           := NULL                       ;
7446              p_ae_line_rec.ae_accounted_cr         := NULL                       ;
7447              p_ae_line_rec.ae_taxable_entered_cr   := NULL                       ;
7448              p_ae_line_rec.ae_taxable_accounted_cr := NULL                       ;
7449              p_ae_line_rec.ae_from_amount_cr       := NULL                       ;
7450              p_ae_line_rec.ae_from_acctd_amount_cr := NULL                       ;
7451 
7452           ELSE --create Credits for accounting entry
7453              p_ae_line_rec.ae_entered_cr           := abs(p_amount)              ;
7454              p_ae_line_rec.ae_accounted_cr         := abs(p_acctd_amount)        ;
7455              p_ae_line_rec.ae_taxable_entered_cr   := abs(p_taxable_amount)      ;
7456              p_ae_line_rec.ae_taxable_accounted_cr := abs(p_taxable_acctd_amount);
7457              p_ae_line_rec.ae_from_amount_cr       := abs(p_from_amount)         ;
7458              p_ae_line_rec.ae_from_acctd_amount_cr := abs(p_from_acctd_amount)   ;
7459              l_taxable_set := TRUE;
7460 
7461              p_ae_line_rec.ae_entered_dr           := NULL                       ;
7462              p_ae_line_rec.ae_accounted_dr         := NULL                       ;
7463              p_ae_line_rec.ae_taxable_entered_dr   := NULL                       ;
7464              p_ae_line_rec.ae_taxable_accounted_dr := NULL                       ;
7465              p_ae_line_rec.ae_from_amount_dr       := NULL        ;
7466              p_ae_line_rec.ae_from_acctd_amount_dr := NULL        ;
7467 
7468           END IF; --negativity indicator
7469 
7470    ELSE --create Credits
7471  /*----------------------------------------------------------------------------+
7472   | Create accounting amount and taxable amount credits based on sign          |
7473   +----------------------------------------------------------------------------*/
7474       p_ae_line_rec.ae_entered_cr           := abs(p_amount)              ;
7475       p_ae_line_rec.ae_accounted_cr         := abs(p_acctd_amount)        ;
7476 
7477       p_ae_line_rec.ae_entered_dr           := NULL                       ;
7478       p_ae_line_rec.ae_accounted_dr         := NULL                       ;
7479       p_ae_line_rec.ae_from_amount_cr       := abs(p_from_amount)              ;
7480       p_ae_line_rec.ae_from_acctd_amount_cr := abs(p_from_acctd_amount)        ;
7481       p_ae_line_rec.ae_from_amount_dr       := NULL                       ;
7482       p_ae_line_rec.ae_from_acctd_amount_dr := NULL                       ;
7483 
7484    END IF; --sign of amounts
7485 
7486  /*----------------------------------------------------------------------------+
7487   | Set the taxable amounts and accounted amounts                              |
7488   +----------------------------------------------------------------------------*/
7489  -- This need to be executed every time???
7490  --  IF (p_ae_line_rec.ae_line_type IN ('TAX', 'DEFERRED_TAX', 'ADJ_NON_REC_TAX',
7491  --                                    'EDISC_NON_REC_TAX','UNEDISC_NON_REC_TAX',
7492  --                                    'FINCHRG_NON_REC_TAX')
7493  --     AND (NOT l_taxable_set)) THEN
7494 
7495  --      l_taxable_set := TRUE;
7496 
7497       IF ((nvl(p_taxable_amount,0) < 0) OR (nvl(p_taxable_acctd_amount,0) < 0))
7498       THEN
7499           p_ae_line_rec.ae_taxable_entered_dr   := abs(p_taxable_amount)       ;
7500           p_ae_line_rec.ae_taxable_accounted_dr := abs(p_taxable_acctd_amount) ;
7501           p_ae_line_rec.ae_taxable_entered_cr   := NULL                        ;
7502           p_ae_line_rec.ae_taxable_accounted_cr := NULL                        ;
7503 
7504       ELSIF (((nvl(p_taxable_amount,0) = 0) AND (nvl(p_taxable_acctd_amount,0) = 0))
7505              AND ((p_amount <> 0) OR (p_acctd_amount <> 0)))
7506       THEN
7507              IF ((p_amount < 0) OR (p_acctd_amount < 0)) THEN
7508                 p_ae_line_rec.ae_taxable_entered_dr    := abs(p_taxable_amount);
7509                 p_ae_line_rec.ae_taxable_accounted_dr  := abs(p_taxable_acctd_amount);
7510                 p_ae_line_rec.ae_taxable_entered_cr    := NULL;
7511                 p_ae_line_rec.ae_taxable_accounted_cr  := NULL;
7512              ELSE
7513                 p_ae_line_rec.ae_taxable_entered_cr    := abs(p_taxable_amount);
7514                 p_ae_line_rec.ae_taxable_accounted_cr  := abs(p_taxable_acctd_amount);
7515                 p_ae_line_rec.ae_taxable_entered_dr    := NULL;
7516                 p_ae_line_rec.ae_taxable_accounted_dr  := NULL;
7517              END IF;
7518       ELSE
7519           p_ae_line_rec.ae_taxable_entered_cr   := abs(p_taxable_amount)       ;
7520           p_ae_line_rec.ae_taxable_accounted_cr := abs(p_taxable_acctd_amount) ;
7521           p_ae_line_rec.ae_taxable_entered_dr   := NULL                        ;
7522           p_ae_line_rec.ae_taxable_accounted_dr := NULL                        ;
7523 
7524       END IF; --sign of taxable amount
7525 
7526 --   END IF; --if line type is tax and taxable amount is not set
7527 --   arp_standard.debug('Start Credit Debit Dump');
7528 --   Dump_Line_Amts(p_ae_line_rec);
7529 --   arp_standard.debug('End Credit Debit Dump');
7530 
7531    arp_standard.debug( 'ARP_ALLOCATION_PKG.Create_Debits_Credits()-');
7532 
7533 EXCEPTION
7534 WHEN OTHERS THEN
7535      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Create_Debits_Credits');
7536      RAISE;
7537 
7538 END Create_Debits_Credits;
7539 
7540 /* ==========================================================================
7541  | PROCEDURE Summarize_Accounting_Lines
7542  |
7543  | DESCRIPTION
7544  |    Net out the accounting for earned discounts, unearned discounts and
7545  |    payments, for adjustment (includes finance charges) multiple layers
7546  |    of accounting lines will not exist however this routine will be
7547  |    executed.
7548  |
7549  | SCOPE - PRIVATE
7550  |
7551  | PARAMETERS
7552  |    NONE
7553  | History
7554  |   21-NOV-2003  Herve Yu    from_amount_dr       , from_amount_cr,
7555  |                            from_acctd_amount_dr , from_acctd_amount_cr
7556  *==========================================================================*/
7557 PROCEDURE Summarize_Accounting_Lines IS
7558 
7559 l_ctr1           BINARY_INTEGER;
7560 l_ctr2           BINARY_INTEGER;
7561 --BUG#3509185  l_summ_ctr       BINARY_INTEGER := 0;
7562 l_ent_amt        NUMBER;
7563 l_ent_acctd_amt  NUMBER;
7564 l_txb_amt        NUMBER;
7565 l_txb_acctd_amt  NUMBER;
7566 l_from_ent_amt        NUMBER;
7567 l_from_ent_acctd_amt  NUMBER;
7568 l_ae_line_rec    ar_ae_alloc_rec_gt%ROWTYPE;
7569 l_ae_empty_rec   ar_ae_alloc_rec_gt%ROWTYPE;
7570 
7571 CURSOR summarize_lines IS
7572 SELECT /*+ INDEX(a1 AR_AE_ALLOC_REC_GT_N3) */
7573        NVL(a1.ae_entered_dr,0)           * -1 + NVL(a1.ae_entered_cr,0),
7574        NVL(a1.ae_accounted_dr,0)         * -1 + NVL(a1.ae_accounted_cr,0),
7575 --       SUM(NVL(ae_pro_amt,0)),
7576 --       SUM(NVL(ae_pro_acctd_amt,0)),
7577        NVL(a1.ae_taxable_entered_dr,0)   * -1 + NVL(a1.ae_taxable_entered_cr,0),
7578        NVL(a1.ae_taxable_accounted_dr,0) * -1 + NVL(a1.ae_taxable_accounted_cr,0),
7579        NVL(a1.ae_from_amount_dr,0)       * -1 + NVL(a1.ae_from_amount_cr,0),
7580        NVL(a1.ae_from_acctd_amount_dr,0) * -1 + NVL(a1.ae_from_acctd_amount_cr,0),
7581        a1.ae_line_type,
7582        a1.ae_line_type_secondary,
7583        a1.ae_source_id,
7584        a1.ae_source_table,
7585        a1.ae_account,
7586        a1.ae_source_id_secondary,
7587        a1.ae_source_table_secondary,
7588        a1.ae_currency_code,
7589        a1.ae_currency_conversion_rate,
7590        a1.ae_currency_conversion_type,
7591        a1.ae_currency_conversion_date,
7592        a1.ae_third_party_id,
7593        a1.ae_third_party_sub_id,
7594        a1.ae_tax_group_code_id,
7595        a1.ae_tax_code_id,
7596        a1.ae_location_segment_id,
7597        a1.ae_tax_link_id,
7598        decode(a1.ae_neg_ind,
7599               -1, decode(Retain_Neg_Ind(a1.rowid),
7600                          1, a1.ae_neg_ind,
7601                          ''),
7602               a1.ae_neg_ind),
7603        a1.ae_reversed_source_id,
7604        DECODE(a1.ae_cust_trx_line_gl_dist_id,0 ,''
7605                                             ,-1,'',a1.ae_customer_trx_line_id),
7606        DECODE(a1.ae_cust_trx_line_gl_dist_id,0 ,''
7607                                             ,-1,'',a1.ae_cust_trx_line_gl_dist_id),
7608        a1.ae_ref_line_id,
7609        a1.ref_account_class,
7610        a1.activity_bucket,
7611        a1.ref_dist_ccid,
7612        a1.ref_mf_dist_flag
7613 FROM ar_ae_alloc_rec_gt a1
7614 WHERE a1.ae_id = g_id
7615 AND a1.ae_summarize_flag = 'N'
7616 AND a1.ae_account_class IS NULL
7617 AND (NVL(a1.ae_entered_dr,0) <> 0 OR NVL(a1.ae_entered_cr,0) <> 0 OR
7618      NVL(a1.ae_accounted_dr,0) <> 0 OR NVL(a1.ae_accounted_cr,0) <> 0  /*6321537*/
7619      OR (a1.ae_line_type IN ('TAX','DEFERRED_TAX','EDISC_NON_REC_TAX',
7620                              'UNEDISC_NON_REC_TAX','ADJ_NON_REC_TAX',
7621                               'FINCHRG_NON_REC_TAX')
7622       AND (NVL(a1.ae_entered_dr,0) = 0 AND NVL(a1.ae_entered_cr,0) = 0 AND
7623            NVL(a1.ae_accounted_dr,0) = 0 AND NVL(a1.ae_accounted_cr,0) = 0)));
7624 --AND NVL(ae_pro_amt,0) <> 0
7625 --}
7626 /*
7627 GROUP BY  a1.ae_customer_trx_line_id,
7628           a1.ae_cust_trx_line_gl_dist_id,
7629           a1.ae_ref_line_id,
7630           a1.ae_line_type,
7631           a1.ae_line_type_secondary,
7632           a1.ae_source_id,
7633           a1.ae_source_table,
7634           a1.ae_account,
7635           a1.ae_source_id_secondary,
7636           a1.ae_source_table_secondary,
7637           a1.ae_currency_code,
7638           a1.ae_currency_conversion_rate,
7639           a1.ae_currency_conversion_type,
7640           a1.ae_currency_conversion_date,
7641           a1.ae_third_party_id,
7642           a1.ae_third_party_sub_id,
7643           a1.ae_tax_group_code_id,
7644           a1.ae_tax_code_id,
7645           a1.ae_location_segment_id,
7646           a1.ae_tax_link_id,
7647           decode(a1.ae_neg_ind,
7648                  -1, decode(Retain_Neg_Ind(a1.rowid),
7649                             1, a1.ae_neg_ind,
7650                             ''),
7651                  a1.ae_neg_ind),
7652           a1.ae_reversed_source_id,
7653 --{HYUDETUPT
7654           a1.ref_account_class,
7655           a1.activity_bucket
7656 --}
7657  ORDER BY  decode(a1.ae_line_type,
7658                 'EDISC'              ,-6,
7659                 'ADJ'                ,-6,
7660                 'FINCHRG'            ,-6,
7661                 'UNEDISC'            ,-6,
7662                 a1.ae_tax_link_id),
7663           decode(a1.ae_line_type,
7664                  'EDISC_NON_REC_TAX' , -5,
7665                  'ADJ_NON_REC_TAX'    ,-5,
7666                  'FINCHRG_NON_REC_TAX',-5,
7667                  'UNEDISC_NON_REC_TAX',-5,
7668                  'DEFERRED_TAX',decode(a1.ae_line_type_secondary,
7669                                        'EDISC'  , -4,
7670                                        'ADJ'    , -4,
7671                                        'FINCHRG', -4,
7672                                        'UNEDISC', -4,
7673                                        -2),
7674                  'TAX'         ,decode(a1.ae_line_type_secondary,
7675                                        'EDISC'  , -3,
7676                                        'ADJ'    , -3,
7677                                        'FINCHRG', -3,
7678                                        'UNEDISC', -3,
7679                                        -1),
7680                 a1.ae_tax_link_id);
7681 */
7682 
7683 BEGIN
7684     arp_standard.debug( 'ARP_ALLOCATION_PKG.Summarize_Accounting_Lines()+');
7685  /*------------------------------------------------------------------------------+
7686   |Summarize Accounting entries for revenue and tax to net out accounting entries|
7687   |because the table g_ae_line_tbl contains accounting for earned discounts,     |
7688   |unearned discounts and payments, so the requirement for another level of      |
7689   |summarization for netting                                                     |
7690   +------------------------------------------------------------------------------*/
7691    --Bulk processing of distribution records [Bug 6454022]
7692    IF NVL(g_simul_app,'N') = 'N' THEN
7693      IF (NVL(g_ae_sys_rec.sob_type,'P') = 'P') THEN
7694 
7695 	INSERT INTO  ar_distributions (
7696 		line_id,
7697 		source_id,
7698 		source_table,
7699 		source_type,
7700 		source_type_secondary,
7701 		code_combination_id,
7702 		amount_dr,
7703 		amount_cr,
7704 		acctd_amount_dr,
7705 		acctd_amount_cr,
7706 		created_by,
7707 		creation_date,
7708 		last_updated_by,
7709 		last_update_date,
7710 		last_update_login,
7711 		source_id_secondary,
7712 		source_table_secondary,
7713 		currency_code        ,
7714 		currency_conversion_rate,
7715 		currency_conversion_type,
7716 		currency_conversion_date,
7717 		third_party_id,
7718 		third_party_sub_id,
7719 		tax_code_id,
7720 		location_segment_id,
7721 		taxable_entered_dr,
7722 		taxable_entered_cr,
7723 		taxable_accounted_dr,
7724 		taxable_accounted_cr,
7725 		tax_link_id,
7726 		reversed_source_id,
7727 		tax_group_code_id,
7728 		org_id,
7729 		ref_customer_trx_line_id,
7730 		ref_cust_trx_line_gl_dist_id,
7731 		ref_line_id,
7732 		from_amount_dr,
7733 		from_amount_cr,
7734 		from_acctd_amount_dr,
7735 		from_acctd_amount_cr,
7736 		ref_account_class,
7737 		activity_bucket,
7738 		ref_dist_ccid,
7739 		ref_mf_dist_flag
7740   	 )
7741 	 SELECT   ar_distributions_s.nextval,
7742 		  al.ae_source_id,
7743 		  al.ae_source_table,
7744 		  al.ae_line_type,
7745 		  al.ae_line_type_secondary,
7746 		  al.ae_account,
7747 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN  abs(amount)
7748 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
7749 		         CASE WHEN (nvl(taxable_amount,0) < 0) OR
7750 				   (nvl(taxable_acctd_amount,0) < 0) THEN abs(amount)
7751 			      WHEN (nvl(taxable_amount,0) = 0) AND
7752 				   (nvl(taxable_acctd_amount,0) = 0) AND
7753 				   (nvl(ae_neg_ind,0) < 0) THEN abs(amount)
7754 			      ELSE null END
7755 		  ELSE  null END  ae_entered_dr,
7756 
7757 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN null
7758 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
7759 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
7760 				   (nvl(taxable_acctd_amount,0) < 0) THEN null
7761 			      WHEN (nvl(taxable_amount,0) = 0) AND
7762 				   (nvl(taxable_acctd_amount,0) = 0) AND
7763 				   (nvl(ae_neg_ind,0) < 0) THEN null
7764 			 ELSE abs(amount) END
7765 		  ELSE abs(amount) END ae_entered_cr,
7766 
7767 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN abs(acctd_amount)
7768 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
7769 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
7770 			           (nvl(taxable_acctd_amount,0) < 0) THEN abs(acctd_amount)
7771 			      WHEN (nvl(taxable_amount,0) = 0) AND
7772 				   (nvl(taxable_acctd_amount,0) = 0) AND
7773 				   (nvl(ae_neg_ind,0) < 0) THEN abs(acctd_amount)
7774 			 ELSE null END
7775 		  ELSE null END ae_accounted_dr,
7776 
7777 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN null
7778 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
7779 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
7780 				   (nvl(taxable_acctd_amount,0) < 0) THEN null
7781 			      WHEN (nvl(taxable_amount,0) = 0) AND
7782 				   (nvl(taxable_acctd_amount,0) = 0) AND
7783 				   (nvl(ae_neg_ind,0) < 0) THEN null
7784 			 ELSE abs(acctd_amount) END
7785 		  ELSE abs(acctd_amount) END ae_accounted_cr,
7786 
7787 		  arp_standard.profile.user_id,
7788 		  SYSDATE,
7789 		  arp_standard.profile.user_id,
7790 		  SYSDATE,
7791 		  arp_standard.profile.last_update_login,
7792 		  al.ae_source_id_secondary,
7793 		  al.ae_source_table_secondary,
7794 		  al.ae_currency_code,
7795 		  al.ae_currency_conversion_rate,
7796 		  al.ae_currency_conversion_type,
7797 		  al.ae_currency_conversion_date,
7798 		  al.ae_third_party_id,
7799 		  al.ae_third_party_sub_id,
7800 		  al.ae_tax_code_id,
7801 		  al.ae_location_segment_id,
7802 		  CASE WHEN ( nvl(taxable_amount,0) < 0) OR
7803 			    ( nvl(taxable_acctd_amount,0) < 0) THEN abs(taxable_amount)
7804 		       WHEN ( nvl(taxable_amount,0) = 0 ) AND
7805 			    ( nvl(taxable_acctd_amount,0) = 0 ) AND
7806 			    ( ( amount < 0 ) OR ( acctd_amount < 0 )) THEN abs(taxable_amount)
7807 		  ELSE	null END  ae_taxable_entered_dr,
7808 
7809 		  CASE WHEN ( nvl(taxable_amount,0) < 0) OR
7810 			    ( nvl(taxable_acctd_amount,0) < 0) THEN null
7811 		       WHEN ( nvl(taxable_amount,0) = 0 ) AND
7812 			    ( nvl(taxable_acctd_amount,0) = 0 ) AND
7813 			    ( ( amount < 0 ) OR ( acctd_amount < 0 ))THEN null
7814 		  ELSE abs(taxable_amount) END ae_taxable_entered_cr,
7815 
7816 		  CASE WHEN ( nvl(taxable_amount,0) < 0) OR
7817 			    ( nvl(taxable_acctd_amount,0) < 0)THEN abs(taxable_acctd_amount)
7818 		       WHEN ( nvl(taxable_amount,0) = 0 ) AND
7819 			    ( nvl(taxable_acctd_amount,0) = 0 ) AND
7820 			    ( ( amount < 0 ) OR ( acctd_amount < 0 ))THEN abs(taxable_acctd_amount)
7821 		  ELSE null END ae_taxable_accounted_dr,
7822 
7823 		  CASE WHEN ( nvl(taxable_amount,0) < 0) OR
7824 			    ( nvl(taxable_acctd_amount,0) < 0)THEN null
7825 		       WHEN ( nvl(taxable_amount,0) = 0 ) AND
7826 			    ( nvl(taxable_acctd_amount,0) = 0 ) AND
7827 			    ( ( amount < 0 ) OR ( acctd_amount < 0 ))THEN null
7828 		  ELSE	abs(taxable_acctd_amount) END ae_taxable_accounted_cr ,
7829 		  al.ae_tax_link_id,
7830 		  al.ae_reversed_source_id,
7831 		  al.ae_tax_group_code_id,
7832 		  arp_standard.sysparm.org_id, /* SSA changes anuj */
7833 		  al.ae_customer_trx_line_id,
7834 		  al.ae_cust_trx_line_gl_dist_id,
7835 		  al.ae_ref_line_id,
7836 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN abs(from_amount)
7837 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
7838 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
7839 				   (nvl(taxable_acctd_amount,0) < 0)THEN abs(from_amount)
7840 			      WHEN (nvl(taxable_amount,0) = 0) AND
7841 				   (nvl(taxable_acctd_amount,0) = 0) AND
7842 				   (nvl(ae_neg_ind,0) < 0) THEN abs(from_amount)
7843 			      ELSE null END
7844 		  ELSE null END ae_from_amount_dr,
7845 
7846 		  CASE WHEN (amount < 0) OR (acctd_amount < 0)THEN null
7847 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
7848 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
7849 				   (nvl(taxable_acctd_amount,0) < 0) THEN null
7850 			      WHEN (nvl(taxable_amount,0) = 0) AND
7851 				   (nvl(taxable_acctd_amount,0) = 0) AND
7852 				   (nvl(ae_neg_ind,0) < 0)THEN null
7853 			 ELSE abs(from_amount) END
7854 		  ELSE abs(from_amount) END ae_from_amount_cr,
7855 
7856 		  CASE WHEN (amount < 0) OR (acctd_amount < 0)THEN abs(from_acctd_amount)
7857 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
7858 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
7859 				   (nvl(taxable_acctd_amount,0) < 0)THEN abs(from_acctd_amount)
7860 			      WHEN (nvl(taxable_amount,0) = 0) AND
7861 				   (nvl(taxable_acctd_amount,0) = 0) AND
7862 				   (nvl(ae_neg_ind,0) < 0)THEN abs(from_acctd_amount)
7863 			 ELSE null END
7864 		  ELSE null END ae_from_acctd_amount_dr,
7865 
7866 		  CASE WHEN (amount < 0) OR (acctd_amount < 0) THEN null
7867 		       WHEN (amount = 0) AND (acctd_amount = 0) THEN
7868 			 CASE WHEN (nvl(taxable_amount,0) < 0) OR
7869 				   (nvl(taxable_acctd_amount,0) < 0)THEN null
7870 			      WHEN (nvl(taxable_amount,0) = 0) AND
7871 				   (nvl(taxable_acctd_amount,0) = 0) AND
7872 				   (nvl(ae_neg_ind,0) < 0)THEN null
7873 			 ELSE abs(from_acctd_amount) END
7874 		  ELSE abs(from_acctd_amount) END ae_from_acctd_amount_cr,
7875 		  al.ref_account_class,
7876 		  al.activity_bucket,
7877 		  al.ref_dist_ccid,
7878 		  al.ref_mf_dist_flag
7879 	  from
7880 	  ( --Defined it as a subquery as some of the values[Amount columns] derived in this
7881 	    --  select are used for further calculations in the main select.
7882 	    SELECT /*+ INDEX(a1 AR_AE_ALLOC_REC_GT_N3) */
7883 		   NVL(a1.ae_entered_dr,0)           * -1 + NVL(a1.ae_entered_cr,0) amount,
7884 		   NVL(a1.ae_accounted_dr,0)         * -1 + NVL(a1.ae_accounted_cr,0) acctd_amount,
7885 		   NVL(a1.ae_taxable_entered_dr,0)   * -1 + NVL(a1.ae_taxable_entered_cr,0)  taxable_amount,
7886 		   NVL(a1.ae_taxable_accounted_dr,0) * -1 + NVL(a1.ae_taxable_accounted_cr,0) taxable_acctd_amount,
7887 		   NVL(a1.ae_from_amount_dr,0)       * -1 + NVL(a1.ae_from_amount_cr,0)  from_amount,
7888 		   NVL(a1.ae_from_acctd_amount_dr,0) * -1 + NVL(a1.ae_from_acctd_amount_cr,0) from_acctd_amount,
7889 		   a1.ae_line_type,
7890 		   a1.ae_line_type_secondary,
7891 		   a1.ae_source_id,
7892 		   a1.ae_source_table,
7893 		   a1.ae_account,
7894 		   a1.ae_source_id_secondary,
7895 		   a1.ae_source_table_secondary,
7896 		   a1.ae_currency_code,
7897 		   a1.ae_currency_conversion_rate,
7898 		   a1.ae_currency_conversion_type,
7899 		   a1.ae_currency_conversion_date,
7900 		   a1.ae_third_party_id,
7901 		   a1.ae_third_party_sub_id,
7902 		   a1.ae_tax_group_code_id,
7903 		   a1.ae_tax_code_id,
7904 		   a1.ae_location_segment_id,
7905 		   a1.ae_tax_link_id,
7906 		   decode(a1.ae_neg_ind,
7907 			  -1, decode(Retain_Neg_Ind(a1.rowid),
7908 				     1, a1.ae_neg_ind,
7909 				     ''),
7910 			  a1.ae_neg_ind) ae_neg_ind,
7911 		   a1.ae_reversed_source_id,
7912 		   DECODE(a1.ae_cust_trx_line_gl_dist_id,0 ,''
7913 							,-1,'',a1.ae_customer_trx_line_id)
7914 							ae_customer_trx_line_id,
7915 		   DECODE(a1.ae_cust_trx_line_gl_dist_id,0 ,''
7916 							,-1,'',a1.ae_cust_trx_line_gl_dist_id)
7917 							ae_cust_trx_line_gl_dist_id,
7918 		   a1.ae_ref_line_id,
7919 		   a1.ref_account_class,
7920 		   a1.activity_bucket,
7921 		   a1.ref_dist_ccid,
7922 		   a1.ref_mf_dist_flag
7923 	    FROM ar_ae_alloc_rec_gt a1
7924 	    WHERE a1.ae_id = g_id
7925 	    AND a1.ae_summarize_flag = 'N'
7926 	    AND a1.ae_account_class IS NULL
7927 	    AND (NVL(a1.ae_entered_dr,0) <> 0 OR NVL(a1.ae_entered_cr,0) <> 0
7928               OR NVL(a1.ae_accounted_dr,0) <> 0 OR NVL(a1.ae_accounted_cr,0) <> 0
7929               OR (a1.ae_line_type IN ('TAX','DEFERRED_TAX','EDISC_NON_REC_TAX',
7930                      'UNEDISC_NON_REC_TAX','ADJ_NON_REC_TAX','FINCHRG_NON_REC_TAX')
7931                 AND (NVL(a1.ae_entered_dr,0) = 0 AND NVL(a1.ae_entered_cr,0) = 0 AND
7932                      NVL(a1.ae_accounted_dr,0) = 0 AND NVL(a1.ae_accounted_cr,0) = 0)))
7933 	  ) al;
7934      END IF;
7935    --If the simulation flag is set to Y then continue with the existing logic
7936    ELSE
7937    OPEN summarize_lines;
7938 
7939    LOOP
7940      /*----------------------------------------------------------------------------+
7941       | Initialise summarize record                                                |
7942       +----------------------------------------------------------------------------*/
7943        l_ae_line_rec := l_ae_empty_rec ;
7944 
7945        FETCH summarize_lines
7946        INTO l_ent_amt       ,
7947             l_ent_acctd_amt ,
7948             l_txb_amt       ,
7949             l_txb_acctd_amt ,
7950             l_from_ent_amt      ,
7951             l_from_ent_acctd_amt,
7952             l_ae_line_rec.ae_line_type,
7953             l_ae_line_rec.ae_line_type_secondary,
7954             l_ae_line_rec.ae_source_id,
7955             l_ae_line_rec.ae_source_table,
7956             l_ae_line_rec.ae_account,
7957             l_ae_line_rec.ae_source_id_secondary,
7958             l_ae_line_rec.ae_source_table_secondary,
7959             l_ae_line_rec.ae_currency_code,
7960             l_ae_line_rec.ae_currency_conversion_rate,
7961             l_ae_line_rec.ae_currency_conversion_type,
7962             l_ae_line_rec.ae_currency_conversion_date,
7963             l_ae_line_rec.ae_third_party_id,
7964             l_ae_line_rec.ae_third_party_sub_id,
7965             l_ae_line_rec.ae_tax_group_code_id,
7966             l_ae_line_rec.ae_tax_code_id,
7967             l_ae_line_rec.ae_location_segment_id,
7968             l_ae_line_rec.ae_tax_link_id,
7969             l_ae_line_rec.ae_neg_ind,
7970             l_ae_line_rec.ae_reversed_source_id,
7971             l_ae_line_rec.ae_customer_trx_line_id,
7972             l_ae_line_rec.ae_cust_trx_line_gl_dist_id,
7973             l_ae_line_rec.ae_ref_line_id,
7974             l_ae_line_rec.ref_account_class,
7975             l_ae_line_rec.activity_bucket,
7976 			l_ae_line_rec.ref_dist_ccid,
7977             l_ae_line_rec.ref_mf_dist_flag;
7978 
7979        --Set cursor not found flag
7980          IF summarize_lines%NOTFOUND THEN
7981             EXIT;
7982          END IF;
7983 
7984        /*------------------------------------------------------------------------------+
7985         | Build final debits and credits for entered amount. It is important to route  |
7986         | through the Create Debits and Credits function, rather than directly insert  |
7987         | into the ar_ae_alloc_rec_gt.                                                   |
7988         +------------------------------------------------------------------------------*/
7989          Create_Debits_Credits(l_ent_amt, l_ent_acctd_amt,
7990                                l_txb_amt, l_txb_acctd_amt,
7991                                l_from_ent_amt, l_from_ent_acctd_amt,
7992                                l_ae_line_rec,
7993                                NULL, 'SUMMARIZE');
7994 
7995          --BUG#3509185 l_summ_ctr := l_summ_ctr + 1;
7996          l_ae_line_rec.ae_summarize_flag := 'Y';
7997 
7998          Dump_Line_Amts(l_ae_line_rec);
7999 
8000          --BUG#3509185  Insert_Ae_Lines(p_ae_line_tbl => l_ae_line_rec);
8001          --{BUG#3509185
8002            Cache_Ae_Lines(p_ae_line_tbl => l_ae_line_rec);
8003          --}
8004 
8005 
8006    END LOOP; --End loop summarize lines
8007 
8008    CLOSE summarize_lines;
8009    END IF;
8010 
8011    --g_ae_summ_ctr := l_summ_ctr;
8012 
8013    arp_standard.debug( 'ARP_ALLOCATION_PKG.Summarize_Accounting_Lines()-');
8014 
8015 EXCEPTION
8016 WHEN OTHERS THEN
8017      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Summarize_Accounting_Lines:'||SQLERRM);
8018      RAISE;
8019 
8020 END Summarize_Accounting_Lines;
8021 
8022 /* ==========================================================================
8023  | PROCEDURE Get_Tax_Count
8024  |
8025  | DESCRIPTION
8026  |    Gets tax code, location count for a invoice line.
8027  |
8028  | SCOPE - PRIVATE
8029  |
8030  | PARAMETERS
8031  |    NONE
8032  *==========================================================================*/
8033 FUNCTION Get_Tax_Count(p_invoice_line_id IN NUMBER) RETURN NUMBER IS
8034 
8035 l_count  NUMBER := 0;
8036 
8037 BEGIN
8038 
8039    select sum(a1.tax_count)
8040    into l_count
8041    from (select 1                                  tax_count
8042          from ra_customer_trx_lines a2
8043          where a2.line_type = 'TAX'
8044          and   a2.link_to_cust_trx_line_id = p_invoice_line_id
8045          group by nvl(a2.location_segment_id, a2.vat_tax_id),
8046                   decode(a2.location_segment_id,
8047                          '', 'VAT',
8048                          'LOC')
8049         ) a1;
8050 
8051    RETURN(l_count);
8052 
8053 EXCEPTION
8054    WHEN NO_DATA_FOUND THEN
8055         RAISE;
8056    WHEN OTHERS THEN
8057         RAISE;
8058 
8059 END Get_Tax_Count;
8060 
8061 /* ==========================================================================
8062  | PROCEDURE Retain_Neg_Ind
8063  |
8064  | DESCRIPTION
8065  |    Ascertains as to whether there is another non zero line into which the
8066  |    0 amount line (amount, accounted amount, taxable amount, taxable
8067  |    accounted amount) can get merged
8068  |
8069  | SCOPE - PRIVATE
8070  |
8071  | PARAMETERS
8072  |    NONE
8073  *==========================================================================*/
8074 FUNCTION Retain_Neg_Ind(p_rowid IN ROWID) RETURN NUMBER IS
8075 
8076 l_amount NUMBER := 0;
8077 
8078 BEGIN
8079 
8080      SELECT /*+ INDEX(a1 AR_AE_ALLOC_REC_GT_N3) */
8081             ABS(SUM(NVL(a1.ae_entered_dr,0)           * -1 + NVL(a1.ae_entered_cr,0))) +
8082             ABS(SUM(NVL(a1.ae_accounted_dr,0)         * -1 + NVL(a1.ae_accounted_cr,0))) +
8083             ABS(SUM(NVL(a1.ae_taxable_entered_dr,0)   * -1 + NVL(a1.ae_taxable_entered_cr,0))) +
8084             ABS(SUM(NVL(a1.ae_taxable_accounted_dr,0) * -1 + NVL(a1.ae_taxable_accounted_cr,0)))
8085      INTO l_amount
8086      FROM ar_ae_alloc_rec_gt a1,
8087           ar_ae_alloc_rec_gt a2
8088      WHERE a1.ae_id = g_id
8089      AND a1.ae_summarize_flag = 'N'
8090      AND a1.ae_account_class IS NULL
8091      AND a2.rowid = p_rowid
8092      AND a1.rowid <> a2.rowid
8093      AND nvl(a1.ae_line_type, '-99') = nvl(a2.ae_line_type, '-99')
8094      AND nvl(a1.ae_line_type_secondary, '-99') = nvl(a2.ae_line_type_secondary, '-99')
8095      AND nvl(a1.ae_source_id,-9999) = nvl(a2.ae_source_id,-9999)
8096      AND nvl(a1.ae_source_table,'-99') = nvl(a2.ae_source_table,'-99')
8097      AND nvl(a1.ae_account,-9999) = nvl(a2.ae_account,-9999)
8098      AND nvl(a1.ae_source_id_secondary,-9999) = nvl(a2.ae_source_id_secondary,-9999)
8099      AND nvl(a1.ae_source_table_secondary,'-99') = nvl(a2.ae_source_table_secondary,'-99')
8100      AND nvl(a1.ae_currency_code, '-99') = nvl(a2.ae_currency_code, '-99')
8101      AND nvl(a1.ae_currency_conversion_rate, -9999) = nvl(a2.ae_currency_conversion_rate, -9999)
8102      AND nvl(a1.ae_currency_conversion_type,'-99') = nvl(a2.ae_currency_conversion_type,'-99')
8103      AND nvl(a1.ae_currency_conversion_date,to_date('01-01-1949','DD-MM-YYYY'))
8104               = nvl(a2.ae_currency_conversion_date,to_date('01-01-1949','DD-MM-YYYY'))
8105      AND nvl(a1.ae_third_party_id, -9999) = nvl(a2.ae_third_party_id, -9999)
8106      AND nvl(a1.ae_third_party_sub_id, -9999) = nvl(a2.ae_third_party_sub_id, -9999)
8107      AND nvl(a1.ae_tax_group_code_id, -9999) = nvl(a2.ae_tax_group_code_id, -9999)
8108      AND nvl(a1.ae_tax_code_id, -9999) = nvl(a2.ae_tax_code_id, -9999)
8109      AND nvl(a1.ae_location_segment_id, -9999) = nvl(a2.ae_location_segment_id, -9999)
8110      AND nvl(a1.ae_tax_link_id, -9999) = nvl(a2.ae_tax_link_id, -9999)
8111      AND nvl(a1.ae_reversed_source_id,-9999) = nvl(a2.ae_reversed_source_id,-9999);
8112 
8113      IF l_amount <> 0 THEN
8114         RETURN(0);
8115      ELSE
8116         RETURN(1);
8117      END IF;
8118 
8119 EXCEPTION
8120 WHEN NO_DATA_FOUND THEN
8121      RETURN(1);
8122 WHEN OTHERS THEN
8123      RAISE;
8124 
8125 END Retain_Neg_Ind;
8126 
8127 /* ==========================================================================
8128  | PROCEDURE Assign_Elements
8129  |
8130  | DESCRIPTION
8131  |    Assign revenue or tax lines built to global table which will eventually
8132  |    be summarized
8133  |
8134  | SCOPE - PRIVATE
8135  |
8136  | PARAMETERS
8137  |    NONE
8138  | Hidtory
8139  |   21-NOV-2003   Herve Yu  from_amount_dr      , from_amount_cr,
8140  |                           from_acctd_amount_dr, from_acctd_amount_cr
8141  *==========================================================================*/
8142 PROCEDURE Assign_Elements(p_ae_line_rec           IN ar_ae_alloc_rec_gt%ROWTYPE) IS
8143 
8144 BEGIN
8145    IF PG_DEBUG IN ('Y','C') THEN
8146     arp_standard.debug( 'ARP_ALLOCATION_PKG.Assign_Elements()+');
8147    END IF;
8148    /*------------------------------------------------------+
8149     | Store AE Line elements in AE Lines temp table        |
8150     +------------------------------------------------------*/
8151     g_ae_ctr := g_ae_ctr +1;
8152 
8153     insert into ar_ae_alloc_rec_gt
8154     ( ae_id,
8155       ae_account_class            ,
8156       ae_customer_trx_id          ,
8157       ae_customer_trx_line_id     ,
8158       ae_link_to_cust_trx_line_id ,
8159       ae_tax_type                 ,
8160       ae_code_combination_id      ,
8161       ae_collected_tax_ccid       ,
8162       ae_line_amount              ,
8163       ae_amount                   ,
8164       ae_acctd_amount             ,
8165       ae_taxable_amount           ,
8166       ae_taxable_acctd_amount     ,
8167       ae_adj_ccid                 ,
8168       ae_edisc_ccid               ,
8169       ae_unedisc_ccid             ,
8170       ae_finchrg_ccid             ,
8171       ae_adj_non_rec_tax_ccid     ,
8172       ae_edisc_non_rec_tax_ccid   ,
8173       ae_unedisc_non_rec_tax_ccid ,
8174       ae_finchrg_non_rec_tax_ccid ,
8175       ae_override_ccid1           ,
8176       ae_override_ccid2           ,
8177       ae_tax_link_id              ,
8178       ae_tax_link_id_ed_adj       ,
8179       ae_tax_link_id_uned         ,
8180       ae_tax_link_id_act          ,
8181       ae_pro_amt                  ,
8182       ae_pro_acctd_amt            ,
8183       ae_pro_chrg_amt             ,
8184       ae_pro_chrg_acctd_amt       ,
8185       ae_pro_taxable_amt          ,
8186       ae_pro_taxable_acctd_amt    ,
8187       ae_counted_flag             ,
8188       ae_autotax                  ,
8189       ae_sum_alloc_amt            ,
8190       ae_sum_alloc_acctd_amt      ,
8191       ae_tax_line_count           ,
8192       ae_line_type                   ,
8193       ae_line_type_secondary         ,
8194       ae_source_id                   ,
8195       ae_source_table                ,
8196       ae_account                     ,
8197       ae_entered_dr                  ,
8198       ae_entered_cr                  ,
8199       ae_accounted_dr                ,
8200       ae_accounted_cr                ,
8201       ae_source_id_secondary         ,
8202       ae_source_table_secondary      ,
8203       ae_currency_code               ,
8204       ae_currency_conversion_rate    ,
8205       ae_currency_conversion_type    ,
8206       ae_currency_conversion_date    ,
8207       ae_third_party_id              ,
8208       ae_third_party_sub_id          ,
8209       ae_tax_group_code_id           ,
8210       ae_tax_code_id                 ,
8211       ae_location_segment_id         ,
8212       ae_taxable_entered_dr          ,
8213       ae_taxable_entered_cr          ,
8214       ae_taxable_accounted_dr        ,
8215       ae_taxable_accounted_cr        ,
8216       ae_reversed_source_id          ,
8217       ae_neg_ind                     ,
8218       ae_summarize_flag              ,
8219       ae_cust_trx_line_gl_dist_id    ,
8220       ae_ref_line_id                 ,
8221       ae_from_amount_dr              ,
8222       ae_from_amount_cr              ,
8223       ae_from_acctd_amount_dr        ,
8224       ae_from_acctd_amount_cr        ,
8225       ref_account_class,
8226       activity_bucket,
8227       ref_dist_ccid,
8228       ref_mf_dist_flag
8229       )
8230 VALUES
8231     ( g_id,
8232       p_ae_line_rec.ae_account_class            ,
8233       p_ae_line_rec.ae_customer_trx_id          ,
8234       p_ae_line_rec.ae_customer_trx_line_id     ,
8235       p_ae_line_rec.ae_link_to_cust_trx_line_id ,
8236       p_ae_line_rec.ae_tax_type                 ,
8237       p_ae_line_rec.ae_code_combination_id      ,
8238       p_ae_line_rec.ae_collected_tax_ccid       ,
8239       p_ae_line_rec.ae_line_amount              ,
8240       p_ae_line_rec.ae_amount                   ,
8241       p_ae_line_rec.ae_acctd_amount             ,
8242       p_ae_line_rec.ae_taxable_amount           ,
8243       p_ae_line_rec.ae_taxable_acctd_amount     ,
8244       p_ae_line_rec.ae_adj_ccid                 ,
8245       p_ae_line_rec.ae_edisc_ccid               ,
8246       p_ae_line_rec.ae_unedisc_ccid             ,
8247       p_ae_line_rec.ae_finchrg_ccid             ,
8248       p_ae_line_rec.ae_adj_non_rec_tax_ccid     ,
8249       p_ae_line_rec.ae_edisc_non_rec_tax_ccid   ,
8250       p_ae_line_rec.ae_unedisc_non_rec_tax_ccid ,
8251       p_ae_line_rec.ae_finchrg_non_rec_tax_ccid ,
8252       p_ae_line_rec.ae_override_ccid1           ,
8253       p_ae_line_rec.ae_override_ccid2           ,
8254       p_ae_line_rec.ae_tax_link_id              ,
8255       p_ae_line_rec.ae_tax_link_id_ed_adj       ,
8256       p_ae_line_rec.ae_tax_link_id_uned         ,
8257       p_ae_line_rec.ae_tax_link_id_act          ,
8258       p_ae_line_rec.ae_pro_amt                  ,
8259       p_ae_line_rec.ae_pro_acctd_amt            ,
8260       p_ae_line_rec.ae_pro_chrg_amt             ,
8261       p_ae_line_rec.ae_pro_chrg_acctd_amt       ,
8262       p_ae_line_rec.ae_pro_taxable_amt          ,
8263       p_ae_line_rec.ae_pro_taxable_acctd_amt    ,
8264       p_ae_line_rec.ae_counted_flag             ,
8265       p_ae_line_rec.ae_autotax                  ,
8266       p_ae_line_rec.ae_sum_alloc_amt            ,
8267       p_ae_line_rec.ae_sum_alloc_acctd_amt      ,
8268       p_ae_line_rec.ae_tax_line_count           ,
8269       p_ae_line_rec.ae_line_type                   ,
8270       p_ae_line_rec.ae_line_type_secondary         ,
8271       p_ae_line_rec.ae_source_id                   ,
8272       p_ae_line_rec.ae_source_table                ,
8273       p_ae_line_rec.ae_account                     ,
8274       p_ae_line_rec.ae_entered_dr                  ,
8275       p_ae_line_rec.ae_entered_cr                  ,
8276       p_ae_line_rec.ae_accounted_dr                ,
8277       p_ae_line_rec.ae_accounted_cr                ,
8278       p_ae_line_rec.ae_source_id_secondary         ,
8279       p_ae_line_rec.ae_source_table_secondary      ,
8280       p_ae_line_rec.ae_currency_code               ,
8281       p_ae_line_rec.ae_currency_conversion_rate    ,
8282       p_ae_line_rec.ae_currency_conversion_type    ,
8283       p_ae_line_rec.ae_currency_conversion_date    ,
8284       p_ae_line_rec.ae_third_party_id              ,
8285       p_ae_line_rec.ae_third_party_sub_id          ,
8286       p_ae_line_rec.ae_tax_group_code_id           ,
8287       p_ae_line_rec.ae_tax_code_id                 ,
8288       p_ae_line_rec.ae_location_segment_id         ,
8289       p_ae_line_rec.ae_taxable_entered_dr          ,
8290       p_ae_line_rec.ae_taxable_entered_cr          ,
8291       p_ae_line_rec.ae_taxable_accounted_dr        ,
8292       p_ae_line_rec.ae_taxable_accounted_cr        ,
8293       p_ae_line_rec.ae_reversed_source_id          ,
8294       p_ae_line_rec.ae_neg_ind                     ,
8295       NVL(p_ae_line_rec.ae_summarize_flag,'N')     ,
8296       p_ae_line_rec.ae_cust_trx_line_gl_dist_id    ,
8297       p_ae_line_rec.ae_ref_line_id                 ,
8298       p_ae_line_rec.ae_from_amount_dr              ,
8299       p_ae_line_rec.ae_from_amount_cr              ,
8300       p_ae_line_rec.ae_from_acctd_amount_dr        ,
8301       p_ae_line_rec.ae_from_acctd_amount_cr        ,
8302       p_ae_line_rec.ref_account_class,
8303       p_ae_line_rec.activity_bucket,
8304       p_ae_line_rec.ref_dist_ccid,
8305       p_ae_line_rec.ref_mf_dist_flag
8306       );
8307 
8308     Dump_Line_Amts(p_ae_line_rec);
8309 
8310    IF PG_DEBUG IN ('Y','C') THEN
8311     arp_standard.debug( 'ARP_ALLOCATION_PKG.Assign_Elements()-');
8312    END IF;
8313 EXCEPTION
8314   WHEN OTHERS THEN
8315      arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Assign_Elements');
8316      RAISE;
8317 
8318 END Assign_Elements;
8319 
8320 /*========================================================================
8321  | PRIVATE PROCEDURE Insert_Ae_Lines
8322  |
8323  | DESCRIPTION
8324  |      Inserts into AR_DISTRIBUTIONS accounting lines
8325  |      ----------------------------------------------
8326  |      Calls the table handler for AR_DISTRIBUTIONS to insert accounting
8327  |      for a given document into the underlying table.
8328  |
8329  | PARAMETERS
8330  |      p_ae_line_tbl   IN      Accounting lines table
8331  | History
8332  |   24-NOV-2003  Herve Yu     Distributions in the receipt currency
8333  *=======================================================================*/
8334 PROCEDURE Insert_Ae_Lines(p_ae_line_tbl IN ar_ae_alloc_rec_gt%ROWTYPE) IS
8335 
8336   l_ae_line_rec         ar_distributions%ROWTYPE;
8337   l_ae_line_rec_empty   ar_distributions%ROWTYPE;
8338   l_dummy               ar_distributions.line_id%TYPE;
8339 
8340 BEGIN
8341 
8342   arp_standard.debug( 'ARP_ALLOCATION_PKG.Insert_Ae_Lines()+');
8343 
8344 -- Initialize
8345    l_ae_line_rec := l_ae_line_rec_empty;
8346 
8347 -- Assign AE Line elements
8348    l_ae_line_rec.source_type              :=  p_ae_line_tbl.ae_line_type;
8349    l_ae_line_rec.source_type_secondary    :=  p_ae_line_tbl.ae_line_type_secondary;
8350    l_ae_line_rec.source_id                :=  p_ae_line_tbl.ae_source_id;
8351    l_ae_line_rec.source_table             :=  p_ae_line_tbl.ae_source_table;
8352    l_ae_line_rec.code_combination_id      :=  p_ae_line_tbl.ae_account;
8353    l_ae_line_rec.amount_dr                :=  p_ae_line_tbl.ae_entered_dr;
8354    l_ae_line_rec.amount_cr                :=  p_ae_line_tbl.ae_entered_cr;
8355    l_ae_line_rec.acctd_amount_dr          :=  p_ae_line_tbl.ae_accounted_dr;
8356    l_ae_line_rec.acctd_amount_cr          :=  p_ae_line_tbl.ae_accounted_cr;
8357    l_ae_line_rec.from_amount_dr           :=  p_ae_line_tbl.ae_from_amount_dr;
8358    l_ae_line_rec.from_amount_cr           :=  p_ae_line_tbl.ae_from_amount_cr;
8359    l_ae_line_rec.from_acctd_amount_dr     :=  p_ae_line_tbl.ae_from_acctd_amount_dr;
8360    l_ae_line_rec.from_acctd_amount_cr     :=  p_ae_line_tbl.ae_from_acctd_amount_cr;
8361    l_ae_line_rec.source_id_secondary      :=  p_ae_line_tbl.ae_source_id_secondary;
8362    l_ae_line_rec.source_table_secondary   :=  p_ae_line_tbl.ae_source_table_secondary;
8363    l_ae_line_rec.currency_code            :=  p_ae_line_tbl.ae_currency_code;
8364    l_ae_line_rec.currency_conversion_rate :=  p_ae_line_tbl.ae_currency_conversion_rate;
8365    l_ae_line_rec.currency_conversion_type :=  p_ae_line_tbl.ae_currency_conversion_type;
8366    l_ae_line_rec.currency_conversion_date :=  p_ae_line_tbl.ae_currency_conversion_date;
8367    l_ae_line_rec.third_party_id           :=  p_ae_line_tbl.ae_third_party_id;
8368    l_ae_line_rec.third_party_sub_id       :=  p_ae_line_tbl.ae_third_party_sub_id;
8369    l_ae_line_rec.tax_group_code_id        :=  p_ae_line_tbl.ae_tax_group_code_id;
8370    l_ae_line_rec.tax_code_id              :=  p_ae_line_tbl.ae_tax_code_id;
8371    l_ae_line_rec.location_segment_id      :=  p_ae_line_tbl.ae_location_segment_id;
8372    l_ae_line_rec.taxable_entered_dr       :=  p_ae_line_tbl.ae_taxable_entered_dr;
8373    l_ae_line_rec.taxable_entered_cr       :=  p_ae_line_tbl.ae_taxable_entered_cr;
8374    l_ae_line_rec.taxable_accounted_dr     :=  p_ae_line_tbl.ae_taxable_accounted_dr;
8375    l_ae_line_rec.taxable_accounted_cr     :=  p_ae_line_tbl.ae_taxable_accounted_cr;
8376    l_ae_line_rec.tax_link_id              :=  p_ae_line_tbl.ae_tax_link_id;
8377    l_ae_line_rec.reversed_source_id       :=  p_ae_line_tbl.ae_reversed_source_id;
8378    l_ae_line_rec.ref_account_class                :=  p_ae_line_tbl.ref_account_class;
8379    l_ae_line_rec.activity_bucket                   :=  p_ae_line_tbl.activity_bucket;
8380    l_ae_line_rec.ref_customer_trx_line_id :=  p_ae_line_tbl.ae_customer_trx_line_id;
8381    l_ae_line_rec.ref_cust_trx_line_gl_dist_id :=  p_ae_line_tbl.ae_cust_trx_line_gl_dist_id;
8382    l_ae_line_rec.ref_dist_ccid    :=  p_ae_line_tbl.ref_dist_ccid;
8383    l_ae_line_rec.ref_mf_dist_flag :=  p_ae_line_tbl.ref_mf_dist_flag;
8384 
8385    l_ae_line_rec.ref_line_id              :=  p_ae_line_tbl.ae_ref_line_id;
8386 
8387    Dump_Dist_Amts(l_ae_line_rec);
8388 
8389 --Insert into ar_distributions
8390    IF (NVL(g_ae_sys_rec.sob_type,'P') = 'P') THEN
8391 
8392        arp_distributions_pkg.insert_p(l_ae_line_rec, l_dummy);
8393 --{BUG#4301323
8394 --   ELSE
8395         /* need to insert records into the MRC table.  Calling new
8396            mrc engine */
8397 
8398                -- before we call the ar_mrc_engine, we need the line_id of
8399         -- the primary row.   If the Source type is EXCH_GAIN, EXCH_LOSS
8400         -- or CURR_ROUND, use a new line_id from the sequence.
8401 --  arp_standard.debug('source type = ' || l_ae_line_rec.source_type);
8402 --        if (l_ae_line_rec.source_type = 'EXCH_GAIN' or
8403 --           l_ae_line_rec.source_type = 'EXCH_LOSS' or
8404 --           l_ae_line_rec.source_type = 'CURR_ROUND' )  THEN
8405 --           select  ar_distributions_s.nextval
8406 --             into l_ae_line_rec.line_id
8407 --           from dual;
8408 --        ELSE
8409 --         BEGIN
8410 --           select line_id
8411 --             into l_ae_line_rec.line_id
8412 --             from ar_distributions
8413 --            where source_id = l_ae_line_rec.source_id
8414 --              and source_table = l_ae_line_rec.source_table
8415 --              and source_type = l_ae_line_rec.source_type
8416 --              and (source_type_secondary =
8417 --                           l_ae_line_rec.source_type_secondary
8418 --                   OR source_type_secondary IS NULL)
8419 --              and ( amount_dr = l_ae_line_rec.amount_dr OR
8420 --                    amount_dr IS NULL)
8421 --              and ( amount_cr = l_ae_line_rec.amount_cr OR
8422 --                    amount_cr IS NULL)
8423 --              and code_combination_id = l_ae_line_rec.code_combination_id;
8424 --           EXCEPTION
8425 --            WHEN OTHERS THEN
8426 --             arp_standard.debug('Can not determine Line id so created new one');
8427 --             select  ar_distributions_s.nextval
8428 --               into l_ae_line_rec.line_id
8429 --               from dual;
8430 --           END;
8431 --           arp_standard.debug('line id = ' || to_char(l_ae_line_rec.line_id));
8432 --        END IF;
8433 --        arp_standard.debug('before calling mrc_acct_main');
8434 --        arp_mrc_acct_main.insert_mrc_dis_data
8435 --                     (l_ae_line_rec,
8436 --                      arp_acct_main.ae_sys_rec.set_of_books_id);
8437 --}
8438    END IF;
8439    arp_standard.debug( 'ARP_ACCT_MAIN.Insert_Ae_Lines()-');
8440 
8441 END Insert_Ae_Lines;
8442 
8443 
8444 /*========================================================================
8445  | PRIVATE PROCEDURE Cache_Ae_Lines
8446  |
8447  | DESCRIPTION
8448  |      Inserts into AR_DISTRIBUTIONS accounting lines
8449  |      ----------------------------------------------
8450  |      Calls the table handler for AR_DISTRIBUTIONS to insert accounting
8451  |      for a given document into the underlying table.
8452  |
8453  | PARAMETERS
8454  |      p_ae_line_tbl   IN      Accounting lines table
8455  |
8456  *=======================================================================*/
8457 PROCEDURE Cache_Ae_Lines(p_ae_line_tbl IN ar_ae_alloc_rec_gt%ROWTYPE) IS
8458 
8459 BEGIN
8460 
8461 
8462   IF PG_DEBUG in ('Y', 'C') THEN
8463      arp_standard.debug( 'ARP_ALLOCATION_PKG.Cache_Ae_Lines()+');
8464   END IF;
8465 
8466   g_ae_summ_ctr := g_ae_summ_ctr + 1;
8467 
8468   g_ae_summarize_tbl(g_ae_summ_ctr).ae_line_type               :=  p_ae_line_tbl.ae_line_type;
8469   g_ae_summarize_tbl(g_ae_summ_ctr).source_id                  :=  p_ae_line_tbl.ae_source_id;
8470   g_ae_summarize_tbl(g_ae_summ_ctr).source_table               :=  p_ae_line_tbl.ae_source_table;
8471   g_ae_summarize_tbl(g_ae_summ_ctr).account                    :=  p_ae_line_tbl.ae_account;
8472   g_ae_summarize_tbl(g_ae_summ_ctr).entered_dr                 :=  p_ae_line_tbl.ae_entered_dr;
8473   g_ae_summarize_tbl(g_ae_summ_ctr).entered_cr                 :=  p_ae_line_tbl.ae_entered_cr;
8474   g_ae_summarize_tbl(g_ae_summ_ctr).accounted_dr               :=  p_ae_line_tbl.ae_accounted_dr;
8475   g_ae_summarize_tbl(g_ae_summ_ctr).accounted_cr               :=  p_ae_line_tbl.ae_accounted_cr;
8476   g_ae_summarize_tbl(g_ae_summ_ctr).ae_line_type_secondary     :=  p_ae_line_tbl.ae_line_type_secondary;
8477   g_ae_summarize_tbl(g_ae_summ_ctr).source_id_secondary        :=  p_ae_line_tbl.ae_source_id_secondary;
8478   g_ae_summarize_tbl(g_ae_summ_ctr).source_table_secondary     :=  p_ae_line_tbl.ae_source_table_secondary;
8479   g_ae_summarize_tbl(g_ae_summ_ctr).currency_code              :=  p_ae_line_tbl.ae_currency_code;
8480   g_ae_summarize_tbl(g_ae_summ_ctr).currency_conversion_rate   :=  p_ae_line_tbl.ae_currency_conversion_rate;
8481   g_ae_summarize_tbl(g_ae_summ_ctr).currency_conversion_type   :=  p_ae_line_tbl.ae_currency_conversion_type;
8482   g_ae_summarize_tbl(g_ae_summ_ctr).currency_conversion_date   :=  p_ae_line_tbl.ae_currency_conversion_date;
8483   g_ae_summarize_tbl(g_ae_summ_ctr).third_party_id             :=  p_ae_line_tbl.ae_third_party_id;
8484   g_ae_summarize_tbl(g_ae_summ_ctr).third_party_sub_id         :=  p_ae_line_tbl.ae_third_party_sub_id;
8485   g_ae_summarize_tbl(g_ae_summ_ctr).tax_group_code_id          :=  p_ae_line_tbl.ae_tax_group_code_id;
8486   g_ae_summarize_tbl(g_ae_summ_ctr).tax_code_id                :=  p_ae_line_tbl.ae_tax_code_id;
8487   g_ae_summarize_tbl(g_ae_summ_ctr).location_segment_id        :=  p_ae_line_tbl.ae_location_segment_id;
8488   g_ae_summarize_tbl(g_ae_summ_ctr).taxable_entered_dr         :=  p_ae_line_tbl.ae_taxable_entered_dr;
8489   g_ae_summarize_tbl(g_ae_summ_ctr).taxable_entered_cr         :=  p_ae_line_tbl.ae_taxable_entered_cr;
8490   g_ae_summarize_tbl(g_ae_summ_ctr).taxable_accounted_dr       :=  p_ae_line_tbl.ae_taxable_accounted_dr;
8491   g_ae_summarize_tbl(g_ae_summ_ctr).taxable_accounted_cr       :=  p_ae_line_tbl.ae_taxable_accounted_cr;
8492   g_ae_summarize_tbl(g_ae_summ_ctr).tax_link_id                :=  p_ae_line_tbl.ae_tax_link_id;
8493   g_ae_summarize_tbl(g_ae_summ_ctr).reversed_source_id         :=  p_ae_line_tbl.ae_reversed_source_id;
8494   g_ae_summarize_tbl(g_ae_summ_ctr).ref_customer_trx_line_id   :=  p_ae_line_tbl.ae_customer_trx_line_id;
8495   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;
8496   g_ae_summarize_tbl(g_ae_summ_ctr).ref_line_id                :=  p_ae_line_tbl.ae_ref_line_id;
8497   g_ae_summarize_tbl(g_ae_summ_ctr).activity_bucket            :=  p_ae_line_tbl.activity_bucket;
8498   g_ae_summarize_tbl(g_ae_summ_ctr).ref_account_class          :=  p_ae_line_tbl.ref_account_class;
8499   g_ae_summarize_tbl(g_ae_summ_ctr).ref_dist_ccid              :=  p_ae_line_tbl.ref_dist_ccid;
8500   g_ae_summarize_tbl(g_ae_summ_ctr).ref_mf_dist_flag           :=  p_ae_line_tbl.ref_mf_dist_flag;
8501   g_ae_summarize_tbl(g_ae_summ_ctr).from_amount_dr             :=  p_ae_line_tbl.ae_from_amount_dr;
8502   g_ae_summarize_tbl(g_ae_summ_ctr).from_amount_cr             :=  p_ae_line_tbl.ae_from_amount_cr;
8503   g_ae_summarize_tbl(g_ae_summ_ctr).from_acctd_amount_dr       :=  p_ae_line_tbl.ae_from_acctd_amount_dr;
8504   g_ae_summarize_tbl(g_ae_summ_ctr).from_acctd_amount_cr       :=  p_ae_line_tbl.ae_from_acctd_amount_cr;
8505 
8506 
8507 
8508   IF PG_DEBUG in ('Y', 'C') THEN
8509      arp_standard.debug( 'ARP_ALLOCATION_PKG.Cache_Ae_Lines()-');
8510   END IF;
8511 
8512 EXCEPTION
8513 WHEN OTHERS THEN
8514   IF PG_DEBUG in ('Y', 'C') THEN
8515      arp_standard.debug( 'OTHERS EXCEPTION : ARP_ALLOCATION_PKG.Cache_Ae_Lines()');
8516   END IF;
8517 
8518 END Cache_Ae_Lines;
8519 
8520 /*========================================================================
8521  | PRIVATE FUNCTION  Set_Adj_CCID
8522  |
8523  | DESCRIPTION
8524  |      Sets the global Variable adj_code_combination_id to NULL
8525  |      Gets the global Variable adj_code_combination_id
8526  |      Used by library ARXTWADJ.pld
8527  |
8528  | PARAMETERS
8529  |      p_action   IN VARCHAR2
8530  *=======================================================================*/
8531 FUNCTION Set_Adj_CCID(p_action IN VARCHAR2) RETURN NUMBER IS
8532 
8533 BEGIN
8534    IF PG_DEBUG in ('Y', 'C') THEN
8535       arp_standard.debug( 'ARP_ACCT_MAIN.Set_Adj_CCID()+');
8536    END IF;
8537 
8538    IF p_action = 'S' THEN --Set
8539       adj_code_combination_id := '';
8540    END IF;
8541 
8542    IF PG_DEBUG in ('Y', 'C') THEN
8543       arp_standard.debug('Set_Adj_CCID: ' ||  'adj_code_combination_id ' || adj_code_combination_id);
8544    END IF;
8545 
8546    RETURN(adj_code_combination_id);
8547 
8548    IF PG_DEBUG in ('Y', 'C') THEN
8549       arp_standard.debug( 'ARP_ACCT_MAIN.Set_Adj_CCID()-');
8550    END IF;
8551 
8552 EXCEPTION
8553 WHEN OTHERS THEN
8554     IF PG_DEBUG in ('Y', 'C') THEN
8555        arp_standard.debug( 'Exception : ARP_ACCT_MAIN.Set_Adj_CCID');
8556     END IF;
8557     RAISE;
8558 
8559 END Set_Adj_CCID;
8560 
8561 
8562 /* ==========================================================================
8563  | PROCEDURE Dump_Alloc_Rev_Tax
8564  |
8565  | DESCRIPTION
8566  |    Dumps data stored in the Allocation Revenue or Tax table
8567  |
8568  | SCOPE - PRIVATE
8569  |
8570  | PARAMETERS
8571  |    p_type                 IN      Indicates Rev or Tax
8572  |    p_alloc_rec            IN      Allocation details Record
8573  *==========================================================================*/
8574 PROCEDURE Dump_Alloc_Rev_Tax(p_type IN VARCHAR2, p_alloc_rec IN ar_ae_alloc_rec_gt%ROWTYPE) IS
8575 
8576 BEGIN
8577    IF PG_DEBUG in ('Y', 'C') THEN
8578       arp_standard.debug( 'ARP_ALLOCATION_PKG.Dump_Alloc_Rev_Tax()+');
8579       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_id = ' ||
8580                                      p_alloc_rec.ae_id);
8581       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_account_class = ' ||
8582                                      p_alloc_rec.ae_account_class);
8583       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_customer_trx_id = ' ||
8584                                      p_alloc_rec.ae_customer_trx_id);
8585       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_customer_trx_line_id = ' ||
8586                                      p_alloc_rec.ae_customer_trx_line_id);
8587       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_link_to_cust_trx_line_id= ' ||
8588                                      p_alloc_rec.ae_link_to_cust_trx_line_id);
8589       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_type= ' ||
8590                                      p_alloc_rec.ae_tax_type);
8591       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_code_combination_id= ' ||
8592                                      p_alloc_rec.ae_code_combination_id);
8593       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_collected_tax_ccid= ' ||
8594                                      p_alloc_rec.ae_collected_tax_ccid);
8595       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_line_amount= ' ||
8596                                      p_alloc_rec.ae_line_amount);
8597       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_amount= ' ||
8598                                      p_alloc_rec.ae_amount);
8599       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_acctd_amount= ' ||
8600                                      p_alloc_rec.ae_acctd_amount);
8601       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_group_code_id= ' ||
8602                                      p_alloc_rec.ae_tax_group_code_id);
8603       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_id= ' ||
8604                                      p_alloc_rec.ae_tax_id);
8605       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_taxable_amount= ' ||
8606                                      p_alloc_rec.ae_taxable_amount);
8607       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_taxable_acctd_amount= ' ||
8608                                      p_alloc_rec.ae_taxable_acctd_amount);
8609       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_adj_ccid= ' ||
8610                                      p_alloc_rec.ae_adj_ccid);
8611       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_edisc_ccid= ' ||
8612                                      p_alloc_rec.ae_edisc_ccid);
8613       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_unedisc_ccid= ' ||
8614                                      p_alloc_rec.ae_unedisc_ccid);
8615       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_finchrg_ccid= ' ||
8616                                      p_alloc_rec.ae_finchrg_ccid);
8617       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_adj_non_rec_tax_ccid= ' ||
8618                                      p_alloc_rec.ae_adj_non_rec_tax_ccid);
8619       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_edisc_non_rec_tax_ccid= ' ||
8620                                      p_alloc_rec.ae_edisc_non_rec_tax_ccid);
8621       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_unedisc_non_rec_tax_ccid= ' ||
8622                                      p_alloc_rec.ae_unedisc_non_rec_tax_ccid);
8623       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_finchrg_non_rec_tax_ccid= ' ||
8624                                      p_alloc_rec.ae_finchrg_non_rec_tax_ccid);
8625       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_override_ccid1= ' ||
8626                                      p_alloc_rec.ae_override_ccid1);
8627       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_override_ccid2= ' ||
8628                                      p_alloc_rec.ae_override_ccid2);
8629       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_link_id= ' ||
8630                                      p_alloc_rec.ae_tax_link_id);
8631       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_link_id_ed_adj= ' ||
8632                                      p_alloc_rec.ae_tax_link_id_ed_adj);
8633       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_link_id_uned= ' ||
8634                                      p_alloc_rec.ae_tax_link_id_uned);
8635       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_tax_link_id_act= ' ||
8636                                      p_alloc_rec.ae_tax_link_id_act);
8637       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_amt= ' ||
8638                                      p_alloc_rec.ae_pro_amt);
8639       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_acctd_amt= ' ||
8640                                      p_alloc_rec.ae_pro_acctd_amt);
8641       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_taxable_amt= ' ||
8642                                      p_alloc_rec.ae_pro_taxable_amt);
8643       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_taxable_acctd_amt= ' ||
8644                                      p_alloc_rec.ae_pro_taxable_acctd_amt);
8645       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_def_tax_amt= ' ||
8646                                      p_alloc_rec.ae_pro_def_tax_amt);
8647       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_pro_def_tax_acctd_amt= ' ||
8648                                      p_alloc_rec.ae_pro_def_tax_acctd_amt);
8649       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_summarize_flag= ' ||
8650                                      p_alloc_rec.ae_summarize_flag);
8651       arp_standard.debug('Dump_Alloc_Rev_Tax: ' || 'g_ae_alloc_'||p_type||'_tbl.ae_counted_flag  = ' ||
8652                                      p_alloc_rec.ae_counted_flag);
8653       arp_standard.debug( 'ARP_ALLOCATION_PKG.Dump_Alloc_Rev_Tax()-');
8654    END IF;
8655 EXCEPTION
8656   WHEN OTHERS THEN
8657      IF PG_DEBUG in ('Y', 'C') THEN
8658         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Dump_Alloc_Rev_Tax');
8659      END IF;
8660      RAISE;
8661 
8662 END Dump_Alloc_Rev_Tax;
8663 
8664 /* ==========================================================================
8665  | PROCEDURE Dump_Init_Amts
8666  |
8667  | DESCRIPTION
8668  |    Dumps data derived by the Init_Amts routine for Revenue and Tax
8669  |
8670  | SCOPE - PRIVATE
8671  |
8672  | PARAMETERS
8673  |    p_type_acct            IN      Type of accounting
8674  |    p_app_rec              IN      Receivable application record
8675  |    p_adj_rec              IN      Adjustment Record
8676  *==========================================================================*/
8677 PROCEDURE Dump_Init_Amts(p_type_acct    IN VARCHAR2                          ,
8678                          p_app_rec      IN ar_receivable_applications%ROWTYPE,
8679                          p_adj_rec      IN ar_adjustments%ROWTYPE             ) IS
8680 BEGIN
8681    IF PG_DEBUG in ('Y', 'C') THEN
8682       arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Init_Amts()+');
8683       arp_standard.debug('Dump_Init_Amts: ' || 'p_type_acct = ' || p_type_acct);
8684    END IF;
8685 
8686    IF p_type_acct = 'ED_ADJ' AND g_ae_doc_rec.source_table = 'RA' THEN
8687 
8688       IF PG_DEBUG in ('Y', 'C') THEN
8689          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.earned_discount_taken       = ' || p_app_rec.earned_discount_taken);
8690          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.acctd_earned_discount_taken = ' || p_app_rec.acctd_earned_discount_taken);
8691          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.line_ediscounted            = ' || p_app_rec.line_ediscounted);
8692          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.tax_ediscounted             = ' || p_app_rec.tax_ediscounted);
8693          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.freight_ediscounted         = ' || p_app_rec.freight_ediscounted);
8694          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.charges_ediscounted         = ' || p_app_rec.charges_ediscounted);
8695       END IF;
8696 
8697    ELSIF p_type_acct = 'PAY' AND g_ae_doc_rec.source_table = 'RA' THEN
8698 
8699       IF PG_DEBUG in ('Y', 'C') THEN
8700          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.amount_applied              = ' || p_app_rec.amount_applied);
8701          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.acctd_amount_applied_to     = ' || p_app_rec.acctd_amount_applied_to);
8702          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.line_applied                = ' || p_app_rec.line_applied);
8703          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.tax_applied                 = ' || p_app_rec.tax_applied);
8704          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.freight_applied             = ' || p_app_rec.freight_applied);
8705          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.receivables_charges_applied = ' || p_app_rec.receivables_charges_applied);
8706       END IF;
8707 
8708    ELSIF p_type_acct = 'ED_ADJ' AND g_ae_doc_rec.source_table = 'ADJ' THEN
8709 
8710          IF PG_DEBUG in ('Y', 'C') THEN
8711             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.amount                   = ' || p_adj_rec.amount);
8712             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.acctd_amount             = ' || p_adj_rec.acctd_amount);
8713             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.line_adjusted            = ' || p_adj_rec.line_adjusted);
8714             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.tax_adjusted             = ' || p_adj_rec.tax_adjusted);
8715             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.freight_adjusted         = ' || p_adj_rec.freight_adjusted);
8716             arp_standard.debug('Dump_Init_Amts: ' || 'p_adj_rec.receivables_charges_adjusted = '
8717                                                            || p_adj_rec.receivables_charges_adjusted);
8718          END IF;
8719 
8720    ELSIF p_type_acct = 'UNED' AND g_ae_doc_rec.source_table = 'RA' THEN
8721 
8722       IF PG_DEBUG in ('Y', 'C') THEN
8723          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.unearned_discount_taken       = ' || p_app_rec.unearned_discount_taken);
8724          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.acctd_unearned_discount_taken = ' || p_app_rec.acctd_unearned_discount_taken);
8725          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.line_uediscounted             = ' || p_app_rec.line_uediscounted);
8726          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.tax_uediscounted              = ' || p_app_rec.tax_uediscounted);
8727          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.freight_uediscounted          = ' || p_app_rec.freight_uediscounted);
8728          arp_standard.debug('Dump_Init_Amts: ' || 'p_app_rec.charges_uediscounted          = ' || p_app_rec.charges_uediscounted);
8729       END IF;
8730 
8731    END IF;
8732 
8733    IF PG_DEBUG in ('Y', 'C') THEN
8734       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.line_amt_alloc            = '||g_ae_rule_rec.line_amt_alloc);
8735       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.line_acctd_amt_alloc      = '||g_ae_rule_rec.line_acctd_amt_alloc);
8736       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.tax_amt_alloc             = '||g_ae_rule_rec.tax_amt_alloc);
8737       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.tax_acctd_amt_alloc       = '||g_ae_rule_rec.tax_acctd_amt_alloc);
8738       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.freight_amt_alloc         = '||g_ae_rule_rec.freight_amt_alloc);
8739       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.freight_acctd_amt_alloc   = '||g_ae_rule_rec.freight_acctd_amt_alloc);
8740       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.charges_amt_alloc         = '||g_ae_rule_rec.charges_amt_alloc);
8741       arp_standard.debug('Dump_Init_Amts: ' || 'g_ae_rule_rec.charges_acctd_amt_alloc   = '||g_ae_rule_rec.charges_acctd_amt_alloc);
8742       arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Init_Amts()-');
8743    END IF;
8744 
8745 EXCEPTION
8746   WHEN OTHERS THEN
8747      IF PG_DEBUG in ('Y', 'C') THEN
8748         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Dump_Init_Amts');
8749      END IF;
8750      RAISE;
8751 
8752 END Dump_Init_Amts;
8753 
8754 /* ==========================================================================
8755  | PROCEDURE Dump_Line_Amts
8756  |
8757  | DESCRIPTION
8758  |    Dumps data accounting line data
8759  |
8760  | SCOPE - PRIVATE
8761  |
8762  | PARAMETERS
8763  |    p_ae_line_rec          IN      Accounting lines record
8764  *==========================================================================*/
8765 PROCEDURE Dump_Line_Amts(p_ae_line_rec  IN ar_ae_alloc_rec_gt%ROWTYPE) IS
8766 
8767 BEGIN
8768 
8769        arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Line_Amts()+');
8770        arp_standard.debug('Dump_Line_Amts: ' || 'ae_id = ' || p_ae_line_rec.ae_id);
8771        arp_standard.debug('Dump_Line_Amts: ' || 'ae_line_type = ' || p_ae_line_rec.ae_line_type);
8772        arp_standard.debug('Dump_Line_Amts: ' || 'ae_line_type_secondary = ' || p_ae_line_rec.ae_line_type_secondary);
8773        arp_standard.debug('Dump_Line_Amts: ' || 'ae_source_id    = ' || p_ae_line_rec.ae_source_id);
8774        arp_standard.debug('Dump_Line_Amts: ' || 'ae_source_table = ' || p_ae_line_rec.ae_source_table);
8775        arp_standard.debug('Dump_Line_Amts: ' || 'ae_account      = ' || p_ae_line_rec.ae_account);
8776        arp_standard.debug('Dump_Line_Amts: ' || 'ae_entered_dr   = ' || p_ae_line_rec.ae_entered_dr);
8777        arp_standard.debug('Dump_Line_Amts: ' || 'ae_entered_cr   = ' || p_ae_line_rec.ae_entered_cr);
8778        arp_standard.debug('Dump_Line_Amts: ' || 'ae_accounted_dr = ' || p_ae_line_rec.ae_accounted_dr);
8779        arp_standard.debug('Dump_Line_Amts: ' || 'ae_accounted_cr = ' || p_ae_line_rec.ae_accounted_cr);
8780        arp_standard.debug('Dump_Line_Amts: ' || 'ae_source_id_secondary = ' || p_ae_line_rec.ae_source_id_secondary);
8781        arp_standard.debug('Dump_Line_Amts: ' || 'ae_source_table_secondary = ' || p_ae_line_rec.ae_source_table_secondary);
8782        arp_standard.debug('Dump_Line_Amts: ' || 'ae_currency_code = ' || p_ae_line_rec.ae_currency_code);
8783        arp_standard.debug('Dump_Line_Amts: ' || 'ae_currency_conversion_rate = ' || p_ae_line_rec.ae_currency_conversion_rate);
8784        arp_standard.debug('Dump_Line_Amts: ' || 'ae_currency_conversion_type = ' || p_ae_line_rec.ae_currency_conversion_type);
8785        arp_standard.debug('Dump_Line_Amts: ' || 'ae_currency_conversion_date = ' || p_ae_line_rec.ae_currency_conversion_date);
8786        arp_standard.debug('Dump_Line_Amts: ' || 'ae_third_party_id           = ' || p_ae_line_rec.ae_third_party_id);
8787        arp_standard.debug('Dump_Line_Amts: ' || 'ae_third_party_sub_id       = ' || p_ae_line_rec.ae_third_party_sub_id);
8788        arp_standard.debug('Dump_Line_Amts: ' || 'ae_tax_group_code_id        = ' || p_ae_line_rec.ae_tax_group_code_id);
8789        arp_standard.debug('Dump_Line_Amts: ' || 'ae_tax_code_id              = ' || p_ae_line_rec.ae_tax_code_id);
8790        arp_standard.debug('Dump_Line_Amts: ' || 'ae_location_segment_id      = ' || p_ae_line_rec.ae_location_segment_id);
8791        arp_standard.debug('Dump_Line_Amts: ' || 'ae_taxable_entered_dr       = ' || p_ae_line_rec.ae_taxable_entered_dr);
8792        arp_standard.debug('Dump_Line_Amts: ' || 'ae_taxable_entered_cr       = ' || p_ae_line_rec.ae_taxable_entered_cr);
8793        arp_standard.debug('Dump_Line_Amts: ' || 'ae_taxable_accounted_dr     = ' || p_ae_line_rec.ae_taxable_accounted_dr);
8794        arp_standard.debug('Dump_Line_Amts: ' || 'ae_taxable_accounted_cr     = ' || p_ae_line_rec.ae_taxable_accounted_cr);
8795        arp_standard.debug('Dump_Line_Amts: ' || 'ae_tax_link_id              = ' || p_ae_line_rec.ae_tax_link_id);
8796        arp_standard.debug('Dump_Line_Amts: ' || 'ae_reversed_source_id       = ' || p_ae_line_rec.ae_reversed_source_id);
8797        arp_standard.debug('Dump_Line_Amts: ' || 'ae_summarize_flag           = ' || p_ae_line_rec.ae_summarize_flag);
8798        arp_standard.debug('Dump_Line_Amts: ' || 'ae_neg_ind               = ' || p_ae_line_rec.ae_neg_ind);
8799 
8800        arp_standard.debug('Dump_Line_Amts: ' || 'activity_bucket                   = ' || p_ae_line_rec.activity_bucket);
8801        arp_standard.debug('Dump_Line_Amts: ' || 'ref_account_class                = ' || p_ae_line_rec.ref_account_class);
8802        arp_standard.debug('Dump_Line_Amts: ' || 'ref_customer_trx_line_id = ' || p_ae_line_rec.ae_customer_trx_line_id);
8803        arp_standard.debug('Dump_Line_Amts: ' || 'ref_cust_trx_line_gl_dist_id= ' || p_ae_line_rec.ae_cust_trx_line_gl_dist_id);
8804        arp_standard.debug('Dump_Line_Amts: ' || 'ref_line_id              = ' || p_ae_line_rec.ae_ref_line_id);
8805        arp_standard.debug('Dump_Line_Amts: ' || 'ref_dist_ccid            = ' || p_ae_line_rec.ref_dist_ccid);
8806        arp_standard.debug('Dump_Line_Amts: ' || 'ref_mf_dist_flag         = ' || p_ae_line_rec.ref_mf_dist_flag);
8807 
8808       arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Line_Amts()-');
8809 
8810 EXCEPTION
8811   WHEN OTHERS THEN
8812      IF PG_DEBUG in ('Y', 'C') THEN
8813         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Dump_Line_Amts');
8814      END IF;
8815      RAISE;
8816 
8817 END Dump_Line_Amts;
8818 
8819 /* ==========================================================================
8820  | PROCEDURE Dump_Dist_Amts
8821  |
8822  | DESCRIPTION
8823  |    Dumps data accounting line data
8824  |
8825  | SCOPE - PRIVATE
8826  |
8827  | PARAMETERS
8828  |    p_ae_line_rec          IN      Accounting lines record
8829  *==========================================================================*/
8830 PROCEDURE Dump_Dist_Amts(p_ae_line_rec  IN ar_distributions%ROWTYPE) IS
8831 BEGIN
8832 
8833     IF PG_DEBUG in ('Y', 'C') THEN
8834        arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Dist_Amts()+');
8835        arp_standard.debug('Dump_Dist_Amts: ' || 'ae_id                    = ' || g_id);
8836        arp_standard.debug('Dump_Dist_Amts: ' || 'source_type              = ' || p_ae_line_rec.source_type);
8837        arp_standard.debug('Dump_Dist_Amts: ' || 'source_type_secondary    = ' || p_ae_line_rec.source_type_secondary);
8838        arp_standard.debug('Dump_Dist_Amts: ' || 'source_id                = ' || p_ae_line_rec.source_id);
8839        arp_standard.debug('Dump_Dist_Amts: ' || 'source_table             = ' || p_ae_line_rec.source_table);
8840        arp_standard.debug('Dump_Dist_Amts: ' || 'code_combination_id      = ' || p_ae_line_rec.code_combination_id);
8841        arp_standard.debug('Dump_Dist_Amts: ' || 'amount_dr                = ' || p_ae_line_rec.amount_dr);
8842        arp_standard.debug('Dump_Dist_Amts: ' || 'amount_cr                = ' || p_ae_line_rec.amount_cr);
8843        arp_standard.debug('Dump_Dist_Amts: ' || 'acctd_amount_dr          = ' || p_ae_line_rec.acctd_amount_dr);
8844        arp_standard.debug('Dump_Dist_Amts: ' || 'acctd_amount_cr          = ' || p_ae_line_rec.acctd_amount_cr);
8845        arp_standard.debug('Dump_Dist_Amts: ' || 'source_id_secondary      = ' || p_ae_line_rec.source_id_secondary);
8846        arp_standard.debug('Dump_Dist_Amts: ' || 'source_table_secondary   = ' || p_ae_line_rec.source_table_secondary);
8847        arp_standard.debug('Dump_Dist_Amts: ' || 'currency_code            = ' || p_ae_line_rec.currency_code);
8848        arp_standard.debug('Dump_Dist_Amts: ' || 'currency_conversion_rate = ' || p_ae_line_rec.currency_conversion_rate);
8849        arp_standard.debug('Dump_Dist_Amts: ' || 'currency_conversion_type = ' || p_ae_line_rec.currency_conversion_type);
8850        arp_standard.debug('Dump_Dist_Amts: ' || 'currency_conversion_date = ' || p_ae_line_rec.currency_conversion_date);
8851        arp_standard.debug('Dump_Dist_Amts: ' || 'third_party_id           = ' || p_ae_line_rec.third_party_id);
8852        arp_standard.debug('Dump_Dist_Amts: ' || 'third_party_sub_id       = ' || p_ae_line_rec.third_party_sub_id);
8853        arp_standard.debug('Dump_Dist_Amts: ' || 'tax_group_code_id        = ' || p_ae_line_rec.tax_group_code_id);
8854        arp_standard.debug('Dump_Dist_Amts: ' || 'tax_code_id              = ' || p_ae_line_rec.tax_code_id);
8855        arp_standard.debug('Dump_Dist_Amts: ' || 'location_segment_id      = ' || p_ae_line_rec.location_segment_id);
8856        arp_standard.debug('Dump_Dist_Amts: ' || 'taxable_entered_dr       = ' || p_ae_line_rec.taxable_entered_dr);
8857        arp_standard.debug('Dump_Dist_Amts: ' || 'taxable_entered_cr       = ' || p_ae_line_rec.taxable_entered_cr);
8858        arp_standard.debug('Dump_Dist_Amts: ' || 'taxable_accounted_dr     = ' || p_ae_line_rec.taxable_accounted_dr);
8859        arp_standard.debug('Dump_Dist_Amts: ' || 'taxable_accounted_cr     = ' || p_ae_line_rec.taxable_accounted_cr);
8860        arp_standard.debug('Dump_Dist_Amts: ' || 'tax_link_id              = ' || p_ae_line_rec.tax_link_id);
8861        arp_standard.debug('Dump_Dist_Amts: ' || 'reversed_source_id       = ' || p_ae_line_rec.reversed_source_id);
8862        arp_standard.debug('ARP_ALLOCATION_PKG.Dump_Dist_Amts()-');
8863     END IF;
8864 
8865 EXCEPTION
8866   WHEN OTHERS THEN
8867      IF PG_DEBUG in ('Y', 'C') THEN
8868         arp_standard.debug('EXCEPTION: ARP_ALLOCATION_PKG.Dump_Dist_Amts');
8869      END IF;
8870      RAISE;
8871 
8872 END Dump_Dist_Amts;
8873 
8874 PROCEDURE adj_boundary_account
8875 (p_receivables_trx_id   IN     NUMBER,
8876  p_bucket               IN     VARCHAR2,
8877  p_ctlgd_id             IN     NUMBER,
8878  x_ccid                 IN OUT NOCOPY NUMBER)
8879 IS
8880 FUNCTION fct_adj_ccid
8881   (p_bucket                IN VARCHAR2,
8882    p_customer_trx_id       IN NUMBER,
8883    p_gl_account_source     IN VARCHAR2,
8884    p_code_combination_id   IN NUMBER,
8885    p_tax_code_source       IN VARCHAR2,
8886    p_receivables_trx_id    IN NUMBER)
8887 RETURN NUMBER
8888  IS
8889    CURSOR c1 IS
8890    SELECT arp_etax_util.get_tax_account(tax.tax_rate_id,
8891                                         null,
8892                                         'ADJ',
8893                                         'TAX_RATE'),
8894           arp_etax_util.get_tax_account(tax.tax_rate_id,
8895                                         null,
8896                                         'ADJ_NON_REC',
8897                                         'TAX_RATE'),
8898           rt.tax_recoverable_flag
8899      FROM ar_receivables_trx  rt,
8900           zx_sco_rates        tax
8901     WHERE rt.receivables_trx_id = p_receivables_trx_id
8902       AND rt.asset_tax_code        = tax.tax_rate_code
8903       AND sysdate between nvl(tax.effective_from, sysdate) AND
8904                           nvl(tax.effective_to, sysdate);
8905 
8906    CURSOR c1_le(p_legal_entity_id NUMBER) IS
8907    SELECT arp_etax_util.get_tax_account(tax.tax_rate_id,
8908                                         null,
8909                                         'ADJ',
8910                                         'TAX_RATE'),
8911           arp_etax_util.get_tax_account(tax.tax_rate_id,
8912                                         null,
8913                                         'ADJ_NON_REC',
8914                                         'TAX_RATE'),
8915           rt.tax_recoverable_flag
8916      FROM ar_receivables_trx    rt,
8917           ar_rec_trx_le_details rtd,
8918           zx_sco_rates          tax
8919     WHERE rt.receivables_trx_id = p_receivables_trx_id
8920       AND rtd.receivables_trx_id (+) = rt.receivables_trx_id
8921       AND rtd.legal_entity_id (+)    = p_legal_entity_id
8922       AND nvl(rtd.asset_tax_code, rt.asset_tax_code)
8923                                      = tax.tax_rate_code
8924       AND trunc(sysdate) between nvl(tax.effective_from, trunc(sysdate)) AND
8925                                  nvl(tax.effective_to, trunc(sysdate));
8926 
8927    CURSOR c2 IS
8928    SELECT arp_etax_util.get_tax_account(tl.customer_trx_line_id,
8929                                         trunc(sysdate),
8930                                         'ADJ',
8931                                         'TAX_LINE'),
8932           arp_etax_util.get_tax_account(tl.customer_trx_line_id,
8933                                         trunc(sysdate),
8934                                         'FINCHRG',
8935                                         'TAX_LINE')
8936      FROM ra_customer_trx_lines   tl
8937     WHERE tl.customer_trx_id   = p_customer_trx_id
8938       AND tl.line_type         = 'TAX'
8939       AND tl.tax_line_id IS NOT NULL;
8940 
8941    l_adj_ccid                   NUMBER;
8942    l_finchrg_ccid               NUMBER;
8943    l_adj_non_rec_tax_ccid       NUMBER;
8944    l_tax_recoverable_flag       VARCHAR2(10);
8945    l_le_id                      NUMBER;
8946  BEGIN
8947    arp_standard.debug('fct_adj_ccid +');
8948    arp_standard.debug('   p_bucket             :'||p_bucket);
8949    arp_standard.debug('   p_customer_trx_id    :'||p_customer_trx_id);
8950    arp_standard.debug('   p_gl_account_source  :'||p_gl_account_source);
8951    arp_standard.debug('   p_code_combination_id:'||p_code_combination_id);
8952    arp_standard.debug('   p_tax_code_source    :'||p_tax_code_source);
8953    arp_standard.debug('   p_receivables_trx_id :'||p_receivables_trx_id);
8954 
8955    /* Determine if LE is required and fetch from target
8956       transaction */
8957    IF arp_legal_entity_util.is_le_subscriber
8958    THEN
8959       /* get LE from target trx */
8960       SELECT legal_entity_id
8961       INTO   l_le_id
8962       FROM   ra_customer_trx
8963       WHERE  customer_trx_id = p_customer_trx_id;
8964    ELSE
8965       /* Not required/used, set to null */
8966       l_le_id := NULL;
8967    END IF;
8968 
8969    -- Tax boundary
8970    IF p_bucket = 'ADJ_TAX' THEN
8971      arp_standard.debug('  Tax boundary');
8972      IF p_tax_code_source = 'ACTIVITY' THEN
8973         arp_standard.debug('    tax_code_source ACTIVITY');
8974 
8975         IF l_le_id IS NOT NULL
8976         THEN
8977            OPEN c1_le(l_le_id);
8978            FETCH c1_le INTO l_adj_ccid,
8979                      l_adj_non_rec_tax_ccid,
8980                      l_tax_recoverable_flag;
8981            CLOSE c1_le;
8982         ELSE
8983            OPEN c1;
8984            FETCH c1 INTO l_adj_ccid,
8985                      l_adj_non_rec_tax_ccid,
8986                      l_tax_recoverable_flag;
8987            CLOSE c1;
8988         END IF;
8989         arp_standard.debug('      l_adj_ccid     :'||l_adj_ccid);
8990         arp_standard.debug('      l_adj_non_rec_tax_ccid     :'||l_adj_non_rec_tax_ccid);
8991         arp_standard.debug('      l_tax_recoverable_flag     :'||l_tax_recoverable_flag);
8992         IF l_tax_recoverable_flag = 'N' THEN
8993             arp_standard.debug('       Returning  l_adj_non_rec_tax_ccid :'||l_adj_non_rec_tax_ccid);
8994             RETURN l_adj_non_rec_tax_ccid;
8995         ELSE
8996             arp_standard.debug('       Returning  l_adj_ccid :'||l_adj_ccid);
8997             RETURN l_adj_ccid;
8998         END IF;
8999      ELSIF p_tax_code_source = 'INVOICE' THEN
9000         -- unexpected situation no tax line on invoice
9001         arp_standard.debug('   tax code source INVOICE unexpected');
9002         arp_standard.debug('       Returning  -8');
9003         RETURN -8;
9004      ELSIF p_tax_code_source = 'NONE' THEN
9005         --unexpected situation inpossible for tax adjustment
9006         arp_standard.debug('   tax code source NONE unexpected');
9007         arp_standard.debug('       Returning  -8');
9008         RETURN -8;
9009      END IF;
9010    -- Line boundary
9011    ELSIF p_bucket = 'ADJ_LINE' THEN
9012      arp_standard.debug('  Line boundary');
9013      IF p_gl_account_source = 'ACTIVITY_GL_ACCOUNT' THEN
9014         arp_standard.debug('    gl_acount_source ACTIVITY_GL_ACCOUNT');
9015         arp_standard.debug('      Returning p_code_combination_id:'||p_code_combination_id);
9016         RETURN p_code_combination_id;
9017      ELSIF p_gl_account_source = 'TAX_CODE_ON_INVOICE' THEN
9018         arp_standard.debug('    gl_acount_source TAX_CODE_ON_INVOICE');
9019         OPEN c2;
9020         FETCH c2 INTO l_adj_ccid,
9021                       l_finchrg_ccid;
9022         CLOSE c2;
9023         arp_standard.debug('      l_adj_ccid     :'||l_adj_ccid);
9024         arp_standard.debug('      l_finchrg_ccid :'||l_finchrg_ccid);
9025         arp_standard.debug('       Returning  l_adj_ccid :'||l_adj_ccid);
9026         RETURN l_adj_ccid;
9027      ELSIF p_gl_account_source = 'REVENUE_ON_INVOICE' THEN
9028         -- Unexpected situation - no invoice line
9029         arp_standard.debug('   gl account source REVENUE_ON_INVOICE unexpected');
9030         arp_standard.debug('       Returning  -6');
9031         RETURN -6;
9032      END IF;
9033    -- Chrg boundary
9034    ELSIF p_bucket = 'ADJ_CHRG' THEN
9035      arp_standard.debug('  Charges boundary');
9036      IF p_gl_account_source = 'ACTIVITY_GL_ACCOUNT' THEN
9037         arp_standard.debug('    gl_acount_source ACTIVITY_GL_ACCOUNT');
9038         arp_standard.debug('      Returning p_code_combination_id:'||p_code_combination_id);
9039         RETURN p_code_combination_id;
9040      ELSIF p_gl_account_source = 'TAX_CODE_ON_INVOICE' THEN
9041         OPEN c2;
9042         FETCH c2 INTO l_adj_ccid,
9043                       l_finchrg_ccid;
9044         CLOSE c2;
9045         arp_standard.debug('      l_adj_ccid     :'||l_adj_ccid);
9046         arp_standard.debug('      l_finchrg_ccid :'||l_finchrg_ccid);
9047         arp_standard.debug('       Returning  l_finchrg_ccid :'||l_finchrg_ccid);
9048         RETURN l_finchrg_ccid;
9049      ELSIF p_gl_account_source = 'REVENUE_ON_INVOICE' THEN
9050         --Chrg boundary <=> no original amount invoice line so REVENUE_ON_INVOICE is unexpected
9051         arp_standard.debug('   gl account source REVENUE_ON_INVOICE unexpected');
9052         arp_standard.debug('       Returning  -7');
9053         RETURN -7;
9054      END IF;
9055    -- Chrg boundary
9056    ELSIF p_bucket = 'ADJ_FRT' THEN
9057      arp_standard.debug('  Charges boundary');
9058      IF p_gl_account_source = 'ACTIVITY_GL_ACCOUNT' THEN
9059         arp_standard.debug('    gl_acount_source ACTIVITY_GL_ACCOUNT');
9060         arp_standard.debug('      Returning p_code_combination_id:'||p_code_combination_id);
9061         RETURN p_code_combination_id;
9062      ELSIF p_gl_account_source = 'TAX_CODE_ON_INVOICE' THEN
9063         OPEN c2;
9064         FETCH c2 INTO l_adj_ccid,
9065                       l_finchrg_ccid;
9066         CLOSE c2;
9067         arp_standard.debug('      l_adj_ccid     :'||l_adj_ccid);
9068         arp_standard.debug('      l_finchrg_ccid :'||l_finchrg_ccid);
9069         arp_standard.debug('       Returning  L_ADJ_CCID :'||l_adj_ccid);
9070         RETURN l_adj_ccid;
9071      ELSIF p_gl_account_source = 'REVENUE_ON_INVOICE' THEN
9072         --Frt boundary <=> no original amount invoice line so REVENUE_ON_INVOICE is unexpected
9073         arp_standard.debug('   gl account source REVENUE_ON_INVOICE unexpected');
9074         arp_standard.debug('       Returning  -9');
9075         RETURN -9;
9076      END IF;
9077    END IF;
9078 END;
9079 BEGIN
9080 --   IF     g_ae_summ_tax_tbl.bucket IN ('ADJ_LINE','ADJ_TAX','ADJ_FRT','ADJ_CHRG')
9081 --      AND g_ae_summ_rev_tbl.ae_cust_trx_line_gl_dist_id IN (-6,-7,-8,-9)
9082  arp_standard.debug('  p_receivables_trx_id  :'||  p_receivables_trx_id);
9083  arp_standard.debug('  p_bucket              :'||  p_bucket);
9084  arp_standard.debug('  p_ctlgd_id            :'||  p_ctlgd_id );
9085   -- bug#5016123
9086   -- No Accounting should be created for the adj the ccid should keep the receivable ccid
9087   -- of the initial doc - but the distributions generated by the shadow adjustment
9088   -- should serve as template for the Transaction history
9089   -- BR shadow adjustment
9090    IF  p_receivables_trx_id  = -15 THEN
9091       x_ccid := NULL;
9092    ELSIF  p_bucket IN ('ADJ_LINE','ADJ_TAX','ADJ_FRT','ADJ_CHRG')
9093 --      AND p_ctlgd_id IN (-6,-7,-8,-9)
9094    THEN
9095       x_ccid := fct_adj_ccid (p_bucket            => p_bucket,
9096                               p_customer_trx_id   => g_Cust_inv_rec.customer_trx_id,
9097                               p_gl_account_source => g_ae_rule_rec.gl_account_source1,
9098                               p_code_combination_id => g_ae_rule_rec.code_combination_id1,
9099                               p_tax_code_source    => g_ae_rule_rec.tax_code_source1,
9100                               p_receivables_trx_id  => p_receivables_trx_id);
9101    arp_standard.debug('  x_ccid            :'||  x_ccid );
9102    END IF;
9103 END;
9104 
9105 
9106 END ARP_ALLOCATION_PKG;