DBA Data[Home] [Help]

PACKAGE BODY: APPS.ARP_ADJUSTMENTS_MAIN

Source


1 PACKAGE BODY ARP_ADJUSTMENTS_MAIN AS
2 /* $Header: ARTADJMB.pls 120.17.12020000.2 2012/08/04 21:42:57 dgaurab ship $ */
3 
4 /* =======================================================================
5  | Package Globals
6  * ======================================================================*/
7   g_mode			VARCHAR2(30);
8   g_ae_doc_rec			ae_doc_rec_type;
9   g_ae_event_rec		ae_event_rec_type;
10   g_ae_line_tbl                 ae_line_tbl_type;
11   g_empty_ae_line_tbl           ae_line_tbl_type;
12   g_ae_sys_rec                  ae_sys_rec_type;
13   g_ae_line_ctr                 BINARY_INTEGER ;
14 
15   --{3377004
16   TYPE ctl_rem_amt_type IS RECORD
17   (customer_trx_line_id           DBMS_SQL.NUMBER_TABLE,
18    amount_due_remaining           DBMS_SQL.NUMBER_TABLE,
19    acctd_amount_due_remaining     DBMS_SQL.NUMBER_TABLE,
20    chrg_amount_remaining          DBMS_SQL.NUMBER_TABLE,
21    chrg_acctd_amount_remaining    DBMS_SQL.NUMBER_TABLE);
22   --}
23 
24 /* =======================================================================
25  | Private Procedure/Function prototypes
26  * ======================================================================*/
27   PROCEDURE Init_Ae_Lines;
28 
29   PROCEDURE Derive_Accounting_Entry(p_from_llca_call      IN  VARCHAR2 DEFAULT 'N',
30 				    p_gt_id               IN  NUMBER   DEFAULT NULL
31   );
32 
33   PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('AFLOG_ENABLED'), 'N');
34 
35   PROCEDURE Delete_ADJ(p_ae_deleted   OUT NOCOPY BOOLEAN);
36 
37   PROCEDURE Reverse_Adjustment;
38 
39   PROCEDURE Get_Doc_Entitity_Data (
40                 p_level          IN  VARCHAR2                           ,
41                 p_adj_rec        OUT NOCOPY ar_adjustments%ROWTYPE             ,
42                 p_cust_inv_rec   OUT NOCOPY ra_customer_trx%ROWTYPE            ,
43                 p_ctlgd_inv_rec  OUT NOCOPY ra_cust_trx_line_gl_dist%ROWTYPE   ,
44                 p_rule_rec       OUT NOCOPY ae_rule_rec_type                   ,
45                 p_ard_rec        OUT NOCOPY ar_distributions%ROWTYPE              );
46 
47 -- Added parameter for Line Level Adjustment
48   PROCEDURE Create_Ae_Lines_Common (
49                 p_level         IN VARCHAR2,
50 		p_from_llca_call      IN  VARCHAR2 DEFAULT 'N',
51 		p_gt_id               IN  NUMBER   DEFAULT NULL);
52 -- Added parameter for Line Level Adjustment
53   PROCEDURE Create_Ae_Lines_ADJ(
54                 p_adj_rec        IN ar_adjustments%ROWTYPE             ,
55                 p_cust_inv_rec   IN ra_customer_trx%ROWTYPE            ,
56                 p_ctlgd_inv_rec  IN ra_cust_trx_line_gl_dist%ROWTYPE   ,
57                 p_rule_rec       IN ae_rule_rec_type                   ,
58                 p_ard_rec        IN ar_distributions%ROWTYPE           ,
59 		p_from_llca_call      IN  VARCHAR2 DEFAULT 'N'         ,
60 		p_gt_id               IN  NUMBER   DEFAULT NULL);
61 
62   PROCEDURE Assign_Ael_Elements(
63                 p_ae_line_rec           IN ae_line_rec_type );
64 
65   --{3377004
66   FUNCTION ctl_id_index(p_ctl_id_tab  IN DBMS_SQL.NUMBER_TABLE,
67                         p_ctl_id      IN NUMBER)
68   RETURN NUMBER;
69 
70   PROCEDURE init_rem_amt(x_rem_amt IN OUT NOCOPY ctl_rem_amt_type,
71                          p_index   IN NUMBER);
72   --}
73 
74 /* =======================================================================
75  | Procedures/functions
76  * ======================================================================*/
77 /* =======================================================================
78  | PUBLIC PROCEDURE Delete_Acct
79  |
80  | DESCRIPTION
81  |      Accounting Entry Deletion
82  |      -------------------------
83  |      This procedure is the Accounting Entry deletion routine which
84  |      deletes data associated with Adjustments based on event and source
85  |      table.
86  |
87  | PARAMETERS
88  |      p_mode          IN      Document or Accounting Event mode
89  |      p_ae_doc_rec    IN      Document Record
90  |      p_ae_event_rec  IN      Event Record
91  |      p_ae_deleted    OUT NOCOPY     AE Lines deletion status
92  * ======================================================================*/
93 PROCEDURE Delete_Acct( p_mode         IN  VARCHAR2,
94                        p_ae_doc_rec   IN  ae_doc_rec_type,
95                        p_ae_event_rec IN  ae_event_rec_type,
96                        p_ae_deleted   OUT NOCOPY BOOLEAN            ) IS
97 BEGIN
98   IF PG_DEBUG in ('Y', 'C') THEN
99      arp_standard.debug(   'ARP_ADJUSTMENTS_MAIN.Delete_Acct()+');
100   END IF;
101 
102   /*----------------------------------------------------+
103    | Copy Document/Event Data to Global                 |
104    +----------------------------------------------------*/
105   g_mode         := p_mode;
106   g_ae_doc_rec   := p_ae_doc_rec;
107   g_ae_event_rec := p_ae_event_rec;
108 
109   IF ( g_ae_doc_rec.source_table = 'ADJ' ) THEN
110 
111      Delete_ADJ(p_ae_deleted => p_ae_deleted) ;
112 
113   END IF;
114 
115   IF PG_DEBUG in ('Y', 'C') THEN
116      arp_standard.debug(   'ARP_ADJUSTMENTS_MAIN.Delete_Acct()-');
117   END IF;
118 
119 EXCEPTION
120   WHEN OTHERS THEN
121      IF PG_DEBUG in ('Y', 'C') THEN
122         arp_standard.debug(  'EXCEPTION: ARP_ADJUSTMENTS_MAIN.Delete_Acct');
123      END IF;
124      RAISE;
125 
126 END Delete_Acct;
127 
128 /* =======================================================================
129  | PUBLIC PROCEDURE Execute
130  |
131  | DESCRIPTION
132  | 	Accounting Entry Derivation Method
133  | 	----------------------------------
134  | 	This procedure is the Accounting Entry derivation method for all
135  | 	accounting events associated with the Adjustments layer.
136  |
137  | 	Functions of the AE Derivation Method are:
138  | 		- Single Entry Point for easy extensibility
139  | 		- Read Event Data
140  | 		- Read Transaction and Setup Data
141  | 		- Determine AE Lines affected
142  | 		- Derive AE Lines
143  | 		- Return AE Lines created in a PL/SQL table.
144  |
145  | PARAMETERS
146  |      p_mode          IN      Document or Accounting Event mode
147  |      p_ae_doc_rec    IN      Document Record
148  | 	p_ae_event_rec	IN	Event Record
149  | 	p_ae_line_tbl	OUT NOCOPY	AE Lines table
150  | 	p_ae_created	OUT NOCOPY	AE Lines creation status
151  * ======================================================================*/
152 PROCEDURE Execute( p_mode                IN VARCHAR2,
153                    p_ae_doc_rec          IN ae_doc_rec_type,
154                    p_ae_event_rec        IN ae_event_rec_type,
155                    p_ae_line_tbl         OUT NOCOPY ae_line_tbl_type,
156                    p_ae_created          OUT NOCOPY BOOLEAN,
157 		   p_from_llca_call      IN  VARCHAR2 DEFAULT 'N',
158 		   p_gt_id               IN  NUMBER   DEFAULT NULL) IS
159 
160 
161 BEGIN
162   IF PG_DEBUG in ('Y', 'C') THEN
163      arp_standard.debug(   'ARP_ADJUSTMENTS_MAIN.Execute()+');
164   END IF;
165   /*------------------------------------------------------+
166    | Initialize Accounting Entry Header and Lines	  |
167    +------------------------------------------------------*/
168   g_ae_line_ctr := 0;
169   Init_Ae_Lines;
170 
171   /*---------------------------------------------------------------+
172    | Copy Document/Event Data to Global, derive System Options info|
173    +---------------------------------------------------------------*/
174   g_mode 	 := p_mode;
175   g_ae_doc_rec 	 := p_ae_doc_rec;
176   g_ae_event_rec := p_ae_event_rec;
177 
178   --Get system options info
179 
180   g_ae_sys_rec.set_of_books_id   := ARP_ACCT_MAIN.ae_sys_rec.set_of_books_id;
181   g_ae_sys_rec.gain_cc_id        := ARP_ACCT_MAIN.ae_sys_rec.gain_cc_id;
182   g_ae_sys_rec.loss_cc_id        := ARP_ACCT_MAIN.ae_sys_rec.loss_cc_id;
183   g_ae_sys_rec.round_cc_id       := ARP_ACCT_MAIN.ae_sys_rec.round_cc_id;
184   g_ae_sys_rec.coa_id            := ARP_ACCT_MAIN.ae_sys_rec.coa_id;
185   g_ae_sys_rec.base_currency     := ARP_ACCT_MAIN.ae_sys_rec.base_currency;
186   g_ae_sys_rec.base_precision    := ARP_ACCT_MAIN.ae_sys_rec.base_precision;
187   g_ae_sys_rec.base_min_acc_unit := ARP_ACCT_MAIN.ae_sys_rec.base_min_acc_unit;
188 
189   -- MRC TRIGGER REPLACEMENT
190   -- Initialize a new global variable:
191   g_ae_sys_rec.sob_type          := ARP_ACCT_MAIN.ae_sys_rec.sob_type;
192 
193   /*------------------------------------------------------+
194    | Derive Accounting Entry                              |
195    +------------------------------------------------------*/
196   Derive_Accounting_Entry( p_from_llca_call  => p_from_llca_call,
197                             p_gt_id          => p_gt_id);
198 
199   /*------------------------------------------------------+
200    | Return Accounting Entry Creation Status              |
201    +------------------------------------------------------*/
202   p_ae_line_tbl := g_ae_line_tbl;
203 
204   IF g_ae_line_tbl.EXISTS(g_ae_line_ctr) THEN
205 
206     p_ae_created := TRUE;
207 
208   ELSE
209 
210     p_ae_created := FALSE;
211 
212   END IF;
213 
214   IF PG_DEBUG in ('Y', 'C') THEN
215      arp_standard.debug(   'ARP_ADJUSTMENTS_MAIN.Execute()-');
216   END IF;
217 
218 EXCEPTION
219   WHEN NO_DATA_FOUND THEN
220      IF PG_DEBUG in ('Y', 'C') THEN
221         arp_standard.debug(  'ARP_ADJUSTMENTS_MAIN.Execute - NO_DATA_FOUND' );
222      END IF;
223      RAISE;
224 
225   WHEN OTHERS THEN
226      IF PG_DEBUG in ('Y', 'C') THEN
227         arp_standard.debug(  'EXCEPTION: ARP_ADJUSTMENTS_MAIN.Execute');
228      END IF;
229      RAISE;
230 
231 END Execute;
232 
233 
234 /* =======================================================================
235  |
236  | PROCEDURE Init_Ae_Lines
237  |
238  * ======================================================================*/
239 PROCEDURE Init_Ae_Lines IS
240 
241 BEGIN
242 
243   IF PG_DEBUG in ('Y', 'C') THEN
244      arp_standard.debug(   'ARP_ADJUSTMENTS_MAIN.Init_Ae_Lines()+');
245   END IF;
246 
247   g_ae_line_tbl := g_empty_ae_line_tbl;
248 
249   IF PG_DEBUG in ('Y', 'C') THEN
250      arp_standard.debug(   'ARP_ADJUSTMENTS_MAIN.Init_Ae_Lines()-');
251   END IF;
252 EXCEPTION
253 
254   WHEN OTHERS THEN
255      IF PG_DEBUG in ('Y', 'C') THEN
256         arp_standard.debug(  'EXCEPTION: ARP_ADJUSTMENTS_MAIN.Init_Ae_Lines');
257      END IF;
258      RAISE;
259 
260 END Init_Ae_Lines;
261 
262 /* =======================================================================
263  |
264  | PROCEDURE Derive_Accounting_Entry
265  |
266  | DESCRIPTION
267  |      This procedure gets the necessary transaction data and determines
268  |      the accounting entries to be created at each of entity level.
269  |
270  | PARAMETERS
271  | 	Event_Rec	Global Event Record
272  * ======================================================================*/
273 PROCEDURE Derive_Accounting_Entry (p_from_llca_call      IN  VARCHAR2 DEFAULT 'N',
274 				   p_gt_id               IN  NUMBER   DEFAULT NULL
275 )
276 IS
277 
278 BEGIN
279   IF PG_DEBUG in ('Y', 'C') THEN
280      arp_standard.debug(  'ARP_ADJUSTMENTS_MAIN.Derive_Accounting_Entry()+');
281   END IF;
282   /*------------------------------------------------------------+
283    | Create Accounting Entries at the Document Entity level.    |
284    +------------------------------------------------------------*/
285   IF ( g_ae_doc_rec.accounting_entity_level = 'ONE' ) THEN
286 
287      IF ((g_ae_doc_rec.source_id_old IS NOT NULL) and (g_ae_doc_rec.other_flag = 'REVERSE')) THEN
288         Reverse_Adjustment;
289 
290      ELSIF ( g_ae_doc_rec.source_table = 'ADJ' ) THEN
291 
292        /*---------------------------------------------------------+
293         | Create Accounting Entry Lines, Adjustments              |
294         +---------------------------------------------------------*/
295            Create_Ae_Lines_Common(p_level => 'ADJ',
296 			    p_from_llca_call => p_from_llca_call,
297                             p_gt_id          => p_gt_id);
298 
299      END IF;
300 
301   END IF;		-- accounting_entity_level = ONE
302 
303   /*------------------------------------------------------+
304    | Create Accounting Entries at the Document Level      |
305    | (All Entities)                                       |
306    +------------------------------------------------------*/
307   IF ( g_ae_doc_rec.accounting_entity_level = 'ALL' ) THEN
308 
309 	NULL;
310 
311   END IF;		-- accounting_entity_level = ALL?
312 
313   IF PG_DEBUG in ('Y', 'C') THEN
314      arp_standard.debug(   'ARP_ADJUSTMENTS_MAIN.Derive_Accounting_Entry()-');
315   END IF;
316 
317 EXCEPTION
318   WHEN OTHERS THEN
319      IF PG_DEBUG in ('Y', 'C') THEN
320         arp_standard.debug(  'EXCEPTION: ARP_ADJUSTMENTS_MAIN.Derive_Accounting_Entry');
321      END IF;
322      RAISE;
323 
324 END Derive_Accounting_Entry;
325 
326 /* =======================================================================
327  |
328  | PROCEDURE Delete_ADJ
329  |
330  | DESCRIPTION
331  |      Deletes accounting associated with a Adjustment id from the
332  |      AR_DISTRIBUTIONS table.This routine deletes all records
333  |      matching the input source_id. Note records from child table
334  |      (AR_DISTRIBUTIONS) be deleted first.
335  |
336  | PARAMETERS
337  |      p_ae_deleted       indicates whether records were deleted
338  |                         for source_id
339  * ======================================================================*/
340 PROCEDURE Delete_ADJ(p_ae_deleted   OUT NOCOPY BOOLEAN) IS
341 
342 l_source_id ar_distributions.source_id%TYPE;
343 
344 l_ar_dist_key_value_list   gl_ca_utility_pkg.r_key_value_arr;
345 
346 BEGIN
347     IF PG_DEBUG in ('Y', 'C') THEN
348        arp_standard.debug('ARP_ADJUSTMENTS_MAIN.Delete_ADJ()+');
349     END IF;
350 
351  /*-------------------------------------------------------------------+
352   | Verify that the source id is a valid candidate for deletion       |
353   +-------------------------------------------------------------------*/
354     SELECT adj.adjustment_id
355     INTO   l_source_id
356     FROM   ar_adjustments adj
357     WHERE  adj.adjustment_id = g_ae_doc_rec.source_id
358     AND    adj.posting_control_id = -3
359     AND    g_ae_doc_rec.source_table = 'ADJ'
360     AND EXISTS (SELECT 'x'
361                 FROM  ar_distributions ard
362                 WHERE ard.source_id = adj.adjustment_id
363                 AND   ard.source_table = 'ADJ');
364 
365  /*-------------------------------------------------------------------+
366   | Delete all accounting for source id and source table combination  |
367   | if valid candidate for deletion                                   |
368   +-------------------------------------------------------------------*/
369 
370     -- modified for mrc trigger elimination.
371     DELETE FROM AR_DISTRIBUTIONS
372     WHERE  source_id    =  l_source_id
373     AND    source_table = 'ADJ'
374     RETURNING line_id
375     BULK COLLECT INTO l_ar_dist_key_value_list;
376 
377     /*---------------------------------+
378      | Calling central MRC library     |
379      | for MRC Integration             |
380      +---------------------------------*/
381 --{BUG#4301323
382 --     ar_mrc_engine.maintain_mrc_data(
383 --               p_event_mode        => 'DELETE',
384 --               p_table_name        => 'AR_DISTRIBUTIONS',
385 --               p_mode              => 'BATCH',
386 --               p_key_value_list    => l_ar_dist_key_value_list);
387 --}
388 
389     IF PG_DEBUG in ('Y', 'C') THEN
390        arp_standard.debug('ARP_ADJUSTMENTS_MAIN.Delete_ADJ()-');
391     END IF;
392 
393 EXCEPTION
394   WHEN NO_DATA_FOUND THEN
395      IF PG_DEBUG in ('Y', 'C') THEN
396         arp_standard.debug('ARP_ADJUSTMENTS_MAIN.Delete_ADJ - NO_DATA_FOUND' );
397      END IF;
398      p_ae_deleted := FALSE;
399      RAISE;
400 
401   WHEN OTHERS THEN
402      IF PG_DEBUG in ('Y', 'C') THEN
403         arp_standard.debug('EXCEPTION: ARP_ADJUSTMENTS_MAIN.Delete_ADJ');
404      END IF;
405      p_ae_deleted := FALSE;
406      RAISE ;
407 
408 END Delete_ADJ;
409 
410 /* =======================================================================
411  |
412  | PROCEDURE Reverse_Adjustment
413  |
414  | DESCRIPTION
415  |      This procedure reverses the records in AR_DISTRIBUTIONS for a
416  |      Adjustment. It gets the accounting for the old adjustment and
417  |      simply swaps the amount columns creating new accounting for the
418  |      new adjustment id. Adjustment reversals occur for receivable trx id
419  |      -13.
420  |
421  |      For Bills Receivable linked deferred tax for an application being
422  |      reversed is also backed out NOCOPY by using a link id to reverse out NOCOPY the
423  |      tax accounting associated with the Transaction history record for
424  |      the maturity date event for a Bill. For this change a union was
425  |      added and the source table is populated from g_ae_doc_rec
426  |
427  |      Reversals are opposite images of the old adjustment, with the exception
428  |      that reconciliation entries are not reversed from the old adjustment,
429  |      it is left to the Reconciliation routine to do this as an effect across
430  |      all activity.
431  |
432  | PARAMETERS
433  |      None
434  * ======================================================================*/
435 PROCEDURE Reverse_Adjustment IS
436 
437 -- MRC TRIGGER REPLACEMENT: Enumerate all columns and add two more unions
438 -- to retrieve MRC data
439 
440 CURSOR get_old_ard  IS
441        select ard.source_type,
442               ard.source_id_secondary,
443               ard.source_type_secondary,
444               ard.source_table_secondary,
445               ard.code_combination_id,
446               ard.amount_dr,
447               ard.amount_cr,
448               ard.acctd_amount_dr,
449               ard.acctd_amount_cr,
450               ard.taxable_entered_cr,
451               ard.taxable_entered_dr,
452               ard.taxable_accounted_cr,
453               ard.taxable_accounted_dr,
454               ard.currency_code,
455               ard.currency_conversion_rate,
456               ard.currency_conversion_type,
457               ard.currency_conversion_date,
458               ard.third_party_id,
459               ard.third_party_sub_id,
460               ard.tax_group_code_id,
461               ard.tax_code_id,
462               ard.location_segment_id,
463               ard.tax_link_id,
464               --{BUG#2979254
465               ard.ref_customer_trx_line_id,
466               ard.ref_cust_trx_line_gl_dist_id,
467               ard.ref_line_id,
468               --}
469               --{3377004
470               DECODE( ard.ref_customer_trx_line_id, NULL,'N',
471                       DECODE(adj.type,'CHARGES','ADDCHRG','ADDCTL')) WHICH_BUCKET,
472               --}
473 	      ard.ref_account_class,
474 	      ard.activity_bucket,
475 	      ard.ref_dist_ccid
476        from   ar_distributions ard,
477               ar_adjustments   adj
478        where  g_ae_sys_rec.sob_type = 'P'
479        and    ard.source_id = g_ae_doc_rec.source_id_old
480        and    ard.source_table = g_ae_doc_rec.source_table
481        and    nvl(ard.source_type_secondary,'X') NOT IN
482                                   ('ASSIGNMENT_RECONCILE','RECONCILE')
483        and    adj.adjustment_id(+) = g_ae_doc_rec.source_id_old  --3377004
484        UNION
485        select ard.source_type,
486               ard.source_id_secondary,
487               ard.source_type_secondary,
488               ard.source_table_secondary,
489               ard.code_combination_id,
490               ard.amount_dr,
491               ard.amount_cr,
492               ard.acctd_amount_dr,
493               ard.acctd_amount_cr,
494               ard.taxable_entered_cr,
495               ard.taxable_entered_dr,
496               ard.taxable_accounted_cr,
497               ard.taxable_accounted_dr,
498               ard.currency_code,
499               ard.currency_conversion_rate,
500               ard.currency_conversion_type,
501               ard.currency_conversion_date,
502               ard.third_party_id,
503               ard.third_party_sub_id,
504               ard.tax_group_code_id,
505               ard.tax_code_id,
506               ard.location_segment_id,
507               ard.tax_link_id,
508               --{BUG#2979254
509               ard.ref_customer_trx_line_id,
510               ard.ref_cust_trx_line_gl_dist_id,
511               ard.ref_line_id,
512               --}
513               --{3377004
514               DECODE( ard.ref_customer_trx_line_id, NULL,'N',
515                       DECODE(adj.type,'CHARGES','ADDCHRG','ADDCTL')) WHICH_BUCKET,
516               --}
517 	      ard.ref_account_class,
518 	      ard.activity_bucket,
519 	      ard.ref_dist_ccid
520        from ar_distributions ard,
521             ar_adjustments adj
522        where g_ae_sys_rec.sob_type = 'P'
523        and   adj.adjustment_id = g_ae_doc_rec.source_id_old
524        and   ard.source_id = adj.link_to_trx_hist_id
525        and   ard.source_table = 'TH' --for Bills Receivable Standard/Factored
526        and   nvl(ard.source_type_secondary,'X') NOT IN
527                                     ('ASSIGNMENT_RECONCILE','RECONCILE')
528        and   nvl(g_ae_doc_rec.event,'NONE') <> 'RISK_UNELIMINATED'
529        order  by 1 ;
530 --{BUG4301323
531 /*
532        UNION
533           select ard.source_type,
534               ard.source_id_secondary,
535               ard.source_type_secondary,
536               ard.source_table_secondary,
537               ard.code_combination_id,
538               ard.amount_dr,
539               ard.amount_cr,
540               ard.acctd_amount_dr,
541               ard.acctd_amount_cr,
542               ard.taxable_entered_cr,
543               ard.taxable_entered_dr,
544               ard.taxable_accounted_cr,
545               ard.taxable_accounted_dr,
546               ard.currency_code,
547               ard.currency_conversion_rate,
548               ard.currency_conversion_type,
549               ard.currency_conversion_date,
550               ard.third_party_id,
551               ard.third_party_sub_id,
552               ard.tax_group_code_id,
553               ard.tax_code_id,
554               ard.location_segment_id,
555               ard.tax_link_id,
556               --{BUG#2979254
557               ard.ref_customer_trx_line_id,
558               ard.ref_cust_trx_line_gl_dist_id,
559               ard.ref_line_id,
560               --}
561               --{3377004
562               DECODE( ard.ref_customer_trx_line_id, NULL,'N',
563                       DECODE(adj.type,'CHARGES','ADDCHRG','ADDCTL')) WHICH_BUCKET
564               --}
565        from   ar_mc_distributions_all ard,
566               ar_adjustments          adj
567        where  g_ae_sys_rec.sob_type = 'R'
568          and  ard.set_of_books_id = g_ae_sys_rec.set_of_books_id
569          and  ard.source_id = g_ae_doc_rec.source_id_old
570          and  ard.source_table = g_ae_doc_rec.source_table
571          and  nvl(ard.source_type_secondary,'X') NOT IN
572                                   ('ASSIGNMENT_RECONCILE','RECONCILE')
573          and  adj.adjustment_id(+)  = g_ae_doc_rec.source_id_old  --3377004
574          UNION
575          select ard.source_type,
576               ard.source_id_secondary,
577               ard.source_type_secondary,
578               ard.source_table_secondary,
579               ard.code_combination_id,
580               ard.amount_dr,
581               ard.amount_cr,
582               ard.acctd_amount_dr,
583               ard.acctd_amount_cr,
584               ard.taxable_entered_cr,
585               ard.taxable_entered_dr,
586               ard.taxable_accounted_cr,
587               ard.taxable_accounted_dr,
588               ard.currency_code,
589               ard.currency_conversion_rate,
590               ard.currency_conversion_type,
591               ard.currency_conversion_date,
592               ard.third_party_id,
593               ard.third_party_sub_id,
594               ard.tax_group_code_id,
595               ard.tax_code_id,
596               ard.location_segment_id,
597               ard.tax_link_id,
598               --{BUG#2979254
599               ard.ref_customer_trx_line_id,
600               ard.ref_cust_trx_line_gl_dist_id,
601               ard.ref_line_id,
602               --}
603               --{3377004
604               DECODE( ard.ref_customer_trx_line_id, NULL,'N',
605                       DECODE(adj.type,'CHARGES','ADDCHRG','ADDCTL')) WHICH_BUCKET
606               --}
607        from ar_mc_distributions_all ard,
608             ar_adjustments adj
609        where g_ae_sys_rec.sob_type = 'R'
610        and   g_ae_sys_rec.set_of_books_id = ard.set_of_books_id
611        and   adj.adjustment_id = g_ae_doc_rec.source_id_old
612        and   ard.source_id = adj.link_to_trx_hist_id
613        and   ard.source_table = 'TH' --for Bills Receivable Standard/Factored
614        and   nvl(ard.source_type_secondary,'X') NOT IN
615                                     ('ASSIGNMENT_RECONCILE','RECONCILE')
616        and   nvl(g_ae_doc_rec.event,'NONE') <> 'RISK_UNELIMINATED'
617        order  by 1 ;
618 */
619 -- MRC Trigger Replacement: modified cursor to extact information.
620 
621 CURSOR get_adj_details IS
622 SELECT adj.customer_trx_id                         customer_trx_id,
623        adj.amount                                  amount,
624 --{BUG4301323
625 --       DECODE(g_ae_sys_rec.sob_type, 'P',
626 --              adj.acctd_amount,
627 --              arp_mrc_acct_main.get_adj_entity_data(
628 --                  adj.adjustment_id,
629 --                  g_ae_sys_rec.set_of_books_id))   acctd_amount,
630          adj.acctd_amount                         acctd_amount,
631 --}
632        ctinv.invoice_currency_code                 invoice_currency_code   ,
633 --{BUG4301323
634 --       DECODE(g_ae_sys_rec.sob_type, 'P',
635 --              ctinv.exchange_rate,
636 --              arp_mrc_acct_main.get_ctx_exg_rate(
637 --                   ctinv.customer_trx_id,
638 --                   g_ae_sys_rec.set_of_books_id))  exchange_rate,
639        ctinv.exchange_rate                          exchange_rate,
640 --}
641 --{BUG4301323
642 --       DECODE(g_ae_sys_rec.sob_type, 'P',
643 --              ctinv.exchange_rate_type,
644 --              arp_mrc_acct_main.get_ctx_exg_rate_type(
645 --                    ctinv.customer_trx_id,
646 --                   g_ae_sys_rec.set_of_books_id))  exchange_rate_type,
647        ctinv.exchange_rate_type                     exchange_rate_type,
648 --}
649 --{BUG4301323
650 --       DECODE(g_ae_sys_rec.sob_type, 'P',
651 --              ctinv.exchange_date,
652 --              arp_mrc_acct_main.get_ctx_exg_date(
653 --                    ctinv.customer_trx_id,
654 --                   g_ae_sys_rec.set_of_books_id))  exchange_date,
655        ctinv.exchange_date                         exchange_date,
656 --}
657        ctinv.trx_date                              trx_date,
658        ctinv.bill_to_customer_id                   bill_to_customer_id,
659        ctinv.bill_to_site_use_id                   bill_to_site_use_id,
660        ctinv.drawee_id                             drawee_id,
661        ctinv.drawee_site_use_id                    drawee_site_use_id
662        from ar_adjustments   adj,
663             ra_customer_trx  ctinv
664        where adj.adjustment_id = g_ae_doc_rec.source_id_old
665        and   adj.status = 'A'
666        and   g_ae_doc_rec.source_table = 'ADJ'
667        and   adj.customer_trx_id = ctinv.customer_trx_id;
668 
669 l_ael_line_rec          ae_line_rec_type;
670 l_ael_empty_line_rec    ae_line_rec_type;
671 i                       BINARY_INTEGER := 1;
672 l_cust_inv_rec          ra_customer_trx%ROWTYPE;
673 l_customer_trx_id       NUMBER;
674 l_amount                NUMBER;
675 l_acctd_amount          NUMBER;
676 --{3377004
677 l_ctl_rem_amt               ctl_rem_amt_type;
678 l_index                     NUMBER := 0;
679 --}
680 
681 BEGIN
682 
683   arp_standard.debug( 'ARP_ADJUSTMENTS_MAIN.Reverse_Adjustments()+');
684 
685 --Reverse all adjustment accounting
686     FOR l_ard_rec in get_old_ard LOOP
687 
688      --Initialize build record source id secondary and table is null for Adjustments
689        l_ael_line_rec := l_ael_empty_line_rec ;
690 
691        l_ael_line_rec.ae_line_type               := l_ard_rec.source_type            ;
692        l_ael_line_rec.ae_line_type_secondary     := l_ard_rec.source_type_secondary  ;
693        l_ael_line_rec.source_id                  := g_ae_doc_rec.source_id           ;
694        l_ael_line_rec.source_table               := g_ae_doc_rec.source_table        ;
695        l_ael_line_rec.source_table_secondary     := l_ard_rec.source_table_secondary ;
696        l_ael_line_rec.source_id_secondary        := l_ard_rec.source_id_secondary    ;
697        l_ael_line_rec.account                    := l_ard_rec.code_combination_id    ;
698 
699      -- For reversals swap debits and credits
700 
701        l_ael_line_rec.entered_cr   := l_ard_rec.amount_dr;
702        l_ael_line_rec.accounted_cr := l_ard_rec.acctd_amount_dr;
703 
704        l_ael_line_rec.entered_dr   := l_ard_rec.amount_cr;
705        l_ael_line_rec.accounted_dr := l_ard_rec.acctd_amount_cr;
706 
707        l_ael_line_rec.taxable_entered_cr    := l_ard_rec.taxable_entered_dr;
708        l_ael_line_rec.taxable_accounted_cr  := l_ard_rec.taxable_accounted_dr;
709 
710        l_ael_line_rec.taxable_entered_dr    := l_ard_rec.taxable_entered_cr;
711        l_ael_line_rec.taxable_accounted_dr  := l_ard_rec.taxable_accounted_cr;
712 
713        l_ael_line_rec.currency_code               := l_ard_rec.currency_code;
714        l_ael_line_rec.currency_conversion_rate    := l_ard_rec.currency_conversion_rate;
715        l_ael_line_rec.currency_conversion_type    := l_ard_rec.currency_conversion_type;
716        l_ael_line_rec.currency_conversion_date    := l_ard_rec.currency_conversion_date;
717        l_ael_line_rec.third_party_id              := l_ard_rec.third_party_id;
718        l_ael_line_rec.third_party_sub_id          := l_ard_rec.third_party_sub_id;
719        l_ael_line_rec.reversed_source_id          := '';
720        l_ael_line_rec.tax_group_code_id           := l_ard_rec.tax_group_code_id;
721        l_ael_line_rec.tax_code_id                 := l_ard_rec.tax_code_id;
722        l_ael_line_rec.location_segment_id         := l_ard_rec.location_segment_id;
723        l_ael_line_rec.tax_link_id                 := l_ard_rec.tax_link_id;
724        --{BUG#2979254
725        l_ael_line_rec.ref_customer_trx_line_id    := l_ard_rec.ref_customer_trx_line_id;
726        l_ael_line_rec.ref_cust_trx_line_gl_dist_id := l_ard_rec.ref_cust_trx_line_gl_dist_id;
727        l_ael_line_rec.ref_line_id                 := l_ard_rec.ref_line_id;
728        --}
729        l_ael_line_rec.ref_account_class		  := l_ard_rec.ref_account_class;
730        l_ael_line_rec.activity_bucket		  := l_ard_rec.activity_bucket;
731        l_ael_line_rec.ref_dist_ccid		  := l_ard_rec.ref_dist_ccid;
732     -- Assign AEL for Reversal
733       Assign_Ael_Elements( p_ae_line_rec        => l_ael_line_rec );
734 
735 
736       --{3377004
737       arp_standard.debug(' l_ard_rec.WHICH_BUCKET:'||l_ard_rec.WHICH_BUCKET);
738       arp_standard.debug(' l_ard_rec.ref_customer_trx_line_id:'||l_ard_rec.ref_customer_trx_line_id);
739       arp_standard.debug(' l_ard_rec.ref_line_id:'||l_ard_rec.ref_line_id);
740       IF    l_ard_rec.WHICH_BUCKET = 'N' THEN
741          NULL;
742       ELSIF l_ard_rec.WHICH_BUCKET = 'ADDCTL' THEN
743 
744         l_index := ctl_id_index(l_ctl_rem_amt.customer_trx_line_id,
745                                 l_ard_rec.ref_customer_trx_line_id);
746 
747         IF NOT l_ctl_rem_amt.amount_due_remaining.EXISTS(l_index) THEN
748           init_rem_amt(x_rem_amt => l_ctl_rem_amt,
749                        p_index   => l_index);
750         END IF;
751 
752         l_ctl_rem_amt.customer_trx_line_id(l_index) := l_ard_rec.ref_customer_trx_line_id;
753 
754         -- Bug 8626971
755 	l_ctl_rem_amt.amount_due_remaining(l_index) :=
756                  NVL(l_ctl_rem_amt.amount_due_remaining(l_index),0)
757            --   + (NVL(l_ard_rec.amount_cr,0) - NVL(l_ard_rec.amount_dr,0));
758 	        + (NVL(l_ael_line_rec.entered_cr,0) - NVL(l_ael_line_rec.entered_dr,0));
759 
760         l_ctl_rem_amt.acctd_amount_due_remaining(l_index) :=
761                NVL(l_ctl_rem_amt.acctd_amount_due_remaining(l_index),0)
762           --  + (NVL(l_ard_rec.acctd_amount_cr,0) - NVL(l_ard_rec.acctd_amount_dr,0));
763               + (NVL(l_ael_line_rec.accounted_cr,0) - NVL(l_ael_line_rec.accounted_dr,0));
764 
765         arp_standard.debug('l_ctl_rem_amt.customer_trx_line_id('||l_index||'):'||
766                                l_ctl_rem_amt.customer_trx_line_id(l_index));
767         arp_standard.debug('l_ctl_rem_amt.amount_due_remaining('||l_index||'):'||
768                                l_ctl_rem_amt.amount_due_remaining(l_index));
769         arp_standard.debug('l_ctl_rem_amt.acctd_amount_due_remaining('||l_index||'):'||
770                                l_ctl_rem_amt.acctd_amount_due_remaining(l_index));
771 
772       ELSIF l_ard_rec.WHICH_BUCKET = 'ADDCHRG' THEN
773 
774         l_index := ctl_id_index(l_ctl_rem_amt.customer_trx_line_id,
775                                 l_ard_rec.ref_customer_trx_line_id);
776 
777         IF NOT l_ctl_rem_amt.chrg_amount_remaining.EXISTS(l_index) THEN
778           init_rem_amt(x_rem_amt => l_ctl_rem_amt,
779                        p_index   => l_index);
780         END IF;
781 
782         l_ctl_rem_amt.customer_trx_line_id(l_index) := l_ard_rec.ref_customer_trx_line_id;
783 
784         l_ctl_rem_amt.chrg_amount_remaining(l_index) :=
785                 NVL(l_ctl_rem_amt.chrg_amount_remaining(l_index),0)
786              + (NVL(l_ard_rec.amount_cr,0) - NVL(l_ard_rec.amount_dr,0));
787 
788         l_ctl_rem_amt.chrg_acctd_amount_remaining(l_index) :=
789               NVL(l_ctl_rem_amt.chrg_acctd_amount_remaining(l_index),0)
790            + (NVL(l_ard_rec.acctd_amount_cr,0) - NVL(l_ard_rec.acctd_amount_dr,0));
791 
792         arp_standard.debug('l_ctl_rem_amt.customer_trx_line_id('||l_index||'):'||
793                                l_ctl_rem_amt.customer_trx_line_id(l_index));
794         arp_standard.debug('l_ctl_rem_amt.chrg_amount_remaining('||l_index||'):'||
795                                l_ctl_rem_amt.chrg_amount_remaining(l_index));
796         arp_standard.debug('l_ctl_rem_amt.chrg_acctd_amount_remaining('||l_index||'):'||
797                                l_ctl_rem_amt.chrg_acctd_amount_remaining(l_index));
798 
799       END IF;
800     --}
801     END LOOP;
802 
803     --{3377004
804     IF l_index <> 0 THEN
805       FORALL m IN l_ctl_rem_amt.customer_trx_line_id.FIRST .. l_ctl_rem_amt.customer_trx_line_id.LAST
806       UPDATE ra_customer_trx_lines
807       SET  AMOUNT_DUE_REMAINING        = AMOUNT_DUE_REMAINING        + l_ctl_rem_amt.amount_due_remaining(m),
808            ACCTD_AMOUNT_DUE_REMAINING  = ACCTD_AMOUNT_DUE_REMAINING  + l_ctl_rem_amt.acctd_amount_due_remaining(m),
809            CHRG_AMOUNT_REMAINING       = CHRG_AMOUNT_REMAINING       + l_ctl_rem_amt.chrg_amount_remaining(m),
810            CHRG_ACCTD_AMOUNT_REMAINING = CHRG_ACCTD_AMOUNT_REMAINING + l_ctl_rem_amt.chrg_acctd_amount_remaining(m)
811       WHERE customer_trx_line_id       = l_ctl_rem_amt.customer_trx_line_id(m);
812     END IF;
813     --}
814 
815  /*-----------------------------------------------------------------------------------+
816   |Call the Reconciliation routine, this is necessary because the transaction or Bill |
817   |which may have been overapplied is now closed due to reversal, or else the reversal|
818   |may have resulted in re-opening the transaction, hence we need to back out NOCOPY the old |
819   |reconciliation entries for the Bill or the Transactions. The reversal of the       |
820   |adjustment accounting must take place to call the Reversal Routine. When called    |
821   |this routine will always attempt do a reversal, first reverse accounting then call |
822   |the reconciliation routine. Note the reversal of Reconciliation entries is left to |
823   |The Reconciliation routine below i.e. we do not reverse them in the above cursor.  |
824   +-----------------------------------------------------------------------------------*/
825     arp_standard.debug('Before call to Reconciliation routine');
826 
827     FOR l_get_adj in get_adj_details LOOP --loop executes once only for adjustment
828 
829        /*-------------------------------------------------------------------------------+
830         | Set currency and exchange rate details to that of the document which has been |
831         | adjusted. These details will get overriden by the transaction line assignment |
832         | exchange rate details for Bill.                                               |
833         +-------------------------------------------------------------------------------*/
834          l_cust_inv_rec.invoice_currency_code := l_get_adj.invoice_currency_code;
835          l_cust_inv_rec.exchange_rate         := l_get_adj.exchange_rate;
836          l_cust_inv_rec.exchange_rate_type    := l_get_adj.exchange_rate_type;
837          l_cust_inv_rec.exchange_date         := l_get_adj.exchange_date;
838          l_cust_inv_rec.trx_date              := l_get_adj.trx_date;
839          l_cust_inv_rec.bill_to_customer_id   := l_get_adj.bill_to_customer_id;
840          l_cust_inv_rec.bill_to_site_use_id   := l_get_adj.bill_to_site_use_id;
841          l_cust_inv_rec.drawee_id             := l_get_adj.drawee_id;
842          l_cust_inv_rec.drawee_site_use_id    := l_get_adj.drawee_site_use_id;
843 
844        --Required to determine whether the payment schedule is closed or not
845        --emulate sign of Receipt application
846          l_customer_trx_id           := l_get_adj.customer_trx_id      ;
847          l_amount                    := l_get_adj.amount               ;
848          l_acctd_amount              := l_get_adj.acctd_amount         ;
849 
850          ARP_RECONCILE.Reconcile_trx_br(
851            p_mode                     => g_mode                             ,
852            p_ae_doc_rec               => g_ae_doc_rec                       ,
853            p_ae_event_rec             => g_ae_event_rec                     ,
854            p_cust_inv_rec             => l_cust_inv_rec                     ,
855            p_activity_cust_trx_id     => l_customer_trx_id                  ,
856            p_activity_amt             => l_amount                           ,
857            p_activity_acctd_amt       => l_acctd_amount                     ,
858            p_call_num                 => 1                                  ,
859            p_g_ae_line_tbl            => g_ae_line_tbl                      ,
860            p_g_ae_ctr                 => g_ae_line_ctr                        );
861 
862         END LOOP; --get adjustment details
863 
864   arp_standard.debug( 'ARP_ADJUSTMENTS_MAIN.Reverse_Adjustments()-');
865 
866 EXCEPTION
867   WHEN NO_DATA_FOUND THEN
868      arp_standard.debug('ARP_ADJUSTMENTS_MAIN.Reverse_Adjustments - NO_DATA_FOUND' );
869      RAISE;
870 
871   WHEN OTHERS THEN
872      arp_standard.debug('EXCEPTION: ARP_ADJUSTMENTS_MAIN.Reverse_Adjustments');
873      RAISE ;
874 
875 END Reverse_Adjustment;
876 
877 
878 /* =======================================================================
879  |
880  | PROCEDURE Get_Doc_Entitity_Data
881  |
882  | DESCRIPTION
883  |      This procedure gets the necessary transaction data for each entity
884  |      level.
885  |
886  | PARAMETERS
887  | 	p_level          Entitity Level
888  |      p_adj_rec        Adjustment Record
889  |      p_cust_inv_rec   Invoice document or On Account Credit Memo data
890  |      p_ctlgd_inv_rec  Receivable account for Adjustment
891  |      p_rule_rec       Rule record
892  |      p_ard_rec        Distributions record used by Bills Receivable
893  * ======================================================================*/
894 PROCEDURE Get_Doc_Entitity_Data (
895                 p_level          IN VARCHAR2                            ,
896                 p_adj_rec        OUT NOCOPY ar_adjustments%ROWTYPE             ,
897                 p_cust_inv_rec   OUT NOCOPY ra_customer_trx%ROWTYPE            ,
898                 p_ctlgd_inv_rec  OUT NOCOPY ra_cust_trx_line_gl_dist%ROWTYPE   ,
899                 p_rule_rec       OUT NOCOPY ae_rule_rec_type                   ,
900                 p_ard_rec        OUT NOCOPY ar_distributions%ROWTYPE               ) IS
901 
902   l_tax_rate_id NUMBER;
903   l_le_id           NUMBER;
904   l_msg_count       NUMBER;
905   l_msg_data        VARCHAR2(1024);
906   l_effective_date  DATE;
907   l_return_status   VARCHAR2(10);
908 
909 BEGIN
910 
911    IF PG_DEBUG in ('Y', 'C') THEN
912       arp_standard.debug('ARP_ADJUSTMENTS_MAIN.Get_Doc_Entitity_Data()+');
913    END IF;
914 
915    IF p_level = 'ADJ' then
916 
917       /* receivables activities can now be set up for either org
918          or legal entity.  When set up by legal entity, there is a
919          new child table that carries the asset_tax_code for each
920          activity/LE pair. */
921 
922       IF arp_legal_entity_util.is_le_subscriber
923       THEN
924          /* LE setup is enabled */
925          /* 5236782 - but ar_rec_trx_le_details not required for adj */
926       select adj.adjustment_id                               ,
927              adj.customer_trx_id                             ,
928              adj.customer_trx_line_id                        ,
929              adj.payment_schedule_id                         ,
930              adj.receivables_trx_id                          ,
931              adj.code_combination_id                         ,
932              adj.apply_date                                  ,
933              adj.gl_date                                     ,
934              adj.type                                        ,
935              adj.status                                      ,
936              adj.amount                                      ,
937              adj.acctd_amount                                ,
938              adj.line_adjusted                               ,
939              adj.freight_adjusted                            ,
940              adj.tax_adjusted                                ,
941              adj.receivables_charges_adjusted                ,
942              ctinv.invoice_currency_code                     ,
943              ctinv.exchange_rate           exchange_rate,
944              ctinv.exchange_rate_type      exchange_rate_type,
945              ctinv.exchange_date             exchange_date,
946              ctinv.bill_to_customer_id                       ,
947              ctinv.bill_to_site_use_id                       ,
948              ctinv.drawee_id                                 ,
949              ctinv.drawee_site_use_id                        ,
950 	     ctinv.upgrade_method                            ,
951              ctlgdinv.code_combination_id                    ,
952              decode(g_ae_doc_rec.other_flag,
953                     'CBREVERSAL', 'ACTIVITY_GL_ACCOUNT',        --trx id -12
954                     'CHARGEBACK', 'ACTIVITY_GL_ACCOUNT',        --trx id -11
955                     'COMMITMENT', 'ACTIVITY_GL_ACCOUNT',        --trx od -1
956                     nvl(rt.gl_account_source, 'NO_SOURCE'))  ,
957              decode(g_ae_doc_rec.other_flag,
958                     'CBREVERSAL', 'NONE',                      --trx id -12
959                     'CHARGEBACK', 'NONE',                      --trx id -11
960                     'COMMITMENT', 'NONE',                      --trx id -1
961                     nvl(rt.tax_code_source  , 'NO_SOURCE'))  ,
962              decode(g_ae_doc_rec.other_flag,
963                     'CBREVERSAL', '',
964                     'CHARGEBACK', '',
965                     'COMMITMENT', '',
966                     rt.tax_recoverable_flag)                 ,
967              decode(g_ae_doc_rec.other_flag,
968                   'CBREVERSAL',g_ae_doc_rec.source_id_old, --chargeback reversal
969                   'CHARGEBACK',g_ae_doc_rec.source_id_old, --chargeback
970                   'COMMITMENT',g_ae_doc_rec.source_id_old, --commitments
971                   'OVERRIDE'  ,g_ae_doc_rec.source_id_old, --when user specifies account
972                     rt.code_combination_id)                  ,  --in adjustment form
973              nvl(rtd.asset_tax_code, rt.asset_tax_code)      ,
974              nvl(rtd.liability_tax_code, rt.liability_tax_code),
975              ''                                              ,
976              ''                                              ,
977              'NO_SOURCE'                                     ,
978              'NO_SOURCE'                                     ,
979              ''                                              ,
980              ''                                              ,
981              ''                                              ,
982              ''                                              ,
983              ''                                              ,
984              ''
985       into   p_adj_rec.adjustment_id                       ,
986              p_adj_rec.customer_trx_id                     ,
987              p_adj_rec.customer_trx_line_id                ,
988              p_adj_rec.payment_schedule_id                 ,
989              p_adj_rec.receivables_trx_id                  ,
990              p_adj_rec.code_combination_id                 ,
991              p_adj_rec.apply_date                          ,
992              p_adj_rec.gl_date                             ,
993              p_adj_rec.type                                ,
994              p_adj_rec.status                              ,
995              p_adj_rec.amount                              ,
996              p_adj_rec.acctd_amount                        ,
997              p_adj_rec.line_adjusted                       ,
998              p_adj_rec.freight_adjusted                    ,
999              p_adj_rec.tax_adjusted                        ,
1000              p_adj_rec.receivables_charges_adjusted        ,
1001              p_cust_inv_rec.invoice_currency_code          ,
1002              p_cust_inv_rec.exchange_rate                  ,
1003              p_cust_inv_rec.exchange_rate_type             ,
1004              p_cust_inv_rec.exchange_date                  ,
1005              p_cust_inv_rec.bill_to_customer_id            ,
1006              p_cust_inv_rec.bill_to_site_use_id            ,
1007              p_cust_inv_rec.drawee_id                      ,
1008              p_cust_inv_rec.drawee_site_use_id             ,
1009 	     p_cust_inv_rec.upgrade_method                 ,
1010              p_ctlgd_inv_rec.code_combination_id           ,
1011              p_rule_rec.gl_account_source1                 ,
1012              p_rule_rec.tax_code_source1                   ,
1013              p_rule_rec.tax_recoverable_flag1              ,
1014              p_rule_rec.code_combination_id1               ,
1015              p_rule_rec.asset_tax_code1                    ,
1016              p_rule_rec.liability_tax_code1                ,
1017              p_rule_rec.act_tax_non_rec_ccid1              ,
1018              p_rule_rec.act_vat_tax_id1                    ,
1019              p_rule_rec.gl_account_source2                 ,
1020              p_rule_rec.tax_code_source2                   ,
1021              p_rule_rec.tax_recoverable_flag2              ,
1022              p_rule_rec.code_combination_id2               ,
1023              p_rule_rec.asset_tax_code2                    ,
1024              p_rule_rec.liability_tax_code2                ,
1025              p_rule_rec.act_tax_non_rec_ccid2              ,
1026              p_rule_rec.act_vat_tax_id2
1027       from ar_adjustments             adj     ,
1028            ar_receivables_trx         rt      ,
1029            ar_rec_trx_le_details      rtd     ,
1030            ra_customer_trx            ctinv   ,
1031            ra_cust_trx_line_gl_dist   ctlgdinv
1032       where adj.adjustment_id = g_ae_doc_rec.source_id
1033       and   adj.status = 'A'                        --only approved adjustments
1034       and   adj.receivables_trx_id = rt.receivables_trx_id
1035       and   rtd.receivables_trx_id (+) = rt.receivables_trx_id
1036       and   nvl(rtd.legal_entity_id, ctinv.legal_entity_id)
1037                    = ctinv.legal_entity_id
1038       and   adj.customer_trx_id = ctinv.customer_trx_id  --INV REC exchange rate Information
1039       and   adj.customer_trx_id = ctlgdinv.customer_trx_id (+) --REC account ccid
1040       and   'REC' = ctlgdinv.account_class (+)
1041       and   'Y' = ctlgdinv.latest_rec_flag (+) ;
1042 
1043       ELSE
1044          /* Setup is by OU, don't need child table */
1045 
1046       select adj.adjustment_id                               ,
1047              adj.customer_trx_id                             ,
1048              adj.customer_trx_line_id                        ,
1049              adj.payment_schedule_id                         ,
1050              adj.receivables_trx_id                          ,
1051              adj.code_combination_id                         ,
1052              adj.apply_date                                  ,
1053              adj.gl_date                                     ,
1054              adj.type                                        ,
1055              adj.status                                      ,
1056              adj.amount                                      ,
1057              adj.acctd_amount                                ,
1058              adj.line_adjusted                               ,
1059              adj.freight_adjusted                            ,
1060              adj.tax_adjusted                                ,
1061              adj.receivables_charges_adjusted                ,
1062              ctinv.invoice_currency_code                     ,
1063              ctinv.exchange_rate           exchange_rate,
1064              ctinv.exchange_rate_type      exchange_rate_type,
1065              ctinv.exchange_date             exchange_date,
1066              ctinv.bill_to_customer_id                       ,
1067              ctinv.bill_to_site_use_id                       ,
1068              ctinv.drawee_id                                 ,
1069              ctinv.drawee_site_use_id                        ,
1070 	     ctinv.upgrade_method                            ,
1071              ctlgdinv.code_combination_id                    ,
1072              decode(g_ae_doc_rec.other_flag,
1073                     'CBREVERSAL', 'ACTIVITY_GL_ACCOUNT',        --trx id -12
1074                     'CHARGEBACK', 'ACTIVITY_GL_ACCOUNT',        --trx id -11
1075                     'COMMITMENT', 'ACTIVITY_GL_ACCOUNT',        --trx od -1
1076                     nvl(rt.gl_account_source, 'NO_SOURCE'))  ,
1077              decode(g_ae_doc_rec.other_flag,
1078                     'CBREVERSAL', 'NONE',                      --trx id -12
1079                     'CHARGEBACK', 'NONE',                      --trx id -11
1080                     'COMMITMENT', 'NONE',                      --trx id -1
1081                     nvl(rt.tax_code_source  , 'NO_SOURCE'))  ,
1082              decode(g_ae_doc_rec.other_flag,
1083                     'CBREVERSAL', '',
1084                     'CHARGEBACK', '',
1085                     'COMMITMENT', '',
1086                     rt.tax_recoverable_flag)                 ,
1087              decode(g_ae_doc_rec.other_flag,
1088                   'CBREVERSAL',g_ae_doc_rec.source_id_old, --chargeback reversal
1089                   'CHARGEBACK',g_ae_doc_rec.source_id_old, --chargeback
1090                   'COMMITMENT',g_ae_doc_rec.source_id_old, --commitments
1091                   'OVERRIDE'  ,g_ae_doc_rec.source_id_old, --when user specifies account
1092                     rt.code_combination_id)                  ,  --in adjustment form
1093              rt.asset_tax_code                               ,
1094              rt.liability_tax_code                           ,
1095              ''                                              ,
1096              ''                                              ,
1097              'NO_SOURCE'                                     ,
1098              'NO_SOURCE'                                     ,
1099              ''                                              ,
1100              ''                                              ,
1101              ''                                              ,
1102              ''                                              ,
1103              ''                                              ,
1104              ''
1105       into   p_adj_rec.adjustment_id                       ,
1106              p_adj_rec.customer_trx_id                     ,
1107              p_adj_rec.customer_trx_line_id                ,
1108              p_adj_rec.payment_schedule_id                 ,
1109              p_adj_rec.receivables_trx_id                  ,
1110              p_adj_rec.code_combination_id                 ,
1111              p_adj_rec.apply_date                          ,
1112              p_adj_rec.gl_date                             ,
1113              p_adj_rec.type                                ,
1114              p_adj_rec.status                              ,
1115              p_adj_rec.amount                              ,
1116              p_adj_rec.acctd_amount                        ,
1117              p_adj_rec.line_adjusted                       ,
1118              p_adj_rec.freight_adjusted                    ,
1119              p_adj_rec.tax_adjusted                        ,
1120              p_adj_rec.receivables_charges_adjusted        ,
1121              p_cust_inv_rec.invoice_currency_code          ,
1122              p_cust_inv_rec.exchange_rate                  ,
1123              p_cust_inv_rec.exchange_rate_type             ,
1124              p_cust_inv_rec.exchange_date                  ,
1125              p_cust_inv_rec.bill_to_customer_id            ,
1126              p_cust_inv_rec.bill_to_site_use_id            ,
1127              p_cust_inv_rec.drawee_id                      ,
1128              p_cust_inv_rec.drawee_site_use_id             ,
1129 	     p_cust_inv_rec.upgrade_method                 ,
1130              p_ctlgd_inv_rec.code_combination_id           ,
1131              p_rule_rec.gl_account_source1                 ,
1132              p_rule_rec.tax_code_source1                   ,
1133              p_rule_rec.tax_recoverable_flag1              ,
1134              p_rule_rec.code_combination_id1               ,
1135              p_rule_rec.asset_tax_code1                    ,
1136              p_rule_rec.liability_tax_code1                ,
1137              p_rule_rec.act_tax_non_rec_ccid1              ,
1138              p_rule_rec.act_vat_tax_id1                    ,
1139              p_rule_rec.gl_account_source2                 ,
1140              p_rule_rec.tax_code_source2                   ,
1141              p_rule_rec.tax_recoverable_flag2              ,
1142              p_rule_rec.code_combination_id2               ,
1143              p_rule_rec.asset_tax_code2                    ,
1144              p_rule_rec.liability_tax_code2                ,
1145              p_rule_rec.act_tax_non_rec_ccid2              ,
1146              p_rule_rec.act_vat_tax_id2
1147       from ar_adjustments             adj     ,
1148            ar_receivables_trx         rt      ,
1149            ra_customer_trx            ctinv   ,
1150            ra_cust_trx_line_gl_dist   ctlgdinv
1151       where adj.adjustment_id = g_ae_doc_rec.source_id
1152       and   adj.status = 'A'                        --only approved adjustments
1153       and   adj.receivables_trx_id = rt.receivables_trx_id
1154       and   adj.customer_trx_id = ctinv.customer_trx_id  --INV REC exchange rate Information
1155       and   adj.customer_trx_id = ctlgdinv.customer_trx_id (+) --REC account ccid
1156       and   'REC' = ctlgdinv.account_class (+)
1157       and   'Y' = ctlgdinv.latest_rec_flag (+) ;
1158 
1159       END IF;
1160 
1161   /*----------------------------------------------------------+
1162    | Process for endorsements for Bills Receivable, get the   |
1163    | Receivable account to offset the Write off account,      |
1164    | based on the sign of the payment schedule, the Unpaid    |
1165    | Bills receivable, or Bills Receivable account is selected|
1166    +----------------------------------------------------------*/
1167       IF (p_cust_inv_rec.drawee_site_use_id IS NOT null) THEN
1168 
1169          -- MRC Trigger Replacment.  Enumerated columns.  Branched based
1170          -- on primary or  Reporting.
1171           IF ( g_ae_sys_rec.sob_type = 'P') THEN
1172             select ard.line_id,
1173               ard.source_id,
1174               ard.source_table,
1175               ard.source_type,
1176               ard.code_combination_id,
1177               ard.amount_dr,
1178               ard.amount_cr,
1179               ard.acctd_amount_dr,
1180               ard.acctd_amount_cr,
1181               ard.creation_date,
1182               ard.created_by,
1183               ard.last_updated_by,
1184               ard.last_update_date,
1185               ard.last_update_login,
1186               ard.org_id,
1187               ard.source_table_secondary,
1188               ard.source_id_secondary,
1189               ard.currency_code,
1190               ard.currency_conversion_rate,
1191               ard.currency_conversion_type,
1192               ard.currency_conversion_date,
1193               ard.taxable_entered_dr,
1194               ard.taxable_entered_cr,
1195               ard.taxable_accounted_dr,
1196               ard.taxable_accounted_cr,
1197               ard.tax_link_id,
1198               ard.third_party_id,
1199               ard.third_party_sub_id,
1200               ard.reversed_source_id,
1201               ard.tax_code_id,
1202               ard.location_segment_id,
1203               ard.source_type_secondary,
1204               ard.tax_group_code_id,
1205               --{BUG#2979254
1206               ard.ref_customer_trx_line_id,
1207               ard.ref_cust_trx_line_gl_dist_id,
1208               ard.ref_line_id
1209               --}
1210          INTO p_ard_rec.line_id,
1211               p_ard_rec.source_id,
1212               p_ard_rec.source_table,
1213               p_ard_rec.source_type,
1214               p_ard_rec.code_combination_id,
1215               p_ard_rec.amount_dr,
1216               p_ard_rec.amount_cr,
1217               p_ard_rec.acctd_amount_dr,
1218               p_ard_rec.acctd_amount_cr,
1219               p_ard_rec.creation_date,
1220               p_ard_rec.created_by,
1221               p_ard_rec.last_updated_by,
1222               p_ard_rec.last_update_date,
1223               p_ard_rec.last_update_login,
1224               p_ard_rec.org_id,
1225               p_ard_rec.source_table_secondary,
1226               p_ard_rec.source_id_secondary,
1227               p_ard_rec.currency_code,
1228               p_ard_rec.currency_conversion_rate,
1229               p_ard_rec.currency_conversion_type,
1230               p_ard_rec.currency_conversion_date,
1231               p_ard_rec.taxable_entered_dr,
1232               p_ard_rec.taxable_entered_cr,
1233               p_ard_rec.taxable_accounted_dr,
1234               p_ard_rec.taxable_accounted_cr,
1235               p_ard_rec.tax_link_id,
1236               p_ard_rec.third_party_id,
1237               p_ard_rec.third_party_sub_id,
1238               p_ard_rec.reversed_source_id,
1239               p_ard_rec.tax_code_id,
1240               p_ard_rec.location_segment_id,
1241               p_ard_rec.source_type_secondary,
1242               p_ard_rec.tax_group_code_id,
1243               --{BUG#2979254
1244               p_ard_rec.ref_customer_trx_line_id,
1245               p_ard_rec.ref_cust_trx_line_gl_dist_id,
1246               p_ard_rec.ref_line_id
1247               --}
1248             from   ar_transaction_history th,
1249                    ar_payment_schedules pay,
1250                    ar_distributions ard
1251            where
1252                  th.transaction_history_id =
1253                      (select max(th1.transaction_history_id)
1254                         from ar_transaction_history th1
1255                        where nvl(th1.POSTABLE_FLAG, 'N') = 'Y'
1256                          and   th1.status IN ('UNPAID', 'PENDING_REMITTANCE')
1257                          and   th1.customer_trx_id = p_adj_rec.customer_trx_id)
1258              and   th.customer_trx_id = pay.customer_trx_id
1259              and   ard.source_id = th.transaction_history_id
1260              and   ard.source_table = 'TH'
1261 	     AND   ard.source_id_secondary is null
1262 	     AND   ard.source_table_secondary is null
1263 	     AND   ard.source_type_secondary is null
1264              and   (((sign(pay.amount_due_original) > 0)
1265                       and ((nvl(ard.AMOUNT_DR,0) <> 0) OR
1266                            (nvl(ard.ACCTD_AMOUNT_DR,0) <> 0))
1267                       and (nvl(ard.AMOUNT_CR,0) = 0) and
1268                           (nvl(ard.ACCTD_AMOUNT_CR,0) = 0))
1269                     OR ((sign(pay.amount_due_original) < 0)
1270                          and ((nvl(ard.AMOUNT_CR,0) <> 0) OR
1271                               (nvl(ard.ACCTD_AMOUNT_CR,0) <> 0))
1272                          and (nvl(ard.AMOUNT_DR,0) = 0) and
1273                               (nvl(ard.ACCTD_AMOUNT_DR,0) = 0)));
1274          END IF;
1275 
1276          p_ctlgd_inv_rec.code_combination_id := p_ard_rec.code_combination_id;
1277 
1278       END IF;
1279 
1280   /*--------------------------------------------------------+
1281    | Get non recoverable tax account for asset or liability |
1282    | tax code for finance charges                           |
1283    +--------------------------------------------------------*/
1284       IF (p_rule_rec.tax_code_source1 = 'ACTIVITY')
1285             AND (p_rule_rec.asset_tax_code1 IS NOT NULL)
1286       THEN
1287 
1288         /* Initialize zx */
1289         zx_api_pub.set_tax_security_context(
1290            p_api_version      => 1.0,
1291            p_init_msg_list    => 'T',
1292            p_commit           => 'F',
1293            p_validation_level => NULL,
1294            x_return_status    => l_return_status,
1295            x_msg_count        => l_msg_count,
1296            x_msg_data         => l_msg_data,
1297            p_internal_org_id  => arp_standard.sysparm.org_id,
1298            p_legal_entity_id  => l_le_id,
1299            p_transaction_date => p_adj_rec.apply_date,
1300            p_related_doc_date => NULL,
1301            p_adjusted_doc_date=> NULL,
1302            x_effective_date   => l_effective_date);
1303 
1304         BEGIN
1305            /* Get tax_rate_id from zx_rates based on tax_rate_code (asset_tax_code)
1306               and then call function to get underlying accounts */
1307          SELECT tax_rate_id
1308          INTO   l_tax_rate_id
1309          FROM   zx_sco_rates
1310          WHERE  tax_rate_code = p_rule_rec.asset_tax_code1
1311          AND    p_adj_rec.apply_date BETWEEN
1312                    NVL(effective_from, p_adj_rec.apply_date) AND
1313                    NVL(effective_to, p_adj_rec.apply_date);
1314 
1315          p_rule_rec.act_tax_non_rec_ccid1 :=
1316               arp_etax_util.get_tax_account(l_tax_rate_id,
1317                                             p_adj_rec.apply_date,
1318                                             'ADJ_NON_REC',
1319                                             'TAX_RATE');
1320 
1321          p_rule_rec.act_vat_tax_id1 :=
1322               arp_etax_util.get_tax_account(l_tax_rate_id,
1323                                             p_adj_rec.apply_date,
1324                                             'FINCHRG_NON_REC',
1325                                             'TAX_RATE');
1326 
1327         EXCEPTION
1328           WHEN NO_DATA_FOUND THEN
1329              IF PG_DEBUG in ('Y', 'C') THEN
1330                 arp_standard.debug('ARP_ADJUSTMENTS_MAIN.Get_Doc_Entitity_Data - ' ||
1331                                      'ACT_TAX_NON_REC_CCID - '|| 'NO_DATA_FOUND' );
1332              END IF;
1333              NULL;
1334 
1335           WHEN OTHERS THEN
1336              IF PG_DEBUG in ('Y', 'C') THEN
1337                 arp_standard.debug('EXCEPTION: ARP_ADJUSTMENTS_MAIN.'||
1338                                      'Get_Doc_Entitity_Data - ACT_TAX_NON_REC_CCID ');
1339              END IF;
1340              RAISE ;
1341         END;
1342 
1343        END IF; --end if tax code source is activity
1344 
1345    END IF; --end if p_level = ADJ
1346 
1347    IF PG_DEBUG in ('Y', 'C') THEN
1348       arp_standard.debug('ARP_ADJUSTMENTS_MAIN.Get_Doc_Entitity_Data()-');
1349    END IF;
1350 
1351 EXCEPTION
1352   WHEN NO_DATA_FOUND THEN
1353      IF PG_DEBUG in ('Y', 'C') THEN
1354         arp_standard.debug('ARP_ADJUSTMENTS_MAIN.Get_Doc_Entitity_Data - NO_DATA_FOUND' );
1355      END IF;
1356      RAISE;
1357 
1358   WHEN OTHERS THEN
1359      IF PG_DEBUG in ('Y', 'C') THEN
1360         arp_standard.debug('EXCEPTION: ARP_ADJUSTMENTS_MAIN.Get_Doc_Entitity_Data');
1361      END IF;
1362      RAISE ;
1363 
1364 END Get_Doc_Entitity_Data;
1365 
1366 /* =======================================================================
1367  | PROCEDURE Create_Ae_Lines_Common
1368  |
1369  | DESCRIPTION
1370  |      This procedure creates the AE lines at each entity level. Used
1371  |      for creating lines as part of Adjustment creation.
1372  |
1373  |      Functions:
1374  |      	- Create AE lines.
1375  |      	- Get additional data to determine the type of AE lines
1376  |
1377  | PARAMETERS
1378  |      p_level       Entity level from which the procedure was called
1379  * ======================================================================*/
1380 PROCEDURE Create_Ae_Lines_Common (
1381 		p_level 	      IN VARCHAR2,
1382 		p_from_llca_call      IN  VARCHAR2 DEFAULT 'N',
1383 		p_gt_id               IN  NUMBER   DEFAULT NULL) IS
1384 
1385   l_adj_rec		ar_adjustments%ROWTYPE ;
1386   l_cust_inv_rec        ra_customer_trx%ROWTYPE            ;
1387   l_ctlgd_inv_rec       ra_cust_trx_line_gl_dist%ROWTYPE   ;
1388   l_rule_rec            ae_rule_rec_type                   ;
1389   l_ard_rec             ar_distributions%ROWTYPE           ;
1390   l_tmp_upg_method      ra_customer_trx.upgrade_method%TYPE;
1391 
1392 BEGIN
1393 
1394    IF PG_DEBUG in ('Y', 'C') THEN
1395       arp_standard.debug( 'ARP_ADJUSTMENTS_MAIN.Create_Ae_Lines_Common()+');
1396    END IF;
1397 
1398    /*-------------------------------------------------------------+
1399     | Get Document Entitity specific data                         |
1400     +-------------------------------------------------------------*/
1401 
1402      Get_Doc_Entitity_Data(p_level          => p_level         ,
1403                            p_adj_rec        => l_adj_rec       ,
1404                            p_cust_inv_rec   => l_cust_inv_rec  ,
1405                            p_ctlgd_inv_rec  => l_ctlgd_inv_rec ,
1406                            p_rule_rec       => l_rule_rec      ,
1407                            p_ard_rec        => l_ard_rec         );
1408 
1409     /**If Detailed distributions is disabled for the current operating unit then
1410        we will stamp the invoice with upgrade_method as R12_MERGE*/
1411     ARP_DET_DIST_PKG.verify_stamp_merge_dist_method
1412 	  ( l_adj_rec.customer_trx_id,
1413 	    l_tmp_upg_method );
1414 
1415     /**Above proc call will update the invoice header record to database.As l_cust_inv_rec
1416        is fetched from db prior to the update,manually setting it with new value */
1417     IF nvl(l_tmp_upg_method,'R12_NLB') = 'R12_MERGE'  THEN
1418       l_cust_inv_rec.upgrade_method := l_tmp_upg_method;
1419     END IF;
1420 
1421   /*------------------------------------------------------+
1422    | Create AE Lines for Adjustment                       |
1423    +------------------------------------------------------*/
1424    IF p_level = 'ADJ' THEN	-- Entity level = ar_adjustments
1425 
1426   	/*------------------------------------------------------+
1427    	 | Create AE Lines for Receivables                      |
1428    	 +------------------------------------------------------*/
1429   	  -- added parameter for Line Level Adjustment
1430 	   Create_Ae_Lines_ADJ(p_adj_rec        => l_adj_rec         ,
1431                                p_cust_inv_rec   => l_cust_inv_rec    ,
1432                                p_ctlgd_inv_rec  => l_ctlgd_inv_rec   ,
1433                                p_rule_rec       => l_rule_rec        ,
1434                                p_ard_rec        => l_ard_rec         ,
1435 			       p_from_llca_call => p_from_llca_call  ,
1436                                p_gt_id          => p_gt_id);
1437 
1438    END IF;
1439 
1440   IF PG_DEBUG in ('Y', 'C') THEN
1441      arp_standard.debug( 'ARP_ADJUSTMENTS_MAIN.Create_Ae_Lines_Common()-');
1442   END IF;
1443 
1444 EXCEPTION
1445   WHEN OTHERS THEN
1446      IF PG_DEBUG in ('Y', 'C') THEN
1447         arp_standard.debug('EXCEPTION: ARP_ADJUSTMENTS_MAIN.Create_Ae_Lines_Common');
1448      END IF;
1449      RAISE;
1450 
1451 END Create_Ae_Lines_Common;
1452 
1453 /* =======================================================================
1454  | PROCEDURE Create_Ae_Lines_ADJ
1455  |
1456  | DESCRIPTION
1457  |      This procedure creates the AE lines for Receivables
1458  |
1459  |      Functions:
1460  |              - Create AE lines for Receivable .
1461  |              - Determines Amounts Dr/Cr.
1462  |
1463  | PARAMETERS
1464  |      p_adj_rec       Adjustments Record
1465  |      p_cust_inv_rec  Invoice header Record
1466  |      p_ctlgd_inv_rec  REC account ccid record
1467  |      p_rule_rec      Rule record
1468  |      p_ard_rec       Distributions record used by Bills Receivable
1469  * ======================================================================*/
1470 PROCEDURE Create_Ae_Lines_ADJ(
1471 		p_adj_rec 	 IN ar_adjustments%ROWTYPE                             ,
1472                 p_cust_inv_rec   IN ra_customer_trx%ROWTYPE                            ,
1473                 p_ctlgd_inv_rec  IN ra_cust_trx_line_gl_dist%ROWTYPE                   ,
1474                 p_rule_rec       IN ae_rule_rec_type                                   ,
1475                 p_ard_rec        IN ar_distributions%ROWTYPE                           ,
1476 		p_from_llca_call      IN  VARCHAR2 DEFAULT 'N'                         ,
1477 		p_gt_id               IN  NUMBER   DEFAULT NULL) IS
1478     l_subs_bal_seg        VARCHAR2(1);  /*7125756*/
1479   l_code_combination_id ar_adjustments.code_combination_id%TYPE; /*7125756*/
1480   l_ael_line_rec	ae_line_rec_type;
1481   l_empty_ael_line_rec	ae_line_rec_type;
1482   l_app_rec             ar_receivable_applications%ROWTYPE;
1483   l_ae_line_tbl         ae_line_tbl_type;
1484   l_ae_ctr              BINARY_INTEGER := 0;
1485   l_ctr                 BINARY_INTEGER;
1486   l_actual_ccid         ar_adjustments.code_combination_id%TYPE := null;
1487 
1488 BEGIN
1489   IF PG_DEBUG in ('Y', 'C') THEN
1490      arp_standard.debug( 'Create_Ae_Lines_ADJ.Create_Ae_Lines_ADJ()+');
1491   END IF;
1492 
1493  /*-------------------------------------------------------------+
1494   | Get exchange rate and third part info from Invoice adjusted |
1495   | for building receivable REC                                 |
1496   +-------------------------------------------------------------*/
1497    l_ael_line_rec.source_id                := p_adj_rec.adjustment_id;
1498    l_ael_line_rec.source_table             := 'ADJ';
1499 
1500  /*-------------------------------------------------------------+
1501   | Set the source tyoe to the previous accounting record which |
1502   | needs to be offset for the Bills Receivable. This could be  |
1503   | the Bills Receivable or Unpaid Bills Receivable account.    |
1504   +-------------------------------------------------------------*/
1505    IF p_cust_inv_rec.drawee_site_use_id IS NOT NULL THEN
1506       l_ael_line_rec.ae_line_type             := p_ard_rec.source_type;
1507    ELSE
1508       l_ael_line_rec.ae_line_type             := 'REC';
1509    END IF;
1510 
1511    l_ael_line_rec.ae_line_type_secondary   := '';
1512    l_ael_line_rec.account                  := p_ctlgd_inv_rec.code_combination_id; --ccid for REC
1513    l_ael_line_rec.currency_code            := p_cust_inv_rec.invoice_currency_code;
1514    l_ael_line_rec.currency_conversion_rate := p_cust_inv_rec.exchange_rate;
1515    l_ael_line_rec.currency_conversion_type := p_cust_inv_rec.exchange_rate_type;
1516    l_ael_line_rec.currency_conversion_date := p_cust_inv_rec.exchange_date;
1517 
1518    IF p_cust_inv_rec.drawee_site_use_id IS NOT NULL THEN --if Bill
1519       l_ael_line_rec.third_party_id           := p_cust_inv_rec.drawee_id;
1520       l_ael_line_rec.third_party_sub_id       := p_cust_inv_rec.drawee_site_use_id;
1521    ELSE
1522       l_ael_line_rec.third_party_id           := p_cust_inv_rec.bill_to_customer_id;
1523       l_ael_line_rec.third_party_sub_id       := p_cust_inv_rec.bill_to_site_use_id;
1524    END IF; --Bill
1525 
1526  /*--------------------------------------------------------------------------------+
1527   | Derive Receivable amounts, for Adjustments.                                    |
1528   +--------------------------------------------------------------------------------*/
1529 
1530    IF ( sign( p_adj_rec.amount ) = -1 ) THEN   -- Credit Receivables for INV
1531 
1532        l_ael_line_rec.entered_cr   := abs(p_adj_rec.amount);
1533        l_ael_line_rec.entered_dr   := NULL;
1534 
1535        l_ael_line_rec.accounted_cr := abs(p_adj_rec.acctd_amount);
1536        l_ael_line_rec.accounted_dr := NULL;
1537 
1538    ELSE   -- Debit Receivables for INV, if amount adjusted is 0 then accounting record created
1539 
1540        l_ael_line_rec.entered_dr   := p_adj_rec.amount;
1541        l_ael_line_rec.entered_cr   := NULL;
1542 
1543        l_ael_line_rec.accounted_dr := p_adj_rec.acctd_amount;
1544        l_ael_line_rec.accounted_cr := NULL;
1545 
1546    END IF;
1547 
1548  /*-------------------------------------------------------------+
1549   | Assign AEL for REC for Invoice document for adjustment      |
1550   +-------------------------------------------------------------*/
1551    Assign_Ael_Elements( p_ae_line_rec 	=> l_ael_line_rec );
1552 
1553  /*---------------------------------------------------------------------------+
1554   | Call tax accounting routine for Adjustments if amount adjusted is non zero|
1555   +---------------------------------------------------------------------------*/
1556 
1557     IF (nvl(p_adj_rec.amount,0) <> 0) THEN
1558 
1559        l_ae_line_tbl := g_empty_ae_line_tbl;
1560        l_ae_ctr      := 0;
1561 
1562    /*---------------------------------------------------------------------------+
1563     | Verify whether invalid rule setup has occurred at the Receivable Activity |
1564     | in this case raise an error stating that the rule be set up correctly.    |
1565     +---------------------------------------------------------------------------*/
1566        IF (((p_rule_rec.gl_account_source1 = 'NO_SOURCE') OR (p_rule_rec.tax_code_source1 = 'NO_SOURCE'))
1567             OR ((p_rule_rec.tax_code_source1 = 'INVOICE')
1568                      AND (nvl(p_rule_rec.tax_recoverable_flag1, 'X') NOT IN ('Y','N'))))
1569        THEN
1570 
1571           RAISE ARP_ALLOCATION_PKG.invalid_allocation_base;
1572 
1573        END IF;
1574 
1575      /*---------------------------------------------------------------------------+
1576       | The deferred tax flag is set by the Bills Receivable Houskeeper. When this|
1577       | is No, it means that there is no Tax accounting impact, as the maturity   |
1578       | date event would have moved the deferred tax. For Transactions and where  |
1579       | the Maturity date event merges with the creation of Receipt application,  |
1580       | the Tax accounting Wrapper routine is called. For Transactions the normal |
1581       | Tax accounting routine is called. Note for Bills Receivable we only move  |
1582       | deferred tax as there is no discount.                                     |
1583       +---------------------------------------------------------------------------*/
1584        IF PG_DEBUG in ('Y', 'C') THEN
1585           arp_standard.debug('g_ae_doc_rec.deferred_tax ' || g_ae_doc_rec.deferred_tax);
1586           arp_standard.debug('p_cust_inv_rec.drawee_site_use_id '
1587                                                                              || p_cust_inv_rec.drawee_site_use_id);
1588        END IF;
1589 
1590        IF (nvl(g_ae_doc_rec.deferred_tax,'Y') = 'Y') THEN
1591 
1592          IF p_cust_inv_rec.drawee_site_use_id IS NULL THEN --check whether adjustment to bill
1593 
1594             -- Added parameter for Line level Adjustment
1595 	    ARP_ALLOCATION_PKG.Allocate_Tax(
1596                         p_ae_doc_rec           => g_ae_doc_rec   ,     --Document detail
1597                         p_ae_event_rec         => g_ae_event_rec ,     --Event record
1598                         p_ae_rule_rec          => p_rule_rec     ,     --Rule info for payment method
1599                         p_app_rec              => l_app_rec      ,     --dummy applications record
1600                         p_cust_inv_rec         => p_cust_inv_rec ,     --Invoice details
1601                         p_adj_rec              => p_adj_rec      ,     --adjustment details
1602                         p_ae_ctr               => l_ae_ctr       ,     --counter
1603                         p_ae_line_tbl          => l_ae_line_tbl  ,     --final tax accounting table
1604 			p_from_llca_call => p_from_llca_call     ,     --Line Adj detail
1605                         p_gt_id          => p_gt_id);
1606          ELSE /*---------------------------------------------------------------------+
1607                | This condition occurs for endorsments without recourse only since   |
1608                | adjustment is gross to activity, hence the Wrapper is called to     |
1609                | move deferred tax only. The parent adjustment must be of type       |
1610                | INVOICE. The wrapper breaks the adjustment into sub adjustments     |
1611                | for each assignment. This is necessary to enable accurate reporting |
1612                | of taxable amount for deferred tax using link ids. So in this case  |
1613                | we cannot simply create one accounting entry for gross to activity  |
1614                | gl account.                                                         |
1615                +---------------------------------------------------------------------*/
1616             ARP_BR_ALLOC_WRAPPER_PKG.Allocate_Tax_BR_Main(
1617                         p_mode                 => g_mode         ,     --Mode
1618                         p_ae_doc_rec           => g_ae_doc_rec   ,     --Document detail
1619                         p_ae_event_rec         => g_ae_event_rec ,     --Event record
1620                         p_ae_rule_rec          => p_rule_rec     ,     --Rule info for payment method
1621                         p_app_rec              => l_app_rec      ,     --Application details
1622                         p_cust_inv_rec         => p_cust_inv_rec ,     --Invoice details
1623                         p_adj_rec              => p_adj_rec      ,     --adjustment record
1624                         p_ae_sys_rec           => g_ae_sys_rec   ,     --system parameters
1625                         p_ae_ctr               => l_ae_ctr       ,     --counter
1626                         p_ae_line_tbl          => l_ae_line_tbl    ); --final tax accounting table
1627 
1628          END IF; --adjustment to Bill or Transaction
1629        ELSE --adjustments with recourse (when Maturity date not same as risk eliminate event
1630        /*-----------------------------------------------------------------------+
1631         | Do not call the Tax accounting to move deferred tax this happens when |
1632         | the Bills Receivable Housekeeper determines that the maturity date    |
1633         | event is seperate from the creation of the adjustment, for approved   |
1634         | Endorsments, we need to update the link id  so the last Transaction   |
1635         | History Record must be for Matured Pending Risk elimination Endorsment|
1636         +-----------------------------------------------------------------------*/
1637           update ar_adjustments
1638           set link_to_trx_hist_id   = (select max(th.transaction_history_id)
1639                                        from ar_transaction_history th
1640                                        where th.customer_trx_id = p_adj_rec.customer_trx_id
1641                                        and th.event = 'MATURITY_DATE'
1642                                        and exists (select 'x'
1643                                                    from ar_distributions ard
1644                                                    where ard.source_id = th.transaction_history_id
1645                                                    and ard.source_table = 'TH'))
1646           where adjustment_id = p_adj_rec.adjustment_id;
1647 
1648         /*-----------------------------------------------------------------------+
1649          | Create the Release 11 Writeoff to the Activity GL Account. Note that  |
1650          | the deferred tax would already have been moved at the maturity date   |
1651          | event, so we do not call the Tax accounting engine as the only        |
1652          | accounting allowed is Gross to Activity Gl Account i.e. GL ACCOUNT    |
1653          | SOURCE = 'ACTIVITY GL ACCOUNT', TAX CODE SOURCE = 'NONE', in this case|
1654          | a single accounting entry is created.
1655          +-----------------------------------------------------------------------*/
1656           IF p_cust_inv_rec.drawee_site_use_id IS NOT NULL THEN
1657 
1658              l_ael_line_rec.ae_line_type  := 'ADJ';
1659              l_ael_line_rec.account       := p_rule_rec.code_combination_id1; --ccid for writeoff
1660 
1661              IF ( sign( p_adj_rec.amount ) = -1 ) THEN   -- Dr the Write-off account
1662 
1663                 l_ael_line_rec.entered_cr   := NULL;
1664                 l_ael_line_rec.entered_dr   := abs(p_adj_rec.amount);
1665 
1666                 l_ael_line_rec.accounted_cr := NULL;
1667                 l_ael_line_rec.accounted_dr := abs(p_adj_rec.acctd_amount);
1668 
1669              ELSE   -- Cr the Write-off account
1670 
1671                 l_ael_line_rec.entered_dr   := NULL;
1672                 l_ael_line_rec.entered_cr   := p_adj_rec.amount;
1673 
1674                 l_ael_line_rec.accounted_dr := NULL;
1675                 l_ael_line_rec.accounted_cr := p_adj_rec.acctd_amount;
1676 
1677              END IF;
1678 
1679           /*-------------------------------------------------------------+
1680            | Assign AEL for ADJ Writeoff for adjustment                  |
1681            +-------------------------------------------------------------*/
1682              Assign_Ael_Elements( p_ae_line_rec 	=> l_ael_line_rec );
1683 
1684          END IF; --End if document is a bills receivable
1685 
1686        END IF; --End if deferred tax required to be moved
1687 
1688       /*-------------------------------------------------------------+
1689        | Assign AEL for Tax accounting                               |
1690        +-------------------------------------------------------------*/
1691        IF l_ae_line_tbl.EXISTS(l_ae_ctr) THEN --Atleast one Tax line exists
1692 
1693          FOR l_ctr IN l_ae_line_tbl.FIRST .. l_ae_line_tbl.LAST LOOP
1694 
1695              --It is necessary to populate the record and then call assign elements
1696              --because of standards and that the User Hook could override accounting
1697              --so need to populate this record (rather than direct table assignments)
1698 
1699              l_ael_line_rec := l_empty_ael_line_rec;
1700              l_ael_line_rec := l_ae_line_tbl(l_ctr);
1701 
1702            /*--------------------------------------------------------------+
1703             | Asign AEL for tax accounting for adjustments, finance charges|
1704             +--------------------------------------------------------------*/
1705              Assign_Ael_Elements( p_ae_line_rec         => l_ael_line_rec );
1706 
1707           END LOOP; --process tax lines
1708 
1709       END IF; --atleast one tax line exists
1710 
1711     ELSE --if adjustment is a zero amount adjustment then Credit the Write off account
1712 
1713        IF PG_DEBUG in ('Y', 'C') THEN
1714           arp_standard.debug('Processing Credit to Write Off for 0 amount adjustment ');
1715        END IF;
1716 
1717      --Substitute the balancing segment
1718      --Bugfix 1948917.
1719      --BugFix7125756
1720 
1721      l_subs_bal_seg := 'N';
1722      IF p_adj_rec.type = 'TAX' THEN
1723        IF (((p_rule_rec.tax_code_source1 = 'INVOICE') AND (p_rule_rec.tax_recoverable_flag1 = 'Y'))
1724            OR (p_rule_rec.tax_code_source1 = 'NONE') )THEN
1725             select min(code_combination_id)
1726             into l_code_combination_id
1727             from ra_cust_trx_line_gl_dist ctlgd
1728             where ctlgd.account_class = 'TAX'
1729             and   ctlgd.account_set_flag = 'N'
1730             and   ctlgd.customer_trx_id = p_adj_rec.customer_trx_id;
1731 
1732           l_subs_bal_seg := 'N';
1733        ELSIF (p_rule_rec.tax_code_source1 = 'ACTIVITY')
1734              AND (p_rule_rec.asset_tax_code1 IS NOT NULL) THEN
1735                l_code_combination_id := p_rule_rec.act_tax_non_rec_ccid1;
1736                l_subs_bal_seg := 'Y';
1737        ELSIF p_rule_rec.tax_code_source1 = 'INVOICE' AND p_rule_rec.tax_recoverable_flag1 = 'N' THEN
1738                select
1739                min(decode(alv.location_segment_id,
1740                        '',avt.adj_non_rec_tax_ccid,
1741                        alv.adj_non_rec_tax_ccid))
1742                into l_code_combination_id
1743                FROM ra_customer_trx_lines     ctl,
1744                     ar_vat_tax                avt,
1745                     ar_location_accounts      alv
1746                where ctl.customer_trx_id = p_adj_rec.customer_trx_id
1747                and   ctl.line_type = 'TAX'
1748                and   ctl.location_segment_id  = alv.location_segment_id(+)
1749                and   ctl.vat_tax_id           = avt.vat_tax_id(+);
1750 
1751 
1752                l_subs_bal_seg := 'Y';
1753 
1754        END IF;
1755        IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N'
1756           AND l_subs_bal_seg = 'Y' THEN
1757               ARP_ALLOCATION_PKG.Substitute_Ccid(
1758                   p_coa_id        => g_ae_sys_rec.coa_id            ,
1759                   p_original_ccid => l_code_combination_id,
1760                   p_subs_ccid     => l_ael_line_rec.account         , --Rec account
1761                   p_actual_ccid   => l_actual_ccid                   );
1762        ELSE
1763               l_actual_ccid := l_code_combination_id; --p_rule_rec.code_combination_id1;
1764        END IF;
1765      ELSE
1766    --END BUGFIX7125756
1767      IF NVL(FND_PROFILE.value('AR_DISABLE_REC_ACTIVITY_BALSEG_SUBSTITUTION'), 'N') = 'N' THEN
1768        ARP_ALLOCATION_PKG.Substitute_Ccid(
1769                   p_coa_id        => g_ae_sys_rec.coa_id            ,
1770                   p_original_ccid => p_rule_rec.code_combination_id1,
1771                   p_subs_ccid     => l_ael_line_rec.account         , --Rec account
1772                   p_actual_ccid   => l_actual_ccid                   );
1773      ELSE
1774        l_actual_ccid := p_rule_rec.code_combination_id1;
1775      END IF;
1776     END IF;
1777      --Set the actual account
1778        l_ael_line_rec.account := l_actual_ccid;
1779 
1780      --Set the source type
1781        IF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'ADJUSTMENT' THEN
1782 
1783            l_ael_line_rec.ae_line_type := 'ADJ';
1784 
1785       /*----------------------------------------------------------------------------+
1786        | Populate source type for finance charges                                   |
1787        +----------------------------------------------------------------------------*/
1788        ELSIF g_ae_doc_rec.source_table = 'ADJ' and g_ae_doc_rec.document_type = 'FINANCE_CHARGES' THEN
1789 
1790              l_ael_line_rec.ae_line_type := 'FINCHRG';
1791 
1792        END IF;
1793 
1794      --Set the amounts and accounted amounts
1795        l_ael_line_rec.entered_cr   := abs(p_adj_rec.amount);
1796        l_ael_line_rec.entered_dr   := NULL;
1797 
1798        l_ael_line_rec.accounted_cr := abs(p_adj_rec.acctd_amount);
1799        l_ael_line_rec.accounted_dr := NULL;
1800 
1801      /*-------------------------------------------------------------+
1802       | Assign AEL for REC for Invoice document for adjustment      |
1803       +-------------------------------------------------------------*/
1804        Assign_Ael_Elements( p_ae_line_rec       => l_ael_line_rec );
1805 
1806    END IF; --adjustment amount is non zero
1807 
1808 /*------------------------------------------------------------------------------------+
1809  |Call the Reconciliation routine, this is necessary because the transaction or Bill  |
1810  |which may have been overapplied is now closed due to adjustment or overapplied      |
1811  |may have resulted in re-opening the transaction, hence we need to back out NOCOPY the old  |
1812  |reconciliation entries for the Bill or the Transactions                             |
1813  +------------------------------------------------------------------------------------*/
1814    ARP_RECONCILE.Reconcile_trx_br(
1815                       p_mode                     => g_mode                             ,
1816                       p_ae_doc_rec               => g_ae_doc_rec                       ,
1817                       p_ae_event_rec             => g_ae_event_rec                     ,
1818                       p_cust_inv_rec             => p_cust_inv_rec                     ,
1819                       p_activity_cust_trx_id     => p_adj_rec.customer_trx_id          ,
1820                       p_activity_amt             => p_adj_rec.amount                   ,
1821                       p_activity_acctd_amt       => p_adj_rec.acctd_amount             ,
1822                       p_call_num                 => 1                                  ,
1823                       p_g_ae_line_tbl            => g_ae_line_tbl                      ,
1824                       p_g_ae_ctr                 => g_ae_line_ctr                        );
1825 
1826    IF PG_DEBUG in ('Y', 'C') THEN
1827       arp_standard.debug('Create_Ae_Lines_ADJ.Create_Ae_Lines_ADJ()-');
1828    END IF;
1829 
1830 EXCEPTION
1831   WHEN ARP_ALLOCATION_PKG.invalid_allocation_base THEN
1832      IF PG_DEBUG in ('Y', 'C') THEN
1833         arp_standard.debug('Create_Ae_Lines_ADJ.Create_Ae_Lines_ADJ - invalid_rule_error');
1834      END IF;
1835      fnd_message.set_name('AR','AR_INVALID_ACTIVITY');
1836      RAISE;
1837 
1838   WHEN NO_DATA_FOUND THEN
1839      IF PG_DEBUG in ('Y', 'C') THEN
1840         arp_standard.debug('NO_DATA_FOUND' );
1841      END IF;
1842      RAISE;
1843 
1844   WHEN OTHERS THEN
1845      IF PG_DEBUG in ('Y', 'C') THEN
1846         arp_standard.debug('EXCEPTION: Create_Ae_Lines_ADJ.Create_Ae_Lines_ADJ');
1847      END IF;
1848      RAISE;
1849 
1850 END Create_Ae_Lines_ADJ;
1851 
1852 /* =======================================================================
1853  | PROCEDURE Assign_Ael_Elements
1854  |
1855  | DESCRIPTION
1856  |      This procedure stores the AE Line record into AE Lines PLSQL table.
1857  |      Functions:
1858  |              - Determine regular or negative Dr/Cr.
1859  |              - Store AE Line Record in AE Lines PLSQL Table.
1860  |              - In a fully implemented SLA model, Will determine the
1861  |                account to use based on AE Line type and other parameters.
1862  |              - In a fully implemented SLA model, Will determine the
1863  |                account descriptions.
1864  |
1865  | GUIDELINE
1866  |      - This procedure can be shared across document types
1867  |      - Recommendation is to have one per document type(AE Derivation)
1868  |
1869  | PARAMETERS
1870  |      p_ae_line_rec     AE Line Record
1871  * ======================================================================*/
1872 PROCEDURE Assign_Ael_Elements(
1873                	p_ae_line_rec 		IN ae_line_rec_type ) IS
1874 
1875   l_account			NUMBER;
1876   l_account_valid		BOOLEAN;
1877   l_replace_default_account	BOOLEAN;
1878 
1879 BEGIN
1880 
1881     IF PG_DEBUG in ('Y', 'C') THEN
1882        arp_standard.debug( 'ARP_ADJUSTMENTS_MAIN.Assign_Ael_Elements()+');
1883     END IF;
1884 
1885    /*------------------------------------------------------+
1886     | Call Hook to Override Account                        |
1887     +------------------------------------------------------*/
1888     ARP_ACCT_HOOK.Override_Account(
1889         	p_mode                    => g_mode,
1890         	p_ae_doc_rec              => g_ae_doc_rec,
1891         	p_ae_event_rec            => g_ae_event_rec,
1892 		p_ae_line_rec		  => p_ae_line_rec,
1893 		p_account		  => l_account,
1894 		p_account_valid		  => l_account_valid,
1895 		p_replace_default_account => l_replace_default_account
1896 		);
1897 
1898     IF ( NOT l_replace_default_account ) THEN
1899 
1900       /*------------------------------------------------------+
1901        | SLA : Build Account for AE Line Type                 |
1902        |       When SLA is fully implemented Account Builder  |
1903        |       will be called from here.                      |
1904        +------------------------------------------------------*/
1905        l_account := p_ae_line_rec.account;
1906 
1907     END IF;		-- Replace default account?
1908 
1909    /*------------------------------------------------------+
1910     | SLA : Build Account description for AE Line Type     |
1911     |       When SLA is fully implemented Description 	   |
1912     |       builder will be called from here.              |
1913     +------------------------------------------------------*/
1914 
1915    /*------------------------------------------------------+
1916     | SLA : Check Negative Dr/Cr for AE Line 		   |
1917     |       When SLA is fully implemented.            	   |
1918     +------------------------------------------------------*/
1919 
1920    /*------------------------------------------------------+
1921     | Store AE Line elements in AE Lines temp table        |
1922     +------------------------------------------------------*/
1923     g_ae_line_ctr := g_ae_line_ctr +1;
1924 
1925     g_ae_line_tbl(g_ae_line_ctr).ae_line_type             :=  p_ae_line_rec.ae_line_type;
1926     g_ae_line_tbl(g_ae_line_ctr).ae_line_type_secondary   :=  p_ae_line_rec.ae_line_type_secondary;
1927     g_ae_line_tbl(g_ae_line_ctr).source_id                :=  p_ae_line_rec.source_id;
1928     g_ae_line_tbl(g_ae_line_ctr).source_table             :=  p_ae_line_rec.source_table;
1929     g_ae_line_tbl(g_ae_line_ctr).account                  :=  p_ae_line_rec.account;
1930     g_ae_line_tbl(g_ae_line_ctr).entered_dr               :=  p_ae_line_rec.entered_dr;
1931     g_ae_line_tbl(g_ae_line_ctr).entered_cr               :=  p_ae_line_rec.entered_cr;
1932     g_ae_line_tbl(g_ae_line_ctr).accounted_dr             :=  p_ae_line_rec.accounted_dr;
1933     g_ae_line_tbl(g_ae_line_ctr).accounted_cr             :=  p_ae_line_rec.accounted_cr;
1934     g_ae_line_tbl(g_ae_line_ctr).source_id_secondary      :=  p_ae_line_rec.source_id_secondary;
1935     g_ae_line_tbl(g_ae_line_ctr).source_table_secondary   :=  p_ae_line_rec.source_table_secondary;
1936     g_ae_line_tbl(g_ae_line_ctr).currency_code            :=  p_ae_line_rec.currency_code;
1937     g_ae_line_tbl(g_ae_line_ctr).currency_conversion_rate :=  p_ae_line_rec.currency_conversion_rate;
1938     g_ae_line_tbl(g_ae_line_ctr).currency_conversion_type :=  p_ae_line_rec.currency_conversion_type;
1939     g_ae_line_tbl(g_ae_line_ctr).currency_conversion_date :=  p_ae_line_rec.currency_conversion_date;
1940     g_ae_line_tbl(g_ae_line_ctr).third_party_id           :=  p_ae_line_rec.third_party_id;
1941     g_ae_line_tbl(g_ae_line_ctr).third_party_sub_id       :=  p_ae_line_rec.third_party_sub_id;
1942     g_ae_line_tbl(g_ae_line_ctr).tax_group_code_id        :=  p_ae_line_rec.tax_group_code_id;
1943     g_ae_line_tbl(g_ae_line_ctr).tax_code_id              :=  p_ae_line_rec.tax_code_id;
1944     g_ae_line_tbl(g_ae_line_ctr).location_segment_id      :=  p_ae_line_rec.location_segment_id;
1945     g_ae_line_tbl(g_ae_line_ctr).taxable_entered_dr       :=  p_ae_line_rec.taxable_entered_dr;
1946     g_ae_line_tbl(g_ae_line_ctr).taxable_entered_cr       :=  p_ae_line_rec.taxable_entered_cr;
1947     g_ae_line_tbl(g_ae_line_ctr).taxable_accounted_dr     :=  p_ae_line_rec.taxable_accounted_dr;
1948     g_ae_line_tbl(g_ae_line_ctr).taxable_accounted_cr     :=  p_ae_line_rec.taxable_accounted_cr;
1949     g_ae_line_tbl(g_ae_line_ctr).applied_from_doc_table   :=  p_ae_line_rec.applied_from_doc_table;
1950     g_ae_line_tbl(g_ae_line_ctr).applied_from_doc_id      :=  p_ae_line_rec.applied_from_doc_id;
1951     g_ae_line_tbl(g_ae_line_ctr).applied_to_doc_table     :=  p_ae_line_rec.applied_to_doc_table;
1952     g_ae_line_tbl(g_ae_line_ctr).applied_to_doc_id        :=  p_ae_line_rec.applied_to_doc_id;
1953     g_ae_line_tbl(g_ae_line_ctr).tax_link_id              :=  p_ae_line_rec.tax_link_id;
1954     g_ae_line_tbl(g_ae_line_ctr).reversed_source_id       :=  p_ae_line_rec.reversed_source_id;
1955     --{3377004
1956     g_ae_line_tbl(g_ae_line_ctr).ref_line_id              :=  p_ae_line_rec.ref_line_id;
1957     g_ae_line_tbl(g_ae_line_ctr).ref_customer_trx_line_id :=  p_ae_line_rec.ref_customer_trx_line_id;
1958     g_ae_line_tbl(g_ae_line_ctr).ref_cust_trx_line_gl_dist_id :=  p_ae_line_rec.ref_cust_trx_line_gl_dist_id;
1959     --}
1960     g_ae_line_tbl(g_ae_line_ctr).ref_account_class	  :=  p_ae_line_rec.ref_account_class;
1961     g_ae_line_tbl(g_ae_line_ctr).activity_bucket	  :=  p_ae_line_rec.activity_bucket;
1962     g_ae_line_tbl(g_ae_line_ctr).ref_dist_ccid		  :=  p_ae_line_rec.ref_dist_ccid;
1963 
1964     IF PG_DEBUG in ('Y', 'C') THEN
1965        arp_standard.debug( 'ARP_ADJUSTMENTS_MAIN.Assign_Ael_Elements()-');
1966     END IF;
1967 
1968 EXCEPTION
1969   WHEN OTHERS THEN
1970      IF PG_DEBUG in ('Y', 'C') THEN
1971         arp_standard.debug('EXCEPTION: ARP_ADJUSTMENTS_MAIN.Assign_Ael_Elements');
1972      END IF;
1973      RAISE;
1974 
1975 END Assign_Ael_Elements;
1976 
1977 --{3377004
1978 FUNCTION ctl_id_index(p_ctl_id_tab  IN DBMS_SQL.NUMBER_TABLE,
1979                       p_ctl_id      IN NUMBER)
1980 RETURN NUMBER
1981 IS
1982   result  NUMBER;
1983   cnt     NUMBER := 0;
1984 BEGIN
1985   arp_standard.debug('ctl_id_index+');
1986   result := -1;
1987   cnt := p_ctl_id_tab.COUNT;
1988   IF cnt > 0 THEN
1989     FOR i IN p_ctl_id_tab.FIRST .. p_ctl_id_tab.LAST LOOP
1990       IF p_ctl_id_tab(i) = p_ctl_id THEN
1991         result := i;
1992         EXIT;
1993       END IF;
1994     END LOOP;
1995   ELSE
1996     result := 1;
1997   END IF;
1998   IF result = -1 THEN
1999      result := cnt + 1;
2000   END IF;
2001   arp_standard.debug('  result index:'||result);
2002   arp_standard.debug('ctl_id_index-');
2003   RETURN result;
2004 END;
2005 
2006 PROCEDURE init_rem_amt(x_rem_amt IN OUT NOCOPY ctl_rem_amt_type,
2007                        p_index   IN NUMBER)
2008 IS
2009 BEGIN
2010   x_rem_amt.customer_trx_line_id(p_index) := 0;
2011   x_rem_amt.amount_due_remaining(p_index) := 0;
2012   x_rem_amt.acctd_amount_due_remaining(p_index) := 0;
2013   x_rem_amt.chrg_amount_remaining(p_index) := 0;
2014   x_rem_amt.chrg_acctd_amount_remaining(p_index) := 0;
2015 END;
2016 --}
2017 
2018 END ARP_ADJUSTMENTS_MAIN;